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