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