Fri Jul 3 20:36:05 2009

Asterisk developer's documentation


app_fax.c File Reference

#include "asterisk.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <pthread.h>
#include <errno.h>
#include <tiffio.h>
#include <spandsp.h>
#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/dsp.h"
#include "asterisk/module.h"
#include "asterisk/manager.h"

Include dependency graph for app_fax.c:

Go to the source code of this file.

Data Structures

struct  fax_session

Defines

#define ast_debug(level,...)
#define MAX_SAMPLES   240
#define WATCHDOG_STATE_TIMEOUT   5 * 60
#define WATCHDOG_TOTAL_TIMEOUT   30 * 60

Functions

static void __reg_module (void)
static void __unreg_module (void)
int ast_tvdiff_sec (struct timeval end, struct timeval start)
int64_t ast_tvdiff_us (struct timeval end, struct timeval start)
static void * fax_generator_alloc (struct ast_channel *chan, void *params)
static int fax_generator_generate (struct ast_channel *chan, void *data, int len, int samples)
static int load_module (void)
static void phase_e_handler (t30_state_t *f, void *user_data, int result)
static int rcvfax_exec (struct ast_channel *chan, void *data)
static void set_ecm (t30_state_t *state, int ecm)
static void set_file (t30_state_t *state, fax_session *s)
static void set_local_info (t30_state_t *state, fax_session *s)
static int set_logging (logging_state_t *state)
static int sndfax_exec (struct ast_channel *chan, void *data)
static void span_message (int level, const char *msg)
static int t38_tx_packet_handler (t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
static int transmit (fax_session *s)
static int transmit_audio (fax_session *s)
static int transmit_t38 (fax_session *s)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "Simple FAX Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, }
static char * app_rcvfax_desc
static char * app_rcvfax_name = "ReceiveFAX"
static char * app_rcvfax_synopsis = "Receive a FAX"
static char * app_rxfax_name = "RxFAX"
static char * app_sndfax_desc
static char * app_sndfax_name = "SendFAX"
static char * app_sndfax_synopsis = "Send a FAX"
static char * app_txfax_name = "TxFAX"
static const struct
ast_module_info * 
ast_module_info = &__mod_info
struct ast_generator generator
int option_debug


Define Documentation

#define ast_debug ( level,
...   ) 

Value:

do {       \
   if (option_debug >= (level) ) \
      ast_log(LOG_DEBUG, __VA_ARGS__); \
} while (0)

Definition at line 46 of file app_fax.c.

Referenced by phase_e_handler(), transmit(), transmit_audio(), and transmit_t38().

#define MAX_SAMPLES   240

Definition at line 124 of file app_fax.c.

Referenced by fax_generator_generate().

#define WATCHDOG_STATE_TIMEOUT   5 * 60

Definition at line 133 of file app_fax.c.

Referenced by transmit_audio(), and transmit_t38().

#define WATCHDOG_TOTAL_TIMEOUT   30 * 60

Definition at line 132 of file app_fax.c.

Referenced by transmit_audio(), and transmit_t38().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 796 of file app_fax.c.

static void __unreg_module ( void   )  [static]

Definition at line 796 of file app_fax.c.

int ast_tvdiff_sec ( struct timeval  end,
struct timeval  start 
) [inline]

Definition at line 52 of file app_fax.c.

References result.

Referenced by transmit_audio(), and transmit_t38().

00053 {
00054    int result = end.tv_sec - start.tv_sec;
00055    if (result > 0 && end.tv_usec < start.tv_usec)
00056       result--;
00057    else if (result < 0 && end.tv_usec > start.tv_usec)
00058       result++;
00059 
00060    return result;
00061 }

int64_t ast_tvdiff_us ( struct timeval  end,
struct timeval  start 
)

Definition at line 63 of file app_fax.c.

Referenced by transmit_t38().

00064 {
00065    return (end.tv_sec - start.tv_sec) * (int64_t) 1000000 +
00066       end.tv_usec - start.tv_usec;
00067 }

static void* fax_generator_alloc ( struct ast_channel *  chan,
void *  params 
) [static]

Definition at line 294 of file app_fax.c.

00295 {
00296    return params;
00297 }

static int fax_generator_generate ( struct ast_channel *  chan,
void *  data,
int  len,
int  samples 
) [static]

Definition at line 299 of file app_fax.c.

