Merged revisions 58825-58826 via svnmerge from
[asterisk/asterisk.git] / channels / misdn / isdn_lib.c
1 /*
2  * Chan_Misdn -- Channel Driver for Asterisk
3  *
4  * Interface to mISDN
5  *
6  * Copyright (C) 2004, Christian Richter
7  *
8  * Christian Richter <crich@beronet.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14
15 #include <syslog.h>
16 #include <mISDNuser/isdn_debug.h>
17
18 #include "isdn_lib_intern.h"
19 #include "isdn_lib.h"
20
21 void misdn_join_conf(struct misdn_bchannel *bc, int conf_id);
22 void misdn_split_conf(struct misdn_bchannel *bc, int conf_id);
23
24 int queue_cleanup_bc(struct misdn_bchannel *bc) ;
25
26 int misdn_lib_get_l2_up(struct misdn_stack *stack);
27
28 struct misdn_stack* get_misdn_stack( void );
29
30 int misdn_lib_port_is_pri(int port)
31 {
32         struct misdn_stack *stack=get_misdn_stack();
33         for ( ; stack; stack=stack->next) {
34                 if (stack->port == port) {
35                         return stack->pri;
36                 }
37         }
38         
39         return -1;
40 }
41
42
43 int misdn_lib_port_block(int port)
44 {
45         struct misdn_stack *stack=get_misdn_stack();
46         for ( ; stack; stack=stack->next) {
47                 if (stack->port == port) {
48                         stack->blocked=1;
49                         return 0;
50                 }
51         }
52         return -1;
53
54 }
55
56 int misdn_lib_port_unblock(int port)
57 {
58         struct misdn_stack *stack=get_misdn_stack();
59         for ( ; stack; stack=stack->next) {
60                 if (stack->port == port) {
61                         stack->blocked=0;
62                         return 0;
63                 }
64         }
65         return -1;
66
67 }
68
69 int misdn_lib_is_port_blocked(int port)
70 {       
71         struct misdn_stack *stack=get_misdn_stack();
72         for ( ; stack; stack=stack->next) {
73                 if (stack->port == port) {
74                         return stack->blocked;
75                 }
76         }
77         return -1;
78 }
79
80 int misdn_lib_is_ptp(int port)
81 {
82         struct misdn_stack *stack=get_misdn_stack();
83         for ( ; stack; stack=stack->next) {
84                 if (stack->port == port) return stack->ptp;
85         }
86         return -1;
87 }
88
89 int misdn_lib_get_maxchans(int port) 
90 {
91         struct misdn_stack *stack=get_misdn_stack();
92         for ( ; stack; stack=stack->next) {
93                 if (stack->port == port) {
94                         if (stack->pri) 
95                                 return 30;
96                         else
97                                 return 2;
98                 }
99         }
100         return -1;
101 }
102
103
104 struct misdn_stack* get_stack_by_bc(struct misdn_bchannel *bc)
105 {
106         struct misdn_stack *stack=get_misdn_stack();
107
108         if (!bc) return NULL;
109         
110         for ( ; stack; stack=stack->next) {
111                 int i;
112                 for (i=0; i <=stack->b_num; i++) {
113                         if ( bc->port == stack->port) return stack;
114                 }
115         }
116
117         return NULL;
118 }
119
120
121 void get_show_stack_details(int port, char *buf)
122 {
123         struct misdn_stack *stack=get_misdn_stack();
124         
125         for ( ; stack; stack=stack->next) {
126                 if (stack->port == port) break;
127         }
128         
129         if (stack) {
130                 sprintf(buf, "* Port %d Type %s Prot. %s L2Link %s L1Link:%s Blocked:%d", stack->port, stack->nt?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN", stack->l1link?"UP":"DOWN",stack->blocked);
131
132         } else {
133                 buf[0]=0;
134         }
135         
136 }
137
138
139 static int nt_err_cnt =0 ;
140
141 enum global_states {
142         MISDN_INITIALIZING,
143         MISDN_INITIALIZED
144 } ;
145
146 static enum global_states  global_state=MISDN_INITIALIZING;
147
148
149 #include <mISDNuser/net_l2.h>
150 #include <mISDNuser/tone.h>
151 #include <unistd.h>
152 #include <semaphore.h>
153 #include <pthread.h>
154 #include <signal.h>
155
156 #include "isdn_lib.h"
157
158
159 struct misdn_lib {
160         int midev;
161         int midev_nt;
162
163         pthread_t event_thread;
164         pthread_t event_handler_thread;
165
166         void *user_data;
167
168         msg_queue_t upqueue;
169         msg_queue_t activatequeue; 
170   
171         sem_t new_msg;
172   
173         struct misdn_stack *stack_list;
174 } ;
175
176 #ifndef ECHOCAN_ON
177 #define ECHOCAN_ON 123
178 #define ECHOCAN_OFF 124
179 #endif
180
181 #define MISDN_DEBUG 0
182
183 void misdn_tx_jitter(struct misdn_bchannel *bc, int len);
184
185 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id);
186
187 struct misdn_bchannel *find_bc_by_confid(unsigned long confid);
188
189 struct misdn_bchannel *stack_holder_find_bychan(struct misdn_stack *stack, int chan);
190
191 int setup_bc(struct misdn_bchannel *bc);
192
193 int manager_isdn_handler(iframe_t *frm ,msg_t *msg);
194
195 int misdn_lib_port_restart(int port);
196 int misdn_lib_pid_restart(int pid);
197
198 extern struct isdn_msg msgs_g[]; 
199
200 #define ISDN_PID_L3_B_USER 0x430000ff
201 #define ISDN_PID_L4_B_USER 0x440000ff
202
203 /* #define MISDN_IBUF_SIZE 1024 */
204 #define MISDN_IBUF_SIZE 512
205
206 /*  Fine Tuning of Inband  Signalling time */
207 #define TONE_ALERT_CNT 41 /*  1 Sec  */
208 #define TONE_ALERT_SILENCE_CNT 200 /*  4 Sec */
209
210 #define TONE_BUSY_CNT 20 /*  ? */
211 #define TONE_BUSY_SILENCE_CNT 48 /*  ? */
212
213 static int  entity;
214
215 static struct misdn_lib *glob_mgr;
216
217 char tone_425_flip[TONE_425_SIZE];
218 char tone_silence_flip[TONE_SILENCE_SIZE];
219
220 static void misdn_lib_isdn_event_catcher(void *arg);
221 static int handle_event_nt(void *dat, void *arg);
222
223
224 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder);
225 void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder);
226 struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id);
227
228 /* from isdn_lib.h */
229 int init_bc(struct misdn_stack * stack,  struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime);
230 struct misdn_stack* stack_init(int midev,  int port, int ptp);
231 void stack_destroy(struct misdn_stack* stack);
232         /* user iface */
233 int te_lib_init( void ) ; /* returns midev */
234 void te_lib_destroy(int midev) ;
235 struct misdn_bchannel *manager_find_bc_by_pid(int pid);
236 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc);
237 void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len);
238 void manager_clean_bc(struct misdn_bchannel *bc );
239 void manager_bchannel_setup (struct misdn_bchannel *bc);
240 void manager_bchannel_cleanup (struct misdn_bchannel *bc);
241
242 void ec_chunk( struct misdn_bchannel *bc, unsigned char *rxchunk, unsigned char *txchunk, int chunk_size);
243         /* end */
244 int bchdev_echocancel_activate(struct misdn_bchannel* dev);
245 void bchdev_echocancel_deactivate(struct misdn_bchannel* dev);
246 /* end */
247
248
249 static char *bearer2str(int cap) {
250         static char *bearers[]={
251                 "Speech",
252                 "Audio 3.1k",
253                 "Unres Digital",
254                 "Res Digital",
255                 "Unknown Bearer"
256         };
257         
258         switch (cap) {
259         case INFO_CAPABILITY_SPEECH:
260                 return bearers[0];
261                 break;
262         case INFO_CAPABILITY_AUDIO_3_1K:
263                 return bearers[1];
264                 break;
265         case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
266                 return bearers[2];
267                 break;
268         case INFO_CAPABILITY_DIGITAL_RESTRICTED:
269                 return bearers[3];
270                 break;
271         default:
272                 return bearers[4];
273                 break;
274         }
275 }
276
277
278 static char flip_table[256];
279
280 static void init_flip_bits(void)
281 {
282         int i,k;
283         
284         for (i = 0 ; i < 256 ; i++) {
285                 unsigned char sample = 0 ;
286                 for (k = 0; k<8; k++) {
287                         if ( i & 1 << k ) sample |= 0x80 >>  k;
288                 }
289                 flip_table[i] = sample;
290         }
291 }
292
293 static char * flip_buf_bits ( char * buf , int len)
294 {
295         int i;
296         char * start = buf;
297         
298         for (i = 0 ; i < len; i++) {
299                 buf[i] = flip_table[(unsigned char)buf[i]];
300         }
301         
302         return start;
303 }
304
305
306
307
308 static msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */
309 {
310         int i = 0;
311         msg_t *dmsg;
312   
313         while(i < 10)
314         {
315                 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
316                 if (dmsg)
317                         return(dmsg);
318       
319                 if (!i)
320                         printf("cannot allocate memory, trying again...\n");
321                 i++;
322                 usleep(300000);
323         }
324         printf("cannot allocate memory, system overloaded.\n");
325         exit(-1);
326 }
327
328
329
330 msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
331 {
332         int i = 0;
333         msg_t *dmsg;
334         Q931_info_t *qi;
335         iframe_t *frm;
336   
337         if (!ntmode)
338                 size = sizeof(Q931_info_t)+2;
339   
340         while(i < 10) {
341                 if (ntmode) {
342                         dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
343                         if (dmsg) {
344                                 return(dmsg);
345                         }
346                 } else {
347                         dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
348                         if (dmsg)
349                         {
350                                 memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
351                                 frm = (iframe_t *)dmsg->data;
352                                 frm->prim = prim;
353                                 frm->dinfo = dinfo;
354                                 qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
355                                 qi->type = mt;
356                                 return(dmsg);
357                         }
358                 }
359     
360                 if (!i) printf("cannot allocate memory, trying again...\n");
361                 i++;
362                 usleep(300000);
363         }
364         printf("cannot allocate memory, system overloaded.\n");
365         exit(-1);
366 }
367
368
369 static int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
370 {
371         iframe_t *frm;
372         frm = (iframe_t *)dmsg->data;
373         struct misdn_stack *stack=get_stack_by_bc(bc);
374
375         if (!stack) {
376                 cb_log(0,bc->port,"send_msg: IEK!! no stack\n ");
377                 return -1;
378         }
379         
380         frm->addr = (stack->upper_id | FLG_MSG_DOWN);
381         frm->dinfo = bc->l3_id;
382         frm->len = (dmsg->len) - mISDN_HEADER_LEN;
383                                 
384         cb_log(4,stack->port,"Sending msg, prim:%x addr:%x dinfo:%x\n",frm->prim,frm->addr,frm->dinfo);
385
386         mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC);
387         free_msg(dmsg);
388
389         return 0;
390 }
391
392
393 static int mypid=1;
394
395
396 int misdn_cap_is_speech(int cap)
397 /** Poor mans version **/
398 {
399         if ( (cap != INFO_CAPABILITY_DIGITAL_UNRESTRICTED) &&
400              (cap != INFO_CAPABILITY_DIGITAL_RESTRICTED) ) return 1;
401         return 0;
402 }
403
404 int misdn_inband_avail(struct misdn_bchannel *bc)
405 {
406
407         /*if ! early_bconnect we have never inband available*/
408         if ( ! bc->early_bconnect ) return 0;
409         
410         switch (bc->progress_indicator) {
411         case INFO_PI_INBAND_AVAILABLE:
412         case INFO_PI_CALL_NOT_E2E_ISDN:
413         case INFO_PI_CALLED_NOT_ISDN:
414                 return 1;
415         default:
416                 return 0;
417         }
418         return 0;
419 }
420
421
422 static void dump_chan_list(struct misdn_stack *stack)
423 {
424         int i;
425
426         for (i=0; i <= stack->b_num; i++) {
427                 cb_log(6, stack->port, "Idx:%d stack->cchan:%d Chan:%d\n",i,stack->channels[i], i+1);
428         }
429 }
430
431
432
433
434 static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchannel *bc, int channel)
435 {
436         int i;
437
438         cb_log(5,stack->port,"find_free_chan: req_chan:%d\n",channel);
439
440         if (channel < 0 || channel > MAX_BCHANS) {
441                 cb_log(0, stack->port, " !! out of bound call to find_free_chan_in_stack! (ch:%d)\n", channel);
442                 return 0;
443         }
444         
445         channel--;
446   
447         for (i = 0; i < stack->b_num; i++) {
448                 if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 Dchannel ;) and work with chan preselection */
449                         if (!stack->channels[i]) {
450                                 cb_log (3, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", 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 || channel>MAX_BCHANS) {
467                 cb_log(0,stack?stack->port:0, "empty_chan_in_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 #ifdef MISDN_1_2
582         *bc->pipeline = 0;
583 #else
584         bc->ec_enable = 0;
585         bc->ec_deftaps = 128;
586 #endif
587
588         bc->AOCD_need_export = 0;
589
590         bc->orig=0;
591   
592         bc->cause=16;
593         bc->out_cause=16;
594         bc->pres=0 ; /* screened */
595         
596         bc->evq=EVENT_NOTHING;
597
598         bc->progress_coding=0;
599         bc->progress_location=0;
600         bc->progress_indicator=0;
601         
602 /** Set Default Bearer Caps **/
603         bc->capability=INFO_CAPABILITY_SPEECH;
604         bc->law=INFO_CODEC_ALAW;
605         bc->mode=0;
606         bc->rate=0x10;
607         bc->user1=0;
608         bc->urate=0;
609         
610         bc->hdlc=0;
611         
612         
613         bc->info_dad[0] = 0;
614         bc->display[0] = 0;
615         bc->infos_pending[0] = 0;
616         bc->cad[0] = 0;
617         bc->oad[0] = 0;
618         bc->dad[0] = 0;
619         bc->rad[0] = 0;
620         bc->orig_dad[0] = 0;
621         bc->uu[0]=0;
622         bc->uulen=0;
623         
624         bc->fac_in.Function = Fac_None;
625         bc->fac_out.Function = Fac_None;
626         
627         bc->te_choose_channel = 0;
628 }
629
630
631 static int clean_up_bc(struct misdn_bchannel *bc)
632 {
633         int ret=0;
634         unsigned char buff[32];
635         struct misdn_stack * stack;
636
637         cb_log(3, bc?bc->port:0, "$$$ CLEANUP CALLED pid:%d\n", bc?bc->pid:-1);
638         
639         if (!bc  ) return -1;
640         stack=get_stack_by_bc(bc);
641         
642         if (!stack) return -1;
643         
644         switch (bc->bc_state ) {
645         case BCHAN_CLEANED:
646                 cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
647                 return -1;
648                 
649         default:
650                 break;
651         }
652         
653         cb_log(2, stack->port, "$$$ Cleaning up bc with stid :%x pid:%d\n", bc->b_stid, bc->pid);
654         
655         manager_ec_disable(bc);
656
657         manager_bchannel_deactivate(bc);
658
659         mISDN_write_frame(stack->midev, buff, bc->layer_id|FLG_MSG_TARGET|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
660         
661         bc->b_stid = 0;
662         bc_state_change(bc, BCHAN_CLEANED);
663         
664         return ret;
665 }
666
667
668
669 static void clear_l3(struct misdn_stack *stack)
670 {
671         int i;
672
673         for (i=0; i<=stack->b_num; i++) {
674                 if (global_state == MISDN_INITIALIZED)  {
675                         cb_event(EVENT_CLEANUP, &stack->bc[i], NULL); 
676                         empty_chan_in_stack(stack,i+1);
677                         empty_bc(&stack->bc[i]);
678                         clean_up_bc(&stack->bc[i]);
679                 }
680                 
681         } 
682 }
683
684 static int set_chan_in_stack(struct misdn_stack *stack, int channel)
685 {
686
687         cb_log(4,stack->port,"set_chan_in_stack: %d\n",channel);
688         dump_chan_list(stack);
689         if (channel >=1 && channel <= MAX_BCHANS) {
690                 if (!stack->channels[channel-1])
691                         stack->channels[channel-1] = 1;
692                 else {
693                         cb_log(0,stack->port,"channel already in use:%d\n", channel );
694                         return -1;
695                 }
696         } else {
697                 cb_log(0,stack->port,"couldn't set channel %d in\n", channel );
698                 return -1;
699         }
700   
701         return 0;
702 }
703
704 #if 0
705 static int chan_in_stack_free(struct misdn_stack *stack, int channel)
706 {
707         if (stack->channels[channel-1])
708                 return 0;
709   
710         return 1;
711 }
712 #endif
713
714
715 static int newteid=0;
716
717 #define MAXPROCS 0x100
718
719 static int misdn_lib_get_l1_down(struct misdn_stack *stack)
720 {
721         /* Pull Up L1 */ 
722         iframe_t act;
723         act.prim = PH_DEACTIVATE | REQUEST; 
724         act.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
725
726         
727         act.dinfo = 0;
728         act.len = 0;
729
730         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
731
732
733 }
734
735
736 static int misdn_lib_get_l2_down(struct misdn_stack *stack)
737 {
738         
739         if (stack->ptp && (stack->nt) ) {
740                 msg_t *dmsg;
741                 /* L2 */
742                 dmsg = create_l2msg(DL_RELEASE| REQUEST, 0, 0);
743                 
744                 if (stack->nst.manager_l3(&stack->nst, dmsg))
745                         free_msg(dmsg);
746                 
747         } else {
748                 iframe_t act;
749                 
750                 act.prim = DL_RELEASE| REQUEST;
751                 act.addr = (stack->upper_id |FLG_MSG_DOWN)  ;
752                 
753                 act.dinfo = 0;
754                 act.len = 0;
755                 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
756         }
757         
758         return 0;
759 }
760
761
762 static int misdn_lib_get_l1_up(struct misdn_stack *stack)
763 {
764         /* Pull Up L1 */ 
765         iframe_t act;
766         act.prim = PH_ACTIVATE | REQUEST; 
767         act.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
768
769         
770         act.dinfo = 0;
771         act.len = 0;
772
773         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
774
775 }
776
777 int misdn_lib_get_l2_up(struct misdn_stack *stack)
778 {
779         
780         if (stack->ptp && (stack->nt) ) {
781                 msg_t *dmsg;
782                 /* L2 */
783                 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
784                 
785                 if (stack->nst.manager_l3(&stack->nst, dmsg))
786                         free_msg(dmsg);
787                 
788         } else {
789                 iframe_t act;
790                 
791                 act.prim = DL_ESTABLISH | REQUEST;
792                 act.addr = (stack->upper_id |FLG_MSG_DOWN)  ;
793                 
794                 act.dinfo = 0;
795                 act.len = 0;
796                 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
797         }
798         
799         return 0;
800 }
801
802 #if 0
803 static int misdn_lib_get_l2_te_ptp_up(struct misdn_stack *stack)
804 {
805         iframe_t act;
806                 
807         act.prim = DL_ESTABLISH | REQUEST;
808         act.addr = (stack->upper_id  & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
809                 
810         act.dinfo = 0;
811         act.len = 0;
812         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
813         return 0;
814 }
815 #endif
816
817 static int misdn_lib_get_short_status(struct misdn_stack *stack)
818 {
819         iframe_t act;
820         
821         
822         act.prim = MGR_SHORTSTATUS | REQUEST; 
823         
824         act.addr = (stack->upper_id | MSG_BROADCAST)  ;
825
826         act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
827         
828         act.len = 0;
829         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
830 }
831
832
833
834 static int create_process (int midev, struct misdn_bchannel *bc) {
835         iframe_t ncr;
836         int l3_id;
837         int i;
838         struct misdn_stack *stack=get_stack_by_bc(bc);
839   
840         if (stack->nt) {
841                 if (!find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0))
842                         return -1;
843                 /*bc->channel=free_chan;*/
844
845                 if (set_chan_in_stack(stack ,bc->channel)<0) return -1;
846                 
847                 cb_log(4,stack->port, " -->  found channel: %d\n", bc->channel);
848     
849                 for (i=0; i <= MAXPROCS; i++)
850                         if (stack->procids[i]==0) break;
851     
852                 if (i== MAXPROCS) {
853                         cb_log(0, stack->port, "Couldnt Create New ProcId.\n");
854                         return -1;
855                 }
856                 stack->procids[i]=1;
857
858                 l3_id = 0xff00 | i;
859     
860                 ncr.prim = CC_NEW_CR | REQUEST; 
861
862                 ncr.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
863
864                 ncr.dinfo = l3_id;
865                 ncr.len = 0;
866
867                 bc->l3_id = l3_id;
868                 cb_log(3, stack->port, " --> new_l3id %x\n",l3_id);
869     
870         } else { 
871                 if (stack->ptp || bc->te_choose_channel) {
872                         /* we know exactly which channels are in use */
873                         if (!find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0))
874                                 return -1;
875                         /*bc->channel=free_chan;*/
876                         cb_log(2,stack->port, " -->  found channel: %d\n", bc->channel);
877                         if (set_chan_in_stack(stack ,bc->channel)<0) return -1;
878                 } else {
879                         /* other phones could have made a call also on this port (ptmp) */
880                         bc->channel=0xff;
881                 }
882     
883     
884                 /* if we are in te-mode, we need to create a process first */
885                 if (newteid++ > 0xffff)
886                         newteid = 0x0001;
887     
888                 l3_id = (entity<<16) | newteid;
889                 /* preparing message */
890                 ncr.prim = CC_NEW_CR | REQUEST; 
891
892                 ncr.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
893
894                 ncr.dinfo =l3_id;
895                 ncr.len = 0;
896                 /* send message */
897
898                 bc->l3_id = l3_id;
899                 cb_log(3, stack->port, "--> new_l3id %x\n",l3_id);
900     
901                 mISDN_write(midev, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
902         }
903   
904         return l3_id;
905 }
906
907
908 void misdn_lib_setup_bc(struct misdn_bchannel *bc)
909 {
910         clean_up_bc(bc);
911         setup_bc(bc);
912 }
913
914
915 int setup_bc(struct misdn_bchannel *bc)
916 {
917         unsigned char buff[1025];
918   
919         mISDN_pid_t pid;
920         int ret;
921         
922
923         struct misdn_stack *stack=get_stack_by_bc(bc);
924
925         if (!stack) {
926                 cb_log(0, bc->port, "setup_bc: NO STACK FOUND!!\n");
927                 return -1;
928         }
929         
930         int midev=stack->midev;
931         int channel=bc->channel-1-(bc->channel>16);
932         int b_stid=stack->b_stids[channel>=0?channel:0];
933
934
935         switch (bc->bc_state) {
936                 case BCHAN_CLEANED:
937                         break;
938                 default:
939                         cb_log(4, stack->port, "$$$ bc already upsetted stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) );
940                         return -1;
941         }
942         
943         cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
944         
945         if (b_stid <= 0) {
946                 cb_log(0, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
947                 
948                 bc_state_change(bc,BCHAN_ERROR);
949                 return 1;
950         }
951         
952         
953         bc->b_stid = b_stid;
954         
955         {
956                 layer_info_t li;
957                 memset(&li, 0, sizeof(li));
958     
959                 li.object_id = -1;
960                 li.extentions = 0;
961                 
962                 li.st = bc->b_stid; /*  given idx */
963
964
965 #define MISDN_DSP
966 #ifndef MISDN_DSP
967                 bc->nodsp=1;
968 #endif
969                 if ( bc->hdlc || bc->nodsp) {
970                         cb_log(4, stack->port,"setup_bc: without dsp\n");
971                         { 
972                                 int l = sizeof(li.name);
973                                 strncpy(li.name, "B L3", l);
974                                 li.name[l-1] = 0;
975                         }
976                         li.pid.layermask = ISDN_LAYER((3));
977                         li.pid.protocol[3] = ISDN_PID_L3_B_USER;
978                         
979                         bc->layer=3;
980                 } else {
981                         cb_log(4, stack->port,"setup_bc: with dsp\n");
982                         { 
983                                 int l = sizeof(li.name);
984                                 strncpy(li.name, "B L4", l);
985                                 li.name[l-1] = 0;
986                         }
987                         li.pid.layermask = ISDN_LAYER((4));
988                         li.pid.protocol[4] = ISDN_PID_L4_B_USER
989 ;
990                         bc->layer=4;
991                         
992                 }  
993                 
994                 ret = mISDN_new_layer(midev, &li);
995                 if (ret ) {
996                         cb_log(0, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
997
998                         bc_state_change(bc,BCHAN_ERROR);
999                         return(-EINVAL);
1000                 }
1001                 
1002                 bc->layer_id = li.id;
1003         }
1004         
1005         memset(&pid, 0, sizeof(pid));
1006         
1007         
1008         
1009         cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
1010         
1011         if (bc->nodsp) {
1012                 cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\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_USER;
1016                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
1017                 
1018         } else if ( bc->hdlc ) {
1019                 cb_log(2, stack->port," --> HDLC Mode\n");
1020                 pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
1021                 pid.protocol[2] = ISDN_PID_L2_B_TRANS  ;
1022                 pid.protocol[3] = ISDN_PID_L3_B_USER;
1023                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
1024         } else {
1025                 cb_log(2, stack->port," --> TRANSPARENT Mode\n");
1026                 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1027                 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1028                 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1029                 pid.protocol[4] = ISDN_PID_L4_B_USER;
1030                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1031                 
1032         } 
1033
1034         ret = mISDN_set_stack(midev, bc->b_stid, &pid);
1035
1036         if (ret){
1037                 cb_log(0, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
1038                 
1039                 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1040                 
1041                 bc_state_change(bc,BCHAN_ERROR);
1042                 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1043                 return(-EINVAL);
1044         }
1045
1046         ret = mISDN_get_setstack_ind(midev, bc->layer_id);
1047
1048         if (ret) {
1049                 cb_log(0, stack->port,"$$$ Set StackIND 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         ret = mISDN_get_layerid(midev, bc->b_stid, bc->layer) ;
1058
1059         bc->addr = ret>0? ret : 0;
1060
1061         if (!bc->addr) {
1062                 cb_log(0, stack->port,"$$$ Get Layerid Err: %d %s\n",ret,strerror(errno));
1063                 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1064                 
1065                 bc_state_change(bc,BCHAN_ERROR);
1066                 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1067                 return (-EINVAL);
1068         }
1069
1070         manager_bchannel_activate(bc);
1071         
1072         bc_state_change(bc,BCHAN_ACTIVATED);
1073
1074         return 0;
1075 }
1076
1077
1078
1079 /** IFACE **/
1080 int init_bc(struct misdn_stack *stack,  struct misdn_bchannel *bc, int midev, int port, int bidx,  char *msn, int firsttime)
1081 {
1082         unsigned char buff[1025];
1083         iframe_t *frm = (iframe_t *)buff;
1084         int ret;
1085   
1086         if (!bc) return -1;
1087   
1088         cb_log(8, port, "Init.BC %d.\n",bidx);
1089         
1090         memset(bc, 0,sizeof(struct misdn_bchannel));
1091         
1092         if (msn) {
1093                 int l = sizeof(bc->msn);
1094                 strncpy(bc->msn,msn, l);
1095                 bc->msn[l-1] = 0;
1096         }
1097         
1098         
1099         empty_bc(bc);
1100         bc_state_change(bc, BCHAN_CLEANED);
1101         
1102         bc->port=stack->port;
1103         bc->nt=stack->nt?1:0;
1104         
1105         {
1106                 ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
1107
1108                 if (!ibuf) return -1;
1109                 
1110                 clear_ibuffer( ibuf);
1111                 
1112                 ibuf->rsem=malloc(sizeof(sem_t));
1113                 
1114                 bc->astbuf=ibuf;
1115
1116                 if (sem_init(ibuf->rsem,1,0)<0)
1117                         sem_init(ibuf->rsem,0,0);
1118                 
1119         }
1120         
1121         
1122         
1123         
1124         {
1125                 stack_info_t *stinf;
1126                 ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
1127                 if (ret < 0) {
1128                         cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1129                         return -1;
1130                 }
1131     
1132                 stinf = (stack_info_t *)&frm->data.p;
1133     
1134                 cb_log(8, port, " --> Child %x\n",stinf->child[bidx]);
1135         }
1136   
1137         return 0;
1138 }
1139
1140
1141
1142 struct misdn_stack* stack_init( int midev, int port, int ptp )
1143 {
1144         int ret;
1145         unsigned char buff[1025];
1146         iframe_t *frm = (iframe_t *)buff;
1147         stack_info_t *stinf;
1148         int i; 
1149         layer_info_t li;
1150
1151         struct misdn_stack *stack = malloc(sizeof(struct misdn_stack));
1152         if (!stack ) return NULL;
1153
1154
1155         cb_log(8, port, "Init. Stack.\n");
1156   
1157         memset(stack,0,sizeof(struct misdn_stack));
1158   
1159         for (i=0; i<MAX_BCHANS + 1; i++ ) stack->channels[i]=0;
1160         
1161         stack->port=port;
1162         stack->midev=midev;
1163         stack->ptp=ptp;
1164   
1165         stack->holding=NULL;
1166         stack->pri=0;
1167   
1168         msg_queue_init(&stack->downqueue);
1169         msg_queue_init(&stack->upqueue);
1170   
1171         /* query port's requirements */
1172         ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
1173         if (ret < 0) {
1174                 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1175                 return(NULL);
1176         }
1177   
1178         stinf = (stack_info_t *)&frm->data.p;
1179
1180         stack->d_stid = stinf->id;
1181         stack->b_num = stinf->childcnt;
1182
1183         for (i=0; i<=stinf->childcnt; i++)
1184                 stack->b_stids[i] = stinf->child[i];
1185   
1186         switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
1187         case ISDN_PID_L0_TE_S0:
1188                 stack->nt=0;
1189                 break;
1190         case ISDN_PID_L0_NT_S0:
1191                 cb_log(8, port, "NT Stack\n");
1192
1193                 stack->nt=1;
1194                 break;
1195
1196         case ISDN_PID_L0_TE_U:
1197                 break;
1198         case ISDN_PID_L0_NT_U:
1199                 break;
1200         case ISDN_PID_L0_TE_UP2:
1201                 break;
1202         case ISDN_PID_L0_NT_UP2:
1203                 break;
1204         case ISDN_PID_L0_TE_E1:
1205                 cb_log(8, port, "TE S2M Stack\n");
1206                 stack->nt=0;
1207                 stack->pri=1;
1208                 break;
1209         case ISDN_PID_L0_NT_E1:
1210                 cb_log(8, port, "TE S2M Stack\n");
1211                 stack->nt=1;
1212                 stack->pri=1;
1213                 
1214                 break;
1215         default:
1216                 cb_log(0, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
1217
1218         }
1219
1220         if (!stack->nt) {
1221                 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) { 
1222                         stack->ptp = 1;
1223                 } else {
1224                         stack->ptp = 0;
1225                 }
1226         }
1227         
1228         {
1229                 int ret;
1230                 int nt=stack->nt;
1231
1232                 cb_log(8, port, "Init. Stack.\n");
1233                 
1234                 memset(&li, 0, sizeof(li));
1235                 {
1236                         int l = sizeof(li.name);
1237                         strncpy(li.name,nt?"net l2":"user l4", l);
1238                         li.name[l-1] = 0;
1239                 }
1240                 li.object_id = -1;
1241                 li.extentions = 0;
1242                 li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1243                 li.pid.layermask = ISDN_LAYER((nt?2:4));
1244                 li.st = stack->d_stid;
1245                 
1246                 
1247                 ret = mISDN_new_layer(midev, &li);
1248                 if (ret) {
1249                         cb_log(0, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
1250                         return(NULL);
1251                 }
1252                 
1253                 
1254                 stack->upper_id = li.id;
1255                 ret = mISDN_register_layer(midev, stack->d_stid, stack->upper_id);
1256                 if (ret)
1257                 {
1258                         cb_log(0,port,"Cannot register layer %d of this port.\n", nt?2:4);
1259                         return(NULL);
1260                 }
1261                 
1262                 stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3); 
1263                 if (stack->lower_id < 0) {
1264                         cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
1265                         return(NULL);
1266                 }
1267                 
1268                 stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
1269                 if (stack->upper_id < 0) {
1270                         cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, 2);
1271                         return(NULL);
1272                 }
1273                 
1274                 cb_log(8, port, "NT Stacks upper_id %x\n",stack->upper_id);
1275                 
1276                 
1277                 /* create nst (nt-mode only) */
1278                 if (nt) {
1279                         
1280                         memset(&stack->nst, 0, sizeof(net_stack_t));
1281                         memset(&stack->mgr, 0, sizeof(manager_t));
1282     
1283                         stack->mgr.nst = &stack->nst;
1284                         stack->nst.manager = &stack->mgr;
1285     
1286                         stack->nst.l3_manager = handle_event_nt;
1287                         stack->nst.device = midev;
1288                         stack->nst.cardnr = port;
1289                         stack->nst.d_stid = stack->d_stid;
1290     
1291                         stack->nst.feature = FEATURE_NET_HOLD;
1292                         if (stack->ptp)
1293                                 stack->nst.feature |= FEATURE_NET_PTP;
1294                         if (stack->pri)
1295                                 stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1296                         
1297                         stack->nst.l1_id = stack->lower_id;
1298                         stack->nst.l2_id = stack->upper_id;
1299                         
1300                         msg_queue_init(&stack->nst.down_queue);
1301                         
1302                         Isdnl2Init(&stack->nst);
1303                         Isdnl3Init(&stack->nst);
1304                         
1305                 } 
1306                 
1307                 if (!stack->nt) {
1308                         /*assume L1 is up, we'll get DEACTIVATES soon, for non
1309                          * up L1s*/
1310                         stack->l1link=0;
1311                 }
1312                 stack->l1link=0;
1313                 stack->l2link=0;
1314 #if 0   
1315                 if (!stack->nt) {
1316                         misdn_lib_get_short_status(stack);
1317                 } else {
1318                         misdn_lib_get_l1_up(stack);
1319                         if (!stack->ptp) misdn_lib_get_l1_up(stack);
1320                         misdn_lib_get_l2_up(stack);
1321                 }
1322 #endif
1323
1324                 misdn_lib_get_short_status(stack);
1325                 misdn_lib_get_l1_up(stack);
1326                 misdn_lib_get_l2_up(stack);
1327                 
1328         }
1329
1330         cb_log(8,0,"stack_init: port:%d lowerId:%x  upperId:%x\n",stack->port,stack->lower_id, stack->upper_id);
1331         
1332         return stack;
1333 }
1334
1335
1336 void stack_destroy(struct misdn_stack* stack)
1337 {
1338         char buf[1024];
1339         if (!stack) return;
1340
1341         if (stack->nt) {
1342                 cleanup_Isdnl2(&stack->nst);
1343                 cleanup_Isdnl3(&stack->nst);
1344         }
1345   
1346         if (stack->lower_id) 
1347                 mISDN_write_frame(stack->midev, buf, stack->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1348
1349         if (stack->upper_id) 
1350                 mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1351 }
1352
1353
1354 static struct misdn_stack * find_stack_by_addr(int  addr)
1355 {
1356         struct misdn_stack *stack;
1357         
1358         for (stack=glob_mgr->stack_list;
1359              stack;
1360              stack=stack->next) {
1361                 if ( (stack->upper_id&STACK_ID_MASK) == (addr&STACK_ID_MASK)) return stack;
1362
1363         }
1364   
1365         return NULL;
1366 }
1367
1368
1369 static struct misdn_stack * find_stack_by_port(int port)
1370 {
1371         struct misdn_stack *stack;
1372   
1373         for (stack=glob_mgr->stack_list;
1374              stack;
1375              stack=stack->next) 
1376                 if (stack->port == port) return stack;
1377   
1378         return NULL;
1379 }
1380
1381 static struct misdn_stack * find_stack_by_mgr(manager_t* mgr_nt)
1382 {
1383         struct misdn_stack *stack;
1384   
1385         for (stack=glob_mgr->stack_list;
1386              stack;
1387              stack=stack->next) 
1388                 if ( &stack->mgr == mgr_nt) return stack;
1389   
1390         return NULL;
1391 }
1392
1393 static struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
1394 {
1395         int i;
1396         for (i=0; i<=stack->b_num; i++) {
1397                 if ( (stack->bc[i].l3_id & mask)  ==  (l3id & mask)) return &stack->bc[i] ;
1398         }
1399         return stack_holder_find(stack,l3id);
1400 }
1401
1402
1403 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
1404 {
1405         int i;
1406         for (i=0; i<=stack->b_num; i++) {
1407                 if (stack->bc[i].l3_id == l3id) return &stack->bc[i] ;
1408         }
1409         return stack_holder_find(stack,l3id);
1410 }
1411
1412 static struct misdn_bchannel *find_bc_holded(struct misdn_stack *stack)
1413 {
1414         int i;
1415         for (i=0; i<=stack->b_num; i++) {
1416                 if (stack->bc[i].holded ) return &stack->bc[i] ;
1417         }
1418         return NULL;
1419 }
1420
1421
1422 static struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
1423 {
1424         struct misdn_stack* stack;
1425         int i;
1426
1427         for (stack=glob_mgr->stack_list;
1428              stack;
1429              stack=stack->next) {
1430                 for (i=0; i<=stack->b_num; i++) {
1431                         if ( (stack->bc[i].addr&STACK_ID_MASK)==(addr&STACK_ID_MASK) ||  stack->bc[i].layer_id== addr ) {
1432                                 return &stack->bc[i];
1433                         }
1434                 }
1435         }
1436         
1437         return NULL;
1438 }
1439
1440 struct misdn_bchannel *find_bc_by_confid(unsigned long confid)
1441 {
1442         struct misdn_stack* stack;
1443         int i;
1444         
1445         for (stack=glob_mgr->stack_list;
1446              stack;
1447              stack=stack->next) {
1448                 for (i=0; i<=stack->b_num; i++) {
1449                         if ( stack->bc[i].conf_id==confid ) {
1450                                 return &stack->bc[i];
1451                         }
1452                 }
1453         }
1454         return NULL;
1455 }
1456
1457
1458 static struct misdn_bchannel *find_bc_by_channel(int port, int channel)
1459 {
1460         struct misdn_stack* stack=find_stack_by_port(port);
1461         int i;
1462
1463         if (!stack) return NULL;        
1464         
1465         for (i=0; i<=stack->b_num; i++) {
1466                 if ( stack->bc[i].channel== channel ) {
1467                         return &stack->bc[i];
1468                 }
1469         }
1470                 
1471         return NULL;
1472 }
1473
1474
1475
1476
1477
1478 static int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
1479 {
1480         struct misdn_stack *stack=get_stack_by_bc(bc);
1481         
1482         if (!stack->nt) {
1483                 
1484                 switch (event) {
1485
1486                 case EVENT_CONNECT_ACKNOWLEDGE:
1487 #if 0
1488                         if ( !misdn_cap_is_speech(bc->capability)) {
1489                                 int ret=setup_bc(bc);
1490                                 if (ret == -EINVAL){
1491                                         cb_log(0,bc->port,"send_event: setup_bc failed\n");
1492                                 }
1493                         }
1494 #endif  
1495                         break;
1496                 case EVENT_CONNECT:
1497
1498                         if ( *bc->crypt_key ) {
1499                                 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);
1500                                 manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
1501                         }
1502                 case EVENT_ALERTING:
1503                 case EVENT_PROGRESS:
1504                 case EVENT_PROCEEDING:
1505                 case EVENT_SETUP_ACKNOWLEDGE:
1506
1507                 setup_bc(bc);
1508
1509                 case EVENT_SETUP:
1510                 {
1511                         if (bc->channel == 0xff) {
1512                                 if (!find_free_chan_in_stack(stack, bc, 0)) {
1513                                         cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n");
1514                                         bc->out_cause=34;
1515                                         misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1516                                         return -1;
1517                                 }
1518                         } 
1519
1520                         if (bc->channel >0 && bc->channel<255) {
1521                                 int ret=set_chan_in_stack(stack ,bc->channel);
1522                                 if (event == EVENT_SETUP && ret<0){
1523                                         /* empty bchannel */
1524                                         bc->channel=0;
1525                                         bc->out_cause=44;
1526                                         misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1527                                         return -1;
1528                                 }
1529                         }
1530                 }
1531                 break;
1532
1533                 case EVENT_RELEASE_COMPLETE:
1534                 case EVENT_RELEASE:
1535                         if (bc->channel>0)
1536                                 empty_chan_in_stack(stack,bc->channel);
1537                         int tmpcause=bc->cause; 
1538                         int tmp_out_cause=bc->out_cause;        
1539                         empty_bc(bc);
1540                         bc->cause=tmpcause;
1541                         bc->out_cause=tmp_out_cause;
1542                         clean_up_bc(bc);
1543                         break;
1544                 default:
1545                         break;
1546                 }
1547         } else {    /** NT MODE **/
1548                 
1549         }
1550         return 0;
1551 }
1552
1553 static int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
1554 {
1555         if (!stack) return -1;
1556   
1557         switch (frm->prim) {
1558         case CC_NEW_CR|INDICATION:
1559                 cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
1560
1561                 struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0, 1);
1562                 if (!bc) {
1563                         cb_log(0, stack->port, " --> !! lib: No free channel!\n");
1564                         return -1;
1565                 }
1566   
1567                 cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
1568                 bc->l3_id=frm->dinfo;
1569                 return 1;
1570         case CC_NEW_CR|CONFIRM:
1571                 return 1;
1572         case CC_NEW_CR|REQUEST:
1573                 return 1;
1574         case CC_RELEASE_CR|REQUEST:
1575                 return 1;
1576         case CC_RELEASE_CR|CONFIRM:
1577                 break;
1578         case CC_RELEASE_CR|INDICATION:
1579                 cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n",frm->dinfo);
1580                 {
1581                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, frm->dinfo);
1582                         struct misdn_bchannel dummybc;
1583       
1584                         if (!bc) {
1585                                 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
1586                                 memset (&dummybc,0,sizeof(dummybc));
1587                                 dummybc.port=stack->port;
1588                                 dummybc.l3_id=frm->dinfo;
1589                                 dummybc.nt=stack->nt;
1590                                 bc=&dummybc; 
1591                         }
1592       
1593                         if (bc) {
1594                                 cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo);
1595                                 if (bc->channel>0)
1596                                         empty_chan_in_stack(stack,bc->channel);
1597                                 empty_bc(bc);
1598                                 
1599                                 clean_up_bc(bc);
1600                                 
1601                                 dump_chan_list(stack);
1602                                 /*bc->pid = 0;*/
1603                                 bc->need_disconnect=0;
1604                                 bc->need_release=0;
1605                                 bc->need_release_complete=0;
1606                                 
1607                                 cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
1608                                 if (bc->stack_holder) {
1609                                         cb_log(4,stack->port, "REMOVEING Holder\n");
1610                                         stack_holder_remove( stack, bc);
1611                                         free(bc);
1612                                 }
1613                         }
1614                         else {
1615                                 if (stack->nt) 
1616                                         cb_log(4, stack->port, "BC with dinfo: %x  not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr);
1617                         }
1618       
1619                         return 1;
1620                 }
1621                 break;
1622         }
1623   
1624         return 0;
1625 }
1626
1627
1628 /*Emptys bc if it's reserved (no SETUP out yet)*/
1629 void misdn_lib_release(struct misdn_bchannel *bc)
1630 {
1631         struct misdn_stack *stack=get_stack_by_bc(bc);
1632
1633         if (!stack) {
1634                 cb_log(1,0,"misdn_release: No Stack found\n");
1635                 return;
1636         }
1637         
1638         if (bc->channel>0) {
1639                 empty_chan_in_stack(stack,bc->channel);
1640         }
1641         empty_bc(bc);
1642         clean_up_bc(bc);
1643 }
1644
1645
1646
1647
1648 int misdn_lib_get_port_up (int port) 
1649 { /* Pull Up L1 */ 
1650         struct misdn_stack *stack;
1651         
1652         for (stack=glob_mgr->stack_list;
1653              stack;
1654              stack=stack->next) {
1655                 
1656                 if (stack->port == port) {
1657
1658                         if (!stack->l1link)
1659                                 misdn_lib_get_l1_up(stack);
1660                         if (!stack->l2link)
1661                                 misdn_lib_get_l2_up(stack);
1662                         
1663                         return 0;
1664                 }
1665         }
1666         return 0;
1667 }
1668
1669
1670 int misdn_lib_get_port_down (int port) 
1671 { /* Pull Down L1 */ 
1672         struct misdn_stack *stack;
1673         for (stack=glob_mgr->stack_list;
1674              stack;
1675              stack=stack->next) {
1676                 if (stack->port == port) {
1677                                 if (stack->l2link)
1678                                         misdn_lib_get_l2_down(stack);
1679                                 misdn_lib_get_l1_down(stack);
1680                         return 0;
1681                 }
1682         }
1683         return 0;
1684 }
1685
1686 int misdn_lib_port_up(int port, int check)
1687 {
1688         struct misdn_stack *stack;
1689
1690
1691         for (stack=glob_mgr->stack_list;
1692              stack;
1693              stack=stack->next) {
1694                 
1695                 if (stack->port == port) {
1696
1697                         if (stack->blocked) {
1698                                 cb_log(0,port, "Port Blocked:%d L2:%d L1:%d\n", stack->blocked, stack->l2link, stack->l1link);
1699                                 return -1;
1700                         }
1701
1702                         if (stack->ptp ) {
1703
1704                                 if (stack->l1link && stack->l2link) {
1705                                         return 1;
1706                                 } else {
1707                                         cb_log(0,port, "Port Down L2:%d L1:%d\n",
1708                                                 stack->l2link, stack->l1link);
1709                                         return 0;
1710                                 }
1711                         } else {
1712                                 if ( !check || stack->l1link )
1713                                         return 1;
1714                                 else {
1715                                         cb_log(0,port, "Port down PMP\n");
1716                                         return 0;
1717                                 }
1718                         }
1719                 }
1720         }
1721   
1722         return -1;
1723 }
1724
1725
1726 int
1727 handle_event_nt(void *dat, void *arg)
1728 {
1729         manager_t *mgr = (manager_t *)dat;
1730         msg_t *msg = (msg_t *)arg;
1731         mISDNuser_head_t *hh;
1732         int reject=0;
1733
1734         struct misdn_stack *stack=find_stack_by_mgr(mgr);
1735         int port;
1736
1737         if (!msg || !mgr)
1738                 return(-EINVAL);
1739
1740         hh=(mISDNuser_head_t*)msg->data;
1741         port=stack->port;
1742         
1743         cb_log(5, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1744         {
1745                 switch(hh->prim){
1746                 case CC_RETRIEVE|INDICATION:
1747                 {
1748                         iframe_t frm; /* fake te frm to add callref to global callreflist */
1749                         frm.dinfo = hh->dinfo;
1750
1751                         frm.addr=stack->upper_id | FLG_MSG_DOWN;
1752
1753                         frm.prim = CC_NEW_CR|INDICATION;
1754                         
1755                         if (handle_cr( stack, &frm)< 0) {
1756                                 msg_t *dmsg;
1757                                 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1758                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1759                                 stack->nst.manager_l3(&stack->nst, dmsg);
1760                                 free_msg(msg);
1761                                 return 0;
1762                         }
1763                         
1764                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1765                         struct misdn_bchannel *hold_bc=stack_holder_find(stack,bc->l3_id);
1766                         cb_log(4, stack->port, "bc_l3id:%x holded_bc_l3id:%x\n",bc->l3_id, hold_bc->l3_id);
1767
1768                         if (hold_bc) {
1769                                 cb_log(4, stack->port, "REMOVEING Holder\n");
1770
1771                                 /*swap the backup to our new channel back*/
1772                                 stack_holder_remove(stack, hold_bc);
1773                                 memcpy(bc,hold_bc,sizeof(struct misdn_bchannel));
1774                                 free(hold_bc);
1775
1776                                 bc->holded=0;
1777                         }
1778                         
1779                 }
1780                         
1781                         break;
1782                         
1783                 case CC_SETUP|CONFIRM:
1784                 {
1785                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1786                         int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1787                         cb_log(4, stack->port, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID  is %x\n",l3id );
1788         
1789                         if (!bc) { cb_log(4, stack->port, "Bc Not found (after SETUP CONFIRM)\n"); return 0; }
1790                         cb_log (2,bc->port,"I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
1791                         bc->l3_id=l3id;
1792                         cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1793                 }
1794                 free_msg(msg);
1795                 return 0;
1796       
1797                 case CC_SETUP|INDICATION:
1798                 {
1799                         struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0, 1);
1800                         if (!bc) 
1801                         ERR_NO_CHANNEL:
1802                         {
1803                                 msg_t *dmsg;
1804                                 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1805                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1806                                 stack->nst.manager_l3(&stack->nst, dmsg);
1807                                 free_msg(msg);
1808                                 return 0;
1809                         }
1810   
1811                         cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",hh->dinfo);
1812                         bc->l3_id=hh->dinfo;
1813                 }
1814                 break;
1815
1816                 case CC_CONNECT_ACKNOWLEDGE|INDICATION:
1817 #if 0
1818                 {
1819                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1820                         if (bc) {
1821                                 if ( !misdn_cap_is_speech(bc->capability)) {
1822                                         int ret=setup_bc(bc);
1823                                         if (ret == -EINVAL){
1824                                                 cb_log(0,bc->port,"send_event: setup_bc failed\n");
1825                                                 
1826                                         }
1827                                 }
1828                         }
1829                 }
1830 #endif
1831                 break;
1832                 
1833                 case CC_ALERTING|INDICATION:
1834                 case CC_PROCEEDING|INDICATION:
1835                 case CC_SETUP_ACKNOWLEDGE|INDICATION:
1836                         if(!stack->ptp) break;  
1837                 case CC_CONNECT|INDICATION:
1838                 {
1839 #if 0
1840                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1841                         
1842                         if (!bc) {
1843                                 msg_t *dmsg;
1844                                 cb_log(0, stack->port,"!!!! We didn't found our bc, dinfo:%x on this port.\n",hh->dinfo);
1845                                 
1846                                 cb_log(0, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
1847                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1848                                 stack->nst.manager_l3(&stack->nst, dmsg);
1849                                 free_msg(msg);
1850                                 return 0;
1851                                 
1852                         }
1853                         int ret=setup_bc(bc);
1854                         if (ret == -EINVAL){
1855                                 cb_log(0,bc->port,"handle_event_nt: setup_bc failed\n");
1856                                 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1857                         }
1858 #endif
1859                 }
1860                 break;
1861                 case CC_DISCONNECT|INDICATION:
1862                 {
1863                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1864                         if (!bc) {
1865                                 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1866                                 if (bc) { 
1867                                         int myprocid=bc->l3_id&0x0000ffff;
1868                                         hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1869                                         cb_log(3,stack->port,"Reject dinfo: %x cause:%d\n",hh->dinfo,bc->cause);
1870                                         reject=1;               
1871                                 }
1872                         }
1873                 }
1874                 break;
1875                 
1876                 case CC_FACILITY|INDICATION:
1877                 {
1878                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1879                         if (!bc) {
1880                                 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1881                                 if (bc) { 
1882                                         int myprocid=bc->l3_id&0x0000ffff;
1883                                         hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1884                                         cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
1885                                 }
1886                         }
1887                 }
1888                 break;
1889                 
1890                 case CC_RELEASE_COMPLETE|INDICATION:
1891                         break;
1892
1893                 case CC_SUSPEND|INDICATION:
1894                 {
1895                         msg_t *dmsg;
1896                         cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
1897                         dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1898                         stack->nst.manager_l3(&stack->nst, dmsg);
1899                         free_msg(msg);
1900                         return 0;
1901                 }
1902                 break;
1903                 case CC_RESUME|INDICATION:
1904                         break;
1905
1906                 case CC_RELEASE|CONFIRM:
1907                         break;
1908                         
1909                 case CC_RELEASE|INDICATION:
1910                         break;
1911
1912                 case CC_RELEASE_CR|INDICATION:
1913                 {
1914                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1915                         struct misdn_bchannel dummybc;
1916                         iframe_t frm; /* fake te frm to remove callref from global callreflist */
1917                         frm.dinfo = hh->dinfo;
1918
1919                         frm.addr=stack->upper_id | FLG_MSG_DOWN;
1920
1921                         frm.prim = CC_RELEASE_CR|INDICATION;
1922                         cb_log(4, stack->port, " --> Faking Realease_cr for %x\n",frm.addr);
1923                         /** removing procid **/
1924                         if (!bc) {
1925                                 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
1926                                 memset (&dummybc,0,sizeof(dummybc));
1927                                 dummybc.port=stack->port;
1928                                 dummybc.l3_id=hh->dinfo;
1929                                 dummybc.nt=stack->nt;
1930                                 bc=&dummybc; 
1931                         }
1932         
1933                         if (bc) {
1934                                 if ( (bc->l3_id & 0xff00) == 0xff00) {
1935                                         cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xff);
1936                                         stack->procids[bc->l3_id&0xff] = 0 ;
1937                                 }
1938                         }
1939                         else cb_log(0, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is a bad port.\n");
1940         
1941                         if (handle_cr(stack, &frm)<0) {
1942                         }
1943
1944                         free_msg(msg);
1945                         return 0 ;
1946                 }
1947                 break;
1948       
1949                 case CC_NEW_CR|INDICATION:
1950                         /*  Got New CR for bchan, for now I handle this one in */
1951                         /*  connect_ack, Need to be changed */
1952                 {
1953                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1954                         int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1955                         if (!bc) { cb_log(0, stack->port, " --> In NEW_CR: didn't found bc ??\n"); return -1;};
1956                         if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
1957                                 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
1958                                 stack->procids[bc->l3_id&0xff] = 0 ;
1959                         }
1960                         cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID  is %x\n",l3id );
1961         
1962                         bc->l3_id =l3id;
1963                         cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1964         
1965                         free_msg(msg);
1966                         return 0;
1967                 }
1968       
1969                 case DL_ESTABLISH | INDICATION:
1970                 case DL_ESTABLISH | CONFIRM:
1971                 {
1972                         cb_log(3, stack->port, "%% GOT L2 Activate Info.\n");
1973                         
1974                         if (stack->ptp && stack->l2link) {
1975                                 cb_log(0, stack->port, "%% GOT L2 Activate Info. but we're activated already.. this l2 is faulty, blocking port\n");
1976                                 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
1977                         }
1978                         
1979                         stack->l2link = 1;
1980                         stack->l2upcnt=0;
1981                         
1982                         free_msg(msg);
1983                         return 0;
1984                 }
1985                 break;
1986
1987
1988                 case DL_RELEASE | INDICATION:
1989                 case DL_RELEASE | CONFIRM:
1990                 {
1991                         if (stack->ptp) {
1992                                 cb_log(3 , stack->port, "%% GOT L2 DeActivate Info.\n");
1993
1994                                 if (stack->l2upcnt>3) {
1995                                         cb_log(0 , stack->port, "!!! Could not Get the L2 up after 3 Attemps!!!\n");
1996                                 }  else {
1997 #if 0
1998                                         if (stack->nt) misdn_lib_reinit_nt_stack(stack->port);
1999 #endif
2000                                         if (stack->l1link) {
2001                                                 misdn_lib_get_l2_up(stack);
2002                                                 stack->l2upcnt++;
2003                                         }
2004                                 }
2005                                 
2006                         } else 
2007                                 cb_log(3, stack->port, "%% GOT L2 DeActivate Info.\n");
2008                         
2009                         stack->l2link = 0;
2010                         free_msg(msg);
2011                         return 0;
2012                 }
2013                 break;
2014                 }
2015         }
2016         
2017         {
2018                 /*  Parse Events and fire_up to App. */
2019                 struct misdn_bchannel *bc;
2020                 struct misdn_bchannel dummybc;
2021                 
2022                 enum event_e event = isdn_msg_get_event(msgs_g, msg, 1);
2023     
2024                 bc=find_bc_by_l3id(stack, hh->dinfo);
2025     
2026                 if (!bc) {
2027       
2028                         cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x).\n", hh->dinfo);
2029                         memset (&dummybc,0,sizeof(dummybc));
2030                         dummybc.port=stack->port;
2031                         dummybc.l3_id=hh->dinfo;
2032                         dummybc.nt=stack->nt;
2033                         bc=&dummybc; 
2034                 }
2035                 if (bc ) {
2036                         isdn_msg_parse_event(msgs_g,msg,bc, 1);
2037
2038                         switch (event) {
2039                                 case EVENT_SETUP:
2040                                         if (bc->channel<=0 || bc->channel==0xff) {
2041                                                 bc->channel=find_free_chan_in_stack(stack,bc, 0);
2042                 
2043                                                 if (bc->channel<=0)
2044                                                         goto ERR_NO_CHANNEL;
2045                                         } else if (!stack->ptp) 
2046                                                 cb_log(3,stack->port," --> PTMP but channel requested\n"); 
2047
2048                                         int ret=set_chan_in_stack(stack, bc->channel);
2049                                         if (event==EVENT_SETUP && ret<0){
2050                                                 /* empty bchannel */
2051                                                 bc->channel=0;
2052                                                 bc->out_cause=44;
2053
2054                                                 goto ERR_NO_CHANNEL;
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                         int ret=handle_event(bc, event, frm);
2601                         if (ret<0) {
2602                                 cb_log(0,stack->port,"couldn't handle event\n");
2603                                 free_msg(msg);
2604                                 return 1;
2605                         }
2606                         /*  shoot up event to App: */
2607                         cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2608       
2609                         if(!isdn_get_info(msgs_g,event,0)) 
2610                                 cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2611                         else
2612                                 response=cb_event(event, bc, glob_mgr->user_data);
2613 #if 1
2614                         if (event == EVENT_SETUP) {
2615                                 switch (response) {
2616                                 case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
2617
2618                                         cb_log(0, stack->port, "TOTALY IGNORING SETUP \n");                                     
2619                                         
2620                                         break;
2621                                 case RESPONSE_IGNORE_SETUP:
2622                                         /* I think we should send CC_RELEASE_CR, but am not sure*/
2623                                         bc->out_cause=16;
2624                                 
2625                                 case RESPONSE_RELEASE_SETUP:
2626                                         misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
2627                                         if (bc->channel>0)
2628                                                 empty_chan_in_stack(stack, bc->channel);
2629                                         empty_bc(bc);
2630                                         bc_state_change(bc,BCHAN_CLEANED);
2631
2632                                         cb_log(0, stack->port, "GOT IGNORE SETUP\n");
2633
2634                                         
2635                                         break;
2636                                 case RESPONSE_OK:
2637                                         cb_log(4, stack->port, "GOT SETUP OK\n");
2638
2639                                         
2640                                         break;
2641                                 default:
2642                                         break;
2643                                 }
2644                         }
2645
2646                         cb_log(5, stack->port, "Freeing Msg on prim:%x \n",frm->prim);
2647
2648                         
2649                         free_msg(msg);
2650                         return 1;
2651 #endif
2652       
2653                 } else {
2654                         cb_log(0, stack->port, " --> Didn't find BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
2655                         struct misdn_bchannel dummybc;
2656                         memset (&dummybc,0,sizeof(dummybc));
2657                         dummybc.port=stack->port;
2658                         dummybc.l3_id=frm->dinfo;
2659                         bc=&dummybc; 
2660                         goto handle_frm_bc;
2661                 }
2662         }
2663
2664         cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x \n",frm->prim);
2665         return 0;
2666 }
2667
2668
2669 static int handle_l1(msg_t *msg)
2670 {
2671         iframe_t *frm = (iframe_t*) msg->data;
2672         struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2673         int i ;
2674         
2675         if (!stack) return 0 ;
2676
2677         switch (frm->prim) {
2678         case PH_ACTIVATE | CONFIRM:
2679         case PH_ACTIVATE | INDICATION:
2680                 cb_log (3, stack->port, "L1: PH L1Link Up!\n");
2681                 stack->l1link=1;
2682                 
2683                 if (stack->nt) {
2684                         
2685                         if (stack->nst.l1_l2(&stack->nst, msg))
2686                                 free_msg(msg);
2687
2688                         if (stack->ptp)
2689                                 misdn_lib_get_l2_up(stack);
2690                 } else {
2691                         free_msg(msg);
2692                 }
2693                 
2694                 for (i=0;i<=stack->b_num; i++) {
2695                         if (stack->bc[i].evq != EVENT_NOTHING) {
2696                                 cb_log(4, stack->port, "Fireing Queued Event %s because L1 got up\n", isdn_get_info(msgs_g, stack->bc[i].evq, 0));
2697                                 misdn_lib_send_event(&stack->bc[i],stack->bc[i].evq);
2698                                 stack->bc[i].evq=EVENT_NOTHING;
2699                         }
2700                         
2701                 }
2702                 return 1;
2703
2704         case PH_ACTIVATE | REQUEST:
2705                 free_msg(msg);
2706                 cb_log(3,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
2707                 return 1;
2708                 
2709         case PH_DEACTIVATE | REQUEST:
2710                 free_msg(msg);
2711                 cb_log(3,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
2712                 return 1;
2713                 
2714         case PH_DEACTIVATE | CONFIRM:
2715         case PH_DEACTIVATE | INDICATION:
2716                 cb_log (3, stack->port, "L1: PH L1Link Down! \n");
2717                 
2718                 for (i=0; i<=stack->b_num; i++) {
2719                         if (global_state == MISDN_INITIALIZED)  {
2720                                 cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
2721                         }
2722                 }
2723                 
2724                 if (stack->nt) {
2725                         if (stack->nst.l1_l2(&stack->nst, msg))
2726                                 free_msg(msg);
2727                 } else {
2728                         free_msg(msg);
2729                 }
2730                 
2731                 stack->l1link=0;
2732                 stack->l2link=0;
2733                 return 1;
2734         }
2735   
2736         return 0;
2737 }
2738
2739 static int handle_l2(msg_t *msg)
2740 {
2741         iframe_t *frm = (iframe_t*) msg->data;
2742
2743         struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2744         
2745         if (!stack) {
2746                 return 0 ;
2747         }
2748         
2749         switch(frm->prim) {
2750
2751         case DL_ESTABLISH | REQUEST:
2752                 cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
2753                 return 1;
2754         case DL_RELEASE | REQUEST:
2755                 cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
2756                 return 1;
2757                 
2758         case DL_ESTABLISH | INDICATION:
2759         case DL_ESTABLISH | CONFIRM:
2760         {
2761                 cb_log (3, stack->port, "L2: L2Link Up! \n");
2762                 if (stack->ptp && stack->l2link) {
2763                         cb_log (-1, stack->port, "L2: L2Link Up! but it's already UP.. must be faulty, blocking port\n"); 
2764                         cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
2765                 }
2766                 stack->l2link=1;
2767                 free_msg(msg);
2768                 return 1;
2769         }
2770         break;
2771     
2772         case DL_RELEASE | INDICATION:
2773         case DL_RELEASE | CONFIRM:
2774         {
2775                 cb_log (3, stack->port, "L2: L2Link Down! \n");
2776                 stack->l2link=0;
2777                 
2778                 free_msg(msg);
2779                 return 1;
2780         }
2781         break;
2782         }
2783         return 0;
2784 }
2785
2786 static int handle_mgmt(msg_t *msg)
2787 {
2788         iframe_t *frm = (iframe_t*) msg->data;
2789
2790         if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
2791                 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
2792                 free_msg(msg);
2793                 return 1;
2794         }
2795         
2796         struct misdn_stack * stack=find_stack_by_addr(frm->addr);
2797         
2798         if (!stack) {
2799                 if (frm->prim == (MGR_DELLAYER|CONFIRM)) {
2800                         cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: %x !\n",
2801                                         frm->addr) ;
2802                         free_msg(msg);
2803                         return 1;
2804                 }
2805                 
2806                 return 0;
2807         }
2808         
2809         switch(frm->prim) {
2810         case MGR_SHORTSTATUS | INDICATION:
2811         case MGR_SHORTSTATUS | CONFIRM:
2812                 cb_log(5, 0, "MGMT: Short status dinfo %x\n",frm->dinfo);
2813                 
2814                 switch (frm->dinfo) {
2815                 case SSTATUS_L1_ACTIVATED:
2816                         cb_log(3, 0, "MGMT: SSTATUS: L1_ACTIVATED \n");
2817                         stack->l1link=1;
2818                 
2819                         break;
2820                 case SSTATUS_L1_DEACTIVATED:
2821                         cb_log(3, 0, "MGMT: SSTATUS: L1_DEACTIVATED \n");
2822                         stack->l1link=0;
2823
2824                         clear_l3(stack);
2825                         break;
2826
2827                 case SSTATUS_L2_ESTABLISHED:
2828                         cb_log(3, stack->port, "MGMT: SSTATUS: L2_ESTABLISH \n");
2829                         stack->l2link=1;
2830                         break;
2831                         
2832                 case SSTATUS_L2_RELEASED:
2833                         cb_log(3, stack->port, "MGMT: SSTATUS: L2_RELEASED \n");
2834                         stack->l2link=0;
2835                         break;
2836                 }
2837                 
2838                 free_msg(msg);
2839                 return 1;
2840                 
2841         case MGR_SETSTACK | INDICATION:
2842                 cb_log(4, stack->port, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
2843                 free_msg(msg);
2844                 return 1;
2845         case MGR_DELLAYER | CONFIRM:
2846                 cb_log(4, stack->port, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
2847                 free_msg(msg);
2848                 return 1;
2849                 
2850         }
2851         
2852         /*
2853         if ( (frm->prim & 0x0f0000) ==  0x0f0000) {
2854         cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
2855         free_msg(msg);
2856         return 1;
2857         } */
2858     
2859         return 0;
2860 }
2861
2862
2863 static msg_t *fetch_msg(int midev) 
2864 {
2865         msg_t *msg=alloc_msg(MAX_MSG_SIZE);
2866         int r;
2867
2868         if (!msg) {
2869                 cb_log(0, 0, "fetch_msg: alloc msg failed !!");
2870                 return NULL;
2871         }
2872
2873         AGAIN:
2874                 r=mISDN_read(midev,msg->data,MAX_MSG_SIZE, TIMEOUT_10SEC);
2875                 msg->len=r;
2876     
2877                 if (r==0) {
2878                         free_msg(msg); /* danger, cauz usualy freeing in main_loop */
2879                         cb_log(6,0,"Got empty Msg..\n");
2880                         return NULL;
2881                 }
2882
2883                 if (r<0) {
2884                         if (errno == EAGAIN) {
2885                                 /*we wait for mISDN here*/
2886                                 cb_log(4,0,"mISDN_read wants us to wait\n");
2887                                 usleep(5000);
2888                                 goto AGAIN;
2889                         }
2890                         
2891                         cb_log(0,0,"mISDN_read returned :%d error:%s (%d)\n",r,strerror(errno),errno); 
2892                 }
2893
2894 #if 0
2895                if  (!(frm->prim == (DL_DATA|INDICATION) )|| (frm->prim == (PH_DATA|INDICATION)))
2896                        cb_log(0,0,"prim: %x dinfo:%x addr:%x msglen:%d frm->len:%d\n",frm->prim, frm->dinfo, frm->addr, msg->len,frm->len );
2897 #endif
2898                 return msg;
2899 }
2900
2901 void misdn_lib_isdn_l1watcher(int port)
2902 {
2903         struct misdn_stack *stack;
2904
2905         for (stack = glob_mgr->stack_list; stack && (stack->port != port); stack = stack->next)
2906                 ;
2907
2908         if (stack) {
2909                 cb_log(4, port, "Checking L1 State\n"); 
2910                 if (!stack->l1link) {
2911                         cb_log(4, port, "L1 State Down, trying to get it up again\n");  
2912                         misdn_lib_get_short_status(stack);
2913                         misdn_lib_get_l1_up(stack); 
2914                         misdn_lib_get_l2_up(stack); 
2915                 }
2916         }
2917 }
2918
2919 static void misdn_lib_isdn_event_catcher(void *arg)
2920 {
2921         struct misdn_lib *mgr = arg;
2922         int zero_frm=0 , fff_frm=0 ;
2923         int midev= mgr->midev;
2924         int port=0;
2925         
2926         while (1) {
2927                 msg_t *msg = fetch_msg(midev); 
2928                 iframe_t *frm;
2929                 
2930                 
2931                 if (!msg) continue;
2932                 
2933                 frm = (iframe_t*) msg->data;
2934                 
2935                 /** When we make a call from NT2Ast we get this frames **/
2936                 if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
2937                         zero_frm++; 
2938                         free_msg(msg);
2939                         continue;
2940                 } else {
2941                         if (zero_frm) {
2942                                 cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm);
2943                                 zero_frm = 0 ;
2944                         }
2945                 }
2946                 
2947                 /** I get this sometimes after setup_bc **/
2948                 if (frm->len == 0 &&  frm->dinfo == 0 && frm->prim == 0xffffffff ) {
2949                         fff_frm++; 
2950                         free_msg(msg);
2951                         continue;
2952                 } else {
2953                         if (fff_frm) {
2954                                 cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm);
2955                                 fff_frm = 0 ;
2956                         }
2957                 }
2958                 
2959                 manager_isdn_handler(frm, msg);
2960         }
2961
2962 }
2963
2964
2965 /** App Interface **/
2966
2967 int te_lib_init() {
2968         char buff[1025];
2969         iframe_t *frm=(iframe_t*)buff;
2970         int midev=mISDN_open();
2971         int ret;
2972
2973         memset(buff,0,1025);
2974   
2975         if  (midev<=0) return midev;
2976   
2977 /* create entity for layer 3 TE-mode */
2978         mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
2979         ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
2980   
2981         if (ret < mISDN_HEADER_LEN) {
2982         noentity:
2983                 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n",strerror(errno));
2984                 exit(-1);
2985         }
2986   
2987         entity = frm->dinfo & 0xffff ;
2988   
2989         if (!entity)
2990                 goto noentity;
2991
2992         return midev;
2993   
2994 }
2995
2996 void te_lib_destroy(int midev)
2997 {
2998         char buf[1024];
2999         mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
3000
3001         cb_log(4, 0, "Entetity deleted\n");
3002         mISDN_close(midev);
3003         cb_log(4, 0, "midev closed\n");
3004 }
3005
3006
3007
3008 void misdn_lib_transfer(struct misdn_bchannel* holded_bc)
3009 {
3010         holded_bc->holded=0;
3011 }
3012
3013 struct misdn_bchannel *manager_find_bc_by_pid(int pid)
3014 {
3015         struct misdn_stack *stack;
3016         int i;
3017   
3018         for (stack=glob_mgr->stack_list;
3019              stack;
3020              stack=stack->next) {
3021                 for (i=0; i<=stack->b_num; i++)
3022                         if (stack->bc[i].pid == pid) return &stack->bc[i];
3023         }
3024   
3025         return NULL;
3026 }
3027
3028 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc)
3029 {
3030         struct misdn_stack *stack=get_stack_by_bc(bc);
3031         return find_bc_holded(stack);
3032 }
3033
3034
3035
3036 static void prepare_bc(struct misdn_bchannel*bc, int channel)
3037 {
3038         bc->channel = channel;
3039         bc->channel_preselected = channel?1:0;
3040         bc->in_use = 1;
3041         bc->need_disconnect=1;
3042         bc->need_release=1;
3043         bc->need_release_complete=1;
3044         bc->cause=16;
3045
3046         if (++mypid>5000) mypid=1;
3047         bc->pid=mypid;
3048
3049 #if 0
3050         bc->addr=0;
3051         bc->b_stid=0;
3052         bc->layer_id=0;
3053 #endif
3054 }
3055
3056 struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel, int inout)
3057 {
3058         struct misdn_stack *stack;
3059         int i;
3060         
3061         if (channel < 0 || channel > MAX_BCHANS) {
3062                 cb_log(0,port,"Requested channel out of bounds (%d)\n",channel);
3063                 return NULL;
3064         }
3065
3066         for (stack=glob_mgr->stack_list; stack; stack=stack->next) {
3067     
3068                 if (stack->port == port) {
3069                         if (stack->blocked) {
3070                                 cb_log(0,port,"Port is blocked\n");
3071                                 return NULL;
3072                         }
3073                 
3074                         if (channel > 0) {
3075                                 if (channel <= stack->b_num) {
3076                                         for (i = 0; i < stack->b_num; i++) {
3077                                                 if (stack->bc[i].in_use && stack->bc[i].channel == channel) {
3078                                                         cb_log(0,port,"Requested channel:%d on port:%d is already in use\n",channel, port);
3079                                                         return NULL;
3080                                                 }
3081                                         }
3082                                 } else {
3083                                         cb_log(0,port,"Requested channel:%d is out of bounds on port:%d\n",channel, port);
3084                                         return NULL;
3085                                 }
3086                         }
3087
3088                         int maxnum=inout&&!stack->pri&&!stack->ptp?stack->b_num+1:stack->b_num;
3089                         //int maxnum=stack->b_num+1;
3090
3091                         cb_log(0,0,"maxnum:%d",maxnum);
3092                         for (i = 0; i <maxnum; i++) {
3093                                 if (!stack->bc[i].in_use) {
3094                                         /* 3. channel on bri means CW*/
3095                                         if (!stack->pri && i==stack->b_num)
3096                                                 stack->bc[i].cw=1;
3097
3098                                         prepare_bc(&stack->bc[i], channel);
3099                                         return &stack->bc[i];
3100                                 }
3101                         }
3102
3103                         cb_log(1,port,"There is no free channel on port (%d)\n",port);
3104                         return NULL;
3105                 }
3106         }
3107
3108         cb_log(0,port,"Port is not configured (%d)\n",port);
3109         return NULL;
3110 }
3111
3112
3113 static char *fac2str (enum FacFunction func)
3114 {
3115         struct arr_el { 
3116                 enum FacFunction p; 
3117                 char *s ;