Stop ignoring .lastclean
[asterisk-bristuff.git] / channels / chan_phone.c
blobb551b1cba703eb2f66ab38b9a8e06ad9e1be52c3
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 #ifdef HAVE_LINUX_COMPILER_H
49 #include <linux/compiler.h>
50 #endif
51 #include <linux/telephony.h>
52 /* Still use some IXJ specific stuff */
53 #include <linux/version.h>
54 #include <linux/ixjuser.h>
56 #include "asterisk/lock.h"
57 #include "asterisk/channel.h"
58 #include "asterisk/config.h"
59 #include "asterisk/logger.h"
60 #include "asterisk/module.h"
61 #include "asterisk/pbx.h"
62 #include "asterisk/options.h"
63 #include "asterisk/utils.h"
64 #include "asterisk/callerid.h"
65 #include "asterisk/causes.h"
66 #include "asterisk/stringfields.h"
67 #include "asterisk/musiconhold.h"
69 #include "DialTone.h"
71 #ifdef QTI_PHONEJACK_TJ_PCI /* check for the newer quicknet driver v.3.1.0 which has this symbol */
72 #define QNDRV_VER 310
73 #else
74 #define QNDRV_VER 100
75 #endif
77 #if QNDRV_VER > 100
78 #ifdef __linux__
79 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
80 #else /* FreeBSD and others */
81 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, x);
82 #endif /* __linux__ */
83 #else /* older driver */
84 #define IXJ_PHONE_RING_START(x) ioctl(p->fd, PHONE_RING_START, &x);
85 #endif
87 #define DEFAULT_CALLER_ID "Unknown"
88 #define PHONE_MAX_BUF 480
89 #define DEFAULT_GAIN 0x100
91 static const char tdesc[] = "Standard Linux Telephony API Driver";
92 static const char config[] = "phone.conf";
94 /* Default context for dialtone mode */
95 static char context[AST_MAX_EXTENSION] = "default";
97 /* Default language */
98 static char language[MAX_LANGUAGE] = "";
100 static int echocancel = AEC_OFF;
102 static int silencesupression = 0;
104 static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW;
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, unsigned int duration);
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 case AST_CONTROL_SRCUPDATE:
230 res = 0;
231 break;
232 default:
233 ast_log(LOG_WARNING, "Condition %d is not supported on channel %s\n", condition, chan->name);
235 return res;
238 static int phone_fixup(struct ast_channel *old, struct ast_channel *new)
240 struct phone_pvt *pvt = old->tech_pvt;
241 if (pvt && pvt->owner == old)
242 pvt->owner = new;
243 return 0;
246 static int phone_digit_begin(struct ast_channel *chan, char digit)
248 /* XXX Modify this callback to let Asterisk support controlling the length of DTMF */
249 return 0;
252 static int phone_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
254 struct phone_pvt *p;
255 int outdigit;
256 p = ast->tech_pvt;
257 ast_log(LOG_DEBUG, "Dialed %c\n", digit);
258 switch(digit) {
259 case '0':
260 case '1':
261 case '2':
262 case '3':
263 case '4':
264 case '5':
265 case '6':
266 case '7':
267 case '8':
268 case '9':
269 outdigit = digit - '0';
270 break;
271 case '*':
272 outdigit = 11;
273 break;
274 case '#':
275 outdigit = 12;
276 break;
277 case 'f': /*flash*/
278 case 'F':
279 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
280 usleep(320000);
281 ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_OFF_HOOK);
282 p->lastformat = -1;
283 return 0;
284 default:
285 ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
286 return -1;
288 ast_log(LOG_DEBUG, "Dialed %d\n", outdigit);
289 ioctl(p->fd, PHONE_PLAY_TONE, outdigit);
290 p->lastformat = -1;
291 return 0;
294 static int phone_call(struct ast_channel *ast, char *dest, int timeout)
296 struct phone_pvt *p;
298 PHONE_CID cid;
299 time_t UtcTime;
300 struct tm tm;
301 int start;
303 time(&UtcTime);
304 ast_localtime(&UtcTime, &tm, NULL);
306 memset(&cid, 0, sizeof(PHONE_CID));
307 if(&tm != NULL) {
308 snprintf(cid.month, sizeof(cid.month), "%02d",(tm.tm_mon + 1));
309 snprintf(cid.day, sizeof(cid.day), "%02d", tm.tm_mday);
310 snprintf(cid.hour, sizeof(cid.hour), "%02d", tm.tm_hour);
311 snprintf(cid.min, sizeof(cid.min), "%02d", tm.tm_min);
313 /* the standard format of ast->callerid is: "name" <number>, but not always complete */
314 if (ast_strlen_zero(ast->cid.cid_name))
315 strcpy(cid.name, DEFAULT_CALLER_ID);
316 else
317 ast_copy_string(cid.name, ast->cid.cid_name, sizeof(cid.name));
319 if (ast->cid.cid_num)
320 ast_copy_string(cid.number, ast->cid.cid_num, sizeof(cid.number));
322 p = ast->tech_pvt;
324 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
325 ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast->name);
326 return -1;
328 if (option_debug)
329 ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fds[0]);
331 start = IXJ_PHONE_RING_START(cid);
332 if (start == -1)
333 return -1;
335 if (p->mode == MODE_FXS) {
336 char *digit = strchr(dest, '/');
337 if (digit)
339 digit++;
340 while (*digit)
341 phone_digit_end(ast, *digit++, 0);
345 ast_setstate(ast, AST_STATE_RINGING);
346 ast_queue_control(ast, AST_CONTROL_RINGING);
347 return 0;
350 static int phone_hangup(struct ast_channel *ast)
352 struct phone_pvt *p;
353 p = ast->tech_pvt;
354 if (option_debug)
355 ast_log(LOG_DEBUG, "phone_hangup(%s)\n", ast->name);
356 if (!ast->tech_pvt) {
357 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
358 return 0;
360 /* XXX Is there anything we can do to really hang up except stop recording? */
361 ast_setstate(ast, AST_STATE_DOWN);
362 if (ioctl(p->fd, PHONE_REC_STOP))
363 ast_log(LOG_WARNING, "Failed to stop recording\n");
364 if (ioctl(p->fd, PHONE_PLAY_STOP))
365 ast_log(LOG_WARNING, "Failed to stop playing\n");
366 if (ioctl(p->fd, PHONE_RING_STOP))
367 ast_log(LOG_WARNING, "Failed to stop ringing\n");
368 if (ioctl(p->fd, PHONE_CPT_STOP))
369 ast_log(LOG_WARNING, "Failed to stop sounds\n");
371 /* If it's an FXO, hang them up */
372 if (p->mode == MODE_FXO) {
373 if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
374 ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",ast->name, strerror(errno));
377 /* If they're off hook, give a busy signal */
378 if (ioctl(p->fd, PHONE_HOOKSTATE)) {
379 if (option_debug)
380 ast_log(LOG_DEBUG, "Got hunghup, giving busy signal\n");
381 ioctl(p->fd, PHONE_BUSY);
382 p->cpt = 1;
384 p->lastformat = -1;
385 p->lastinput = -1;
386 p->ministate = 0;
387 p->obuflen = 0;
388 p->dialtone = 0;
389 memset(p->ext, 0, sizeof(p->ext));
390 ((struct phone_pvt *)(ast->tech_pvt))->owner = NULL;
391 ast_module_unref(ast_module_info->self);
392 if (option_verbose > 2)
393 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
394 ast->tech_pvt = NULL;
395 ast_setstate(ast, AST_STATE_DOWN);
396 restart_monitor();
397 return 0;
400 static int phone_setup(struct ast_channel *ast)
402 struct phone_pvt *p;
403 p = ast->tech_pvt;
404 ioctl(p->fd, PHONE_CPT_STOP);
405 /* Nothing to answering really, just start recording */
406 if (ast->rawreadformat == AST_FORMAT_G723_1) {
407 /* Prefer g723 */
408 ioctl(p->fd, PHONE_REC_STOP);
409 if (p->lastinput != AST_FORMAT_G723_1) {
410 p->lastinput = AST_FORMAT_G723_1;
411 if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
412 ast_log(LOG_WARNING, "Failed to set codec to g723.1\n");
413 return -1;
416 } else if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
417 ioctl(p->fd, PHONE_REC_STOP);
418 if (p->lastinput != AST_FORMAT_SLINEAR) {
419 p->lastinput = AST_FORMAT_SLINEAR;
420 if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
421 ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
422 return -1;
425 } else if (ast->rawreadformat == AST_FORMAT_ULAW) {
426 ioctl(p->fd, PHONE_REC_STOP);
427 if (p->lastinput != AST_FORMAT_ULAW) {
428 p->lastinput = AST_FORMAT_ULAW;
429 if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
430 ast_log(LOG_WARNING, "Failed to set codec to uLaw\n");
431 return -1;
434 } else if (p->mode == MODE_FXS) {
435 ioctl(p->fd, PHONE_REC_STOP);
436 if (p->lastinput != ast->rawreadformat) {
437 p->lastinput = ast->rawreadformat;
438 if (ioctl(p->fd, PHONE_REC_CODEC, ast->rawreadformat)) {
439 ast_log(LOG_WARNING, "Failed to set codec to %d\n",
440 ast->rawreadformat);
441 return -1;
444 } else {
445 ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast->rawreadformat));
446 return -1;
448 if (ioctl(p->fd, PHONE_REC_START)) {
449 ast_log(LOG_WARNING, "Failed to start recording\n");
450 return -1;
452 /* set the DTMF times (the default is too short) */
453 ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
454 ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
455 return 0;
458 static int phone_answer(struct ast_channel *ast)
460 struct phone_pvt *p;
461 p = ast->tech_pvt;
462 /* In case it's a LineJack, take it off hook */
463 if (p->mode == MODE_FXO) {
464 if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_OFF_HOOK))
465 ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n", ast->name, strerror(errno));
466 else
467 ast_log(LOG_DEBUG, "Took linejack off hook\n");
469 phone_setup(ast);
470 if (option_debug)
471 ast_log(LOG_DEBUG, "phone_answer(%s)\n", ast->name);
472 ast->rings = 0;
473 ast_setstate(ast, AST_STATE_UP);
474 return 0;
477 #if 0
478 static char phone_2digit(char c)
480 if (c == 12)
481 return '#';
482 else if (c == 11)
483 return '*';
484 else if ((c < 10) && (c >= 0))
485 return '0' + c - 1;
486 else
487 return '?';
489 #endif
491 static struct ast_frame *phone_exception(struct ast_channel *ast)
493 int res;
494 union telephony_exception phonee;
495 struct phone_pvt *p = ast->tech_pvt;
496 char digit;
498 /* Some nice norms */
499 p->fr.datalen = 0;
500 p->fr.samples = 0;
501 p->fr.data = NULL;
502 p->fr.src = "Phone";
503 p->fr.offset = 0;
504 p->fr.mallocd=0;
505 p->fr.delivery = ast_tv(0,0);
507 phonee.bytes = ioctl(p->fd, PHONE_EXCEPTION);
508 if (phonee.bits.dtmf_ready) {
509 if (option_debug)
510 ast_log(LOG_DEBUG, "phone_exception(): DTMF\n");
512 /* We've got a digit -- Just handle this nicely and easily */
513 digit = ioctl(p->fd, PHONE_GET_DTMF_ASCII);
514 p->fr.subclass = digit;
515 p->fr.frametype = AST_FRAME_DTMF;
516 return &p->fr;
518 if (phonee.bits.hookstate) {
519 if (option_debug)
520 ast_log(LOG_DEBUG, "Hookstate changed\n");
521 res = ioctl(p->fd, PHONE_HOOKSTATE);
522 /* See if we've gone on hook, if so, notify by returning NULL */
523 if (option_debug)
524 ast_log(LOG_DEBUG, "New hookstate: %d\n", res);
525 if (!res && (p->mode != MODE_FXO))
526 return NULL;
527 else {
528 if (ast->_state == AST_STATE_RINGING) {
529 /* They've picked up the phone */
530 p->fr.frametype = AST_FRAME_CONTROL;
531 p->fr.subclass = AST_CONTROL_ANSWER;
532 phone_setup(ast);
533 ast_setstate(ast, AST_STATE_UP);
534 return &p->fr;
535 } else
536 ast_log(LOG_WARNING, "Got off hook in weird state %d\n", ast->_state);
539 #if 1
540 if (phonee.bits.pstn_ring)
541 ast_verbose("Unit is ringing\n");
542 if (phonee.bits.caller_id) {
543 ast_verbose("We have caller ID\n");
545 if (phonee.bits.pstn_wink)
546 ast_verbose("Detected Wink\n");
547 #endif
548 /* Strange -- nothing there.. */
549 p->fr.frametype = AST_FRAME_NULL;
550 p->fr.subclass = 0;
551 return &p->fr;
554 static struct ast_frame *phone_read(struct ast_channel *ast)
556 int res;
557 struct phone_pvt *p = ast->tech_pvt;
560 /* Some nice norms */
561 p->fr.datalen = 0;
562 p->fr.samples = 0;
563 p->fr.data = NULL;
564 p->fr.src = "Phone";
565 p->fr.offset = 0;
566 p->fr.mallocd=0;
567 p->fr.delivery = ast_tv(0,0);
569 /* Try to read some data... */
570 CHECK_BLOCKING(ast);
571 res = read(p->fd, p->buf, PHONE_MAX_BUF);
572 ast_clear_flag(ast, AST_FLAG_BLOCKING);
573 if (res < 0) {
574 #if 0
575 if (errno == EAGAIN) {
576 ast_log(LOG_WARNING, "Null frame received\n");
577 p->fr.frametype = AST_FRAME_NULL;
578 p->fr.subclass = 0;
579 return &p->fr;
581 #endif
582 ast_log(LOG_WARNING, "Error reading: %s\n", strerror(errno));
583 return NULL;
585 p->fr.data = p->buf;
586 if (p->mode != MODE_FXS)
587 switch(p->buf[0] & 0x3) {
588 case '0':
589 case '1':
590 /* Normal */
591 break;
592 case '2':
593 case '3':
594 /* VAD/CNG, only send two words */
595 res = 4;
596 break;
598 p->fr.samples = 240;
599 p->fr.datalen = res;
600 p->fr.frametype = p->lastinput <= AST_FORMAT_MAX_AUDIO ?
601 AST_FRAME_VOICE :
602 p->lastinput <= AST_FORMAT_PNG ? AST_FRAME_IMAGE
603 : AST_FRAME_VIDEO;
604 p->fr.subclass = p->lastinput;
605 p->fr.offset = AST_FRIENDLY_OFFSET;
606 /* Byteswap from little-endian to native-endian */
607 if (p->fr.subclass == AST_FORMAT_SLINEAR)
608 ast_frame_byteswap_le(&p->fr);
609 return &p->fr;
612 static int phone_write_buf(struct phone_pvt *p, const char *buf, int len, int frlen, int swap)
614 int res;
615 /* Store as much of the buffer as we can, then write fixed frames */
616 int space = sizeof(p->obuf) - p->obuflen;
617 /* Make sure we have enough buffer space to store the frame */
618 if (space < len)
619 len = space;
620 if (swap)
621 ast_swapcopy_samples(p->obuf+p->obuflen, buf, len/2);
622 else
623 memcpy(p->obuf + p->obuflen, buf, len);
624 p->obuflen += len;
625 while(p->obuflen > frlen) {
626 res = write(p->fd, p->obuf, frlen);
627 if (res != frlen) {
628 if (res < 1) {
630 * Card is in non-blocking mode now and it works well now, but there are
631 * lot of messages like this. So, this message is temporarily disabled.
633 return 0;
634 } else {
635 ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
638 p->obuflen -= frlen;
639 /* Move memory if necessary */
640 if (p->obuflen)
641 memmove(p->obuf, p->obuf + frlen, p->obuflen);
643 return len;
646 static int phone_send_text(struct ast_channel *ast, const char *text)
648 int length = strlen(text);
649 return phone_write_buf(ast->tech_pvt, text, length, length, 0) ==
650 length ? 0 : -1;
653 static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
655 struct phone_pvt *p = ast->tech_pvt;
656 int res;
657 int maxfr=0;
658 char *pos;
659 int sofar;
660 int expected;
661 int codecset = 0;
662 char tmpbuf[4];
663 /* Write a frame of (presumably voice) data */
664 if (frame->frametype != AST_FRAME_VOICE && p->mode != MODE_FXS) {
665 if (frame->frametype != AST_FRAME_IMAGE)
666 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
667 return 0;
669 if (!(frame->subclass &
670 (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW)) &&
671 p->mode != MODE_FXS) {
672 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
673 return -1;
675 #if 0
676 /* If we're not in up mode, go into up mode now */
677 if (ast->_state != AST_STATE_UP) {
678 ast_setstate(ast, AST_STATE_UP);
679 phone_setup(ast);
681 #else
682 if (ast->_state != AST_STATE_UP) {
683 /* Don't try tos end audio on-hook */
684 return 0;
686 #endif
687 if (frame->subclass == AST_FORMAT_G723_1) {
688 if (p->lastformat != AST_FORMAT_G723_1) {
689 ioctl(p->fd, PHONE_PLAY_STOP);
690 ioctl(p->fd, PHONE_REC_STOP);
691 if (ioctl(p->fd, PHONE_PLAY_CODEC, G723_63)) {
692 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
693 return -1;
695 if (ioctl(p->fd, PHONE_REC_CODEC, G723_63)) {
696 ast_log(LOG_WARNING, "Unable to set G723.1 mode\n");
697 return -1;
699 p->lastformat = AST_FORMAT_G723_1;
700 p->lastinput = AST_FORMAT_G723_1;
701 /* Reset output buffer */
702 p->obuflen = 0;
703 codecset = 1;
705 if (frame->datalen > 24) {
706 ast_log(LOG_WARNING, "Frame size too large for G.723.1 (%d bytes)\n", frame->datalen);
707 return -1;
709 maxfr = 24;
710 } else if (frame->subclass == AST_FORMAT_SLINEAR) {
711 if (p->lastformat != AST_FORMAT_SLINEAR) {
712 ioctl(p->fd, PHONE_PLAY_STOP);
713 ioctl(p->fd, PHONE_REC_STOP);
714 if (ioctl(p->fd, PHONE_PLAY_CODEC, LINEAR16)) {
715 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
716 return -1;
718 if (ioctl(p->fd, PHONE_REC_CODEC, LINEAR16)) {
719 ast_log(LOG_WARNING, "Unable to set 16-bit linear mode\n");
720 return -1;
722 p->lastformat = AST_FORMAT_SLINEAR;
723 p->lastinput = AST_FORMAT_SLINEAR;
724 codecset = 1;
725 /* Reset output buffer */
726 p->obuflen = 0;
728 maxfr = 480;
729 } else if (frame->subclass == AST_FORMAT_ULAW) {
730 if (p->lastformat != AST_FORMAT_ULAW) {
731 ioctl(p->fd, PHONE_PLAY_STOP);
732 ioctl(p->fd, PHONE_REC_STOP);
733 if (ioctl(p->fd, PHONE_PLAY_CODEC, ULAW)) {
734 ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
735 return -1;
737 if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
738 ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
739 return -1;
741 p->lastformat = AST_FORMAT_ULAW;
742 p->lastinput = AST_FORMAT_ULAW;
743 codecset = 1;
744 /* Reset output buffer */
745 p->obuflen = 0;
747 maxfr = 240;
748 } else {
749 if (p->lastformat != frame->subclass) {
750 ioctl(p->fd, PHONE_PLAY_STOP);
751 ioctl(p->fd, PHONE_REC_STOP);
752 if (ioctl(p->fd, PHONE_PLAY_CODEC, frame->subclass)) {
753 ast_log(LOG_WARNING, "Unable to set %d mode\n",
754 frame->subclass);
755 return -1;
757 if (ioctl(p->fd, PHONE_REC_CODEC, frame->subclass)) {
758 ast_log(LOG_WARNING, "Unable to set %d mode\n",
759 frame->subclass);
760 return -1;
762 p->lastformat = frame->subclass;
763 p->lastinput = frame->subclass;
764 codecset = 1;
765 /* Reset output buffer */
766 p->obuflen = 0;
768 maxfr = 480;
770 if (codecset) {
771 ioctl(p->fd, PHONE_REC_DEPTH, 3);
772 ioctl(p->fd, PHONE_PLAY_DEPTH, 3);
773 if (ioctl(p->fd, PHONE_PLAY_START)) {
774 ast_log(LOG_WARNING, "Failed to start playback\n");
775 return -1;
777 if (ioctl(p->fd, PHONE_REC_START)) {
778 ast_log(LOG_WARNING, "Failed to start recording\n");
779 return -1;
782 /* If we get here, we have a frame of Appropriate data */
783 sofar = 0;
784 pos = frame->data;
785 while(sofar < frame->datalen) {
786 /* Write in no more than maxfr sized frames */
787 expected = frame->datalen - sofar;
788 if (maxfr < expected)
789 expected = maxfr;
790 /* XXX Internet Phone Jack does not handle the 4-byte VAD frame properly! XXX
791 we have to pad it to 24 bytes still. */
792 if (frame->datalen == 4) {
793 if (p->silencesupression) {
794 memset(tmpbuf + 4, 0, sizeof(tmpbuf) - 4);
795 memcpy(tmpbuf, frame->data, 4);
796 expected = 24;
797 res = phone_write_buf(p, tmpbuf, expected, maxfr, 0);
799 res = 4;
800 expected=4;
801 } else {
802 int swap = 0;
803 #if __BYTE_ORDER == __BIG_ENDIAN
804 if (frame->subclass == AST_FORMAT_SLINEAR)
805 swap = 1; /* Swap big-endian samples to little-endian as we copy */
806 #endif
807 res = phone_write_buf(p, pos, expected, maxfr, swap);
809 if (res != expected) {
810 if ((errno != EAGAIN) && (errno != EINTR)) {
811 if (res < 0)
812 ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
814 * Card is in non-blocking mode now and it works well now, but there are
815 * lot of messages like this. So, this message is temporarily disabled.
817 #if 0
818 else
819 ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
820 #endif
821 return -1;
822 } else /* Pretend it worked */
823 res = expected;
825 sofar += res;
826 pos += res;
828 return 0;
831 static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *context)
833 struct ast_channel *tmp;
834 struct phone_codec_data codec;
835 tmp = ast_channel_alloc(1, state, i->cid_num, i->cid_name, "", i->ext, i->context, 0, "Phone/%s", i->dev + 5);
836 if (tmp) {
837 tmp->tech = cur_tech;
838 tmp->fds[0] = i->fd;
839 /* XXX Switching formats silently causes kernel panics XXX */
840 if (i->mode == MODE_FXS &&
841 ioctl(i->fd, PHONE_QUERY_CODEC, &codec) == 0) {
842 if (codec.type == LINEAR16)
843 tmp->nativeformats =
844 tmp->rawreadformat =
845 tmp->rawwriteformat =
846 AST_FORMAT_SLINEAR;
847 else {
848 tmp->nativeformats =
849 tmp->rawreadformat =
850 tmp->rawwriteformat =
851 prefformat & ~AST_FORMAT_SLINEAR;
854 else {
855 tmp->nativeformats = prefformat;
856 tmp->rawreadformat = prefformat;
857 tmp->rawwriteformat = prefformat;
859 /* no need to call ast_setstate: the channel_alloc already did its job */
860 if (state == AST_STATE_RING)
861 tmp->rings = 1;
862 tmp->tech_pvt = i;
863 ast_copy_string(tmp->context, context, sizeof(tmp->context));
864 if (!ast_strlen_zero(i->ext))
865 ast_copy_string(tmp->exten, i->ext, sizeof(tmp->exten));
866 else
867 strcpy(tmp->exten, "s");
868 if (!ast_strlen_zero(i->language))
869 ast_string_field_set(tmp, language, i->language);
871 /* Don't use ast_set_callerid() here because it will
872 * generate a NewCallerID event before the NewChannel event */
873 tmp->cid.cid_ani = ast_strdup(i->cid_num);
875 i->owner = tmp;
876 ast_module_ref(ast_module_info->self);
877 if (state != AST_STATE_DOWN) {
878 if (state == AST_STATE_RING) {
879 ioctl(tmp->fds[0], PHONE_RINGBACK);
880 i->cpt = 1;
882 if (ast_pbx_start(tmp)) {
883 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
884 ast_hangup(tmp);
887 } else
888 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
889 return tmp;
892 static void phone_mini_packet(struct phone_pvt *i)
894 int res;
895 char buf[1024];
896 /* Ignore stuff we read... */
897 res = read(i->fd, buf, sizeof(buf));
898 if (res < 1) {
899 ast_log(LOG_WARNING, "Read returned %d: %s\n", res, strerror(errno));
900 return;
904 static void phone_check_exception(struct phone_pvt *i)
906 int offhook=0;
907 char digit[2] = {0 , 0};
908 union telephony_exception phonee;
909 /* XXX Do something XXX */
910 #if 0
911 ast_log(LOG_DEBUG, "Exception!\n");
912 #endif
913 phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION);
914 if (phonee.bits.dtmf_ready) {
915 digit[0] = ioctl(i->fd, PHONE_GET_DTMF_ASCII);
916 if (i->mode == MODE_DIALTONE || i->mode == MODE_FXS || i->mode == MODE_SIGMA) {
917 ioctl(i->fd, PHONE_PLAY_STOP);
918 ioctl(i->fd, PHONE_REC_STOP);
919 ioctl(i->fd, PHONE_CPT_STOP);
920 i->dialtone = 0;
921 if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
922 strncat(i->ext, digit, sizeof(i->ext) - strlen(i->ext) - 1);
923 if ((i->mode != MODE_FXS ||
924 !(phonee.bytes = ioctl(i->fd, PHONE_EXCEPTION)) ||
925 !phonee.bits.dtmf_ready) &&
926 ast_exists_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
927 /* It's a valid extension in its context, get moving! */
928 phone_new(i, AST_STATE_RING, i->context);
929 /* No need to restart monitor, we are the monitor */
930 } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->cid_num)) {
931 /* There is nothing in the specified extension that can match anymore.
932 Try the default */
933 if (ast_exists_extension(NULL, "default", i->ext, 1, i->cid_num)) {
934 /* Check the default, too... */
935 phone_new(i, AST_STATE_RING, "default");
936 /* XXX This should probably be justified better XXX */
937 } else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->cid_num)) {
938 /* It's not a valid extension, give a busy signal */
939 if (option_debug)
940 ast_log(LOG_DEBUG, "%s can't match anything in %s or default\n", i->ext, i->context);
941 ioctl(i->fd, PHONE_BUSY);
942 i->cpt = 1;
945 #if 0
946 ast_verbose("Extension is %s\n", i->ext);
947 #endif
950 if (phonee.bits.hookstate) {
951 offhook = ioctl(i->fd, PHONE_HOOKSTATE);
952 if (offhook) {
953 if (i->mode == MODE_IMMEDIATE) {
954 phone_new(i, AST_STATE_RING, i->context);
955 } else if (i->mode == MODE_DIALTONE) {
956 ast_module_ref(ast_module_info->self);
957 /* Reset the extension */
958 i->ext[0] = '\0';
959 /* Play the dialtone */
960 i->dialtone++;
961 ioctl(i->fd, PHONE_PLAY_STOP);
962 ioctl(i->fd, PHONE_PLAY_CODEC, ULAW);
963 ioctl(i->fd, PHONE_PLAY_START);
964 i->lastformat = -1;
965 } else if (i->mode == MODE_SIGMA) {
966 ast_module_ref(ast_module_info->self);
967 /* Reset the extension */
968 i->ext[0] = '\0';
969 /* Play the dialtone */
970 i->dialtone++;
971 ioctl(i->fd, PHONE_DIALTONE);
973 } else {
974 if (i->dialtone)
975 ast_module_unref(ast_module_info->self);
976 memset(i->ext, 0, sizeof(i->ext));
977 if (i->cpt)
979 ioctl(i->fd, PHONE_CPT_STOP);
980 i->cpt = 0;
982 ioctl(i->fd, PHONE_PLAY_STOP);
983 ioctl(i->fd, PHONE_REC_STOP);
984 i->dialtone = 0;
985 i->lastformat = -1;
988 if (phonee.bits.pstn_ring) {
989 ast_verbose("Unit is ringing\n");
990 phone_new(i, AST_STATE_RING, i->context);
992 if (phonee.bits.caller_id)
993 ast_verbose("We have caller ID\n");
998 static void *do_monitor(void *data)
1000 fd_set rfds, efds;
1001 int n, res;
1002 struct phone_pvt *i;
1003 int tonepos = 0;
1004 /* The tone we're playing this round */
1005 struct timeval tv = {0,0};
1006 int dotone;
1007 /* This thread monitors all the frame relay interfaces which are not yet in use
1008 (and thus do not have a separate thread) indefinitely */
1009 while (monitor) {
1010 /* Don't let anybody kill us right away. Nobody should lock the interface list
1011 and wait for the monitor list, but the other way around is okay. */
1012 /* Lock the interface list */
1013 if (ast_mutex_lock(&iflock)) {
1014 ast_log(LOG_ERROR, "Unable to grab interface lock\n");
1015 return NULL;
1017 /* Build the stuff we're going to select on, that is the socket of every
1018 phone_pvt that does not have an associated owner channel */
1019 n = -1;
1020 FD_ZERO(&rfds);
1021 FD_ZERO(&efds);
1022 i = iflist;
1023 dotone = 0;
1024 while (i) {
1025 if (FD_ISSET(i->fd, &rfds))
1026 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
1027 if (!i->owner) {
1028 /* This needs to be watched, as it lacks an owner */
1029 FD_SET(i->fd, &rfds);
1030 FD_SET(i->fd, &efds);
1031 if (i->fd > n)
1032 n = i->fd;
1033 if (i->dialtone && i->mode != MODE_SIGMA) {
1034 /* Remember we're going to have to come back and play
1035 more dialtones */
1036 if (ast_tvzero(tv)) {
1037 /* If we're due for a dialtone, play one */
1038 if (write(i->fd, DialTone + tonepos, 240) != 240)
1039 ast_log(LOG_WARNING, "Dial tone write error\n");
1041 dotone++;
1045 i = i->next;
1047 /* Okay, now that we know what to do, release the interface lock */
1048 ast_mutex_unlock(&iflock);
1050 /* Wait indefinitely for something to happen */
1051 if (dotone && i && i->mode != MODE_SIGMA) {
1052 /* If we're ready to recycle the time, set it to 30 ms */
1053 tonepos += 240;
1054 if (tonepos >= sizeof(DialTone))
1055 tonepos = 0;
1056 if (ast_tvzero(tv)) {
1057 tv = ast_tv(30000, 0);
1059 res = ast_select(n + 1, &rfds, NULL, &efds, &tv);
1060 } else {
1061 res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
1062 tv = ast_tv(0,0);
1063 tonepos = 0;
1065 /* Okay, select has finished. Let's see what happened. */
1066 if (res < 0) {
1067 ast_log(LOG_DEBUG, "select return %d: %s\n", res, strerror(errno));
1068 continue;
1070 /* If there are no fd's changed, just continue, it's probably time
1071 to play some more dialtones */
1072 if (!res)
1073 continue;
1074 /* Alright, lock the interface list again, and let's look and see what has
1075 happened */
1076 if (ast_mutex_lock(&iflock)) {
1077 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1078 continue;
1081 i = iflist;
1082 for(; i; i=i->next) {
1083 if (FD_ISSET(i->fd, &rfds)) {
1084 if (i->owner) {
1085 continue;
1087 phone_mini_packet(i);
1089 if (FD_ISSET(i->fd, &efds)) {
1090 if (i->owner) {
1091 continue;
1093 phone_check_exception(i);
1096 ast_mutex_unlock(&iflock);
1098 return NULL;
1102 static int restart_monitor()
1104 /* If we're supposed to be stopped -- stay stopped */
1105 if (monitor_thread == AST_PTHREADT_STOP)
1106 return 0;
1107 if (ast_mutex_lock(&monlock)) {
1108 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1109 return -1;
1111 if (monitor_thread == pthread_self()) {
1112 ast_mutex_unlock(&monlock);
1113 ast_log(LOG_WARNING, "Cannot kill myself\n");
1114 return -1;
1116 if (monitor_thread != AST_PTHREADT_NULL) {
1117 if (ast_mutex_lock(&iflock)) {
1118 ast_mutex_unlock(&monlock);
1119 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1120 return -1;
1122 monitor = 0;
1123 while (pthread_kill(monitor_thread, SIGURG) == 0)
1124 sched_yield();
1125 pthread_join(monitor_thread, NULL);
1126 ast_mutex_unlock(&iflock);
1128 monitor = 1;
1129 /* Start a new monitor */
1130 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
1131 ast_mutex_unlock(&monlock);
1132 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1133 return -1;
1135 ast_mutex_unlock(&monlock);
1136 return 0;
1139 static struct phone_pvt *mkif(char *iface, int mode, int txgain, int rxgain)
1141 /* Make a phone_pvt structure for this interface */
1142 struct phone_pvt *tmp;
1143 int flags;
1145 tmp = malloc(sizeof(struct phone_pvt));
1146 if (tmp) {
1147 tmp->fd = open(iface, O_RDWR);
1148 if (tmp->fd < 0) {
1149 ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
1150 free(tmp);
1151 return NULL;
1153 if (mode == MODE_FXO) {
1154 if (ioctl(tmp->fd, IXJCTL_PORT, PORT_PSTN))
1155 ast_log(LOG_DEBUG, "Unable to set port to PSTN\n");
1156 } else {
1157 if (ioctl(tmp->fd, IXJCTL_PORT, PORT_POTS))
1158 if (mode != MODE_FXS)
1159 ast_log(LOG_DEBUG, "Unable to set port to POTS\n");
1161 ioctl(tmp->fd, PHONE_PLAY_STOP);
1162 ioctl(tmp->fd, PHONE_REC_STOP);
1163 ioctl(tmp->fd, PHONE_RING_STOP);
1164 ioctl(tmp->fd, PHONE_CPT_STOP);
1165 if (ioctl(tmp->fd, PHONE_PSTN_SET_STATE, PSTN_ON_HOOK))
1166 ast_log(LOG_DEBUG, "ioctl(PHONE_PSTN_SET_STATE) failed on %s (%s)\n",iface, strerror(errno));
1167 if (echocancel != AEC_OFF)
1168 ioctl(tmp->fd, IXJCTL_AEC_START, echocancel);
1169 if (silencesupression)
1170 tmp->silencesupression = 1;
1171 #ifdef PHONE_VAD
1172 ioctl(tmp->fd, PHONE_VAD, tmp->silencesupression);
1173 #endif
1174 tmp->mode = mode;
1175 flags = fcntl(tmp->fd, F_GETFL);
1176 fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
1177 tmp->owner = NULL;
1178 tmp->lastformat = -1;
1179 tmp->lastinput = -1;
1180 tmp->ministate = 0;
1181 memset(tmp->ext, 0, sizeof(tmp->ext));
1182 ast_copy_string(tmp->language, language, sizeof(tmp->language));
1183 ast_copy_string(tmp->dev, iface, sizeof(tmp->dev));
1184 ast_copy_string(tmp->context, context, sizeof(tmp->context));
1185 tmp->next = NULL;
1186 tmp->obuflen = 0;
1187 tmp->dialtone = 0;
1188 tmp->cpt = 0;
1189 ast_copy_string(tmp->cid_num, cid_num, sizeof(tmp->cid_num));
1190 ast_copy_string(tmp->cid_name, cid_name, sizeof(tmp->cid_name));
1191 tmp->txgain = txgain;
1192 ioctl(tmp->fd, PHONE_PLAY_VOLUME, tmp->txgain);
1193 tmp->rxgain = rxgain;
1194 ioctl(tmp->fd, PHONE_REC_VOLUME, tmp->rxgain);
1196 return tmp;
1199 static struct ast_channel *phone_request(const char *type, int format, void *data, int *cause)
1201 int oldformat;
1202 struct phone_pvt *p;
1203 struct ast_channel *tmp = NULL;
1204 char *name = data;
1206 /* Search for an unowned channel */
1207 if (ast_mutex_lock(&iflock)) {
1208 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1209 return NULL;
1211 p = iflist;
1212 while(p) {
1213 if (p->mode == MODE_FXS ||
1214 format & (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW)) {
1215 size_t length = strlen(p->dev + 5);
1216 if (strncmp(name, p->dev + 5, length) == 0 &&
1217 !isalnum(name[length])) {
1218 if (!p->owner) {
1219 tmp = phone_new(p, AST_STATE_DOWN, p->context);
1220 break;
1221 } else
1222 *cause = AST_CAUSE_BUSY;
1225 p = p->next;
1227 ast_mutex_unlock(&iflock);
1228 restart_monitor();
1229 if (tmp == NULL) {
1230 oldformat = format;
1231 format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
1232 if (!format) {
1233 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
1234 return NULL;
1237 return tmp;
1240 /* parse gain value from config file */
1241 static int parse_gain_value(char *gain_type, char *value)
1243 float gain;
1245 /* try to scan number */
1246 if (sscanf(value, "%f", &gain) != 1)
1248 ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n",
1249 value, gain_type, config);
1250 return DEFAULT_GAIN;
1253 /* multiplicate gain by 1.0 gain value */
1254 gain = gain * (float)DEFAULT_GAIN;
1256 /* percentage? */
1257 if (value[strlen(value) - 1] == '%')
1258 return (int)(gain / (float)100);
1260 return (int)gain;
1263 static int __unload_module(void)
1265 struct phone_pvt *p, *pl;
1266 /* First, take us out of the channel loop */
1267 if (cur_tech)
1268 ast_channel_unregister(cur_tech);
1269 if (!ast_mutex_lock(&iflock)) {
1270 /* Hangup all interfaces if they have an owner */
1271 p = iflist;
1272 while(p) {
1273 if (p->owner)
1274 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1275 p = p->next;
1277 iflist = NULL;
1278 ast_mutex_unlock(&iflock);
1279 } else {
1280 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1281 return -1;
1283 if (!ast_mutex_lock(&monlock)) {
1284 if (monitor_thread > AST_PTHREADT_NULL) {
1285 monitor = 0;
1286 while (pthread_kill(monitor_thread, SIGURG) == 0)
1287 sched_yield();
1288 pthread_join(monitor_thread, NULL);
1290 monitor_thread = AST_PTHREADT_STOP;
1291 ast_mutex_unlock(&monlock);
1292 } else {
1293 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1294 return -1;
1297 if (!ast_mutex_lock(&iflock)) {
1298 /* Destroy all the interfaces and free their memory */
1299 p = iflist;
1300 while(p) {
1301 /* Close the socket, assuming it's real */
1302 if (p->fd > -1)
1303 close(p->fd);
1304 pl = p;
1305 p = p->next;
1306 /* Free associated memory */
1307 free(pl);
1309 iflist = NULL;
1310 ast_mutex_unlock(&iflock);
1311 } else {
1312 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1313 return -1;
1316 return 0;
1319 static int unload_module(void)
1321 return __unload_module();
1324 static int load_module(void)
1326 struct ast_config *cfg;
1327 struct ast_variable *v;
1328 struct phone_pvt *tmp;
1329 int mode = MODE_IMMEDIATE;
1330 int txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; /* default gain 1.0 */
1331 cfg = ast_config_load(config);
1333 /* We *must* have a config file otherwise stop immediately */
1334 if (!cfg) {
1335 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1336 return AST_MODULE_LOAD_DECLINE;
1338 if (ast_mutex_lock(&iflock)) {
1339 /* It's a little silly to lock it, but we mind as well just to be sure */
1340 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
1341 return AST_MODULE_LOAD_FAILURE;
1343 v = ast_variable_browse(cfg, "interfaces");
1344 while(v) {
1345 /* Create the interface list */
1346 if (!strcasecmp(v->name, "device")) {
1347 tmp = mkif(v->value, mode, txgain, rxgain);
1348 if (tmp) {
1349 tmp->next = iflist;
1350 iflist = tmp;
1352 } else {
1353 ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
1354 ast_config_destroy(cfg);
1355 ast_mutex_unlock(&iflock);
1356 __unload_module();
1357 return AST_MODULE_LOAD_FAILURE;
1359 } else if (!strcasecmp(v->name, "silencesupression")) {
1360 silencesupression = ast_true(v->value);
1361 } else if (!strcasecmp(v->name, "language")) {
1362 ast_copy_string(language, v->value, sizeof(language));
1363 } else if (!strcasecmp(v->name, "callerid")) {
1364 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
1365 } else if (!strcasecmp(v->name, "mode")) {
1366 if (!strncasecmp(v->value, "di", 2))
1367 mode = MODE_DIALTONE;
1368 else if (!strncasecmp(v->value, "sig", 3))
1369 mode = MODE_SIGMA;
1370 else if (!strncasecmp(v->value, "im", 2))
1371 mode = MODE_IMMEDIATE;
1372 else if (!strncasecmp(v->value, "fxs", 3)) {
1373 mode = MODE_FXS;
1374 prefformat = 0x01ff0000; /* All non-voice */
1376 else if (!strncasecmp(v->value, "fx", 2))
1377 mode = MODE_FXO;
1378 else
1379 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
1380 } else if (!strcasecmp(v->name, "context")) {
1381 ast_copy_string(context, v->value, sizeof(context));
1382 } else if (!strcasecmp(v->name, "format")) {
1383 if (!strcasecmp(v->value, "g723.1")) {
1384 prefformat = AST_FORMAT_G723_1;
1385 } else if (!strcasecmp(v->value, "slinear")) {
1386 if (mode == MODE_FXS)
1387 prefformat |= AST_FORMAT_SLINEAR;
1388 else prefformat = AST_FORMAT_SLINEAR;
1389 } else if (!strcasecmp(v->value, "ulaw")) {
1390 prefformat = AST_FORMAT_ULAW;
1391 } else
1392 ast_log(LOG_WARNING, "Unknown format '%s'\n", v->value);
1393 } else if (!strcasecmp(v->name, "echocancel")) {
1394 if (!strcasecmp(v->value, "off")) {
1395 echocancel = AEC_OFF;
1396 } else if (!strcasecmp(v->value, "low")) {
1397 echocancel = AEC_LOW;
1398 } else if (!strcasecmp(v->value, "medium")) {
1399 echocancel = AEC_MED;
1400 } else if (!strcasecmp(v->value, "high")) {
1401 echocancel = AEC_HIGH;
1402 } else
1403 ast_log(LOG_WARNING, "Unknown echo cancellation '%s'\n", v->value);
1404 } else if (!strcasecmp(v->name, "txgain")) {
1405 txgain = parse_gain_value(v->name, v->value);
1406 } else if (!strcasecmp(v->name, "rxgain")) {
1407 rxgain = parse_gain_value(v->name, v->value);
1409 v = v->next;
1411 ast_mutex_unlock(&iflock);
1413 if (mode == MODE_FXS) {
1414 phone_tech_fxs.capabilities = prefformat;
1415 cur_tech = &phone_tech_fxs;
1416 } else
1417 cur_tech = (struct ast_channel_tech *) &phone_tech;
1419 /* Make sure we can register our Adtranphone channel type */
1421 if (ast_channel_register(cur_tech)) {
1422 ast_log(LOG_ERROR, "Unable to register channel class 'Phone'\n");
1423 ast_config_destroy(cfg);
1424 __unload_module();
1425 return AST_MODULE_LOAD_FAILURE;
1427 ast_config_destroy(cfg);
1428 /* And start the monitor for the first time */
1429 restart_monitor();
1430 return AST_MODULE_LOAD_SUCCESS;
1433 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Linux Telephony API Support");