7294f8f5667ef7c266975268e1737e1fb045ad4d
[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         
622         bc->fac_in.Function = Fac_None;
623         bc->fac_out.Function = Fac_None;
624         
625         bc->te_choose_channel = 0;
626 }
627
628
629 static int clean_up_bc(struct misdn_bchannel *bc)
630 {
631         int ret=0;
632         unsigned char buff[32];
633         struct misdn_stack * stack;
634
635         cb_log(3, bc?bc->port:0, "$$$ CLEANUP CALLED pid:%d\n", bc?bc->pid:-1);
636         
637         if (!bc  ) return -1;
638         stack=get_stack_by_bc(bc);
639         
640         if (!stack) return -1;
641         
642         switch (bc->bc_state ) {
643         case BCHAN_CLEANED:
644                 cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
645                 return -1;
646                 
647         default:
648                 break;
649         }
650         
651         cb_log(2, stack->port, "$$$ Cleaning up bc with stid :%x pid:%d\n", bc->b_stid, bc->pid);
652         
653         manager_ec_disable(bc);
654
655         manager_bchannel_deactivate(bc);
656
657         mISDN_write_frame(stack->midev, buff, bc->layer_id|FLG_MSG_TARGET|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
658         
659         bc->b_stid = 0;
660         bc_state_change(bc, BCHAN_CLEANED);
661         
662         return ret;
663 }
664
665
666
667 static void clear_l3(struct misdn_stack *stack)
668 {
669         int i;
670
671         for (i=0; i<=stack->b_num; i++) {
672                 if (global_state == MISDN_INITIALIZED)  {
673                         cb_event(EVENT_CLEANUP, &stack->bc[i], NULL); 
674                         empty_chan_in_stack(stack,i+1);
675                         empty_bc(&stack->bc[i]);
676                         clean_up_bc(&stack->bc[i]);
677                 }
678                 
679         } 
680 }
681
682 static int set_chan_in_stack(struct misdn_stack *stack, int channel)
683 {
684
685         cb_log(4,stack->port,"set_chan_in_stack: %d\n",channel);
686         dump_chan_list(stack);
687         if (channel >=1 && channel <= MAX_BCHANS) {
688                 if (!stack->channels[channel-1])
689                         stack->channels[channel-1] = 1;
690                 else {
691                         cb_log(0,stack->port,"channel already in use:%d\n", channel );
692                         return -1;
693                 }
694         } else {
695                 cb_log(0,stack->port,"couldn't set channel %d in\n", channel );
696                 return -1;
697         }
698   
699         return 0;
700 }
701
702 #if 0
703 static int chan_in_stack_free(struct misdn_stack *stack, int channel)
704 {
705         if (stack->channels[channel-1])
706                 return 0;
707   
708         return 1;
709 }
710 #endif
711
712
713 static int newteid=0;
714
715 #define MAXPROCS 0x100
716
717 static int misdn_lib_get_l1_down(struct misdn_stack *stack)
718 {
719         /* Pull Up L1 */ 
720         iframe_t act;
721         act.prim = PH_DEACTIVATE | REQUEST; 
722         act.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
723
724         
725         act.dinfo = 0;
726         act.len = 0;
727
728         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
729
730
731 }
732
733
734 static int misdn_lib_get_l2_down(struct misdn_stack *stack)
735 {
736         
737         if (stack->ptp && (stack->nt) ) {
738                 msg_t *dmsg;
739                 /* L2 */
740                 dmsg = create_l2msg(DL_RELEASE| REQUEST, 0, 0);
741                 
742                 if (stack->nst.manager_l3(&stack->nst, dmsg))
743                         free_msg(dmsg);
744                 
745         } else {
746                 iframe_t act;
747                 
748                 act.prim = DL_RELEASE| REQUEST;
749                 act.addr = (stack->upper_id |FLG_MSG_DOWN)  ;
750                 
751                 act.dinfo = 0;
752                 act.len = 0;
753                 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
754         }
755         
756         return 0;
757 }
758
759
760 static int misdn_lib_get_l1_up(struct misdn_stack *stack)
761 {
762         /* Pull Up L1 */ 
763         iframe_t act;
764         act.prim = PH_ACTIVATE | REQUEST; 
765         act.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
766
767         
768         act.dinfo = 0;
769         act.len = 0;
770
771         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
772
773 }
774
775 int misdn_lib_get_l2_up(struct misdn_stack *stack)
776 {
777         
778         if (stack->ptp && (stack->nt) ) {
779                 msg_t *dmsg;
780                 /* L2 */
781                 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
782                 
783                 if (stack->nst.manager_l3(&stack->nst, dmsg))
784                         free_msg(dmsg);
785                 
786         } else {
787                 iframe_t act;
788                 
789                 act.prim = DL_ESTABLISH | REQUEST;
790                 act.addr = (stack->upper_id |FLG_MSG_DOWN)  ;
791                 
792                 act.dinfo = 0;
793                 act.len = 0;
794                 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
795         }
796         
797         return 0;
798 }
799
800 #if 0
801 static int misdn_lib_get_l2_te_ptp_up(struct misdn_stack *stack)
802 {
803         iframe_t act;
804                 
805         act.prim = DL_ESTABLISH | REQUEST;
806         act.addr = (stack->upper_id  & ~LAYER_ID_MASK) | 3 | FLG_MSG_DOWN;
807                 
808         act.dinfo = 0;
809         act.len = 0;
810         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
811         return 0;
812 }
813 #endif
814
815 static int misdn_lib_get_short_status(struct misdn_stack *stack)
816 {
817         iframe_t act;
818         
819         
820         act.prim = MGR_SHORTSTATUS | REQUEST; 
821         
822         act.addr = (stack->upper_id | MSG_BROADCAST)  ;
823
824         act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
825         
826         act.len = 0;
827         return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
828 }
829
830
831
832 static int create_process (int midev, struct misdn_bchannel *bc) {
833         iframe_t ncr;
834         int l3_id;
835         int i;
836         struct misdn_stack *stack=get_stack_by_bc(bc);
837   
838         if (stack->nt) {
839                 if (!find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0))
840                         return -1;
841                 /*bc->channel=free_chan;*/
842
843                 if (set_chan_in_stack(stack ,bc->channel)<0) return -1;
844                 
845                 cb_log(4,stack->port, " -->  found channel: %d\n", bc->channel);
846     
847                 for (i=0; i <= MAXPROCS; i++)
848                         if (stack->procids[i]==0) break;
849     
850                 if (i== MAXPROCS) {
851                         cb_log(0, stack->port, "Couldnt Create New ProcId.\n");
852                         return -1;
853                 }
854                 stack->procids[i]=1;
855
856                 l3_id = 0xff00 | i;
857     
858                 ncr.prim = CC_NEW_CR | REQUEST; 
859
860                 ncr.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
861
862                 ncr.dinfo = l3_id;
863                 ncr.len = 0;
864
865                 bc->l3_id = l3_id;
866                 cb_log(3, stack->port, " --> new_l3id %x\n",l3_id);
867     
868         } else { 
869                 if (stack->ptp || bc->te_choose_channel) {
870                         /* we know exactly which channels are in use */
871                         if (!find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0))
872                                 return -1;
873                         /*bc->channel=free_chan;*/
874                         cb_log(2,stack->port, " -->  found channel: %d\n", bc->channel);
875                         if (set_chan_in_stack(stack ,bc->channel)<0) return -1;
876                 } else {
877                         /* other phones could have made a call also on this port (ptmp) */
878                         bc->channel=0xff;
879                 }
880     
881     
882                 /* if we are in te-mode, we need to create a process first */
883                 if (newteid++ > 0xffff)
884                         newteid = 0x0001;
885     
886                 l3_id = (entity<<16) | newteid;
887                 /* preparing message */
888                 ncr.prim = CC_NEW_CR | REQUEST; 
889
890                 ncr.addr = (stack->upper_id | FLG_MSG_DOWN)  ;
891
892                 ncr.dinfo =l3_id;
893                 ncr.len = 0;
894                 /* send message */
895
896                 bc->l3_id = l3_id;
897                 cb_log(3, stack->port, "--> new_l3id %x\n",l3_id);
898     
899                 mISDN_write(midev, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC);
900         }
901   
902         return l3_id;
903 }
904
905
906 void misdn_lib_setup_bc(struct misdn_bchannel *bc)
907 {
908         clean_up_bc(bc);
909         setup_bc(bc);
910 }
911
912
913 int setup_bc(struct misdn_bchannel *bc)
914 {
915         unsigned char buff[1025];
916   
917         mISDN_pid_t pid;
918         int ret;
919         
920
921         struct misdn_stack *stack=get_stack_by_bc(bc);
922
923         if (!stack) {
924                 cb_log(0, bc->port, "setup_bc: NO STACK FOUND!!\n");
925                 return -1;
926         }
927         
928         int midev=stack->midev;
929         int channel=bc->channel-1-(bc->channel>16);
930         int b_stid=stack->b_stids[channel>=0?channel:0];
931
932
933         switch (bc->bc_state) {
934                 case BCHAN_CLEANED:
935                         break;
936                 default:
937                         cb_log(4, stack->port, "$$$ bc already upsetted stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) );
938                         return -1;
939         }
940         
941         cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
942         
943         if (b_stid <= 0) {
944                 cb_log(0, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
945                 
946                 bc_state_change(bc,BCHAN_ERROR);
947                 return 1;
948         }
949         
950         
951         bc->b_stid = b_stid;
952         
953         {
954                 layer_info_t li;
955                 memset(&li, 0, sizeof(li));
956     
957                 li.object_id = -1;
958                 li.extentions = 0;
959                 
960                 li.st = bc->b_stid; /*  given idx */
961
962
963 #define MISDN_DSP
964 #ifndef MISDN_DSP
965                 bc->nodsp=1;
966 #endif
967                 if ( bc->hdlc || bc->nodsp) {
968                         cb_log(4, stack->port,"setup_bc: without dsp\n");
969                         { 
970                                 int l = sizeof(li.name);
971                                 strncpy(li.name, "B L3", l);
972                                 li.name[l-1] = 0;
973                         }
974                         li.pid.layermask = ISDN_LAYER((3));
975                         li.pid.protocol[3] = ISDN_PID_L3_B_USER;
976                         
977                         bc->layer=3;
978                 } else {
979                         cb_log(4, stack->port,"setup_bc: with dsp\n");
980                         { 
981                                 int l = sizeof(li.name);
982                                 strncpy(li.name, "B L4", l);
983                                 li.name[l-1] = 0;
984                         }
985                         li.pid.layermask = ISDN_LAYER((4));
986                         li.pid.protocol[4] = ISDN_PID_L4_B_USER
987 ;
988                         bc->layer=4;
989                         
990                 }  
991                 
992                 ret = mISDN_new_layer(midev, &li);
993                 if (ret ) {
994                         cb_log(0, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
995
996                         bc_state_change(bc,BCHAN_ERROR);
997                         return(-EINVAL);
998                 }
999                 
1000                 bc->layer_id = li.id;
1001         }
1002         
1003         memset(&pid, 0, sizeof(pid));
1004         
1005         
1006         
1007         cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
1008         
1009         if (bc->nodsp) {
1010                 cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
1011                 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1012                 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1013                 pid.protocol[3] = ISDN_PID_L3_B_USER;
1014                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
1015                 
1016         } else if ( bc->hdlc ) {
1017                 cb_log(2, stack->port," --> HDLC Mode\n");
1018                 pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
1019                 pid.protocol[2] = ISDN_PID_L2_B_TRANS  ;
1020                 pid.protocol[3] = ISDN_PID_L3_B_USER;
1021                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
1022         } else {
1023                 cb_log(2, stack->port," --> TRANSPARENT Mode\n");
1024                 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1025                 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1026                 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1027                 pid.protocol[4] = ISDN_PID_L4_B_USER;
1028                 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1029                 
1030         } 
1031
1032         ret = mISDN_set_stack(midev, bc->b_stid, &pid);
1033
1034         if (ret){
1035                 cb_log(0, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
1036                 
1037                 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1038                 
1039                 bc_state_change(bc,BCHAN_ERROR);
1040                 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1041                 return(-EINVAL);
1042         }
1043
1044         ret = mISDN_get_setstack_ind(midev, bc->layer_id);
1045
1046         if (ret) {
1047                 cb_log(0, stack->port,"$$$ Set StackIND Err: %d %s\n",ret,strerror(errno));
1048                 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1049                 
1050                 bc_state_change(bc,BCHAN_ERROR);
1051                 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1052                 return(-EINVAL);
1053         }
1054
1055         ret = mISDN_get_layerid(midev, bc->b_stid, bc->layer) ;
1056
1057         bc->addr = ret>0? ret : 0;
1058
1059         if (!bc->addr) {
1060                 cb_log(0, stack->port,"$$$ Get Layerid Err: %d %s\n",ret,strerror(errno));
1061                 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1062                 
1063                 bc_state_change(bc,BCHAN_ERROR);
1064                 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1065                 return (-EINVAL);
1066         }
1067
1068         manager_bchannel_activate(bc);
1069         
1070         bc_state_change(bc,BCHAN_ACTIVATED);
1071
1072         return 0;
1073 }
1074
1075
1076
1077 /** IFACE **/
1078 int init_bc(struct misdn_stack *stack,  struct misdn_bchannel *bc, int midev, int port, int bidx,  char *msn, int firsttime)
1079 {
1080         unsigned char buff[1025];
1081         iframe_t *frm = (iframe_t *)buff;
1082         int ret;
1083   
1084         if (!bc) return -1;
1085   
1086         cb_log(8, port, "Init.BC %d.\n",bidx);
1087         
1088         memset(bc, 0,sizeof(struct misdn_bchannel));
1089         
1090         if (msn) {
1091                 int l = sizeof(bc->msn);
1092                 strncpy(bc->msn,msn, l);
1093                 bc->msn[l-1] = 0;
1094         }
1095         
1096         
1097         empty_bc(bc);
1098         bc_state_change(bc, BCHAN_CLEANED);
1099         
1100         bc->port=stack->port;
1101         bc->nt=stack->nt?1:0;
1102         
1103         {
1104                 ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
1105
1106                 if (!ibuf) return -1;
1107                 
1108                 clear_ibuffer( ibuf);
1109                 
1110                 ibuf->rsem=malloc(sizeof(sem_t));
1111                 
1112                 bc->astbuf=ibuf;
1113
1114                 if (sem_init(ibuf->rsem,1,0)<0)
1115                         sem_init(ibuf->rsem,0,0);
1116                 
1117         }
1118         
1119         
1120         
1121         
1122         {
1123                 stack_info_t *stinf;
1124                 ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
1125                 if (ret < 0) {
1126                         cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1127                         return -1;
1128                 }
1129     
1130                 stinf = (stack_info_t *)&frm->data.p;
1131     
1132                 cb_log(8, port, " --> Child %x\n",stinf->child[bidx]);
1133         }
1134   
1135         return 0;
1136 }
1137
1138
1139
1140 struct misdn_stack* stack_init( int midev, int port, int ptp )
1141 {
1142         int ret;
1143         unsigned char buff[1025];
1144         iframe_t *frm = (iframe_t *)buff;
1145         stack_info_t *stinf;
1146         int i; 
1147         layer_info_t li;
1148
1149         struct misdn_stack *stack = malloc(sizeof(struct misdn_stack));
1150         if (!stack ) return NULL;
1151
1152
1153         cb_log(8, port, "Init. Stack.\n");
1154   
1155         memset(stack,0,sizeof(struct misdn_stack));
1156   
1157         for (i=0; i<MAX_BCHANS + 1; i++ ) stack->channels[i]=0;
1158         
1159         stack->port=port;
1160         stack->midev=midev;
1161         stack->ptp=ptp;
1162   
1163         stack->holding=NULL;
1164         stack->pri=0;
1165   
1166         msg_queue_init(&stack->downqueue);
1167         msg_queue_init(&stack->upqueue);
1168   
1169         /* query port's requirements */
1170         ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
1171         if (ret < 0) {
1172                 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1173                 return(NULL);
1174         }
1175   
1176         stinf = (stack_info_t *)&frm->data.p;
1177
1178         stack->d_stid = stinf->id;
1179         stack->b_num = stinf->childcnt;
1180
1181         for (i=0; i<=stinf->childcnt; i++)
1182                 stack->b_stids[i] = stinf->child[i];
1183   
1184         switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
1185         case ISDN_PID_L0_TE_S0:
1186                 stack->nt=0;
1187                 break;
1188         case ISDN_PID_L0_NT_S0:
1189                 cb_log(8, port, "NT Stack\n");
1190
1191                 stack->nt=1;
1192                 break;
1193
1194         case ISDN_PID_L0_TE_U:
1195                 break;
1196         case ISDN_PID_L0_NT_U:
1197                 break;
1198         case ISDN_PID_L0_TE_UP2:
1199                 break;
1200         case ISDN_PID_L0_NT_UP2:
1201                 break;
1202         case ISDN_PID_L0_TE_E1:
1203                 cb_log(8, port, "TE S2M Stack\n");
1204                 stack->nt=0;
1205                 stack->pri=1;
1206                 break;
1207         case ISDN_PID_L0_NT_E1:
1208                 cb_log(8, port, "TE S2M Stack\n");
1209                 stack->nt=1;
1210                 stack->pri=1;
1211                 
1212                 break;
1213         default:
1214                 cb_log(0, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
1215
1216         }
1217
1218         if (!stack->nt) {
1219                 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) { 
1220                         stack->ptp = 1;
1221                 } else {
1222                         stack->ptp = 0;
1223                 }
1224         }
1225         
1226         {
1227                 int ret;
1228                 int nt=stack->nt;
1229
1230                 cb_log(8, port, "Init. Stack.\n");
1231                 
1232                 memset(&li, 0, sizeof(li));
1233                 {
1234                         int l = sizeof(li.name);
1235                         strncpy(li.name,nt?"net l2":"user l4", l);
1236                         li.name[l-1] = 0;
1237                 }
1238                 li.object_id = -1;
1239                 li.extentions = 0;
1240                 li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1241                 li.pid.layermask = ISDN_LAYER((nt?2:4));
1242                 li.st = stack->d_stid;
1243                 
1244                 
1245                 ret = mISDN_new_layer(midev, &li);
1246                 if (ret) {
1247                         cb_log(0, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
1248                         return(NULL);
1249                 }
1250                 
1251                 
1252                 stack->upper_id = li.id;
1253                 ret = mISDN_register_layer(midev, stack->d_stid, stack->upper_id);
1254                 if (ret)
1255                 {
1256                         cb_log(0,port,"Cannot register layer %d of this port.\n", nt?2:4);
1257                         return(NULL);
1258                 }
1259                 
1260                 stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3); 
1261                 if (stack->lower_id < 0) {
1262                         cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
1263                         return(NULL);
1264                 }
1265                 
1266                 stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
1267                 if (stack->upper_id < 0) {
1268                         cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, 2);
1269                         return(NULL);
1270                 }
1271                 
1272                 cb_log(8, port, "NT Stacks upper_id %x\n",stack->upper_id);
1273                 
1274                 
1275                 /* create nst (nt-mode only) */
1276                 if (nt) {
1277                         
1278                         memset(&stack->nst, 0, sizeof(net_stack_t));
1279                         memset(&stack->mgr, 0, sizeof(manager_t));
1280     
1281                         stack->mgr.nst = &stack->nst;
1282                         stack->nst.manager = &stack->mgr;
1283     
1284                         stack->nst.l3_manager = handle_event_nt;
1285                         stack->nst.device = midev;
1286                         stack->nst.cardnr = port;
1287                         stack->nst.d_stid = stack->d_stid;
1288     
1289                         stack->nst.feature = FEATURE_NET_HOLD;
1290                         if (stack->ptp)
1291                                 stack->nst.feature |= FEATURE_NET_PTP;
1292                         if (stack->pri)
1293                                 stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1294                         
1295                         stack->nst.l1_id = stack->lower_id;
1296                         stack->nst.l2_id = stack->upper_id;
1297                         
1298                         msg_queue_init(&stack->nst.down_queue);
1299                         
1300                         Isdnl2Init(&stack->nst);
1301                         Isdnl3Init(&stack->nst);
1302                         
1303                 } 
1304                 
1305                 if (!stack->nt) {
1306                         /*assume L1 is up, we'll get DEACTIVATES soon, for non
1307                          * up L1s*/
1308                         stack->l1link=0;
1309                 }
1310                 stack->l1link=0;
1311                 stack->l2link=0;
1312 #if 0   
1313                 if (!stack->nt) {
1314                         misdn_lib_get_short_status(stack);
1315                 } else {
1316                         misdn_lib_get_l1_up(stack);
1317                         if (!stack->ptp) misdn_lib_get_l1_up(stack);
1318                         misdn_lib_get_l2_up(stack);
1319                 }
1320 #endif
1321
1322                 misdn_lib_get_short_status(stack);
1323                 misdn_lib_get_l1_up(stack);
1324                 misdn_lib_get_l2_up(stack);
1325                 
1326         }
1327
1328         cb_log(8,0,"stack_init: port:%d lowerId:%x  upperId:%x\n",stack->port,stack->lower_id, stack->upper_id);
1329         
1330         return stack;
1331 }
1332
1333
1334 void stack_destroy(struct misdn_stack* stack)
1335 {
1336         char buf[1024];
1337         if (!stack) return;
1338
1339         if (stack->nt) {
1340                 cleanup_Isdnl2(&stack->nst);
1341                 cleanup_Isdnl3(&stack->nst);
1342         }
1343   
1344         if (stack->lower_id) 
1345                 mISDN_write_frame(stack->midev, buf, stack->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1346
1347         if (stack->upper_id) 
1348                 mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1349 }
1350
1351
1352 static struct misdn_stack * find_stack_by_addr(int  addr)
1353 {
1354         struct misdn_stack *stack;
1355         
1356         for (stack=glob_mgr->stack_list;
1357              stack;
1358              stack=stack->next) {
1359                 if ( (stack->upper_id&STACK_ID_MASK) == (addr&STACK_ID_MASK)) return stack;
1360
1361         }
1362   
1363         return NULL;
1364 }
1365
1366
1367 static struct misdn_stack * find_stack_by_port(int port)
1368 {
1369         struct misdn_stack *stack;
1370   
1371         for (stack=glob_mgr->stack_list;
1372              stack;
1373              stack=stack->next) 
1374                 if (stack->port == port) return stack;
1375   
1376         return NULL;
1377 }
1378
1379 static struct misdn_stack * find_stack_by_mgr(manager_t* mgr_nt)
1380 {
1381         struct misdn_stack *stack;
1382   
1383         for (stack=glob_mgr->stack_list;
1384              stack;
1385              stack=stack->next) 
1386                 if ( &stack->mgr == mgr_nt) return stack;
1387   
1388         return NULL;
1389 }
1390
1391 static struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
1392 {
1393         int i;
1394         for (i=0; i<=stack->b_num; i++) {
1395                 if ( (stack->bc[i].l3_id & mask)  ==  (l3id & mask)) return &stack->bc[i] ;
1396         }
1397         return stack_holder_find(stack,l3id);
1398 }
1399
1400
1401 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
1402 {
1403         int i;
1404         for (i=0; i<=stack->b_num; i++) {
1405                 if (stack->bc[i].l3_id == l3id) return &stack->bc[i] ;
1406         }
1407         return stack_holder_find(stack,l3id);
1408 }
1409
1410 static struct misdn_bchannel *find_bc_holded(struct misdn_stack *stack)
1411 {
1412         int i;
1413         for (i=0; i<=stack->b_num; i++) {
1414                 if (stack->bc[i].holded ) return &stack->bc[i] ;
1415         }
1416         return NULL;
1417 }
1418
1419
1420 static struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
1421 {
1422         struct misdn_stack* stack;
1423         int i;
1424
1425         for (stack=glob_mgr->stack_list;
1426              stack;
1427              stack=stack->next) {
1428                 for (i=0; i<=stack->b_num; i++) {
1429                         if ( (stack->bc[i].addr&STACK_ID_MASK)==(addr&STACK_ID_MASK) ||  stack->bc[i].layer_id== addr ) {
1430                                 return &stack->bc[i];
1431                         }
1432                 }
1433         }
1434         
1435         return NULL;
1436 }
1437
1438 struct misdn_bchannel *find_bc_by_confid(unsigned long confid)
1439 {
1440         struct misdn_stack* stack;
1441         int i;
1442         
1443         for (stack=glob_mgr->stack_list;
1444              stack;
1445              stack=stack->next) {
1446                 for (i=0; i<=stack->b_num; i++) {
1447                         if ( stack->bc[i].conf_id==confid ) {
1448                                 return &stack->bc[i];
1449                         }
1450                 }
1451         }
1452         return NULL;
1453 }
1454
1455
1456 static struct misdn_bchannel *find_bc_by_channel(int port, int channel)
1457 {
1458         struct misdn_stack* stack=find_stack_by_port(port);
1459         int i;
1460
1461         if (!stack) return NULL;        
1462         
1463         for (i=0; i<=stack->b_num; i++) {
1464                 if ( stack->bc[i].channel== channel ) {
1465                         return &stack->bc[i];
1466                 }
1467         }
1468                 
1469         return NULL;
1470 }
1471
1472
1473
1474
1475
1476 static int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
1477 {
1478         struct misdn_stack *stack=get_stack_by_bc(bc);
1479         
1480         if (!stack->nt) {
1481                 
1482                 switch (event) {
1483
1484                 case EVENT_CONNECT_ACKNOWLEDGE:
1485 #if 0
1486                         if ( !misdn_cap_is_speech(bc->capability)) {
1487                                 int ret=setup_bc(bc);
1488                                 if (ret == -EINVAL){
1489                                         cb_log(0,bc->port,"send_event: setup_bc failed\n");
1490                                 }
1491                         }
1492 #endif  
1493                         break;
1494                 case EVENT_CONNECT:
1495
1496                         if ( *bc->crypt_key ) {
1497                                 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);
1498                                 manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
1499                         }
1500                 case EVENT_ALERTING:
1501                 case EVENT_PROGRESS:
1502                 case EVENT_PROCEEDING:
1503                 case EVENT_SETUP_ACKNOWLEDGE:
1504
1505                 setup_bc(bc);
1506
1507                 case EVENT_SETUP:
1508                 {
1509                         if (bc->channel == 0xff) {
1510                                 if (!find_free_chan_in_stack(stack, bc, 0)) {
1511                                         cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n");
1512                                         bc->out_cause=34;
1513                                         misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1514                                         return -1;
1515                                 }
1516                         } 
1517
1518                         if (bc->channel >0 && bc->channel<255) {
1519                                 int ret=set_chan_in_stack(stack ,bc->channel);
1520                                 if (event == EVENT_SETUP && ret<0){
1521                                         /* empty bchannel */
1522                                         bc->channel=0;
1523                                         bc->out_cause=44;
1524                                         misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1525                                         return -1;
1526                                 }
1527                         }
1528                 }
1529                 break;
1530
1531                 case EVENT_RELEASE_COMPLETE:
1532                 case EVENT_RELEASE:
1533                         if (bc->channel>0)
1534                                 empty_chan_in_stack(stack,bc->channel);
1535                         int tmpcause=bc->cause; 
1536                         empty_bc(bc);
1537                         bc->cause=tmpcause;
1538                         clean_up_bc(bc);
1539                         break;
1540                 default:
1541                         break;
1542                 }
1543         } else {    /** NT MODE **/
1544                 
1545         }
1546         return 0;
1547 }
1548
1549 static int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
1550 {
1551         if (!stack) return -1;
1552   
1553         switch (frm->prim) {
1554         case CC_NEW_CR|INDICATION:
1555                 cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
1556
1557                 struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0, 1);
1558                 if (!bc) {
1559                         cb_log(0, stack->port, " --> !! lib: No free channel!\n");
1560                         return -1;
1561                 }
1562   
1563                 cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
1564                 bc->l3_id=frm->dinfo;
1565                 return 1;
1566         case CC_NEW_CR|CONFIRM:
1567                 return 1;
1568         case CC_NEW_CR|REQUEST:
1569                 return 1;
1570         case CC_RELEASE_CR|REQUEST:
1571                 return 1;
1572         case CC_RELEASE_CR|CONFIRM:
1573                 break;
1574         case CC_RELEASE_CR|INDICATION:
1575                 cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n",frm->dinfo);
1576                 {
1577                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, frm->dinfo);
1578                         struct misdn_bchannel dummybc;
1579       
1580                         if (!bc) {
1581                                 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
1582                                 memset (&dummybc,0,sizeof(dummybc));
1583                                 dummybc.port=stack->port;
1584                                 dummybc.l3_id=frm->dinfo;
1585                                 dummybc.nt=stack->nt;
1586                                 bc=&dummybc; 
1587                         }
1588       
1589                         if (bc) {
1590                                 cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo);
1591                                 if (bc->channel>0)
1592                                         empty_chan_in_stack(stack,bc->channel);
1593                                 empty_bc(bc);
1594                                 
1595                                 clean_up_bc(bc);
1596                                 
1597                                 dump_chan_list(stack);
1598                                 /*bc->pid = 0;*/
1599                                 bc->need_disconnect=0;
1600                                 bc->need_release=0;
1601                                 bc->need_release_complete=0;
1602                                 
1603                                 cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
1604                                 if (bc->stack_holder) {
1605                                         cb_log(4,stack->port, "REMOVEING Holder\n");
1606                                         stack_holder_remove( stack, bc);
1607                                         free(bc);
1608                                 }
1609                         }
1610                         else {
1611                                 if (stack->nt) 
1612                                         cb_log(4, stack->port, "BC with dinfo: %x  not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr);
1613                         }
1614       
1615                         return 1;
1616                 }
1617                 break;
1618         }
1619   
1620         return 0;
1621 }
1622
1623
1624 /*Emptys bc if it's reserved (no SETUP out yet)*/
1625 void misdn_lib_release(struct misdn_bchannel *bc)
1626 {
1627         struct misdn_stack *stack=get_stack_by_bc(bc);
1628
1629         if (!stack) {
1630                 cb_log(1,0,"misdn_release: No Stack found\n");
1631                 return;
1632         }
1633         
1634         if (bc->channel>0) {
1635                 empty_chan_in_stack(stack,bc->channel);
1636         }
1637         empty_bc(bc);
1638         clean_up_bc(bc);
1639 }
1640
1641
1642
1643
1644 int misdn_lib_get_port_up (int port) 
1645 { /* Pull Up L1 */ 
1646         struct misdn_stack *stack;
1647         
1648         for (stack=glob_mgr->stack_list;
1649              stack;
1650              stack=stack->next) {
1651                 
1652                 if (stack->port == port) {
1653
1654                         if (!stack->l1link)
1655                                 misdn_lib_get_l1_up(stack);
1656                         if (!stack->l2link)
1657                                 misdn_lib_get_l2_up(stack);
1658                         
1659                         return 0;
1660                 }
1661         }
1662         return 0;
1663 }
1664
1665
1666 int misdn_lib_get_port_down (int port) 
1667 { /* Pull Down L1 */ 
1668         struct misdn_stack *stack;
1669         for (stack=glob_mgr->stack_list;
1670              stack;
1671              stack=stack->next) {
1672                 if (stack->port == port) {
1673                                 if (stack->l2link)
1674                                         misdn_lib_get_l2_down(stack);
1675                                 misdn_lib_get_l1_down(stack);
1676                         return 0;
1677                 }
1678         }
1679         return 0;
1680 }
1681
1682 int misdn_lib_port_up(int port, int check)
1683 {
1684         struct misdn_stack *stack;
1685
1686
1687         for (stack=glob_mgr->stack_list;
1688              stack;
1689              stack=stack->next) {
1690                 
1691                 if (stack->port == port) {
1692
1693                         if (stack->blocked) {
1694                                 cb_log(0,port, "Port Blocked:%d L2:%d L1:%d\n", stack->blocked, stack->l2link, stack->l1link);
1695                                 return -1;
1696                         }
1697
1698                         if (stack->ptp ) {
1699
1700                                 if (stack->l1link && stack->l2link) {
1701                                         return 1;
1702                                 } else {
1703                                         cb_log(0,port, "Port Down L2:%d L1:%d\n",
1704                                                 stack->l2link, stack->l1link);
1705                                         return 0;
1706                                 }
1707                         } else {
1708                                 if ( !check || stack->l1link )
1709                                         return 1;
1710                                 else {
1711                                         cb_log(0,port, "Port down PMP\n");
1712                                         return 0;
1713                                 }
1714                         }
1715                 }
1716         }
1717   
1718         return -1;
1719 }
1720
1721
1722 int
1723 handle_event_nt(void *dat, void *arg)
1724 {
1725         manager_t *mgr = (manager_t *)dat;
1726         msg_t *msg = (msg_t *)arg;
1727         mISDNuser_head_t *hh;
1728         int reject=0;
1729
1730         struct misdn_stack *stack=find_stack_by_mgr(mgr);
1731         int port;
1732
1733         if (!msg || !mgr)
1734                 return(-EINVAL);
1735
1736         hh=(mISDNuser_head_t*)msg->data;
1737         port=stack->port;
1738         
1739         cb_log(5, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1740         {
1741                 switch(hh->prim){
1742                 case CC_RETRIEVE|INDICATION:
1743                 {
1744                         iframe_t frm; /* fake te frm to add callref to global callreflist */
1745                         frm.dinfo = hh->dinfo;
1746
1747                         frm.addr=stack->upper_id | FLG_MSG_DOWN;
1748
1749                         frm.prim = CC_NEW_CR|INDICATION;
1750                         
1751                         if (handle_cr( stack, &frm)< 0) {
1752                                 msg_t *dmsg;
1753                                 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1754                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1755                                 stack->nst.manager_l3(&stack->nst, dmsg);
1756                                 free_msg(msg);
1757                                 return 0;
1758                         }
1759                         
1760                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1761                         struct misdn_bchannel *hold_bc=stack_holder_find(stack,bc->l3_id);
1762                         cb_log(4, stack->port, "bc_l3id:%x holded_bc_l3id:%x\n",bc->l3_id, hold_bc->l3_id);
1763
1764                         if (hold_bc) {
1765                                 cb_log(4, stack->port, "REMOVEING Holder\n");
1766
1767                                 /*swap the backup to our new channel back*/
1768                                 stack_holder_remove(stack, hold_bc);
1769                                 memcpy(bc,hold_bc,sizeof(struct misdn_bchannel));
1770                                 free(hold_bc);
1771
1772                                 bc->holded=0;
1773                         }
1774                         
1775                 }
1776                         
1777                         break;
1778                         
1779                 case CC_SETUP|CONFIRM:
1780                 {
1781                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1782                         int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1783                         cb_log(4, stack->port, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID  is %x\n",l3id );
1784         
1785                         if (!bc) { cb_log(4, stack->port, "Bc Not found (after SETUP CONFIRM)\n"); return 0; }
1786                         cb_log (2,bc->port,"I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
1787                         bc->l3_id=l3id;
1788                         cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1789                 }
1790                 free_msg(msg);
1791                 return 0;
1792       
1793                 case CC_SETUP|INDICATION:
1794                 {
1795                         struct misdn_bchannel* bc=misdn_lib_get_free_bc(stack->port, 0, 1);
1796                         if (!bc) 
1797                         ERR_NO_CHANNEL:
1798                         {
1799                                 msg_t *dmsg;
1800                                 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
1801                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1802                                 stack->nst.manager_l3(&stack->nst, dmsg);
1803                                 free_msg(msg);
1804                                 return 0;
1805                         }
1806   
1807                         cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",hh->dinfo);
1808                         bc->l3_id=hh->dinfo;
1809                 }
1810                 break;
1811
1812                 case CC_CONNECT_ACKNOWLEDGE|INDICATION:
1813 #if 0
1814                 {
1815                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1816                         if (bc) {
1817                                 if ( !misdn_cap_is_speech(bc->capability)) {
1818                                         int ret=setup_bc(bc);
1819                                         if (ret == -EINVAL){
1820                                                 cb_log(0,bc->port,"send_event: setup_bc failed\n");
1821                                                 
1822                                         }
1823                                 }
1824                         }
1825                 }
1826 #endif
1827                 break;
1828                 
1829                 case CC_ALERTING|INDICATION:
1830                 case CC_PROCEEDING|INDICATION:
1831                 case CC_SETUP_ACKNOWLEDGE|INDICATION:
1832                         if(!stack->ptp) break;  
1833                 case CC_CONNECT|INDICATION:
1834                 {
1835 #if 0
1836                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1837                         
1838                         if (!bc) {
1839                                 msg_t *dmsg;
1840                                 cb_log(0, stack->port,"!!!! We didn't found our bc, dinfo:%x on this port.\n",hh->dinfo);
1841                                 
1842                                 cb_log(0, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
1843                                 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1844                                 stack->nst.manager_l3(&stack->nst, dmsg);
1845                                 free_msg(msg);
1846                                 return 0;
1847                                 
1848                         }
1849                         int ret=setup_bc(bc);
1850                         if (ret == -EINVAL){
1851                                 cb_log(0,bc->port,"handle_event_nt: setup_bc failed\n");
1852                                 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1853                         }
1854 #endif
1855                 }
1856                 break;
1857                 case CC_DISCONNECT|INDICATION:
1858                 {
1859                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1860                         if (!bc) {
1861                                 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1862                                 if (bc) { 
1863                                         int myprocid=bc->l3_id&0x0000ffff;
1864                                         hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1865                                         cb_log(3,stack->port,"Reject dinfo: %x cause:%d\n",hh->dinfo,bc->cause);
1866                                         reject=1;               
1867                                 }
1868                         }
1869                 }
1870                 break;
1871                 
1872                 case CC_FACILITY|INDICATION:
1873                 {
1874                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1875                         if (!bc) {
1876                                 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
1877                                 if (bc) { 
1878                                         int myprocid=bc->l3_id&0x0000ffff;
1879                                         hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
1880                                         cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
1881                                 }
1882                         }
1883                 }
1884                 break;
1885                 
1886                 case CC_RELEASE_COMPLETE|INDICATION:
1887                         break;
1888
1889                 case CC_SUSPEND|INDICATION:
1890                 {
1891                         msg_t *dmsg;
1892                         cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
1893                         dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
1894                         stack->nst.manager_l3(&stack->nst, dmsg);
1895                         free_msg(msg);
1896                         return 0;
1897                 }
1898                 break;
1899                 case CC_RESUME|INDICATION:
1900                         break;
1901
1902                 case CC_RELEASE|CONFIRM:
1903                         break;
1904                         
1905                 case CC_RELEASE|INDICATION:
1906                         break;
1907
1908                 case CC_RELEASE_CR|INDICATION:
1909                 {
1910                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1911                         struct misdn_bchannel dummybc;
1912                         iframe_t frm; /* fake te frm to remove callref from global callreflist */
1913                         frm.dinfo = hh->dinfo;
1914
1915                         frm.addr=stack->upper_id | FLG_MSG_DOWN;
1916
1917                         frm.prim = CC_RELEASE_CR|INDICATION;
1918                         cb_log(4, stack->port, " --> Faking Realease_cr for %x\n",frm.addr);
1919                         /** removing procid **/
1920                         if (!bc) {
1921                                 cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
1922                                 memset (&dummybc,0,sizeof(dummybc));
1923                                 dummybc.port=stack->port;
1924                                 dummybc.l3_id=hh->dinfo;
1925                                 dummybc.nt=stack->nt;
1926                                 bc=&dummybc; 
1927                         }
1928         
1929                         if (bc) {
1930                                 if ( (bc->l3_id & 0xff00) == 0xff00) {
1931                                         cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id&0xff);
1932                                         stack->procids[bc->l3_id&0xff] = 0 ;
1933                                 }
1934                         }
1935                         else cb_log(0, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is a bad port.\n");
1936         
1937                         if (handle_cr(stack, &frm)<0) {
1938                         }
1939
1940                         free_msg(msg);
1941                         return 0 ;
1942                 }
1943                 break;
1944       
1945                 case CC_NEW_CR|INDICATION:
1946                         /*  Got New CR for bchan, for now I handle this one in */
1947                         /*  connect_ack, Need to be changed */
1948                 {
1949                         struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1950                         int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
1951                         if (!bc) { cb_log(0, stack->port, " --> In NEW_CR: didn't found bc ??\n"); return -1;};
1952                         if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
1953                                 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
1954                                 stack->procids[bc->l3_id&0xff] = 0 ;
1955                         }
1956                         cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID  is %x\n",l3id );
1957         
1958                         bc->l3_id =l3id;
1959                         cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1960         
1961                         free_msg(msg);
1962                         return 0;
1963                 }
1964       
1965                 case DL_ESTABLISH | INDICATION:
1966                 case DL_ESTABLISH | CONFIRM:
1967                 {
1968                         cb_log(3, stack->port, "%% GOT L2 Activate Info.\n");
1969                         
1970                         if (stack->ptp && stack->l2link) {
1971                                 cb_log(0, stack->port, "%% GOT L2 Activate Info. but we're activated already.. this l2 is faulty, blocking port\n");
1972                                 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
1973                         }
1974                         
1975                         stack->l2link = 1;
1976                         stack->l2upcnt=0;
1977                         
1978                         free_msg(msg);
1979                         return 0;
1980                 }
1981                 break;
1982
1983
1984                 case DL_RELEASE | INDICATION:
1985                 case DL_RELEASE | CONFIRM:
1986                 {
1987                         if (stack->ptp) {
1988                                 cb_log(3 , stack->port, "%% GOT L2 DeActivate Info.\n");
1989
1990                                 if (stack->l2upcnt>3) {
1991                                         cb_log(0 , stack->port, "!!! Could not Get the L2 up after 3 Attemps!!!\n");
1992                                 }  else {
1993 #if 0
1994                                         if (stack->nt) misdn_lib_reinit_nt_stack(stack->port);
1995 #endif
1996                                         if (stack->l1link) {
1997                                                 misdn_lib_get_l2_up(stack);
1998                                                 stack->l2upcnt++;
1999                                         }
2000                                 }
2001                                 
2002                         } else 
2003                                 cb_log(3, stack->port, "%% GOT L2 DeActivate Info.\n");
2004                         
2005                         stack->l2link = 0;
2006                         free_msg(msg);
2007                         return 0;
2008                 }
2009                 break;
2010                 }
2011         }
2012         
2013         {
2014                 /*  Parse Events and fire_up to App. */
2015                 struct misdn_bchannel *bc;
2016                 struct misdn_bchannel dummybc;
2017                 
2018                 enum event_e event = isdn_msg_get_event(msgs_g, msg, 1);
2019     
2020                 bc=find_bc_by_l3id(stack, hh->dinfo);
2021     
2022                 if (!bc) {
2023       
2024                         cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x).\n", hh->dinfo);
2025                         memset (&dummybc,0,sizeof(dummybc));
2026                         dummybc.port=stack->port;
2027                         dummybc.l3_id=hh->dinfo;
2028                         dummybc.nt=stack->nt;
2029                         bc=&dummybc; 
2030                 }
2031                 if (bc ) {
2032                         isdn_msg_parse_event(msgs_g,msg,bc, 1);
2033
2034                         switch (event) {
2035                                 case EVENT_SETUP:
2036                                         if (bc->channel<=0 || bc->channel==0xff) {
2037                                                 bc->channel=find_free_chan_in_stack(stack,bc, 0);
2038                 
2039                                                 if (bc->channel<=0)
2040                                                         goto ERR_NO_CHANNEL;
2041                                         } else if (!stack->ptp) 
2042                                                 cb_log(3,stack->port," --> PTMP but channel requested\n"); 
2043
2044                                         int ret=set_chan_in_stack(stack, bc->channel);
2045                                         if (event==EVENT_SETUP && ret<0){
2046                                                 /* empty bchannel */
2047                                                 bc->channel=0;
2048                                                 bc->out_cause=44;
2049
2050                                                 goto ERR_NO_CHANNEL;
2051                                         } 
2052                                         break;
2053                                 case EVENT_RELEASE:
2054                                 case EVENT_RELEASE_COMPLETE:
2055                                         if (bc->channel>0)
2056                                                 empty_chan_in_stack(stack, bc->channel);
2057                                         int tmpcause=bc->cause; 
2058                                         empty_bc(bc);
2059                                         bc->cause=tmpcause;
2060                                         clean_up_bc(bc);
2061                                         break;
2062
2063                                 default:
2064                                 break;
2065                         }
2066                         
2067                         if(!isdn_get_info(msgs_g,event,1)) {
2068                                 cb_log(4, stack->port, "Unknown Event Ind: prim %x dinfo %x\n",hh->prim, hh->dinfo);
2069                         } else {
2070                                 if (reject) {
2071                                         switch(bc->cause){
2072                                                 case 17:
2073                                                         cb_log(1, stack->port, "Siemens Busy reject..\n");
2074
2075                                                         break;
2076                                                 default:
2077                                                         break;
2078                                         }
2079                                 }
2080                                 cb_event(event, bc, glob_mgr->user_data);
2081                         }
2082       
2083                 } else {
2084                         cb_log(4, stack->port, "No BC found with l3id: prim %x dinfo %x\n",hh->prim, hh->dinfo);
2085                 }
2086
2087                 free_msg(msg);
2088         }
2089
2090
2091         return 0;
2092 }
2093
2094
2095 static int handle_timers(msg_t* msg)
2096 {
2097         iframe_t *frm= (iframe_t*)msg->data;
2098         struct misdn_stack *stack; 
2099   
2100         /* Timer Stuff */
2101         switch (frm->prim) {
2102         case MGR_INITTIMER | CONFIRM:
2103         case MGR_ADDTIMER | CONFIRM:
2104         case MGR_DELTIMER | CONFIRM:
2105         case MGR_REMOVETIMER | CONFIRM:
2106                 free_msg(msg);
2107                 return(1);
2108         }
2109   
2110   
2111   
2112         if (frm->prim==(MGR_TIMER | INDICATION) ) {
2113                 for (stack = glob_mgr->stack_list;
2114                      stack;
2115                      stack = stack->next) {
2116                         itimer_t *it;
2117       
2118                         if (!stack->nt) continue;
2119       
2120                         it = stack->nst.tlist;
2121                         /* find timer */
2122                         for(it=stack->nst.tlist;
2123                             it;
2124                             it=it->next) {
2125                                 if (it->id == (int)frm->addr)
2126                                         break;
2127                         }
2128                         if (it) {
2129                                 int ret;
2130                                 ret = mISDN_write_frame(stack->midev, msg->data, frm->addr,
2131                                                         MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
2132                                 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
2133                                 ret = it->function(it->data);
2134                                 free_msg(msg);
2135                                 return 1;
2136                         }
2137                 }
2138     
2139                 cb_log(0, 0, "Timer Msg without Timer ??\n");
2140                 free_msg(msg);
2141                 return 1;
2142         }
2143   
2144         return 0;
2145 }
2146
2147
2148
2149 void misdn_lib_tone_generator_start(struct misdn_bchannel *bc)
2150 {
2151         bc->generate_tone=1;
2152 }
2153
2154 void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc)
2155 {
2156         bc->generate_tone=0;
2157 }
2158
2159
2160 static int do_tone(struct misdn_bchannel *bc, int len)
2161 {
2162         bc->tone_cnt=len;
2163         
2164         if (bc->generate_tone) {
2165                 cb_event(EVENT_TONE_GENERATE, bc, glob_mgr->user_data);
2166                 
2167                 if ( !bc->nojitter ) {
2168                         misdn_tx_jitter(bc,len);
2169                 }
2170                 
2171                 return 1;
2172         }
2173         
2174         return 0;
2175 }
2176
2177
2178 #ifdef MISDN_SAVE_DATA
2179 static void misdn_save_data(int id, char *p1, int l1, char *p2, int l2) 
2180 {
2181         char n1[32],n2[32];
2182
2183         sprintf(n1,"/tmp/misdn-rx-%d.raw",id);
2184         sprintf(n2,"/tmp/misdn-tx-%d.raw",id);
2185
2186         FILE *rx=fopen(n1,"a+"); 
2187         FILE *tx=fopen(n2,"a+");
2188
2189         if (!rx || !tx) {
2190                 cb_log(0,0,"Couldn't open files: %s\n",strerror(errno));
2191                 return ;
2192         }
2193         
2194         fwrite(p1,1,l1,rx);
2195         fwrite(p2,1,l2,tx);
2196         
2197         fclose(rx);
2198         fclose(tx);
2199
2200 }
2201 #endif
2202
2203 void misdn_tx_jitter(struct misdn_bchannel *bc, int len)
2204 {
2205         char buf[4096 + mISDN_HEADER_LEN];
2206         char *data=&buf[mISDN_HEADER_LEN];
2207         iframe_t *txfrm= (iframe_t*)buf;
2208         int jlen, r;
2209         
2210         jlen=cb_jb_empty(bc,data,len);
2211         
2212         if (jlen) {
2213 #ifdef MISDN_SAVE_DATA
2214                 misdn_save_data((bc->port*100+bc->channel), data, jlen, bc->bframe, bc->bframe_len);
2215 #endif
2216                 flip_buf_bits( data, jlen);
2217                 
2218                 if (jlen < len) {
2219                         cb_log(7,bc->port,"Jitterbuffer Underrun.\n");
2220                 }
2221                 
2222                 txfrm->prim = DL_DATA|REQUEST;
2223                 
2224                 txfrm->dinfo = 0;
2225                 
2226                 txfrm->addr = bc->addr|FLG_MSG_DOWN; /*  | IF_DOWN; */
2227                 
2228                 txfrm->len =jlen;
2229                 cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
2230
2231                 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
2232         } else {
2233 #define MISDN_GEN_SILENCE
2234 #ifdef MISDN_GEN_SILENCE
2235                 int cnt=len/TONE_SILENCE_SIZE;
2236                 int rest=len%TONE_SILENCE_SIZE;
2237                 int i;
2238
2239                 for (i=0; i<cnt; i++) {
2240                         memcpy(data, tone_silence_flip, TONE_SILENCE_SIZE );
2241                         data +=TONE_SILENCE_SIZE;
2242                 }
2243
2244                 if (rest) {
2245                         memcpy(data, tone_silence_flip, rest);
2246                 }
2247
2248                 txfrm->prim = DL_DATA|REQUEST;
2249
2250                 txfrm->dinfo = 0;
2251
2252                 txfrm->addr = bc->addr|FLG_MSG_DOWN; /*  | IF_DOWN; */
2253
2254                 txfrm->len =len;
2255                 cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
2256
2257                 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
2258 #endif
2259
2260         }
2261 }
2262
2263 static int handle_bchan(msg_t *msg)
2264 {
2265         iframe_t *frm= (iframe_t*)msg->data;
2266
2267
2268         struct misdn_bchannel *bc=find_bc_by_addr(frm->addr);
2269         
2270         if (!bc) {
2271                 cb_log(1,0,"handle_bchan: BC not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
2272                 return 0 ;
2273         }
2274         
2275         struct misdn_stack *stack=get_stack_by_bc(bc);
2276         
2277         if (!stack) {
2278                 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);
2279                 return 0;
2280         }
2281         
2282         switch (frm->prim) {
2283
2284         case MGR_SETSTACK| CONFIRM:
2285                 cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|CONFIRM pid:%d\n",bc->pid);
2286                 break;
2287                 
2288         case MGR_SETSTACK| INDICATION:
2289                 cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|IND pid:%d\n",bc->pid);
2290         break;
2291 #if 0
2292         AGAIN:
2293                 bc->addr = mISDN_get_layerid(stack->midev, bc->b_stid, bc->layer);
2294                 if (!bc->addr) {
2295
2296                         if (errno == EAGAIN) {
2297                                 usleep(1000);
2298                                 goto AGAIN;
2299                         }
2300                         
2301                         cb_log(0,stack->port,"$$$ Get Layer (%d) Id Error: %s\n",bc->layer,strerror(errno));
2302                         
2303                         /* we kill the channel later, when we received some
2304                            data. */
2305                         bc->addr= frm->addr;
2306                 } else if ( bc->addr < 0) {
2307                         cb_log(0, stack->port,"$$$ bc->addr <0 Error:%s\n",strerror(errno));
2308                         bc->addr=0;
2309                 }
2310                 
2311                 cb_log(4, stack->port," --> Got Adr %x\n", bc->addr);
2312
2313                 free_msg(msg);
2314         
2315                 
2316                 switch(bc->bc_state) {
2317                 case BCHAN_SETUP:
2318                         bc_state_change(bc,BCHAN_SETUPED);
2319                 break;
2320
2321                 case BCHAN_CLEAN_REQUEST:
2322                 default:
2323                         cb_log(0, stack->port," --> STATE WASN'T SETUP (but %s) in SETSTACK|IND pid:%d\n",bc_state2str(bc->bc_state), bc->pid);
2324                         clean_up_bc(bc);
2325                 }
2326                 return 1;
2327 #endif
2328
2329         case MGR_DELLAYER| INDICATION:
2330                 cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|IND pid:%d\n",bc->pid);
2331                 break;
2332                 
2333         case MGR_DELLAYER| CONFIRM:
2334                 cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|CNF pid:%d\n",bc->pid);
2335                 
2336                 bc->pid=0;
2337                 bc->addr=0;
2338                 
2339                 free_msg(msg);
2340                 return 1;
2341                 
2342         case PH_ACTIVATE | INDICATION:
2343         case DL_ESTABLISH | INDICATION:
2344                 cb_log(3, stack->port, "BCHAN: ACT Ind pid:%d\n", bc->pid);
2345
2346                 free_msg(msg);
2347                 return 1;    
2348
2349         case PH_ACTIVATE | CONFIRM:
2350         case DL_ESTABLISH | CONFIRM:
2351                 
2352                 cb_log(3, stack->port, "BCHAN: bchan ACT Confirm pid:%d\n",bc->pid);
2353                 free_msg(msg);
2354                 
2355                 return 1;    
2356
2357         case DL_ESTABLISH | REQUEST:
2358                 {
2359                         char buf[128];
2360                         mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN,  DL_ESTABLISH | CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
2361                 }
2362                 free_msg(msg);
2363                 return 1;
2364
2365         case DL_RELEASE|REQUEST:
2366                 {
2367                         char buf[128];
2368                         mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN,  DL_RELEASE| CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
2369                 }
2370                 free_msg(msg);
2371                 return 1;
2372                 
2373         case PH_DEACTIVATE | INDICATION:
2374         case DL_RELEASE | INDICATION:
2375                 cb_log (3, stack->port, "BCHAN: DeACT Ind pid:%d\n",bc->pid);
2376                 
2377                 free_msg(msg);
2378                 return 1;
2379     
2380         case PH_DEACTIVATE | CONFIRM:
2381         case DL_RELEASE | CONFIRM:
2382                 cb_log(3, stack->port, "BCHAN: DeACT Conf pid:%d\n",bc->pid);
2383                 
2384                 free_msg(msg);
2385                 return 1;
2386     
2387         case PH_CONTROL|INDICATION:
2388         {
2389                 unsigned int cont = *((unsigned int *)&frm->data.p);
2390                 
2391                 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);
2392
2393                 if ((cont&~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
2394                         int dtmf = cont & DTMF_TONE_MASK;
2395                         cb_log(4, stack->port, " --> DTMF TONE: %c\n",dtmf);
2396                         bc->dtmf=dtmf;
2397                         cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data);
2398         
2399                         free_msg(msg);
2400                         return 1;
2401                 }
2402                 if (cont == BF_REJECT) {
2403                         cb_log(4, stack->port, " --> BF REJECT\n");
2404                         free_msg(msg);
2405                         return 1;
2406                 }
2407                 if (cont == BF_ACCEPT) {
2408                         cb_log(4, stack->port, " --> BF ACCEPT\n");
2409                         free_msg(msg);
2410                         return 1;
2411                 }
2412         }
2413         break;
2414
2415         case PH_DATA|REQUEST:
2416         case DL_DATA|REQUEST:
2417                 cb_log(0, stack->port, "DL_DATA REQUEST \n");
2418                 do_tone(bc, 64);
2419                 
2420                 free_msg(msg);
2421                 return 1;
2422         
2423         
2424         case PH_DATA|INDICATION:
2425         case DL_DATA|INDICATION:
2426         {
2427                 bc->bframe = (void*)&frm->data.i;
2428                 bc->bframe_len = frm->len;
2429
2430                 /** Anyway flip the bufbits **/
2431                 if ( misdn_cap_is_speech(bc->capability) ) 
2432                         flip_buf_bits(bc->bframe, bc->bframe_len);
2433         
2434
2435                 if (!bc->bframe_len) {
2436                         cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
2437                         free_msg(msg);
2438                         return 1;
2439                 }
2440
2441                 if ( (bc->addr&STACK_ID_MASK) != (frm->addr&STACK_ID_MASK) ) {
2442                         cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
2443                         free_msg(msg);
2444                         return 1;
2445                 }
2446                 
2447 #if MISDN_DEBUG
2448                 cb_log(0, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
2449
2450 #endif
2451                 
2452                 if ( (bc->bc_state == BCHAN_ACTIVATED) && frm->len > 0) {
2453                         int t;
2454
2455 #ifdef MISDN_B_DEBUG
2456                         cb_log(0,bc->port,"do_tone START\n");
2457 #endif
2458                         t=do_tone(bc,frm->len);
2459
2460 #ifdef MISDN_B_DEBUG
2461                         cb_log(0,bc->port,"do_tone STOP (%d)\n",t);
2462 #endif
2463                         if (  !t ) {
2464                                 
2465                                 if ( misdn_cap_is_speech(bc->capability)) {
2466                                         if ( !bc->nojitter ) {
2467 #ifdef MISDN_B_DEBUG
2468                                                 cb_log(0,bc->port,"tx_jitter START\n");
2469 #endif
2470                                                 misdn_tx_jitter(bc,frm->len);
2471 #ifdef MISDN_B_DEBUG
2472                                                 cb_log(0,bc->port,"tx_jitter STOP\n");
2473 #endif
2474                                         }
2475                                 }
2476
2477 #ifdef MISDN_B_DEBUG    
2478                                 cb_log(0,bc->port,"EVENT_B_DATA START\n");
2479 #endif
2480                                 
2481                                 int i=cb_event( EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
2482 #ifdef MISDN_B_DEBUG    
2483                                 cb_log(0,bc->port,"EVENT_B_DATA STOP\n");
2484 #endif
2485                                 
2486                                 if (i<0) {
2487                                         cb_log(10,stack->port,"cb_event returned <0\n");
2488                                         /*clean_up_bc(bc);*/
2489                                 }
2490                         }
2491                 }
2492                 free_msg(msg);
2493                 return 1;
2494         }
2495
2496
2497         case PH_CONTROL | CONFIRM:
2498                 cb_log(4, stack->port, "PH_CONTROL|CNF bc->addr:%x\n", frm->addr);
2499                 free_msg(msg);
2500                 return 1;
2501
2502         case PH_DATA | CONFIRM:
2503         case DL_DATA|CONFIRM:
2504 #if MISDN_DEBUG
2505
2506                 cb_log(0, stack->port, "Data confirmed\n");
2507
2508 #endif
2509                 free_msg(msg);
2510                 return 1;
2511         case DL_DATA|RESPONSE:
2512 #if MISDN_DEBUG
2513                 cb_log(0, stack->port, "Data response\n");
2514
2515 #endif
2516                 break;
2517         }
2518   
2519         return 0;
2520 }
2521
2522
2523
2524 static int handle_frm_nt(msg_t *msg)
2525 {
2526         iframe_t *frm= (iframe_t*)msg->data;
2527         struct misdn_stack *stack;
2528         int err=0;
2529
2530         stack=find_stack_by_addr( frm->addr );
2531
2532         
2533   
2534         if (!stack || !stack->nt) {
2535                 return 0;
2536         }
2537
2538         
2539         if ((err=stack->nst.l1_l2(&stack->nst,msg))) {
2540     
2541                 if (nt_err_cnt > 0 ) {
2542                         if (nt_err_cnt < 100) {
2543                                 nt_err_cnt++; 
2544                                 cb_log(0, stack->port, "NT Stack sends us error: %d \n", err);
2545                         } else if (nt_err_cnt < 105){
2546                                 cb_log(0, stack->port, "NT Stack sends us error: %d over 100 times, so I'll stop this message\n", err);
2547                                 nt_err_cnt = - 1; 
2548                         }
2549                 }
2550                 free_msg(msg);
2551                 return 1;
2552                 
2553         }
2554         
2555         return 1;
2556 }
2557
2558
2559 static int handle_frm(msg_t *msg)
2560 {
2561         iframe_t *frm = (iframe_t*) msg->data;
2562         
2563         struct misdn_stack *stack=find_stack_by_addr(frm->addr);
2564
2565         if (!stack || stack->nt) {
2566                 return 0;
2567         }
2568         
2569         cb_log(4,stack?stack->port:0,"handle_frm: frm->addr:%x frm->prim:%x\n",frm->addr,frm->prim);
2570
2571         {
2572                 struct misdn_bchannel *bc;
2573                 int ret=handle_cr(stack, frm);
2574
2575                 if (ret<0) {
2576                         cb_log(3,stack?stack->port:0,"handle_frm: handle_cr <0 prim:%x addr:%x\n", frm->prim, frm->addr);
2577
2578
2579                 }
2580
2581                 if(ret) {
2582                         free_msg(msg);
2583                         return 1;
2584                 }
2585     
2586                 bc=find_bc_by_l3id(stack, frm->dinfo);
2587     
2588 handle_frm_bc:
2589                 if (bc ) {
2590                         enum event_e event = isdn_msg_get_event(msgs_g, msg, 0);
2591                         enum event_response_e response=RESPONSE_OK;
2592       
2593                         isdn_msg_parse_event(msgs_g,msg,bc, 0);
2594                         
2595                         /** Preprocess some Events **/
2596                         int ret=handle_event(bc, event, frm);
2597                         if (ret<0) {
2598                                 cb_log(0,stack->port,"couldn't handle event\n");
2599                                 free_msg(msg);
2600                                 return 1;
2601                         }
2602                         /*  shoot up event to App: */
2603                         cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2604       
2605                         if(!isdn_get_info(msgs_g,event,0)) 
2606                                 cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo);
2607                         else
2608                                 response=cb_event(event, bc, glob_mgr->user_data);
2609 #if 1
2610                         if (event == EVENT_SETUP) {
2611                                 switch (response) {
2612                                 case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
2613
2614                                         cb_log(0, stack->port, "TOTALY IGNORING SETUP \n");                                     
2615                                         
2616                                         break;
2617                                 case RESPONSE_IGNORE_SETUP:
2618                                         /* I think we should send CC_RELEASE_CR, but am not sure*/
2619                                         bc->out_cause=16;
2620                                 
2621                                 case RESPONSE_RELEASE_SETUP:
2622                                         misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
2623                                         if (bc->channel>0)
2624                                                 empty_chan_in_stack(stack, bc->channel);
2625                                         empty_bc(bc);
2626                                         bc_state_change(bc,BCHAN_CLEANED);
2627
2628                                         cb_log(0, stack->port, "GOT IGNORE SETUP\n");
2629
2630                                         
2631                                         break;
2632                                 case RESPONSE_OK:
2633                                         cb_log(4, stack->port, "GOT SETUP OK\n");
2634
2635                                         
2636                                         break;
2637                                 default:
2638                                         break;
2639                                 }
2640                         }
2641
2642                         cb_log(5, stack->port, "Freeing Msg on prim:%x \n",frm->prim);
2643
2644                         
2645                         free_msg(msg);
2646                         return 1;
2647 #endif
2648       
2649                 } else {
2650                         cb_log(0, stack->port, " --> Didn't find BC so temporarly creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
2651                         struct misdn_bchannel dummybc;
2652                         memset (&dummybc,0,sizeof(dummybc));
2653                         dummybc.port=stack->port;
2654                         dummybc.l3_id=frm->dinfo;
2655                         bc=&dummybc; 
2656                         goto handle_frm_bc;
2657                 }
2658         }
2659
2660         cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x \n",frm->prim);
2661         return 0;
2662 }
2663
2664
2665 static int handle_l1(msg_t *msg)
2666 {
2667         iframe_t *frm = (iframe_t*) msg->data;
2668         struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2669         int i ;
2670         
2671         if (!stack) return 0 ;
2672
2673         switch (frm->prim) {
2674         case PH_ACTIVATE | CONFIRM:
2675         case PH_ACTIVATE | INDICATION:
2676                 cb_log (3, stack->port, "L1: PH L1Link Up!\n");
2677                 stack->l1link=1;
2678                 
2679                 if (stack->nt) {
2680                         
2681                         if (stack->nst.l1_l2(&stack->nst, msg))
2682                                 free_msg(msg);
2683
2684                         if (stack->ptp)
2685                                 misdn_lib_get_l2_up(stack);
2686                 } else {
2687                         free_msg(msg);
2688                 }
2689                 
2690                 for (i=0;i<=stack->b_num; i++) {
2691                         if (stack->bc[i].evq != EVENT_NOTHING) {
2692                                 cb_log(4, stack->port, "Fireing Queued Event %s because L1 got up\n", isdn_get_info(msgs_g, stack->bc[i].evq, 0));
2693                                 misdn_lib_send_event(&stack->bc[i],stack->bc[i].evq);
2694                                 stack->bc[i].evq=EVENT_NOTHING;
2695                         }
2696                         
2697                 }
2698                 return 1;
2699
2700         case PH_ACTIVATE | REQUEST:
2701                 free_msg(msg);
2702                 cb_log(3,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
2703                 return 1;
2704                 
2705         case PH_DEACTIVATE | REQUEST:
2706                 free_msg(msg);
2707                 cb_log(3,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
2708                 return 1;
2709                 
2710         case PH_DEACTIVATE | CONFIRM:
2711         case PH_DEACTIVATE | INDICATION:
2712                 cb_log (3, stack->port, "L1: PH L1Link Down! \n");
2713                 
2714                 for (i=0; i<=stack->b_num; i++) {
2715                         if (global_state == MISDN_INITIALIZED)  {
2716                                 cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
2717                         }
2718                 }
2719                 
2720                 if (stack->nt) {
2721                         if (stack->nst.l1_l2(&stack->nst, msg))
2722                                 free_msg(msg);
2723                 } else {
2724                         free_msg(msg);
2725                 }
2726                 
2727                 stack->l1link=0;
2728                 stack->l2link=0;
2729                 return 1;
2730         }
2731   
2732         return 0;
2733 }
2734
2735 static int handle_l2(msg_t *msg)
2736 {
2737         iframe_t *frm = (iframe_t*) msg->data;
2738
2739         struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2740         
2741         if (!stack) {
2742                 return 0 ;
2743         }
2744         
2745         switch(frm->prim) {
2746
2747         case DL_ESTABLISH | REQUEST:
2748                 cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
2749                 return 1;
2750         case DL_RELEASE | REQUEST:
2751                 cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
2752                 return 1;
2753                 
2754         case DL_ESTABLISH | INDICATION:
2755         case DL_ESTABLISH | CONFIRM:
2756         {
2757                 cb_log (3, stack->port, "L2: L2Link Up! \n");
2758                 if (stack->ptp && stack->l2link) {
2759                         cb_log (-1, stack->port, "L2: L2Link Up! but it's already UP.. must be faulty, blocking port\n"); 
2760                         cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
2761                 }
2762                 stack->l2link=1;
2763                 free_msg(msg);
2764                 return 1;
2765         }
2766         break;
2767     
2768         case DL_RELEASE | INDICATION:
2769         case DL_RELEASE | CONFIRM:
2770         {
2771                 cb_log (3, stack->port, "L2: L2Link Down! \n");
2772                 stack->l2link=0;
2773                 
2774                 free_msg(msg);
2775                 return 1;
2776         }
2777         break;
2778         }
2779         return 0;
2780 }
2781
2782 static int handle_mgmt(msg_t *msg)
2783 {
2784         iframe_t *frm = (iframe_t*) msg->data;
2785
2786         if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
2787                 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
2788                 free_msg(msg);
2789                 return 1;
2790         }
2791         
2792         struct misdn_stack * stack=find_stack_by_addr(frm->addr);
2793         
2794         if (!stack) {
2795                 if (frm->prim == (MGR_DELLAYER|CONFIRM)) {
2796                         cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: %x !\n",
2797                                         frm->addr) ;
2798                         free_msg(msg);
2799                         return 1;
2800                 }
2801                 
2802                 return 0;
2803         }
2804         
2805         switch(frm->prim) {
2806         case MGR_SHORTSTATUS | INDICATION:
2807         case MGR_SHORTSTATUS | CONFIRM:
2808                 cb_log(5, 0, "MGMT: Short status dinfo %x\n",frm->dinfo);
2809                 
2810                 switch (frm->dinfo) {
2811                 case SSTATUS_L1_ACTIVATED:
2812                         cb_log(3, 0, "MGMT: SSTATUS: L1_ACTIVATED \n");
2813                         stack->l1link=1;
2814                 
2815                         break;
2816                 case SSTATUS_L1_DEACTIVATED:
2817                         cb_log(3, 0, "MGMT: SSTATUS: L1_DEACTIVATED \n");
2818                         stack->l1link=0;
2819
2820                         clear_l3(stack);
2821                         break;
2822
2823                 case SSTATUS_L2_ESTABLISHED:
2824                         cb_log(3, stack->port, "MGMT: SSTATUS: L2_ESTABLISH \n");
2825                         stack->l2link=1;
2826                         break;
2827                         
2828                 case SSTATUS_L2_RELEASED:
2829                         cb_log(3, stack->port, "MGMT: SSTATUS: L2_RELEASED \n");
2830                         stack->l2link=0;
2831                         break;
2832                 }
2833                 
2834                 free_msg(msg);
2835                 return 1;
2836                 
2837         case MGR_SETSTACK | INDICATION:
2838                 cb_log(4, stack->port, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
2839                 free_msg(msg);
2840                 return 1;
2841         case MGR_DELLAYER | CONFIRM:
2842                 cb_log(4, stack->port, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
2843                 free_msg(msg);
2844                 return 1;
2845                 
2846         }
2847         
2848         /*
2849         if ( (frm->prim & 0x0f0000) ==  0x0f0000) {
2850         cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
2851         free_msg(msg);
2852         return 1;
2853         } */
2854     
2855         return 0;
2856 }
2857
2858
2859 static msg_t *fetch_msg(int midev) 
2860 {
2861         msg_t *msg=alloc_msg(MAX_MSG_SIZE);
2862         int r;
2863
2864         if (!msg) {
2865                 cb_log(0, 0, "fetch_msg: alloc msg failed !!");
2866                 return NULL;
2867         }
2868
2869         AGAIN:
2870                 r=mISDN_read(midev,msg->data,MAX_MSG_SIZE, TIMEOUT_10SEC);
2871                 msg->len=r;
2872     
2873                 if (r==0) {
2874                         free_msg(msg); /* danger, cauz usualy freeing in main_loop */
2875                         cb_log(6,0,"Got empty Msg..\n");
2876                         return NULL;
2877                 }
2878
2879                 if (r<0) {
2880                         if (errno == EAGAIN) {
2881                                 /*we wait for mISDN here*/
2882                                 cb_log(4,0,"mISDN_read wants us to wait\n");
2883                                 usleep(5000);
2884                                 goto AGAIN;
2885                         }
2886                         
2887                         cb_log(0,0,"mISDN_read returned :%d error:%s (%d)\n",r,strerror(errno),errno); 
2888                 }
2889
2890 #if 0
2891                if  (!(frm->prim == (DL_DATA|INDICATION) )|| (frm->prim == (PH_DATA|INDICATION)))
2892                        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 );
2893 #endif
2894                 return msg;
2895 }
2896
2897 void misdn_lib_isdn_l1watcher(int port)
2898 {
2899         struct misdn_stack *stack;
2900
2901         for (stack = glob_mgr->stack_list; stack && (stack->port != port); stack = stack->next)
2902                 ;
2903
2904         if (stack) {
2905                 cb_log(4, port, "Checking L1 State\n"); 
2906                 if (!stack->l1link) {
2907                         cb_log(4, port, "L1 State Down, trying to get it up again\n");  
2908                         misdn_lib_get_short_status(stack);
2909                         misdn_lib_get_l1_up(stack); 
2910                         misdn_lib_get_l2_up(stack); 
2911                 }
2912         }
2913 }
2914
2915 static void misdn_lib_isdn_event_catcher(void *arg)
2916 {
2917         struct misdn_lib *mgr = arg;
2918         int zero_frm=0 , fff_frm=0 ;
2919         int midev= mgr->midev;
2920         int port=0;
2921         
2922         while (1) {
2923                 msg_t *msg = fetch_msg(midev); 
2924                 iframe_t *frm;
2925                 
2926                 
2927                 if (!msg) continue;
2928                 
2929                 frm = (iframe_t*) msg->data;
2930                 
2931                 /** When we make a call from NT2Ast we get this frames **/
2932                 if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
2933                         zero_frm++; 
2934                         free_msg(msg);
2935                         continue;
2936                 } else {
2937                         if (zero_frm) {
2938                                 cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm);
2939                                 zero_frm = 0 ;
2940                         }
2941                 }
2942                 
2943                 /** I get this sometimes after setup_bc **/
2944                 if (frm->len == 0 &&  frm->dinfo == 0 && frm->prim == 0xffffffff ) {
2945                         fff_frm++; 
2946                         free_msg(msg);
2947                         continue;
2948                 } else {
2949                         if (fff_frm) {
2950                                 cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm);
2951                                 fff_frm = 0 ;
2952                         }
2953                 }
2954                 
2955                 manager_isdn_handler(frm, msg);
2956         }
2957
2958 }
2959
2960
2961 /** App Interface **/
2962
2963 int te_lib_init() {
2964         char buff[1025];
2965         iframe_t *frm=(iframe_t*)buff;
2966         int midev=mISDN_open();
2967         int ret;
2968
2969         memset(buff,0,1025);
2970   
2971         if  (midev<=0) return midev;
2972   
2973 /* create entity for layer 3 TE-mode */
2974         mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
2975         ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
2976   
2977         if (ret < mISDN_HEADER_LEN) {
2978         noentity:
2979                 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n",strerror(errno));
2980                 exit(-1);
2981         }
2982   
2983         entity = frm->dinfo & 0xffff ;
2984   
2985         if (!entity)
2986                 goto noentity;
2987
2988         return midev;
2989   
2990 }
2991
2992 void te_lib_destroy(int midev)
2993 {
2994         char buf[1024];
2995         mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
2996
2997         cb_log(4, 0, "Entetity deleted\n");
2998         mISDN_close(midev);
2999         cb_log(4, 0, "midev closed\n");
3000 }
3001
3002
3003
3004 void misdn_lib_transfer(struct misdn_bchannel* holded_bc)
3005 {
3006         holded_bc->holded=0;
3007 }
3008
3009 struct misdn_bchannel *manager_find_bc_by_pid(int pid)
3010 {
3011         struct misdn_stack *stack;
3012         int i;
3013   
3014         for (stack=glob_mgr->stack_list;
3015              stack;
3016              stack=stack->next) {
3017                 for (i=0; i<=stack->b_num; i++)
3018                         if (stack->bc[i].pid == pid) return &stack->bc[i];
3019         }
3020   
3021         return NULL;
3022 }
3023
3024 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc)
3025 {
3026         struct misdn_stack *stack=get_stack_by_bc(bc);
3027         return find_bc_holded(stack);
3028 }
3029
3030
3031
3032 static void prepare_bc(struct misdn_bchannel*bc, int channel)
3033 {
3034         bc->channel = channel;
3035         bc->channel_preselected = channel?1:0;
3036         bc->in_use = 1;
3037         bc->need_disconnect=1;
3038         bc->need_release=1;
3039         bc->need_release_complete=1;
3040         bc->cause=16;
3041
3042         if (++mypid>5000) mypid=1;
3043         bc->pid=mypid;
3044
3045 #if 0
3046         bc->addr=0;
3047         bc->b_stid=0;
3048         bc->layer_id=0;
3049 #endif
3050 }
3051
3052 struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel, int inout)
3053 {
3054         struct misdn_stack *stack;
3055         int i;
3056         
3057         if (channel < 0 || channel > MAX_BCHANS) {
3058                 cb_log(0,port,"Requested channel out of bounds (%d)\n",channel);
3059                 return NULL;
3060         }
3061
3062         for (stack=glob_mgr->stack_list; stack; stack=stack->next) {
3063     
3064                 if (stack->port == port) {
3065                         if (stack->blocked) {
3066                                 cb_log(0,port,"Port is blocked\n");
3067                                 return NULL;
3068                         }
3069                 
3070                         if (channel > 0) {
3071                                 if (channel <= stack->b_num) {
3072                                         for (i = 0; i < stack->b_num; i++) {
3073                                                 if (stack->bc[i].in_use && stack->bc[i].channel == channel) {
3074                                                         cb_log(0,port,"Requested channel:%d on port:%d is already in use\n",channel, port);
3075                                                         return NULL;
3076                                                 }
3077                                         }
3078                                 } else {
3079                                         cb_log(0,port,"Requested channel:%d is out of bounds on port:%d\n",channel, port);
3080                                         return NULL;
3081                                 }
3082                         }
3083
3084                         int maxnum=inout&&!stack->pri&&!stack->ptp?stack->b_num+1:stack->b_num;
3085                         for (i = 0; i <maxnum; i++) {
3086                                 if (!stack->bc[i].in_use) {
3087                                         /* 3. channel on bri means CW*/
3088                                         if (!stack->pri && i==stack->b_num)
3089                                                 stack->bc[i].cw=1;
3090
3091                                         prepare_bc(&stack->bc[i], channel);
3092                                         return &stack->bc[i];
3093                                 }
3094                         }
3095
3096                         cb_log(1,port,"There is no free channel on port (%d)\n",port);
3097                         return NULL;
3098                 }
3099         }
3100
3101         cb_log(0,port,"Port is not configured (%d)\n",port);
3102         return NULL;
3103 }
3104
3105
3106 static char *fac2str (enum FacFunction func)
3107 {
3108         struct arr_el { 
3109                 enum FacFunction p; 
3110                 char *s ; 
3111         } arr[] = {
3112                 { Fac_None, "Fac_None" },
3113                 { Fac_CD, "Fac_CD"},
3114         };
3115         
3116         int i;
3117         
3118         for (i=0; i < sizeof(arr)/sizeof( struct arr_el) ; i ++)
3119                 if ( arr[i].p==func) return arr[i].s;
3120         
3121         return "unknown";
3122 }
3123
3124 void misdn_lib_log_ies(struct misdn_bchannel *bc)
3125 {
3126         if (!bc) return;
3127
3128         struct misdn_stack *stack=get_stack_by_bc(bc);
3129
3130         if (!stack) return;
3131
3132         cb_log(2, stack->port, " --> channel:%d mode:%s cause:%d ocause:%d rad:%s cad:%s\n", bc->channel, stack->nt?"NT":"TE", bc->cause, bc->out_cause, bc->rad, bc->cad);
3133         
3134         cb_log(2, stack->port,
3135                " --> info_dad:%s onumplan:%c dnumplan:%c rnumplan:%c cpnnumplan:%c\n",
3136                bc->info_dad,
3137                bc->onumplan>=0?'0'+bc->onumplan:' ',
3138                bc->dnumplan>=0?'0'+bc->dnumplan:' ',
3139                bc->rnumplan>=0?'0'+bc->rnumplan:' ',
3140                bc->cpnnumplan>=0?'0'+bc->cpnnumplan:' '
3141                 );
3142         
3143         cb_log(3, stack->port, " --> caps:%s pi:%x keypad:%s sending_complete:%d\n", bearer2str(bc->capability),bc->progress_indicator, bc->keypad, bc->sending_complete);
3144         cb_log(4, stack->port, " --> screen:%d --> pres:%d\n",
3145                         bc->screen, bc->pres);
3146         
3147         cb_log(4, stack->port, " --> addr:%x l3id:%x b_stid:%x layer_id:%x\n", bc->addr, bc->l3_id, bc->b_stid, bc->layer_id);
3148         
3149         cb_log(4, stack->port, " --> facility:%s out_facility:%s\n",fac2str(bc->fac_in.Function),fac2str(bc->fac_out.Function));
3150
3151         cb_log(5, stack->port, " --> urate:%d rate:%d mode:%d user1:%d\n", bc->urate, bc->rate, bc->mode,bc->user1);
3152         
3153         cb_log(5, stack->port, " --> bc:%x h:%d sh:%d\n", bc, bc->holded, bc->stack_holder);
3154 }
3155
3156 int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
3157 {
3158         msg_t *msg; 
3159         int err = -1 ;
3160         int ret=0;
3161   
3162         if (!bc) goto ERR; 
3163         
3164         struct misdn_stack *stack=get_stack_by_bc(bc);
3165         
3166         if (!stack) {
3167                 cb_log(0,bc->port,"SENDEVENT: no Stack for event:%s oad:%s dad:%s \n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad);
3168                 return -1;
3169         }
3170         
3171         cb_log(6,stack->port,"SENDEVENT: stack->nt:%d stack->uperid:%x\n",stack->nt, stack->upper_id);
3172
3173         if ( stack->nt && !stack->l1link) {
3174                 /** Queue Event **/
3175                 bc->evq=event;
3176                 cb_log(1, stack->port, "Queueing Event %s because L1 is down (btw. Activating L1)\n", isdn_get_info(msgs_g, event, 0));
3177                 misdn_lib_get_l1_up(stack);
3178                 return 0;
3179         }
3180         
3181         cb_log(1, stack->port, "I SEND:%s oad:%s dad:%s pid:%d\n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad, bc->pid);
3182         cb_log(4, stack->port, " --> bc_state:%s\n",bc_state2str(bc->bc_state));
3183         misdn_lib_log_ies(bc);
3184         
3185         switch (event) {
3186         case EVENT_SETUP:
3187                 if (create_process(glob_mgr->midev, bc)<0) {
3188                         cb_log(0,  stack->port, " No free channel at the moment @ send_event\n");
3189
3190                         err=-ENOCHAN;
3191                         goto ERR;
3192                 }
3193 #if 0
3194         if (stack->nt) {
3195                 ret=setup_bc(bc);
3196                 if (ret == -EINVAL) {
3197                         cb_log(0,bc->port,"send_event: setup_bc failed\n");
3198                 }
3199         }
3200 #endif
3201                 break;
3202
3203         case EVENT_PROGRESS:
3204         case EVENT_ALERTING:
3205         case EVENT_PROCEEDING:
3206         case EVENT_SETUP_ACKNOWLEDGE:
3207                 if (!bc->nt && !stack->ptp) break;
3208
3209         case EVENT_CONNECT:
3210         case EVENT_RETRIEVE_ACKNOWLEDGE:
3211
3212                 if (stack->nt) {
3213                         if (bc->channel <=0 ) { /*  else we have the channel already */
3214                                 if (!find_free_chan_in_stack(stack, bc, 0)) {
3215                                         cb_log(0, stack->port, " No free channel at the moment\n");
3216                                         /*FIXME: add disconnect*/
3217                                         err=-ENOCHAN;
3218                                         goto ERR;
3219                                 }
3220                                 
3221                                 if (set_chan_in_stack(stack ,bc->channel)<0) {
3222                                         /*FIXME: add disconnect*/
3223                                         err=-ENOCHAN;
3224                                         goto ERR;
3225                                 }
3226                         }
3227                         /* Its that i generate channels */
3228                 }
3229
3230                 ret=setup_bc(bc);
3231                 if (ret == -EINVAL) {
3232                         cb_log(0,bc->port,"send_event: setup_bc failed\n");
3233                 }
3234
3235                 if (misdn_cap_is_speech(bc->capability)) {
3236                         if ((event==EVENT_CONNECT)||(event==EVENT_RETRIEVE_ACKNOWLEDGE)) {
3237                                 if ( *bc->crypt_key ) {
3238                                         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);
3239                                         
3240                                         manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
3241                                 }
3242                                 
3243                                 if (!bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
3244                                 manager_ec_enable(bc);
3245                                 
3246                                 if (bc->txgain != 0) {
3247                                         cb_log(4, stack->port,  "--> Changing txgain to %d\n", bc->txgain);
3248                                         manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
3249                                 }
3250                                 
3251                                 if ( bc->rxgain != 0 ) {
3252                                         cb_log(4, stack->port,  "--> Changing rxgain to %d\n", bc->rxgain);
3253                                         manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
3254                                 }
3255                         }
3256                 }
3257                 break;
3258
3259         case EVENT_HOLD_ACKNOWLEDGE:
3260         {
3261                 struct misdn_bchannel *holded_bc=malloc(sizeof(struct misdn_bchannel));
3262                 if (!holded_bc) {
3263                         cb_log(0,bc->port, "Could not allocate holded_bc!!!\n");
3264                         return -1;
3265                 }
3266
3267                 /*backup the bc*/
3268                 memcpy(holded_bc,bc,sizeof(struct misdn_bchannel));
3269                 holded_bc->holded=1;
3270                 bc_state_change(holded_bc,BCHAN_CLEANED);
3271
3272                 stack_holder_add(stack,holded_bc);
3273         
3274                 /*kill the bridge and clean the bchannel*/
3275                 if (stack->nt) {
3276                         if (bc->bc_state == BCHAN_BRIDGED) {
3277                                 misdn_split_conf(bc,bc->conf_id);
3278                                 struct misdn_bchannel *bc2=find_bc_by_confid(bc->conf_id);
3279                                 if (!bc2) 
3280                                         cb_log(0,bc->port,"We have no second bc in bridge???\n");
3281                                 else 
3282                                         misdn_split_conf(bc2,bc->conf_id);
3283                         }
3284
3285                         if (bc->channel>0)
3286                                 empty_chan_in_stack(stack,bc->channel);
3287                         empty_bc(bc);
3288                         clean_up_bc(bc);
3289                 }
3290                 
3291         }
3292         break;
3293
3294         /* finishing the channel eh ? */
3295         case EVENT_DISCONNECT:
3296                 if (!bc->need_disconnect) {
3297                         cb_log(0,bc->port," --> we have already send Disconnect\n");
3298                         return -1;
3299                 }
3300                 
3301                 bc->need_disconnect=0;
3302                 break;
3303         case EVENT_RELEASE:
3304                 if (!bc->need_release) {
3305                         cb_log(0,bc->port," --> we have already send Release\n");
3306                         return -1;
3307                 }
3308                 bc->need_disconnect=0;
3309                 bc->need_release=0;
3310                 break;
3311         case EVENT_RELEASE_COMPLETE:
3312                 if (!bc->need_release_complete) {
3313                         cb_log(0,bc->port," --> we have already send Release_complete\n");
3314                         return -1;
3315                 }
3316                 bc->need_disconnect=0;
3317                 bc->need_release=0;
3318                 bc->need_release_complete=0;
3319
3320                 if (!stack->nt) {
3321                         /*create clenaup in TE*/
3322                         if (bc->channel>0)
3323                                 empty_chan_in_stack(stack,bc->channel);
3324                         int tmpcause=bc->cause; 
3325                         empty_bc(bc);
3326                         bc->cause=tmpcause;
3327                         clean_up_bc(bc);
3328                 }
3329                 break;
3330     
3331         case EVENT_CONNECT_ACKNOWLEDGE:
3332
3333                 if ( bc->nt || misdn_cap_is_speech(bc->capability)) {
3334                         int ret=setup_bc(bc);
3335                         if (ret == -EINVAL){
3336                                 cb_log(0,bc->port,"send_event: setup_bc failed\n");
3337                                 
3338                         }
3339                 }
3340         
3341                 
3342                 if (misdn_cap_is_speech(bc->capability)) {
3343                         if (  !bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
3344                         manager_ec_enable(bc);
3345
3346                         if ( bc->txgain != 0 ) {
3347                                 cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
3348                                 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
3349                         }
3350                         if ( bc->rxgain != 0 ) {
3351                                 cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
3352                                 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
3353                         }
3354                 }
3355                 break;
3356     
3357         default:
3358                 break;
3359         }
3360   
3361         /* Later we should think about sending bchannel data directly to misdn. */
3362         msg = isdn_msg_build_event(msgs_g, bc, event, stack->nt);
3363         msg_queue_tail(&stack->downqueue, msg);
3364         sem_post(&glob_mgr->new_msg);
3365   
3366         return 0;
3367   
3368  ERR:
3369         return -1; 
3370 }
3371
3372
3373 static int handle_err(msg_t *msg)
3374 {
3375         iframe_t *frm = (iframe_t*) msg->data;
3376
3377
3378         if (!frm->addr) {
3379                 static int cnt=0;
3380                 if (!cnt)
3381                         cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x\n",frm->prim,frm->dinfo);
3382                 cnt++;
3383                 if (cnt>100) {
3384                         cb_log(0,0,"mISDN Msg without Address pr:%x dinfo:%x (already more than 100 of them)\n",frm->prim,frm->dinfo);
3385                         cnt=0;
3386                 }
3387                 
3388                 free_msg(msg);
3389                 return 1;
3390                 
3391         }
3392         
3393         switch (frm->prim) {
3394                 case MGR_SETSTACK|INDICATION:
3395                         return handle_bchan(msg);
3396                 break;
3397
3398                 case MGR_SETSTACK|CONFIRM:
3399                 case MGR_CLEARSTACK|CONFIRM:
3400                         free_msg(msg) ; 
3401                         return 1;
3402                 break;
3403
3404                 case DL_DATA|CONFIRM:
3405                         cb_log(4,0,"DL_DATA|CONFIRM\n");
3406                         free_msg(msg);
3407                         return 1;
3408
3409                 case PH_CONTROL|CONFIRM:
3410                         cb_log(4,0,"PH_CONTROL|CONFIRM\n");
3411                         free_msg(msg);
3412                         return 1;
3413
3414                 case DL_DATA|INDICATION:
3415                 {
3416                         int port=(frm->addr&MASTER_ID_MASK) >> 8;
3417                         int channel=(frm->addr&CHILD_ID_MASK) >> 16;
3418
3419                         /*we flush the read buffer here*/
3420                         
3421                         cb_log(9,0,"BCHAN DATA without BC: addr:%x port:%d channel:%d\n",frm->addr, port,channel);
3422                         
3423                         free_msg(msg) ; 
3424                         return 1;
3425                         
3426                         
3427                         struct misdn_bchannel *bc=find_bc_by_channel( port , channel);
3428
3429                         if (!bc) {
3430                                 struct misdn_stack *stack=find_stack_by_port( port );
3431
3432                                 if (!stack) {
3433                                         cb_log(0,0," --> stack not found\n");
3434                                         free_msg(msg);
3435                                         return 1;
3436                                 }
3437                                 
3438                                 cb_log(0,0," --> bc not found by channel\n");
3439                                 if (stack->l2link)
3440                                         misdn_lib_get_l2_down(stack);
3441
3442                                 if (stack->l1link)
3443                                         misdn_lib_get_l1_down(stack);
3444
3445                                 free_msg(msg);
3446                                 return 1;
3447                         }
3448                         
3449                         cb_log(3,port," --> BC in state:%s\n", bc_state2str(bc->bc_state));
3450                 }
3451         }
3452
3453         return 0;
3454 }
3455
3456 #if 0
3457 static int queue_l2l3(msg_t *msg)
3458 {
3459         iframe_t *frm= (iframe_t*)msg->data;
3460         struct misdn_stack *stack;
3461         stack=find_stack_by_addr( frm->addr );
3462
3463         
3464         if (!stack) {
3465                 return 0;
3466         }
3467
3468         msg_queue_tail(&stack->upqueue, msg);
3469         sem_post(&glob_mgr->new_msg);
3470         return 1;
3471 }
3472 #endif
3473
3474 int manager_isdn_handler(iframe_t *frm ,msg_t *msg)
3475 {  
3476
3477         if (frm->dinfo==0xffffffff && frm->prim==(PH_DATA|CONFIRM)) {
3478                 cb_log(0,0,"SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n");
3479         }
3480
3481         if ( ((frm->addr | ISDN_PID_BCHANNEL_BIT )>> 28 ) == 0x5) {
3482                 if (handle_bchan(msg)) {
3483                         return 0 ;
3484                 }
3485         }       
3486
3487 #ifdef RECV_FRM_SYSLOG_DEBUG
3488         syslog(LOG_NOTICE,"mISDN recv: P(%02d): ADDR:%x PRIM:%x DINFO:%x\n",stack->port, frm->addr, frm->prim, frm->dinfo);
3489 #endif
3490
3491         if (handle_timers(msg)) 
3492                 return 0 ;
3493
3494         
3495         if (handle_mgmt(msg)) 
3496                 return 0 ; 
3497         
3498         if (handle_l2(msg)) 
3499                 return 0 ;
3500
3501         /* Its important to handle l1 AFTER l2  */
3502         if (handle_l1(msg)) 
3503                 return 0 ;
3504         
3505         if (handle_frm_nt(msg)) {
3506                 return 0;
3507         }
3508
3509         if (handle_frm(msg)) {
3510                 return 0;
3511         }
3512
3513         if (handle_err(msg)) {
3514                 return 0 ;
3515         }
3516
3517         cb_log(0, 0, "Unhandled Message: prim %x len %d from addr %x, dinfo %x on this port.\n",frm->prim, frm->len, frm->addr, frm->dinfo);            
3518         free_msg(msg);
3519         
3520
3521         return 0;
3522 }
3523
3524
3525
3526
3527 int misdn_lib_get_port_info(int port)
3528 {
3529         msg_t *msg=alloc_msg(MAX_MSG_SIZE);
3530         iframe_t *frm;
3531         struct misdn_stack *stack=find_stack_by_port(port);
3532         if (!msg) {
3533                 cb_log(0, port, "misgn_lib_get_port: alloc_msg failed!\n");
3534                 return -1;
3535         }
3536         frm=(iframe_t*)msg->data;
3537         if (!stack ) {
3538                 cb_log(0, port, "There is no Stack for this port.\n");
3539                 return -1;
3540         }
3541         /* activate bchannel */
3542         frm->prim = CC_STATUS_ENQUIRY | REQUEST;
3543
3544         frm->addr = stack->upper_id| FLG_MSG_DOWN;
3545
3546         frm->dinfo = 0;
3547         frm->len = 0;
3548   
3549         msg_queue_tail(&glob_mgr->activatequeue, msg);
3550         sem_post(&glob_mgr->new_msg);
3551
3552   
3553         return 0; 
3554 }
3555
3556
3557 int queue_cleanup_bc(struct misdn_bchannel *bc) 
3558 {
3559         msg_t *msg=alloc_msg(MAX_MSG_SIZE);
3560         iframe_t *frm;
3561         if (!msg) {
3562                 cb_log(0, bc->port, "misgn_lib_get_port: alloc_msg failed!\n");
3563                 return -1;
3564         }
3565         frm=(iframe_t*)msg->data;
3566
3567         /* activate bchannel */
3568         frm->prim = MGR_CLEARSTACK| REQUEST;
3569
3570         frm->addr = bc->l3_id;
3571
3572         frm->dinfo = bc->port;
3573         frm->len = 0;
3574   
3575         msg_queue_tail(&glob_mgr->activatequeue, msg);
3576         sem_post(&glob_mgr->new_msg);
3577
3578         return 0; 
3579