00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <asterisk.h>
00019
00020 #include <string.h>
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <inttypes.h>
00024 #include <pthread.h>
00025 #include <errno.h>
00026 #include <tiffio.h>
00027
00028 #include <spandsp.h>
00029
00030 #include "asterisk/lock.h"
00031 #include "asterisk/file.h"
00032 #include "asterisk/logger.h"
00033 #include "asterisk/channel.h"
00034 #include "asterisk/pbx.h"
00035 #include "asterisk/module.h"
00036 #include "asterisk/translate.h"
00037
00038 static char *app = "TxFAX";
00039
00040 static char *synopsis = "Send a FAX file";
00041
00042 static char *descrip =
00043 " TxFAX(filename[|caller][|debug]): Send a given TIFF file to the channel as a FAX.\n"
00044 "The \"caller\" option makes the application behave as a calling machine,\n"
00045 "rather than the answering machine. The default behaviour is to behave as\n"
00046 "an answering machine.\n"
00047 "Uses LOCALSTATIONID to identify itself to the remote end.\n"
00048 " LOCALHEADERINFO to generate a header line on each page.\n"
00049 "Sets REMOTESTATIONID to the receiver CSID.\n" "Returns -1 when the user hangs up, or if the file does not exist.\n" "Returns 0 otherwise.\n";
00050
00051 #define MAX_BLOCK_SIZE 240
00052
00053 static void span_message(int level, const char *msg)
00054 {
00055 int ast_level;
00056
00057 if (level == SPAN_LOG_WARNING)
00058 ast_level = __LOG_WARNING;
00059 else if (level == SPAN_LOG_WARNING)
00060 ast_level = __LOG_WARNING;
00061 else
00062 ast_level = __LOG_DEBUG;
00063 ast_log(ast_level, __FILE__, __LINE__, __PRETTY_FUNCTION__, msg);
00064 }
00065
00066
00067
00068 #if 0
00069 static void t30_flush(t30_state_t * s, int which)
00070 {
00071
00072 }
00073
00074
00075 #endif
00076
00077 static void phase_e_handler(t30_state_t * s, void *user_data, int result)
00078 {
00079 struct ast_channel *chan;
00080 char far_ident[21];
00081
00082 chan = (struct ast_channel *) user_data;
00083 if (result == T30_ERR_OK) {
00084 t30_get_far_ident(s, far_ident);
00085 pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", far_ident);
00086 } else {
00087 ast_log(LOG_DEBUG, "==============================================================================\n");
00088 ast_log(LOG_DEBUG, "Fax send not successful - result (%d) %s.\n", result, t30_completion_code_to_str(result));
00089 ast_log(LOG_DEBUG, "==============================================================================\n");
00090 }
00091 }
00092
00093
00094
00095 static int txfax_exec(struct ast_channel *chan, void *data)
00096 {
00097 int res = 0;
00098 char source_file[256];
00099 char *s;
00100 char *t;
00101 char *v;
00102 const char *x;
00103 int option;
00104 int len;
00105 fax_state_t fax;
00106 int calling_party;
00107 int verbose;
00108 int samples;
00109
00110 struct ast_module_user *u;
00111 struct ast_frame *inf = NULL;
00112 struct ast_frame outf;
00113
00114 int original_read_fmt;
00115 int original_write_fmt;
00116
00117 uint8_t __buf[sizeof(uint16_t) * MAX_BLOCK_SIZE + 2 * AST_FRIENDLY_OFFSET];
00118 uint8_t *buf = __buf + AST_FRIENDLY_OFFSET;
00119
00120 if (chan == NULL) {
00121 ast_log(LOG_WARNING, "Fax transmit channel is NULL. Giving up.\n");
00122 return -1;
00123 }
00124
00125 span_set_message_handler(span_message);
00126
00127
00128 if (data == NULL) {
00129
00130 ast_log(LOG_WARNING, "Txfax requires an argument (filename)\n");
00131 return -1;
00132 }
00133
00134 calling_party = FALSE;
00135 verbose = FALSE;
00136 source_file[0] = '\0';
00137
00138 for (option = 0, v = s = data; v; option++, s++) {
00139 t = s;
00140 v = strchr(s, '|');
00141 s = (v) ? v : s + strlen(s);
00142 strncpy((char *) buf, t, s - t);
00143 buf[s - t] = '\0';
00144 if (option == 0) {
00145
00146 len = s - t;
00147 if (len > 255)
00148 len = 255;
00149 strncpy(source_file, t, len);
00150 source_file[len] = '\0';
00151 } else if (strncmp("caller", t, s - t) == 0) {
00152 calling_party = TRUE;
00153 } else if (strncmp("debug", t, s - t) == 0) {
00154 verbose = TRUE;
00155 }
00156 }
00157
00158
00159
00160 u = ast_module_user_add(chan);
00161
00162 if (chan->_state != AST_STATE_UP) {
00163
00164
00165 res = ast_answer(chan);
00166 }
00167
00168 if (!res) {
00169 original_read_fmt = chan->readformat;
00170 if (original_read_fmt != AST_FORMAT_SLINEAR) {
00171 res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
00172 if (res < 0) {
00173 ast_log(LOG_WARNING, "Unable to set to linear read mode, giving up\n");
00174 return -1;
00175 }
00176 }
00177 original_write_fmt = chan->writeformat;
00178 if (original_write_fmt != AST_FORMAT_SLINEAR) {
00179 res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
00180 if (res < 0) {
00181 ast_log(LOG_WARNING, "Unable to set to linear write mode, giving up\n");
00182 res = ast_set_read_format(chan, original_read_fmt);
00183 if (res)
00184 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
00185 return -1;
00186 }
00187 }
00188 fax_init(&fax, calling_party);
00189 if (verbose)
00190 fax.logging.level = SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW;
00191
00192 x = pbx_builtin_getvar_helper(chan, "LOCALSTATIONID");
00193 if (x && x[0])
00194 t30_set_local_ident(&fax.t30_state, x);
00195 x = pbx_builtin_getvar_helper(chan, "LOCALHEADERINFO");
00196 if (x && x[0])
00197 t30_set_header_info(&fax.t30_state, x);
00198 t30_set_tx_file(&fax.t30_state, source_file, -1, -1);
00199
00200
00201 t30_set_phase_e_handler(&fax.t30_state, phase_e_handler, chan);
00202 t30_set_ecm_capability(&fax.t30_state, TRUE);
00203 t30_set_supported_compressions(&fax.t30_state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
00204 while (ast_waitfor(chan, -1) > -1) {
00205 inf = ast_read(chan);
00206 if (inf == NULL) {
00207 res = -1;
00208 break;
00209 }
00210 if (inf->frametype == AST_FRAME_VOICE) {
00211 if (fax_rx(&fax, inf->data, inf->samples))
00212 break;
00213 samples = (inf->samples <= MAX_BLOCK_SIZE) ? inf->samples : MAX_BLOCK_SIZE;
00214 len = fax_tx(&fax, (int16_t *) & buf[AST_FRIENDLY_OFFSET], samples);
00215 if (len) {
00216 memset(&outf, 0, sizeof(outf));
00217 outf.frametype = AST_FRAME_VOICE;
00218 outf.subclass = AST_FORMAT_SLINEAR;
00219 outf.datalen = len * sizeof(int16_t);
00220 outf.samples = len;
00221 outf.data = &buf[AST_FRIENDLY_OFFSET];
00222 outf.offset = AST_FRIENDLY_OFFSET;
00223 if (ast_write(chan, &outf) < 0) {
00224 ast_log(LOG_WARNING, "Unable to write frame to channel; %s\n", strerror(errno));
00225 break;
00226 }
00227 }
00228 }
00229 ast_frfree(inf);
00230 }
00231 if (inf == NULL) {
00232 ast_log(LOG_DEBUG, "Got hangup\n");
00233 res = -1;
00234 }
00235 if (original_read_fmt != AST_FORMAT_SLINEAR) {
00236 res = ast_set_read_format(chan, original_read_fmt);
00237 if (res)
00238 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
00239 }
00240 if (original_write_fmt != AST_FORMAT_SLINEAR) {
00241 res = ast_set_write_format(chan, original_write_fmt);
00242 if (res)
00243 ast_log(LOG_WARNING, "Unable to restore write format on '%s'\n", chan->name);
00244 }
00245 t30_terminate(&fax.t30_state);
00246 } else {
00247 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
00248 }
00249 ast_module_user_remove(u);
00250 return res;
00251 }
00252
00253
00254
00255 static int unload_module(void)
00256 {
00257 int res;
00258
00259 ast_module_user_hangup_all();
00260
00261 res = ast_unregister_application(app);
00262
00263
00264 return res;
00265 }
00266
00267
00268
00269 static int load_module(void)
00270 {
00271 return ast_register_application(app, txfax_exec, synopsis, descrip);
00272 }
00273
00274
00275
00276 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Trivial FAX Transmit Application");
00277
00278