@@ -5,98 +5,93 @@ import (
5
5
"math/big"
6
6
)
7
7
8
- // const MaxDataLength = 8
9
-
10
- // // Data holds the data in a CAN frame.
11
- // //
12
- // // Layout
13
- // //
14
- // // Individual bits in the data are numbered according to the following scheme:
15
- // //
16
- // // BIT
17
- // // NUMBER
18
- // // +------+------+------+------+------+------+------+------+
19
- // // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
20
- // // BYTE +------+------+------+------+------+------+------+------+
21
- // // NUMBER
22
- // // +-----+ +------+------+------+------+------+------+------+------+
23
- // // | 0 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
24
- // // +-----+ +------+------+------+------+------+------+------+------+
25
- // // | 1 | | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
26
- // // +-----+ +------+------+------+------+------+------+------+------+
27
- // // | 2 | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
28
- // // +-----+ +------+------+------+------+------+------+------+------+
29
- // // | 3 | | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 |
30
- // // +-----+ +------+------+------+------+------+------+------+------+
31
- // // | 4 | | 39 | 38 | 37 | 36 | 35 | 34 | 33 | 32 |
32
- // // +-----+ +------+------+------+------+------+------+------+------+
33
- // // | 5 | | 47 | 46 | 45 | 44 | 43 | 42 | 41 | 40 |
34
- // // +-----+ +------+------+------+------+------+------+------+------+
35
- // // | 6 | | 55 | 54 | 53 | 52 | 51 | 50 | 49 | 48 |
36
- // // +-----+ +------+------+------+------+------+------+------+------+
37
- // // | 7 | | 63 | 62 | 61 | 60 | 59 | 58 | 57 | 56 |
38
- // // +-----+ +------+------+------+------+------+------+------+------+
39
- // //
40
- // // Bit ranges can be manipulated using little-endian and big-endian bit ordering.
41
- // //
42
- // // Little-endian bit ranges
43
- // //
44
- // // Example range of length 32 starting at bit 29:
45
- // //
46
- // // BIT
47
- // // NUMBER
48
- // // +------+------+------+------+------+------+------+------+
49
- // // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
50
- // // BYTE +------+------+------+------+------+------+------+------+
51
- // // NUMBER
52
- // // +-----+ +------+------+------+------+------+------+------+------+
53
- // // | 0 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
54
- // // +-----+ +------+------+------+------+------+------+------+------+
55
- // // | 1 | | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
56
- // // +-----+ +------+------+------+------+------+------+------+------+
57
- // // | 2 | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
58
- // // +-----+ +------+------+------+------+------+------+------+------+
59
- // // | 3 | | <-------------LSb | 28 | 27 | 26 | 25 | 24 |
60
- // // +-----+ +------+------+------+------+------+------+------+------+
61
- // // | 4 | | <-------------------------------------------------- |
62
- // // +-----+ +------+------+------+------+------+------+------+------+
63
- // // | 5 | | <-------------------------------------------------- |
64
- // // +-----+ +------+------+------+------+------+------+------+------+
65
- // // | 6 | | <-------------------------------------------------- |
66
- // // +-----+ +------+------+------+------+------+------+------+------+
67
- // // | 7 | | 63 | 62 | 61 | <-MSb--------------------------- |
68
- // // +-----+ +------+------+------+------+------+------+------+------+
69
- // //
70
- // // Big-endian bit ranges
71
- // //
72
- // // Example range of length 32 starting at bit 29:
73
- // //
74
- // // BIT
75
- // // NUMBER
76
- // // +------+------+------+------+------+------+------+------+
77
- // // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
78
- // // BYTE +------+------+------+------+------+------+------+------+
79
- // // NUMBER
80
- // // +-----+ +------+------+------+------+------+------+------+------+
81
- // // | 0 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
82
- // // +-----+ +------+------+------+------+------+------+------+------+
83
- // // | 1 | | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
84
- // // +-----+ +------+------+------+------+------+------+------+------+
85
- // // | 2 | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
86
- // // +-----+ +------+------+------+------+------+------+------+------+
87
- // // | 3 | | 31 | 30 | <-MSb--------------------------------- |
88
- // // +-----+ +------+------+------+------+------+------+------+------+
89
- // // | 4 | | <-------------------------------------------------- |
90
- // // +-----+ +------+------+------+------+------+------+------+------+
91
- // // | 5 | | <-------------------------------------------------- |
92
- // // +-----+ +------+------+------+------+------+------+------+------+
93
- // // | 6 | | <-------------------------------------------------- |
94
- // // +-----+ +------+------+------+------+------+------+------+------+
95
- // // | 7 | | <------LSb | 61 | 60 | 59 | 58 | 57 | 56 |
96
- // // +-----+ +------+------+------+------+------+------+------+------+
97
- // type Data [MaxDataLength]byte
98
-
99
- //type Packed *big.Int
8
+ // Data holds the data in a CAN frame.
9
+ //
10
+ // Layout
11
+ //
12
+ // Individual bits in the data are numbered according to the following scheme:
13
+ //
14
+ // BIT
15
+ // NUMBER
16
+ // +------+------+------+------+------+------+------+------+
17
+ // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
18
+ // BYTE +------+------+------+------+------+------+------+------+
19
+ // NUMBER
20
+ // +-----+ +------+------+------+------+------+------+------+------+
21
+ // | 0 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
22
+ // +-----+ +------+------+------+------+------+------+------+------+
23
+ // | 1 | | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
24
+ // +-----+ +------+------+------+------+------+------+------+------+
25
+ // | 2 | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
26
+ // +-----+ +------+------+------+------+------+------+------+------+
27
+ // | 3 | | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 |
28
+ // +-----+ +------+------+------+------+------+------+------+------+
29
+ // | 4 | | 39 | 38 | 37 | 36 | 35 | 34 | 33 | 32 |
30
+ // +-----+ +------+------+------+------+------+------+------+------+
31
+ // | 5 | | 47 | 46 | 45 | 44 | 43 | 42 | 41 | 40 |
32
+ // +-----+ +------+------+------+------+------+------+------+------+
33
+ // | 6 | | 55 | 54 | 53 | 52 | 51 | 50 | 49 | 48 |
34
+ // +-----+ +------+------+------+------+------+------+------+------+
35
+ // | 7 | | 63 | 62 | 61 | 60 | 59 | 58 | 57 | 56 |
36
+ // +-----+ +------+------+------+------+------+------+------+------+
37
+ //
38
+ // Bit ranges can be manipulated using little-endian and big-endian bit ordering.
39
+ //
40
+ // Little-endian bit ranges
41
+ //
42
+ // Example range of length 32 starting at bit 29:
43
+ //
44
+ // BIT
45
+ // NUMBER
46
+ // +------+------+------+------+------+------+------+------+
47
+ // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
48
+ // BYTE +------+------+------+------+------+------+------+------+
49
+ // NUMBER
50
+ // +-----+ +------+------+------+------+------+------+------+------+
51
+ // | 0 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
52
+ // +-----+ +------+------+------+------+------+------+------+------+
53
+ // | 1 | | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
54
+ // +-----+ +------+------+------+------+------+------+------+------+
55
+ // | 2 | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
56
+ // +-----+ +------+------+------+------+------+------+------+------+
57
+ // | 3 | | <-------------LSb | 28 | 27 | 26 | 25 | 24 |
58
+ // +-----+ +------+------+------+------+------+------+------+------+
59
+ // | 4 | | <-------------------------------------------------- |
60
+ // +-----+ +------+------+------+------+------+------+------+------+
61
+ // | 5 | | <-------------------------------------------------- |
62
+ // +-----+ +------+------+------+------+------+------+------+------+
63
+ // | 6 | | <-------------------------------------------------- |
64
+ // +-----+ +------+------+------+------+------+------+------+------+
65
+ // | 7 | | 63 | 62 | 61 | <-MSb--------------------------- |
66
+ // +-----+ +------+------+------+------+------+------+------+------+
67
+ //
68
+ // Big-endian bit ranges
69
+ //
70
+ // Example range of length 32 starting at bit 29:
71
+ //
72
+ // BIT
73
+ // NUMBER
74
+ // +------+------+------+------+------+------+------+------+
75
+ // | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
76
+ // BYTE +------+------+------+------+------+------+------+------+
77
+ // NUMBER
78
+ // +-----+ +------+------+------+------+------+------+------+------+
79
+ // | 0 | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
80
+ // +-----+ +------+------+------+------+------+------+------+------+
81
+ // | 1 | | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 |
82
+ // +-----+ +------+------+------+------+------+------+------+------+
83
+ // | 2 | | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
84
+ // +-----+ +------+------+------+------+------+------+------+------+
85
+ // | 3 | | 31 | 30 | <-MSb--------------------------------- |
86
+ // +-----+ +------+------+------+------+------+------+------+------+
87
+ // | 4 | | <-------------------------------------------------- |
88
+ // +-----+ +------+------+------+------+------+------+------+------+
89
+ // | 5 | | <-------------------------------------------------- |
90
+ // +-----+ +------+------+------+------+------+------+------+------+
91
+ // | 6 | | <-------------------------------------------------- |
92
+ // +-----+ +------+------+------+------+------+------+------+------+
93
+ // | 7 | | <------LSb | 61 | 60 | 59 | 58 | 57 | 56 |
94
+ // +-----+ +------+------+------+------+------+------+------+------+
100
95
101
96
type Payload struct {
102
97
// Binary data
@@ -168,13 +163,8 @@ func (p *Payload) SignedBitsBigEndian(start, length uint16) int64 {
168
163
return AsSigned (unsigned , length )
169
164
}
170
165
171
- // SignedBitsBigEndian returns little-endian bit range [start, start+length) as a signed value.
172
- // func (d *Data) SignedBitsBigEndian(start, length uint8) int64 {
173
- // unsigned := d.UnsignedBitsBigEndian(start, length)
174
- // return reinterpret.AsSigned(unsigned, length)
175
- // }
176
-
177
- // // SetUnsignedBitsBigEndian sets the little-endian bit range [start, start+length) to the provided unsigned value.
166
+ // TODO: Implement SetUnsignedBitsLittleEndian for Payload
167
+ // SetUnsignedBitsLittleEndian sets the little-endian bit range [start, start+length) to the provided unsigned value.
178
168
// func (d *Data) SetUnsignedBitsLittleEndian(start, length uint8, value uint64) {
179
169
// // pack bits into one continuous value
180
170
// packed := d.PackLittleEndian()
@@ -190,7 +180,8 @@ func (p *Payload) SignedBitsBigEndian(start, length uint16) int64 {
190
180
// d.UnpackLittleEndian(newPacked)
191
181
// }
192
182
193
- // // SetUnsignedBitsBigEndian sets the big-endian bit range [start, start+length) to the provided unsigned value.
183
+ // TODO: Implement SetUnsignedBitsBigEndian for Payload
184
+ // SetUnsignedBitsBigEndian sets the big-endian bit range [start, start+length) to the provided unsigned value.
194
185
// func (d *Data) SetUnsignedBitsBigEndian(start, length uint8, value uint64) {
195
186
// // pack bits into one continuous value
196
187
// packed := d.PackBigEndian()
@@ -208,12 +199,14 @@ func (p *Payload) SignedBitsBigEndian(start, length uint16) int64 {
208
199
// d.UnpackBigEndian(newPacked)
209
200
// }
210
201
211
- // // SetSignedBitsLittleEndian sets the little-endian bit range [start, start+length) to the provided signed value.
202
+ // TODO: Implement SetSignedBitsLittleEndian for Payload
203
+ // SetSignedBitsLittleEndian sets the little-endian bit range [start, start+length) to the provided signed value.
212
204
// func (d *Data) SetSignedBitsLittleEndian(start, length uint8, value int64) {
213
205
// d.SetUnsignedBitsLittleEndian(start, length, reinterpret.AsUnsigned(value, length))
214
206
// }
215
207
216
- // // SetSignedBitsBigEndian sets the big-endian bit range [start, start+length) to the provided signed value.
208
+ // TODO: Implement SetSignedBitsBigEndian for Payload
209
+ // SetSignedBitsBigEndian sets the big-endian bit range [start, start+length) to the provided signed value.
217
210
// func (d *Data) SetSignedBitsBigEndian(start, length uint8, value int64) {
218
211
// d.SetUnsignedBitsBigEndian(start, length, reinterpret.AsUnsigned(value, length))
219
212
// }
@@ -247,20 +240,6 @@ func (p *Payload) SetBit(i uint16, value bool) {
247
240
}
248
241
}
249
242
250
- // PackLittleEndian packs the data into a contiguous uint64 value for little-endian signals.
251
- // func (d *Data) PackLittleEndian() uint64 {
252
- // var packed uint64
253
- // packed |= uint64(d[0]) << (0 * 8)
254
- // packed |= uint64(d[1]) << (1 * 8)
255
- // packed |= uint64(d[2]) << (2 * 8)
256
- // packed |= uint64(d[3]) << (3 * 8)
257
- // packed |= uint64(d[4]) << (4 * 8)
258
- // packed |= uint64(d[5]) << (5 * 8)
259
- // packed |= uint64(d[6]) << (6 * 8)
260
- // packed |= uint64(d[7]) << (7 * 8)
261
- // return packed
262
- // }
263
-
264
243
// PackLittleEndian packs the byte array into a continuous little endian big.Int
265
244
func (p * Payload ) PackLittleEndian () * big.Int {
266
245
if p .PackedLittleEndian == nil {
@@ -279,20 +258,6 @@ func reverse(data []byte) []byte {
279
258
return reversedArray
280
259
}
281
260
282
- // // PackBigEndian packs the data into a contiguous uint64 value for big-endian signals.
283
- // func (d *Data) PackBigEndian() uint64 {
284
- // var packed uint64
285
- // packed |= uint64(d[0]) << (7 * 8)
286
- // packed |= uint64(d[1]) << (6 * 8)
287
- // packed |= uint64(d[2]) << (5 * 8)
288
- // packed |= uint64(d[3]) << (4 * 8)
289
- // packed |= uint64(d[4]) << (3 * 8)
290
- // packed |= uint64(d[5]) << (2 * 8)
291
- // packed |= uint64(d[6]) << (1 * 8)
292
- // packed |= uint64(d[7]) << (0 * 8)
293
- // return packed
294
- // }
295
-
296
261
// PackBigEndian packs the byte array into a continuous big endian big.Int
297
262
func (p * Payload ) PackBigEndian () * big.Int {
298
263
if p .PackedBigEndian == nil {
@@ -302,7 +267,8 @@ func (p *Payload) PackBigEndian() *big.Int {
302
267
return new (big.Int ).Set (p .PackedBigEndian )
303
268
}
304
269
305
- // // UnpackLittleEndian sets the value of d.Bytes by unpacking the provided value as sequential little-endian bits.
270
+ // TODO: Implement UnpackLittleEndian for Payload
271
+ // UnpackLittleEndian sets the value of d.Bytes by unpacking the provided value as sequential little-endian bits.
306
272
// func (d *Data) UnpackLittleEndian(packed uint64) {
307
273
// d[0] = uint8(packed >> (0 * 8))
308
274
// d[1] = uint8(packed >> (1 * 8))
@@ -314,7 +280,8 @@ func (p *Payload) PackBigEndian() *big.Int {
314
280
// d[7] = uint8(packed >> (7 * 8))
315
281
// }
316
282
317
- // // UnpackBigEndian sets the value of d.Bytes by unpacking the provided value as sequential big-endian bits.
283
+ // TODO: Implement UnpackBigEndian for Payload
284
+ // UnpackBigEndian sets the value of d.Bytes by unpacking the provided value as sequential big-endian bits.
318
285
// func (d *Data) UnpackBigEndian(packed uint64) {
319
286
// d[0] = uint8(packed >> (7 * 8))
320
287
// d[1] = uint8(packed >> (6 * 8))
@@ -335,41 +302,6 @@ func (p *Payload) invertEndian(i uint16) uint16 {
335
302
return bitIndex
336
303
}
337
304
338
- // // CheckBitRangeLittleEndian checks that a little-endian bit range fits in the data.
339
- // func CheckBitRangeLittleEndian(frameLength, rangeStart, rangeLength uint8) error {
340
- // lsbIndex := rangeStart
341
- // msbIndex := rangeStart + rangeLength - 1
342
- // upperBound := frameLength * 8
343
- // if msbIndex >= upperBound {
344
- // return fmt.Errorf("bit range out of bounds [0, %v): [%v, %v]", upperBound, lsbIndex, msbIndex)
345
- // }
346
- // return nil
347
- // }
348
-
349
- // // CheckBitRangeBigEndian checks that a big-endian bit range fits in the data.
350
- // func CheckBitRangeBigEndian(frameLength, rangeStart, rangeLength uint8) error {
351
- // upperBound := frameLength * 8
352
- // if rangeStart >= upperBound {
353
- // return fmt.Errorf("bit range starts out of bounds [0, %v): %v", upperBound, rangeStart)
354
- // }
355
- // msbIndex := invertEndian(rangeStart)
356
- // lsbIndex := msbIndex - rangeLength + 1
357
- // end := invertEndian(lsbIndex)
358
- // if end >= upperBound {
359
- // return fmt.Errorf("bit range ends out of bounds [0, %v): %v", upperBound, end)
360
- // }
361
- // return nil
362
- // }
363
-
364
- // // CheckValue checks that a value fits in a number of bits.
365
- // func CheckValue(value uint64, bits uint8) error {
366
- // upperBound := uint64(1 << bits)
367
- // if value >= upperBound {
368
- // return fmt.Errorf("value out of bounds [0, %v): %v", upperBound, value)
369
- // }
370
- // return nil
371
- // }
372
-
373
305
// AsSigned reinterprets the provided unsigned value as a signed value.
374
306
func AsSigned (unsigned uint64 , bits uint16 ) int64 {
375
307
switch bits {
0 commit comments