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