when building a version string for a developer branch, include the base branch in...
[asterisk-bristuff.git] / channels / chan_phone.c
blob961ec78b34a1dc5e3642f671851c4ba6457f26dd
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Generic Linux Telephony Interface driver
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup channel_drivers
28 /*** MODULEINFO
29 <depend>ixjuser</depend>
30 ***/
32 #include "asterisk.h"
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include <stdio.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <sys/socket.h>
40 #include <sys/time.h>
41 #include <errno.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44 #include <arpa/inet.h>
45 #include <fcntl.h>
46 #include <sys/ioctl.h>
47 #include <signal.h>
48 #include <linux/telephony.h>
49 /* Still use some IXJ specific stuff */
50 #include <linux/version.h>
51 #include <linux/ixjuser.h>
53 #include "asterisk/lock.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/config.h"
56 #include "asterisk/logger.h"
57 #include "asterisk/module.h"
58 #include "asterisk/pbx.h"
59 #include "asterisk/options.h"
60 #include "asterisk/utils.h"
61 #include "asterisk/callerid.h"
62 #include "asterisk/causes.h"
63 #include "asterisk/stringfields.h"
64 #include "asterisk/musiconhold.h"
66 #include "DialTone.h"
68 #ifdef QTI_PHONEJACK_TJ_PCI /* check for the newer quicknet driver v.3.1.0 which has this symbol */
69 #define QNDRV_VER 310
70 #else
71 #define QNDRV_VER 100
72 #endif
74 #if QNDRV_VER > 100
75 #ifdef __linux__
76 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
77 #else /* FreeBSD and others */
78 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, x);
79 #endif /* __linux__ */
80 #else /* older driver */
81 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
82 #endif
84 #define DEFAULT_CALLER_ID "Unknown"
85 #define PHONE_MAX_BUF 480
86 #define DEFAULT_GAIN 0x100
88 static const char tdesc[] = "Standard Linux Telephony API Driver";
89 static const char config[] = "phone.conf";
91 /* Default context for dialtone mode */
92 static char context[AST_MAX_EXTENSION] = "default";
94 /* Default language */
95 static char language[MAX_LANGUAGE] = "";
97 static int echocancel = AEC_OFF;
99 static int silencesupression = 0;
101 static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW;
103 /* Protect the interface list (of phone_pvt's) */
104 AST_MUTEX_DEFINE_STATIC(iflock);
106 /* Protect the monitoring thread, so only one process can kill or start it, and not
107 when it's doing something critical. */
108 AST_MUTEX_DEFINE_STATIC(monlock);
110 /* Boolean value whether the monitoring thread shall continue. */
111 static unsigned int monitor;
113 /* This is the thread for the monitor which checks for input on the channels
114 which are not currently in use. */
115 static pthread_t monitor_thread = AST_PTHREADT_NULL;
117 static int restart_monitor(void);
119 /* The private structures of the Phone Jack channels are linked for
120 selecting outgoing channels */
122 #define MODE_DIALTONE 1
123 #define MODE_IMMEDIATE 2
124 #define MODE_FXO 3
125 #define MODE_FXS 4
126 #define MODE_SIGMA 5
128 static struct phone_pvt {
129 int fd; /* Raw file descriptor for this device */
130 struct ast_channel *owner; /* Channel we belong to, possibly NULL */
131 int mode; /* Is this in the */
132 int lastformat; /* Last output format */
133 int lastinput; /* Last input format */
134 int ministate; /* Miniature state, for dialtone mode */
135 char dev[256]; /* Device name */
136 struct phone_pvt *next; /* Next channel in list */
137 struct ast_frame fr; /* Frame */
138 char offset[AST_FRIENDLY_OFFSET];
139 char buf[PHONE_MAX_BUF]; /* Static buffer for reading frames */
140 int obuflen;
141 int dialtone;
142 int txgain, rxgain; /* gain control for playing, recording */
143 /* 0x100 - 1.0, 0x200 - 2.0, 0x80 - 0.5 */
144 int cpt; /* Call Progress Tone playing? */
145 int silencesupression;
146 char context[AST_MAX_EXTENSION];
147 char obuf[PHONE_MAX_BUF * 2];
148 char ext[AST_MAX_EXTENSION];
149 char language[MAX_LANGUAGE];
150 char cid_num[AST_MAX_EXTENSION];
151 char cid_name[AST_MAX_EXTENSION];
152 } *iflist = NULL;
154 static char cid_num[AST_MAX_EXTENSION];
155 static char cid_name[AST_MAX_EXTENSION];
157 static struct ast_channel *phone_request(const char *type, int format, void *data, int *cause);
158 static int phone_digit_begin(struct ast_channel *ast, char digit);
159 static int phone_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
160 static int phone_call(struct ast_channel *ast, char *dest, int timeout);
161 static int phone_hangup(struct ast_channel *ast);
162 static int phone_answer(struct ast_channel *ast);
163 static struct ast_frame *phone_read(struct ast_channel *ast);
164 static int phone_write(struct ast_channel *ast, struct ast_frame *frame);
165 static struct ast_frame *phone_exception(struct ast_channel *ast);
166 static int phone_send_text(struct ast_channel *ast, const char *text);
167 static int phone_fixup(struct ast_channel *old, struct ast_channel *new);
168 static int phone_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
170 static const struct ast_channel_tech phone_tech = {
171 .type = "Phone",
172 .description = tdesc,
173 .capabilities = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW,
174 .requester = phone_request,
175 .send_digit_begin = phone_digit_begin,
176 .send_digit_end = phone_digit_end,
177 .call = phone_call,
178 .hangup = phone_hangup,
179 .answer = phone_answer,
180 .read = phone_read,
181 .write = phone_write,
182 .exception = phone_exception,
183 .indicate = phone_indicate,
184 .fixup = phone_fixup
187 static struct ast_channel_tech phone_tech_fxs = {
188 .type = "Phone",
189 .description = tdesc,
190 .requester = phone_request,
191 .send_digit_begin = phone_digit_begin,
192 .send_digit_end = phone_digit_end,
193 .call = phone_call,
194 .hangup = phone_hangup,
195 .answer = phone_answer,
196 .read = phone_read,
197 .write = phone_write,
198 .exception = phone_exception,
199 .write_video = phone_write,
200 .send_text = phone_send_text,
201 .indicate = phone_indicate,
202 .fixup = phone_fixup
205 static struct ast_channel_tech *cur_tech;
207 static int phone_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
209 struct phone_pvt *p = chan->tech_pvt;
210 int res=-1;
211 ast_log(LOG_DEBUG, "Requested indication %d on channel %s\n", condition, chan->name);
212 switch(condition) {
213 case AST_CONTROL_FLASH:
214 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
215 usleep(320000);
216 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
217 p->lastformat = -1;
218 res = 0;
219 break;
220 case AST_CONTROL_HOLD:
221 ast_moh_start(chan, data, NULL);
222 break;
223 case AST_CONTROL_UNHOLD:
224 ast_moh_stop(chan);
225 break;
226 default:
227 ast_log(LOG_WARNING, "Condition %d is not supported on channel %s\n", condition, chan->name);
229 return res;
232 static int phone_fixup(struct ast_channel *old, struct ast_channel *new)
234 struct phone_pvt *pvt = old->tech_pvt;
235 if (pvt && pvt->owner == old)
236 pvt->owner = new;
237 return 0;
240 static int phone_digit_begin(struct ast_channel *chan, char digit)
242 /* XXX Modify this callback to let Asterisk support controlling the length of DTMF */
243 return 0;
246 static int phone_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
248 struct phone_pvt *p;
249 int outdigit;
250 p = ast->tech_pvt;
251 ast_log(LOG_DEBUG, "Dialed %c\n", digit);
252 switch(digit) {
253 case '0':
254 case '1':
255 case '2':
256 case '3':
257 case '4':
258 case '5':
259 case '6':
260 case '7':
261 case '8':
262 case '9':
263 outdigit = digit - '0';
264 break;
265 case '*':
266 outdigit = 11;
267 break;
268 case '#':
269 outdigit = 12;
270 break;
271 case 'f': /*flash*/
272 case 'F':
273 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
274 usleep(320000);
275 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
276 p->lastformat = -1;
277 return 0;
278 default:
279 ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
280 return -1;
282 ast_log(LOG_DEBUG, "Dialed %d\n", outdigit);
283 ioctl(p->fd, PHONE_PLAY_TONE, outdigit);
284 p->lastformat = -1;
285 return 0;
288 static int phone_call(struct ast_channel *ast, char *dest, int timeout)
290 struct phone_pvt *p;
292 PHONE_CID cid;
293 time_t UtcTime;
294 struct tm tm;
295 int start;
297 time(&UtcTime);
298 localtime_r(&UtcTime,&tm);
300 memset(&cid, 0, sizeof(PHONE_CID));
301 if(&tm != NULL) {
302 snprintf(cid.month, sizeof(cid.month), "%02d",(tm.tm_mon + 1));
303 snprintf(cid.day, sizeof(cid.day), "%02d", tm.tm_mday);
304 snprintf(cid.hour, sizeof(cid.hour), "%02d", tm.tm_hour);
305 snprintf(cid.min, sizeof(cid.min), "%02d", tm.tm_min);
307 /* the standard format of ast->callerid is: "name" <number>, but not always complete */
308 if (ast_strlen_zero(ast->cid.cid_name))
309 strcpy(cid.name, DEFAULT_CALLER_ID);
310 else
311 ast_copy_string(cid.name, ast->cid.cid_name, sizeof(cid.name));
313 if (ast->cid.cid_num)
314 ast_copy_string(cid.number, ast->cid.cid_num, sizeof(cid.number));
316 p = ast->tech_pvt;
318 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
319 ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast->name);
320 return -1;
322 if (option_debug)
323 ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fds[0]);
325 start = IXJ_PHONE_RING_START(cid);
326 if (start == -1)
327 return -1;
329 if (p->mode == MODE_FXS) {
330 char *digit = strchr(dest, '/');
331 if (digit)
333 digit++;
334 while (*digit)
335 phone_digit_end(ast, *digit++, 0);
339 ast_setstate(ast, AST_STATE_RINGING);
340 ast_queue_control(ast, AST_CONTROL_RINGING);
341 return 0;
344 static int phone_hangup(struct ast_channel *ast)
346 struct phone_pvt *p;
347 p = ast->tech_pvt;
348 if (option_debug)
349 ast_log(LOG_DEBUG, "phone_hangup(%s)\n", ast->name);
350 if (!ast->tech_pvt) {
351 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
352 return 0;
354 /* XXX Is there anything we can do to really hang up except stop recording? */
355 ast_setstate(ast, AST_STATE_DOWN);
356 if (ioctl(p->fd, PHONE_REC_STOP))
357 ast_log(LOG_WARNING, "Failed to stop recording\n");
358 if (ioctl(p->fd, PHONE_PLAY_STOP))
359 ast_log(LOG_WARNING, "Failed to stop playing\n");
360 if (ioctl(p->fd, PHONE_RING_STOP))
361 ast_log(LOG_WARNING, "Failed to stop ringing\n");
362 if (ioctl(p->fd, PHONE_CPT_STOP))
363 ast_log(LOG_WARNING, "Failed to stop sounds\n");
365 /* If it's an FXO, hang them up */
366 if (p->mode == MODE_FXO) {
367 if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
368 ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",ast->name, strerror(errno));
371 /* If they're off hook, give a busy signal */
372 if (ioctl(p->fd, PHONE_HOOKSTATE)) {
373 if (option_debug)
374 ast_log(LOG_DEBUG, "Got hunghup, giving busy signal\n");
375 ioctl(p->fd, PHONE_BUSY);
376 p->cpt = 1;
378 p->lastformat = -1;
379 p->lastinput = -1;
380 p->ministate = 0;
381 p->obuflen = 0;
382 p->dialtone = 0;
383 memset(p->ext, 0, sizeof(p->ext));
384 ((struct phone_pvt *)(ast->tech_pvt))->owner = NULL;
385 ast_module_unref(ast_module_info->self);
386 if (option_verbose > 2)
387 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
388 ast->tech_pvt = NULL;
389 ast_setstate(ast, AST_STATE_DOWN);
390 restart_monitor();
391 return 0;
394 static int phone_setup(struct ast_channel *ast)
396 struct phone_pvt *p;
397 p = ast->tech_pvt;
398 ioctl(p->fd, PHONE_CPT_STOP);
399 /* Nothing to answering really, just start recording */
400 if (ast->rawreadformat == AST_FORMAT_G723_1) {
401 /* Prefer g723 */
402 ioctl(p->fd, PHONE_REC_STOP);
403 if (p->lastinput != AST_FORMAT_G723_1) {
404 p->lastinput = AST_FORMAT_G723_1;
405 if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
406 ast_log(LOG_WARNING, "Failed to set codec to g723.1\n");
407 return -1;
410 } else if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
411 ioctl(p->fd, PHONE_REC_STOP);
412 if (p->lastinput != AST_FORMAT_SLINEAR) {
413 p->lastinput = AST_FORMAT_SLINEAR;
414 if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
415 ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
416 return -1;
419 } else if (ast->rawreadformat == AST_FORMAT_ULAW) {
420 ioctl(p->fd, PHONE_REC_STOP);
421 if (p->lastinput != AST_FORMAT_ULAW) {
422 p->lastinput = AST_FORMAT_ULAW;
423 if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
424 ast_log(LOG_WARNING, "Failed to set codec to uLaw\n");
425 return -1;
428 } else if (p->mode == MODE_FXS) {
429 ioctl(p->fd, PHONE_REC_STOP);
430 if (p->lastinput != ast->rawreadformat) {
431 p->lastinput = ast->rawreadformat;
432 if (ioctl(p->fd, PHONE_REC_CODEC, ast->rawreadformat)) {
433 ast_log(LOG_WARNING, "Failed to set codec to %d\n",
434 ast->rawreadformat);
435 return -1;
438 } else {
439 ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast->rawreadformat));
440 return -1;
442 if (ioctl(p->fd, PHONE_REC_START)) {
443 ast_log(LOG_WARNING, "Failed to start recording\n");
444 return -1;
446 /* set the DTMF times (the default is too short) */
447 ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
448 ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
449 return 0;
452 static int phone_answer(struct ast_channel *ast)
454 struct phone_pvt *p;
455 p = ast->tech_pvt;
456 /* In case it's a LineJack, take it off hook */
457 if (p->mode == MODE_FXO) {
458 if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_OFF_HOOK))
459 ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n", ast->name, strerror(errno));
460 else
461 ast_log(LOG_DEBUG, "Took linejack off hook\n");
463 phone_setup(ast);
464 if (option_debug)
465 ast_log(LOG_DEBUG, "phone_answer(%s)\n", ast->name);
466 ast->rings = 0;
467 ast_setstate(ast, AST_STATE_UP);
468 return 0;
471 #if 0
472 static char phone_2digit(char c)
474 if (c == 12)
475 return '#';
476 else if (c == 11)
477 return '*';
478 else if ((c < 10) && (c >= 0))
479 return '0' + c - 1;
480 else
481 return '?';
483 #endif
485 static struct ast_frame *phone_exception(struct ast_channel *ast)
487 int res;
488 union telephony_exception phonee;
489 struct phone_pvt *p = ast->tech_pvt;
490 char digit;
492 /* Some nice norms */
493 p->fr.datalen = 0;
494 p->fr.samples = 0;
495 p->fr.data = NULL;
496 p->fr.src = "Phone";
497 p->fr.offset = 0;
498 p->fr.mallocd=0;
499 p->fr.delivery = ast_tv(0,0);
501 phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
502 if (phonee.bits.dtmf_ready) {
503 if (option_debug)
504 ast_log(LOG_DEBUG, "phone_exception(): DTMF\n");
506 /* We've got a digit -- Just handle this nicely and easily */
507 digit = ioctl(p->fd, PHONE_GET_DTMF_ASCII);
508 p->fr.subclass = digit;
509 p->fr.frametype = AST_FRAME_DTMF;
510 return &p->fr;
512 if (phonee.bits.hookstate) {
513 if (option_debug)
514 ast_log(LOG_DEBUG, "Hookstate changed\n");
515 res = ioctl(p->fd, PHONE_HOOKSTATE);
516 /* See if we've gone on hook, if so, notify by returning NULL */
517 if (option_debug)
518 ast_log(LOG_DEBUG, "New hookstate: %d\n", res);
519 if (!res && (p->mode != MODE_FXO))
520 return NULL;
521 else {
522 if (ast->_state == AST_STATE_RINGING) {
523 /* They've picked up the phone */
524 p->fr.frametype = AST_FRAME_CONTROL;
525 p->fr.subclass = AST_CONTROL_ANSWER;
526 phone_setup(ast);
527 ast_setstate(ast, AST_STATE_UP);
528 return &p->fr;
529 } else
530 ast_log(LOG_WARNING, "Got off hook in weird state %d\n", ast->_state);
533 #if 1
534 if (phonee.bits.pstn_ring)
535 ast_verbose("Unit is ringing\n");
536 if (phonee.bits.caller_id) {
537 ast_verbose("We have caller ID\n");
539 if (phonee.bits.pstn_wink)
540 ast_verbose("Detected Wink\n");
541 #endif
542 /* Strange -- nothing there.. */
543 p->fr.frametype = AST_FRAME_NULL;
544 p->fr.subclass = 0;
545 return &p->fr;
548 static struct ast_frame *phone_read(struct ast_channel *ast)
550 int res;
551 struct phone_pvt *p = ast->tech_pvt;
554 /* Some nice norms */
555 p->fr.datalen = 0;
556 p->fr.samples = 0;
557 p->fr.data = NULL;
558 p->fr.src = "Phone";
559 p->fr.offset = 0;
560 p->fr.mallocd=0;
561 p->fr.delivery = ast_tv(0,0);
563 /* Try to read some data... */
564 CHECK_BLOCKING(ast);
565 res = read(p->fd, p->buf, PHONE_MAX_BUF);
566 ast_clear_flag(ast, AST_FLAG_BLOCKING);
567 if (res < 0) {
568 #if 0
569 if (errno == EAGAIN) {
570 ast_log(LOG_WARNING, "Null frame received\n");
571 p->fr.frametype = AST_FRAME_NULL;
572 p->fr.subclass = 0;
573 return &p->fr;
575 #endif
576 ast_log(LOG_WARNING, "Error reading: %s\n", strerror(errno));
577 return NULL;
579 p->fr.data = p->buf;
580 if (p->mode != MODE_FXS)
581 switch(p->buf[0] & 0x3) {
582 case '0':
583 case '1':
584 /* Normal */
585 break;
586 case '2':
587 case '3':
588 /* VAD/CNG, only send two words */
589 res = 4;
590 break;
592 p->fr.samples = 240;
593 p->fr.datalen = res;
594 p->fr.frametype = p->lastinput <= AST_FORMAT_MAX_AUDIO ?
595 AST_FRAME_VOICE :
596 p->lastinput <= AST_FORMAT_PNG ? AST_FRAME_IMAGE
597 : AST_FRAME_VIDEO;
598 p->fr.subclass = p->lastinput;
599 p->fr.offset = AST_FRIENDLY_OFFSET;
600 /* Byteswap from little-endian to native-endian */
601 if (p->fr.subclass == AST_FORMAT_SLINEAR)
602 ast_frame_byteswap_le(&p->fr);
603 return &p->fr;
606 static int phone_write_buf(struct phone_pvt *p, const char *buf, int len, int frlen, int swap)
608 int res;
609 /* Store as much of the buffer as we can, then write fixed frames */
610 int space = sizeof(p->obuf) - p->obuflen;
611 /* Make sure we have enough buffer space to store the frame */
612 if (space < len)
613 len = space;
614 if (swap)
615 ast_swapcopy_samples(p->obuf+p->obuflen, buf, len/2);
616 else
617 memcpy(p->obuf + p->obuflen, buf, len);
618 p->obuflen += len;
619 while(p->obuflen > frlen) {
620 res = write(p->fd, p->obuf, frlen);
621 if (res != frlen) {
622 if (res < 1) {
624 * Card is in non-blocking mode now and it works well now, but there are
625 * lot of messages like this. So, this message is temporarily disabled.
627 return 0;
628 } else {
629 ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
632 p->obuflen -= frlen;
633 /* Move memory if necessary */
634 if (p->obuflen)
635 memmove(p->obuf, p->obuf + frlen, p->obuflen);
637 return len;
640 static int phone_send_text(struct ast_channel *ast, const char *text)
642 int length = strlen(text);
643 return phone_write_buf(ast->tech_pvt, text, length, length, 0) ==
644 length ? 0 : -1;
647 static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
649 struct phone_pvt *p = ast->tech_pvt;
650 int res;
651 int maxfr=0;
652 char *pos;
653 int sofar;
654 int expected;
655 int codecset = 0;
656 char tmpbuf[4];
657 /* Write a frame of (presumably voice) data */
658 if (frame->frametype != AST_FRAME_VOICE && p->mode != MODE_FXS) {
659 if (frame->frametype != AST_FRAME_IMAGE)
660 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
661 return 0;
663 if (!(frame->subclass &
664 (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW)) &&
665 p->mode != MODE_FXS) {
666 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
667 return -1;
669 #if 0
670 /* If we're not in up mode, go into up mode now */
671 if (ast->_state != AST_STATE_UP) {
672 ast_setstate(ast, AST_STATE_UP);
673 phone_setup(ast);
675 #else
676 if (ast->_state != AST_STATE_UP) {
677 /* Don't try tos end audio on-hook */
678 return 0;
680 #endif
681 if (frame->subclass == AST_FORMAT_G723_1) {
682 if (p->lastformat != AST_FORMAT_G723_1) {
683 ioctl(p->fd, PHONE_PLAY_STOP);
684 ioctl(p->fd, PHONE_REC_STOP);
685 if (ioctl(p->fd, PHONE_PLAY_CODEC, G723_63)) {
686 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
687 return -1;
689 if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
690 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
691 return -1;
693 p->lastformat = AST_FORMAT_G723_1;
694 p->lastinput = AST_FORMAT_G723_1;
695 /* Reset output buffer */
696 p->obuflen = 0;
697 codecset = 1;
699 if (frame->datalen > 24) {
700 ast_log(LOG_WARNING, "Frame size too large for G.723.1 (%d bytes)\n", frame->datalen);
701 return -1;
703 maxfr = 24;
704 } else if (frame->subclass == AST_FORMAT_SLINEAR) {
705 if (p->lastformat != AST_FORMAT_SLINEAR) {
706 ioctl(p->fd, PHONE_PLAY_STOP);
707 ioctl(p->fd, PHONE_REC_STOP);
708 if (ioctl(p->fd, PHONE_PLAY_CODEC, LINEAR16)) {
709 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
710 return -1;
712 if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
713 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
714 return -1;
716 p->lastformat = AST_FORMAT_SLINEAR;
717 p->lastinput = AST_FORMAT_SLINEAR;
718 codecset = 1;
719 /* Reset output buffer */
720 p->obuflen = 0;
722 maxfr = 480;
723 } else if (frame->subclass == AST_FORMAT_ULAW) {
724 if (p->lastformat != AST_FORMAT_ULAW) {
725 ioctl(p->fd, PHONE_PLAY_STOP);
726 ioctl(p->fd, PHONE_REC_STOP);
727 if (ioctl(p->fd, PHONE_PLAY_CODEC, ULAW)) {
728 ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
729 return -1;
731 if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
732 ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
733 return -1;
735 p->lastformat = AST_FORMAT_ULAW;
736 p->lastinput = AST_FORMAT_ULAW;
737 codecset = 1;
738 /* Reset output buffer */
739 p->obuflen = 0;
741 maxfr = 240;
742 } else {
743 if (p->lastformat != frame->subclass) {
744 ioctl(p->fd, PHONE_PLAY_STOP);
745 ioctl(p->fd, PHONE_REC_STOP);
746 if (ioctl(p->fd, PHONE_PLAY_CODEC, frame->subclass)) {
747 ast_log(LOG_WARNING, "Unable to set %d mode\n",
748 frame->subclass);
749 return -1;
751 if (ioctl(p->fd, PHONE_REC_CODEC, frame->subclass)) {
752 ast_log(LOG_WARNING, "Unable to set %d mode\n",
753 frame->subclass);
754 return -1;
756 p->lastformat = frame->subclass;
757 p->lastinput = frame->subclass;
758 codecset = 1;
759 /* Reset output buffer */
760 p->obuflen = 0;
762 maxfr = 480;
764 if (codecset) {
765 ioctl(p->fd, PHONE_REC_DEPTH, 3);
766 ioctl(p->fd, PHONE_PLAY_DEPTH, 3);
767 if (ioctl(p->fd, PHONE_PLAY_START)) {
768 ast_log(LOG_WARNING, "Failed to start playback\n");
769 return -1;
771 if (ioctl(p->fd, PHONE_REC_START)) {
772 ast_log(LOG_WARNING, "Failed to start recording\n");
773 return -1;
776 /* If we get here, we have a frame of Appropriate data */
777 sofar = 0;
778 pos = frame->data;
779 while(sofar < frame->datalen) {
780 /* Write in no more than maxfr sized frames */
781 expected = frame->datalen - sofar;
782 if (maxfr < expected)
783 expected = maxfr;
784 /* XXX Internet Phone Jack does not handle the 4-byte VAD frame properly! XXX
785 we have to pad it to 24 bytes still. */
786 if (frame->datalen == 4) {
787 if (p->silencesupression) {
788 memset(tmpbuf + 4, 0, sizeof(tmpbuf) - 4);
789 memcpy(tmpbuf, frame->data, 4);
790 expected = 24;
791 res = phone_write_buf(p, tmpbuf, expected, maxfr, 0);
793 res = 4;
794 expected=4;
795 } else {
796 int swap = 0;
797 #if __BYTE_ORDER == __BIG_ENDIAN
798 if (frame->subclass == AST_FORMAT_SLINEAR)
799 swap = 1; /* Swap big-endian samples to little-endian as we copy */
800 #endif
801 res = phone_write_buf(p, pos, expected, maxfr, swap);
803 if (res != expected) {
804 if ((errno != EAGAIN) && (errno != EINTR)) {
805 if (res < 0)
806 ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
808 * Card is in non-blocking mode now and it works well now, but there are
809 * lot of messages like this. So, this message is temporarily disabled.
811 #if 0
812 else
813 ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
814 #endif
815 return -1;
816 } else /* Pretend it worked */
817 res = expected;
819 sofar += res;
820 pos += res;
822 return 0;
825 static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *context)
827 struct ast_channel *tmp;
828 struct phone_codec_data codec;
829 tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, "", i->ext, i->context, 0, "Phone/%s", i->dev + 5);
830 if (tmp) {
831 tmp->tech = cur_tech;
832 tmp->fds[0] = i->fd;
833 /* XXX Switching formats silently causes kernel panics XXX */
834 if (i->mode == MODE_FXS &&
835 ioctl(i->fd, PHONE_QUERY_CODEC, &codec) == 0) {
836 if (codec.type == LINEAR16)
837 tmp->nativeformats =
838 tmp->rawreadformat =
839 tmp->rawwriteformat =
840 AST_FORMAT_SLINEAR;
841 else {
842 tmp->nativeformats =
843 tmp->rawreadformat =
844 tmp->rawwriteformat =
845 prefformat & ~AST_FORMAT_SLINEAR;
848 else {
849 tmp->nativeformats = prefformat;
850 tmp->rawreadformat = prefformat;
851 tmp->rawwriteformat = prefformat;
853 /* no need to call ast_setstate: the channel_alloc already did its job */
854 if (state == AST_STATE_RING)
855 tmp->rings = 1;
856 tmp->tech_pvt = i;
857 ast_copy_string(tmp->context, context, sizeof(tmp->context));
858 if (!ast_strlen_zero(i->ext))
859 ast_copy_string(tmp->exten, i->ext, sizeof(tmp->exten));
860 else
861 strcpy(tmp->exten, "s");
862 if (!ast_strlen_zero(i->language))
863 ast_string_field_set(tmp, language, i->language);
865 /* Don't use ast_set_callerid() here because it will
866 * generate a NewCallerID event before the NewChannel event */
867 tmp->cid.cid_num = ast_strdup(i->cid_num);
868 tmp->cid.cid_ani = ast_strdup(i->cid_num);
869 tmp->cid.cid_name = ast_strdup(i->cid_name);
871 i->owner = tmp;
872 ast_module_ref(ast_module_info->self);
873 if (state != AST_STATE_DOWN) {
874 if (state == AST_STATE_RING) {
875 ioctl(tmp->fds[0], PHONE_RINGBACK);
876 i->cpt = 1;
878 if (ast_pbx_start(tmp)) {
879 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
880 ast_hangup(tmp);
883 } else
884 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
885 return tmp;
888 static void phone_mini_packet(struct phone_pvt *i)
890 int res;
891 char buf[1024];
892 /* Ignore stuff we read... */
893 res = read(i->fd, buf, sizeof(buf));
894 if (res < 1) {
895 ast_log(LOG_WARNING, "Read returned %d: %s\n", res, strerror(errno));
896 return;
900 static void phone_check_exception(struct phone_pvt *i)
902 int offhook=0;
903 char digit[2] = {0 , 0};
904 union telephony_exception phonee;
905 /* XXX Do something XXX */
906 #if 0
907 ast_log(LOG_DEBUG, "Exception!\n");
908 #endif
909 phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION);
910 if (phonee.bits.dtmf_ready) {
911 digit[0] = ioctl(i->fd, PHONE_GET_DTMF_ASCII);
912 if (i->mode == MODE_DIALTONE || i->mode == MODE_FXS || i->mode == MODE_SIGMA) {
913 ioctl(i->fd, PHONE_PLAY_STOP);
914 ioctl(i->fd, PHONE_REC_STOP);
915 ioctl(i->fd, PHONE_CPT_STOP);
916 i->dialtone = 0;
917 if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
918 strncat(i->ext, digit, sizeof(i->ext) - strlen(i->ext) - 1);
919 if ((i->mode != MODE_FXS ||
920 !(phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION)) ||
921 !phonee.bits.dtmf_ready) &&
922 ast_exists_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
923 /* It's a valid extension in its context, get moving! */
924 phone_new(i, AST_STATE_RING, i->context);
925 /* No need to restart monitor, we are the monitor */
926 } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
927 /* There is nothing in the specified extension that can match anymore.
928 Try the default */
929 if (ast_exists_extension(NULL, "default", i->ext, 1, i->cid_num)) {
930 /* Check the default, too... */
931 phone_new(i, AST_STATE_RING, "default");
932 /* XXX This should probably be justified better XXX */
933 } else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->cid_num)) {
934 /* It's not a valid extension, give a busy signal */
935 if (option_debug)
936 ast_log(LOG_DEBUG, "%s can't match anything in %s or default\n", i->ext, i->context);
937 ioctl(i->fd, PHONE_BUSY);
938 i->cpt = 1;
941 #if 0
942 ast_verbose("Extension is %s\n", i->ext);
943 #endif
946 if (phonee.bits.hookstate) {
947 offhook = ioctl(i->fd, PHONE_HOOKSTATE);
948 if (offhook) {
949 if (i->mode == MODE_IMMEDIATE) {
950 phone_new(i, AST_STATE_RING, i->context);
951 } else if (i->mode == MODE_DIALTONE) {
952 ast_module_ref(ast_module_info->self);
953 /* Reset the extension */
954 i->ext[0] = '\0';
955 /* Play the dialtone */
956 i->dialtone++;
957 ioctl(i->fd, PHONE_PLAY_STOP);
958 ioctl(i->fd, PHONE_PLAY_CODEC, ULAW);
959 ioctl(i->fd, PHONE_PLAY_START);
960 i->lastformat = -1;
961 } else if (i->mode == MODE_SIGMA) {
962 ast_module_ref(ast_module_info->self);
963 /* Reset the extension */
964 i->ext[0] = '\0';
965 /* Play the dialtone */
966 i->dialtone++;
967 ioctl(i->fd, PHONE_DIALTONE);
969 } else {
970 if (i->dialtone)
971 ast_module_unref(ast_module_info->self);
972 memset(i->ext, 0, sizeof(i->ext));
973 if (i->cpt)
975 ioctl(i->fd, PHONE_CPT_STOP);
976 i->cpt = 0;
978 ioctl(i->fd, PHONE_PLAY_STOP);
979 ioctl(i->fd, PHONE_REC_STOP);
980 i->dialtone = 0;
981 i->lastformat = -1;
984 if (phonee.bits.pstn_ring) {
985 ast_verbose("Unit is ringing\n");
986 phone_new(i, AST_STATE_RING, i->context);
988 if (phonee.bits.caller_id)
989 ast_verbose("We have caller ID\n");
994 static void *do_monitor(void *data)
996 fd_set rfds, efds;
997 int n, res;
998 struct phone_pvt *i;
999 int tonepos = 0;
1000 /* The tone we're playing this round */
1001 struct timeval tv = {0,0};
1002 int dotone;
1003 /* This thread monitors all the frame relay interfaces which are not yet in use
1004 (and thus do not have a separate thread) indefinitely */
1005 while (monitor) {
1006 /* Don't let anybody kill us right away. Nobody should lock the interface list
1007 and wait for the monitor list, but the other way around is okay. */
1008 /* Lock the interface list */
1009 if (ast_mutex_lock(&iflock)) {
1010 ast_log(LOG_ERROR, "Unable to grab interface lock\n");
1011 return NULL;
1013 /* Build the stuff we're going to select on, that is the socket of every
1014 phone_pvt that does not have an associated owner channel */
1015 n = -1;
1016 FD_ZERO(&rfds);
1017 FD_ZERO(&efds);
1018 i = iflist;
1019 dotone = 0;
1020 while (i) {
1021 if (FD_ISSET(i->fd, &rfds))
1022 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
1023 if (!i->owner) {
1024 /* This needs to be watched, as it lacks an owner */
1025 FD_SET(i->fd, &rfds);
1026 FD_SET(i->fd, &efds);
1027 if (i->fd > n)
1028 n = i->fd;
1029 if (i->dialtone && i->mode != MODE_SIGMA) {
1030 /* Remember we're going to have to come back and play
1031 more dialtones */
1032 if (ast_tvzero(tv)) {
1033 /* If we're due for a dialtone, play one */
1034 if (write(i->fd, DialTone + tonepos, 240) != 240)
1035 ast_log(LOG_WARNING, "Dial tone write error\n");
1037 dotone++;
1041 i = i->next;
1043 /* Okay, now that we know what to do, release the interface lock */
1044 ast_mutex_unlock(&iflock);
1046 /* Wait indefinitely for something to happen */
1047 if (dotone && i->mode != MODE_SIGMA) {
1048 /* If we're ready to recycle the time, set it to 30 ms */
1049 tonepos += 240;
1050 if (tonepos >= sizeof(DialTone))
1051 tonepos = 0;
1052 if (ast_tvzero(tv)) {
1053 tv = ast_tv(30000, 0);
1055 res = ast_select(n + 1, &rfds, NULL, &efds, &tv);
1056 } else {
1057 res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
1058 tv = ast_tv(0,0);
1059 tonepos = 0;
1061 /* Okay, select has finished. Let's see what happened. */
1062 if (res < 0) {
1063 ast_log(LOG_DEBUG, "select return %d: %s\n", res, strerror(errno));
1064 continue;
1066 /* If there are no fd's changed, just continue, it's probably time
1067 to play some more dialtones */
1068 if (!res)
1069 continue;
1070 /* Alright, lock the interface list again, and let's look and see what has
1071 happened */
1072 if (ast_mutex_lock(&iflock)) {
1073 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1074 continue;
1077 i = iflist;
1078 for(; i; i=i->next) {
1079 if (FD_ISSET(i->fd, &rfds)) {
1080 if (i->owner) {
1081 continue;
1083 phone_mini_packet(i);
1085 if (FD_ISSET(i->fd, &efds)) {
1086 if (i->owner) {
1087 continue;
1089 phone_check_exception(i);
1092 ast_mutex_unlock(&iflock);
1094 return NULL;
1098 static int restart_monitor()
1100 /* If we're supposed to be stopped -- stay stopped */
1101 if (monitor_thread == AST_PTHREADT_STOP)
1102 return 0;
1103 if (ast_mutex_lock(&monlock)) {
1104 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1105 return -1;
1107 if (monitor_thread == pthread_self()) {
1108 ast_mutex_unlock(&monlock);
1109 ast_log(LOG_WARNING, "Cannot kill myself\n");
1110 return -1;
1112 if (monitor_thread != AST_PTHREADT_NULL) {
1113 if (ast_mutex_lock(&iflock)) {
1114 ast_mutex_unlock(&monlock);
1115 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1116 return -1;
1118 monitor = 0;
1119 while (pthread_kill(monitor_thread, SIGURG) == 0)
1120 sched_yield();
1121 pthread_join(monitor_thread, NULL);
1122 ast_mutex_unlock(&iflock);
1124 monitor = 1;
1125 /* Start a new monitor */
1126 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
1127 ast_mutex_unlock(&monlock);
1128 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1129 return -1;
1131 ast_mutex_unlock(&monlock);
1132 return 0;
1135 static struct phone_pvt *mkif(char *iface, int mode, int txgain, int rxgain)
1137 /* Make a phone_pvt structure for this interface */
1138 struct phone_pvt *tmp;
1139 int flags;
1141 tmp = malloc(sizeof(struct phone_pvt));
1142 if (tmp) {
1143 tmp->fd = open(iface, O_RDWR);
1144 if (tmp->fd < 0) {
1145 ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
1146 free(tmp);
1147 return NULL;
1149 if (mode == MODE_FXO) {
1150 if (ioctl(tmp->fd, IXJCTL_PORT, PORT_PSTN))
1151 ast_log(LOG_DEBUG, "Unable to set port to PSTN\n");
1152 } else {
1153 if (ioctl(tmp->fd, IXJCTL_PORT, PORT_POTS))
1154 if (mode != MODE_FXS)
1155 ast_log(LOG_DEBUG, "Unable to set port to POTS\n");
1157 ioctl(tmp->fd, PHONE_PLAY_STOP);
1158 ioctl(tmp->fd, PHONE_REC_STOP);
1159 ioctl(tmp->fd, PHONE_RING_STOP);
1160 ioctl(tmp->fd, PHONE_CPT_STOP);
1161 if (ioctl(tmp->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
1162 ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",iface, strerror(errno));
1163 if (echocancel != AEC_OFF)
1164 ioctl(tmp->fd, IXJCTL_AEC_START, echocancel);
1165 if (silencesupression)
1166 tmp->silencesupression = 1;
1167 #ifdef PHONE_VAD
1168 ioctl(tmp->fd, PHONE_VAD, tmp->silencesupression);
1169 #endif
1170 tmp->mode = mode;
1171 flags = fcntl(tmp->fd, F_GETFL);
1172 fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
1173 tmp->owner = NULL;
1174 tmp->lastformat = -1;
1175 tmp->lastinput = -1;
1176 tmp->ministate = 0;
1177 memset(tmp->ext, 0, sizeof(tmp->ext));
1178 ast_copy_string(tmp->language, language, sizeof(tmp->language));
1179 ast_copy_string(tmp->dev, iface, sizeof(tmp->dev));
1180 ast_copy_string(tmp->context, context, sizeof(tmp->context));
1181 tmp->next = NULL;
1182 tmp->obuflen = 0;
1183 tmp->dialtone = 0;
1184 tmp->cpt = 0;
1185 ast_copy_string(tmp->cid_num, cid_num, sizeof(tmp->cid_num));
1186 ast_copy_string(tmp->cid_name, cid_name, sizeof(tmp->cid_name));
1187 tmp->txgain = txgain;
1188 ioctl(tmp->fd, PHONE_PLAY_VOLUME, tmp->txgain);
1189 tmp->rxgain = rxgain;
1190 ioctl(tmp->fd, PHONE_REC_VOLUME, tmp->rxgain);
1192 return tmp;
1195 static struct ast_channel *phone_request(const char *type, int format, void *data, int *cause)
1197 int oldformat;
1198 struct phone_pvt *p;
1199 struct ast_channel *tmp = NULL;
1200 char *name = data;
1202 /* Search for an unowned channel */
1203 if (ast_mutex_lock(&iflock)) {
1204 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1205 return NULL;
1207 p = iflist;
1208 while(p) {
1209 if (p->mode == MODE_FXS ||
1210 format & (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW)) {
1211 size_t length = strlen(p->dev + 5);
1212 if (strncmp(name, p->dev + 5, length) == 0 &&
1213 !isalnum(name[length])) {
1214 if (!p->owner) {
1215 tmp = phone_new(p, AST_STATE_DOWN, p->context);
1216 break;
1217 } else
1218 *cause = AST_CAUSE_BUSY;
1221 p = p->next;
1223 ast_mutex_unlock(&iflock);
1224 restart_monitor();
1225 if (tmp == NULL) {
1226 oldformat = format;
1227 format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
1228 if (!format) {
1229 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
1230 return NULL;
1233 return tmp;
1236 /* parse gain value from config file */
1237 static int parse_gain_value(char *gain_type, char *value)
1239 float gain;
1241 /* try to scan number */
1242 if (sscanf(value, "%f", &gain) != 1)
1244 ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n",
1245 value, gain_type, config);
1246 return DEFAULT_GAIN;
1249 /* multiplicate gain by 1.0 gain value */
1250 gain = gain * (float)DEFAULT_GAIN;
1252 /* percentage? */
1253 if (value[strlen(value) - 1] == '%')
1254 return (int)(gain / (float)100);
1256 return (int)gain;
1259 static int __unload_module(void)
1261 struct phone_pvt *p, *pl;
1262 /* First, take us out of the channel loop */
1263 ast_channel_unregister(cur_tech);
1264 if (!ast_mutex_lock(&iflock)) {
1265 /* Hangup all interfaces if they have an owner */
1266 p = iflist;
1267 while(p) {
1268 if (p->owner)
1269 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1270 p = p->next;
1272 iflist = NULL;
1273 ast_mutex_unlock(&iflock);
1274 } else {
1275 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1276 return -1;
1278 if (!ast_mutex_lock(&monlock)) {
1279 if (monitor_thread > AST_PTHREADT_NULL) {
1280 monitor = 0;
1281 while (pthread_kill(monitor_thread, SIGURG) == 0)
1282 sched_yield();
1283 pthread_join(monitor_thread, NULL);
1285 monitor_thread = AST_PTHREADT_STOP;
1286 ast_mutex_unlock(&monlock);
1287 } else {
1288 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1289 return -1;
1292 if (!ast_mutex_lock(&iflock)) {
1293 /* Destroy all the interfaces and free their memory */
1294 p = iflist;
1295 while(p) {
1296 /* Close the socket, assuming it's real */
1297 if (p->fd > -1)
1298 close(p->fd);
1299 pl = p;
1300 p = p->next;
1301 /* Free associated memory */
1302 free(pl);
1304 iflist = NULL;
1305 ast_mutex_unlock(&iflock);
1306 } else {
1307 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1308 return -1;
1311 return 0;
1314 static int unload_module(void)
1316 return __unload_module();
1319 static int load_module(void)
1321 struct ast_config *cfg;
1322 struct ast_variable *v;
1323 struct phone_pvt *tmp;
1324 int mode = MODE_IMMEDIATE;
1325 int txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; /* default gain 1.0 */
1326 cfg = ast_config_load(config);
1328 /* We *must* have a config file otherwise stop immediately */
1329 if (!cfg) {
1330 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1331 return AST_MODULE_LOAD_DECLINE;
1333 if (ast_mutex_lock(&iflock)) {
1334 /* It's a little silly to lock it, but we mind as well just to be sure */
1335 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1336 return -1;
1338 v = ast_variable_browse(cfg, "interfaces");
1339 while(v) {
1340 /* Create the interface list */
1341 if (!strcasecmp(v->name, "device")) {
1342 tmp = mkif(v->value, mode, txgain, rxgain);
1343 if (tmp) {
1344 tmp->next = iflist;
1345 iflist = tmp;
1347 } else {
1348 ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
1349 ast_config_destroy(cfg);
1350 ast_mutex_unlock(&iflock);
1351 __unload_module();
1352 return -1;
1354 } else if (!strcasecmp(v->name, "silencesupression")) {
1355 silencesupression = ast_true(v->value);
1356 } else if (!strcasecmp(v->name, "language")) {
1357 ast_copy_string(language, v->value, sizeof(language));
1358 } else if (!strcasecmp(v->name, "callerid")) {
1359 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
1360 } else if (!strcasecmp(v->name, "mode")) {
1361 if (!strncasecmp(v->value, "di", 2))
1362 mode = MODE_DIALTONE;
1363 else if (!strncasecmp(v->value, "sig", 3))
1364 mode = MODE_SIGMA;
1365 else if (!strncasecmp(v->value, "im", 2))
1366 mode = MODE_IMMEDIATE;
1367 else if (!strncasecmp(v->value, "fxs", 3)) {
1368 mode = MODE_FXS;
1369 prefformat = 0x01ff0000; /* All non-voice */
1371 else if (!strncasecmp(v->value, "fx", 2))
1372 mode = MODE_FXO;
1373 else
1374 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
1375 } else if (!strcasecmp(v->name, "context")) {
1376 ast_copy_string(context, v->value, sizeof(context));
1377 } else if (!strcasecmp(v->name, "format")) {
1378 if (!strcasecmp(v->value, "g723.1")) {
1379 prefformat = AST_FORMAT_G723_1;
1380 } else if (!strcasecmp(v->value, "slinear")) {
1381 if (mode == MODE_FXS)
1382 prefformat |= AST_FORMAT_SLINEAR;
1383 else prefformat = AST_FORMAT_SLINEAR;
1384 } else if (!strcasecmp(v->value, "ulaw")) {
1385 prefformat = AST_FORMAT_ULAW;
1386 } else
1387 ast_log(LOG_WARNING, "Unknown format '%s'\n", v->value);
1388 } else if (!strcasecmp(v->name, "echocancel")) {
1389 if (!strcasecmp(v->value, "off")) {
1390 echocancel = AEC_OFF;
1391 } else if (!strcasecmp(v->value, "low")) {
1392 echocancel = AEC_LOW;
1393 } else if (!strcasecmp(v->value, "medium")) {
1394 echocancel = AEC_MED;
1395 } else if (!strcasecmp(v->value, "high")) {
1396 echocancel = AEC_HIGH;
1397 } else
1398 ast_log(LOG_WARNING, "Unknown echo cancellation '%s'\n", v->value);
1399 } else if (!strcasecmp(v->name, "txgain")) {
1400 txgain = parse_gain_value(v->name, v->value);
1401 } else if (!strcasecmp(v->name, "rxgain")) {
1402 rxgain = parse_gain_value(v->name, v->value);
1404 v = v->next;
1406 ast_mutex_unlock(&iflock);
1408 if (mode == MODE_FXS) {
1409 phone_tech_fxs.capabilities = prefformat;
1410 cur_tech = &phone_tech_fxs;
1411 } else
1412 cur_tech = (struct ast_channel_tech *) &phone_tech;
1414 /* Make sure we can register our Adtranphone channel type */
1416 if (ast_channel_register(cur_tech)) {
1417 ast_log(LOG_ERROR, "Unable to register channel class 'Phone'\n");
1418 ast_config_destroy(cfg);
1419 __unload_module();
1420 return -1;
1422 ast_config_destroy(cfg);
1423 /* And start the monitor for the first time */
1424 restart_monitor();
1425 return 0;
1428 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Linux Telephony API Support");