References AST_FORMAT_SLINEAR, AST_FRAME_SET_BUFFER, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), ast_write(), errno, ast_frame::frametype, LOG_WARNING, MAX_SAMPLES, ast_channel::name, and ast_frame::samples.

00300 {
00301    fax_state_t *fax = (fax_state_t*) data;
00302    uint8_t buffer[AST_FRIENDLY_OFFSET + MAX_SAMPLES * sizeof(uint16_t)];
00303    int16_t *buf = (int16_t *) (buffer + AST_FRIENDLY_OFFSET);
00304     
00305    struct ast_frame outf = {
00306       .frametype = AST_FRAME_VOICE,
00307       .subclass = AST_FORMAT_SLINEAR,
00308       .src = __FUNCTION__,
00309    };
00310 
00311    if (samples > MAX_SAMPLES) {
00312       ast_log(LOG_WARNING, "Only generating %d samples, where %d requested\n", MAX_SAMPLES, samples);
00313       samples = MAX_SAMPLES;
00314    }
00315    
00316    if ((len = fax_tx(fax, buf, samples)) > 0) {
00317       outf.samples = len;
00318       AST_FRAME_SET_BUFFER(&outf, buffer, AST_FRIENDLY_OFFSET, len * sizeof(int16_t));
00319 
00320       if (ast_write(chan, &outf) < 0) {
00321          ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno));
00322          return -1;
00323       }
00324    }
00325 
00326    return 0;
00327 }

static int load_module ( void   )  [static]

Definition at line 777 of file app_fax.c.

References ast_register_application(), rcvfax_exec(), and sndfax_exec().

00778 {
00779    int res ;
00780 
00781    res = ast_register_application(app_sndfax_name, sndfax_exec, app_sndfax_synopsis, app_sndfax_desc);
00782    res |= ast_register_application(app_txfax_name, sndfax_exec, app_sndfax_synopsis, app_sndfax_desc);
00783    res |= ast_register_application(app_rcvfax_name, rcvfax_exec, app_rcvfax_synopsis, app_rcvfax_desc);
00784    res |= ast_register_application(app_rxfax_name, rcvfax_exec, app_rcvfax_synopsis, app_rcvfax_desc);
00785 
00786    /* The default SPAN message handler prints to stderr. It is something we do not want */
00787    span_set_message_handler(NULL);
00788 
00789    return res;
00790 }

static void phase_e_handler ( t30_state_t *  f,
void *  user_data,
int  result 
) [static]

Definition at line 183 of file app_fax.c.

References ast_debug, ast_log(), fax_session::chan, ast_channel::cid, ast_callerid::cid_num, fax_session::direction, EVENT_FLAG_CALL, ast_channel::exten, fax_session::file_name, fax_session::finished, LOG_WARNING, manager_event(), ast_channel::name, pbx_builtin_setvar_helper(), s, and S_OR.

Referenced by transmit_audio(), and transmit_t38().

