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