diff -u -N -r ast_clean/channels/chan_misdn.c ast_misdn/channels/chan_misdn.c --- ast_clean/channels/chan_misdn.c 1970-01-01 01:00:00.000000000 +0100 +++ ast_misdn/channels/chan_misdn.c 2005-10-17 19:31:23.000000000 +0200 @@ -0,0 +1,3700 @@ +/* + * Chan_Misdn -- Channel Driver for Asterisk + * + * Interface to Asterisk + * + * Copyright (C) 2004, Christian Richter + * + * Christian Richter + * + * This program is free software, distributed under the terms of + * the GNU General Public License + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "chan_misdn_config.h" +#include "isdn_lib.h" + +pthread_mutex_t release_lock_mutex; + +#define release_lock ast_mutex_lock(&release_lock_mutex) +#define release_unlock ast_mutex_unlock(&release_lock_mutex) + + +/* BEGIN: chan_misdn.h */ + +enum misdn_chan_state { + MISDN_NOTHING, /* at beginning */ + MISDN_WAITING4DIGS, /* when waiting for infos */ + MISDN_EXTCANTMATCH, /* when asterisk couldnt match our ext */ + MISDN_DIALING, /* when pbx_start */ + MISDN_PROGRESS, /* we got a progress */ + MISDN_CALLING, /* when misdn_call is called */ + MISDN_CALLING_ACKNOWLEDGE, /* when we get SETUP_ACK */ + MISDN_ALERTING, /* when Alerting */ + MISDN_BUSY, /* when BUSY */ + MISDN_CONNECTED, /* when connected */ + MISDN_BRIDGED, /* when bridged */ + MISDN_CLEANING, /* when hangup from * but we were connected before */ + MISDN_HUNGUP_FROM_MISDN, /* when DISCONNECT/RELEASE/REL_COMP cam from misdn */ + MISDN_HUNGUP_FROM_AST, /* when DISCONNECT/RELEASE/REL_COMP came out of */ + /* misdn_hangup */ + MISDN_HOLDED, /* if this chan is holded */ + MISDN_HOLD_DISCONNECT /* if this chan is holded */ + +}; + +#define ORG_AST 1 +#define ORG_MISDN 2 + +struct chan_list { + + sem_t sem; + pthread_mutex_t lock; + + pthread_t *audio_thread; + + enum misdn_chan_state state; + int holded; + int orginator; + + int norxtone; + int notxtone; + + int pipe[2]; + char ast_rd_buf[4096]; + struct ast_frame frame; + + int faxdetect; + int faxhandled; + + int ast_dsp; + + struct ast_dsp *dsp; + struct ast_trans_pvt *trans; + + struct ast_channel * ast; + + struct misdn_bchannel *bc; + struct misdn_bchannel *holded_bc; + + unsigned int l3id; + int addr; + + struct chan_list *peer; + struct chan_list *next; + struct chan_list *prev; + struct chan_list *first; +}; + +struct robin_list { + char *group; + int port; + int channel; + struct robin_list *next; + struct robin_list *prev; +}; +static struct robin_list *robin = NULL; + +static inline void free_robin_list_r (struct robin_list *r) +{ + if (r) { + if (r->next) free_robin_list_r(r->next); + if (r->group) free(r->group); + free(r); + } +} + +static void free_robin_list () +{ + free_robin_list_r(robin); +} + +struct robin_list* get_robin_position (char *group) +{ + struct robin_list *iter = robin; + for (; iter; iter = iter->next) { + if (!strcasecmp(iter->group, group)) + return iter; + } + struct robin_list *new = (struct robin_list *)calloc(1, sizeof(struct robin_list)); + new->group = strndup(group, strlen(group)); + new->channel = 1; + if (robin) { + new->next = robin; + robin->prev = new; + } + robin = new; + return robin; +} + +struct ast_channel *misdn_new(struct chan_list *cl, int state, char * name, char * context, char *exten, char *callerid, int format, int port, int c); +void send_digit_to_chan(struct chan_list *cl, char digit ); + + +#define AST_CID_P(ast) ast->cid.cid_num +#define AST_BRIDGED_P(ast) ast_bridged_channel(ast) +#define AST_LOAD_CFG ast_config_load +#define AST_DESTROY_CFG ast_config_destroy + +/* END: chan_misdn.h */ + +#include + +/* #define MISDN_DEBUG 1 */ + +static char *desc = "Channel driver for mISDN Support (Bri/Pri)"; +static char *type = "mISDN"; + +int tracing = 0 ; + +static int usecnt=0; + +char **misdn_key_vector=NULL; +int misdn_key_vector_size=0; + +/* Only alaw and mulaw is allowed for now */ +static int prefformat = AST_FORMAT_ALAW ; /* AST_FORMAT_SLINEAR ; AST_FORMAT_ULAW | */ + +static ast_mutex_t usecnt_lock; + +int *misdn_debug; +int *misdn_debug_only; +int max_ports; + +struct chan_list dummy_cl; + +struct chan_list *cl_te=NULL; +pthread_mutex_t cl_te_lock; + +enum event_response_e +cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data); + +void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel*bc); + +void cl_queue_chan(struct chan_list **list, struct chan_list *chan); +void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan); +struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc); +void * audio_thread( void * data); +void chan_misdn_log(int level, int port, char *tmpl, ...); +void chan_misdn_trace_call(struct ast_channel *chan, int debug, char *tmpl, ...); + +static int start_bc_tones(struct chan_list *cl); +static int stop_bc_tones(struct chan_list *cl); +static void release_chan(struct misdn_bchannel *bc); + +static int misdn_set_opt_exec(struct ast_channel *chan, void *data); +static int misdn_facility_exec(struct ast_channel *chan, void *data); + +/*************** Helpers *****************/ + +static struct chan_list * get_chan_by_ast(struct ast_channel *ast) +{ + struct chan_list *tmp; + + for (tmp=cl_te; tmp; tmp = tmp->next) { + if ( tmp->ast == ast ) return tmp; + } + + return NULL; +} + +static struct chan_list * get_chan_by_ast_name(char *name) +{ + struct chan_list *tmp; + + for (tmp=cl_te; tmp; tmp = tmp->next) { + if ( tmp->ast && strcmp(tmp->ast->name,name) == 0) return tmp; + } + + return NULL; +} + +static char* tone2str(struct misdn_bchannel *bc) +{ + static struct { + char name[16]; + enum tone_e tone; + } *tone, tone_buf[] = { + {"NOTONE",TONE_NONE}, + {"DIAL",TONE_DIAL}, + {"BUSY",TONE_BUSY}, + {"ALERT",TONE_ALERTING}, + {"",TONE_NONE} + }; + + + for (tone=&tone_buf[0]; tone->name[0]; tone++) { + if (tone->tone == bc->tone) return tone->name; + } + return NULL; +} + +static char *bearer2str(int cap) { + static char *bearers[]={ + "Speech", + "Audio 3.1k", + "Unres Digital", + "Res Digital", + "Unknown Bearer" + }; + + switch (cap) { + case INFO_CAPABILITY_SPEECH: + return bearers[0]; + break; + case INFO_CAPABILITY_AUDIO_3_1K: + return bearers[1]; + break; + case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: + return bearers[2]; + break; + case INFO_CAPABILITY_DIGITAL_RESTRICTED: + return bearers[3]; + break; + default: + return bearers[4]; + break; + } +} + +static void print_bearer(struct misdn_bchannel *bc) +{ + + chan_misdn_log(2, bc->stack->port, " --> Bearer: %s\n",bearer2str(bc->capability)); + + switch(bc->law) { + case INFO_CODEC_ALAW: + chan_misdn_log(2, bc->stack->port, " --> Codec: Alaw\n"); + break; + case INFO_CODEC_ULAW: + chan_misdn_log(2, bc->stack->port, " --> Codec: Ulaw\n"); + break; + } +} +/*************** Helpers END *************/ + +void send_digit_to_chan(struct chan_list *cl, char digit ) +{ + static const char* dtmf_tones[] = { + "!941+1336/100,!0/100", /* 0 */ + "!697+1209/100,!0/100", /* 1 */ + "!697+1336/100,!0/100", /* 2 */ + "!697+1477/100,!0/100", /* 3 */ + "!770+1209/100,!0/100", /* 4 */ + "!770+1336/100,!0/100", /* 5 */ + "!770+1477/100,!0/100", /* 6 */ + "!852+1209/100,!0/100", /* 7 */ + "!852+1336/100,!0/100", /* 8 */ + "!852+1477/100,!0/100", /* 9 */ + "!697+1633/100,!0/100", /* A */ + "!770+1633/100,!0/100", /* B */ + "!852+1633/100,!0/100", /* C */ + "!941+1633/100,!0/100", /* D */ + "!941+1209/100,!0/100", /* * */ + "!941+1477/100,!0/100" }; /* # */ + struct ast_channel *chan=cl->ast; + + if (digit >= '0' && digit <='9') + ast_playtones_start(chan,0,dtmf_tones[digit-'0'], 0); + else if (digit >= 'A' && digit <= 'D') + ast_playtones_start(chan,0,dtmf_tones[digit-'A'+10], 0); + else if (digit == '*') + ast_playtones_start(chan,0,dtmf_tones[14], 0); + else if (digit == '#') + ast_playtones_start(chan,0,dtmf_tones[15], 0); + else { + /* not handled */ + ast_log(LOG_DEBUG, "Unable to handle DTMF tone '%c' for '%s'\n", digit, chan->name); + + + } +} +/*** CLI HANDLING ***/ +static int misdn_set_debug(int fd, int argc, char *argv[]) +{ + if (argc != 4 && argc != 5 && argc != 6 && argc != 7) + return RESULT_SHOWUSAGE; + + int level = atoi(argv[3]); + + switch (argc) { + case 4: + case 5: { + int only = 0; + if (argc == 5) { + if (strncasecmp(argv[4], "only", strlen(argv[4]))) + return RESULT_SHOWUSAGE; + else + only = 1; + } + int i; + for (i=0; i<=max_ports; i++) { + misdn_debug[i] = level; + misdn_debug_only[i] = only; + } + ast_cli(fd, "changing debug level for all ports to %d%s\n",misdn_debug[0], only?" (only)":""); + } + break; + case 6: + case 7: { + if (strncasecmp(argv[4], "port", strlen(argv[4]))) + return RESULT_SHOWUSAGE; + int port = atoi(argv[5]); + if (port <= 0 || port > max_ports) { + switch (max_ports) { + case 0: + ast_cli(fd, "port number not valid! no ports available so you won't get lucky with any number here...\n"); + break; + case 1: + ast_cli(fd, "port number not valid! only port 1 is availble.\n"); + break; + default: + ast_cli(fd, "port number not valid! only ports 1 to %d are available.\n", max_ports); + } + return 0; + } + if (argc == 7) { + if (strncasecmp(argv[6], "only", strlen(argv[6]))) + return RESULT_SHOWUSAGE; + else + misdn_debug_only[port] = 1; + } else + misdn_debug_only[port] = 0; + misdn_debug[port] = level; + ast_cli(fd, "changing debug level to %d%s for port %d\n", misdn_debug[port], misdn_debug_only[port]?" (only)":"", port); + } + } + return 0; +} + + +static int misdn_set_crypt_debug(int fd, int argc, char *argv[]) +{ + if (argc != 5 )return RESULT_SHOWUSAGE; + + return 0; +} + +static int misdn_flush_stack (int fd, int argc, char *argv[]) +{ + int port, i; + struct misdn_stack *stack = get_misdn_stack(); + + if (argc != 4) + return RESULT_SHOWUSAGE; + + port = atoi(argv[3]); + + for (; + stack; + stack=stack->next ) { + if (stack->port == port) { + for (i=0; i< stack->b_num; i++) { + struct misdn_bchannel *mybc=&stack->bc[i]; + mybc->in_use=0; + } + } + } + + + return 0; +} + +static int misdn_restart_port (int fd, int argc, char *argv[]) +{ + int port; + + if (argc != 4) + return RESULT_SHOWUSAGE; + + port = atoi(argv[3]); + + misdn_lib_port_restart(port); + + return 0; +} + +static int misdn_port_up (int fd, int argc, char *argv[]) +{ + int port; + + if (argc != 4) + return RESULT_SHOWUSAGE; + + port = atoi(argv[3]); + + misdn_lib_get_port_up(port); + + return 0; +} + + +static int misdn_show_config (int fd, int argc, char *argv[]) +{ + char buffer[BUFFERSIZE]; + enum misdn_cfg_elements elem; + int linebreak; + + int onlyport = -1; + if (argc >= 4) { + if (!sscanf(argv[3], "%d", &onlyport) || onlyport < 0) { + ast_cli(fd, "Unknown option: %s\n", argv[3]); + return RESULT_SHOWUSAGE; + } + } + + if (argc == 3 || onlyport == 0) { + ast_cli(fd,"Misdn General-Config: \n"); + ast_cli(fd," -> VERSION: " CHAN_MISDN_VERSION "\n"); + + for (elem = MISDN_GEN_FIRST + 1, linebreak = 1; elem < MISDN_GEN_LAST; elem++, linebreak++) { + misdn_cfg_get_config_string( 0, elem, buffer, BUFFERSIZE); + ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : ""); + } + } + + if (onlyport < 0) { + int port = misdn_cfg_get_next_port(0); + for (; port > 0; port = misdn_cfg_get_next_port(port)) { + ast_cli(fd, "\n[PORT %d]\n", port); + for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) { + misdn_cfg_get_config_string( port, elem, buffer, BUFFERSIZE); + ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : ""); + } + ast_cli(fd, "\n"); + } + } + + if (onlyport > 0) { + if (misdn_cfg_is_port_valid(onlyport)) { + ast_cli(fd, "[PORT %d]\n", onlyport); + for (elem = MISDN_CFG_FIRST + 1, linebreak = 1; elem < MISDN_CFG_LAST; elem++, linebreak++) { + misdn_cfg_get_config_string( onlyport, elem, buffer, BUFFERSIZE); + ast_cli(fd, "%-36s%s", buffer, !(linebreak % 2) ? "\n" : ""); + } + ast_cli(fd, "\n"); + } else { + ast_cli(fd, "Port %d is not active!\n", onlyport); + } + } + return 0; +} + + + +struct state_struct { + enum misdn_chan_state state; + char txt[255] ; +} ; + +struct state_struct state_array[] = { + {MISDN_NOTHING,"NOTHING"}, /* at beginning */ + {MISDN_WAITING4DIGS,"WAITING4DIGS"}, /* when waiting for infos */ + {MISDN_EXTCANTMATCH,"EXTCANTMATCH"}, /* when asterisk couldnt match our ext */ + {MISDN_DIALING,"DIALING"}, /* when pbx_start */ + {MISDN_PROGRESS,"PROGRESS"}, /* when pbx_start */ + {MISDN_CALLING,"CALLING"}, /* when misdn_call is called */ + {MISDN_ALERTING,"ALERTING"}, /* when Alerting */ + {MISDN_BUSY,"BUSY"}, /* when BUSY */ + {MISDN_CONNECTED,"CONNECTED"}, /* when connected */ + {MISDN_BRIDGED,"BRIDGED"}, /* when bridged */ + {MISDN_CLEANING,"CLEANING"}, /* when hangup from * but we were connected before */ + {MISDN_HUNGUP_FROM_MISDN,"HUNGUP_FROM_MISDN"}, /* when DISCONNECT/RELEASE/REL_COMP cam from misdn */ + {MISDN_HOLDED,"HOLDED"}, /* when DISCONNECT/RELEASE/REL_COMP cam from misdn */ + {MISDN_HOLD_DISCONNECT,"HOLD_DISCONNECT"}, /* when DISCONNECT/RELEASE/REL_COMP cam from misdn */ + {MISDN_HUNGUP_FROM_AST,"HUNGUP_FROM_AST"} /* when DISCONNECT/RELEASE/REL_COMP came out of */ + /* misdn_hangup */ +}; + + + + +char *misdn_get_ch_state(struct chan_list *p) +{ + int i; + if( !p) return NULL; + + for (i=0; i< sizeof(state_array)/sizeof(struct state_struct); i++) { + if ( state_array[i].state == p->state) return state_array[i].txt; + } + + return NULL; +} + +static int misdn_reload (int fd, int argc, char *argv[]) +{ + int i, cfg_debug; + + ast_cli(fd, "Reloading mISDN Config\n"); + chan_misdn_log(0, 0, "Dynamic Crypting Activation is not support during reload at the moment\n"); + + free_robin_list(); + + misdn_cfg_reload(); + + { + char tempbuf[BUFFERSIZE]; + misdn_cfg_get( 0, MISDN_GEN_TRACEFILE, tempbuf, BUFFERSIZE); + if (strlen(tempbuf)) + tracing = 1; + } + + misdn_cfg_get( 0, MISDN_GEN_DEBUG, &cfg_debug, sizeof(int)); + for (i = 0; i <= max_ports; i++) { + misdn_debug[i] = cfg_debug; + misdn_debug_only[i] = 0; + } + + return 0; +} + +static void print_bc_info (int fd, struct chan_list* help, struct misdn_bchannel* bc) +{ + struct ast_channel *ast=help->ast; + ast_cli(fd, + "* Pid:%d Prt:%d Ch:%d Mode:%s Org:%s dad:%s oad:%s ctx:%s state:%s\n", + bc->pid, bc->stack->port, bc->channel, + bc->stack->mode==NT_MODE?"NT":"TE", + help->orginator == ORG_AST?"*":"I", + ast?ast->exten:NULL, + ast?AST_CID_P(ast):NULL, + ast?ast->context:NULL, + misdn_get_ch_state(help) + ); + if (misdn_debug[bc->stack->port] > 0) + ast_cli(fd, + " --> astname: %s\n" + " --> ch_l3id: %x\n" + " --> ch_addr: %x\n" + " --> bc_addr: %x\n" + " --> bc_l3id: %x\n" + " --> display: %s\n" + " --> activated: %d\n" + " --> capability: %s\n" + " --> echo_cancel: %d\n" + " --> notone : rx %d tx:%d\n" + " --> bc_hold: %d holded_bc :%d\n", + help->ast->name, + help->l3id, + help->addr, + bc->addr, + bc?bc->l3_id:-1, + bc->display, + + bc->active, + bearer2str(bc->capability), + bc->ec_enable, + help->norxtone,help->notxtone, + bc->holded, help->holded_bc?1:0 + ); + +} + + +static int misdn_show_cls (int fd, int argc, char *argv[]) +{ + struct chan_list *help=cl_te; + + ast_cli(fd,"Chan List: %p\n",cl_te); + + for (;help; help=help->next) { + struct misdn_bchannel *bc=help->bc; + struct ast_channel *ast=help->ast; + if (misdn_debug[0] > 2) ast_cli(fd, "Bc:%p Ast:%p\n", bc, ast); + if (bc) { + print_bc_info(fd, help, bc); + } else if ( (bc=help->holded_bc) ) { + chan_misdn_log(0, 0, "ITS A HOLDED BC:\n"); + print_bc_info(fd, help, bc); + } else { + ast_cli(fd,"* Channel in unknown STATE !!! Exten:%s, Callerid:%s\n", ast->exten, AST_CID_P(ast)); + } + } + + + return 0; +} + + + +static int misdn_show_cl (int fd, int argc, char *argv[]) +{ + struct chan_list *help=cl_te; + + if (argc != 4) + return RESULT_SHOWUSAGE; + + for (;help; help=help->next) { + struct misdn_bchannel *bc=help->bc; + struct ast_channel *ast=help->ast; + + if (bc && ast) { + if (!strcasecmp(ast->name,argv[3])) { + print_bc_info(fd, help, bc); + break; + } + } + } + + + return 0; +} + +pthread_mutex_t lock; +int MAXTICS=8; + +static int misdn_set_tics (int fd, int argc, char *argv[]) +{ + if (argc != 4) + return RESULT_SHOWUSAGE; + + MAXTICS=atoi(argv[3]); + + return 0; +} + + +static int misdn_show_fullstacks (int fd, int argc, char *argv[]) +{ + struct misdn_stack *stack = get_misdn_stack(); + ast_cli(fd, "BEGIN STACK_LIST:\n"); + for (; + stack; + stack=stack->next ) { + int i; + ast_cli(fd, "* Stack Addr: Uid %x Port %d Type %s Prot. %s Link %s\n",stack->upper_id, stack->upper_id & IF_CONTRMASK, stack->mode==NT_MODE?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN"); + for (i=0; i< stack->b_num; i++) { + struct misdn_bchannel *mybc=&stack->bc[i]; + ast_cli(fd," --> bchan: addr %x channel %d pid %d cr %x tone %s inuse %d\n", mybc->addr,mybc->channel, mybc?mybc->pid:-1,mybc?mybc->l3_id:-1 , tone2str(mybc), mybc->in_use); + } + } + + return 0; +} + +static int misdn_show_stacks (int fd, int argc, char *argv[]) +{ + struct misdn_stack *stack = get_misdn_stack(); + int i=1; + + ast_cli(fd, "BEGIN STACK_LIST:\n"); + for (; + stack; + stack=stack->next ) { + ast_cli(fd, "* Stack Addr: Port %d Type %s Prot. %s L2Link %s L1Link:%s Debug:%d%s\n", stack->upper_id & IF_CONTRMASK, stack->mode==NT_MODE?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN", stack->l1link?"UP":"DOWN", misdn_debug[i], misdn_debug_only[i]?"(only)":""); + i++; + } + + return 0; + +} + +static int misdn_show_port (int fd, int argc, char *argv[]) +{ + struct misdn_stack *stack = get_misdn_stack(); + int i; + int port; + + if (argc != 4) + return RESULT_SHOWUSAGE; + + port = atoi(argv[3]); + + ast_cli(fd, "BEGIN STACK_LIST:\n"); + for (; + stack; + stack=stack->next ) { + if (stack->port == port) { + ast_cli(fd, "* Stack Addr: %x Port %d Type %s Prot. %s L2Link %s L1Link:%s\n",stack->upper_id, stack->upper_id & IF_CONTRMASK, stack->mode==NT_MODE?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN", stack->l1link?"UP":"DOWN"); + for (i=0; i b_num; i++) { + ast_cli(fd,"Idx: %d stack->chan: %d Chan %d InUse:%d\n",i,stack->channels[i], i+1, stack->bc[i].in_use); + } + } + } + + return 0; +} + +static int misdn_send_cd (int fd, int argc, char *argv[]) +{ + char *channame; + char *nr; + + if (argc != 5) + return RESULT_SHOWUSAGE; + + channame = argv[3]; + nr = argv[4]; + + ast_cli(fd, "Sending Calldeflection (%s) to %s\n",nr, channame); + + { + struct chan_list *tmp=get_chan_by_ast_name(channame); + + if (!tmp) { + ast_cli(fd, "Sending CD with nr %s to %s failed Channel does not exist\n",nr, channame); + return 0; + } else { + + misdn_lib_send_facility(tmp->bc, FACILITY_CALLDEFLECT, nr); + } + } + + return 0; +} + + + +static int misdn_send_digit (int fd, int argc, char *argv[]) +{ + char *channame; + char *msg; + + if (argc != 5) + return RESULT_SHOWUSAGE; + + channame = argv[3]; + msg = argv[4]; + + ast_cli(fd, "Sending %s to %s\n",msg, channame); + + { + struct chan_list *tmp=get_chan_by_ast_name(channame); + + if (!tmp) { + ast_cli(fd, "Sending %s to %s failed Channel does not exist\n",msg, channame); + return 0; + } else { +#if 1 + int i; + int msglen = strlen(msg); + for (i=0; iast, 250); */ + usleep(250000); + /* res = ast_waitfor(tmp->ast,100); */ + } +#else + int res; + res = ast_dtmf_stream(tmp->ast,NULL,msg,250); +#endif + } + } + + return 0; +} + +static int misdn_toggle_echocancel (int fd, int argc, char *argv[]) +{ + char *channame; + + if (argc != 4) + return RESULT_SHOWUSAGE; + + channame = argv[3]; + + ast_cli(fd, "Toggling EchoCancel on %s\n", channame); + + { + struct chan_list *tmp=get_chan_by_ast_name(channame); + + if (!tmp) { + ast_cli(fd, "Toggling EchoCancel %s failed Channel does not exist\n", channame); + return 0; + } else { + tmp->bc->ec_enable=tmp->bc->ec_enable?0:1; + + if (tmp->bc->ec_enable) { + manager_ec_enable(tmp->bc); + } else { + manager_ec_disable(tmp->bc); + } + } + } + + return 0; +} + + + +static int misdn_send_display (int fd, int argc, char *argv[]) +{ + char *channame; + char *msg; + + if (argc != 5) + return RESULT_SHOWUSAGE; + + channame = argv[3]; + msg = argv[4]; + + ast_cli(fd, "Sending %s to %s\n",msg, channame); + { + struct chan_list *tmp; + tmp=get_chan_by_ast_name(channame); + + if (tmp && tmp->bc) { + int l = sizeof(tmp->bc->display); + strncpy(tmp->bc->display, msg, l); + tmp->bc->display[l-1] = 0; + misdn_lib_send_event(tmp->bc, EVENT_INFORMATION); + } else { + ast_cli(fd,"No such channel %s\n",channame); + return RESULT_FAILURE; + } + } + + return RESULT_SUCCESS ; +} + + + + +static char *complete_ch_helper(char *line, char *word, int pos, int state, int rpos) +{ + struct ast_channel *c; + int which=0; + char *ret; + if (pos != rpos) + return NULL; + c = ast_channel_walk_locked(NULL); + while(c) { + if (!strncasecmp(word, c->name, strlen(word))) { + if (++which > state) + break; + } + ast_mutex_unlock(&c->lock); + c = ast_channel_walk_locked(c); + } + if (c) { + ret = strdup(c->name); + ast_mutex_unlock(&c->lock); + } else + ret = NULL; + return ret; +} + +static char *complete_ch(char *line, char *word, int pos, int state) +{ + return complete_ch_helper(line, word, pos, state, 3); +} + +static char *complete_debug_port (char *line, char *word, int pos, int state) +{ + if (state) + return NULL; + + switch (pos) { + case 4: if (*word == 'p') + return strdup("port"); + else if (*word == 'o') + return strdup("only"); + break; + case 6: if (*word == 'o') + return strdup("only"); + break; + } + return NULL; +} + +static struct ast_cli_entry cli_send_cd = +{ {"misdn","send","calldeflect", NULL}, + misdn_send_cd, + "Sends CallDeflection to mISDN Channel", + "Usage: misdn send calldeflect \"\" \n", + complete_ch +}; + + +static struct ast_cli_entry cli_send_digit = +{ {"misdn","send","digit", NULL}, + misdn_send_digit, + "Sends DTMF Digit to mISDN Channel", + "Usage: misdn send digit \"\" \n" + " Send to as DTMF Tone\n" + " when channel is a mISDN channel\n", + complete_ch +}; + + +static struct ast_cli_entry cli_toggle_echocancel = +{ {"misdn","toggle","echocancel", NULL}, + misdn_toggle_echocancel, + "Toggles EchoCancel on mISDN Channel", + "Usage: misdn toggle echocancel \n", + complete_ch +}; + + + +static struct ast_cli_entry cli_send_display = +{ {"misdn","send","display", NULL}, + misdn_send_display, + "Sends Text to mISDN Channel", + "Usage: misdn send display \"\" \n" + " Send to as Display Message\n" + " when channel is a mISDN channel\n", + complete_ch +}; + + +static struct ast_cli_entry cli_show_config = +{ {"misdn","show","config", NULL}, + misdn_show_config, + "Shows internal mISDN config, read from cfg-file", + "Usage: misdn show config [port | 0]\n use 0 to only print the general config.\n" +}; + + +static struct ast_cli_entry cli_reload = +{ {"misdn","reload", NULL}, + misdn_reload, + "Reloads internal mISDN config, read from cfg-file", + "Usage: misdn reload\n" +}; + +static struct ast_cli_entry cli_set_tics = +{ {"misdn","set","tics", NULL}, + misdn_set_tics, + "", + "\n" +}; + + +static struct ast_cli_entry cli_show_cls = +{ {"misdn","show","channels", NULL}, + misdn_show_cls, + "Shows internal mISDN chan_list", + "Usage: misdn show channels\n" +}; + +static struct ast_cli_entry cli_show_cl = +{ {"misdn","show","channel", NULL}, + misdn_show_cl, + "Shows internal mISDN chan_list", + "Usage: misdn show channels\n", + complete_ch +}; + +static struct ast_cli_entry cli_show_fullstacks = +{ {"misdn","show","fullstacks", NULL}, + misdn_show_fullstacks, + "Shows internal mISDN stack_list with bchannels", + "Usage: misdn show fullstacks\n" +}; + +static struct ast_cli_entry cli_flush_stack = +{ {"misdn","flush","stack", NULL}, + misdn_flush_stack, + "Flushes the in_use flag", + "Usage: misdn flush stack\n" +}; + +static struct ast_cli_entry cli_restart_port = +{ {"misdn","restart","port", NULL}, + misdn_restart_port, + "Restarts the given port", + "Usage: misdn restart port\n" +}; + + +static struct ast_cli_entry cli_port_up = +{ {"misdn","port","up", NULL}, + misdn_port_up, + "Tries to establish L1 on the given port", + "Usage: misdn port up \n" +}; + + +static struct ast_cli_entry cli_show_stacks = +{ {"misdn","show","stacks", NULL}, + misdn_show_stacks, + "Shows internal mISDN stack_list", + "Usage: misdn show stacks\n" +}; + +static struct ast_cli_entry cli_show_port = +{ {"misdn","show","port", NULL}, + misdn_show_port, + "Shows detailed information for given port", + "Usage: misdn show port \n" +}; + + + +static struct ast_cli_entry cli_set_debug = +{ {"misdn","set","debug", NULL}, + misdn_set_debug, + "Sets Debuglevel of chan_misdn", + "Usage: misdn set debug [only] | [port [only]]\n", + complete_debug_port +}; + +static struct ast_cli_entry cli_set_crypt_debug = +{ {"misdn","set","crypt","debug", NULL}, + misdn_set_crypt_debug, + "Sets CryptDebuglevel of chan_misdn, at the moment, level={1,2}", + "Usage: misdn set crypt debug \n" +}; +/*** CLI END ***/ + + +/*****************************/ +/*** AST Indications Start ***/ +/*****************************/ + +static int misdn_call(struct ast_channel *ast, char *dest, int timeout) +{ + int port=0; + int r; + struct chan_list *ch=MISDN_ASTERISK_TECH_PVT(ast); + struct misdn_bchannel *newbc; + char *opts=NULL; + char dest_cp[256]; + + { + strncpy(dest_cp,dest,sizeof(dest_cp)-1); + dest_cp[sizeof(dest_cp)]=0; + opts=strchr(dest_cp,'/'); + if ( opts && (opts=strchr(++opts,'/')) ) { + if (opts) { + opts++; + if (!*opts) opts=NULL; + } + } + } + + if (!ast) { + ast_log(LOG_WARNING, " --> ! misdn_call called on ast_channel *ast where ast == NULL\n"); + return -1; + } + + if (((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) || !dest ) { + ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name); + ast->hangupcause=41; + ast_setstate(ast, AST_STATE_DOWN); + return -1; + } + + + if (!ch) { + ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name); + ast->hangupcause=41; + ast_setstate(ast, AST_STATE_DOWN); + return -1; + } + + newbc=ch->bc; + + if (!newbc) { + ast_log(LOG_WARNING, " --> ! misdn_call called on %s, neither down nor reserved (or dest==NULL)\n", ast->name); + ast->hangupcause=41; + ast_setstate(ast, AST_STATE_DOWN); + return -1; + } + + port=newbc->stack->port; + + chan_misdn_log(1, 0, "* CALL: %s\n",dest); + + + chan_misdn_log(1, port, " --> * dad:%s tech:%s ctx:%s\n",ast->exten,ast->name, ast->context); + + { + char context[BUFFERSIZE]; + + misdn_cfg_get( port, MISDN_CFG_CONTEXT, context, sizeof(ast->context)); + { + int l = sizeof(ast->context); + strncpy(ast->context,context, l); + ast->context[l-1] = 0; + } + chan_misdn_log(2, port, " --> * Setting Context to %s\n",context); + misdn_cfg_get( port, MISDN_CFG_LANGUAGE, ast->language, BUFFERSIZE); + + misdn_cfg_get( port, MISDN_CFG_TXGAIN, &newbc->txgain, sizeof(int)); + misdn_cfg_get( port, MISDN_CFG_RXGAIN, &newbc->rxgain, sizeof(int)); + + misdn_cfg_get( port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(newbc->stack->te_choose_channel), sizeof(int)); + + + { + char callerid[BUFFERSIZE]; + misdn_cfg_get( port, MISDN_CFG_CALLERID, callerid, BUFFERSIZE); + if ( ! ast_strlen_zero(callerid) ) { + chan_misdn_log(1, port, " --> * Setting Cid to %s\n", callerid); + { + int l = sizeof(newbc->oad); + strncpy(newbc->oad,callerid, l); + newbc->oad[l-1] = 0; + } + + misdn_cfg_get( port, MISDN_CFG_DIALPLAN, &newbc->dnumplan, sizeof(int)); + switch (newbc->dnumplan) { + case NUMPLAN_INTERNATIONAL: + case NUMPLAN_NATIONAL: + case NUMPLAN_SUBSCRIBER: + case NUMPLAN_UNKNOWN: + /* Maybe we should cut off the prefix if present ? */ + break; + default: + chan_misdn_log(0, port, " --> !!!! Wrong dialplan setting, please see the misdn.conf sample file\n "); + break; + } + + } + } + + + { + char buf[256]; + ast_group_t pg,cg; + + misdn_cfg_get( port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg)); + misdn_cfg_get( port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg)); + + chan_misdn_log(2, port, " --> * CallGrp:%s PickupGrp:%s\n",ast_print_group(buf,sizeof(buf),cg),ast_print_group(buf,sizeof(buf),pg)); + ast->pickupgroup=pg; + ast->callgroup=cg; + } + + /* Will be overridden by asterisk in head! */ + { + int pres; + + misdn_cfg_get( port, MISDN_CFG_PRES, &pres, sizeof(int)); + newbc->pres=pres?0:1; + + } + + int def_callingpres; + misdn_cfg_get( 0, MISDN_CFG_USE_CALLINGPRES, &def_callingpres, sizeof(int)); + if ( def_callingpres) { + switch (ast->cid.cid_pres){ + case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED: + newbc->pres=1; + break; + + case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN: + newbc->pres=0; + break; + default: + newbc->pres=0; + } + } + + + { + int ec, ectr; + + misdn_cfg_get( port, MISDN_CFG_ECHOCANCEL, &ec, sizeof(int)); + + misdn_cfg_get( port, MISDN_CFG_ECHOTRAINING, &ectr, sizeof(int)); + if (ec == 1 ) { + newbc->ec_enable=1; + } else if ( ec > 1 ) { + newbc->ec_enable=1; + newbc->ec_deftaps=ec; + } + + if ( !ectr ) { + newbc->ec_training=0; + } + } + + } + + + + + chan_misdn_log(3, port, " --> * adding2newbc ext %s\n",ast->exten); + if (ast->exten) { + int l = sizeof(newbc->dad); + strncpy(newbc->dad,ast->exten, l); + newbc->dad[l-1] = 0; + } + newbc->rad[0]=0; + chan_misdn_log(3, port, " --> * adding2newbc callerid %s\n",AST_CID_P(ast)); + if (ast_strlen_zero(newbc->oad) && AST_CID_P(ast) ) { + if (AST_CID_P(ast)) { + int l = sizeof(newbc->oad); + strncpy(newbc->oad,AST_CID_P(ast), l); + newbc->oad[l-1] = 0; + } + } + + { + struct chan_list *ch=MISDN_ASTERISK_TECH_PVT(ast); + if (!ch) { ast_verbose("No chan_list in misdn_call"); return -1;} + ch->bc = newbc; + ch->orginator=ORG_AST; + ch->ast = ast; + + MISDN_ASTERISK_TECH_PVT(ast) = ch ; + + + + newbc->capability=ast->transfercapability; + pbx_builtin_setvar_helper(ast,"TRANSFERCAPABILITY",ast_transfercapability2str(newbc->capability)); + if ( ast->transfercapability == INFO_CAPABILITY_DIGITAL_UNRESTRICTED) { + chan_misdn_log(2, port, " --> * Call with flag Digital\n"); + } + + + /* Finally The Options Override Everything */ + if (opts) misdn_set_opt_exec(ast,opts); + else + chan_misdn_log(1,0,"NO OPTS GIVEN\n"); + +/* switch (newbc->pres) { + case 0: + chan_misdn_log(1, port, " --> Number Screened\n"); + break; + case 1: + chan_misdn_log(1, port, " --> Number Not Screened\n"); + break; + default: + chan_misdn_log(1, port, " --> Other Screened\n"); + }*/ + + cl_queue_chan(&cl_te, ch) ; + ch->state=MISDN_CALLING; + + chan_misdn_trace_call(ast,1,"*->I: EVENT_CALL\n" ); + + r=misdn_lib_send_event( newbc, EVENT_SETUP ); + + /** we should have l3id after sending setup **/ + ch->l3id=newbc->l3_id; + } + + if ( r == -ENOCHAN ) { + chan_misdn_log(0, port, " --> * Theres no Channel at the moment .. !\n"); + chan_misdn_log(1, port, " --> * SEND: State Down pid:%d\n",newbc?newbc->pid:-1); + ast->hangupcause=34; + ast_setstate(ast, AST_STATE_DOWN); + return -1; + } + + chan_misdn_log(1, port, " --> * SEND: State Dialing pid:%d\n",newbc?newbc->pid:1); + + ast_setstate(ast, AST_STATE_DIALING); + + ast->hangupcause=16; + return 0; +} + + +int misdn_answer(struct ast_channel *ast) +{ + struct chan_list *p; + + + if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1; + p = MISDN_ASTERISK_TECH_PVT(ast) ; + + chan_misdn_trace_call(ast,1,"*->I: EVENT_ANSWER\n"); + + chan_misdn_log(1, p? (p->bc? p->bc->stack->port : 0) : 0, "* ANSWER:\n"); + + if (!p) { + ast_log(LOG_WARNING, " --> Channel not connected ??\n"); + ast_queue_hangup(ast); + } + + if (!p->bc) { + chan_misdn_log(1, 0, " --> Got Answer, but theres no bc obj ??\n"); + + ast_queue_hangup(ast); + } + + { + char *tmp_key = pbx_builtin_getvar_helper(p->ast, "CRYPT_KEY"); + + if (tmp_key ) { + chan_misdn_log(1, p->bc->stack->port, " --> Connection will be BF crypted\n"); + { + int l = sizeof(p->bc->crypt_key); + strncpy(p->bc->crypt_key,tmp_key, l); + p->bc->crypt_key[l-1] = 0; + } + } else { + chan_misdn_log(3, p->bc->stack->port, " --> Connection is without BF encryption\n"); + } + + } + + p->state = MISDN_CONNECTED; + misdn_lib_send_event( p->bc, EVENT_CONNECT); + start_bc_tones(p); + + + return 0; +} + +int misdn_digit(struct ast_channel *ast, char digit ) +{ + struct chan_list *p; + + if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1; + p = MISDN_ASTERISK_TECH_PVT(ast) ; + + + struct misdn_bchannel *bc=p->bc; + chan_misdn_log(1, bc?bc->stack->port:0, "* IND : Digit %c\n",digit); + + if (!bc) { + ast_log(LOG_WARNING, " --> !! Got Digit Event withut having bchannel Object\n"); + return -1; + } + + switch (p->state ) { + case MISDN_CALLING: + { + + char buf[8]; + buf[0]=digit; + buf[1]=0; + + int l = sizeof(bc->infos_pending); + strncat(bc->infos_pending,buf,l); + bc->infos_pending[l-1] = 0; + } + break; + case MISDN_CALLING_ACKNOWLEDGE: + { + bc->info_dad[0]=digit; + bc->info_dad[1]=0; + + { + int l = sizeof(bc->dad); + strncat(bc->dad,bc->info_dad, l - strlen(bc->dad)); + bc->dad[l-1] = 0; + } + { + int l = sizeof(p->ast->exten); + strncpy(p->ast->exten, bc->dad, l); + p->ast->exten[l-1] = 0; + } + + misdn_lib_send_event( bc, EVENT_INFORMATION); + } + break; + + default: + if ( bc->send_dtmf ) { + send_digit_to_chan(p,digit); + } + break; + } + + return 0; +} + + +int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast) +{ + struct chan_list *p; + + if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1; + p = MISDN_ASTERISK_TECH_PVT(ast) ; + + chan_misdn_log(1, p->bc?p->bc->stack->port:0, "* IND: Got Fixup State:%s Holded:%d L3id:%x\n", misdn_get_ch_state(p), p->holded, p->l3id); + + p->ast = ast ; + p->state=MISDN_CONNECTED; + + return 0; +} + + +int misdn_transfer (struct ast_channel *ast, char *dest) +{ + struct chan_list *p; + + if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1; + p = MISDN_ASTERISK_TECH_PVT(ast) ; + + chan_misdn_log(1, p->bc?p->bc->stack->port:0, "* IND : Got Transfer %s\n",dest); + return 0; +} + + + +int misdn_indication(struct ast_channel *ast, int cond) +{ + struct chan_list *p; + + + if (!ast || ! MISDN_ASTERISK_PVT(ast)) { + ast_log(LOG_WARNING, "Returnded -1 in misdn_indication\n"); + return -1; + } + p = MISDN_ASTERISK_TECH_PVT(ast) ; + + if (!p->bc ) { + chan_misdn_log(1, 0, "* IND : Indication from %s\n",ast->exten); + ast_log(LOG_WARNING, "Private Pointer but no bc ?\n"); + return -1; + } + + chan_misdn_log(1, p->bc->stack->port, "* IND : Indication from %s\n",ast->exten); + + switch (cond) { + case AST_CONTROL_BUSY: + chan_misdn_log(1, p->bc->stack->port, "* IND :\tbusy\n"); + chan_misdn_log(1, p->bc->stack->port, " --> * SEND: State Busy pid:%d\n",p->bc?p->bc->pid:-1); + ast_setstate(ast,AST_STATE_BUSY); + + p->bc->out_cause=17; + if (p->state != MISDN_CONNECTED) { + misdn_lib_send_event( p->bc, EVENT_DISCONNECT); + manager_send_tone(p->bc, TONE_BUSY); + } else { + chan_misdn_log(0, p->bc->stack->port, " --> !! Got Busy in Connected State !?! port:%d ast:%s\n", + p->bc->stack->port, ast->name); + } + break; + case AST_CONTROL_RING: + chan_misdn_log(1, p->bc->stack->port, " --> * IND :\tring pid:%d\n",p->bc?p->bc->pid:-1); + break; + case AST_CONTROL_RINGING: + if ( p->state == MISDN_ALERTING) { + chan_misdn_log(1, p->bc->stack->port, " --> * IND :\tringing pid:%d but I ws Ringing before, so ignoreing it\n",p->bc?p->bc->pid:-1); + break; + } + p->state=MISDN_ALERTING; + + chan_misdn_log(1, p->bc->stack->port, " --> * IND :\tringing pid:%d\n",p->bc?p->bc->pid:-1); + + misdn_lib_send_event( p->bc, EVENT_ALERTING); + + manager_send_tone(p->bc, TONE_ALERTING); + chan_misdn_log(1, p->bc->stack->port, " --> * SEND: State Ring pid:%d\n",p->bc?p->bc->pid:-1); + ast_setstate(ast,AST_STATE_RINGING); + break; + + case AST_CONTROL_ANSWER: + chan_misdn_log(1, p->bc->stack->port, " --> * IND :\tanswer pid:%d\n",p->bc?p->bc->pid:-1); + break; + case AST_CONTROL_TAKEOFFHOOK: + chan_misdn_log(1, p->bc->stack->port, " --> *\ttakeoffhook pid:%d\n",p->bc?p->bc->pid:-1); + break; + case AST_CONTROL_OFFHOOK: + chan_misdn_log(1, p->bc->stack->port, " --> *\toffhook pid:%d\n",p->bc?p->bc->pid:-1); + break; + case AST_CONTROL_FLASH: + chan_misdn_log(1, p->bc->stack->port, " --> *\tflash pid:%d\n",p->bc?p->bc->pid:-1); + break; + case AST_CONTROL_PROGRESS: + chan_misdn_log(1, p->bc->stack->port, " --> * IND :\tprogress pid:%d\n",p->bc?p->bc->pid:-1); + break; + case AST_CONTROL_CONGESTION: + chan_misdn_log(1, p->bc->stack->port, " --> * IND :\tcongestion pid:%d\n",p->bc?p->bc->pid:-1); + + p->bc->out_cause=42; + if (p->state != MISDN_CONNECTED) { + start_bc_tones(p); + //misdn_lib_send_event( p->bc, EVENT_RELEASE_COMPLETE); + misdn_lib_send_event( p->bc, EVENT_RELEASE); + } else { + misdn_lib_send_event( p->bc, EVENT_DISCONNECT); + } + if (p->bc->stack->mode == NT_MODE) { + manager_send_tone(p->bc, TONE_BUSY); + } + break; + case -1 : + chan_misdn_log(1, p->bc->stack->port, " --> * IND :\t-1! pid:%d\n",p->bc?p->bc->pid:-1); + break; + case AST_CONTROL_HOLD: + chan_misdn_log(1, p->bc->stack->port, " --> *\tHOLD pid:%d\n",p->bc?p->bc->pid:-1); + break; + case AST_CONTROL_UNHOLD: + chan_misdn_log(1, p->bc->stack->port, " --> *\tUNHOLD pid:%d\n",p->bc?p->bc->pid:-1); + break; + default: + ast_log(LOG_WARNING, " --> * Unknown Indication:%d pid:%d\n",cond,p->bc?p->bc->pid:-1); + } + + return 0; +} + +int misdn_hangup(struct ast_channel *ast) +{ + struct chan_list *p; + struct misdn_bchannel *bc=NULL; + + if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1; + p = MISDN_ASTERISK_TECH_PVT(ast) ; + + release_lock; + + chan_misdn_trace_call(ast,1,"*->I: EVENT_HANGUP cause=%d\n",ast->hangupcause); + + ast_log(LOG_DEBUG, "misdn_hangup(%s)\n", ast->name); + + if (!p) { + chan_misdn_log(3, 0, "misdn_hangup called, without chan_list obj.\n"); + release_unlock; + return 0 ; + } + + + + MISDN_ASTERISK_TECH_PVT(ast)=NULL; + p->ast=NULL; + + if (ast->_state == AST_STATE_RESERVED) { + /* between request and call */ + MISDN_ASTERISK_TECH_PVT(ast)=NULL; + release_unlock; + + cl_dequeue_chan(&cl_te, p); + free(p); + + misdn_lib_release(bc); + + return 0; + } + + stop_bc_tones(p); + + release_unlock; + + + bc=p->bc; + + if (!bc) { + ast_log(LOG_WARNING,"Hangup with private but no bc ?\n"); + return 0; + } + + { + char *varcause=NULL; + bc->cause=ast->hangupcause?ast->hangupcause:16; + + if ( (varcause=pbx_builtin_getvar_helper(ast, "HANGUPCAUSE")) || + (varcause=pbx_builtin_getvar_helper(ast, "PRI_CAUSE"))) { + int tmpcause=atoi(varcause); + bc->out_cause=tmpcause?tmpcause:16; + } + + chan_misdn_log(1, bc->stack->port, "* IND : HANGUP\tpid:%d ctx:%s dad:%s oad:%s State:%s\n",p->bc?p->bc->pid:-1, ast->context, ast->exten, AST_CID_P(ast), misdn_get_ch_state(p)); + chan_misdn_log(2, bc->stack->port, " --> l3id:%x\n",p->l3id); + chan_misdn_log(1, bc->stack->port, " --> cause:%d\n",bc->cause); + chan_misdn_log(1, bc->stack->port, " --> out_cause:%d\n",bc->out_cause); + + switch (p->state) { + case MISDN_CALLING: + p->state=MISDN_CLEANING; + misdn_lib_send_event( bc, EVENT_RELEASE_COMPLETE); + break; + case MISDN_HOLDED: + case MISDN_DIALING: + start_bc_tones(p); + manager_send_tone(bc, TONE_BUSY); + p->state=MISDN_CLEANING; + + misdn_lib_send_event( bc, EVENT_RELEASE_COMPLETE); + + break; + + case MISDN_ALERTING: + chan_misdn_log(2, bc->stack->port, " --> * State Alerting\n"); + + if (p->orginator != ORG_AST) + manager_send_tone(bc, TONE_BUSY); + + p->state=MISDN_CLEANING; + misdn_lib_send_event( bc, EVENT_DISCONNECT); + break; + case MISDN_CONNECTED: + /* Alerting or Disconect */ + chan_misdn_log(2, bc->stack->port, " --> * State Connected\n"); + start_bc_tones(p); + manager_send_tone(bc, TONE_BUSY); + misdn_lib_send_event( bc, EVENT_DISCONNECT); + + p->state=MISDN_CLEANING; /* MISDN_HUNGUP_FROM_AST; */ + break; + + case MISDN_CLEANING: + break; + + case MISDN_HOLD_DISCONNECT: + /* need to send release here */ + chan_misdn_log(2, bc->stack->port, " --> state HOLD_DISC\n"); + chan_misdn_log(1, bc->stack->port, " --> cause %d\n",bc->cause); + chan_misdn_log(1, bc->stack->port, " --> out_cause %d\n",bc->out_cause); + + misdn_lib_send_event(bc,EVENT_RELEASE); + break; + default: + /* Alerting or Disconect */ + if (bc->stack->mode == NT_MODE) + misdn_lib_send_event(bc, EVENT_RELEASE); + else + misdn_lib_send_event(bc, EVENT_DISCONNECT); + p->state=MISDN_CLEANING; /* MISDN_HUNGUP_FROM_AST; */ + } + + } + + chan_misdn_log(1, bc->stack->port, "Channel: %s hanguped\n",ast->name); + + return 0; +} + +struct ast_frame *misdn_read(struct ast_channel *ast) +{ + struct chan_list *tmp; + + char blah[255]; + int len =0 ; + + if (!ast) return NULL; + tmp = MISDN_ASTERISK_TECH_PVT(ast); + if (!tmp) return NULL; + if (!tmp->bc) return NULL; + + + read(tmp->pipe[0],blah,sizeof(blah)); + + len = ibuf_usedcount(tmp->bc->astbuf); + + /*shrinken len if necessary, we transmit at maximum 4k*/ + len = len<=sizeof(tmp->ast_rd_buf)?len:sizeof(tmp->ast_rd_buf); + + ibuf_memcpy_r(tmp->ast_rd_buf, tmp->bc->astbuf,len); + + tmp->frame.frametype = AST_FRAME_VOICE; + tmp->frame.subclass = AST_FORMAT_ALAW; + tmp->frame.datalen = len; + tmp->frame.samples = len ; + tmp->frame.mallocd =0 ; + tmp->frame.offset= 0 ; + tmp->frame.src = NULL; + tmp->frame.data = tmp->ast_rd_buf ; + + chan_misdn_trace_call(tmp->ast,3,"*->I: EVENT_READ len=%d\n",len); + + return &tmp->frame; +} + +int misdn_write(struct ast_channel *ast, struct ast_frame *frame) +{ + struct chan_list *p; + int i = 0; + + if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1; + p = MISDN_ASTERISK_TECH_PVT(ast) ; + + if (!p->bc ) { + ast_log(LOG_WARNING, "private but no bc\n"); + return -1; + } + + if (p->bc->tone != TONE_NONE) + manager_send_tone(p->bc,TONE_NONE); + + + if (p->holded ) { + chan_misdn_log(5, p->bc->stack->port, "misdn_write: Returning because holded\n"); + return 0; + } + + if (p->notxtone) { + chan_misdn_log(5, p->bc->stack->port, "misdn_write: Returning because notxone\n"); + return 0; + } + + if ( !(frame->subclass & prefformat)) { + chan_misdn_log(0, p->bc->stack->port, "Got Unsupported Frame with Format:%d\n", frame->subclass); + } + + +#if MISDN_DEBUG + { + int i, max=5>frame->samples?frame->samples:5; + + printf("write2mISDN %p %d bytes: ", p, frame->samples); + + for (i=0; i< max ; i++) printf("%2.2x ",((char*) frame->data)[i]); + printf ("\n"); + } +#endif + chan_misdn_trace_call(ast,3,"*->I: EVENT_WRITE len=%d\n",frame->samples); + + i= manager_tx2misdn_frm(p->bc, frame->data, frame->samples); + + return 0; +} + + + +int misdn_bridge (struct ast_channel *c0, struct ast_channel *c1, int flags, + struct ast_frame **fo, struct ast_channel **rc) +{ + struct chan_list *ch1,*ch2; + struct ast_channel *carr[2], *who; + int to=-1; + struct ast_frame *f; + + ch1=get_chan_by_ast(c0); + ch2=get_chan_by_ast(c1); + + carr[0]=c0; + carr[1]=c1; + + + if (ch1 && ch2 ) ; + else + return -1; + + + int bridging; + misdn_cfg_get( 0, MISDN_GEN_BRIDGING, &bridging, sizeof(int)); + if (bridging) { + int ecwb; + misdn_cfg_get( ch1->bc->stack->port, MISDN_CFG_ECHOCANCELWHENBRIDGED, &ecwb, sizeof(int)); + if ( !ecwb ) { + chan_misdn_log(0, ch1->bc->stack->port, "Disabling Echo Cancellor when Bridged\n"); + ch1->bc->ec_enable=0; + manager_ec_disable(ch1->bc); + } + misdn_cfg_get( ch2->bc->stack->port, MISDN_CFG_ECHOCANCELWHENBRIDGED, &ecwb, sizeof(int)); + if ( !ecwb ) { + chan_misdn_log(0, ch2->bc->stack->port, "Disabling Echo Cancellor when Bridged\n"); + ch2->bc->ec_enable=0; + manager_ec_disable(ch2->bc); + } + + /* trying to make a mISDN_dsp conference */ + chan_misdn_log(0, ch1->bc->stack->port, "I SEND: Making conference with Number:%d\n", (ch1->bc->pid<<1) +1); + manager_ph_control(ch1->bc, CMX_RECEIVE_OFF, 0); + manager_ph_control(ch2->bc, CMX_RECEIVE_OFF, 0); + + + manager_ph_control(ch1->bc, CMX_CONF_JOIN, (ch1->bc->pid<<1) +1); + manager_ph_control(ch2->bc, CMX_CONF_JOIN, (ch1->bc->pid<<1) +1); + } + + chan_misdn_log(1, ch1->bc->stack->port, "* Makeing Native Bridge between %s and %s\n", ch1->bc->oad, ch2->bc->oad); + + while(1) { + to=-1; + who = ast_waitfor_n(carr, 2, &to); + f = ast_read(who); + + if (!f || f->frametype == AST_FRAME_CONTROL) { + /* got hangup .. */ + *fo=f; + *rc=who; + + break; + } + + + if (who == c0) { + ast_write(c1,f); + } + else { + ast_write(c0,f); + } + + } + + if (bridging) { + manager_ph_control(ch1->bc, CMX_RECEIVE_ON, 0); + manager_ph_control(ch2->bc, CMX_RECEIVE_ON, 0); + + chan_misdn_log(0, ch1->bc->stack->port, "I SEND: Splitting conference with Number:%d\n", (ch1->bc->pid<<1) +1); + manager_ph_control(ch1->bc, CMX_CONF_SPLIT, (ch1->bc->pid<<1) +1); + manager_ph_control(ch2->bc, CMX_CONF_SPLIT, (ch1->bc->pid<<1) +1); + } + + return 0; +} + +/** AST INDICATIONS END **/ + +static int start_bc_tones(struct chan_list* cl) +{ + manager_bchannel_activate(cl->bc); + manager_send_tone(cl->bc ,TONE_NONE); + cl->notxtone=0; + cl->norxtone=0; + return 0; +} + +static int stop_bc_tones(struct chan_list *cl) +{ + if (cl->bc) { + manager_bchannel_deactivate(cl->bc); + } + cl->notxtone=1; + cl->norxtone=1; + + return 0; +} + + +struct chan_list *init_chan_list(void) +{ + struct chan_list *cl=malloc(sizeof(struct chan_list)); + + if (!cl) { + chan_misdn_log(0, 0, "misdn_request: malloc failed!"); + return NULL; + } + + memset(cl,0,sizeof(struct chan_list)); + + return cl; + +} + +static struct ast_channel *misdn_request(const char *type, int format, void *data, int *cause) + +{ + struct ast_channel *tmp = NULL; + char group[BUFFERSIZE]=""; + char buf[128]; + char buf2[128], *ext=NULL, *port_str; + char *tokb=NULL, *p=NULL; + int channel=0, port=0; + struct misdn_bchannel *newbc = NULL; + + struct chan_list *cl=init_chan_list(); + + sprintf(buf,"%s/%s",type,(char*)data); + strncpy(buf2,data, 128); + buf2[127] = 0; + port_str=strtok_r(buf2,"/", &tokb); + + ext=strtok_r(NULL,"/", &tokb); + + if (!ext) { + ast_log(LOG_WARNING, " --> ! IND : CALL dad:%s WITH WRONG ARGS, check extension.conf\n",ext); + + return NULL; + } + + if (port_str) { + if (port_str[0]=='g' && port_str[1]==':' ) { + /* We make a group call lets checkout which ports are in my group */ + port_str += 2; + strncpy(group, port_str, BUFFERSIZE); + group[127] = 0; + chan_misdn_log(2, 0, " --> Group Call group: %s\n",group); + } + else if ((p = strchr(port_str, ':'))) { + // we have a preselected channel + *p = 0; + channel = atoi(++p); + port = atoi(port_str); + chan_misdn_log(2, port, " --> Call on preselected Channel (%d) on Port %d\n", channel, port); + } + else { + port = atoi(port_str); + } + + + } else { + ast_log(LOG_WARNING, " --> ! IND : CALL dad:%s WITHOUT PORT/Group, check extension.conf\n",ext); + return NULL; + } + + if (!ast_strlen_zero(group)) { + + char cfg_group[BUFFERSIZE]; + struct robin_list *rr = NULL; + + if (misdn_cfg_is_group_method(group, METHOD_ROUND_ROBIN)) { + chan_misdn_log(4, port, " --> STARTING ROUND ROBIN..."); + rr = get_robin_position(group); + } + + if (rr) { + int robin_channel = rr->channel; + int port_start; + int next_chan = 1; + + do { + port_start = 0; + for (port = misdn_cfg_get_next_port_spin(rr->port); port > 0 && port != port_start; + port = misdn_cfg_get_next_port_spin(port)) { + + if (!port_start) + port_start = port; + + if (port >= port_start) + next_chan = 1; + + if (port < port_start && next_chan) { + if (++robin_channel >= MAX_BCHANS) { + robin_channel = 1; + } + next_chan = 0; + } + + misdn_cfg_get(port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE); + + if (!strcasecmp(cfg_group, group)) { + int l1, port_up; + + misdn_cfg_get( 0, MISDN_GEN_L1_INFO_OK, &l1, sizeof(l1)); + port_up = misdn_lib_port_up(port); + + if ((l1 && port_up) || !l1) { + newbc = misdn_lib_get_free_bc(port, robin_channel); + if (newbc) { + chan_misdn_log(4, port, " Success! Found port:%d channel:%d\n", newbc->stack->port, newbc->channel); + if (port_up) + chan_misdn_log(4, port, "def_l1:%d, portup:%d\n", l1, port_up); + rr->port = newbc->stack->port; + rr->channel = newbc->channel; + break; + } + } + } + } + } while (!newbc && robin_channel != rr->channel); + + if (!newbc) + chan_misdn_log(4, port, " Failed! No free channel in group %d!", group); + } + + else { + for (port=misdn_cfg_get_next_port(0); port > 0; + port=misdn_cfg_get_next_port(port)) { + + misdn_cfg_get( port, MISDN_CFG_GROUPNAME, cfg_group, BUFFERSIZE); + + if (!strcasecmp(cfg_group, group)) { + int l1, port_up; + + misdn_cfg_get( 0, MISDN_GEN_L1_INFO_OK, &l1, sizeof(l1)); + port_up = misdn_lib_port_up(port); + + chan_misdn_log(4, port, "def_l1:%d, portup:%d\n", l1, port_up); + + if ((l1 && port_up) || !l1) { + newbc = misdn_lib_get_free_bc(port, 0); + if (newbc) + break; + } + } + } + } + + } else { + if (channel) + chan_misdn_log(1, port," --> preselected_channel: %d\n",channel); + newbc = misdn_lib_get_free_bc(port, channel); + } + + if (!newbc) { + chan_misdn_log(1, port, " --> ! No free channel chan ext:%s even after Group Call\n",ext); + chan_misdn_log(1, port, " --> SEND: State Down\n"); + return NULL; + } + + cl->bc=newbc; + + tmp = misdn_new(cl, AST_STATE_RESERVED, buf, "",ext, NULL, format, port, channel); + + + return tmp; +} + + +struct ast_channel_tech misdn_tech = { + .type="mISDN", + .description="Channel driver for mISDN Support (Bri/Pri)", + .capabilities= AST_FORMAT_ALAW , + .requester=misdn_request, + .send_digit=misdn_digit, + .call=misdn_call, + .bridge=misdn_bridge, + .hangup=misdn_hangup, + .answer=misdn_answer, + .read=misdn_read, + .write=misdn_write, + .indicate=misdn_indication, + .fixup=misdn_fixup, + .properties=0 + /* .transfer=misdn_transfer */ +}; + +struct ast_channel_tech misdn_tech_wo_bridge = { + .type="mISDN", + .description="Channel driver for mISDN Support (Bri/Pri)", + .capabilities=AST_FORMAT_ALAW , + .requester=misdn_request, + .send_digit=misdn_digit, + .call=misdn_call, + .hangup=misdn_hangup, + .answer=misdn_answer, + .read=misdn_read, + .write=misdn_write, + .indicate=misdn_indication, + .fixup=misdn_fixup, + .properties=0 + /* .transfer=misdn_transfer */ +}; + + +unsigned long glob_channel=0; + +struct ast_channel *misdn_new(struct chan_list *chlist, int state, char * name, char * context, char *exten, char *callerid, int format, int port, int c) +{ + struct ast_channel *tmp; + + tmp = ast_channel_alloc(1); + + if (tmp) { + chan_misdn_log(2, 0, " --> * NEW CHANNEL dad:%s oad:%s ctx:%s\n",exten,callerid, context); + + + if (c<=0) { + c=glob_channel++; + snprintf(tmp->name, sizeof(tmp->name), "%s/%d-u%d", + type, port, c); + } else { + snprintf(tmp->name, sizeof(tmp->name), "%s/%d-%d", + type, port, c); + } + + tmp->type = type; + + tmp->nativeformats = prefformat; + tmp->readformat = format; + tmp->writeformat = format; + + tmp->tech_pvt = chlist; + + int bridging; + misdn_cfg_get( 0, MISDN_GEN_BRIDGING, &bridging, sizeof(int)); + if (bridging) + tmp->tech = &misdn_tech; + else + tmp->tech = &misdn_tech_wo_bridge; + + + tmp->writeformat = format; + tmp->readformat = format; + tmp->priority=1; + + + strncpy(tmp->context, context, sizeof(tmp->context)-1); + strncpy(tmp->exten, exten, sizeof(tmp->exten) - 1); + + if (callerid) { + char *cid_name, *cid_num; + + ast_callerid_parse(callerid, &cid_name, &cid_num); + if (cid_name) + tmp->cid.cid_name=strdup(cid_name); + if (cid_num) + tmp->cid.cid_num=strdup(cid_num); + } + + { + if (pipe(chlist->pipe)<0) + perror("Pipe failed\n"); + + tmp->fds[0]=chlist->pipe[0]; + + } + misdn_cfg_get( 0, MISDN_CFG_LANGUAGE, tmp->language, sizeof(tmp->language)); + + if (chlist->bc) { + + if (misdn_cfg_is_port_valid(chlist->bc->stack->port) ) { + { + char buf[256]; + ast_group_t pg,cg; + + misdn_cfg_get( chlist->bc->stack->port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg)); + misdn_cfg_get( chlist->bc->stack->port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg)); + + chan_misdn_log(2, chlist->bc->stack->port, " --> * CallGrp:%s PickupGrp:%s\n",ast_print_group(buf,sizeof(buf),cg),ast_print_group(buf,sizeof(buf),pg)); + tmp->pickupgroup=pg; + tmp->callgroup=cg; + } + misdn_cfg_get( chlist->bc->stack->port, MISDN_CFG_TXGAIN, &chlist->bc->txgain, sizeof(int)); + misdn_cfg_get( chlist->bc->stack->port, MISDN_CFG_RXGAIN, &chlist->bc->rxgain, sizeof(int)); + chan_misdn_log(2, chlist->bc->stack->port, " --> rxgain:%d txgain:%d\n",chlist->bc->rxgain,chlist->bc->txgain); + + + } else { + chan_misdn_log(0, 0, " --> !! Config Not found in misdn_new port:%d\n",chlist->bc->stack->port); + } + + } else { + chan_misdn_log(3, 0, " --> Not Setting Pickupgroup, we have no bc yet\n"); + } + + ast_setstate(tmp, state); + if (state == AST_STATE_RING) + tmp->rings = 1; + else + tmp->rings = 0; + } else + ast_log(LOG_WARNING, "Unable to allocate channel structure\n"); + + return tmp; +} + + + +int misdn_tx2ast_frm(struct chan_list * tmp, char * buf, int len ) +{ + struct ast_frame frame; + + /* If in hold state we drop frame .. */ + if (tmp->holded || tmp->state == MISDN_CLEANING ) return 0; + + if (tmp->norxtone) { + chan_misdn_log(3, tmp->bc->stack->port, "misdn_tx2ast_frm: Returning because norxtone\n"); + return 0; + } + + frame.frametype = AST_FRAME_VOICE; + frame.subclass = AST_FORMAT_ALAW; + frame.datalen = len; + frame.samples = len ; + frame.mallocd =0 ; + frame.offset= 0 ; + frame.src = NULL; + frame.data = buf ; + + if (tmp->faxdetect || tmp->ast_dsp ) { + struct ast_frame *f,*f2; + if (tmp->trans) + f2=ast_translate(tmp->trans, &frame,0); + else { + chan_misdn_log(0, tmp->bc->stack->port, "No T-Path found\n"); + return 0; + } + + f = ast_dsp_process(tmp->ast, tmp->dsp, f2); + if (f && (f->frametype == AST_FRAME_DTMF)) { + ast_log(LOG_DEBUG, "Detected inband DTMF digit: %c", f->subclass); + if (f->subclass == 'f' && tmp->faxdetect) { + /* Fax tone -- Handle and return NULL */ + struct ast_channel *ast = tmp->ast; + if (!tmp->faxhandled) { + tmp->faxhandled++; + if (strcmp(ast->exten, "fax")) { + if (ast_exists_extension(ast, ast_strlen_zero(ast->macrocontext)? ast->context : ast->macrocontext, "fax", 1, AST_CID_P(ast))) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name); + /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */ + pbx_builtin_setvar_helper(ast,"FAXEXTEN",ast->exten); + if (ast_async_goto(ast, ast->context, "fax", 1)) + ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, ast->context); + } else + ast_log(LOG_NOTICE, "Fax detected, but no fax extension ctx:%s exten:%s\n",ast->context, ast->exten); + } else + ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n"); + } else + ast_log(LOG_DEBUG, "Fax already handled\n"); + frame.frametype = AST_FRAME_NULL; + frame.subclass = 0; + f = &frame; + } else if ( tmp->ast_dsp) { + struct ast_frame fr; + memset(&fr, 0 , sizeof(fr)); + fr.frametype = AST_FRAME_DTMF; + fr.subclass = f->subclass ; + fr.src=NULL; + fr.data = NULL ; + fr.datalen = 0; + fr.samples = 0 ; + fr.mallocd =0 ; + fr.offset= 0 ; + + chan_misdn_log(2, tmp->bc->stack->port, " --> * SEND: DTMF (AST_DSP) :%c\n",f->subclass); + ast_queue_frame(tmp->ast, &fr); + + frame.frametype = AST_FRAME_NULL; + frame.subclass = 0; + f = &frame; + } + } + } + + if (tmp && tmp->ast && MISDN_ASTERISK_PVT (tmp->ast) && MISDN_ASTERISK_TECH_PVT(tmp->ast) ) { +#if MISDN_DEBUG + int i, max=5>len?len:5; + + printf("write2* %p %d bytes: ",tmp, len); + + for (i=0; i< max ; i++) printf("%2.2x ",((char*) frame.data)[i]); + printf ("\n"); +#endif + chan_misdn_log(9, tmp->bc->stack->port, "Queueing %d bytes 2 Asterisk\n",len); + + ast_queue_frame(tmp->ast,&frame); + + } else { + ast_log (LOG_WARNING, "No ast || ast->pvt || ch\n"); + } + + return 0; +} + +/** Channel Queue ***/ + +struct chan_list *find_chan_by_l3id(struct chan_list *list, unsigned long l3id) +{ + struct chan_list *help=list; + for (;help; help=help->next) { + if (help->l3id == l3id ) return help; + } + + chan_misdn_log(4, list? (list->bc? list->bc->stack->port : 0) : 0, "$$$ find_chan: No channel found with l3id:%x\n",l3id); + + return NULL; +} + +struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel *bc) +{ + struct chan_list *help=list; + for (;help; help=help->next) { + if (help->bc == bc) return help; + } + + chan_misdn_log(4, bc->stack->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n",bc->oad,bc->dad); + + return NULL; +} + + +struct chan_list *find_holded(struct chan_list *list, struct misdn_bchannel *bc) +{ + struct chan_list *help=list; + + chan_misdn_log(4, bc->stack->port, "$$$ find_holded: channel:%d oad:%s dad:%s\n",bc->channel, bc->oad,bc->dad); + for (;help; help=help->next) { + chan_misdn_log(4, bc->stack->port, "$$$ find_holded: --> holded:%d channel:%d\n",help->bc->holded, help->bc->channel); + if (help->bc->stack == bc->stack + && help->bc->holded ) return help; + } + + chan_misdn_log(4, bc->stack->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n",bc->oad,bc->dad); + + return NULL; +} + +void cl_queue_chan(struct chan_list **list, struct chan_list *chan) +{ + chan_misdn_log(4, chan->bc? chan->bc->stack->port : 0, "* Queuing chan %p\n",chan); + + pthread_mutex_lock(&cl_te_lock); + if (!*list) { + *list = chan; + } else { + struct chan_list *help=*list; + for (;help->next; help=help->next); + help->next=chan; + } + chan->next=NULL; + pthread_mutex_unlock(&cl_te_lock); +} + +void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan) +{ + if (chan->dsp) + ast_dsp_free(chan->dsp); + if (chan->trans) + ast_translator_free_path(chan->trans); + + pthread_mutex_lock(&cl_te_lock); + if (!*list) { + pthread_mutex_unlock(&cl_te_lock); + return; + } + + if (*list == chan) { + *list=(*list)->next; + pthread_mutex_unlock(&cl_te_lock); + return ; + } + + { + struct chan_list *help=*list; + for (;help->next; help=help->next) { + if (help->next == chan) { + help->next=help->next->next; + pthread_mutex_unlock(&cl_te_lock); + return; + } + } + } + + pthread_mutex_unlock(&cl_te_lock); +} + +/** Channel Queue End **/ + + + +/** Isdn asks us to release channel, pendant to misdn_hangup **/ +static void release_chan(struct misdn_bchannel *bc) { + struct ast_channel *ast=NULL; + + { + struct chan_list *ch=find_chan_by_bc(cl_te, bc); + if (!ch) ch=find_chan_by_l3id (cl_te, bc->l3_id); + + release_lock; + if (ch->ast) { + ast=ch->ast; + } + release_unlock; + + chan_misdn_log(1, bc->stack->port, "Trying to Release bc with l3id: %x\n",bc->l3_id); + if (ch) { + if (ast) + chan_misdn_trace_call(ast,1,"I->*: EVENT_RELEASE\n"); + + close(ch->pipe[0]); + close(ch->pipe[1]); + + if (ast && MISDN_ASTERISK_PVT(ast)) { + chan_misdn_log(1, bc->stack->port, "* RELEASING CHANNEL pid:%d ctx:%s dad:%s oad:%s state: %s\n",bc?bc->pid:-1, ast->context, ast->exten,AST_CID_P(ast),misdn_get_ch_state(ch)); + chan_misdn_log(3, bc->stack->port, " --> * State Down\n"); + /* copy cause */ + send_cause2ast(ast,bc); + + MISDN_ASTERISK_TECH_PVT(ast)=NULL; + + + if (ast->_state != AST_STATE_RESERVED) { + chan_misdn_log(3, bc->stack->port, " --> Setting AST State to down\n"); + ast_setstate(ast, AST_STATE_DOWN); + } + + switch(ch->state) { + case MISDN_EXTCANTMATCH: + case MISDN_WAITING4DIGS: + { + chan_misdn_log(3, bc->stack->port, " --> * State Wait4dig | ExtCantMatch\n"); + ast_hangup(ast); + } + break; + + case MISDN_CALLING: + case MISDN_CALLING_ACKNOWLEDGE: + case MISDN_DIALING: + case MISDN_PROGRESS: + chan_misdn_log(2, bc->stack->port, "* --> In State Calling|Dialing\n"); + chan_misdn_log(2, bc->stack->port, "* --> Queue Hangup\n"); + + + ast_queue_control(ast, AST_CONTROL_HANGUP); + break; + case MISDN_CLEANING: + /* this state comes out of ast so we mustnt call a ast function ! */ + chan_misdn_log(2, bc->stack->port, "* --> In StateCleaning\n"); + break; + case MISDN_HOLD_DISCONNECT: + chan_misdn_log(2, bc->stack->port, "* --> In HOLD_DISC\n"); + break; + default: + chan_misdn_log(2, bc->stack->port, "* --> In State Default\n"); + chan_misdn_log(2, bc->stack->port, "* --> Queue Hangup\n"); + + + if (ast && MISDN_ASTERISK_PVT(ast)) { + ast_queue_hangup(ast); + } else { + chan_misdn_log (0, bc->stack->port, "!! Not really queued!\n"); + } + } + } + cl_dequeue_chan(&cl_te, ch); + + free(ch); + } else { + /* chan is already cleaned, so exiting */ + } + } +} +/*** release end **/ + +void misdn_transfer_bc(struct chan_list *tmp_ch, struct chan_list *holded_chan) +{ + chan_misdn_log(4,0,"TRANSFERING %s to %s\n",holded_chan->ast->name, tmp_ch->ast->name); + + tmp_ch->state=MISDN_HOLD_DISCONNECT; + + ast_moh_stop(AST_BRIDGED_P(holded_chan->ast)); + + holded_chan->state=MISDN_CONNECTED; + holded_chan->holded=0; + misdn_lib_transfer(holded_chan->bc?holded_chan->bc:holded_chan->holded_bc); + + ast_channel_masquerade(holded_chan->ast, AST_BRIDGED_P(tmp_ch->ast)); +} + + +void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , struct ast_channel *ast) +{ + char predial[256]=""; + char *p = predial; + + struct ast_frame fr; + + strncpy(predial, ast->exten, sizeof(predial) -1 ); + + ch->state=MISDN_DIALING; + + if (bc->stack->mode == NT_MODE) { + int ret; + ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE ); + } else { + int ret; + if (bc->stack->ptp) { + ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE ); + } else { + ret = misdn_lib_send_event(bc, EVENT_PROCEEDING ); + } + } + + manager_send_tone(bc,TONE_DIAL); + + chan_misdn_log(1, bc->stack->port, "* Starting Ast ctx:%s dad:%s oad:%s with 's' extension\n", ast->context, ast->exten, AST_CID_P(ast)); + + strncpy(ast->exten,"s", 2); + + if (ast_pbx_start(ast)<0) { + ast=NULL; + manager_send_tone(bc,TONE_BUSY); + if (bc->stack->mode == NT_MODE) + misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE ); + else + misdn_lib_send_event(bc, EVENT_DISCONNECT ); + } + + + while (!ast_strlen_zero(p) ) { + fr.frametype = AST_FRAME_DTMF; + fr.subclass = *p ; + fr.src=NULL; + fr.data = NULL ; + fr.datalen = 0; + fr.samples = 0 ; + fr.mallocd =0 ; + fr.offset= 0 ; + + if (ch->ast && MISDN_ASTERISK_PVT(ch->ast) && MISDN_ASTERISK_TECH_PVT(ch->ast)) { + ast_queue_frame(ch->ast, &fr); + } + p++; + } +} + + + +void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel*bc) { + + ast->hangupcause=bc->cause; + + switch ( bc->cause) { + + case 1: /** Congestion Cases **/ + case 2: + case 3: + case 4: + case 22: + case 27: + chan_misdn_log(1, bc?bc->stack->port:0, " --> * SEND: Queue Congestion pid:%d\n", bc?bc->pid:-1); + + ast_queue_control(ast, AST_CONTROL_CONGESTION); + break; + + case 21: + case 17: /* user busy */ + chan_misdn_log(1, bc?bc->stack->port:0, " --> * SEND: Queue Busy pid:%d\n", bc?bc->pid:-1); + + ast_queue_control(ast, AST_CONTROL_BUSY); + + break; + } +} + +/************************************************************/ +/* Receive Events from isdn_lib here */ +/************************************************************/ +enum event_response_e +cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data) +{ + struct chan_list *ch=find_chan_by_bc(cl_te, bc); + + if (!ch) + ch=find_chan_by_l3id(cl_te, bc->l3_id); + + if (event != EVENT_BCHAN_DATA) { /* Debug Only Non-Bchan */ + chan_misdn_log(1, bc->stack->port, "I IND :%s oad:%s dad:%s port:%d\n", manager_isdn_get_info(event), bc->oad, bc->dad, bc->stack->port); + misdn_lib_log_ies(bc); + } + + if (event != EVENT_SETUP) { + if (!ch) { + if (event != EVENT_CLEANUP ) + ast_log(LOG_WARNING, "Chan not existing at the moment bc->l3id:%x bc:%p event:%s port:%d channel:%d\n",bc->l3_id, bc, manager_isdn_get_info( event), bc->stack->port,bc->channel); + return -1; + } + } + + if (ch ) { + switch (event) { + case EVENT_RELEASE: + case EVENT_RELEASE_COMPLETE: + case EVENT_CLEANUP: + break; + default: + if ( !ch->ast || !MISDN_ASTERISK_PVT(ch->ast) || !MISDN_ASTERISK_TECH_PVT(ch->ast)) { + if (event!=EVENT_BCHAN_DATA) + ast_log(LOG_WARNING, "No Ast or No private Pointer in Event (%d:%s)\n", event, manager_isdn_get_info(event)); + return -1; + } + } + } + + + switch (event) { + case EVENT_NEW_L3ID: + ch->l3id=bc->l3_id; + break; + + case EVENT_NEW_BC: + if (bc) + ch->bc=bc; + break; + + case EVENT_DTMF_TONE: + { + /* sending INFOS as DTMF-Frames :) */ + struct ast_frame fr; + memset(&fr, 0 , sizeof(fr)); + fr.frametype = AST_FRAME_DTMF; + fr.subclass = bc->dtmf ; + fr.src=NULL; + fr.data = NULL ; + fr.datalen = 0; + fr.samples = 0 ; + fr.mallocd =0 ; + fr.offset= 0 ; + + chan_misdn_log(2, bc->stack->port, " --> DTMF:%c\n", bc->dtmf); + + ast_queue_frame(ch->ast, &fr); + } + break; + case EVENT_STATUS: + break; + + case EVENT_INFORMATION: + { + int stop_tone; + misdn_cfg_get( 0, MISDN_GEN_STOP_TONE, &stop_tone, sizeof(int)); + if ( stop_tone && bc->tone != TONE_NONE) { + manager_send_tone(bc,TONE_NONE); + } + + if (ch->state == MISDN_WAITING4DIGS ) { + /* Ok, incomplete Setup, waiting till extension exists */ + { + int l = sizeof(bc->dad); + strncat(bc->dad,bc->info_dad, l); + bc->dad[l-1] = 0; + } + + + { + int l = sizeof(ch->ast->exten); + strncpy(ch->ast->exten, bc->dad, l); + ch->ast->exten[l-1] = 0; + } +/* chan_misdn_log(5, bc->stack->port, "Can Match Extension: dad:%s oad:%s\n",bc->dad,bc->oad);*/ + + char bc_context[BUFFERSIZE]; + misdn_cfg_get( bc->stack->port, MISDN_CFG_CONTEXT, bc_context, BUFFERSIZE); + if(!ast_canmatch_extension(ch->ast, bc_context, bc->dad, 1, bc->oad)) { + chan_misdn_log(0, bc->stack->port, "Extension can never match, so disconnecting\n"); + manager_send_tone(bc,TONE_BUSY); + ch->state=MISDN_EXTCANTMATCH; + bc->out_cause=1; + if (bc->stack->mode == NT_MODE) + misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE ); + else + misdn_lib_send_event(bc, EVENT_DISCONNECT ); + break; + } + if (ast_exists_extension(ch->ast, bc_context, bc->dad, 1, bc->oad)) { + ch->state=MISDN_DIALING; + + manager_send_tone(bc,TONE_NONE); +/* chan_misdn_log(1, bc->stack->port, " --> * Starting Ast ctx:%s\n", ch->ast->context);*/ + if (ast_pbx_start(ch->ast)<0) { + chan_misdn_log(0, bc->stack->port, "ast_pbx_start returned < 0 in INFO\n"); + manager_send_tone(bc,TONE_BUSY); + if (bc->stack->mode == NT_MODE) + misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE ); + else + misdn_lib_send_event(bc, EVENT_DISCONNECT ); + } + } + + } else { + /* sending INFOS as DTMF-Frames :) */ + struct ast_frame fr; + fr.frametype = AST_FRAME_DTMF; + fr.subclass = bc->info_dad[0] ; + fr.src=NULL; + fr.data = NULL ; + fr.datalen = 0; + fr.samples = 0 ; + fr.mallocd =0 ; + fr.offset= 0 ; + + + int digits; + misdn_cfg_get( 0, MISDN_GEN_APPEND_DIGITS2EXTEN, &digits, sizeof(int)); + if (ch->state != MISDN_CONNECTED && digits) { + { + int l = sizeof(bc->dad); + strncat(bc->dad,bc->info_dad, l); + bc->dad[l-1] = 0; + } + { + int l = sizeof(ch->ast->exten); + strncpy(ch->ast->exten, bc->dad, l); + ch->ast->exten[l-1] = 0; + } + ast_cdr_update(ch->ast); + + ast_queue_frame(ch->ast, &fr); + + } + + } + } + break; + case EVENT_SETUP: + { + struct chan_list *ch=find_chan_by_bc(cl_te, bc); + if (ch && ch->state != MISDN_NOTHING ) { + chan_misdn_log(1, bc->stack->port, " --> Ignoring Call we have already one\n"); + return RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE; /* Ignore MSNs which are not in our List */ + } + } + + int msn_valid = misdn_cfg_is_msn_valid(bc->stack->port, bc->dad); + if (bc->stack->mode == TE_MODE && ! msn_valid) { + chan_misdn_log(1, bc->stack->port, " --> Ignoring Call, its not in our MSN List\n"); + return RESPONSE_IGNORE_SETUP; /* Ignore MSNs which are not in our List */ + } + + print_bearer(bc); + + { + struct chan_list *ch=init_chan_list(); + struct ast_channel *chan; + char name[128]; + if (!ch) { chan_misdn_log(0, bc->stack->port, "cb_events: malloc for chan_list failed!\n"); return 0;} + + ch->bc = bc; + ch->l3id=bc->l3_id; + ch->addr=bc->addr; + ch->orginator = ORG_MISDN; + + + { + char prefix[BUFFERSIZE]=""; + switch( bc->onumplan ) { + case NUMPLAN_INTERNATIONAL: + misdn_cfg_get( bc->stack->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE); + break; + + case NUMPLAN_NATIONAL: + misdn_cfg_get( bc->stack->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE); + break; + + + case NUMPLAN_SUBSCRIBER: + /* dunno what to do here ? */ + break; + + case NUMPLAN_UNKNOWN: + break; + default: + break; + } + + { + int l = strlen(prefix) + strlen(bc->oad); + char tmp[l+1]; + strcpy(tmp,prefix); + strcat(tmp,bc->oad); + strcpy(bc->oad,tmp); + } + + if (!ast_strlen_zero(bc->oad)) + sprintf(name,"mISDN/%d/%s",bc->stack->port,bc->oad); + else + sprintf(name,"mISDN/%d",bc->stack->port); + + + if (!ast_strlen_zero(bc->dad)) { + strncpy(bc->orig_dad,bc->dad, sizeof(bc->orig_dad)); + bc->orig_dad[sizeof(bc->orig_dad)-1] = 0; + } + + if ( ast_strlen_zero(bc->dad) && !ast_strlen_zero(bc->keypad)) { + strncpy(bc->dad,bc->keypad, sizeof(bc->dad)); + bc->dad[sizeof(bc->dad)-1] = 0; + } + prefix[0] = 0; + + switch( bc->dnumplan ) { + case NUMPLAN_INTERNATIONAL: + misdn_cfg_get( bc->stack->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE); + break; + + case NUMPLAN_NATIONAL: + misdn_cfg_get( bc->stack->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE); + break; + + + case NUMPLAN_SUBSCRIBER: + /* dunno what to do here ? */ + break; + + case NUMPLAN_UNKNOWN: + break; + default: + break; + } + + { + int l = strlen(prefix) + strlen(bc->dad); + char tmp[l+1]; + strcpy(tmp,prefix); + strcat(tmp,bc->dad); + strcpy(bc->dad,tmp); + } + + char bc_context[BUFFERSIZE]; + misdn_cfg_get( bc->stack->port, MISDN_CFG_CONTEXT, bc_context, BUFFERSIZE); + chan=misdn_new(ch, AST_STATE_RING,name ,bc_context, bc->dad, bc->oad, AST_FORMAT_ALAW, bc->stack->port, bc->channel); + + pbx_builtin_setvar_helper(ch->ast,"REDIRECTING_NUMBER",bc->rad); + + } + + ch->ast = chan; + + chan_misdn_trace_call(chan,1,"I->*: EVENT_SETUP\n"); + + if ( bc->pres ) { + chan->cid.cid_pres=AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED; + } else { + chan->cid.cid_pres=AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN; + } + + pbx_builtin_setvar_helper(chan, "TRANSFERCAPABILITY", ast_transfercapability2str(bc->capability)); + chan->transfercapability=bc->capability; + + switch (bc->capability) { + case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: + pbx_builtin_setvar_helper(chan,"CALLTYPE","DIGITAL"); + break; + default: + pbx_builtin_setvar_helper(chan,"CALLTYPE","SPEECH"); + } + + /** queue new chan **/ + cl_queue_chan(&cl_te, ch) ; + + /* Check for Pickup Request first */ + if (!strcmp(chan->exten, ast_pickup_ext())) { + int ret;/** Sending SETUP_ACK**/ + ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE ); + + if (ast_pickup_call(chan)) { + ast_hangup(chan); + } else { + ch->state = MISDN_CALLING_ACKNOWLEDGE; + + ch->ast=NULL; + + ast_setstate(chan, AST_STATE_DOWN); + ast_hangup(chan); + + break; + } + } + /* + added support for s extension hope it will help those poor cretains + which haven't overlap dial. + */ + { + + misdn_cfg_get( bc->stack->port, MISDN_CFG_LANGUAGE, chan->language, sizeof(chan->language)); + int ai; + misdn_cfg_get( bc->stack->port, MISDN_CFG_ALWAYS_IMMEDIATE, &ai, sizeof(ai)); + if ( ai ) { + do_immediate_setup(bc, ch , chan); + break; + } + + int immediate; + misdn_cfg_get( bc->stack->port, MISDN_CFG_IMMEDIATE, &immediate, sizeof(int)); + + if (ast_strlen_zero(bc->orig_dad) && immediate ) { + do_immediate_setup(bc, ch , chan); + break; + } + + } + + /** Now after we've finished configuring our channel object + we'll jump into the dialplan **/ + + char bc_context[BUFFERSIZE]; + misdn_cfg_get( bc->stack->port, MISDN_CFG_CONTEXT, bc_context, BUFFERSIZE); + if(!ast_canmatch_extension(ch->ast, bc_context, bc->dad, 1, bc->oad)) { + chan_misdn_log(0, bc->stack->port, "Extension can never match, so disconnecting\n"); + manager_send_tone(bc,TONE_BUSY); + ch->state=MISDN_EXTCANTMATCH; + bc->out_cause=1; + if (bc->stack->mode == NT_MODE) + misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE ); + else + misdn_lib_send_event(bc, EVENT_DISCONNECT ); + break; + } + + if (ast_exists_extension(ch->ast, bc_context, bc->dad, 1, bc->oad)) { + ch->state=MISDN_DIALING; + + if (bc->stack->mode == NT_MODE) { + int ret; + ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE ); + } else { + int ret; + ret= misdn_lib_send_event(bc, EVENT_PROCEEDING ); + } + + if (ast_pbx_start(chan)<0) { + chan_misdn_log(0, bc->stack->port, "ast_pbx_start returned <0 in SETUP\n"); + chan=NULL; + manager_send_tone(bc,TONE_BUSY); + if (bc->stack->mode == NT_MODE) + misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE ); + else + misdn_lib_send_event(bc, EVENT_DISCONNECT ); + } + } else { + int ret= misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE ); + if (ret == -ENOCHAN) { + ast_log(LOG_WARNING,"Channel was catched, before we could Acknowledge\n"); + misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); + } + /* send tone to phone :) */ + + int stop_tone; + misdn_cfg_get( 0, MISDN_GEN_STOP_TONE, &stop_tone, sizeof(int)); + if ( (!ast_strlen_zero(bc->dad)) && stop_tone ) + manager_send_tone(bc,TONE_NONE); + else + manager_send_tone(bc,TONE_DIAL); + + ch->state=MISDN_WAITING4DIGS; + } + + } + break; + case EVENT_SETUP_ACKNOWLEDGE: + { + ch->state = MISDN_CALLING_ACKNOWLEDGE; + if (!ast_strlen_zero(bc->infos_pending)) { + /* TX Pending Infos */ + + { + int l = sizeof(bc->dad); + strncat(bc->dad,bc->infos_pending, l - strlen(bc->dad)); + bc->dad[l-1] = 0; + } + { + int l = sizeof(ch->ast->exten); + strncpy(ch->ast->exten, bc->dad, l); + ch->ast->exten[l-1] = 0; + } + { + int l = sizeof(bc->info_dad); + strncpy(bc->info_dad, bc->infos_pending, l); + bc->info_dad[l-1] = 0; + } + strncpy(bc->infos_pending,"", 1); + + misdn_lib_send_event(bc, EVENT_INFORMATION); + } + } + break; + case EVENT_PROCEEDING: + { + + if ( misdn_cap_is_speech(bc->capability) && + misdn_inband_avail(bc) ) { + start_bc_tones(ch); + } + } + break; + case EVENT_PROGRESS: + if (bc->stack->mode == TE_MODE ) { + if ( misdn_cap_is_speech(bc->capability) && + misdn_inband_avail(bc) + ) { + start_bc_tones(ch); + } + + ast_queue_control(ch->ast, AST_CONTROL_PROGRESS); + + ch->state=MISDN_PROGRESS; + } + break; + + + case EVENT_ALERTING: + { + ch->state = MISDN_ALERTING; + + chan_misdn_trace_call(ch->ast,1,"I->*: EVENT_ALERTING\n"); + + ast_queue_control(ch->ast, AST_CONTROL_RINGING); + ast_setstate(ch->ast, AST_STATE_RINGING); + + if ( misdn_cap_is_speech(bc->capability) && misdn_inband_avail(bc)) { + start_bc_tones(ch); + } + } + break; + case EVENT_CONNECT: + misdn_lib_send_event(bc,EVENT_CONNECT_ACKNOWLEDGE); + case EVENT_CONNECT_ACKNOWLEDGE: + { + bc->state=STATE_CONNECTED; + + ch->l3id=bc->l3_id; + ch->addr=bc->addr; + + start_bc_tones(ch); + + chan_misdn_trace_call(ch->ast,1,"I->*: EVENT_CONNECT\n"); + + ch->state = MISDN_CONNECTED; + ast_queue_control(ch->ast, AST_CONTROL_ANSWER); + } + break; + case EVENT_DISCONNECT: + { + + struct chan_list *holded_ch=find_holded(cl_te, bc); + + if (holded_ch ) { + if (ch->state == MISDN_CONNECTED ) { + misdn_transfer_bc(ch, holded_ch) ; + misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); + break; + } + } + + send_cause2ast(ch->ast,bc); + + if (!misdn_inband_avail(bc)) { + /* If Inband Avail. In TE, wait till we get hangup from ast. */ + stop_bc_tones(ch); + } + bc->out_cause=16; + misdn_lib_send_event(bc,EVENT_RELEASE); + + } + break; + + case EVENT_RELEASE: + { + + switch ( bc->cause) { + + case -1: + /* + OK, it really sucks, this is a RELEASE from NT-Stack So we take + it and return easylie, It seems that we've send a DISCONNECT + before, so we should RELEASE_COMPLETE after that Disconnect + (looks like ALERTING State at misdn_hangup !! + */ + return RESPONSE_OK; + break; + } + + + bc->out_cause=16; + + stop_bc_tones(ch); + release_chan(bc); + } + break; + case EVENT_RELEASE_COMPLETE: + { + stop_bc_tones(ch); + release_chan(bc); + } + break; + + case EVENT_BCHAN_DATA: + { + chan_misdn_trace_call(ch->ast,3,"I->*: EVENT_B_DATA len=%d\n",bc->bframe_len); + + if ( !misdn_cap_is_speech(ch->bc->capability) || bc->nojitter) { + misdn_tx2ast_frm(ch, bc->bframe, bc->bframe_len ); + } else { + int len=bc->bframe_len; + + if (bc->bframe_len > ibuf_freecount(bc->astbuf)) { + ast_log(LOG_DEBUG, "sbuf overflow!\n"); + len=ibuf_freecount(bc->astbuf); + } + + ibuf_memcpy_w(bc->astbuf, bc->bframe, len); + + { + char blah[1]="\0"; +#ifdef FLATTEN_JITTER + { + struct timeval tv; + gettimeofday(&tv,NULL); + + if (tv.tv_usec % 10000 > 0 ) { + write(ch->pipe[1], blah,sizeof(blah)); + bc->time_usec=tv.tv_usec; + } + } +#else + write(ch->pipe[1], blah,sizeof(blah)); +#endif + + + } + } + + } + break; + case EVENT_TIMEOUT: + break; /* Ignore now .. */ + { + switch (ch->state) { + case MISDN_CALLING: + chan_misdn_log(0, bc?bc->stack->port:0, "GOT TIMOUT AT CALING pid:%d\n", bc?bc->pid:-1); + break; + case MISDN_DIALING: + case MISDN_PROGRESS: + break; + default: + misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); + } + } + break; + case EVENT_CLEANUP: + { + stop_bc_tones(ch); + release_chan(bc); + } + break; + + /***************************/ + /** Suplementary Services **/ + /***************************/ + case EVENT_RETRIEVE: + { + struct ast_channel *hold_ast=AST_BRIDGED_P(ch->ast); + ch->state = MISDN_CONNECTED; + + //ast_moh_stop(ch->ast); + //start_bc_tones(ch); + if (hold_ast) { + ast_moh_stop(hold_ast); + } + + if ( misdn_lib_send_event(bc, EVENT_RETRIEVE_ACKNOWLEDGE) < 0) + misdn_lib_send_event(bc, EVENT_RETRIEVE_REJECT); + + + } + break; + + case EVENT_HOLD: + { + int hold_allowed; + misdn_cfg_get( bc->stack->port, MISDN_CFG_HOLD_ALLOWED, &hold_allowed, sizeof(int)); + + if (!hold_allowed) { + chan_misdn_log(0, bc->stack->port, "Hold not allowed on port:%d\n", bc->stack->port); + misdn_lib_send_event(bc, EVENT_HOLD_REJECT); + break; + } + + { + struct chan_list *holded_ch=find_holded(cl_te, bc); + if (holded_ch) { + misdn_lib_send_event(bc, EVENT_HOLD_REJECT); + chan_misdn_log(0, bc->stack->port, "We can't use RETRIEVE at the moment due to mISDN bug!\n"); + break; + } + } + + if (AST_BRIDGED_P(ch->ast)){ + ch->state = MISDN_HOLDED; + ch->l3id = bc->l3_id; + + ast_moh_start(AST_BRIDGED_P(ch->ast), NULL); + misdn_lib_send_event(bc, EVENT_HOLD_ACKNOWLEDGE); + } else { + misdn_lib_send_event(bc, EVENT_HOLD_REJECT); + chan_misdn_log(0, bc->stack->port, "We aren't bridged to anybody\n"); + } + } + break; + default: + ast_log(LOG_WARNING, "Got Unknown Event\n"); + break; + } + + return RESPONSE_OK; +} + +/** TE STUFF END **/ + +/****************************************** + * + * Asterisk Channel Endpoint END + * + * + *******************************************/ + + +int clearl3_true ( void ) { + int default_clearl3; + misdn_cfg_get( 0, MISDN_GEN_CLEAR_L3, &default_clearl3, sizeof(int)); + return default_clearl3; +} + +int g_config_initialized=0; + +int load_module(void) +{ + int i; + + char ports[256]=""; + + max_ports=misdn_lib_maxports_get(); + + if (max_ports<=0) { + ast_log(LOG_ERROR, "Unable to initialize mISDN\n"); + return -1; + } + + + misdn_cfg_init(max_ports); + g_config_initialized=1; + + misdn_debug = (int *)malloc(sizeof(int) * (max_ports+1)); + misdn_cfg_get( 0, MISDN_GEN_DEBUG, &misdn_debug[0], sizeof(int)); + for (i = 1; i <= max_ports; i++) + misdn_debug[i] = misdn_debug[0]; + misdn_debug_only = (int *)calloc(max_ports + 1, sizeof(int)); + + + { + char tempbuf[BUFFERSIZE]; + misdn_cfg_get( 0, MISDN_GEN_TRACEFILE, tempbuf, BUFFERSIZE); + if (strlen(tempbuf)) + tracing = 1; + } + + pthread_mutex_init(&cl_te_lock, NULL); + pthread_mutex_init(&release_lock_mutex, NULL); + + misdn_cfg_get_ports_string(ports); + if (strlen(ports)) + chan_misdn_log(0, 0, "Got: %s from get_ports\n",ports); + + { + struct misdn_lib_iface iface = { + .cb_event = cb_events, + .cb_log = chan_misdn_log, + .cb_clearl3_true = clearl3_true + }; + if (misdn_lib_init(ports, &iface, NULL)) + chan_misdn_log(0, 0, "No te ports initialized\n"); + } + + + { + if (ast_channel_register(&misdn_tech)) { + ast_log(LOG_ERROR, "Unable to register channel class %s\n", type); + unload_module(); + return -1; + } + } + + ast_cli_register(&cli_send_display); + ast_cli_register(&cli_send_cd); + ast_cli_register(&cli_send_digit); + ast_cli_register(&cli_toggle_echocancel); + ast_cli_register(&cli_set_tics); + + ast_cli_register(&cli_show_cls); + ast_cli_register(&cli_show_cl); + ast_cli_register(&cli_show_config); + ast_cli_register(&cli_show_port); + ast_cli_register(&cli_show_stacks); + ast_cli_register(&cli_show_fullstacks); + ast_cli_register(&cli_flush_stack); + ast_cli_register(&cli_restart_port); + ast_cli_register(&cli_port_up); + ast_cli_register(&cli_set_debug); + ast_cli_register(&cli_set_crypt_debug); + ast_cli_register(&cli_reload); + + + ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_flags", + "misdn_set_opt(::..):\n" + "Sets mISDN opts. and optargs\n" + "\n" + ); + + + ast_register_application("misdn_facility", misdn_facility_exec, "misdn_facility", + "misdn_facility(||..)\n" + "Sends the Facility Message FACILITY_TYPE with \n" + "the given Arguments to the current ISDN Channel\n" + "Supported Facilities are:\n" + "\n" + "type=calldeflect args=Nr where to deflect\n" + "\n" + ); + + chan_misdn_log(0, 0, "-- mISDN Channel Driver Registred -- (BE AWARE THIS DRIVER IS EXPERIMENTAL!)\n"); + + return 0; +} + + + +int unload_module(void) +{ + /* First, take us out of the channel loop */ + chan_misdn_log(0, 0, "-- Unregistering mISDN Channel Driver --\n"); + + if (!g_config_initialized) return 0; + + ast_cli_unregister(&cli_send_display); + + ast_cli_unregister(&cli_send_cd); + + ast_cli_unregister(&cli_send_digit); + ast_cli_unregister(&cli_toggle_echocancel); + ast_cli_unregister(&cli_set_tics); + + ast_cli_unregister(&cli_show_cls); + ast_cli_unregister(&cli_show_cl); + ast_cli_unregister(&cli_show_config); + ast_cli_unregister(&cli_show_port); + ast_cli_unregister(&cli_show_stacks); + ast_cli_unregister(&cli_show_fullstacks); + ast_cli_unregister(&cli_flush_stack); + ast_cli_unregister(&cli_restart_port); + ast_cli_unregister(&cli_port_up); + ast_cli_unregister(&cli_set_debug); + ast_cli_unregister(&cli_set_crypt_debug); + ast_cli_unregister(&cli_reload); + /* ast_unregister_application("misdn_crypt"); */ + ast_unregister_application("misdn_set_opt"); + ast_unregister_application("misdn_facility"); + + ast_channel_unregister(&misdn_tech); + + free_robin_list(); + misdn_cfg_destroy(); + misdn_lib_destroy(); + + if (misdn_debug) + free(misdn_debug); + if (misdn_debug_only) + free(misdn_debug_only); + + return 0; +} + +int usecount(void) +{ + int res; + ast_mutex_lock(&usecnt_lock); + res = usecnt; + ast_mutex_unlock(&usecnt_lock); + return res; +} + +char *description(void) +{ + return desc; +} + +char *key(void) +{ + return ASTERISK_GPL_KEY; +} + +void chan_misdn_log(int level, int port, char *tmpl, ...) +{ + if (! (0 <= port <= max_ports)) { + ast_console_puts("cb_log called with out-of-range port number!\n"); + return; + } + + va_list ap; + char buf[1024]; + + va_start(ap, tmpl); + vsnprintf( buf, 1023, tmpl, ap ); + va_end(ap); + + if (misdn_debug_only[port] ? (level==1 && misdn_debug[port]) || (level==misdn_debug[port]) : level <= misdn_debug[port]) { + ast_console_puts(buf); + } + + if (level <= misdn_debug[0] && tracing) { + time_t tm = time(NULL); + char *tmp=ctime(&tm),*p; + char file[BUFFERSIZE]; + misdn_cfg_get( 0, MISDN_GEN_TRACEFILE, file, BUFFERSIZE); + FILE *fp= fopen(file, "a+"); + + p=strchr(tmp,'\n'); + if (p) *p=':'; + + if (!fp) { + ast_console_puts("Error opening Tracefile: "); + ast_console_puts(strerror(errno)); + ast_console_puts("\n"); + return ; + } + + fputs(tmp,fp); + fputs(" ", fp); + fputs(buf, fp); + + fclose(fp); + } +} + + +void chan_misdn_trace_call(struct ast_channel *chan, int debug, char *tmpl, ...) +{ + va_list ap; + char buf[1024]; + char name[1024]; + + int trace; + misdn_cfg_get( 0, MISDN_GEN_TRACE_CALLS, &trace, sizeof(int)); + if (!trace) return ; + + if (misdn_debug[0] < debug) return ; + + char tracedir[BUFFERSIZE]; + misdn_cfg_get( 0, MISDN_GEN_TRACE_DIR, tracedir, BUFFERSIZE); + sprintf(name,"%s/%s.%s",tracedir, chan->uniqueid, chan->cid.cid_num ); + + va_start(ap, tmpl); + + vsprintf( buf, tmpl, ap ); + + va_end(ap); + + time_t tm = time(NULL); + char *tmp=ctime(&tm),*p; + FILE *fp= fopen(name, "a"); + int fd; + + if (!fp) { + ast_console_puts("Error opening Tracefile"); + ast_console_puts(strerror(errno)); + ast_console_puts("\n"); + return ; + } + + fd=fileno(fp) ; + + flock(fd, LOCK_EX); + + p=strchr(tmp,'\n'); + if (p) *p=':'; + + + + fputs(tmp,fp); + fputs(" ", fp); + fputs(buf, fp); + + flock(fd, LOCK_UN); + + fclose(fp); + +} + + +/*** SOME APPS ;)***/ + +static int misdn_facility_exec(struct ast_channel *chan, void *data) +{ + struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan); + char *tok, *tokb; + + + if (strcasecmp(MISDN_ASTERISK_TYPE(chan),"mISDN")) { + ast_log(LOG_WARNING, "misdn_facility makes only sense with chan_misdn channels!\n"); + return -1; + } + + if (!data || ast_strlen_zero((char *)data)) { + ast_log(LOG_WARNING, "misdn_facility Requires arguments\n"); + return -1; + } + + tok=strtok_r((char*)data,"|", &tokb) ; + + if (!tok) { + ast_log(LOG_WARNING, "misdn_facility Requires arguments\n"); + return -1; + } + + if (!strcasecmp(tok,"calldeflect")) { + tok=strtok_r(NULL,"|", &tokb) ; + + if (!tok) { + ast_log(LOG_WARNING, "Facility: Call Defl Requires arguments\n"); + } + + misdn_lib_send_facility(ch->bc, FACILITY_CALLDEFLECT, tok); + + } else { + ast_log(LOG_WARNING, "Unknown Facility: %s\n",tok); + } + + return 0; + +} + + +static int misdn_set_opt_exec(struct ast_channel *chan, void *data) +{ + struct chan_list *ch = MISDN_ASTERISK_TECH_PVT(chan); + char *tok,*tokb; + int keyidx=0; + int rxgain=0; + int txgain=0; + + if (strcasecmp(MISDN_ASTERISK_TYPE(chan),"mISDN")) { + ast_log(LOG_WARNING, "misdn_set_opt makes only sense with chan_misdn channels!\n"); + return -1; + } + + if (!data || ast_strlen_zero((char *)data)) { + ast_log(LOG_WARNING, "misdn_set_opt Requires arguments\n"); + return -1; + } + + for (tok=strtok_r((char*)data, ":",&tokb); + tok; + tok=strtok_r(NULL,":",&tokb) ) { + int neglect=0; + + if (tok[0] == '!' ) { + neglect=1; + tok++; + } + + switch(tok[0]) { + + case 'd' : + strncpy(ch->bc->display,++tok,84); + chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Display:%s\n",ch->bc->display); + break; + + case 'n': + chan_misdn_log(1, ch->bc->stack->port, "SETOPT: No DSP\n"); + ch->bc->nodsp=1; + break; + + case 'j': + chan_misdn_log(1, ch->bc->stack->port, "SETOPT: No jitter\n"); + ch->bc->nojitter=1; + break; + + case 'v': + tok++; + + switch ( tok[0] ) { + case 'r' : + rxgain=atoi(++tok); + if (rxgain<-8) rxgain=-8; + if (rxgain>8) rxgain=8; + ch->bc->rxgain=rxgain; + chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Volume:%d\n",rxgain); + break; + case 't': + txgain=atoi(++tok); + if (txgain<-8) txgain=-8; + if (txgain>8) txgain=8; + ch->bc->txgain=txgain; + chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Volume:%d\n",txgain); + break; + } + break; + + case 'c': + keyidx=atoi(++tok); + + if (keyidx > misdn_key_vector_size || keyidx < 0 ) { + ast_log(LOG_WARNING, "You entered the keyidx: %d but we have only %d keys\n",keyidx, misdn_key_vector_size ); + continue; + } + + { + int l = sizeof(ch->bc->crypt_key); + strncpy(ch->bc->crypt_key, misdn_key_vector[keyidx], l); + ch->bc->crypt_key[l-1] = 0; + } + chan_misdn_log(0, ch->bc->stack->port, "SETOPT: crypt with key:%s\n",misdn_key_vector[keyidx]); + break; + + case 'e': + chan_misdn_log(1, ch->bc->stack->port, "SETOPT: EchoCancel\n"); + + if (neglect) { + ch->bc->ec_enable=0; + } else { + ch->bc->ec_enable=1; + ch->bc->orig=ch->orginator; + tok++; + if (tok) { + ch->bc->ec_deftaps=atoi(tok); + } + } + + break; + + case 'h': + chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Digital\n"); + if (strlen(tok) > 1 && tok[1]=='1') { + chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Digital TRANS_DIGITAL\n"); + ch->bc->nohdlc=1; + ch->bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED; + } else { + ch->bc->nohdlc=0; + ch->bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED; + } + break; + + case 's': + chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Send DTMF\n"); + ch->bc->send_dtmf=1; + break; + + case 'f': + chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Faxdetect\n"); + ch->faxdetect=1; + break; + + case 'a': + chan_misdn_log(1, ch->bc->stack->port, "SETOPT: AST_DSP (for DTMF)\n"); + ch->ast_dsp=1; + break; + + case 'p': + chan_misdn_log(1, ch->bc->stack->port, "SETOPT: callerpres: %s\n",&tok[1]); + /* CRICH: callingpres!!! */ + if (strstr(tok,"allowed") ) { + ch->bc->pres=0; + } else if (strstr(tok,"not_screened")) { + ch->bc->pres=1; + } + + + break; + + + default: + break; + } + } + + if (ch->faxdetect || ch->ast_dsp) { + + if (!ch->dsp) ch->dsp = ast_dsp_new(); + if (ch->dsp) ast_dsp_set_features(ch->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT); + if (!ch->trans) ch->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, AST_FORMAT_ALAW); + } + + if (ch->ast_dsp) { + chan_misdn_log(1,ch->bc->stack->port,"SETOPT: with AST_DSP we deactivate mISDN_dsp\n"); + ch->bc->nodsp=1; + ch->bc->nojitter=1; + } + + return 0; +} + + diff -u -N -r ast_clean/channels/chan_misdn_config.c ast_misdn/channels/chan_misdn_config.c --- ast_clean/channels/chan_misdn_config.c 1970-01-01 01:00:00.000000000 +0100 +++ ast_misdn/channels/chan_misdn_config.c 2005-10-17 19:31:23.000000000 +0200 @@ -0,0 +1,1057 @@ +/* + * Chan_Misdn -- Channel Driver for Asterisk + * + * Interface to Asterisk + * + * Copyright (C) 2005, Christian Richter + * + * Christian Richter + * + * This program is free software, distributed under the terms of + * the GNU General Public License + */ + + +#include "chan_misdn_config.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#define AST_LOAD_CFG ast_config_load +#define AST_DESTROY_CFG ast_config_destroy + +#define DEF_ECHOCANCEL 128 +#define DEF_ECHOTRAINING 1 + +struct msn_list { + char *msn; + struct msn_list *next; +}; + +struct port_config { + char *name; + int *rxgain; + int *txgain; + int *te_choose_channel; + char *context; + char *language; + char *callerid; + char *method; + int *dialplan; + char *nationalprefix; + char *internationalprefix; + int *pres; + int *always_immediate; + int *immediate; + int *hold_allowed; + int *early_bconnect; + int *use_callingpres; + int *echocancel; + int *echocancelwhenbridged; + int *echotraining; + struct msn_list *msn_list; + ast_group_t *callgroup; /* Call group */ + ast_group_t *pickupgroup; /* Pickup group */ +}; + +struct general_config { + int *debug; + char *tracefile; + int *trace_calls; + char *trace_dir; + int *bridging; + int *stop_tone_after_first_digit; + int *append_digits2exten; + int *l1_info_ok; + int *clear_l3; + int *dynamic_crypt; + char *crypt_prefix; + char *crypt_keys; +}; + +/* array of port configs, default is at position 0. */ +static struct port_config **port_cfg; +/* max number of available ports, is set on init */ +static int max_ports; +/* general config */ +static struct general_config *general_cfg; +/* storing the ptp flag separated to save memory */ +static int *ptp; + +static ast_mutex_t config_mutex; + + +static inline void misdn_cfg_lock (void) { + ast_mutex_lock(&config_mutex); +} + +static inline void misdn_cfg_unlock (void) { + ast_mutex_unlock(&config_mutex); +} + +static void free_msn_list (struct msn_list* iter) { + if (iter->next) + free_msn_list(iter->next); + if (iter->msn) + free(iter->msn); + free(iter); +} + +static void free_port_cfg (void) { + + struct port_config **free_list = (struct port_config **)calloc(max_ports + 1, sizeof(struct port_config *)); + + int i, j; + + for (i = 0; i < max_ports; i++) { + if (port_cfg[i]) { + for (j = 0; j < max_ports && free_list[j]; j++) { + if (free_list[j] && free_list[j] == port_cfg[i]) + continue; /* already in list */ + free_list[j] = port_cfg[i]; + } + } + } + +#define FREE_ELEM(elem) ({ \ + if (free_list[i]->elem) \ + free(free_list[i]->elem); \ + }) + + for (i = 0; i < max_ports; i++) { + if (free_list[i]) { + FREE_ELEM(name); + FREE_ELEM(rxgain); + FREE_ELEM(txgain); + FREE_ELEM(te_choose_channel); + FREE_ELEM(context); + FREE_ELEM(language); + FREE_ELEM(callerid); + FREE_ELEM(method); + FREE_ELEM(dialplan); + FREE_ELEM(nationalprefix); + FREE_ELEM(internationalprefix); + FREE_ELEM(pres); + FREE_ELEM(always_immediate); + FREE_ELEM(immediate); + FREE_ELEM(hold_allowed); + FREE_ELEM(early_bconnect); + FREE_ELEM(use_callingpres); + FREE_ELEM(echocancel); + FREE_ELEM(echocancelwhenbridged); + FREE_ELEM(echotraining); + if (free_list[i]->msn_list) + free_msn_list(free_list[i]->msn_list); + FREE_ELEM(callgroup); + FREE_ELEM(pickupgroup); + free(free_list[i]); + } + } + free(free_list); +} + +static void free_general_cfg (void) { + +#define FREE_GEN_ELEM(elem) ({ \ + if (general_cfg->elem) \ + free(general_cfg->elem); \ + }) + + FREE_GEN_ELEM(debug); + FREE_GEN_ELEM(tracefile); + FREE_GEN_ELEM(trace_calls); + FREE_GEN_ELEM(trace_dir); + FREE_GEN_ELEM(bridging); + FREE_GEN_ELEM(stop_tone_after_first_digit); + FREE_GEN_ELEM(append_digits2exten); + FREE_GEN_ELEM(l1_info_ok); + FREE_GEN_ELEM(clear_l3); + FREE_GEN_ELEM(dynamic_crypt); + FREE_GEN_ELEM(crypt_prefix); + FREE_GEN_ELEM(crypt_keys); +} + +#define GET_PORTCFG_STRCPY(item) ({ \ + char *temp; \ + if (port_cfg[port] && port_cfg[port]->item) \ + temp = port_cfg[port]->item; \ + else \ + temp = port_cfg[0]->item; \ + if (!temp || !memccpy((void *)buf, (void *)temp, '\0', bufsize)) \ + memset(buf, 0, 1); \ + }) + +#define GET_GENCFG_STRCPY(item) ({ \ + if (general_cfg && general_cfg->item) { \ + if (!memccpy((void *)buf, (void *)general_cfg->item, '\0', bufsize)) \ + memset(buf, 0, 1); \ + } else \ + memset(buf, 0, 1); \ + }) + +#define GET_PORTCFG_MEMCPY(item) ({ \ + typeof(port_cfg[0]->item) temp; \ + if (port_cfg[port] && port_cfg[port]->item) \ + temp = port_cfg[port]->item; \ + else \ + temp = port_cfg[0]->item; \ + if (temp) { \ + int l = sizeof(*temp); \ + if (l > bufsize) \ + memset(buf, 0, bufsize); \ + else \ + memcpy(buf, temp, l); \ + } else \ + memset(buf, 0, bufsize); \ + }) + +#define GET_GENCFG_MEMCPY(item) ({ \ + if (general_cfg && general_cfg->item) { \ + typeof(general_cfg->item) temp = general_cfg->item; \ + int l = sizeof(*temp); \ + if (l > bufsize) \ + memset(buf, 0, bufsize); \ + else \ + memcpy(buf, temp, l); \ + } else \ + memset(buf, 0, bufsize); \ + }) + +void misdn_cfg_get(int port, enum misdn_cfg_elements elem, void *buf, int bufsize) { + + if (!(elem > MISDN_GEN_FIRST) && !misdn_cfg_is_port_valid(port)) { + memset(buf, 0, bufsize); + ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Port number %d is not valid.\n", port); + return; + } + + misdn_cfg_lock(); + + switch (elem) { + + /* port config elements */ + + case MISDN_CFG_PTP: if (sizeof(ptp[port]) <= bufsize) + memcpy(buf, &ptp[port], sizeof(ptp[port])); + else + buf = 0; /* error, should not happen */ + break; + case MISDN_CFG_GROUPNAME: GET_PORTCFG_STRCPY(name); + break; + case MISDN_CFG_RXGAIN: GET_PORTCFG_MEMCPY(rxgain); + break; + case MISDN_CFG_TXGAIN: GET_PORTCFG_MEMCPY(txgain); + break; + case MISDN_CFG_TE_CHOOSE_CHANNEL: + GET_PORTCFG_MEMCPY(te_choose_channel); + break; + case MISDN_CFG_CONTEXT: GET_PORTCFG_STRCPY(context); + break; + case MISDN_CFG_LANGUAGE: GET_PORTCFG_STRCPY(language); + break; + case MISDN_CFG_CALLERID: GET_PORTCFG_STRCPY(callerid); + break; + case MISDN_CFG_METHOD: GET_PORTCFG_STRCPY(method); + break; + case MISDN_CFG_DIALPLAN: GET_PORTCFG_MEMCPY(dialplan); + break; + case MISDN_CFG_NATPREFIX: GET_PORTCFG_STRCPY(nationalprefix); + break; + case MISDN_CFG_INTERNATPREFIX: + GET_PORTCFG_STRCPY(internationalprefix); + break; + case MISDN_CFG_PRES: GET_PORTCFG_MEMCPY(pres); + break; + case MISDN_CFG_ALWAYS_IMMEDIATE: + GET_PORTCFG_MEMCPY(always_immediate); + break; + case MISDN_CFG_IMMEDIATE: GET_PORTCFG_MEMCPY(immediate); + break; + case MISDN_CFG_HOLD_ALLOWED: + GET_PORTCFG_MEMCPY(hold_allowed); + break; + case MISDN_CFG_EARLY_BCONNECT: + GET_PORTCFG_MEMCPY(early_bconnect); + break; + case MISDN_CFG_USE_CALLINGPRES: + GET_PORTCFG_MEMCPY(use_callingpres); + break; + case MISDN_CFG_ECHOCANCEL: + GET_PORTCFG_MEMCPY(echocancel ); + break; + case MISDN_CFG_ECHOCANCELWHENBRIDGED: + GET_PORTCFG_MEMCPY(echocancelwhenbridged); + break; + case MISDN_CFG_ECHOTRAINING: + GET_PORTCFG_MEMCPY(echotraining); + break; + case MISDN_CFG_CALLGROUP: GET_PORTCFG_MEMCPY(callgroup); + break; + case MISDN_CFG_PICKUPGROUP: GET_PORTCFG_MEMCPY(pickupgroup); + break; + + /* general config elements */ + + case MISDN_GEN_DEBUG: GET_GENCFG_MEMCPY(debug); + break; + case MISDN_GEN_TRACEFILE: GET_GENCFG_STRCPY(tracefile); + break; + case MISDN_GEN_TRACE_CALLS: GET_GENCFG_MEMCPY(trace_calls); + break; + case MISDN_GEN_TRACE_DIR: GET_GENCFG_STRCPY(trace_dir); + break; + case MISDN_GEN_BRIDGING: GET_GENCFG_MEMCPY(bridging); + break; + case MISDN_GEN_STOP_TONE: GET_GENCFG_MEMCPY(stop_tone_after_first_digit); + break; + case MISDN_GEN_APPEND_DIGITS2EXTEN: + GET_GENCFG_MEMCPY(append_digits2exten); + break; + case MISDN_GEN_L1_INFO_OK: GET_GENCFG_MEMCPY(l1_info_ok); + break; + case MISDN_GEN_CLEAR_L3: GET_GENCFG_MEMCPY(clear_l3); + break; + case MISDN_GEN_DYNAMIC_CRYPT: GET_GENCFG_MEMCPY(dynamic_crypt); + break; + case MISDN_GEN_CRYPT_PREFIX: GET_GENCFG_STRCPY(crypt_prefix); + break; + case MISDN_GEN_CRYPT_KEYS: GET_GENCFG_STRCPY(crypt_keys); + break; + default: memset(buf, 0, bufsize); + } + + misdn_cfg_unlock(); +} + +int misdn_cfg_is_msn_valid (int port, char* msn) { + + if (!misdn_cfg_is_port_valid(port)) { + ast_log(LOG_WARNING, "Invalid call to misdn_cfg_is_msn_valid! Port number %d is not valid.\n", port); + return 0; + } + + struct msn_list *iter; + + misdn_cfg_lock(); + + if (port_cfg[port]->msn_list) + iter = port_cfg[port]->msn_list; + else + iter = port_cfg[0]->msn_list; + for (; iter; iter = iter->next) + if (*(iter->msn) == '*' || !strcasecmp(iter->msn, msn)) { + misdn_cfg_unlock(); + return 1; + } + + misdn_cfg_unlock(); + + return 0; +} + +int misdn_cfg_is_port_valid (int port) { + + misdn_cfg_lock(); + + if (port < 1 || port > max_ports) { + misdn_cfg_unlock(); + return 0; + } + + int valid = (port_cfg[port] != NULL); + + misdn_cfg_unlock(); + + return valid; +} + +int misdn_cfg_is_group_method (char *group, enum misdn_cfg_method meth) { + + int i, re = 0; + char *method = NULL; + + misdn_cfg_lock(); + + for (i = 0; i < max_ports; i++) { + if (port_cfg[i]) { + if (!strcasecmp(port_cfg[i]->name, group)) + method = port_cfg[i]->method ? port_cfg[i]->method : port_cfg[0]->method; + } + } + + if (method) { + switch (meth) { + case METHOD_STANDARD: re = !strcasecmp(method, "standard"); + break; + case METHOD_ROUND_ROBIN: re = !strcasecmp(method, "round_robin"); + break; + } + } + + misdn_cfg_unlock(); + + return re; +} + +void misdn_cfg_get_ports_string (char *ports) { + *ports = 0; + char tmp[16]; + int l; + + misdn_cfg_lock(); + + int i = 1; + for (; i <= max_ports; i++) { + if (port_cfg[i]) { + if (ptp[i]) + sprintf(tmp, "%dptp,", i); + else + sprintf(tmp, "%d,", i); + strcat(ports, tmp); + } + } + + misdn_cfg_unlock(); + + if ((l = strlen(ports))) + ports[l-1] = 0; +} + +#define GET_CFG_STRING(typestr, type) ({ \ + if (port_cfg[port] && port_cfg[port]->type) \ + snprintf(buf, bufsize, "%s " #typestr ": %s", begin, port_cfg[port]->type); \ + else \ + snprintf(buf, bufsize, "%s " #typestr ": %s", begin, port_cfg[0]->type); \ + }) \ + +#define GET_GEN_STRING(typestr, type) ({ \ + snprintf(buf, bufsize, "%s " #typestr ": %s", begin, general_cfg->type ? general_cfg->type : "not set"); \ + }) \ + +#define GET_CFG_INT(typestr, type) ({ \ + if (port_cfg[port] && port_cfg[port]->type) \ + snprintf(buf, bufsize, "%s " #typestr ": %d", begin, *port_cfg[port]->type); \ + else \ + snprintf(buf, bufsize, "%s " #typestr ": %d", begin, *port_cfg[0]->type); \ + }) \ + +#define GET_GEN_INT(typestr, type) ({ \ + snprintf(buf, bufsize, "%s " #typestr ": %d", begin, general_cfg->type ? *general_cfg->type : 0); \ + }) \ + +#define GET_CFG_BOOL(typestr, type, yes, no) ({ \ + int bool; \ + if (port_cfg[port] && port_cfg[port]->type) \ + bool = *port_cfg[port]->type; \ + else \ + bool = *port_cfg[0]->type; \ + snprintf(buf, bufsize, "%s " #typestr ": %s", begin, bool ? #yes : #no); \ + }) \ + +#define GET_CFG_HYBRID(typestr, type, yes, no) ({ \ + int bool; \ + if (port_cfg[port] && port_cfg[port]->type) \ + bool = *port_cfg[port]->type; \ + else \ + bool = *port_cfg[0]->type; \ + if (bool == 1 || bool == 0) \ + snprintf(buf, bufsize, "%s " #typestr ": %s", begin, bool ? #yes : #no); \ + else \ + snprintf(buf, bufsize, "%s " #typestr ": %d", begin, bool); \ + }) \ + +#define GET_GEN_BOOL(typestr, type, yes, no) ({ \ + snprintf(buf, bufsize, "%s " #typestr ": %s", begin, general_cfg->type ? (*general_cfg->type ? #yes : #no) : "not set"); \ + }) \ + +#define GET_CFG_AST_GROUP_T(typestr, type) ({ \ + ast_group_t *tmp; \ + if (port_cfg[port] && port_cfg[port]->type) \ + tmp = port_cfg[port]->type; \ + else \ + tmp = port_cfg[0]->type; \ + if (tmp) { \ + char tmpbuf[256]; \ + snprintf(buf, bufsize, "%s " #typestr ": %s", begin, ast_print_group(tmpbuf, sizeof(tmpbuf), *tmp)); \ + } else \ + snprintf(buf, bufsize, "%s " #typestr ": %s", begin, "none"); \ + }) \ + +void misdn_cfg_get_config_string(int port, enum misdn_cfg_elements elem, char* buf, int bufsize) { + + if (!(elem > MISDN_GEN_FIRST) && !misdn_cfg_is_port_valid(port)) { + *buf = 0; + ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Port number %d is not valid.\n", port); + return; + } + + char begin[] = " -> "; + + misdn_cfg_lock(); + + switch (elem) { + + case MISDN_CFG_PTP: snprintf(buf, bufsize, "%s PTP: %s", begin, ptp[port] ? "yes" : "no"); + break; + case MISDN_CFG_GROUPNAME: GET_CFG_STRING(GROUPNAME, name); + break; + case MISDN_CFG_RXGAIN: GET_CFG_INT(RXGAIN, rxgain); + break; + case MISDN_CFG_TXGAIN: GET_CFG_INT(TXGAIN, txgain); + break; + case MISDN_CFG_TE_CHOOSE_CHANNEL: + GET_CFG_BOOL(TE_CHOOSE_CHANNEL, te_choose_channel, yes, no); + break; + case MISDN_CFG_CONTEXT: GET_CFG_STRING(CONTEXT, context); + break; + case MISDN_CFG_LANGUAGE: GET_CFG_STRING(LANGUAGE, language); + break; + case MISDN_CFG_CALLERID: GET_CFG_STRING(CALLERID, callerid); + break; + case MISDN_CFG_METHOD: GET_CFG_STRING(METHOD, method); + break; + case MISDN_CFG_DIALPLAN: GET_CFG_INT(DIALPLAN, dialplan); + break; + case MISDN_CFG_NATPREFIX: GET_CFG_STRING(NATIONALPREFIX, nationalprefix); + break; + case MISDN_CFG_INTERNATPREFIX: + GET_CFG_STRING(INTERNATIONALPREFIX, internationalprefix); + break; + case MISDN_CFG_PRES: GET_CFG_BOOL(PRESENTATION, pres, allowed, not_screened); + break; + case MISDN_CFG_ALWAYS_IMMEDIATE: + GET_CFG_BOOL(ALWAYS_IMMEDIATE, always_immediate, yes, no); + break; + case MISDN_CFG_IMMEDIATE: GET_CFG_BOOL(IMMEDIATE, immediate, yes, no); + break; + case MISDN_CFG_HOLD_ALLOWED: + GET_CFG_BOOL(HOLD_ALLOWED, hold_allowed, yes, no); + break; + case MISDN_CFG_EARLY_BCONNECT: + GET_CFG_BOOL(EARLY_BCONNECT, early_bconnect, yes, no); + break; + case MISDN_CFG_USE_CALLINGPRES: + GET_CFG_BOOL(USE_CALLINGPRES, use_callingpres, yes, no); + break; + case MISDN_CFG_ECHOCANCEL: GET_CFG_HYBRID(ECHOCANCEL, echocancel, yes, no); + break; + case MISDN_CFG_ECHOCANCELWHENBRIDGED: + GET_CFG_BOOL(ECHOCANCELWHENBRIDGED, echocancelwhenbridged, yes, no); + break; + case MISDN_CFG_ECHOTRAINING: + GET_CFG_HYBRID(ECHOTRAINING, echotraining, yes, no); + break; + case MISDN_CFG_CALLGROUP: GET_CFG_AST_GROUP_T(CALLINGGROUP, callgroup); + break; + case MISDN_CFG_PICKUPGROUP: GET_CFG_AST_GROUP_T(PICKUPGROUP, pickupgroup); + break; + case MISDN_CFG_MSNS: { + char tmpbuffer[BUFFERSIZE]; + tmpbuffer[0] = 0; + struct msn_list *iter; + if (port_cfg[port]->msn_list) + iter = port_cfg[port]->msn_list; + else + iter = port_cfg[0]->msn_list; + if (iter) { + for (; iter; iter = iter->next) + sprintf(tmpbuffer, "%s%s, ", tmpbuffer, iter->msn); + tmpbuffer[strlen(tmpbuffer)-2] = 0; + } + snprintf(buf, bufsize, "%s MSNs: %s", begin, *tmpbuffer ? tmpbuffer : "none"); \ + } + break; + + /* general config elements */ + + case MISDN_GEN_DEBUG: GET_GEN_INT(DEBUG_LEVEL, debug); + break; + case MISDN_GEN_TRACEFILE: GET_GEN_STRING(TRACEFILE, tracefile); + break; + case MISDN_GEN_TRACE_CALLS: GET_GEN_BOOL(TRACE_CALLS, trace_calls, true, false); + break; + case MISDN_GEN_TRACE_DIR: GET_GEN_STRING(TRACE_DIR, trace_dir); + break; + case MISDN_GEN_BRIDGING: GET_GEN_BOOL(BRIDGING, bridging, yes, no); + break; + case MISDN_GEN_STOP_TONE: GET_GEN_BOOL(STOP_TONE_AFTER_FIRST_DIGIT, stop_tone_after_first_digit, yes, no); + break; + case MISDN_GEN_APPEND_DIGITS2EXTEN: + GET_GEN_BOOL(APPEND_DIGITS2EXTEN, append_digits2exten, yes, no); + break; + case MISDN_GEN_L1_INFO_OK: GET_GEN_BOOL(L1_INFO_OK, l1_info_ok, yes, no); + break; + case MISDN_GEN_CLEAR_L3: GET_GEN_BOOL(CLEAR_L3, clear_l3, yes, no); + break; + case MISDN_GEN_DYNAMIC_CRYPT: + GET_GEN_BOOL(DYNAMIC_CRYPT,dynamic_crypt, yes, no); + break; + case MISDN_GEN_CRYPT_PREFIX: + GET_GEN_STRING(CRYPT_PREFIX, crypt_prefix); + break; + case MISDN_GEN_CRYPT_KEYS: GET_GEN_STRING(CRYPT_KEYS, crypt_keys); + break; + + default: *buf = 0; + break; + } + + misdn_cfg_unlock(); +} + +int misdn_cfg_get_next_port (int port) { + + misdn_cfg_lock(); + + for (port++; port <= max_ports; port++) { + if (port_cfg[port]) { + misdn_cfg_unlock(); + return port; + } + } + + misdn_cfg_unlock(); + + return -1; +} + +int misdn_cfg_get_next_port_spin (int port) { + + int ret = misdn_cfg_get_next_port(port); + + if (ret > 0) + return ret; + + return misdn_cfg_get_next_port(0); +} + +#define PARSE_GEN_INT(item) ({ \ + if (!strcasecmp(v->name, #item)) { \ + int temp; \ + if (!sscanf(v->value, "%d", &temp)) { \ + ast_log(LOG_WARNING, "Value \"%s\" for \"" #item "\" (generals section) invalid or out of range! Please edit your misdn.conf and then do a \"misdn reload\".\n", v->value); \ + } else { \ + general_cfg->item = (int *)malloc(sizeof(int)); \ + memcpy(general_cfg->item, &temp, sizeof(int)); \ + } \ + continue; \ + } \ + }) \ + +#define PARSE_GEN_BOOL(item) ({ \ + if (!strcasecmp(v->name, #item)) { \ + general_cfg->item = (int *)malloc(sizeof(int)); \ + *(general_cfg->item) = ast_true(v->value)?1:0; \ + continue; \ + } \ + }) + +#define PARSE_GEN_STR(item) ({ \ + if (!strcasecmp(v->name, #item)) { \ + int l = strlen(v->value); \ + if (l) { \ + general_cfg->item = (char *)calloc(l+1, sizeof(char)); \ + strncpy(general_cfg->item,v->value, l); \ + } \ + continue; \ + } \ + }) + +static void build_general_config(struct ast_variable *v) { + + if (!v) + return; + + for (; v; v = v->next) { + + PARSE_GEN_INT(debug); + PARSE_GEN_STR(tracefile); + PARSE_GEN_BOOL(trace_calls); + PARSE_GEN_STR(trace_dir); + PARSE_GEN_BOOL(bridging); + PARSE_GEN_BOOL(stop_tone_after_first_digit); + PARSE_GEN_BOOL(append_digits2exten); + PARSE_GEN_BOOL(l1_info_ok); + PARSE_GEN_BOOL(clear_l3); + PARSE_GEN_BOOL(dynamic_crypt); + PARSE_GEN_STR(crypt_prefix); + PARSE_GEN_STR(crypt_keys); + + } +} + +#define PARSE_CFG_HYBRID(item, def) ({ \ + if (!strcasecmp(v->name, #item)) { \ + new->item = (int *)malloc(sizeof(int)); \ + if (!sscanf(v->value, "%d", new->item)) { \ + if (ast_true(v->value)) \ + *new->item = def; \ + else \ + *new->item = 0; \ + } \ + continue; \ + } \ + }) \ + +#define PARSE_CFG_INT(item) ({ \ + if (!strcasecmp(v->name, #item)) { \ + new->item = (int *)malloc(sizeof(int)); \ + if (!sscanf(v->value, "%d", new->item)) { \ + ast_log(LOG_WARNING, "Value \"%s\" for \"" #item "\" of group \"%s\" invalid or out of range! Please edit your misdn.conf and then do a \"misdn reload\".\n", v->value, cat); \ + free(new->item); \ + new->item = NULL; \ + } \ + continue; \ + } \ + }) \ + +#define PARSE_CFG_BOOL(item) ({ \ + if (!strcasecmp(v->name, #item)) { \ + new->item = (int *)malloc(sizeof(int)); \ + *(new->item) = ast_true(v->value)?1:0; \ + continue; \ + } \ + }) + +#define PARSE_CFG_STR(item) ({ \ + if (!strcasecmp(v->name, #item)) { \ + int l = strlen(v->value); \ + if (l) { \ + new->item = (char *)calloc(l+1, sizeof(char)); \ + strncpy(new->item,v->value,l); \ + } \ + continue; \ + } \ + }) + +static void build_port_config(struct ast_variable *v, char *cat) { + if (!v || !cat) + return; + + int cfg_for_ports[max_ports + 1]; + int i = 0; + for (; i < (max_ports + 1); i++) { + cfg_for_ports[i] = 0; + } + + /* we store the default config at position 0 */ + if (!strcasecmp(cat, "default")) { + cfg_for_ports[0] = 1; + } + + struct port_config* new = (struct port_config *)calloc(1, sizeof(struct port_config)); + + { + int l = strlen(cat); + new->name = (char *)calloc(l+1, sizeof(char)); + strncpy(new->name, cat, l); + } + + for (; v; v=v->next) { + if (!strcasecmp(v->name, "ports")) { + /* TODO check for value not beeing set, like PORTS= */ + char *iter; + char *value = v->value; + while ((iter = strchr(value, ',')) != NULL) { + *iter = 0; + /* strip spaces */ + while (*value && *value == ' ') { + value++; + } + /* TODO check for char not 0-9 */ + + if (*value){ + int p = atoi(value); + if (p <= max_ports && p > 0) { + cfg_for_ports[p] = 1; + if (strstr(value, "ptp")) + ptp[p] = 1; + } else + ast_log(LOG_WARNING, "Port value \"%s\" of group %s invalid or out of range! Please edit your misdn.conf and then do a \"misdn reload\".\n", value, cat); + value = ++iter; + } + } + /* the remaining or the only one */ + /* strip spaces */ + while (*value && *value == ' ') { + value++; + } + /* TODO check for char not 0-9 */ + if (*value) { + int p = atoi(value); + if (p <= max_ports && p > 0) { + cfg_for_ports[p] = 1; + if (strstr(value, "ptp")) + ptp[p] = 1; + } else + ast_log(LOG_WARNING, "Port value \"%s\" of group %s invalid or out of range! Please edit your misdn.conf and then do a \"misdn reload\".\n", value, cat); + } + continue; + } + PARSE_CFG_STR(context); + PARSE_CFG_INT(dialplan); + PARSE_CFG_STR(nationalprefix); + PARSE_CFG_STR(internationalprefix); + PARSE_CFG_STR(language); + if (!strcasecmp(v->name, "presentation")) { + if (v->value && strlen(v->value)) { + new->pres = (int *)malloc(sizeof(int)); + if (!strcasecmp(v->value, "allowed")) { + *(new->pres) = 1; + } + /* TODO: i assume if it is not "allowed", it is "not_screened" */ + else { + *(new->pres) = 0; + } + } + continue; + } + PARSE_CFG_INT(rxgain); + PARSE_CFG_INT(txgain); + PARSE_CFG_BOOL(te_choose_channel); + PARSE_CFG_BOOL(immediate); + PARSE_CFG_BOOL(always_immediate); + PARSE_CFG_BOOL(hold_allowed); + PARSE_CFG_BOOL(early_bconnect); + PARSE_CFG_BOOL(use_callingpres); + PARSE_CFG_HYBRID(echocancel, DEF_ECHOCANCEL); + PARSE_CFG_BOOL(echocancelwhenbridged); + PARSE_CFG_HYBRID(echotraining, DEF_ECHOTRAINING); + PARSE_CFG_STR(callerid); + PARSE_CFG_STR(method); + if (!strcasecmp(v->name, "msns")) { + /* TODO check for value not beeing set, like msns= */ + char *iter; + char *value = v->value; + + while ((iter = strchr(value, ',')) != NULL) { + *iter = 0; + /* strip spaces */ + while (*value && *value == ' ') { + value++; + } + /* TODO check for char not 0-9 */ + if (*value){ + int l = strlen(value); + if (l) { + struct msn_list *ml = (struct msn_list *)calloc(1, sizeof(struct msn_list)); + ml->msn = (char *)calloc(l+1, sizeof(char)); + strncpy(ml->msn,value,l); + ml->next = new->msn_list; + new->msn_list = ml; + } + value = ++iter; + } + } + /* the remaining or the only one */ + /* strip spaces */ + while (*value && *value == ' ') { + value++; + } + /* TODO check for char not 0-9 */ + if (*value) { + int l = strlen(value); + if (l) { + struct msn_list *ml = (struct msn_list *)calloc(1, sizeof(struct msn_list)); + ml->msn = (char *)calloc(l+1, sizeof(char)); + strncpy(ml->msn,value,l); + ml->next = new->msn_list; + new->msn_list = ml; + } + } + continue; + } + if (!strcasecmp(v->name, "callgroup")) { + new->callgroup = (ast_group_t *)malloc(sizeof(ast_group_t)); + *(new->callgroup)=ast_get_group(v->value); + continue; + } + if (!strcasecmp(v->name, "pickupgroup")) { + new->pickupgroup = (ast_group_t *)malloc(sizeof(ast_group_t)); + *(new->pickupgroup)=ast_get_group(v->value); + continue; + } + } + /* store the new config in our array of port configs */ + for (i = 0; i < (max_ports + 1); i++) { + if (cfg_for_ports[i]) + port_cfg[i] = new; + } +} + + +static void fill_defaults (void) { + + /* general defaults */ + if (!general_cfg->debug) + general_cfg->debug = (int*)calloc(1, sizeof(int)); + if (!general_cfg->trace_calls) + general_cfg->trace_calls = (int*)calloc(1, sizeof(int)); + if (!general_cfg->trace_dir) { + general_cfg->trace_dir = (char *)malloc(10 * sizeof(char)); + sprintf(general_cfg->trace_dir, "/var/log/"); + } + if (!general_cfg->bridging) { + general_cfg->bridging = (int*)malloc(sizeof(int)); + *general_cfg->bridging = 1; + } + if (!general_cfg->stop_tone_after_first_digit) { + general_cfg->stop_tone_after_first_digit = (int*)malloc(sizeof(int)); + *general_cfg->stop_tone_after_first_digit = 1; + } + if (!general_cfg->append_digits2exten) { + general_cfg->append_digits2exten = (int*)malloc(sizeof(int)); + *general_cfg->append_digits2exten = 1; + } + if (!general_cfg->l1_info_ok) { + general_cfg->l1_info_ok = (int*)malloc(sizeof(int)); + *general_cfg->l1_info_ok = 1; + } + if (!general_cfg->clear_l3) + general_cfg->clear_l3 =(int*)calloc(1, sizeof(int)); + if (!general_cfg->dynamic_crypt) + general_cfg->dynamic_crypt = (int*)calloc(1, sizeof(int)); + + /* defaults for default port config */ + if (!port_cfg[0]) + port_cfg[0] = (struct port_config*)calloc(1, sizeof(struct port_config)); + if (!port_cfg[0]->name) { + port_cfg[0]->name = (char *)malloc(8 * sizeof(char)); + sprintf(port_cfg[0]->name, "default"); + } + if (!port_cfg[0]->rxgain) + port_cfg[0]->rxgain = (int *)calloc(1, sizeof(int)); + if (!port_cfg[0]->txgain) + port_cfg[0]->txgain = (int *)calloc(1, sizeof(int)); + if (!port_cfg[0]->te_choose_channel) + port_cfg[0]->te_choose_channel = (int *)calloc(1, sizeof(int)); + if (!port_cfg[0]->context) { + port_cfg[0]->context = (char *)malloc(8 * sizeof(char)); + sprintf(port_cfg[0]->context, "default"); + } + if (!port_cfg[0]->language) { + port_cfg[0]->language = (char *)malloc(3 * sizeof(char)); + sprintf(port_cfg[0]->language, "en"); + } + if (!port_cfg[0]->callerid) + port_cfg[0]->callerid = (char *)calloc(1, sizeof(char)); + if (!port_cfg[0]->method) { + port_cfg[0]->method = (char *)malloc(9 * sizeof(char)); + sprintf(port_cfg[0]->method, "standard"); + } + if (!port_cfg[0]->dialplan) + port_cfg[0]->dialplan = (int *)calloc(1, sizeof(int)); + if (!port_cfg[0]->nationalprefix) { + port_cfg[0]->nationalprefix = (char *)malloc(2 * sizeof(char)); + sprintf(port_cfg[0]->nationalprefix, "0"); + } + if (!port_cfg[0]->internationalprefix) { + port_cfg[0]->internationalprefix = (char *)malloc(3 * sizeof(char)); + sprintf(port_cfg[0]->internationalprefix, "00"); + } + if (!port_cfg[0]->pres) { + port_cfg[0]->pres = (int *)malloc(sizeof(int)); + *port_cfg[0]->pres = 1; + } + if (!port_cfg[0]->always_immediate) + port_cfg[0]->always_immediate = (int *)calloc(1, sizeof(int)); + if (!port_cfg[0]->immediate) + port_cfg[0]->immediate = (int *)calloc(1, sizeof(int)); + if (!port_cfg[0]->hold_allowed) + port_cfg[0]->hold_allowed = (int *)calloc(1, sizeof(int)); + if (!port_cfg[0]->early_bconnect) { + port_cfg[0]->early_bconnect = (int *)malloc(sizeof(int)); + *port_cfg[0]->early_bconnect = 1; + } + if (!port_cfg[0]->echocancel) + port_cfg[0]->echocancel=(int *)calloc(1, sizeof(int)); + if (!port_cfg[0]->echocancelwhenbridged) + port_cfg[0]->echocancelwhenbridged=(int *)calloc(1, sizeof(int)); + if (!port_cfg[0]->echotraining) { + port_cfg[0]->echotraining=(int *)malloc(sizeof(int)); + *port_cfg[0]->echotraining = 1; + } + if (!port_cfg[0]->use_callingpres) { + port_cfg[0]->use_callingpres = (int *)malloc(sizeof(int)); + *port_cfg[0]->use_callingpres = 1; + } + if (!port_cfg[0]->msn_list) { + port_cfg[0]->msn_list = (struct msn_list *)malloc(sizeof(struct msn_list)); + port_cfg[0]->msn_list->next = NULL; + port_cfg[0]->msn_list->msn = (char *)calloc(2, sizeof(char)); + *(port_cfg[0]->msn_list->msn) = '*'; + } +} + +void misdn_cfg_reload (void) { + misdn_cfg_init (0); +} + +void misdn_cfg_destroy (void) { + + misdn_cfg_lock(); + + free_port_cfg(); + free_general_cfg(); + + free(port_cfg); + free(general_cfg); + free(ptp); + + misdn_cfg_unlock(); + ast_mutex_destroy(&config_mutex); +} + +void misdn_cfg_init (int this_max_ports) +{ + char config[]="misdn.conf"; + + struct ast_config *cfg; + cfg = AST_LOAD_CFG(config); + if (!cfg) { + ast_log(LOG_WARNING,"no misdn.conf ?\n"); + return; + } + + misdn_cfg_lock(); + + if (this_max_ports) { + /* this is the first run */ + max_ports = this_max_ports; + port_cfg = (struct port_config **)calloc(max_ports + 1, sizeof(struct port_config *)); + general_cfg = (struct general_config*)calloc(1, sizeof(struct general_config)); + ptp = (int *)calloc(max_ports + 1, sizeof(int)); + } + else { + free_port_cfg(); + free_general_cfg(); + port_cfg = memset(port_cfg, 0, sizeof(struct port_config *) * (max_ports + 1)); + general_cfg = memset(general_cfg, 0, sizeof(struct general_config)); + ptp = memset(ptp, 0, sizeof(int) * (max_ports + 1)); + } + + char *cat; + cat = ast_category_browse(cfg, NULL); + + while(cat) { + struct ast_variable *v=ast_variable_browse(cfg,cat); + if (!strcasecmp(cat,"general")) { + build_general_config (v); + } else { + build_port_config (v, cat); + } + cat=ast_category_browse(cfg,cat); + } + + fill_defaults(); + + misdn_cfg_unlock(); + + AST_DESTROY_CFG(cfg); +} diff -u -N -r ast_clean/channels/Makefile ast_misdn/channels/Makefile --- ast_clean/channels/Makefile 2005-10-05 02:17:57.000000000 +0200 +++ ast_misdn/channels/Makefile 2005-10-17 19:31:23.000000000 +0200 @@ -69,6 +69,10 @@ CHANNEL_LIBS+=chan_h323.so endif +ifneq ($(wildcard misdn/chan_misdn_lib.a),) + CHANNEL_LIBS+=chan_misdn.so +endif + CFLAGS+=-Wno-missing-prototypes -Wno-missing-declarations ifneq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/include/alsa/asoundlib.h),) @@ -146,6 +150,10 @@ include h323/Makefile.ast endif +ifneq ($(wildcard misdn/Makefile.ast),) + include misdn/Makefile.ast +endif + gentone: gentone.c $(HOST_CC) -o gentone gentone.c -lm @@ -205,6 +213,12 @@ $(CC) $(SOLINK) -o $@ $< h323/libchanh323.a $(CHANH323LIB) -L$(PWLIBDIR)/lib $(PTLIB) -L$(OPENH323DIR)/lib $(H323LIB) -L/usr/lib -lcrypto -lssl -lexpat endif +chan_misdn.so: chan_misdn.o chan_misdn_config.o misdn/chan_misdn_lib.a + $(CC) -shared -Xlinker -x -o $@ $^ $(MISDNUSER)/i4lnet/libisdnnet.a $(MISDNUSER)/lib/libmISDN.a + +chan_misdn_config.o: chan_misdn_config.c + $(CC) $(CFLAGS) -c chan_misdn_config.c + #chan_modem.so : chan_modem.o # $(CC) -rdynamic -shared -Xlinker -x -o $@ $< diff -u -N -r ast_clean/channels/misdn/chan_misdn_config.h ast_misdn/channels/misdn/chan_misdn_config.h --- ast_clean/channels/misdn/chan_misdn_config.h 1970-01-01 01:00:00.000000000 +0100 +++ ast_misdn/channels/misdn/chan_misdn_config.h 2005-10-17 19:31:23.000000000 +0200 @@ -0,0 +1,97 @@ +/* + * Chan_Misdn -- Channel Driver for Asterisk + * + * Interface to mISDN + * + * Copyright (C) 2004, Christian Richter + * + * Christian Richter + * + * This program is free software, distributed under the terms of + * the GNU General Public License + */ + + + +#ifndef CHAN_MISDN_CONFIG_H +#define CHAN_MISDN_CONFIG_H + +#define BUFFERSIZE 512 + +enum misdn_cfg_elements { + + /* port config items */ + MISDN_CFG_FIRST = 0, + MISDN_CFG_PTP, /* int (bool) */ + MISDN_CFG_GROUPNAME, /* char[] */ + MISDN_CFG_RXGAIN, /* int */ + MISDN_CFG_TXGAIN, /* int */ + MISDN_CFG_TE_CHOOSE_CHANNEL, /* int (bool) */ + MISDN_CFG_CONTEXT, /* char[] */ + MISDN_CFG_LANGUAGE, /* char[] */ + MISDN_CFG_CALLERID, /* char[] */ + MISDN_CFG_METHOD, /* char[] */ + MISDN_CFG_DIALPLAN, /* int */ + MISDN_CFG_NATPREFIX, /* char[] */ + MISDN_CFG_INTERNATPREFIX, /* char[] */ + MISDN_CFG_PRES, /* int (bool) */ + MISDN_CFG_ALWAYS_IMMEDIATE, /* int (bool) */ + MISDN_CFG_IMMEDIATE, /* int (bool) */ + MISDN_CFG_HOLD_ALLOWED, /* int (bool) */ + MISDN_CFG_EARLY_BCONNECT, /* int (bool) */ + MISDN_CFG_USE_CALLINGPRES, /* int (bool) */ + MISDN_CFG_ECHOCANCEL, /* int */ + MISDN_CFG_ECHOCANCELWHENBRIDGED, /* int (bool) */ + MISDN_CFG_ECHOTRAINING, /* int (bool) */ + MISDN_CFG_CALLGROUP, /* ast_group_t */ + MISDN_CFG_PICKUPGROUP, /* ast_group_t */ + MISDN_CFG_MSNS, /* char[] */ + MISDN_CFG_LAST, + + /* general config items */ + MISDN_GEN_FIRST, + MISDN_GEN_DEBUG, /* int */ + MISDN_GEN_TRACEFILE, /* char[] */ + MISDN_GEN_TRACE_CALLS, /* int (bool) */ + MISDN_GEN_TRACE_DIR, /* char[] */ + MISDN_GEN_BRIDGING, /* int (bool) */ + MISDN_GEN_STOP_TONE, /* int (bool) */ + MISDN_GEN_APPEND_DIGITS2EXTEN, /* int (bool) */ + MISDN_GEN_L1_INFO_OK, /* int (bool) */ + MISDN_GEN_CLEAR_L3, /* int (bool) */ + MISDN_GEN_DYNAMIC_CRYPT, /* int (bool) */ + MISDN_GEN_CRYPT_PREFIX, /* char[] */ + MISDN_GEN_CRYPT_KEYS, /* char[] */ + MISDN_GEN_LAST +}; + +enum misdn_cfg_method { + METHOD_STANDARD = 0, + METHOD_ROUND_ROBIN +}; + +/* you must call misdn_cfg_init before any other function of this header file */ +void misdn_cfg_init(int max_ports); +void misdn_cfg_reload(void); +void misdn_cfg_destroy(void); + +/* if you requst a general config element, the port value is ignored. if the requested + * value is not available, or the buffer is too small, the buffer will be nulled (in + * case of a char* only its first byte will be nulled). */ +void misdn_cfg_get(int port, enum misdn_cfg_elements elem, void* buf, int bufsize); + +/* fills the buffer with a ',' separated list of all active ports */ +void misdn_cfg_get_ports_string(char *ports); + +/* fills the buffer with a nice printable string representation of the config element */ +void misdn_cfg_get_config_string(int port, enum misdn_cfg_elements elem, char* buf, int bufsize); + +/* returns the next available port number. returns -1 if the last one was reached. */ +int misdn_cfg_get_next_port(int port); +int misdn_cfg_get_next_port_spin(int port); + +int misdn_cfg_is_msn_valid(int port, char* msn); +int misdn_cfg_is_port_valid(int port); +int misdn_cfg_is_group_method(char *group, enum misdn_cfg_method meth); + +#endif diff -u -N -r ast_clean/channels/misdn/ie.c ast_misdn/channels/misdn/ie.c --- ast_clean/channels/misdn/ie.c 1970-01-01 01:00:00.000000000 +0100 +++ ast_misdn/channels/misdn/ie.c 2005-10-17 19:31:23.000000000 +0200 @@ -0,0 +1,1608 @@ + +/* + * Chan_Misdn -- Channel Driver for Asterisk + * + * Interface to mISDN + * + * Copyright (C) 2005, Christian Richter + * + * Christian Richter + * + * heaviliy patched from jollys ie.cpp, jolly gave me ALL + * rights for this code, i can even have my own copyright on it. + * + * This program is free software, distributed under the terms of + * the GNU General Public License + */ + +/* + the pointer of enc_ie_* always points to the IE itself + if qi is not NULL (TE-mode), offset is set +*/ + +#include "isdn_lib.h" + +#define CENTREX_FAC 0x88 +#define CENTREX_ID 0xa1 + +#define MISDN_IE_DEBG 0 + +/* support stuff */ +static void strnncpy(unsigned char *dest, unsigned char *src, int len, int dst_len) +{ + if (len > dst_len-1) + len = dst_len-1; + strncpy((char *)dest, (char *)src, len); + dest[len] = '\0'; +} + + +/* IE_COMPLETE */ +void enc_ie_complete(unsigned char **ntmode, msg_t *msg, int complete, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + + if (complete<0 || complete>1) + { + printf("%s: ERROR: complete(%d) is out of range.\n", __FUNCTION__, complete); + return; + } + + if (complete) + if (MISDN_IE_DEBG) printf(" complete=%d\n", complete); + + if (complete) + { + p = msg_put(msg, 1); + if (nt) + { + *ntmode = p; + } else + qi->sending_complete = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_COMPLETE; + } +} + +void dec_ie_complete(unsigned char *p, Q931_info_t *qi, int *complete, int nt, struct misdn_bchannel *bc) +{ + *complete = 0; + if (!nt) + { + if (qi->sending_complete) + *complete = 1; + } else + if (p) + *complete = 1; + + if (*complete) + if (MISDN_IE_DEBG) printf(" complete=%d\n", *complete); +} + + +/* IE_BEARER */ +void enc_ie_bearer(unsigned char **ntmode, msg_t *msg, int coding, int capability, int mode, int rate, int multi, int user, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + if (coding<0 || coding>3) + { + printf("%s: ERROR: coding(%d) is out of range.\n", __FUNCTION__, coding); + return; + } + if (capability<0 || capability>31) + { + printf("%s: ERROR: capability(%d) is out of range.\n", __FUNCTION__, capability); + return; + } + if (mode<0 || mode>3) + { + printf("%s: ERROR: mode(%d) is out of range.\n", __FUNCTION__, mode); + return; + } + if (rate<0 || rate>31) + { + printf("%s: ERROR: rate(%d) is out of range.\n", __FUNCTION__, rate); + return; + } + if (multi>127) + { + printf("%s: ERROR: multi(%d) is out of range.\n", __FUNCTION__, multi); + return; + } + if (user>31) + { + printf("%s: ERROR: user L1(%d) is out of range.\n", __FUNCTION__, rate); + return; + } + if (rate!=24 && multi>=0) + { + printf("%s: WARNING: multi(%d) is only possible if rate(%d) would be 24.\n", __FUNCTION__, multi, rate); + multi = -1; + } + + if (MISDN_IE_DEBG) printf(" coding=%d capability=%d mode=%d rate=%d multi=%d user=%d\n", coding, capability, mode, rate, multi, user); + + l = 2 + (multi>=0) + (user>=0); + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->bearer_capability = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_BEARER; + p[1] = l; + p[2] = 0x80 + (coding<<5) + capability; + p[3] = 0x80 + (mode<<5) + rate; + if (multi >= 0) + p[4] = 0x80 + multi; + if (user >= 0) + p[4+(multi>=0)] = 0xa0 + user; +} + +void dec_ie_bearer(unsigned char *p, Q931_info_t *qi, int *coding, int *capability, int *mode, int *rate, int *multi, int *user, + int *async, int *urate, int *stopbits, int *dbits, int *parity, int nt, struct misdn_bchannel *bc) +{ + int octet; + *coding = -1; + *capability = -1; + *mode = -1; + *rate = -1; + *multi = -1; + *user = -1; + *async = -1; + *urate = -1; + *stopbits = -1; + *dbits = -1; + *parity = -1; + + if (!nt) + { + p = NULL; + if (qi->llc) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->llc + 1; + else if (qi->bearer_capability) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->bearer_capability + 1; + } + if (!p) + return; + if (p[0] < 2) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + *coding = (p[1]&0x60) >> 5; + *capability = p[1] & 0x1f; + octet = 2; + if (!(p[1] & 0x80)) + octet++; + + if (p[0] < octet) + goto done; + + *mode = (p[octet]&0x60) >> 5; + *rate = p[octet] & 0x1f; + + octet++; + + if (p[0] < octet) + goto done; + + if (*rate == 0x18) { + /* Rate multiplier only present if 64Kb/s base rate */ + *multi = p[octet++] & 0x7f; + } + + if (p[0] < octet) + goto done; + + /* Start L1 info */ + if ((p[octet] & 0x60) == 0x20) { + *user = p[octet] & 0x1f; + + if (p[0] <= octet) + goto done; + + if (p[octet++] & 0x80) + goto l2; + + *async = !!(p[octet] & 0x40); + /* 0x20 is inband negotiation */ + *urate = p[octet] & 0x1f; + + if (p[0] <= octet) + goto done; + + if (p[octet++] & 0x80) + goto l2; + + /* Ignore next byte for now: Intermediate rate, NIC, flow control */ + + if (p[0] <= octet) + goto done; + + if (p[octet++] & 0x80) + goto l2; + + /* And the next one. Header, multiframe, mode, assignor/ee, negotiation */ + + if (p[0] <= octet) + goto done; + + if (!p[octet++] & 0x80) + goto l2; + + /* Wheee. V.110 speed information */ + + *stopbits = (p[octet] & 0x60) >> 5; + *dbits = (p[octet] & 0x18) >> 3; + *parity = p[octet] & 7; + + octet++; + } + l2: /* Nobody seems to want the rest so we don't bother (yet) */ + done: + if (MISDN_IE_DEBG) printf(" coding=%d capability=%d mode=%d rate=%d multi=%d user=%d async=%d urate=%d stopbits=%d dbits=%d parity=%d\n", *coding, *capability, *mode, *rate, *multi, *user, *async, *urate, *stopbits, *dbits, *parity); +} + + +/* IE_CALL_ID */ +void enc_ie_call_id(unsigned char **ntmode, msg_t *msg, unsigned char *callid, int callid_len, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + char debug[25]; + int i; + + if (!callid || callid_len<=0) + { + return; + } + if (callid_len>8) + { + printf("%s: ERROR: callid_len(%d) is out of range.\n", __FUNCTION__, callid_len); + return; + } + + i = 0; + while(i < callid_len) + { + if (MISDN_IE_DEBG) printf(debug+(i*3), " %02x", callid[i]); + i++; + } + + if (MISDN_IE_DEBG) printf(" callid%s\n", debug); + + l = callid_len; + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->call_id = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_CALL_ID; + p[1] = l; + memcpy(p+2, callid, callid_len); +} + +void dec_ie_call_id(unsigned char *p, Q931_info_t *qi, unsigned char *callid, int *callid_len, int nt, struct misdn_bchannel *bc) +{ + char debug[25]; + int i; + + *callid_len = -1; + + if (!nt) + { + p = NULL; + if (qi->call_id) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->call_id + 1; + } + if (!p) + return; + if (p[0] > 8) + { + printf("%s: ERROR: IE too long (%d).\n", __FUNCTION__, p[0]); + return; + } + + *callid_len = p[0]; + memcpy(callid, p+1, *callid_len); + + i = 0; + while(i < *callid_len) + { + if (MISDN_IE_DEBG) printf(debug+(i*3), " %02x", callid[i]); + i++; + } + + if (MISDN_IE_DEBG) printf(" callid%s\n", debug); +} + + +/* IE_CALLED_PN */ +void enc_ie_called_pn(unsigned char **ntmode, msg_t *msg, int type, int plan, unsigned char *number, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + if (type<0 || type>7) + { + printf("%s: ERROR: type(%d) is out of range.\n", __FUNCTION__, type); + return; + } + if (plan<0 || plan>15) + { + printf("%s: ERROR: plan(%d) is out of range.\n", __FUNCTION__, plan); + return; + } + if (!number[0]) + { + printf("%s: ERROR: number is not given.\n", __FUNCTION__); + return; + } + + if (MISDN_IE_DEBG) printf(" type=%d plan=%d number='%s'\n", type, plan, number); + + l = 1+strlen((char *)number); + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->called_nr = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_CALLED_PN; + p[1] = l; + p[2] = 0x80 + (type<<4) + plan; + strncpy((char *)p+3, (char *)number, strlen((char *)number)); +} + +void dec_ie_called_pn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, unsigned char *number, int number_len, int nt, struct misdn_bchannel *bc) +{ + *type = -1; + *plan = -1; + *number = '\0'; + + if (!nt) + { + p = NULL; + if (qi->called_nr) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->called_nr + 1; + } + if (!p) + return; + if (p[0] < 2) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + *type = (p[1]&0x70) >> 4; + *plan = p[1] & 0xf; + strnncpy(number, p+2, p[0]-1, number_len); + + if (MISDN_IE_DEBG) printf(" type=%d plan=%d number='%s'\n", *type, *plan, number); +} + + +/* IE_CALLING_PN */ +void enc_ie_calling_pn(unsigned char **ntmode, msg_t *msg, int type, int plan, int present, int screen, unsigned char *number, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + if (type<0 || type>7) + { + printf("%s: ERROR: type(%d) is out of range.\n", __FUNCTION__, type); + return; + } + if (plan<0 || plan>15) + { + printf("%s: ERROR: plan(%d) is out of range.\n", __FUNCTION__, plan); + return; + } + if (present>3) + { + printf("%s: ERROR: present(%d) is out of range.\n", __FUNCTION__, present); + return; + } + if (present >= 0) if (screen<0 || screen>3) + { + printf("%s: ERROR: screen(%d) is out of range.\n", __FUNCTION__, screen); + return; + } + + if (MISDN_IE_DEBG) printf(" type=%d plan=%d present=%d screen=%d number='%s'\n", type, plan, present, screen, number); + + l = 1; + if (number) if (number[0]) + l += strlen((char *)number); + if (present >= 0) + l += 1; + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->calling_nr = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_CALLING_PN; + p[1] = l; + if (present >= 0) + { + p[2] = 0x00 + (type<<4) + plan; + p[3] = 0x80 + (present<<5) + screen; + if (number) if (number[0]) + strncpy((char *)p+4, (char *)number, strlen((char *)number)); + } else + { + p[2] = 0x80 + (type<<4) + plan; + if (number) if (number[0]) + strncpy((char *)p+3, (char *)number, strlen((char *)number)); + } +} + +void dec_ie_calling_pn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, int *present, int *screen, unsigned char *number, int number_len, int nt, struct misdn_bchannel *bc) +{ + *type = -1; + *plan = -1; + *present = -1; + *screen = -1; + *number = '\0'; + + if (!nt) + { + p = NULL; + if (qi->calling_nr) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->calling_nr + 1; + } + if (!p) + return; + if (p[0] < 1) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + *type = (p[1]&0x70) >> 4; + *plan = p[1] & 0xf; + if (!(p[1] & 0x80)) + { + if (p[0] < 2) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + *present = (p[2]&0x60) >> 5; + *screen = p[2] & 0x3; + strnncpy(number, p+3, p[0]-2, number_len); + } else + { + strnncpy(number, p+2, p[0]-1, number_len); + /* SPECIAL workarround for IBT software bug */ + /* if (number[0]==0x80) */ + /* strcpy((char *)number, (char *)number+1); */ + } + + if (MISDN_IE_DEBG) printf(" type=%d plan=%d present=%d screen=%d number='%s'\n", *type, *plan, *present, *screen, number); +} + + +/* IE_CONNECTED_PN */ +void enc_ie_connected_pn(unsigned char **ntmode, msg_t *msg, int type, int plan, int present, int screen, unsigned char *number, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + if (type<0 || type>7) + { + printf("%s: ERROR: type(%d) is out of range.\n", __FUNCTION__, type); + return; + } + if (plan<0 || plan>15) + { + printf("%s: ERROR: plan(%d) is out of range.\n", __FUNCTION__, plan); + return; + } + if (present>3) + { + printf("%s: ERROR: present(%d) is out of range.\n", __FUNCTION__, present); + return; + } + if (present >= 0) if (screen<0 || screen>3) + { + printf("%s: ERROR: screen(%d) is out of range.\n", __FUNCTION__, screen); + return; + } + + if (MISDN_IE_DEBG) printf(" type=%d plan=%d present=%d screen=%d number='%s'\n", type, plan, present, screen, number); + + l = 1; + if (number) if (number[0]) + l += strlen((char *)number); + if (present >= 0) + l += 1; + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->connected_nr = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_CONNECT_PN; + p[1] = l; + if (present >= 0) + { + p[2] = 0x00 + (type<<4) + plan; + p[3] = 0x80 + (present<<5) + screen; + if (number) if (number[0]) + strncpy((char *)p+4, (char *)number, strlen((char *)number)); + } else + { + p[2] = 0x80 + (type<<4) + plan; + if (number) if (number[0]) + strncpy((char *)p+3, (char *)number, strlen((char *)number)); + } +} + +void dec_ie_connected_pn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, int *present, int *screen, unsigned char *number, int number_len, int nt, struct misdn_bchannel *bc) +{ + *type = -1; + *plan = -1; + *present = -1; + *screen = -1; + *number = '\0'; + + if (!nt) + { + p = NULL; + if (qi->connected_nr) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->connected_nr + 1; + } + if (!p) + return; + if (p[0] < 1) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + *type = (p[1]&0x70) >> 4; + *plan = p[1] & 0xf; + if (!(p[1] & 0x80)) + { + if (p[0] < 2) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + *present = (p[2]&0x60) >> 5; + *screen = p[2] & 0x3; + strnncpy(number, p+3, p[0]-2, number_len); + } else + { + strnncpy(number, p+2, p[0]-1, number_len); + } + + if (MISDN_IE_DEBG) printf(" type=%d plan=%d present=%d screen=%d number='%s'\n", *type, *plan, *present, *screen, number); +} + + +/* IE_CAUSE */ +void enc_ie_cause(unsigned char **ntmode, msg_t *msg, int location, int cause, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + if (location<0 || location>7) + { + printf("%s: ERROR: location(%d) is out of range.\n", __FUNCTION__, location); + return; + } + if (cause<0 || cause>127) + { + printf("%s: ERROR: cause(%d) is out of range.\n", __FUNCTION__, cause); + return; + } + + if (MISDN_IE_DEBG) printf(" location=%d cause=%d\n", location, cause); + + l = 2; + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->cause = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_CAUSE; + p[1] = l; + p[2] = 0x80 + location; + p[3] = 0x80 + cause; +} +void enc_ie_cause_standalone(unsigned char **ntmode, msg_t *msg, int location, int cause, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p = msg_put(msg, 4); + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + if (ntmode) + *ntmode = p+1; + else + qi->cause = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_CAUSE; + p[1] = 2; + p[2] = 0x80 + location; + p[3] = 0x80 + cause; +} + + +void dec_ie_cause(unsigned char *p, Q931_info_t *qi, int *location, int *cause, int nt, struct misdn_bchannel *bc) +{ + *location = -1; + *cause = -1; + + if (!nt) + { + p = NULL; + if (qi->cause) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->cause + 1; + } + if (!p) + return; + if (p[0] < 2) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + *location = p[1] & 0x0f; + *cause = p[2] & 0x7f; + + if (MISDN_IE_DEBG) printf(" location=%d cause=%d\n", *location, *cause); +} + + +/* IE_CHANNEL_ID */ +void enc_ie_channel_id(unsigned char **ntmode, msg_t *msg, int exclusive, int channel, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + int pri = bc->stack->pri; + + if (exclusive<0 || exclusive>1) + { + printf("%s: ERROR: exclusive(%d) is out of range.\n", __FUNCTION__, exclusive); + return; + } + if ((channel<0 || channel>0xff) + || (!pri && (channel>2 && channel<0xff)) + || (pri && (channel>31 && channel<0xff)) + || (pri && channel==16)) + { + printf("%s: ERROR: channel(%d) is out of range.\n", __FUNCTION__, channel); + return; + } + + /* if (MISDN_IE_DEBG) printf(" exclusive=%d channel=%d\n", exclusive, channel); */ + + + if (!pri) + { + /* BRI */ + l = 1; + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->channel_id = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_CHANNEL_ID; + p[1] = l; + if (channel == 0xff) + channel = 3; + p[2] = 0x80 + (exclusive<<3) + channel; + /* printf(" exclusive=%d channel=%d\n", exclusive, channel); */ + } else + { + /* PRI */ + if (channel == 0) /* no channel */ + return; /* IE not present */ +/* if (MISDN_IE_DEBG) printf("channel = %d\n", channel); */ + if (channel == 0xff) /* any channel */ + { + l = 1; + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->channel_id = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_CHANNEL_ID; + p[1] = l; + p[2] = 0x80 + 0x20 + 0x03; +/* if (MISDN_IE_DEBG) printf("%02x\n", p[2]); */ + return; /* end */ + } + l = 3; + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->channel_id = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_CHANNEL_ID; + p[1] = l; + p[2] = 0x80 + 0x20 + (exclusive<<3) + 0x01; + p[3] = 0x80 + 3; /* CCITT, Number, B-type */ + p[4] = 0x80 + channel; +/* if (MISDN_IE_DEBG) printf("%02x %02x %02x\n", p[2], p[3], p[4]); */ + } +} + +void dec_ie_channel_id(unsigned char *p, Q931_info_t *qi, int *exclusive, int *channel, int nt, struct misdn_bchannel *bc) +{ + int pri = bc->stack->pri; + + *exclusive = -1; + *channel = -1; + + if (!nt) + { + p = NULL; + if (qi->channel_id) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->channel_id + 1; + } + if (!p) + return; + if (p[0] < 1) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + if (p[1] & 0x40) + { + printf("%s: ERROR: refering to channels of other interfaces is not supported.\n", __FUNCTION__); + return; + } + if (p[1] & 0x04) + { + printf("%s: ERROR: using d-channel is not supported.\n", __FUNCTION__); + return; + } + + *exclusive = (p[1]&0x08) >> 3; + if (!pri) + { + /* BRI */ + if (p[1] & 0x20) + { + printf("%s: ERROR: extended channel ID with non PRI interface.\n", __FUNCTION__); + return; + } + *channel = p[1] & 0x03; + if (*channel == 3) + *channel = 0xff; + } else + { + /* PRI */ + if (p[0] < 1) + { + printf("%s: ERROR: IE too short for PRI (%d).\n", __FUNCTION__, p[0]); + return; + } + if (!(p[1] & 0x20)) + { + printf("%s: ERROR: basic channel ID with PRI interface.\n", __FUNCTION__); + return; + } + if ((p[1]&0x03) == 0x00) + { + /* no channel */ + *channel = 0; + return; + } + if ((p[1]&0x03) == 0x03) + { + /* any channel */ + *channel = 0xff; + return; + } + if (p[0] < 3) + { + printf("%s: ERROR: IE too short for PRI with channel(%d).\n", __FUNCTION__, p[0]); + return; + } + if (p[2] & 0x10) + { + printf("%s: ERROR: channel map not supported.\n", __FUNCTION__); + return; + } + *channel = p[3] & 0x7f; + if ( (*channel<1) | (*channel==16) | (*channel>31)) + { + printf("%s: ERROR: PRI interface channel out of range (%d).\n", __FUNCTION__, *channel); + return; + } +/* if (MISDN_IE_DEBG) printf("%02x %02x %02x\n", p[1], p[2], p[3]); */ + } + + if (MISDN_IE_DEBG) printf(" exclusive=%d channel=%d\n", *exclusive, *channel); +} + + +/* IE_DATE */ +void enc_ie_date(unsigned char **ntmode, msg_t *msg, time_t ti, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + struct tm *tm; + + tm = localtime(&ti); + if (!tm) + { + printf("%s: ERROR: gettimeofday() returned NULL.\n", __FUNCTION__); + return; + } + + if (MISDN_IE_DEBG) printf(" year=%d month=%d day=%d hour=%d minute=%d\n", tm->tm_year%100, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); + + l = 5; + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->date = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_DATE; + p[1] = l; + p[2] = tm->tm_year % 100; + p[3] = tm->tm_mon + 1; + p[4] = tm->tm_mday; + p[5] = tm->tm_hour; + p[6] = tm->tm_min; +} + + +/* IE_DISPLAY */ +void enc_ie_display(unsigned char **ntmode, msg_t *msg, unsigned char *display, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + if (!display[0]) + { + printf("%s: ERROR: display text not given.\n", __FUNCTION__); + return; + } + + if (strlen((char *)display) > 80) + { + printf("%s: WARNING: display text too long (max 80 chars), cutting.\n", __FUNCTION__); + display[80] = '\0'; + } + + /* if (MISDN_IE_DEBG) printf(" display='%s' (len=%d)\n", display, strlen((char *)display)); */ + + l = strlen((char *)display); + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->display = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_DISPLAY; + p[1] = l; + strncpy((char *)p+2, (char *)display, strlen((char *)display)); +} + +void dec_ie_display(unsigned char *p, Q931_info_t *qi, unsigned char *display, int display_len, int nt, struct misdn_bchannel *bc) +{ + *display = '\0'; + + if (!nt) + { + p = NULL; + if (qi->display) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->display + 1; + } + if (!p) + return; + if (p[0] < 1) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + strnncpy(display, p+1, p[0], display_len); + + if (MISDN_IE_DEBG) printf(" display='%s'\n", display); +} + + +/* IE_KEYPAD */ +void enc_ie_keypad(unsigned char **ntmode, msg_t *msg, unsigned char *keypad, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + if (!keypad[0]) + { + printf("%s: ERROR: keypad info not given.\n", __FUNCTION__); + return; + } + + if (MISDN_IE_DEBG) printf(" keypad='%s'\n", keypad); + + l = strlen((char *)keypad); + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->keypad = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_KEYPAD; + p[1] = l; + strncpy((char *)p+2, (char *)keypad, strlen((char *)keypad)); +} + +void dec_ie_keypad(unsigned char *p, Q931_info_t *qi, unsigned char *keypad, int keypad_len, int nt, struct misdn_bchannel *bc) +{ + *keypad = '\0'; + + if (!nt) + { + p = NULL; + if (qi->keypad) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->keypad + 1; + } + if (!p) + return; + if (p[0] < 1) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + strnncpy(keypad, p+1, p[0], keypad_len); + + if (MISDN_IE_DEBG) printf(" keypad='%s'\n", keypad); +} + + +/* IE_NOTIFY */ +void enc_ie_notify(unsigned char **ntmode, msg_t *msg, int notify, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + if (notify<0 || notify>0x7f) + { + printf("%s: ERROR: notify(%d) is out of range.\n", __FUNCTION__, notify); + return; + } + + if (MISDN_IE_DEBG) printf(" notify=%d\n", notify); + + l = 1; + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->notify = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_NOTIFY; + p[1] = l; + p[2] = 0x80 + notify; +} + +void dec_ie_notify(unsigned char *p, Q931_info_t *qi, int *notify, int nt, struct misdn_bchannel *bc) +{ + *notify = -1; + + if (!nt) + { + p = NULL; + if (qi->notify) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->notify + 1; + } + if (!p) + return; + if (p[0] < 1) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + *notify = p[1] & 0x7f; + + if (MISDN_IE_DEBG) printf(" notify=%d\n", *notify); +} + + +/* IE_PROGRESS */ +void enc_ie_progress(unsigned char **ntmode, msg_t *msg, int coding, int location, int progress, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + if (coding<0 || coding>0x03) + { + printf("%s: ERROR: coding(%d) is out of range.\n", __FUNCTION__, coding); + return; + } + if (location<0 || location>0x0f) + { + printf("%s: ERROR: location(%d) is out of range.\n", __FUNCTION__, location); + return; + } + if (progress<0 || progress>0x7f) + { + printf("%s: ERROR: progress(%d) is out of range.\n", __FUNCTION__, progress); + return; + } + + if (MISDN_IE_DEBG) printf(" coding=%d location=%d progress=%d\n", coding, location, progress); + + l = 2; + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->progress = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_PROGRESS; + p[1] = l; + p[2] = 0x80 + (coding<<5) + location; + p[3] = 0x80 + progress; +} + +void dec_ie_progress(unsigned char *p, Q931_info_t *qi, int *coding, int *location, int *progress, int nt, struct misdn_bchannel *bc) +{ + *coding = -1; + *location = -1; + //*progress = -1; + *progress = 0; + + if (!nt) + { + p = NULL; + if (qi->progress) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->progress + 1; + } + if (!p) + return; + if (p[0] < 1) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + *coding = (p[1]&0x60) >> 5; + *location = p[1] & 0x0f; + *progress = p[2] & 0x7f; + + if (MISDN_IE_DEBG) printf(" coding=%d location=%d progress=%d\n", *coding, *location, *progress); +} + + +/* IE_REDIR_NR (redirecting = during MT_SETUP) */ +void enc_ie_redir_nr(unsigned char **ntmode, msg_t *msg, int type, int plan, int present, int screen, int reason, unsigned char *number, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + if (type<0 || type>7) + { + printf("%s: ERROR: type(%d) is out of range.\n", __FUNCTION__, type); + return; + } + if (plan<0 || plan>15) + { + printf("%s: ERROR: plan(%d) is out of range.\n", __FUNCTION__, plan); + return; + } + if (present > 3) + { + printf("%s: ERROR: present(%d) is out of range.\n", __FUNCTION__, present); + return; + } + if (present >= 0) if (screen<0 || screen>3) + { + printf("%s: ERROR: screen(%d) is out of range.\n", __FUNCTION__, screen); + return; + } + if (reason > 0x0f) + { + printf("%s: ERROR: reason(%d) is out of range.\n", __FUNCTION__, reason); + return; + } + + if (MISDN_IE_DEBG) printf(" type=%d plan=%d present=%d screen=%d readon=%d number='%s'\n", type, plan, present, screen, reason, number); + + l = 1; + if (number) + l += strlen((char *)number); + if (present >= 0) + { + l += 1; + if (reason >= 0) + l += 1; + } + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->redirect_nr = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_REDIR_NR; + p[1] = l; + if (present >= 0) + { + if (reason >= 0) + { + p[2] = 0x00 + (type<<4) + plan; + p[3] = 0x00 + (present<<5) + screen; + p[4] = 0x80 + reason; + if (number) + strncpy((char *)p+5, (char *)number, strlen((char *)number)); + } else + { + p[2] = 0x00 + (type<<4) + plan; + p[3] = 0x80 + (present<<5) + screen; + if (number) + strncpy((char *)p+4, (char *)number, strlen((char *)number)); + } + } else + { + p[2] = 0x80 + (type<<4) + plan; + if (number) if (number[0]) + strncpy((char *)p+3, (char *)number, strlen((char *)number)); + } +} + +void dec_ie_redir_nr(unsigned char *p, Q931_info_t *qi, int *type, int *plan, int *present, int *screen, int *reason, unsigned char *number, int number_len, int nt, struct misdn_bchannel *bc) +{ + *type = -1; + *plan = -1; + *present = -1; + *screen = -1; + *reason = -1; + *number = '\0'; + + if (!nt) + { + p = NULL; + if (qi->redirect_nr) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->redirect_nr + 1; + } + if (!p) + return; + if (p[0] < 1) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + *type = (p[1]&0x70) >> 4; + *plan = p[1] & 0xf; + if (!(p[1] & 0x80)) + { + *present = (p[2]&0x60) >> 5; + *screen = p[2] & 0x3; + if (!(p[2] & 0x80)) + { + *reason = p[3] & 0x0f; + strnncpy(number, p+4, p[0]-3, number_len); + } else + { + strnncpy(number, p+3, p[0]-2, number_len); + } + } else + { + strnncpy(number, p+2, p[0]-1, number_len); + } + + if (MISDN_IE_DEBG) printf(" type=%d plan=%d present=%d screen=%d reason=%d number='%s'\n", *type, *plan, *present, *screen, *reason, number); +} + + +/* IE_REDIR_DN (redirection = during MT_NOTIFY) */ +void enc_ie_redir_dn(unsigned char **ntmode, msg_t *msg, int type, int plan, int present, unsigned char *number, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; +/* Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); */ + int l; + + if (type<0 || type>7) + { + printf("%s: ERROR: type(%d) is out of range.\n", __FUNCTION__, type); + return; + } + if (plan<0 || plan>15) + { + printf("%s: ERROR: plan(%d) is out of range.\n", __FUNCTION__, plan); + return; + } + if (present > 3) + { + printf("%s: ERROR: present(%d) is out of range.\n", __FUNCTION__, present); + return; + } + + if (MISDN_IE_DEBG) printf(" type=%d plan=%d present=%d number='%s'\n", type, plan, present, number); + + l = 1; + if (number) + l += strlen((char *)number); + if (present >= 0) + l += 1; + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else +/* #warning REINSERT redir_dn, when included in te-mode */ + /*qi->redir_dn = p - (unsigned char *)qi - sizeof(Q931_info_t)*/; + p[0] = IE_REDIR_DN; + p[1] = l; + if (present >= 0) + { + p[2] = 0x00 + (type<<4) + plan; + p[3] = 0x80 + (present<<5); + if (number) + strncpy((char *)p+4, (char *)number, strlen((char *)number)); + } else + { + p[2] = 0x80 + (type<<4) + plan; + if (number) + strncpy((char *)p+3, (char *)number, strlen((char *)number)); + } +} + +void dec_ie_redir_dn(unsigned char *p, Q931_info_t *qi, int *type, int *plan, int *present, unsigned char *number, int number_len, int nt, struct misdn_bchannel *bc) +{ + *type = -1; + *plan = -1; + *present = -1; + *number = '\0'; + + if (!nt) + { + p = NULL; +/* #warning REINSERT redir_dn, when included in te-mode */ +/* if (qi->redir_dn) */ +/* p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->redir_dn + 1; */ + } + if (!p) + return; + if (p[0] < 1) + { + printf("%s: ERROR: IE too short (%d).\n", __FUNCTION__, p[0]); + return; + } + + *type = (p[1]&0x70) >> 4; + *plan = p[1] & 0xf; + if (!(p[1] & 0x80)) + { + *present = (p[2]&0x60) >> 5; + strnncpy(number, p+3, p[0]-2, number_len); + } else + { + strnncpy(number, p+2, p[0]-1, number_len); + } + + if (MISDN_IE_DEBG) printf(" type=%d plan=%d present=%d number='%s'\n", *type, *plan, *present, number); +} + + +/* IE_FACILITY */ +void enc_ie_facility(unsigned char **ntmode, msg_t *msg, unsigned char *facility, int facility_len, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + char debug[768]; + int i; + + if (!facility || facility_len<=0) + { + return; + } + + i = 0; + while(i < facility_len) + { + if (MISDN_IE_DEBG) printf(debug+(i*3), " %02x", facility[i]); + i++; + } + + if (MISDN_IE_DEBG) printf(" facility%s\n", debug); + + l = facility_len; + p = msg_put(msg, l+2); + if (nt) + *ntmode = p+1; + else + qi->facility = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_FACILITY; + p[1] = l; + memcpy(p+2, facility, facility_len); +} + +void dec_ie_facility(unsigned char *p, Q931_info_t *qi, unsigned char *facility, int *facility_len, int nt, struct misdn_bchannel *bc) +{ + int i; + + *facility_len = 0; + + if (!nt) + { + p = NULL; + if (qi->facility) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->facility + 1; + } + if (!p) + return; + + *facility_len = p[0]; + memcpy(facility, p+1, *facility_len); + + i = 0; + while(i < *facility_len) + { + cb_log(3, bc->stack->port, " %02x", facility[i]); + i++; + } + cb_log(3, bc->stack->port, " facility\n"); +} + + +/* facility for siemens CENTEX (known parts implemented only) */ +void enc_facility_centrex(unsigned char **ntmode, msg_t *msg, unsigned char *cnip, int setup, int nt, struct misdn_bchannel *bc) +{ + unsigned char centrex[256]; + int i = 0; + + if (!cnip) + return; + + /* centrex facility */ + centrex[i++] = CENTREX_FAC; + centrex[i++] = CENTREX_ID; + + /* cnip */ + if (strlen((char *)cnip) > 15) + { +/* if (options.deb & DEBUG_PORT) */ + if (MISDN_IE_DEBG) printf("%s: CNIP/CONP text too long (max 13 chars), cutting.\n", __FUNCTION__); + cnip[15] = '\0'; + } + /* dunno what the 8 bytes mean */ + if (setup) + { + centrex[i++] = 0x17; + centrex[i++] = 0x02; + centrex[i++] = 0x02; + centrex[i++] = 0x44; + centrex[i++] = 0x18; + centrex[i++] = 0x02; + centrex[i++] = 0x01; + centrex[i++] = 0x09; + } else + { + centrex[i++] = 0x18; + centrex[i++] = 0x02; + centrex[i++] = 0x02; + centrex[i++] = 0x81; + centrex[i++] = 0x09; + centrex[i++] = 0x02; + centrex[i++] = 0x01; + centrex[i++] = 0x0a; + } + + centrex[i++] = 0x80; + centrex[i++] = strlen((char *)cnip); + strcpy((char *)(¢rex[i]), (char *)cnip); + i += strlen((char *)cnip); + if (MISDN_IE_DEBG) printf(" cnip='%s'\n", cnip); + + /* encode facility */ + enc_ie_facility(ntmode, msg, centrex, i, nt , bc); +} + +void dec_facility_centrex(unsigned char *p, Q931_info_t *qi, unsigned char *cnip, int cnip_len, int nt, struct misdn_bchannel *bc) +{ + unsigned char centrex[256]; + char debug[768]; + int facility_len = 0; + int i = 0, j; + *cnip = '\0'; + + dec_ie_facility(p, qi, centrex, &facility_len, nt, bc); + if (facility_len >= 2) + { + if (centrex[i++] != CENTREX_FAC) + return; + if (centrex[i++] != CENTREX_ID) + return; + } + + /* loop sub IEs of facility */ + while(facility_len > i+1) + { + if (centrex[i+1]+i+1 > facility_len) + { + printf("%s: ERROR: short read of centrex facility.\n", __FUNCTION__); + return; + } + switch(centrex[i]) + { + case 0x80: + strnncpy(cnip, ¢rex[i+2], centrex[i+1], cnip_len); + if (MISDN_IE_DEBG) printf(" CENTREX cnip='%s'\n", cnip); + break; + + default: + j = 0; + while(j < centrex[i+1]) + { + if (MISDN_IE_DEBG) printf(debug+(j*3), " %02x", centrex[i+1+j]); + i++; + } + if (MISDN_IE_DEBG) printf(" CENTREX unknown=0x%2x len=%d%s\n", centrex[i], centrex[i+1], debug); + } + i += 1+centrex[i+1]; + } +} + + + + +/* facility for siemens CENTEX (known parts implemented only) */ +void enc_facility_calldeflect(unsigned char **ntmode, msg_t *msg, unsigned char *nr, int nt, struct misdn_bchannel *bc) +{ + unsigned char fac[256]; + + if (!nr) + return; + + /* calldeflect facility */ + + /* cnip */ + if (strlen((char *)nr) > 15) + { +/* if (options.deb & DEBUG_PORT) */ + if (MISDN_IE_DEBG) printf("%s: NR text too long (max 13 chars), cutting.\n", __FUNCTION__); + nr[15] = '\0'; + } + + fac[0]=0; // len + fac[1]=0; //len + fac[2]=0x01; // Use D-Chan + fac[3]=0; // Keypad len + fac[4]=31; // user user data? len = 31 = 29 + 2 + fac[5]=0x1c; // magic? + fac[6]=0x1d; // strlen destination + 18 = 29 + fac[7]=0x91; // .. + fac[8]=0xA1; + fac[9]=0x1A; // strlen destination + 15 = 26 + fac[10]=0x02; + fac[11]=0x01; + fac[12]=0x70; + fac[13]=0x02; + fac[14]=0x01; + fac[15]=0x0d; + fac[16]=0x30; + fac[17]=0x12; // strlen destination + 7 = 18 + fac[18]=0x30; // ...hm 0x30 + fac[19]=0x0d; // strlen destination + 2 + fac[20]=0x80; // CLIP + fac[21]=0x0b; // strlen destination + fac[22]=0x01; // destination start + fac[23]=0x01; // + fac[24]=0x01; // + fac[25]=0x01; // + fac[26]=0x01; // + fac[27]=0x01; // + fac[28]=0x01; // + fac[29]=0x01; // + fac[30]=0x01; // + fac[31]=0x01; // + fac[32]=0x01; // + fac[33]=0x01; // 0x1 = sending complete + fac[34]=0x01; + fac[35]=0x01; + + memcpy((unsigned char *)fac+22,nr,strlen(nr)); + fac[22+strlen( nr)]=0x01; // fill with 0x01 if number is only 6 numbers (local call) + fac[23+strlen(nr)]=0x01; + fac[24+strlen(nr)]=0x01; + fac[25+strlen(nr)]=0x01; + fac[26+strlen(nr)]=0x01; + + fac[6]=18+strlen(nr); + fac[9]=15+strlen(nr); + fac[17]=7+strlen(nr); + fac[19]=2+strlen(nr); + fac[21]=strlen(nr); + + enc_ie_facility(ntmode, msg, &fac[4], 36-4, nt , bc); +} + + +/* IE_USERUSER */ +void enc_ie_useruser(unsigned char **ntmode, msg_t *msg, int protocol, unsigned char *user, int user_len, int nt, struct misdn_bchannel *bc) +{ + unsigned char *p; + Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN); + int l; + + char debug[768]; + int i; + + if (protocol<0 || protocol>127) + { + printf("%s: ERROR: protocol(%d) is out of range.\n", __FUNCTION__, protocol); + return; + } + if (!user || user_len<=0) + { + return; + } + + i = 0; + while(i < user_len) + { + if (MISDN_IE_DEBG) printf(debug+(i*3), " %02x", user[i]); + i++; + } + + if (MISDN_IE_DEBG) printf(" protocol=%d user-user%s\n", protocol, debug); + + l = user_len; + p = msg_put(msg, l+3); + if (nt) + *ntmode = p+1; + else + qi->useruser = p - (unsigned char *)qi - sizeof(Q931_info_t); + p[0] = IE_USER_USER; + p[1] = l; + p[2] = 0x80 + protocol; + memcpy(p+3, user, user_len); +} + +void dec_ie_useruser(unsigned char *p, Q931_info_t *qi, int *protocol, unsigned char *user, int *user_len, int nt, struct misdn_bchannel *bc) +{ + char debug[768]; + int i; + + *user_len = 0; + *protocol = -1; + + if (!nt) + { + p = NULL; + if (qi->useruser) + p = (unsigned char *)qi + sizeof(Q931_info_t) + qi->useruser + 1; + } + if (!p) + return; + + *user_len = p[0]-1; + if (p[0] < 1) + return; + *protocol = p[1]; + memcpy(user, p+2, (*user_len<=128)?*(user_len):128); /* clip to 128 maximum */ + + i = 0; + while(i < *user_len) + { + if (MISDN_IE_DEBG) printf(debug+(i*3), " %02x", user[i]); + i++; + } + debug[i*3] = '\0'; + + if (MISDN_IE_DEBG) printf(" protocol=%d user-user%s\n", *protocol, debug); +} + + diff -u -N -r ast_clean/channels/misdn/isdn_lib.c ast_misdn/channels/misdn/isdn_lib.c --- ast_clean/channels/misdn/isdn_lib.c 1970-01-01 01:00:00.000000000 +0100 +++ ast_misdn/channels/misdn/isdn_lib.c 2005-10-17 19:31:23.000000000 +0200 @@ -0,0 +1,3103 @@ +/* + * Chan_Misdn -- Channel Driver for Asterisk + * + * Interface to mISDN + * + * Copyright (C) 2004, Christian Richter + * + * Christian Richter + * + * This program is free software, distributed under the terms of + * the GNU General Public License + */ + +static int nt_err_cnt =0 ; + +enum global_states { + MISDN_INITIALIZING, + MISDN_INITIALIZED +} ; + +static enum global_states global_state=MISDN_INITIALIZING; + + +#include <../i4lnet/net_l2.h> +#include +#include +#include +#include +#include + +#include "isdn_lib.h" + + +struct misdn_lib { + int midev; + int midev_nt; + + pthread_t event_thread; + pthread_t event_handler_thread; + + void *user_data; + + msg_queue_t upqueue; + msg_queue_t activatequeue; + + sem_t new_msg; + + struct misdn_stack *stack_list; +} ; + +#ifndef ECHOCAN_ON +#define ECHOCAN_ON 123 +#define ECHOCAN_OFF 124 +#endif + +#define MISDN_DEBUG 0 + +struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id); + +int setup_bc(struct misdn_bchannel *bc); + +int manager_isdn_handler(iframe_t *frm ,msg_t *msg); + +int misdn_lib_port_restart(int port); + +extern struct isdn_msg msgs_g[]; + +#define ISDN_PID_L3_B_USER 0x430000ff +#define ISDN_PID_L4_B_USER 0x440000ff + +/* #define MISDN_IBUF_SIZE 1024 */ +#define MISDN_IBUF_SIZE 512 + +/* Fine Tuning of Inband Signalling time */ +#define TONE_ALERT_CNT 41 /* 1 Sec */ +#define TONE_ALERT_SILENCE_CNT 200 /* 4 Sec */ + +#define TONE_BUSY_CNT 20 /* ? */ +#define TONE_BUSY_SILENCE_CNT 48 /* ? */ + +static int entity; + +static struct misdn_lib *glob_mgr; + +unsigned char tone_425_flip[TONE_425_SIZE]; +unsigned char tone_silence_flip[TONE_SILENCE_SIZE]; + +static void misdn_lib_isdn_event_catcher(void *arg); +static int handle_event_nt(void *dat, void *arg); + + +void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder); +void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder); +struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id); + +/* from isdn_lib.h */ +int init_bc(struct misdn_stack * stack, struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime); +struct misdn_stack* stack_te_init(int midev, int port, int ptp); +void stack_te_destroy(struct misdn_stack* stack); + /* user iface */ +int te_lib_init( void ) ; /* returns midev */ +void te_lib_destroy(int midev) ; +struct misdn_bchannel *manager_find_bc_by_pid(int pid); +struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc); +unsigned char * manager_flip_buf_bits ( unsigned char * buf , int len); +void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len); +void manager_clean_bc(struct misdn_bchannel *bc ); +void manager_bchannel_setup (struct misdn_bchannel *bc); +void manager_bchannel_cleanup (struct misdn_bchannel *bc); + +int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *frm, int nt); +enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *frm, int nt); +int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *frm, struct misdn_bchannel *bc, int nt); +char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt); +msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt); +void ec_chunk( struct misdn_bchannel *bc, unsigned char *rxchunk, unsigned char *txchunk, int chunk_size); + /* end */ +int bchdev_echocancel_activate(struct misdn_bchannel* dev); +void bchdev_echocancel_deactivate(struct misdn_bchannel* dev); +/* end */ + + +static char *bearer2str(int cap) { + static char *bearers[]={ + "Speech", + "Audio 3.1k", + "Unres Digital", + "Res Digital", + "Unknown Bearer" + }; + + switch (cap) { + case INFO_CAPABILITY_SPEECH: + return bearers[0]; + break; + case INFO_CAPABILITY_AUDIO_3_1K: + return bearers[1]; + break; + case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: + return bearers[2]; + break; + case INFO_CAPABILITY_DIGITAL_RESTRICTED: + return bearers[3]; + break; + default: + return bearers[4]; + break; + } +} + + +static char flip_table[256]; + +void init_flip_bits(void) +{ + int i,k; + + for (i = 0 ; i < 256 ; i++) { + unsigned char sample = 0 ; + for (k = 0; k<8; k++) { + if ( i & 1 << k ) sample |= 0x80 >> k; + } + flip_table[i] = sample; + } +} + +static unsigned char * flip_buf_bits ( unsigned char * buf , int len) +{ + int i; + char * start = buf; + + for (i = 0 ; i < len; i++) { + buf[i] = flip_table[buf[i]]; + } + + return start; +} + + + + +msg_t *create_l2msg(int prim, int dinfo, int size) /* NT only */ +{ + int i = 0; + msg_t *dmsg; + + while(i < 10) + { + dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL); + if (dmsg) + return(dmsg); + + if (!i) + printf("cannot allocate memory, trying again...\n"); + i++; + usleep(300000); + } + printf("cannot allocate memory, system overloaded.\n"); + exit(-1); +} + + + +msg_t *create_l3msg(int prim, int mt, int dinfo, int size, int ntmode) +{ + int i = 0; + msg_t *dmsg; + Q931_info_t *qi; + iframe_t *frm; + + if (!ntmode) + size = sizeof(Q931_info_t)+2; + + while(i < 10) { + if (ntmode) { + dmsg = prep_l3data_msg(prim, dinfo, size, 256, NULL); + if (dmsg) { + return(dmsg); + } + } else { + dmsg = alloc_msg(size+256+mISDN_HEADER_LEN+DEFAULT_HEADROOM); + if (dmsg) + { + memset(msg_put(dmsg,size+mISDN_HEADER_LEN), 0, size+mISDN_HEADER_LEN); + frm = (iframe_t *)dmsg->data; + frm->prim = prim; + frm->dinfo = dinfo; + qi = (Q931_info_t *)(dmsg->data + mISDN_HEADER_LEN); + qi->type = mt; + return(dmsg); + } + } + + if (!i) printf("cannot allocate memory, trying again...\n"); + i++; + usleep(300000); + } + printf("cannot allocate memory, system overloaded.\n"); + exit(-1); +} + + +int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg) +{ + iframe_t *frm; + frm = (iframe_t *)dmsg->data; + + frm->addr = (bc->stack->upper_id & IF_ADDRMASK) | IF_DOWN ; + frm->dinfo = bc->l3_id; + + frm->len = (dmsg->len) - mISDN_HEADER_LEN; + + mISDN_write(midev, dmsg->data, dmsg->len, TIMEOUT_1SEC); + + free_msg(dmsg); + + return 0; +} + + +static int mypid=0; + +int misdn_cap_is_speech(int cap) +/** Poor mans version **/ +{ + if (cap != INFO_CAPABILITY_DIGITAL_UNRESTRICTED) return 1; + return 0; +} + +int misdn_inband_avail(struct misdn_bchannel *bc) +{ + switch (bc->progress_indicator) { + case INFO_PI_INBAND_AVAILABLE: + case INFO_PI_CALL_NOT_E2E_ISDN: + return 1; + default: + return 0; + } + return 0; +} + + +void dump_chan_list(struct misdn_stack *stack) +{ + int i; + + for (i=0; i b_num; i++) { + cb_log(3, stack->port, "Idx:%d stack->cchan:%d Chan:%d\n",i,stack->channels[i], i+1); + } +} + + + + +static int find_free_chan_in_stack(struct misdn_stack *stack, int channel) +{ + int i; + + if (channel < 0 || channel > MAX_BCHANS) { + cb_log(4, stack->port, " !! out of bound call to find_free_chan_in_stack! (port:%d ch:%d)\n", stack->port, channel); + return 0; + } + + channel--; + + for (i = 0; i < stack->b_num; i++) { + if (i != 15 && (channel < 0 || i == channel)) { /* skip E1 Dchannel ;) and work with chan preselection */ + if (!stack->channels[i]) { + cb_log (4, stack->port, " --> found chan%s: %d\n", channel>=0?" (preselected)":"", i+1); + stack->channels[i] = 1; + return i+1; + } + } + } + + cb_log (4, stack->port, " !! NO FREE CHAN IN STACK\n"); + dump_chan_list(stack); + + return 0; +} + +int empty_chan_in_stack(struct misdn_stack *stack, int channel) +{ + cb_log (4, stack?stack->port:0, " --> empty chan %d\n",channel); + stack->channels[channel-1] = 0; + dump_chan_list(stack); + return 0; +} + + +void empty_bc(struct misdn_bchannel *bc) +{ + bc->channel = 0; + bc->in_use = 0; + + bc->nohdlc=0; + + bc->send_dtmf=0; + bc->nodsp=0; + bc->nojitter=0; + + bc->time_usec=0; + + bc->rxgain=0; + bc->txgain=0; + + bc->crypt=0; + bc->curptx=0; bc->curprx=0; + + bc->crypt_key[0] = 0; + + bc->tone=TONE_NONE; + bc->tone_cnt2 = bc->tone_cnt=0; + + bc->dnumplan=NUMPLAN_UNKNOWN; + bc->onumplan=NUMPLAN_UNKNOWN; + bc->rnumplan=NUMPLAN_UNKNOWN; + + bc->active = 0; + + bc->ec_enable = 0; + bc->ec_deftaps = 128; + bc->ec_whenbridged = 0; + bc->ec_training = 400; + + + bc->orig=0; + + bc->cause=16; + bc->out_cause=16; + bc->pres=0 ; /* screened */ + + bc->evq=EVENT_NOTHING; + + bc->progress_coding=0; + bc->progress_location=0; + bc->progress_indicator=0; + +/** Set Default Bearer Caps **/ + bc->capability=INFO_CAPABILITY_SPEECH; + bc->law=INFO_CODEC_ALAW; + bc->mode=0; + bc->rate=0; + bc->user1=0; + bc->async=0; + bc->urate=0; + + + + bc->info_dad[0] = 0; + bc->display[0] = 0; + bc->infos_pending[0] = 0; + bc->oad[0] = 0; + bc->dad[0] = 0; + bc->orig_dad[0] = 0; + + bc->facility=FACILITY_NONE; + bc->facility_calldeflect_nr[0]=0; +} + + +int clean_up_bc(struct misdn_bchannel *bc) +{ + int ret=0; + unsigned char buff[32]; + if (!bc || !bc->stack) return -1; + + if (!bc->upset) { + cb_log(5, bc->stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid); + return -1; + } + + cb_log(5, bc->stack->port, "$$$ Cleaning up bc with stid :%x\n", bc->b_stid); + + + if ( misdn_cap_is_speech(bc->capability) && bc->ec_enable) { + manager_ec_disable(bc); + } + + mISDN_write_frame(bc->stack->midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC); + + + bc->b_stid = 0; + + bc->upset=0; + + return ret; +} + + + +void clear_l3(struct misdn_stack *stack) +{ + int i; + for (i=0; ib_num; i++) { + if (global_state == MISDN_INITIALIZED) { + cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data); + empty_chan_in_stack(stack,i+1); + empty_bc(&stack->bc[i]); + clean_up_bc(&stack->bc[i]); + + } + + } +} + +int set_chan_in_stack(struct misdn_stack *stack, int channel) +{ + stack->channels[channel-1] = 1; + + return 0; +} + +int chan_in_stack_free(struct misdn_stack *stack, int channel) +{ + if (stack->channels[channel-1]) + return 0; + + return 1; +} + + + +static int newteid=0; + +#ifdef MISDNUSER_JOLLY +#define MAXPROCS 0x100 +#else +#define MAXPROCS 0x10 +#endif + +static int create_process (int midev, struct misdn_bchannel *bc) { + iframe_t ncr; + int l3_id; + int i; + struct misdn_stack *stack=bc->stack; + int free_chan; + + if (stack->mode == NT_MODE) { + free_chan = find_free_chan_in_stack(stack, bc->channel_preselected?bc->channel:0); + if (!free_chan) return -1; + bc->channel=free_chan; + + for (i=0; i <= MAXPROCS; i++) + if (stack->procids[i]==0) break; + + if (i== MAXPROCS) { + cb_log(0, stack->port, "Couldnt Create New ProcId Port:%d\n",stack->port); + return -1; + } + stack->procids[i]=1; + +#ifdef MISDNUSER_JOLLY + l3_id = 0xff00 | i; +#else + l3_id = 0xfff0 | i; +#endif + + ncr.prim = CC_NEW_CR | REQUEST; + ncr.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ; + ncr.dinfo = l3_id; + ncr.len = 0; + + bc->l3_id = l3_id; + if (mypid>5000) mypid=0; + bc->pid=mypid++; + + cb_log(3, stack->port, " --> new_l3id %x\n",l3_id); + + } else { + if (stack->ptp || stack->te_choose_channel) { + /* we know exactly which channels are in use */ + free_chan = find_free_chan_in_stack(stack, bc->channel_preselected?bc->channel:0); + if (!free_chan) return -1; + bc->channel=free_chan; + } else { + /* other phones could have made a call also on this port (ptmp) */ + bc->channel=0xff; + } + + + /* if we are in te-mode, we need to create a process first */ + if (newteid++ > 0xffff) + newteid = 0x0001; + + l3_id = (entity<<16) | newteid; + /* preparing message */ + ncr.prim = CC_NEW_CR | REQUEST; + ncr.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ; + ncr.dinfo =l3_id; + ncr.len = 0; + /* send message */ + + bc->l3_id = l3_id; + if (mypid>5000) mypid=0; + bc->pid=mypid++; + + cb_log(3, stack->port, "--> new_l3id %x\n",l3_id); + + mISDN_write(midev, &ncr, mISDN_HEADER_LEN+ncr.len, TIMEOUT_1SEC); + } + + return l3_id; +} + + + +int setup_bc(struct misdn_bchannel *bc) +{ + unsigned char buff[1025]; + + mISDN_pid_t pid; + int ret; + int midev=bc->stack->midev; + int channel=bc->channel-1-(bc->channel>16); + int b_stid=bc->stack->b_stids[channel>=0?channel:0]; + + if (bc->upset) { + cb_log(5, bc->stack->port, "$$$ bc already upsetted stid :%x\n", b_stid); + return -1; + } + + + if (bc->nodsp) { + clean_up_bc(bc); + } + + cb_log(5, bc->stack->port, "$$$ Setting up bc with stid :%x\n", b_stid); + + if (b_stid <= 0) { + cb_log(0, bc->stack->port," -- Stid <=0 at the moment on port:%d channel:%d\n",bc->stack->port,channel); + return 1; + } + + + bc->b_stid = b_stid; + + { + layer_info_t li; + memset(&li, 0, sizeof(li)); + + li.object_id = -1; + li.extentions = 0; + + li.st = bc->b_stid; /* given idx */ + + + if ( misdn_cap_is_speech(bc->capability) && !bc->nodsp && bc->async != 1) { + cb_log(4, bc->stack->port,"setup_bc: with dsp\n"); + { + int l = sizeof(li.name); + strncpy(li.name, "B L4", l); + li.name[l-1] = 0; + } + li.pid.layermask = ISDN_LAYER((4)); + li.pid.protocol[4] = ISDN_PID_L4_B_USER; + + } else { + cb_log(4, bc->stack->port,"setup_bc: without dsp\n"); + { + int l = sizeof(li.name); + strncpy(li.name, "B L3", l); + li.name[l-1] = 0; + } + li.pid.layermask = ISDN_LAYER((3)); + li.pid.protocol[3] = ISDN_PID_L3_B_USER; + } + + ret = mISDN_new_layer(midev, &li); + if (ret <= 0) { + cb_log(0, bc->stack->port,"New Layer Err: %d %s port:%d\n",ret,strerror(errno), bc->stack->port); + return(-EINVAL); + } + + bc->layer_id = ret; + } + + memset(&pid, 0, sizeof(pid)); + + bc->addr = ( bc->layer_id & IF_ADDRMASK) | IF_DOWN; + cb_log(4, bc->stack->port," --> Got Adr %x\n", bc->addr); + cb_log(4, bc->stack->port," --> Channel is %d\n", bc->channel); + + + if (bc->async == 1 || bc->nodsp) { + cb_log(4, bc->stack->port," --> TRANSPARENT Mode (no DSP)\n"); + pid.protocol[1] = ISDN_PID_L1_B_64TRANS; + pid.protocol[2] = ISDN_PID_L2_B_TRANS; + pid.protocol[3] = ISDN_PID_L3_B_USER; + pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)); + } else if ( misdn_cap_is_speech(bc->capability)) { + cb_log(4, bc->stack->port," --> TRANSPARENT Mode\n"); + pid.protocol[1] = ISDN_PID_L1_B_64TRANS; + pid.protocol[2] = ISDN_PID_L2_B_TRANS; + pid.protocol[3] = ISDN_PID_L3_B_DSP; + pid.protocol[4] = ISDN_PID_L4_B_USER; + pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4)); + + } else { + cb_log(4, bc->stack->port," --> HDLC Mode\n"); + pid.protocol[1] = ISDN_PID_L1_B_64HDLC ; + pid.protocol[2] = ISDN_PID_L2_B_TRANS ; + pid.protocol[3] = ISDN_PID_L3_B_USER; + pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) ; + } + + ret = mISDN_set_stack(midev, bc->b_stid, &pid); + + + if (ret){ + cb_log(5, bc->stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno)); + + mISDN_write_frame(midev, buff, bc->addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC); + return(-EINVAL); + } + + bc->upset=1; + + return 0; +} + + + +/** IFACE **/ +int init_bc(struct misdn_stack *stack, struct misdn_bchannel *bc, int midev, int port, int bidx, char *msn, int firsttime) +{ + unsigned char buff[1025]; + iframe_t *frm = (iframe_t *)buff; + int ret; + + if (!bc) return -1; + + cb_log(4, port, "Init.BC %d on port:%d\n",bidx, port); + + memset(bc, 0,sizeof(struct misdn_bchannel)); + + if (msn) { + int l = sizeof(bc->msn); + strncpy(bc->msn,msn, l); + bc->msn[l-1] = 0; + } + + + empty_bc(bc); + bc->upset=0; + + bc->astbuf= init_ibuffer(MISDN_IBUF_SIZE); + clear_ibuffer( bc->astbuf); + bc->astbuf->rsem=malloc(sizeof(sem_t)); + + + if (sem_init(bc->astbuf->rsem,1,0)<0) + sem_init(bc->astbuf->rsem,0,0); + + + bc->misdnbuf= init_ibuffer(MISDN_IBUF_SIZE); + clear_ibuffer( bc->misdnbuf); + bc->misdnbuf->rsem=malloc(sizeof(sem_t)); + + if (sem_init(bc->misdnbuf->rsem,1,0)< 0) + sem_init(bc->misdnbuf->rsem,0,0); + + bc->stack=stack; + + { + stack_info_t *stinf; + ret = mISDN_get_stack_info(midev, bc->stack->port, buff, sizeof(buff)); + if (ret < 0) { + cb_log(0, port, "%s: Cannot get stack info for port:%d (ret=%d)\n", __FUNCTION__, port, ret); + return -1; + } + + stinf = (stack_info_t *)&frm->data.p; + + cb_log(4, port, " --> Child %x\n",stinf->child[bidx]); + } + + return 0; +} + + + +struct misdn_stack * stack_nt_init(struct misdn_stack *stack, int midev, int port) +{ + int ret; + layer_info_t li; + interface_info_t ii; + + + cb_log(4, port, "Init. Stack on port:%d\n",port); + stack->mode = NT_MODE; + + stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, 1); + if (stack->lower_id <= 0) { + cb_log(0, port, "%s: Cannot get layer(%d) id of port:%d\n", __FUNCTION__, 1, port); + return(NULL); + } + + + memset(&li, 0, sizeof(li)); + { + int l = sizeof(li.name); + strncpy(li.name,"net l2", l); + li.name[l-1] = 0; + } + li.object_id = -1; + li.extentions = 0; + li.pid.protocol[2] = ISDN_PID_L2_LAPD_NET; + li.pid.layermask = ISDN_LAYER((2)); + li.st = stack->d_stid; + + + stack->upper_id = mISDN_new_layer(midev, &li); + if (stack->upper_id <= 0) { + cb_log(0, port, "%s: Cannot add layer %d of port:%d\n", __FUNCTION__, 2, port); + return(NULL); + } + + cb_log(4, port, "NT Stacks upper_id %x\n",stack->upper_id); + + memset(&ii, 0, sizeof(ii)); + ii.extentions = EXT_IF_EXCLUSIV; + ii.owner = stack->upper_id; + ii.peer = stack->lower_id; + ii.stat = IF_DOWN; + ret = mISDN_connect(midev, &ii); + if (ret) { + cb_log(0, port, "%s: Cannot connect layer %d of port:%d exclusively.\n", __FUNCTION__, 2, port); + return(NULL); + } + + /* create nst (nt-mode only) */ + { + memset(&stack->nst, 0, sizeof(net_stack_t)); + memset(&stack->mgr, 0, sizeof(manager_t)); + + stack->mgr.nst = &stack->nst; + stack->nst.manager = &stack->mgr; + + stack->nst.l3_manager = handle_event_nt; + stack->nst.device = midev; + stack->nst.cardnr = port; + stack->nst.d_stid = stack->d_stid; + +#ifdef MISDNUSER_JOLLY + stack->nst.feature = FEATURE_NET_HOLD; + if (stack->ptp) + stack->nst.feature |= FEATURE_NET_PTP; + if (stack->pri) + stack->nst.feature |= FEATURE_NET_CRLEN2 | FEATURE_NET_EXTCID; +#endif + + stack->nst.l1_id = stack->lower_id; + stack->nst.l2_id = stack->upper_id; + + msg_queue_init(&stack->nst.down_queue); + + Isdnl2Init(&stack->nst); + Isdnl3Init(&stack->nst); + } + /* if ntmode, establish L1 to send the tei removal during start */ + { + iframe_t act; + act.prim = PH_ACTIVATE | REQUEST; + act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ; + act.dinfo = 0; + act.len = 0; + mISDN_write(midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); + } + + stack->l2link=0; + if (stack->ptp) { + msg_t *dmsg; + /* L2 */ + dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0); + + if (stack->nst.manager_l3(&stack->nst, dmsg)) + free_msg(dmsg); + + stack->l2link=1; + } + + return stack; +} + + +struct misdn_stack* stack_te_init( int midev, int port, int ptp ) +{ + int ret; + unsigned char buff[1025]; + iframe_t *frm = (iframe_t *)buff; + stack_info_t *stinf; + int i; + layer_info_t li; + interface_info_t ii; + struct misdn_stack *stack = malloc(sizeof(struct misdn_stack)); + if (!stack ) return NULL; + + + //cb_log(2, "Init. Stack on port:%d\n",port); + cb_log(4, port, "Init. Stack on port:%d\n",port); + + memset(stack,0,sizeof(struct misdn_stack)); + + for (i=0; ichannels[i]=0; + + stack->port=port; + stack->midev=midev; + stack->ptp=ptp; + + stack->holding=NULL; + stack->pri=0; + + msg_queue_init(&stack->downqueue); + + /* query port's requirements */ + ret = mISDN_get_stack_info(midev, port, buff, sizeof(buff)); + if (ret < 0) { + cb_log(0, port, "%s: Cannot get stack info for port:%d (ret=%d)\n", __FUNCTION__, port, ret); + return(NULL); + } + + stinf = (stack_info_t *)&frm->data.p; + + stack->d_stid = stinf->id; + stack->b_num = stinf->childcnt; + + for (i=0; ichildcnt; i++) + stack->b_stids[i] = stinf->child[i]; + + switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) { + case ISDN_PID_L0_TE_S0: + //cb_log(2, "TE Stack\n"); + stack->mode = TE_MODE; + break; + case ISDN_PID_L0_NT_S0: + cb_log(4, port, "NT Stack\n"); + + return stack_nt_init(stack,midev,port); + break; + + case ISDN_PID_L0_TE_U: + break; + case ISDN_PID_L0_NT_U: + break; + case ISDN_PID_L0_TE_UP2: + break; + case ISDN_PID_L0_NT_UP2: + break; + case ISDN_PID_L0_TE_E1: + cb_log(4, port, "TE S2M Stack\n"); + stack->mode = TE_MODE; + stack->pri=1; + break; + case ISDN_PID_L0_NT_E1: + cb_log(4, port, "TE S2M Stack\n"); + stack->mode = NT_MODE; + stack->pri=1; + + return stack_nt_init(stack,midev,port); + break; + default: + cb_log(0, port, "unknown port(%d) type 0x%08x\n", port, stinf->pid.protocol[0]); + + } + + if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP ) { /* || (nt&&ptp) || pri */ + stack->ptp = 1; + } else { + stack->ptp = 0; + } + + stack->lower_id = mISDN_get_layerid(midev, stack->d_stid, 3); + if (stack->lower_id <= 0) { + cb_log(0, stack->port, "No lower Id port:%d\n", stack->port); + return(NULL); + } + + memset(&li, 0, sizeof(li)); + { + int l = sizeof(li.name); + strncpy(li.name, "user L4", l); + li.name[l-1] = 0; + } + li.object_id = -1; + li.extentions = 0; + + li.pid.protocol[4] = ISDN_PID_L4_CAPI20; + + li.pid.layermask = ISDN_LAYER((4)); + li.st = stack->d_stid; + stack->upper_id = mISDN_new_layer(midev, &li); + + if (stack->upper_id <= 0) { + cb_log(0, stack->port, "No Upper ID port:%d\n",stack->port); + return(NULL); + } + + memset(&ii, 0, sizeof(ii)); + ii.extentions = EXT_IF_EXCLUSIV | EXT_IF_CREATE; + ii.owner = stack->upper_id; + ii.peer = stack->lower_id; + ii.stat = IF_DOWN; + ret = mISDN_connect(midev, &ii); + if (ret) { + cb_log(0, stack->port, "No Connect port:%d\n", stack->port); + return NULL; + } + + + /* if ptp, establish link */ + if (stack->ptp) { + iframe_t act; + +#ifdef DL_STATUS + act.prim = DL_STATUS | REQUEST; +#else + act.prim = DL_ESTABLISH | REQUEST; +#endif + act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN; + act.dinfo = 0; + act.len = 0; + mISDN_write(midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); + } + +#ifdef DL_STATUS + { /* Pull Up L1 if we have JOLLY */ + iframe_t act; + act.prim = PH_ACTIVATE | REQUEST; + act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ; + act.dinfo = 0; + act.len = 0; + mISDN_write(midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); + } + + + if (!stack->ptp) { + iframe_t act; + + + act.prim = DL_STATUS | REQUEST; + + act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN; + act.dinfo = 0; + act.len = 0; + mISDN_write(midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); + } +#endif + + + /* initially, we assume that the link is NOT up */ + stack->l2link = 0; + stack->l1link = 0; + + stack->next=NULL; + + return stack; + +} + +void stack_te_destroy(struct misdn_stack* stack) +{ + char buf[1024]; + if (!stack) return; + + if (stack->lower_id) + mISDN_write_frame(stack->midev, buf, stack->lower_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC); + + if (stack->upper_id) + mISDN_write_frame(stack->midev, buf, stack->upper_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC); +} + + +struct misdn_stack * find_stack_by_addr(int addr) +{ + struct misdn_stack *stack; + + for (stack=glob_mgr->stack_list; + stack; + stack=stack->next) { + if ( stack->upper_id == addr) return stack; + } + + return NULL; +} + + +struct misdn_stack * find_stack_by_port(int port) +{ + struct misdn_stack *stack; + + for (stack=glob_mgr->stack_list; + stack; + stack=stack->next) + if (stack->port == port) return stack; + + return NULL; +} + +struct misdn_stack * find_stack_by_mgr(manager_t* mgr_nt) +{ + struct misdn_stack *stack; + + for (stack=glob_mgr->stack_list; + stack; + stack=stack->next) + if ( &stack->mgr == mgr_nt) return stack; + + return NULL; +} + +struct misdn_bchannel *find_bc_by_masked_l3id(struct misdn_stack *stack, unsigned long l3id, unsigned long mask) +{ + int i; + for (i=0; ib_num; i++) { + if ( (stack->bc[i].l3_id & mask) == (l3id & mask)) return &stack->bc[i] ; + } + return stack_holder_find(stack,l3id); +} + + +struct misdn_bchannel *find_bc_by_l3id(struct misdn_stack *stack, unsigned long l3id) +{ + int i; + for (i=0; ib_num; i++) { + if (stack->bc[i].l3_id == l3id) return &stack->bc[i] ; + } + return stack_holder_find(stack,l3id); +} + +struct misdn_bchannel *find_bc_holded(struct misdn_stack *stack) +{ + int i; + for (i=0; ib_num; i++) { + if (stack->bc[i].holded ) return &stack->bc[i] ; + } + return NULL; +} + + +struct misdn_bchannel *find_bc_by_addr(unsigned long addr) +{ + int port = addr & IF_CONTRMASK; + struct misdn_stack* stack; + int i; + + + for (stack=glob_mgr->stack_list; + stack; + stack=stack->next) { + + if (stack->port == port) { + for (i=0; i< stack->b_num; i++) { + if (stack->bc[i].addr==addr) { + return &stack->bc[i]; + } + } + } + } + + return NULL; +} + + + + +int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm) +{ + + if (bc->stack->mode == TE_MODE) { + switch (event) { + case EVENT_CONNECT: + if ( *bc->crypt_key ) { + cb_log(4, bc->stack->port, "ENABLING BLOWFISH port:%d channel:%d oad%d:%s dad%d:%s\n", bc->stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad); + + manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) ); + } + case EVENT_SETUP: + if (bc->channel>0 && bc->channel<255) + set_chan_in_stack(bc->stack, bc->channel); + break; + case EVENT_ALERTING: + case EVENT_PROGRESS: + case EVENT_PROCEEDING: + case EVENT_SETUP_ACKNOWLEDGE: + + { + struct misdn_stack *stack=find_stack_by_port(frm->addr&IF_CONTRMASK); + if (!stack) return -1; + + if (bc->channel == 0xff) { + bc->channel=find_free_chan_in_stack(bc->stack, 0); + if (!bc->channel) { + cb_log(0, bc->stack->port, "Any Channel Requested, but we have no more!!\n"); + break; + } + } + + if (stack->mode == TE_MODE) { + setup_bc(bc); + } + } + + default: + break; + } + } else { /** NT MODE **/ + + } + return 0; +} + +int handle_new_process(struct misdn_stack *stack, iframe_t *frm) +{ + + struct misdn_bchannel* bc=misdn_lib_get_free_bc(frm->addr&IF_CONTRMASK, 0); + + if (!bc) { + cb_log(0, 0, " --> !! lib: No free channel!\n"); + return -1; + } + + cb_log(4, bc->stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo); + bc->l3_id=frm->dinfo; + + if (mypid>5000) mypid=0; + bc->pid=mypid++; + return 0; +} + +int handle_cr ( iframe_t *frm) +{ + struct misdn_stack *stack=find_stack_by_port(frm->addr&IF_CONTRMASK); + + if (!stack) return -1; + + switch (frm->prim) { + case CC_NEW_CR|INDICATION: + cb_log(4, stack->port, " --> lib: NEW_CR Ind with l3id:%x port:%d\n",frm->dinfo, stack->port); + handle_new_process(stack, frm); + return 1; + case CC_NEW_CR|CONFIRM: + return 1; + case CC_NEW_CR|REQUEST: + return 1; + case CC_RELEASE_CR|REQUEST: + return 1; + case CC_RELEASE_CR|CONFIRM: + break; + case CC_RELEASE_CR|INDICATION: + cb_log(4, stack->port, " --> lib: RELEASE_CR Ind with l3id:%x\n",frm->dinfo); + { + struct misdn_bchannel *bc=find_bc_by_l3id(stack, frm->dinfo); + struct misdn_bchannel dummybc; + + if (!bc) { + cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on port:%d\n", frm->dinfo, stack->port); + memset (&dummybc,0,sizeof(dummybc)); + dummybc.stack=stack; + dummybc.l3_id=frm->dinfo; + bc=&dummybc; + } + + if (bc) { + cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo); + empty_chan_in_stack(bc->stack,bc->channel); + empty_bc(bc); + clean_up_bc(bc); + dump_chan_list(bc->stack); + bc->pid = 0; + cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data); + + if (bc->stack_holder) { + cb_log(4,stack->port, "REMOVEING Holder\n"); + stack_holder_remove( bc->stack, bc); + free(bc); + } + } + else { + if (stack->mode == NT_MODE) + cb_log(4, stack->port, "BC with dinfo: %x not found.. (prim was %x and addr %x)\n",frm->dinfo, frm->prim, frm->addr); + } + + return 1; + } + break; + } + + return 0; +} + + +/*Emptys bc if it's reserved (no SETUP out yet)*/ +void misdn_lib_release(struct misdn_bchannel *bc) +{ + if (bc->channel>=0) { + empty_chan_in_stack(bc->stack,bc->channel); + empty_bc(bc); + } + clean_up_bc(bc); +} + +int misdn_lib_get_port_up (int port) +{ /* Pull Up L1 if we have JOLLY */ + struct misdn_stack *stack; + + for (stack=glob_mgr->stack_list; + stack; + stack=stack->next) { + + if (stack->port == port) { + iframe_t act; + act.prim = DL_ESTABLISH | REQUEST; + act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ; + act.dinfo = 0; + act.len = 0; + mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); + return 0; + } + } + return 0; +} + + +int misdn_lib_send_facility(struct misdn_bchannel *bc, enum facility_type fac, void *data) +{ + bc->facility=fac; + strcpy(bc->facility_calldeflect_nr,(char*)data); + + misdn_lib_send_event(bc,EVENT_FACILITY); + return 0; +} + + +int misdn_lib_port_up(int port) +{ + struct misdn_stack *stack; + + for (stack=glob_mgr->stack_list; + stack; + stack=stack->next) { + + if (stack->port == port) { + if (stack->mode == NT_MODE) { + if (stack->l1link) + return 1; + else + return 0; + } else { + if (stack->l2link) + return 1; + else + return 0; + } + + } + } + + return -1; +} + + +int +handle_event_nt(void *dat, void *arg) +{ + manager_t *mgr = (manager_t *)dat; + msg_t *msg = (msg_t *)arg; +#ifdef MISDNUSER_JOLLY + mISDNuser_head_t *hh; +#else + mISDN_head_t *hh; +#endif + struct misdn_stack *stack=find_stack_by_mgr(mgr); + int port; + + if (!msg || !mgr) + return(-EINVAL); + +#ifdef MISDNUSER_JOLLY + hh=(mISDNuser_head_t*)msg->data; +#else + hh=(mISDN_head_t*)msg->data; +#endif + + port=hh->dinfo & IF_CONTRMASK; + + cb_log(4, stack->port, " --> lib: prim %x dinfo %x port: %d\n",hh->prim, hh->dinfo ,stack->port); + + { + switch(hh->prim){ + + case CC_RETRIEVE|INDICATION: + { + iframe_t frm; /* fake te frm to add callref to global callreflist */ + frm.dinfo = hh->dinfo; + frm.addr=stack->upper_id; + frm.prim = CC_NEW_CR|INDICATION; + + if (handle_cr(&frm)< 0) { + msg_t *dmsg; + cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo); + dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1); + stack->nst.manager_l3(&stack->nst, dmsg); + free_msg(msg); + return 0; + } + + struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo); + cb_event(EVENT_NEW_BC, bc, glob_mgr->user_data); + struct misdn_bchannel *hold_bc=stack_holder_find(stack,bc->l3_id); + if (hold_bc) { + cb_log(4, stack->port, "REMOVEING Holder\n"); + stack_holder_remove(stack, hold_bc); + free(hold_bc); + } + + } + + break; + + + case CC_SETUP|CONFIRM: + { + struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo); + int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE)); + + cb_log(4, bc?bc->stack->port:0, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID is %x\n",l3id ); + + if (!bc) { cb_log(4, 0, "Bc Not found (after SETUP CONFIRM)\n"); return 0; } + + bc->l3_id=l3id; + cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data); + } + free_msg(msg); + return 0; + + case CC_SETUP|INDICATION: + { + iframe_t frm; /* fake te frm to add callref to global callreflist */ + frm.dinfo = hh->dinfo; + frm.addr=stack->upper_id; + frm.prim = CC_NEW_CR|INDICATION; + + if (handle_cr(&frm)< 0) { + msg_t *dmsg; + cb_log(4, stack->port, "Patch from MEIDANIS:Sending RELEASE_COMPLETE %x (No free Chan for you..)\n", hh->dinfo); + dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1); + stack->nst.manager_l3(&stack->nst, dmsg); + free_msg(msg); + return 0; + } + } + break; + + + case CC_ALERTING|INDICATION: + case CC_PROCEEDING|INDICATION: + case CC_CONNECT|INDICATION: + { + struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo); + + setup_bc(bc); + } + break; + case CC_DISCONNECT|INDICATION: + { + struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo); + if (!bc) { + bc=find_bc_by_masked_l3id(stack, hh->dinfo, 0xffff0000); + if (bc) { //repair reject bug + int myprocid=bc->l3_id&0x0000ffff; + hh->dinfo=(hh->dinfo&0xffff0000)|myprocid; + cb_log(4,bc->stack->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo); + } + } + } + break; + + case CC_RELEASE_COMPLETE|INDICATION: + break; + + case CC_SUSPEND|INDICATION: + { + msg_t *dmsg; + cb_log(4, stack->port, " --> Got Suspend, sending Reject for now\n"); + dmsg = create_l3msg(CC_SUSPEND_REJECT | REQUEST,MT_SUSPEND_REJECT, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1); + stack->nst.manager_l3(&stack->nst, dmsg); + free_msg(msg); + return 0; + } + break; + case CC_RESUME|INDICATION: + break; + + case CC_RELEASE|CONFIRM: + { + struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo); + cb_log(4, stack->port, " --> RELEASE CONFIRM, sending RELEASE_COMPLETE\n"); + if (bc) misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); + } + hh->prim=CC_RELEASE|INDICATION; + break; + case CC_RELEASE|INDICATION: + break; + + case CC_RELEASE_CR|INDICATION: + { + struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo); + struct misdn_bchannel dummybc; + iframe_t frm; /* fake te frm to remove callref from global callreflist */ + frm.dinfo = hh->dinfo; + frm.addr=stack->upper_id; + frm.prim = CC_RELEASE_CR|INDICATION; + cb_log(4, stack->port, " --> Faking Realease_cr for %x\n",frm.addr); + /** removing procid **/ + + if (!bc) { + cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on port:%d\n", hh->dinfo, stack->port); + memset (&dummybc,0,sizeof(dummybc)); + dummybc.stack=stack; + dummybc.l3_id=hh->dinfo; + bc=&dummybc; + } + + if (bc) { +#ifdef MISDNUSER_JOLLY + if ( (bc->l3_id & 0xff00) == 0xff00) { + cb_log(4, stack->port, " --> Removing Process Id:%x on port:%d\n", bc->l3_id&0xff, stack->port); + stack->procids[bc->l3_id&0xff] = 0 ; + } +#else + if ( (bc->l3_id & 0xfff0) == 0xfff0) { + cb_log(4, stack->port, " --> Removing Process Id:%x on port:%d\n", bc->l3_id&0xf, stack->port); + stack->procids[bc->l3_id&0xf] = 0 ; + + } + +#endif + + } + else cb_log(0, stack->port, "Couldnt find BC so I couldnt remove the Process!!!! this is bad Port:%d\n", stack->port ); + + handle_cr(&frm); + free_msg(msg); + return 0 ; + } + + break; + + case CC_NEW_CR|INDICATION: + /* Got New CR for bchan, for now I handle this one in */ + /* connect_ack, Need to be changed */ + { + struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo); + int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE)); + if (!bc) { cb_log(0, 0, " --> In NEW_CR: didn't found bc ??\n"); return -1;}; +#ifdef MISDNUSER_JOLLY + if (((l3id&0xff00)!=0xff00) && ((bc->l3_id&0xff00)==0xff00)) { + cb_log(4, stack->port, " --> Removing Process Id:%x on port:%d\n", 0xff&bc->l3_id, stack->port); + stack->procids[bc->l3_id&0xff] = 0 ; + } +#else + if (((l3id&0xfff0)!=0xfff0) && ((bc->l3_id&0xfff0)==0xfff0)) { + cb_log(4, stack->port, "Removing Process Id:%x on port:%d\n", 0xf&bc->l3_id, stack->port); + stack->procids[bc->l3_id&0xf] = 0 ; + } + +#endif + cb_log(4, stack->port, "lib: Event_ind:CC_NEW_CR : very new L3ID is %x\n",l3id ); + + bc->l3_id =l3id; + cb_event(EVENT_NEW_L3ID, bc, glob_mgr->user_data); + + + free_msg(msg); + return 0; + } + + case DL_ESTABLISH | INDICATION: + case DL_ESTABLISH | CONFIRM: + { + cb_log(4, stack->port, "%% GOT L2 Activate Info port:%d\n",stack->port); + stack->l2link = 1; + + free_msg(msg); + return 0; + } + break; + + case DL_RELEASE | INDICATION: + case DL_RELEASE | CONFIRM: + { + cb_log(4, stack->port, "%% GOT L2 DeActivate Info port:%d\n",stack->port); + stack->l2link = 0; + + /** Clean the L3 here **/ + if (cb_clearl3_true()) + clear_l3(stack); + + free_msg(msg); + return 0; + } + break; + } + } + + + + { + /* Parse Events and fire_up to App. */ + struct misdn_bchannel *bc; + struct misdn_bchannel dummybc; + + enum event_e event = isdn_msg_get_event(msgs_g, msg, 1); + + bc=find_bc_by_l3id(stack, hh->dinfo); + + if (!bc) { + + cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on port:%d\n", hh->dinfo, stack->port); + memset (&dummybc,0,sizeof(dummybc)); + dummybc.stack=stack; + dummybc.l3_id=hh->dinfo; + bc=&dummybc; + } + if (bc ) { + isdn_msg_parse_event(msgs_g,msg,bc, 1); + + if(!isdn_get_info(msgs_g,event,1)) { + cb_log(4, bc->stack->port, "Unknown Event Ind: prim %x dinfo %x\n",hh->prim, hh->dinfo); + } else { + cb_event(event, bc, glob_mgr->user_data); + } + + + } else { + cb_log(4, stack->port, "No BC found with l3id: prim %x dinfo %x\n",hh->prim, hh->dinfo); + } + + free_msg(msg); + } + + + return 0; +} + + +int handle_timers(msg_t* msg) +{ + iframe_t *frm= (iframe_t*)msg->data; + struct misdn_stack *stack; + + /* Timer Stuff */ + switch (frm->prim) { + case MGR_INITTIMER | CONFIRM: + case MGR_ADDTIMER | CONFIRM: + case MGR_DELTIMER | CONFIRM: + case MGR_REMOVETIMER | CONFIRM: + free_msg(msg); + return(1); + } + + + + if (frm->prim==(MGR_TIMER | INDICATION) ) { + for (stack = glob_mgr->stack_list; + stack; + stack = stack->next) { + itimer_t *it; + + if (stack->mode != NT_MODE) continue; + + it = stack->nst.tlist; + /* find timer */ + for(it=stack->nst.tlist; + it; + it=it->next) { + if (it->id == (int)frm->addr) + break; + } + if (it) { + int ret; + ret = mISDN_write_frame(stack->midev, msg->data, frm->addr, + MGR_TIMER | RESPONSE, 0, 0, NULL, TIMEOUT_1SEC); + test_and_clear_bit(FLG_TIMER_RUNING, (long unsigned int *)&it->Flags); + ret = it->function(it->data); + free_msg(msg); + return 1; + } + } + + cb_log(0, 0, "Timer Msg without Timer ??\n"); + free_msg(msg); + return 1; + } + + return 0; +} + + + + +static int do_tone(struct misdn_bchannel *bc, int len) +{ + char buf[4096 + mISDN_HEADER_LEN]; + iframe_t *frm= (iframe_t*)buf; + int r; + + if (bc->tone == TONE_NONE) return 0; + + frm->prim = DL_DATA|REQUEST; + frm->dinfo = 0; + frm->addr = bc->addr | IF_DOWN; + + bc->tone_cnt+=len; + + if (bc->tone_cnt < TONE_425_SIZE) return 1; + + switch(bc->tone) { + case TONE_DIAL: + { + frm->len = TONE_425_SIZE; + memcpy(&buf[mISDN_HEADER_LEN], tone_425_flip,TONE_425_SIZE); + + r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC); + if (rlen) { + perror("Error written less than told bytes :(\n"); + } + } + break; + + case TONE_ALERTING: + bc->tone_cnt2++; + + if (bc->tone_cnt2 <= TONE_ALERT_CNT) { + frm->len = TONE_425_SIZE; + memcpy(&buf[mISDN_HEADER_LEN], tone_425_flip,TONE_425_SIZE); + r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC); + if (rlen) { + perror("Error written less than told bytes :(\n"); + } + } else if (bc->tone_cnt2 <= (TONE_ALERT_SILENCE_CNT)) { + frm->len = TONE_SILENCE_SIZE; + memcpy(&buf[mISDN_HEADER_LEN], tone_silence_flip ,TONE_SILENCE_SIZE); + r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC); + } else { + bc->tone_cnt2=-1; + } + break; + case TONE_BUSY: + bc->tone_cnt2++; + + if (bc->tone_cnt2 <= TONE_BUSY_CNT) { + frm->len = TONE_425_SIZE; + memcpy(&buf[mISDN_HEADER_LEN], tone_425_flip,TONE_425_SIZE); + r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC); + if (rlen) { + perror("Error written less than told bytes :(\n"); + } + } else if (bc->tone_cnt2 <= (TONE_BUSY_SILENCE_CNT)) { + frm->len = TONE_SILENCE_SIZE; + memcpy(&buf[mISDN_HEADER_LEN], tone_silence_flip ,TONE_SILENCE_SIZE); + r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC); + } else { + bc->tone_cnt2=-1; + } + break; + case TONE_FILE: + break; + case TONE_NONE: + return 0; + } + + bc->tone_cnt -= TONE_425_SIZE ; + return 1; +} + + + +int handle_bchan(msg_t *msg) +{ + iframe_t *frm= (iframe_t*)msg->data; + struct misdn_bchannel *bc; + + bc=find_bc_by_addr(frm->addr); + + if (!bc) return 0 ; + + switch (frm->prim) { + case PH_ACTIVATE | INDICATION: + case DL_ESTABLISH | INDICATION: + cb_log(4, bc->stack->port, "BCHAN: ACT Ind\n"); + free_msg(msg); + return 1; + + case PH_ACTIVATE | CONFIRM: + case DL_ESTABLISH | CONFIRM: + cb_log(4, bc->stack->port, "BCHAN: bchan ACT Confirm\n"); + free_msg(msg); + return 1; + + case PH_DEACTIVATE | INDICATION: + case DL_RELEASE | INDICATION: + cb_log (4, bc->stack->port, "BCHAN: DeACT Ind\n"); + free_msg(msg); + return 1; + + case PH_DEACTIVATE | CONFIRM: + case DL_RELEASE | CONFIRM: + cb_log(4, bc->stack->port, "BCHAN: DeACT Conf\n"); + free_msg(msg); + return 1; + + case PH_CONTROL|INDICATION: + { + unsigned long cont = *((unsigned long *)&frm->data.p); + + cb_log(4, bc->stack->port, "PH_CONTROL: port:%d channel:%d oad%d:%s dad%d:%s \n", bc->stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad); + + if ((cont&~DTMF_TONE_MASK) == DTMF_TONE_VAL) { + int dtmf = cont & DTMF_TONE_MASK; + cb_log(4, bc->stack->port, " --> DTMF TONE: %c\n",dtmf); + bc->dtmf=dtmf; + cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data); + + free_msg(msg); + return 1; + } + if (cont == BF_REJECT) { + cb_log(4, bc->stack->port, " --> BF REJECT\n"); + free_msg(msg); + return 1; + } + if (cont == BF_ACCEPT) { + cb_log(4, bc->stack->port, " --> BF ACCEPT\n"); + free_msg(msg); + return 1; + } + } + break; + + case PH_DATA|INDICATION: + case DL_DATA|INDICATION: + { + bc->bframe = (void*)&frm->data.i; + bc->bframe_len = frm->len; + + /** Anyway flip the bufbits **/ + flip_buf_bits(bc->bframe, bc->bframe_len); + + +#if MISDN_DEBUG + cb_log(0, bc->stack->port, "DL_DATA INDICATION Len %d\n", frm->len); +#endif + + if (bc->active && frm->len > 0) { + if ( !do_tone(bc, frm->len) ) { + + if ( misdn_cap_is_speech(bc->capability)) { + if ( !bc->nojitter ) { + char buf[4096 + mISDN_HEADER_LEN]; + iframe_t *txfrm= (iframe_t*)buf; + int len, r; + + len = ibuf_usedcount(bc->misdnbuf); + + if (len < frm->len) { + /** send nothing + * till we are synced + **/ + } else { + + txfrm->prim = DL_DATA|REQUEST; + txfrm->dinfo = 0; + txfrm->addr = bc->addr; /* | IF_DOWN; */ + txfrm->len = frm->len; + ibuf_memcpy_r(&buf[mISDN_HEADER_LEN], bc->misdnbuf,frm->len); + cb_log(9, bc->stack->port, "Transmitting %d samples 2 misdn\n", txfrm->len); + + r=mISDN_write(bc->stack->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 ); + + } + + } + } + + cb_event( EVENT_BCHAN_DATA, bc, glob_mgr->user_data); + } + } + free_msg(msg); + return 1; + } + + + case PH_DATA | CONFIRM: + case DL_DATA|CONFIRM: +#if MISDN_DEBUG + cb_log(0, bc->stack->port, "Data confirmed\n"); +#endif + free_msg(msg); + return 1; + break; + case DL_DATA|RESPONSE: +#if MISDN_DEBUG + cb_log(0, bc->stack->port, "Data response\n"); +#endif + break; + + case DL_DATA | REQUEST: + break; + } + + return 0; +} + + + +int handle_frm_nt(msg_t *msg) +{ + iframe_t *frm= (iframe_t*)msg->data; + struct misdn_stack *stack; + int err=0; + + stack=find_stack_by_addr((frm->addr & IF_ADDRMASK ) ); + + if (!stack || stack->mode != NT_MODE) { + return 0; + } + + + if ((err=stack->nst.l1_l2(&stack->nst,msg))) { + + if (nt_err_cnt > 0 ) { + if (nt_err_cnt < 100) { + nt_err_cnt++; + cb_log(0, stack->port, "NT Stack sends us error: %d port:%d\n", err,stack->port); + } else if (nt_err_cnt < 105){ + cb_log(0, stack->port, "NT Stack sends us error: %d port:%d over 100 times, so I'll stop this message\n", err,stack->port); + nt_err_cnt = - 1; + } + } + free_msg(msg); + return 1; + + } + + return 1; +} + + +int handle_frm(msg_t *msg) +{ + iframe_t *frm = (iframe_t*) msg->data; + struct misdn_stack *stack=find_stack_by_addr(frm->addr & IF_ADDRMASK); + + if (!stack || stack->mode != TE_MODE) { + return 0; + } + + { + struct misdn_bchannel *bc; + + if(handle_cr(frm)) { + free_msg(msg); + return 1; + } + + bc=find_bc_by_l3id(stack, frm->dinfo); + + if (bc ) { + enum event_e event = isdn_msg_get_event(msgs_g, msg, 0); + enum event_response_e response=RESPONSE_OK; + + isdn_msg_parse_event(msgs_g,msg,bc, 0); + + /** Preprocess some Events **/ + handle_event(bc, event, frm); + /* shoot up event to App: */ + cb_log(5, stack->port, "lib Got Prim: Addr %x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo); + + if(!isdn_get_info(msgs_g,event,0)) + cb_log(0, stack->port, "Unknown Event Ind: Addr:%x prim %x dinfo %x\n",frm->addr, frm->prim, frm->dinfo); + else + response=cb_event(event, bc, glob_mgr->user_data); +#if 1 + if (event == EVENT_SETUP) { + switch (response) { + case RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE: + cb_log(0, stack->port, "TOTALY IGNORING SETUP: port:%d\n", frm->addr&IF_CONTRMASK); + break; + case RESPONSE_IGNORE_SETUP: + /* I think we should send CC_RELEASE_CR, but am not sure*/ + empty_chan_in_stack(bc->stack, bc->channel); + empty_bc(bc); + + cb_log(0, stack->port, "GOT IGNORE SETUP: port:%d\n", frm->addr&IF_CONTRMASK); + break; + case RESPONSE_OK: + cb_log(4, stack->port, "GOT SETUP OK: port:%d\n", frm->addr&IF_CONTRMASK); + break; + default: + break; + } + } + + cb_log(5, stack->port, "Freeing Msg on prim:%x port:%d\n",frm->prim, frm->addr&IF_CONTRMASK); + free_msg(msg); + return 1; +#endif + + } else { + cb_log(0, stack->port, "NO BC FOR STACK: port:%d\n", frm->addr&IF_CONTRMASK); + } + + } + cb_log(4, stack->port, "TE_FRM_HANDLER: Returning 0 on prim:%x port:%d\n",frm->prim, frm->addr&IF_CONTRMASK); + + return 0; +} + + +int handle_l1(msg_t *msg) +{ + iframe_t *frm = (iframe_t*) msg->data; + struct misdn_stack *stack = find_stack_by_port(frm->addr & IF_CONTRMASK); + int i ; + + if (!stack) return 0 ; + + switch (frm->prim) { + case PH_ACTIVATE | CONFIRM: + case PH_ACTIVATE | INDICATION: + cb_log (1, stack->port, "L1: PH L1Link Up! port:%d\n",stack->port); + stack->l1link=1; + free_msg(msg); + + for (i=0;ib_num; i++) { + if (stack->bc[i].evq != EVENT_NOTHING) { + cb_log(4, stack->port, "Fireing Queued Event %s because L1 got up\n", isdn_get_info(msgs_g, stack->bc[i].evq, 0)); + misdn_lib_send_event(&stack->bc[i],stack->bc[i].evq); + stack->bc[i].evq=EVENT_NOTHING; + } + + } + + return 1; + case PH_DEACTIVATE | CONFIRM: + case PH_DEACTIVATE | INDICATION: + cb_log (1, stack->port, "L1: PH L1Link Down! port:%d\n",stack->port); + stack->l1link=0; + for (i=0; ib_num; i++) { + if (global_state == MISDN_INITIALIZED) { + cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data); + } + + } + free_msg(msg); + return 1; + } + + return 0; +} + +int handle_l2(msg_t *msg) +{ + iframe_t *frm = (iframe_t*) msg->data; + struct misdn_stack *stack = find_stack_by_addr(frm->addr & IF_ADDRMASK); + + if (!stack) return 0 ; + + switch(frm->prim) { + +#ifdef DL_STATUS + case DL_STATUS | INDICATION: + case DL_STATUS | CONFIRM: + cb_log (3, stack->port, "L1: DL_STATUS! port:%d\n", stack->port); + + switch (frm->dinfo) { + case SDL_ESTAB: + cb_log (4, stack->port, " --> SDL_ESTAB port:%d\n", stack->port); + stack->l1link=1; + goto dl_estab; + case SDL_REL: + cb_log (4, stack->port, " --> SDL_REL port:%d\n", stack->port); + stack->l1link=0; + misdn_lib_get_port_up(stack->port); + goto dl_rel; + } + break; +#endif + + + case DL_ESTABLISH | INDICATION: + case DL_ESTABLISH | CONFIRM: +#ifdef DL_STATUS + dl_estab: +#endif + { + cb_log (3, stack->port, "L2: L2Link Up! port:%d\n", stack->port); + stack->l2link=1; + free_msg(msg); + return 1; + } + break; + + case DL_RELEASE | INDICATION: + case DL_RELEASE | CONFIRM: +#ifdef DL_STATUS + dl_rel: +#endif + { + cb_log (3, stack->port, "L2: L2Link Down! port:%d\n", stack->port); + stack->l2link=0; + + if (cb_clearl3_true()) + clear_l3(stack); + + free_msg(msg); + return 1; + } + break; + } + return 0; +} + + +int handle_mgmt(msg_t *msg) +{ + iframe_t *frm = (iframe_t*) msg->data; + + if ( (frm->prim & 0x0f0000) == 0x0f0000) { + cb_log(5, 0, "$$$ MGMT FRAME: prim %x addr %x dinfo %x\n",frm->prim, frm->addr, frm->dinfo) ; + free_msg(msg); + return 1; + } + + return 0; +} + + +msg_t *fetch_msg(int midev) +{ + msg_t *msg=alloc_msg(MAX_MSG_SIZE); + int r; + fd_set rdfs; + + if (!msg) { + cb_log(0, 0, "fetch_msg: alloc msg failed !!"); + return NULL; + } + + FD_ZERO(&rdfs); + FD_SET(midev,&rdfs); + + mISDN_select(FD_SETSIZE, &rdfs, NULL, NULL, NULL); + + if (FD_ISSET(midev, &rdfs)) { + + r=mISDN_read(midev,msg->data,MAX_MSG_SIZE,0); + msg->len=r; + + if (r==0) { + free_msg(msg); /* danger, cauz usualy freeing in main_loop */ + printf ("Got empty Msg?\n"); + return NULL; + } + + return msg; + } else { + printf ("Select timeout\n"); + } + + return NULL; +} + + +static void misdn_lib_isdn_event_catcher(void *arg) +{ + struct misdn_lib *mgr = arg; + int zero_frm=0 , fff_frm=0 ; + int midev= mgr->midev; + int port; + + //cb_log(5, 0, "In event_catcher thread\n"); + + while (1) { + msg_t *msg = fetch_msg(midev); + iframe_t *frm; + + + if (!msg) continue; + + frm = (iframe_t*) msg->data; + port = frm->addr&IF_CONTRMASK; + + /** When we make a call from NT2Ast we get this frames **/ + if (frm->len == 0 && frm->addr == 0 && frm->dinfo == 0 && frm->prim == 0 ) { + zero_frm++; + free_msg(msg); + continue; + } else { + if (zero_frm) { + cb_log(0, port, "*** Alert: %d zero_frms caught\n", zero_frm); + zero_frm = 0 ; + } + } + + /** I get this sometimes after setup_bc **/ + if (frm->len == 0 && frm->dinfo == 0 && frm->prim == 0xffffffff ) { + fff_frm++; + free_msg(msg); + continue; + } else { + if (fff_frm) { + cb_log(0, port, "*** Alert: %d fff_frms caught\n", fff_frm); + fff_frm = 0 ; + } + } + + manager_isdn_handler(frm, msg); + } + +} + + +/** App Interface **/ + +int te_lib_init() { + char buff[1025]; + iframe_t *frm=(iframe_t*)buff; + int midev=mISDN_open(); + int ret; + + memset(buff,0,1025); + + if (midev<=0) return midev; + +/* create entity for layer 3 TE-mode */ + mISDN_write_frame(midev, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC); + ret = mISDN_read_frame(midev, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC); + + if (ret < mISDN_HEADER_LEN) { + noentity: + fprintf(stderr, "cannot request MGR_NEWENTITY from mISDN: %s\n",strerror(errno)); + exit(-1); + } + + entity = frm->dinfo & 0xffff ; + + if (!entity) + goto noentity; + + return midev; + +} + +void te_lib_destroy(int midev) +{ + char buf[1024]; + mISDN_write_frame(midev, buf, 0, MGR_DELENTITY | REQUEST, entity, 0, NULL, TIMEOUT_1SEC); + + cb_log(4, 0, "Entetity deleted\n"); + mISDN_close(midev); + cb_log(4, 0, "midev closed\n"); +} + + + +void misdn_lib_transfer(struct misdn_bchannel* holded_bc) +{ + holded_bc->holded=0; +} + +struct misdn_bchannel *manager_find_bc_by_pid(int pid) +{ + struct misdn_stack *stack; + int i; + + for (stack=glob_mgr->stack_list; + stack; + stack=stack->next) { + for (i=0; ib_num; i++) + if (stack->bc[i].pid == pid) return &stack->bc[i]; + } + + return NULL; +} + +struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc) +{ + return find_bc_holded(bc->stack); +} + + + +struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel) +{ + struct misdn_stack *stack; + int i; + + if (channel < 0 || channel > MAX_BCHANS) + return NULL; + + for (stack=glob_mgr->stack_list; stack; stack=stack->next) { + + if (stack->port == port) { + if (channel > 0) { + if (channel <= stack->b_num) { + for (i = 0; i < stack->b_num; i++) { + if (stack->bc[i].in_use && stack->bc[i].channel == channel) { + return NULL; + } + } + } else + return NULL; + } + for (i = 0; i < stack->b_num; i++) { + if (!stack->bc[i].in_use) { + stack->bc[i].channel = channel; + stack->bc[i].channel_preselected = channel?1:0; + stack->bc[i].in_use = 1; + return &stack->bc[i]; + } + } + return NULL; + } + } + return NULL; +} + +void misdn_lib_log_ies(struct misdn_bchannel *bc) +{ + if (!bc) return; + + cb_log(2, bc->stack->port, " --> mode:%s cause:%d ocause:%d rad:%s\n", bc->stack->mode==NT_MODE?"NT":"TE", bc->cause, bc->out_cause, bc->rad); + + cb_log(2, bc->stack->port, + " --> info_dad:%s onumplan:%c dnumplan:%c rnumplan:%c\n", + bc->info_dad, + bc->onumplan>=0?'0'+bc->onumplan:' ', + bc->dnumplan>=0?'0'+bc->dnumplan:' ', + bc->rnumplan>=0?'0'+bc->rnumplan:' ' + ); + + cb_log(2, bc->stack->port, " --> channel:%d caps:%s pi:%x keypad:%s\n", bc->channel, bearer2str(bc->capability),bc->progress_indicator, bc->keypad); + + cb_log(3, bc->stack->port, " --> pid:%d addr:%x l3id:%x\n", bc->pid, bc->addr, bc->l3_id); + + cb_log(4, bc->stack->port, " --> bc:%x h:%d sh:%d\n", bc, bc->holded, bc->stack_holder); +} + +int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event ) +{ + msg_t *msg; + int err = -1 ; + + if (!bc) goto ERR; + + if ( bc->stack->mode == NT_MODE && !bc->stack->l1link) { + /** Queue Event **/ + bc->evq=event; + cb_log(1, bc->stack->port, "Queueing Event %s because L1 is down (btw. Activating L1)\n", isdn_get_info(msgs_g, event, 0)); + { /* Pull Up L1 */ + iframe_t act; + act.prim = PH_ACTIVATE | REQUEST; + act.addr = (bc->stack->upper_id & IF_ADDRMASK) | IF_DOWN ; + act.dinfo = 0; + act.len = 0; + mISDN_write(glob_mgr->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC); + } + + return 0; + } + + cb_log(1, bc->stack->port, "I SEND:%s oad:%s dad:%s port:%d\n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad, bc->stack->port); + misdn_lib_log_ies(bc); + + switch (event) { + case EVENT_SETUP: + if (create_process(glob_mgr->midev, bc)<0) { + cb_log(0, bc->stack->port, " No free channel at the moment @ send_event\n"); + err=-ENOCHAN; + goto ERR; + } + break; + + case EVENT_CONNECT: + case EVENT_PROGRESS: + case EVENT_ALERTING: + case EVENT_PROCEEDING: + case EVENT_SETUP_ACKNOWLEDGE: + case EVENT_RETRIEVE_ACKNOWLEDGE: + + if (bc->stack->mode == NT_MODE) { + if (bc->channel <=0 ) { /* else we have the channel already */ + bc->channel = find_free_chan_in_stack(bc->stack, 0); + if (!bc->channel) { + cb_log(0, bc->stack->port, " No free channel at the moment\n"); + err=-ENOCHAN; + goto ERR; + } + } + /* Its that i generate channels */ + } + + setup_bc(bc); + + + if ( event == EVENT_CONNECT ) { + if ( *bc->crypt_key ) { + cb_log(4, bc->stack->port, " --> ENABLING BLOWFISH port:%d channel:%d oad%d:%s dad%d:%s \n", bc->stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad); + + manager_ph_control_block(bc, BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) ); + } + + if ( misdn_cap_is_speech(bc->capability)) { + if (!bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0); + + if (bc->ec_enable) manager_ec_enable(bc); + + if (bc->txgain != 0) { + cb_log(4, bc->stack->port, "--> Changing txgain to %d\n", bc->txgain); + manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain); + } + + if ( bc->rxgain != 0 ) { + cb_log(4, bc->stack->port, "--> Changing rxgain to %d\n", bc->rxgain); + manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain); + } + } + } + + if (event == EVENT_RETRIEVE_ACKNOWLEDGE) { + manager_bchannel_activate(bc); + } + + break; + + case EVENT_HOLD_ACKNOWLEDGE: + { + struct misdn_bchannel *holded_bc=malloc(sizeof(struct misdn_bchannel)); + + memcpy(holded_bc,bc,sizeof(struct misdn_bchannel)); + + holded_bc->holded=1; + stack_holder_add(bc->stack,holded_bc); + + if (bc->stack->mode == NT_MODE) { + empty_chan_in_stack(bc->stack,bc->channel); + empty_bc(bc); + clean_up_bc(bc); + } + + /** we set it up later at RETRIEVE_ACK again.**/ + holded_bc->upset=0; + holded_bc->active=0; + + cb_event( EVENT_NEW_BC, holded_bc, glob_mgr->user_data); + } + break; + + case EVENT_RELEASE: + break; + + case EVENT_RELEASE_COMPLETE: + empty_chan_in_stack(bc->stack,bc->channel); + empty_bc(bc); + clean_up_bc(bc); + break; + + case EVENT_CONNECT_ACKNOWLEDGE: + + if (misdn_cap_is_speech(bc->capability)) { + if ( !bc->nodsp) manager_ph_control(bc, DTMF_TONE_START, 0); + if (bc->ec_enable) manager_ec_enable(bc); + if ( bc->txgain != 0 ) { + cb_log(4, bc->stack->port, "--> Changing txgain to %d\n", bc->txgain); + manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain); + } + if ( bc->rxgain != 0 ) { + cb_log(4, bc->stack->port, "--> Changing rxgain to %d\n", bc->rxgain); + manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain); + } + } + break; + + default: + break; + } + + /* Later we should think about sending bchannel data directly to misdn. */ + msg = isdn_msg_build_event(msgs_g, bc, event, bc->stack->mode==NT_MODE?1:0); + msg_queue_tail(&bc->stack->downqueue, msg); + sem_post(&glob_mgr->new_msg); + + return 0; + + ERR: + return -1; +} + + + +int manager_isdn_handler(iframe_t *frm ,msg_t *msg) +{ + + if (frm->dinfo==(signed long)0xffffffff && frm->prim==(PH_DATA|CONFIRM)) { + printf("SERIOUS BUG, dinfo == 0xffffffff, prim == PH_DATA | CONFIRM !!!!\n"); + } + + if (handle_timers(msg)) + return 0 ; + + if (handle_mgmt(msg)) + return 0 ; + + if (handle_l2(msg)) + return 0 ; + + /* Its important to handle l1 AFTER l2 */ + if (handle_l1(msg)) + return 0 ; + + if (handle_bchan(msg)) + return 0 ; + + /** Handle L2/3 Signalling after bchans **/ + if (handle_frm_nt(msg)) + return 0 ; + + if (handle_frm(msg)) + return 0 ; + + cb_log(0, frm->addr&IF_CONTRMASK, "Unhandled Message: prim %x len %d from addr %x, dinfo %x on port: %d\n",frm->prim, frm->len, frm->addr, frm->dinfo, frm->addr&IF_CONTRMASK); + + free_msg(msg); + + return 0; +} + + + + +int misdn_lib_get_port_info(int port) +{ + msg_t *msg=alloc_msg(MAX_MSG_SIZE); + iframe_t *frm; + struct misdn_stack *stack=find_stack_by_port(port); + if (!msg) { + cb_log(0, port, "misgn_lib_get_port: alloc_msg failed!\n"); + return -1; + } + frm=(iframe_t*)msg->data; + if (!stack ) { + cb_log(0, port, "There is no Stack on Port:%d\n",port); + return -1; + } + /* activate bchannel */ + frm->prim = CC_STATUS_ENQUIRY | REQUEST; + frm->addr = stack->upper_id; + frm->dinfo = 0; + frm->len = 0; + + msg_queue_tail(&glob_mgr->activatequeue, msg); + sem_post(&glob_mgr->new_msg); + + + return 0; +} + +int misdn_lib_port_restart(int port) +{ + struct misdn_stack *stack=find_stack_by_port(port); + + cb_log(0, port, "Restarting Port:%d\n",port); + if (stack) { + cb_log(0, port, "Stack:%p\n",stack); + + + clear_l3(stack); + + { + msg_t *msg=alloc_msg(MAX_MSG_SIZE); + iframe_t *frm; + + if (!msg) { + cb_log(0, port, "port_restart: alloc_msg fialed\n"); + return -1; + } + + frm=(iframe_t*)msg->data; + /* we must activate if we are deactivated */ + /* activate bchannel */ + + frm->prim = DL_RELEASE | REQUEST; + + frm->addr = stack->upper_id ; + frm->dinfo = 0; + frm->len = 0; + msg_queue_tail(&glob_mgr->activatequeue, msg); + sem_post(&glob_mgr->new_msg); + } + return 0; + + stack_te_destroy(stack); + + { + struct misdn_stack *tmpstack; + struct misdn_stack *newstack=stack_te_init(stack->midev ,port, stack->ptp); + + + if (stack == glob_mgr->stack_list) { + struct misdn_stack *n=glob_mgr->stack_list->next; + glob_mgr->stack_list = newstack ; + glob_mgr->stack_list->next = n; + } else { + for (tmpstack=glob_mgr->stack_list; + tmpstack->next; + tmpstack=tmpstack->next) + if (tmpstack->next == stack) break; + + if (!tmpstack->next) { + cb_log(0, port, "Stack to restart not found\n"); + return 0; + } else { + struct misdn_stack *n=tmpstack->next->next; + tmpstack->next=newstack; + newstack->next=n; + } + } + + { + int i; + for(i=0;ib_num; i++) { + int r; + if ((r=init_bc(newstack, &newstack->bc[i], newstack->midev,port,i, "", 1))<0) { + cb_log(0, port, "Got Err @ init_bc :%d\n",r); + return 0; + } + } + } + + free(stack); + } + } + + return 0; +} + + + +sem_t handler_started; + +void manager_event_handler(void *arg) +{ + sem_post(&handler_started); + while (1) { + struct misdn_stack *stack; + msg_t *msg; + + /** wait for events **/ + sem_wait(&glob_mgr->new_msg); + + for (msg=msg_dequeue(&glob_mgr->activatequeue); + msg; + msg=msg_dequeue(&glob_mgr->activatequeue) + ) + { + + iframe_t *frm = (iframe_t*) msg->data ; + + switch ( frm->prim) { + case MGR_SETSTACK | REQUEST : + break; + default: + mISDN_write(glob_mgr->midev, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC); + free_msg(msg); + } + } + + for (stack=glob_mgr->stack_list; + stack; + stack=stack->next ) { + while ( (msg=msg_dequeue(&stack->downqueue)) ) { + + if (stack->mode == NT_MODE ){ + + if (stack->nst.manager_l3(&stack->nst, msg)) + cb_log(0, stack->port, "Error@ Sending Message in NT-Stack.\n"); + + } else { + if (msg) { + iframe_t *frm = (iframe_t *)msg->data; + struct misdn_bchannel *bc = find_bc_by_l3id(stack, frm->dinfo); + + if (bc) send_msg(glob_mgr->midev, bc, msg); + } + } + } + } + } +} + + +int misdn_lib_maxports_get() { /** BE AWARE WE HAVE NO CB_LOG HERE! **/ + + int i = mISDN_open(); + int max=0; + + if (i<0) + return -1; + + max = mISDN_get_stack_count(i); + + mISDN_close(i); + + return max; +} + +int misdn_lib_init(char *portlist, struct misdn_lib_iface *iface, void *user_data) +{ + struct misdn_lib *mgr=calloc(1, sizeof(struct misdn_lib)); + char *tok, *tokb; + char plist[1024]; + int midev; + int port_count=0; + + cb_log = iface->cb_log; + cb_event = iface->cb_event; + cb_clearl3_true = iface->cb_clearl3_true; + + glob_mgr = mgr; + + msg_init(); + debug_init(0 , NULL, NULL, NULL); + + if (!portlist || (*portlist == 0) ) return 1; + + init_flip_bits(); + + { + strncpy(plist,portlist, 1024); + plist[1023] = 0; + } + + memcpy(tone_425_flip,tone_425,TONE_425_SIZE); + flip_buf_bits(tone_425_flip,TONE_425_SIZE); + + memcpy(tone_silence_flip,tone_SILENCE,TONE_SILENCE_SIZE); + flip_buf_bits(tone_silence_flip,TONE_SILENCE_SIZE); + + midev=te_lib_init(); + mgr->midev=midev; + + port_count=mISDN_get_stack_count(midev); + + msg_queue_init(&mgr->activatequeue); + + if (sem_init(&mgr->new_msg, 1, 0)<0) + sem_init(&mgr->new_msg, 0, 0); + + for (tok=strtok_r(plist," ,",&tokb ); + tok; + tok=strtok_r(NULL," ,",&tokb)) { + int port = atoi(tok); + struct misdn_stack *stack; + static int first=1; + int ptp=0; + + if (strstr(tok, "ptp")) + ptp=1; + + if (port > port_count) { + cb_log(0, port, "Couldn't Initialize Port:%d since we have only %d ports\n",port, port_count); + exit(1); + } + stack=stack_te_init(midev, port, ptp); + + + + if (!stack) { + perror("init_stack"); + exit(1); + } + + if (stack && first) { + mgr->stack_list=stack; + first=0; + { + int i; + for(i=0;ib_num; i++) { + int r; + if ((r=init_bc(stack, &stack->bc[i], stack->midev,port,i, "", 1))<0) { + cb_log(0, port, "Got Err @ init_bc :%d\n",r); + exit(1); + } + } + } + + continue; + } + + if (stack) { + struct misdn_stack * help; + for ( help=mgr->stack_list; help; help=help->next ) + if (help->next == NULL) break; + + + help->next=stack; + + { + int i; + for(i=0;ib_num; i++) { + int r; + if ((r=init_bc(stack, &stack->bc[i], stack->midev,port,i, "",1 ))<0) { + cb_log(0, port, "Got Err @ init_bc :%d\n",r); + exit(1); + } + } + } + } + + } + + if (sem_init(&handler_started, 1, 0)<0) + sem_init(&handler_started, 0, 0); + + cb_log(4, 0, "Starting Event Handler\n"); + pthread_create( &mgr->event_handler_thread, NULL,(void*)manager_event_handler, mgr); + + sem_wait(&handler_started) ; + cb_log(4, 0, "Starting Event Catcher\n"); + pthread_create( &mgr->event_thread, NULL, (void*)misdn_lib_isdn_event_catcher, mgr); + + cb_log(4, 0, "Event Catcher started\n"); + + global_state= MISDN_INITIALIZED; + + return (mgr == NULL); +} + + + +void misdn_lib_destroy() +{ + struct misdn_stack *help; + int i; + + for ( help=glob_mgr->stack_list; help; help=help->next ) { + for(i=0;ib_num; i++) { + char buf[1024]; + + mISDN_write_frame(help->midev, buf, help->bc[i].addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC); + help->bc[i].addr = 0; + } + + + cb_log (1, help->port, "Destroying port:%d\n", help->port); + stack_te_destroy(help); + } + + if (global_state == MISDN_INITIALIZED) { + cb_log(4, 0, "Killing Handler Thread\n"); + if ( pthread_cancel(glob_mgr->event_handler_thread) == 0 ) { + cb_log(4, 0, "Joining Handler Thread\n"); + pthread_join(glob_mgr->event_handler_thread, NULL); + } + + + cb_log(4, 0, "Killing Main Thread\n"); + if ( pthread_cancel(glob_mgr->event_thread) == 0 ) { + cb_log(4, 0, "Joining Main Thread\n"); + pthread_join(glob_mgr->event_thread, NULL); + } + } + + cb_log(1, 0, "Closing mISDN device\n"); + te_lib_destroy(glob_mgr->midev); +} + + +char *manager_isdn_get_info(enum event_e event) +{ + return isdn_get_info(msgs_g , event, 0); +} + + + + +void manager_bchannel_activate(struct misdn_bchannel *bc) +{ + msg_t *msg=alloc_msg(MAX_MSG_SIZE); + iframe_t *frm; + + if (!msg) { + cb_log(0, bc->stack->port, "bchannel_activate: alloc_msg failed !"); + return ; + } + + frm=(iframe_t*)msg->data; + /* we must activate if we are deactivated */ + clear_ibuffer(bc->misdnbuf); + clear_ibuffer(bc->astbuf); + + + if (bc->active) return; + + cb_log(5, bc->stack->port, "$$$ Bchan Activated addr %x\n", bc->addr); + + /* activate bchannel */ + frm->prim = DL_ESTABLISH | REQUEST; + frm->addr = bc->addr ; + frm->dinfo = 0; + frm->len = 0; + + msg_queue_tail(&glob_mgr->activatequeue, msg); + sem_post(&glob_mgr->new_msg); + + bc->active=1; + + return ; + +} + + +void manager_bchannel_deactivate(struct misdn_bchannel * bc) +{ + iframe_t dact; + + + if (!bc->active) return; + + cb_log(5, bc->stack->port, "$$$ Bchan deActivated addr %x\n", bc->addr); + + bc->tone=TONE_NONE; + + dact.prim = DL_RELEASE | REQUEST; + dact.addr = bc->addr; + dact.dinfo = 0; + dact.len = 0; + + mISDN_write(bc->stack->midev, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC); + clear_ibuffer(bc->misdnbuf); + clear_ibuffer(bc->astbuf); + bc->active=0; + + return; +} + + +int manager_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len) +{ + + if (!bc->active) return -1; + + flip_buf_bits(data,len); + + if ( !bc->nojitter && misdn_cap_is_speech(bc->capability) ) { + if (len > ibuf_freecount(bc->misdnbuf)) { + len=ibuf_freecount(bc->misdnbuf); + } + ibuf_memcpy_w(bc->misdnbuf, (unsigned char*)data, len); + } else { + char buf[4096 + mISDN_HEADER_LEN]; + iframe_t *frm= (iframe_t*)buf; + int r; + + frm->prim = DL_DATA|REQUEST; + frm->dinfo = 0; + frm->addr = bc->addr | IF_DOWN; + frm->len = len; + memcpy(&buf[mISDN_HEADER_LEN], data,len); + + + + if ( misdn_cap_is_speech(bc->capability)) + cb_log(4, bc->stack->port, "Writing %d bytes\n",len); + + cb_log(9, bc->stack->port, "Wrinting %d bytes 2 mISDN\n",len); + r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT); + } + + return 0; +} + + + + +void manager_send_tone (struct misdn_bchannel *bc, enum tone_e tone) +{ + if (tone != TONE_NONE) manager_bchannel_activate(bc); + bc->tone=tone; + bc->tone_cnt2=-1; + bc->tone_cnt=0; +} + + + +/* + * send control information to the channel (dsp-module) + */ +void manager_ph_control(struct misdn_bchannel *bc, int c1, int c2) +{ + unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+sizeof(int)]; + iframe_t *ctrl = (iframe_t *)buffer; /* preload data */ + unsigned long *d = (unsigned long *)&ctrl->data.p; + + ctrl->prim = PH_CONTROL | REQUEST; + ctrl->addr = bc->addr; + ctrl->dinfo = 0; + ctrl->len = sizeof(unsigned long)*2; + *d++ = c1; + *d++ = c2; + mISDN_write(bc->stack->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC); +} + +/* + * send control information to the channel (dsp-module) + */ +void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c2_len) +{ + unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len]; + iframe_t *ctrl = (iframe_t *)buffer; + unsigned long *d = (unsigned long *)&ctrl->data.p; + + ctrl->prim = PH_CONTROL | REQUEST; + ctrl->addr = bc->addr; + ctrl->dinfo = 0; + ctrl->len = sizeof(unsigned long) + c2_len; + *d++ = c1; + memcpy(d, c2, c2_len); + mISDN_write(bc->stack->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC); +} + + + + +void manager_clean_bc(struct misdn_bchannel *bc ) +{ + if (bc->state == STATE_CONNECTED) + misdn_lib_send_event(bc,EVENT_DISCONNECT); + + empty_chan_in_stack(bc->stack, bc->channel); + empty_bc(bc); + + misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE); +} + + +void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder) +{ + struct misdn_bchannel *help; + + cb_log(4,holder->stack->port, "*HOLDER: add %x\n",holder->l3_id); + + holder->stack_holder=1; + + if (!stack ) return ; + + holder->next=NULL; + + if (!stack->holding) { + stack->holding = holder; + return; + } + + for (help=stack->holding; + help; + help=help->next) { + if (!help->next) { + help->next=holder; + } + } + +} + +void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holder) +{ + struct misdn_bchannel *h1; + + if (!holder->stack_holder) return; + + cb_log(4,holder->stack->port, "*HOLDER: remove %x\n",holder->l3_id); + if (!stack || ! stack->holding) return; + + if (holder == stack->holding) { + stack->holding = stack->holding->next; + return; + } + + for (h1=stack->holding; + h1; + h1=h1->next) { + if (h1->next == holder) { + h1->next=h1->next->next; + return ; + } + } +} + + +struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned long l3id) +{ + struct misdn_bchannel *help; + + cb_log(4,stack?stack->port:0, "*HOLDER: find %x\n",l3id); + + if (!stack) return NULL; + + for (help=stack->holding; + help; + help=help->next) { + if (help->l3_id == l3id) { + cb_log(4,stack->port, "*HOLDER: found bc\n"); + return help; + } + } + + cb_log(4,stack->port, "*HOLDER: find nothing\n"); + return NULL; +} + + + +void manager_ec_enable(struct misdn_bchannel *bc) +{ + int ec_arr[2]; + + cb_log(1, bc?bc->stack->port:0,"Sending Control ECHOCAN_ON enblock\n"); + + switch (bc->ec_deftaps) { + case 4: + case 8: + case 16: + case 32: + case 64: + case 128: + case 256: + case 512: + case 1024: + cb_log(4, bc->stack->port, "Taps is %d\n",bc->ec_deftaps); + break; + default: + cb_log(0, bc->stack->port, "Taps should be power of 2\n"); + bc->ec_deftaps=128; + } + + ec_arr[0]=bc->ec_deftaps; + ec_arr[1]=bc->ec_training; + + manager_ph_control_block(bc, ECHOCAN_ON, ec_arr, sizeof(ec_arr)); +} + + +void manager_ec_disable(struct misdn_bchannel *bc) +{ + cb_log(1, bc?bc->stack->port:0, "Sending Control ECHOCAN_OFF\n"); + manager_ph_control(bc, ECHOCAN_OFF, 0); +} + +struct misdn_stack* get_misdn_stack() { + return glob_mgr->stack_list; +} diff -u -N -r ast_clean/channels/misdn/isdn_lib.h ast_misdn/channels/misdn/isdn_lib.h --- ast_clean/channels/misdn/isdn_lib.h 1970-01-01 01:00:00.000000000 +0100 +++ ast_misdn/channels/misdn/isdn_lib.h 2005-10-17 19:31:23.000000000 +0200 @@ -0,0 +1,439 @@ +/* + * Chan_Misdn -- Channel Driver for Asterisk + * + * Interface to mISDN + * + * Copyright (C) 2004, Christian Richter + * + * Christian Richter + * + * This program is free software, distributed under the terms of + * the GNU General Public License + */ + +#ifndef TE_LIB +#define TE_LIB + +#include +#include +#include +#include + +#include + +#ifndef mISDNUSER_HEAD_SIZE + +#ifdef MISDNUSER_JOLLY +#define mISDNUSER_HEAD_SIZE (sizeof(mISDNuser_head_t)) +#else +#define mISDNUSER_HEAD_SIZE (sizeof(mISDN_head_t)) +#endif +#endif + +#define MISDN_ASTERISK_TECH_PVT(ast) ast->tech_pvt +#define MISDN_ASTERISK_PVT(ast) 1 +#define MISDN_ASTERISK_TYPE(ast) ast->tech->type + + +/* #include "ies.h" */ + +#define MAX_BCHANS 30 + + +/** For initialization usage **/ +/* typedef int ie_nothing_t ;*/ +/** end of init usage **/ + + +enum bc_state_e { + STATE_NOTHING=0, + STATE_NULL, + STATE_CALL_INIT, + STATE_CONNECTED, + STATE_HOLD_ACKNOWLEDGE +}; + + +enum tone_e { + TONE_NONE=0, + TONE_DIAL, + TONE_ALERTING, + TONE_BUSY, + TONE_FILE +}; + +enum misdn_err_e { + ENOCHAN=1 +}; + + + +enum mISDN_NUMBER_PLAN { + NUMPLAN_UNINITIALIZED=-1, + NUMPLAN_INTERNATIONAL=0x1, + NUMPLAN_NATIONAL=0x2, + NUMPLAN_SUBSCRIBER=0x4, + NUMPLAN_UNKNOWN=0x0 +}; + + +enum event_response_e { + RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE, + RESPONSE_IGNORE_SETUP, + RESPONSE_ERR, + RESPONSE_OK +}; + + + +enum event_e { + EVENT_NOTHING, + EVENT_BCHAN_DATA, + EVENT_CLEANUP, + EVENT_PROCEEDING, + EVENT_PROGRESS, + EVENT_SETUP, + EVENT_ALERTING, + EVENT_CONNECT, + EVENT_SETUP_ACKNOWLEDGE, + EVENT_CONNECT_ACKNOWLEDGE , + EVENT_USER_INFORMATION, + EVENT_SUSPEND_REJECT, + EVENT_RESUME_REJECT, + EVENT_HOLD, + EVENT_SUSPEND, + EVENT_RESUME, + EVENT_HOLD_ACKNOWLEDGE, + EVENT_SUSPEND_ACKNOWLEDGE, + EVENT_RESUME_ACKNOWLEDGE, + EVENT_HOLD_REJECT, + EVENT_RETRIEVE, + EVENT_RETRIEVE_ACKNOWLEDGE, + EVENT_RETRIEVE_REJECT, + EVENT_DISCONNECT, + EVENT_RESTART, + EVENT_RELEASE, + EVENT_RELEASE_COMPLETE, + EVENT_FACILITY, + EVENT_NOTIFY, + EVENT_STATUS_ENQUIRY, + EVENT_INFORMATION, + EVENT_STATUS, + EVENT_TIMEOUT, + EVENT_DTMF_TONE, + EVENT_NEW_L3ID, + EVENT_NEW_BC, + EVENT_UNKNOWN +}; + + +enum ie_name_e { + IE_DUMMY, + IE_LAST +}; + +enum { /* bearer capability */ + INFO_CAPABILITY_SPEECH=0, + INFO_CAPABILITY_AUDIO_3_1K=0x10 , + INFO_CAPABILITY_AUDIO_7K=0x11 , + INFO_CAPABILITY_VIDEO =0x18, + INFO_CAPABILITY_DIGITAL_UNRESTRICTED =0x8, + INFO_CAPABILITY_DIGITAL_RESTRICTED =0x09, + INFO_CAPABILITY_DIGITAL_UNRESTRICTED_TONES +}; + +enum { /* progress indicators */ + INFO_PI_CALL_NOT_E2E_ISDN =0x01, + INFO_PI_CALLED_NOT_ISDN =0x02, + INFO_PI_CALLER_NOT_ISDN =0x03, + INFO_PI_CALLER_RETURNED_TO_ISDN =0x04, + INFO_PI_INBAND_AVAILABLE =0x08, + INFO_PI_DELAY_AT_INTERF =0x0a, + INFO_PI_INTERWORKING_WITH_PUBLIC =0x10, + INFO_PI_INTERWORKING_NO_RELEASE =0x11, + INFO_PI_INTERWORKING_NO_RELEASE_PRE_ANSWER =0x12, + INFO_PI_INTERWORKING_NO_RELEASE_POST_ANSWER =0x13 +}; + +enum { /*CODECS*/ + INFO_CODEC_ULAW=2, + INFO_CODEC_ALAW=3 +}; + + +enum layer_e { + L3, + L2, + L1, + UNKNOWN +}; + +enum facility_type { + FACILITY_NONE, + FACILITY_CALLDEFLECT +}; + +struct misdn_bchannel { + /** init stuff **/ + int b_stid; + /* int b_addr; */ + int layer_id; + + /** var stuff**/ + int l3_id; + int pid; + int ces; + + int channel; + int channel_preselected; + + int in_use; + int addr; + + unsigned char * bframe; + int bframe_len; + int time_usec; + + sem_t astsem; + sem_t misdnsem; + ibuffer_t *astbuf; + ibuffer_t *misdnbuf; + + /* dtmf digit */ + int dtmf; + int send_dtmf; + + /* wether we should use jollys dsp or not */ + int nodsp; + + /* wether we should use our jitter buf system or not */ + int nojitter; + + enum mISDN_NUMBER_PLAN dnumplan; + enum mISDN_NUMBER_PLAN rnumplan; + enum mISDN_NUMBER_PLAN onumplan; + + int progress_coding; + int progress_location; + int progress_indicator; + + enum facility_type facility; + char facility_calldeflect_nr[15]; + + enum event_e evq; + + /*** CRYPTING STUFF ***/ + + int crypt; + int curprx; + int curptx; + char crypt_key[255]; + + int crypt_state; + + /*char ast_dtmf_buf[255]; + char misdn_dtmf_buf[255]; */ + + /*** CRYPTING STUFF END***/ + + int active; + int upset; + + enum tone_e tone; + int tone_cnt; + int tone_cnt2; + + enum bc_state_e state; + + int holded; + int stack_holder; + + int pres; + + int nohdlc; + + int capability; + int law; + /** V110 Stuff **/ + int rate; + int mode; + + int user1; + int urate; + int async; + /* V110 */ + + unsigned char display[84]; + unsigned char msn[32]; + unsigned char oad[32]; + unsigned char rad[32]; + unsigned char dad[32]; + unsigned char orig_dad[32]; + unsigned char keypad[32]; + + unsigned char info_dad[64]; + unsigned char infos_pending[64]; + unsigned char info_keypad[32]; + unsigned char clisub[24]; + unsigned char cldsub[24]; + unsigned char fac[132]; + unsigned char uu[256]; + + int cause; + int out_cause; + + /* struct misdn_bchannel hold_bc; */ + + /** list stuf **/ + + int ec_enable; + int ec_deftaps; + int ec_whenbridged; + int ec_training; + + int orig; + + int txgain; + int rxgain; + + struct misdn_bchannel *next; + struct misdn_stack *stack; +}; + +struct misdn_stack { + /** is first element because &nst equals &mISDNlist **/ + net_stack_t nst; + manager_t mgr; + + int d_stid; + + int b_num; + + int b_stids[MAX_BCHANS + 1]; + + int ptp; + int lower_id; + int upper_id; + + int l2link; + + time_t l2establish; + + int l1link; + int midev; + + enum mode_e {NT_MODE, TE_MODE} mode; + int pri; + + + int procids[0x100+1]; + + msg_queue_t downqueue; + int busy; + + int port; + struct misdn_bchannel bc[MAX_BCHANS + 1]; + + struct misdn_bchannel* bc_list; + + int channels[MAX_BCHANS + 1]; + + + + int te_choose_channel; + + + struct misdn_bchannel *holding; /* Queue which holds holded channels :) */ + + struct misdn_stack *next; +}; + +struct misdn_stack* get_misdn_stack( void ); + +enum event_response_e (*cb_event) (enum event_e event, struct misdn_bchannel *bc, void *user_data); +void (*cb_log) (int level, int port, char *tmpl, ...); +int (*cb_clearl3_true)(void); + +struct misdn_lib_iface { + + enum event_response_e (*cb_event)(enum event_e event, struct misdn_bchannel *bc, void *user_data); + void (*cb_log)(int level, int port, char *tmpl, ...); + int (*cb_clearl3_true)(void); +}; + +/***** USER IFACE **********/ + +int misdn_lib_init(char *portlist, struct misdn_lib_iface* iface, void *user_data); +int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event ); +void misdn_lib_destroy(void); + +void misdn_lib_log_ies(struct misdn_bchannel *bc); + +char *manager_isdn_get_info(enum event_e event); + +void misdn_lib_transfer(struct misdn_bchannel* holded_bc); + +struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel); + +void manager_bchannel_activate(struct misdn_bchannel *bc); +void manager_bchannel_deactivate(struct misdn_bchannel * bc); +int manager_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len); +void manager_send_tone (struct misdn_bchannel *bc, enum tone_e tone); + +void manager_ph_control(struct misdn_bchannel *bc, int c1, int c2); + + +int misdn_lib_port_restart(int port); +int misdn_lib_get_port_info(int port); + +int misdn_lib_port_up(int port); + +int misdn_lib_get_port_up (int port) ; + +int misdn_lib_maxports_get(void) ; + +void misdn_lib_release(struct misdn_bchannel *bc); + +int misdn_cap_is_speech(int cap); +int misdn_inband_avail(struct misdn_bchannel *bc); + +int misdn_lib_send_facility(struct misdn_bchannel *bc, enum facility_type fac, void *data); + + +struct isdn_msg { + unsigned long misdn_msg; + + enum layer_e layer; + enum event_e event; + + void (*msg_parser)(struct isdn_msg *msgs, msg_t *msg, struct misdn_bchannel *bc, int nt); + msg_t *(*msg_builder)(struct isdn_msg *msgs, struct misdn_bchannel *bc, int nt); + void (*msg_printer)(struct isdn_msg *msgs); + + char *info; + +} ; + + + + + + + + +void manager_ec_enable(struct misdn_bchannel *bc); +void manager_ec_disable(struct misdn_bchannel *bc); + + +/* for isdn_msg_parser.c */ +msg_t *create_l3msg(int prim, int mt, int dinfo , int size, int nt); + + + +#define PRI_TRANS_CAP_SPEECH 0x0 +#define PRI_TRANS_CAP_DIGITAL 0x08 +#define PRI_TRANS_CAP_RESTRICTED_DIGITAL 0x09 +#define PRI_TRANS_CAP_3_1K_AUDIO 0x10 +#define PRI_TRANS_CAP_7K_AUDIO 0x11 + +#endif diff -u -N -r ast_clean/channels/misdn/isdn_msg_parser.c ast_misdn/channels/misdn/isdn_msg_parser.c --- ast_clean/channels/misdn/isdn_msg_parser.c 1970-01-01 01:00:00.000000000 +0100 +++ ast_misdn/channels/misdn/isdn_msg_parser.c 2005-10-17 19:31:23.000000000 +0200 @@ -0,0 +1,1352 @@ +/* + * Chan_Misdn -- Channel Driver for Asterisk + * + * Interface to mISDN + * + * Copyright (C) 2004, Christian Richter + * + * Christian Richter + * + * This program is free software, distributed under the terms of + * the GNU General Public License + */ + + +#include "isdn_lib.h" +#include "ie.c" + + +void parse_proceeding (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + CALL_PROCEEDING_t *proceeding=(CALL_PROCEEDING_t*)((unsigned long)msg->data+ HEADER_LEN); + { + int exclusive, channel; + dec_ie_channel_id(proceeding->CHANNEL_ID, (Q931_info_t *)proceeding, &exclusive, &channel, nt,bc); + + if (channel==0xff) /* any channel */ + channel=-1; + + /* ALERT: is that everytime true ? */ + if (channel > 0 && bc->stack->mode == NT_MODE) + bc->channel = channel; + } + + dec_ie_progress(proceeding->PROGRESS, (Q931_info_t *)proceeding, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc); + +#if DEBUG + printf("Parsing PROCEEDING Msg\n"); +#endif + + +} +msg_t *build_proceeding (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + CALL_PROCEEDING_t *proceeding; + msg_t *msg =(msg_t*)create_l3msg(CC_PROCEEDING | REQUEST, MT_CALL_PROCEEDING, bc?bc->l3_id:-1, sizeof(CALL_PROCEEDING_t) ,nt); + + proceeding=(CALL_PROCEEDING_t*)((msg->data+HEADER_LEN)); + + enc_ie_channel_id(&proceeding->CHANNEL_ID, msg, 1,bc->channel, nt,bc); + + if (nt) + enc_ie_progress(&proceeding->PROGRESS, msg, 0, nt?1:5, 8, nt,bc); + + +#if DEBUG + printf("Building PROCEEDING Msg\n"); +#endif + return msg; +} +void print_proceeding (struct isdn_msg msgs[]) +{ +} + +void parse_alerting (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + ALERTING_t *alerting=(ALERTING_t*)((unsigned long)(msg->data+HEADER_LEN)); + //Q931_info_t *qi=(Q931_info_t*)(msg->data+HEADER_LEN); + + dec_ie_progress(alerting->PROGRESS, (Q931_info_t *)alerting, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc); + +#if DEBUG + printf("Parsing ALERTING Msg\n"); +#endif + + +} +msg_t *build_alerting (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + ALERTING_t *alerting; + msg_t *msg =(msg_t*)create_l3msg(CC_ALERTING | REQUEST, MT_ALERTING, bc?bc->l3_id:-1, sizeof(ALERTING_t) ,nt); + + alerting=(ALERTING_t*)((msg->data+HEADER_LEN)); + + enc_ie_channel_id(&alerting->CHANNEL_ID, msg, 1,bc->channel, nt,bc); + + if (nt) + enc_ie_progress(&alerting->PROGRESS, msg, 0, nt?1:5, 8, nt,bc); +#if DEBUG + printf("Building ALERTING Msg\n"); +#endif + return msg; +} +void print_alerting (struct isdn_msg msgs[]) +{ +} + + +void parse_progress (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + PROGRESS_t *progress=(PROGRESS_t*)((unsigned long)(msg->data+HEADER_LEN)); + //Q931_info_t *qi=(Q931_info_t*)(msg->data+HEADER_LEN); + + dec_ie_progress(progress->PROGRESS, (Q931_info_t *)progress, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc); + +#if DEBUG + printf("Parsing PROGRESS Msg\n"); +#endif +} + +msg_t *build_progress (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + PROGRESS_t *progress; + msg_t *msg =(msg_t*)create_l3msg(CC_PROGRESS | REQUEST, MT_PROGRESS, bc?bc->l3_id:-1, sizeof(PROGRESS_t) ,nt); + + progress=(PROGRESS_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building PROGRESS Msg\n"); +#endif + return msg; +} +void print_progress (struct isdn_msg msgs[]) +{ +} + + +void parse_setup (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + SETUP_t *setup= (SETUP_t*)((unsigned long)msg->data+HEADER_LEN); + Q931_info_t *qi=(Q931_info_t*)((unsigned long)msg->data+HEADER_LEN); + +#if DEBUG + printf("Parsing SETUP Msg\n"); +#endif + { + int type,plan,present, screen; + char id[32]; + dec_ie_calling_pn(setup->CALLING_PN, qi, &type, &plan, &present, &screen, (unsigned char *)id, sizeof(id), nt,bc); + + bc->onumplan=type; + strcpy(bc->oad, id); + switch (present) { + case 0: +// cb_log(3, bc->stack->port, " --> Pres:0\n"); + bc->pres=0; /* screened */ + break; + case 1: +// cb_log(3, bc->stack->port, " --> Pres:1\n"); + bc->pres=1; /* not screened */ + break; + default: +// cb_log(3, bc->stack->port, " --> Pres:%d\n",present); + bc->pres=0; + } + switch (screen) { + case 0: +// cb_log(4, bc->stack->port, " --> Screen:0\n"); + break; + default: +// cb_log(4, bc->stack->port, " --> Screen:%d\n",screen); + ; + } + } + { + int type, plan; + char number[32]; + dec_ie_called_pn(setup->CALLED_PN, (Q931_info_t *)setup, &type, &plan, (unsigned char *)number, sizeof(number), nt,bc); + strcpy(bc->dad, number); + bc->dnumplan=type; + } + { + char keypad[32]; + dec_ie_keypad(setup->KEYPAD, (Q931_info_t *)setup, (unsigned char *)keypad, sizeof(keypad), nt,bc); + strcpy(bc->keypad, keypad); + } + + { + int sending_complete; + dec_ie_complete(setup->COMPLETE, (Q931_info_t *)setup, &sending_complete, nt,bc); + } + + { + int type, plan, present, screen, reason; + char id[32]; + dec_ie_redir_nr(setup->REDIR_NR, (Q931_info_t *)setup, &type, &plan, &present, &screen, &reason, (unsigned char *)id, sizeof(id), nt,bc); + + strcpy(bc->rad, id); + bc->rnumplan=type; +// cb_log(3, bc->stack->port, " --> Redirecting number (REDIR_NR): '%s'\n", id); + } + { + int coding, capability, mode, rate, multi, user, async, urate, stopbits, dbits, parity; + dec_ie_bearer(setup->BEARER, (Q931_info_t *)setup, &coding, &capability, &mode, &rate, &multi, &user, &async, &urate, &stopbits, &dbits, &parity, nt,bc); + switch (capability) { + case -1: bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED; +// cb_log(2, bc->stack->port, " --> cap -1 -> digital\n"); + break; + case 0: bc->capability=INFO_CAPABILITY_SPEECH; +// cb_log(2, bc->stack->port, " --> cap speech\n"); + break; + case 8: bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED; + bc->user1 = user; + bc->async = async; + bc->urate = urate; + + bc->rate = rate; + bc->mode = mode; + +// cb_log(2, bc->stack->port, " --> cap unres Digital (user l1 %d, async %d, user rate %d\n", user, async, urate); + break; + case 9: bc->capability=INFO_CAPABILITY_DIGITAL_RESTRICTED; +// cb_log(2, bc->stack->port, " --> cap res Digital\n"); + break; + default: +// cb_log(2, bc->stack->port, " --> cap Else\n"); + break; + } + + switch(user) { + case 2: + bc->law=INFO_CODEC_ULAW; + break; + case 3: + bc->law=INFO_CODEC_ALAW; + break; + default: + bc->law=INFO_CODEC_ALAW; + + } + + bc->capability=capability; + } + { + int exclusive, channel; + dec_ie_channel_id(setup->CHANNEL_ID, (Q931_info_t *)setup, &exclusive, &channel, nt,bc); + if (channel==0xff) /* any channel */ + channel=-1; + + if (channel > 0) + bc->channel = channel; + } + + dec_ie_progress(setup->PROGRESS, (Q931_info_t *)setup, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc); + +} + +#define ANY_CHANNEL 0xff /* IE attribut for 'any channel' */ +msg_t *build_setup (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + SETUP_t *setup; + msg_t *msg =(msg_t*)create_l3msg(CC_SETUP | REQUEST, MT_SETUP, bc?bc->l3_id:-1, sizeof(SETUP_t) ,nt); + + setup=(SETUP_t*)((msg->data+HEADER_LEN)); + +// cb_log(2, bc->stack->port, " --> oad %s dad %s channel %d\n",bc->oad, bc->dad,bc->channel); + if (bc->channel == 0 || bc->channel == ANY_CHANNEL || bc->channel==-1) + enc_ie_channel_id(&setup->CHANNEL_ID, msg, 0, bc->channel, nt,bc); + else + enc_ie_channel_id(&setup->CHANNEL_ID, msg, 1, bc->channel, nt,bc); + + { + int type=bc->onumplan,plan=1,present=bc->pres,screen=0; + enc_ie_calling_pn(&setup->CALLING_PN, msg, type, plan, present, + screen, bc->oad, nt, bc); + } + + { + if (bc->dad[0]) + enc_ie_called_pn(&setup->CALLED_PN, msg, bc->dnumplan, 1, bc->dad, nt,bc); + } + + if (*bc->display) { + enc_ie_display(&setup->DISPLAY, msg, bc->display, nt,bc); + } + + { + int coding=0, capability, mode=0 /* 2 for packet ! */ + ,user, rate=0x10; + switch (bc->capability) { + case INFO_CAPABILITY_SPEECH: capability = 0; +// cb_log(2, bc->stack->port, " --> Speech\n"); + break; + case INFO_CAPABILITY_DIGITAL_UNRESTRICTED: capability = 8; +// cb_log(2, bc->stack->port, " --> cap unres Digital\n"); + break; + case INFO_CAPABILITY_DIGITAL_RESTRICTED: capability = 9; +// cb_log(2, bc->stack->port, " --> cap res Digital\n"); + break; + default: +// cb_log(2, bc->stack->port, " --> cap Speech\n"); + capability=bc->capability; + } + + switch (bc->law) { + case INFO_CODEC_ULAW: user=2; +// cb_log(2, bc->stack->port, " --> Codec Ulaw\n"); + break; + case INFO_CODEC_ALAW: user=3; +// cb_log(2, bc->stack->port, " --> Codec Alaw\n"); + break; + default: + user=3; + } + + enc_ie_bearer(&setup->BEARER, msg, coding, capability, mode, rate, -1, user, nt,bc); + } + +#if DEBUG + printf("Building SETUP Msg\n"); +#endif + return msg; +} + +void print_setup (struct isdn_msg msgs[]) +{ +} + +void parse_connect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + CONNECT_t *connect=(CONNECT_t*)((unsigned long)(msg->data+HEADER_LEN)); + + bc->ces = connect->ces; + bc->ces = connect->ces; + + dec_ie_progress(connect->PROGRESS, (Q931_info_t *)connect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc); + +#if DEBUG + printf("Parsing CONNECT Msg\n"); +#endif +} +msg_t *build_connect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + CONNECT_t *connect; + msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | REQUEST, MT_CONNECT, bc?bc->l3_id:-1, sizeof(CONNECT_t) ,nt); + + connect=(CONNECT_t*)((msg->data+HEADER_LEN)); + + if (nt) { + time_t now; + time(&now); + enc_ie_date(&connect->DATE, msg, now, nt,bc); + } + + { + int type=0, plan=1, present=2, screen=0; + enc_ie_connected_pn(&connect->CONNECT_PN, msg, type,plan, present, screen, (unsigned char*) bc->dad , nt , bc); + } + +#if DEBUG + printf("Building CONNECT Msg\n"); +#endif + return msg; +} +void print_connect (struct isdn_msg msgs[]) +{ +} + + +void parse_setup_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + SETUP_ACKNOWLEDGE_t *setup_acknowledge=(SETUP_ACKNOWLEDGE_t*)((unsigned long)(msg->data+HEADER_LEN)); + + { + int exclusive, channel; + dec_ie_channel_id(setup_acknowledge->CHANNEL_ID, (Q931_info_t *)setup_acknowledge, &exclusive, &channel, nt,bc); + + if (channel==0xff) /* any channel */ + channel=-1; + + if (channel > 0) + bc->channel = channel; + } + + dec_ie_progress(setup_acknowledge->PROGRESS, (Q931_info_t *)setup_acknowledge, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc); +#if DEBUG + printf("Parsing SETUP_ACKNOWLEDGE Msg\n"); +#endif + + +} +msg_t *build_setup_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + SETUP_ACKNOWLEDGE_t *setup_acknowledge; + msg_t *msg =(msg_t*)create_l3msg(CC_SETUP_ACKNOWLEDGE | REQUEST, MT_SETUP_ACKNOWLEDGE, bc?bc->l3_id:-1, sizeof(SETUP_ACKNOWLEDGE_t) ,nt); + + setup_acknowledge=(SETUP_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); + + enc_ie_channel_id(&setup_acknowledge->CHANNEL_ID, msg, 1,bc->channel, nt,bc); + + if (nt) + enc_ie_progress(&setup_acknowledge->PROGRESS, msg, 0, nt?1:5, 8, nt,bc); + +#if DEBUG + printf("Building SETUP_ACKNOWLEDGE Msg\n"); +#endif + return msg; +} + +void print_setup_acknowledge (struct isdn_msg msgs[]) +{ +} + +void parse_connect_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing CONNECT_ACKNOWLEDGE Msg\n"); +#endif + + +} +msg_t *build_connect_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + CONNECT_ACKNOWLEDGE_t *connect_acknowledge; + msg_t *msg =(msg_t*)create_l3msg(CC_CONNECT | RESPONSE, MT_CONNECT, bc?bc->l3_id:-1, sizeof(CONNECT_ACKNOWLEDGE_t) ,nt); + + connect_acknowledge=(CONNECT_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); + + enc_ie_channel_id(&connect_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc); + +#if DEBUG + printf("Building CONNECT_ACKNOWLEDGE Msg\n"); +#endif + return msg; +} +void print_connect_acknowledge (struct isdn_msg msgs[]) +{ +} + + +void parse_user_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing USER_INFORMATION Msg\n"); +#endif + + +} +msg_t *build_user_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + USER_INFORMATION_t *user_information; + msg_t *msg =(msg_t*)create_l3msg(CC_USER_INFORMATION | REQUEST, MT_USER_INFORMATION, bc?bc->l3_id:-1, sizeof(USER_INFORMATION_t) ,nt); + + user_information=(USER_INFORMATION_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building USER_INFORMATION Msg\n"); +#endif + return msg; +} +void print_user_information (struct isdn_msg msgs[]) +{ +} + + +void parse_suspend_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing SUSPEND_REJECT Msg\n"); +#endif + + +} +msg_t *build_suspend_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + SUSPEND_REJECT_t *suspend_reject; + msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_REJECT | REQUEST, MT_SUSPEND_REJECT, bc?bc->l3_id:-1, sizeof(SUSPEND_REJECT_t) ,nt); + + suspend_reject=(SUSPEND_REJECT_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building SUSPEND_REJECT Msg\n"); +#endif + return msg; +} +void print_suspend_reject (struct isdn_msg msgs[]) +{ +} + + +void parse_resume_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing RESUME_REJECT Msg\n"); +#endif + + +} +msg_t *build_resume_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + RESUME_REJECT_t *resume_reject; + msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_REJECT | REQUEST, MT_RESUME_REJECT, bc?bc->l3_id:-1, sizeof(RESUME_REJECT_t) ,nt); + + resume_reject=(RESUME_REJECT_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building RESUME_REJECT Msg\n"); +#endif + return msg; +} +void print_resume_reject (struct isdn_msg msgs[]) +{ +} + + +void parse_hold (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing HOLD Msg\n"); +#endif + + +} +msg_t *build_hold (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + HOLD_t *hold; + msg_t *msg =(msg_t*)create_l3msg(CC_HOLD | REQUEST, MT_HOLD, bc?bc->l3_id:-1, sizeof(HOLD_t) ,nt); + + hold=(HOLD_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building HOLD Msg\n"); +#endif + return msg; +} +void print_hold (struct isdn_msg msgs[]) +{ +} + + +void parse_suspend (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing SUSPEND Msg\n"); +#endif + + +} +msg_t *build_suspend (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + SUSPEND_t *suspend; + msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND | REQUEST, MT_SUSPEND, bc?bc->l3_id:-1, sizeof(SUSPEND_t) ,nt); + + suspend=(SUSPEND_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building SUSPEND Msg\n"); +#endif + return msg; +} +void print_suspend (struct isdn_msg msgs[]) +{ +} + + +void parse_resume (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing RESUME Msg\n"); +#endif + + +} +msg_t *build_resume (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + RESUME_t *resume; + msg_t *msg =(msg_t*)create_l3msg(CC_RESUME | REQUEST, MT_RESUME, bc?bc->l3_id:-1, sizeof(RESUME_t) ,nt); + + resume=(RESUME_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building RESUME Msg\n"); +#endif + return msg; +} +void print_resume (struct isdn_msg msgs[]) +{ +} + + +void parse_hold_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing HOLD_ACKNOWLEDGE Msg\n"); +#endif + + +} +msg_t *build_hold_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + HOLD_ACKNOWLEDGE_t *hold_acknowledge; + msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_ACKNOWLEDGE | REQUEST, MT_HOLD_ACKNOWLEDGE, bc?bc->l3_id:-1, sizeof(HOLD_ACKNOWLEDGE_t) ,nt); + + hold_acknowledge=(HOLD_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building HOLD_ACKNOWLEDGE Msg\n"); +#endif + return msg; +} +void print_hold_acknowledge (struct isdn_msg msgs[]) +{ +} + + +void parse_suspend_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing SUSPEND_ACKNOWLEDGE Msg\n"); +#endif + + +} +msg_t *build_suspend_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + SUSPEND_ACKNOWLEDGE_t *suspend_acknowledge; + msg_t *msg =(msg_t*)create_l3msg(CC_SUSPEND_ACKNOWLEDGE | REQUEST, MT_SUSPEND_ACKNOWLEDGE, bc?bc->l3_id:-1, sizeof(SUSPEND_ACKNOWLEDGE_t) ,nt); + + suspend_acknowledge=(SUSPEND_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building SUSPEND_ACKNOWLEDGE Msg\n"); +#endif + return msg; +} +void print_suspend_acknowledge (struct isdn_msg msgs[]) +{ +} + + +void parse_resume_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing RESUME_ACKNOWLEDGE Msg\n"); +#endif + + +} +msg_t *build_resume_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + RESUME_ACKNOWLEDGE_t *resume_acknowledge; + msg_t *msg =(msg_t*)create_l3msg(CC_RESUME_ACKNOWLEDGE | REQUEST, MT_RESUME_ACKNOWLEDGE, bc?bc->l3_id:-1, sizeof(RESUME_ACKNOWLEDGE_t) ,nt); + + resume_acknowledge=(RESUME_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building RESUME_ACKNOWLEDGE Msg\n"); +#endif + return msg; +} +void print_resume_acknowledge (struct isdn_msg msgs[]) +{ +} + + +void parse_hold_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing HOLD_REJECT Msg\n"); +#endif + + +} +msg_t *build_hold_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + HOLD_REJECT_t *hold_reject; + msg_t *msg =(msg_t*)create_l3msg(CC_HOLD_REJECT | REQUEST, MT_HOLD_REJECT, bc?bc->l3_id:-1, sizeof(HOLD_REJECT_t) ,nt); + + hold_reject=(HOLD_REJECT_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building HOLD_REJECT Msg\n"); +#endif + return msg; +} +void print_hold_reject (struct isdn_msg msgs[]) +{ +} + + +void parse_retrieve (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing RETRIEVE Msg\n"); +#endif + + +} +msg_t *build_retrieve (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + RETRIEVE_t *retrieve; + msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE | REQUEST, MT_RETRIEVE, bc?bc->l3_id:-1, sizeof(RETRIEVE_t) ,nt); + + retrieve=(RETRIEVE_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building RETRIEVE Msg\n"); +#endif + return msg; +} +void print_retrieve (struct isdn_msg msgs[]) +{ +} + + +void parse_retrieve_acknowledge (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing RETRIEVE_ACKNOWLEDGE Msg\n"); +#endif + + +} +msg_t *build_retrieve_acknowledge (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + RETRIEVE_ACKNOWLEDGE_t *retrieve_acknowledge; + msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_ACKNOWLEDGE | REQUEST, MT_RETRIEVE_ACKNOWLEDGE, bc?bc->l3_id:-1, sizeof(RETRIEVE_ACKNOWLEDGE_t) ,nt); + + retrieve_acknowledge=(RETRIEVE_ACKNOWLEDGE_t*)((msg->data+HEADER_LEN)); + + enc_ie_channel_id(&retrieve_acknowledge->CHANNEL_ID, msg, 1, bc->channel, nt,bc); +#if DEBUG + printf("Building RETRIEVE_ACKNOWLEDGE Msg\n"); +#endif + return msg; +} +void print_retrieve_acknowledge (struct isdn_msg msgs[]) +{ +} + + +void parse_retrieve_reject (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing RETRIEVE_REJECT Msg\n"); +#endif + + +} +msg_t *build_retrieve_reject (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + RETRIEVE_REJECT_t *retrieve_reject; + msg_t *msg =(msg_t*)create_l3msg(CC_RETRIEVE_REJECT | REQUEST, MT_RETRIEVE_REJECT, bc?bc->l3_id:-1, sizeof(RETRIEVE_REJECT_t) ,nt); + + retrieve_reject=(RETRIEVE_REJECT_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building RETRIEVE_REJECT Msg\n"); +#endif + return msg; +} +void print_retrieve_reject (struct isdn_msg msgs[]) +{ +} + + +void parse_disconnect (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + DISCONNECT_t *disconnect=(DISCONNECT_t*)((unsigned long)(msg->data+HEADER_LEN)); + int location; + + dec_ie_cause(disconnect->CAUSE, (Q931_info_t *)(disconnect), &location, &bc->cause, nt,bc); + + dec_ie_progress(disconnect->PROGRESS, (Q931_info_t *)disconnect, &bc->progress_coding, &bc->progress_location, &bc->progress_indicator, nt, bc); +#if DEBUG + printf("Parsing DISCONNECT Msg\n"); +#endif + + +} +msg_t *build_disconnect (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + DISCONNECT_t *disconnect; + msg_t *msg =(msg_t*)create_l3msg(CC_DISCONNECT | REQUEST, MT_DISCONNECT, bc?bc->l3_id:-1, sizeof(DISCONNECT_t) ,nt); + + disconnect=(DISCONNECT_t*)((msg->data+HEADER_LEN)); + + enc_ie_cause(&disconnect->CAUSE, msg, (nt)?1:0, bc->out_cause,nt,bc); + if (nt) enc_ie_progress(&disconnect->PROGRESS, msg, 0, nt?1:5, 8 ,nt,bc); + +#if DEBUG + printf("Building DISCONNECT Msg\n"); +#endif + return msg; +} +void print_disconnect (struct isdn_msg msgs[]) +{ +} + + +void parse_restart (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + RESTART_t *restart=(RESTART_t*)((unsigned long)(msg->data+HEADER_LEN)); + +#if DEBUG + printf("Parsing RESTART Msg\n"); +#endif + + { + int exclusive, channel; + dec_ie_channel_id(restart->CHANNEL_ID, (Q931_info_t *)restart, &exclusive, &channel, nt,bc); + if (channel==0xff) /* any channel */ + channel=-1; + cb_log(0, bc->stack->port, "CC_RESTART Request on channel:%d on port:%d\n",bc->stack->port); + } + + +} +msg_t *build_restart (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + RESTART_t *restart; + msg_t *msg =(msg_t*)create_l3msg(CC_RESTART | REQUEST, MT_RESTART, bc?bc->l3_id:-1, sizeof(RESTART_t) ,nt); + + restart=(RESTART_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building RESTART Msg\n"); +#endif + return msg; +} +void print_restart (struct isdn_msg msgs[]) +{ +} + + +void parse_release (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + RELEASE_t *release=(RELEASE_t*)((unsigned long)(msg->data+HEADER_LEN)); + int location; + + dec_ie_cause(release->CAUSE, (Q931_info_t *)(release), &location, &bc->cause, nt,bc); +#if DEBUG + printf("Parsing RELEASE Msg\n"); +#endif + + +} +msg_t *build_release (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + RELEASE_t *release; + msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE | REQUEST, MT_RELEASE, bc?bc->l3_id:-1, sizeof(RELEASE_t) ,nt); + + release=(RELEASE_t*)((msg->data+HEADER_LEN)); + + + enc_ie_cause(&release->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc); + +#if DEBUG + printf("Building RELEASE Msg\n"); +#endif + return msg; +} +void print_release (struct isdn_msg msgs[]) +{ +} + + +void parse_release_complete (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + RELEASE_COMPLETE_t *release_complete=(RELEASE_COMPLETE_t*)((unsigned long)(msg->data+HEADER_LEN)); + int location; + iframe_t *frm = (iframe_t*) msg->data; +#ifdef MISDNUSER_JOLLY + mISDNuser_head_t *hh; + hh=(mISDNuser_head_t*)msg->data; +#else + mISDN_head_t *hh; + hh=(mISDN_head_t*)msg->data; +#endif + + if (nt) { + if (hh->prim == (CC_RELEASE_COMPLETE|CONFIRM)) { + cb_log(0, bc->stack->port, "CC_RELEASE_COMPLETE|CONFIRM [NT] port:%d\n",bc->stack->port); + return; + } + } else { + if (frm->prim == (CC_RELEASE_COMPLETE|CONFIRM)) { + cb_log(0, bc->stack->port, "CC_RELEASE_COMPLETE|CONFIRM [TE] port:%d\n",bc->stack->port); + return; + } + } + dec_ie_cause(release_complete->CAUSE, (Q931_info_t *)(release_complete), &location, &bc->cause, nt,bc); + + +#if DEBUG + printf("Parsing RELEASE_COMPLETE Msg\n"); +#endif + + +} +msg_t *build_release_complete (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + RELEASE_COMPLETE_t *release_complete; + msg_t *msg =(msg_t*)create_l3msg(CC_RELEASE_COMPLETE | REQUEST, MT_RELEASE_COMPLETE, bc?bc->l3_id:-1, sizeof(RELEASE_COMPLETE_t) ,nt); + + release_complete=(RELEASE_COMPLETE_t*)((msg->data+HEADER_LEN)); + + enc_ie_cause(&release_complete->CAUSE, msg, nt?1:0, bc->out_cause, nt,bc); + +#if DEBUG + printf("Building RELEASE_COMPLETE Msg\n"); +#endif + return msg; +} +void print_release_complete (struct isdn_msg msgs[]) +{ +} + + +void parse_facility (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + FACILITY_t *facility=(FACILITY_t*)((unsigned long)(msg->data+HEADER_LEN)); + Q931_info_t *qi=(Q931_info_t*)(msg->data+HEADER_LEN); + + +#if DEBUG + printf("Parsing FACILITY Msg\n"); +#endif + + { + char fac[128]; + int facility_len; + + dec_ie_facility(facility->FACILITY, qi, fac, &facility_len, nt, bc); + } + + +} +msg_t *build_facility (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + FACILITY_t *facility; + msg_t *msg =(msg_t*)create_l3msg(CC_FACILITY | REQUEST, MT_FACILITY, bc?bc->l3_id:-1, sizeof(FACILITY_t) ,nt); + + facility=(FACILITY_t*)((msg->data+HEADER_LEN)); + + { + if (*bc->display) { + printf("Sending %s as Display\n", bc->display); + enc_ie_display(&facility->DISPLAY, msg, bc->display, nt,bc); + } + + + + switch ( bc->facility ) { + case FACILITY_CALLDEFLECT: + enc_facility_calldeflect(&facility->FACILITY, msg, bc->facility_calldeflect_nr, nt, bc); + + break; + case FACILITY_NONE: + break; + } + + } + +#if DEBUG + printf("Building FACILITY Msg\n"); +#endif + return msg; +} +void print_facility (struct isdn_msg msgs[]) +{ +} + + +void parse_notify (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing NOTIFY Msg\n"); +#endif + + +} +msg_t *build_notify (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + NOTIFY_t *notify; + msg_t *msg =(msg_t*)create_l3msg(CC_NOTIFY | REQUEST, MT_NOTIFY, bc?bc->l3_id:-1, sizeof(NOTIFY_t) ,nt); + + notify=(NOTIFY_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building NOTIFY Msg\n"); +#endif + return msg; +} +void print_notify (struct isdn_msg msgs[]) +{ +} + + +void parse_status_enquiry (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing STATUS_ENQUIRY Msg\n"); +#endif + + +} +msg_t *build_status_enquiry (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + STATUS_ENQUIRY_t *status_enquiry; + msg_t *msg =(msg_t*)create_l3msg(CC_STATUS_ENQUIRY | REQUEST, MT_STATUS_ENQUIRY, bc?bc->l3_id:-1, sizeof(STATUS_ENQUIRY_t) ,nt); + + status_enquiry=(STATUS_ENQUIRY_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building STATUS_ENQUIRY Msg\n"); +#endif + return msg; +} +void print_status_enquiry (struct isdn_msg msgs[]) +{ +} + + +void parse_information (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + INFORMATION_t *information=(INFORMATION_t*)((unsigned long)(msg->data+HEADER_LEN)); + + { + int type, plan; + char number[32]; + char keypad[32]; + dec_ie_called_pn(information->CALLED_PN, (Q931_info_t *)information, &type, &plan, (unsigned char *)number, sizeof(number), nt,bc); + dec_ie_keypad(information->KEYPAD, (Q931_info_t *)information, (unsigned char *)keypad, sizeof(keypad), nt,bc); + strcpy(bc->info_dad, number); + strcpy(bc->keypad,keypad); + + } +#if DEBUG + printf("Parsing INFORMATION Msg\n"); +#endif + + +} +msg_t *build_information (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + INFORMATION_t *information; + msg_t *msg =(msg_t*)create_l3msg(CC_INFORMATION | REQUEST, MT_INFORMATION, bc?bc->l3_id:-1, sizeof(INFORMATION_t) ,nt); + + information=(INFORMATION_t*)((msg->data+HEADER_LEN)); + + { + enc_ie_called_pn(&information->CALLED_PN, msg, 0, 1, bc->info_dad, nt,bc); + } + + { + if (*bc->display) { + printf("Sending %s as Display\n", bc->display); + enc_ie_display(&information->DISPLAY, msg, bc->display, nt,bc); + } + } + +#if DEBUG + printf("Building INFORMATION Msg\n"); +#endif + return msg; +} +void print_information (struct isdn_msg msgs[]) +{ +} + + +void parse_status (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + STATUS_t *status=(STATUS_t*)((unsigned long)(msg->data+HEADER_LEN)); + int location; + + dec_ie_cause(status->CAUSE, (Q931_info_t *)(status), &location, &bc->cause, nt,bc); + ; + +#if DEBUG + printf("Parsing STATUS Msg\n"); +#endif + + +} +msg_t *build_status (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + STATUS_t *status; + msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS, bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt); + + status=(STATUS_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building STATUS Msg\n"); +#endif + return msg; +} +void print_status (struct isdn_msg msgs[]) +{ +} + + +void parse_timeout (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ +#if DEBUG + printf("Parsing STATUS Msg\n"); +#endif + + +} +msg_t *build_timeout (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt) +{ + int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN; + STATUS_t *status; + msg_t *msg =(msg_t*)create_l3msg(CC_STATUS | REQUEST, MT_STATUS, bc?bc->l3_id:-1, sizeof(STATUS_t) ,nt); + + status=(STATUS_t*)((msg->data+HEADER_LEN)); + +#if DEBUG + printf("Building STATUS Msg\n"); +#endif + return msg; +} +void print_timeout (struct isdn_msg msgs[]) +{ +} + + +/************************************/ + + + + +/** Msg Array **/ + +struct isdn_msg msgs_g[] = { + {CC_PROCEEDING,L3,EVENT_PROCEEDING, + parse_proceeding,build_proceeding,print_proceeding, + "PROCEEDING"}, + {CC_ALERTING,L3,EVENT_ALERTING, + parse_alerting,build_alerting,print_alerting, + "ALERTING"}, + {CC_PROGRESS,L3,EVENT_PROGRESS, + parse_progress,build_progress,print_progress, + "PROGRESS"}, + {CC_SETUP,L3,EVENT_SETUP, + parse_setup,build_setup,print_setup, + "SETUP"}, + {CC_CONNECT,L3,EVENT_CONNECT, + parse_connect,build_connect,print_connect, + "CONNECT"}, + {CC_SETUP_ACKNOWLEDGE,L3,EVENT_SETUP_ACKNOWLEDGE, + parse_setup_acknowledge,build_setup_acknowledge,print_setup_acknowledge, + "SETUP_ACKNOWLEDGE"}, + {CC_CONNECT_ACKNOWLEDGE ,L3,EVENT_CONNECT_ACKNOWLEDGE , + parse_connect_acknowledge ,build_connect_acknowledge ,print_connect_acknowledge , + "CONNECT_ACKNOWLEDGE "}, + {CC_USER_INFORMATION,L3,EVENT_USER_INFORMATION, + parse_user_information,build_user_information,print_user_information, + "USER_INFORMATION"}, + {CC_SUSPEND_REJECT,L3,EVENT_SUSPEND_REJECT, + parse_suspend_reject,build_suspend_reject,print_suspend_reject, + "SUSPEND_REJECT"}, + {CC_RESUME_REJECT,L3,EVENT_RESUME_REJECT, + parse_resume_reject,build_resume_reject,print_resume_reject, + "RESUME_REJECT"}, + {CC_HOLD,L3,EVENT_HOLD, + parse_hold,build_hold,print_hold, + "HOLD"}, + {CC_SUSPEND,L3,EVENT_SUSPEND, + parse_suspend,build_suspend,print_suspend, + "SUSPEND"}, + {CC_RESUME,L3,EVENT_RESUME, + parse_resume,build_resume,print_resume, + "RESUME"}, + {CC_HOLD_ACKNOWLEDGE,L3,EVENT_HOLD_ACKNOWLEDGE, + parse_hold_acknowledge,build_hold_acknowledge,print_hold_acknowledge, + "HOLD_ACKNOWLEDGE"}, + {CC_SUSPEND_ACKNOWLEDGE,L3,EVENT_SUSPEND_ACKNOWLEDGE, + parse_suspend_acknowledge,build_suspend_acknowledge,print_suspend_acknowledge, + "SUSPEND_ACKNOWLEDGE"}, + {CC_RESUME_ACKNOWLEDGE,L3,EVENT_RESUME_ACKNOWLEDGE, + parse_resume_acknowledge,build_resume_acknowledge,print_resume_acknowledge, + "RESUME_ACKNOWLEDGE"}, + {CC_HOLD_REJECT,L3,EVENT_HOLD_REJECT, + parse_hold_reject,build_hold_reject,print_hold_reject, + "HOLD_REJECT"}, + {CC_RETRIEVE,L3,EVENT_RETRIEVE, + parse_retrieve,build_retrieve,print_retrieve, + "RETRIEVE"}, + {CC_RETRIEVE_ACKNOWLEDGE,L3,EVENT_RETRIEVE_ACKNOWLEDGE, + parse_retrieve_acknowledge,build_retrieve_acknowledge,print_retrieve_acknowledge, + "RETRIEVE_ACKNOWLEDGE"}, + {CC_RETRIEVE_REJECT,L3,EVENT_RETRIEVE_REJECT, + parse_retrieve_reject,build_retrieve_reject,print_retrieve_reject, + "RETRIEVE_REJECT"}, + {CC_DISCONNECT,L3,EVENT_DISCONNECT, + parse_disconnect,build_disconnect,print_disconnect, + "DISCONNECT"}, + {CC_RESTART,L3,EVENT_RESTART, + parse_restart,build_restart,print_restart, + "RESTART"}, + {CC_RELEASE,L3,EVENT_RELEASE, + parse_release,build_release,print_release, + "RELEASE"}, + {CC_RELEASE_COMPLETE,L3,EVENT_RELEASE_COMPLETE, + parse_release_complete,build_release_complete,print_release_complete, + "RELEASE_COMPLETE"}, + {CC_FACILITY,L3,EVENT_FACILITY, + parse_facility,build_facility,print_facility, + "FACILITY"}, + {CC_NOTIFY,L3,EVENT_NOTIFY, + parse_notify,build_notify,print_notify, + "NOTIFY"}, + {CC_STATUS_ENQUIRY,L3,EVENT_STATUS_ENQUIRY, + parse_status_enquiry,build_status_enquiry,print_status_enquiry, + "STATUS_ENQUIRY"}, + {CC_INFORMATION,L3,EVENT_INFORMATION, + parse_information,build_information,print_information, + "INFORMATION"}, + {CC_STATUS,L3,EVENT_STATUS, + parse_status,build_status,print_status, + "STATUS"}, + {CC_TIMEOUT,L3,EVENT_TIMEOUT, + parse_timeout,build_timeout,print_timeout, + "TIMEOUT"}, + {0,0,0,NULL,NULL,NULL,NULL} +}; + +#define msgs_max (sizeof(msgs_g)/sizeof(struct isdn_msg)) + +/** INTERFACE FCTS ***/ +int isdn_msg_get_index(struct isdn_msg msgs[], msg_t *msg, int nt) +{ + int i; + + if (nt){ +#ifdef MISDNUSER_JOLLY + mISDNuser_head_t *hh = (mISDNuser_head_t*)msg->data; +#else + mISDN_head_t *hh = (mISDN_head_t*)msg->data; +#endif + + for (i=0; i< msgs_max -1; i++) + if ( (hh->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i; + + } else { + iframe_t *frm = (iframe_t*)msg->data; + + for (i=0; i< msgs_max -1; i++) + if ( (frm->prim&COMMAND_MASK)==(msgs[i].misdn_msg&COMMAND_MASK)) return i; + } + + return -1; +} + +int isdn_msg_get_index_by_event(struct isdn_msg msgs[], enum event_e event, int nt) +{ + int i; + for (i=0; i< msgs_max; i++) + if ( event == msgs[i].event) return i; + + cb_log(4,0, "get_index: EVENT NOT FOUND!!\n"); + + return -1; +} + +enum event_e isdn_msg_get_event(struct isdn_msg msgs[], msg_t *msg, int nt) +{ + int i=isdn_msg_get_index(msgs, msg, nt); + if(i>=0) return msgs[i].event; + return EVENT_UNKNOWN; +} + +char * isdn_msg_get_info(struct isdn_msg msgs[], msg_t *msg, int nt) +{ + int i=isdn_msg_get_index(msgs, msg, nt); + if(i>=0) return msgs[i].info; + return NULL; +} + + +char EVENT_CLEAN_INFO[] = "CLEAN_UP"; +char EVENT_DTMF_TONE_INFO[] = "DTMF_TONE"; +char EVENT_NEW_L3ID_INFO[] = "NEW_L3ID"; +char EVENT_NEW_BC_INFO[] = "NEW_BC"; +char EVENT_BCHAN_DATA_INFO[] = "BCHAN_DATA"; + +char * isdn_get_info(struct isdn_msg msgs[], enum event_e event, int nt) +{ + int i=isdn_msg_get_index_by_event(msgs, event, nt); + + if(i>=0) return msgs[i].info; + + if (event == EVENT_CLEANUP) return EVENT_CLEAN_INFO; + if (event == EVENT_DTMF_TONE) return EVENT_DTMF_TONE_INFO; + if (event == EVENT_NEW_L3ID) return EVENT_NEW_L3ID_INFO; + if (event == EVENT_NEW_BC) return EVENT_NEW_BC_INFO; + if (event == EVENT_BCHAN_DATA) return EVENT_BCHAN_DATA_INFO; + + return NULL; +} + +int isdn_msg_parse_event(struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *bc, int nt) +{ + int i=isdn_msg_get_index(msgs, msg, nt); + if(i<0) return -1; + + msgs[i].msg_parser(msgs, msg, bc, nt); + return 0; +} + +msg_t * isdn_msg_build_event(struct isdn_msg msgs[], struct misdn_bchannel *bc, enum event_e event, int nt) +{ + int i=isdn_msg_get_index_by_event(msgs, event, nt); + if(i<0) return NULL; + + return msgs[i].msg_builder(msgs, bc, nt); +} + diff -u -N -r ast_clean/channels/misdn/Makefile ast_misdn/channels/misdn/Makefile --- ast_clean/channels/misdn/Makefile 1970-01-01 01:00:00.000000000 +0100 +++ ast_misdn/channels/misdn/Makefile 2005-10-17 19:31:23.000000000 +0200 @@ -0,0 +1,45 @@ +# +# Makefile +# +# Make file for chan_misdn support +# + +# Verify those options with main Makefile +ifndef LINUX +LINUX=/lib/modules/$(shell uname -r)/build +endif + +CFLAGS += -pipe -c +SOURCES = isdn_lib.c isdn_msg_parser.c +OBJDIR = . +OBJS = isdn_lib.o isdn_msg_parser.o + +ifndef MISDNUSER +MISDNUSER=/usr/src/install-misdn/mISDNuser +endif + +MISDNCFLAGS += -I$(MISDNUSER)/include -I$(MISDNUSER)/i4lnet -I$(MISDNUSER)/lib +MISDNCFLAGS += -DMISDNUSER_JOLLY -I$(LINUX)/include + + +all: chan_misdn_lib.a Makefile.ast + + +%.o: %.c + $(CC) $(MISDNCFLAGS) $(CFLAGS) -o $@ $< + + +chan_misdn_lib.a: $(OBJS) + ar crv $@ $(OBJS) + +Makefile.ast: FORCE + @echo CFLAGS+=$(MISDNCFLAGS) -Imisdn/ -DCHAN_MISDN_VERSION=\\\"0.1.1\\\" >$@.tmp + @echo MISDNUSER = $(MISDNUSER) >>$@.tmp + @if [ -r $@ ] && cmp -s $@ $@.tmp; then rm -f $@.tmp; else mv -f $@.tmp $@; fi + + +FORCE: + + +clean: + rm *.a *.o Makefile.ast diff -u -N -r ast_clean/channels/misdn/portinfo.c ast_misdn/channels/misdn/portinfo.c --- ast_clean/channels/misdn/portinfo.c 1970-01-01 01:00:00.000000000 +0100 +++ ast_misdn/channels/misdn/portinfo.c 2005-10-17 19:31:23.000000000 +0200 @@ -0,0 +1,197 @@ + + +#include "isdn_lib.h" + + +/* + * global function to show all available isdn ports + */ +void isdn_port_info(void) +{ + int err; + int i, ii, p; + int useable, nt, pri; + unsigned char buff[1025]; + iframe_t *frm = (iframe_t *)buff; + stack_info_t *stinf; + int device; + + /* open mISDN */ + if ((device = mISDN_open()) < 0) + { + fprintf(stderr, "mISDN_open() failed: ret=%d errno=%d (%s) Check for mISDN modules and device.\n", device, errno, strerror(errno)); + exit(-1); + } + + /* get number of stacks */ + i = 1; + ii = mISDN_get_stack_count(device); + printf("\n"); + if (ii <= 0) + { + printf("Found no card. Please be sure to load card drivers.\n"); + } + + /* loop the number of cards and get their info */ + while(i <= ii) + { + err = mISDN_get_stack_info(device, i, buff, sizeof(buff)); + if (err <= 0) + { + fprintf(stderr, "mISDN_get_stack_info() failed: port=%d err=%d\n", i, err); + break; + } + stinf = (stack_info_t *)&frm->data.p; + + nt = pri = 0; + useable = 1; + + /* output the port info */ + printf("Port %2d: ", i); + switch(stinf->pid.protocol[0] & ~ISDN_PID_FEATURE_MASK) + { + case ISDN_PID_L0_TE_S0: + printf("TE-mode BRI S/T interface line (for phone lines)"); +#if 0 + if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_S0_HFC & ISDN_PID_FEATURE_MASK) + printf(" HFC multiport card"); +#endif + break; + case ISDN_PID_L0_NT_S0: + nt = 1; + printf("NT-mode BRI S/T interface port (for phones)"); +#if 0 + if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_S0_HFC & ISDN_PID_FEATURE_MASK) + printf(" HFC multiport card"); +#endif + break; + case ISDN_PID_L0_TE_U: + printf("TE-mode BRI U interface line"); + break; + case ISDN_PID_L0_NT_U: + nt = 1; + printf("NT-mode BRI U interface port"); + break; + case ISDN_PID_L0_TE_UP2: + printf("TE-mode BRI Up2 interface line"); + break; + case ISDN_PID_L0_NT_UP2: + nt = 1; + printf("NT-mode BRI Up2 interface port"); + break; + case ISDN_PID_L0_TE_E1: + pri = 1; + printf("TE-mode PRI E1 interface line (for phone lines)"); +#if 0 + if (stinf->pid.protocol[0] & ISDN_PID_L0_TE_E1_HFC & ISDN_PID_FEATURE_MASK) + printf(" HFC-E1 card"); +#endif + break; + case ISDN_PID_L0_NT_E1: + nt = 1; + pri = 1; + printf("NT-mode PRI E1 interface port (for phones)"); +#if 0 + if (stinf->pid.protocol[0] & ISDN_PID_L0_NT_E1_HFC & ISDN_PID_FEATURE_MASK) + printf(" HFC-E1 card"); +#endif + break; + default: + useable = 0; + printf("unknown type 0x%08x",stinf->pid.protocol[0]); + } + printf("\n"); + + if (nt) + { + if (stinf->pid.protocol[1] == 0) + { + useable = 0; + printf(" -> Missing layer 1 NT-mode protocol.\n"); + } + p = 2; + while(p <= MAX_LAYER_NR) { + if (stinf->pid.protocol[p]) + { + useable = 0; + printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for NT lib.\n", p, stinf->pid.protocol[p]); + } + p++; + } + if (useable) + { + if (pri) + printf(" -> Interface is Point-To-Point (PRI).\n"); + else + printf(" -> Interface can be Poin-To-Point/Multipoint.\n"); + } + } else + { + if (stinf->pid.protocol[1] == 0) + { + useable = 0; + printf(" -> Missing layer 1 protocol.\n"); + } + if (stinf->pid.protocol[2] == 0) + { + useable = 0; + printf(" -> Missing layer 2 protocol.\n"); + } + if (stinf->pid.protocol[2] & ISDN_PID_L2_DF_PTP) + { + printf(" -> Interface is Poin-To-Point.\n"); + } + if (stinf->pid.protocol[3] == 0) + { + useable = 0; + printf(" -> Missing layer 3 protocol.\n"); + } else + { + printf(" -> Protocol: "); + switch(stinf->pid.protocol[3] & ~ISDN_PID_FEATURE_MASK) + { + case ISDN_PID_L3_DSS1USER: + printf("DSS1 (Euro ISDN)"); + break; + + default: + useable = 0; + printf("unknown protocol 0x%08x",stinf->pid.protocol[3]); + } + printf("\n"); + } + p = 4; + while(p <= MAX_LAYER_NR) { + if (stinf->pid.protocol[p]) + { + useable = 0; + printf(" -> Layer %d protocol 0x%08x is detected, but not allowed for TE lib.\n", p, stinf->pid.protocol[p]); + } + p++; + } + printf(" -> childcnt: %d\n",stinf->childcnt); + } + + if (!useable) + printf(" * Port NOT useable for PBX\n"); + + printf("--------\n"); + + i++; + } + printf("\n"); + + /* close mISDN */ + if ((err = mISDN_close(device))) + { + fprintf(stderr, "mISDN_close() failed: err=%d '%s'\n", err, strerror(err)); + exit(-1); + } +} + + +int main() +{ + isdn_port_info(); + return 0; +} diff -u -N -r ast_clean/configs/misdn.conf.sample ast_misdn/configs/misdn.conf.sample --- ast_clean/configs/misdn.conf.sample 1970-01-01 01:00:00.000000000 +0100 +++ ast_misdn/configs/misdn.conf.sample 2005-10-17 19:31:23.000000000 +0200 @@ -0,0 +1,267 @@ +; +; chan_misdn sample config +; + +; general section: +; +; for debugging and general setup, things that are not bound to port groups +; + +[general] + +; set debugging flag: +; 0 - No Debug +; 1 - mISDN Messages and * - Messages, and * - State changes +; 2 - Messages + Message specific Informations (e.g. bearer capability) +; 3 - very Verbose, the above + lots of Driver specific infos +; 4 - even more Verbose than 3 +; +; default value: 0 +; +debug=0 + +; the big trace +; +; default value: [not set] +; +;tracefile=/var/log/misdn.trace + +; single call trace files +; set to true if you want to have them +; they depend on debug level +; +; default values: trace_calls : false +; trace_dir : /var/log/ +; +trace_calls=false +trace_dir=/var/log/ + +; set to yes if you want mISDN_dsp to bridge the calls in HW +; +; default value: yes +; +bridging=yes + +; stops dialtone after getting first digit on nt Port +; +; default value: yes +; +stop_tone_after_first_digit=yes + +; wether to append overlapdialed Digits to Extension or not +; +; default value: yes +; +append_digits2exten=yes + +; set this to yes if you have jollys mISDN which sends correct L1 Infos +; +; default value: yes +; +l1_info_ok=yes + +; set this to yes if you want to clear the l3 in case the l2 deactivates +; some environments have a flickering l2 which causes this option to +; damage active calls .. highly experimental +; +; default value: no +; +clear_l3=no + +; set the method to use for channel selection: +; standard - always choose the first free channel with the lowest number +; round_robin - use the round robin algorithm to select a channel. use this +; if you want to balance your load. +; +; default value: standard +; +method=standard + +;;; CRYPTION STUFF + +; Wether to look for dynamic crypting attempt +; +; default value: no +; +dynamic_crypt=no + +; crypt_prefix, what is used for crypting Protocol +; +; default value: [not set] +; +crypt_prefix=** + +; Keys for cryption, you reference them in the dialplan +; later also in dynamic encr. +; +; default value: [not set] +; +crypt_keys=test,muh + +; users sections: +; +; name your sections as you which but not "general" ! +; the secions are Groups, you can dial out in extensions.conf +; with Dial(mISDN/g:extern/101) where extern is a section name, +; chan_misdn tries every port in this section to find a +; new free channel +; + +; The default section is not a group section, it just contains config elements +; which are inherited by group sections. +; + +[default] + +; define your default context here +; +; default value: default +; +context=misdn + +; language +; +; default value: en +; +language=en + +; Prefixes for national and international, those are put before the +; oad if an according dialplan is set by the other end. +; +; default values: nationalprefix : 0 +; internationalprefix : 00 +; +nationalprefix=0 +internationalprefix=00 + +; set rx/tx gains between -8 and 8 to change the RX/TX Gain +; +; default values: rxgain: 0 +; txgain: 0 +; +rxgain=0 +txgain=0 + +; some telcos espacially in NL seem to need this set to yes, also in +; switzerland this seems to be important +; +; default value: no +; +te_choose_channel=no + +; dialplan options: +; +; 0 - unknown +; 1 - National +; 2 - International +; 4 - Subscriber +; +; This setting is used for outgoing calls +; +; default value: 0 +; +dialplan=0 + +; This is only for asterisk head and will result in only considering +; misdn.confs and misdn_set_opts callingpresentation informations if set to no. +; Otherwise asterisks callingpresentation overwrites misdn.confs settings. +; +; default value: yes +; +use_callingpres=yes + +; uncomment the following to get into s extension at extension conf +; there you can use DigitTimeout if you can't or don't want to use +; isdn overlap dial. +; note: This will jump into the s exten for every exten! +; +; default value: no +; +;always_immediate=no + +; uncomment the following if you want callers which called exactly the +; base number (so no extension is set) jump to the s extension. +; if the user dials something more it jumps to the correct extension +; instead +; +; default value: no +; +;immediate=no + +; uncomment the following to have hold and retrieve support +; +; default value: no +; +;hold_allowed=yes + +; Pickup and Callgroup +; +; deafult values: not set = 0 +; +;callgroup=1 +;pickupgroup=1 + +; Allows/Screens Callerid +; +; possible values: allowed,not_screened +; +; be aware, if you set to allowed you need to set a correct +; callerid in the dialplan or set it here in the misdn.conf +; Some Telcos don't care about wrong callerids, others do ! +; +; default value: allowed +; +;presentation=not_screened + +; this enables echocancellation, with the given number of taps +; be aware, move this setting only to outgoing portgroups! +; A value of zero turns echocancellation off. +; +; possible values are: 0,32,64,128.256,yes(=128),no(=0) +; +; default value: no +; +;echocancel=no + +; this disables echocancellation when the call is bridged between +; mISDN channels +; +; default value: no +; +echocancelwhenbridged=no + +; Set this to no to disable echotraining +; +; default value: yes +; +echotraining=yes + +[intern] +; define your ports, e.g. 1,2 (depends on mISDN-driver loading order) +ports=1,2 +; context where to go to when incoming Call on one of the above ports +context=Intern + +[internPP] +; if you want to have pp Protocol on one nt Port, you need +; to add a ptp directly after the portnumber, you can still add +; more ports and multiple ptp adds in your config. +ports=3ptp + +[first_extern] +; again port defs +ports=4 +; again a context for incomming calls +context=Extern1 +; msns for te ports, listen on those numbers on the above ports, and +; indicate the incoming calls to asterisk +; here you can give a comma seperated list or simply an '*' for +; any msn. +msns=* + +; here an example with given msns +[second_extern] +ports=5 +context=Extern2 +callerid=15 +msns=102,144,101,104 diff -u -N -r ast_clean/doc/README.misdn ast_misdn/doc/README.misdn --- ast_clean/doc/README.misdn 1970-01-01 01:00:00.000000000 +0100 +++ ast_misdn/doc/README.misdn 2005-10-17 19:31:23.000000000 +0200 @@ -0,0 +1,363 @@ +mISDN Channel Driver for Asterisk PBX +====================================== + + +This package contains the mISDN Channel Driver for the Asterisk PBX. It +supports every mISDN Hardware and provides an interface for asterisk. + +Features: + +* NT and TE mode +* PP and PMP mode +* BRI and PRI (with BNE1 and BN2E1 Cards) +* DTMF Detection in HW+mISDNdsp (much better than asterisks internal!) +* Display Messages to Phones (which support display msg) +* HOLD/RETRIEVE/TRANSFER on ISDN Phones : ) +* Screen/ Not Screen User Number +* Basic EchoCancellation +* Volume Control +* Crypting with mISDNdsp (Blowfish) +* Data (HDLC) callthrough +* Data Callin (with app_ptyfork +pppd) +* echo cancellation +* some other + +Supported Hardware: + +chan_misdn supports any mISDN compatible Hardware. Especially the 1-8 Port +BRI Cards available from http://shop.beronet.com + +Overview +-------- + +- Fast Installation Guide +- Pre-Requisites +- Compilation +- Installation +- Configuration +- Dial and Options String +- misdn cli commands +- Debugging and sending Bugreports +- Examples +- Known working Configurations +- Known Problems +- Changes + + +Fast Installation Guide +----------------------- + +You have two options on how to install chan_misdn. + +(1) Requirements: + - mISDN and mISDNuser from jolly (see Pre-Requisites below) + Installation: + - cd /channels/misdn + - edit Makefile so that MISDNUSER points to your mISDNuser directory + - run 'make' + - cd + - compile and install asterisk via 'make install' + +(2) Requirements: + - Asterisk headers and Kernel headers + Description: + The Makefile gets the newest mISDN and mISDNuser Sources from + jollys webpage and the newest release of chan_misdn from beronets + Servers. After that it compiles and installs everything (hopefully). + Installation: + - cd /usr/src + - wget http://www.beronet.com/downloads/install-misdn.tar.gz + - tar zxf install-misdn.tar.gz + - cd install-misdn + - make install + + +Pre-Requisites +-------------- + +To compile and install this driver, you'll need at least one mISDN Driver, the +mISDNuser package and the asterisk includes (which will be inside of the +sources). Chan_misdn works with both, the current stable release and the cvs-head version of Asterisk. + +To get the mISDN stuff please follow the instructions at +http://www.isdn4linux.de. Please Note that mISDN works good for the +linux-2.6.x kernels. Some of the mISDN drivers do not compile against the +2.4.x or older kernels, you can patch them, but than you'll get myterius +errors. + + I use Kernels > 2.6.9 and it works perfect. with kernels >= 2.6.10 there is a +very litle bug in hfc_multi.c which causes the module not to compile, it can +be easyly fixed by changenging pci_findsubsys to pci_getsubsys in code. + +Ok so far so good, now follow the compilation instructions. + +!! Dont forget to create the /dev/mISDN device node. + +Compilation +----------- + +!! Be aware, in the actual mISDNuser package theres a bug in the Makefile +!! the compilation stops near iapplication.h, this isn't very important +!! at this step you are ready. + +After you've successfully installed mISDN, mISDNuser and asterisk, you should +modify the Makefile in the chan_misdn source path. There you can tell the +Makefile where to install the driver, sample-conf, and most important where it +can find the linux kernel includes, the mISDNuser package and the asterisk +includes. If you use the Head-Revision of Asterisk (or at least a newer +version than stable) uncomment the CCFLAGS+=-DASTERISK_STABLE, the stable +version of asterisk is at the moment v1-0-X as cvs tag. + +Now you can type in: + +make + +This should compile chan_misdn.so, if theres an error check the paths in the +Makefile again. + + +Installation +------------ + +After successful compilation of chan_misdn, you should simply type in: + +make install + +as privileged user to put chan_misdn.so in the asterisk modules +directory. + +You should see a Msg like: "Successfully installed chan_misdn". +Congratulations. + +Theres a sample init.d script for loading the mISDN modules (mISDN.sample), +simply copy it to /etc/init.d/ and modify it, there you can enter your cards. + +!! Forget to use capi together with chan_misdn. + + + +Configuration +------------- + +First of all you must configure the mISDN drivers. Each driver module has got +an options and layermask option, which tells the driver wether to start in +TE,NT, PP or PMP mode (there are lots more please read docs in misdn for +that). + +After thinking about the above you'll probably want to configure the +misdn.conf file which resides in the asterisk-config directory. + +The misdn.conf file contains a "general" Section, and user sections which +contain misdn port settings and different asterisk contexts. + +The general section contains especially a variable named context with which +the default context is set. There is also the very important debug variable +which you can set from the asterisk cli (command line interface) or in this +configfile, bigger numbers will lead to more debug output. Theres also a +tracefile options, which takes a path+filename where debug output is written +to. + +The user Sections have names which are unequal to "general". Those sections +contain the ports variable which mean the mISDN Ports. Here you can add +comma-sepperated multiple ports. + +Espacially for TE-Mode Ports there is a msns variable. This variable tells the +chan_misdn driver to listen for incomming calls with the given msns, you can +insert a '*' as single msn, which leads in getting every incomming call (if +you want to share on PMP TE S0 with a asterisk and a phone or isdn card you +should insert here the msns which you'll like to give the asterisk). Finally +a context variable resides in the user sections, which tells chan_misdn where +to send incomming calls to (extension.conf). + +In NT-Mode Ports there is a new option, directly after the port number you can +write ptp, this enables PP Mode for this port, please look at misdn.conf for +an example. + +When everything worked you should get the asterisk running. + + +Dial and Options String +----------------------- + +The Dialstring of chan_misdn got more complex, because we added more features, +so the generic dialstring looks like: + +mISDN/|g:/[/] + +The Optionsstring looks Like: +:: + +the ":" character is the delimeter. + +The available Optchars are: + d - Send display text on called phone, text is the optparam + n - don't detect dtmf tones on called channel + h - make digital outgoing call + c - make crypted outgoing call, param is keyindex + e - perform echo cancelation on this channel, takes taps as + arguments (32,64,128,256) + s - send Non Inband DTMF as inband + vr - rxgain control + vt - txgain control + + +chan_misdn registers a new application "misdn_set_opt" when loaded. This +application takes the Optionsstring as argument. The Syntax is: + +misdn_set_opt() + + +When you set options in the dialstring, the options are set in the external +channel. When you set options with misdn_set_opt, they are set in the current +incoming channel. So if you like to use static encryption, the scenario looks +as follows: + +Phone1 --> * Box 1 --> PSTN_TE +PSTN_TE --> * Box 2 --> Phone2 + +The Encryption must be done on the PSTN sides, so the dialplan on the boxes +are: + +* Box 1: +exten => _${CRYPT_PREFIX}X.,1,Dial(mISDN/g:outbound/:c1) + +* Box 2: +exten => ${CRYPT_MSN},1,misdn_set_opt(:c1) +exten => ${CRYPT_MSN},2,dial(${PHONE2}) + + + + +misdn cli commands +------------------ + +At the asterisk cli you can try to type in: + +misdn + +Now you should see the misdn cli commands: + +- clean + -> pid (cleans a broken call, use with care, leads often + to a segmentation fault) +- send + -> display (sends a Text Message to a asterisk channel, + this channel must be an misdn channel) +- set + -> debug (sets debug level) +- show + -> config (shows the configuration options) + -> channels (shows the current active misdn channels) + -> channel (shows details about the given misdn channels) + -> stacks (shows the currend ports, there protocols and states) + -> fullstacks (shows the current active and inactive misdn channels) + +- restart + -> port (restarts given port (L2 Restart) ) + +- reload (reloads misdn.conf) + +You can only use "misdn send display" when a asterisk channel is created and +isdn is in the corect state, correct state means that you have established a +call to another phone (mustnt be isdn though). + +Then you use it like this: + +misdn send display mISDN/1/101 "Hello World!" + +where 1 is the Port of the Card where the phone is plugged in, and 101 is the +msn (callerid) of the Phone to send the text to. + + + +Debugging and sending Bug-Reports +--------------------------------- + +If you encounter problems, you should set up the debugging flag, usually debug=1 should be enough. the Messages are divided in asterisk and misdn parts. Misdn Debug messages begin with an 'I', asterisk messages begin with an '*', the rest is clear I think. + +Please take a trace of the problem and send this trace via mail to bugs@beronet.com + + +Examples +-------- + +here some examples of how to use chan_misdn in the dialplan (extensions.conf): + + +[globals] +OUT_PORT=1 ; The physical Port of the Card +OUT_GROUP=ExternE1 ; The Group of Ports defined in misdn.conf + +[misdnIn] +exten => _X.,1,Dial(mISDN/${OUT_PORT}/${EXTEN}) +exten => _0X.,1,Dial(mISDN/g:${OUT_GROUP}/${EXTEN:1}) +exten => _1X.,1,Dial(mISDN/g:${OUT_GROUP}/${EXTEN:1}/:dHello) +exten => _1X.,1,Dial(mISDN/g:${OUT_GROUP}/${EXTEN:1}/:dHello Test:n) + +In the last line you will notice the last argument (Hello), this is sended +as Display Message to the Phone. + + +Known working Configurations +---------------------------- + +In this Section I'll put working configurations for chan_misdn. Beware It +seems that between Kernel 2.6.3 and Kernel 2.6.8 there were lots of mISDN +Bugs. I use Kernel 2.6.9 now, it works quite ok, Kernel 2.6.10+ has changed +the pci_find_subgsys funktion, so hfc_multi from mISDN doesn't compile against +it, you can just change pci_find_subsys to pci_get_subsys, this works. + + +- chan_misdn-0.0.3-rc1: + * linux-kernel >= 2.6.3 (but at least 2.6) + * asterisk >= v1-0 + * mISDN/mISDNuser since September/04 + +- chan_misdn-0.0.3-rc3: + * linux-kernel >= 2.6.3 (but at least 2.6) + * asterisk >= v1-0.2 + * mISDN/mISDNuser since December/04 + +- chan_misdn-0.0.3-rc4: + * linux-kernel >= 2.6.8 (but at least 2.6) + * asterisk >= v1-0.2 + * mISDN/mISDNuser head on cvs.isdn4linux.de + +- chan_misdn-0.0.3-rc6: + * linux-kernel >= 2.6.8 (but at least 2.6) + * asterisk >= v1-0.2 + * mISDN/mISDNuser head on cvs.isdn4linux.de + +- chan_misdn-0.1.0 + * linux-kernel >= 2.6.8 (but at least 2.6) + * asterisk >= v1-0.2 , also CVS Head + * mISDN/mISDNuser (3.0-beta) from isdn.jolly.de + + +Known Problems +-------------- + +* When I use mISDN->IAX I cannot make Trunk calls + +-> You need to use ztdummy as dummy zaptel interface for the iax timing in +trunking mode, simply grab libpri, zaptel and compile them (i think you need +to modify the makefile in zaptel to add ztdummy to the defaultly compiled +modules) then modprobe ztdummy, this resolves the problem. + + +* I cannot hear any tone after succesfull CONNECT to other end + +-> you forgot to load mISDNdsp, which is now needed by chan_misdn for switching +and dtmf tone detection + +* I have strange ISDN behavior: sometimes I hear the other end, sometimes +not. also i get STATUS Events with cause 100, with misdn debugging + +-> Please update to newest version of chan_misdn and set the te_choose_channel +option in misdn.conf to yes + +Changes +------- + +in the Changes File +