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