forked from bluenviron/gomavlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe.go
More file actions
153 lines (131 loc) · 3.46 KB
/
frame.go
File metadata and controls
153 lines (131 loc) · 3.46 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package gomavlib
const (
v1MagicByte = 0xfe
v2MagicByte = 0xfd
flagSigned = 0x01
)
// Frame is the interface implemented by frames of every supported version.
type Frame interface {
// the frame version.
GetVersion() int
// the system id of the author of the frame.
GetSystemId() byte
// the component id of the author of the frame.
GetComponentId() byte
// the message encapsuled in the frame.
GetMessage() Message
// the frame checksum.
GetChecksum() uint16
// generate a clone of the frame
Clone() Frame
// the sequence id of the frame
GetSequenceId() byte
}
// FrameV1 represents a 1.0 frame.
type FrameV1 struct {
SequenceId byte
SystemId byte
ComponentId byte
Message Message
Checksum uint16
}
// GetSequenceId returns the SequenceId of the frame
func (f *FrameV1) GetSequenceId() byte {
return f.SequenceId
}
// Clone is part of the Frame interface.
func (f *FrameV1) Clone() Frame {
return &FrameV1{
SequenceId: f.SequenceId,
SystemId: f.SystemId,
ComponentId: f.ComponentId,
Message: f.Message,
Checksum: f.Checksum,
}
}
// GetVersion is part of the Frame interface.
func (f *FrameV1) GetVersion() int {
return 1
}
// GetSystemId is part of the Frame interface.
func (f *FrameV1) GetSystemId() byte {
return f.SystemId
}
// GetComponentId is part of the Frame interface.
func (f *FrameV1) GetComponentId() byte {
return f.ComponentId
}
// GetMessage is part of the Frame interface.
func (f *FrameV1) GetMessage() Message {
return f.Message
}
// GetChecksum is part of the Frame interface.
func (f *FrameV1) GetChecksum() uint16 {
return f.Checksum
}
// FrameV2 represents a 2.0 frame.
type FrameV2 struct {
IncompatibilityFlag byte
CompatibilityFlag byte
SequenceId byte
SystemId byte
ComponentId byte
Message Message
Checksum uint16
SignatureLinkId byte
SignatureTimestamp uint64
Signature *Signature
}
// GetSequenceId returns the SequenceId of the frame
func (f *FrameV2) GetSequenceId() byte {
return f.SequenceId
}
// Clone is part of the Frame interface.
func (f *FrameV2) Clone() Frame {
return &FrameV2{
IncompatibilityFlag: f.IncompatibilityFlag,
CompatibilityFlag: f.CompatibilityFlag,
SequenceId: f.SequenceId,
SystemId: f.SystemId,
ComponentId: f.ComponentId,
Message: f.Message,
Checksum: f.Checksum,
SignatureLinkId: f.SignatureLinkId,
SignatureTimestamp: f.SignatureTimestamp,
Signature: f.Signature,
}
}
// GetVersion is part of the Frame interface.
func (f *FrameV2) GetVersion() int {
return 2
}
// GetSystemId is part of the Frame interface.
func (f *FrameV2) GetSystemId() byte {
return f.SystemId
}
// GetComponentId is part of the Frame interface.
func (f *FrameV2) GetComponentId() byte {
return f.ComponentId
}
// GetMessage is part of the Frame interface.
func (f *FrameV2) GetMessage() Message {
return f.Message
}
// GetChecksum is part of the Frame interface.
func (f *FrameV2) GetChecksum() uint16 {
return f.Checksum
}
// IsSigned checks whether the frame contains a signature. It does not validate the signature.
func (f *FrameV2) IsSigned() bool {
return (f.IncompatibilityFlag & flagSigned) != 0
}
// Key is a key able to sign and validate V2 frames.
type Key [32]byte
// NewKey allocates a Key.
func NewKey(in []byte) *Key {
key := new(Key)
copy(key[:], in)
return key
}
// Signature is a V2 frame signature.
type Signature [6]byte