Fax gateway functionality (i.e. translating between a T.30 terminal and a T.38
[asterisk/asterisk.git] / res / res_fax_spandsp.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009-2010, Digium, Inc.
5  *
6  * Matthew Nicholson <mnicholson@digium.com>
7  *
8  * Initial T.38-gateway code
9  * 2008, Daniel Ferenci <daniel.ferenci@nethemba.com>
10  * Created by Nethemba s.r.o. http://www.nethemba.com
11  * Sponsored by IPEX a.s. http://www.ipex.cz
12  *
13  * T.38-gateway integration into asterisk app_fax and rework
14  * 2008, Gregory Hinton Nietsky <gregory@dnstelecom.co.za>
15  * dns Telecom http://www.dnstelecom.co.za
16  *
17  * Modified to make T.38-gateway compatible with Asterisk 1.6.2
18  * 2010, Anton Verevkin <mymail@verevkin.it>
19  * ViaNetTV http://www.vianettv.com
20  *
21  * Modified to make T.38-gateway work
22  * 2010, Klaus Darilion, IPCom GmbH, www.ipcom.at
23  *
24  * See http://www.asterisk.org for more information about
25  * the Asterisk project. Please do not directly contact
26  * any of the maintainers of this project for assistance;
27  * the project provides a web site, mailing lists and IRC
28  * channels for your use.
29  *
30  * This program is free software, distributed under the terms of
31  * the GNU General Public License Version 2. See the LICENSE file
32  * at the top of the source tree.
33  */
34
35 /*! \file
36  *
37  * \brief Spandsp T.38 and G.711 FAX Resource
38  *
39  * \author Matthew Nicholson <mnicholson@digium.com>
40  *
41  * This module registers the Spandsp FAX technology with the res_fax module.
42  */
43
44 /*** MODULEINFO
45         <depend>spandsp</depend>
46         <depend>res_fax</depend>
47 ***/
48
49 #include "asterisk.h"
50
51 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
52
53 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
54 #include <spandsp.h>
55 #include <spandsp/version.h>
56
57 #include "asterisk/logger.h"
58 #include "asterisk/module.h"
59 #include "asterisk/strings.h"
60 #include "asterisk/cli.h"
61 #include "asterisk/utils.h"
62 #include "asterisk/timing.h"
63 #include "asterisk/astobj2.h"
64 #include "asterisk/res_fax.h"
65 #include "asterisk/channel.h"
66
67 #define SPANDSP_FAX_SAMPLES 160
68 #define SPANDSP_FAX_TIMER_RATE 8000 / SPANDSP_FAX_SAMPLES       /* 50 ticks per second, 20ms, 160 samples per second */
69 #define SPANDSP_ENGAGE_UDPTL_NAT_RETRY 3
70
71 static void *spandsp_fax_new(struct ast_fax_session *s, struct ast_fax_tech_token *token);
72 static void spandsp_fax_destroy(struct ast_fax_session *s);
73 static struct ast_frame *spandsp_fax_read(struct ast_fax_session *s);
74 static int spandsp_fax_write(struct ast_fax_session *s, const struct ast_frame *f);
75 static int spandsp_fax_start(struct ast_fax_session *s);
76 static int spandsp_fax_cancel(struct ast_fax_session *s);
77 static int spandsp_fax_switch_to_t38(struct ast_fax_session *s);
78 static int spandsp_fax_gateway_start(struct ast_fax_session *s);
79 static int spandsp_fax_gateway_process(struct ast_fax_session *s, const struct ast_frame *f);
80 static void spandsp_fax_gateway_cleanup(struct ast_fax_session *s);
81
82 static char *spandsp_fax_cli_show_capabilities(int fd);
83 static char *spandsp_fax_cli_show_session(struct ast_fax_session *s, int fd);
84 static char *spandsp_fax_cli_show_stats(int fd);
85 static char *spandsp_fax_cli_show_settings(int fd);
86
87 static struct ast_fax_tech spandsp_fax_tech = {
88         .type = "Spandsp",
89         .description = "Spandsp FAX Driver",
90 #if SPANDSP_RELEASE_DATE >= 20090220
91         /* spandsp 0.0.6 */
92         .version = SPANDSP_RELEASE_DATETIME_STRING,
93 #else
94         /* spandsp 0.0.5
95          * TODO: maybe we should determine the version better way
96          */
97         .version = "pre-20090220",
98 #endif
99         .caps = AST_FAX_TECH_AUDIO | AST_FAX_TECH_T38 | AST_FAX_TECH_SEND | AST_FAX_TECH_RECEIVE | AST_FAX_TECH_GATEWAY,
100         .new_session = spandsp_fax_new,
101         .destroy_session = spandsp_fax_destroy,
102         .read = spandsp_fax_read,
103         .write = spandsp_fax_write,
104         .start_session = spandsp_fax_start,
105         .cancel_session = spandsp_fax_cancel,
106         .switch_to_t38 = spandsp_fax_switch_to_t38,
107         .cli_show_capabilities = spandsp_fax_cli_show_capabilities,
108         .cli_show_session = spandsp_fax_cli_show_session,
109         .cli_show_stats = spandsp_fax_cli_show_stats,
110         .cli_show_settings = spandsp_fax_cli_show_settings,
111 };
112
113 struct spandsp_fax_stats {
114         int success;
115         int nofax;
116         int neg_failed;
117         int failed_to_train;
118         int rx_protocol_error;
119         int tx_protocol_error;
120         int protocol_error;
121         int retries_exceeded;
122         int file_error;
123         int mem_error;
124         int call_dropped;
125         int unknown_error;
126         int switched;
127 };
128
129 static struct {
130         ast_mutex_t lock;
131         struct spandsp_fax_stats g711;
132         struct spandsp_fax_stats t38;
133 } spandsp_global_stats;
134
135 struct spandsp_pvt {
136         unsigned int ist38:1;
137         unsigned int isdone:1;
138         enum ast_t38_state ast_t38_state;
139         fax_state_t fax_state;
140         t38_terminal_state_t t38_state;
141         t30_state_t *t30_state;
142         t38_core_state_t *t38_core_state;
143
144         struct spandsp_fax_stats *stats;
145
146         struct spandsp_fax_gw_stats *t38stats;
147         t38_gateway_state_t t38_gw_state;
148
149         struct ast_timer *timer;
150         AST_LIST_HEAD(frame_queue, ast_frame) read_frames;
151 };
152
153 static void session_destroy(struct spandsp_pvt *p);
154 static int t38_tx_packet_handler(t38_core_state_t *t38_core_state, void *data, const uint8_t *buf, int len, int count);
155 static void t30_phase_e_handler(t30_state_t *t30_state, void *data, int completion_code);
156 static void spandsp_log(int level, const char *msg);
157 static int update_stats(struct spandsp_pvt *p, int completion_code);
158
159 static void set_logging(logging_state_t *state, struct ast_fax_session_details *details);
160 static void set_local_info(t30_state_t *t30_state, struct ast_fax_session_details *details);
161 static void set_file(t30_state_t *t30_state, struct ast_fax_session_details *details);
162 static void set_ecm(t30_state_t *t30_state, struct ast_fax_session_details *details);
163
164 static void session_destroy(struct spandsp_pvt *p)
165 {
166         struct ast_frame *f;
167
168         t30_terminate(p->t30_state);
169         p->isdone = 1;
170
171         ast_timer_close(p->timer);
172
173         fax_release(&p->fax_state);
174         t38_terminal_release(&p->t38_state);
175
176         while ((f = AST_LIST_REMOVE_HEAD(&p->read_frames, frame_list))) {
177                 ast_frfree(f);
178         }
179 }
180
181 /*! \brief
182  *
183  */
184 static int t38_tx_packet_handler(t38_core_state_t *t38_core_state, void *data, const uint8_t *buf, int len, int count)
185 {
186         int res = -1;
187         struct ast_fax_session *s = data;
188         struct spandsp_pvt *p = s->tech_pvt;
189         struct ast_frame fax_frame = {
190                 .frametype = AST_FRAME_MODEM,
191                 .subclass.integer = AST_MODEM_T38,
192                 .src = "res_fax_spandsp_t38",
193         };
194
195         struct ast_frame *f = &fax_frame;
196
197
198         /* TODO: Asterisk does not provide means of resending the same packet multiple
199           times so count is ignored at the moment */
200
201         AST_FRAME_SET_BUFFER(f, buf, 0, len);
202
203         if (!(f = ast_frisolate(f))) {
204                 return res;
205         }
206
207         if (s->details->caps & AST_FAX_TECH_GATEWAY) {
208                 ast_set_flag(f, AST_FAX_FRFLAG_GATEWAY);
209                 if (p->ast_t38_state == T38_STATE_NEGOTIATED) {
210                         res = ast_write(s->chan, f);
211                 } else {
212                         res = ast_queue_frame(s->chan, f);
213                 }
214                 ast_frfree(f);
215         } else {
216                 /* no need to lock, this all runs in the same thread */
217                 AST_LIST_INSERT_TAIL(&p->read_frames, f, frame_list);
218         }
219
220         return res;
221 }
222
223 static int update_stats(struct spandsp_pvt *p, int completion_code)
224 {
225         switch (completion_code) {
226         case T30_ERR_OK:
227                 ast_atomic_fetchadd_int(&p->stats->success, 1);
228                 break;
229
230         /* Link problems */
231         case T30_ERR_CEDTONE:            /*! The CED tone exceeded 5s */
232         case T30_ERR_T0_EXPIRED:         /*! Timed out waiting for initial communication */
233         case T30_ERR_T1_EXPIRED:         /*! Timed out waiting for the first message */
234         case T30_ERR_T3_EXPIRED:         /*! Timed out waiting for procedural interrupt */
235         case T30_ERR_HDLC_CARRIER:       /*! The HDLC carrier did not stop in a timely manner */
236         case T30_ERR_CANNOT_TRAIN:       /*! Failed to train with any of the compatible modems */
237                 ast_atomic_fetchadd_int(&p->stats->failed_to_train, 1);
238                 break;
239
240         case T30_ERR_OPER_INT_FAIL:      /*! Operator intervention failed */
241         case T30_ERR_INCOMPATIBLE:       /*! Far end is not compatible */
242         case T30_ERR_RX_INCAPABLE:       /*! Far end is not able to receive */
243         case T30_ERR_TX_INCAPABLE:       /*! Far end is not able to transmit */
244         case T30_ERR_NORESSUPPORT:       /*! Far end cannot receive at the resolution of the image */
245         case T30_ERR_NOSIZESUPPORT:      /*! Far end cannot receive at the size of image */
246                 ast_atomic_fetchadd_int(&p->stats->neg_failed, 1);
247                 break;
248
249         case T30_ERR_UNEXPECTED:         /*! Unexpected message received */
250                 ast_atomic_fetchadd_int(&p->stats->protocol_error, 1);
251                 break;
252
253         /* Phase E status values returned to a transmitter */
254         case T30_ERR_TX_BADDCS:          /*! Received bad response to DCS or training */
255         case T30_ERR_TX_BADPG:           /*! Received a DCN from remote after sending a page */
256         case T30_ERR_TX_ECMPHD:          /*! Invalid ECM response received from receiver */
257         case T30_ERR_TX_GOTDCN:          /*! Received a DCN while waiting for a DIS */
258         case T30_ERR_TX_INVALRSP:        /*! Invalid response after sending a page */
259         case T30_ERR_TX_NODIS:           /*! Received other than DIS while waiting for DIS */
260         case T30_ERR_TX_PHBDEAD:         /*! Received no response to DCS, training or TCF */
261         case T30_ERR_TX_PHDDEAD:         /*! No response after sending a page */
262         case T30_ERR_TX_T5EXP:           /*! Timed out waiting for receiver ready (ECM mode) */
263                 ast_atomic_fetchadd_int(&p->stats->tx_protocol_error, 1);
264                 break;
265
266         /* Phase E status values returned to a receiver */
267         case T30_ERR_RX_ECMPHD:          /*! Invalid ECM response received from transmitter */
268         case T30_ERR_RX_GOTDCS:          /*! DCS received while waiting for DTC */
269         case T30_ERR_RX_INVALCMD:        /*! Unexpected command after page received */
270         case T30_ERR_RX_NOCARRIER:       /*! Carrier lost during fax receive */
271         case T30_ERR_RX_NOEOL:           /*! Timed out while waiting for EOL (end Of line) */
272                 ast_atomic_fetchadd_int(&p->stats->rx_protocol_error, 1);
273                 break;
274         case T30_ERR_RX_NOFAX:           /*! Timed out while waiting for first line */
275                 ast_atomic_fetchadd_int(&p->stats->nofax, 1);
276                 break;
277         case T30_ERR_RX_T2EXPDCN:        /*! Timer T2 expired while waiting for DCN */
278         case T30_ERR_RX_T2EXPD:          /*! Timer T2 expired while waiting for phase D */
279         case T30_ERR_RX_T2EXPFAX:        /*! Timer T2 expired while waiting for fax page */
280         case T30_ERR_RX_T2EXPMPS:        /*! Timer T2 expired while waiting for next fax page */
281         case T30_ERR_RX_T2EXPRR:         /*! Timer T2 expired while waiting for RR command */
282         case T30_ERR_RX_T2EXP:           /*! Timer T2 expired while waiting for NSS, DCS or MCF */
283         case T30_ERR_RX_DCNWHY:          /*! Unexpected DCN while waiting for DCS or DIS */
284         case T30_ERR_RX_DCNDATA:         /*! Unexpected DCN while waiting for image data */
285         case T30_ERR_RX_DCNFAX:          /*! Unexpected DCN while waiting for EOM, EOP or MPS */
286         case T30_ERR_RX_DCNPHD:          /*! Unexpected DCN after EOM or MPS sequence */
287         case T30_ERR_RX_DCNRRD:          /*! Unexpected DCN after RR/RNR sequence */
288         case T30_ERR_RX_DCNNORTN:        /*! Unexpected DCN after requested retransmission */
289                 ast_atomic_fetchadd_int(&p->stats->rx_protocol_error, 1);
290                 break;
291
292         /* TIFF file problems */
293         case T30_ERR_FILEERROR:          /*! TIFF/F file cannot be opened */
294         case T30_ERR_NOPAGE:             /*! TIFF/F page not found */
295         case T30_ERR_BADTIFF:            /*! TIFF/F format is not compatible */
296         case T30_ERR_BADPAGE:            /*! TIFF/F page number tag missing */
297         case T30_ERR_BADTAG:             /*! Incorrect values for TIFF/F tags */
298         case T30_ERR_BADTIFFHDR:         /*! Bad TIFF/F header - incorrect values in fields */
299                 ast_atomic_fetchadd_int(&p->stats->file_error, 1);
300                 break;
301         case T30_ERR_NOMEM:              /*! Cannot allocate memory for more pages */
302                 ast_atomic_fetchadd_int(&p->stats->mem_error, 1);
303                 break;
304
305         /* General problems */
306         case T30_ERR_RETRYDCN:           /*! Disconnected after permitted retries */
307                 ast_atomic_fetchadd_int(&p->stats->retries_exceeded, 1);
308                 break;
309         case T30_ERR_CALLDROPPED:        /*! The call dropped prematurely */
310                 ast_atomic_fetchadd_int(&p->stats->call_dropped, 1);
311                 break;
312
313         /* Feature negotiation issues */
314         case T30_ERR_NOPOLL:             /*! Poll not accepted */
315         case T30_ERR_IDENT_UNACCEPTABLE: /*! Far end's ident is not acceptable */
316         case T30_ERR_SUB_UNACCEPTABLE:   /*! Far end's sub-address is not acceptable */
317         case T30_ERR_SEP_UNACCEPTABLE:   /*! Far end's selective polling address is not acceptable */
318         case T30_ERR_PSA_UNACCEPTABLE:   /*! Far end's polled sub-address is not acceptable */
319         case T30_ERR_SID_UNACCEPTABLE:   /*! Far end's sender identification is not acceptable */
320         case T30_ERR_PWD_UNACCEPTABLE:   /*! Far end's password is not acceptable */
321         case T30_ERR_TSA_UNACCEPTABLE:   /*! Far end's transmitting subscriber internet address is not acceptable */
322         case T30_ERR_IRA_UNACCEPTABLE:   /*! Far end's internet routing address is not acceptable */
323         case T30_ERR_CIA_UNACCEPTABLE:   /*! Far end's calling subscriber internet address is not acceptable */
324         case T30_ERR_ISP_UNACCEPTABLE:   /*! Far end's internet selective polling address is not acceptable */
325         case T30_ERR_CSA_UNACCEPTABLE:   /*! Far end's called subscriber internet address is not acceptable */
326                 ast_atomic_fetchadd_int(&p->stats->neg_failed, 1);
327                 break;
328         default:
329                 ast_atomic_fetchadd_int(&p->stats->unknown_error, 1);
330                 ast_log(LOG_WARNING, "unknown FAX session result '%d' (%s)\n", completion_code, t30_completion_code_to_str(completion_code));
331                 return -1;
332         }
333         return 0;
334 }
335
336 /*! \brief Phase E handler callback.
337  * \param t30_state the span t30 state
338  * \param data this will be the ast_fax_session
339  * \param completion_code the result of the fax session
340  *
341  * This function pulls stats from the spandsp stack and stores them for res_fax
342  * to use later.
343  */
344 static void t30_phase_e_handler(t30_state_t *t30_state, void *data, int completion_code)
345 {
346         struct ast_fax_session *s = data;
347         struct spandsp_pvt *p = s->tech_pvt;
348         char headerinfo[T30_MAX_PAGE_HEADER_INFO + 1];
349         const char *c;
350         t30_stats_t stats;
351
352         ast_debug(5, "FAX session '%d' entering phase E\n", s->id);
353
354         p->isdone = 1;
355
356         update_stats(p, completion_code);
357
358         t30_get_transfer_statistics(t30_state, &stats);
359
360         if (completion_code == T30_ERR_OK) {
361                 ast_string_field_set(s->details, result, "SUCCESS");
362         } else {
363                 ast_string_field_set(s->details, result, "FAILED");
364                 ast_string_field_set(s->details, error, t30_completion_code_to_str(completion_code));
365         }
366
367         ast_string_field_set(s->details, resultstr, t30_completion_code_to_str(completion_code));
368
369         ast_debug(5, "FAX session '%d' completed with result: %s (%s)\n", s->id, s->details->result, s->details->resultstr);
370
371         if ((c = t30_get_tx_ident(t30_state))) {
372                 ast_string_field_set(s->details, localstationid, c);
373         }
374
375         if ((c = t30_get_rx_ident(t30_state))) {
376                 ast_string_field_set(s->details, remotestationid, c);
377         }
378
379 #if SPANDSP_RELEASE_DATE >= 20090220
380         s->details->pages_transferred = (s->details->caps & AST_FAX_TECH_RECEIVE) ? stats.pages_rx : stats.pages_tx;
381 #else
382         s->details->pages_transferred = stats.pages_transferred;
383 #endif
384
385         ast_string_field_build(s->details, transfer_rate, "%d", stats.bit_rate);
386
387         ast_string_field_build(s->details, resolution, "%dx%d", stats.x_resolution, stats.y_resolution);
388
389         t30_get_tx_page_header_info(t30_state, headerinfo);
390         ast_string_field_set(s->details, headerinfo, headerinfo);
391 }
392
393 /*! \brief Send spandsp log messages to asterisk.
394  * \param level the spandsp logging level
395  * \param msg the log message
396  *
397  * \note This function is a callback function called by spandsp.
398  */
399 static void spandsp_log(int level, const char *msg)
400 {
401         if (level == SPAN_LOG_ERROR) {
402                 ast_log(LOG_ERROR, "%s", msg);
403         } else if (level == SPAN_LOG_WARNING) {
404                 ast_log(LOG_WARNING, "%s", msg);
405         } else {
406                 ast_fax_log(LOG_DEBUG, msg);
407         }
408 }
409
410 static void set_logging(logging_state_t *state, struct ast_fax_session_details *details)
411 {
412         int level = SPAN_LOG_WARNING;
413
414         if (details->option.debug) {
415                 level = SPAN_LOG_DEBUG_3;
416         }
417
418         span_log_set_message_handler(state, spandsp_log);
419         span_log_set_level(state, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | level);
420 }
421
422 static void set_local_info(t30_state_t *t30_state, struct ast_fax_session_details *details)
423 {
424         if (!ast_strlen_zero(details->localstationid)) {
425                 t30_set_tx_ident(t30_state, details->localstationid);
426         }
427
428         if (!ast_strlen_zero(details->headerinfo)) {
429                 t30_set_tx_page_header_info(t30_state, details->headerinfo);
430         }
431 }
432
433 static void set_file(t30_state_t *t30_state, struct ast_fax_session_details *details)
434 {
435         if (details->caps & AST_FAX_TECH_RECEIVE) {
436                 t30_set_rx_file(t30_state, AST_LIST_FIRST(&details->documents)->filename, -1);
437         } else {
438                 /* if not AST_FAX_TECH_RECEIVE, assume AST_FAX_TECH_SEND, this
439                  * should be safe because we ensure either RECEIVE or SEND is
440                  * indicated in spandsp_fax_new() */
441                 t30_set_tx_file(t30_state, AST_LIST_FIRST(&details->documents)->filename, -1, -1);
442         }
443 }
444
445 static void set_ecm(t30_state_t *t30_state, struct ast_fax_session_details *details)
446 {
447         t30_set_ecm_capability(t30_state, details->option.ecm);
448         t30_set_supported_compressions(t30_state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
449 }
450
451 /*! \brief create an instance of the spandsp tech_pvt for a fax session */
452 static void *spandsp_fax_new(struct ast_fax_session *s, struct ast_fax_tech_token *token)
453 {
454         struct spandsp_pvt *p;
455         int caller_mode;
456
457         if ((!(p = ast_calloc(1, sizeof(*p))))) {
458                 ast_log(LOG_ERROR, "Cannot initialize the spandsp private FAX technology structure.\n");
459                 goto e_return;
460         }
461
462         if (s->details->caps & AST_FAX_TECH_GATEWAY) {
463                 s->state = AST_FAX_STATE_INITIALIZED;
464                 return p;
465         }
466
467         AST_LIST_HEAD_INIT(&p->read_frames);
468
469         if (s->details->caps & AST_FAX_TECH_RECEIVE) {
470                 caller_mode = 0;
471         } else if (s->details->caps & AST_FAX_TECH_SEND) {
472                 caller_mode = 1;
473         } else {
474                 ast_log(LOG_ERROR, "Are we sending or receiving? The FAX requirements (capabilities: 0x%X) were not properly set.\n", s->details->caps);
475                 goto e_free;
476         }
477
478         if (!(p->timer = ast_timer_open())) {
479                 ast_log(LOG_ERROR, "Channel '%s' FAX session '%d' failed to create timing source.\n", s->channame, s->id);
480                 goto e_free;
481         }
482
483         s->fd = ast_timer_fd(p->timer);
484
485         p->stats = &spandsp_global_stats.g711;
486
487         if (s->details->caps & AST_FAX_TECH_T38) {
488                 if ((s->details->caps & AST_FAX_TECH_AUDIO) == 0) {
489                         /* audio mode was not requested, start in T.38 mode */
490                         p->ist38 = 1;
491                         p->stats = &spandsp_global_stats.t38;
492                 }
493
494                 /* init t38 stuff */
495                 t38_terminal_init(&p->t38_state, caller_mode, t38_tx_packet_handler, s);
496                 set_logging(&p->t38_state.logging, s->details);
497         }
498
499         if (s->details->caps & AST_FAX_TECH_AUDIO) {
500                 /* init audio stuff */
501                 fax_init(&p->fax_state, caller_mode);
502                 set_logging(&p->fax_state.logging, s->details);
503         }
504
505         s->state = AST_FAX_STATE_INITIALIZED;
506         return p;
507
508 e_free:
509         ast_free(p);
510 e_return:
511         return NULL;
512 }
513
514 /*! \brief Destroy a spandsp fax session.
515  */
516 static void spandsp_fax_destroy(struct ast_fax_session *s)
517 {
518         struct spandsp_pvt *p = s->tech_pvt;
519
520         if (s->details->caps & AST_FAX_TECH_GATEWAY) {
521                 spandsp_fax_gateway_cleanup(s);
522         } else {
523                 session_destroy(p);
524         }
525
526         ast_free(p);
527         s->tech_pvt = NULL;
528         s->fd = -1;
529 }
530
531 /*! \brief Read a frame from the spandsp fax stack.
532  */
533 static struct ast_frame *spandsp_fax_read(struct ast_fax_session *s)
534 {
535         struct spandsp_pvt *p = s->tech_pvt;
536         uint8_t buffer[AST_FRIENDLY_OFFSET + SPANDSP_FAX_SAMPLES * sizeof(uint16_t)];
537         int16_t *buf = (int16_t *) (buffer + AST_FRIENDLY_OFFSET);
538         int samples;
539
540         struct ast_frame fax_frame = {
541                 .frametype = AST_FRAME_VOICE,
542                 .src = "res_fax_spandsp_g711",
543         };
544         struct ast_frame *f = &fax_frame;
545         ast_format_set(&fax_frame.subclass.format, AST_FORMAT_SLINEAR, 0);
546
547         ast_timer_ack(p->timer, 1);
548
549         /* XXX do we need to lock here? */
550         if (p->isdone) {
551                 s->state = AST_FAX_STATE_COMPLETE;
552                 ast_debug(5, "FAX session '%d' is complete.\n", s->id);
553                 return NULL;
554         }
555
556         if (p->ist38) {
557                 t38_terminal_send_timeout(&p->t38_state, SPANDSP_FAX_SAMPLES);
558                 if ((f = AST_LIST_REMOVE_HEAD(&p->read_frames, frame_list))) {
559                         return f;
560                 }
561         } else {
562                 if ((samples = fax_tx(&p->fax_state, buf, SPANDSP_FAX_SAMPLES)) > 0) {
563                         f->samples = samples;
564                         AST_FRAME_SET_BUFFER(f, buffer, AST_FRIENDLY_OFFSET, samples * sizeof(int16_t));
565                         return ast_frisolate(f);
566                 }
567         }
568
569         return &ast_null_frame;
570 }
571
572 /*! \brief Write a frame to the spandsp fax stack.
573  * \param s a fax session
574  * \param f the frame to write
575  *
576  * \note res_fax does not currently use the return value of this function.
577  * Also the fax_rx() function never fails.
578  *
579  * \retval 0 success
580  * \retval -1 failure
581  */
582 static int spandsp_fax_write(struct ast_fax_session *s, const struct ast_frame *f)
583 {
584         struct spandsp_pvt *p = s->tech_pvt;
585
586         if (s->details->caps & AST_FAX_TECH_GATEWAY) {
587                 return spandsp_fax_gateway_process(s, f);
588         }
589
590         /* XXX do we need to lock here? */
591         if (s->state == AST_FAX_STATE_COMPLETE) {
592                 ast_log(LOG_WARNING, "FAX session '%d' is in the '%s' state.\n", s->id, ast_fax_state_to_str(s->state));
593                 return -1;
594         }
595
596         if (p->ist38) {
597                 return t38_core_rx_ifp_packet(p->t38_core_state, f->data.ptr, f->datalen, f->seqno);
598         } else {
599                 return fax_rx(&p->fax_state, f->data.ptr, f->samples);
600         }
601 }
602
603 /*! \brief generate T.30 packets sent to the T.30 leg of gateway
604  * \param chan T.30 channel
605  * \param data fax session structure
606  * \param len not used
607  * \param samples no of samples generated
608  * \return -1 on failure or 0 on sucess*/
609 static int spandsp_fax_gw_t30_gen(struct ast_channel *chan, void *data, int len, int samples)
610 {
611         int res = -1;
612         struct ast_fax_session *s = data;
613         struct spandsp_pvt *p = s->tech_pvt;
614         uint8_t buffer[AST_FRIENDLY_OFFSET + samples * sizeof(uint16_t)];
615         struct ast_frame *f;
616         struct ast_frame t30_frame = {
617                 .frametype = AST_FRAME_VOICE,
618                 .src = "res_fax_spandsp_g711",
619                 .samples = samples,
620                 .flags = AST_FAX_FRFLAG_GATEWAY,
621         };
622
623         AST_FRAME_SET_BUFFER(&t30_frame, buffer, AST_FRIENDLY_OFFSET, t30_frame.samples * sizeof(int16_t));
624
625         ast_format_set(&t30_frame.subclass.format, AST_FORMAT_SLINEAR, 0);
626         if (!(f = ast_frisolate(&t30_frame))) {
627                 return p->isdone ? -1 : res;
628         }
629
630         /* generate a T.30 packet */
631         if ((f->samples = t38_gateway_tx(&p->t38_gw_state, f->data.ptr, f->samples))) {
632                 f->datalen = f->samples * sizeof(int16_t);
633                 res = ast_write(chan, f);
634         }
635         ast_frfree(f);
636         return p->isdone ? -1 : res;
637 }
638
639 /*! \brief simple routine to allocate data to generator
640  * \param chan channel
641  * \param params generator data
642  * \return data to use in generator call*/
643 static void *spandsp_fax_gw_gen_alloc(struct ast_channel *chan, void *params) {
644         ao2_ref(params, +1);
645         return params;
646 }
647
648 static void spandsp_fax_gw_gen_release(struct ast_channel *chan, void *data) {
649         ao2_ref(data, -1);
650 }
651
652 /*! \brief activate a spandsp gateway based on the information in the given fax session
653  * \param s fax session
654  * \return -1 on error 0 on sucess*/
655 static int spandsp_fax_gateway_start(struct ast_fax_session *s) {
656         struct spandsp_pvt *p = s->tech_pvt;
657         struct ast_fax_t38_parameters *t38_param;
658         int i, modems = 0;
659         struct ast_channel *peer;
660         static struct ast_generator t30_gen = {
661                 alloc: spandsp_fax_gw_gen_alloc,
662                 release: spandsp_fax_gw_gen_release,
663                 generate: spandsp_fax_gw_t30_gen,
664         };
665
666 #if SPANDSP_RELEASE_DATE >= 20081012
667         /* for spandsp shaphots 0.0.6 and higher */
668         p->t38_core_state=&p->t38_gw_state.t38x.t38;
669 #else
670         /* for spandsp release 0.0.5 */
671         p->t38_core_state=&p->t38_gw_state.t38;
672 #endif
673
674         if (!t38_gateway_init(&p->t38_gw_state, t38_tx_packet_handler, s)) {
675                 return -1;
676         }
677
678         p->ist38 = 1;
679         p->ast_t38_state = ast_channel_get_t38_state(s->chan);
680         if (!(peer = ast_bridged_channel(s->chan))) {
681                 ast_channel_unlock(s->chan);
682                 return -1;
683         }
684         ast_activate_generator(p->ast_t38_state == T38_STATE_NEGOTIATED ? peer : s->chan, &t30_gen , s);
685
686         set_logging(&p->t38_gw_state.logging, s->details);
687         set_logging(&p->t38_core_state->logging, s->details);
688
689         t38_param = (p->ast_t38_state == T38_STATE_NEGOTIATED) ? &s->details->our_t38_parameters : &s->details->their_t38_parameters;
690         t38_set_t38_version(p->t38_core_state, t38_param->version);
691         t38_gateway_set_ecm_capability(&p->t38_gw_state, s->details->option.ecm);
692         t38_set_max_datagram_size(p->t38_core_state, t38_param->max_ifp);
693         t38_set_fill_bit_removal(p->t38_core_state, t38_param->fill_bit_removal);
694         t38_set_mmr_transcoding(p->t38_core_state, t38_param->transcoding_mmr);
695         t38_set_jbig_transcoding(p->t38_core_state, t38_param->transcoding_jbig);
696         t38_set_data_rate_management_method(p->t38_core_state, 
697                         (t38_param->rate_management == AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF)? 1 : 2);
698
699         t38_gateway_set_transmit_on_idle(&p->t38_gw_state, TRUE);
700         t38_set_sequence_number_handling(p->t38_core_state, TRUE);
701
702         if (AST_FAX_MODEM_V17 & s->details->modems) {
703                 modems |= T30_SUPPORT_V17;
704         }
705         if (AST_FAX_MODEM_V27 & s->details->modems) {
706                 modems |= T30_SUPPORT_V27TER;
707         }
708         if (AST_FAX_MODEM_V29 & s->details->modems) {
709                 modems |= T30_SUPPORT_V29;
710         }
711         if (AST_FAX_MODEM_V34 & s->details->modems) {
712 #if defined(T30_SUPPORT_V34)
713                 modems |= T30_SUPPORT_V34;
714 #elif defined(T30_SUPPORT_V34HDX)
715                 modems |= T30_SUPPORT_V34HDX;
716 #else
717                 ast_log(LOG_WARNING, "v34 not supported in this version of spandsp\n");
718 #endif
719         }
720
721         t38_gateway_set_supported_modems(&p->t38_gw_state, modems);
722
723         /* engage udptl nat on other side of T38 line 
724          * (Asterisk changes media ports thus we send a few packets to reinitialize
725          * pinholes in NATs and FWs
726          */
727         for (i=0; i < SPANDSP_ENGAGE_UDPTL_NAT_RETRY; i++) {
728 #if SPANDSP_RELEASE_DATE >= 20091228
729                 t38_core_send_indicator(&p->t38_gw_state.t38x.t38, T38_IND_NO_SIGNAL);
730 #elif SPANDSP_RELEASE_DATE >= 20081012
731                 t38_core_send_indicator(&p->t38_gw_state.t38x.t38, T38_IND_NO_SIGNAL, p->t38_gw_state.t38x.t38.indicator_tx_count);
732 #else
733                 t38_core_send_indicator(&p->t38_gw_state.t38, T38_IND_NO_SIGNAL, p->t38_gw_state.t38.indicator_tx_count);
734 #endif
735         }
736
737         s->state = AST_FAX_STATE_ACTIVE;
738
739         return 0;
740 }
741
742 /*! \brief process a frame from the bridge
743  * \param s fax session
744  * \param f frame to process
745  * \return 1 on sucess 0 on incorect packet*/
746 static int spandsp_fax_gateway_process(struct ast_fax_session *s, const struct ast_frame *f)
747 {
748         struct spandsp_pvt *p = s->tech_pvt;
749
750         /*invalid frame*/
751         if (!f->data.ptr || !f->datalen) {
752                 return -1;
753         }
754
755         /* Process a IFP packet */
756         if ((f->frametype == AST_FRAME_MODEM) && (f->subclass.integer == AST_MODEM_T38)) {
757                 return t38_core_rx_ifp_packet(p->t38_core_state, f->data.ptr, f->datalen, f->seqno);
758         } else if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.format.id == AST_FORMAT_SLINEAR)) {
759                 return t38_gateway_rx(&p->t38_gw_state, f->data.ptr, f->samples);
760         }
761
762         return -1;
763 }
764
765 /*! \brief gather data and clean up after gateway ends
766  * \param s fax session*/
767 static void spandsp_fax_gateway_cleanup(struct ast_fax_session *s)
768 {
769         struct spandsp_pvt *p = s->tech_pvt;
770         t38_stats_t t38_stats;
771
772         t38_gateway_get_transfer_statistics(&p->t38_gw_state, &t38_stats);
773
774         s->details->option.ecm = t38_stats.error_correcting_mode ? AST_FAX_OPTFLAG_TRUE : AST_FAX_OPTFLAG_FALSE;
775         s->details->pages_transferred = t38_stats.pages_transferred;
776         ast_string_field_build(s->details, transfer_rate, "%d", t38_stats.bit_rate);
777 }
778
779 /*! \brief */
780 static int spandsp_fax_start(struct ast_fax_session *s)
781 {
782         struct spandsp_pvt *p = s->tech_pvt;
783
784         s->state = AST_FAX_STATE_OPEN;
785
786         if (s->details->caps & AST_FAX_TECH_GATEWAY) {
787                 return spandsp_fax_gateway_start(s);
788         }
789
790         if (p->ist38) {
791 #if SPANDSP_RELEASE_DATE >= 20080725
792                 /* for spandsp shaphots 0.0.6 and higher */
793                 p->t30_state = &p->t38_state.t30;
794                 p->t38_core_state = &p->t38_state.t38_fe.t38;
795 #else
796                 /* for spandsp releases 0.0.5 */
797                 p->t30_state = &p->t38_state.t30_state;
798                 p->t38_core_state = &p->t38_state.t38;
799 #endif
800         } else {
801 #if SPANDSP_RELEASE_DATE >= 20080725
802                 /* for spandsp shaphots 0.0.6 and higher */
803                 p->t30_state = &p->fax_state.t30;
804 #else
805                 /* for spandsp release 0.0.5 */
806                 p->t30_state = &p->fax_state.t30_state;
807 #endif
808         }
809
810         set_logging(&p->t30_state->logging, s->details);
811
812         /* set some parameters */
813         set_local_info(p->t30_state, s->details);
814         set_file(p->t30_state, s->details);
815         set_ecm(p->t30_state, s->details);
816
817         /* perhaps set_transmit_on_idle() should be called */
818
819         t30_set_phase_e_handler(p->t30_state, t30_phase_e_handler, s);
820
821         /* set T.38 parameters */
822         if (p->ist38) {
823                 set_logging(&p->t38_core_state->logging, s->details);
824
825                 t38_set_max_datagram_size(p->t38_core_state, s->details->their_t38_parameters.max_ifp);
826
827                 if (s->details->their_t38_parameters.fill_bit_removal) {
828                         t38_set_fill_bit_removal(p->t38_core_state, TRUE);
829                 }
830
831                 if (s->details->their_t38_parameters.transcoding_mmr) {
832                         t38_set_mmr_transcoding(p->t38_core_state, TRUE);
833                 }
834
835                 if (s->details->their_t38_parameters.transcoding_jbig) {
836                         t38_set_jbig_transcoding(p->t38_core_state, TRUE);
837                 }
838         } else {
839                 /* have the fax stack generate silence if it has no data to send */
840                 fax_set_transmit_on_idle(&p->fax_state, 1);
841         }
842
843
844         /* start the timer */
845         if (ast_timer_set_rate(p->timer, SPANDSP_FAX_TIMER_RATE)) {
846                 ast_log(LOG_ERROR, "FAX session '%d' error setting rate on timing source.\n", s->id);
847                 return -1;
848         }
849
850         s->state = AST_FAX_STATE_ACTIVE;
851
852         return 0;
853 }
854
855 /*! \brief */
856 static int spandsp_fax_cancel(struct ast_fax_session *s)
857 {
858         struct spandsp_pvt *p = s->tech_pvt;
859
860         if (s->details->caps & AST_FAX_TECH_GATEWAY) {
861                 p->isdone = 1;
862                 return 0;
863         }
864
865         t30_terminate(p->t30_state);
866         p->isdone = 1;
867         return 0;
868 }
869
870 /*! \brief */
871 static int spandsp_fax_switch_to_t38(struct ast_fax_session *s)
872 {
873         struct spandsp_pvt *p = s->tech_pvt;
874
875         /* prevent the phase E handler from running, this is not a real termination */
876         t30_set_phase_e_handler(p->t30_state, NULL, NULL);
877
878         t30_terminate(p->t30_state);
879
880         s->details->option.switch_to_t38 = 1;
881         ast_atomic_fetchadd_int(&p->stats->switched, 1);
882
883         p->ist38 = 1;
884         p->stats = &spandsp_global_stats.t38;
885         spandsp_fax_start(s);
886
887         return 0;
888 }
889
890 /*! \brief */
891 static char *spandsp_fax_cli_show_capabilities(int fd)
892 {
893         ast_cli(fd, "SEND RECEIVE T.38 G.711 GATEWAY\n\n");
894         return  CLI_SUCCESS;
895 }
896
897 /*! \brief */
898 static char *spandsp_fax_cli_show_session(struct ast_fax_session *s, int fd)
899 {
900         struct spandsp_pvt *p = s->tech_pvt;
901
902         ao2_lock(s);
903         if (s->details->caps & AST_FAX_TECH_GATEWAY) {
904                 ast_cli(fd, "%-22s : %d\n", "session", s->id);
905                 ast_cli(fd, "%-22s : %s\n", "operation", "Gateway");
906                 ast_cli(fd, "%-22s : %s\n", "state", ast_fax_state_to_str(s->state));
907                 if (s->state != AST_FAX_STATE_UNINITIALIZED) {
908                         t38_stats_t stats;
909                         t38_gateway_get_transfer_statistics(&p->t38_gw_state, &stats);
910                         ast_cli(fd, "%-22s : %s\n", "ECM Mode", stats.error_correcting_mode ? "Yes" : "No");
911                         ast_cli(fd, "%-22s : %d\n", "Data Rate", stats.bit_rate);
912                         ast_cli(fd, "%-22s : %d\n", "Page Number", stats.pages_transferred + 1);
913                 }
914         } else {
915                 ast_cli(fd, "%-22s : %d\n", "session", s->id);
916                 ast_cli(fd, "%-22s : %s\n", "operation", (s->details->caps & AST_FAX_TECH_RECEIVE) ? "Receive" : "Transmit");
917                 ast_cli(fd, "%-22s : %s\n", "state", ast_fax_state_to_str(s->state));
918                 if (s->state != AST_FAX_STATE_UNINITIALIZED) {
919                         t30_stats_t stats;
920                         t30_get_transfer_statistics(p->t30_state, &stats);
921                         ast_cli(fd, "%-22s : %s\n", "Last Status", t30_completion_code_to_str(stats.current_status));
922                         ast_cli(fd, "%-22s : %s\n", "ECM Mode", stats.error_correcting_mode ? "Yes" : "No");
923                         ast_cli(fd, "%-22s : %d\n", "Data Rate", stats.bit_rate);
924                         ast_cli(fd, "%-22s : %dx%d\n", "Image Resolution", stats.x_resolution, stats.y_resolution);
925 #if SPANDSP_RELEASE_DATE >= 20090220
926                         ast_cli(fd, "%-22s : %d\n", "Page Number", ((s->details->caps & AST_FAX_TECH_RECEIVE) ? stats.pages_rx : stats.pages_tx) + 1);
927 #else
928                         ast_cli(fd, "%-22s : %d\n", "Page Number", stats.pages_transferred + 1);
929 #endif
930                         ast_cli(fd, "%-22s : %s\n", "File Name", s->details->caps & AST_FAX_TECH_RECEIVE ? p->t30_state->rx_file : p->t30_state->tx_file);
931
932                         ast_cli(fd, "\nData Statistics:\n");
933 #if SPANDSP_RELEASE_DATE >= 20090220
934                         ast_cli(fd, "%-22s : %d\n", "Tx Pages", stats.pages_tx);
935                         ast_cli(fd, "%-22s : %d\n", "Rx Pages", stats.pages_rx);
936 #else
937                         ast_cli(fd, "%-22s : %d\n", "Tx Pages", (s->details->caps & AST_FAX_TECH_SEND) ? stats.pages_transferred : 0);
938                         ast_cli(fd, "%-22s : %d\n", "Rx Pages", (s->details->caps & AST_FAX_TECH_RECEIVE) ? stats.pages_transferred : 0);
939 #endif
940                         ast_cli(fd, "%-22s : %d\n", "Longest Bad Line Run", stats.longest_bad_row_run);
941                         ast_cli(fd, "%-22s : %d\n", "Total Bad Lines", stats.bad_rows);
942                 }
943         }
944         ao2_unlock(s);
945         ast_cli(fd, "\n\n");
946         return CLI_SUCCESS;
947 }
948
949 /*! \brief */
950 static char *spandsp_fax_cli_show_stats(int fd)
951 {
952         ast_mutex_lock(&spandsp_global_stats.lock);
953         ast_cli(fd, "\n%-20.20s\n", "Spandsp G.711");
954         ast_cli(fd, "%-20.20s : %d\n", "Success", spandsp_global_stats.g711.success);
955         ast_cli(fd, "%-20.20s : %d\n", "Switched to T.38", spandsp_global_stats.g711.switched);
956         ast_cli(fd, "%-20.20s : %d\n", "Call Dropped", spandsp_global_stats.g711.call_dropped);
957         ast_cli(fd, "%-20.20s : %d\n", "No FAX", spandsp_global_stats.g711.nofax);
958         ast_cli(fd, "%-20.20s : %d\n", "Negotiation Failed", spandsp_global_stats.g711.neg_failed);
959         ast_cli(fd, "%-20.20s : %d\n", "Train Failure", spandsp_global_stats.g711.failed_to_train);
960         ast_cli(fd, "%-20.20s : %d\n", "Retries Exceeded", spandsp_global_stats.g711.retries_exceeded);
961         ast_cli(fd, "%-20.20s : %d\n", "Protocol Error", spandsp_global_stats.g711.protocol_error);
962         ast_cli(fd, "%-20.20s : %d\n", "TX Protocol Error", spandsp_global_stats.g711.tx_protocol_error);
963         ast_cli(fd, "%-20.20s : %d\n", "RX Protocol Error", spandsp_global_stats.g711.rx_protocol_error);
964         ast_cli(fd, "%-20.20s : %d\n", "File Error", spandsp_global_stats.g711.file_error);
965         ast_cli(fd, "%-20.20s : %d\n", "Memory Error", spandsp_global_stats.g711.mem_error);
966         ast_cli(fd, "%-20.20s : %d\n", "Unknown Error", spandsp_global_stats.g711.unknown_error);
967
968         ast_cli(fd, "\n%-20.20s\n", "Spandsp T.38");
969         ast_cli(fd, "%-20.20s : %d\n", "Success", spandsp_global_stats.t38.success);
970         ast_cli(fd, "%-20.20s : %d\n", "Call Dropped", spandsp_global_stats.t38.call_dropped);
971         ast_cli(fd, "%-20.20s : %d\n", "No FAX", spandsp_global_stats.t38.nofax);
972         ast_cli(fd, "%-20.20s : %d\n", "Negotiation Failed", spandsp_global_stats.t38.neg_failed);
973         ast_cli(fd, "%-20.20s : %d\n", "Train Failure", spandsp_global_stats.t38.failed_to_train);
974         ast_cli(fd, "%-20.20s : %d\n", "Retries Exceeded", spandsp_global_stats.t38.retries_exceeded);
975         ast_cli(fd, "%-20.20s : %d\n", "Protocol Error", spandsp_global_stats.t38.protocol_error);
976         ast_cli(fd, "%-20.20s : %d\n", "TX Protocol Error", spandsp_global_stats.t38.tx_protocol_error);
977         ast_cli(fd, "%-20.20s : %d\n", "RX Protocol Error", spandsp_global_stats.t38.rx_protocol_error);
978         ast_cli(fd, "%-20.20s : %d\n", "File Error", spandsp_global_stats.t38.file_error);
979         ast_cli(fd, "%-20.20s : %d\n", "Memory Error", spandsp_global_stats.t38.mem_error);
980         ast_cli(fd, "%-20.20s : %d\n", "Unknown Error", spandsp_global_stats.t38.unknown_error);
981         ast_mutex_unlock(&spandsp_global_stats.lock);
982
983         return CLI_SUCCESS;
984 }
985
986 /*! \brief Show res_fax_spandsp settings */
987 static char *spandsp_fax_cli_show_settings(int fd)
988 {
989         /* no settings at the moment */
990         return CLI_SUCCESS;
991 }
992
993 /*! \brief unload res_fax_spandsp */
994 static int unload_module(void)
995 {
996         ast_fax_tech_unregister(&spandsp_fax_tech);
997         ast_mutex_destroy(&spandsp_global_stats.lock);
998         return AST_MODULE_LOAD_SUCCESS;
999 }
1000
1001 /*! \brief load res_fax_spandsp */
1002 static int load_module(void)
1003 {
1004         ast_mutex_init(&spandsp_global_stats.lock);
1005         spandsp_fax_tech.module = ast_module_info->self;
1006         if (ast_fax_tech_register(&spandsp_fax_tech) < 0) {
1007                 ast_log(LOG_ERROR, "failed to register FAX technology\n");
1008                 return AST_MODULE_LOAD_DECLINE;
1009         }
1010
1011         /* prevent logging to stderr */
1012         span_set_message_handler(NULL);
1013
1014         return AST_MODULE_LOAD_SUCCESS;
1015 }
1016
1017
1018 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Spandsp G.711 and T.38 FAX Technologies",
1019                 .load = load_module,
1020                 .unload = unload_module,
1021                );