123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868 |
- /*
- * SpanDSP - a series of DSP components for telephony
- *
- * hdlc_tests.c
- *
- * Written by Steve Underwood <steveu@coppice.org>
- *
- * Copyright (C) 2003 Steve Underwood
- *
- * All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2, as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
- /*! \file */
- /*! \page hdlc_tests_page HDLC tests
- \section hdlc_tests_page_sec_1 What does it do?
- The HDLC tests exercise the HDLC module, and verifies correct operation
- using both 16 and 32 bit CRCs.
- */
- #if defined(HAVE_CONFIG_H)
- #include "config.h"
- #endif
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include "spandsp.h"
- #include "spandsp/private/hdlc.h"
- int ref_len;
- uint8_t buf[1000];
- bool abort_reported;
- bool frame_handled;
- bool frame_failed;
- int frame_len_errors;
- int frame_data_errors;
- bool underflow_reported;
- bool framing_ok_reported;
- int framing_ok_reports;
- hdlc_rx_state_t rx;
- hdlc_tx_state_t tx;
- int frames_sent;
- int bytes_sent;
- uint64_t start;
- uint64_t end;
- /* Use a local random generator, so the results are consistent across platforms. We have hard coded
- correct results for a message sequence generated by this particular PRNG. */
- static int my_rand(void)
- {
- static int rndnum = 1234567;
- return (rndnum = 1664525U*rndnum + 1013904223U) >> 8;
- }
- /*- End of function --------------------------------------------------------*/
- static int cook_up_msg(uint8_t *buf)
- {
- int i;
- int len;
- /* Use medium length messages, with some randomised length variation. */
- /* TODO: this doesn't exercise extremely short or long messages. */
- len = (my_rand() & 0x3F) + 100;
- for (i = 0; i < len; i++)
- buf[i] = my_rand();
- return len;
- }
- /*- End of function --------------------------------------------------------*/
- static void frame_handler(void *user_data, const uint8_t *pkt, int len, int ok)
- {
- if (len < 0)
- {
- /* Special conditions */
- printf("HDLC rx status is %s (%d)\n", signal_status_to_str(len), len);
- switch (len)
- {
- case SIG_STATUS_FRAMING_OK:
- framing_ok_reported = true;
- framing_ok_reports++;
- break;
- case SIG_STATUS_ABORT:
- abort_reported = true;
- break;
- }
- return;
- }
- if (ok)
- {
- if (len != ref_len)
- {
- printf("Len error - %d %d\n", len, ref_len);
- frame_len_errors++;
- return;
- }
- if (memcmp(pkt, buf, len))
- {
- printf("Frame data error\n");
- frame_data_errors++;
- return;
- }
- frame_handled = true;
- }
- else
- {
- frame_failed = true;
- }
- }
- /*- End of function --------------------------------------------------------*/
- static void underflow_handler(void *user_data)
- {
- //printf("Underflow reported\n");
- underflow_reported = true;
- }
- /*- End of function --------------------------------------------------------*/
- static void check_result(void)
- {
- hdlc_rx_stats_t rx_stats;
- hdlc_rx_get_stats(&rx, &rx_stats);
- printf("%lu bytes\n", rx_stats.bytes);
- printf("%lu good frames\n", rx_stats.good_frames);
- printf("%lu CRC errors\n", rx_stats.crc_errors);
- printf("%lu length errors\n", rx_stats.length_errors);
- printf("%lu aborts\n", rx_stats.aborts);
- printf("%d frame length errors\n", frame_len_errors);
- printf("%d frame data errors\n", frame_data_errors);
- printf("Test duration %" PRIu64 " ticks\n", end - start);
- if (rx_stats.bytes != bytes_sent
- ||
- rx_stats.good_frames != frames_sent
- ||
- rx_stats.crc_errors != 0
- ||
- rx_stats.length_errors != 0
- ||
- rx_stats.aborts != 0
- ||
- frame_len_errors != 0
- ||
- frame_data_errors != 0)
- {
- printf("Tests failed.\n");
- exit(2);
- }
- printf("Test passed.\n\n");
- }
- /*- End of function --------------------------------------------------------*/
- static int test_hdlc_modes(void)
- {
- int i;
- int j;
- int len;
- int nextbyte;
- int progress;
- int progress_delay;
- uint8_t bufx[100];
- /* Try sending HDLC messages with CRC-16 */
- printf("Testing with CRC-16 (byte by byte)\n");
- frame_len_errors = 0;
- frame_data_errors = 0;
- hdlc_tx_init(&tx, false, 1, false, underflow_handler, NULL);
- hdlc_rx_init(&rx, false, false, 5, frame_handler, NULL);
- underflow_reported = false;
- start = rdtscll();
- hdlc_tx_flags(&tx, 40);
- /* Push an initial message so we should NOT get an underflow after the preamble. */
- ref_len = cook_up_msg(buf);
- hdlc_tx_frame(&tx, buf, ref_len);
- frame_handled = false;
- frame_failed = false;
- frames_sent = 0;
- bytes_sent = 0;
- for (i = 0; i < 1000000; i++)
- {
- nextbyte = hdlc_tx_get_byte(&tx);
- hdlc_rx_put_byte(&rx, nextbyte);
- if (underflow_reported)
- {
- underflow_reported = false;
- nextbyte = hdlc_tx_get_byte(&tx);
- hdlc_rx_put_byte(&rx, nextbyte);
- frames_sent++;
- bytes_sent += ref_len;
- if (!frame_handled)
- {
- printf("Frame not received.\n");
- return -1;
- }
- ref_len = cook_up_msg(buf);
- hdlc_tx_frame(&tx, buf, ref_len);
- frame_handled = false;
- }
- }
- end = rdtscll();
- check_result();
- /* Now try sending HDLC messages with CRC-16 */
- printf("Testing with CRC-16 (chunk by chunk)\n");
- frame_len_errors = 0;
- frame_data_errors = 0;
- hdlc_tx_init(&tx, false, 1, false, underflow_handler, NULL);
- hdlc_rx_init(&rx, false, false, 5, frame_handler, NULL);
- underflow_reported = false;
- start = rdtscll();
- hdlc_tx_flags(&tx, 40);
- /* Push an initial message so we should NOT get an underflow after the preamble. */
- ref_len = cook_up_msg(buf);
- hdlc_tx_frame(&tx, buf, ref_len);
- frame_handled = false;
- frame_failed = false;
- frames_sent = 0;
- bytes_sent = 0;
- for (i = 0; i < 10000; i++)
- {
- len = hdlc_tx_get(&tx, bufx, 100);
- hdlc_rx_put(&rx, bufx, len);
- if (underflow_reported)
- {
- underflow_reported = false;
- len = hdlc_tx_get(&tx, bufx, 100);
- hdlc_rx_put(&rx, bufx, len);
- frames_sent++;
- bytes_sent += ref_len;
- if (!frame_handled)
- {
- printf("Frame not received.\n");
- return -1;
- }
- ref_len = cook_up_msg(buf);
- hdlc_tx_frame(&tx, buf, ref_len);
- frame_handled = false;
- }
- }
- end = rdtscll();
- check_result();
- /* Now try sending HDLC messages with CRC-16 bit by bit */
- printf("Testing with CRC-16 (bit by bit)\n");
- frame_len_errors = 0;
- frame_data_errors = 0;
- hdlc_tx_init(&tx, false, 2, false, underflow_handler, NULL);
- hdlc_rx_init(&rx, false, false, 5, frame_handler, NULL);
- underflow_reported = false;
- start = rdtscll();
- hdlc_tx_flags(&tx, 40);
- /* Don't push an initial message so we should get an underflow after the preamble. */
- /* Lie for the first message, as there isn't really one */
- frame_handled = true;
- frame_failed = false;
- frames_sent = 0;
- bytes_sent = 0;
- ref_len = 0;
- for (i = 0; i < 8*1000000; i++)
- {
- nextbyte = hdlc_tx_get_bit(&tx);
- hdlc_rx_put_bit(&rx, nextbyte);
- if (underflow_reported)
- {
- underflow_reported = false;
- for (j = 0; j < 20; j++)
- {
- nextbyte = hdlc_tx_get_bit(&tx);
- hdlc_rx_put_bit(&rx, nextbyte);
- }
- if (ref_len)
- {
- frames_sent++;
- bytes_sent += ref_len;
- }
- if (!frame_handled)
- {
- printf("Frame not received.\n");
- return -1;
- }
- ref_len = cook_up_msg(buf);
- hdlc_tx_frame(&tx, buf, ref_len);
- frame_handled = false;
- }
- }
- end = rdtscll();
- check_result();
- /* Now try sending HDLC messages with CRC-32 */
- printf("Testing with CRC-32 (byte by byte)\n");
- frame_len_errors = 0;
- frame_data_errors = 0;
- hdlc_tx_init(&tx, true, 1, false, underflow_handler, NULL);
- hdlc_rx_init(&rx, true, false, 1, frame_handler, NULL);
- underflow_reported = false;
- start = rdtscll();
- hdlc_tx_flags(&tx, 40);
- /* Don't push an initial message so we should get an underflow after the preamble. */
- /* Lie for the first message, as there isn't really one */
- frame_handled = true;
- frame_failed = false;
- frames_sent = 0;
- bytes_sent = 0;
- ref_len = 0;
- for (i = 0; i < 1000000; i++)
- {
- nextbyte = hdlc_tx_get_byte(&tx);
- hdlc_rx_put_byte(&rx, nextbyte);
- if (underflow_reported)
- {
- underflow_reported = false;
- nextbyte = hdlc_tx_get_byte(&tx);
- hdlc_rx_put_byte(&rx, nextbyte);
- if (ref_len)
- {
- frames_sent++;
- bytes_sent += ref_len;
- }
- if (!frame_handled)
- {
- printf("Frame not received.\n");
- return -1;
- }
- ref_len = cook_up_msg(buf);
- hdlc_tx_frame(&tx, buf, ref_len);
- frame_handled = false;
- }
- }
- end = rdtscll();
- check_result();
- /* Now try progressive mode with CRC-16 */
- printf("Testing progressive mode with CRC-16 (byte by byte)\n");
- frame_len_errors = 0;
- frame_data_errors = 0;
- hdlc_tx_init(&tx, true, 1, true, underflow_handler, NULL);
- hdlc_rx_init(&rx, true, false, 1, frame_handler, NULL);
- underflow_reported = false;
- start = rdtscll();
- hdlc_tx_flags(&tx, 40);
- /* Don't push an initial message so we should get an underflow after the preamble. */
- /* Lie for the first message, as there isn't really one */
- frame_handled = true;
- frame_failed = false;
- progress = 9999;
- progress_delay = 9999;
- frames_sent = 0;
- bytes_sent = 0;
- ref_len = 0;
- for (i = 0; i < 1000000; i++)
- {
- nextbyte = hdlc_tx_get_byte(&tx);
- hdlc_rx_put_byte(&rx, nextbyte);
- if (underflow_reported)
- {
- underflow_reported = false;
- nextbyte = hdlc_tx_get_byte(&tx);
- hdlc_rx_put_byte(&rx, nextbyte);
- if (ref_len)
- {
- frames_sent++;
- bytes_sent += ref_len;
- }
- if (!frame_handled)
- {
- printf("Frame not received.\n");
- return -1;
- }
- ref_len = cook_up_msg(buf);
- hdlc_tx_frame(&tx, buf, 10);
- progress = 10;
- progress_delay = 8;
- frame_handled = false;
- }
- if (progress < ref_len && progress_delay-- <= 0)
- {
- if (hdlc_tx_frame(&tx, buf + progress, (progress + 10 <= ref_len) ? 10 : ref_len - progress) < 0)
- {
- printf("Failed to add progressively\n");
- return -1;
- }
- progress += 10;
- progress_delay = 8;
- }
- }
- end = rdtscll();
- check_result();
- printf("Tests passed.\n");
- return 0;
- }
- /*- End of function --------------------------------------------------------*/
- static int test_hdlc_frame_length_error_handling(void)
- {
- int i;
- int j;
- int nextbyte;
- printf("Testing frame length error handling using CRC-16 (bit by bit)\n");
- frame_len_errors = 0;
- frame_data_errors = 0;
- hdlc_tx_init(&tx, false, 2, false, underflow_handler, NULL);
- hdlc_rx_init(&rx, false, true, 5, frame_handler, NULL);
- hdlc_rx_set_max_frame_len(&rx, 100);
- underflow_reported = false;
- framing_ok_reported = false;
- framing_ok_reports = 0;
- hdlc_tx_flags(&tx, 10);
- /* Don't push an initial message so we should get an underflow after the preamble. */
- /* Lie for the first message, as there isn't really one */
- frame_handled = true;
- frame_failed = false;
- frames_sent = 0;
- bytes_sent = 0;
- ref_len = 0;
- for (i = 0; i < 8*1000000; i++)
- {
- nextbyte = hdlc_tx_get_bit(&tx);
- hdlc_rx_put_bit(&rx, nextbyte);
- if (framing_ok_reported)
- {
- printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports);
- framing_ok_reported = false;
- }
- if (underflow_reported)
- {
- underflow_reported = false;
- for (j = 0; j < 20; j++)
- {
- nextbyte = hdlc_tx_get_bit(&tx);
- hdlc_rx_put_bit(&rx, nextbyte);
- if (framing_ok_reported)
- {
- printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled);
- framing_ok_reported = false;
- }
- }
- if (ref_len)
- {
- frames_sent++;
- bytes_sent += ref_len;
- }
- if (!frame_handled)
- {
- /* We should get a failure when the length reaches 101 */
- if (ref_len == 101)
- {
- printf("Tests passed.\n");
- return 0;
- }
- if (frame_failed)
- printf("Frame failed.\n");
- printf("Frame not received at length %d.\n", ref_len);
- return -1;
- }
- else
- {
- /* We should get a failure when the length reaches 101 */
- if (ref_len > 100)
- {
- printf("Tests failed.\n");
- return -1;
- }
- }
- ref_len++;
- hdlc_tx_frame(&tx, buf, ref_len);
- frame_handled = false;
- }
- }
- /* We shouldn't reach here */
- printf("Tests failed.\n");
- return -1;
- }
- /*- End of function --------------------------------------------------------*/
- static int test_hdlc_crc_error_handling(void)
- {
- int i;
- int j;
- int nextbyte;
- int corrupt;
- printf("Testing CRC error handling using CRC-16 (bit by bit)\n");
- frame_len_errors = 0;
- frame_data_errors = 0;
- hdlc_tx_init(&tx, false, 2, false, underflow_handler, NULL);
- hdlc_rx_init(&rx, false, true, 5, frame_handler, NULL);
- underflow_reported = false;
- framing_ok_reported = false;
- framing_ok_reports = 0;
- hdlc_tx_flags(&tx, 10);
- /* Don't push an initial message so we should get an underflow after the preamble. */
- /* Lie for the first message, as there isn't really one */
- frame_handled = true;
- frame_failed = false;
- frames_sent = 0;
- bytes_sent = 0;
- ref_len = 100;
- corrupt = false;
- for (i = 0; i < 8*1000000; i++)
- {
- nextbyte = hdlc_tx_get_bit(&tx);
- hdlc_rx_put_bit(&rx, nextbyte);
- if (framing_ok_reported)
- {
- printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports);
- framing_ok_reported = false;
- }
- if (underflow_reported)
- {
- underflow_reported = false;
- for (j = 0; j < 20; j++)
- {
- nextbyte = hdlc_tx_get_bit(&tx);
- hdlc_rx_put_bit(&rx, nextbyte);
- if (framing_ok_reported)
- {
- printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled);
- framing_ok_reported = false;
- }
- }
- if (ref_len)
- {
- frames_sent++;
- bytes_sent += ref_len;
- }
- if (!frame_handled)
- {
- if (!corrupt)
- {
- printf("Frame not received when it should be correct.\n");
- return -1;
- }
- }
- else
- {
- if (corrupt)
- {
- printf("Frame received when it should be corrupt.\n");
- return -1;
- }
- }
- ref_len = cook_up_msg(buf);
- hdlc_tx_frame(&tx, buf, ref_len);
- if ((corrupt = rand() & 1))
- hdlc_tx_corrupt_frame(&tx);
- frame_handled = false;
- }
- }
- printf("Tests passed.\n");
- return 0;
- }
- /*- End of function --------------------------------------------------------*/
- static int test_hdlc_abort_handling(void)
- {
- int i;
- int j;
- int nextbyte;
- int abort;
- printf("Testing abort handling using CRC-16 (bit by bit)\n");
- frame_len_errors = 0;
- frame_data_errors = 0;
- hdlc_tx_init(&tx, false, 2, false, underflow_handler, NULL);
- hdlc_rx_init(&rx, false, true, 0, frame_handler, NULL);
- underflow_reported = false;
- framing_ok_reported = false;
- framing_ok_reports = 0;
- hdlc_tx_flags(&tx, 10);
- /* Don't push an initial message so we should get an underflow after the preamble. */
- /* Lie for the first message, as there isn't really one */
- frame_handled = true;
- frame_failed = false;
- frames_sent = 0;
- bytes_sent = 0;
- ref_len = 0;
- abort = false;
- abort_reported = false;
- for (i = 0; i < 8*1000000; i++)
- {
- nextbyte = hdlc_tx_get_bit(&tx);
- hdlc_rx_put_bit(&rx, nextbyte);
- if (framing_ok_reported)
- {
- printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports);
- framing_ok_reported = false;
- }
- if (underflow_reported)
- {
- underflow_reported = false;
- for (j = 0; j < 20; j++)
- {
- nextbyte = hdlc_tx_get_bit(&tx);
- hdlc_rx_put_bit(&rx, nextbyte);
- if (framing_ok_reported)
- {
- printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled);
- framing_ok_reported = false;
- }
- }
- if (ref_len)
- {
- frames_sent++;
- bytes_sent += ref_len;
- }
- if (abort)
- {
- if (abort && !abort_reported)
- {
- printf("Abort not received when sent\n");
- return -1;
- }
- if (frame_handled)
- {
- if (frame_failed)
- printf("Frame failed.\n");
- printf("Frame received when abort sent.\n");
- return -1;
- }
- }
- else
- {
- if (abort_reported)
- {
- printf("Abort received when not sent\n");
- return -1;
- }
- if (!frame_handled)
- {
- if (frame_failed)
- printf("Frame failed.\n");
- printf("Frame not received.\n");
- return -1;
- }
- }
- ref_len = cook_up_msg(buf);
- hdlc_tx_frame(&tx, buf, ref_len);
- if ((abort = rand() & 1))
- hdlc_tx_abort(&tx);
- frame_handled = false;
- abort_reported = false;
- }
- }
- printf("Tests passed.\n");
- return 0;
- }
- /*- End of function --------------------------------------------------------*/
- #if 0
- static int test_hdlc_octet_count_handling(void)
- {
- int i;
- int j;
- int nextbyte;
- printf("Testing the octet_counting handling using CRC-16 (bit by bit)\n");
- frame_len_errors = 0;
- frame_data_errors = 0;
- hdlc_tx_init(&tx, false, 2, false, underflow_handler, NULL);
- hdlc_rx_init(&rx, false, true, 0, frame_handler, NULL);
- //hdlc_rx_set_max_frame_len(&rx, 50);
- hdlc_rx_set_octet_counting_report_interval(&rx, 16);
- underflow_reported = false;
- framing_ok_reported = false;
- framing_ok_reports = 0;
- hdlc_tx_flags(&tx, 10);
- /* Don't push an initial message so we should get an underflow after the preamble. */
- /* Lie for the first message, as there isn't really one */
- frame_handled = true;
- frame_failed = false;
- frames_sent = 0;
- bytes_sent = 0;
- ref_len = 0;
- for (i = 0; i < 8*1000000; i++)
- {
- nextbyte = hdlc_tx_get_bit(&tx);
- hdlc_rx_put_bit(&rx, nextbyte);
- if (framing_ok_reported)
- {
- printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports);
- framing_ok_reported = false;
- }
- if (underflow_reported)
- {
- underflow_reported = false;
- for (j = 0; j < 20; j++)
- {
- nextbyte = hdlc_tx_get_bit(&tx);
- hdlc_rx_put_bit(&rx, nextbyte);
- if (framing_ok_reported)
- {
- printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled);
- framing_ok_reported = false;
- }
- }
- if (ref_len)
- {
- frames_sent++;
- bytes_sent += ref_len;
- }
- if (!frame_handled)
- {
- if (frame_failed)
- printf("Frame failed.\n");
- printf("Frame not received.\n");
- return -1;
- }
- ref_len = cook_up_msg(buf);
- hdlc_tx_frame(&tx, buf, ref_len);
- hdlc_tx_abort(&tx);
- //hdlc_tx_corrupt_frame(&tx);
- frame_handled = false;
- }
- }
- printf("Tests passed.\n");
- return 0;
- }
- /*- End of function --------------------------------------------------------*/
- #endif
- static void hdlc_tests(void)
- {
- printf("HDLC module tests\n");
- if (test_hdlc_modes())
- {
- printf("Tests failed\n");
- exit(2);
- }
- if (test_hdlc_frame_length_error_handling())
- {
- printf("Tests failed\n");
- exit(2);
- }
- if (test_hdlc_crc_error_handling())
- {
- printf("Tests failed\n");
- exit(2);
- }
- if (test_hdlc_abort_handling())
- {
- printf("Tests failed\n");
- exit(2);
- }
- #if 0
- if (test_hdlc_octet_count_handling())
- {
- printf("Tests failed\n");
- exit(2);
- }
- #endif
- printf("Tests passed.\n");
- }
- /*- End of function --------------------------------------------------------*/
- static void decode_handler(void *user_data, const uint8_t *pkt, int len, int ok)
- {
- int i;
- if (len < 0)
- {
- /* Special conditions */
- printf("HDLC rx status is %s (%d)\n", signal_status_to_str(len), len);
- return;
- }
- if (ok)
- {
- printf("Good frame, len = %d\n", len);
- printf("HDLC: ");
- for (i = 0; i < len; i++)
- printf("%02X ", pkt[i]);
- printf("\n");
- }
- else
- {
- printf("Bad frame, len = %d\n", len);
- }
- }
- /*- End of function --------------------------------------------------------*/
- static void decode_bitstream(const char *in_file_name)
- {
- char buf[1024];
- int bit;
- int num;
- hdlc_rx_state_t rx;
- FILE *in;
- if ((in = fopen(in_file_name, "r")) == NULL)
- {
- fprintf(stderr, "Failed to open '%s'\n", in_file_name);
- exit(2);
- }
- hdlc_rx_init(&rx, false, true, 2, decode_handler, NULL);
- while (fgets(buf, 1024, in))
- {
- if (sscanf(buf, "Rx bit %d - %d", &num, &bit) == 2)
- {
- //printf("Rx bit %d - %d\n", num, bit);
- //printf("Rx bit %d\n", bit);
- hdlc_rx_put_bit(&rx, bit);
- }
- }
- fclose(in);
- }
- /*- End of function --------------------------------------------------------*/
- int main(int argc, char *argv[])
- {
- int opt;
- const char *in_file_name;
- in_file_name = NULL;
- while ((opt = getopt(argc, argv, "d:")) != -1)
- {
- switch (opt)
- {
- case 'd':
- in_file_name = optarg;
- break;
- default:
- //usage();
- exit(2);
- break;
- }
- }
- if (in_file_name)
- decode_bitstream(in_file_name);
- else
- hdlc_tests();
- return 0;
- }
- /*- End of function --------------------------------------------------------*/
- /*- End of file ------------------------------------------------------------*/
|