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