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