]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - latester/canframelen.c
Commit not-finished state of frame length calculation
[can-benchmark.git] / latester / canframelen.c
1 #include "canframelen.h"
2 #include <stdint.h>
3 #include <stddef.h>
4 #include <stdbool.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <stdio.h>              /* FIXME: remove me */
8
9 static void sprint_canframe(char *buf , struct can_frame *cf, int sep) {
10         /* documentation see lib.h */
11
12         int i,offset;
13         int dlc = (cf->can_dlc > 8)? 8 : cf->can_dlc;
14
15         if (cf->can_id & CAN_ERR_FLAG) {
16                 sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
17                 offset = 9;
18         } else if (cf->can_id & CAN_EFF_FLAG) {
19                 sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
20                 offset = 9;
21         } else {
22                 sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
23                 offset = 4;
24         }
25
26         if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */
27                 sprintf(buf+offset, "R");
28         else
29                 for (i = 0; i < dlc; i++) {
30                         sprintf(buf+offset, "%02X", cf->data[i]);
31                         offset += 2;
32                         if (sep && (i+1 < dlc))
33                                 sprintf(buf+offset++, ".");
34                 }
35 }
36
37 /**
38  * Functions and types for CRC checks.
39  *
40  * Generated on Wed Jan  8 15:14:20 2014,
41  * by pycrc v0.8.1, http://www.tty1.net/pycrc/
42  * using the configuration:
43  *    Width        = 15
44  *    Poly         = 0x4599
45  *    XorIn        = 0x0000
46  *    ReflectIn    = False
47  *    XorOut       = 0x0000
48  *    ReflectOut   = False
49  *    Algorithm    = table-driven
50  *****************************************************************************/
51
52 typedef uint16_t crc_t;
53
54 /**
55  * Static table used for the table_driven implementation.
56  *****************************************************************************/
57 static const crc_t crc_table[256] = {
58     0x0000, 0x4599, 0x4eab, 0x0b32, 0x58cf, 0x1d56, 0x1664, 0x53fd, 0x7407, 0x319e, 0x3aac, 0x7f35, 0x2cc8, 0x6951, 0x6263, 0x27fa,
59     0x2d97, 0x680e, 0x633c, 0x26a5, 0x7558, 0x30c1, 0x3bf3, 0x7e6a, 0x5990, 0x1c09, 0x173b, 0x52a2, 0x015f, 0x44c6, 0x4ff4, 0x0a6d,
60     0x5b2e, 0x1eb7, 0x1585, 0x501c, 0x03e1, 0x4678, 0x4d4a, 0x08d3, 0x2f29, 0x6ab0, 0x6182, 0x241b, 0x77e6, 0x327f, 0x394d, 0x7cd4,
61     0x76b9, 0x3320, 0x3812, 0x7d8b, 0x2e76, 0x6bef, 0x60dd, 0x2544, 0x02be, 0x4727, 0x4c15, 0x098c, 0x5a71, 0x1fe8, 0x14da, 0x5143,
62     0x73c5, 0x365c, 0x3d6e, 0x78f7, 0x2b0a, 0x6e93, 0x65a1, 0x2038, 0x07c2, 0x425b, 0x4969, 0x0cf0, 0x5f0d, 0x1a94, 0x11a6, 0x543f,
63     0x5e52, 0x1bcb, 0x10f9, 0x5560, 0x069d, 0x4304, 0x4836, 0x0daf, 0x2a55, 0x6fcc, 0x64fe, 0x2167, 0x729a, 0x3703, 0x3c31, 0x79a8,
64     0x28eb, 0x6d72, 0x6640, 0x23d9, 0x7024, 0x35bd, 0x3e8f, 0x7b16, 0x5cec, 0x1975, 0x1247, 0x57de, 0x0423, 0x41ba, 0x4a88, 0x0f11,
65     0x057c, 0x40e5, 0x4bd7, 0x0e4e, 0x5db3, 0x182a, 0x1318, 0x5681, 0x717b, 0x34e2, 0x3fd0, 0x7a49, 0x29b4, 0x6c2d, 0x671f, 0x2286,
66     0x2213, 0x678a, 0x6cb8, 0x2921, 0x7adc, 0x3f45, 0x3477, 0x71ee, 0x5614, 0x138d, 0x18bf, 0x5d26, 0x0edb, 0x4b42, 0x4070, 0x05e9,
67     0x0f84, 0x4a1d, 0x412f, 0x04b6, 0x574b, 0x12d2, 0x19e0, 0x5c79, 0x7b83, 0x3e1a, 0x3528, 0x70b1, 0x234c, 0x66d5, 0x6de7, 0x287e,
68     0x793d, 0x3ca4, 0x3796, 0x720f, 0x21f2, 0x646b, 0x6f59, 0x2ac0, 0x0d3a, 0x48a3, 0x4391, 0x0608, 0x55f5, 0x106c, 0x1b5e, 0x5ec7,
69     0x54aa, 0x1133, 0x1a01, 0x5f98, 0x0c65, 0x49fc, 0x42ce, 0x0757, 0x20ad, 0x6534, 0x6e06, 0x2b9f, 0x7862, 0x3dfb, 0x36c9, 0x7350,
70     0x51d6, 0x144f, 0x1f7d, 0x5ae4, 0x0919, 0x4c80, 0x47b2, 0x022b, 0x25d1, 0x6048, 0x6b7a, 0x2ee3, 0x7d1e, 0x3887, 0x33b5, 0x762c,
71     0x7c41, 0x39d8, 0x32ea, 0x7773, 0x248e, 0x6117, 0x6a25, 0x2fbc, 0x0846, 0x4ddf, 0x46ed, 0x0374, 0x5089, 0x1510, 0x1e22, 0x5bbb,
72     0x0af8, 0x4f61, 0x4453, 0x01ca, 0x5237, 0x17ae, 0x1c9c, 0x5905, 0x7eff, 0x3b66, 0x3054, 0x75cd, 0x2630, 0x63a9, 0x689b, 0x2d02,
73     0x276f, 0x62f6, 0x69c4, 0x2c5d, 0x7fa0, 0x3a39, 0x310b, 0x7492, 0x5368, 0x16f1, 0x1dc3, 0x585a, 0x0ba7, 0x4e3e, 0x450c, 0x0095
74 };
75
76 /**
77  * Update the crc value with new data.
78  *
79  * \param crc      The current crc value.
80  * \param data     Pointer to a buffer of \a data_len bytes.
81  * \param data_len Number of bytes in the \a data buffer.
82  * \return         The updated crc value.
83  *****************************************************************************/
84 crc_t crc_update_bytewise(crc_t crc, const unsigned char *data, size_t data_len)
85 {
86     unsigned int tbl_idx;
87
88     while (data_len--) {
89         tbl_idx = ((crc >> 7) ^ *data) & 0xff;
90         crc = (crc_table[tbl_idx] ^ (crc << 8)) & 0x7fff;
91
92         data++;
93     }
94     return crc & 0x7fff;
95 }
96
97 /**
98  * Update the crc value with new data.
99  *
100  * \param crc      The current crc value.
101  * \param data     Data value
102  * \param bits     The number of most significant bits in data used for CRC calculation
103  * \return         The updated crc value.
104  *****************************************************************************/
105 crc_t crc_update_bitwise(crc_t crc, uint8_t data, size_t bits)
106 {
107     uint8_t i;
108     bool bit;
109
110     for (i = 0x80; bits--; i >>= 1) {
111             bit = crc & 0x4000;
112             if (data & i) {
113                     bit = !bit;
114             }
115             crc <<= 1;
116             if (bit) {
117                     crc ^= 0x4599;
118             }
119     }
120     return crc & 0x7fff;
121 }
122
123 crc_t calc_bitmap_crc(uint8_t *bitmap, unsigned start, unsigned end)
124 {
125     crc_t crc = 0;
126
127     if (start%8) {
128             crc = crc_update_bitwise(crc, bitmap[start/8] << (start%8), 8 - start%8);
129             start += 8 - start%8;
130     }
131     crc = crc_update_bytewise(crc, &bitmap[start/8], (end - start) / 8);
132     crc = crc_update_bitwise(crc, bitmap[end/8], end%8);
133     return crc;
134 }
135
136 void print_bitmap(const char *prefix, uint8_t *bitmap, unsigned start, unsigned end)
137 {
138         int i;
139         printf("%s", prefix);
140         for (i = start; i < end; i++)
141                 printf("%c", (bitmap[i/8] << (i % 8)) & 0x80 ? '1' : '0');
142         if (*prefix)
143                 printf("\n");
144 }
145
146 unsigned calc_stuff_bits(struct can_frame *frame) {
147         uint8_t bitmap[16];
148         unsigned start = 0, end;
149
150         char received[64];
151         sprint_canframe(received, frame, true);
152         printf("Frame: %s\n", received);
153
154         memset(bitmap, 0, sizeof(bitmap));
155
156         if (frame->can_id & CAN_EFF_FLAG) {
157                 //            bit 7      0 7      0 7      0 7      0
158                 // bitmap[0-3]   |.sBBBBBB BBBBBSIE EEEEEEEE EEEEEEEE| s = SOF, B = Base ID (11 bits), S = SRR, I = IDE, E = Extended ID (18 bits)
159                 // bitmap[4-7]   |ER10DLC4 00000000 11111111 22222222| R = RTR, 0 = r0, 1 = r1, DLC4 = DLC, Data bytes
160                 // bitmap[8-11]  |33333333 44444444 55555555 66666666| Data bytes
161                 // bitmap[12-15] |77777777 ........ ........ ........| Data bytes
162                 bitmap[0] = (frame->can_id & CAN_EFF_MASK) >> 23;
163                 bitmap[1] = ((frame->can_id >> 18) & 0x3f) << 3 |
164                             3 << 1                              | /* SRR, IDE */
165                             ((frame->can_id >> 17) & 0x01);
166                 bitmap[2] = (frame->can_id >> 9) & 0xff;
167                 bitmap[3] = (frame->can_id >> 1) & 0xff;
168                 bitmap[4] = (frame->can_id & 0x1) << 7              |
169                             (!!(frame->can_id & CAN_RTR_FLAG)) << 6 |
170                             0 << 4                                  | /* r1, r0 */
171                             frame->can_dlc & 0xf;
172                 memcpy(&bitmap[5], &frame->data, frame->can_dlc);
173                 start = 1;
174                 end = 40 + 8*frame->can_dlc;
175         } else {
176                 //           bit 7      0 7      0 7      0 7      0
177                 // bitmap[0-3]  |.....sII IIIIIIII IRE0DLC4 00000000| s = SOF, I = ID (11 bits), R = RTR, E = IDE, DLC4 = DLC
178                 // bitmap[4-7]  |11111111 22222222 33333333 44444444| Data bytes
179                 // bitmap[8-11] |55555555 66666666 77777777 ........| Data bytes
180                 bitmap[0] = (frame->can_id & CAN_SFF_MASK) >> 9;
181                 bitmap[1] = (frame->can_id >> 1) & 0xff;
182                 bitmap[2] = (frame->can_id << 7) & 0xff |
183                             (!!(frame->can_id & CAN_RTR_FLAG)) << 6 |
184                             0 << 4 | /* IDE, r0 */
185                             frame->can_dlc & 0xf;
186                 memcpy(&bitmap[3], &frame->data, frame->can_dlc);
187                 start = 5;
188                 end = 24 + 8*frame->can_dlc;
189         }
190
191         crc_t crc = calc_bitmap_crc(bitmap, start, end);
192         uint16_t crc_be = htons(crc << 1);
193         assert(end%8 == 0);
194         memcpy(bitmap + end/8, &crc_be, 2);
195         print_bitmap("Frame:    ", bitmap, start, end);
196         end += 15;
197         print_bitmap("FrameCRC: ", bitmap, start, end);
198
199         uint8_t mask, ones = 0, basemask = 0xf8;
200         unsigned stuffed = 0;
201         while (start < end) {
202                 start++;        /* FIXME: This loop is currently broken!!! */
203                 mask = basemask >> (start%32);
204                 while (1) {
205                         ones = ones ? mask : 0;
206                         uint32_t chunk = (bitmap[start/32] & mask) ^ ones;
207                         //printf("start=%d  bitmap=0x%08x  mask=0x%08x  ones=0x%08x  chunk=0x%08x\n", start, bitmap[start >> 5], mask, ones, chunk);
208                         if (chunk) {
209                                 unsigned change = __builtin_clz(chunk);
210                                 start = start & ~0x1f | change;
211                                 basemask = 0xf8000000;
212                         } else {
213                                 unsigned oldstart = start;
214                                 start += __builtin_popcount(mask);
215                                 mask = (oldstart & 0x1f) ? basemask << (-oldstart & 0x1f) : 0;
216                                 //printf("oldstart=%d  shl=%d  mask=0x%08x\n", oldstart, -oldstart & 0x1f, mask);
217                                 if (mask && start < end)
218                                         continue;
219                                 if (start <= end && !mask) {
220                                         stuffed++;
221                                         basemask = 0xf0000000;
222                                         //printf("stuffed %d\n", !ones);
223                                 }
224                         }
225                         break;
226                 }
227                 ones = !ones;
228         }
229         //printf ("STUFFED %d BITS\n", stuffed);
230         return stuffed;
231 }
232
233 unsigned calc_frame_length(struct can_frame *frame)
234 {
235         return calc_stuff_bits(frame) +
236                 1 +             /* SOF */
237                 11 +            /* ID A */
238                 ((frame->can_id & CAN_EFF_FLAG) ?
239                  1 +            /* SRR */
240                  1 +            /* IDE */
241                  18 +           /* ID B */
242                  1 +            /* RTR */
243                  2              /* r1, r0 */
244                  :
245                  1 +            /* rtr */
246                  2) +           /* ide, r0 */
247                 4 +             /* dlc */
248                 8*frame->can_dlc +
249                 15 +            /* CRC */
250                 3 +             /* CRC del, ACK, ACK del */
251                 7;              /* EOF */
252 }