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