-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.go
More file actions
365 lines (331 loc) · 7.62 KB
/
Copy pathprotocol.go
File metadata and controls
365 lines (331 loc) · 7.62 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package imc
import (
"bytes"
"encoding/binary"
"fmt"
"time"
)
// Message is a generic IMC message holding fields in a map.
type Message struct {
Header Header
Fields map[string]any
}
type Header struct {
Sync uint16
MGID uint16
Size uint16
Timestamp float64
Src uint16
SrcEnt uint8
Dst uint16
DstEnt uint8
}
type Protocol struct {
Version string
SyncWord uint16
Messages map[uint16]*XMLMessage
Lookup map[string]*XMLMessage
}
func NewProtocol(xmlProto *XMLProtocol) *Protocol {
p := &Protocol{
Version: xmlProto.Version,
SyncWord: 0xFE55, // Default
Messages: make(map[uint16]*XMLMessage),
Lookup: make(map[string]*XMLMessage),
}
for _, field := range xmlProto.Header {
if field.Abbrev == "sync" {
var val uint64
if _, err := fmt.Sscanf(field.Value, "0x%x", &val); err == nil {
p.SyncWord = uint16(val)
} else {
fmt.Sscanf(field.Value, "%d", &val)
p.SyncWord = uint16(val)
}
break
}
}
for i := range xmlProto.Messages {
msg := &xmlProto.Messages[i]
p.Messages[msg.ID] = msg
p.Lookup[msg.Abbrev] = msg
}
return p
}
func (p *Protocol) CreateMessage(abbrev string) (*Message, error) {
msgDef, ok := p.Lookup[abbrev]
if !ok {
return nil, fmt.Errorf("message %s not found in protocol", abbrev)
}
return &Message{
Header: Header{
Sync: p.SyncWord,
MGID: msgDef.ID,
},
Fields: make(map[string]any),
}, nil
}
func (p *Protocol) GetField(mgid uint16, abbrev string) *XMLField {
msgDef, ok := p.Messages[mgid]
if !ok {
return nil
}
for i := range msgDef.Fields {
if msgDef.Fields[i].Abbrev == abbrev {
return &msgDef.Fields[i]
}
}
return nil
}
// Serialization and Deserialization implementation will go here.
// I'll need a way to map IMC types (uint8_t, fp64_t, plaintext, etc.) to binary.
func (m *Message) Serialize(p *Protocol) ([]byte, error) {
payload, err := m.SerializePayload(p)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
m.Header.Size = uint16(len(payload))
m.Header.Sync = p.SyncWord
if m.Header.Timestamp == 0 {
m.Header.Timestamp = float64(time.Now().UnixNano()) / 1e9
}
// Write Header
binary.Write(buf, binary.LittleEndian, m.Header.Sync)
binary.Write(buf, binary.LittleEndian, m.Header.MGID)
binary.Write(buf, binary.LittleEndian, m.Header.Size)
binary.Write(buf, binary.LittleEndian, m.Header.Timestamp)
binary.Write(buf, binary.LittleEndian, m.Header.Src)
binary.Write(buf, binary.LittleEndian, m.Header.SrcEnt)
binary.Write(buf, binary.LittleEndian, m.Header.Dst)
binary.Write(buf, binary.LittleEndian, m.Header.DstEnt)
// Write Payload
buf.Write(payload)
// Write Footer (CRC)
crc := CalculateCRC16(buf.Bytes())
binary.Write(buf, binary.LittleEndian, crc)
return buf.Bytes(), nil
}
func (m *Message) SerializePayload(p *Protocol) ([]byte, error) {
msgDef, ok := p.Messages[m.Header.MGID]
if !ok {
return nil, fmt.Errorf("message ID %d not found in protocol", m.Header.MGID)
}
buf := new(bytes.Buffer)
for _, field := range msgDef.Fields {
val := m.Fields[field.Abbrev]
if err := p.writeField(buf, field.Type, val); err != nil {
return nil, fmt.Errorf("failed to write field %s: %w", field.Abbrev, err)
}
}
return buf.Bytes(), nil
}
func (p *Protocol) writeField(buf *bytes.Buffer, typ string, val any) error {
switch typ {
case "int8_t":
return binary.Write(buf, binary.LittleEndian, ToInt8(val))
case "uint8_t":
return binary.Write(buf, binary.LittleEndian, ToUint8(val))
case "int16_t":
return binary.Write(buf, binary.LittleEndian, ToInt16(val))
case "uint16_t":
return binary.Write(buf, binary.LittleEndian, ToUint16(val))
case "int32_t":
return binary.Write(buf, binary.LittleEndian, ToInt32(val))
case "uint32_t":
return binary.Write(buf, binary.LittleEndian, ToUint32(val))
case "int64_t":
return binary.Write(buf, binary.LittleEndian, ToInt64(val))
case "fp32_t":
return binary.Write(buf, binary.LittleEndian, ToFloat32(val))
case "fp64_t":
return binary.Write(buf, binary.LittleEndian, ToFloat64(val))
case "plaintext":
s := ToString(val)
binary.Write(buf, binary.LittleEndian, uint16(len(s)))
buf.WriteString(s)
return nil
case "rawdata":
d := ToByteSlice(val)
binary.Write(buf, binary.LittleEndian, uint16(len(d)))
buf.Write(d)
return nil
case "message":
msg := ToGenericMessage(val)
if msg == nil {
return binary.Write(buf, binary.LittleEndian, uint16(0xFFFF))
}
if err := binary.Write(buf, binary.LittleEndian, msg.Header.MGID); err != nil {
return err
}
data, err := msg.SerializePayload(p)
if err != nil {
return err
}
_, err = buf.Write(data)
return err
case "message-list":
msgs := ToGenericMessageList(val)
if err := binary.Write(buf, binary.LittleEndian, uint16(len(msgs))); err != nil {
return err
}
for _, m := range msgs {
if m == nil {
binary.Write(buf, binary.LittleEndian, uint16(0xFFFF))
continue
}
binary.Write(buf, binary.LittleEndian, m.Header.MGID)
data, err := m.SerializePayload(p)
if err != nil {
return err
}
buf.Write(data)
}
return nil
default:
return fmt.Errorf("unknown type: %s", typ)
}
}
// Helpers to handle various input types for values
func ToUint8(v any) uint8 {
if x, ok := v.(uint8); ok {
return x
}
return uint8(ToInt64(v))
}
func ToInt8(v any) int8 {
if x, ok := v.(int8); ok {
return x
}
return int8(ToInt64(v))
}
func ToUint16(v any) uint16 {
if x, ok := v.(uint16); ok {
return x
}
return uint16(ToInt64(v))
}
func ToInt16(v any) int16 {
if x, ok := v.(int16); ok {
return x
}
return int16(ToInt64(v))
}
func ToUint32(v any) uint32 {
if x, ok := v.(uint32); ok {
return x
}
return uint32(ToInt64(v))
}
func ToInt32(v any) int32 {
if x, ok := v.(int32); ok {
return x
}
return int32(ToInt64(v))
}
func ToInt64(v any) int64 {
switch x := v.(type) {
case int:
return int64(x)
case int8:
return int64(x)
case int16:
return int64(x)
case int32:
return int64(x)
case int64:
return x
case uint8:
return int64(x)
case uint16:
return int64(x)
case uint32:
return int64(x)
case float64:
return int64(x)
}
return 0
}
func ToFloat32(v any) float32 {
if x, ok := v.(float32); ok {
return x
}
return float32(ToFloat64(v))
}
func ToFloat64(v any) float64 {
switch x := v.(type) {
case float64:
return x
case float32:
return float64(x)
case int:
return float64(x)
case int8:
return float64(x)
case int16:
return float64(x)
case int32:
return float64(x)
case int64:
return float64(x)
case uint8:
return float64(x)
case uint16:
return float64(x)
case uint32:
return float64(x)
}
return 0
}
func ToString(v any) string {
if x, ok := v.(string); ok {
return x
}
return ""
}
func ToByteSlice(v any) []byte {
if x, ok := v.([]byte); ok {
return x
}
return nil
}
func ToGenericMessage(v any) *Message {
if v == nil {
return nil
}
if m, ok := v.(*Message); ok {
return m
}
if im, ok := v.(IMessage); ok {
return im.ToMessage()
}
return nil
}
func ToGenericMessageList(v any) []*Message {
if v == nil {
return nil
}
if ms, ok := v.([]*Message); ok {
return ms
}
// Use reflection or type assertion if we know the types.
// Since we use slice of pointer to structs, we can't easily assert to []IMessage.
// But the generator can be made to emit []*Message for simplicity.
return nil
}
func CalculateCRC16(data []byte) uint16 {
var crc uint16 = 0x0000
// CRC-16-IBM (poly 0x8005, reflected as 0xA001, init 0x0000)
// Must match DUNE's Algorithms::CRC16::compute() which uses init=0.
for _, b := range data {
crc ^= uint16(b)
for i := 0; i < 8; i++ {
if (crc & 0x0001) != 0 {
crc = (crc >> 1) ^ 0xA001
} else {
crc >>= 1
}
}
}
return crc
}