Fri Jul 3 20:37:21 2009

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 void __reg_module (void)
static int __unload_module (void)
static void __unreg_module (void)
static void * do_monitor (void *data)
static int load_module (void)
static struct phone_pvt * mkif (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_frame * phone_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_channel * phone_new (struct phone_pvt *i, int state, char *context)
static struct ast_frame * phone_read (struct ast_channel *ast)
static struct ast_channel * phone_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)
 Start the channel monitor thread.
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "Linux Telephony API Support" , .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 const struct
ast_module_info * 
ast_module_info = &__mod_info
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_tech * cur_tech
static int echocancel = AEC_OFF
static struct phone_pvt * iflist
static ast_mutex_t iflock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER )
static char language [MAX_LANGUAGE] = ""
static unsigned int monitor
static pthread_t monitor_thread = AST_PTHREADT_NULL
static ast_mutex_t monlock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER )
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 83 of file chan_phone.c.

Referenced by phone_call().

#define DEFAULT_GAIN   0x100

Definition at line 85 of file chan_phone.c.

Referenced by load_module(), and parse_gain_value().

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

Definition at line 80 of file chan_phone.c.

Referenced by phone_call().

#define MODE_DIALTONE   1

Definition at line 121 of file chan_phone.c.

Referenced by load_module(), and phone_check_exception().

#define MODE_FXO   3

Definition at line 123 of file chan_phone.c.

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

#define MODE_FXS   4

#define MODE_IMMEDIATE   2

Definition at line 122 of file chan_phone.c.

Referenced by load_module(), and phone_check_exception().

#define MODE_SIGMA   5

Definition at line 125 of file chan_phone.c.

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

#define PHONE_MAX_BUF   480

Definition at line 84 of file chan_phone.c.

Referenced by phone_read().

#define QNDRV_VER   100

Definition at line 70 of file chan_phone.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 1429 of file chan_phone.c.

static int __unload_module ( void   )  [static]

Definition at line 1259 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, phone_pvt::fd, free, iflist, iflock, LOG_WARNING, monlock, phone_pvt::next, and phone_pvt::owner.

Referenced by load_module(), and unload_module().

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

static void __unreg_module ( void   )  [static]

Definition at line 1429 of file chan_phone.c.

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

Definition at line 994 of file chan_phone.c.

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

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

static int load_module ( void   )  [static]

Definition at line 1320 of file chan_phone.c.

