Mon May 14 04:45:40 2007

Asterisk developer's documentation


chan_phone.c File Reference

Generic Linux Telephony Interface driver. More...

#include "asterisk.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <linux/telephony.h>
#include <linux/version.h>
#include <linux/ixjuser.h>
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/logger.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
#include "asterisk/callerid.h"
#include "asterisk/causes.h"
#include "asterisk/stringfields.h"
#include "asterisk/musiconhold.h"
#include "DialTone.h"

Include dependency graph for chan_phone.c:

Go to the source code of this file.

Data Structures

struct  phone_pvt

Defines

#define DEFAULT_CALLER_ID   "Unknown"
#define DEFAULT_GAIN   0x100
#define IXJ_PHONE_RING_START(x)   ioctl(p->fd, PHONE_RING_START, &x);
#define MODE_DIALTONE   1
#define MODE_FXO   3
#define MODE_FXS   4
#define MODE_IMMEDIATE   2
#define MODE_SIGMA   5
#define PHONE_MAX_BUF   480
#define QNDRV_VER   100

Functions

static int __unload_module (void)
 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Linux Telephony API Support")
 AST_MUTEX_DEFINE_STATIC (monlock)
 AST_MUTEX_DEFINE_STATIC (iflock)
static void * do_monitor (void *data)
static int load_module (void)
static struct phone_pvtmkif (char *iface, int mode, int txgain, int rxgain)
static int parse_gain_value (char *gain_type, char *value)
static int phone_answer (struct ast_channel *ast)
static int phone_call (struct ast_channel *ast, char *dest, int timeout)
static void phone_check_exception (struct phone_pvt *i)
static int phone_digit_begin (struct ast_channel *ast, char digit)
static int phone_digit_end (struct ast_channel *ast, char digit, unsigned int duration)
static struct ast_framephone_exception (struct ast_channel *ast)
static int phone_fixup (struct ast_channel *old, struct ast_channel *new)
static int phone_hangup (struct ast_channel *ast)
static int phone_indicate (struct ast_channel *chan, int condition, const void *data, size_t datalen)
static void phone_mini_packet (struct phone_pvt *i)
static struct ast_channelphone_new (struct phone_pvt *i, int state, char *context)
static struct ast_framephone_read (struct ast_channel *ast)
static struct ast_channelphone_request (const char *type, int format, void *data, int *cause)
static int phone_send_text (struct ast_channel *ast, const char *text)
static int phone_setup (struct ast_channel *ast)
static int phone_write (struct ast_channel *ast, struct ast_frame *frame)
static int phone_write_buf (struct phone_pvt *p, const char *buf, int len, int frlen, int swap)
static int restart_monitor (void)
static int unload_module (void)

Variables

static char cid_name [AST_MAX_EXTENSION]
static char cid_num [AST_MAX_EXTENSION]
static const char config [] = "phone.conf"
static char context [AST_MAX_EXTENSION] = "default"
static struct ast_channel_techcur_tech
static int echocancel = AEC_OFF
static struct phone_pvtiflist
static char language [MAX_LANGUAGE] = ""
static unsigned int monitor
static pthread_t monitor_thread = AST_PTHREADT_NULL
static struct ast_channel_tech phone_tech
static struct ast_channel_tech phone_tech_fxs
static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW
static int silencesupression = 0
static const char tdesc [] = "Standard Linux Telephony API Driver"


Detailed Description

Generic Linux Telephony Interface driver.

Author:
Mark Spencer <markster@digium.com>

Definition in file chan_phone.c.


Define Documentation

#define DEFAULT_CALLER_ID   "Unknown"

Definition at line 80 of file chan_phone.c.

Referenced by phone_call().

#define DEFAULT_GAIN   0x100

Definition at line 82 of file chan_phone.c.

Referenced by load_module(), and parse_gain_value().

#define IXJ_PHONE_RING_START (  )     ioctl(p->fd, PHONE_RING_START, &x);

Definition at line 77 of file chan_phone.c.

Referenced by phone_call().

#define MODE_DIALTONE   1

Definition at line 118 of file chan_phone.c.

Referenced by load_module(), and phone_check_exception().

#define MODE_FXO   3

Definition at line 120 of file chan_phone.c.

Referenced by load_module(), mkif(), phone_answer(), phone_exception(), and phone_hangup().

#define MODE_FXS   4

Definition at line 121 of file chan_phone.c.

Referenced by load_module(), mkif(), phone_call(), phone_check_exception(), phone_new(), phone_read(), phone_request(), phone_setup(), and phone_write().

#define MODE_IMMEDIATE   2

Definition at line 119 of file chan_phone.c.

Referenced by load_module(), and phone_check_exception().

#define MODE_SIGMA   5

Definition at line 122 of file chan_phone.c.

Referenced by do_monitor(), load_module(), and phone_check_exception().

#define PHONE_MAX_BUF   480

Definition at line 81 of file chan_phone.c.

Referenced by phone_read().

#define QNDRV_VER   100

Definition at line 67 of file chan_phone.c.


Function Documentation

static int __unload_module ( void   )  [static]

Definition at line 1255 of file chan_phone.c.

References ast_channel_unregister(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), AST_PTHREADT_NULL, AST_PTHREADT_STOP, ast_softhangup(), AST_SOFTHANGUP_APPUNLOAD, cur_tech, phone_pvt::fd, free, iflist, LOG_WARNING, phone_pvt::next, and phone_pvt::owner.

01256 {
01257    struct phone_pvt *p, *pl;
01258    /* First, take us out of the channel loop */
01259    ast_channel_unregister(cur_tech);
01260    if (!ast_mutex_lock(&iflock)) {
01261       /* Hangup all interfaces if they have an owner */
01262       p = iflist;
01263       while(p) {
01264          if (p->owner)
01265             ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
01266          p = p->next;
01267       }
01268       iflist = NULL;
01269       ast_mutex_unlock(&iflock);
01270    } else {
01271       ast_log(LOG_WARNING, "Unable to lock the monitor\n");
01272       return -1;
01273    }
01274    if (!ast_mutex_lock(&monlock)) {
01275       if (monitor_thread > AST_PTHREADT_NULL) {
01276          monitor = 0;
01277          while (pthread_kill(monitor_thread, SIGURG) == 0)
01278             sched_yield();
01279          pthread_join(monitor_thread, NULL);
01280       }
01281       monitor_thread = AST_PTHREADT_STOP;
01282       ast_mutex_unlock(&monlock);
01283    } else {
01284       ast_log(LOG_WARNING, "Unable to lock the monitor\n");
01285       return -1;
01286    }
01287 
01288    if (!ast_mutex_lock(&iflock)) {
01289       /* Destroy all the interfaces and free their memory */
01290       p = iflist;
01291       while(p) {
01292          /* Close the socket, assuming it's real */
01293          if (p->fd > -1)
01294             close(p->fd);
01295          pl = p;
01296          p = p->next;
01297          /* Free associated memory */
01298          free(pl);
01299       }
01300       iflist = NULL;
01301       ast_mutex_unlock(&iflock);
01302    } else {
01303       ast_log(LOG_WARNING, "Unable to lock the monitor\n");
01304       return -1;
01305    }
01306       
01307    return 0;
01308 }

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Linux Telephony API Support"   
)

AST_MUTEX_DEFINE_STATIC ( monlock   ) 

AST_MUTEX_DEFINE_STATIC ( iflock   ) 

static void* do_monitor ( void *  data  )  [static]

Definition at line 990 of file chan_phone.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_select(), phone_pvt::dev, DialTone, phone_pvt::dialtone, phone_pvt::fd, iflist, LOG_DEBUG, LOG_ERROR, LOG_WARNING, phone_pvt::mode, MODE_SIGMA, phone_pvt::next, phone_pvt::owner, phone_check_exception(), and phone_mini_packet().

