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