Permalink
Browse files

layers: Add name & getters to LCM

Adds getter for fingerprint of a LCM layer, getter for all Supported
LCM fingerprints and the string representation of the LCM name when
registering a new LCM payload layer.

Co-Authored-By: TacoVox <jonas@derkahler.de>
  • Loading branch information...
2 people authored and gconnell committed May 24, 2018
1 parent d26664f commit 79cff32cd2cb4b955635abee1ed8a632133d3420
Showing with 26 additions and 6 deletions.
  1. +26 −6 layers/lcm.go
@@ -43,13 +43,18 @@ type LCM struct {
ChannelName string
// Gopacket helper fields
Fragmented bool
fingerprint uint64
fingerprint LCMFingerprint
contents []byte
payload []byte
}
// LCMFingerprint is the type of a LCM fingerprint.
type LCMFingerprint uint64
var (
lcmLayerTypes = map[uint64]gopacket.LayerType{}
// lcmLayerTypes contains a map of all LCM fingerprints that we support and
// their LayerType
lcmLayerTypes = map[LCMFingerprint]gopacket.LayerType{}
layerTypeIndex = 1001
)
@@ -58,9 +63,8 @@ var (
// contains and which identifies it uniquely. If num is not the zero value it
// will be used when registering with RegisterLayerType towards gopacket,
// otherwise an incremental value starting from 1001 will be used.
func RegisterLCMLayerType(num int, fingerprint uint64,
func RegisterLCMLayerType(num int, name string, fingerprint LCMFingerprint,
decoder gopacket.Decoder) gopacket.LayerType {
name := fmt.Sprintf("%v", fingerprint)
metadata := gopacket.LayerTypeMetadata{Name: name, Decoder: decoder}
if num == 0 {
@@ -73,9 +77,19 @@ func RegisterLCMLayerType(num int, fingerprint uint64,
return lcmLayerTypes[fingerprint]
}
// SupportedLCMFingerprints returns a slice of all LCM fingerprints that has
// been registered so far.
func SupportedLCMFingerprints() []LCMFingerprint {
fingerprints := make([]LCMFingerprint, 0, len(lcmLayerTypes))
for fp := range lcmLayerTypes {
fingerprints = append(fingerprints, fp)
}
return fingerprints
}
// GetLCMLayerType returns the underlying LCM message's LayerType.
// This LayerType has to be registered by using RegisterLCMLayerType.
func GetLCMLayerType(fingerprint uint64) gopacket.LayerType {
func GetLCMLayerType(fingerprint LCMFingerprint) gopacket.LayerType {
layerType, ok := lcmLayerTypes[fingerprint]
if !ok {
return gopacket.LayerTypePayload
@@ -146,7 +160,8 @@ func (lcm *LCM) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
lcm.ChannelName = string(buffer)
}
lcm.fingerprint = binary.BigEndian.Uint64(data[offset : offset+8])
lcm.fingerprint = LCMFingerprint(
binary.BigEndian.Uint64(data[offset : offset+8]))
lcm.contents = data[:offset]
lcm.payload = data[offset:]
@@ -191,3 +206,8 @@ func (lcm LCM) LayerPayload() []byte {
func (lcm LCM) Payload() []byte {
return lcm.LayerPayload()
}
// Fingerprint returns the LCM fingerprint of the underlying message.
func (lcm LCM) Fingerprint() LCMFingerprint {
return lcm.fingerprint
}

0 comments on commit 79cff32

Please sign in to comment.