00991 {
00992    fd_set rfds, efds;
00993    int n, res;
00994    struct phone_pvt *i;
00995    int tonepos = 0;
00996    /* The tone we're playing this round */
00997    struct timeval tv = {0,0};
00998    int dotone;
00999    /* This thread monitors all the frame relay interfaces which are not yet in use
01000       (and thus do not have a separate thread) indefinitely */
01001    while (monitor) {
01002       /* Don't let anybody kill us right away.  Nobody should lock the interface list
01003          and wait for the monitor list, but the other way around is okay. */
01004       /* Lock the interface list */
01005       if (ast_mutex_lock(&iflock)) {
01006          ast_log(LOG_ERROR, "Unable to grab interface lock\n");
01007          return NULL;
01008       }
01009       /* Build the stuff we're going to select on, that is the socket of every
01010          phone_pvt that does not have an associated owner channel */
01011       n = -1;
01012       FD_ZERO(&rfds);
01013       FD_ZERO(&efds);
01014       i = iflist;
01015       dotone = 0;
01016       while (i) {
01017          if (FD_ISSET(i->fd, &rfds)) 
01018             ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
01019          if (!i->owner) {
01020             /* This needs to be watched, as it lacks an owner */
01021             FD_SET(i->fd, &rfds);
01022             FD_SET(i->fd, &efds);
01023             if (i->fd > n)
01024                n = i->fd;
01025             if (i->dialtone && i->mode != MODE_SIGMA) {
01026                /* Remember we're going to have to come back and play
01027                   more dialtones */
01028                if (ast_tvzero(tv)) {
01029                   /* If we're due for a dialtone, play one */
01030                   if (write(i->fd, DialTone + tonepos, 240) != 240)
01031                      ast_log(LOG_WARNING, "Dial tone write error\n");
01032                }
01033                dotone++;
01034             }
01035          }
01036          
01037          i = i->next;
01038       }
01039       /* Okay, now that we know what to do, release the interface lock */
01040       ast_mutex_unlock(&iflock);
01041 
01042       /* Wait indefinitely for something to happen */
01043       if (dotone && i->mode != MODE_SIGMA) {
01044          /* If we're ready to recycle the time, set it to 30 ms */
01045          tonepos += 240;
01046          if (tonepos >= sizeof(DialTone))
01047                tonepos = 0;
01048          if (ast_tvzero(tv)) {
01049             tv = ast_tv(30000, 0);
01050          }
01051          res = ast_select(n + 1, &rfds, NULL, &efds, &tv);
01052       } else {
01053          res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
01054          tv = ast_tv(0,0);
01055          tonepos = 0;
01056       }
01057       /* Okay, select has finished.  Let's see what happened.  */
01058       if (res < 0) {
01059          ast_log(LOG_DEBUG, "select return %d: %s\n", res, strerror(errno));
01060          continue;
01061       }
01062       /* If there are no fd's changed, just continue, it's probably time
01063          to play some more dialtones */
01064       if (!res)
01065          continue;
01066       /* Alright, lock the interface list again, and let's look and see what has
01067          happened */
01068       if (ast_mutex_lock(&iflock)) {
01069          ast_log(LOG_WARNING, "Unable to lock the interface list\n");
01070          continue;
01071       }
01072 
01073       i = iflist;
01074       for(; i; i=i->next) {
01075          if (FD_ISSET(i->fd, &rfds)) {
01076             if (i->owner) {
01077                continue;
01078             }
01079             phone_mini_packet(i);
01080          }
01081          if (FD_ISSET(i->fd, &efds)) {
01082             if (i->owner) {
01083                continue;
01084             }
01085             phone_check_exception(i);
01086          }
01087       }
01088       ast_mutex_unlock(&iflock);
01089    }
01090    return NULL;
01091    
01092 }

static int load_module ( void   )  [static]

Definition at line 1315 of file chan_phone.c.

References __unload_module(), ast_callerid_split(), ast_channel_register(), ast_config_destroy(), ast_config_load(), AST_FORMAT_G723_1, AST_FORMAT_SLINEAR, AST_FORMAT_ULAW, ast_log(), AST_MODULE_LOAD_DECLINE, ast_mutex_lock(), ast_mutex_unlock(), ast_true(), ast_variable_browse(), ast_channel_tech::capabilities, cur_tech, DEFAULT_GAIN, iflist, LOG_ERROR, LOG_WARNING, mkif(), phone_pvt::mode, MODE_DIALTONE, MODE_FXO, MODE_FXS, MODE_IMMEDIATE, MODE_SIGMA, ast_variable::name, ast_variable::next, phone_pvt::next, parse_gain_value(), phone_tech, phone_tech_fxs, restart_monitor(), phone_pvt::rxgain, phone_pvt::txgain, and ast_variable::value.

01316 {
01317    struct ast_config *cfg;
01318    struct ast_variable *v;
01319    struct phone_pvt *tmp;
01320    int mode = MODE_IMMEDIATE;
01321    int txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; /* default gain 1.0 */
01322    cfg = ast_config_load(config);
01323 
01324    /* We *must* have a config file otherwise stop immediately */
01325    if (!cfg) {
01326       ast_log(LOG_ERROR, "Unable to load config %s\n", config);
01327       return AST_MODULE_LOAD_DECLINE;
01328    }
01329    if (ast_mutex_lock(&iflock)) {
01330       /* It's a little silly to lock it, but we mind as well just to be sure */
01331       ast_log(LOG_ERROR, "Unable to lock interface list???\n");
01332       return -1;
01333    }
01334    v = ast_variable_browse(cfg, "interfaces");
01335    while(v) {
01336       /* Create the interface list */
01337       if (!strcasecmp(v->name, "device")) {
01338             tmp = mkif(v->value, mode, txgain, rxgain);
01339             if (tmp) {
01340                tmp->next = iflist;
01341                iflist = tmp;
01342                
01343             } else {
01344                ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
01345                ast_config_destroy(cfg);
01346                ast_mutex_unlock(&iflock);
01347                __unload_module();
01348                return -1;
01349             }
01350       } else if (!strcasecmp(v->name, "silencesupression")) {
01351          silencesupression = ast_true(v->value);
01352       } else if (!strcasecmp(v->name, "language")) {
01353          ast_copy_string(language, v->value, sizeof(language));
01354       } else if (!strcasecmp(v->name, "callerid")) {
01355          ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
01356       } else if (!strcasecmp(v->name, "mode")) {
01357          if (!strncasecmp(v->value, "di", 2)) 
01358             mode = MODE_DIALTONE;
01359          else if (!strncasecmp(v->value, "sig", 3))
01360             mode = MODE_SIGMA;
01361          else if (!strncasecmp(v->value, "im", 2))
01362             mode = MODE_IMMEDIATE;
01363          else if (!strncasecmp(v->value, "fxs", 3)) {
01364             mode = MODE_FXS;
01365             prefformat = 0x01ff0000; /* All non-voice */
01366          }
01367          else if (!strncasecmp(v->value, "fx", 2))
01368             mode = MODE_FXO;
01369          else
01370             ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
01371       } else if (!strcasecmp(v->name, "context")) {
01372          ast_copy_string(context, v->value, sizeof(context));
01373       } else if (!strcasecmp(v->name, "format")) {
01374          if (!strcasecmp(v->value, "g723.1")) {
01375             prefformat = AST_FORMAT_G723_1;
01376          } else if (!strcasecmp(v->value, "slinear")) {
01377             if (mode == MODE_FXS)
01378                 prefformat |= AST_FORMAT_SLINEAR;
01379             else prefformat = AST_FORMAT_SLINEAR;
01380          } else if (!strcasecmp(v->value, "ulaw")) {
01381             prefformat = AST_FORMAT_ULAW;
01382          } else
01383             ast_log(LOG_WARNING, "Unknown format '%s'\n", v->value);
01384       } else if (!strcasecmp(v->name, "echocancel")) {
01385          if (!strcasecmp(v->value, "off")) {
01386             echocancel = AEC_OFF;
01387          } else if (!strcasecmp(v->value, "low")) {
01388             echocancel = AEC_LOW;
01389          } else if (!strcasecmp(v->value, "medium")) {
01390             echocancel = AEC_MED;
01391          } else if (!strcasecmp(v->value, "high")) {
01392             echocancel = AEC_HIGH;
01393          } else 
01394             ast_log(LOG_WARNING, "Unknown echo cancellation '%s'\n", v->value);
01395       } else if (!strcasecmp(v->name, "txgain")) {
01396          txgain = parse_gain_value(v->name, v->value);
01397       } else if (!strcasecmp(v->name, "rxgain")) {
01398          rxgain = parse_gain_value(v->name, v->value);
01399       }  
01400       v = v->next;
01401    }
01402    ast_mutex_unlock(&iflock);
01403 
01404    if (mode == MODE_FXS) {
01405       phone_tech_fxs.capabilities = prefformat;
01406       cur_tech = &phone_tech_fxs;
01407    } else
01408       cur_tech = (struct ast_channel_tech *) &phone_tech;
01409 
01410    /* Make sure we can register our Adtranphone channel type */
01411 
01412    if (ast_channel_register(cur_tech)) {
01413       ast_log(LOG_ERROR, "Unable to register channel class 'Phone'\n");
01414       ast_config_destroy(cfg);
01415       __unload_module();
01416       return -1;
01417    }
01418    ast_config_destroy(cfg);
01419    /* And start the monitor for the first time */
01420    restart_monitor();
01421    return 0;
01422 }

