st5481_hdlc.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:15k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * Driver for ST5481 USB ISDN modem
  3.  *
  4.  * Author       Frode Isaksen
  5.  * Copyright    2001 by Frode Isaksen      <fisaksen@bewan.com>
  6.  *              2001 by Kai Germaschewski  <kai.germaschewski@gmx.de>
  7.  * 
  8.  * This software may be used and distributed according to the terms
  9.  * of the GNU General Public License, incorporated herein by reference.
  10.  *
  11.  */
  12. #include "st5481_hdlc.h"
  13. static const unsigned short int crc16_tab[] = {
  14. 0x0000,0x1189,0x2312,0x329b,0x4624,0x57ad,0x6536,0x74bf,
  15. 0x8c48,0x9dc1,0xaf5a,0xbed3,0xca6c,0xdbe5,0xe97e,0xf8f7,
  16. 0x1081,0x0108,0x3393,0x221a,0x56a5,0x472c,0x75b7,0x643e,
  17. 0x9cc9,0x8d40,0xbfdb,0xae52,0xdaed,0xcb64,0xf9ff,0xe876,
  18. 0x2102,0x308b,0x0210,0x1399,0x6726,0x76af,0x4434,0x55bd,
  19. 0xad4a,0xbcc3,0x8e58,0x9fd1,0xeb6e,0xfae7,0xc87c,0xd9f5,
  20. 0x3183,0x200a,0x1291,0x0318,0x77a7,0x662e,0x54b5,0x453c,
  21. 0xbdcb,0xac42,0x9ed9,0x8f50,0xfbef,0xea66,0xd8fd,0xc974,
  22. 0x4204,0x538d,0x6116,0x709f,0x0420,0x15a9,0x2732,0x36bb,
  23. 0xce4c,0xdfc5,0xed5e,0xfcd7,0x8868,0x99e1,0xab7a,0xbaf3,
  24. 0x5285,0x430c,0x7197,0x601e,0x14a1,0x0528,0x37b3,0x263a,
  25. 0xdecd,0xcf44,0xfddf,0xec56,0x98e9,0x8960,0xbbfb,0xaa72,
  26. 0x6306,0x728f,0x4014,0x519d,0x2522,0x34ab,0x0630,0x17b9,
  27. 0xef4e,0xfec7,0xcc5c,0xddd5,0xa96a,0xb8e3,0x8a78,0x9bf1,
  28. 0x7387,0x620e,0x5095,0x411c,0x35a3,0x242a,0x16b1,0x0738,
  29. 0xffcf,0xee46,0xdcdd,0xcd54,0xb9eb,0xa862,0x9af9,0x8b70,
  30. 0x8408,0x9581,0xa71a,0xb693,0xc22c,0xd3a5,0xe13e,0xf0b7,
  31. 0x0840,0x19c9,0x2b52,0x3adb,0x4e64,0x5fed,0x6d76,0x7cff,
  32. 0x9489,0x8500,0xb79b,0xa612,0xd2ad,0xc324,0xf1bf,0xe036,
  33. 0x18c1,0x0948,0x3bd3,0x2a5a,0x5ee5,0x4f6c,0x7df7,0x6c7e,
  34. 0xa50a,0xb483,0x8618,0x9791,0xe32e,0xf2a7,0xc03c,0xd1b5,
  35. 0x2942,0x38cb,0x0a50,0x1bd9,0x6f66,0x7eef,0x4c74,0x5dfd,
  36. 0xb58b,0xa402,0x9699,0x8710,0xf3af,0xe226,0xd0bd,0xc134,
  37. 0x39c3,0x284a,0x1ad1,0x0b58,0x7fe7,0x6e6e,0x5cf5,0x4d7c,
  38. 0xc60c,0xd785,0xe51e,0xf497,0x8028,0x91a1,0xa33a,0xb2b3,
  39. 0x4a44,0x5bcd,0x6956,0x78df,0x0c60,0x1de9,0x2f72,0x3efb,
  40. 0xd68d,0xc704,0xf59f,0xe416,0x90a9,0x8120,0xb3bb,0xa232,
  41. 0x5ac5,0x4b4c,0x79d7,0x685e,0x1ce1,0x0d68,0x3ff3,0x2e7a,
  42. 0xe70e,0xf687,0xc41c,0xd595,0xa12a,0xb0a3,0x8238,0x93b1,
  43. 0x6b46,0x7acf,0x4854,0x59dd,0x2d62,0x3ceb,0x0e70,0x1ff9,
  44. 0xf78f,0xe606,0xd49d,0xc514,0xb1ab,0xa022,0x92b9,0x8330,
  45. 0x7bc7,0x6a4e,0x58d5,0x495c,0x3de3,0x2c6a,0x1ef1,0x0f78
  46. };
  47. enum {
  48. HDLC_FAST_IDLE,HDLC_GET_FLAG_B0,HDLC_GETFLAG_B1A6,HDLC_GETFLAG_B7,
  49. HDLC_GET_DATA,HDLC_FAST_FLAG
  50. };
  51. enum {
  52. HDLC_SEND_DATA,HDLC_SEND_CRC1,HDLC_SEND_FAST_FLAG,
  53. HDLC_SEND_FIRST_FLAG,HDLC_SEND_CRC2,HDLC_SEND_CLOSING_FLAG,
  54. HDLC_SEND_IDLE1,HDLC_SEND_FAST_IDLE,HDLC_SENDFLAG_B0,
  55. HDLC_SENDFLAG_B1A6,HDLC_SENDFLAG_B7,STOPPED
  56. };
  57. void 
  58. hdlc_rcv_init(struct hdlc_vars *hdlc, int do_adapt56)
  59. {
  60.     hdlc->bit_shift = 0;
  61. hdlc->hdlc_bits1 = 0;
  62. hdlc->data_bits = 0;
  63. hdlc->ffbit_shift = 0;
  64. hdlc->data_received = 0;
  65. hdlc->state = HDLC_GET_DATA;
  66. hdlc->do_adapt56 = do_adapt56;
  67. hdlc->dchannel = 0;
  68. hdlc->crc = 0;
  69. hdlc->cbin = 0;
  70. hdlc->shift_reg = 0;
  71. hdlc->ffvalue = 0;
  72. hdlc->dstpos = 0;
  73. }
  74. void 
  75. hdlc_out_init(struct hdlc_vars *hdlc, int is_d_channel, int do_adapt56)
  76. {
  77.     hdlc->bit_shift = 0;
  78. hdlc->hdlc_bits1 = 0;
  79. hdlc->data_bits = 0;
  80. hdlc->ffbit_shift = 0;
  81. hdlc->data_received = 0;
  82. hdlc->do_closing = 0;
  83. hdlc->ffvalue = 0;
  84. if (is_d_channel) {
  85. hdlc->dchannel = 1;
  86. hdlc->state = HDLC_SEND_FIRST_FLAG;
  87. } else {
  88. hdlc->dchannel = 0;
  89. hdlc->state = HDLC_SEND_FAST_FLAG;
  90. hdlc->ffvalue = 0x7e;
  91. hdlc->cbin = 0x7e;
  92. hdlc->bit_shift = 0;
  93. if(do_adapt56){
  94. hdlc->do_adapt56 = 1;
  95. hdlc->data_bits = 0;
  96. hdlc->state = HDLC_SENDFLAG_B0;
  97. } else {
  98. hdlc->do_adapt56 = 0;
  99. hdlc->data_bits = 8;
  100. }
  101. hdlc->shift_reg = 0;
  102. }
  103. /*
  104.   hdlc_decode - decodes HDLC frames from a transparent bit stream.
  105.   The source buffer is scanned for valid HDLC frames looking for
  106.   flags (01111110) to indicate the start of a frame. If the start of
  107.   the frame is found, the bit stuffing is removed (0 after 5 1's).
  108.   When a new flag is found, the complete frame has been received
  109.   and the CRC is checked.
  110.   If a valid frame is found, the function returns the frame length 
  111.   excluding the CRC with the bit HDLC_END_OF_FRAME set.
  112.   If the beginning of a valid frame is found, the function returns
  113.   the length. 
  114.   If a framing error is found (too many 1s and not a flag) the function 
  115.   returns the length with the bit HDLC_FRAMING_ERROR set.
  116.   If a CRC error is found the function returns the length with the
  117.   bit HDLC_CRC_ERROR set.
  118.   If the frame length exceeds the destination buffer size, the function
  119.   returns the length with the bit HDLC_LENGTH_ERROR set.
  120.   src - source buffer
  121.   slen - source buffer length
  122.   count - number of bytes removed (decoded) from the source buffer
  123.   dst _ destination buffer
  124.   dsize - destination buffer size
  125.   returns - number of decoded bytes in the destination buffer and status
  126.   flag.
  127.  */
  128. int hdlc_decode(struct hdlc_vars *hdlc, const unsigned char *src,
  129. int slen, int *count, unsigned char *dst, int dsize)
  130. {
  131. int status=0;
  132. static const unsigned char fast_flag[]={
  133. 0x00,0x00,0x00,0x20,0x30,0x38,0x3c,0x3e,0x3f
  134. };
  135. static const unsigned char fast_flag_value[]={
  136. 0x00,0x7e,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f
  137. };
  138. static const unsigned char fast_abort[]={
  139. 0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff
  140. };
  141. *count = slen;
  142. while(slen > 0){
  143. if(hdlc->bit_shift==0){
  144. hdlc->cbin = *src++;
  145. slen--;
  146. hdlc->bit_shift = 8;
  147. if(hdlc->do_adapt56){
  148. hdlc->bit_shift --;
  149. }
  150. }
  151. switch(hdlc->state){
  152. case STOPPED:
  153. return 0;
  154. case HDLC_FAST_IDLE:
  155. if(hdlc->cbin == 0xff){
  156. hdlc->bit_shift = 0;
  157. break;
  158. }
  159. hdlc->state = HDLC_GET_FLAG_B0;
  160. hdlc->hdlc_bits1 = 0;
  161. hdlc->bit_shift = 8;
  162. break;
  163. case HDLC_GET_FLAG_B0:
  164. if(!(hdlc->cbin & 0x80)) {
  165. hdlc->state = HDLC_GETFLAG_B1A6;
  166. hdlc->hdlc_bits1 = 0;
  167. } else {
  168. if(!hdlc->do_adapt56){
  169. if(++hdlc->hdlc_bits1 >=8 ) if(hdlc->bit_shift==1)
  170. hdlc->state = HDLC_FAST_IDLE;
  171. }
  172. }
  173. hdlc->cbin<<=1;
  174. hdlc->bit_shift --;
  175. break;
  176. case HDLC_GETFLAG_B1A6:
  177. if(hdlc->cbin & 0x80){
  178. hdlc->hdlc_bits1++;
  179. if(hdlc->hdlc_bits1==6){
  180. hdlc->state = HDLC_GETFLAG_B7;
  181. }
  182. } else {
  183. hdlc->hdlc_bits1 = 0;
  184. }
  185. hdlc->cbin<<=1;
  186. hdlc->bit_shift --;
  187. break;
  188. case HDLC_GETFLAG_B7:
  189. if(hdlc->cbin & 0x80) {
  190. hdlc->state = HDLC_GET_FLAG_B0;
  191. } else {
  192. hdlc->state = HDLC_GET_DATA;
  193. hdlc->crc = 0xffff;
  194. hdlc->shift_reg = 0;
  195. hdlc->hdlc_bits1 = 0;
  196. hdlc->data_bits = 0;
  197. hdlc->data_received = 0;
  198. }
  199. hdlc->cbin<<=1;
  200. hdlc->bit_shift --;
  201. break;
  202. case HDLC_GET_DATA:
  203. if(hdlc->cbin & 0x80){
  204. hdlc->hdlc_bits1++;
  205. switch(hdlc->hdlc_bits1){
  206. case 6:
  207. break;
  208. case 7:
  209. if(hdlc->data_received) {
  210. // bad frame
  211. status = -HDLC_FRAMING_ERROR;
  212. }
  213. if(!hdlc->do_adapt56){
  214. if(hdlc->cbin==fast_abort[hdlc->bit_shift+1]){
  215. hdlc->state = HDLC_FAST_IDLE;
  216. hdlc->bit_shift=1;
  217. break;
  218. }
  219. } else {
  220. hdlc->state = HDLC_GET_FLAG_B0;
  221. }
  222. break;
  223. default:
  224. hdlc->shift_reg>>=1;
  225. hdlc->shift_reg |= 0x80;
  226. hdlc->data_bits++;
  227. break;
  228. }
  229. } else {
  230. switch(hdlc->hdlc_bits1){
  231. case 5:
  232. break;
  233. case 6:
  234. if(hdlc->data_received){
  235. if (hdlc->dstpos < 2) {
  236. status = -HDLC_FRAMING_ERROR;
  237. } else if (hdlc->crc != 0xf0b8){
  238. // crc error
  239. status = -HDLC_CRC_ERROR;
  240. } else {
  241. // remove CRC
  242. hdlc->dstpos -= 2;
  243. // good frame
  244. status = hdlc->dstpos;
  245. }
  246. }
  247. hdlc->crc = 0xffff;
  248. hdlc->shift_reg = 0;
  249. hdlc->data_bits = 0;
  250. if(!hdlc->do_adapt56){
  251. if(hdlc->cbin==fast_flag[hdlc->bit_shift]){
  252. hdlc->ffvalue = fast_flag_value[hdlc->bit_shift];
  253. hdlc->state = HDLC_FAST_FLAG;
  254. hdlc->ffbit_shift = hdlc->bit_shift;
  255. hdlc->bit_shift = 1;
  256. } else {
  257. hdlc->state = HDLC_GET_DATA;
  258. hdlc->data_received = 0;
  259. }
  260. } else {
  261. hdlc->state = HDLC_GET_DATA;
  262. hdlc->data_received = 0;
  263. }
  264. break;
  265. default:
  266. hdlc->shift_reg>>=1;
  267. hdlc->data_bits++;
  268. break;
  269. }
  270. hdlc->hdlc_bits1 = 0;
  271. }
  272. if (status) {
  273. hdlc->dstpos = 0;
  274. *count -= slen;
  275. hdlc->cbin <<= 1;
  276. hdlc->bit_shift--;
  277. return status;
  278. }
  279. if(hdlc->data_bits==8){
  280. unsigned cval;
  281. hdlc->data_bits = 0;
  282. hdlc->data_received = 1;
  283. cval = (hdlc->crc^hdlc->shift_reg) & 0xff;
  284. hdlc->crc = (hdlc->crc>>8)^crc16_tab[cval];
  285. // good byte received
  286. if (dsize--) {
  287. dst[hdlc->dstpos++] = hdlc->shift_reg;
  288. } else {
  289. // frame too long
  290. status = -HDLC_LENGTH_ERROR;
  291. hdlc->dstpos = 0;
  292. }
  293. }
  294. hdlc->cbin <<= 1;
  295. hdlc->bit_shift--;
  296. break;
  297. case HDLC_FAST_FLAG:
  298. if(hdlc->cbin==hdlc->ffvalue){
  299. hdlc->bit_shift = 0;
  300. break;
  301. } else {
  302. if(hdlc->cbin == 0xff){
  303. hdlc->state = HDLC_FAST_IDLE;
  304. hdlc->bit_shift=0;
  305. } else if(hdlc->ffbit_shift==8){
  306. hdlc->state = HDLC_GETFLAG_B7;
  307. break;
  308. } else {
  309. hdlc->shift_reg = fast_abort[hdlc->ffbit_shift-1];
  310. hdlc->hdlc_bits1 = hdlc->ffbit_shift-2;
  311. if(hdlc->hdlc_bits1<0)hdlc->hdlc_bits1 = 0;
  312. hdlc->data_bits = hdlc->ffbit_shift-1;
  313. hdlc->state = HDLC_GET_DATA;
  314. hdlc->data_received = 0;
  315. }
  316. }
  317. break;
  318. default:
  319. break;
  320. }
  321. }
  322. *count -= slen;
  323. return 0;
  324. }
  325. /*
  326.   hdlc_encode - encodes HDLC frames to a transparent bit stream.
  327.   The bit stream starts with a beginning flag (01111110). After
  328.   that each byte is added to the bit stream with bit stuffing added
  329.   (0 after 5 1's).
  330.   When the last byte has been removed from the source buffer, the
  331.   CRC (2 bytes is added) and the frame terminates with the ending flag.
  332.   For the dchannel, the idle character (all 1's) is also added at the end.
  333.   If this function is called with empty source buffer (slen=0), flags or
  334.   idle character will be generated.
  335.  
  336.   src - source buffer
  337.   slen - source buffer length
  338.   count - number of bytes removed (encoded) from source buffer
  339.   dst _ destination buffer
  340.   dsize - destination buffer size
  341.   returns - number of encoded bytes in the destination buffer
  342. */
  343. int hdlc_encode(struct hdlc_vars *hdlc, const unsigned char *src, 
  344. unsigned short slen, int *count,
  345. unsigned char *dst, int dsize)
  346. {
  347. static const unsigned char xfast_flag_value[] = {
  348. 0x7e,0x3f,0x9f,0xcf,0xe7,0xf3,0xf9,0xfc,0x7e
  349. };
  350. int len = 0;
  351. *count = slen;
  352. while (dsize > 0) {
  353. if(hdlc->bit_shift==0){
  354. if(slen && !hdlc->do_closing){
  355. hdlc->shift_reg = *src++;
  356. slen--;
  357. if (slen == 0) 
  358. hdlc->do_closing = 1;  /* closing sequence, CRC + flag(s) */
  359. hdlc->bit_shift = 8;
  360. } else {
  361. if(hdlc->state == HDLC_SEND_DATA){
  362. if(hdlc->data_received){
  363. hdlc->state = HDLC_SEND_CRC1;
  364. hdlc->crc ^= 0xffff;
  365. hdlc->bit_shift = 8;
  366. hdlc->shift_reg = hdlc->crc & 0xff;
  367. } else if(!hdlc->do_adapt56){
  368. hdlc->state = HDLC_SEND_FAST_FLAG;
  369. } else {
  370. hdlc->state = HDLC_SENDFLAG_B0;
  371. }
  372. }
  373.   
  374. }
  375. }
  376. switch(hdlc->state){
  377. case STOPPED:
  378. while (dsize--)
  379. *dst++ = 0xff;
  380.   
  381. return dsize;
  382. case HDLC_SEND_FAST_FLAG:
  383. hdlc->do_closing = 0;
  384. if(slen == 0){
  385. *dst++ = hdlc->ffvalue;
  386. len++;
  387. dsize--;
  388. break;
  389. }
  390. if(hdlc->bit_shift==8){
  391. hdlc->cbin = hdlc->ffvalue>>(8-hdlc->data_bits);
  392. hdlc->state = HDLC_SEND_DATA;
  393. hdlc->crc = 0xffff;
  394. hdlc->hdlc_bits1 = 0;
  395. hdlc->data_received = 1;
  396. }
  397. break;
  398. case HDLC_SENDFLAG_B0:
  399. hdlc->do_closing = 0;
  400. hdlc->cbin <<= 1;
  401. hdlc->data_bits++;
  402. hdlc->hdlc_bits1 = 0;
  403. hdlc->state = HDLC_SENDFLAG_B1A6;
  404. break;
  405. case HDLC_SENDFLAG_B1A6:
  406. hdlc->cbin <<= 1;
  407. hdlc->data_bits++;
  408. hdlc->cbin++;
  409. if(++hdlc->hdlc_bits1 == 6)
  410. hdlc->state = HDLC_SENDFLAG_B7;
  411. break;
  412. case HDLC_SENDFLAG_B7:
  413. hdlc->cbin <<= 1;
  414. hdlc->data_bits++;
  415. if(slen == 0){
  416. hdlc->state = HDLC_SENDFLAG_B0;
  417. break;
  418. }
  419. if(hdlc->bit_shift==8){
  420. hdlc->state = HDLC_SEND_DATA;
  421. hdlc->crc = 0xffff;
  422. hdlc->hdlc_bits1 = 0;
  423. hdlc->data_received = 1;
  424. }
  425. break;
  426. case HDLC_SEND_FIRST_FLAG:
  427. hdlc->data_received = 1;
  428. if(hdlc->data_bits==8){
  429. hdlc->state = HDLC_SEND_DATA;
  430. hdlc->crc = 0xffff;
  431. hdlc->hdlc_bits1 = 0;
  432. break;
  433. }
  434. hdlc->cbin <<= 1;
  435. hdlc->data_bits++;
  436. if(hdlc->shift_reg & 0x01)
  437. hdlc->cbin++;
  438. hdlc->shift_reg >>= 1;
  439. hdlc->bit_shift--;
  440. if(hdlc->bit_shift==0){
  441. hdlc->state = HDLC_SEND_DATA;
  442. hdlc->crc = 0xffff;
  443. hdlc->hdlc_bits1 = 0;
  444. }
  445. break;
  446. case HDLC_SEND_DATA:
  447. hdlc->cbin <<= 1;
  448. hdlc->data_bits++;
  449. if(hdlc->hdlc_bits1 == 5){
  450. hdlc->hdlc_bits1 = 0;
  451. break;
  452. }
  453. if(hdlc->bit_shift==8){
  454. unsigned cval;
  455. cval = (hdlc->crc^hdlc->shift_reg) & 0xff;
  456. hdlc->crc = (hdlc->crc>>8)^crc16_tab[cval];
  457. }
  458. if(hdlc->shift_reg & 0x01){
  459. hdlc->hdlc_bits1++;
  460. hdlc->cbin++;
  461. hdlc->shift_reg >>= 1;
  462. hdlc->bit_shift--;
  463. } else {
  464. hdlc->hdlc_bits1 = 0;
  465. hdlc->shift_reg >>= 1;
  466. hdlc->bit_shift--;
  467. }
  468. break;
  469. case HDLC_SEND_CRC1:
  470. hdlc->cbin <<= 1;
  471. hdlc->data_bits++;
  472. if(hdlc->hdlc_bits1 == 5){
  473. hdlc->hdlc_bits1 = 0;
  474. break;
  475. }
  476. if(hdlc->shift_reg & 0x01){
  477. hdlc->hdlc_bits1++;
  478. hdlc->cbin++;
  479. hdlc->shift_reg >>= 1;
  480. hdlc->bit_shift--;
  481. } else {
  482. hdlc->hdlc_bits1 = 0;
  483. hdlc->shift_reg >>= 1;
  484. hdlc->bit_shift--;
  485. }
  486. if(hdlc->bit_shift==0){
  487. hdlc->shift_reg = (hdlc->crc >> 8);
  488. hdlc->state = HDLC_SEND_CRC2;
  489. hdlc->bit_shift = 8;
  490. }
  491. break;
  492. case HDLC_SEND_CRC2:
  493. hdlc->cbin <<= 1;
  494. hdlc->data_bits++;
  495. if(hdlc->hdlc_bits1 == 5){
  496. hdlc->hdlc_bits1 = 0;
  497. break;
  498. }
  499. if(hdlc->shift_reg & 0x01){
  500. hdlc->hdlc_bits1++;
  501. hdlc->cbin++;
  502. hdlc->shift_reg >>= 1;
  503. hdlc->bit_shift--;
  504. } else {
  505. hdlc->hdlc_bits1 = 0;
  506. hdlc->shift_reg >>= 1;
  507. hdlc->bit_shift--;
  508. }
  509. if(hdlc->bit_shift==0){
  510. hdlc->shift_reg = 0x7e;
  511. hdlc->state = HDLC_SEND_CLOSING_FLAG;
  512. hdlc->bit_shift = 8;
  513. }
  514. break;
  515. case HDLC_SEND_CLOSING_FLAG:
  516. hdlc->cbin <<= 1;
  517. hdlc->data_bits++;
  518. if(hdlc->hdlc_bits1 == 5){
  519. hdlc->hdlc_bits1 = 0;
  520. break;
  521. }
  522. if(hdlc->shift_reg & 0x01){
  523. hdlc->cbin++;
  524. }
  525. hdlc->shift_reg >>= 1;
  526. hdlc->bit_shift--;
  527. if(hdlc->bit_shift==0){
  528. hdlc->ffvalue = xfast_flag_value[hdlc->data_bits];
  529. if(hdlc->dchannel){
  530. hdlc->ffvalue = 0x7e;
  531. hdlc->state = HDLC_SEND_IDLE1;
  532. hdlc->bit_shift = 8-hdlc->data_bits;
  533. if(hdlc->bit_shift==0)
  534. hdlc->state = HDLC_SEND_FAST_IDLE;
  535. } else {
  536. if(!hdlc->do_adapt56){
  537. hdlc->state = HDLC_SEND_FAST_FLAG;
  538. hdlc->data_received = 0;
  539. } else {
  540. hdlc->state = HDLC_SENDFLAG_B0;
  541. hdlc->data_received = 0;
  542. }
  543. // Finished with this frame, send flags
  544. if (dsize > 1) dsize = 1; 
  545. }
  546. }
  547. break;
  548. case HDLC_SEND_IDLE1:
  549. hdlc->do_closing = 0;
  550. hdlc->cbin <<= 1;
  551. hdlc->cbin++;
  552. hdlc->data_bits++;
  553. hdlc->bit_shift--;
  554. if(hdlc->bit_shift==0){
  555. hdlc->state = HDLC_SEND_FAST_IDLE;
  556. hdlc->bit_shift = 0;
  557. }
  558. break;
  559. case HDLC_SEND_FAST_IDLE:
  560. hdlc->do_closing = 0;
  561. hdlc->cbin = 0xff;
  562. hdlc->data_bits = 8;
  563. if(hdlc->bit_shift == 8){
  564. hdlc->cbin = 0x7e;
  565. hdlc->state = HDLC_SEND_FIRST_FLAG;
  566. } else {
  567. *dst++ = hdlc->cbin;
  568. hdlc->bit_shift = hdlc->data_bits = 0;
  569. len++;
  570. dsize = 0;
  571. }
  572. break;
  573. default:
  574. break;
  575. }
  576. if(hdlc->do_adapt56){
  577. if(hdlc->data_bits==7){
  578. hdlc->cbin <<= 1;
  579. hdlc->cbin++;
  580. hdlc->data_bits++;
  581. }
  582. }
  583. if(hdlc->data_bits==8){
  584. *dst++ = hdlc->cbin;
  585. hdlc->data_bits = 0;
  586. len++;
  587. dsize--;
  588. }
  589. }
  590. *count -= slen;
  591. return len;
  592. }