hdlc_tests.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * hdlc_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2003 Steve Underwood
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2, as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*! \file */
  26. /*! \page hdlc_tests_page HDLC tests
  27. \section hdlc_tests_page_sec_1 What does it do?
  28. The HDLC tests exercise the HDLC module, and verifies correct operation
  29. using both 16 and 32 bit CRCs.
  30. */
  31. #if defined(HAVE_CONFIG_H)
  32. #include "config.h"
  33. #endif
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <unistd.h>
  37. #include <string.h>
  38. #include "spandsp.h"
  39. #include "spandsp/private/hdlc.h"
  40. int ref_len;
  41. uint8_t buf[1000];
  42. bool abort_reported;
  43. bool frame_handled;
  44. bool frame_failed;
  45. int frame_len_errors;
  46. int frame_data_errors;
  47. bool underflow_reported;
  48. bool framing_ok_reported;
  49. int framing_ok_reports;
  50. hdlc_rx_state_t rx;
  51. hdlc_tx_state_t tx;
  52. int frames_sent;
  53. int bytes_sent;
  54. uint64_t start;
  55. uint64_t end;
  56. /* Use a local random generator, so the results are consistent across platforms. We have hard coded
  57. correct results for a message sequence generated by this particular PRNG. */
  58. static int my_rand(void)
  59. {
  60. static int rndnum = 1234567;
  61. return (rndnum = 1664525U*rndnum + 1013904223U) >> 8;
  62. }
  63. /*- End of function --------------------------------------------------------*/
  64. static int cook_up_msg(uint8_t *buf)
  65. {
  66. int i;
  67. int len;
  68. /* Use medium length messages, with some randomised length variation. */
  69. /* TODO: this doesn't exercise extremely short or long messages. */
  70. len = (my_rand() & 0x3F) + 100;
  71. for (i = 0; i < len; i++)
  72. buf[i] = my_rand();
  73. return len;
  74. }
  75. /*- End of function --------------------------------------------------------*/
  76. static void frame_handler(void *user_data, const uint8_t *pkt, int len, int ok)
  77. {
  78. if (len < 0)
  79. {
  80. /* Special conditions */
  81. printf("HDLC rx status is %s (%d)\n", signal_status_to_str(len), len);
  82. switch (len)
  83. {
  84. case SIG_STATUS_FRAMING_OK:
  85. framing_ok_reported = true;
  86. framing_ok_reports++;
  87. break;
  88. case SIG_STATUS_ABORT:
  89. abort_reported = true;
  90. break;
  91. }
  92. return;
  93. }
  94. if (ok)
  95. {
  96. if (len != ref_len)
  97. {
  98. printf("Len error - %d %d\n", len, ref_len);
  99. frame_len_errors++;
  100. return;
  101. }
  102. if (memcmp(pkt, buf, len))
  103. {
  104. printf("Frame data error\n");
  105. frame_data_errors++;
  106. return;
  107. }
  108. frame_handled = true;
  109. }
  110. else
  111. {
  112. frame_failed = true;
  113. }
  114. }
  115. /*- End of function --------------------------------------------------------*/
  116. static void underflow_handler(void *user_data)
  117. {
  118. //printf("Underflow reported\n");
  119. underflow_reported = true;
  120. }
  121. /*- End of function --------------------------------------------------------*/
  122. static void check_result(void)
  123. {
  124. hdlc_rx_stats_t rx_stats;
  125. hdlc_rx_get_stats(&rx, &rx_stats);
  126. printf("%lu bytes\n", rx_stats.bytes);
  127. printf("%lu good frames\n", rx_stats.good_frames);
  128. printf("%lu CRC errors\n", rx_stats.crc_errors);
  129. printf("%lu length errors\n", rx_stats.length_errors);
  130. printf("%lu aborts\n", rx_stats.aborts);
  131. printf("%d frame length errors\n", frame_len_errors);
  132. printf("%d frame data errors\n", frame_data_errors);
  133. printf("Test duration %" PRIu64 " ticks\n", end - start);
  134. if (rx_stats.bytes != bytes_sent
  135. ||
  136. rx_stats.good_frames != frames_sent
  137. ||
  138. rx_stats.crc_errors != 0
  139. ||
  140. rx_stats.length_errors != 0
  141. ||
  142. rx_stats.aborts != 0
  143. ||
  144. frame_len_errors != 0
  145. ||
  146. frame_data_errors != 0)
  147. {
  148. printf("Tests failed.\n");
  149. exit(2);
  150. }
  151. printf("Test passed.\n\n");
  152. }
  153. /*- End of function --------------------------------------------------------*/
  154. static int test_hdlc_modes(void)
  155. {
  156. int i;
  157. int j;
  158. int len;
  159. int nextbyte;
  160. int progress;
  161. int progress_delay;
  162. uint8_t bufx[100];
  163. /* Try sending HDLC messages with CRC-16 */
  164. printf("Testing with CRC-16 (byte by byte)\n");
  165. frame_len_errors = 0;
  166. frame_data_errors = 0;
  167. hdlc_tx_init(&tx, false, 1, false, underflow_handler, NULL);
  168. hdlc_rx_init(&rx, false, false, 5, frame_handler, NULL);
  169. underflow_reported = false;
  170. start = rdtscll();
  171. hdlc_tx_flags(&tx, 40);
  172. /* Push an initial message so we should NOT get an underflow after the preamble. */
  173. ref_len = cook_up_msg(buf);
  174. hdlc_tx_frame(&tx, buf, ref_len);
  175. frame_handled = false;
  176. frame_failed = false;
  177. frames_sent = 0;
  178. bytes_sent = 0;
  179. for (i = 0; i < 1000000; i++)
  180. {
  181. nextbyte = hdlc_tx_get_byte(&tx);
  182. hdlc_rx_put_byte(&rx, nextbyte);
  183. if (underflow_reported)
  184. {
  185. underflow_reported = false;
  186. nextbyte = hdlc_tx_get_byte(&tx);
  187. hdlc_rx_put_byte(&rx, nextbyte);
  188. frames_sent++;
  189. bytes_sent += ref_len;
  190. if (!frame_handled)
  191. {
  192. printf("Frame not received.\n");
  193. return -1;
  194. }
  195. ref_len = cook_up_msg(buf);
  196. hdlc_tx_frame(&tx, buf, ref_len);
  197. frame_handled = false;
  198. }
  199. }
  200. end = rdtscll();
  201. check_result();
  202. /* Now try sending HDLC messages with CRC-16 */
  203. printf("Testing with CRC-16 (chunk by chunk)\n");
  204. frame_len_errors = 0;
  205. frame_data_errors = 0;
  206. hdlc_tx_init(&tx, false, 1, false, underflow_handler, NULL);
  207. hdlc_rx_init(&rx, false, false, 5, frame_handler, NULL);
  208. underflow_reported = false;
  209. start = rdtscll();
  210. hdlc_tx_flags(&tx, 40);
  211. /* Push an initial message so we should NOT get an underflow after the preamble. */
  212. ref_len = cook_up_msg(buf);
  213. hdlc_tx_frame(&tx, buf, ref_len);
  214. frame_handled = false;
  215. frame_failed = false;
  216. frames_sent = 0;
  217. bytes_sent = 0;
  218. for (i = 0; i < 10000; i++)
  219. {
  220. len = hdlc_tx_get(&tx, bufx, 100);
  221. hdlc_rx_put(&rx, bufx, len);
  222. if (underflow_reported)
  223. {
  224. underflow_reported = false;
  225. len = hdlc_tx_get(&tx, bufx, 100);
  226. hdlc_rx_put(&rx, bufx, len);
  227. frames_sent++;
  228. bytes_sent += ref_len;
  229. if (!frame_handled)
  230. {
  231. printf("Frame not received.\n");
  232. return -1;
  233. }
  234. ref_len = cook_up_msg(buf);
  235. hdlc_tx_frame(&tx, buf, ref_len);
  236. frame_handled = false;
  237. }
  238. }
  239. end = rdtscll();
  240. check_result();
  241. /* Now try sending HDLC messages with CRC-16 bit by bit */
  242. printf("Testing with CRC-16 (bit by bit)\n");
  243. frame_len_errors = 0;
  244. frame_data_errors = 0;
  245. hdlc_tx_init(&tx, false, 2, false, underflow_handler, NULL);
  246. hdlc_rx_init(&rx, false, false, 5, frame_handler, NULL);
  247. underflow_reported = false;
  248. start = rdtscll();
  249. hdlc_tx_flags(&tx, 40);
  250. /* Don't push an initial message so we should get an underflow after the preamble. */
  251. /* Lie for the first message, as there isn't really one */
  252. frame_handled = true;
  253. frame_failed = false;
  254. frames_sent = 0;
  255. bytes_sent = 0;
  256. ref_len = 0;
  257. for (i = 0; i < 8*1000000; i++)
  258. {
  259. nextbyte = hdlc_tx_get_bit(&tx);
  260. hdlc_rx_put_bit(&rx, nextbyte);
  261. if (underflow_reported)
  262. {
  263. underflow_reported = false;
  264. for (j = 0; j < 20; j++)
  265. {
  266. nextbyte = hdlc_tx_get_bit(&tx);
  267. hdlc_rx_put_bit(&rx, nextbyte);
  268. }
  269. if (ref_len)
  270. {
  271. frames_sent++;
  272. bytes_sent += ref_len;
  273. }
  274. if (!frame_handled)
  275. {
  276. printf("Frame not received.\n");
  277. return -1;
  278. }
  279. ref_len = cook_up_msg(buf);
  280. hdlc_tx_frame(&tx, buf, ref_len);
  281. frame_handled = false;
  282. }
  283. }
  284. end = rdtscll();
  285. check_result();
  286. /* Now try sending HDLC messages with CRC-32 */
  287. printf("Testing with CRC-32 (byte by byte)\n");
  288. frame_len_errors = 0;
  289. frame_data_errors = 0;
  290. hdlc_tx_init(&tx, true, 1, false, underflow_handler, NULL);
  291. hdlc_rx_init(&rx, true, false, 1, frame_handler, NULL);
  292. underflow_reported = false;
  293. start = rdtscll();
  294. hdlc_tx_flags(&tx, 40);
  295. /* Don't push an initial message so we should get an underflow after the preamble. */
  296. /* Lie for the first message, as there isn't really one */
  297. frame_handled = true;
  298. frame_failed = false;
  299. frames_sent = 0;
  300. bytes_sent = 0;
  301. ref_len = 0;
  302. for (i = 0; i < 1000000; i++)
  303. {
  304. nextbyte = hdlc_tx_get_byte(&tx);
  305. hdlc_rx_put_byte(&rx, nextbyte);
  306. if (underflow_reported)
  307. {
  308. underflow_reported = false;
  309. nextbyte = hdlc_tx_get_byte(&tx);
  310. hdlc_rx_put_byte(&rx, nextbyte);
  311. if (ref_len)
  312. {
  313. frames_sent++;
  314. bytes_sent += ref_len;
  315. }
  316. if (!frame_handled)
  317. {
  318. printf("Frame not received.\n");
  319. return -1;
  320. }
  321. ref_len = cook_up_msg(buf);
  322. hdlc_tx_frame(&tx, buf, ref_len);
  323. frame_handled = false;
  324. }
  325. }
  326. end = rdtscll();
  327. check_result();
  328. /* Now try progressive mode with CRC-16 */
  329. printf("Testing progressive mode with CRC-16 (byte by byte)\n");
  330. frame_len_errors = 0;
  331. frame_data_errors = 0;
  332. hdlc_tx_init(&tx, true, 1, true, underflow_handler, NULL);
  333. hdlc_rx_init(&rx, true, false, 1, frame_handler, NULL);
  334. underflow_reported = false;
  335. start = rdtscll();
  336. hdlc_tx_flags(&tx, 40);
  337. /* Don't push an initial message so we should get an underflow after the preamble. */
  338. /* Lie for the first message, as there isn't really one */
  339. frame_handled = true;
  340. frame_failed = false;
  341. progress = 9999;
  342. progress_delay = 9999;
  343. frames_sent = 0;
  344. bytes_sent = 0;
  345. ref_len = 0;
  346. for (i = 0; i < 1000000; i++)
  347. {
  348. nextbyte = hdlc_tx_get_byte(&tx);
  349. hdlc_rx_put_byte(&rx, nextbyte);
  350. if (underflow_reported)
  351. {
  352. underflow_reported = false;
  353. nextbyte = hdlc_tx_get_byte(&tx);
  354. hdlc_rx_put_byte(&rx, nextbyte);
  355. if (ref_len)
  356. {
  357. frames_sent++;
  358. bytes_sent += ref_len;
  359. }
  360. if (!frame_handled)
  361. {
  362. printf("Frame not received.\n");
  363. return -1;
  364. }
  365. ref_len = cook_up_msg(buf);
  366. hdlc_tx_frame(&tx, buf, 10);
  367. progress = 10;
  368. progress_delay = 8;
  369. frame_handled = false;
  370. }
  371. if (progress < ref_len && progress_delay-- <= 0)
  372. {
  373. if (hdlc_tx_frame(&tx, buf + progress, (progress + 10 <= ref_len) ? 10 : ref_len - progress) < 0)
  374. {
  375. printf("Failed to add progressively\n");
  376. return -1;
  377. }
  378. progress += 10;
  379. progress_delay = 8;
  380. }
  381. }
  382. end = rdtscll();
  383. check_result();
  384. printf("Tests passed.\n");
  385. return 0;
  386. }
  387. /*- End of function --------------------------------------------------------*/
  388. static int test_hdlc_frame_length_error_handling(void)
  389. {
  390. int i;
  391. int j;
  392. int nextbyte;
  393. printf("Testing frame length error handling using CRC-16 (bit by bit)\n");
  394. frame_len_errors = 0;
  395. frame_data_errors = 0;
  396. hdlc_tx_init(&tx, false, 2, false, underflow_handler, NULL);
  397. hdlc_rx_init(&rx, false, true, 5, frame_handler, NULL);
  398. hdlc_rx_set_max_frame_len(&rx, 100);
  399. underflow_reported = false;
  400. framing_ok_reported = false;
  401. framing_ok_reports = 0;
  402. hdlc_tx_flags(&tx, 10);
  403. /* Don't push an initial message so we should get an underflow after the preamble. */
  404. /* Lie for the first message, as there isn't really one */
  405. frame_handled = true;
  406. frame_failed = false;
  407. frames_sent = 0;
  408. bytes_sent = 0;
  409. ref_len = 0;
  410. for (i = 0; i < 8*1000000; i++)
  411. {
  412. nextbyte = hdlc_tx_get_bit(&tx);
  413. hdlc_rx_put_bit(&rx, nextbyte);
  414. if (framing_ok_reported)
  415. {
  416. printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports);
  417. framing_ok_reported = false;
  418. }
  419. if (underflow_reported)
  420. {
  421. underflow_reported = false;
  422. for (j = 0; j < 20; j++)
  423. {
  424. nextbyte = hdlc_tx_get_bit(&tx);
  425. hdlc_rx_put_bit(&rx, nextbyte);
  426. if (framing_ok_reported)
  427. {
  428. printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled);
  429. framing_ok_reported = false;
  430. }
  431. }
  432. if (ref_len)
  433. {
  434. frames_sent++;
  435. bytes_sent += ref_len;
  436. }
  437. if (!frame_handled)
  438. {
  439. /* We should get a failure when the length reaches 101 */
  440. if (ref_len == 101)
  441. {
  442. printf("Tests passed.\n");
  443. return 0;
  444. }
  445. if (frame_failed)
  446. printf("Frame failed.\n");
  447. printf("Frame not received at length %d.\n", ref_len);
  448. return -1;
  449. }
  450. else
  451. {
  452. /* We should get a failure when the length reaches 101 */
  453. if (ref_len > 100)
  454. {
  455. printf("Tests failed.\n");
  456. return -1;
  457. }
  458. }
  459. ref_len++;
  460. hdlc_tx_frame(&tx, buf, ref_len);
  461. frame_handled = false;
  462. }
  463. }
  464. /* We shouldn't reach here */
  465. printf("Tests failed.\n");
  466. return -1;
  467. }
  468. /*- End of function --------------------------------------------------------*/
  469. static int test_hdlc_crc_error_handling(void)
  470. {
  471. int i;
  472. int j;
  473. int nextbyte;
  474. int corrupt;
  475. printf("Testing CRC error handling using CRC-16 (bit by bit)\n");
  476. frame_len_errors = 0;
  477. frame_data_errors = 0;
  478. hdlc_tx_init(&tx, false, 2, false, underflow_handler, NULL);
  479. hdlc_rx_init(&rx, false, true, 5, frame_handler, NULL);
  480. underflow_reported = false;
  481. framing_ok_reported = false;
  482. framing_ok_reports = 0;
  483. hdlc_tx_flags(&tx, 10);
  484. /* Don't push an initial message so we should get an underflow after the preamble. */
  485. /* Lie for the first message, as there isn't really one */
  486. frame_handled = true;
  487. frame_failed = false;
  488. frames_sent = 0;
  489. bytes_sent = 0;
  490. ref_len = 100;
  491. corrupt = false;
  492. for (i = 0; i < 8*1000000; i++)
  493. {
  494. nextbyte = hdlc_tx_get_bit(&tx);
  495. hdlc_rx_put_bit(&rx, nextbyte);
  496. if (framing_ok_reported)
  497. {
  498. printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports);
  499. framing_ok_reported = false;
  500. }
  501. if (underflow_reported)
  502. {
  503. underflow_reported = false;
  504. for (j = 0; j < 20; j++)
  505. {
  506. nextbyte = hdlc_tx_get_bit(&tx);
  507. hdlc_rx_put_bit(&rx, nextbyte);
  508. if (framing_ok_reported)
  509. {
  510. printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled);
  511. framing_ok_reported = false;
  512. }
  513. }
  514. if (ref_len)
  515. {
  516. frames_sent++;
  517. bytes_sent += ref_len;
  518. }
  519. if (!frame_handled)
  520. {
  521. if (!corrupt)
  522. {
  523. printf("Frame not received when it should be correct.\n");
  524. return -1;
  525. }
  526. }
  527. else
  528. {
  529. if (corrupt)
  530. {
  531. printf("Frame received when it should be corrupt.\n");
  532. return -1;
  533. }
  534. }
  535. ref_len = cook_up_msg(buf);
  536. hdlc_tx_frame(&tx, buf, ref_len);
  537. if ((corrupt = rand() & 1))
  538. hdlc_tx_corrupt_frame(&tx);
  539. frame_handled = false;
  540. }
  541. }
  542. printf("Tests passed.\n");
  543. return 0;
  544. }
  545. /*- End of function --------------------------------------------------------*/
  546. static int test_hdlc_abort_handling(void)
  547. {
  548. int i;
  549. int j;
  550. int nextbyte;
  551. int abort;
  552. printf("Testing abort handling using CRC-16 (bit by bit)\n");
  553. frame_len_errors = 0;
  554. frame_data_errors = 0;
  555. hdlc_tx_init(&tx, false, 2, false, underflow_handler, NULL);
  556. hdlc_rx_init(&rx, false, true, 0, frame_handler, NULL);
  557. underflow_reported = false;
  558. framing_ok_reported = false;
  559. framing_ok_reports = 0;
  560. hdlc_tx_flags(&tx, 10);
  561. /* Don't push an initial message so we should get an underflow after the preamble. */
  562. /* Lie for the first message, as there isn't really one */
  563. frame_handled = true;
  564. frame_failed = false;
  565. frames_sent = 0;
  566. bytes_sent = 0;
  567. ref_len = 0;
  568. abort = false;
  569. abort_reported = false;
  570. for (i = 0; i < 8*1000000; i++)
  571. {
  572. nextbyte = hdlc_tx_get_bit(&tx);
  573. hdlc_rx_put_bit(&rx, nextbyte);
  574. if (framing_ok_reported)
  575. {
  576. printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports);
  577. framing_ok_reported = false;
  578. }
  579. if (underflow_reported)
  580. {
  581. underflow_reported = false;
  582. for (j = 0; j < 20; j++)
  583. {
  584. nextbyte = hdlc_tx_get_bit(&tx);
  585. hdlc_rx_put_bit(&rx, nextbyte);
  586. if (framing_ok_reported)
  587. {
  588. printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled);
  589. framing_ok_reported = false;
  590. }
  591. }
  592. if (ref_len)
  593. {
  594. frames_sent++;
  595. bytes_sent += ref_len;
  596. }
  597. if (abort)
  598. {
  599. if (abort && !abort_reported)
  600. {
  601. printf("Abort not received when sent\n");
  602. return -1;
  603. }
  604. if (frame_handled)
  605. {
  606. if (frame_failed)
  607. printf("Frame failed.\n");
  608. printf("Frame received when abort sent.\n");
  609. return -1;
  610. }
  611. }
  612. else
  613. {
  614. if (abort_reported)
  615. {
  616. printf("Abort received when not sent\n");
  617. return -1;
  618. }
  619. if (!frame_handled)
  620. {
  621. if (frame_failed)
  622. printf("Frame failed.\n");
  623. printf("Frame not received.\n");
  624. return -1;
  625. }
  626. }
  627. ref_len = cook_up_msg(buf);
  628. hdlc_tx_frame(&tx, buf, ref_len);
  629. if ((abort = rand() & 1))
  630. hdlc_tx_abort(&tx);
  631. frame_handled = false;
  632. abort_reported = false;
  633. }
  634. }
  635. printf("Tests passed.\n");
  636. return 0;
  637. }
  638. /*- End of function --------------------------------------------------------*/
  639. #if 0
  640. static int test_hdlc_octet_count_handling(void)
  641. {
  642. int i;
  643. int j;
  644. int nextbyte;
  645. printf("Testing the octet_counting handling using CRC-16 (bit by bit)\n");
  646. frame_len_errors = 0;
  647. frame_data_errors = 0;
  648. hdlc_tx_init(&tx, false, 2, false, underflow_handler, NULL);
  649. hdlc_rx_init(&rx, false, true, 0, frame_handler, NULL);
  650. //hdlc_rx_set_max_frame_len(&rx, 50);
  651. hdlc_rx_set_octet_counting_report_interval(&rx, 16);
  652. underflow_reported = false;
  653. framing_ok_reported = false;
  654. framing_ok_reports = 0;
  655. hdlc_tx_flags(&tx, 10);
  656. /* Don't push an initial message so we should get an underflow after the preamble. */
  657. /* Lie for the first message, as there isn't really one */
  658. frame_handled = true;
  659. frame_failed = false;
  660. frames_sent = 0;
  661. bytes_sent = 0;
  662. ref_len = 0;
  663. for (i = 0; i < 8*1000000; i++)
  664. {
  665. nextbyte = hdlc_tx_get_bit(&tx);
  666. hdlc_rx_put_bit(&rx, nextbyte);
  667. if (framing_ok_reported)
  668. {
  669. printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports);
  670. framing_ok_reported = false;
  671. }
  672. if (underflow_reported)
  673. {
  674. underflow_reported = false;
  675. for (j = 0; j < 20; j++)
  676. {
  677. nextbyte = hdlc_tx_get_bit(&tx);
  678. hdlc_rx_put_bit(&rx, nextbyte);
  679. if (framing_ok_reported)
  680. {
  681. printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled);
  682. framing_ok_reported = false;
  683. }
  684. }
  685. if (ref_len)
  686. {
  687. frames_sent++;
  688. bytes_sent += ref_len;
  689. }
  690. if (!frame_handled)
  691. {
  692. if (frame_failed)
  693. printf("Frame failed.\n");
  694. printf("Frame not received.\n");
  695. return -1;
  696. }
  697. ref_len = cook_up_msg(buf);
  698. hdlc_tx_frame(&tx, buf, ref_len);
  699. hdlc_tx_abort(&tx);
  700. //hdlc_tx_corrupt_frame(&tx);
  701. frame_handled = false;
  702. }
  703. }
  704. printf("Tests passed.\n");
  705. return 0;
  706. }
  707. /*- End of function --------------------------------------------------------*/
  708. #endif
  709. static void hdlc_tests(void)
  710. {
  711. printf("HDLC module tests\n");
  712. if (test_hdlc_modes())
  713. {
  714. printf("Tests failed\n");
  715. exit(2);
  716. }
  717. if (test_hdlc_frame_length_error_handling())
  718. {
  719. printf("Tests failed\n");
  720. exit(2);
  721. }
  722. if (test_hdlc_crc_error_handling())
  723. {
  724. printf("Tests failed\n");
  725. exit(2);
  726. }
  727. if (test_hdlc_abort_handling())
  728. {
  729. printf("Tests failed\n");
  730. exit(2);
  731. }
  732. #if 0
  733. if (test_hdlc_octet_count_handling())
  734. {
  735. printf("Tests failed\n");
  736. exit(2);
  737. }
  738. #endif
  739. printf("Tests passed.\n");
  740. }
  741. /*- End of function --------------------------------------------------------*/
  742. static void decode_handler(void *user_data, const uint8_t *pkt, int len, int ok)
  743. {
  744. int i;
  745. if (len < 0)
  746. {
  747. /* Special conditions */
  748. printf("HDLC rx status is %s (%d)\n", signal_status_to_str(len), len);
  749. return;
  750. }
  751. if (ok)
  752. {
  753. printf("Good frame, len = %d\n", len);
  754. printf("HDLC: ");
  755. for (i = 0; i < len; i++)
  756. printf("%02X ", pkt[i]);
  757. printf("\n");
  758. }
  759. else
  760. {
  761. printf("Bad frame, len = %d\n", len);
  762. }
  763. }
  764. /*- End of function --------------------------------------------------------*/
  765. static void decode_bitstream(const char *in_file_name)
  766. {
  767. char buf[1024];
  768. int bit;
  769. int num;
  770. hdlc_rx_state_t rx;
  771. FILE *in;
  772. if ((in = fopen(in_file_name, "r")) == NULL)
  773. {
  774. fprintf(stderr, "Failed to open '%s'\n", in_file_name);
  775. exit(2);
  776. }
  777. hdlc_rx_init(&rx, false, true, 2, decode_handler, NULL);
  778. while (fgets(buf, 1024, in))
  779. {
  780. if (sscanf(buf, "Rx bit %d - %d", &num, &bit) == 2)
  781. {
  782. //printf("Rx bit %d - %d\n", num, bit);
  783. //printf("Rx bit %d\n", bit);
  784. hdlc_rx_put_bit(&rx, bit);
  785. }
  786. }
  787. fclose(in);
  788. }
  789. /*- End of function --------------------------------------------------------*/
  790. int main(int argc, char *argv[])
  791. {
  792. int opt;
  793. const char *in_file_name;
  794. in_file_name = NULL;
  795. while ((opt = getopt(argc, argv, "d:")) != -1)
  796. {
  797. switch (opt)
  798. {
  799. case 'd':
  800. in_file_name = optarg;
  801. break;
  802. default:
  803. //usage();
  804. exit(2);
  805. break;
  806. }
  807. }
  808. if (in_file_name)
  809. decode_bitstream(in_file_name);
  810. else
  811. hdlc_tests();
  812. return 0;
  813. }
  814. /*- End of function --------------------------------------------------------*/
  815. /*- End of file ------------------------------------------------------------*/