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