Merge pull request #164 from guidosarducci/lede-17.01-siproxd-update
[feed/telephony.git] / net / asterisk-11.x / src-lantiq / channels / chan_lantiq.c
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
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
13 *
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>
21 *
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.
27 *
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.
31 */
32
33 /*! \file
34 *
35 * \brief Asterisk channel line driver for Lantiq based TAPI boards
36 *
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>
44 *
45 * \ingroup channel_drivers
46 */
47
48 #include "asterisk.h"
49
50 ASTERISK_FILE_VERSION(__FILE__, "$Revision: xxx $")
51
52 #include <ctype.h>
53 #include <sys/socket.h>
54 #include <sys/time.h>
55 #include <arpa/inet.h>
56 #include <fcntl.h>
57 #include <sys/ioctl.h>
58 #include <sys/stat.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #ifdef HAVE_LINUX_COMPILER_H
62 #include <linux/compiler.h>
63 #endif
64 #include <linux/telephony.h>
65
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
81 /* Lantiq TAPI includes */
82 #include <drv_tapi/drv_tapi_io.h>
83 #include <drv_vmmc/vmmc_io.h>
84
85 #define TAPI_AUDIO_PORT_NUM_MAX 2
86
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
93
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
99
100 #define LANTIQ_CONTEXT_PREFIX "lantiq"
101 #define DEFAULT_INTERDIGIT_TIMEOUT 4000
102 #define G723_HIGH_RATE 1
103 #define LED_NAME_LENGTH 32
104
105 static const char config[] = "lantiq.conf";
106
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;
111
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 */
117 };
118
119 /* tone generator (default is integraded) */
120 static enum tone_generator_t tone_generator = TONE_INTEGRATED;
121
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;
127
128 /*
129 * The private structures of the Phone Jack channels are linked for selecting
130 * outgoing channels.
131 */
132 enum channel_state {
133 ONHOOK,
134 OFFHOOK,
135 DIALING,
136 INCALL,
137 CALL_ENDED,
138 RINGING,
139 UNKNOWN
140 };
141
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 */
145 int channel_state;
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 */
162 } *iflist = NULL;
163
164 static struct lantiq_ctx {
165 int dev_fd;
166 int channels;
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 */
171 } dev_ctx;
172
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);
191
192 static struct ast_channel_tech lantiq_tech = {
193 .type = "TAPI",
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
208 };
209
210 /* Protect the interface list (of lantiq_pvt's) */
211 AST_MUTEX_DEFINE_STATIC(iflock);
212
213 /*
214 * Protect the monitoring thread, so only one process can kill or start it, and
215 * not when it's doing something critical.
216 */
217 AST_MUTEX_DEFINE_STATIC(monlock);
218
219 /* The scheduling context */
220 struct ast_sched_context *sched;
221
222 /*
223 * This is the thread for the monitor which checks for input on the channels
224 * which are not currently in use.
225 */
226 static pthread_t monitor_thread = AST_PTHREADT_NULL;
227
228
229 #define WORDS_BIGENDIAN
230 /* struct taken from some GPLed code by Mike Borella */
231 typedef struct rtp_header
232 {
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;
236 #else
237 uint8_t csrc_count:4, extension:1, padding:1, version:2;
238 uint8_t payload_type:7, marker:1;
239 #endif
240 uint16_t seqno;
241 uint32_t timestamp;
242 uint32_t ssrc;
243 } rtp_header_t;
244 #define RTP_HEADER_LEN 12
245 #define RTP_BUFFER_LEN 512
246 /* Internal RTP payload types - standard */
247 #define RTP_PCMU 0
248 #define RTP_G723_63 4
249 #define RTP_PCMA 8
250 #define RTP_G722 9
251 #define RTP_CN 13
252 #define RTP_G729 18
253 /* Internal RTP payload types - custom */
254 #define RTP_G7221 100
255 #define RTP_G726 101
256 #define RTP_ILBC 102
257 #define RTP_SLIN8 103
258 #define RTP_SLIN16 104
259 #define RTP_SIREN7 105
260 #define RTP_G723_53 106
261
262
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)
267 {
268 char fname[100];
269
270 if (snprintf(fname, sizeof(fname), "/sys/class/leds/%s/%s", led, sub) >= sizeof(fname))
271 return NULL;
272 return fopen(fname, "r+");
273 }
274
275 static FILE *led_trigger(const char *led)
276 {
277 return led_open(led, "trigger");
278 }
279
280 static void led_delay(const char *led, int onoff, int msec)
281 {
282 FILE *fp = led_open(led, onoff ? "delay_on" : "delay_off");
283 if (fp) {
284 fprintf(fp,"%d\n",msec);
285 fclose(fp);
286 }
287 }
288
289 static void led_on(const char *led)
290 {
291 FILE *fp;
292
293 fp = led_trigger(led);
294 if (fp) {
295 fprintf(fp,"default-on\n");
296 fclose(fp);
297 }
298 }
299
300 static void led_off(const char *led)
301 {
302 FILE *fp;
303
304 fp = led_trigger(led);
305 if (fp) {
306 fprintf(fp,"none\n");
307 fclose(fp);
308 }
309 }
310
311 static void led_blink(const char *led, int period)
312 {
313 FILE *fp;
314
315 fp = led_trigger(led);
316 if (fp) {
317 fprintf(fp, "timer\n");
318 fclose(fp);
319 led_delay(led, 1, period/2);
320 led_delay(led, 0, period/2);
321 }
322 }
323
324 static uint32_t now(void) {
325 struct timespec ts;
326 clock_gettime(CLOCK_MONOTONIC, &ts);
327
328 uint64_t tmp = ts.tv_sec*1000 + (ts.tv_nsec/1000000);
329 return (uint32_t) tmp;
330 }
331
332 static uint32_t epoch(void) {
333 struct timeval tv;
334 gettimeofday(&tv, NULL);
335
336 return tv.tv_sec;
337 }
338
339 static int lantiq_dev_open(const char *dev_path, const int32_t ch_num)
340 {
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);
345 }
346
347 static void lantiq_ring(int c, int r, const char *cid, const char *name)
348 {
349 uint8_t status;
350
351 if (r) {
352 led_blink(dev_ctx.ch_led[c], LED_FAST_BLINK);
353 if (!cid) {
354 status = (uint8_t) ioctl(dev_ctx.ch_fd[c], IFX_TAPI_RING_START, 0);
355 } else {
356 IFX_TAPI_CID_MSG_t msg;
357 IFX_TAPI_CID_MSG_ELEMENT_t elements[3];
358 int count = 0;
359 time_t timestamp;
360 struct tm *tm;
361
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;
366 }
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';
369 count++;
370
371 if (name) {
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;
376 }
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';
379 count++;
380 }
381
382 if ((time(&timestamp) != -1) && ((tm=localtime(&timestamp)) != 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;
388 count++;
389 }
390
391 msg.txMode = IFX_TAPI_CID_HM_ONHOOK;
392 msg.messageType = IFX_TAPI_CID_MT_CSUP;
393 msg.message = elements;
394 msg.nMsgElements = count;
395
396 status = (uint8_t) ioctl(dev_ctx.ch_fd[c], IFX_TAPI_CID_TX_SEQ_START, (IFX_int32_t) &msg);
397 }
398 } else {
399 status = (uint8_t) ioctl(dev_ctx.ch_fd[c], IFX_TAPI_RING_STOP, 0);
400 led_off(dev_ctx.ch_led[c]);
401 }
402
403 if (status) {
404 ast_log(LOG_ERROR, "%s ioctl failed\n",
405 (r ? "IFX_TAPI_RING_START" : "IFX_TAPI_RING_STOP"));
406 }
407 }
408
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 */
413 int error=0;
414 int ret;
415 int i;
416
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) };
419
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) };
422
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 };
425
426 /* determine if freq already set */
427 for (i = 0; i < n; i++) {
428 if(*freqarr[i] == freq) /* freq found */
429 break;
430 else if (i == n-1) /* last iteration */
431 error=1; /* not found */
432 }
433
434 /* write frequency if not already set */
435 if(error) {
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 */
442 break;
443 } else if (i == n-1) /* last iteration */
444 error=1; /* no free place becaus maximum count of freq's is set */
445 }
446 }
447
448 /* set freq ID return value */
449 if (!freq || error)
450 ret = IFX_TAPI_TONE_FREQNONE;
451 else
452 ret = retarr[i];
453
454 return ret; /* freq ID */
455 }
456
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)
460 {
461 char *s, *data = ast_strdupa(playlst);
462 char *stringp;
463 char *separator;
464 int i;
465
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;
470
471 stringp = data;
472
473 /* check if the data is separated with '|' or with ',' by default */
474 if (strchr(stringp,'|')) {
475 separator = "|";
476 } else {
477 separator = ",";
478 }
479
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 = {
482 .time = 0,
483 };
484
485 s = ast_strip(s);
486 if (s[0]=='!') {
487 s++;
488 }
489
490 if (ast_tone_zone_part_parse(s, &tone_data)) {
491 ast_log(LOG_ERROR, "Failed to parse tone part '%s'\n", s);
492 continue;
493 }
494
495 /* first tone must hava a cadence */
496 if (i==0 && !tone_data.time)
497 tone->simple.cadence[i] = 1000;
498 else
499 tone->simple.cadence[i] = tone_data.time;
500
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;
505 }
506
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);
511 }
512 }
513
514 static int lantiq_play_tone(int c, int t)
515 {
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);
518
519 /* do not handle stop tone twice */
520 if (t != TAPI_TONE_LOCALE_NONE) {
521 /* start new tone */
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");
524 return -1;
525 }
526 }
527
528 return 0;
529 }
530
531 static enum channel_state lantiq_get_hookstatus(int port)
532 {
533 uint8_t status;
534
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");
537 return UNKNOWN;
538 }
539
540 if (status) {
541 return OFFHOOK;
542 } else {
543 return ONHOOK;
544 }
545 }
546
547 static int
548 lantiq_dev_binary_buffer_create(const char *path, uint8_t **ppBuf, uint32_t *pBufSz)
549 {
550 FILE *fd;
551 struct stat file_stat;
552 int status = -1;
553
554 fd = fopen(path, "rb");
555 if (fd == NULL) {
556 ast_log(LOG_ERROR, "binary file %s open failed\n", path);
557 goto on_exit;
558 }
559
560 if (stat(path, &file_stat)) {
561 ast_log(LOG_ERROR, "file %s statistics get failed\n", path);
562 goto on_exit;
563 }
564
565 *ppBuf = malloc(file_stat.st_size);
566 if (*ppBuf == NULL) {
567 ast_log(LOG_ERROR, "binary file %s memory allocation failed\n", path);
568 goto on_exit;
569 }
570
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);
573 status = -1;
574 goto on_exit;
575 }
576
577 *pBufSz = file_stat.st_size;
578 status = 0;
579
580 on_exit:
581 if (fd != NULL)
582 fclose(fd);
583
584 if (*ppBuf != NULL && status)
585 free(*ppBuf);
586
587 return status;
588 }
589
590 static int32_t lantiq_dev_firmware_download(int32_t fd, const char *path)
591 {
592 uint8_t *firmware = NULL;
593 uint32_t size = 0;
594 VMMC_IO_INIT vmmc_io_init;
595
596 ast_log(LOG_DEBUG, "loading firmware: \"%s\".\n", path);
597
598 if (lantiq_dev_binary_buffer_create(path, &firmware, &size))
599 return -1;
600
601 memset(&vmmc_io_init, 0, sizeof(VMMC_IO_INIT));
602 vmmc_io_init.pPRAMfw = firmware;
603 vmmc_io_init.pram_size = size;
604
605 if (ioctl(fd, FIO_FW_DOWNLOAD, &vmmc_io_init)) {
606 ast_log(LOG_ERROR, "FIO_FW_DOWNLOAD ioctl failed\n");
607 return -1;
608 }
609
610 if (firmware != NULL)
611 free(firmware);
612
613 return 0;
614 }
615
616 static const char *state_string(enum channel_state s)
617 {
618 switch (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";
626 }
627 }
628
629 static const char *control_string(int c)
630 {
631 switch (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";
655 }
656 }
657
658 static int ast_lantiq_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
659 {
660 struct lantiq_pvt *pvt = ast_channel_tech_pvt(chan);
661
662 ast_verb(3, "phone indication \"%s\"\n", control_string(condition));
663
664 switch (condition) {
665 case -1:
666 {
667 lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_NONE);
668 return 0;
669 }
670 case AST_CONTROL_CONGESTION:
671 {
672 if (tone_generator == TONE_INTEGRATED)
673 lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_CONGESTION_CODE);
674 else
675 ast_playtones_start(chan, 0, ts_congestion->data, 1);
676
677 return 0;
678 }
679 case AST_CONTROL_BUSY:
680 {
681 if (tone_generator == TONE_INTEGRATED)
682 lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_BUSY_CODE);
683 else
684 ast_playtones_start(chan, 0, ts_busy->data, 1);
685
686 return 0;
687 }
688 case AST_CONTROL_RINGING:
689 case AST_CONTROL_PROGRESS:
690 {
691 pvt->call_setup_delay = now() - pvt->call_setup_start;
692
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);
697
698 return 0;
699 }
700 default:
701 {
702 /* -1 lets asterisk generate the tone */
703 return -1;
704 }
705 }
706 }
707
708 static int ast_lantiq_fixup(struct ast_channel *old, struct ast_channel *new)
709 {
710 ast_log(LOG_DEBUG, "entering... no code here...\n");
711 return 0;
712 }
713
714 static int ast_digit_begin(struct ast_channel *chan, char digit)
715 {
716 /* TODO: Modify this callback to let Asterisk support controlling the length of DTMF */
717 ast_log(LOG_DEBUG, "entering... no code here...\n");
718 return 0;
719 }
720
721 static int ast_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
722 {
723 ast_log(LOG_DEBUG, "entering... no code here...\n");
724 return 0;
725 }
726
727 static int ast_lantiq_call(struct ast_channel *ast, const char *dest, int timeout)
728 {
729 int res = 0;
730 struct lantiq_pvt *pvt;
731
732 /* lock to prevent simultaneous access with do_monitor thread processing */
733 ast_mutex_lock(&iflock);
734
735 pvt = ast_channel_tech_pvt(ast);
736 ast_log(LOG_DEBUG, "state: %s\n", state_string(pvt->channel_state));
737
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;
742
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");
746
747 lantiq_ring(pvt->port_id, 1, cid, name);
748 pvt->channel_state = RINGING;
749
750 ast_setstate(ast, AST_STATE_RINGING);
751 ast_queue_control(ast, AST_CONTROL_RINGING);
752 } else {
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);
756 res = -1;
757 }
758
759 ast_mutex_unlock(&iflock);
760
761 return res;
762 }
763
764 static int ast_lantiq_hangup(struct ast_channel *ast)
765 {
766 struct lantiq_pvt *pvt;
767
768 /* lock to prevent simultaneous access with do_monitor thread processing */
769 ast_mutex_lock(&iflock);
770
771 pvt = ast_channel_tech_pvt(ast);
772 ast_log(LOG_DEBUG, "state: %s\n", state_string(pvt->channel_state));
773
774 if (ast_channel_state(ast) == AST_STATE_RINGING) {
775 ast_debug(1, "channel state is RINGING\n");
776 }
777
778 switch (pvt->channel_state) {
779 case RINGING:
780 case ONHOOK:
781 lantiq_ring(pvt->port_id, 0, NULL, NULL);
782 pvt->channel_state = ONHOOK;
783 break;
784 default:
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);
788 }
789
790 lantiq_jb_get_stats(pvt->port_id);
791
792 ast_setstate(ast, AST_STATE_DOWN);
793 ast_module_unref(ast_module_info->self);
794 ast_channel_tech_pvt_set(ast, NULL);
795 pvt->owner = NULL;
796
797 ast_mutex_unlock(&iflock);
798
799 return 0;
800 }
801
802 static int ast_lantiq_answer(struct ast_channel *ast)
803 {
804 struct lantiq_pvt *pvt = ast_channel_tech_pvt(ast);
805
806 ast_log(LOG_DEBUG, "Remote end has answered call.\n");
807
808 if (lantiq_conf_enc(pvt->port_id, ast_channel_writeformat(ast)))
809 return -1;
810
811 pvt->call_answer = epoch();
812
813 return 0;
814 }
815
816 static struct ast_frame * ast_lantiq_read(struct ast_channel *ast)
817 {
818 ast_log(LOG_DEBUG, "entering... no code here...\n");
819 return NULL;
820 }
821
822 /* create asterisk format from rtp payload type */
823 static struct ast_format lantiq_map_rtptype_to_format(uint8_t rtptype)
824 {
825 struct ast_format format = {0};
826
827 switch (rtptype) {
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;
839 default:
840 {
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);
843 }
844 }
845
846 return format;
847 }
848
849 /* create rtp payload type from asterisk format */
850 static uint8_t lantiq_map_format_to_rtptype(const struct ast_format *format)
851 {
852 uint8_t rtptype = 0;
853 enum ast_format_id formatid = format ? format->id : 0;
854
855 switch (formatid) {
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;
866 #else
867 case AST_FORMAT_G723_1: rtptype = RTP_G723_53; break;
868 #endif
869 case AST_FORMAT_G729A: rtptype = RTP_G729; break;
870 default:
871 {
872 ast_log(LOG_ERROR, "unsupported format %s (0x%x), forcing ulaw\n", ast_getformatname(format), (int) formatid);
873 rtptype = RTP_PCMU;
874 }
875 }
876
877 return rtptype;
878 }
879
880 static int lantiq_conf_enc(int c, const struct ast_format *format)
881 {
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;
885
886 memset(&enc_cfg, 0, sizeof(IFX_TAPI_ENC_CFG_t));
887 switch (formatid) {
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;
892 break;
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;
897 break;
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;
902 break;
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;
907 break;
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;
912 break;
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;
917 break;
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;
923 break;
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;
928 break;
929 case AST_FORMAT_G723_1:
930 #if defined G723_HIGH_RATE
931 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_G723_63;
932 #else
933 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_G723_53;
934 #endif
935 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_30;
936 iflist[c].ptime = 30;
937 break;
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;
942 break;
943 default:
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;
948 }
949
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);
951
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);
954 }
955
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");
958 }
959
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");
962 }
963
964 return 0;
965 }
966
967 static int ast_lantiq_write(struct ast_channel *ast, struct ast_frame *frame)
968 {
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);
972 int ret;
973 uint8_t rtptype;
974 int subframes, subframes_rtp, length, samples;
975 char *head, *tail;
976
977 if(frame->frametype != AST_FRAME_VOICE) {
978 ast_log(LOG_DEBUG, "unhandled frame type\n");
979 return 0;
980 }
981
982 if (frame->datalen == 0) {
983 ast_log(LOG_DEBUG, "we've been prodded\n");
984 return 0;
985 }
986
987 /* get rtp payload type */
988 rtptype = lantiq_map_format_to_rtptype(&frame->subclass.format);
989
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;
997
998 subframes = (iflist[pvt->port_id].ptime + frame->len - 1) / iflist[pvt->port_id].ptime; /* number of subframes in AST frame */
999 if (subframes == 0)
1000 subframes = 1;
1001
1002 subframes_rtp = (RTP_BUFFER_LEN - RTP_HEADER_LEN) * subframes / frame->datalen; /* how many frames fit in a single RTP packet */
1003
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;
1007
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;
1013
1014 if ((tail - head) < (RTP_BUFFER_LEN - RTP_HEADER_LEN)) {
1015 length = tail - head;
1016 samples = length * frame->samples / frame->datalen;
1017 }
1018
1019 if ( frame->datalen <= (sizeof(buf) - RTP_HEADER_LEN) )
1020 memcpy(buf + RTP_HEADER_LEN, head, length);
1021 else {
1022 ast_log(LOG_WARNING, "buffer is too small\n");
1023 return -1;
1024 }
1025
1026 head += length;
1027 pvt->rtp_timestamp += (rtp_header->payload_type == RTP_G722) ? samples / 2 : samples; /* per RFC3551 */
1028
1029 ret = write(dev_ctx.ch_fd[pvt->port_id], buf, RTP_HEADER_LEN + length);
1030 if (ret < 0) {
1031 ast_debug(1, "TAPI: ast_lantiq_write(): error writing.\n");
1032 return -1;
1033 }
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);
1036 continue;
1037 }
1038 }
1039
1040 return 0;
1041 }
1042
1043 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *args, char *buf, size_t buflen)
1044 {
1045 struct lantiq_pvt *pvt;
1046 int res = 0;
1047
1048 if (!chan || ast_channel_tech(chan) != &lantiq_tech) {
1049 ast_log(LOG_ERROR, "This function requires a valid Lantiq TAPI channel\n");
1050 return -1;
1051 }
1052
1053 ast_mutex_lock(&iflock);
1054
1055 pvt = (struct lantiq_pvt*) ast_channel_tech_pvt(chan);
1056
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);
1083 } else {
1084 res = -1;
1085 }
1086
1087 ast_mutex_unlock(&iflock);
1088
1089 return res;
1090 }
1091
1092 static struct ast_frame * ast_lantiq_exception(struct ast_channel *ast)
1093 {
1094 ast_log(LOG_DEBUG, "entering... no code here...\n");
1095 return NULL;
1096 }
1097
1098 static void lantiq_jb_get_stats(int c) {
1099 struct lantiq_pvt *pvt = &iflist[c];
1100
1101 IFX_TAPI_JB_STATISTICS_t param;
1102 memset (&param, 0, sizeof (param));
1103 if (ioctl (dev_ctx.ch_fd[c], IFX_TAPI_JB_STATISTICS_GET, (IFX_int32_t) &param) != IFX_SUCCESS) {
1104 ast_debug(1, "Error getting jitter buffer stats.\n");
1105 } else {
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,
1110 #else
1111 ast_debug(1, "Jitter buffer stats: nType=%u, nBufSize=%u, nIsUnderflow=%u, nDsOverflow=%u, nPODelay=%u, nInvalid=%u\n",
1112 #endif
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);
1119
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;
1125 }
1126 }
1127
1128
1129 static int lantiq_standby(int c)
1130 {
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");
1134 return -1;
1135 }
1136
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");
1139 return -1;
1140 }
1141
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");
1144 return -1;
1145 }
1146
1147 return lantiq_play_tone(c, TAPI_TONE_LOCALE_NONE);
1148 }
1149
1150 static int lantiq_end_dialing(int c)
1151 {
1152 struct lantiq_pvt *pvt = &iflist[c];
1153
1154 ast_log(LOG_DEBUG, "end of dialing\n");
1155
1156 if (pvt->dial_timer != -1) {
1157 AST_SCHED_DEL(sched, pvt->dial_timer);
1158 pvt->dial_timer = -1;
1159 }
1160
1161 if(pvt->owner) {
1162 ast_hangup(pvt->owner);
1163 }
1164 lantiq_reset_dtmfbuf(pvt);
1165
1166 return 0;
1167 }
1168
1169 static int lantiq_end_call(int c)
1170 {
1171 struct lantiq_pvt *pvt = &iflist[c];
1172
1173 ast_log(LOG_DEBUG, "end of call\n");
1174
1175 if(pvt->owner) {
1176 lantiq_jb_get_stats(c);
1177 ast_queue_hangup(pvt->owner);
1178 }
1179
1180 return 0;
1181 }
1182
1183 static struct ast_channel *lantiq_channel(int state, int c, char *ext, char *ctx, struct ast_format_cap *cap)
1184 {
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();
1189
1190 if (!newcap) {
1191 ast_log(LOG_DEBUG, "Cannot allocate format capabilities!\n");
1192 return NULL;
1193 }
1194
1195 chan = ast_channel_alloc(1, state, NULL, NULL, "", ext, ctx, 0, c, "TAPI/%d", (c + 1));
1196 if (!chan) {
1197 ast_log(LOG_DEBUG, "Cannot allocate channel!\n");
1198 ast_format_cap_destroy(newcap);
1199 return NULL;
1200 }
1201
1202 ast_channel_tech_set(chan, &lantiq_tech);
1203 ast_channel_tech_pvt_set(chan, pvt);
1204 pvt->owner = chan;
1205
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 */
1211 }
1212
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);
1219
1220 ast_format_cap_destroy(newcap);
1221
1222 /* configuring encoder */
1223 if (format.id != 0)
1224 if (lantiq_conf_enc(c, &format) < 0)
1225 return NULL;
1226
1227 return chan;
1228 }
1229
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)
1231 {
1232 char buf[BUFSIZ];
1233 struct ast_channel *chan = NULL;
1234 int port_id = -1;
1235
1236 ast_mutex_lock(&iflock);
1237
1238 ast_debug(1, "Asked to create a TAPI channel with formats: %s\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
1239
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;
1244 goto bailout;
1245 }
1246
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;
1252 goto bailout;
1253 }
1254
1255 /* on asterisk user's side, we're using port 1-2.
1256 * Here in non normal human's world, we begin
1257 * counting at 0.
1258 */
1259 port_id -= 1;
1260
1261
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);
1266 } else {
1267 chan = lantiq_channel(AST_STATE_DOWN, port_id, NULL, NULL, cap);
1268 ast_channel_unlock(chan);
1269 }
1270
1271 bailout:
1272 ast_mutex_unlock(&iflock);
1273 return chan;
1274 }
1275
1276 static int ast_lantiq_devicestate(const char *data)
1277 {
1278 int port = atoi(data) - 1;
1279 if ((port < 1) || (port > dev_ctx.channels)) {
1280 return AST_DEVICE_INVALID;
1281 }
1282
1283 switch (iflist[port].channel_state) {
1284 case ONHOOK:
1285 return AST_DEVICE_NOT_INUSE;
1286 case OFFHOOK:
1287 case DIALING:
1288 case INCALL:
1289 case CALL_ENDED:
1290 return AST_DEVICE_INUSE;
1291 case RINGING:
1292 return AST_DEVICE_RINGING;
1293 case UNKNOWN:
1294 default:
1295 return AST_DEVICE_UNKNOWN;
1296 }
1297 }
1298
1299 static int lantiq_dev_data_handler(int c)
1300 {
1301 char buf[BUFSIZ];
1302 struct ast_frame frame = {0};
1303 struct ast_format format = {0};
1304
1305 int res = read(dev_ctx.ch_fd[c], buf, sizeof(buf));
1306 if (res <= 0) {
1307 ast_log(LOG_ERROR, "we got read error %i\n", res);
1308 return -1;
1309 } else if (res < RTP_HEADER_LEN) {
1310 ast_log(LOG_ERROR, "we got data smaller than header size\n");
1311 return -1;
1312 }
1313
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)) {
1317 return 0;
1318 }
1319
1320 if (rtp->payload_type == RTP_CN) {
1321 /* TODO: Handle Comfort Noise frames */
1322 ast_debug(1, "Dropping Comfort Noise frame\n");
1323 }
1324
1325 format = lantiq_map_rtptype_to_format(rtp->payload_type);
1326 frame.src = "TAPI";
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);
1332
1333 if(!ast_channel_trylock(pvt->owner)) {
1334 ast_queue_frame(pvt->owner, &frame);
1335 ast_channel_unlock(pvt->owner);
1336 }
1337
1338 return 0;
1339 }
1340
1341 static int accept_call(int c)
1342 {
1343 struct lantiq_pvt *pvt = &iflist[c];
1344
1345 ast_log(LOG_DEBUG, "accept call\n");
1346
1347 if (pvt->owner) {
1348 struct ast_channel *chan = pvt->owner;
1349
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;
1357 break;
1358 default:
1359 ast_log(LOG_WARNING, "entered unhandled state %s\n", ast_state2str(ast_channel_state(chan)));
1360 }
1361 }
1362
1363 return 0;
1364 }
1365
1366 static int lantiq_dev_event_hook(int c, int state)
1367 {
1368 ast_mutex_lock(&iflock);
1369
1370 ast_log(LOG_DEBUG, "on port %i detected event %s hook\n", c, state ? "on" : "off");
1371
1372 struct lantiq_pvt *pvt = &iflist[c];
1373
1374 int ret = -1;
1375 if (state) { /* going onhook */
1376 switch (iflist[c].channel_state) {
1377 case DIALING:
1378 ret = lantiq_end_dialing(c);
1379 break;
1380 case INCALL:
1381 ret = lantiq_end_call(c);
1382 break;
1383 }
1384
1385 iflist[c].channel_state = ONHOOK;
1386
1387 /* stop DSP data feed */
1388 lantiq_standby(c);
1389 led_off(dev_ctx.ch_led[c]);
1390
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");
1394 goto out;
1395 }
1396
1397 switch (iflist[c].channel_state) {
1398 case RINGING:
1399 ret = accept_call(c);
1400 led_blink(dev_ctx.ch_led[c], LED_SLOW_BLINK);
1401 break;
1402 default:
1403 iflist[c].channel_state = OFFHOOK;
1404 lantiq_play_tone(c, TAPI_TONE_LOCALE_DIAL_CODE);
1405 lantiq_reset_dtmfbuf(pvt);
1406 ret = 0;
1407 led_on(dev_ctx.ch_led[c]);
1408 break;
1409 }
1410
1411 }
1412
1413 out:
1414 ast_mutex_unlock(&iflock);
1415
1416 return ret;
1417 }
1418
1419 static void lantiq_reset_dtmfbuf(struct lantiq_pvt *pvt)
1420 {
1421 pvt->dtmfbuf[0] = '\0';
1422 pvt->dtmfbuf_len = 0;
1423 }
1424
1425 static void lantiq_dial(struct lantiq_pvt *pvt)
1426 {
1427 struct ast_channel *chan = NULL;
1428
1429 ast_mutex_lock(&iflock);
1430 ast_log(LOG_DEBUG, "user want's to dial %s.\n", pvt->dtmfbuf);
1431
1432 if (ast_exists_extension(NULL, pvt->context, pvt->dtmfbuf, 1, NULL)) {
1433 ast_debug(1, "found extension %s, dialing\n", pvt->dtmfbuf);
1434
1435 ast_verbose(VERBOSE_PREFIX_3 " extension exists, starting PBX %s\n", pvt->dtmfbuf);
1436
1437 chan = lantiq_channel(AST_STATE_UP, pvt->port_id, pvt->dtmfbuf, pvt->context, NULL);
1438 if (!chan) {
1439 ast_log(LOG_ERROR, "couldn't create channel\n");
1440 goto bailout;
1441 }
1442 ast_channel_tech_pvt_set(chan, pvt);
1443 pvt->owner = chan;
1444
1445 ast_setstate(chan, AST_STATE_RING);
1446 pvt->channel_state = INCALL;
1447
1448 pvt->call_setup_start = now();
1449 pvt->call_start = epoch();
1450
1451 if (ast_pbx_start(chan)) {
1452 ast_log(LOG_WARNING, " unable to start PBX on %s\n", ast_channel_name(chan));
1453 ast_hangup(chan);
1454 }
1455
1456 ast_channel_unlock(chan);
1457 } else {
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;
1461 }
1462
1463 lantiq_reset_dtmfbuf(pvt);
1464 bailout:
1465 ast_mutex_unlock(&iflock);
1466 }
1467
1468 static int lantiq_event_dial_timeout(const void* data)
1469 {
1470 ast_debug(1, "TAPI: lantiq_event_dial_timeout()\n");
1471
1472 struct lantiq_pvt *pvt = (struct lantiq_pvt *) data;
1473 pvt->dial_timer = -1;
1474
1475 if (! pvt->channel_state == ONHOOK) {
1476 lantiq_dial(pvt);
1477 } else {
1478 ast_debug(1, "TAPI: lantiq_event_dial_timeout(): dial timeout in state ONHOOK.\n");
1479 }
1480
1481 return 0;
1482 }
1483
1484 static int lantiq_send_digit(int c, char digit)
1485 {
1486 struct lantiq_pvt *pvt = &iflist[c];
1487
1488 struct ast_frame f = { .frametype = AST_FRAME_DTMF, .subclass.integer = digit };
1489
1490 if (pvt->owner) {
1491 ast_log(LOG_DEBUG, "Port %i transmitting digit \"%c\"\n", c, digit);
1492 return ast_queue_frame(pvt->owner, &f);
1493 } else {
1494 ast_debug(1, "Warning: lantiq_send_digit() without owner!\n");
1495 return -1;
1496 }
1497 }
1498
1499 static void lantiq_dev_event_digit(int c, char digit)
1500 {
1501 ast_mutex_lock(&iflock);
1502
1503 ast_log(LOG_DEBUG, "on port %i detected digit \"%c\"\n", c, digit);
1504
1505 struct lantiq_pvt *pvt = &iflist[c];
1506
1507 switch (pvt->channel_state) {
1508 case INCALL:
1509 lantiq_send_digit(c, digit);
1510 break;
1511 case OFFHOOK:
1512 pvt->channel_state = DIALING;
1513
1514 lantiq_play_tone(c, TAPI_TONE_LOCALE_NONE);
1515 led_blink(dev_ctx.ch_led[c], LED_SLOW_BLINK);
1516
1517 /* fall through */
1518 case DIALING:
1519 if (pvt->dtmfbuf_len < AST_MAX_EXTENSION - 1) {
1520 pvt->dtmfbuf[pvt->dtmfbuf_len] = digit;
1521 pvt->dtmfbuf[++pvt->dtmfbuf_len] = '\0';
1522 } else {
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;
1527 break;
1528 }
1529
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);
1534 } else {
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);
1537 }
1538 break;
1539 default:
1540 ast_log(LOG_ERROR, "don't know what to do in unhandled state\n");
1541 break;
1542 }
1543
1544 ast_mutex_unlock(&iflock);
1545 return;
1546 }
1547
1548 static void lantiq_dev_event_handler(void)
1549 {
1550 IFX_TAPI_EVENT_t event;
1551 unsigned int i;
1552
1553 for (i = 0; i < dev_ctx.channels ; i++) {
1554 ast_mutex_lock(&iflock);
1555
1556 memset (&event, 0, sizeof(event));
1557 event.ch = i;
1558 if (ioctl(dev_ctx.dev_fd, IFX_TAPI_EVENT_GET, &event)) {
1559 ast_mutex_unlock(&iflock);
1560 continue;
1561 }
1562 if (event.id == IFX_TAPI_EVENT_NONE) {
1563 ast_mutex_unlock(&iflock);
1564 continue;
1565 }
1566
1567 ast_mutex_unlock(&iflock);
1568
1569 switch(event.id) {
1570 case IFX_TAPI_EVENT_FXS_ONHOOK:
1571 lantiq_dev_event_hook(i, 1);
1572 break;
1573 case IFX_TAPI_EVENT_FXS_OFFHOOK:
1574 lantiq_dev_event_hook(i, 0);
1575 break;
1576 case IFX_TAPI_EVENT_DTMF_DIGIT:
1577 lantiq_dev_event_digit(i, (char)event.data.dtmf.ascii);
1578 break;
1579 case IFX_TAPI_EVENT_PULSE_DIGIT:
1580 if (event.data.pulse.digit == 0xB) {
1581 lantiq_dev_event_digit(i, '0');
1582 } else {
1583 lantiq_dev_event_digit(i, '0' + (char)event.data.pulse.digit);
1584 }
1585 break;
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:
1589 break;
1590 default:
1591 ast_log(LOG_ERROR, "Unknown TAPI event %08X. Restarting Asterisk...\n", event.id);
1592 sleep(1);
1593 ast_cli_command(-1, "core restart now");
1594 break;
1595 }
1596 }
1597 }
1598
1599 static void * lantiq_events_monitor(void *data)
1600 {
1601 ast_verbose("TAPI thread started\n");
1602
1603 struct pollfd fds[TAPI_AUDIO_PORT_NUM_MAX + 1];
1604 int c;
1605
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;
1611 }
1612
1613 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
1614 for (;;) {
1615 if (poll(fds, dev_ctx.channels + 1, 2000) <= 0) {
1616 continue;
1617 }
1618
1619 ast_mutex_lock(&monlock);
1620 if (fds[0].revents & POLLIN) {
1621 lantiq_dev_event_handler();
1622 }
1623
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);
1627 break;
1628 }
1629 }
1630 ast_mutex_unlock(&monlock);
1631 }
1632
1633 return NULL;
1634 }
1635
1636 static int restart_monitor(void)
1637 {
1638 /* If we're supposed to be stopped -- stay stopped */
1639 if (monitor_thread == AST_PTHREADT_STOP)
1640 return 0;
1641
1642 ast_mutex_lock(&monlock);
1643
1644 if (monitor_thread == pthread_self()) {
1645 ast_mutex_unlock(&monlock);
1646 ast_log(LOG_WARNING, "Cannot kill myself\n");
1647 return -1;
1648 }
1649
1650 if (monitor_thread != AST_PTHREADT_NULL) {
1651 /* Wake up the thread */
1652 pthread_kill(monitor_thread, SIGURG);
1653 } else {
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");
1658 return -1;
1659 }
1660 }
1661 ast_mutex_unlock(&monlock);
1662
1663 return 0;
1664 }
1665
1666 static void lantiq_cleanup(void)
1667 {
1668 int c;
1669
1670 if (dev_ctx.dev_fd < 0) {
1671 return;
1672 }
1673
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");
1677 }
1678
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");
1681 }
1682
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");
1685 }
1686 led_off(dev_ctx.ch_led[c]);
1687 }
1688
1689 if (ioctl(dev_ctx.dev_fd, IFX_TAPI_DEV_STOP, 0)) {
1690 ast_log(LOG_WARNING, "IFX_TAPI_DEV_STOP ioctl failed\n");
1691 }
1692
1693 close(dev_ctx.dev_fd);
1694 dev_ctx.dev_fd = -1;
1695 led_off(dev_ctx.voip_led);
1696 }
1697
1698 static int unload_module(void)
1699 {
1700 int c;
1701
1702 ast_channel_unregister(&lantiq_tech);
1703
1704 if (ast_mutex_lock(&iflock)) {
1705 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1706 return -1;
1707 }
1708 for (c = 0; c < dev_ctx.channels ; c++) {
1709 if (iflist[c].owner)
1710 ast_softhangup(iflist[c].owner, AST_SOFTHANGUP_APPUNLOAD);
1711 }
1712 ast_mutex_unlock(&iflock);
1713
1714 if (ast_mutex_lock(&monlock)) {
1715 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1716 return -1;
1717 }
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;
1721 pthread_cancel(th);
1722 pthread_kill(th, SIGURG);
1723 ast_mutex_unlock(&monlock);
1724 pthread_join(th, NULL);
1725 } else {
1726 monitor_thread = AST_PTHREADT_STOP;
1727 ast_mutex_unlock(&monlock);
1728 }
1729
1730 ast_sched_context_destroy(sched);
1731 ast_mutex_destroy(&iflock);
1732 ast_mutex_destroy(&monlock);
1733
1734 lantiq_cleanup();
1735 ast_free(iflist);
1736
1737 return 0;
1738 }
1739
1740 static struct lantiq_pvt *lantiq_init_pvt(struct lantiq_pvt *pvt)
1741 {
1742 if (pvt) {
1743 pvt->owner = NULL;
1744 pvt->port_id = -1;
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;
1753 pvt->jb_size = 0;
1754 pvt->jb_underflow = 0;
1755 pvt->jb_overflow = 0;
1756 pvt->jb_delay = 0;
1757 pvt->jb_invalid = 0;
1758 } else {
1759 ast_log(LOG_ERROR, "unable to clear pvt structure\n");
1760 }
1761
1762 return pvt;
1763 }
1764
1765 static int lantiq_create_pvts(void)
1766 {
1767 int i;
1768
1769 iflist = ast_calloc(1, sizeof(struct lantiq_pvt) * dev_ctx.channels);
1770
1771 if (!iflist) {
1772 ast_log(LOG_ERROR, "unable to allocate memory\n");
1773 return -1;
1774 }
1775
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);
1782 } else {
1783 snprintf(iflist[i].context, AST_MAX_CONTEXT, "default");
1784 }
1785 }
1786 return 0;
1787 }
1788
1789 static int lantiq_setup_rtp(int c)
1790 {
1791 /* Configure RTP payload type tables */
1792 IFX_TAPI_PKT_RTP_PT_CFG_t rtpPTConf;
1793
1794 memset((char*)&rtpPTConf, '\0', sizeof(rtpPTConf));
1795
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;
1807
1808 int ret;
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);
1811 return -1;
1812 }
1813
1814 return 0;
1815 }
1816
1817 static int load_module(void)
1818 {
1819 struct ast_config *cfg;
1820 struct ast_variable *v;
1821 int txgain = 0;
1822 int rxgain = 0;
1823 int wlec_type = 0;
1824 int wlec_nlp = 0;
1825 int wlec_nbfe = 0;
1826 int wlec_nbne = 0;
1827 int wlec_wbne = 0;
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 };
1842 int c;
1843 struct ast_format format;
1844
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;
1848 }
1849
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));
1856
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]);
1861
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;
1865 }
1866
1867 /* We *must* have a config file otherwise stop immediately */
1868 if (!cfg) {
1869 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1870 return AST_MODULE_LOAD_DECLINE;
1871 }
1872
1873 if (ast_mutex_lock(&iflock)) {
1874 ast_log(LOG_ERROR, "Unable to lock interface list.\n");
1875 goto cfg_error;
1876 }
1877
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);
1883 goto cfg_error_il;
1884 }
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;
1896 } else {
1897 ast_log(LOG_ERROR, "Unknown per_channel_context value '%s'. Try 'on' or 'off'.\n", v->value);
1898 goto cfg_error_il;
1899 }
1900 }
1901 }
1902
1903 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
1904 if (!strcasecmp(v->name, "rxgain")) {
1905 rxgain = atoi(v->value);
1906 if (!rxgain) {
1907 rxgain = 0;
1908 ast_log(LOG_WARNING, "Invalid rxgain: %s, using default.\n", v->value);
1909 }
1910 } else if (!strcasecmp(v->name, "txgain")) {
1911 txgain = atoi(v->value);
1912 if (!txgain) {
1913 txgain = 0;
1914 ast_log(LOG_WARNING, "Invalid txgain: %s, using default.\n", v->value);
1915 }
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);
1923 }
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);
1932 }
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;
1939 } else {
1940 wlec_type = IFX_TAPI_WLEC_TYPE_OFF;
1941 ast_log(LOG_ERROR, "Unknown echo cancellation type '%s'\n", v->value);
1942 goto cfg_error_il;
1943 }
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;
1949 } else {
1950 ast_log(LOG_ERROR, "Unknown echo cancellation nlp '%s'\n", v->value);
1951 goto cfg_error_il;
1952 }
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;
1964 }
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);
1973 }
1974 } else {
1975 ast_log(LOG_ERROR, "Unknown jitter buffer type '%s'\n", v->value);
1976 goto cfg_error_il;
1977 }
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;
1985 } else {
1986 ast_log(LOG_ERROR, "Unknown jitter buffer packet adaptation type '%s'\n", v->value);
1987 goto cfg_error_il;
1988 }
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;
2005 } else {
2006 ast_log(LOG_ERROR, "Unknown caller id type '%s'\n", v->value);
2007 goto cfg_error_il;
2008 }
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;
2018 } else {
2019 ast_log(LOG_ERROR, "Unknown voice activity detection value '%s'\n", v->value);
2020 goto cfg_error_il;
2021 }
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);
2028 }
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;
2036 } else {
2037 ast_log(LOG_ERROR, "Unknown tone_generator value '%s'. Try 'integrated', 'asterisk' or 'media'.\n", v->value);
2038 goto cfg_error_il;
2039 }
2040 }
2041 }
2042
2043 lantiq_create_pvts();
2044
2045 ast_mutex_unlock(&iflock);
2046 ast_config_destroy(cfg);
2047
2048 if (!(sched = ast_sched_context_create())) {
2049 ast_log(LOG_ERROR, "Unable to create scheduler context\n");
2050 goto load_error;
2051 }
2052
2053 if (ast_sched_start_thread(sched)) {
2054 ast_log(LOG_ERROR, "Unable to create scheduler context thread\n");
2055 goto load_error_st;
2056 }
2057
2058 if (ast_channel_register(&lantiq_tech)) {
2059 ast_log(LOG_ERROR, "Unable to register channel class 'Phone'\n");
2060 goto load_error_st;
2061 }
2062
2063 /* tapi */
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;
2072
2073 /* open device */
2074 dev_ctx.dev_fd = lantiq_dev_open(base_path, 0);
2075
2076 if (dev_ctx.dev_fd < 0) {
2077 ast_log(LOG_ERROR, "lantiq TAPI device open function failed\n");
2078 goto load_error_st;
2079 }
2080
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);
2084
2085 if (dev_ctx.ch_fd[c] < 0) {
2086 ast_log(LOG_ERROR, "lantiq TAPI channel %d open function failed\n", c);
2087 goto load_error_st;
2088 }
2089 snprintf(dev_ctx.ch_led[c], LED_NAME_LENGTH, "fxs%d", c + 1);
2090 }
2091
2092 if (lantiq_dev_firmware_download(dev_ctx.dev_fd, firmware_filename)) {
2093 ast_log(LOG_ERROR, "voice firmware download failed\n");
2094 goto load_error_st;
2095 }
2096
2097 if (ioctl(dev_ctx.dev_fd, IFX_TAPI_DEV_STOP, 0)) {
2098 ast_log(LOG_ERROR, "IFX_TAPI_DEV_STOP ioctl failed\n");
2099 goto load_error_st;
2100 }
2101
2102 memset(&dev_start, 0x0, sizeof(IFX_TAPI_DEV_START_CFG_t));
2103 dev_start.nMode = IFX_TAPI_INIT_MODE_VOICE_CODER;
2104
2105 /* Start TAPI */
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");
2108 goto load_error_st;
2109 }
2110
2111 tz = ast_get_indication_zone(NULL);
2112
2113 if (!tz) {
2114 ast_log(LOG_ERROR, "Unable to alloc tone zone\n");
2115 goto load_error_st;
2116 }
2117
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");
2122
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");
2125 goto load_error_st;
2126 }
2127
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);
2134 goto load_error_st;
2135 }
2136
2137 /* tones */
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);
2143 goto load_error_st;
2144 }
2145
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);
2151 goto load_error_st;
2152 }
2153
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);
2159 goto load_error_st;
2160 }
2161
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);
2167 goto load_error_st;
2168 }
2169
2170 /* ringing type */
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");
2177 goto load_error_st;
2178 }
2179
2180 /* ring cadence */
2181 IFX_char_t data[15] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2182 0x00, 0x00, 0x00, 0x00, 0x00,
2183 0x00, 0x00, 0x00, 0x00, 0x00 };
2184
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;
2189
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");
2192 goto load_error_st;
2193 }
2194
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;
2199
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);
2202 goto load_error_st;
2203 }
2204
2205 /* set line feed */
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);
2208 goto load_error_st;
2209 }
2210
2211 /* set volume */
2212 memset(&line_vol, 0, sizeof(line_vol));
2213 line_vol.nGainRx = rxgain;
2214 line_vol.nGainTx = txgain;
2215
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);
2218 goto load_error_st;
2219 }
2220
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;
2228
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);
2231 goto load_error_st;
2232 }
2233
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;
2243
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);
2246 goto load_error_st;
2247 }
2248
2249 /* Configure Caller ID type */
2250 memset(&cid_cfg, 0, sizeof(cid_cfg));
2251 cid_cfg.nStandard = cid_type;
2252
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);
2255 goto load_error_st;
2256 }
2257
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);
2261 goto load_error_st;
2262 }
2263
2264 /* Setup TAPI <-> internal RTP codec type mapping */
2265 if (lantiq_setup_rtp(c)) {
2266 goto load_error_st;
2267 }
2268
2269 /* Set initial hook status */
2270 iflist[c].channel_state = lantiq_get_hookstatus(c);
2271
2272 if (iflist[c].channel_state == UNKNOWN) {
2273 goto load_error_st;
2274 }
2275 }
2276
2277 /* make sure our device will be closed properly */
2278 ast_register_atexit(lantiq_cleanup);
2279
2280 restart_monitor();
2281 led_on(dev_ctx.voip_led);
2282 return AST_MODULE_LOAD_SUCCESS;
2283
2284 cfg_error_il:
2285 ast_mutex_unlock(&iflock);
2286 cfg_error:
2287 ast_config_destroy(cfg);
2288 return AST_MODULE_LOAD_DECLINE;
2289
2290 load_error_st:
2291 ast_sched_context_destroy(sched);
2292 load_error:
2293 unload_module();
2294 ast_free(iflist);
2295 return AST_MODULE_LOAD_FAILURE;
2296 }
2297
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
2302 );