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