Mon May 14 04:42:53 2007

Asterisk developer's documentation


app_rxfax.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- A telephony toolkit for Linux.
00003  *
00004  * Trivial application to receive a TIFF FAX file
00005  * 
00006  * Copyright (C) 2003, Steve Underwood
00007  *
00008  * Steve Underwood <steveu@coppice.org>
00009  *
00010  * This program is free software, distributed under the terms of
00011  * the GNU General Public License
00012  */
00013 
00014 /*** MODULEINFO
00015    <depend>spandsp</depend>
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 #include "asterisk/dsp.h"
00038 #include "asterisk/manager.h"
00039 
00040 static char *app = "RxFAX";
00041 
00042 static char *synopsis = "Receive a FAX to a file";
00043 
00044 static char *descrip =
00045    "  RxFAX(filename[|caller][|debug]): Receives a FAX from the channel into the\n"
00046    "given filename. If the file exists it will be overwritten. The file\n"
00047    "should be in TIFF/F format.\n"
00048    "The \"caller\" option makes the application behave as a calling machine,\n"
00049    "rather than the answering machine. The default behaviour is to behave as\n"
00050    "an answering machine.\n"
00051    "Uses LOCALSTATIONID to identify itself to the remote end.\n"
00052    "     LOCALHEADERINFO to generate a header line on each page.\n"
00053    "Sets REMOTESTATIONID to the sender CSID.\n"
00054    "     FAXPAGES to the number of pages received.\n"
00055    "     FAXBITRATE to the transmition rate.\n" "     FAXRESOLUTION to the resolution.\n" "Returns -1 when the user hangs up.\n" "Returns 0 otherwise.\n";
00056 
00057 #define MAX_BLOCK_SIZE 240
00058 
00059 static void span_message(int level, const char *msg)
00060 {
00061    int ast_level;
00062 
00063    if (level == SPAN_LOG_WARNING)
00064       ast_level = __LOG_WARNING;
00065    else if (level == SPAN_LOG_WARNING)
00066       ast_level = __LOG_WARNING;
00067    else
00068       ast_level = __LOG_DEBUG;
00069    ast_log(ast_level, __FILE__, __LINE__, __PRETTY_FUNCTION__, msg);
00070 }
00071 
00072 /*- End of function --------------------------------------------------------*/
00073 
00074 static void t30_flush(t30_state_t * s, int which)
00075 {
00076    /* TODO: */
00077 }
00078 
00079 /*- End of function --------------------------------------------------------*/
00080 
00081 static void phase_e_handler(t30_state_t * s, void *user_data, int result)
00082 {
00083    struct ast_channel *chan;
00084    t30_stats_t t;
00085    char local_ident[21];
00086    char far_ident[21];
00087    char buf[11];
00088 
00089    chan = (struct ast_channel *) user_data;
00090    if (result == T30_ERR_OK) {
00091       t30_get_transfer_statistics(s, &t);
00092       t30_get_far_ident(s, far_ident);
00093       t30_get_local_ident(s, local_ident);
00094       ast_log(LOG_DEBUG, "==============================================================================\n");
00095       ast_log(LOG_DEBUG, "Fax successfully received.\n");
00096       ast_log(LOG_DEBUG, "Remote station id: %s\n", far_ident);
00097       ast_log(LOG_DEBUG, "Local station id:  %s\n", local_ident);
00098       ast_log(LOG_DEBUG, "Pages transferred: %i\n", t.pages_transferred);
00099       ast_log(LOG_DEBUG, "Image resolution:  %i x %i\n", t.x_resolution, t.y_resolution);
00100       ast_log(LOG_DEBUG, "Transfer Rate:     %i\n", t.bit_rate);
00101       ast_log(LOG_DEBUG, "==============================================================================\n");
00102       manager_event(EVENT_FLAG_CALL,
00103                  "FaxReceived", "Channel: %s\nExten: %s\nCallerID: %s\nRemoteStationID: %s\nLocalStationID: %s\nPagesTransferred: %i\nResolution: %i\nTransferRate: %i\nFileName: %s\n",
00104                  chan->name, chan->exten, (chan->cid.cid_num) ? chan->cid.cid_num : "", far_ident, local_ident, t.pages_transferred, t.y_resolution, t.bit_rate, s->rx_file);
00105       pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", far_ident);
00106       snprintf(buf, sizeof(buf), "%i", t.pages_transferred);
00107       pbx_builtin_setvar_helper(chan, "FAXPAGES", buf);
00108       snprintf(buf, sizeof(buf), "%i", t.y_resolution);
00109       pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", buf);
00110       snprintf(buf, sizeof(buf), "%i", t.bit_rate);
00111       pbx_builtin_setvar_helper(chan, "FAXBITRATE", buf);
00112    } else {
00113       ast_log(LOG_DEBUG, "==============================================================================\n");
00114       ast_log(LOG_DEBUG, "Fax receive not successful - result (%d) %s.\n", result, t30_completion_code_to_str(result));
00115       ast_log(LOG_DEBUG, "==============================================================================\n");
00116    }
00117 }
00118 
00119 /*- End of function --------------------------------------------------------*/
00120 
00121 static void phase_d_handler(t30_state_t * s, void *user_data, int result)
00122 {
00123    struct ast_channel *chan;
00124    t30_stats_t t;
00125 
00126    chan = (struct ast_channel *) user_data;
00127    if (result) {
00128       t30_get_transfer_statistics(s, &t);
00129       ast_log(LOG_DEBUG, "==============================================================================\n");
00130       ast_log(LOG_DEBUG, "Pages transferred:  %i\n", t.pages_transferred);
00131       ast_log(LOG_DEBUG, "Image size:         %i x %i\n", t.width, t.length);
00132       ast_log(LOG_DEBUG, "Image resolution    %i x %i\n", t.x_resolution, t.y_resolution);
00133       ast_log(LOG_DEBUG, "Transfer Rate:      %i\n", t.bit_rate);
00134       ast_log(LOG_DEBUG, "Bad rows            %i\n", t.bad_rows);
00135       ast_log(LOG_DEBUG, "Longest bad row run %i\n", t.longest_bad_row_run);
00136       ast_log(LOG_DEBUG, "Compression type    %i\n", t.encoding);
00137       ast_log(LOG_DEBUG, "Image size (bytes)  %i\n", t.image_size);
00138       ast_log(LOG_DEBUG, "==============================================================================\n");
00139    }
00140 }
00141 
00142 /*- End of function --------------------------------------------------------*/
00143 
00144 static int rxfax_exec(struct ast_channel *chan, void *data)
00145 {
00146    int res = 0;
00147    char template_file[256];
00148    char target_file[256];
00149    char *s;
00150    char *t;
00151    char *v;
00152    const char *x;
00153    int option;
00154    int len;
00155    int i;
00156    fax_state_t fax;
00157    int calling_party;
00158    int verbose;
00159    int samples;
00160 
00161    struct ast_module_user *u;
00162    struct ast_frame *inf = NULL;
00163    struct ast_frame outf;
00164 
00165    int original_read_fmt;
00166    int original_write_fmt;
00167 
00168    uint8_t __buf[sizeof(uint16_t) * MAX_BLOCK_SIZE + 2 * AST_FRIENDLY_OFFSET];
00169    uint8_t *buf = __buf + AST_FRIENDLY_OFFSET;
00170 
00171    if (chan == NULL) {
00172       ast_log(LOG_WARNING, "Fax receive channel is NULL. Giving up.\n");
00173       return -1;
00174    }
00175 
00176    span_set_message_handler(span_message);
00177 
00178    /* The next few lines of code parse out the filename and header from the input string */
00179    if (data == NULL) {
00180       /* No data implies no filename or anything is present */
00181       ast_log(LOG_WARNING, "Rxfax requires an argument (filename)\n");
00182       return -1;
00183    }
00184 
00185    calling_party = FALSE;
00186    verbose = FALSE;
00187    target_file[0] = '\0';
00188 
00189    for (option = 0, v = s = data; v; option++, s++) {
00190       t = s;
00191       v = strchr(s, '|');
00192       s = (v) ? v : s + strlen(s);
00193       strncpy((char *) buf, t, s - t);
00194       buf[s - t] = '\0';
00195       if (option == 0) {
00196          /* The first option is always the file name */
00197          len = s - t;
00198          if (len > 255)
00199             len = 255;
00200          strncpy(target_file, t, len);
00201          target_file[len] = '\0';
00202          /* Allow the use of %d in the file name for a wild card of sorts, to
00203             create a new file with the specified name scheme */
00204          if ((x = strchr(target_file, '%')) && x[1] == 'd') {
00205             strcpy(template_file, target_file);
00206             i = 0;
00207             do {
00208                snprintf(target_file, 256, template_file, 1);
00209                i++;
00210             }
00211             while (ast_fileexists(target_file, "", chan->language) != -1);
00212          }
00213       } else if (strncmp("caller", t, s - t) == 0) {
00214          calling_party = TRUE;
00215       } else if (strncmp("debug", t, s - t) == 0) {
00216          verbose = TRUE;
00217       }
00218    }
00219 
00220    /* Done parsing */
00221 
00222    u = ast_module_user_add(chan);
00223 
00224    if (chan->_state != AST_STATE_UP) {
00225       /* Shouldn't need this, but checking to see if channel is already answered
00226        * Theoretically asterisk should already have answered before running the app */
00227       res = ast_answer(chan);
00228    }
00229 
00230    if (!res) {
00231       original_read_fmt = chan->readformat;
00232       if (original_read_fmt != AST_FORMAT_SLINEAR) {
00233          res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
00234          if (res < 0) {
00235             ast_log(LOG_WARNING, "Unable to set to linear read mode, giving up\n");
00236             return -1;
00237          }
00238       }
00239       original_write_fmt = chan->writeformat;
00240       if (original_write_fmt != AST_FORMAT_SLINEAR) {
00241          res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
00242          if (res < 0) {
00243             ast_log(LOG_WARNING, "Unable to set to linear write mode, giving up\n");
00244             res = ast_set_read_format(chan, original_read_fmt);
00245             if (res)
00246                ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
00247             return -1;
00248          }
00249       }
00250       fax_init(&fax, calling_party);
00251       if (verbose)
00252          fax.logging.level = SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW;
00253       x = pbx_builtin_getvar_helper(chan, "LOCALSTATIONID");
00254       if (x && x[0])
00255          t30_set_local_ident(&fax.t30_state, x);
00256       x = pbx_builtin_getvar_helper(chan, "LOCALHEADERINFO");
00257       if (x && x[0])
00258          t30_set_header_info(&fax.t30_state, x);
00259       t30_set_rx_file(&fax.t30_state, target_file, -1);
00260       //t30_set_phase_b_handler(&fax.t30_state, phase_b_handler, chan);
00261       t30_set_phase_d_handler(&fax.t30_state, phase_d_handler, chan);
00262       t30_set_phase_e_handler(&fax.t30_state, phase_e_handler, chan);
00263       t30_set_ecm_capability(&fax.t30_state, TRUE);
00264       t30_set_supported_compressions(&fax.t30_state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
00265       while (ast_waitfor(chan, -1) > -1) {
00266          inf = ast_read(chan);
00267          if (inf == NULL) {
00268             res = -1;
00269             break;
00270          }
00271          if (inf->frametype == AST_FRAME_VOICE) {
00272             if (fax_rx(&fax, inf->data, inf->samples))
00273                break;
00274             samples = (inf->samples <= MAX_BLOCK_SIZE) ? inf->samples : MAX_BLOCK_SIZE;
00275             len = fax_tx(&fax, (int16_t *) & buf[AST_FRIENDLY_OFFSET], samples);
00276             if (len) {
00277                memset(&outf, 0, sizeof(outf));
00278                outf.frametype = AST_FRAME_VOICE;
00279                outf.subclass = AST_FORMAT_SLINEAR;
00280                outf.datalen = len * sizeof(int16_t);
00281                outf.samples = len;
00282                outf.data = &buf[AST_FRIENDLY_OFFSET];
00283                outf.offset = AST_FRIENDLY_OFFSET;
00284                outf.src = "RxFAX";
00285                if (ast_write(chan, &outf) < 0) {
00286                   ast_log(LOG_WARNING, "Unable to write frame to channel; %s\n", strerror(errno));
00287                   break;
00288                }
00289             }
00290          }
00291          ast_frfree(inf);
00292       }
00293       if (inf == NULL) {
00294          ast_log(LOG_DEBUG, "Got hangup\n");
00295          res = -1;
00296       }
00297       if (original_read_fmt != AST_FORMAT_SLINEAR) {
00298          res = ast_set_read_format(chan, original_read_fmt);
00299          if (res)
00300             ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
00301       }
00302       if (original_write_fmt != AST_FORMAT_SLINEAR) {
00303          res = ast_set_write_format(chan, original_write_fmt);
00304          if (res)
00305             ast_log(LOG_WARNING, "Unable to restore write format on '%s'\n", chan->name);
00306       }
00307       t30_terminate(&fax.t30_state);
00308    } else {
00309       ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
00310    }
00311    ast_module_user_remove(u);
00312    return res;
00313 }
00314 
00315 /*- End of function --------------------------------------------------------*/
00316 
00317 static int unload_module(void)
00318 {
00319    int res;
00320 
00321    ast_module_user_hangup_all();
00322 
00323    res = ast_unregister_application(app);
00324 
00325 
00326    return res;
00327 }
00328 
00329 /*- End of function --------------------------------------------------------*/
00330 
00331 static int load_module(void)
00332 {
00333    return ast_register_application(app, rxfax_exec, synopsis, descrip);
00334 }
00335 
00336 /*- End of function --------------------------------------------------------*/
00337 
00338 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Trivial FAX Receive Application");
00339 
00340 /*- End of file ------------------------------------------------------------*/

Generated on Mon May 14 04:42:53 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1