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