ref: bd369765abe0b4b897bb923e6d5cec60338ce769
server/tlv/tlvpackets.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package tlv import ( "fmt" "github.com/pkg/errors" "github.com/pauloavelar/go-tlv/tlv" ) func GetTlvString(data []byte) (string, error) { n, _, err := tlv.DecodeSingle(data) if err != nil { return "", errors.WithStack(err) } return n.String(), nil } func GetNodes(data []byte) (tlv.Nodes, error) { n, _, err := tlv.DecodeSingle(data) if err != nil { return nil, errors.WithStack(err) } return n.GetNodes() } func DecodeTlv(data []byte) (tlv.Tag, []byte, error) { n, _, err := tlv.DecodeSingle(data) if err != nil { return 0, nil, errors.WithStack(err) } return n.Tag, n.Value, nil } func example() { data := []byte{0xF0, 0x02, 0x00, 0x01, 0xFF} n, read, err := tlv.DecodeSingle(data) if err != nil { panic(err) // invalid payload length vs bytes available } fmt.Printf("Read: %d", read) n.String() // returns a base64 representation of the raw message n.GetNodes() // parses the value as TLV and returns a Nodes structure (or error) n.GetUint8() // parses the value as uint8 (returns error if value is too small) n.GetPaddedUint8() // parses the value as uint8 and pads it if too small fmt.Printf("string: %s, tag: %d", n.String(), n.Tag) // all available types: bool, uint8, uint16, uint32, uint64, string, time.Time and Nodes } |