00184 {
00185    const char *local_ident;
00186    const char *far_ident;
00187    char buf[20];
00188    fax_session *s = (fax_session *) user_data;
00189    t30_stats_t stat;
00190 
00191    ast_debug(1, "Fax phase E handler. result=%d\n", result);
00192 
00193    t30_get_transfer_statistics(f, &stat);
00194 
00195    s = (fax_session *) user_data;
00196 
00197    if (result != T30_ERR_OK) {
00198       s->finished = -1;
00199 
00200       /* FAXSTATUS is already set to FAILED */
00201       pbx_builtin_setvar_helper(s->chan, "FAXERROR", t30_completion_code_to_str(result));
00202 
00203       ast_log(LOG_WARNING, "Error transmitting fax. result=%d: %s.\n", result, t30_completion_code_to_str(result));
00204 
00205       return;
00206    }
00207    
00208    s->finished = 1; 
00209    
00210    local_ident = t30_get_tx_ident(f);
00211    far_ident = t30_get_rx_ident(f);
00212    pbx_builtin_setvar_helper(s->chan, "FAXSTATUS", "SUCCESS"); 
00213    pbx_builtin_setvar_helper(s->chan, "FAXERROR", NULL); 
00214    pbx_builtin_setvar_helper(s->chan, "REMOTESTATIONID", far_ident);
00215    snprintf(buf, sizeof(buf), "%d", stat.pages_transferred);
00216    pbx_builtin_setvar_helper(s->chan, "FAXPAGES", buf);
00217    snprintf(buf, sizeof(buf), "%d", stat.y_resolution);
00218    pbx_builtin_setvar_helper(s->chan, "FAXRESOLUTION", buf);
00219    snprintf(buf, sizeof(buf), "%d", stat.bit_rate);
00220    pbx_builtin_setvar_helper(s->chan, "FAXBITRATE", buf); 
00221    
00222    ast_debug(1, "Fax transmitted successfully.\n");
00223    ast_debug(1, "  Remote station ID: %s\n", far_ident);
00224    ast_debug(1, "  Pages transferred: %d\n", stat.pages_transferred);
00225    ast_debug(1, "  Image resolution:  %d x %d\n", stat.x_resolution, stat.y_resolution);
00226    ast_debug(1, "  Transfer Rate:     %d\n", stat.bit_rate);
00227    
00228    manager_event(EVENT_FLAG_CALL,
00229             s->direction ? "FaxSent" : "FaxReceived", 
00230             "Channel: %s\r\n"
00231             "Exten: %s\r\n"
00232             "CallerID: %s\r\n"
00233             "RemoteStationID: %s\r\n"
00234             "LocalStationID: %s\r\n"
00235             "PagesTransferred: %d\r\n"
00236             "Resolution: %d\r\n"
00237             "TransferRate: %d\r\n"
00238             "FileName: %s\r\n",
00239             s->chan->name,
00240             s->chan->exten,
00241             S_OR(s->chan->cid.cid_num, ""),
00242             far_ident,
00243             local_ident,
00244             stat.pages_transferred,
00245             stat.y_resolution,
00246             stat.bit_rate,
00247             s->file_name);
00248 }

static int rcvfax_exec ( struct ast_channel *  chan,
void *  data 
) [static]

Definition at line 721 of file app_fax.c.

References AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log(), AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero(), fax_session::caller_mode, fax_session::chan, fax_session::direction, FALSE, fax_session::file_name, fax_session::finished, LOG_ERROR, parse(), transmit(), and TRUE.

Referenced by load_module().

00722 {
00723    int res = 0;
00724    char *parse;
00725    fax_session session;
00726 
00727    AST_DECLARE_APP_ARGS(args,
00728       AST_APP_ARG(file_name);
00729       AST_APP_ARG(options);
00730    );
00731 
00732    if (chan == NULL) {
00733       ast_log(LOG_ERROR, "Fax channel is NULL. Giving up.\n");
00734       return -1;
00735    }
00736 
00737    /* The next few lines of code parse out the filename and header from the input string */
00738    if (ast_strlen_zero(data)) {
00739       /* No data implies no filename or anything is present */
00740       ast_log(LOG_ERROR, "ReceiveFAX requires an argument (filename)\n");
00741       return -1;
00742    }
00743 
00744    parse = ast_strdupa(data);
00745    AST_STANDARD_APP_ARGS(args, parse);
00746    
00747    session.caller_mode = FALSE;
00748 
00749    if (args.options) {
00750       if (strchr(args.options, 'c'))
00751          session.caller_mode = TRUE;
00752    }
00753 
00754    /* Done parsing */
00755    session.direction = 0;
00756    session.file_name = args.file_name;
00757    session.chan = chan;
00758    session.finished = 0;
00759 
00760    res = transmit(&session);
00761 
00762    return res;
00763 }

static void set_ecm ( t30_state_t *  state,
int  ecm 
) [static]

Definition at line 284 of file app_fax.c.

Referenced by transmit_audio(), and transmit_t38().

