canframelen.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * canframelen.c
  3. *
  4. * Copyright (c) 2013, 2014 Czech Technical University in Prague
  5. *
  6. * Author: Michal Sojka <sojkam1@fel.cvut.cz>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of Czech Technical University in Prague nor the
  17. * names of its contributors may be used to endorse or promote
  18. * products derived from this software without specific prior
  19. * written permission.
  20. *
  21. * Alternatively, provided that this notice is retained in full, this
  22. * software may be distributed under the terms of the GNU General
  23. * Public License ("GPL") version 2, in which case the provisions of the
  24. * GPL apply INSTEAD OF those given above.
  25. *
  26. * The provided data structures and external interfaces from this code
  27. * are not restricted to be used by modules with a GPL compatible license.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  40. * DAMAGE.
  41. *
  42. * Send feedback to <linux-can@vger.kernel.org>
  43. *
  44. */
  45. #include "canframelen.h"
  46. #include <stdint.h>
  47. #include <stddef.h>
  48. #include <stdbool.h>
  49. #include <string.h>
  50. #include <assert.h>
  51. #include <arpa/inet.h>
  52. /**
  53. * Functions and types for CRC checks.
  54. *
  55. * Generated on Wed Jan 8 15:14:20 2014,
  56. * by pycrc v0.8.1, http://www.tty1.net/pycrc/
  57. * using the configuration:
  58. * Width = 15
  59. * Poly = 0x4599
  60. * XorIn = 0x0000
  61. * ReflectIn = False
  62. * XorOut = 0x0000
  63. * ReflectOut = False
  64. * Algorithm = table-driven
  65. *****************************************************************************/
  66. typedef uint16_t crc_t;
  67. /**
  68. * Static table used for the table_driven implementation.
  69. *****************************************************************************/
  70. static const crc_t crc_table[256] = {
  71. 0x0000, 0x4599, 0x4eab, 0x0b32, 0x58cf, 0x1d56, 0x1664, 0x53fd, 0x7407, 0x319e, 0x3aac, 0x7f35, 0x2cc8, 0x6951, 0x6263, 0x27fa,
  72. 0x2d97, 0x680e, 0x633c, 0x26a5, 0x7558, 0x30c1, 0x3bf3, 0x7e6a, 0x5990, 0x1c09, 0x173b, 0x52a2, 0x015f, 0x44c6, 0x4ff4, 0x0a6d,
  73. 0x5b2e, 0x1eb7, 0x1585, 0x501c, 0x03e1, 0x4678, 0x4d4a, 0x08d3, 0x2f29, 0x6ab0, 0x6182, 0x241b, 0x77e6, 0x327f, 0x394d, 0x7cd4,
  74. 0x76b9, 0x3320, 0x3812, 0x7d8b, 0x2e76, 0x6bef, 0x60dd, 0x2544, 0x02be, 0x4727, 0x4c15, 0x098c, 0x5a71, 0x1fe8, 0x14da, 0x5143,
  75. 0x73c5, 0x365c, 0x3d6e, 0x78f7, 0x2b0a, 0x6e93, 0x65a1, 0x2038, 0x07c2, 0x425b, 0x4969, 0x0cf0, 0x5f0d, 0x1a94, 0x11a6, 0x543f,
  76. 0x5e52, 0x1bcb, 0x10f9, 0x5560, 0x069d, 0x4304, 0x4836, 0x0daf, 0x2a55, 0x6fcc, 0x64fe, 0x2167, 0x729a, 0x3703, 0x3c31, 0x79a8,
  77. 0x28eb, 0x6d72, 0x6640, 0x23d9, 0x7024, 0x35bd, 0x3e8f, 0x7b16, 0x5cec, 0x1975, 0x1247, 0x57de, 0x0423, 0x41ba, 0x4a88, 0x0f11,
  78. 0x057c, 0x40e5, 0x4bd7, 0x0e4e, 0x5db3, 0x182a, 0x1318, 0x5681, 0x717b, 0x34e2, 0x3fd0, 0x7a49, 0x29b4, 0x6c2d, 0x671f, 0x2286,
  79. 0x2213, 0x678a, 0x6cb8, 0x2921, 0x7adc, 0x3f45, 0x3477, 0x71ee, 0x5614, 0x138d, 0x18bf, 0x5d26, 0x0edb, 0x4b42, 0x4070, 0x05e9,
  80. 0x0f84, 0x4a1d, 0x412f, 0x04b6, 0x574b, 0x12d2, 0x19e0, 0x5c79, 0x7b83, 0x3e1a, 0x3528, 0x70b1, 0x234c, 0x66d5, 0x6de7, 0x287e,
  81. 0x793d, 0x3ca4, 0x3796, 0x720f, 0x21f2, 0x646b, 0x6f59, 0x2ac0, 0x0d3a, 0x48a3, 0x4391, 0x0608, 0x55f5, 0x106c, 0x1b5e, 0x5ec7,
  82. 0x54aa, 0x1133, 0x1a01, 0x5f98, 0x0c65, 0x49fc, 0x42ce, 0x0757, 0x20ad, 0x6534, 0x6e06, 0x2b9f, 0x7862, 0x3dfb, 0x36c9, 0x7350,
  83. 0x51d6, 0x144f, 0x1f7d, 0x5ae4, 0x0919, 0x4c80, 0x47b2, 0x022b, 0x25d1, 0x6048, 0x6b7a, 0x2ee3, 0x7d1e, 0x3887, 0x33b5, 0x762c,
  84. 0x7c41, 0x39d8, 0x32ea, 0x7773, 0x248e, 0x6117, 0x6a25, 0x2fbc, 0x0846, 0x4ddf, 0x46ed, 0x0374, 0x5089, 0x1510, 0x1e22, 0x5bbb,
  85. 0x0af8, 0x4f61, 0x4453, 0x01ca, 0x5237, 0x17ae, 0x1c9c, 0x5905, 0x7eff, 0x3b66, 0x3054, 0x75cd, 0x2630, 0x63a9, 0x689b, 0x2d02,
  86. 0x276f, 0x62f6, 0x69c4, 0x2c5d, 0x7fa0, 0x3a39, 0x310b, 0x7492, 0x5368, 0x16f1, 0x1dc3, 0x585a, 0x0ba7, 0x4e3e, 0x450c, 0x0095
  87. };
  88. /**
  89. * Update the crc value with new data.
  90. *
  91. * \param crc The current crc value.
  92. * \param data Pointer to a buffer of \a data_len bytes.
  93. * \param data_len Number of bytes in the \a data buffer.
  94. * \return The updated crc value.
  95. *****************************************************************************/
  96. static crc_t crc_update_bytewise(crc_t crc, const unsigned char *data, size_t data_len)
  97. {
  98. unsigned int tbl_idx;
  99. while (data_len--) {
  100. tbl_idx = ((crc >> 7) ^ *data) & 0xff;
  101. crc = (crc_table[tbl_idx] ^ (crc << 8)) & 0x7fff;
  102. data++;
  103. }
  104. return crc & 0x7fff;
  105. }
  106. /**
  107. * Update the crc value with new data.
  108. *
  109. * \param crc The current crc value.
  110. * \param data Data value
  111. * \param bits The number of most significant bits in data used for CRC calculation
  112. * \return The updated crc value.
  113. *****************************************************************************/
  114. static crc_t crc_update_bitwise(crc_t crc, uint8_t data, size_t bits)
  115. {
  116. uint8_t i;
  117. bool bit;
  118. for (i = 0x80; bits--; i >>= 1) {
  119. bit = crc & 0x4000;
  120. if (data & i) {
  121. bit = !bit;
  122. }
  123. crc <<= 1;
  124. if (bit) {
  125. crc ^= 0x4599;
  126. }
  127. }
  128. return crc & 0x7fff;
  129. }
  130. static crc_t calc_bitmap_crc(uint8_t *bitmap, unsigned start, unsigned end)
  131. {
  132. crc_t crc = 0;
  133. if (start % 8) {
  134. crc = crc_update_bitwise(crc, bitmap[start / 8] << (start % 8), 8 - start % 8);
  135. start += 8 - start % 8;
  136. }
  137. crc = crc_update_bytewise(crc, &bitmap[start / 8], (end - start) / 8);
  138. crc = crc_update_bitwise(crc, bitmap[end / 8], end % 8);
  139. return crc;
  140. }
  141. static unsigned cfl_exact(struct can_frame *frame)
  142. {
  143. uint8_t bitmap[16];
  144. unsigned start = 0, end;
  145. crc_t crc;
  146. uint16_t crc_be;
  147. uint8_t mask, lookfor;
  148. unsigned i, stuffed;
  149. const int8_t clz[32] = /* count of leading zeros in 5 bit numbers */
  150. { 5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  151. /* Prepare bitmap */
  152. memset(bitmap, 0, sizeof(bitmap));
  153. if (frame->can_id & CAN_EFF_FLAG) {
  154. /* bit 7 0 7 0 7 0 7 0
  155. * bitmap[0-3] |.sBBBBBB BBBBBSIE EEEEEEEE EEEEEEEE| s = SOF, B = Base ID (11 bits), S = SRR, I = IDE, E = Extended ID (18 bits)
  156. * bitmap[4-7] |ER10DLC4 00000000 11111111 22222222| R = RTR, 0 = r0, 1 = r1, DLC4 = DLC, Data bytes
  157. * bitmap[8-11] |33333333 44444444 55555555 66666666| Data bytes
  158. * bitmap[12-15] |77777777 ........ ........ ........| Data bytes
  159. */
  160. bitmap[0] = (frame->can_id & CAN_EFF_MASK) >> 23;
  161. bitmap[1] = ((frame->can_id >> 18) & 0x3f) << 3 |
  162. 3 << 1 | /* SRR, IDE */
  163. ((frame->can_id >> 17) & 0x01);
  164. bitmap[2] = (frame->can_id >> 9) & 0xff;
  165. bitmap[3] = (frame->can_id >> 1) & 0xff;
  166. bitmap[4] = (frame->can_id & 0x1) << 7 |
  167. (!!(frame->can_id & CAN_RTR_FLAG)) << 6 |
  168. 0 << 4 | /* r1, r0 */
  169. (frame->can_dlc & 0xf);
  170. memcpy(&bitmap[5], &frame->data, frame->can_dlc);
  171. start = 1;
  172. end = 40 + 8*frame->can_dlc;
  173. } else {
  174. /* bit 7 0 7 0 7 0 7 0
  175. * bitmap[0-3] |.....sII IIIIIIII IRE0DLC4 00000000| s = SOF, I = ID (11 bits), R = RTR, E = IDE, DLC4 = DLC
  176. * bitmap[4-7] |11111111 22222222 33333333 44444444| Data bytes
  177. * bitmap[8-11] |55555555 66666666 77777777 ........| Data bytes
  178. */
  179. bitmap[0] = (frame->can_id & CAN_SFF_MASK) >> 9;
  180. bitmap[1] = (frame->can_id >> 1) & 0xff;
  181. bitmap[2] = ((frame->can_id << 7) & 0xff) |
  182. (!!(frame->can_id & CAN_RTR_FLAG)) << 6 |
  183. 0 << 4 | /* IDE, r0 */
  184. (frame->can_dlc & 0xf);
  185. memcpy(&bitmap[3], &frame->data, frame->can_dlc);
  186. start = 5;
  187. end = 24 + 8 * frame->can_dlc;
  188. }
  189. /* Calc and append CRC */
  190. crc = calc_bitmap_crc(bitmap, start, end);
  191. crc_be = htons(crc << 1);
  192. assert(end % 8 == 0);
  193. memcpy(bitmap + end / 8, &crc_be, 2);
  194. end += 15;
  195. /* Count stuffed bits */
  196. mask = 0x1f;
  197. lookfor = 0;
  198. i = start;
  199. stuffed = 0;
  200. while (i < end) {
  201. unsigned change;
  202. unsigned bits = (bitmap[i / 8] << 8 | bitmap[i / 8 + 1]) >> (16 - 5 - i % 8);
  203. lookfor = lookfor ? 0 : mask; /* We alternate between looking for a series of zeros or ones */
  204. change = (bits & mask) ^ lookfor; /* 1 indicates a change */
  205. if (change) { /* No bit was stuffed here */
  206. i += clz[change];
  207. mask = 0x1f; /* Next look for 5 same bits */
  208. } else {
  209. i += (mask == 0x1f) ? 5 : 4;
  210. if (i <= end) {
  211. stuffed++;
  212. mask = 0x1e; /* Next look for 4 bits (5th bit is the stuffed one) */
  213. }
  214. }
  215. }
  216. return end - start + stuffed +
  217. 3 + /* CRC del, ACK, ACK del */
  218. 7 + /* EOF */
  219. 3; /* IFS */
  220. }
  221. unsigned can_frame_length(struct canfd_frame *frame, enum cfl_mode mode, int mtu)
  222. {
  223. int eff = (frame->can_id & CAN_EFF_FLAG);
  224. if (mtu != CAN_MTU)
  225. return 0; /* CANFD is not supported yet */
  226. switch (mode) {
  227. case CFL_NO_BITSTUFFING:
  228. return (eff ? 67 : 47) + frame->len * 8;
  229. case CFL_WORSTCASE:
  230. return (eff ? 80 : 55) + frame->len * 10;
  231. case CFL_EXACT:
  232. return cfl_exact((struct can_frame*)frame);
  233. }
  234. return 0; /* Unknown mode */
  235. }