+uint32_t calc_bitmap_crc(uint32_t *bitmap, unsigned start, unsigned end)
+{
+ crc_t crc = 0;
+ crc = crc_update(crc, bitmap[0] << start, 32 - start);
+ crc = crc_update(crc, bitmap[1], 32);
+ crc = crc_update(crc, bitmap[2], end - 64 > 32 ? 32 : end - 64); /* FIXME: This seems to be broken for end < 64 */
+ crc = crc_update(crc, bitmap[3], end > 96 ? end - 96 : 0);
+ return crc << 17;
+}
+
unsigned calc_stuff_bits(struct can_frame *frame) {
uint32_t bitmap[4];
unsigned start = 0, end;
uint32_t mask, ones = 0, basemask = 0xf8000000;
unsigned stuffed = 0;
memset(bitmap, 0, sizeof(bitmap));
unsigned calc_stuff_bits(struct can_frame *frame) {
uint32_t bitmap[4];
unsigned start = 0, end;
uint32_t mask, ones = 0, basemask = 0xf8000000;
unsigned stuffed = 0;
memset(bitmap, 0, sizeof(bitmap));
/* char received[64]; */
/* sprint_canframe(received, frame, true); */
/* printf("Frame: %s\n", received); */
if (frame->can_id & CAN_EFF_FLAG) {
/* char received[64]; */
/* sprint_canframe(received, frame, true); */
/* printf("Frame: %s\n", received); */
if (frame->can_id & CAN_EFF_FLAG) {
+ // bit 31 0
+ // [0] |...........................sBBBB| s = SOF, B = Base ID (11 bits)
+ // [1] |BBBBBBBSIEEEEEEEEEEEEEEEEEER10DLC4| S = SRR, I = IDE, E = Extended ID (18 bits), DLC4 = DLC
+ // [2] |00000000111111112222222233333333| Data bytes, MSB first
+ // [3] |44444444555555556666666677777777|
- crc = crc_update(crc, bitmap[0] << start, 32-start);
- crc = crc_update(crc, bitmap[1], 32);
- crc = crc_update(crc, bitmap[2], end - 64 > 32 ? 32 : end - 64);
- crc = crc_update(crc, bitmap[3], end > 96 ? end - 96 : 0);
- crcx = (uint32_t)htons(crc) << 17;
- if (frame->can_dlc < 4)
- bitmap[2] |= crcx >> (frame->can_dlc*8);
- if (frame->can_dlc = 3)
- bitmap[3] |= crcx << 8;
- if (frame->can_dlc >= 4)
- bitmap[3] |= crcx >> (frame->can_dlc*8);
sprint_canframe(sent, &mi->sent, true);
sprint_canframe(received, &mi->received, true);
sprint_canframe(sent, &mi->sent, true);
sprint_canframe(received, &mi->received, true);
- fclose(opt.f_hist);
- fclose(opt.f_hist_gw);
- fclose(opt.f_msgs);
-
-
- fprintf(opt.f_stat, "cmdline='");
- for (i=0; i<argc; i++)
- fprintf(opt.f_stat, "%s%s", argv[i], i < argc-1 ? " " : "");
- fprintf(opt.f_stat, "'\n");
-
- timespec_subtract(&diff, &stats.tac, &stats.tic);
- fprintf(opt.f_stat, "duration=%s # seconds\n", tstamp_str(NULL, &diff));
+ if (opt.f_hist)
+ fclose(opt.f_hist);
+ if (opt.f_hist_gw)
+ fclose(opt.f_hist_gw);
+ if (opt.f_msgs)
+ fclose(opt.f_msgs);
+
+ if (opt.f_stat) {
+ fprintf(opt.f_stat, "cmdline='");
+ for (i=0; i<argc; i++)
+ fprintf(opt.f_stat, "%s%s", argv[i], i < argc-1 ? " " : "");
+ fprintf(opt.f_stat, "'\n");
+
+ timespec_subtract(&diff, &stats.tac, &stats.tic);
+ fprintf(opt.f_stat, "duration=%s # seconds\n", tstamp_str(NULL, &diff));
- fprintf(opt.f_stat, "sent=%d\n", count);
- fprintf(opt.f_stat, "overrun=%d\n", stats.overrun);
- if (stats.overrun && !opt.quiet)
- printf("overrun=%d\n", stats.overrun);
- fprintf(opt.f_stat, "enobufs=%d\n", stats.enobufs);
- if (stats.enobufs && !opt.quiet)
- printf("enobufs=%d\n", stats.enobufs);
- fprintf(opt.f_stat, "lost=%d\n", stats.lost);
- if (stats.lost && !opt.quiet)
- printf("lost=%d\n", stats.lost);
- fprintf(opt.f_stat, "timeouts=%d\n", stats.timeouts);
- if (stats.timeouts && !opt.quiet)
- printf("timeouts=%d\n", stats.timeouts);
- fprintf(opt.f_stat, "invalid_frame=%d\n", stats.timeouts);
- if (stats.timeouts && !opt.quiet)
- printf("invalid_frame=%d\n", stats.timeouts);
-
- fclose(opt.f_stat);
+ fprintf(opt.f_stat, "sent=%d\n", count);
+ fprintf(opt.f_stat, "overrun=%d\n", stats.overrun);
+ if (stats.overrun && !opt.quiet)
+ printf("overrun=%d\n", stats.overrun);
+ fprintf(opt.f_stat, "enobufs=%d\n", stats.enobufs);
+ if (stats.enobufs && !opt.quiet)
+ printf("enobufs=%d\n", stats.enobufs);
+ fprintf(opt.f_stat, "lost=%d\n", stats.lost);
+ if (stats.lost && !opt.quiet)
+ printf("lost=%d\n", stats.lost);
+ fprintf(opt.f_stat, "timeouts=%d\n", stats.timeouts);
+ if (stats.timeouts && !opt.quiet)
+ printf("timeouts=%d\n", stats.timeouts);
+ fprintf(opt.f_stat, "invalid_frame=%d\n", stats.timeouts);
+ if (stats.timeouts && !opt.quiet)
+ printf("invalid_frame=%d\n", stats.timeouts);
+
+ fclose(opt.f_stat);
+ }