From 33a4adba62eb7286cb1bffd6347da47c86c39aea Mon Sep 17 00:00:00 2001 From: Steve Underwood Date: Fri, 18 Jul 2014 10:29:23 +0800 Subject: [PATCH] Better handling of bad HDLC frames in T.38 gateway operation. --- libs/spandsp/src/t38_gateway.c | 17 +++++++++++++---- libs/spandsp/tests/fax_tests.c | 3 +-- libs/spandsp/tests/v22bis_tests.c | 4 ++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/libs/spandsp/src/t38_gateway.c b/libs/spandsp/src/t38_gateway.c index 2a9e41b74c..bb3d02e902 100644 --- a/libs/spandsp/src/t38_gateway.c +++ b/libs/spandsp/src/t38_gateway.c @@ -1914,7 +1914,7 @@ static void t38_hdlc_rx_put_bit(hdlc_rx_state_t *t, int new_bit) } /*endif*/ t->num_bits++; - if (!t->framing_ok_announced) + if (t->flags_seen < t->framing_ok_threshold) return; /*endif*/ t->byte_in_progress = (t->byte_in_progress >> 1) | ((t->raw_bit_stream & 0x01) << 7); @@ -1922,17 +1922,26 @@ static void t38_hdlc_rx_put_bit(hdlc_rx_state_t *t, int new_bit) return; /*endif*/ t->num_bits = 0; + s = (t38_gateway_state_t *) t->frame_user_data; + u = &s->core.to_t38; if (t->len >= (int) sizeof(t->buffer)) { /* This is too long. Abandon the frame, and wait for the next flag octet. */ + if ((t->len + 2) >= u->octets_per_data_packet) + { + /* We will have sent some of this frame already, so we need to send termination of this bad HDLC frame. */ + category = (s->t38x.current_tx_data_type == T38_DATA_V21) ? T38_PACKET_CATEGORY_CONTROL_DATA : T38_PACKET_CATEGORY_IMAGE_DATA; + if (t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_FCS_BAD, NULL, 0, category) < 0) + span_log(&s->logging, SPAN_LOG_WARNING, "T.38 send failed\n"); + /*endif*/ + } + /*endif*/ t->rx_length_errors++; t->flags_seen = t->framing_ok_threshold - 1; t->len = 0; return; } /*endif*/ - s = (t38_gateway_state_t *) t->frame_user_data; - u = &s->core.to_t38; t->buffer[t->len] = (uint8_t) t->byte_in_progress; if (t->len == 1) { @@ -1969,7 +1978,7 @@ static void t38_hdlc_rx_put_bit(hdlc_rx_state_t *t, int new_bit) } if (++u->data_ptr >= u->octets_per_data_packet) { - bit_reverse(u->data, t->buffer + t->len - 2 - u->data_ptr, u->data_ptr); + bit_reverse(u->data, &t->buffer[t->len - 2 - u->data_ptr], u->data_ptr); category = (s->t38x.current_tx_data_type == T38_DATA_V21) ? T38_PACKET_CATEGORY_CONTROL_DATA : T38_PACKET_CATEGORY_IMAGE_DATA; if (t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_DATA, u->data, u->data_ptr, category) < 0) span_log(&s->logging, SPAN_LOG_WARNING, "T.38 send failed\n"); diff --git a/libs/spandsp/tests/fax_tests.c b/libs/spandsp/tests/fax_tests.c index 9dbf6ce4e6..f3b34b91a3 100644 --- a/libs/spandsp/tests/fax_tests.c +++ b/libs/spandsp/tests/fax_tests.c @@ -420,8 +420,7 @@ static void set_t30_callbacks(t30_state_t *t30, int chan) } /*- End of function --------------------------------------------------------*/ -static void real_time_gateway_frame_handler(t38_gateway_state_t *s, - void *user_data, +static void real_time_gateway_frame_handler(void *user_data, bool incoming, const uint8_t *msg, int len) diff --git a/libs/spandsp/tests/v22bis_tests.c b/libs/spandsp/tests/v22bis_tests.c index d42b2f4f4d..f3c0532e92 100644 --- a/libs/spandsp/tests/v22bis_tests.c +++ b/libs/spandsp/tests/v22bis_tests.c @@ -263,11 +263,11 @@ int main(int argc, char *argv[]) int j; int test_bps; int line_model_no; + int channel_codec; + int rbs_pattern; int bits_per_test; int noise_level; int signal_level; - int channel_codec; - int rbs_pattern; int guard_tone_option; int opt; bool log_audio;