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