Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/src/t38_gateway.c @ 4:26cd8f1ef0b1
import spandsp-0.0.6pre17
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 15:50:58 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:c6c5a16ce2f2 | 4:26cd8f1ef0b1 |
---|---|
1 //#define LOG_FAX_AUDIO | |
2 /* | |
3 * SpanDSP - a series of DSP components for telephony | |
4 * | |
5 * t38_gateway.c - A T.38 gateway, less the packet exchange part | |
6 * | |
7 * Written by Steve Underwood <steveu@coppice.org> | |
8 * | |
9 * Copyright (C) 2005, 2006, 2007, 2008 Steve Underwood | |
10 * | |
11 * All rights reserved. | |
12 * | |
13 * This program is free software; you can redistribute it and/or modify | |
14 * it under the terms of the GNU Lesser General Public License version 2.1, | |
15 * as published by the Free Software Foundation. | |
16 * | |
17 * This program is distributed in the hope that it will be useful, | |
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 * GNU Lesser General Public License for more details. | |
21 * | |
22 * You should have received a copy of the GNU Lesser General Public | |
23 * License along with this program; if not, write to the Free Software | |
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 * | |
26 * $Id: t38_gateway.c,v 1.171.4.2 2009/12/19 10:44:10 steveu Exp $ | |
27 */ | |
28 | |
29 /*! \file */ | |
30 | |
31 #if defined(HAVE_CONFIG_H) | |
32 #include "config.h" | |
33 #endif | |
34 | |
35 #include <inttypes.h> | |
36 #include <stdlib.h> | |
37 #include <stdio.h> | |
38 #include <fcntl.h> | |
39 #include <time.h> | |
40 #include <string.h> | |
41 #if defined(HAVE_TGMATH_H) | |
42 #include <tgmath.h> | |
43 #endif | |
44 #if defined(HAVE_MATH_H) | |
45 #include <math.h> | |
46 #endif | |
47 #include "floating_fudge.h" | |
48 #include <assert.h> | |
49 #if defined(LOG_FAX_AUDIO) | |
50 #include <unistd.h> | |
51 #endif | |
52 #include <tiffio.h> | |
53 | |
54 #include "spandsp/telephony.h" | |
55 #include "spandsp/logging.h" | |
56 #include "spandsp/queue.h" | |
57 #include "spandsp/dc_restore.h" | |
58 #include "spandsp/bit_operations.h" | |
59 #include "spandsp/power_meter.h" | |
60 #include "spandsp/complex.h" | |
61 #include "spandsp/tone_detect.h" | |
62 #include "spandsp/tone_generate.h" | |
63 #include "spandsp/async.h" | |
64 #include "spandsp/crc.h" | |
65 #include "spandsp/hdlc.h" | |
66 #include "spandsp/silence_gen.h" | |
67 #include "spandsp/fsk.h" | |
68 #include "spandsp/v29tx.h" | |
69 #include "spandsp/v29rx.h" | |
70 #include "spandsp/v27ter_tx.h" | |
71 #include "spandsp/v27ter_rx.h" | |
72 #include "spandsp/v17tx.h" | |
73 #include "spandsp/v17rx.h" | |
74 #include "spandsp/super_tone_rx.h" | |
75 #include "spandsp/modem_connect_tones.h" | |
76 #include "spandsp/t4_rx.h" | |
77 #include "spandsp/t4_tx.h" | |
78 #include "spandsp/t30_fcf.h" | |
79 #include "spandsp/t35.h" | |
80 #include "spandsp/t30.h" | |
81 #include "spandsp/t30_logging.h" | |
82 #include "spandsp/fax_modems.h" | |
83 #include "spandsp/t38_core.h" | |
84 #include "spandsp/t38_non_ecm_buffer.h" | |
85 #include "spandsp/t38_gateway.h" | |
86 | |
87 #include "spandsp/private/logging.h" | |
88 #include "spandsp/private/silence_gen.h" | |
89 #include "spandsp/private/fsk.h" | |
90 #include "spandsp/private/v17tx.h" | |
91 #include "spandsp/private/v17rx.h" | |
92 #include "spandsp/private/v27ter_tx.h" | |
93 #include "spandsp/private/v27ter_rx.h" | |
94 #include "spandsp/private/v29tx.h" | |
95 #include "spandsp/private/v29rx.h" | |
96 #include "spandsp/private/modem_connect_tones.h" | |
97 #include "spandsp/private/hdlc.h" | |
98 #include "spandsp/private/fax_modems.h" | |
99 #include "spandsp/private/t4_rx.h" | |
100 #include "spandsp/private/t4_tx.h" | |
101 #include "spandsp/private/t30.h" | |
102 #include "spandsp/private/t38_core.h" | |
103 #include "spandsp/private/t38_non_ecm_buffer.h" | |
104 #include "spandsp/private/t38_gateway.h" | |
105 | |
106 /* This is the target time per transmission chunk. The actual | |
107 packet timing will sync to the data octets. */ | |
108 /*! The default number of milliseconds per transmitted IFP when sending bulk T.38 data */ | |
109 #define MS_PER_TX_CHUNK 30 | |
110 /*! The number of bytes which must be in the audio to T.38 HDLC buffer before we start | |
111 outputting them as IFP messages. */ | |
112 #define HDLC_START_BUFFER_LEVEL 8 | |
113 | |
114 /*! The number of transmissions of indicator IFP packets */ | |
115 #define INDICATOR_TX_COUNT 3 | |
116 /*! The number of transmissions of data IFP packets */ | |
117 #define DATA_TX_COUNT 1 | |
118 /*! The number of transmissions of terminating data IFP packets */ | |
119 #define DATA_END_TX_COUNT 3 | |
120 | |
121 enum | |
122 { | |
123 DISBIT1 = 0x01, | |
124 DISBIT2 = 0x02, | |
125 DISBIT3 = 0x04, | |
126 DISBIT4 = 0x08, | |
127 DISBIT5 = 0x10, | |
128 DISBIT6 = 0x20, | |
129 DISBIT7 = 0x40, | |
130 DISBIT8 = 0x80 | |
131 }; | |
132 | |
133 enum | |
134 { | |
135 T38_NONE, | |
136 T38_V27TER_RX, | |
137 T38_V29_RX, | |
138 T38_V17_RX | |
139 }; | |
140 | |
141 enum | |
142 { | |
143 HDLC_FLAG_FINISHED = 0x01, | |
144 HDLC_FLAG_CORRUPT_CRC = 0x02, | |
145 HDLC_FLAG_PROCEED_WITH_OUTPUT = 0x04, | |
146 HDLC_FLAG_MISSING_DATA = 0x08 | |
147 }; | |
148 | |
149 enum | |
150 { | |
151 FLAG_INDICATOR = 0x100, | |
152 FLAG_DATA = 0x200 | |
153 }; | |
154 | |
155 enum | |
156 { | |
157 TIMED_MODE_STARTUP = 0, | |
158 TIMED_MODE_IDLE, | |
159 TIMED_MODE_TCF_PREDICTABLE_MODEM_START_FAST_MODEM_ANNOUNCED, | |
160 TIMED_MODE_TCF_PREDICTABLE_MODEM_START_FAST_MODEM_SEEN, | |
161 TIMED_MODE_TCF_PREDICTABLE_MODEM_START_PAST_V21_MODEM, | |
162 TIMED_MODE_TCF_PREDICTABLE_MODEM_START_BEGIN, | |
163 }; | |
164 | |
165 /*! The maximum number of bytes to be zapped, in order to corrupt NSF, | |
166 NSS and NSC messages, so the receiver does not recognise them. */ | |
167 #define MAX_NSX_SUPPRESSION 10 | |
168 | |
169 /*! The number of consecutive flags to declare HDLC framing is OK. */ | |
170 #define HDLC_FRAMING_OK_THRESHOLD 5 | |
171 | |
172 static uint8_t nsx_overwrite[2][MAX_NSX_SUPPRESSION] = | |
173 { | |
174 {0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | |
175 {0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | |
176 }; | |
177 | |
178 static int restart_rx_modem(t38_gateway_state_t *s); | |
179 static int process_rx_indicator(t38_core_state_t *t, void *user_data, int indicator); | |
180 static void hdlc_underflow_handler(void *user_data); | |
181 static void to_t38_buffer_init(t38_gateway_to_t38_state_t *s); | |
182 static void t38_hdlc_rx_put_bit(hdlc_rx_state_t *t, int new_bit); | |
183 static void non_ecm_put_bit(void *user_data, int bit); | |
184 static void non_ecm_remove_fill_and_put_bit(void *user_data, int bit); | |
185 static void non_ecm_push_residue(t38_gateway_state_t *s); | |
186 static void tone_detected(void *user_data, int tone, int level, int delay); | |
187 | |
188 static void set_rx_handler(t38_gateway_state_t *s, span_rx_handler_t *handler, void *user_data) | |
189 { | |
190 if (s->audio.modems.rx_handler != span_dummy_rx) | |
191 s->audio.modems.rx_handler = handler; | |
192 s->audio.base_rx_handler = handler; | |
193 s->audio.modems.rx_user_data = user_data; | |
194 } | |
195 /*- End of function --------------------------------------------------------*/ | |
196 | |
197 static void set_tx_handler(t38_gateway_state_t *s, span_tx_handler_t *handler, void *user_data) | |
198 { | |
199 s->audio.modems.tx_handler = handler; | |
200 s->audio.modems.tx_user_data = user_data; | |
201 } | |
202 /*- End of function --------------------------------------------------------*/ | |
203 | |
204 static void set_next_tx_handler(t38_gateway_state_t *s, span_tx_handler_t *handler, void *user_data) | |
205 { | |
206 s->audio.modems.next_tx_handler = handler; | |
207 s->audio.modems.next_tx_user_data = user_data; | |
208 } | |
209 /*- End of function --------------------------------------------------------*/ | |
210 | |
211 static void set_rx_active(t38_gateway_state_t *s, int active) | |
212 { | |
213 s->audio.modems.rx_handler = (active) ? s->audio.base_rx_handler : span_dummy_rx; | |
214 } | |
215 /*- End of function --------------------------------------------------------*/ | |
216 | |
217 static int v17_v21_rx(void *user_data, const int16_t amp[], int len) | |
218 { | |
219 t38_gateway_state_t *t; | |
220 fax_modems_state_t *s; | |
221 | |
222 t = (t38_gateway_state_t *) user_data; | |
223 s = &t->audio.modems; | |
224 v17_rx(&s->v17_rx, amp, len); | |
225 if (s->rx_trained) | |
226 { | |
227 /* The fast modem has trained, so we no longer need to run the slow | |
228 one in parallel. */ | |
229 span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.17 (%.2fdBm0)\n", v17_rx_signal_power(&s->v17_rx)); | |
230 set_rx_handler(t, (span_rx_handler_t *) &v17_rx, &s->v17_rx); | |
231 } | |
232 else | |
233 { | |
234 fsk_rx(&s->v21_rx, amp, len); | |
235 if (s->rx_signal_present) | |
236 { | |
237 span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx)); | |
238 set_rx_handler(t, (span_rx_handler_t *) &fsk_rx, &s->v21_rx); | |
239 } | |
240 /*endif*/ | |
241 } | |
242 /*endif*/ | |
243 return 0; | |
244 } | |
245 /*- End of function --------------------------------------------------------*/ | |
246 | |
247 static int v27ter_v21_rx(void *user_data, const int16_t amp[], int len) | |
248 { | |
249 t38_gateway_state_t *t; | |
250 fax_modems_state_t *s; | |
251 | |
252 t = (t38_gateway_state_t *) user_data; | |
253 s = &t->audio.modems; | |
254 v27ter_rx(&s->v27ter_rx, amp, len); | |
255 if (s->rx_trained) | |
256 { | |
257 /* The fast modem has trained, so we no longer need to run the slow | |
258 one in parallel. */ | |
259 span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.27ter (%.2fdBm0)\n", v27ter_rx_signal_power(&s->v27ter_rx)); | |
260 set_rx_handler(t, (span_rx_handler_t *) &v27ter_rx, &s->v27ter_rx); | |
261 } | |
262 else | |
263 { | |
264 fsk_rx(&s->v21_rx, amp, len); | |
265 if (s->rx_signal_present) | |
266 { | |
267 span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx)); | |
268 set_rx_handler(t, (span_rx_handler_t *) &fsk_rx, &s->v21_rx); | |
269 } | |
270 /*endif*/ | |
271 } | |
272 /*endif*/ | |
273 return 0; | |
274 } | |
275 /*- End of function --------------------------------------------------------*/ | |
276 | |
277 static int v29_v21_rx(void *user_data, const int16_t amp[], int len) | |
278 { | |
279 t38_gateway_state_t *t; | |
280 fax_modems_state_t *s; | |
281 | |
282 t = (t38_gateway_state_t *) user_data; | |
283 s = &t->audio.modems; | |
284 v29_rx(&s->v29_rx, amp, len); | |
285 if (s->rx_trained) | |
286 { | |
287 /* The fast modem has trained, so we no longer need to run the slow | |
288 one in parallel. */ | |
289 span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.29 (%.2fdBm0)\n", v29_rx_signal_power(&s->v29_rx)); | |
290 set_rx_handler(t, (span_rx_handler_t *) &v29_rx, &s->v29_rx); | |
291 } | |
292 else | |
293 { | |
294 fsk_rx(&s->v21_rx, amp, len); | |
295 if (s->rx_signal_present) | |
296 { | |
297 span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx)); | |
298 set_rx_handler(t, (span_rx_handler_t *) &fsk_rx, &s->v21_rx); | |
299 } | |
300 /*endif*/ | |
301 } | |
302 /*endif*/ | |
303 return 0; | |
304 } | |
305 /*- End of function --------------------------------------------------------*/ | |
306 | |
307 static void tone_detected(void *user_data, int tone, int level, int delay) | |
308 { | |
309 t38_gateway_state_t *s; | |
310 | |
311 s = (t38_gateway_state_t *) user_data; | |
312 span_log(&s->logging, SPAN_LOG_FLOW, "%s detected (%ddBm0)\n", modem_connect_tone_to_str(tone), level); | |
313 } | |
314 /*- End of function --------------------------------------------------------*/ | |
315 | |
316 static void hdlc_underflow_handler(void *user_data) | |
317 { | |
318 t38_gateway_state_t *s; | |
319 t38_gateway_hdlc_state_t *t; | |
320 int old_data_type; | |
321 | |
322 s = (t38_gateway_state_t *) user_data; | |
323 t = &s->core.hdlc_to_modem; | |
324 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC underflow at %d\n", t->out); | |
325 /* If the current HDLC buffer is not at the HDLC_FLAG_PROCEED_WITH_OUTPUT stage, this | |
326 underflow must be an end of preamble condition. */ | |
327 if ((t->buf[t->out].flags & HDLC_FLAG_PROCEED_WITH_OUTPUT)) | |
328 { | |
329 old_data_type = t->buf[t->out].contents; | |
330 t->buf[t->out].len = 0; | |
331 t->buf[t->out].flags = 0; | |
332 t->buf[t->out].contents = 0; | |
333 if (++t->out >= T38_TX_HDLC_BUFS) | |
334 t->out = 0; | |
335 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC next is 0x%X\n", t->buf[t->out].contents); | |
336 if ((t->buf[t->out].contents & FLAG_INDICATOR)) | |
337 { | |
338 /* The next thing in the queue is an indicator, so we need to stop this modem. */ | |
339 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC shutdown\n"); | |
340 hdlc_tx_frame(&s->audio.modems.hdlc_tx, NULL, 0); | |
341 } | |
342 else if ((t->buf[t->out].contents & FLAG_DATA)) | |
343 { | |
344 /* Check if we should start sending the next frame */ | |
345 if ((t->buf[t->out].flags & HDLC_FLAG_PROCEED_WITH_OUTPUT)) | |
346 { | |
347 /* This frame is ready to go, and uses the same modem we are running now. So, send | |
348 whatever we have. This might or might not be an entire frame. */ | |
349 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC start next frame\n"); | |
350 hdlc_tx_frame(&s->audio.modems.hdlc_tx, t->buf[t->out].buf, t->buf[t->out].len); | |
351 if ((t->buf[t->out].flags & HDLC_FLAG_CORRUPT_CRC)) | |
352 hdlc_tx_corrupt_frame(&s->audio.modems.hdlc_tx); | |
353 /*endif*/ | |
354 } | |
355 /*endif*/ | |
356 } | |
357 /*endif*/ | |
358 } | |
359 /*endif*/ | |
360 } | |
361 /*- End of function --------------------------------------------------------*/ | |
362 | |
363 static int set_next_tx_type(t38_gateway_state_t *s) | |
364 { | |
365 get_bit_func_t get_bit_func; | |
366 void *get_bit_user_data; | |
367 int indicator; | |
368 int short_train; | |
369 fax_modems_state_t *t; | |
370 t38_gateway_hdlc_state_t *u; | |
371 | |
372 t = &s->audio.modems; | |
373 u = &s->core.hdlc_to_modem; | |
374 t38_non_ecm_buffer_report_output_status(&s->core.non_ecm_to_modem, &s->logging); | |
375 if (t->next_tx_handler) | |
376 { | |
377 /* There is a handler queued, so that is the next one. */ | |
378 set_tx_handler(s, t->next_tx_handler, t->next_tx_user_data); | |
379 set_next_tx_handler(s, NULL, NULL); | |
380 if (t->tx_handler == (span_tx_handler_t *) &(silence_gen) | |
381 || | |
382 t->tx_handler == (span_tx_handler_t *) &(tone_gen)) | |
383 { | |
384 set_rx_active(s, TRUE); | |
385 } | |
386 else | |
387 { | |
388 set_rx_active(s, FALSE); | |
389 } | |
390 /*endif*/ | |
391 return TRUE; | |
392 } | |
393 /*endif*/ | |
394 if (u->in == u->out) | |
395 return FALSE; | |
396 /*endif*/ | |
397 if ((u->buf[u->out].contents & FLAG_INDICATOR) == 0) | |
398 return FALSE; | |
399 /*endif*/ | |
400 indicator = (u->buf[u->out].contents & 0xFF); | |
401 u->buf[u->out].len = 0; | |
402 u->buf[u->out].flags = 0; | |
403 u->buf[u->out].contents = 0; | |
404 if (++u->out >= T38_TX_HDLC_BUFS) | |
405 u->out = 0; | |
406 /*endif*/ | |
407 span_log(&s->logging, SPAN_LOG_FLOW, "Changing to %s\n", t38_indicator_to_str(indicator)); | |
408 if (s->core.image_data_mode && s->core.ecm_mode) | |
409 { | |
410 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC mode\n"); | |
411 hdlc_tx_init(&t->hdlc_tx, FALSE, 2, TRUE, hdlc_underflow_handler, s); | |
412 get_bit_func = (get_bit_func_t) hdlc_tx_get_bit; | |
413 get_bit_user_data = (void *) &t->hdlc_tx; | |
414 } | |
415 else | |
416 { | |
417 span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM mode\n"); | |
418 get_bit_func = t38_non_ecm_buffer_get_bit; | |
419 get_bit_user_data = (void *) &s->core.non_ecm_to_modem; | |
420 } | |
421 /*endif*/ | |
422 switch (indicator) | |
423 { | |
424 case T38_IND_NO_SIGNAL: | |
425 t->tx_bit_rate = 0; | |
426 /* Impose 75ms minimum on transmitted silence */ | |
427 //silence_gen_set(&t->silence_gen, ms_to_samples(75)); | |
428 set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); | |
429 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
430 set_rx_active(s, TRUE); | |
431 break; | |
432 case T38_IND_CNG: | |
433 t->tx_bit_rate = 0; | |
434 modem_connect_tones_tx_init(&t->connect_tx, MODEM_CONNECT_TONES_FAX_CNG); | |
435 set_tx_handler(s, (span_tx_handler_t *) &modem_connect_tones_tx, &t->connect_tx); | |
436 silence_gen_set(&t->silence_gen, 0); | |
437 set_next_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); | |
438 set_rx_active(s, TRUE); | |
439 break; | |
440 case T38_IND_CED: | |
441 t->tx_bit_rate = 0; | |
442 modem_connect_tones_tx_init(&t->connect_tx, MODEM_CONNECT_TONES_FAX_CED); | |
443 set_tx_handler(s, (span_tx_handler_t *) &modem_connect_tones_tx, &t->connect_tx); | |
444 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
445 set_rx_active(s, TRUE); | |
446 break; | |
447 case T38_IND_V21_PREAMBLE: | |
448 t->tx_bit_rate = 300; | |
449 hdlc_tx_init(&t->hdlc_tx, FALSE, 2, TRUE, hdlc_underflow_handler, s); | |
450 hdlc_tx_flags(&t->hdlc_tx, 32); | |
451 silence_gen_alter(&t->silence_gen, ms_to_samples(75)); | |
452 u->buf[u->in].len = 0; | |
453 fsk_tx_init(&t->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &t->hdlc_tx); | |
454 set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); | |
455 set_next_tx_handler(s, (span_tx_handler_t *) &fsk_tx, &t->v21_tx); | |
456 set_rx_active(s, TRUE); | |
457 break; | |
458 case T38_IND_V27TER_2400_TRAINING: | |
459 case T38_IND_V27TER_4800_TRAINING: | |
460 switch (indicator) | |
461 { | |
462 case T38_IND_V27TER_2400_TRAINING: | |
463 t->tx_bit_rate = 2400; | |
464 break; | |
465 case T38_IND_V27TER_4800_TRAINING: | |
466 t->tx_bit_rate = 2400; | |
467 break; | |
468 } | |
469 /*endswitch*/ | |
470 silence_gen_alter(&t->silence_gen, ms_to_samples(75)); | |
471 v27ter_tx_restart(&t->v27ter_tx, t->tx_bit_rate, t->use_tep); | |
472 v27ter_tx_set_get_bit(&t->v27ter_tx, get_bit_func, get_bit_user_data); | |
473 set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); | |
474 set_next_tx_handler(s, (span_tx_handler_t *) &v27ter_tx, &t->v27ter_tx); | |
475 set_rx_active(s, TRUE); | |
476 break; | |
477 case T38_IND_V29_7200_TRAINING: | |
478 case T38_IND_V29_9600_TRAINING: | |
479 switch (indicator) | |
480 { | |
481 case T38_IND_V29_7200_TRAINING: | |
482 t->tx_bit_rate = 7200; | |
483 break; | |
484 case T38_IND_V29_9600_TRAINING: | |
485 t->tx_bit_rate = 9600; | |
486 break; | |
487 } | |
488 /*endswitch*/ | |
489 silence_gen_alter(&t->silence_gen, ms_to_samples(75)); | |
490 v29_tx_restart(&t->v29_tx, t->tx_bit_rate, t->use_tep); | |
491 v29_tx_set_get_bit(&t->v29_tx, get_bit_func, get_bit_user_data); | |
492 set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); | |
493 set_next_tx_handler(s, (span_tx_handler_t *) &v29_tx, &t->v29_tx); | |
494 set_rx_active(s, TRUE); | |
495 break; | |
496 case T38_IND_V17_7200_SHORT_TRAINING: | |
497 case T38_IND_V17_7200_LONG_TRAINING: | |
498 case T38_IND_V17_9600_SHORT_TRAINING: | |
499 case T38_IND_V17_9600_LONG_TRAINING: | |
500 case T38_IND_V17_12000_SHORT_TRAINING: | |
501 case T38_IND_V17_12000_LONG_TRAINING: | |
502 case T38_IND_V17_14400_SHORT_TRAINING: | |
503 case T38_IND_V17_14400_LONG_TRAINING: | |
504 short_train = FALSE; | |
505 switch (indicator) | |
506 { | |
507 case T38_IND_V17_7200_SHORT_TRAINING: | |
508 short_train = TRUE; | |
509 t->tx_bit_rate = 7200; | |
510 break; | |
511 case T38_IND_V17_7200_LONG_TRAINING: | |
512 t->tx_bit_rate = 7200; | |
513 break; | |
514 case T38_IND_V17_9600_SHORT_TRAINING: | |
515 short_train = TRUE; | |
516 t->tx_bit_rate = 9600; | |
517 break; | |
518 case T38_IND_V17_9600_LONG_TRAINING: | |
519 t->tx_bit_rate = 9600; | |
520 break; | |
521 case T38_IND_V17_12000_SHORT_TRAINING: | |
522 short_train = TRUE; | |
523 t->tx_bit_rate = 12000; | |
524 break; | |
525 case T38_IND_V17_12000_LONG_TRAINING: | |
526 t->tx_bit_rate = 12000; | |
527 break; | |
528 case T38_IND_V17_14400_SHORT_TRAINING: | |
529 short_train = TRUE; | |
530 t->tx_bit_rate = 14400; | |
531 break; | |
532 case T38_IND_V17_14400_LONG_TRAINING: | |
533 t->tx_bit_rate = 14400; | |
534 break; | |
535 } | |
536 /*endswitch*/ | |
537 silence_gen_alter(&t->silence_gen, ms_to_samples(75)); | |
538 v17_tx_restart(&t->v17_tx, t->tx_bit_rate, t->use_tep, short_train); | |
539 v17_tx_set_get_bit(&t->v17_tx, get_bit_func, get_bit_user_data); | |
540 set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); | |
541 set_next_tx_handler(s, (span_tx_handler_t *) &v17_tx, &t->v17_tx); | |
542 set_rx_active(s, TRUE); | |
543 break; | |
544 case T38_IND_V8_ANSAM: | |
545 t->tx_bit_rate = 300; | |
546 break; | |
547 case T38_IND_V8_SIGNAL: | |
548 t->tx_bit_rate = 300; | |
549 break; | |
550 case T38_IND_V34_CNTL_CHANNEL_1200: | |
551 t->tx_bit_rate = 1200; | |
552 break; | |
553 case T38_IND_V34_PRI_CHANNEL: | |
554 t->tx_bit_rate = 33600; | |
555 break; | |
556 case T38_IND_V34_CC_RETRAIN: | |
557 t->tx_bit_rate = 0; | |
558 break; | |
559 case T38_IND_V33_12000_TRAINING: | |
560 t->tx_bit_rate = 12000; | |
561 break; | |
562 case T38_IND_V33_14400_TRAINING: | |
563 t->tx_bit_rate = 14400; | |
564 break; | |
565 default: | |
566 break; | |
567 } | |
568 /*endswitch*/ | |
569 /* For any fast modem, set 200ms of preamble flags */ | |
570 if (t->tx_bit_rate > 300) | |
571 hdlc_tx_flags(&t->hdlc_tx, t->tx_bit_rate/(8*5)); | |
572 /*endif*/ | |
573 s->t38x.in_progress_rx_indicator = indicator; | |
574 return TRUE; | |
575 } | |
576 /*- End of function --------------------------------------------------------*/ | |
577 | |
578 static void finalise_hdlc_frame(t38_gateway_state_t *s, int good_fcs) | |
579 { | |
580 t38_gateway_hdlc_buf_t *hdlc_buf; | |
581 | |
582 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
583 if (!good_fcs || (hdlc_buf->flags & HDLC_FLAG_MISSING_DATA)) | |
584 hdlc_buf->flags |= HDLC_FLAG_CORRUPT_CRC; | |
585 /*endif*/ | |
586 if (s->core.hdlc_to_modem.in == s->core.hdlc_to_modem.out) | |
587 { | |
588 /* This is the frame in progress at the output. */ | |
589 if ((hdlc_buf->flags & HDLC_FLAG_PROCEED_WITH_OUTPUT) == 0) | |
590 { | |
591 /* Output of this frame has not yet begun. Throw it all out now. */ | |
592 hdlc_tx_frame(&s->audio.modems.hdlc_tx, hdlc_buf->buf, hdlc_buf->len); | |
593 } | |
594 /*endif*/ | |
595 if ((hdlc_buf->flags & HDLC_FLAG_CORRUPT_CRC)) | |
596 hdlc_tx_corrupt_frame(&s->audio.modems.hdlc_tx); | |
597 /*endif*/ | |
598 } | |
599 /*endif*/ | |
600 hdlc_buf->flags |= (HDLC_FLAG_PROCEED_WITH_OUTPUT | HDLC_FLAG_FINISHED); | |
601 if (++s->core.hdlc_to_modem.in >= T38_TX_HDLC_BUFS) | |
602 s->core.hdlc_to_modem.in = 0; | |
603 /*endif*/ | |
604 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
605 hdlc_buf->len = 0; | |
606 hdlc_buf->flags = 0; | |
607 hdlc_buf->contents = 0; | |
608 } | |
609 /*- End of function --------------------------------------------------------*/ | |
610 | |
611 static void edit_control_messages(t38_gateway_state_t *s, int from_modem, uint8_t *buf, int len) | |
612 { | |
613 /* Frames need to be fed to this routine byte by byte as they arrive. It basically just | |
614 edits the last byte received, based on the frame up to that point. */ | |
615 if (s->t38x.corrupt_current_frame[from_modem]) | |
616 { | |
617 /* We simply need to overwrite a section of the message, so it is not recognisable at | |
618 the receiver. This is used for the NSF, NSC, and NSS messages. Several strategies are | |
619 possible for the replacement data. If you have a manufacturer code of your own, the | |
620 sane thing is to overwrite the original data with that. */ | |
621 if (len <= s->t38x.suppress_nsx_len[from_modem]) | |
622 buf[len - 1] = nsx_overwrite[from_modem][len - 4]; | |
623 /*endif*/ | |
624 return; | |
625 } | |
626 /*endif*/ | |
627 /* Edit the message, if we need to control the communication between the end points. */ | |
628 switch (len) | |
629 { | |
630 case 3: | |
631 switch (buf[2]) | |
632 { | |
633 case T30_NSF: | |
634 case T30_NSC: | |
635 case T30_NSS: | |
636 if (s->t38x.suppress_nsx_len[from_modem]) | |
637 { | |
638 /* Corrupt the message, so it will be ignored by the far end. If it were | |
639 processed, 2 machines which recognise each other might do special things | |
640 we cannot handle as a middle man. */ | |
641 span_log(&s->logging, SPAN_LOG_FLOW, "Corrupting %s message to prevent recognition\n", t30_frametype(buf[2])); | |
642 s->t38x.corrupt_current_frame[from_modem] = TRUE; | |
643 } | |
644 /*endif*/ | |
645 break; | |
646 } | |
647 /*endswitch*/ | |
648 break; | |
649 case 4: | |
650 switch (buf[2]) | |
651 { | |
652 case T30_DIS: | |
653 /* Make sure the V.8 capability doesn't pass through. If it | |
654 did then two V.34 capable FAX machines might start some | |
655 V.8 re-negotiation. */ | |
656 buf[3] &= ~DISBIT6; | |
657 break; | |
658 } | |
659 /*endswitch*/ | |
660 break; | |
661 case 5: | |
662 switch (buf[2]) | |
663 { | |
664 case T30_DIS: | |
665 /* We may need to adjust the capabilities, so they do not exceed our own */ | |
666 span_log(&s->logging, SPAN_LOG_FLOW, "Applying fast modem type constraints.\n"); | |
667 switch (buf[4] & (DISBIT6 | DISBIT5 | DISBIT4 | DISBIT3)) | |
668 { | |
669 case 0: | |
670 case DISBIT4: | |
671 /* V.27ter only */ | |
672 break; | |
673 case DISBIT3: | |
674 case (DISBIT4 | DISBIT3): | |
675 /* V.27ter and V.29 */ | |
676 if (!(s->core.supported_modems & T30_SUPPORT_V29)) | |
677 buf[4] &= ~DISBIT3; | |
678 /*endif*/ | |
679 break; | |
680 case (DISBIT6 | DISBIT4 | DISBIT3): | |
681 /* V.27ter, V.29 and V.17 */ | |
682 if (!(s->core.supported_modems & T30_SUPPORT_V17)) | |
683 buf[4] &= ~DISBIT6; | |
684 /*endif*/ | |
685 if (!(s->core.supported_modems & T30_SUPPORT_V29)) | |
686 buf[4] &= ~DISBIT3; | |
687 /*endif*/ | |
688 break; | |
689 case (DISBIT5 | DISBIT4): | |
690 case (DISBIT6 | DISBIT4): | |
691 case (DISBIT6 | DISBIT5 | DISBIT4): | |
692 case (DISBIT6 | DISBIT5 | DISBIT4 | DISBIT3): | |
693 /* Reserved */ | |
694 buf[4] &= ~(DISBIT6 | DISBIT5); | |
695 buf[4] |= (DISBIT4 | DISBIT3); | |
696 break; | |
697 default: | |
698 /* Not used */ | |
699 buf[4] &= ~(DISBIT6 | DISBIT5); | |
700 buf[4] |= (DISBIT4 | DISBIT3); | |
701 break; | |
702 } | |
703 /*endswitch*/ | |
704 break; | |
705 } | |
706 /*endswitch*/ | |
707 break; | |
708 case 7: | |
709 switch (buf[2]) | |
710 { | |
711 case T30_DIS: | |
712 if (!s->core.ecm_allowed) | |
713 { | |
714 /* Do not allow ECM or T.6 coding */ | |
715 span_log(&s->logging, SPAN_LOG_FLOW, "Inhibiting ECM\n"); | |
716 buf[6] &= ~(DISBIT3 | DISBIT7); | |
717 } | |
718 /*endif*/ | |
719 break; | |
720 } | |
721 /*endswitch*/ | |
722 break; | |
723 } | |
724 /*endswitch*/ | |
725 } | |
726 /*- End of function --------------------------------------------------------*/ | |
727 | |
728 static void monitor_control_messages(t38_gateway_state_t *s, | |
729 int from_modem, | |
730 const uint8_t *buf, | |
731 int len) | |
732 { | |
733 static const struct | |
734 { | |
735 int bit_rate; | |
736 int modem_type; | |
737 uint8_t dcs_code; | |
738 } modem_codes[] = | |
739 { | |
740 {14400, T38_V17_RX, DISBIT6}, | |
741 {12000, T38_V17_RX, (DISBIT6 | DISBIT4)}, | |
742 { 9600, T38_V17_RX, (DISBIT6 | DISBIT3)}, | |
743 { 9600, T38_V29_RX, DISBIT3}, | |
744 { 7200, T38_V17_RX, (DISBIT6 | DISBIT4 | DISBIT3)}, | |
745 { 7200, T38_V29_RX, (DISBIT4 | DISBIT3)}, | |
746 { 4800, T38_V27TER_RX, DISBIT4}, | |
747 { 2400, T38_V27TER_RX, 0}, | |
748 { 0, T38_NONE, 0} | |
749 }; | |
750 static const int minimum_scan_line_times[8] = | |
751 { | |
752 20, | |
753 5, | |
754 10, | |
755 0, | |
756 40, | |
757 0, | |
758 0, | |
759 0 | |
760 }; | |
761 int dcs_code; | |
762 int i; | |
763 int j; | |
764 | |
765 /* Monitor the control messages, at the point where we have the whole message, so we can | |
766 see what is happening to things like training success/failure. */ | |
767 span_log(&s->logging, SPAN_LOG_FLOW, "Monitoring %s\n", t30_frametype(buf[2])); | |
768 if (len < 3) | |
769 return; | |
770 /*endif*/ | |
771 s->core.timed_mode = TIMED_MODE_IDLE; | |
772 switch (buf[2]) | |
773 { | |
774 case T30_CFR: | |
775 /* We are changing from TCF exchange to image exchange */ | |
776 /* Successful training means we should change to short training */ | |
777 s->core.image_data_mode = TRUE; | |
778 s->core.short_train = TRUE; | |
779 span_log(&s->logging, SPAN_LOG_FLOW, "CFR - short train = %d, ECM = %d\n", s->core.short_train, s->core.ecm_mode); | |
780 if (!from_modem) | |
781 restart_rx_modem(s); | |
782 /*endif*/ | |
783 break; | |
784 case T30_RTN: | |
785 case T30_RTP: | |
786 /* We are going back to the exchange of fresh TCF */ | |
787 s->core.image_data_mode = FALSE; | |
788 s->core.short_train = FALSE; | |
789 break; | |
790 case T30_CTR: | |
791 /* T.30 says the first image data after this does full training, yet does not | |
792 return to TCF. This seems to be the sole case of long training for image | |
793 data. */ | |
794 s->core.short_train = FALSE; | |
795 break; | |
796 case T30_DTC: | |
797 case T30_DCS: | |
798 case T30_DCS | 1: | |
799 /* We need to check which modem type is about to be used, so we can start the | |
800 correct modem. */ | |
801 s->core.fast_bit_rate = 0; | |
802 s->core.fast_rx_modem = T38_NONE; | |
803 s->core.image_data_mode = FALSE; | |
804 s->core.short_train = FALSE; | |
805 if (from_modem) | |
806 s->core.timed_mode = TIMED_MODE_TCF_PREDICTABLE_MODEM_START_BEGIN; | |
807 /*endif*/ | |
808 if (len >= 5) | |
809 { | |
810 /* The table is short, and not searched often, so a brain-dead linear scan seems OK */ | |
811 dcs_code = buf[4] & (DISBIT6 | DISBIT5 | DISBIT4 | DISBIT3); | |
812 for (i = 0; modem_codes[i].bit_rate; i++) | |
813 { | |
814 if (modem_codes[i].dcs_code == dcs_code) | |
815 break; | |
816 /*endif*/ | |
817 } | |
818 /*endfor*/ | |
819 /* If we are processing a message from the modem side, the contents determine the fast receive modem. | |
820 we are to use. If it comes from the T.38 side the contents do not. */ | |
821 s->core.fast_bit_rate = modem_codes[i].bit_rate; | |
822 if (from_modem) | |
823 s->core.fast_rx_modem = modem_codes[i].modem_type; | |
824 /*endif*/ | |
825 } | |
826 /*endif*/ | |
827 if (len >= 6) | |
828 { | |
829 j = (buf[5] & (DISBIT7 | DISBIT6 | DISBIT5)) >> 4; | |
830 span_log(&s->logging, SPAN_LOG_FLOW, "Min bits test = 0x%X\n", buf[5]); | |
831 s->core.min_row_bits = (s->core.fast_bit_rate*minimum_scan_line_times[j])/1000; | |
832 } | |
833 else | |
834 { | |
835 s->core.min_row_bits = 0; | |
836 } | |
837 /*endif*/ | |
838 s->core.ecm_mode = (len >= 7) && (buf[6] & DISBIT3); | |
839 span_log(&s->logging, SPAN_LOG_FLOW, "Fast rx modem = %d/%d, ECM = %d, Min bits per row = %d\n", s->core.fast_rx_modem, s->core.fast_bit_rate, s->core.ecm_mode, s->core.min_row_bits); | |
840 break; | |
841 case T30_PPS: | |
842 case T30_PPS | 1: | |
843 switch (buf[3] & 0xFE) | |
844 { | |
845 case T30_EOP: | |
846 case T30_PRI_EOP: | |
847 case T30_EOM: | |
848 case T30_PRI_EOM: | |
849 case T30_EOS: | |
850 #if 0 | |
851 /* If we are hitting one of these conditions, it will take another DCS/DTC to select | |
852 the fast modem again, so abandon our idea of it. */ | |
853 s->core.fast_bit_rate = 0; | |
854 s->core.fast_rx_modem = T38_NONE; | |
855 s->core.image_data_mode = FALSE; | |
856 s->core.short_train = FALSE; | |
857 #endif | |
858 /* Fall through */ | |
859 case T30_MPS: | |
860 case T30_PRI_MPS: | |
861 s->core.count_page_on_mcf = TRUE; | |
862 break; | |
863 } | |
864 /*endswitch*/ | |
865 break; | |
866 case T30_EOP: | |
867 case T30_EOP | 1: | |
868 case T30_PRI_EOP: | |
869 case T30_PRI_EOP | 1: | |
870 case T30_EOM: | |
871 case T30_EOM | 1: | |
872 case T30_PRI_EOM: | |
873 case T30_PRI_EOM | 1: | |
874 case T30_EOS: | |
875 case T30_EOS | 1: | |
876 #if 0 | |
877 /* If we are hitting one of these conditions, it will take another DCS/DTC to select | |
878 the fast modem again, so abandon our idea of t. */ | |
879 s->core.fast_bit_rate = 0; | |
880 s->core.fast_rx_modem = T38_NONE; | |
881 s->core.image_data_mode = FALSE; | |
882 s->core.short_train = FALSE; | |
883 #endif | |
884 /* Fall through */ | |
885 case T30_MPS: | |
886 case T30_MPS | 1: | |
887 case T30_PRI_MPS: | |
888 case T30_PRI_MPS | 1: | |
889 s->core.count_page_on_mcf = TRUE; | |
890 break; | |
891 case T30_MCF: | |
892 case T30_MCF | 1: | |
893 if (s->core.count_page_on_mcf) | |
894 { | |
895 s->core.pages_confirmed++; | |
896 span_log(&s->logging, SPAN_LOG_FLOW, "Pages confirmed = %d\n", s->core.pages_confirmed); | |
897 s->core.count_page_on_mcf = FALSE; | |
898 } | |
899 /*endif*/ | |
900 break; | |
901 default: | |
902 break; | |
903 } | |
904 /*endswitch*/ | |
905 } | |
906 /*- End of function --------------------------------------------------------*/ | |
907 | |
908 static void queue_missing_indicator(t38_gateway_state_t *s, int data_type) | |
909 { | |
910 t38_core_state_t *t; | |
911 int expected; | |
912 int expected_alt; | |
913 | |
914 t = &s->t38x.t38; | |
915 expected = -1; | |
916 expected_alt = -1; | |
917 /* Missing packets might have lost us the indicator that should have put us in | |
918 the required mode of operation. It might be a bit late to fill in such a gap | |
919 now, but we should try. We may also want to force indicators into the queue, | |
920 such as when the data says 'end of signal'. */ | |
921 /* We have an expectation of whether long or short training should occur, but be | |
922 tolerant of either kind of indicator being present. */ | |
923 switch (data_type) | |
924 { | |
925 case T38_DATA_NONE: | |
926 expected = T38_IND_NO_SIGNAL; | |
927 break; | |
928 case T38_DATA_V21: | |
929 expected = T38_IND_V21_PREAMBLE; | |
930 break; | |
931 case T38_DATA_V27TER_2400: | |
932 expected = T38_IND_V27TER_2400_TRAINING; | |
933 break; | |
934 case T38_DATA_V27TER_4800: | |
935 expected = T38_IND_V27TER_4800_TRAINING; | |
936 break; | |
937 case T38_DATA_V29_7200: | |
938 expected = T38_IND_V29_7200_TRAINING; | |
939 break; | |
940 case T38_DATA_V29_9600: | |
941 expected = T38_IND_V29_9600_TRAINING; | |
942 break; | |
943 case T38_DATA_V17_7200: | |
944 expected = (s->core.short_train) ? T38_IND_V17_7200_SHORT_TRAINING : T38_IND_V17_7200_LONG_TRAINING; | |
945 expected_alt = (s->core.short_train) ? T38_IND_V17_7200_LONG_TRAINING : T38_IND_V17_7200_SHORT_TRAINING; | |
946 break; | |
947 case T38_DATA_V17_9600: | |
948 expected = (s->core.short_train) ? T38_IND_V17_9600_SHORT_TRAINING : T38_IND_V17_9600_LONG_TRAINING; | |
949 expected_alt = (s->core.short_train) ? T38_IND_V17_9600_LONG_TRAINING : T38_IND_V17_9600_SHORT_TRAINING; | |
950 break; | |
951 case T38_DATA_V17_12000: | |
952 expected = (s->core.short_train) ? T38_IND_V17_12000_SHORT_TRAINING : T38_IND_V17_12000_LONG_TRAINING; | |
953 expected_alt = (s->core.short_train) ? T38_IND_V17_12000_LONG_TRAINING : T38_IND_V17_12000_SHORT_TRAINING; | |
954 break; | |
955 case T38_DATA_V17_14400: | |
956 expected = (s->core.short_train) ? T38_IND_V17_14400_SHORT_TRAINING : T38_IND_V17_14400_LONG_TRAINING; | |
957 expected_alt = (s->core.short_train) ? T38_IND_V17_14400_LONG_TRAINING : T38_IND_V17_14400_SHORT_TRAINING; | |
958 break; | |
959 case T38_DATA_V8: | |
960 break; | |
961 case T38_DATA_V34_PRI_RATE: | |
962 break; | |
963 case T38_DATA_V34_CC_1200: | |
964 break; | |
965 case T38_DATA_V34_PRI_CH: | |
966 break; | |
967 case T38_DATA_V33_12000: | |
968 break; | |
969 case T38_DATA_V33_14400: | |
970 break; | |
971 } | |
972 /*endswitch*/ | |
973 if (expected < 0) | |
974 return; | |
975 if (t->current_rx_indicator == expected) | |
976 return; | |
977 if (expected_alt >= 0 && t->current_rx_indicator == expected_alt) | |
978 return; | |
979 span_log(&s->logging, | |
980 SPAN_LOG_FLOW, | |
981 "Queuing missing indicator - %s\n", | |
982 t38_indicator_to_str(expected)); | |
983 process_rx_indicator(t, (void *) s, expected); | |
984 /* Force the indicator setting here, as the core won't set in when its missing. */ | |
985 t->current_rx_indicator = expected; | |
986 } | |
987 /*- End of function --------------------------------------------------------*/ | |
988 | |
989 static int process_rx_missing(t38_core_state_t *t, void *user_data, int rx_seq_no, int expected_seq_no) | |
990 { | |
991 t38_gateway_state_t *s; | |
992 | |
993 s = (t38_gateway_state_t *) user_data; | |
994 s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in].flags |= HDLC_FLAG_MISSING_DATA; | |
995 return 0; | |
996 } | |
997 /*- End of function --------------------------------------------------------*/ | |
998 | |
999 static int process_rx_indicator(t38_core_state_t *t, void *user_data, int indicator) | |
1000 { | |
1001 t38_gateway_state_t *s; | |
1002 | |
1003 s = (t38_gateway_state_t *) user_data; | |
1004 | |
1005 t38_non_ecm_buffer_report_input_status(&s->core.non_ecm_to_modem, &s->logging); | |
1006 if (t->current_rx_indicator == indicator) | |
1007 { | |
1008 /* This is probably due to the far end repeating itself. Ignore it. Its harmless */ | |
1009 return 0; | |
1010 } | |
1011 /*endif*/ | |
1012 if (s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in].contents) | |
1013 { | |
1014 if (++s->core.hdlc_to_modem.in >= T38_TX_HDLC_BUFS) | |
1015 s->core.hdlc_to_modem.in = 0; | |
1016 /*endif*/ | |
1017 } | |
1018 /*endif*/ | |
1019 s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in].contents = (indicator | FLAG_INDICATOR); | |
1020 if (++s->core.hdlc_to_modem.in >= T38_TX_HDLC_BUFS) | |
1021 s->core.hdlc_to_modem.in = 0; | |
1022 /*endif*/ | |
1023 t38_non_ecm_buffer_set_mode(&s->core.non_ecm_to_modem, s->core.image_data_mode, s->core.min_row_bits); | |
1024 | |
1025 span_log(&s->logging, | |
1026 SPAN_LOG_FLOW, | |
1027 "Queued change - (%d) %s -> %s\n", | |
1028 silence_gen_remainder(&(s->audio.modems.silence_gen)), | |
1029 t38_indicator_to_str(t->current_rx_indicator), | |
1030 t38_indicator_to_str(indicator)); | |
1031 s->t38x.current_rx_field_class = T38_FIELD_CLASS_NONE; | |
1032 /* We need to set this here, since we might have been called as a fake | |
1033 indication when the real one was missing */ | |
1034 t->current_rx_indicator = indicator; | |
1035 return 0; | |
1036 } | |
1037 /*- End of function --------------------------------------------------------*/ | |
1038 | |
1039 static int process_rx_data(t38_core_state_t *t, void *user_data, int data_type, int field_type, const uint8_t *buf, int len) | |
1040 { | |
1041 int i; | |
1042 t38_gateway_state_t *s; | |
1043 t38_gateway_t38_state_t *xx; | |
1044 t38_gateway_hdlc_buf_t *hdlc_buf; | |
1045 | |
1046 s = (t38_gateway_state_t *) user_data; | |
1047 xx = &s->t38x; | |
1048 /* There are a couple of special cases of data type that need their own treatment. */ | |
1049 switch (data_type) | |
1050 { | |
1051 case T38_DATA_V8: | |
1052 switch (field_type) | |
1053 { | |
1054 case T38_FIELD_CM_MESSAGE: | |
1055 if (len >= 1) | |
1056 span_log(&s->logging, SPAN_LOG_FLOW, "CM profile %d - %s\n", buf[0] - '0', t38_cm_profile_to_str(buf[0])); | |
1057 else | |
1058 span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for CM message - %d\n", len); | |
1059 /*endif*/ | |
1060 break; | |
1061 case T38_FIELD_JM_MESSAGE: | |
1062 if (len >= 2) | |
1063 span_log(&s->logging, SPAN_LOG_FLOW, "JM - %s\n", t38_jm_to_str(buf, len)); | |
1064 else | |
1065 span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for JM message - %d\n", len); | |
1066 /*endif*/ | |
1067 break; | |
1068 case T38_FIELD_CI_MESSAGE: | |
1069 if (len >= 1) | |
1070 span_log(&s->logging, SPAN_LOG_FLOW, "CI 0x%X\n", buf[0]); | |
1071 else | |
1072 span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for CI message - %d\n", len); | |
1073 /*endif*/ | |
1074 break; | |
1075 default: | |
1076 break; | |
1077 } | |
1078 /*endswitch*/ | |
1079 return 0; | |
1080 case T38_DATA_V34_PRI_RATE: | |
1081 switch (field_type) | |
1082 { | |
1083 case T38_FIELD_V34RATE: | |
1084 if (len >= 3) | |
1085 { | |
1086 xx->t38.v34_rate = t38_v34rate_to_bps(buf, len); | |
1087 span_log(&s->logging, SPAN_LOG_FLOW, "V.34 rate %d bps\n", xx->t38.v34_rate); | |
1088 } | |
1089 else | |
1090 { | |
1091 span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for V34rate message - %d\n", len); | |
1092 } | |
1093 /*endif*/ | |
1094 break; | |
1095 default: | |
1096 break; | |
1097 } | |
1098 /*endswitch*/ | |
1099 return 0; | |
1100 default: | |
1101 break; | |
1102 } | |
1103 /*endswitch*/ | |
1104 switch (field_type) | |
1105 { | |
1106 case T38_FIELD_HDLC_DATA: | |
1107 xx->current_rx_field_class = T38_FIELD_CLASS_HDLC; | |
1108 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1109 if (hdlc_buf->contents != (data_type | FLAG_DATA)) | |
1110 { | |
1111 queue_missing_indicator(s, data_type); | |
1112 /* All real HDLC messages in the FAX world start with 0xFF. If this one is not starting | |
1113 with 0xFF it would appear some octets must have been missed before this one. */ | |
1114 if (len <= 0 || buf[0] != 0xFF) | |
1115 s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in].flags |= HDLC_FLAG_MISSING_DATA; | |
1116 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1117 } | |
1118 /*endif*/ | |
1119 /* Check if this data would overflow the buffer. */ | |
1120 if (len <= 0 || hdlc_buf->len + len > T38_MAX_HDLC_LEN) | |
1121 break; | |
1122 /*endif*/ | |
1123 hdlc_buf->contents = (data_type | FLAG_DATA); | |
1124 bit_reverse(&hdlc_buf->buf[hdlc_buf->len], buf, len); | |
1125 /* We need to send out the control messages as they are arriving. They are | |
1126 too slow to capture a whole frame before starting to pass it on. | |
1127 For the faster frames, take in the whole frame before sending it out. Also, there | |
1128 is no need to monitor, or modify, the contents of the faster frames. */ | |
1129 if (data_type == T38_DATA_V21) | |
1130 { | |
1131 for (i = 1; i <= len; i++) | |
1132 edit_control_messages(s, 0, hdlc_buf->buf, hdlc_buf->len + i); | |
1133 /*endfor*/ | |
1134 /* Don't start pumping data into the actual output stream until there is | |
1135 enough backlog to create some elasticity for jitter tolerance. */ | |
1136 if (hdlc_buf->len + len >= HDLC_START_BUFFER_LEVEL) | |
1137 { | |
1138 if (s->core.hdlc_to_modem.in == s->core.hdlc_to_modem.out) | |
1139 { | |
1140 /* Output is not running, so kick it into life. */ | |
1141 if ((hdlc_buf->flags & HDLC_FLAG_PROCEED_WITH_OUTPUT) == 0) | |
1142 hdlc_tx_frame(&s->audio.modems.hdlc_tx, hdlc_buf->buf, hdlc_buf->len + len); | |
1143 else | |
1144 hdlc_tx_frame(&s->audio.modems.hdlc_tx, hdlc_buf->buf + hdlc_buf->len, len); | |
1145 /*endif*/ | |
1146 } | |
1147 /*endif*/ | |
1148 hdlc_buf->flags |= HDLC_FLAG_PROCEED_WITH_OUTPUT; | |
1149 } | |
1150 /*endif*/ | |
1151 } | |
1152 /*endif*/ | |
1153 s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in].len += len; | |
1154 break; | |
1155 case T38_FIELD_HDLC_FCS_OK: | |
1156 xx->current_rx_field_class = T38_FIELD_CLASS_HDLC; | |
1157 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1158 if (len > 0) | |
1159 { | |
1160 span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_OK!\n"); | |
1161 /* The sender has incorrectly included data in this message. It is unclear what we should do | |
1162 with it, to maximise tolerance of buggy implementations. */ | |
1163 } | |
1164 /*endif*/ | |
1165 /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_OK messages, in IFP packets with | |
1166 incrementing sequence numbers, which are actually repeats. They get through to this point because | |
1167 of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */ | |
1168 if (t->current_rx_data_type != data_type || t->current_rx_field_type != field_type) | |
1169 { | |
1170 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s - CRC good\n", t30_frametype(hdlc_buf->buf[2])); | |
1171 if (hdlc_buf->contents != (data_type | FLAG_DATA)) | |
1172 { | |
1173 queue_missing_indicator(s, data_type); | |
1174 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1175 } | |
1176 /*endif*/ | |
1177 if (data_type == T38_DATA_V21) | |
1178 { | |
1179 if ((hdlc_buf->flags & HDLC_FLAG_MISSING_DATA) == 0) | |
1180 { | |
1181 monitor_control_messages(s, FALSE, hdlc_buf->buf, hdlc_buf->len); | |
1182 if (s->core.real_time_frame_handler) | |
1183 s->core.real_time_frame_handler(s, s->core.real_time_frame_user_data, FALSE, hdlc_buf->buf, hdlc_buf->len); | |
1184 /*endif*/ | |
1185 } | |
1186 /*endif*/ | |
1187 } | |
1188 else | |
1189 { | |
1190 /* Make sure we go back to short training if CTC/CTR has kicked us into | |
1191 long training. There has to be more than one value HDLC frame in a | |
1192 chunk of image data, so just setting short training mode here should | |
1193 be enough. */ | |
1194 s->core.short_train = TRUE; | |
1195 } | |
1196 /*endif*/ | |
1197 hdlc_buf->contents = (data_type | FLAG_DATA); | |
1198 finalise_hdlc_frame(s, TRUE); | |
1199 } | |
1200 /*endif*/ | |
1201 xx->corrupt_current_frame[0] = FALSE; | |
1202 break; | |
1203 case T38_FIELD_HDLC_FCS_BAD: | |
1204 xx->current_rx_field_class = T38_FIELD_CLASS_HDLC; | |
1205 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1206 if (len > 0) | |
1207 { | |
1208 span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_BAD!\n"); | |
1209 /* The sender has incorrectly included data in this message. We can safely ignore it, as the | |
1210 bad FCS means we will throw away the whole message, anyway. */ | |
1211 } | |
1212 /*endif*/ | |
1213 /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_BAD messages, in IFP packets with | |
1214 incrementing sequence numbers, which are actually repeats. They get through to this point because | |
1215 of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */ | |
1216 if (t->current_rx_data_type != data_type || t->current_rx_field_type != field_type) | |
1217 { | |
1218 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s - CRC bad\n", t30_frametype(hdlc_buf->buf[2])); | |
1219 /* Only bother with frames that have a bad CRC, if they also have some content. */ | |
1220 if (hdlc_buf->len > 0) | |
1221 { | |
1222 if (hdlc_buf->contents != (data_type | FLAG_DATA)) | |
1223 { | |
1224 queue_missing_indicator(s, data_type); | |
1225 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1226 } | |
1227 /*endif*/ | |
1228 hdlc_buf->contents = (data_type | FLAG_DATA); | |
1229 finalise_hdlc_frame(s, FALSE); | |
1230 } | |
1231 else | |
1232 { | |
1233 /* Just restart using the current frame buffer */ | |
1234 hdlc_buf->contents = 0; | |
1235 } | |
1236 /*endif*/ | |
1237 } | |
1238 /*endif*/ | |
1239 xx->corrupt_current_frame[0] = FALSE; | |
1240 break; | |
1241 case T38_FIELD_HDLC_FCS_OK_SIG_END: | |
1242 xx->current_rx_field_class = T38_FIELD_CLASS_HDLC; | |
1243 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1244 if (len > 0) | |
1245 { | |
1246 span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_OK_SIG_END!\n"); | |
1247 /* The sender has incorrectly included data in this message. It is unclear what we should do | |
1248 with it, to maximise tolerance of buggy implementations. */ | |
1249 } | |
1250 /*endif*/ | |
1251 /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_OK_SIG_END messages, in IFP packets with | |
1252 incrementing sequence numbers, which are actually repeats. They get through to this point because | |
1253 of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */ | |
1254 if (t->current_rx_data_type != data_type || t->current_rx_field_type != field_type) | |
1255 { | |
1256 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s - CRC OK, sig end\n", t30_frametype(hdlc_buf->buf[2])); | |
1257 if (hdlc_buf->contents != (data_type | FLAG_DATA)) | |
1258 { | |
1259 queue_missing_indicator(s, data_type); | |
1260 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1261 } | |
1262 /*endif*/ | |
1263 if (data_type == T38_DATA_V21) | |
1264 { | |
1265 if ((hdlc_buf->flags & HDLC_FLAG_MISSING_DATA) == 0) | |
1266 { | |
1267 monitor_control_messages(s, FALSE, hdlc_buf->buf, hdlc_buf->len); | |
1268 if (s->core.real_time_frame_handler) | |
1269 s->core.real_time_frame_handler(s, s->core.real_time_frame_user_data, FALSE, hdlc_buf->buf, hdlc_buf->len); | |
1270 /*endif*/ | |
1271 } | |
1272 /*endif*/ | |
1273 } | |
1274 else | |
1275 { | |
1276 /* Make sure we go back to short training if CTC/CTR has kicked us into | |
1277 long training. There has to be more than one value HDLC frame in a | |
1278 chunk of image data, so just setting short training mode here should | |
1279 be enough. */ | |
1280 s->core.short_train = TRUE; | |
1281 } | |
1282 /*endif*/ | |
1283 hdlc_buf->contents = (data_type | FLAG_DATA); | |
1284 finalise_hdlc_frame(s, TRUE); | |
1285 queue_missing_indicator(s, T38_DATA_NONE); | |
1286 xx->current_rx_field_class = T38_FIELD_CLASS_NONE; | |
1287 } | |
1288 /*endif*/ | |
1289 xx->corrupt_current_frame[0] = FALSE; | |
1290 break; | |
1291 case T38_FIELD_HDLC_FCS_BAD_SIG_END: | |
1292 xx->current_rx_field_class = T38_FIELD_CLASS_HDLC; | |
1293 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1294 if (len > 0) | |
1295 { | |
1296 span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_BAD_SIG_END!\n"); | |
1297 /* The sender has incorrectly included data in this message. We can safely ignore it, as the | |
1298 bad FCS means we will throw away the whole message, anyway. */ | |
1299 } | |
1300 /*endif*/ | |
1301 /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_BAD_SIG_END messages, in IFP packets with | |
1302 incrementing sequence numbers, which are actually repeats. They get through to this point because | |
1303 of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */ | |
1304 if (t->current_rx_data_type != data_type || t->current_rx_field_type != field_type) | |
1305 { | |
1306 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s - CRC bad, sig end\n", t30_frametype(hdlc_buf->buf[2])); | |
1307 if (hdlc_buf->contents != (data_type | FLAG_DATA)) | |
1308 { | |
1309 queue_missing_indicator(s, data_type); | |
1310 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1311 } | |
1312 /*endif*/ | |
1313 /* Only bother with frames that have a bad CRC, if they also have some content. */ | |
1314 if (hdlc_buf->len > 0) | |
1315 { | |
1316 hdlc_buf->contents = (data_type | FLAG_DATA); | |
1317 finalise_hdlc_frame(s, FALSE); | |
1318 } | |
1319 else | |
1320 { | |
1321 /* Just restart using the current frame buffer */ | |
1322 hdlc_buf->contents = 0; | |
1323 } | |
1324 /*endif*/ | |
1325 queue_missing_indicator(s, T38_DATA_NONE); | |
1326 xx->current_rx_field_class = T38_FIELD_CLASS_NONE; | |
1327 } | |
1328 /*endif*/ | |
1329 xx->corrupt_current_frame[0] = FALSE; | |
1330 break; | |
1331 case T38_FIELD_HDLC_SIG_END: | |
1332 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1333 if (len > 0) | |
1334 { | |
1335 span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_SIG_END!\n"); | |
1336 /* The sender has incorrectly included data in this message, but there seems nothing meaningful | |
1337 it could be. There could not be an FCS good/bad report beyond this. */ | |
1338 } | |
1339 /*endif*/ | |
1340 /* Some T.38 implementations send multiple T38_FIELD_HDLC_SIG_END messages, in IFP packets with | |
1341 incrementing sequence numbers, which are actually repeats. They get through to this point because | |
1342 of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */ | |
1343 if (t->current_rx_data_type != data_type || t->current_rx_field_type != field_type) | |
1344 { | |
1345 if (hdlc_buf->contents != (data_type | FLAG_DATA)) | |
1346 { | |
1347 queue_missing_indicator(s, data_type); | |
1348 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1349 } | |
1350 /* WORKAROUND: At least some Mediatrix boxes have a bug, where they can send this message at the | |
1351 end of non-ECM data. We need to tolerate this. */ | |
1352 if (xx->current_rx_field_class == T38_FIELD_CLASS_NON_ECM) | |
1353 { | |
1354 span_log(&s->logging, SPAN_LOG_WARNING, "T38_FIELD_HDLC_SIG_END received at the end of non-ECM data!\n"); | |
1355 /* Don't flow control the data any more. Just pump out the remainder as fast as we can. */ | |
1356 t38_non_ecm_buffer_push(&s->core.non_ecm_to_modem); | |
1357 } | |
1358 else | |
1359 { | |
1360 /* This message is expected under 2 circumstances. One is as an alternative to T38_FIELD_HDLC_FCS_OK_SIG_END - | |
1361 i.e. they send T38_FIELD_HDLC_FCS_OK, and then T38_FIELD_HDLC_SIG_END when the carrier actually drops. | |
1362 The other is because the HDLC signal drops unexpectedly - i.e. not just after a final frame. In | |
1363 this case we just clear out any partial frame data that might be in the buffer. */ | |
1364 /* TODO: what if any junk in the buffer has reached the HDLC_FLAG_PROCEED_WITH_OUTPUT stage? */ | |
1365 hdlc_buf->len = 0; | |
1366 hdlc_buf->flags = 0; | |
1367 hdlc_buf->contents = 0; | |
1368 } | |
1369 /*endif*/ | |
1370 queue_missing_indicator(s, T38_DATA_NONE); | |
1371 xx->current_rx_field_class = T38_FIELD_CLASS_NONE; | |
1372 } | |
1373 /*endif*/ | |
1374 xx->corrupt_current_frame[0] = FALSE; | |
1375 break; | |
1376 case T38_FIELD_T4_NON_ECM_DATA: | |
1377 xx->current_rx_field_class = T38_FIELD_CLASS_NON_ECM; | |
1378 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1379 if (hdlc_buf->contents != (data_type | FLAG_DATA)) | |
1380 { | |
1381 queue_missing_indicator(s, data_type); | |
1382 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1383 } | |
1384 if (len > 0) | |
1385 t38_non_ecm_buffer_inject(&s->core.non_ecm_to_modem, buf, len); | |
1386 xx->corrupt_current_frame[0] = FALSE; | |
1387 break; | |
1388 case T38_FIELD_T4_NON_ECM_SIG_END: | |
1389 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1390 /* Some T.38 implementations send multiple T38_FIELD_T4_NON_ECM_SIG_END messages, in IFP packets with | |
1391 incrementing sequence numbers, which are actually repeats. They get through to this point because | |
1392 of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */ | |
1393 if (t->current_rx_data_type != data_type || t->current_rx_field_type != field_type) | |
1394 { | |
1395 /* WORKAROUND: At least some Mediatrix boxes have a bug, where they can send HDLC signal end where | |
1396 they should send non-ECM signal end. It is possible they also do the opposite. | |
1397 We need to tolerate this. */ | |
1398 if (xx->current_rx_field_class == T38_FIELD_CLASS_NON_ECM) | |
1399 { | |
1400 if (len > 0) | |
1401 { | |
1402 if (hdlc_buf->contents != (data_type | FLAG_DATA)) | |
1403 { | |
1404 queue_missing_indicator(s, data_type); | |
1405 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1406 } | |
1407 /*endif*/ | |
1408 t38_non_ecm_buffer_inject(&s->core.non_ecm_to_modem, buf, len); | |
1409 } | |
1410 /*endif*/ | |
1411 if (hdlc_buf->contents != (data_type | FLAG_DATA)) | |
1412 { | |
1413 queue_missing_indicator(s, data_type); | |
1414 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1415 } | |
1416 /*endif*/ | |
1417 /* Don't flow control the data any more. Just pump out the remainder as fast as we can. */ | |
1418 t38_non_ecm_buffer_push(&s->core.non_ecm_to_modem); | |
1419 } | |
1420 else | |
1421 { | |
1422 span_log(&s->logging, SPAN_LOG_WARNING, "T38_FIELD_NON_ECM_SIG_END received at the end of HDLC data!\n"); | |
1423 if (s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in].contents != (data_type | FLAG_DATA)) | |
1424 { | |
1425 queue_missing_indicator(s, data_type); | |
1426 hdlc_buf = &s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in]; | |
1427 } | |
1428 /*endif*/ | |
1429 /* TODO: what if any junk in the buffer has reached the HDLC_FLAG_PROCEED_WITH_OUTPUT stage? */ | |
1430 hdlc_buf->len = 0; | |
1431 hdlc_buf->flags = 0; | |
1432 hdlc_buf->contents = 0; | |
1433 } | |
1434 /*endif*/ | |
1435 queue_missing_indicator(s, T38_DATA_NONE); | |
1436 xx->current_rx_field_class = T38_FIELD_CLASS_NONE; | |
1437 } | |
1438 /*endif*/ | |
1439 xx->corrupt_current_frame[0] = FALSE; | |
1440 break; | |
1441 default: | |
1442 break; | |
1443 } | |
1444 /*endswitch*/ | |
1445 | |
1446 #if 0 | |
1447 if (span_log_test(&s->logging, SPAN_LOG_FLOW)) | |
1448 { | |
1449 int i; | |
1450 | |
1451 if (len > 0) | |
1452 { | |
1453 span_log(&s->logging, SPAN_LOG_FLOW, "Data: "); | |
1454 for (i = 0; i < len; i++) | |
1455 span_log(&s->logging, SPAN_LOG_FLOW | SPAN_LOG_SUPPRESS_LABELLING, " %02X", buf[i]); | |
1456 /*endfor*/ | |
1457 } | |
1458 /*endif*/ | |
1459 } | |
1460 /*endif*/ | |
1461 span_log(&s->logging, SPAN_LOG_FLOW | SPAN_LOG_SUPPRESS_LABELLING, "\n"); | |
1462 #endif | |
1463 return 0; | |
1464 } | |
1465 /*- End of function --------------------------------------------------------*/ | |
1466 | |
1467 static void set_octets_per_data_packet(t38_gateway_state_t *s, int bit_rate) | |
1468 { | |
1469 int octets; | |
1470 | |
1471 octets = MS_PER_TX_CHUNK*bit_rate/(8*1000); | |
1472 if (octets < 1) | |
1473 octets = 1; | |
1474 /*endif*/ | |
1475 s->core.to_t38.octets_per_data_packet = octets; | |
1476 } | |
1477 /*- End of function --------------------------------------------------------*/ | |
1478 | |
1479 static int set_slow_packetisation(t38_gateway_state_t *s) | |
1480 { | |
1481 set_octets_per_data_packet(s, 300); | |
1482 s->t38x.current_tx_data_type = T38_DATA_V21; | |
1483 return T38_IND_V21_PREAMBLE; | |
1484 } | |
1485 /*- End of function --------------------------------------------------------*/ | |
1486 | |
1487 static int set_fast_packetisation(t38_gateway_state_t *s) | |
1488 { | |
1489 int ind; | |
1490 | |
1491 ind = T38_IND_NO_SIGNAL; | |
1492 switch (s->core.fast_rx_active) | |
1493 { | |
1494 case T38_V17_RX: | |
1495 set_octets_per_data_packet(s, s->core.fast_bit_rate); | |
1496 switch (s->core.fast_bit_rate) | |
1497 { | |
1498 case 7200: | |
1499 ind = (s->core.short_train) ? T38_IND_V17_7200_SHORT_TRAINING : T38_IND_V17_7200_LONG_TRAINING; | |
1500 s->t38x.current_tx_data_type = T38_DATA_V17_7200; | |
1501 break; | |
1502 case 9600: | |
1503 ind = (s->core.short_train) ? T38_IND_V17_9600_SHORT_TRAINING : T38_IND_V17_9600_LONG_TRAINING; | |
1504 s->t38x.current_tx_data_type = T38_DATA_V17_9600; | |
1505 break; | |
1506 case 12000: | |
1507 ind = (s->core.short_train) ? T38_IND_V17_12000_SHORT_TRAINING : T38_IND_V17_12000_LONG_TRAINING; | |
1508 s->t38x.current_tx_data_type = T38_DATA_V17_12000; | |
1509 break; | |
1510 default: | |
1511 case 14400: | |
1512 ind = (s->core.short_train) ? T38_IND_V17_14400_SHORT_TRAINING : T38_IND_V17_14400_LONG_TRAINING; | |
1513 s->t38x.current_tx_data_type = T38_DATA_V17_14400; | |
1514 break; | |
1515 } | |
1516 break; | |
1517 case T38_V27TER_RX: | |
1518 set_octets_per_data_packet(s, s->core.fast_bit_rate); | |
1519 switch (s->core.fast_bit_rate) | |
1520 { | |
1521 case 2400: | |
1522 ind = T38_IND_V27TER_2400_TRAINING; | |
1523 s->t38x.current_tx_data_type = T38_DATA_V27TER_2400; | |
1524 break; | |
1525 default: | |
1526 case 4800: | |
1527 ind = T38_IND_V27TER_4800_TRAINING; | |
1528 s->t38x.current_tx_data_type = T38_DATA_V27TER_4800; | |
1529 break; | |
1530 } | |
1531 break; | |
1532 case T38_V29_RX: | |
1533 set_octets_per_data_packet(s, s->core.fast_bit_rate); | |
1534 switch (s->core.fast_bit_rate) | |
1535 { | |
1536 case 7200: | |
1537 ind = T38_IND_V29_7200_TRAINING; | |
1538 s->t38x.current_tx_data_type = T38_DATA_V29_7200; | |
1539 break; | |
1540 default: | |
1541 case 9600: | |
1542 ind = T38_IND_V29_9600_TRAINING; | |
1543 s->t38x.current_tx_data_type = T38_DATA_V29_9600; | |
1544 break; | |
1545 } | |
1546 break; | |
1547 } | |
1548 return ind; | |
1549 } | |
1550 /*- End of function --------------------------------------------------------*/ | |
1551 | |
1552 static void announce_training(t38_gateway_state_t *s) | |
1553 { | |
1554 t38_core_send_indicator(&s->t38x.t38, set_fast_packetisation(s)); | |
1555 } | |
1556 /*- End of function --------------------------------------------------------*/ | |
1557 | |
1558 static void non_ecm_rx_status(void *user_data, int status) | |
1559 { | |
1560 t38_gateway_state_t *s; | |
1561 | |
1562 s = (t38_gateway_state_t *) user_data; | |
1563 span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM signal status is %s (%d)\n", signal_status_to_str(status), status); | |
1564 switch (status) | |
1565 { | |
1566 case SIG_STATUS_TRAINING_IN_PROGRESS: | |
1567 if (s->core.timed_mode == TIMED_MODE_IDLE) | |
1568 { | |
1569 announce_training(s); | |
1570 } | |
1571 else | |
1572 { | |
1573 if (s->core.timed_mode == TIMED_MODE_TCF_PREDICTABLE_MODEM_START_PAST_V21_MODEM) | |
1574 s->core.timed_mode = TIMED_MODE_TCF_PREDICTABLE_MODEM_START_FAST_MODEM_SEEN; | |
1575 else | |
1576 s->core.samples_to_timeout = ms_to_samples(500); | |
1577 set_fast_packetisation(s); | |
1578 } | |
1579 break; | |
1580 case SIG_STATUS_TRAINING_FAILED: | |
1581 break; | |
1582 case SIG_STATUS_TRAINING_SUCCEEDED: | |
1583 /* The modem is now trained */ | |
1584 s->audio.modems.rx_signal_present = TRUE; | |
1585 s->audio.modems.rx_trained = TRUE; | |
1586 s->core.timed_mode = TIMED_MODE_IDLE; | |
1587 s->core.samples_to_timeout = 0; | |
1588 to_t38_buffer_init(&s->core.to_t38); | |
1589 break; | |
1590 case SIG_STATUS_CARRIER_UP: | |
1591 break; | |
1592 case SIG_STATUS_CARRIER_DOWN: | |
1593 switch (s->t38x.current_tx_data_type) | |
1594 { | |
1595 case T38_DATA_V17_7200: | |
1596 case T38_DATA_V17_9600: | |
1597 case T38_DATA_V17_12000: | |
1598 case T38_DATA_V17_14400: | |
1599 case T38_DATA_V27TER_2400: | |
1600 case T38_DATA_V27TER_4800: | |
1601 case T38_DATA_V29_7200: | |
1602 case T38_DATA_V29_9600: | |
1603 if (s->core.timed_mode != TIMED_MODE_TCF_PREDICTABLE_MODEM_START_FAST_MODEM_ANNOUNCED) | |
1604 { | |
1605 /* TODO: If the carrier really did fall for good during the 500ms TEP blocking timeout, we | |
1606 won't declare the no-signal condition. */ | |
1607 non_ecm_push_residue(s); | |
1608 t38_core_send_indicator(&s->t38x.t38, T38_IND_NO_SIGNAL); | |
1609 } | |
1610 restart_rx_modem(s); | |
1611 break; | |
1612 } | |
1613 break; | |
1614 default: | |
1615 span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected non-ECM special bit - %d!\n", status); | |
1616 break; | |
1617 } | |
1618 } | |
1619 /*- End of function --------------------------------------------------------*/ | |
1620 | |
1621 static void to_t38_buffer_init(t38_gateway_to_t38_state_t *s) | |
1622 { | |
1623 s->data_ptr = 0; | |
1624 s->bit_stream = 0xFFFF; | |
1625 s->bit_no = 0; | |
1626 | |
1627 s->in_bits = 0; | |
1628 s->out_octets = 0; | |
1629 } | |
1630 /*- End of function --------------------------------------------------------*/ | |
1631 | |
1632 static void non_ecm_push_residue(t38_gateway_state_t *t) | |
1633 { | |
1634 t38_gateway_to_t38_state_t *s; | |
1635 | |
1636 s = &t->core.to_t38; | |
1637 if (s->bit_no) | |
1638 { | |
1639 /* There is a fractional octet in progress. We might as well send every last bit we can. */ | |
1640 s->data[s->data_ptr++] = (uint8_t) (s->bit_stream << (8 - s->bit_no)); | |
1641 } | |
1642 t38_core_send_data(&t->t38x.t38, t->t38x.current_tx_data_type, T38_FIELD_T4_NON_ECM_SIG_END, s->data, s->data_ptr, T38_PACKET_CATEGORY_IMAGE_DATA_END); | |
1643 s->in_bits += s->bits_absorbed; | |
1644 s->out_octets += s->data_ptr; | |
1645 s->data_ptr = 0; | |
1646 } | |
1647 /*- End of function --------------------------------------------------------*/ | |
1648 | |
1649 static void non_ecm_push(t38_gateway_state_t *t) | |
1650 { | |
1651 t38_gateway_to_t38_state_t *s; | |
1652 | |
1653 s = &t->core.to_t38; | |
1654 if (s->data_ptr) | |
1655 { | |
1656 t38_core_send_data(&t->t38x.t38, t->t38x.current_tx_data_type, T38_FIELD_T4_NON_ECM_DATA, s->data, s->data_ptr, T38_PACKET_CATEGORY_IMAGE_DATA); | |
1657 s->in_bits += s->bits_absorbed; | |
1658 s->out_octets += s->data_ptr; | |
1659 s->bits_absorbed = 0; | |
1660 s->data_ptr = 0; | |
1661 } | |
1662 /*endif*/ | |
1663 } | |
1664 /*- End of function --------------------------------------------------------*/ | |
1665 | |
1666 static void non_ecm_put_bit(void *user_data, int bit) | |
1667 { | |
1668 t38_gateway_state_t *t; | |
1669 t38_gateway_to_t38_state_t *s; | |
1670 | |
1671 if (bit < 0) | |
1672 { | |
1673 non_ecm_rx_status(user_data, bit); | |
1674 return; | |
1675 } | |
1676 t = (t38_gateway_state_t *) user_data; | |
1677 s = &t->core.to_t38; | |
1678 | |
1679 s->in_bits++; | |
1680 bit &= 1; | |
1681 s->bit_stream = (s->bit_stream << 1) | bit; | |
1682 if (++s->bit_no >= 8) | |
1683 { | |
1684 s->data[s->data_ptr++] = (uint8_t) s->bit_stream & 0xFF; | |
1685 if (s->data_ptr >= s->octets_per_data_packet) | |
1686 non_ecm_push(t); | |
1687 s->bit_no = 0; | |
1688 } | |
1689 } | |
1690 /*- End of function --------------------------------------------------------*/ | |
1691 | |
1692 static void non_ecm_remove_fill_and_put_bit(void *user_data, int bit) | |
1693 { | |
1694 t38_gateway_state_t *t; | |
1695 t38_gateway_to_t38_state_t *s; | |
1696 | |
1697 if (bit < 0) | |
1698 { | |
1699 non_ecm_rx_status(user_data, bit); | |
1700 return; | |
1701 } | |
1702 t = (t38_gateway_state_t *) user_data; | |
1703 s = &t->core.to_t38; | |
1704 | |
1705 s->bits_absorbed++; | |
1706 bit &= 1; | |
1707 /* Drop any extra zero bits when we already have enough for an EOL symbol. */ | |
1708 /* The snag here is that if we just look for 11 bits, a line ending with | |
1709 a code that has trailing zero bits will cause problems. The longest run of | |
1710 trailing zeros for any code is 3, so we need to look for at least 14 zeros | |
1711 if we don't want to actually analyse the compressed data in depth. This means | |
1712 we do not strip every fill bit, but we strip most of them. */ | |
1713 if ((s->bit_stream & 0x3FFF) == 0 && bit == 0) | |
1714 { | |
1715 if (s->bits_absorbed > 2*8*s->octets_per_data_packet) | |
1716 { | |
1717 /* We need to pump out what we have, even though we have not accumulated a full | |
1718 buffer of data. If we don't, we stand to delay rows excessively, so the far | |
1719 end gateway (assuming the far end is a gateway) cannot play them out. */ | |
1720 non_ecm_push(t); | |
1721 } | |
1722 return; | |
1723 } | |
1724 s->bit_stream = (s->bit_stream << 1) | bit; | |
1725 if (++s->bit_no >= 8) | |
1726 { | |
1727 s->data[s->data_ptr++] = (uint8_t) s->bit_stream & 0xFF; | |
1728 if (s->data_ptr >= s->octets_per_data_packet) | |
1729 non_ecm_push(t); | |
1730 s->bit_no = 0; | |
1731 } | |
1732 } | |
1733 /*- End of function --------------------------------------------------------*/ | |
1734 | |
1735 static void hdlc_rx_status(hdlc_rx_state_t *t, int status) | |
1736 { | |
1737 t38_gateway_state_t *s; | |
1738 int category; | |
1739 | |
1740 s = (t38_gateway_state_t *) t->frame_user_data; | |
1741 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC signal status is %s (%d)\n", signal_status_to_str(status), status); | |
1742 switch (status) | |
1743 { | |
1744 case SIG_STATUS_TRAINING_IN_PROGRESS: | |
1745 announce_training(s); | |
1746 break; | |
1747 case SIG_STATUS_TRAINING_FAILED: | |
1748 break; | |
1749 case SIG_STATUS_TRAINING_SUCCEEDED: | |
1750 /* The modem is now trained. */ | |
1751 s->audio.modems.rx_signal_present = TRUE; | |
1752 s->audio.modems.rx_trained = TRUE; | |
1753 /* Behave like HDLC preamble has been announced. */ | |
1754 t->framing_ok_announced = TRUE; | |
1755 to_t38_buffer_init(&s->core.to_t38); | |
1756 break; | |
1757 case SIG_STATUS_CARRIER_UP: | |
1758 /* Reset the HDLC receiver. */ | |
1759 t->raw_bit_stream = 0; | |
1760 t->len = 0; | |
1761 t->num_bits = 0; | |
1762 t->flags_seen = 0; | |
1763 t->framing_ok_announced = FALSE; | |
1764 to_t38_buffer_init(&s->core.to_t38); | |
1765 break; | |
1766 case SIG_STATUS_CARRIER_DOWN: | |
1767 if (t->framing_ok_announced) | |
1768 { | |
1769 category = (s->t38x.current_tx_data_type == T38_DATA_V21) ? T38_PACKET_CATEGORY_CONTROL_DATA_END : T38_PACKET_CATEGORY_IMAGE_DATA_END; | |
1770 t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_SIG_END, NULL, 0, category); | |
1771 t38_core_send_indicator(&s->t38x.t38, T38_IND_NO_SIGNAL); | |
1772 t->framing_ok_announced = FALSE; | |
1773 } | |
1774 restart_rx_modem(s); | |
1775 if (s->core.timed_mode == TIMED_MODE_TCF_PREDICTABLE_MODEM_START_BEGIN) | |
1776 { | |
1777 /* If we are doing TCF, we need to announce the fast carrier training very | |
1778 quickly, to ensure it starts 75+-20ms after the HDLC carrier ends. Waiting until | |
1779 it trains will be too late. We need to announce the fast modem a fixed time after | |
1780 the end of the V.21 carrier, in anticipation of its arrival. If we announce it, | |
1781 and it doesn't arrive, we will worry about that later. */ | |
1782 s->core.samples_to_timeout = ms_to_samples(75); | |
1783 s->core.timed_mode = TIMED_MODE_TCF_PREDICTABLE_MODEM_START_PAST_V21_MODEM; | |
1784 } | |
1785 break; | |
1786 default: | |
1787 span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected HDLC special bit - %d!\n", status); | |
1788 break; | |
1789 } | |
1790 } | |
1791 /*- End of function --------------------------------------------------------*/ | |
1792 | |
1793 static void rx_flag_or_abort(hdlc_rx_state_t *t) | |
1794 { | |
1795 t38_gateway_state_t *s; | |
1796 t38_gateway_to_t38_state_t *u; | |
1797 int category; | |
1798 | |
1799 s = (t38_gateway_state_t *) t->frame_user_data; | |
1800 u = &s->core.to_t38; | |
1801 if ((t->raw_bit_stream & 0x80)) | |
1802 { | |
1803 /* Hit HDLC abort */ | |
1804 t->rx_aborts++; | |
1805 if (t->flags_seen < t->framing_ok_threshold) | |
1806 t->flags_seen = 0; | |
1807 else | |
1808 t->flags_seen = t->framing_ok_threshold - 1; | |
1809 /*endif*/ | |
1810 } | |
1811 else | |
1812 { | |
1813 /* Hit HDLC flag */ | |
1814 if (t->flags_seen >= t->framing_ok_threshold) | |
1815 { | |
1816 category = (s->t38x.current_tx_data_type == T38_DATA_V21) ? T38_PACKET_CATEGORY_CONTROL_DATA : T38_PACKET_CATEGORY_IMAGE_DATA; | |
1817 if (t->len) | |
1818 { | |
1819 /* This is not back-to-back flags */ | |
1820 if (t->len >= 2) | |
1821 { | |
1822 if (u->data_ptr) | |
1823 { | |
1824 bit_reverse(u->data, t->buffer + t->len - 2 - u->data_ptr, u->data_ptr); | |
1825 t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_DATA, u->data, u->data_ptr, category); | |
1826 } | |
1827 /*endif*/ | |
1828 if (t->num_bits != 7) | |
1829 { | |
1830 t->rx_crc_errors++; | |
1831 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s, misaligned terminating flag at %d\n", t30_frametype(t->buffer[2]), t->len); | |
1832 /* It seems some boxes may not like us sending a _SIG_END here, and then another | |
1833 when the carrier actually drops. Lets just send T38_FIELD_HDLC_FCS_OK here. */ | |
1834 if (t->len > 2) | |
1835 t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_FCS_BAD, NULL, 0, category); | |
1836 /*endif*/ | |
1837 } | |
1838 else if ((u->crc & 0xFFFF) != 0xF0B8) | |
1839 { | |
1840 t->rx_crc_errors++; | |
1841 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s, bad CRC at %d\n", t30_frametype(t->buffer[2]), t->len); | |
1842 /* It seems some boxes may not like us sending a _SIG_END here, and then another | |
1843 when the carrier actually drops. Lets just send T38_FIELD_HDLC_FCS_OK here. */ | |
1844 if (t->len > 2) | |
1845 t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_FCS_BAD, NULL, 0, category); | |
1846 /*endif*/ | |
1847 } | |
1848 else | |
1849 { | |
1850 t->rx_frames++; | |
1851 t->rx_bytes += t->len - 2; | |
1852 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s, CRC OK\n", t30_frametype(t->buffer[2])); | |
1853 if (s->t38x.current_tx_data_type == T38_DATA_V21) | |
1854 { | |
1855 monitor_control_messages(s, TRUE, t->buffer, t->len - 2); | |
1856 if (s->core.real_time_frame_handler) | |
1857 s->core.real_time_frame_handler(s, s->core.real_time_frame_user_data, TRUE, t->buffer, t->len - 2); | |
1858 /*endif*/ | |
1859 } | |
1860 else | |
1861 { | |
1862 /* Make sure we go back to short training if CTC/CTR has kicked us into | |
1863 long training. Any successful HDLC frame received at a rate other than | |
1864 V.21 is an adequate indication we should change. */ | |
1865 s->core.short_train = TRUE; | |
1866 } | |
1867 /*endif*/ | |
1868 /* It seems some boxes may not like us sending a _SIG_END here, and then another | |
1869 when the carrier actually drops. Lets just send T38_FIELD_HDLC_FCS_OK here. */ | |
1870 t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_FCS_OK, NULL, 0, category); | |
1871 } | |
1872 /*endif*/ | |
1873 } | |
1874 else | |
1875 { | |
1876 /* Frame too short */ | |
1877 t->rx_length_errors++; | |
1878 } | |
1879 /*endif*/ | |
1880 } | |
1881 /*endif*/ | |
1882 } | |
1883 else | |
1884 { | |
1885 /* Check the flags are back-to-back when testing for valid preamble. This | |
1886 greatly reduces the chances of false preamble detection, and anything | |
1887 which doesn't send them back-to-back is badly broken. */ | |
1888 if (t->num_bits != 7) | |
1889 t->flags_seen = 0; | |
1890 /*endif*/ | |
1891 if (++t->flags_seen >= t->framing_ok_threshold && !t->framing_ok_announced) | |
1892 { | |
1893 if (s->t38x.current_tx_data_type == T38_DATA_V21) | |
1894 { | |
1895 t38_core_send_indicator(&s->t38x.t38, set_slow_packetisation(s)); | |
1896 s->audio.modems.rx_signal_present = TRUE; | |
1897 } | |
1898 /*endif*/ | |
1899 if (s->t38x.in_progress_rx_indicator == T38_IND_CNG) | |
1900 set_next_tx_type(s); | |
1901 /*endif*/ | |
1902 t->framing_ok_announced = TRUE; | |
1903 } | |
1904 /*endif*/ | |
1905 } | |
1906 /*endif*/ | |
1907 } | |
1908 /*endif*/ | |
1909 t->len = 0; | |
1910 t->num_bits = 0; | |
1911 u->crc = 0xFFFF; | |
1912 u->data_ptr = 0; | |
1913 s->t38x.corrupt_current_frame[1] = FALSE; | |
1914 } | |
1915 /*- End of function --------------------------------------------------------*/ | |
1916 | |
1917 static void t38_hdlc_rx_put_bit(hdlc_rx_state_t *t, int new_bit) | |
1918 { | |
1919 t38_gateway_state_t *s; | |
1920 t38_gateway_to_t38_state_t *u; | |
1921 int category; | |
1922 | |
1923 if (new_bit < 0) | |
1924 { | |
1925 hdlc_rx_status(t, new_bit); | |
1926 return; | |
1927 } | |
1928 /*endif*/ | |
1929 t->raw_bit_stream = (t->raw_bit_stream << 1) | (new_bit & 1); | |
1930 if ((t->raw_bit_stream & 0x3F) == 0x3E) | |
1931 { | |
1932 /* Its time to either skip a bit, for stuffing, or process a flag or abort */ | |
1933 if ((t->raw_bit_stream & 0x40)) | |
1934 rx_flag_or_abort(t); | |
1935 return; | |
1936 } | |
1937 /*endif*/ | |
1938 t->num_bits++; | |
1939 if (!t->framing_ok_announced) | |
1940 return; | |
1941 /*endif*/ | |
1942 t->byte_in_progress = (t->byte_in_progress >> 1) | ((t->raw_bit_stream & 0x01) << 7); | |
1943 if (t->num_bits != 8) | |
1944 return; | |
1945 /*endif*/ | |
1946 t->num_bits = 0; | |
1947 if (t->len >= (int) sizeof(t->buffer)) | |
1948 { | |
1949 /* This is too long. Abandon the frame, and wait for the next flag octet. */ | |
1950 t->rx_length_errors++; | |
1951 t->flags_seen = t->framing_ok_threshold - 1; | |
1952 t->len = 0; | |
1953 return; | |
1954 } | |
1955 /*endif*/ | |
1956 s = (t38_gateway_state_t *) t->frame_user_data; | |
1957 u = &s->core.to_t38; | |
1958 t->buffer[t->len] = (uint8_t) t->byte_in_progress; | |
1959 /* Calculate the CRC progressively, before we start altering the frame */ | |
1960 u->crc = crc_itu16_calc(&t->buffer[t->len], 1, u->crc); | |
1961 /* Make the transmission lag by two octets, so we do not send the CRC, and | |
1962 do not report the CRC result too late. */ | |
1963 if (++t->len <= 2) | |
1964 return; | |
1965 /*endif*/ | |
1966 if (s->t38x.current_tx_data_type == T38_DATA_V21) | |
1967 { | |
1968 /* The V.21 control messages need to be monitored, and possibly corrupted, to manage the | |
1969 man-in-the-middle role of T.38 */ | |
1970 edit_control_messages(s, 1, t->buffer, t->len); | |
1971 } | |
1972 if (++u->data_ptr >= u->octets_per_data_packet) | |
1973 { | |
1974 bit_reverse(u->data, t->buffer + t->len - 2 - u->data_ptr, u->data_ptr); | |
1975 category = (s->t38x.current_tx_data_type == T38_DATA_V21) ? T38_PACKET_CATEGORY_CONTROL_DATA : T38_PACKET_CATEGORY_IMAGE_DATA; | |
1976 t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_DATA, u->data, u->data_ptr, category); | |
1977 /* Since we delay transmission by 2 octets, we should now have sent the last of the data octets when | |
1978 we have just received the last of the CRC octets. */ | |
1979 u->data_ptr = 0; | |
1980 } | |
1981 /*endif*/ | |
1982 } | |
1983 /*- End of function --------------------------------------------------------*/ | |
1984 | |
1985 static int restart_rx_modem(t38_gateway_state_t *s) | |
1986 { | |
1987 put_bit_func_t put_bit_func; | |
1988 void *put_bit_user_data; | |
1989 | |
1990 if (s->core.to_t38.in_bits || s->core.to_t38.out_octets) | |
1991 { | |
1992 span_log(&s->logging, | |
1993 SPAN_LOG_FLOW, | |
1994 "%d incoming audio bits. %d outgoing T.38 octets\n", | |
1995 s->core.to_t38.in_bits, | |
1996 s->core.to_t38.out_octets); | |
1997 s->core.to_t38.in_bits = 0; | |
1998 s->core.to_t38.out_octets = 0; | |
1999 } | |
2000 span_log(&s->logging, SPAN_LOG_FLOW, "Restart rx modem - modem = %d, short train = %d, ECM = %d\n", s->core.fast_rx_modem, s->core.short_train, s->core.ecm_mode); | |
2001 | |
2002 hdlc_rx_init(&(s->audio.modems.hdlc_rx), FALSE, TRUE, HDLC_FRAMING_OK_THRESHOLD, NULL, s); | |
2003 s->audio.modems.rx_signal_present = FALSE; | |
2004 s->audio.modems.rx_trained = FALSE; | |
2005 /* Default to the transmit data being V.21, unless a faster modem pops up trained. */ | |
2006 s->t38x.current_tx_data_type = T38_DATA_V21; | |
2007 fsk_rx_init(&(s->audio.modems.v21_rx), &preset_fsk_specs[FSK_V21CH2], FSK_FRAME_MODE_SYNC, (put_bit_func_t) t38_hdlc_rx_put_bit, &(s->audio.modems.hdlc_rx)); | |
2008 #if 0 | |
2009 fsk_rx_signal_cutoff(&(s->audio.modems.v21_rx), -45.5f); | |
2010 #endif | |
2011 if (s->core.image_data_mode && s->core.ecm_mode) | |
2012 { | |
2013 put_bit_func = (put_bit_func_t) t38_hdlc_rx_put_bit; | |
2014 put_bit_user_data = (void *) &(s->audio.modems.hdlc_rx); | |
2015 } | |
2016 else | |
2017 { | |
2018 if (s->core.image_data_mode && s->core.to_t38.fill_bit_removal) | |
2019 put_bit_func = non_ecm_remove_fill_and_put_bit; | |
2020 else | |
2021 put_bit_func = non_ecm_put_bit; | |
2022 put_bit_user_data = (void *) s; | |
2023 } | |
2024 /*endif*/ | |
2025 to_t38_buffer_init(&s->core.to_t38); | |
2026 s->core.to_t38.octets_per_data_packet = 1; | |
2027 switch (s->core.fast_rx_modem) | |
2028 { | |
2029 case T38_V17_RX: | |
2030 v17_rx_restart(&s->audio.modems.v17_rx, s->core.fast_bit_rate, s->core.short_train); | |
2031 v17_rx_set_put_bit(&s->audio.modems.v17_rx, put_bit_func, put_bit_user_data); | |
2032 set_rx_handler(s, (span_rx_handler_t *) &v17_v21_rx, s); | |
2033 s->core.fast_rx_active = T38_V17_RX; | |
2034 break; | |
2035 case T38_V27TER_RX: | |
2036 v27ter_rx_restart(&s->audio.modems.v27ter_rx, s->core.fast_bit_rate, FALSE); | |
2037 v27ter_rx_set_put_bit(&s->audio.modems.v27ter_rx, put_bit_func, put_bit_user_data); | |
2038 set_rx_handler(s, (span_rx_handler_t *) &v27ter_v21_rx, s); | |
2039 s->core.fast_rx_active = T38_V27TER_RX; | |
2040 break; | |
2041 case T38_V29_RX: | |
2042 v29_rx_restart(&s->audio.modems.v29_rx, s->core.fast_bit_rate, FALSE); | |
2043 v29_rx_set_put_bit(&s->audio.modems.v29_rx, put_bit_func, put_bit_user_data); | |
2044 set_rx_handler(s, (span_rx_handler_t *) &v29_v21_rx, s); | |
2045 s->core.fast_rx_active = T38_V29_RX; | |
2046 break; | |
2047 default: | |
2048 set_rx_handler(s, (span_rx_handler_t *) &fsk_rx, &(s->audio.modems.v21_rx)); | |
2049 s->core.fast_rx_active = T38_NONE; | |
2050 break; | |
2051 } | |
2052 /*endswitch*/ | |
2053 return 0; | |
2054 } | |
2055 /*- End of function --------------------------------------------------------*/ | |
2056 | |
2057 SPAN_DECLARE(int) t38_gateway_rx(t38_gateway_state_t *s, int16_t amp[], int len) | |
2058 { | |
2059 int i; | |
2060 | |
2061 #if defined(LOG_FAX_AUDIO) | |
2062 if (s->audio.modems.audio_rx_log >= 0) | |
2063 write(s->audio.modems.audio_rx_log, amp, len*sizeof(int16_t)); | |
2064 /*endif*/ | |
2065 #endif | |
2066 if (s->core.samples_to_timeout > 0) | |
2067 { | |
2068 if ((s->core.samples_to_timeout -= len) <= 0) | |
2069 { | |
2070 switch (s->core.timed_mode) | |
2071 { | |
2072 case TIMED_MODE_TCF_PREDICTABLE_MODEM_START_PAST_V21_MODEM: | |
2073 /* Timed announcement of training, 75ms after the DCS carrier fell. */ | |
2074 s->core.timed_mode = TIMED_MODE_TCF_PREDICTABLE_MODEM_START_FAST_MODEM_ANNOUNCED; | |
2075 announce_training(s); | |
2076 break; | |
2077 case TIMED_MODE_TCF_PREDICTABLE_MODEM_START_FAST_MODEM_SEEN: | |
2078 /* Timed announcement of training, 75ms after the DCS carrier fell. */ | |
2079 /* Use a timeout to ride over TEP, if it is present */ | |
2080 s->core.samples_to_timeout = ms_to_samples(500); | |
2081 s->core.timed_mode = TIMED_MODE_TCF_PREDICTABLE_MODEM_START_FAST_MODEM_ANNOUNCED; | |
2082 announce_training(s); | |
2083 break; | |
2084 case TIMED_MODE_TCF_PREDICTABLE_MODEM_START_FAST_MODEM_ANNOUNCED: | |
2085 s->core.timed_mode = TIMED_MODE_IDLE; | |
2086 span_log(&s->logging, SPAN_LOG_FLOW, "TEP jamming expired\n"); | |
2087 break; | |
2088 case TIMED_MODE_STARTUP: | |
2089 /* Ensure a no-signal condition goes out the moment the received audio starts */ | |
2090 t38_core_send_indicator(&s->t38x.t38, T38_IND_NO_SIGNAL); | |
2091 s->core.timed_mode = TIMED_MODE_IDLE; | |
2092 break; | |
2093 } | |
2094 /*endswitch*/ | |
2095 } | |
2096 /*endif*/ | |
2097 } | |
2098 /*endif*/ | |
2099 for (i = 0; i < len; i++) | |
2100 amp[i] = dc_restore(&(s->audio.modems.dc_restore), amp[i]); | |
2101 /*endfor*/ | |
2102 s->audio.modems.rx_handler(s->audio.modems.rx_user_data, amp, len); | |
2103 return 0; | |
2104 } | |
2105 /*- End of function --------------------------------------------------------*/ | |
2106 | |
2107 SPAN_DECLARE(int) t38_gateway_tx(t38_gateway_state_t *s, int16_t amp[], int max_len) | |
2108 { | |
2109 int len; | |
2110 #if defined(LOG_FAX_AUDIO) | |
2111 int required_len; | |
2112 | |
2113 required_len = max_len; | |
2114 #endif | |
2115 if ((len = s->audio.modems.tx_handler(s->audio.modems.tx_user_data, amp, max_len)) < max_len) | |
2116 { | |
2117 if (set_next_tx_type(s)) | |
2118 { | |
2119 /* Give the new handler a chance to file the remaining buffer space */ | |
2120 len += s->audio.modems.tx_handler(s->audio.modems.tx_user_data, amp + len, max_len - len); | |
2121 if (len < max_len) | |
2122 { | |
2123 silence_gen_set(&(s->audio.modems.silence_gen), 0); | |
2124 set_next_tx_type(s); | |
2125 } | |
2126 /*endif*/ | |
2127 } | |
2128 /*endif*/ | |
2129 } | |
2130 /*endif*/ | |
2131 if (s->audio.modems.transmit_on_idle) | |
2132 { | |
2133 /* Pad to the requested length with silence */ | |
2134 memset(amp + len, 0, (max_len - len)*sizeof(int16_t)); | |
2135 len = max_len; | |
2136 } | |
2137 /*endif*/ | |
2138 #if defined(LOG_FAX_AUDIO) | |
2139 if (s->audio.modems.audio_tx_log >= 0) | |
2140 { | |
2141 if (len < required_len) | |
2142 memset(amp + len, 0, (required_len - len)*sizeof(int16_t)); | |
2143 /*endif*/ | |
2144 write(s->audio.modems.audio_tx_log, amp, required_len*sizeof(int16_t)); | |
2145 } | |
2146 /*endif*/ | |
2147 #endif | |
2148 return len; | |
2149 } | |
2150 /*- End of function --------------------------------------------------------*/ | |
2151 | |
2152 SPAN_DECLARE(void) t38_gateway_get_transfer_statistics(t38_gateway_state_t *s, t38_stats_t *t) | |
2153 { | |
2154 memset(t, 0, sizeof(*t)); | |
2155 t->bit_rate = s->core.fast_bit_rate; | |
2156 t->error_correcting_mode = s->core.ecm_mode; | |
2157 t->pages_transferred = s->core.pages_confirmed; | |
2158 } | |
2159 /*- End of function --------------------------------------------------------*/ | |
2160 | |
2161 SPAN_DECLARE(t38_core_state_t *) t38_gateway_get_t38_core_state(t38_gateway_state_t *s) | |
2162 { | |
2163 return &s->t38x.t38; | |
2164 } | |
2165 /*- End of function --------------------------------------------------------*/ | |
2166 | |
2167 SPAN_DECLARE(logging_state_t *) t38_gateway_get_logging_state(t38_gateway_state_t *s) | |
2168 { | |
2169 return &s->logging; | |
2170 } | |
2171 /*- End of function --------------------------------------------------------*/ | |
2172 | |
2173 SPAN_DECLARE(void) t38_gateway_set_ecm_capability(t38_gateway_state_t *s, int ecm_allowed) | |
2174 { | |
2175 s->core.ecm_allowed = ecm_allowed; | |
2176 } | |
2177 /*- End of function --------------------------------------------------------*/ | |
2178 | |
2179 SPAN_DECLARE(void) t38_gateway_set_transmit_on_idle(t38_gateway_state_t *s, int transmit_on_idle) | |
2180 { | |
2181 s->audio.modems.transmit_on_idle = transmit_on_idle; | |
2182 } | |
2183 /*- End of function --------------------------------------------------------*/ | |
2184 | |
2185 SPAN_DECLARE(void) t38_gateway_set_supported_modems(t38_gateway_state_t *s, int supported_modems) | |
2186 { | |
2187 s->core.supported_modems = supported_modems; | |
2188 if ((s->core.supported_modems & T30_SUPPORT_V17)) | |
2189 t38_set_fastest_image_data_rate(&s->t38x.t38, 14400); | |
2190 else if ((s->core.supported_modems & T30_SUPPORT_V29)) | |
2191 t38_set_fastest_image_data_rate(&s->t38x.t38, 9600); | |
2192 else | |
2193 t38_set_fastest_image_data_rate(&s->t38x.t38, 4800); | |
2194 /*endif*/ | |
2195 } | |
2196 /*- End of function --------------------------------------------------------*/ | |
2197 | |
2198 SPAN_DECLARE(void) t38_gateway_set_nsx_suppression(t38_gateway_state_t *s, | |
2199 const uint8_t *from_t38, | |
2200 int from_t38_len, | |
2201 const uint8_t *from_modem, | |
2202 int from_modem_len) | |
2203 { | |
2204 s->t38x.suppress_nsx_len[0] = (from_t38_len < 0 || from_t38_len < MAX_NSX_SUPPRESSION) ? (from_t38_len + 3) : 0; | |
2205 s->t38x.suppress_nsx_len[1] = (from_modem_len < 0 || from_modem_len < MAX_NSX_SUPPRESSION) ? (from_modem_len + 3) : 0; | |
2206 } | |
2207 /*- End of function --------------------------------------------------------*/ | |
2208 | |
2209 SPAN_DECLARE(void) t38_gateway_set_tep_mode(t38_gateway_state_t *s, int use_tep) | |
2210 { | |
2211 s->audio.modems.use_tep = use_tep; | |
2212 } | |
2213 /*- End of function --------------------------------------------------------*/ | |
2214 | |
2215 SPAN_DECLARE(void) t38_gateway_set_fill_bit_removal(t38_gateway_state_t *s, int remove) | |
2216 { | |
2217 s->core.to_t38.fill_bit_removal = remove; | |
2218 } | |
2219 /*- End of function --------------------------------------------------------*/ | |
2220 | |
2221 SPAN_DECLARE(void) t38_gateway_set_real_time_frame_handler(t38_gateway_state_t *s, | |
2222 t38_gateway_real_time_frame_handler_t *handler, | |
2223 void *user_data) | |
2224 { | |
2225 s->core.real_time_frame_handler = handler; | |
2226 s->core.real_time_frame_user_data = user_data; | |
2227 } | |
2228 /*- End of function --------------------------------------------------------*/ | |
2229 | |
2230 static int t38_gateway_audio_init(t38_gateway_state_t *s) | |
2231 { | |
2232 fax_modems_init(&s->audio.modems, | |
2233 FALSE, | |
2234 NULL, | |
2235 hdlc_underflow_handler, | |
2236 non_ecm_put_bit, | |
2237 t38_non_ecm_buffer_get_bit, | |
2238 tone_detected, | |
2239 s); | |
2240 /* We need to use progressive HDLC transmit, and a special HDLC receiver, which is different | |
2241 from the other uses of FAX modems. */ | |
2242 hdlc_tx_init(&s->audio.modems.hdlc_tx, FALSE, 2, TRUE, hdlc_underflow_handler, s); | |
2243 fsk_rx_set_put_bit(&s->audio.modems.v21_rx, (put_bit_func_t) t38_hdlc_rx_put_bit, &s->audio.modems.hdlc_rx); | |
2244 /* TODO: Don't use the very low cutoff levels we would like to. We get some quirks if we do. | |
2245 We need to sort this out. */ | |
2246 fsk_rx_signal_cutoff(&s->audio.modems.v21_rx, -30.0f); | |
2247 v29_rx_signal_cutoff(&s->audio.modems.v29_rx, -28.5f); | |
2248 return 0; | |
2249 } | |
2250 /*- End of function --------------------------------------------------------*/ | |
2251 | |
2252 static int t38_gateway_t38_init(t38_gateway_state_t *t, | |
2253 t38_tx_packet_handler_t *tx_packet_handler, | |
2254 void *tx_packet_user_data) | |
2255 { | |
2256 t38_gateway_t38_state_t *s; | |
2257 | |
2258 s = &t->t38x; | |
2259 t38_core_init(&s->t38, | |
2260 process_rx_indicator, | |
2261 process_rx_data, | |
2262 process_rx_missing, | |
2263 (void *) t, | |
2264 tx_packet_handler, | |
2265 tx_packet_user_data); | |
2266 t38_set_redundancy_control(&s->t38, T38_PACKET_CATEGORY_INDICATOR, INDICATOR_TX_COUNT); | |
2267 t38_set_redundancy_control(&s->t38, T38_PACKET_CATEGORY_CONTROL_DATA, DATA_TX_COUNT); | |
2268 t38_set_redundancy_control(&s->t38, T38_PACKET_CATEGORY_CONTROL_DATA_END, DATA_END_TX_COUNT); | |
2269 t38_set_redundancy_control(&s->t38, T38_PACKET_CATEGORY_IMAGE_DATA, DATA_TX_COUNT); | |
2270 t38_set_redundancy_control(&s->t38, T38_PACKET_CATEGORY_IMAGE_DATA_END, DATA_END_TX_COUNT); | |
2271 return 0; | |
2272 } | |
2273 /*- End of function --------------------------------------------------------*/ | |
2274 | |
2275 SPAN_DECLARE(t38_gateway_state_t *) t38_gateway_init(t38_gateway_state_t *s, | |
2276 t38_tx_packet_handler_t *tx_packet_handler, | |
2277 void *tx_packet_user_data) | |
2278 { | |
2279 if (tx_packet_handler == NULL) | |
2280 return NULL; | |
2281 /*endif*/ | |
2282 if (s == NULL) | |
2283 { | |
2284 if ((s = (t38_gateway_state_t *) malloc(sizeof(*s))) == NULL) | |
2285 return NULL; | |
2286 /*endif*/ | |
2287 } | |
2288 /*endif*/ | |
2289 memset(s, 0, sizeof(*s)); | |
2290 span_log_init(&s->logging, SPAN_LOG_NONE, NULL); | |
2291 span_log_set_protocol(&s->logging, "T.38G"); | |
2292 | |
2293 t38_gateway_audio_init(s); | |
2294 t38_gateway_t38_init(s, tx_packet_handler, tx_packet_user_data); | |
2295 | |
2296 set_rx_active(s, TRUE); | |
2297 t38_gateway_set_supported_modems(s, T30_SUPPORT_V27TER | T30_SUPPORT_V29); | |
2298 t38_gateway_set_nsx_suppression(s, (const uint8_t *) "\x00\x00\x00", 3, (const uint8_t *) "\x00\x00\x00", 3); | |
2299 | |
2300 s->core.to_t38.octets_per_data_packet = 1; | |
2301 s->core.ecm_allowed = TRUE; | |
2302 t38_non_ecm_buffer_init(&s->core.non_ecm_to_modem, FALSE, 0); | |
2303 restart_rx_modem(s); | |
2304 s->core.timed_mode = TIMED_MODE_STARTUP; | |
2305 s->core.samples_to_timeout = 1; | |
2306 #if defined(LOG_FAX_AUDIO) | |
2307 { | |
2308 char buf[100 + 1]; | |
2309 struct tm *tm; | |
2310 time_t now; | |
2311 | |
2312 time(&now); | |
2313 tm = localtime(&now); | |
2314 sprintf(buf, | |
2315 "/tmp/t38-rx-audio-%p-%02d%02d%02d%02d%02d%02d", | |
2316 s, | |
2317 tm->tm_year%100, | |
2318 tm->tm_mon + 1, | |
2319 tm->tm_mday, | |
2320 tm->tm_hour, | |
2321 tm->tm_min, | |
2322 tm->tm_sec); | |
2323 s->audio.modems.audio_rx_log = open(buf, O_CREAT | O_TRUNC | O_WRONLY, 0666); | |
2324 sprintf(buf, | |
2325 "/tmp/t38-tx-audio-%p-%02d%02d%02d%02d%02d%02d", | |
2326 s, | |
2327 tm->tm_year%100, | |
2328 tm->tm_mon + 1, | |
2329 tm->tm_mday, | |
2330 tm->tm_hour, | |
2331 tm->tm_min, | |
2332 tm->tm_sec); | |
2333 s->audio.modems.audio_tx_log = open(buf, O_CREAT | O_TRUNC | O_WRONLY, 0666); | |
2334 } | |
2335 #endif | |
2336 return s; | |
2337 } | |
2338 /*- End of function --------------------------------------------------------*/ | |
2339 | |
2340 SPAN_DECLARE(int) t38_gateway_release(t38_gateway_state_t *s) | |
2341 { | |
2342 return 0; | |
2343 } | |
2344 /*- End of function --------------------------------------------------------*/ | |
2345 | |
2346 SPAN_DECLARE(int) t38_gateway_free(t38_gateway_state_t *s) | |
2347 { | |
2348 free(s); | |
2349 return 0; | |
2350 } | |
2351 /*- End of function --------------------------------------------------------*/ | |
2352 /*- End of file ------------------------------------------------------------*/ |