00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00031
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <ctype.h>
00035 #include <sys/socket.h>
00036 #include <sys/time.h>
00037 #include <errno.h>
00038 #include <unistd.h>
00039 #include <stdlib.h>
00040 #include <arpa/inet.h>
00041 #include <fcntl.h>
00042 #include <sys/ioctl.h>
00043 #include <signal.h>
00044 #include <linux/telephony.h>
00045
00046 #include <linux/version.h>
00047 #include <linux/ixjuser.h>
00048
00049 #include "asterisk/lock.h"
00050 #include "asterisk/channel.h"
00051 #include "asterisk/config.h"
00052 #include "asterisk/logger.h"
00053 #include "asterisk/module.h"
00054 #include "asterisk/pbx.h"
00055 #include "asterisk/options.h"
00056 #include "asterisk/utils.h"
00057 #include "asterisk/callerid.h"
00058 #include "asterisk/causes.h"
00059 #include "asterisk/stringfields.h"
00060 #include "asterisk/musiconhold.h"
00061
00062 #include "DialTone.h"
00063
00064 #ifdef QTI_PHONEJACK_TJ_PCI
00065 #define QNDRV_VER 310
00066 #else
00067 #define QNDRV_VER 100
00068 #endif
00069
00070 #if QNDRV_VER > 100
00071 #ifdef __linux__
00072 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
00073 #else
00074 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, x);
00075 #endif
00076 #else
00077 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
00078 #endif
00079
00080 #define DEFAULT_CALLER_ID "Unknown"
00081 #define PHONE_MAX_BUF 480
00082 #define DEFAULT_GAIN 0x100
00083
00084 static const char tdesc[] = "Standard Linux Telephony API Driver";
00085 static const char config[] = "phone.conf";
00086
00087
00088 static char context[AST_MAX_EXTENSION] = "default";
00089
00090
00091 static char language[MAX_LANGUAGE] = "";
00092
00093 static int echocancel = AEC_OFF;
00094
00095 static int silencesupression = 0;
00096
00097 static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW;
00098
00099
00100 AST_MUTEX_DEFINE_STATIC(iflock);
00101
00102
00103
00104 AST_MUTEX_DEFINE_STATIC(monlock);
00105
00106
00107 static unsigned int monitor;
00108
00109
00110
00111 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00112
00113 static int restart_monitor(void);
00114
00115
00116
00117
00118 #define MODE_DIALTONE 1
00119 #define MODE_IMMEDIATE 2
00120 #define MODE_FXO 3
00121 #define MODE_FXS 4
00122 #define MODE_SIGMA 5
00123
00124 static struct phone_pvt {
00125 int fd;
00126 struct ast_channel *owner;
00127 int mode;
00128 int lastformat;
00129 int lastinput;
00130 int ministate;
00131 char dev[256];
00132 struct phone_pvt *next;
00133 struct ast_frame fr;
00134 char offset[AST_FRIENDLY_OFFSET];
00135 char buf[PHONE_MAX_BUF];
00136 int obuflen;
00137 int dialtone;
00138 int txgain, rxgain;
00139
00140 int cpt;
00141 int silencesupression;
00142 char context[AST_MAX_EXTENSION];
00143 char obuf[PHONE_MAX_BUF * 2];
00144 char ext[AST_MAX_EXTENSION];
00145 char language[MAX_LANGUAGE];
00146 char cid_num[AST_MAX_EXTENSION];
00147 char cid_name[AST_MAX_EXTENSION];
00148 } *iflist = NULL;
00149
00150 static char cid_num[AST_MAX_EXTENSION];
00151 static char cid_name[AST_MAX_EXTENSION];
00152
00153 static struct ast_channel *phone_request(const char *type, int format, void *data, int *cause);
00154 static int phone_digit_begin(struct ast_channel *ast, char digit);
00155 static int phone_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
00156 static int phone_call(struct ast_channel *ast, char *dest, int timeout);
00157 static int phone_hangup(struct ast_channel *ast);
00158 static int phone_answer(struct ast_channel *ast);
00159 static struct ast_frame *phone_read(struct ast_channel *ast);
00160 static int phone_write(struct ast_channel *ast, struct ast_frame *frame);
00161 static struct ast_frame *phone_exception(struct ast_channel *ast);
00162 static int phone_send_text(struct ast_channel *ast, const char *text);
00163 static int phone_fixup(struct ast_channel *old, struct ast_channel *new);
00164 static int phone_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
00165
00166 static const struct ast_channel_tech phone_tech = {
00167 .type = "Phone",
00168 .description = tdesc,
00169 .capabilities = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW,
00170 .requester = phone_request,
00171 .send_digit_begin = phone_digit_begin,
00172 .send_digit_end = phone_digit_end,
00173 .call = phone_call,
00174 .hangup = phone_hangup,
00175 .answer = phone_answer,
00176 .read = phone_read,
00177 .write = phone_write,
00178 .exception = phone_exception,
00179 .indicate = phone_indicate,
00180 .fixup = phone_fixup
00181 };
00182
00183 static struct ast_channel_tech phone_tech_fxs = {
00184 .type = "Phone",
00185 .description = tdesc,
00186 .requester = phone_request,
00187 .send_digit_begin = phone_digit_begin,
00188 .send_digit_end = phone_digit_end,
00189 .call = phone_call,
00190 .hangup = phone_hangup,
00191 .answer = phone_answer,
00192 .read = phone_read,
00193 .write = phone_write,
00194 .exception = phone_exception,
00195 .write_video = phone_write,
00196 .send_text = phone_send_text,
00197 .indicate = phone_indicate,
00198 .fixup = phone_fixup
00199 };
00200
00201 static struct ast_channel_tech *cur_tech;
00202
00203 static int phone_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
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 }
00227
00228 static int phone_fixup(struct ast_channel *old, struct ast_channel *new)
00229 {
00230 struct phone_pvt *pvt = old->tech_pvt;
00231 if (pvt && pvt->owner == old)
00232 pvt->owner = new;
00233 return 0;
00234 }
00235
00236 static int phone_digit_begin(struct ast_channel *chan, char digit)
00237 {
00238
00239 return 0;
00240 }
00241
00242 static int phone_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
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':
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 }
00283
00284 static int phone_call(struct ast_channel *ast, char *dest, int timeout)
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
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 }
00339
00340 static int phone_hangup(struct ast_channel *ast)
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
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
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
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 }
00389
00390 static int phone_setup(struct ast_channel *ast)
00391 {
00392 struct phone_pvt *p;
00393 p = ast->tech_pvt;
00394 ioctl(p->fd, PHONE_CPT_STOP);
00395
00396 if (ast->rawreadformat == AST_FORMAT_G723_1) {
00397
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
00443 ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
00444 ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
00445 return 0;
00446 }
00447
00448 static int phone_answer(struct ast_channel *ast)
00449 {
00450 struct phone_pvt *p;
00451 p = ast->tech_pvt;
00452
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 }
00466
00467 #if 0
00468 static char phone_2digit(char c)
00469 {
00470 if (c == 12)
00471 return '#';
00472 else if (c == 11)
00473 return '*';
00474 else if ((c < 10) && (c >= 0))
00475 return '0' + c - 1;
00476 else
00477 return '?';
00478 }
00479 #endif
00480
00481 static struct ast_frame *phone_exception(struct ast_channel *ast)
00482 {
00483 int res;
00484 union telephony_exception phonee;
00485 struct phone_pvt *p = ast->tech_pvt;
00486 char digit;
00487
00488
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
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
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
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
00539 p->fr.frametype = AST_FRAME_NULL;
00540 p->fr.subclass = 0;
00541 return &p->fr;
00542 }
00543
00544 static struct ast_frame *phone_read(struct ast_channel *ast)
00545 {
00546 int res;
00547 struct phone_pvt *p = ast->tech_pvt;
00548
00549
00550
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
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
00581 break;
00582 case '2':
00583 case '3':
00584
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
00597 if (p->fr.subclass == AST_FORMAT_SLINEAR)
00598 ast_frame_byteswap_le(&p->fr);
00599 return &p->fr;
00600 }
00601
00602 static int phone_write_buf(struct phone_pvt *p, const char *buf, int len, int frlen, int swap)
00603 {
00604 int res;
00605
00606 int space = sizeof(p->obuf) - p->obuflen;
00607
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
00621
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
00630 if (p->obuflen)
00631 memmove(p->obuf, p->obuf + frlen, p->obuflen);
00632 }
00633 return len;
00634 }
00635
00636 static int phone_send_text(struct ast_channel *ast, const char *text)
00637 {
00638 int length = strlen(text);
00639 return phone_write_buf(ast->tech_pvt, text, length, length, 0) ==
00640 length ? 0 : -1;
00641 }
00642
00643 static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
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
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
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
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
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
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
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
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
00773 sofar = 0;
00774 pos = frame->data;
00775 while(sofar < frame->datalen) {
00776
00777 expected = frame->datalen - sofar;
00778 if (maxfr < expected)
00779 expected = maxfr;
00780
00781
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;
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
00805
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
00813 res = expected;
00814 }
00815 sofar += res;
00816 pos += res;
00817 }
00818 return 0;
00819 }
00820
00821 static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *context)
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
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
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
00862
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 }
00883
00884 static void phone_mini_packet(struct phone_pvt *i)
00885 {
00886 int res;
00887 char buf[1024];
00888
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 }
00895
00896 static void phone_check_exception(struct phone_pvt *i)
00897 {
00898 int offhook=0;
00899 char digit[2] = {0 , 0};
00900 union telephony_exception phonee;
00901
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
00920 phone_new(i, AST_STATE_RING, i->context);
00921
00922 } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
00923
00924
00925 if (ast_exists_extension(NULL, "default", i->ext, 1, i->cid_num)) {
00926
00927 phone_new(i, AST_STATE_RING, "default");
00928
00929 } else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->cid_num)) {
00930
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
00950 i->ext[0] = '\0';
00951
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
00960 i->ext[0] = '\0';
00961
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 }
00989
00990 static void *do_monitor(void *data)
00991 {
00992 fd_set rfds, efds;
00993 int n, res;
00994 struct phone_pvt *i;
00995 int tonepos = 0;
00996
00997 struct timeval tv = {0,0};
00998 int dotone;
00999
01000
01001 while (monitor) {
01002
01003
01004
01005 if (ast_mutex_lock(&iflock)) {
01006 ast_log(LOG_ERROR, "Unable to grab interface lock\n");
01007 return NULL;
01008 }
01009
01010
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
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
01027
01028 if (ast_tvzero(tv)) {
01029
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
01040 ast_mutex_unlock(&iflock);
01041
01042
01043 if (dotone && i->mode != MODE_SIGMA) {
01044
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
01058 if (res < 0) {
01059 ast_log(LOG_DEBUG, "select return %d: %s\n", res, strerror(errno));
01060 continue;
01061 }
01062
01063
01064 if (!res)
01065 continue;
01066
01067
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 }
01093
01094 static int restart_monitor()
01095 {
01096
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
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 }
01130
01131 static struct phone_pvt *mkif(char *iface, int mode, int txgain, int rxgain)
01132 {
01133
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 }
01190
01191 static struct ast_channel *phone_request(const char *type, int format, void *data, int *cause)
01192 {
01193 int oldformat;
01194 struct phone_pvt *p;
01195 struct ast_channel *tmp = NULL;
01196 char *name = data;
01197
01198
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 }
01231
01232
01233 static int parse_gain_value(char *gain_type, char *value)
01234 {
01235 float gain;
01236
01237
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
01246 gain = gain * (float)DEFAULT_GAIN;
01247
01248
01249 if (value[strlen(value) - 1] == '%')
01250 return (int)(gain / (float)100);
01251
01252 return (int)gain;
01253 }
01254
01255 static int __unload_module(void)
01256 {
01257 struct phone_pvt *p, *pl;
01258
01259 ast_channel_unregister(cur_tech);
01260 if (!ast_mutex_lock(&iflock)) {
01261
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
01290 p = iflist;
01291 while(p) {
01292
01293 if (p->fd > -1)
01294 close(p->fd);
01295 pl = p;
01296 p = p->next;
01297
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 }
01309
01310 static int unload_module(void)
01311 {
01312 return __unload_module();
01313 }
01314
01315 static int load_module(void)
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;
01322 cfg = ast_config_load(config);
01323
01324
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
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
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;
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
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
01420 restart_monitor();
01421 return 0;
01422 }
01423
01424 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Linux Telephony API Support");