19d422e52cf0c341a7c7be3c34e4bd81a802816e
[feed/telephony.git] / net / asterisk-13.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 #include <asterisk/format_cache.h>
81
82 /* Lantiq TAPI includes */
83 #include <drv_tapi/drv_tapi_io.h>
84 #include <drv_vmmc/vmmc_io.h>
85
86 #define TAPI_AUDIO_PORT_NUM_MAX 2
87
88 /* Tapi predefined tones 0 to 31 */
89 #define TAPI_TONE_LOCALE_NONE 0
90 //#define TAPI_TONE_LOCALE_DIAL_CODE 25
91 //#define TAPI_TONE_LOCALE_RINGING_CODE 26
92 //#define TAPI_TONE_LOCALE_BUSY_CODE 27
93 //#define TAPI_TONE_LOCALE_CONGESTION_CODE 27
94
95 /* Tapi custom tones 32 to 256 */
96 #define TAPI_TONE_LOCALE_DIAL_CODE 32
97 #define TAPI_TONE_LOCALE_RINGING_CODE 33
98 #define TAPI_TONE_LOCALE_BUSY_CODE 34
99 #define TAPI_TONE_LOCALE_CONGESTION_CODE 35
100
101 #define LANTIQ_CONTEXT_PREFIX "lantiq"
102 #define DEFAULT_INTERDIGIT_TIMEOUT 4000
103 #define G723_HIGH_RATE 1
104 #define LED_NAME_LENGTH 32
105
106 static const char config[] = "lantiq.conf";
107
108 static char firmware_filename[PATH_MAX] = "/lib/firmware/ifx_firmware.bin";
109 static char bbd_filename[PATH_MAX] = "/lib/firmware/ifx_bbd_fxs.bin";
110 static char base_path[PATH_MAX] = "/dev/vmmc";
111 static int per_channel_context = 0;
112
113 /* tone generator types */
114 enum tone_generator_t {
115 TONE_INTEGRATED, /* tapi tone generator */
116 TONE_ASTERISK, /* asterisk tone generator where possible */
117 TONE_MEDIA /* media tone where possible */
118 };
119
120 /* tone generator (default is integraded) */
121 static enum tone_generator_t tone_generator = TONE_INTEGRATED;
122
123 /* tone zones for dial, ring, busy and congestion */
124 struct ast_tone_zone_sound *ts_dial;
125 struct ast_tone_zone_sound *ts_ring;
126 struct ast_tone_zone_sound *ts_busy;
127 struct ast_tone_zone_sound *ts_congestion;
128
129 /*
130 * The private structures of the Phone Jack channels are linked for selecting
131 * outgoing channels.
132 */
133 enum channel_state {
134 ONHOOK,
135 OFFHOOK,
136 DIALING,
137 INCALL,
138 CALL_ENDED,
139 RINGING,
140 UNKNOWN
141 };
142
143 static struct lantiq_pvt {
144 struct ast_channel *owner; /* Channel we belong to, possibly NULL */
145 int port_id; /* Port number of this object, 0..n */
146 int channel_state;
147 char context[AST_MAX_CONTEXT]; /* this port's dialplan context */
148 int dial_timer; /* timer handle for autodial timeout */
149 char dtmfbuf[AST_MAX_EXTENSION]; /* buffer holding dialed digits */
150 int dtmfbuf_len; /* lenght of dtmfbuf */
151 int rtp_timestamp; /* timestamp for RTP packets */
152 int ptime; /* Codec base ptime */
153 uint16_t rtp_seqno; /* Sequence nr for RTP packets */
154 uint32_t call_setup_start; /* Start of dialling in ms */
155 uint32_t call_setup_delay; /* time between ^ and 1st ring in ms */
156 uint32_t call_start; /* time we started dialling / answered */
157 uint32_t call_answer; /* time the callee answered our call */
158 uint16_t jb_size; /* Jitter buffer size */
159 uint32_t jb_underflow; /* Jitter buffer injected samples */
160 uint32_t jb_overflow; /* Jitter buffer dropped samples */
161 uint16_t jb_delay; /* Jitter buffer: playout delay */
162 uint16_t jb_invalid; /* Jitter buffer: Nr. of invalid packets */
163 } *iflist = NULL;
164
165 static struct lantiq_ctx {
166 int dev_fd;
167 int channels;
168 int ch_fd[TAPI_AUDIO_PORT_NUM_MAX];
169 char voip_led[LED_NAME_LENGTH]; /* VOIP LED name */
170 char ch_led[TAPI_AUDIO_PORT_NUM_MAX][LED_NAME_LENGTH]; /* FXS LED names */
171 int interdigit_timeout; /* Timeout in ms between dialed digits */
172 } dev_ctx;
173
174 static int ast_digit_begin(struct ast_channel *ast, char digit);
175 static int ast_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
176 static int ast_lantiq_call(struct ast_channel *ast, const char *dest, int timeout);
177 static int ast_lantiq_hangup(struct ast_channel *ast);
178 static int ast_lantiq_answer(struct ast_channel *ast);
179 static struct ast_frame *ast_lantiq_read(struct ast_channel *ast);
180 static int ast_lantiq_write(struct ast_channel *ast, struct ast_frame *frame);
181 static struct ast_frame *ast_lantiq_exception(struct ast_channel *ast);
182 static int ast_lantiq_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
183 static int ast_lantiq_fixup(struct ast_channel *old, struct ast_channel *new);
184 static struct ast_channel *ast_lantiq_requester(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assigned_ids, const struct ast_channel *requestor, const char *data, int *cause);
185 static int ast_lantiq_devicestate(const char *data);
186 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *args, char *buf, size_t buflen);
187 static void lantiq_jb_get_stats(int c);
188 static struct ast_format *lantiq_map_rtptype_to_format(uint8_t rtptype);
189 static uint8_t lantiq_map_format_to_rtptype(const struct ast_format *format);
190 static int lantiq_conf_enc(int c, const struct ast_format *format);
191 static void lantiq_reset_dtmfbuf(struct lantiq_pvt *pvt);
192
193 static struct ast_channel_tech lantiq_tech = {
194 .type = "TAPI",
195 .description = "Lantiq TAPI Telephony API Driver",
196 .send_digit_begin = ast_digit_begin,
197 .send_digit_end = ast_digit_end,
198 .call = ast_lantiq_call,
199 .hangup = ast_lantiq_hangup,
200 .answer = ast_lantiq_answer,
201 .read = ast_lantiq_read,
202 .write = ast_lantiq_write,
203 .exception = ast_lantiq_exception,
204 .indicate = ast_lantiq_indicate,
205 .fixup = ast_lantiq_fixup,
206 .requester = ast_lantiq_requester,
207 .devicestate = ast_lantiq_devicestate,
208 .func_channel_read = acf_channel_read
209 };
210
211 /* Protect the interface list (of lantiq_pvt's) */
212 AST_MUTEX_DEFINE_STATIC(iflock);
213
214 /*
215 * Protect the monitoring thread, so only one process can kill or start it, and
216 * not when it's doing something critical.
217 */
218 AST_MUTEX_DEFINE_STATIC(monlock);
219
220 /* The scheduling context */
221 struct ast_sched_context *sched;
222
223 /*
224 * This is the thread for the monitor which checks for input on the channels
225 * which are not currently in use.
226 */
227 static pthread_t monitor_thread = AST_PTHREADT_NULL;
228
229
230 #define WORDS_BIGENDIAN
231 /* struct taken from some GPLed code by Mike Borella */
232 typedef struct rtp_header
233 {
234 #if defined(WORDS_BIGENDIAN)
235 uint8_t version:2, padding:1, extension:1, csrc_count:4;
236 uint8_t marker:1, payload_type:7;
237 #else
238 uint8_t csrc_count:4, extension:1, padding:1, version:2;
239 uint8_t payload_type:7, marker:1;
240 #endif
241 uint16_t seqno;
242 uint32_t timestamp;
243 uint32_t ssrc;
244 } rtp_header_t;
245 #define RTP_HEADER_LEN 12
246 #define RTP_BUFFER_LEN 512
247 /* Internal RTP payload types - standard */
248 #define RTP_PCMU 0
249 #define RTP_G723_63 4
250 #define RTP_PCMA 8
251 #define RTP_G722 9
252 #define RTP_CN 13
253 #define RTP_G729 18
254 /* Internal RTP payload types - custom */
255 #define RTP_G7221 100
256 #define RTP_G726 101
257 #define RTP_ILBC 102
258 #define RTP_SLIN8 103
259 #define RTP_SLIN16 104
260 #define RTP_SIREN7 105
261 #define RTP_G723_53 106
262
263
264 /* LED Control. Taken with modifications from SVD by Luca Olivetti <olivluca@gmail.com> */
265 #define LED_SLOW_BLINK 1000
266 #define LED_FAST_BLINK 100
267 static FILE *led_open(const char *led, char* sub)
268 {
269 char fname[100];
270
271 if (snprintf(fname, sizeof(fname), "/sys/class/leds/%s/%s", led, sub) >= sizeof(fname))
272 return NULL;
273 return fopen(fname, "r+");
274 }
275
276 static FILE *led_trigger(const char *led)
277 {
278 return led_open(led, "trigger");
279 }
280
281 static void led_delay(const char *led, int onoff, int msec)
282 {
283 FILE *fp = led_open(led, onoff ? "delay_on" : "delay_off");
284 if (fp) {
285 fprintf(fp,"%d\n",msec);
286 fclose(fp);
287 }
288 }
289
290 static void led_on(const char *led)
291 {
292 FILE *fp;
293
294 fp = led_trigger(led);
295 if (fp) {
296 fprintf(fp,"default-on\n");
297 fclose(fp);
298 }
299 }
300
301 static void led_off(const char *led)
302 {
303 FILE *fp;
304
305 fp = led_trigger(led);
306 if (fp) {
307 fprintf(fp,"none\n");
308 fclose(fp);
309 }
310 }
311
312 static void led_blink(const char *led, int period)
313 {
314 FILE *fp;
315
316 fp = led_trigger(led);
317 if (fp) {
318 fprintf(fp, "timer\n");
319 fclose(fp);
320 led_delay(led, 1, period/2);
321 led_delay(led, 0, period/2);
322 }
323 }
324
325 static uint32_t now(void) {
326 struct timespec ts;
327 clock_gettime(CLOCK_MONOTONIC, &ts);
328
329 uint64_t tmp = ts.tv_sec*1000 + (ts.tv_nsec/1000000);
330 return (uint32_t) tmp;
331 }
332
333 static uint32_t epoch(void) {
334 struct timeval tv;
335 gettimeofday(&tv, NULL);
336
337 return tv.tv_sec;
338 }
339
340 static int lantiq_dev_open(const char *dev_path, const int32_t ch_num)
341 {
342 char dev_name[PATH_MAX];
343 memset(dev_name, 0, sizeof(dev_name));
344 snprintf(dev_name, PATH_MAX, "%s%u%u", dev_path, 1, ch_num);
345 return open((const char*)dev_name, O_RDWR, 0644);
346 }
347
348 static void lantiq_ring(int c, int r, const char *cid, const char *name)
349 {
350 uint8_t status;
351
352 if (r) {
353 led_blink(dev_ctx.ch_led[c], LED_FAST_BLINK);
354 if (!cid) {
355 status = (uint8_t) ioctl(dev_ctx.ch_fd[c], IFX_TAPI_RING_START, 0);
356 } else {
357 IFX_TAPI_CID_MSG_t msg;
358 IFX_TAPI_CID_MSG_ELEMENT_t elements[3];
359 int count = 0;
360 time_t timestamp;
361 struct tm *tm;
362
363 elements[count].string.elementType = IFX_TAPI_CID_ST_CLI;
364 elements[count].string.len = strlen(cid);
365 if (elements[count].string.len > IFX_TAPI_CID_MSG_LEN_MAX) {
366 elements[count].string.len = IFX_TAPI_CID_MSG_LEN_MAX;
367 }
368 strncpy((char *)elements[count].string.element, cid, IFX_TAPI_CID_MSG_LEN_MAX);
369 elements[count].string.element[IFX_TAPI_CID_MSG_LEN_MAX-1] = '\0';
370 count++;
371
372 if (name) {
373 elements[count].string.elementType = IFX_TAPI_CID_ST_NAME;
374 elements[count].string.len = strlen(name);
375 if (elements[count].string.len > IFX_TAPI_CID_MSG_LEN_MAX) {
376 elements[count].string.len = IFX_TAPI_CID_MSG_LEN_MAX;
377 }
378 strncpy((char *)elements[count].string.element, name, IFX_TAPI_CID_MSG_LEN_MAX);
379 elements[count].string.element[IFX_TAPI_CID_MSG_LEN_MAX-1] = '\0';
380 count++;
381 }
382
383 if ((time(&timestamp) != -1) && ((tm=localtime(&timestamp)) != NULL)) {
384 elements[count].date.elementType = IFX_TAPI_CID_ST_DATE;
385 elements[count].date.day = tm->tm_mday;
386 elements[count].date.month = tm->tm_mon + 1;
387 elements[count].date.hour = tm->tm_hour;
388 elements[count].date.mn = tm->tm_min;
389 count++;
390 }
391
392 msg.txMode = IFX_TAPI_CID_HM_ONHOOK;
393 msg.messageType = IFX_TAPI_CID_MT_CSUP;
394 msg.message = elements;
395 msg.nMsgElements = count;
396
397 status = (uint8_t) ioctl(dev_ctx.ch_fd[c], IFX_TAPI_CID_TX_SEQ_START, (IFX_int32_t) &msg);
398 }
399 } else {
400 status = (uint8_t) ioctl(dev_ctx.ch_fd[c], IFX_TAPI_RING_STOP, 0);
401 led_off(dev_ctx.ch_led[c]);
402 }
403
404 if (status) {
405 ast_log(LOG_ERROR, "%s ioctl failed\n",
406 (r ? "IFX_TAPI_RING_START" : "IFX_TAPI_RING_STOP"));
407 }
408 }
409
410 /* add a frequency to TAPE tone structure */
411 /* returns the TAPI frequency ID */
412 static int tapitone_add_freq (IFX_TAPI_TONE_t *tone, IFX_uint32_t freq) {
413 const int n=4; /* TAPI tone structure supports up to 4 frequencies */
414 int error=0;
415 int ret;
416 int i;
417
418 /* pointer array for freq's A, B, C, D */
419 IFX_uint32_t *freqarr[] = { &(tone->simple.freqA), &(tone->simple.freqB), &(tone->simple.freqC), &(tone->simple.freqD) };
420
421 /* pointer array for level's A, B, C, D */
422 IFX_int32_t *lvlarr[] = { &(tone->simple.levelA), &(tone->simple.levelB), &(tone->simple.levelC), &(tone->simple.levelD) };
423
424 /* array for freq IDs */
425 IFX_uint32_t retarr[] = { IFX_TAPI_TONE_FREQA, IFX_TAPI_TONE_FREQB, IFX_TAPI_TONE_FREQC, IFX_TAPI_TONE_FREQD };
426
427 /* determine if freq already set */
428 for (i = 0; i < n; i++) {
429 if(*freqarr[i] == freq) /* freq found */
430 break;
431 else if (i == n-1) /* last iteration */
432 error=1; /* not found */
433 }
434
435 /* write frequency if not already set */
436 if(error) {
437 error=0; /* reset error flag */
438 /* since freq is not set, write it into first free place */
439 for (i = 0; i < n; i++) {
440 if(!*freqarr[i]) { /* free place */
441 *freqarr[i] = freq; /* set freq */
442 *lvlarr[i] = -150; /* set volume level */
443 break;
444 } else if (i == n-1) /* last iteration */
445 error=1; /* no free place becaus maximum count of freq's is set */
446 }
447 }
448
449 /* set freq ID return value */
450 if (!freq || error)
451 ret = IFX_TAPI_TONE_FREQNONE;
452 else
453 ret = retarr[i];
454
455 return ret; /* freq ID */
456 }
457
458 /* convert asterisk playlist string to tapi tone structure */
459 /* based on ast_playtones_start() from indications.c of asterisk 13 */
460 static void playlist_to_tapitone (const char *playlst, IFX_uint32_t index, IFX_TAPI_TONE_t *tone)
461 {
462 char *s, *data = ast_strdupa(playlst);
463 char *stringp;
464 char *separator;
465 int i;
466
467 /* initialize tapi tone structure */
468 memset(tone, 0, sizeof(IFX_TAPI_TONE_t));
469 tone->simple.format = IFX_TAPI_TONE_TYPE_SIMPLE;
470 tone->simple.index = index;
471
472 stringp = data;
473
474 /* check if the data is separated with '|' or with ',' by default */
475 if (strchr(stringp,'|')) {
476 separator = "|";
477 } else {
478 separator = ",";
479 }
480
481 for ( i = 0; (s = strsep(&stringp, separator)) && !ast_strlen_zero(s) && i < IFX_TAPI_TONE_STEPS_MAX; i++) {
482 struct ast_tone_zone_part tone_data = {
483 .time = 0,
484 };
485
486 s = ast_strip(s);
487 if (s[0]=='!') {
488 s++;
489 }
490
491 if (ast_tone_zone_part_parse(s, &tone_data)) {
492 ast_log(LOG_ERROR, "Failed to parse tone part '%s'\n", s);
493 continue;
494 }
495
496 /* first tone must hava a cadence */
497 if (i==0 && !tone_data.time)
498 tone->simple.cadence[i] = 1000;
499 else
500 tone->simple.cadence[i] = tone_data.time;
501
502 /* check for modulation */
503 if (tone_data.modulate) {
504 tone->simple.modulation[i] = IFX_TAPI_TONE_MODULATION_ON;
505 tone->simple.modulation_factor = IFX_TAPI_TONE_MODULATION_FACTOR_90;
506 }
507
508 /* copy freq's to tapi tone structure */
509 /* a freq will implicitly skipped if it is zero */
510 tone->simple.frequencies[i] |= tapitone_add_freq(tone, tone_data.freq1);
511 tone->simple.frequencies[i] |= tapitone_add_freq(tone, tone_data.freq2);
512 }
513 }
514
515 static int lantiq_play_tone(int c, int t)
516 {
517 /* stop currently playing tone before starting new one */
518 ioctl(dev_ctx.ch_fd[c], IFX_TAPI_TONE_LOCAL_PLAY, TAPI_TONE_LOCALE_NONE);
519
520 /* do not handle stop tone twice */
521 if (t != TAPI_TONE_LOCALE_NONE) {
522 /* start new tone */
523 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_TONE_LOCAL_PLAY, t)) {
524 ast_log(LOG_ERROR, "IFX_TAPI_TONE_LOCAL_PLAY ioctl failed\n");
525 return -1;
526 }
527 }
528
529 return 0;
530 }
531
532 static enum channel_state lantiq_get_hookstatus(int port)
533 {
534 uint8_t status;
535
536 if (ioctl(dev_ctx.ch_fd[port], IFX_TAPI_LINE_HOOK_STATUS_GET, &status)) {
537 ast_log(LOG_ERROR, "IFX_TAPI_LINE_HOOK_STATUS_GET ioctl failed\n");
538 return UNKNOWN;
539 }
540
541 if (status) {
542 return OFFHOOK;
543 } else {
544 return ONHOOK;
545 }
546 }
547
548 static int
549 lantiq_dev_binary_buffer_create(const char *path, uint8_t **ppBuf, uint32_t *pBufSz)
550 {
551 FILE *fd;
552 struct stat file_stat;
553 int status = -1;
554
555 fd = fopen(path, "rb");
556 if (fd == NULL) {
557 ast_log(LOG_ERROR, "binary file %s open failed\n", path);
558 goto on_exit;
559 }
560
561 if (stat(path, &file_stat)) {
562 ast_log(LOG_ERROR, "file %s statistics get failed\n", path);
563 goto on_exit;
564 }
565
566 *ppBuf = malloc(file_stat.st_size);
567 if (*ppBuf == NULL) {
568 ast_log(LOG_ERROR, "binary file %s memory allocation failed\n", path);
569 goto on_exit;
570 }
571
572 if (fread (*ppBuf, sizeof(uint8_t), file_stat.st_size, fd) != file_stat.st_size) {
573 ast_log(LOG_ERROR, "file %s read failed\n", path);
574 status = -1;
575 goto on_exit;
576 }
577
578 *pBufSz = file_stat.st_size;
579 status = 0;
580
581 on_exit:
582 if (fd != NULL)
583 fclose(fd);
584
585 if (*ppBuf != NULL && status)
586 free(*ppBuf);
587
588 return status;
589 }
590
591 static int32_t lantiq_dev_firmware_download(int32_t fd, const char *path)
592 {
593 uint8_t *firmware = NULL;
594 uint32_t size = 0;
595 VMMC_IO_INIT vmmc_io_init;
596
597 ast_log(LOG_DEBUG, "loading firmware: \"%s\".\n", path);
598
599 if (lantiq_dev_binary_buffer_create(path, &firmware, &size))
600 return -1;
601
602 memset(&vmmc_io_init, 0, sizeof(VMMC_IO_INIT));
603 vmmc_io_init.pPRAMfw = firmware;
604 vmmc_io_init.pram_size = size;
605
606 if (ioctl(fd, FIO_FW_DOWNLOAD, &vmmc_io_init)) {
607 ast_log(LOG_ERROR, "FIO_FW_DOWNLOAD ioctl failed\n");
608 return -1;
609 }
610
611 if (firmware != NULL)
612 free(firmware);
613
614 return 0;
615 }
616
617 static const char *state_string(enum channel_state s)
618 {
619 switch (s) {
620 case ONHOOK: return "ONHOOK";
621 case OFFHOOK: return "OFFHOOK";
622 case DIALING: return "DIALING";
623 case INCALL: return "INCALL";
624 case CALL_ENDED: return "CALL_ENDED";
625 case RINGING: return "RINGING";
626 default: return "UNKNOWN";
627 }
628 }
629
630 static const char *control_string(int c)
631 {
632 switch (c) {
633 case AST_CONTROL_HANGUP: return "Other end has hungup";
634 case AST_CONTROL_RING: return "Local ring";
635 case AST_CONTROL_RINGING: return "Remote end is ringing";
636 case AST_CONTROL_ANSWER: return "Remote end has answered";
637 case AST_CONTROL_BUSY: return "Remote end is busy";
638 case AST_CONTROL_TAKEOFFHOOK: return "Make it go off hook";
639 case AST_CONTROL_OFFHOOK: return "Line is off hook";
640 case AST_CONTROL_CONGESTION: return "Congestion (circuits busy)";
641 case AST_CONTROL_FLASH: return "Flash hook";
642 case AST_CONTROL_WINK: return "Wink";
643 case AST_CONTROL_OPTION: return "Set a low-level option";
644 case AST_CONTROL_RADIO_KEY: return "Key Radio";
645 case AST_CONTROL_RADIO_UNKEY: return "Un-Key Radio";
646 case AST_CONTROL_PROGRESS: return "Remote end is making Progress";
647 case AST_CONTROL_PROCEEDING: return "Remote end is proceeding";
648 case AST_CONTROL_HOLD: return "Hold";
649 case AST_CONTROL_UNHOLD: return "Unhold";
650 case AST_CONTROL_SRCUPDATE: return "Media Source Update";
651 case AST_CONTROL_CONNECTED_LINE: return "Connected Line";
652 case AST_CONTROL_REDIRECTING: return "Redirecting";
653 case AST_CONTROL_INCOMPLETE: return "Incomplete";
654 case -1: return "Stop tone";
655 default: return "Unknown";
656 }
657 }
658
659 static int ast_lantiq_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
660 {
661 struct lantiq_pvt *pvt = ast_channel_tech_pvt(chan);
662
663 ast_verb(3, "phone indication \"%s\"\n", control_string(condition));
664
665 switch (condition) {
666 case -1:
667 {
668 lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_NONE);
669 return 0;
670 }
671 case AST_CONTROL_CONGESTION:
672 {
673 if (tone_generator == TONE_INTEGRATED)
674 lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_CONGESTION_CODE);
675 else
676 ast_playtones_start(chan, 0, ts_congestion->data, 1);
677
678 return 0;
679 }
680 case AST_CONTROL_BUSY:
681 {
682 if (tone_generator == TONE_INTEGRATED)
683 lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_BUSY_CODE);
684 else
685 ast_playtones_start(chan, 0, ts_busy->data, 1);
686
687 return 0;
688 }
689 case AST_CONTROL_RINGING:
690 case AST_CONTROL_PROGRESS:
691 {
692 pvt->call_setup_delay = now() - pvt->call_setup_start;
693
694 if (tone_generator == TONE_INTEGRATED)
695 lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_RINGING_CODE);
696 else if (tone_generator == TONE_ASTERISK) /* do nothing if TONE_MEDIA is set */
697 ast_playtones_start(chan, 0, ts_ring->data, 1);
698
699 return 0;
700 }
701 default:
702 {
703 /* -1 lets asterisk generate the tone */
704 return -1;
705 }
706 }
707 }
708
709 static int ast_lantiq_fixup(struct ast_channel *old, struct ast_channel *new)
710 {
711 ast_log(LOG_DEBUG, "entering... no code here...\n");
712 return 0;
713 }
714
715 static int ast_digit_begin(struct ast_channel *chan, char digit)
716 {
717 /* TODO: Modify this callback to let Asterisk support controlling the length of DTMF */
718 ast_log(LOG_DEBUG, "entering... no code here...\n");
719 return 0;
720 }
721
722 static int ast_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
723 {
724 ast_log(LOG_DEBUG, "entering... no code here...\n");
725 return 0;
726 }
727
728 static int ast_lantiq_call(struct ast_channel *ast, const char *dest, int timeout)
729 {
730 int res = 0;
731 struct lantiq_pvt *pvt;
732
733 /* lock to prevent simultaneous access with do_monitor thread processing */
734 ast_mutex_lock(&iflock);
735
736 pvt = ast_channel_tech_pvt(ast);
737 ast_log(LOG_DEBUG, "state: %s\n", state_string(pvt->channel_state));
738
739 if (pvt->channel_state == ONHOOK) {
740 struct ast_party_id connected_id = ast_channel_connected_effective_id(ast);
741 const char *cid = connected_id.number.valid ? connected_id.number.str : NULL;
742 const char *name = connected_id.name.valid ? connected_id.name.str : NULL;
743
744 ast_log(LOG_DEBUG, "port %i is ringing\n", pvt->port_id);
745 ast_log(LOG_DEBUG, "port %i CID: %s\n", pvt->port_id, cid ? cid : "none");
746 ast_log(LOG_DEBUG, "port %i NAME: %s\n", pvt->port_id, name ? name : "none");
747
748 lantiq_ring(pvt->port_id, 1, cid, name);
749 pvt->channel_state = RINGING;
750
751 ast_setstate(ast, AST_STATE_RINGING);
752 ast_queue_control(ast, AST_CONTROL_RINGING);
753 } else {
754 ast_log(LOG_DEBUG, "port %i is busy\n", pvt->port_id);
755 ast_setstate(ast, AST_STATE_BUSY);
756 ast_queue_control(ast, AST_CONTROL_BUSY);
757 res = -1;
758 }
759
760 ast_mutex_unlock(&iflock);
761
762 return res;
763 }
764
765 static int ast_lantiq_hangup(struct ast_channel *ast)
766 {
767 struct lantiq_pvt *pvt;
768
769 /* lock to prevent simultaneous access with do_monitor thread processing */
770 ast_mutex_lock(&iflock);
771
772 pvt = ast_channel_tech_pvt(ast);
773 ast_log(LOG_DEBUG, "state: %s\n", state_string(pvt->channel_state));
774
775 if (ast_channel_state(ast) == AST_STATE_RINGING) {
776 ast_debug(1, "channel state is RINGING\n");
777 }
778
779 switch (pvt->channel_state) {
780 case RINGING:
781 case ONHOOK:
782 lantiq_ring(pvt->port_id, 0, NULL, NULL);
783 pvt->channel_state = ONHOOK;
784 break;
785 default:
786 ast_log(LOG_DEBUG, "we were hung up, play busy tone\n");
787 pvt->channel_state = CALL_ENDED;
788 lantiq_play_tone(pvt->port_id, TAPI_TONE_LOCALE_BUSY_CODE);
789 }
790
791 lantiq_jb_get_stats(pvt->port_id);
792
793 ast_setstate(ast, AST_STATE_DOWN);
794 ast_module_unref(ast_module_info->self);
795 ast_channel_tech_pvt_set(ast, NULL);
796 pvt->owner = NULL;
797
798 ast_mutex_unlock(&iflock);
799
800 return 0;
801 }
802
803 static int ast_lantiq_answer(struct ast_channel *ast)
804 {
805 struct lantiq_pvt *pvt = ast_channel_tech_pvt(ast);
806
807 ast_log(LOG_DEBUG, "Remote end has answered call.\n");
808
809 if (lantiq_conf_enc(pvt->port_id, ast_channel_writeformat(ast)))
810 return -1;
811
812 pvt->call_answer = epoch();
813
814 return 0;
815 }
816
817 static struct ast_frame * ast_lantiq_read(struct ast_channel *ast)
818 {
819 ast_log(LOG_DEBUG, "entering... no code here...\n");
820 return NULL;
821 }
822
823 /* create asterisk format from rtp payload type */
824 static struct ast_format *lantiq_map_rtptype_to_format(uint8_t rtptype)
825 {
826 struct ast_format *format = NULL;
827
828 switch (rtptype) {
829 case RTP_PCMU: format = ast_format_ulaw; break;
830 case RTP_PCMA: format = ast_format_alaw; break;
831 case RTP_G722: format = ast_format_g722; break;
832 case RTP_G726: format = ast_format_g726; break;
833 case RTP_SLIN8: format = ast_format_slin; break;
834 case RTP_SLIN16: format = ast_format_slin16; break;
835 case RTP_ILBC: format = ast_format_ilbc; break;
836 case RTP_SIREN7: format = ast_format_siren7; break;
837 case RTP_G723_63: format = ast_format_g723; break;
838 case RTP_G723_53: format = ast_format_g723; break;
839 case RTP_G729: format = ast_format_g729; break;
840 default:
841 {
842 ast_log(LOG_ERROR, "unsupported rtptype received is 0x%x, forcing ulaw\n", (unsigned) rtptype);
843 format = ast_format_ulaw;
844 }
845 }
846
847 return format;
848 }
849
850 /* create rtp payload type from asterisk format */
851 static uint8_t lantiq_map_format_to_rtptype(const struct ast_format *format)
852 {
853 uint8_t rtptype = 0;
854
855 if (ast_format_cmp(format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL)
856 rtptype = RTP_PCMU;
857 else if (ast_format_cmp(format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL)
858 rtptype = RTP_PCMA;
859 else if (ast_format_cmp(format, ast_format_g722) == AST_FORMAT_CMP_EQUAL)
860 rtptype = RTP_G722;
861 else if (ast_format_cmp(format, ast_format_g726) == AST_FORMAT_CMP_EQUAL)
862 rtptype = RTP_G726;
863 else if (ast_format_cmp(format, ast_format_slin) == AST_FORMAT_CMP_EQUAL)
864 rtptype = RTP_SLIN8;
865 else if (ast_format_cmp(format, ast_format_slin16) == AST_FORMAT_CMP_EQUAL)
866 rtptype = RTP_SLIN16;
867 else if (ast_format_cmp(format, ast_format_ilbc) == AST_FORMAT_CMP_EQUAL)
868 rtptype = RTP_ILBC;
869 else if (ast_format_cmp(format, ast_format_siren7) == AST_FORMAT_CMP_EQUAL)
870 rtptype = RTP_SIREN7;
871 else if (ast_format_cmp(format, ast_format_g723) == AST_FORMAT_CMP_EQUAL)
872 #if defined G723_HIGH_RATE
873 rtptype = RTP_G723_63;
874 #else
875 rtptype = RTP_G723_53;
876 #endif
877 else if (ast_format_cmp(format, ast_format_g729) == AST_FORMAT_CMP_EQUAL)
878 rtptype = RTP_G729;
879 else {
880 ast_log(LOG_ERROR, "unsupported format %s, forcing ulaw\n", ast_format_get_name(format));
881 rtptype = RTP_PCMU;
882 }
883
884 return rtptype;
885 }
886
887 static int lantiq_conf_enc(int c, const struct ast_format *format)
888 {
889 /* Configure encoder before starting RTP session */
890 IFX_TAPI_ENC_CFG_t enc_cfg;
891
892 memset(&enc_cfg, 0, sizeof(IFX_TAPI_ENC_CFG_t));
893
894 if (ast_format_cmp(format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL) {
895 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_MLAW;
896 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_20;
897 iflist[c].ptime = 10;
898 } else if (ast_format_cmp(format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) {
899 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_ALAW;
900 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_20;
901 iflist[c].ptime = 10;
902 } else if (ast_format_cmp(format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) {
903 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_G722_64;
904 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_20;
905 iflist[c].ptime = 20;
906 } else if (ast_format_cmp(format, ast_format_g726) == AST_FORMAT_CMP_EQUAL) {
907 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_G726_32;
908 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_20;
909 iflist[c].ptime = 10;
910 } else if (ast_format_cmp(format, ast_format_slin) == AST_FORMAT_CMP_EQUAL) {
911 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_LIN16_8;
912 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_20;
913 iflist[c].ptime = 10;
914 } else if (ast_format_cmp(format, ast_format_slin16) == AST_FORMAT_CMP_EQUAL) {
915 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_LIN16_16;
916 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_10;
917 iflist[c].ptime = 10;
918 } else if (ast_format_cmp(format, ast_format_ilbc) == AST_FORMAT_CMP_EQUAL) {
919 /* iLBC 15.2kbps is currently unsupported by Asterisk */
920 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_ILBC_133;
921 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_30;
922 iflist[c].ptime = 30;
923 } else if (ast_format_cmp(format, ast_format_siren7) == AST_FORMAT_CMP_EQUAL) {
924 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_G7221_32;
925 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_20;
926 iflist[c].ptime = 20;
927 } else if (ast_format_cmp(format, ast_format_g723) == AST_FORMAT_CMP_EQUAL) {
928 #if defined G723_HIGH_RATE
929 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_G723_63;
930 #else
931 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_G723_53;
932 #endif
933 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_30;
934 iflist[c].ptime = 30;
935 } else if (ast_format_cmp(format, ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
936 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_G729;
937 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_20;
938 iflist[c].ptime = 10;
939 } else {
940 ast_log(LOG_ERROR, "unsupported format %s, forcing ulaw\n", ast_format_get_name(format));
941 enc_cfg.nEncType = IFX_TAPI_COD_TYPE_MLAW;
942 enc_cfg.nFrameLen = IFX_TAPI_COD_LENGTH_20;
943 iflist[c].ptime = 10;
944 }
945
946 ast_log(LOG_DEBUG, "Configuring encoder to use TAPI codec type %d (%s) on channel %i\n", enc_cfg.nEncType, ast_format_get_name(format), c);
947
948 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_ENC_CFG_SET, &enc_cfg)) {
949 ast_log(LOG_ERROR, "IFX_TAPI_ENC_CFG_SET %d failed\n", c);
950 }
951
952 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_ENC_START, 0)) {
953 ast_log(LOG_ERROR, "IFX_TAPI_ENC_START ioctl failed\n");
954 }
955
956 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_DEC_START, 0)) {
957 ast_log(LOG_ERROR, "IFX_TAPI_DEC_START ioctl failed\n");
958 }
959
960 return 0;
961 }
962
963 static int ast_lantiq_write(struct ast_channel *ast, struct ast_frame *frame)
964 {
965 char buf[RTP_BUFFER_LEN];
966 rtp_header_t *rtp_header = (rtp_header_t *) buf;
967 struct lantiq_pvt *pvt = ast_channel_tech_pvt(ast);
968 int ret;
969 uint8_t rtptype;
970 int subframes, subframes_rtp, length, samples;
971 char *head, *tail;
972
973 if(frame->frametype != AST_FRAME_VOICE) {
974 ast_log(LOG_DEBUG, "unhandled frame type\n");
975 return 0;
976 }
977
978 if (frame->datalen == 0) {
979 ast_log(LOG_DEBUG, "we've been prodded\n");
980 return 0;
981 }
982
983 /* get rtp payload type */
984 rtptype = lantiq_map_format_to_rtptype(frame->subclass.format);
985
986 rtp_header->version = 2;
987 rtp_header->padding = 0;
988 rtp_header->extension = 0;
989 rtp_header->csrc_count = 0;
990 rtp_header->marker = 0;
991 rtp_header->ssrc = 0;
992 rtp_header->payload_type = rtptype;
993
994 subframes = (iflist[pvt->port_id].ptime + frame->len - 1) / iflist[pvt->port_id].ptime; /* number of subframes in AST frame */
995 if (subframes == 0)
996 subframes = 1;
997
998 subframes_rtp = (RTP_BUFFER_LEN - RTP_HEADER_LEN) * subframes / frame->datalen; /* how many frames fit in a single RTP packet */
999
1000 /* By default stick to the maximum multiple of native frame length */
1001 length = subframes_rtp * frame->datalen / subframes;
1002 samples = length * frame->samples / frame->datalen;
1003
1004 head = frame->data.ptr;
1005 tail = frame->data.ptr + frame->datalen;
1006 while (head < tail) {
1007 rtp_header->seqno = pvt->rtp_seqno++;
1008 rtp_header->timestamp = pvt->rtp_timestamp;
1009
1010 if ((tail - head) < (RTP_BUFFER_LEN - RTP_HEADER_LEN)) {
1011 length = tail - head;
1012 samples = length * frame->samples / frame->datalen;
1013 }
1014
1015 if ( frame->datalen <= (sizeof(buf) - RTP_HEADER_LEN) )
1016 memcpy(buf + RTP_HEADER_LEN, head, length);
1017 else {
1018 ast_log(LOG_WARNING, "buffer is too small\n");
1019 return -1;
1020 }
1021
1022 head += length;
1023 pvt->rtp_timestamp += (rtp_header->payload_type == RTP_G722) ? samples / 2 : samples; /* per RFC3551 */
1024
1025 ret = write(dev_ctx.ch_fd[pvt->port_id], buf, RTP_HEADER_LEN + length);
1026 if (ret < 0) {
1027 ast_debug(1, "TAPI: ast_lantiq_write(): error writing.\n");
1028 return -1;
1029 }
1030 if (ret != (RTP_HEADER_LEN + length)) {
1031 ast_log(LOG_WARNING, "Short TAPI write of %d bytes, expected %d bytes\n", ret, RTP_HEADER_LEN + length);
1032 continue;
1033 }
1034 }
1035
1036 return 0;
1037 }
1038
1039 static int acf_channel_read(struct ast_channel *chan, const char *funcname, char *args, char *buf, size_t buflen)
1040 {
1041 struct lantiq_pvt *pvt;
1042 int res = 0;
1043
1044 if (!chan || ast_channel_tech(chan) != &lantiq_tech) {
1045 ast_log(LOG_ERROR, "This function requires a valid Lantiq TAPI channel\n");
1046 return -1;
1047 }
1048
1049 ast_mutex_lock(&iflock);
1050
1051 pvt = (struct lantiq_pvt*) ast_channel_tech_pvt(chan);
1052
1053 if (!strcasecmp(args, "csd")) {
1054 snprintf(buf, buflen, "%lu", (unsigned long int) pvt->call_setup_delay);
1055 } else if (!strcasecmp(args, "jitter_stats")){
1056 lantiq_jb_get_stats(pvt->port_id);
1057 snprintf(buf, buflen, "jbBufSize=%u,jbUnderflow=%u,jbOverflow=%u,jbDelay=%u,jbInvalid=%u",
1058 (uint32_t) pvt->jb_size,
1059 (uint32_t) pvt->jb_underflow,
1060 (uint32_t) pvt->jb_overflow,
1061 (uint32_t) pvt->jb_delay,
1062 (uint32_t) pvt->jb_invalid);
1063 } else if (!strcasecmp(args, "jbBufSize")) {
1064 snprintf(buf, buflen, "%u", (uint32_t) pvt->jb_size);
1065 } else if (!strcasecmp(args, "jbUnderflow")) {
1066 snprintf(buf, buflen, "%u", (uint32_t) pvt->jb_underflow);
1067 } else if (!strcasecmp(args, "jbOverflow")) {
1068 snprintf(buf, buflen, "%u", (uint32_t) pvt->jb_overflow);
1069 } else if (!strcasecmp(args, "jbDelay")) {
1070 snprintf(buf, buflen, "%u", (uint32_t) pvt->jb_delay);
1071 } else if (!strcasecmp(args, "jbInvalid")) {
1072 snprintf(buf, buflen, "%u", (uint32_t) pvt->jb_invalid);
1073 } else if (!strcasecmp(args, "start")) {
1074 struct tm *tm = gmtime((const time_t*)&pvt->call_start);
1075 strftime(buf, buflen, "%F %T", tm);
1076 } else if (!strcasecmp(args, "answer")) {
1077 struct tm *tm = gmtime((const time_t*)&pvt->call_answer);
1078 strftime(buf, buflen, "%F %T", tm);
1079 } else {
1080 res = -1;
1081 }
1082
1083 ast_mutex_unlock(&iflock);
1084
1085 return res;
1086 }
1087
1088 static struct ast_frame * ast_lantiq_exception(struct ast_channel *ast)
1089 {
1090 ast_log(LOG_DEBUG, "entering... no code here...\n");
1091 return NULL;
1092 }
1093
1094 static void lantiq_jb_get_stats(int c) {
1095 struct lantiq_pvt *pvt = &iflist[c];
1096
1097 IFX_TAPI_JB_STATISTICS_t param;
1098 memset (&param, 0, sizeof (param));
1099 if (ioctl (dev_ctx.ch_fd[c], IFX_TAPI_JB_STATISTICS_GET, (IFX_int32_t) &param) != IFX_SUCCESS) {
1100 ast_debug(1, "Error getting jitter buffer stats.\n");
1101 } else {
1102 #if !defined (TAPI_VERSION3) && defined (TAPI_VERSION4)
1103 ast_debug(1, "Jitter buffer stats: dev=%u, ch=%u, nType=%u, nBufSize=%u, nIsUnderflow=%u, nDsOverflow=%u, nPODelay=%u, nInvalid=%u\n",
1104 (uint32_t) param.dev,
1105 (uint32_t) param.ch,
1106 #else
1107 ast_debug(1, "Jitter buffer stats: nType=%u, nBufSize=%u, nIsUnderflow=%u, nDsOverflow=%u, nPODelay=%u, nInvalid=%u\n",
1108 #endif
1109 (uint32_t) param.nType,
1110 (uint32_t) param.nBufSize,
1111 (uint32_t) param.nIsUnderflow,
1112 (uint32_t) param.nDsOverflow,
1113 (uint32_t) param.nPODelay,
1114 (uint32_t) param.nInvalid);
1115
1116 pvt->jb_size = param.nBufSize;
1117 pvt->jb_underflow = param.nIsUnderflow;
1118 pvt->jb_overflow = param.nDsOverflow;
1119 pvt->jb_invalid = param.nInvalid;
1120 pvt->jb_delay = param.nPODelay;
1121 }
1122 }
1123
1124
1125 static int lantiq_standby(int c)
1126 {
1127 ast_debug(1, "Stopping line feed for channel %i\n", c);
1128 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_LINE_FEED_SET, IFX_TAPI_LINE_FEED_STANDBY)) {
1129 ast_log(LOG_ERROR, "IFX_TAPI_LINE_FEED_SET ioctl failed\n");
1130 return -1;
1131 }
1132
1133 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_ENC_STOP, 0)) {
1134 ast_log(LOG_ERROR, "IFX_TAPI_ENC_STOP ioctl failed\n");
1135 return -1;
1136 }
1137
1138 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_DEC_STOP, 0)) {
1139 ast_log(LOG_ERROR, "IFX_TAPI_DEC_STOP ioctl failed\n");
1140 return -1;
1141 }
1142
1143 return lantiq_play_tone(c, TAPI_TONE_LOCALE_NONE);
1144 }
1145
1146 static int lantiq_end_dialing(int c)
1147 {
1148 struct lantiq_pvt *pvt = &iflist[c];
1149
1150 ast_log(LOG_DEBUG, "end of dialing\n");
1151
1152 if (pvt->dial_timer != -1) {
1153 AST_SCHED_DEL(sched, pvt->dial_timer);
1154 pvt->dial_timer = -1;
1155 }
1156
1157 if(pvt->owner) {
1158 ast_hangup(pvt->owner);
1159 }
1160 lantiq_reset_dtmfbuf(pvt);
1161
1162 return 0;
1163 }
1164
1165 static int lantiq_end_call(int c)
1166 {
1167 struct lantiq_pvt *pvt = &iflist[c];
1168
1169 ast_log(LOG_DEBUG, "end of call\n");
1170
1171 if(pvt->owner) {
1172 lantiq_jb_get_stats(c);
1173 ast_queue_hangup(pvt->owner);
1174 }
1175
1176 return 0;
1177 }
1178
1179 static struct ast_channel *lantiq_channel(int state, int c, char *ext, char *ctx, struct ast_format_cap *cap, const struct ast_assigned_ids *assigned_ids, const struct ast_channel *requestor)
1180 {
1181 struct ast_channel *chan = NULL;
1182 struct lantiq_pvt *pvt = &iflist[c];
1183 struct ast_format *format = NULL;
1184 struct ast_format_cap *newcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1185 struct ast_format_cap *formatcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
1186
1187 if (!newcap || !formatcap) {
1188 ast_log(LOG_DEBUG, "Cannot allocate format capabilities!\n");
1189 return NULL;
1190 }
1191
1192 chan = ast_channel_alloc(1, state, NULL, NULL, "", ext, ctx, assigned_ids, requestor, c, "TAPI/%d", (c + 1));
1193 if (!chan) {
1194 ast_log(LOG_DEBUG, "Cannot allocate channel!\n");
1195 ao2_ref(newcap, -1);
1196 ao2_ref(formatcap, -1);
1197 return NULL;
1198 }
1199
1200 ast_channel_tech_set(chan, &lantiq_tech);
1201 ast_channel_tech_pvt_set(chan, pvt);
1202 pvt->owner = chan;
1203
1204 if (cap && ast_format_cap_iscompatible(cap, lantiq_tech.capabilities)) { /* compatible format capabilities given */
1205 ast_format_cap_get_compatible(lantiq_tech.capabilities, cap, newcap);
1206 format = ast_format_cap_get_format(newcap, 0); /* choose format */
1207 } else { /* no or unsupported format capabilities given */
1208 format = ast_format_cap_get_format(lantiq_tech.capabilities, 0); /* choose format from capabilities */
1209 }
1210
1211 /* set choosed format */
1212 ast_format_cap_append(formatcap, format, 0);
1213 ast_channel_nativeformats_set(chan, formatcap);
1214 ast_channel_set_readformat(chan, format);
1215 ast_channel_set_writeformat(chan, format);
1216 ast_channel_set_rawreadformat(chan, format);
1217 ast_channel_set_rawwriteformat(chan, format);
1218
1219 ao2_ref(newcap, -1);
1220 ao2_ref(formatcap, -1);
1221
1222 /* configuring encoder */
1223 if (format != 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_assigned_ids *assigned_ids, const struct ast_channel *requestor, const char *data, int *cause)
1231 {
1232 struct ast_str *buf = ast_str_alloca(64);
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_format_cap_get_names(cap, &buf));
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, assigned_ids, requestor);
1268 ast_channel_unlock(chan); /* it's essential to unlock channel */
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 = NULL;
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 frame.subclass.format = format;
1329 frame.datalen = res - RTP_HEADER_LEN;
1330 frame.data.ptr = buf + RTP_HEADER_LEN;
1331 frame.samples = ast_codec_samples_count(&frame);
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, 0, 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); /* it's essential to unlock channel */
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
1844 if(!(lantiq_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
1845 ast_log(LOG_ERROR, "Unable to allocate format capabilities.\n");
1846 return AST_MODULE_LOAD_DECLINE;
1847 }
1848
1849 /* channel format capabilities */
1850 ast_format_cap_append(lantiq_tech.capabilities, ast_format_ulaw, 0);
1851 ast_format_cap_append(lantiq_tech.capabilities, ast_format_alaw, 0);
1852 ast_format_cap_append(lantiq_tech.capabilities, ast_format_g722, 0);
1853 ast_format_cap_append(lantiq_tech.capabilities, ast_format_g726, 0);
1854 ast_format_cap_append(lantiq_tech.capabilities, ast_format_slin, 0);
1855
1856 /* Turn off the LEDs, just in case */
1857 led_off(dev_ctx.voip_led);
1858 for(c = 0; c < TAPI_AUDIO_PORT_NUM_MAX; c++)
1859 led_off(dev_ctx.ch_led[c]);
1860
1861 if ((cfg = ast_config_load(config, config_flags)) == CONFIG_STATUS_FILEINVALID) {
1862 ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config);
1863 return AST_MODULE_LOAD_DECLINE;
1864 }
1865
1866 /* We *must* have a config file otherwise stop immediately */
1867 if (!cfg) {
1868 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
1869 return AST_MODULE_LOAD_DECLINE;
1870 }
1871
1872 if (ast_mutex_lock(&iflock)) {
1873 ast_log(LOG_ERROR, "Unable to lock interface list.\n");
1874 goto cfg_error;
1875 }
1876
1877 for (v = ast_variable_browse(cfg, "interfaces"); v; v = v->next) {
1878 if (!strcasecmp(v->name, "channels")) {
1879 dev_ctx.channels = atoi(v->value);
1880 if (!dev_ctx.channels) {
1881 ast_log(LOG_ERROR, "Invalid value for channels in config %s\n", config);
1882 goto cfg_error_il;
1883 }
1884 } else if (!strcasecmp(v->name, "firmwarefilename")) {
1885 ast_copy_string(firmware_filename, v->value, sizeof(firmware_filename));
1886 } else if (!strcasecmp(v->name, "bbdfilename")) {
1887 ast_copy_string(bbd_filename, v->value, sizeof(bbd_filename));
1888 } else if (!strcasecmp(v->name, "basepath")) {
1889 ast_copy_string(base_path, v->value, sizeof(base_path));
1890 } else if (!strcasecmp(v->name, "per_channel_context")) {
1891 if (!strcasecmp(v->value, "on")) {
1892 per_channel_context = 1;
1893 } else if (!strcasecmp(v->value, "off")) {
1894 per_channel_context = 0;
1895 } else {
1896 ast_log(LOG_ERROR, "Unknown per_channel_context value '%s'. Try 'on' or 'off'.\n", v->value);
1897 goto cfg_error_il;
1898 }
1899 }
1900 }
1901
1902 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
1903 if (!strcasecmp(v->name, "rxgain")) {
1904 rxgain = atoi(v->value);
1905 if (!rxgain) {
1906 rxgain = 0;
1907 ast_log(LOG_WARNING, "Invalid rxgain: %s, using default.\n", v->value);
1908 }
1909 } else if (!strcasecmp(v->name, "txgain")) {
1910 txgain = atoi(v->value);
1911 if (!txgain) {
1912 txgain = 0;
1913 ast_log(LOG_WARNING, "Invalid txgain: %s, using default.\n", v->value);
1914 }
1915 } else if (!strcasecmp(v->name, "echocancel")) {
1916 if (!strcasecmp(v->value, "off")) {
1917 wlec_type = IFX_TAPI_WLEC_TYPE_OFF;
1918 } else if (!strcasecmp(v->value, "nlec")) {
1919 wlec_type = IFX_TAPI_WLEC_TYPE_NE;
1920 if (!strcasecmp(v->name, "echocancelfixedwindowsize")) {
1921 wlec_nbne = atoi(v->value);
1922 }
1923 } else if (!strcasecmp(v->value, "wlec")) {
1924 wlec_type = IFX_TAPI_WLEC_TYPE_NFE;
1925 if (!strcasecmp(v->name, "echocancelnfemovingwindowsize")) {
1926 wlec_nbfe = atoi(v->value);
1927 } else if (!strcasecmp(v->name, "echocancelfixedwindowsize")) {
1928 wlec_nbne = atoi(v->value);
1929 } else if (!strcasecmp(v->name, "echocancelwidefixedwindowsize")) {
1930 wlec_wbne = atoi(v->value);
1931 }
1932 } else if (!strcasecmp(v->value, "nees")) {
1933 wlec_type = IFX_TAPI_WLEC_TYPE_NE_ES;
1934 } else if (!strcasecmp(v->value, "nfees")) {
1935 wlec_type = IFX_TAPI_WLEC_TYPE_NFE_ES;
1936 } else if (!strcasecmp(v->value, "es")) {
1937 wlec_type = IFX_TAPI_WLEC_TYPE_ES;
1938 } else {
1939 wlec_type = IFX_TAPI_WLEC_TYPE_OFF;
1940 ast_log(LOG_ERROR, "Unknown echo cancellation type '%s'\n", v->value);
1941 goto cfg_error_il;
1942 }
1943 } else if (!strcasecmp(v->name, "echocancelnlp")) {
1944 if (!strcasecmp(v->value, "on")) {
1945 wlec_nlp = IFX_TAPI_WLEC_NLP_ON;
1946 } else if (!strcasecmp(v->value, "off")) {
1947 wlec_nlp = IFX_TAPI_WLEC_NLP_OFF;
1948 } else {
1949 ast_log(LOG_ERROR, "Unknown echo cancellation nlp '%s'\n", v->value);
1950 goto cfg_error_il;
1951 }
1952 } else if (!strcasecmp(v->name, "jitterbuffertype")) {
1953 if (!strcasecmp(v->value, "fixed")) {
1954 jb_type = IFX_TAPI_JB_TYPE_FIXED;
1955 } else if (!strcasecmp(v->value, "adaptive")) {
1956 jb_type = IFX_TAPI_JB_TYPE_ADAPTIVE;
1957 jb_localadpt = IFX_TAPI_JB_LOCAL_ADAPT_DEFAULT;
1958 if (!strcasecmp(v->name, "jitterbufferadaptation")) {
1959 if (!strcasecmp(v->value, "on")) {
1960 jb_localadpt = IFX_TAPI_JB_LOCAL_ADAPT_ON;
1961 } else if (!strcasecmp(v->value, "off")) {
1962 jb_localadpt = IFX_TAPI_JB_LOCAL_ADAPT_OFF;
1963 }
1964 } else if (!strcasecmp(v->name, "jitterbufferscalling")) {
1965 jb_scaling = atoi(v->value);
1966 } else if (!strcasecmp(v->name, "jitterbufferinitialsize")) {
1967 jb_initialsize = atoi(v->value);
1968 } else if (!strcasecmp(v->name, "jitterbufferminsize")) {
1969 jb_minsize = atoi(v->value);
1970 } else if (!strcasecmp(v->name, "jitterbuffermaxsize")) {
1971 jb_maxsize = atoi(v->value);
1972 }
1973 } else {
1974 ast_log(LOG_ERROR, "Unknown jitter buffer type '%s'\n", v->value);
1975 goto cfg_error_il;
1976 }
1977 } else if (!strcasecmp(v->name, "jitterbufferpackettype")) {
1978 if (!strcasecmp(v->value, "voice")) {
1979 jb_pckadpt = IFX_TAPI_JB_PKT_ADAPT_VOICE;
1980 } else if (!strcasecmp(v->value, "data")) {
1981 jb_pckadpt = IFX_TAPI_JB_PKT_ADAPT_DATA;
1982 } else if (!strcasecmp(v->value, "datanorep")) {
1983 jb_pckadpt = IFX_TAPI_JB_PKT_ADAPT_DATA_NO_REP;
1984 } else {
1985 ast_log(LOG_ERROR, "Unknown jitter buffer packet adaptation type '%s'\n", v->value);
1986 goto cfg_error_il;
1987 }
1988 } else if (!strcasecmp(v->name, "calleridtype")) {
1989 ast_log(LOG_DEBUG, "Setting CID type to %s.\n", v->value);
1990 if (!strcasecmp(v->value, "telecordia")) {
1991 cid_type = IFX_TAPI_CID_STD_TELCORDIA;
1992 } else if (!strcasecmp(v->value, "etsifsk")) {
1993 cid_type = IFX_TAPI_CID_STD_ETSI_FSK;
1994 } else if (!strcasecmp(v->value, "etsidtmf")) {
1995 cid_type = IFX_TAPI_CID_STD_ETSI_DTMF;
1996 } else if (!strcasecmp(v->value, "sin")) {
1997 cid_type = IFX_TAPI_CID_STD_SIN;
1998 } else if (!strcasecmp(v->value, "ntt")) {
1999 cid_type = IFX_TAPI_CID_STD_NTT;
2000 } else if (!strcasecmp(v->value, "kpndtmf")) {
2001 cid_type = IFX_TAPI_CID_STD_KPN_DTMF;
2002 } else if (!strcasecmp(v->value, "kpndtmffsk")) {
2003 cid_type = IFX_TAPI_CID_STD_KPN_DTMF_FSK;
2004 } else {
2005 ast_log(LOG_ERROR, "Unknown caller id type '%s'\n", v->value);
2006 goto cfg_error_il;
2007 }
2008 } else if (!strcasecmp(v->name, "voiceactivitydetection")) {
2009 if (!strcasecmp(v->value, "on")) {
2010 vad_type = IFX_TAPI_ENC_VAD_ON;
2011 } else if (!strcasecmp(v->value, "g711")) {
2012 vad_type = IFX_TAPI_ENC_VAD_G711;
2013 } else if (!strcasecmp(v->value, "cng")) {
2014 vad_type = IFX_TAPI_ENC_VAD_CNG_ONLY;
2015 } else if (!strcasecmp(v->value, "sc")) {
2016 vad_type = IFX_TAPI_ENC_VAD_SC_ONLY;
2017 } else {
2018 ast_log(LOG_ERROR, "Unknown voice activity detection value '%s'\n", v->value);
2019 goto cfg_error_il;
2020 }
2021 } else if (!strcasecmp(v->name, "interdigit")) {
2022 dev_ctx.interdigit_timeout = atoi(v->value);
2023 ast_log(LOG_DEBUG, "Setting interdigit timeout to %s.\n", v->value);
2024 if (!dev_ctx.interdigit_timeout) {
2025 dev_ctx.interdigit_timeout = DEFAULT_INTERDIGIT_TIMEOUT;
2026 ast_log(LOG_WARNING, "Invalid interdigit timeout: %s, using default.\n", v->value);
2027 }
2028 } else if (!strcasecmp(v->name, "tone_generator")) {
2029 if (!strcasecmp(v->value, "integrated")) {
2030 tone_generator = TONE_INTEGRATED;
2031 } else if (!strcasecmp(v->value, "asterisk")) {
2032 tone_generator = TONE_ASTERISK;
2033 } else if (!strcasecmp(v->value, "media")) {
2034 tone_generator = TONE_MEDIA;
2035 } else {
2036 ast_log(LOG_ERROR, "Unknown tone_generator value '%s'. Try 'integrated', 'asterisk' or 'media'.\n", v->value);
2037 goto cfg_error_il;
2038 }
2039 }
2040 }
2041
2042 lantiq_create_pvts();
2043
2044 ast_mutex_unlock(&iflock);
2045 ast_config_destroy(cfg);
2046
2047 if (!(sched = ast_sched_context_create())) {
2048 ast_log(LOG_ERROR, "Unable to create scheduler context\n");
2049 goto load_error;
2050 }
2051
2052 if (ast_sched_start_thread(sched)) {
2053 ast_log(LOG_ERROR, "Unable to create scheduler context thread\n");
2054 goto load_error_st;
2055 }
2056
2057 if (ast_channel_register(&lantiq_tech)) {
2058 ast_log(LOG_ERROR, "Unable to register channel class 'Phone'\n");
2059 goto load_error_st;
2060 }
2061
2062 /* tapi */
2063 IFX_TAPI_TONE_t tone;
2064 IFX_TAPI_DEV_START_CFG_t dev_start;
2065 IFX_TAPI_MAP_DATA_t map_data;
2066 IFX_TAPI_LINE_TYPE_CFG_t line_type;
2067 IFX_TAPI_LINE_VOLUME_t line_vol;
2068 IFX_TAPI_WLEC_CFG_t wlec_cfg;
2069 IFX_TAPI_JB_CFG_t jb_cfg;
2070 IFX_TAPI_CID_CFG_t cid_cfg;
2071
2072 /* open device */
2073 dev_ctx.dev_fd = lantiq_dev_open(base_path, 0);
2074
2075 if (dev_ctx.dev_fd < 0) {
2076 ast_log(LOG_ERROR, "lantiq TAPI device open function failed\n");
2077 goto load_error_st;
2078 }
2079
2080 snprintf(dev_ctx.voip_led, LED_NAME_LENGTH, "voice");
2081 for (c = 0; c < dev_ctx.channels ; c++) {
2082 dev_ctx.ch_fd[c] = lantiq_dev_open(base_path, c + 1);
2083
2084 if (dev_ctx.ch_fd[c] < 0) {
2085 ast_log(LOG_ERROR, "lantiq TAPI channel %d open function failed\n", c);
2086 goto load_error_st;
2087 }
2088 snprintf(dev_ctx.ch_led[c], LED_NAME_LENGTH, "fxs%d", c + 1);
2089 }
2090
2091 if (lantiq_dev_firmware_download(dev_ctx.dev_fd, firmware_filename)) {
2092 ast_log(LOG_ERROR, "voice firmware download failed\n");
2093 goto load_error_st;
2094 }
2095
2096 if (ioctl(dev_ctx.dev_fd, IFX_TAPI_DEV_STOP, 0)) {
2097 ast_log(LOG_ERROR, "IFX_TAPI_DEV_STOP ioctl failed\n");
2098 goto load_error_st;
2099 }
2100
2101 memset(&dev_start, 0x0, sizeof(IFX_TAPI_DEV_START_CFG_t));
2102 dev_start.nMode = IFX_TAPI_INIT_MODE_VOICE_CODER;
2103
2104 /* Start TAPI */
2105 if (ioctl(dev_ctx.dev_fd, IFX_TAPI_DEV_START, &dev_start)) {
2106 ast_log(LOG_ERROR, "IFX_TAPI_DEV_START ioctl failed\n");
2107 goto load_error_st;
2108 }
2109
2110 tz = ast_get_indication_zone(NULL);
2111
2112 if (!tz) {
2113 ast_log(LOG_ERROR, "Unable to alloc tone zone\n");
2114 goto load_error_st;
2115 }
2116
2117 ts_dial = ast_get_indication_tone(tz, "dial");
2118 ts_ring = ast_get_indication_tone(tz, "ring");
2119 ts_busy = ast_get_indication_tone(tz, "busy");
2120 ts_congestion = ast_get_indication_tone(tz, "congestion");
2121
2122 if (!ts_dial || !ts_dial->data || !ts_ring || !ts_ring->data || !ts_busy || !ts_busy->data || !ts_congestion || !ts_congestion->data) {
2123 ast_log(LOG_ERROR, "Unable to get indication tones\n");
2124 goto load_error_st;
2125 }
2126
2127 for (c = 0; c < dev_ctx.channels ; c++) {
2128 /* We're a FXS and want to switch between narrow & wide band automatically */
2129 memset(&line_type, 0, sizeof(IFX_TAPI_LINE_TYPE_CFG_t));
2130 line_type.lineType = IFX_TAPI_LINE_TYPE_FXS_AUTO;
2131 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_LINE_TYPE_SET, &line_type)) {
2132 ast_log(LOG_ERROR, "IFX_TAPI_LINE_TYPE_SET %d failed\n", c);
2133 goto load_error_st;
2134 }
2135
2136 /* tones */
2137 memset(&tone, 0, sizeof(IFX_TAPI_TONE_t));
2138 tone.simple.format = IFX_TAPI_TONE_TYPE_SIMPLE;
2139 playlist_to_tapitone(ts_dial->data, TAPI_TONE_LOCALE_DIAL_CODE, &tone);
2140 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_TONE_TABLE_CFG_SET, &tone)) {
2141 ast_log(LOG_ERROR, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c);
2142 goto load_error_st;
2143 }
2144
2145 memset(&tone, 0, sizeof(IFX_TAPI_TONE_t));
2146 tone.simple.format = IFX_TAPI_TONE_TYPE_SIMPLE;
2147 playlist_to_tapitone(ts_ring->data, TAPI_TONE_LOCALE_RINGING_CODE, &tone);
2148 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_TONE_TABLE_CFG_SET, &tone)) {
2149 ast_log(LOG_ERROR, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c);
2150 goto load_error_st;
2151 }
2152
2153 memset(&tone, 0, sizeof(IFX_TAPI_TONE_t));
2154 tone.simple.format = IFX_TAPI_TONE_TYPE_SIMPLE;
2155 playlist_to_tapitone(ts_busy->data, TAPI_TONE_LOCALE_BUSY_CODE, &tone);
2156 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_TONE_TABLE_CFG_SET, &tone)) {
2157 ast_log(LOG_ERROR, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c);
2158 goto load_error_st;
2159 }
2160
2161 memset(&tone, 0, sizeof(IFX_TAPI_TONE_t));
2162 tone.simple.format = IFX_TAPI_TONE_TYPE_SIMPLE;
2163 playlist_to_tapitone(ts_congestion->data, TAPI_TONE_LOCALE_CONGESTION_CODE, &tone);
2164 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_TONE_TABLE_CFG_SET, &tone)) {
2165 ast_log(LOG_ERROR, "IFX_TAPI_TONE_TABLE_CFG_SET %d failed\n", c);
2166 goto load_error_st;
2167 }
2168
2169 /* ringing type */
2170 IFX_TAPI_RING_CFG_t ringingType;
2171 memset(&ringingType, 0, sizeof(IFX_TAPI_RING_CFG_t));
2172 ringingType.nMode = IFX_TAPI_RING_CFG_MODE_INTERNAL_BALANCED;
2173 ringingType.nSubmode = IFX_TAPI_RING_CFG_SUBMODE_DC_RNG_TRIP_FAST;
2174 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_RING_CFG_SET, (IFX_int32_t) &ringingType)) {
2175 ast_log(LOG_ERROR, "IFX_TAPI_RING_CFG_SET failed\n");
2176 goto load_error_st;
2177 }
2178
2179 /* ring cadence */
2180 IFX_char_t data[15] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
2181 0x00, 0x00, 0x00, 0x00, 0x00,
2182 0x00, 0x00, 0x00, 0x00, 0x00 };
2183
2184 IFX_TAPI_RING_CADENCE_t ringCadence;
2185 memset(&ringCadence, 0, sizeof(IFX_TAPI_RING_CADENCE_t));
2186 memcpy(&ringCadence.data, data, sizeof(data));
2187 ringCadence.nr = sizeof(data) * 8;
2188
2189 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_RING_CADENCE_HR_SET, &ringCadence)) {
2190 ast_log(LOG_ERROR, "IFX_TAPI_RING_CADENCE_HR_SET failed\n");
2191 goto load_error_st;
2192 }
2193
2194 /* perform mapping */
2195 memset(&map_data, 0x0, sizeof(IFX_TAPI_MAP_DATA_t));
2196 map_data.nDstCh = c;
2197 map_data.nChType = IFX_TAPI_MAP_TYPE_PHONE;
2198
2199 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_MAP_DATA_ADD, &map_data)) {
2200 ast_log(LOG_ERROR, "IFX_TAPI_MAP_DATA_ADD %d failed\n", c);
2201 goto load_error_st;
2202 }
2203
2204 /* set line feed */
2205 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_LINE_FEED_SET, IFX_TAPI_LINE_FEED_STANDBY)) {
2206 ast_log(LOG_ERROR, "IFX_TAPI_LINE_FEED_SET %d failed\n", c);
2207 goto load_error_st;
2208 }
2209
2210 /* set volume */
2211 memset(&line_vol, 0, sizeof(line_vol));
2212 line_vol.nGainRx = rxgain;
2213 line_vol.nGainTx = txgain;
2214
2215 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_PHONE_VOLUME_SET, &line_vol)) {
2216 ast_log(LOG_ERROR, "IFX_TAPI_PHONE_VOLUME_SET %d failed\n", c);
2217 goto load_error_st;
2218 }
2219
2220 /* Configure line echo canceller */
2221 memset(&wlec_cfg, 0, sizeof(wlec_cfg));
2222 wlec_cfg.nType = wlec_type;
2223 wlec_cfg.bNlp = wlec_nlp;
2224 wlec_cfg.nNBFEwindow = wlec_nbfe;
2225 wlec_cfg.nNBNEwindow = wlec_nbne;
2226 wlec_cfg.nWBNEwindow = wlec_wbne;
2227
2228 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_WLEC_PHONE_CFG_SET, &wlec_cfg)) {
2229 ast_log(LOG_ERROR, "IFX_TAPI_WLEC_PHONE_CFG_SET %d failed\n", c);
2230 goto load_error_st;
2231 }
2232
2233 /* Configure jitter buffer */
2234 memset(&jb_cfg, 0, sizeof(jb_cfg));
2235 jb_cfg.nJbType = jb_type;
2236 jb_cfg.nPckAdpt = jb_pckadpt;
2237 jb_cfg.nLocalAdpt = jb_localadpt;
2238 jb_cfg.nScaling = jb_scaling;
2239 jb_cfg.nInitialSize = jb_initialsize;
2240 jb_cfg.nMinSize = jb_minsize;
2241 jb_cfg.nMaxSize = jb_maxsize;
2242
2243 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_JB_CFG_SET, &jb_cfg)) {
2244 ast_log(LOG_ERROR, "IFX_TAPI_JB_CFG_SET %d failed\n", c);
2245 goto load_error_st;
2246 }
2247
2248 /* Configure Caller ID type */
2249 memset(&cid_cfg, 0, sizeof(cid_cfg));
2250 cid_cfg.nStandard = cid_type;
2251
2252 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_CID_CFG_SET, &cid_cfg)) {
2253 ast_log(LOG_ERROR, "IIFX_TAPI_CID_CFG_SET %d failed\n", c);
2254 goto load_error_st;
2255 }
2256
2257 /* Configure voice activity detection */
2258 if (ioctl(dev_ctx.ch_fd[c], IFX_TAPI_ENC_VAD_CFG_SET, vad_type)) {
2259 ast_log(LOG_ERROR, "IFX_TAPI_ENC_VAD_CFG_SET %d failed\n", c);
2260 goto load_error_st;
2261 }
2262
2263 /* Setup TAPI <-> internal RTP codec type mapping */
2264 if (lantiq_setup_rtp(c)) {
2265 goto load_error_st;
2266 }
2267
2268 /* Set initial hook status */
2269 iflist[c].channel_state = lantiq_get_hookstatus(c);
2270
2271 if (iflist[c].channel_state == UNKNOWN) {
2272 goto load_error_st;
2273 }
2274 }
2275
2276 /* make sure our device will be closed properly */
2277 ast_register_atexit(lantiq_cleanup);
2278
2279 restart_monitor();
2280 led_on(dev_ctx.voip_led);
2281 return AST_MODULE_LOAD_SUCCESS;
2282
2283 cfg_error_il:
2284 ast_mutex_unlock(&iflock);
2285 cfg_error:
2286 ast_config_destroy(cfg);
2287 return AST_MODULE_LOAD_DECLINE;
2288
2289 load_error_st:
2290 ast_sched_context_destroy(sched);
2291 load_error:
2292 unload_module();
2293 ast_free(iflist);
2294 return AST_MODULE_LOAD_FAILURE;
2295 }
2296
2297 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Lantiq TAPI Telephony API Support",
2298 .load = load_module,
2299 .unload = unload_module,
2300 .load_pri = AST_MODPRI_CHANNEL_DRIVER
2301 );