static struct phone_pvt* mkif ( char *  iface,
int  mode,
int  txgain,
int  rxgain 
) [static]

Definition at line 1131 of file chan_phone.c.

References ast_log(), phone_pvt::cid_name, phone_pvt::cid_num, phone_pvt::context, phone_pvt::cpt, phone_pvt::dev, phone_pvt::dialtone, phone_pvt::ext, phone_pvt::fd, free, phone_pvt::language, phone_pvt::lastformat, phone_pvt::lastinput, LOG_DEBUG, LOG_WARNING, malloc, phone_pvt::ministate, phone_pvt::mode, MODE_FXO, MODE_FXS, phone_pvt::next, phone_pvt::obuflen, phone_pvt::owner, phone_pvt::rxgain, phone_pvt::silencesupression, and phone_pvt::txgain.

Referenced by load_module().

01132 {
01133    /* Make a phone_pvt structure for this interface */
01134    struct phone_pvt *tmp;
01135    int flags;  
01136    
01137    tmp = malloc(sizeof(struct phone_pvt));
01138    if (tmp) {
01139       tmp->fd = open(iface, O_RDWR);
01140       if (tmp->fd < 0) {
01141          ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
01142          free(tmp);
01143          return NULL;
01144       }
01145       if (mode == MODE_FXO) {
01146          if (ioctl(tmp->fd, IXJCTL_PORT, PORT_PSTN)) 
01147             ast_log(LOG_DEBUG, "Unable to set port to PSTN\n");
01148       } else {
01149          if (ioctl(tmp->fd, IXJCTL_PORT, PORT_POTS)) 
01150              if (mode != MODE_FXS)
01151                   ast_log(LOG_DEBUG, "Unable to set port to POTS\n");
01152       }
01153       ioctl(tmp->fd, PHONE_PLAY_STOP);
01154       ioctl(tmp->fd, PHONE_REC_STOP);
01155       ioctl(tmp->fd, PHONE_RING_STOP);
01156       ioctl(tmp->fd, PHONE_CPT_STOP);
01157       if (ioctl(tmp->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK)) 
01158          ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",iface, strerror(errno));
01159       if (echocancel != AEC_OFF)
01160          ioctl(tmp->fd, IXJCTL_AEC_START, echocancel);
01161       if (silencesupression) 
01162          tmp->silencesupression = 1;
01163 #ifdef PHONE_VAD
01164       ioctl(tmp->fd, PHONE_VAD, tmp->silencesupression);
01165 #endif
01166       tmp->mode = mode;
01167       flags = fcntl(tmp->fd, F_GETFL);
01168       fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
01169       tmp->owner = NULL;
01170       tmp->lastformat = -1;
01171       tmp->lastinput = -1;
01172       tmp->ministate = 0;
01173       memset(tmp->ext, 0, sizeof(tmp->ext));
01174       ast_copy_string(tmp->language, language, sizeof(tmp->language));
01175       ast_copy_string(tmp->dev, iface, sizeof(tmp->dev));
01176       ast_copy_string(tmp->context, context, sizeof(tmp->context));
01177       tmp->next = NULL;
01178       tmp->obuflen = 0;
01179       tmp->dialtone = 0;
01180       tmp->cpt = 0;
01181       ast_copy_string(tmp->cid_num, cid_num, sizeof(tmp->cid_num));
01182       ast_copy_string(tmp->cid_name, cid_name, sizeof(tmp->cid_name));
01183       tmp->txgain = txgain;
01184       ioctl(tmp->fd, PHONE_PLAY_VOLUME, tmp->txgain);
01185       tmp->rxgain = rxgain;
01186       ioctl(tmp->fd, PHONE_REC_VOLUME, tmp->rxgain);
01187    }
01188    return tmp;
01189 }

static int parse_gain_value ( char *  gain_type,
char *  value 
) [static]

Definition at line 1233 of file chan_phone.c.

References ast_log(), DEFAULT_GAIN, and LOG_ERROR.

Referenced by load_module().

01234 {
01235    float gain;
01236 
01237    /* try to scan number */
01238    if (sscanf(value, "%f", &gain) != 1)
01239    {
01240       ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n",
01241          value, gain_type, config);
01242       return DEFAULT_GAIN;
01243    }
01244 
01245    /* multiplicate gain by 1.0 gain value */ 
01246    gain = gain * (float)DEFAULT_GAIN;
01247 
01248    /* percentage? */
01249    if (value[strlen(value) - 1] == '%')
01250       return (int)(gain / (float)100);
01251 
01252    return (int)gain;
01253 }

static int phone_answer ( struct ast_channel ast  )  [static]

Definition at line 448 of file chan_phone.c.

References ast_log(), ast_setstate(), AST_STATE_UP, phone_pvt::fd, LOG_DEBUG, phone_pvt::mode, MODE_FXO, option_debug, phone_setup(), ast_channel::rings, and ast_channel::tech_pvt.

00449 {
00450    struct phone_pvt *p;
00451    p = ast->tech_pvt;
00452    /* In case it's a LineJack, take it off hook */
00453    if (p->mode == MODE_FXO) {
00454       if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_OFF_HOOK)) 
00455          ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n", ast->name, strerror(errno));
00456       else
00457          ast_log(LOG_DEBUG, "Took linejack off hook\n");
00458    }
00459    phone_setup(ast);
00460    if (option_debug)
00461       ast_log(LOG_DEBUG, "phone_answer(%s)\n", ast->name);
00462    ast->rings = 0;
00463    ast_setstate(ast, AST_STATE_UP);
00464    return 0;
00465 }

static int phone_call ( struct ast_channel ast,
char *  dest,
int  timeout 
) [static]

Definition at line 284 of file chan_phone.c.

References ast_channel::_state, AST_CONTROL_RINGING, ast_log(), ast_queue_control(), ast_setstate(), AST_STATE_DOWN, AST_STATE_RESERVED, AST_STATE_RINGING, ast_strlen_zero(), ast_channel::cid, ast_callerid::cid_name, ast_callerid::cid_num, DEFAULT_CALLER_ID, ast_channel::fds, IXJ_PHONE_RING_START, LOG_DEBUG, LOG_WARNING, phone_pvt::mode, MODE_FXS, option_debug, phone_digit_end(), and ast_channel::tech_pvt.

