5
|
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 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 General Public License version 2, as
|
|
17 * 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 General Public License for more details.
|
|
23 *
|
|
24 * You should have received a copy of the GNU General Public License
|
|
25 * 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.83 2006/11/30 15:41:47 steveu Exp $
|
|
29 */
|
|
30
|
|
31 /*! \file */
|
|
32
|
|
33 #ifdef HAVE_CONFIG_H
|
|
34 #include <config.h>
|
|
35 #endif
|
|
36
|
|
37 #define _GNU_SOURCE
|
|
38
|
|
39 #include <inttypes.h>
|
|
40 #include <stdlib.h>
|
|
41 #include <stdio.h>
|
|
42 #include <fcntl.h>
|
|
43 #include <time.h>
|
|
44 #include <memory.h>
|
|
45 #include <string.h>
|
|
46 #include <ctype.h>
|
|
47 #if defined(HAVE_TGMATH_H)
|
|
48 #include <tgmath.h>
|
|
49 #endif
|
|
50 #if defined(HAVE_MATH_H)
|
|
51 #include <math.h>
|
|
52 #endif
|
|
53 #include <assert.h>
|
|
54 #include <tiffio.h>
|
|
55
|
|
56 #include "spandsp/telephony.h"
|
|
57 #include "spandsp/logging.h"
|
|
58 #include "spandsp/bit_operations.h"
|
|
59 #include "spandsp/dc_restore.h"
|
|
60 #include "spandsp/queue.h"
|
|
61 #include "spandsp/power_meter.h"
|
|
62 #include "spandsp/complex.h"
|
|
63 #include "spandsp/tone_generate.h"
|
|
64 #include "spandsp/async.h"
|
|
65 #include "spandsp/hdlc.h"
|
|
66 #include "spandsp/silence_gen.h"
|
|
67 #include "spandsp/fsk.h"
|
|
68 #include "spandsp/v29rx.h"
|
|
69 #include "spandsp/v29tx.h"
|
|
70 #include "spandsp/v27ter_rx.h"
|
|
71 #include "spandsp/v27ter_tx.h"
|
|
72 #if defined(ENABLE_V17)
|
|
73 #include "spandsp/v17rx.h"
|
|
74 #include "spandsp/v17tx.h"
|
|
75 #endif
|
|
76 #include "spandsp/t4.h"
|
|
77 #include "spandsp/t30.h"
|
|
78 #include "spandsp/t38_core.h"
|
|
79
|
|
80 #include "spandsp/at_interpreter.h"
|
|
81 #include "spandsp/t31.h"
|
|
82
|
|
83 #define MS_PER_TX_CHUNK 30
|
|
84 #define INDICATOR_TX_COUNT 4
|
|
85 #define DEFAULT_DTE_TIMEOUT 5
|
|
86
|
|
87 typedef const char *(*at_cmd_service_t)(t31_state_t *s, const char *cmd);
|
|
88
|
|
89 #define ETX 0x03
|
|
90 #define DLE 0x10
|
|
91 #define SUB 0x1A
|
|
92
|
|
93 enum
|
|
94 {
|
|
95 T31_FLUSH,
|
|
96 T31_SILENCE_TX,
|
|
97 T31_SILENCE_RX,
|
|
98 T31_CED_TONE,
|
|
99 T31_CNG_TONE,
|
|
100 T31_NOCNG_TONE,
|
|
101 T31_V21_TX,
|
|
102 T31_V17_TX,
|
|
103 T31_V27TER_TX,
|
|
104 T31_V29_TX,
|
|
105 T31_V21_RX,
|
|
106 T31_V17_RX,
|
|
107 T31_V27TER_RX,
|
|
108 T31_V29_RX
|
|
109 };
|
|
110
|
|
111 enum
|
|
112 {
|
|
113 T38_TIMED_STEP_NONE = 0,
|
|
114 T38_TIMED_STEP_NON_ECM_MODEM,
|
|
115 T38_TIMED_STEP_NON_ECM_MODEM_2,
|
|
116 T38_TIMED_STEP_NON_ECM_MODEM_3,
|
|
117 T38_TIMED_STEP_HDLC_MODEM,
|
|
118 T38_TIMED_STEP_HDLC_MODEM_2,
|
|
119 T38_TIMED_STEP_HDLC_MODEM_3,
|
|
120 T38_TIMED_STEP_HDLC_MODEM_4,
|
|
121 T38_TIMED_STEP_PAUSE
|
|
122 };
|
|
123
|
|
124 static int restart_modem(t31_state_t *s, int new_modem);
|
|
125 static void hdlc_accept(void *user_data, int ok, const uint8_t *msg, int len);
|
|
126 #if defined(ENABLE_V17)
|
|
127 static int early_v17_rx(void *user_data, const int16_t amp[], int len);
|
|
128 #endif
|
|
129 static int early_v27ter_rx(void *user_data, const int16_t amp[], int len);
|
|
130 static int early_v29_rx(void *user_data, const int16_t amp[], int len);
|
|
131 static int dummy_rx(void *s, const int16_t amp[], int len);
|
|
132 static int silence_rx(void *user_data, const int16_t amp[], int len);
|
|
133 static int cng_rx(void *user_data, const int16_t amp[], int len);
|
|
134
|
|
135 static __inline__ void t31_set_at_rx_mode(t31_state_t *s, int new_mode)
|
|
136 {
|
|
137 s->at_state.at_rx_mode = new_mode;
|
|
138 }
|
|
139 /*- End of function --------------------------------------------------------*/
|
|
140
|
|
141 static int process_rx_indicator(t38_core_state_t *s, void *user_data, int indicator)
|
|
142 {
|
|
143 t31_state_t *t;
|
|
144
|
|
145 t = (t31_state_t *) user_data;
|
|
146 switch (indicator)
|
|
147 {
|
|
148 case T38_IND_NO_SIGNAL:
|
|
149 break;
|
|
150 case T38_IND_CNG:
|
|
151 break;
|
|
152 case T38_IND_CED:
|
|
153 break;
|
|
154 case T38_IND_V21_PREAMBLE:
|
|
155 t->hdlc_rx_len = 0;
|
|
156 break;
|
|
157 case T38_IND_V27TER_2400_TRAINING:
|
|
158 break;
|
|
159 case T38_IND_V27TER_4800_TRAINING:
|
|
160 break;
|
|
161 case T38_IND_V29_7200_TRAINING:
|
|
162 break;
|
|
163 case T38_IND_V29_9600_TRAINING:
|
|
164 break;
|
|
165 case T38_IND_V17_7200_SHORT_TRAINING:
|
|
166 break;
|
|
167 case T38_IND_V17_7200_LONG_TRAINING:
|
|
168 break;
|
|
169 case T38_IND_V17_9600_SHORT_TRAINING:
|
|
170 break;
|
|
171 case T38_IND_V17_9600_LONG_TRAINING:
|
|
172 break;
|
|
173 case T38_IND_V17_12000_SHORT_TRAINING:
|
|
174 break;
|
|
175 case T38_IND_V17_12000_LONG_TRAINING:
|
|
176 break;
|
|
177 case T38_IND_V17_14400_SHORT_TRAINING:
|
|
178 break;
|
|
179 case T38_IND_V17_14400_LONG_TRAINING:
|
|
180 break;
|
|
181 case T38_IND_V8_ANSAM:
|
|
182 break;
|
|
183 case T38_IND_V8_SIGNAL:
|
|
184 break;
|
|
185 case T38_IND_V34_CNTL_CHANNEL_1200:
|
|
186 break;
|
|
187 case T38_IND_V34_PRI_CHANNEL:
|
|
188 break;
|
|
189 case T38_IND_V34_CC_RETRAIN:
|
|
190 break;
|
|
191 case T38_IND_V33_12000_TRAINING:
|
|
192 break;
|
|
193 case T38_IND_V33_14400_TRAINING:
|
|
194 break;
|
|
195 default:
|
|
196 break;
|
|
197 }
|
|
198 t->missing_data = FALSE;
|
|
199 return 0;
|
|
200 }
|
|
201 /*- End of function --------------------------------------------------------*/
|
|
202
|
|
203 static int process_rx_data(t38_core_state_t *s, void *user_data, int data_type, int field_type, const uint8_t *buf, int len)
|
|
204 {
|
|
205 t31_state_t *t;
|
|
206 int i;
|
|
207
|
|
208 t = (t31_state_t *) user_data;
|
|
209 switch (data_type)
|
|
210 {
|
|
211 case T38_DATA_V21:
|
|
212 case T38_DATA_V27TER_2400:
|
|
213 case T38_DATA_V27TER_4800:
|
|
214 case T38_DATA_V29_7200:
|
|
215 case T38_DATA_V29_9600:
|
|
216 case T38_DATA_V17_7200:
|
|
217 case T38_DATA_V17_9600:
|
|
218 case T38_DATA_V17_12000:
|
|
219 case T38_DATA_V17_14400:
|
|
220 case T38_DATA_V8:
|
|
221 case T38_DATA_V34_PRI_RATE:
|
|
222 case T38_DATA_V34_CC_1200:
|
|
223 case T38_DATA_V34_PRI_CH:
|
|
224 case T38_DATA_V33_12000:
|
|
225 case T38_DATA_V33_14400:
|
|
226 default:
|
|
227 break;
|
|
228 }
|
|
229 switch (field_type)
|
|
230 {
|
|
231 case T38_FIELD_HDLC_DATA:
|
|
232 if (t->hdlc_rx_len + len <= 256 - 2)
|
|
233 {
|
|
234 for (i = 0; i < len; i++)
|
|
235 t->hdlc_rx_buf[t->hdlc_rx_len++] = bit_reverse8(buf[i]);
|
|
236 }
|
|
237 break;
|
|
238 case T38_FIELD_HDLC_FCS_OK:
|
|
239 if (len > 0)
|
|
240 span_log(&t->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_OK!\n");
|
|
241 span_log(&t->logging, SPAN_LOG_FLOW, "Type %s - CRC OK (%s)\n", t30_frametype(t->tx_data[2]), (t->missing_data) ? "missing octets" : "clean");
|
|
242 /* Don't deal with zero length frames. Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_OK
|
|
243 packets, when they have sent no data for the body of the frame. */
|
|
244 if (t->current_rx_type == T31_V21_RX && t->tx_out_bytes > 0 && !t->missing_data)
|
|
245 hdlc_accept((void *) t, TRUE, t->hdlc_rx_buf, t->hdlc_rx_len);
|
|
246 t->hdlc_rx_len = 0;
|
|
247 t->missing_data = FALSE;
|
|
248 break;
|
|
249 case T38_FIELD_HDLC_FCS_BAD:
|
|
250 if (len > 0)
|
|
251 span_log(&t->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_BAD!\n");
|
|
252 span_log(&t->logging, SPAN_LOG_FLOW, "Type %s - CRC bad (%s)\n", t30_frametype(t->tx_data[2]), (t->missing_data) ? "missing octets" : "clean");
|
|
253 t->hdlc_rx_len = 0;
|
|
254 t->missing_data = FALSE;
|
|
255 break;
|
|
256 case T38_FIELD_HDLC_FCS_OK_SIG_END:
|
|
257 if (len > 0)
|
|
258 span_log(&t->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_OK_SIG_END!\n");
|
|
259 span_log(&t->logging, SPAN_LOG_FLOW, "Type %s - CRC OK, sig end (%s)\n", t30_frametype(t->tx_data[2]), (t->missing_data) ? "missing octets" : "clean");
|
|
260 if (t->current_rx_type == T31_V21_RX)
|
|
261 {
|
|
262 /* Don't deal with zero length frames. Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_OK
|
|
263 packets, when they have sent no data for the body of the frame. */
|
|
264 if (t->tx_out_bytes > 0)
|
|
265 hdlc_accept((void *) t, TRUE, t->hdlc_rx_buf, t->hdlc_rx_len);
|
|
266 hdlc_accept((void *) t, TRUE, NULL, PUTBIT_CARRIER_DOWN);
|
|
267 }
|
|
268 t->tx_out_bytes = 0;
|
|
269 t->missing_data = FALSE;
|
|
270 t->hdlc_rx_len = 0;
|
|
271 t->missing_data = FALSE;
|
|
272 break;
|
|
273 case T38_FIELD_HDLC_FCS_BAD_SIG_END:
|
|
274 if (len > 0)
|
|
275 span_log(&t->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_BAD_SIG_END!\n");
|
|
276 span_log(&t->logging, SPAN_LOG_FLOW, "Type %s - CRC bad, sig end (%s)\n", t30_frametype(t->tx_data[2]), (t->missing_data) ? "missing octets" : "clean");
|
|
277 if (t->current_rx_type == T31_V21_RX)
|
|
278 hdlc_accept((void *) t, TRUE, NULL, PUTBIT_CARRIER_DOWN);
|
|
279 t->hdlc_rx_len = 0;
|
|
280 t->missing_data = FALSE;
|
|
281 break;
|
|
282 case T38_FIELD_HDLC_SIG_END:
|
|
283 if (len > 0)
|
|
284 span_log(&t->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_SIG_END!\n");
|
|
285 /* This message is expected under 2 circumstances. One is as an alternative to T38_FIELD_HDLC_FCS_OK_SIG_END -
|
|
286 i.e. they send T38_FIELD_HDLC_FCS_OK, and then T38_FIELD_HDLC_SIG_END when the carrier actually drops.
|
|
287 The other is because the HDLC signal drops unexpectedly - i.e. not just after a final frame. */
|
|
288 if (t->current_rx_type == T31_V21_RX)
|
|
289 hdlc_accept((void *) t, TRUE, NULL, PUTBIT_CARRIER_DOWN);
|
|
290 t->hdlc_rx_len = 0;
|
|
291 t->missing_data = FALSE;
|
|
292 break;
|
|
293 case T38_FIELD_T4_NON_ECM_DATA:
|
|
294 break;
|
|
295 case T38_FIELD_T4_NON_ECM_SIG_END:
|
|
296 break;
|
|
297 case T38_FIELD_CM_MESSAGE:
|
|
298 case T38_FIELD_JM_MESSAGE:
|
|
299 case T38_FIELD_CI_MESSAGE:
|
|
300 case T38_FIELD_V34RATE:
|
|
301 default:
|
|
302 break;
|
|
303 }
|
|
304 return 0;
|
|
305 }
|
|
306 /*- End of function --------------------------------------------------------*/
|
|
307
|
|
308 static int process_rx_missing(t38_core_state_t *s, void *user_data, int rx_seq_no, int expected_seq_no)
|
|
309 {
|
|
310 t31_state_t *t;
|
|
311
|
|
312 t = (t31_state_t *) user_data;
|
|
313 t->missing_data = TRUE;
|
|
314 return 0;
|
|
315 }
|
|
316 /*- End of function --------------------------------------------------------*/
|
|
317
|
|
318 int t31_t38_send_timeout(t31_state_t *s, int samples)
|
|
319 {
|
|
320 int len;
|
|
321 uint8_t buf[100];
|
|
322 /* Training times for all the modem options, with and without TEP */
|
|
323 static const int training_time[] =
|
|
324 {
|
|
325 0, 0, /* T38_IND_NO_SIGNAL */
|
|
326 0, 0, /* T38_IND_CNG */
|
|
327 0, 0, /* T38_IND_CED */
|
|
328 1000, 1000, /* T38_IND_V21_PREAMBLE */
|
|
329 943, 1158, /* T38_IND_V27TER_2400_TRAINING */
|
|
330 708, 923, /* T38_IND_V27TER_4800_TRAINING */
|
|
331 234, 454, /* T38_IND_V29_7200_TRAINING */
|
|
332 234, 454, /* T38_IND_V29_9600_TRAINING */
|
|
333 142, 367, /* T38_IND_V17_7200_SHORT_TRAINING */
|
|
334 1393, 1618, /* T38_IND_V17_7200_LONG_TRAINING */
|
|
335 142, 367, /* T38_IND_V17_9600_SHORT_TRAINING */
|
|
336 1393, 1618, /* T38_IND_V17_9600_LONG_TRAINING */
|
|
337 142, 367, /* T38_IND_V17_12000_SHORT_TRAINING */
|
|
338 1393, 1618, /* T38_IND_V17_12000_LONG_TRAINING */
|
|
339 142, 367, /* T38_IND_V17_14400_SHORT_TRAINING */
|
|
340 1393, 1618, /* T38_IND_V17_14400_LONG_TRAINING */
|
|
341 0, 0, /* T38_IND_V8_ANSAM */
|
|
342 0, 0, /* T38_IND_V8_SIGNAL */
|
|
343 0, 0, /* T38_IND_V34_CNTL_CHANNEL_1200 */
|
|
344 0, 0, /* T38_IND_V34_PRI_CHANNEL */
|
|
345 0, 0, /* T38_IND_V34_CC_RETRAIN */
|
|
346 0, 0, /* T38_IND_V33_12000_TRAINING */
|
|
347 0, 0, /* T38_IND_V33_14400_TRAINING */
|
|
348 };
|
|
349
|
|
350 s->call_samples += samples;
|
|
351
|
|
352 if (s->timed_step == T38_TIMED_STEP_NONE)
|
|
353 return 0;
|
|
354 if (s->call_samples < s->next_send_samples)
|
|
355 return 0;
|
|
356 len = 0;
|
|
357 switch (s->timed_step)
|
|
358 {
|
|
359 case T38_TIMED_STEP_NON_ECM_MODEM:
|
|
360 /* Create a 75ms silence */
|
|
361 t38_core_send_indicator(&s->t38, T38_IND_NO_SIGNAL, INDICATOR_TX_COUNT);
|
|
362 s->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_2;
|
|
363 s->next_send_samples += ms_to_samples(75);
|
|
364 break;
|
|
365 case T38_TIMED_STEP_NON_ECM_MODEM_2:
|
|
366 /* Switch on a fast modem, and give the training time to complete */
|
|
367 t38_core_send_indicator(&s->t38, s->next_tx_indicator, INDICATOR_TX_COUNT);
|
|
368 s->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_3;
|
|
369 s->next_send_samples += ms_to_samples(training_time[s->next_tx_indicator << 1]);
|
|
370 break;
|
|
371 case T38_TIMED_STEP_NON_ECM_MODEM_3:
|
|
372 /* Send a chunk of non-ECM image data */
|
|
373 //if ((len = get_non_ecm_image_chunk(s, buf, s->octets_per_non_ecm_packet)))
|
|
374 // t38_core_send_data(&s->t38, s->current_tx_data, T38_FIELD_T4_NON_ECM_DATA, buf, abs(len));
|
|
375 if (len > 0)
|
|
376 {
|
|
377 s->next_send_samples += ms_to_samples(MS_PER_TX_CHUNK);
|
|
378 }
|
|
379 else
|
|
380 {
|
|
381 t38_core_send_data(&s->t38, s->current_tx_data, T38_FIELD_T4_NON_ECM_SIG_END, NULL, 0);
|
|
382 t38_core_send_indicator(&s->t38, T38_IND_NO_SIGNAL, INDICATOR_TX_COUNT);
|
|
383 s->timed_step = T38_TIMED_STEP_NONE;
|
|
384 }
|
|
385 break;
|
|
386 case T38_TIMED_STEP_HDLC_MODEM:
|
|
387 /* Send HDLC preambling */
|
|
388 t38_core_send_indicator(&s->t38, s->next_tx_indicator, INDICATOR_TX_COUNT);
|
|
389 s->next_send_samples += ms_to_samples(training_time[s->next_tx_indicator << 1]);
|
|
390 s->timed_step = T38_TIMED_STEP_HDLC_MODEM_2;
|
|
391 break;
|
|
392 case T38_TIMED_STEP_HDLC_MODEM_2:
|
|
393 /* Send HDLC octet */
|
|
394 buf[0] = bit_reverse8(s->hdlc_tx_buf[s->hdlc_tx_ptr++]);
|
|
395 t38_core_send_data(&s->t38, s->current_tx_data, T38_FIELD_HDLC_DATA, buf, 1);
|
|
396 if (s->hdlc_tx_ptr >= s->hdlc_tx_len)
|
|
397 s->timed_step = T38_TIMED_STEP_HDLC_MODEM_3;
|
|
398 s->next_send_samples += ms_to_samples(MS_PER_TX_CHUNK);
|
|
399 break;
|
|
400 case T38_TIMED_STEP_HDLC_MODEM_3:
|
|
401 /* End of HDLC frame */
|
|
402 s->hdlc_tx_ptr = 0;
|
|
403 if (s->hdlc_final)
|
|
404 {
|
|
405 s->hdlc_tx_len = 0;
|
|
406 t38_core_send_data(&s->t38, s->current_tx_data, T38_FIELD_HDLC_FCS_OK_SIG_END, NULL, 0);
|
|
407 s->timed_step = T38_TIMED_STEP_HDLC_MODEM_4;
|
|
408 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK);
|
|
409 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
|
|
410 s->hdlc_final = FALSE;
|
|
411 restart_modem(s, T31_SILENCE_TX);
|
|
412 }
|
|
413 else
|
|
414 {
|
|
415 t38_core_send_data(&s->t38, s->current_tx_data, T38_FIELD_HDLC_FCS_OK, NULL, 0);
|
|
416 s->timed_step = T38_TIMED_STEP_HDLC_MODEM_2;
|
|
417 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
|
|
418 }
|
|
419 s->next_send_samples += ms_to_samples(MS_PER_TX_CHUNK);
|
|
420 break;
|
|
421 case T38_TIMED_STEP_HDLC_MODEM_4:
|
|
422 /* End of HDLC transmission */
|
|
423 /* We have already sent T38_FIELD_HDLC_FCS_OK_SIG_END. It seems some boxes may not like
|
|
424 us sending a T38_FIELD_HDLC_SIG_END at this point. Just say there is no signal. */
|
|
425 //t38_core_send_data(&s->t38, s->current_tx_data, T38_FIELD_HDLC_SIG_END, NULL, 0);
|
|
426 t38_core_send_indicator(&s->t38, T38_IND_NO_SIGNAL, INDICATOR_TX_COUNT);
|
|
427 s->timed_step = T38_TIMED_STEP_NONE;
|
|
428 break;
|
|
429 case T38_TIMED_STEP_PAUSE:
|
|
430 /* End of timed pause */
|
|
431 s->timed_step = T38_TIMED_STEP_NONE;
|
|
432 break;
|
|
433 }
|
|
434 return 0;
|
|
435 }
|
|
436 /*- End of function --------------------------------------------------------*/
|
|
437
|
|
438 static int t31_modem_control_handler(at_state_t *s, void *user_data, int op, const char *num)
|
|
439 {
|
|
440 t31_state_t *t;
|
|
441
|
|
442 t = (t31_state_t *) user_data;
|
|
443 switch (op)
|
|
444 {
|
|
445 case AT_MODEM_CONTROL_ANSWER:
|
|
446 t->call_samples = 0;
|
|
447 break;
|
|
448 case AT_MODEM_CONTROL_CALL:
|
|
449 t->call_samples = 0;
|
|
450 break;
|
|
451 case AT_MODEM_CONTROL_ONHOOK:
|
|
452 if (t->tx_holding)
|
|
453 {
|
|
454 t->tx_holding = FALSE;
|
|
455 /* Tell the application to release further data */
|
|
456 at_modem_control(&t->at_state, AT_MODEM_CONTROL_CTS, (void *) 1);
|
|
457 }
|
|
458 if (t->at_state.rx_signal_present)
|
|
459 {
|
|
460 t->at_state.rx_data[t->at_state.rx_data_bytes++] = DLE;
|
|
461 t->at_state.rx_data[t->at_state.rx_data_bytes++] = ETX;
|
|
462 t->at_state.at_tx_handler(&t->at_state, t->at_state.at_tx_user_data, t->at_state.rx_data, t->at_state.rx_data_bytes);
|
|
463 t->at_state.rx_data_bytes = 0;
|
|
464 }
|
|
465 restart_modem(t, T31_SILENCE_TX);
|
|
466 break;
|
|
467 case AT_MODEM_CONTROL_RESTART:
|
|
468 restart_modem(t, (int) (intptr_t) num);
|
|
469 return 0;
|
|
470 case AT_MODEM_CONTROL_DTE_TIMEOUT:
|
|
471 if (num)
|
|
472 t->dte_data_timeout = t->call_samples + ms_to_samples((intptr_t) num);
|
|
473 else
|
|
474 t->dte_data_timeout = 0;
|
|
475 return 0;
|
|
476 }
|
|
477 return t->modem_control_handler(t, t->modem_control_user_data, op, num);
|
|
478 }
|
|
479 /*- End of function --------------------------------------------------------*/
|
|
480
|
|
481 static void non_ecm_put_bit(void *user_data, int bit)
|
|
482 {
|
|
483 t31_state_t *s;
|
|
484
|
|
485 s = (t31_state_t *) user_data;
|
|
486 if (bit < 0)
|
|
487 {
|
|
488 /* Special conditions */
|
|
489 switch (bit)
|
|
490 {
|
|
491 case PUTBIT_TRAINING_FAILED:
|
|
492 s->at_state.rx_trained = FALSE;
|
|
493 break;
|
|
494 case PUTBIT_TRAINING_SUCCEEDED:
|
|
495 /* The modem is now trained */
|
|
496 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
|
|
497 s->at_state.rx_signal_present = TRUE;
|
|
498 s->at_state.rx_trained = TRUE;
|
|
499 break;
|
|
500 case PUTBIT_CARRIER_UP:
|
|
501 break;
|
|
502 case PUTBIT_CARRIER_DOWN:
|
|
503 if (s->at_state.rx_signal_present)
|
|
504 {
|
|
505 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE;
|
|
506 s->at_state.rx_data[s->at_state.rx_data_bytes++] = ETX;
|
|
507 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);
|
|
508 s->at_state.rx_data_bytes = 0;
|
|
509 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_NO_CARRIER);
|
|
510 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
|
|
511 }
|
|
512 s->at_state.rx_signal_present = FALSE;
|
|
513 s->at_state.rx_trained = FALSE;
|
|
514 break;
|
|
515 default:
|
|
516 if (s->at_state.p.result_code_format)
|
|
517 span_log(&s->logging, SPAN_LOG_FLOW, "Eh!\n");
|
|
518 break;
|
|
519 }
|
|
520 return;
|
|
521 }
|
|
522 s->current_byte = (s->current_byte >> 1) | (bit << 7);
|
|
523 if (++s->bit_no >= 8)
|
|
524 {
|
|
525 if (s->current_byte == DLE)
|
|
526 s->at_state.rx_data[s->at_state.rx_data_bytes++] = (uint8_t) s->current_byte;
|
|
527 s->at_state.rx_data[s->at_state.rx_data_bytes++] = (uint8_t) s->current_byte;
|
|
528 if (s->at_state.rx_data_bytes >= 250)
|
|
529 {
|
|
530 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);
|
|
531 s->at_state.rx_data_bytes = 0;
|
|
532 }
|
|
533 s->bit_no = 0;
|
|
534 s->current_byte = 0;
|
|
535 }
|
|
536 }
|
|
537 /*- End of function --------------------------------------------------------*/
|
|
538
|
|
539 static int non_ecm_get_bit(void *user_data)
|
|
540 {
|
|
541 t31_state_t *s;
|
|
542 int bit;
|
|
543 int fill;
|
|
544
|
|
545 s = (t31_state_t *) user_data;
|
|
546 if (s->bit_no <= 0)
|
|
547 {
|
|
548 if (s->tx_out_bytes != s->tx_in_bytes)
|
|
549 {
|
|
550 /* There is real data available to send */
|
|
551 s->current_byte = s->tx_data[s->tx_out_bytes];
|
|
552 s->tx_out_bytes = (s->tx_out_bytes + 1) & (T31_TX_BUF_LEN - 1);
|
|
553 if (s->tx_holding)
|
|
554 {
|
|
555 /* See if the buffer is approaching empty. It might be time to release flow control. */
|
|
556 fill = (s->tx_in_bytes - s->tx_out_bytes);
|
|
557 if (s->tx_in_bytes < s->tx_out_bytes)
|
|
558 fill += (T31_TX_BUF_LEN + 1);
|
|
559 if (fill < T31_TX_BUF_LOW_TIDE)
|
|
560 {
|
|
561 s->tx_holding = FALSE;
|
|
562 /* Tell the application to release further data */
|
|
563 at_modem_control(&s->at_state, AT_MODEM_CONTROL_CTS, (void *) 1);
|
|
564 }
|
|
565 }
|
|
566 s->tx_data_started = TRUE;
|
|
567 }
|
|
568 else
|
|
569 {
|
|
570 if (s->data_final)
|
|
571 {
|
|
572 s->data_final = FALSE;
|
|
573 /* This will put the modem into its shutdown sequence. When
|
|
574 it has finally shut down, an OK response will be sent. */
|
|
575 return PUTBIT_END_OF_DATA;
|
|
576 }
|
|
577 /* Fill with 0xFF bytes at the start of transmission, or 0x00 if we are in
|
|
578 the middle of transmission. This follows T.31 and T.30 practice. */
|
|
579 s->current_byte = (s->tx_data_started) ? 0x00 : 0xFF;
|
|
580 }
|
|
581 s->bit_no = 8;
|
|
582 }
|
|
583 s->bit_no--;
|
|
584 bit = s->current_byte & 1;
|
|
585 s->current_byte >>= 1;
|
|
586 return bit;
|
|
587 }
|
|
588 /*- End of function --------------------------------------------------------*/
|
|
589
|
|
590 static void hdlc_tx_underflow(void *user_data)
|
|
591 {
|
|
592 t31_state_t *s;
|
|
593
|
|
594 s = (t31_state_t *) user_data;
|
|
595 if (s->hdlc_final)
|
|
596 {
|
|
597 s->hdlc_final = FALSE;
|
|
598 /* Schedule an orderly shutdown of the modem */
|
|
599 hdlc_tx_frame(&(s->hdlctx), NULL, 0);
|
|
600 }
|
|
601 else
|
|
602 {
|
|
603 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
|
|
604 }
|
|
605 }
|
|
606 /*- End of function --------------------------------------------------------*/
|
|
607
|
|
608 static void hdlc_accept(void *user_data, int ok, const uint8_t *msg, int len)
|
|
609 {
|
|
610 uint8_t buf[256];
|
|
611 t31_state_t *s;
|
|
612 int i;
|
|
613
|
|
614 s = (t31_state_t *) user_data;
|
|
615 if (len < 0)
|
|
616 {
|
|
617 /* Special conditions */
|
|
618 switch (len)
|
|
619 {
|
|
620 case PUTBIT_TRAINING_FAILED:
|
|
621 s->at_state.rx_trained = FALSE;
|
|
622 break;
|
|
623 case PUTBIT_TRAINING_SUCCEEDED:
|
|
624 /* The modem is now trained */
|
|
625 s->at_state.rx_signal_present = TRUE;
|
|
626 s->at_state.rx_trained = TRUE;
|
|
627 break;
|
|
628 case PUTBIT_CARRIER_UP:
|
|
629 if (s->modem == T31_CNG_TONE || s->modem == T31_NOCNG_TONE || s->modem == T31_V21_RX)
|
|
630 {
|
|
631 s->at_state.rx_signal_present = TRUE;
|
|
632 s->rx_message_received = FALSE;
|
|
633 }
|
|
634 break;
|
|
635 case PUTBIT_CARRIER_DOWN:
|
|
636 if (s->rx_message_received)
|
|
637 {
|
|
638 if (s->at_state.dte_is_waiting)
|
|
639 {
|
|
640 if (s->at_state.ok_is_pending)
|
|
641 {
|
|
642 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK);
|
|
643 s->at_state.ok_is_pending = FALSE;
|
|
644 }
|
|
645 else
|
|
646 {
|
|
647 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_NO_CARRIER);
|
|
648 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
|
|
649 }
|
|
650 s->at_state.dte_is_waiting = FALSE;
|
|
651 }
|
|
652 else
|
|
653 {
|
|
654 buf[0] = AT_RESPONSE_CODE_NO_CARRIER;
|
|
655 queue_write_msg(&(s->rx_queue), buf, 1);
|
|
656 }
|
|
657 }
|
|
658 s->at_state.rx_signal_present = FALSE;
|
|
659 s->at_state.rx_trained = FALSE;
|
|
660 break;
|
|
661 case PUTBIT_FRAMING_OK:
|
|
662 if (s->modem == T31_CNG_TONE || s->modem == T31_NOCNG_TONE)
|
|
663 {
|
|
664 /* Once we get any valid HDLC the CNG tone stops, and we drop
|
|
665 to the V.21 receive modem on its own. */
|
|
666 s->modem = T31_V21_RX;
|
|
667 s->at_state.transmit = FALSE;
|
|
668 }
|
|
669 if (s->modem != T31_V21_RX)
|
|
670 {
|
|
671 /* V.21 has been detected while expecting a different carrier.
|
|
672 If +FAR=0 then result +FCERROR and return to command-mode.
|
|
673 If +FAR=1 then report +FRH:3 and CONNECT, switching to
|
|
674 V.21 receive mode. */
|
|
675 if (s->at_state.p.adaptive_receive)
|
|
676 {
|
|
677 s->at_state.rx_signal_present = TRUE;
|
|
678 s->rx_message_received = TRUE;
|
|
679 s->modem = T31_V21_RX;
|
|
680 s->at_state.transmit = FALSE;
|
|
681 s->at_state.dte_is_waiting = TRUE;
|
|
682 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_FRH3);
|
|
683 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
|
|
684 }
|
|
685 else
|
|
686 {
|
|
687 s->modem = T31_SILENCE_TX;
|
|
688 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
|
|
689 s->rx_message_received = FALSE;
|
|
690 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_FCERROR);
|
|
691 }
|
|
692 }
|
|
693 else
|
|
694 {
|
|
695 if (!s->rx_message_received)
|
|
696 {
|
|
697 /* Report CONNECT as soon as possible to avoid a timeout. */
|
|
698 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
|
|
699 s->rx_message_received = TRUE;
|
|
700 }
|
|
701 }
|
|
702 break;
|
|
703 case PUTBIT_ABORT:
|
|
704 /* Just ignore these */
|
|
705 break;
|
|
706 default:
|
|
707 span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected HDLC special length - %d!\n", len);
|
|
708 break;
|
|
709 }
|
|
710 return;
|
|
711 }
|
|
712 /* If OK is pending then we just ignore whatever comes in */
|
|
713 if (!s->at_state.ok_is_pending)
|
|
714 {
|
|
715 if (s->at_state.dte_is_waiting)
|
|
716 {
|
|
717 /* Send straight away */
|
|
718 /* It is safe to look at the two bytes beyond the length of the message,
|
|
719 and expect to find the FCS there. */
|
|
720 for (i = 0; i < len + 2; i++)
|
|
721 {
|
|
722 if (msg[i] == DLE)
|
|
723 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE;
|
|
724 s->at_state.rx_data[s->at_state.rx_data_bytes++] = msg[i];
|
|
725 }
|
|
726 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE;
|
|
727 s->at_state.rx_data[s->at_state.rx_data_bytes++] = ETX;
|
|
728 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);
|
|
729 s->at_state.rx_data_bytes = 0;
|
|
730 if (msg[1] == 0x13 && ok)
|
|
731 {
|
|
732 /* This is the last frame. We don't send OK until the carrier drops to avoid
|
|
733 redetecting it later. */
|
|
734 s->at_state.ok_is_pending = TRUE;
|
|
735 }
|
|
736 else
|
|
737 {
|
|
738 at_put_response_code(&s->at_state, (ok) ? AT_RESPONSE_CODE_OK : AT_RESPONSE_CODE_ERROR);
|
|
739 s->at_state.dte_is_waiting = FALSE;
|
|
740 s->rx_message_received = FALSE;
|
|
741 }
|
|
742 }
|
|
743 else
|
|
744 {
|
|
745 /* Queue it */
|
|
746 buf[0] = (ok) ? AT_RESPONSE_CODE_OK : AT_RESPONSE_CODE_ERROR;
|
|
747 /* It is safe to look at the two bytes beyond the length of the message,
|
|
748 and expect to find the FCS there. */
|
|
749 memcpy(buf + 1, msg, len + 2);
|
|
750 queue_write_msg(&(s->rx_queue), buf, len + 3);
|
|
751 }
|
|
752 }
|
|
753 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
|
|
754 }
|
|
755 /*- End of function --------------------------------------------------------*/
|
|
756
|
|
757 static void t31_v21_rx(t31_state_t *s)
|
|
758 {
|
|
759 hdlc_rx_init(&(s->hdlcrx), FALSE, TRUE, 5, hdlc_accept, s);
|
|
760 s->at_state.ok_is_pending = FALSE;
|
|
761 s->hdlc_final = FALSE;
|
|
762 s->hdlc_tx_len = 0;
|
|
763 s->dled = FALSE;
|
|
764 fsk_rx_init(&(s->v21rx), &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) hdlc_rx_put_bit, &(s->hdlcrx));
|
|
765 s->at_state.transmit = TRUE;
|
|
766 }
|
|
767 /*- End of function --------------------------------------------------------*/
|
|
768
|
|
769 static int restart_modem(t31_state_t *s, int new_modem)
|
|
770 {
|
|
771 tone_gen_descriptor_t tone_desc;
|
|
772 int ind;
|
|
773
|
|
774 span_log(&s->logging, SPAN_LOG_FLOW, "Restart modem %d\n", new_modem);
|
|
775 if (s->modem == new_modem)
|
|
776 return 0;
|
|
777 queue_flush(&(s->rx_queue));
|
|
778 s->modem = new_modem;
|
|
779 s->data_final = FALSE;
|
|
780 s->at_state.rx_signal_present = FALSE;
|
|
781 s->at_state.rx_trained = FALSE;
|
|
782 s->rx_message_received = FALSE;
|
|
783 s->rx_handler = (span_rx_handler_t *) &dummy_rx;
|
|
784 s->rx_user_data = NULL;
|
|
785 switch (s->modem)
|
|
786 {
|
|
787 case T31_CNG_TONE:
|
|
788 if (s->t38_mode)
|
|
789 {
|
|
790 t38_core_send_indicator(&s->t38, T38_IND_CNG, INDICATOR_TX_COUNT);
|
|
791 }
|
|
792 else
|
|
793 {
|
|
794 /* CNG is special, since we need to receive V.21 HDLC messages while sending the
|
|
795 tone. Everything else in FAX processing sends only one way at a time. */
|
|
796 /* 0.5s of 1100Hz + 3.0s of silence repeating */
|
|
797 make_tone_gen_descriptor(&tone_desc,
|
|
798 1100,
|
|
799 -11,
|
|
800 0,
|
|
801 0,
|
|
802 500,
|
|
803 3000,
|
|
804 0,
|
|
805 0,
|
|
806 TRUE);
|
|
807 tone_gen_init(&(s->tone_gen), &tone_desc);
|
|
808 /* Do V.21/HDLC receive in parallel. The other end may send its
|
|
809 first message at any time. The CNG tone will continue until
|
|
810 we get a valid preamble. */
|
|
811 s->rx_handler = (span_rx_handler_t *) &cng_rx;
|
|
812 s->rx_user_data = s;
|
|
813 t31_v21_rx(s);
|
|
814 s->tx_handler = (span_tx_handler_t *) &tone_gen;
|
|
815 s->tx_user_data = &(s->tone_gen);
|
|
816 s->next_tx_handler = NULL;
|
|
817 }
|
|
818 s->at_state.transmit = TRUE;
|
|
819 break;
|
|
820 case T31_NOCNG_TONE:
|
|
821 if (s->t38_mode)
|
|
822 {
|
|
823 }
|
|
824 else
|
|
825 {
|
|
826 s->rx_handler = (span_rx_handler_t *) &cng_rx;
|
|
827 s->rx_user_data = s;
|
|
828 t31_v21_rx(s);
|
|
829 silence_gen_set(&(s->silence_gen), 0);
|
|
830 s->tx_handler = (span_tx_handler_t *) &silence_gen;
|
|
831 s->tx_user_data = &(s->silence_gen);
|
|
832 }
|
|
833 s->at_state.transmit = FALSE;
|
|
834 break;
|
|
835 case T31_CED_TONE:
|
|
836 if (s->t38_mode)
|
|
837 {
|
|
838 t38_core_send_indicator(&s->t38, T38_IND_CED, INDICATOR_TX_COUNT);
|
|
839 }
|
|
840 else
|
|
841 {
|
|
842 silence_gen_alter(&(s->silence_gen), ms_to_samples(200));
|
|
843 make_tone_gen_descriptor(&tone_desc,
|
|
844 2100,
|
|
845 -11,
|
|
846 0,
|
|
847 0,
|
|
848 2600,
|
|
849 75,
|
|
850 0,
|
|
851 0,
|
|
852 FALSE);
|
|
853 tone_gen_init(&(s->tone_gen), &tone_desc);
|
|
854 s->tx_handler = (span_tx_handler_t *) &silence_gen;
|
|
855 s->tx_user_data = &(s->silence_gen);
|
|
856 s->next_tx_handler = (span_tx_handler_t *) &tone_gen;
|
|
857 s->next_tx_user_data = &(s->tone_gen);
|
|
858 }
|
|
859 s->at_state.transmit = TRUE;
|
|
860 break;
|
|
861 case T31_V21_TX:
|
|
862 if (s->t38_mode)
|
|
863 {
|
|
864 t38_core_send_indicator(&s->t38, T38_IND_V21_PREAMBLE, INDICATOR_TX_COUNT);
|
|
865 }
|
|
866 else
|
|
867 {
|
|
868 hdlc_tx_init(&(s->hdlctx), FALSE, 2, FALSE, hdlc_tx_underflow, s);
|
|
869 /* The spec says 1s +-15% of preamble. So, the minimum is 32 octets. */
|
|
870 hdlc_tx_preamble(&(s->hdlctx), 32);
|
|
871 fsk_tx_init(&(s->v21tx), &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &(s->hdlctx));
|
|
872 s->tx_handler = (span_tx_handler_t *) &fsk_tx;
|
|
873 s->tx_user_data = &(s->v21tx);
|
|
874 s->next_tx_handler = NULL;
|
|
875 }
|
|
876 s->hdlc_final = FALSE;
|
|
877 s->hdlc_tx_len = 0;
|
|
878 s->dled = FALSE;
|
|
879 s->at_state.transmit = TRUE;
|
|
880 break;
|
|
881 case T31_V21_RX:
|
|
882 if (s->t38_mode)
|
|
883 {
|
|
884 }
|
|
885 else
|
|
886 {
|
|
887 s->rx_handler = (span_rx_handler_t *) &fsk_rx;
|
|
888 s->rx_user_data = &(s->v21rx);
|
|
889 t31_v21_rx(s);
|
|
890 }
|
|
891 break;
|
|
892 #if defined(ENABLE_V17)
|
|
893 case T31_V17_TX:
|
|
894 if (s->t38_mode)
|
|
895 {
|
|
896 switch (s->bit_rate)
|
|
897 {
|
|
898 case 7200:
|
|
899 ind = (s->short_train) ? T38_IND_V17_7200_SHORT_TRAINING : T38_IND_V17_7200_LONG_TRAINING;
|
|
900 break;
|
|
901 case 9600:
|
|
902 ind = (s->short_train) ? T38_IND_V17_9600_SHORT_TRAINING : T38_IND_V17_9600_LONG_TRAINING;
|
|
903 break;
|
|
904 case 12000:
|
|
905 ind = (s->short_train) ? T38_IND_V17_12000_SHORT_TRAINING : T38_IND_V17_12000_LONG_TRAINING;
|
|
906 break;
|
|
907 case 14400:
|
|
908 default:
|
|
909 ind = (s->short_train) ? T38_IND_V17_14400_SHORT_TRAINING : T38_IND_V17_14400_LONG_TRAINING;
|
|
910 break;
|
|
911 }
|
|
912 t38_core_send_indicator(&s->t38, ind, INDICATOR_TX_COUNT);
|
|
913 }
|
|
914 else
|
|
915 {
|
|
916 v17_tx_restart(&(s->v17tx), s->bit_rate, FALSE, s->short_train);
|
|
917 s->tx_handler = (span_tx_handler_t *) &v17_tx;
|
|
918 s->tx_user_data = &(s->v17tx);
|
|
919 s->next_tx_handler = NULL;
|
|
920 }
|
|
921 s->tx_out_bytes = 0;
|
|
922 s->tx_data_started = FALSE;
|
|
923 s->at_state.transmit = TRUE;
|
|
924 break;
|
|
925 case T31_V17_RX:
|
|
926 if (!s->t38_mode)
|
|
927 {
|
|
928 s->rx_handler = (span_rx_handler_t *) &early_v17_rx;
|
|
929 s->rx_user_data = s;
|
|
930 v17_rx_restart(&(s->v17rx), s->bit_rate, s->short_train);
|
|
931 /* Allow for +FCERROR/+FRH:3 */
|
|
932 t31_v21_rx(s);
|
|
933 }
|
|
934 s->at_state.transmit = FALSE;
|
|
935 break;
|
|
936 #endif
|
|
937 case T31_V27TER_TX:
|
|
938 if (s->t38_mode)
|
|
939 {
|
|
940 switch (s->bit_rate)
|
|
941 {
|
|
942 case 2400:
|
|
943 ind = T38_IND_V27TER_2400_TRAINING;
|
|
944 break;
|
|
945 case 4800:
|
|
946 default:
|
|
947 ind = T38_IND_V27TER_4800_TRAINING;
|
|
948 break;
|
|
949 }
|
|
950 t38_core_send_indicator(&s->t38, ind, INDICATOR_TX_COUNT);
|
|
951 }
|
|
952 else
|
|
953 {
|
|
954 v27ter_tx_restart(&(s->v27ter_tx), s->bit_rate, FALSE);
|
|
955 s->tx_handler = (span_tx_handler_t *) &v27ter_tx;
|
|
956 s->tx_user_data = &(s->v27ter_tx);
|
|
957 s->next_tx_handler = NULL;
|
|
958 }
|
|
959 s->tx_out_bytes = 0;
|
|
960 s->tx_data_started = FALSE;
|
|
961 s->at_state.transmit = TRUE;
|
|
962 break;
|
|
963 case T31_V27TER_RX:
|
|
964 if (!s->t38_mode)
|
|
965 {
|
|
966 s->rx_handler = (span_rx_handler_t *) &early_v27ter_rx;
|
|
967 s->rx_user_data = s;
|
|
968 v27ter_rx_restart(&(s->v27ter_rx), s->bit_rate, FALSE);
|
|
969 /* Allow for +FCERROR/+FRH:3 */
|
|
970 t31_v21_rx(s);
|
|
971 }
|
|
972 s->at_state.transmit = FALSE;
|
|
973 break;
|
|
974 case T31_V29_TX:
|
|
975 if (s->t38_mode)
|
|
976 {
|
|
977 switch (s->bit_rate)
|
|
978 {
|
|
979 case 7200:
|
|
980 ind = T38_IND_V29_7200_TRAINING;
|
|
981 break;
|
|
982 case 9600:
|
|
983 default:
|
|
984 ind = T38_IND_V29_9600_TRAINING;
|
|
985 break;
|
|
986 }
|
|
987 t38_core_send_indicator(&s->t38, ind, INDICATOR_TX_COUNT);
|
|
988 }
|
|
989 else
|
|
990 {
|
|
991 v29_tx_restart(&(s->v29tx), s->bit_rate, FALSE);
|
|
992 s->tx_handler = (span_tx_handler_t *) &v29_tx;
|
|
993 s->tx_user_data = &(s->v29tx);
|
|
994 s->next_tx_handler = NULL;
|
|
995 }
|
|
996 s->tx_out_bytes = 0;
|
|
997 s->tx_data_started = FALSE;
|
|
998 s->at_state.transmit = TRUE;
|
|
999 break;
|
|
1000 case T31_V29_RX:
|
|
1001 if (!s->t38_mode)
|
|
1002 {
|
|
1003 s->rx_handler = (span_rx_handler_t *) &early_v29_rx;
|
|
1004 s->rx_user_data = s;
|
|
1005 v29_rx_restart(&(s->v29rx), s->bit_rate, FALSE);
|
|
1006 /* Allow for +FCERROR/+FRH:3 */
|
|
1007 t31_v21_rx(s);
|
|
1008 }
|
|
1009 s->at_state.transmit = FALSE;
|
|
1010 break;
|
|
1011 case T31_SILENCE_TX:
|
|
1012 if (s->t38_mode)
|
|
1013 {
|
|
1014 t38_core_send_indicator(&s->t38, T38_IND_NO_SIGNAL, INDICATOR_TX_COUNT);
|
|
1015 }
|
|
1016 else
|
|
1017 {
|
|
1018 silence_gen_set(&(s->silence_gen), 0);
|
|
1019 s->tx_handler = (span_tx_handler_t *) &silence_gen;
|
|
1020 s->tx_user_data = &(s->silence_gen);
|
|
1021 s->next_tx_handler = NULL;
|
|
1022 }
|
|
1023 s->at_state.transmit = FALSE;
|
|
1024 break;
|
|
1025 case T31_SILENCE_RX:
|
|
1026 if (!s->t38_mode)
|
|
1027 {
|
|
1028 s->rx_handler = (span_rx_handler_t *) &silence_rx;
|
|
1029 s->rx_user_data = s;
|
|
1030
|
|
1031 silence_gen_set(&(s->silence_gen), 0);
|
|
1032 s->tx_handler = (span_tx_handler_t *) &silence_gen;
|
|
1033 s->tx_user_data = &(s->silence_gen);
|
|
1034 s->next_tx_handler = NULL;
|
|
1035 }
|
|
1036 s->at_state.transmit = FALSE;
|
|
1037 break;
|
|
1038 case T31_FLUSH:
|
|
1039 /* Send 200ms of silence to "push" the last audio out */
|
|
1040 if (s->t38_mode)
|
|
1041 {
|
|
1042 t38_core_send_indicator(&s->t38, T38_IND_NO_SIGNAL, INDICATOR_TX_COUNT);
|
|
1043 }
|
|
1044 else
|
|
1045 {
|
|
1046 s->modem = T31_SILENCE_TX;
|
|
1047 silence_gen_alter(&(s->silence_gen), ms_to_samples(200));
|
|
1048 s->tx_handler = (span_tx_handler_t *) &silence_gen;
|
|
1049 s->tx_user_data = &(s->silence_gen);
|
|
1050 s->next_tx_handler = NULL;
|
|
1051 s->at_state.transmit = TRUE;
|
|
1052 }
|
|
1053 break;
|
|
1054 }
|
|
1055 s->bit_no = 0;
|
|
1056 s->current_byte = 0xFF;
|
|
1057 s->tx_in_bytes = 0;
|
|
1058 return 0;
|
|
1059 }
|
|
1060 /*- End of function --------------------------------------------------------*/
|
|
1061
|
|
1062 static __inline__ void dle_unstuff_hdlc(t31_state_t *s, const char *stuffed, int len)
|
|
1063 {
|
|
1064 int i;
|
|
1065
|
|
1066 for (i = 0; i < len; i++)
|
|
1067 {
|
|
1068 if (s->dled)
|
|
1069 {
|
|
1070 s->dled = FALSE;
|
|
1071 if (stuffed[i] == ETX)
|
|
1072 {
|
|
1073 if (s->t38_mode)
|
|
1074 {
|
|
1075 }
|
|
1076 else
|
|
1077 {
|
|
1078 hdlc_tx_frame(&(s->hdlctx), s->hdlc_tx_buf, s->hdlc_tx_len);
|
|
1079 }
|
|
1080 s->hdlc_final = (s->hdlc_tx_buf[1] & 0x10);
|
|
1081 s->hdlc_tx_len = 0;
|
|
1082 }
|
|
1083 else if (stuffed[i] == SUB)
|
|
1084 {
|
|
1085 s->hdlc_tx_buf[s->hdlc_tx_len++] = DLE;
|
|
1086 s->hdlc_tx_buf[s->hdlc_tx_len++] = DLE;
|
|
1087 }
|
|
1088 else
|
|
1089 {
|
|
1090 s->hdlc_tx_buf[s->hdlc_tx_len++] = stuffed[i];
|
|
1091 }
|
|
1092 }
|
|
1093 else
|
|
1094 {
|
|
1095 if (stuffed[i] == DLE)
|
|
1096 s->dled = TRUE;
|
|
1097 else
|
|
1098 s->hdlc_tx_buf[s->hdlc_tx_len++] = stuffed[i];
|
|
1099 }
|
|
1100 }
|
|
1101 }
|
|
1102 /*- End of function --------------------------------------------------------*/
|
|
1103
|
|
1104 static __inline__ void dle_unstuff(t31_state_t *s, const char *stuffed, int len)
|
|
1105 {
|
|
1106 int i;
|
|
1107 int fill;
|
|
1108 int next;
|
|
1109
|
|
1110 for (i = 0; i < len; i++)
|
|
1111 {
|
|
1112 if (s->dled)
|
|
1113 {
|
|
1114 s->dled = FALSE;
|
|
1115 if (stuffed[i] == ETX)
|
|
1116 {
|
|
1117 s->data_final = TRUE;
|
|
1118 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
|
|
1119 return;
|
|
1120 }
|
|
1121 }
|
|
1122 else if (stuffed[i] == DLE)
|
|
1123 {
|
|
1124 s->dled = TRUE;
|
|
1125 continue;
|
|
1126 }
|
|
1127 s->tx_data[s->tx_in_bytes] = stuffed[i];
|
|
1128 next = (s->tx_in_bytes + 1) & (T31_TX_BUF_LEN - 1);
|
|
1129 if (next == s->tx_out_bytes)
|
|
1130 {
|
|
1131 /* Oops. We hit the end of the buffer. Give up. Loose stuff. :-( */
|
|
1132 return;
|
|
1133 }
|
|
1134 s->tx_in_bytes = next;
|
|
1135 }
|
|
1136 if (!s->tx_holding)
|
|
1137 {
|
|
1138 /* See if the buffer is approaching full. We might need to apply flow control. */
|
|
1139 fill = (s->tx_in_bytes - s->tx_out_bytes);
|
|
1140 if (s->tx_in_bytes < s->tx_out_bytes)
|
|
1141 fill += (T31_TX_BUF_LEN + 1);
|
|
1142 if (fill > T31_TX_BUF_HIGH_TIDE)
|
|
1143 {
|
|
1144 s->tx_holding = TRUE;
|
|
1145 /* Tell the application to hold further data */
|
|
1146 at_modem_control(&s->at_state, AT_MODEM_CONTROL_CTS, (void *) 0);
|
|
1147 }
|
|
1148 }
|
|
1149 }
|
|
1150 /*- End of function --------------------------------------------------------*/
|
|
1151
|
|
1152 static int process_class1_cmd(at_state_t *t, void *user_data, int direction, int operation, int val)
|
|
1153 {
|
|
1154 int new_modem;
|
|
1155 int new_transmit;
|
|
1156 int i;
|
|
1157 int len;
|
|
1158 int immediate_response;
|
|
1159 t31_state_t *s;
|
|
1160 uint8_t msg[256];
|
|
1161
|
|
1162 s = (t31_state_t *) user_data;
|
|
1163 new_transmit = direction;
|
|
1164 immediate_response = TRUE;
|
|
1165 switch (operation)
|
|
1166 {
|
|
1167 case 'S':
|
|
1168 s->at_state.transmit = new_transmit;
|
|
1169 if (new_transmit)
|
|
1170 {
|
|
1171 /* Send a specified period of silence, to space transmissions. */
|
|
1172 restart_modem(s, T31_SILENCE_TX);
|
|
1173 silence_gen_alter(&(s->silence_gen), val*80);
|
|
1174 s->at_state.transmit = TRUE;
|
|
1175 }
|
|
1176 else
|
|
1177 {
|
|
1178 /* Wait until we have received a specified period of silence. */
|
|
1179 queue_flush(&(s->rx_queue));
|
|
1180 s->silence_awaited = val*80;
|
|
1181 t31_set_at_rx_mode(s, AT_MODE_DELIVERY);
|
|
1182 restart_modem(s, T31_SILENCE_RX);
|
|
1183 }
|
|
1184 immediate_response = FALSE;
|
|
1185 span_log(&s->logging, SPAN_LOG_FLOW, "Silence %dms\n", val*10);
|
|
1186 break;
|
|
1187 case 'H':
|
|
1188 switch (val)
|
|
1189 {
|
|
1190 case 3:
|
|
1191 new_modem = (new_transmit) ? T31_V21_TX : T31_V21_RX;
|
|
1192 s->short_train = FALSE;
|
|
1193 s->bit_rate = 300;
|
|
1194 break;
|
|
1195 default:
|
|
1196 return -1;
|
|
1197 }
|
|
1198 span_log(&s->logging, SPAN_LOG_FLOW, "HDLC\n");
|
|
1199 if (new_modem != s->modem)
|
|
1200 {
|
|
1201 restart_modem(s, new_modem);
|
|
1202 immediate_response = FALSE;
|
|
1203 }
|
|
1204 s->at_state.transmit = new_transmit;
|
|
1205 if (new_transmit)
|
|
1206 {
|
|
1207 t31_set_at_rx_mode(s, AT_MODE_HDLC);
|
|
1208 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
|
|
1209 }
|
|
1210 else
|
|
1211 {
|
|
1212 /* Send straight away, if there is something queued. */
|
|
1213 t31_set_at_rx_mode(s, AT_MODE_DELIVERY);
|
|
1214 s->rx_message_received = FALSE;
|
|
1215 do
|
|
1216 {
|
|
1217 if (!queue_empty(&(s->rx_queue)))
|
|
1218 {
|
|
1219 len = queue_read_msg(&(s->rx_queue), msg, 256);
|
|
1220 if (len > 1)
|
|
1221 {
|
|
1222 if (msg[0] == AT_RESPONSE_CODE_OK)
|
|
1223 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
|
|
1224 for (i = 1; i < len; i++)
|
|
1225 {
|
|
1226 if (msg[i] == DLE)
|
|
1227 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE;
|
|
1228 s->at_state.rx_data[s->at_state.rx_data_bytes++] = msg[i];
|
|
1229 }
|
|
1230 s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE;
|
|
1231 s->at_state.rx_data[s->at_state.rx_data_bytes++] = ETX;
|
|
1232 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);
|
|
1233 s->at_state.rx_data_bytes = 0;
|
|
1234 }
|
|
1235 at_put_response_code(&s->at_state, msg[0]);
|
|
1236 }
|
|
1237 else
|
|
1238 {
|
|
1239 s->at_state.dte_is_waiting = TRUE;
|
|
1240 break;
|
|
1241 }
|
|
1242 }
|
|
1243 while (msg[0] == AT_RESPONSE_CODE_CONNECT);
|
|
1244 }
|
|
1245 immediate_response = FALSE;
|
|
1246 break;
|
|
1247 default:
|
|
1248 switch (val)
|
|
1249 {
|
|
1250 case 24:
|
|
1251 new_modem = (new_transmit) ? T31_V27TER_TX : T31_V27TER_RX;
|
|
1252 s->short_train = FALSE;
|
|
1253 s->bit_rate = 2400;
|
|
1254 break;
|
|
1255 case 48:
|
|
1256 new_modem = (new_transmit) ? T31_V27TER_TX : T31_V27TER_RX;
|
|
1257 s->short_train = FALSE;
|
|
1258 s->bit_rate = 4800;
|
|
1259 break;
|
|
1260 case 72:
|
|
1261 new_modem = (new_transmit) ? T31_V29_TX : T31_V29_RX;
|
|
1262 s->short_train = FALSE;
|
|
1263 s->bit_rate = 7200;
|
|
1264 break;
|
|
1265 case 96:
|
|
1266 new_modem = (new_transmit) ? T31_V29_TX : T31_V29_RX;
|
|
1267 s->short_train = FALSE;
|
|
1268 s->bit_rate = 9600;
|
|
1269 break;
|
|
1270 #if defined(ENABLE_V17)
|
|
1271 case 73:
|
|
1272 new_modem = (new_transmit) ? T31_V17_TX : T31_V17_RX;
|
|
1273 s->short_train = FALSE;
|
|
1274 s->bit_rate = 7200;
|
|
1275 break;
|
|
1276 case 74:
|
|
1277 new_modem = (new_transmit) ? T31_V17_TX : T31_V17_RX;
|
|
1278 s->short_train = TRUE;
|
|
1279 s->bit_rate = 7200;
|
|
1280 break;
|
|
1281 case 97:
|
|
1282 new_modem = (new_transmit) ? T31_V17_TX : T31_V17_RX;
|
|
1283 s->short_train = FALSE;
|
|
1284 s->bit_rate = 9600;
|
|
1285 break;
|
|
1286 case 98:
|
|
1287 new_modem = (new_transmit) ? T31_V17_TX : T31_V17_RX;
|
|
1288 s->short_train = TRUE;
|
|
1289 s->bit_rate = 9600;
|
|
1290 break;
|
|
1291 case 121:
|
|
1292 new_modem = (new_transmit) ? T31_V17_TX : T31_V17_RX;
|
|
1293 s->short_train = FALSE;
|
|
1294 s->bit_rate = 12000;
|
|
1295 break;
|
|
1296 case 122:
|
|
1297 new_modem = (new_transmit) ? T31_V17_TX : T31_V17_RX;
|
|
1298 s->short_train = TRUE;
|
|
1299 s->bit_rate = 12000;
|
|
1300 break;
|
|
1301 case 145:
|
|
1302 new_modem = (new_transmit) ? T31_V17_TX : T31_V17_RX;
|
|
1303 s->short_train = FALSE;
|
|
1304 s->bit_rate = 14400;
|
|
1305 break;
|
|
1306 case 146:
|
|
1307 new_modem = (new_transmit) ? T31_V17_TX : T31_V17_RX;
|
|
1308 s->short_train = TRUE;
|
|
1309 s->bit_rate = 14400;
|
|
1310 break;
|
|
1311 #endif
|
|
1312 default:
|
|
1313 return -1;
|
|
1314 }
|
|
1315 span_log(&s->logging, SPAN_LOG_FLOW, "Short training = %d, bit rate = %d\n", s->short_train, s->bit_rate);
|
|
1316 if (new_transmit)
|
|
1317 {
|
|
1318 t31_set_at_rx_mode(s, AT_MODE_STUFFED);
|
|
1319 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
|
|
1320 }
|
|
1321 else
|
|
1322 {
|
|
1323 t31_set_at_rx_mode(s, AT_MODE_DELIVERY);
|
|
1324 }
|
|
1325 restart_modem(s, new_modem);
|
|
1326 immediate_response = FALSE;
|
|
1327 break;
|
|
1328 }
|
|
1329 return immediate_response;
|
|
1330 }
|
|
1331 /*- End of function --------------------------------------------------------*/
|
|
1332
|
|
1333 void t31_call_event(t31_state_t *s, int event)
|
|
1334 {
|
|
1335 span_log(&s->logging, SPAN_LOG_FLOW, "Call event %d received\n", event);
|
|
1336 at_call_event(&s->at_state, event);
|
|
1337 }
|
|
1338 /*- End of function --------------------------------------------------------*/
|
|
1339
|
|
1340 int t31_at_rx(t31_state_t *s, const char *t, int len)
|
|
1341 {
|
|
1342 if (s->dte_data_timeout)
|
|
1343 s->dte_data_timeout = s->call_samples + ms_to_samples(5000);
|
|
1344 switch (s->at_state.at_rx_mode)
|
|
1345 {
|
|
1346 case AT_MODE_ONHOOK_COMMAND:
|
|
1347 case AT_MODE_OFFHOOK_COMMAND:
|
|
1348 at_interpreter(&s->at_state, t, len);
|
|
1349 break;
|
|
1350 case AT_MODE_DELIVERY:
|
|
1351 /* Data from the DTE in this state returns us to command mode */
|
|
1352 if (len)
|
|
1353 {
|
|
1354 s->at_state.rx_data_bytes = 0;
|
|
1355 s->at_state.transmit = FALSE;
|
|
1356 s->modem = T31_SILENCE_TX;
|
|
1357 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
|
|
1358 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK);
|
|
1359 }
|
|
1360 break;
|
|
1361 case AT_MODE_HDLC:
|
|
1362 dle_unstuff_hdlc(s, t, len);
|
|
1363 break;
|
|
1364 case AT_MODE_STUFFED:
|
|
1365 dle_unstuff(s, t, len);
|
|
1366 break;
|
|
1367 }
|
|
1368 return len;
|
|
1369 }
|
|
1370 /*- End of function --------------------------------------------------------*/
|
|
1371
|
|
1372 static int dummy_rx(void *user_data, const int16_t amp[], int len)
|
|
1373 {
|
|
1374 return 0;
|
|
1375 }
|
|
1376 /*- End of function --------------------------------------------------------*/
|
|
1377
|
|
1378 static int silence_rx(void *user_data, const int16_t amp[], int len)
|
|
1379 {
|
|
1380 t31_state_t *s;
|
|
1381
|
|
1382 /* Searching for a specified minimum period of silence. */
|
|
1383 s = (t31_state_t *) user_data;
|
|
1384 if (s->silence_awaited && s->silence_heard >= s->silence_awaited)
|
|
1385 {
|
|
1386 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK);
|
|
1387 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
|
|
1388 s->silence_heard = 0;
|
|
1389 s->silence_awaited = 0;
|
|
1390 }
|
|
1391 return 0;
|
|
1392 }
|
|
1393 /*- End of function --------------------------------------------------------*/
|
|
1394
|
|
1395 static int cng_rx(void *user_data, const int16_t amp[], int len)
|
|
1396 {
|
|
1397 t31_state_t *s;
|
|
1398
|
|
1399 s = (t31_state_t *) user_data;
|
|
1400 if (s->call_samples > ms_to_samples(s->at_state.p.s_regs[7]*1000))
|
|
1401 {
|
|
1402 /* After calling, S7 has elapsed... no carrier found. */
|
|
1403 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_NO_CARRIER);
|
|
1404 restart_modem(s, T31_SILENCE_TX);
|
|
1405 at_modem_control(&s->at_state, AT_MODEM_CONTROL_HANGUP, NULL);
|
|
1406 t31_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
|
|
1407 }
|
|
1408 else
|
|
1409 {
|
|
1410 fsk_rx(&(s->v21rx), amp, len);
|
|
1411 }
|
|
1412 return 0;
|
|
1413 }
|
|
1414 /*- End of function --------------------------------------------------------*/
|
|
1415
|
|
1416 #if defined(ENABLE_V17)
|
|
1417 static int early_v17_rx(void *user_data, const int16_t amp[], int len)
|
|
1418 {
|
|
1419 t31_state_t *s;
|
|
1420
|
|
1421 s = (t31_state_t *) user_data;
|
|
1422 v17_rx(&(s->v17rx), amp, len);
|
|
1423 if (s->at_state.rx_trained)
|
|
1424 {
|
|
1425 /* The fast modem has trained, so we no longer need to run the slow
|
|
1426 one in parallel. */
|
|
1427 span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.17 (%.2fdBm0)\n", v17_rx_signal_power(&(s->v17rx)));
|
|
1428 s->rx_handler = (span_rx_handler_t *) &v17_rx;
|
|
1429 s->rx_user_data = &(s->v17rx);
|
|
1430 }
|
|
1431 else
|
|
1432 {
|
|
1433 fsk_rx(&(s->v21rx), amp, len);
|
|
1434 if (s->rx_message_received)
|
|
1435 {
|
|
1436 /* We have received something, and the fast modem has not trained. We must
|
|
1437 be receiving valid V.21 */
|
|
1438 span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.21\n");
|
|
1439 s->rx_handler = (span_rx_handler_t *) &fsk_rx;
|
|
1440 s->rx_user_data = &(s->v21rx);
|
|
1441 }
|
|
1442 }
|
|
1443 return len;
|
|
1444 }
|
|
1445 /*- End of function --------------------------------------------------------*/
|
|
1446 #endif
|
|
1447
|
|
1448 static int early_v27ter_rx(void *user_data, const int16_t amp[], int len)
|
|
1449 {
|
|
1450 t31_state_t *s;
|
|
1451
|
|
1452 s = (t31_state_t *) user_data;
|
|
1453 v27ter_rx(&(s->v27ter_rx), amp, len);
|
|
1454 if (s->at_state.rx_trained)
|
|
1455 {
|
|
1456 /* The fast modem has trained, so we no longer need to run the slow
|
|
1457 one in parallel. */
|
|
1458 span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.27ter (%.2fdBm0)\n", v27ter_rx_signal_power(&(s->v27ter_rx)));
|
|
1459 s->rx_handler = (span_rx_handler_t *) &v27ter_rx;
|
|
1460 s->rx_user_data = &(s->v27ter_rx);
|
|
1461 }
|
|
1462 else
|
|
1463 {
|
|
1464 fsk_rx(&(s->v21rx), amp, len);
|
|
1465 if (s->rx_message_received)
|
|
1466 {
|
|
1467 /* We have received something, and the fast modem has not trained. We must
|
|
1468 be receiving valid V.21 */
|
|
1469 span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.21\n");
|
|
1470 s->rx_handler = (span_rx_handler_t *) &fsk_rx;
|
|
1471 s->rx_user_data = &(s->v21rx);
|
|
1472 }
|
|
1473 }
|
|
1474 return len;
|
|
1475 }
|
|
1476 /*- End of function --------------------------------------------------------*/
|
|
1477
|
|
1478 static int early_v29_rx(void *user_data, const int16_t amp[], int len)
|
|
1479 {
|
|
1480 t31_state_t *s;
|
|
1481
|
|
1482 s = (t31_state_t *) user_data;
|
|
1483 v29_rx(&(s->v29rx), amp, len);
|
|
1484 if (s->at_state.rx_trained)
|
|
1485 {
|
|
1486 /* The fast modem has trained, so we no longer need to run the slow
|
|
1487 one in parallel. */
|
|
1488 span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.29 (%.2fdBm0)\n", v29_rx_signal_power(&(s->v29rx)));
|
|
1489 s->rx_handler = (span_rx_handler_t *) &v29_rx;
|
|
1490 s->rx_user_data = &(s->v29rx);
|
|
1491 }
|
|
1492 else
|
|
1493 {
|
|
1494 fsk_rx(&(s->v21rx), amp, len);
|
|
1495 if (s->rx_message_received)
|
|
1496 {
|
|
1497 /* We have received something, and the fast modem has not trained. We must
|
|
1498 be receiving valid V.21 */
|
|
1499 span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.21\n");
|
|
1500 s->rx_handler = (span_rx_handler_t *) &fsk_rx;
|
|
1501 s->rx_user_data = &(s->v21rx);
|
|
1502 }
|
|
1503 }
|
|
1504 return len;
|
|
1505 }
|
|
1506 /*- End of function --------------------------------------------------------*/
|
|
1507
|
|
1508 int t31_rx(t31_state_t *s, int16_t amp[], int len)
|
|
1509 {
|
|
1510 int i;
|
|
1511 int32_t power;
|
|
1512
|
|
1513 /* Monitor for received silence. Maximum needed detection is AT+FRS=255 (255*10ms). */
|
|
1514 /* We could probably only run this loop if (s->modem == T31_SILENCE_RX), however,
|
|
1515 the spec says "when silence has been present on the line for the amount of
|
|
1516 time specified". That means some of the silence may have occurred before
|
|
1517 the AT+FRS=n command. This condition, however, is not likely to ever be the
|
|
1518 case. (AT+FRS=n will usually be issued before the remote goes silent.) */
|
|
1519 for (i = 0; i < len; i++)
|
|
1520 {
|
|
1521 /* Clean up any DC influence. */
|
|
1522 power = power_meter_update(&(s->rx_power), amp[i] - s->last_sample);
|
|
1523 s->last_sample = amp[i];
|
|
1524 if (power > s->silence_threshold_power)
|
|
1525 {
|
|
1526 s->silence_heard = 0;
|
|
1527 }
|
|
1528 else
|
|
1529 {
|
|
1530 if (s->silence_heard <= ms_to_samples(255*10))
|
|
1531 s->silence_heard++;
|
|
1532 }
|
|
1533 }
|
|
1534
|
|
1535 /* Time is determined by counting the samples in audio packets coming in. */
|
|
1536 s->call_samples += len;
|
|
1537
|
|
1538 /* In HDLC transmit mode, if 5 seconds elapse without data from the DTE
|
|
1539 we must treat this as an error. We return the result ERROR, and change
|
|
1540 to command-mode. */
|
|
1541 if (s->dte_data_timeout && s->call_samples > s->dte_data_timeout)
|
|
1542 {
|
|
1543 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
|
|
1544 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_ERROR);
|
|
1545 restart_modem(s, T31_SILENCE_TX);
|
|
1546 }
|
|
1547
|
|
1548 if (!s->at_state.transmit || s->modem == T31_CNG_TONE)
|
|
1549 s->rx_handler(s->rx_user_data, amp, len);
|
|
1550 return 0;
|
|
1551 }
|
|
1552 /*- End of function --------------------------------------------------------*/
|
|
1553
|
|
1554 static int set_next_tx_type(t31_state_t *s)
|
|
1555 {
|
|
1556 if (s->next_tx_handler)
|
|
1557 {
|
|
1558 s->tx_handler = s->next_tx_handler;
|
|
1559 s->tx_user_data = s->next_tx_user_data;
|
|
1560 s->next_tx_handler = NULL;
|
|
1561 return 0;
|
|
1562 }
|
|
1563 /* If there is nothing else to change to, so use zero length silence */
|
|
1564 silence_gen_alter(&(s->silence_gen), 0);
|
|
1565 s->tx_handler = (span_tx_handler_t *) &silence_gen;
|
|
1566 s->tx_user_data = &(s->silence_gen);
|
|
1567 s->next_tx_handler = NULL;
|
|
1568 return -1;
|
|
1569 }
|
|
1570 /*- End of function --------------------------------------------------------*/
|
|
1571
|
|
1572 int t31_tx(t31_state_t *s, int16_t amp[], int max_len)
|
|
1573 {
|
|
1574 int len;
|
|
1575
|
|
1576 len = 0;
|
|
1577 if (s->at_state.transmit)
|
|
1578 {
|
|
1579 if ((len = s->tx_handler(s->tx_user_data, amp, max_len)) < max_len)
|
|
1580 {
|
|
1581 /* Allow for one change of tx handler within a block */
|
|
1582 set_next_tx_type(s);
|
|
1583 if ((len += s->tx_handler(s->tx_user_data, amp + len, max_len - len)) < max_len)
|
|
1584 {
|
|
1585 switch (s->modem)
|
|
1586 {
|
|
1587 case T31_SILENCE_TX:
|
|
1588 s->modem = -1;
|
|
1589 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK);
|
|
1590 if (s->at_state.do_hangup)
|
|
1591 {
|
|
1592 at_modem_control(&s->at_state, AT_MODEM_CONTROL_HANGUP, NULL);
|
|
1593 t31_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
|
|
1594 s->at_state.do_hangup = FALSE;
|
|
1595 }
|
|
1596 else
|
|
1597 {
|
|
1598 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
|
|
1599 }
|
|
1600 break;
|
|
1601 case T31_CED_TONE:
|
|
1602 /* Go directly to V.21/HDLC transmit. */
|
|
1603 s->modem = -1;
|
|
1604 restart_modem(s, T31_V21_TX);
|
|
1605 t31_set_at_rx_mode(s, AT_MODE_HDLC);
|
|
1606 break;
|
|
1607 case T31_V21_TX:
|
|
1608 #if defined(ENABLE_V17)
|
|
1609 case T31_V17_TX:
|
|
1610 #endif
|
|
1611 case T31_V27TER_TX:
|
|
1612 case T31_V29_TX:
|
|
1613 s->modem = -1;
|
|
1614 at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK);
|
|
1615 t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
|
|
1616 restart_modem(s, T31_SILENCE_TX);
|
|
1617 break;
|
|
1618 }
|
|
1619 }
|
|
1620 }
|
|
1621 }
|
|
1622 if (s->transmit_on_idle)
|
|
1623 {
|
|
1624 /* Pad to the requested length with silence */
|
|
1625 memset(amp, 0, max_len*sizeof(int16_t));
|
|
1626 len = max_len;
|
|
1627 }
|
|
1628 return len;
|
|
1629 }
|
|
1630 /*- End of function --------------------------------------------------------*/
|
|
1631
|
|
1632 void t31_set_transmit_on_idle(t31_state_t *s, int transmit_on_idle)
|
|
1633 {
|
|
1634 s->transmit_on_idle = transmit_on_idle;
|
|
1635 }
|
|
1636 /*- End of function --------------------------------------------------------*/
|
|
1637
|
|
1638 t31_state_t *t31_init(t31_state_t *s,
|
|
1639 at_tx_handler_t *at_tx_handler,
|
|
1640 void *at_tx_user_data,
|
|
1641 t31_modem_control_handler_t *modem_control_handler,
|
|
1642 void *modem_control_user_data,
|
|
1643 t38_tx_packet_handler_t *tx_t38_packet_handler,
|
|
1644 void *tx_t38_packet_user_data)
|
|
1645 {
|
|
1646 if (at_tx_handler == NULL || modem_control_handler == NULL)
|
|
1647 return NULL;
|
|
1648
|
|
1649 memset(s, 0, sizeof(*s));
|
|
1650 span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
|
|
1651 span_log_set_protocol(&s->logging, "T.31");
|
|
1652
|
|
1653 s->modem_control_handler = modem_control_handler;
|
|
1654 s->modem_control_user_data = modem_control_user_data;
|
|
1655 #if defined(ENABLE_V17)
|
|
1656 v17_rx_init(&(s->v17rx), 14400, non_ecm_put_bit, s);
|
|
1657 v17_tx_init(&(s->v17tx), 14400, FALSE, non_ecm_get_bit, s);
|
|
1658 #endif
|
|
1659 v29_rx_init(&(s->v29rx), 9600, non_ecm_put_bit, s);
|
|
1660 v29_rx_signal_cutoff(&(s->v29rx), -45.5);
|
|
1661 v29_tx_init(&(s->v29tx), 9600, FALSE, non_ecm_get_bit, s);
|
|
1662 v27ter_rx_init(&(s->v27ter_rx), 4800, non_ecm_put_bit, s);
|
|
1663 v27ter_tx_init(&(s->v27ter_tx), 4800, FALSE, non_ecm_get_bit, s);
|
|
1664 silence_gen_init(&(s->silence_gen), 0);
|
|
1665 power_meter_init(&(s->rx_power), 4);
|
|
1666 s->last_sample = 0;
|
|
1667 s->silence_threshold_power = power_meter_level_dbm0(-43);
|
|
1668 s->at_state.rx_signal_present = FALSE;
|
|
1669 s->at_state.rx_trained = FALSE;
|
|
1670
|
|
1671 s->at_state.do_hangup = FALSE;
|
|
1672 s->at_state.line_ptr = 0;
|
|
1673 s->silence_heard = 0;
|
|
1674 s->silence_awaited = 0;
|
|
1675 s->call_samples = 0;
|
|
1676 s->modem = -1;
|
|
1677 s->at_state.transmit = TRUE;
|
|
1678 s->rx_handler = dummy_rx;
|
|
1679 s->rx_user_data = NULL;
|
|
1680 s->tx_handler = (span_tx_handler_t *) &silence_gen;
|
|
1681 s->tx_user_data = &(s->silence_gen);
|
|
1682
|
|
1683 if (queue_create(&(s->rx_queue), 4096, QUEUE_WRITE_ATOMIC | QUEUE_READ_ATOMIC) < 0)
|
|
1684 return NULL;
|
|
1685 at_init(&s->at_state, at_tx_handler, at_tx_user_data, t31_modem_control_handler, s);
|
|
1686 at_set_class1_handler(&s->at_state, process_class1_cmd, s);
|
|
1687 s->at_state.dte_inactivity_timeout = DEFAULT_DTE_TIMEOUT;
|
|
1688 if (tx_t38_packet_handler)
|
|
1689 {
|
|
1690 t38_core_init(&s->t38, process_rx_indicator, process_rx_data, process_rx_missing, (void *) s);
|
|
1691 s->t38.tx_packet_handler = tx_t38_packet_handler;
|
|
1692 s->t38.tx_packet_user_data = tx_t38_packet_user_data;
|
|
1693 }
|
|
1694 s->t38_mode = FALSE;
|
|
1695 return s;
|
|
1696 }
|
|
1697 /*- End of function --------------------------------------------------------*/
|
|
1698
|
|
1699 int t31_release(t31_state_t *s)
|
|
1700 {
|
|
1701 at_reset_call_info(&s->at_state);
|
|
1702 free(s);
|
|
1703 return 0;
|
|
1704 }
|
|
1705 /*- End of function --------------------------------------------------------*/
|
|
1706 /*- End of file ------------------------------------------------------------*/
|