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