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