00285 {
00286    t30_set_ecm_capability(state, ecm);
00287    t30_set_supported_compressions(state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
00288 }

static void set_file ( t30_state_t *  state,
fax_session *  s 
) [static]

Definition at line 276 of file app_fax.c.

References fax_session::direction, and fax_session::file_name.

Referenced by transmit_audio(), and transmit_t38().

00277 {
00278    if (s->direction)
00279       t30_set_tx_file(state, s->file_name, -1, -1);
00280    else
00281       t30_set_rx_file(state, s->file_name, -1);
00282 }

static void set_local_info ( t30_state_t *  state,
fax_session *  s 
) [static]

Definition at line 263 of file app_fax.c.

References ast_strlen_zero(), fax_session::chan, and pbx_builtin_getvar_helper().

Referenced by transmit_audio(), and transmit_t38().

00264 {
00265    const char *x;
00266 
00267    x = pbx_builtin_getvar_helper(s->chan, "LOCALSTATIONID");
00268    if (!ast_strlen_zero(x))
00269       t30_set_tx_ident(state, x);
00270 
00271    x = pbx_builtin_getvar_helper(s->chan, "LOCALHEADERINFO");
00272    if (!ast_strlen_zero(x))
00273       t30_set_tx_page_header_info(state, x);
00274 }

static int set_logging ( logging_state_t *  state  )  [static]

Definition at line 253 of file app_fax.c.

References span_message().

Referenced by transmit_audio(), and transmit_t38().

00254 {
00255    int level = SPAN_LOG_WARNING + option_debug;
00256 
00257    span_log_set_message_handler(state, span_message);
00258    span_log_set_level(state, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | level); 
00259 
00260    return 0;
00261 }

static int sndfax_exec ( struct ast_channel *  chan,
void *  data 
) [static]

Definition at line 677 of file app_fax.c.

References AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log(), AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero(), fax_session::caller_mode, fax_session::chan, fax_session::direction, FALSE, fax_session::file_name, fax_session::finished, LOG_ERROR, parse(), transmit(), and TRUE.

Referenced by load_module().

00678 {
00679    int res = 0;
00680    char *parse;
00681    fax_session session;
00682 
00683    AST_DECLARE_APP_ARGS(args,
00684       AST_APP_ARG(file_name);
00685       AST_APP_ARG(options);
00686    );
00687 
00688    if (chan == NULL) {
00689       ast_log(LOG_ERROR, "Fax channel is NULL. Giving up.\n");
00690       return -1;
00691    }
00692 
00693    /* The next few lines of code parse out the filename and header from the input string */
00694    if (ast_strlen_zero(data)) {
00695       /* No data implies no filename or anything is present */
00696       ast_log(LOG_ERROR, "SendFAX requires an argument (filename)\n");
00697       return -1;
00698    }
00699 
00700    parse = ast_strdupa(data);
00701    AST_STANDARD_APP_ARGS(args, parse);
00702    
00703    session.caller_mode = TRUE;
00704 
00705    if (args.options) {
00706       if (strchr(args.options, 'a'))
00707          session.caller_mode = FALSE;
00708    }
00709 
00710    /* Done parsing */
00711    session.direction = 1;
00712    session.file_name = args.file_name;
00713    session.chan = chan;
00714    session.finished = 0;
00715 
00716    res = transmit(&session);
00717 
00718    return res;
00719 }

static void span_message ( int  level,
const char *  msg 
) [static]

Definition at line 149 of file app_fax.c.

References ast_log(), LOG_DEBUG, LOG_ERROR, and LOG_WARNING.

Referenced by set_logging().

00150 {
00151    if (level == SPAN_LOG_ERROR) {
00152       ast_log(LOG_ERROR, "%s", msg);
00153    } else if (level == SPAN_LOG_WARNING) {
00154       ast_log(LOG_WARNING, "%s", msg);
00155    } else {
00156       ast_log(LOG_DEBUG, "%s", msg);
00157    }
00158 }

static int t38_tx_packet_handler ( t38_core_state_t *  s,
void *  user_data,
const uint8_t *  buf,
int  len,
int  count 
) [static]

Definition at line 160 of file app_fax.c.

References AST_FRAME_MODEM, AST_FRAME_SET_BUFFER, ast_log(), AST_MODEM_T38, ast_write(), errno, ast_frame::frametype, and LOG_WARNING.

Referenced by transmit_t38().

00161 {
00162    struct ast_channel *chan = (struct ast_channel *) user_data;
00163 
00164    struct ast_frame outf = {
00165       .frametype = AST_FRAME_MODEM,
00166       .subclass = AST_MODEM_T38,
00167       .src = __FUNCTION__,
00168    };
00169 
00170    /* TODO: Asterisk does not provide means of resending the same packet multiple
00171      times so count is ignored at the moment */
00172 
00173    AST_FRAME_SET_BUFFER(&outf, buf, 0, len);
00174 
00175    if (ast_write(chan, &outf) < 0) {
00176       ast_log(LOG_WARNING, "Unable to write frame to channel; %s\n", strerror(errno));
00177       return -1;
00178    }
00179 
00180    return 0;
00181 }

static int transmit ( fax_session *  s  )  [static]

Definition at line 616 of file app_fax.c.

References ast_channel::_state, ast_answer(), ast_debug, ast_log(), AST_STATE_UP, fax_session::chan, fax_session::finished, LOG_ERROR, LOG_WARNING, ast_channel::name, pbx_builtin_setvar_helper(), transmit_audio(), and transmit_t38().

Referenced by rcvfax_exec(), and sndfax_exec().

00617 {
00618    int res = 0;
00619 
00620    /* Clear all channel variables which to be set by the application.
00621       Pre-set status to error so in case of any problems we can just leave */
00622    pbx_builtin_setvar_helper(s->chan, "FAXSTATUS", "FAILED"); 
00623    pbx_builtin_setvar_helper(s->chan, "FAXERROR", "Channel problems"); 
00624 
00625    pbx_builtin_setvar_helper(s->chan, "REMOTESTATIONID", NULL);
00626    pbx_builtin_setvar_helper(s->chan, "FAXPAGES", NULL);
00627    pbx_builtin_setvar_helper(s->chan, "FAXRESOLUTION", NULL);
00628    pbx_builtin_setvar_helper(s->chan, "FAXBITRATE", NULL); 
00629 
00630    if (s->chan->_state != AST_STATE_UP) {
00631       /* Shouldn't need this, but checking to see if channel is already answered
00632        * Theoretically asterisk should already have answered before running the app */
00633       res = ast_answer(s->chan);
00634       if (res) {
00635          ast_log(LOG_WARNING, "Could not answer channel '%s'\n", s->chan->name);
00636          return res;
00637       }
00638    }
00639 
00640 #ifdef AST_HAS_T38_SUPPORT
00641    s->t38state = ast_channel_get_t38_state(s->chan);
00642    if (s->t38state != T38_STATE_NEGOTIATED) {
00643 #endif
00644       /* T38 is not negotiated on the channel yet. First start regular transmission. If it switches to T38, follow */   
00645       res = transmit_audio(s);
00646       if (res > 0) {
00647          /* transmit_audio reports switchover to T38. Update t38state */
00648 #ifdef AST_HAS_T38_SUPPORT
00649          s->t38state = ast_channel_get_t38_state(s->chan);
00650          if (s->t38state != T38_STATE_NEGOTIATED) {
00651             ast_log(LOG_ERROR, "Audio loop reports T38 switchover but t38state != T38_STATE_NEGOTIATED\n");
00652          }
00653 #endif
00654       }
00655 #ifdef AST_HAS_T38_SUPPORT
00656    }
00657 
00658    if (s->t38state == T38_STATE_NEGOTIATED) {
00659       res = transmit_t38(s);
00660    }
00661 #endif
00662 
00663    if (res) {
00664       ast_log(LOG_WARNING, "Transmission error\n");
00665       res = -1;
00666    } else if (s->finished < 0) {
00667       ast_log(LOG_WARNING, "Transmission failed\n");
00668    } else if (s->finished > 0) {
00669       ast_debug(1, "Transmission finished Ok\n");
00670    }
00671 
00672    return res;
00673 }

static int transmit_audio ( fax_session *  s  )  [static]

Definition at line 337 of file app_fax.c.

References ast_activate_generator(), ast_deactivate_generator(), ast_debug, ast_dsp_free(), ast_dsp_new(), ast_dsp_process(), ast_dsp_set_features(), AST_FORMAT_SLINEAR, AST_FRAME_CONTROL, AST_FRAME_DTMF, AST_FRAME_VOICE, ast_frdup(), ast_frfree, ast_indicate_data(), ast_log(), ast_read(), ast_set_read_format(), ast_set_write_format(), ast_tvdiff_sec(), ast_tvnow(), ast_waitfor(), fax_session::caller_mode, fax_session::chan, ast_frame::data, ast_frame::datalen, fax_session::direction, DSP_FEATURE_FAX_DETECT, fax_session::finished, ast_frame::frametype, LOG_WARNING, ast_channel::name, phase_e_handler(), ast_channel::readformat, ast_frame::samples, set_ecm(), set_file(), set_local_info(), set_logging(), ast_frame::subclass, TRUE, WATCHDOG_STATE_TIMEOUT, WATCHDOG_TOTAL_TIMEOUT, and ast_channel::writeformat.

Referenced by transmit().

00338 {
00339    int res = -1;
00340    int original_read_fmt = AST_FORMAT_SLINEAR;
00341    int original_write_fmt = AST_FORMAT_SLINEAR;
00342    fax_state_t fax;
00343    struct ast_dsp *dsp = NULL;
00344    int detect_tone = 0;
00345    struct ast_frame *inf = NULL;
00346    struct ast_frame *fr;
00347    int last_state = 0;
00348    struct timeval now, start, state_change;
00349 #ifdef AST_HAS_T38_SUPPORT
00350    enum ast_control_t38 t38control;
00351 #endif
00352 
00353    original_read_fmt = s->chan->readformat;
00354    if (original_read_fmt != AST_FORMAT_SLINEAR) {
00355       res = ast_set_read_format(s->chan, AST_FORMAT_SLINEAR);
00356       if (res < 0) {
00357          ast_log(LOG_WARNING, "Unable to set to linear read mode, giving up\n");
00358          goto done;
00359       }
00360    }
00361 
00362    original_write_fmt = s->chan->writeformat;
00363    if (original_write_fmt != AST_FORMAT_SLINEAR) {
00364       res = ast_set_write_format(s->chan, AST_FORMAT_SLINEAR);
00365       if (res < 0) {
00366          ast_log(LOG_WARNING, "Unable to set to linear write mode, giving up\n");
00367          goto done;
00368       }
00369    }
00370 
00371    /* Initialize T30 terminal */
00372    fax_init(&fax, s->caller_mode);
00373 
00374    /* Setup logging */
00375    set_logging(&fax.logging);
00376    set_logging(&fax.t30_state.logging);
00377 
00378    /* Configure terminal */
00379    set_local_info(&fax.t30_state, s);
00380    set_file(&fax.t30_state, s);
00381    set_ecm(&fax.t30_state, TRUE);
00382 
00383    fax_set_transmit_on_idle(&fax, TRUE);
00384 
00385    t30_set_phase_e_handler(&fax.t30_state, phase_e_handler, s);
00386 
00387 #ifdef AST_HAS_T38_SUPPORT
00388    if (s->t38state == T38_STATE_UNAVAILABLE) {
00389       ast_debug(1, "T38 is unavailable on %s\n", s->chan->name);
00390    } else if (!s->direction) {
00391       /* We are receiving side and this means we are the side which should
00392          request T38 when the fax is detected. Use DSP to detect fax tone */
00393       ast_debug(1, "Setting up CNG detection on %s\n", s->chan->name);
00394       dsp = ast_dsp_new();
00395       ast_dsp_set_features(dsp, DSP_FEATURE_FAX_DETECT);
00396       ast_dsp_set_faxmode(dsp, DSP_FAXMODE_DETECT_CNG);
00397       detect_tone = 1;
00398    }
00399 #endif
00400 
00401    start = state_change = ast_tvnow();
00402 
00403    ast_activate_generator(s->chan, &generator, &fax);
00404 
00405    while (!s->finished) {
00406       res = ast_waitfor(s->chan, 20);
00407       if (res < 0)
00408          break;
00409       else if (res > 0)
00410          res = 0;
00411 
00412       inf = ast_read(s->chan);
00413       if (inf == NULL) {
00414          ast_debug(1, "Channel hangup\n");
00415          res = -1;
00416          break;
00417       }
00418 
00419       ast_debug(10, "frame %d/%d, len=%d\n", inf->frametype, inf->subclass, inf->datalen);
00420 
00421 /* FIXME: that part should probably be included, not cut off (Tzafrir)*/
00422 #ifdef AST_HAS_T38_SUPPORT
00423       /* Detect fax tone */
00424       if (detect_tone && inf->frametype == AST_FRAME_VOICE) {
00425          /* Duplicate frame because ast_dsp_process may free the frame passed */
00426          fr = ast_frdup(inf);
00427 
00428          /* Do not pass channel to ast_dsp_process otherwise it may queue modified audio frame back */
00429          fr = ast_dsp_process(NULL, dsp, fr);
00430          if (fr && fr->frametype == AST_FRAME_DTMF && fr->subclass == 'f') {
00431             ast_debug(1, "Fax tone detected. Requesting T38\n");
00432             t38control = AST_T38_REQUEST_NEGOTIATE;
00433             ast_indicate_data(s->chan, AST_CONTROL_T38, &t38control, sizeof(t38control));
00434             detect_tone = 0;
00435          }
00436 
00437          ast_frfree(fr);
00438       }
00439 #endif
00440 
00441 
00442       /* Check the frame type. Format also must be checked because there is a chance
00443          that a frame in old format was already queued before we set chanel format
00444          to slinear so it will still be received by ast_read */
00445       if (inf->frametype == AST_FRAME_VOICE && inf->subclass == AST_FORMAT_SLINEAR) {
00446 
00447          if (fax_rx(&fax, inf->data, inf->samples) < 0) {
00448             /* I know fax_rx never returns errors. The check here is for good style only */
00449             ast_log(LOG_WARNING, "fax_rx returned error\n");
00450             res = -1;
00451             break;
00452          }
00453 
00454          /* Watchdog */
00455          if (last_state != fax.t30_state.state) {
00456             state_change = ast_tvnow();
00457             last_state = fax.t30_state.state;
00458          }
00459 #ifdef AST_HAS_T38_SUPPORT
00460       } else if (inf->frametype == AST_FRAME_CONTROL && inf->subclass == AST_CONTROL_T38 &&
00461             inf->datalen == sizeof(enum ast_control_t38)) {
00462          t38control =*((enum ast_control_t38 *) inf->data);
00463          if (t38control == AST_T38_NEGOTIATED) {
00464             /* T38 switchover completed */
00465             ast_debug(1, "T38 negotiated, finishing audio loop\n");
00466             res = 1;
00467             break;
00468          }
00469 #endif
00470       }
00471 
00472       ast_frfree(inf);
00473       inf = NULL;
00474 
00475       /* Watchdog */
00476       now = ast_tvnow();
00477       if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) {
00478          ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n");
00479          res = -1;
00480          break;
00481       }
00482    }
00483 
00484    ast_debug(1, "Loop finished, res=%d\n", res);
00485 
00486    if (inf)
00487       ast_frfree(inf);
00488 
00489    if (dsp)
00490       ast_dsp_free(dsp);
00491 
00492    ast_deactivate_generator(s->chan);
00493 
00494    /* If we are switching to T38, remove phase E handler. Otherwise it will be executed
00495       by t30_terminate, display diagnostics and set status variables although no transmittion
00496       has taken place yet. */
00497    if (res > 0) {
00498       t30_set_phase_e_handler(&fax.t30_state, NULL, NULL);
00499    }
00500 
00501    t30_terminate(&fax.t30_state);
00502    fax_release(&fax);
00503 
00504 done:
00505    if (original_write_fmt != AST_FORMAT_SLINEAR) {
00506       if (ast_set_write_format(s->chan, original_write_fmt) < 0)
00507          ast_log(LOG_WARNING, "Unable to restore write format on '%s'\n", s->chan->name);
00508    }
00509 
00510    if (original_read_fmt != AST_FORMAT_SLINEAR) {
00511       if (ast_set_read_format(s->chan, original_read_fmt) < 0)
00512          ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", s->chan->name);
00513    }
00514 
00515    return res;
00516 
00517 }

