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>
81 /* Lantiq TAPI includes */
82 #include <drv_tapi/drv_tapi_io.h>
83 #include <drv_vmmc/vmmc_io.h>
85 #define TAPI_AUDIO_PORT_NUM_MAX 2
87 /* Tapi predefined tones 0 to 31 */
88 #define TAPI_TONE_LOCALE_NONE 0
89 //#define TAPI_TONE_LOCALE_DIAL_CODE 25
90 //#define TAPI_TONE_LOCALE_RINGING_CODE 26
91 //#define TAPI_TONE_LOCALE_BUSY_CODE 27
92 //#define TAPI_TONE_LOCALE_CONGESTION_CODE 27
94 /* Tapi custom tones 32 to 256 */
95 #define TAPI_TONE_LOCALE_DIAL_CODE 32
96 #define TAPI_TONE_LOCALE_RINGING_CODE 33
97 #define TAPI_TONE_LOCALE_BUSY_CODE 34
98 #define TAPI_TONE_LOCALE_CONGESTION_CODE 35
100 #define LANTIQ_CONTEXT_PREFIX "lantiq"
101 #define DEFAULT_INTERDIGIT_TIMEOUT 4000
102 #define G723_HIGH_RATE 1
103 #define LED_NAME_LENGTH 32
105 static const char config
[] = "lantiq.conf";
107 static char firmware_filename
[PATH_MAX
] = "/lib/firmware/ifx_firmware.bin";
108 static char bbd_filename
[PATH_MAX
] = "/lib/firmware/ifx_bbd_fxs.bin";
109 static char base_path
[PATH_MAX
] = "/dev/vmmc";
110 static int per_channel_context
= 0;
112 /* tone generator types */
113 enum tone_generator_t
{
114 TONE_INTEGRATED
, /* tapi tone generator */
115 TONE_ASTERISK
, /* asterisk tone generator where possible */
116 TONE_MEDIA
/* media tone where possible */
119 /* tone generator (default is integraded) */
120 static enum tone_generator_t tone_generator
= TONE_INTEGRATED
;
122 /* tone zones for dial, ring, busy and congestion */
123 struct ast_tone_zone_sound
*ts_dial
;
124 struct ast_tone_zone_sound
*ts_ring
;
125 struct ast_tone_zone_sound
*ts_busy
;
126 struct ast_tone_zone_sound
*ts_congestion
;
129 * The private structures of the Phone Jack channels are linked for selecting
142 static struct lantiq_pvt
{
143 struct ast_channel
*owner
; /* Channel we belong to, possibly NULL */
144 int port_id
; /* Port number of this object, 0..n */
146 char context
[AST_MAX_CONTEXT
]; /* this port's dialplan context */
147 int dial_timer
; /* timer handle for autodial timeout */
148 char dtmfbuf
[AST_MAX_EXTENSION
]; /* buffer holding dialed digits */
149 int dtmfbuf_len
; /* lenght of dtmfbuf */
150 int rtp_timestamp
; /* timestamp for RTP packets */
151 int ptime
; /* Codec base ptime */
152 uint16_t rtp_seqno
; /* Sequence nr for RTP packets */
153 uint32_t call_setup_start
; /* Start of dialling in ms */
154 uint32_t call_setup_delay
; /* time between ^ and 1st ring in ms */
155 uint32_t call_start
; /* time we started dialling / answered */
156 uint32_t call_answer
; /* time the callee answered our call */
157 uint16_t jb_size
; /* Jitter buffer size */
158 uint32_t jb_underflow
; /* Jitter buffer injected samples */
159 uint32_t jb_overflow
; /* Jitter buffer dropped samples */
160 uint16_t jb_delay
; /* Jitter buffer: playout delay */
161 uint16_t jb_invalid
; /* Jitter buffer: Nr. of invalid packets */
164 static struct lantiq_ctx
{
167 int ch_fd
[TAPI_AUDIO_PORT_NUM_MAX
];
168 char voip_led
[LED_NAME_LENGTH
]; /* VOIP LED name */
169 char ch_led
[TAPI_AUDIO_PORT_NUM_MAX
][LED_NAME_LENGTH
]; /* FXS LED names */
170 int interdigit_timeout
; /* Timeout in ms between dialed digits */
173 static int ast_digit_begin(struct ast_channel
*ast
, char digit
);
174 static int ast_digit_end(struct ast_channel
*ast
, char digit
, unsigned int duration
);
175 static int ast_lantiq_call(struct ast_channel
*ast
, const char *dest
, int timeout
);
176 static int ast_lantiq_hangup(struct ast_channel
*ast
);
177 static int ast_lantiq_answer(struct ast_channel
*ast
);
178 static struct ast_frame
*ast_lantiq_read(struct ast_channel
*ast
);
179 static int ast_lantiq_write(struct ast_channel
*ast
, struct ast_frame
*frame
);
180 static struct ast_frame
*ast_lantiq_exception(struct ast_channel
*ast
);
181 static int ast_lantiq_indicate(struct ast_channel
*chan
, int condition
, const void *data
, size_t datalen
);
182 static int ast_lantiq_fixup(struct ast_channel
*old
, struct ast_channel
*new);
183 static struct ast_channel
*ast_lantiq_requester(const char *type
, struct ast_format_cap
*cap
, const struct ast_channel
*requestor
, const char *data
, int *cause
);
184 static int ast_lantiq_devicestate(const char *data
);
185 static int acf_channel_read(struct ast_channel
*chan
, const char *funcname
, char *args
, char *buf
, size_t buflen
);
186 static void lantiq_jb_get_stats(int c
);
187 static struct ast_format
lantiq_map_rtptype_to_format(uint8_t rtptype
);
188 static uint8_t lantiq_map_format_to_rtptype(const struct ast_format
*format
);
189 static int lantiq_conf_enc(int c
, const struct ast_format
*format
);
190 static void lantiq_reset_dtmfbuf(struct lantiq_pvt
*pvt
);
192 static struct ast_channel_tech lantiq_tech
= {
194 .description
= "Lantiq TAPI Telephony API Driver",
195 .send_digit_begin
= ast_digit_begin
,
196 .send_digit_end
= ast_digit_end
,
197 .call
= ast_lantiq_call
,
198 .hangup
= ast_lantiq_hangup
,
199 .answer
= ast_lantiq_answer
,
200 .read
= ast_lantiq_read
,
201 .write
= ast_lantiq_write
,
202 .exception
= ast_lantiq_exception
,
203 .indicate
= ast_lantiq_indicate
,
204 .fixup
= ast_lantiq_fixup
,
205 .requester
= ast_lantiq_requester
,
206 .devicestate
= ast_lantiq_devicestate
,
207 .func_channel_read
= acf_channel_read
210 /* Protect the interface list (of lantiq_pvt's) */
211 AST_MUTEX_DEFINE_STATIC(iflock
);
214 * Protect the monitoring thread, so only one process can kill or start it, and
215 * not when it's doing something critical.
217 AST_MUTEX_DEFINE_STATIC(monlock
);
219 /* The scheduling context */
220 struct ast_sched_context
*sched
;
223 * This is the thread for the monitor which checks for input on the channels
224 * which are not currently in use.
226 static pthread_t monitor_thread
= AST_PTHREADT_NULL
;
229 #define WORDS_BIGENDIAN
230 /* struct taken from some GPLed code by Mike Borella */
231 typedef struct rtp_header
233 #if defined(WORDS_BIGENDIAN)
234 uint8_t version
:2, padding
:1, extension
:1, csrc_count
:4;
235 uint8_t marker
:1, payload_type
:7;
237 uint8_t csrc_count
:4, extension
:1, padding
:1, version
:2;
238 uint8_t payload_type
:7, marker
:1;
244 #define RTP_HEADER_LEN 12
245 #define RTP_BUFFER_LEN 512
246 /* Internal RTP payload types - standard */
248 #define RTP_G723_63 4
253 /* Internal RTP payload types - custom */
254 #define RTP_G7221 100
257 #define RTP_SLIN8 103
258 #define RTP_SLIN16 104
259 #define RTP_SIREN7 105
260 #define RTP_G723_53 106
263 /* LED Control. Taken with modifications from SVD by Luca Olivetti <olivluca@gmail.com> */
264 #define LED_SLOW_BLINK 1000
265 #define LED_FAST_BLINK 100
266 static FILE *led_open(const char *led
, char* sub
)
270 if (snprintf(fname
, sizeof(fname
), "/sys/class/leds/%s/%s", led
, sub
) >= sizeof(fname
))
272 return fopen(fname
, "r+");
275 static FILE *led_trigger(const char *led
)
277 return led_open(led
, "trigger");
280 static void led_delay(const char *led
, int onoff
, int msec
)
282 FILE *fp
= led_open(led
, onoff
? "delay_on" : "delay_off");
284 fprintf(fp
,"%d\n",msec
);
289 static void led_on(const char *led
)
293 fp
= led_trigger(led
);
295 fprintf(fp
,"default-on\n");
300 static void led_off(const char *led
)
304 fp
= led_trigger(led
);
306 fprintf(fp
,"none\n");
311 static void led_blink(const char *led
, int period
)
315 fp
= led_trigger(led
);
317 fprintf(fp
, "timer\n");
319 led_delay(led
, 1, period
/2);
320 led_delay(led
, 0, period
/2);
324 static uint32_t now(void) {
326 clock_gettime(CLOCK_MONOTONIC
, &ts
);
328 uint64_t tmp
= ts
.tv_sec
*1000 + (ts
.tv_nsec
/1000000);
329 return (uint32_t) tmp
;
332 static uint32_t epoch(void) {
334 gettimeofday(&tv
, NULL
);
339 static int lantiq_dev_open(const char *dev_path
, const int32_t ch_num
)
341 char dev_name
[PATH_MAX
];
342 memset(dev_name
, 0, sizeof(dev_name
));
343 snprintf(dev_name
, PATH_MAX
, "%s%u%u", dev_path
, 1, ch_num
);
344 return open((const char*)dev_name
, O_RDWR
, 0644);
347 static void lantiq_ring(int c
, int r
, const char *cid
, const char *name
)
352 led_blink(dev_ctx
.ch_led
[c
], LED_FAST_BLINK
);
354 status
= (uint8_t) ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_RING_START
, 0);
356 IFX_TAPI_CID_MSG_t msg
;
357 IFX_TAPI_CID_MSG_ELEMENT_t elements
[3];
362 elements
[count
].string
.elementType
= IFX_TAPI_CID_ST_CLI
;
363 elements
[count
].string
.len
= strlen(cid
);
364 if (elements
[count
].string
.len
> IFX_TAPI_CID_MSG_LEN_MAX
) {
365 elements
[count
].string
.len
= IFX_TAPI_CID_MSG_LEN_MAX
;
367 strncpy((char *)elements
[count
].string
.element
, cid
, IFX_TAPI_CID_MSG_LEN_MAX
);
368 elements
[count
].string
.element
[IFX_TAPI_CID_MSG_LEN_MAX
-1] = '\0';
372 elements
[count
].string
.elementType
= IFX_TAPI_CID_ST_NAME
;
373 elements
[count
].string
.len
= strlen(name
);
374 if (elements
[count
].string
.len
> IFX_TAPI_CID_MSG_LEN_MAX
) {
375 elements
[count
].string
.len
= IFX_TAPI_CID_MSG_LEN_MAX
;
377 strncpy((char *)elements
[count
].string
.element
, name
, IFX_TAPI_CID_MSG_LEN_MAX
);
378 elements
[count
].string
.element
[IFX_TAPI_CID_MSG_LEN_MAX
-1] = '\0';
382 if ((time(×tamp
) != -1) && ((tm
=localtime(×tamp
)) != NULL
)) {
383 elements
[count
].date
.elementType
= IFX_TAPI_CID_ST_DATE
;
384 elements
[count
].date
.day
= tm
->tm_mday
;
385 elements
[count
].date
.month
= tm
->tm_mon
+ 1;
386 elements
[count
].date
.hour
= tm
->tm_hour
;
387 elements
[count
].date
.mn
= tm
->tm_min
;
391 msg
.txMode
= IFX_TAPI_CID_HM_ONHOOK
;
392 msg
.messageType
= IFX_TAPI_CID_MT_CSUP
;
393 msg
.message
= elements
;
394 msg
.nMsgElements
= count
;
396 status
= (uint8_t) ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_CID_TX_SEQ_START
, (IFX_int32_t
) &msg
);
399 status
= (uint8_t) ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_RING_STOP
, 0);
400 led_off(dev_ctx
.ch_led
[c
]);
404 ast_log(LOG_ERROR
, "%s ioctl failed\n",
405 (r
? "IFX_TAPI_RING_START" : "IFX_TAPI_RING_STOP"));
409 /* add a frequency to TAPE tone structure */
410 /* returns the TAPI frequency ID */
411 static int tapitone_add_freq (IFX_TAPI_TONE_t
*tone
, IFX_uint32_t freq
) {
412 const int n
=4; /* TAPI tone structure supports up to 4 frequencies */
417 /* pointer array for freq's A, B, C, D */
418 IFX_uint32_t
*freqarr
[] = { &(tone
->simple
.freqA
), &(tone
->simple
.freqB
), &(tone
->simple
.freqC
), &(tone
->simple
.freqD
) };
420 /* pointer array for level's A, B, C, D */
421 IFX_int32_t
*lvlarr
[] = { &(tone
->simple
.levelA
), &(tone
->simple
.levelB
), &(tone
->simple
.levelC
), &(tone
->simple
.levelD
) };
423 /* array for freq IDs */
424 IFX_uint32_t retarr
[] = { IFX_TAPI_TONE_FREQA
, IFX_TAPI_TONE_FREQB
, IFX_TAPI_TONE_FREQC
, IFX_TAPI_TONE_FREQD
};
426 /* determine if freq already set */
427 for (i
= 0; i
< n
; i
++) {
428 if(*freqarr
[i
] == freq
) /* freq found */
430 else if (i
== n
-1) /* last iteration */
431 error
=1; /* not found */
434 /* write frequency if not already set */
436 error
=0; /* reset error flag */
437 /* since freq is not set, write it into first free place */
438 for (i
= 0; i
< n
; i
++) {
439 if(!*freqarr
[i
]) { /* free place */
440 *freqarr
[i
] = freq
; /* set freq */
441 *lvlarr
[i
] = -150; /* set volume level */
443 } else if (i
== n
-1) /* last iteration */
444 error
=1; /* no free place becaus maximum count of freq's is set */
448 /* set freq ID return value */
450 ret
= IFX_TAPI_TONE_FREQNONE
;
454 return ret
; /* freq ID */
457 /* convert asterisk playlist string to tapi tone structure */
458 /* based on ast_playtones_start() from indications.c of asterisk 13 */
459 static void playlist_to_tapitone (const char *playlst
, IFX_uint32_t index
, IFX_TAPI_TONE_t
*tone
)
461 char *s
, *data
= ast_strdupa(playlst
);
466 /* initialize tapi tone structure */
467 memset(tone
, 0, sizeof(IFX_TAPI_TONE_t
));
468 tone
->simple
.format
= IFX_TAPI_TONE_TYPE_SIMPLE
;
469 tone
->simple
.index
= index
;
473 /* check if the data is separated with '|' or with ',' by default */
474 if (strchr(stringp
,'|')) {
480 for ( i
= 0; (s
= strsep(&stringp
, separator
)) && !ast_strlen_zero(s
) && i
< IFX_TAPI_TONE_STEPS_MAX
; i
++) {
481 struct ast_tone_zone_part tone_data
= {
490 if (ast_tone_zone_part_parse(s
, &tone_data
)) {
491 ast_log(LOG_ERROR
, "Failed to parse tone part '%s'\n", s
);
495 /* first tone must hava a cadence */
496 if (i
==0 && !tone_data
.time
)
497 tone
->simple
.cadence
[i
] = 1000;
499 tone
->simple
.cadence
[i
] = tone_data
.time
;
501 /* check for modulation */
502 if (tone_data
.modulate
) {
503 tone
->simple
.modulation
[i
] = IFX_TAPI_TONE_MODULATION_ON
;
504 tone
->simple
.modulation_factor
= IFX_TAPI_TONE_MODULATION_FACTOR_90
;
507 /* copy freq's to tapi tone structure */
508 /* a freq will implicitly skipped if it is zero */
509 tone
->simple
.frequencies
[i
] |= tapitone_add_freq(tone
, tone_data
.freq1
);
510 tone
->simple
.frequencies
[i
] |= tapitone_add_freq(tone
, tone_data
.freq2
);
514 static int lantiq_play_tone(int c
, int t
)
516 /* stop currently playing tone before starting new one */
517 ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_TONE_LOCAL_PLAY
, TAPI_TONE_LOCALE_NONE
);
519 /* do not handle stop tone twice */
520 if (t
!= TAPI_TONE_LOCALE_NONE
) {
522 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_TONE_LOCAL_PLAY
, t
)) {
523 ast_log(LOG_ERROR
, "IFX_TAPI_TONE_LOCAL_PLAY ioctl failed\n");
531 static enum channel_state
lantiq_get_hookstatus(int port
)
535 if (ioctl(dev_ctx
.ch_fd
[port
], IFX_TAPI_LINE_HOOK_STATUS_GET
, &status
)) {
536 ast_log(LOG_ERROR
, "IFX_TAPI_LINE_HOOK_STATUS_GET ioctl failed\n");
548 lantiq_dev_binary_buffer_create(const char *path
, uint8_t **ppBuf
, uint32_t *pBufSz
)
551 struct stat file_stat
;
554 fd
= fopen(path
, "rb");
556 ast_log(LOG_ERROR
, "binary file %s open failed\n", path
);
560 if (stat(path
, &file_stat
)) {
561 ast_log(LOG_ERROR
, "file %s statistics get failed\n", path
);
565 *ppBuf
= malloc(file_stat
.st_size
);
566 if (*ppBuf
== NULL
) {
567 ast_log(LOG_ERROR
, "binary file %s memory allocation failed\n", path
);
571 if (fread (*ppBuf
, sizeof(uint8_t), file_stat
.st_size
, fd
) != file_stat
.st_size
) {
572 ast_log(LOG_ERROR
, "file %s read failed\n", path
);
577 *pBufSz
= file_stat
.st_size
;
584 if (*ppBuf
!= NULL
&& status
)
590 static int32_t lantiq_dev_firmware_download(int32_t fd
, const char *path
)
592 uint8_t *firmware
= NULL
;
594 VMMC_IO_INIT vmmc_io_init
;
596 ast_log(LOG_DEBUG
, "loading firmware: \"%s\".\n", path
);
598 if (lantiq_dev_binary_buffer_create(path
, &firmware
, &size
))
601 memset(&vmmc_io_init
, 0, sizeof(VMMC_IO_INIT
));
602 vmmc_io_init
.pPRAMfw
= firmware
;
603 vmmc_io_init
.pram_size
= size
;
605 if (ioctl(fd
, FIO_FW_DOWNLOAD
, &vmmc_io_init
)) {
606 ast_log(LOG_ERROR
, "FIO_FW_DOWNLOAD ioctl failed\n");
610 if (firmware
!= NULL
)
616 static const char *state_string(enum channel_state s
)
619 case ONHOOK
: return "ONHOOK";
620 case OFFHOOK
: return "OFFHOOK";
621 case DIALING
: return "DIALING";
622 case INCALL
: return "INCALL";
623 case CALL_ENDED
: return "CALL_ENDED";
624 case RINGING
: return "RINGING";
625 default: return "UNKNOWN";
629 static const char *control_string(int c
)
632 case AST_CONTROL_HANGUP
: return "Other end has hungup";
633 case AST_CONTROL_RING
: return "Local ring";
634 case AST_CONTROL_RINGING
: return "Remote end is ringing";
635 case AST_CONTROL_ANSWER
: return "Remote end has answered";
636 case AST_CONTROL_BUSY
: return "Remote end is busy";
637 case AST_CONTROL_TAKEOFFHOOK
: return "Make it go off hook";
638 case AST_CONTROL_OFFHOOK
: return "Line is off hook";
639 case AST_CONTROL_CONGESTION
: return "Congestion (circuits busy)";
640 case AST_CONTROL_FLASH
: return "Flash hook";
641 case AST_CONTROL_WINK
: return "Wink";
642 case AST_CONTROL_OPTION
: return "Set a low-level option";
643 case AST_CONTROL_RADIO_KEY
: return "Key Radio";
644 case AST_CONTROL_RADIO_UNKEY
: return "Un-Key Radio";
645 case AST_CONTROL_PROGRESS
: return "Remote end is making Progress";
646 case AST_CONTROL_PROCEEDING
: return "Remote end is proceeding";
647 case AST_CONTROL_HOLD
: return "Hold";
648 case AST_CONTROL_UNHOLD
: return "Unhold";
649 case AST_CONTROL_SRCUPDATE
: return "Media Source Update";
650 case AST_CONTROL_CONNECTED_LINE
: return "Connected Line";
651 case AST_CONTROL_REDIRECTING
: return "Redirecting";
652 case AST_CONTROL_INCOMPLETE
: return "Incomplete";
653 case -1: return "Stop tone";
654 default: return "Unknown";
658 static int ast_lantiq_indicate(struct ast_channel
*chan
, int condition
, const void *data
, size_t datalen
)
660 struct lantiq_pvt
*pvt
= ast_channel_tech_pvt(chan
);
662 ast_verb(3, "phone indication \"%s\"\n", control_string(condition
));
667 lantiq_play_tone(pvt
->port_id
, TAPI_TONE_LOCALE_NONE
);
670 case AST_CONTROL_CONGESTION
:
672 if (tone_generator
== TONE_INTEGRATED
)
673 lantiq_play_tone(pvt
->port_id
, TAPI_TONE_LOCALE_CONGESTION_CODE
);
675 ast_playtones_start(chan
, 0, ts_congestion
->data
, 1);
679 case AST_CONTROL_BUSY
:
681 if (tone_generator
== TONE_INTEGRATED
)
682 lantiq_play_tone(pvt
->port_id
, TAPI_TONE_LOCALE_BUSY_CODE
);
684 ast_playtones_start(chan
, 0, ts_busy
->data
, 1);
688 case AST_CONTROL_RINGING
:
689 case AST_CONTROL_PROGRESS
:
691 pvt
->call_setup_delay
= now() - pvt
->call_setup_start
;
693 if (tone_generator
== TONE_INTEGRATED
)
694 lantiq_play_tone(pvt
->port_id
, TAPI_TONE_LOCALE_RINGING_CODE
);
695 else if (tone_generator
== TONE_ASTERISK
) /* do nothing if TONE_MEDIA is set */
696 ast_playtones_start(chan
, 0, ts_ring
->data
, 1);
702 /* -1 lets asterisk generate the tone */
708 static int ast_lantiq_fixup(struct ast_channel
*old
, struct ast_channel
*new)
710 ast_log(LOG_DEBUG
, "entering... no code here...\n");
714 static int ast_digit_begin(struct ast_channel
*chan
, char digit
)
716 /* TODO: Modify this callback to let Asterisk support controlling the length of DTMF */
717 ast_log(LOG_DEBUG
, "entering... no code here...\n");
721 static int ast_digit_end(struct ast_channel
*ast
, char digit
, unsigned int duration
)
723 ast_log(LOG_DEBUG
, "entering... no code here...\n");
727 static int ast_lantiq_call(struct ast_channel
*ast
, const char *dest
, int timeout
)
730 struct lantiq_pvt
*pvt
;
732 /* lock to prevent simultaneous access with do_monitor thread processing */
733 ast_mutex_lock(&iflock
);
735 pvt
= ast_channel_tech_pvt(ast
);
736 ast_log(LOG_DEBUG
, "state: %s\n", state_string(pvt
->channel_state
));
738 if (pvt
->channel_state
== ONHOOK
) {
739 struct ast_party_id connected_id
= ast_channel_connected_effective_id(ast
);
740 const char *cid
= connected_id
.number
.valid
? connected_id
.number
.str
: NULL
;
741 const char *name
= connected_id
.name
.valid
? connected_id
.name
.str
: NULL
;
743 ast_log(LOG_DEBUG
, "port %i is ringing\n", pvt
->port_id
);
744 ast_log(LOG_DEBUG
, "port %i CID: %s\n", pvt
->port_id
, cid
? cid
: "none");
745 ast_log(LOG_DEBUG
, "port %i NAME: %s\n", pvt
->port_id
, name
? name
: "none");
747 lantiq_ring(pvt
->port_id
, 1, cid
, name
);
748 pvt
->channel_state
= RINGING
;
750 ast_setstate(ast
, AST_STATE_RINGING
);
751 ast_queue_control(ast
, AST_CONTROL_RINGING
);
753 ast_log(LOG_DEBUG
, "port %i is busy\n", pvt
->port_id
);
754 ast_setstate(ast
, AST_STATE_BUSY
);
755 ast_queue_control(ast
, AST_CONTROL_BUSY
);
759 ast_mutex_unlock(&iflock
);
764 static int ast_lantiq_hangup(struct ast_channel
*ast
)
766 struct lantiq_pvt
*pvt
;
768 /* lock to prevent simultaneous access with do_monitor thread processing */
769 ast_mutex_lock(&iflock
);
771 pvt
= ast_channel_tech_pvt(ast
);
772 ast_log(LOG_DEBUG
, "state: %s\n", state_string(pvt
->channel_state
));
774 if (ast_channel_state(ast
) == AST_STATE_RINGING
) {
775 ast_debug(1, "channel state is RINGING\n");
778 switch (pvt
->channel_state
) {
781 lantiq_ring(pvt
->port_id
, 0, NULL
, NULL
);
782 pvt
->channel_state
= ONHOOK
;
785 ast_log(LOG_DEBUG
, "we were hung up, play busy tone\n");
786 pvt
->channel_state
= CALL_ENDED
;
787 lantiq_play_tone(pvt
->port_id
, TAPI_TONE_LOCALE_BUSY_CODE
);
790 lantiq_jb_get_stats(pvt
->port_id
);
792 ast_setstate(ast
, AST_STATE_DOWN
);
793 ast_module_unref(ast_module_info
->self
);
794 ast_channel_tech_pvt_set(ast
, NULL
);
797 ast_mutex_unlock(&iflock
);
802 static int ast_lantiq_answer(struct ast_channel
*ast
)
804 struct lantiq_pvt
*pvt
= ast_channel_tech_pvt(ast
);
806 ast_log(LOG_DEBUG
, "Remote end has answered call.\n");
808 if (lantiq_conf_enc(pvt
->port_id
, ast_channel_writeformat(ast
)))
811 pvt
->call_answer
= epoch();
816 static struct ast_frame
* ast_lantiq_read(struct ast_channel
*ast
)
818 ast_log(LOG_DEBUG
, "entering... no code here...\n");
822 /* create asterisk format from rtp payload type */
823 static struct ast_format
lantiq_map_rtptype_to_format(uint8_t rtptype
)
825 struct ast_format format
= {0};
828 case RTP_PCMU
: ast_format_set(&format
, AST_FORMAT_ULAW
, 0); break;
829 case RTP_PCMA
: ast_format_set(&format
, AST_FORMAT_ALAW
, 0); break;
830 case RTP_G722
: ast_format_set(&format
, AST_FORMAT_G722
, 0); break;
831 case RTP_G726
: ast_format_set(&format
, AST_FORMAT_G726
, 0); break;
832 case RTP_SLIN8
: ast_format_set(&format
, AST_FORMAT_SLINEAR
, 0); break;
833 case RTP_SLIN16
: ast_format_set(&format
, AST_FORMAT_SLINEAR16
, 0); break;
834 case RTP_ILBC
: ast_format_set(&format
, AST_FORMAT_ILBC
, 0); break;
835 case RTP_SIREN7
: ast_format_set(&format
, AST_FORMAT_SIREN7
, 0); break;
836 case RTP_G723_63
: ast_format_set(&format
, AST_FORMAT_G723_1
, 0); break;
837 case RTP_G723_53
: ast_format_set(&format
, AST_FORMAT_G723_1
, 0); break;
838 case RTP_G729
: ast_format_set(&format
, AST_FORMAT_G729A
, 0); break;
841 ast_log(LOG_ERROR
, "unsupported rtptype received is 0x%x, forcing ulaw\n", (unsigned) rtptype
);
842 ast_format_set(&format
, AST_FORMAT_ULAW
, 0);
849 /* create rtp payload type from asterisk format */
850 static uint8_t lantiq_map_format_to_rtptype(const struct ast_format
*format
)
853 enum ast_format_id formatid
= format
? format
->id
: 0;
856 case AST_FORMAT_ULAW
: rtptype
= RTP_PCMU
; break;
857 case AST_FORMAT_ALAW
: rtptype
= RTP_PCMA
; break;
858 case AST_FORMAT_G722
: rtptype
= RTP_G722
; break;
859 case AST_FORMAT_G726
: rtptype
= RTP_G726
; break;
860 case AST_FORMAT_SLINEAR
: rtptype
= RTP_SLIN8
; break;
861 case AST_FORMAT_SLINEAR16
: rtptype
= RTP_SLIN16
; break;
862 case AST_FORMAT_ILBC
: rtptype
= RTP_ILBC
; break;
863 case AST_FORMAT_SIREN7
: rtptype
= RTP_SIREN7
; break;
864 #if defined G723_HIGH_RATE
865 case AST_FORMAT_G723_1
: rtptype
= RTP_G723_63
; break;
867 case AST_FORMAT_G723_1
: rtptype
= RTP_G723_53
; break;
869 case AST_FORMAT_G729A
: rtptype
= RTP_G729
; break;
872 ast_log(LOG_ERROR
, "unsupported format %s (0x%x), forcing ulaw\n", ast_getformatname(format
), (int) formatid
);
880 static int lantiq_conf_enc(int c
, const struct ast_format
*format
)
882 /* Configure encoder before starting RTP session */
883 IFX_TAPI_ENC_CFG_t enc_cfg
;
884 enum ast_format_id formatid
= format
? format
->id
: 0;
886 memset(&enc_cfg
, 0, sizeof(IFX_TAPI_ENC_CFG_t
));
888 case AST_FORMAT_ULAW
:
889 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_MLAW
;
890 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
891 iflist
[c
].ptime
= 10;
893 case AST_FORMAT_ALAW
:
894 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_ALAW
;
895 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
896 iflist
[c
].ptime
= 10;
898 case AST_FORMAT_G722
:
899 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_G722_64
;
900 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
901 iflist
[c
].ptime
= 20;
903 case AST_FORMAT_G726
:
904 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_G726_32
;
905 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
906 iflist
[c
].ptime
= 10;
908 case AST_FORMAT_SLINEAR
:
909 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_LIN16_8
;
910 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
911 iflist
[c
].ptime
= 10;
913 case AST_FORMAT_SLINEAR16
:
914 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_LIN16_16
;
915 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_10
;
916 iflist
[c
].ptime
= 10;
918 case AST_FORMAT_ILBC
:
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;
924 case AST_FORMAT_SIREN7
:
925 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_G7221_32
;
926 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
927 iflist
[c
].ptime
= 20;
929 case AST_FORMAT_G723_1
:
930 #if defined G723_HIGH_RATE
931 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_G723_63
;
933 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_G723_53
;
935 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_30
;
936 iflist
[c
].ptime
= 30;
938 case AST_FORMAT_G729A
:
939 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_G729
;
940 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
941 iflist
[c
].ptime
= 10;
944 ast_log(LOG_ERROR
, "unsupported format %s (0x%x)\n", ast_getformatname(format
), (int) formatid
);
945 enc_cfg
.nEncType
= IFX_TAPI_COD_TYPE_MLAW
;
946 enc_cfg
.nFrameLen
= IFX_TAPI_COD_LENGTH_20
;
947 iflist
[c
].ptime
= 10;
950 ast_log(LOG_DEBUG
, "Configuring encoder to use TAPI codec type %d (%s) on channel %i\n", enc_cfg
.nEncType
, ast_getformatname(format
), c
);
952 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_ENC_CFG_SET
, &enc_cfg
)) {
953 ast_log(LOG_ERROR
, "IFX_TAPI_ENC_CFG_SET %d failed\n", c
);
956 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_ENC_START
, 0)) {
957 ast_log(LOG_ERROR
, "IFX_TAPI_ENC_START ioctl failed\n");
960 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_DEC_START
, 0)) {
961 ast_log(LOG_ERROR
, "IFX_TAPI_DEC_START ioctl failed\n");
967 static int ast_lantiq_write(struct ast_channel
*ast
, struct ast_frame
*frame
)
969 char buf
[RTP_BUFFER_LEN
];
970 rtp_header_t
*rtp_header
= (rtp_header_t
*) buf
;
971 struct lantiq_pvt
*pvt
= ast_channel_tech_pvt(ast
);
974 int subframes
, subframes_rtp
, length
, samples
;
977 if(frame
->frametype
!= AST_FRAME_VOICE
) {
978 ast_log(LOG_DEBUG
, "unhandled frame type\n");
982 if (frame
->datalen
== 0) {
983 ast_log(LOG_DEBUG
, "we've been prodded\n");
987 /* get rtp payload type */
988 rtptype
= lantiq_map_format_to_rtptype(&frame
->subclass
.format
);
990 rtp_header
->version
= 2;
991 rtp_header
->padding
= 0;
992 rtp_header
->extension
= 0;
993 rtp_header
->csrc_count
= 0;
994 rtp_header
->marker
= 0;
995 rtp_header
->ssrc
= 0;
996 rtp_header
->payload_type
= rtptype
;
998 subframes
= (iflist
[pvt
->port_id
].ptime
+ frame
->len
- 1) / iflist
[pvt
->port_id
].ptime
; /* number of subframes in AST frame */
1002 subframes_rtp
= (RTP_BUFFER_LEN
- RTP_HEADER_LEN
) * subframes
/ frame
->datalen
; /* how many frames fit in a single RTP packet */
1004 /* By default stick to the maximum multiple of native frame length */
1005 length
= subframes_rtp
* frame
->datalen
/ subframes
;
1006 samples
= length
* frame
->samples
/ frame
->datalen
;
1008 head
= frame
->data
.ptr
;
1009 tail
= frame
->data
.ptr
+ frame
->datalen
;
1010 while (head
< tail
) {
1011 rtp_header
->seqno
= pvt
->rtp_seqno
++;
1012 rtp_header
->timestamp
= pvt
->rtp_timestamp
;
1014 if ((tail
- head
) < (RTP_BUFFER_LEN
- RTP_HEADER_LEN
)) {
1015 length
= tail
- head
;
1016 samples
= length
* frame
->samples
/ frame
->datalen
;
1019 if ( frame
->datalen
<= (sizeof(buf
) - RTP_HEADER_LEN
) )
1020 memcpy(buf
+ RTP_HEADER_LEN
, head
, length
);
1022 ast_log(LOG_WARNING
, "buffer is too small\n");
1027 pvt
->rtp_timestamp
+= (rtp_header
->payload_type
== RTP_G722
) ? samples
/ 2 : samples
; /* per RFC3551 */
1029 ret
= write(dev_ctx
.ch_fd
[pvt
->port_id
], buf
, RTP_HEADER_LEN
+ length
);
1031 ast_debug(1, "TAPI: ast_lantiq_write(): error writing.\n");
1034 if (ret
!= (RTP_HEADER_LEN
+ length
)) {
1035 ast_log(LOG_WARNING
, "Short TAPI write of %d bytes, expected %d bytes\n", ret
, RTP_HEADER_LEN
+ length
);
1043 static int acf_channel_read(struct ast_channel
*chan
, const char *funcname
, char *args
, char *buf
, size_t buflen
)
1045 struct lantiq_pvt
*pvt
;
1048 if (!chan
|| ast_channel_tech(chan
) != &lantiq_tech
) {
1049 ast_log(LOG_ERROR
, "This function requires a valid Lantiq TAPI channel\n");
1053 ast_mutex_lock(&iflock
);
1055 pvt
= (struct lantiq_pvt
*) ast_channel_tech_pvt(chan
);
1057 if (!strcasecmp(args
, "csd")) {
1058 snprintf(buf
, buflen
, "%lu", (unsigned long int) pvt
->call_setup_delay
);
1059 } else if (!strcasecmp(args
, "jitter_stats")){
1060 lantiq_jb_get_stats(pvt
->port_id
);
1061 snprintf(buf
, buflen
, "jbBufSize=%u,jbUnderflow=%u,jbOverflow=%u,jbDelay=%u,jbInvalid=%u",
1062 (uint32_t) pvt
->jb_size
,
1063 (uint32_t) pvt
->jb_underflow
,
1064 (uint32_t) pvt
->jb_overflow
,
1065 (uint32_t) pvt
->jb_delay
,
1066 (uint32_t) pvt
->jb_invalid
);
1067 } else if (!strcasecmp(args
, "jbBufSize")) {
1068 snprintf(buf
, buflen
, "%u", (uint32_t) pvt
->jb_size
);
1069 } else if (!strcasecmp(args
, "jbUnderflow")) {
1070 snprintf(buf
, buflen
, "%u", (uint32_t) pvt
->jb_underflow
);
1071 } else if (!strcasecmp(args
, "jbOverflow")) {
1072 snprintf(buf
, buflen
, "%u", (uint32_t) pvt
->jb_overflow
);
1073 } else if (!strcasecmp(args
, "jbDelay")) {
1074 snprintf(buf
, buflen
, "%u", (uint32_t) pvt
->jb_delay
);
1075 } else if (!strcasecmp(args
, "jbInvalid")) {
1076 snprintf(buf
, buflen
, "%u", (uint32_t) pvt
->jb_invalid
);
1077 } else if (!strcasecmp(args
, "start")) {
1078 struct tm
*tm
= gmtime((const time_t*)&pvt
->call_start
);
1079 strftime(buf
, buflen
, "%F %T", tm
);
1080 } else if (!strcasecmp(args
, "answer")) {
1081 struct tm
*tm
= gmtime((const time_t*)&pvt
->call_answer
);
1082 strftime(buf
, buflen
, "%F %T", tm
);
1087 ast_mutex_unlock(&iflock
);
1092 static struct ast_frame
* ast_lantiq_exception(struct ast_channel
*ast
)
1094 ast_log(LOG_DEBUG
, "entering... no code here...\n");
1098 static void lantiq_jb_get_stats(int c
) {
1099 struct lantiq_pvt
*pvt
= &iflist
[c
];
1101 IFX_TAPI_JB_STATISTICS_t param
;
1102 memset (¶m
, 0, sizeof (param
));
1103 if (ioctl (dev_ctx
.ch_fd
[c
], IFX_TAPI_JB_STATISTICS_GET
, (IFX_int32_t
) ¶m
) != IFX_SUCCESS
) {
1104 ast_debug(1, "Error getting jitter buffer stats.\n");
1106 #if !defined (TAPI_VERSION3) && defined (TAPI_VERSION4)
1107 ast_debug(1, "Jitter buffer stats: dev=%u, ch=%u, nType=%u, nBufSize=%u, nIsUnderflow=%u, nDsOverflow=%u, nPODelay=%u, nInvalid=%u\n",
1108 (uint32_t) param
.dev
,
1109 (uint32_t) param
.ch
,
1111 ast_debug(1, "Jitter buffer stats: nType=%u, nBufSize=%u, nIsUnderflow=%u, nDsOverflow=%u, nPODelay=%u, nInvalid=%u\n",
1113 (uint32_t) param
.nType
,
1114 (uint32_t) param
.nBufSize
,
1115 (uint32_t) param
.nIsUnderflow
,
1116 (uint32_t) param
.nDsOverflow
,
1117 (uint32_t) param
.nPODelay
,
1118 (uint32_t) param
.nInvalid
);
1120 pvt
->jb_size
= param
.nBufSize
;
1121 pvt
->jb_underflow
= param
.nIsUnderflow
;
1122 pvt
->jb_overflow
= param
.nDsOverflow
;
1123 pvt
->jb_invalid
= param
.nInvalid
;
1124 pvt
->jb_delay
= param
.nPODelay
;
1129 static int lantiq_standby(int c
)
1131 ast_debug(1, "Stopping line feed for channel %i\n", c
);
1132 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_LINE_FEED_SET
, IFX_TAPI_LINE_FEED_STANDBY
)) {
1133 ast_log(LOG_ERROR
, "IFX_TAPI_LINE_FEED_SET ioctl failed\n");
1137 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_ENC_STOP
, 0)) {
1138 ast_log(LOG_ERROR
, "IFX_TAPI_ENC_STOP ioctl failed\n");
1142 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_DEC_STOP
, 0)) {
1143 ast_log(LOG_ERROR
, "IFX_TAPI_DEC_STOP ioctl failed\n");
1147 return lantiq_play_tone(c
, TAPI_TONE_LOCALE_NONE
);
1150 static int lantiq_end_dialing(int c
)
1152 struct lantiq_pvt
*pvt
= &iflist
[c
];
1154 ast_log(LOG_DEBUG
, "end of dialing\n");
1156 if (pvt
->dial_timer
!= -1) {
1157 AST_SCHED_DEL(sched
, pvt
->dial_timer
);
1158 pvt
->dial_timer
= -1;
1162 ast_hangup(pvt
->owner
);
1164 lantiq_reset_dtmfbuf(pvt
);
1169 static int lantiq_end_call(int c
)
1171 struct lantiq_pvt
*pvt
= &iflist
[c
];
1173 ast_log(LOG_DEBUG
, "end of call\n");
1176 lantiq_jb_get_stats(c
);
1177 ast_queue_hangup(pvt
->owner
);
1183 static struct ast_channel
*lantiq_channel(int state
, int c
, char *ext
, char *ctx
, struct ast_format_cap
*cap
)
1185 struct ast_channel
*chan
= NULL
;
1186 struct lantiq_pvt
*pvt
= &iflist
[c
];
1187 struct ast_format format
= {0};
1188 struct ast_format_cap
*newcap
= ast_format_cap_alloc();
1191 ast_log(LOG_DEBUG
, "Cannot allocate format capabilities!\n");
1195 chan
= ast_channel_alloc(1, state
, NULL
, NULL
, "", ext
, ctx
, 0, c
, "TAPI/%d", (c
+ 1));
1197 ast_log(LOG_DEBUG
, "Cannot allocate channel!\n");
1198 ast_format_cap_destroy(newcap
);
1202 ast_channel_tech_set(chan
, &lantiq_tech
);
1203 ast_channel_tech_pvt_set(chan
, pvt
);
1206 if (cap
&& ast_format_cap_has_joint(cap
, lantiq_tech
.capabilities
)) { /* compatible format capabilities given */
1207 ast_format_cap_joint_copy(lantiq_tech
.capabilities
, cap
, newcap
);
1208 ast_best_codec(newcap
, &format
); /* choose format */
1209 } else { /* no or unsupported format capabilities given */
1210 ast_best_codec(lantiq_tech
.capabilities
, &format
); /* choose format from capabilities */
1213 /* set choosed format */
1214 ast_format_cap_set(ast_channel_nativeformats(chan
), &format
);
1215 ast_format_copy(ast_channel_readformat(chan
), &format
);
1216 ast_format_copy(ast_channel_writeformat(chan
), &format
);
1217 ast_format_copy(ast_channel_rawreadformat(chan
), &format
);
1218 ast_format_copy(ast_channel_rawwriteformat(chan
), &format
);
1220 ast_format_cap_destroy(newcap
);
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_channel
*requestor
, const char *data
, int *cause
)
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_getformatname_multiple(buf
, sizeof(buf
), cap
));
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
);
1268 ast_channel_unlock(chan
);
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
= {0};
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 ast_format_copy(&frame
.subclass
.format
, &format
);
1329 frame
.datalen
= res
- RTP_HEADER_LEN
;
1330 frame
.data
.ptr
= buf
+ RTP_HEADER_LEN
;
1331 frame
.samples
= ast_codec_get_samples(&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
);
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
);
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 };
1843 struct ast_format format
;
1845 if(!(lantiq_tech
.capabilities
= ast_format_cap_alloc())) {
1846 ast_log(LOG_ERROR
, "Unable to allocate format capabilities.\n");
1847 return AST_MODULE_LOAD_DECLINE
;
1850 /* channel format capabilities */
1851 ast_format_cap_add(lantiq_tech
.capabilities
, ast_format_set(&format
, AST_FORMAT_ULAW
, 0));
1852 ast_format_cap_add(lantiq_tech
.capabilities
, ast_format_set(&format
, AST_FORMAT_ALAW
, 0));
1853 ast_format_cap_add(lantiq_tech
.capabilities
, ast_format_set(&format
, AST_FORMAT_G722
, 0));
1854 ast_format_cap_add(lantiq_tech
.capabilities
, ast_format_set(&format
, AST_FORMAT_G726
, 0));
1855 ast_format_cap_add(lantiq_tech
.capabilities
, ast_format_set(&format
, AST_FORMAT_SLINEAR
, 0));
1857 /* Turn off the LEDs, just in case */
1858 led_off(dev_ctx
.voip_led
);
1859 for(c
= 0; c
< TAPI_AUDIO_PORT_NUM_MAX
; c
++)
1860 led_off(dev_ctx
.ch_led
[c
]);
1862 if ((cfg
= ast_config_load(config
, config_flags
)) == CONFIG_STATUS_FILEINVALID
) {
1863 ast_log(LOG_ERROR
, "Config file %s is in an invalid format. Aborting.\n", config
);
1864 return AST_MODULE_LOAD_DECLINE
;
1867 /* We *must* have a config file otherwise stop immediately */
1869 ast_log(LOG_ERROR
, "Unable to load config %s\n", config
);
1870 return AST_MODULE_LOAD_DECLINE
;
1873 if (ast_mutex_lock(&iflock
)) {
1874 ast_log(LOG_ERROR
, "Unable to lock interface list.\n");
1878 for (v
= ast_variable_browse(cfg
, "interfaces"); v
; v
= v
->next
) {
1879 if (!strcasecmp(v
->name
, "channels")) {
1880 dev_ctx
.channels
= atoi(v
->value
);
1881 if (!dev_ctx
.channels
) {
1882 ast_log(LOG_ERROR
, "Invalid value for channels in config %s\n", config
);
1885 } else if (!strcasecmp(v
->name
, "firmwarefilename")) {
1886 ast_copy_string(firmware_filename
, v
->value
, sizeof(firmware_filename
));
1887 } else if (!strcasecmp(v
->name
, "bbdfilename")) {
1888 ast_copy_string(bbd_filename
, v
->value
, sizeof(bbd_filename
));
1889 } else if (!strcasecmp(v
->name
, "basepath")) {
1890 ast_copy_string(base_path
, v
->value
, sizeof(base_path
));
1891 } else if (!strcasecmp(v
->name
, "per_channel_context")) {
1892 if (!strcasecmp(v
->value
, "on")) {
1893 per_channel_context
= 1;
1894 } else if (!strcasecmp(v
->value
, "off")) {
1895 per_channel_context
= 0;
1897 ast_log(LOG_ERROR
, "Unknown per_channel_context value '%s'. Try 'on' or 'off'.\n", v
->value
);
1903 for (v
= ast_variable_browse(cfg
, "general"); v
; v
= v
->next
) {
1904 if (!strcasecmp(v
->name
, "rxgain")) {
1905 rxgain
= atoi(v
->value
);
1908 ast_log(LOG_WARNING
, "Invalid rxgain: %s, using default.\n", v
->value
);
1910 } else if (!strcasecmp(v
->name
, "txgain")) {
1911 txgain
= atoi(v
->value
);
1914 ast_log(LOG_WARNING
, "Invalid txgain: %s, using default.\n", v
->value
);
1916 } else if (!strcasecmp(v
->name
, "echocancel")) {
1917 if (!strcasecmp(v
->value
, "off")) {
1918 wlec_type
= IFX_TAPI_WLEC_TYPE_OFF
;
1919 } else if (!strcasecmp(v
->value
, "nlec")) {
1920 wlec_type
= IFX_TAPI_WLEC_TYPE_NE
;
1921 if (!strcasecmp(v
->name
, "echocancelfixedwindowsize")) {
1922 wlec_nbne
= atoi(v
->value
);
1924 } else if (!strcasecmp(v
->value
, "wlec")) {
1925 wlec_type
= IFX_TAPI_WLEC_TYPE_NFE
;
1926 if (!strcasecmp(v
->name
, "echocancelnfemovingwindowsize")) {
1927 wlec_nbfe
= atoi(v
->value
);
1928 } else if (!strcasecmp(v
->name
, "echocancelfixedwindowsize")) {
1929 wlec_nbne
= atoi(v
->value
);
1930 } else if (!strcasecmp(v
->name
, "echocancelwidefixedwindowsize")) {
1931 wlec_wbne
= atoi(v
->value
);
1933 } else if (!strcasecmp(v
->value
, "nees")) {
1934 wlec_type
= IFX_TAPI_WLEC_TYPE_NE_ES
;
1935 } else if (!strcasecmp(v
->value
, "nfees")) {
1936 wlec_type
= IFX_TAPI_WLEC_TYPE_NFE_ES
;
1937 } else if (!strcasecmp(v
->value
, "es")) {
1938 wlec_type
= IFX_TAPI_WLEC_TYPE_ES
;
1940 wlec_type
= IFX_TAPI_WLEC_TYPE_OFF
;
1941 ast_log(LOG_ERROR
, "Unknown echo cancellation type '%s'\n", v
->value
);
1944 } else if (!strcasecmp(v
->name
, "echocancelnlp")) {
1945 if (!strcasecmp(v
->value
, "on")) {
1946 wlec_nlp
= IFX_TAPI_WLEC_NLP_ON
;
1947 } else if (!strcasecmp(v
->value
, "off")) {
1948 wlec_nlp
= IFX_TAPI_WLEC_NLP_OFF
;
1950 ast_log(LOG_ERROR
, "Unknown echo cancellation nlp '%s'\n", v
->value
);
1953 } else if (!strcasecmp(v
->name
, "jitterbuffertype")) {
1954 if (!strcasecmp(v
->value
, "fixed")) {
1955 jb_type
= IFX_TAPI_JB_TYPE_FIXED
;
1956 } else if (!strcasecmp(v
->value
, "adaptive")) {
1957 jb_type
= IFX_TAPI_JB_TYPE_ADAPTIVE
;
1958 jb_localadpt
= IFX_TAPI_JB_LOCAL_ADAPT_DEFAULT
;
1959 if (!strcasecmp(v
->name
, "jitterbufferadaptation")) {
1960 if (!strcasecmp(v
->value
, "on")) {
1961 jb_localadpt
= IFX_TAPI_JB_LOCAL_ADAPT_ON
;
1962 } else if (!strcasecmp(v
->value
, "off")) {
1963 jb_localadpt
= IFX_TAPI_JB_LOCAL_ADAPT_OFF
;
1965 } else if (!strcasecmp(v
->name
, "jitterbufferscalling")) {
1966 jb_scaling
= atoi(v
->value
);
1967 } else if (!strcasecmp(v
->name
, "jitterbufferinitialsize")) {
1968 jb_initialsize
= atoi(v
->value
);
1969 } else if (!strcasecmp(v
->name
, "jitterbufferminsize")) {
1970 jb_minsize
= atoi(v
->value
);
1971 } else if (!strcasecmp(v
->name
, "jitterbuffermaxsize")) {
1972 jb_maxsize
= atoi(v
->value
);
1975 ast_log(LOG_ERROR
, "Unknown jitter buffer type '%s'\n", v
->value
);
1978 } else if (!strcasecmp(v
->name
, "jitterbufferpackettype")) {
1979 if (!strcasecmp(v
->value
, "voice")) {
1980 jb_pckadpt
= IFX_TAPI_JB_PKT_ADAPT_VOICE
;
1981 } else if (!strcasecmp(v
->value
, "data")) {
1982 jb_pckadpt
= IFX_TAPI_JB_PKT_ADAPT_DATA
;
1983 } else if (!strcasecmp(v
->value
, "datanorep")) {
1984 jb_pckadpt
= IFX_TAPI_JB_PKT_ADAPT_DATA_NO_REP
;
1986 ast_log(LOG_ERROR
, "Unknown jitter buffer packet adaptation type '%s'\n", v
->value
);
1989 } else if (!strcasecmp(v
->name
, "calleridtype")) {
1990 ast_log(LOG_DEBUG
, "Setting CID type to %s.\n", v
->value
);
1991 if (!strcasecmp(v
->value
, "telecordia")) {
1992 cid_type
= IFX_TAPI_CID_STD_TELCORDIA
;
1993 } else if (!strcasecmp(v
->value
, "etsifsk")) {
1994 cid_type
= IFX_TAPI_CID_STD_ETSI_FSK
;
1995 } else if (!strcasecmp(v
->value
, "etsidtmf")) {
1996 cid_type
= IFX_TAPI_CID_STD_ETSI_DTMF
;
1997 } else if (!strcasecmp(v
->value
, "sin")) {
1998 cid_type
= IFX_TAPI_CID_STD_SIN
;
1999 } else if (!strcasecmp(v
->value
, "ntt")) {
2000 cid_type
= IFX_TAPI_CID_STD_NTT
;
2001 } else if (!strcasecmp(v
->value
, "kpndtmf")) {
2002 cid_type
= IFX_TAPI_CID_STD_KPN_DTMF
;
2003 } else if (!strcasecmp(v
->value
, "kpndtmffsk")) {
2004 cid_type
= IFX_TAPI_CID_STD_KPN_DTMF_FSK
;
2006 ast_log(LOG_ERROR
, "Unknown caller id type '%s'\n", v
->value
);
2009 } else if (!strcasecmp(v
->name
, "voiceactivitydetection")) {
2010 if (!strcasecmp(v
->value
, "on")) {
2011 vad_type
= IFX_TAPI_ENC_VAD_ON
;
2012 } else if (!strcasecmp(v
->value
, "g711")) {
2013 vad_type
= IFX_TAPI_ENC_VAD_G711
;
2014 } else if (!strcasecmp(v
->value
, "cng")) {
2015 vad_type
= IFX_TAPI_ENC_VAD_CNG_ONLY
;
2016 } else if (!strcasecmp(v
->value
, "sc")) {
2017 vad_type
= IFX_TAPI_ENC_VAD_SC_ONLY
;
2019 ast_log(LOG_ERROR
, "Unknown voice activity detection value '%s'\n", v
->value
);
2022 } else if (!strcasecmp(v
->name
, "interdigit")) {
2023 dev_ctx
.interdigit_timeout
= atoi(v
->value
);
2024 ast_log(LOG_DEBUG
, "Setting interdigit timeout to %s.\n", v
->value
);
2025 if (!dev_ctx
.interdigit_timeout
) {
2026 dev_ctx
.interdigit_timeout
= DEFAULT_INTERDIGIT_TIMEOUT
;
2027 ast_log(LOG_WARNING
, "Invalid interdigit timeout: %s, using default.\n", v
->value
);
2029 } else if (!strcasecmp(v
->name
, "tone_generator")) {
2030 if (!strcasecmp(v
->value
, "integrated")) {
2031 tone_generator
= TONE_INTEGRATED
;
2032 } else if (!strcasecmp(v
->value
, "asterisk")) {
2033 tone_generator
= TONE_ASTERISK
;
2034 } else if (!strcasecmp(v
->value
, "media")) {
2035 tone_generator
= TONE_MEDIA
;
2037 ast_log(LOG_ERROR
, "Unknown tone_generator value '%s'. Try 'integrated', 'asterisk' or 'media'.\n", v
->value
);
2043 lantiq_create_pvts();
2045 ast_mutex_unlock(&iflock
);
2046 ast_config_destroy(cfg
);
2048 if (!(sched
= ast_sched_context_create())) {
2049 ast_log(LOG_ERROR
, "Unable to create scheduler context\n");
2053 if (ast_sched_start_thread(sched
)) {
2054 ast_log(LOG_ERROR
, "Unable to create scheduler context thread\n");
2058 if (ast_channel_register(&lantiq_tech
)) {
2059 ast_log(LOG_ERROR
, "Unable to register channel class 'Phone'\n");
2064 IFX_TAPI_TONE_t tone
;
2065 IFX_TAPI_DEV_START_CFG_t dev_start
;
2066 IFX_TAPI_MAP_DATA_t map_data
;
2067 IFX_TAPI_LINE_TYPE_CFG_t line_type
;
2068 IFX_TAPI_LINE_VOLUME_t line_vol
;
2069 IFX_TAPI_WLEC_CFG_t wlec_cfg
;
2070 IFX_TAPI_JB_CFG_t jb_cfg
;
2071 IFX_TAPI_CID_CFG_t cid_cfg
;
2074 dev_ctx
.dev_fd
= lantiq_dev_open(base_path
, 0);
2076 if (dev_ctx
.dev_fd
< 0) {
2077 ast_log(LOG_ERROR
, "lantiq TAPI device open function failed\n");
2081 snprintf(dev_ctx
.voip_led
, LED_NAME_LENGTH
, "voice");
2082 for (c
= 0; c
< dev_ctx
.channels
; c
++) {
2083 dev_ctx
.ch_fd
[c
] = lantiq_dev_open(base_path
, c
+ 1);
2085 if (dev_ctx
.ch_fd
[c
] < 0) {
2086 ast_log(LOG_ERROR
, "lantiq TAPI channel %d open function failed\n", c
);
2089 snprintf(dev_ctx
.ch_led
[c
], LED_NAME_LENGTH
, "fxs%d", c
+ 1);
2092 if (lantiq_dev_firmware_download(dev_ctx
.dev_fd
, firmware_filename
)) {
2093 ast_log(LOG_ERROR
, "voice firmware download failed\n");
2097 if (ioctl(dev_ctx
.dev_fd
, IFX_TAPI_DEV_STOP
, 0)) {
2098 ast_log(LOG_ERROR
, "IFX_TAPI_DEV_STOP ioctl failed\n");
2102 memset(&dev_start
, 0x0, sizeof(IFX_TAPI_DEV_START_CFG_t
));
2103 dev_start
.nMode
= IFX_TAPI_INIT_MODE_VOICE_CODER
;
2106 if (ioctl(dev_ctx
.dev_fd
, IFX_TAPI_DEV_START
, &dev_start
)) {
2107 ast_log(LOG_ERROR
, "IFX_TAPI_DEV_START ioctl failed\n");
2111 tz
= ast_get_indication_zone(NULL
);
2114 ast_log(LOG_ERROR
, "Unable to alloc tone zone\n");
2118 ts_dial
= ast_get_indication_tone(tz
, "dial");
2119 ts_ring
= ast_get_indication_tone(tz
, "ring");
2120 ts_busy
= ast_get_indication_tone(tz
, "busy");
2121 ts_congestion
= ast_get_indication_tone(tz
, "congestion");
2123 if (!ts_dial
|| !ts_dial
->data
|| !ts_ring
|| !ts_ring
->data
|| !ts_busy
|| !ts_busy
->data
|| !ts_congestion
|| !ts_congestion
->data
) {
2124 ast_log(LOG_ERROR
, "Unable to get indication tones\n");
2128 for (c
= 0; c
< dev_ctx
.channels
; c
++) {
2129 /* We're a FXS and want to switch between narrow & wide band automatically */
2130 memset(&line_type
, 0, sizeof(IFX_TAPI_LINE_TYPE_CFG_t
));
2131 line_type
.lineType
= IFX_TAPI_LINE_TYPE_FXS_AUTO
;
2132 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_LINE_TYPE_SET
, &line_type
)) {
2133 ast_log(LOG_ERROR
, "IFX_TAPI_LINE_TYPE_SET %d failed\n", c
);
2138 memset(&tone
, 0, sizeof(IFX_TAPI_TONE_t
));
2139 tone
.simple
.format
= IFX_TAPI_TONE_TYPE_SIMPLE
;
2140 playlist_to_tapitone(ts_dial
->data
, TAPI_TONE_LOCALE_DIAL_CODE
, &tone
);
2141 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_TONE_TABLE_CFG_SET
, &tone
)) {
2142 ast_log(LOG_ERROR
, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c
);
2146 memset(&tone
, 0, sizeof(IFX_TAPI_TONE_t
));
2147 tone
.simple
.format
= IFX_TAPI_TONE_TYPE_SIMPLE
;
2148 playlist_to_tapitone(ts_ring
->data
, TAPI_TONE_LOCALE_RINGING_CODE
, &tone
);
2149 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_TONE_TABLE_CFG_SET
, &tone
)) {
2150 ast_log(LOG_ERROR
, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c
);
2154 memset(&tone
, 0, sizeof(IFX_TAPI_TONE_t
));
2155 tone
.simple
.format
= IFX_TAPI_TONE_TYPE_SIMPLE
;
2156 playlist_to_tapitone(ts_busy
->data
, TAPI_TONE_LOCALE_BUSY_CODE
, &tone
);
2157 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_TONE_TABLE_CFG_SET
, &tone
)) {
2158 ast_log(LOG_ERROR
, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c
);
2162 memset(&tone
, 0, sizeof(IFX_TAPI_TONE_t
));
2163 tone
.simple
.format
= IFX_TAPI_TONE_TYPE_SIMPLE
;
2164 playlist_to_tapitone(ts_congestion
->data
, TAPI_TONE_LOCALE_CONGESTION_CODE
, &tone
);
2165 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_TONE_TABLE_CFG_SET
, &tone
)) {
2166 ast_log(LOG_ERROR
, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c
);
2171 IFX_TAPI_RING_CFG_t ringingType
;
2172 memset(&ringingType
, 0, sizeof(IFX_TAPI_RING_CFG_t
));
2173 ringingType
.nMode
= IFX_TAPI_RING_CFG_MODE_INTERNAL_BALANCED
;
2174 ringingType
.nSubmode
= IFX_TAPI_RING_CFG_SUBMODE_DC_RNG_TRIP_FAST
;
2175 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_RING_CFG_SET
, (IFX_int32_t
) &ringingType
)) {
2176 ast_log(LOG_ERROR
, "IFX_TAPI_RING_CFG_SET failed\n");
2181 IFX_char_t data
[15] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2182 0x00, 0x00, 0x00, 0x00, 0x00,
2183 0x00, 0x00, 0x00, 0x00, 0x00 };
2185 IFX_TAPI_RING_CADENCE_t ringCadence
;
2186 memset(&ringCadence
, 0, sizeof(IFX_TAPI_RING_CADENCE_t
));
2187 memcpy(&ringCadence
.data
, data
, sizeof(data
));
2188 ringCadence
.nr
= sizeof(data
) * 8;
2190 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_RING_CADENCE_HR_SET
, &ringCadence
)) {
2191 ast_log(LOG_ERROR
, "IFX_TAPI_RING_CADENCE_HR_SET failed\n");
2195 /* perform mapping */
2196 memset(&map_data
, 0x0, sizeof(IFX_TAPI_MAP_DATA_t
));
2197 map_data
.nDstCh
= c
;
2198 map_data
.nChType
= IFX_TAPI_MAP_TYPE_PHONE
;
2200 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_MAP_DATA_ADD
, &map_data
)) {
2201 ast_log(LOG_ERROR
, "IFX_TAPI_MAP_DATA_ADD %d failed\n", c
);
2206 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_LINE_FEED_SET
, IFX_TAPI_LINE_FEED_STANDBY
)) {
2207 ast_log(LOG_ERROR
, "IFX_TAPI_LINE_FEED_SET %d failed\n", c
);
2212 memset(&line_vol
, 0, sizeof(line_vol
));
2213 line_vol
.nGainRx
= rxgain
;
2214 line_vol
.nGainTx
= txgain
;
2216 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_PHONE_VOLUME_SET
, &line_vol
)) {
2217 ast_log(LOG_ERROR
, "IFX_TAPI_PHONE_VOLUME_SET %d failed\n", c
);
2221 /* Configure line echo canceller */
2222 memset(&wlec_cfg
, 0, sizeof(wlec_cfg
));
2223 wlec_cfg
.nType
= wlec_type
;
2224 wlec_cfg
.bNlp
= wlec_nlp
;
2225 wlec_cfg
.nNBFEwindow
= wlec_nbfe
;
2226 wlec_cfg
.nNBNEwindow
= wlec_nbne
;
2227 wlec_cfg
.nWBNEwindow
= wlec_wbne
;
2229 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_WLEC_PHONE_CFG_SET
, &wlec_cfg
)) {
2230 ast_log(LOG_ERROR
, "IFX_TAPI_WLEC_PHONE_CFG_SET %d failed\n", c
);
2234 /* Configure jitter buffer */
2235 memset(&jb_cfg
, 0, sizeof(jb_cfg
));
2236 jb_cfg
.nJbType
= jb_type
;
2237 jb_cfg
.nPckAdpt
= jb_pckadpt
;
2238 jb_cfg
.nLocalAdpt
= jb_localadpt
;
2239 jb_cfg
.nScaling
= jb_scaling
;
2240 jb_cfg
.nInitialSize
= jb_initialsize
;
2241 jb_cfg
.nMinSize
= jb_minsize
;
2242 jb_cfg
.nMaxSize
= jb_maxsize
;
2244 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_JB_CFG_SET
, &jb_cfg
)) {
2245 ast_log(LOG_ERROR
, "IFX_TAPI_JB_CFG_SET %d failed\n", c
);
2249 /* Configure Caller ID type */
2250 memset(&cid_cfg
, 0, sizeof(cid_cfg
));
2251 cid_cfg
.nStandard
= cid_type
;
2253 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_CID_CFG_SET
, &cid_cfg
)) {
2254 ast_log(LOG_ERROR
, "IIFX_TAPI_CID_CFG_SET %d failed\n", c
);
2258 /* Configure voice activity detection */
2259 if (ioctl(dev_ctx
.ch_fd
[c
], IFX_TAPI_ENC_VAD_CFG_SET
, vad_type
)) {
2260 ast_log(LOG_ERROR
, "IFX_TAPI_ENC_VAD_CFG_SET %d failed\n", c
);
2264 /* Setup TAPI <-> internal RTP codec type mapping */
2265 if (lantiq_setup_rtp(c
)) {
2269 /* Set initial hook status */
2270 iflist
[c
].channel_state
= lantiq_get_hookstatus(c
);
2272 if (iflist
[c
].channel_state
== UNKNOWN
) {
2277 /* make sure our device will be closed properly */
2278 ast_register_atexit(lantiq_cleanup
);
2281 led_on(dev_ctx
.voip_led
);
2282 return AST_MODULE_LOAD_SUCCESS
;
2285 ast_mutex_unlock(&iflock
);
2287 ast_config_destroy(cfg
);
2288 return AST_MODULE_LOAD_DECLINE
;
2291 ast_sched_context_destroy(sched
);
2295 return AST_MODULE_LOAD_FAILURE
;
2298 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_LOAD_ORDER
, "Lantiq TAPI Telephony API Support",
2299 .load
= load_module
,
2300 .unload
= unload_module
,
2301 .load_pri
= AST_MODPRI_CHANNEL_DRIVER