Mon Mar 31 07:37:55 2008

Asterisk developer's documentation


app_rxfax.c

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

Generated on Mon Mar 31 07:37:55 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.1