00285 {
00286    struct phone_pvt *p;
00287 
00288    PHONE_CID cid;
00289    time_t UtcTime;
00290    struct tm tm;
00291    int start;
00292 
00293    time(&UtcTime);
00294    localtime_r(&UtcTime,&tm);
00295 
00296    memset(&cid, 0, sizeof(PHONE_CID));
00297    if(&tm != NULL) {
00298       snprintf(cid.month, sizeof(cid.month), "%02d",(tm.tm_mon + 1));
00299       snprintf(cid.day, sizeof(cid.day),     "%02d", tm.tm_mday);
00300       snprintf(cid.hour, sizeof(cid.hour),   "%02d", tm.tm_hour);
00301       snprintf(cid.min, sizeof(cid.min),     "%02d", tm.tm_min);
00302    }
00303    /* the standard format of ast->callerid is:  "name" <number>, but not always complete */
00304    if (ast_strlen_zero(ast->cid.cid_name))
00305       strcpy(cid.name, DEFAULT_CALLER_ID);
00306    else
00307       ast_copy_string(cid.name, ast->cid.cid_name, sizeof(cid.name));
00308 
00309    if (ast->cid.cid_num) 
00310       ast_copy_string(cid.number, ast->cid.cid_num, sizeof(cid.number));
00311 
00312    p = ast->tech_pvt;
00313 
00314    if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
00315       ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast->name);
00316       return -1;
00317    }
00318    if (option_debug)
00319       ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fds[0]);
00320 
00321    start = IXJ_PHONE_RING_START(cid);
00322    if (start == -1)
00323       return -1;
00324    
00325    if (p->mode == MODE_FXS) {
00326       char *digit = strchr(dest, '/');
00327       if (digit)
00328       {
00329         digit++;
00330         while (*digit)
00331           phone_digit_end(ast, *digit++, 0);
00332       }
00333    }
00334  
00335    ast_setstate(ast, AST_STATE_RINGING);
00336    ast_queue_control(ast, AST_CONTROL_RINGING);
00337    return 0;
00338 }

static void phone_check_exception ( struct phone_pvt i  )  [static]

Definition at line 896 of file chan_phone.c.

References ast_canmatch_extension(), ast_exists_extension(), ast_log(), AST_MAX_EXTENSION, ast_module_ref(), ast_module_unref(), AST_STATE_RING, ast_verbose(), phone_pvt::cid_num, phone_pvt::context, phone_pvt::cpt, phone_pvt::dialtone, phone_pvt::ext, phone_pvt::fd, phone_pvt::lastformat, LOG_DEBUG, phone_pvt::mode, MODE_DIALTONE, MODE_FXS, MODE_IMMEDIATE, MODE_SIGMA, option_debug, and phone_new().

Referenced by do_monitor().

00897 {
00898    int offhook=0;
00899    char digit[2] = {0 , 0};
00900    union telephony_exception phonee;
00901    /* XXX Do something XXX */
00902 #if 0
00903    ast_log(LOG_DEBUG, "Exception!\n");
00904 #endif
00905    phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION);
00906    if (phonee.bits.dtmf_ready)  {
00907       digit[0] = ioctl(i->fd, PHONE_GET_DTMF_ASCII);
00908       if (i->mode == MODE_DIALTONE || i->mode == MODE_FXS || i->mode == MODE_SIGMA) {
00909          ioctl(i->fd, PHONE_PLAY_STOP);
00910          ioctl(i->fd, PHONE_REC_STOP);
00911          ioctl(i->fd, PHONE_CPT_STOP);
00912          i->dialtone = 0;
00913          if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
00914             strncat(i->ext, digit, sizeof(i->ext) - strlen(i->ext) - 1);
00915          if ((i->mode != MODE_FXS ||
00916               !(phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION)) ||
00917               !phonee.bits.dtmf_ready) &&
00918              ast_exists_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
00919             /* It's a valid extension in its context, get moving! */
00920             phone_new(i, AST_STATE_RING, i->context);
00921             /* No need to restart monitor, we are the monitor */
00922          } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
00923             /* There is nothing in the specified extension that can match anymore.
00924                Try the default */
00925             if (ast_exists_extension(NULL, "default", i->ext, 1, i->cid_num)) {
00926                /* Check the default, too... */
00927                phone_new(i, AST_STATE_RING, "default");
00928                /* XXX This should probably be justified better XXX */
00929             }  else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->cid_num)) {
00930                /* It's not a valid extension, give a busy signal */
00931                if (option_debug)
00932                   ast_log(LOG_DEBUG, "%s can't match anything in %s or default\n", i->ext, i->context);
00933                ioctl(i->fd, PHONE_BUSY);
00934                i->cpt = 1;
00935             }
00936          }
00937 #if 0
00938          ast_verbose("Extension is %s\n", i->ext);
00939 #endif
00940       }
00941    }
00942    if (phonee.bits.hookstate) {
00943       offhook = ioctl(i->fd, PHONE_HOOKSTATE);
00944       if (offhook) {
00945          if (i->mode == MODE_IMMEDIATE) {
00946             phone_new(i, AST_STATE_RING, i->context);
00947          } else if (i->mode == MODE_DIALTONE) {
00948             ast_module_ref(ast_module_info->self);
00949             /* Reset the extension */
00950             i->ext[0] = '\0';
00951             /* Play the dialtone */
00952             i->dialtone++;
00953             ioctl(i->fd, PHONE_PLAY_STOP);
00954             ioctl(i->fd, PHONE_PLAY_CODEC, ULAW);
00955             ioctl(i->fd, PHONE_PLAY_START);
00956             i->lastformat = -1;
00957          } else if (i->mode == MODE_SIGMA) {
00958             ast_module_ref(ast_module_info->self);
00959             /* Reset the extension */
00960             i->ext[0] = '\0';
00961             /* Play the dialtone */
00962             i->dialtone++;
00963             ioctl(i->fd, PHONE_DIALTONE);
00964          }
00965       } else {
00966          if (i->dialtone)
00967             ast_module_unref(ast_module_info->self);
00968          memset(i->ext, 0, sizeof(i->ext));
00969          if (i->cpt)
00970          {
00971             ioctl(i->fd, PHONE_CPT_STOP);
00972             i->cpt = 0;
00973          }
00974          ioctl(i->fd, PHONE_PLAY_STOP);
00975          ioctl(i->fd, PHONE_REC_STOP);
00976          i->dialtone = 0;
00977          i->lastformat = -1;
00978       }
00979    }
00980    if (phonee.bits.pstn_ring) {
00981       ast_verbose("Unit is ringing\n");
00982       phone_new(i, AST_STATE_RING, i->context);
00983    }
00984    if (phonee.bits.caller_id)
00985       ast_verbose("We have caller ID\n");
00986    
00987    
00988 }

static int phone_digit_begin ( struct ast_channel ast,
char  digit 
) [static]

Definition at line 236 of file chan_phone.c.

00237 {
00238    /* XXX Modify this callback to let Asterisk support controlling the length of DTMF */
00239    return 0;
00240 }

static int phone_digit_end ( struct ast_channel ast,
char  digit,
unsigned int  duration 
) [static]

Definition at line 242 of file chan_phone.c.

References ast_log(), phone_pvt::fd, phone_pvt::lastformat, LOG_DEBUG, LOG_WARNING, and ast_channel::tech_pvt.

Referenced by phone_call().

00243 {
00244    struct phone_pvt *p;
00245    int outdigit;
00246    p = ast->tech_pvt;
00247    ast_log(LOG_DEBUG, "Dialed %c\n", digit);
00248    switch(digit) {
00249    case '0':
00250    case '1':
00251    case '2':
00252    case '3':
00253    case '4':
00254    case '5':
00255    case '6':
00256    case '7':
00257    case '8':
00258    case '9':
00259       outdigit = digit - '0';
00260       break;
00261    case '*':
00262       outdigit = 11;
00263       break;
00264    case '#':
00265       outdigit = 12;
00266       break;
00267    case 'f':   /*flash*/
00268    case 'F':
00269       ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
00270       usleep(320000);
00271       ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
00272       p->lastformat = -1;
00273       return 0;
00274    default:
00275       ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
00276       return -1;
00277    }
00278    ast_log(LOG_DEBUG, "Dialed %d\n", outdigit);
00279    ioctl(p->fd, PHONE_PLAY_TONE, outdigit);
00280    p->lastformat = -1;
00281    return 0;
00282 }

static struct ast_frame * phone_exception ( struct ast_channel ast  )  [static]

