-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
759 lines (615 loc) · 18.7 KB
/
message.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
package acmelib
import (
"fmt"
"slices"
"strings"
)
// MessageID rappresents the ID of a [Message].
// It must be unique within all the messages sended by a [NodeInterface].
type MessageID uint32
// MessagePriority rappresents the priority of a [Message].
// The priorities are very high, high, medium, and low.
// The higher priority has the value 0 and the lower has 3.
type MessagePriority uint32
const (
// MessagePriorityVeryHigh defines a very high priority.
MessagePriorityVeryHigh MessagePriority = iota
// MessagePriorityHigh defines an high priority.
MessagePriorityHigh
// MessagePriorityMedium defines a medium priority.
MessagePriorityMedium
// MessagePriorityLow defines a low priority.
MessagePriorityLow
)
// MessageSendType rappresents the transition type of a [Message].
type MessageSendType int
const (
// MessageSendTypeUnset defines an unset transmission type.
MessageSendTypeUnset MessageSendType = iota
// MessageSendTypeCyclic defines a cyclic transmission type.
MessageSendTypeCyclic
// MessageSendTypeCyclicIfActive defines a cyclic if active transmission type.
MessageSendTypeCyclicIfActive
// MessageSendTypeCyclicAndTriggered defines a cyclic and triggered transmission type.
MessageSendTypeCyclicAndTriggered
// MessageSendTypeCyclicIfActiveAndTriggered defines a cyclic if active and triggered transmission type.
MessageSendTypeCyclicIfActiveAndTriggered
)
func (mst MessageSendType) String() string {
switch mst {
case MessageSendTypeUnset:
return "unset"
case MessageSendTypeCyclic:
return "cyclic"
case MessageSendTypeCyclicIfActive:
return "cyclic_if_active"
case MessageSendTypeCyclicAndTriggered:
return "cyclic_and_triggered"
case MessageSendTypeCyclicIfActiveAndTriggered:
return "cyclic_if_active_and_triggered"
default:
return "unknown"
}
}
// MessageByteOrder rappresents the byte order of the payload of a [Message].
// By default a [MessageByteOrder] of [MessageByteOrderLittleEndian] is used.
type MessageByteOrder int
const (
// MessageByteOrderLittleEndian defines a little endian byte order.
MessageByteOrderLittleEndian MessageByteOrder = iota
// MessageByteOrderBigEndian defines a big endian byte order.
MessageByteOrderBigEndian
)
func (mbo MessageByteOrder) String() string {
switch mbo {
case MessageByteOrderLittleEndian:
return "little-endian"
case MessageByteOrderBigEndian:
return "big-endian"
default:
return "unknown"
}
}
// Message is the representation of data sent by a node thought the bus.
// It holds a list of signals that are contained in the message payload.
type Message struct {
*entity
*withAttributes
senderNodeInt *NodeInterface
signals *set[EntityID, Signal]
signalNames *set[string, EntityID]
signalPayload *signalPayload
sizeByte int
id MessageID
staticCANID CANID
hasStaticCANID bool
priority MessagePriority
byteOrder MessageByteOrder
cycleTime int
sendType MessageSendType
delayTime int
startDelayTime int
receivers *set[EntityID, *NodeInterface]
}
func newMessageFromEntity(ent *entity, id MessageID, sizeByte int) *Message {
return &Message{
entity: ent,
withAttributes: newWithAttributes(),
senderNodeInt: nil,
signals: newSet[EntityID, Signal](),
signalNames: newSet[string, EntityID](),
signalPayload: newSignalPayload(sizeByte * 8),
sizeByte: sizeByte,
id: id,
staticCANID: 0,
hasStaticCANID: false,
priority: MessagePriorityVeryHigh,
byteOrder: MessageByteOrderLittleEndian,
cycleTime: 0,
sendType: MessageSendTypeUnset,
delayTime: 0,
startDelayTime: 0,
receivers: newSet[EntityID, *NodeInterface](),
}
}
// NewMessage creates a new [Message] with the given name, id and size in bytes.
// By default a [MessagePriority] of [MessagePriorityVeryHigh] is used.
func NewMessage(name string, id MessageID, sizeByte int) *Message {
return newMessageFromEntity(newEntity(name, EntityKindMessage), id, sizeByte)
}
func (m *Message) hasSenderNodeInt() bool {
return m.senderNodeInt != nil
}
func (m *Message) errorf(err error) error {
msgErr := &EntityError{
Kind: EntityKindMessage,
EntityID: m.entityID,
Name: m.name,
Err: err,
}
if m.hasSenderNodeInt() {
return m.senderNodeInt.errorf(msgErr)
}
return msgErr
}
func (m *Message) verifySignalName(name string) error {
err := m.signalNames.verifyKeyUnique(name)
if err != nil {
return &NameError{
Name: name,
Err: err,
}
}
return nil
}
func (m *Message) verifySignalSizeAmount(sigID EntityID, amount int) error {
if amount == 0 {
return nil
}
sig, err := m.signals.getValue(sigID)
if err != nil {
return err
}
if amount > 0 {
if err := m.signalPayload.verifyBeforeGrow(sig, amount); err != nil {
return &SignalSizeError{
Size: sig.GetSize() + amount,
Err: err,
}
}
return nil
}
if err := m.signalPayload.verifyBeforeShrink(sig, -amount); err != nil {
return &SignalSizeError{
Size: sig.GetSize() + amount,
Err: err,
}
}
return nil
}
func (m *Message) modifySignalSize(sigID EntityID, amount int) error {
if amount == 0 {
return nil
}
sig, err := m.signals.getValue(sigID)
if err != nil {
return err
}
if amount > 0 {
return m.signalPayload.modifyStartBitsOnGrow(sig, amount)
}
return m.signalPayload.modifyStartBitsOnShrink(sig, -amount)
}
func (m *Message) stringify(b *strings.Builder, tabs int) {
m.entity.stringify(b, tabs)
tabStr := getTabString(tabs)
if m.id != 0 {
b.WriteString(fmt.Sprintf("%smessage_id: %d\n", tabStr, m.id))
}
b.WriteString(fmt.Sprintf("%spriority: %d (very_high=0; low=3)\n", tabStr, m.priority))
b.WriteString(fmt.Sprintf("%ssize: %d bytes\n", tabStr, m.sizeByte))
if m.cycleTime != 0 {
b.WriteString(fmt.Sprintf("%scycle_time: %d ms\n", tabStr, m.cycleTime))
}
if m.delayTime != 0 {
b.WriteString(fmt.Sprintf("%sdelay_time: %d ms\n", tabStr, m.delayTime))
}
if m.startDelayTime != 0 {
b.WriteString(fmt.Sprintf("%sstart_delay_time: %d ms\n", tabStr, m.startDelayTime))
}
if m.sendType != MessageSendTypeUnset {
b.WriteString(fmt.Sprintf("%ssend_type: %q\n", tabStr, m.sendType))
}
if m.receivers.size() > 0 {
b.WriteString(fmt.Sprintf("%sreceivers:\n", tabStr))
for _, rec := range m.Receivers() {
b.WriteString(fmt.Sprintf("%s\tname: %s; node_id: %d; entity_id: %s\n", tabStr, rec.node.name, rec.node.id, rec.node.entityID))
}
}
if m.signals.size() == 0 {
return
}
b.WriteString(fmt.Sprintf("%ssignals:\n", tabStr))
for _, sig := range m.Signals() {
sig.stringify(b, tabs+1)
b.WriteRune('\n')
}
}
func (m *Message) addSignal(sig Signal) {
sigID := sig.EntityID()
m.signals.add(sigID, sig)
m.signalNames.add(sig.Name(), sigID)
sig.setParentMsg(m)
if sig.Kind() != SignalKindMultiplexer {
return
}
muxSigStack := newStack[Signal]()
muxSigStack.push(sig)
for muxSigStack.size() > 0 {
currSig := muxSigStack.pop()
muxSig, err := currSig.ToMultiplexer()
if err != nil {
panic(err)
}
for tmpSigID, tmpSig := range muxSig.signals.entries() {
if tmpSig.Kind() == SignalKindMultiplexer {
muxSigStack.push(tmpSig)
}
m.signals.add(tmpSigID, tmpSig)
tmpSig.setParentMsg(m)
}
for tmpName, tmpSigID := range muxSig.signalNames.entries() {
m.signalNames.add(tmpName, tmpSigID)
}
}
}
func (m *Message) removeSignal(sig Signal) {
sigID := sig.EntityID()
m.signals.remove(sigID)
m.signalNames.remove(sig.Name())
sig.setParentMsg(nil)
if sig.Kind() != SignalKindMultiplexer {
return
}
muxSigStack := newStack[Signal]()
muxSigStack.push(sig)
for muxSigStack.size() > 0 {
currSig := muxSigStack.pop()
muxSig, err := currSig.ToMultiplexer()
if err != nil {
panic(err)
}
for tmpSigID, tmpSig := range muxSig.signals.entries() {
if tmpSig.Kind() == SignalKindMultiplexer {
muxSigStack.push(tmpSig)
}
m.signals.remove(tmpSigID)
tmpSig.setParentMsg(nil)
}
for _, tmpName := range muxSig.signalNames.getKeys() {
m.signalNames.remove(tmpName)
}
}
}
func (m *Message) String() string {
builder := new(strings.Builder)
m.stringify(builder, 0)
return builder.String()
}
// UpdateName updates the name of the [Message].
// It may return an error if the new name is already used within a node.
func (m *Message) UpdateName(newName string) error {
if m.name == newName {
return nil
}
if m.hasSenderNodeInt() {
if err := m.senderNodeInt.sentMessageNames.verifyKeyUnique(newName); err != nil {
return m.errorf(&UpdateNameError{
Err: &NameError{
Name: newName,
Err: err,
},
})
}
m.senderNodeInt.sentMessageNames.modifyKey(m.name, newName, m.entityID)
}
m.name = newName
return nil
}
// SenderNodeInterface returns the [NodeInterface] that is responsible for sending the [Message].
// If the [Message] is not sent by a [NodeInterface], it will return nil.
func (m *Message) SenderNodeInterface() *NodeInterface {
return m.senderNodeInt
}
// AppendSignal appends a [Signal] to the last position of the [Message] payload.
// It may return an error if the signal name is already used within the message,
// or if the signal cannot fit in the available space left at the end of the message payload.
func (m *Message) AppendSignal(signal Signal) error {
if signal == nil {
return &ArgumentError{
Name: "signal",
Err: ErrIsNil,
}
}
if err := m.verifySignalName(signal.Name()); err != nil {
return m.errorf(&AppendSignalError{
EntityID: signal.EntityID(),
Name: signal.Name(),
Err: err,
})
}
if err := m.signalPayload.append(signal); err != nil {
return m.errorf(err)
}
m.addSignal(signal)
return nil
}
// InsertSignal inserts a [Signal] at the given position of the [Message] payload.
// The start bit defines the index of the message payload where the signal will start.
// It may return an error if the signal name is already used within the message,
// or if the signal cannot fit in the available space left at the start bit.
func (m *Message) InsertSignal(signal Signal, startBit int) error {
if signal == nil {
return &ArgumentError{
Name: "signal",
Err: ErrIsNil,
}
}
if err := m.verifySignalName(signal.Name()); err != nil {
return m.errorf(&InsertSignalError{
EntityID: signal.EntityID(),
Name: signal.Name(),
StartBit: startBit,
Err: err,
})
}
if err := m.signalPayload.verifyAndInsert(signal, startBit); err != nil {
return m.errorf(err)
}
m.addSignal(signal)
return nil
}
// RemoveSignal removes a [Signal] that matches the given entity id from the [Message].
// It may return an error if the signal with the given entity id is not part of the message payload.
func (m *Message) RemoveSignal(signalEntityID EntityID) error {
sig, err := m.signals.getValue(signalEntityID)
if err != nil {
return m.errorf(&RemoveEntityError{
EntityID: signalEntityID,
Err: err,
})
}
m.removeSignal(sig)
m.signalPayload.remove(signalEntityID)
return nil
}
// RemoveAllSignals removes all signals from the [Message].
func (m *Message) RemoveAllSignals() {
for _, tmpSig := range m.signals.entries() {
tmpSig.setParentMsg(nil)
}
m.signals.clear()
m.signalNames.clear()
m.signalPayload.removeAll()
}
// ShiftSignalLeft shifts the signal with the given entity id left by the given amount.
// It returns the amount of bits shifted.
func (m *Message) ShiftSignalLeft(signalEntityID EntityID, amount int) int {
sig, err := m.signals.getValue(signalEntityID)
if err != nil {
return 0
}
return m.signalPayload.shiftLeft(sig.EntityID(), amount)
}
// ShiftSignalRight shifts the signal with the given entity id right by the given amount.
// It returns the amount of bits shifted.
func (m *Message) ShiftSignalRight(signalEntityID EntityID, amount int) int {
sig, err := m.signals.getValue(signalEntityID)
if err != nil {
return 0
}
return m.signalPayload.shiftRight(sig.EntityID(), amount)
}
// CompactSignals compacts the [Message] payload.
func (m *Message) CompactSignals() {
m.signalPayload.compact()
}
// Signals returns a slice of all signals in the [Message].
func (m *Message) Signals() []Signal {
return m.signalPayload.signals
}
// GetSignal returns the [Signal] that matches the given entity id.
func (m *Message) GetSignal(signalEntityID EntityID) (Signal, error) {
sig, err := m.signals.getValue(signalEntityID)
if err != nil {
return nil, m.errorf(&GetEntityError{
EntityID: signalEntityID,
Err: err,
})
}
return sig, nil
}
// GetSignalByName returns the [Signal] with the given name.
//
// It returns an [ErrNotFound] wrapped by a [NameError]
// if the name does not match any signal.
func (m *Message) GetSignalByName(name string) (Signal, error) {
id, err := m.signalNames.getValue(name)
if err != nil {
return nil, m.errorf(&NameError{Err: err})
}
sig, err := m.signals.getValue(id)
if err != nil {
panic(err)
}
return sig, nil
}
// SignalNames returns a slice of all signal names in the [Message].
func (m *Message) SignalNames() []string {
return m.signalNames.getKeys()
}
// SizeByte returns the size of the [Message] in bytes.
func (m *Message) SizeByte() int {
return m.sizeByte
}
// SetPriority sets the message priority.
func (m *Message) SetPriority(priority MessagePriority) {
m.priority = priority
}
// Priority returns the message priority.
func (m *Message) Priority() MessagePriority {
return m.priority
}
// SetCycleTime sets the message cycle time in ms.
func (m *Message) SetCycleTime(cycleTime int) {
m.cycleTime = cycleTime
}
// CycleTime returns the message cycle time.
func (m *Message) CycleTime() int {
return m.cycleTime
}
// SetSendType sets the send type of the [Message].
func (m *Message) SetSendType(sendType MessageSendType) {
m.sendType = sendType
}
// SendType returns the message send type.
func (m *Message) SendType() MessageSendType {
return m.sendType
}
// SetDelayTime sets the delay time of the [Message].
func (m *Message) SetDelayTime(delayTime int) {
m.delayTime = delayTime
}
// DelayTime returns the message delay time.
func (m *Message) DelayTime() int {
return m.delayTime
}
// SetStartDelayTime sets the start delay time of the [Message].
func (m *Message) SetStartDelayTime(startDelayTime int) {
m.startDelayTime = startDelayTime
}
// StartDelayTime returns the message start delay time.
func (m *Message) StartDelayTime() int {
return m.startDelayTime
}
// AddReceiver adds a receiver to the [Message].
//
// It returns an [ArgumentError] if the given receiver is nil or
// a [ErrReceiverIsSender] wrapped by an [AddEntityError]
// if the receiver is the same as the sender.
func (m *Message) AddReceiver(receiver *NodeInterface) error {
if receiver == nil {
return m.errorf(&ArgumentError{
Name: "receiver",
Err: ErrIsNil,
})
}
if err := receiver.addReceivedMessage(m); err != nil {
return m.errorf(&AddEntityError{
EntityID: receiver.node.entityID,
Name: receiver.node.name,
Err: err,
})
}
return nil
}
// RemoveReceiver removes a receiver from the [Message].
//
// It returns an [ErrNotFound] wrapped by a [RemoveEntityError]
// if the receiver with the given entity id is not found.
func (m *Message) RemoveReceiver(receiverEntityID EntityID) error {
receiver, err := m.receivers.getValue(receiverEntityID)
if err != nil {
return m.errorf(&RemoveEntityError{
EntityID: receiverEntityID,
Err: err,
})
}
receiver.removeReceivedMessage(m)
return nil
}
// Receivers returns a slice of all receivers of the [Message].
func (m *Message) Receivers() []*NodeInterface {
recSlice := m.receivers.getValues()
slices.SortFunc(recSlice, func(a, b *NodeInterface) int {
return strings.Compare(a.node.name, b.node.name)
})
return recSlice
}
// SetByteOrder sets the byte order of the [Message].
func (m *Message) SetByteOrder(byteOrder MessageByteOrder) {
m.byteOrder = byteOrder
}
// ByteOrder returns the byte order of the [Message].
func (m *Message) ByteOrder() MessageByteOrder {
return m.byteOrder
}
// UpdateID updates the id of the [Message].
// It will also reset the static CAN-ID of the message.
//
// It may return an error if the new message id is already used within a [NodeInterface].
func (m *Message) UpdateID(newID MessageID) error {
if m.id == newID && !m.hasStaticCANID {
return nil
}
if m.hasSenderNodeInt() {
if err := m.senderNodeInt.verifyMessageID(newID); err != nil {
return m.errorf(err)
}
}
m.staticCANID = 0
m.hasStaticCANID = false
m.id = newID
return nil
}
// ID returns the id of the [Message].
func (m *Message) ID() MessageID {
return m.id
}
// GetCANID returns the [GetCANID] associated to the [Message].
// If the message has a static CAN-ID, it will be returned.
// If the message does not have a sender [NodeInterface], it will return the message id.
// Otherwise, it will calculate the CAN-ID based on the [CANIDBuilder]
// provided by the [Bus] which owns the node interface.
func (m *Message) GetCANID() CANID {
if m.hasStaticCANID {
return m.staticCANID
}
if !m.hasSenderNodeInt() {
return CANID(m.id)
}
nodeInt := m.senderNodeInt
if !nodeInt.hasParentBus() {
return CANID(m.id)
}
return nodeInt.parentBus.canIDBuilder.Calculate(m.priority, m.id, nodeInt.node.id)
}
// SetStaticCANID sets the static CAN-ID of the [Message].
//
// It returns a [CANIDError] if the given static CAN-ID is already used.
func (m *Message) SetStaticCANID(staticCANID CANID) error {
if m.hasSenderNodeInt() {
if err := m.senderNodeInt.verifyStaticCANID(staticCANID); err != nil {
return err
}
}
m.hasStaticCANID = true
m.staticCANID = staticCANID
m.id = MessageID(staticCANID)
return nil
}
// HasStaticCANID returns whether the [Message] has a static CAN-ID.
func (m *Message) HasStaticCANID() bool {
return m.hasStaticCANID
}
// AssignAttribute assigns the given attribute/value pair to the [Message].
//
// It returns an [ArgumentError] if the attribute is nil,
// or an [AttributeValueError] if the value does not conform to the attribute.
func (m *Message) AssignAttribute(attribute Attribute, value any) error {
if err := m.addAttributeAssignment(attribute, m, value); err != nil {
return m.errorf(err)
}
return nil
}
// RemoveAttributeAssignment removes the [AttributeAssignment]
// with the given attribute entity id from the [Message].
//
// It returns an [ErrNotFound] if the provided attribute entity id is not found.
func (m *Message) RemoveAttributeAssignment(attributeEntityID EntityID) error {
if err := m.removeAttributeAssignment(attributeEntityID); err != nil {
return m.errorf(err)
}
return nil
}
// GetAttributeAssignment returns the [AttributeAssignment]
// with the given attribute entity id from the [Message].
//
// It returns an [ErrNotFound] if the provided attribute entity id is not found.
func (m *Message) GetAttributeAssignment(attributeEntityID EntityID) (*AttributeAssignment, error) {
attAss, err := m.getAttributeAssignment(attributeEntityID)
if err != nil {
return nil, m.errorf(err)
}
return attAss, nil
}