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