Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/src/t31.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 /* | |
2 * SpanDSP - a series of DSP components for telephony | |
3 * | |
4 * t31.c - A T.31 compatible class 1 FAX modem interface. | |
5 * | |
6 * Written by Steve Underwood <steveu@coppice.org> | |
7 * | |
8 * Special thanks to Lee Howard <faxguy@howardsilvan.com> | |
9 * for his great work debugging and polishing this code. | |
10 * | |
11 * Copyright (C) 2004, 2005, 2006, 2008 Steve Underwood | |
12 * | |
13 * All rights reserved. | |
14 * | |
15 * This program is free software; you can redistribute it and/or modify | |
16 * it under the terms of the GNU Lesser General Public License version 2.1, | |
17 * as published by the Free Software Foundation. | |
18 * | |
19 * This program is distributed in the hope that it will be useful, | |
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 * GNU Lesser General Public License for more details. | |
23 * | |
24 * You should have received a copy of the GNU Lesser General Public | |
25 * License along with this program; if not, write to the Free Software | |
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
27 * | |
28 * $Id: t31.c,v 1.155.4.1 2009/12/19 10:44:10 steveu Exp $ | |
29 */ | |
30 | |
31 /*! \file */ | |
32 | |
33 #if defined(HAVE_CONFIG_H) | |
34 #include "config.h" | |
35 #endif | |
36 | |
37 #include <inttypes.h> | |
38 #include <stdlib.h> | |
39 #include <stdio.h> | |
40 #include <fcntl.h> | |
41 #include <time.h> | |
42 #include <memory.h> | |
43 #include <string.h> | |
44 #include <ctype.h> | |
45 #if defined(HAVE_TGMATH_H) | |
46 #include <tgmath.h> | |
47 #endif | |
48 #if defined(HAVE_MATH_H) | |
49 #include <math.h> | |
50 #endif | |
51 #include "floating_fudge.h" | |
52 #include <assert.h> | |
53 #include <tiffio.h> | |
54 | |
55 #include "spandsp/telephony.h" | |
56 #include "spandsp/logging.h" | |
57 #include "spandsp/bit_operations.h" | |
58 #include "spandsp/dc_restore.h" | |
59 #include "spandsp/queue.h" | |
60 #include "spandsp/power_meter.h" | |
61 #include "spandsp/complex.h" | |
62 #include "spandsp/tone_detect.h" | |
63 #include "spandsp/tone_generate.h" | |
64 #include "spandsp/async.h" | |
65 #include "spandsp/crc.h" | |
66 #include "spandsp/hdlc.h" | |
67 #include "spandsp/silence_gen.h" | |
68 #include "spandsp/fsk.h" | |
69 #include "spandsp/v29tx.h" | |
70 #include "spandsp/v29rx.h" | |
71 #include "spandsp/v27ter_tx.h" | |
72 #include "spandsp/v27ter_rx.h" | |
73 #include "spandsp/v17tx.h" | |
74 #include "spandsp/v17rx.h" | |
75 #include "spandsp/super_tone_rx.h" | |
76 #include "spandsp/modem_connect_tones.h" | |
77 #include "spandsp/t4_rx.h" | |
78 #include "spandsp/t4_tx.h" | |
79 #include "spandsp/t30.h" | |
80 #include "spandsp/t30_logging.h" | |
81 #include "spandsp/t38_core.h" | |
82 | |
83 #include "spandsp/at_interpreter.h" | |
84 #include "spandsp/fax_modems.h" | |
85 #include "spandsp/t31.h" | |
86 #include "spandsp/t30_fcf.h" | |
87 | |
88 #include "spandsp/private/logging.h" | |
89 #include "spandsp/private/t38_core.h" | |
90 #include "spandsp/private/silence_gen.h" | |
91 #include "spandsp/private/fsk.h" | |
92 #include "spandsp/private/v17tx.h" | |
93 #include "spandsp/private/v17rx.h" | |
94 #include "spandsp/private/v27ter_tx.h" | |
95 #include "spandsp/private/v27ter_rx.h" | |
96 #include "spandsp/private/v29tx.h" | |
97 #include "spandsp/private/v29rx.h" | |
98 #include "spandsp/private/modem_connect_tones.h" | |
99 #include "spandsp/private/hdlc.h" | |
100 #include "spandsp/private/fax_modems.h" | |
101 #include "spandsp/private/at_interpreter.h" | |
102 #include "spandsp/private/t31.h" | |
103 | |
104 /* Settings suitable for paced transmission over a UDP transport */ | |
105 /*! The default number of milliseconds per transmitted IFP when sending bulk T.38 data */ | |
106 #define MS_PER_TX_CHUNK 30 | |
107 /*! The number of transmissions of indicator IFP packets */ | |
108 #define INDICATOR_TX_COUNT 3 | |
109 /*! The number of transmissions of data IFP packets */ | |
110 #define DATA_TX_COUNT 1 | |
111 /*! The number of transmissions of terminating data IFP packets */ | |
112 #define DATA_END_TX_COUNT 3 | |
113 /*! The default DTE timeout, in seconds */ | |
114 #define DEFAULT_DTE_TIMEOUT 5 | |
115 | |
116 /* Settings suitable for unpaced transmission over a TCP transport */ | |
117 #define MAX_OCTETS_PER_UNPACED_CHUNK 300 | |
118 | |
119 /* Backstop timeout if reception of packets stops in the middle of a burst */ | |
120 #define MID_RX_TIMEOUT 15000 | |
121 | |
122 #define HDLC_FRAMING_OK_THRESHOLD 5 | |
123 | |
124 typedef const char *(*at_cmd_service_t)(t31_state_t *s, const char *cmd); | |
125 | |
126 enum | |
127 { | |
128 ETX = 0x03, | |
129 DLE = 0x10, | |
130 SUB = 0x1A | |
131 }; | |
132 | |
133 enum | |
134 { | |
135 DISBIT1 = 0x01, | |
136 DISBIT2 = 0x02, | |
137 DISBIT3 = 0x04, | |
138 DISBIT4 = 0x08, | |
139 DISBIT5 = 0x10, | |
140 DISBIT6 = 0x20, | |
141 DISBIT7 = 0x40, | |
142 DISBIT8 = 0x80 | |
143 }; | |
144 | |
145 enum | |
146 { | |
147 T38_CHUNKING_MERGE_FCS_WITH_DATA = 0x0001, | |
148 T38_CHUNKING_WHOLE_FRAMES = 0x0002, | |
149 T38_CHUNKING_ALLOW_TEP_TIME = 0x0004 | |
150 }; | |
151 | |
152 enum | |
153 { | |
154 T38_TIMED_STEP_NONE = 0, | |
155 T38_TIMED_STEP_NON_ECM_MODEM = 0x10, | |
156 T38_TIMED_STEP_NON_ECM_MODEM_2 = 0x11, | |
157 T38_TIMED_STEP_NON_ECM_MODEM_3 = 0x12, | |
158 T38_TIMED_STEP_NON_ECM_MODEM_4 = 0x13, | |
159 T38_TIMED_STEP_NON_ECM_MODEM_5 = 0x14, | |
160 T38_TIMED_STEP_HDLC_MODEM = 0x20, | |
161 T38_TIMED_STEP_HDLC_MODEM_2 = 0x21, | |
162 T38_TIMED_STEP_HDLC_MODEM_3 = 0x22, | |
163 T38_TIMED_STEP_HDLC_MODEM_4 = 0x23, | |
164 T38_TIMED_STEP_HDLC_MODEM_5 = 0x24, | |
165 T38_TIMED_STEP_FAKE_HDLC_MODEM = 0x30, | |
166 T38_TIMED_STEP_FAKE_HDLC_MODEM_2 = 0x31, | |
167 T38_TIMED_STEP_FAKE_HDLC_MODEM_3 = 0x32, | |
168 T38_TIMED_STEP_FAKE_HDLC_MODEM_4 = 0x33, | |
169 T38_TIMED_STEP_FAKE_HDLC_MODEM_5 = 0x34, | |
170 T38_TIMED_STEP_CED = 0x40, | |
171 T38_TIMED_STEP_CED_2 = 0x41, | |
172 T38_TIMED_STEP_CED_3 = 0x42, | |
173 T38_TIMED_STEP_CNG = 0x50, | |
174 T38_TIMED_STEP_CNG_2 = 0x51, | |
175 T38_TIMED_STEP_PAUSE = 0x60 | |
176 }; | |
177 | |
178 static int restart_modem(t31_state_t *s, int new_modem); | |
179 static void hdlc_accept_frame(void *user_data, const uint8_t *msg, int len, int ok); | |
180 static void set_rx_handler(t31_state_t *s, span_rx_handler_t *rx_handler, span_rx_fillin_handler_t *fillin_handler, void *user_data); | |
181 static void set_tx_handler(t31_state_t *s, span_tx_handler_t *handler, void *user_data); | |
182 static void set_next_tx_handler(t31_state_t *s, span_tx_handler_t *handler, void *user_data); | |
183 static int v17_v21_rx(void *user_data, const int16_t amp[], int len); | |
184 static int v17_v21_rx_fillin(void *user_data, int len); | |
185 static int v27ter_v21_rx(void *user_data, const int16_t amp[], int len); | |
186 static int v27ter_v21_rx_fillin(void *user_data, int len); | |
187 static int v29_v21_rx(void *user_data, const int16_t amp[], int len); | |
188 static int v29_v21_rx_fillin(void *user_data, int len); | |
189 static int silence_rx(void *user_data, const int16_t amp[], int len); | |
190 static int cng_rx(void *user_data, const int16_t amp[], int len); | |
191 static void non_ecm_put_bit(void *user_data, int bit); | |
192 static void non_ecm_put_chunk(void *user_data, const uint8_t buf[], int len); | |
193 static int non_ecm_get_chunk(void *user_data, uint8_t buf[], int len); | |
194 static void non_ecm_rx_status(void *user_data, int status); | |
195 static void hdlc_rx_status(void *user_data, int status); | |
196 | |
197 static __inline__ void t31_set_at_rx_mode(t31_state_t *s, int new_mode) | |
198 { | |
199 s->at_state.at_rx_mode = new_mode; | |
200 } | |
201 /*- End of function --------------------------------------------------------*/ | |
202 | |
203 #if 0 | |
204 static void monitor_control_messages(t31_state_t *s, const uint8_t *buf, int len) | |
205 { | |
206 /* Monitor the control messages, at the point where we have the whole message, so we can | |
207 see what is happening to things like training success/failure. */ | |
208 span_log(&s->logging, SPAN_LOG_FLOW, "Monitoring %s\n", t30_frametype(buf[2])); | |
209 if (len < 3) | |
210 return; | |
211 /*endif*/ | |
212 switch (buf[2]) | |
213 { | |
214 case T30_DCS: | |
215 case T30_DCS | 1: | |
216 /* We need to know if ECM is about to be used, so we can fake HDLC stuff. */ | |
217 s->t38_fe.ecm_mode = (len >= 7) && (buf[6] & DISBIT3); | |
218 break; | |
219 default: | |
220 break; | |
221 } | |
222 /*endswitch*/ | |
223 } | |
224 /*- End of function --------------------------------------------------------*/ | |
225 #endif | |
226 | |
227 static void front_end_status(t31_state_t *s, int status) | |
228 { | |
229 span_log(&s->logging, SPAN_LOG_FLOW, "Front end status %d\n", status); | |
230 switch (status) | |
231 { | |
232 case T30_FRONT_END_SEND_STEP_COMPLETE: | |
233 switch (s->modem) | |
234 { | |
235 case FAX_MODEM_SILENCE_TX: | |
236 s->modem = FAX_MODEM_NONE; | |
237 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK); | |
238 if (s->at_state.do_hangup) | |
239 { | |
240 at_modem_control(&s->at_state, AT_MODEM_CONTROL_HANGUP, NULL); | |
241 t31_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND); | |
242 s->at_state.do_hangup = FALSE; | |
243 } | |
244 else | |
245 { | |
246 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
247 } | |
248 break; | |
249 case FAX_MODEM_CED_TONE: | |
250 /* Go directly to V.21/HDLC transmit. */ | |
251 s->modem = FAX_MODEM_NONE; | |
252 restart_modem(s, FAX_MODEM_V21_TX); | |
253 t31_set_at_rx_mode(s, AT_MODE_HDLC); | |
254 break; | |
255 case FAX_MODEM_V21_TX: | |
256 case FAX_MODEM_V17_TX: | |
257 case FAX_MODEM_V27TER_TX: | |
258 case FAX_MODEM_V29_TX: | |
259 s->modem = FAX_MODEM_NONE; | |
260 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK); | |
261 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
262 restart_modem(s, FAX_MODEM_SILENCE_TX); | |
263 break; | |
264 } | |
265 break; | |
266 case T30_FRONT_END_RECEIVE_COMPLETE: | |
267 break; | |
268 } | |
269 } | |
270 /*- End of function --------------------------------------------------------*/ | |
271 | |
272 static int extra_bits_in_stuffed_frame(const uint8_t buf[], int len) | |
273 { | |
274 int bitstream; | |
275 int ones; | |
276 int stuffed; | |
277 int i; | |
278 int j; | |
279 | |
280 bitstream = 0; | |
281 ones = 0; | |
282 stuffed = 0; | |
283 /* We should really append the CRC, and include the stuffed bits for that, to get | |
284 the exact number of bits in the frame. */ | |
285 //len = crc_itu16_append(buf, len); | |
286 for (i = 0; i < len; i++) | |
287 { | |
288 bitstream = buf[i]; | |
289 for (j = 0; j < 8; j++) | |
290 { | |
291 if ((bitstream & 1)) | |
292 { | |
293 if (++ones >= 5) | |
294 { | |
295 ones = 0; | |
296 stuffed++; | |
297 } | |
298 } | |
299 else | |
300 { | |
301 ones = 0; | |
302 } | |
303 bitstream >>= 1; | |
304 } | |
305 } | |
306 /* The total length of the frame is: | |
307 the number of bits in the body | |
308 + the number of additional bits in the body due to stuffing | |
309 + the number of bits in the CRC | |
310 + the number of additional bits in the CRC due to stuffing | |
311 + 16 bits for the two terminating flag octets. | |
312 Lets just allow 3 bits for the CRC, which is the worst case. It | |
313 avoids calculating the real CRC, and the worst it can do is cause | |
314 a flag octet's worth of additional output. | |
315 */ | |
316 return stuffed + 16 + 3 + 16; | |
317 } | |
318 /*- End of function --------------------------------------------------------*/ | |
319 | |
320 static int process_rx_missing(t38_core_state_t *t, void *user_data, int rx_seq_no, int expected_seq_no) | |
321 { | |
322 t31_state_t *s; | |
323 | |
324 s = (t31_state_t *) user_data; | |
325 s->t38_fe.rx_data_missing = TRUE; | |
326 return 0; | |
327 } | |
328 /*- End of function --------------------------------------------------------*/ | |
329 | |
330 static int process_rx_indicator(t38_core_state_t *t, void *user_data, int indicator) | |
331 { | |
332 t31_state_t *s; | |
333 t31_t38_front_end_state_t *fe; | |
334 | |
335 s = (t31_state_t *) user_data; | |
336 fe = &s->t38_fe; | |
337 | |
338 if (t->current_rx_indicator == indicator) | |
339 { | |
340 /* This is probably due to the far end repeating itself, or slipping | |
341 preamble messages in between HDLC frames. T.38/V.1.3 tells us to | |
342 ignore it. Its harmless. */ | |
343 return 0; | |
344 } | |
345 /* In termination mode we don't care very much about indicators telling us training | |
346 is starting. We only care about V.21 preamble starting, for timeout control, and | |
347 the actual data. */ | |
348 switch (indicator) | |
349 { | |
350 case T38_IND_NO_SIGNAL: | |
351 if (t->current_rx_indicator == T38_IND_V21_PREAMBLE | |
352 && | |
353 (fe->current_rx_type == T30_MODEM_V21 || fe->current_rx_type == T30_MODEM_CNG)) | |
354 { | |
355 hdlc_rx_status(s, SIG_STATUS_CARRIER_DOWN); | |
356 } | |
357 fe->timeout_rx_samples = 0; | |
358 front_end_status(s, T30_FRONT_END_SIGNAL_ABSENT); | |
359 break; | |
360 case T38_IND_CNG: | |
361 front_end_status(s, T30_FRONT_END_CNG_PRESENT); | |
362 break; | |
363 case T38_IND_CED: | |
364 front_end_status(s, T30_FRONT_END_CED_PRESENT); | |
365 break; | |
366 case T38_IND_V21_PREAMBLE: | |
367 /* Some T.38 implementations insert these preamble indicators between HDLC frames, so | |
368 we need to be tolerant of that. */ | |
369 fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT); | |
370 front_end_status(s, T30_FRONT_END_SIGNAL_PRESENT); | |
371 break; | |
372 case T38_IND_V27TER_2400_TRAINING: | |
373 case T38_IND_V27TER_4800_TRAINING: | |
374 case T38_IND_V29_7200_TRAINING: | |
375 case T38_IND_V29_9600_TRAINING: | |
376 case T38_IND_V17_7200_SHORT_TRAINING: | |
377 case T38_IND_V17_7200_LONG_TRAINING: | |
378 case T38_IND_V17_9600_SHORT_TRAINING: | |
379 case T38_IND_V17_9600_LONG_TRAINING: | |
380 case T38_IND_V17_12000_SHORT_TRAINING: | |
381 case T38_IND_V17_12000_LONG_TRAINING: | |
382 case T38_IND_V17_14400_SHORT_TRAINING: | |
383 case T38_IND_V17_14400_LONG_TRAINING: | |
384 case T38_IND_V33_12000_TRAINING: | |
385 case T38_IND_V33_14400_TRAINING: | |
386 /* We really don't care what kind of modem is delivering the following image data. | |
387 We only care that some kind of fast modem signal is coming next. */ | |
388 fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT); | |
389 front_end_status(s, T30_FRONT_END_SIGNAL_PRESENT); | |
390 break; | |
391 case T38_IND_V8_ANSAM: | |
392 case T38_IND_V8_SIGNAL: | |
393 case T38_IND_V34_CNTL_CHANNEL_1200: | |
394 case T38_IND_V34_PRI_CHANNEL: | |
395 case T38_IND_V34_CC_RETRAIN: | |
396 /* V.34 support is a work in progress. */ | |
397 front_end_status(s, T30_FRONT_END_SIGNAL_PRESENT); | |
398 break; | |
399 default: | |
400 front_end_status(s, T30_FRONT_END_SIGNAL_ABSENT); | |
401 break; | |
402 } | |
403 fe->hdlc_rx.len = 0; | |
404 fe->rx_data_missing = FALSE; | |
405 return 0; | |
406 } | |
407 /*- End of function --------------------------------------------------------*/ | |
408 | |
409 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) | |
410 { | |
411 t31_state_t *s; | |
412 t31_t38_front_end_state_t *fe; | |
413 #if defined(_MSC_VER) | |
414 uint8_t *buf2 = (uint8_t *) _alloca(len); | |
415 #else | |
416 uint8_t buf2[len]; | |
417 #endif | |
418 | |
419 s = (t31_state_t *) user_data; | |
420 fe = &s->t38_fe; | |
421 #if 0 | |
422 /* In termination mode we don't care very much what the data type is. */ | |
423 switch (data_type) | |
424 { | |
425 case T38_DATA_V21: | |
426 case T38_DATA_V27TER_2400: | |
427 case T38_DATA_V27TER_4800: | |
428 case T38_DATA_V29_7200: | |
429 case T38_DATA_V29_9600: | |
430 case T38_DATA_V17_7200: | |
431 case T38_DATA_V17_9600: | |
432 case T38_DATA_V17_12000: | |
433 case T38_DATA_V17_14400: | |
434 case T38_DATA_V8: | |
435 case T38_DATA_V34_PRI_RATE: | |
436 case T38_DATA_V34_CC_1200: | |
437 case T38_DATA_V34_PRI_CH: | |
438 case T38_DATA_V33_12000: | |
439 case T38_DATA_V33_14400: | |
440 default: | |
441 break; | |
442 } | |
443 #endif | |
444 switch (field_type) | |
445 { | |
446 case T38_FIELD_HDLC_DATA: | |
447 if (fe->timeout_rx_samples == 0) | |
448 { | |
449 /* HDLC can just start without any signal indicator on some platforms, even when | |
450 there is zero packet lost. Nasty, but true. Its a good idea to be tolerant of | |
451 loss, though, so accepting a sudden start of HDLC data is the right thing to do. */ | |
452 fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT); | |
453 front_end_status(s, T30_FRONT_END_SIGNAL_PRESENT); | |
454 /* All real HDLC messages in the FAX world start with 0xFF. If this one is not starting | |
455 with 0xFF it would appear some octets must have been missed before this one. */ | |
456 if (len <= 0 || buf[0] != 0xFF) | |
457 fe->rx_data_missing = TRUE; | |
458 } | |
459 if (len > 0 && fe->hdlc_rx.len + len <= T31_T38_MAX_HDLC_LEN) | |
460 { | |
461 bit_reverse(fe->hdlc_rx.buf + fe->hdlc_rx.len, buf, len); | |
462 fe->hdlc_rx.len += len; | |
463 } | |
464 fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT); | |
465 break; | |
466 case T38_FIELD_HDLC_FCS_OK: | |
467 if (len > 0) | |
468 { | |
469 span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_OK!\n"); | |
470 /* The sender has incorrectly included data in this message. It is unclear what we should do | |
471 with it, to maximise tolerance of buggy implementations. */ | |
472 } | |
473 /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_OK messages, in IFP packets with | |
474 incrementing sequence numbers, which are actually repeats. They get through to this point because | |
475 of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */ | |
476 if (t->current_rx_data_type != data_type || t->current_rx_field_type != field_type) | |
477 { | |
478 span_log(&s->logging, SPAN_LOG_FLOW, "Type %s - CRC OK (%s)\n", (fe->hdlc_rx.len >= 3) ? t30_frametype(fe->hdlc_rx.buf[2]) : "???", (fe->rx_data_missing) ? "missing octets" : "clean"); | |
479 crc_itu16_append(fe->hdlc_rx.buf, fe->hdlc_rx.len); | |
480 hdlc_accept_frame(s, fe->hdlc_rx.buf, fe->hdlc_rx.len, !fe->rx_data_missing); | |
481 } | |
482 fe->hdlc_rx.len = 0; | |
483 fe->rx_data_missing = FALSE; | |
484 fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT); | |
485 break; | |
486 case T38_FIELD_HDLC_FCS_BAD: | |
487 if (len > 0) | |
488 { | |
489 span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_BAD!\n"); | |
490 /* The sender has incorrectly included data in this message. We can safely ignore it, as the | |
491 bad FCS means we will throw away the whole message, anyway. */ | |
492 } | |
493 /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_BAD messages, in IFP packets with | |
494 incrementing sequence numbers, which are actually repeats. They get through to this point because | |
495 of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */ | |
496 if (t->current_rx_data_type != data_type || t->current_rx_field_type != field_type) | |
497 { | |
498 span_log(&s->logging, SPAN_LOG_FLOW, "Type %s - CRC bad (%s)\n", (fe->hdlc_rx.len >= 3) ? t30_frametype(fe->hdlc_rx.buf[2]) : "???", (fe->rx_data_missing) ? "missing octets" : "clean"); | |
499 hdlc_accept_frame(s, fe->hdlc_rx.buf, fe->hdlc_rx.len, FALSE); | |
500 } | |
501 fe->hdlc_rx.len = 0; | |
502 fe->rx_data_missing = FALSE; | |
503 fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT); | |
504 break; | |
505 case T38_FIELD_HDLC_FCS_OK_SIG_END: | |
506 if (len > 0) | |
507 { | |
508 span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_OK_SIG_END!\n"); | |
509 /* The sender has incorrectly included data in this message. It is unclear what we should do | |
510 with it, to maximise tolerance of buggy implementations. */ | |
511 } | |
512 /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_OK_SIG_END messages, in IFP packets with | |
513 incrementing sequence numbers, which are actually repeats. They get through to this point because | |
514 of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */ | |
515 if (t->current_rx_data_type != data_type || t->current_rx_field_type != field_type) | |
516 { | |
517 span_log(&s->logging, SPAN_LOG_FLOW, "Type %s - CRC OK, sig end (%s)\n", (fe->hdlc_rx.len >= 3) ? t30_frametype(fe->hdlc_rx.buf[2]) : "???", (fe->rx_data_missing) ? "missing octets" : "clean"); | |
518 crc_itu16_append(fe->hdlc_rx.buf, fe->hdlc_rx.len); | |
519 hdlc_accept_frame(s, fe->hdlc_rx.buf, fe->hdlc_rx.len, !fe->rx_data_missing); | |
520 hdlc_rx_status(s, SIG_STATUS_CARRIER_DOWN); | |
521 } | |
522 fe->hdlc_rx.len = 0; | |
523 fe->rx_data_missing = FALSE; | |
524 fe->timeout_rx_samples = 0; | |
525 break; | |
526 case T38_FIELD_HDLC_FCS_BAD_SIG_END: | |
527 if (len > 0) | |
528 { | |
529 span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_BAD_SIG_END!\n"); | |
530 /* The sender has incorrectly included data in this message. We can safely ignore it, as the | |
531 bad FCS means we will throw away the whole message, anyway. */ | |
532 } | |
533 /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_BAD_SIG_END messages, in IFP packets with | |
534 incrementing sequence numbers, which are actually repeats. They get through to this point because | |
535 of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */ | |
536 if (t->current_rx_data_type != data_type || t->current_rx_field_type != field_type) | |
537 { | |
538 span_log(&s->logging, SPAN_LOG_FLOW, "Type %s - CRC bad, sig end (%s)\n", (fe->hdlc_rx.len >= 3) ? t30_frametype(fe->hdlc_rx.buf[2]) : "???", (fe->rx_data_missing) ? "missing octets" : "clean"); | |
539 hdlc_accept_frame(s, fe->hdlc_rx.buf, fe->hdlc_rx.len, FALSE); | |
540 hdlc_rx_status(s, SIG_STATUS_CARRIER_DOWN); | |
541 } | |
542 fe->hdlc_rx.len = 0; | |
543 fe->rx_data_missing = FALSE; | |
544 fe->timeout_rx_samples = 0; | |
545 break; | |
546 case T38_FIELD_HDLC_SIG_END: | |
547 if (len > 0) | |
548 { | |
549 span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_SIG_END!\n"); | |
550 /* The sender has incorrectly included data in this message, but there seems nothing meaningful | |
551 it could be. There could not be an FCS good/bad report beyond this. */ | |
552 } | |
553 /* Some T.38 implementations send multiple T38_FIELD_HDLC_SIG_END messages, in IFP packets with | |
554 incrementing sequence numbers, which are actually repeats. They get through to this point because | |
555 of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */ | |
556 if (t->current_rx_data_type != data_type || t->current_rx_field_type != field_type) | |
557 { | |
558 /* WORKAROUND: At least some Mediatrix boxes have a bug, where they can send this message at the | |
559 end of non-ECM data. We need to tolerate this. We use the generic receive complete | |
560 indication, rather than the specific HDLC carrier down. */ | |
561 /* This message is expected under 2 circumstances. One is as an alternative to T38_FIELD_HDLC_FCS_OK_SIG_END - | |
562 i.e. they send T38_FIELD_HDLC_FCS_OK, and then T38_FIELD_HDLC_SIG_END when the carrier actually drops. | |
563 The other is because the HDLC signal drops unexpectedly - i.e. not just after a final frame. */ | |
564 fe->hdlc_rx.len = 0; | |
565 fe->rx_data_missing = FALSE; | |
566 fe->timeout_rx_samples = 0; | |
567 hdlc_rx_status(s, SIG_STATUS_CARRIER_DOWN); | |
568 } | |
569 break; | |
570 case T38_FIELD_T4_NON_ECM_DATA: | |
571 if (!s->at_state.rx_signal_present) | |
572 { | |
573 non_ecm_rx_status(s, SIG_STATUS_TRAINING_SUCCEEDED); | |
574 s->at_state.rx_signal_present = TRUE; | |
575 } | |
576 if (len > 0) | |
577 { | |
578 bit_reverse(buf2, buf, len); | |
579 non_ecm_put_chunk(s, buf, len); | |
580 } | |
581 fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT); | |
582 break; | |
583 case T38_FIELD_T4_NON_ECM_SIG_END: | |
584 /* Some T.38 implementations send multiple T38_FIELD_T4_NON_ECM_SIG_END messages, in IFP packets with | |
585 incrementing sequence numbers, which are actually repeats. They get through to this point because | |
586 of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */ | |
587 if (t->current_rx_data_type != data_type || t->current_rx_field_type != field_type) | |
588 { | |
589 if (len > 0) | |
590 { | |
591 if (!s->at_state.rx_signal_present) | |
592 { | |
593 non_ecm_rx_status(s, SIG_STATUS_TRAINING_SUCCEEDED); | |
594 s->at_state.rx_signal_present = TRUE; | |
595 } | |
596 bit_reverse(buf2, buf, len); | |
597 non_ecm_put_chunk(s, buf, len); | |
598 } | |
599 /* WORKAROUND: At least some Mediatrix boxes have a bug, where they can send HDLC signal end where | |
600 they should send non-ECM signal end. It is possible they also do the opposite. | |
601 We need to tolerate this, so we use the generic receive complete | |
602 indication, rather than the specific non-ECM carrier down. */ | |
603 non_ecm_rx_status(s, SIG_STATUS_CARRIER_DOWN); | |
604 } | |
605 s->at_state.rx_signal_present = FALSE; | |
606 fe->timeout_rx_samples = 0; | |
607 break; | |
608 case T38_FIELD_CM_MESSAGE: | |
609 if (len >= 1) | |
610 span_log(&s->logging, SPAN_LOG_FLOW, "CM profile %d - %s\n", buf[0] - '0', t38_cm_profile_to_str(buf[0])); | |
611 else | |
612 span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for CM message - %d\n", len); | |
613 break; | |
614 case T38_FIELD_JM_MESSAGE: | |
615 if (len >= 2) | |
616 span_log(&s->logging, SPAN_LOG_FLOW, "JM - %s\n", t38_jm_to_str(buf, len)); | |
617 else | |
618 span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for JM message - %d\n", len); | |
619 break; | |
620 case T38_FIELD_CI_MESSAGE: | |
621 if (len >= 1) | |
622 span_log(&s->logging, SPAN_LOG_FLOW, "CI 0x%X\n", buf[0]); | |
623 else | |
624 span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for CI message - %d\n", len); | |
625 break; | |
626 case T38_FIELD_V34RATE: | |
627 if (len >= 3) | |
628 { | |
629 fe->t38.v34_rate = t38_v34rate_to_bps(buf, len); | |
630 span_log(&s->logging, SPAN_LOG_FLOW, "V.34 rate %d bps\n", fe->t38.v34_rate); | |
631 } | |
632 else | |
633 { | |
634 span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for V34rate message - %d\n", len); | |
635 } | |
636 break; | |
637 default: | |
638 break; | |
639 } | |
640 return 0; | |
641 } | |
642 /*- End of function --------------------------------------------------------*/ | |
643 | |
644 static void send_hdlc(void *user_data, const uint8_t *msg, int len) | |
645 { | |
646 t31_state_t *s; | |
647 | |
648 s = (t31_state_t *) user_data; | |
649 if (len <= 0) | |
650 { | |
651 s->hdlc_tx.len = -1; | |
652 } | |
653 else | |
654 { | |
655 s->t38_fe.hdlc_tx.extra_bits = extra_bits_in_stuffed_frame(msg, len); | |
656 bit_reverse(s->hdlc_tx.buf, msg, len); | |
657 s->hdlc_tx.len = len; | |
658 s->hdlc_tx.ptr = 0; | |
659 } | |
660 } | |
661 /*- End of function --------------------------------------------------------*/ | |
662 | |
663 static __inline__ int bits_to_us(t31_state_t *s, int bits) | |
664 { | |
665 if (s->t38_fe.ms_per_tx_chunk == 0 || s->t38_fe.tx_bit_rate == 0) | |
666 return 0; | |
667 return bits*1000000/s->t38_fe.tx_bit_rate; | |
668 } | |
669 /*- End of function --------------------------------------------------------*/ | |
670 | |
671 static void set_octets_per_data_packet(t31_state_t *s, int bit_rate) | |
672 { | |
673 s->t38_fe.tx_bit_rate = bit_rate; | |
674 if (s->t38_fe.ms_per_tx_chunk) | |
675 { | |
676 s->t38_fe.octets_per_data_packet = s->t38_fe.ms_per_tx_chunk*bit_rate/(8*1000); | |
677 /* Make sure we have a positive number (i.e. we didn't truncate to zero). */ | |
678 if (s->t38_fe.octets_per_data_packet < 1) | |
679 s->t38_fe.octets_per_data_packet = 1; | |
680 } | |
681 else | |
682 { | |
683 s->t38_fe.octets_per_data_packet = MAX_OCTETS_PER_UNPACED_CHUNK; | |
684 } | |
685 } | |
686 /*- End of function --------------------------------------------------------*/ | |
687 | |
688 static int stream_non_ecm(t31_state_t *s) | |
689 { | |
690 t31_t38_front_end_state_t *fe; | |
691 uint8_t buf[MAX_OCTETS_PER_UNPACED_CHUNK + 50]; | |
692 int delay; | |
693 int len; | |
694 | |
695 fe = &s->t38_fe; | |
696 for (delay = 0; delay == 0; ) | |
697 { | |
698 switch (fe->timed_step) | |
699 { | |
700 case T38_TIMED_STEP_NON_ECM_MODEM: | |
701 /* Create a 75ms silence */ | |
702 if (fe->t38.current_tx_indicator != T38_IND_NO_SIGNAL) | |
703 delay = t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL); | |
704 fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_2; | |
705 fe->next_tx_samples = fe->samples; | |
706 break; | |
707 case T38_TIMED_STEP_NON_ECM_MODEM_2: | |
708 /* Switch on a fast modem, and give the training time to complete */ | |
709 delay = t38_core_send_indicator(&fe->t38, fe->next_tx_indicator); | |
710 fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_3; | |
711 break; | |
712 case T38_TIMED_STEP_NON_ECM_MODEM_3: | |
713 /* Send a chunk of non-ECM image data */ | |
714 /* T.38 says it is OK to send the last of the non-ECM data in the signal end message. | |
715 However, I think the early versions of T.38 said the signal end message should not | |
716 contain data. Hopefully, following the current spec will not cause compatibility | |
717 issues. */ | |
718 len = non_ecm_get_chunk(s, buf, fe->octets_per_data_packet); | |
719 if (len > 0) | |
720 bit_reverse(buf, buf, len); | |
721 if (len < fe->octets_per_data_packet) | |
722 { | |
723 /* That's the end of the image data. */ | |
724 if (s->t38_fe.ms_per_tx_chunk) | |
725 { | |
726 /* Pad the end of the data with some zeros. If we just stop abruptly | |
727 at the end of the EOLs, some ATAs fail to clean up properly before | |
728 shutting down their transmit modem, and the last few rows of the image | |
729 are lost or corrupted. Simply delaying the no-signal message does not | |
730 help for all implentations. It is usually ignored, which is probably | |
731 the right thing to do after receiving a message saying the signal has | |
732 ended. */ | |
733 memset(buf + len, 0, fe->octets_per_data_packet - len); | |
734 fe->non_ecm_trailer_bytes = 3*fe->octets_per_data_packet + len; | |
735 len = fe->octets_per_data_packet; | |
736 fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_4; | |
737 } | |
738 else | |
739 { | |
740 /* If we are sending quickly there seems no point in doing any padding */ | |
741 t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_T4_NON_ECM_SIG_END, buf, len, T38_PACKET_CATEGORY_IMAGE_DATA_END); | |
742 fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_5; | |
743 delay = 0; | |
744 } | |
745 } | |
746 t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_T4_NON_ECM_DATA, buf, len, T38_PACKET_CATEGORY_IMAGE_DATA); | |
747 delay = bits_to_us(s, 8*len); | |
748 break; | |
749 case T38_TIMED_STEP_NON_ECM_MODEM_4: | |
750 /* Send padding */ | |
751 len = fe->octets_per_data_packet; | |
752 fe->non_ecm_trailer_bytes -= len; | |
753 if (fe->non_ecm_trailer_bytes <= 0) | |
754 { | |
755 len += fe->non_ecm_trailer_bytes; | |
756 memset(buf, 0, len); | |
757 t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_T4_NON_ECM_SIG_END, buf, len, T38_PACKET_CATEGORY_IMAGE_DATA_END); | |
758 fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_5; | |
759 /* Allow a bit more time than the data will take to play out, to ensure the far ATA does not | |
760 cut things short. */ | |
761 delay = bits_to_us(s, 8*len); | |
762 if (s->t38_fe.ms_per_tx_chunk) | |
763 delay += 60000; | |
764 front_end_status(s, T30_FRONT_END_SEND_STEP_COMPLETE); | |
765 break; | |
766 } | |
767 memset(buf, 0, len); | |
768 t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_T4_NON_ECM_DATA, buf, len, T38_PACKET_CATEGORY_IMAGE_DATA); | |
769 delay = bits_to_us(s, 8*len); | |
770 break; | |
771 case T38_TIMED_STEP_NON_ECM_MODEM_5: | |
772 /* This should not be needed, since the message above indicates the end of the signal, but it | |
773 seems like it can improve compatibility with quirky implementations. */ | |
774 delay = t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL); | |
775 fe->timed_step = T38_TIMED_STEP_NONE; | |
776 return delay; | |
777 } | |
778 } | |
779 return delay; | |
780 } | |
781 /*- End of function --------------------------------------------------------*/ | |
782 | |
783 static int stream_hdlc(t31_state_t *s) | |
784 { | |
785 t31_t38_front_end_state_t *fe; | |
786 uint8_t buf[MAX_OCTETS_PER_UNPACED_CHUNK + 50]; | |
787 t38_data_field_t data_fields[2]; | |
788 int previous; | |
789 int delay; | |
790 int i; | |
791 int category; | |
792 | |
793 fe = &s->t38_fe; | |
794 for (delay = 0; delay == 0; ) | |
795 { | |
796 switch (fe->timed_step) | |
797 { | |
798 case T38_TIMED_STEP_HDLC_MODEM: | |
799 /* Create a 75ms silence */ | |
800 if (fe->t38.current_tx_indicator != T38_IND_NO_SIGNAL) | |
801 delay = t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL); | |
802 fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_2; | |
803 fe->next_tx_samples = fe->samples + ms_to_samples(75); | |
804 break; | |
805 case T38_TIMED_STEP_HDLC_MODEM_2: | |
806 /* Send HDLC preambling */ | |
807 delay = t38_core_send_indicator(&fe->t38, fe->next_tx_indicator); | |
808 delay += t38_core_send_flags_delay(&fe->t38, fe->next_tx_indicator); | |
809 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT); | |
810 fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_3; | |
811 break; | |
812 case T38_TIMED_STEP_HDLC_MODEM_3: | |
813 /* Send a chunk of HDLC data */ | |
814 if (s->hdlc_tx.len == 0) | |
815 { | |
816 /* We don't have a frame ready yet, so wait a little */ | |
817 delay = MS_PER_TX_CHUNK*1000; | |
818 break; | |
819 } | |
820 i = s->hdlc_tx.len - s->hdlc_tx.ptr; | |
821 if (fe->octets_per_data_packet >= i) | |
822 { | |
823 /* The last part of an HDLC frame */ | |
824 if (fe->chunking_modes & T38_CHUNKING_MERGE_FCS_WITH_DATA) | |
825 { | |
826 /* Copy the data, as we might be about to refill the buffer it is in */ | |
827 memcpy(buf, &s->hdlc_tx.buf[s->hdlc_tx.ptr], i); | |
828 data_fields[0].field_type = T38_FIELD_HDLC_DATA; | |
829 data_fields[0].field = buf; | |
830 data_fields[0].field_len = i; | |
831 | |
832 /* Now see about the next HDLC frame. This will tell us whether to send FCS_OK or FCS_OK_SIG_END */ | |
833 previous = fe->current_tx_data_type; | |
834 s->hdlc_tx.ptr = 0; | |
835 s->hdlc_tx.len = 0; | |
836 front_end_status(s, T30_FRONT_END_SEND_STEP_COMPLETE); | |
837 if (s->hdlc_tx.final) | |
838 { | |
839 data_fields[1].field_type = T38_FIELD_HDLC_FCS_OK_SIG_END; | |
840 data_fields[1].field = NULL; | |
841 data_fields[1].field_len = 0; | |
842 category = (s->t38_fe.current_tx_data_type == T38_DATA_V21) ? T38_PACKET_CATEGORY_CONTROL_DATA_END : T38_PACKET_CATEGORY_IMAGE_DATA_END; | |
843 t38_core_send_data_multi_field(&fe->t38, fe->current_tx_data_type, data_fields, 2, category); | |
844 fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_5; | |
845 /* We add a bit of extra time here, as with some implementations | |
846 the carrier falling too abruptly causes data loss. */ | |
847 delay = bits_to_us(s, i*8 + fe->hdlc_tx.extra_bits); | |
848 if (s->t38_fe.ms_per_tx_chunk) | |
849 delay += 100000; | |
850 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK); | |
851 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
852 } | |
853 else | |
854 { | |
855 data_fields[1].field_type = T38_FIELD_HDLC_FCS_OK; | |
856 data_fields[1].field = NULL; | |
857 data_fields[1].field_len = 0; | |
858 category = (s->t38_fe.current_tx_data_type == T38_DATA_V21) ? T38_PACKET_CATEGORY_CONTROL_DATA : T38_PACKET_CATEGORY_IMAGE_DATA; | |
859 t38_core_send_data_multi_field(&fe->t38, fe->current_tx_data_type, data_fields, 2, category); | |
860 fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_3; | |
861 delay = bits_to_us(s, i*8 + fe->hdlc_tx.extra_bits); | |
862 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT); | |
863 } | |
864 break; | |
865 } | |
866 category = (s->t38_fe.current_tx_data_type == T38_DATA_V21) ? T38_PACKET_CATEGORY_CONTROL_DATA : T38_PACKET_CATEGORY_IMAGE_DATA; | |
867 t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_HDLC_DATA, &s->hdlc_tx.buf[s->hdlc_tx.ptr], i, category); | |
868 fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_4; | |
869 } | |
870 else | |
871 { | |
872 i = fe->octets_per_data_packet; | |
873 category = (s->t38_fe.current_tx_data_type == T38_DATA_V21) ? T38_PACKET_CATEGORY_CONTROL_DATA : T38_PACKET_CATEGORY_IMAGE_DATA; | |
874 t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_HDLC_DATA, &s->hdlc_tx.buf[s->hdlc_tx.ptr], i, category); | |
875 s->hdlc_tx.ptr += i; | |
876 } | |
877 delay = bits_to_us(s, i*8); | |
878 break; | |
879 case T38_TIMED_STEP_HDLC_MODEM_4: | |
880 /* End of HDLC frame */ | |
881 previous = fe->current_tx_data_type; | |
882 s->hdlc_tx.ptr = 0; | |
883 s->hdlc_tx.len = 0; | |
884 if (s->hdlc_tx.final) | |
885 { | |
886 /* End of transmission */ | |
887 s->hdlc_tx.len = 0; | |
888 s->hdlc_tx.final = FALSE; | |
889 category = (s->t38_fe.current_tx_data_type == T38_DATA_V21) ? T38_PACKET_CATEGORY_CONTROL_DATA : T38_PACKET_CATEGORY_IMAGE_DATA; | |
890 t38_core_send_data(&fe->t38, previous, T38_FIELD_HDLC_FCS_OK, NULL, 0, category); | |
891 fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_5; | |
892 /* We add a bit of extra time here, as with some implementations | |
893 the carrier falling too abruptly causes data loss. */ | |
894 delay = bits_to_us(s, fe->hdlc_tx.extra_bits); | |
895 if (s->t38_fe.ms_per_tx_chunk) | |
896 delay += 100000; | |
897 front_end_status(s, T30_FRONT_END_SEND_STEP_COMPLETE); | |
898 break; | |
899 } | |
900 /* Finish the current frame off, and prepare for the next one. */ | |
901 category = (s->t38_fe.current_tx_data_type == T38_DATA_V21) ? T38_PACKET_CATEGORY_CONTROL_DATA : T38_PACKET_CATEGORY_IMAGE_DATA; | |
902 t38_core_send_data(&fe->t38, previous, T38_FIELD_HDLC_FCS_OK, NULL, 0, category); | |
903 fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_3; | |
904 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT); | |
905 /* We should now wait enough time for everything to clear through an analogue modem at the far end. */ | |
906 delay = bits_to_us(s, fe->hdlc_tx.extra_bits); | |
907 if (s->hdlc_tx.len == 0) | |
908 span_log(&s->logging, SPAN_LOG_FLOW, "No new frame or end transmission condition.\n"); | |
909 break; | |
910 case T38_TIMED_STEP_HDLC_MODEM_5: | |
911 /* Note that some boxes do not like us sending a T38_FIELD_HDLC_SIG_END at this point. | |
912 A T38_IND_NO_SIGNAL should always be OK. */ | |
913 category = (s->t38_fe.current_tx_data_type == T38_DATA_V21) ? T38_PACKET_CATEGORY_CONTROL_DATA_END : T38_PACKET_CATEGORY_IMAGE_DATA_END; | |
914 t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_HDLC_SIG_END, NULL, 0, category); | |
915 delay = t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL); | |
916 fe->timed_step = T38_TIMED_STEP_NONE; | |
917 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK); | |
918 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
919 return 0; | |
920 } | |
921 } | |
922 return delay; | |
923 } | |
924 /*- End of function --------------------------------------------------------*/ | |
925 | |
926 static int stream_ced(t31_state_t *s) | |
927 { | |
928 t31_t38_front_end_state_t *fe; | |
929 int delay; | |
930 | |
931 fe = &s->t38_fe; | |
932 for (delay = 0; delay == 0; ) | |
933 { | |
934 switch (fe->timed_step) | |
935 { | |
936 case T38_TIMED_STEP_CED: | |
937 /* It seems common practice to start with a no signal indicator, though | |
938 this is not a specified requirement. Since we should be sending 200ms | |
939 of silence, starting the delay with a no signal indication makes sense. | |
940 We do need a 200ms delay, as that is a specification requirement. */ | |
941 fe->timed_step = T38_TIMED_STEP_CED_2; | |
942 delay = t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL); | |
943 delay = 200000; | |
944 fe->next_tx_samples = fe->samples; | |
945 break; | |
946 case T38_TIMED_STEP_CED_2: | |
947 /* Initial 200ms delay over. Send the CED indicator */ | |
948 fe->timed_step = T38_TIMED_STEP_CED_3; | |
949 delay = t38_core_send_indicator(&fe->t38, T38_IND_CED); | |
950 fe->current_tx_data_type = T38_DATA_NONE; | |
951 break; | |
952 case T38_TIMED_STEP_CED_3: | |
953 /* End of CED */ | |
954 fe->timed_step = T38_TIMED_STEP_NONE; | |
955 front_end_status(s, T30_FRONT_END_SEND_STEP_COMPLETE); | |
956 return 0; | |
957 } | |
958 } | |
959 return delay; | |
960 } | |
961 /*- End of function --------------------------------------------------------*/ | |
962 | |
963 static int stream_cng(t31_state_t *s) | |
964 { | |
965 t31_t38_front_end_state_t *fe; | |
966 int delay; | |
967 | |
968 fe = &s->t38_fe; | |
969 for (delay = 0; delay == 0; ) | |
970 { | |
971 switch (fe->timed_step) | |
972 { | |
973 case T38_TIMED_STEP_CNG: | |
974 /* It seems common practice to start with a no signal indicator, though | |
975 this is not a specified requirement of the T.38 spec. Since we should | |
976 be sending 200ms of silence, according to T.30, starting that delay with | |
977 a no signal indication makes sense. */ | |
978 fe->timed_step = T38_TIMED_STEP_CNG_2; | |
979 delay = t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL); | |
980 delay = 200000; | |
981 fe->next_tx_samples = fe->samples; | |
982 break; | |
983 case T38_TIMED_STEP_CNG_2: | |
984 /* Initial short delay over. Send the CNG indicator. CNG persists until something | |
985 coming the other way interrupts it, or a long timeout controlled by the T.30 engine | |
986 expires. */ | |
987 fe->timed_step = T38_TIMED_STEP_NONE; | |
988 delay = t38_core_send_indicator(&fe->t38, T38_IND_CNG); | |
989 fe->current_tx_data_type = T38_DATA_NONE; | |
990 return delay; | |
991 } | |
992 } | |
993 return delay; | |
994 } | |
995 /*- End of function --------------------------------------------------------*/ | |
996 | |
997 SPAN_DECLARE(int) t31_t38_send_timeout(t31_state_t *s, int samples) | |
998 { | |
999 t31_t38_front_end_state_t *fe; | |
1000 int delay; | |
1001 | |
1002 fe = &s->t38_fe; | |
1003 if (fe->current_rx_type == T30_MODEM_DONE || fe->current_tx_type == T30_MODEM_DONE) | |
1004 return TRUE; | |
1005 | |
1006 fe->samples += samples; | |
1007 if (fe->timeout_rx_samples && fe->samples > fe->timeout_rx_samples) | |
1008 { | |
1009 span_log(&s->logging, SPAN_LOG_FLOW, "Timeout mid-receive\n"); | |
1010 fe->timeout_rx_samples = 0; | |
1011 front_end_status(s, T30_FRONT_END_RECEIVE_COMPLETE); | |
1012 } | |
1013 if (fe->timed_step == T38_TIMED_STEP_NONE) | |
1014 return FALSE; | |
1015 /* Wait until the right time comes along, unless we are working in "no delays" mode, while talking to an | |
1016 IAF terminal. */ | |
1017 if (fe->ms_per_tx_chunk && fe->samples < fe->next_tx_samples) | |
1018 return FALSE; | |
1019 /* Its time to send something */ | |
1020 delay = 0; | |
1021 switch (fe->timed_step & 0xFFF0) | |
1022 { | |
1023 case T38_TIMED_STEP_NON_ECM_MODEM: | |
1024 delay = stream_non_ecm(s); | |
1025 break; | |
1026 case T38_TIMED_STEP_HDLC_MODEM: | |
1027 delay = stream_hdlc(s); | |
1028 break; | |
1029 //case T38_TIMED_STEP_FAKE_HDLC_MODEM: | |
1030 // delay = stream_fake_hdlc(s); | |
1031 // break; | |
1032 case T38_TIMED_STEP_CED: | |
1033 delay = stream_ced(s); | |
1034 break; | |
1035 case T38_TIMED_STEP_CNG: | |
1036 delay = stream_cng(s); | |
1037 break; | |
1038 case T38_TIMED_STEP_PAUSE: | |
1039 /* End of timed pause */ | |
1040 fe->timed_step = T38_TIMED_STEP_NONE; | |
1041 front_end_status(s, T30_FRONT_END_SEND_STEP_COMPLETE); | |
1042 break; | |
1043 } | |
1044 fe->next_tx_samples += us_to_samples(delay); | |
1045 return FALSE; | |
1046 } | |
1047 /*- End of function --------------------------------------------------------*/ | |
1048 | |
1049 static int t31_modem_control_handler(at_state_t *s, void *user_data, int op, const char *num) | |
1050 { | |
1051 t31_state_t *t; | |
1052 | |
1053 t = (t31_state_t *) user_data; | |
1054 switch (op) | |
1055 { | |
1056 case AT_MODEM_CONTROL_CALL: | |
1057 t->call_samples = 0; | |
1058 break; | |
1059 case AT_MODEM_CONTROL_ANSWER: | |
1060 t->call_samples = 0; | |
1061 break; | |
1062 case AT_MODEM_CONTROL_ONHOOK: | |
1063 if (t->tx.holding) | |
1064 { | |
1065 t->tx.holding = FALSE; | |
1066 /* Tell the application to release further data */ | |
1067 at_modem_control(&t->at_state, AT_MODEM_CONTROL_CTS, (void *) 1); | |
1068 } | |
1069 if (t->at_state.rx_signal_present) | |
1070 { | |
1071 t->at_state.rx_data[t->at_state.rx_data_bytes++] = DLE; | |
1072 t->at_state.rx_data[t->at_state.rx_data_bytes++] = ETX; | |
1073 t->at_state.at_tx_handler(&t->at_state, | |
1074 t->at_state.at_tx_user_data, | |
1075 t->at_state.rx_data, | |
1076 t->at_state.rx_data_bytes); | |
1077 t->at_state.rx_data_bytes = 0; | |
1078 } | |
1079 restart_modem(t, FAX_MODEM_SILENCE_TX); | |
1080 break; | |
1081 case AT_MODEM_CONTROL_RESTART: | |
1082 restart_modem(t, (int) (intptr_t) num); | |
1083 return 0; | |
1084 case AT_MODEM_CONTROL_DTE_TIMEOUT: | |
1085 if (num) | |
1086 t->dte_data_timeout = t->call_samples + ms_to_samples((intptr_t) num); | |
1087 else | |
1088 t->dte_data_timeout = 0; | |
1089 return 0; | |
1090 } | |
1091 return t->modem_control_handler(t, t->modem_control_user_data, op, num); | |
1092 } | |
1093 /*- End of function --------------------------------------------------------*/ | |
1094 | |
1095 static void non_ecm_rx_status(void *user_data, int status) | |
1096 { | |
1097 t31_state_t *s; | |
1098 | |
1099 s = (t31_state_t *) user_data; | |
1100 switch (status) | |
1101 { | |
1102 case SIG_STATUS_TRAINING_IN_PROGRESS: | |
1103 break; | |
1104 case SIG_STATUS_TRAINING_FAILED: | |
1105 s->at_state.rx_trained = FALSE; | |
1106 break; | |
1107 case SIG_STATUS_TRAINING_SUCCEEDED: | |
1108 /* The modem is now trained */ | |
1109 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT); | |
1110 s->at_state.rx_signal_present = TRUE; | |
1111 s->at_state.rx_trained = TRUE; | |
1112 break; | |
1113 case SIG_STATUS_CARRIER_UP: | |
1114 break; | |
1115 case SIG_STATUS_CARRIER_DOWN: | |
1116 if (s->at_state.rx_signal_present) | |
1117 { | |
1118 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE; | |
1119 s->at_state.rx_data[s->at_state.rx_data_bytes++] = ETX; | |
1120 s->at_state.at_tx_handler(&s->at_state, | |
1121 s->at_state.at_tx_user_data, | |
1122 s->at_state.rx_data, | |
1123 s->at_state.rx_data_bytes); | |
1124 s->at_state.rx_data_bytes = 0; | |
1125 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_NO_CARRIER); | |
1126 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
1127 } | |
1128 s->at_state.rx_signal_present = FALSE; | |
1129 s->at_state.rx_trained = FALSE; | |
1130 break; | |
1131 default: | |
1132 if (s->at_state.p.result_code_format) | |
1133 span_log(&s->logging, SPAN_LOG_FLOW, "Eh!\n"); | |
1134 break; | |
1135 } | |
1136 } | |
1137 /*- End of function --------------------------------------------------------*/ | |
1138 | |
1139 static void non_ecm_put_bit(void *user_data, int bit) | |
1140 { | |
1141 t31_state_t *s; | |
1142 | |
1143 if (bit < 0) | |
1144 { | |
1145 non_ecm_rx_status(user_data, bit); | |
1146 return; | |
1147 } | |
1148 s = (t31_state_t *) user_data; | |
1149 s->audio.current_byte = (s->audio.current_byte >> 1) | (bit << 7); | |
1150 if (++s->audio.bit_no >= 8) | |
1151 { | |
1152 if (s->audio.current_byte == DLE) | |
1153 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE; | |
1154 s->at_state.rx_data[s->at_state.rx_data_bytes++] = (uint8_t) s->audio.current_byte; | |
1155 if (s->at_state.rx_data_bytes >= 250) | |
1156 { | |
1157 s->at_state.at_tx_handler(&s->at_state, | |
1158 s->at_state.at_tx_user_data, | |
1159 s->at_state.rx_data, | |
1160 s->at_state.rx_data_bytes); | |
1161 s->at_state.rx_data_bytes = 0; | |
1162 } | |
1163 s->audio.bit_no = 0; | |
1164 s->audio.current_byte = 0; | |
1165 } | |
1166 } | |
1167 /*- End of function --------------------------------------------------------*/ | |
1168 | |
1169 static void non_ecm_put_chunk(void *user_data, const uint8_t buf[], int len) | |
1170 { | |
1171 t31_state_t *s; | |
1172 int i; | |
1173 | |
1174 s = (t31_state_t *) user_data; | |
1175 /* Ignore any fractional bytes which may have accumulated */ | |
1176 for (i = 0; i < len; i++) | |
1177 { | |
1178 if (buf[i] == DLE) | |
1179 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE; | |
1180 s->at_state.rx_data[s->at_state.rx_data_bytes++] = buf[i]; | |
1181 if (s->at_state.rx_data_bytes >= 250) | |
1182 { | |
1183 s->at_state.at_tx_handler(&s->at_state, | |
1184 s->at_state.at_tx_user_data, | |
1185 s->at_state.rx_data, | |
1186 s->at_state.rx_data_bytes); | |
1187 s->at_state.rx_data_bytes = 0; | |
1188 } | |
1189 } | |
1190 s->audio.bit_no = 0; | |
1191 s->audio.current_byte = 0; | |
1192 } | |
1193 /*- End of function --------------------------------------------------------*/ | |
1194 | |
1195 static int non_ecm_get_bit(void *user_data) | |
1196 { | |
1197 t31_state_t *s; | |
1198 int bit; | |
1199 | |
1200 s = (t31_state_t *) user_data; | |
1201 if (s->audio.bit_no <= 0) | |
1202 { | |
1203 if (s->tx.out_bytes != s->tx.in_bytes) | |
1204 { | |
1205 /* There is real data available to send */ | |
1206 s->audio.current_byte = s->tx.data[s->tx.out_bytes++]; | |
1207 if (s->tx.out_bytes > T31_TX_BUF_LEN - 1) | |
1208 { | |
1209 s->tx.out_bytes = T31_TX_BUF_LEN - 1; | |
1210 span_log(&s->logging, SPAN_LOG_FLOW, "End of transmit buffer reached!\n"); | |
1211 } | |
1212 if (s->tx.holding) | |
1213 { | |
1214 /* See if the buffer is approaching empty. It might be time to | |
1215 release flow control. */ | |
1216 if (s->tx.out_bytes > T31_TX_BUF_LOW_TIDE) | |
1217 { | |
1218 s->tx.holding = FALSE; | |
1219 /* Tell the application to release further data */ | |
1220 at_modem_control(&s->at_state, AT_MODEM_CONTROL_CTS, (void *) 1); | |
1221 } | |
1222 } | |
1223 s->tx.data_started = TRUE; | |
1224 } | |
1225 else | |
1226 { | |
1227 if (s->tx.final) | |
1228 { | |
1229 s->tx.final = FALSE; | |
1230 /* This will put the modem into its shutdown sequence. When | |
1231 it has finally shut down, an OK response will be sent. */ | |
1232 return SIG_STATUS_END_OF_DATA; | |
1233 } | |
1234 /* Fill with 0xFF bytes at the start of transmission, or 0x00 if we are in | |
1235 the middle of transmission. This follows T.31 and T.30 practice. */ | |
1236 s->audio.current_byte = (s->tx.data_started) ? 0x00 : 0xFF; | |
1237 } | |
1238 s->audio.bit_no = 8; | |
1239 } | |
1240 s->audio.bit_no--; | |
1241 bit = s->audio.current_byte & 1; | |
1242 s->audio.current_byte >>= 1; | |
1243 return bit; | |
1244 } | |
1245 /*- End of function --------------------------------------------------------*/ | |
1246 | |
1247 static int non_ecm_get_chunk(void *user_data, uint8_t buf[], int len) | |
1248 { | |
1249 t31_state_t *s; | |
1250 int i; | |
1251 | |
1252 s = (t31_state_t *) user_data; | |
1253 for (i = 0; i < len; i++) | |
1254 { | |
1255 if (s->tx.out_bytes != s->tx.in_bytes) | |
1256 { | |
1257 /* There is real data available to send */ | |
1258 buf[i] = s->tx.data[s->tx.out_bytes++]; | |
1259 if (s->tx.out_bytes > T31_TX_BUF_LEN - 1) | |
1260 { | |
1261 s->tx.out_bytes = T31_TX_BUF_LEN - 1; | |
1262 span_log(&s->logging, SPAN_LOG_FLOW, "End of transmit buffer reached!\n"); | |
1263 } | |
1264 if (s->tx.holding) | |
1265 { | |
1266 /* See if the buffer is approaching empty. It might be time to release flow control. */ | |
1267 if (s->tx.out_bytes > T31_TX_BUF_LOW_TIDE) | |
1268 { | |
1269 s->tx.holding = FALSE; | |
1270 /* Tell the application to release further data */ | |
1271 at_modem_control(&s->at_state, AT_MODEM_CONTROL_CTS, (void *) 1); | |
1272 } | |
1273 } | |
1274 s->tx.data_started = TRUE; | |
1275 } | |
1276 else | |
1277 { | |
1278 if (s->tx.final) | |
1279 { | |
1280 s->tx.final = FALSE; | |
1281 /* This will put the modem into its shutdown sequence. When | |
1282 it has finally shut down, an OK response will be sent. */ | |
1283 //return SIG_STATUS_END_OF_DATA; | |
1284 return i; | |
1285 } | |
1286 /* Fill with 0xFF bytes at the start of transmission, or 0x00 if we are in | |
1287 the middle of transmission. This follows T.31 and T.30 practice. */ | |
1288 buf[i] = (s->tx.data_started) ? 0x00 : 0xFF; | |
1289 } | |
1290 } | |
1291 s->audio.bit_no = 0; | |
1292 s->audio.current_byte = 0; | |
1293 return len; | |
1294 } | |
1295 /*- End of function --------------------------------------------------------*/ | |
1296 | |
1297 static void tone_detected(void *user_data, int tone, int level, int delay) | |
1298 { | |
1299 t31_state_t *s; | |
1300 | |
1301 s = (t31_state_t *) user_data; | |
1302 span_log(&s->logging, SPAN_LOG_FLOW, "%s detected (%ddBm0)\n", modem_connect_tone_to_str(tone), level); | |
1303 } | |
1304 /*- End of function --------------------------------------------------------*/ | |
1305 | |
1306 static void hdlc_tx_underflow(void *user_data) | |
1307 { | |
1308 t31_state_t *s; | |
1309 | |
1310 s = (t31_state_t *) user_data; | |
1311 if (s->hdlc_tx.final) | |
1312 { | |
1313 s->hdlc_tx.final = FALSE; | |
1314 /* Schedule an orderly shutdown of the modem */ | |
1315 hdlc_tx_frame(&(s->audio.modems.hdlc_tx), NULL, 0); | |
1316 } | |
1317 else | |
1318 { | |
1319 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT); | |
1320 } | |
1321 } | |
1322 /*- End of function --------------------------------------------------------*/ | |
1323 | |
1324 static void hdlc_rx_status(void *user_data, int status) | |
1325 { | |
1326 t31_state_t *s; | |
1327 uint8_t buf[2]; | |
1328 | |
1329 s = (t31_state_t *) user_data; | |
1330 switch (status) | |
1331 { | |
1332 case SIG_STATUS_TRAINING_IN_PROGRESS: | |
1333 break; | |
1334 case SIG_STATUS_TRAINING_FAILED: | |
1335 s->at_state.rx_trained = FALSE; | |
1336 break; | |
1337 case SIG_STATUS_TRAINING_SUCCEEDED: | |
1338 /* The modem is now trained */ | |
1339 s->at_state.rx_signal_present = TRUE; | |
1340 s->at_state.rx_trained = TRUE; | |
1341 break; | |
1342 case SIG_STATUS_CARRIER_UP: | |
1343 if (s->modem == FAX_MODEM_CNG_TONE || s->modem == FAX_MODEM_NOCNG_TONE || s->modem == FAX_MODEM_V21_RX) | |
1344 { | |
1345 s->at_state.rx_signal_present = TRUE; | |
1346 s->rx_frame_received = FALSE; | |
1347 } | |
1348 break; | |
1349 case SIG_STATUS_CARRIER_DOWN: | |
1350 if (s->rx_frame_received) | |
1351 { | |
1352 if (s->at_state.dte_is_waiting) | |
1353 { | |
1354 if (s->at_state.ok_is_pending) | |
1355 { | |
1356 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK); | |
1357 s->at_state.ok_is_pending = FALSE; | |
1358 } | |
1359 else | |
1360 { | |
1361 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_NO_CARRIER); | |
1362 } | |
1363 s->at_state.dte_is_waiting = FALSE; | |
1364 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
1365 } | |
1366 else | |
1367 { | |
1368 buf[0] = AT_RESPONSE_CODE_NO_CARRIER; | |
1369 queue_write_msg(s->rx_queue, buf, 1); | |
1370 } | |
1371 } | |
1372 s->at_state.rx_signal_present = FALSE; | |
1373 s->at_state.rx_trained = FALSE; | |
1374 break; | |
1375 case SIG_STATUS_FRAMING_OK: | |
1376 if (s->modem == FAX_MODEM_CNG_TONE || s->modem == FAX_MODEM_NOCNG_TONE) | |
1377 { | |
1378 /* Once we get any valid HDLC the CNG tone stops, and we drop | |
1379 to the V.21 receive modem on its own. */ | |
1380 s->modem = FAX_MODEM_V21_RX; | |
1381 s->at_state.transmit = FALSE; | |
1382 } | |
1383 if (s->modem == FAX_MODEM_V17_RX || s->modem == FAX_MODEM_V27TER_RX || s->modem == FAX_MODEM_V29_RX) | |
1384 { | |
1385 /* V.21 has been detected while expecting a different carrier. | |
1386 If +FAR=0 then result +FCERROR and return to command-mode. | |
1387 If +FAR=1 then report +FRH:3 and CONNECT, switching to | |
1388 V.21 receive mode. */ | |
1389 if (s->at_state.p.adaptive_receive) | |
1390 { | |
1391 s->at_state.rx_signal_present = TRUE; | |
1392 s->rx_frame_received = TRUE; | |
1393 s->modem = FAX_MODEM_V21_RX; | |
1394 s->at_state.transmit = FALSE; | |
1395 s->at_state.dte_is_waiting = TRUE; | |
1396 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_FRH3); | |
1397 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT); | |
1398 } | |
1399 else | |
1400 { | |
1401 s->modem = FAX_MODEM_SILENCE_TX; | |
1402 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
1403 s->rx_frame_received = FALSE; | |
1404 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_FCERROR); | |
1405 } | |
1406 } | |
1407 else | |
1408 { | |
1409 if (!s->rx_frame_received) | |
1410 { | |
1411 if (s->at_state.dte_is_waiting) | |
1412 { | |
1413 /* Report CONNECT as soon as possible to avoid a timeout. */ | |
1414 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT); | |
1415 s->rx_frame_received = TRUE; | |
1416 } | |
1417 else | |
1418 { | |
1419 buf[0] = AT_RESPONSE_CODE_CONNECT; | |
1420 queue_write_msg(s->rx_queue, buf, 1); | |
1421 } | |
1422 } | |
1423 } | |
1424 break; | |
1425 case SIG_STATUS_ABORT: | |
1426 /* Just ignore these */ | |
1427 break; | |
1428 default: | |
1429 span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected HDLC rx status - %d!\n", status); | |
1430 break; | |
1431 } | |
1432 } | |
1433 /*- End of function --------------------------------------------------------*/ | |
1434 | |
1435 static void hdlc_accept_frame(void *user_data, const uint8_t *msg, int len, int ok) | |
1436 { | |
1437 t31_state_t *s; | |
1438 uint8_t buf[256]; | |
1439 int i; | |
1440 | |
1441 if (len < 0) | |
1442 { | |
1443 hdlc_rx_status(user_data, len); | |
1444 return; | |
1445 } | |
1446 s = (t31_state_t *) user_data; | |
1447 if (!s->rx_frame_received) | |
1448 { | |
1449 if (s->at_state.dte_is_waiting) | |
1450 { | |
1451 /* Report CONNECT as soon as possible to avoid a timeout. */ | |
1452 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT); | |
1453 s->rx_frame_received = TRUE; | |
1454 } | |
1455 else | |
1456 { | |
1457 buf[0] = AT_RESPONSE_CODE_CONNECT; | |
1458 queue_write_msg(s->rx_queue, buf, 1); | |
1459 } | |
1460 } | |
1461 /* If OK is pending then we just ignore whatever comes in */ | |
1462 if (!s->at_state.ok_is_pending) | |
1463 { | |
1464 if (s->at_state.dte_is_waiting) | |
1465 { | |
1466 /* Send straight away */ | |
1467 /* It is safe to look at the two bytes beyond the length of the message, | |
1468 and expect to find the FCS there. */ | |
1469 for (i = 0; i < len + 2; i++) | |
1470 { | |
1471 if (msg[i] == DLE) | |
1472 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE; | |
1473 s->at_state.rx_data[s->at_state.rx_data_bytes++] = msg[i]; | |
1474 } | |
1475 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE; | |
1476 s->at_state.rx_data[s->at_state.rx_data_bytes++] = ETX; | |
1477 s->at_state.at_tx_handler(&s->at_state, s->at_state.at_tx_user_data, s->at_state.rx_data, s->at_state.rx_data_bytes); | |
1478 s->at_state.rx_data_bytes = 0; | |
1479 if (msg[1] == 0x13 && ok) | |
1480 { | |
1481 /* This is the last frame. We don't send OK until the carrier drops to avoid | |
1482 redetecting it later. */ | |
1483 s->at_state.ok_is_pending = TRUE; | |
1484 } | |
1485 else | |
1486 { | |
1487 at_put_response_code(&s->at_state, (ok) ? AT_RESPONSE_CODE_OK : AT_RESPONSE_CODE_ERROR); | |
1488 s->at_state.dte_is_waiting = FALSE; | |
1489 s->rx_frame_received = FALSE; | |
1490 } | |
1491 } | |
1492 else | |
1493 { | |
1494 /* Queue it */ | |
1495 buf[0] = (ok) ? AT_RESPONSE_CODE_OK : AT_RESPONSE_CODE_ERROR; | |
1496 /* It is safe to look at the two bytes beyond the length of the message, | |
1497 and expect to find the FCS there. */ | |
1498 memcpy(buf + 1, msg, len + 2); | |
1499 queue_write_msg(s->rx_queue, buf, len + 3); | |
1500 } | |
1501 } | |
1502 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
1503 } | |
1504 /*- End of function --------------------------------------------------------*/ | |
1505 | |
1506 static void t31_v21_rx(t31_state_t *s) | |
1507 { | |
1508 s->at_state.ok_is_pending = FALSE; | |
1509 s->hdlc_tx.final = FALSE; | |
1510 s->hdlc_tx.len = 0; | |
1511 s->dled = FALSE; | |
1512 hdlc_rx_init(&(s->audio.modems.hdlc_rx), FALSE, TRUE, HDLC_FRAMING_OK_THRESHOLD, hdlc_accept_frame, s); | |
1513 fsk_rx_init(&(s->audio.modems.v21_rx), &preset_fsk_specs[FSK_V21CH2], FSK_FRAME_MODE_SYNC, (put_bit_func_t) hdlc_rx_put_bit, &(s->audio.modems.hdlc_rx)); | |
1514 fsk_rx_signal_cutoff(&(s->audio.modems.v21_rx), -39.09f); | |
1515 s->at_state.transmit = TRUE; | |
1516 } | |
1517 /*- End of function --------------------------------------------------------*/ | |
1518 | |
1519 static int restart_modem(t31_state_t *s, int new_modem) | |
1520 { | |
1521 int use_hdlc; | |
1522 fax_modems_state_t *t; | |
1523 | |
1524 t = &s->audio.modems; | |
1525 span_log(&s->logging, SPAN_LOG_FLOW, "Restart modem %d\n", new_modem); | |
1526 if (s->modem == new_modem) | |
1527 return 0; | |
1528 queue_flush(s->rx_queue); | |
1529 s->modem = new_modem; | |
1530 s->tx.final = FALSE; | |
1531 s->at_state.rx_signal_present = FALSE; | |
1532 s->at_state.rx_trained = FALSE; | |
1533 s->rx_frame_received = FALSE; | |
1534 set_rx_handler(s, (span_rx_handler_t *) &span_dummy_rx, (span_rx_fillin_handler_t *) &span_dummy_rx_fillin, NULL); | |
1535 use_hdlc = FALSE; | |
1536 switch (s->modem) | |
1537 { | |
1538 case FAX_MODEM_CNG_TONE: | |
1539 if (s->t38_mode) | |
1540 { | |
1541 s->t38_fe.next_tx_samples = s->t38_fe.samples; | |
1542 s->t38_fe.timed_step = T38_TIMED_STEP_CNG; | |
1543 s->t38_fe.current_tx_data_type = T38_DATA_NONE; | |
1544 } | |
1545 else | |
1546 { | |
1547 modem_connect_tones_tx_init(&t->connect_tx, MODEM_CONNECT_TONES_FAX_CNG); | |
1548 /* CNG is special, since we need to receive V.21 HDLC messages while sending the | |
1549 tone. Everything else in FAX processing sends only one way at a time. */ | |
1550 /* Do V.21/HDLC receive in parallel. The other end may send its | |
1551 first message at any time. The CNG tone will continue until | |
1552 we get a valid preamble. */ | |
1553 set_rx_handler(s, (span_rx_handler_t *) &cng_rx, (span_rx_fillin_handler_t *) &span_dummy_rx_fillin, s); | |
1554 t31_v21_rx(s); | |
1555 set_tx_handler(s, (span_tx_handler_t *) &modem_connect_tones_tx, &t->connect_tx); | |
1556 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
1557 } | |
1558 s->at_state.transmit = TRUE; | |
1559 break; | |
1560 case FAX_MODEM_NOCNG_TONE: | |
1561 if (s->t38_mode) | |
1562 { | |
1563 } | |
1564 else | |
1565 { | |
1566 set_rx_handler(s, (span_rx_handler_t *) &cng_rx, (span_rx_fillin_handler_t *) &span_dummy_rx_fillin, s); | |
1567 t31_v21_rx(s); | |
1568 silence_gen_set(&t->silence_gen, 0); | |
1569 set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); | |
1570 } | |
1571 s->at_state.transmit = FALSE; | |
1572 break; | |
1573 case FAX_MODEM_CED_TONE: | |
1574 if (s->t38_mode) | |
1575 { | |
1576 s->t38_fe.next_tx_samples = s->t38_fe.samples; | |
1577 s->t38_fe.timed_step = T38_TIMED_STEP_CED; | |
1578 s->t38_fe.current_tx_data_type = T38_DATA_NONE; | |
1579 } | |
1580 else | |
1581 { | |
1582 modem_connect_tones_tx_init(&t->connect_tx, MODEM_CONNECT_TONES_FAX_CED); | |
1583 set_tx_handler(s, (span_tx_handler_t *) &modem_connect_tones_tx, &t->connect_tx); | |
1584 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
1585 } | |
1586 s->at_state.transmit = TRUE; | |
1587 break; | |
1588 case FAX_MODEM_V21_TX: | |
1589 if (s->t38_mode) | |
1590 { | |
1591 s->t38_fe.next_tx_indicator = T38_IND_V21_PREAMBLE; | |
1592 s->t38_fe.current_tx_data_type = T38_DATA_V21; | |
1593 use_hdlc = TRUE; | |
1594 s->t38_fe.timed_step = (use_hdlc) ? T38_TIMED_STEP_HDLC_MODEM : T38_TIMED_STEP_NON_ECM_MODEM; | |
1595 set_octets_per_data_packet(s, 300); | |
1596 } | |
1597 else | |
1598 { | |
1599 hdlc_tx_init(&t->hdlc_tx, FALSE, 2, FALSE, hdlc_tx_underflow, s); | |
1600 /* The spec says 1s +-15% of preamble. So, the minimum is 32 octets. */ | |
1601 hdlc_tx_flags(&t->hdlc_tx, 32); | |
1602 fsk_tx_init(&t->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &t->hdlc_tx); | |
1603 set_tx_handler(s, (span_tx_handler_t *) &fsk_tx, &t->v21_tx); | |
1604 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
1605 } | |
1606 s->hdlc_tx.final = FALSE; | |
1607 s->hdlc_tx.len = 0; | |
1608 s->dled = FALSE; | |
1609 s->at_state.transmit = TRUE; | |
1610 break; | |
1611 case FAX_MODEM_V21_RX: | |
1612 if (s->t38_mode) | |
1613 { | |
1614 } | |
1615 else | |
1616 { | |
1617 set_rx_handler(s, (span_rx_handler_t *) &fsk_rx, (span_rx_fillin_handler_t *) &fsk_rx_fillin, &t->v21_rx); | |
1618 t31_v21_rx(s); | |
1619 } | |
1620 break; | |
1621 case FAX_MODEM_V17_TX: | |
1622 if (s->t38_mode) | |
1623 { | |
1624 switch (s->bit_rate) | |
1625 { | |
1626 case 7200: | |
1627 s->t38_fe.next_tx_indicator = (s->short_train) ? T38_IND_V17_7200_SHORT_TRAINING : T38_IND_V17_7200_LONG_TRAINING; | |
1628 s->t38_fe.current_tx_data_type = T38_DATA_V17_7200; | |
1629 break; | |
1630 case 9600: | |
1631 s->t38_fe.next_tx_indicator = (s->short_train) ? T38_IND_V17_9600_SHORT_TRAINING : T38_IND_V17_9600_LONG_TRAINING; | |
1632 s->t38_fe.current_tx_data_type = T38_DATA_V17_9600; | |
1633 break; | |
1634 case 12000: | |
1635 s->t38_fe.next_tx_indicator = (s->short_train) ? T38_IND_V17_12000_SHORT_TRAINING : T38_IND_V17_12000_LONG_TRAINING; | |
1636 s->t38_fe.current_tx_data_type = T38_DATA_V17_12000; | |
1637 break; | |
1638 case 14400: | |
1639 s->t38_fe.next_tx_indicator = (s->short_train) ? T38_IND_V17_14400_SHORT_TRAINING : T38_IND_V17_14400_LONG_TRAINING; | |
1640 s->t38_fe.current_tx_data_type = T38_DATA_V17_14400; | |
1641 break; | |
1642 } | |
1643 set_octets_per_data_packet(s, s->bit_rate); | |
1644 s->t38_fe.timed_step = (use_hdlc) ? T38_TIMED_STEP_HDLC_MODEM : T38_TIMED_STEP_NON_ECM_MODEM; | |
1645 } | |
1646 else | |
1647 { | |
1648 v17_tx_restart(&t->v17_tx, s->bit_rate, FALSE, s->short_train); | |
1649 set_tx_handler(s, (span_tx_handler_t *) &v17_tx, &t->v17_tx); | |
1650 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
1651 } | |
1652 s->tx.out_bytes = 0; | |
1653 s->tx.data_started = FALSE; | |
1654 s->at_state.transmit = TRUE; | |
1655 break; | |
1656 case FAX_MODEM_V17_RX: | |
1657 if (!s->t38_mode) | |
1658 { | |
1659 set_rx_handler(s, (span_rx_handler_t *) &v17_v21_rx, (span_rx_fillin_handler_t *) &v17_v21_rx_fillin, s); | |
1660 v17_rx_restart(&t->v17_rx, s->bit_rate, s->short_train); | |
1661 /* Allow for +FCERROR/+FRH:3 */ | |
1662 t31_v21_rx(s); | |
1663 } | |
1664 s->at_state.transmit = FALSE; | |
1665 break; | |
1666 case FAX_MODEM_V27TER_TX: | |
1667 if (s->t38_mode) | |
1668 { | |
1669 switch (s->bit_rate) | |
1670 { | |
1671 case 2400: | |
1672 s->t38_fe.next_tx_indicator = T38_IND_V27TER_2400_TRAINING; | |
1673 s->t38_fe.current_tx_data_type = T38_DATA_V27TER_2400; | |
1674 break; | |
1675 case 4800: | |
1676 s->t38_fe.next_tx_indicator = T38_IND_V27TER_4800_TRAINING; | |
1677 s->t38_fe.current_tx_data_type = T38_DATA_V27TER_4800; | |
1678 break; | |
1679 } | |
1680 set_octets_per_data_packet(s, s->bit_rate); | |
1681 s->t38_fe.timed_step = (use_hdlc) ? T38_TIMED_STEP_HDLC_MODEM : T38_TIMED_STEP_NON_ECM_MODEM; | |
1682 } | |
1683 else | |
1684 { | |
1685 v27ter_tx_restart(&t->v27ter_tx, s->bit_rate, FALSE); | |
1686 set_tx_handler(s, (span_tx_handler_t *) &v27ter_tx, &t->v27ter_tx); | |
1687 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
1688 } | |
1689 s->tx.out_bytes = 0; | |
1690 s->tx.data_started = FALSE; | |
1691 s->at_state.transmit = TRUE; | |
1692 break; | |
1693 case FAX_MODEM_V27TER_RX: | |
1694 if (!s->t38_mode) | |
1695 { | |
1696 set_rx_handler(s, (span_rx_handler_t *) &v27ter_v21_rx, (span_rx_fillin_handler_t *) &v27ter_v21_rx_fillin, s); | |
1697 v27ter_rx_restart(&t->v27ter_rx, s->bit_rate, FALSE); | |
1698 /* Allow for +FCERROR/+FRH:3 */ | |
1699 t31_v21_rx(s); | |
1700 } | |
1701 s->at_state.transmit = FALSE; | |
1702 break; | |
1703 case FAX_MODEM_V29_TX: | |
1704 if (s->t38_mode) | |
1705 { | |
1706 switch (s->bit_rate) | |
1707 { | |
1708 case 7200: | |
1709 s->t38_fe.next_tx_indicator = T38_IND_V29_7200_TRAINING; | |
1710 s->t38_fe.current_tx_data_type = T38_DATA_V29_7200; | |
1711 break; | |
1712 case 9600: | |
1713 s->t38_fe.next_tx_indicator = T38_IND_V29_9600_TRAINING; | |
1714 s->t38_fe.current_tx_data_type = T38_DATA_V29_9600; | |
1715 break; | |
1716 } | |
1717 set_octets_per_data_packet(s, s->bit_rate); | |
1718 s->t38_fe.timed_step = (use_hdlc) ? T38_TIMED_STEP_HDLC_MODEM : T38_TIMED_STEP_NON_ECM_MODEM; | |
1719 } | |
1720 else | |
1721 { | |
1722 v29_tx_restart(&t->v29_tx, s->bit_rate, FALSE); | |
1723 set_tx_handler(s, (span_tx_handler_t *) &v29_tx, &t->v29_tx); | |
1724 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
1725 } | |
1726 s->tx.out_bytes = 0; | |
1727 s->tx.data_started = FALSE; | |
1728 s->at_state.transmit = TRUE; | |
1729 break; | |
1730 case FAX_MODEM_V29_RX: | |
1731 if (!s->t38_mode) | |
1732 { | |
1733 set_rx_handler(s, (span_rx_handler_t *) &v29_v21_rx, (span_rx_fillin_handler_t *) &v29_v21_rx_fillin, s); | |
1734 v29_rx_restart(&t->v29_rx, s->bit_rate, FALSE); | |
1735 /* Allow for +FCERROR/+FRH:3 */ | |
1736 t31_v21_rx(s); | |
1737 } | |
1738 s->at_state.transmit = FALSE; | |
1739 break; | |
1740 case FAX_MODEM_SILENCE_TX: | |
1741 if (s->t38_mode) | |
1742 { | |
1743 t38_core_send_indicator(&s->t38_fe.t38, T38_IND_NO_SIGNAL); | |
1744 s->t38_fe.next_tx_samples = s->t38_fe.samples + ms_to_samples(700); | |
1745 s->t38_fe.timed_step = T38_TIMED_STEP_PAUSE; | |
1746 s->t38_fe.current_tx_data_type = T38_DATA_NONE; | |
1747 } | |
1748 else | |
1749 { | |
1750 silence_gen_set(&t->silence_gen, 0); | |
1751 set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); | |
1752 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
1753 } | |
1754 s->at_state.transmit = FALSE; | |
1755 break; | |
1756 case FAX_MODEM_SILENCE_RX: | |
1757 if (!s->t38_mode) | |
1758 { | |
1759 set_rx_handler(s, (span_rx_handler_t *) &silence_rx, (span_rx_fillin_handler_t *) &span_dummy_rx_fillin, s); | |
1760 silence_gen_set(&t->silence_gen, 0); | |
1761 set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); | |
1762 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
1763 } | |
1764 s->at_state.transmit = FALSE; | |
1765 break; | |
1766 case FAX_MODEM_FLUSH: | |
1767 /* Send 200ms of silence to "push" the last audio out */ | |
1768 if (s->t38_mode) | |
1769 { | |
1770 t38_core_send_indicator(&s->t38_fe.t38, T38_IND_NO_SIGNAL); | |
1771 } | |
1772 else | |
1773 { | |
1774 s->modem = FAX_MODEM_SILENCE_TX; | |
1775 silence_gen_alter(&t->silence_gen, ms_to_samples(200)); | |
1776 set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &t->silence_gen); | |
1777 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
1778 s->at_state.transmit = TRUE; | |
1779 } | |
1780 break; | |
1781 } | |
1782 s->audio.bit_no = 0; | |
1783 s->audio.current_byte = 0xFF; | |
1784 s->tx.in_bytes = 0; | |
1785 s->tx.out_bytes = 0; | |
1786 return 0; | |
1787 } | |
1788 /*- End of function --------------------------------------------------------*/ | |
1789 | |
1790 static __inline__ void dle_unstuff_hdlc(t31_state_t *s, const char *stuffed, int len) | |
1791 { | |
1792 int i; | |
1793 | |
1794 for (i = 0; i < len; i++) | |
1795 { | |
1796 if (s->dled) | |
1797 { | |
1798 s->dled = FALSE; | |
1799 if (stuffed[i] == ETX) | |
1800 { | |
1801 s->hdlc_tx.final = (s->hdlc_tx.buf[1] & 0x10); | |
1802 if (s->t38_mode) | |
1803 { | |
1804 send_hdlc(s, s->hdlc_tx.buf, s->hdlc_tx.len); | |
1805 } | |
1806 else | |
1807 { | |
1808 hdlc_tx_frame(&(s->audio.modems.hdlc_tx), s->hdlc_tx.buf, s->hdlc_tx.len); | |
1809 s->hdlc_tx.len = 0; | |
1810 } | |
1811 } | |
1812 else if (stuffed[i] == SUB) | |
1813 { | |
1814 s->hdlc_tx.buf[s->hdlc_tx.len++] = DLE; | |
1815 s->hdlc_tx.buf[s->hdlc_tx.len++] = DLE; | |
1816 } | |
1817 else | |
1818 { | |
1819 s->hdlc_tx.buf[s->hdlc_tx.len++] = stuffed[i]; | |
1820 } | |
1821 } | |
1822 else | |
1823 { | |
1824 if (stuffed[i] == DLE) | |
1825 s->dled = TRUE; | |
1826 else | |
1827 s->hdlc_tx.buf[s->hdlc_tx.len++] = stuffed[i]; | |
1828 } | |
1829 } | |
1830 } | |
1831 /*- End of function --------------------------------------------------------*/ | |
1832 | |
1833 static __inline__ void dle_unstuff(t31_state_t *s, const char *stuffed, int len) | |
1834 { | |
1835 int i; | |
1836 | |
1837 for (i = 0; i < len; i++) | |
1838 { | |
1839 if (s->dled) | |
1840 { | |
1841 s->dled = FALSE; | |
1842 if (stuffed[i] == ETX) | |
1843 { | |
1844 s->tx.final = TRUE; | |
1845 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
1846 return; | |
1847 } | |
1848 } | |
1849 else if (stuffed[i] == DLE) | |
1850 { | |
1851 s->dled = TRUE; | |
1852 continue; | |
1853 } | |
1854 s->tx.data[s->tx.in_bytes++] = stuffed[i]; | |
1855 if (s->tx.in_bytes > T31_TX_BUF_LEN - 1) | |
1856 { | |
1857 /* Oops. We hit the end of the buffer. Give up. Loose stuff. :-( */ | |
1858 span_log(&s->logging, SPAN_LOG_FLOW, "No room in buffer for new data!\n"); | |
1859 return; | |
1860 } | |
1861 } | |
1862 if (!s->tx.holding) | |
1863 { | |
1864 /* See if the buffer is approaching full. We might need to apply flow control. */ | |
1865 if (s->tx.in_bytes > T31_TX_BUF_HIGH_TIDE) | |
1866 { | |
1867 s->tx.holding = TRUE; | |
1868 /* Tell the application to hold further data */ | |
1869 at_modem_control(&s->at_state, AT_MODEM_CONTROL_CTS, (void *) 0); | |
1870 } | |
1871 } | |
1872 } | |
1873 /*- End of function --------------------------------------------------------*/ | |
1874 | |
1875 static int process_class1_cmd(at_state_t *t, void *user_data, int direction, int operation, int val) | |
1876 { | |
1877 int new_modem; | |
1878 int new_transmit; | |
1879 int i; | |
1880 int len; | |
1881 int immediate_response; | |
1882 t31_state_t *s; | |
1883 uint8_t msg[256]; | |
1884 | |
1885 s = (t31_state_t *) user_data; | |
1886 new_transmit = direction; | |
1887 immediate_response = TRUE; | |
1888 switch (operation) | |
1889 { | |
1890 case 'S': | |
1891 s->at_state.transmit = new_transmit; | |
1892 if (new_transmit) | |
1893 { | |
1894 /* Send a specified period of silence, to space transmissions. */ | |
1895 restart_modem(s, FAX_MODEM_SILENCE_TX); | |
1896 if (s->t38_mode) | |
1897 s->t38_fe.next_tx_samples = s->t38_fe.samples + ms_to_samples(val*10); | |
1898 else | |
1899 silence_gen_alter(&(s->audio.modems.silence_gen), ms_to_samples(val*10)); | |
1900 s->at_state.transmit = TRUE; | |
1901 } | |
1902 else | |
1903 { | |
1904 /* Wait until we have received a specified period of silence. */ | |
1905 queue_flush(s->rx_queue); | |
1906 s->silence_awaited = ms_to_samples(val*10); | |
1907 t31_set_at_rx_mode(s, AT_MODE_DELIVERY); | |
1908 if (s->t38_mode) | |
1909 { | |
1910 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK); | |
1911 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
1912 } | |
1913 else | |
1914 { | |
1915 restart_modem(s, FAX_MODEM_SILENCE_RX); | |
1916 } | |
1917 } | |
1918 immediate_response = FALSE; | |
1919 span_log(&s->logging, SPAN_LOG_FLOW, "Silence %dms\n", val*10); | |
1920 break; | |
1921 case 'H': | |
1922 switch (val) | |
1923 { | |
1924 case 3: | |
1925 new_modem = (new_transmit) ? FAX_MODEM_V21_TX : FAX_MODEM_V21_RX; | |
1926 s->short_train = FALSE; | |
1927 s->bit_rate = 300; | |
1928 break; | |
1929 default: | |
1930 return -1; | |
1931 } | |
1932 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC\n"); | |
1933 if (new_modem != s->modem) | |
1934 { | |
1935 restart_modem(s, new_modem); | |
1936 immediate_response = FALSE; | |
1937 } | |
1938 s->at_state.transmit = new_transmit; | |
1939 if (new_transmit) | |
1940 { | |
1941 t31_set_at_rx_mode(s, AT_MODE_HDLC); | |
1942 if (!s->t38_mode) | |
1943 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT); | |
1944 } | |
1945 else | |
1946 { | |
1947 /* Send straight away, if there is something queued. */ | |
1948 t31_set_at_rx_mode(s, AT_MODE_DELIVERY); | |
1949 s->rx_frame_received = FALSE; | |
1950 do | |
1951 { | |
1952 if (!queue_empty(s->rx_queue)) | |
1953 { | |
1954 len = queue_read_msg(s->rx_queue, msg, 256); | |
1955 if (len > 1) | |
1956 { | |
1957 if (msg[0] == AT_RESPONSE_CODE_OK) | |
1958 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT); | |
1959 for (i = 1; i < len; i++) | |
1960 { | |
1961 if (msg[i] == DLE) | |
1962 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE; | |
1963 s->at_state.rx_data[s->at_state.rx_data_bytes++] = msg[i]; | |
1964 } | |
1965 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE; | |
1966 s->at_state.rx_data[s->at_state.rx_data_bytes++] = ETX; | |
1967 s->at_state.at_tx_handler(&s->at_state, s->at_state.at_tx_user_data, s->at_state.rx_data, s->at_state.rx_data_bytes); | |
1968 s->at_state.rx_data_bytes = 0; | |
1969 } | |
1970 at_put_response_code(&s->at_state, msg[0]); | |
1971 } | |
1972 else | |
1973 { | |
1974 s->at_state.dte_is_waiting = TRUE; | |
1975 break; | |
1976 } | |
1977 } | |
1978 while (msg[0] == AT_RESPONSE_CODE_CONNECT); | |
1979 } | |
1980 immediate_response = FALSE; | |
1981 break; | |
1982 default: | |
1983 switch (val) | |
1984 { | |
1985 case 24: | |
1986 s->t38_fe.next_tx_indicator = T38_IND_V27TER_2400_TRAINING; | |
1987 s->t38_fe.current_tx_data_type = T38_DATA_V27TER_2400; | |
1988 new_modem = (new_transmit) ? FAX_MODEM_V27TER_TX : FAX_MODEM_V27TER_RX; | |
1989 s->short_train = FALSE; | |
1990 s->bit_rate = 2400; | |
1991 break; | |
1992 case 48: | |
1993 s->t38_fe.next_tx_indicator = T38_IND_V27TER_4800_TRAINING; | |
1994 s->t38_fe.current_tx_data_type = T38_DATA_V27TER_4800; | |
1995 new_modem = (new_transmit) ? FAX_MODEM_V27TER_TX : FAX_MODEM_V27TER_RX; | |
1996 s->short_train = FALSE; | |
1997 s->bit_rate = 4800; | |
1998 break; | |
1999 case 72: | |
2000 s->t38_fe.next_tx_indicator = T38_IND_V29_7200_TRAINING; | |
2001 s->t38_fe.current_tx_data_type = T38_DATA_V29_7200; | |
2002 new_modem = (new_transmit) ? FAX_MODEM_V29_TX : FAX_MODEM_V29_RX; | |
2003 s->short_train = FALSE; | |
2004 s->bit_rate = 7200; | |
2005 break; | |
2006 case 96: | |
2007 s->t38_fe.next_tx_indicator = T38_IND_V29_9600_TRAINING; | |
2008 s->t38_fe.current_tx_data_type = T38_DATA_V29_9600; | |
2009 new_modem = (new_transmit) ? FAX_MODEM_V29_TX : FAX_MODEM_V29_RX; | |
2010 s->short_train = FALSE; | |
2011 s->bit_rate = 9600; | |
2012 break; | |
2013 case 73: | |
2014 s->t38_fe.next_tx_indicator = T38_IND_V17_7200_LONG_TRAINING; | |
2015 s->t38_fe.current_tx_data_type = T38_DATA_V17_7200; | |
2016 new_modem = (new_transmit) ? FAX_MODEM_V17_TX : FAX_MODEM_V17_RX; | |
2017 s->short_train = FALSE; | |
2018 s->bit_rate = 7200; | |
2019 break; | |
2020 case 74: | |
2021 s->t38_fe.next_tx_indicator = T38_IND_V17_7200_SHORT_TRAINING; | |
2022 s->t38_fe.current_tx_data_type = T38_DATA_V17_7200; | |
2023 new_modem = (new_transmit) ? FAX_MODEM_V17_TX : FAX_MODEM_V17_RX; | |
2024 s->short_train = TRUE; | |
2025 s->bit_rate = 7200; | |
2026 break; | |
2027 case 97: | |
2028 s->t38_fe.next_tx_indicator = T38_IND_V17_9600_LONG_TRAINING; | |
2029 s->t38_fe.current_tx_data_type = T38_DATA_V17_9600; | |
2030 new_modem = (new_transmit) ? FAX_MODEM_V17_TX : FAX_MODEM_V17_RX; | |
2031 s->short_train = FALSE; | |
2032 s->bit_rate = 9600; | |
2033 break; | |
2034 case 98: | |
2035 s->t38_fe.next_tx_indicator = T38_IND_V17_9600_SHORT_TRAINING; | |
2036 s->t38_fe.current_tx_data_type = T38_DATA_V17_9600; | |
2037 new_modem = (new_transmit) ? FAX_MODEM_V17_TX : FAX_MODEM_V17_RX; | |
2038 s->short_train = TRUE; | |
2039 s->bit_rate = 9600; | |
2040 break; | |
2041 case 121: | |
2042 s->t38_fe.next_tx_indicator = T38_IND_V17_12000_LONG_TRAINING; | |
2043 s->t38_fe.current_tx_data_type = T38_DATA_V17_12000; | |
2044 new_modem = (new_transmit) ? FAX_MODEM_V17_TX : FAX_MODEM_V17_RX; | |
2045 s->short_train = FALSE; | |
2046 s->bit_rate = 12000; | |
2047 break; | |
2048 case 122: | |
2049 s->t38_fe.next_tx_indicator = T38_IND_V17_12000_SHORT_TRAINING; | |
2050 s->t38_fe.current_tx_data_type = T38_DATA_V17_12000; | |
2051 new_modem = (new_transmit) ? FAX_MODEM_V17_TX : FAX_MODEM_V17_RX; | |
2052 s->short_train = TRUE; | |
2053 s->bit_rate = 12000; | |
2054 break; | |
2055 case 145: | |
2056 s->t38_fe.next_tx_indicator = T38_IND_V17_14400_LONG_TRAINING; | |
2057 s->t38_fe.current_tx_data_type = T38_DATA_V17_14400; | |
2058 new_modem = (new_transmit) ? FAX_MODEM_V17_TX : FAX_MODEM_V17_RX; | |
2059 s->short_train = FALSE; | |
2060 s->bit_rate = 14400; | |
2061 break; | |
2062 case 146: | |
2063 s->t38_fe.next_tx_indicator = T38_IND_V17_14400_SHORT_TRAINING; | |
2064 s->t38_fe.current_tx_data_type = T38_DATA_V17_14400; | |
2065 new_modem = (new_transmit) ? FAX_MODEM_V17_TX : FAX_MODEM_V17_RX; | |
2066 s->short_train = TRUE; | |
2067 s->bit_rate = 14400; | |
2068 break; | |
2069 default: | |
2070 return -1; | |
2071 } | |
2072 span_log(&s->logging, SPAN_LOG_FLOW, "Short training = %d, bit rate = %d\n", s->short_train, s->bit_rate); | |
2073 if (new_transmit) | |
2074 { | |
2075 t31_set_at_rx_mode(s, AT_MODE_STUFFED); | |
2076 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT); | |
2077 } | |
2078 else | |
2079 { | |
2080 t31_set_at_rx_mode(s, AT_MODE_DELIVERY); | |
2081 } | |
2082 restart_modem(s, new_modem); | |
2083 immediate_response = FALSE; | |
2084 break; | |
2085 } | |
2086 return immediate_response; | |
2087 } | |
2088 /*- End of function --------------------------------------------------------*/ | |
2089 | |
2090 SPAN_DECLARE(void) t31_call_event(t31_state_t *s, int event) | |
2091 { | |
2092 span_log(&s->logging, SPAN_LOG_FLOW, "Call event %d received\n", event); | |
2093 at_call_event(&s->at_state, event); | |
2094 } | |
2095 /*- End of function --------------------------------------------------------*/ | |
2096 | |
2097 SPAN_DECLARE(int) t31_at_rx(t31_state_t *s, const char *t, int len) | |
2098 { | |
2099 if (s->dte_data_timeout) | |
2100 s->dte_data_timeout = s->call_samples + ms_to_samples(5000); | |
2101 switch (s->at_state.at_rx_mode) | |
2102 { | |
2103 case AT_MODE_ONHOOK_COMMAND: | |
2104 case AT_MODE_OFFHOOK_COMMAND: | |
2105 at_interpreter(&s->at_state, t, len); | |
2106 break; | |
2107 case AT_MODE_DELIVERY: | |
2108 /* Data from the DTE in this state returns us to command mode */ | |
2109 if (len) | |
2110 { | |
2111 if (s->at_state.rx_signal_present) | |
2112 { | |
2113 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE; | |
2114 s->at_state.rx_data[s->at_state.rx_data_bytes++] = ETX; | |
2115 s->at_state.at_tx_handler(&s->at_state, s->at_state.at_tx_user_data, s->at_state.rx_data, s->at_state.rx_data_bytes); | |
2116 } | |
2117 s->at_state.rx_data_bytes = 0; | |
2118 s->at_state.transmit = FALSE; | |
2119 s->modem = FAX_MODEM_SILENCE_TX; | |
2120 set_rx_handler(s, (span_rx_handler_t *) &span_dummy_rx, (span_rx_fillin_handler_t *) &span_dummy_rx_fillin, NULL); | |
2121 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
2122 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK); | |
2123 } | |
2124 break; | |
2125 case AT_MODE_HDLC: | |
2126 dle_unstuff_hdlc(s, t, len); | |
2127 break; | |
2128 case AT_MODE_STUFFED: | |
2129 if (s->tx.out_bytes) | |
2130 { | |
2131 /* Make room for new data in existing data buffer. */ | |
2132 s->tx.in_bytes = &(s->tx.data[s->tx.in_bytes]) - &(s->tx.data[s->tx.out_bytes]); | |
2133 memmove(&(s->tx.data[0]), &(s->tx.data[s->tx.out_bytes]), s->tx.in_bytes); | |
2134 s->tx.out_bytes = 0; | |
2135 } | |
2136 dle_unstuff(s, t, len); | |
2137 break; | |
2138 } | |
2139 return len; | |
2140 } | |
2141 /*- End of function --------------------------------------------------------*/ | |
2142 | |
2143 static void set_rx_handler(t31_state_t *s, | |
2144 span_rx_handler_t *rx_handler, | |
2145 span_rx_fillin_handler_t *fillin_handler, | |
2146 void *user_data) | |
2147 { | |
2148 s->audio.modems.rx_handler = rx_handler; | |
2149 s->audio.modems.rx_fillin_handler = fillin_handler; | |
2150 s->audio.modems.rx_user_data = user_data; | |
2151 } | |
2152 /*- End of function --------------------------------------------------------*/ | |
2153 | |
2154 static void set_tx_handler(t31_state_t *s, span_tx_handler_t *handler, void *user_data) | |
2155 { | |
2156 s->audio.modems.tx_handler = handler; | |
2157 s->audio.modems.tx_user_data = user_data; | |
2158 } | |
2159 /*- End of function --------------------------------------------------------*/ | |
2160 | |
2161 static void set_next_tx_handler(t31_state_t *s, span_tx_handler_t *handler, void *user_data) | |
2162 { | |
2163 s->audio.modems.next_tx_handler = handler; | |
2164 s->audio.modems.next_tx_user_data = user_data; | |
2165 } | |
2166 /*- End of function --------------------------------------------------------*/ | |
2167 | |
2168 static int silence_rx(void *user_data, const int16_t amp[], int len) | |
2169 { | |
2170 t31_state_t *s; | |
2171 | |
2172 /* Searching for a specified minimum period of silence. */ | |
2173 s = (t31_state_t *) user_data; | |
2174 if (s->silence_awaited && s->audio.silence_heard >= s->silence_awaited) | |
2175 { | |
2176 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK); | |
2177 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
2178 s->audio.silence_heard = 0; | |
2179 s->silence_awaited = 0; | |
2180 } | |
2181 return 0; | |
2182 } | |
2183 /*- End of function --------------------------------------------------------*/ | |
2184 | |
2185 static int cng_rx(void *user_data, const int16_t amp[], int len) | |
2186 { | |
2187 t31_state_t *s; | |
2188 | |
2189 s = (t31_state_t *) user_data; | |
2190 if (s->call_samples > ms_to_samples(s->at_state.p.s_regs[7]*1000)) | |
2191 { | |
2192 /* After calling, S7 has elapsed... no carrier found. */ | |
2193 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_NO_CARRIER); | |
2194 restart_modem(s, FAX_MODEM_SILENCE_TX); | |
2195 at_modem_control(&s->at_state, AT_MODEM_CONTROL_HANGUP, NULL); | |
2196 t31_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND); | |
2197 } | |
2198 else | |
2199 { | |
2200 fsk_rx(&(s->audio.modems.v21_rx), amp, len); | |
2201 } | |
2202 return 0; | |
2203 } | |
2204 /*- End of function --------------------------------------------------------*/ | |
2205 | |
2206 static int v17_v21_rx(void *user_data, const int16_t amp[], int len) | |
2207 { | |
2208 t31_state_t *t; | |
2209 fax_modems_state_t *s; | |
2210 | |
2211 t = (t31_state_t *) user_data; | |
2212 s = &t->audio.modems; | |
2213 v17_rx(&s->v17_rx, amp, len); | |
2214 if (t->at_state.rx_trained) | |
2215 { | |
2216 /* The fast modem has trained, so we no longer need to run the slow | |
2217 one in parallel. */ | |
2218 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)); | |
2219 set_rx_handler(t, (span_rx_handler_t *) &v17_rx, (span_rx_fillin_handler_t *) &v17_rx_fillin, &s->v17_rx); | |
2220 } | |
2221 else | |
2222 { | |
2223 fsk_rx(&s->v21_rx, amp, len); | |
2224 if (t->rx_frame_received) | |
2225 { | |
2226 /* We have received something, and the fast modem has not trained. We must | |
2227 be receiving valid V.21 */ | |
2228 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)); | |
2229 set_rx_handler(t, (span_rx_handler_t *) &fsk_rx, (span_rx_fillin_handler_t *) &fsk_rx_fillin, &s->v21_rx); | |
2230 } | |
2231 } | |
2232 return len; | |
2233 } | |
2234 /*- End of function --------------------------------------------------------*/ | |
2235 | |
2236 static int v17_v21_rx_fillin(void *user_data, int len) | |
2237 { | |
2238 t31_state_t *t; | |
2239 fax_modems_state_t *s; | |
2240 | |
2241 t = (t31_state_t *) user_data; | |
2242 s = &t->audio.modems; | |
2243 v17_rx_fillin(&s->v17_rx, len); | |
2244 fsk_rx_fillin(&s->v21_rx, len); | |
2245 return 0; | |
2246 } | |
2247 /*- End of function --------------------------------------------------------*/ | |
2248 | |
2249 static int v27ter_v21_rx(void *user_data, const int16_t amp[], int len) | |
2250 { | |
2251 t31_state_t *t; | |
2252 fax_modems_state_t *s; | |
2253 | |
2254 t = (t31_state_t *) user_data; | |
2255 s = &t->audio.modems; | |
2256 v27ter_rx(&s->v27ter_rx, amp, len); | |
2257 if (t->at_state.rx_trained) | |
2258 { | |
2259 /* The fast modem has trained, so we no longer need to run the slow | |
2260 one in parallel. */ | |
2261 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)); | |
2262 set_rx_handler(t, (span_rx_handler_t *) &v27ter_rx, (span_rx_fillin_handler_t *) &v27ter_rx_fillin, &s->v27ter_rx); | |
2263 } | |
2264 else | |
2265 { | |
2266 fsk_rx(&s->v21_rx, amp, len); | |
2267 if (t->rx_frame_received) | |
2268 { | |
2269 /* We have received something, and the fast modem has not trained. We must | |
2270 be receiving valid V.21 */ | |
2271 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)); | |
2272 set_rx_handler(t, (span_rx_handler_t *) &fsk_rx, (span_rx_fillin_handler_t *) &fsk_rx_fillin, &s->v21_rx); | |
2273 } | |
2274 } | |
2275 return len; | |
2276 } | |
2277 /*- End of function --------------------------------------------------------*/ | |
2278 | |
2279 static int v27ter_v21_rx_fillin(void *user_data, int len) | |
2280 { | |
2281 t31_state_t *t; | |
2282 fax_modems_state_t *s; | |
2283 | |
2284 t = (t31_state_t *) user_data; | |
2285 s = &t->audio.modems; | |
2286 v27ter_rx_fillin(&s->v27ter_rx, len); | |
2287 fsk_rx_fillin(&s->v21_rx, len); | |
2288 return 0; | |
2289 } | |
2290 /*- End of function --------------------------------------------------------*/ | |
2291 | |
2292 static int v29_v21_rx(void *user_data, const int16_t amp[], int len) | |
2293 { | |
2294 t31_state_t *t; | |
2295 fax_modems_state_t *s; | |
2296 | |
2297 t = (t31_state_t *) user_data; | |
2298 s = &t->audio.modems; | |
2299 v29_rx(&s->v29_rx, amp, len); | |
2300 if (t->at_state.rx_trained) | |
2301 { | |
2302 /* The fast modem has trained, so we no longer need to run the slow | |
2303 one in parallel. */ | |
2304 span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.29 (%.2fdBm0)\n", v29_rx_signal_power(&s->v29_rx)); | |
2305 set_rx_handler(t, (span_rx_handler_t *) &v29_rx, (span_rx_fillin_handler_t *) &v29_rx_fillin, &s->v29_rx); | |
2306 } | |
2307 else | |
2308 { | |
2309 fsk_rx(&s->v21_rx, amp, len); | |
2310 if (t->rx_frame_received) | |
2311 { | |
2312 /* We have received something, and the fast modem has not trained. We must | |
2313 be receiving valid V.21 */ | |
2314 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)); | |
2315 set_rx_handler(t, (span_rx_handler_t *) &fsk_rx, (span_rx_fillin_handler_t *) &fsk_rx_fillin, &s->v21_rx); | |
2316 } | |
2317 } | |
2318 return len; | |
2319 } | |
2320 /*- End of function --------------------------------------------------------*/ | |
2321 | |
2322 static int v29_v21_rx_fillin(void *user_data, int len) | |
2323 { | |
2324 t31_state_t *t; | |
2325 fax_modems_state_t *s; | |
2326 | |
2327 t = (t31_state_t *) user_data; | |
2328 s = &t->audio.modems; | |
2329 v29_rx_fillin(&s->v29_rx, len); | |
2330 fsk_rx_fillin(&s->v21_rx, len); | |
2331 return 0; | |
2332 } | |
2333 /*- End of function --------------------------------------------------------*/ | |
2334 | |
2335 SPAN_DECLARE(int) t31_rx(t31_state_t *s, int16_t amp[], int len) | |
2336 { | |
2337 int i; | |
2338 int32_t power; | |
2339 | |
2340 /* Monitor for received silence. Maximum needed detection is AT+FRS=255 (255*10ms). */ | |
2341 /* We could probably only run this loop if (s->modem == FAX_MODEM_SILENCE_RX), however, | |
2342 the spec says "when silence has been present on the line for the amount of | |
2343 time specified". That means some of the silence may have occurred before | |
2344 the AT+FRS=n command. This condition, however, is not likely to ever be the | |
2345 case. (AT+FRS=n will usually be issued before the remote goes silent.) */ | |
2346 for (i = 0; i < len; i++) | |
2347 { | |
2348 /* Clean up any DC influence. */ | |
2349 power = power_meter_update(&(s->audio.rx_power), amp[i] - s->audio.last_sample); | |
2350 s->audio.last_sample = amp[i]; | |
2351 if (power > s->audio.silence_threshold_power) | |
2352 { | |
2353 s->audio.silence_heard = 0; | |
2354 } | |
2355 else | |
2356 { | |
2357 if (s->audio.silence_heard <= ms_to_samples(255*10)) | |
2358 s->audio.silence_heard++; | |
2359 } | |
2360 } | |
2361 | |
2362 /* Time is determined by counting the samples in audio packets coming in. */ | |
2363 s->call_samples += len; | |
2364 | |
2365 /* In HDLC transmit mode, if 5 seconds elapse without data from the DTE | |
2366 we must treat this as an error. We return the result ERROR, and change | |
2367 to command-mode. */ | |
2368 if (s->dte_data_timeout && s->call_samples > s->dte_data_timeout) | |
2369 { | |
2370 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
2371 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_ERROR); | |
2372 restart_modem(s, FAX_MODEM_SILENCE_TX); | |
2373 } | |
2374 | |
2375 if (!s->at_state.transmit || s->modem == FAX_MODEM_CNG_TONE) | |
2376 s->audio.modems.rx_handler(s->audio.modems.rx_user_data, amp, len); | |
2377 return 0; | |
2378 } | |
2379 /*- End of function --------------------------------------------------------*/ | |
2380 | |
2381 SPAN_DECLARE(int) t31_rx_fillin(t31_state_t *s, int len) | |
2382 { | |
2383 /* To mitigate the effect of lost packets on a packet network we should | |
2384 try to sustain the status quo. If there is no receive modem running, keep | |
2385 things that way. If there is a receive modem running, try to sustain its | |
2386 operation, without causing a phase hop, or letting its adaptive functions | |
2387 diverge. */ | |
2388 /* Time is determined by counting the samples in audio packets coming in. */ | |
2389 s->call_samples += len; | |
2390 | |
2391 /* In HDLC transmit mode, if 5 seconds elapse without data from the DTE | |
2392 we must treat this as an error. We return the result ERROR, and change | |
2393 to command-mode. */ | |
2394 if (s->dte_data_timeout && s->call_samples > s->dte_data_timeout) | |
2395 { | |
2396 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND); | |
2397 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_ERROR); | |
2398 restart_modem(s, FAX_MODEM_SILENCE_TX); | |
2399 } | |
2400 /* Call the fillin function of the current modem (if there is one). */ | |
2401 switch (s->modem) | |
2402 { | |
2403 case FAX_MODEM_V21_RX: | |
2404 len = fsk_rx_fillin(&s->audio.modems.v21_rx, len); | |
2405 break; | |
2406 case FAX_MODEM_V27TER_RX: | |
2407 /* TODO: what about FSK in the early stages */ | |
2408 len = v27ter_rx_fillin(&s->audio.modems.v27ter_rx, len); | |
2409 break; | |
2410 case FAX_MODEM_V29_RX: | |
2411 /* TODO: what about FSK in the early stages */ | |
2412 len = v29_rx_fillin(&s->audio.modems.v29_rx, len); | |
2413 break; | |
2414 case FAX_MODEM_V17_RX: | |
2415 /* TODO: what about FSK in the early stages */ | |
2416 len = v17_rx_fillin(&s->audio.modems.v17_rx, len); | |
2417 break; | |
2418 } | |
2419 return 0; | |
2420 } | |
2421 /*- End of function --------------------------------------------------------*/ | |
2422 | |
2423 static int set_next_tx_type(t31_state_t *s) | |
2424 { | |
2425 if (s->audio.next_tx_handler) | |
2426 { | |
2427 set_tx_handler(s, s->audio.next_tx_handler, s->audio.next_tx_user_data); | |
2428 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
2429 return 0; | |
2430 } | |
2431 /* There is nothing else to change to, so use zero length silence */ | |
2432 silence_gen_alter(&(s->audio.modems.silence_gen), 0); | |
2433 set_tx_handler(s, (span_tx_handler_t *) &silence_gen, &s->audio.modems.silence_gen); | |
2434 set_next_tx_handler(s, (span_tx_handler_t *) NULL, NULL); | |
2435 return -1; | |
2436 } | |
2437 /*- End of function --------------------------------------------------------*/ | |
2438 | |
2439 SPAN_DECLARE(int) t31_tx(t31_state_t *s, int16_t amp[], int max_len) | |
2440 { | |
2441 int len; | |
2442 | |
2443 len = 0; | |
2444 if (s->at_state.transmit) | |
2445 { | |
2446 if ((len = s->audio.modems.tx_handler(s->audio.modems.tx_user_data, amp, max_len)) < max_len) | |
2447 { | |
2448 /* Allow for one change of tx handler within a block */ | |
2449 set_next_tx_type(s); | |
2450 if ((len += s->audio.modems.tx_handler(s->audio.modems.tx_user_data, amp + len, max_len - len)) < max_len) | |
2451 front_end_status(s, T30_FRONT_END_SEND_STEP_COMPLETE); | |
2452 } | |
2453 } | |
2454 if (s->audio.modems.transmit_on_idle) | |
2455 { | |
2456 /* Pad to the requested length with silence */ | |
2457 memset(amp + len, 0, (max_len - len)*sizeof(int16_t)); | |
2458 len = max_len; | |
2459 } | |
2460 return len; | |
2461 } | |
2462 /*- End of function --------------------------------------------------------*/ | |
2463 | |
2464 SPAN_DECLARE(void) t31_set_transmit_on_idle(t31_state_t *s, int transmit_on_idle) | |
2465 { | |
2466 s->audio.modems.transmit_on_idle = transmit_on_idle; | |
2467 } | |
2468 /*- End of function --------------------------------------------------------*/ | |
2469 | |
2470 SPAN_DECLARE(void) t31_set_tep_mode(t31_state_t *s, int use_tep) | |
2471 { | |
2472 s->audio.modems.use_tep = use_tep; | |
2473 } | |
2474 /*- End of function --------------------------------------------------------*/ | |
2475 | |
2476 SPAN_DECLARE(void) t31_set_t38_config(t31_state_t *s, int without_pacing) | |
2477 { | |
2478 if (without_pacing) | |
2479 { | |
2480 /* Continuous streaming mode, as used for TPKT over TCP transport */ | |
2481 /* Inhibit indicator packets */ | |
2482 t38_set_redundancy_control(&s->t38_fe.t38, T38_PACKET_CATEGORY_INDICATOR, 0); | |
2483 t38_set_redundancy_control(&s->t38_fe.t38, T38_PACKET_CATEGORY_CONTROL_DATA, 1); | |
2484 t38_set_redundancy_control(&s->t38_fe.t38, T38_PACKET_CATEGORY_CONTROL_DATA_END, 1); | |
2485 t38_set_redundancy_control(&s->t38_fe.t38, T38_PACKET_CATEGORY_IMAGE_DATA, 1); | |
2486 t38_set_redundancy_control(&s->t38_fe.t38, T38_PACKET_CATEGORY_IMAGE_DATA_END, 1); | |
2487 s->t38_fe.ms_per_tx_chunk = 0; | |
2488 } | |
2489 else | |
2490 { | |
2491 /* Paced streaming mode, as used for UDP transports */ | |
2492 t38_set_redundancy_control(&s->t38_fe.t38, T38_PACKET_CATEGORY_INDICATOR, INDICATOR_TX_COUNT); | |
2493 t38_set_redundancy_control(&s->t38_fe.t38, T38_PACKET_CATEGORY_CONTROL_DATA, DATA_TX_COUNT); | |
2494 t38_set_redundancy_control(&s->t38_fe.t38, T38_PACKET_CATEGORY_CONTROL_DATA_END, DATA_END_TX_COUNT); | |
2495 t38_set_redundancy_control(&s->t38_fe.t38, T38_PACKET_CATEGORY_IMAGE_DATA, DATA_TX_COUNT); | |
2496 t38_set_redundancy_control(&s->t38_fe.t38, T38_PACKET_CATEGORY_IMAGE_DATA_END, DATA_END_TX_COUNT); | |
2497 s->t38_fe.ms_per_tx_chunk = MS_PER_TX_CHUNK; | |
2498 } | |
2499 set_octets_per_data_packet(s, 300); | |
2500 } | |
2501 /*- End of function --------------------------------------------------------*/ | |
2502 | |
2503 SPAN_DECLARE(void) t31_set_mode(t31_state_t *s, int t38_mode) | |
2504 { | |
2505 s->t38_mode = t38_mode; | |
2506 span_log(&s->logging, SPAN_LOG_FLOW, "Mode set to %d\n", s->t38_mode); | |
2507 } | |
2508 /*- End of function --------------------------------------------------------*/ | |
2509 | |
2510 SPAN_DECLARE(logging_state_t *) t31_get_logging_state(t31_state_t *s) | |
2511 { | |
2512 return &s->logging; | |
2513 } | |
2514 /*- End of function --------------------------------------------------------*/ | |
2515 | |
2516 SPAN_DECLARE(t38_core_state_t *) t31_get_t38_core_state(t31_state_t *s) | |
2517 { | |
2518 return &s->t38_fe.t38; | |
2519 } | |
2520 /*- End of function --------------------------------------------------------*/ | |
2521 | |
2522 static int t31_t38_fe_init(t31_state_t *t, | |
2523 t38_tx_packet_handler_t *tx_packet_handler, | |
2524 void *tx_packet_user_data) | |
2525 { | |
2526 t31_t38_front_end_state_t *s; | |
2527 | |
2528 s = &t->t38_fe; | |
2529 | |
2530 t38_core_init(&s->t38, | |
2531 process_rx_indicator, | |
2532 process_rx_data, | |
2533 process_rx_missing, | |
2534 (void *) t, | |
2535 tx_packet_handler, | |
2536 tx_packet_user_data); | |
2537 s->t38.fastest_image_data_rate = 14400; | |
2538 | |
2539 s->timed_step = T38_TIMED_STEP_NONE; | |
2540 //s->iaf = T30_IAF_MODE_T37 | T30_IAF_MODE_T38; | |
2541 s->iaf = T30_IAF_MODE_T38; | |
2542 | |
2543 s->current_tx_data_type = T38_DATA_NONE; | |
2544 s->next_tx_samples = 0; | |
2545 s->chunking_modes = T38_CHUNKING_ALLOW_TEP_TIME; | |
2546 | |
2547 t->hdlc_tx.ptr = 0; | |
2548 | |
2549 hdlc_tx_init(&s->hdlc_tx_term, | |
2550 FALSE, | |
2551 1, | |
2552 FALSE, | |
2553 NULL, | |
2554 NULL); | |
2555 hdlc_rx_init(&s->hdlc_rx_term, | |
2556 FALSE, | |
2557 TRUE, | |
2558 2, | |
2559 NULL, | |
2560 NULL); | |
2561 return 0; | |
2562 } | |
2563 /*- End of function --------------------------------------------------------*/ | |
2564 | |
2565 SPAN_DECLARE(t31_state_t *) t31_init(t31_state_t *s, | |
2566 at_tx_handler_t *at_tx_handler, | |
2567 void *at_tx_user_data, | |
2568 t31_modem_control_handler_t *modem_control_handler, | |
2569 void *modem_control_user_data, | |
2570 t38_tx_packet_handler_t *tx_t38_packet_handler, | |
2571 void *tx_t38_packet_user_data) | |
2572 { | |
2573 int alloced; | |
2574 | |
2575 if (at_tx_handler == NULL || modem_control_handler == NULL) | |
2576 return NULL; | |
2577 | |
2578 alloced = FALSE; | |
2579 if (s == NULL) | |
2580 { | |
2581 if ((s = (t31_state_t *) malloc(sizeof (*s))) == NULL) | |
2582 return NULL; | |
2583 alloced = TRUE; | |
2584 } | |
2585 memset(s, 0, sizeof(*s)); | |
2586 span_log_init(&s->logging, SPAN_LOG_NONE, NULL); | |
2587 span_log_set_protocol(&s->logging, "T.31"); | |
2588 | |
2589 s->modem_control_handler = modem_control_handler; | |
2590 s->modem_control_user_data = modem_control_user_data; | |
2591 fax_modems_init(&s->audio.modems, | |
2592 FALSE, | |
2593 hdlc_accept_frame, | |
2594 hdlc_tx_underflow, | |
2595 non_ecm_put_bit, | |
2596 non_ecm_get_bit, | |
2597 tone_detected, | |
2598 (void *) s); | |
2599 power_meter_init(&(s->audio.rx_power), 4); | |
2600 s->audio.last_sample = 0; | |
2601 s->audio.silence_threshold_power = power_meter_level_dbm0(-36); | |
2602 s->at_state.rx_signal_present = FALSE; | |
2603 s->at_state.rx_trained = FALSE; | |
2604 | |
2605 s->at_state.do_hangup = FALSE; | |
2606 s->at_state.line_ptr = 0; | |
2607 s->audio.silence_heard = 0; | |
2608 s->silence_awaited = 0; | |
2609 s->call_samples = 0; | |
2610 s->modem = FAX_MODEM_NONE; | |
2611 s->at_state.transmit = TRUE; | |
2612 | |
2613 if ((s->rx_queue = queue_init(NULL, 4096, QUEUE_WRITE_ATOMIC | QUEUE_READ_ATOMIC)) == NULL) | |
2614 { | |
2615 if (alloced) | |
2616 free(s); | |
2617 return NULL; | |
2618 } | |
2619 at_init(&s->at_state, at_tx_handler, at_tx_user_data, t31_modem_control_handler, s); | |
2620 at_set_class1_handler(&s->at_state, process_class1_cmd, s); | |
2621 s->at_state.dte_inactivity_timeout = DEFAULT_DTE_TIMEOUT; | |
2622 if (tx_t38_packet_handler) | |
2623 { | |
2624 t31_t38_fe_init(s, | |
2625 tx_t38_packet_handler, | |
2626 tx_t38_packet_user_data); | |
2627 t31_set_t38_config(s, FALSE); | |
2628 } | |
2629 s->t38_mode = FALSE; | |
2630 return s; | |
2631 } | |
2632 /*- End of function --------------------------------------------------------*/ | |
2633 | |
2634 SPAN_DECLARE(int) t31_release(t31_state_t *s) | |
2635 { | |
2636 at_reset_call_info(&s->at_state); | |
2637 return 0; | |
2638 } | |
2639 /*- End of function --------------------------------------------------------*/ | |
2640 | |
2641 SPAN_DECLARE(int) t31_free(t31_state_t *s) | |
2642 { | |
2643 t31_release(s); | |
2644 free(s); | |
2645 return 0; | |
2646 } | |
2647 /*- End of function --------------------------------------------------------*/ | |
2648 /*- End of file ------------------------------------------------------------*/ |