Definition at line 481 of file chan_phone.c.

References ast_channel::_state, AST_CONTROL_ANSWER, AST_FRAME_CONTROL, AST_FRAME_DTMF, AST_FRAME_NULL, ast_log(), ast_setstate(), AST_STATE_RINGING, AST_STATE_UP, ast_verbose(), ast_frame::data, ast_frame::datalen, ast_frame::delivery, phone_pvt::fd, phone_pvt::fr, ast_frame::frametype, LOG_DEBUG, LOG_WARNING, ast_frame::mallocd, phone_pvt::mode, MODE_FXO, ast_frame::offset, option_debug, phone_setup(), ast_frame::samples, ast_frame::src, ast_frame::subclass, and ast_channel::tech_pvt.

00482 {
00483    int res;
00484    union telephony_exception phonee;
00485    struct phone_pvt *p = ast->tech_pvt;
00486    char digit;
00487 
00488    /* Some nice norms */
00489    p->fr.datalen = 0;
00490    p->fr.samples = 0;
00491    p->fr.data =  NULL;
00492    p->fr.src = "Phone";
00493    p->fr.offset = 0;
00494    p->fr.mallocd=0;
00495    p->fr.delivery = ast_tv(0,0);
00496    
00497    phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
00498    if (phonee.bits.dtmf_ready)  {
00499       if (option_debug)
00500          ast_log(LOG_DEBUG, "phone_exception(): DTMF\n");
00501    
00502       /* We've got a digit -- Just handle this nicely and easily */
00503       digit =  ioctl(p->fd, PHONE_GET_DTMF_ASCII);
00504       p->fr.subclass = digit;
00505       p->fr.frametype = AST_FRAME_DTMF;
00506       return &p->fr;
00507    }
00508    if (phonee.bits.hookstate) {
00509       if (option_debug)
00510          ast_log(LOG_DEBUG, "Hookstate changed\n");
00511       res = ioctl(p->fd, PHONE_HOOKSTATE);
00512       /* See if we've gone on hook, if so, notify by returning NULL */
00513       if (option_debug)
00514          ast_log(LOG_DEBUG, "New hookstate: %d\n", res);
00515       if (!res && (p->mode != MODE_FXO))
00516          return NULL;
00517       else {
00518          if (ast->_state == AST_STATE_RINGING) {
00519             /* They've picked up the phone */
00520             p->fr.frametype = AST_FRAME_CONTROL;
00521             p->fr.subclass = AST_CONTROL_ANSWER;
00522             phone_setup(ast);
00523             ast_setstate(ast, AST_STATE_UP);
00524             return &p->fr;
00525          }  else 
00526             ast_log(LOG_WARNING, "Got off hook in weird state %d\n", ast->_state);
00527       }
00528    }
00529 #if 1
00530    if (phonee.bits.pstn_ring)
00531       ast_verbose("Unit is ringing\n");
00532    if (phonee.bits.caller_id) {
00533       ast_verbose("We have caller ID\n");
00534    }
00535    if (phonee.bits.pstn_wink)
00536       ast_verbose("Detected Wink\n");
00537 #endif
00538    /* Strange -- nothing there.. */
00539    p->fr.frametype = AST_FRAME_NULL;
00540    p->fr.subclass = 0;
00541    return &p->fr;
00542 }

static int phone_fixup ( struct ast_channel old,
struct ast_channel new 
) [static]

Definition at line 228 of file chan_phone.c.

References phone_pvt::owner, and ast_channel::tech_pvt.

00229 {
00230    struct phone_pvt *pvt = old->tech_pvt;
00231    if (pvt && pvt->owner == old)
00232       pvt->owner = new;
00233    return 0;
00234 }

static int phone_hangup ( struct ast_channel ast  )  [static]

Definition at line 340 of file chan_phone.c.

References ast_log(), ast_module_unref(), ast_setstate(), AST_STATE_DOWN, ast_verbose(), phone_pvt::cpt, phone_pvt::dialtone, phone_pvt::ext, phone_pvt::fd, phone_pvt::lastformat, phone_pvt::lastinput, LOG_DEBUG, LOG_WARNING, phone_pvt::ministate, phone_pvt::mode, MODE_FXO, phone_pvt::obuflen, option_debug, option_verbose, phone_pvt::owner, restart_monitor(), ast_channel::tech_pvt, and VERBOSE_PREFIX_3.

00341 {
00342    struct phone_pvt *p;
00343    p = ast->tech_pvt;
00344    if (option_debug)
00345       ast_log(LOG_DEBUG, "phone_hangup(%s)\n", ast->name);
00346    if (!ast->tech_pvt) {
00347       ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
00348       return 0;
00349    }
00350    /* XXX Is there anything we can do to really hang up except stop recording? */
00351    ast_setstate(ast, AST_STATE_DOWN);
00352    if (ioctl(p->fd, PHONE_REC_STOP))
00353       ast_log(LOG_WARNING, "Failed to stop recording\n");
00354    if (ioctl(p->fd, PHONE_PLAY_STOP))
00355       ast_log(LOG_WARNING, "Failed to stop playing\n");
00356    if (ioctl(p->fd, PHONE_RING_STOP))
00357       ast_log(LOG_WARNING, "Failed to stop ringing\n");
00358    if (ioctl(p->fd, PHONE_CPT_STOP))
00359       ast_log(LOG_WARNING, "Failed to stop sounds\n");
00360 
00361    /* If it's an FXO, hang them up */
00362    if (p->mode == MODE_FXO) {
00363       if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK)) 
00364          ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",ast->name, strerror(errno));
00365    }
00366 
00367    /* If they're off hook, give a busy signal */
00368    if (ioctl(p->fd, PHONE_HOOKSTATE)) {
00369       if (option_debug)
00370          ast_log(LOG_DEBUG, "Got hunghup, giving busy signal\n");
00371       ioctl(p->fd, PHONE_BUSY);
00372       p->cpt = 1;
00373    }
00374    p->lastformat = -1;
00375    p->lastinput = -1;
00376    p->ministate = 0;
00377    p->obuflen = 0;
00378    p->dialtone = 0;
00379    memset(p->ext, 0, sizeof(p->ext));
00380    ((struct phone_pvt *)(ast->tech_pvt))->owner = NULL;
00381    ast_module_unref(ast_module_info->self);
00382    if (option_verbose > 2) 
00383       ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
00384    ast->tech_pvt = NULL;
00385    ast_setstate(ast, AST_STATE_DOWN);
00386    restart_monitor();
00387    return 0;
00388 }

static int phone_indicate ( struct ast_channel chan,
int  condition,
const void *  data,
size_t  datalen 
) [static]

Definition at line 203 of file chan_phone.c.

References AST_CONTROL_FLASH, AST_CONTROL_HOLD, AST_CONTROL_UNHOLD, ast_log(), ast_moh_start(), ast_moh_stop(), phone_pvt::fd, phone_pvt::lastformat, LOG_DEBUG, LOG_WARNING, and ast_channel::tech_pvt.

00204 {
00205    struct phone_pvt *p = chan->tech_pvt;
00206    int res=-1;
00207    ast_log(LOG_DEBUG, "Requested indication %d on channel %s\n", condition, chan->name);
00208    switch(condition) {
00209       case AST_CONTROL_FLASH:
00210          ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
00211          usleep(320000);
00212          ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
00213          p->lastformat = -1;
00214          res = 0;
00215          break;
00216       case AST_CONTROL_HOLD:
00217          ast_moh_start(chan, data, NULL);
00218          break;
00219       case AST_CONTROL_UNHOLD:
00220          ast_moh_stop(chan);
00221          break;
00222       default:
00223          ast_log(LOG_WARNING, "Condition %d is not supported on channel %s\n", condition, chan->name);
00224    }
00225    return res;
00226 }

static void phone_mini_packet ( struct phone_pvt i  )  [static]

Definition at line 884 of file chan_phone.c.

References ast_log(), phone_pvt::fd, and LOG_WARNING.

Referenced by do_monitor().