static int transmit_t38 ( fax_session *  s  )  [static]

Definition at line 519 of file app_fax.c.

References ast_debug, AST_FRAME_CONTROL, AST_FRAME_MODEM, ast_frfree, ast_log(), AST_MODEM_T38, ast_read(), ast_tvdiff_sec(), ast_tvdiff_us(), ast_tvnow(), ast_waitfor(), fax_session::caller_mode, fax_session::chan, ast_frame::data, ast_frame::datalen, fax_session::finished, ast_frame::frametype, LOG_WARNING, phase_e_handler(), ast_frame::seqno, set_ecm(), set_file(), set_local_info(), set_logging(), ast_frame::subclass, t38_tx_packet_handler(), TRUE, WATCHDOG_STATE_TIMEOUT, and WATCHDOG_TOTAL_TIMEOUT.

Referenced by transmit().

00520 {
00521    int res = 0;
00522    t38_terminal_state_t t38;
00523    struct ast_frame *inf = NULL;
00524    int last_state = 0;
00525    struct timeval now, start, state_change, last_frame;
00526 #ifdef AST_HAS_T38_SUPPORT
00527    enum ast_control_t38 t38control;
00528 #endif
00529 
00530    /* Initialize terminal */
00531    memset(&t38, 0, sizeof(t38));
00532    if (t38_terminal_init(&t38, s->caller_mode, t38_tx_packet_handler, s->chan) == NULL) {
00533       ast_log(LOG_WARNING, "Unable to start T.38 termination.\n");
00534       return -1;
00535    }
00536 
00537    /* Setup logging */
00538    set_logging(&t38.logging);
00539    set_logging(&t38.t30_state.logging);
00540    set_logging(&t38.t38.logging);
00541 
00542    /* Configure terminal */
00543    set_local_info(&t38.t30_state, s);
00544    set_file(&t38.t30_state, s);
00545    set_ecm(&t38.t30_state, TRUE);
00546 
00547    t30_set_phase_e_handler(&t38.t30_state, phase_e_handler, s);
00548 
00549    now = start = state_change = ast_tvnow();
00550 
00551    while (!s->finished) {
00552 
00553       res = ast_waitfor(s->chan, 20);
00554       if (res < 0)
00555          break;
00556       else if (res > 0)
00557          res = 0;
00558 
00559       last_frame = now;
00560       now = ast_tvnow();
00561       t38_terminal_send_timeout(&t38, ast_tvdiff_us(now, last_frame) / (1000000 / 8000));
00562 
00563       inf = ast_read(s->chan);
00564       if (inf == NULL) {
00565          ast_debug(1, "Channel hangup\n");
00566          res = -1;
00567          break;
00568       }
00569 
00570       ast_debug(10, "frame %d/%d, len=%d\n", inf->frametype, inf->subclass, inf->datalen);
00571 
00572       if (inf->frametype == AST_FRAME_MODEM && inf->subclass == AST_MODEM_T38) {
00573          t38_core_rx_ifp_packet(&t38.t38, inf->data, inf->datalen, inf->seqno);
00574 
00575          /* Watchdog */
00576          if (last_state != t38.t30_state.state) {
00577             state_change = ast_tvnow();
00578             last_state = t38.t30_state.state;
00579          }
00580 #ifdef AST_HAS_T38_SUPPORT
00581       } else if (inf->frametype == AST_FRAME_CONTROL && inf->subclass == AST_CONTROL_T38 &&
00582             inf->datalen == sizeof(enum ast_control_t38)) {
00583 
00584          t38control = *((enum ast_control_t38 *) inf->data);
00585 
00586          if (t38control == AST_T38_TERMINATED || t38control == AST_T38_REFUSED) {
00587             ast_debug(1, "T38 down, terminating\n");
00588             res = -1;
00589             break;
00590          }
00591 #endif
00592       }
00593 
00594       ast_frfree(inf);
00595       inf = NULL;
00596 
00597       /* Watchdog */
00598       if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) {
00599          ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n");
00600          res = -1;
00601          break;
00602       }
00603    }
00604 
00605    ast_debug(1, "Loop finished, res=%d\n", res);
00606 
00607    if (inf)
00608       ast_frfree(inf);
00609 
00610    t30_terminate(&t38.t30_state);
00611    t38_terminal_release(&t38);
00612 
00613    return res;
00614 }

