2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2012, Luka Perkov
5 * Copyright (C) 2012, John Crispin
6 * Copyright (C) 2012, Andrej Vlašić
7 * Copyright (C) 2012, Kaspar Schleiser for T-Labs
8 * (Deutsche Telekom Innovation Laboratories)
9 * Copyright (C) 2012, Mirko Vogt for T-Labs
10 * (Deutsche Telekom Innovation Laboratories)
11 * Copyright (c) 2015, Antonio Eugenio Burriel
12 * Copyright (C) 2017, Stefan Koch
14 * Luka Perkov <openwrt@lukaperkov.net>
15 * John Crispin <blogic@openwrt.org>
16 * Andrej Vlašić <andrej.vlasic0@gmail.com>
17 * Kaspar Schleiser <kaspar@schleiser.de>
18 * Mirko Vogt <mirko@openwrt.org>
19 * Antonio Eugenio Burriel <aeburriel@gmail.com>
20 * Stefan Koch <stefan.koch10@gmail.com>
22 * See http://www.asterisk.org for more information about
23 * the Asterisk project. Please do not directly contact
24 * any of the maintainers of this project for assistance;
25 * the project provides a web site, mailing lists and IRC
26 * channels for your use.
28 * This program is free software, distributed under the terms of
29 * the GNU General Public License Version 2. See the LICENSE file
30 * at the top of the source tree.
35 * \brief Asterisk channel line driver for Lantiq based TAPI boards
37 * \author Luka Perkov <openwrt@lukaperkov.net>
38 * \author John Crispin <blogic@openwrt.org>
39 * \author Andrej Vlašić <andrej.vlasic0@gmail.com>
40 * \author Kaspar Schleiser <kaspar@schleiser.de>
41 * \author Mirko Vogt <mirko@openwrt.org>
42 * \author Antonio Eugenio Burriel <aeburriel@gmail.com>
43 * \author Stefan Koch <stefan.koch10@gmail.com>
45 * \ingroup channel_drivers
50 ASTERISK_FILE_VERSION(__FILE__
, "$Revision: xxx $")
53 #include <sys/socket.h>
55 #include <arpa/inet.h>
57 #include <sys/ioctl.h>
61 #ifdef HAVE_LINUX_COMPILER_H
62 #include <linux/compiler.h>
64 #include <linux/telephony.h>
66 #include <asterisk/lock.h>
67 #include <asterisk/channel.h>
68 #include <asterisk/config.h>
69 #include <asterisk/module.h>
70 #include <asterisk/pbx.h>
71 #include <asterisk/utils.h>
72 #include <asterisk/callerid.h>
73 #include <asterisk/causes.h>
74 #include <asterisk/indications.h>
75 #include <asterisk/stringfields.h>
76 #include <asterisk/musiconhold.h>
77 #include <asterisk/sched.h>
78 #include <asterisk/cli.h>
79 #include <asterisk/devicestate.h>
80 #include <asterisk/format_cache.h>
82 /* Lantiq TAPI includes */
83 #include <drv_tapi/drv_tapi_io.h>
84 #include <drv_vmmc/vmmc_io.h>
86 #define TAPI_AUDIO_PORT_NUM_MAX 2
88 /* Tapi predefined tones 0 to 31 */
89 #define TAPI_TONE_LOCALE_NONE 0
90 //#define TAPI_TONE_LOCALE_DIAL_CODE 25
91 //#define TAPI_TONE_LOCALE_RINGING_CODE 26
92 //#define TAPI_TONE_LOCALE_BUSY_CODE 27
93 //#define TAPI_TONE_LOCALE_CONGESTION_CODE 27
95 /* Tapi custom tones 32 to 256 */
96 #define TAPI_TONE_LOCALE_DIAL_CODE 32
97 #define TAPI_TONE_LOCALE_RINGING_CODE 33
98 #define TAPI_TONE_LOCALE_BUSY_CODE 34
99 #define TAPI_TONE_LOCALE_CONGESTION_CODE 35
101 #define LANTIQ_CONTEXT_PREFIX "lantiq"
102 #define DEFAULT_INTERDIGIT_TIMEOUT 4000
103 #define G723_HIGH_RATE 1
104 #define LED_NAME_LENGTH 32
106 static const char config
[] = "lantiq.conf";
108 static char firmware_filename
[PATH_MAX
] = "/lib/firmware/ifx_firmware.bin";
109 static char bbd_filename
[PATH_MAX
] = "/lib/firmware/ifx_bbd_fxs.bin";
110 static char base_path
[PATH_MAX
] = "/dev/vmmc";
111 static int per_channel_context
= 0;
113 /* tone generator types */
114 enum tone_generator_t
{
115 TONE_INTEGRATED
, /* tapi tone generator */
116 TONE_ASTERISK
, /* asterisk tone generator where possible */
117 TONE_MEDIA
/* media tone where possible */
120 /* tone generator (default is integraded) */
121 static enum tone_generator_t tone_generator
= TONE_INTEGRATED
;
123 /* tone zones for dial, ring, busy and congestion */
124 struct ast_tone_zone_sound
*ts_dial
;
125 struct ast_tone_zone_sound
*ts_ring
;
126 struct ast_tone_zone_sound
*ts_busy
;
127 struct ast_tone_zone_sound
*ts_congestion
;
130 * The private structures of the Phone Jack channels are linked for selecting
143 static struct lantiq_pvt
{
144 struct ast_channel
*owner
; /* Channel we belong to, possibly NULL */
145 int port_id
; /* Port number of this object, 0..n */
147 char context
[AST_MAX_CONTEXT
]; /* this port's dialplan context */
148 int dial_timer
; /* timer handle for autodial timeout */
149 char dtmfbuf
[AST_MAX_EXTENSION
]; /* buffer holding dialed digits */
150 int dtmfbuf_len
; /* lenght of dtmfbuf */
151 int rtp_timestamp
; /* timestamp for RTP packets */
152 int ptime
; /* Codec base ptime */
153 uint16_t rtp_seqno
; /* Sequence nr for RTP packets */
154 uint32_t call_setup_start
; /* Start of dialling in ms */
155 uint32_t call_setup_delay
; /* time between ^ and 1st ring in ms */
156 uint32_t call_start
; /* time we started dialling / answered */
157 uint32_t call_answer
; /* time the callee answered our call */
158 uint16_t jb_size
; /* Jitter buffer size */
159 uint32_t jb_underflow
; /* Jitter buffer injected samples */
160 uint32_t jb_overflow
; /* Jitter buffer dropped samples */
161 uint16_t jb_delay
; /* Jitter buffer: playout delay */
162 uint16_t jb_invalid
; /* Jitter buffer: Nr. of invalid packets */
165 static struct lantiq_ctx
{
168 int ch_fd
[TAPI_AUDIO_PORT_NUM_MAX
];
169 char voip_led
[LED_NAME_LENGTH
]; /* VOIP LED name */
170 char ch_led
[TAPI_AUDIO_PORT_NUM_MAX
][LED_NAME_LENGTH
]; /* FXS LED names */
171 int interdigit_timeout
; /* Timeout in ms between dialed digits */
174 static int ast_digit_begin(struct ast_channel
*ast
, char digit
);
175 static int ast_digit_end(struct ast_channel
*ast
, char digit
, unsigned int duration
);
176 static int ast_lantiq_call(struct ast_channel
*ast
, const char *dest
, int timeout
);
177 static int ast_lantiq_hangup(struct ast_channel
*ast
);
178 static int ast_lantiq_answer(struct ast_channel
*ast
);
179 static struct ast_frame
*ast_lantiq_read(struct ast_channel
*ast
);
180 static int ast_lantiq_write(struct ast_channel
*ast
, struct ast_frame
*frame
);
181 static struct ast_frame
*ast_lantiq_exception(struct ast_channel
*ast
);
182 static int ast_lantiq_indicate(struct ast_channel
*chan
, int condition
, const void *data
, size_t datalen
);
183 static int ast_lantiq_fixup(struct ast_channel
*old
, struct ast_channel
*new);
184 static struct ast_channel
*ast_lantiq_requester(const char *type
, struct ast_format_cap
*cap
, const struct ast_assigned_ids
*assigned_ids
, const struct ast_channel
*requestor
, const char *data
, int *cause
);
185 static int ast_lantiq_devicestate(const char *data
);
186 static int acf_channel_read(struct ast_channel
*chan
, const char *funcname
, char *args
, char *buf
, size_t buflen
);
187 static void lantiq_jb_get_stats(int c
);
188 static struct ast_format
*lantiq_map_rtptype_to_format(uint8_t rtptype
);
189 static uint8_t lantiq_map_format_to_rtptype(const struct ast_format
*format
);
190 static int lantiq_conf_enc(int c
, const struct ast_format
*format
);
191 static void lantiq_reset_dtmfbuf(struct lantiq_pvt
*pvt
);
193 static struct ast_channel_tech lantiq_tech
= {
195 .description
= "Lantiq TAPI Telephony API Driver",
196 .send_digit_begin
= ast_digit_begin
,
197 .send_digit_end
= ast_digit_end
,
198 .call
= ast_lantiq_call
,
199 .hangup
= ast_lantiq_hangup
,
200 .answer
= ast_lantiq_answer
,
201 .read
= ast_lantiq_read
,
202 .write
= ast_lantiq_write
,
203 .exception
= ast_lantiq_exception
,
204 .indicate
= ast_lantiq_indicate
,
205 .fixup
= ast_lantiq_fixup
,
206 .requester
= ast_lantiq_requester
,
207 .devicestate
= ast_lantiq_devicestate
,
208 .func_channel_read
= acf_channel_read
211 /* Protect the interface list (of lantiq_pvt's) */
212 AST_MUTEX_DEFINE_STATIC(iflock
);
215 * Protect the monitoring thread, so only one process can kill or start it, and
216 * not when it's doing something critical.
218 AST_MUTEX_DEFINE_STATIC(monlock
);
220 /* The scheduling context */
221 struct ast_sched_context
*sched
;
224 * This is the thread for the monitor which checks for input on the channels
225 * which are not currently in use.
227 static pthread_t monitor_thread
= AST_PTHREADT_NULL
;
230 #define WORDS_BIGENDIAN
231 /* struct taken from some GPLed code by Mike Borella */
232 typedef struct rtp_header
234 #if defined(WORDS_BIGENDIAN)
235 uint8_t version
:2, padding
:1, extension
:1, csrc_count
:4;
236 uint8_t marker
:1, payload_type
:7;
238 uint8_t csrc_count
:4, extension
:1, padding
:1, version
:2;
239 uint8_t payload_type
:7, marker
:1;
245 #define RTP_HEADER_LEN 12
246 #define RTP_BUFFER_LEN 512
247 /* Internal RTP payload types - standard */
249 #define RTP_G723_63 4
254 /* Internal RTP payload types - custom */
255 #define RTP_G7221 100
258 #define RTP_SLIN8 103
259 #define RTP_SLIN16 104
260 #define RTP_SIREN7 105
261 #define RTP_G723_53 106
264 /* LED Control. Taken with modifications from SVD by Luca Olivetti <olivluca@gmail.com> */
265 #define LED_SLOW_BLINK 1000
266 #define LED_FAST_BLINK 100
267 static FILE *led_open(const char *led
, char* sub
)
271 if (snprintf(fname
, sizeof(fname
), "/sys/class/leds/%s/%s", led
, sub
) >= sizeof(fname
))
273 return fopen(fname
, "r+");
276 static FILE *led_trigger(const char *led
)
278 return led_open(led
, "trigger");
281 static void led_delay(const char *led
, int onoff
, int msec
)
283 FILE *fp
= led_open(led
, onoff
? "delay_on" : "delay_off");
285 fprintf(fp
,"%d\n",msec
);
290 static void led_on(const char *led
)
294 fp
= led_trigger(led
);
296 fprintf(fp
,"default-on\n");
301 static void led_off(const char *led
)
305 fp
= led_trigger(led
);
307 fprintf(fp
,"none\n");
312 static void led_blink(const char *led
, int period
)
316 fp
= led_trigger(led
);
318 fprintf(fp
, "timer\n");
320 led_delay(led
, 1, period
/2);
321 led_delay(led
, 0, period
/2);
325 static uint32_t now(void) {
327 clock_gettime(CLOCK_MONOTONIC
, &ts
);
329 uint64_t tmp
= ts
.tv_sec
*1000 + (ts
.tv_nsec
/1000000);
330 return (uint32_t) tmp
;
333 static uint32_t epoch(void) {
335 gettimeofday(&tv
, NULL
);
340 static int lantiq_dev_open(const char *dev_path
, const int32_t ch_num
)
342 char dev_name
[PATH_MAX
];
343 memset(dev_name
, 0, sizeof(dev_name
));
344 snprintf(dev_name
, PATH_MAX
, "%s%u%u", dev_path
, 1, ch_num
);
345 return open((const char*)dev_name
, O_RDWR
, 0644);
348 static void lantiq_ring(int c
, int r
, const char *cid
, const char *name
)
353 led_blink(dev_ctx
.ch_led
[c
], LED_FAST_BLINK
);
355 status
= (uint8_t) ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_RING_START
, 0);
357 IFX_TAPI_CID_MSG_t msg
;
358 IFX_TAPI_CID_MSG_ELEMENT_t elements
[3];
363 elements
[count
].string
.elementType
= IFX_TAPI_CID_ST_CLI
;
364 elements
[count
].string
.len
= strlen(cid
);
365 if (elements
[count
].string
.len
> IFX_TAPI_CID_MSG_LEN_MAX
) {
366 elements
[count
].string
.len
= IFX_TAPI_CID_MSG_LEN_MAX
;
368 strncpy((char *)elements
[count
].string
.element
, cid
, IFX_TAPI_CID_MSG_LEN_MAX
);
369 elements
[count
].string
.element
[IFX_TAPI_CID_MSG_LEN_MAX
-1] = '\0';
373 elements
[count
].string
.elementType
= IFX_TAPI_CID_ST_NAME
;
374 elements
[count
].string
.len
= strlen(name
);
375 if (elements
[count
].string
.len
> IFX_TAPI_CID_MSG_LEN_MAX
) {
376 elements
[count
].string
.len
= IFX_TAPI_CID_MSG_LEN_MAX
;
378 strncpy((char *)elements
[count
].string
.element
, name
, IFX_TAPI_CID_MSG_LEN_MAX
);
379 elements
[count
].string
.element
[IFX_TAPI_CID_MSG_LEN_MAX
-1] = '\0';
383 if ((time(×tamp
) != -1) && ((tm
=localtime(×tamp
)) != NULL
)) {
384 elements
[count
].date
.elementType
= IFX_TAPI_CID_ST_DATE
;
385 elements
[count
].date
.day
= tm
->tm_mday
;
386 elements
[count
].date
.month
= tm
->tm_mon
+ 1;
387 elements
[count
].date
.hour
= tm
->tm_hour
;
388 elements
[count
].date
.mn
= tm
->tm_min
;
392 msg
.txMode
= IFX_TAPI_CID_HM_ONHOOK
;
393 msg
.messageType
= IFX_TAPI_CID_MT_CSUP
;
394 msg
.message
= elements
;
395 msg
.nMsgElements
= count
;
397 status
= (uint8_t) ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_CID_TX_SEQ_START
, (IFX_int32_t
) &msg
);
400 status
= (uint8_t) ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_RING_STOP
, 0);
401 led_off(dev_ctx
.ch_led
[c
]);
405 ast_log(LOG_ERROR
, "%s ioctl failed\n",
406 (r
? "IFX_TAPI_RING_START" : "IFX_TAPI_RING_STOP"));
410 /* add a frequency to TAPE tone structure */
411 /* returns the TAPI frequency ID */
412 static int tapitone_add_freq (IFX_TAPI_TONE_t
*tone
, IFX_uint32_t freq
) {
413 const int n
=4; /* TAPI tone structure supports up to 4 frequencies */
418 /* pointer array for freq's A, B, C, D */
419 IFX_uint32_t
*freqarr
[] = { &(tone
->simple
.freqA
), &(tone
->simple
.freqB
), &(tone
->simple
.freqC
), &(tone
->simple
.freqD
) };
421 /* pointer array for level's A, B, C, D */
422 IFX_int32_t
*lvlarr
[] = { &(tone
->simple
.levelA
), &(tone
->simple
.levelB
), &(tone
->simple
.levelC
), &(tone
->simple
.levelD
) };
424 /* array for freq IDs */
425 IFX_uint32_t retarr
[] = { IFX_TAPI_TONE_FREQA
, IFX_TAPI_TONE_FREQB
, IFX_TAPI_TONE_FREQC
, IFX_TAPI_TONE_FREQD
};
427 /* determine if freq already set */
428 for (i
= 0; i
< n
; i
++) {
429 if(*freqarr
[i
] == freq
) /* freq found */
431 else if (i
== n
-1) /* last iteration */
432 error
=1; /* not found */
435 /* write frequency if not already set */
437 error
=0; /* reset error flag */
438 /* since freq is not set, write it into first free place */
439 for (i
= 0; i
< n
; i
++) {
440 if(!*freqarr
[i
]) { /* free place */
441 *freqarr
[i
] = freq
; /* set freq */
442 *lvlarr
[i
] = -150; /* set volume level */
444 } else if (i
== n
-1) /* last iteration */
445 error
=1; /* no free place becaus maximum count of freq's is set */
449 /* set freq ID return value */
451 ret
= IFX_TAPI_TONE_FREQNONE
;
455 return ret
; /* freq ID */
458 /* convert asterisk playlist string to tapi tone structure */
459 /* based on ast_playtones_start() from indications.c of asterisk 13 */
460 static void playlist_to_tapitone (const char *playlst
, IFX_uint32_t index
, IFX_TAPI_TONE_t
*tone
)
462 char *s
, *data
= ast_strdupa(playlst
);
467 /* initialize tapi tone structure */
468 memset(tone
, 0, sizeof(IFX_TAPI_TONE_t
));
469 tone
->simple
.format
= IFX_TAPI_TONE_TYPE_SIMPLE
;
470 tone
->simple
.index
= index
;
474 /* check if the data is separated with '|' or with ',' by default */
475 if (strchr(stringp
,'|')) {
481 for ( i
= 0; (s
= strsep(&stringp
, separator
)) && !ast_strlen_zero(s
) && i
< IFX_TAPI_TONE_STEPS_MAX
; i
++) {
482 struct ast_tone_zone_part tone_data
= {
491 if (ast_tone_zone_part_parse(s
, &tone_data
)) {
492 ast_log(LOG_ERROR
, "Failed to parse tone part '%s'\n", s
);
496 /* first tone must hava a cadence */
497 if (i
==0 && !tone_data
.time
)
498 tone
->simple
.cadence
[i
] = 1000;
500 tone
->simple
.cadence
[i
] = tone_data
.time
;
502 /* check for modulation */
503 if (tone_data
.modulate
) {
504 tone
->simple
.modulation
[i
] = IFX_TAPI_TONE_MODULATION_ON
;
505 tone
->simple
.modulation_factor
= IFX_TAPI_TONE_MODULATION_FACTOR_90
;
508 /* copy freq's to tapi tone structure */
509 /* a freq will implicitly skipped if it is zero */
510 tone
->simple
.frequencies
[i
] |= tapitone_add_freq(tone
, tone_data
.freq1
);
511 tone
->simple
.frequencies
[i
] |= tapitone_add_freq(tone
, tone_data
.freq2
);
515 static int lantiq_play_tone(int c
, int t
)
517 /* stop currently playing tone before starting new one */
518 ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_TONE_LOCAL_PLAY
, TAPI_TONE_LOCALE_NONE
);
520 /* do not handle stop tone twice */
521 if (t
!= TAPI_TONE_LOCALE_NONE
) {
523 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_TONE_LOCAL_PLAY
, t
)) {
524 ast_log(LOG_ERROR
, "IFX_TAPI_TONE_LOCAL_PLAY ioctl failed\n");
532 static enum channel_state
lantiq_get_hookstatus(int port
)
536 if (ioctl(dev_ctx
.ch_fd
[port
], IFX_TAPI_LINE_HOOK_STATUS_GET
, &status
)) {
537 ast_log(LOG_ERROR
, "IFX_TAPI_LINE_HOOK_STATUS_GET ioctl failed\n");
549 lantiq_dev_binary_buffer_create(const char *path
, uint8_t **ppBuf
, uint32_t *pBufSz
)
552 struct stat file_stat
;
555 fd
= fopen(path
, "rb");
557 ast_log(LOG_ERROR
, "binary file %s open failed\n", path
);
561 if (stat(path
, &file_stat
)) {
562 ast_log(LOG_ERROR
, "file %s statistics get failed\n", path
);
566 *ppBuf
= malloc(file_stat
.st_size
);
567 if (*ppBuf
== NULL
) {
568 ast_log(LOG_ERROR
, "binary file %s memory allocation failed\n", path
);
572 if (fread (*ppBuf
, sizeof(uint8_t), file_stat
.st_size
, fd
) != file_stat
.st_size
) {
573 ast_log(LOG_ERROR
, "file %s read failed\n", path
);
578 *pBufSz
= file_stat
.st_size
;
585 if (*ppBuf
!= NULL
&& status
)
591 static int32_t lantiq_dev_firmware_download(int32_t fd
, const char *path
)
593 uint8_t *firmware
= NULL
;
595 VMMC_IO_INIT vmmc_io_init
;
597 ast_log(LOG_DEBUG
, "loading firmware: \"%s\".\n", path
);
599 if (lantiq_dev_binary_buffer_create(path
, &firmware
, &size
))
602 memset(&vmmc_io_init
, 0, sizeof(VMMC_IO_INIT
));
603 vmmc_io_init
.pPRAMfw
= firmware
;
604 vmmc_io_init
.pram_size
= size
;
606 if (ioctl(fd
, FIO_FW_DOWNLOAD
, &vmmc_io_init
)) {
607 ast_log(LOG_ERROR
, "FIO_FW_DOWNLOAD ioctl failed\n");
611 if (firmware
!= NULL
)
617 static const char *state_string(enum channel_state s
)
620 case ONHOOK
: return "ONHOOK";
621 case OFFHOOK
: return "OFFHOOK";
622 case DIALING
: return "DIALING";
623 case INCALL
: return "INCALL";
624 case CALL_ENDED
: return "CALL_ENDED";
625 case RINGING
: return "RINGING";
626 default: return "UNKNOWN";
630 static const char *control_string(int c
)
633 case AST_CONTROL_HANGUP
: return "Other end has hungup";
634 case AST_CONTROL_RING
: return "Local ring";
635 case AST_CONTROL_RINGING
: return "Remote end is ringing";
636 case AST_CONTROL_ANSWER
: return "Remote end has answered";
637 case AST_CONTROL_BUSY
: return "Remote end is busy";
638 case AST_CONTROL_TAKEOFFHOOK
: return "Make it go off hook";
639 case AST_CONTROL_OFFHOOK
: return "Line is off hook";
640 case AST_CONTROL_CONGESTION
: return "Congestion (circuits busy)";
641 case AST_CONTROL_FLASH
: return "Flash hook";
642 case AST_CONTROL_WINK
: return "Wink";
643 case AST_CONTROL_OPTION
: return "Set a low-level option";
644 case AST_CONTROL_RADIO_KEY
: return "Key Radio";
645 case AST_CONTROL_RADIO_UNKEY
: return "Un-Key Radio";
646 case AST_CONTROL_PROGRESS
: return "Remote end is making Progress";
647 case AST_CONTROL_PROCEEDING
: return "Remote end is proceeding";
648 case AST_CONTROL_HOLD
: return "Hold";
649 case AST_CONTROL_UNHOLD
: return "Unhold";
650 case AST_CONTROL_SRCUPDATE
: return "Media Source Update";
651 case AST_CONTROL_CONNECTED_LINE
: return "Connected Line";
652 case AST_CONTROL_REDIRECTING
: return "Redirecting";
653 case AST_CONTROL_INCOMPLETE
: return "Incomplete";
654 case -1: return "Stop tone";
655 default: return "Unknown";
659 static int ast_lantiq_indicate(struct ast_channel
*chan
, int condition
, const void *data
, size_t datalen
)
661 struct lantiq_pvt
*pvt
= ast_channel_tech_pvt(chan
);
663 ast_verb(3, "phone indication \"%s\"\n", control_string(condition
));
668 lantiq_play_tone(pvt
->port_id
, TAPI_TONE_LOCALE_NONE
);
671 case AST_CONTROL_CONGESTION
:
673 if (tone_generator
== TONE_INTEGRATED
)
674 lantiq_play_tone(pvt
->port_id
, TAPI_TONE_LOCALE_CONGESTION_CODE
);
676 ast_playtones_start(chan
, 0, ts_congestion
->data
, 1);
680 case AST_CONTROL_BUSY
:
682 if (tone_generator
== TONE_INTEGRATED
)
683 lantiq_play_tone(pvt
->port_id
, TAPI_TONE_LOCALE_BUSY_CODE
);
685 ast_playtones_start(chan
, 0, ts_busy
->data
, 1);
689 case AST_CONTROL_RINGING
:
690 case AST_CONTROL_PROGRESS
:
692 pvt
->call_setup_delay
= now() - pvt
->call_setup_start
;
694 if (tone_generator
== TONE_INTEGRATED
)
695 lantiq_play_tone(pvt
->port_id
, TAPI_TONE_LOCALE_RINGING_CODE
);
696 else if (tone_generator
== TONE_ASTERISK
) /* do nothing if TONE_MEDIA is set */
697 ast_playtones_start(chan
, 0, ts_ring
->data
, 1);
703 /* -1 lets asterisk generate the tone */
709 static int ast_lantiq_fixup(struct ast_channel
*old
, struct ast_channel
*new)
711 ast_log(LOG_DEBUG
, "entering... no code here...\n");
715 static int ast_digit_begin(struct ast_channel
*chan
, char digit
)
717 /* TODO: Modify this callback to let Asterisk support controlling the length of DTMF */
718 ast_log(LOG_DEBUG
, "entering... no code here...\n");
722 static int ast_digit_end(struct ast_channel
*ast
, char digit
, unsigned int duration
)
724 ast_log(LOG_DEBUG
, "entering... no code here...\n");
728 static int ast_lantiq_call(struct ast_channel
*ast
, const char *dest
, int timeout
)
731 struct lantiq_pvt
*pvt
;
733 /* lock to prevent simultaneous access with do_monitor thread processing */
734 ast_mutex_lock(&iflock
);
736 pvt
= ast_channel_tech_pvt(ast
);
737 ast_log(LOG_DEBUG
, "state: %s\n", state_string(pvt
->channel_state
));
739 if (pvt
->channel_state
== ONHOOK
) {
740 struct ast_party_id connected_id
= ast_channel_connected_effective_id(ast
);
741 const char *cid
= connected_id
.number
.valid
? connected_id
.number
.str
: NULL
;
742 const char *name
= connected_id
.name
.valid
? connected_id
.name
.str
: NULL
;
744 ast_log(LOG_DEBUG
, "port %i is ringing\n", pvt
->port_id
);
745 ast_log(LOG_DEBUG
, "port %i CID: %s\n", pvt
->port_id
, cid
? cid
: "none");
746 ast_log(LOG_DEBUG
, "port %i NAME: %s\n", pvt
->port_id
, name
? name
: "none");
748 lantiq_ring(pvt
->port_id
, 1, cid
, name
);
749 pvt
->channel_state
= RINGING
;
751 ast_setstate(ast
, AST_STATE_RINGING
);
752 ast_queue_control(ast
, AST_CONTROL_RINGING
);
754 ast_log(LOG_DEBUG
, "port %i is busy\n", pvt
->port_id
);
755 ast_setstate(ast
, AST_STATE_BUSY
);
756 ast_queue_control(ast
, AST_CONTROL_BUSY
);
760 ast_mutex_unlock(&iflock
);
765 static int ast_lantiq_hangup(struct ast_channel
*ast
)
767 struct lantiq_pvt
*pvt
;
769 /* lock to prevent simultaneous access with do_monitor thread processing */
770 ast_mutex_lock(&iflock
);
772 pvt
= ast_channel_tech_pvt(ast
);
773 ast_log(LOG_DEBUG
, "state: %s\n", state_string(pvt
->channel_state
));
775 if (ast_channel_state(ast
) == AST_STATE_RINGING
) {
776 ast_debug(1, "channel state is RINGING\n");
779 switch (pvt
->channel_state
) {
782 lantiq_ring(pvt
->port_id
, 0, NULL
, NULL
);
783 pvt
->channel_state
= ONHOOK
;
786 ast_log(LOG_DEBUG
, "we were hung up, play busy tone\n");
787 pvt
->channel_state
= CALL_ENDED
;
788 lantiq_play_tone(pvt
->port_id
, TAPI_TONE_LOCALE_BUSY_CODE
);
791 lantiq_jb_get_stats(pvt
->port_id
);
793 ast_setstate(ast
, AST_STATE_DOWN
);
794 ast_module_unref(ast_module_info
->self
);
795 ast_channel_tech_pvt_set(ast
, NULL
);
798 ast_mutex_unlock(&iflock
);
803 static int ast_lantiq_answer(struct ast_channel
*ast
)
805 struct lantiq_pvt
*pvt
= ast_channel_tech_pvt(ast
);
807 ast_log(LOG_DEBUG
, "Remote end has answered call.\n");
809 if (lantiq_conf_enc(pvt
->port_id
, ast_channel_writeformat(ast
)))
812 pvt
->call_answer
= epoch();
817 static struct ast_frame
* ast_lantiq_read(struct ast_channel
*ast
)
819 ast_log(LOG_DEBUG
, "entering... no code here...\n");
823 /* create asterisk format from rtp payload type */
824 static struct ast_format
*lantiq_map_rtptype_to_format(uint8_t rtptype
)
826 struct ast_format
*format
= NULL
;
829 case RTP_PCMU
: format
= ast_format_ulaw
; break;
830 case RTP_PCMA
: format
= ast_format_alaw
; break;
831 case RTP_G722
: format
= ast_format_g722
; break;
832 case RTP_G726
: format
= ast_format_g726
; break;
833 case RTP_SLIN8
: format
= ast_format_slin
; break;
834 case RTP_SLIN16
: format
= ast_format_slin16
; break;
835 case RTP_ILBC
: format
= ast_format_ilbc
; break;
836 case RTP_SIREN7
: format
= ast_format_siren7
; break;
837 case RTP_G723_63
: format
= ast_format_g723
; break;
838 case RTP_G723_53
: format
= ast_format_g723
; break;
839 case RTP_G729
: format
= ast_format_g729
; break;
842 ast_log(LOG_ERROR
, "unsupported rtptype received is 0x%x, forcing ulaw\n", (unsigned) rtptype
);
843 format
= ast_format_ulaw
;
850 /* create rtp payload type from asterisk format */
851 static uint8_t lantiq_map_format_to_rtptype(const struct ast_format
*format
)
855 if (ast_format_cmp(format
, ast_format_ulaw
) == AST_FORMAT_CMP_EQUAL
)
857 else if (ast_format_cmp(format
, ast_format_alaw
) == AST_FORMAT_CMP_EQUAL
)
859 else if (ast_format_cmp(format
, ast_format_g722
) == AST_FORMAT_CMP_EQUAL
)
861 else if (ast_format_cmp(format
, ast_format_g726
) == AST_FORMAT_CMP_EQUAL
)
863 else if (ast_format_cmp(format
, ast_format_slin
) == AST_FORMAT_CMP_EQUAL
)
865 else if (ast_format_cmp(format
, ast_format_slin16
) == AST_FORMAT_CMP_EQUAL
)
866 rtptype
= RTP_SLIN16
;
867 else if (ast_format_cmp(format
, ast_format_ilbc
) == AST_FORMAT_CMP_EQUAL
)
869 else if (ast_format_cmp(format
, ast_format_siren7
) == AST_FORMAT_CMP_EQUAL
)
870 rtptype
= RTP_SIREN7
;
871 else if (ast_format_cmp(format
, ast_format_g723
) == AST_FORMAT_CMP_EQUAL
)
872 #if defined G723_HIGH_RATE
873 rtptype
= RTP_G723_63
;
875 rtptype
= RTP_G723_53
;
877 else if (ast_format_cmp(format
, ast_format_g729
) == AST_FORMAT_CMP_EQUAL
)
880 ast_log(LOG_ERROR
, "unsupported format %s, forcing ulaw\n", ast_format_get_name(format
));
887 static int lantiq_conf_enc(int c
, const struct ast_format
*format
)
889 /* Configure encoder before starting RTP session */
890 IFX_TAPI_ENC_CFG_t enc_cfg
;
892 memset(&enc_cfg
, 0, sizeof(IFX_TAPI_ENC_CFG_t
));
894 if (ast_format_cmp(format
, ast_format_ulaw
) == AST_FORMAT_CMP_EQUAL
) {
895 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_MLAW
;
896 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
897 iflist
[c
].ptime
= 10;
898 } else if (ast_format_cmp(format
, ast_format_alaw
) == AST_FORMAT_CMP_EQUAL
) {
899 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_ALAW
;
900 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
901 iflist
[c
].ptime
= 10;
902 } else if (ast_format_cmp(format
, ast_format_g722
) == AST_FORMAT_CMP_EQUAL
) {
903 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_G722_64
;
904 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
905 iflist
[c
].ptime
= 20;
906 } else if (ast_format_cmp(format
, ast_format_g726
) == AST_FORMAT_CMP_EQUAL
) {
907 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_G726_32
;
908 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
909 iflist
[c
].ptime
= 10;
910 } else if (ast_format_cmp(format
, ast_format_slin
) == AST_FORMAT_CMP_EQUAL
) {
911 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_LIN16_8
;
912 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
913 iflist
[c
].ptime
= 10;
914 } else if (ast_format_cmp(format
, ast_format_slin16
) == AST_FORMAT_CMP_EQUAL
) {
915 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_LIN16_16
;
916 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_10
;
917 iflist
[c
].ptime
= 10;
918 } else if (ast_format_cmp(format
, ast_format_ilbc
) == AST_FORMAT_CMP_EQUAL
) {
919 /* iLBC 15.2kbps is currently unsupported by Asterisk */
920 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_ILBC_133
;
921 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_30
;
922 iflist
[c
].ptime
= 30;
923 } else if (ast_format_cmp(format
, ast_format_siren7
) == AST_FORMAT_CMP_EQUAL
) {
924 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_G7221_32
;
925 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
926 iflist
[c
].ptime
= 20;
927 } else if (ast_format_cmp(format
, ast_format_g723
) == AST_FORMAT_CMP_EQUAL
) {
928 #if defined G723_HIGH_RATE
929 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_G723_63
;
931 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_G723_53
;
933 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_30
;
934 iflist
[c
].ptime
= 30;
935 } else if (ast_format_cmp(format
, ast_format_g729
) == AST_FORMAT_CMP_EQUAL
) {
936 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_G729
;
937 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
938 iflist
[c
].ptime
= 10;
940 ast_log(LOG_ERROR
, "unsupported format %s, forcing ulaw\n", ast_format_get_name(format
));
941 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_MLAW
;
942 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
943 iflist
[c
].ptime
= 10;
946 ast_log(LOG_DEBUG
, "Configuring encoder to use TAPI codec type %d (%s) on channel %i\n", enc_cfg
.nEncType
, ast_format_get_name(format
), c
);
948 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_ENC_CFG_SET
, &enc_cfg
)) {
949 ast_log(LOG_ERROR
, "IFX_TAPI_ENC_CFG_SET %d failed\n", c
);
952 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_ENC_START
, 0)) {
953 ast_log(LOG_ERROR
, "IFX_TAPI_ENC_START ioctl failed\n");
956 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_DEC_START
, 0)) {
957 ast_log(LOG_ERROR
, "IFX_TAPI_DEC_START ioctl failed\n");
963 static int ast_lantiq_write(struct ast_channel
*ast
, struct ast_frame
*frame
)
965 char buf
[RTP_BUFFER_LEN
];
966 rtp_header_t
*rtp_header
= (rtp_header_t
*) buf
;
967 struct lantiq_pvt
*pvt
= ast_channel_tech_pvt(ast
);
970 int subframes
, subframes_rtp
, length
, samples
;
973 if(frame
->frametype
!= AST_FRAME_VOICE
) {
974 ast_log(LOG_DEBUG
, "unhandled frame type\n");
978 if (frame
->datalen
== 0) {
979 ast_log(LOG_DEBUG
, "we've been prodded\n");
983 /* get rtp payload type */
984 rtptype
= lantiq_map_format_to_rtptype(frame
->subclass
.format
);
986 rtp_header
->version
= 2;
987 rtp_header
->padding
= 0;
988 rtp_header
->extension
= 0;
989 rtp_header
->csrc_count
= 0;
990 rtp_header
->marker
= 0;
991 rtp_header
->ssrc
= 0;
992 rtp_header
->payload_type
= rtptype
;
994 subframes
= (iflist
[pvt
->port_id
].ptime
+ frame
->len
- 1) / iflist
[pvt
->port_id
].ptime
; /* number of subframes in AST frame */
998 subframes_rtp
= (RTP_BUFFER_LEN
- RTP_HEADER_LEN
) * subframes
/ frame
->datalen
; /* how many frames fit in a single RTP packet */
1000 /* By default stick to the maximum multiple of native frame length */
1001 length
= subframes_rtp
* frame
->datalen
/ subframes
;
1002 samples
= length
* frame
->samples
/ frame
->datalen
;
1004 head
= frame
->data
.ptr
;
1005 tail
= frame
->data
.ptr
+ frame
->datalen
;
1006 while (head
< tail
) {
1007 rtp_header
->seqno
= pvt
->rtp_seqno
++;
1008 rtp_header
->timestamp
= pvt
->rtp_timestamp
;
1010 if ((tail
- head
) < (RTP_BUFFER_LEN
- RTP_HEADER_LEN
)) {
1011 length
= tail
- head
;
1012 samples
= length
* frame
->samples
/ frame
->datalen
;
1015 if ( frame
->datalen
<= (sizeof(buf
) - RTP_HEADER_LEN
) )
1016 memcpy(buf
+ RTP_HEADER_LEN
, head
, length
);
1018 ast_log(LOG_WARNING
, "buffer is too small\n");
1023 pvt
->rtp_timestamp
+= (rtp_header
->payload_type
== RTP_G722
) ? samples
/ 2 : samples
; /* per RFC3551 */
1025 ret
= write(dev_ctx
.ch_fd
[pvt
->port_id
], buf
, RTP_HEADER_LEN
+ length
);
1027 ast_debug(1, "TAPI: ast_lantiq_write(): error writing.\n");
1030 if (ret
!= (RTP_HEADER_LEN
+ length
)) {
1031 ast_log(LOG_WARNING
, "Short TAPI write of %d bytes, expected %d bytes\n", ret
, RTP_HEADER_LEN
+ length
);
1039 static int acf_channel_read(struct ast_channel
*chan
, const char *funcname
, char *args
, char *buf
, size_t buflen
)
1041 struct lantiq_pvt
*pvt
;
1044 if (!chan
|| ast_channel_tech(chan
) != &lantiq_tech
) {
1045 ast_log(LOG_ERROR
, "This function requires a valid Lantiq TAPI channel\n");
1049 ast_mutex_lock(&iflock
);
1051 pvt
= (struct lantiq_pvt
*) ast_channel_tech_pvt(chan
);
1053 if (!strcasecmp(args
, "csd")) {
1054 snprintf(buf
, buflen
, "%lu", (unsigned long int) pvt
->call_setup_delay
);
1055 } else if (!strcasecmp(args
, "jitter_stats")){
1056 lantiq_jb_get_stats(pvt
->port_id
);
1057 snprintf(buf
, buflen
, "jbBufSize=%u,jbUnderflow=%u,jbOverflow=%u,jbDelay=%u,jbInvalid=%u",
1058 (uint32_t) pvt
->jb_size
,
1059 (uint32_t) pvt
->jb_underflow
,
1060 (uint32_t) pvt
->jb_overflow
,
1061 (uint32_t) pvt
->jb_delay
,
1062 (uint32_t) pvt
->jb_invalid
);
1063 } else if (!strcasecmp(args
, "jbBufSize")) {
1064 snprintf(buf
, buflen
, "%u", (uint32_t) pvt
->jb_size
);
1065 } else if (!strcasecmp(args
, "jbUnderflow")) {
1066 snprintf(buf
, buflen
, "%u", (uint32_t) pvt
->jb_underflow
);
1067 } else if (!strcasecmp(args
, "jbOverflow")) {
1068 snprintf(buf
, buflen
, "%u", (uint32_t) pvt
->jb_overflow
);
1069 } else if (!strcasecmp(args
, "jbDelay")) {
1070 snprintf(buf
, buflen
, "%u", (uint32_t) pvt
->jb_delay
);
1071 } else if (!strcasecmp(args
, "jbInvalid")) {
1072 snprintf(buf
, buflen
, "%u", (uint32_t) pvt
->jb_invalid
);
1073 } else if (!strcasecmp(args
, "start")) {
1074 struct tm
*tm
= gmtime((const time_t*)&pvt
->call_start
);
1075 strftime(buf
, buflen
, "%F %T", tm
);
1076 } else if (!strcasecmp(args
, "answer")) {
1077 struct tm
*tm
= gmtime((const time_t*)&pvt
->call_answer
);
1078 strftime(buf
, buflen
, "%F %T", tm
);
1083 ast_mutex_unlock(&iflock
);
1088 static struct ast_frame
* ast_lantiq_exception(struct ast_channel
*ast
)
1090 ast_log(LOG_DEBUG
, "entering... no code here...\n");
1094 static void lantiq_jb_get_stats(int c
) {
1095 struct lantiq_pvt
*pvt
= &iflist
[c
];
1097 IFX_TAPI_JB_STATISTICS_t param
;
1098 memset (¶m
, 0, sizeof (param
));
1099 if (ioctl (dev_ctx
.ch_fd
[c
], IFX_TAPI_JB_STATISTICS_GET
, (IFX_int32_t
) ¶m
) != IFX_SUCCESS
) {
1100 ast_debug(1, "Error getting jitter buffer stats.\n");
1102 #if !defined (TAPI_VERSION3) && defined (TAPI_VERSION4)
1103 ast_debug(1, "Jitter buffer stats: dev=%u, ch=%u, nType=%u, nBufSize=%u, nIsUnderflow=%u, nDsOverflow=%u, nPODelay=%u, nInvalid=%u\n",
1104 (uint32_t) param
.dev
,
1105 (uint32_t) param
.ch
,
1107 ast_debug(1, "Jitter buffer stats: nType=%u, nBufSize=%u, nIsUnderflow=%u, nDsOverflow=%u, nPODelay=%u, nInvalid=%u\n",
1109 (uint32_t) param
.nType
,
1110 (uint32_t) param
.nBufSize
,
1111 (uint32_t) param
.nIsUnderflow
,
1112 (uint32_t) param
.nDsOverflow
,
1113 (uint32_t) param
.nPODelay
,
1114 (uint32_t) param
.nInvalid
);
1116 pvt
->jb_size
= param
.nBufSize
;
1117 pvt
->jb_underflow
= param
.nIsUnderflow
;
1118 pvt
->jb_overflow
= param
.nDsOverflow
;
1119 pvt
->jb_invalid
= param
.nInvalid
;
1120 pvt
->jb_delay
= param
.nPODelay
;
1125 static int lantiq_standby(int c
)
1127 ast_debug(1, "Stopping line feed for channel %i\n", c
);
1128 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_LINE_FEED_SET
, IFX_TAPI_LINE_FEED_STANDBY
)) {
1129 ast_log(LOG_ERROR
, "IFX_TAPI_LINE_FEED_SET ioctl failed\n");
1133 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_ENC_STOP
, 0)) {
1134 ast_log(LOG_ERROR
, "IFX_TAPI_ENC_STOP ioctl failed\n");
1138 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_DEC_STOP
, 0)) {
1139 ast_log(LOG_ERROR
, "IFX_TAPI_DEC_STOP ioctl failed\n");
1143 return lantiq_play_tone(c
, TAPI_TONE_LOCALE_NONE
);
1146 static int lantiq_end_dialing(int c
)
1148 struct lantiq_pvt
*pvt
= &iflist
[c
];
1150 ast_log(LOG_DEBUG
, "end of dialing\n");
1152 if (pvt
->dial_timer
!= -1) {
1153 AST_SCHED_DEL(sched
, pvt
->dial_timer
);
1154 pvt
->dial_timer
= -1;
1158 ast_hangup(pvt
->owner
);
1160 lantiq_reset_dtmfbuf(pvt
);
1165 static int lantiq_end_call(int c
)
1167 struct lantiq_pvt
*pvt
= &iflist
[c
];
1169 ast_log(LOG_DEBUG
, "end of call\n");
1172 lantiq_jb_get_stats(c
);
1173 ast_queue_hangup(pvt
->owner
);
1179 static struct ast_channel
*lantiq_channel(int state
, int c
, char *ext
, char *ctx
, struct ast_format_cap
*cap
, const struct ast_assigned_ids
*assigned_ids
, const struct ast_channel
*requestor
)
1181 struct ast_channel
*chan
= NULL
;
1182 struct lantiq_pvt
*pvt
= &iflist
[c
];
1183 struct ast_format
*format
= NULL
;
1184 struct ast_format_cap
*newcap
= ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT
);
1185 struct ast_format_cap
*formatcap
= ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT
);
1187 if (!newcap
|| !formatcap
) {
1188 ast_log(LOG_DEBUG
, "Cannot allocate format capabilities!\n");
1192 chan
= ast_channel_alloc(1, state
, NULL
, NULL
, "", ext
, ctx
, assigned_ids
, requestor
, c
, "TAPI/%d", (c
+ 1));
1194 ast_log(LOG_DEBUG
, "Cannot allocate channel!\n");
1195 ao2_ref(newcap
, -1);
1196 ao2_ref(formatcap
, -1);
1200 ast_channel_tech_set(chan
, &lantiq_tech
);
1201 ast_channel_tech_pvt_set(chan
, pvt
);
1204 if (cap
&& ast_format_cap_iscompatible(cap
, lantiq_tech
.capabilities
)) { /* compatible format capabilities given */
1205 ast_format_cap_get_compatible(lantiq_tech
.capabilities
, cap
, newcap
);
1206 format
= ast_format_cap_get_format(newcap
, 0); /* choose format */
1207 } else { /* no or unsupported format capabilities given */
1208 format
= ast_format_cap_get_format(lantiq_tech
.capabilities
, 0); /* choose format from capabilities */
1211 /* set choosed format */
1212 ast_format_cap_append(formatcap
, format
, 0);
1213 ast_channel_nativeformats_set(chan
, formatcap
);
1214 ast_channel_set_readformat(chan
, format
);
1215 ast_channel_set_writeformat(chan
, format
);
1216 ast_channel_set_rawreadformat(chan
, format
);
1217 ast_channel_set_rawwriteformat(chan
, format
);
1219 ao2_ref(newcap
, -1);
1220 ao2_ref(formatcap
, -1);
1222 /* configuring encoder */
1224 if (lantiq_conf_enc(c
, format
) < 0)
1230 static struct ast_channel
*ast_lantiq_requester(const char *type
, struct ast_format_cap
*cap
, const struct ast_assigned_ids
*assigned_ids
, const struct ast_channel
*requestor
, const char *data
, int *cause
)
1232 struct ast_str
*buf
= ast_str_alloca(64);
1233 struct ast_channel
*chan
= NULL
;
1236 ast_mutex_lock(&iflock
);
1238 ast_debug(1, "Asked to create a TAPI channel with formats: %s\n", ast_format_cap_get_names(cap
, &buf
));
1240 /* check for correct data argument */
1241 if (ast_strlen_zero(data
)) {
1242 ast_log(LOG_ERROR
, "Unable to create channel with empty destination.\n");
1243 *cause
= AST_CAUSE_CHANNEL_UNACCEPTABLE
;
1247 /* get our port number */
1248 port_id
= atoi((char*) data
);
1249 if (port_id
< 1 || port_id
> dev_ctx
.channels
) {
1250 ast_log(LOG_ERROR
, "Unknown channel ID: \"%s\"\n", data
);
1251 *cause
= AST_CAUSE_CHANNEL_UNACCEPTABLE
;
1255 /* on asterisk user's side, we're using port 1-2.
1256 * Here in non normal human's world, we begin
1262 /* Bail out if channel is already in use */
1263 struct lantiq_pvt
*pvt
= &iflist
[port_id
];
1264 if (! pvt
->channel_state
== ONHOOK
) {
1265 ast_debug(1, "TAPI channel %i alread in use.\n", port_id
+1);
1267 chan
= lantiq_channel(AST_STATE_DOWN
, port_id
, NULL
, NULL
, cap
, assigned_ids
, requestor
);
1268 ast_channel_unlock(chan
); /* it's essential to unlock channel */
1272 ast_mutex_unlock(&iflock
);
1276 static int ast_lantiq_devicestate(const char *data
)
1278 int port
= atoi(data
) - 1;
1279 if ((port
< 1) || (port
> dev_ctx
.channels
)) {
1280 return AST_DEVICE_INVALID
;
1283 switch (iflist
[port
].channel_state
) {
1285 return AST_DEVICE_NOT_INUSE
;
1290 return AST_DEVICE_INUSE
;
1292 return AST_DEVICE_RINGING
;
1295 return AST_DEVICE_UNKNOWN
;
1299 static int lantiq_dev_data_handler(int c
)
1302 struct ast_frame frame
= {0};
1303 struct ast_format
*format
= NULL
;
1305 int res
= read(dev_ctx
.ch_fd
[c
], buf
, sizeof(buf
));
1307 ast_log(LOG_ERROR
, "we got read error %i\n", res
);
1309 } else if (res
< RTP_HEADER_LEN
) {
1310 ast_log(LOG_ERROR
, "we got data smaller than header size\n");
1314 rtp_header_t
*rtp
= (rtp_header_t
*) buf
;
1315 struct lantiq_pvt
*pvt
= (struct lantiq_pvt
*) &iflist
[c
];
1316 if ((!pvt
->owner
) || (ast_channel_state(pvt
->owner
) != AST_STATE_UP
)) {
1320 if (rtp
->payload_type
== RTP_CN
) {
1321 /* TODO: Handle Comfort Noise frames */
1322 ast_debug(1, "Dropping Comfort Noise frame\n");
1325 format
= lantiq_map_rtptype_to_format(rtp
->payload_type
);
1327 frame
.frametype
= AST_FRAME_VOICE
;
1328 frame
.subclass
.format
= format
;
1329 frame
.datalen
= res
- RTP_HEADER_LEN
;
1330 frame
.data
.ptr
= buf
+ RTP_HEADER_LEN
;
1331 frame
.samples
= ast_codec_samples_count(&frame
);
1333 if(!ast_channel_trylock(pvt
->owner
)) {
1334 ast_queue_frame(pvt
->owner
, &frame
);
1335 ast_channel_unlock(pvt
->owner
);
1341 static int accept_call(int c
)
1343 struct lantiq_pvt
*pvt
= &iflist
[c
];
1345 ast_log(LOG_DEBUG
, "accept call\n");
1348 struct ast_channel
*chan
= pvt
->owner
;
1350 switch (ast_channel_state(chan
)) {
1351 case AST_STATE_RINGING
:
1352 lantiq_play_tone(c
, TAPI_TONE_LOCALE_NONE
);
1353 ast_queue_control(pvt
->owner
, AST_CONTROL_ANSWER
);
1354 pvt
->channel_state
= INCALL
;
1355 pvt
->call_start
= epoch();
1356 pvt
->call_answer
= pvt
->call_start
;
1359 ast_log(LOG_WARNING
, "entered unhandled state %s\n", ast_state2str(ast_channel_state(chan
)));
1366 static int lantiq_dev_event_hook(int c
, int state
)
1368 ast_mutex_lock(&iflock
);
1370 ast_log(LOG_DEBUG
, "on port %i detected event %s hook\n", c
, state
? "on" : "off");
1372 struct lantiq_pvt
*pvt
= &iflist
[c
];
1375 if (state
) { /* going onhook */
1376 switch (iflist
[c
].channel_state
) {
1378 ret
= lantiq_end_dialing(c
);
1381 ret
= lantiq_end_call(c
);
1385 iflist
[c
].channel_state
= ONHOOK
;
1387 /* stop DSP data feed */
1389 led_off(dev_ctx
.ch_led
[c
]);
1391 } else { /* going offhook */
1392 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_LINE_FEED_SET
, IFX_TAPI_LINE_FEED_ACTIVE
)) {
1393 ast_log(LOG_ERROR
, "IFX_TAPI_LINE_FEED_SET ioctl failed\n");
1397 switch (iflist
[c
].channel_state
) {
1399 ret
= accept_call(c
);
1400 led_blink(dev_ctx
.ch_led
[c
], LED_SLOW_BLINK
);
1403 iflist
[c
].channel_state
= OFFHOOK
;
1404 lantiq_play_tone(c
, TAPI_TONE_LOCALE_DIAL_CODE
);
1405 lantiq_reset_dtmfbuf(pvt
);
1407 led_on(dev_ctx
.ch_led
[c
]);
1414 ast_mutex_unlock(&iflock
);
1419 static void lantiq_reset_dtmfbuf(struct lantiq_pvt
*pvt
)
1421 pvt
->dtmfbuf
[0] = '\0';
1422 pvt
->dtmfbuf_len
= 0;
1425 static void lantiq_dial(struct lantiq_pvt
*pvt
)
1427 struct ast_channel
*chan
= NULL
;
1429 ast_mutex_lock(&iflock
);
1430 ast_log(LOG_DEBUG
, "user want's to dial %s.\n", pvt
->dtmfbuf
);
1432 if (ast_exists_extension(NULL
, pvt
->context
, pvt
->dtmfbuf
, 1, NULL
)) {
1433 ast_debug(1, "found extension %s, dialing\n", pvt
->dtmfbuf
);
1435 ast_verbose(VERBOSE_PREFIX_3
" extension exists, starting PBX %s\n", pvt
->dtmfbuf
);
1437 chan
= lantiq_channel(AST_STATE_UP
, pvt
->port_id
, pvt
->dtmfbuf
, pvt
->context
, NULL
, 0, NULL
);
1439 ast_log(LOG_ERROR
, "couldn't create channel\n");
1442 ast_channel_tech_pvt_set(chan
, pvt
);
1445 ast_setstate(chan
, AST_STATE_RING
);
1446 pvt
->channel_state
= INCALL
;
1448 pvt
->call_setup_start
= now();
1449 pvt
->call_start
= epoch();
1451 if (ast_pbx_start(chan
)) {
1452 ast_log(LOG_WARNING
, " unable to start PBX on %s\n", ast_channel_name(chan
));
1456 ast_channel_unlock(chan
); /* it's essential to unlock channel */
1458 ast_log(LOG_DEBUG
, "no extension found\n");
1459 lantiq_play_tone(pvt
->port_id
, TAPI_TONE_LOCALE_CONGESTION_CODE
);
1460 pvt
->channel_state
= CALL_ENDED
;
1463 lantiq_reset_dtmfbuf(pvt
);
1465 ast_mutex_unlock(&iflock
);
1468 static int lantiq_event_dial_timeout(const void* data
)
1470 ast_debug(1, "TAPI: lantiq_event_dial_timeout()\n");
1472 struct lantiq_pvt
*pvt
= (struct lantiq_pvt
*) data
;
1473 pvt
->dial_timer
= -1;
1475 if (! pvt
->channel_state
== ONHOOK
) {
1478 ast_debug(1, "TAPI: lantiq_event_dial_timeout(): dial timeout in state ONHOOK.\n");
1484 static int lantiq_send_digit(int c
, char digit
)
1486 struct lantiq_pvt
*pvt
= &iflist
[c
];
1488 struct ast_frame f
= { .frametype
= AST_FRAME_DTMF
, .subclass
.integer
= digit
};
1491 ast_log(LOG_DEBUG
, "Port %i transmitting digit \"%c\"\n", c
, digit
);
1492 return ast_queue_frame(pvt
->owner
, &f
);
1494 ast_debug(1, "Warning: lantiq_send_digit() without owner!\n");
1499 static void lantiq_dev_event_digit(int c
, char digit
)
1501 ast_mutex_lock(&iflock
);
1503 ast_log(LOG_DEBUG
, "on port %i detected digit \"%c\"\n", c
, digit
);
1505 struct lantiq_pvt
*pvt
= &iflist
[c
];
1507 switch (pvt
->channel_state
) {
1509 lantiq_send_digit(c
, digit
);
1512 pvt
->channel_state
= DIALING
;
1514 lantiq_play_tone(c
, TAPI_TONE_LOCALE_NONE
);
1515 led_blink(dev_ctx
.ch_led
[c
], LED_SLOW_BLINK
);
1519 if (pvt
->dtmfbuf_len
< AST_MAX_EXTENSION
- 1) {
1520 pvt
->dtmfbuf
[pvt
->dtmfbuf_len
] = digit
;
1521 pvt
->dtmfbuf
[++pvt
->dtmfbuf_len
] = '\0';
1523 /* No more room for another digit */
1524 lantiq_end_dialing(c
);
1525 lantiq_play_tone(pvt
->port_id
, TAPI_TONE_LOCALE_BUSY_CODE
);
1526 pvt
->channel_state
= CALL_ENDED
;
1530 /* setup autodial timer */
1531 if (pvt
->dial_timer
== -1) {
1532 ast_log(LOG_DEBUG
, "setting new timer\n");
1533 pvt
->dial_timer
= ast_sched_add(sched
, dev_ctx
.interdigit_timeout
, lantiq_event_dial_timeout
, (const void*) pvt
);
1535 ast_log(LOG_DEBUG
, "replacing timer\n");
1536 AST_SCHED_REPLACE(pvt
->dial_timer
, sched
, dev_ctx
.interdigit_timeout
, lantiq_event_dial_timeout
, (const void*) pvt
);
1540 ast_log(LOG_ERROR
, "don't know what to do in unhandled state\n");
1544 ast_mutex_unlock(&iflock
);
1548 static void lantiq_dev_event_handler(void)
1550 IFX_TAPI_EVENT_t event
;
1553 for (i
= 0; i
< dev_ctx
.channels
; i
++) {
1554 ast_mutex_lock(&iflock
);
1556 memset (&event
, 0, sizeof(event
));
1558 if (ioctl(dev_ctx
.dev_fd
, IFX_TAPI_EVENT_GET
, &event
)) {
1559 ast_mutex_unlock(&iflock
);
1562 if (event
.id
== IFX_TAPI_EVENT_NONE
) {
1563 ast_mutex_unlock(&iflock
);
1567 ast_mutex_unlock(&iflock
);
1570 case IFX_TAPI_EVENT_FXS_ONHOOK
:
1571 lantiq_dev_event_hook(i
, 1);
1573 case IFX_TAPI_EVENT_FXS_OFFHOOK
:
1574 lantiq_dev_event_hook(i
, 0);
1576 case IFX_TAPI_EVENT_DTMF_DIGIT
:
1577 lantiq_dev_event_digit(i
, (char)event
.data
.dtmf
.ascii
);
1579 case IFX_TAPI_EVENT_PULSE_DIGIT
:
1580 if (event
.data
.pulse
.digit
== 0xB) {
1581 lantiq_dev_event_digit(i
, '0');
1583 lantiq_dev_event_digit(i
, '0' + (char)event
.data
.pulse
.digit
);
1586 case IFX_TAPI_EVENT_COD_DEC_CHG
:
1587 case IFX_TAPI_EVENT_TONE_GEN_END
:
1588 case IFX_TAPI_EVENT_CID_TX_SEQ_END
:
1591 ast_log(LOG_ERROR
, "Unknown TAPI event %08X. Restarting Asterisk...\n", event
.id
);
1593 ast_cli_command(-1, "core restart now");
1599 static void * lantiq_events_monitor(void *data
)
1601 ast_verbose("TAPI thread started\n");
1603 struct pollfd fds
[TAPI_AUDIO_PORT_NUM_MAX
+ 1];
1606 fds
[0].fd
= dev_ctx
.dev_fd
;
1607 fds
[0].events
= POLLIN
;
1608 for (c
= 0; c
< dev_ctx
.channels
; c
++) {
1609 fds
[c
+ 1].fd
= dev_ctx
.ch_fd
[c
];
1610 fds
[c
+ 1].events
= POLLIN
;
1613 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS
, NULL
);
1615 if (poll(fds
, dev_ctx
.channels
+ 1, 2000) <= 0) {
1619 ast_mutex_lock(&monlock
);
1620 if (fds
[0].revents
& POLLIN
) {
1621 lantiq_dev_event_handler();
1624 for (c
= 0; c
< dev_ctx
.channels
; c
++) {
1625 if ((fds
[c
+ 1].revents
& POLLIN
) && (lantiq_dev_data_handler(c
))) {
1626 ast_log(LOG_ERROR
, "data handler %d failed\n", c
);
1630 ast_mutex_unlock(&monlock
);
1636 static int restart_monitor(void)
1638 /* If we're supposed to be stopped -- stay stopped */
1639 if (monitor_thread
== AST_PTHREADT_STOP
)
1642 ast_mutex_lock(&monlock
);
1644 if (monitor_thread
== pthread_self()) {
1645 ast_mutex_unlock(&monlock
);
1646 ast_log(LOG_WARNING
, "Cannot kill myself\n");
1650 if (monitor_thread
!= AST_PTHREADT_NULL
) {
1651 /* Wake up the thread */
1652 pthread_kill(monitor_thread
, SIGURG
);
1654 /* Start a new monitor */
1655 if (ast_pthread_create_background(&monitor_thread
, NULL
, lantiq_events_monitor
, NULL
) < 0) {
1656 ast_mutex_unlock(&monlock
);
1657 ast_log(LOG_ERROR
, "Unable to start monitor thread.\n");
1661 ast_mutex_unlock(&monlock
);
1666 static void lantiq_cleanup(void)
1670 if (dev_ctx
.dev_fd
< 0) {
1674 for (c
= 0; c
< dev_ctx
.channels
; c
++) {
1675 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_LINE_FEED_SET
, IFX_TAPI_LINE_FEED_STANDBY
)) {
1676 ast_log(LOG_WARNING
, "IFX_TAPI_LINE_FEED_SET ioctl failed\n");
1679 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_ENC_STOP
, 0)) {
1680 ast_log(LOG_WARNING
, "IFX_TAPI_ENC_STOP ioctl failed\n");
1683 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_DEC_STOP
, 0)) {
1684 ast_log(LOG_WARNING
, "IFX_TAPI_DEC_STOP ioctl failed\n");
1686 led_off(dev_ctx
.ch_led
[c
]);
1689 if (ioctl(dev_ctx
.dev_fd
, IFX_TAPI_DEV_STOP
, 0)) {
1690 ast_log(LOG_WARNING
, "IFX_TAPI_DEV_STOP ioctl failed\n");
1693 close(dev_ctx
.dev_fd
);
1694 dev_ctx
.dev_fd
= -1;
1695 led_off(dev_ctx
.voip_led
);
1698 static int unload_module(void)
1702 ast_channel_unregister(&lantiq_tech
);
1704 if (ast_mutex_lock(&iflock
)) {
1705 ast_log(LOG_WARNING
, "Unable to lock the interface list\n");
1708 for (c
= 0; c
< dev_ctx
.channels
; c
++) {
1709 if (iflist
[c
].owner
)
1710 ast_softhangup(iflist
[c
].owner
, AST_SOFTHANGUP_APPUNLOAD
);
1712 ast_mutex_unlock(&iflock
);
1714 if (ast_mutex_lock(&monlock
)) {
1715 ast_log(LOG_WARNING
, "Unable to lock the monitor\n");
1718 if (monitor_thread
&& (monitor_thread
!= AST_PTHREADT_STOP
) && (monitor_thread
!= AST_PTHREADT_NULL
)) {
1719 pthread_t th
= monitor_thread
;
1720 monitor_thread
= AST_PTHREADT_STOP
;
1722 pthread_kill(th
, SIGURG
);
1723 ast_mutex_unlock(&monlock
);
1724 pthread_join(th
, NULL
);
1726 monitor_thread
= AST_PTHREADT_STOP
;
1727 ast_mutex_unlock(&monlock
);
1730 ast_sched_context_destroy(sched
);
1731 ast_mutex_destroy(&iflock
);
1732 ast_mutex_destroy(&monlock
);
1740 static struct lantiq_pvt
*lantiq_init_pvt(struct lantiq_pvt
*pvt
)
1745 pvt
->channel_state
= UNKNOWN
;
1746 pvt
->context
[0] = '\0';
1747 pvt
->dial_timer
= -1;
1748 pvt
->dtmfbuf
[0] = '\0';
1749 pvt
->dtmfbuf_len
= 0;
1750 pvt
->call_setup_start
= 0;
1751 pvt
->call_setup_delay
= 0;
1752 pvt
->call_answer
= 0;
1754 pvt
->jb_underflow
= 0;
1755 pvt
->jb_overflow
= 0;
1757 pvt
->jb_invalid
= 0;
1759 ast_log(LOG_ERROR
, "unable to clear pvt structure\n");
1765 static int lantiq_create_pvts(void)
1769 iflist
= ast_calloc(1, sizeof(struct lantiq_pvt
) * dev_ctx
.channels
);
1772 ast_log(LOG_ERROR
, "unable to allocate memory\n");
1776 for (i
= 0; i
< dev_ctx
.channels
; i
++) {
1777 lantiq_init_pvt(&iflist
[i
]);
1778 iflist
[i
].port_id
= i
;
1779 if (per_channel_context
) {
1780 snprintf(iflist
[i
].context
, AST_MAX_CONTEXT
, "%s%i", LANTIQ_CONTEXT_PREFIX
, i
+ 1);
1781 ast_debug(1, "Context for channel %i: %s\n", i
, iflist
[i
].context
);
1783 snprintf(iflist
[i
].context
, AST_MAX_CONTEXT
, "default");
1789 static int lantiq_setup_rtp(int c
)
1791 /* Configure RTP payload type tables */
1792 IFX_TAPI_PKT_RTP_PT_CFG_t rtpPTConf
;
1794 memset((char*)&rtpPTConf
, '\0', sizeof(rtpPTConf
));
1796 rtpPTConf
.nPTup
[IFX_TAPI_COD_TYPE_G723_63
] = rtpPTConf
.nPTdown
[IFX_TAPI_COD_TYPE_G723_63
] = RTP_G723_63
;
1797 rtpPTConf
.nPTup
[IFX_TAPI_COD_TYPE_G723_53
] = rtpPTConf
.nPTdown
[IFX_TAPI_COD_TYPE_G723_53
] = RTP_G723_53
;
1798 rtpPTConf
.nPTup
[IFX_TAPI_COD_TYPE_G729
] = rtpPTConf
.nPTdown
[IFX_TAPI_COD_TYPE_G729
] = RTP_G729
;
1799 rtpPTConf
.nPTup
[IFX_TAPI_COD_TYPE_MLAW
] = rtpPTConf
.nPTdown
[IFX_TAPI_COD_TYPE_MLAW
] = RTP_PCMU
;
1800 rtpPTConf
.nPTup
[IFX_TAPI_COD_TYPE_ALAW
] = rtpPTConf
.nPTdown
[IFX_TAPI_COD_TYPE_ALAW
] = RTP_PCMA
;
1801 rtpPTConf
.nPTup
[IFX_TAPI_COD_TYPE_G726_32
] = rtpPTConf
.nPTdown
[IFX_TAPI_COD_TYPE_G726_32
] = RTP_G726
;
1802 rtpPTConf
.nPTup
[IFX_TAPI_COD_TYPE_ILBC_152
] = rtpPTConf
.nPTdown
[IFX_TAPI_COD_TYPE_ILBC_152
] = RTP_ILBC
;
1803 rtpPTConf
.nPTup
[IFX_TAPI_COD_TYPE_LIN16_8
] = rtpPTConf
.nPTdown
[IFX_TAPI_COD_TYPE_LIN16_8
] = RTP_SLIN8
;
1804 rtpPTConf
.nPTup
[IFX_TAPI_COD_TYPE_LIN16_16
] = rtpPTConf
.nPTdown
[IFX_TAPI_COD_TYPE_LIN16_16
] = RTP_SLIN16
;
1805 rtpPTConf
.nPTup
[IFX_TAPI_COD_TYPE_G722_64
] = rtpPTConf
.nPTdown
[IFX_TAPI_COD_TYPE_G722_64
] = RTP_G722
;
1806 rtpPTConf
.nPTup
[IFX_TAPI_COD_TYPE_G7221_32
] = rtpPTConf
.nPTdown
[IFX_TAPI_COD_TYPE_G7221_32
] = RTP_G7221
;
1809 if ((ret
= ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_PKT_RTP_PT_CFG_SET
, (IFX_int32_t
) &rtpPTConf
))) {
1810 ast_log(LOG_ERROR
, "IFX_TAPI_PKT_RTP_PT_CFG_SET failed: ret=%i\n", ret
);
1817 static int load_module(void)
1819 struct ast_config
*cfg
;
1820 struct ast_variable
*v
;
1828 int jb_type
= IFX_TAPI_JB_TYPE_ADAPTIVE
;
1829 int jb_pckadpt
= IFX_TAPI_JB_PKT_ADAPT_VOICE
;
1830 int jb_localadpt
= IFX_TAPI_JB_LOCAL_ADAPT_DEFAULT
;
1831 int jb_scaling
= 0x10;
1832 int jb_initialsize
= 0x2d0;
1833 int jb_minsize
= 0x50;
1834 int jb_maxsize
= 0x5a0;
1835 int cid_type
= IFX_TAPI_CID_STD_TELCORDIA
;
1836 int vad_type
= IFX_TAPI_ENC_VAD_NOVAD
;
1837 dev_ctx
.dev_fd
= -1;
1838 dev_ctx
.channels
= TAPI_AUDIO_PORT_NUM_MAX
;
1839 dev_ctx
.interdigit_timeout
= DEFAULT_INTERDIGIT_TIMEOUT
;
1840 struct ast_tone_zone
*tz
;
1841 struct ast_flags config_flags
= { 0 };
1844 if(!(lantiq_tech
.capabilities
= ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT
))) {
1845 ast_log(LOG_ERROR
, "Unable to allocate format capabilities.\n");
1846 return AST_MODULE_LOAD_DECLINE
;
1849 /* channel format capabilities */
1850 ast_format_cap_append(lantiq_tech
.capabilities
, ast_format_ulaw
, 0);
1851 ast_format_cap_append(lantiq_tech
.capabilities
, ast_format_alaw
, 0);
1852 ast_format_cap_append(lantiq_tech
.capabilities
, ast_format_g722
, 0);
1853 ast_format_cap_append(lantiq_tech
.capabilities
, ast_format_g726
, 0);
1854 ast_format_cap_append(lantiq_tech
.capabilities
, ast_format_slin
, 0);
1856 /* Turn off the LEDs, just in case */
1857 led_off(dev_ctx
.voip_led
);
1858 for(c
= 0; c
< TAPI_AUDIO_PORT_NUM_MAX
; c
++)
1859 led_off(dev_ctx
.ch_led
[c
]);
1861 if ((cfg
= ast_config_load(config
, config_flags
)) == CONFIG_STATUS_FILEINVALID
) {
1862 ast_log(LOG_ERROR
, "Config file %s is in an invalid format. Aborting.\n", config
);
1863 return AST_MODULE_LOAD_DECLINE
;
1866 /* We *must* have a config file otherwise stop immediately */
1868 ast_log(LOG_ERROR
, "Unable to load config %s\n", config
);
1869 return AST_MODULE_LOAD_DECLINE
;
1872 if (ast_mutex_lock(&iflock
)) {
1873 ast_log(LOG_ERROR
, "Unable to lock interface list.\n");
1877 for (v
= ast_variable_browse(cfg
, "interfaces"); v
; v
= v
->next
) {
1878 if (!strcasecmp(v
->name
, "channels")) {
1879 dev_ctx
.channels
= atoi(v
->value
);
1880 if (!dev_ctx
.channels
) {
1881 ast_log(LOG_ERROR
, "Invalid value for channels in config %s\n", config
);
1884 } else if (!strcasecmp(v
->name
, "firmwarefilename")) {
1885 ast_copy_string(firmware_filename
, v
->value
, sizeof(firmware_filename
));
1886 } else if (!strcasecmp(v
->name
, "bbdfilename")) {
1887 ast_copy_string(bbd_filename
, v
->value
, sizeof(bbd_filename
));
1888 } else if (!strcasecmp(v
->name
, "basepath")) {
1889 ast_copy_string(base_path
, v
->value
, sizeof(base_path
));
1890 } else if (!strcasecmp(v
->name
, "per_channel_context")) {
1891 if (!strcasecmp(v
->value
, "on")) {
1892 per_channel_context
= 1;
1893 } else if (!strcasecmp(v
->value
, "off")) {
1894 per_channel_context
= 0;
1896 ast_log(LOG_ERROR
, "Unknown per_channel_context value '%s'. Try 'on' or 'off'.\n", v
->value
);
1902 for (v
= ast_variable_browse(cfg
, "general"); v
; v
= v
->next
) {
1903 if (!strcasecmp(v
->name
, "rxgain")) {
1904 rxgain
= atoi(v
->value
);
1907 ast_log(LOG_WARNING
, "Invalid rxgain: %s, using default.\n", v
->value
);
1909 } else if (!strcasecmp(v
->name
, "txgain")) {
1910 txgain
= atoi(v
->value
);
1913 ast_log(LOG_WARNING
, "Invalid txgain: %s, using default.\n", v
->value
);
1915 } else if (!strcasecmp(v
->name
, "echocancel")) {
1916 if (!strcasecmp(v
->value
, "off")) {
1917 wlec_type
= IFX_TAPI_WLEC_TYPE_OFF
;
1918 } else if (!strcasecmp(v
->value
, "nlec")) {
1919 wlec_type
= IFX_TAPI_WLEC_TYPE_NE
;
1920 if (!strcasecmp(v
->name
, "echocancelfixedwindowsize")) {
1921 wlec_nbne
= atoi(v
->value
);
1923 } else if (!strcasecmp(v
->value
, "wlec")) {
1924 wlec_type
= IFX_TAPI_WLEC_TYPE_NFE
;
1925 if (!strcasecmp(v
->name
, "echocancelnfemovingwindowsize")) {
1926 wlec_nbfe
= atoi(v
->value
);
1927 } else if (!strcasecmp(v
->name
, "echocancelfixedwindowsize")) {
1928 wlec_nbne
= atoi(v
->value
);
1929 } else if (!strcasecmp(v
->name
, "echocancelwidefixedwindowsize")) {
1930 wlec_wbne
= atoi(v
->value
);
1932 } else if (!strcasecmp(v
->value
, "nees")) {
1933 wlec_type
= IFX_TAPI_WLEC_TYPE_NE_ES
;
1934 } else if (!strcasecmp(v
->value
, "nfees")) {
1935 wlec_type
= IFX_TAPI_WLEC_TYPE_NFE_ES
;
1936 } else if (!strcasecmp(v
->value
, "es")) {
1937 wlec_type
= IFX_TAPI_WLEC_TYPE_ES
;
1939 wlec_type
= IFX_TAPI_WLEC_TYPE_OFF
;
1940 ast_log(LOG_ERROR
, "Unknown echo cancellation type '%s'\n", v
->value
);
1943 } else if (!strcasecmp(v
->name
, "echocancelnlp")) {
1944 if (!strcasecmp(v
->value
, "on")) {
1945 wlec_nlp
= IFX_TAPI_WLEC_NLP_ON
;
1946 } else if (!strcasecmp(v
->value
, "off")) {
1947 wlec_nlp
= IFX_TAPI_WLEC_NLP_OFF
;
1949 ast_log(LOG_ERROR
, "Unknown echo cancellation nlp '%s'\n", v
->value
);
1952 } else if (!strcasecmp(v
->name
, "jitterbuffertype")) {
1953 if (!strcasecmp(v
->value
, "fixed")) {
1954 jb_type
= IFX_TAPI_JB_TYPE_FIXED
;
1955 } else if (!strcasecmp(v
->value
, "adaptive")) {
1956 jb_type
= IFX_TAPI_JB_TYPE_ADAPTIVE
;
1957 jb_localadpt
= IFX_TAPI_JB_LOCAL_ADAPT_DEFAULT
;
1958 if (!strcasecmp(v
->name
, "jitterbufferadaptation")) {
1959 if (!strcasecmp(v
->value
, "on")) {
1960 jb_localadpt
= IFX_TAPI_JB_LOCAL_ADAPT_ON
;
1961 } else if (!strcasecmp(v
->value
, "off")) {
1962 jb_localadpt
= IFX_TAPI_JB_LOCAL_ADAPT_OFF
;
1964 } else if (!strcasecmp(v
->name
, "jitterbufferscalling")) {
1965 jb_scaling
= atoi(v
->value
);
1966 } else if (!strcasecmp(v
->name
, "jitterbufferinitialsize")) {
1967 jb_initialsize
= atoi(v
->value
);
1968 } else if (!strcasecmp(v
->name
, "jitterbufferminsize")) {
1969 jb_minsize
= atoi(v
->value
);
1970 } else if (!strcasecmp(v
->name
, "jitterbuffermaxsize")) {
1971 jb_maxsize
= atoi(v
->value
);
1974 ast_log(LOG_ERROR
, "Unknown jitter buffer type '%s'\n", v
->value
);
1977 } else if (!strcasecmp(v
->name
, "jitterbufferpackettype")) {
1978 if (!strcasecmp(v
->value
, "voice")) {
1979 jb_pckadpt
= IFX_TAPI_JB_PKT_ADAPT_VOICE
;
1980 } else if (!strcasecmp(v
->value
, "data")) {
1981 jb_pckadpt
= IFX_TAPI_JB_PKT_ADAPT_DATA
;
1982 } else if (!strcasecmp(v
->value
, "datanorep")) {
1983 jb_pckadpt
= IFX_TAPI_JB_PKT_ADAPT_DATA_NO_REP
;
1985 ast_log(LOG_ERROR
, "Unknown jitter buffer packet adaptation type '%s'\n", v
->value
);
1988 } else if (!strcasecmp(v
->name
, "calleridtype")) {
1989 ast_log(LOG_DEBUG
, "Setting CID type to %s.\n", v
->value
);
1990 if (!strcasecmp(v
->value
, "telecordia")) {
1991 cid_type
= IFX_TAPI_CID_STD_TELCORDIA
;
1992 } else if (!strcasecmp(v
->value
, "etsifsk")) {
1993 cid_type
= IFX_TAPI_CID_STD_ETSI_FSK
;
1994 } else if (!strcasecmp(v
->value
, "etsidtmf")) {
1995 cid_type
= IFX_TAPI_CID_STD_ETSI_DTMF
;
1996 } else if (!strcasecmp(v
->value
, "sin")) {
1997 cid_type
= IFX_TAPI_CID_STD_SIN
;
1998 } else if (!strcasecmp(v
->value
, "ntt")) {
1999 cid_type
= IFX_TAPI_CID_STD_NTT
;
2000 } else if (!strcasecmp(v
->value
, "kpndtmf")) {
2001 cid_type
= IFX_TAPI_CID_STD_KPN_DTMF
;
2002 } else if (!strcasecmp(v
->value
, "kpndtmffsk")) {
2003 cid_type
= IFX_TAPI_CID_STD_KPN_DTMF_FSK
;
2005 ast_log(LOG_ERROR
, "Unknown caller id type '%s'\n", v
->value
);
2008 } else if (!strcasecmp(v
->name
, "voiceactivitydetection")) {
2009 if (!strcasecmp(v
->value
, "on")) {
2010 vad_type
= IFX_TAPI_ENC_VAD_ON
;
2011 } else if (!strcasecmp(v
->value
, "g711")) {
2012 vad_type
= IFX_TAPI_ENC_VAD_G711
;
2013 } else if (!strcasecmp(v
->value
, "cng")) {
2014 vad_type
= IFX_TAPI_ENC_VAD_CNG_ONLY
;
2015 } else if (!strcasecmp(v
->value
, "sc")) {
2016 vad_type
= IFX_TAPI_ENC_VAD_SC_ONLY
;
2018 ast_log(LOG_ERROR
, "Unknown voice activity detection value '%s'\n", v
->value
);
2021 } else if (!strcasecmp(v
->name
, "interdigit")) {
2022 dev_ctx
.interdigit_timeout
= atoi(v
->value
);
2023 ast_log(LOG_DEBUG
, "Setting interdigit timeout to %s.\n", v
->value
);
2024 if (!dev_ctx
.interdigit_timeout
) {
2025 dev_ctx
.interdigit_timeout
= DEFAULT_INTERDIGIT_TIMEOUT
;
2026 ast_log(LOG_WARNING
, "Invalid interdigit timeout: %s, using default.\n", v
->value
);
2028 } else if (!strcasecmp(v
->name
, "tone_generator")) {
2029 if (!strcasecmp(v
->value
, "integrated")) {
2030 tone_generator
= TONE_INTEGRATED
;
2031 } else if (!strcasecmp(v
->value
, "asterisk")) {
2032 tone_generator
= TONE_ASTERISK
;
2033 } else if (!strcasecmp(v
->value
, "media")) {
2034 tone_generator
= TONE_MEDIA
;
2036 ast_log(LOG_ERROR
, "Unknown tone_generator value '%s'. Try 'integrated', 'asterisk' or 'media'.\n", v
->value
);
2042 lantiq_create_pvts();
2044 ast_mutex_unlock(&iflock
);
2045 ast_config_destroy(cfg
);
2047 if (!(sched
= ast_sched_context_create())) {
2048 ast_log(LOG_ERROR
, "Unable to create scheduler context\n");
2052 if (ast_sched_start_thread(sched
)) {
2053 ast_log(LOG_ERROR
, "Unable to create scheduler context thread\n");
2057 if (ast_channel_register(&lantiq_tech
)) {
2058 ast_log(LOG_ERROR
, "Unable to register channel class 'Phone'\n");
2063 IFX_TAPI_TONE_t tone
;
2064 IFX_TAPI_DEV_START_CFG_t dev_start
;
2065 IFX_TAPI_MAP_DATA_t map_data
;
2066 IFX_TAPI_LINE_TYPE_CFG_t line_type
;
2067 IFX_TAPI_LINE_VOLUME_t line_vol
;
2068 IFX_TAPI_WLEC_CFG_t wlec_cfg
;
2069 IFX_TAPI_JB_CFG_t jb_cfg
;
2070 IFX_TAPI_CID_CFG_t cid_cfg
;
2073 dev_ctx
.dev_fd
= lantiq_dev_open(base_path
, 0);
2075 if (dev_ctx
.dev_fd
< 0) {
2076 ast_log(LOG_ERROR
, "lantiq TAPI device open function failed\n");
2080 snprintf(dev_ctx
.voip_led
, LED_NAME_LENGTH
, "voice");
2081 for (c
= 0; c
< dev_ctx
.channels
; c
++) {
2082 dev_ctx
.ch_fd
[c
] = lantiq_dev_open(base_path
, c
+ 1);
2084 if (dev_ctx
.ch_fd
[c
] < 0) {
2085 ast_log(LOG_ERROR
, "lantiq TAPI channel %d open function failed\n", c
);
2088 snprintf(dev_ctx
.ch_led
[c
], LED_NAME_LENGTH
, "fxs%d", c
+ 1);
2091 if (lantiq_dev_firmware_download(dev_ctx
.dev_fd
, firmware_filename
)) {
2092 ast_log(LOG_ERROR
, "voice firmware download failed\n");
2096 if (ioctl(dev_ctx
.dev_fd
, IFX_TAPI_DEV_STOP
, 0)) {
2097 ast_log(LOG_ERROR
, "IFX_TAPI_DEV_STOP ioctl failed\n");
2101 memset(&dev_start
, 0x0, sizeof(IFX_TAPI_DEV_START_CFG_t
));
2102 dev_start
.nMode
= IFX_TAPI_INIT_MODE_VOICE_CODER
;
2105 if (ioctl(dev_ctx
.dev_fd
, IFX_TAPI_DEV_START
, &dev_start
)) {
2106 ast_log(LOG_ERROR
, "IFX_TAPI_DEV_START ioctl failed\n");
2110 tz
= ast_get_indication_zone(NULL
);
2113 ast_log(LOG_ERROR
, "Unable to alloc tone zone\n");
2117 ts_dial
= ast_get_indication_tone(tz
, "dial");
2118 ts_ring
= ast_get_indication_tone(tz
, "ring");
2119 ts_busy
= ast_get_indication_tone(tz
, "busy");
2120 ts_congestion
= ast_get_indication_tone(tz
, "congestion");
2122 if (!ts_dial
|| !ts_dial
->data
|| !ts_ring
|| !ts_ring
->data
|| !ts_busy
|| !ts_busy
->data
|| !ts_congestion
|| !ts_congestion
->data
) {
2123 ast_log(LOG_ERROR
, "Unable to get indication tones\n");
2127 for (c
= 0; c
< dev_ctx
.channels
; c
++) {
2128 /* We're a FXS and want to switch between narrow & wide band automatically */
2129 memset(&line_type
, 0, sizeof(IFX_TAPI_LINE_TYPE_CFG_t
));
2130 line_type
.lineType
= IFX_TAPI_LINE_TYPE_FXS_AUTO
;
2131 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_LINE_TYPE_SET
, &line_type
)) {
2132 ast_log(LOG_ERROR
, "IFX_TAPI_LINE_TYPE_SET %d failed\n", c
);
2137 memset(&tone
, 0, sizeof(IFX_TAPI_TONE_t
));
2138 tone
.simple
.format
= IFX_TAPI_TONE_TYPE_SIMPLE
;
2139 playlist_to_tapitone(ts_dial
->data
, TAPI_TONE_LOCALE_DIAL_CODE
, &tone
);
2140 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_TONE_TABLE_CFG_SET
, &tone
)) {
2141 ast_log(LOG_ERROR
, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c
);
2145 memset(&tone
, 0, sizeof(IFX_TAPI_TONE_t
));
2146 tone
.simple
.format
= IFX_TAPI_TONE_TYPE_SIMPLE
;
2147 playlist_to_tapitone(ts_ring
->data
, TAPI_TONE_LOCALE_RINGING_CODE
, &tone
);
2148 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_TONE_TABLE_CFG_SET
, &tone
)) {
2149 ast_log(LOG_ERROR
, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c
);
2153 memset(&tone
, 0, sizeof(IFX_TAPI_TONE_t
));
2154 tone
.simple
.format
= IFX_TAPI_TONE_TYPE_SIMPLE
;
2155 playlist_to_tapitone(ts_busy
->data
, TAPI_TONE_LOCALE_BUSY_CODE
, &tone
);
2156 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_TONE_TABLE_CFG_SET
, &tone
)) {
2157 ast_log(LOG_ERROR
, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c
);
2161 memset(&tone
, 0, sizeof(IFX_TAPI_TONE_t
));
2162 tone
.simple
.format
= IFX_TAPI_TONE_TYPE_SIMPLE
;
2163 playlist_to_tapitone(ts_congestion
->data
, TAPI_TONE_LOCALE_CONGESTION_CODE
, &tone
);
2164 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_TONE_TABLE_CFG_SET
, &tone
)) {
2165 ast_log(LOG_ERROR
, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c
);
2170 IFX_TAPI_RING_CFG_t ringingType
;
2171 memset(&ringingType
, 0, sizeof(IFX_TAPI_RING_CFG_t
));
2172 ringingType
.nMode
= IFX_TAPI_RING_CFG_MODE_INTERNAL_BALANCED
;
2173 ringingType
.nSubmode
= IFX_TAPI_RING_CFG_SUBMODE_DC_RNG_TRIP_FAST
;
2174 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_RING_CFG_SET
, (IFX_int32_t
) &ringingType
)) {
2175 ast_log(LOG_ERROR
, "IFX_TAPI_RING_CFG_SET failed\n");
2180 IFX_char_t data
[15] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2181 0x00, 0x00, 0x00, 0x00, 0x00,
2182 0x00, 0x00, 0x00, 0x00, 0x00 };
2184 IFX_TAPI_RING_CADENCE_t ringCadence
;
2185 memset(&ringCadence
, 0, sizeof(IFX_TAPI_RING_CADENCE_t
));
2186 memcpy(&ringCadence
.data
, data
, sizeof(data
));
2187 ringCadence
.nr
= sizeof(data
) * 8;
2189 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_RING_CADENCE_HR_SET
, &ringCadence
)) {
2190 ast_log(LOG_ERROR
, "IFX_TAPI_RING_CADENCE_HR_SET failed\n");
2194 /* perform mapping */
2195 memset(&map_data
, 0x0, sizeof(IFX_TAPI_MAP_DATA_t
));
2196 map_data
.nDstCh
= c
;
2197 map_data
.nChType
= IFX_TAPI_MAP_TYPE_PHONE
;
2199 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_MAP_DATA_ADD
, &map_data
)) {
2200 ast_log(LOG_ERROR
, "IFX_TAPI_MAP_DATA_ADD %d failed\n", c
);
2205 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_LINE_FEED_SET
, IFX_TAPI_LINE_FEED_STANDBY
)) {
2206 ast_log(LOG_ERROR
, "IFX_TAPI_LINE_FEED_SET %d failed\n", c
);
2211 memset(&line_vol
, 0, sizeof(line_vol
));
2212 line_vol
.nGainRx
= rxgain
;
2213 line_vol
.nGainTx
= txgain
;
2215 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_PHONE_VOLUME_SET
, &line_vol
)) {
2216 ast_log(LOG_ERROR
, "IFX_TAPI_PHONE_VOLUME_SET %d failed\n", c
);
2220 /* Configure line echo canceller */
2221 memset(&wlec_cfg
, 0, sizeof(wlec_cfg
));
2222 wlec_cfg
.nType
= wlec_type
;
2223 wlec_cfg
.bNlp
= wlec_nlp
;
2224 wlec_cfg
.nNBFEwindow
= wlec_nbfe
;
2225 wlec_cfg
.nNBNEwindow
= wlec_nbne
;
2226 wlec_cfg
.nWBNEwindow
= wlec_wbne
;
2228 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_WLEC_PHONE_CFG_SET
, &wlec_cfg
)) {
2229 ast_log(LOG_ERROR
, "IFX_TAPI_WLEC_PHONE_CFG_SET %d failed\n", c
);
2233 /* Configure jitter buffer */
2234 memset(&jb_cfg
, 0, sizeof(jb_cfg
));
2235 jb_cfg
.nJbType
= jb_type
;
2236 jb_cfg
.nPckAdpt
= jb_pckadpt
;
2237 jb_cfg
.nLocalAdpt
= jb_localadpt
;
2238 jb_cfg
.nScaling
= jb_scaling
;
2239 jb_cfg
.nInitialSize
= jb_initialsize
;
2240 jb_cfg
.nMinSize
= jb_minsize
;
2241 jb_cfg
.nMaxSize
= jb_maxsize
;
2243 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_JB_CFG_SET
, &jb_cfg
)) {
2244 ast_log(LOG_ERROR
, "IFX_TAPI_JB_CFG_SET %d failed\n", c
);
2248 /* Configure Caller ID type */
2249 memset(&cid_cfg
, 0, sizeof(cid_cfg
));
2250 cid_cfg
.nStandard
= cid_type
;
2252 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_CID_CFG_SET
, &cid_cfg
)) {
2253 ast_log(LOG_ERROR
, "IIFX_TAPI_CID_CFG_SET %d failed\n", c
);
2257 /* Configure voice activity detection */
2258 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_ENC_VAD_CFG_SET
, vad_type
)) {
2259 ast_log(LOG_ERROR
, "IFX_TAPI_ENC_VAD_CFG_SET %d failed\n", c
);
2263 /* Setup TAPI <-> internal RTP codec type mapping */
2264 if (lantiq_setup_rtp(c
)) {
2268 /* Set initial hook status */
2269 iflist
[c
].channel_state
= lantiq_get_hookstatus(c
);
2271 if (iflist
[c
].channel_state
== UNKNOWN
) {
2276 /* make sure our device will be closed properly */
2277 ast_register_atexit(lantiq_cleanup
);
2280 led_on(dev_ctx
.voip_led
);
2281 return AST_MODULE_LOAD_SUCCESS
;
2284 ast_mutex_unlock(&iflock
);
2286 ast_config_destroy(cfg
);
2287 return AST_MODULE_LOAD_DECLINE
;
2290 ast_sched_context_destroy(sched
);
2294 return AST_MODULE_LOAD_FAILURE
;
2297 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_LOAD_ORDER
, "Lantiq TAPI Telephony API Support",
2298 .load
= load_module
,
2299 .unload
= unload_module
,
2300 .load_pri
= AST_MODPRI_CHANNEL_DRIVER