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