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