8c94c5a2e71fbd8b2d0900726254dfbfe68d197d
[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
197 extern struct isdn_msg msgs_g[]; 
198
199 #define ISDN_PID_L3_B_USER 0x430000ff
200 #define ISDN_PID_L4_B_USER 0x440000ff
201
202 /* #define MISDN_IBUF_SIZE 1024 */
203 #define MISDN_IBUF_SIZE 512
204
205 /*  Fine Tuning of Inband  Signalling time */
206 #define TONE_ALERT_CNT 41 /*  1 Sec  */
207 #define TONE_ALERT_SILENCE_CNT 200 /*  4 Sec */
208
209 #define TONE_BUSY_CNT 20 /*  ? */
210 #define TONE_BUSY_SILENCE_CNT 48 /*  ? */
211
212 static int  entity;
213
214 static struct misdn_lib *glob_mgr;
215
216 char tone_425_flip[TONE_425_SIZE];
217 char tone_silence_flip[TONE_SILENCE_SIZE];
218
219 static void misdn_lib_isdn_event_catcher(void *arg);
220 static int handle_event_nt(void *dat, void *arg);
221
222
223 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder);
224 void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder);
225 struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id);
226
227 /* from isdn_lib.h */
228 int init_bc(struct misdn_stack * stack,  struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime);
229 struct misdn_stack* stack_init(int midev,  int port, int ptp);
230 void stack_destroy(struct misdn_stack* stack);
231         /* user iface */
232 int te_lib_init( void ) ; /* returns midev */
233 void te_lib_destroy(int midev) ;
234 struct misdn_bchannel *manager_find_bc_by_pid(int pid);
235 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc);
236 void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len);
237 void manager_clean_bc(struct misdn_bchannel *bc );
238 void manager_bchannel_setup (struct misdn_bchannel *bc);
239 void manager_bchannel_cleanup (struct misdn_bchannel *bc);
240
241 void ec_chunk( struct misdn_bchannel *bc, unsigned char *rxchunk, unsigned char *txchunk, int chunk_size);
242         /* end */
243 int bchdev_echocancel_activate(struct misdn_bchannel* dev);
244 void bchdev_echocancel_deactivate(struct misdn_bchannel* dev);
245 /* end */
246
247
248 static char *bearer2str(int cap) {
249         static char *bearers[]={
250                 "Speech",
251                 "Audio 3.1k",
252                 "Unres Digital",
253                 "Res Digital",
254                 "Unknown Bearer"
255         };
256         
257         switch (cap) {
258         case INFO_CAPABILITY_SPEECH:
259                 return bearers[0];
260                 break;
261         case INFO_CAPABILITY_AUDIO_3_1K:
262                 return bearers[1];
263                 break;
264         case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
265                 return bearers[2];
266                 break;
267         case INFO_CAPABILITY_DIGITAL_RESTRICTED:
268                 return bearers[3];
269                 break;
270         default:
271                 return bearers[4];
272                 break;
273         }
274 }
275
276
277 static char flip_table[256];
278
279 static void init_flip_bits(void)
280 {
281         int i,k;
282         
283         for (i = 0 ; i < 256 ; i++) {
284                 unsigned char sample = 0 ;
285                 for (k = 0; k<8; k++) {
286                         if ( i & 1 << k ) sample |= 0x80 >>  k;
287                 }
288                 flip_table[i] = sample;
289         }
290 }
291
292 static char * flip_buf_bits ( char * buf , int len)
293 {
294         int i;
295         char * start = buf;
296         
297         for (i = 0 ; i < len; i++) {
298                 buf[i] = flip_table[(unsigned char)buf[i]];
299         }
300         
301         return start;
302 }
303
304
305
306
307 static msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */
308 {
309         int i = 0;
310         msg_t *dmsg;
311   
312         while(i < 10)
313         {
314                 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
315                 if (dmsg)
316                         return(dmsg);
317       
318                 if (!i)
319                         printf("cannot allocate memory, trying again...\n");
320                 i++;
321                 usleep(300000);
322         }
323         printf("cannot allocate memory, system overloaded.\n");
324         exit(-1);
325 }
326
327
328
329 msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
330 {
331         int i = 0;
332         msg_t *dmsg;
333         Q931_info_t *qi;
334         iframe_t *frm;
335   
336         if (!ntmode)
337                 size = sizeof(Q931_info_t)+2;
338   
339         while(i < 10) {
340                 if (ntmode) {
341                         dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
342                         if (dmsg) {
343                                 return(dmsg);
344                         }
345                 } else {
346                         dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
347                         if (dmsg)
348                         {
349                                 memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
350                                 frm = (iframe_t *)dmsg->data;
351                                 frm->prim = prim;
352                                 frm->dinfo = dinfo;
353                                 qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
354                                 qi->type = mt;
355                                 return(dmsg);
356                         }
357                 }
358     
359                 if (!i) printf("cannot allocate memory, trying again...\n");
360                 i++;
361                 usleep(300000);
362         }
363         printf("cannot allocate memory, system overloaded.\n");
364         exit(-1);
365 }
366
367
368 static int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
369 {
370         iframe_t *frm;
371         frm = (iframe_t *)dmsg->data;
372         struct misdn_stack *stack=get_stack_by_bc(bc);
373
374         if (!stack) {
375                 cb_log(0,bc->port,"send_msg: IEK!! no stack\n ");
376                 return -1;
377         }
378         
379         frm->addr = (stack->upper_id | FLG_MSG_DOWN);
380         frm->dinfo = bc->l3_id;
381         frm->len = (dmsg->len) - mISDN_HEADER_LEN;
382                                 
383         cb_log(4,stack->port,"Sending msg, prim:%x addr:%x dinfo:%x\n",frm->prim,frm->addr,frm->dinfo);
384
385         mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC);
386         free_msg(dmsg);
387
388         return 0;
389 }
390
391
392 static int mypid=1;
393
394
395 int misdn_cap_is_speech(int cap)
396 /** Poor mans version **/
397 {
398         if ( (cap != INFO_CAPABILITY_DIGITAL_UNRESTRICTED) &&
399              (cap != INFO_CAPABILITY_DIGITAL_RESTRICTED) ) return 1;
400         return 0;
401 }
402
403 int misdn_inband_avail(struct misdn_bchannel *bc)
404 {
405
406         /*if ! early_bconnect we have never inband available*/
407         if ( ! bc->early_bconnect ) return 0;
408         
409         switch (bc->progress_indicator) {
410         case INFO_PI_INBAND_AVAILABLE:
411         case INFO_PI_CALL_NOT_E2E_ISDN:
412         case INFO_PI_CALLED_NOT_ISDN:
413                 return 1;
414         default:
415                 return 0;
416         }
417         return 0;
418 }
419
420
421 static void dump_chan_list(struct misdn_stack *stack)
422 {
423         int i;
424
425         for (i=0; i <= stack->b_num; i++) {
426                 cb_log(6, stack->port, "Idx:%d stack->cchan:%d Chan:%d\n",i,stack->channels[i], i+1);
427         }
428 }
429
430
431
432
433 static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchannel *bc, int channel)
434 {
435         int i;
436
437         cb_log(1,stack->port,"find_free_chan: req_chan:%d\n",channel);
438
439         if (channel < 0 || channel > MAX_BCHANS) {
440                 cb_log(4, stack->port, " !! out of bound call to find_free_chan_in_stack! (ch:%d)\n", channel);
441                 return 0;
442         }
443         
444         channel--;
445   
446         for (i = 0; i < stack->b_num; i++) {
447                 if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 Dchannel ;) and work with chan preselection */
448                         if (!stack->channels[i]) {
449                                 cb_log (1, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1);
450                                 stack->channels[i] = 1;
451                                 bc->channel=i+1;
452                                 cb_event(EVENT_NEW_CHANNEL, bc, NULL);
453                                 return i+1;
454                         }
455                 }
456         }
457
458         cb_log (4, stack->port, " !! NO FREE CHAN IN STACK\n");
459         dump_chan_list(stack);
460   
461         return 0;
462 }
463
464 static int empty_chan_in_stack(struct misdn_stack *stack, int channel)
465 {
466         if (channel<=0) {
467                 cb_log(0,stack?stack->port:0, "empty_chan_inst_stack: cannot empty channel %d\n",channel);
468                 return -1;
469         }
470         
471         cb_log (4, stack?stack->port:0, "empty_chan_in_stack: %d\n",channel); 
472         stack->channels[channel-1] = 0;
473         dump_chan_list(stack);
474         return 0;
475 }
476
477 char *bc_state2str(enum bchannel_state state) {
478         int i;
479         
480         struct bchan_state_s {
481                 char *n;
482                 enum bchannel_state s;
483         } states[] = {
484                 {"BCHAN_CLEANED", BCHAN_CLEANED },
485                 {"BCHAN_EMPTY", BCHAN_EMPTY},
486                 {"BCHAN_SETUP", BCHAN_SETUP},
487                 {"BCHAN_SETUPED", BCHAN_SETUPED},
488                 {"BCHAN_ACTIVE", BCHAN_ACTIVE},
489                 {"BCHAN_ACTIVATED", BCHAN_ACTIVATED},
490                 {"BCHAN_BRIDGE",  BCHAN_BRIDGE},
491                 {"BCHAN_BRIDGED", BCHAN_BRIDGED},
492                 {"BCHAN_RELEASE", BCHAN_RELEASE},
493                 {"BCHAN_RELEASED", BCHAN_RELEASED},
494                 {"BCHAN_CLEAN", BCHAN_CLEAN},
495                 {"BCHAN_CLEAN_REQUEST", BCHAN_CLEAN_REQUEST},
496                 {"BCHAN_ERROR", BCHAN_ERROR}
497         };
498         
499         for (i=0; i< sizeof(states)/sizeof(struct bchan_state_s); i++)
500                 if ( states[i].s == state)
501                         return states[i].n;
502
503         return "UNKNOWN";
504 }
505
506 void bc_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
507 {
508         cb_log(5,bc->port,"BC_STATE_CHANGE: l3id:%x from:%s to:%s\n",
509                 bc->l3_id,
510                bc_state2str(bc->bc_state),
511                bc_state2str(state) );
512         
513         switch (state) {
514                 case BCHAN_ACTIVATED:
515                         if (bc->next_bc_state ==  BCHAN_BRIDGED) {
516                                 misdn_join_conf(bc, bc->conf_id);
517                                 bc->next_bc_state = BCHAN_EMPTY;
518                                 return;
519                         }
520                 default:
521                         bc->bc_state=state;
522                         break;
523         }
524 }
525
526 static void bc_next_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
527 {
528         cb_log(5,bc->port,"BC_NEXT_STATE_CHANGE: from:%s to:%s\n",
529                bc_state2str(bc->next_bc_state),
530                bc_state2str(state) );
531
532         bc->next_bc_state=state;
533 }
534
535
536 static void empty_bc(struct misdn_bchannel *bc)
537 {
538         bc->bframe_len=0;
539         
540
541         bc->in_use= 0;
542         bc->cw= 0;
543
544         bc->channel = 0;
545
546         bc->sending_complete = 0;
547
548         bc->restart_channel=0;
549         
550         bc->conf_id = 0;
551
552         bc->need_more_infos = 0;
553         
554         bc->send_dtmf=0;
555         bc->nodsp=0;
556         bc->nojitter=0;
557
558         bc->time_usec=0;
559         
560         bc->rxgain=0;
561         bc->txgain=0;
562
563         bc->crypt=0;
564         bc->curptx=0; bc->curprx=0;
565         
566         bc->crypt_key[0] = 0;
567         
568         bc->generate_tone=0;
569         bc->tone_cnt=0;
570   
571         bc->dnumplan=NUMPLAN_UNKNOWN;
572         bc->onumplan=NUMPLAN_UNKNOWN;
573         bc->rnumplan=NUMPLAN_UNKNOWN;
574         bc->cpnnumplan=NUMPLAN_UNKNOWN;
575         
576
577         bc->active = 0;
578
579         bc->early_bconnect = 1;
580         
581         bc->ec_enable = 0;
582         bc->ec_deftaps = 128;
583         bc->ec_whenbridged = 0;
584
585 #ifdef EC_TRAIN
586         bc->ec_training = 1;
587 #endif
588         
589         bc->orig=0;
590   
591         bc->cause=16;
592         bc->out_cause=16;
593         bc->pres=0 ; /* screened */
594         
595         bc->evq=EVENT_NOTHING;
596
597         bc->progress_coding=0;
598         bc->progress_location=0;
599         bc->progress_indicator=0;
600         
601 /** Set Default Bearer Caps **/
602         bc->capability=INFO_CAPABILITY_SPEECH;
603         bc->law=INFO_CODEC_ALAW;
604         bc->mode=0;
605         bc->rate=0x10;
606         bc->user1=0;
607         bc->urate=0;
608         
609         bc->hdlc=0;
610         
611         
612         bc->info_dad[0] = 0;
613         bc->display[0] = 0;
614         bc->infos_pending[0] = 0;
615         bc->cad[0] = 0;
616         bc->oad[0] = 0;
617         bc->dad[0] = 0;
618         bc->rad[0] = 0;
619         bc->orig_dad[0] = 0;
620         
621         bc->fac_in.Function = Fac_None;
622         bc->fac_out.Function = Fac_None;
623         
624         bc->te_choose_channel = 0;
625 }
626
627
628 static int clean_up_bc(struct misdn_bchannel *bc)
629 {
630         int ret=0;
631         unsigned char buff[32];
632         struct misdn_stack * stack;
633
634         cb_log(3, bc?bc->port:0, "$$$ CLEANUP CALLED pid:%d\n", bc?bc->pid:-1);
635         
636         if (!bc  ) return -1;
637         stack=get_stack_by_bc(bc);
638         
639         if (!stack) return -1;
640         
641         switch (bc->bc_state ) {
642         case BCHAN_CLEANED:
643                 cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
644                 return -1;
645                 
646         default:
647                 break;
648         }
649         
650         cb_log(2, stack->port, "$$$ Cleaning up bc with stid :%x pid:%d\n", bc->b_stid, bc->pid);
651         
652         manager_bchannel_deactivate(bc);
653
654
655         manager_ec_disable(bc);
656
657
658         mISDN_write_frame(stack->midev, buff, bc->layer_id|FLG_MSG_TARGET|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
659         
660         /*mISDN_clear_stack(stack->midev, bc->b_stid);*/
661
662         
663         bc->b_stid = 0;
664         bc_state_change(bc, BCHAN_CLEANED);
665         
666         return ret;
667 }
668
669
670
671 static void clear_l3(struct misdn_stack *stack)
672 {
673         int i;
674
675         for (i=0; i<=stack->b_num; i++) {
676                 if (global_state == MISDN_INITIALIZED)  {
677                         cb_event(EVENT_CLEANUP, &stack->bc[i], NULL); 
678                         empty_chan_in_stack(stack,i+1);
679                         empty_bc(&stack->bc[i]);
680                         clean_up_bc(&stack->bc[i]);
681                 }
682                 
683         } 
684 }
685
686 static int set_chan_in_stack(struct misdn_stack *stack, int channel)
687 {
688
689         cb_log(4,stack->port,"set_chan_in_stack: %d\n",channel);
690         if (channel >=1 ) {
691                 stack->channels[channel-1] = 1;
692         } else {
693                 cb_log(0,stack->port,"couldn't set channel %d in\n", channel );
694         }
695   
696         return 0;
697 }
698
699 #if 0
700 static int chan_in_stack_free(struct misdn_stack *stack, int channel)
701 {
702         if (stack->channels[channel-1])
703                 return 0;
704   
705         return 1;
706 }
707 #endif
708
709
710 static int newteid=0;
711
712 #define MAXPROCS 0x100
713
714 static int misdn_lib_get_l1_down(struct misdn_stack *stack)
715 {
716         /* Pull Up L1 */ 
717         iframe_t act;
718         act.prim = PH_DEACTIVATE | REQUEST; 
719         act.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
720
721         
722         act.dinfo = 0;
723         act.len = 0;
724
725         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
726
727
728 }
729
730
731 static int misdn_lib_get_l2_down(struct misdn_stack *stack)
732 {
733         
734         if (stack->ptp && (stack->nt) ) {
735                 msg_t *dmsg;
736                 /* L2 */
737                 dmsg = create_l2msg(DL_RELEASE| REQUEST, 0, 0);
738                 
739                 if (stack->nst.manager_l3(&stack->nst, dmsg))
740                         free_msg(dmsg);
741                 
742         } else {
743                 iframe_t act;
744                 
745                 act.prim = DL_RELEASE| REQUEST;
746                 act.addr = (stack->upper_id |FLG_MSG_DOWN)  ;
747                 
748                 act.dinfo = 0;
749                 act.len = 0;
750                 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
751         }
752         
753         return 0;
754 }
755
756
757 static int misdn_lib_get_l1_up(struct misdn_stack *stack)
758 {
759         /* Pull Up L1 */ 
760         iframe_t act;
761         act.prim = PH_ACTIVATE | REQUEST; 
762         act.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
763
764         
765         act.dinfo = 0;
766         act.len = 0;
767
768         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
769
770 }
771
772 int misdn_lib_get_l2_up(struct misdn_stack *stack)
773 {
774         
775         if (stack->ptp && (stack->nt) ) {
776                 msg_t *dmsg;
777                 /* L2 */
778                 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
779                 
780                 if (stack->nst.manager_l3(&stack->nst, dmsg))
781                         free_msg(dmsg);
782                 
783         } else {
784                 iframe_t act;
785                 
786                 act.prim = DL_ESTABLISH | REQUEST;
787                 act.addr = (stack->upper_id |FLG_MSG_DOWN)  ;
788                 
789                 act.dinfo = 0;
790                 act.len = 0;
791                 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
792         }
793         
794         return 0;
795 }
796
797 #if 0
798 static int misdn_lib_get_l2_te_ptp_up(struct misdn_stack *stack)
799 {
800         iframe_t act;
801                 
802         act.prim = DL_ESTABLISH | REQUEST;
803         act.addr = (stack->upper_id  & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
804                 
805         act.dinfo = 0;
806         act.len = 0;
807         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
808         return 0;
809 }
810 #endif
811
812 static int misdn_lib_get_short_status(struct misdn_stack *stack)
813 {
814         iframe_t act;
815         
816         
817         act.prim = MGR_SHORTSTATUS | REQUEST; 
818         
819         act.addr = (stack->upper_id | MSG_BROADCAST)  ;
820
821         act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
822         
823         act.len = 0;
824         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
825 }
826
827
828
829 static int create_process (int midev, struct misdn_bchannel *bc) {
830         iframe_t ncr;
831         int l3_id;
832         int i;
833         struct misdn_stack *stack=get_stack_by_bc(bc);
834         int free_chan;
835   
836         if (stack->nt) {
837                 free_chan = find_free_chan_in_stack(stack, bc, bc->channel_preselected?bc->channel:0);
838                 if (!free_chan) return -1;
839                 /*bc->channel=free_chan;*/
840                 
841                 cb_log(4,stack->port, " -->  found channel: %d\n",free_chan);
842     
843                 for (i=0; i <= MAXPROCS; i++)
844                         if (stack->procids[i]==0) break;
845     
846                 if (i== MAXPROCS) {
847                         cb_log(0, stack->port, "Couldnt Create New ProcId.\n");
848                         return -1;
849                 }
850                 stack->procids[i]=1;
851
852                 l3_id = 0xff00 | i;
853     
854                 ncr.prim = CC_NEW_CR | REQUEST; 
855
856                 ncr.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
857
858                 ncr.dinfo = l3_id;
859                 ncr.len = 0;
860
861                 bc->l3_id = l3_id;
862                 cb_log(3, stack->port, " --> new_l3id %x\n",l3_id);
863     
864         } else { 
865                 if (stack->ptp || bc->te_choose_channel) {
866                         /* we know exactly which channels are in use */
867                         free_chan = find_free_chan_in_stack(stack, bc, bc->channel_preselected?bc->channel:0);
868                         if (!free_chan) return -1;
869                         /*bc->channel=free_chan;*/
870                         cb_log(2,stack->port, " -->  found channel: %d\n",free_chan);
871                 } else {
872                         /* other phones could have made a call also on this port (ptmp) */
873                         bc->channel=0xff;
874                 }
875     
876     
877                 /* if we are in te-mode, we need to create a process first */
878                 if (newteid++ > 0xffff)
879                         newteid = 0x0001;
880     
881                 l3_id = (entity<<16) | newteid;
882                 /* preparing message */
883                 ncr.prim = CC_NEW_CR | REQUEST; 
884
885                 ncr.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
886
887                 ncr.dinfo =l3_id;
888                 ncr.len = 0;
889                 /* send message */
890
891                 bc->l3_id = l3_id;
892                 cb_log(3, stack->port, "--> new_l3id %x\n",l3_id);
893     
894                 mISDN_write(midev, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
895         }
896   
897         return l3_id;
898 }
899
900
901 void misdn_lib_setup_bc(struct misdn_bchannel *bc)
902 {
903         clean_up_bc(bc);
904         setup_bc(bc);
905 }
906
907
908 int setup_bc(struct misdn_bchannel *bc)
909 {
910         unsigned char buff[1025];
911   
912         mISDN_pid_t pid;
913         int ret;
914         
915
916         struct misdn_stack *stack=get_stack_by_bc(bc);
917
918         if (!stack) {
919                 cb_log(0, bc->port, "setup_bc: NO STACK FOUND!!\n");
920                 return -1;
921         }
922         
923         int midev=stack->midev;
924         int channel=bc->channel-1-(bc->channel>16);
925         int b_stid=stack->b_stids[channel>=0?channel:0];
926
927
928         switch (bc->bc_state) {
929                 case BCHAN_CLEANED:
930                         break;
931                 default:
932                         cb_log(4, stack->port, "$$$ bc already upsetted stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) );
933                         return -1;
934         }
935         
936         cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
937         
938         if (b_stid <= 0) {
939                 cb_log(0, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
940                 
941                 bc_state_change(bc,BCHAN_ERROR);
942                 return 1;
943         }
944         
945         
946         bc->b_stid = b_stid;
947         
948         {
949                 layer_info_t li;
950                 memset(&li, 0, sizeof(li));
951     
952                 li.object_id = -1;
953                 li.extentions = 0;
954                 
955                 li.st = bc->b_stid; /*  given idx */
956
957
958 #define MISDN_DSP
959 #ifndef MISDN_DSP
960                 bc->nodsp=1;
961 #endif
962                 if ( bc->hdlc || bc->nodsp) {
963                         cb_log(4, stack->port,"setup_bc: without dsp\n");
964                         { 
965                                 int l = sizeof(li.name);
966                                 strncpy(li.name, "B L3", l);
967                                 li.name[l-1] = 0;
968                         }
969                         li.pid.layermask = ISDN_LAYER((3));
970                         li.pid.protocol[3] = ISDN_PID_L3_B_USER;
971                         
972                         bc->layer=3;
973                 } else {
974                         cb_log(4, stack->port,"setup_bc: with dsp\n");
975                         { 
976                                 int l = sizeof(li.name);
977                                 strncpy(li.name, "B L4", l);
978                                 li.name[l-1] = 0;
979                         }
980                         li.pid.layermask = ISDN_LAYER((4));
981                         li.pid.protocol[4] = ISDN_PID_L4_B_USER
982 ;
983                         bc->layer=4;
984                         
985                 }  
986                 
987                 ret = mISDN_new_layer(midev, &li);
988                 if (ret ) {
989                         cb_log(0, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
990
991                         bc_state_change(bc,BCHAN_ERROR);
992                         return(-EINVAL);
993                 }
994                 
995                 bc->layer_id = li.id;
996         }
997         
998         memset(&pid, 0, sizeof(pid));
999         
1000         
1001         
1002         cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
1003         
1004         if (bc->nodsp) {
1005                 cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
1006                 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1007                 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1008                 pid.protocol[3] = ISDN_PID_L3_B_USER;
1009                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
1010                 
1011         } else if ( bc->hdlc ) {
1012                 cb_log(2, stack->port," --> HDLC Mode\n");
1013                 pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
1014                 pid.protocol[2] = ISDN_PID_L2_B_TRANS  ;
1015                 pid.protocol[3] = ISDN_PID_L3_B_USER;
1016                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
1017         } else {
1018                 cb_log(2, stack->port," --> TRANSPARENT Mode\n");
1019                 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1020                 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1021                 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1022                 pid.protocol[4] = ISDN_PID_L4_B_USER;
1023                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1024                 
1025         } 
1026
1027         ret = mISDN_set_stack(midev, bc->b_stid, &pid);
1028
1029         if (ret){
1030                 cb_log(0, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
1031                 
1032                 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1033                 
1034                 bc_state_change(bc,BCHAN_ERROR);
1035                 return(-EINVAL);
1036         }
1037
1038         ret = mISDN_get_setstack_ind(midev, bc->layer_id);
1039
1040         if (ret) {
1041                 cb_log(0, stack->port,"$$$ Set StackIND Err: %d %s\n",ret,strerror(errno));
1042                 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1043                 
1044                 bc_state_change(bc,BCHAN_ERROR);
1045                 return(-EINVAL);
1046         }
1047
1048         ret = mISDN_get_layerid(midev, bc->b_stid, bc->layer) ;
1049
1050         bc->addr = ret>0? ret : 0;
1051
1052         if (!bc->addr) {
1053                 cb_log(0, stack->port,"$$$ Get Layerid Err: %d %s\n",ret,strerror(errno));
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         }
1058
1059         manager_bchannel_activate(bc);
1060         
1061         bc_state_change(bc,BCHAN_ACTIVATED);
1062
1063         return 0;
1064 }
1065
1066
1067
1068 /** IFACE **/
1069 int init_bc(struct misdn_stack *stack,  struct misdn_bchannel *bc, int midev, int port, int bidx,  char *msn, int firsttime)
1070 {
1071         unsigned char buff[1025];
1072         iframe_t *frm = (iframe_t *)buff;
1073         int ret;
1074   
1075         if (!bc) return -1;
1076   
1077         cb_log(8, port, "Init.BC %d.\n",bidx);
1078         
1079         memset(bc, 0,sizeof(struct misdn_bchannel));
1080         
1081         if (msn) {
1082                 int l = sizeof(bc->msn);
1083                 strncpy(bc->msn,msn, l);
1084                 bc->msn[l-1] = 0;
1085         }
1086         
1087         
1088         empty_bc(bc);
1089         bc_state_change(bc, BCHAN_CLEANED);
1090         
1091         bc->port=stack->port;
1092         bc->nt=stack->nt?1:0;
1093         
1094         {
1095                 ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
1096
1097                 if (!ibuf) return -1;
1098                 
1099                 clear_ibuffer( ibuf);
1100                 
1101                 ibuf->rsem=malloc(sizeof(sem_t));
1102                 
1103                 bc->astbuf=ibuf;
1104
1105                 if (sem_init(ibuf->rsem,1,0)<0)
1106                         sem_init(ibuf->rsem,0,0);
1107                 
1108         }
1109         
1110         
1111         
1112         
1113         {
1114                 stack_info_t *stinf;
1115                 ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
1116                 if (ret < 0) {
1117                         cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1118                         return -1;
1119                 }
1120     
1121                 stinf = (stack_info_t *)&frm->data.p;
1122     
1123                 cb_log(8, port, " --> Child %x\n",stinf->child[bidx]);
1124         }
1125   
1126         return 0;
1127 }
1128
1129
1130
1131 struct misdn_stack* stack_init( int midev, int port, int ptp )
1132 {
1133         int ret;
1134         unsigned char buff[1025];
1135         iframe_t *frm = (iframe_t *)buff;
1136         stack_info_t *stinf;
1137         int i; 
1138         layer_info_t li;
1139
1140         struct misdn_stack *stack = malloc(sizeof(struct misdn_stack));
1141         if (!stack ) return NULL;
1142
1143
1144         cb_log(8, port, "Init. Stack.\n");
1145   
1146         memset(stack,0,sizeof(struct misdn_stack));
1147   
1148         for (i=0; i<MAX_BCHANS + 1; i++ ) stack->channels[i]=0;
1149         
1150         stack->port=port;
1151         stack->midev=midev;
1152         stack->ptp=ptp;
1153   
1154         stack->holding=NULL;
1155         stack->pri=0;
1156   
1157         msg_queue_init(&stack->downqueue);
1158         msg_queue_init(&stack->upqueue);
1159   
1160         /* query port's requirements */
1161         ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
1162         if (ret < 0) {
1163                 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1164                 return(NULL);
1165         }
1166   
1167         stinf = (stack_info_t *)&frm->data.p;
1168
1169         stack->d_stid = stinf->id;
1170         stack->b_num = stinf->childcnt;
1171
1172         for (i=0; i<=stinf->childcnt; i++)
1173                 stack->b_stids[i] = stinf->child[i];
1174   
1175         switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
1176         case ISDN_PID_L0_TE_S0:
1177                 stack->nt=0;
1178                 break;
1179         case ISDN_PID_L0_NT_S0:
1180                 cb_log(8, port, "NT Stack\n");
1181
1182                 stack->nt=1;
1183                 break;
1184
1185         case ISDN_PID_L0_TE_U:
1186                 break;
1187         case ISDN_PID_L0_NT_U:
1188                 break;
1189         case ISDN_PID_L0_TE_UP2:
1190                 break;
1191         case ISDN_PID_L0_NT_UP2:
1192                 break;
1193         case ISDN_PID_L0_TE_E1:
1194                 cb_log(8, port, "TE S2M Stack\n");
1195                 stack->nt=0;
1196                 stack->pri=1;
1197                 break;
1198         case ISDN_PID_L0_NT_E1:
1199                 cb_log(8, port, "TE S2M Stack\n");
1200                 stack->nt=1;
1201                 stack->pri=1;
1202                 
1203                 break;
1204         default:
1205                 cb_log(0, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
1206
1207         }
1208
1209         if (!stack->nt) {
1210                 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) { 
1211                         stack->ptp = 1;
1212                 } else {
1213                         stack->ptp = 0;
1214                 }
1215         }
1216         
1217         {
1218                 int ret;
1219                 int nt=stack->nt;
1220
1221                 cb_log(8, port, "Init. Stack.\n");
1222                 
1223                 memset(&li, 0, sizeof(li));
1224                 {
1225                         int l = sizeof(li.name);
1226                         strncpy(li.name,nt?"net l2":"user l4", l);
1227                         li.name[l-1] = 0;
1228                 }
1229                 li.object_id = -1;
1230                 li.extentions = 0;
1231                 li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1232                 li.pid.layermask = ISDN_LAYER((nt?2:4));
1233                 li.st = stack->d_stid;
1234                 
1235                 
1236                 ret = mISDN_new_layer(midev, &li);
1237                 if (ret) {
1238                         cb_log(0, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
1239                         return(NULL);
1240                 }
1241                 
1242                 
1243                 stack->upper_id = li.id;
1244                 ret = mISDN_register_layer(midev, stack->d_stid, stack->upper_id);
1245                 if (ret)
1246                 {
1247                         cb_log(0,port,"Cannot register layer %d of this port.\n", nt?2:4);
1248                         return(NULL);
1249                 }
1250                 
1251                 stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3); 
1252                 if (stack->lower_id < 0) {
1253                         cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
1254                         return(NULL);
1255                 }
1256                 
1257                 stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
1258                 if (stack->upper_id < 0) {
1259                         cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, 2);
1260                         return(NULL);
1261                 }
1262                 
1263                 cb_log(8, port, "NT Stacks upper_id %x\n",stack->upper_id);
1264                 
1265                 
1266                 /* create nst (nt-mode only) */
1267                 if (nt) {
1268                         
1269                         memset(&stack->nst, 0, sizeof(net_stack_t));
1270                         memset(&stack->mgr, 0, sizeof(manager_t));
1271     
1272                         stack->mgr.nst = &stack->nst;
1273                         stack->nst.manager = &stack->mgr;
1274     
1275                         stack->nst.l3_manager = handle_event_nt;
1276                         stack->nst.device = midev;
1277                         stack->nst.cardnr = port;
1278                         stack->nst.d_stid = stack->d_stid;
1279     
1280                         stack->nst.feature = FEATURE_NET_HOLD;
1281                         if (stack->ptp)
1282                                 stack->nst.feature |= FEATURE_NET_PTP;
1283                         if (stack->pri)
1284                                 stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1285                         
1286                         stack->nst.l1_id = stack->lower_id;
1287                         stack->nst.l2_id = stack->upper_id;
1288                         
1289                         msg_queue_init(&stack->nst.down_queue);
1290                         
1291                         Isdnl2Init(&stack->nst);
1292                         Isdnl3Init(&stack->nst);
1293                         
1294                 } 
1295                 
1296                 if (!stack->nt) {
1297                         /*assume L1 is up, we'll get DEACTIVATES soon, for non
1298                          * up L1s*/
1299                         stack->l1link=0;
1300                 }
1301                 stack->l1link=0;
1302                 stack->l2link=0;
1303 #if 0   
1304                 if (!stack->nt) {
1305                         misdn_lib_get_short_status(stack);
1306                 } else {
1307                         misdn_lib_get_l1_up(stack);
1308                         if (!stack->ptp) misdn_lib_get_l1_up(stack);
1309                         misdn_lib_get_l2_up(stack);
1310                 }
1311 #endif
1312
1313                 misdn_lib_get_short_status(stack);
1314                 misdn_lib_get_l1_up(stack);
1315                 misdn_lib_get_l2_up(stack);
1316                 
1317         }
1318
1319         cb_log(8,0,"stack_init: port:%d lowerId:%x  upperId:%x\n",stack->port,stack->lower_id, stack->upper_id);
1320         
1321         return stack;
1322 }
1323
1324
1325 void stack_destroy(struct misdn_stack* stack)
1326 {
1327         char buf[1024];
1328         if (!stack) return;
1329
1330         if (stack->nt) {
1331                 cleanup_Isdnl2(&stack->nst);
1332                 cleanup_Isdnl3(&stack->nst);
1333         }
1334   
1335         if (stack->lower_id) 
1336                 mISDN_write_frame(stack->midev, buf, stack->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1337
1338         if (stack->upper_id) 
1339                 mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1340 }
1341
1342
1343 static struct misdn_stack * find_stack_by_addr(int  addr)
1344 {
1345         struct misdn_stack *stack;
1346         
1347         for (stack=glob_mgr->stack_list;
1348              stack;
1349              stack=stack->next) {
1350                 if ( (stack->upper_id&STACK_ID_MASK) == (addr&STACK_ID_MASK)) return stack;
1351
1352         }
1353   
1354         return NULL;
1355 }
1356
1357
1358 static struct misdn_stack * find_stack_by_port(int port)
1359 {
1360         struct misdn_stack *stack;
1361   
1362         for (stack=glob_mgr->stack_list;
1363              stack;
1364              stack=stack->next) 
1365                 if (stack->port == port) return stack;
1366   
1367         return NULL;
1368 }
1369
1370 static struct misdn_stack * find_stack_by_mgr(manager_t* mgr_nt)
1371 {
1372         struct misdn_stack *stack;
1373   
1374         for (stack=glob_mgr->stack_list;
1375              stack;
1376              stack=stack->next) 
1377                 if ( &stack->mgr == mgr_nt) return stack;
1378   
1379         return NULL;
1380 }
1381
1382 static struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
1383 {
1384         int i;
1385         for (i=0; i<=stack->b_num; i++) {
1386                 if ( (stack->bc[i].l3_id & mask)  ==  (l3id & mask)) return &stack->bc[i] ;
1387         }
1388         return stack_holder_find(stack,l3id);
1389 }
1390
1391
1392 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
1393 {
1394         int i;
1395         for (i=0; i<=stack->b_num; i++) {
1396                 if (stack->bc[i].l3_id == l3id) return &stack->bc[i] ;
1397         }
1398         return stack_holder_find(stack,l3id);
1399 }
1400
1401 static struct misdn_bchannel *find_bc_holded(struct misdn_stack *stack)
1402 {
1403         int i;
1404         for (i=0; i<=stack->b_num; i++) {
1405                 if (stack->bc[i].holded ) return &stack->bc[i] ;
1406         }
1407         return NULL;
1408 }
1409
1410
1411 static struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
1412 {
1413         struct misdn_stack* stack;
1414         int i;
1415
1416         for (stack=glob_mgr->stack_list;
1417              stack;
1418              stack=stack->next) {
1419                 for (i=0; i<=stack->b_num; i++) {
1420                         if ( (stack->bc[i].addr&STACK_ID_MASK)==(addr&STACK_ID_MASK) ||  stack->bc[i].layer_id== addr ) {
1421                                 return &stack->bc[i];
1422                         }
1423                 }
1424         }
1425         
1426         return NULL;
1427 }
1428
1429 struct misdn_bchannel *find_bc_by_confid(unsigned long confid)
1430 {
1431         struct misdn_stack* stack;
1432         int i;
1433         
1434         for (stack=glob_mgr->stack_list;
1435              stack;
1436              stack=stack->next) {
1437                 for (i=0; i<=stack->b_num; i++) {
1438                         if ( stack->bc[i].conf_id==confid ) {
1439                                 return &stack->bc[i];
1440                         }
1441                 }
1442         }
1443         return NULL;
1444 }
1445
1446
1447 static struct misdn_bchannel *find_bc_by_channel(int port, int channel)
1448 {
1449         struct misdn_stack* stack=find_stack_by_port(port);
1450         int i;
1451
1452         if (!stack) return NULL;        
1453         
1454         for (i=0; i<=stack->b_num; i++) {
1455                 if ( stack->bc[i].channel== channel ) {
1456                         return &stack->bc[i];
1457                 }
1458         }
1459                 
1460         return NULL;
1461 }
1462
1463
1464
1465
1466
1467 static int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
1468 {
1469         struct misdn_stack *stack=get_stack_by_bc(bc);
1470         
1471         if (!stack->nt) {
1472                 
1473                 switch (event) {
1474
1475                 case EVENT_CONNECT_ACKNOWLEDGE:
1476 #if 0
1477                         if ( !misdn_cap_is_speech(bc->capability)) {
1478                                 int ret=setup_bc(bc);
1479                                 if (ret == -EINVAL){
1480                                         cb_log(0,bc->port,"send_event: setup_bc failed\n");
1481                                 }
1482                         }
1483 #endif  
1484                         break;
1485                 case EVENT_CONNECT:
1486
1487                         if ( *bc->crypt_key ) {
1488                                 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);
1489                                 manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
1490                         }
1491                 case EVENT_ALERTING:
1492                 case EVENT_PROGRESS:
1493                 case EVENT_PROCEEDING:
1494                 case EVENT_SETUP_ACKNOWLEDGE:
1495
1496                 setup_bc(bc);
1497                 
1498                 case EVENT_SETUP:
1499                         
1500                 {
1501                         if (bc->channel == 0xff) {
1502                                 bc->channel=find_free_chan_in_stack(stack, bc,  0);
1503                                 if (!bc->channel) {
1504                                         cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n");
1505                                         break;
1506                                 }
1507                         }  
1508
1509                         if (bc->channel >0 && bc->channel<255) {
1510                                 set_chan_in_stack(stack ,bc->channel);
1511                         }
1512
1513 #if 0
1514                         int ret=setup_bc(bc);
1515                         if (ret == -EINVAL){
1516                                 cb_log(0,bc->port,"handle_event: setup_bc failed\n");
1517                                 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1518                         }
1519 #endif
1520                 }
1521                 break;
1522
1523                 case EVENT_RELEASE_COMPLETE:
1524                 case EVENT_RELEASE:
1525                         if (bc->channel>0)
1526                                 empty_chan_in_stack(stack,bc->channel);
1527                         int tmpcause=bc->cause; 
1528                         empty_bc(bc);
1529                         bc->cause=tmpcause;
1530                         clean_up_bc(bc);
1531                         break;
1532                 default:
1533                         break;
1534                 }
1535         } else {    /** NT MODE **/
1536                 
1537         }
1538         return 0;
1539 }
1540
1541 static int handle_new_process(struct misdn_stack *stack, iframe_t *frm)
1542 {
1543   
1544         struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0, 1);
1545         
1546         
1547         if (!bc) {
1548                 cb_log(0, stack->port, " --> !! lib: No free channel!\n");
1549                 return -1;
1550         }
1551   
1552         cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
1553         bc->l3_id=frm->dinfo;
1554         return 0;
1555 }
1556
1557 static int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
1558 {
1559         if (!stack) return -1;
1560   
1561         switch (frm->prim) {
1562         case CC_NEW_CR|INDICATION:
1563                 cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
1564                 if (handle_new_process(stack, frm) <0) {
1565                         return -1;
1566                 }
1567                 return 1;
1568         case CC_NEW_CR|CONFIRM:
1569                 return 1;
1570         case CC_NEW_CR|REQUEST:
1571                 return 1;
1572         case CC_RELEASE_CR|REQUEST:
1573                 return 1;
1574         case CC_RELEASE_CR|CONFIRM:
1575                 break;
1576         case CC_RELEASE_CR|INDICATION:
1577                 cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n",frm->dinfo);
1578                 {
1579                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, frm->dinfo);
1580                         struct misdn_bchannel dummybc;
1581       
1582                         if (!bc) {
1583                                 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
1584                                 memset (&dummybc,0,sizeof(dummybc));
1585                                 dummybc.port=stack->port;
1586                                 dummybc.l3_id=frm->dinfo;
1587                                 dummybc.nt=stack->nt;
1588                                 bc=&dummybc; 
1589                         }
1590       
1591                         if (bc) {
1592                                 cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo);
1593                                 if (bc->channel>0)
1594                                         empty_chan_in_stack(stack,bc->channel);
1595                                 empty_bc(bc);
1596                                 
1597                                 clean_up_bc(bc);
1598                                 
1599                                 dump_chan_list(stack);
1600                                 /*bc->pid = 0;*/
1601                                 bc->need_disconnect=0;
1602                                 bc->need_release=0;
1603                                 bc->need_release_complete=0;
1604                                 
1605                                 cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
1606                                 if (bc->stack_holder) {
1607                                         cb_log(4,stack->port, "REMOVEING Holder\n");
1608                                         stack_holder_remove( stack, bc);
1609                                         free(bc);
1610                                 }
1611                         }
1612                         else {
1613                                 if (stack->nt) 
1614                                         cb_log(4, stack->port, "BC with dinfo: %x  not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr);
1615                         }
1616       
1617                         return 1;
1618                 }
1619                 break;
1620         }
1621   
1622         return 0;
1623 }
1624
1625
1626 /*Emptys bc if it's reserved (no SETUP out yet)*/
1627 void misdn_lib_release(struct misdn_bchannel *bc)
1628 {
1629         struct misdn_stack *stack=get_stack_by_bc(bc);
1630
1631         if (!stack) {
1632                 cb_log(1,0,"misdn_release: No Stack found\n");
1633                 return;
1634         }
1635         
1636         if (bc->channel>0) {
1637                 empty_chan_in_stack(stack,bc->channel);
1638         }
1639         empty_bc(bc);
1640         clean_up_bc(bc);
1641 }
1642
1643
1644
1645
1646 int misdn_lib_get_port_up (int port) 
1647 { /* Pull Up L1 */ 
1648         struct misdn_stack *stack;
1649         
1650         for (stack=glob_mgr->stack_list;
1651              stack;
1652              stack=stack->next) {
1653                 
1654                 if (stack->port == port) {
1655
1656                         if (!stack->l1link)
1657                                 misdn_lib_get_l1_up(stack);
1658                         if (!stack->l2link)
1659                                 misdn_lib_get_l2_up(stack);
1660                         
1661                         return 0;
1662                 }
1663         }
1664         return 0;
1665 }
1666
1667
1668 int misdn_lib_get_port_down (int port) 
1669 { /* Pull Down L1 */ 
1670         struct misdn_stack *stack;
1671         for (stack=glob_mgr->stack_list;
1672              stack;
1673              stack=stack->next) {
1674                 if (stack->port == port) {
1675                                 if (stack->l2link)
1676                                         misdn_lib_get_l2_down(stack);
1677                                 misdn_lib_get_l1_down(stack);
1678                         return 0;
1679                 }
1680         }
1681         return 0;
1682 }
1683
1684 int misdn_lib_port_up(int port, int check)
1685 {
1686         struct misdn_stack *stack;
1687
1688
1689         for (stack=glob_mgr->stack_list;
1690              stack;
1691              stack=stack->next) {
1692                 
1693                 if ( !stack->ptp && !check) return 1;
1694                 
1695                 if (stack->port == port) {
1696
1697                         if (stack->blocked) {
1698                                 cb_log(0,port, "Port Blocked:%d L2:%d L1:%d\n", stack->blocked, stack->l2link, stack->l1link);
1699                                 return -1;
1700                         }
1701
1702                         if (stack->ptp ) {
1703
1704                                 if (stack->l1link && stack->l2link) {
1705                                         return 1;
1706                                 } else {
1707                                         cb_log(0,port, "Port Down L2:%d L1:%d\n",
1708                                                 stack->l2link, stack->l1link);
1709                                         return 0;
1710                                 }
1711                         } else {
1712                                 if ( stack->l1link)
1713                                         return 1;
1714                                 else {
1715                                         cb_log(0,port, "Port down PMP\n");
1716                                         return 0;
1717                                 }
1718                         }
1719                 }
1720         }
1721   
1722         return -1;
1723 }
1724
1725
1726 int
1727 handle_event_nt(void *dat, void *arg)
1728 {
1729         manager_t *mgr = (manager_t *)dat;
1730         msg_t *msg = (msg_t *)arg;
1731         mISDNuser_head_t *hh;
1732         int reject=0;
1733
1734         struct misdn_stack *stack=find_stack_by_mgr(mgr);
1735         int port;
1736
1737         if (!msg || !mgr)
1738                 return(-EINVAL);
1739
1740         hh=(mISDNuser_head_t*)msg->data;
1741         port=stack->port;
1742         
1743         cb_log(5, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1744         {
1745                 switch(hh->prim){
1746                 case CC_RETRIEVE|INDICATION:
1747                 {
1748                         iframe_t frm; /* fake te frm to add callref to global callreflist */
1749                         frm.dinfo = hh->dinfo;
1750
1751                         frm.addr=stack->upper_id | FLG_MSG_DOWN;
1752
1753                         frm.prim = CC_NEW_CR|INDICATION;
1754                         
1755                         if (handle_cr( stack, &frm)< 0) {
1756                                 msg_t *dmsg;
1757                                 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1758                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1759                                 stack->nst.manager_l3(&stack->nst, dmsg);
1760                                 free_msg(msg);
1761                                 return 0;
1762                         }
1763                         
1764                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1765                         struct misdn_bchannel *hold_bc=stack_holder_find(stack,bc->l3_id);
1766                         cb_log(4, stack->port, "bc_l3id:%x holded_bc_l3id:%x\n",bc->l3_id, hold_bc->l3_id);
1767
1768                         if (hold_bc) {
1769                                 cb_log(4, stack->port, "REMOVEING Holder\n");
1770
1771                                 /*swap the backup to our new channel back*/
1772                                 stack_holder_remove(stack, hold_bc);
1773                                 memcpy(bc,hold_bc,sizeof(struct misdn_bchannel));
1774                                 free(hold_bc);
1775
1776                                 bc->holded=0;
1777                         }
1778                         
1779                 }
1780                         
1781                         break;
1782                         
1783                 case CC_SETUP|CONFIRM:
1784                 {
1785                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1786                         int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1787                         cb_log(4, stack->port, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID  is %x\n",l3id );
1788         
1789                         if (!bc) { cb_log(4, stack->port, "Bc Not found (after SETUP CONFIRM)\n"); return 0; }
1790                         cb_log (2,bc->port,"I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
1791                         bc->l3_id=l3id;
1792                         cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1793                 }
1794                 free_msg(msg);
1795                 return 0;
1796       
1797                 case CC_SETUP|INDICATION:
1798                 {
1799                         iframe_t frm; /* fake te frm to add callref to global callreflist */
1800                         frm.dinfo = hh->dinfo;
1801                         frm.addr=stack->upper_id;
1802                         frm.prim = CC_NEW_CR|INDICATION;
1803                         
1804                         if (handle_cr(stack, &frm)< 0) {
1805                                 msg_t *dmsg;
1806                                 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1807                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1808                                 stack->nst.manager_l3(&stack->nst, dmsg);
1809                                 free_msg(msg);
1810                                 return 0;
1811                         }
1812                 }
1813                 break;
1814
1815                 case CC_CONNECT_ACKNOWLEDGE|INDICATION:
1816 #if 0
1817                 {
1818                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1819                         if (bc) {
1820                                 if ( !misdn_cap_is_speech(bc->capability)) {
1821                                         int ret=setup_bc(bc);
1822                                         if (ret == -EINVAL){
1823                                                 cb_log(0,bc->port,"send_event: setup_bc failed\n");
1824                                                 
1825                                         }
1826                                 }
1827                         }
1828                 }
1829 #endif
1830                 break;
1831                 
1832                 case CC_ALERTING|INDICATION:
1833                 case CC_PROCEEDING|INDICATION:
1834                 case CC_SETUP_ACKNOWLEDGE|INDICATION:
1835                         if(!stack->ptp) break;  
1836                 case CC_CONNECT|INDICATION:
1837                 {
1838 #if 0
1839                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1840                         
1841                         if (!bc) {
1842                                 msg_t *dmsg;
1843                                 cb_log(0, stack->port,"!!!! We didn't found our bc, dinfo:%x on this port.\n",hh->dinfo);
1844                                 
1845                                 cb_log(0, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
1846                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1847                                 stack->nst.manager_l3(&stack->nst, dmsg);
1848                                 free_msg(msg);
1849                                 return 0;
1850                                 
1851                         }
1852                         int ret=setup_bc(bc);
1853                         if (ret == -EINVAL){
1854                                 cb_log(0,bc->port,"handle_event_nt: setup_bc failed\n");
1855                                 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1856                         }
1857 #endif
1858                 }
1859                 break;
1860                 case CC_DISCONNECT|INDICATION:
1861                 {
1862                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1863                         if (!bc) {
1864                                 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1865                                 if (bc) { 
1866                                         int myprocid=bc->l3_id&0x0000ffff;
1867                                         hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1868                                         cb_log(3,stack->port,"Reject dinfo: %x cause:%d\n",hh->dinfo,bc->cause);
1869                                         reject=1;               
1870                                 }
1871                         }
1872                 }
1873                 break;
1874                 
1875                 case CC_FACILITY|INDICATION:
1876                 {
1877                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1878                         if (!bc) {
1879                                 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1880                                 if (bc) { 
1881                                         int myprocid=bc->l3_id&0x0000ffff;
1882                                         hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1883                                         cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
1884                                 }
1885                         }
1886                 }
1887                 break;
1888                 
1889                 case CC_RELEASE_COMPLETE|INDICATION:
1890                         break;
1891
1892                 case CC_SUSPEND|INDICATION:
1893                 {
1894                         msg_t *dmsg;
1895                         cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
1896                         dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1897                         stack->nst.manager_l3(&stack->nst, dmsg);
1898                         free_msg(msg);
1899                         return 0;
1900                 }
1901                 break;
1902                 case CC_RESUME|INDICATION:
1903                         break;
1904
1905                 case CC_RELEASE|CONFIRM:
1906                         break;
1907                         
1908                 case CC_RELEASE|INDICATION:
1909                         break;
1910
1911                 case CC_RELEASE_CR|INDICATION:
1912                 {
1913                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1914                         struct misdn_bchannel dummybc;
1915                         iframe_t frm; /* fake te frm to remove callref from global callreflist */
1916                         frm.dinfo = hh->dinfo;
1917
1918                         frm.addr=stack->upper_id | FLG_MSG_DOWN;
1919
1920                         frm.prim = CC_RELEASE_CR|INDICATION;
1921                         cb_log(4, stack->port, " --> Faking Realease_cr for %x\n",frm.addr);
1922                         /** removing procid **/
1923                         if (!bc) {
1924                                 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
1925                                 memset (&dummybc,0,sizeof(dummybc));
1926                                 dummybc.port=stack->port;
1927                                 dummybc.l3_id=hh->dinfo;
1928                                 dummybc.nt=stack->nt;
1929                                 bc=&dummybc; 
1930                         }
1931         
1932                         if (bc) {
1933                                 if ( (bc->l3_id & 0xff00) == 0xff00) {
1934                                         cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xff);
1935                                         stack->procids[bc->l3_id&0xff] = 0 ;
1936                                 }
1937                         }
1938                         else cb_log(0, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is a bad port.\n");
1939         
1940                         if (handle_cr(stack, &frm)<0) {
1941                         }
1942
1943                         free_msg(msg);
1944                         return 0 ;
1945                 }
1946                 break;
1947       
1948                 case CC_NEW_CR|INDICATION:
1949                         /*  Got New CR for bchan, for now I handle this one in */
1950                         /*  connect_ack, Need to be changed */
1951                 {
1952                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1953                         int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1954                         if (!bc) { cb_log(0, stack->port, " --> In NEW_CR: didn't found bc ??\n"); return -1;};
1955                         if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
1956                                 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
1957                                 stack->procids[bc->l3_id&0xff] = 0 ;
1958                         }
1959                         cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID  is %x\n",l3id );
1960         
1961                         bc->l3_id =l3id;
1962                         cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1963         
1964                         free_msg(msg);
1965                         return 0;
1966                 }
1967       
1968                 case DL_ESTABLISH | INDICATION:
1969                 case DL_ESTABLISH | CONFIRM:
1970                 {
1971                         cb_log(3, stack->port, "%% GOT L2 Activate Info.\n");
1972                         
1973                         if (stack->ptp && stack->l2link) {
1974                                 cb_log(0, stack->port, "%% GOT L2 Activate Info. but we're activated already.. this l2 is faulty, blocking port\n");
1975                                 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
1976                         }
1977                         
1978                         stack->l2link = 1;
1979                         stack->l2upcnt=0;
1980                         
1981                         free_msg(msg);
1982                         return 0;
1983                 }
1984                 break;
1985
1986
1987                 case DL_RELEASE | INDICATION:
1988                 case DL_RELEASE | CONFIRM:
1989                 {
1990                         if (stack->ptp) {
1991                                 cb_log(3 , stack->port, "%% GOT L2 DeActivate Info.\n");
1992
1993                                 if (stack->l2upcnt>3) {
1994                                         cb_log(0 , stack->port, "!!! Could not Get the L2 up after 3 Attemps!!!\n");
1995                                 }  else {
1996 #if 0
1997                                         if (stack->nt) misdn_lib_reinit_nt_stack(stack->port);
1998 #endif
1999                                         if (stack->l1link) {
2000                                                 misdn_lib_get_l2_up(stack);
2001                                                 stack->l2upcnt++;
2002                                         }
2003                                 }
2004                                 
2005                         } else 
2006                                 cb_log(3, stack->port, "%% GOT L2 DeActivate Info.\n");
2007                         
2008                         stack->l2link = 0;
2009                         free_msg(msg);
2010                         return 0;
2011                 }
2012                 break;
2013                 }
2014         }
2015         
2016         {
2017                 /*  Parse Events and fire_up to App. */
2018                 struct misdn_bchannel *bc;
2019                 struct misdn_bchannel dummybc;
2020                 
2021                 enum event_e event = isdn_msg_get_event(msgs_g, msg, 1);
2022     
2023                 bc=find_bc_by_l3id(stack, hh->dinfo);
2024     
2025                 if (!bc) {
2026       
2027                         cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x).\n", hh->dinfo);
2028                         memset (&dummybc,0,sizeof(dummybc));
2029                         dummybc.port=stack->port;
2030                         dummybc.l3_id=hh->dinfo;
2031                         dummybc.nt=stack->nt;
2032                         bc=&dummybc; 
2033                 }
2034                 if (bc ) {
2035                         isdn_msg_parse_event(msgs_g,msg,bc, 1);
2036
2037                         switch (event) {
2038                                 case EVENT_SETUP:
2039                                         if (bc->channel>0 && bc->channel<255) {
2040
2041                                                 if (stack->ptp) 
2042                                                         set_chan_in_stack(stack, bc->channel);
2043                                                 else 
2044                                                         cb_log(0,stack->port," --> PTMP but channel requested\n");
2045
2046                                         } else {
2047
2048                                                 bc->channel = find_free_chan_in_stack(stack, bc, 0);
2049                                                 if (!bc->channel) {
2050                                                         cb_log(0, stack->port, " No free channel at the moment\n");
2051                                         
2052                                                         msg_t *dmsg;
2053                                 
2054                                                         cb_log(0, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
2055                                                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
2056                                                         stack->nst.manager_l3(&stack->nst, dmsg);
2057                                                         free_msg(msg);
2058                                                         return 0;
2059                                                 }
2060                                                 
2061                                         }
2062 #if 0
2063                                         setup_bc(bc);
2064 #endif
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                         handle_event(bc, event, frm);
2611                         /*  shoot up event to App: */
2612                         cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2613       
2614                         if(!isdn_get_info(msgs_g,event,0)) 
2615                                 cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2616                         else
2617                                 response=cb_event(event, bc, glob_mgr->user_data);
2618 #if 1
2619                         if (event == EVENT_SETUP) {
2620                                 switch (response) {
2621                                 case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
2622
2623                                         cb_log(0, stack->port, "TOTALY IGNORING SETUP \n");                                     
2624                                         
2625                                         break;
2626                                 case RESPONSE_IGNORE_SETUP:
2627                                         /* I think we should send CC_RELEASE_CR, but am not sure*/
2628                                         bc->out_cause=16;
2629                                 
2630                                 case RESPONSE_RELEASE_SETUP:
2631                                         misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
2632                                         if (bc->channel>0)
2633                                                 empty_chan_in_stack(stack, bc->channel);
2634                                         empty_bc(bc);
2635                                         bc_state_change(bc,BCHAN_CLEANED);
2636
2637                                         cb_log(0, stack->port, "GOT IGNORE SETUP\n");
2638
2639                                         
2640                                         break;
2641                                 case RESPONSE_OK:
2642                                         cb_log(4, stack->port, "GOT SETUP OK\n");
2643
2644                                         
2645                                         break;
2646                                 default:
2647                                         break;
2648                                 }
2649                         }
2650
2651                         cb_log(5, stack->port, "Freeing Msg on prim:%x \n",frm->prim);
2652
2653                         
2654                         free_msg(msg);
2655                         return 1;
2656 #endif
2657       
2658                 } else {
2659                         cb_log(0, stack->port, " --> Didn't find BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
2660                         struct misdn_bchannel dummybc;
2661                         memset (&dummybc,0,sizeof(dummybc));
2662                         dummybc.port=stack->port;
2663                         dummybc.l3_id=frm->dinfo;
2664                         bc=&dummybc; 
2665                         goto handle_frm_bc;
2666                 }
2667         }
2668
2669         cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x \n",frm->prim);
2670         return 0;
2671 }
2672
2673
2674 static int handle_l1(msg_t *msg)
2675 {
2676         iframe_t *frm = (iframe_t*) msg->data;
2677         struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2678         int i ;
2679         
2680         if (!stack) return 0 ;
2681
2682         switch (frm->prim) {
2683         case PH_ACTIVATE | CONFIRM:
2684         case PH_ACTIVATE | INDICATION:
2685                 cb_log (3, stack->port, "L1: PH L1Link Up!\n");
2686                 stack->l1link=1;
2687                 
2688                 if (stack->nt) {
2689                         
2690                         if (stack->nst.l1_l2(&stack->nst, msg))
2691                                 free_msg(msg);
2692
2693                         if (stack->ptp)
2694                                 misdn_lib_get_l2_up(stack);
2695                 } else {
2696                         free_msg(msg);
2697                 }
2698                 
2699                 for (i=0;i<=stack->b_num; i++) {
2700                         if (stack->bc[i].evq != EVENT_NOTHING) {
2701                                 cb_log(4, stack->port, "Fireing Queued Event %s because L1 got up\n", isdn_get_info(msgs_g, stack->bc[i].evq, 0));
2702                                 misdn_lib_send_event(&stack->bc[i],stack->bc[i].evq);
2703                                 stack->bc[i].evq=EVENT_NOTHING;
2704                         }
2705                         
2706                 }
2707                 return 1;
2708
2709         case PH_ACTIVATE | REQUEST:
2710                 free_msg(msg);
2711                 cb_log(3,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
2712                 return 1;
2713                 
2714         case PH_DEACTIVATE | REQUEST:
2715                 free_msg(msg);
2716                 cb_log(3,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
2717                 return 1;
2718                 
2719         case PH_DEACTIVATE | CONFIRM:
2720         case PH_DEACTIVATE | INDICATION:
2721                 cb_log (3, stack->port, "L1: PH L1Link Down! \n");
2722                 
2723                 for (i=0; i<=stack->b_num; i++) {
2724                         if (global_state == MISDN_INITIALIZED)  {
2725                                 cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
2726                         }
2727                 }
2728                 
2729                 if (stack->nt) {
2730                         if (stack->nst.l1_l2(&stack->nst, msg))
2731                                 free_msg(msg);
2732                 } else {
2733                         free_msg(msg);
2734                 }
2735                 
2736                 stack->l1link=0;
2737                 stack->l2link=0;
2738                 return 1;
2739         }
2740   
2741         return 0;
2742 }
2743
2744 static int handle_l2(msg_t *msg)
2745 {
2746         iframe_t *frm = (iframe_t*) msg->data;
2747
2748         struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2749         
2750         if (!stack) {
2751                 return 0 ;
2752         }
2753         
2754         switch(frm->prim) {
2755
2756         case DL_ESTABLISH | REQUEST:
2757                 cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
2758                 return 1;
2759         case DL_RELEASE | REQUEST:
2760                 cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
2761                 return 1;
2762                 
2763         case DL_ESTABLISH | INDICATION:
2764         case DL_ESTABLISH | CONFIRM:
2765         {
2766                 cb_log (3, stack->port, "L2: L2Link Up! \n");
2767                 if (stack->ptp && stack->l2link) {
2768                         cb_log (-1, stack->port, "L2: L2Link Up! but it's already UP.. must be faulty, blocking port\n"); 
2769                         cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
2770                 }
2771                 stack->l2link=1;
2772                 free_msg(msg);
2773                 return 1;
2774         }
2775         break;
2776     
2777         case DL_RELEASE | INDICATION:
2778         case DL_RELEASE | CONFIRM:
2779         {
2780                 cb_log (3, stack->port, "L2: L2Link Down! \n");
2781                 stack->l2link=0;
2782                 
2783                 free_msg(msg);
2784                 return 1;
2785         }
2786         break;
2787         }
2788         return 0;
2789 }
2790
2791 static int handle_mgmt(msg_t *msg)
2792 {
2793         iframe_t *frm = (iframe_t*) msg->data;
2794
2795         if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
2796                 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
2797                 free_msg(msg);
2798                 return 1;
2799         }
2800         
2801         struct misdn_stack * stack=find_stack_by_addr(frm->addr);
2802         
2803         if (!stack) {
2804                 if (frm->prim == (MGR_DELLAYER|CONFIRM)) {
2805                         cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: %x !\n",
2806                                         frm->addr) ;
2807                         free_msg(msg);
2808                         return 1;
2809                 }
2810                 
2811                 return 0;
2812         }
2813         
2814         switch(frm->prim) {
2815         case MGR_SHORTSTATUS | INDICATION:
2816         case MGR_SHORTSTATUS | CONFIRM:
2817                 cb_log(5, 0, "MGMT: Short status dinfo %x\n",frm->dinfo);
2818                 
2819                 switch (frm->dinfo) {
2820                 case SSTATUS_L1_ACTIVATED:
2821                         cb_log(3, 0, "MGMT: SSTATUS: L1_ACTIVATED \n");
2822                         stack->l1link=1;
2823                 
2824                         break;
2825                 case SSTATUS_L1_DEACTIVATED:
2826                         cb_log(3, 0, "MGMT: SSTATUS: L1_DEACTIVATED \n");
2827                         stack->l1link=0;
2828
2829                         clear_l3(stack);
2830                         break;
2831
2832                 case SSTATUS_L2_ESTABLISHED:
2833                         cb_log(3, stack->port, "MGMT: SSTATUS: L2_ESTABLISH \n");
2834                         stack->l2link=1;
2835                         break;
2836                         
2837                 case SSTATUS_L2_RELEASED:
2838                         cb_log(3, stack->port, "MGMT: SSTATUS: L2_RELEASED \n");
2839                         stack->l2link=0;
2840                         break;
2841                 }
2842                 
2843                 free_msg(msg);
2844                 return 1;
2845                 
2846         case MGR_SETSTACK | INDICATION:
2847                 cb_log(4, stack->port, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
2848                 free_msg(msg);
2849                 return 1;
2850         case MGR_DELLAYER | CONFIRM:
2851                 cb_log(4, stack->port, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
2852                 free_msg(msg);
2853                 return 1;
2854                 
2855         }
2856         
2857         /*
2858         if ( (frm->prim & 0x0f0000) ==  0x0f0000) {
2859         cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
2860         free_msg(msg);
2861         return 1;
2862         } */
2863     
2864         return 0;
2865 }
2866
2867
2868 static msg_t *fetch_msg(int midev) 
2869 {
2870         msg_t *msg=alloc_msg(MAX_MSG_SIZE);
2871         int r;
2872 /*      fd_set rdfs; */
2873
2874         if (!msg) {
2875                 cb_log(0, 0, "fetch_msg: alloc msg failed !!");
2876                 return NULL;
2877         }
2878
2879 #if 0
2880         FD_ZERO(&rdfs);
2881         FD_SET(midev,&rdfs);
2882   
2883         mISDN_select(FD_SETSIZE, &rdfs, NULL, NULL, NULL);
2884         //select(FD_SETSIZE, &rdfs, NULL, NULL, NULL);
2885   
2886         if (FD_ISSET(midev, &rdfs)) {
2887 #endif
2888
2889         AGAIN:
2890                 r=mISDN_read(midev,msg->data,MAX_MSG_SIZE, TIMEOUT_10SEC);
2891                 msg->len=r;
2892     
2893                 if (r==0) {
2894                         free_msg(msg); /* danger, cauz usualy freeing in main_loop */
2895                         cb_log(6,0,"Got empty Msg..\n");
2896                         return NULL;
2897                 }
2898
2899                 if (r<0) {
2900                         if (errno == EAGAIN) {
2901                                 /*we wait for mISDN here*/
2902                                 cb_log(4,0,"mISDN_read wants us to wait\n");
2903                                 usleep(5000);
2904                                 goto AGAIN;
2905                         }
2906                         
2907                         cb_log(0,0,"mISDN_read returned :%d error:%s (%d)\n",r,strerror(errno),errno); 
2908                 }
2909
2910                 return msg;
2911
2912 #if 0
2913         } else {
2914                 printf ("Select timeout\n");
2915         }
2916 #endif
2917   
2918         return NULL;
2919 }
2920
2921 void misdn_lib_isdn_l1watcher(int port)
2922 {
2923         struct misdn_stack *stack;
2924
2925         for (stack = glob_mgr->stack_list; stack && (stack->port != port); stack = stack->next)
2926                 ;
2927
2928         if (stack) {
2929                 cb_log(4, port, "Checking L1 State\n"); 
2930                 if (!stack->l1link) {
2931                         cb_log(4, port, "L1 State Down, trying to get it up again\n");  
2932                         misdn_lib_get_short_status(stack);
2933                         misdn_lib_get_l1_up(stack); 
2934                         misdn_lib_get_l2_up(stack); 
2935                 }
2936         }
2937 }
2938
2939 static void misdn_lib_isdn_event_catcher(void *arg)
2940 {
2941         struct misdn_lib *mgr = arg;
2942         int zero_frm=0 , fff_frm=0 ;
2943         int midev= mgr->midev;
2944         int port=0;
2945         
2946         while (1) {
2947                 msg_t *msg = fetch_msg(midev); 
2948                 iframe_t *frm;
2949                 
2950                 
2951                 if (!msg) continue;
2952                 
2953                 frm = (iframe_t*) msg->data;
2954                 
2955                 /** When we make a call from NT2Ast we get this frames **/
2956                 if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
2957                         zero_frm++; 
2958                         free_msg(msg);
2959                         continue;
2960                 } else {
2961                         if (zero_frm) {
2962                                 cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm);
2963                                 zero_frm = 0 ;
2964                         }
2965                 }
2966                 
2967                 /** I get this sometimes after setup_bc **/
2968                 if (frm->len == 0 &&  frm->dinfo == 0 && frm->prim == 0xffffffff ) {
2969                         fff_frm++; 
2970                         free_msg(msg);
2971                         continue;
2972                 } else {
2973                         if (fff_frm) {
2974                                 cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm);
2975                                 fff_frm = 0 ;
2976                         }
2977                 }
2978                 
2979                 manager_isdn_handler(frm, msg);
2980         }
2981
2982 }
2983
2984
2985 /** App Interface **/
2986
2987 int te_lib_init() {
2988         char buff[1025];
2989         iframe_t *frm=(iframe_t*)buff;
2990         int midev=mISDN_open();
2991         int ret;
2992
2993         memset(buff,0,1025);
2994   
2995         if  (midev<=0) return midev;
2996   
2997 /* create entity for layer 3 TE-mode */
2998         mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
2999         ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
3000   
3001         if (ret < mISDN_HEADER_LEN) {
3002         noentity:
3003                 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n",strerror(errno));
3004                 exit(-1);
3005         }
3006   
3007         entity = frm->dinfo & 0xffff ;
3008   
3009         if (!entity)
3010                 goto noentity;
3011
3012         return midev;
3013   
3014 }
3015
3016 void te_lib_destroy(int midev)
3017 {
3018         char buf[1024];
3019         mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
3020
3021         cb_log(4, 0, "Entetity deleted\n");
3022         mISDN_close(midev);
3023         cb_log(4, 0, "midev closed\n");
3024 }
3025
3026
3027
3028 void misdn_lib_transfer(struct misdn_bchannel* holded_bc)
3029 {
3030         holded_bc->holded=0;
3031 }
3032
3033 struct misdn_bchannel *manager_find_bc_by_pid(int pid)
3034 {
3035         struct misdn_stack *stack;
3036         int i;
3037   
3038         for (stack=glob_mgr->stack_list;
3039              stack;
3040              stack=stack->next) {
3041                 for (i=0; i<=stack->b_num; i++)
3042                         if (stack->bc[i].pid == pid) return &stack->bc[i];
3043         }
3044   
3045         return NULL;
3046 }
3047
3048 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc)
3049 {
3050         struct misdn_stack *stack=get_stack_by_bc(bc);
3051         return find_bc_holded(stack);
3052 }
3053
3054
3055
3056 static void prepare_bc(struct misdn_bchannel*bc, int channel)
3057 {
3058         bc->channel = channel;
3059         bc->channel_preselected = channel?1:0;
3060         bc->in_use = 1;
3061         bc->need_disconnect=1;
3062         bc->need_release=1;
3063         bc->need_release_complete=1;
3064         bc->cause=16;
3065
3066         if (++mypid>5000) mypid=1;
3067         bc->pid=mypid;
3068
3069 #if 0
3070         bc->addr=0;
3071         bc->b_stid=0;
3072         bc->layer_id=0;
3073 #endif
3074 }
3075
3076 struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel, int inout)
3077 {
3078         struct misdn_stack *stack;
3079         int i;
3080         
3081         if (channel < 0 || channel > MAX_BCHANS) {
3082                 cb_log(0,port,"Requested channel out of bounds (%d)\n",channel);
3083                 return NULL;
3084         }
3085
3086         for (stack=glob_mgr->stack_list; stack; stack=stack->next) {
3087     
3088                 if (stack->port == port) {
3089                         if (stack->blocked) {
3090                                 cb_log(0,port,"Port is blocked\n");
3091                                 return NULL;
3092                         }
3093                 
3094                         if (channel > 0) {
3095                                 if (channel <= stack->b_num) {
3096                                         for (i = 0; i < stack->b_num; i++) {
3097                                                 if (stack->bc[i].in_use && stack->bc[i].channel == channel) {
3098                                                         cb_log(0,port,"Requested channel:%d on port:%d is already in use\n",channel, port);
3099                                                         return NULL;
3100                                                 }
3101                                         }
3102                                 } else {
3103                                         cb_log(0,port,"Requested channel:%d is out of bounds on port:%d\n",channel, port);
3104                                         return NULL;
3105                                 }
3106                         }
3107
3108                         int maxnum=inout&&!stack->pri&&!stack->ptp?stack->b_num+1:stack->b_num;
3109                         for (i = 0; i <maxnum; i++) {
3110                                 if (!stack->bc[i].in_use) {
3111                                         /* 3. channel on bri means CW*/
3112                                         if (!stack->pri && i==stack->b_num)
3113                                                 stack->bc[i].cw=1;
3114
3115                                         prepare_bc(&stack->bc[i], channel);
3116                                         return &stack->bc[i];
3117                                 }
3118                         }
3119
3120                         cb_log(1,port,"There is no free channel on port (%d)\n",port);
3121                         return NULL;
3122                 }
3123         }
3124
3125         cb_log(0,port,"Port is not configured (%d)\n",port);
3126         return NULL;
3127 }
3128
3129
3130 static char *fac2str (enum FacFunction func)
3131 {
3132         struct arr_el { 
3133                 enum FacFunction p; 
3134                 char *s ; 
3135         } arr[] = {
3136                 { Fac_None, "Fac_None" },
3137                 { Fac_CD, "Fac_CD"},
3138         };
3139         
3140         int i;
3141         
3142         for (i=0; i < sizeof(arr)/sizeof( struct arr_el) ; i ++)
3143                 if ( arr[i].p==func) return arr[i].s;
3144         
3145         return "unknown";
3146 }
3147
3148 void misdn_lib_log_ies(struct misdn_bchannel *bc)
3149 {
3150         if (!bc) return;
3151
3152         struct misdn_stack *stack=get_stack_by_bc(bc);
3153
3154         if (!stack) return;
3155
3156         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);
3157         
3158         cb_log(2, stack->port,
3159                " --> info_dad:%s onumplan:%c dnumplan:%c rnumplan:%c cpnnumplan:%c\n",
3160                bc->info_dad,
3161                bc->onumplan>=0?'0'+bc->onumplan:' ',
3162                bc->dnumplan>=0?'0'+bc->dnumplan:' ',
3163                bc->rnumplan>=0?'0'+bc->rnumplan:' ',
3164                bc->cpnnumplan>=0?'0'+bc->cpnnumplan:' '
3165                 );
3166         
3167         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);
3168         cb_log(4, stack->port, " --> screen:%d --> pres:%d\n",
3169                         bc->screen, bc->pres);
3170         
3171         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);
3172         
3173         cb_log(4, stack->port, " --> facility:%s out_facility:%s\n",fac2str(bc->fac_in.Function),fac2str(bc->fac_out.Function));
3174
3175         cb_log(5, stack->port, " --> urate:%d rate:%d mode:%d user1:%d\n", bc->urate, bc->rate, bc->mode,bc->user1);
3176         
3177         cb_log(5, stack->port, " --> bc:%x h:%d sh:%d\n", bc, bc->holded, bc->stack_holder);
3178 }
3179
3180 int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
3181 {
3182         msg_t *msg; 
3183         int err = -1 ;
3184         int ret=0;
3185   
3186         if (!bc) goto ERR; 
3187         
3188         struct misdn_stack *stack=get_stack_by_bc(bc);
3189         
3190         if (!stack) {
3191                 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);
3192                 return -1;
3193         }
3194         
3195         cb_log(6,stack->port,"SENDEVENT: stack->nt:%d stack->uperid:%x\n",stack->nt, stack->upper_id);
3196
3197         if ( stack->nt && !stack->l1link) {
3198                 /** Queue Event **/
3199                 bc->evq=event;
3200                 cb_log(1, stack->port, "Queueing Event %s because L1 is down (btw. Activating L1)\n", isdn_get_info(msgs_g, event, 0));
3201                 misdn_lib_get_l1_up(stack);
3202                 return 0;
3203         }
3204         
3205         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);
3206         cb_log(1, stack->port, " --> bc_state:%s\n",bc_state2str(bc->bc_state));
3207         misdn_lib_log_ies(bc);
3208         
3209         switch (event) {
3210         case EVENT_SETUP:
3211                 if (create_process(glob_mgr->midev, bc)<0) {
3212                         cb_log(0,  stack->port, " No free channel at the moment @ send_event\n");
3213
3214                         err=-ENOCHAN;
3215                         goto ERR;
3216                 }
3217 #if 0
3218         if (stack->nt) {
3219                 ret=setup_bc(bc);
3220                 if (ret == -EINVAL) {
3221                         cb_log(0,bc->port,"send_event: setup_bc failed\n");
3222                 }
3223         }
3224 #endif
3225                 break;
3226
3227         case EVENT_PROGRESS:
3228         case EVENT_ALERTING:
3229         case EVENT_PROCEEDING:
3230         case EVENT_SETUP_ACKNOWLEDGE:
3231                 if (!bc->nt && !stack->ptp) break;
3232
3233         case EVENT_CONNECT:
3234         case EVENT_RETRIEVE_ACKNOWLEDGE:
3235
3236                 if (stack->nt) {
3237                         if (bc->channel <=0 ) { /*  else we have the channel already */
3238                                 bc->channel = find_free_chan_in_stack(stack, bc, 0);
3239                                 if (!bc->channel) {
3240                                         cb_log(0, stack->port, " No free channel at the moment\n");
3241                                         
3242                                         err=-ENOCHAN;
3243                                         goto ERR;
3244                                 }
3245                         }
3246                         /* Its that i generate channels */
3247                 }
3248
3249                 ret=setup_bc(bc);
3250                 if (ret == -EINVAL) {
3251                         cb_log(0,bc->port,"send_event: setup_bc failed\n");
3252                 }
3253
3254                 if (misdn_cap_is_speech(bc->capability)) {
3255                         if ((event==EVENT_CONNECT)||(event==EVENT_RETRIEVE_ACKNOWLEDGE)) {
3256                                 if ( *bc->crypt_key ) {
3257                                         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);
3258                                         
3259                                         manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
3260                                 }
3261                                 
3262                                 if (!bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
3263                                 manager_ec_enable(bc);
3264                                 
3265                                 if (bc->txgain != 0) {
3266                                         cb_log(4, stack->port,  "--> Changing txgain to %d\n", bc->txgain);
3267                                         manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
3268                                 }
3269                                 
3270                                 if ( bc->rxgain != 0 ) {
3271                                         cb_log(4, stack->port,  "--> Changing rxgain to %d\n", bc->rxgain);
3272                                         manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
3273                                 }
3274                         }
3275                 }
3276                 break;
3277
3278         case EVENT_HOLD_ACKNOWLEDGE:
3279         {
3280                 struct misdn_bchannel *holded_bc=malloc(sizeof(struct misdn_bchannel));
3281                 if (!holded_bc) {
3282                         cb_log(0,bc->port, "Could not allocate holded_bc!!!\n");
3283                         return -1;
3284                 }
3285
3286                 /*backup the bc*/
3287                 memcpy(holded_bc,bc,sizeof(struct misdn_bchannel));
3288                 holded_bc->holded=1;
3289                 bc_state_change(holded_bc,BCHAN_CLEANED);
3290
3291                 stack_holder_add(stack,holded_bc);
3292         
3293                 /*kill the bridge and clean the bchannel*/
3294                 if (stack->nt) {
3295                         if (bc->bc_state == BCHAN_BRIDGED) {
3296                                 misdn_split_conf(bc,bc->conf_id);
3297                                 struct misdn_bchannel *bc2=find_bc_by_confid(bc->conf_id);
3298                                 if (!bc2) 
3299                                         cb_log(0,bc->port,"We have no second bc in bridge???\n");
3300                                 else 
3301                                         misdn_split_conf(bc2,bc->conf_id);
3302                         }
3303
3304                         if (bc->channel>0)
3305                                 empty_chan_in_stack(stack,bc->channel);
3306                         empty_bc(bc);
3307                         clean_up_bc(bc);
3308                 }
3309                 
3310         }
3311         break;
3312
3313         /* finishing the channel eh ? */
3314         case EVENT_DISCONNECT:
3315                 if (!bc->need_disconnect) {
3316                         cb_log(0,bc->port," --> we have already send Disconnect\n");
3317                         return -1;
3318                 }
3319                 
3320                 bc->need_disconnect=0;
3321                 break;
3322         case EVENT_RELEASE:
3323                 if (!bc->need_release) {
3324                         cb_log(0,bc->port," --> we have already send Release\n");
3325                         return -1;
3326                 }
3327                 bc->need_disconnect=0;
3328                 bc->need_release=0;
3329                 break;
3330         case EVENT_RELEASE_COMPLETE:
3331                 if (!bc->need_release_complete) {
3332                         cb_log(0,bc->port," --> we have already send Release_complete\n");
3333                         return -1;
3334                 }
3335                 bc->need_disconnect=0;
3336                 bc->need_release=0;
3337                 bc->need_release_complete=0;
3338                 break;
3339     
3340         case EVENT_CONNECT_ACKNOWLEDGE:
3341
3342                 if ( bc->nt || misdn_cap_is_speech(bc->capability)) {
3343                         int ret=setup_bc(bc);
3344                         if (ret == -EINVAL){
3345                                 cb_log(0,bc->port,"send_event: setup_bc failed\n");
3346                                 
3347                         }
3348                 }
3349         
3350                 
3351                 if (misdn_cap_is_speech(bc->capability)) {
3352                         if (  !bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
3353                         manager_ec_enable(bc);
3354
3355                         if ( bc->txgain != 0 ) {
3356                                 cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
3357                                 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
3358                         }
3359                         if ( bc->rxgain != 0 ) {
3360                                 cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
3361                                 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
3362                         }
3363                 }
3364                 break;
3365     
3366         default:
3367                 break;
3368         }
3369   
3370         /* Later we should think about sending bchannel data directly to misdn. */
3371         msg = isdn_msg_build_event(msgs_g, bc, event, stack->nt);
3372         msg_queue_tail(&stack->downqueue, msg);
3373         sem_post(&glob_mgr->new_msg);
3374   
3375         return 0;
3376   
3377  ERR:
3378         return -1; 
3379 }
3380
3381
3382 static int handle_err(msg_t *msg)
3383 {
3384         iframe_t *frm = (iframe_t*) msg->data;
3385
3386
3387         if (!frm->addr) {
3388                 static int cnt=0;
3389                 if (!cnt)
3390                         cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x\n",frm->prim,frm->dinfo);
3391                 cnt++;
3392                 if (cnt>100) {
3393                         cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x (already more than 100 of them)\n",frm->prim,frm->dinfo);
3394                         cnt=0;
3395                 }
3396                 
3397                 free_msg(msg);
3398                 return 1;
3399                 
3400         }
3401         
3402         switch (frm->prim) {
3403                 case MGR_SETSTACK|INDICATION:
3404                         return handle_bchan(msg);
3405                 break;
3406
3407                 case MGR_SETSTACK|CONFIRM:
3408                 case MGR_CLEARSTACK|CONFIRM:
3409                         free_msg(msg) ; 
3410                         return 1;
3411                 break;
3412
3413                 case DL_DATA|CONFIRM:
3414                         cb_log(4,0,"DL_DATA|CONFIRM\n");
3415                         free_msg(msg);
3416                         return 1;
3417
3418                 case PH_CONTROL|CONFIRM:
3419                         cb_log(4,0,"PH_CONTROL|CONFIRM\n");
3420                         free_msg(msg);
3421                         return 1;
3422
3423                 case DL_DATA|INDICATION:
3424                 {
3425                         int port=(frm->addr&MASTER_ID_MASK) >> 8;
3426                         int channel=(frm->addr&CHILD_ID_MASK) >> 16;
3427
3428                         /*we flush the read buffer here*/
3429                         
3430                         cb_log(9,0,"BCHAN DATA without BC: addr:%x port:%d channel:%d\n",frm->addr, port,channel);
3431                         
3432                         free_msg(msg) ; 
3433                         return 1;
3434                         
3435                         
3436                         struct misdn_bchannel *bc=find_bc_by_channel( port , channel);
3437
3438                         if (!bc) {
3439                                 struct misdn_stack *stack=find_stack_by_port( port );
3440
3441                                 if (!stack) {
3442                                         cb_log(0,0," --> stack not found\n");
3443                                         free_msg(msg);
3444                                         return 1;
3445                                 }
3446                                 
3447                                 cb_log(0,0," --> bc not found by channel\n");
3448                                 if (stack->l2link)
3449                                         misdn_lib_get_l2_down(stack);
3450
3451                                 if (stack->l1link)
3452                                         misdn_lib_get_l1_down(stack);
3453
3454                                 free_msg(msg);
3455                                 return 1;
3456                         }
3457                         
3458                         cb_log(3,port," --> BC in state:%s\n", bc_state2str(bc->bc_state));
3459                 }
3460         }
3461
3462         return 0;
3463 }
3464
3465 #if 0
3466 static int queue_l2l3(msg_t *msg)
3467 {
3468         iframe_t *frm= (iframe_t*)msg->data;
3469         struct misdn_stack *stack;
3470         stack=find_stack_by_addr( frm->addr );
3471
3472         
3473         if (!stack) {
3474                 return 0;
3475         }
3476
3477         msg_queue_tail(&stack->upqueue, msg);
3478         sem_post(&glob_mgr->new_msg);
3479         return 1;
3480 }
3481 #endif
3482
3483 int manager_isdn_handler(iframe_t *frm ,msg_t *msg)
3484 {  
3485
3486         if (frm->dinfo==(signed long)0xffffffff && frm->prim==(PH_DATA|CONFIRM)) {
3487                 cb_log(0,0,"SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
3488         }
3489
3490         if ( ((frm->addr | ISDN_PID_BCHANNEL_BIT )>> 28 ) == 0x5) {
3491                 if (handle_bchan(msg)) {
3492                         return 0 ;
3493                 }
3494         }       
3495
3496 #ifdef RECV_FRM_SYSLOG_DEBUG
3497         syslog(LOG_NOTICE,"mISDN recv: P(%02d): ADDR:%x PRIM:%x DINFO:%x\n",stack->port, frm->addr, frm->prim, frm->dinfo);
3498 #endif
3499
3500         if (handle_timers(msg)) 
3501                 return 0 ;
3502
3503         
3504         if (handle_mgmt(msg)) 
3505                 return 0 ; 
3506         
3507         if (handle_l2(msg)) 
3508                 return 0 ;
3509
3510         /* Its important to handle l1 AFTER l2  */
3511         if (handle_l1(msg)) 
3512                 return 0 ;
3513         
3514         if (handle_frm_nt(msg)) {
3515                 return 0;
3516         }
3517
3518         if (handle_frm(msg)) {
3519                 return 0;
3520         }
3521
3522         if (handle_err(msg)) {
3523                 return 0 ;
3524         }
3525
3526         cb_log(0, 0, "Unhandled Message: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo);            
3527         free_msg(msg);
3528         
3529
3530         return 0;
3531 }
3532
3533
3534
3535
3536 int misdn_lib_get_port_info(int port)
3537 {
3538         msg_t *msg=alloc_msg(MAX_MSG_SIZE);
3539         iframe_t *frm;
3540         struct misdn_stack *stack=find_stack_by_port(port);
3541         if (!msg) {
3542                 cb_log(0, port, "misgn_lib_get_port: alloc_msg failed!\n");
3543                 return -1;
3544         }
3545         frm=(iframe_t*)msg->data;
3546         if (!stack ) {
3547                 cb_log(0, port, "There is no Stack for this port.\n");
3548                 return -1;
3549         }
3550         /* activate bchannel */
3551         frm->prim = CC_STATUS_ENQUIRY | REQUEST;
3552
3553         frm->addr = stack->upper_id| FLG_MSG_DOWN;
3554
3555         frm->dinfo = 0;
3556         frm->len = 0;
3557   
3558         msg_queue_tail(&glob_mgr->activatequeue, msg);
3559         sem_post(&glob_mgr->new_msg);
3560
3561   
3562         return 0; 
3563 }
3564
3565
3566 int queue_cleanup_bc(struct misdn_bchannel *bc) 
3567 {
3568         msg_t *msg=alloc_msg(MAX_MSG_SIZE);
3569         iframe_t *frm;
3570         if (!msg) {
3571                 cb_log(0, bc->port, "misgn_lib_get_port: alloc_msg failed!\n");
3572                 return -1;
3573         }
3574         frm=(iframe_t*)msg->data;
3575
3576         /* activate bchannel */
3577         frm->prim = MGR_CLEARSTACK| REQUEST;
3578
3579         frm->addr = bc->l3_id;
3580
3581         frm->dinfo = bc->port;
3582         frm->len = 0;
3583   
3584         msg_queue_tail(&glob_mgr->activatequeue, msg);
3585         sem_post(&glob_mgr->new_msg);
3586
3587         return 0; 
3588
3589 }
3590
3591 int misdn_lib_port_restart(int port)
3592 {
<