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 #ifdef HAVE_LINUX_COMPILER_H
49 #include <linux/compiler.h>
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"
71 #ifdef QTI_PHONEJACK_TJ_PCI /* check for the newer quicknet driver v.3.1.0 which has this symbol */
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);
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
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
, 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
= {
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
:
229 case AST_CONTROL_SRCUPDATE
:
233 ast_log(LOG_WARNING
, "Condition %d is not supported on channel %s\n", condition
, chan
->name
);
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
)
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 */
252 static int phone_digit_end(struct ast_channel
*ast
, char digit
, unsigned int duration
)
257 ast_log(LOG_DEBUG
, "Dialed %c\n", digit
);
269 outdigit
= digit
- '0';
279 ioctl(p
->fd
, IXJCTL_PSTN_SET_STATE
, PSTN_ON_HOOK
);
281 ioctl(p
->fd
, IXJCTL_PSTN_SET_STATE
, PSTN_OFF_HOOK
);
285 ast_log(LOG_WARNING
, "Unknown digit '%c'\n", digit
);
288 ast_log(LOG_DEBUG
, "Dialed %d\n", outdigit
);
289 ioctl(p
->fd
, PHONE_PLAY_TONE
, outdigit
);
294 static int phone_call(struct ast_channel
*ast
, char *dest
, int timeout
)
304 ast_localtime(&UtcTime
, &tm
, NULL
);
306 memset(&cid
, 0, sizeof(PHONE_CID
));
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
);
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
));
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
);
329 ast_log(LOG_DEBUG
, "Ringing %s on %s (%d)\n", dest
, ast
->name
, ast
->fds
[0]);
331 start
= IXJ_PHONE_RING_START(cid
);
335 if (p
->mode
== MODE_FXS
) {
336 char *digit
= strchr(dest
, '/');
341 phone_digit_end(ast
, *digit
++, 0);
345 ast_setstate(ast
, AST_STATE_RINGING
);
346 ast_queue_control(ast
, AST_CONTROL_RINGING
);
350 static int phone_hangup(struct ast_channel
*ast
)
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");
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
)) {
380 ast_log(LOG_DEBUG
, "Got hunghup, giving busy signal\n");
381 ioctl(p
->fd
, PHONE_BUSY
);
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
);
400 static int phone_setup(struct ast_channel
*ast
)
404 ioctl(p
->fd
, PHONE_CPT_STOP
);
405 /* Nothing to answering really, just start recording */
406 if (ast
->rawreadformat
== AST_FORMAT_G723_1
) {
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");
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");
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");
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",
445 ast_log(LOG_WARNING
, "Can't do format %s\n", ast_getformatname(ast
->rawreadformat
));
448 if (ioctl(p
->fd
, PHONE_REC_START
)) {
449 ast_log(LOG_WARNING
, "Failed to start recording\n");
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);
458 static int phone_answer(struct ast_channel
*ast
)
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
));
467 ast_log(LOG_DEBUG
, "Took linejack off hook\n");
471 ast_log(LOG_DEBUG
, "phone_answer(%s)\n", ast
->name
);
473 ast_setstate(ast
, AST_STATE_UP
);
478 static char phone_2digit(char c
)
484 else if ((c
< 10) && (c
>= 0))
491 static struct ast_frame
*phone_exception(struct ast_channel
*ast
)
494 union telephony_exception phonee
;
495 struct phone_pvt
*p
= ast
->tech_pvt
;
498 /* Some nice norms */
505 p
->fr
.delivery
= ast_tv(0,0);
507 phonee
.bytes
= ioctl(p
->fd
, PHONE_EXCEPTION
);
508 if (phonee
.bits
.dtmf_ready
) {
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
;
518 if (phonee
.bits
.hookstate
) {
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 */
524 ast_log(LOG_DEBUG
, "New hookstate: %d\n", res
);
525 if (!res
&& (p
->mode
!= MODE_FXO
))
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
;
533 ast_setstate(ast
, AST_STATE_UP
);
536 ast_log(LOG_WARNING
, "Got off hook in weird state %d\n", ast
->_state
);
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");
548 /* Strange -- nothing there.. */
549 p
->fr
.frametype
= AST_FRAME_NULL
;
554 static struct ast_frame
*phone_read(struct ast_channel
*ast
)
557 struct phone_pvt
*p
= ast
->tech_pvt
;
560 /* Some nice norms */
567 p
->fr
.delivery
= ast_tv(0,0);
569 /* Try to read some data... */
571 res
= read(p
->fd
, p
->buf
, PHONE_MAX_BUF
);
572 ast_clear_flag(ast
, AST_FLAG_BLOCKING
);
575 if (errno
== EAGAIN
) {
576 ast_log(LOG_WARNING
, "Null frame received\n");
577 p
->fr
.frametype
= AST_FRAME_NULL
;
582 ast_log(LOG_WARNING
, "Error reading: %s\n", strerror(errno
));
586 if (p
->mode
!= MODE_FXS
)
587 switch(p
->buf
[0] & 0x3) {
594 /* VAD/CNG, only send two words */
600 p
->fr
.frametype
= p
->lastinput
<= AST_FORMAT_MAX_AUDIO
?
602 p
->lastinput
<= AST_FORMAT_PNG
? AST_FRAME_IMAGE
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
);
612 static int phone_write_buf(struct phone_pvt
*p
, const char *buf
, int len
, int frlen
, int swap
)
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 */
621 ast_swapcopy_samples(p
->obuf
+p
->obuflen
, buf
, len
/2);
623 memcpy(p
->obuf
+ p
->obuflen
, buf
, len
);
625 while(p
->obuflen
> frlen
) {
626 res
= write(p
->fd
, p
->obuf
, frlen
);
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.
635 ast_log(LOG_WARNING
, "Only wrote %d of %d bytes\n", res
, frlen
);
639 /* Move memory if necessary */
641 memmove(p
->obuf
, p
->obuf
+ frlen
, p
->obuflen
);
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) ==
653 static int phone_write(struct ast_channel
*ast
, struct ast_frame
*frame
)
655 struct phone_pvt
*p
= ast
->tech_pvt
;
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
);
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
);
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
);
682 if (ast
->_state
!= AST_STATE_UP
) {
683 /* Don't try tos end audio on-hook */
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");
695 if (ioctl(p
->fd
, PHONE_REC_CODEC
, G723_63
)) {
696 ast_log(LOG_WARNING
, "Unable to set G723.1 mode\n");
699 p
->lastformat
= AST_FORMAT_G723_1
;
700 p
->lastinput
= AST_FORMAT_G723_1
;
701 /* Reset output buffer */
705 if (frame
->datalen
> 24) {
706 ast_log(LOG_WARNING
, "Frame size too large for G.723.1 (%d bytes)\n", frame
->datalen
);
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");
718 if (ioctl(p
->fd
, PHONE_REC_CODEC
, LINEAR16
)) {
719 ast_log(LOG_WARNING
, "Unable to set 16-bit linear mode\n");
722 p
->lastformat
= AST_FORMAT_SLINEAR
;
723 p
->lastinput
= AST_FORMAT_SLINEAR
;
725 /* Reset output buffer */
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");
737 if (ioctl(p
->fd
, PHONE_REC_CODEC
, ULAW
)) {
738 ast_log(LOG_WARNING
, "Unable to set uLaw mode\n");
741 p
->lastformat
= AST_FORMAT_ULAW
;
742 p
->lastinput
= AST_FORMAT_ULAW
;
744 /* Reset output buffer */
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",
757 if (ioctl(p
->fd
, PHONE_REC_CODEC
, frame
->subclass
)) {
758 ast_log(LOG_WARNING
, "Unable to set %d mode\n",
762 p
->lastformat
= frame
->subclass
;
763 p
->lastinput
= frame
->subclass
;
765 /* Reset output buffer */
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");
777 if (ioctl(p
->fd
, PHONE_REC_START
)) {
778 ast_log(LOG_WARNING
, "Failed to start recording\n");
782 /* If we get here, we have a frame of Appropriate data */
785 while(sofar
< frame
->datalen
) {
786 /* Write in no more than maxfr sized frames */
787 expected
= frame
->datalen
- sofar
;
788 if (maxfr
< expected
)
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);
797 res
= phone_write_buf(p
, tmpbuf
, expected
, maxfr
, 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 */
807 res
= phone_write_buf(p
, pos
, expected
, maxfr
, swap
);
809 if (res
!= expected
) {
810 if ((errno
!= EAGAIN
) && (errno
!= EINTR
)) {
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.
819 ast_log(LOG_WARNING
, "Only wrote %d of %d bytes\n", res
, frame
->datalen
);
822 } else /* Pretend it worked */
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);
837 tmp
->tech
= cur_tech
;
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
)
845 tmp
->rawwriteformat
=
850 tmp
->rawwriteformat
=
851 prefformat
& ~AST_FORMAT_SLINEAR
;
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
)
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
));
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
);
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
);
882 if (ast_pbx_start(tmp
)) {
883 ast_log(LOG_WARNING
, "Unable to start PBX on %s\n", tmp
->name
);
888 ast_log(LOG_WARNING
, "Unable to allocate channel structure\n");
892 static void phone_mini_packet(struct phone_pvt
*i
)
896 /* Ignore stuff we read... */
897 res
= read(i
->fd
, buf
, sizeof(buf
));
899 ast_log(LOG_WARNING
, "Read returned %d: %s\n", res
, strerror(errno
));
904 static void phone_check_exception(struct phone_pvt
*i
)
907 char digit
[2] = {0 , 0};
908 union telephony_exception phonee
;
909 /* XXX Do something XXX */
911 ast_log(LOG_DEBUG
, "Exception!\n");
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
);
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.
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 */
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
);
946 ast_verbose("Extension is %s\n", i
->ext
);
950 if (phonee
.bits
.hookstate
) {
951 offhook
= ioctl(i
->fd
, PHONE_HOOKSTATE
);
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 */
959 /* Play the dialtone */
961 ioctl(i
->fd
, PHONE_PLAY_STOP
);
962 ioctl(i
->fd
, PHONE_PLAY_CODEC
, ULAW
);
963 ioctl(i
->fd
, PHONE_PLAY_START
);
965 } else if (i
->mode
== MODE_SIGMA
) {
966 ast_module_ref(ast_module_info
->self
);
967 /* Reset the extension */
969 /* Play the dialtone */
971 ioctl(i
->fd
, PHONE_DIALTONE
);
975 ast_module_unref(ast_module_info
->self
);
976 memset(i
->ext
, 0, sizeof(i
->ext
));
979 ioctl(i
->fd
, PHONE_CPT_STOP
);
982 ioctl(i
->fd
, PHONE_PLAY_STOP
);
983 ioctl(i
->fd
, PHONE_REC_STOP
);
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
)
1002 struct phone_pvt
*i
;
1004 /* The tone we're playing this round */
1005 struct timeval tv
= {0,0};
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 */
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");
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 */
1025 if (FD_ISSET(i
->fd
, &rfds
))
1026 ast_log(LOG_WARNING
, "Descriptor %d appears twice (%s)?\n", i
->fd
, i
->dev
);
1028 /* This needs to be watched, as it lacks an owner */
1029 FD_SET(i
->fd
, &rfds
);
1030 FD_SET(i
->fd
, &efds
);
1033 if (i
->dialtone
&& i
->mode
!= MODE_SIGMA
) {
1034 /* Remember we're going to have to come back and play
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");
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 */
1054 if (tonepos
>= sizeof(DialTone
))
1056 if (ast_tvzero(tv
)) {
1057 tv
= ast_tv(30000, 0);
1059 res
= ast_select(n
+ 1, &rfds
, NULL
, &efds
, &tv
);
1061 res
= ast_select(n
+ 1, &rfds
, NULL
, &efds
, NULL
);
1065 /* Okay, select has finished. Let's see what happened. */
1067 ast_log(LOG_DEBUG
, "select return %d: %s\n", res
, strerror(errno
));
1070 /* If there are no fd's changed, just continue, it's probably time
1071 to play some more dialtones */
1074 /* Alright, lock the interface list again, and let's look and see what has
1076 if (ast_mutex_lock(&iflock
)) {
1077 ast_log(LOG_WARNING
, "Unable to lock the interface list\n");
1082 for(; i
; i
=i
->next
) {
1083 if (FD_ISSET(i
->fd
, &rfds
)) {
1087 phone_mini_packet(i
);
1089 if (FD_ISSET(i
->fd
, &efds
)) {
1093 phone_check_exception(i
);
1096 ast_mutex_unlock(&iflock
);
1102 static int restart_monitor()
1104 /* If we're supposed to be stopped -- stay stopped */
1105 if (monitor_thread
== AST_PTHREADT_STOP
)
1107 if (ast_mutex_lock(&monlock
)) {
1108 ast_log(LOG_WARNING
, "Unable to lock monitor\n");
1111 if (monitor_thread
== pthread_self()) {
1112 ast_mutex_unlock(&monlock
);
1113 ast_log(LOG_WARNING
, "Cannot kill myself\n");
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");
1123 while (pthread_kill(monitor_thread
, SIGURG
) == 0)
1125 pthread_join(monitor_thread
, NULL
);
1126 ast_mutex_unlock(&iflock
);
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");
1135 ast_mutex_unlock(&monlock
);
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
;
1145 tmp
= malloc(sizeof(struct phone_pvt
));
1147 tmp
->fd
= open(iface
, O_RDWR
);
1149 ast_log(LOG_WARNING
, "Unable to open '%s'\n", iface
);
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");
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;
1172 ioctl(tmp
->fd
, PHONE_VAD
, tmp
->silencesupression
);
1175 flags
= fcntl(tmp
->fd
, F_GETFL
);
1176 fcntl(tmp
->fd
, F_SETFL
, flags
| O_NONBLOCK
);
1178 tmp
->lastformat
= -1;
1179 tmp
->lastinput
= -1;
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
));
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
);
1199 static struct ast_channel
*phone_request(const char *type
, int format
, void *data
, int *cause
)
1202 struct phone_pvt
*p
;
1203 struct ast_channel
*tmp
= NULL
;
1206 /* Search for an unowned channel */
1207 if (ast_mutex_lock(&iflock
)) {
1208 ast_log(LOG_ERROR
, "Unable to lock interface list???\n");
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
])) {
1219 tmp
= phone_new(p
, AST_STATE_DOWN
, p
->context
);
1222 *cause
= AST_CAUSE_BUSY
;
1227 ast_mutex_unlock(&iflock
);
1231 format
&= (AST_FORMAT_G723_1
| AST_FORMAT_SLINEAR
| AST_FORMAT_ULAW
);
1233 ast_log(LOG_NOTICE
, "Asked to get a channel of unsupported format '%d'\n", oldformat
);
1240 /* parse gain value from config file */
1241 static int parse_gain_value(char *gain_type
, char *value
)
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
;
1257 if (value
[strlen(value
) - 1] == '%')
1258 return (int)(gain
/ (float)100);
1263 static int __unload_module(void)
1265 struct phone_pvt
*p
, *pl
;
1266 /* First, take us out of the channel loop */
1268 ast_channel_unregister(cur_tech
);
1269 if (!ast_mutex_lock(&iflock
)) {
1270 /* Hangup all interfaces if they have an owner */
1274 ast_softhangup(p
->owner
, AST_SOFTHANGUP_APPUNLOAD
);
1278 ast_mutex_unlock(&iflock
);
1280 ast_log(LOG_WARNING
, "Unable to lock the monitor\n");
1283 if (!ast_mutex_lock(&monlock
)) {
1284 if (monitor_thread
> AST_PTHREADT_NULL
) {
1286 while (pthread_kill(monitor_thread
, SIGURG
) == 0)
1288 pthread_join(monitor_thread
, NULL
);
1290 monitor_thread
= AST_PTHREADT_STOP
;
1291 ast_mutex_unlock(&monlock
);
1293 ast_log(LOG_WARNING
, "Unable to lock the monitor\n");
1297 if (!ast_mutex_lock(&iflock
)) {
1298 /* Destroy all the interfaces and free their memory */
1301 /* Close the socket, assuming it's real */
1306 /* Free associated memory */
1310 ast_mutex_unlock(&iflock
);
1312 ast_log(LOG_WARNING
, "Unable to lock the monitor\n");
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 */
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");
1345 /* Create the interface list */
1346 if (!strcasecmp(v
->name
, "device")) {
1347 tmp
= mkif(v
->value
, mode
, txgain
, rxgain
);
1353 ast_log(LOG_ERROR
, "Unable to register channel '%s'\n", v
->value
);
1354 ast_config_destroy(cfg
);
1355 ast_mutex_unlock(&iflock
);
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))
1370 else if (!strncasecmp(v
->value
, "im", 2))
1371 mode
= MODE_IMMEDIATE
;
1372 else if (!strncasecmp(v
->value
, "fxs", 3)) {
1374 prefformat
= 0x01ff0000; /* All non-voice */
1376 else if (!strncasecmp(v
->value
, "fx", 2))
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
;
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
;
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
);
1411 ast_mutex_unlock(&iflock
);
1413 if (mode
== MODE_FXS
) {
1414 phone_tech_fxs
.capabilities
= prefformat
;
1415 cur_tech
= &phone_tech_fxs
;
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
);
1425 return AST_MODULE_LOAD_FAILURE
;
1427 ast_config_destroy(cfg
);
1428 /* And start the monitor for the first time */
1430 return AST_MODULE_LOAD_SUCCESS
;
1433 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Linux Telephony API Support");