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.
21 * \brief Generic Linux Telephony Interface driver
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup channel_drivers
29 <depend>ixjuser</depend>
34 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
39 #include <sys/socket.h>
44 #include <arpa/inet.h>
46 #include <sys/ioctl.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"
68 #ifdef QTI_PHONEJACK_TJ_PCI /* check for the newer quicknet driver v.3.1.0 which has this symbol */
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);
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
] = "";
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
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 */
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
];
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
= {
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
,
181 .hangup
= phone_hangup
,
182 .answer
= phone_answer
,
184 .write
= phone_write
,
185 .exception
= phone_exception
,
186 .indicate
= phone_indicate
,
190 static struct ast_channel_tech phone_tech_fxs
= {
192 .description
= tdesc
,
193 .requester
= phone_request
,
194 .send_digit_begin
= phone_digit_begin
,
195 .send_digit_end
= phone_digit_end
,
197 .hangup
= phone_hangup
,
198 .answer
= phone_answer
,
200 .write
= phone_write
,
201 .exception
= phone_exception
,
202 .write_video
= phone_write
,
203 .send_text
= phone_send_text
,
204 .indicate
= phone_indicate
,
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
;
214 ast_log(LOG_DEBUG
, "Requested indication %d on channel %s\n", condition
, chan
->name
);
216 case AST_CONTROL_FLASH
:
217 ioctl(p
->fd
, IXJCTL_PSTN_SET_STATE
, PSTN_ON_HOOK
);
219 ioctl(p
->fd
, IXJCTL_PSTN_SET_STATE
, PSTN_OFF_HOOK
);
223 case AST_CONTROL_HOLD
:
224 ast_moh_start(chan
, data
, NULL
);
226 case AST_CONTROL_UNHOLD
:
230 ast_log(LOG_WARNING
, "Condition %d is not supported on channel %s\n", condition
, chan
->name
);
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
)
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 */
249 static int phone_digit_end(struct ast_channel
*ast
, char digit
)
254 ast_log(LOG_NOTICE
, "Dialed %c\n", digit
);
266 outdigit
= digit
- '0';
276 ioctl(p
->fd
, IXJCTL_PSTN_SET_STATE
, PSTN_ON_HOOK
);
278 ioctl(p
->fd
, IXJCTL_PSTN_SET_STATE
, PSTN_OFF_HOOK
);
282 ast_log(LOG_WARNING
, "Unknown digit '%c'\n", digit
);
285 ast_log(LOG_NOTICE
, "Dialed %d\n", outdigit
);
286 ioctl(p
->fd
, PHONE_PLAY_TONE
, outdigit
);
291 static int phone_call(struct ast_channel
*ast
, char *dest
, int timeout
)
301 localtime_r(&UtcTime
,&tm
);
303 memset(&cid
, 0, sizeof(PHONE_CID
));
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
);
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
));
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
);
326 ast_log(LOG_DEBUG
, "Ringing %s on %s (%d)\n", dest
, ast
->name
, ast
->fds
[0]);
328 start
= IXJ_PHONE_RING_START(cid
);
332 if (p
->mode
== MODE_FXS
) {
333 char *digit
= strchr(dest
, '/');
338 phone_digit_end(ast
, *digit
++);
342 ast_setstate(ast
, AST_STATE_RINGING
);
343 ast_queue_control(ast
, AST_CONTROL_RINGING
);
347 static int phone_hangup(struct ast_channel
*ast
)
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");
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
)) {
377 ast_log(LOG_DEBUG
, "Got hunghup, giving busy signal\n");
378 ioctl(p
->fd
, PHONE_BUSY
);
386 memset(p
->ext
, 0, sizeof(p
->ext
));
387 ((struct phone_pvt
*)(ast
->tech_pvt
))->owner
= NULL
;
388 ast_mutex_lock(&usecnt_lock
);
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
);
402 static int phone_setup(struct ast_channel
*ast
)
406 ioctl(p
->fd
, PHONE_CPT_STOP
);
407 /* Nothing to answering really, just start recording */
408 if (ast
->rawreadformat
== AST_FORMAT_G723_1
) {
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");
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");
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");
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",
447 ast_log(LOG_WARNING
, "Can't do format %s\n", ast_getformatname(ast
->rawreadformat
));
450 if (ioctl(p
->fd
, PHONE_REC_START
)) {
451 ast_log(LOG_WARNING
, "Failed to start recording\n");
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);
460 static int phone_answer(struct ast_channel
*ast
)
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
));
469 ast_log(LOG_DEBUG
, "Took linejack off hook\n");
473 ast_log(LOG_DEBUG
, "phone_answer(%s)\n", ast
->name
);
475 ast_setstate(ast
, AST_STATE_UP
);
480 static char phone_2digit(char c
)
486 else if ((c
< 10) && (c
>= 0))
493 static struct ast_frame
*phone_exception(struct ast_channel
*ast
)
496 union telephony_exception phonee
;
497 struct phone_pvt
*p
= ast
->tech_pvt
;
500 /* Some nice norms */
507 p
->fr
.delivery
= ast_tv(0,0);
509 phonee
.bytes
= ioctl(p
->fd
, PHONE_EXCEPTION
);
510 if (phonee
.bits
.dtmf_ready
) {
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
;
520 if (phonee
.bits
.hookstate
) {
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 */
526 ast_log(LOG_DEBUG
, "New hookstate: %d\n", res
);
527 if (!res
&& (p
->mode
!= MODE_FXO
))
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
;
535 ast_setstate(ast
, AST_STATE_UP
);
538 ast_log(LOG_WARNING
, "Got off hook in weird state %d\n", ast
->_state
);
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");
550 /* Strange -- nothing there.. */
551 p
->fr
.frametype
= AST_FRAME_NULL
;
556 static struct ast_frame
*phone_read(struct ast_channel
*ast
)
559 struct phone_pvt
*p
= ast
->tech_pvt
;
562 /* Some nice norms */
569 p
->fr
.delivery
= ast_tv(0,0);
571 /* Try to read some data... */
573 res
= read(p
->fd
, p
->buf
, PHONE_MAX_BUF
);
574 ast_clear_flag(ast
, AST_FLAG_BLOCKING
);
577 if (errno
== EAGAIN
) {
578 ast_log(LOG_WARNING
, "Null frame received\n");
579 p
->fr
.frametype
= AST_FRAME_NULL
;
584 ast_log(LOG_WARNING
, "Error reading: %s\n", strerror(errno
));
588 if (p
->mode
!= MODE_FXS
)
589 switch(p
->buf
[0] & 0x3) {
596 /* VAD/CNG, only send two words */
602 p
->fr
.frametype
= p
->lastinput
<= AST_FORMAT_MAX_AUDIO
?
604 p
->lastinput
<= AST_FORMAT_PNG
? AST_FRAME_IMAGE
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
);
614 static int phone_write_buf(struct phone_pvt
*p
, const char *buf
, int len
, int frlen
, int swap
)
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 */
623 ast_swapcopy_samples(p
->obuf
+p
->obuflen
, buf
, len
/2);
625 memcpy(p
->obuf
+ p
->obuflen
, buf
, len
);
627 while(p
->obuflen
> frlen
) {
628 res
= write(p
->fd
, p
->obuf
, frlen
);
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.
637 ast_log(LOG_WARNING
, "Only wrote %d of %d bytes\n", res
, frlen
);
641 /* Move memory if necessary */
643 memmove(p
->obuf
, p
->obuf
+ frlen
, p
->obuflen
);
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) ==
655 static int phone_write(struct ast_channel
*ast
, struct ast_frame
*frame
)
657 struct phone_pvt
*p
= ast
->tech_pvt
;
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
);
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
);
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
);
684 if (ast
->_state
!= AST_STATE_UP
) {
685 /* Don't try tos end audio on-hook */
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");
697 if (ioctl(p
->fd
, PHONE_REC_CODEC
, G723_63
)) {
698 ast_log(LOG_WARNING
, "Unable to set G723.1 mode\n");
701 p
->lastformat
= AST_FORMAT_G723_1
;
702 p
->lastinput
= AST_FORMAT_G723_1
;
703 /* Reset output buffer */
707 if (frame
->datalen
> 24) {
708 ast_log(LOG_WARNING
, "Frame size too large for G.723.1 (%d bytes)\n", frame
->datalen
);
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");
720 if (ioctl(p
->fd
, PHONE_REC_CODEC
, LINEAR16
)) {
721 ast_log(LOG_WARNING
, "Unable to set 16-bit linear mode\n");
724 p
->lastformat
= AST_FORMAT_SLINEAR
;
725 p
->lastinput
= AST_FORMAT_SLINEAR
;
727 /* Reset output buffer */
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");
739 if (ioctl(p
->fd
, PHONE_REC_CODEC
, ULAW
)) {
740 ast_log(LOG_WARNING
, "Unable to set uLaw mode\n");
743 p
->lastformat
= AST_FORMAT_ULAW
;
744 p
->lastinput
= AST_FORMAT_ULAW
;
746 /* Reset output buffer */
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",
759 if (ioctl(p
->fd
, PHONE_REC_CODEC
, frame
->subclass
)) {
760 ast_log(LOG_WARNING
, "Unable to set %d mode\n",
764 p
->lastformat
= frame
->subclass
;
765 p
->lastinput
= frame
->subclass
;
767 /* Reset output buffer */
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");
779 if (ioctl(p
->fd
, PHONE_REC_START
)) {
780 ast_log(LOG_WARNING
, "Failed to start recording\n");
784 /* If we get here, we have a frame of Appropriate data */
787 while(sofar
< frame
->datalen
) {
788 /* Write in no more than maxfr sized frames */
789 expected
= frame
->datalen
- sofar
;
790 if (maxfr
< expected
)
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);
799 res
= phone_write_buf(p
, tmpbuf
, expected
, maxfr
, 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 */
809 res
= phone_write_buf(p
, pos
, expected
, maxfr
, swap
);
811 if (res
!= expected
) {
812 if ((errno
!= EAGAIN
) && (errno
!= EINTR
)) {
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.
821 ast_log(LOG_WARNING
, "Only wrote %d of %d bytes\n", res
, frame
->datalen
);
824 } else /* Pretend it worked */
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);
839 tmp
->tech
= cur_tech
;
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
)
847 tmp
->rawwriteformat
=
852 tmp
->rawwriteformat
=
853 prefformat
& ~AST_FORMAT_SLINEAR
;
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
)
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
));
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
);
880 ast_mutex_lock(&usecnt_lock
);
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
);
889 if (ast_pbx_start(tmp
)) {
890 ast_log(LOG_WARNING
, "Unable to start PBX on %s\n", tmp
->name
);
895 ast_log(LOG_WARNING
, "Unable to allocate channel structure\n");
899 static void phone_mini_packet(struct phone_pvt
*i
)
903 /* Ignore stuff we read... */
904 res
= read(i
->fd
, buf
, sizeof(buf
));
906 ast_log(LOG_WARNING
, "Read returned %d: %s\n", res
, strerror(errno
));
911 static void phone_check_exception(struct phone_pvt
*i
)
914 char digit
[2] = {0 , 0};
915 union telephony_exception phonee
;
916 /* XXX Do something XXX */
918 ast_log(LOG_DEBUG
, "Exception!\n");
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
);
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.
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 */
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
);
953 ast_verbose("Extension is %s\n", i
->ext
);
957 if (phonee
.bits
.hookstate
) {
958 offhook
= ioctl(i
->fd
, PHONE_HOOKSTATE
);
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
);
965 ast_mutex_unlock(&usecnt_lock
);
966 ast_update_use_count();
967 /* Reset the extension */
969 /* Play the dialtone */
971 ioctl(i
->fd
, PHONE_PLAY_STOP
);
972 ioctl(i
->fd
, PHONE_PLAY_CODEC
, ULAW
);
973 ioctl(i
->fd
, PHONE_PLAY_START
);
975 } else if (i
->mode
== MODE_SIGMA
) {
976 ast_mutex_lock(&usecnt_lock
);
978 ast_mutex_unlock(&usecnt_lock
);
979 ast_update_use_count();
980 /* Reset the extension */
982 /* Play the dialtone */
984 ioctl(i
->fd
, PHONE_DIALTONE
);
988 ast_mutex_lock(&usecnt_lock
);
990 ast_mutex_unlock(&usecnt_lock
);
991 ast_update_use_count();
993 memset(i
->ext
, 0, sizeof(i
->ext
));
996 ioctl(i
->fd
, PHONE_CPT_STOP
);
999 ioctl(i
->fd
, PHONE_PLAY_STOP
);
1000 ioctl(i
->fd
, PHONE_REC_STOP
);
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
)
1019 struct phone_pvt
*i
;
1021 /* The tone we're playing this round */
1022 struct timeval tv
= {0,0};
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 */
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");
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 */
1042 if (FD_ISSET(i
->fd
, &rfds
))
1043 ast_log(LOG_WARNING
, "Descriptor %d appears twice (%s)?\n", i
->fd
, i
->dev
);
1045 /* This needs to be watched, as it lacks an owner */
1046 FD_SET(i
->fd
, &rfds
);
1047 FD_SET(i
->fd
, &efds
);
1050 if (i
->dialtone
&& i
->mode
!= MODE_SIGMA
) {
1051 /* Remember we're going to have to come back and play
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");
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 */
1071 if (tonepos
>= sizeof(DialTone
))
1073 if (ast_tvzero(tv
)) {
1074 tv
= ast_tv(30000, 0);
1076 res
= ast_select(n
+ 1, &rfds
, NULL
, &efds
, &tv
);
1078 res
= ast_select(n
+ 1, &rfds
, NULL
, &efds
, NULL
);
1082 /* Okay, select has finished. Let's see what happened. */
1084 ast_log(LOG_DEBUG
, "select return %d: %s\n", res
, strerror(errno
));
1087 /* If there are no fd's changed, just continue, it's probably time
1088 to play some more dialtones */
1091 /* Alright, lock the interface list again, and let's look and see what has
1093 if (ast_mutex_lock(&iflock
)) {
1094 ast_log(LOG_WARNING
, "Unable to lock the interface list\n");
1099 for(; i
; i
=i
->next
) {
1100 if (FD_ISSET(i
->fd
, &rfds
)) {
1104 phone_mini_packet(i
);
1106 if (FD_ISSET(i
->fd
, &efds
)) {
1110 phone_check_exception(i
);
1113 ast_mutex_unlock(&iflock
);
1119 static int restart_monitor()
1121 /* If we're supposed to be stopped -- stay stopped */
1122 if (monitor_thread
== AST_PTHREADT_STOP
)
1124 if (ast_mutex_lock(&monlock
)) {
1125 ast_log(LOG_WARNING
, "Unable to lock monitor\n");
1128 if (monitor_thread
== pthread_self()) {
1129 ast_mutex_unlock(&monlock
);
1130 ast_log(LOG_WARNING
, "Cannot kill myself\n");
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");
1140 while (pthread_kill(monitor_thread
, SIGURG
) == 0)
1142 pthread_join(monitor_thread
, NULL
);
1143 ast_mutex_unlock(&iflock
);
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");
1152 ast_mutex_unlock(&monlock
);
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
;
1162 tmp
= malloc(sizeof(struct phone_pvt
));
1164 tmp
->fd
= open(iface
, O_RDWR
);
1166 ast_log(LOG_WARNING
, "Unable to open '%s'\n", iface
);
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");
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;
1189 ioctl(tmp
->fd
, PHONE_VAD
, tmp
->silencesupression
);
1192 flags
= fcntl(tmp
->fd
, F_GETFL
);
1193 fcntl(tmp
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
1195 tmp
->lastformat
= -1;
1196 tmp
->lastinput
= -1;
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
));
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
);
1216 static struct ast_channel
*phone_request(const char *type
, int format
, void *data
, int *cause
)
1219 struct phone_pvt
*p
;
1220 struct ast_channel
*tmp
= NULL
;
1223 /* Search for an unowned channel */
1224 if (ast_mutex_lock(&iflock
)) {
1225 ast_log(LOG_ERROR
, "Unable to lock interface list???\n");
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
])) {
1236 tmp
= phone_new(p
, AST_STATE_DOWN
, p
->context
);
1239 *cause
= AST_CAUSE_BUSY
;
1244 ast_mutex_unlock(&iflock
);
1248 format
&= (AST_FORMAT_G723_1
| AST_FORMAT_SLINEAR
| AST_FORMAT_ULAW
);
1250 ast_log(LOG_NOTICE
, "Asked to get a channel of unsupported format '%d'\n", oldformat
);
1257 /* parse gain value from config file */
1258 static int parse_gain_value(char *gain_type
, char *value
)
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
;
1274 if (value
[strlen(value
) - 1] == '%')
1275 return (int)(gain
/ (float)100);
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 */
1290 ast_softhangup(p
->owner
, AST_SOFTHANGUP_APPUNLOAD
);
1294 ast_mutex_unlock(&iflock
);
1296 ast_log(LOG_WARNING
, "Unable to lock the monitor\n");
1299 if (!ast_mutex_lock(&monlock
)) {
1300 if (monitor_thread
> AST_PTHREADT_NULL
) {
1302 while (pthread_kill(monitor_thread
, SIGURG
) == 0)
1304 pthread_join(monitor_thread
, NULL
);
1306 monitor_thread
= AST_PTHREADT_STOP
;
1307 ast_mutex_unlock(&monlock
);
1309 ast_log(LOG_WARNING
, "Unable to lock the monitor\n");
1313 if (!ast_mutex_lock(&iflock
)) {
1314 /* Destroy all the interfaces and free their memory */
1317 /* Close the socket, assuming it's real */
1322 /* Free associated memory */
1326 ast_mutex_unlock(&iflock
);
1328 ast_log(LOG_WARNING
, "Unable to lock the monitor\n");
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 */
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");
1359 v
= ast_variable_browse(cfg
, "interfaces");
1361 /* Create the interface list */
1362 if (!strcasecmp(v
->name
, "device")) {
1363 tmp
= mkif(v
->value
, mode
, txgain
, rxgain
);
1369 ast_log(LOG_ERROR
, "Unable to register channel '%s'\n", v
->value
);
1370 ast_config_destroy(cfg
);
1371 ast_mutex_unlock(&iflock
);
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))
1386 else if (!strncasecmp(v
->value
, "im", 2))
1387 mode
= MODE_IMMEDIATE
;
1388 else if (!strncasecmp(v
->value
, "fxs", 3)) {
1390 prefformat
= 0x01ff0000; /* All non-voice */
1392 else if (!strncasecmp(v
->value
, "fx", 2))
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
;
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
;
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
);
1427 ast_mutex_unlock(&iflock
);
1429 if (mode
== MODE_FXS
) {
1430 phone_tech_fxs
.capabilities
= prefformat
;
1431 cur_tech
= &phone_tech_fxs
;
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
);
1443 ast_config_destroy(cfg
);
1444 /* And start the monitor for the first time */
1449 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Linux Telephony API Support");