e5528fa6568c05b0fd1aab2f8e55013d5b1e16e8
[asterisk/asterisk.git] / channels / chan_misdn.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  * 
4  * Copyright (C) 2004 - 2006, Christian Richter
5  *
6  * Christian Richter <crich@beronet.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  *
18  */
19
20 /*!
21  * \file
22  *
23  * \brief the chan_misdn channel driver for Asterisk
24  * \author Christian Richter <crich@beronet.com>
25  *
26  * \ingroup channel_drivers
27  */
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include <stdio.h>
34 #include <pthread.h>
35 #include <string.h>
36 #include <sys/socket.h>
37 #include <sys/time.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <arpa/inet.h>
42 #include <fcntl.h>
43 #include <sys/ioctl.h>
44 #include <sys/file.h>
45
46 #include "asterisk/channel.h"
47 #include "asterisk/config.h"
48 #include "asterisk/logger.h"
49 #include "asterisk/module.h"
50 #include "asterisk/pbx.h"
51 #include "asterisk/options.h"
52 #include "asterisk/io.h"
53 #include "asterisk/frame.h"
54 #include "asterisk/translate.h"
55 #include "asterisk/cli.h"
56 #include "asterisk/musiconhold.h"
57 #include "asterisk/dsp.h"
58 #include "asterisk/translate.h"
59 #include "asterisk/config.h"
60 #include "asterisk/file.h"
61 #include "asterisk/callerid.h"
62 #include "asterisk/indications.h"
63 #include "asterisk/app.h"
64 #include "asterisk/features.h"
65 #include "asterisk/term.h"
66 #include "asterisk/stringfields.h"
67
68 #include "chan_misdn_config.h"
69 #include "isdn_lib.h"
70
71 ast_mutex_t release_lock_mutex;
72
73 #define release_lock ast_mutex_lock(&release_lock_mutex)
74 #define release_unlock ast_mutex_unlock(&release_lock_mutex)
75
76
77 char global_tracefile[BUFFERSIZE+1];
78
79
80 struct misdn_jb{
81         int size;
82         int upper_threshold;
83         char *samples, *ok;
84         int wp,rp;
85         int state_empty;
86         int state_full;
87         int state_buffer;
88         int bytes_wrote;
89         ast_mutex_t mutexjb;
90 };
91
92
93
94 /* allocates the jb-structure and initialise the elements*/
95 struct misdn_jb *misdn_jb_init(int size, int upper_threshold);
96
97 /* frees the data and destroys the given jitterbuffer struct */
98 void misdn_jb_destroy(struct misdn_jb *jb);
99
100 /* fills the jitterbuffer with len data returns < 0 if there was an
101 error (bufferoverun). */
102 int misdn_jb_fill(struct misdn_jb *jb, const char *data, int len);
103
104 /* gets len bytes out of the jitterbuffer if available, else only the
105 available data is returned and the return value indicates the number
106 of data. */
107 int misdn_jb_empty(struct misdn_jb *jb, char *data, int len);
108
109
110
111
112 /* BEGIN: chan_misdn.h */
113
114
115
116 enum misdn_chan_state {
117         MISDN_NOTHING,          /*!< at beginning */
118         MISDN_WAITING4DIGS, /*!<  when waiting for infos */
119         MISDN_EXTCANTMATCH, /*!<  when asterisk couldnt match our ext */
120         MISDN_DIALING, /*!<  when pbx_start */
121         MISDN_PROGRESS, /*!<  we got a progress */
122         MISDN_PROCEEDING, /*!<  we got a progress */
123         MISDN_CALLING, /*!<  when misdn_call is called */
124         MISDN_CALLING_ACKNOWLEDGE, /*!<  when we get SETUP_ACK */
125         MISDN_ALERTING, /*!<  when Alerting */
126         MISDN_BUSY, /*!<  when BUSY */
127         MISDN_CONNECTED, /*!<  when connected */
128         MISDN_PRECONNECTED, /*!<  when connected */
129         MISDN_DISCONNECTED, /*!<  when connected */
130         MISDN_RELEASED, /*!<  when connected */
131         MISDN_BRIDGED, /*!<  when bridged */
132         MISDN_CLEANING, /*!< when hangup from * but we were connected before */
133         MISDN_HUNGUP_FROM_MISDN, /*!< when DISCONNECT/RELEASE/REL_COMP  cam from misdn */
134         MISDN_HUNGUP_FROM_AST, /*!< when DISCONNECT/RELEASE/REL_COMP came out of */
135         /* misdn_hangup */
136         MISDN_HOLDED, /*!< if this chan is holded */
137         MISDN_HOLD_DISCONNECT /*!< if this chan is holded */
138   
139 };
140
141 #define ORG_AST 1
142 #define ORG_MISDN 2
143
144 struct chan_list {
145   
146         ast_mutex_t lock;
147
148         char allowed_bearers[BUFFERSIZE+1];
149         
150         enum misdn_chan_state state;
151         int need_queue_hangup;
152         int need_hangup;
153         
154         int holded; 
155         int orginator;
156
157         int norxtone;
158         int notxtone; 
159         
160         int incoming_early_audio;
161
162         int ignore_dtmf;
163
164         int pipe[2];
165         char ast_rd_buf[4096];
166         struct ast_frame frame;
167
168         int faxdetect;
169         int faxhandled;
170
171         int ast_dsp;
172
173         int jb_len;
174         int jb_upper_threshold;
175         struct misdn_jb *jb;
176         
177         struct ast_dsp *dsp;
178         struct ast_trans_pvt *trans;
179   
180         struct ast_channel * ast;
181
182         int dummy;
183   
184         struct misdn_bchannel *bc;
185         struct misdn_bchannel *holded_bc;
186
187         unsigned int l3id;
188         int addr;
189
190         char context[BUFFERSIZE];
191
192         int zero_read_cnt;
193         int dropped_frame_cnt;
194
195         int far_alerting;
196         int other_pid;
197         struct chan_list *other_ch;
198
199         const struct tone_zone_sound *ts;
200         
201         struct chan_list *peer;
202         struct chan_list *next;
203         struct chan_list *prev;
204         struct chan_list *first;
205 };
206
207
208
209 void export_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
210 void import_ch(struct ast_channel *chan, struct misdn_bchannel *bc, struct chan_list *ch);
211
212 struct robin_list {
213         char *group;
214         int port;
215         int channel;
216         struct robin_list *next;
217         struct robin_list *prev;
218 };
219 static struct robin_list *robin = NULL;
220
221
222
223 struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame);
224
225
226
227 static inline void free_robin_list_r (struct robin_list *r)
228 {
229         if (r) {
230                 if (r->next) free_robin_list_r(r->next);
231                 if (r->group) free(r->group);
232                 free(r);
233         }
234 }
235
236 static void free_robin_list ( void )
237 {
238         free_robin_list_r(robin);
239         robin = NULL;
240 }
241
242 static struct robin_list* get_robin_position (char *group) 
243 {
244         struct robin_list *iter = robin;
245         for (; iter; iter = iter->next) {
246                 if (!strcasecmp(iter->group, group))
247                         return iter;
248         }
249         struct robin_list *new = (struct robin_list *)calloc(1, sizeof(struct robin_list));
250         new->group = strndup(group, strlen(group));
251         new->channel = 1;
252         if (robin) {
253                 new->next = robin;
254                 robin->prev = new;
255         }
256         robin = new;
257         return robin;
258 }
259
260
261 static void chan_misdn_log(int level, int port, char *tmpl, ...);
262
263 static struct ast_channel *misdn_new(struct chan_list *cl, int state,  char *exten, char *callerid, int format, int port, int c);
264 static void send_digit_to_chan(struct chan_list *cl, char digit );
265
266 static void hangup_chan(struct chan_list *ch);
267 static int pbx_start_chan(struct chan_list *ch);
268
269
270 #define AST_CID_P(ast) ast->cid.cid_num
271 #define AST_BRIDGED_P(ast) ast_bridged_channel(ast) 
272 #define AST_LOAD_CFG ast_config_load
273 #define AST_DESTROY_CFG ast_config_destroy
274
275 #define MISDN_ASTERISK_TECH_PVT(ast) ast->tech_pvt
276 #define MISDN_ASTERISK_PVT(ast) 1
277
278 #include <asterisk/strings.h>
279
280 /* #define MISDN_DEBUG 1 */
281
282 static char *desc = "Channel driver for mISDN Support (Bri/Pri)";
283 static const char misdn_type[] = "mISDN";
284
285 static int tracing = 0 ;
286
287 static char **misdn_key_vector=NULL;
288 static int misdn_key_vector_size=0;
289
290 /* Only alaw and mulaw is allowed for now */
291 static int prefformat =  AST_FORMAT_ALAW ; /*  AST_FORMAT_SLINEAR ;  AST_FORMAT_ULAW | */
292
293 static int *misdn_debug;
294 static int *misdn_debug_only;
295 static int max_ports;
296
297 static int *misdn_in_calls;
298 static int *misdn_out_calls;
299
300
301 struct chan_list dummy_cl;
302
303 struct chan_list *cl_te=NULL;
304 ast_mutex_t cl_te_lock;
305
306 static enum event_response_e
307 cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data);
308
309 static void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel*bc, struct chan_list *ch);
310
311 static void cl_queue_chan(struct chan_list **list, struct chan_list *chan);
312 static void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan);
313 static struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc);
314 static struct chan_list *find_chan_by_pid(struct chan_list *list, int pid);
315
316
317
318 static int dialtone_indicate(struct chan_list *cl);
319 static int hanguptone_indicate(struct chan_list *cl);
320 static int stop_indicate(struct chan_list *cl);
321
322 static int start_bc_tones(struct chan_list *cl);
323 static int stop_bc_tones(struct chan_list *cl);
324 static void release_chan(struct misdn_bchannel *bc);
325
326 static int misdn_set_opt_exec(struct ast_channel *chan, void *data);
327 static int misdn_facility_exec(struct ast_channel *chan, void *data);
328
329 int chan_misdn_jb_empty(struct misdn_bchannel *bc, char *buf, int len);
330
331
332 void debug_numplan(int port, int numplan, char *type);
333
334
335 int add_out_calls(int port);
336 int add_in_calls(int port);
337
338
339 /*************** Helpers *****************/
340
341 static struct chan_list * get_chan_by_ast(struct ast_channel *ast)
342 {
343         struct chan_list *tmp;
344   
345         for (tmp=cl_te; tmp; tmp = tmp->next) {
346                 if ( tmp->ast == ast ) return tmp;
347         }
348   
349         return NULL;
350 }
351
352 static struct chan_list * get_chan_by_ast_name(char *name)
353 {
354         struct chan_list *tmp;
355   
356         for (tmp=cl_te; tmp; tmp = tmp->next) {
357                 if ( tmp->ast  && strcmp(tmp->ast->name,name) == 0) return tmp;
358         }
359   
360         return NULL;
361 }
362
363
364
365 struct allowed_bearers {
366         int cap;
367         int val;
368         char *name;
369 };
370
371 struct allowed_bearers allowed_bearers_array[]={
372         {INFO_CAPABILITY_SPEECH,1,"speech"},
373         {INFO_CAPABILITY_AUDIO_3_1K,2,"3_1khz"},
374         {INFO_CAPABILITY_DIGITAL_UNRESTRICTED,4,"digital_unrestricted"},
375         {INFO_CAPABILITY_DIGITAL_RESTRICTED,8,"digital_restriced"},
376         {INFO_CAPABILITY_VIDEO,16,"video"}
377 };
378
379 static char *bearer2str(int cap) {
380         static char *bearers[]={
381                 "Speech",
382                 "Audio 3.1k",
383                 "Unres Digital",
384                 "Res Digital",
385                 "Video",
386                 "Unknown Bearer"
387         };
388         
389         switch (cap) {
390         case INFO_CAPABILITY_SPEECH:
391                 return bearers[0];
392                 break;
393         case INFO_CAPABILITY_AUDIO_3_1K:
394                 return bearers[1];
395                 break;
396         case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
397                 return bearers[2];
398                 break;
399         case INFO_CAPABILITY_DIGITAL_RESTRICTED:
400                 return bearers[3];
401                 break;
402         case INFO_CAPABILITY_VIDEO:
403                 return bearers[4];
404                 break;
405         default:
406                 return bearers[5];
407                 break;
408         }
409 }
410
411
412 static void print_facility( struct misdn_bchannel *bc)
413 {
414         switch (bc->fac_type) {
415         case FACILITY_CALLDEFLECT:
416                 chan_misdn_log(2,bc->port," --> calldeflect: %s\n",
417                                bc->fac.calldeflect_nr);
418                 break;
419         case FACILITY_CENTREX:
420                 chan_misdn_log(2,bc->port," --> centrex: %s\n",
421                                bc->fac.cnip);
422                 break;
423         default:
424                 chan_misdn_log(2,bc->port," --> unknown\n");
425                 
426         }
427 }
428
429 static void print_bearer(struct misdn_bchannel *bc) 
430 {
431         
432         chan_misdn_log(2, bc->port, " --> Bearer: %s\n",bearer2str(bc->capability));
433         
434         switch(bc->law) {
435         case INFO_CODEC_ALAW:
436                 chan_misdn_log(2, bc->port, " --> Codec: Alaw\n");
437                 break;
438         case INFO_CODEC_ULAW:
439                 chan_misdn_log(2, bc->port, " --> Codec: Ulaw\n");
440                 break;
441         }
442 }
443 /*************** Helpers END *************/
444
445 static void send_digit_to_chan(struct chan_list *cl, char digit )
446 {
447         static const char* dtmf_tones[] = {
448                 "!941+1336/100,!0/100", /* 0 */
449                 "!697+1209/100,!0/100", /* 1 */
450                 "!697+1336/100,!0/100", /* 2 */
451                 "!697+1477/100,!0/100", /* 3 */
452                 "!770+1209/100,!0/100", /* 4 */
453                 "!770+1336/100,!0/100", /* 5 */
454                 "!770+1477/100,!0/100", /* 6 */
455                 "!852+1209/100,!0/100", /* 7 */
456                 "!852+1336/100,!0/100", /* 8 */
457                 "!852+1477/100,!0/100", /* 9 */
458                 "!697+1633/100,!0/100", /* A */
459                 "!770+1633/100,!0/100", /* B */
460                 "!852+1633/100,!0/100", /* C */
461                 "!941+1633/100,!0/100", /* D */
462                 "!941+1209/100,!0/100", /* * */
463                 "!941+1477/100,!0/100" };       /* # */
464         struct ast_channel *chan=cl->ast; 
465   
466         if (digit >= '0' && digit <='9')
467                 ast_playtones_start(chan,0,dtmf_tones[digit-'0'], 0);
468         else if (digit >= 'A' && digit <= 'D')
469                 ast_playtones_start(chan,0,dtmf_tones[digit-'A'+10], 0);
470         else if (digit == '*')
471                 ast_playtones_start(chan,0,dtmf_tones[14], 0);
472         else if (digit == '#')
473                 ast_playtones_start(chan,0,dtmf_tones[15], 0);
474         else {
475                 /* not handled */
476                 ast_log(LOG_DEBUG, "Unable to handle DTMF tone '%c' for '%s'\n", digit, chan->name);
477     
478     
479         }
480 }
481 /*** CLI HANDLING ***/
482 static int misdn_set_debug(int fd, int argc, char *argv[])
483 {
484         if (argc != 4 && argc != 5 && argc != 6 && argc != 7)
485                 return RESULT_SHOWUSAGE; 
486
487         int level = atoi(argv[3]);
488
489         switch (argc) {
490                 case 4: 
491                 case 5: {
492                                         int only = 0;
493                                         if (argc == 5) {
494                                                 if (strncasecmp(argv[4], "only", strlen(argv[4])))
495                                                         return RESULT_SHOWUSAGE;
496                                                 else
497                                                         only = 1;
498                                         }
499                                         int i;
500                                         for (i=0; i<=max_ports; i++) {
501                                                 misdn_debug[i] = level;
502                                                 misdn_debug_only[i] = only;
503                                         }
504                                         ast_cli(fd, "changing debug level for all ports to %d%s\n",misdn_debug[0], only?" (only)":"");
505                                 }
506                                 break;
507                 case 6: 
508                 case 7: {
509                                         if (strncasecmp(argv[4], "port", strlen(argv[4])))
510                                                 return RESULT_SHOWUSAGE;
511                                         int port = atoi(argv[5]);
512                                         if (port <= 0 || port > max_ports) {
513                                                 switch (max_ports) {
514                                                         case 0:
515                                                                 ast_cli(fd, "port number not valid! no ports available so you won't get lucky with any number here...\n");
516                                                                 break;
517                                                         case 1:
518                                                                 ast_cli(fd, "port number not valid! only port 1 is availble.\n");
519                                                                 break;
520                                                         default:
521                                                                 ast_cli(fd, "port number not valid! only ports 1 to %d are available.\n", max_ports);
522                                                         }
523                                                         return 0;
524                                         }
525                                         if (argc == 7) {
526                                                 if (strncasecmp(argv[6], "only", strlen(argv[6])))
527                                                         return RESULT_SHOWUSAGE;
528                                                 else
529                                                         misdn_debug_only[port] = 1;
530                                         } else
531                                                 misdn_debug_only[port] = 0;
532                                         misdn_debug[port] = level;
533                                         ast_cli(fd, "changing debug level to %d%s for port %d\n", misdn_debug[port], misdn_debug_only[port]?" (only)":"", port);
534                                 }
535         }
536         return 0;
537 }
538
539 static int misdn_set_crypt_debug(int fd, int argc, char *argv[])
540 {
541         if (argc != 5) return RESULT_SHOWUSAGE; 
542
543         return 0;
544 }
545
546
547 static int misdn_restart_port (int fd, int argc, char *argv[])
548 {
549         int port;
550   
551         if (argc != 4)
552                 return RESULT_SHOWUSAGE;
553   
554         port = atoi(argv[3]);
555
556         misdn_lib_port_restart(port);
557
558         return 0;
559 }
560
561 static int misdn_port_up (int fd, int argc, char *argv[])
562 {
563         int port;
564         
565         if (argc != 4)
566                 return RESULT_SHOWUSAGE;
567         
568         port = atoi(argv[3]);
569         
570         misdn_lib_get_port_up(port);
571   
572         return 0;
573 }
574
575 static int misdn_port_down (int fd, int argc, char *argv[])
576 {
577         int port;
578         
579         if (argc != 4)
580                 return RESULT_SHOWUSAGE;
581         
582         port = atoi(argv[3]);
583         
584         misdn_lib_get_port_down(port);
585   
586         return 0;
587 }
588
589 static inline void show_config_description (int fd, enum misdn_cfg_elements elem)
590 {
591         char section[BUFFERSIZE];
592         char name[BUFFERSIZE];
593         char desc[BUFFERSIZE];
594         char def[BUFFERSIZE];
595         char tmp[BUFFERSIZE];
596
597         misdn_cfg_get_name(elem, tmp, sizeof(tmp));
598         term_color(name, tmp, COLOR_BRWHITE, 0, sizeof(tmp));
599         misdn_cfg_get_desc(elem, desc, sizeof(desc), def, sizeof(def));
600
601         if (elem < MISDN_CFG_LAST)
602                 term_color(section, "PORTS SECTION", COLOR_YELLOW, 0, sizeof(section));
603         else
604                 term_color(section, "GENERAL SECTION", COLOR_YELLOW, 0, sizeof(section));
605
606         if (*def)
607                 ast_cli(fd, "[%s] %s   (Default: %s)\n\t%s\n", section, name, def, desc);
608         else
609                 ast_cli(fd, "[%s] %s\n\t%s\n", section, name, desc);
610 }
611
612 static int misdn_show_config (int fd, int argc, char *argv[])
613 {
614         char buffer[BUFFERSIZE];
615         enum misdn_cfg_elements elem;
616         int linebreak;
617         int onlyport = -1;
618         int ok = 0;
619
620         if (argc >= 4) {
621                 if (!strcmp(argv[3], "description")) {
622                         if (argc == 5) {
623                                 enum misdn_cfg_elements elem = misdn_cfg_get_elem (argv[4]);
624                                 if (elem == MISDN_CFG_FIRST)
625                                         ast_cli(fd, "Unknown element: %s\n", argv[4]);
626                                 else
627                                         show_config_description(fd, elem);
628                                 return 0;
629                         }
630                         return RESULT_SHOWUSAGE;
631                 }
632                 if (!strcmp(argv[3], "descriptions")) {
633                         if ((argc == 4) || ((argc == 5) && !strcmp(argv[4], "general"))) {
634                                 for (elem = MISDN_GEN_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
635                                         show_config_description(fd, elem);
636                                         ast_cli(fd, "\n");
637                                 }
638                                 ok = 1;
639                         }
640                         if ((argc == 4) || ((argc == 5) && !strcmp(argv[4], "ports"))) {
641                                 for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_CFG_LAST; ++elem) {
642                                         show_config_description(fd, elem);
643                                         ast_cli(fd, "\n");
644                                 }
645                                 ok = 1;
646                         }
647                         return ok ? 0 : RESULT_SHOWUSAGE;
648                 }
649                 if (!sscanf(argv[3], "%d", &onlyport) || onlyport < 0) {
650                         ast_cli(fd, "Unknown option: %s\n", argv[3]);
651                         return RESULT_SHOWUSAGE;
652                 }
653         }
654         
655         if (argc == 3 || onlyport == 0) {
656                 ast_cli(fd,"Misdn General-Config: \n"); 
657                 ast_cli(fd," -> Version: chan_misdn-" CHAN_MISDN_VERSION "\n");
658                 for (elem = MISDN_GEN_FIRST + 1, linebreak = 1; elem < MISDN_GEN_LAST; elem++, linebreak++) {
659                         misdn_cfg_get_config_string( 0, elem, buffer, BUFFERSIZE);
660                         ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
661                 }
662                 ast_cli(fd, "\n");
663         }
664
665         if (onlyport < 0) {
666                 int port = misdn_cfg_get_next_port(0);
667                 for (; port > 0; port = misdn_cfg_get_next_port(port)) {
668                         ast_cli(fd, "\n[PORT %d]\n", port);
669                         for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
670                                 misdn_cfg_get_config_string( port, elem, buffer, BUFFERSIZE);
671                                 ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
672                         }       
673                         ast_cli(fd, "\n");
674                 }
675         }
676         
677         if (onlyport > 0) {
678                 if (misdn_cfg_is_port_valid(onlyport)) {
679                         ast_cli(fd, "[PORT %d]\n", onlyport);
680                         for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) {
681                                 misdn_cfg_get_config_string(onlyport, elem, buffer, BUFFERSIZE);
682                                 ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : "");
683                         }       
684                         ast_cli(fd, "\n");
685                 } else {
686                         ast_cli(fd, "Port %d is not active!\n", onlyport);
687                 }
688         }
689         return 0;
690 }
691
692 struct state_struct {
693         enum misdn_chan_state state;
694         char txt[255] ;
695 } ;
696
697 static struct state_struct state_array[] = {
698         {MISDN_NOTHING,"NOTHING"}, /* at beginning */
699         {MISDN_WAITING4DIGS,"WAITING4DIGS"}, /*  when waiting for infos */
700         {MISDN_EXTCANTMATCH,"EXTCANTMATCH"}, /*  when asterisk couldnt match our ext */
701         {MISDN_DIALING,"DIALING"}, /*  when pbx_start */
702         {MISDN_PROGRESS,"PROGRESS"}, /*  when pbx_start */
703         {MISDN_PROCEEDING,"PROCEEDING"}, /*  when pbx_start */
704         {MISDN_CALLING,"CALLING"}, /*  when misdn_call is called */
705         {MISDN_CALLING_ACKNOWLEDGE,"CALLING_ACKNOWLEDGE"}, /*  when misdn_call is called */
706         {MISDN_ALERTING,"ALERTING"}, /*  when Alerting */
707         {MISDN_BUSY,"BUSY"}, /*  when BUSY */
708         {MISDN_CONNECTED,"CONNECTED"}, /*  when connected */
709         {MISDN_DISCONNECTED,"DISCONNECTED"}, /*  when connected */
710         {MISDN_RELEASED,"RELEASED"}, /*  when connected */
711         {MISDN_BRIDGED,"BRIDGED"}, /*  when bridged */
712         {MISDN_CLEANING,"CLEANING"}, /* when hangup from * but we were connected before */
713         {MISDN_HUNGUP_FROM_MISDN,"HUNGUP_FROM_MISDN"}, /* when DISCONNECT/RELEASE/REL_COMP  cam from misdn */
714         {MISDN_HOLDED,"HOLDED"}, /* when DISCONNECT/RELEASE/REL_COMP  cam from misdn */
715         {MISDN_HOLD_DISCONNECT,"HOLD_DISCONNECT"}, /* when DISCONNECT/RELEASE/REL_COMP  cam from misdn */
716         {MISDN_HUNGUP_FROM_AST,"HUNGUP_FROM_AST"} /* when DISCONNECT/RELEASE/REL_COMP came out of */
717         /* misdn_hangup */
718 };
719
720 static char *misdn_get_ch_state(struct chan_list *p) 
721 {
722         int i;
723         static char state[8];
724         
725         if( !p) return NULL;
726   
727         for (i=0; i< sizeof(state_array)/sizeof(struct state_struct); i++) {
728                 if ( state_array[i].state == p->state) return state_array[i].txt; 
729         }
730
731         sprintf(state,"%d",p->state) ;
732
733         return state;
734 }
735
736
737
738 static void reload_config(void)
739 {
740         int i, cfg_debug;
741         chan_misdn_log(-1, 0, "Dynamic Crypting Activation is not support during reload at the moment\n");
742         
743         free_robin_list();
744         misdn_cfg_reload();
745         misdn_cfg_update_ptp();
746         misdn_cfg_get( 0, MISDN_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
747         misdn_cfg_get( 0, MISDN_GEN_DEBUG, &cfg_debug, sizeof(int));
748
749         for (i = 0;  i <= max_ports; i++) {
750                 misdn_debug[i] = cfg_debug;
751                 misdn_debug_only[i] = 0;
752         }
753 }
754
755 static int misdn_reload (int fd, int argc, char *argv[])
756 {
757         ast_cli(fd, "Reloading mISDN Config\n");
758         reload_config();
759         return 0;
760 }
761
762 static void print_bc_info (int fd, struct chan_list* help, struct misdn_bchannel* bc)
763 {
764         struct ast_channel *ast=help->ast;
765         ast_cli(fd,
766                 "* Pid:%d Prt:%d Ch:%d Mode:%s Org:%s dad:%s oad:%s rad:%s ctx:%s state:%s\n",
767
768                 bc->pid, bc->port, bc->channel,
769                 bc->nt?"NT":"TE",
770                 help->orginator == ORG_AST?"*":"I",
771                 ast?ast->exten:NULL,
772                 ast?AST_CID_P(ast):NULL,
773                 bc->rad,
774                 ast?ast->context:NULL,
775                 misdn_get_ch_state(help)
776                 );
777         if (misdn_debug[bc->port] > 0)
778                 ast_cli(fd,
779                         "  --> astname: %s\n"
780                         "  --> ch_l3id: %x\n"
781                         "  --> ch_addr: %x\n"
782                         "  --> bc_addr: %x\n"
783                         "  --> bc_l3id: %x\n"
784                         "  --> display: %s\n"
785                         "  --> activated: %d\n"
786                         "  --> state: %s\n"
787                         "  --> capability: %s\n"
788                         "  --> echo_cancel: %d\n"
789                         "  --> notone : rx %d tx:%d\n"
790                         "  --> bc_hold: %d holded_bc :%d\n",
791                         help->ast->name,
792                         help->l3id,
793                         help->addr,
794                         bc->addr,
795                         bc?bc->l3_id:-1,
796                         bc->display,
797                         
798                         bc->active,
799                         bc_state2str(bc->bc_state),
800                         bearer2str(bc->capability),
801                         bc->ec_enable,
802                         help->norxtone,help->notxtone,
803                         bc->holded, help->holded_bc?1:0
804                         );
805   
806 }
807
808 static int misdn_show_cls (int fd, int argc, char *argv[])
809 {
810         struct chan_list *help=cl_te;
811   
812         ast_cli(fd,"Chan List: %p\n",cl_te); 
813   
814         for (;help; help=help->next) {
815                 struct misdn_bchannel *bc=help->bc;   
816                 struct ast_channel *ast=help->ast;
817                 if (misdn_debug[0] > 2) ast_cli(fd, "Bc:%p Ast:%p\n", bc, ast);
818                 if (bc) {
819                         print_bc_info(fd, help, bc);
820                 } else if ( (bc=help->holded_bc) ) {
821                         chan_misdn_log(0, 0, "ITS A HOLDED BC:\n");
822                         print_bc_info(fd, help,  bc);
823                 } else {
824                         ast_cli(fd,"* Channel in unknown STATE !!! Exten:%s, Callerid:%s\n", ast->exten, AST_CID_P(ast));
825                 }
826         }
827   
828   
829         return 0;
830 }
831
832 static int misdn_show_cl (int fd, int argc, char *argv[])
833 {
834         struct chan_list *help=cl_te;
835
836         if (argc != 4)
837                 return RESULT_SHOWUSAGE;
838   
839         for (;help; help=help->next) {
840                 struct misdn_bchannel *bc=help->bc;   
841                 struct ast_channel *ast=help->ast;
842     
843                 if (bc && ast) {
844                         if (!strcasecmp(ast->name,argv[3])) {
845                                 print_bc_info(fd, help, bc);
846                                 break; 
847                         }
848                 } 
849         }
850   
851   
852         return 0;
853 }
854
855 ast_mutex_t lock;
856 int MAXTICS=8;
857
858 static int misdn_set_tics (int fd, int argc, char *argv[])
859 {
860         if (argc != 4)
861                 return RESULT_SHOWUSAGE;
862   
863         MAXTICS=atoi(argv[3]);
864   
865         return 0;
866 }
867
868 static int misdn_show_stacks (int fd, int argc, char *argv[])
869 {
870         int port;
871
872         ast_cli(fd, "BEGIN STACK_LIST:\n");
873
874         for (port=misdn_cfg_get_next_port(0); port > 0;
875              port=misdn_cfg_get_next_port(port)) {
876                 char buf[128];
877                 get_show_stack_details(port,buf);
878                 ast_cli(fd,"  %s  Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port]?"(only)":"");
879         }
880                 
881
882         return 0;
883
884 }
885
886
887 static int misdn_show_ports_stats (int fd, int argc, char *argv[])
888 {
889         int port;
890
891         ast_cli(fd, "Port\tin_calls\tout_calls\n");
892         
893         for (port=misdn_cfg_get_next_port(0); port > 0;
894              port=misdn_cfg_get_next_port(port)) {
895                 ast_cli(fd,"%d\t%d\t\t%d\n",port,misdn_in_calls[port],misdn_out_calls[port]);
896         }
897         ast_cli(fd,"\n");
898         
899         return 0;
900
901 }
902
903
904 static int misdn_show_port (int fd, int argc, char *argv[])
905 {
906         int port;
907         
908         if (argc != 4)
909                 return RESULT_SHOWUSAGE;
910   
911         port = atoi(argv[3]);
912   
913         ast_cli(fd, "BEGIN STACK_LIST:\n");
914
915         char buf[128];
916         get_show_stack_details(port,buf);
917         ast_cli(fd,"  %s  Debug:%d%s\n",buf, misdn_debug[port], misdn_debug_only[port]?"(only)":"");
918
919         
920         return 0;
921 }
922
923 static int misdn_send_cd (int fd, int argc, char *argv[])
924 {
925         char *channame; 
926         char *nr; 
927   
928         if (argc != 5)
929                 return RESULT_SHOWUSAGE;
930   
931         channame = argv[3];
932         nr = argv[4];
933         
934         ast_cli(fd, "Sending Calldeflection (%s) to %s\n",nr, channame);
935         
936         {
937                 struct chan_list *tmp=get_chan_by_ast_name(channame);
938                 
939                 if (!tmp) {
940                         ast_cli(fd, "Sending CD with nr %s to %s failed Channel does not exist\n",nr, channame);
941                         return 0; 
942                 } else {
943                         
944                         misdn_lib_send_facility(tmp->bc, FACILITY_CALLDEFLECT, nr);
945                 }
946         }
947   
948         return 0; 
949 }
950
951 static int misdn_send_digit (int fd, int argc, char *argv[])
952 {
953         char *channame; 
954         char *msg; 
955   
956         if (argc != 5)
957                 return RESULT_SHOWUSAGE;
958   
959         channame = argv[3];
960         msg = argv[4];
961
962         ast_cli(fd, "Sending %s to %s\n",msg, channame);
963   
964         {
965                 struct chan_list *tmp=get_chan_by_ast_name(channame);
966     
967                 if (!tmp) {
968                         ast_cli(fd, "Sending %s to %s failed Channel does not exist\n",msg, channame);
969                         return 0; 
970                 } else {
971 #if 1
972                         int i;
973                         int msglen = strlen(msg);
974                         for (i=0; i<msglen; i++) {
975                                 ast_cli(fd, "Sending: %c\n",msg[i]);
976                                 send_digit_to_chan(tmp, msg[i]);
977                                 /* res = ast_safe_sleep(tmp->ast, 250); */
978                                 usleep(250000);
979                                 /* res = ast_waitfor(tmp->ast,100); */
980                         }
981 #else
982                         int res;
983                         res = ast_dtmf_stream(tmp->ast,NULL,msg,250);
984 #endif
985                 }
986         }
987   
988         return 0; 
989 }
990
991 static int misdn_toggle_echocancel (int fd, int argc, char *argv[])
992 {
993         char *channame; 
994
995         if (argc != 4)
996                 return RESULT_SHOWUSAGE;
997         
998         channame = argv[3];
999   
1000         ast_cli(fd, "Toggling EchoCancel on %s\n", channame);
1001   
1002         {
1003                 struct chan_list *tmp=get_chan_by_ast_name(channame);
1004     
1005                 if (!tmp) {
1006                         ast_cli(fd, "Toggling EchoCancel %s failed Channel does not exist\n", channame);
1007                         return 0; 
1008                 } else {
1009                         tmp->bc->ec_enable=tmp->bc->ec_enable?0:1;
1010
1011                         if (tmp->bc->ec_enable) {
1012                                 manager_ec_enable(tmp->bc);
1013                         } else {
1014                                 manager_ec_disable(tmp->bc);
1015                         }
1016                 }
1017         }
1018   
1019         return 0; 
1020 }
1021
1022 static int misdn_send_display (int fd, int argc, char *argv[])
1023 {
1024         char *channame; 
1025         char *msg; 
1026   
1027         if (argc != 5)
1028                 return RESULT_SHOWUSAGE;
1029   
1030         channame = argv[3];
1031         msg = argv[4];
1032
1033         ast_cli(fd, "Sending %s to %s\n",msg, channame);
1034         {
1035                 struct chan_list *tmp;
1036                 tmp=get_chan_by_ast_name(channame);
1037     
1038                 if (tmp && tmp->bc) {
1039                         ast_copy_string(tmp->bc->display, msg, sizeof(tmp->bc->display));
1040                         misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
1041                 } else {
1042                         ast_cli(fd,"No such channel %s\n",channame);
1043                         return RESULT_FAILURE;
1044                 }
1045         }
1046
1047         return RESULT_SUCCESS ;
1048 }
1049
1050 static char *complete_ch_helper(const char *line, const char *word, int pos, int state, int rpos)
1051 {
1052         struct ast_channel *c;
1053         int which=0;
1054         char *ret;
1055         if (pos != rpos)
1056                 return NULL;
1057         c = ast_channel_walk_locked(NULL);
1058         while(c) {
1059                 if (!strncasecmp(word, c->name, strlen(word))) {
1060                         if (++which > state)
1061                                 break;
1062                 }
1063                 ast_mutex_unlock(&c->lock);
1064                 c = ast_channel_walk_locked(c);
1065         }
1066         if (c) {
1067                 ret = strdup(c->name);
1068                 ast_mutex_unlock(&c->lock);
1069         } else
1070                 ret = NULL;
1071         return ret;
1072 }
1073
1074 static char *complete_ch(const char *line, const char *word, int pos, int state)
1075 {
1076         return complete_ch_helper(line, word, pos, state, 3);
1077 }
1078
1079 static char *complete_debug_port (const char *line, const char *word, int pos, int state)
1080 {
1081         if (state)
1082                 return NULL;
1083
1084         switch (pos) {
1085         case 4: if (*word == 'p')
1086                                 return strdup("port");
1087                         else if (*word == 'o')
1088                                 return strdup("only");
1089                         break;
1090         case 6: if (*word == 'o')
1091                                 return strdup("only");
1092                         break;
1093         }
1094         return NULL;
1095 }
1096
1097 static char *complete_show_config (const char *line, const char *word, int pos, int state)
1098 {
1099         char buffer[BUFFERSIZE];
1100         enum misdn_cfg_elements elem;
1101         int wordlen = strlen(word);
1102         int which = 0;
1103         int port = 0;
1104
1105         switch (pos) {
1106         case 3: if ((!strncmp(word, "description", wordlen)) && (++which > state))
1107                                 return strdup("description");
1108                         if ((!strncmp(word, "descriptions", wordlen)) && (++which > state))
1109                                 return strdup("descriptions");
1110                         if ((!strncmp(word, "0", wordlen)) && (++which > state))
1111                                 return strdup("0");
1112                         while ((port = misdn_cfg_get_next_port(port)) != -1) {
1113                                 snprintf(buffer, sizeof(buffer), "%d", port);
1114                                 if ((!strncmp(word, buffer, wordlen)) && (++which > state)) {
1115                                         return strdup(buffer);
1116                                 }
1117                         }
1118                         break;
1119         case 4:
1120                         if (strstr(line, "description ")) {
1121                                 for (elem = MISDN_CFG_FIRST + 1; elem < MISDN_GEN_LAST; ++elem) {
1122                                         if ((elem == MISDN_CFG_LAST) || (elem == MISDN_GEN_FIRST))
1123                                                 continue;
1124                                         misdn_cfg_get_name(elem, buffer, BUFFERSIZE);
1125                                         if (!wordlen || !strncmp(word, buffer, wordlen)) {
1126                                                 if (++which > state)
1127                                                         return strdup(buffer);
1128                                         }
1129                                 }
1130                         } else if (strstr(line, "descriptions ")) {
1131                                 if ((!wordlen || !strncmp(word, "general", wordlen)) && (++which > state))
1132                                         return strdup("general");
1133                                 if ((!wordlen || !strncmp(word, "ports", wordlen)) && (++which > state))
1134                                         return strdup("ports");
1135                         }
1136                         break;
1137         }
1138         return NULL;
1139 }
1140
1141 static struct ast_cli_entry cli_send_cd =
1142 { {"misdn","send","calldeflect", NULL},
1143   misdn_send_cd,
1144   "Sends CallDeflection to mISDN Channel", 
1145   "Usage: misdn send calldeflect <channel> \"<nr>\" \n",
1146   complete_ch
1147 };
1148
1149 static struct ast_cli_entry cli_send_digit =
1150 { {"misdn","send","digit", NULL},
1151   misdn_send_digit,
1152   "Sends DTMF Digit to mISDN Channel", 
1153   "Usage: misdn send digit <channel> \"<msg>\" \n"
1154   "       Send <digit> to <channel> as DTMF Tone\n"
1155   "       when channel is a mISDN channel\n",
1156   complete_ch
1157 };
1158
1159 static struct ast_cli_entry cli_toggle_echocancel =
1160 { {"misdn","toggle","echocancel", NULL},
1161   misdn_toggle_echocancel,
1162   "Toggles EchoCancel on mISDN Channel", 
1163   "Usage: misdn toggle echocancel <channel>\n", 
1164   complete_ch
1165 };
1166
1167 static struct ast_cli_entry cli_send_display =
1168 { {"misdn","send","display", NULL},
1169   misdn_send_display,
1170   "Sends Text to mISDN Channel", 
1171   "Usage: misdn send display <channel> \"<msg>\" \n"
1172   "       Send <msg> to <channel> as Display Message\n"
1173   "       when channel is a mISDN channel\n",
1174   complete_ch
1175 };
1176
1177 static struct ast_cli_entry cli_show_config =
1178 { {"misdn","show","config", NULL},
1179   misdn_show_config,
1180   "Shows internal mISDN config, read from cfg-file", 
1181   "Usage: misdn show config [<port> | description <config element> | descriptions [general|ports]]\n"
1182   "       Use 0 for <port> to only print the general config.\n",
1183   complete_show_config
1184 };
1185  
1186 static struct ast_cli_entry cli_reload =
1187 { {"misdn","reload", NULL},
1188   misdn_reload,
1189   "Reloads internal mISDN config, read from cfg-file", 
1190   "Usage: misdn reload\n"
1191 };
1192
1193 static struct ast_cli_entry cli_set_tics =
1194 { {"misdn","set","tics", NULL},
1195   misdn_set_tics,
1196   "", 
1197   "\n"
1198 };
1199
1200 static struct ast_cli_entry cli_show_cls =
1201 { {"misdn","show","channels", NULL},
1202   misdn_show_cls,
1203   "Shows internal mISDN chan_list", 
1204   "Usage: misdn show channels\n"
1205 };
1206
1207 static struct ast_cli_entry cli_show_cl =
1208 { {"misdn","show","channel", NULL},
1209   misdn_show_cl,
1210   "Shows internal mISDN chan_list", 
1211   "Usage: misdn show channels\n",
1212   complete_ch
1213 };
1214
1215
1216
1217 static struct ast_cli_entry cli_restart_port =
1218 { {"misdn","restart","port", NULL},
1219   misdn_restart_port,
1220   "Restarts the given port", 
1221   "Usage: misdn restart port\n"
1222 };
1223
1224 static struct ast_cli_entry cli_port_up =
1225 { {"misdn","port","up", NULL},
1226   misdn_port_up,
1227   "Tries to establish L1 on the given port", 
1228   "Usage: misdn port up <port>\n"
1229 };
1230
1231 static struct ast_cli_entry cli_port_down =
1232 { {"misdn","port","down", NULL},
1233   misdn_port_down,
1234   "Tries to deacivate the L1 on the given port", 
1235   "Usage: misdn port down <port>\n"
1236 };
1237
1238
1239
1240 static struct ast_cli_entry cli_show_stacks =
1241 { {"misdn","show","stacks", NULL},
1242   misdn_show_stacks,
1243   "Shows internal mISDN stack_list", 
1244   "Usage: misdn show stacks\n"
1245 };
1246
1247 static struct ast_cli_entry cli_show_ports_stats =
1248 { {"misdn","show","ports","stats", NULL},
1249   misdn_show_ports_stats,
1250   "Shows chan_misdns call statistics per port", 
1251   "Usage: misdn show port stats\n"
1252 };
1253
1254
1255 static struct ast_cli_entry cli_show_port =
1256 { {"misdn","show","port", NULL},
1257   misdn_show_port,
1258   "Shows detailed information for given port", 
1259   "Usage: misdn show port <port>\n"
1260 };
1261
1262 static struct ast_cli_entry cli_set_debug =
1263 { {"misdn","set","debug", NULL},
1264   misdn_set_debug,
1265   "Sets Debuglevel of chan_misdn",
1266   "Usage: misdn set debug <level> [only] | [port <port> [only]]\n",
1267   complete_debug_port
1268 };
1269
1270 static struct ast_cli_entry cli_set_crypt_debug =
1271 { {"misdn","set","crypt","debug", NULL},
1272   misdn_set_crypt_debug,
1273   "Sets CryptDebuglevel of chan_misdn, at the moment, level={1,2}", 
1274   "Usage: misdn set crypt debug <level>\n"
1275 };
1276 /*** CLI END ***/
1277
1278
1279 static int update_config (struct chan_list *ch, int orig) 
1280 {
1281         if (!ch) {
1282                 ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
1283                 return -1;
1284         }
1285         
1286         struct ast_channel *ast=ch->ast;
1287         struct misdn_bchannel *bc=ch->bc;
1288         if (! ast || ! bc ) {
1289                 ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
1290                 return -1;
1291         }
1292         
1293         int port=bc->port;
1294         
1295         chan_misdn_log(5,port,"update_config: Getting Config\n");
1296
1297
1298         int hdlc=0;
1299         misdn_cfg_get( port, MISDN_CFG_HDLC, &hdlc, sizeof(int));
1300         
1301         if (hdlc) {
1302                 switch (bc->capability) {
1303                 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
1304                 case INFO_CAPABILITY_DIGITAL_RESTRICTED:
1305                         chan_misdn_log(1,bc->port," --> CONF HDLC\n");
1306                         bc->hdlc=1;
1307                         break;
1308                 }
1309                 
1310         }
1311         
1312         
1313         int pres, screen;
1314                         
1315         misdn_cfg_get( port, MISDN_CFG_PRES, &pres, sizeof(int));
1316         misdn_cfg_get( port, MISDN_CFG_SCREEN, &screen, sizeof(int));
1317         chan_misdn_log(2,port," --> pres: %d screen: %d\n",pres, screen);
1318                 
1319         if ( (pres + screen) < 0 ) {
1320
1321                 chan_misdn_log(2,port," --> pres: %x\n", ast->cid.cid_pres);
1322                         
1323                 switch (ast->cid.cid_pres & 0x60){
1324                                 
1325                 case AST_PRES_RESTRICTED:
1326                         bc->pres=1;
1327                         chan_misdn_log(2, port, " --> PRES: Restricted (0x1)\n");
1328                         break;
1329                                 
1330                                 
1331                 case AST_PRES_UNAVAILABLE:
1332                         bc->pres=2;
1333                         chan_misdn_log(2, port, " --> PRES: Unavailable (0x2)\n");
1334                         break;
1335                                 
1336                 default:
1337                         bc->pres=0;
1338                         chan_misdn_log(2, port, " --> PRES: Allowed (0x0)\n");
1339                 }
1340                         
1341                 switch (ast->cid.cid_pres & 0x3){
1342                                 
1343                 case AST_PRES_USER_NUMBER_UNSCREENED:
1344                         bc->screen=0;
1345                         chan_misdn_log(2, port, " --> SCREEN: Unscreened (0x0)\n");
1346                         break;
1347
1348                 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
1349                         bc->screen=1;
1350                         chan_misdn_log(2, port, " --> SCREEN: Passed Screen (0x1)\n");
1351                         break;
1352                 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
1353                         bc->screen=2;
1354                         chan_misdn_log(2, port, " --> SCREEN: Failed Screen (0x2)\n");
1355                         break;
1356                                 
1357                 case AST_PRES_NETWORK_NUMBER:
1358                         bc->screen=3;
1359                         chan_misdn_log(2, port, " --> SCREEN: Network Nr. (0x3)\n");
1360                         break;
1361                                 
1362                 default:
1363                         bc->screen=0;
1364                         chan_misdn_log(2, port, " --> SCREEN: Unscreened (0x0)\n");
1365                 }
1366
1367                         
1368         } else {
1369                 bc->screen=screen;
1370                 bc->pres=pres;
1371         }
1372
1373         return 0;
1374         
1375 }
1376
1377
1378
1379
1380 static void config_jitterbuffer(struct chan_list *ch)
1381 {
1382         struct misdn_bchannel *bc=ch->bc;
1383         int len=ch->jb_len, threshold=ch->jb_upper_threshold;
1384         
1385         chan_misdn_log(5,bc->port, "config_jb: Called\n");
1386         
1387         if ( ! len ) {
1388                 chan_misdn_log(1,bc->port, "config_jb: Deactivating Jitterbuffer\n");
1389                 bc->nojitter=1;
1390         } else {
1391                 
1392                 if (len <=100 || len > 8000) {
1393                         chan_misdn_log(-1,bc->port,"config_jb: Jitterbuffer out of Bounds, setting to 1000\n");
1394                         len=1000;
1395                 }
1396                 
1397                 if ( threshold > len ) {
1398                         chan_misdn_log(-1,bc->port,"config_jb: Jitterbuffer Threshold > Jitterbuffer setting to Jitterbuffer -1\n");
1399                 }
1400                 
1401                 if ( ch->jb) {
1402                         cb_log(0,bc->port,"config_jb: We've got a Jitterbuffer Already on this port.\n");
1403                         misdn_jb_destroy(ch->jb);
1404                         ch->jb=NULL;
1405                 }
1406                 
1407                 ch->jb=misdn_jb_init(len, threshold);
1408
1409                 if (!ch->jb ) 
1410                         bc->nojitter=1;
1411         }
1412 }
1413
1414
1415 void debug_numplan(int port, int numplan, char *type)
1416 {
1417         switch (numplan) {
1418         case NUMPLAN_INTERNATIONAL:
1419                 chan_misdn_log(2, port, " --> %s: International\n",type);
1420                 break;
1421         case NUMPLAN_NATIONAL:
1422                 chan_misdn_log(2, port, " --> %s: National\n",type);
1423                 break;
1424         case NUMPLAN_SUBSCRIBER:
1425                 chan_misdn_log(2, port, " --> %s: Subscriber\n",type);
1426                 break;
1427         case NUMPLAN_UNKNOWN:
1428                 chan_misdn_log(2, port, " --> %s: Unknown\n",type);
1429                 break;
1430                 /* Maybe we should cut off the prefix if present ? */
1431         default:
1432                 chan_misdn_log(0, port, " --> !!!! Wrong dialplan setting, please see the misdn.conf sample file\n ");
1433                 break;
1434         }
1435 }
1436
1437
1438
1439
1440
1441 static int read_config(struct chan_list *ch, int orig) {
1442
1443         if (!ch) {
1444                 ast_log(LOG_WARNING, "Cannot configure without chanlist\n");
1445                 return -1;
1446         }
1447
1448         struct ast_channel *ast=ch->ast;
1449         struct misdn_bchannel *bc=ch->bc;
1450         if (! ast || ! bc ) {
1451                 ast_log(LOG_WARNING, "Cannot configure without ast || bc\n");
1452                 return -1;
1453         }
1454         
1455         int port=bc->port;
1456         
1457         chan_misdn_log(1,port,"read_config: Getting Config\n");
1458
1459         char lang[BUFFERSIZE+1];
1460         
1461
1462         misdn_cfg_get( port, MISDN_CFG_LANGUAGE, lang, BUFFERSIZE);
1463         ast_string_field_set(ast, language, lang);
1464
1465         char localmusicclass[BUFFERSIZE+1];
1466         
1467         misdn_cfg_get( port, MISDN_CFG_MUSICCLASS, localmusicclass, BUFFERSIZE);
1468         ast_string_field_set(ast, musicclass, localmusicclass);
1469         
1470         
1471         misdn_cfg_get( port, MISDN_CFG_TXGAIN, &bc->txgain, sizeof(int));
1472         misdn_cfg_get( port, MISDN_CFG_RXGAIN, &bc->rxgain, sizeof(int));
1473         
1474         misdn_cfg_get( port, MISDN_CFG_INCOMING_EARLY_AUDIO, &ch->incoming_early_audio, sizeof(int));
1475         
1476         misdn_cfg_get( port, MISDN_CFG_SENDDTMF, &bc->send_dtmf, sizeof(int));
1477
1478         misdn_cfg_get( port, MISDN_CFG_NEED_MORE_INFOS, &bc->need_more_infos, sizeof(int));
1479         
1480         misdn_cfg_get( port, MISDN_CFG_FAR_ALERTING, &ch->far_alerting, sizeof(int));
1481
1482         misdn_cfg_get( port, MISDN_CFG_ALLOWED_BEARERS, &ch->allowed_bearers, BUFFERSIZE);
1483         
1484         
1485         int hdlc=0;
1486         misdn_cfg_get( port, MISDN_CFG_HDLC, &hdlc, sizeof(int));
1487         
1488         if (hdlc) {
1489                 switch (bc->capability) {
1490                 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
1491                 case INFO_CAPABILITY_DIGITAL_RESTRICTED:
1492                         chan_misdn_log(1,bc->port," --> CONF HDLC\n");
1493                         bc->hdlc=1;
1494                         break;
1495                 }
1496                 
1497         }
1498         /*Initialize new Jitterbuffer*/
1499         {
1500                 misdn_cfg_get( port, MISDN_CFG_JITTERBUFFER, &ch->jb_len, sizeof(int));
1501                 misdn_cfg_get( port, MISDN_CFG_JITTERBUFFER_UPPER_THRESHOLD, &ch->jb_upper_threshold, sizeof(int));
1502                 
1503                 config_jitterbuffer(ch);
1504         }
1505         
1506         misdn_cfg_get( bc->port, MISDN_CFG_CONTEXT, ch->context, sizeof(ch->context));
1507         
1508         ast_copy_string (ast->context,ch->context,sizeof(ast->context));        
1509         {
1510                 int ec, ectr;
1511                 
1512                 misdn_cfg_get( port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int));
1513                 
1514                 misdn_cfg_get( port, MISDN_CFG_ECHOTRAINING, &ectr, sizeof(int));
1515                 if (ec == 1 ) {
1516                         bc->ec_enable=1;
1517                 } else if ( ec > 1 ) {
1518                         bc->ec_enable=1;
1519                         bc->ec_deftaps=ec;
1520                 }
1521                 
1522                 if ( ectr >= 0 ) {
1523                         bc->ec_training=ectr;
1524                 }
1525         }
1526         
1527         {
1528                 int eb3;
1529                 
1530                 misdn_cfg_get( bc->port, MISDN_CFG_EARLY_BCONNECT, &eb3, sizeof(int));
1531                 bc->early_bconnect=eb3;
1532         }
1533         
1534         port=bc->port;
1535         
1536         {
1537                 char buf[256];
1538                 ast_group_t pg,cg;
1539                 
1540                 misdn_cfg_get(port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg));
1541                 misdn_cfg_get(port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg));
1542                 
1543                 chan_misdn_log(5, port, " --> * CallGrp:%s PickupGrp:%s\n",ast_print_group(buf,sizeof(buf),cg),ast_print_group(buf,sizeof(buf),pg));
1544                 ast->pickupgroup=pg;
1545                 ast->callgroup=cg;
1546         }
1547         
1548         if ( orig  == ORG_AST) {
1549                 misdn_cfg_get( port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(bc->te_choose_channel), sizeof(int));
1550                 
1551                 {
1552                         char callerid[BUFFERSIZE+1];
1553                         misdn_cfg_get( port, MISDN_CFG_CALLERID, callerid, BUFFERSIZE);
1554                         if ( ! ast_strlen_zero(callerid) ) {
1555                                 chan_misdn_log(1, port, " --> * Setting Cid to %s\n", callerid);
1556                                 {
1557                                         int l = sizeof(bc->oad);
1558                                         strncpy(bc->oad,callerid, l);
1559                                         bc->oad[l-1] = 0;
1560                                 }
1561
1562                         }
1563
1564                         
1565                         misdn_cfg_get( port, MISDN_CFG_DIALPLAN, &bc->dnumplan, sizeof(int));
1566                         misdn_cfg_get( port, MISDN_CFG_LOCALDIALPLAN, &bc->onumplan, sizeof(int));
1567                         misdn_cfg_get( port, MISDN_CFG_CPNDIALPLAN, &bc->cpnnumplan, sizeof(int));
1568                         debug_numplan(port, bc->dnumplan,"TON");
1569                         debug_numplan(port, bc->onumplan,"LTON");
1570                         debug_numplan(port, bc->cpnnumplan,"CTON");
1571                 }
1572
1573                 
1574                 
1575         } else { /** ORIGINATOR MISDN **/
1576         
1577                 misdn_cfg_get( port, MISDN_CFG_CPNDIALPLAN, &bc->cpnnumplan, sizeof(int));
1578                 debug_numplan(port, bc->cpnnumplan,"CTON");
1579                 
1580                 char prefix[BUFFERSIZE+1]="";
1581                 switch( bc->onumplan ) {
1582                 case NUMPLAN_INTERNATIONAL:
1583                         misdn_cfg_get( bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
1584                         break;
1585                         
1586                 case NUMPLAN_NATIONAL:
1587                         misdn_cfg_get( bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
1588                         break;
1589                 default:
1590                         break;
1591                 }
1592                 
1593                 {
1594                         int l = strlen(prefix) + strlen(bc->oad);
1595                         char tmp[l+1];
1596                         strcpy(tmp,prefix);
1597                         strcat(tmp,bc->oad);
1598                         strcpy(bc->oad,tmp);
1599                 }
1600                 
1601                 if (!ast_strlen_zero(bc->dad)) {
1602                         ast_copy_string(bc->orig_dad,bc->dad, sizeof(bc->orig_dad));
1603                 }
1604                 
1605                 if ( ast_strlen_zero(bc->dad) && !ast_strlen_zero(bc->keypad)) {
1606                         ast_copy_string(bc->dad,bc->keypad, sizeof(bc->dad));
1607                 }
1608
1609                 prefix[0] = 0;
1610                 
1611                 switch( bc->dnumplan ) {
1612                 case NUMPLAN_INTERNATIONAL:
1613                         misdn_cfg_get( bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
1614                         break;
1615                 case NUMPLAN_NATIONAL:
1616                         misdn_cfg_get( bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
1617                         break;
1618                 default:
1619                         break;
1620                 }
1621                 
1622                 {
1623                         int l = strlen(prefix) + strlen(bc->dad);
1624                         char tmp[l+1];
1625                         strcpy(tmp,prefix);
1626                         strcat(tmp,bc->dad);
1627                         strcpy(bc->dad,tmp);
1628                 }
1629                 
1630                 if ( strcmp(bc->dad,ast->exten)) {
1631                         ast_copy_string(ast->exten, bc->dad, sizeof(ast->exten));
1632                 }
1633                 
1634                 ast_set_callerid(ast, bc->oad, NULL, bc->oad);
1635                 
1636                 if ( !ast_strlen_zero(bc->rad) ) {
1637                         if (ast->cid.cid_rdnis)
1638                                 free(ast->cid.cid_rdnis);
1639                         ast->cid.cid_rdnis = strdup(bc->rad);
1640                 }
1641         } /* ORIG MISDN END */
1642
1643         return 0;
1644 }
1645
1646
1647 /*****************************/
1648 /*** AST Indications Start ***/
1649 /*****************************/
1650
1651 static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
1652 {
1653         int port=0;
1654         int r;
1655         struct chan_list *ch=MISDN_ASTERISK_TECH_PVT(ast);
1656         struct misdn_bchannel *newbc;
1657         char *opts=NULL, *ext,*tokb;
1658         char dest_cp[256];
1659
1660         {
1661                 strncpy(dest_cp,dest,sizeof(dest_cp)-1);
1662                 dest_cp[sizeof(dest_cp)]=0;
1663                 
1664                 ext=strtok_r(dest_cp,"/",&tokb);
1665                 
1666                 if (ext) {
1667                         ext=strtok_r(NULL,"/",&tokb);
1668                         if (ext) {
1669                                 opts=strtok_r(NULL,"/",&tokb);
1670                         } else {
1671                                 chan_misdn_log(-1,0,"misdn_call: No Extension given!\n");
1672                                 return -1;
1673                         }
1674                 }
1675         }
1676
1677         if (!ast) {
1678                 ast_log(LOG_WARNING, " --> ! misdn_call called on ast_channel *ast where ast == NULL\n");
1679                 return -1;
1680         }
1681
1682         if (((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) || !dest  ) {
1683                 ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
1684                 ast->hangupcause=41;
1685                 ast_setstate(ast, AST_STATE_DOWN);
1686                 return -1;
1687         }
1688
1689         if (!ch) {
1690                 ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
1691                 ast->hangupcause=41;
1692                 ast_setstate(ast, AST_STATE_DOWN);
1693                 return -1;
1694         }
1695         
1696         newbc=ch->bc;
1697         
1698         if (!newbc) {
1699                 ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name);
1700                 ast->hangupcause=41;
1701                 ast_setstate(ast, AST_STATE_DOWN);
1702                 return -1;
1703         }
1704         
1705         port=newbc->port;
1706         strncpy(newbc->dad,ext,sizeof( newbc->dad));
1707         strncpy(ast->exten,ext,sizeof(ast->exten));
1708
1709         int exceed;
1710         if ((exceed=add_out_calls(port))) {
1711                 char tmp[16];
1712                 sprintf(tmp,"%d",exceed);
1713                 pbx_builtin_setvar_helper(ast,"MAX_OVERFLOW",tmp);
1714                 return -1;
1715         }
1716         
1717         chan_misdn_log(1, port, "* CALL: %s\n",dest);
1718         
1719         chan_misdn_log(1, port, " --> * dad:%s tech:%s ctx:%s\n",ast->exten,ast->name, ast->context);
1720         
1721         chan_misdn_log(3, port, " --> * adding2newbc ext %s\n",ast->exten);
1722         if (ast->exten) {
1723                 int l = sizeof(newbc->dad);
1724                 strncpy(newbc->dad,ast->exten, l);
1725                 newbc->dad[l-1] = 0;
1726         }
1727         newbc->rad[0]=0;
1728         chan_misdn_log(3, port, " --> * adding2newbc callerid %s\n",AST_CID_P(ast));
1729         if (ast_strlen_zero(newbc->oad) && AST_CID_P(ast) ) {
1730
1731                 if (AST_CID_P(ast)) {
1732                         int l = sizeof(newbc->oad);
1733                         strncpy(newbc->oad,AST_CID_P(ast), l);
1734                         newbc->oad[l-1] = 0;
1735                 }
1736         }
1737         
1738         {
1739                 struct chan_list *ch=MISDN_ASTERISK_TECH_PVT(ast);
1740                 if (!ch) { ast_verbose("No chan_list in misdn_call"); return -1;}
1741                 
1742                 newbc->capability=ast->transfercapability;
1743                 pbx_builtin_setvar_helper(ast,"TRANSFERCAPABILITY",ast_transfercapability2str(newbc->capability));
1744                 if ( ast->transfercapability == INFO_CAPABILITY_DIGITAL_UNRESTRICTED) {
1745                         chan_misdn_log(2, port, " --> * Call with flag Digital\n");
1746                 }
1747                 
1748
1749                 /* update screening and presentation */ 
1750                 update_config(ch,ORG_AST);
1751                 
1752                 /* fill in some ies from channel vary*/
1753                 import_ch(ast, newbc, ch);
1754                 
1755                 /* Finally The Options Override Everything */
1756                 if (opts)
1757                         misdn_set_opt_exec(ast,opts);
1758                 else
1759                         chan_misdn_log(2,port,"NO OPTS GIVEN\n");
1760                 
1761                 ch->state=MISDN_CALLING;
1762                 
1763                 r=misdn_lib_send_event( newbc, EVENT_SETUP );
1764                 
1765                 /** we should have l3id after sending setup **/
1766                 ch->l3id=newbc->l3_id;
1767         }
1768         
1769         if ( r == -ENOCHAN  ) {
1770                 chan_misdn_log(0, port, " --> * Theres no Channel at the moment .. !\n");
1771                 chan_misdn_log(1, port, " --> * SEND: State Down pid:%d\n",newbc?newbc->pid:-1);
1772                 ast->hangupcause=34;
1773                 ast_setstate(ast, AST_STATE_DOWN);
1774                 return -1;
1775         }
1776         
1777         chan_misdn_log(1, port, " --> * SEND: State Dialing pid:%d\n",newbc?newbc->pid:1);
1778
1779         ast_setstate(ast, AST_STATE_DIALING);
1780         ast->hangupcause=16;
1781         
1782         if (newbc->nt) stop_bc_tones(ch);
1783         
1784         return 0; 
1785 }
1786
1787
1788 static int misdn_answer(struct ast_channel *ast)
1789 {
1790         struct chan_list *p;
1791
1792         
1793         if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast)) ) return -1;
1794         
1795         chan_misdn_log(1, p? (p->bc? p->bc->port : 0) : 0, "* ANSWER:\n");
1796         
1797         if (!p) {
1798                 ast_log(LOG_WARNING, " --> Channel not connected ??\n");
1799                 ast_queue_hangup(ast);
1800         }
1801
1802         if (!p->bc) {
1803                 chan_misdn_log(1, 0, " --> Got Answer, but theres no bc obj ??\n");
1804
1805                 ast_queue_hangup(ast);
1806         }
1807
1808         {
1809                 const char *tmp_key = pbx_builtin_getvar_helper(p->ast, "CRYPT_KEY");
1810                 
1811                 if (tmp_key ) {
1812                         chan_misdn_log(1, p->bc->port, " --> Connection will be BF crypted\n");
1813                         {
1814                                 int l = sizeof(p->bc->crypt_key);
1815                                 strncpy(p->bc->crypt_key,tmp_key, l);
1816                                 p->bc->crypt_key[l-1] = 0;
1817                         }
1818                 } else {
1819                         chan_misdn_log(3, p->bc->port, " --> Connection is without BF encryption\n");
1820                 }
1821     
1822         }
1823
1824         {
1825                 const char *nodsp=pbx_builtin_getvar_helper(ast, "MISDN_DIGITAL_TRANS");
1826                 if (nodsp) {
1827                         chan_misdn_log(1, p->bc->port, " --> Connection is transparent digital\n");
1828                         p->bc->nodsp=1;
1829                         p->bc->hdlc=0;
1830                         p->bc->nojitter=1;
1831                 }
1832         }
1833         
1834         p->state = MISDN_CONNECTED;
1835         misdn_lib_echo(p->bc,0);
1836         stop_indicate(p);
1837
1838         if ( ast_strlen_zero(p->bc->cad) ) {
1839                 chan_misdn_log(2,p->bc->port," --> empty cad using dad\n");
1840                 ast_copy_string(p->bc->cad,p->bc->dad,sizeof(p->bc->cad));
1841         }
1842
1843         misdn_lib_send_event( p->bc, EVENT_CONNECT);
1844         start_bc_tones(p);
1845         
1846         return 0;
1847 }
1848
1849 static int misdn_digit(struct ast_channel *ast, char digit )
1850 {
1851         struct chan_list *p;
1852         
1853         if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast))) return -1;
1854
1855         struct misdn_bchannel *bc=p->bc;
1856         chan_misdn_log(1, bc?bc->port:0, "* IND : Digit %c\n",digit);
1857         
1858         if (!bc) {
1859                 ast_log(LOG_WARNING, " --> !! Got Digit Event withut having bchannel Object\n");
1860                 return -1;
1861         }
1862         
1863         switch (p->state ) {
1864                 case MISDN_CALLING:
1865                 {
1866                         
1867                         char buf[8];
1868                         buf[0]=digit;
1869                         buf[1]=0;
1870                         
1871                         int l = sizeof(bc->infos_pending);
1872                         strncat(bc->infos_pending,buf,l);
1873                         bc->infos_pending[l-1] = 0;
1874                 }
1875                 break;
1876                 case MISDN_CALLING_ACKNOWLEDGE:
1877                 {
1878                         bc->info_dad[0]=digit;
1879                         bc->info_dad[1]=0;
1880                         
1881                         {
1882                                 int l = sizeof(bc->dad);
1883                                 strncat(bc->dad,bc->info_dad, l - strlen(bc->dad));
1884                                 bc->dad[l-1] = 0;
1885                 }
1886                         {
1887                                 int l = sizeof(p->ast->exten);
1888                                 strncpy(p->ast->exten, bc->dad, l);
1889                                 p->ast->exten[l-1] = 0;
1890                         }
1891                         
1892                         misdn_lib_send_event( bc, EVENT_INFORMATION);
1893                 }
1894                 break;
1895                 
1896                 default:
1897                         if ( bc->send_dtmf ) {
1898                                 send_digit_to_chan(p,digit);
1899                         }
1900                 break;
1901         }
1902         
1903         return 0;
1904 }
1905
1906
1907 static int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast)
1908 {
1909         struct chan_list *p;
1910         
1911         if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast) )) return -1;
1912         
1913         chan_misdn_log(1, p->bc?p->bc->port:0, "* IND: Got Fixup State:%s Holded:%d L3id:%x\n", misdn_get_ch_state(p), p->holded, p->l3id);
1914         
1915         p->ast = ast ;
1916         p->state=MISDN_CONNECTED;
1917   
1918         return 0;
1919 }
1920
1921
1922
1923 static int misdn_indication(struct ast_channel *ast, int cond, const void *data, size_t datalen)
1924 {
1925         struct chan_list *p;
1926
1927   
1928         if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast))) {
1929                 ast_log(LOG_WARNING, "Returnded -1 in misdn_indication\n");
1930                 return -1;
1931         }
1932         
1933         if (!p->bc ) {
1934                 chan_misdn_log(1, 0, "* IND : Indication from %s\n",ast->exten);
1935                 ast_log(LOG_WARNING, "Private Pointer but no bc ?\n");
1936                 return -1;
1937         }
1938         
1939         chan_misdn_log(1, p->bc->port, "* IND : Indication [%d] from %s\n",cond, ast->exten);
1940         
1941         switch (cond) {
1942         case AST_CONTROL_BUSY:
1943                 chan_misdn_log(1, p->bc->port, "* IND :\tbusy\n");
1944                 chan_misdn_log(1, p->bc->port, " --> * SEND: State Busy pid:%d\n",p->bc?p->bc->pid:-1);
1945                 ast_setstate(ast,AST_STATE_BUSY);
1946
1947                 p->bc->out_cause=17;
1948                 if (p->state != MISDN_CONNECTED) {
1949                         start_bc_tones(p);
1950                         misdn_lib_send_event( p->bc, EVENT_DISCONNECT);
1951                 } else {
1952                         chan_misdn_log(-1, p->bc->port, " --> !! Got Busy in Connected State !?! ast:%s\n", ast->name);
1953                 }
1954                 return -1;
1955                 break;
1956         case AST_CONTROL_RING:
1957                 chan_misdn_log(1, p->bc->port, " --> * IND :\tring pid:%d\n",p->bc?p->bc->pid:-1);
1958                 return -1;
1959                 break;
1960                 
1961         case AST_CONTROL_RINGING:
1962                 switch (p->state) {
1963                         case MISDN_ALERTING:
1964                                 chan_misdn_log(1, p->bc->port, " --> * IND :\tringing pid:%d but I was Ringing before, so ignoreing it\n",p->bc?p->bc->pid:-1);
1965                                 break;
1966                         case MISDN_CONNECTED:
1967                                 chan_misdn_log(1, p->bc->port, " --> * IND :\tringing pid:%d but Connected, so just send TONE_ALERTING without state changes \n",p->bc?p->bc->pid:-1);
1968                                 return -1;
1969                                 break;
1970                         default:
1971                                 p->state=MISDN_ALERTING;
1972                                 chan_misdn_log(1, p->bc->port, " --> * IND :\tringing pid:%d\n",p->bc?p->bc->pid:-1);
1973                                 misdn_lib_send_event( p->bc, EVENT_ALERTING);
1974                         
1975                                 if (p->other_ch && p->other_ch->bc) {
1976                                         if (misdn_inband_avail(p->other_ch->bc)) {
1977                                                 chan_misdn_log(1,p->bc->port, " --> other End is mISDN and has inband info available\n");
1978                                                 break;
1979                                         }
1980
1981                                         if (!p->other_ch->bc->nt) {
1982                                                 chan_misdn_log(1,p->bc->port, " --> other End is mISDN TE so it has inband info for sure (?)\n");
1983                                                 break;
1984                                         }
1985                                 }
1986
1987                                 chan_misdn_log(1, p->bc->port, " --> * SEND: State Ring pid:%d\n",p->bc?p->bc->pid:-1);
1988                                 ast_setstate(ast,AST_STATE_RINGING);
1989                         
1990                                 if ( !p->bc->nt && (p->orginator==ORG_MISDN) && !p->incoming_early_audio ) 
1991                                         chan_misdn_log(1,p->bc->port, " --> incoming_early_audio off\n");
1992                                 else 
1993                                         return -1;
1994                 }
1995                 break;
1996         case AST_CONTROL_ANSWER:
1997                 chan_misdn_log(1, p->bc->port, " --> * IND :\tanswer pid:%d\n",p->bc?p->bc->pid:-1);
1998                 start_bc_tones(p);
1999                 break;
2000         case AST_CONTROL_TAKEOFFHOOK:
2001                 chan_misdn_log(1, p->bc->port, " --> *\ttakeoffhook pid:%d\n",p->bc?p->bc->pid:-1);
2002                 return -1;
2003                 break;
2004         case AST_CONTROL_OFFHOOK:
2005                 chan_misdn_log(1, p->bc->port, " --> *\toffhook pid:%d\n",p->bc?p->bc->pid:-1);
2006                 return -1;
2007                 break; 
2008         case AST_CONTROL_FLASH:
2009                 chan_misdn_log(1, p->bc->port, " --> *\tflash pid:%d\n",p->bc?p->bc->pid:-1);
2010                 break;
2011         case AST_CONTROL_PROGRESS:
2012                 chan_misdn_log(1, p->bc->port, " --> * IND :\tprogress pid:%d\n",p->bc?p->bc->pid:-1);
2013                 misdn_lib_send_event( p->bc, EVENT_PROGRESS);
2014                 break;
2015         case AST_CONTROL_PROCEEDING:
2016                 chan_misdn_log(1, p->bc->port, " --> * IND :\tproceeding pid:%d\n",p->bc?p->bc->pid:-1);
2017                 misdn_lib_send_event( p->bc, EVENT_PROCEEDING);
2018                 break;
2019         case AST_CONTROL_CONGESTION:
2020                 chan_misdn_log(1, p->bc->port, " --> * IND :\tcongestion pid:%d\n",p->bc?p->bc->pid:-1);
2021
2022                 p->bc->out_cause=42;
2023                 if (p->state != MISDN_CONNECTED) {
2024                         start_bc_tones(p);
2025                         misdn_lib_send_event( p->bc, EVENT_RELEASE);
2026                 } else {
2027                         misdn_lib_send_event( p->bc, EVENT_DISCONNECT);
2028                 }
2029
2030                 if (p->bc->nt) {
2031                         hanguptone_indicate(p);
2032                 }
2033                 break;
2034         case -1 :
2035                 chan_misdn_log(1, p->bc->port, " --> * IND :\t-1! (stop indication) pid:%d\n",p->bc?p->bc->pid:-1);
2036                 
2037                 stop_indicate(p);
2038
2039                 if (p->state == MISDN_CONNECTED) 
2040                         start_bc_tones(p);
2041
2042                 break;
2043
2044         case AST_CONTROL_HOLD:
2045                 chan_misdn_log(1, p->bc->port, " --> *\tHOLD pid:%d\n",p->bc?p->bc->pid:-1);
2046                 break;
2047         case AST_CONTROL_UNHOLD:
2048                 chan_misdn_log(1, p->bc->port, " --> *\tUNHOLD pid:%d\n",p->bc?p->bc->pid:-1);
2049                 break;
2050         default:
2051                 ast_log(LOG_NOTICE, " --> * Unknown Indication:%d pid:%d\n",cond,p->bc?p->bc->pid:-1);
2052         }
2053   
2054         return 0;
2055 }
2056
2057 static int misdn_hangup(struct ast_channel *ast)
2058 {
2059         struct chan_list *p;
2060         struct misdn_bchannel *bc=NULL;
2061         
2062         if (!ast || ! (p=MISDN_ASTERISK_TECH_PVT(ast) ) ) return -1;
2063         
2064         release_lock;
2065
2066         
2067         ast_log(LOG_DEBUG, "misdn_hangup(%s)\n", ast->name);
2068         
2069         if (!p) {
2070                 chan_misdn_log(3, 0, "misdn_hangup called, without chan_list obj.\n");
2071                 release_unlock;
2072                 return 0 ;
2073         }
2074         
2075         bc=p->bc;
2076
2077         if (!bc) {
2078                 release_unlock;
2079                 ast_log(LOG_WARNING,"Hangup with private but no bc ?\n");
2080                 return 0;
2081         }
2082
2083         
2084         MISDN_ASTERISK_TECH_PVT(ast)=NULL;
2085         p->ast=NULL;
2086
2087         bc=p->bc;
2088         
2089         if (ast->_state == AST_STATE_RESERVED) {
2090                 /* between request and call */
2091                 MISDN_ASTERISK_TECH_PVT(ast)=NULL;
2092                 release_unlock;
2093                 
2094                 cl_dequeue_chan(&cl_te, p);
2095                 free(p);
2096                 
2097                 if (bc)
2098                         misdn_lib_release(bc);
2099                 
2100                 return 0;
2101         }
2102
2103         if (!p->bc->nt) 
2104                 stop_bc_tones(p);
2105
2106         
2107         release_unlock;
2108         
2109         {
2110                 const char *varcause=NULL;
2111                 bc->out_cause=ast->hangupcause?ast->hangupcause:16;
2112                 
2113                 if ( (varcause=pbx_builtin_getvar_helper(ast, "HANGUPCAUSE")) ||
2114                      (varcause=pbx_builtin_getvar_helper(ast, "PRI_CAUSE"))) {
2115                         int tmpcause=atoi(varcause);
2116                         bc->out_cause=tmpcause?tmpcause:16;
2117                 }
2118     
2119                 chan_misdn_log(1, bc->port, "* IND : HANGUP\tpid:%d ctx:%s dad:%s oad:%s State:%s\n",p->bc?p->bc->pid:-1, ast->context, ast->exten, AST_CID_P(ast), misdn_get_ch_state(p));
2120                 chan_misdn_log(2, bc->port, " --> l3id:%x\n",p->l3id);
2121                 chan_misdn_log(1, bc->port, " --> cause:%d\n",bc->cause);
2122                 chan_misdn_log(1, bc->port, " --> out_cause:%d\n",bc->out_cause);
2123                 chan_misdn_log(1, bc->port, " --> state:%s\n", misdn_get_ch_state(p));
2124                 
2125                 switch (p->state) {
2126                 case MISDN_CALLING:
2127                         p->state=MISDN_CLEANING;
2128                         misdn_lib_send_event( bc, EVENT_RELEASE_COMPLETE);
2129                         break;
2130                 case MISDN_HOLDED:
2131                 case MISDN_DIALING:
2132                         start_bc_tones(p);
2133                         hanguptone_indicate(p);
2134                 
2135                         if (bc->nt) {
2136                                 misdn_lib_send_event( bc, EVENT_DISCONNECT);
2137                         } else {
2138                                 misdn_lib_send_event( bc, EVENT_RELEASE_COMPLETE);
2139                                 p->state=MISDN_CLEANING;
2140                         }
2141                         break;
2142
2143                 case MISDN_CALLING_ACKNOWLEDGE:
2144                         start_bc_tones(p);
2145                         hanguptone_indicate(p);
2146                 
2147                         misdn_lib_send_event( bc, EVENT_DISCONNECT);
2148                         break;
2149       
2150                 case MISDN_ALERTING:
2151                 case MISDN_PROGRESS:
2152                 case MISDN_PROCEEDING:
2153                         if (p->orginator != ORG_AST) 
2154                                 hanguptone_indicate(p);
2155       
2156                         /*p->state=MISDN_CLEANING;*/
2157                         misdn_lib_send_event( bc, EVENT_DISCONNECT);
2158                         break;
2159                 case MISDN_CONNECTED:
2160                         /*  Alerting or Disconect */
2161                         if (p->bc->nt) {
2162                                 start_bc_tones(p);
2163                                 hanguptone_indicate(p);
2164                                 p->bc->progress_indicator=8;
2165                         }
2166                         misdn_lib_send_event( bc, EVENT_DISCONNECT);
2167
2168                         /*p->state=MISDN_CLEANING;*/
2169                         break;
2170                 case MISDN_DISCONNECTED:
2171                         misdn_lib_send_event( bc, EVENT_RELEASE);
2172                         p->state=MISDN_CLEANING; /* MISDN_HUNGUP_FROM_AST; */
2173                         break;
2174
2175                 case MISDN_RELEASED:
2176                 case MISDN_CLEANING:
2177                         p->state=MISDN_CLEANING;
2178                         break;
2179
2180                 case MISDN_BUSY:
2181                         break;
2182       
2183                 case MISDN_HOLD_DISCONNECT:
2184                         /* need to send release here */
2185                         chan_misdn_log(1, bc->port, " --> cause %d\n",bc->cause);
2186                         chan_misdn_log(1, bc->port, " --> out_cause %d\n",bc->out_cause);
2187                         
2188                         bc->out_cause=-1;
2189                         misdn_lib_send_event(bc,EVENT_RELEASE);
2190                         p->state=MISDN_CLEANING;
2191                         break;
2192                 default:
2193                         if (bc->nt) {
2194                                 bc->out_cause=-1;
2195                                 misdn_lib_send_event(bc, EVENT_RELEASE);
2196                                 p->state=MISDN_CLEANING; 
2197                         } else {
2198                                 misdn_lib_send_event(bc, EVENT_DISCONNECT);
2199                         }
2200                 }
2201
2202                 p->state=MISDN_CLEANING;
2203     
2204         }
2205         
2206
2207         chan_misdn_log(1, bc->port, "Channel: %s hanguped new state:%s\n",ast->name,misdn_get_ch_state(p));
2208         
2209         return 0;
2210 }
2211
2212
2213 struct ast_frame *process_ast_dsp(struct chan_list *tmp, struct ast_frame *frame)
2214 {
2215         struct ast_frame *f,*f2;
2216         if (tmp->trans)
2217                 f2=ast_translate(tmp->trans, frame,0);
2218         else {
2219                 chan_misdn_log(0, tmp->bc->port, "No T-Path found\n");
2220                 return NULL;
2221         }
2222         
2223         f = ast_dsp_process(tmp->ast, tmp->dsp, f2);
2224         if (f && (f->frametype == AST_FRAME_DTMF)) {
2225                 ast_log(LOG_DEBUG, "Detected inband DTMF digit: %c", f->subclass);
2226                 if (f->subclass == 'f' && tmp->faxdetect) {
2227                         /* Fax tone -- Handle and return NULL */
2228                         struct ast_channel *ast = tmp->ast;
2229                         if (!tmp->faxhandled) {
2230                                 tmp->faxhandled++;
2231                                 if (strcmp(ast->exten, "fax")) {
2232                                         if (ast_exists_extension(ast, ast_strlen_zero(ast->macrocontext)? ast->context : ast->macrocontext, "fax", 1, AST_CID_P(ast))) {
2233                                                 if (option_verbose > 2)
2234                                                         ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name);
2235                                                 /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
2236                                                 pbx_builtin_setvar_helper(ast,"FAXEXTEN",ast->exten);
2237                                                 if (ast_async_goto(ast, ast->context, "fax", 1))
2238                                                         ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, ast->context);
2239                                         } else
2240                                                 ast_log(LOG_NOTICE, "Fax detected, but no fax extension ctx:%s exten:%s\n",ast->context, ast->exten);
2241                                 } else
2242                                         ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
2243                         } else
2244                                 ast_log(LOG_DEBUG, "Fax already handled\n");
2245                         
2246                 }  else if ( tmp->ast_dsp) {
2247                         chan_misdn_log(2, tmp->bc->port, " --> * SEND: DTMF (AST_DSP) :%c\n",f->subclass);
2248                         return f;
2249                 }
2250         }
2251
2252         frame->frametype = AST_FRAME_NULL;
2253         frame->subclass = 0;
2254         return frame;
2255 }
2256
2257
2258 static struct ast_frame  *misdn_read(struct ast_channel *ast)
2259 {
2260         struct chan_list *tmp;
2261         int len;
2262         
2263         if (!ast) {
2264                 chan_misdn_log(1,0,"misdn_read called without ast\n");
2265                 return NULL;
2266         }
2267         if (! (tmp=MISDN_ASTERISK_TECH_PVT(ast)) ) {
2268                 chan_misdn_log(1,0,"misdn_read called without ast->pvt\n");
2269                 return NULL;
2270         }
2271         if (!tmp->bc) {
2272                 chan_misdn_log(1,0,"misdn_read called without bc\n");
2273                 return NULL;
2274         }
2275
2276         len=read(tmp->pipe[0],tmp->ast_rd_buf,sizeof(tmp->ast_rd_buf));
2277
2278         if (len<=0) {
2279                 /* we hangup here, since our pipe is closed */
2280                 chan_misdn_log(2,tmp->bc->port,"misdn_read: Pipe closed, hanging up\n");
2281                 return NULL;
2282         }
2283
2284         tmp->frame.frametype  = AST_FRAME_VOICE;
2285         tmp->frame.subclass = AST_FORMAT_ALAW;
2286         tmp->frame.datalen = len;
2287         tmp->frame.samples = len ;
2288         tmp->frame.mallocd =0 ;
2289         tmp->frame.offset= 0 ;
2290         tmp->frame.src = NULL;
2291         tmp->frame.data = tmp->ast_rd_buf ;
2292         
2293         if (tmp->faxdetect || tmp->ast_dsp ) {
2294                 return process_ast_dsp(tmp, &tmp->frame);
2295         }
2296         
2297         return &tmp->frame;
2298 }
2299
2300
2301 static int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
2302 {
2303         struct chan_list *ch;
2304         int i  = 0;
2305         
2306         if (!ast || ! (ch=MISDN_ASTERISK_TECH_PVT(ast)) ) return -1;
2307         
2308         if (!ch->bc ) {
2309                 ast_log(LOG_WARNING, "private but no bc\n");
2310                 return -1;
2311         }
2312         
2313         if (ch->holded ) {
2314                 chan_misdn_log(5, ch->bc->port, "misdn_write: Returning because holded\n");
2315                 return 0;
2316         }
2317         
2318         if (ch->notxtone) {
2319                 chan_misdn_log(5, ch->bc->port, "misdn_write: Returning because notxone\n");
2320                 return 0;
2321         }
2322
2323
2324         if ( !frame->subclass) {
2325                 chan_misdn_log(4, ch->bc->port, "misdn_write: * prods us\n");
2326                 return 0;
2327         }
2328         
2329         if ( !(frame->subclass & prefformat)) {
2330                 
2331                 chan_misdn_log(-1, ch->bc->port, "Got Unsupported Frame with Format:%d\n", frame->subclass);
2332                 return 0;
2333         }
2334         
2335
2336         if ( !frame->samples ) {
2337                 chan_misdn_log(4, ch->bc->port, "misdn_write: zero write\n");
2338                 return 0;
2339         }
2340
2341         if ( ! ch->bc->addr ) {
2342                 chan_misdn_log(8, ch->bc->port, "misdn_write: no addr for bc dropping:%d\n", frame->samples);
2343                 return 0;
2344         }
2345         
2346 #if MISDN_DEBUG
2347         {
2348                 int i, max=5>frame->samples?frame->samples:5;
2349                 
2350                 printf("write2mISDN %p %d bytes: ", p, frame->samples);
2351                 
2352                 for (i=0; i<  max ; i++) printf("%2.2x ",((char*) frame->data)[i]);
2353                 printf ("\n");
2354         }
2355 #endif
2356
2357
2358         switch (ch->bc->bc_state) {
2359                 case BCHAN_ACTIVATED:
2360                 case BCHAN_BRIDGED:
2361                         break;
2362                 default:
2363                 if (!ch->dropped_frame_cnt)
2364                         chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) droping: %d frames addr:%x exten:%s cid:%s ch->state:%s bc_state:%d\n",frame->samples,ch->bc->addr, ast->exten, ast->cid.cid_num,misdn_get_ch_state( ch), ch->bc->bc_state);
2365                 
2366                 ch->dropped_frame_cnt++;
2367                 if (ch->dropped_frame_cnt > 100) {
2368                         ch->dropped_frame_cnt=0;
2369                         chan_misdn_log(5, ch->bc->port, "BC not active (nor bridged) droping: %d frames addr:%x  dropped > 100 frames!\n",frame->samples,ch->bc->addr);
2370
2371                 }
2372
2373                 return 0;
2374         }
2375
2376         chan_misdn_log(9, ch->bc->port, "Sending :%d bytes 2 MISDN\n",frame->samples);
2377         
2378         if ( !ch->bc->nojitter && misdn_cap_is_speech(ch->bc->capability) ) {
2379                 /* Buffered Transmit (triggert by read from isdn side)*/
2380                 if (misdn_jb_fill(ch->jb,frame->data,frame->samples) < 0) {
2381                         if (ch->bc->active)
2382                                 cb_log(0,ch->bc->port,"Misdn Jitterbuffer Overflow.\n");
2383                 }
2384                 
2385         } else {
2386                 /*transmit without jitterbuffer*/
2387                 i=misdn_lib_tx2misdn_frm(ch->bc, frame->data, frame->samples);
2388         }
2389
2390         
2391         
2392         return 0;
2393 }
2394
2395
2396
2397
2398 static enum ast_bridge_result  misdn_bridge (struct ast_channel *c0,
2399                                       struct ast_channel *c1, int flags,
2400                                       struct ast_frame **fo,
2401                                       struct ast_channel **rc,
2402                                       int timeoutms)
2403
2404 {
2405         struct chan_list *ch1,*ch2;
2406         struct ast_channel *carr[2], *who;
2407         int to=-1;
2408         struct ast_frame *f;
2409   
2410         ch1=get_chan_by_ast(c0);
2411         ch2=get_chan_by_ast(c1);
2412
2413         carr[0]=c0;
2414         carr[1]=c1;
2415   
2416   
2417         if (ch1 && ch2 ) ;
2418         else
2419                 return -1;
2420   
2421
2422         int bridging;
2423         misdn_cfg_get( 0, MISDN_GEN_BRIDGING, &bridging, sizeof(int));
2424         if (bridging) {
2425                 int ecwb, ec;
2426                 misdn_cfg_get( ch1->bc->port, MISDN_CFG_ECHOCANCELWHENBRIDGED, &ecwb, sizeof(int));
2427                 misdn_cfg_get( ch1->bc->port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int));
2428                 if ( !ecwb && ec ) {
2429                         chan_misdn_log(2, ch1->bc->port, "Disabling Echo Cancellor when Bridged\n");
2430                         ch1->bc->ec_enable=0;
2431                         manager_ec_disable(ch1->bc);
2432                 }
2433                 misdn_cfg_get( ch2->bc->port, MISDN_CFG_ECHOCANCELWHENBRIDGED, &ecwb, sizeof(int));
2434                 misdn_cfg_get( ch2->bc->port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int));
2435                 if ( !ecwb && ec) {
2436                         chan_misdn_log(2, ch2->bc->port, "Disabling Echo Cancellor when Bridged\n");
2437                         ch2->bc->ec_enable=0;
2438                         manager_ec_disable(ch2->bc); 
2439                 }
2440                 
2441                 /* trying to make a mISDN_dsp conference */
2442                 chan_misdn_log(1, ch1->bc->port, "I SEND: Making conference with Number:%d\n", ch1->bc->pid +1);
2443
2444                 misdn_lib_bridge(ch1->bc,ch2->bc);
2445         }
2446         
2447         if (option_verbose > 2) 
2448                 ast_verbose(VERBOSE_PREFIX_3 "Native bridging %s and %s\n", c0->name, c1->name);
2449
2450         chan_misdn_log(1, ch1->bc->port, "* Making Native Bridge between %s and %s\n", ch1->bc->oad, ch2->bc->oad);
2451  
2452         if (! (flags&AST_BRIDGE_DTMF_CHANNEL_0) )
2453                 ch1->ignore_dtmf=1;
2454
2455         if (! (flags&AST_BRIDGE_DTMF_CHANNEL_1) )
2456                 ch2->ignore_dtmf=1;
2457
2458         while(1) {
2459                 to=-1;
2460                 who = ast_waitfor_n(carr, 2, &to);
2461
2462                 if (!who) {
2463                         ast_log(LOG_NOTICE,"misdn_bridge: empty read, breaking out\n");
2464                         break;
2465                 }
2466                 f = ast_read(who);
2467     
2468                 if (!f || f->frametype == AST_FRAME_CONTROL) {
2469                         /* got hangup .. */
2470
2471                         if (!f) 
2472                                 chan_misdn_log(1,ch1->bc->port,"Read Null Frame\n");
2473                         else
2474                                 chan_misdn_log(1,ch1->bc->port,"Read Frame Controll class:%d\n",f->subclass);
2475                         
2476                         *fo=f;
2477                         *rc=who;
2478       
2479                         break;
2480                 }
2481                 
2482                 if ( f->frametype == AST_FRAME_DTMF ) {
2483                         chan_misdn_log(1,0,"Read DTMF %d from %s\n",f->subclass, who->exten);
2484
2485                         *fo=f;
2486                         *rc=who;
2487                         break;
2488                 }
2489                 
2490                 
2491                 if (who == c0) {
2492                         ast_write(c1,f);
2493                 }
2494                 else {
2495                         ast_write(c0,f);
2496                 }
2497     
2498         }
2499         
2500         chan_misdn_log(1, ch1->bc->port, "I SEND: Splitting conference with Number:%d\n", ch1->bc->pid +1);
2501         
2502         misdn_lib_split_bridge(ch1->bc,ch2->bc);
2503         
2504         
2505         return AST_BRIDGE_COMPLETE;
2506 }
2507
2508 /** AST INDICATIONS END **/
2509
2510 static int dialtone_indicate(struct chan_list *cl)
2511 {
2512         const struct tone_zone_sound *ts= NULL;
2513         struct ast_channel *ast=cl->ast;
2514         chan_misdn_log(3,cl->bc->port," --> Dial\n");
2515         ts=ast_get_indication_tone(ast->zone,"dial");
2516         cl->ts=ts;      
2517         
2518         if (ts) {
2519                 cl->notxtone=0;
2520                 cl->norxtone=0;
2521                 ast_playtones_start(ast,0, ts->data, 0);
2522                 chan_misdn_log(4,cl->bc->port,"Starting Playtones\n");
2523                 misdn_lib_tone_generator_start(cl->bc);
2524         }
2525
2526         return 0;
2527 }
2528
2529 static int hanguptone_indicate(struct chan_list *cl)
2530 {
2531         misdn_lib_send_tone(cl->bc,TONE_HANGUP);
2532         misdn_lib_tone_generator_start(cl->bc);
2533         return 0;
2534 }
2535
2536 static int stop_indicate(struct chan_list *cl)
2537 {
2538         struct ast_channel *ast=cl->ast;
2539         chan_misdn_log(3,cl->bc->port," --> None\n");
2540         misdn_lib_tone_generator_stop(cl->bc);
2541         ast_playtones_stop(ast);
2542         /*ast_deactivate_generator(ast);*/
2543         
2544         return 0;
2545 }
2546
2547
2548 static int start_bc_tones(struct chan_list* cl)
2549 {
2550         misdn_lib_tone_generator_stop(cl->bc);
2551         cl->notxtone=0;
2552         cl->norxtone=0;
2553         return 0;
2554 }
2555
2556 static int stop_bc_tones(struct chan_list *cl)
2557 {
2558         if (!cl) return -1;
2559
2560         cl->notxtone=1;
2561         cl->norxtone=1;
2562         
2563         return 0;
2564 }
2565
2566
2567 static struct chan_list *init_chan_list(int orig)
2568 {
2569         struct chan_list *cl=malloc(sizeof(struct chan_list));
2570         
2571         if (!cl) {
2572                 chan_misdn_log(-1, 0, "misdn_request: malloc failed!");
2573                 return NULL;
2574         }
2575         
2576         memset(cl,0,sizeof(struct chan_list));
2577
2578         cl->orginator=orig;
2579         cl->need_queue_hangup=1;
2580         cl->need_hangup=1;
2581         
2582         return cl;
2583         
2584 }
2585
2586 static struct ast_channel *misdn_request(const char *type, int format, void *data, int *cause)
2587
2588 {
2589         struct ast_channel *tmp = NULL;
2590         char group[BUFFERSIZE+1]="";
2591         char buf[128];
2592         char buf2[128], *ext=NULL, *port_str;
2593         char *tokb=NULL, *p=NULL;
2594         int channel=0, port=0;
2595         struct misdn_bchannel *newbc = NULL;
2596         
2597         struct chan_list *cl=init_chan_list(ORG_AST);
2598         
2599         sprintf(buf,"%s/%s",misdn_type,(char*)data);
2600         ast_copy_string(buf2,data, 128);
2601         
2602         port_str=strtok_r(buf2,"/", &tokb);
2603
2604         ext=strtok_r(NULL,"/", &tokb);
2605
2606         if (port_str) {
2607                 if (port_str[0]=='g' && port_str[1]==':' ) {
2608                         /* We make a group call lets checkout which ports are in my group */
2609                         port_str += 2;
2610                         strncpy(group, port_str, BUFFERSIZE);
2611                         group[127] = 0;
2612                         chan_misdn_log(2, 0, " --> Group Call group: %s\n",group);
2613                 } 
2614                 else if ((p = strchr(port_str, ':'))) {
2615                         /* we have a preselected channel */
2616                         *p = 0;
2617                         channel = atoi(++p);
2618                         port = atoi(port_str);
2619                         chan_misdn_log(2, port, " --> Call on preselected Channel (%d).\n", channel);
2620                 }
2621                 else {
2622                         port = atoi(port_str);
2623                 }
2624                 
2625                 
2626         } else {
2627                 ast_log(LOG_WARNING, " --> ! IND : CALL dad:%s WITHOUT PORT/Group, check extension.conf\n",ext);
2628                 return NULL;
2629         }
2630
2631         if (!ast_strlen_zero(group)) {
2632         
2633                 char cfg_group[BUFFERSIZE+1];
2634                 struct robin_list *rr = NULL;
2635
2636                 if (misdn_cfg_is_group_method(group, METHOD_ROUND_ROBIN)) {
2637                         chan_misdn_log(4, port, " --> STARTING ROUND ROBIN...");
2638                         rr = get_robin_position(group);
2639                 }
2640                 
2641                 if (rr) {
2642                         int robin_channel = rr->channel;
2643                         int port_start;
2644                         int next_chan = 1;
2645
2646                         do {
2647                                 port_start = 0;
2648                                 for (port = misdn_cfg_get_next_port_spin(rr->port); port > 0 && port != port_start;
2649                                          port = misdn_cfg_get_next_port_spin(port)) {
2650
2651                                         if (!port_start)
2652                                                 port_start = port;
2653
2654                                         if (port >= port_start)
2655                                                 next_chan = 1;
2656                                         
2657                                         if (port <= port_start && next_chan) {
2658                                                 int maxbchans=misdn_lib_get_maxchans(port);
2659                                                 if (++robin_channel >= maxbchans) {
2660                                                         robin_channel = 1;
2661                                                 }
2662                                                 next_chan = 0;
2663                                         }
2664
2665                                         misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE);
2666                                         
2667                                         if (!strcasecmp(cfg_group, group)) {
2668                                                 int port_up;
2669                                                 int check,checkl2;
2670                                                 misdn_cfg_get(port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(int));
2671                                                 misdn_cfg_get(port, MISDN_CFG_PP_L2_CHECK, &checkl2, sizeof(int));
2672                                                 check=checkl2?2:check;
2673
2674                                                 port_up = misdn_lib_port_up(port, check);
2675
2676                                                 if (check && !port_up) 
2677                                                         chan_misdn_log(1,port,"L1 is not Up on this Port\n");
2678                                                 
2679                                                 
2680                                                 if ( port_up )  {
2681                                                         newbc = misdn_lib_get_free_bc(port, robin_channel);
2682                                                         if (newbc) {
2683                                                                 chan_misdn_log(4, port, " Success! Found port:%d channel:%d\n", newbc->port, newbc->channel);
2684                                                                 if (port_up)
2685                                                                         chan_misdn_log(4, port, "portup:%d\n",  port_up);
2686                                                                 rr->port = newbc->port;
2687                                                                 rr->channel = newbc->channel;
2688                                                                 break;
2689                                                         }
2690                                                 }
2691                                         }
2692                                 }
2693                         } while (!newbc && robin_channel != rr->channel);
2694                         
2695                         if (!newbc)
2696                                 chan_misdn_log(4, port, " Failed! No free channel in group %d!", group);
2697                 }
2698                 
2699                 else {          
2700                         for (port=misdn_cfg_get_next_port(0); port > 0;
2701                                  port=misdn_cfg_get_next_port(port)) {
2702                                 
2703                                 misdn_cfg_get( port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE);
2704
2705                                 chan_misdn_log(3,port, "Group [%s] Port [%d]\n", group, port);
2706                                 if (!strcasecmp(cfg_group, group)) {
2707                                         int port_up;
2708                                         int check,checkl2;
2709                                         misdn_cfg_get(port, MISDN_CFG_PMP_L1_CHECK, &check, sizeof(int));
2710                                         misdn_cfg_get(port, MISDN_CFG_PP_L2_CHECK, &checkl2, sizeof(int));
2711                                         check=checkl2?2:check;
2712                                         port_up = misdn_lib_port_up(port, check);
2713                                         
2714                                         chan_misdn_log(4, port, "portup:%d\n", port_up);
2715                                         
2716                                         if ( port_up )  {
2717                                                 newbc = misdn_lib_get_free_bc(port, 0);
2718                                                 if (newbc)
2719                                                         break;
2720                                         }
2721                                 }
2722                         }
2723                 }
2724                 
2725         } else {
2726                 if (channel)
2727                         chan_misdn_log(1, port," --> preselected_channel: %d\n",channel);
2728                 newbc = misdn_lib_get_free_bc(port, channel);
2729         }
2730         
2731         if (!newbc) {
2732                 chan_misdn_log(-1, 0, "Could not create channel on port:%d with extensions:%s\n",port,ext);
2733                 return NULL;
2734         }
2735
2736         /* create ast_channel and link all the objects together */
2737         cl->bc=newbc;
2738         
2739         tmp = misdn_new(cl, AST_STATE_RESERVED, ext, NULL, format, port, channel);
2740         cl->ast=tmp;
2741         
2742         /* register chan in local list */
2743         cl_queue_chan(&cl_te, cl) ;
2744         
2745         /* fill in the config into the objects */
2746         read_config(cl, ORG_AST);
2747
2748         /* important */
2749         cl->need_hangup=0;
2750         
2751         return tmp;
2752 }
2753
2754
2755 static int misdn_send_text (struct ast_channel *chan, const char *text)
2756 {
2757         struct chan_list *tmp=chan->tech_pvt;
2758         
2759         if (tmp && tmp->bc) {
2760                 ast_copy_string(tmp->bc->display,text,sizeof(tmp->bc->display));
2761                 misdn_lib_send_event(tmp->bc, EVENT_INFORMATION);
2762         } else {
2763                 ast_log(LOG_WARNING, "No chan_list but send_text request?\n");
2764                 return -1;
2765         }
2766         
2767         return 0;
2768 }
2769
2770 static struct ast_channel_tech misdn_tech = {
2771         .type="mISDN",
2772         .description="Channel driver for mISDN Support (Bri/Pri)",
2773         .capabilities= AST_FORMAT_ALAW ,
2774         .requester=misdn_request,
2775         .send_digit=misdn_digit,
2776         .call=misdn_call,
2777         .bridge=misdn_bridge, 
2778         .hangup=misdn_hangup,
2779         .answer=misdn_answer,
2780         .read=misdn_read,
2781         .write=misdn_write,
2782         .indicate=misdn_indication,
2783         .fixup=misdn_fixup,
2784         .send_text=misdn_send_text,
2785         .properties=0
2786 };
2787
2788 static struct ast_channel_tech misdn_tech_wo_bridge = {
2789         .type="mISDN",
2790         .description="Channel driver for mISDN Support (Bri/Pri)",
2791         .capabilities=AST_FORMAT_ALAW ,
2792         .requester=misdn_request,
2793         .send_digit=misdn_digit,
2794         .call=misdn_call,
2795         .hangup=misdn_hangup,
2796         .answer=misdn_answer,
2797         .read=misdn_read,
2798         .write=misdn_write,
2799         .indicate=misdn_indication,
2800         .fixup=misdn_fixup,
2801         .send_text=misdn_send_text,
2802         .properties=0
2803 };
2804
2805
2806 static unsigned long glob_channel=0;
2807
2808 static void update_name(struct ast_channel *tmp, int port, int c) 
2809 {
2810         if (c<=0) {
2811                         c=glob_channel++;
2812                         ast_string_field_build(tmp, name, "%s/%d-u%d",
2813                                  misdn_type, port, c);
2814         } else {
2815                          ast_string_field_build(tmp, name, "%s/%d-%d",
2816                                  misdn_type, port, c);
2817         }
2818
2819         chan_misdn_log(3,port," --> updating channel name to [%s]\n",tmp->name);
2820         
2821 }
2822
2823 static struct ast_channel *misdn_new(struct chan_list *chlist, int state,  char *exten, char *callerid, int format, int port, int c)
2824 {
2825         struct ast_channel *tmp;
2826         
2827         tmp = ast_channel_alloc(1);
2828         
2829         if (tmp) {
2830                 chan_misdn_log(2, 0, " --> * NEW CHANNEL dad:%s oad:%s\n",exten,callerid);
2831                 
2832                 update_name(tmp,port,c);
2833         
2834                 tmp->nativeformats = prefformat;
2835
2836                 tmp->readformat = format;
2837                 tmp->rawreadformat = format;
2838                 tmp->writeformat = format;
2839                 tmp->rawwriteformat = format;
2840     
2841                 tmp->tech_pvt = chlist;
2842                 
2843                 int bridging;
2844                 misdn_cfg_get( 0, MISDN_GEN_BRIDGING, &bridging, sizeof(int));
2845                 if (bridging)
2846                         tmp->tech = &misdn_tech;
2847                 else
2848                         tmp->tech = &misdn_tech_wo_bridge;
2849                 
2850                 tmp->writeformat = format;
2851                 tmp->readformat = format;
2852                 tmp->priority=1;
2853                 
2854                 if (exten) 
2855                         ast_copy_string(tmp->exten, exten,  sizeof(tmp->exten));
2856                 else
2857                         chan_misdn_log(1,0,"misdn_new: no exten given.\n");
2858                 
2859                 if (callerid) {
2860                         char *cid_name, *cid_num;
2861       
2862                         ast_callerid_parse(callerid, &cid_name, &cid_num);
2863                         ast_set_callerid(tmp, cid_num,cid_name,cid_num);
2864                 } else {
2865                         ast_set_callerid(tmp, NULL,NULL,NULL);
2866                 }
2867
2868                 {
2869                         if (pipe(chlist->pipe)<0)
2870                                 perror("Pipe failed\n");
2871                         
2872                         tmp->fds[0]=chlist->pipe[0];
2873                         
2874                 }
2875                 
2876                 ast_setstate(tmp, state);
2877                 if (state == AST_STATE_RING)
2878                         tmp->rings = 1;
2879                 else
2880                         tmp->rings = 0;
2881                 
2882                 
2883         } else {
2884                 chan_misdn_log(-1,0,"Unable to allocate channel structure\n");
2885         }
2886         
2887         return tmp;
2888 }
2889
2890 static struct chan_list *find_chan_by_l3id(struct chan_list *list, unsigned long l3id)
2891 {
2892         struct chan_list *help=list;
2893         for (;help; help=help->next) {
2894                 if (help->l3id == l3id ) return help;
2895         }
2896   
2897         chan_misdn_log(6, list? (list->bc? list->bc->port : 0) : 0, "$$$ find_chan: No channel found with l3id:%x\n",l3id);
2898   
2899         return NULL;
2900 }
2901
2902 static struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc)
2903 {
2904         struct chan_list *help=list;
2905         for (;help; help=help->next) {
2906                 if (help->bc == bc) return help;
2907         }
2908   
2909         chan_misdn_log(6, bc->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n",bc->oad,bc->dad);
2910   
2911         return NULL;
2912 }
2913
2914 static struct chan_list *find_chan_by_pid(struct chan_list *list, int pid)
2915 {
2916         struct chan_list *help=list;
2917         for (;help; help=help->next) {
2918                 if (help->bc->pid == pid) return help;
2919         }
2920   
2921         chan_misdn_log(6, 0, "$$$ find_chan: No channel found for pid:%d\n",pid);
2922   
2923         return NULL;
2924 }
2925
2926 static struct chan_list *find_holded(struct chan_list *list, struct misdn_bchannel *bc)
2927 {
2928         struct chan_list *help=list;
2929         
2930         chan_misdn_log(6, bc->port, "$$$ find_holded: channel:%d oad:%s dad:%s\n",bc->channel, bc->oad,bc->dad);
2931         for (;help; help=help->next) {
2932                 chan_misdn_log(4, bc->port, "$$$ find_holded: --> holded:%d channel:%d\n",help->bc->holded, help->bc->channel);
2933                 if (help->bc->port == bc->port
2934                     && help->bc->holded ) return help;
2935         }
2936         
2937         chan_misdn_log(6, bc->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n",bc->oad,bc->dad);
2938   
2939         return NULL;
2940 }
2941
2942 static void cl_queue_chan(struct chan_list **list, struct chan_list *chan)
2943 {
2944         chan_misdn_log(4, chan->bc? chan->bc->port : 0, "* Queuing chan %p\n",chan);
2945   
2946         ast_mutex_lock(&cl_te_lock);
2947         if (!*list) {
2948                 *list = chan;
2949         } else {
2950                 struct chan_list *help=*list;
2951                 for (;help->next; help=help->next); 
2952                 help->next=chan;
2953         }
2954         chan->next=NULL;
2955         ast_mutex_unlock(&cl_te_lock);
2956 }
2957
2958 static void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan) 
2959 {
2960         if (chan->dsp) 
2961                 ast_dsp_free(chan->dsp);
2962         if (chan->trans)
2963                 ast_translator_free_path(chan->trans);
2964
2965         
2966
2967         ast_mutex_lock(&cl_te_lock);
2968         if (!*list) {
2969                 ast_mutex_unlock(&cl_te_lock);
2970                 return;
2971         }
2972   
2973         if (*list == chan) {
2974                 *list=(*list)->next;
2975                 ast_mutex_unlock(&cl_te_lock);
2976                 return ;
2977         }
2978   
2979         {
2980                 struct chan_list *help=*list;
2981                 for (;help->next; help=help->next) {
2982                         if (help->next == chan) {
2983                                 help->next=help->next->next;
2984                                 ast_mutex_unlock(&cl_te_lock);
2985                                 return;
2986                         }
2987                 }
2988         }
2989         
2990         ast_mutex_unlock(&cl_te_lock);
2991 }
2992
2993 /** Channel Queue End **/
2994
2995
2996 int pbx_start_chan(struct chan_list *ch)
2997 {
2998         int ret=ast_pbx_start(ch->ast); 
2999
3000         if (ret>=0) 
3001                 ch->need_hangup=0;
3002         else
3003                 ch->need_hangup=1;
3004
3005         return ret;
3006 }
3007
3008 static void hangup_chan(struct chan_list *ch)
3009 {
3010         if (!ch) {
3011                 cb_log(1,0,"Cannot hangup chan, no ch\n");
3012                 return;
3013         }
3014
3015
3016         if (ch->need_hangup) 
3017         {
3018                 send_cause2ast(ch->ast,ch->bc,ch);
3019                 ch->need_hangup=0;
3020                 ch->need_queue_hangup=0;
3021                 if (ch->ast)
3022                         ast_hangup(ch->ast);
3023                 return;
3024         }
3025
3026         if (!ch->need_queue_hangup) {
3027                 cb_log(1,ch->bc->port,"No need to queue hangup\n");
3028         }
3029
3030         ch->need_queue_hangup=0;
3031         if (ch->ast) {
3032                 send_cause2ast(ch->ast,ch->bc,ch);
3033                 ast_queue_hangup(ch->ast);
3034         } else {
3035                 cb_log(1,ch->bc->port,"Cannot hangup chan, no ast\n");
3036         }
3037 }
3038
3039 /** Isdn asks us to release channel, pendant to misdn_hangup **/
3040 static void release_chan(struct misdn_bchannel *bc) {
3041         struct ast_channel *ast=NULL;
3042         {
3043                 struct chan_list *ch=find_chan_by_bc(cl_te, bc);
3044                 if (!ch) ch=find_chan_by_l3id (cl_te, bc->l3_id);
3045                 if (!ch)  {
3046                         chan_misdn_log(0, bc->port, "release_chan: Ch not found!\n");
3047                         return;
3048                 }
3049                 
3050                 release_lock;
3051                 if (ch->ast) {
3052                         ast=ch->ast;
3053                 } 
3054                 release_unlock;
3055                 
3056                 chan_misdn_log(1, bc->port, "release_chan: bc with l3id: %x\n",bc->l3_id);
3057                 
3058                 /*releaseing jitterbuffer*/
3059                 if (ch->jb ) {
3060                         misdn_jb_destroy(ch->jb);
3061                         ch->jb=NULL;
3062                 } else {
3063                         if (!bc->nojitter)
3064                                 chan_misdn_log(5,bc->port,"Jitterbuffer already destroyed.\n");
3065                 }
3066
3067
3068                 if (ch->orginator == ORG_AST) {
3069                         misdn_out_calls[bc->port]--;
3070                 } else {
3071                         misdn_in_calls[bc->port]--;
3072                 }
3073                 
3074                 if (ch) {
3075                         
3076                         close(ch->pipe[0]);
3077                         close(ch->pipe[1]);
3078
3079                         
3080                         if (ast && MISDN_ASTERISK_TECH_PVT(ast)) {
3081                                 chan_misdn_log(1, bc->port, "* RELEASING CHANNEL pid:%d ctx:%s dad:%s oad:%s state: %s\n",bc?bc->pid:-1, ast->context, ast->exten,AST_CID_P(ast),misdn_get_ch_state(ch));
3082                                 chan_misdn_log(3, bc->port, " --> * State Down\n");
3083                                 MISDN_ASTERISK_TECH_PVT(ast)=NULL;
3084                                 
3085       
3086                                 if (ast->_state != AST_STATE_RESERVED) {
3087                                         chan_misdn_log(3, bc->port, " --> Setting AST State to down\n");
3088                                         ast_setstate(ast, AST_STATE_DOWN);
3089                                 }
3090                         }
3091                                 
3092                         ch->state=MISDN_CLEANING;
3093                         cl_dequeue_chan(&cl_te, ch);
3094                         
3095                         free(ch);
3096                 } else {
3097                         /* chan is already cleaned, so exiting  */
3098                 }
3099         }
3100 }
3101 /*** release end **/
3102
3103 static void misdn_transfer_bc(struct chan_list *tmp_ch, struct chan_list *holded_chan)
3104 {
3105         chan_misdn_log(4,0,"TRANSFERING %s to %s\n",holded_chan->ast->name, tmp_ch->ast->name);
3106         
3107         tmp_ch->state=MISDN_HOLD_DISCONNECT;
3108   
3109         ast_moh_stop(AST_BRIDGED_P(holded_chan->ast));
3110
3111         holded_chan->state=MISDN_CONNECTED;
3112         holded_chan->holded=0;
3113         misdn_lib_transfer(holded_chan->bc?holded_chan->bc:holded_chan->holded_bc);
3114         ast_channel_masquerade(holded_chan->ast, AST_BRIDGED_P(tmp_ch->ast));
3115 }
3116
3117
3118 static void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , struct ast_channel *ast)
3119 {
3120         char predial[256]="";
3121         char *p = predial;
3122   
3123         struct ast_frame fr;
3124   
3125         strncpy(predial, ast->exten, sizeof(predial) -1 );
3126   
3127         ch->state=MISDN_DIALING;
3128
3129         if (bc->nt) {
3130                 int ret; 
3131                 ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
3132         } else {
3133                 int ret;
3134                 if ( misdn_lib_is_ptp(bc->port)) {
3135                         ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
3136                 } else {
3137                         ret = misdn_lib_send_event(bc, EVENT_PROCEEDING );
3138                 }
3139         }
3140
3141         if ( !bc->nt && (ch->orginator==ORG_MISDN) && !ch->incoming_early_audio ) 
3142                 chan_misdn_log(1,bc->port, " --> incoming_early_audio off\n");
3143          else  
3144                 dialtone_indicate(ch);
3145   
3146         chan_misdn_log(1, bc->port, "* Starting Ast ctx:%s dad:%s oad:%s with 's' extension\n", ast->context, ast->exten, AST_CID_P(ast));
3147   
3148         strncpy(ast->exten,"s", 2);
3149   
3150         if (pbx_start_chan(ch)<0) {
3151                 ast=NULL;
3152                 hangup_chan(ch);
3153                 hanguptone_indicate(ch);
3154
3155                 if (bc->nt)
3156                         misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
3157                 else
3158                         misdn_lib_send_event(bc, EVENT_DISCONNECT );
3159         }
3160   
3161