00885 {
00886    int res;
00887    char buf[1024];
00888    /* Ignore stuff we read... */
00889    res = read(i->fd, buf, sizeof(buf));
00890    if (res < 1) {
00891       ast_log(LOG_WARNING, "Read returned %d: %s\n", res, strerror(errno));
00892       return;
00893    }
00894 }

static struct ast_channel* phone_new ( struct phone_pvt i,
int  state,
char *  context 
) [static]

Definition at line 821 of file chan_phone.c.

References ast_channel_alloc(), AST_FORMAT_SLINEAR, ast_hangup(), ast_log(), ast_module_ref(), ast_pbx_start(), AST_STATE_DOWN, AST_STATE_RING, ast_strdup, ast_string_field_set, ast_strlen_zero(), phone_pvt::cid_name, phone_pvt::cid_num, phone_pvt::context, phone_pvt::cpt, cur_tech, phone_pvt::dev, phone_pvt::ext, phone_pvt::fd, phone_pvt::language, LOG_WARNING, phone_pvt::mode, MODE_FXS, and phone_pvt::owner.

Referenced by phone_check_exception(), and phone_request().

00822 {
00823    struct ast_channel *tmp;
00824    struct phone_codec_data codec;
00825    tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, "", i->ext, i->context, 0, "Phone/%s", i->dev + 5);
00826    if (tmp) {
00827       tmp->tech = cur_tech;
00828       tmp->fds[0] = i->fd;
00829       /* XXX Switching formats silently causes kernel panics XXX */
00830       if (i->mode == MODE_FXS &&
00831           ioctl(i->fd, PHONE_QUERY_CODEC, &codec) == 0) {
00832          if (codec.type == LINEAR16)
00833             tmp->nativeformats =
00834             tmp->rawreadformat =
00835             tmp->rawwriteformat =
00836             AST_FORMAT_SLINEAR;
00837          else {
00838             tmp->nativeformats =
00839             tmp->rawreadformat =
00840             tmp->rawwriteformat =
00841             prefformat & ~AST_FORMAT_SLINEAR;
00842          }
00843       }
00844       else {
00845          tmp->nativeformats = prefformat;
00846          tmp->rawreadformat = prefformat;
00847          tmp->rawwriteformat = prefformat;
00848       }
00849       /* no need to call ast_setstate: the channel_alloc already did its job */
00850       if (state == AST_STATE_RING)
00851          tmp->rings = 1;
00852       tmp->tech_pvt = i;
00853       ast_copy_string(tmp->context, context, sizeof(tmp->context));
00854       if (!ast_strlen_zero(i->ext))
00855          ast_copy_string(tmp->exten, i->ext, sizeof(tmp->exten));
00856       else
00857          strcpy(tmp->exten, "s");
00858       if (!ast_strlen_zero(i->language))
00859          ast_string_field_set(tmp, language, i->language);
00860 
00861       /* Don't use ast_set_callerid() here because it will
00862        * generate a NewCallerID event before the NewChannel event */
00863       tmp->cid.cid_num = ast_strdup(i->cid_num);
00864       tmp->cid.cid_ani = ast_strdup(i->cid_num);
00865       tmp->cid.cid_name = ast_strdup(i->cid_name);
00866 
00867       i->owner = tmp;
00868       ast_module_ref(ast_module_info->self);
00869       if (state != AST_STATE_DOWN) {
00870          if (state == AST_STATE_RING) {
00871             ioctl(tmp->fds[0], PHONE_RINGBACK);
00872             i->cpt = 1;
00873          }
00874          if (ast_pbx_start(tmp)) {
00875             ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
00876             ast_hangup(tmp);
00877          }
00878       }
00879    } else
00880       ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
00881    return tmp;
00882 }

static struct ast_frame * phone_read ( struct ast_channel ast  )  [static]

Definition at line 544 of file chan_phone.c.

References ast_clear_flag, AST_FLAG_BLOCKING, AST_FORMAT_MAX_AUDIO, AST_FORMAT_PNG, AST_FORMAT_SLINEAR, ast_frame_byteswap_le, AST_FRAME_IMAGE, AST_FRAME_NULL, AST_FRAME_VIDEO, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), phone_pvt::buf, CHECK_BLOCKING, ast_frame::data, ast_frame::datalen, ast_frame::delivery, phone_pvt::fd, phone_pvt::fr, ast_frame::frametype, phone_pvt::lastinput, LOG_WARNING, ast_frame::mallocd, phone_pvt::mode, MODE_FXS, ast_frame::offset, PHONE_MAX_BUF, ast_frame::samples, ast_frame::src, ast_frame::subclass, and ast_channel::tech_pvt.

00545 {
00546    int res;
00547    struct phone_pvt *p = ast->tech_pvt;
00548    
00549 
00550    /* Some nice norms */
00551    p->fr.datalen = 0;
00552    p->fr.samples = 0;
00553    p->fr.data =  NULL;
00554    p->fr.src = "Phone";
00555    p->fr.offset = 0;
00556    p->fr.mallocd=0;
00557    p->fr.delivery = ast_tv(0,0);
00558 
00559    /* Try to read some data... */
00560    CHECK_BLOCKING(ast);
00561    res = read(p->fd, p->buf, PHONE_MAX_BUF);
00562    ast_clear_flag(ast, AST_FLAG_BLOCKING);
00563    if (res < 0) {
00564 #if 0
00565       if (errno == EAGAIN) {
00566          ast_log(LOG_WARNING, "Null frame received\n");
00567          p->fr.frametype = AST_FRAME_NULL;
00568          p->fr.subclass = 0;
00569          return &p->fr;
00570       }
00571 #endif
00572       ast_log(LOG_WARNING, "Error reading: %s\n", strerror(errno));
00573       return NULL;
00574    }
00575    p->fr.data = p->buf;
00576    if (p->mode != MODE_FXS)
00577    switch(p->buf[0] & 0x3) {
00578    case '0':
00579    case '1':
00580       /* Normal */
00581       break;
00582    case '2':
00583    case '3':
00584       /* VAD/CNG, only send two words */
00585       res = 4;
00586       break;
00587    }
00588    p->fr.samples = 240;
00589    p->fr.datalen = res;
00590    p->fr.frametype = p->lastinput <= AST_FORMAT_MAX_AUDIO ?
00591                           AST_FRAME_VOICE : 
00592            p->lastinput <= AST_FORMAT_PNG ? AST_FRAME_IMAGE 
00593            : AST_FRAME_VIDEO;
00594    p->fr.subclass = p->lastinput;
00595    p->fr.offset = AST_FRIENDLY_OFFSET;
00596    /* Byteswap from little-endian to native-endian */
00597    if (p->fr.subclass == AST_FORMAT_SLINEAR)
00598       ast_frame_byteswap_le(&p->fr);
00599    return &p->fr;
00600 }

static struct ast_channel * phone_request ( const char *  type,
int  format,
void *  data,
int *  cause 
) [static]

Definition at line 1191 of file chan_phone.c.

References AST_CAUSE_BUSY, AST_FORMAT_G723_1, AST_FORMAT_SLINEAR, AST_FORMAT_ULAW, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), AST_STATE_DOWN, phone_pvt::context, phone_pvt::dev, iflist, LOG_ERROR, LOG_NOTICE, phone_pvt::mode, MODE_FXS, name, phone_pvt::next, phone_pvt::owner, phone_new(), and restart_monitor().

01192 {
01193    int oldformat;
01194    struct phone_pvt *p;
01195    struct ast_channel *tmp = NULL;
01196    char *name = data;
01197 
01198    /* Search for an unowned channel */
01199    if (ast_mutex_lock(&iflock)) {
01200       ast_log(LOG_ERROR, "Unable to lock interface list???\n");
01201       return NULL;
01202    }
01203    p = iflist;
01204    while(p) {
01205       if (p->mode == MODE_FXS ||
01206           format & (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW)) {
01207           size_t length = strlen(p->dev + 5);
01208          if (strncmp(name, p->dev + 5, length) == 0 &&
01209              !isalnum(name[length])) {
01210              if (!p->owner) {
01211                      tmp = phone_new(p, AST_STATE_DOWN, p->context);
01212                      break;
01213                 } else
01214                      *cause = AST_CAUSE_BUSY;
01215             }
01216       }
01217       p = p->next;
01218    }
01219    ast_mutex_unlock(&iflock);
01220    restart_monitor();
01221    if (tmp == NULL) {
01222       oldformat = format;
01223       format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
01224       if (!format) {
01225          ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
01226          return NULL;
01227       }
01228    }
01229    return tmp;
01230 }

