SPAN_DECLARE(fax_modems_state_t *) fax_modems_init(fax_modems_state_t *s, int use_tep, hdlc_frame_handler_t hdlc_accept, hdlc_underflow_handler_t hdlc_tx_underflow, put_bit_func_t non_ecm_put_bit, get_bit_func_t non_ecm_get_bit, tone_report_func_t tone_callback, void *user_data) { if (s == NULL) { if ((s = (fax_modems_state_t *) span_alloc(sizeof(*s))) == NULL) return NULL; } /*endif*/ memset(s, 0, sizeof(*s)); s->use_tep = use_tep; modem_connect_tones_tx_init(&s->connect_tx, MODEM_CONNECT_TONES_FAX_CNG); span_log_init(&s->logging, SPAN_LOG_NONE, NULL); span_log_set_protocol(&s->logging, "FAX modems"); s->tone_callback = tone_callback; s->tone_callback_user_data = user_data; if (tone_callback) { modem_connect_tones_rx_init(&s->connect_rx, MODEM_CONNECT_TONES_FAX_CNG, s->tone_callback, s->tone_callback_user_data); } /*endif*/ dc_restore_init(&s->dc_restore); s->get_bit = non_ecm_get_bit; s->get_bit_user_data = user_data; s->put_bit = non_ecm_put_bit; s->put_bit_user_data = user_data; s->hdlc_accept = hdlc_accept; s->hdlc_accept_user_data = user_data; hdlc_rx_init(&s->hdlc_rx, false, false, HDLC_FRAMING_OK_THRESHOLD, fax_modems_hdlc_accept, s); hdlc_tx_init(&s->hdlc_tx, false, 2, false, hdlc_tx_underflow, user_data); fax_modems_start_slow_modem(s, FAX_MODEM_V21_RX); fsk_tx_init(&s->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &s->hdlc_tx); silence_gen_init(&s->silence_gen, 0); s->rx_signal_present = false; s->rx_handler = (span_rx_handler_t) &span_dummy_rx; s->rx_fillin_handler = (span_rx_fillin_handler_t) &span_dummy_rx; s->rx_user_data = NULL; s->rx_fillin_user_data = NULL; s->tx_handler = (span_tx_handler_t) &silence_gen; s->tx_user_data = &s->silence_gen; return s; }
SPAN_DECLARE(fax_modems_state_t *) fax_modems_init(fax_modems_state_t *s, int use_tep, hdlc_frame_handler_t hdlc_accept, hdlc_underflow_handler_t hdlc_tx_underflow, put_bit_func_t non_ecm_put_bit, get_bit_func_t non_ecm_get_bit, tone_report_func_t tone_callback, void *user_data) { if (s == NULL) { if ((s = (fax_modems_state_t *) malloc(sizeof(*s))) == NULL) return NULL; } memset(s, 0, sizeof(*s)); s->use_tep = use_tep; hdlc_rx_init(&s->hdlc_rx, FALSE, FALSE, HDLC_FRAMING_OK_THRESHOLD, hdlc_accept, user_data); hdlc_tx_init(&s->hdlc_tx, FALSE, 2, FALSE, hdlc_tx_underflow, user_data); fsk_rx_init(&s->v21_rx, &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) hdlc_rx_put_bit, &s->hdlc_rx); fsk_rx_signal_cutoff(&s->v21_rx, -39.09f); fsk_tx_init(&s->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &s->hdlc_tx); v17_rx_init(&s->v17_rx, 14400, non_ecm_put_bit, user_data); v17_tx_init(&s->v17_tx, 14400, s->use_tep, non_ecm_get_bit, user_data); v29_rx_init(&s->v29_rx, 9600, non_ecm_put_bit, user_data); v29_rx_signal_cutoff(&s->v29_rx, -45.5f); v29_tx_init(&s->v29_tx, 9600, s->use_tep, non_ecm_get_bit, user_data); v27ter_rx_init(&s->v27ter_rx, 4800, non_ecm_put_bit, user_data); v27ter_tx_init(&s->v27ter_tx, 4800, s->use_tep, non_ecm_get_bit, user_data); silence_gen_init(&s->silence_gen, 0); modem_connect_tones_tx_init(&s->connect_tx, MODEM_CONNECT_TONES_FAX_CNG); if (tone_callback) { modem_connect_tones_rx_init(&s->connect_rx, MODEM_CONNECT_TONES_FAX_CNG, tone_callback, user_data); } dc_restore_init(&s->dc_restore); s->rx_signal_present = FALSE; s->rx_handler = (span_rx_handler_t *) &span_dummy_rx; s->rx_user_data = NULL; s->tx_handler = (span_tx_handler_t *) &silence_gen; s->tx_user_data = &s->silence_gen; return s; }
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; }
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; }
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; }
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; }
fax_state_t *fax_init(fax_state_t *s, int calling_party) { if (s == NULL) { if ((s = (fax_state_t *) malloc(sizeof(*s))) == NULL) return NULL; } memset(s, 0, sizeof(*s)); span_log_init(&s->logging, SPAN_LOG_NONE, NULL); span_log_set_protocol(&s->logging, "FAX"); t30_init(&(s->t30_state), calling_party, fax_set_rx_type, (void *) s, fax_set_tx_type, (void *) s, fax_send_hdlc, (void *) s); t30_set_supported_modems(&(s->t30_state), T30_SUPPORT_V27TER | T30_SUPPORT_V29); hdlc_rx_init(&(s->hdlcrx), FALSE, FALSE, 5, t30_hdlc_accept, &(s->t30_state)); fsk_rx_init(&(s->v21rx), &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) hdlc_rx_put_bit, &(s->hdlcrx)); fsk_rx_signal_cutoff(&(s->v21rx), -45.5); hdlc_tx_init(&(s->hdlctx), FALSE, 2, FALSE, hdlc_underflow_handler, &(s->t30_state)); s->first_tx_hdlc_frame = TRUE; fsk_tx_init(&(s->v21tx), &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &(s->hdlctx)); v17_rx_init(&(s->v17rx), 14400, t30_non_ecm_put_bit, &(s->t30_state)); v17_tx_init(&(s->v17tx), 14400, s->use_tep, t30_non_ecm_get_bit, &(s->t30_state)); v29_rx_init(&(s->v29rx), 9600, t30_non_ecm_put_bit, &(s->t30_state)); v29_rx_signal_cutoff(&(s->v29rx), -45.5); v29_tx_init(&(s->v29tx), 9600, s->use_tep, t30_non_ecm_get_bit, &(s->t30_state)); v27ter_rx_init(&(s->v27ter_rx), 4800, t30_non_ecm_put_bit, &(s->t30_state)); v27ter_tx_init(&(s->v27ter_tx), 4800, s->use_tep, t30_non_ecm_get_bit, &(s->t30_state)); silence_gen_init(&(s->silence_gen), 0); dc_restore_init(&(s->dc_restore)); t30_restart(&(s->t30_state)); #if defined(LOG_FAX_AUDIO) { char buf[100 + 1]; struct tm *tm; time_t now; time(&now); tm = localtime(&now); sprintf(buf, "/tmp/fax-rx-audio-%x-%02d%02d%02d%02d%02d%02d", s, tm->tm_year%100, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); s->fax_audio_rx_log = open(buf, O_CREAT | O_TRUNC | O_WRONLY, 0666); sprintf(buf, "/tmp/fax-tx-audio-%x-%02d%02d%02d%02d%02d%02d", s, tm->tm_year%100, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); s->fax_audio_tx_log = open(buf, O_CREAT | O_TRUNC | O_WRONLY, 0666); } #endif return s; }