static int unload_module ( void   )  [static]

Definition at line 765 of file app_fax.c.

References ast_unregister_application().

00766 {
00767    int res;
00768 
00769    res = ast_unregister_application(app_sndfax_name); 
00770    res |= ast_unregister_application(app_txfax_name);
00771    res |= ast_unregister_application(app_rcvfax_name);   
00772    res |= ast_unregister_application(app_rxfax_name);
00773 
00774    return res;
00775 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "Simple FAX Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static]

Definition at line 796 of file app_fax.c.

char* app_rcvfax_desc [static]

Definition at line 100 of file app_fax.c.

char* app_rcvfax_name = "ReceiveFAX" [static]

Definition at line 97 of file app_fax.c.

char* app_rcvfax_synopsis = "Receive a FAX" [static]

Definition at line 99 of file app_fax.c.

char* app_rxfax_name = "RxFAX" [static]

Definition at line 98 of file app_fax.c.

char* app_sndfax_desc [static]

Definition at line 74 of file app_fax.c.

char* app_sndfax_name = "SendFAX" [static]

Definition at line 71 of file app_fax.c.

char* app_sndfax_synopsis = "Send a FAX" [static]

Definition at line 73 of file app_fax.c.

char* app_txfax_name = "TxFAX" [static]

Definition at line 72 of file app_fax.c.

const struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 796 of file app_fax.c.

Initial value:

Definition at line 329 of file app_fax.c.


Generated on Fri Jul 3 20:36:06 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9