2 * Chan_Misdn -- Channel Driver for Asterisk
6 * Copyright (C) 2004, Christian Richter
8 * Christian Richter <crich@beronet.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
15 * \brief Interface to mISDN
16 * \author Christian Richter <crich@beronet.com>
20 <support_level>extended</support_level>
25 #include <mISDNuser/isdn_debug.h>
27 #include "isdn_lib_intern.h"
30 enum event_response_e (*cb_event)(enum event_e event, struct misdn_bchannel *bc, void *user_data);
32 void (*cb_log)(int level, int port, char *tmpl, ...)
33 __attribute__ ((format (printf, 3, 4)));
35 int (*cb_jb_empty)(struct misdn_bchannel *bc, char *buffer, int len);
39 * Define ARRAY_LEN() because I cannot
40 * #include "asterisk/utils.h"
42 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
44 #include "asterisk/causes.h"
46 void misdn_join_conf(struct misdn_bchannel *bc, int conf_id);
47 void misdn_split_conf(struct misdn_bchannel *bc, int conf_id);
49 int misdn_lib_get_l2_up(struct misdn_stack *stack);
51 struct misdn_stack *get_misdn_stack(void);
53 int misdn_lib_port_is_pri(int port)
55 struct misdn_stack *stack=get_misdn_stack();
56 for ( ; stack; stack=stack->next) {
57 if (stack->port == port) {
65 int misdn_lib_port_is_nt(int port)
67 struct misdn_stack *stack=get_misdn_stack();
68 for ( ; stack; stack=stack->next) {
69 if (stack->port == port) {
77 void misdn_make_dummy(struct misdn_bchannel *dummybc, int port, int l3id, int nt, int channel)
79 memset (dummybc,0,sizeof(struct misdn_bchannel));
82 dummybc->l3_id = MISDN_ID_DUMMY;
88 dummybc->channel=channel;
91 int misdn_lib_port_block(int port)
93 struct misdn_stack *stack=get_misdn_stack();
94 for ( ; stack; stack=stack->next) {
95 if (stack->port == port) {
104 int misdn_lib_port_unblock(int port)
106 struct misdn_stack *stack=get_misdn_stack();
107 for ( ; stack; stack=stack->next) {
108 if (stack->port == port) {
117 int misdn_lib_is_port_blocked(int port)
119 struct misdn_stack *stack=get_misdn_stack();
120 for ( ; stack; stack=stack->next) {
121 if (stack->port == port) {
122 return stack->blocked;
128 int misdn_lib_is_ptp(int port)
130 struct misdn_stack *stack=get_misdn_stack();
131 for ( ; stack; stack=stack->next) {
132 if (stack->port == port) return stack->ptp;
137 int misdn_lib_get_maxchans(int port)
139 struct misdn_stack *stack=get_misdn_stack();
140 for ( ; stack; stack=stack->next) {
141 if (stack->port == port) {
152 struct misdn_stack *get_stack_by_bc(struct misdn_bchannel *bc)
154 struct misdn_stack *stack = get_misdn_stack();
159 for ( ; stack; stack = stack->next) {
160 if (bc->port == stack->port)
168 void get_show_stack_details(int port, char *buf)
170 struct misdn_stack *stack = get_misdn_stack();
172 for (; stack; stack = stack->next) {
173 if (stack->port == port) {
179 sprintf(buf, "* Port %2d Type %s Prot. %s L2Link %s L1Link:%s Blocked:%d",
181 stack->nt ? "NT" : "TE",
182 stack->ptp ? "PTP" : "PMP",
183 (stack->nt && !stack->ptp) ? "UNKN" : stack->l2link ? "UP " : "DOWN",
184 stack->l1link ? "UP " : "DOWN",
192 static int nt_err_cnt =0 ;
199 static enum global_states global_state=MISDN_INITIALIZING;
202 #include <mISDNuser/net_l2.h>
203 #include <mISDNuser/tone.h>
205 #include <semaphore.h>
209 #include "isdn_lib.h"
213 /*! \brief mISDN device handle returned by mISDN_open() */
216 pthread_t event_thread;
217 pthread_t event_handler_thread;
221 msg_queue_t activatequeue;
225 struct misdn_stack *stack_list;
229 #define ECHOCAN_ON 123
230 #define ECHOCAN_OFF 124
233 #define MISDN_DEBUG 0
235 void misdn_tx_jitter(struct misdn_bchannel *bc, int len);
237 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id);
239 int manager_isdn_handler(iframe_t *frm ,msg_t *msg);
241 int misdn_lib_port_restart(int port);
242 int misdn_lib_pid_restart(int pid);
244 extern struct isdn_msg msgs_g[];
246 #define ISDN_PID_L3_B_USER 0x430000ff
247 #define ISDN_PID_L4_B_USER 0x440000ff
249 /* #define MISDN_IBUF_SIZE 1024 */
250 #define MISDN_IBUF_SIZE 512
252 /* Fine Tuning of Inband Signalling time */
253 #define TONE_ALERT_CNT 41 /* 1 Sec */
254 #define TONE_ALERT_SILENCE_CNT 200 /* 4 Sec */
256 #define TONE_BUSY_CNT 20 /* ? */
257 #define TONE_BUSY_SILENCE_CNT 48 /* ? */
261 static struct misdn_lib *glob_mgr;
263 static char tone_425_flip[TONE_425_SIZE];
264 static char tone_silence_flip[TONE_SILENCE_SIZE];
266 static void misdn_lib_isdn_event_catcher(void *arg);
267 static int handle_event_nt(void *dat, void *arg);
270 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder);
271 void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder);
272 struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id);
274 /* from isdn_lib.h */
276 void te_lib_destroy(int midev) ;
277 struct misdn_bchannel *manager_find_bc_by_pid(int pid);
278 void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len);
279 void manager_clean_bc(struct misdn_bchannel *bc );
280 void manager_bchannel_setup (struct misdn_bchannel *bc);
281 void manager_bchannel_cleanup (struct misdn_bchannel *bc);
283 void ec_chunk( struct misdn_bchannel *bc, unsigned char *rxchunk, unsigned char *txchunk, int chunk_size);
285 int bchdev_echocancel_activate(struct misdn_bchannel* dev);
286 void bchdev_echocancel_deactivate(struct misdn_bchannel* dev);
290 static char *bearer2str(int cap) {
291 static char *bearers[]={
300 case INFO_CAPABILITY_SPEECH:
303 case INFO_CAPABILITY_AUDIO_3_1K:
306 case INFO_CAPABILITY_DIGITAL_UNRESTRICTED:
309 case INFO_CAPABILITY_DIGITAL_RESTRICTED:
319 static char flip_table[256];
321 static void init_flip_bits(void)
325 for (i = 0 ; i < 256 ; i++) {
326 unsigned char sample = 0 ;
327 for (k = 0; k<8; k++) {
328 if ( i & 1 << k ) sample |= 0x80 >> k;
330 flip_table[i] = sample;
334 static char * flip_buf_bits ( char * buf , int len)
339 for (i = 0 ; i < len; i++) {
340 buf[i] = flip_table[(unsigned char)buf[i]];
349 static msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */
356 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
361 printf("cannot allocate memory, trying again...\n");
365 printf("cannot allocate memory, system overloaded.\n");
371 msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode)
379 size = sizeof(Q931_info_t)+2;
383 dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL);
388 dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM);
391 memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN);
392 frm = (iframe_t *)dmsg->data;
395 qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN);
401 if (!i) printf("cannot allocate memory, trying again...\n");
405 printf("cannot allocate memory, system overloaded.\n");
410 static int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
412 iframe_t *frm = (iframe_t *)dmsg->data;
413 struct misdn_stack *stack=get_stack_by_bc(bc);
416 cb_log(0,bc->port,"send_msg: IEK!! no stack\n ");
420 frm->addr = (stack->upper_id | FLG_MSG_DOWN);
421 frm->dinfo = bc->l3_id;
422 frm->len = (dmsg->len) - mISDN_HEADER_LEN;
424 cb_log(4,stack->port,"Sending msg, prim:%x addr:%x dinfo:%x\n",frm->prim,frm->addr,frm->dinfo);
426 mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC);
436 int misdn_cap_is_speech(int cap)
437 /** Poor mans version **/
439 if ( (cap != INFO_CAPABILITY_DIGITAL_UNRESTRICTED) &&
440 (cap != INFO_CAPABILITY_DIGITAL_RESTRICTED) ) return 1;
444 int misdn_inband_avail(struct misdn_bchannel *bc)
447 if (!bc->early_bconnect) {
448 /* We have opted to never receive any available inband recorded messages */
452 switch (bc->progress_indicator) {
453 case INFO_PI_INBAND_AVAILABLE:
454 case INFO_PI_CALL_NOT_E2E_ISDN:
455 case INFO_PI_CALLED_NOT_ISDN:
464 static void dump_chan_list(struct misdn_stack *stack)
468 for (i = 0; i <= stack->b_num; ++i) {
469 cb_log(6, stack->port, "Idx:%d stack->cchan:%d in_use:%d Chan:%d\n",
470 i, stack->channels[i], stack->bc[i].in_use, i + 1);
472 #if defined(AST_MISDN_ENHANCEMENTS)
473 for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
474 if (stack->bc[i].in_use) {
475 cb_log(6, stack->port, "Idx:%d stack->cchan:%d REGISTER Chan:%d in_use\n",
476 i, stack->channels[i], i + 1);
479 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
483 void misdn_dump_chanlist(void)
485 struct misdn_stack *stack=get_misdn_stack();
486 for ( ; stack; stack=stack->next) {
487 dump_chan_list(stack);
492 static int set_chan_in_stack(struct misdn_stack *stack, int channel)
494 cb_log(4,stack->port,"set_chan_in_stack: %d\n",channel);
495 dump_chan_list(stack);
496 if (1 <= channel && channel <= ARRAY_LEN(stack->channels)) {
497 if (!stack->channels[channel-1])
498 stack->channels[channel-1] = 1;
500 cb_log(4,stack->port,"channel already in use:%d\n", channel );
504 cb_log(0,stack->port,"couldn't set channel %d in\n", channel );
513 static int find_free_chan_in_stack(struct misdn_stack *stack, struct misdn_bchannel *bc, int channel, int dec)
519 if (bc->channel_found) {
523 bc->channel_found = 1;
525 #if defined(AST_MISDN_ENHANCEMENTS)
526 if (bc->is_register_pool) {
527 pthread_mutex_lock(&stack->st_lock);
528 for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->channels); ++i) {
529 if (!stack->channels[i]) {
531 cb_log(3, stack->port, " --> found REGISTER chan: %d\n", chan);
536 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
538 cb_log(5, stack->port, "find_free_chan: req_chan:%d\n", channel);
540 if (channel < 0 || channel > MAX_BCHANS) {
541 cb_log(0, stack->port, " !! out of bound call to find_free_chan_in_stack! (ch:%d)\n", channel);
547 pthread_mutex_lock(&stack->st_lock);
548 bnums = stack->pri ? stack->b_num : stack->b_num - 1;
550 for (i = bnums; i >= 0; --i) {
551 if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 D channel ;) and work with chan preselection */
552 if (!stack->channels[i]) {
554 cb_log(3, stack->port, " --> found chan%s: %d\n", channel >= 0 ? " (preselected)" : "", chan);
560 for (i = 0; i <= bnums; ++i) {
561 if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 D channel ;) and work with chan preselection */
562 if (!stack->channels[i]) {
564 cb_log(3, stack->port, " --> found chan%s: %d\n", channel >= 0 ? " (preselected)" : "", chan);
573 pthread_mutex_unlock(&stack->st_lock);
574 cb_log(1, stack->port, " !! NO FREE CHAN IN STACK\n");
575 dump_chan_list(stack);
576 bc->out_cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
580 if (set_chan_in_stack(stack, chan) < 0) {
581 pthread_mutex_unlock(&stack->st_lock);
582 cb_log(0, stack->port, "Channel Already in use:%d\n", chan);
583 bc->out_cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
586 pthread_mutex_unlock(&stack->st_lock);
594 * \brief Release a B channel to the allocation pool.
596 * \param stack Which port stack B channel belongs.
597 * \param channel B channel to release. (Range 1-MAX_BCHANS representing B1-Bn)
602 * Must be called after clean_up_bc() to make sure that the media stream is
603 * no longer connected.
605 static void empty_chan_in_stack(struct misdn_stack *stack, int channel)
607 if (channel < 1 || ARRAY_LEN(stack->channels) < channel) {
608 cb_log(0, stack->port, "empty_chan_in_stack: cannot empty channel %d\n", channel);
612 cb_log(4, stack->port, "empty_chan_in_stack: %d\n", channel);
613 stack->channels[channel - 1] = 0;
614 dump_chan_list(stack);
617 char *bc_state2str(enum bchannel_state state) {
620 struct bchan_state_s {
622 enum bchannel_state s;
624 {"BCHAN_CLEANED", BCHAN_CLEANED },
625 {"BCHAN_EMPTY", BCHAN_EMPTY},
626 {"BCHAN_ACTIVATED", BCHAN_ACTIVATED},
627 {"BCHAN_BRIDGED", BCHAN_BRIDGED},
628 {"BCHAN_RELEASE", BCHAN_RELEASE},
629 {"BCHAN_ERROR", BCHAN_ERROR}
632 for (i=0; i< sizeof(states)/sizeof(struct bchan_state_s); i++)
633 if ( states[i].s == state)
639 void bc_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
641 cb_log(5,bc->port,"BC_STATE_CHANGE: l3id:%x from:%s to:%s\n",
643 bc_state2str(bc->bc_state),
644 bc_state2str(state) );
647 case BCHAN_ACTIVATED:
648 if (bc->next_bc_state == BCHAN_BRIDGED) {
649 misdn_join_conf(bc, bc->conf_id);
650 bc->next_bc_state = BCHAN_EMPTY;
659 static void bc_next_state_change(struct misdn_bchannel *bc, enum bchannel_state state)
661 cb_log(5,bc->port,"BC_NEXT_STATE_CHANGE: from:%s to:%s\n",
662 bc_state2str(bc->next_bc_state),
663 bc_state2str(state) );
665 bc->next_bc_state=state;
670 * \brief Empty the B channel record of most call data.
672 * \param bc B channel record to empty of most call data.
677 * Sets the last_used time and must be called before clearing bc->in_use.
679 static void empty_bc(struct misdn_bchannel *bc)
681 bc->caller.presentation = 0; /* allowed */
682 bc->caller.number_plan = NUMPLAN_ISDN;
683 bc->caller.number_type = NUMTYPE_UNKNOWN;
684 bc->caller.name[0] = 0;
685 bc->caller.number[0] = 0;
686 bc->caller.subaddress[0] = 0;
688 bc->connected.presentation = 0; /* allowed */
689 bc->connected.number_plan = NUMPLAN_ISDN;
690 bc->connected.number_type = NUMTYPE_UNKNOWN;
691 bc->connected.name[0] = 0;
692 bc->connected.number[0] = 0;
693 bc->connected.subaddress[0] = 0;
695 bc->redirecting.from.presentation = 0; /* allowed */
696 bc->redirecting.from.number_plan = NUMPLAN_ISDN;
697 bc->redirecting.from.number_type = NUMTYPE_UNKNOWN;
698 bc->redirecting.from.name[0] = 0;
699 bc->redirecting.from.number[0] = 0;
700 bc->redirecting.from.subaddress[0] = 0;
702 bc->redirecting.to.presentation = 0; /* allowed */
703 bc->redirecting.to.number_plan = NUMPLAN_ISDN;
704 bc->redirecting.to.number_type = NUMTYPE_UNKNOWN;
705 bc->redirecting.to.name[0] = 0;
706 bc->redirecting.to.number[0] = 0;
707 bc->redirecting.to.subaddress[0] = 0;
709 bc->redirecting.reason = mISDN_REDIRECTING_REASON_UNKNOWN;
710 bc->redirecting.count = 0;
711 bc->redirecting.to_changed = 0;
722 bc->sending_complete = 0;
724 bc->restart_channel=0;
728 bc->need_more_infos = 0;
740 bc->curptx=0; bc->curprx=0;
742 bc->crypt_key[0] = 0;
749 bc->early_bconnect = 1;
755 bc->ec_deftaps = 128;
758 bc->AOCD_need_export = 0;
762 bc->cause = AST_CAUSE_NORMAL_CLEARING;
763 bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
765 bc->display_connected = 0; /* none */
766 bc->display_setup = 0; /* none */
768 bc->outgoing_colp = 0;/* pass */
770 bc->presentation = 0; /* allowed */
771 bc->set_presentation = 0;
773 bc->notify_description_code = mISDN_NOTIFY_CODE_INVALID;
775 bc->progress_coding=0;
776 bc->progress_location=0;
777 bc->progress_indicator=0;
779 #if defined(AST_MISDN_ENHANCEMENTS)
780 bc->div_leg_3_rx_wanted = 0;
781 bc->div_leg_3_tx_pending = 0;
782 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
784 /** Set Default Bearer Caps **/
785 bc->capability=INFO_CAPABILITY_SPEECH;
786 bc->law=INFO_CODEC_ALAW;
794 bc->dialed.number_plan = NUMPLAN_ISDN;
795 bc->dialed.number_type = NUMTYPE_UNKNOWN;
796 bc->dialed.number[0] = 0;
797 bc->dialed.subaddress[0] = 0;
801 bc->infos_pending[0] = 0;
805 bc->fac_in.Function = Fac_None;
806 bc->fac_out.Function = Fac_None;
808 bc->te_choose_channel = 0;
809 bc->channel_found= 0;
811 gettimeofday(&bc->last_used, NULL);
815 static int clean_up_bc(struct misdn_bchannel *bc)
818 unsigned char buff[32];
819 struct misdn_stack * stack;
821 cb_log(3, bc->port, "$$$ CLEANUP CALLED pid:%d\n", bc->pid);
823 stack=get_stack_by_bc(bc);
825 if (!stack) return -1;
827 switch (bc->bc_state ) {
829 cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
836 cb_log(2, stack->port, "$$$ Cleaning up bc with stid :%x pid:%d\n", bc->b_stid, bc->pid);
838 manager_ec_disable(bc);
840 manager_bchannel_deactivate(bc);
842 mISDN_write_frame(stack->midev, buff, bc->layer_id|FLG_MSG_TARGET|FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
845 bc_state_change(bc, BCHAN_CLEANED);
852 static void clear_l3(struct misdn_stack *stack)
856 if (global_state == MISDN_INITIALIZED) {
857 for (i = 0; i <= stack->b_num; ++i) {
858 cb_event(EVENT_CLEANUP, &stack->bc[i], NULL);
859 empty_bc(&stack->bc[i]);
860 clean_up_bc(&stack->bc[i]);
861 empty_chan_in_stack(stack, i + 1);
862 stack->bc[i].in_use = 0;
864 #if defined(AST_MISDN_ENHANCEMENTS)
865 for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
866 empty_bc(&stack->bc[i]);
867 empty_chan_in_stack(stack, i + 1);
868 stack->bc[i].in_use = 0;
870 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
874 static int new_te_id = 0;
876 static int misdn_lib_get_l1_down(struct misdn_stack *stack)
880 act.prim = PH_DEACTIVATE | REQUEST;
881 act.addr = stack->upper_id | FLG_MSG_DOWN;
885 cb_log(1, stack->port, "SENDING PH_DEACTIVATE | REQ\n");
886 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
890 static int misdn_lib_get_l2_down(struct misdn_stack *stack)
893 if (stack->ptp && stack->nt) {
896 dmsg = create_l2msg(DL_RELEASE| REQUEST, 0, 0);
898 pthread_mutex_lock(&stack->nstlock);
899 if (stack->nst.manager_l3(&stack->nst, dmsg))
901 pthread_mutex_unlock(&stack->nstlock);
902 } else if (!stack->nt) {
905 act.prim = DL_RELEASE| REQUEST;
906 act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
910 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
912 /* cannot deestablish L2 for NT PTMP to unknown TE TEIs */
918 static int misdn_lib_get_l1_up(struct misdn_stack *stack)
922 act.prim = PH_ACTIVATE | REQUEST;
923 act.addr = stack->upper_id | FLG_MSG_DOWN;
928 cb_log(1, stack->port, "SENDING PH_ACTIVATE | REQ\n");
929 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
932 int misdn_lib_get_l2_up(struct misdn_stack *stack)
935 if (stack->ptp && stack->nt) {
938 dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
940 pthread_mutex_lock(&stack->nstlock);
941 if (stack->nst.manager_l3(&stack->nst, dmsg))
943 pthread_mutex_unlock(&stack->nstlock);
944 } else if (!stack->nt) {
947 act.prim = DL_ESTABLISH | REQUEST;
948 act.addr = (stack->upper_id |FLG_MSG_DOWN) ;
952 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
954 /* cannot establish L2 for NT PTMP to unknown TE TEIs */
959 static int misdn_lib_get_short_status(struct misdn_stack *stack)
964 act.prim = MGR_SHORTSTATUS | REQUEST;
966 act.addr = (stack->upper_id | MSG_BROADCAST) ;
968 act.dinfo = SSTATUS_BROADCAST_BIT | SSTATUS_ALL;
971 return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
976 static int create_process(int midev, struct misdn_bchannel *bc)
981 struct misdn_stack *stack;
983 stack = get_stack_by_bc(bc);
985 if (find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0, 0) < 0) {
988 cb_log(4, stack->port, " --> found channel: %d\n", bc->channel);
990 for (proc_id = 0; proc_id < MAXPROCS; ++proc_id) {
991 if (stack->procids[proc_id] == 0) {
995 if (proc_id == MAXPROCS) {
996 cb_log(0, stack->port, "Couldn't Create New ProcId.\n");
1000 stack->procids[proc_id] = 1;
1002 l3_id = 0xff00 | proc_id;
1004 cb_log(3, stack->port, " --> new_l3id %x\n", l3_id);
1006 if ((stack->pri && stack->ptp) || bc->te_choose_channel) {
1007 /* we know exactly which channels are in use */
1008 if (find_free_chan_in_stack(stack, bc, bc->channel_preselected ? bc->channel : 0, bc->dec) < 0) {
1011 cb_log(2, stack->port, " --> found channel: %d\n", bc->channel);
1013 /* other phones could have made a call also on this port (ptmp) */
1017 /* if we are in te-mode, we need to create a process first */
1018 if (++new_te_id > 0xffff) {
1022 l3_id = (entity << 16) | new_te_id;
1024 cb_log(3, stack->port, "--> new_l3id %x\n", l3_id);
1027 ncr.prim = CC_NEW_CR | REQUEST;
1028 ncr.addr = (stack->upper_id | FLG_MSG_DOWN);
1031 mISDN_write(midev, &ncr, mISDN_HEADER_LEN + ncr.len, TIMEOUT_1SEC);
1038 static int setup_bc(struct misdn_bchannel *bc)
1040 unsigned char buff[1025];
1048 struct misdn_stack *stack=get_stack_by_bc(bc);
1051 cb_log(0, bc->port, "setup_bc: NO STACK FOUND!!\n");
1055 midev = stack->midev;
1056 channel = bc->channel - 1 - (bc->channel > 16);
1057 b_stid = stack->b_stids[channel >= 0 ? channel : 0];
1059 switch (bc->bc_state) {
1063 cb_log(4, stack->port, "$$$ bc already setup stid :%x (state:%s)\n", b_stid, bc_state2str(bc->bc_state) );
1067 cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
1069 /*check if the b_stid is already initialized*/
1070 for (i=0; i <= stack->b_num; i++) {
1071 if (stack->bc[i].b_stid == b_stid) {
1072 cb_log(0, bc->port, "setup_bc: b_stid:%x already in use !!!\n", b_stid);
1078 cb_log(0, stack->port," -- Stid <=0 at the moment in channel:%d\n",channel);
1080 bc_state_change(bc,BCHAN_ERROR);
1084 bc->b_stid = b_stid;
1088 memset(&li, 0, sizeof(li));
1093 li.st = bc->b_stid; /* given idx */
1100 if ( bc->hdlc || bc->nodsp) {
1101 cb_log(4, stack->port,"setup_bc: without dsp\n");
1103 int l = sizeof(li.name);
1104 strncpy(li.name, "B L3", l);
1107 li.pid.layermask = ISDN_LAYER((3));
1108 li.pid.protocol[3] = ISDN_PID_L3_B_USER;
1112 cb_log(4, stack->port,"setup_bc: with dsp\n");
1114 int l = sizeof(li.name);
1115 strncpy(li.name, "B L4", l);
1118 li.pid.layermask = ISDN_LAYER((4));
1119 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
1124 ret = mISDN_new_layer(midev, &li);
1126 cb_log(0, stack->port,"New Layer Err: %d %s\n",ret,strerror(errno));
1128 bc_state_change(bc,BCHAN_ERROR);
1132 bc->layer_id = li.id;
1135 memset(&pid, 0, sizeof(pid));
1139 cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
1141 if (bc->nodsp && !bc->hdlc) {
1142 cb_log(2, stack->port," --> TRANSPARENT Mode (no DSP, no HDLC)\n");
1143 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1144 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1145 pid.protocol[3] = ISDN_PID_L3_B_USER;
1146 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
1148 } else if ( bc->hdlc ) {
1149 cb_log(2, stack->port," --> HDLC Mode\n");
1150 pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
1151 pid.protocol[2] = ISDN_PID_L2_B_TRANS ;
1152 pid.protocol[3] = ISDN_PID_L3_B_USER;
1153 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ;
1155 cb_log(2, stack->port," --> TRANSPARENT Mode\n");
1156 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
1157 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
1158 pid.protocol[3] = ISDN_PID_L3_B_DSP;
1159 pid.protocol[4] = ISDN_PID_L4_B_USER;
1160 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
1164 ret = mISDN_set_stack(midev, bc->b_stid, &pid);
1167 cb_log(0, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
1169 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1171 bc_state_change(bc,BCHAN_ERROR);
1172 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1176 ret = mISDN_get_setstack_ind(midev, bc->layer_id);
1179 cb_log(0, stack->port,"$$$ Set StackIND Err: %d %s\n",ret,strerror(errno));
1180 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1182 bc_state_change(bc,BCHAN_ERROR);
1183 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1187 ret = mISDN_get_layerid(midev, bc->b_stid, bc->layer) ;
1189 bc->addr = ret>0? ret : 0;
1192 cb_log(0, stack->port,"$$$ Get Layerid Err: %d %s\n",ret,strerror(errno));
1193 mISDN_write_frame(midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1195 bc_state_change(bc,BCHAN_ERROR);
1196 cb_event(EVENT_BCHAN_ERROR, bc, glob_mgr->user_data);
1200 manager_bchannel_activate(bc);
1202 bc_state_change(bc,BCHAN_ACTIVATED);
1210 static int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, int port, int bidx)
1216 cb_log(8, port, "Init.BC %d.\n",bidx);
1218 bc->send_lock = malloc(sizeof(struct send_lock)); /* XXX BUG! memory leak never freed */
1219 if (!bc->send_lock) {
1222 pthread_mutex_init(&bc->send_lock->lock, NULL);
1226 bc->port=stack->port;
1227 bc_state_change(bc, BCHAN_CLEANED);
1228 bc->nt=stack->nt?1:0;
1232 ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
1234 if (!ibuf) return -1;
1236 clear_ibuffer( ibuf);
1238 ibuf->rsem=malloc(sizeof(sem_t));
1245 if (sem_init(ibuf->rsem,1,0)<0)
1246 sem_init(ibuf->rsem,0,0);
1250 #if 0 /* This code does not seem to do anything useful */
1251 if (bidx <= stack->b_num) {
1252 unsigned char buff[1025];
1253 iframe_t *frm = (iframe_t *) buff;
1254 stack_info_t *stinf;
1257 ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
1259 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1263 stinf = (stack_info_t *)&frm->data.p;
1265 cb_log(8, port, " --> Child %x\n",stinf->child[bidx]);
1274 static struct misdn_stack *stack_init(int midev, int port, int ptp)
1277 unsigned char buff[1025];
1278 iframe_t *frm = (iframe_t *)buff;
1279 stack_info_t *stinf;
1280 struct misdn_stack *stack;
1284 stack = calloc(1, sizeof(struct misdn_stack));
1289 cb_log(8, port, "Init. Stack.\n");
1295 stack->holding=NULL;
1298 msg_queue_init(&stack->downqueue);
1300 pthread_mutex_init(&stack->st_lock, NULL);
1302 /* query port's requirements */
1303 ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff));
1305 cb_log(0, port, "%s: Cannot get stack info for this port. (ret=%d)\n", __FUNCTION__, ret);
1310 stinf = (stack_info_t *)&frm->data.p;
1312 stack->d_stid = stinf->id;
1313 stack->b_num = stinf->childcnt;
1315 for (i=0; i<=stinf->childcnt; i++)
1316 stack->b_stids[i] = stinf->child[i];
1318 switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) {
1319 case ISDN_PID_L0_TE_S0:
1320 cb_log(8, port, "TE Stack\n");
1323 case ISDN_PID_L0_NT_S0:
1324 cb_log(8, port, "NT Stack\n");
1327 case ISDN_PID_L0_TE_E1:
1328 cb_log(8, port, "TE S2M Stack\n");
1332 case ISDN_PID_L0_NT_E1:
1333 cb_log(8, port, "NT S2M Stack\n");
1338 cb_log(0, port, "this is a unknown port type 0x%08x\n", stinf->pid.protocol[0]);
1343 if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) {
1354 memset(&li, 0, sizeof(li));
1356 int l = sizeof(li.name);
1357 strncpy(li.name,nt?"net l2":"user l4", l);
1362 li.pid.protocol[nt?2:4] = nt?ISDN_PID_L2_LAPD_NET:ISDN_PID_L4_CAPI20;
1363 li.pid.layermask = ISDN_LAYER((nt?2:4));
1364 li.st = stack->d_stid;
1367 ret = mISDN_new_layer(midev, &li);
1369 cb_log(0, port, "%s: Cannot add layer %d to this port.\n", __FUNCTION__, nt?2:4);
1375 ret = mISDN_register_layer(midev, stack->d_stid, li.id);
1378 cb_log(0,port,"Cannot register layer %d of this port.\n", nt?2:4);
1383 stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, nt?1:3);
1384 if (stack->lower_id < 0) {
1385 cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?1:3);
1390 stack->upper_id = mISDN_get_layerid(midev, stack->d_stid, nt?2:4);
1391 if (stack->upper_id < 0) {
1392 cb_log(0, port, "%s: Cannot get layer(%d) id of this port.\n", __FUNCTION__, nt?2:4);
1397 /* create nst (nt-mode only) */
1400 memset(&stack->nst, 0, sizeof(net_stack_t));
1401 memset(&stack->mgr, 0, sizeof(manager_t));
1403 stack->mgr.nst = &stack->nst;
1404 stack->nst.manager = &stack->mgr;
1406 stack->nst.l3_manager = handle_event_nt;
1407 stack->nst.device = midev;
1408 stack->nst.cardnr = port;
1409 stack->nst.d_stid = stack->d_stid;
1411 stack->nst.feature = FEATURE_NET_HOLD;
1413 stack->nst.feature |= FEATURE_NET_PTP;
1415 stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID;
1417 stack->nst.l1_id = stack->lower_id; /* never used */
1418 stack->nst.l2_id = stack->upper_id;
1420 msg_queue_init(&stack->nst.down_queue);
1421 pthread_mutex_init(&stack->nstlock, NULL);
1423 Isdnl2Init(&stack->nst);
1424 Isdnl3Init(&stack->nst);
1432 misdn_lib_get_short_status(stack);
1434 misdn_lib_get_l1_up(stack);
1435 if (!stack->ptp) misdn_lib_get_l1_up(stack);
1436 misdn_lib_get_l2_up(stack);
1440 misdn_lib_get_short_status(stack);
1441 misdn_lib_get_l1_up(stack);
1442 /* handle_l1 will start L2 for NT. */
1444 misdn_lib_get_l2_up(stack);
1448 cb_log(8, port, "stack_init: lowerId:%x upperId:%x\n", stack->lower_id, stack->upper_id);
1454 static void stack_destroy(struct misdn_stack *stack)
1460 pthread_mutex_destroy(&stack->nstlock);
1461 cleanup_Isdnl2(&stack->nst);
1462 cleanup_Isdnl3(&stack->nst);
1465 if (stack->upper_id)
1466 mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
1468 pthread_mutex_destroy(&stack->st_lock);
1472 static struct misdn_stack * find_stack_by_addr(int addr)
1474 struct misdn_stack *stack;
1476 for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
1477 if ((stack->upper_id & STACK_ID_MASK) == (addr & STACK_ID_MASK)) {
1478 /* Found the stack */
1487 static struct misdn_stack *find_stack_by_port(int port)
1489 struct misdn_stack *stack;
1491 for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
1492 if (stack->port == port) {
1493 /* Found the stack */
1501 static struct misdn_stack *find_stack_by_mgr(manager_t *mgr_nt)
1503 struct misdn_stack *stack;
1505 for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
1506 if (&stack->mgr == mgr_nt) {
1507 /* Found the stack */
1515 static struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask)
1519 for (i = 0; i <= stack->b_num; ++i) {
1520 if (stack->bc[i].in_use && (stack->bc[i].l3_id & mask) == (l3id & mask)) {
1521 return &stack->bc[i];
1524 #if defined(AST_MISDN_ENHANCEMENTS)
1525 /* Search the B channel records for a REGISTER signaling link. */
1526 for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
1527 if (stack->bc[i].in_use && (stack->bc[i].l3_id & mask) == (l3id & mask)) {
1528 return &stack->bc[i];
1531 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
1532 return stack_holder_find(stack, l3id);
1536 struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id)
1540 for (i = 0; i <= stack->b_num; ++i) {
1541 if (stack->bc[i].in_use && stack->bc[i].l3_id == l3id) {
1542 return &stack->bc[i];
1545 #if defined(AST_MISDN_ENHANCEMENTS)
1546 /* Search the B channel records for a REGISTER signaling link. */
1547 for (i = MAX_BCHANS + 1; i < ARRAY_LEN(stack->bc); ++i) {
1548 if (stack->bc[i].in_use && stack->bc[i].l3_id == l3id) {
1549 return &stack->bc[i];
1552 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
1553 return stack_holder_find(stack, l3id);
1556 static struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
1558 struct misdn_stack *stack;
1561 for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
1562 for (i = 0; i <= stack->b_num; i++) {
1563 if (stack->bc[i].in_use
1564 && ((stack->bc[i].addr & STACK_ID_MASK) == (addr & STACK_ID_MASK)
1565 || stack->bc[i].layer_id == addr)) {
1566 return &stack->bc[i];
1574 static struct misdn_bchannel *find_bc_by_confid(unsigned long confid)
1576 struct misdn_stack *stack;
1579 for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
1580 for (i = 0; i <= stack->b_num; i++) {
1581 if (stack->bc[i].in_use && stack->bc[i].conf_id == confid) {
1582 return &stack->bc[i];
1590 static int handle_event_te(struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
1592 struct misdn_stack *stack=get_stack_by_bc(bc);
1595 case EVENT_CONNECT_ACKNOWLEDGE:
1598 if ( *bc->crypt_key ) {
1599 cb_log(4, stack->port,
1600 "ENABLING BLOWFISH channel:%d caller%d:\"%s\" <%s> dialed%d:%s\n",
1602 bc->caller.number_type,
1605 bc->dialed.number_type,
1607 manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
1610 if (misdn_cap_is_speech(bc->capability)) {
1611 if ( !bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0);
1612 manager_ec_enable(bc);
1614 if ( bc->txgain != 0 ) {
1615 cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
1616 manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
1618 if ( bc->rxgain != 0 ) {
1619 cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
1620 manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
1627 if ( *bc->crypt_key ) {
1628 cb_log(4, stack->port,
1629 "ENABLING BLOWFISH channel:%d caller%d:\"%s\" <%s> dialed%d:%s\n",
1631 bc->caller.number_type,
1634 bc->dialed.number_type,
1636 manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
1638 case EVENT_ALERTING:
1639 case EVENT_PROGRESS:
1640 case EVENT_PROCEEDING:
1641 case EVENT_SETUP_ACKNOWLEDGE:
1644 if (bc->channel == 0xff || bc->channel<=0)
1647 if (find_free_chan_in_stack(stack, bc, bc->channel, 0)<0){
1648 if (!stack->pri && !stack->ptp) {
1654 cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n");
1656 cb_log(0, stack->port,
1657 "Requested Channel Already in Use releasing this call with cause %d!!!!\n",
1661 /* when the channel is already in use, we can't
1662 * simply clear it, we need to make sure that
1663 * it will still be marked as in_use in the
1664 * available channels list.*/
1667 misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
1671 if (event != EVENT_SETUP) {
1677 case EVENT_RELEASE_COMPLETE:
1686 static int handle_cr ( struct misdn_stack *stack, iframe_t *frm)
1688 struct misdn_bchannel dummybc;
1689 struct misdn_bchannel *bc;
1692 if (!stack) return -1;
1694 switch (frm->prim) {
1695 case CC_NEW_CR|INDICATION:
1696 cb_log(7, stack->port, " --> lib: NEW_CR Ind with l3id:%x on this port.\n",frm->dinfo);
1698 bc = misdn_lib_get_free_bc(stack->port, 0, 1, 0);
1700 cb_log(0, stack->port, " --> !! lib: No free channel!\n");
1704 cb_log(7, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
1705 bc->l3_id=frm->dinfo;
1707 case CC_NEW_CR|CONFIRM:
1709 case CC_NEW_CR|REQUEST:
1711 case CC_RELEASE_CR|REQUEST:
1713 case CC_RELEASE_CR|CONFIRM:
1715 case CC_RELEASE_CR|INDICATION:
1716 cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n", frm->dinfo);
1717 bc = find_bc_by_l3id(stack, frm->dinfo);
1719 cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
1720 misdn_make_dummy(&dummybc, stack->port, frm->dinfo, stack->nt, 0);
1724 channel = bc->channel;
1725 cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n", frm->dinfo);
1728 bc->need_disconnect = 0;
1729 bc->need_release = 0;
1730 bc->need_release_complete = 0;
1732 cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
1738 empty_chan_in_stack(stack, channel);
1741 dump_chan_list(stack);
1743 if (bc->stack_holder) {
1744 cb_log(4, stack->port, "REMOVING Holder\n");
1745 stack_holder_remove(stack, bc);
1758 /* Empties bc if it's reserved (no SETUP out yet) */
1759 void misdn_lib_release(struct misdn_bchannel *bc)
1762 struct misdn_stack *stack=get_stack_by_bc(bc);
1765 cb_log(1,0,"misdn_release: No Stack found\n");
1769 channel = bc->channel;
1773 empty_chan_in_stack(stack, channel);
1781 int misdn_lib_get_port_up (int port)
1783 struct misdn_stack *stack;
1785 for (stack=glob_mgr->stack_list;
1787 stack=stack->next) {
1789 if (stack->port == port) {
1792 misdn_lib_get_l1_up(stack);
1793 /* handle_l1 will start L2 for NT. */
1794 if (!stack->l2link && !stack->nt) {
1795 misdn_lib_get_l2_up(stack);
1805 int misdn_lib_get_port_down (int port)
1806 { /* Pull Down L1 */
1807 struct misdn_stack *stack;
1808 for (stack=glob_mgr->stack_list;
1810 stack=stack->next) {
1811 if (stack->port == port) {
1813 misdn_lib_get_l2_down(stack);
1814 misdn_lib_get_l1_down(stack);
1821 int misdn_lib_port_up(int port, int check)
1823 struct misdn_stack *stack;
1826 for (stack=glob_mgr->stack_list;
1828 stack=stack->next) {
1830 if (stack->port == port) {
1832 if (stack->blocked) {
1833 cb_log(0,port, "Port Blocked:%d L2:%d L1:%d\n", stack->blocked, stack->l2link, stack->l1link);
1839 if (stack->l1link && stack->l2link) {
1842 cb_log(1,port, "Port Down L2:%d L1:%d\n",
1843 stack->l2link, stack->l1link);
1847 if ( !check || stack->l1link )
1850 cb_log(1,port, "Port down PMP\n");
1861 static int release_cr(struct misdn_stack *stack, mISDNuser_head_t *hh)
1863 struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
1864 struct misdn_bchannel dummybc;
1865 iframe_t frm; /* fake te frm to remove callref from global callreflist */
1867 frm.dinfo = hh->dinfo;
1868 frm.addr=stack->upper_id | FLG_MSG_DOWN;
1869 frm.prim = CC_RELEASE_CR|INDICATION;
1870 cb_log(4, stack->port, " --> CC_RELEASE_CR: Faking Release_cr for %x l3id:%x\n",frm.addr, frm.dinfo);
1872 /** removing procid **/
1874 cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", hh->dinfo);
1875 misdn_make_dummy(&dummybc, stack->port, hh->dinfo, stack->nt, 0);
1879 if ((bc->l3_id & 0xff00) == 0xff00) {
1880 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", bc->l3_id & 0xff);
1881 stack->procids[bc->l3_id & 0xff] = 0;
1884 if (handle_cr(stack, &frm)<0) {
1890 static int handle_event_nt(void *dat, void *arg)
1892 struct misdn_bchannel dummybc;
1893 struct misdn_bchannel *bc;
1894 manager_t *mgr = (manager_t *)dat;
1895 msg_t *msg = (msg_t *)arg;
1897 mISDNuser_head_t *hh;
1898 struct misdn_stack *stack;
1908 stack = find_stack_by_mgr(mgr);
1909 hh=(mISDNuser_head_t*)msg->data;
1912 * When we are called from the mISDNuser lib, the nstlock is held and it
1913 * must be held when we return. We unlock here because the lib may be
1914 * entered again recursively.
1916 pthread_mutex_unlock(&stack->nstlock);
1918 cb_log(5, stack->port, " --> lib: prim %x dinfo %x\n",hh->prim, hh->dinfo);
1920 case CC_RETRIEVE|INDICATION:
1922 struct misdn_bchannel *hold_bc;
1923 iframe_t frm; /* fake te frm to add callref to global callreflist */
1925 frm.dinfo = hh->dinfo;
1926 frm.addr=stack->upper_id | FLG_MSG_DOWN;
1927 frm.prim = CC_NEW_CR|INDICATION;
1928 if (handle_cr( stack, &frm)< 0) {
1929 goto ERR_NO_CHANNEL;
1932 bc = find_bc_by_l3id(stack, hh->dinfo);
1933 hold_bc = stack_holder_find(stack, bc->l3_id);
1934 cb_log(4, stack->port, "bc_l3id:%x holded_bc_l3id:%x\n",bc->l3_id, hold_bc->l3_id);
1937 cb_log(4, stack->port, "REMOVING Holder\n");
1939 /* swap the backup to our new channel back */
1940 stack_holder_remove(stack, hold_bc);
1941 memcpy(bc, hold_bc, sizeof(*bc));
1950 case CC_SETUP | CONFIRM:
1951 l3id = *((int *) (msg->data + mISDNUSER_HEAD_SIZE));
1953 cb_log(4, stack->port, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID is %x\n", l3id);
1955 bc = find_bc_by_l3id(stack, hh->dinfo);
1957 cb_log (2, bc->port, "I IND :CC_SETUP|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
1959 cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
1961 cb_log(4, stack->port, "Bc Not found (after SETUP CONFIRM)\n");
1964 pthread_mutex_lock(&stack->nstlock);
1967 case CC_SETUP | INDICATION:
1968 bc = misdn_lib_get_free_bc(stack->port, 0, 1, 0);
1970 goto ERR_NO_CHANNEL;
1973 cb_log(4, stack->port, " --> new_process: New L3Id: %x\n",hh->dinfo);
1974 bc->l3_id=hh->dinfo;
1977 #if defined(AST_MISDN_ENHANCEMENTS)
1978 case CC_REGISTER | CONFIRM:
1979 l3id = *((int *) (msg->data + mISDNUSER_HEAD_SIZE));
1981 cb_log(4, stack->port, " --> lib: Event_ind:REGISTER CONFIRM [NT] : new L3ID is %x\n", l3id);
1983 bc = find_bc_by_l3id(stack, hh->dinfo);
1985 cb_log (2, bc->port, "I IND :CC_REGISTER|CONFIRM: old l3id:%x new l3id:%x\n", bc->l3_id, l3id);
1988 cb_log(4, stack->port, "Bc Not found (after REGISTER CONFIRM)\n");
1991 pthread_mutex_lock(&stack->nstlock);
1993 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
1995 #if defined(AST_MISDN_ENHANCEMENTS)
1996 case CC_REGISTER | INDICATION:
1997 bc = misdn_lib_get_register_bc(stack->port);
1999 goto ERR_NO_CHANNEL;
2002 cb_log(4, stack->port, " --> new_process: New L3Id: %x\n",hh->dinfo);
2003 bc->l3_id=hh->dinfo;
2005 #endif /* defined(AST_MISDN_ENHANCEMENTS) */
2007 case CC_CONNECT_ACKNOWLEDGE|INDICATION:
2010 case CC_ALERTING|INDICATION:
2011 case CC_PROCEEDING|INDICATION:
2012 case CC_SETUP_ACKNOWLEDGE|INDICATION:
2013 case CC_CONNECT|INDICATION:
2015 case CC_DISCONNECT|INDICATION:
2016 bc = find_bc_by_l3id(stack, hh->dinfo);
2018 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
2020 int myprocid=bc->l3_id&0x0000ffff;
2022 hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
2023 cb_log(3,stack->port,"Reject dinfo: %x cause:%d\n",hh->dinfo,bc->cause);
2029 case CC_FACILITY|INDICATION:
2030 bc = find_bc_by_l3id(stack, hh->dinfo);
2032 bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000);
2034 int myprocid=bc->l3_id&0x0000ffff;
2036 hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
2037 cb_log(4,bc->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
2042 case CC_RELEASE_COMPLETE|INDICATION:
2045 case CC_SUSPEND|INDICATION:
2046 cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n");
2047 dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
2048 pthread_mutex_lock(&stack->nstlock);
2049 stack->nst.manager_l3(&stack->nst, dmsg);
2053 case CC_RESUME|INDICATION:
2056 case CC_RELEASE|CONFIRM:
2057 bc = find_bc_by_l3id(stack, hh->dinfo);
2059 cb_log(1, stack->port, "CC_RELEASE|CONFIRM (l3id:%x), sending RELEASE_COMPLETE\n", hh->dinfo);
2060 misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
2064 case CC_RELEASE|INDICATION:
2067 case CC_RELEASE_CR|INDICATION:
2068 release_cr(stack, hh);
2070 pthread_mutex_lock(&stack->nstlock);
2073 case CC_NEW_CR|INDICATION:
2074 /* Got New CR for bchan, for now I handle this one in */
2075 /* connect_ack, Need to be changed */
2076 l3id = *((int *) (msg->data + mISDNUSER_HEAD_SIZE));
2078 bc = find_bc_by_l3id(stack, hh->dinfo);
2080 cb_log(0, stack->port, " --> In NEW_CR: didn't found bc ??\n");
2081 pthread_mutex_lock(&stack->nstlock);
2084 if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) {
2085 cb_log(4, stack->port, " --> Removing Process Id:%x on this port.\n", 0xff&bc->l3_id);
2086 stack->procids[bc->l3_id&0xff] = 0 ;
2088 cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID is %x\n",l3id );
2091 if (!bc->is_register_pool) {
2092 cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data);
2096 pthread_mutex_lock(&stack->nstlock);
2099 case DL_ESTABLISH | INDICATION:
2100 case DL_ESTABLISH | CONFIRM:
2101 cb_log(3, stack->port, "%% GOT L2 Activate Info.\n");
2103 if (stack->ptp && stack->l2link) {
2104 cb_log(0, stack->port, "%% GOT L2 Activate Info. but we're activated already.. this l2 is faulty, blocking port\n");
2105 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
2108 if (stack->ptp && !stack->restart_sent) {
2109 /* make sure we restart the interface of the
2111 stack->restart_sent=1;
2112 misdn_lib_send_restart(stack->port, -1);
2116 /* when we get the L2 UP, the L1 is UP definitely too*/
2121 pthread_mutex_lock(&stack->nstlock);
2124 case DL_RELEASE | INDICATION:
2125 case DL_RELEASE | CONFIRM:
2126 cb_log(3, stack->port, "%% GOT L2 DeActivate Info.\n");
2129 if (stack->l2upcnt>3) {
2130 cb_log(0 , stack->port, "!!! Could not Get the L2 up after 3 Attempts!!!\n");
2132 if (stack->l1link) {
2133 misdn_lib_get_l2_up(stack);
2141 pthread_mutex_lock(&stack->nstlock);
2148 /* Parse Events and fire_up to App. */
2149 event = isdn_msg_get_event(msgs_g, msg, 1);
2151 bc = find_bc_by_l3id(stack, hh->dinfo);
2153 cb_log(4, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x).\n", hh->dinfo);
2154 misdn_make_dummy(&dummybc, stack->port, hh->dinfo, stack->nt, 0);
2158 isdn_msg_parse_event(msgs_g, msg, bc, 1);
2162 if (bc->channel <= 0 || bc->channel == 0xff) {
2166 if (find_free_chan_in_stack(stack, bc, bc->channel, 0) < 0) {
2167 goto ERR_NO_CHANNEL;
2171 case EVENT_RELEASE_COMPLETE:
2172 channel = bc->channel;
2173 tmpcause = bc->cause;
2176 bc->cause = tmpcause;
2180 empty_chan_in_stack(stack, channel);
2187 if(!isdn_get_info(msgs_g, event, 1)) {
2188 cb_log(4, stack->port, "Unknown Event Ind: prim %x dinfo %x\n", hh->prim, hh->dinfo);
2192 case AST_CAUSE_USER_BUSY:
2193 cb_log(1, stack->port, "Siemens Busy reject..\n");
2199 cb_event(event, bc, glob_mgr->user_data);
2203 pthread_mutex_lock(&stack->nstlock);
2207 cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo);
2208 dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, hh->dinfo, sizeof(RELEASE_COMPLETE_t), 1);
2209 pthread_mutex_lock(&stack->nstlock);
2210 stack->nst.manager_l3(&stack->nst, dmsg);
2216 static int handle_timers(msg_t* msg)
2218 iframe_t *frm= (iframe_t*)msg->data;
2219 struct misdn_stack *stack;
2222 switch (frm->prim) {
2223 case MGR_INITTIMER | CONFIRM:
2224 case MGR_ADDTIMER | CONFIRM:
2225 case MGR_DELTIMER | CONFIRM:
2226 case MGR_REMOVETIMER | CONFIRM:
2233 if (frm->prim==(MGR_TIMER | INDICATION) ) {
2234 for (stack = glob_mgr->stack_list;
2236 stack = stack->next) {
2239 if (!stack->nt) continue;
2241 it = stack->nst.tlist;
2243 for(it=stack->nst.tlist;
2246 if (it->id == (int)frm->addr)
2250 mISDN_write_frame(stack->midev, msg->data, frm->addr,
2251 MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC);
2252 test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags);
2253 pthread_mutex_lock(&stack->nstlock);
2254 it->function(it->data);
2255 pthread_mutex_unlock(&stack->nstlock);
2261 cb_log(0, 0, "Timer Msg without Timer ??\n");
2271 void misdn_lib_tone_generator_start(struct misdn_bchannel *bc)
2273 bc->generate_tone=1;
2276 void misdn_lib_tone_generator_stop(struct misdn_bchannel *bc)
2278 bc->generate_tone=0;
2282 static int do_tone(struct misdn_bchannel *bc, int len)
2286 if (bc->generate_tone) {
2287 cb_event(EVENT_TONE_GENERATE, bc, glob_mgr->user_data);
2289 if ( !bc->nojitter ) {
2290 misdn_tx_jitter(bc,len);
2300 #ifdef MISDN_SAVE_DATA
2301 static void misdn_save_data(int id, char *p1, int l1, char *p2, int l2)
2306 sprintf(n1,"/tmp/misdn-rx-%d.raw",id);
2307 sprintf(n2,"/tmp/misdn-tx-%d.raw",id);
2309 rx = fopen(n1,"a+");
2310 tx = fopen(n2,"a+");
2313 cb_log(0,0,"Couldn't open files: %s\n",strerror(errno));
2330 void misdn_tx_jitter(struct misdn_bchannel *bc, int len)
2332 char buf[4096 + mISDN_HEADER_LEN];
2333 char *data=&buf[mISDN_HEADER_LEN];
2334 iframe_t *txfrm= (iframe_t*)buf;
2337 jlen=cb_jb_empty(bc,data,len);
2340 #ifdef MISDN_SAVE_DATA
2341 misdn_save_data((bc->port*100+bc->channel), data, jlen, bc->bframe, bc->bframe_len);
2343 flip_buf_bits( data, jlen);
2346 cb_log(1, bc->port, "Jitterbuffer Underrun. Got %d of expected %d\n", jlen, len);
2349 txfrm->prim = DL_DATA|REQUEST;
2353 txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */
2356 cb_log(9, bc->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
2358 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
2360 #define MISDN_GEN_SILENCE
2361 #ifdef MISDN_GEN_SILENCE
2362 int cnt=len/TONE_SILENCE_SIZE;
2363 int rest=len%TONE_SILENCE_SIZE;
2366 for (i=0; i<cnt; i++) {
2367 memcpy(data, tone_silence_flip, TONE_SILENCE_SIZE );
2368 data +=TONE_SILENCE_SIZE;
2372 memcpy(data, tone_silence_flip, rest);
2375 txfrm->prim = DL_DATA|REQUEST;
2379 txfrm->addr = bc->addr|FLG_MSG_DOWN; /* | IF_DOWN; */
2382 cb_log(5, bc->port, "Transmitting %d samples of silence to misdn\n", len);
2384 r=mISDN_write( glob_mgr->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
2391 cb_log(1, bc->port, "Error in mISDN_write (%s)\n", strerror(errno));
2395 static int handle_bchan(msg_t *msg)
2397 iframe_t *frm= (iframe_t*)msg->data;
2398 struct misdn_bchannel *bc=find_bc_by_addr(frm->addr);
2399 struct misdn_stack *stack;
2402 cb_log(1,0,"handle_bchan: BC not found for prim:%x with addr:%x dinfo:%x\n", frm->prim, frm->addr, frm->dinfo);
2406 stack = get_stack_by_bc(bc);
2409 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);
2413 switch (frm->prim) {
2415 case MGR_SETSTACK| CONFIRM:
2416 cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|CONFIRM pid:%d\n",bc->pid);
2419 case MGR_SETSTACK| INDICATION:
2420 cb_log(3, stack->port, "BCHAN: MGR_SETSTACK|IND pid:%d\n",bc->pid);
2423 case MGR_DELLAYER| INDICATION:
2424 cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|IND pid:%d\n",bc->pid);
2427 case MGR_DELLAYER| CONFIRM:
2428 cb_log(3, stack->port, "BCHAN: MGR_DELLAYER|CNF pid:%d\n",bc->pid);
2436 case PH_ACTIVATE | INDICATION:
2437 case DL_ESTABLISH | INDICATION:
2438 cb_log(3, stack->port, "BCHAN: ACT Ind pid:%d\n", bc->pid);
2443 case PH_ACTIVATE | CONFIRM:
2444 case DL_ESTABLISH | CONFIRM:
2446 cb_log(3, stack->port, "BCHAN: bchan ACT Confirm pid:%d\n",bc->pid);
2451 case DL_ESTABLISH | REQUEST:
2454 mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_ESTABLISH | CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
2459 case DL_RELEASE|REQUEST:
2462 mISDN_write_frame(stack->midev, buf, bc->addr | FLG_MSG_TARGET | FLG_MSG_DOWN, DL_RELEASE| CONFIRM, 0,0, NULL, TIMEOUT_1SEC);
2467 case PH_DEACTIVATE | INDICATION:
2468 case DL_RELEASE | INDICATION:
2469 cb_log (3, stack->port, "BCHAN: DeACT Ind pid:%d\n",bc->pid);
2474 case PH_DEACTIVATE | CONFIRM:
2475 case DL_RELEASE | CONFIRM:
2476 cb_log(3, stack->port, "BCHAN: DeACT Conf pid:%d\n",bc->pid);
2481 case PH_CONTROL|INDICATION:
2483 unsigned int *cont = (unsigned int *) &frm->data.p;
2485 cb_log(4, stack->port,
2486 "PH_CONTROL: channel:%d caller%d:\"%s\" <%s> dialed%d:%s \n",
2488 bc->caller.number_type,
2491 bc->dialed.number_type,
2494 if ((*cont & ~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
2495 int dtmf = *cont & DTMF_TONE_MASK;
2496 cb_log(4, stack->port, " --> DTMF TONE: %c\n",dtmf);
2498 cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data);
2503 if (*cont == BF_REJECT) {
2504 cb_log(4, stack->port, " --> BF REJECT\n");
2508 if (*cont == BF_ACCEPT) {
2509 cb_log(4, stack->port, " --> BF ACCEPT\n");
2516 case PH_DATA|REQUEST:
2517 case DL_DATA|REQUEST:
2518 cb_log(0, stack->port, "DL_DATA REQUEST \n");
2525 case PH_DATA|INDICATION:
2526 case DL_DATA|INDICATION:
2528 bc->bframe = (void*)&frm->data.i;
2529 bc->bframe_len = frm->len;
2531 /** Anyway flip the bufbits **/
2532 if ( misdn_cap_is_speech(bc->capability) )
2533 flip_buf_bits(bc->bframe, bc->bframe_len);
2536 if (!bc->bframe_len) {
2537 cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
2542 if ( (bc->addr&STACK_ID_MASK) != (frm->addr&STACK_ID_MASK) ) {
2543 cb_log(2, stack->port, "DL_DATA INDICATION bc->addr:%x frm->addr:%x\n", bc->addr, frm->addr);
2549 cb_log(0, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
2553 if ( (bc->bc_state == BCHAN_ACTIVATED) && frm->len > 0) {
2556 #ifdef MISDN_B_DEBUG
2557 cb_log(0,bc->port,"do_tone START\n");
2559 t=do_tone(bc,frm->len);
2561 #ifdef MISDN_B_DEBUG
2562 cb_log(0,bc->port,"do_tone STOP (%d)\n",t);
2567 if ( misdn_cap_is_speech(bc->capability)) {
2568 if ( !bc->nojitter ) {
2569 #ifdef MISDN_B_DEBUG
2570 cb_log(0,bc->port,"tx_jitter START\n");
2572 misdn_tx_jitter(bc,frm->len);
2573 #ifdef MISDN_B_DEBUG
2574 cb_log(0,bc->port,"tx_jitter STOP\n");
2579 #ifdef MISDN_B_DEBUG
2580 cb_log(0,bc->port,"EVENT_B_DATA START\n");
2583 i = cb_event(EVENT_BCHAN_DATA, bc, glob_mgr->user_data);
2584 #ifdef MISDN_B_DEBUG
2585 cb_log(0,bc->port,"EVENT_B_DATA STOP\n");
2589 cb_log(10,stack->port,"cb_event returned <0\n");
2590 /*clean_up_bc(bc);*/
2599 case PH_CONTROL | CONFIRM:
2600 cb_log(4, stack->port, "PH_CONTROL|CNF bc->addr:%x\n", frm->addr);
2604 case PH_DATA | CONFIRM:
2605 case DL_DATA|CONFIRM:
2608 cb_log(0, stack->port, "Data confirmed\n");
2613 case DL_DATA|RESPONSE:
2615 cb_log(0, stack->port, "Data response\n");
2626 static int handle_frm_nt(msg_t *msg)
2628 iframe_t *frm= (iframe_t*)msg->data;
2629 struct misdn_stack *stack;
2632 stack=find_stack_by_addr( frm->addr );
2636 if (!stack || !stack->nt) {
2641 pthread_mutex_lock(&stack->nstlock);
2642 if ((err=stack->nst.l1_l2(&stack->nst,msg))) {
2643 pthread_mutex_unlock(&stack->nstlock);
2644 if (nt_err_cnt > 0 ) {
2645 if (nt_err_cnt < 100) {
2647 cb_log(0, stack->port, "NT Stack sends us error: %d \n", err);
2648 } else if (nt_err_cnt < 105){
2649 cb_log(0, stack->port, "NT Stack sends us error: %d over 100 times, so I'll stop this message\n", err);
2656 pthread_mutex_unlock(&stack->nstlock);
2661 static int handle_frm_te(msg_t *msg)
2663 struct misdn_bchannel dummybc;
2664 struct misdn_bchannel *bc;
2666 struct misdn_stack *stack;
2668 enum event_response_e response;
2674 frm = (iframe_t*) msg->data;
2675 stack = find_stack_by_addr(frm->addr);
2676 if (!stack || stack->nt) {
2680 cb_log(4, stack->port, "handle_frm_te: frm->addr:%x frm->prim:%x\n", frm->addr, frm->prim);
2682 ret = handle_cr(stack, frm);
2684 cb_log(3, stack->port, "handle_frm_te: handle_cr <0 prim:%x addr:%x\n", frm->prim, frm->addr);
2691 bc = find_bc_by_l3id(stack, frm->dinfo);
2693 misdn_make_dummy(&dummybc, stack->port, 0, stack->nt, 0);
2694 switch (frm->prim) {
2695 case CC_RESTART | CONFIRM:
2696 dummybc.l3_id = MISDN_ID_GLOBAL;
2699 case CC_SETUP | INDICATION:
2700 dummybc.l3_id = frm->dinfo;
2703 /* set a reasonable cause */
2704 bc->out_cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
2705 /* if we want to send something the flag must be set! */
2706 bc->need_release_complete = 1;
2707 misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
2712 if (frm->prim == (CC_FACILITY | INDICATION)) {
2713 cb_log(5, stack->port, " --> Using Dummy BC for FACILITY\n");
2715 cb_log(0, stack->port, " --> Didn't find BC so temporarily creating dummy BC (l3id:%x) on this port.\n", frm->dinfo);
2716 dummybc.l3_id = frm->dinfo;
2723 event = isdn_msg_get_event(msgs_g, msg, 0);
2724 isdn_msg_parse_event(msgs_g, msg, bc, 0);
2726 /* Preprocess some Events */
2727 ret = handle_event_te(bc, event, frm);
2729 cb_log(0, stack->port, "couldn't handle event\n");
2734 /* shoot up event to App: */
2735 cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n", frm->addr, frm->prim, frm->dinfo);
2737 if (!isdn_get_info(msgs_g, event, 0)) {
2738 cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n", frm->addr, frm->prim, frm->dinfo);
2739 response = RESPONSE_OK;
2741 response = cb_event(event, bc, glob_mgr->user_data);
2747 case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE:
2748 cb_log(0, stack->port, "TOTALLY IGNORING SETUP\n");
2750 case RESPONSE_IGNORE_SETUP:
2751 cb_log(0, stack->port, "GOT IGNORE SETUP\n");
2752 /* I think we should send CC_RELEASE_CR, but am not sure*/
2753 bc->out_cause = AST_CAUSE_NORMAL_CLEARING;
2755 case RESPONSE_RELEASE_SETUP:
2756 if (response == RESPONSE_RELEASE_SETUP) {
2757 cb_log(0, stack->port, "GOT RELEASE SETUP\n");
2759 misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE);
2762 cb_log(4, stack->port, "GOT SETUP OK\n");
2765 cb_log(0, stack->port, "GOT UNKNOWN SETUP RESPONSE\n");
2769 case EVENT_RELEASE_COMPLETE:
2770 /* release bchannel only after we've announced the RELEASE_COMPLETE */
2771 channel = bc->channel;
2772 tmpcause = bc->cause;
2773 tmp_out_cause = bc->out_cause;
2776 bc->cause = tmpcause;
2777 bc->out_cause = tmp_out_cause;
2781 if (tmpcause == AST_CAUSE_REQUESTED_CHAN_UNAVAIL) {
2782 cb_log(0, stack->port, "**** Received CAUSE:%d, restarting channel %d\n", AST_CAUSE_REQUESTED_CHAN_UNAVAIL, channel);
2783 misdn_lib_send_restart(stack->port, channel);
2786 empty_chan_in_stack(stack, channel);
2790 cb_log(0, stack->port, "**** Received RESTART channel:%d\n", bc->restart_channel);
2791 empty_chan_in_stack(stack, bc->restart_channel);
2797 cb_log(5, stack->port, "Freeing Msg on prim:%x \n", frm->prim);
2803 static int handle_l1(msg_t *msg)
2805 iframe_t *frm = (iframe_t*) msg->data;
2806 struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2808 if (!stack) return 0 ;
2810 switch (frm->prim) {
2811 case PH_ACTIVATE | CONFIRM:
2812 /* we have to check for errors! */
2814 cb_log (3, stack->port, "L1: PH_ACTIVATE|REQUEST returned error!\n");
2819 case PH_ACTIVATE | INDICATION:
2820 cb_log (3, stack->port, "L1: PH L1Link Up!\n");
2825 pthread_mutex_lock(&stack->nstlock);
2826 if (stack->nst.l1_l2(&stack->nst, msg))
2828 pthread_mutex_unlock(&stack->nstlock);
2831 misdn_lib_get_l2_up(stack);
2837 case PH_ACTIVATE | REQUEST:
2839 cb_log(3,stack->port,"L1: PH_ACTIVATE|REQUEST \n");
2842 case PH_DEACTIVATE | REQUEST:
2844 cb_log(3,stack->port,"L1: PH_DEACTIVATE|REQUEST \n");
2847 case PH_DEACTIVATE | CONFIRM:
2848 /* we have to check for errors! */
2850 cb_log (3, stack->port, "L1: PH_DEACTIVATE|REQUEST returned error!\n");
2855 case PH_DEACTIVATE | INDICATION:
2856 cb_log (3, stack->port, "L1: PH L1Link Down! \n");
2859 for (i=0; i<=stack->b_num; i++) {
2860 if (global_state == MISDN_INITIALIZED) {
2861 cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
2867 pthread_mutex_lock(&stack->nstlock);
2868 if (stack->nst.l1_l2(&stack->nst, msg))
2870 pthread_mutex_unlock(&stack->nstlock);
2883 static int handle_l2(msg_t *msg)
2885 iframe_t *frm = (iframe_t*) msg->data;
2887 struct misdn_stack *stack = find_stack_by_addr(frm->addr);
2895 case DL_ESTABLISH | REQUEST:
2896 cb_log(1,stack->port,"DL_ESTABLISH|REQUEST \n");
2899 case DL_RELEASE | REQUEST:
2900 cb_log(1,stack->port,"DL_RELEASE|REQUEST \n");
2904 case DL_ESTABLISH | INDICATION:
2905 case DL_ESTABLISH | CONFIRM:
2907 cb_log (3, stack->port, "L2: L2Link Up! \n");
2908 if (stack->ptp && stack->l2link) {
2909 cb_log (-1, stack->port, "L2: L2Link Up! but it's already UP.. must be faulty, blocking port\n");
2910 cb_event(EVENT_PORT_ALARM, &stack->bc[0], glob_mgr->user_data);
2918 case DL_RELEASE | INDICATION:
2919 case DL_RELEASE | CONFIRM:
2921 cb_log (3, stack->port, "L2: L2Link Down! \n");
2932 static int handle_mgmt(msg_t *msg)
2934 iframe_t *frm = (iframe_t*) msg->data;
2935 struct misdn_stack *stack;
2937 if ( (frm->addr == 0) && (frm->prim == (MGR_DELLAYER|CONFIRM)) ) {
2938 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: 0 !\n") ;
2943 stack = find_stack_by_addr(frm->addr);
2946 if (frm->prim == (MGR_DELLAYER|CONFIRM)) {
2947 cb_log(2, 0, "MGMT: DELLAYER|CONFIRM Addr: %x !\n",
2957 case MGR_SHORTSTATUS | INDICATION:
2958 case MGR_SHORTSTATUS | CONFIRM:
2959 cb_log(5, stack->port, "MGMT: Short status dinfo %x\n",frm->dinfo);
2961 switch (frm->dinfo) {
2962 case SSTATUS_L1_ACTIVATED:
2963 cb_log(3, stack->port, "MGMT: SSTATUS: L1_ACTIVATED \n");
2967 case SSTATUS_L1_DEACTIVATED:
2968 cb_log(3, stack->port, "MGMT: SSTATUS: L1_DEACTIVATED \n");
2975 case SSTATUS_L2_ESTABLISHED:
2976 cb_log(3, stack->port, "MGMT: SSTATUS: L2_ESTABLISH \n");
2980 case SSTATUS_L2_RELEASED:
2981 cb_log(3, stack->port, "MGMT: SSTATUS: L2_RELEASED \n");
2989 case MGR_SETSTACK | INDICATION:
2990 cb_log(4, stack->port, "MGMT: SETSTACK|IND dinfo %x\n",frm->dinfo);
2993 case MGR_DELLAYER | CONFIRM:
2994 cb_log(4, stack->port, "MGMT: DELLAYER|CNF dinfo %x\n",frm->dinfo) ;
3001 if ( (frm->prim & 0x0f0000) == 0x0f0000) {
3002 cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ;
3011 static msg_t *fetch_msg(int midev)
3013 msg_t *msg=alloc_msg(MAX_MSG_SIZE);
3017 cb_log(0, 0, "fetch_msg: alloc msg failed !!");
3022 r=mISDN_read(midev,msg->data,MAX_MSG_SIZE, TIMEOUT_10SEC);
3026 free_msg(msg); /* danger, cause usually freeing in main_loop */
3027 cb_log(6,0,"Got empty Msg..\n");
3032 if (errno == EAGAIN) {
3033 /*we wait for mISDN here*/
3034 cb_log(4,0,"mISDN_read wants us to wait\n");
3039 cb_log(0,0,"mISDN_read returned :%d error:%s (%d)\n",r,strerror(errno),errno);
3043 if (!(frm->prim == (DL_DATA|INDICATION) )|| (frm->prim == (PH_DATA|INDICATION)))
3044 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 );
3049 void misdn_lib_isdn_l1watcher(int port)
3051 struct misdn_stack *stack;
3053 for (stack = glob_mgr->stack_list; stack && (stack->port != port); stack = stack->next)
3057 cb_log(4, port, "Checking L1 State\n");
3058 if (!stack->l1link) {
3059 cb_log(4, port, "L1 State Down, trying to get it up again\n");
3060 misdn_lib_get_short_status(stack);
3061 misdn_lib_get_l1_up(stack);
3062 misdn_lib_get_l2_up(stack);
3067 /* This is a thread */
3068 static void misdn_lib_isdn_event_catcher(void *arg)
3070 struct misdn_lib *mgr = arg;
3071 int zero_frm=0 , fff_frm=0 ;
3072 int midev= mgr->midev;
3076 msg_t *msg = fetch_msg(midev);
3082 frm = (iframe_t*) msg->data;
3084 /** When we make a call from NT2Ast we get these frames **/
3085 if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) {
3091 cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm);
3096 /** I get this sometimes after setup_bc **/
3097 if (frm->len == 0 && frm->dinfo == 0 && frm->prim == 0xffffffff ) {
3103 cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm);
3108 manager_isdn_handler(frm, msg);
3114 /** App Interface **/
3116 static int te_lib_init(void)
3118 char buff[1025] = "";
3119 iframe_t *frm = (iframe_t *) buff;
3123 midev = mISDN_open();
3128 /* create entity for layer 3 TE-mode */
3129 mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
3131 ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
3132 entity = frm->dinfo & 0xffff;
3133 if (ret < mISDN_HEADER_LEN || !entity) {
3134 fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n", strerror(errno));
3142 void te_lib_destroy(int midev)
3145 mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC);
3147 cb_log(4, 0, "Entity deleted\n");
3149 cb_log(4, 0, "midev closed\n");
3152 struct misdn_bchannel *manager_find_bc_by_pid(int pid)
3154 struct misdn_stack *stack;
3157 for (stack = glob_mgr->stack_list; stack; stack = stack->next) {
3158 for (i = 0; i <= stack->b_num; i++) {
3159 if (stack->bc[i].in_use && stack->bc[i].pid == pid) {
3160 return &stack->bc[i];
3168 static int test_inuse(struct misdn_bchannel *bc)
3173 gettimeofday(&now, NULL);
3174 if (bc->last_used.tv_sec == now.tv_sec
3175 && misdn_lib_port_is_pri(bc->port)) {
3176 cb_log(2, bc->port, "channel with stid:%x for one second still in use! (n:%d lu:%d)\n",
3177 bc->b_stid, (int) now.tv_sec, (int) bc->last_used.tv_sec);
3181 cb_log(3,bc->port, "channel with stid:%x not in use!\n", bc->b_stid);
3185 cb_log(2,bc->port, "channel with stid:%x in use!\n", bc->b_stid);
3190 static void prepare_bc(struct misdn_bchannel*bc, int channel)
3192 bc->channel = channel;
3193 bc->channel_preselected = channel?1:0;
3194 bc->need_disconnect=1;
3196 bc->need_release_complete=1;
3197 bc->cause = AST_CAUSE_NORMAL_CLEARING;
3199 if (++mypid>5000) mypid=1;
3205 struct misdn_bchannel *misdn_lib_get_free_bc(int port, int channel, int inout, int dec)
3207 struct misdn_stack *stack;
3211 if (channel < 0 || channel > MAX_BCHANS) {
3212 cb_log(0, port, "Requested channel out of bounds (%d)\n", channel);
3216 /* Find the port stack structure */