* removed unnecessary struct elements and functions
[asterisk/asterisk.git] / channels / misdn / isdn_lib.c
1 /*
2  * Chan_Misdn -- Channel Driver for Asterisk
3  *
4  * Interface to mISDN
5  *
6  * Copyright (C) 2004, Christian Richter
7  *
8  * Christian Richter <crich@beronet.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include "isdn_lib_intern.h"
15 #include <mISDNuser/isdn_debug.h>
16
17 void misdn_free_ibuffer(void *ibuf)
18 {
19         free_ibuffer((ibuffer_t*)ibuf);
20 }
21
22
23 void misdn_clear_ibuffer(void *ibuf)
24 {
25         clear_ibuffer( (ibuffer_t*)ibuf);
26 }
27
28 void *misdn_init_ibuffer(int len)
29 {
30         return init_ibuffer(len);
31 }
32
33 int misdn_ibuf_freecount(void *buf)
34 {
35         return ibuf_freecount( (ibuffer_t*)buf);
36 }
37
38 int misdn_ibuf_usedcount(void *buf)
39 {
40         return ibuf_usedcount( (ibuffer_t*)buf);
41 }
42
43 void misdn_ibuf_memcpy_r(char *to, void *buf, int len)
44 {
45         ibuf_memcpy_r( to, (ibuffer_t*)buf, len);
46 }
47
48 void misdn_ibuf_memcpy_w(void *buf, char *from,  int len)
49 {
50         ibuf_memcpy_w((ibuffer_t*)buf, from, len);
51 }
52
53 struct misdn_stack* get_misdn_stack( void );
54
55
56 int misdn_lib_is_ptp(int port)
57 {
58         struct misdn_stack *stack=get_misdn_stack();
59         for ( ; stack; stack=stack->next) {
60                 if (stack->port == port) return stack->ptp;
61         }
62         return -1;
63 }
64
65
66 struct misdn_stack* get_stack_by_bc(struct misdn_bchannel *bc)
67 {
68         struct misdn_stack *stack=get_misdn_stack();
69
70         if (!bc) return NULL;
71         
72         for ( ; stack; stack=stack->next) {
73                 int i;
74                 for (i=0; i <stack->b_num; i++) {
75                         if ( bc->port == stack->port) return stack;
76                 }
77         }
78
79         return NULL;
80 }
81
82
83 void get_show_stack_details(int port, char *buf)
84 {
85         struct misdn_stack *stack=get_misdn_stack();
86         
87         for ( ; stack; stack=stack->next) {
88                 if (stack->port == port) break;
89         }
90         
91         if (stack) {
92                 sprintf(buf, "* Stack Addr:%x Port %d Type %s Prot. %s L2Link %s L1Link:%s", stack->upper_id, stack->port, stack->nt?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN", stack->l1link?"UP":"DOWN");
93                 
94         } else {
95                 buf[0]=0;
96         }
97         
98 }
99
100
101 static int nt_err_cnt =0 ;
102
103 enum global_states {
104         MISDN_INITIALIZING,
105         MISDN_INITIALIZED
106 } ;
107
108 static enum global_states  global_state=MISDN_INITIALIZING;
109
110
111 #include <mISDNuser/net_l2.h>
112 #include <mISDNuser/tone.h>
113 #include <unistd.h>
114 #include <semaphore.h>
115 #include <pthread.h>
116 #include <signal.h>
117
118 #include "isdn_lib.h"
119
120
121 struct misdn_lib {
122         int midev;
123         int midev_nt;
124
125         pthread_t event_thread;
126         pthread_t event_handler_thread;
127   
128         void *user_data;
129
130         msg_queue_t upqueue;
131         msg_queue_t activatequeue; 
132   
133         sem_t new_msg;
134   
135         struct misdn_stack *stack_list;
136 } ;
137
138 #ifndef ECHOCAN_ON
139 #define ECHOCAN_ON 123
140 #define ECHOCAN_OFF 124
141 #endif
142
143 #define MISDN_DEBUG 0
144
145 void misdn_tx_jitter(struct misdn_bchannel *bc, int len);
146
147 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id);
148
149 int setup_bc(struct misdn_bchannel *bc);
150
151 int manager_isdn_handler(iframe_t *frm ,msg_t *msg);
152
153 int misdn_lib_port_restart(int port);
154
155 extern struct isdn_msg msgs_g[]; 
156
157 #define ISDN_PID_L3_B_USER 0x430000ff
158 #define ISDN_PID_L4_B_USER 0x440000ff
159
160 /* #define MISDN_IBUF_SIZE 1024 */
161 #define MISDN_IBUF_SIZE 512
162
163 /*  Fine Tuning of Inband  Signalling time */
164 #define TONE_ALERT_CNT 41 /*  1 Sec  */
165 #define TONE_ALERT_SILENCE_CNT 200 /*  4 Sec */
166
167 #define TONE_BUSY_CNT 20 /*  ? */
168 #define TONE_BUSY_SILENCE_CNT 48 /*  ? */
169
170 static int  entity;
171
172 static struct misdn_lib *glob_mgr;
173
174 unsigned char tone_425_flip[TONE_425_SIZE];
175 unsigned char tone_silence_flip[TONE_SILENCE_SIZE];
176
177 static void misdn_lib_isdn_event_catcher(void *arg);
178 static int handle_event_nt(void *dat, void *arg);
179
180
181 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder);
182 void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder);
183 struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id);
184
185 /* from isdn_lib.h */
186 int init_bc(struct misdn_stack * stack,  struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime);
187 struct misdn_stack* stack_init(int midev,  int port, int ptp);
188 void stack_te_destroy(struct misdn_stack* stack);
189         /* user iface */
190 int te_lib_init( void ) ; /* returns midev */
191 void te_lib_destroy(int midev) ;
192 struct misdn_bchannel *manager_find_bc_by_pid(int pid);
193 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc);
194 unsigned char * manager_flip_buf_bits ( unsigned char * buf , int len);
195 void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len);
196 void manager_clean_bc(struct misdn_bchannel *bc );
197 void manager_bchannel_setup (struct misdn_bchannel *bc);
198 void manager_bchannel_cleanup (struct misdn_bchannel *bc);
199
200 int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *frm, int nt);
201 enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *frm, int nt);
202 int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *frm, struct misdn_bchannel *bc, int nt);
203 char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt);
204 msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt);
205 void ec_chunk( struct misdn_bchannel *bc, unsigned char *rxchunk, unsigned char *txchunk, int chunk_size);
206         /* end */
207 int bchdev_echocancel_activate(struct misdn_bchannel* dev);
208 void bchdev_echocancel_deactivate(struct misdn_bchannel* dev);
209 /* end */
210
211
212 static char *bearer2str(int cap) {
213         static char *bearers[]={
214                 "Speech",
215                 "Audio 3.1k",
216                 "Unres Digital",
217                 "Res Digital",
218                 "Unknown Bearer"
219         };
220         
221         switch (cap) {
222         case INFO_CAPABILITY_SPEECH:
223                 return bearers[0];
224                 break;
225         case INFO_CAPABILITY_AUDIO_3_1K:
226                 return bearers[1];
227                 break;
228         case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
229                 return bearers[2];
230                 break;
231         case INFO_CAPABILITY_DIGITAL_RESTRICTED:
232                 return bearers[3];
233                 break;
234         default:
235                 return bearers[4];
236                 break;
237         }
238 }
239
240
241 static char flip_table[256];
242
243 void init_flip_bits(void)
244 {
245         int i,k;
246         
247         for (i = 0 ; i < 256 ; i++) {
248                 unsigned char sample = 0 ;
249                 for (k = 0; k<8; k++) {
250                         if ( i & 1 << k ) sample |= 0x80 >>  k;
251                 }
252                 flip_table[i] = sample;
253         }
254 }
255
256 unsigned char * flip_buf_bits ( unsigned char * buf , int len)
257 {
258         int i;
259         char * start = buf;
260         
261         for (i = 0 ; i < len; i++) {
262                 buf[i] = flip_table[buf[i]];
263         }
264         
265         return start;
266 }
267
268
269
270
271 msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */
272 {
273         int i = 0;
274         msg_t *dmsg;
275   
276         while(i < 10)
277         {
278                 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
279                 if (dmsg)
280                         return(dmsg);
281       
282                 if (!i)
283                         printf("cannot allocate memory, trying again...\n");
284                 i++;
285                 usleep(300000);
286         }
287         printf("cannot allocate memory, system overloaded.\n");
288         exit(-1);
289 }
290
291
292
293 msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
294 {
295         int i = 0;
296         msg_t *dmsg;
297         Q931_info_t *qi;
298         iframe_t *frm;
299   
300         if (!ntmode)
301                 size = sizeof(Q931_info_t)+2;
302   
303         while(i < 10) {
304                 if (ntmode) {
305                         dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
306                         if (dmsg) {
307                                 return(dmsg);
308                         }
309                 } else {
310                         dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
311                         if (dmsg)
312                         {
313                                 memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
314                                 frm = (iframe_t *)dmsg->data;
315                                 frm->prim = prim;
316                                 frm->dinfo = dinfo;
317                                 qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
318                                 qi->type = mt;
319                                 return(dmsg);
320                         }
321                 }
322     
323                 if (!i) printf("cannot allocate memory, trying again...\n");
324                 i++;
325                 usleep(300000);
326         }
327         printf("cannot allocate memory, system overloaded.\n");
328         exit(-1);
329 }
330
331
332 int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
333 {
334         iframe_t *frm;
335         frm = (iframe_t *)dmsg->data;
336         struct misdn_stack *stack=get_stack_by_bc(bc);
337
338
339         if (!stack) {
340                 cb_log(0,0,"send_msg: IEK!! no stack\n ");
341                 return -1;
342         }
343         
344         frm->addr = (stack->upper_id | FLG_MSG_DOWN);
345
346         
347         frm->dinfo = bc->l3_id;
348   
349         frm->len = (dmsg->len) - mISDN_HEADER_LEN;
350         
351         mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC);
352   
353         free_msg(dmsg);
354
355         return 0;
356 }
357
358
359 static int mypid=0;
360
361
362 int misdn_cap_is_speech(int cap)
363 /** Poor mans version **/
364 {
365         if ( (cap != INFO_CAPABILITY_DIGITAL_UNRESTRICTED) &&
366              (cap != INFO_CAPABILITY_DIGITAL_RESTRICTED) ) return 1;
367         return 0;
368 }
369
370 int misdn_inband_avail(struct misdn_bchannel *bc)
371 {
372
373         /*if ! early_bconnect we have never inband available*/
374         if ( ! bc->early_bconnect ) return 0;
375         
376         switch (bc->progress_indicator) {
377         case INFO_PI_INBAND_AVAILABLE:
378         case INFO_PI_CALL_NOT_E2E_ISDN:
379         case INFO_PI_CALLED_NOT_ISDN:
380                 return 1;
381         default:
382                 return 0;
383         }
384         return 0;
385 }
386
387
388 void dump_chan_list(struct misdn_stack *stack)
389 {
390         int i;
391
392         for (i=0; i <stack->b_num; i++) {
393                 cb_log(5, stack->port, "Idx:%d stack->cchan:%d Chan:%d\n",i,stack->channels[i], i+1);
394         }
395 }
396
397
398
399
400 static int find_free_chan_in_stack(struct misdn_stack *stack, int channel)
401 {
402         int i;
403
404         if (channel < 0 || channel > MAX_BCHANS) {
405                 cb_log(4, stack->port, " !! out of bound call to find_free_chan_in_stack! (ch:%d)\n", channel);
406                 return 0;
407         }
408         
409         channel--;
410   
411         for (i = 0; i < stack->b_num; i++) {
412                 if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 Dchannel ;) and work with chan preselection */
413                         if (!stack->channels[i]) {
414                                 cb_log (4, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1);
415                                 stack->channels[i] = 1;
416                                 return i+1;
417                         }
418                 }
419         }
420
421         cb_log (4, stack->port, " !! NO FREE CHAN IN STACK\n");
422         dump_chan_list(stack);
423   
424         return 0;
425 }
426
427 int empty_chan_in_stack(struct misdn_stack *stack, int channel)
428 {
429         cb_log (4, stack?stack->port:0, " --> empty chan %d\n",channel); 
430         stack->channels[channel-1] = 0;
431         dump_chan_list(stack);
432         return 0;
433 }
434
435
436 void empty_bc(struct misdn_bchannel *bc)
437 {
438         bc->bframe_len=0;
439
440         bc->state=STATE_NOTHING;
441         
442         bc->channel = 0;
443         bc->in_use = 0;
444
445         bc->need_more_infos = 0;
446         
447         bc->send_dtmf=0;
448         bc->nodsp=0;
449         bc->nojitter=0;
450
451         bc->time_usec=0;
452         
453         bc->rxgain=0;
454         bc->txgain=0;
455
456         bc->crypt=0;
457         bc->curptx=0; bc->curprx=0;
458         
459         bc->crypt_key[0] = 0;
460         
461         bc->generate_tone=0;
462         bc->tone_cnt=0;
463   
464         bc->dnumplan=NUMPLAN_UNKNOWN;
465         bc->onumplan=NUMPLAN_UNKNOWN;
466         bc->rnumplan=NUMPLAN_UNKNOWN;
467         
468
469         bc->active = 0;
470
471         bc->early_bconnect = 1;
472         
473         bc->ec_enable = 0;
474         bc->ec_deftaps = 128;
475         bc->ec_whenbridged = 0;
476         bc->ec_training = 1;
477         
478         
479         bc->orig=0;
480   
481         bc->cause=16;
482         bc->out_cause=16;
483         bc->pres=0 ; /* screened */
484         
485         bc->evq=EVENT_NOTHING;
486
487         bc->progress_coding=0;
488         bc->progress_location=0;
489         bc->progress_indicator=0;
490         
491 /** Set Default Bearer Caps **/
492         bc->capability=INFO_CAPABILITY_SPEECH;
493         bc->law=INFO_CODEC_ALAW;
494         bc->mode=0;
495         bc->rate=0;
496         bc->user1=0;
497         bc->urate=0;
498         
499         bc->hdlc=0;
500         
501         
502         bc->info_dad[0] = 0;
503         bc->display[0] = 0;
504         bc->infos_pending[0] = 0;
505         bc->oad[0] = 0;
506         bc->dad[0] = 0;
507         bc->orig_dad[0] = 0;
508         
509         bc->fac_type=FACILITY_NONE;
510         
511         bc->te_choose_channel = 0;
512 }
513
514
515 int clean_up_bc(struct misdn_bchannel *bc)
516 {
517         int ret=0;
518         unsigned char buff[32];
519         struct misdn_stack * stack;
520         
521         if (!bc  ) return -1;
522         stack=get_stack_by_bc(bc);
523         if (!stack) return -1;
524         
525         if (!bc->upset) {
526                 cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
527                 return -1;
528         }
529
530         if (bc->active) {
531                 cb_log(2, stack->port, "$$$ bc still active, deactivatiing .. stid :%x\n", bc->b_stid);
532                 manager_bchannel_deactivate(bc);
533         }
534         
535         cb_log(5, stack->port, "$$$ Cleaning up bc with stid :%x\n", bc->b_stid);
536         
537         
538         if ( misdn_cap_is_speech(bc->capability) && bc->ec_enable) {
539                 manager_ec_disable(bc);
540         }
541
542         mISDN_write_frame(stack->midev, buff, bc->addr|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
543         
544
545 #ifdef ACK_HDLC 
546         if (bc->ack_hdlc ) {
547                 free(bc->ack_hdlc);
548                 bc->ack_hdlc=NULL;
549         }
550 #endif
551         
552         
553         bc->b_stid = 0;
554         
555         bc->upset=0;
556         
557         
558         return ret;
559 }
560
561
562
563 void clear_l3(struct misdn_stack *stack)
564 {
565         int i;
566         for (i=0; i<stack->b_num; i++) {
567                 if (global_state == MISDN_INITIALIZED)  {
568                         cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
569                         empty_chan_in_stack(stack,i+1);
570                         empty_bc(&stack->bc[i]);
571                         clean_up_bc(&stack->bc[i]);
572                         
573                 }
574                 
575         } 
576 }
577
578 int set_chan_in_stack(struct misdn_stack *stack, int channel)
579 {
580         stack->channels[channel-1] = 1;
581   
582         return 0;
583 }
584
585 int chan_in_stack_free(struct misdn_stack *stack, int channel)
586 {
587         if (stack->channels[channel-1])
588                 return 0;
589   
590         return 1;
591 }
592
593
594
595 static int newteid=0;
596
597 #ifdef MISDNUSER_JOLLY
598 #define MAXPROCS 0x100
599 #else
600 #define MAXPROCS 0x10
601 #endif
602
603
604 int misdn_lib_get_l1_up(struct misdn_stack *stack)
605 {
606         /* Pull Up L1 if we have JOLLY */ 
607         iframe_t act;
608         act.prim = PH_ACTIVATE | REQUEST; 
609         act.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
610
611         
612         act.dinfo = 0;
613         act.len = 0;
614
615         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
616
617 }
618
619 int misdn_lib_get_l2_up(struct misdn_stack *stack)
620 {
621         
622         if (stack->ptp && (stack->nt) ) {
623                 msg_t *dmsg;
624                 /* L2 */
625                 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
626                 
627                 if (stack->nst.manager_l3(&stack->nst, dmsg))
628                         free_msg(dmsg);
629                 
630         } else {
631                 iframe_t act;
632                 
633                 act.prim = DL_ESTABLISH | REQUEST;
634                 act.addr = (stack->upper_id |FLG_MSG_DOWN)  ;
635                 
636                 act.dinfo = 0;
637                 act.len = 0;
638                 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
639         }
640         
641         return 0;
642 }
643
644
645 int misdn_lib_get_l2_status(struct misdn_stack *stack)
646 {
647         iframe_t act;
648         
649         act.prim = DL_ESTABLISH | REQUEST;
650
651         act.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
652
653         act.dinfo = 0;
654         act.len = 0;
655         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
656 }
657
658 int misdn_lib_get_short_status(struct misdn_stack *stack)
659 {
660         iframe_t act;
661         
662         
663         act.prim = MGR_SHORTSTATUS | REQUEST; 
664         
665         act.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
666
667         act.dinfo = SSTATUS_L1;
668         
669         act.len = 0;
670         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
671 }
672
673
674
675 static int create_process (int midev, struct misdn_bchannel *bc) {
676         iframe_t ncr;
677         int l3_id;
678         int i;
679         struct misdn_stack *stack=get_stack_by_bc(bc);
680         int free_chan;
681   
682         if (stack->nt) {
683                 free_chan = find_free_chan_in_stack(stack, bc->channel_preselected?bc->channel:0);
684                 if (!free_chan) return -1;
685                 bc->channel=free_chan;
686     
687                 for (i=0; i <= MAXPROCS; i++)
688                         if (stack->procids[i]==0) break;
689     
690                 if (i== MAXPROCS) {
691                         cb_log(-1, stack->port, "Couldnt Create New ProcId.\n");
692                         return -1;
693                 }
694                 stack->procids[i]=1;
695
696 #ifdef MISDNUSER_JOLLY
697                 l3_id = 0xff00 | i;
698 #else
699                 l3_id = 0xfff0 | i;
700 #endif
701     
702                 ncr.prim = CC_NEW_CR | REQUEST; 
703
704                 ncr.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
705
706                 ncr.dinfo = l3_id;
707                 ncr.len = 0;
708
709                 bc->l3_id = l3_id;
710                 if (mypid>5000) mypid=0;
711                 bc->pid=mypid++;
712       
713                 cb_log(3, stack->port, " --> new_l3id %x\n",l3_id);
714     
715         } else { 
716                 if (stack->ptp || bc->te_choose_channel) {
717                         /* we know exactly which channels are in use */
718                         free_chan = find_free_chan_in_stack(stack, bc->channel_preselected?bc->channel:0);
719                         if (!free_chan) return -1;
720                         bc->channel=free_chan;
721                 } else {
722                         /* other phones could have made a call also on this port (ptmp) */
723                         bc->channel=0xff;
724                 }
725     
726     
727                 /* if we are in te-mode, we need to create a process first */
728                 if (newteid++ > 0xffff)
729                         newteid = 0x0001;
730     
731                 l3_id = (entity<<16) | newteid;
732                 /* preparing message */
733                 ncr.prim = CC_NEW_CR | REQUEST; 
734
735                 ncr.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
736
737                 ncr.dinfo =l3_id;
738                 ncr.len = 0;
739                 /* send message */
740
741                 bc->l3_id = l3_id;
742                 if (mypid>5000) mypid=0;
743                 bc->pid=mypid++;
744     
745                 cb_log(3, stack->port, "--> new_l3id %x\n",l3_id);
746     
747                 mISDN_write(midev, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
748         }
749   
750         return l3_id;
751 }
752
753
754 void misdn_lib_setup_bc(struct misdn_bchannel *bc)
755 {
756         setup_bc(bc);
757 }
758
759
760 int setup_bc(struct misdn_bchannel *bc)
761 {
762         unsigned char buff[1025];
763   
764         mISDN_pid_t pid;
765         int ret;
766         
767
768         struct misdn_stack *stack=get_stack_by_bc(bc);
769         
770         int midev=stack->midev;
771         int channel=bc->channel-1-(bc->channel>16);
772         int b_stid=stack->b_stids[channel>=0?channel:0];
773
774         if (bc->hdlc) {
775                 clean_up_bc(bc);
776         }
777         
778         if (bc->upset) {
779                 cb_log(4, stack->port, "$$$ bc already upsetted stid :%x\n", b_stid);
780                 return -1;
781         }
782         
783         cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
784         
785         if (b_stid <= 0) {
786                 cb_log(-1, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
787
788                 return 1;
789         }
790         
791         
792         bc->b_stid = b_stid;
793         
794         {
795                 layer_info_t li;
796                 memset(&li, 0, sizeof(li));
797     
798                 li.object_id = -1;
799                 li.extentions = 0;
800                 
801                 li.st = bc->b_stid; /*  given idx */
802
803
804 #define MISDN_DSP
805 #ifndef MISDN_DSP
806                 bc->nodsp=1;
807 #endif
808                 if ( bc->hdlc || bc->nodsp) {
809                         cb_log(4, stack->port,"setup_bc: without dsp\n");
810                         { 
811                                 int l = sizeof(li.name);
812                                 strncpy(li.name, "B L3", l);
813                                 li.name[l-1] = 0;
814                         }
815                         li.pid.layermask = ISDN_LAYER((3));
816                         li.pid.protocol[3] = ISDN_PID_L3_B_USER;
817                         
818                         bc->layer=3;
819                 } else {
820                         cb_log(4, stack->port,"setup_bc: with dsp\n");
821                         { 
822                                 int l = sizeof(li.name);
823                                 strncpy(li.name, "B L4", l);
824                                 li.name[l-1] = 0;
825                         }
826                         li.pid.layermask = ISDN_LAYER((4));
827                         li.pid.protocol[4] = ISDN_PID_L4_B_USER
828 ;
829                         bc->layer=4;
830                         
831                 }  
832                 
833                 ret = mISDN_new_layer(midev, &li);
834                 if (ret ) {
835                         cb_log(-1, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
836                         
837                         return(-EINVAL);
838                 }
839                 
840                 bc->layer_id = li.id;
841         }
842         
843         memset(&pid, 0, sizeof(pid));
844         
845         
846         
847         cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
848         
849         if (bc->nodsp) {
850                 cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
851                 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
852                 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
853                 pid.protocol[3] = ISDN_PID_L3_B_USER;
854                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
855                 
856         } else if ( bc->hdlc ) {
857                 cb_log(2, stack->port," --> HDLC Mode\n");
858 #ifdef ACK_HDLC
859                 bc->ack_hdlc=(sem_t*)malloc(sizeof(sem_t));
860                 if ( sem_init((sem_t*)bc->ack_hdlc, 1, 0)<0 )
861                         sem_init((sem_t*)bc->ack_hdlc, 0, 0);
862 #endif
863                 
864                 pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
865                 pid.protocol[2] = ISDN_PID_L2_B_TRANS  ;
866                 pid.protocol[3] = ISDN_PID_L3_B_USER;
867                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
868         } else {
869                 cb_log(2, stack->port," --> TRANSPARENT Mode\n");
870                 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
871                 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
872                 pid.protocol[3] = ISDN_PID_L3_B_DSP;
873                 pid.protocol[4] = ISDN_PID_L4_B_USER;
874                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
875                 
876         } 
877         
878         ret = mISDN_set_stack(midev, bc->b_stid, &pid);
879         
880         if (ret){
881                 cb_log(5, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
882                 
883                 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
884                 return(-EINVAL);
885         }
886
887 #if 0
888         ret = mISDN_get_setstack_ind(midev, bc->layer_id );
889         
890         if (ret){
891                 cb_log(5, stack->port,"$$$ Get Set Stack Err: %d %s\n",ret,strerror(errno));
892                 
893                 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
894                 return(-EINVAL);
895         }
896
897 #endif
898
899         
900         bc->upset=1;
901         
902         
903         manager_bchannel_deactivate(bc);
904         
905         return 0;
906 }
907
908
909
910 /** IFACE **/
911 int init_bc(struct misdn_stack *stack,  struct misdn_bchannel *bc, int midev, int port, int bidx,  char *msn, int firsttime)
912 {
913         unsigned char buff[1025];
914         iframe_t *frm = (iframe_t *)buff;
915         int ret;
916   
917         if (!bc) return -1;
918   
919         cb_log(4, port, "Init.BC %d.\n",bidx);
920         
921         memset(bc, 0,sizeof(struct misdn_bchannel));
922         
923         if (msn) {
924                 int l = sizeof(bc->msn);
925                 strncpy(bc->msn,msn, l);
926                 bc->msn[l-1] = 0;
927         }
928         
929         
930         empty_bc(bc);
931         bc->upset=0;
932         bc->port=stack->port;
933         bc->nt=stack->nt?1:0;
934         
935         {
936                 ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
937
938                 if (!ibuf) return -1;
939                 
940                 clear_ibuffer( ibuf);
941                 
942                 ibuf->rsem=malloc(sizeof(sem_t));
943                 
944                 bc->astbuf=ibuf;
945
946                 if (sem_init(ibuf->rsem,1,0)<0)
947                         sem_init(ibuf->rsem,0,0);
948                 
949         }
950         
951         
952         
953         
954         {
955                 stack_info_t *stinf;
956                 ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
957                 if (ret < 0) {
958                         cb_log(-1, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
959                         return -1;
960                 }
961     
962                 stinf = (stack_info_t *)&frm->data.p;
963     
964                 cb_log(4, port, " --> Child %x\n",stinf->child[bidx]);
965         }
966   
967         return 0;
968 }
969
970
971
972 struct misdn_stack* stack_init( int midev, int port, int ptp )
973 {
974         int ret;
975         unsigned char buff[1025];
976         iframe_t *frm = (iframe_t *)buff;
977         stack_info_t *stinf;
978         int i; 
979         layer_info_t li;
980
981         struct misdn_stack *stack = malloc(sizeof(struct misdn_stack));
982         if (!stack ) return NULL;
983
984
985         cb_log(4, port, "Init. Stack.\n");
986   
987         memset(stack,0,sizeof(struct misdn_stack));
988   
989         for (i=0; i<MAX_BCHANS + 1; i++ ) stack->channels[i]=0;
990         
991         stack->port=port;
992         stack->midev=midev;
993         stack->ptp=ptp;
994   
995         stack->holding=NULL;
996         stack->pri=0;
997   
998         msg_queue_init(&stack->downqueue);
999   
1000         /* query port's requirements */
1001         ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
1002         if (ret < 0) {
1003                 cb_log(-1, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1004                 return(NULL);
1005         }
1006   
1007         stinf = (stack_info_t *)&frm->data.p;
1008
1009         stack->d_stid = stinf->id;
1010         stack->b_num = stinf->childcnt;
1011
1012         for (i=0; i<stinf->childcnt; i++)
1013                 stack->b_stids[i] = stinf->child[i];
1014   
1015         switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
1016         case ISDN_PID_L0_TE_S0:
1017                 stack->nt=0;
1018                 break;
1019         case ISDN_PID_L0_NT_S0:
1020                 cb_log(4, port, "NT Stack\n");
1021
1022                 stack->nt=1;
1023                 break;
1024
1025         case ISDN_PID_L0_TE_U:
1026                 break;
1027         case ISDN_PID_L0_NT_U:
1028                 break;
1029         case ISDN_PID_L0_TE_UP2:
1030                 break;
1031         case ISDN_PID_L0_NT_UP2:
1032                 break;
1033         case ISDN_PID_L0_TE_E1:
1034                 cb_log(4, port, "TE S2M Stack\n");
1035                 stack->nt=0;
1036                 stack->pri=1;
1037                 break;
1038         case ISDN_PID_L0_NT_E1:
1039                 cb_log(4, port, "TE S2M Stack\n");
1040                 stack->nt=1;
1041                 stack->pri=1;
1042                 
1043                 break;
1044         default:
1045                 cb_log(-1, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
1046
1047         }
1048
1049         if (!stack->nt) {
1050                 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) { 
1051                         stack->ptp = 1;
1052                 } else {
1053                         stack->ptp = 0;
1054                 }
1055         }
1056         
1057         
1058
1059         {
1060                 int ret;
1061                 int nt=stack->nt;
1062
1063                 cb_log(4, port, "Init. Stack.\n");
1064                 
1065                 
1066                 
1067                 memset(&li, 0, sizeof(li));
1068                 {
1069                         int l = sizeof(li.name);
1070                         strncpy(li.name,nt?"net l2":"user l4", l);
1071                         li.name[l-1] = 0;
1072                 }
1073                 li.object_id = -1;
1074                 li.extentions = 0;
1075                 li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1076                 li.pid.layermask = ISDN_LAYER((nt?2:4));
1077                 li.st = stack->d_stid;
1078                 
1079                 
1080                 ret = mISDN_new_layer(midev, &li);
1081                 if (ret) {
1082                         cb_log(-1, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
1083                         return(NULL);
1084                 }
1085                 
1086                 
1087                 stack->upper_id = li.id;
1088                 ret = mISDN_register_layer(midev, stack->d_stid, stack->upper_id);
1089                 if (ret)
1090                 {
1091                         cb_log(-1,port,"Cannot register layer %d of this port.\n", nt?2:4);
1092                         return(NULL);
1093                 }
1094                 
1095                 
1096                 stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3); 
1097                 if (stack->lower_id < 0) {
1098                         cb_log(-1, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
1099                         return(NULL);
1100                 }
1101                 
1102                 stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
1103                 if (stack->upper_id < 0) {
1104                         cb_log(-1, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, 2);
1105                         return(NULL);
1106                 }
1107                 
1108                 cb_log(4, port, "NT Stacks upper_id %x\n",stack->upper_id);
1109                 
1110                 
1111                 /* create nst (nt-mode only) */
1112                 if (nt) {
1113                         
1114                         memset(&stack->nst, 0, sizeof(net_stack_t));
1115                         memset(&stack->mgr, 0, sizeof(manager_t));
1116     
1117                         stack->mgr.nst = &stack->nst;
1118                         stack->nst.manager = &stack->mgr;
1119     
1120                         stack->nst.l3_manager = handle_event_nt;
1121                         stack->nst.device = midev;
1122                         stack->nst.cardnr = port;
1123                         stack->nst.d_stid = stack->d_stid;
1124     
1125                         stack->nst.feature = FEATURE_NET_HOLD;
1126                         if (stack->ptp)
1127                                 stack->nst.feature |= FEATURE_NET_PTP;
1128                         if (stack->pri)
1129                                 stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1130                         
1131                         stack->nst.l1_id = stack->lower_id;
1132                         stack->nst.l2_id = stack->upper_id;
1133                         
1134                         msg_queue_init(&stack->nst.down_queue);
1135                         
1136                         Isdnl2Init(&stack->nst);
1137                         Isdnl3Init(&stack->nst);
1138                         
1139                 } 
1140                 
1141                 if (!stack->nt) {
1142                         /*assume L1 is up, we'll get DEACTIVATES soon, for non
1143                          * up L1s*/
1144                         stack->l1link=1;
1145                 }
1146
1147                 misdn_lib_get_l1_up(stack);
1148                 misdn_lib_get_l2_up(stack);
1149                 
1150         }
1151
1152         cb_log(1,0,"stack_init: port:%d lowerId:%x  upperId:%x\n",stack->port,stack->lower_id, stack->upper_id);
1153         
1154         return stack;
1155 }
1156
1157
1158 void stack_te_destroy(struct misdn_stack* stack)
1159 {
1160         char buf[1024];
1161         if (!stack) return;
1162   
1163         if (stack->lower_id) 
1164                 mISDN_write_frame(stack->midev, buf, stack->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1165
1166         if (stack->upper_id) 
1167                 mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1168 }
1169
1170
1171 struct misdn_stack * find_stack_by_addr(int  addr)
1172 {
1173         struct misdn_stack *stack;
1174         
1175         for (stack=glob_mgr->stack_list;
1176              stack;
1177              stack=stack->next) {
1178                 if ( (stack->upper_id&STACK_ID_MASK) == (addr&STACK_ID_MASK)) return stack;
1179
1180         }
1181   
1182         return NULL;
1183 }
1184
1185
1186 struct misdn_stack * find_stack_by_port(int port)
1187 {
1188         struct misdn_stack *stack;
1189   
1190         for (stack=glob_mgr->stack_list;
1191              stack;
1192              stack=stack->next) 
1193                 if (stack->port == port) return stack;
1194   
1195         return NULL;
1196 }
1197
1198 struct misdn_stack * find_stack_by_mgr(manager_t* mgr_nt)
1199 {
1200         struct misdn_stack *stack;
1201   
1202         for (stack=glob_mgr->stack_list;
1203              stack;
1204              stack=stack->next) 
1205                 if ( &stack->mgr == mgr_nt) return stack;
1206   
1207         return NULL;
1208 }
1209
1210 struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
1211 {
1212         int i;
1213         for (i=0; i<stack->b_num; i++) {
1214                 if ( (stack->bc[i].l3_id & mask)  ==  (l3id & mask)) return &stack->bc[i] ;
1215         }
1216         return stack_holder_find(stack,l3id);
1217 }
1218
1219
1220 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
1221 {
1222         int i;
1223         for (i=0; i<stack->b_num; i++) {
1224                 if (stack->bc[i].l3_id == l3id) return &stack->bc[i] ;
1225         }
1226         return stack_holder_find(stack,l3id);
1227 }
1228
1229 struct misdn_bchannel *find_bc_holded(struct misdn_stack *stack)
1230 {
1231         int i;
1232         for (i=0; i<stack->b_num; i++) {
1233                 if (stack->bc[i].holded ) return &stack->bc[i] ;
1234         }
1235         return NULL;
1236 }
1237
1238
1239 struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
1240 {
1241         struct misdn_stack* stack;
1242         int i;
1243
1244         
1245         for (stack=glob_mgr->stack_list;
1246              stack;
1247              stack=stack->next) {
1248                 
1249                 for (i=0; i< stack->b_num; i++) {
1250
1251                         if ( (stack->bc[i].addr&STACK_ID_MASK)==(addr&STACK_ID_MASK) ||  stack->bc[i].layer_id== addr ) {
1252                                 return &stack->bc[i];
1253                         }
1254                 }
1255                 
1256         }
1257
1258         
1259         return NULL;
1260 }
1261
1262
1263
1264
1265
1266 int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
1267 {
1268         struct misdn_stack *stack=get_stack_by_bc(bc);
1269         
1270         if (!stack->nt) {
1271                 
1272                 switch (event) {
1273
1274                 case EVENT_CONNECT_ACKNOWLEDGE:
1275                         
1276                         if ( !misdn_cap_is_speech(bc->capability)) {
1277                                 int ret=setup_bc(bc);
1278                                 if (ret == -EINVAL){
1279                                         cb_log(-1,bc->port,"send_event: setup_bc failed\n");
1280                                         
1281                                 }
1282                         }
1283                         
1284                         manager_bchannel_activate(bc);
1285                         break;
1286                 case EVENT_CONNECT:
1287
1288                         if ( *bc->crypt_key ) {
1289                                 cb_log(4, stack->port, "ENABLING BLOWFISH channel:%d oad%d:%s dad%d:%s\n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
1290                                 
1291                                 manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
1292                         }
1293                 case EVENT_SETUP:
1294                         if (bc->channel>0 && bc->channel<255)
1295                                 set_chan_in_stack(stack, bc->channel);
1296                         break;
1297                 case EVENT_ALERTING:
1298                 case EVENT_PROGRESS:
1299                 case EVENT_PROCEEDING:
1300                 case EVENT_SETUP_ACKNOWLEDGE:
1301                         
1302                 {
1303                         if (bc->channel == 0xff) {
1304                                 bc->channel=find_free_chan_in_stack(stack, 0);
1305                                 if (!bc->channel) {
1306                                         cb_log(-1, stack->port, "Any Channel Requested, but we have no more!!\n");
1307                                         break;
1308                                 }
1309                         } 
1310                         
1311                         if (!stack->nt) {
1312                                 int ret=setup_bc(bc);
1313                                 if (ret == -EINVAL){
1314                                         cb_log(-1,bc->port,"handle_event: setup_bc failed\n");
1315                                         misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1316                                 }
1317                         }
1318                 }
1319                 
1320                 default:
1321                         break;
1322                 }
1323         } else {    /** NT MODE **/
1324                 
1325         }
1326         return 0;
1327 }
1328
1329 int handle_new_process(struct misdn_stack *stack, iframe_t *frm)
1330 {
1331   
1332         struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0);
1333         
1334         
1335         if (!bc) {
1336                 cb_log(-1, 0, " --> !! lib: No free channel!\n");
1337                 return -1;
1338         }
1339   
1340         cb_log(4, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
1341         bc->l3_id=frm->dinfo;
1342         
1343         if (mypid>5000) mypid=0;
1344         bc->pid=mypid++;
1345         return 0;
1346 }
1347
1348 int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
1349 {
1350         
1351         if (!stack) return -1;
1352   
1353         switch (frm->prim) {
1354         case CC_NEW_CR|INDICATION:
1355                 cb_log(4, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
1356                 handle_new_process(stack, frm); 
1357                 return 1;
1358         case CC_NEW_CR|CONFIRM:
1359                 return 1;
1360         case CC_NEW_CR|REQUEST:
1361                 return 1;
1362         case CC_RELEASE_CR|REQUEST:
1363                 return 1;
1364         case CC_RELEASE_CR|CONFIRM:
1365                 break;
1366         case CC_RELEASE_CR|INDICATION:
1367                 cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n",frm->dinfo);
1368                 {
1369                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, frm->dinfo);
1370                         struct misdn_bchannel dummybc;
1371       
1372                         if (!bc) {
1373                                 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
1374                                 memset (&dummybc,0,sizeof(dummybc));
1375                                 dummybc.port=stack->port;
1376                                 dummybc.l3_id=frm->dinfo;
1377                                 bc=&dummybc; 
1378                         }
1379       
1380                         if (bc) {
1381                                 cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo);
1382                                 empty_chan_in_stack(stack,bc->channel);
1383                                 empty_bc(bc);
1384                                 clean_up_bc(bc);
1385                                 dump_chan_list(stack);
1386                                 bc->pid = 0;
1387                                 cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
1388                                 
1389                                 if (bc->stack_holder) {
1390                                         cb_log(4,stack->port, "REMOVEING Holder\n");
1391                                         stack_holder_remove( stack, bc);
1392                                         free(bc);
1393                                 }
1394                         }
1395                         else {
1396                                 if (stack->nt) 
1397                                         cb_log(4, stack->port, "BC with dinfo: %x  not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr);
1398                         }
1399       
1400                         return 1;
1401                 }
1402                 break;
1403         }
1404   
1405         return 0;
1406 }
1407
1408
1409 /*Emptys bc if it's reserved (no SETUP out yet)*/
1410 void misdn_lib_release(struct misdn_bchannel *bc)
1411 {
1412         struct misdn_stack *stack=get_stack_by_bc(bc);
1413
1414         if (!stack) {
1415                 cb_log(1,0,"misdn_release: No Stack found\n");
1416                 return;
1417         }
1418         
1419         if (bc->channel>=0) {
1420                 empty_chan_in_stack(stack,bc->channel);
1421                 empty_bc(bc);
1422         }
1423         clean_up_bc(bc);
1424 }
1425
1426
1427
1428
1429 int misdn_lib_get_port_up (int port) 
1430 { /* Pull Up L1 if we have JOLLY */ 
1431         struct misdn_stack *stack;
1432         
1433         for (stack=glob_mgr->stack_list;
1434              stack;
1435              stack=stack->next) {
1436                 
1437                 if (stack->port == port) {
1438
1439                         if (!stack->l1link)
1440                                 misdn_lib_get_l1_up(stack);
1441                         if (!stack->l2link)
1442                                 misdn_lib_get_l2_up(stack);
1443                         
1444                         return 0;
1445                 }
1446         }
1447         return 0;
1448 }
1449
1450
1451 int misdn_lib_send_facility(struct misdn_bchannel *bc, enum facility_type fac, void *data)
1452 {
1453         switch (fac) {
1454         case FACILITY_CALLDEFLECT:
1455                 strcpy(bc->out_fac.calldeflect_nr,(char*)data);
1456                 break;
1457         default:
1458                 cb_log(1,bc?bc->port:0,"We don't handle this facility yet: %d\n",fac);
1459                 return 0;
1460         }
1461         
1462         bc->out_fac_type=fac;
1463         
1464         misdn_lib_send_event(bc,EVENT_FACILITY);
1465         return 0;
1466 }
1467
1468
1469 int misdn_lib_port_up(int port)
1470 {
1471         struct misdn_stack *stack;
1472         
1473         for (stack=glob_mgr->stack_list;
1474              stack;
1475              stack=stack->next) {
1476                 
1477                 if (stack->port == port) {
1478                         if (stack->nt) {
1479                                 if (stack->l1link)
1480                                         return 1;
1481                                 else
1482                                         return 0;
1483                         } else {
1484                                 if (stack->l1link)
1485                                         return 1;
1486                                 else
1487                                         return 0;
1488                         }
1489
1490                 }
1491         }
1492   
1493         return -1;
1494 }
1495
1496
1497 int
1498 handle_event_nt(void *dat, void *arg)
1499 {
1500         manager_t *mgr = (manager_t *)dat;
1501         msg_t *msg = (msg_t *)arg;
1502         mISDNuser_head_t *hh;
1503
1504         struct misdn_stack *stack=find_stack_by_mgr(mgr);
1505         int port;
1506
1507         if (!msg || !mgr)
1508                 return(-EINVAL);
1509
1510         hh=(mISDNuser_head_t*)msg->data;
1511
1512
1513         port=stack->port;
1514         
1515         
1516         cb_log(4, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1517   
1518         {
1519                 switch(hh->prim){
1520                         
1521                 case CC_RETRIEVE|INDICATION:
1522                 {
1523                         iframe_t frm; /* fake te frm to add callref to global callreflist */
1524                         frm.dinfo = hh->dinfo;
1525
1526                         frm.addr=stack->upper_id | FLG_MSG_DOWN;
1527
1528                         frm.prim = CC_NEW_CR|INDICATION;
1529                         
1530                         if (handle_cr( stack, &frm)< 0) {
1531                                 msg_t *dmsg;
1532                                 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1533                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1534                                 stack->nst.manager_l3(&stack->nst, dmsg);
1535                                 free_msg(msg);
1536                                 return 0;
1537                         }
1538                         
1539                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1540                         cb_event(EVENT_NEW_BC, bc, glob_mgr->user_data);
1541                         struct misdn_bchannel *hold_bc=stack_holder_find(stack,bc->l3_id);
1542                         if (hold_bc) {
1543                                 cb_log(4, stack->port, "REMOVEING Holder\n");
1544                                 stack_holder_remove(stack, hold_bc);
1545                                 free(hold_bc);
1546                         }
1547                         
1548                 }
1549                         
1550                         break;
1551                         
1552                         
1553                 case CC_SETUP|CONFIRM:
1554                 {
1555                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1556                         int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1557                         
1558                         cb_log(4, bc?stack->port:0, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID  is %x\n",l3id );
1559         
1560                         if (!bc) { cb_log(4, 0, "Bc Not found (after SETUP CONFIRM)\n"); return 0; }
1561                         cb_log (2,bc->port,"I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
1562                         bc->l3_id=l3id;
1563                         cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1564                 }
1565                 free_msg(msg);
1566                 return 0;
1567       
1568                 case CC_SETUP|INDICATION:
1569                 {
1570                         iframe_t frm; /* fake te frm to add callref to global callreflist */
1571                         frm.dinfo = hh->dinfo;
1572                         frm.addr=stack->upper_id;
1573                         frm.prim = CC_NEW_CR|INDICATION;
1574                         
1575                         if (handle_cr(stack, &frm)< 0) {
1576                                 msg_t *dmsg;
1577                                 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1578                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1579                                 stack->nst.manager_l3(&stack->nst, dmsg);
1580                                 free_msg(msg);
1581                                 return 0;
1582                         }
1583                 }
1584                 break;
1585
1586                 case CC_CONNECT_ACKNOWLEDGE|INDICATION:
1587 #if 0
1588                 {
1589                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1590                         if (bc) {
1591                                 if ( !misdn_cap_is_speech(bc->capability)) {
1592                                         int ret=setup_bc(bc);
1593                                         if (ret == -EINVAL){
1594                                                 cb_log(-1,bc->port,"send_event: setup_bc failed\n");
1595                                                 
1596                                         }
1597                                 }
1598                                 
1599                                 manager_bchannel_activate(bc);
1600                         }
1601                 }
1602 #endif
1603                 break;
1604                 
1605                 case CC_CONNECT|INDICATION:
1606                 case CC_ALERTING|INDICATION:
1607                 case CC_PROCEEDING|INDICATION:
1608
1609                 {
1610                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1611                         
1612                         if (!bc) {
1613                                 msg_t *dmsg;
1614                                 cb_log(-1, stack->port,"!!!! We didn't found our bc, dinfo:%x on this port.\n",hh->dinfo);
1615                                 
1616                                 cb_log(-1, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
1617                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1618                                 stack->nst.manager_l3(&stack->nst, dmsg);
1619                                 free_msg(msg);
1620                                 return 0;
1621                                 
1622                         }
1623                         int ret=setup_bc(bc);
1624                         if (ret == -EINVAL){
1625                                 cb_log(-1,bc->port,"handle_event_nt: setup_bc failed\n");
1626                                 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1627                         }
1628                 }
1629                 break;
1630                 case CC_DISCONNECT|INDICATION:
1631                 {
1632                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1633                         if (!bc) {
1634                                 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1635                                 if (bc) { 
1636                                         int myprocid=bc->l3_id&0x0000ffff;
1637                                         hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1638                                         cb_log(3,stack->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
1639                                         return 0;
1640                                 }
1641                         }
1642                 }
1643                 break;
1644                 
1645                 case CC_FACILITY|INDICATION:
1646                 {
1647                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1648                         if (!bc) {
1649                                 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1650                                 if (bc) { 
1651                                         int myprocid=bc->l3_id&0x0000ffff;
1652                                         hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1653                                         cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
1654                                 }
1655                         }
1656                 }
1657                 break;
1658                 
1659
1660                 case CC_RELEASE_COMPLETE|INDICATION:
1661                         break;
1662
1663                 case CC_SUSPEND|INDICATION:
1664                 {
1665                         msg_t *dmsg;
1666                         cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
1667                         dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1668                         stack->nst.manager_l3(&stack->nst, dmsg);
1669                         free_msg(msg);
1670                         return 0;
1671                 }
1672                 break;
1673                 case CC_RESUME|INDICATION:
1674                         break;
1675
1676                 case CC_RELEASE|CONFIRM:
1677                 {
1678                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1679                         cb_log(3, stack->port, " --> RELEASE CONFIRM, doing nothin\n");
1680                         cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
1681                         empty_chan_in_stack(stack,bc->channel);
1682                         empty_bc(bc);
1683                         free_msg(msg);
1684                         return 0;
1685                 }
1686                 break;  
1687                 case CC_RELEASE|INDICATION:
1688                         break;
1689
1690                 case CC_RELEASE_CR|INDICATION:
1691                 {
1692                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1693                         struct misdn_bchannel dummybc;
1694                         iframe_t frm; /* fake te frm to remove callref from global callreflist */
1695                         frm.dinfo = hh->dinfo;
1696
1697                         frm.addr=stack->upper_id | FLG_MSG_DOWN;
1698
1699
1700                         frm.prim = CC_RELEASE_CR|INDICATION;
1701                         cb_log(4, stack->port, " --> Faking Realease_cr for %x\n",frm.addr);
1702                         /** removing procid **/
1703
1704                         if (!bc) {
1705                                 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
1706                                 memset (&dummybc,0,sizeof(dummybc));
1707                                 dummybc.port=stack->port;
1708                                 dummybc.l3_id=hh->dinfo;
1709                                 bc=&dummybc; 
1710                         }
1711         
1712                         if (bc) {
1713 #ifdef MISDNUSER_JOLLY
1714                                 if ( (bc->l3_id & 0xff00) == 0xff00) {
1715                                         cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xff);
1716                                         stack->procids[bc->l3_id&0xff] = 0 ;
1717                                 }
1718 #else
1719                                 if ( (bc->l3_id & 0xfff0) == 0xfff0) {
1720                                         cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xf);
1721                                         stack->procids[bc->l3_id&0xf] = 0 ;
1722             
1723                                 }
1724           
1725 #endif
1726
1727                         }
1728                         else cb_log(-1, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is a bad port.\n");
1729         
1730                         handle_cr(stack, &frm);
1731                         free_msg(msg);
1732                         return 0 ;
1733                 }
1734       
1735                 break;
1736       
1737                 case CC_NEW_CR|INDICATION:
1738                         /*  Got New CR for bchan, for now I handle this one in */
1739                         /*  connect_ack, Need to be changed */
1740                 {
1741                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1742                         int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1743                         if (!bc) { cb_log(-1, 0, " --> In NEW_CR: didn't found bc ??\n"); return -1;};
1744 #ifdef MISDNUSER_JOLLY
1745                         if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
1746                                 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
1747                                 stack->procids[bc->l3_id&0xff] = 0 ;
1748                         }
1749 #else
1750                         if (((l3id&0xfff0)!=0xfff0) && ((bc->l3_id&0xfff0)==0xfff0)) {
1751                                 cb_log(4, stack->port, "Removing Process Id:%x on this port.\n", 0xf&bc->l3_id);
1752                                 stack->procids[bc->l3_id&0xf] = 0 ;
1753                         }
1754
1755 #endif
1756                         cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID  is %x\n",l3id );
1757         
1758                         bc->l3_id =l3id;
1759                         cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1760         
1761         
1762                         free_msg(msg);
1763                         return 0;
1764                 }
1765       
1766                 case DL_ESTABLISH | INDICATION:
1767                 case DL_ESTABLISH | CONFIRM:
1768                 {
1769                         cb_log(4, stack->port, "%% GOT L2 Activate Info.\n");
1770                         stack->l2link = 1;
1771                         
1772                         free_msg(msg);
1773                         return 0;
1774                 }
1775                 break;
1776
1777                 case DL_RELEASE | INDICATION:
1778                 case DL_RELEASE | CONFIRM:
1779                 {
1780                         cb_log(4, stack->port, "%% GOT L2 DeActivate Info.\n");
1781                         stack->l2link = 0;
1782                         
1783                         free_msg(msg);
1784                         return 0;
1785                 }
1786                 break;
1787                 }
1788         }
1789         
1790   
1791
1792         {
1793                 /*  Parse Events and fire_up to App. */
1794                 struct misdn_bchannel *bc;
1795                 struct misdn_bchannel dummybc;
1796                 
1797                 enum event_e event = isdn_msg_get_event(msgs_g, msg, 1);
1798     
1799                 bc=find_bc_by_l3id(stack, hh->dinfo);
1800     
1801                 if (!bc) {
1802       
1803                         cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x).\n", hh->dinfo);
1804                         memset (&dummybc,0,sizeof(dummybc));
1805                         dummybc.port=stack->port;
1806                         dummybc.l3_id=hh->dinfo;
1807                         bc=&dummybc; 
1808                 }
1809                 if (bc ) {
1810                         isdn_msg_parse_event(msgs_g,msg,bc, 1);
1811                         
1812                         if(!isdn_get_info(msgs_g,event,1)) {
1813                                 cb_log(4, stack->port, "Unknown Event Ind: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1814                         } else {
1815                                 cb_event(event, bc, glob_mgr->user_data);
1816                         }
1817
1818       
1819                 } else {
1820                         cb_log(4, stack->port, "No BC found with l3id: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1821                 }
1822
1823                 free_msg(msg);
1824         }
1825
1826
1827         return 0;
1828 }
1829
1830
1831 int handle_timers(msg_t* msg)
1832 {
1833         iframe_t *frm= (iframe_t*)msg->data;
1834         struct misdn_stack *stack; 
1835   
1836         /* Timer Stuff */
1837         switch (frm->prim) {
1838         case MGR_INITTIMER | CONFIRM:
1839         case MGR_ADDTIMER | CONFIRM:
1840         case MGR_DELTIMER | CONFIRM:
1841         case MGR_REMOVETIMER | CONFIRM:
1842                 free_msg(msg);
1843                 return(1);
1844         }
1845   
1846   
1847   
1848         if (frm->prim==(MGR_TIMER | INDICATION) ) {
1849                 for (stack = glob_mgr->stack_list;
1850                      stack;
1851                      stack = stack->next) {
1852                         itimer_t *it;
1853       
1854                         if (!stack->nt) continue;
1855       
1856                         it = stack->nst.tlist;
1857                         /* find timer */
1858                         for(it=stack->nst.tlist;
1859                             it;
1860                             it=it->next) {
1861                                 if (it->id == (int)frm->addr)
1862                                         break;
1863                         }
1864                         if (it) {
1865                                 int ret;
1866                                 ret = mISDN_write_frame(stack->midev, msg->data, frm->addr,
1867                                                         MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
1868                                 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
1869                                 ret = it->function(it->data);
1870                                 free_msg(msg);
1871                                 return 1;
1872                         }
1873                 }
1874     
1875                 cb_log(-1, 0, "Timer Msg without Timer ??\n");
1876                 free_msg(msg);
1877                 return 1;
1878         }
1879   
1880         return 0;
1881 }
1882
1883
1884
1885 void misdn_lib_tone_generator_start(struct misdn_bchannel *bc)
1886 {
1887         bc->generate_tone=1;
1888 }
1889
1890 void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc)
1891 {
1892         bc->generate_tone=0;
1893 }
1894
1895
1896 static int do_tone(struct misdn_bchannel *bc, int len)
1897 {
1898         bc->tone_cnt=len;
1899         
1900         if (bc->generate_tone) {
1901                 cb_event(EVENT_TONE_GENERATE, bc, glob_mgr->user_data);
1902
1903                 if ( !bc->nojitter ) {
1904                         misdn_tx_jitter(bc,len);
1905                 }
1906                 
1907                 return 1;
1908         }
1909         
1910         return 0;
1911 }
1912
1913
1914
1915 void misdn_tx_jitter(struct misdn_bchannel *bc, int len)
1916 {
1917         char buf[4096 + mISDN_HEADER_LEN];
1918         iframe_t *txfrm= (iframe_t*)buf;
1919         int jlen, r;
1920         
1921         jlen=cb_jb_empty(bc,&buf[mISDN_HEADER_LEN],len);
1922         
1923         if (jlen) {
1924                 
1925                 if (jlen < len) {
1926                         cb_log(5,bc->port,"Jitterbuffer Underrun.\n");
1927                 }
1928                 
1929                 txfrm->prim = DL_DATA|REQUEST;
1930                 
1931                 txfrm->dinfo = 0;
1932                 
1933                 txfrm->addr = bc->addr|FLG_MSG_DOWN; /*  | IF_DOWN; */
1934                 
1935                 txfrm->len =jlen;
1936                 cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
1937                 
1938                 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
1939         }
1940 }
1941
1942 int handle_bchan(msg_t *msg)
1943 {
1944         iframe_t *frm= (iframe_t*)msg->data;
1945         struct misdn_bchannel *bc;
1946
1947         bc=find_bc_by_addr(frm->addr);
1948         
1949         if (!bc) {
1950                 cb_log(0,0,"handle_bchan: BC not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
1951                 return 0 ;
1952         }
1953         
1954         struct misdn_stack *stack=get_stack_by_bc(bc);
1955         
1956         if (!stack) {
1957                 cb_log(0,0,"handle_bchan: STACK not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
1958                 return 0;
1959         }
1960         
1961         switch (frm->prim) {
1962         case MGR_SETSTACK| INDICATION:
1963                 cb_log(2, stack->port, "BCHAN: MGR_SETSTACK|IND \n");
1964                 
1965                 
1966                 bc->addr = mISDN_get_layerid(stack->midev, bc->b_stid, bc->layer);
1967                 if (!bc->addr) {
1968                         cb_log(0,stack->port,"$$$ Get Layer (%d) Id Error: %s\n",bc->layer,strerror(errno));
1969
1970                         /* we kill the channel later, when we received some
1971                            data. */
1972                         bc->addr= frm->addr;
1973                 }
1974                 
1975                 cb_log(4, stack->port," --> Got Adr %x\n", bc->addr);
1976                 
1977                 bc->upset=2;
1978                 
1979                 free_msg(msg);
1980                 
1981                 manager_bchannel_activate(bc);
1982                                 
1983                 return 1;
1984         case MGR_DELLAYER| CONFIRM:
1985                 cb_log(2, stack->port, "BCHAN: MGR_DELLAYER|CNF \n");
1986                 
1987                 free_msg(msg);
1988                 return 1;
1989                 
1990         case PH_ACTIVATE | INDICATION:
1991         case DL_ESTABLISH | INDICATION:
1992                 cb_log(4, stack->port, "BCHAN: ACT Ind\n");
1993                 free_msg(msg);
1994                 return 1;    
1995
1996         case PH_ACTIVATE | CONFIRM:
1997         case DL_ESTABLISH | CONFIRM:
1998                 cb_log(4, stack->port, "BCHAN: bchan ACT Confirm\n");
1999                 free_msg(msg);
2000                 
2001                 return 1;    
2002                 
2003         case PH_DEACTIVATE | INDICATION:
2004         case DL_RELEASE | INDICATION:
2005                 cb_log (4, stack->port, "BCHAN: DeACT Ind\n");
2006                 free_msg(msg);
2007                 return 1;
2008     
2009         case PH_DEACTIVATE | CONFIRM:
2010         case DL_RELEASE | CONFIRM:
2011                 cb_log(4, stack->port, "BCHAN: DeACT Conf\n");
2012                 free_msg(msg);
2013                 return 1;
2014     
2015         case PH_CONTROL|INDICATION:
2016         {
2017                 unsigned long cont = *((unsigned long *)&frm->data.p);
2018                 
2019                 cb_log(4, stack->port, "PH_CONTROL: channel:%d oad%d:%s dad%d:%s \n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
2020
2021                 if ((cont&~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
2022                         int dtmf = cont & DTMF_TONE_MASK;
2023                         cb_log(4, stack->port, " --> DTMF TONE: %c\n",dtmf);
2024                         bc->dtmf=dtmf;
2025                         cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data);
2026         
2027                         free_msg(msg);
2028                         return 1;
2029                 }
2030                 if (cont == BF_REJECT) {
2031                         cb_log(4, stack->port, " --> BF REJECT\n");
2032                         free_msg(msg);
2033                         return 1;
2034                 }
2035                 if (cont == BF_ACCEPT) {
2036                         cb_log(4, stack->port, " --> BF ACCEPT\n");
2037                         free_msg(msg);
2038                         return 1;
2039                 }
2040         }
2041         break;
2042
2043         case PH_DATA|REQUEST:
2044         case DL_DATA|REQUEST:
2045                 cb_log(0, stack->port, "DL_DATA REQUEST \n");
2046                 do_tone(bc, 64);
2047                 
2048                 free_msg(msg);
2049                 return 1;
2050                 
2051         case PH_DATA|INDICATION:
2052         case DL_DATA|INDICATION:
2053         {
2054                 bc->bframe = (void*)&frm->data.i;
2055                 bc->bframe_len = frm->len;
2056
2057                 /** Anyway flip the bufbits **/
2058                 if ( misdn_cap_is_speech(bc->capability) ) 
2059                         flip_buf_bits(bc->bframe, bc->bframe_len);
2060                 
2061                 
2062 #if MISDN_DEBUG
2063                 cb_log(-1, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
2064
2065 #endif
2066                 
2067                 if (bc->active && frm->len > 0) {
2068                         if (  !do_tone(bc, frm->len)   ) {
2069                                 
2070                                 if ( misdn_cap_is_speech(bc->capability)) {
2071                                         if ( !bc->nojitter ) {
2072                                                 misdn_tx_jitter(bc,frm->len);
2073                                         }
2074                                 }
2075                                 
2076                                 int i=cb_event( EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
2077                                 if (i<0) {
2078                                         clean_up_bc(bc);
2079                                 }
2080                         }
2081                 }
2082                 free_msg(msg);
2083                 return 1;
2084         }
2085
2086         case PH_DATA | CONFIRM:
2087         case DL_DATA|CONFIRM:
2088 #if MISDN_DEBUG
2089
2090                 cb_log(-1, stack->port, "Data confirmed\n");
2091
2092 #endif
2093                 free_msg(msg);
2094
2095 #ifdef ACK_HDLC
2096                 if (bc->hdlc) {
2097                         cb_log(4,stack->port,"Acknowledge Packet\n");
2098                         sem_post( (sem_t*)bc->ack_hdlc);
2099                 }
2100 #endif
2101                 return 1;
2102         case DL_DATA|RESPONSE:
2103 #if MISDN_DEBUG
2104                 cb_log(-1, stack->port, "Data response\n");
2105
2106 #endif
2107                 break;
2108         }
2109   
2110         return 0;
2111 }
2112
2113
2114
2115 int handle_frm_nt(msg_t *msg)
2116 {
2117         iframe_t *frm= (iframe_t*)msg->data;
2118         struct misdn_stack *stack;
2119         int err=0;
2120
2121         stack=find_stack_by_addr( frm->addr );
2122
2123         
2124   
2125         if (!stack || !stack->nt) {
2126                 return 0;
2127         }
2128   
2129         if ((err=stack->nst.l1_l2(&stack->nst,msg))) {
2130     
2131                 if (nt_err_cnt > 0 ) {
2132                         if (nt_err_cnt < 100) {
2133                                 nt_err_cnt++; 
2134                                 cb_log(-1, stack->port, "NT Stack sends us error: %d \n", err);
2135                         } else if (nt_err_cnt < 105){
2136                                 cb_log(-1, stack->port, "NT Stack sends us error: %d over 100 times, so I'll stop this message\n", err);
2137                                 nt_err_cnt = - 1; 
2138                         }
2139                 }
2140                 free_msg(msg);
2141                 return 1;
2142                 
2143         }
2144         
2145         return 1;
2146 }
2147
2148
2149 int handle_frm(msg_t *msg)
2150 {
2151         iframe_t *frm = (iframe_t*) msg->data;
2152         
2153         struct misdn_stack *stack=find_stack_by_addr(frm->addr);
2154
2155         
2156         cb_log(4,stack?stack->port:0,"handle_frm: frm->addr:%x frm->prim:%x\n",frm->addr,frm->prim);
2157         
2158   
2159         if (!stack || stack->nt) {
2160                 return 0;
2161         }
2162
2163         {
2164                 struct misdn_bchannel *bc;
2165
2166                 if(handle_cr(stack, frm)) {
2167                         free_msg(msg);
2168                         return 1;
2169                 }
2170     
2171                 bc=find_bc_by_l3id(stack, frm->dinfo);
2172     
2173                 if (bc ) {
2174                         enum event_e event = isdn_msg_get_event(msgs_g, msg, 0);
2175                         enum event_response_e response=RESPONSE_OK;
2176       
2177                         isdn_msg_parse_event(msgs_g,msg,bc, 0);
2178                         
2179                         /** Preprocess some Events **/
2180                         handle_event(bc, event, frm);
2181                         /*  shoot up event to App: */
2182                         cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2183       
2184                         if(!isdn_get_info(msgs_g,event,0)) 
2185                                 cb_log(-1, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2186                         else 
2187                                 response=cb_event(event, bc, glob_mgr->user_data);
2188 #if 1
2189                         if (event == EVENT_SETUP) {
2190                                 switch (response) {
2191                                 case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
2192
2193                                         cb_log(-1, stack->port, "TOTALY IGNORING SETUP \n");                                    
2194                                         
2195                                         break;
2196                                 case RESPONSE_IGNORE_SETUP:
2197                                         /* I think we should send CC_RELEASE_CR, but am not sure*/
2198                                         empty_chan_in_stack(stack, bc->channel);
2199                                         empty_bc(bc);
2200                                         cb_log(-1, stack->port, "GOT IGNORE SETUP\n");
2201
2202                                         
2203                                         break;
2204                                 case RESPONSE_OK:
2205                                         cb_log(4, stack->port, "GOT SETUP OK\n");
2206
2207                                         
2208                                         break;
2209                                 default:
2210                                         break;
2211                                 }
2212                         }
2213
2214                         cb_log(5, stack->port, "Freeing Msg on prim:%x \n",frm->prim);
2215
2216                         
2217                         free_msg(msg);
2218                         return 1;
2219 #endif
2220       
2221                 } else {
2222         
2223                         cb_log(-1, stack->port, "NO BC FOR STACK\n");           
2224                         
2225                 }
2226
2227         }
2228
2229         cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x \n",frm->prim);
2230
2231         
2232   
2233         return 0;
2234 }
2235
2236
2237 int handle_l1(msg_t *msg)
2238 {
2239         iframe_t *frm = (iframe_t*) msg->data;
2240
2241         struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2242
2243         
2244         int i ;
2245         
2246         if (!stack) return 0 ;
2247   
2248         switch (frm->prim) {
2249         case PH_ACTIVATE | CONFIRM:
2250         case PH_ACTIVATE | INDICATION:
2251                 cb_log (1, stack->port, "L1: PH L1Link Up!\n");
2252                 stack->l1link=1;
2253                 
2254                 if (stack->nt) {
2255                         
2256                         if (stack->nst.l1_l2(&stack->nst, msg))
2257                                 free_msg(msg);
2258                 } else {
2259                         free_msg(msg);
2260                 }
2261                 
2262                 for (i=0;i<stack->b_num; i++) {
2263                         if (stack->bc[i].evq != EVENT_NOTHING) {
2264                                 cb_log(4, stack->port, "Fireing Queued Event %s because L1 got up\n", isdn_get_info(msgs_g, stack->bc[i].evq, 0));
2265                                 misdn_lib_send_event(&stack->bc[i],stack->bc[i].evq);
2266                                 stack->bc[i].evq=EVENT_NOTHING;
2267                         }
2268                         
2269                 }
2270                 
2271                 return 1;
2272
2273         case PH_ACTIVATE | REQUEST:
2274                 free_msg(msg);
2275                 cb_log(1,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
2276                 
2277                 return 1;
2278                 
2279         case PH_DEACTIVATE | REQUEST:
2280                 free_msg(msg);
2281                 cb_log(1,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
2282                 
2283                 return 1;
2284                 
2285         case PH_DEACTIVATE | CONFIRM:
2286         case PH_DEACTIVATE | INDICATION:
2287                 cb_log (1, stack->port, "L1: PH L1Link Down! \n");
2288                 
2289                 for (i=0; i<stack->b_num; i++) {
2290                         if (global_state == MISDN_INITIALIZED)  {
2291                                 cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
2292                         }
2293                         
2294                 }
2295                 
2296                 if (stack->nt) {
2297                         if (stack->nst.l1_l2(&stack->nst, msg))
2298                                 free_msg(msg);
2299                         
2300                 } else {
2301                         free_msg(msg);
2302                 }
2303                 
2304                 stack->l1link=0;
2305                 stack->l2link=0;
2306                 
2307                 return 1;
2308         }
2309   
2310         return 0;
2311 }
2312
2313 int handle_l2(msg_t *msg)
2314 {
2315         iframe_t *frm = (iframe_t*) msg->data;
2316
2317         struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2318
2319         
2320         
2321         if (!stack) {
2322                 return 0 ;
2323         }
2324         
2325         switch(frm->prim) {
2326
2327         case DL_ESTABLISH | REQUEST:
2328                 cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
2329                 return 1;
2330         case DL_RELEASE | REQUEST:
2331                 cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
2332                 return 1;
2333                 
2334         case DL_ESTABLISH | INDICATION:
2335         case DL_ESTABLISH | CONFIRM:
2336         {
2337                 cb_log (3, stack->port, "L2: L2Link Up! \n");
2338                 stack->l2link=1;
2339                 free_msg(msg);
2340                 return 1;
2341         }
2342         break;
2343     
2344         case DL_RELEASE | INDICATION:
2345         case DL_RELEASE | CONFIRM:
2346         {
2347                 cb_log (3, stack->port, "L2: L2Link Down! \n");
2348                 stack->l2link=0;
2349                 
2350                 free_msg(msg);
2351                 return 1;
2352         }
2353         break;
2354         }
2355         return 0;
2356 }
2357
2358
2359 int handle_mgmt(msg_t *msg)
2360 {
2361         iframe_t *frm = (iframe_t*) msg->data;
2362         
2363
2364
2365         if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
2366                 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
2367                 free(msg);
2368                 return 1;
2369         }
2370         
2371         struct misdn_stack * stack=find_stack_by_addr(frm->addr);
2372         
2373         if (!stack) {
2374                 return 0;
2375         }
2376         
2377         switch(frm->prim) {
2378         case MGR_SHORTSTATUS | INDICATION:
2379                 cb_log(2, 0, "MGMT: Short status dinfo %x\n",frm->dinfo);
2380                 
2381                 switch (frm->dinfo) {
2382                 case SSTATUS_L1_ACTIVATED:
2383                         cb_log(1, 0, "MGMT: SSTATUS: L1_ACTIVATED \n");
2384                         stack->l1link=1;
2385                         break;
2386                 case SSTATUS_L1_DEACTIVATED:
2387                         cb_log(1, 0, "MGMT: SSTATUS: L1_DEACTIVATED \n");
2388
2389                         /*reopen L1 if down*/
2390                         if (stack->l1link==2)
2391                                 stack->l1link--;
2392                         else                    
2393                                 stack->l1link=0;
2394                         
2395                         break;
2396                 case SSTATUS_L2_ESTABLISHED:
2397                         cb_log(1, 0, "MGMT: SSTATUS: L2_ESTABLISH \n");
2398                         stack->l2link=1;
2399                         
2400                         break;
2401                         
2402                 case SSTATUS_L2_RELEASED:
2403                         cb_log(1, 0, "MGMT: SSTATUS: L2_RELEASED \n");
2404                         stack->l2link=0;
2405                         stack->l1link=2;
2406                         
2407                         break;
2408                         
2409                 }
2410                 
2411                 
2412                 free_msg(msg);
2413                 return 1;
2414                 
2415         case MGR_SETSTACK | INDICATION:
2416                 cb_log(2, 0, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
2417                 free_msg(msg);
2418                 return 1;
2419         case MGR_DELLAYER | CONFIRM:
2420                 cb_log(2, 0, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
2421                 free_msg(msg);
2422                 return 1;
2423                 
2424         }
2425         
2426         /*
2427         if ( (frm->prim & 0x0f0000) ==  0x0f0000) {
2428         cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
2429         free_msg(msg);
2430         return 1;
2431         } */
2432     
2433         return 0;
2434 }
2435
2436
2437 msg_t *fetch_msg(int midev) 
2438 {
2439         msg_t *msg=alloc_msg(MAX_MSG_SIZE);
2440         int r;
2441         fd_set rdfs;
2442
2443         if (!msg) {
2444                 cb_log(-1, 0, "fetch_msg: alloc msg failed !!");
2445                 return NULL;
2446         }
2447         
2448         FD_ZERO(&rdfs);
2449         FD_SET(midev,&rdfs);
2450   
2451         mISDN_select(FD_SETSIZE, &rdfs, NULL, NULL, NULL);
2452   
2453         if (FD_ISSET(midev, &rdfs)) {
2454     
2455                 r=mISDN_read(midev,msg->data,MAX_MSG_SIZE,0);
2456                 msg->len=r;
2457     
2458                 if (r==0) {
2459                         free_msg(msg); /* danger, cauz usualy freeing in main_loop */
2460                         printf ("Got empty Msg?\n");
2461                         return NULL;
2462                 }
2463
2464                 return msg;
2465         } else {
2466                 printf ("Select timeout\n");
2467         }
2468   
2469         return NULL;
2470 }
2471
2472
2473 static void misdn_lib_isdn_event_catcher(void *arg)
2474 {
2475         struct misdn_lib *mgr = arg;
2476         int zero_frm=0 , fff_frm=0 ;
2477         int midev= mgr->midev;
2478         int port=0;
2479         
2480         while (1) {
2481                 msg_t *msg = fetch_msg(midev); 
2482                 iframe_t *frm;
2483                 
2484                 
2485                 if (!msg) continue;
2486                 
2487                 frm = (iframe_t*) msg->data;
2488                 
2489                 /** When we make a call from NT2Ast we get this frames **/
2490                 if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
2491                         zero_frm++; 
2492                         free_msg(msg);
2493                         continue;
2494                 } else {
2495                         if (zero_frm) {
2496                                 cb_log(-1, port, "*** Alert: %d zero_frms caught\n", zero_frm);
2497                                 zero_frm = 0 ;
2498                         }
2499                 }
2500                 
2501                 /** I get this sometimes after setup_bc **/
2502                 if (frm->len == 0 &&  frm->dinfo == 0 && frm->prim == 0xffffffff ) {
2503                         fff_frm++; 
2504                         free_msg(msg);
2505                         continue;
2506                 } else {
2507                         if (fff_frm) {
2508                                 cb_log(-1, port, "*** Alert: %d fff_frms caught\n", fff_frm);
2509                                 fff_frm = 0 ;
2510                         }
2511                 }
2512                 
2513                 manager_isdn_handler(frm, msg);
2514         }
2515
2516 }
2517
2518
2519 /** App Interface **/
2520
2521 int te_lib_init() {
2522         char buff[1025];
2523         iframe_t *frm=(iframe_t*)buff;
2524         int midev=mISDN_open();
2525         int ret;
2526
2527         memset(buff,0,1025);
2528   
2529         if  (midev<=0) return midev;
2530   
2531 /* create entity for layer 3 TE-mode */
2532         mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
2533         ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
2534   
2535         if (ret < mISDN_HEADER_LEN) {
2536         noentity:
2537                 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n",strerror(errno));
2538                 exit(-1);
2539         }
2540   
2541         entity = frm->dinfo & 0xffff ;
2542   
2543         if (!entity)
2544                 goto noentity;
2545
2546         return midev;
2547   
2548 }
2549
2550 void te_lib_destroy(int midev)
2551 {
2552         char buf[1024];
2553         mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
2554
2555         cb_log(4, 0, "Entetity deleted\n");
2556         mISDN_close(midev);
2557         cb_log(4, 0, "midev closed\n");
2558 }
2559
2560
2561
2562 void misdn_lib_transfer(struct misdn_bchannel* holded_bc)
2563 {
2564         holded_bc->holded=0;
2565 }
2566
2567 struct misdn_bchannel *manager_find_bc_by_pid(int pid)
2568 {
2569         struct misdn_stack *stack;
2570         int i;
2571   
2572         for (stack=glob_mgr->stack_list;
2573              stack;
2574              stack=stack->next) {
2575                 for (i=0; i<stack->b_num; i++)
2576                         if (stack->bc[i].pid == pid) return &stack->bc[i];
2577         }
2578   
2579         return NULL;
2580 }
2581
2582 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc)
2583 {
2584         struct misdn_stack *stack=get_stack_by_bc(bc);
2585         return find_bc_holded(stack);
2586 }
2587
2588
2589
2590 struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel)
2591 {
2592         struct misdn_stack *stack;
2593         int i;
2594         
2595         if (channel < 0 || channel > MAX_BCHANS)
2596                 return NULL;
2597
2598         for (stack=glob_mgr->stack_list; stack; stack=stack->next) {
2599     
2600                 if (stack->port == port) {
2601                         if (channel > 0) {
2602                                 if (channel <= stack->b_num) {
2603                                         for (i = 0; i < stack->b_num; i++) {
2604                                                 if (stack->bc[i].in_use && stack->bc[i].channel == channel) {
2605                                                         return NULL;
2606                                                 }
2607                                         }
2608                                 } else
2609                                         return NULL;
2610                         }
2611                         for (i = 0; i < stack->b_num; i++) {
2612                                 if (!stack->bc[i].in_use) {
2613                                         stack->bc[i].channel = channel;
2614                                         stack->bc[i].channel_preselected = channel?1:0;
2615                                         stack->bc[i].in_use = 1;
2616                                         return &stack->bc[i];
2617                                 }
2618                         }
2619                         return NULL;
2620                 }
2621         }
2622         return NULL;
2623 }
2624
2625
2626 char *fac2str (enum facility_type type) {
2627         struct arr_el { 
2628                 enum facility_type p; 
2629                 char *s ; 
2630         } arr[] = {
2631                 { FACILITY_NONE, "FAC_NONE" },
2632                 { FACILITY_CALLDEFLECT, "FAC_CALLDEFLECT"},
2633                 { FACILITY_CENTREX, "FAC_CENTREX"}
2634         };
2635         
2636         int i;
2637         
2638         for (i=0; i < sizeof(arr)/sizeof( struct arr_el) ; i ++)
2639                 if ( arr[i].p==type) return arr[i].s;
2640         
2641         return "FAC_UNKNOWN";
2642 }
2643
2644 void misdn_lib_log_ies(struct misdn_bchannel *bc)
2645 {
2646         if (!bc) return;
2647
2648         struct misdn_stack *stack=get_stack_by_bc(bc);
2649
2650         if (!stack) return;
2651         
2652
2653         cb_log(2, stack->port, " --> mode:%s cause:%d ocause:%d rad:%s\n", stack->nt?"NT":"TE", bc->cause, bc->out_cause, bc->rad);
2654         
2655         cb_log(3, stack->port, " --> facility:%s out_facility:%s\n",fac2str(bc->fac_type),fac2str(bc->out_fac_type));
2656         
2657         cb_log(2, stack->port,
2658                " --> info_dad:%s onumplan:%c dnumplan:%c rnumplan:%c\n",
2659                bc->info_dad,
2660                bc->onumplan>=0?'0'+bc->onumplan:' ',
2661                bc->dnumplan>=0?'0'+bc->dnumplan:' ',
2662                bc->rnumplan>=0?'0'+bc->rnumplan:' '
2663                 );
2664         cb_log(3, stack->port, " --> screen:%d --> pres:%d\n",
2665                         bc->screen, bc->pres);
2666         
2667         cb_log(2, stack->port, " --> channel:%d caps:%s pi:%x keypad:%s\n", bc->channel, bearer2str(bc->capability),bc->progress_indicator, bc->keypad);
2668
2669         cb_log(3, stack->port, " --> urate:%d rate:%d mode:%d user1:%d\n", bc->urate, bc->rate, bc->mode,bc->user1);
2670         
2671         cb_log(3, stack->port, " --> pid:%d addr:%x l3id:%x\n", bc->pid, bc->addr, bc->l3_id);
2672         cb_log(3, stack->port, " --> b_stid:%x layer_id:%x\n", bc->b_stid, bc->layer_id);
2673         
2674         cb_log(4, stack->port, " --> bc:%x h:%d sh:%d\n", bc, bc->holded, bc->stack_holder);
2675 }
2676
2677 int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
2678 {
2679         msg_t *msg; 
2680         int err = -1 ;
2681   
2682         if (!bc) goto ERR; 
2683
2684         
2685         struct misdn_stack *stack=get_stack_by_bc(bc);
2686
2687
2688         cb_log(0,0,"SENDEVENT: stack->nt:%d stack->uperid:%x\n",stack->nt, stack->upper_id);
2689
2690         if ( stack->nt && !stack->l1link) {
2691                 /** Queue Event **/
2692                 bc->evq=event;
2693                 cb_log(1, stack->port, "Queueing Event %s because L1 is down (btw. Activating L1)\n", isdn_get_info(msgs_g, event, 0));
2694                 misdn_lib_get_l1_up(stack);
2695                 return 0;
2696         }
2697         
2698         cb_log(1, stack->port, "I SEND:%s oad:%s dad:%s \n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad);
2699         misdn_lib_log_ies(bc);
2700         
2701         switch (event) {
2702         case EVENT_SETUP:
2703                 if (create_process(glob_mgr->midev, bc)<0) {
2704                         cb_log(-1,  stack->port, " No free channel at the moment @ send_event\n");
2705
2706                         err=-ENOCHAN;
2707                         goto ERR;
2708                 }
2709                 break;
2710
2711                 
2712
2713         case EVENT_CONNECT:
2714         case EVENT_PROGRESS:
2715         case EVENT_ALERTING:
2716         case EVENT_PROCEEDING:
2717         case EVENT_SETUP_ACKNOWLEDGE:
2718         case EVENT_RETRIEVE_ACKNOWLEDGE:
2719                 if (stack->nt) {
2720                         if (bc->channel <=0 ) { /*  else we have the channel already */
2721                                 bc->channel = find_free_chan_in_stack(stack, 0);
2722                                 if (!bc->channel) {
2723                                         cb_log(-1, stack->port, " No free channel at the moment\n");
2724                                         
2725                                         err=-ENOCHAN;
2726                                         goto ERR;
2727                                 }
2728                         }
2729                         /* Its that i generate channels */
2730                 }
2731                 
2732                 if ( bc->nt || misdn_cap_is_speech(bc->capability)) {
2733                         int ret=setup_bc(bc);
2734                         if (ret == -EINVAL){
2735                                 cb_log(-1,bc->port,"send_event: setup_bc failed\n");
2736                                 
2737                         }
2738                 } 
2739                 
2740                 
2741                 if ( (event == EVENT_CONNECT ) && misdn_cap_is_speech(bc->capability) ) {
2742                         if ( *bc->crypt_key ) {
2743                                 cb_log(4, stack->port,  " --> ENABLING BLOWFISH channel:%d oad%d:%s dad%d:%s \n", bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
2744                                 
2745                                 manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
2746                         }
2747                         
2748                         
2749                         if (!bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
2750                         
2751                         if (bc->ec_enable) manager_ec_enable(bc);
2752                         
2753                         if (bc->txgain != 0) {
2754                                 cb_log(4, stack->port,  "--> Changing txgain to %d\n", bc->txgain);
2755                                 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
2756                         }
2757                         
2758                         if ( bc->rxgain != 0 ) {
2759                                 cb_log(4, stack->port,  "--> Changing rxgain to %d\n", bc->rxgain);
2760                                 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
2761                         }
2762                         
2763                 }
2764                 
2765                 if (event == EVENT_RETRIEVE_ACKNOWLEDGE) {
2766                         manager_bchannel_activate(bc);
2767                 }
2768                 break;
2769
2770         case EVENT_HOLD_ACKNOWLEDGE:
2771         {
2772                 struct misdn_bchannel *holded_bc=malloc(sizeof(struct misdn_bchannel));
2773                 memcpy(holded_bc,bc,sizeof(struct misdn_bchannel));
2774                 holded_bc->holded=1;
2775                 stack_holder_add(stack,holded_bc);
2776                 
2777                 if (stack->nt) {
2778                         empty_chan_in_stack(stack,bc->channel);
2779                         empty_bc(bc);
2780                         clean_up_bc(bc);
2781                 }
2782                 
2783                 /** we set it up later at RETRIEVE_ACK again.**/
2784                 holded_bc->upset=0;
2785                 holded_bc->active=0;
2786                 
2787                 cb_event( EVENT_NEW_BC, holded_bc, glob_mgr->user_data);
2788         }
2789         break;
2790         
2791         case EVENT_RELEASE:
2792                 break;
2793                 
2794         case EVENT_RELEASE_COMPLETE:
2795                 /*we do the cleanup in EVENT_CLEANUP*/
2796                 break;
2797     
2798         case EVENT_CONNECT_ACKNOWLEDGE:
2799                 
2800                 if (misdn_cap_is_speech(bc->capability)) {
2801                         if (  !bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
2802                         if (bc->ec_enable) manager_ec_enable(bc);
2803                         if ( bc->txgain != 0 ) {
2804                                 cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
2805                                 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
2806                         }
2807                         if ( bc->rxgain != 0 ) {
2808                                 cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
2809                                 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
2810                         }
2811                 }
2812                 break;
2813     
2814         default:
2815                 break;
2816         }
2817   
2818         /* Later we should think about sending bchannel data directly to misdn. */
2819         msg = isdn_msg_build_event(msgs_g, bc, event, stack->nt);
2820         msg_queue_tail(&stack->downqueue, msg);
2821         sem_post(&glob_mgr->new_msg);
2822   
2823         return 0;
2824   
2825  ERR:
2826         return -1; 
2827 }
2828
2829
2830
2831 int manager_isdn_handler(iframe_t *frm ,msg_t *msg)
2832 {  
2833
2834         if (frm->dinfo==(signed long)0xffffffff && frm->prim==(PH_DATA|CONFIRM)) {
2835                 printf("SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
2836         }
2837
2838         if ( ((frm->addr | ISDN_PID_BCHANNEL_BIT )>> 28 ) == 0x5) {
2839                 cb_log(9,0,"BCHANNEL_BIT\n");
2840                 if (handle_bchan(msg)) 
2841                         return 0 ;
2842         } else {
2843                 cb_log(9,0,"NO BCHANNEL_BIT !\n");
2844         }
2845         
2846         if (handle_timers(msg)) 
2847                 return 0 ;
2848         
2849         if (handle_mgmt(msg)) 
2850                 return 0 ; 
2851         
2852         if (handle_l2(msg)) 
2853                 return 0 ;
2854
2855         /* Its important to handle l1 AFTER l2  */
2856         if (handle_l1(msg)) 
2857                 return 0 ;
2858         
2859         
2860         /** Handle L2/3 Signalling after bchans **/ 
2861         if (handle_frm_nt(msg)) 
2862                 return 0 ;
2863         
2864         if (handle_frm(msg)) 
2865                 return 0 ;
2866         
2867         cb_log(-1, 0, "Unhandled Message: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo);           
2868    
2869         free_msg(msg);
2870
2871         return 0;
2872 }
2873
2874
2875
2876
2877 int misdn_lib_get_port_info(int port)
2878 {
2879         msg_t *msg=alloc_msg(MAX_MSG_SIZE);
2880         iframe_t *frm;
2881         struct misdn_stack *stack=find_stack_by_port(port);
2882         if (!msg) {
2883                 cb_log(-1, port, "misgn_lib_get_port: alloc_msg failed!\n");
2884                 return -1;
2885         }
2886         frm=(iframe_t*)msg->data;
2887         if (!stack ) {
2888                 cb_log(-1, port, "There is no Stack for this port.\n");
2889                 return -1;
2890         }
2891         /* activate bchannel */
2892         frm->prim = CC_STATUS_ENQUIRY | REQUEST;
2893
2894         frm->addr = stack->upper_id| FLG_MSG_DOWN;
2895
2896         frm->dinfo = 0;
2897         frm->len = 0;
2898   
2899         msg_queue_tail(&glob_mgr->activatequeue, msg);
2900         sem_post(&glob_mgr->new_msg);
2901
2902   
2903         return 0; 
2904 }
2905
2906 int misdn_lib_port_restart(int port)
2907 {
2908         struct misdn_stack *stack=find_stack_by_port(port);
2909  
2910         cb_log(0, port, "Restarting this port.\n");
2911         if (stack) {
2912                 cb_log(0, port, "Stack:%p\n",stack);
2913
2914                 
2915                 clear_l3(stack);
2916                 
2917                 {
2918                         msg_t *msg=alloc_msg(MAX_MSG_SIZE);
2919                         iframe_t *frm;
2920
2921                         if (!msg) {
2922                                 cb_log(-1, port, "port_restart: alloc_msg failed\n");
2923                                 return -1;
2924                         }
2925                         
2926                         frm=(iframe_t*)msg->data;
2927                         /* we must activate if we are deactivated */
2928                         /* activate bchannel */
2929         
2930                         frm->prim = DL_RELEASE | REQUEST;
2931
2932                         frm->addr = stack->upper_id | FLG_MSG_DOWN;
2933
2934         
2935                         frm->dinfo = 0;
2936                         frm->len = 0;
2937                         msg_queue_tail(&glob_mgr->activatequeue, msg);
2938                         sem_post(&glob_mgr->new_msg);
2939                 }
2940                 return 0;
2941     
2942                 stack_te_destroy(stack);
2943       
2944                 {
2945                         struct misdn_stack *tmpstack;
2946                         struct misdn_stack *newstack=stack_init(stack->midev ,port, stack->ptp);
2947       
2948       
2949                         if (stack == glob_mgr->stack_list) {
2950                                 struct misdn_stack *n=glob_mgr->stack_list->next;
2951                                 glob_mgr->stack_list = newstack ;
2952                                 glob_mgr->stack_list->next = n;
2953                         } else {
2954                                 for (tmpstack=glob_mgr->stack_list;
2955                                      tmpstack->next;
2956                                      tmpstack=tmpstack->next) 
2957                                         if (tmpstack->next == stack) break;
2958
2959                                 if (!tmpstack->next) {
2960                                         cb_log(-1, port, "Stack to restart not found\n");
2961                                         return 0;
2962                                 }  else {
2963                                         struct misdn_stack *n=tmpstack->next->next;
2964                                         tmpstack->next=newstack;
2965                                         newstack->next=n;
2966                                 }
2967                         }
2968       
2969                         {
2970                                 int i;
2971                                 for(i=0;i<newstack->b_num; i++) {
2972                                         int r;
2973                                         if ((r=init_bc(newstack, &newstack->bc[i], newstack->midev,port,i, "", 1))<0) {
2974                                                 cb_log(-1, port, "Got Err @ init_bc :%d\n",r);
2975                                                 return 0;
2976                                         }
2977                                 }
2978                         }
2979       
2980                         free(stack);
2981                 }
2982         }
2983
2984         return 0;
2985 }
2986
2987
2988
2989 sem_t handler_started; 
2990
2991 void manager_event_handler(void *arg)
2992 {
2993         sem_post(&handler_started); 
2994         while (1) {
2995                 struct misdn_stack *stack;
2996                 msg_t *msg;
2997     
2998                 /** wait for events **/
2999                 sem_wait(&glob_mgr->new_msg);
3000     
3001                 for (msg=msg_dequeue(&glob_mgr->activatequeue);
3002                      msg;
3003                      msg=msg_dequeue(&glob_mgr->activatequeue)
3004                         )
3005                 {
3006         
3007                         iframe_t *frm =  (iframe_t*) msg->data ;
3008
3009                         switch ( frm->prim) {
3010                         case MGR_SETSTACK | REQUEST :
3011                                 break;
3012                         default:
3013                                 mISDN_write(glob_mgr->midev, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
3014                                 free_msg(msg);
3015                         }
3016                 }
3017
3018                 for (stack=glob_mgr->stack_list;
3019                      stack;
3020                      stack=stack->next ) { 
3021                         while ( (msg=msg_dequeue(&stack->downqueue)) ) {
3022                                 if (stack->nt ) {
3023                                         if (stack->nst.manager_l3(&stack->nst, msg))
3024                                                 cb_log(-1, stack->port, "Error@ Sending Message in NT-Stack.\n");
3025           
3026                                 } else {
3027                                         iframe_t *frm = (iframe_t *)msg->data;
3028                                         struct misdn_bchannel *bc = find_bc_by_l3id(stack, frm->dinfo);
3029                                         if (bc) send_msg(glob_mgr->midev, bc, msg);
3030                                 }
3031                         }
3032                 }
3033         }
3034 }
3035
3036
3037 int misdn_lib_maxports_get() { /** BE AWARE WE HAVE NO CB_LOG HERE! **/
3038         
3039         int i = mISDN_open();
3040         int max=0;
3041         
3042         if (i<0)
3043                 return -1;
3044
3045         max = mISDN_get_stack_count(i);
3046         
3047         mISDN_close(i);
3048         
3049         return max;
3050 }
3051
3052 int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_data)
3053 {
3054         struct misdn_lib *mgr=calloc(1, sizeof(struct misdn_lib));
3055         char *tok, *tokb;
3056         char plist[1024];
3057         int midev;
3058         int port_count=0;
3059  
3060         cb_log = iface->cb_log;
3061         cb_event = iface->cb_event;
3062         cb_jb_empty = iface->cb_jb_empty;
3063         
3064         glob_mgr = mgr;
3065   
3066         msg_init();
3067         debug_init(0 , NULL, NULL, NULL);
3068 #if 0
3069         int flags=0xff;
3070         flags &= ~DBGM_MSG;
3071         debug_init( flags , NULL, NULL, NULL);
3072 #endif  
3073         if (!portlist || (*portlist == 0) ) return 1;
3074         
3075         init_flip_bits();
3076         
3077         {
3078                 strncpy(plist,portlist, 1024);
3079                 plist[1023] = 0;
3080         }
3081   
3082         memcpy(tone_425_flip,tone_425,TONE_425_SIZE);
3083         flip_buf_bits(tone_425_flip,TONE_425_SIZE);
3084
3085         memcpy(tone_silence_flip,tone_SILENCE,TONE_SILENCE_SIZE);
3086         flip_buf_bits(tone_silence_flip,TONE_SILENCE_SIZE);
3087   
3088         midev=te_lib_init();
3089         mgr->midev=midev;
3090
3091         port_count=mISDN_get_stack_count(midev);
3092   
3093         msg_queue_init(&mgr->activatequeue);
3094   
3095         if (sem_init(&mgr->new_msg, 1, 0)<0)
3096                 sem_init(&mgr->new_msg, 0, 0);
3097   
3098         for (tok=strtok_r(plist," ,",&tokb );
3099              tok; 
3100              tok=strtok_r(NULL," ,",&tokb)) {
3101                 int port = atoi(tok);
3102                 struct misdn_stack *stack;
3103                 static int first=1;
3104                 int ptp=0;
3105     
3106                 if (strstr(tok, "ptp"))
3107                         ptp=1;
3108
3109                 if (port > port_count) {
3110                         cb_log(-1, port, "Couldn't Initialize this port since we have only %d ports\n", port_count);
3111                         exit(1);
3112                 }
3113                 stack=stack_init(midev, port, ptp);
3114     
3115     
3116     
3117                 if (!stack) {
3118                         perror("init_stack");
3119                         exit(1);
3120                 }
3121     
3122                 if (stack && first) {
3123                         mgr->stack_list=stack;
3124                         first=0;
3125                         {
3126                                 int i;
3127                                 for(i=0;i<stack->b_num; i++) {
3128                                         int r;
3129                                         if ((r=init_bc(stack, &stack->bc[i], stack->midev,port,i, "", 1))<0) {
3130                                                 cb_log(-1, port, "Got Err @ init_bc :%d\n",r);
3131                                                 exit(1);
3132                                         }
3133                                 }
3134                         }
3135       
3136                         continue;
3137                 }
3138     
3139                 if (stack) {
3140                         struct misdn_stack * help;
3141                         for ( help=mgr->stack_list; help; help=help->next ) 
3142                                 if (help->next == NULL) break;
3143       
3144       
3145                         help->next=stack;
3146
3147                         {
3148                                 int i;
3149                                 for(i=0;i<stack->b_num; i++) {
3150                                         int r;
3151                                         if ((r=init_bc(stack, &stack->bc[i], stack->midev,port,i, "",1 ))<0) {
3152                                                 cb_log(-1, port, "Got Err @ init_bc :%d\n",r);
3153                                                 exit(1);
3154                                         } 
3155                                 }
3156                         }
3157                 }
3158     
3159         }
3160   
3161         if (sem_init(&handler_started, 1, 0)<0)
3162                 sem_init(&handler_started, 0, 0);
3163   
3164         cb_log(4, 0, "Starting Event Handler\n");
3165         pthread_create( &mgr->event_handler_thread, NULL,(void*)manager_event_handler, mgr);
3166   
3167         sem_wait(&handler_started) ;
3168         cb_log(4, 0, "Starting Event Catcher\n");
3169         pthread_create( &mgr->event_thread, NULL, (void*)misdn_lib_isdn_event_catcher, mgr);
3170   
3171         cb_log(4, 0, "Event Catcher started\n");
3172   
3173         global_state= MISDN_INITIALIZED; 
3174   
3175         return (mgr == NULL);
3176 }
3177
3178 void misdn_lib_destroy()
3179 {
3180         struct misdn_stack *help;
3181         int i;
3182   
3183         for ( help=glob_mgr->stack_list; help; help=help->next ) {
3184                 for(i=0;i<help->b_num; i++) {
3185                         char buf[1024];
3186                         mISDN_write_frame(help->midev, buf, help->bc[i].addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
3187                         help->bc[i].addr = 0;
3188                 }
3189                 cb_log (1, help->port, "Destroying this port.\n");
3190                 stack_te_destroy(help);
3191         }
3192         
3193         if (global_state == MISDN_INITIALIZED) {
3194                 cb_log(4, 0, "Killing Handler Thread\n");
3195                 if ( pthread_cancel(glob_mgr->event_handler_thread) == 0 ) {
3196                         cb_log(4, 0, "Joining Handler Thread\n");
3197                         pthread_join(glob_mgr->event_handler_thread, NULL);
3198                 }
3199           
3200                 cb_log(4, 0, "Killing Main Thread\n");
3201                 if ( pthread_cancel(glob_mgr->event_thread) == 0 ) {
3202                         cb_log(4, 0, "Joining Main Thread\n");
3203                         pthread_join(glob_mgr->event_thread, NULL);
3204                 }
3205         }
3206   
3207         cb_log(1, 0, "Closing mISDN device\n");
3208         te_lib_destroy(glob_mgr->midev);
3209 }
3210
3211 char *manager_isdn_get_info(enum event_e event)
3212 {
3213         return isdn_get_info(msgs_g , event, 0);
3214 }
3215
3216 void manager_bchannel_activate(struct misdn_bchannel *bc)
3217 {
3218         msg_t *msg=alloc_msg(MAX_MSG_SIZE);
3219         iframe_t *frm;
3220         
3221         struct misdn_stack *stack=get_stack_by_bc(bc);
3222
3223         if (!stack) {
3224                 cb_log(-1, stack->port, "bchannel_activate: Stack not found !");
3225         }
3226         
3227         if (!msg) {
3228                 cb_log(-1, stack->port, "bchannel_activate: alloc_msg failed !");
3229
3230                 return ;
3231         }
3232
3233         if (bc->upset != 2 ) {
3234                 cb_log(1, stack->port, "bchannel_activate: BC Not properly upsetted addr:%x\n", bc->addr);
3235                 return;
3236         }
3237         
3238         
3239         frm=(iframe_t*)msg->data;
3240         /* we must activate if we are deactivated */
3241         clear_ibuffer(bc->astbuf);
3242         
3243         if (bc->active) return;
3244   
3245         cb_log(5, stack->port, "$$$ Bchan Activated addr %x\n", bc->addr);
3246
3247         /* activate bchannel */
3248         frm->prim = DL_ESTABLISH | REQUEST;
3249         frm->addr = bc->addr | FLG_MSG_DOWN ;
3250         frm->dinfo = 0;
3251         frm->len = 0;
3252         
3253         msg_queue_tail(&glob_mgr->activatequeue, msg);
3254         sem_post(&glob_mgr->new_msg);
3255
3256         bc->active=1;
3257   
3258         return ;
3259   
3260 }
3261
3262
3263 void manager_bchannel_deactivate(struct misdn_bchannel * bc)
3264 {
3265         iframe_t dact;
3266
3267         struct misdn_stack *stack=get_stack_by_bc(bc);
3268         if (!bc->active) return;
3269   
3270         cb_log(5, stack->port, "$$$ Bchan deActivated addr %x\n", bc->addr);
3271   
3272         bc->generate_tone=0;
3273         
3274         dact.prim = DL_RELEASE | REQUEST;
3275         dact.addr = bc->addr | FLG_MSG_DOWN;
3276         dact.dinfo = 0;
3277         dact.len = 0;
3278   
3279         mISDN_write(stack->midev, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
3280         clear_ibuffer(bc->astbuf);
3281         bc->active=0;
3282   
3283         return;
3284 }
3285
3286
3287 int misdn_lib_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len)
3288 {
3289         struct misdn_stack *stack=get_stack_by_bc(bc);
3290         if (!bc->active) return -1;   
3291         
3292         char buf[4096 + mISDN_HEADER_LEN];
3293         iframe_t *frm= (iframe_t*)buf;
3294         int  r;
3295         
3296         frm->prim = DL_DATA|REQUEST;
3297         frm->dinfo = 0;
3298         frm->addr = bc->addr | FLG_MSG_DOWN ;
3299         
3300         frm->len = len;
3301         memcpy(&buf[mISDN_HEADER_LEN], data,len);
3302         
3303         if ( ! misdn_cap_is_speech(bc->capability))
3304                 cb_log(6, stack->port, "Writing %d data bytes\n",len);
3305         
3306         cb_log(9, stack->port, "Writing %d bytes 2 mISDN\n",len);
3307         r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT);
3308 #ifdef ACK_HDLC
3309         if (bc->hdlc && bc->ack_hdlc) {
3310                 cb_log(4,stack->port,"Awaiting Acknowledge [%d]\n",len);
3311                 sem_wait((sem_t*)bc->ack_hdlc);
3312                 cb_log(4,stack->port,"Acknowledged\n");
3313         }
3314 #endif  
3315         return 0;
3316 }
3317
3318
3319
3320 /*
3321  * send control information to the channel (dsp-module)
3322  */
3323 void manager_ph_control(struct misdn_bchannel *bc, int c1, int c2)
3324 {
3325         unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
3326         iframe_t *ctrl = (iframe_t *)buffer; /* preload data */
3327         unsigned long *d = (unsigned long *)&ctrl->data.p;
3328         struct misdn_stack *stack=get_stack_by_bc(bc);
3329         
3330         ctrl->prim = PH_CONTROL | REQUEST;
3331         ctrl->addr = bc->addr | FLG_MSG_DOWN;
3332         ctrl->dinfo = 0;
3333         ctrl->len = sizeof(unsigned long)*2;
3334         *d++ = c1;
3335         *d++ = c2;
3336         mISDN_write(stack->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
3337 }
3338
3339 /*
3340  * send control information to the channel (dsp-module)
3341  */
3342 void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len)
3343 {
3344         unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
3345         iframe_t *ctrl = (iframe_t *)buffer;
3346         unsigned long *d = (unsigned long *)&ctrl->data.p;
3347         struct misdn_stack *stack=get_stack_by_bc(bc);
3348         
3349         ctrl->prim = PH_CONTROL | REQUEST;
3350         ctrl->addr = bc->addr | FLG_MSG_DOWN;
3351         ctrl->dinfo = 0;
3352         ctrl->len = sizeof(unsigned long) + c2_len;
3353         *d++ = c1;
3354         memcpy(d, c2, c2_len);
3355         mISDN_write(stack->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
3356 }
3357
3358
3359
3360
3361 void manager_clean_bc(struct misdn_bchannel *bc )
3362 {
3363         struct misdn_stack *stack=get_stack_by_bc(bc);
3364         
3365         if (bc->state == STATE_CONNECTED)
3366                 misdn_lib_send_event(bc,EVENT_DISCONNECT);
3367
3368         empty_chan_in_stack(stack, bc->channel);
3369         empty_bc(bc);
3370   
3371         misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
3372 }
3373
3374
3375 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder)
3376 {
3377         struct misdn_bchannel *help;
3378         cb_log(4,stack->port, "*HOLDER: add %x\n",holder->l3_id);
3379         
3380         holder->stack_holder=1;
3381
3382         if (!stack ) return ;
3383         
3384         holder->next=NULL;
3385         
3386         if (!stack->holding) {
3387                 stack->holding = holder;
3388                 return;
3389         }
3390   
3391         for (help=stack->holding;
3392              help;
3393              help=help->next) {
3394                 if (!help->next) {
3395                         help->next=holder;
3396                         break;
3397                 }
3398         }
3399   
3400 }
3401
3402 void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder)
3403 {
3404         struct misdn_bchannel *h1;
3405
3406         if (!holder->stack_holder) return;
3407         
3408         cb_log(4,stack->port, "*HOLDER: remove %x\n",holder->l3_id);
3409         if (!stack || ! stack->holding) return;
3410   
3411         if (holder == stack->holding) {
3412                 stack->holding = stack->holding->next;
3413                 return;
3414         }
3415         
3416         for (h1=stack->holding;
3417              h1;
3418              h1=h1->next) {
3419                 if (h1->next == holder) {
3420                         h1->next=h1->next->next;
3421                         return ;
3422                 }
3423         }
3424 }
3425
3426
3427 struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id)
3428 {
3429         struct misdn_bchannel *help;
3430
3431         cb_log(4,stack?stack->port:0, "*HOLDER: find %x\n",l3id);
3432         
3433         if (!stack) return NULL;
3434         
3435         for (help=stack->holding;
3436              help;
3437              help=help->next) {
3438                 if (help->l3_id == l3id) {
3439                         cb_log(4,stack->port, "*HOLDER: found bc\n");
3440                         return help;
3441                 }
3442         }
3443
3444         cb_log(4,stack->port, "*HOLDER: find nothing\n");
3445         return NULL;
3446 }
3447
3448
3449
3450 void manager_ec_enable(struct misdn_bchannel *bc)
3451 {
3452         int ec_arr[2];
3453
3454         struct misdn_stack *stack=get_stack_by_bc(bc);
3455         
3456         cb_log(1, stack?stack->port:0,"Sending Control ECHOCAN_ON taps:%d training:%d\n",bc->ec_deftaps, bc->ec_training);
3457         
3458         switch (bc->ec_deftaps) {
3459         case 4:
3460         case 8:
3461         case 16:
3462         case 32:
3463         case 64:
3464         case 128:
3465         case 256:
3466         case 512:
3467         case 1024:
3468                 cb_log(4, stack->port, "Taps is %d\n",bc->ec_deftaps);
3469                 break;
3470         default:
3471                 cb_log(-1, stack->port, "Taps should be power of 2\n");
3472                 bc->ec_deftaps=128;
3473         }
3474
3475         ec_arr[0]=bc->ec_deftaps;
3476         ec_arr[1]=bc->ec_training;
3477         
3478         manager_ph_control_block(bc,  ECHOCAN_ON,  ec_arr, sizeof(ec_arr));
3479 }
3480
3481
3482
3483 void manager_ec_disable(struct misdn_bchannel *bc)
3484 {
3485         struct misdn_stack *stack=get_stack_by_bc(bc);
3486         
3487         cb_log(1, stack?stack->port:0, "Sending Control ECHOCAN_OFF\n");
3488         manager_ph_control(bc,  ECHOCAN_OFF, 0);
3489 }
3490
3491 struct misdn_stack* get_misdn_stack() {
3492         return glob_mgr->stack_list;
3493 }
3494
3495
3496
3497 void misdn_lib_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2) {
3498         manager_ph_control(bc1, CMX_RECEIVE_OFF, 0);
3499         manager_ph_control(bc2, CMX_RECEIVE_OFF, 0);
3500         
3501         manager_ph_control(bc1, CMX_CONF_JOIN, (bc1->pid<<1) +1);
3502         manager_ph_control(bc2, CMX_CONF_JOIN, (bc1->pid<<1) +1);
3503 }
3504
3505 void misdn_lib_split_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2)
3506 {
3507         
3508         manager_ph_control(bc1, CMX_RECEIVE_ON, 0) ;
3509         manager_ph_control(bc2, CMX_RECEIVE_ON, 0);
3510         
3511         manager_ph_control(bc1, CMX_CONF_SPLIT, (bc1->pid<<1) +1);
3512         manager_ph_control(bc2, CMX_CONF_SPLIT, (bc1->pid<<1) +1);
3513         
3514 }