revert the dynamic_list insertion change... that was not the right thing to do
[asterisk-bristuff.git] / channels / chan_phone.c
blob73113d57e3f499d52a91bad7f7cd1a55a26c9f86
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] = "";
96 static int usecnt =0;
98 static int echocancel = AEC_OFF;
100 static int silencesupression = 0;
102 static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW;
104 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
106 /* Protect the interface list (of phone_pvt's) */
107 AST_MUTEX_DEFINE_STATIC(iflock);
109 /* Protect the monitoring thread, so only one process can kill or start it, and not
110 when it's doing something critical. */
111 AST_MUTEX_DEFINE_STATIC(monlock);
113 /* Boolean value whether the monitoring thread shall continue. */
114 static unsigned int monitor;
116 /* This is the thread for the monitor which checks for input on the channels
117 which are not currently in use. */
118 static pthread_t monitor_thread = AST_PTHREADT_NULL;
120 static int restart_monitor(void);
122 /* The private structures of the Phone Jack channels are linked for
123 selecting outgoing channels */
125 #define MODE_DIALTONE 1
126 #define MODE_IMMEDIATE 2
127 #define MODE_FXO 3
128 #define MODE_FXS 4
129 #define MODE_SIGMA 5
131 static struct phone_pvt {
132 int fd; /* Raw file descriptor for this device */
133 struct ast_channel *owner; /* Channel we belong to, possibly NULL */
134 int mode; /* Is this in the */
135 int lastformat; /* Last output format */
136 int lastinput; /* Last input format */
137 int ministate; /* Miniature state, for dialtone mode */
138 char dev[256]; /* Device name */
139 struct phone_pvt *next; /* Next channel in list */
140 struct ast_frame fr; /* Frame */
141 char offset[AST_FRIENDLY_OFFSET];
142 char buf[PHONE_MAX_BUF]; /* Static buffer for reading frames */
143 int obuflen;
144 int dialtone;
145 int txgain, rxgain; /* gain control for playing, recording */
146 /* 0x100 - 1.0, 0x200 - 2.0, 0x80 - 0.5 */
147 int cpt; /* Call Progress Tone playing? */
148 int silencesupression;
149 char context[AST_MAX_EXTENSION];
150 char obuf[PHONE_MAX_BUF * 2];
151 char ext[AST_MAX_EXTENSION];
152 char language[MAX_LANGUAGE];
153 char cid_num[AST_MAX_EXTENSION];
154 char cid_name[AST_MAX_EXTENSION];
155 } *iflist = NULL;
157 static char cid_num[AST_MAX_EXTENSION];
158 static char cid_name[AST_MAX_EXTENSION];
160 static struct ast_channel *phone_request(const char *type, int format, void *data, int *cause);
161 static int phone_digit_begin(struct ast_channel *ast, char digit);
162 static int phone_digit_end(struct ast_channel *ast, char digit);
163 static int phone_call(struct ast_channel *ast, char *dest, int timeout);
164 static int phone_hangup(struct ast_channel *ast);
165 static int phone_answer(struct ast_channel *ast);
166 static struct ast_frame *phone_read(struct ast_channel *ast);
167 static int phone_write(struct ast_channel *ast, struct ast_frame *frame);
168 static struct ast_frame *phone_exception(struct ast_channel *ast);
169 static int phone_send_text(struct ast_channel *ast, const char *text);
170 static int phone_fixup(struct ast_channel *old, struct ast_channel *new);
171 static int phone_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
173 static const struct ast_channel_tech phone_tech = {
174 .type = "Phone",
175 .description = tdesc,
176 .capabilities = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW,
177 .requester = phone_request,
178 .send_digit_begin = phone_digit_begin,
179 .send_digit_end = phone_digit_end,
180 .call = phone_call,
181 .hangup = phone_hangup,
182 .answer = phone_answer,
183 .read = phone_read,
184 .write = phone_write,
185 .exception = phone_exception,
186 .indicate = phone_indicate,
187 .fixup = phone_fixup
190 static struct ast_channel_tech phone_tech_fxs = {
191 .type = "Phone",
192 .description = tdesc,
193 .requester = phone_request,
194 .send_digit_begin = phone_digit_begin,
195 .send_digit_end = phone_digit_end,
196 .call = phone_call,
197 .hangup = phone_hangup,
198 .answer = phone_answer,
199 .read = phone_read,
200 .write = phone_write,
201 .exception = phone_exception,
202 .write_video = phone_write,
203 .send_text = phone_send_text,
204 .indicate = phone_indicate,
205 .fixup = phone_fixup
208 static struct ast_channel_tech *cur_tech;
210 static int phone_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
212 struct phone_pvt *p = chan->tech_pvt;
213 int res=-1;
214 ast_log(LOG_DEBUG, "Requested indication %d on channel %s\n", condition, chan->name);
215 switch(condition) {
216 case AST_CONTROL_FLASH:
217 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
218 usleep(320000);
219 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
220 p->lastformat = -1;
221 res = 0;
222 break;
223 case AST_CONTROL_HOLD:
224 ast_moh_start(chan, data, NULL);
225 break;
226 case AST_CONTROL_UNHOLD:
227 ast_moh_stop(chan);
228 break;
229 default:
230 ast_log(LOG_WARNING, "Condition %d is not supported on channel %s\n", condition, chan->name);
232 return res;
235 static int phone_fixup(struct ast_channel *old, struct ast_channel *new)
237 struct phone_pvt *pvt = old->tech_pvt;
238 if (pvt && pvt->owner == old)
239 pvt->owner = new;
240 return 0;
243 static int phone_digit_begin(struct ast_channel *chan, char digit)
245 /* XXX Modify this callback to let Asterisk support controlling the length of DTMF */
246 return 0;
249 static int phone_digit_end(struct ast_channel *ast, char digit)
251 struct phone_pvt *p;
252 int outdigit;
253 p = ast->tech_pvt;
254 ast_log(LOG_NOTICE, "Dialed %c\n", digit);
255 switch(digit) {
256 case '0':
257 case '1':
258 case '2':
259 case '3':
260 case '4':
261 case '5':
262 case '6':
263 case '7':
264 case '8':
265 case '9':
266 outdigit = digit - '0';
267 break;
268 case '*':
269 outdigit = 11;
270 break;
271 case '#':
272 outdigit = 12;
273 break;
274 case 'f': /*flash*/
275 case 'F':
276 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
277 usleep(320000);
278 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
279 p->lastformat = -1;
280 return 0;
281 default:
282 ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
283 return -1;
285 ast_log(LOG_NOTICE, "Dialed %d\n", outdigit);
286 ioctl(p->fd, PHONE_PLAY_TONE, outdigit);
287 p->lastformat = -1;
288 return 0;
291 static int phone_call(struct ast_channel *ast, char *dest, int timeout)
293 struct phone_pvt *p;
295 PHONE_CID cid;
296 time_t UtcTime;
297 struct tm tm;
298 int start;
300 time(&UtcTime);
301 localtime_r(&UtcTime,&tm);
303 memset(&cid, 0, sizeof(PHONE_CID));
304 if(&tm != NULL) {
305 snprintf(cid.month, sizeof(cid.month), "%02d",(tm.tm_mon + 1));
306 snprintf(cid.day, sizeof(cid.day), "%02d", tm.tm_mday);
307 snprintf(cid.hour, sizeof(cid.hour), "%02d", tm.tm_hour);
308 snprintf(cid.min, sizeof(cid.min), "%02d", tm.tm_min);
310 /* the standard format of ast->callerid is: "name" <number>, but not always complete */
311 if (ast_strlen_zero(ast->cid.cid_name))
312 strcpy(cid.name, DEFAULT_CALLER_ID);
313 else
314 ast_copy_string(cid.name, ast->cid.cid_name, sizeof(cid.name));
316 if (ast->cid.cid_num)
317 ast_copy_string(cid.number, ast->cid.cid_num, sizeof(cid.number));
319 p = ast->tech_pvt;
321 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
322 ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast->name);
323 return -1;
325 if (option_debug)
326 ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fds[0]);
328 start = IXJ_PHONE_RING_START(cid);
329 if (start == -1)
330 return -1;
332 if (p->mode == MODE_FXS) {
333 char *digit = strchr(dest, '/');
334 if (digit)
336 digit++;
337 while (*digit)
338 phone_digit_end(ast, *digit++);
342 ast_setstate(ast, AST_STATE_RINGING);
343 ast_queue_control(ast, AST_CONTROL_RINGING);
344 return 0;
347 static int phone_hangup(struct ast_channel *ast)
349 struct phone_pvt *p;
350 p = ast->tech_pvt;
351 if (option_debug)
352 ast_log(LOG_DEBUG, "phone_hangup(%s)\n", ast->name);
353 if (!ast->tech_pvt) {
354 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
355 return 0;
357 /* XXX Is there anything we can do to really hang up except stop recording? */
358 ast_setstate(ast, AST_STATE_DOWN);
359 if (ioctl(p->fd, PHONE_REC_STOP))
360 ast_log(LOG_WARNING, "Failed to stop recording\n");
361 if (ioctl(p->fd, PHONE_PLAY_STOP))
362 ast_log(LOG_WARNING, "Failed to stop playing\n");
363 if (ioctl(p->fd, PHONE_RING_STOP))
364 ast_log(LOG_WARNING, "Failed to stop ringing\n");
365 if (ioctl(p->fd, PHONE_CPT_STOP))
366 ast_log(LOG_WARNING, "Failed to stop sounds\n");
368 /* If it's an FXO, hang them up */
369 if (p->mode == MODE_FXO) {
370 if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
371 ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",ast->name, strerror(errno));
374 /* If they're off hook, give a busy signal */
375 if (ioctl(p->fd, PHONE_HOOKSTATE)) {
376 if (option_debug)
377 ast_log(LOG_DEBUG, "Got hunghup, giving busy signal\n");
378 ioctl(p->fd, PHONE_BUSY);
379 p->cpt = 1;
381 p->lastformat = -1;
382 p->lastinput = -1;
383 p->ministate = 0;
384 p->obuflen = 0;
385 p->dialtone = 0;
386 memset(p->ext, 0, sizeof(p->ext));
387 ((struct phone_pvt *)(ast->tech_pvt))->owner = NULL;
388 ast_mutex_lock(&usecnt_lock);
389 usecnt--;
390 if (usecnt < 0)
391 ast_log(LOG_WARNING, "Usecnt < 0???\n");
392 ast_mutex_unlock(&usecnt_lock);
393 ast_update_use_count();
394 if (option_verbose > 2)
395 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
396 ast->tech_pvt = NULL;
397 ast_setstate(ast, AST_STATE_DOWN);
398 restart_monitor();
399 return 0;
402 static int phone_setup(struct ast_channel *ast)
404 struct phone_pvt *p;
405 p = ast->tech_pvt;
406 ioctl(p->fd, PHONE_CPT_STOP);
407 /* Nothing to answering really, just start recording */
408 if (ast->rawreadformat == AST_FORMAT_G723_1) {
409 /* Prefer g723 */
410 ioctl(p->fd, PHONE_REC_STOP);
411 if (p->lastinput != AST_FORMAT_G723_1) {
412 p->lastinput = AST_FORMAT_G723_1;
413 if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
414 ast_log(LOG_WARNING, "Failed to set codec to g723.1\n");
415 return -1;
418 } else if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
419 ioctl(p->fd, PHONE_REC_STOP);
420 if (p->lastinput != AST_FORMAT_SLINEAR) {
421 p->lastinput = AST_FORMAT_SLINEAR;
422 if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
423 ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
424 return -1;
427 } else if (ast->rawreadformat == AST_FORMAT_ULAW) {
428 ioctl(p->fd, PHONE_REC_STOP);
429 if (p->lastinput != AST_FORMAT_ULAW) {
430 p->lastinput = AST_FORMAT_ULAW;
431 if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
432 ast_log(LOG_WARNING, "Failed to set codec to uLaw\n");
433 return -1;
436 } else if (p->mode == MODE_FXS) {
437 ioctl(p->fd, PHONE_REC_STOP);
438 if (p->lastinput != ast->rawreadformat) {
439 p->lastinput = ast->rawreadformat;
440 if (ioctl(p->fd, PHONE_REC_CODEC, ast->rawreadformat)) {
441 ast_log(LOG_WARNING, "Failed to set codec to %d\n",
442 ast->rawreadformat);
443 return -1;
446 } else {
447 ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast->rawreadformat));
448 return -1;
450 if (ioctl(p->fd, PHONE_REC_START)) {
451 ast_log(LOG_WARNING, "Failed to start recording\n");
452 return -1;
454 /* set the DTMF times (the default is too short) */
455 ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
456 ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
457 return 0;
460 static int phone_answer(struct ast_channel *ast)
462 struct phone_pvt *p;
463 p = ast->tech_pvt;
464 /* In case it's a LineJack, take it off hook */
465 if (p->mode == MODE_FXO) {
466 if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_OFF_HOOK))
467 ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n", ast->name, strerror(errno));
468 else
469 ast_log(LOG_DEBUG, "Took linejack off hook\n");
471 phone_setup(ast);
472 if (option_debug)
473 ast_log(LOG_DEBUG, "phone_answer(%s)\n", ast->name);
474 ast->rings = 0;
475 ast_setstate(ast, AST_STATE_UP);
476 return 0;
479 #if 0
480 static char phone_2digit(char c)
482 if (c == 12)
483 return '#';
484 else if (c == 11)
485 return '*';
486 else if ((c < 10) && (c >= 0))
487 return '0' + c - 1;
488 else
489 return '?';
491 #endif
493 static struct ast_frame *phone_exception(struct ast_channel *ast)
495 int res;
496 union telephony_exception phonee;
497 struct phone_pvt *p = ast->tech_pvt;
498 char digit;
500 /* Some nice norms */
501 p->fr.datalen = 0;
502 p->fr.samples = 0;
503 p->fr.data = NULL;
504 p->fr.src = "Phone";
505 p->fr.offset = 0;
506 p->fr.mallocd=0;
507 p->fr.delivery = ast_tv(0,0);
509 phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
510 if (phonee.bits.dtmf_ready) {
511 if (option_debug)
512 ast_log(LOG_DEBUG, "phone_exception(): DTMF\n");
514 /* We've got a digit -- Just handle this nicely and easily */
515 digit = ioctl(p->fd, PHONE_GET_DTMF_ASCII);
516 p->fr.subclass = digit;
517 p->fr.frametype = AST_FRAME_DTMF;
518 return &p->fr;
520 if (phonee.bits.hookstate) {
521 if (option_debug)
522 ast_log(LOG_DEBUG, "Hookstate changed\n");
523 res = ioctl(p->fd, PHONE_HOOKSTATE);
524 /* See if we've gone on hook, if so, notify by returning NULL */
525 if (option_debug)
526 ast_log(LOG_DEBUG, "New hookstate: %d\n", res);
527 if (!res && (p->mode != MODE_FXO))
528 return NULL;
529 else {
530 if (ast->_state == AST_STATE_RINGING) {
531 /* They've picked up the phone */
532 p->fr.frametype = AST_FRAME_CONTROL;
533 p->fr.subclass = AST_CONTROL_ANSWER;
534 phone_setup(ast);
535 ast_setstate(ast, AST_STATE_UP);
536 return &p->fr;
537 } else
538 ast_log(LOG_WARNING, "Got off hook in weird state %d\n", ast->_state);
541 #if 1
542 if (phonee.bits.pstn_ring)
543 ast_verbose("Unit is ringing\n");
544 if (phonee.bits.caller_id) {
545 ast_verbose("We have caller ID\n");
547 if (phonee.bits.pstn_wink)
548 ast_verbose("Detected Wink\n");
549 #endif
550 /* Strange -- nothing there.. */
551 p->fr.frametype = AST_FRAME_NULL;
552 p->fr.subclass = 0;
553 return &p->fr;
556 static struct ast_frame *phone_read(struct ast_channel *ast)
558 int res;
559 struct phone_pvt *p = ast->tech_pvt;
562 /* Some nice norms */
563 p->fr.datalen = 0;
564 p->fr.samples = 0;
565 p->fr.data = NULL;
566 p->fr.src = "Phone";
567 p->fr.offset = 0;
568 p->fr.mallocd=0;
569 p->fr.delivery = ast_tv(0,0);
571 /* Try to read some data... */
572 CHECK_BLOCKING(ast);
573 res = read(p->fd, p->buf, PHONE_MAX_BUF);
574 ast_clear_flag(ast, AST_FLAG_BLOCKING);
575 if (res < 0) {
576 #if 0
577 if (errno == EAGAIN) {
578 ast_log(LOG_WARNING, "Null frame received\n");
579 p->fr.frametype = AST_FRAME_NULL;
580 p->fr.subclass = 0;
581 return &p->fr;
583 #endif
584 ast_log(LOG_WARNING, "Error reading: %s\n", strerror(errno));
585 return NULL;
587 p->fr.data = p->buf;
588 if (p->mode != MODE_FXS)
589 switch(p->buf[0] & 0x3) {
590 case '0':
591 case '1':
592 /* Normal */
593 break;
594 case '2':
595 case '3':
596 /* VAD/CNG, only send two words */
597 res = 4;
598 break;
600 p->fr.samples = 240;
601 p->fr.datalen = res;
602 p->fr.frametype = p->lastinput <= AST_FORMAT_MAX_AUDIO ?
603 AST_FRAME_VOICE :
604 p->lastinput <= AST_FORMAT_PNG ? AST_FRAME_IMAGE
605 : AST_FRAME_VIDEO;
606 p->fr.subclass = p->lastinput;
607 p->fr.offset = AST_FRIENDLY_OFFSET;
608 /* Byteswap from little-endian to native-endian */
609 if (p->fr.subclass == AST_FORMAT_SLINEAR)
610 ast_frame_byteswap_le(&p->fr);
611 return &p->fr;
614 static int phone_write_buf(struct phone_pvt *p, const char *buf, int len, int frlen, int swap)
616 int res;
617 /* Store as much of the buffer as we can, then write fixed frames */
618 int space = sizeof(p->obuf) - p->obuflen;
619 /* Make sure we have enough buffer space to store the frame */
620 if (space < len)
621 len = space;
622 if (swap)
623 ast_swapcopy_samples(p->obuf+p->obuflen, buf, len/2);
624 else
625 memcpy(p->obuf + p->obuflen, buf, len);
626 p->obuflen += len;
627 while(p->obuflen > frlen) {
628 res = write(p->fd, p->obuf, frlen);
629 if (res != frlen) {
630 if (res < 1) {
632 * Card is in non-blocking mode now and it works well now, but there are
633 * lot of messages like this. So, this message is temporarily disabled.
635 return 0;
636 } else {
637 ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
640 p->obuflen -= frlen;
641 /* Move memory if necessary */
642 if (p->obuflen)
643 memmove(p->obuf, p->obuf + frlen, p->obuflen);
645 return len;
648 static int phone_send_text(struct ast_channel *ast, const char *text)
650 int length = strlen(text);
651 return phone_write_buf(ast->tech_pvt, text, length, length, 0) ==
652 length ? 0 : -1;
655 static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
657 struct phone_pvt *p = ast->tech_pvt;
658 int res;
659 int maxfr=0;
660 char *pos;
661 int sofar;
662 int expected;
663 int codecset = 0;
664 char tmpbuf[4];
665 /* Write a frame of (presumably voice) data */
666 if (frame->frametype != AST_FRAME_VOICE && p->mode != MODE_FXS) {
667 if (frame->frametype != AST_FRAME_IMAGE)
668 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
669 return 0;
671 if (!(frame->subclass &
672 (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW)) &&
673 p->mode != MODE_FXS) {
674 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
675 return -1;
677 #if 0
678 /* If we're not in up mode, go into up mode now */
679 if (ast->_state != AST_STATE_UP) {
680 ast_setstate(ast, AST_STATE_UP);
681 phone_setup(ast);
683 #else
684 if (ast->_state != AST_STATE_UP) {
685 /* Don't try tos end audio on-hook */
686 return 0;
688 #endif
689 if (frame->subclass == AST_FORMAT_G723_1) {
690 if (p->lastformat != AST_FORMAT_G723_1) {
691 ioctl(p->fd, PHONE_PLAY_STOP);
692 ioctl(p->fd, PHONE_REC_STOP);
693 if (ioctl(p->fd, PHONE_PLAY_CODEC, G723_63)) {
694 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
695 return -1;
697 if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
698 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
699 return -1;
701 p->lastformat = AST_FORMAT_G723_1;
702 p->lastinput = AST_FORMAT_G723_1;
703 /* Reset output buffer */
704 p->obuflen = 0;
705 codecset = 1;
707 if (frame->datalen > 24) {
708 ast_log(LOG_WARNING, "Frame size too large for G.723.1 (%d bytes)\n", frame->datalen);
709 return -1;
711 maxfr = 24;
712 } else if (frame->subclass == AST_FORMAT_SLINEAR) {
713 if (p->lastformat != AST_FORMAT_SLINEAR) {
714 ioctl(p->fd, PHONE_PLAY_STOP);
715 ioctl(p->fd, PHONE_REC_STOP);
716 if (ioctl(p->fd, PHONE_PLAY_CODEC, LINEAR16)) {
717 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
718 return -1;
720 if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
721 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
722 return -1;
724 p->lastformat = AST_FORMAT_SLINEAR;
725 p->lastinput = AST_FORMAT_SLINEAR;
726 codecset = 1;
727 /* Reset output buffer */
728 p->obuflen = 0;
730 maxfr = 480;
731 } else if (frame->subclass == AST_FORMAT_ULAW) {
732 if (p->lastformat != AST_FORMAT_ULAW) {
733 ioctl(p->fd, PHONE_PLAY_STOP);
734 ioctl(p->fd, PHONE_REC_STOP);
735 if (ioctl(p->fd, PHONE_PLAY_CODEC, ULAW)) {
736 ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
737 return -1;
739 if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
740 ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
741 return -1;
743 p->lastformat = AST_FORMAT_ULAW;
744 p->lastinput = AST_FORMAT_ULAW;
745 codecset = 1;
746 /* Reset output buffer */
747 p->obuflen = 0;
749 maxfr = 240;
750 } else {
751 if (p->lastformat != frame->subclass) {
752 ioctl(p->fd, PHONE_PLAY_STOP);
753 ioctl(p->fd, PHONE_REC_STOP);
754 if (ioctl(p->fd, PHONE_PLAY_CODEC, frame->subclass)) {
755 ast_log(LOG_WARNING, "Unable to set %d mode\n",
756 frame->subclass);
757 return -1;
759 if (ioctl(p->fd, PHONE_REC_CODEC, frame->subclass)) {
760 ast_log(LOG_WARNING, "Unable to set %d mode\n",
761 frame->subclass);
762 return -1;
764 p->lastformat = frame->subclass;
765 p->lastinput = frame->subclass;
766 codecset = 1;
767 /* Reset output buffer */
768 p->obuflen = 0;
770 maxfr = 480;
772 if (codecset) {
773 ioctl(p->fd, PHONE_REC_DEPTH, 3);
774 ioctl(p->fd, PHONE_PLAY_DEPTH, 3);
775 if (ioctl(p->fd, PHONE_PLAY_START)) {
776 ast_log(LOG_WARNING, "Failed to start playback\n");
777 return -1;
779 if (ioctl(p->fd, PHONE_REC_START)) {
780 ast_log(LOG_WARNING, "Failed to start recording\n");
781 return -1;
784 /* If we get here, we have a frame of Appropriate data */
785 sofar = 0;
786 pos = frame->data;
787 while(sofar < frame->datalen) {
788 /* Write in no more than maxfr sized frames */
789 expected = frame->datalen - sofar;
790 if (maxfr < expected)
791 expected = maxfr;
792 /* XXX Internet Phone Jack does not handle the 4-byte VAD frame properly! XXX
793 we have to pad it to 24 bytes still. */
794 if (frame->datalen == 4) {
795 if (p->silencesupression) {
796 memset(tmpbuf + 4, 0, sizeof(tmpbuf) - 4);
797 memcpy(tmpbuf, frame->data, 4);
798 expected = 24;
799 res = phone_write_buf(p, tmpbuf, expected, maxfr, 0);
801 res = 4;
802 expected=4;
803 } else {
804 int swap = 0;
805 #if __BYTE_ORDER == __BIG_ENDIAN
806 if (frame->subclass == AST_FORMAT_SLINEAR)
807 swap = 1; /* Swap big-endian samples to little-endian as we copy */
808 #endif
809 res = phone_write_buf(p, pos, expected, maxfr, swap);
811 if (res != expected) {
812 if ((errno != EAGAIN) && (errno != EINTR)) {
813 if (res < 0)
814 ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
816 * Card is in non-blocking mode now and it works well now, but there are
817 * lot of messages like this. So, this message is temporarily disabled.
819 #if 0
820 else
821 ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
822 #endif
823 return -1;
824 } else /* Pretend it worked */
825 res = expected;
827 sofar += res;
828 pos += res;
830 return 0;
833 static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *context)
835 struct ast_channel *tmp;
836 struct phone_codec_data codec;
837 tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, "Phone/%s", i->dev + 5);
838 if (tmp) {
839 tmp->tech = cur_tech;
840 tmp->fds[0] = i->fd;
841 /* XXX Switching formats silently causes kernel panics XXX */
842 if (i->mode == MODE_FXS &&
843 ioctl(i->fd, PHONE_QUERY_CODEC, &codec) == 0) {
844 if (codec.type == LINEAR16)
845 tmp->nativeformats =
846 tmp->rawreadformat =
847 tmp->rawwriteformat =
848 AST_FORMAT_SLINEAR;
849 else {
850 tmp->nativeformats =
851 tmp->rawreadformat =
852 tmp->rawwriteformat =
853 prefformat & ~AST_FORMAT_SLINEAR;
856 else {
857 tmp->nativeformats = prefformat;
858 tmp->rawreadformat = prefformat;
859 tmp->rawwriteformat = prefformat;
861 /* no need to call ast_setstate: the channel_alloc already did its job */
862 if (state == AST_STATE_RING)
863 tmp->rings = 1;
864 tmp->tech_pvt = i;
865 ast_copy_string(tmp->context, context, sizeof(tmp->context));
866 if (!ast_strlen_zero(i->ext))
867 ast_copy_string(tmp->exten, i->ext, sizeof(tmp->exten));
868 else
869 strcpy(tmp->exten, "s");
870 if (!ast_strlen_zero(i->language))
871 ast_string_field_set(tmp, language, i->language);
873 /* Don't use ast_set_callerid() here because it will
874 * generate a NewCallerID event before the NewChannel event */
875 tmp->cid.cid_num = ast_strdup(i->cid_num);
876 tmp->cid.cid_ani = ast_strdup(i->cid_num);
877 tmp->cid.cid_name = ast_strdup(i->cid_name);
879 i->owner = tmp;
880 ast_mutex_lock(&usecnt_lock);
881 usecnt++;
882 ast_mutex_unlock(&usecnt_lock);
883 ast_update_use_count();
884 if (state != AST_STATE_DOWN) {
885 if (state == AST_STATE_RING) {
886 ioctl(tmp->fds[0], PHONE_RINGBACK);
887 i->cpt = 1;
889 if (ast_pbx_start(tmp)) {
890 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
891 ast_hangup(tmp);
894 } else
895 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
896 return tmp;
899 static void phone_mini_packet(struct phone_pvt *i)
901 int res;
902 char buf[1024];
903 /* Ignore stuff we read... */
904 res = read(i->fd, buf, sizeof(buf));
905 if (res < 1) {
906 ast_log(LOG_WARNING, "Read returned %d: %s\n", res, strerror(errno));
907 return;
911 static void phone_check_exception(struct phone_pvt *i)
913 int offhook=0;
914 char digit[2] = {0 , 0};
915 union telephony_exception phonee;
916 /* XXX Do something XXX */
917 #if 0
918 ast_log(LOG_DEBUG, "Exception!\n");
919 #endif
920 phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION);
921 if (phonee.bits.dtmf_ready) {
922 digit[0] = ioctl(i->fd, PHONE_GET_DTMF_ASCII);
923 if (i->mode == MODE_DIALTONE || i->mode == MODE_FXS || i->mode == MODE_SIGMA) {
924 ioctl(i->fd, PHONE_PLAY_STOP);
925 ioctl(i->fd, PHONE_REC_STOP);
926 ioctl(i->fd, PHONE_CPT_STOP);
927 i->dialtone = 0;
928 if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
929 strncat(i->ext, digit, sizeof(i->ext) - strlen(i->ext) - 1);
930 if ((i->mode != MODE_FXS ||
931 !(phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION)) ||
932 !phonee.bits.dtmf_ready) &&
933 ast_exists_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
934 /* It's a valid extension in its context, get moving! */
935 phone_new(i, AST_STATE_RING, i->context);
936 /* No need to restart monitor, we are the monitor */
937 } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
938 /* There is nothing in the specified extension that can match anymore.
939 Try the default */
940 if (ast_exists_extension(NULL, "default", i->ext, 1, i->cid_num)) {
941 /* Check the default, too... */
942 phone_new(i, AST_STATE_RING, "default");
943 /* XXX This should probably be justified better XXX */
944 } else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->cid_num)) {
945 /* It's not a valid extension, give a busy signal */
946 if (option_debug)
947 ast_log(LOG_DEBUG, "%s can't match anything in %s or default\n", i->ext, i->context);
948 ioctl(i->fd, PHONE_BUSY);
949 i->cpt = 1;
952 #if 0
953 ast_verbose("Extension is %s\n", i->ext);
954 #endif
957 if (phonee.bits.hookstate) {
958 offhook = ioctl(i->fd, PHONE_HOOKSTATE);
959 if (offhook) {
960 if (i->mode == MODE_IMMEDIATE) {
961 phone_new(i, AST_STATE_RING, i->context);
962 } else if (i->mode == MODE_DIALTONE) {
963 ast_mutex_lock(&usecnt_lock);
964 usecnt++;
965 ast_mutex_unlock(&usecnt_lock);
966 ast_update_use_count();
967 /* Reset the extension */
968 i->ext[0] = '\0';
969 /* Play the dialtone */
970 i->dialtone++;
971 ioctl(i->fd, PHONE_PLAY_STOP);
972 ioctl(i->fd, PHONE_PLAY_CODEC, ULAW);
973 ioctl(i->fd, PHONE_PLAY_START);
974 i->lastformat = -1;
975 } else if (i->mode == MODE_SIGMA) {
976 ast_mutex_lock(&usecnt_lock);
977 usecnt++;
978 ast_mutex_unlock(&usecnt_lock);
979 ast_update_use_count();
980 /* Reset the extension */
981 i->ext[0] = '\0';
982 /* Play the dialtone */
983 i->dialtone++;
984 ioctl(i->fd, PHONE_DIALTONE);
986 } else {
987 if (i->dialtone) {
988 ast_mutex_lock(&usecnt_lock);
989 usecnt--;
990 ast_mutex_unlock(&usecnt_lock);
991 ast_update_use_count();
993 memset(i->ext, 0, sizeof(i->ext));
994 if (i->cpt)
996 ioctl(i->fd, PHONE_CPT_STOP);
997 i->cpt = 0;
999 ioctl(i->fd, PHONE_PLAY_STOP);
1000 ioctl(i->fd, PHONE_REC_STOP);
1001 i->dialtone = 0;
1002 i->lastformat = -1;
1005 if (phonee.bits.pstn_ring) {
1006 ast_verbose("Unit is ringing\n");
1007 phone_new(i, AST_STATE_RING, i->context);
1009 if (phonee.bits.caller_id)
1010 ast_verbose("We have caller ID\n");
1015 static void *do_monitor(void *data)
1017 fd_set rfds, efds;
1018 int n, res;
1019 struct phone_pvt *i;
1020 int tonepos = 0;
1021 /* The tone we're playing this round */
1022 struct timeval tv = {0,0};
1023 int dotone;
1024 /* This thread monitors all the frame relay interfaces which are not yet in use
1025 (and thus do not have a separate thread) indefinitely */
1026 while (monitor) {
1027 /* Don't let anybody kill us right away. Nobody should lock the interface list
1028 and wait for the monitor list, but the other way around is okay. */
1029 /* Lock the interface list */
1030 if (ast_mutex_lock(&iflock)) {
1031 ast_log(LOG_ERROR, "Unable to grab interface lock\n");
1032 return NULL;
1034 /* Build the stuff we're going to select on, that is the socket of every
1035 phone_pvt that does not have an associated owner channel */
1036 n = -1;
1037 FD_ZERO(&rfds);
1038 FD_ZERO(&efds);
1039 i = iflist;
1040 dotone = 0;
1041 while (i) {
1042 if (FD_ISSET(i->fd, &rfds))
1043 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
1044 if (!i->owner) {
1045 /* This needs to be watched, as it lacks an owner */
1046 FD_SET(i->fd, &rfds);
1047 FD_SET(i->fd, &efds);
1048 if (i->fd > n)
1049 n = i->fd;
1050 if (i->dialtone && i->mode != MODE_SIGMA) {
1051 /* Remember we're going to have to come back and play
1052 more dialtones */
1053 if (ast_tvzero(tv)) {
1054 /* If we're due for a dialtone, play one */
1055 if (write(i->fd, DialTone + tonepos, 240) != 240)
1056 ast_log(LOG_WARNING, "Dial tone write error\n");
1058 dotone++;
1062 i = i->next;
1064 /* Okay, now that we know what to do, release the interface lock */
1065 ast_mutex_unlock(&iflock);
1067 /* Wait indefinitely for something to happen */
1068 if (dotone && i->mode != MODE_SIGMA) {
1069 /* If we're ready to recycle the time, set it to 30 ms */
1070 tonepos += 240;
1071 if (tonepos >= sizeof(DialTone))
1072 tonepos = 0;
1073 if (ast_tvzero(tv)) {
1074 tv = ast_tv(30000, 0);
1076 res = ast_select(n + 1, &rfds, NULL, &efds, &tv);
1077 } else {
1078 res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
1079 tv = ast_tv(0,0);
1080 tonepos = 0;
1082 /* Okay, select has finished. Let's see what happened. */
1083 if (res < 0) {
1084 ast_log(LOG_DEBUG, "select return %d: %s\n", res, strerror(errno));
1085 continue;
1087 /* If there are no fd's changed, just continue, it's probably time
1088 to play some more dialtones */
1089 if (!res)
1090 continue;
1091 /* Alright, lock the interface list again, and let's look and see what has
1092 happened */
1093 if (ast_mutex_lock(&iflock)) {
1094 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1095 continue;
1098 i = iflist;
1099 for(; i; i=i->next) {
1100 if (FD_ISSET(i->fd, &rfds)) {
1101 if (i->owner) {
1102 continue;
1104 phone_mini_packet(i);
1106 if (FD_ISSET(i->fd, &efds)) {
1107 if (i->owner) {
1108 continue;
1110 phone_check_exception(i);
1113 ast_mutex_unlock(&iflock);
1115 return NULL;
1119 static int restart_monitor()
1121 /* If we're supposed to be stopped -- stay stopped */
1122 if (monitor_thread == AST_PTHREADT_STOP)
1123 return 0;
1124 if (ast_mutex_lock(&monlock)) {
1125 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1126 return -1;
1128 if (monitor_thread == pthread_self()) {
1129 ast_mutex_unlock(&monlock);
1130 ast_log(LOG_WARNING, "Cannot kill myself\n");
1131 return -1;
1133 if (monitor_thread != AST_PTHREADT_NULL) {
1134 if (ast_mutex_lock(&iflock)) {
1135 ast_mutex_unlock(&monlock);
1136 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1137 return -1;
1139 monitor = 0;
1140 while (pthread_kill(monitor_thread, SIGURG) == 0)
1141 sched_yield();
1142 pthread_join(monitor_thread, NULL);
1143 ast_mutex_unlock(&iflock);
1145 monitor = 1;
1146 /* Start a new monitor */
1147 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
1148 ast_mutex_unlock(&monlock);
1149 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1150 return -1;
1152 ast_mutex_unlock(&monlock);
1153 return 0;
1156 static struct phone_pvt *mkif(char *iface, int mode, int txgain, int rxgain)
1158 /* Make a phone_pvt structure for this interface */
1159 struct phone_pvt *tmp;
1160 int flags;
1162 tmp = malloc(sizeof(struct phone_pvt));
1163 if (tmp) {
1164 tmp->fd = open(iface, O_RDWR);
1165 if (tmp->fd < 0) {
1166 ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
1167 free(tmp);
1168 return NULL;
1170 if (mode == MODE_FXO) {
1171 if (ioctl(tmp->fd, IXJCTL_PORT, PORT_PSTN))
1172 ast_log(LOG_DEBUG, "Unable to set port to PSTN\n");
1173 } else {
1174 if (ioctl(tmp->fd, IXJCTL_PORT, PORT_POTS))
1175 if (mode != MODE_FXS)
1176 ast_log(LOG_DEBUG, "Unable to set port to POTS\n");
1178 ioctl(tmp->fd, PHONE_PLAY_STOP);
1179 ioctl(tmp->fd, PHONE_REC_STOP);
1180 ioctl(tmp->fd, PHONE_RING_STOP);
1181 ioctl(tmp->fd, PHONE_CPT_STOP);
1182 if (ioctl(tmp->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
1183 ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",iface, strerror(errno));
1184 if (echocancel != AEC_OFF)
1185 ioctl(tmp->fd, IXJCTL_AEC_START, echocancel);
1186 if (silencesupression)
1187 tmp->silencesupression = 1;
1188 #ifdef PHONE_VAD
1189 ioctl(tmp->fd, PHONE_VAD, tmp->silencesupression);
1190 #endif
1191 tmp->mode = mode;
1192 flags = fcntl(tmp->fd, F_GETFL);
1193 fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
1194 tmp->owner = NULL;
1195 tmp->lastformat = -1;
1196 tmp->lastinput = -1;
1197 tmp->ministate = 0;
1198 memset(tmp->ext, 0, sizeof(tmp->ext));
1199 ast_copy_string(tmp->language, language, sizeof(tmp->language));
1200 ast_copy_string(tmp->dev, iface, sizeof(tmp->dev));
1201 ast_copy_string(tmp->context, context, sizeof(tmp->context));
1202 tmp->next = NULL;
1203 tmp->obuflen = 0;
1204 tmp->dialtone = 0;
1205 tmp->cpt = 0;
1206 ast_copy_string(tmp->cid_num, cid_num, sizeof(tmp->cid_num));
1207 ast_copy_string(tmp->cid_name, cid_name, sizeof(tmp->cid_name));
1208 tmp->txgain = txgain;
1209 ioctl(tmp->fd, PHONE_PLAY_VOLUME, tmp->txgain);
1210 tmp->rxgain = rxgain;
1211 ioctl(tmp->fd, PHONE_REC_VOLUME, tmp->rxgain);
1213 return tmp;
1216 static struct ast_channel *phone_request(const char *type, int format, void *data, int *cause)
1218 int oldformat;
1219 struct phone_pvt *p;
1220 struct ast_channel *tmp = NULL;
1221 char *name = data;
1223 /* Search for an unowned channel */
1224 if (ast_mutex_lock(&iflock)) {
1225 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1226 return NULL;
1228 p = iflist;
1229 while(p) {
1230 if (p->mode == MODE_FXS ||
1231 format & (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW)) {
1232 size_t length = strlen(p->dev + 5);
1233 if (strncmp(name, p->dev + 5, length) == 0 &&
1234 !isalnum(name[length])) {
1235 if (!p->owner) {
1236 tmp = phone_new(p, AST_STATE_DOWN, p->context);
1237 break;
1238 } else
1239 *cause = AST_CAUSE_BUSY;
1242 p = p->next;
1244 ast_mutex_unlock(&iflock);
1245 restart_monitor();
1246 if (tmp == NULL) {
1247 oldformat = format;
1248 format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
1249 if (!format) {
1250 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
1251 return NULL;
1254 return tmp;
1257 /* parse gain value from config file */
1258 static int parse_gain_value(char *gain_type, char *value)
1260 float gain;
1262 /* try to scan number */
1263 if (sscanf(value, "%f", &gain) != 1)
1265 ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n",
1266 value, gain_type, config);
1267 return DEFAULT_GAIN;
1270 /* multiplicate gain by 1.0 gain value */
1271 gain = gain * (float)DEFAULT_GAIN;
1273 /* percentage? */
1274 if (value[strlen(value) - 1] == '%')
1275 return (int)(gain / (float)100);
1277 return (int)gain;
1280 static int __unload_module(void)
1282 struct phone_pvt *p, *pl;
1283 /* First, take us out of the channel loop */
1284 ast_channel_unregister(cur_tech);
1285 if (!ast_mutex_lock(&iflock)) {
1286 /* Hangup all interfaces if they have an owner */
1287 p = iflist;
1288 while(p) {
1289 if (p->owner)
1290 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1291 p = p->next;
1293 iflist = NULL;
1294 ast_mutex_unlock(&iflock);
1295 } else {
1296 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1297 return -1;
1299 if (!ast_mutex_lock(&monlock)) {
1300 if (monitor_thread > AST_PTHREADT_NULL) {
1301 monitor = 0;
1302 while (pthread_kill(monitor_thread, SIGURG) == 0)
1303 sched_yield();
1304 pthread_join(monitor_thread, NULL);
1306 monitor_thread = AST_PTHREADT_STOP;
1307 ast_mutex_unlock(&monlock);
1308 } else {
1309 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1310 return -1;
1313 if (!ast_mutex_lock(&iflock)) {
1314 /* Destroy all the interfaces and free their memory */
1315 p = iflist;
1316 while(p) {
1317 /* Close the socket, assuming it's real */
1318 if (p->fd > -1)
1319 close(p->fd);
1320 pl = p;
1321 p = p->next;
1322 /* Free associated memory */
1323 free(pl);
1325 iflist = NULL;
1326 ast_mutex_unlock(&iflock);
1327 } else {
1328 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1329 return -1;
1332 return 0;
1335 static int unload_module(void)
1337 return __unload_module();
1340 static int load_module(void)
1342 struct ast_config *cfg;
1343 struct ast_variable *v;
1344 struct phone_pvt *tmp;
1345 int mode = MODE_IMMEDIATE;
1346 int txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; /* default gain 1.0 */
1347 cfg = ast_config_load(config);
1349 /* We *must* have a config file otherwise stop immediately */
1350 if (!cfg) {
1351 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1352 return AST_MODULE_LOAD_DECLINE;
1354 if (ast_mutex_lock(&iflock)) {
1355 /* It's a little silly to lock it, but we mind as well just to be sure */
1356 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1357 return -1;
1359 v = ast_variable_browse(cfg, "interfaces");
1360 while(v) {
1361 /* Create the interface list */
1362 if (!strcasecmp(v->name, "device")) {
1363 tmp = mkif(v->value, mode, txgain, rxgain);
1364 if (tmp) {
1365 tmp->next = iflist;
1366 iflist = tmp;
1368 } else {
1369 ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
1370 ast_config_destroy(cfg);
1371 ast_mutex_unlock(&iflock);
1372 __unload_module();
1373 return -1;
1375 } else if (!strcasecmp(v->name, "silencesupression")) {
1376 silencesupression = ast_true(v->value);
1377 } else if (!strcasecmp(v->name, "language")) {
1378 ast_copy_string(language, v->value, sizeof(language));
1379 } else if (!strcasecmp(v->name, "callerid")) {
1380 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
1381 } else if (!strcasecmp(v->name, "mode")) {
1382 if (!strncasecmp(v->value, "di", 2))
1383 mode = MODE_DIALTONE;
1384 else if (!strncasecmp(v->value, "sig", 3))
1385 mode = MODE_SIGMA;
1386 else if (!strncasecmp(v->value, "im", 2))
1387 mode = MODE_IMMEDIATE;
1388 else if (!strncasecmp(v->value, "fxs", 3)) {
1389 mode = MODE_FXS;
1390 prefformat = 0x01ff0000; /* All non-voice */
1392 else if (!strncasecmp(v->value, "fx", 2))
1393 mode = MODE_FXO;
1394 else
1395 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
1396 } else if (!strcasecmp(v->name, "context")) {
1397 ast_copy_string(context, v->value, sizeof(context));
1398 } else if (!strcasecmp(v->name, "format")) {
1399 if (!strcasecmp(v->value, "g723.1")) {
1400 prefformat = AST_FORMAT_G723_1;
1401 } else if (!strcasecmp(v->value, "slinear")) {
1402 if (mode == MODE_FXS)
1403 prefformat |= AST_FORMAT_SLINEAR;
1404 else prefformat = AST_FORMAT_SLINEAR;
1405 } else if (!strcasecmp(v->value, "ulaw")) {
1406 prefformat = AST_FORMAT_ULAW;
1407 } else
1408 ast_log(LOG_WARNING, "Unknown format '%s'\n", v->value);
1409 } else if (!strcasecmp(v->name, "echocancel")) {
1410 if (!strcasecmp(v->value, "off")) {
1411 echocancel = AEC_OFF;
1412 } else if (!strcasecmp(v->value, "low")) {
1413 echocancel = AEC_LOW;
1414 } else if (!strcasecmp(v->value, "medium")) {
1415 echocancel = AEC_MED;
1416 } else if (!strcasecmp(v->value, "high")) {
1417 echocancel = AEC_HIGH;
1418 } else
1419 ast_log(LOG_WARNING, "Unknown echo cancellation '%s'\n", v->value);
1420 } else if (!strcasecmp(v->name, "txgain")) {
1421 txgain = parse_gain_value(v->name, v->value);
1422 } else if (!strcasecmp(v->name, "rxgain")) {
1423 rxgain = parse_gain_value(v->name, v->value);
1425 v = v->next;
1427 ast_mutex_unlock(&iflock);
1429 if (mode == MODE_FXS) {
1430 phone_tech_fxs.capabilities = prefformat;
1431 cur_tech = &phone_tech_fxs;
1432 } else
1433 cur_tech = (struct ast_channel_tech *) &phone_tech;
1435 /* Make sure we can register our Adtranphone channel type */
1437 if (ast_channel_register(cur_tech)) {
1438 ast_log(LOG_ERROR, "Unable to register channel class 'Phone'\n");
1439 ast_config_destroy(cfg);
1440 __unload_module();
1441 return -1;
1443 ast_config_destroy(cfg);
1444 /* And start the monitor for the first time */
1445 restart_monitor();
1446 return 0;
1449 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Linux Telephony API Support");