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