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