2 * Asterisk -- A telephony toolkit for Linux.
4 * Trivial application to send a TIFF file as a FAX
6 * Copyright (C) 2003, Steve Underwood
8 * Steve Underwood <steveu@coppice.org>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
26 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
28 #include "asterisk/lock.h"
29 #include "asterisk/file.h"
30 #include "asterisk/logger.h"
31 #include "asterisk/channel.h"
32 #include "asterisk/pbx.h"
33 #include "asterisk/module.h"
34 #include "asterisk/translate.h"
36 static char *tdesc = "Trivial FAX Transmit Application";
38 static char *app = "TxFAX";
40 static char *synopsis = "Send a FAX file";
42 static char *descrip =
43 " TxFAX(filename[|caller][|debug]): Send a given TIFF file to the channel as a FAX.\n"
44 "The \"caller\" option makes the application behave as a calling machine,\n"
45 "rather than the answering machine. The default behaviour is to behave as\n"
46 "an answering machine.\n"
47 "Uses LOCALSTATIONID to identify itself to the remote end.\n"
48 " LOCALHEADERINFO to generate a header line on each page.\n"
49 "Sets REMOTESTATIONID to the receiver CSID.\n"
50 "Returns -1 when the user hangs up, or if the file does not exist.\n"
51 "Returns 0 otherwise.\n";
57 #define MAX_BLOCK_SIZE 240
59 static void span_message(int level, const char *msg)
63 if (level == SPAN_LOG_WARNING)
64 ast_level = __LOG_WARNING;
65 else if (level == SPAN_LOG_WARNING)
66 ast_level = __LOG_WARNING;
68 ast_level = __LOG_DEBUG;
69 ast_log(ast_level, __FILE__, __LINE__, __PRETTY_FUNCTION__, msg);
71 /*- End of function --------------------------------------------------------*/
73 static void t30_flush(t30_state_t *s, int which)
77 /*- End of function --------------------------------------------------------*/
79 static void phase_e_handler(t30_state_t *s, void *user_data, int result)
81 struct ast_channel *chan;
84 chan = (struct ast_channel *) user_data;
85 if (result == T30_ERR_OK)
87 t30_get_far_ident(s, far_ident);
88 pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", far_ident);
92 ast_log(LOG_DEBUG, "==============================================================================\n");
93 ast_log(LOG_DEBUG, "Fax send not successful - result (%d) %s.\n", result, t30_completion_code_to_str(result));
94 ast_log(LOG_DEBUG, "==============================================================================\n");
97 /*- End of function --------------------------------------------------------*/
99 static int txfax_exec(struct ast_channel *chan, void *data)
102 char source_file[256];
115 struct ast_frame *inf = NULL;
116 struct ast_frame outf;
118 int original_read_fmt;
119 int original_write_fmt;
121 uint8_t __buf[sizeof(uint16_t)*MAX_BLOCK_SIZE + 2*AST_FRIENDLY_OFFSET];
122 uint8_t *buf = __buf + AST_FRIENDLY_OFFSET;
126 ast_log(LOG_WARNING, "Fax transmit channel is NULL. Giving up.\n");
130 span_set_message_handler(span_message);
132 /* The next few lines of code parse out the filename and header from the input string */
135 /* No data implies no filename or anything is present */
136 ast_log(LOG_WARNING, "Txfax requires an argument (filename)\n");
140 calling_party = FALSE;
142 source_file[0] = '\0';
144 for (option = 0, v = s = data; v; option++, s++)
148 s = (v) ? v : s + strlen(s);
149 strncpy((char *) buf, t, s - t);
153 /* The first option is always the file name */
157 strncpy(source_file, t, len);
158 source_file[len] = '\0';
160 else if (strncmp("caller", t, s - t) == 0)
162 calling_party = TRUE;
164 else if (strncmp("debug", t, s - t) == 0)
174 if (chan->_state != AST_STATE_UP)
176 /* Shouldn't need this, but checking to see if channel is already answered
177 * Theoretically asterisk should already have answered before running the app */
178 res = ast_answer(chan);
183 original_read_fmt = chan->readformat;
184 if (original_read_fmt != AST_FORMAT_SLINEAR)
186 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
189 ast_log(LOG_WARNING, "Unable to set to linear read mode, giving up\n");
193 original_write_fmt = chan->writeformat;
194 if (original_write_fmt != AST_FORMAT_SLINEAR)
196 res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
199 ast_log(LOG_WARNING, "Unable to set to linear write mode, giving up\n");
200 res = ast_set_read_format(chan, original_read_fmt);
202 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
206 fax_init(&fax, calling_party, NULL);
208 fax.logging.level = SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW;
210 x = pbx_builtin_getvar_helper(chan, "LOCALSTATIONID");
212 t30_set_local_ident(&fax, x);
213 x = pbx_builtin_getvar_helper(chan, "LOCALHEADERINFO");
215 t30_set_header_info(&fax, x);
216 t30_set_tx_file(&fax, source_file, -1, -1);
217 //t30_set_phase_b_handler(&fax, phase_b_handler, chan);
218 //t30_set_phase_d_handler(&fax, phase_d_handler, chan);
219 t30_set_phase_e_handler(&fax, phase_e_handler, chan);
220 while (ast_waitfor(chan, -1) > -1)
222 inf = ast_read(chan);
228 if (inf->frametype == AST_FRAME_VOICE)
230 if (fax_rx(&fax, inf->data, inf->samples))
232 samples = (inf->samples <= MAX_BLOCK_SIZE) ? inf->samples : MAX_BLOCK_SIZE;
233 len = fax_tx(&fax, (int16_t *) &buf[AST_FRIENDLY_OFFSET], samples);
236 memset(&outf, 0, sizeof(outf));
237 outf.frametype = AST_FRAME_VOICE;
238 outf.subclass = AST_FORMAT_SLINEAR;
239 outf.datalen = len*sizeof(int16_t);
241 outf.data = &buf[AST_FRIENDLY_OFFSET];
242 outf.offset = AST_FRIENDLY_OFFSET;
243 if (ast_write(chan, &outf) < 0)
245 ast_log(LOG_WARNING, "Unable to write frame to channel; %s\n", strerror(errno));
254 ast_log(LOG_DEBUG, "Got hangup\n");
257 if (original_read_fmt != AST_FORMAT_SLINEAR)
259 res = ast_set_read_format(chan, original_read_fmt);
261 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
263 if (original_write_fmt != AST_FORMAT_SLINEAR)
265 res = ast_set_write_format(chan, original_write_fmt);
267 ast_log(LOG_WARNING, "Unable to restore write format on '%s'\n", chan->name);
273 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
275 LOCAL_USER_REMOVE(u);
278 /*- End of function --------------------------------------------------------*/
280 int unload_module(void)
282 STANDARD_HANGUP_LOCALUSERS;
283 return ast_unregister_application(app);
285 /*- End of function --------------------------------------------------------*/
287 int load_module(void)
289 return ast_register_application(app, txfax_exec, synopsis, descrip);
291 /*- End of function --------------------------------------------------------*/
293 char *description(void)
297 /*- End of function --------------------------------------------------------*/
303 STANDARD_USECOUNT(res);
306 /*- End of function --------------------------------------------------------*/
310 return ASTERISK_GPL_KEY;
312 /*- End of function --------------------------------------------------------*/
313 /*- End of file ------------------------------------------------------------*/