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