static int phone_send_text ( struct ast_channel ast,
const char *  text 
) [static]

Definition at line 636 of file chan_phone.c.

References phone_write_buf(), and ast_channel::tech_pvt.

00637 {
00638     int length = strlen(text);
00639     return phone_write_buf(ast->tech_pvt, text, length, length, 0) == 
00640            length ? 0 : -1;
00641 }

static int phone_setup ( struct ast_channel ast  )  [static]

Definition at line 390 of file chan_phone.c.

References AST_FORMAT_G723_1, AST_FORMAT_SLINEAR, AST_FORMAT_ULAW, ast_getformatname(), ast_log(), phone_pvt::fd, phone_pvt::lastinput, LOG_WARNING, phone_pvt::mode, MODE_FXS, ast_channel::rawreadformat, and ast_channel::tech_pvt.

Referenced by phone_answer(), phone_exception(), and phone_write().

00391 {
00392    struct phone_pvt *p;
00393    p = ast->tech_pvt;
00394    ioctl(p->fd, PHONE_CPT_STOP);
00395    /* Nothing to answering really, just start recording */
00396    if (ast->rawreadformat == AST_FORMAT_G723_1) {
00397       /* Prefer g723 */
00398       ioctl(p->fd, PHONE_REC_STOP);
00399       if (p->lastinput != AST_FORMAT_G723_1) {
00400          p->lastinput = AST_FORMAT_G723_1;
00401          if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
00402             ast_log(LOG_WARNING, "Failed to set codec to g723.1\n");
00403             return -1;
00404          }
00405       }
00406    } else if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
00407       ioctl(p->fd, PHONE_REC_STOP);
00408       if (p->lastinput != AST_FORMAT_SLINEAR) {
00409          p->lastinput = AST_FORMAT_SLINEAR;
00410          if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
00411             ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
00412             return -1;
00413          }
00414       }
00415    } else if (ast->rawreadformat == AST_FORMAT_ULAW) {
00416       ioctl(p->fd, PHONE_REC_STOP);
00417       if (p->lastinput != AST_FORMAT_ULAW) {
00418          p->lastinput = AST_FORMAT_ULAW;
00419          if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
00420             ast_log(LOG_WARNING, "Failed to set codec to uLaw\n");
00421             return -1;
00422          }
00423       }
00424    } else if (p->mode == MODE_FXS) {
00425       ioctl(p->fd, PHONE_REC_STOP);
00426       if (p->lastinput != ast->rawreadformat) {
00427          p->lastinput = ast->rawreadformat;
00428          if (ioctl(p->fd, PHONE_REC_CODEC, ast->rawreadformat)) {
00429             ast_log(LOG_WARNING, "Failed to set codec to %d\n", 
00430                ast->rawreadformat);
00431             return -1;
00432          }
00433       }
00434    } else {
00435       ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast->rawreadformat));
00436       return -1;
00437    }
00438    if (ioctl(p->fd, PHONE_REC_START)) {
00439       ast_log(LOG_WARNING, "Failed to start recording\n");
00440       return -1;
00441    }
00442    /* set the DTMF times (the default is too short) */
00443    ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
00444    ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
00445    return 0;
00446 }

static int phone_write ( struct ast_channel ast,
struct ast_frame frame 
) [static]

Definition at line 643 of file chan_phone.c.

References ast_channel::_state, AST_FORMAT_G723_1, AST_FORMAT_SLINEAR, AST_FORMAT_ULAW, AST_FRAME_IMAGE, AST_FRAME_VOICE, ast_log(), ast_setstate(), AST_STATE_UP, ast_frame::data, ast_frame::datalen, phone_pvt::fd, ast_frame::frametype, phone_pvt::lastformat, phone_pvt::lastinput, LOG_WARNING, phone_pvt::mode, MODE_FXS, phone_pvt::obuflen, phone_setup(), phone_write_buf(), phone_pvt::silencesupression, ast_frame::subclass, and ast_channel::tech_pvt.

00644 {
00645    struct phone_pvt *p = ast->tech_pvt;
00646    int res;
00647    int maxfr=0;
00648    char *pos;
00649    int sofar;
00650    int expected;
00651    int codecset = 0;
00652    char tmpbuf[4];
00653    /* Write a frame of (presumably voice) data */
00654    if (frame->frametype != AST_FRAME_VOICE && p->mode != MODE_FXS) {
00655       if (frame->frametype != AST_FRAME_IMAGE)
00656          ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
00657       return 0;
00658    }
00659    if (!(frame->subclass &
00660       (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW)) && 
00661        p->mode != MODE_FXS) {
00662       ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
00663       return -1;
00664    }
00665 #if 0
00666    /* If we're not in up mode, go into up mode now */
00667    if (ast->_state != AST_STATE_UP) {
00668       ast_setstate(ast, AST_STATE_UP);
00669       phone_setup(ast);
00670    }
00671 #else
00672    if (ast->_state != AST_STATE_UP) {
00673       /* Don't try tos end audio on-hook */
00674       return 0;
00675    }
00676 #endif   
00677    if (frame->subclass == AST_FORMAT_G723_1) {
00678       if (p->lastformat != AST_FORMAT_G723_1) {
00679          ioctl(p->fd, PHONE_PLAY_STOP);
00680          ioctl(p->fd, PHONE_REC_STOP);
00681          if (ioctl(p->fd, PHONE_PLAY_CODEC, G723_63)) {
00682             ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
00683             return -1;
00684          }
00685          if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
00686             ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
00687             return -1;
00688          }
00689          p->lastformat = AST_FORMAT_G723_1;
00690          p->lastinput = AST_FORMAT_G723_1;
00691          /* Reset output buffer */
00692          p->obuflen = 0;
00693          codecset = 1;
00694       }
00695       if (frame->datalen > 24) {
00696          ast_log(LOG_WARNING, "Frame size too large for G.723.1 (%d bytes)\n", frame->datalen);
00697          return -1;
00698       }
00699       maxfr = 24;
00700    } else if (frame->subclass == AST_FORMAT_SLINEAR) {
00701       if (p->lastformat != AST_FORMAT_SLINEAR) {
00702          ioctl(p->fd, PHONE_PLAY_STOP);
00703          ioctl(p->fd, PHONE_REC_STOP);
00704          if (ioctl(p->fd, PHONE_PLAY_CODEC, LINEAR16)) {
00705             ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
00706             return -1;
00707          }
00708          if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
00709             ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
00710             return -1;
00711          }
00712          p->lastformat = AST_FORMAT_SLINEAR;
00713          p->lastinput = AST_FORMAT_SLINEAR;
00714          codecset = 1;
00715          /* Reset output buffer */
00716          p->obuflen = 0;
00717       }
00718       maxfr = 480;
00719    } else if (frame->subclass == AST_FORMAT_ULAW) {
00720       if (p->lastformat != AST_FORMAT_ULAW) {
00721          ioctl(p->fd, PHONE_PLAY_STOP);
00722          ioctl(p->fd, PHONE_REC_STOP);
00723          if (ioctl(p->fd, PHONE_PLAY_CODEC, ULAW)) {
00724             ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
00725             return -1;
00726          }
00727          if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
00728             ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
00729             return -1;
00730          }
00731          p->lastformat = AST_FORMAT_ULAW;
00732          p->lastinput = AST_FORMAT_ULAW;
00733          codecset = 1;
00734          /* Reset output buffer */
00735          p->obuflen = 0;
00736       }
00737       maxfr = 240;
00738    } else {
00739       if (p->lastformat != frame->subclass) {
00740          ioctl(p->fd, PHONE_PLAY_STOP);
00741          ioctl(p->fd, PHONE_REC_STOP);
00742          if (ioctl(p->fd, PHONE_PLAY_CODEC, frame->subclass)) {
00743             ast_log(LOG_WARNING, "Unable to set %d mode\n",
00744                frame->subclass);
00745             return -1;
00746          }
00747          if (ioctl(p->fd, PHONE_REC_CODEC, frame->subclass)) {
00748             ast_log(LOG_WARNING, "Unable to set %d mode\n",
00749                frame->subclass);
00750             return -1;
00751          }
00752          p->lastformat = frame->subclass;
00753          p->lastinput = frame->subclass;
00754          codecset = 1;
00755          /* Reset output buffer */
00756          p->obuflen = 0;
00757       }
00758       maxfr = 480;
00759    }
00760    if (codecset) {
00761       ioctl(p->fd, PHONE_REC_DEPTH, 3);
00762       ioctl(p->fd, PHONE_PLAY_DEPTH, 3);
00763       if (ioctl(p->fd, PHONE_PLAY_START)) {
00764          ast_log(LOG_WARNING, "Failed to start playback\n");
00765          return -1;
00766       }
00767       if (ioctl(p->fd, PHONE_REC_START)) {
00768          ast_log(LOG_WARNING, "Failed to start recording\n");
00769          return -1;
00770       }
00771    }
00772    /* If we get here, we have a frame of Appropriate data */
00773    sofar = 0;
00774    pos = frame->data;
00775    while(sofar < frame->datalen) {
00776       /* Write in no more than maxfr sized frames */
00777       expected = frame->datalen - sofar;
00778       if (maxfr < expected)
00779          expected = maxfr;
00780       /* XXX Internet Phone Jack does not handle the 4-byte VAD frame properly! XXX 
00781          we have to pad it to 24 bytes still.  */
00782       if (frame->datalen == 4) {
00783          if (p->silencesupression) {
00784             memset(tmpbuf + 4, 0, sizeof(tmpbuf) - 4);
00785             memcpy(tmpbuf, frame->data, 4);
00786             expected = 24;
00787             res = phone_write_buf(p, tmpbuf, expected, maxfr, 0);
00788          }
00789          res = 4;
00790          expected=4;
00791       } else {
00792          int swap = 0;
00793 #if __BYTE_ORDER == __BIG_ENDIAN
00794          if (frame->subclass == AST_FORMAT_SLINEAR)
00795             swap = 1; /* Swap big-endian samples to little-endian as we copy */
00796 #endif
00797          res = phone_write_buf(p, pos, expected, maxfr, swap);
00798       }
00799       if (res != expected) {
00800          if ((errno != EAGAIN) && (errno != EINTR)) {
00801             if (res < 0) 
00802                ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
00803    /*
00804     * Card is in non-blocking mode now and it works well now, but there are
00805     * lot of messages like this. So, this message is temporarily disabled.
00806     */
00807 #if 0
00808             else
00809                ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
00810 #endif
00811             return -1;
00812          } else /* Pretend it worked */
00813             res = expected;
00814       }
00815       sofar += res;
00816       pos += res;
00817    }
00818    return 0;
00819 }

