fasthdlc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * Mark's Mythical Table-based raw HDLC implementation
  3. *
  4. * This is designed to be a very fast, but memory efficient
  5. * implementation of standard HDLC protocol.
  6. *
  7. * This table based HDLC technology is PATENT PENDING, but will always be
  8. * remain freely distributable under the terms of the GPL version 2.
  9. *
  10. * For non-GPL licensing, please contact Mark Spencer at
  11. * the below e-mail address.
  12. *
  13. * Copyright (C) 2001-2008, Digium, Inc.
  14. *
  15. * Written by Mark Spencer <markster@digium.com>
  16. *
  17. */
  18. /*
  19. * See http://www.asterisk.org for more information about
  20. * the Asterisk project. Please do not directly contact
  21. * any of the maintainers of this project for assistance;
  22. * the project provides a web site, mailing lists and IRC
  23. * channels for your use.
  24. *
  25. * This program is free software, distributed under the terms of
  26. * the GNU General Public License Version 2 as published by the
  27. * Free Software Foundation. See the LICENSE file included with
  28. * this program for more details.
  29. */
  30. #ifndef _FASTHDLC_H
  31. #define _FASTHDLC_H
  32. enum fasthdlc_mode {
  33. FASTHDLC_MODE_64 = 0,
  34. FASTHDLC_MODE_56,
  35. FASTHDLC_MODE_16,
  36. };
  37. struct fasthdlc_state {
  38. int state; /* What state we are in */
  39. unsigned int data; /* Our current data queue */
  40. int bits; /* Number of bits in our data queue */
  41. int ones; /* Number of ones */
  42. enum fasthdlc_mode mode;
  43. unsigned int minbits;
  44. };
  45. #ifdef FAST_HDLC_NEED_TABLES
  46. #define RETURN_COMPLETE_FLAG (0x1000)
  47. #define RETURN_DISCARD_FLAG (0x2000)
  48. #define RETURN_EMPTY_FLAG (0x4000)
  49. /* Unlike most HDLC implementations, we define only two states,
  50. when we are in a valid frame, and when we are searching for
  51. a frame header */
  52. #define FRAME_SEARCH 0
  53. #define PROCESS_FRAME 1
  54. /*
  55. HDLC Search State table -- Look for a frame header. The return value
  56. of this table is as follows:
  57. |---8---|---7---|---6---|---5---|---4---|---3---|---2---|---1---|
  58. | Z E R O E S | Next | Bits Consumed |
  59. |-------|-------|-------|-------|-------|-------|-------|-------|
  60. The indexes for this table are the state (0 or 1) and the next 8
  61. bits of the stream.
  62. Note that this table is only used for state 0 and 1.
  63. The user should discard the top "bits consumed" bits of data before
  64. the next call. "Next state" represents the actual next state for
  65. decoding.
  66. */
  67. static unsigned char hdlc_search[256];
  68. /*
  69. HDLC Data Table
  70. The indexes to this table are the number of one's we've seen so far (0-5) and
  71. the next 10 bits of input (which is enough to guarantee us that we
  72. will retrieve at least one byte of data (or frame or whatever).
  73. The format for the return value is:
  74. Bits 15: Status (1=Valid Data, 0=Control Frame (see bits 7-0 for type))
  75. Bits 14-12: Number of ones in a row, so far
  76. Bits 11-8: The number of bits consumed (0-10)
  77. Bits 7-0: The return data (if appropriate)
  78. The next state is simply bit #15
  79. */
  80. #define CONTROL_COMPLETE 1
  81. #define CONTROL_ABORT 2
  82. #define STATUS_MASK (1 << 15)
  83. #define STATUS_VALID (1 << 15)
  84. #define STATUS_CONTROL (0 << 15)
  85. #define STATE_MASK (1 << 15)
  86. #define ONES_MASK (7 << 12)
  87. #define DATA_MASK (0xff)
  88. static unsigned short hdlc_frame[6][1024];
  89. static unsigned int minbits[2] = { 8, 10 };
  90. /*
  91. Last, but not least, we have the encoder table. It takes
  92. as its indices the number of ones so far and a byte of data
  93. and returns an int composed of the following fields:
  94. Bots 31-22: Actual Data
  95. Bits 21-16: Unused
  96. Bits 15-8: Number of ones
  97. Bits 3-0: Number of bits of output (13-4) to use
  98. Of course we could optimize by reducing to two tables, but I don't
  99. really think it's worth the trouble at this point.
  100. */
  101. static unsigned int hdlc_encode[6][256];
  102. static inline char hdlc_search_precalc(unsigned char c)
  103. {
  104. int x, p=0;
  105. /* Look for a flag. If this isn't a flag,
  106. line us up for the next possible shot at
  107. a flag */
  108. /* If it's a flag, we go to state 1, and have
  109. consumed 8 bits */
  110. if (c == 0x7e)
  111. return 0x10 | 8;
  112. /* If it's an abort, we stay in the same state
  113. and have consumed 8 bits */
  114. if (c == 0x7f)
  115. return 0x00 | 8;
  116. /* If it's all 1's, we state in the same state and
  117. have consumed 8 bits */
  118. if (c == 0xff)
  119. return 0x00 | 8;
  120. /* If we get here, we must have at least one zero in us
  121. but we're not the flag. So, start at the end (LSB) and
  122. work our way to the top (MSB) looking for a zero. The
  123. position of that 0 is most optimistic start of a real
  124. frame header */
  125. x=1;
  126. p=7;
  127. while(p && (c & x)) {
  128. x <<= 1;
  129. p--;
  130. }
  131. return p;
  132. }
  133. #ifdef DEBUG_PRECALC
  134. static inline void hdlc_search_print(char c, char r)
  135. {
  136. int x=0x80;
  137. while(x) {
  138. printf("%s", c & x ? "1" : "0");
  139. x >>= 1;
  140. }
  141. printf(" => State %d, Consume %d\n", (r & 0x10) >> 4, r & 0xf);
  142. }
  143. #endif
  144. #define HFP(status, ones, bits, data) \
  145. ((status) | ((ones) << 12) | ((bits) << 8) | (data))
  146. static inline unsigned int hdlc_frame_precalc(unsigned char x, unsigned short c)
  147. {
  148. /* Assume we have seen 'x' one's so far, and have read the
  149. bottom 10 bytes of c (MSB first). Now, we HAVE to have
  150. a byte of data or a frame or something. We are assumed
  151. to be at the beginning of a byte of data or something */
  152. unsigned char ones = x;
  153. unsigned char data=0;
  154. int bits=0;
  155. int consumed=0;
  156. while(bits < 8) {
  157. data >>=1;
  158. consumed++;
  159. if (ones == 5) {
  160. /* We've seen five ones */
  161. if (c & 0x0200) {
  162. /* Another one -- Some sort of signal frame */
  163. if ((!(c & 0x0100)) && (bits == 6)) {
  164. /* This is a frame terminator (10) */
  165. return HFP(0,
  166. 0, 8, CONTROL_COMPLETE);
  167. } else {
  168. /* Yuck! It's something else...
  169. Abort this entire frame, and
  170. start looking for a good frame */
  171. return HFP(0,
  172. 0, consumed+1, CONTROL_ABORT);
  173. }
  174. } else {
  175. /* It's an inserted zero, just skip it */
  176. ones = 0;
  177. data <<= 1;
  178. }
  179. } else {
  180. /* Add it to our bit list, LSB to
  181. MSB */
  182. if (c & 0x0200) {
  183. data |= 0x80;
  184. ones++;
  185. } else
  186. ones=0;
  187. bits++;
  188. }
  189. c <<= 1;
  190. }
  191. /* Consume the extra 0 now rather than later. */
  192. if (ones == 5) {
  193. ones = 0;
  194. consumed++;
  195. }
  196. return HFP(STATUS_VALID, ones, consumed, data);
  197. }
  198. #ifdef DEBUG_PRECALC
  199. static inline void hdlc_frame_print(unsigned char x, unsigned short c, unsigned int res)
  200. {
  201. int z=0x0200;
  202. char *status[] = {
  203. "Control",
  204. "Valid",
  205. };
  206. printf("%d one's then ", x);
  207. while(z) {
  208. printf("%s", c & z ? "1" : "0");
  209. z >>= 1;
  210. }
  211. printf(" => Status %s, ", res & STATUS_MASK ? "1" : "0");
  212. printf("Consumed: %d, ", (res & 0x0f00) >> 8);
  213. printf("Status: %s, ", status[(res & STATUS_MASK) >> 15]);
  214. printf("Ones: %d, ", (res & ONES_MASK) >> 12);
  215. printf("Data: %02x\n", res & 0xff);
  216. }
  217. #endif
  218. static inline unsigned int hdlc_encode_precalc(int x, unsigned char y)
  219. {
  220. int bits=0;
  221. int ones=x;
  222. unsigned short data=0;
  223. int z;
  224. for (z=0;z<8;z++) {
  225. /* Zero-stuff if needed */
  226. if (ones == 5) {
  227. /* Stuff a zero */
  228. data <<= 1;
  229. ones=0;
  230. bits++;
  231. }
  232. if (y & 0x01) {
  233. /* There's a one */
  234. data <<= 1;
  235. data |= 0x1;
  236. ones++;
  237. bits++;
  238. } else {
  239. data <<= 1;
  240. ones = 0;
  241. bits++;
  242. }
  243. y >>= 1;
  244. }
  245. /* Special case -- Stuff the zero at the end if appropriate */
  246. if (ones == 5) {
  247. /* Stuff a zero */
  248. data <<= 1;
  249. ones=0;
  250. bits++;
  251. }
  252. data <<= (10-bits);
  253. return (data << 22) | (ones << 8) | (bits);
  254. }
  255. #ifdef DEBUG_PRECALC
  256. static inline void hdlc_encode_print(int x, unsigned char y, unsigned int val)
  257. {
  258. unsigned int z;
  259. unsigned short c;
  260. printf("%d ones, %02x (", x, y);
  261. z = 0x80;
  262. while(z) {
  263. printf("%s", y & z ? "1" : "0");
  264. z >>= 1;
  265. }
  266. printf(") encoded as ");
  267. z = 1 << 31;
  268. for (x=0;x<(val & 0xf);x++) {
  269. printf("%s", val & z ? "1" : "0");
  270. z >>= 1;
  271. }
  272. printf(" with %d ones now, %d bits in len\n", (val & 0xf00) >> 8, val & 0xf);
  273. }
  274. #endif
  275. static inline void fasthdlc_precalc(void)
  276. {
  277. int x;
  278. int y;
  279. /* First the easy part -- the searching */
  280. for (x=0;x<256;x++) {
  281. hdlc_search[x] = hdlc_search_precalc(x);
  282. #ifdef DEBUG_PRECALC
  283. hdlc_search_print(x, hdlc_search[x]);
  284. #endif
  285. }
  286. /* Now the hard part -- the frame tables */
  287. for (x=0;x<6;x++) {
  288. /* Given the # of preceeding ones, process the next
  289. byte of input (up to 10 actual bits) */
  290. for (y=0;y<1024;y++) {
  291. hdlc_frame[x][y] = hdlc_frame_precalc(x, y);
  292. #ifdef DEBUG_PRECALC
  293. hdlc_frame_print(x, y, hdlc_frame[x][y]);
  294. #endif
  295. }
  296. }
  297. /* Now another not-so-hard part, the encoding table */
  298. for (x=0;x<6;x++) {
  299. for (y=0;y<256;y++) {
  300. hdlc_encode[x][y] = hdlc_encode_precalc(x,y);
  301. #ifdef DEBUG_PRECALC
  302. hdlc_encode_print(x,y,hdlc_encode[x][y]);
  303. #endif
  304. }
  305. }
  306. }
  307. static inline void fasthdlc_init(struct fasthdlc_state *h, enum fasthdlc_mode mode)
  308. {
  309. /* Initializes all states appropriately */
  310. h->mode = mode;
  311. h->state = 0;
  312. h->bits = 0;
  313. h->data = 0;
  314. h->ones = 0;
  315. switch (mode) {
  316. case FASTHDLC_MODE_64:
  317. h->minbits = 8;
  318. break;
  319. case FASTHDLC_MODE_56:
  320. h->minbits = 7;
  321. break;
  322. case FASTHDLC_MODE_16:
  323. h->minbits = 2;
  324. break;
  325. }
  326. }
  327. static inline int fasthdlc_tx_load_nocheck(struct fasthdlc_state *h, unsigned char c)
  328. {
  329. unsigned int res;
  330. res = hdlc_encode[h->ones][c];
  331. h->ones = (res & 0xf00) >> 8;
  332. h->data |= (res & 0xffc00000) >> h->bits;
  333. h->bits += (res & 0xf);
  334. return 0;
  335. }
  336. static inline int fasthdlc_tx_load(struct fasthdlc_state *h, unsigned char c)
  337. {
  338. /* Gotta have at least 10 bits left */
  339. if (h->bits > 22)
  340. return -1;
  341. return fasthdlc_tx_load_nocheck(h, c);
  342. }
  343. static inline int fasthdlc_tx_frame_nocheck(struct fasthdlc_state *h)
  344. {
  345. h->ones = 0;
  346. h->data |= ( 0x7e000000 >> h->bits);
  347. h->bits += 8;
  348. return 0;
  349. }
  350. static inline int fasthdlc_tx_frame(struct fasthdlc_state *h)
  351. {
  352. if (h->bits > 24)
  353. return -1;
  354. return fasthdlc_tx_frame_nocheck(h);
  355. }
  356. static inline int fasthdlc_tx_need_data(struct fasthdlc_state *h)
  357. {
  358. if (h->mode == FASTHDLC_MODE_56) {
  359. if (h->bits < 7)
  360. return 1;
  361. } else if (h->mode == FASTHDLC_MODE_16) {
  362. if (h->bits < 2)
  363. return 1;
  364. } else {
  365. if (h->bits < 8)
  366. return 1;
  367. }
  368. return 0;
  369. }
  370. static inline int fasthdlc_tx_run_nocheck(struct fasthdlc_state *h)
  371. {
  372. unsigned char b;
  373. if (h->mode == FASTHDLC_MODE_16) {
  374. b = h->data >> 30;
  375. h->bits -= 2;
  376. h->data <<= 2;
  377. return (b & 3) << 6;
  378. } else if (h->mode == FASTHDLC_MODE_56) {
  379. b = h->data >> 25;
  380. h->bits -= 7;
  381. h->data <<= 7;
  382. return ((b & 0x7f) << 1) | 1;
  383. } else {
  384. b = h->data >> 24;
  385. h->bits -= 8;
  386. h->data <<= 8;
  387. return b;
  388. }
  389. }
  390. static inline int fasthdlc_tx_run(struct fasthdlc_state *h)
  391. {
  392. if (h->bits < h->minbits)
  393. return -1;
  394. return fasthdlc_tx_run_nocheck(h);
  395. }
  396. static inline int fasthdlc_rx_load_nocheck(struct fasthdlc_state *h, unsigned char b)
  397. {
  398. if (h->mode == FASTHDLC_MODE_16) {
  399. h->data |= (b >> 6) << (30-h->bits);
  400. h->bits += 2;
  401. } else if (h->mode == FASTHDLC_MODE_56) {
  402. h->data |= (b >> 1) << (25-h->bits);
  403. h->bits += 7;
  404. } else {
  405. /* Put the new byte in the data stream */
  406. h->data |= b << (24-h->bits);
  407. h->bits += 8;
  408. }
  409. return 0;
  410. }
  411. static inline int fasthdlc_rx_load(struct fasthdlc_state *h, unsigned char b)
  412. {
  413. /* Make sure we have enough space */
  414. if (h->bits > 24)
  415. return -1;
  416. return fasthdlc_rx_load_nocheck(h, b);
  417. }
  418. /*
  419. Returns a data character if available, logical OR'd with
  420. zero or more of RETURN_COMPLETE_FLAG, RETURN_DISCARD_FLAG,
  421. and RETURN_EMPTY_FLAG, signifying a complete frame, a
  422. discarded frame, or there is nothing to return.
  423. */
  424. static inline int fasthdlc_rx_run(struct fasthdlc_state *h)
  425. {
  426. unsigned short next;
  427. int retval=RETURN_EMPTY_FLAG;
  428. while ((h->bits >= minbits[h->state]) && (retval == RETURN_EMPTY_FLAG)) {
  429. /* Run until we can no longer be assured that we will
  430. have enough bits to continue */
  431. switch(h->state) {
  432. case FRAME_SEARCH:
  433. /* Look for an HDLC frame, keying from
  434. the top byte. */
  435. next = hdlc_search[h->data >> 24];
  436. h->bits -= next & 0x0f;
  437. h->data <<= next & 0x0f;
  438. h->state = next >> 4;
  439. h->ones = 0;
  440. break;
  441. case PROCESS_FRAME:
  442. /* Process as much as the next ten bits */
  443. next = hdlc_frame[h->ones][h->data >> 22];
  444. h->bits -= ((next & 0x0f00) >> 8);
  445. h->data <<= ((next & 0x0f00) >> 8);
  446. h->state = (next & STATE_MASK) >> 15;
  447. h->ones = (next & ONES_MASK) >> 12;
  448. switch(next & STATUS_MASK) {
  449. case STATUS_CONTROL:
  450. if (next & CONTROL_COMPLETE) {
  451. /* A complete, valid frame received */
  452. retval = (RETURN_COMPLETE_FLAG);
  453. /* Stay in this state */
  454. h->state = 1;
  455. } else {
  456. /* An abort (either out of sync of explicit) */
  457. retval = (RETURN_DISCARD_FLAG);
  458. }
  459. break;
  460. case STATUS_VALID:
  461. retval = (next & DATA_MASK);
  462. }
  463. }
  464. }
  465. return retval;
  466. }
  467. #endif /* FAST_HDLC_NEED_TABLES */
  468. #endif