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