X-Git-Url: http://git.eversberg.eu/gitweb.cgi?p=lcr.git;a=blobdiff_plain;f=chan_lcr.c;h=fa37ef63fbf024eefde9dba0a83d9afd1283c4bf;hp=0a3830466e1c47c673c16db3c258c72212563875;hb=44b6b77e68c2b685174c09da4467139e009130b4;hpb=cd8dc297eb5d3a75f86bc71d6c110af3bbf829a9 diff --git a/chan_lcr.c b/chan_lcr.c index 0a38304..fa37ef6 100644 --- a/chan_lcr.c +++ b/chan_lcr.c @@ -79,12 +79,6 @@ If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure */ -locking asterisk process and handler -reconnect after socket closed, release all calls. -debug of call handling -denke an alle info-elements in jeder message (from asterisk & from lcr) -ausloesen beim socket-verlust - #include #include #include @@ -98,18 +92,8 @@ ausloesen beim socket-verlust #include #include -#include #include -#include "extension.h" -#include "message.h" -#include "lcrsocket.h" -#include "cause.h" -#include "bchannel.h" -#include "chan_lcr.h" - - - #include #include #include @@ -130,17 +114,30 @@ ausloesen beim socket-verlust #include #include +#include "extension.h" +#include "message.h" +#include "lcrsocket.h" +#include "cause.h" +#include "bchannel.h" +#include "chan_lcr.h" +#include "callerid.h" + CHAN_LCR_STATE // state description structure +u_char flip_bits[256]; + int lcr_debug=1; int mISDN_created=1; -char lcr_type[]="LCR"; +char lcr_type[]="lcr"; pthread_t chan_tid; -pthread_mutex_t chan_lock; +ast_mutex_t chan_lock; /* global lock */ +ast_mutex_t log_lock; /* logging log */ int quit; +int glob_channel = 0; + int lcr_sock = -1; struct admin_list { @@ -148,6 +145,36 @@ struct admin_list { struct admin_msg msg; } *admin_first = NULL; +static struct ast_channel_tech lcr_tech; + +/* + * logging + */ +void chan_lcr_log(int type, const char *file, int line, struct chan_call *call, struct ast_channel *ast, const char *fmt, ...) +{ + char buffer[1024]; + char call_text[128] = "NULL"; + char ast_text[128] = "NULL"; + va_list args; + + ast_mutex_lock(&log_lock); + + va_start(args,fmt); + vsnprintf(buffer,sizeof(buffer)-1,fmt,args); + buffer[sizeof(buffer)-1]=0; + va_end(args); + + if (call) + sprintf(call_text, "%ld", call->ref); + if (ast) + strncpy(ast_text, ast->name, sizeof(ast_text)-1); + ast_text[sizeof(ast_text)-1] = '\0'; + + ast_log(type, file, line, "[call=%s ast=%s] %s", call_text, ast_text, buffer); + + ast_mutex_unlock(&log_lock); +} + /* * channel and call instances */ @@ -166,31 +193,33 @@ struct chan_call *find_call_ref(unsigned long ref) return(call); } -struct chan_call *find_call_handle(unsigned long handle) +#if 0 +struct chan_call *find_call_ast(struct ast_channel *ast) { struct chan_call *call = call_first; while(call) { - if (call->bchannel_handle == handle) + if (call->ast == ast) break; call = call->next; } return(call); } -struct chan_call *alloc_call(void) +struct chan_call *find_call_handle(unsigned long handle) { - struct chan_call **callp = &call_first; - - while(*callp) - callp = &((*callp)->next); + struct chan_call *call = call_first; - *callp = (struct chan_call *)malloc(sizeof(struct chan_call)); - if (*callp) - memset(*callp, 0, sizeof(struct chan_call)); - return(*callp); + while(call) + { + if (call->bchannel_handle == handle) + break; + call = call->next; + } + return(call); } +#endif void free_call(struct chan_call *call) { @@ -201,14 +230,52 @@ void free_call(struct chan_call *call) if (*temp == call) { *temp = (*temp)->next; + if (call->pipe[0]) + close(call->pipe[0]); + if (call->pipe[1]) + close(call->pipe[1]); + if (call->bchannel) + { + if (call->bchannel->call != call) + CERROR(call, NULL, "Linked bchannel structure has no link to us.\n"); + call->bchannel->call = NULL; + } + if (call->bridge_call) + { + if (call->bridge_call->bridge_call != call) + CERROR(call, NULL, "Linked call structure has no link to us.\n"); + call->bridge_call->bridge_call = NULL; + } + CDEBUG(call, NULL, "Call instance freed.\n"); free(call); return; } temp = &((*temp)->next); } + CERROR(call, NULL, "Call instance not found in list.\n"); +} + +struct chan_call *alloc_call(void) +{ + struct chan_call **callp = &call_first; + + while(*callp) + callp = &((*callp)->next); + + *callp = (struct chan_call *)malloc(sizeof(struct chan_call)); + if (*callp) + memset(*callp, 0, sizeof(struct chan_call)); + if (pipe((*callp)->pipe) < 0) { + CERROR(*callp, NULL, "Failed to create pipe.\n"); + free_call(*callp); + return(NULL); + } + CDEBUG(*callp, NULL, "Call instance allocated.\n"); + return(*callp); } -unsigned short new_brige_id(void) + +unsigned short new_bridge_id(void) { struct chan_call *call; unsigned short id = 1; @@ -227,28 +294,24 @@ unsigned short new_brige_id(void) break; id++; } + CDEBUG(NULL, NULL, "New bridge ID %d.\n", id); return(id); } /* - * receive bchannel data - */ -void rx_data(struct bchannel *bchannel, unsigned char *data, int len) -{ -} - -void rx_dtmf(struct bchannel *bchannel, char tone) -{ -} - -/* * enque message to LCR */ int send_message(int message_type, unsigned long ref, union parameter *param) { struct admin_list *admin, **adminp; + if (lcr_sock < 0) { + CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type); + return -1; + } + CDEBUG(NULL, NULL, "Sending message %d to socket.\n", message_type); + adminp = &admin_first; while(*adminp) adminp = &((*adminp)->next); @@ -263,6 +326,196 @@ int send_message(int message_type, unsigned long ref, union parameter *param) } /* + * send setup info to LCR + * this function is called, when asterisk call is received and ref is received + */ +static void send_setup_to_lcr(struct chan_call *call) +{ + union parameter newparam; + struct ast_channel *ast = call->ast; + + if (!call->ast || !call->ref) + return; + + CDEBUG(call, call->ast, "Sending setup to LCR.\n"); + + /* send setup message to LCR */ + memset(&newparam, 0, sizeof(union parameter)); + newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN; + newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN; + if (ast->cid.cid_num) if (ast->cid.cid_num[0]) + strncpy(newparam.setup.callerinfo.id, ast->cid.cid_num, sizeof(newparam.setup.callerinfo.id)-1); + if (ast->cid.cid_name) if (ast->cid.cid_name[0]) + strncpy(newparam.setup.callerinfo.name, ast->cid.cid_name, sizeof(newparam.setup.callerinfo.name)-1); + if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0]) + { + strncpy(newparam.setup.redirinfo.id, ast->cid.cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1); + newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN; + newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN; + } + switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) + { + case AST_PRES_ALLOWED: + newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED; + break; + case AST_PRES_RESTRICTED: + newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED; + break; + case AST_PRES_UNAVAILABLE: + newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL; + break; + default: + newparam.setup.callerinfo.present = INFO_PRESENT_NULL; + } + switch(ast->cid.cid_ton) + { + case 4: + newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER; + break; + case 2: + newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL; + break; + case 1: + newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL; + break; + default: + newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN; + } + newparam.setup.capainfo.bearer_capa = ast->transfercapability; +#ifdef TODO + newparam.setup.capainfo.bearer_info1 = alaw 3, ulaw 2; +#endif + newparam.setup.capainfo.bearer_info1 = 3; + newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT; + newparam.setup.capainfo.hlc = INFO_HLC_NONE; + newparam.setup.capainfo.exthlc = INFO_HLC_NONE; + send_message(MESSAGE_SETUP, call->ref, &newparam); + + /* change to outgoing setup state */ + call->state = CHAN_LCR_STATE_OUT_SETUP; +} + +/* + * send dialing info to LCR + * this function is called, when setup acknowledge is received and dialing + * info is available. + */ +static void send_dialque_to_lcr(struct chan_call *call) +{ + union parameter newparam; + + if (!call->ast || !call->ref || !call->dialque) + return; + + CDEBUG(call, call->ast, "Sending dial queue to LCR.\n"); + + /* send setup message to LCR */ + memset(&newparam, 0, sizeof(union parameter)); + strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1); + call->dialque[0] = '\0'; + send_message(MESSAGE_INFORMATION, call->ref, &newparam); +} + +/* + * in case of a bridge, the unsupported message can be forwarded directly + * to the remote call. + */ +static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param) +{ + /* check bridge */ + if (!call) return; + if (!call->bridge_call) return; + CDEBUG(call, NULL, "Sending message due briding.\n"); + send_message(message_type, call->bridge_call->ref, param); +} + +/* + * check if extension matches and start asterisk + * if it can match, proceed + * if not, release + */ +static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete) +{ + int cause, ret; + union parameter newparam; + + if (complete) + { + /* if not match */ + if (!ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad)) + { + CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", ast->exten, ast->context); + cause = 1; + goto release; + } + if (!ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad)) + { + CDEBUG(call, ast, "Got 'sending complete', but extension '%s' would match at context '%s', if more digits would be dialed - releasing.\n", ast->exten, ast->context); + cause = 28; + goto release; + } + CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n"); + /* send setup acknowledge to lcr */ + memset(&newparam, 0, sizeof(union parameter)); + send_message(MESSAGE_PROCEEDING, call->ref, &newparam); + + /* change state */ + call->state = CHAN_LCR_STATE_IN_PROCEEDING; + + goto start; + } + + if (ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad)) + { + /* send setup acknowledge to lcr */ + memset(&newparam, 0, sizeof(union parameter)); + send_message(MESSAGE_OVERLAP, call->ref, &newparam); + + /* change state */ + call->state = CHAN_LCR_STATE_IN_DIALING; + + /* if match, start pbx */ + if (ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad)) { + CDEBUG(call, ast, "Extensions matches.\n"); + goto start; + } + + /* if can match */ + CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n"); + return; + } + + /* if not match */ + cause = 1; + release: + /* release lcr */ + CDEBUG(call, ast, "Releasing due to extension missmatch.\n"); + memset(&newparam, 0, sizeof(union parameter)); + newparam.disconnectinfo.cause = cause; + newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL; + send_message(MESSAGE_RELEASE, call->ref, &newparam); + call->ref = 0; + /* release asterisk */ + ast->hangupcause = call->cause; + /* change to release state */ + call->state = CHAN_LCR_STATE_RELEASE; + ast_hangup(ast); // call will be destroyed here + return; + + start: + /* send setup to asterisk */ + CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n"); + ret = ast_pbx_start(ast); + if (ret < 0) + { + cause = (ret==-2)?34:27; + goto release; + } + call->pbx_started = 1; + return; +} + +/* * incoming setup from LCR */ static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param) @@ -270,11 +523,14 @@ static void lcr_in_setup(struct chan_call *call, int message_type, union paramet struct ast_channel *ast; union parameter newparam; + CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %d, dialing %d)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id); + /* create asterisk channel instrance */ - ast = ast_channel_alloc(1); + ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel); if (!ast) { /* release */ + CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n"); memset(&newparam, 0, sizeof(union parameter)); newparam.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL; newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL; @@ -285,15 +541,71 @@ static void lcr_in_setup(struct chan_call *call, int message_type, union paramet } /* set ast pointer */ call->ast = ast; + ast->tech_pvt = call; + ast->tech = &lcr_tech; + ast->fds[0] = call->pipe[0]; /* fill setup information */ -#warning todo: setup-info reinschreiben + if (param->setup.dialinginfo.id) + strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1); + if (param->setup.context[0]) + strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1); + if (param->setup.callerinfo.id[0]) + ast->cid.cid_num = strdup(param->setup.callerinfo.id); + if (param->setup.callerinfo.name[0]) + ast->cid.cid_name = strdup(param->setup.callerinfo.name); +#ifdef TODO + if (param->setup.redirinfo.id[0]) + ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter)); +#endif + switch (param->setup.callerinfo.present) + { + case INFO_PRESENT_ALLOWED: + ast->cid.cid_pres = AST_PRES_ALLOWED; + break; + case INFO_PRESENT_RESTRICTED: + ast->cid.cid_pres = AST_PRES_RESTRICTED; + break; + default: + ast->cid.cid_pres = AST_PRES_UNAVAILABLE; + } + switch (param->setup.callerinfo.ntype) + { + case INFO_NTYPE_SUBSCRIBER: + ast->cid.cid_ton = 4; + break; + case INFO_NTYPE_NATIONAL: + ast->cid.cid_ton = 2; + break; + case INFO_NTYPE_INTERNATIONAL: + ast->cid.cid_ton = 1; + break; + default: + ast->cid.cid_ton = 0; + } + ast->transfercapability = param->setup.capainfo.bearer_capa; +#ifdef TODO + strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter), sizeof(call->oad)-1); +#else + strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, "0", "00"), sizeof(call->oad)-1); +#endif - /* send setup to asterisk */ -#warning todo: setup bei der asterisk triggern + /* configure channel */ +#ifdef TODO + ast->nativeformats = configfile->lawformat; + ast->readformat = ast->rawreadformat = configfile->lawformat; + ast->writeformat = ast->rawwriteformat = configfile->lawformat; +#else + ast->nativeformats = AST_FORMAT_ALAW; + ast->readformat = ast->rawreadformat = AST_FORMAT_ALAW; + ast->writeformat = ast->rawwriteformat = AST_FORMAT_ALAW; +#endif + ast->hangupcause = 0; /* change state */ call->state = CHAN_LCR_STATE_IN_SETUP; + + lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete); } /* @@ -301,9 +613,13 @@ static void lcr_in_setup(struct chan_call *call, int message_type, union paramet */ static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param) { + if (!call->ast) return; + + CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n"); + /* send pending digits in dialque */ if (call->dialque) - send_dialing_to_lcr(call); + send_dialque_to_lcr(call); /* change to overlap state */ call->state = CHAN_LCR_STATE_OUT_DIALING; } @@ -313,10 +629,13 @@ static void lcr_in_overlap(struct chan_call *call, int message_type, union param */ static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param) { + CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n"); + /* change state */ call->state = CHAN_LCR_STATE_OUT_PROCEEDING; /* send event to asterisk */ - ast_queue_... todo + if (call->ast && call->pbx_started) + ast_queue_control(call->ast, AST_CONTROL_PROCEEDING); } /* @@ -324,10 +643,13 @@ static void lcr_in_proceeding(struct chan_call *call, int message_type, union pa */ static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param) { + CDEBUG(call, call->ast, "Incomming alerting from LCR.\n"); + /* change state */ call->state = CHAN_LCR_STATE_OUT_ALERTING; /* send event to asterisk */ - ast_queue_... todo + if (call->ast && call->pbx_started) + ast_queue_control(call->ast, AST_CONTROL_RINGING); } /* @@ -335,12 +657,15 @@ static void lcr_in_alerting(struct chan_call *call, int message_type, union para */ static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param) { + CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n"); + /* change state */ call->state = CHAN_LCR_STATE_CONNECT; /* copy connectinfo */ - todo + memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info)); /* send event to asterisk */ - ast_queue_... todo + if (call->ast && call->pbx_started) + ast_queue_control(call->ast, AST_CONTROL_ANSWER); } /* @@ -348,12 +673,40 @@ static void lcr_in_connect(struct chan_call *call, int message_type, union param */ static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param) { + struct ast_channel *ast = call->ast; + + CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause); + /* change state */ call->state = CHAN_LCR_STATE_IN_DISCONNECT; - /* copy disconnect info */ - todo - /* send event to asterisk */ - ast_queue_... todo + /* save cause */ + call->cause = param->disconnectinfo.cause; + call->location = param->disconnectinfo.location; + /* if bridge, forward disconnect and return */ +#ifdef TODO + feature flag + if (call->bridge_call) + { + CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n"); + bridge_message_if_bridged(call, message_type, param); + return; + } +#endif + /* release lcr with same cause */ + send_message(MESSAGE_RELEASE, call->ref, param); + call->ref = 0; + /* change to release state */ + call->state = CHAN_LCR_STATE_RELEASE; + /* release asterisk */ + if (ast) + { + ast->hangupcause = call->cause; + if (call->pbx_started) + ast_queue_hangup(ast); + else { + ast_hangup(ast); // call will be destroyed here + } + } } /* @@ -361,16 +714,29 @@ static void lcr_in_disconnect(struct chan_call *call, int message_type, union pa */ static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param) { + struct ast_channel *ast = call->ast; + + CDEBUG(call, call->ast, "Incomming release from LCR. (cause=%d)\n", param->disconnectinfo.cause); + /* release ref */ - call->ref = NULL; + call->ref = 0; /* change to release state */ call->state = CHAN_LCR_STATE_RELEASE; /* copy release info */ - todo + if (!call->cause) + { + call->cause = param->disconnectinfo.cause; + call->location = param->disconnectinfo.location; + } /* if we have an asterisk instance, send hangup, else we are done */ - if (call->ast) + if (ast) { - ast_queue_hangup(call->ast); + ast->hangupcause = call->cause; + if (call->pbx_started) + ast_queue_hangup(ast); + else { + ast_hangup(ast); // call will be destroyed here + } } else { free_call(call); @@ -383,10 +749,57 @@ static void lcr_in_release(struct chan_call *call, int message_type, union param */ static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param) { + struct ast_channel *ast = call->ast; + struct ast_frame fr; + char *p; + + CDEBUG(call, call->ast, "Incomming information from LCR. (dialing=%d)\n", param->information.id); + + if (!ast) return; + + /* pbx not started */ + if (!call->pbx_started) + { + CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n"); + strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1); + lcr_start_pbx(call, ast, param->information.sending_complete); + return; + } + /* copy digits */ - todo and write them, maybe queue them for asterisk - /* send event to asterisk */ - ast_queue_... todo + p = param->information.id; + if (call->state == CHAN_LCR_STATE_IN_DIALING && *p) + { + CDEBUG(call, call->ast, "Asterisk is started, sending DTMF frame.\n"); + while (*p) + { + /* send digit to asterisk */ + memset(&fr, 0, sizeof(fr)); + fr.frametype = AST_FRAME_DTMF; + fr.subclass = *p; + fr.delivery = ast_tv(0, 0); + ast_queue_frame(call->ast, &fr); + p++; + } + } + /* use bridge to forware message not supported by asterisk */ + if (call->state == CHAN_LCR_STATE_CONNECT) { + CDEBUG(call, call->ast, "Call is connected, briding.\n"); + bridge_message_if_bridged(call, message_type, param); + } +} + +/* + * incoming information from LCR + */ +static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param) +{ + CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify); + + if (!call->ast) return; + + /* use bridge to forware message not supported by asterisk */ + bridge_message_if_bridged(call, message_type, param); } /* @@ -394,11 +807,12 @@ static void lcr_in_information(struct chan_call *call, int message_type, union p */ static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param) { - /* copy progress info */ - todo and write them, maybe queue them for asterisk - /* send event to asterisk */ - ast_queue_... todo - or maybe use bride info to forward facility. + CDEBUG(call, call->ast, "Incomming facility from LCR.\n"); + + if (!call->ast) return; + + /* use bridge to forware message not supported by asterisk */ + bridge_message_if_bridged(call, message_type, param); } /* @@ -418,16 +832,17 @@ int receive_message(int message_type, unsigned long ref, union parameter *param) switch(param->bchannel.type) { case BCHANNEL_ASSIGN: + CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx)\n", param->bchannel.handle); if ((bchannel = find_bchannel_handle(param->bchannel.handle))) { - fprintf(stderr, "error: bchannel handle %x already assigned.\n", (int)param->bchannel.handle); + CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle); return(-1); } /* create bchannel */ bchannel = alloc_bchannel(param->bchannel.handle); if (!bchannel) { - fprintf(stderr, "error: alloc bchannel handle %x failed.\n", (int)param->bchannel.handle); + CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle); return(-1); } @@ -450,10 +865,15 @@ int receive_message(int message_type, unsigned long ref, union parameter *param) /* link to call */ if ((call = find_call_ref(ref))) { - bchannel->ref = ref; - call->bchannel_handle = param->bchannel.handle; -#warning hier muesen alle stati gesetzt werden falls sie vor dem b-kanal verfügbar waren - bchannel_join(bchannel, call->bridge_id); + bchannel->call = call; + call->bchannel = bchannel; +#ifdef TODO +hier muesen alle bchannel-features gesetzt werden (pipeline...) falls sie vor dem b-kanal verfügbar waren +#endif + if (call->bridge_id) { + CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n"); + bchannel_join(bchannel, call->bridge_id); + } } if (bchannel_create(bchannel)) bchannel_activate(bchannel, 1); @@ -465,18 +885,13 @@ int receive_message(int message_type, unsigned long ref, union parameter *param) break; case BCHANNEL_REMOVE: + CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle); if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) { - #warning alle fprintf nach ast_log - fprintf(stderr, "error: bchannel handle %x not assigned.\n", (int)param->bchannel.handle); + CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle); return(-1); } - /* unlink from call */ - if ((call = find_call_ref(bchannel->ref))) - { - call->bchannel_handle = 0; - } - /* destroy and remove bchannel */ + /* unklink from call and destroy bchannel */ free_bchannel(bchannel); /* acknowledge */ @@ -487,7 +902,7 @@ int receive_message(int message_type, unsigned long ref, union parameter *param) break; default: - fprintf(stderr, "received unknown bchannel message %d\n", param->bchannel.type); + CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type); } return(0); } @@ -498,9 +913,10 @@ int receive_message(int message_type, unsigned long ref, union parameter *param) if (param->direction) { /* new ref from lcr */ + CDEBUG(NULL, NULL, "Received new ref by LCR, of call from LCR. (ref=%ld)\n", ref); if (!ref || find_call_ref(ref)) { - fprintf(stderr, "illegal new ref %d received\n", ref); + CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref); return(-1); } /* allocate new call instance */ @@ -513,10 +929,12 @@ int receive_message(int message_type, unsigned long ref, union parameter *param) } else { /* new ref, as requested from this remote application */ + CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref); call = find_call_ref(0); if (!call) { /* send release, if ref does not exist */ + CDEBUG(NULL, NULL, "No call found, that requests a ref.\n"); newparam.disconnectinfo.cause = CAUSE_NORMAL; newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL; send_message(MESSAGE_RELEASE, ref, &newparam); @@ -531,8 +949,15 @@ int receive_message(int message_type, unsigned long ref, union parameter *param) else if (call->state == CHAN_LCR_STATE_RELEASE) { /* send release */ - newparam.disconnectinfo.cause = todo - newparam.disconnectinfo.location = todo + if (call->cause) + { + newparam.disconnectinfo.cause = call->cause; + newparam.disconnectinfo.location = call->location; + } else + { + newparam.disconnectinfo.cause = 16; + newparam.disconnectinfo.location = 5; + } send_message(MESSAGE_RELEASE, ref, &newparam); /* free call */ free_call(call); @@ -545,13 +970,14 @@ int receive_message(int message_type, unsigned long ref, union parameter *param) /* check ref */ if (!ref) { - fprintf(stderr, "received message %d without ref\n", message_type); + CERROR(NULL, NULL, "Received message %d without ref.\n", message_type); return(-1); } call = find_call_ref(ref); if (!call) { /* ignore ref that is not used (anymore) */ + CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type); return(0); } @@ -587,31 +1013,71 @@ int receive_message(int message_type, unsigned long ref, union parameter *param) break; case MESSAGE_INFORMATION: - lcr_in_disconnect(call, message_type, param); + lcr_in_information(call, message_type, param); + break; + + case MESSAGE_NOTIFY: + lcr_in_notify(call, message_type, param); break; case MESSAGE_FACILITY: - lcr_in_disconnect(call, message_type, param); + lcr_in_facility(call, message_type, param); break; - case MESSAGE_PATTERN: -#warning todo + case MESSAGE_PATTERN: // audio available from LCR break; - case MESSAGE_NOPATTERN: -#warning todo + case MESSAGE_NOPATTERN: // audio not available from LCR break; - case MESSAGE_AUDIOPATH: -#warning todo + case MESSAGE_AUDIOPATH: // if remote audio connected or hold + call->audiopath = param->audiopath; break; default: -#warning unhandled + CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type); + break; } return(0); } +/* + * release all calls (due to broken socket) + */ +static void release_all_calls(void) +{ + struct chan_call *call; + +again: + call = call_first; + while(call) + { + /* no ast, so we may directly free call */ + if (!call->ast) { + CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n"); + free_call(call); + goto again; + } + /* already in release process */ + if (call->state == CHAN_LCR_STATE_RELEASE) { + call = call->next; + continue; + } + /* release or queue release */ + call->ref = 0; + call->state = CHAN_LCR_STATE_RELEASE; + if (!call->pbx_started) + { + CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n"); + ast_hangup(call->ast); // call will be destroyed here + goto again; + } + CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n"); + ast_queue_hangup(call->ast); + call = call->next; + } +} + /* asterisk handler * warning! not thread safe @@ -626,34 +1092,32 @@ int handle_socket(void) int sock; - #warning SOCKET FEHLT! /* read from socket */ len = read(sock, &msg, sizeof(msg)); if (len == 0) { - printf("Socket closed\n"); + CERROR(NULL, NULL, "Socket closed.\n"); return(-1); // socket closed } if (len > 0) { if (len != sizeof(msg)) { - fprintf(stderr, "Socket short read (%d)\n", len); + CERROR(NULL, NULL, "Socket short read. (len %d)\n", len); return(-1); // socket error } if (msg.message != ADMIN_MESSAGE) { - fprintf(stderr, "Socket received illegal message %d\n", msg.message); + CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message); return(-1); // socket error } receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param); - printf("message received %d\n", msg.u.msg.type); work = 1; } else { if (errno != EWOULDBLOCK) { - fprintf(stderr, "Socket error %d\n", errno); + CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno); return(-1); } } @@ -665,14 +1129,14 @@ int handle_socket(void) len = write(sock, &admin->msg, sizeof(msg)); if (len == 0) { - printf("Socket closed\n"); + CERROR(NULL, NULL, "Socket closed.\n"); return(-1); // socket closed } if (len > 0) { if (len != sizeof(msg)) { - fprintf(stderr, "Socket short write (%d)\n", len); + CERROR(NULL, NULL, "Socket short write. (len %d)\n", len); return(-1); // socket error } /* free head */ @@ -684,7 +1148,7 @@ int handle_socket(void) { if (errno != EWOULDBLOCK) { - fprintf(stderr, "Socket error %d\n", errno); + CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno); return(-1); } } @@ -693,7 +1157,7 @@ int handle_socket(void) } /* - * open and close socket + * open and close socket and thread */ int open_socket(void) { @@ -708,7 +1172,7 @@ int open_socket(void) /* open socket */ if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { - ast_log(LOG_ERROR, "Failed to create socket.\n"); + CERROR(NULL, NULL, "Failed to create socket.\n"); return(sock); } @@ -721,7 +1185,7 @@ int open_socket(void) if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) { close(sock); - ast_log(LOG_ERROR, "Failed to connect to socket \"%s\". Is LCR running?\n", sock_address.sun_path); + CERROR(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path); return(conn); } @@ -729,7 +1193,7 @@ int open_socket(void) if ((ret = ioctl(sock, FIONBIO, (unsigned char *)(&on))) < 0) { close(sock); - ast_log(LOG_ERROR, "Failed to set socket into non-blocking IO.\n"); + CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n"); return(ret); } @@ -748,125 +1212,89 @@ void close_socket(int sock) close(sock); } - -socket muss per timer fuer das öffnen checken static void *chan_thread(void *arg) { int work; + int ret; + union parameter param; + time_t retry = 0, now; - pthread_mutex_lock(&chan_lock); + bchannel_pid = getpid(); + + memset(¶m, 0, sizeof(union parameter)); + if (lcr_sock > 0) + time(&retry); - while(!quit) - { + ast_mutex_lock(&chan_lock); + + while(!quit) { work = 0; - /* handle socket */ - int ret = handle_socket(); - if (ret < 0) - break; - if (ret) - work = 1; + if (lcr_sock > 0) { + /* handle socket */ + ret = handle_socket(); + if (ret < 0) { + CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n"); + close_socket(lcr_sock); + lcr_sock = -1; + release_all_calls(); + time(&retry); + } + if (ret) + work = 1; + } else { + time(&now); + if (retry && now-retry > 5) { + CERROR(NULL, NULL, "Retry to open socket.\n"); + retry = 0; + if (!(lcr_sock = open_socket())) { + time(&retry); + } + work = 1; + } + + } /* handle mISDN */ ret = bchannel_handle(); if (ret) work = 1; - if (!work) - { - pthread_mutex_unlock(&chan_lock); + if (!work) { + ast_mutex_unlock(&chan_lock); usleep(30000); - pthread_mutex_lock(&chan_lock); + ast_mutex_lock(&chan_lock); } } + + CERROR(NULL, NULL, "Thread exit.\n"); - pthread_mutex_unlock(&chan_lock); + ast_mutex_unlock(&chan_lock); return NULL; } /* - * send setup info to LCR - * this function is called, when asterisk call is received and ref is received + * new asterisk instance */ -static void send_setup_to_lcr(struct chan_call *call) +static +struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause) { - if (!ast || !call->ref) - return; - - /* send setup message to LCR */ - memset(&newparam, 0, sizeof(union parameter)); - newparam.setup.xxxxxx = - send_message(MESSAGE_SETUP, call->ref, &newparam); - /* change to outgoing setup state */ - call->state = CHAN_LCR_STATE_OUT_SETUP; -} - -#if 0 -CHRISTIAN: das war ein konflikt beim pullen -siehe anderes lcr_request(); -bedenke: das ast_log muss noch üeberall eingepflegt werden + union parameter newparam; + struct ast_channel *ast; + struct chan_call *call; -static struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause) -{ - struct chan_call *call=alloc_call(); + ast_mutex_lock(&chan_lock); - if (call) { -#warning hier muss jetzt wohl eine Ref angefordert werden! - ast=lcr_ast_new(call, ext, NULL, 0 ); + CDEBUG(NULL, NULL, "Received request from Asterisk.\n"); - if (ast) { - call->ast=ast; - } else { - ast_log(LOG_WARNING, "Could not create new Asterisk Channel\n"); - free_call(call); - } - } else { - ast_log(LOG_WARNING, "Could not create new Lcr Call Handle\n"); + /* if socket is closed */ + if (lcr_sock < 0) + { + CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n"); + return NULL; } - - return ast; -} - - struct ast_channel *ast=NULL; - - char buf[128]; - char *port_str, *ext, *p; - int port; - - ast_copy_string(buf, data, sizeof(buf)-1); - p=buf; - port_str=strsep(&p, "/"); - ext=strsep(&p, "/"); - ast_verbose("portstr:%s ext:%s\n",port_str, ext); - - sprintf(buf,"%s/%s",lcr_type,(char*)data); -#endif - -/* - * send dialing info to LCR - * this function is called, when setup acknowledge is received and dialing - * info is available. - */ -static void send_dialing_to_lcr(struct chan_call *call) -{ - if (!ast || !call->ref || !call->dialque) - return; - /* send setup message to LCR */ - memset(&newparam, 0, sizeof(union parameter)); - strncpy(newparam.dialinginfo.id, call->dialque, sizeof(newparam.dialinginfo.id)-1); - call->dialque[0] = '\0'; - send_message(MESSAGE_INFORMATION, call->ref, &newparam); -} - -/* - * new asterisk instance - */ -static struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause) -{ - pthread_mutex_lock(&chan_lock); - /* create call instance */ call = alloc_call(); if (!call) @@ -875,9 +1303,10 @@ static struct ast_channel *lcr_request(const char *type, int format, void *data, return NULL; } /* create asterisk channel instrance */ - ast = ast_channel_alloc(1); + ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel); if (!ast) { + CERROR(NULL, NULL, "Failed to create Asterisk channel.\n"); free_call(call); /* failed to create instance */ return NULL; @@ -885,6 +1314,22 @@ static struct ast_channel *lcr_request(const char *type, int format, void *data, /* link together */ ast->tech_pvt = call; call->ast = ast; + ast->tech = &lcr_tech; + /* configure channel */ +#ifdef TODO + snprintf(ast->name, sizeof(ast->name), "%s/%d", lcr_type, ++glob_channel); + ast->name[sizeof(ast->name)-1] = '\0'; +#endif +#ifdef TODO + ast->nativeformats = configfile->lawformat; + ast->readformat = ast->rawreadformat = configfile->lawformat; + ast->writeformat = ast->rawwriteformat = configfile->lawformat; +#else + ast->nativeformats = AST_FORMAT_ALAW; + ast->readformat = ast->rawreadformat = AST_FORMAT_ALAW; + ast->writeformat = ast->rawwriteformat = AST_FORMAT_ALAW; +#endif + ast->hangupcause = 0; /* send MESSAGE_NEWREF */ memset(&newparam, 0, sizeof(union parameter)); newparam.direction = 0; /* request from app */ @@ -892,7 +1337,9 @@ static struct ast_channel *lcr_request(const char *type, int format, void *data, /* set state */ call->state = CHAN_LCR_STATE_OUT_PREPARE; - pthread_mutex_unlock(&chan_lock); + ast_mutex_unlock(&chan_lock); + + return ast; } /* @@ -900,95 +1347,135 @@ static struct ast_channel *lcr_request(const char *type, int format, void *data, */ static int lcr_call(struct ast_channel *ast, char *dest, int timeout) { - struct chan_call *call=ast->tech_pvt; - char buf[128]; - char *port_str, *dad, *p; + struct chan_call *call; - if (!call) return -1; + ast_mutex_lock(&chan_lock); + call = ast->tech_pvt; + if (!call) { + CERROR(NULL, ast, "Received call from Asterisk, but no call instance exists.\n"); + ast_mutex_unlock(&chan_lock); + return -1; + } - pthread_mutex_lock(&chan_lock); + CDEBUG(call, ast, "Received call from Asterisk.\n"); - hier muss noch - ast_copy_string(buf, dest, sizeof(buf)-1); - p=buf; - port_str=strsep(&p, "/"); - dad=strsep(&p, "/"); + call->pbx_started = 1; /* send setup message, if we already have a callref */ if (call->ref) send_setup_to_lcr(call); - if (lcr_debug) - ast_verbose("Call: ext:%s dest:(%s) -> dad(%s) \n", ast->exten,dest, dad); - -#warning hier müssen wi eine der geholten REFs nehmen und ein SETUP schicken, die INFOS zum SETUP stehen im Ast pointer drin, bzw. werden hier übergeben. - - pthread_mutex_unlock(&chan_lock); + ast_mutex_unlock(&chan_lock); return 0; } static int lcr_digit(struct ast_channel *ast, char digit) { + struct chan_call *call; + union parameter newparam; char buf[]="x"; - if (!call) return -1; - /* only pass IA5 number space */ if (digit > 126 || digit < 32) return 0; - pthread_mutex_lock(&chan_lock); + ast_mutex_lock(&chan_lock); + call = ast->tech_pvt; + if (!call) { + CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n"); + ast_mutex_unlock(&chan_lock); + return -1; + } + + CDEBUG(call, ast, "Received digit Asterisk.\n"); /* send information or queue them */ if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) { - send_dialing_to_lcr(call); + CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n"); + memset(&newparam, 0, sizeof(union parameter)); + newparam.information.id[0] = digit; + newparam.information.id[1] = '\0'; + send_message(MESSAGE_INFORMATION, call->ref, &newparam); } else if (!call->ref && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)); { + CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n"); *buf = digit; - strncat(call->dialque, buf, strlen(char->dialque)-1); - } else - { -digits kommen, koennen aber nicht verwendet werden. - sollen wir sie als info senden (im connect zb.) + strncat(call->dialque, buf, strlen(call->dialque)-1); } - pthread_mutex_unlock(&chan_lock); + ast_mutex_unlock(&chan_lock); return(0); } -static int lcr_answer(struct ast_channel *c) +static int lcr_answer(struct ast_channel *ast) { - struct chan_call *call=c->tech_pvt; - if (!call) return -1; - pthread_mutex_lock(&chan_lock); - pthread_mutex_unlock(&chan_lock); + union parameter newparam; + struct chan_call *call; + + ast_mutex_lock(&chan_lock); + call = ast->tech_pvt; + if (!call) { + CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n"); + ast_mutex_unlock(&chan_lock); + return -1; + } + + CDEBUG(call, ast, "Received answer from Asterisk.\n"); + + /* copy connectinfo, if bridged */ + if (call->bridge_call) + memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info)); + /* send connect message to lcr */ + memset(&newparam, 0, sizeof(union parameter)); + memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info)); + send_message(MESSAGE_CONNECT, call->ref, &newparam); + /* change state */ + call->state = CHAN_LCR_STATE_CONNECT; + + ast_mutex_unlock(&chan_lock); return 0; } static int lcr_hangup(struct ast_channel *ast) { - struct chan_call *call = ast->tech_pvt; + union parameter newparam; + struct chan_call *call; + pthread_t tid = pthread_self(); + + if (!pthread_equal(tid, chan_tid)) + ast_mutex_lock(&chan_lock); + call = ast->tech_pvt; + if (!call) { + CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n"); + if (!pthread_equal(tid, chan_tid)) + ast_mutex_unlock(&chan_lock); + return -1; + } - if (!call) - return 0; + if (!pthread_equal(tid, chan_tid)) + CDEBUG(call, ast, "Received hangup from Asterisk thread.\n"); + else + CDEBUG(call, ast, "Received hangup from LCR thread.\n"); - pthread_mutex_lock(&chan_lock); /* disconnect asterisk, maybe not required */ ast->tech_pvt = NULL; + ast->fds[0] = -1; if (call->ref) { /* release */ + CDEBUG(call, ast, "Releasing ref and freeing call instance.\n"); memset(&newparam, 0, sizeof(union parameter)); newparam.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL; newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL; send_message(MESSAGE_RELEASE, call->ref, &newparam); /* remove call */ free_call(call); - pthread_mutex_unlock(&chan_lock); + if (!pthread_equal(tid, chan_tid)) + ast_mutex_unlock(&chan_lock); return 0; } else { @@ -996,81 +1483,270 @@ static int lcr_hangup(struct ast_channel *ast) if (call->state == CHAN_LCR_STATE_RELEASE) { /* we get the response to our release */ + CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n"); free_call(call); } else { /* during prepare, we change to release state */ + CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n"); call->state = CHAN_LCR_STATE_RELEASE; } } - pthread_mutex_unlock(&chan_lock); + if (!pthread_equal(tid, chan_tid)) + ast_mutex_unlock(&chan_lock); return 0; } -static int lcr_write(struct ast_channel *c, struct ast_frame *f) +static int lcr_write(struct ast_channel *ast, struct ast_frame *f) { - struct chan_call *call= c->tech_pvt; - if (!call) return 0; - pthread_mutex_lock(&chan_lock); - pthread_mutex_unlock(&chan_lock); + struct chan_call *call; + unsigned char buffer[1024], *s, *d = buffer; + int i, ii; + + ast_mutex_lock(&chan_lock); + call = ast->tech_pvt; + if (!call) { + ast_mutex_unlock(&chan_lock); + return -1; + } + if (call->bchannel && ((ii = f->samples))) + { + if (ii > sizeof(buffer)) + ii = sizeof(buffer); + s = f->data; + for (i = 0; i < ii; i++) + *d++ = flip_bits[*s++]; + bchannel_transmit(call->bchannel, buffer, ii); + } + ast_mutex_unlock(&chan_lock); + return 0; } -static struct ast_frame *lcr_read(struct ast_channel *c) +static struct ast_frame *lcr_read(struct ast_channel *ast) { - struct chan_call *call = c->tech_pvt; - if (!call) return 0; - pthread_mutex_lock(&chan_lock); - pthread_mutex_unlock(&chan_lock); + struct chan_call *call; + int i, len; + unsigned char *p; + + ast_mutex_lock(&chan_lock); + call = ast->tech_pvt; + if (!call) { + ast_mutex_unlock(&chan_lock); + return NULL; + } + len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff)); + if (len <= 0) + return NULL; + + p = call->read_buff; + for (i = 0; i < len; i++) { + *p = flip_bits[*p]; + p++; + } + + call->read_fr.frametype = AST_FRAME_VOICE; +#ifdef TODO + format aus config +#endif + call->read_fr.subclass = AST_FORMAT_ALAW; + call->read_fr.datalen = len; + call->read_fr.samples = len; + call->read_fr.delivery = ast_tv(0,0); + call->read_fr.data = call->read_buff; + ast_mutex_unlock(&chan_lock); + + return &call->read_fr; } -static int lcr_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen) +static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen) { + union parameter newparam; int res = -1; - if (!call) return -1; + struct chan_call *call; - pthread_mutex_lock(&chan_lock); + ast_mutex_lock(&chan_lock); + call = ast->tech_pvt; + if (!call) { + CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n"); + ast_mutex_unlock(&chan_lock); + return -1; + } switch (cond) { case AST_CONTROL_BUSY: + CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n"); + /* send message to lcr */ + memset(&newparam, 0, sizeof(union parameter)); + newparam.disconnectinfo.cause = 17; + newparam.disconnectinfo.location = 5; + send_message(MESSAGE_DISCONNECT, call->ref, &newparam); + /* change state */ + call->state = CHAN_LCR_STATE_OUT_DISCONNECT; + /* return */ + ast_mutex_unlock(&chan_lock); + return 0; case AST_CONTROL_CONGESTION: - case AST_CONTROL_RINGING: - pthread_mutex_unlock(&chan_lock); + CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk.\n"); + /* return */ + ast_mutex_unlock(&chan_lock); return -1; + case AST_CONTROL_RINGING: + CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n"); + /* send message to lcr */ + memset(&newparam, 0, sizeof(union parameter)); + send_message(MESSAGE_ALERTING, call->ref, &newparam); + /* change state */ + call->state = CHAN_LCR_STATE_OUT_ALERTING; + /* return */ + ast_mutex_unlock(&chan_lock); + return 0; case -1: - pthread_mutex_unlock(&chan_lock); + /* return */ + ast_mutex_unlock(&chan_lock); return 0; case AST_CONTROL_VIDUPDATE: res = -1; break; case AST_CONTROL_HOLD: - ast_verbose(" << Console Has Been Placed on Hold >> \n"); - //ast_moh_start(c, data, g->mohinterpret); + CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n"); + /* send message to lcr */ + memset(&newparam, 0, sizeof(union parameter)); + newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD; + send_message(MESSAGE_NOTIFY, call->ref, &newparam); break; case AST_CONTROL_UNHOLD: - ast_verbose(" << Console Has Been Retrieved from Hold >> \n"); - //ast_moh_stop(c); + CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n"); + /* send message to lcr */ + memset(&newparam, 0, sizeof(union parameter)); + newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL; + send_message(MESSAGE_NOTIFY, call->ref, &newparam); break; default: - ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name); - pthread_mutex_unlock(&chan_lock); + CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond); + /* return */ + ast_mutex_unlock(&chan_lock); return -1; } - pthread_mutex_unlock(&chan_lock); + /* return */ + ast_mutex_unlock(&chan_lock); return 0; } +/* + * bridge process + */ +enum ast_bridge_result lcr_bridge(struct ast_channel *ast1, + struct ast_channel *ast2, int flags, + struct ast_frame **fo, + struct ast_channel **rc, int timeoutms) + +{ + struct chan_call *call1, *call2; + struct ast_channel *carr[2], *who; + int to = -1; + struct ast_frame *f; + int bridge_id; + + CDEBUG(NULL, NULL, "Received briding request from Asterisk.\n"); + + /* join via dsp (if the channels are currently open) */ + ast_mutex_lock(&chan_lock); + bridge_id = new_bridge_id(); + call1 = ast1->tech_pvt; + call2 = ast2->tech_pvt; + if (call1 && call2) + { + call1->bridge_id = bridge_id; + if (call1->bchannel) + bchannel_join(call1->bchannel, bridge_id); + call1->bridge_call = call2; + } + if (call2) + { + call2->bridge_id = bridge_id; + if (call2->bchannel) + bchannel_join(call2->bchannel, bridge_id); + call2->bridge_call = call1; + } + ast_mutex_unlock(&chan_lock); + + while(1) { + who = ast_waitfor_n(carr, 2, &to); + + if (!who) { + CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n"); + break; + } + f = ast_read(who); + + if (!f || f->frametype == AST_FRAME_CONTROL) { + /* got hangup .. */ + *fo=f; + *rc=who; + break; + } + + if ( f->frametype == AST_FRAME_DTMF ) { + *fo=f; + *rc=who; + break; + } + + + if (who == ast1) { + ast_write(ast2,f); + } + else { + ast_write(ast1,f); + } + + } + + CDEBUG(NULL, NULL, "Releasing bride.\n"); + + /* split channels */ + ast_mutex_lock(&chan_lock); + call1 = ast1->tech_pvt; + call2 = ast2->tech_pvt; + if (call1) + { + call1->bridge_id = 0; + if (call1->bchannel) + bchannel_join(call1->bchannel, 0); + if (call1->bridge_call) + call1->bridge_call->bridge_call = NULL; + call1->bridge_call = NULL; + } + if (call2) + { + call2->bridge_id = 0; + if (call2->bchannel) + bchannel_join(call2->bchannel, 0); + if (call2->bridge_call) + call2->bridge_call->bridge_call = NULL; + call2->bridge_call = NULL; + } + ast_mutex_unlock(&chan_lock); + + + return AST_BRIDGE_COMPLETE; +} static struct ast_channel_tech lcr_tech = { - .type=lcr_type, + .type="LCR", .description="Channel driver for connecting to Linux-Call-Router", +#ifdef TODO + law from config +#else .capabilities=AST_FORMAT_ALAW, +#endif .requester=lcr_request, - .send_digit=lcr_digit, + .send_digit_begin=lcr_digit, .call=lcr_call, -// .bridge=lcr_bridge, + .bridge=lcr_bridge, .hangup=lcr_hangup, .answer=lcr_answer, .read=lcr_read, @@ -1081,63 +1757,43 @@ static struct ast_channel_tech lcr_tech = { .properties=0 }; -#warning das muss mal aus der config datei gelesen werden: -char lcr_context[]="from-lcr"; - -static struct ast_channel *lcr_ast_new(struct chan_call *call, char *exten, char *callerid, int ref) -{ - struct ast_channel *tmp; - char *cid_name = 0, *cid_num = 0; - - - if (callerid) - ast_callerid_parse(callerid, &cid_name, &cid_num); - - tmp = ast_channel_alloc(1, AST_STATE_RESERVED, cid_num, cid_name, "", exten, "", 0, "%s/%d", lcr_type, ref); - - if (tmp) { - tmp->tech = &lcr_tech; - tmp->writeformat = AST_FORMAT_ALAW; - tmp->readformat = AST_FORMAT_ALAW; - - ast_copy_string(tmp->context, lcr_context, AST_MAX_CONTEXT); - - } - - return tmp; -} - - /* * cli */ static int lcr_show_lcr (int fd, int argc, char *argv[]) { + return 0; } static int lcr_show_calls (int fd, int argc, char *argv[]) { + return 0; } static int lcr_reload_routing (int fd, int argc, char *argv[]) { + return 0; } static int lcr_reload_interfaces (int fd, int argc, char *argv[]) { + return 0; } static int lcr_port_block (int fd, int argc, char *argv[]) { + return 0; } static int lcr_port_unblock (int fd, int argc, char *argv[]) { + return 0; } static int lcr_port_unload (int fd, int argc, char *argv[]) { + return 0; } static struct ast_cli_entry cli_show_lcr = @@ -1195,26 +1851,30 @@ static struct ast_cli_entry cli_port_unload = */ int load_module(void) { -// ast_mutex_init(&release_lock); + int i; -// lcr_cfg_update_ptp(); + for (i = 0; i < 256; i++) + flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8) + || (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1); - pthread_mutex_init(&chan_lock, NULL); + ast_mutex_init(&chan_lock); + ast_mutex_init(&log_lock); if (!(lcr_sock = open_socket())) { - ast_log(LOG_ERROR, "Unable to connect\n"); - lcr_sock = -1; /* continue with closed socket */ } if (!bchannel_initialize()) { - ast_log(LOG_ERROR, "Unable to open mISDN device\n"); + CERROR(NULL, NULL, "Unable to open mISDN device\n"); + close_socket(lcr_sock); return -1; } mISDN_created = 1; if (ast_channel_register(&lcr_tech)) { - ast_log(LOG_ERROR, "Unable to register channel class\n"); + CERROR(NULL, NULL, "Unable to register channel class\n"); + bchannel_deinitialize(); + close_socket(lcr_sock); return -1; } @@ -1247,15 +1907,17 @@ int load_module(void) lcr_cfg_get( 0, LCR_GEN_TRACEFILE, global_tracefile, BUFFERSIZE); - chan_lcr_log(0, 0, "-- mISDN Channel Driver Registred -- (BE AWARE THIS DRIVER IS EXPERIMENTAL!)\n"); ======= //lcr_cfg_get( 0, LCR_GEN_TRACEFILE, global_tracefile, BUFFERSIZE); #endif - quit = 1; - if ((pthread_create(&chan_tid, NULL, chan_thread, arg)<0)) + quit = 0; + if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) { - failed to create thread + /* failed to create thread */ + bchannel_deinitialize(); + close_socket(lcr_sock); + ast_channel_unregister(&lcr_tech); return -1; } return 0; @@ -1264,7 +1926,7 @@ int load_module(void) int unload_module(void) { /* First, take us out of the channel loop */ - ast_log(LOG_VERBOSE, "-- Unregistering mISDN Channel Driver --\n"); + CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n"); quit = 1; pthread_join(chan_tid, NULL); @@ -1284,13 +1946,14 @@ int unload_module(void) return 0; } -static int reload_module(void) +int reload_module(void) { // reload_config(); return 0; } - +#ifdef TODO +mutex init fehlt noch ast_mutex_t usecnt_lock; int usecnt; @@ -1302,6 +1965,7 @@ int usecount(void) ast_mutex_unlock(&usecnt_lock); return res; } +#endif char *desc="Channel driver for lcr"; @@ -1316,12 +1980,4 @@ char *key(void) return ASTERISK_GPL_KEY; } -#define AST_MODULE "chan_lcr" -AST_MODULE_INFO(ASTERISK_GPL_KEY, - AST_MODFLAG_DEFAULT, - "Channel driver for LCR", - .load = load_module, - .unload = unload_module, - .reload = reload_module, - );