Newer
Older
* Asterisk -- An open source telephony toolkit.
* Copyright (C) 1999 - 2005, Digium, Inc.
* Mark Spencer <markster@digium.com>
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
*
* \brief Generic Linux Telephony Interface driver
*
* \author Mark Spencer <markster@digium.com>
*
/*! \li \ref chan_phone.c uses the configuration file \ref phone.conf
* \addtogroup configuration_file
*/
/*! \page phone.conf phone.conf
* \verbinclude phone.conf.sample
*/
Kevin P. Fleming
committed
/*** MODULEINFO
<depend>ixjuser</depend>
<support_level>extended</support_level>
Kevin P. Fleming
committed
***/
Kevin P. Fleming
committed
#include "asterisk.h"
ASTERISK_REGISTER_FILE()
Kevin P. Fleming
committed
#include <sys/socket.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#ifdef HAVE_LINUX_COMPILER_H
#include <linux/compiler.h>
#endif
#include <linux/telephony.h>
/* Still use some IXJ specific stuff */
Kevin P. Fleming
committed
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/callerid.h"
#include "asterisk/causes.h"
#include "asterisk/stringfields.h"
Kevin P. Fleming
committed
#include "asterisk/musiconhold.h"
#include "asterisk/format_cache.h"
#include "asterisk/format_compatibility.h"
Michiel van Baak
committed
#include "chan_phone.h"
#ifdef QTI_PHONEJACK_TJ_PCI /* check for the newer quicknet driver v.3.1.0 which has this symbol */
#define QNDRV_VER 310
#else
#define QNDRV_VER 100
#endif
#if QNDRV_VER > 100
#ifdef __linux__
#define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
#else /* FreeBSD and others */
#define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, x);
#endif /* __linux__ */
#else /* older driver */
#define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
#endif
#define DEFAULT_CALLER_ID "Unknown"
static const char tdesc[] = "Standard Linux Telephony API Driver";
static const char config[] = "phone.conf";
/* Default context for dialtone mode */
static char context[AST_MAX_EXTENSION] = "default";
/* Default language */
static char language[MAX_LANGUAGE] = "";
static int echocancel = AEC_OFF;
static int silencesupression = 0;
static struct ast_format_cap *prefcap;
/* Protect the interface list (of phone_pvt's) */
/* Protect the monitoring thread, so only one process can kill or start it, and not
when it's doing something critical. */
AST_MUTEX_DEFINE_STATIC(monlock);
/* Boolean value whether the monitoring thread shall continue. */
static unsigned int monitor;
/* This is the thread for the monitor which checks for input on the channels
which are not currently in use. */
static pthread_t monitor_thread = AST_PTHREADT_NULL;
static int restart_monitor(void);
/* The private structures of the Phone Jack channels are linked for
selecting outgoing channels */
#define MODE_DIALTONE 1
#define MODE_IMMEDIATE 2
#define MODE_FXO 3
#define MODE_SIGMA 5
static struct phone_pvt {
int fd; /* Raw file descriptor for this device */
struct ast_channel *owner; /* Channel we belong to, possibly NULL */
int mode; /* Is this in the */
struct ast_format *lastformat; /* Last output format */
struct ast_format *lastinput; /* Last input format */
int ministate; /* Miniature state, for dialtone mode */
char dev[256]; /* Device name */
struct phone_pvt *next; /* Next channel in list */
struct ast_frame fr; /* Frame */
char offset[AST_FRIENDLY_OFFSET];
char buf[PHONE_MAX_BUF]; /* Static buffer for reading frames */
int txgain, rxgain; /* gain control for playing, recording */
/* 0x100 - 1.0, 0x200 - 2.0, 0x80 - 0.5 */
int cpt; /* Call Progress Tone playing? */
int silencesupression;
char context[AST_MAX_EXTENSION];
char cid_num[AST_MAX_EXTENSION];
char cid_name[AST_MAX_EXTENSION];
static char cid_num[AST_MAX_EXTENSION];
static char cid_name[AST_MAX_EXTENSION];
static struct ast_channel *phone_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
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 int phone_call(struct ast_channel *ast, const char *dest, int timeout);
static int phone_hangup(struct ast_channel *ast);
static int phone_answer(struct ast_channel *ast);
static struct ast_frame *phone_read(struct ast_channel *ast);
static int phone_write(struct ast_channel *ast, struct ast_frame *frame);
static struct ast_frame *phone_exception(struct ast_channel *ast);
Mark Spencer
committed
static int phone_send_text(struct ast_channel *ast, const char *text);
static int phone_fixup(struct ast_channel *old, struct ast_channel *new);
Kevin P. Fleming
committed
static int phone_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
static struct ast_channel_tech phone_tech = {
.description = tdesc,
.requester = phone_request,
.send_digit_begin = phone_digit_begin,
.send_digit_end = phone_digit_end,
.call = phone_call,
.hangup = phone_hangup,
.answer = phone_answer,
.read = phone_read,
.write = phone_write,
.exception = phone_exception,
.indicate = phone_indicate,
};
static struct ast_channel_tech phone_tech_fxs = {
.description = tdesc,
.requester = phone_request,
.send_digit_begin = phone_digit_begin,
.send_digit_end = phone_digit_end,
.call = phone_call,
.hangup = phone_hangup,
.answer = phone_answer,
.read = phone_read,
.write = phone_write,
.exception = phone_exception,
.write_video = phone_write,
.send_text = phone_send_text,
.indicate = phone_indicate,
};
static struct ast_channel_tech *cur_tech;
Kevin P. Fleming
committed
static int phone_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
struct phone_pvt *p = ast_channel_tech_pvt(chan);
ast_debug(1, "Requested indication %d on channel %s\n", condition, ast_channel_name(chan));
switch(condition) {
case AST_CONTROL_FLASH:
ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
usleep(320000);
ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
ao2_cleanup(p->lastformat);
p->lastformat = NULL;
res = 0;
break;
case AST_CONTROL_HOLD:
ast_moh_start(chan, data, NULL);
break;
case AST_CONTROL_UNHOLD:
ast_moh_stop(chan);
break;
case AST_CONTROL_SRCUPDATE:
res = 0;
break;
case AST_CONTROL_PVT_CAUSE_CODE:
break;
ast_log(LOG_WARNING, "Condition %d is not supported on channel %s\n", condition, ast_channel_name(chan));
}
return res;
}
static int phone_fixup(struct ast_channel *old, struct ast_channel *new)
{
struct phone_pvt *pvt = ast_channel_tech_pvt(old);
if (pvt && pvt->owner == old)
pvt->owner = new;
return 0;
}
static int phone_digit_begin(struct ast_channel *chan, char digit)
{
/* XXX Modify this callback to let Asterisk support controlling the length of DTMF */
return 0;
}
static int phone_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
p = ast_channel_tech_pvt(ast);
ast_debug(1, "Dialed %c\n", digit);
switch(digit) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
case '*':
outdigit = 11;
break;
case '#':
outdigit = 12;
break;
case 'F':
ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
usleep(320000);
ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
ao2_cleanup(p->lastformat);
p->lastformat = NULL;
default:
ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
return -1;
}
ast_debug(1, "Dialed %d\n", outdigit);
ao2_cleanup(p->lastformat);
p->lastformat = NULL;
static int phone_call(struct ast_channel *ast, const char *dest, int timeout)
Tilghman Lesher
committed
struct timeval UtcTime = ast_tvnow();
struct ast_tm tm;
ast_localtime(&UtcTime, &tm, NULL);
memset(&cid, 0, sizeof(PHONE_CID));
snprintf(cid.month, sizeof(cid.month), "%02d",(tm.tm_mon + 1));
snprintf(cid.day, sizeof(cid.day), "%02d", tm.tm_mday);
snprintf(cid.hour, sizeof(cid.hour), "%02d", tm.tm_hour);
snprintf(cid.min, sizeof(cid.min), "%02d", tm.tm_min);
/* the standard format of ast->callerid is: "name" <number>, but not always complete */
if (!ast_channel_connected(ast)->id.name.valid
|| ast_strlen_zero(ast_channel_connected(ast)->id.name.str)) {
strcpy(cid.name, DEFAULT_CALLER_ID);
ast_copy_string(cid.name, ast_channel_connected(ast)->id.name.str, sizeof(cid.name));
if (ast_channel_connected(ast)->id.number.valid && ast_channel_connected(ast)->id.number.str) {
ast_copy_string(cid.number, ast_channel_connected(ast)->id.number.str, sizeof(cid.number));
p = ast_channel_tech_pvt(ast);
if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
ast_debug(1, "Ringing %s on %s (%d)\n", dest, ast_channel_name(ast), ast_channel_fd(ast, 0));
start = IXJ_PHONE_RING_START(cid);
if (start == -1)
return -1;
if (p->mode == MODE_FXS) {
const char *digit = strchr(dest, '/');
if (digit)
{
digit++;
while (*digit)
phone_digit_end(ast, *digit++, 0);
}
}
ast_setstate(ast, AST_STATE_RINGING);
Mark Spencer
committed
ast_queue_control(ast, AST_CONTROL_RINGING);
return 0;
}
static int phone_hangup(struct ast_channel *ast)
{
struct phone_pvt *p;
p = ast_channel_tech_pvt(ast);
ast_debug(1, "phone_hangup(%s)\n", ast_channel_name(ast));
if (!ast_channel_tech_pvt(ast)) {
ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
return 0;
}
/* XXX Is there anything we can do to really hang up except stop recording? */
if (ioctl(p->fd, PHONE_REC_STOP))
ast_log(LOG_WARNING, "Failed to stop recording\n");
if (ioctl(p->fd, PHONE_PLAY_STOP))
ast_log(LOG_WARNING, "Failed to stop playing\n");
if (ioctl(p->fd, PHONE_RING_STOP))
ast_log(LOG_WARNING, "Failed to stop ringing\n");
if (ioctl(p->fd, PHONE_CPT_STOP))
ast_log(LOG_WARNING, "Failed to stop sounds\n");
/* If it's an FXO, hang them up */
if (p->mode == MODE_FXO) {
if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
ast_debug(1, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",ast_channel_name(ast), strerror(errno));
/* If they're off hook, give a busy signal */
if (ioctl(p->fd, PHONE_HOOKSTATE)) {
ast_debug(1, "Got hunghup, giving busy signal\n");
ao2_cleanup(p->lastformat);
p->lastformat = NULL;
ao2_cleanup(p->lastinput);
p->lastinput = NULL;
p->ministate = 0;
p->obuflen = 0;
p->dialtone = 0;
memset(p->ext, 0, sizeof(p->ext));
((struct phone_pvt *)(ast_channel_tech_pvt(ast)))->owner = NULL;
ast_module_unref(ast_module_info->self);
ast_verb(3, "Hungup '%s'\n", ast_channel_name(ast));
ast_channel_tech_pvt_set(ast, NULL);
restart_monitor();
return 0;
}
static int phone_setup(struct ast_channel *ast)
{
struct phone_pvt *p;
p = ast_channel_tech_pvt(ast);
ioctl(p->fd, PHONE_CPT_STOP);
/* Nothing to answering really, just start recording */
if (ast_format_cmp(ast_channel_rawreadformat(ast), ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
Joshua Colp
committed
/* Prefer g729 */
ioctl(p->fd, PHONE_REC_STOP);
if (!p->lastinput || (ast_format_cmp(p->lastinput, ast_format_g729) != AST_FORMAT_CMP_EQUAL)) {
ao2_replace(p->lastinput, ast_format_g729);
Joshua Colp
committed
if (ioctl(p->fd, PHONE_REC_CODEC, G729)) {
ast_log(LOG_WARNING, "Failed to set codec to g729\n");
return -1;
}
}
} else if (ast_format_cmp(ast_channel_rawreadformat(ast), ast_format_g723) == AST_FORMAT_CMP_EQUAL) {
if (!p->lastinput || (ast_format_cmp(p->lastinput, ast_format_g723) != AST_FORMAT_CMP_EQUAL)) {
ao2_replace(p->lastinput, ast_format_g723);
if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
ast_log(LOG_WARNING, "Failed to set codec to g723.1\n");
return -1;
}
}
} else if (ast_format_cmp(ast_channel_rawreadformat(ast), ast_format_slin) == AST_FORMAT_CMP_EQUAL) {
if (!p->lastinput || (ast_format_cmp(p->lastinput, ast_format_slin) != AST_FORMAT_CMP_EQUAL)) {
ao2_replace(p->lastinput, ast_format_slin);
if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
return -1;
}
}
} else if (ast_format_cmp(ast_channel_rawreadformat(ast), ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
if (!p->lastinput || (ast_format_cmp(p->lastinput, ast_format_ulaw) != AST_FORMAT_CMP_EQUAL)) {
ao2_replace(p->lastinput, ast_format_ulaw);
ast_log(LOG_WARNING, "Failed to set codec to uLaw\n");
} else if (p->mode == MODE_FXS) {
ioctl(p->fd, PHONE_REC_STOP);
if (!p->lastinput || (ast_format_cmp(p->lastinput, ast_channel_rawreadformat(ast)) == AST_FORMAT_CMP_NOT_EQUAL)) {
ao2_replace(p->lastinput, ast_channel_rawreadformat(ast));
if (ioctl(p->fd, PHONE_REC_CODEC, ast_channel_rawreadformat(ast))) {
ast_log(LOG_WARNING, "Failed to set codec to %s\n",
ast_format_get_name(ast_channel_rawreadformat(ast)));
ast_log(LOG_WARNING, "Can't do format %s\n", ast_format_get_name(ast_channel_rawreadformat(ast)));
return -1;
}
if (ioctl(p->fd, PHONE_REC_START)) {
ast_log(LOG_WARNING, "Failed to start recording\n");
return -1;
}
/* set the DTMF times (the default is too short) */
ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
return 0;
}
static int phone_answer(struct ast_channel *ast)
{
p = ast_channel_tech_pvt(ast);
/* In case it's a LineJack, take it off hook */
if (p->mode == MODE_FXO) {
if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_OFF_HOOK))
ast_debug(1, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n", ast_channel_name(ast), strerror(errno));
else
ast_debug(1, "Took linejack off hook\n");
ast_debug(1, "phone_answer(%s)\n", ast_channel_name(ast));
ast_channel_rings_set(ast, 0);
static char phone_2digit(char c)
{
if (c == 12)
return '#';
else if (c == 11)
return '*';
else if ((c < 10) && (c >= 0))
return '0' + c - 1;
else
return '?';
}
static struct ast_frame *phone_exception(struct ast_channel *ast)
{
int res;
union telephony_exception phonee;
struct phone_pvt *p = ast_channel_tech_pvt(ast);
char digit;
/* Some nice norms */
p->fr.datalen = 0;
Michiel van Baak
committed
p->fr.data.ptr = NULL;
Kevin P. Fleming
committed
p->fr.delivery = ast_tv(0,0);
phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
if (phonee.bits.dtmf_ready) {
ast_debug(1, "phone_exception(): DTMF\n");
/* We've got a digit -- Just handle this nicely and easily */
digit = ioctl(p->fd, PHONE_GET_DTMF_ASCII);
p->fr.subclass.integer = digit;
p->fr.frametype = AST_FRAME_DTMF;
return &p->fr;
}
if (phonee.bits.hookstate) {
ast_debug(1, "Hookstate changed\n");
res = ioctl(p->fd, PHONE_HOOKSTATE);
/* See if we've gone on hook, if so, notify by returning NULL */
ast_debug(1, "New hookstate: %d\n", res);
if (ast_channel_state(ast) == AST_STATE_RINGING) {
/* They've picked up the phone */
p->fr.frametype = AST_FRAME_CONTROL;
p->fr.subclass.integer = AST_CONTROL_ANSWER;
ast_log(LOG_WARNING, "Got off hook in weird state %u\n", ast_channel_state(ast));
if (phonee.bits.pstn_ring)
ast_verbose("Unit is ringing\n");
if (phonee.bits.caller_id) {
if (phonee.bits.pstn_wink)
ast_verbose("Detected Wink\n");
/* Strange -- nothing there.. */
p->fr.frametype = AST_FRAME_NULL;
p->fr.subclass.integer = 0;
return &p->fr;
}
static struct ast_frame *phone_read(struct ast_channel *ast)
{
int res;
struct phone_pvt *p = ast_channel_tech_pvt(ast);
Michiel van Baak
committed
p->fr.data.ptr = NULL;
Kevin P. Fleming
committed
p->fr.delivery = ast_tv(0,0);
/* Try to read some data... */
CHECK_BLOCKING(ast);
ast_clear_flag(ast_channel_flags(ast), AST_FLAG_BLOCKING);
if (res < 0) {
#if 0
if (errno == EAGAIN) {
ast_log(LOG_WARNING, "Null frame received\n");
p->fr.frametype = AST_FRAME_NULL;
p->fr.subclass = 0;
return &p->fr;
}
#endif
ast_log(LOG_WARNING, "Error reading: %s\n", strerror(errno));
return NULL;
}
Michiel van Baak
committed
p->fr.data.ptr = p->buf;
switch(p->buf[0] & 0x3) {
case '0':
case '1':
/* Normal */
break;
case '2':
case '3':
/* VAD/CNG, only send two words */
res = 4;
break;
}
p->fr.frametype = ast_format_get_type(p->lastinput) == AST_MEDIA_TYPE_AUDIO ?
AST_FRAME_VOICE : ast_format_get_type(p->lastinput) == AST_MEDIA_TYPE_IMAGE ?
AST_FRAME_IMAGE : AST_FRAME_VIDEO;
p->fr.subclass.format = p->lastinput;
Kevin P. Fleming
committed
/* Byteswap from little-endian to native-endian */
if (ast_format_cmp(p->fr.subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)
Kevin P. Fleming
committed
ast_frame_byteswap_le(&p->fr);
Kevin P. Fleming
committed
static int phone_write_buf(struct phone_pvt *p, const char *buf, int len, int frlen, int swap)
{
int res;
/* Store as much of the buffer as we can, then write fixed frames */
int space = sizeof(p->obuf) - p->obuflen;
/* Make sure we have enough buffer space to store the frame */
if (space < len)
len = space;
Kevin P. Fleming
committed
if (swap)
ast_swapcopy_samples(p->obuf+p->obuflen, buf, len/2);
Kevin P. Fleming
committed
else
memcpy(p->obuf + p->obuflen, buf, len);
p->obuflen += len;
while(p->obuflen > frlen) {
res = write(p->fd, p->obuf, frlen);
if (res != frlen) {
if (res < 1) {
/*
* Card is in non-blocking mode now and it works well now, but there are
* lot of messages like this. So, this message is temporarily disabled.
*/
return 0;
} else {
ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
}
}
p->obuflen -= frlen;
/* Move memory if necessary */
if (p->obuflen)
memmove(p->obuf, p->obuf + frlen, p->obuflen);
}
return len;
}
Mark Spencer
committed
static int phone_send_text(struct ast_channel *ast, const char *text)
return phone_write_buf(ast_channel_tech_pvt(ast), text, length, length, 0) ==
static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
{
struct phone_pvt *p = ast_channel_tech_pvt(ast);
int res;
int maxfr=0;
char *pos;
int sofar;
int expected;
char tmpbuf[4];
/* Write a frame of (presumably voice) data */
if (frame->frametype != AST_FRAME_VOICE && p->mode != MODE_FXS) {
ast_log(LOG_WARNING, "Don't know what to do with frame type '%u'\n", frame->frametype);
/* If we're not in up mode, go into up mode now */
if (ast->_state != AST_STATE_UP) {
ast_setstate(ast, AST_STATE_UP);
if (ast_channel_state(ast) != AST_STATE_UP) {
/* Don't try tos end audio on-hook */
return 0;
}
#endif
if (ast_format_cmp(frame->subclass.format, ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
if (!p->lastformat || (ast_format_cmp(p->lastformat, ast_format_g729) != AST_FORMAT_CMP_EQUAL)) {
Joshua Colp
committed
ioctl(p->fd, PHONE_PLAY_STOP);
ioctl(p->fd, PHONE_REC_STOP);
if (ioctl(p->fd, PHONE_PLAY_CODEC, G729)) {
ast_log(LOG_WARNING, "Unable to set G729 mode\n");
return -1;
}
if (ioctl(p->fd, PHONE_REC_CODEC, G729)) {
ast_log(LOG_WARNING, "Unable to set G729 mode\n");
return -1;
}
ao2_replace(p->lastformat, ast_format_g729);
ao2_replace(p->lastinput, ast_format_g729);
Joshua Colp
committed
/* Reset output buffer */
p->obuflen = 0;
codecset = 1;
}
if (frame->datalen > 80) {
ast_log(LOG_WARNING, "Frame size too large for G.729 (%d bytes)\n", frame->datalen);
return -1;
}
maxfr = 80;
} else if (ast_format_cmp(frame->subclass.format, ast_format_g723) == AST_FORMAT_CMP_EQUAL) {
if (!p->lastformat || (ast_format_cmp(p->lastformat, ast_format_g723) != AST_FORMAT_CMP_EQUAL)) {
ioctl(p->fd, PHONE_PLAY_STOP);
ioctl(p->fd, PHONE_REC_STOP);
if (ioctl(p->fd, PHONE_PLAY_CODEC, G723_63)) {
ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
return -1;
}
if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
return -1;
}
ao2_replace(p->lastformat, ast_format_g723);
ao2_replace(p->lastinput, ast_format_g723);
}
if (frame->datalen > 24) {
ast_log(LOG_WARNING, "Frame size too large for G.723.1 (%d bytes)\n", frame->datalen);
return -1;
}
maxfr = 24;
} else if (ast_format_cmp(frame->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) {
if (!p->lastformat || (ast_format_cmp(p->lastformat, ast_format_slin) != AST_FORMAT_CMP_EQUAL)) {
ioctl(p->fd, PHONE_PLAY_STOP);
ioctl(p->fd, PHONE_REC_STOP);
if (ioctl(p->fd, PHONE_PLAY_CODEC, LINEAR16)) {
ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
return -1;
}
if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
return -1;
}
ao2_replace(p->lastformat, ast_format_slin);
ao2_replace(p->lastinput, ast_format_slin);
/* Reset output buffer */
p->obuflen = 0;
}
maxfr = 480;
} else if (ast_format_cmp(frame->subclass.format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
if (!p->lastformat || (ast_format_cmp(p->lastformat, ast_format_ulaw) != AST_FORMAT_CMP_EQUAL)) {
ioctl(p->fd, PHONE_PLAY_STOP);
ioctl(p->fd, PHONE_REC_STOP);
if (ioctl(p->fd, PHONE_PLAY_CODEC, ULAW)) {
ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
return -1;
}
if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
return -1;
}
ao2_replace(p->lastformat, ast_format_ulaw);
ao2_replace(p->lastinput, ast_format_ulaw);
codecset = 1;
/* Reset output buffer */
p->obuflen = 0;
}
maxfr = 240;
if (!p->lastformat || (ast_format_cmp(p->lastformat, frame->subclass.format) != AST_FORMAT_CMP_EQUAL)) {
ioctl(p->fd, PHONE_PLAY_STOP);
ioctl(p->fd, PHONE_REC_STOP);
if (ioctl(p->fd, PHONE_PLAY_CODEC, ast_format_compatibility_format2bitfield(frame->subclass.format))) {
ast_log(LOG_WARNING, "Unable to set %s mode\n",
ast_format_get_name(frame->subclass.format));
if (ioctl(p->fd, PHONE_REC_CODEC, ast_format_compatibility_format2bitfield(frame->subclass.format))) {
ast_log(LOG_WARNING, "Unable to set %s mode\n",
ast_format_get_name(frame->subclass.format));
ao2_replace(p->lastformat, frame->subclass.format);
ao2_replace(p->lastinput, frame->subclass.format);
codecset = 1;
/* Reset output buffer */
p->obuflen = 0;
}
maxfr = 480;
ioctl(p->fd, PHONE_REC_DEPTH, 3);
ioctl(p->fd, PHONE_PLAY_DEPTH, 3);
if (ioctl(p->fd, PHONE_PLAY_START)) {
ast_log(LOG_WARNING, "Failed to start playback\n");
return -1;
}
if (ioctl(p->fd, PHONE_REC_START)) {
ast_log(LOG_WARNING, "Failed to start recording\n");
return -1;
}
/* If we get here, we have a frame of Appropriate data */
Michiel van Baak
committed
pos = frame->data.ptr;
while(sofar < frame->datalen) {
/* Write in no more than maxfr sized frames */
expected = frame->datalen - sofar;
if (maxfr < expected)
expected = maxfr;
/* XXX Internet Phone Jack does not handle the 4-byte VAD frame properly! XXX
we have to pad it to 24 bytes still. */
if (frame->datalen == 4) {
if (p->silencesupression) {
Michiel van Baak
committed
memcpy(tmpbuf, frame->data.ptr, 4);
Kevin P. Fleming
committed
res = phone_write_buf(p, tmpbuf, expected, maxfr, 0);
Kevin P. Fleming
committed
int swap = 0;
#if __BYTE_ORDER == __BIG_ENDIAN
if (ast_format_cmp(frame->subclass.format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)
Kevin P. Fleming
committed
swap = 1; /* Swap big-endian samples to little-endian as we copy */
#endif
res = phone_write_buf(p, pos, expected, maxfr, swap);
if (res < 0)
ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
/*
* Card is in non-blocking mode now and it works well now, but there are
* lot of messages like this. So, this message is temporarily disabled.
*/
else
ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
return -1;
} else /* Pretend it worked */
res = expected;
}
sofar += res;
pos += res;
}
return 0;
}
static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *cntx, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
struct ast_format_cap *caps = NULL;
struct phone_codec_data queried_codec;
struct ast_format *tmpfmt;
caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, "", i->ext, i->context, assignedids, requestor, 0, "Phone/%s", i->dev + 5);
if (tmp && caps) {
ast_channel_lock(tmp);
ast_channel_tech_set(tmp, cur_tech);
Joshua Colp
committed
ast_channel_set_fd(tmp, 0, i->fd);
/* XXX Switching formats silently causes kernel panics XXX */
ioctl(i->fd, PHONE_QUERY_CODEC, &queried_codec) == 0) {
if (queried_codec.type == LINEAR16) {
ast_format_cap_append(caps, ast_format_slin, 0);
} else {
ast_format_cap_remove(prefcap, ast_format_slin);
ast_format_cap_append_from_cap(caps, prefcap, AST_MEDIA_TYPE_UNKNOWN);
} else {
ast_format_cap_append_from_cap(caps, prefcap, AST_MEDIA_TYPE_UNKNOWN);
tmpfmt = ast_format_cap_get_format(caps, 0);
ast_channel_nativeformats_set(tmp, caps);
ao2_ref(caps, -1);
ast_channel_set_rawreadformat(tmp, tmpfmt);
ast_channel_set_rawwriteformat(tmp, tmpfmt);
ao2_ref(tmpfmt, -1);
/* no need to call ast_setstate: the channel_alloc already did its job */
ast_channel_rings_set(tmp, 1);
ast_channel_tech_pvt_set(tmp, i);
ast_channel_context_set(tmp, cntx);
if (!ast_strlen_zero(i->ext))
ast_channel_exten_set(tmp, i->ext);
ast_channel_exten_set(tmp, "s");
if (!ast_strlen_zero(i->language))
ast_channel_language_set(tmp, i->language);
/* Don't use ast_set_callerid() here because it will
* generate a NewCallerID event before the NewChannel event */
if (!ast_strlen_zero(i->cid_num)) {
ast_channel_caller(tmp)->ani.number.valid = 1;
ast_channel_caller(tmp)->ani.number.str = ast_strdup(i->cid_num);
ast_module_ref(ast_module_info->self);
if (state != AST_STATE_DOWN) {
if (state == AST_STATE_RING) {
ioctl(ast_channel_fd(tmp, 0), PHONE_RINGBACK);
ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(tmp));
} else {
ao2_cleanup(caps);
ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
}
return tmp;
}
static void phone_mini_packet(struct phone_pvt *i)
{
int res;
char buf[1024];
/* Ignore stuff we read... */
res = read(i->fd, buf, sizeof(buf));
if (res < 1) {
ast_log(LOG_WARNING, "Read returned %d: %s\n", res, strerror(errno));
return;
}
}
static void phone_check_exception(struct phone_pvt *i)
{
int offhook=0;
char digit[2] = {0 , 0};
union telephony_exception phonee;
/* XXX Do something XXX */
#if 0
ast_debug(1, "Exception!\n");
#endif
phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION);
if (phonee.bits.dtmf_ready) {
digit[0] = ioctl(i->fd, PHONE_GET_DTMF_ASCII);
if (i->mode == MODE_DIALTONE || i->mode == MODE_FXS || i->mode == MODE_SIGMA) {
ioctl(i->fd, PHONE_PLAY_STOP);
ioctl(i->fd, PHONE_REC_STOP);
ioctl(i->fd, PHONE_CPT_STOP);
i->dialtone = 0;
if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
strncat(i->ext, digit, sizeof(i->ext) - strlen(i->ext) - 1);
if ((i->mode != MODE_FXS ||
!(phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION)) ||
!phonee.bits.dtmf_ready) &&
ast_exists_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
/* It's a valid extension in its context, get moving! */
phone_new(i, AST_STATE_RING, i->context, NULL, NULL);
/* No need to restart monitor, we are the monitor */
} else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
/* There is nothing in the specified extension that can match anymore.
Try the default */
if (ast_exists_extension(NULL, "default", i->ext, 1, i->cid_num)) {
phone_new(i, AST_STATE_RING, "default", NULL, NULL);
/* XXX This should probably be justified better XXX */
} else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->cid_num)) {
/* It's not a valid extension, give a busy signal */
ast_debug(1, "%s can't match anything in %s or default\n", i->ext, i->context);
}
}
#if 0
ast_verbose("Extension is %s\n", i->ext);
#endif
}
}
if (phonee.bits.hookstate) {
offhook = ioctl(i->fd, PHONE_HOOKSTATE);
if (offhook) {
if (i->mode == MODE_IMMEDIATE) {
phone_new(i, AST_STATE_RING, i->context, NULL, NULL);
ast_module_ref(ast_module_info->self);
/* Reset the extension */
i->ext[0] = '\0';
/* Play the dialtone */
i->dialtone++;
ioctl(i->fd, PHONE_PLAY_STOP);
ioctl(i->fd, PHONE_PLAY_CODEC, ULAW);
ioctl(i->fd, PHONE_PLAY_START);
ao2_cleanup(i->lastformat);
i->lastformat = NULL;
} else if (i->mode == MODE_SIGMA) {
ast_module_ref(ast_module_info->self);