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