static int phone_write_buf ( struct phone_pvt p,
const char *  buf,
int  len,
int  frlen,
int  swap 
) [static]

Definition at line 602 of file chan_phone.c.

References ast_log(), ast_swapcopy_samples(), phone_pvt::fd, LOG_WARNING, phone_pvt::obuf, phone_pvt::obuflen, and space.

Referenced by phone_send_text(), and phone_write().

00603 {
00604    int res;
00605    /* Store as much of the buffer as we can, then write fixed frames */
00606    int space = sizeof(p->obuf) - p->obuflen;
00607    /* Make sure we have enough buffer space to store the frame */
00608    if (space < len)
00609       len = space;
00610    if (swap)
00611       ast_swapcopy_samples(p->obuf+p->obuflen, buf, len/2);
00612    else
00613       memcpy(p->obuf + p->obuflen, buf, len);
00614    p->obuflen += len;
00615    while(p->obuflen > frlen) {
00616       res = write(p->fd, p->obuf, frlen);
00617       if (res != frlen) {
00618          if (res < 1) {
00619 /*
00620  * Card is in non-blocking mode now and it works well now, but there are
00621  * lot of messages like this. So, this message is temporarily disabled.
00622  */
00623             return 0;
00624          } else {
00625             ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
00626          }
00627       }
00628       p->obuflen -= frlen;
00629       /* Move memory if necessary */
00630       if (p->obuflen) 
00631          memmove(p->obuf, p->obuf + frlen, p->obuflen);
00632    }
00633    return len;
00634 }

static int restart_monitor ( void   )  [static]

Definition at line 1094 of file chan_phone.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_pthread_create_background, AST_PTHREADT_NULL, AST_PTHREADT_STOP, do_monitor(), LOG_ERROR, and LOG_WARNING.

01095 {
01096    /* If we're supposed to be stopped -- stay stopped */
01097    if (monitor_thread == AST_PTHREADT_STOP)
01098       return 0;
01099    if (ast_mutex_lock(&monlock)) {
01100       ast_log(LOG_WARNING, "Unable to lock monitor\n");
01101       return -1;
01102    }
01103    if (monitor_thread == pthread_self()) {
01104       ast_mutex_unlock(&monlock);
01105       ast_log(LOG_WARNING, "Cannot kill myself\n");
01106       return -1;
01107    }
01108    if (monitor_thread != AST_PTHREADT_NULL) {
01109       if (ast_mutex_lock(&iflock)) {
01110          ast_mutex_unlock(&monlock);
01111          ast_log(LOG_WARNING, "Unable to lock the interface list\n");
01112          return -1;
01113       }
01114       monitor = 0;
01115       while (pthread_kill(monitor_thread, SIGURG) == 0)
01116          sched_yield();
01117       pthread_join(monitor_thread, NULL);
01118       ast_mutex_unlock(&iflock);
01119    }
01120    monitor = 1;
01121    /* Start a new monitor */
01122    if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
01123       ast_mutex_unlock(&monlock);
01124       ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
01125       return -1;
01126    }
01127    ast_mutex_unlock(&monlock);
01128    return 0;
01129 }

static int unload_module ( void   )  [static]

Definition at line 1310 of file chan_phone.c.

References __unload_module().

01311 {
01312    return __unload_module();
01313 }


Variable Documentation

char cid_name[AST_MAX_EXTENSION] [static]

Definition at line 151 of file chan_phone.c.

char cid_num[AST_MAX_EXTENSION] [static]

Definition at line 150 of file chan_phone.c.

const char config[] = "phone.conf" [static]

Definition at line 85 of file chan_phone.c.

char context[AST_MAX_EXTENSION] = "default" [static]

Definition at line 88 of file chan_phone.c.

struct ast_channel_tech* cur_tech [static]

Definition at line 201 of file chan_phone.c.

Referenced by __unload_module(), load_module(), and phone_new().

int echocancel = AEC_OFF [static]

Definition at line 93 of file chan_phone.c.

struct phone_pvt * iflist [static]

char language[MAX_LANGUAGE] = "" [static]

Definition at line 91 of file chan_phone.c.

unsigned int monitor [static]

Definition at line 107 of file chan_phone.c.

Referenced by ast_monitor_start().

pthread_t monitor_thread = AST_PTHREADT_NULL [static]

Definition at line 111 of file chan_phone.c.

struct ast_channel_tech phone_tech [static]

Definition at line 166 of file chan_phone.c.

Referenced by load_module().

struct ast_channel_tech phone_tech_fxs [static]

Definition at line 183 of file chan_phone.c.

Referenced by load_module().

int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW [static]

Definition at line 97 of file chan_phone.c.

int silencesupression = 0 [static]

Definition at line 95 of file chan_phone.c.

const char tdesc[] = "Standard Linux Telephony API Driver" [static]

Definition at line 84 of file chan_phone.c.


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