References __unload_module(), ast_callerid_split(), ast_channel_register(), ast_config_destroy(), ast_config_load(), ast_copy_string(), AST_FORMAT_G723_1, AST_FORMAT_SLINEAR, AST_FORMAT_ULAW, ast_log(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, ast_mutex_lock(), ast_mutex_unlock(), ast_true(), ast_variable_browse(), ast_channel_tech::capabilities, DEFAULT_GAIN, iflist, iflock, 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(), restart_monitor(), phone_pvt::rxgain, phone_pvt::txgain, and ast_variable::value.

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

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

Definition at line 1135 of file chan_phone.c.

References ast_copy_string(), ast_log(), phone_pvt::cid_name, phone_pvt::cid_num, phone_pvt::context, phone_pvt::cpt, phone_pvt::dev, phone_pvt::dialtone, errno, 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().

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

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

Definition at line 1237 of file chan_phone.c.

References ast_log(), DEFAULT_GAIN, and LOG_ERROR.

Referenced by load_module().

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

static int phone_answer ( struct ast_channel *  ast  )  [static]

Definition at line 454 of file chan_phone.c.

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

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

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

Definition at line 290 of file chan_phone.c.

References ast_channel::_state, AST_CONTROL_RINGING, ast_copy_string(), ast_localtime(), 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, ast_channel::name, option_debug, phone_digit_end(), and ast_channel::tech_pvt.

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

static void phone_check_exception ( struct phone_pvt *  i  )  [static]

Definition at line 900 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().

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

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

Definition at line 242 of file chan_phone.c.

00243 {
00244    /* XXX Modify this callback to let Asterisk support controlling the length of DTMF */
00245    return 0;
00246 }

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

Definition at line 248 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().

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

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

Definition at line 487 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_tv(), 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.

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

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

Definition at line 234 of file chan_phone.c.

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

00235 {
00236    struct phone_pvt *pvt = old->tech_pvt;
00237    if (pvt && pvt->owner == old)
00238       pvt->owner = new;
00239    return 0;
00240 }

static int phone_hangup ( struct ast_channel *  ast  )  [static]

Definition at line 346 of file chan_phone.c.

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

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

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

Definition at line 206 of file chan_phone.c.

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

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

static void phone_mini_packet ( struct phone_pvt *  i  )  [static]

Definition at line 888 of file chan_phone.c.

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

Referenced by do_monitor().

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

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

Definition at line 827 of file chan_phone.c.

References ast_channel_alloc(), ast_copy_string(), 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, 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().

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

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

Definition at line 550 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(), ast_tv(), phone_pvt::buf, CHECK_BLOCKING, ast_frame::data, ast_frame::datalen, ast_frame::delivery, errno, 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.

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

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

Definition at line 1195 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, iflock, LOG_ERROR, LOG_NOTICE, phone_pvt::mode, MODE_FXS, name, phone_pvt::next, phone_pvt::owner, phone_new(), and restart_monitor().

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

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

Definition at line 642 of file chan_phone.c.

References phone_write_buf(), and ast_channel::tech_pvt.

00643 {
00644     int length = strlen(text);
00645     return phone_write_buf(ast->tech_pvt, text, length, length, 0) == 
00646            length ? 0 : -1;
00647 }

static int phone_setup ( struct ast_channel *  ast  )  [static]

Definition at line 396 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().

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

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

Definition at line 649 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, errno, 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.

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

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

Definition at line 608 of file chan_phone.c.

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

Referenced by phone_send_text(), and phone_write().

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

static int restart_monitor ( void   )  [static]

Start the channel monitor thread.

Definition at line 2734 of file chan_ooh323.c.

02735 {
02736    pthread_attr_t attr;
02737 
02738    /* If we're supposed to be stopped -- stay stopped */
02739    if (monitor_thread == AST_PTHREADT_STOP)
02740       return 0;
02741    if (ast_mutex_lock(&monlock)) {
02742       ast_log(LOG_WARNING, "Unable to lock monitor\n");
02743       return -1;
02744    }
02745    if (monitor_thread == pthread_self()) {
02746       ast_mutex_unlock(&monlock);
02747       ast_log(LOG_WARNING, "Cannot kill myself\n");
02748       return -1;
02749    }
02750    if (monitor_thread != AST_PTHREADT_NULL) {
02751       /* Wake up the thread */
02752       pthread_kill(monitor_thread, SIGURG);
02753    } 
02754    else {
02755       pthread_attr_init(&attr);
02756       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
02757       /* Start a new monitor */
02758       if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
02759          ast_mutex_unlock(&monlock);
02760          ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
02761          return -1;
02762       }
02763    }
02764    ast_mutex_unlock(&monlock);
02765    return 0;
02766 }

static int unload_module ( void   )  [static]

Definition at line 1315 of file chan_phone.c.

References __unload_module().

01316 {
01317    return __unload_module();
01318 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "Linux Telephony API Support" , .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 1429 of file chan_phone.c.

const struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 1429 of file chan_phone.c.

char cid_name[AST_MAX_EXTENSION] [static]

Definition at line 154 of file chan_phone.c.

char cid_num[AST_MAX_EXTENSION] [static]

Definition at line 153 of file chan_phone.c.

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

Definition at line 88 of file chan_phone.c.

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

Definition at line 91 of file chan_phone.c.

struct ast_channel_tech* cur_tech [static]

Definition at line 204 of file chan_phone.c.

int echocancel = AEC_OFF [static]

Definition at line 96 of file chan_phone.c.

struct phone_pvt * iflist [static]

ast_mutex_t iflock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) [static]

Definition at line 103 of file chan_phone.c.

Referenced by __unload_module(), do_monitor(), load_module(), and phone_request().

char language[MAX_LANGUAGE] = "" [static]

Definition at line 94 of file chan_phone.c.

unsigned int monitor [static]

Definition at line 110 of file chan_phone.c.

Referenced by ast_monitor_start().

pthread_t monitor_thread = AST_PTHREADT_NULL [static]

Definition at line 114 of file chan_phone.c.

ast_mutex_t monlock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) [static]

Definition at line 107 of file chan_phone.c.

Referenced by __unload_module().

struct ast_channel_tech phone_tech [static]

Definition at line 169 of file chan_phone.c.

Definition at line 186 of file chan_phone.c.

int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW [static]

Definition at line 100 of file chan_phone.c.

int silencesupression = 0 [static]

Definition at line 98 of file chan_phone.c.

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

Definition at line 87 of file chan_phone.c.


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