From d2d71759084d4a504253d1baa982c6797b1bf6d7 Mon Sep 17 00:00:00 2001 From: Steve Underwood Date: Tue, 28 Aug 2012 15:32:00 +0800 Subject: [PATCH] Bring fax_modem handling in the fax_tester code into line with the upstream code --- libs/spandsp/tests/fax_tester.c | 129 +++++++++----------------------- 1 file changed, 36 insertions(+), 93 deletions(-) diff --git a/libs/spandsp/tests/fax_tester.c b/libs/spandsp/tests/fax_tester.c index 30a58650ef..57707aa1ea 100644 --- a/libs/spandsp/tests/fax_tester.c +++ b/libs/spandsp/tests/fax_tester.c @@ -316,20 +316,17 @@ static void hdlc_accept(void *user_data, const uint8_t *msg, int len, int ok) static int v17_v21_rx(void *user_data, const int16_t amp[], int len) { - faxtester_state_t *t; fax_modems_state_t *s; - t = (faxtester_state_t *) user_data; - s = &t->modems; + s = (fax_modems_state_t *) user_data; v17_rx(&s->fast_modems.v17_rx, amp, len); fsk_rx(&s->v21_rx, amp, len); if (s->rx_trained) { /* The fast modem has trained, so we no longer need to run the slow one in parallel. */ - span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.17 (%.2fdBm0)\n", v17_rx_signal_power(&s->fast_modems.v17_rx)); - s->rx_handler = (span_rx_handler_t) &v17_rx; - s->rx_user_data = &s->fast_modems.v17_rx; + span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.17 (%.2fdBm0)\n", v17_rx_signal_power(&s->fast_modems.v17_rx)); + fax_modems_set_rx_handler(s, (span_rx_handler_t) &v17_rx, &s->fast_modems.v17_rx, NULL, NULL); } return 0; } @@ -337,20 +334,17 @@ static int v17_v21_rx(void *user_data, const int16_t amp[], int len) static int v27ter_v21_rx(void *user_data, const int16_t amp[], int len) { - faxtester_state_t *t; fax_modems_state_t *s; - t = (faxtester_state_t *) user_data; - s = &t->modems; + s = (fax_modems_state_t *) user_data; v27ter_rx(&s->fast_modems.v27ter_rx, amp, len); fsk_rx(&s->v21_rx, amp, len); if (s->rx_trained) { /* The fast modem has trained, so we no longer need to run the slow one in parallel. */ - span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.27ter (%.2fdBm0)\n", v27ter_rx_signal_power(&s->fast_modems.v27ter_rx)); - s->rx_handler = (span_rx_handler_t) &v27ter_rx; - s->rx_user_data = &s->fast_modems.v27ter_rx; + span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.27ter (%.2fdBm0)\n", v27ter_rx_signal_power(&s->fast_modems.v27ter_rx)); + fax_modems_set_rx_handler(s, (span_rx_handler_t) &v27ter_rx, &s->fast_modems.v27ter_rx, NULL, NULL); } return 0; } @@ -358,20 +352,17 @@ static int v27ter_v21_rx(void *user_data, const int16_t amp[], int len) static int v29_v21_rx(void *user_data, const int16_t amp[], int len) { - faxtester_state_t *t; fax_modems_state_t *s; - t = (faxtester_state_t *) user_data; - s = &t->modems; + s = (fax_modems_state_t *) user_data; v29_rx(&s->fast_modems.v29_rx, amp, len); fsk_rx(&s->v21_rx, amp, len); if (s->rx_trained) { /* The fast modem has trained, so we no longer need to run the slow one in parallel. */ - span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.29 (%.2fdBm0)\n", v29_rx_signal_power(&s->fast_modems.v29_rx)); - s->rx_handler = (span_rx_handler_t) &v29_rx; - s->rx_user_data = &s->fast_modems.v29_rx; + span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.29 (%.2fdBm0)\n", v29_rx_signal_power(&s->fast_modems.v29_rx)); + fax_modems_set_rx_handler(s, (span_rx_handler_t) &v29_rx, &s->fast_modems.v29_rx, NULL, NULL); } return 0; } @@ -444,8 +435,6 @@ void faxtester_set_rx_type(void *user_data, int type, int bit_rate, int short_tr faxtester_state_t *s; fax_modems_state_t *t; int tone; - put_bit_func_t put_bit_func; - void *put_bit_user_data; s = (faxtester_state_t *) user_data; t = &s->modems; @@ -454,16 +443,7 @@ void faxtester_set_rx_type(void *user_data, int type, int bit_rate, int short_tr return; s->current_rx_type = type; if (use_hdlc) - { - put_bit_func = (put_bit_func_t) hdlc_rx_put_bit; - put_bit_user_data = (void *) &t->hdlc_rx; hdlc_rx_init(&t->hdlc_rx, FALSE, FALSE, HDLC_FRAMING_OK_THRESHOLD, hdlc_accept, s); - } - else - { - put_bit_func = non_ecm_put_bit; - put_bit_user_data = (void *) s; - } switch (type) { case T30_MODEM_CED: @@ -473,41 +453,31 @@ void faxtester_set_rx_type(void *user_data, int type, int bit_rate, int short_tr tone, tone_detected, (void *) s); - t->rx_handler = (span_rx_handler_t) &modem_connect_tones_rx; - t->rx_user_data = &t->connect_rx; + fax_modems_set_rx_handler(t, (span_rx_handler_t) &modem_connect_tones_rx, &t->connect_rx, NULL, NULL); s->tone_state = MODEM_CONNECT_TONES_NONE; break; case T30_MODEM_V21: if (s->flush_handler) s->flush_handler(s, s->flush_user_data, 3); - fsk_rx_init(&t->v21_rx, &preset_fsk_specs[FSK_V21CH2], FSK_FRAME_MODE_SYNC, (put_bit_func_t) hdlc_rx_put_bit, put_bit_user_data); - fsk_rx_signal_cutoff(&t->v21_rx, -45.5); - t->rx_handler = (span_rx_handler_t) &fsk_rx; - t->rx_user_data = &t->v21_rx; + fax_modems_start_slow_modem(t, FAX_MODEM_V21_RX); + fax_modems_set_rx_handler(t, (span_rx_handler_t) &fsk_rx, &t->v21_rx, NULL, NULL); break; case T30_MODEM_V27TER: - v27ter_rx_restart(&t->fast_modems.v27ter_rx, bit_rate, FALSE); - v27ter_rx_set_put_bit(&t->fast_modems.v27ter_rx, put_bit_func, put_bit_user_data); - t->rx_handler = (span_rx_handler_t) &v27ter_v21_rx; - t->rx_user_data = s; + fax_modems_start_fast_modem(t, FAX_MODEM_V27TER_RX, bit_rate, short_train, use_hdlc); + fax_modems_set_rx_handler(t, (span_rx_handler_t) &v27ter_v21_rx, t, NULL, NULL); break; case T30_MODEM_V29: - v29_rx_restart(&t->fast_modems.v29_rx, bit_rate, FALSE); - v29_rx_set_put_bit(&t->fast_modems.v29_rx, put_bit_func, put_bit_user_data); - t->rx_handler = (span_rx_handler_t) &v29_v21_rx; - t->rx_user_data = s; + fax_modems_start_fast_modem(t, FAX_MODEM_V29_RX, bit_rate, short_train, use_hdlc); + fax_modems_set_rx_handler(t, (span_rx_handler_t) &v29_v21_rx, t, NULL, NULL); break; case T30_MODEM_V17: - v17_rx_restart(&t->fast_modems.v17_rx, bit_rate, short_train); - v17_rx_set_put_bit(&t->fast_modems.v17_rx, put_bit_func, put_bit_user_data); - t->rx_handler = (span_rx_handler_t) &v17_v21_rx; - t->rx_user_data = s; + fax_modems_start_fast_modem(t, FAX_MODEM_V17_RX, bit_rate, short_train, use_hdlc); + fax_modems_set_rx_handler(t, (span_rx_handler_t) &v17_v21_rx, t, NULL, NULL); break; case T30_MODEM_DONE: span_log(&s->logging, SPAN_LOG_FLOW, "FAX exchange complete\n"); default: - t->rx_handler = (span_rx_handler_t) &span_dummy_rx; - t->rx_user_data = s; + fax_modems_set_rx_handler(t, (span_rx_handler_t) &span_dummy_rx, s, NULL, NULL); break; } } @@ -554,14 +524,14 @@ void faxtester_set_tx_type(void *user_data, int type, int bit_rate, int short_tr s->transmit = TRUE; break; case T30_MODEM_V21: - fsk_tx_init(&t->v21_tx, &preset_fsk_specs[FSK_V21CH2], get_bit_func, get_bit_user_data); + fax_modems_start_slow_modem(t, FAX_MODEM_V21_TX); fsk_tx_set_modem_status_handler(&t->v21_tx, modem_tx_status, (void *) s); fax_modems_set_tx_handler(t, (span_tx_handler_t) &fsk_tx, &t->v21_tx); s->transmit = TRUE; break; case T30_MODEM_V27TER: - v27ter_tx_restart(&t->fast_modems.v27ter_tx, bit_rate, t->use_tep); - v27ter_tx_set_get_bit(&t->fast_modems.v27ter_tx, get_bit_func, get_bit_user_data); + fax_modems_set_get_bit(t, get_bit_func, get_bit_user_data); + fax_modems_start_fast_modem(t, FAX_MODEM_V27TER_TX, bit_rate, short_train, use_hdlc); v27ter_tx_set_modem_status_handler(&t->fast_modems.v27ter_tx, modem_tx_status, (void *) s); fax_modems_set_tx_handler(t, (span_tx_handler_t) &v27ter_tx, &t->fast_modems.v27ter_tx); /* For any fast modem, set 200ms of preamble flags */ @@ -569,8 +539,8 @@ void faxtester_set_tx_type(void *user_data, int type, int bit_rate, int short_tr s->transmit = TRUE; break; case T30_MODEM_V29: - v29_tx_restart(&t->fast_modems.v29_tx, bit_rate, t->use_tep); - v29_tx_set_get_bit(&t->fast_modems.v29_tx, get_bit_func, get_bit_user_data); + fax_modems_set_get_bit(t, get_bit_func, get_bit_user_data); + fax_modems_start_fast_modem(t, FAX_MODEM_V29_TX, bit_rate, short_train, use_hdlc); v29_tx_set_modem_status_handler(&t->fast_modems.v29_tx, modem_tx_status, (void *) s); fax_modems_set_tx_handler(t, (span_tx_handler_t) &v29_tx, &t->fast_modems.v29_tx); /* For any fast modem, set 200ms of preamble flags */ @@ -578,8 +548,8 @@ void faxtester_set_tx_type(void *user_data, int type, int bit_rate, int short_tr s->transmit = TRUE; break; case T30_MODEM_V17: - v17_tx_restart(&t->fast_modems.v17_tx, bit_rate, t->use_tep, short_train); - v17_tx_set_get_bit(&t->fast_modems.v17_tx, get_bit_func, get_bit_user_data); + fax_modems_set_get_bit(t, get_bit_func, get_bit_user_data); + fax_modems_start_fast_modem(t, FAX_MODEM_V17_TX, bit_rate, short_train, use_hdlc); v17_tx_set_modem_status_handler(&t->fast_modems.v17_tx, modem_tx_status, (void *) s); fax_modems_set_tx_handler(t, (span_tx_handler_t) &v17_tx, &t->fast_modems.v17_tx); /* For any fast modem, set 200ms of preamble flags */ @@ -641,42 +611,6 @@ void faxtester_set_front_end_step_timeout_handler(faxtester_state_t *s, faxteste } /*- End of function --------------------------------------------------------*/ -static void faxtester_fax_modems_init(fax_modems_state_t *s, int use_tep, void *user_data) -{ - 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_underflow_handler, user_data); - fsk_rx_init(&s->v21_rx, &preset_fsk_specs[FSK_V21CH2], FSK_FRAME_MODE_SYNC, (put_bit_func_t) hdlc_rx_put_bit, &s->hdlc_rx); - fsk_rx_signal_cutoff(&s->v21_rx, -45.5); - fsk_tx_init(&s->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &s->hdlc_tx); - fsk_tx_set_modem_status_handler(&s->v21_tx, modem_tx_status, user_data); - v17_rx_init(&s->fast_modems.v17_rx, 14400, non_ecm_put_bit, user_data); - v17_tx_init(&s->fast_modems.v17_tx, 14400, s->use_tep, non_ecm_get_bit, user_data); - v17_tx_set_modem_status_handler(&s->fast_modems.v17_tx, modem_tx_status, user_data); - v29_rx_init(&s->fast_modems.v29_rx, 9600, non_ecm_put_bit, user_data); - v29_rx_signal_cutoff(&s->fast_modems.v29_rx, -45.5); - v29_tx_init(&s->fast_modems.v29_tx, 9600, s->use_tep, non_ecm_get_bit, user_data); - v29_tx_set_modem_status_handler(&s->fast_modems.v29_tx, modem_tx_status, user_data); - v27ter_rx_init(&s->fast_modems.v27ter_rx, 4800, non_ecm_put_bit, user_data); - v27ter_tx_init(&s->fast_modems.v27ter_tx, 4800, s->use_tep, non_ecm_get_bit, user_data); - v27ter_tx_set_modem_status_handler(&s->fast_modems.v27ter_tx, modem_tx_status, user_data); - silence_gen_init(&s->silence_gen, 0); - modem_connect_tones_tx_init(&s->connect_tx, MODEM_CONNECT_TONES_FAX_CNG); - modem_connect_tones_rx_init(&s->connect_rx, - MODEM_CONNECT_TONES_FAX_CNG, - tone_detected, - 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; -} -/*- End of function --------------------------------------------------------*/ - faxtester_state_t *faxtester_init(faxtester_state_t *s, int calling_party) { if (s == NULL) @@ -688,7 +622,16 @@ faxtester_state_t *faxtester_init(faxtester_state_t *s, int calling_party) memset(s, 0, sizeof(*s)); span_log_init(&s->logging, SPAN_LOG_NONE, NULL); span_log_set_protocol(&s->logging, "TST"); - faxtester_fax_modems_init(&s->modems, FALSE, s); + fax_modems_init(&s->modems, + FALSE, + NULL, + hdlc_underflow_handler, + non_ecm_put_bit, + t38_non_ecm_buffer_get_bit, + tone_detected, + s); + fax_modems_set_tep_mode(&s->modems, FALSE); + fax_modems_set_rx_active(&s->modems, TRUE); faxtester_set_timeout(s, -1); faxtester_set_tx_type(s, T30_MODEM_NONE, 0, FALSE, FALSE);