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