--- /tmp/asterisk/channels/chan_dahdi.c 2008-11-14 02:10:18.000000000 +0200 +++ channels/chan_dahdi.c 2008-11-14 02:04:46.000000000 +0200 @@ -330,6 +330,7 @@ int span; int resetting; int resetpos; + int h324musellc; #ifdef HAVE_PRI_INBANDDISCONNECT unsigned int inbanddisconnect:1; /*!< Should we support inband audio after receiving DISCONNECT? */ #endif @@ -624,6 +625,7 @@ .localprefix = "", .privateprefix = "", .unknownprefix = "", + .h324musellc = 0, .resetinterval = 3600 }, @@ -2180,11 +2182,28 @@ else exclusive = 1; } - + + if (p->digital) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "digital call, setting user information layer 1 to %d (0x%x)\n", + ast->userinformationlayer1, ast->userinformationlayer1); + } + pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1); pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability, - (p->digital ? -1 : - ((p->law == DAHDI_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW))); + (p->digital ? ast->userinformationlayer1 : + ((p->law == DAHDI_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW))); + + /* check if h324m is configure to use low layer compatibility instead of bearer capability */ + ast_verbose(VERBOSE_PREFIX_3 "dahdi call: h324musellc=%d, ast->userinformationlayer1=%d\n", + p->pri->h324musellc, ast->userinformationlayer1); + + if (p->pri->h324musellc && (ast->userinformationlayer1==PRI_LAYER_1_H223_H245)) { + pri_sr_set_llc(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability, + (p->digital ? ast->userinformationlayer1 : + ((p->law == DAHDI_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW))); + } + if (p->pri->facilityenable) pri_facility_enable(p->pri->pri); @@ -3588,7 +3607,7 @@ static void *ss_thread(void *data); -static struct ast_channel *dahdi_new(struct dahdi_pvt *, int, int, int, int, int); +static struct ast_channel *dahdi_new(struct dahdi_pvt *, int, int, int, int, int, int); static int attempt_transfer(struct dahdi_pvt *p) { @@ -4368,7 +4387,7 @@ goto winkflashdone; } /* Make new channel */ - chan = dahdi_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0); + chan = dahdi_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0, -1); if (p->dahditrcallerid) { if (!p->origcid_num) p->origcid_num = ast_strdup(p->cid_num); @@ -5316,7 +5335,8 @@ return res; } -static struct ast_channel *dahdi_new(struct dahdi_pvt *i, int state, int startpbx, int index, int law, int transfercapability) +static struct ast_channel *dahdi_new(struct dahdi_pvt *i, int state, int startpbx, int index, int law, + int transfercapability, int userinformationlayer1) { struct ast_channel *tmp; int deflaw; @@ -5364,943 +5384,1159 @@ ast_log(LOG_WARNING, "Unable to get parameters, assuming MULAW: %s\n", strerror(errno)); ps.curlaw = DAHDI_LAW_MULAW; } - if (ps.curlaw == DAHDI_LAW_ALAW) + if (ps.curlaw == DAHDI_LAW_ALAW) { + ast_log(LOG_DEBUG, "dahdi_new: ps.curlaw=DAHDI_LAW_ALAW, setting deflaw to AST_FORMAT_ALAW\n"); deflaw = AST_FORMAT_ALAW; - else + } else { + ast_log(LOG_DEBUG, "dahdi_new: ps.curlaw!=DAHDI_LAW_ALAW, setting deflaw to AST_FORMAT_ULAW\n"); deflaw = AST_FORMAT_ULAW; + } if (law) { - if (law == DAHDI_LAW_ALAW) + if (law == DAHDI_LAW_ALAW) { + ast_log(LOG_DEBUG, "zt_new: law=DAHDI_LAW_ALAW, setting deflaw to AST_FORMAT_ALAW\n"); deflaw = AST_FORMAT_ALAW; - else + } else { + ast_log(LOG_DEBUG, "zt_new: law=DAHDI_LAW_ALAW, setting deflaw to AST_FORMAT_ULAW\n"); deflaw = AST_FORMAT_ULAW; - } - tmp->fds[0] = i->subs[index].dfd; - tmp->nativeformats = AST_FORMAT_SLINEAR | deflaw; - /* Start out assuming ulaw since it's smaller :) */ - tmp->rawreadformat = deflaw; - tmp->readformat = deflaw; - tmp->rawwriteformat = deflaw; - tmp->writeformat = deflaw; - i->subs[index].linear = 0; - dahdi_setlinear(i->subs[index].dfd, i->subs[index].linear); - features = 0; - if (index == SUB_REAL) { - if (i->busydetect && CANBUSYDETECT(i)) - features |= DSP_FEATURE_BUSY_DETECT; - if ((i->callprogress & 1) && CANPROGRESSDETECT(i)) - features |= DSP_FEATURE_CALL_PROGRESS; - if ((!i->outgoing && (i->callprogress & 4)) || - (i->outgoing && (i->callprogress & 2))) { - features |= DSP_FEATURE_FAX_DETECT; - } -#ifdef DAHDI_TONEDETECT - x = DAHDI_TONEDETECT_ON | DAHDI_TONEDETECT_MUTE; - if (ioctl(i->subs[index].dfd, DAHDI_TONEDETECT, &x)) { -#endif - i->hardwaredtmf = 0; - features |= DSP_FEATURE_DTMF_DETECT; -#ifdef DAHDI_TONEDETECT - } else if (NEED_MFDETECT(i)) { - i->hardwaredtmf = 1; - features |= DSP_FEATURE_DTMF_DETECT; } -#endif } - if (features) { - if (i->dsp) { - ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", tmp->name); - } else { - if (i->channel != CHAN_PSEUDO) - i->dsp = ast_dsp_new(); - else - i->dsp = NULL; + + tmp->fds[0] = i->subs[index].dfd; + tmp->nativeformats = AST_FORMAT_SLINEAR | deflaw; + /* Start out assuming ulaw since it's smaller :) */ + tmp->rawreadformat = deflaw; + tmp->readformat = deflaw; + tmp->rawwriteformat = deflaw; + tmp->writeformat = deflaw; + i->subs[index].linear = 0; + dahdi_setlinear(i->subs[index].dfd, i->subs[index].linear); + features = 0; + if (index == SUB_REAL) { + if (i->busydetect && CANBUSYDETECT(i)) + features |= DSP_FEATURE_BUSY_DETECT; + if ((i->callprogress & 1) && CANPROGRESSDETECT(i)) + features |= DSP_FEATURE_CALL_PROGRESS; + if ((!i->outgoing && (i->callprogress & 4)) || + (i->outgoing && (i->callprogress & 2))) { + features |= DSP_FEATURE_FAX_DETECT; + } + #ifdef DAHDI_TONEDETECT + x = DAHDI_TONEDETECT_ON | DAHDI_TONEDETECT_MUTE; + if (ioctl(i->subs[index].dfd, DAHDI_TONEDETECT, &x)) { + #endif + i->hardwaredtmf = 0; + features |= DSP_FEATURE_DTMF_DETECT; + #ifdef DAHDI_TONEDETECT + } else if (NEED_MFDETECT(i)) { + i->hardwaredtmf = 1; + features |= DSP_FEATURE_DTMF_DETECT; + } + #endif + } + if (features) { if (i->dsp) { - i->dsp_features = features; -#ifdef HAVE_PRI - /* We cannot do progress detection until receives PROGRESS message */ - if (i->outgoing && (i->sig == SIG_PRI)) { - /* Remember requested DSP features, don't treat - talking as ANSWER */ - i->dsp_features = features & ~DSP_PROGRESS_TALK; - features = 0; - } -#endif - ast_dsp_set_features(i->dsp, features); - ast_dsp_digitmode(i->dsp, DSP_DIGITMODE_DTMF | i->dtmfrelax); - if (!ast_strlen_zero(progzone)) - ast_dsp_set_call_progress_zone(i->dsp, progzone); - if (i->busydetect && CANBUSYDETECT(i)) { - ast_dsp_set_busy_count(i->dsp, i->busycount); - ast_dsp_set_busy_pattern(i->dsp, i->busy_tonelength, i->busy_quietlength); + ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", tmp->name); + } else { + if (i->channel != CHAN_PSEUDO) + i->dsp = ast_dsp_new(); + else + i->dsp = NULL; + if (i->dsp) { + i->dsp_features = features; + #ifdef HAVE_PRI + /* We cannot do progress detection until receives PROGRESS message */ + if (i->outgoing && (i->sig == SIG_PRI)) { + /* Remember requested DSP features, don't treat + talking as ANSWER */ + i->dsp_features = features & ~DSP_PROGRESS_TALK; + features = 0; + } + #endif + ast_dsp_set_features(i->dsp, features); + ast_dsp_digitmode(i->dsp, DSP_DIGITMODE_DTMF | i->dtmfrelax); + if (!ast_strlen_zero(progzone)) + ast_dsp_set_call_progress_zone(i->dsp, progzone); + if (i->busydetect && CANBUSYDETECT(i)) { + ast_dsp_set_busy_count(i->dsp, i->busycount); + ast_dsp_set_busy_pattern(i->dsp, i->busy_tonelength, i->busy_quietlength); + } } } } - } - - if (state == AST_STATE_RING) - tmp->rings = 1; - tmp->tech_pvt = i; - if ((i->sig == SIG_FXOKS) || (i->sig == SIG_FXOGS) || (i->sig == SIG_FXOLS)) { - /* Only FXO signalled stuff can be picked up */ - tmp->callgroup = i->callgroup; - tmp->pickupgroup = i->pickupgroup; - } - if (!ast_strlen_zero(i->language)) - ast_string_field_set(tmp, language, i->language); - if (!i->owner) - i->owner = tmp; - if (!ast_strlen_zero(i->accountcode)) - ast_string_field_set(tmp, accountcode, i->accountcode); - if (i->amaflags) - tmp->amaflags = i->amaflags; - i->subs[index].owner = tmp; - ast_copy_string(tmp->context, i->context, sizeof(tmp->context)); - ast_string_field_set(tmp, call_forward, i->call_forward); - /* If we've been told "no ADSI" then enforce it */ - if (!i->adsi) - tmp->adsicpe = AST_ADSI_UNAVAILABLE; - if (!ast_strlen_zero(i->exten)) - ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten)); - if (!ast_strlen_zero(i->rdnis)) - tmp->cid.cid_rdnis = ast_strdup(i->rdnis); - if (!ast_strlen_zero(i->dnid)) - tmp->cid.cid_dnid = ast_strdup(i->dnid); - - /* Don't use ast_set_callerid() here because it will - * generate a needless NewCallerID event */ -#ifdef PRI_ANI - if (!ast_strlen_zero(i->cid_ani)) - tmp->cid.cid_ani = ast_strdup(i->cid_ani); - else + + if (state == AST_STATE_RING) + tmp->rings = 1; + tmp->tech_pvt = i; + if ((i->sig == SIG_FXOKS) || (i->sig == SIG_FXOGS) || (i->sig == SIG_FXOLS)) { + /* Only FXO signalled stuff can be picked up */ + tmp->callgroup = i->callgroup; + tmp->pickupgroup = i->pickupgroup; + } + if (!ast_strlen_zero(i->language)) + ast_string_field_set(tmp, language, i->language); + if (!i->owner) + i->owner = tmp; + if (!ast_strlen_zero(i->accountcode)) + ast_string_field_set(tmp, accountcode, i->accountcode); + if (i->amaflags) + tmp->amaflags = i->amaflags; + i->subs[index].owner = tmp; + ast_copy_string(tmp->context, i->context, sizeof(tmp->context)); + ast_string_field_set(tmp, call_forward, i->call_forward); + /* If we've been told "no ADSI" then enforce it */ + if (!i->adsi) + tmp->adsicpe = AST_ADSI_UNAVAILABLE; + if (!ast_strlen_zero(i->exten)) + ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten)); + if (!ast_strlen_zero(i->rdnis)) + tmp->cid.cid_rdnis = ast_strdup(i->rdnis); + if (!ast_strlen_zero(i->dnid)) + tmp->cid.cid_dnid = ast_strdup(i->dnid); + + /* Don't use ast_set_callerid() here because it will + * generate a needless NewCallerID event */ + #ifdef PRI_ANI + if (!ast_strlen_zero(i->cid_ani)) + tmp->cid.cid_ani = ast_strdup(i->cid_ani); + else + tmp->cid.cid_ani = ast_strdup(i->cid_num); + #else tmp->cid.cid_ani = ast_strdup(i->cid_num); -#else - tmp->cid.cid_ani = ast_strdup(i->cid_num); -#endif - tmp->cid.cid_pres = i->callingpres; - tmp->cid.cid_ton = i->cid_ton; -#ifdef HAVE_PRI - tmp->transfercapability = transfercapability; - pbx_builtin_setvar_helper(tmp, "TRANSFERCAPABILITY", ast_transfercapability2str(transfercapability)); - if (transfercapability & PRI_TRANS_CAP_DIGITAL) - i->digital = 1; - /* Assume calls are not idle calls unless we're told differently */ - i->isidlecall = 0; - i->alreadyhungup = 0; -#endif - /* clear the fake event in case we posted one before we had ast_channel */ - i->fake_event = 0; - /* Assure there is no confmute on this channel */ - dahdi_confmute(i, 0); - /* Configure the new channel jb */ - ast_jb_configure(tmp, &global_jbconf); - if (startpbx) { - if (ast_pbx_start(tmp)) { - ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name); - ast_hangup(tmp); - i->owner = NULL; - return NULL; + #endif + tmp->cid.cid_pres = i->callingpres; + tmp->cid.cid_ton = i->cid_ton; + #ifdef HAVE_PRI + tmp->transfercapability = transfercapability; + tmp->userinformationlayer1 = userinformationlayer1; + pbx_builtin_setvar_helper(tmp, "TRANSFERCAPABILITY", ast_transfercapability2str(transfercapability)); + if (transfercapability & PRI_TRANS_CAP_DIGITAL) + i->digital = 1; + /* Assume calls are not idle calls unless we're told differently */ + i->isidlecall = 0; + i->alreadyhungup = 0; + #endif + /* clear the fake event in case we posted one before we had ast_channel */ + i->fake_event = 0; + /* Assure there is no confmute on this channel */ + dahdi_confmute(i, 0); + /* Configure the new channel jb */ + ast_jb_configure(tmp, &global_jbconf); + if (startpbx) { + if (ast_pbx_start(tmp)) { + ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name); + ast_hangup(tmp); + i->owner = NULL; + return NULL; + } } + + ast_module_ref(ast_module_info->self); + + return tmp; } - - ast_module_ref(ast_module_info->self); - return tmp; -} - - -static int my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms) -{ - char c; - - *str = 0; /* start with empty output buffer */ - for (;;) + + static int my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms) { - /* Wait for the first digit (up to specified ms). */ - c = ast_waitfordigit(chan, ms); - /* if timeout, hangup or error, return as such */ - if (c < 1) - return c; - *str++ = c; - *str = 0; - if (strchr(term, c)) - return 1; + char c; + + *str = 0; /* start with empty output buffer */ + for (;;) + { + /* Wait for the first digit (up to specified ms). */ + c = ast_waitfordigit(chan, ms); + /* if timeout, hangup or error, return as such */ + if (c < 1) + return c; + *str++ = c; + *str = 0; + if (strchr(term, c)) + return 1; + } } -} - -static int dahdi_wink(struct dahdi_pvt *p, int index) -{ - int j; - dahdi_set_hook(p->subs[index].dfd, DAHDI_WINK); - for (;;) + + static int dahdi_wink(struct dahdi_pvt *p, int index) { - /* set bits of interest */ - j = DAHDI_IOMUX_SIGEVENT; - /* wait for some happening */ - if (ioctl(p->subs[index].dfd,DAHDI_IOMUX,&j) == -1) return(-1); - /* exit loop if we have it */ - if (j & DAHDI_IOMUX_SIGEVENT) break; - } - /* get the event info */ - if (ioctl(p->subs[index].dfd,DAHDI_GETEVENT,&j) == -1) return(-1); - return 0; -} - -static void *ss_thread(void *data) -{ - struct ast_channel *chan = data; - struct dahdi_pvt *p = chan->tech_pvt; - char exten[AST_MAX_EXTENSION] = ""; - char exten2[AST_MAX_EXTENSION] = ""; - unsigned char buf[256]; - char dtmfcid[300]; - char dtmfbuf[300]; - struct callerid_state *cs = NULL; - char *name = NULL, *number = NULL; - int distMatches; - int curRingData[3]; - int receivedRingT; - int counter1; - int counter; - int samples = 0; - struct ast_smdi_md_message *smdi_msg = NULL; - int flags; - int i; - int timeout; - int getforward = 0; - char *s1, *s2; - int len = 0; - int res; - int index; - - ast_mutex_lock(&ss_thread_lock); - ss_thread_count++; - ast_mutex_unlock(&ss_thread_lock); - /* in the bizarre case where the channel has become a zombie before we - even get started here, abort safely - */ - if (!p) { - ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name); - ast_hangup(chan); - goto quit; - } - if (option_verbose > 2) - ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s'\n", chan->name); - index = dahdi_get_index(chan, p, 1); - if (index < 0) { - ast_log(LOG_WARNING, "Huh?\n"); - ast_hangup(chan); - goto quit; - } - if (p->dsp) - ast_dsp_digitreset(p->dsp); - switch (p->sig) { -#ifdef HAVE_PRI - case SIG_PRI: - /* Now loop looking for an extension */ - ast_copy_string(exten, p->exten, sizeof(exten)); - len = strlen(exten); - res = 0; - while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) { - if (len && !ast_ignore_pattern(chan->context, exten)) - tone_zone_play_tone(p->subs[index].dfd, -1); - else - tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALTONE); - if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) - timeout = matchdigittimeout; - else - timeout = gendigittimeout; - res = ast_waitfordigit(chan, timeout); - if (res < 0) { - ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n"); - ast_hangup(chan); - goto quit; - } else if (res) { - exten[len++] = res; - exten[len] = '\0'; - } else - break; - } - /* if no extension was received ('unspecified') on overlap call, use the 's' extension */ - if (ast_strlen_zero(exten)) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of empty extension received on overlap call\n"); - exten[0] = 's'; - exten[1] = '\0'; + int j; + dahdi_set_hook(p->subs[index].dfd, DAHDI_WINK); + for (;;) + { + /* set bits of interest */ + j = DAHDI_IOMUX_SIGEVENT; + /* wait for some happening */ + if (ioctl(p->subs[index].dfd,DAHDI_IOMUX,&j) == -1) return(-1); + /* exit loop if we have it */ + if (j & DAHDI_IOMUX_SIGEVENT) break; } - tone_zone_play_tone(p->subs[index].dfd, -1); - if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) { - /* Start the real PBX */ - ast_copy_string(chan->exten, exten, sizeof(chan->exten)); - if (p->dsp) ast_dsp_digitreset(p->dsp); - dahdi_enable_ec(p); - ast_setstate(chan, AST_STATE_RING); - res = ast_pbx_run(chan); - if (res) { - ast_log(LOG_WARNING, "PBX exited non-zero!\n"); - } - } else { - ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context); - chan->hangupcause = AST_CAUSE_UNALLOCATED; + /* get the event info */ + if (ioctl(p->subs[index].dfd,DAHDI_GETEVENT,&j) == -1) return(-1); + return 0; + } + + static void *ss_thread(void *data) + { + struct ast_channel *chan = data; + struct dahdi_pvt *p = chan->tech_pvt; + char exten[AST_MAX_EXTENSION] = ""; + char exten2[AST_MAX_EXTENSION] = ""; + unsigned char buf[256]; + char dtmfcid[300]; + char dtmfbuf[300]; + struct callerid_state *cs = NULL; + char *name = NULL, *number = NULL; + int distMatches; + int curRingData[3]; + int receivedRingT; + int counter1; + int counter; + int samples = 0; + struct ast_smdi_md_message *smdi_msg = NULL; + int flags; + int i; + int timeout; + int getforward = 0; + char *s1, *s2; + int len = 0; + int res; + int index; + + ast_mutex_lock(&ss_thread_lock); + ss_thread_count++; + ast_mutex_unlock(&ss_thread_lock); + /* in the bizarre case where the channel has become a zombie before we + even get started here, abort safely + */ + if (!p) { + ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name); ast_hangup(chan); - p->exten[0] = '\0'; - /* Since we send release complete here, we won't get one */ - p->call = NULL; + goto quit; } - goto quit; - break; -#endif - case SIG_FEATD: - case SIG_FEATDMF: - case SIG_FEATDMF_TA: - case SIG_E911: - case SIG_FGC_CAMAMF: - case SIG_FEATB: - case SIG_EMWINK: - case SIG_SF_FEATD: - case SIG_SF_FEATDMF: - case SIG_SF_FEATB: - case SIG_SFWINK: - if (dahdi_wink(p, index)) + if (option_verbose > 2) + ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s'\n", chan->name); + index = dahdi_get_index(chan, p, 1); + if (index < 0) { + ast_log(LOG_WARNING, "Huh?\n"); + ast_hangup(chan); goto quit; - /* Fall through */ - case SIG_EM: - case SIG_EM_E1: - case SIG_SF: - case SIG_FGC_CAMA: - res = tone_zone_play_tone(p->subs[index].dfd, -1); + } if (p->dsp) ast_dsp_digitreset(p->dsp); - /* set digit mode appropriately */ - if (p->dsp) { - if (NEED_MFDETECT(p)) - ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax); - else - ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); - } - memset(dtmfbuf, 0, sizeof(dtmfbuf)); - /* Wait for the first digit only if immediate=no */ - if (!p->immediate) - /* Wait for the first digit (up to 5 seconds). */ - res = ast_waitfordigit(chan, 5000); - else + switch (p->sig) { + #ifdef HAVE_PRI + case SIG_PRI: + /* Now loop looking for an extension */ + ast_copy_string(exten, p->exten, sizeof(exten)); + len = strlen(exten); res = 0; - if (res > 0) { - /* save first char */ - dtmfbuf[0] = res; - switch (p->sig) { - case SIG_FEATD: - case SIG_SF_FEATD: - res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000); - if (res > 0) - res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000); - if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp); - break; - case SIG_FEATDMF_TA: - res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000); - if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp); - if (dahdi_wink(p, index)) goto quit; - dtmfbuf[0] = 0; + while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) { + if (len && !ast_ignore_pattern(chan->context, exten)) + tone_zone_play_tone(p->subs[index].dfd, -1); + else + tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALTONE); + if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) + timeout = matchdigittimeout; + else + timeout = gendigittimeout; + res = ast_waitfordigit(chan, timeout); + if (res < 0) { + ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n"); + ast_hangup(chan); + goto quit; + } else if (res) { + exten[len++] = res; + exten[len] = '\0'; + } else + break; + } + /* if no extension was received ('unspecified') on overlap call, use the 's' extension */ + if (ast_strlen_zero(exten)) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of empty extension received on overlap call\n"); + exten[0] = 's'; + exten[1] = '\0'; + } + tone_zone_play_tone(p->subs[index].dfd, -1); + if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) { + /* Start the real PBX */ + ast_copy_string(chan->exten, exten, sizeof(chan->exten)); + if (p->dsp) ast_dsp_digitreset(p->dsp); + dahdi_enable_ec(p); + ast_setstate(chan, AST_STATE_RING); + res = ast_pbx_run(chan); + if (res) { + ast_log(LOG_WARNING, "PBX exited non-zero!\n"); + } + } else { + ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context); + chan->hangupcause = AST_CAUSE_UNALLOCATED; + ast_hangup(chan); + p->exten[0] = '\0'; + /* Since we send release complete here, we won't get one */ + p->call = NULL; + } + goto quit; + break; + #endif + case SIG_FEATD: + case SIG_FEATDMF: + case SIG_FEATDMF_TA: + case SIG_E911: + case SIG_FGC_CAMAMF: + case SIG_FEATB: + case SIG_EMWINK: + case SIG_SF_FEATD: + case SIG_SF_FEATDMF: + case SIG_SF_FEATB: + case SIG_SFWINK: + if (dahdi_wink(p, index)) + goto quit; + /* Fall through */ + case SIG_EM: + case SIG_EM_E1: + case SIG_SF: + case SIG_FGC_CAMA: + res = tone_zone_play_tone(p->subs[index].dfd, -1); + if (p->dsp) + ast_dsp_digitreset(p->dsp); + /* set digit mode appropriately */ + if (p->dsp) { + if (NEED_MFDETECT(p)) + ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax); + else + ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); + } + memset(dtmfbuf, 0, sizeof(dtmfbuf)); + /* Wait for the first digit only if immediate=no */ + if (!p->immediate) /* Wait for the first digit (up to 5 seconds). */ res = ast_waitfordigit(chan, 5000); - if (res <= 0) break; + else + res = 0; + if (res > 0) { + /* save first char */ dtmfbuf[0] = res; - /* fall through intentionally */ - case SIG_FEATDMF: - case SIG_E911: - case SIG_FGC_CAMAMF: - case SIG_SF_FEATDMF: - res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000); - /* if international caca, do it again to get real ANO */ - if ((p->sig == SIG_FEATDMF) && (dtmfbuf[1] != '0') && (strlen(dtmfbuf) != 14)) - { + switch (p->sig) { + case SIG_FEATD: + case SIG_SF_FEATD: + res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000); + if (res > 0) + res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000); + if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp); + break; + case SIG_FEATDMF_TA: + res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000); + if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp); if (dahdi_wink(p, index)) goto quit; dtmfbuf[0] = 0; /* Wait for the first digit (up to 5 seconds). */ res = ast_waitfordigit(chan, 5000); if (res <= 0) break; dtmfbuf[0] = res; + /* fall through intentionally */ + case SIG_FEATDMF: + case SIG_E911: + case SIG_FGC_CAMAMF: + case SIG_SF_FEATDMF: res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000); - } - if (res > 0) { - /* if E911, take off hook */ - if (p->sig == SIG_E911) - dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK); - res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000); - } - if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp); - break; - case SIG_FEATB: - case SIG_SF_FEATB: - res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000); - if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp); - break; - case SIG_EMWINK: - /* if we received a '*', we are actually receiving Feature Group D - dial syntax, so use that mode; otherwise, fall through to normal - mode - */ - if (res == '*') { - res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000); - if (res > 0) - res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000); + /* if international caca, do it again to get real ANO */ + if ((p->sig == SIG_FEATDMF) && (dtmfbuf[1] != '0') && (strlen(dtmfbuf) != 14)) + { + if (dahdi_wink(p, index)) goto quit; + dtmfbuf[0] = 0; + /* Wait for the first digit (up to 5 seconds). */ + res = ast_waitfordigit(chan, 5000); + if (res <= 0) break; + dtmfbuf[0] = res; + res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000); + } + if (res > 0) { + /* if E911, take off hook */ + if (p->sig == SIG_E911) + dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK); + res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000); + } if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp); break; - } - default: - /* If we got the first digit, get the rest */ - len = 1; - dtmfbuf[len] = '\0'; - while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) { - if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) { - timeout = matchdigittimeout; - } else { - timeout = gendigittimeout; - } - res = ast_waitfordigit(chan, timeout); - if (res < 0) { - ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n"); - ast_hangup(chan); - goto quit; - } else if (res) { - dtmfbuf[len++] = res; - dtmfbuf[len] = '\0'; - } else { + case SIG_FEATB: + case SIG_SF_FEATB: + res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000); + if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp); + break; + case SIG_EMWINK: + /* if we received a '*', we are actually receiving Feature Group D + dial syntax, so use that mode; otherwise, fall through to normal + mode + */ + if (res == '*') { + res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000); + if (res > 0) + res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000); + if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp); break; } + default: + /* If we got the first digit, get the rest */ + len = 1; + dtmfbuf[len] = '\0'; + while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) { + if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) { + timeout = matchdigittimeout; + } else { + timeout = gendigittimeout; + } + res = ast_waitfordigit(chan, timeout); + if (res < 0) { + ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n"); + ast_hangup(chan); + goto quit; + } else if (res) { + dtmfbuf[len++] = res; + dtmfbuf[len] = '\0'; + } else { + break; + } + } + break; } - break; } - } - if (res == -1) { - ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno)); - ast_hangup(chan); - goto quit; - } else if (res < 0) { - ast_log(LOG_DEBUG, "Got hung up before digits finished\n"); - ast_hangup(chan); - goto quit; - } - - if (p->sig == SIG_FGC_CAMA) { - char anibuf[100]; - - if (ast_safe_sleep(chan,1000) == -1) { - ast_hangup(chan); - goto quit; - } - dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK); - ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax); - res = my_getsigstr(chan, anibuf, "#", 10000); - if ((res > 0) && (strlen(anibuf) > 2)) { - if (anibuf[strlen(anibuf) - 1] == '#') - anibuf[strlen(anibuf) - 1] = 0; - ast_set_callerid(chan, anibuf + 2, NULL, anibuf + 2); - } - ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); - } - - ast_copy_string(exten, dtmfbuf, sizeof(exten)); - if (ast_strlen_zero(exten)) - ast_copy_string(exten, "s", sizeof(exten)); - if (p->sig == SIG_FEATD || p->sig == SIG_EMWINK) { - /* Look for Feature Group D on all E&M Wink and Feature Group D trunks */ - if (exten[0] == '*') { - char *stringp=NULL; - ast_copy_string(exten2, exten, sizeof(exten2)); - /* Parse out extension and callerid */ - stringp=exten2 +1; - s1 = strsep(&stringp, "*"); - s2 = strsep(&stringp, "*"); - if (s2) { - if (!ast_strlen_zero(p->cid_num)) - ast_set_callerid(chan, p->cid_num, NULL, p->cid_num); - else - ast_set_callerid(chan, s1, NULL, s1); - ast_copy_string(exten, s2, sizeof(exten)); - } else - ast_copy_string(exten, s1, sizeof(exten)); - } else if (p->sig == SIG_FEATD) - ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel); - } - if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_FEATDMF_TA)) { - if (exten[0] == '*') { - char *stringp=NULL; - ast_copy_string(exten2, exten, sizeof(exten2)); - /* Parse out extension and callerid */ - stringp=exten2 +1; - s1 = strsep(&stringp, "#"); - s2 = strsep(&stringp, "#"); - if (s2) { - if (!ast_strlen_zero(p->cid_num)) - ast_set_callerid(chan, p->cid_num, NULL, p->cid_num); - else - if (*(s1 + 2)) - ast_set_callerid(chan, s1 + 2, NULL, s1 + 2); - ast_copy_string(exten, s2 + 1, sizeof(exten)); - } else - ast_copy_string(exten, s1 + 2, sizeof(exten)); - } else - ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel); - } - if ((p->sig == SIG_E911) || (p->sig == SIG_FGC_CAMAMF)) { - if (exten[0] == '*') { - char *stringp=NULL; - ast_copy_string(exten2, exten, sizeof(exten2)); - /* Parse out extension and callerid */ - stringp=exten2 +1; - s1 = strsep(&stringp, "#"); - s2 = strsep(&stringp, "#"); - if (s2 && (*(s2 + 1) == '0')) { - if (*(s2 + 2)) - ast_set_callerid(chan, s2 + 2, NULL, s2 + 2); + if (res == -1) { + ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno)); + ast_hangup(chan); + goto quit; + } else if (res < 0) { + ast_log(LOG_DEBUG, "Got hung up before digits finished\n"); + ast_hangup(chan); + goto quit; + } + + if (p->sig == SIG_FGC_CAMA) { + char anibuf[100]; + + if (ast_safe_sleep(chan,1000) == -1) { + ast_hangup(chan); + goto quit; } - if (s1) ast_copy_string(exten, s1, sizeof(exten)); - else ast_copy_string(exten, "911", sizeof(exten)); - } else - ast_log(LOG_WARNING, "Got a non-E911/FGC CAMA input on channel %d. Assuming E&M Wink instead\n", p->channel); - } - if (p->sig == SIG_FEATB) { - if (exten[0] == '*') { - char *stringp=NULL; - ast_copy_string(exten2, exten, sizeof(exten2)); - /* Parse out extension and callerid */ - stringp=exten2 +1; - s1 = strsep(&stringp, "#"); - ast_copy_string(exten, exten2 + 1, sizeof(exten)); - } else - ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d. Assuming E&M Wink instead\n", p->channel); - } - if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_FEATDMF_TA)) { - dahdi_wink(p, index); - /* some switches require a minimum guard time between - the last FGD wink and something that answers - immediately. This ensures it */ - if (ast_safe_sleep(chan,100)) goto quit; - } - dahdi_enable_ec(p); - if (NEED_MFDETECT(p)) { - if (p->dsp) { - if (!p->hardwaredtmf) - ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); - else { - ast_dsp_free(p->dsp); - p->dsp = NULL; + dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK); + ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax); + res = my_getsigstr(chan, anibuf, "#", 10000); + if ((res > 0) && (strlen(anibuf) > 2)) { + if (anibuf[strlen(anibuf) - 1] == '#') + anibuf[strlen(anibuf) - 1] = 0; + ast_set_callerid(chan, anibuf + 2, NULL, anibuf + 2); + } + ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); + } + + ast_copy_string(exten, dtmfbuf, sizeof(exten)); + if (ast_strlen_zero(exten)) + ast_copy_string(exten, "s", sizeof(exten)); + if (p->sig == SIG_FEATD || p->sig == SIG_EMWINK) { + /* Look for Feature Group D on all E&M Wink and Feature Group D trunks */ + if (exten[0] == '*') { + char *stringp=NULL; + ast_copy_string(exten2, exten, sizeof(exten2)); + /* Parse out extension and callerid */ + stringp=exten2 +1; + s1 = strsep(&stringp, "*"); + s2 = strsep(&stringp, "*"); + if (s2) { + if (!ast_strlen_zero(p->cid_num)) + ast_set_callerid(chan, p->cid_num, NULL, p->cid_num); + else + ast_set_callerid(chan, s1, NULL, s1); + ast_copy_string(exten, s2, sizeof(exten)); + } else + ast_copy_string(exten, s1, sizeof(exten)); + } else if (p->sig == SIG_FEATD) + ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel); + } + if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_FEATDMF_TA)) { + if (exten[0] == '*') { + char *stringp=NULL; + ast_copy_string(exten2, exten, sizeof(exten2)); + /* Parse out extension and callerid */ + stringp=exten2 +1; + s1 = strsep(&stringp, "#"); + s2 = strsep(&stringp, "#"); + if (s2) { + if (!ast_strlen_zero(p->cid_num)) + ast_set_callerid(chan, p->cid_num, NULL, p->cid_num); + else + if (*(s1 + 2)) + ast_set_callerid(chan, s1 + 2, NULL, s1 + 2); + ast_copy_string(exten, s2 + 1, sizeof(exten)); + } else + ast_copy_string(exten, s1 + 2, sizeof(exten)); + } else + ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel); + } + if ((p->sig == SIG_E911) || (p->sig == SIG_FGC_CAMAMF)) { + if (exten[0] == '*') { + char *stringp=NULL; + ast_copy_string(exten2, exten, sizeof(exten2)); + /* Parse out extension and callerid */ + stringp=exten2 +1; + s1 = strsep(&stringp, "#"); + s2 = strsep(&stringp, "#"); + if (s2 && (*(s2 + 1) == '0')) { + if (*(s2 + 2)) + ast_set_callerid(chan, s2 + 2, NULL, s2 + 2); + } + if (s1) ast_copy_string(exten, s1, sizeof(exten)); + else ast_copy_string(exten, "911", sizeof(exten)); + } else + ast_log(LOG_WARNING, "Got a non-E911/FGC CAMA input on channel %d. Assuming E&M Wink instead\n", p->channel); + } + if (p->sig == SIG_FEATB) { + if (exten[0] == '*') { + char *stringp=NULL; + ast_copy_string(exten2, exten, sizeof(exten2)); + /* Parse out extension and callerid */ + stringp=exten2 +1; + s1 = strsep(&stringp, "#"); + ast_copy_string(exten, exten2 + 1, sizeof(exten)); + } else + ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d. Assuming E&M Wink instead\n", p->channel); + } + if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_FEATDMF_TA)) { + dahdi_wink(p, index); + /* some switches require a minimum guard time between + the last FGD wink and something that answers + immediately. This ensures it */ + if (ast_safe_sleep(chan,100)) goto quit; + } + dahdi_enable_ec(p); + if (NEED_MFDETECT(p)) { + if (p->dsp) { + if (!p->hardwaredtmf) + ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); + else { + ast_dsp_free(p->dsp); + p->dsp = NULL; + } } } - } - - if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) { - ast_copy_string(chan->exten, exten, sizeof(chan->exten)); - if (p->dsp) ast_dsp_digitreset(p->dsp); - res = ast_pbx_run(chan); - if (res) { - ast_log(LOG_WARNING, "PBX exited non-zero\n"); + + if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) { + ast_copy_string(chan->exten, exten, sizeof(chan->exten)); + if (p->dsp) ast_dsp_digitreset(p->dsp); + res = ast_pbx_run(chan); + if (res) { + ast_log(LOG_WARNING, "PBX exited non-zero\n"); + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); + } + goto quit; + } else { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_2 "Unknown extension '%s' in context '%s' requested\n", exten, chan->context); + sleep(2); + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_INFO); + if (res < 0) + ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel); + else + sleep(1); + res = ast_streamfile(chan, "ss-noservice", chan->language); + if (res >= 0) + ast_waitstream(chan, ""); res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); - } - goto quit; - } else { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_2 "Unknown extension '%s' in context '%s' requested\n", exten, chan->context); - sleep(2); - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_INFO); - if (res < 0) - ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel); - else - sleep(1); - res = ast_streamfile(chan, "ss-noservice", chan->language); - if (res >= 0) - ast_waitstream(chan, ""); - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); - ast_hangup(chan); - goto quit; - } - break; - case SIG_FXOLS: - case SIG_FXOGS: - case SIG_FXOKS: - /* Read the first digit */ - timeout = firstdigittimeout; - /* If starting a threeway call, never timeout on the first digit so someone - can use flash-hook as a "hold" feature */ - if (p->subs[SUB_THREEWAY].owner) - timeout = 999999; - while (len < AST_MAX_EXTENSION-1) { - /* Read digit unless it's supposed to be immediate, in which case the - only answer is 's' */ - if (p->immediate) - res = 's'; - else - res = ast_waitfordigit(chan, timeout); - timeout = 0; - if (res < 0) { - ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n"); - res = tone_zone_play_tone(p->subs[index].dfd, -1); ast_hangup(chan); goto quit; - } else if (res) { - exten[len++]=res; - exten[len] = '\0'; } - if (!ast_ignore_pattern(chan->context, exten)) - tone_zone_play_tone(p->subs[index].dfd, -1); - else - tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALTONE); - if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) { - if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) { - if (getforward) { - /* Record this as the forwarding extension */ - ast_copy_string(p->call_forward, exten, sizeof(p->call_forward)); - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel); - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); - if (res) - break; - usleep(500000); - res = tone_zone_play_tone(p->subs[index].dfd, -1); - sleep(1); - memset(exten, 0, sizeof(exten)); - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALTONE); - len = 0; - getforward = 0; - } else { - res = tone_zone_play_tone(p->subs[index].dfd, -1); - ast_copy_string(chan->exten, exten, sizeof(chan->exten)); - if (!ast_strlen_zero(p->cid_num)) { - if (!p->hidecallerid) - ast_set_callerid(chan, p->cid_num, NULL, p->cid_num); - else - ast_set_callerid(chan, NULL, NULL, p->cid_num); + break; + case SIG_FXOLS: + case SIG_FXOGS: + case SIG_FXOKS: + /* Read the first digit */ + timeout = firstdigittimeout; + /* If starting a threeway call, never timeout on the first digit so someone + can use flash-hook as a "hold" feature */ + if (p->subs[SUB_THREEWAY].owner) + timeout = 999999; + while (len < AST_MAX_EXTENSION-1) { + /* Read digit unless it's supposed to be immediate, in which case the + only answer is 's' */ + if (p->immediate) + res = 's'; + else + res = ast_waitfordigit(chan, timeout); + timeout = 0; + if (res < 0) { + ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n"); + res = tone_zone_play_tone(p->subs[index].dfd, -1); + ast_hangup(chan); + goto quit; + } else if (res) { + exten[len++]=res; + exten[len] = '\0'; + } + if (!ast_ignore_pattern(chan->context, exten)) + tone_zone_play_tone(p->subs[index].dfd, -1); + else + tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALTONE); + if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) { + if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) { + if (getforward) { + /* Record this as the forwarding extension */ + ast_copy_string(p->call_forward, exten, sizeof(p->call_forward)); + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel); + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); + if (res) + break; + usleep(500000); + res = tone_zone_play_tone(p->subs[index].dfd, -1); + sleep(1); + memset(exten, 0, sizeof(exten)); + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALTONE); + len = 0; + getforward = 0; + } else { + res = tone_zone_play_tone(p->subs[index].dfd, -1); + ast_copy_string(chan->exten, exten, sizeof(chan->exten)); + if (!ast_strlen_zero(p->cid_num)) { + if (!p->hidecallerid) + ast_set_callerid(chan, p->cid_num, NULL, p->cid_num); + else + ast_set_callerid(chan, NULL, NULL, p->cid_num); + } + if (!ast_strlen_zero(p->cid_name)) { + if (!p->hidecallerid) + ast_set_callerid(chan, NULL, p->cid_name, NULL); + } + ast_setstate(chan, AST_STATE_RING); + dahdi_enable_ec(p); + res = ast_pbx_run(chan); + if (res) { + ast_log(LOG_WARNING, "PBX exited non-zero\n"); + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); + } + goto quit; } - if (!ast_strlen_zero(p->cid_name)) { - if (!p->hidecallerid) - ast_set_callerid(chan, NULL, p->cid_name, NULL); + } else { + /* It's a match, but they just typed a digit, and there is an ambiguous match, + so just set the timeout to matchdigittimeout and wait some more */ + timeout = matchdigittimeout; + } + } else if (res == 0) { + ast_log(LOG_DEBUG, "not enough digits (and no ambiguous match)...\n"); + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); + dahdi_wait_event(p->subs[index].dfd); + ast_hangup(chan); + goto quit; + } else if (p->callwaiting && !strcmp(exten, "*70")) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name); + /* Disable call waiting if enabled */ + p->callwaiting = 0; + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); + if (res) { + ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n", + chan->name, strerror(errno)); + } + len = 0; + ioctl(p->subs[index].dfd,DAHDI_CONFDIAG,&len); + memset(exten, 0, sizeof(exten)); + timeout = firstdigittimeout; + + } else if (!strcmp(exten,ast_pickup_ext())) { + /* Scan all channels and see if there are any + * ringing channels that have call groups + * that equal this channels pickup group + */ + if (index == SUB_REAL) { + /* Switch us from Third call to Call Wait */ + if (p->subs[SUB_THREEWAY].owner) { + /* If you make a threeway call and the *8# a call, it should actually + look like a callwait */ + alloc_sub(p, SUB_CALLWAIT); + swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY); + unalloc_sub(p, SUB_THREEWAY); } - ast_setstate(chan, AST_STATE_RING); dahdi_enable_ec(p); - res = ast_pbx_run(chan); - if (res) { - ast_log(LOG_WARNING, "PBX exited non-zero\n"); + if (ast_pickup_call(chan)) { + ast_log(LOG_DEBUG, "No call pickup possible...\n"); res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); + dahdi_wait_event(p->subs[index].dfd); } + ast_hangup(chan); + goto quit; + } else { + ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n"); + ast_hangup(chan); goto quit; } - } else { - /* It's a match, but they just typed a digit, and there is an ambiguous match, - so just set the timeout to matchdigittimeout and wait some more */ - timeout = matchdigittimeout; - } - } else if (res == 0) { - ast_log(LOG_DEBUG, "not enough digits (and no ambiguous match)...\n"); - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); - dahdi_wait_event(p->subs[index].dfd); - ast_hangup(chan); - goto quit; - } else if (p->callwaiting && !strcmp(exten, "*70")) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name); - /* Disable call waiting if enabled */ - p->callwaiting = 0; - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); - if (res) { - ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n", - chan->name, strerror(errno)); - } - len = 0; - ioctl(p->subs[index].dfd,DAHDI_CONFDIAG,&len); - memset(exten, 0, sizeof(exten)); - timeout = firstdigittimeout; - } else if (!strcmp(exten,ast_pickup_ext())) { - /* Scan all channels and see if there are any - * ringing channels that have call groups - * that equal this channels pickup group - */ - if (index == SUB_REAL) { - /* Switch us from Third call to Call Wait */ - if (p->subs[SUB_THREEWAY].owner) { - /* If you make a threeway call and the *8# a call, it should actually - look like a callwait */ - alloc_sub(p, SUB_CALLWAIT); - swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY); - unalloc_sub(p, SUB_THREEWAY); + } else if (!p->hidecallerid && !strcmp(exten, "*67")) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name); + /* Disable Caller*ID if enabled */ + p->hidecallerid = 1; + if (chan->cid.cid_num) + free(chan->cid.cid_num); + chan->cid.cid_num = NULL; + if (chan->cid.cid_name) + free(chan->cid.cid_name); + chan->cid.cid_name = NULL; + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); + if (res) { + ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n", + chan->name, strerror(errno)); } - dahdi_enable_ec(p); - if (ast_pickup_call(chan)) { - ast_log(LOG_DEBUG, "No call pickup possible...\n"); - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); - dahdi_wait_event(p->subs[index].dfd); + len = 0; + memset(exten, 0, sizeof(exten)); + timeout = firstdigittimeout; + } else if (p->callreturn && !strcmp(exten, "*69")) { + res = 0; + if (!ast_strlen_zero(p->lastcid_num)) { + res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language); } - ast_hangup(chan); - goto quit; - } else { - ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n"); - ast_hangup(chan); - goto quit; - } - - } else if (!p->hidecallerid && !strcmp(exten, "*67")) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name); - /* Disable Caller*ID if enabled */ - p->hidecallerid = 1; - if (chan->cid.cid_num) - free(chan->cid.cid_num); - chan->cid.cid_num = NULL; - if (chan->cid.cid_name) - free(chan->cid.cid_name); - chan->cid.cid_name = NULL; - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); - if (res) { - ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n", - chan->name, strerror(errno)); - } - len = 0; - memset(exten, 0, sizeof(exten)); - timeout = firstdigittimeout; - } else if (p->callreturn && !strcmp(exten, "*69")) { - res = 0; - if (!ast_strlen_zero(p->lastcid_num)) { - res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language); - } - if (!res) + if (!res) + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); + break; + } else if (!strcmp(exten, "*78")) { + /* Do not disturb */ + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %d\n", p->channel); + manager_event(EVENT_FLAG_SYSTEM, "DNDState", + "Channel: %s/%d\r\n" + "Status: enabled\r\n", dahdi_chan_name, p->channel); res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); - break; - } else if (!strcmp(exten, "*78")) { - /* Do not disturb */ - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %d\n", p->channel); - manager_event(EVENT_FLAG_SYSTEM, "DNDState", - "Channel: %s/%d\r\n" - "Status: enabled\r\n", dahdi_chan_name, p->channel); - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); - p->dnd = 1; - getforward = 0; - memset(exten, 0, sizeof(exten)); - len = 0; - } else if (!strcmp(exten, "*79")) { - /* Do not disturb */ - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %d\n", p->channel); - manager_event(EVENT_FLAG_SYSTEM, "DNDState", - "Channel: %s/%d\r\n" - "Status: disabled\r\n", dahdi_chan_name, p->channel); - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); - p->dnd = 0; - getforward = 0; - memset(exten, 0, sizeof(exten)); - len = 0; - } else if (p->cancallforward && !strcmp(exten, "*72")) { - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); - getforward = 1; - memset(exten, 0, sizeof(exten)); - len = 0; - } else if (p->cancallforward && !strcmp(exten, "*73")) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %d\n", p->channel); - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); - memset(p->call_forward, 0, sizeof(p->call_forward)); - getforward = 0; - memset(exten, 0, sizeof(exten)); - len = 0; - } else if ((p->transfer || p->canpark) && !strcmp(exten, ast_parking_ext()) && - p->subs[SUB_THREEWAY].owner && - ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) { - /* This is a three way call, the main call being a real channel, - and we're parking the first call. */ - ast_masq_park_call(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), chan, 0, NULL); - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name); - break; - } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", p->lastcid_num); - res = ast_db_put("blacklist", p->lastcid_num, "1"); - if (!res) { + p->dnd = 1; + getforward = 0; + memset(exten, 0, sizeof(exten)); + len = 0; + } else if (!strcmp(exten, "*79")) { + /* Do not disturb */ + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %d\n", p->channel); + manager_event(EVENT_FLAG_SYSTEM, "DNDState", + "Channel: %s/%d\r\n" + "Status: disabled\r\n", dahdi_chan_name, p->channel); res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); + p->dnd = 0; + getforward = 0; memset(exten, 0, sizeof(exten)); len = 0; + } else if (p->cancallforward && !strcmp(exten, "*72")) { + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); + getforward = 1; + memset(exten, 0, sizeof(exten)); + len = 0; + } else if (p->cancallforward && !strcmp(exten, "*73")) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %d\n", p->channel); + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); + memset(p->call_forward, 0, sizeof(p->call_forward)); + getforward = 0; + memset(exten, 0, sizeof(exten)); + len = 0; + } else if ((p->transfer || p->canpark) && !strcmp(exten, ast_parking_ext()) && + p->subs[SUB_THREEWAY].owner && + ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) { + /* This is a three way call, the main call being a real channel, + and we're parking the first call. */ + ast_masq_park_call(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), chan, 0, NULL); + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name); + break; + } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", p->lastcid_num); + res = ast_db_put("blacklist", p->lastcid_num, "1"); + if (!res) { + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); + memset(exten, 0, sizeof(exten)); + len = 0; + } + } else if (p->hidecallerid && !strcmp(exten, "*82")) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name); + /* Enable Caller*ID if enabled */ + p->hidecallerid = 0; + if (chan->cid.cid_num) + free(chan->cid.cid_num); + chan->cid.cid_num = NULL; + if (chan->cid.cid_name) + free(chan->cid.cid_name); + chan->cid.cid_name = NULL; + ast_set_callerid(chan, p->cid_num, p->cid_name, NULL); + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); + if (res) { + ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n", + chan->name, strerror(errno)); + } + len = 0; + memset(exten, 0, sizeof(exten)); + timeout = firstdigittimeout; + } else if (!strcmp(exten, "*0")) { + struct ast_channel *nbridge = + p->subs[SUB_THREEWAY].owner; + struct dahdi_pvt *pbridge = NULL; + /* set up the private struct of the bridged one, if any */ + if (nbridge && ast_bridged_channel(nbridge)) + pbridge = ast_bridged_channel(nbridge)->tech_pvt; + if (nbridge && pbridge && + (nbridge->tech == chan_tech) && + (ast_bridged_channel(nbridge)->tech == chan_tech) && + ISTRUNK(pbridge)) { + int func = DAHDI_FLASH; + /* Clear out the dial buffer */ + p->dop.dialstr[0] = '\0'; + /* flash hookswitch */ + if ((ioctl(pbridge->subs[SUB_REAL].dfd,DAHDI_HOOK,&func) == -1) && (errno != EINPROGRESS)) { + ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n", + nbridge->name, strerror(errno)); + } + swap_subs(p, SUB_REAL, SUB_THREEWAY); + unalloc_sub(p, SUB_THREEWAY); + p->owner = p->subs[SUB_REAL].owner; + if (ast_bridged_channel(p->subs[SUB_REAL].owner)) + ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD); + ast_hangup(chan); + goto quit; + } else { + tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); + dahdi_wait_event(p->subs[index].dfd); + tone_zone_play_tone(p->subs[index].dfd, -1); + swap_subs(p, SUB_REAL, SUB_THREEWAY); + unalloc_sub(p, SUB_THREEWAY); + p->owner = p->subs[SUB_REAL].owner; + ast_hangup(chan); + goto quit; + } + } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) && + ((exten[0] != '*') || (strlen(exten) > 2))) { + if (option_debug) + ast_log(LOG_DEBUG, "Can't match %s from '%s' in context %s\n", exten, chan->cid.cid_num ? chan->cid.cid_num : "", chan->context); + break; } - } else if (p->hidecallerid && !strcmp(exten, "*82")) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name); - /* Enable Caller*ID if enabled */ - p->hidecallerid = 0; - if (chan->cid.cid_num) - free(chan->cid.cid_num); - chan->cid.cid_num = NULL; - if (chan->cid.cid_name) - free(chan->cid.cid_name); - chan->cid.cid_name = NULL; - ast_set_callerid(chan, p->cid_num, p->cid_name, NULL); - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL); - if (res) { - ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n", - chan->name, strerror(errno)); + if (!timeout) + timeout = gendigittimeout; + if (len && !ast_ignore_pattern(chan->context, exten)) + tone_zone_play_tone(p->subs[index].dfd, -1); + } + break; + case SIG_FXSLS: + case SIG_FXSGS: + case SIG_FXSKS: + #ifdef HAVE_PRI + if (p->pri) { + /* This is a GR-303 trunk actually. Wait for the first ring... */ + struct ast_frame *f; + int res; + time_t start; + + time(&start); + ast_setstate(chan, AST_STATE_RING); + while (time(NULL) < start + 3) { + res = ast_waitfor(chan, 1000); + if (res) { + f = ast_read(chan); + if (!f) { + ast_log(LOG_WARNING, "Whoa, hangup while waiting for first ring!\n"); + ast_hangup(chan); + goto quit; + } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) { + res = 1; + } else + res = 0; + ast_frfree(f); + if (res) { + ast_log(LOG_DEBUG, "Got ring!\n"); + res = 0; + break; + } + } } - len = 0; - memset(exten, 0, sizeof(exten)); - timeout = firstdigittimeout; - } else if (!strcmp(exten, "*0")) { - struct ast_channel *nbridge = - p->subs[SUB_THREEWAY].owner; - struct dahdi_pvt *pbridge = NULL; - /* set up the private struct of the bridged one, if any */ - if (nbridge && ast_bridged_channel(nbridge)) - pbridge = ast_bridged_channel(nbridge)->tech_pvt; - if (nbridge && pbridge && - (nbridge->tech == chan_tech) && - (ast_bridged_channel(nbridge)->tech == chan_tech) && - ISTRUNK(pbridge)) { - int func = DAHDI_FLASH; - /* Clear out the dial buffer */ - p->dop.dialstr[0] = '\0'; - /* flash hookswitch */ - if ((ioctl(pbridge->subs[SUB_REAL].dfd,DAHDI_HOOK,&func) == -1) && (errno != EINPROGRESS)) { - ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n", - nbridge->name, strerror(errno)); + } + #endif + /* check for SMDI messages */ + if (p->use_smdi && p->smdi_iface) { + smdi_msg = ast_smdi_md_message_wait(p->smdi_iface, SMDI_MD_WAIT_TIMEOUT); + + if (smdi_msg != NULL) { + ast_copy_string(chan->exten, smdi_msg->fwd_st, sizeof(chan->exten)); + + if (smdi_msg->type == 'B') + pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "b"); + else if (smdi_msg->type == 'N') + pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "u"); + + ast_log(LOG_DEBUG, "Recieved SMDI message on %s\n", chan->name); + } else { + ast_log(LOG_WARNING, "SMDI enabled but no SMDI message present\n"); + } + } + + if (p->use_callerid && (p->cid_signalling == CID_SIG_SMDI && smdi_msg)) { + number = smdi_msg->calling_st; + + /* If we want caller id, we're in a prering state due to a polarity reversal + * and we're set to use a polarity reversal to trigger the start of caller id, + * grab the caller id and wait for ringing to start... */ + } else if (p->use_callerid && (chan->_state == AST_STATE_PRERING && p->cid_start == CID_START_POLARITY)) { + /* If set to use DTMF CID signalling, listen for DTMF */ + if (p->cid_signalling == CID_SIG_DTMF) { + int i = 0; + cs = NULL; + ast_log(LOG_DEBUG, "Receiving DTMF cid on " + "channel %s\n", chan->name); + dahdi_setlinear(p->subs[index].dfd, 0); + res = 2000; + for (;;) { + struct ast_frame *f; + res = ast_waitfor(chan, res); + if (res <= 0) { + ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. " + "Exiting simple switch\n"); + ast_hangup(chan); + goto quit; + } + f = ast_read(chan); + if (!f) + break; + if (f->frametype == AST_FRAME_DTMF) { + dtmfbuf[i++] = f->subclass; + ast_log(LOG_DEBUG, "CID got digit '%c'\n", f->subclass); + res = 2000; + } + ast_frfree(f); + if (chan->_state == AST_STATE_RING || + chan->_state == AST_STATE_RINGING) + break; /* Got ring */ } - swap_subs(p, SUB_REAL, SUB_THREEWAY); - unalloc_sub(p, SUB_THREEWAY); - p->owner = p->subs[SUB_REAL].owner; - if (ast_bridged_channel(p->subs[SUB_REAL].owner)) - ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD); - ast_hangup(chan); - goto quit; + dtmfbuf[i] = '\0'; + dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear); + /* Got cid and ring. */ + ast_log(LOG_DEBUG, "CID got string '%s'\n", dtmfbuf); + callerid_get_dtmf(dtmfbuf, dtmfcid, &flags); + ast_log(LOG_DEBUG, "CID is '%s', flags %d\n", + dtmfcid, flags); + /* If first byte is NULL, we have no cid */ + if (!ast_strlen_zero(dtmfcid)) + number = dtmfcid; + else + number = NULL; + /* If set to use V23 Signalling, launch our FSK gubbins and listen for it */ + } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) { + cs = callerid_new(p->cid_signalling); + if (cs) { + samples = 0; + #if 1 + bump_gains(p); + #endif + /* Take out of linear mode for Caller*ID processing */ + dahdi_setlinear(p->subs[index].dfd, 0); + + /* First we wait and listen for the Caller*ID */ + for (;;) { + i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT; + if ((res = ioctl(p->subs[index].dfd, DAHDI_IOMUX, &i))) { + ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno)); + callerid_free(cs); + ast_hangup(chan); + goto quit; + } + if (i & DAHDI_IOMUX_SIGEVENT) { + res = dahdi_get_event(p->subs[index].dfd); + ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res)); + + if (p->cid_signalling == CID_SIG_V23_JP) { + #ifdef DAHDI_EVENT_RINGBEGIN + if (res == DAHDI_EVENT_RINGBEGIN) { + res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK); + usleep(1); + } + #endif + } else { + res = 0; + break; + } + } else if (i & DAHDI_IOMUX_READ) { + res = read(p->subs[index].dfd, buf, sizeof(buf)); + if (res < 0) { + if (errno != ELAST) { + ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno)); + callerid_free(cs); + ast_hangup(chan); + goto quit; + } + break; + } + samples += res; + + if (p->cid_signalling == CID_SIG_V23_JP) { + res = callerid_feed_jp(cs, buf, res, AST_LAW(p)); + } else { + res = callerid_feed(cs, buf, res, AST_LAW(p)); + } + + if (res < 0) { + ast_log(LOG_WARNING, "CallerID feed failed on channel '%s'\n", chan->name); + break; + } else if (res) + break; + else if (samples > (8000 * 10)) + break; + } + } + if (res == 1) { + callerid_get(cs, &name, &number, &flags); + ast_log(LOG_NOTICE, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags); + } + + if (p->cid_signalling == CID_SIG_V23_JP) { + res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_ONHOOK); + usleep(1); + res = 4000; + } else { + + /* Finished with Caller*ID, now wait for a ring to make sure there really is a call coming */ + res = 2000; + } + + for (;;) { + struct ast_frame *f; + res = ast_waitfor(chan, res); + if (res <= 0) { + ast_log(LOG_WARNING, "CID timed out waiting for ring. " + "Exiting simple switch\n"); + ast_hangup(chan); + goto quit; + } + if (!(f = ast_read(chan))) { + ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n"); + ast_hangup(chan); + goto quit; + } + ast_frfree(f); + if (chan->_state == AST_STATE_RING || + chan->_state == AST_STATE_RINGING) + break; /* Got ring */ + } + + /* We must have a ring by now, so, if configured, lets try to listen for + * distinctive ringing */ + if (p->usedistinctiveringdetection == 1) { + len = 0; + distMatches = 0; + /* Clear the current ring data array so we dont have old data in it. */ + for (receivedRingT = 0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++) + curRingData[receivedRingT] = 0; + receivedRingT = 0; + counter = 0; + counter1 = 0; + /* Check to see if context is what it should be, if not set to be. */ + if (strcmp(p->context,p->defcontext) != 0) { + ast_copy_string(p->context, p->defcontext, sizeof(p->context)); + ast_copy_string(chan->context,p->defcontext,sizeof(chan->context)); + } + + for (;;) { + i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT; + if ((res = ioctl(p->subs[index].dfd, DAHDI_IOMUX, &i))) { + ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno)); + callerid_free(cs); + ast_hangup(chan); + goto quit; + } + if (i & DAHDI_IOMUX_SIGEVENT) { + res = dahdi_get_event(p->subs[index].dfd); + ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res)); + res = 0; + /* Let us detect distinctive ring */ + + curRingData[receivedRingT] = p->ringt; + + if (p->ringt < p->ringt_base/2) + break; + /* Increment the ringT counter so we can match it against + values in chan_dahdi.conf for distinctive ring */ + if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0]))) + break; + } else if (i & DAHDI_IOMUX_READ) { + res = read(p->subs[index].dfd, buf, sizeof(buf)); + if (res < 0) { + if (errno != ELAST) { + ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno)); + callerid_free(cs); + ast_hangup(chan); + goto quit; + } + break; + } + if (p->ringt) + p->ringt--; + if (p->ringt == 1) { + res = -1; + break; + } + } + } + if (option_verbose > 2) + /* this only shows up if you have n of the dring patterns filled in */ + ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]); + + for (counter = 0; counter < 3; counter++) { + /* Check to see if the rings we received match any of the ones in chan_dahdi.conf for this + channel */ + distMatches = 0; + for (counter1 = 0; counter1 < 3; counter1++) { + if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >= + (p->drings.ringnum[counter].ring[counter1]-10)) { + distMatches++; + } + } + if (distMatches == 3) { + /* The ring matches, set the context to whatever is for distinctive ring.. */ + ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context)); + ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context)); + if (option_verbose > 2) + ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context); + break; + } + } + } + /* Restore linear mode (if appropriate) for Caller*ID processing */ + dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear); + #if 1 + restore_gains(p); + #endif + } else + ast_log(LOG_WARNING, "Unable to get caller ID space\n"); } else { - tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); - dahdi_wait_event(p->subs[index].dfd); - tone_zone_play_tone(p->subs[index].dfd, -1); - swap_subs(p, SUB_REAL, SUB_THREEWAY); - unalloc_sub(p, SUB_THREEWAY); - p->owner = p->subs[SUB_REAL].owner; + ast_log(LOG_WARNING, "Channel %s in prering " + "state, but I have nothing to do. " + "Terminating simple switch, should be " + "restarted by the actual ring.\n", + chan->name); ast_hangup(chan); goto quit; - } - } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) && - ((exten[0] != '*') || (strlen(exten) > 2))) { - if (option_debug) - ast_log(LOG_DEBUG, "Can't match %s from '%s' in context %s\n", exten, chan->cid.cid_num ? chan->cid.cid_num : "", chan->context); - break; - } - if (!timeout) - timeout = gendigittimeout; - if (len && !ast_ignore_pattern(chan->context, exten)) - tone_zone_play_tone(p->subs[index].dfd, -1); - } - break; - case SIG_FXSLS: - case SIG_FXSGS: - case SIG_FXSKS: -#ifdef HAVE_PRI - if (p->pri) { - /* This is a GR-303 trunk actually. Wait for the first ring... */ - struct ast_frame *f; - int res; - time_t start; - - time(&start); - ast_setstate(chan, AST_STATE_RING); - while (time(NULL) < start + 3) { - res = ast_waitfor(chan, 1000); - if (res) { - f = ast_read(chan); - if (!f) { - ast_log(LOG_WARNING, "Whoa, hangup while waiting for first ring!\n"); - ast_hangup(chan); - goto quit; - } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) { - res = 1; - } else - res = 0; - ast_frfree(f); - if (res) { - ast_log(LOG_DEBUG, "Got ring!\n"); - res = 0; - break; - } } - } - } -#endif - /* check for SMDI messages */ - if (p->use_smdi && p->smdi_iface) { - smdi_msg = ast_smdi_md_message_wait(p->smdi_iface, SMDI_MD_WAIT_TIMEOUT); - - if (smdi_msg != NULL) { - ast_copy_string(chan->exten, smdi_msg->fwd_st, sizeof(chan->exten)); - - if (smdi_msg->type == 'B') - pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "b"); - else if (smdi_msg->type == 'N') - pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "u"); - - ast_log(LOG_DEBUG, "Recieved SMDI message on %s\n", chan->name); - } else { - ast_log(LOG_WARNING, "SMDI enabled but no SMDI message present\n"); - } - } - - if (p->use_callerid && (p->cid_signalling == CID_SIG_SMDI && smdi_msg)) { - number = smdi_msg->calling_st; - - /* If we want caller id, we're in a prering state due to a polarity reversal - * and we're set to use a polarity reversal to trigger the start of caller id, - * grab the caller id and wait for ringing to start... */ - } else if (p->use_callerid && (chan->_state == AST_STATE_PRERING && p->cid_start == CID_START_POLARITY)) { - /* If set to use DTMF CID signalling, listen for DTMF */ - if (p->cid_signalling == CID_SIG_DTMF) { - int i = 0; - cs = NULL; - ast_log(LOG_DEBUG, "Receiving DTMF cid on " - "channel %s\n", chan->name); - dahdi_setlinear(p->subs[index].dfd, 0); - res = 2000; - for (;;) { - struct ast_frame *f; - res = ast_waitfor(chan, res); - if (res <= 0) { - ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. " - "Exiting simple switch\n"); - ast_hangup(chan); - goto quit; - } - f = ast_read(chan); - if (!f) - break; - if (f->frametype == AST_FRAME_DTMF) { - dtmfbuf[i++] = f->subclass; - ast_log(LOG_DEBUG, "CID got digit '%c'\n", f->subclass); - res = 2000; - } - ast_frfree(f); - if (chan->_state == AST_STATE_RING || - chan->_state == AST_STATE_RINGING) - break; /* Got ring */ - } - dtmfbuf[i] = '\0'; - dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear); - /* Got cid and ring. */ - ast_log(LOG_DEBUG, "CID got string '%s'\n", dtmfbuf); - callerid_get_dtmf(dtmfbuf, dtmfcid, &flags); - ast_log(LOG_DEBUG, "CID is '%s', flags %d\n", - dtmfcid, flags); - /* If first byte is NULL, we have no cid */ - if (!ast_strlen_zero(dtmfcid)) - number = dtmfcid; - else - number = NULL; - /* If set to use V23 Signalling, launch our FSK gubbins and listen for it */ - } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) { + } else if (p->use_callerid && p->cid_start == CID_START_RING) { + /* FSK Bell202 callerID */ cs = callerid_new(p->cid_signalling); if (cs) { - samples = 0; -#if 1 + #if 1 bump_gains(p); -#endif + #endif + samples = 0; + len = 0; + distMatches = 0; + /* Clear the current ring data array so we dont have old data in it. */ + for (receivedRingT = 0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++) + curRingData[receivedRingT] = 0; + receivedRingT = 0; + counter = 0; + counter1 = 0; + /* Check to see if context is what it should be, if not set to be. */ + if (strcmp(p->context,p->defcontext) != 0) { + ast_copy_string(p->context, p->defcontext, sizeof(p->context)); + ast_copy_string(chan->context,p->defcontext,sizeof(chan->context)); + } + /* Take out of linear mode for Caller*ID processing */ dahdi_setlinear(p->subs[index].dfd, 0); - - /* First we wait and listen for the Caller*ID */ for (;;) { i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT; if ((res = ioctl(p->subs[index].dfd, DAHDI_IOMUX, &i))) { @@ -6312,18 +6548,25 @@ if (i & DAHDI_IOMUX_SIGEVENT) { res = dahdi_get_event(p->subs[index].dfd); ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res)); - - if (p->cid_signalling == CID_SIG_V23_JP) { -#ifdef DAHDI_EVENT_RINGBEGIN - if (res == DAHDI_EVENT_RINGBEGIN) { - res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK); - usleep(1); - } -#endif - } else { - res = 0; - break; + /* If we get a PR event, they hung up while processing calerid */ + if ( res == DAHDI_EVENT_POLARITY && p->hanguponpolarityswitch && p->polarity == POLARITY_REV) { + ast_log(LOG_DEBUG, "Hanging up due to polarity reversal on channel %d while detecting callerid\n", p->channel); + p->polarity = POLARITY_IDLE; + callerid_free(cs); + ast_hangup(chan); + goto quit; } + res = 0; + /* Let us detect callerid when the telco uses distinctive ring */ + + curRingData[receivedRingT] = p->ringt; + + if (p->ringt < p->ringt_base/2) + break; + /* Increment the ringT counter so we can match it against + values in chan_dahdi.conf for distinctive ring */ + if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0]))) + break; } else if (i & DAHDI_IOMUX_READ) { res = read(p->subs[index].dfd, buf, sizeof(buf)); if (res < 0) { @@ -6335,16 +6578,16 @@ } break; } - samples += res; - - if (p->cid_signalling == CID_SIG_V23_JP) { - res = callerid_feed_jp(cs, buf, res, AST_LAW(p)); - } else { - res = callerid_feed(cs, buf, res, AST_LAW(p)); + if (p->ringt) + p->ringt--; + if (p->ringt == 1) { + res = -1; + break; } - + samples += res; + res = callerid_feed(cs, buf, res, AST_LAW(p)); if (res < 0) { - ast_log(LOG_WARNING, "CallerID feed failed on channel '%s'\n", chan->name); + ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno)); break; } else if (res) break; @@ -6354,59 +6597,20 @@ } if (res == 1) { callerid_get(cs, &name, &number, &flags); - ast_log(LOG_NOTICE, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags); - } - - if (p->cid_signalling == CID_SIG_V23_JP) { - res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_ONHOOK); - usleep(1); - res = 4000; - } else { - - /* Finished with Caller*ID, now wait for a ring to make sure there really is a call coming */ - res = 2000; - } - - for (;;) { - struct ast_frame *f; - res = ast_waitfor(chan, res); - if (res <= 0) { - ast_log(LOG_WARNING, "CID timed out waiting for ring. " - "Exiting simple switch\n"); - ast_hangup(chan); - goto quit; - } - if (!(f = ast_read(chan))) { - ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n"); - ast_hangup(chan); - goto quit; - } - ast_frfree(f); - if (chan->_state == AST_STATE_RING || - chan->_state == AST_STATE_RINGING) - break; /* Got ring */ + if (option_debug) + ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags); } - - /* We must have a ring by now, so, if configured, lets try to listen for - * distinctive ringing */ - if (p->usedistinctiveringdetection == 1) { - len = 0; - distMatches = 0; + if (distinctiveringaftercid == 1) { /* Clear the current ring data array so we dont have old data in it. */ - for (receivedRingT = 0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++) + for (receivedRingT = 0; receivedRingT < 3; receivedRingT++) { curRingData[receivedRingT] = 0; - receivedRingT = 0; - counter = 0; - counter1 = 0; - /* Check to see if context is what it should be, if not set to be. */ - if (strcmp(p->context,p->defcontext) != 0) { - ast_copy_string(p->context, p->defcontext, sizeof(p->context)); - ast_copy_string(chan->context,p->defcontext,sizeof(chan->context)); } - - for (;;) { + receivedRingT = 0; + if (option_verbose > 2) + ast_verbose( VERBOSE_PREFIX_3 "Detecting post-CID distinctive ring\n"); + for (;;) { i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT; - if ((res = ioctl(p->subs[index].dfd, DAHDI_IOMUX, &i))) { + if ((res = ioctl(p->subs[index].dfd, DAHDI_IOMUX, &i))) { ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno)); callerid_free(cs); ast_hangup(chan); @@ -6416,10 +6620,10 @@ res = dahdi_get_event(p->subs[index].dfd); ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res)); res = 0; - /* Let us detect distinctive ring */ - + /* Let us detect callerid when the telco uses distinctive ring */ + curRingData[receivedRingT] = p->ringt; - + if (p->ringt < p->ringt_base/2) break; /* Increment the ringT counter so we can match it against @@ -6437,14 +6641,16 @@ } break; } - if (p->ringt) - p->ringt--; + if (p->ringt) + p->ringt--; if (p->ringt == 1) { res = -1; break; } } } + } + if (p->usedistinctiveringdetection == 1) { if (option_verbose > 2) /* this only shows up if you have n of the dring patterns filled in */ ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]); @@ -6452,6 +6658,12 @@ for (counter = 0; counter < 3; counter++) { /* Check to see if the rings we received match any of the ones in chan_dahdi.conf for this channel */ + if (option_verbose > 2) + /* this only shows up if you have n of the dring patterns filled in */ + ast_verbose( VERBOSE_PREFIX_3 "Checking %d,%d,%d\n", + p->drings.ringnum[counter].ring[0], + p->drings.ringnum[counter].ring[1], + p->drings.ringnum[counter].ring[2]); distMatches = 0; for (counter1 = 0; counter1 < 3; counter1++) { if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >= @@ -6471,2895 +6683,2791 @@ } /* Restore linear mode (if appropriate) for Caller*ID processing */ dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear); -#if 1 + #if 1 restore_gains(p); -#endif + #endif + if (res < 0) { + ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name); + } } else - ast_log(LOG_WARNING, "Unable to get caller ID space\n"); - } else { - ast_log(LOG_WARNING, "Channel %s in prering " - "state, but I have nothing to do. " - "Terminating simple switch, should be " - "restarted by the actual ring.\n", - chan->name); + ast_log(LOG_WARNING, "Unable to get caller ID space\n"); + } + else + cs = NULL; + + if (number) + ast_shrink_phone_number(number); + ast_set_callerid(chan, number, name, number); + + if (smdi_msg) + ASTOBJ_UNREF(smdi_msg, ast_smdi_md_message_destroy); + + if (cs) + callerid_free(cs); + + ast_setstate(chan, AST_STATE_RING); + chan->rings = 1; + p->ringt = p->ringt_base; + res = ast_pbx_run(chan); + if (res) { ast_hangup(chan); - goto quit; + ast_log(LOG_WARNING, "PBX exited non-zero\n"); } - } else if (p->use_callerid && p->cid_start == CID_START_RING) { - /* FSK Bell202 callerID */ - cs = callerid_new(p->cid_signalling); - if (cs) { -#if 1 - bump_gains(p); -#endif - samples = 0; - len = 0; - distMatches = 0; - /* Clear the current ring data array so we dont have old data in it. */ - for (receivedRingT = 0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++) - curRingData[receivedRingT] = 0; - receivedRingT = 0; - counter = 0; - counter1 = 0; - /* Check to see if context is what it should be, if not set to be. */ - if (strcmp(p->context,p->defcontext) != 0) { - ast_copy_string(p->context, p->defcontext, sizeof(p->context)); - ast_copy_string(chan->context,p->defcontext,sizeof(chan->context)); - } - - /* Take out of linear mode for Caller*ID processing */ - dahdi_setlinear(p->subs[index].dfd, 0); - for (;;) { - i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT; - if ((res = ioctl(p->subs[index].dfd, DAHDI_IOMUX, &i))) { - ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno)); - callerid_free(cs); - ast_hangup(chan); - goto quit; - } - if (i & DAHDI_IOMUX_SIGEVENT) { - res = dahdi_get_event(p->subs[index].dfd); - ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res)); - /* If we get a PR event, they hung up while processing calerid */ - if ( res == DAHDI_EVENT_POLARITY && p->hanguponpolarityswitch && p->polarity == POLARITY_REV) { - ast_log(LOG_DEBUG, "Hanging up due to polarity reversal on channel %d while detecting callerid\n", p->channel); - p->polarity = POLARITY_IDLE; - callerid_free(cs); - ast_hangup(chan); - goto quit; - } - res = 0; - /* Let us detect callerid when the telco uses distinctive ring */ - - curRingData[receivedRingT] = p->ringt; - - if (p->ringt < p->ringt_base/2) - break; - /* Increment the ringT counter so we can match it against - values in chan_dahdi.conf for distinctive ring */ - if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0]))) - break; - } else if (i & DAHDI_IOMUX_READ) { - res = read(p->subs[index].dfd, buf, sizeof(buf)); - if (res < 0) { - if (errno != ELAST) { - ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno)); - callerid_free(cs); - ast_hangup(chan); - goto quit; - } - break; - } - if (p->ringt) - p->ringt--; - if (p->ringt == 1) { - res = -1; - break; - } - samples += res; - res = callerid_feed(cs, buf, res, AST_LAW(p)); - if (res < 0) { - ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno)); - break; - } else if (res) - break; - else if (samples > (8000 * 10)) - break; - } - } - if (res == 1) { - callerid_get(cs, &name, &number, &flags); - if (option_debug) - ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags); - } - if (distinctiveringaftercid == 1) { - /* Clear the current ring data array so we dont have old data in it. */ - for (receivedRingT = 0; receivedRingT < 3; receivedRingT++) { - curRingData[receivedRingT] = 0; - } - receivedRingT = 0; - if (option_verbose > 2) - ast_verbose( VERBOSE_PREFIX_3 "Detecting post-CID distinctive ring\n"); - for (;;) { - i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT; - if ((res = ioctl(p->subs[index].dfd, DAHDI_IOMUX, &i))) { - ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno)); - callerid_free(cs); - ast_hangup(chan); - goto quit; - } - if (i & DAHDI_IOMUX_SIGEVENT) { - res = dahdi_get_event(p->subs[index].dfd); - ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res)); - res = 0; - /* Let us detect callerid when the telco uses distinctive ring */ - - curRingData[receivedRingT] = p->ringt; - - if (p->ringt < p->ringt_base/2) - break; - /* Increment the ringT counter so we can match it against - values in chan_dahdi.conf for distinctive ring */ - if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0]))) - break; - } else if (i & DAHDI_IOMUX_READ) { - res = read(p->subs[index].dfd, buf, sizeof(buf)); - if (res < 0) { - if (errno != ELAST) { - ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno)); - callerid_free(cs); - ast_hangup(chan); - goto quit; - } - break; - } - if (p->ringt) - p->ringt--; - if (p->ringt == 1) { - res = -1; - break; - } - } - } - } - if (p->usedistinctiveringdetection == 1) { - if (option_verbose > 2) - /* this only shows up if you have n of the dring patterns filled in */ - ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]); - - for (counter = 0; counter < 3; counter++) { - /* Check to see if the rings we received match any of the ones in chan_dahdi.conf for this - channel */ - if (option_verbose > 2) - /* this only shows up if you have n of the dring patterns filled in */ - ast_verbose( VERBOSE_PREFIX_3 "Checking %d,%d,%d\n", - p->drings.ringnum[counter].ring[0], - p->drings.ringnum[counter].ring[1], - p->drings.ringnum[counter].ring[2]); - distMatches = 0; - for (counter1 = 0; counter1 < 3; counter1++) { - if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >= - (p->drings.ringnum[counter].ring[counter1]-10)) { - distMatches++; - } - } - if (distMatches == 3) { - /* The ring matches, set the context to whatever is for distinctive ring.. */ - ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context)); - ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context)); - if (option_verbose > 2) - ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context); - break; - } - } - } - /* Restore linear mode (if appropriate) for Caller*ID processing */ - dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear); -#if 1 - restore_gains(p); -#endif - if (res < 0) { - ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name); - } - } else - ast_log(LOG_WARNING, "Unable to get caller ID space\n"); - } - else - cs = NULL; - - if (number) - ast_shrink_phone_number(number); - ast_set_callerid(chan, number, name, number); - - if (smdi_msg) - ASTOBJ_UNREF(smdi_msg, ast_smdi_md_message_destroy); - - if (cs) - callerid_free(cs); - - ast_setstate(chan, AST_STATE_RING); - chan->rings = 1; - p->ringt = p->ringt_base; - res = ast_pbx_run(chan); - if (res) { - ast_hangup(chan); - ast_log(LOG_WARNING, "PBX exited non-zero\n"); + goto quit; + default: + ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", sig2str(p->sig), p->channel); + res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); + if (res < 0) + ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel); } - goto quit; - default: - ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", sig2str(p->sig), p->channel); res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); if (res < 0) ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel); - } - res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION); - if (res < 0) - ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel); - ast_hangup(chan); -quit: - ast_mutex_lock(&ss_thread_lock); - ss_thread_count--; - ast_cond_signal(&ss_thread_complete); - ast_mutex_unlock(&ss_thread_lock); - return NULL; -} - -/* destroy a DAHDI channel, identified by its number */ -static int dahdi_destroy_channel_bynum(int channel) -{ - struct dahdi_pvt *tmp = NULL; - struct dahdi_pvt *prev = NULL; - - tmp = iflist; - while (tmp) { - if (tmp->channel == channel) { - int x = DAHDI_FLASH; - ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_HOOK, &x); /* important to create an event for dahdi_wait_event to register so that all ss_threads terminate */ - destroy_channel(prev, tmp, 1); - ast_module_unref(ast_module_info->self); - return RESULT_SUCCESS; + ast_hangup(chan); + quit: + ast_mutex_lock(&ss_thread_lock); + ss_thread_count--; + ast_cond_signal(&ss_thread_complete); + ast_mutex_unlock(&ss_thread_lock); + return NULL; + } + + /* destroy a DAHDI channel, identified by its number */ + static int dahdi_destroy_channel_bynum(int channel) + { + struct dahdi_pvt *tmp = NULL; + struct dahdi_pvt *prev = NULL; + + tmp = iflist; + while (tmp) { + if (tmp->channel == channel) { + int x = DAHDI_FLASH; + ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_HOOK, &x); /* important to create an event for dahdi_wait_event to register so that all ss_threads terminate */ + destroy_channel(prev, tmp, 1); + ast_module_unref(ast_module_info->self); + return RESULT_SUCCESS; + } + prev = tmp; + tmp = tmp->next; } - prev = tmp; - tmp = tmp->next; + return RESULT_FAILURE; } - return RESULT_FAILURE; -} - -static int handle_init_event(struct dahdi_pvt *i, int event) -{ - int res; - pthread_t threadid; - pthread_attr_t attr; - struct ast_channel *chan; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - /* Handle an event on a given channel for the monitor thread. */ - switch (event) { - case DAHDI_EVENT_NONE: - case DAHDI_EVENT_BITSCHANGED: - break; - case DAHDI_EVENT_WINKFLASH: - case DAHDI_EVENT_RINGOFFHOOK: - if (i->inalarm) break; - if (i->radio) break; - /* Got a ring/answer. What kind of channel are we? */ - switch (i->sig) { - case SIG_FXOLS: - case SIG_FXOGS: - case SIG_FXOKS: - res = dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_OFFHOOK); - if (res && (errno == EBUSY)) - break; - if (i->cidspill) { - /* Cancel VMWI spill */ - free(i->cidspill); - i->cidspill = NULL; - } - if (i->immediate) { - dahdi_enable_ec(i); - /* The channel is immediately up. Start right away */ - res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE); - chan = dahdi_new(i, AST_STATE_RING, 1, SUB_REAL, 0, 0); - if (!chan) { - ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel); - res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION); - if (res < 0) - ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel); + + static int handle_init_event(struct dahdi_pvt *i, int event) + { + int res; + pthread_t threadid; + pthread_attr_t attr; + struct ast_channel *chan; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + /* Handle an event on a given channel for the monitor thread. */ + switch (event) { + case DAHDI_EVENT_NONE: + case DAHDI_EVENT_BITSCHANGED: + break; + case DAHDI_EVENT_WINKFLASH: + case DAHDI_EVENT_RINGOFFHOOK: + if (i->inalarm) break; + if (i->radio) break; + /* Got a ring/answer. What kind of channel are we? */ + switch (i->sig) { + case SIG_FXOLS: + case SIG_FXOGS: + case SIG_FXOKS: + res = dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_OFFHOOK); + if (res && (errno == EBUSY)) + break; + if (i->cidspill) { + /* Cancel VMWI spill */ + free(i->cidspill); + i->cidspill = NULL; + } + if (i->immediate) { + dahdi_enable_ec(i); + /* The channel is immediately up. Start right away */ + res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE); + chan = dahdi_new(i, AST_STATE_RING, 1, SUB_REAL, 0, 0, -1); + if (!chan) { + ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel); + res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION); + if (res < 0) + ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel); + } + } else { + /* Check for callerid, digits, etc */ + chan = dahdi_new(i, AST_STATE_RESERVED, 0, SUB_REAL, 0, 0, -1); + if (chan) { + if (has_voicemail(i)) + res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_STUTTER); + else + res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_DIALTONE); + if (res < 0) + ast_log(LOG_WARNING, "Unable to play dialtone on channel %d, do you have defaultzone and loadzone defined?\n", i->channel); + if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) { + ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel); + res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION); + if (res < 0) + ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel); + ast_hangup(chan); + } + } else + ast_log(LOG_WARNING, "Unable to create channel\n"); } - } else { - /* Check for callerid, digits, etc */ - chan = dahdi_new(i, AST_STATE_RESERVED, 0, SUB_REAL, 0, 0); - if (chan) { - if (has_voicemail(i)) - res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_STUTTER); - else - res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_DIALTONE); - if (res < 0) - ast_log(LOG_WARNING, "Unable to play dialtone on channel %d, do you have defaultzone and loadzone defined?\n", i->channel); - if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) { + break; + case SIG_FXSLS: + case SIG_FXSGS: + case SIG_FXSKS: + i->ringt = i->ringt_base; + /* Fall through */ + case SIG_EMWINK: + case SIG_FEATD: + case SIG_FEATDMF: + case SIG_FEATDMF_TA: + case SIG_E911: + case SIG_FGC_CAMA: + case SIG_FGC_CAMAMF: + case SIG_FEATB: + case SIG_EM: + case SIG_EM_E1: + case SIG_SFWINK: + case SIG_SF_FEATD: + case SIG_SF_FEATDMF: + case SIG_SF_FEATB: + case SIG_SF: + /* Check for callerid, digits, etc */ + chan = dahdi_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0, -1); + if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) { ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel); res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION); if (res < 0) ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel); ast_hangup(chan); + } else if (!chan) { + ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel); } - } else - ast_log(LOG_WARNING, "Unable to create channel\n"); - } - break; - case SIG_FXSLS: - case SIG_FXSGS: - case SIG_FXSKS: - i->ringt = i->ringt_base; - /* Fall through */ - case SIG_EMWINK: - case SIG_FEATD: - case SIG_FEATDMF: - case SIG_FEATDMF_TA: - case SIG_E911: - case SIG_FGC_CAMA: - case SIG_FGC_CAMAMF: - case SIG_FEATB: - case SIG_EM: - case SIG_EM_E1: - case SIG_SFWINK: - case SIG_SF_FEATD: - case SIG_SF_FEATDMF: - case SIG_SF_FEATB: - case SIG_SF: - /* Check for callerid, digits, etc */ - chan = dahdi_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0); - if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) { - ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel); - res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION); - if (res < 0) + break; + default: + ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", sig2str(i->sig), i->channel); + res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION); + if (res < 0) ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel); - ast_hangup(chan); - } else if (!chan) { - ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel); - } - break; - default: - ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", sig2str(i->sig), i->channel); - res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION); - if (res < 0) - ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel); - return -1; - } - break; - case DAHDI_EVENT_NOALARM: - i->inalarm = 0; - if (!i->unknown_alarm) { - ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", i->channel); - manager_event(EVENT_FLAG_SYSTEM, "AlarmClear", - "Channel: %d\r\n", i->channel); - } else { - i->unknown_alarm = 0; - } - break; - case DAHDI_EVENT_ALARM: - i->inalarm = 1; - res = get_alarms(i); - handle_alarms(i, res); - /* fall thru intentionally */ - case DAHDI_EVENT_ONHOOK: - if (i->radio) - break; - /* Back on hook. Hang up. */ - switch (i->sig) { - case SIG_FXOLS: - case SIG_FXOGS: - case SIG_FEATD: - case SIG_FEATDMF: - case SIG_FEATDMF_TA: - case SIG_E911: - case SIG_FGC_CAMA: - case SIG_FGC_CAMAMF: - case SIG_FEATB: - case SIG_EM: - case SIG_EM_E1: - case SIG_EMWINK: - case SIG_SF_FEATD: - case SIG_SF_FEATDMF: - case SIG_SF_FEATB: - case SIG_SF: - case SIG_SFWINK: - case SIG_FXSLS: - case SIG_FXSGS: - case SIG_FXSKS: - case SIG_GR303FXSKS: - dahdi_disable_ec(i); - res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1); - dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_ONHOOK); + return -1; + } break; - case SIG_GR303FXOKS: - case SIG_FXOKS: - dahdi_disable_ec(i); - /* Diddle the battery for the zhone */ -#ifdef ZHONE_HACK - dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_OFFHOOK); - usleep(1); -#endif - res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1); - dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_ONHOOK); + case DAHDI_EVENT_NOALARM: + i->inalarm = 0; + if (!i->unknown_alarm) { + ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", i->channel); + manager_event(EVENT_FLAG_SYSTEM, "AlarmClear", + "Channel: %d\r\n", i->channel); + } else { + i->unknown_alarm = 0; + } break; - case SIG_PRI: - dahdi_disable_ec(i); - res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1); + case DAHDI_EVENT_ALARM: + i->inalarm = 1; + res = get_alarms(i); + handle_alarms(i, res); + /* fall thru intentionally */ + case DAHDI_EVENT_ONHOOK: + if (i->radio) + break; + /* Back on hook. Hang up. */ + switch (i->sig) { + case SIG_FXOLS: + case SIG_FXOGS: + case SIG_FEATD: + case SIG_FEATDMF: + case SIG_FEATDMF_TA: + case SIG_E911: + case SIG_FGC_CAMA: + case SIG_FGC_CAMAMF: + case SIG_FEATB: + case SIG_EM: + case SIG_EM_E1: + case SIG_EMWINK: + case SIG_SF_FEATD: + case SIG_SF_FEATDMF: + case SIG_SF_FEATB: + case SIG_SF: + case SIG_SFWINK: + case SIG_FXSLS: + case SIG_FXSGS: + case SIG_FXSKS: + case SIG_GR303FXSKS: + dahdi_disable_ec(i); + res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1); + dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_ONHOOK); + break; + case SIG_GR303FXOKS: + case SIG_FXOKS: + dahdi_disable_ec(i); + /* Diddle the battery for the zhone */ + #ifdef ZHONE_HACK + dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_OFFHOOK); + usleep(1); + #endif + res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1); + dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_ONHOOK); + break; + case SIG_PRI: + dahdi_disable_ec(i); + res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1); + break; + default: + ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", sig2str(i->sig), i->channel); + res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1); + return -1; + } break; - default: - ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", sig2str(i->sig), i->channel); - res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1); - return -1; - } - break; - case DAHDI_EVENT_POLARITY: - switch (i->sig) { - case SIG_FXSLS: - case SIG_FXSKS: - case SIG_FXSGS: - /* We have already got a PR before the channel was - created, but it wasn't handled. We need polarity - to be REV for remote hangup detection to work. - At least in Spain */ - if (i->hanguponpolarityswitch) - i->polarity = POLARITY_REV; - - if (i->cid_start == CID_START_POLARITY) { - i->polarity = POLARITY_REV; - ast_verbose(VERBOSE_PREFIX_2 "Starting post polarity " - "CID detection on channel %d\n", - i->channel); - chan = dahdi_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0); - if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) { - ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel); + case DAHDI_EVENT_POLARITY: + switch (i->sig) { + case SIG_FXSLS: + case SIG_FXSKS: + case SIG_FXSGS: + /* We have already got a PR before the channel was + created, but it wasn't handled. We need polarity + to be REV for remote hangup detection to work. + At least in Spain */ + if (i->hanguponpolarityswitch) + i->polarity = POLARITY_REV; + + if (i->cid_start == CID_START_POLARITY) { + i->polarity = POLARITY_REV; + ast_verbose(VERBOSE_PREFIX_2 "Starting post polarity " + "CID detection on channel %d\n", + i->channel); + chan = dahdi_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0, -1); + if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) { + ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel); + } } + break; + default: + ast_log(LOG_WARNING, "handle_init_event detected " + "polarity reversal on non-FXO (SIG_FXS) " + "interface %d\n", i->channel); } break; - default: - ast_log(LOG_WARNING, "handle_init_event detected " - "polarity reversal on non-FXO (SIG_FXS) " - "interface %d\n", i->channel); + case DAHDI_EVENT_REMOVED: /* destroy channel */ + ast_log(LOG_NOTICE, + "Got DAHDI_EVENT_REMOVED. Destroying channel %d\n", + i->channel); + dahdi_destroy_channel_bynum(i->channel); + break; } - break; - case DAHDI_EVENT_REMOVED: /* destroy channel */ - ast_log(LOG_NOTICE, - "Got DAHDI_EVENT_REMOVED. Destroying channel %d\n", - i->channel); - dahdi_destroy_channel_bynum(i->channel); - break; - } - pthread_attr_destroy(&attr); - return 0; -} - -static void *do_monitor(void *data) -{ - int count, res, res2, spoint, pollres=0; - struct dahdi_pvt *i; - struct dahdi_pvt *last = NULL; - time_t thispass = 0, lastpass = 0; - int found; - char buf[1024]; - struct pollfd *pfds=NULL; - int lastalloc = -1; - /* This thread monitors all the frame relay interfaces which are not yet in use - (and thus do not have a separate thread) indefinitely */ - /* From here on out, we die whenever asked */ -#if 0 - if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) { - ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n"); - return NULL; + pthread_attr_destroy(&attr); + return 0; } - ast_log(LOG_DEBUG, "Monitor starting...\n"); -#endif - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); - - for (;;) { - /* Lock the interface list */ - ast_mutex_lock(&iflock); - if (!pfds || (lastalloc != ifcount)) { - if (pfds) { - free(pfds); - pfds = NULL; - } - if (ifcount) { - if (!(pfds = ast_calloc(1, ifcount * sizeof(*pfds)))) { - ast_mutex_unlock(&iflock); - return NULL; + + static void *do_monitor(void *data) + { + int count, res, res2, spoint, pollres=0; + struct dahdi_pvt *i; + struct dahdi_pvt *last = NULL; + time_t thispass = 0, lastpass = 0; + int found; + char buf[1024]; + struct pollfd *pfds=NULL; + int lastalloc = -1; + /* This thread monitors all the frame relay interfaces which are not yet in use + (and thus do not have a separate thread) indefinitely */ + /* From here on out, we die whenever asked */ + #if 0 + if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) { + ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n"); + return NULL; + } + ast_log(LOG_DEBUG, "Monitor starting...\n"); + #endif + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + + for (;;) { + /* Lock the interface list */ + ast_mutex_lock(&iflock); + if (!pfds || (lastalloc != ifcount)) { + if (pfds) { + free(pfds); + pfds = NULL; + } + if (ifcount) { + if (!(pfds = ast_calloc(1, ifcount * sizeof(*pfds)))) { + ast_mutex_unlock(&iflock); + return NULL; + } } + lastalloc = ifcount; } - lastalloc = ifcount; - } - /* Build the stuff we're going to poll on, that is the socket of every - dahdi_pvt that does not have an associated owner channel */ - count = 0; - i = iflist; - while (i) { - if ((i->subs[SUB_REAL].dfd > -1) && i->sig && (!i->radio)) { - if (!i->owner && !i->subs[SUB_REAL].owner) { - /* This needs to be watched, as it lacks an owner */ - pfds[count].fd = i->subs[SUB_REAL].dfd; - pfds[count].events = POLLPRI; - pfds[count].revents = 0; - /* Message waiting or r2 channels also get watched for reading */ - if (i->cidspill) - pfds[count].events |= POLLIN; - count++; + /* Build the stuff we're going to poll on, that is the socket of every + dahdi_pvt that does not have an associated owner channel */ + count = 0; + i = iflist; + while (i) { + if ((i->subs[SUB_REAL].dfd > -1) && i->sig && (!i->radio)) { + if (!i->owner && !i->subs[SUB_REAL].owner) { + /* This needs to be watched, as it lacks an owner */ + pfds[count].fd = i->subs[SUB_REAL].dfd; + pfds[count].events = POLLPRI; + pfds[count].revents = 0; + /* Message waiting or r2 channels also get watched for reading */ + if (i->cidspill) + pfds[count].events |= POLLIN; + count++; + } } + i = i->next; } - i = i->next; - } - /* Okay, now that we know what to do, release the interface lock */ - ast_mutex_unlock(&iflock); - - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); - pthread_testcancel(); - /* Wait at least a second for something to happen */ - res = poll(pfds, count, 1000); - pthread_testcancel(); - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); - - /* Okay, poll has finished. Let's see what happened. */ - if (res < 0) { - if ((errno != EAGAIN) && (errno != EINTR)) - ast_log(LOG_WARNING, "poll return %d: %s\n", res, strerror(errno)); - continue; - } - /* Alright, lock the interface list again, and let's look and see what has - happened */ - ast_mutex_lock(&iflock); - found = 0; - spoint = 0; - lastpass = thispass; - thispass = time(NULL); - i = iflist; - while (i) { - if (thispass != lastpass) { - if (!found && ((i == last) || ((i == iflist) && !last))) { - last = i; - if (last) { - if (!last->cidspill && !last->owner && !ast_strlen_zero(last->mailbox) && (thispass - last->onhooktime > 3) && - (last->sig & __DAHDI_SIG_FXO)) { - res = ast_app_has_voicemail(last->mailbox, NULL); - if (last->msgstate != res) { - int x; - ast_log(LOG_DEBUG, "Message status for %s changed from %d to %d on %d\n", last->mailbox, last->msgstate, res, last->channel); - x = DAHDI_FLUSH_BOTH; - res2 = ioctl(last->subs[SUB_REAL].dfd, DAHDI_FLUSH, &x); - if (res2) - ast_log(LOG_WARNING, "Unable to flush input on channel %d: %s\n", last->channel, strerror(errno)); - if ((last->cidspill = ast_calloc(1, MAX_CALLERID_SIZE))) { - /* Turn on on hook transfer for 4 seconds */ - x = 4000; - ioctl(last->subs[SUB_REAL].dfd, DAHDI_ONHOOKTRANSFER, &x); - last->cidlen = vmwi_generate(last->cidspill, res, 1, AST_LAW(last)); - last->cidpos = 0; - last->msgstate = res; - last->onhooktime = thispass; + /* Okay, now that we know what to do, release the interface lock */ + ast_mutex_unlock(&iflock); + + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + pthread_testcancel(); + /* Wait at least a second for something to happen */ + res = poll(pfds, count, 1000); + pthread_testcancel(); + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + + /* Okay, poll has finished. Let's see what happened. */ + if (res < 0) { + if ((errno != EAGAIN) && (errno != EINTR)) + ast_log(LOG_WARNING, "poll return %d: %s\n", res, strerror(errno)); + continue; + } + /* Alright, lock the interface list again, and let's look and see what has + happened */ + ast_mutex_lock(&iflock); + found = 0; + spoint = 0; + lastpass = thispass; + thispass = time(NULL); + i = iflist; + while (i) { + if (thispass != lastpass) { + if (!found && ((i == last) || ((i == iflist) && !last))) { + last = i; + if (last) { + if (!last->cidspill && !last->owner && !ast_strlen_zero(last->mailbox) && (thispass - last->onhooktime > 3) && + (last->sig & __DAHDI_SIG_FXO)) { + res = ast_app_has_voicemail(last->mailbox, NULL); + if (last->msgstate != res) { + int x; + ast_log(LOG_DEBUG, "Message status for %s changed from %d to %d on %d\n", last->mailbox, last->msgstate, res, last->channel); + x = DAHDI_FLUSH_BOTH; + res2 = ioctl(last->subs[SUB_REAL].dfd, DAHDI_FLUSH, &x); + if (res2) + ast_log(LOG_WARNING, "Unable to flush input on channel %d: %s\n", last->channel, strerror(errno)); + if ((last->cidspill = ast_calloc(1, MAX_CALLERID_SIZE))) { + /* Turn on on hook transfer for 4 seconds */ + x = 4000; + ioctl(last->subs[SUB_REAL].dfd, DAHDI_ONHOOKTRANSFER, &x); + last->cidlen = vmwi_generate(last->cidspill, res, 1, AST_LAW(last)); + last->cidpos = 0; + last->msgstate = res; + last->onhooktime = thispass; + } + found ++; } - found ++; } + last = last->next; } - last = last->next; } } - } - if ((i->subs[SUB_REAL].dfd > -1) && i->sig) { - if (i->radio && !i->owner) - { - res = dahdi_get_event(i->subs[SUB_REAL].dfd); - if (res) + if ((i->subs[SUB_REAL].dfd > -1) && i->sig) { + if (i->radio && !i->owner) { - if (option_debug) - ast_log(LOG_DEBUG, "Monitor doohicky got event %s on radio channel %d\n", event2str(res), i->channel); - /* Don't hold iflock while handling init events */ - ast_mutex_unlock(&iflock); - handle_init_event(i, res); - ast_mutex_lock(&iflock); - } - i = i->next; - continue; - } - pollres = ast_fdisset(pfds, i->subs[SUB_REAL].dfd, count, &spoint); - if (pollres & POLLIN) { - if (i->owner || i->subs[SUB_REAL].owner) { -#ifdef HAVE_PRI - if (!i->pri) -#endif - ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d) in read...\n", i->subs[SUB_REAL].dfd); - i = i->next; - continue; - } - if (!i->cidspill) { - ast_log(LOG_WARNING, "Whoa.... I'm reading but have no cidspill (%d)...\n", i->subs[SUB_REAL].dfd); + res = dahdi_get_event(i->subs[SUB_REAL].dfd); + if (res) + { + if (option_debug) + ast_log(LOG_DEBUG, "Monitor doohicky got event %s on radio channel %d\n", event2str(res), i->channel); + /* Don't hold iflock while handling init events */ + ast_mutex_unlock(&iflock); + handle_init_event(i, res); + ast_mutex_lock(&iflock); + } i = i->next; continue; - } - res = read(i->subs[SUB_REAL].dfd, buf, sizeof(buf)); - if (res > 0) { - /* We read some number of bytes. Write an equal amount of data */ - if (res > i->cidlen - i->cidpos) - res = i->cidlen - i->cidpos; - res2 = write(i->subs[SUB_REAL].dfd, i->cidspill + i->cidpos, res); - if (res2 > 0) { - i->cidpos += res2; - if (i->cidpos >= i->cidlen) { - free(i->cidspill); - i->cidspill = 0; - i->cidpos = 0; - i->cidlen = 0; + } + pollres = ast_fdisset(pfds, i->subs[SUB_REAL].dfd, count, &spoint); + if (pollres & POLLIN) { + if (i->owner || i->subs[SUB_REAL].owner) { + #ifdef HAVE_PRI + if (!i->pri) + #endif + ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d) in read...\n", i->subs[SUB_REAL].dfd); + i = i->next; + continue; + } + if (!i->cidspill) { + ast_log(LOG_WARNING, "Whoa.... I'm reading but have no cidspill (%d)...\n", i->subs[SUB_REAL].dfd); + i = i->next; + continue; + } + res = read(i->subs[SUB_REAL].dfd, buf, sizeof(buf)); + if (res > 0) { + /* We read some number of bytes. Write an equal amount of data */ + if (res > i->cidlen - i->cidpos) + res = i->cidlen - i->cidpos; + res2 = write(i->subs[SUB_REAL].dfd, i->cidspill + i->cidpos, res); + if (res2 > 0) { + i->cidpos += res2; + if (i->cidpos >= i->cidlen) { + free(i->cidspill); + i->cidspill = 0; + i->cidpos = 0; + i->cidlen = 0; + } + } else { + ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno)); + i->msgstate = -1; } } else { - ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno)); - i->msgstate = -1; + ast_log(LOG_WARNING, "Read failed with %d: %s\n", res, strerror(errno)); } - } else { - ast_log(LOG_WARNING, "Read failed with %d: %s\n", res, strerror(errno)); } - } - if (pollres & POLLPRI) { - if (i->owner || i->subs[SUB_REAL].owner) { -#ifdef HAVE_PRI - if (!i->pri) -#endif - ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d)...\n", i->subs[SUB_REAL].dfd); - i = i->next; - continue; + if (pollres & POLLPRI) { + if (i->owner || i->subs[SUB_REAL].owner) { + #ifdef HAVE_PRI + if (!i->pri) + #endif + ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d)...\n", i->subs[SUB_REAL].dfd); + i = i->next; + continue; + } + res = dahdi_get_event(i->subs[SUB_REAL].dfd); + if (option_debug) + ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel); + /* Don't hold iflock while handling init events */ + ast_mutex_unlock(&iflock); + handle_init_event(i, res); + ast_mutex_lock(&iflock); } - res = dahdi_get_event(i->subs[SUB_REAL].dfd); - if (option_debug) - ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel); - /* Don't hold iflock while handling init events */ - ast_mutex_unlock(&iflock); - handle_init_event(i, res); - ast_mutex_lock(&iflock); } + i=i->next; } - i=i->next; + ast_mutex_unlock(&iflock); } - ast_mutex_unlock(&iflock); + /* Never reached */ + return NULL; + } - /* Never reached */ - return NULL; -} - -static int restart_monitor(void) -{ - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - /* If we're supposed to be stopped -- stay stopped */ - if (monitor_thread == AST_PTHREADT_STOP) - return 0; - ast_mutex_lock(&monlock); - if (monitor_thread == pthread_self()) { - ast_mutex_unlock(&monlock); - ast_log(LOG_WARNING, "Cannot kill myself\n"); - return -1; - } - if (monitor_thread != AST_PTHREADT_NULL) { - /* Wake up the thread */ - pthread_kill(monitor_thread, SIGURG); - } else { - /* Start a new monitor */ - if (ast_pthread_create_background(&monitor_thread, &attr, do_monitor, NULL) < 0) { + static int restart_monitor(void) + { + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + /* If we're supposed to be stopped -- stay stopped */ + if (monitor_thread == AST_PTHREADT_STOP) + return 0; + ast_mutex_lock(&monlock); + if (monitor_thread == pthread_self()) { ast_mutex_unlock(&monlock); - ast_log(LOG_ERROR, "Unable to start monitor thread.\n"); - pthread_attr_destroy(&attr); + ast_log(LOG_WARNING, "Cannot kill myself\n"); return -1; } - } - ast_mutex_unlock(&monlock); - pthread_attr_destroy(&attr); - return 0; -} - -#ifdef HAVE_PRI -static int pri_resolve_span(int *span, int channel, int offset, struct dahdi_spaninfo *si) -{ - int x; - int trunkgroup; - /* Get appropriate trunk group if there is one */ - trunkgroup = pris[*span].mastertrunkgroup; - if (trunkgroup) { - /* Select a specific trunk group */ - for (x = 0; x < NUM_SPANS; x++) { - if (pris[x].trunkgroup == trunkgroup) { - *span = x; - return 0; + if (monitor_thread != AST_PTHREADT_NULL) { + /* Wake up the thread */ + pthread_kill(monitor_thread, SIGURG); + } else { + /* Start a new monitor */ + if (ast_pthread_create_background(&monitor_thread, &attr, do_monitor, NULL) < 0) { + ast_mutex_unlock(&monlock); + ast_log(LOG_ERROR, "Unable to start monitor thread.\n"); + pthread_attr_destroy(&attr); + return -1; } } - ast_log(LOG_WARNING, "Channel %d on span %d configured to use nonexistent trunk group %d\n", channel, *span, trunkgroup); - *span = -1; - } else { - if (pris[*span].trunkgroup) { - ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is trunk group %d (please use spanmap)\n", *span, pris[*span].trunkgroup); - *span = -1; - } else if (pris[*span].mastertrunkgroup) { - ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is already part of trunk group %d\n", *span, pris[*span].mastertrunkgroup); + ast_mutex_unlock(&monlock); + pthread_attr_destroy(&attr); + return 0; + } + + #ifdef HAVE_PRI + static int pri_resolve_span(int *span, int channel, int offset, struct dahdi_spaninfo *si) + { + int x; + int trunkgroup; + /* Get appropriate trunk group if there is one */ + trunkgroup = pris[*span].mastertrunkgroup; + if (trunkgroup) { + /* Select a specific trunk group */ + for (x = 0; x < NUM_SPANS; x++) { + if (pris[x].trunkgroup == trunkgroup) { + *span = x; + return 0; + } + } + ast_log(LOG_WARNING, "Channel %d on span %d configured to use nonexistent trunk group %d\n", channel, *span, trunkgroup); *span = -1; } else { - if (si->totalchans == 31) { - /* E1 */ - pris[*span].dchannels[0] = 16 + offset; - } else if (si->totalchans == 24) { - /* T1 or J1 */ - pris[*span].dchannels[0] = 24 + offset; - } else if (si->totalchans == 3) { - /* BRI */ - pris[*span].dchannels[0] = 3 + offset; - } else { - ast_log(LOG_WARNING, "Unable to use span %d, since the D-channel cannot be located (unexpected span size of %d channels)\n", *span, si->totalchans); + if (pris[*span].trunkgroup) { + ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is trunk group %d (please use spanmap)\n", *span, pris[*span].trunkgroup); *span = -1; - return 0; + } else if (pris[*span].mastertrunkgroup) { + ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is already part of trunk group %d\n", *span, pris[*span].mastertrunkgroup); + *span = -1; + } else { + if (si->totalchans == 31) { + /* E1 */ + pris[*span].dchannels[0] = 16 + offset; + } else if (si->totalchans == 24) { + /* T1 or J1 */ + pris[*span].dchannels[0] = 24 + offset; + } else if (si->totalchans == 3) { + /* BRI */ + pris[*span].dchannels[0] = 3 + offset; + } else { + ast_log(LOG_WARNING, "Unable to use span %d, since the D-channel cannot be located (unexpected span size of %d channels)\n", *span, si->totalchans); + *span = -1; + return 0; + } + pris[*span].dchanavail[0] |= DCHAN_PROVISIONED; + pris[*span].offset = offset; + pris[*span].span = *span + 1; } - pris[*span].dchanavail[0] |= DCHAN_PROVISIONED; - pris[*span].offset = offset; - pris[*span].span = *span + 1; - } - } - return 0; -} - -static int pri_create_trunkgroup(int trunkgroup, int *channels) -{ - struct dahdi_spaninfo si; - struct dahdi_params p; - int fd; - int span; - int ospan=0; - int x,y; - for (x = 0; x < NUM_SPANS; x++) { - if (pris[x].trunkgroup == trunkgroup) { - ast_log(LOG_WARNING, "Trunk group %d already exists on span %d, Primary d-channel %d\n", trunkgroup, x + 1, pris[x].dchannels[0]); - return -1; } + return 0; } - for (y = 0; y < NUM_DCHANS; y++) { - if (!channels[y]) - break; - memset(&si, 0, sizeof(si)); - memset(&p, 0, sizeof(p)); -#ifdef HAVE_ZAPTEL - fd = open("/dev/zap/channel", O_RDWR); -#else - fd = open("/dev/dahdi/channel", O_RDWR); -#endif - if (fd < 0) { - ast_log(LOG_WARNING, "Failed to open channel: %s\n", strerror(errno)); - return -1; - } - x = channels[y]; - if (ioctl(fd, DAHDI_SPECIFY, &x)) { - ast_log(LOG_WARNING, "Failed to specify channel %d: %s\n", channels[y], strerror(errno)); - close(fd); - return -1; - } - if (ioctl(fd, DAHDI_GET_PARAMS, &p)) { - ast_log(LOG_WARNING, "Failed to get channel parameters for channel %d: %s\n", channels[y], strerror(errno)); - return -1; - } - if (ioctl(fd, DAHDI_SPANSTAT, &si)) { - ast_log(LOG_WARNING, "Failed go get span information on channel %d (span %d): %s\n", channels[y], p.spanno, strerror(errno)); - close(fd); - return -1; - } - span = p.spanno - 1; - if (pris[span].trunkgroup) { - ast_log(LOG_WARNING, "Span %d is already provisioned for trunk group %d\n", span + 1, pris[span].trunkgroup); - close(fd); - return -1; - } - if (pris[span].pvts[0]) { - ast_log(LOG_WARNING, "Span %d is already provisioned with channels (implicit PRI maybe?)\n", span + 1); - close(fd); - return -1; + + static int pri_create_trunkgroup(int trunkgroup, int *channels) + { + struct dahdi_spaninfo si; + struct dahdi_params p; + int fd; + int span; + int ospan=0; + int x,y; + for (x = 0; x < NUM_SPANS; x++) { + if (pris[x].trunkgroup == trunkgroup) { + ast_log(LOG_WARNING, "Trunk group %d already exists on span %d, Primary d-channel %d\n", trunkgroup, x + 1, pris[x].dchannels[0]); + return -1; + } } - if (!y) { - pris[span].trunkgroup = trunkgroup; - pris[span].offset = channels[y] - p.chanpos; - ospan = span; - } - pris[ospan].dchannels[y] = channels[y]; - pris[ospan].dchanavail[y] |= DCHAN_PROVISIONED; - pris[span].span = span + 1; - close(fd); - } - return 0; -} - -static int pri_create_spanmap(int span, int trunkgroup, int logicalspan) -{ - if (pris[span].mastertrunkgroup) { - ast_log(LOG_WARNING, "Span %d is already part of trunk group %d, cannot add to trunk group %d\n", span + 1, pris[span].mastertrunkgroup, trunkgroup); - return -1; - } - pris[span].mastertrunkgroup = trunkgroup; - pris[span].prilogicalspan = logicalspan; - return 0; -} - -#endif - -static struct dahdi_pvt *mkintf(int channel, const struct dahdi_chan_conf *conf, struct dahdi_pri *pri, int reloading) -{ - /* Make a dahdi_pvt structure for this interface (or CRV if "pri" is specified) */ - struct dahdi_pvt *tmp = NULL, *tmp2, *prev = NULL; - char fn[80]; -#if 1 - struct dahdi_bufferinfo bi; -#endif - int res; - int span=0; - int here = 0; - int x; - struct dahdi_pvt **wlist; - struct dahdi_pvt **wend; - struct dahdi_params p; - - wlist = &iflist; - wend = &ifend; - -#ifdef HAVE_PRI - if (pri) { - wlist = &pri->crvs; - wend = &pri->crvend; - } -#endif - - tmp2 = *wlist; - prev = NULL; - - while (tmp2) { - if (!tmp2->destroy) { - if (tmp2->channel == channel) { - tmp = tmp2; - here = 1; + for (y = 0; y < NUM_DCHANS; y++) { + if (!channels[y]) break; + memset(&si, 0, sizeof(si)); + memset(&p, 0, sizeof(p)); + #ifdef HAVE_ZAPTEL + fd = open("/dev/zap/channel", O_RDWR); + #else + fd = open("/dev/dahdi/channel", O_RDWR); + #endif + if (fd < 0) { + ast_log(LOG_WARNING, "Failed to open channel: %s\n", strerror(errno)); + return -1; } - if (tmp2->channel > channel) { - break; + x = channels[y]; + if (ioctl(fd, DAHDI_SPECIFY, &x)) { + ast_log(LOG_WARNING, "Failed to specify channel %d: %s\n", channels[y], strerror(errno)); + close(fd); + return -1; + } + if (ioctl(fd, DAHDI_GET_PARAMS, &p)) { + ast_log(LOG_WARNING, "Failed to get channel parameters for channel %d: %s\n", channels[y], strerror(errno)); + return -1; + } + if (ioctl(fd, DAHDI_SPANSTAT, &si)) { + ast_log(LOG_WARNING, "Failed go get span information on channel %d (span %d): %s\n", channels[y], p.spanno, strerror(errno)); + close(fd); + return -1; + } + span = p.spanno - 1; + if (pris[span].trunkgroup) { + ast_log(LOG_WARNING, "Span %d is already provisioned for trunk group %d\n", span + 1, pris[span].trunkgroup); + close(fd); + return -1; + } + if (pris[span].pvts[0]) { + ast_log(LOG_WARNING, "Span %d is already provisioned with channels (implicit PRI maybe?)\n", span + 1); + close(fd); + return -1; } + if (!y) { + pris[span].trunkgroup = trunkgroup; + pris[span].offset = channels[y] - p.chanpos; + ospan = span; + } + pris[ospan].dchannels[y] = channels[y]; + pris[ospan].dchanavail[y] |= DCHAN_PROVISIONED; + pris[span].span = span + 1; + close(fd); } - prev = tmp2; - tmp2 = tmp2->next; + return 0; } - - if (!here && reloading != 1) { - if (!(tmp = ast_calloc(1, sizeof(*tmp)))) { - if (tmp) - free(tmp); - return NULL; + + static int pri_create_spanmap(int span, int trunkgroup, int logicalspan) + { + if (pris[span].mastertrunkgroup) { + ast_log(LOG_WARNING, "Span %d is already part of trunk group %d, cannot add to trunk group %d\n", span + 1, pris[span].mastertrunkgroup, trunkgroup); + return -1; } - ast_mutex_init(&tmp->lock); - ifcount++; - for (x = 0; x < 3; x++) - tmp->subs[x].dfd = -1; - tmp->channel = channel; - } - - if (tmp) { - int chan_sig = conf->chan.sig; - if (!here) { - if ((channel != CHAN_PSEUDO) && !pri) { - int count = 0; - snprintf(fn, sizeof(fn), "%d", channel); - /* Open non-blocking */ - tmp->subs[SUB_REAL].dfd = dahdi_open(fn); - while (tmp->subs[SUB_REAL].dfd < 0 && reloading == 2 && count < 1000) { /* the kernel may not call dahdi_release fast enough for the open flagbit to be cleared in time */ - usleep(1); - tmp->subs[SUB_REAL].dfd = dahdi_open(fn); - count++; - } - /* Allocate a DAHDI structure */ - if (tmp->subs[SUB_REAL].dfd < 0) { - ast_log(LOG_ERROR, "Unable to open channel %d: %s\nhere = %d, tmp->channel = %d, channel = %d\n", channel, strerror(errno), here, tmp->channel, channel); - destroy_dahdi_pvt(&tmp); - return NULL; - } - memset(&p, 0, sizeof(p)); - res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &p); - if (res < 0) { - ast_log(LOG_ERROR, "Unable to get parameters: %s\n", strerror(errno)); - destroy_dahdi_pvt(&tmp); - return NULL; - } - if (p.sigtype != (conf->chan.sig & 0x3ffff)) { - ast_log(LOG_ERROR, "Signalling requested on channel %d is %s but line is in %s signalling\n", channel, sig2str(conf->chan.sig), sig2str(p.sigtype)); - destroy_dahdi_pvt(&tmp); - return NULL; + pris[span].mastertrunkgroup = trunkgroup; + pris[span].prilogicalspan = logicalspan; + return 0; + } + + #endif + + static struct dahdi_pvt *mkintf(int channel, const struct dahdi_chan_conf *conf, struct dahdi_pri *pri, int reloading) + { + /* Make a dahdi_pvt structure for this interface (or CRV if "pri" is specified) */ + struct dahdi_pvt *tmp = NULL, *tmp2, *prev = NULL; + char fn[80]; + #if 1 + struct dahdi_bufferinfo bi; + #endif + int res; + int span=0; + int here = 0; + int x; + struct dahdi_pvt **wlist; + struct dahdi_pvt **wend; + struct dahdi_params p; + + wlist = &iflist; + wend = &ifend; + + #ifdef HAVE_PRI + if (pri) { + wlist = &pri->crvs; + wend = &pri->crvend; + } + #endif + + tmp2 = *wlist; + prev = NULL; + + while (tmp2) { + if (!tmp2->destroy) { + if (tmp2->channel == channel) { + tmp = tmp2; + here = 1; + break; } - tmp->law = p.curlaw; - tmp->span = p.spanno; - span = p.spanno - 1; - } else { - if (channel == CHAN_PSEUDO) - chan_sig = 0; - else if ((chan_sig != SIG_FXOKS) && (chan_sig != SIG_FXSKS)) { - ast_log(LOG_ERROR, "CRV's must use FXO/FXS Kewl Start (fxo_ks/fxs_ks) signalling only.\n"); - return NULL; + if (tmp2->channel > channel) { + break; } } -#ifdef HAVE_PRI - if ((chan_sig == SIG_PRI) || (chan_sig == SIG_GR303FXOKS) || (chan_sig == SIG_GR303FXSKS)) { - int offset; - int myswitchtype; - int matchesdchan; - int x,y; - offset = 0; - if ((chan_sig == SIG_PRI) && ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &offset)) { - ast_log(LOG_ERROR, "Unable to set clear mode on clear channel %d of span %d: %s\n", channel, p.spanno, strerror(errno)); - destroy_dahdi_pvt(&tmp); - return NULL; - } - if (span >= NUM_SPANS) { - ast_log(LOG_ERROR, "Channel %d does not lie on a span I know of (%d)\n", channel, span); - destroy_dahdi_pvt(&tmp); - return NULL; - } else { - struct dahdi_spaninfo si; - si.spanno = 0; - if (ioctl(tmp->subs[SUB_REAL].dfd,DAHDI_SPANSTAT,&si) == -1) { - ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno)); + prev = tmp2; + tmp2 = tmp2->next; + } + + if (!here && reloading != 1) { + if (!(tmp = ast_calloc(1, sizeof(*tmp)))) { + if (tmp) + free(tmp); + return NULL; + } + ast_mutex_init(&tmp->lock); + ifcount++; + for (x = 0; x < 3; x++) + tmp->subs[x].dfd = -1; + tmp->channel = channel; + } + + if (tmp) { + int chan_sig = conf->chan.sig; + if (!here) { + if ((channel != CHAN_PSEUDO) && !pri) { + int count = 0; + snprintf(fn, sizeof(fn), "%d", channel); + /* Open non-blocking */ + tmp->subs[SUB_REAL].dfd = dahdi_open(fn); + while (tmp->subs[SUB_REAL].dfd < 0 && reloading == 2 && count < 1000) { /* the kernel may not call dahdi_release fast enough for the open flagbit to be cleared in time */ + usleep(1); + tmp->subs[SUB_REAL].dfd = dahdi_open(fn); + count++; + } + /* Allocate a DAHDI structure */ + if (tmp->subs[SUB_REAL].dfd < 0) { + ast_log(LOG_ERROR, "Unable to open channel %d: %s\nhere = %d, tmp->channel = %d, channel = %d\n", channel, strerror(errno), here, tmp->channel, channel); destroy_dahdi_pvt(&tmp); return NULL; } - /* Store the logical span first based upon the real span */ - tmp->logicalspan = pris[span].prilogicalspan; - pri_resolve_span(&span, channel, (channel - p.chanpos), &si); - if (span < 0) { - ast_log(LOG_WARNING, "Channel %d: Unable to find locate channel/trunk group!\n", channel); + memset(&p, 0, sizeof(p)); + res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &p); + if (res < 0) { + ast_log(LOG_ERROR, "Unable to get parameters: %s\n", strerror(errno)); destroy_dahdi_pvt(&tmp); return NULL; } - if (chan_sig == SIG_PRI) - myswitchtype = conf->pri.switchtype; - else - myswitchtype = PRI_SWITCH_GR303_TMC; - /* Make sure this isn't a d-channel */ - matchesdchan=0; - for (x = 0; x < NUM_SPANS; x++) { - for (y = 0; y < NUM_DCHANS; y++) { - if (pris[x].dchannels[y] == tmp->channel) { - matchesdchan = 1; - break; - } - } + if (p.sigtype != (conf->chan.sig & 0x3ffff)) { + ast_log(LOG_ERROR, "Signalling requested on channel %d is %s but line is in %s signalling\n", channel, sig2str(conf->chan.sig), sig2str(p.sigtype)); + destroy_dahdi_pvt(&tmp); + return NULL; } - offset = p.chanpos; - if (!matchesdchan) { - if (pris[span].nodetype && (pris[span].nodetype != conf->pri.nodetype)) { - ast_log(LOG_ERROR, "Span %d is already a %s node\n", span + 1, pri_node2str(pris[span].nodetype)); - destroy_dahdi_pvt(&tmp); - return NULL; - } - if (pris[span].switchtype && (pris[span].switchtype != myswitchtype)) { - ast_log(LOG_ERROR, "Span %d is already a %s switch\n", span + 1, pri_switch2str(pris[span].switchtype)); - destroy_dahdi_pvt(&tmp); - return NULL; - } - if ((pris[span].dialplan) && (pris[span].dialplan != conf->pri.dialplan)) { - ast_log(LOG_ERROR, "Span %d is already a %s dialing plan\n", span + 1, dialplan2str(pris[span].dialplan)); - destroy_dahdi_pvt(&tmp); - return NULL; - } - if (!ast_strlen_zero(pris[span].idledial) && strcmp(pris[span].idledial, conf->pri.idledial)) { - ast_log(LOG_ERROR, "Span %d already has idledial '%s'.\n", span + 1, conf->pri.idledial); - destroy_dahdi_pvt(&tmp); - return NULL; - } - if (!ast_strlen_zero(pris[span].idleext) && strcmp(pris[span].idleext, conf->pri.idleext)) { - ast_log(LOG_ERROR, "Span %d already has idleext '%s'.\n", span + 1, conf->pri.idleext); + tmp->law = p.curlaw; + tmp->span = p.spanno; + span = p.spanno - 1; + } else { + if (channel == CHAN_PSEUDO) + chan_sig = 0; + else if ((chan_sig != SIG_FXOKS) && (chan_sig != SIG_FXSKS)) { + ast_log(LOG_ERROR, "CRV's must use FXO/FXS Kewl Start (fxo_ks/fxs_ks) signalling only.\n"); + return NULL; + } + } + #ifdef HAVE_PRI + if ((chan_sig == SIG_PRI) || (chan_sig == SIG_GR303FXOKS) || (chan_sig == SIG_GR303FXSKS)) { + int offset; + int myswitchtype; + int matchesdchan; + int x,y; + offset = 0; + if ((chan_sig == SIG_PRI) && ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &offset)) { + ast_log(LOG_ERROR, "Unable to set clear mode on clear channel %d of span %d: %s\n", channel, p.spanno, strerror(errno)); + destroy_dahdi_pvt(&tmp); + return NULL; + } + if (span >= NUM_SPANS) { + ast_log(LOG_ERROR, "Channel %d does not lie on a span I know of (%d)\n", channel, span); + destroy_dahdi_pvt(&tmp); + return NULL; + } else { + struct dahdi_spaninfo si; + si.spanno = 0; + if (ioctl(tmp->subs[SUB_REAL].dfd,DAHDI_SPANSTAT,&si) == -1) { + ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno)); destroy_dahdi_pvt(&tmp); return NULL; } - if (pris[span].minunused && (pris[span].minunused != conf->pri.minunused)) { - ast_log(LOG_ERROR, "Span %d already has minunused of %d.\n", span + 1, conf->pri.minunused); + /* Store the logical span first based upon the real span */ + tmp->logicalspan = pris[span].prilogicalspan; + pri_resolve_span(&span, channel, (channel - p.chanpos), &si); + if (span < 0) { + ast_log(LOG_WARNING, "Channel %d: Unable to find locate channel/trunk group!\n", channel); destroy_dahdi_pvt(&tmp); return NULL; } - if (pris[span].minidle && (pris[span].minidle != conf->pri.minidle)) { - ast_log(LOG_ERROR, "Span %d already has minidle of %d.\n", span + 1, conf->pri.minidle); - destroy_dahdi_pvt(&tmp); - return NULL; + if (chan_sig == SIG_PRI) + myswitchtype = conf->pri.switchtype; + else + myswitchtype = PRI_SWITCH_GR303_TMC; + /* Make sure this isn't a d-channel */ + matchesdchan=0; + for (x = 0; x < NUM_SPANS; x++) { + for (y = 0; y < NUM_DCHANS; y++) { + if (pris[x].dchannels[y] == tmp->channel) { + matchesdchan = 1; + break; + } + } } - if (pris[span].numchans >= MAX_CHANNELS) { - ast_log(LOG_ERROR, "Unable to add channel %d: Too many channels in trunk group %d!\n", channel, - pris[span].trunkgroup); + offset = p.chanpos; + if (!matchesdchan) { + if (pris[span].nodetype && (pris[span].nodetype != conf->pri.nodetype)) { + ast_log(LOG_ERROR, "Span %d is already a %s node\n", span + 1, pri_node2str(pris[span].nodetype)); + destroy_dahdi_pvt(&tmp); + return NULL; + } + if (pris[span].switchtype && (pris[span].switchtype != myswitchtype)) { + ast_log(LOG_ERROR, "Span %d is already a %s switch\n", span + 1, pri_switch2str(pris[span].switchtype)); + destroy_dahdi_pvt(&tmp); + return NULL; + } + if ((pris[span].dialplan) && (pris[span].dialplan != conf->pri.dialplan)) { + ast_log(LOG_ERROR, "Span %d is already a %s dialing plan\n", span + 1, dialplan2str(pris[span].dialplan)); + destroy_dahdi_pvt(&tmp); + return NULL; + } + if (!ast_strlen_zero(pris[span].idledial) && strcmp(pris[span].idledial, conf->pri.idledial)) { + ast_log(LOG_ERROR, "Span %d already has idledial '%s'.\n", span + 1, conf->pri.idledial); + destroy_dahdi_pvt(&tmp); + return NULL; + } + if (!ast_strlen_zero(pris[span].idleext) && strcmp(pris[span].idleext, conf->pri.idleext)) { + ast_log(LOG_ERROR, "Span %d already has idleext '%s'.\n", span + 1, conf->pri.idleext); + destroy_dahdi_pvt(&tmp); + return NULL; + } + if (pris[span].minunused && (pris[span].minunused != conf->pri.minunused)) { + ast_log(LOG_ERROR, "Span %d already has minunused of %d.\n", span + 1, conf->pri.minunused); + destroy_dahdi_pvt(&tmp); + return NULL; + } + if (pris[span].minidle && (pris[span].minidle != conf->pri.minidle)) { + ast_log(LOG_ERROR, "Span %d already has minidle of %d.\n", span + 1, conf->pri.minidle); + destroy_dahdi_pvt(&tmp); + return NULL; + } + if (pris[span].numchans >= MAX_CHANNELS) { + ast_log(LOG_ERROR, "Unable to add channel %d: Too many channels in trunk group %d!\n", channel, + pris[span].trunkgroup); + destroy_dahdi_pvt(&tmp); + return NULL; + } + pris[span].nodetype = conf->pri.nodetype; + pris[span].switchtype = myswitchtype; + pris[span].nsf = conf->pri.nsf; + pris[span].dialplan = conf->pri.dialplan; + pris[span].localdialplan = conf->pri.localdialplan; + pris[span].h324musellc = conf->pri.h324musellc; + pris[span].pvts[pris[span].numchans++] = tmp; + pris[span].minunused = conf->pri.minunused; + pris[span].minidle = conf->pri.minidle; + pris[span].overlapdial = conf->pri.overlapdial; + #ifdef HAVE_PRI_INBANDDISCONNECT + pris[span].inbanddisconnect = conf->pri.inbanddisconnect; + #endif + pris[span].facilityenable = conf->pri.facilityenable; + ast_copy_string(pris[span].idledial, conf->pri.idledial, sizeof(pris[span].idledial)); + ast_copy_string(pris[span].idleext, conf->pri.idleext, sizeof(pris[span].idleext)); + ast_copy_string(pris[span].internationalprefix, conf->pri.internationalprefix, sizeof(pris[span].internationalprefix)); + ast_copy_string(pris[span].nationalprefix, conf->pri.nationalprefix, sizeof(pris[span].nationalprefix)); + ast_copy_string(pris[span].localprefix, conf->pri.localprefix, sizeof(pris[span].localprefix)); + ast_copy_string(pris[span].privateprefix, conf->pri.privateprefix, sizeof(pris[span].privateprefix)); + ast_copy_string(pris[span].unknownprefix, conf->pri.unknownprefix, sizeof(pris[span].unknownprefix)); + pris[span].resetinterval = conf->pri.resetinterval; + + tmp->pri = &pris[span]; + tmp->prioffset = offset; + tmp->call = NULL; + } else { + ast_log(LOG_ERROR, "Channel %d is reserved for D-channel.\n", offset); destroy_dahdi_pvt(&tmp); return NULL; } - pris[span].nodetype = conf->pri.nodetype; - pris[span].switchtype = myswitchtype; - pris[span].nsf = conf->pri.nsf; - pris[span].dialplan = conf->pri.dialplan; - pris[span].localdialplan = conf->pri.localdialplan; - pris[span].pvts[pris[span].numchans++] = tmp; - pris[span].minunused = conf->pri.minunused; - pris[span].minidle = conf->pri.minidle; - pris[span].overlapdial = conf->pri.overlapdial; -#ifdef HAVE_PRI_INBANDDISCONNECT - pris[span].inbanddisconnect = conf->pri.inbanddisconnect; -#endif - pris[span].facilityenable = conf->pri.facilityenable; - ast_copy_string(pris[span].idledial, conf->pri.idledial, sizeof(pris[span].idledial)); - ast_copy_string(pris[span].idleext, conf->pri.idleext, sizeof(pris[span].idleext)); - ast_copy_string(pris[span].internationalprefix, conf->pri.internationalprefix, sizeof(pris[span].internationalprefix)); - ast_copy_string(pris[span].nationalprefix, conf->pri.nationalprefix, sizeof(pris[span].nationalprefix)); - ast_copy_string(pris[span].localprefix, conf->pri.localprefix, sizeof(pris[span].localprefix)); - ast_copy_string(pris[span].privateprefix, conf->pri.privateprefix, sizeof(pris[span].privateprefix)); - ast_copy_string(pris[span].unknownprefix, conf->pri.unknownprefix, sizeof(pris[span].unknownprefix)); - pris[span].resetinterval = conf->pri.resetinterval; - - tmp->pri = &pris[span]; - tmp->prioffset = offset; - tmp->call = NULL; - } else { - ast_log(LOG_ERROR, "Channel %d is reserved for D-channel.\n", offset); - destroy_dahdi_pvt(&tmp); - return NULL; } + } else { + tmp->prioffset = 0; } + #endif } else { - tmp->prioffset = 0; + chan_sig = tmp->sig; + memset(&p, 0, sizeof(p)); + if (tmp->subs[SUB_REAL].dfd > -1) + res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &p); + } + /* Adjust starttime on loopstart and kewlstart trunks to reasonable values */ + switch (chan_sig) { + case SIG_FXSKS: + case SIG_FXSLS: + case SIG_EM: + case SIG_EM_E1: + case SIG_EMWINK: + case SIG_FEATD: + case SIG_FEATDMF: + case SIG_FEATDMF_TA: + case SIG_FEATB: + case SIG_E911: + case SIG_SF: + case SIG_SFWINK: + case SIG_FGC_CAMA: + case SIG_FGC_CAMAMF: + case SIG_SF_FEATD: + case SIG_SF_FEATDMF: + case SIG_SF_FEATB: + p.starttime = 250; + break; } -#endif - } else { - chan_sig = tmp->sig; - memset(&p, 0, sizeof(p)); - if (tmp->subs[SUB_REAL].dfd > -1) - res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &p); - } - /* Adjust starttime on loopstart and kewlstart trunks to reasonable values */ - switch (chan_sig) { - case SIG_FXSKS: - case SIG_FXSLS: - case SIG_EM: - case SIG_EM_E1: - case SIG_EMWINK: - case SIG_FEATD: - case SIG_FEATDMF: - case SIG_FEATDMF_TA: - case SIG_FEATB: - case SIG_E911: - case SIG_SF: - case SIG_SFWINK: - case SIG_FGC_CAMA: - case SIG_FGC_CAMAMF: - case SIG_SF_FEATD: - case SIG_SF_FEATDMF: - case SIG_SF_FEATB: - p.starttime = 250; - break; - } - - if (tmp->radio) { - /* XXX Waiting to hear back from Jim if these should be adjustable XXX */ - p.channo = channel; - p.rxwinktime = 1; - p.rxflashtime = 1; - p.starttime = 1; - p.debouncetime = 5; - } - if (!tmp->radio) { - p.channo = channel; - /* Override timing settings based on config file */ - if (conf->timing.prewinktime >= 0) - p.prewinktime = conf->timing.prewinktime; - if (conf->timing.preflashtime >= 0) - p.preflashtime = conf->timing.preflashtime; - if (conf->timing.winktime >= 0) - p.winktime = conf->timing.winktime; - if (conf->timing.flashtime >= 0) - p.flashtime = conf->timing.flashtime; - if (conf->timing.starttime >= 0) - p.starttime = conf->timing.starttime; - if (conf->timing.rxwinktime >= 0) - p.rxwinktime = conf->timing.rxwinktime; - if (conf->timing.rxflashtime >= 0) - p.rxflashtime = conf->timing.rxflashtime; - if (conf->timing.debouncetime >= 0) - p.debouncetime = conf->timing.debouncetime; - } - - /* dont set parms on a pseudo-channel (or CRV) */ - if (tmp->subs[SUB_REAL].dfd >= 0) - { - res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_SET_PARAMS, &p); - if (res < 0) { - ast_log(LOG_ERROR, "Unable to set parameters: %s\n", strerror(errno)); - destroy_dahdi_pvt(&tmp); - return NULL; + + if (tmp->radio) { + /* XXX Waiting to hear back from Jim if these should be adjustable XXX */ + p.channo = channel; + p.rxwinktime = 1; + p.rxflashtime = 1; + p.starttime = 1; + p.debouncetime = 5; + } + if (!tmp->radio) { + p.channo = channel; + /* Override timing settings based on config file */ + if (conf->timing.prewinktime >= 0) + p.prewinktime = conf->timing.prewinktime; + if (conf->timing.preflashtime >= 0) + p.preflashtime = conf->timing.preflashtime; + if (conf->timing.winktime >= 0) + p.winktime = conf->timing.winktime; + if (conf->timing.flashtime >= 0) + p.flashtime = conf->timing.flashtime; + if (conf->timing.starttime >= 0) + p.starttime = conf->timing.starttime; + if (conf->timing.rxwinktime >= 0) + p.rxwinktime = conf->timing.rxwinktime; + if (conf->timing.rxflashtime >= 0) + p.rxflashtime = conf->timing.rxflashtime; + if (conf->timing.debouncetime >= 0) + p.debouncetime = conf->timing.debouncetime; } - } -#if 1 - if (!here && (tmp->subs[SUB_REAL].dfd > -1)) { - memset(&bi, 0, sizeof(bi)); - res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_BUFINFO, &bi); - if (!res) { - bi.txbufpolicy = conf->chan.buf_policy; - bi.rxbufpolicy = conf->chan.buf_policy; - bi.numbufs = conf->chan.buf_no; - res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi); + + /* dont set parms on a pseudo-channel (or CRV) */ + if (tmp->subs[SUB_REAL].dfd >= 0) + { + res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_SET_PARAMS, &p); if (res < 0) { - ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", channel, strerror(errno)); + ast_log(LOG_ERROR, "Unable to set parameters: %s\n", strerror(errno)); + destroy_dahdi_pvt(&tmp); + return NULL; } - } else - ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", channel, strerror(errno)); - } -#endif - tmp->immediate = conf->chan.immediate; - tmp->transfertobusy = conf->chan.transfertobusy; - tmp->sig = chan_sig; - tmp->outsigmod = conf->chan.outsigmod; - tmp->ringt_base = ringt_base; - tmp->firstradio = 0; - if ((chan_sig == SIG_FXOKS) || (chan_sig == SIG_FXOLS) || (chan_sig == SIG_FXOGS)) - tmp->permcallwaiting = conf->chan.callwaiting; - else - tmp->permcallwaiting = 0; - /* Flag to destroy the channel must be cleared on new mkif. Part of changes for reload to work */ - tmp->destroy = 0; - tmp->drings = drings; - tmp->usedistinctiveringdetection = conf->chan.usedistinctiveringdetection; - tmp->callwaitingcallerid = conf->chan.callwaitingcallerid; - tmp->threewaycalling = conf->chan.threewaycalling; - tmp->adsi = conf->chan.adsi; - tmp->use_smdi = conf->chan.use_smdi; - tmp->permhidecallerid = conf->chan.hidecallerid; - tmp->callreturn = conf->chan.callreturn; - tmp->echocancel = conf->chan.echocancel; - tmp->echotraining = conf->chan.echotraining; - tmp->pulse = conf->chan.pulse; - if (tmp->echocancel) - tmp->echocanbridged = conf->chan.echocanbridged; - else { - if (conf->chan.echocanbridged) - ast_log(LOG_NOTICE, "echocancelwhenbridged requires echocancel to be enabled; ignoring\n"); - tmp->echocanbridged = 0; - } - tmp->busydetect = conf->chan.busydetect; - tmp->busycount = conf->chan.busycount; - tmp->busy_tonelength = conf->chan.busy_tonelength; - tmp->busy_quietlength = conf->chan.busy_quietlength; - tmp->callprogress = conf->chan.callprogress; - tmp->cancallforward = conf->chan.cancallforward; - tmp->dtmfrelax = conf->chan.dtmfrelax; - tmp->callwaiting = tmp->permcallwaiting; - tmp->hidecallerid = tmp->permhidecallerid; - tmp->channel = channel; - tmp->stripmsd = conf->chan.stripmsd; - tmp->use_callerid = conf->chan.use_callerid; - tmp->cid_signalling = conf->chan.cid_signalling; - tmp->cid_start = conf->chan.cid_start; - tmp->dahditrcallerid = conf->chan.dahditrcallerid; - tmp->restrictcid = conf->chan.restrictcid; - tmp->use_callingpres = conf->chan.use_callingpres; - tmp->priindication_oob = conf->chan.priindication_oob; - tmp->priexclusive = conf->chan.priexclusive; - if (tmp->usedistinctiveringdetection) { - if (!tmp->use_callerid) { - ast_log(LOG_NOTICE, "Distinctive Ring detect requires 'usecallerid' be on\n"); - tmp->use_callerid = 1; - } - } - - if (tmp->cid_signalling == CID_SIG_SMDI) { - if (!tmp->use_smdi) { - ast_log(LOG_WARNING, "SMDI callerid requires SMDI to be enabled, enabling...\n"); - tmp->use_smdi = 1; - } - } - if (tmp->use_smdi) { - tmp->smdi_iface = ast_smdi_interface_find(conf->smdi_port); - if (!(tmp->smdi_iface)) { - ast_log(LOG_ERROR, "Invalid SMDI port specfied, disabling SMDI support\n"); - tmp->use_smdi = 0; - } - } - - ast_copy_string(tmp->accountcode, conf->chan.accountcode, sizeof(tmp->accountcode)); - tmp->amaflags = conf->chan.amaflags; - if (!here) { - tmp->confno = -1; - tmp->propconfno = -1; - } - tmp->canpark = conf->chan.canpark; - tmp->transfer = conf->chan.transfer; - ast_copy_string(tmp->defcontext,conf->chan.context,sizeof(tmp->defcontext)); - ast_copy_string(tmp->language, conf->chan.language, sizeof(tmp->language)); - ast_copy_string(tmp->mohinterpret, conf->chan.mohinterpret, sizeof(tmp->mohinterpret)); - ast_copy_string(tmp->mohsuggest, conf->chan.mohsuggest, sizeof(tmp->mohsuggest)); - ast_copy_string(tmp->context, conf->chan.context, sizeof(tmp->context)); - ast_copy_string(tmp->cid_num, conf->chan.cid_num, sizeof(tmp->cid_num)); - tmp->cid_ton = 0; - ast_copy_string(tmp->cid_name, conf->chan.cid_name, sizeof(tmp->cid_name)); - ast_copy_string(tmp->mailbox, conf->chan.mailbox, sizeof(tmp->mailbox)); - tmp->msgstate = -1; - tmp->group = conf->chan.group; - tmp->callgroup = conf->chan.callgroup; - tmp->pickupgroup= conf->chan.pickupgroup; - tmp->rxgain = conf->chan.rxgain; - tmp->txgain = conf->chan.txgain; - tmp->tonezone = conf->chan.tonezone; - tmp->onhooktime = time(NULL); - if (tmp->subs[SUB_REAL].dfd > -1) { - set_actual_gain(tmp->subs[SUB_REAL].dfd, 0, tmp->rxgain, tmp->txgain, tmp->law); - if (tmp->dsp) - ast_dsp_digitmode(tmp->dsp, DSP_DIGITMODE_DTMF | tmp->dtmfrelax); - update_conf(tmp); - if (!here) { - if (chan_sig != SIG_PRI) - /* Hang it up to be sure it's good */ - dahdi_set_hook(tmp->subs[SUB_REAL].dfd, DAHDI_ONHOOK); } - ioctl(tmp->subs[SUB_REAL].dfd,DAHDI_SETTONEZONE,&tmp->tonezone); -#ifdef HAVE_PRI - /* the dchannel is down so put the channel in alarm */ - if (tmp->pri && !pri_is_up(tmp->pri)) { - tmp->inalarm = 1; + #if 1 + if (!here && (tmp->subs[SUB_REAL].dfd > -1)) { + memset(&bi, 0, sizeof(bi)); + res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_BUFINFO, &bi); + if (!res) { + bi.txbufpolicy = conf->chan.buf_policy; + bi.rxbufpolicy = conf->chan.buf_policy; + bi.numbufs = conf->chan.buf_no; + res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi); + if (res < 0) { + ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", channel, strerror(errno)); + } + } else + ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", channel, strerror(errno)); } -#endif - if ((res = get_alarms(tmp)) != DAHDI_ALARM_NONE) { - tmp->inalarm = 1; - handle_alarms(tmp, res); - } else { - /* yes, this looks strange... the unknown_alarm flag is only used to - control whether an 'alarm cleared' message gets generated when we - get an indication that the channel is no longer in alarm status. - however, the channel *could* be in an alarm status that we aren't - aware of (since get_alarms() only reports span alarms, not channel - alarms). setting this flag will cause any potential 'alarm cleared' - message to be suppressed, but if a real alarm occurs before that - happens, this flag will get cleared by it and the situation will - be normal. - */ - tmp->unknown_alarm = 1; + #endif + tmp->immediate = conf->chan.immediate; + tmp->transfertobusy = conf->chan.transfertobusy; + tmp->sig = chan_sig; + tmp->outsigmod = conf->chan.outsigmod; + tmp->ringt_base = ringt_base; + tmp->firstradio = 0; + if ((chan_sig == SIG_FXOKS) || (chan_sig == SIG_FXOLS) || (chan_sig == SIG_FXOGS)) + tmp->permcallwaiting = conf->chan.callwaiting; + else + tmp->permcallwaiting = 0; + /* Flag to destroy the channel must be cleared on new mkif. Part of changes for reload to work */ + tmp->destroy = 0; + tmp->drings = drings; + tmp->usedistinctiveringdetection = conf->chan.usedistinctiveringdetection; + tmp->callwaitingcallerid = conf->chan.callwaitingcallerid; + tmp->threewaycalling = conf->chan.threewaycalling; + tmp->adsi = conf->chan.adsi; + tmp->use_smdi = conf->chan.use_smdi; + tmp->permhidecallerid = conf->chan.hidecallerid; + tmp->callreturn = conf->chan.callreturn; + tmp->echocancel = conf->chan.echocancel; + tmp->echotraining = conf->chan.echotraining; + tmp->pulse = conf->chan.pulse; + if (tmp->echocancel) + tmp->echocanbridged = conf->chan.echocanbridged; + else { + if (conf->chan.echocanbridged) + ast_log(LOG_NOTICE, "echocancelwhenbridged requires echocancel to be enabled; ignoring\n"); + tmp->echocanbridged = 0; + } + tmp->busydetect = conf->chan.busydetect; + tmp->busycount = conf->chan.busycount; + tmp->busy_tonelength = conf->chan.busy_tonelength; + tmp->busy_quietlength = conf->chan.busy_quietlength; + tmp->callprogress = conf->chan.callprogress; + tmp->cancallforward = conf->chan.cancallforward; + tmp->dtmfrelax = conf->chan.dtmfrelax; + tmp->callwaiting = tmp->permcallwaiting; + tmp->hidecallerid = tmp->permhidecallerid; + tmp->channel = channel; + tmp->stripmsd = conf->chan.stripmsd; + tmp->use_callerid = conf->chan.use_callerid; + tmp->cid_signalling = conf->chan.cid_signalling; + tmp->cid_start = conf->chan.cid_start; + tmp->dahditrcallerid = conf->chan.dahditrcallerid; + tmp->restrictcid = conf->chan.restrictcid; + tmp->use_callingpres = conf->chan.use_callingpres; + tmp->priindication_oob = conf->chan.priindication_oob; + tmp->priexclusive = conf->chan.priexclusive; + if (tmp->usedistinctiveringdetection) { + if (!tmp->use_callerid) { + ast_log(LOG_NOTICE, "Distinctive Ring detect requires 'usecallerid' be on\n"); + tmp->use_callerid = 1; + } } - } - - tmp->polarityonanswerdelay = conf->chan.polarityonanswerdelay; - tmp->answeronpolarityswitch = conf->chan.answeronpolarityswitch; - tmp->hanguponpolarityswitch = conf->chan.hanguponpolarityswitch; - tmp->sendcalleridafter = conf->chan.sendcalleridafter; - - } - if (tmp && !here) { - /* nothing on the iflist */ - if (!*wlist) { - *wlist = tmp; - tmp->prev = NULL; - tmp->next = NULL; - *wend = tmp; - } else { - /* at least one member on the iflist */ - struct dahdi_pvt *working = *wlist; - - /* check if we maybe have to put it on the begining */ - if (working->channel > tmp->channel) { - tmp->next = *wlist; - tmp->prev = NULL; - (*wlist)->prev = tmp; + + if (tmp->cid_signalling == CID_SIG_SMDI) { + if (!tmp->use_smdi) { + ast_log(LOG_WARNING, "SMDI callerid requires SMDI to be enabled, enabling...\n"); + tmp->use_smdi = 1; + } + } + if (tmp->use_smdi) { + tmp->smdi_iface = ast_smdi_interface_find(conf->smdi_port); + if (!(tmp->smdi_iface)) { + ast_log(LOG_ERROR, "Invalid SMDI port specfied, disabling SMDI support\n"); + tmp->use_smdi = 0; + } + } + + ast_copy_string(tmp->accountcode, conf->chan.accountcode, sizeof(tmp->accountcode)); + tmp->amaflags = conf->chan.amaflags; + if (!here) { + tmp->confno = -1; + tmp->propconfno = -1; + } + tmp->canpark = conf->chan.canpark; + tmp->transfer = conf->chan.transfer; + ast_copy_string(tmp->defcontext,conf->chan.context,sizeof(tmp->defcontext)); + ast_copy_string(tmp->language, conf->chan.language, sizeof(tmp->language)); + ast_copy_string(tmp->mohinterpret, conf->chan.mohinterpret, sizeof(tmp->mohinterpret)); + ast_copy_string(tmp->mohsuggest, conf->chan.mohsuggest, sizeof(tmp->mohsuggest)); + ast_copy_string(tmp->context, conf->chan.context, sizeof(tmp->context)); + ast_copy_string(tmp->cid_num, conf->chan.cid_num, sizeof(tmp->cid_num)); + tmp->cid_ton = 0; + ast_copy_string(tmp->cid_name, conf->chan.cid_name, sizeof(tmp->cid_name)); + ast_copy_string(tmp->mailbox, conf->chan.mailbox, sizeof(tmp->mailbox)); + tmp->msgstate = -1; + tmp->group = conf->chan.group; + tmp->callgroup = conf->chan.callgroup; + tmp->pickupgroup= conf->chan.pickupgroup; + tmp->rxgain = conf->chan.rxgain; + tmp->txgain = conf->chan.txgain; + tmp->tonezone = conf->chan.tonezone; + tmp->onhooktime = time(NULL); + if (tmp->subs[SUB_REAL].dfd > -1) { + set_actual_gain(tmp->subs[SUB_REAL].dfd, 0, tmp->rxgain, tmp->txgain, tmp->law); + if (tmp->dsp) + ast_dsp_digitmode(tmp->dsp, DSP_DIGITMODE_DTMF | tmp->dtmfrelax); + update_conf(tmp); + if (!here) { + if (chan_sig != SIG_PRI) + /* Hang it up to be sure it's good */ + dahdi_set_hook(tmp->subs[SUB_REAL].dfd, DAHDI_ONHOOK); + } + ioctl(tmp->subs[SUB_REAL].dfd,DAHDI_SETTONEZONE,&tmp->tonezone); + #ifdef HAVE_PRI + /* the dchannel is down so put the channel in alarm */ + if (tmp->pri && !pri_is_up(tmp->pri)) { + tmp->inalarm = 1; + } + #endif + if ((res = get_alarms(tmp)) != DAHDI_ALARM_NONE) { + tmp->inalarm = 1; + handle_alarms(tmp, res); + } else { + /* yes, this looks strange... the unknown_alarm flag is only used to + control whether an 'alarm cleared' message gets generated when we + get an indication that the channel is no longer in alarm status. + however, the channel *could* be in an alarm status that we aren't + aware of (since get_alarms() only reports span alarms, not channel + alarms). setting this flag will cause any potential 'alarm cleared' + message to be suppressed, but if a real alarm occurs before that + happens, this flag will get cleared by it and the situation will + be normal. + */ + tmp->unknown_alarm = 1; + } + } + + tmp->polarityonanswerdelay = conf->chan.polarityonanswerdelay; + tmp->answeronpolarityswitch = conf->chan.answeronpolarityswitch; + tmp->hanguponpolarityswitch = conf->chan.hanguponpolarityswitch; + tmp->sendcalleridafter = conf->chan.sendcalleridafter; + + } + if (tmp && !here) { + /* nothing on the iflist */ + if (!*wlist) { *wlist = tmp; + tmp->prev = NULL; + tmp->next = NULL; + *wend = tmp; } else { - /* go through all the members and put the member in the right place */ - while (working) { - /* in the middle */ - if (working->next) { - if (working->channel < tmp->channel && working->next->channel > tmp->channel) { - tmp->next = working->next; - tmp->prev = working; - working->next->prev = tmp; - working->next = tmp; - break; - } - } else { - /* the last */ - if (working->channel < tmp->channel) { - working->next = tmp; - tmp->next = NULL; - tmp->prev = working; - *wend = tmp; - break; + /* at least one member on the iflist */ + struct dahdi_pvt *working = *wlist; + + /* check if we maybe have to put it on the begining */ + if (working->channel > tmp->channel) { + tmp->next = *wlist; + tmp->prev = NULL; + (*wlist)->prev = tmp; + *wlist = tmp; + } else { + /* go through all the members and put the member in the right place */ + while (working) { + /* in the middle */ + if (working->next) { + if (working->channel < tmp->channel && working->next->channel > tmp->channel) { + tmp->next = working->next; + tmp->prev = working; + working->next->prev = tmp; + working->next = tmp; + break; + } + } else { + /* the last */ + if (working->channel < tmp->channel) { + working->next = tmp; + tmp->next = NULL; + tmp->prev = working; + *wend = tmp; + break; + } } + working = working->next; } - working = working->next; } } } + return tmp; } - return tmp; -} - -static inline int available(struct dahdi_pvt *p, int channelmatch, ast_group_t groupmatch, int *busy, int *channelmatched, int *groupmatched) -{ - int res; - struct dahdi_params par; - - /* First, check group matching */ - if (groupmatch) { - if ((p->group & groupmatch) != groupmatch) - return 0; - *groupmatched = 1; - } - /* Check to see if we have a channel match */ - if (channelmatch != -1) { - if (p->channel != channelmatch) - return 0; - *channelmatched = 1; - } - /* We're at least busy at this point */ - if (busy) { - if ((p->sig == SIG_FXOKS) || (p->sig == SIG_FXOLS) || (p->sig == SIG_FXOGS)) - *busy = 1; - } - /* If do not disturb, definitely not */ - if (p->dnd) - return 0; - /* If guard time, definitely not */ - if (p->guardtime && (time(NULL) < p->guardtime)) - return 0; - - /* If no owner definitely available */ - if (!p->owner) { -#ifdef HAVE_PRI - /* Trust PRI */ - if (p->pri) { - if (p->resetting || p->call) + + static inline int available(struct dahdi_pvt *p, int channelmatch, ast_group_t groupmatch, int *busy, int *channelmatched, int *groupmatched) + { + int res; + struct dahdi_params par; + + /* First, check group matching */ + if (groupmatch) { + if ((p->group & groupmatch) != groupmatch) return 0; - else - return 1; + *groupmatched = 1; } -#endif - if (!(p->radio || (p->oprmode < 0))) - { - if (!p->sig || (p->sig == SIG_FXSLS)) - return 1; - /* Check hook state */ - if (p->subs[SUB_REAL].dfd > -1) - res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par); - else { - /* Assume not off hook on CVRS */ - res = 0; - par.rxisoffhook = 0; - } - if (res) { - ast_log(LOG_WARNING, "Unable to check hook state on channel %d: %s\n", p->channel, strerror(errno)); - } else if ((p->sig == SIG_FXSKS) || (p->sig == SIG_FXSGS)) { - /* When "onhook" that means no battery on the line, and thus - it is out of service..., if it's on a TDM card... If it's a channel - bank, there is no telling... */ - if (par.rxbits > -1) + /* Check to see if we have a channel match */ + if (channelmatch != -1) { + if (p->channel != channelmatch) + return 0; + *channelmatched = 1; + } + /* We're at least busy at this point */ + if (busy) { + if ((p->sig == SIG_FXOKS) || (p->sig == SIG_FXOLS) || (p->sig == SIG_FXOGS)) + *busy = 1; + } + /* If do not disturb, definitely not */ + if (p->dnd) + return 0; + /* If guard time, definitely not */ + if (p->guardtime && (time(NULL) < p->guardtime)) + return 0; + + /* If no owner definitely available */ + if (!p->owner) { + #ifdef HAVE_PRI + /* Trust PRI */ + if (p->pri) { + if (p->resetting || p->call) + return 0; + else return 1; - if (par.rxisoffhook) + } + #endif + if (!(p->radio || (p->oprmode < 0))) + { + if (!p->sig || (p->sig == SIG_FXSLS)) return 1; - else -#ifdef DAHDI_CHECK_HOOKSTATE + /* Check hook state */ + if (p->subs[SUB_REAL].dfd > -1) + res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par); + else { + /* Assume not off hook on CVRS */ + res = 0; + par.rxisoffhook = 0; + } + if (res) { + ast_log(LOG_WARNING, "Unable to check hook state on channel %d: %s\n", p->channel, strerror(errno)); + } else if ((p->sig == SIG_FXSKS) || (p->sig == SIG_FXSGS)) { + /* When "onhook" that means no battery on the line, and thus + it is out of service..., if it's on a TDM card... If it's a channel + bank, there is no telling... */ + if (par.rxbits > -1) + return 1; + if (par.rxisoffhook) + return 1; + else + #ifdef DAHDI_CHECK_HOOKSTATE + return 0; + #else + return 1; + #endif + } else if (par.rxisoffhook) { + ast_log(LOG_DEBUG, "Channel %d off hook, can't use\n", p->channel); + /* Not available when the other end is off hook */ return 0; -#else - return 1; -#endif - } else if (par.rxisoffhook) { - ast_log(LOG_DEBUG, "Channel %d off hook, can't use\n", p->channel); - /* Not available when the other end is off hook */ - return 0; + } } + return 1; } - return 1; - } - - /* If it's not an FXO, forget about call wait */ - if ((p->sig != SIG_FXOKS) && (p->sig != SIG_FXOLS) && (p->sig != SIG_FXOGS)) - return 0; - - if (!p->callwaiting) { - /* If they don't have call waiting enabled, then for sure they're unavailable at this point */ - return 0; - } - - if (p->subs[SUB_CALLWAIT].dfd > -1) { - /* If there is already a call waiting call, then we can't take a second one */ - return 0; - } - if ((p->owner->_state != AST_STATE_UP) && - ((p->owner->_state != AST_STATE_RINGING) || p->outgoing)) { - /* If the current call is not up, then don't allow the call */ - return 0; - } - if ((p->subs[SUB_THREEWAY].owner) && (!p->subs[SUB_THREEWAY].inthreeway)) { - /* Can't take a call wait when the three way calling hasn't been merged yet. */ - return 0; - } - /* We're cool */ - return 1; -} - -static struct dahdi_pvt *chandup(struct dahdi_pvt *src) -{ - struct dahdi_pvt *p; - struct dahdi_bufferinfo bi; - int res; + /* If it's not an FXO, forget about call wait */ + if ((p->sig != SIG_FXOKS) && (p->sig != SIG_FXOLS) && (p->sig != SIG_FXOGS)) + return 0; - if ((p = ast_malloc(sizeof(*p)))) { - memcpy(p, src, sizeof(struct dahdi_pvt)); - ast_mutex_init(&p->lock); -#ifdef HAVE_ZAPTEL - p->subs[SUB_REAL].dfd = dahdi_open("/dev/zap/pseudo"); -#else - p->subs[SUB_REAL].dfd = dahdi_open("/dev/dahdi/pseudo"); -#endif - /* Allocate a DAHDI structure */ - if (p->subs[SUB_REAL].dfd < 0) { - ast_log(LOG_ERROR, "Unable to dup channel: %s\n", strerror(errno)); - destroy_dahdi_pvt(&p); - return NULL; + if (!p->callwaiting) { + /* If they don't have call waiting enabled, then for sure they're unavailable at this point */ + return 0; } - res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_BUFINFO, &bi); - if (!res) { - bi.txbufpolicy = p->buf_policy; - bi.rxbufpolicy = p->buf_policy; - bi.numbufs = p->buf_no; - res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi); - if (res < 0) { - ast_log(LOG_WARNING, "Unable to set buffer policy on dup channel: %s\n", strerror(errno)); - } - } else - ast_log(LOG_WARNING, "Unable to check buffer policy on dup channel: %s\n", strerror(errno)); + + if (p->subs[SUB_CALLWAIT].dfd > -1) { + /* If there is already a call waiting call, then we can't take a second one */ + return 0; + } + + if ((p->owner->_state != AST_STATE_UP) && + ((p->owner->_state != AST_STATE_RINGING) || p->outgoing)) { + /* If the current call is not up, then don't allow the call */ + return 0; + } + if ((p->subs[SUB_THREEWAY].owner) && (!p->subs[SUB_THREEWAY].inthreeway)) { + /* Can't take a call wait when the three way calling hasn't been merged yet. */ + return 0; + } + /* We're cool */ + return 1; } - p->destroy = 1; - p->next = iflist; - p->prev = NULL; - iflist = p; - if (iflist->next) - iflist->next->prev = p; - return p; -} - -#ifdef HAVE_PRI -static int pri_find_empty_chan(struct dahdi_pri *pri, int backwards) -{ - int x; - if (backwards) - x = pri->numchans; - else - x = 0; - for (;;) { - if (backwards && (x < 0)) - break; - if (!backwards && (x >= pri->numchans)) - break; - if (pri->pvts[x] && !pri->pvts[x]->inalarm && !pri->pvts[x]->owner) { - ast_log(LOG_DEBUG, "Found empty available channel %d/%d\n", - pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset); - return x; + static struct dahdi_pvt *chandup(struct dahdi_pvt *src) + { + struct dahdi_pvt *p; + struct dahdi_bufferinfo bi; + int res; + + if ((p = ast_malloc(sizeof(*p)))) { + memcpy(p, src, sizeof(struct dahdi_pvt)); + ast_mutex_init(&p->lock); + #ifdef HAVE_ZAPTEL + p->subs[SUB_REAL].dfd = dahdi_open("/dev/zap/pseudo"); + #else + p->subs[SUB_REAL].dfd = dahdi_open("/dev/dahdi/pseudo"); + #endif + /* Allocate a DAHDI structure */ + if (p->subs[SUB_REAL].dfd < 0) { + ast_log(LOG_ERROR, "Unable to dup channel: %s\n", strerror(errno)); + destroy_dahdi_pvt(&p); + return NULL; + } + res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_BUFINFO, &bi); + if (!res) { + bi.txbufpolicy = p->buf_policy; + bi.rxbufpolicy = p->buf_policy; + bi.numbufs = p->buf_no; + res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi); + if (res < 0) { + ast_log(LOG_WARNING, "Unable to set buffer policy on dup channel: %s\n", strerror(errno)); + } + } else + ast_log(LOG_WARNING, "Unable to check buffer policy on dup channel: %s\n", strerror(errno)); } + p->destroy = 1; + p->next = iflist; + p->prev = NULL; + iflist = p; + if (iflist->next) + iflist->next->prev = p; + return p; + } + + + #ifdef HAVE_PRI + static int pri_find_empty_chan(struct dahdi_pri *pri, int backwards) + { + int x; if (backwards) - x--; + x = pri->numchans; else - x++; - } - return -1; -} -#endif - -static struct ast_channel *dahdi_request(const char *type, int format, void *data, int *cause) -{ - ast_group_t groupmatch = 0; - int channelmatch = -1; - int roundrobin = 0; - int callwait = 0; - int busy = 0; - struct dahdi_pvt *p; - struct ast_channel *tmp = NULL; - char *dest=NULL; - int x; - char *s; - char opt=0; - int res=0, y=0; - int backwards = 0; -#ifdef HAVE_PRI - int crv; - int bearer = -1; - int trunkgroup; - struct dahdi_pri *pri=NULL; -#endif - struct dahdi_pvt *exit, *start, *end; - ast_mutex_t *lock; - int channelmatched = 0; - int groupmatched = 0; - - /* Assume we're locking the iflock */ - lock = &iflock; - start = iflist; - end = ifend; - if (data) { - dest = ast_strdupa((char *)data); - } else { - ast_log(LOG_WARNING, "Channel requested with no data\n"); - return NULL; + x = 0; + for (;;) { + if (backwards && (x < 0)) + break; + if (!backwards && (x >= pri->numchans)) + break; + if (pri->pvts[x] && !pri->pvts[x]->inalarm && !pri->pvts[x]->owner) { + ast_log(LOG_DEBUG, "Found empty available channel %d/%d\n", + pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset); + return x; + } + if (backwards) + x--; + else + x++; + } + return -1; } - if (toupper(dest[0]) == 'G' || toupper(dest[0])=='R') { - /* Retrieve the group number */ - char *stringp=NULL; - stringp=dest + 1; - s = strsep(&stringp, "/"); - if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) { - ast_log(LOG_WARNING, "Unable to determine group for data %s\n", (char *)data); + #endif + + static struct ast_channel *dahdi_request(const char *type, int format, void *data, int *cause) + { + ast_group_t groupmatch = 0; + int channelmatch = -1; + int roundrobin = 0; + int callwait = 0; + int busy = 0; + struct dahdi_pvt *p; + struct ast_channel *tmp = NULL; + char *dest=NULL; + int x; + char *s; + char opt=0; + int res=0, y=0; + int backwards = 0; + #ifdef HAVE_PRI + int crv; + int bearer = -1; + int trunkgroup; + struct dahdi_pri *pri=NULL; + #endif + struct dahdi_pvt *exit, *start, *end; + ast_mutex_t *lock; + int channelmatched = 0; + int groupmatched = 0; + + /* Assume we're locking the iflock */ + lock = &iflock; + start = iflist; + end = ifend; + if (data) { + dest = ast_strdupa((char *)data); + } else { + ast_log(LOG_WARNING, "Channel requested with no data\n"); return NULL; } - groupmatch = ((ast_group_t) 1 << x); - if (toupper(dest[0]) == 'G') { - if (dest[0] == 'G') { - backwards = 1; - p = ifend; - } else - p = iflist; - } else { - if (dest[0] == 'R') { - backwards = 1; - p = round_robin[x]?round_robin[x]->prev:ifend; - if (!p) + if (toupper(dest[0]) == 'G' || toupper(dest[0])=='R') { + /* Retrieve the group number */ + char *stringp=NULL; + stringp=dest + 1; + s = strsep(&stringp, "/"); + if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) { + ast_log(LOG_WARNING, "Unable to determine group for data %s\n", (char *)data); + return NULL; + } + groupmatch = ((ast_group_t) 1 << x); + if (toupper(dest[0]) == 'G') { + if (dest[0] == 'G') { + backwards = 1; p = ifend; - } else { - p = round_robin[x]?round_robin[x]->next:iflist; - if (!p) + } else p = iflist; + } else { + if (dest[0] == 'R') { + backwards = 1; + p = round_robin[x]?round_robin[x]->prev:ifend; + if (!p) + p = ifend; + } else { + p = round_robin[x]?round_robin[x]->next:iflist; + if (!p) + p = iflist; + } + roundrobin = 1; } - roundrobin = 1; - } - } else { - char *stringp=NULL; - stringp=dest; - s = strsep(&stringp, "/"); - p = iflist; - if (!strcasecmp(s, "pseudo")) { - /* Special case for pseudo */ - x = CHAN_PSEUDO; - channelmatch = x; - } -#ifdef HAVE_PRI - else if ((res = sscanf(s, "%d:%d%c%d", &trunkgroup, &crv, &opt, &y)) > 1) { - if ((trunkgroup < 1) || (crv < 1)) { - ast_log(LOG_WARNING, "Unable to determine trunk group and CRV for data %s\n", (char *)data); - return NULL; - } - res--; - for (x = 0; x < NUM_SPANS; x++) { - if (pris[x].trunkgroup == trunkgroup) { - pri = pris + x; - lock = &pri->lock; - start = pri->crvs; - end = pri->crvend; - break; + } else { + char *stringp=NULL; + stringp=dest; + s = strsep(&stringp, "/"); + p = iflist; + if (!strcasecmp(s, "pseudo")) { + /* Special case for pseudo */ + x = CHAN_PSEUDO; + channelmatch = x; + } + #ifdef HAVE_PRI + else if ((res = sscanf(s, "%d:%d%c%d", &trunkgroup, &crv, &opt, &y)) > 1) { + if ((trunkgroup < 1) || (crv < 1)) { + ast_log(LOG_WARNING, "Unable to determine trunk group and CRV for data %s\n", (char *)data); + return NULL; + } + res--; + for (x = 0; x < NUM_SPANS; x++) { + if (pris[x].trunkgroup == trunkgroup) { + pri = pris + x; + lock = &pri->lock; + start = pri->crvs; + end = pri->crvend; + break; + } } + if (!pri) { + ast_log(LOG_WARNING, "Unable to find trunk group %d\n", trunkgroup); + return NULL; + } + channelmatch = crv; + p = pris[x].crvs; } - if (!pri) { - ast_log(LOG_WARNING, "Unable to find trunk group %d\n", trunkgroup); + #endif + else if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) { + ast_log(LOG_WARNING, "Unable to determine channel for data %s\n", (char *)data); return NULL; + } else { + channelmatch = x; } - channelmatch = crv; - p = pris[x].crvs; } -#endif - else if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) { - ast_log(LOG_WARNING, "Unable to determine channel for data %s\n", (char *)data); - return NULL; - } else { - channelmatch = x; - } - } - /* Search for an unowned channel */ - ast_mutex_lock(lock); - exit = p; - while (p && !tmp) { - if (roundrobin) - round_robin[x] = p; -#if 0 - ast_verbose("name = %s, %d, %d, %d\n",p->owner ? p->owner->name : "", p->channel, channelmatch, groupmatch); -#endif - - if (p && available(p, channelmatch, groupmatch, &busy, &channelmatched, &groupmatched)) { - if (option_debug) - ast_log(LOG_DEBUG, "Using channel %d\n", p->channel); - if (p->inalarm) - goto next; - - callwait = (p->owner != NULL); -#ifdef HAVE_PRI - if (pri && (p->subs[SUB_REAL].dfd < 0)) { - if (p->sig != SIG_FXSKS) { - /* Gotta find an actual channel to use for this - CRV if this isn't a callwait */ - bearer = pri_find_empty_chan(pri, 0); - if (bearer < 0) { - ast_log(LOG_NOTICE, "Out of bearer channels on span %d for call to CRV %d:%d\n", pri->span, trunkgroup, crv); - p = NULL; + /* Search for an unowned channel */ + ast_mutex_lock(lock); + exit = p; + while (p && !tmp) { + if (roundrobin) + round_robin[x] = p; + #if 0 + ast_verbose("name = %s, %d, %d, %d\n",p->owner ? p->owner->name : "", p->channel, channelmatch, groupmatch); + #endif + + if (p && available(p, channelmatch, groupmatch, &busy, &channelmatched, &groupmatched)) { + if (option_debug) + ast_log(LOG_DEBUG, "Using channel %d\n", p->channel); + if (p->inalarm) + goto next; + + callwait = (p->owner != NULL); + #ifdef HAVE_PRI + if (pri && (p->subs[SUB_REAL].dfd < 0)) { + if (p->sig != SIG_FXSKS) { + /* Gotta find an actual channel to use for this + CRV if this isn't a callwait */ + bearer = pri_find_empty_chan(pri, 0); + if (bearer < 0) { + ast_log(LOG_NOTICE, "Out of bearer channels on span %d for call to CRV %d:%d\n", pri->span, trunkgroup, crv); + p = NULL; + break; + } + pri_assign_bearer(p, pri, pri->pvts[bearer]); + } else { + if (alloc_sub(p, 0)) { + ast_log(LOG_NOTICE, "Failed to allocate place holder pseudo channel!\n"); + p = NULL; + break; + } else + ast_log(LOG_DEBUG, "Allocated placeholder pseudo channel\n"); + p->pri = pri; + } + } + #endif + if (p->channel == CHAN_PSEUDO) { + p = chandup(p); + if (!p) { break; } - pri_assign_bearer(p, pri, pri->pvts[bearer]); - } else { - if (alloc_sub(p, 0)) { - ast_log(LOG_NOTICE, "Failed to allocate place holder pseudo channel!\n"); + } + if (p->owner) { + if (alloc_sub(p, SUB_CALLWAIT)) { p = NULL; break; - } else - ast_log(LOG_DEBUG, "Allocated placeholder pseudo channel\n"); - p->pri = pri; - } - } -#endif - if (p->channel == CHAN_PSEUDO) { - p = chandup(p); - if (!p) { - break; + } } - } - if (p->owner) { - if (alloc_sub(p, SUB_CALLWAIT)) { - p = NULL; - break; + p->outgoing = 1; + tmp = dahdi_new(p, AST_STATE_RESERVED, 0, p->owner ? SUB_CALLWAIT : SUB_REAL, 0, 0, -1); + #ifdef HAVE_PRI + if (p->bearer) { + /* Log owner to bearer channel, too */ + p->bearer->owner = tmp; + } + #endif + /* Make special notes */ + if (res > 1) { + if (opt == 'c') { + /* Confirm answer */ + p->confirmanswer = 1; + } else if (opt == 'r') { + /* Distinctive ring */ + if (res < 3) + ast_log(LOG_WARNING, "Distinctive ring missing identifier in '%s'\n", (char *)data); + else + p->distinctivering = y; + } else if (opt == 'd') { + /* If this is an ISDN call, make it digital */ + p->digital = 1; + if (tmp) + tmp->transfercapability = AST_TRANS_CAP_DIGITAL; + } else { + ast_log(LOG_WARNING, "Unknown option '%c' in '%s'\n", opt, (char *)data); + } } + /* Note if the call is a call waiting call */ + if (tmp && callwait) + tmp->cdrflags |= AST_CDR_CALLWAIT; + break; } - p->outgoing = 1; - tmp = dahdi_new(p, AST_STATE_RESERVED, 0, p->owner ? SUB_CALLWAIT : SUB_REAL, 0, 0); -#ifdef HAVE_PRI - if (p->bearer) { - /* Log owner to bearer channel, too */ - p->bearer->owner = tmp; + next: + if (backwards) { + p = p->prev; + if (!p) + p = end; + } else { + p = p->next; + if (!p) + p = start; } -#endif - /* Make special notes */ - if (res > 1) { - if (opt == 'c') { - /* Confirm answer */ - p->confirmanswer = 1; - } else if (opt == 'r') { - /* Distinctive ring */ - if (res < 3) - ast_log(LOG_WARNING, "Distinctive ring missing identifier in '%s'\n", (char *)data); - else - p->distinctivering = y; - } else if (opt == 'd') { - /* If this is an ISDN call, make it digital */ - p->digital = 1; - if (tmp) - tmp->transfercapability = AST_TRANS_CAP_DIGITAL; - } else { - ast_log(LOG_WARNING, "Unknown option '%c' in '%s'\n", opt, (char *)data); - } + /* stop when you roll to the one that we started from */ + if (p == exit) + break; + } + ast_mutex_unlock(lock); + restart_monitor(); + if (callwait) + *cause = AST_CAUSE_BUSY; + else if (!tmp) { + if (channelmatched) { + if (busy) + *cause = AST_CAUSE_BUSY; + } else if (groupmatched) { + *cause = AST_CAUSE_CONGESTION; } - /* Note if the call is a call waiting call */ - if (tmp && callwait) - tmp->cdrflags |= AST_CDR_CALLWAIT; - break; } -next: - if (backwards) { - p = p->prev; - if (!p) - p = end; - } else { + + return tmp; + } + + + #ifdef HAVE_PRI + static struct dahdi_pvt *pri_find_crv(struct dahdi_pri *pri, int crv) + { + struct dahdi_pvt *p; + p = pri->crvs; + while (p) { + if (p->channel == crv) + return p; p = p->next; - if (!p) - p = start; } - /* stop when you roll to the one that we started from */ - if (p == exit) - break; + return NULL; } - ast_mutex_unlock(lock); - restart_monitor(); - if (callwait) - *cause = AST_CAUSE_BUSY; - else if (!tmp) { - if (channelmatched) { - if (busy) - *cause = AST_CAUSE_BUSY; - } else if (groupmatched) { - *cause = AST_CAUSE_CONGESTION; + + + static int pri_find_principle(struct dahdi_pri *pri, int channel) + { + int x; + int span = PRI_SPAN(channel); + int spanfd; + struct dahdi_params param; + int principle = -1; + int explicit = PRI_EXPLICIT(channel); + channel = PRI_CHANNEL(channel); + + if (!explicit) { + spanfd = pri_active_dchan_fd(pri); + if (ioctl(spanfd, DAHDI_GET_PARAMS, ¶m)) + return -1; + span = pris[param.spanno - 1].prilogicalspan; + } + + for (x = 0; x < pri->numchans; x++) { + if (pri->pvts[x] && (pri->pvts[x]->prioffset == channel) && (pri->pvts[x]->logicalspan == span)) { + principle = x; + break; + } } - } - return tmp; -} - - -#ifdef HAVE_PRI -static struct dahdi_pvt *pri_find_crv(struct dahdi_pri *pri, int crv) -{ - struct dahdi_pvt *p; - p = pri->crvs; - while (p) { - if (p->channel == crv) - return p; - p = p->next; - } - return NULL; -} - - -static int pri_find_principle(struct dahdi_pri *pri, int channel) -{ - int x; - int span = PRI_SPAN(channel); - int spanfd; - struct dahdi_params param; - int principle = -1; - int explicit = PRI_EXPLICIT(channel); - channel = PRI_CHANNEL(channel); - - if (!explicit) { - spanfd = pri_active_dchan_fd(pri); - if (ioctl(spanfd, DAHDI_GET_PARAMS, ¶m)) - return -1; - span = pris[param.spanno - 1].prilogicalspan; + return principle; } - - for (x = 0; x < pri->numchans; x++) { - if (pri->pvts[x] && (pri->pvts[x]->prioffset == channel) && (pri->pvts[x]->logicalspan == span)) { - principle = x; - break; + + static int pri_fixup_principle(struct dahdi_pri *pri, int principle, q931_call *c) + { + int x; + struct dahdi_pvt *crv; + if (!c) { + if (principle < 0) + return -1; + return principle; } - } + if ((principle > -1) && + (principle < pri->numchans) && + (pri->pvts[principle]) && + (pri->pvts[principle]->call == c)) + return principle; + /* First, check for other bearers */ + for (x = 0; x < pri->numchans; x++) { + if (!pri->pvts[x]) + continue; + if (pri->pvts[x]->call == c) { + /* Found our call */ + if (principle != x) { + struct dahdi_pvt *new = pri->pvts[principle], *old = pri->pvts[x]; - return principle; -} - -static int pri_fixup_principle(struct dahdi_pri *pri, int principle, q931_call *c) -{ - int x; - struct dahdi_pvt *crv; - if (!c) { - if (principle < 0) - return -1; - return principle; - } - if ((principle > -1) && - (principle < pri->numchans) && - (pri->pvts[principle]) && - (pri->pvts[principle]->call == c)) - return principle; - /* First, check for other bearers */ - for (x = 0; x < pri->numchans; x++) { - if (!pri->pvts[x]) - continue; - if (pri->pvts[x]->call == c) { - /* Found our call */ - if (principle != x) { - struct dahdi_pvt *new = pri->pvts[principle], *old = pri->pvts[x]; - - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Moving call from channel %d to channel %d\n", - old->channel, new->channel); - if (new->owner) { - ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n", - old->channel, new->channel, new->channel); - return -1; + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Moving call from channel %d to channel %d\n", + old->channel, new->channel); + if (new->owner) { + ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n", + old->channel, new->channel, new->channel); + return -1; + } + /* Fix it all up now */ + new->owner = old->owner; + old->owner = NULL; + if (new->owner) { + ast_string_field_build(new->owner, name, "%s/%d:%d-%d", dahdi_chan_name, pri->trunkgroup, new->channel, 1); + new->owner->tech_pvt = new; + new->owner->fds[0] = new->subs[SUB_REAL].dfd; + new->subs[SUB_REAL].owner = old->subs[SUB_REAL].owner; + old->subs[SUB_REAL].owner = NULL; + } else + ast_log(LOG_WARNING, "Whoa, there's no owner, and we're having to fix up channel %d to channel %d\n", old->channel, new->channel); + new->call = old->call; + old->call = NULL; + + /* Copy any DSP that may be present */ + new->dsp = old->dsp; + new->dsp_features = old->dsp_features; + old->dsp = NULL; + old->dsp_features = 0; + } + return principle; + } + } + /* Now check for a CRV with no bearer */ + crv = pri->crvs; + while (crv) { + if (crv->call == c) { + /* This is our match... Perform some basic checks */ + if (crv->bearer) + ast_log(LOG_WARNING, "Trying to fix up call which already has a bearer which isn't the one we think it is\n"); + else if (pri->pvts[principle]->owner) + ast_log(LOG_WARNING, "Tring to fix up a call to a bearer which already has an owner!\n"); + else { + /* Looks good. Drop the pseudo channel now, clear up the assignment, and + wakeup the potential sleeper */ + dahdi_close_sub(crv, SUB_REAL); + pri->pvts[principle]->call = crv->call; + pri_assign_bearer(crv, pri, pri->pvts[principle]); + ast_log(LOG_DEBUG, "Assigning bearer %d/%d to CRV %d:%d\n", + pri->pvts[principle]->logicalspan, pri->pvts[principle]->prioffset, + pri->trunkgroup, crv->channel); + wakeup_sub(crv, SUB_REAL, pri); } - /* Fix it all up now */ - new->owner = old->owner; - old->owner = NULL; - if (new->owner) { - ast_string_field_build(new->owner, name, "%s/%d:%d-%d", dahdi_chan_name, pri->trunkgroup, new->channel, 1); - new->owner->tech_pvt = new; - new->owner->fds[0] = new->subs[SUB_REAL].dfd; - new->subs[SUB_REAL].owner = old->subs[SUB_REAL].owner; - old->subs[SUB_REAL].owner = NULL; - } else - ast_log(LOG_WARNING, "Whoa, there's no owner, and we're having to fix up channel %d to channel %d\n", old->channel, new->channel); - new->call = old->call; - old->call = NULL; - - /* Copy any DSP that may be present */ - new->dsp = old->dsp; - new->dsp_features = old->dsp_features; - old->dsp = NULL; - old->dsp_features = 0; + return principle; } - return principle; + crv = crv->next; } + ast_log(LOG_WARNING, "Call specified, but not found?\n"); + return -1; } - /* Now check for a CRV with no bearer */ - crv = pri->crvs; - while (crv) { - if (crv->call == c) { - /* This is our match... Perform some basic checks */ - if (crv->bearer) - ast_log(LOG_WARNING, "Trying to fix up call which already has a bearer which isn't the one we think it is\n"); - else if (pri->pvts[principle]->owner) - ast_log(LOG_WARNING, "Tring to fix up a call to a bearer which already has an owner!\n"); - else { - /* Looks good. Drop the pseudo channel now, clear up the assignment, and - wakeup the potential sleeper */ - dahdi_close_sub(crv, SUB_REAL); - pri->pvts[principle]->call = crv->call; - pri_assign_bearer(crv, pri, pri->pvts[principle]); - ast_log(LOG_DEBUG, "Assigning bearer %d/%d to CRV %d:%d\n", - pri->pvts[principle]->logicalspan, pri->pvts[principle]->prioffset, - pri->trunkgroup, crv->channel); - wakeup_sub(crv, SUB_REAL, pri); + + static void *do_idle_thread(void *vchan) + { + struct ast_channel *chan = vchan; + struct dahdi_pvt *pvt = chan->tech_pvt; + struct ast_frame *f; + char ex[80]; + /* Wait up to 30 seconds for an answer */ + int newms, ms = 30000; + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Initiating idle call on channel %s\n", chan->name); + snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial); + if (ast_call(chan, ex, 0)) { + ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex); + ast_hangup(chan); + return NULL; + } + while ((newms = ast_waitfor(chan, ms)) > 0) { + f = ast_read(chan); + if (!f) { + /* Got hangup */ + break; + } + if (f->frametype == AST_FRAME_CONTROL) { + switch (f->subclass) { + case AST_CONTROL_ANSWER: + /* Launch the PBX */ + ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten)); + ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context)); + chan->priority = 1; + if (option_verbose > 3) + ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context); + ast_pbx_run(chan); + /* It's already hungup, return immediately */ + return NULL; + case AST_CONTROL_BUSY: + if (option_verbose > 3) + ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' busy, waiting...\n", chan->name); + break; + case AST_CONTROL_CONGESTION: + if (option_verbose > 3) + ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' congested, waiting...\n", chan->name); + break; + }; } - return principle; + ast_frfree(f); + ms = newms; } - crv = crv->next; - } - ast_log(LOG_WARNING, "Call specified, but not found?\n"); - return -1; -} - -static void *do_idle_thread(void *vchan) -{ - struct ast_channel *chan = vchan; - struct dahdi_pvt *pvt = chan->tech_pvt; - struct ast_frame *f; - char ex[80]; - /* Wait up to 30 seconds for an answer */ - int newms, ms = 30000; - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Initiating idle call on channel %s\n", chan->name); - snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial); - if (ast_call(chan, ex, 0)) { - ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex); + /* Hangup the channel since nothing happend */ ast_hangup(chan); return NULL; } - while ((newms = ast_waitfor(chan, ms)) > 0) { - f = ast_read(chan); - if (!f) { - /* Got hangup */ - break; - } - if (f->frametype == AST_FRAME_CONTROL) { - switch (f->subclass) { - case AST_CONTROL_ANSWER: - /* Launch the PBX */ - ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten)); - ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context)); - chan->priority = 1; - if (option_verbose > 3) - ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context); - ast_pbx_run(chan); - /* It's already hungup, return immediately */ - return NULL; - case AST_CONTROL_BUSY: - if (option_verbose > 3) - ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' busy, waiting...\n", chan->name); - break; - case AST_CONTROL_CONGESTION: - if (option_verbose > 3) - ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' congested, waiting...\n", chan->name); - break; - }; - } - ast_frfree(f); - ms = newms; - } - /* Hangup the channel since nothing happend */ - ast_hangup(chan); - return NULL; -} - -#ifndef PRI_RESTART -#error "Upgrade your libpri" -#endif -static void dahdi_pri_message(struct pri *pri, char *s) -{ - int x, y; - int dchan = -1, span = -1; - int dchancount = 0; - - if (pri) { - for (x = 0; x < NUM_SPANS; x++) { - for (y = 0; y < NUM_DCHANS; y++) { - if (pris[x].dchans[y]) - dchancount++; - - if (pris[x].dchans[y] == pri) - dchan = y; - } - if (dchan >= 0) { - span = x; - break; - } - dchancount = 0; - } - if ((dchan >= 0) && (span >= 0)) { - if (dchancount > 1) - ast_verbose("[Span %d D-Channel %d]%s", span, dchan, s); - else - ast_verbose("%s", s); - } else - ast_log(LOG_ERROR, "PRI debug error: could not find pri associated it with debug message output\n"); - } else - ast_verbose("%s", s); - - ast_mutex_lock(&pridebugfdlock); - - if (pridebugfd >= 0) - write(pridebugfd, s, strlen(s)); - - ast_mutex_unlock(&pridebugfdlock); -} - -static void dahdi_pri_error(struct pri *pri, char *s) -{ - int x, y; - int dchan = -1, span = -1; - int dchancount = 0; - - if (pri) { - for (x = 0; x < NUM_SPANS; x++) { - for (y = 0; y < NUM_DCHANS; y++) { - if (pris[x].dchans[y]) - dchancount++; - - if (pris[x].dchans[y] == pri) - dchan = y; - } - if (dchan >= 0) { - span = x; - break; + + #ifndef PRI_RESTART + #error "Upgrade your libpri" + #endif + static void dahdi_pri_message(struct pri *pri, char *s) + { + int x, y; + int dchan = -1, span = -1; + int dchancount = 0; + + if (pri) { + for (x = 0; x < NUM_SPANS; x++) { + for (y = 0; y < NUM_DCHANS; y++) { + if (pris[x].dchans[y]) + dchancount++; + + if (pris[x].dchans[y] == pri) + dchan = y; + } + if (dchan >= 0) { + span = x; + break; + } + dchancount = 0; } - dchancount = 0; - } - if ((dchan >= 0) && (span >= 0)) { - if (dchancount > 1) - ast_log(LOG_ERROR, "[Span %d D-Channel %d] PRI: %s", span, dchan, s); - else - ast_log(LOG_ERROR, "%s", s); + if ((dchan >= 0) && (span >= 0)) { + if (dchancount > 1) + ast_verbose("[Span %d D-Channel %d]%s", span, dchan, s); + else + ast_verbose("%s", s); + } else + ast_log(LOG_ERROR, "PRI debug error: could not find pri associated it with debug message output\n"); } else - ast_log(LOG_ERROR, "PRI debug error: could not find pri associated it with debug message output\n"); - } else - ast_log(LOG_ERROR, "%s", s); - - ast_mutex_lock(&pridebugfdlock); - - if (pridebugfd >= 0) - write(pridebugfd, s, strlen(s)); - - ast_mutex_unlock(&pridebugfdlock); -} - -static int pri_check_restart(struct dahdi_pri *pri) -{ - do { - pri->resetpos++; - } while ((pri->resetpos < pri->numchans) && - (!pri->pvts[pri->resetpos] || - pri->pvts[pri->resetpos]->call || - pri->pvts[pri->resetpos]->resetting)); - if (pri->resetpos < pri->numchans) { - /* Mark the channel as resetting and restart it */ - pri->pvts[pri->resetpos]->resetting = 1; - pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos])); - } else { - pri->resetting = 0; - time(&pri->lastreset); - } - return 0; -} - -static int pri_hangup_all(struct dahdi_pvt *p, struct dahdi_pri *pri) -{ - int x; - int redo; - ast_mutex_unlock(&pri->lock); - ast_mutex_lock(&p->lock); - do { - redo = 0; - for (x = 0; x < 3; x++) { - while (p->subs[x].owner && ast_mutex_trylock(&p->subs[x].owner->lock)) { - redo++; - DEADLOCK_AVOIDANCE(&p->lock); - } - if (p->subs[x].owner) { - ast_queue_hangup(p->subs[x].owner); - ast_mutex_unlock(&p->subs[x].owner->lock); - } - } - } while (redo); - ast_mutex_unlock(&p->lock); - ast_mutex_lock(&pri->lock); - return 0; -} - -static char * redirectingreason2str(int redirectingreason) -{ - switch (redirectingreason) { - case 0: - return "UNKNOWN"; - case 1: - return "BUSY"; - case 2: - return "NO_REPLY"; - case 0xF: - return "UNCONDITIONAL"; - default: - return "NOREDIRECT"; - } -} - -static void apply_plan_to_number(char *buf, size_t size, const struct dahdi_pri *pri, const char *number, const int plan) -{ - switch (plan) { - case PRI_INTERNATIONAL_ISDN: /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */ - snprintf(buf, size, "%s%s", pri->internationalprefix, number); - break; - case PRI_NATIONAL_ISDN: /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */ - snprintf(buf, size, "%s%s", pri->nationalprefix, number); - break; - case PRI_LOCAL_ISDN: /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */ - snprintf(buf, size, "%s%s", pri->localprefix, number); - break; - case PRI_PRIVATE: /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */ - snprintf(buf, size, "%s%s", pri->privateprefix, number); - break; - case PRI_UNKNOWN: /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */ - snprintf(buf, size, "%s%s", pri->unknownprefix, number); - break; - default: /* other Q.931 dialplan => don't twiddle with callingnum */ - snprintf(buf, size, "%s", number); - break; + ast_verbose("%s", s); + + ast_mutex_lock(&pridebugfdlock); + + if (pridebugfd >= 0) + write(pridebugfd, s, strlen(s)); + + ast_mutex_unlock(&pridebugfdlock); } -} - -static int dahdi_setlaw(int dfd, int law) -{ - int res; - res = ioctl(dfd, DAHDI_SETLAW, &law); - if (res) - return res; - return 0; -} - -static void *pri_dchannel(void *vpri) -{ - struct dahdi_pri *pri = vpri; - pri_event *e; - struct pollfd fds[NUM_DCHANS]; - int res; - int chanpos = 0; - int x; - int haveidles; - int activeidles; - int nextidle = -1; - struct ast_channel *c; - struct timeval tv, lowest, *next; - struct timeval lastidle = { 0, 0 }; - int doidling=0; - char *cc; - char idlen[80]; - struct ast_channel *idle; - pthread_t p; - time_t t; - int i, which=-1; - int numdchans; - int cause=0; - struct dahdi_pvt *crv; - pthread_t threadid; - pthread_attr_t attr; - char ani2str[6]; - char plancallingnum[256]; - char plancallingani[256]; - char calledtonstr[10]; - - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); - - gettimeofday(&lastidle, NULL); - if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) { - /* Need to do idle dialing, check to be sure though */ - cc = strchr(pri->idleext, '@'); - if (cc) { - *cc = '\0'; - cc++; - ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext)); -#if 0 - /* Extensions may not be loaded yet */ - if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL)) - ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext); - else -#endif - doidling = 1; + + static void dahdi_pri_error(struct pri *pri, char *s) + { + int x, y; + int dchan = -1, span = -1; + int dchancount = 0; + + if (pri) { + for (x = 0; x < NUM_SPANS; x++) { + for (y = 0; y < NUM_DCHANS; y++) { + if (pris[x].dchans[y]) + dchancount++; + + if (pris[x].dchans[y] == pri) + dchan = y; + } + if (dchan >= 0) { + span = x; + break; + } + dchancount = 0; + } + if ((dchan >= 0) && (span >= 0)) { + if (dchancount > 1) + ast_log(LOG_ERROR, "[Span %d D-Channel %d] PRI: %s", span, dchan, s); + else + ast_log(LOG_ERROR, "%s", s); + } else + ast_log(LOG_ERROR, "PRI debug error: could not find pri associated it with debug message output\n"); } else - ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext); + ast_log(LOG_ERROR, "%s", s); + + ast_mutex_lock(&pridebugfdlock); + + if (pridebugfd >= 0) + write(pridebugfd, s, strlen(s)); + + ast_mutex_unlock(&pridebugfdlock); } - for (;;) { - for (i = 0; i < NUM_DCHANS; i++) { - if (!pri->dchannels[i]) - break; - fds[i].fd = pri->fds[i]; - fds[i].events = POLLIN | POLLPRI; - fds[i].revents = 0; + + static int pri_check_restart(struct dahdi_pri *pri) + { + do { + pri->resetpos++; + } while ((pri->resetpos < pri->numchans) && + (!pri->pvts[pri->resetpos] || + pri->pvts[pri->resetpos]->call || + pri->pvts[pri->resetpos]->resetting)); + if (pri->resetpos < pri->numchans) { + /* Mark the channel as resetting and restart it */ + pri->pvts[pri->resetpos]->resetting = 1; + pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos])); + } else { + pri->resetting = 0; + time(&pri->lastreset); } - numdchans = i; - time(&t); - ast_mutex_lock(&pri->lock); - if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->resetinterval > 0)) { - if (pri->resetting && pri_is_up(pri)) { - if (pri->resetpos < 0) - pri_check_restart(pri); - } else { - if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) { - pri->resetting = 1; - pri->resetpos = -1; + return 0; + } + + static int pri_hangup_all(struct dahdi_pvt *p, struct dahdi_pri *pri) + { + int x; + int redo; + ast_mutex_unlock(&pri->lock); + ast_mutex_lock(&p->lock); + do { + redo = 0; + for (x = 0; x < 3; x++) { + while (p->subs[x].owner && ast_mutex_trylock(&p->subs[x].owner->lock)) { + redo++; + DEADLOCK_AVOIDANCE(&p->lock); + } + if (p->subs[x].owner) { + ast_queue_hangup(p->subs[x].owner); + ast_mutex_unlock(&p->subs[x].owner->lock); } } + } while (redo); + ast_mutex_unlock(&p->lock); + ast_mutex_lock(&pri->lock); + return 0; + } + + static char * redirectingreason2str(int redirectingreason) + { + switch (redirectingreason) { + case 0: + return "UNKNOWN"; + case 1: + return "BUSY"; + case 2: + return "NO_REPLY"; + case 0xF: + return "UNCONDITIONAL"; + default: + return "NOREDIRECT"; } - /* Look for any idle channels if appropriate */ - if (doidling && pri_is_up(pri)) { - nextidle = -1; - haveidles = 0; - activeidles = 0; - for (x = pri->numchans; x >= 0; x--) { - if (pri->pvts[x] && !pri->pvts[x]->owner && - !pri->pvts[x]->call) { - if (haveidles < pri->minunused) { - haveidles++; - } else if (!pri->pvts[x]->resetting) { - nextidle = x; - break; + } + + static void apply_plan_to_number(char *buf, size_t size, const struct dahdi_pri *pri, const char *number, const int plan) + { + switch (plan) { + case PRI_INTERNATIONAL_ISDN: /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */ + snprintf(buf, size, "%s%s", pri->internationalprefix, number); + break; + case PRI_NATIONAL_ISDN: /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */ + snprintf(buf, size, "%s%s", pri->nationalprefix, number); + break; + case PRI_LOCAL_ISDN: /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */ + snprintf(buf, size, "%s%s", pri->localprefix, number); + break; + case PRI_PRIVATE: /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */ + snprintf(buf, size, "%s%s", pri->privateprefix, number); + break; + case PRI_UNKNOWN: /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */ + snprintf(buf, size, "%s%s", pri->unknownprefix, number); + break; + default: /* other Q.931 dialplan => don't twiddle with callingnum */ + snprintf(buf, size, "%s", number); + break; + } + } + + static int dahdi_setlaw(int dfd, int law) + { + int res; + res = ioctl(dfd, DAHDI_SETLAW, &law); + if (res) + return res; + return 0; + } + + static void *pri_dchannel(void *vpri) + { + struct dahdi_pri *pri = vpri; + pri_event *e; + struct pollfd fds[NUM_DCHANS]; + int res; + int chanpos = 0; + int x; + int haveidles; + int activeidles; + int nextidle = -1; + struct ast_channel *c; + struct timeval tv, lowest, *next; + struct timeval lastidle = { 0, 0 }; + int doidling=0; + char *cc; + char idlen[80]; + struct ast_channel *idle; + pthread_t p; + time_t t; + int i, which=-1; + int numdchans; + int cause=0; + struct dahdi_pvt *crv; + pthread_t threadid; + pthread_attr_t attr; + char ani2str[6]; + char plancallingnum[256]; + char plancallingani[256]; + char calledtonstr[10]; + + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + + gettimeofday(&lastidle, NULL); + if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) { + /* Need to do idle dialing, check to be sure though */ + cc = strchr(pri->idleext, '@'); + if (cc) { + *cc = '\0'; + cc++; + ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext)); + #if 0 + /* Extensions may not be loaded yet */ + if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL)) + ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext); + else + #endif + doidling = 1; + } else + ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext); + } + for (;;) { + for (i = 0; i < NUM_DCHANS; i++) { + if (!pri->dchannels[i]) + break; + fds[i].fd = pri->fds[i]; + fds[i].events = POLLIN | POLLPRI; + fds[i].revents = 0; + } + numdchans = i; + time(&t); + ast_mutex_lock(&pri->lock); + if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->resetinterval > 0)) { + if (pri->resetting && pri_is_up(pri)) { + if (pri->resetpos < 0) + pri_check_restart(pri); + } else { + if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) { + pri->resetting = 1; + pri->resetpos = -1; } - } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) - activeidles++; - } - if (nextidle > -1) { - if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) { - /* Don't create a new idle call more than once per second */ - snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial); - idle = dahdi_request(dahdi_chan_name, AST_FORMAT_ULAW, idlen, &cause); - if (idle) { - pri->pvts[nextidle]->isidlecall = 1; - if (ast_pthread_create_background(&p, NULL, do_idle_thread, idle)) { - ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name); - dahdi_hangup(idle); - } - } else - ast_log(LOG_WARNING, "Unable to request channel 'DAHDI/%s' for idle call\n", idlen); - gettimeofday(&lastidle, NULL); } - } else if ((haveidles < pri->minunused) && - (activeidles > pri->minidle)) { - /* Mark something for hangup if there is something - that can be hungup */ + } + /* Look for any idle channels if appropriate */ + if (doidling && pri_is_up(pri)) { + nextidle = -1; + haveidles = 0; + activeidles = 0; for (x = pri->numchans; x >= 0; x--) { - /* find a candidate channel */ - if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) { - pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV; - haveidles++; - /* Stop if we have enough idle channels or - can't spare any more active idle ones */ - if ((haveidles >= pri->minunused) || - (activeidles <= pri->minidle)) + if (pri->pvts[x] && !pri->pvts[x]->owner && + !pri->pvts[x]->call) { + if (haveidles < pri->minunused) { + haveidles++; + } else if (!pri->pvts[x]->resetting) { + nextidle = x; break; - } + } + } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) + activeidles++; } - } - } - /* Start with reasonable max */ - lowest = ast_tv(60, 0); - for (i = 0; i < NUM_DCHANS; i++) { - /* Find lowest available d-channel */ - if (!pri->dchannels[i]) - break; - if ((next = pri_schedule_next(pri->dchans[i]))) { - /* We need relative time here */ - tv = ast_tvsub(*next, ast_tvnow()); - if (tv.tv_sec < 0) { - tv = ast_tv(0,0); - } - if (doidling || pri->resetting) { - if (tv.tv_sec > 1) { - tv = ast_tv(1, 0); + if (nextidle > -1) { + if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) { + /* Don't create a new idle call more than once per second */ + snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial); + idle = dahdi_request(dahdi_chan_name, AST_FORMAT_ULAW, idlen, &cause); + if (idle) { + pri->pvts[nextidle]->isidlecall = 1; + if (ast_pthread_create_background(&p, NULL, do_idle_thread, idle)) { + ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name); + dahdi_hangup(idle); + } + } else + ast_log(LOG_WARNING, "Unable to request channel 'DAHDI/%s' for idle call\n", idlen); + gettimeofday(&lastidle, NULL); } - } else { - if (tv.tv_sec > 60) { - tv = ast_tv(60, 0); + } else if ((haveidles < pri->minunused) && + (activeidles > pri->minidle)) { + /* Mark something for hangup if there is something + that can be hungup */ + for (x = pri->numchans; x >= 0; x--) { + /* find a candidate channel */ + if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) { + pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV; + haveidles++; + /* Stop if we have enough idle channels or + can't spare any more active idle ones */ + if ((haveidles >= pri->minunused) || + (activeidles <= pri->minidle)) + break; + } } } - } else if (doidling || pri->resetting) { - /* Make sure we stop at least once per second if we're - monitoring idle channels */ - tv = ast_tv(1,0); - } else { - /* Don't poll for more than 60 seconds */ - tv = ast_tv(60, 0); } - if (!i || ast_tvcmp(tv, lowest) < 0) { - lowest = tv; - } - } - ast_mutex_unlock(&pri->lock); - - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); - pthread_testcancel(); - e = NULL; - res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000); - pthread_testcancel(); - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); - - ast_mutex_lock(&pri->lock); - if (!res) { - for (which = 0; which < NUM_DCHANS; which++) { - if (!pri->dchans[which]) + /* Start with reasonable max */ + lowest = ast_tv(60, 0); + for (i = 0; i < NUM_DCHANS; i++) { + /* Find lowest available d-channel */ + if (!pri->dchannels[i]) break; - /* Just a timeout, run the scheduler */ - e = pri_schedule_run(pri->dchans[which]); - if (e) - break; - } - } else if (res > -1) { - for (which = 0; which < NUM_DCHANS; which++) { - if (!pri->dchans[which]) - break; - if (fds[which].revents & POLLPRI) { - /* Check for an event */ - x = 0; - res = ioctl(pri->fds[which], DAHDI_GETEVENT, &x); - if (x) - ast_log(LOG_NOTICE, "PRI got event: %s (%d) on %s D-channel of span %d\n", event2str(x), x, pri_order(which), pri->span); - /* Keep track of alarm state */ - if (x == DAHDI_EVENT_ALARM) { - pri->dchanavail[which] &= ~(DCHAN_NOTINALARM | DCHAN_UP); - pri_find_dchan(pri); - } else if (x == DAHDI_EVENT_NOALARM) { - pri->dchanavail[which] |= DCHAN_NOTINALARM; - pri_restart(pri->dchans[which]); + if ((next = pri_schedule_next(pri->dchans[i]))) { + /* We need relative time here */ + tv = ast_tvsub(*next, ast_tvnow()); + if (tv.tv_sec < 0) { + tv = ast_tv(0,0); + } + if (doidling || pri->resetting) { + if (tv.tv_sec > 1) { + tv = ast_tv(1, 0); + } + } else { + if (tv.tv_sec > 60) { + tv = ast_tv(60, 0); + } } - - if (option_debug) - ast_log(LOG_DEBUG, "Got event %s (%d) on D-channel for span %d\n", event2str(x), x, pri->span); - } else if (fds[which].revents & POLLIN) { - e = pri_check_event(pri->dchans[which]); + } else if (doidling || pri->resetting) { + /* Make sure we stop at least once per second if we're + monitoring idle channels */ + tv = ast_tv(1,0); + } else { + /* Don't poll for more than 60 seconds */ + tv = ast_tv(60, 0); + } + if (!i || ast_tvcmp(tv, lowest) < 0) { + lowest = tv; } - if (e) - break; } - } else if (errno != EINTR) - ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno)); - - if (e) { - if (pri->debug) - pri_dump_event(pri->dchans[which], e); - - if (e->e != PRI_EVENT_DCHAN_DOWN) { - if (!(pri->dchanavail[which] & DCHAN_UP)) { - if (option_verbose > 1) - ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d up\n", pri_order(which), pri->span); + ast_mutex_unlock(&pri->lock); + + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + pthread_testcancel(); + e = NULL; + res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000); + pthread_testcancel(); + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + + ast_mutex_lock(&pri->lock); + if (!res) { + for (which = 0; which < NUM_DCHANS; which++) { + if (!pri->dchans[which]) + break; + /* Just a timeout, run the scheduler */ + e = pri_schedule_run(pri->dchans[which]); + if (e) + break; } - pri->dchanavail[which] |= DCHAN_UP; - } else { - if (pri->dchanavail[which] & DCHAN_UP) { - if (option_verbose > 1) - ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d down\n", pri_order(which), pri->span); - } - pri->dchanavail[which] &= ~DCHAN_UP; - } - - if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which])) - /* Must be an NFAS group that has the secondary dchan active */ - pri->pri = pri->dchans[which]; - - switch (e->e) { - case PRI_EVENT_DCHAN_UP: - if (!pri->pri) pri_find_dchan(pri); - - /* Note presense of D-channel */ - time(&pri->lastreset); - - /* Restart in 5 seconds */ - if (pri->resetinterval > -1) { - pri->lastreset -= pri->resetinterval; - pri->lastreset += 5; - } - pri->resetting = 0; - /* Take the channels from inalarm condition */ - for (i = 0; i < pri->numchans; i++) - if (pri->pvts[i]) { - pri->pvts[i]->inalarm = 0; + } else if (res > -1) { + for (which = 0; which < NUM_DCHANS; which++) { + if (!pri->dchans[which]) + break; + if (fds[which].revents & POLLPRI) { + /* Check for an event */ + x = 0; + res = ioctl(pri->fds[which], DAHDI_GETEVENT, &x); + if (x) + ast_log(LOG_NOTICE, "PRI got event: %s (%d) on %s D-channel of span %d\n", event2str(x), x, pri_order(which), pri->span); + /* Keep track of alarm state */ + if (x == DAHDI_EVENT_ALARM) { + pri->dchanavail[which] &= ~(DCHAN_NOTINALARM | DCHAN_UP); + pri_find_dchan(pri); + } else if (x == DAHDI_EVENT_NOALARM) { + pri->dchanavail[which] |= DCHAN_NOTINALARM; + pri_restart(pri->dchans[which]); + } + + if (option_debug) + ast_log(LOG_DEBUG, "Got event %s (%d) on D-channel for span %d\n", event2str(x), x, pri->span); + } else if (fds[which].revents & POLLIN) { + e = pri_check_event(pri->dchans[which]); + } + if (e) + break; + } + } else if (errno != EINTR) + ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno)); + + if (e) { + if (pri->debug) + pri_dump_event(pri->dchans[which], e); + + if (e->e != PRI_EVENT_DCHAN_DOWN) { + if (!(pri->dchanavail[which] & DCHAN_UP)) { + if (option_verbose > 1) + ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d up\n", pri_order(which), pri->span); + } + pri->dchanavail[which] |= DCHAN_UP; + } else { + if (pri->dchanavail[which] & DCHAN_UP) { + if (option_verbose > 1) + ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d down\n", pri_order(which), pri->span); + } + pri->dchanavail[which] &= ~DCHAN_UP; + } + + if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which])) + /* Must be an NFAS group that has the secondary dchan active */ + pri->pri = pri->dchans[which]; + + switch (e->e) { + case PRI_EVENT_DCHAN_UP: + if (!pri->pri) pri_find_dchan(pri); + + /* Note presense of D-channel */ + time(&pri->lastreset); + + /* Restart in 5 seconds */ + if (pri->resetinterval > -1) { + pri->lastreset -= pri->resetinterval; + pri->lastreset += 5; } - break; - case PRI_EVENT_DCHAN_DOWN: - pri_find_dchan(pri); - if (!pri_is_up(pri)) { pri->resetting = 0; - /* Hangup active channels and put them in alarm mode */ - for (i = 0; i < pri->numchans; i++) { - struct dahdi_pvt *p = pri->pvts[i]; - if (p) { - if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) { - /* T309 is not enabled : hangup calls when alarm occurs */ - if (p->call) { - if (p->pri && p->pri->pri) { - pri_hangup(p->pri->pri, p->call, -1); - pri_destroycall(p->pri->pri, p->call); - p->call = NULL; - } else - ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n"); + /* Take the channels from inalarm condition */ + for (i = 0; i < pri->numchans; i++) + if (pri->pvts[i]) { + pri->pvts[i]->inalarm = 0; + } + break; + case PRI_EVENT_DCHAN_DOWN: + pri_find_dchan(pri); + if (!pri_is_up(pri)) { + pri->resetting = 0; + /* Hangup active channels and put them in alarm mode */ + for (i = 0; i < pri->numchans; i++) { + struct dahdi_pvt *p = pri->pvts[i]; + if (p) { + if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) { + /* T309 is not enabled : hangup calls when alarm occurs */ + if (p->call) { + if (p->pri && p->pri->pri) { + pri_hangup(p->pri->pri, p->call, -1); + pri_destroycall(p->pri->pri, p->call); + p->call = NULL; + } else + ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n"); + } + if (p->realcall) { + pri_hangup_all(p->realcall, pri); + } else if (p->owner) + p->owner->_softhangup |= AST_SOFTHANGUP_DEV; } - if (p->realcall) { - pri_hangup_all(p->realcall, pri); - } else if (p->owner) - p->owner->_softhangup |= AST_SOFTHANGUP_DEV; + p->inalarm = 1; } - p->inalarm = 1; } } - } - break; - case PRI_EVENT_RESTART: - if (e->restart.channel > -1) { - chanpos = pri_find_principle(pri, e->restart.channel); - if (chanpos < 0) - ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n", - PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span); - else { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d restarted on span %d\n", + break; + case PRI_EVENT_RESTART: + if (e->restart.channel > -1) { + chanpos = pri_find_principle(pri, e->restart.channel); + if (chanpos < 0) + ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n", PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span); - ast_mutex_lock(&pri->pvts[chanpos]->lock); - if (pri->pvts[chanpos]->call) { - pri_destroycall(pri->pri, pri->pvts[chanpos]->call); - pri->pvts[chanpos]->call = NULL; - } - /* Force soft hangup if appropriate */ - if (pri->pvts[chanpos]->realcall) - pri_hangup_all(pri->pvts[chanpos]->realcall, pri); - else if (pri->pvts[chanpos]->owner) - pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV; - ast_mutex_unlock(&pri->pvts[chanpos]->lock); - } - } else { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_2 "Restart on requested on entire span %d\n", pri->span); - for (x = 0; x < pri->numchans; x++) - if (pri->pvts[x]) { - ast_mutex_lock(&pri->pvts[x]->lock); - if (pri->pvts[x]->call) { - pri_destroycall(pri->pri, pri->pvts[x]->call); - pri->pvts[x]->call = NULL; + else { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d restarted on span %d\n", + PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span); + ast_mutex_lock(&pri->pvts[chanpos]->lock); + if (pri->pvts[chanpos]->call) { + pri_destroycall(pri->pri, pri->pvts[chanpos]->call); + pri->pvts[chanpos]->call = NULL; } + /* Force soft hangup if appropriate */ if (pri->pvts[chanpos]->realcall) pri_hangup_all(pri->pvts[chanpos]->realcall, pri); - else if (pri->pvts[x]->owner) - pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV; - ast_mutex_unlock(&pri->pvts[x]->lock); + else if (pri->pvts[chanpos]->owner) + pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV; + ast_mutex_unlock(&pri->pvts[chanpos]->lock); } - } - break; - case PRI_EVENT_KEYPAD_DIGIT: - chanpos = pri_find_principle(pri, e->digit.channel); - if (chanpos < 0) { - ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n", - PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span); - } else { - chanpos = pri_fixup_principle(pri, chanpos, e->digit.call); - if (chanpos > -1) { - ast_mutex_lock(&pri->pvts[chanpos]->lock); - /* queue DTMF frame if the PBX for this call was already started (we're forwarding KEYPAD_DIGITs further on */ - if (pri->overlapdial && pri->pvts[chanpos]->call==e->digit.call && pri->pvts[chanpos]->owner) { - /* how to do that */ - int digitlen = strlen(e->digit.digits); - char digit; - int i; - for (i = 0; i < digitlen; i++) { - digit = e->digit.digits[i]; - { - struct ast_frame f = { AST_FRAME_DTMF, digit, }; - dahdi_queue_frame(pri->pvts[chanpos], &f, pri); + } else { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_2 "Restart on requested on entire span %d\n", pri->span); + for (x = 0; x < pri->numchans; x++) + if (pri->pvts[x]) { + ast_mutex_lock(&pri->pvts[x]->lock); + if (pri->pvts[x]->call) { + pri_destroycall(pri->pri, pri->pvts[x]->call); + pri->pvts[x]->call = NULL; } + if (pri->pvts[chanpos]->realcall) + pri_hangup_all(pri->pvts[chanpos]->realcall, pri); + else if (pri->pvts[x]->owner) + pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV; + ast_mutex_unlock(&pri->pvts[x]->lock); } - } - ast_mutex_unlock(&pri->pvts[chanpos]->lock); } - } - break; - - case PRI_EVENT_INFO_RECEIVED: - chanpos = pri_find_principle(pri, e->ring.channel); - if (chanpos < 0) { - ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n", - PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span); - } else { - chanpos = pri_fixup_principle(pri, chanpos, e->ring.call); - if (chanpos > -1) { - ast_mutex_lock(&pri->pvts[chanpos]->lock); - /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */ - if (pri->overlapdial && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) { - /* how to do that */ - int digitlen = strlen(e->ring.callednum); - char digit; - int i; - for (i = 0; i < digitlen; i++) { - digit = e->ring.callednum[i]; - { - struct ast_frame f = { AST_FRAME_DTMF, digit, }; - dahdi_queue_frame(pri->pvts[chanpos], &f, pri); + break; + case PRI_EVENT_KEYPAD_DIGIT: + chanpos = pri_find_principle(pri, e->digit.channel); + if (chanpos < 0) { + ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n", + PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span); + } else { + chanpos = pri_fixup_principle(pri, chanpos, e->digit.call); + if (chanpos > -1) { + ast_mutex_lock(&pri->pvts[chanpos]->lock); + /* queue DTMF frame if the PBX for this call was already started (we're forwarding KEYPAD_DIGITs further on */ + if (pri->overlapdial && pri->pvts[chanpos]->call==e->digit.call && pri->pvts[chanpos]->owner) { + /* how to do that */ + int digitlen = strlen(e->digit.digits); + char digit; + int i; + for (i = 0; i < digitlen; i++) { + digit = e->digit.digits[i]; + { + struct ast_frame f = { AST_FRAME_DTMF, digit, }; + dahdi_queue_frame(pri->pvts[chanpos], &f, pri); + } } } + ast_mutex_unlock(&pri->pvts[chanpos]->lock); } - ast_mutex_unlock(&pri->pvts[chanpos]->lock); } - } - break; - case PRI_EVENT_RING: - crv = NULL; - if (e->ring.channel == -1) - chanpos = pri_find_empty_chan(pri, 1); - else + break; + + case PRI_EVENT_INFO_RECEIVED: chanpos = pri_find_principle(pri, e->ring.channel); - /* if no channel specified find one empty */ - if (chanpos < 0) { - ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n", - PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span); - } else { - ast_mutex_lock(&pri->pvts[chanpos]->lock); - if (pri->pvts[chanpos]->owner) { - if (pri->pvts[chanpos]->call == e->ring.call) { - ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n", - PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span); - break; - } else { - /* This is where we handle initial glare */ - ast_log(LOG_DEBUG, "Ring requested on channel %d/%d already in use or previously requested on span %d. Attempting to renegotiating channel.\n", + if (chanpos < 0) { + ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n", PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span); + } else { + chanpos = pri_fixup_principle(pri, chanpos, e->ring.call); + if (chanpos > -1) { + ast_mutex_lock(&pri->pvts[chanpos]->lock); + /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */ + if (pri->overlapdial && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) { + /* how to do that */ + int digitlen = strlen(e->ring.callednum); + char digit; + int i; + for (i = 0; i < digitlen; i++) { + digit = e->ring.callednum[i]; + { + struct ast_frame f = { AST_FRAME_DTMF, digit, }; + dahdi_queue_frame(pri->pvts[chanpos], &f, pri); + } + } + } ast_mutex_unlock(&pri->pvts[chanpos]->lock); - chanpos = -1; - } - } - if (chanpos > -1) - ast_mutex_unlock(&pri->pvts[chanpos]->lock); - } - if ((chanpos < 0) && (e->ring.flexible)) - chanpos = pri_find_empty_chan(pri, 1); - if (chanpos > -1) { - ast_mutex_lock(&pri->pvts[chanpos]->lock); - if (pri->switchtype == PRI_SWITCH_GR303_TMC) { - /* Should be safe to lock CRV AFAIK while bearer is still locked */ - crv = pri_find_crv(pri, pri_get_crv(pri->pri, e->ring.call, NULL)); - if (crv) - ast_mutex_lock(&crv->lock); - if (!crv || crv->owner) { - pri->pvts[chanpos]->call = NULL; - if (crv) { - if (crv->owner) - crv->owner->_softhangup |= AST_SOFTHANGUP_DEV; - ast_log(LOG_WARNING, "Call received for busy CRV %d on span %d\n", pri_get_crv(pri->pri, e->ring.call, NULL), pri->span); - } else - ast_log(LOG_NOTICE, "Call received for unconfigured CRV %d on span %d\n", pri_get_crv(pri->pri, e->ring.call, NULL), pri->span); - pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_INVALID_CALL_REFERENCE); - if (crv) - ast_mutex_unlock(&crv->lock); - ast_mutex_unlock(&pri->pvts[chanpos]->lock); - break; } } - pri->pvts[chanpos]->call = e->ring.call; - apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan); - if (pri->pvts[chanpos]->use_callerid) { - ast_shrink_phone_number(plancallingnum); - ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num)); -#ifdef PRI_ANI - if (!ast_strlen_zero(e->ring.callingani)) { - apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani); - ast_shrink_phone_number(plancallingani); - ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani)); - } else { - pri->pvts[chanpos]->cid_ani[0] = '\0'; - } -#endif - ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name)); - pri->pvts[chanpos]->cid_ton = e->ring.callingplan; /* this is the callingplan (TON/NPI), e->ring.callingplan>>4 would be the TON */ + break; + case PRI_EVENT_RING: + crv = NULL; + if (e->ring.channel == -1) + chanpos = pri_find_empty_chan(pri, 1); + else + chanpos = pri_find_principle(pri, e->ring.channel); + /* if no channel specified find one empty */ + if (chanpos < 0) { + ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n", + PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span); } else { - pri->pvts[chanpos]->cid_num[0] = '\0'; - pri->pvts[chanpos]->cid_ani[0] = '\0'; - pri->pvts[chanpos]->cid_name[0] = '\0'; - pri->pvts[chanpos]->cid_ton = 0; - } - apply_plan_to_number(pri->pvts[chanpos]->rdnis, sizeof(pri->pvts[chanpos]->rdnis), pri, - e->ring.redirectingnum, e->ring.callingplanrdnis); - /* If immediate=yes go to s|1 */ - if (pri->pvts[chanpos]->immediate) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of immediate=yes\n"); - pri->pvts[chanpos]->exten[0] = 's'; - pri->pvts[chanpos]->exten[1] = '\0'; - } - /* Get called number */ - else if (!ast_strlen_zero(e->ring.callednum)) { - ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten)); - ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid)); - } else if (pri->overlapdial) - pri->pvts[chanpos]->exten[0] = '\0'; - else { - /* Some PRI circuits are set up to send _no_ digits. Handle them as 's'. */ - pri->pvts[chanpos]->exten[0] = 's'; - pri->pvts[chanpos]->exten[1] = '\0'; - } - /* Set DNID on all incoming calls -- even immediate */ - if (!ast_strlen_zero(e->ring.callednum)) - ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid)); - /* No number yet, but received "sending complete"? */ - if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of Complete received\n"); - pri->pvts[chanpos]->exten[0] = 's'; - pri->pvts[chanpos]->exten[1] = '\0'; - } - /* Make sure extension exists (or in overlap dial mode, can exist) */ - if ((pri->overlapdial && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) || - ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) { - /* Setup law */ - int law; - if (pri->switchtype != PRI_SWITCH_GR303_TMC) { - /* Set to audio mode at this point */ - law = 1; - if (ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &law) == -1) - ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n", pri->pvts[chanpos]->channel, law, strerror(errno)); - } - if (e->ring.layer1 == PRI_LAYER_1_ALAW) - law = DAHDI_LAW_ALAW; - else - law = DAHDI_LAW_MULAW; - res = dahdi_setlaw(pri->pvts[chanpos]->subs[SUB_REAL].dfd, law); - if (res < 0) - ast_log(LOG_WARNING, "Unable to set law on channel %d\n", pri->pvts[chanpos]->channel); - res = set_actual_gain(pri->pvts[chanpos]->subs[SUB_REAL].dfd, 0, pri->pvts[chanpos]->rxgain, pri->pvts[chanpos]->txgain, law); - if (res < 0) - ast_log(LOG_WARNING, "Unable to set gains on channel %d\n", pri->pvts[chanpos]->channel); - if (e->ring.complete || !pri->overlapdial) { - /* Just announce proceeding */ - pri->pvts[chanpos]->proceeding = 1; - pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0); - } else { - if (pri->switchtype != PRI_SWITCH_GR303_TMC) - pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1); - else - pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1); - } - /* Get the use_callingpres state */ - pri->pvts[chanpos]->callingpres = e->ring.callingpres; - - /* Start PBX */ - if (!e->ring.complete && pri->overlapdial && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) { - /* Release the PRI lock while we create the channel */ - ast_mutex_unlock(&pri->lock); - if (crv) { - /* Set bearer and such */ - pri_assign_bearer(crv, pri, pri->pvts[chanpos]); - c = dahdi_new(crv, AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype); - pri->pvts[chanpos]->owner = &inuse; - ast_log(LOG_DEBUG, "Started up crv %d:%d on bearer channel %d\n", pri->trunkgroup, crv->channel, crv->bearer->channel); + ast_mutex_lock(&pri->pvts[chanpos]->lock); + if (pri->pvts[chanpos]->owner) { + if (pri->pvts[chanpos]->call == e->ring.call) { + ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n", + PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span); + break; } else { - c = dahdi_new(pri->pvts[chanpos], AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype); + /* This is where we handle initial glare */ + ast_log(LOG_DEBUG, "Ring requested on channel %d/%d already in use or previously requested on span %d. Attempting to renegotiating channel.\n", + PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span); + ast_mutex_unlock(&pri->pvts[chanpos]->lock); + chanpos = -1; } - + } + if (chanpos > -1) ast_mutex_unlock(&pri->pvts[chanpos]->lock); - - if (!ast_strlen_zero(e->ring.callingsubaddr)) { - pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr); + } + if ((chanpos < 0) && (e->ring.flexible)) + chanpos = pri_find_empty_chan(pri, 1); + if (chanpos > -1) { + ast_mutex_lock(&pri->pvts[chanpos]->lock); + if (pri->switchtype == PRI_SWITCH_GR303_TMC) { + /* Should be safe to lock CRV AFAIK while bearer is still locked */ + crv = pri_find_crv(pri, pri_get_crv(pri->pri, e->ring.call, NULL)); + if (crv) + ast_mutex_lock(&crv->lock); + if (!crv || crv->owner) { + pri->pvts[chanpos]->call = NULL; + if (crv) { + if (crv->owner) + crv->owner->_softhangup |= AST_SOFTHANGUP_DEV; + ast_log(LOG_WARNING, "Call received for busy CRV %d on span %d\n", pri_get_crv(pri->pri, e->ring.call, NULL), pri->span); + } else + ast_log(LOG_NOTICE, "Call received for unconfigured CRV %d on span %d\n", pri_get_crv(pri->pri, e->ring.call, NULL), pri->span); + pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_INVALID_CALL_REFERENCE); + if (crv) + ast_mutex_unlock(&crv->lock); + ast_mutex_unlock(&pri->pvts[chanpos]->lock); + break; } - if (e->ring.ani2 >= 0) { - snprintf(ani2str, 5, "%.2d", e->ring.ani2); - pbx_builtin_setvar_helper(c, "ANI2", ani2str); + } + pri->pvts[chanpos]->call = e->ring.call; + apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan); + if (pri->pvts[chanpos]->use_callerid) { + ast_shrink_phone_number(plancallingnum); + ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num)); + #ifdef PRI_ANI + if (!ast_strlen_zero(e->ring.callingani)) { + apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani); + ast_shrink_phone_number(plancallingani); + ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani)); + } else { + pri->pvts[chanpos]->cid_ani[0] = '\0'; } - -#ifdef SUPPORT_USERUSER - if (!ast_strlen_zero(e->ring.useruserinfo)) { - pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo); + #endif + ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name)); + pri->pvts[chanpos]->cid_ton = e->ring.callingplan; /* this is the callingplan (TON/NPI), e->ring.callingplan>>4 would be the TON */ + } else { + pri->pvts[chanpos]->cid_num[0] = '\0'; + pri->pvts[chanpos]->cid_ani[0] = '\0'; + pri->pvts[chanpos]->cid_name[0] = '\0'; + pri->pvts[chanpos]->cid_ton = 0; + } + apply_plan_to_number(pri->pvts[chanpos]->rdnis, sizeof(pri->pvts[chanpos]->rdnis), pri, + e->ring.redirectingnum, e->ring.callingplanrdnis); + /* If immediate=yes go to s|1 */ + if (pri->pvts[chanpos]->immediate) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of immediate=yes\n"); + pri->pvts[chanpos]->exten[0] = 's'; + pri->pvts[chanpos]->exten[1] = '\0'; + } + /* Get called number */ + else if (!ast_strlen_zero(e->ring.callednum)) { + ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten)); + ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid)); + } else if (pri->overlapdial) + pri->pvts[chanpos]->exten[0] = '\0'; + else { + /* Some PRI circuits are set up to send _no_ digits. Handle them as 's'. */ + pri->pvts[chanpos]->exten[0] = 's'; + pri->pvts[chanpos]->exten[1] = '\0'; + } + /* Set DNID on all incoming calls -- even immediate */ + if (!ast_strlen_zero(e->ring.callednum)) + ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid)); + /* No number yet, but received "sending complete"? */ + if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of Complete received\n"); + pri->pvts[chanpos]->exten[0] = 's'; + pri->pvts[chanpos]->exten[1] = '\0'; + } + /* Make sure extension exists (or in overlap dial mode, can exist) */ + if ((pri->overlapdial && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) || + ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) { + /* Setup law */ + int law; + if (pri->switchtype != PRI_SWITCH_GR303_TMC) { + /* Set to audio mode at this point */ + law = 1; + ast_log(LOG_DEBUG, "PRI_EVENT_RING: seting incoming call on channel %d to law=%d\n", pri->pvts[chanpos]->channel, law); + if (ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &law) == -1) + ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n", pri->pvts[chanpos]->channel, law, strerror(errno)); } -#endif - - snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan); - pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr); - if (e->ring.redirectingreason >= 0) - pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason)); - - ast_mutex_lock(&pri->pvts[chanpos]->lock); - ast_mutex_lock(&pri->lock); - - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - if (c && !ast_pthread_create(&threadid, &attr, ss_thread, c)) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n", - plancallingnum, S_OR(pri->pvts[chanpos]->exten, ""), - pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span); + /* note: unknown layer1 will be handled like ULAW (e.g. digital H324M calls) */ + ast_log(LOG_DEBUG, "PRI_EVENT_RING: user information layer 1 of incoming call = %d (0x%x)\n",e->ring.layer1, e->ring.layer1); + if (e->ring.layer1 == PRI_LAYER_1_ALAW) { + law = DAHDI_LAW_ALAW; + ast_log(LOG_DEBUG, "PRI_EVENT_RING: treat incoming call on channel %d as ALAW(%d)\n", pri->pvts[chanpos]->channel, law); } else { - ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n", - pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span); - if (c) - ast_hangup(c); - else { - pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION); - pri->pvts[chanpos]->call = NULL; - } + law = DAHDI_LAW_MULAW; + ast_log(LOG_DEBUG, "PRI_EVENT_RING: treat incoming call on channel %d as ULAW(%d)\n", pri->pvts[chanpos]->channel, law); } - pthread_attr_destroy(&attr); - } else { - ast_mutex_unlock(&pri->lock); - /* Release PRI lock while we create the channel */ - c = dahdi_new(pri->pvts[chanpos], AST_STATE_RING, 1, SUB_REAL, law, e->ring.ctype); - if (c) { - char calledtonstr[10]; - + ast_log(LOG_DEBUG, "PRI_EVENT_RING: seting incoming call on channel %d to law=%d\n", pri->pvts[chanpos]->channel, law); + res = dahdi_setlaw(pri->pvts[chanpos]->subs[SUB_REAL].dfd, law); + if (res < 0) + ast_log(LOG_WARNING, "Unable to set law on channel %d\n", pri->pvts[chanpos]->channel); + res = set_actual_gain(pri->pvts[chanpos]->subs[SUB_REAL].dfd, 0, pri->pvts[chanpos]->rxgain, pri->pvts[chanpos]->txgain, law); + if (res < 0) + ast_log(LOG_WARNING, "Unable to set gains on channel %d\n", pri->pvts[chanpos]->channel); + if (e->ring.complete || !pri->overlapdial) { + /* Just announce proceeding */ + pri->pvts[chanpos]->proceeding = 1; + pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0); + } else { + if (pri->switchtype != PRI_SWITCH_GR303_TMC) + pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1); + else + pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1); + } + /* Get the use_callingpres state */ + pri->pvts[chanpos]->callingpres = e->ring.callingpres; + + /* Start PBX */ + if (!e->ring.complete && pri->overlapdial && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) { + /* Release the PRI lock while we create the channel */ + ast_mutex_unlock(&pri->lock); + if (crv) { + /* Set bearer and such */ + pri_assign_bearer(crv, pri, pri->pvts[chanpos]); + c = dahdi_new(crv, AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype, e->ring.layer1); + pri->pvts[chanpos]->owner = &inuse; + ast_log(LOG_DEBUG, "Started up crv %d:%d on bearer channel %d\n", pri->trunkgroup, crv->channel, crv->bearer->channel); + } else { + c = dahdi_new(pri->pvts[chanpos], AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype, e->ring.layer1); + } + ast_mutex_unlock(&pri->pvts[chanpos]->lock); - + + if (!ast_strlen_zero(e->ring.callingsubaddr)) { + pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr); + } if (e->ring.ani2 >= 0) { - snprintf(ani2str, 5, "%d", e->ring.ani2); + snprintf(ani2str, 5, "%.2d", e->ring.ani2); pbx_builtin_setvar_helper(c, "ANI2", ani2str); } - -#ifdef SUPPORT_USERUSER + + #ifdef SUPPORT_USERUSER if (!ast_strlen_zero(e->ring.useruserinfo)) { pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo); } -#endif - + #endif + + snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan); + pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr); if (e->ring.redirectingreason >= 0) pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason)); - snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan); - pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr); - ast_mutex_lock(&pri->pvts[chanpos]->lock); ast_mutex_lock(&pri->lock); - - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n", - plancallingnum, pri->pvts[chanpos]->exten, + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + if (c && !ast_pthread_create(&threadid, &attr, ss_thread, c)) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n", + plancallingnum, S_OR(pri->pvts[chanpos]->exten, ""), pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span); - dahdi_enable_ec(pri->pvts[chanpos]); - } else { - - ast_mutex_lock(&pri->lock); - - ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n", - pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span); - pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION); - pri->pvts[chanpos]->call = NULL; + } else { + ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n", + pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span); + if (c) + ast_hangup(c); + else { + pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION); + pri->pvts[chanpos]->call = NULL; + } + } + pthread_attr_destroy(&attr); + } else { + ast_mutex_unlock(&pri->lock); + /* Release PRI lock while we create the channel */ + ast_log(LOG_DEBUG, "PRI_EVENT_RING: zt_new: law=%d,ul1=%d\n",law, e->ring.layer1); + c = dahdi_new(pri->pvts[chanpos], AST_STATE_RING, 1, SUB_REAL, law, e->ring.ctype, e->ring.layer1); + if (c) { + char calledtonstr[10]; + + ast_mutex_unlock(&pri->pvts[chanpos]->lock); + + if (e->ring.ani2 >= 0) { + snprintf(ani2str, 5, "%d", e->ring.ani2); + pbx_builtin_setvar_helper(c, "ANI2", ani2str); + } + + #ifdef SUPPORT_USERUSER + if (!ast_strlen_zero(e->ring.useruserinfo)) { + pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo); + } + #endif + + if (e->ring.redirectingreason >= 0) + pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason)); + + snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan); + pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr); + + ast_mutex_lock(&pri->pvts[chanpos]->lock); + ast_mutex_lock(&pri->lock); + + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n", + plancallingnum, pri->pvts[chanpos]->exten, + pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span); + dahdi_enable_ec(pri->pvts[chanpos]); + } else { + + ast_mutex_lock(&pri->lock); + + ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n", + pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span); + pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION); + pri->pvts[chanpos]->call = NULL; + } } + } else { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Extension '%s' in context '%s' from '%s' does not exist. Rejecting call on channel %d/%d, span %d\n", + pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan, + pri->pvts[chanpos]->prioffset, pri->span); + pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED); + pri->pvts[chanpos]->call = NULL; + pri->pvts[chanpos]->exten[0] = '\0'; } + if (crv) + ast_mutex_unlock(&crv->lock); + ast_mutex_unlock(&pri->pvts[chanpos]->lock); } else { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Extension '%s' in context '%s' from '%s' does not exist. Rejecting call on channel %d/%d, span %d\n", - pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan, - pri->pvts[chanpos]->prioffset, pri->span); - pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED); - pri->pvts[chanpos]->call = NULL; - pri->pvts[chanpos]->exten[0] = '\0'; - } - if (crv) - ast_mutex_unlock(&crv->lock); - ast_mutex_unlock(&pri->pvts[chanpos]->lock); - } else { - if (e->ring.flexible) - pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION); - else - pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL); - } - break; - case PRI_EVENT_RINGING: - chanpos = pri_find_principle(pri, e->ringing.channel); - if (chanpos < 0) { - ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n", - PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span); - } else { - chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call); + if (e->ring.flexible) + pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION); + else + pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL); + } + break; + case PRI_EVENT_RINGING: + chanpos = pri_find_principle(pri, e->ringing.channel); if (chanpos < 0) { - ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n", + ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n", PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span); } else { - ast_mutex_lock(&pri->pvts[chanpos]->lock); - if (ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) { - dahdi_enable_ec(pri->pvts[chanpos]); - pri->pvts[chanpos]->subs[SUB_REAL].needringing = 1; - pri->pvts[chanpos]->alerting = 1; - } else - ast_log(LOG_DEBUG, "Deferring ringing notification because of extra digits to dial...\n"); -#ifdef PRI_PROGRESS_MASK - if (e->ringing.progressmask & PRI_PROG_INBAND_AVAILABLE) { -#else - if (e->ringing.progress == 8) { -#endif - /* Now we can do call progress detection */ - if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) { - /* RINGING detection isn't required because we got ALERTING signal */ - ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features & ~DSP_PROGRESS_RINGING); - pri->pvts[chanpos]->dsp_features = 0; + chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call); + if (chanpos < 0) { + ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n", + PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span); + } else { + ast_mutex_lock(&pri->pvts[chanpos]->lock); + if (ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) { + dahdi_enable_ec(pri->pvts[chanpos]); + pri->pvts[chanpos]->subs[SUB_REAL].needringing = 1; + pri->pvts[chanpos]->alerting = 1; + } else + ast_log(LOG_DEBUG, "Deferring ringing notification because of extra digits to dial...\n"); + #ifdef PRI_PROGRESS_MASK + if (e->ringing.progressmask & PRI_PROG_INBAND_AVAILABLE) { + #else + if (e->ringing.progress == 8) { + #endif + /* Now we can do call progress detection */ + if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) { + /* RINGING detection isn't required because we got ALERTING signal */ + ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features & ~DSP_PROGRESS_RINGING); + pri->pvts[chanpos]->dsp_features = 0; + } } - } - -#ifdef SUPPORT_USERUSER - if (!ast_strlen_zero(e->ringing.useruserinfo)) { - struct ast_channel *owner = pri->pvts[chanpos]->owner; + + #ifdef SUPPORT_USERUSER + if (!ast_strlen_zero(e->ringing.useruserinfo)) { + struct ast_channel *owner = pri->pvts[chanpos]->owner; + ast_mutex_unlock(&pri->pvts[chanpos]->lock); + pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->ringing.useruserinfo); + ast_mutex_lock(&pri->pvts[chanpos]->lock); + } + #endif + ast_mutex_unlock(&pri->pvts[chanpos]->lock); - pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->ringing.useruserinfo); - ast_mutex_lock(&pri->pvts[chanpos]->lock); } -#endif - - ast_mutex_unlock(&pri->pvts[chanpos]->lock); } - } - break; - case PRI_EVENT_PROGRESS: - /* Get chan value if e->e is not PRI_EVNT_RINGING */ - chanpos = pri_find_principle(pri, e->proceeding.channel); - if (chanpos > -1) { -#ifdef PRI_PROGRESS_MASK - if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)) { -#else - if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progress == 8)) { -#endif - struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, }; - - if (e->proceeding.cause > -1) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with cause code %d received\n", e->proceeding.cause); - - /* Work around broken, out of spec USER_BUSY cause in a progress message */ - if (e->proceeding.cause == AST_CAUSE_USER_BUSY) { - if (pri->pvts[chanpos]->owner) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n"); - - pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause; - f.subclass = AST_CONTROL_BUSY; + break; + case PRI_EVENT_PROGRESS: + /* Get chan value if e->e is not PRI_EVNT_RINGING */ + chanpos = pri_find_principle(pri, e->proceeding.channel); + if (chanpos > -1) { + #ifdef PRI_PROGRESS_MASK + if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)) { + #else + if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progress == 8)) { + #endif + struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, }; + + if (e->proceeding.cause > -1) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with cause code %d received\n", e->proceeding.cause); + + /* Work around broken, out of spec USER_BUSY cause in a progress message */ + if (e->proceeding.cause == AST_CAUSE_USER_BUSY) { + if (pri->pvts[chanpos]->owner) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n"); + + pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause; + f.subclass = AST_CONTROL_BUSY; + } } } - } - - ast_mutex_lock(&pri->pvts[chanpos]->lock); - ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n", - pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span); - dahdi_queue_frame(pri->pvts[chanpos], &f, pri); -#ifdef PRI_PROGRESS_MASK - if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) { -#else - if (e->proceeding.progress == 8) { -#endif - /* Now we can do call progress detection */ - if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) { - ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features); - pri->pvts[chanpos]->dsp_features = 0; + + ast_mutex_lock(&pri->pvts[chanpos]->lock); + ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n", + pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span); + dahdi_queue_frame(pri->pvts[chanpos], &f, pri); + #ifdef PRI_PROGRESS_MASK + if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) { + #else + if (e->proceeding.progress == 8) { + #endif + /* Now we can do call progress detection */ + if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) { + ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features); + pri->pvts[chanpos]->dsp_features = 0; + } } + pri->pvts[chanpos]->progress = 1; + ast_mutex_unlock(&pri->pvts[chanpos]->lock); } - pri->pvts[chanpos]->progress = 1; - ast_mutex_unlock(&pri->pvts[chanpos]->lock); } - } - break; - case PRI_EVENT_PROCEEDING: - chanpos = pri_find_principle(pri, e->proceeding.channel); - if (chanpos > -1) { - if (!pri->pvts[chanpos]->proceeding) { - struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, }; - - ast_mutex_lock(&pri->pvts[chanpos]->lock); - ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n", - pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span); - dahdi_queue_frame(pri->pvts[chanpos], &f, pri); -#ifdef PRI_PROGRESS_MASK - if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) { -#else - if (e->proceeding.progress == 8) { -#endif - /* Now we can do call progress detection */ - if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) { - ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features); - pri->pvts[chanpos]->dsp_features = 0; - } - /* Bring voice path up */ - f.subclass = AST_CONTROL_PROGRESS; + break; + case PRI_EVENT_PROCEEDING: + chanpos = pri_find_principle(pri, e->proceeding.channel); + if (chanpos > -1) { + if (!pri->pvts[chanpos]->proceeding) { + struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, }; + + ast_mutex_lock(&pri->pvts[chanpos]->lock); + ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n", + pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span); dahdi_queue_frame(pri->pvts[chanpos], &f, pri); + #ifdef PRI_PROGRESS_MASK + if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) { + #else + if (e->proceeding.progress == 8) { + #endif + /* Now we can do call progress detection */ + if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) { + ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features); + pri->pvts[chanpos]->dsp_features = 0; + } + /* Bring voice path up */ + f.subclass = AST_CONTROL_PROGRESS; + dahdi_queue_frame(pri->pvts[chanpos], &f, pri); + } + pri->pvts[chanpos]->proceeding = 1; + ast_mutex_unlock(&pri->pvts[chanpos]->lock); } - pri->pvts[chanpos]->proceeding = 1; - ast_mutex_unlock(&pri->pvts[chanpos]->lock); } - } - break; - case PRI_EVENT_FACNAME: - chanpos = pri_find_principle(pri, e->facname.channel); - if (chanpos < 0) { - ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n", - PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span); - } else { - chanpos = pri_fixup_principle(pri, chanpos, e->facname.call); + break; + case PRI_EVENT_FACNAME: + chanpos = pri_find_principle(pri, e->facname.channel); if (chanpos < 0) { - ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n", + ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n", PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span); } else { - /* Re-use *69 field for PRI */ - ast_mutex_lock(&pri->pvts[chanpos]->lock); - ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num)); - ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name)); - pri->pvts[chanpos]->subs[SUB_REAL].needcallerid =1; - dahdi_enable_ec(pri->pvts[chanpos]); - ast_mutex_unlock(&pri->pvts[chanpos]->lock); + chanpos = pri_fixup_principle(pri, chanpos, e->facname.call); + if (chanpos < 0) { + ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n", + PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span); + } else { + /* Re-use *69 field for PRI */ + ast_mutex_lock(&pri->pvts[chanpos]->lock); + ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num)); + ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name)); + pri->pvts[chanpos]->subs[SUB_REAL].needcallerid =1; + dahdi_enable_ec(pri->pvts[chanpos]); + ast_mutex_unlock(&pri->pvts[chanpos]->lock); + } } - } - break; - case PRI_EVENT_ANSWER: - chanpos = pri_find_principle(pri, e->answer.channel); - if (chanpos < 0) { - ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n", - PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span); - } else { - chanpos = pri_fixup_principle(pri, chanpos, e->answer.call); + break; + case PRI_EVENT_ANSWER: + chanpos = pri_find_principle(pri, e->answer.channel); if (chanpos < 0) { - ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n", + ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n", PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span); } else { - ast_mutex_lock(&pri->pvts[chanpos]->lock); - /* Now we can do call progress detection */ - - /* We changed this so it turns on the DSP no matter what... progress or no progress. - * By this time, we need DTMF detection and other features that were previously disabled - * -- Matt F */ - if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) { - ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features); - pri->pvts[chanpos]->dsp_features = 0; - } - if (pri->pvts[chanpos]->realcall && (pri->pvts[chanpos]->realcall->sig == SIG_FXSKS)) { - ast_log(LOG_DEBUG, "Starting up GR-303 trunk now that we got CONNECT...\n"); - x = DAHDI_START; - res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_HOOK, &x); - if (res < 0) { - if (errno != EINPROGRESS) { - ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno)); - } + chanpos = pri_fixup_principle(pri, chanpos, e->answer.call); + if (chanpos < 0) { + ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n", + PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span); + } else { + ast_mutex_lock(&pri->pvts[chanpos]->lock); + /* Now we can do call progress detection */ + + /* We changed this so it turns on the DSP no matter what... progress or no progress. + * By this time, we need DTMF detection and other features that were previously disabled + * -- Matt F */ + if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) { + ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features); + pri->pvts[chanpos]->dsp_features = 0; } - } else if (!ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) { - pri->pvts[chanpos]->dialing = 1; - /* Send any "w" waited stuff */ - res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_DIAL, &pri->pvts[chanpos]->dop); - if (res < 0) { - ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", pri->pvts[chanpos]->channel, strerror(errno)); + if (pri->pvts[chanpos]->realcall && (pri->pvts[chanpos]->realcall->sig == SIG_FXSKS)) { + ast_log(LOG_DEBUG, "Starting up GR-303 trunk now that we got CONNECT...\n"); + x = DAHDI_START; + res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_HOOK, &x); + if (res < 0) { + if (errno != EINPROGRESS) { + ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno)); + } + } + } else if (!ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) { + pri->pvts[chanpos]->dialing = 1; + /* Send any "w" waited stuff */ + res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_DIAL, &pri->pvts[chanpos]->dop); + if (res < 0) { + ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", pri->pvts[chanpos]->channel, strerror(errno)); + pri->pvts[chanpos]->dop.dialstr[0] = '\0'; + } else + ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", pri->pvts[chanpos]->dop.dialstr); pri->pvts[chanpos]->dop.dialstr[0] = '\0'; - } else - ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", pri->pvts[chanpos]->dop.dialstr); - pri->pvts[chanpos]->dop.dialstr[0] = '\0'; - } else if (pri->pvts[chanpos]->confirmanswer) { - ast_log(LOG_DEBUG, "Waiting on answer confirmation on channel %d!\n", pri->pvts[chanpos]->channel); - } else { - pri->pvts[chanpos]->subs[SUB_REAL].needanswer =1; - /* Enable echo cancellation if it's not on already */ - dahdi_enable_ec(pri->pvts[chanpos]); - } - -#ifdef SUPPORT_USERUSER - if (!ast_strlen_zero(e->answer.useruserinfo)) { - struct ast_channel *owner = pri->pvts[chanpos]->owner; + } else if (pri->pvts[chanpos]->confirmanswer) { + ast_log(LOG_DEBUG, "Waiting on answer confirmation on channel %d!\n", pri->pvts[chanpos]->channel); + } else { + pri->pvts[chanpos]->subs[SUB_REAL].needanswer =1; + /* Enable echo cancellation if it's not on already */ + dahdi_enable_ec(pri->pvts[chanpos]); + } + + #ifdef SUPPORT_USERUSER + if (!ast_strlen_zero(e->answer.useruserinfo)) { + struct ast_channel *owner = pri->pvts[chanpos]->owner; + ast_mutex_unlock(&pri->pvts[chanpos]->lock); + pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->answer.useruserinfo); + ast_mutex_lock(&pri->pvts[chanpos]->lock); + } + #endif + ast_mutex_unlock(&pri->pvts[chanpos]->lock); - pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->answer.useruserinfo); - ast_mutex_lock(&pri->pvts[chanpos]->lock); } -#endif - - ast_mutex_unlock(&pri->pvts[chanpos]->lock); } - } - break; - case PRI_EVENT_HANGUP: - chanpos = pri_find_principle(pri, e->hangup.channel); - if (chanpos < 0) { - ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n", - PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); - } else { - chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call); - if (chanpos > -1) { - ast_mutex_lock(&pri->pvts[chanpos]->lock); - if (!pri->pvts[chanpos]->alreadyhungup) { - /* we're calling here dahdi_hangup so once we get there we need to clear p->call after calling pri_hangup */ - pri->pvts[chanpos]->alreadyhungup = 1; + break; + case PRI_EVENT_HANGUP: + chanpos = pri_find_principle(pri, e->hangup.channel); + if (chanpos < 0) { + ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n", + PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); + } else { + chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call); + if (chanpos > -1) { + ast_mutex_lock(&pri->pvts[chanpos]->lock); + if (!pri->pvts[chanpos]->alreadyhungup) { + /* we're calling here dahdi_hangup so once we get there we need to clear p->call after calling pri_hangup */ + pri->pvts[chanpos]->alreadyhungup = 1; + if (pri->pvts[chanpos]->realcall) + pri_hangup_all(pri->pvts[chanpos]->realcall, pri); + else if (pri->pvts[chanpos]->owner) { + /* Queue a BUSY instead of a hangup if our cause is appropriate */ + pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause; + if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP) + pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV; + else { + switch (e->hangup.cause) { + case PRI_CAUSE_USER_BUSY: + pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1; + break; + case PRI_CAUSE_CALL_REJECTED: + case PRI_CAUSE_NETWORK_OUT_OF_ORDER: + case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION: + case PRI_CAUSE_SWITCH_CONGESTION: + case PRI_CAUSE_DESTINATION_OUT_OF_ORDER: + case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE: + pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1; + break; + default: + pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV; + } + } + } + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup, cause %d\n", + pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, e->hangup.cause); + } else { + pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause); + pri->pvts[chanpos]->call = NULL; + } + if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d on span %d since channel reported in use\n", + PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); + pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos])); + pri->pvts[chanpos]->resetting = 1; + } + if (e->hangup.aoc_units > -1) + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n", + pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s"); + + #ifdef SUPPORT_USERUSER + if (pri->pvts[chanpos]->owner && !ast_strlen_zero(e->hangup.useruserinfo)) { + struct ast_channel *owner = pri->pvts[chanpos]->owner; + ast_mutex_unlock(&pri->pvts[chanpos]->lock); + pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo); + ast_mutex_lock(&pri->pvts[chanpos]->lock); + } + #endif + + ast_mutex_unlock(&pri->pvts[chanpos]->lock); + } else { + ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n", + PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); + } + } + break; + #ifndef PRI_EVENT_HANGUP_REQ + #error please update libpri + #endif + case PRI_EVENT_HANGUP_REQ: + chanpos = pri_find_principle(pri, e->hangup.channel); + if (chanpos < 0) { + ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n", + PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); + } else { + chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call); + if (chanpos > -1) { + ast_mutex_lock(&pri->pvts[chanpos]->lock); if (pri->pvts[chanpos]->realcall) pri_hangup_all(pri->pvts[chanpos]->realcall, pri); else if (pri->pvts[chanpos]->owner) { - /* Queue a BUSY instead of a hangup if our cause is appropriate */ pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause; if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP) pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV; @@ -9380,2628 +9488,2572 @@ pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV; } } + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup request, cause %d\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span, e->hangup.cause); + if (e->hangup.aoc_units > -1) + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n", + pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s"); + } else { + pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause); + pri->pvts[chanpos]->call = NULL; } - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup, cause %d\n", - pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, e->hangup.cause); - } else { - pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause); - pri->pvts[chanpos]->call = NULL; - } - if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d on span %d since channel reported in use\n", - PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); - pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos])); - pri->pvts[chanpos]->resetting = 1; - } - if (e->hangup.aoc_units > -1) - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n", - pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s"); - -#ifdef SUPPORT_USERUSER - if (pri->pvts[chanpos]->owner && !ast_strlen_zero(e->hangup.useruserinfo)) { - struct ast_channel *owner = pri->pvts[chanpos]->owner; - ast_mutex_unlock(&pri->pvts[chanpos]->lock); - pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo); - ast_mutex_lock(&pri->pvts[chanpos]->lock); - } -#endif - - ast_mutex_unlock(&pri->pvts[chanpos]->lock); - } else { - ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n", - PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); - } - } - break; -#ifndef PRI_EVENT_HANGUP_REQ -#error please update libpri -#endif - case PRI_EVENT_HANGUP_REQ: - chanpos = pri_find_principle(pri, e->hangup.channel); - if (chanpos < 0) { - ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n", - PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); - } else { - chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call); - if (chanpos > -1) { - ast_mutex_lock(&pri->pvts[chanpos]->lock); - if (pri->pvts[chanpos]->realcall) - pri_hangup_all(pri->pvts[chanpos]->realcall, pri); - else if (pri->pvts[chanpos]->owner) { - pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause; - if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP) - pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV; - else { - switch (e->hangup.cause) { - case PRI_CAUSE_USER_BUSY: - pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1; - break; - case PRI_CAUSE_CALL_REJECTED: - case PRI_CAUSE_NETWORK_OUT_OF_ORDER: - case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION: - case PRI_CAUSE_SWITCH_CONGESTION: - case PRI_CAUSE_DESTINATION_OUT_OF_ORDER: - case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE: - pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1; - break; - default: - pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV; - } - } - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup request, cause %d\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span, e->hangup.cause); - if (e->hangup.aoc_units > -1) + if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) { if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n", - pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s"); + ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d span %d since channel reported in use\n", + PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); + pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos])); + pri->pvts[chanpos]->resetting = 1; + } + + #ifdef SUPPORT_USERUSER + if (!ast_strlen_zero(e->hangup.useruserinfo)) { + struct ast_channel *owner = pri->pvts[chanpos]->owner; + ast_mutex_unlock(&pri->pvts[chanpos]->lock); + pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo); + ast_mutex_lock(&pri->pvts[chanpos]->lock); + } + #endif + + ast_mutex_unlock(&pri->pvts[chanpos]->lock); } else { - pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause); - pri->pvts[chanpos]->call = NULL; - } - if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d span %d since channel reported in use\n", - PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); - pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos])); - pri->pvts[chanpos]->resetting = 1; + ast_log(LOG_WARNING, "Hangup REQ on bad channel %d/%d on span %d\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); } - -#ifdef SUPPORT_USERUSER - if (!ast_strlen_zero(e->hangup.useruserinfo)) { - struct ast_channel *owner = pri->pvts[chanpos]->owner; - ast_mutex_unlock(&pri->pvts[chanpos]->lock); - pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo); + } + break; + case PRI_EVENT_HANGUP_ACK: + chanpos = pri_find_principle(pri, e->hangup.channel); + if (chanpos < 0) { + ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n", + PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); + } else { + chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call); + if (chanpos > -1) { ast_mutex_lock(&pri->pvts[chanpos]->lock); + pri->pvts[chanpos]->call = NULL; + pri->pvts[chanpos]->resetting = 0; + if (pri->pvts[chanpos]->owner) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup ACK\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); + } + + #ifdef SUPPORT_USERUSER + if (!ast_strlen_zero(e->hangup.useruserinfo)) { + struct ast_channel *owner = pri->pvts[chanpos]->owner; + ast_mutex_unlock(&pri->pvts[chanpos]->lock); + pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo); + ast_mutex_lock(&pri->pvts[chanpos]->lock); + } + #endif + + ast_mutex_unlock(&pri->pvts[chanpos]->lock); } -#endif - - ast_mutex_unlock(&pri->pvts[chanpos]->lock); - } else { - ast_log(LOG_WARNING, "Hangup REQ on bad channel %d/%d on span %d\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); } - } - break; - case PRI_EVENT_HANGUP_ACK: - chanpos = pri_find_principle(pri, e->hangup.channel); - if (chanpos < 0) { - ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n", - PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); - } else { - chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call); - if (chanpos > -1) { - ast_mutex_lock(&pri->pvts[chanpos]->lock); - pri->pvts[chanpos]->call = NULL; - pri->pvts[chanpos]->resetting = 0; - if (pri->pvts[chanpos]->owner) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup ACK\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span); + break; + case PRI_EVENT_CONFIG_ERR: + ast_log(LOG_WARNING, "PRI Error on span %d: %s\n", pri->trunkgroup, e->err.err); + break; + case PRI_EVENT_RESTART_ACK: + chanpos = pri_find_principle(pri, e->restartack.channel); + if (chanpos < 0) { + /* Sometime switches (e.g. I421 / British Telecom) don't give us the + channel number, so we have to figure it out... This must be why + everybody resets exactly a channel at a time. */ + for (x = 0; x < pri->numchans; x++) { + if (pri->pvts[x] && pri->pvts[x]->resetting) { + chanpos = x; + ast_mutex_lock(&pri->pvts[chanpos]->lock); + ast_log(LOG_DEBUG, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan, + pri->pvts[chanpos]->prioffset, pri->span); + if (pri->pvts[chanpos]->realcall) + pri_hangup_all(pri->pvts[chanpos]->realcall, pri); + else if (pri->pvts[chanpos]->owner) { + ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan, + pri->pvts[chanpos]->prioffset, pri->span); + pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV; + } + pri->pvts[chanpos]->resetting = 0; + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan, + pri->pvts[chanpos]->prioffset, pri->span); + ast_mutex_unlock(&pri->pvts[chanpos]->lock); + if (pri->resetting) + pri_check_restart(pri); + break; + } } - -#ifdef SUPPORT_USERUSER - if (!ast_strlen_zero(e->hangup.useruserinfo)) { - struct ast_channel *owner = pri->pvts[chanpos]->owner; - ast_mutex_unlock(&pri->pvts[chanpos]->lock); - pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo); - ast_mutex_lock(&pri->pvts[chanpos]->lock); + if (chanpos < 0) { + ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n", + PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span); } -#endif - - ast_mutex_unlock(&pri->pvts[chanpos]->lock); - } - } - break; - case PRI_EVENT_CONFIG_ERR: - ast_log(LOG_WARNING, "PRI Error on span %d: %s\n", pri->trunkgroup, e->err.err); - break; - case PRI_EVENT_RESTART_ACK: - chanpos = pri_find_principle(pri, e->restartack.channel); - if (chanpos < 0) { - /* Sometime switches (e.g. I421 / British Telecom) don't give us the - channel number, so we have to figure it out... This must be why - everybody resets exactly a channel at a time. */ - for (x = 0; x < pri->numchans; x++) { - if (pri->pvts[x] && pri->pvts[x]->resetting) { - chanpos = x; + } else { + if (pri->pvts[chanpos]) { ast_mutex_lock(&pri->pvts[chanpos]->lock); - ast_log(LOG_DEBUG, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan, - pri->pvts[chanpos]->prioffset, pri->span); if (pri->pvts[chanpos]->realcall) pri_hangup_all(pri->pvts[chanpos]->realcall, pri); else if (pri->pvts[chanpos]->owner) { - ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan, - pri->pvts[chanpos]->prioffset, pri->span); + ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n", + PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span); pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV; } pri->pvts[chanpos]->resetting = 0; if (option_verbose > 2) ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan, - pri->pvts[chanpos]->prioffset, pri->span); + pri->pvts[chanpos]->prioffset, pri->span); ast_mutex_unlock(&pri->pvts[chanpos]->lock); if (pri->resetting) pri_check_restart(pri); - break; } } + break; + case PRI_EVENT_SETUP_ACK: + chanpos = pri_find_principle(pri, e->setup_ack.channel); if (chanpos < 0) { - ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n", - PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span); - } - } else { - if (pri->pvts[chanpos]) { - ast_mutex_lock(&pri->pvts[chanpos]->lock); - if (pri->pvts[chanpos]->realcall) - pri_hangup_all(pri->pvts[chanpos]->realcall, pri); - else if (pri->pvts[chanpos]->owner) { - ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n", - PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span); - pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV; - } - pri->pvts[chanpos]->resetting = 0; - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan, - pri->pvts[chanpos]->prioffset, pri->span); - ast_mutex_unlock(&pri->pvts[chanpos]->lock); - if (pri->resetting) - pri_check_restart(pri); + ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n", + PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span); + } else { + chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call); + if (chanpos > -1) { + ast_mutex_lock(&pri->pvts[chanpos]->lock); + pri->pvts[chanpos]->setup_ack = 1; + /* Send any queued digits */ + for (x = 0;x < strlen(pri->pvts[chanpos]->dialdest); x++) { + ast_log(LOG_DEBUG, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]); + pri_information(pri->pri, pri->pvts[chanpos]->call, + pri->pvts[chanpos]->dialdest[x]); + } + ast_mutex_unlock(&pri->pvts[chanpos]->lock); + } else + ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel); } - } - break; - case PRI_EVENT_SETUP_ACK: - chanpos = pri_find_principle(pri, e->setup_ack.channel); - if (chanpos < 0) { - ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n", - PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span); - } else { - chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call); - if (chanpos > -1) { + break; + case PRI_EVENT_NOTIFY: + chanpos = pri_find_principle(pri, e->notify.channel); + if (chanpos < 0) { + ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n", + PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span); + } else { + struct ast_frame f = { AST_FRAME_CONTROL, }; ast_mutex_lock(&pri->pvts[chanpos]->lock); - pri->pvts[chanpos]->setup_ack = 1; - /* Send any queued digits */ - for (x = 0;x < strlen(pri->pvts[chanpos]->dialdest); x++) { - ast_log(LOG_DEBUG, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]); - pri_information(pri->pri, pri->pvts[chanpos]->call, - pri->pvts[chanpos]->dialdest[x]); + switch (e->notify.info) { + case PRI_NOTIFY_REMOTE_HOLD: + f.subclass = AST_CONTROL_HOLD; + dahdi_queue_frame(pri->pvts[chanpos], &f, pri); + break; + case PRI_NOTIFY_REMOTE_RETRIEVAL: + f.subclass = AST_CONTROL_UNHOLD; + dahdi_queue_frame(pri->pvts[chanpos], &f, pri); + break; } ast_mutex_unlock(&pri->pvts[chanpos]->lock); - } else - ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel); - } - break; - case PRI_EVENT_NOTIFY: - chanpos = pri_find_principle(pri, e->notify.channel); - if (chanpos < 0) { - ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n", - PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span); - } else { - struct ast_frame f = { AST_FRAME_CONTROL, }; - ast_mutex_lock(&pri->pvts[chanpos]->lock); - switch (e->notify.info) { - case PRI_NOTIFY_REMOTE_HOLD: - f.subclass = AST_CONTROL_HOLD; - dahdi_queue_frame(pri->pvts[chanpos], &f, pri); - break; - case PRI_NOTIFY_REMOTE_RETRIEVAL: - f.subclass = AST_CONTROL_UNHOLD; - dahdi_queue_frame(pri->pvts[chanpos], &f, pri); - break; } - ast_mutex_unlock(&pri->pvts[chanpos]->lock); + break; + default: + ast_log(LOG_DEBUG, "Event: %d\n", e->e); } - break; - default: - ast_log(LOG_DEBUG, "Event: %d\n", e->e); - } - } - ast_mutex_unlock(&pri->lock); - } - /* Never reached */ - return NULL; -} - -static int start_pri(struct dahdi_pri *pri) -{ - int res, x; - struct dahdi_params p; - struct dahdi_bufferinfo bi; - struct dahdi_spaninfo si; - int i; - - for (i = 0; i < NUM_DCHANS; i++) { - if (!pri->dchannels[i]) - break; -#ifdef HAVE_ZAPTEL - pri->fds[i] = open("/dev/zap/channel", O_RDWR, 0600); -#else - pri->fds[i] = open("/dev/dahdi/channel", O_RDWR, 0600); -#endif - x = pri->dchannels[i]; - if ((pri->fds[i] < 0) || (ioctl(pri->fds[i],DAHDI_SPECIFY,&x) == -1)) { - ast_log(LOG_ERROR, "Unable to open D-channel %d (%s)\n", x, strerror(errno)); - return -1; - } - res = ioctl(pri->fds[i], DAHDI_GET_PARAMS, &p); - if (res) { - dahdi_close_pri_fd(pri, i); - ast_log(LOG_ERROR, "Unable to get parameters for D-channel %d (%s)\n", x, strerror(errno)); - return -1; - } - if ((p.sigtype != DAHDI_SIG_HDLCFCS) && (p.sigtype != DAHDI_SIG_HARDHDLC)) { - dahdi_close_pri_fd(pri, i); - ast_log(LOG_ERROR, "D-channel %d is not in HDLC/FCS mode. See /etc/dahdi/system.conf\n", x); - return -1; - } - memset(&si, 0, sizeof(si)); - res = ioctl(pri->fds[i], DAHDI_SPANSTAT, &si); - if (res) { - dahdi_close_pri_fd(pri, i); - ast_log(LOG_ERROR, "Unable to get span state for D-channel %d (%s)\n", x, strerror(errno)); - } - if (!si.alarms) - pri->dchanavail[i] |= DCHAN_NOTINALARM; - else - pri->dchanavail[i] &= ~DCHAN_NOTINALARM; - bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE; - bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE; - bi.numbufs = 32; - bi.bufsize = 1024; - if (ioctl(pri->fds[i], DAHDI_SET_BUFINFO, &bi)) { - ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d: %s\n", x, strerror(errno)); - dahdi_close_pri_fd(pri, i); - return -1; - } - pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype); - /* Force overlap dial if we're doing GR-303! */ - if (pri->switchtype == PRI_SWITCH_GR303_TMC) - pri->overlapdial = 1; - pri_set_overlapdial(pri->dchans[i],pri->overlapdial); -#ifdef HAVE_PRI_INBANDDISCONNECT - pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect); -#endif - /* Enslave to master if appropriate */ - if (i) - pri_enslave(pri->dchans[0], pri->dchans[i]); - if (!pri->dchans[i]) { - dahdi_close_pri_fd(pri, i); - ast_log(LOG_ERROR, "Unable to create PRI structure\n"); - return -1; + } + ast_mutex_unlock(&pri->lock); } - pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG); - pri_set_nsf(pri->dchans[i], pri->nsf); -#ifdef PRI_GETSET_TIMERS - for (x = 0; x < PRI_MAX_TIMERS; x++) { - if (pritimers[x] != 0) - pri_set_timer(pri->dchans[i], x, pritimers[x]); - } -#endif + /* Never reached */ + return NULL; } - /* Assume primary is the one we use */ - pri->pri = pri->dchans[0]; - pri->resetpos = -1; - if (ast_pthread_create_background(&pri->master, NULL, pri_dchannel, pri)) { + + static int start_pri(struct dahdi_pri *pri) + { + int res, x; + struct dahdi_params p; + struct dahdi_bufferinfo bi; + struct dahdi_spaninfo si; + int i; + for (i = 0; i < NUM_DCHANS; i++) { if (!pri->dchannels[i]) break; - dahdi_close_pri_fd(pri, i); + #ifdef HAVE_ZAPTEL + pri->fds[i] = open("/dev/zap/channel", O_RDWR, 0600); + #else + pri->fds[i] = open("/dev/dahdi/channel", O_RDWR, 0600); + #endif + x = pri->dchannels[i]; + if ((pri->fds[i] < 0) || (ioctl(pri->fds[i],DAHDI_SPECIFY,&x) == -1)) { + ast_log(LOG_ERROR, "Unable to open D-channel %d (%s)\n", x, strerror(errno)); + return -1; + } + res = ioctl(pri->fds[i], DAHDI_GET_PARAMS, &p); + if (res) { + dahdi_close_pri_fd(pri, i); + ast_log(LOG_ERROR, "Unable to get parameters for D-channel %d (%s)\n", x, strerror(errno)); + return -1; + } + if ((p.sigtype != DAHDI_SIG_HDLCFCS) && (p.sigtype != DAHDI_SIG_HARDHDLC)) { + dahdi_close_pri_fd(pri, i); + ast_log(LOG_ERROR, "D-channel %d is not in HDLC/FCS mode. See /etc/dahdi/system.conf\n", x); + return -1; + } + memset(&si, 0, sizeof(si)); + res = ioctl(pri->fds[i], DAHDI_SPANSTAT, &si); + if (res) { + dahdi_close_pri_fd(pri, i); + ast_log(LOG_ERROR, "Unable to get span state for D-channel %d (%s)\n", x, strerror(errno)); + } + if (!si.alarms) + pri->dchanavail[i] |= DCHAN_NOTINALARM; + else + pri->dchanavail[i] &= ~DCHAN_NOTINALARM; + bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE; + bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE; + bi.numbufs = 32; + bi.bufsize = 1024; + if (ioctl(pri->fds[i], DAHDI_SET_BUFINFO, &bi)) { + ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d: %s\n", x, strerror(errno)); + dahdi_close_pri_fd(pri, i); + return -1; + } + pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype); + /* Force overlap dial if we're doing GR-303! */ + if (pri->switchtype == PRI_SWITCH_GR303_TMC) + pri->overlapdial = 1; + pri_set_overlapdial(pri->dchans[i],pri->overlapdial); + #ifdef HAVE_PRI_INBANDDISCONNECT + pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect); + #endif + /* Enslave to master if appropriate */ + if (i) + pri_enslave(pri->dchans[0], pri->dchans[i]); + if (!pri->dchans[i]) { + dahdi_close_pri_fd(pri, i); + ast_log(LOG_ERROR, "Unable to create PRI structure\n"); + return -1; + } + pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG); + pri_set_nsf(pri->dchans[i], pri->nsf); + #ifdef PRI_GETSET_TIMERS + for (x = 0; x < PRI_MAX_TIMERS; x++) { + if (pritimers[x] != 0) + pri_set_timer(pri->dchans[i], x, pritimers[x]); + } + #endif } - ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno)); - return -1; - } - return 0; -} - -static char *complete_span_helper(const char *line, const char *word, int pos, int state, int rpos) -{ - int which, span; - char *ret = NULL; - - if (pos != rpos) - return ret; - - for (which = span = 0; span < NUM_SPANS; span++) { - if (pris[span].pri && ++which > state) { - asprintf(&ret, "%d", span + 1); /* user indexes start from 1 */ - break; + /* Assume primary is the one we use */ + pri->pri = pri->dchans[0]; + pri->resetpos = -1; + if (ast_pthread_create_background(&pri->master, NULL, pri_dchannel, pri)) { + for (i = 0; i < NUM_DCHANS; i++) { + if (!pri->dchannels[i]) + break; + dahdi_close_pri_fd(pri, i); + } + ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno)); + return -1; } + return 0; } - return ret; -} - -static char *complete_span_4(const char *line, const char *word, int pos, int state) -{ - return complete_span_helper(line,word,pos,state,3); -} - -static char *complete_span_5(const char *line, const char *word, int pos, int state) -{ - return complete_span_helper(line,word,pos,state,4); -} - -static int handle_pri_set_debug_file(int fd, int argc, char **argv) -{ - int myfd; - - if (!strncasecmp(argv[1], "set", 3)) { - if (argc < 5) - return RESULT_SHOWUSAGE; - - if (ast_strlen_zero(argv[4])) - return RESULT_SHOWUSAGE; - - myfd = open(argv[4], O_CREAT|O_WRONLY, 0600); - if (myfd < 0) { - ast_cli(fd, "Unable to open '%s' for writing\n", argv[4]); - return RESULT_SUCCESS; + + static char *complete_span_helper(const char *line, const char *word, int pos, int state, int rpos) + { + int which, span; + char *ret = NULL; + + if (pos != rpos) + return ret; + + for (which = span = 0; span < NUM_SPANS; span++) { + if (pris[span].pri && ++which > state) { + asprintf(&ret, "%d", span + 1); /* user indexes start from 1 */ + break; + } } - - ast_mutex_lock(&pridebugfdlock); - - if (pridebugfd >= 0) - close(pridebugfd); - - pridebugfd = myfd; - ast_copy_string(pridebugfilename,argv[4],sizeof(pridebugfilename)); - - ast_mutex_unlock(&pridebugfdlock); - - ast_cli(fd, "PRI debug output will be sent to '%s'\n", argv[4]); - } else { - /* Assume it is unset */ - ast_mutex_lock(&pridebugfdlock); - close(pridebugfd); - pridebugfd = -1; - ast_cli(fd, "PRI debug output to file disabled\n"); - ast_mutex_unlock(&pridebugfdlock); - } - - return RESULT_SUCCESS; -} - -#ifdef HAVE_PRI_VERSION -static int handle_pri_version(int fd, int agc, char *argv[]) { - ast_cli(fd, "libpri version: %s\n", pri_get_version()); - return RESULT_SUCCESS; -} -#endif - -static int handle_pri_debug(int fd, int argc, char *argv[]) -{ - int span; - int x; - if (argc < 4) { - return RESULT_SHOWUSAGE; - } - span = atoi(argv[3]); - if ((span < 1) || (span > NUM_SPANS)) { - ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[3], 1, NUM_SPANS); - return RESULT_SUCCESS; + return ret; } - if (!pris[span-1].pri) { - ast_cli(fd, "No PRI running on span %d\n", span); - return RESULT_SUCCESS; + + static char *complete_span_4(const char *line, const char *word, int pos, int state) + { + return complete_span_helper(line,word,pos,state,3); } - for (x = 0; x < NUM_DCHANS; x++) { - if (pris[span-1].dchans[x]) - pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_APDU | - PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE | - PRI_DEBUG_Q921_STATE); + + static char *complete_span_5(const char *line, const char *word, int pos, int state) + { + return complete_span_helper(line,word,pos,state,4); } - ast_cli(fd, "Enabled debugging on span %d\n", span); - return RESULT_SUCCESS; -} - - - -static int handle_pri_no_debug(int fd, int argc, char *argv[]) -{ - int span; - int x; - if (argc < 5) - return RESULT_SHOWUSAGE; - span = atoi(argv[4]); - if ((span < 1) || (span > NUM_SPANS)) { - ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS); + + static int handle_pri_set_debug_file(int fd, int argc, char **argv) + { + int myfd; + + if (!strncasecmp(argv[1], "set", 3)) { + if (argc < 5) + return RESULT_SHOWUSAGE; + + if (ast_strlen_zero(argv[4])) + return RESULT_SHOWUSAGE; + + myfd = open(argv[4], O_CREAT|O_WRONLY, 0600); + if (myfd < 0) { + ast_cli(fd, "Unable to open '%s' for writing\n", argv[4]); + return RESULT_SUCCESS; + } + + ast_mutex_lock(&pridebugfdlock); + + if (pridebugfd >= 0) + close(pridebugfd); + + pridebugfd = myfd; + ast_copy_string(pridebugfilename,argv[4],sizeof(pridebugfilename)); + + ast_mutex_unlock(&pridebugfdlock); + + ast_cli(fd, "PRI debug output will be sent to '%s'\n", argv[4]); + } else { + /* Assume it is unset */ + ast_mutex_lock(&pridebugfdlock); + close(pridebugfd); + pridebugfd = -1; + ast_cli(fd, "PRI debug output to file disabled\n"); + ast_mutex_unlock(&pridebugfdlock); + } + return RESULT_SUCCESS; } - if (!pris[span-1].pri) { - ast_cli(fd, "No PRI running on span %d\n", span); + + #ifdef HAVE_PRI_VERSION + static int handle_pri_version(int fd, int agc, char *argv[]) { + ast_cli(fd, "libpri version: %s\n", pri_get_version()); return RESULT_SUCCESS; } - for (x = 0; x < NUM_DCHANS; x++) { - if (pris[span-1].dchans[x]) - pri_set_debug(pris[span-1].dchans[x], 0); - } - ast_cli(fd, "Disabled debugging on span %d\n", span); - return RESULT_SUCCESS; -} - -static int handle_pri_really_debug(int fd, int argc, char *argv[]) -{ - int span; - int x; - if (argc < 5) - return RESULT_SHOWUSAGE; - span = atoi(argv[4]); - if ((span < 1) || (span > NUM_SPANS)) { - ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS); + #endif + + static int handle_pri_debug(int fd, int argc, char *argv[]) + { + int span; + int x; + if (argc < 4) { + return RESULT_SHOWUSAGE; + } + span = atoi(argv[3]); + if ((span < 1) || (span > NUM_SPANS)) { + ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[3], 1, NUM_SPANS); + return RESULT_SUCCESS; + } + if (!pris[span-1].pri) { + ast_cli(fd, "No PRI running on span %d\n", span); + return RESULT_SUCCESS; + } + for (x = 0; x < NUM_DCHANS; x++) { + if (pris[span-1].dchans[x]) + pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_APDU | + PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE | + PRI_DEBUG_Q921_STATE); + } + ast_cli(fd, "Enabled debugging on span %d\n", span); return RESULT_SUCCESS; } - if (!pris[span-1].pri) { - ast_cli(fd, "No PRI running on span %d\n", span); + + + + static int handle_pri_no_debug(int fd, int argc, char *argv[]) + { + int span; + int x; + if (argc < 5) + return RESULT_SHOWUSAGE; + span = atoi(argv[4]); + if ((span < 1) || (span > NUM_SPANS)) { + ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS); + return RESULT_SUCCESS; + } + if (!pris[span-1].pri) { + ast_cli(fd, "No PRI running on span %d\n", span); + return RESULT_SUCCESS; + } + for (x = 0; x < NUM_DCHANS; x++) { + if (pris[span-1].dchans[x]) + pri_set_debug(pris[span-1].dchans[x], 0); + } + ast_cli(fd, "Disabled debugging on span %d\n", span); return RESULT_SUCCESS; } - for (x = 0; x < NUM_DCHANS; x++) { - if (pris[span-1].dchans[x]) - pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_APDU | - PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE | - PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_STATE); + + static int handle_pri_really_debug(int fd, int argc, char *argv[]) + { + int span; + int x; + if (argc < 5) + return RESULT_SHOWUSAGE; + span = atoi(argv[4]); + if ((span < 1) || (span > NUM_SPANS)) { + ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS); + return RESULT_SUCCESS; + } + if (!pris[span-1].pri) { + ast_cli(fd, "No PRI running on span %d\n", span); + return RESULT_SUCCESS; + } + for (x = 0; x < NUM_DCHANS; x++) { + if (pris[span-1].dchans[x]) + pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_APDU | + PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE | + PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_STATE); + } + ast_cli(fd, "Enabled EXTENSIVE debugging on span %d\n", span); + return RESULT_SUCCESS; } - ast_cli(fd, "Enabled EXTENSIVE debugging on span %d\n", span); - return RESULT_SUCCESS; -} - -static void build_status(char *s, size_t len, int status, int active) -{ - if (!s || len < 1) { - return; + + static void build_status(char *s, size_t len, int status, int active) + { + if (!s || len < 1) { + return; + } + s[0] = '\0'; + if (status & DCHAN_PROVISIONED) + strncat(s, "Provisioned, ", len - strlen(s) - 1); + if (!(status & DCHAN_NOTINALARM)) + strncat(s, "In Alarm, ", len - strlen(s) - 1); + if (status & DCHAN_UP) + strncat(s, "Up", len - strlen(s) - 1); + else + strncat(s, "Down", len - strlen(s) - 1); + if (active) + strncat(s, ", Active", len - strlen(s) - 1); + else + strncat(s, ", Standby", len - strlen(s) - 1); + s[len - 1] = '\0'; } - s[0] = '\0'; - if (status & DCHAN_PROVISIONED) - strncat(s, "Provisioned, ", len - strlen(s) - 1); - if (!(status & DCHAN_NOTINALARM)) - strncat(s, "In Alarm, ", len - strlen(s) - 1); - if (status & DCHAN_UP) - strncat(s, "Up", len - strlen(s) - 1); - else - strncat(s, "Down", len - strlen(s) - 1); - if (active) - strncat(s, ", Active", len - strlen(s) - 1); - else - strncat(s, ", Standby", len - strlen(s) - 1); - s[len - 1] = '\0'; -} - -static int handle_pri_show_spans(int fd, int argc, char *argv[]) -{ - int span; - int x; - char status[256]; - if (argc != 3) - return RESULT_SHOWUSAGE; - - for (span = 0; span < NUM_SPANS; span++) { - if (pris[span].pri) { - for (x = 0; x < NUM_DCHANS; x++) { - if (pris[span].dchannels[x]) { - build_status(status, sizeof(status), pris[span].dchanavail[x], pris[span].dchans[x] == pris[span].pri); - ast_cli(fd, "PRI span %d/%d: %s\n", span + 1, x, status); + + static int handle_pri_show_spans(int fd, int argc, char *argv[]) + { + int span; + int x; + char status[256]; + if (argc != 3) + return RESULT_SHOWUSAGE; + + for (span = 0; span < NUM_SPANS; span++) { + if (pris[span].pri) { + for (x = 0; x < NUM_DCHANS; x++) { + if (pris[span].dchannels[x]) { + build_status(status, sizeof(status), pris[span].dchanavail[x], pris[span].dchans[x] == pris[span].pri); + ast_cli(fd, "PRI span %d/%d: %s\n", span + 1, x, status); + } } } } - } - return RESULT_SUCCESS; -} - -static int handle_pri_show_span(int fd, int argc, char *argv[]) -{ - int span; - int x; - char status[256]; - if (argc < 4) - return RESULT_SHOWUSAGE; - span = atoi(argv[3]); - if ((span < 1) || (span > NUM_SPANS)) { - ast_cli(fd, "Invalid span '%s'. Should be a number from %d to %d\n", argv[3], 1, NUM_SPANS); return RESULT_SUCCESS; } - if (!pris[span-1].pri) { - ast_cli(fd, "No PRI running on span %d\n", span); - return RESULT_SUCCESS; - } - for (x = 0; x < NUM_DCHANS; x++) { - if (pris[span-1].dchannels[x]) { -#ifdef PRI_DUMP_INFO_STR - char *info_str = NULL; -#endif - ast_cli(fd, "%s D-channel: %d\n", pri_order(x), pris[span-1].dchannels[x]); - build_status(status, sizeof(status), pris[span-1].dchanavail[x], pris[span-1].dchans[x] == pris[span-1].pri); - ast_cli(fd, "Status: %s\n", status); -#ifdef PRI_DUMP_INFO_STR - info_str = pri_dump_info_str(pris[span-1].pri); - if (info_str) { - ast_cli(fd, "%s", info_str); - free(info_str); + + static int handle_pri_show_span(int fd, int argc, char *argv[]) + { + int span; + int x; + char status[256]; + if (argc < 4) + return RESULT_SHOWUSAGE; + span = atoi(argv[3]); + if ((span < 1) || (span > NUM_SPANS)) { + ast_cli(fd, "Invalid span '%s'. Should be a number from %d to %d\n", argv[3], 1, NUM_SPANS); + return RESULT_SUCCESS; + } + if (!pris[span-1].pri) { + ast_cli(fd, "No PRI running on span %d\n", span); + return RESULT_SUCCESS; + } + for (x = 0; x < NUM_DCHANS; x++) { + if (pris[span-1].dchannels[x]) { + #ifdef PRI_DUMP_INFO_STR + char *info_str = NULL; + #endif + ast_cli(fd, "%s D-channel: %d\n", pri_order(x), pris[span-1].dchannels[x]); + build_status(status, sizeof(status), pris[span-1].dchanavail[x], pris[span-1].dchans[x] == pris[span-1].pri); + ast_cli(fd, "Status: %s\n", status); + #ifdef PRI_DUMP_INFO_STR + info_str = pri_dump_info_str(pris[span-1].pri); + if (info_str) { + ast_cli(fd, "%s", info_str); + free(info_str); + } + #else + pri_dump_info(pris[span-1].pri); + #endif + ast_cli(fd, "\n"); } -#else - pri_dump_info(pris[span-1].pri); -#endif - ast_cli(fd, "\n"); } + return RESULT_SUCCESS; } - return RESULT_SUCCESS; -} - -static int handle_pri_show_debug(int fd, int argc, char *argv[]) -{ - int x; - int span; - int count=0; - int debug=0; - - for (span = 0; span < NUM_SPANS; span++) { - if (pris[span].pri) { - for (x = 0; x < NUM_DCHANS; x++) { - debug = 0; - if (pris[span].dchans[x]) { - debug = pri_get_debug(pris[span].dchans[x]); - ast_cli(fd, "Span %d: Debug: %s\tIntense: %s\n", span+1, (debug&PRI_DEBUG_Q931_STATE)? "Yes" : "No" ,(debug&PRI_DEBUG_Q921_RAW)? "Yes" : "No" ); - count++; + + static int handle_pri_show_debug(int fd, int argc, char *argv[]) + { + int x; + int span; + int count=0; + int debug=0; + + for (span = 0; span < NUM_SPANS; span++) { + if (pris[span].pri) { + for (x = 0; x < NUM_DCHANS; x++) { + debug = 0; + if (pris[span].dchans[x]) { + debug = pri_get_debug(pris[span].dchans[x]); + ast_cli(fd, "Span %d: Debug: %s\tIntense: %s\n", span+1, (debug&PRI_DEBUG_Q931_STATE)? "Yes" : "No" ,(debug&PRI_DEBUG_Q921_RAW)? "Yes" : "No" ); + count++; + } } } + } - + ast_mutex_lock(&pridebugfdlock); + if (pridebugfd >= 0) + ast_cli(fd, "Logging PRI debug to file %s\n", pridebugfilename); + ast_mutex_unlock(&pridebugfdlock); + + if (!count) + ast_cli(fd, "No debug set or no PRI running\n"); + return RESULT_SUCCESS; } - ast_mutex_lock(&pridebugfdlock); - if (pridebugfd >= 0) - ast_cli(fd, "Logging PRI debug to file %s\n", pridebugfilename); - ast_mutex_unlock(&pridebugfdlock); - - if (!count) - ast_cli(fd, "No debug set or no PRI running\n"); - return RESULT_SUCCESS; -} - -static const char pri_debug_help[] = - "Usage: pri debug span \n" - " Enables debugging on a given PRI span\n"; -static const char pri_no_debug_help[] = - "Usage: pri no debug span \n" - " Disables debugging on a given PRI span\n"; - -static const char pri_really_debug_help[] = - "Usage: pri intensive debug span \n" - " Enables debugging down to the Q.921 level\n"; - -static const char pri_show_span_help[] = - "Usage: pri show span \n" - " Displays PRI Information on a given PRI span\n"; - -static const char pri_show_spans_help[] = - "Usage: pri show spans\n" - " Displays PRI Information\n"; - -static struct ast_cli_entry dahdi_pri_cli[] = { - { { "pri", "debug", "span", NULL }, - handle_pri_debug, "Enables PRI debugging on a span", - pri_debug_help, complete_span_4 }, - - { { "pri", "no", "debug", "span", NULL }, - handle_pri_no_debug, "Disables PRI debugging on a span", - pri_no_debug_help, complete_span_5 }, - - { { "pri", "intense", "debug", "span", NULL }, - handle_pri_really_debug, "Enables REALLY INTENSE PRI debugging", - pri_really_debug_help, complete_span_5 }, - - { { "pri", "show", "spans", NULL }, - handle_pri_show_spans, "Displays PRI Information", - pri_show_spans_help }, - - { { "pri", "show", "span", NULL }, - handle_pri_show_span, "Displays PRI Information", - pri_show_span_help, complete_span_4 }, - - { { "pri", "show", "debug", NULL }, - handle_pri_show_debug, "Displays current PRI debug settings" }, - - { { "pri", "set", "debug", "file", NULL }, - handle_pri_set_debug_file, "Sends PRI debug output to the specified file" }, - - { { "pri", "unset", "debug", "file", NULL }, - handle_pri_set_debug_file, "Ends PRI debug output to file" }, - -#ifdef HAVE_PRI_VERSION - { { "pri", "show", "version", NULL }, - handle_pri_version, "Displays version of libpri" }, -#endif -}; - -#endif /* HAVE_PRI */ - -static int dahdi_destroy_channel(int fd, int argc, char **argv) -{ - int channel; + static const char pri_debug_help[] = + "Usage: pri debug span \n" + " Enables debugging on a given PRI span\n"; + + static const char pri_no_debug_help[] = + "Usage: pri no debug span \n" + " Disables debugging on a given PRI span\n"; + + static const char pri_really_debug_help[] = + "Usage: pri intensive debug span \n" + " Enables debugging down to the Q.921 level\n"; + + static const char pri_show_span_help[] = + "Usage: pri show span \n" + " Displays PRI Information on a given PRI span\n"; + + static const char pri_show_spans_help[] = + "Usage: pri show spans\n" + " Displays PRI Information\n"; + + static struct ast_cli_entry dahdi_pri_cli[] = { + { { "pri", "debug", "span", NULL }, + handle_pri_debug, "Enables PRI debugging on a span", + pri_debug_help, complete_span_4 }, + + { { "pri", "no", "debug", "span", NULL }, + handle_pri_no_debug, "Disables PRI debugging on a span", + pri_no_debug_help, complete_span_5 }, + + { { "pri", "intense", "debug", "span", NULL }, + handle_pri_really_debug, "Enables REALLY INTENSE PRI debugging", + pri_really_debug_help, complete_span_5 }, + + { { "pri", "show", "spans", NULL }, + handle_pri_show_spans, "Displays PRI Information", + pri_show_spans_help }, + + { { "pri", "show", "span", NULL }, + handle_pri_show_span, "Displays PRI Information", + pri_show_span_help, complete_span_4 }, + + { { "pri", "show", "debug", NULL }, + handle_pri_show_debug, "Displays current PRI debug settings" }, + + { { "pri", "set", "debug", "file", NULL }, + handle_pri_set_debug_file, "Sends PRI debug output to the specified file" }, + + { { "pri", "unset", "debug", "file", NULL }, + handle_pri_set_debug_file, "Ends PRI debug output to file" }, + + #ifdef HAVE_PRI_VERSION + { { "pri", "show", "version", NULL }, + handle_pri_version, "Displays version of libpri" }, + #endif + }; - if (argc != 4) - return RESULT_SHOWUSAGE; + #endif /* HAVE_PRI */ - channel = atoi(argv[3]); - - return dahdi_destroy_channel_bynum(channel); -} - -static void dahdi_softhangup_all(void) -{ - struct dahdi_pvt *p; -retry: - ast_mutex_lock(&iflock); - for (p = iflist; p; p = p->next) { - ast_mutex_lock(&p->lock); - if (p->owner && !p->restartpending) { - if (ast_channel_trylock(p->owner)) { + static int dahdi_destroy_channel(int fd, int argc, char **argv) + { + int channel; + + if (argc != 4) + return RESULT_SHOWUSAGE; + + channel = atoi(argv[3]); + + return dahdi_destroy_channel_bynum(channel); + } + + static void dahdi_softhangup_all(void) + { + struct dahdi_pvt *p; + retry: + ast_mutex_lock(&iflock); + for (p = iflist; p; p = p->next) { + ast_mutex_lock(&p->lock); + if (p->owner && !p->restartpending) { + if (ast_channel_trylock(p->owner)) { + if (option_debug > 2) + ast_verbose("Avoiding deadlock\n"); + /* Avoid deadlock since you're not supposed to lock iflock or pvt before a channel */ + ast_mutex_unlock(&p->lock); + ast_mutex_unlock(&iflock); + goto retry; + } if (option_debug > 2) - ast_verbose("Avoiding deadlock\n"); - /* Avoid deadlock since you're not supposed to lock iflock or pvt before a channel */ - ast_mutex_unlock(&p->lock); - ast_mutex_unlock(&iflock); - goto retry; + ast_verbose("Softhanging up on %s\n", p->owner->name); + ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_EXPLICIT); + p->restartpending = 1; + num_restart_pending++; + ast_channel_unlock(p->owner); } - if (option_debug > 2) - ast_verbose("Softhanging up on %s\n", p->owner->name); - ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_EXPLICIT); - p->restartpending = 1; - num_restart_pending++; - ast_channel_unlock(p->owner); + ast_mutex_unlock(&p->lock); } - ast_mutex_unlock(&p->lock); - } - ast_mutex_unlock(&iflock); -} - -static int setup_dahdi(int reload); -static int dahdi_restart(void) -{ -#if defined(HAVE_PRI) - int i, j; -#endif - int cancel_code; - struct dahdi_pvt *p; - - ast_mutex_lock(&restart_lock); - - if (option_verbose) - ast_verbose("Destroying channels and reloading DAHDI configuration.\n"); - dahdi_softhangup_all(); - if (option_verbose > 3) - ast_verbose("Initial softhangup of all DAHDI channels complete.\n"); - + ast_mutex_unlock(&iflock); + } + + static int setup_dahdi(int reload); + static int dahdi_restart(void) + { #if defined(HAVE_PRI) - for (i = 0; i < NUM_SPANS; i++) { - if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL)) { - cancel_code = pthread_cancel(pris[i].master); - pthread_kill(pris[i].master, SIGURG); + int i, j; + #endif + int cancel_code; + struct dahdi_pvt *p; + + ast_mutex_lock(&restart_lock); + + if (option_verbose) + ast_verbose("Destroying channels and reloading DAHDI configuration.\n"); + dahdi_softhangup_all(); + if (option_verbose > 3) + ast_verbose("Initial softhangup of all DAHDI channels complete.\n"); + + #if defined(HAVE_PRI) + for (i = 0; i < NUM_SPANS; i++) { + if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL)) { + cancel_code = pthread_cancel(pris[i].master); + pthread_kill(pris[i].master, SIGURG); + if (option_debug > 3) + ast_verbose("Waiting to join thread of span %d with pid=%p, cancel_code=%d\n", i, (void *) pris[i].master, cancel_code); + pthread_join(pris[i].master, NULL); + if (option_debug > 3) + ast_verbose("Joined thread of span %d\n", i); + } + } + #endif + + ast_mutex_lock(&monlock); + if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) { + cancel_code = pthread_cancel(monitor_thread); + pthread_kill(monitor_thread, SIGURG); if (option_debug > 3) - ast_verbose("Waiting to join thread of span %d with pid=%p, cancel_code=%d\n", i, (void *) pris[i].master, cancel_code); - pthread_join(pris[i].master, NULL); + ast_verbose("Waiting to join monitor thread with pid=%p, cancel_code=%d\n", (void *) monitor_thread, cancel_code); + pthread_join(monitor_thread, NULL); if (option_debug > 3) - ast_verbose("Joined thread of span %d\n", i); + ast_verbose("Joined monitor thread\n"); + } + monitor_thread = AST_PTHREADT_NULL; /* prepare to restart thread in setup_dahdi once channels are reconfigured */ + + ast_mutex_lock(&ss_thread_lock); + while (ss_thread_count > 0) { /* let ss_threads finish and run dahdi_hangup before dahvi_pvts are destroyed */ + int x = DAHDI_FLASH; + if (option_debug > 2) + ast_verbose("Waiting on %d ss_thread(s) to finish\n", ss_thread_count); + + for (p = iflist; p; p = p->next) { + if (p->owner) + ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x); /* important to create an event for dahdi_wait_event to register so that all ss_threads terminate */ + } + ast_cond_wait(&ss_thread_complete, &ss_thread_lock); + } + + /* ensure any created channels before monitor threads were stopped are hungup */ + dahdi_softhangup_all(); + if (option_verbose > 3) + ast_verbose("Final softhangup of all DAHDI channels complete.\n"); + destroy_all_channels(); + if (option_debug) + ast_verbose("Channels destroyed. Now re-reading config. %d active channels remaining.\n", ast_active_channels()); + + ast_mutex_unlock(&monlock); + + #ifdef HAVE_PRI + for (i = 0; i < NUM_SPANS; i++) { + for (j = 0; j < NUM_DCHANS; j++) + dahdi_close_pri_fd(&(pris[i]), j); + } + + memset(pris, 0, sizeof(pris)); + for (i = 0; i < NUM_SPANS; i++) { + ast_mutex_init(&pris[i].lock); + pris[i].offset = -1; + pris[i].master = AST_PTHREADT_NULL; + for (j = 0; j < NUM_DCHANS; j++) + pris[i].fds[j] = -1; + } + pri_set_error(dahdi_pri_error); + pri_set_message(dahdi_pri_message); + #endif + + if (setup_dahdi(2) != 0) { + ast_log(LOG_WARNING, "Reload channels from dahdi config failed!\n"); + ast_mutex_unlock(&ss_thread_lock); + return 1; } - } - #endif - - ast_mutex_lock(&monlock); - if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) { - cancel_code = pthread_cancel(monitor_thread); - pthread_kill(monitor_thread, SIGURG); - if (option_debug > 3) - ast_verbose("Waiting to join monitor thread with pid=%p, cancel_code=%d\n", (void *) monitor_thread, cancel_code); - pthread_join(monitor_thread, NULL); - if (option_debug > 3) - ast_verbose("Joined monitor thread\n"); - } - monitor_thread = AST_PTHREADT_NULL; /* prepare to restart thread in setup_dahdi once channels are reconfigured */ - - ast_mutex_lock(&ss_thread_lock); - while (ss_thread_count > 0) { /* let ss_threads finish and run dahdi_hangup before dahvi_pvts are destroyed */ - int x = DAHDI_FLASH; - if (option_debug > 2) - ast_verbose("Waiting on %d ss_thread(s) to finish\n", ss_thread_count); - - for (p = iflist; p; p = p->next) { - if (p->owner) - ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x); /* important to create an event for dahdi_wait_event to register so that all ss_threads terminate */ - } - ast_cond_wait(&ss_thread_complete, &ss_thread_lock); - } - - /* ensure any created channels before monitor threads were stopped are hungup */ - dahdi_softhangup_all(); - if (option_verbose > 3) - ast_verbose("Final softhangup of all DAHDI channels complete.\n"); - destroy_all_channels(); - if (option_debug) - ast_verbose("Channels destroyed. Now re-reading config. %d active channels remaining.\n", ast_active_channels()); - - ast_mutex_unlock(&monlock); - - #ifdef HAVE_PRI - for (i = 0; i < NUM_SPANS; i++) { - for (j = 0; j < NUM_DCHANS; j++) - dahdi_close_pri_fd(&(pris[i]), j); - } - - memset(pris, 0, sizeof(pris)); - for (i = 0; i < NUM_SPANS; i++) { - ast_mutex_init(&pris[i].lock); - pris[i].offset = -1; - pris[i].master = AST_PTHREADT_NULL; - for (j = 0; j < NUM_DCHANS; j++) - pris[i].fds[j] = -1; - } - pri_set_error(dahdi_pri_error); - pri_set_message(dahdi_pri_message); - #endif - - if (setup_dahdi(2) != 0) { - ast_log(LOG_WARNING, "Reload channels from dahdi config failed!\n"); ast_mutex_unlock(&ss_thread_lock); - return 1; - } - ast_mutex_unlock(&ss_thread_lock); - ast_mutex_unlock(&restart_lock); - return 0; -} - -static int dahdi_restart_cmd(int fd, int argc, char **argv) -{ - if (argc != 2) { - return RESULT_SHOWUSAGE; + ast_mutex_unlock(&restart_lock); + return 0; } - - if (dahdi_restart() != 0) - return RESULT_FAILURE; - return RESULT_SUCCESS; -} - -static int dahdi_show_channels(int fd, int argc, char **argv) -{ -#define FORMAT "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n" -#define FORMAT2 "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n" - struct dahdi_pvt *tmp = NULL; - char tmps[20] = ""; - ast_mutex_t *lock; - struct dahdi_pvt *start; -#ifdef HAVE_PRI - int trunkgroup; - struct dahdi_pri *pri = NULL; - int x; -#endif - - lock = &iflock; - start = iflist; - -#ifdef HAVE_PRI - if (argc == 4) { - if ((trunkgroup = atoi(argv[3])) < 1) + + static int dahdi_restart_cmd(int fd, int argc, char **argv) + { + if (argc != 2) { return RESULT_SHOWUSAGE; - for (x = 0; x < NUM_SPANS; x++) { - if (pris[x].trunkgroup == trunkgroup) { - pri = pris + x; - break; - } } - if (pri) { - start = pri->crvs; - lock = &pri->lock; - } else { - ast_cli(fd, "No such trunk group %d\n", trunkgroup); + + if (dahdi_restart() != 0) return RESULT_FAILURE; - } - } else -#endif - if (argc != 3) - return RESULT_SHOWUSAGE; - - ast_mutex_lock(lock); -#ifdef HAVE_PRI - ast_cli(fd, FORMAT2, pri ? "CRV" : "Chan", "Extension", "Context", "Language", "MOH Interpret"); -#else - ast_cli(fd, FORMAT2, "Chan", "Extension", "Context", "Language", "MOH Interpret"); -#endif + return RESULT_SUCCESS; + } + + static int dahdi_show_channels(int fd, int argc, char **argv) + { + #define FORMAT "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n" + #define FORMAT2 "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n" + struct dahdi_pvt *tmp = NULL; + char tmps[20] = ""; + ast_mutex_t *lock; + struct dahdi_pvt *start; + #ifdef HAVE_PRI + int trunkgroup; + struct dahdi_pri *pri = NULL; + int x; + #endif - tmp = start; - while (tmp) { - if (tmp->channel > 0) { - snprintf(tmps, sizeof(tmps), "%d", tmp->channel); + lock = &iflock; + start = iflist; + + #ifdef HAVE_PRI + if (argc == 4) { + if ((trunkgroup = atoi(argv[3])) < 1) + return RESULT_SHOWUSAGE; + for (x = 0; x < NUM_SPANS; x++) { + if (pris[x].trunkgroup == trunkgroup) { + pri = pris + x; + break; + } + } + if (pri) { + start = pri->crvs; + lock = &pri->lock; + } else { + ast_cli(fd, "No such trunk group %d\n", trunkgroup); + return RESULT_FAILURE; + } } else - ast_copy_string(tmps, "pseudo", sizeof(tmps)); - ast_cli(fd, FORMAT, tmps, tmp->exten, tmp->context, tmp->language, tmp->mohinterpret); - tmp = tmp->next; - } - ast_mutex_unlock(lock); - return RESULT_SUCCESS; -#undef FORMAT -#undef FORMAT2 -} - -static int dahdi_show_channel(int fd, int argc, char **argv) -{ - int channel; - struct dahdi_pvt *tmp = NULL; - struct dahdi_confinfo ci; - struct dahdi_params ps; - int x; - ast_mutex_t *lock; - struct dahdi_pvt *start; -#ifdef HAVE_PRI - char *c; - int trunkgroup; - struct dahdi_pri *pri=NULL; -#endif - - lock = &iflock; - start = iflist; - - if (argc != 4) - return RESULT_SHOWUSAGE; -#ifdef HAVE_PRI - if ((c = strchr(argv[3], ':'))) { - if (sscanf(argv[3], "%d:%d", &trunkgroup, &channel) != 2) + #endif + if (argc != 3) return RESULT_SHOWUSAGE; - if ((trunkgroup < 1) || (channel < 1)) + + ast_mutex_lock(lock); + #ifdef HAVE_PRI + ast_cli(fd, FORMAT2, pri ? "CRV" : "Chan", "Extension", "Context", "Language", "MOH Interpret"); + #else + ast_cli(fd, FORMAT2, "Chan", "Extension", "Context", "Language", "MOH Interpret"); + #endif + + tmp = start; + while (tmp) { + if (tmp->channel > 0) { + snprintf(tmps, sizeof(tmps), "%d", tmp->channel); + } else + ast_copy_string(tmps, "pseudo", sizeof(tmps)); + ast_cli(fd, FORMAT, tmps, tmp->exten, tmp->context, tmp->language, tmp->mohinterpret); + tmp = tmp->next; + } + ast_mutex_unlock(lock); + return RESULT_SUCCESS; + #undef FORMAT + #undef FORMAT2 + } + + static int dahdi_show_channel(int fd, int argc, char **argv) + { + int channel; + struct dahdi_pvt *tmp = NULL; + struct dahdi_confinfo ci; + struct dahdi_params ps; + int x; + ast_mutex_t *lock; + struct dahdi_pvt *start; + #ifdef HAVE_PRI + char *c; + int trunkgroup; + struct dahdi_pri *pri=NULL; + #endif + + lock = &iflock; + start = iflist; + + if (argc != 4) return RESULT_SHOWUSAGE; - for (x = 0; x < NUM_SPANS; x++) { - if (pris[x].trunkgroup == trunkgroup) { - pri = pris + x; - break; + #ifdef HAVE_PRI + if ((c = strchr(argv[3], ':'))) { + if (sscanf(argv[3], "%d:%d", &trunkgroup, &channel) != 2) + return RESULT_SHOWUSAGE; + if ((trunkgroup < 1) || (channel < 1)) + return RESULT_SHOWUSAGE; + for (x = 0; x < NUM_SPANS; x++) { + if (pris[x].trunkgroup == trunkgroup) { + pri = pris + x; + break; + } } - } - if (pri) { - start = pri->crvs; - lock = &pri->lock; - } else { - ast_cli(fd, "No such trunk group %d\n", trunkgroup); - return RESULT_FAILURE; - } - } else -#endif - channel = atoi(argv[3]); - - ast_mutex_lock(lock); - tmp = start; - while (tmp) { - if (tmp->channel == channel) { -#ifdef HAVE_PRI - if (pri) - ast_cli(fd, "Trunk/CRV: %d/%d\n", trunkgroup, tmp->channel); - else -#endif - ast_cli(fd, "Channel: %d\n", tmp->channel); - ast_cli(fd, "File Descriptor: %d\n", tmp->subs[SUB_REAL].dfd); - ast_cli(fd, "Span: %d\n", tmp->span); - ast_cli(fd, "Extension: %s\n", tmp->exten); - ast_cli(fd, "Dialing: %s\n", tmp->dialing ? "yes" : "no"); - ast_cli(fd, "Context: %s\n", tmp->context); - ast_cli(fd, "Caller ID: %s\n", tmp->cid_num); - ast_cli(fd, "Calling TON: %d\n", tmp->cid_ton); - ast_cli(fd, "Caller ID name: %s\n", tmp->cid_name); - ast_cli(fd, "Destroy: %d\n", tmp->destroy); - ast_cli(fd, "InAlarm: %d\n", tmp->inalarm); - ast_cli(fd, "Signalling Type: %s\n", sig2str(tmp->sig)); - ast_cli(fd, "Radio: %d\n", tmp->radio); - ast_cli(fd, "Owner: %s\n", tmp->owner ? tmp->owner->name : ""); - ast_cli(fd, "Real: %s%s%s\n", tmp->subs[SUB_REAL].owner ? tmp->subs[SUB_REAL].owner->name : "", tmp->subs[SUB_REAL].inthreeway ? " (Confed)" : "", tmp->subs[SUB_REAL].linear ? " (Linear)" : ""); - ast_cli(fd, "Callwait: %s%s%s\n", tmp->subs[SUB_CALLWAIT].owner ? tmp->subs[SUB_CALLWAIT].owner->name : "", tmp->subs[SUB_CALLWAIT].inthreeway ? " (Confed)" : "", tmp->subs[SUB_CALLWAIT].linear ? " (Linear)" : ""); - ast_cli(fd, "Threeway: %s%s%s\n", tmp->subs[SUB_THREEWAY].owner ? tmp->subs[SUB_THREEWAY].owner->name : "", tmp->subs[SUB_THREEWAY].inthreeway ? " (Confed)" : "", tmp->subs[SUB_THREEWAY].linear ? " (Linear)" : ""); - ast_cli(fd, "Confno: %d\n", tmp->confno); - ast_cli(fd, "Propagated Conference: %d\n", tmp->propconfno); - ast_cli(fd, "Real in conference: %d\n", tmp->inconference); - ast_cli(fd, "DSP: %s\n", tmp->dsp ? "yes" : "no"); - ast_cli(fd, "Relax DTMF: %s\n", tmp->dtmfrelax ? "yes" : "no"); - ast_cli(fd, "Dialing/CallwaitCAS: %d/%d\n", tmp->dialing, tmp->callwaitcas); - ast_cli(fd, "Default law: %s\n", tmp->law == DAHDI_LAW_MULAW ? "ulaw" : tmp->law == DAHDI_LAW_ALAW ? "alaw" : "unknown"); - ast_cli(fd, "Fax Handled: %s\n", tmp->faxhandled ? "yes" : "no"); - ast_cli(fd, "Pulse phone: %s\n", tmp->pulsedial ? "yes" : "no"); - ast_cli(fd, "Echo Cancellation: %d taps%s, currently %s\n", tmp->echocancel, tmp->echocanbridged ? "" : " unless TDM bridged", tmp->echocanon ? "ON" : "OFF"); - if (tmp->master) - ast_cli(fd, "Master Channel: %d\n", tmp->master->channel); - for (x = 0; x < MAX_SLAVES; x++) { - if (tmp->slaves[x]) - ast_cli(fd, "Slave Channel: %d\n", tmp->slaves[x]->channel); + if (pri) { + start = pri->crvs; + lock = &pri->lock; + } else { + ast_cli(fd, "No such trunk group %d\n", trunkgroup); + return RESULT_FAILURE; } -#ifdef HAVE_PRI - if (tmp->pri) { - ast_cli(fd, "PRI Flags: "); - if (tmp->resetting) - ast_cli(fd, "Resetting "); - if (tmp->call) - ast_cli(fd, "Call "); - if (tmp->bearer) - ast_cli(fd, "Bearer "); - ast_cli(fd, "\n"); - if (tmp->logicalspan) - ast_cli(fd, "PRI Logical Span: %d\n", tmp->logicalspan); + } else + #endif + channel = atoi(argv[3]); + + ast_mutex_lock(lock); + tmp = start; + while (tmp) { + if (tmp->channel == channel) { + #ifdef HAVE_PRI + if (pri) + ast_cli(fd, "Trunk/CRV: %d/%d\n", trunkgroup, tmp->channel); else - ast_cli(fd, "PRI Logical Span: Implicit\n"); - } - -#endif - memset(&ci, 0, sizeof(ci)); - ps.channo = tmp->channel; - if (tmp->subs[SUB_REAL].dfd > -1) { - if (!ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GETCONF, &ci)) { - ast_cli(fd, "Actual Confinfo: Num/%d, Mode/0x%04x\n", ci.confno, ci.confmode); + #endif + ast_cli(fd, "Channel: %d\n", tmp->channel); + ast_cli(fd, "File Descriptor: %d\n", tmp->subs[SUB_REAL].dfd); + ast_cli(fd, "Span: %d\n", tmp->span); + ast_cli(fd, "Extension: %s\n", tmp->exten); + ast_cli(fd, "Dialing: %s\n", tmp->dialing ? "yes" : "no"); + ast_cli(fd, "Context: %s\n", tmp->context); + ast_cli(fd, "Caller ID: %s\n", tmp->cid_num); + ast_cli(fd, "Calling TON: %d\n", tmp->cid_ton); + ast_cli(fd, "Caller ID name: %s\n", tmp->cid_name); + ast_cli(fd, "Destroy: %d\n", tmp->destroy); + ast_cli(fd, "InAlarm: %d\n", tmp->inalarm); + ast_cli(fd, "Signalling Type: %s\n", sig2str(tmp->sig)); + ast_cli(fd, "Radio: %d\n", tmp->radio); + ast_cli(fd, "Owner: %s\n", tmp->owner ? tmp->owner->name : ""); + ast_cli(fd, "Real: %s%s%s\n", tmp->subs[SUB_REAL].owner ? tmp->subs[SUB_REAL].owner->name : "", tmp->subs[SUB_REAL].inthreeway ? " (Confed)" : "", tmp->subs[SUB_REAL].linear ? " (Linear)" : ""); + ast_cli(fd, "Callwait: %s%s%s\n", tmp->subs[SUB_CALLWAIT].owner ? tmp->subs[SUB_CALLWAIT].owner->name : "", tmp->subs[SUB_CALLWAIT].inthreeway ? " (Confed)" : "", tmp->subs[SUB_CALLWAIT].linear ? " (Linear)" : ""); + ast_cli(fd, "Threeway: %s%s%s\n", tmp->subs[SUB_THREEWAY].owner ? tmp->subs[SUB_THREEWAY].owner->name : "", tmp->subs[SUB_THREEWAY].inthreeway ? " (Confed)" : "", tmp->subs[SUB_THREEWAY].linear ? " (Linear)" : ""); + ast_cli(fd, "Confno: %d\n", tmp->confno); + ast_cli(fd, "Propagated Conference: %d\n", tmp->propconfno); + ast_cli(fd, "Real in conference: %d\n", tmp->inconference); + ast_cli(fd, "DSP: %s\n", tmp->dsp ? "yes" : "no"); + ast_cli(fd, "Relax DTMF: %s\n", tmp->dtmfrelax ? "yes" : "no"); + ast_cli(fd, "Dialing/CallwaitCAS: %d/%d\n", tmp->dialing, tmp->callwaitcas); + ast_cli(fd, "Default law: %s\n", tmp->law == DAHDI_LAW_MULAW ? "ulaw" : tmp->law == DAHDI_LAW_ALAW ? "alaw" : "unknown"); + ast_cli(fd, "Fax Handled: %s\n", tmp->faxhandled ? "yes" : "no"); + ast_cli(fd, "Pulse phone: %s\n", tmp->pulsedial ? "yes" : "no"); + ast_cli(fd, "Echo Cancellation: %d taps%s, currently %s\n", tmp->echocancel, tmp->echocanbridged ? "" : " unless TDM bridged", tmp->echocanon ? "ON" : "OFF"); + if (tmp->master) + ast_cli(fd, "Master Channel: %d\n", tmp->master->channel); + for (x = 0; x < MAX_SLAVES; x++) { + if (tmp->slaves[x]) + ast_cli(fd, "Slave Channel: %d\n", tmp->slaves[x]->channel); } -#ifdef DAHDI_GETCONFMUTE - if (!ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GETCONFMUTE, &x)) { - ast_cli(fd, "Actual Confmute: %s\n", x ? "Yes" : "No"); + #ifdef HAVE_PRI + if (tmp->pri) { + ast_cli(fd, "PRI Flags: "); + if (tmp->resetting) + ast_cli(fd, "Resetting "); + if (tmp->call) + ast_cli(fd, "Call "); + if (tmp->bearer) + ast_cli(fd, "Bearer "); + ast_cli(fd, "\n"); + if (tmp->logicalspan) + ast_cli(fd, "PRI Logical Span: %d\n", tmp->logicalspan); + else + ast_cli(fd, "PRI Logical Span: Implicit\n"); + if (tmp->pri->h324musellc == 1) + ast_cli(fd, "H324M Signalling: use 'Low Layer Compatibility' IE\n"); + else + ast_cli(fd, "H324M Signalling: use 'Bearer Capability' IE\n"); } -#endif - if (ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &ps) < 0) { - ast_log(LOG_WARNING, "Failed to get parameters on channel %d: %s\n", tmp->channel, strerror(errno)); - } else { - ast_cli(fd, "Hookstate (FXS only): %s\n", ps.rxisoffhook ? "Offhook" : "Onhook"); + + #endif + memset(&ci, 0, sizeof(ci)); + ps.channo = tmp->channel; + if (tmp->subs[SUB_REAL].dfd > -1) { + if (!ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GETCONF, &ci)) { + ast_cli(fd, "Actual Confinfo: Num/%d, Mode/0x%04x\n", ci.confno, ci.confmode); + } + #ifdef DAHDI_GETCONFMUTE + if (!ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GETCONFMUTE, &x)) { + ast_cli(fd, "Actual Confmute: %s\n", x ? "Yes" : "No"); + } + #endif + if (ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &ps) < 0) { + ast_log(LOG_WARNING, "Failed to get parameters on channel %d: %s\n", tmp->channel, strerror(errno)); + } else { + ast_cli(fd, "Hookstate (FXS only): %s\n", ps.rxisoffhook ? "Offhook" : "Onhook"); + } } + ast_mutex_unlock(lock); + return RESULT_SUCCESS; } - ast_mutex_unlock(lock); - return RESULT_SUCCESS; + tmp = tmp->next; } - tmp = tmp->next; + + ast_cli(fd, "Unable to find given channel %d\n", channel); + ast_mutex_unlock(lock); + return RESULT_FAILURE; } - ast_cli(fd, "Unable to find given channel %d\n", channel); - ast_mutex_unlock(lock); - return RESULT_FAILURE; -} - -static char dahdi_show_cadences_usage[] = -"Usage: dahdi show cadences\n" -" Shows all cadences currently defined\n"; - -static int handle_dahdi_show_cadences(int fd, int argc, char *argv[]) -{ - int i, j; - for (i = 0; i < num_cadence; i++) { - char output[1024]; - char tmp[16], tmp2[64]; - snprintf(tmp, sizeof(tmp), "r%d: ", i + 1); - term_color(output, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(output)); - - for (j = 0; j < 16; j++) { - if (cadences[i].ringcadence[j] == 0) - break; - snprintf(tmp, sizeof(tmp), "%d", cadences[i].ringcadence[j]); - if (cidrings[i] * 2 - 1 == j) - term_color(tmp2, tmp, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp2) - 1); - else - term_color(tmp2, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(tmp2) - 1); - if (j != 0) - strncat(output, ",", sizeof(output) - strlen(output) - 1); - strncat(output, tmp2, sizeof(output) - strlen(output) - 1); + static char dahdi_show_cadences_usage[] = + "Usage: dahdi show cadences\n" + " Shows all cadences currently defined\n"; + + static int handle_dahdi_show_cadences(int fd, int argc, char *argv[]) + { + int i, j; + for (i = 0; i < num_cadence; i++) { + char output[1024]; + char tmp[16], tmp2[64]; + snprintf(tmp, sizeof(tmp), "r%d: ", i + 1); + term_color(output, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(output)); + + for (j = 0; j < 16; j++) { + if (cadences[i].ringcadence[j] == 0) + break; + snprintf(tmp, sizeof(tmp), "%d", cadences[i].ringcadence[j]); + if (cidrings[i] * 2 - 1 == j) + term_color(tmp2, tmp, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp2) - 1); + else + term_color(tmp2, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(tmp2) - 1); + if (j != 0) + strncat(output, ",", sizeof(output) - strlen(output) - 1); + strncat(output, tmp2, sizeof(output) - strlen(output) - 1); + } + ast_cli(fd,"%s\n",output); } - ast_cli(fd,"%s\n",output); - } - return 0; -} - -/* Based on irqmiss.c */ -static int dahdi_show_status(int fd, int argc, char *argv[]) { - #define FORMAT "%-40.40s %-10.10s %-10d %-10d %-10d\n" - #define FORMAT2 "%-40.40s %-10.10s %-10.10s %-10.10s %-10.10s\n" - - int span; - int res; - char alarms[50]; - - int ctl; - struct dahdi_spaninfo s; - -#ifdef HAVE_ZAPTEL - if ((ctl = open("/dev/zap/ctl", O_RDWR)) < 0) { - ast_log(LOG_WARNING, "Unable to open /dev/zap/ctl: %s\n", strerror(errno)); - ast_cli(fd, "No Zaptel interface found.\n"); - return RESULT_FAILURE; - } -#else - if ((ctl = open("/dev/dahdi/ctl", O_RDWR)) < 0) { - ast_log(LOG_WARNING, "Unable to open /dev/dahdi/ctl: %s\n", strerror(errno)); - ast_cli(fd, "No DAHDI interface found.\n"); - return RESULT_FAILURE; + return 0; } -#endif - ast_cli(fd, FORMAT2, "Description", "Alarms", "IRQ", "bpviol", "CRC4"); - - for (span = 1; span < DAHDI_MAX_SPANS; ++span) { - s.spanno = span; - res = ioctl(ctl, DAHDI_SPANSTAT, &s); - if (res) { - continue; + + /* Based on irqmiss.c */ + static int dahdi_show_status(int fd, int argc, char *argv[]) { + #define FORMAT "%-40.40s %-10.10s %-10d %-10d %-10d\n" + #define FORMAT2 "%-40.40s %-10.10s %-10.10s %-10.10s %-10.10s\n" + + int span; + int res; + char alarms[50]; + + int ctl; + struct dahdi_spaninfo s; + + #ifdef HAVE_ZAPTEL + if ((ctl = open("/dev/zap/ctl", O_RDWR)) < 0) { + ast_log(LOG_WARNING, "Unable to open /dev/zap/ctl: %s\n", strerror(errno)); + ast_cli(fd, "No Zaptel interface found.\n"); + return RESULT_FAILURE; } - alarms[0] = '\0'; - if (s.alarms > 0) { - if (s.alarms & DAHDI_ALARM_BLUE) - strcat(alarms, "BLU/"); - if (s.alarms & DAHDI_ALARM_YELLOW) - strcat(alarms, "YEL/"); - if (s.alarms & DAHDI_ALARM_RED) - strcat(alarms, "RED/"); - if (s.alarms & DAHDI_ALARM_LOOPBACK) - strcat(alarms, "LB/"); - if (s.alarms & DAHDI_ALARM_RECOVER) - strcat(alarms, "REC/"); - if (s.alarms & DAHDI_ALARM_NOTOPEN) - strcat(alarms, "NOP/"); - if (!strlen(alarms)) - strcat(alarms, "UUU/"); - if (strlen(alarms)) { - /* Strip trailing / */ - alarms[strlen(alarms) - 1] = '\0'; + #else + if ((ctl = open("/dev/dahdi/ctl", O_RDWR)) < 0) { + ast_log(LOG_WARNING, "Unable to open /dev/dahdi/ctl: %s\n", strerror(errno)); + ast_cli(fd, "No DAHDI interface found.\n"); + return RESULT_FAILURE; + } + #endif + ast_cli(fd, FORMAT2, "Description", "Alarms", "IRQ", "bpviol", "CRC4"); + + for (span = 1; span < DAHDI_MAX_SPANS; ++span) { + s.spanno = span; + res = ioctl(ctl, DAHDI_SPANSTAT, &s); + if (res) { + continue; } - } else { - if (s.numchans) - strcpy(alarms, "OK"); - else - strcpy(alarms, "UNCONFIGURED"); + alarms[0] = '\0'; + if (s.alarms > 0) { + if (s.alarms & DAHDI_ALARM_BLUE) + strcat(alarms, "BLU/"); + if (s.alarms & DAHDI_ALARM_YELLOW) + strcat(alarms, "YEL/"); + if (s.alarms & DAHDI_ALARM_RED) + strcat(alarms, "RED/"); + if (s.alarms & DAHDI_ALARM_LOOPBACK) + strcat(alarms, "LB/"); + if (s.alarms & DAHDI_ALARM_RECOVER) + strcat(alarms, "REC/"); + if (s.alarms & DAHDI_ALARM_NOTOPEN) + strcat(alarms, "NOP/"); + if (!strlen(alarms)) + strcat(alarms, "UUU/"); + if (strlen(alarms)) { + /* Strip trailing / */ + alarms[strlen(alarms) - 1] = '\0'; + } + } else { + if (s.numchans) + strcpy(alarms, "OK"); + else + strcpy(alarms, "UNCONFIGURED"); + } + + ast_cli(fd, FORMAT, s.desc, alarms, s.irqmisses, s.bpvcount, s.crc4count); } - - ast_cli(fd, FORMAT, s.desc, alarms, s.irqmisses, s.bpvcount, s.crc4count); + close(ctl); + + return RESULT_SUCCESS; + #undef FORMAT + #undef FORMAT2 } - close(ctl); - - return RESULT_SUCCESS; -#undef FORMAT -#undef FORMAT2 -} - -static char show_channels_usage[] = - "Usage: dahdi show channels\n" - " Shows a list of available channels\n"; - -static char show_channel_usage[] = - "Usage: dahdi show channel \n" - " Detailed information about a given channel\n"; - -static char dahdi_show_status_usage[] = - "Usage: dahdi show status\n" - " Shows a list of DAHDI cards with status\n"; - -static char destroy_channel_usage[] = - "Usage: dahdi destroy channel \n" - " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING. Immediately removes a given channel, whether it is in use or not\n"; - -static char dahdi_restart_usage[] = - "Usage: dahdi restart\n" - " Restarts the DAHDI channels: destroys them all and then\n" - " re-reads them from chan_dahdi.conf.\n" - " Note that this will STOP any running CALL on DAHDI channels.\n" - ""; - -static struct ast_cli_entry cli_zap_show_cadences_deprecated = { - { "zap", "show", "cadences", NULL }, - handle_dahdi_show_cadences, NULL, - NULL }; - -static struct ast_cli_entry cli_zap_show_channels_deprecated = { - { "zap", "show", "channels", NULL }, - dahdi_show_channels, NULL, - NULL }; - -static struct ast_cli_entry cli_zap_show_channel_deprecated = { - { "zap", "show", "channel", NULL }, - dahdi_show_channel, NULL, - NULL }; - -static struct ast_cli_entry cli_zap_destroy_channel_deprecated = { - { "zap", "destroy", "channel", NULL }, - dahdi_destroy_channel, NULL, - NULL }; - -static struct ast_cli_entry cli_zap_restart_deprecated = { - { "zap", "restart", NULL }, - dahdi_restart_cmd, NULL, - NULL }; - -static struct ast_cli_entry cli_zap_show_status_deprecated = { - { "zap", "show", "status", NULL }, - dahdi_show_status, NULL, - NULL }; - -static struct ast_cli_entry dahdi_cli[] = { - { { "dahdi", "show", "cadences", NULL }, - handle_dahdi_show_cadences, "List cadences", - dahdi_show_cadences_usage, NULL, &cli_zap_show_cadences_deprecated }, - - { { "dahdi", "show", "channels", NULL}, - dahdi_show_channels, "Show active DAHDI channels", - show_channels_usage, NULL, &cli_zap_show_channels_deprecated }, - - { { "dahdi", "show", "channel", NULL}, - dahdi_show_channel, "Show information on a channel", - show_channel_usage, NULL, &cli_zap_show_channel_deprecated }, - - { { "dahdi", "destroy", "channel", NULL}, - dahdi_destroy_channel, "Destroy a channel", - destroy_channel_usage, NULL, &cli_zap_destroy_channel_deprecated }, - - { { "dahdi", "restart", NULL}, - dahdi_restart_cmd, "Fully restart DAHDI channels", - dahdi_restart_usage, NULL, &cli_zap_restart_deprecated }, - - { { "dahdi", "show", "status", NULL}, - dahdi_show_status, "Show all DAHDI cards status", - dahdi_show_status_usage, NULL, &cli_zap_show_status_deprecated }, -}; - -#define TRANSFER 0 -#define HANGUP 1 - -static int dahdi_fake_event(struct dahdi_pvt *p, int mode) -{ - if (p) { - switch (mode) { - case TRANSFER: - p->fake_event = DAHDI_EVENT_WINKFLASH; - break; - case HANGUP: - p->fake_event = DAHDI_EVENT_ONHOOK; - break; - default: - ast_log(LOG_WARNING, "I don't know how to handle transfer event with this: %d on channel %s\n",mode, p->owner->name); + + static char show_channels_usage[] = + "Usage: dahdi show channels\n" + " Shows a list of available channels\n"; + + static char show_channel_usage[] = + "Usage: dahdi show channel \n" + " Detailed information about a given channel\n"; + + static char dahdi_show_status_usage[] = + "Usage: dahdi show status\n" + " Shows a list of DAHDI cards with status\n"; + + static char destroy_channel_usage[] = + "Usage: dahdi destroy channel \n" + " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING. Immediately removes a given channel, whether it is in use or not\n"; + + static char dahdi_restart_usage[] = + "Usage: dahdi restart\n" + " Restarts the DAHDI channels: destroys them all and then\n" + " re-reads them from chan_dahdi.conf.\n" + " Note that this will STOP any running CALL on DAHDI channels.\n" + ""; + + static struct ast_cli_entry cli_zap_show_cadences_deprecated = { + { "zap", "show", "cadences", NULL }, + handle_dahdi_show_cadences, NULL, + NULL }; + + static struct ast_cli_entry cli_zap_show_channels_deprecated = { + { "zap", "show", "channels", NULL }, + dahdi_show_channels, NULL, + NULL }; + + static struct ast_cli_entry cli_zap_show_channel_deprecated = { + { "zap", "show", "channel", NULL }, + dahdi_show_channel, NULL, + NULL }; + + static struct ast_cli_entry cli_zap_destroy_channel_deprecated = { + { "zap", "destroy", "channel", NULL }, + dahdi_destroy_channel, NULL, + NULL }; + + static struct ast_cli_entry cli_zap_restart_deprecated = { + { "zap", "restart", NULL }, + dahdi_restart_cmd, NULL, + NULL }; + + static struct ast_cli_entry cli_zap_show_status_deprecated = { + { "zap", "show", "status", NULL }, + dahdi_show_status, NULL, + NULL }; + + static struct ast_cli_entry dahdi_cli[] = { + { { "dahdi", "show", "cadences", NULL }, + handle_dahdi_show_cadences, "List cadences", + dahdi_show_cadences_usage, NULL, &cli_zap_show_cadences_deprecated }, + + { { "dahdi", "show", "channels", NULL}, + dahdi_show_channels, "Show active DAHDI channels", + show_channels_usage, NULL, &cli_zap_show_channels_deprecated }, + + { { "dahdi", "show", "channel", NULL}, + dahdi_show_channel, "Show information on a channel", + show_channel_usage, NULL, &cli_zap_show_channel_deprecated }, + + { { "dahdi", "destroy", "channel", NULL}, + dahdi_destroy_channel, "Destroy a channel", + destroy_channel_usage, NULL, &cli_zap_destroy_channel_deprecated }, + + { { "dahdi", "restart", NULL}, + dahdi_restart_cmd, "Fully restart DAHDI channels", + dahdi_restart_usage, NULL, &cli_zap_restart_deprecated }, + + { { "dahdi", "show", "status", NULL}, + dahdi_show_status, "Show all DAHDI cards status", + dahdi_show_status_usage, NULL, &cli_zap_show_status_deprecated }, + }; + + #define TRANSFER 0 + #define HANGUP 1 + + static int dahdi_fake_event(struct dahdi_pvt *p, int mode) + { + if (p) { + switch (mode) { + case TRANSFER: + p->fake_event = DAHDI_EVENT_WINKFLASH; + break; + case HANGUP: + p->fake_event = DAHDI_EVENT_ONHOOK; + break; + default: + ast_log(LOG_WARNING, "I don't know how to handle transfer event with this: %d on channel %s\n",mode, p->owner->name); + } } + return 0; } - return 0; -} -static struct dahdi_pvt *find_channel(int channel) -{ - struct dahdi_pvt *p = iflist; - while (p) { - if (p->channel == channel) { - break; + static struct dahdi_pvt *find_channel(int channel) + { + struct dahdi_pvt *p = iflist; + while (p) { + if (p->channel == channel) { + break; + } + p = p->next; } - p = p->next; + return p; } - return p; -} - -#define local_astman_ack(s, m, msg, zap) do { if (!zap) astman_send_ack(s, m, "DAHDI" msg); else astman_send_ack(s, m, "Zap" msg); } while (0) -#define local_astman_header(m, hdr, zap) astman_get_header(m, (!zap) ? "DAHDI" hdr : "Zap" hdr) - -static int __action_dnd(struct mansession *s, const struct message *m, int zap_mode, int dnd) -{ - struct dahdi_pvt *p = NULL; - const char *channel = local_astman_header(m, "Channel", zap_mode); - - if (ast_strlen_zero(channel)) { - astman_send_error(s, m, "No channel specified"); + + #define local_astman_ack(s, m, msg, zap) do { if (!zap) astman_send_ack(s, m, "DAHDI" msg); else astman_send_ack(s, m, "Zap" msg); } while (0) + #define local_astman_header(m, hdr, zap) astman_get_header(m, (!zap) ? "DAHDI" hdr : "Zap" hdr) + + static int __action_dnd(struct mansession *s, const struct message *m, int zap_mode, int dnd) + { + struct dahdi_pvt *p = NULL; + const char *channel = local_astman_header(m, "Channel", zap_mode); + + if (ast_strlen_zero(channel)) { + astman_send_error(s, m, "No channel specified"); + return 0; + } + if (!(p = find_channel(atoi(channel)))) { + astman_send_error(s, m, "No such channel"); + return 0; + } + p->dnd = dnd; + local_astman_ack(s, m, "DND", zap_mode); + return 0; } - if (!(p = find_channel(atoi(channel)))) { - astman_send_error(s, m, "No such channel"); - return 0; + + static int zap_action_dndon(struct mansession *s, const struct message *m) + { + return __action_dnd(s, m, 1, 1); } - p->dnd = dnd; - local_astman_ack(s, m, "DND", zap_mode); - - return 0; -} - -static int zap_action_dndon(struct mansession *s, const struct message *m) -{ - return __action_dnd(s, m, 1, 1); -} - -static int dahdi_action_dndon(struct mansession *s, const struct message *m) -{ - return __action_dnd(s, m, 0, 1); -} - -static int zap_action_dndoff(struct mansession *s, const struct message *m) -{ - return __action_dnd(s, m, 1, 0); -} - -static int dahdi_action_dndoff(struct mansession *s, const struct message *m) -{ - return __action_dnd(s, m, 0, 0); -} - -static int __action_transfer(struct mansession *s, const struct message *m, int zap_mode) -{ - struct dahdi_pvt *p = NULL; - const char *channel = local_astman_header(m, "Channel", zap_mode); - - if (ast_strlen_zero(channel)) { - astman_send_error(s, m, "No channel specified"); - return 0; + + static int dahdi_action_dndon(struct mansession *s, const struct message *m) + { + return __action_dnd(s, m, 0, 1); } - if (!(p = find_channel(atoi(channel)))) { - astman_send_error(s, m, "No such channel"); - return 0; + + static int zap_action_dndoff(struct mansession *s, const struct message *m) + { + return __action_dnd(s, m, 1, 0); } - dahdi_fake_event(p,TRANSFER); - local_astman_ack(s, m, "Transfer", zap_mode); - - return 0; -} - -static int zap_action_transfer(struct mansession *s, const struct message *m) -{ - return __action_transfer(s, m, 1); -} - -static int dahdi_action_transfer(struct mansession *s, const struct message *m) -{ - return __action_transfer(s, m, 0); -} - -static int __action_transferhangup(struct mansession *s, const struct message *m, int zap_mode) -{ - struct dahdi_pvt *p = NULL; - const char *channel = local_astman_header(m, "Channel", zap_mode); - - if (ast_strlen_zero(channel)) { - astman_send_error(s, m, "No channel specified"); - return 0; + + static int dahdi_action_dndoff(struct mansession *s, const struct message *m) + { + return __action_dnd(s, m, 0, 0); } - if (!(p = find_channel(atoi(channel)))) { - astman_send_error(s, m, "No such channel"); + + static int __action_transfer(struct mansession *s, const struct message *m, int zap_mode) + { + struct dahdi_pvt *p = NULL; + const char *channel = local_astman_header(m, "Channel", zap_mode); + + if (ast_strlen_zero(channel)) { + astman_send_error(s, m, "No channel specified"); + return 0; + } + if (!(p = find_channel(atoi(channel)))) { + astman_send_error(s, m, "No such channel"); + return 0; + } + dahdi_fake_event(p,TRANSFER); + local_astman_ack(s, m, "Transfer", zap_mode); + return 0; } - dahdi_fake_event(p, HANGUP); - local_astman_ack(s, m, "Hangup", zap_mode); - return 0; -} - -static int zap_action_transferhangup(struct mansession *s, const struct message *m) -{ - return __action_transferhangup(s, m, 1); -} - -static int dahdi_action_transferhangup(struct mansession *s, const struct message *m) -{ - return __action_transferhangup(s, m, 0); -} - -static int __action_dialoffhook(struct mansession *s, const struct message *m, int zap_mode) -{ - struct dahdi_pvt *p = NULL; - const char *channel = local_astman_header(m, "Channel", zap_mode); - const char *number = astman_get_header(m, "Number"); - int i; - - if (ast_strlen_zero(channel)) { - astman_send_error(s, m, "No channel specified"); - return 0; + + static int zap_action_transfer(struct mansession *s, const struct message *m) + { + return __action_transfer(s, m, 1); + } + + static int dahdi_action_transfer(struct mansession *s, const struct message *m) + { + return __action_transfer(s, m, 0); } - if (ast_strlen_zero(number)) { - astman_send_error(s, m, "No number specified"); + + static int __action_transferhangup(struct mansession *s, const struct message *m, int zap_mode) + { + struct dahdi_pvt *p = NULL; + const char *channel = local_astman_header(m, "Channel", zap_mode); + + if (ast_strlen_zero(channel)) { + astman_send_error(s, m, "No channel specified"); + return 0; + } + if (!(p = find_channel(atoi(channel)))) { + astman_send_error(s, m, "No such channel"); + return 0; + } + dahdi_fake_event(p, HANGUP); + local_astman_ack(s, m, "Hangup", zap_mode); return 0; } - if (!(p = find_channel(atoi(channel)))) { - astman_send_error(s, m, "No such channel"); + + static int zap_action_transferhangup(struct mansession *s, const struct message *m) + { + return __action_transferhangup(s, m, 1); + } + + static int dahdi_action_transferhangup(struct mansession *s, const struct message *m) + { + return __action_transferhangup(s, m, 0); + } + + static int __action_dialoffhook(struct mansession *s, const struct message *m, int zap_mode) + { + struct dahdi_pvt *p = NULL; + const char *channel = local_astman_header(m, "Channel", zap_mode); + const char *number = astman_get_header(m, "Number"); + int i; + + if (ast_strlen_zero(channel)) { + astman_send_error(s, m, "No channel specified"); + return 0; + } + if (ast_strlen_zero(number)) { + astman_send_error(s, m, "No number specified"); + return 0; + } + if (!(p = find_channel(atoi(channel)))) { + astman_send_error(s, m, "No such channel"); + return 0; + } + if (!p->owner) { + astman_send_error(s, m, "Channel does not have an owner"); + return 0; + } + for (i = 0; i < strlen(number); i++) { + struct ast_frame f = { AST_FRAME_DTMF, number[i] }; + + dahdi_queue_frame(p, &f, NULL); + } + local_astman_ack(s, m, "DialOffHook", zap_mode); + return 0; } - if (!p->owner) { - astman_send_error(s, m, "Channel does not have an owner"); + + static int zap_action_dialoffhook(struct mansession *s, const struct message *m) + { + return __action_dialoffhook(s, m, 1); + } + + static int dahdi_action_dialoffhook(struct mansession *s, const struct message *m) + { + return __action_dialoffhook(s, m, 0); + } + + static int __action_showchannels(struct mansession *s, const struct message *m, int zap_mode) + { + struct dahdi_pvt *tmp = NULL; + const char *id = astman_get_header(m, "ActionID"); + char idText[256] = ""; + + local_astman_ack(s, m, " channel status will follow", zap_mode); + if (!ast_strlen_zero(id)) + snprintf(idText, sizeof(idText) - 1, "ActionID: %s\r\n", id); + + ast_mutex_lock(&iflock); + + tmp = iflist; + while (tmp) { + if (tmp->channel > 0) { + int alarm = get_alarms(tmp); + astman_append(s, + "Event: %sShowChannels\r\n" + "Channel: %d\r\n" + "Signalling: %s\r\n" + "Context: %s\r\n" + "DND: %s\r\n" + "Alarm: %s\r\n" + "%s" + "\r\n", + dahdi_chan_name, + tmp->channel, sig2str(tmp->sig), tmp->context, + tmp->dnd ? "Enabled" : "Disabled", + alarm2str(alarm), idText); + } + + tmp = tmp->next; + } + + ast_mutex_unlock(&iflock); + + astman_append(s, + "Event: %sShowChannelsComplete\r\n" + "%s" + "\r\n", + dahdi_chan_name, + idText); return 0; } - for (i = 0; i < strlen(number); i++) { - struct ast_frame f = { AST_FRAME_DTMF, number[i] }; - - dahdi_queue_frame(p, &f, NULL); + + static int zap_action_showchannels(struct mansession *s, const struct message *m) + { + return __action_showchannels(s, m, 1); } - local_astman_ack(s, m, "DialOffHook", zap_mode); - - return 0; -} - -static int zap_action_dialoffhook(struct mansession *s, const struct message *m) -{ - return __action_dialoffhook(s, m, 1); -} - -static int dahdi_action_dialoffhook(struct mansession *s, const struct message *m) -{ - return __action_dialoffhook(s, m, 0); -} - -static int __action_showchannels(struct mansession *s, const struct message *m, int zap_mode) -{ - struct dahdi_pvt *tmp = NULL; - const char *id = astman_get_header(m, "ActionID"); - char idText[256] = ""; - - local_astman_ack(s, m, " channel status will follow", zap_mode); - if (!ast_strlen_zero(id)) - snprintf(idText, sizeof(idText) - 1, "ActionID: %s\r\n", id); - - ast_mutex_lock(&iflock); - tmp = iflist; - while (tmp) { - if (tmp->channel > 0) { - int alarm = get_alarms(tmp); - astman_append(s, - "Event: %sShowChannels\r\n" - "Channel: %d\r\n" - "Signalling: %s\r\n" - "Context: %s\r\n" - "DND: %s\r\n" - "Alarm: %s\r\n" - "%s" - "\r\n", - dahdi_chan_name, - tmp->channel, sig2str(tmp->sig), tmp->context, - tmp->dnd ? "Enabled" : "Disabled", - alarm2str(alarm), idText); - } - - tmp = tmp->next; + static int dahdi_action_showchannels(struct mansession *s, const struct message *m) + { + return __action_showchannels(s, m, 0); } - - ast_mutex_unlock(&iflock); - astman_append(s, - "Event: %sShowChannelsComplete\r\n" - "%s" - "\r\n", - dahdi_chan_name, - idText); - return 0; -} - -static int zap_action_showchannels(struct mansession *s, const struct message *m) -{ - return __action_showchannels(s, m, 1); -} - -static int dahdi_action_showchannels(struct mansession *s, const struct message *m) -{ - return __action_showchannels(s, m, 0); -} - -static int __action_restart(struct mansession *s, const struct message *m, int zap_mode) -{ - if (dahdi_restart() != 0) { - if (zap_mode) { - astman_send_error(s, m, "Failed to restart Zap"); - } else { - astman_send_error(s, m, "Failed to restart DAHDI"); + static int __action_restart(struct mansession *s, const struct message *m, int zap_mode) + { + if (dahdi_restart() != 0) { + if (zap_mode) { + astman_send_error(s, m, "Failed to restart Zap"); + } else { + astman_send_error(s, m, "Failed to restart DAHDI"); + } + return 1; } - return 1; - } - local_astman_ack(s, m, "Restart: Success", zap_mode); - return 0; -} - -static int zap_action_restart(struct mansession *s, const struct message *m) -{ - return __action_restart(s, m, 1); -} - -static int dahdi_action_restart(struct mansession *s, const struct message *m) -{ - return __action_restart(s, m, 0); -} - -#define local_astman_unregister(a) do { \ - if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) { \ - ast_manager_unregister("DAHDI" a); \ - } \ - ast_manager_unregister("Zap" a); \ - } while (0) - -static int __unload_module(void) -{ - struct dahdi_pvt *p; - -#ifdef HAVE_PRI - int i, j; - for (i = 0; i < NUM_SPANS; i++) { - if (pris[i].master != AST_PTHREADT_NULL) - pthread_cancel(pris[i].master); - } - ast_cli_unregister_multiple(dahdi_pri_cli, sizeof(dahdi_pri_cli) / sizeof(struct ast_cli_entry)); - - if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) { - ast_unregister_application(dahdi_send_keypad_facility_app); - } - ast_unregister_application(zap_send_keypad_facility_app); -#endif - ast_cli_unregister_multiple(dahdi_cli, sizeof(dahdi_cli) / sizeof(struct ast_cli_entry)); - local_astman_unregister("DialOffHook"); - local_astman_unregister("Hangup"); - local_astman_unregister("Transfer"); - local_astman_unregister("DNDoff"); - local_astman_unregister("DNDon"); - local_astman_unregister("ShowChannels"); - local_astman_unregister("Restart"); - ast_channel_unregister(chan_tech); - ast_mutex_lock(&iflock); - /* Hangup all interfaces if they have an owner */ - p = iflist; - while (p) { - if (p->owner) - ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD); - p = p->next; + local_astman_ack(s, m, "Restart: Success", zap_mode); + return 0; } - ast_mutex_unlock(&iflock); - ast_mutex_lock(&monlock); - if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) { - pthread_cancel(monitor_thread); - pthread_kill(monitor_thread, SIGURG); - pthread_join(monitor_thread, NULL); - } - monitor_thread = AST_PTHREADT_STOP; - ast_mutex_unlock(&monlock); - - destroy_all_channels(); -#ifdef HAVE_PRI - for (i = 0; i < NUM_SPANS; i++) { - if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL)) - pthread_join(pris[i].master, NULL); - for (j = 0; j < NUM_DCHANS; j++) { - dahdi_close_pri_fd(&(pris[i]), j); - } + + static int zap_action_restart(struct mansession *s, const struct message *m) + { + return __action_restart(s, m, 1); } -#endif - ast_cond_destroy(&ss_thread_complete); - return 0; -} - -static int unload_module(void) -{ -#ifdef HAVE_PRI - int y; - for (y = 0; y < NUM_SPANS; y++) - ast_mutex_destroy(&pris[y].lock); -#endif - return __unload_module(); -} - -static int build_channels(struct dahdi_chan_conf *conf, int iscrv, const char *value, int reload, int lineno, int *found_pseudo) -{ - char *c, *chan; - int x, start, finish; - struct dahdi_pvt *tmp; -#ifdef HAVE_PRI - struct dahdi_pri *pri; - int trunkgroup, y; -#endif - if ((reload == 0) && (conf->chan.sig < 0)) { - ast_log(LOG_ERROR, "Signalling must be specified before any channels are.\n"); - return -1; + static int dahdi_action_restart(struct mansession *s, const struct message *m) + { + return __action_restart(s, m, 0); } - - c = ast_strdupa(value); - -#ifdef HAVE_PRI - pri = NULL; - if (iscrv) { - if (sscanf(c, "%d:%n", &trunkgroup, &y) != 1) { - ast_log(LOG_WARNING, "CRV must begin with trunkgroup followed by a colon at line %d\n", lineno); - return -1; + + #define local_astman_unregister(a) do { \ + if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) { \ + ast_manager_unregister("DAHDI" a); \ + } \ + ast_manager_unregister("Zap" a); \ + } while (0) + + static int __unload_module(void) + { + struct dahdi_pvt *p; + + #ifdef HAVE_PRI + int i, j; + for (i = 0; i < NUM_SPANS; i++) { + if (pris[i].master != AST_PTHREADT_NULL) + pthread_cancel(pris[i].master); } - if (trunkgroup < 1) { - ast_log(LOG_WARNING, "CRV trunk group must be a positive number at line %d\n", lineno); - return -1; + ast_cli_unregister_multiple(dahdi_pri_cli, sizeof(dahdi_pri_cli) / sizeof(struct ast_cli_entry)); + + if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) { + ast_unregister_application(dahdi_send_keypad_facility_app); } - c += y; - for (y = 0; y < NUM_SPANS; y++) { - if (pris[y].trunkgroup == trunkgroup) { - pri = pris + y; - break; - } + ast_unregister_application(zap_send_keypad_facility_app); + #endif + ast_cli_unregister_multiple(dahdi_cli, sizeof(dahdi_cli) / sizeof(struct ast_cli_entry)); + local_astman_unregister("DialOffHook"); + local_astman_unregister("Hangup"); + local_astman_unregister("Transfer"); + local_astman_unregister("DNDoff"); + local_astman_unregister("DNDon"); + local_astman_unregister("ShowChannels"); + local_astman_unregister("Restart"); + ast_channel_unregister(chan_tech); + ast_mutex_lock(&iflock); + /* Hangup all interfaces if they have an owner */ + p = iflist; + while (p) { + if (p->owner) + ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD); + p = p->next; } - if (!pri) { - ast_log(LOG_WARNING, "No such trunk group %d at CRV declaration at line %d\n", trunkgroup, lineno); - return -1; + ast_mutex_unlock(&iflock); + ast_mutex_lock(&monlock); + if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) { + pthread_cancel(monitor_thread); + pthread_kill(monitor_thread, SIGURG); + pthread_join(monitor_thread, NULL); + } + monitor_thread = AST_PTHREADT_STOP; + ast_mutex_unlock(&monlock); + + destroy_all_channels(); + #ifdef HAVE_PRI + for (i = 0; i < NUM_SPANS; i++) { + if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL)) + pthread_join(pris[i].master, NULL); + for (j = 0; j < NUM_DCHANS; j++) { + dahdi_close_pri_fd(&(pris[i]), j); + } } + #endif + ast_cond_destroy(&ss_thread_complete); + return 0; } -#endif - - while ((chan = strsep(&c, ","))) { - if (sscanf(chan, "%d-%d", &start, &finish) == 2) { - /* Range */ - } else if (sscanf(chan, "%d", &start)) { - /* Just one */ - finish = start; - } else if (!strcasecmp(chan, "pseudo")) { - finish = start = CHAN_PSEUDO; - if (found_pseudo) - *found_pseudo = 1; - } else { - ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'\n", value, chan); + + static int unload_module(void) + { + #ifdef HAVE_PRI + int y; + for (y = 0; y < NUM_SPANS; y++) + ast_mutex_destroy(&pris[y].lock); + #endif + return __unload_module(); + } + + static int build_channels(struct dahdi_chan_conf *conf, int iscrv, const char *value, int reload, int lineno, int *found_pseudo) + { + char *c, *chan; + int x, start, finish; + struct dahdi_pvt *tmp; + #ifdef HAVE_PRI + struct dahdi_pri *pri; + int trunkgroup, y; + #endif + + if ((reload == 0) && (conf->chan.sig < 0)) { + ast_log(LOG_ERROR, "Signalling must be specified before any channels are.\n"); return -1; } - if (finish < start) { - ast_log(LOG_WARNING, "Sillyness: %d < %d\n", start, finish); - x = finish; - finish = start; - start = x; - } - - for (x = start; x <= finish; x++) { -#ifdef HAVE_PRI - tmp = mkintf(x, conf, pri, reload); -#else - tmp = mkintf(x, conf, NULL, reload); -#endif - - if (tmp) { - if (option_verbose > 2) { -#ifdef HAVE_PRI - if (pri) - ast_verbose(VERBOSE_PREFIX_3 "%s CRV %d:%d, %s signalling\n", reload ? "Reconfigured" : "Registered", trunkgroup, x, sig2str(tmp->sig)); - else -#endif - ast_verbose(VERBOSE_PREFIX_3 "%s channel %d, %s signalling\n", reload ? "Reconfigured" : "Registered", x, sig2str(tmp->sig)); + + c = ast_strdupa(value); + + #ifdef HAVE_PRI + pri = NULL; + if (iscrv) { + if (sscanf(c, "%d:%n", &trunkgroup, &y) != 1) { + ast_log(LOG_WARNING, "CRV must begin with trunkgroup followed by a colon at line %d\n", lineno); + return -1; + } + if (trunkgroup < 1) { + ast_log(LOG_WARNING, "CRV trunk group must be a positive number at line %d\n", lineno); + return -1; + } + c += y; + for (y = 0; y < NUM_SPANS; y++) { + if (pris[y].trunkgroup == trunkgroup) { + pri = pris + y; + break; } - } else { - ast_log(LOG_ERROR, "Unable to %s channel '%s'\n", - (reload == 1) ? "reconfigure" : "register", value); + } + if (!pri) { + ast_log(LOG_WARNING, "No such trunk group %d at CRV declaration at line %d\n", trunkgroup, lineno); return -1; } } - } - - return 0; -} - -/** The length of the parameters list of 'dahdichan'. - * \todo Move definition of MAX_CHANLIST_LEN to a proper place. */ -#define MAX_CHANLIST_LEN 80 -static int process_dahdi(struct dahdi_chan_conf *confp, const char *cat, struct ast_variable *v, int reload, int skipchannels) -{ - struct dahdi_pvt *tmp; - int y; - int found_pseudo = 0; - char dahdichan[MAX_CHANLIST_LEN] = {}; - - for (; v; v = v->next) { - if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) - continue; - - /* Create the interface list */ - if (!strcasecmp(v->name, "channel") -#ifdef HAVE_PRI - || !strcasecmp(v->name, "crv") -#endif - ) { - int iscrv; - if (skipchannels) - continue; - iscrv = !strcasecmp(v->name, "crv"); - if (build_channels(confp, iscrv, v->value, reload, v->lineno, &found_pseudo)) - return -1; - } else if (!strcasecmp(v->name, "buffers")) { - int res; - char policy[8] = ""; - res = sscanf(v->value, "%d,%s", &confp->chan.buf_no, policy); - if (res != 2) { - ast_log(LOG_WARNING, "Parsing buffers option data failed, using defaults.\n"); - confp->chan.buf_no = numbufs; - continue; - } - if (confp->chan.buf_no < 0) - confp->chan.buf_no = numbufs; - if (!strcasecmp(policy, "full")) { - confp->chan.buf_policy = DAHDI_POLICY_WHEN_FULL; - } else if (!strcasecmp(policy, "half")) { - confp->chan.buf_policy = DAHDI_POLICY_IMMEDIATE /* TODO: change to HALF_FULL */; - } else if (!strcasecmp(policy, "immediate")) { - confp->chan.buf_policy = DAHDI_POLICY_IMMEDIATE; + #endif + + while ((chan = strsep(&c, ","))) { + if (sscanf(chan, "%d-%d", &start, &finish) == 2) { + /* Range */ + } else if (sscanf(chan, "%d", &start)) { + /* Just one */ + finish = start; + } else if (!strcasecmp(chan, "pseudo")) { + finish = start = CHAN_PSEUDO; + if (found_pseudo) + *found_pseudo = 1; } else { - ast_log(LOG_WARNING, "Invalid policy name given (%s).\n", policy); + ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'\n", value, chan); + return -1; } - } else if (!strcasecmp(v->name, "zapchan") || !strcasecmp(v->name, "dahdichan")) { - ast_copy_string(dahdichan, v->value, sizeof(dahdichan)); - if (v->name[0] == 'z' || v->name[0] == 'Z') { - ast_log(LOG_WARNING, "Option zapchan has been deprecated in favor of dahdichan (found in [%s])\n", cat); - } - } else if (!strcasecmp(v->name, "usedistinctiveringdetection")) { - if (ast_true(v->value)) - confp->chan.usedistinctiveringdetection = 1; - } else if (!strcasecmp(v->name, "distinctiveringaftercid")) { - if (ast_true(v->value)) - distinctiveringaftercid = 1; - } else if (!strcasecmp(v->name, "dring1context")) { - ast_copy_string(drings.ringContext[0].contextData, v->value, sizeof(drings.ringContext[0].contextData)); - } else if (!strcasecmp(v->name, "dring2context")) { - ast_copy_string(drings.ringContext[1].contextData, v->value, sizeof(drings.ringContext[1].contextData)); - } else if (!strcasecmp(v->name, "dring3context")) { - ast_copy_string(drings.ringContext[2].contextData, v->value, sizeof(drings.ringContext[2].contextData)); - } else if (!strcasecmp(v->name, "dring1")) { - sscanf(v->value, "%d,%d,%d", &drings.ringnum[0].ring[0], &drings.ringnum[0].ring[1], &drings.ringnum[0].ring[2]); - } else if (!strcasecmp(v->name, "dring2")) { - sscanf(v->value, "%d,%d,%d", &drings.ringnum[1].ring[0], &drings.ringnum[1].ring[1], &drings.ringnum[1].ring[2]); - } else if (!strcasecmp(v->name, "dring3")) { - sscanf(v->value, "%d,%d,%d", &drings.ringnum[2].ring[0], &drings.ringnum[2].ring[1], &drings.ringnum[2].ring[2]); - } else if (!strcasecmp(v->name, "usecallerid")) { - confp->chan.use_callerid = ast_true(v->value); - } else if (!strcasecmp(v->name, "cidsignalling")) { - if (!strcasecmp(v->value, "bell")) - confp->chan.cid_signalling = CID_SIG_BELL; - else if (!strcasecmp(v->value, "v23")) - confp->chan.cid_signalling = CID_SIG_V23; - else if (!strcasecmp(v->value, "dtmf")) - confp->chan.cid_signalling = CID_SIG_DTMF; - else if (!strcasecmp(v->value, "smdi")) - confp->chan.cid_signalling = CID_SIG_SMDI; - else if (!strcasecmp(v->value, "v23_jp")) - confp->chan.cid_signalling = CID_SIG_V23_JP; - else if (ast_true(v->value)) - confp->chan.cid_signalling = CID_SIG_BELL; - } else if (!strcasecmp(v->name, "cidstart")) { - if (!strcasecmp(v->value, "ring")) - confp->chan.cid_start = CID_START_RING; - else if (!strcasecmp(v->value, "polarity")) - confp->chan.cid_start = CID_START_POLARITY; - else if (ast_true(v->value)) - confp->chan.cid_start = CID_START_RING; - } else if (!strcasecmp(v->name, "threewaycalling")) { - confp->chan.threewaycalling = ast_true(v->value); - } else if (!strcasecmp(v->name, "cancallforward")) { - confp->chan.cancallforward = ast_true(v->value); - } else if (!strcasecmp(v->name, "relaxdtmf")) { - if (ast_true(v->value)) - confp->chan.dtmfrelax = DSP_DIGITMODE_RELAXDTMF; - else - confp->chan.dtmfrelax = 0; - } else if (!strcasecmp(v->name, "mailbox")) { - ast_copy_string(confp->chan.mailbox, v->value, sizeof(confp->chan.mailbox)); - } else if (!strcasecmp(v->name, "hasvoicemail")) { - if (ast_true(v->value) && ast_strlen_zero(confp->chan.mailbox)) { - ast_copy_string(confp->chan.mailbox, cat, sizeof(confp->chan.mailbox)); - } - } else if (!strcasecmp(v->name, "adsi")) { - confp->chan.adsi = ast_true(v->value); - } else if (!strcasecmp(v->name, "usesmdi")) { - confp->chan.use_smdi = ast_true(v->value); - } else if (!strcasecmp(v->name, "smdiport")) { - ast_copy_string(confp->smdi_port, v->value, sizeof(confp->smdi_port)); - } else if (!strcasecmp(v->name, "transfer")) { - confp->chan.transfer = ast_true(v->value); - } else if (!strcasecmp(v->name, "canpark")) { - confp->chan.canpark = ast_true(v->value); - } else if (!strcasecmp(v->name, "echocancelwhenbridged")) { - confp->chan.echocanbridged = ast_true(v->value); - } else if (!strcasecmp(v->name, "busydetect")) { - confp->chan.busydetect = ast_true(v->value); - } else if (!strcasecmp(v->name, "busycount")) { - confp->chan.busycount = atoi(v->value); - } else if (!strcasecmp(v->name, "busypattern")) { - if (sscanf(v->value, "%d,%d", &confp->chan.busy_tonelength, &confp->chan.busy_quietlength) != 2) { - ast_log(LOG_ERROR, "busypattern= expects busypattern=tonelength,quietlength\n"); - } - } else if (!strcasecmp(v->name, "callprogress")) { - if (ast_true(v->value)) - confp->chan.callprogress |= 1; - else - confp->chan.callprogress &= ~1; - } else if (!strcasecmp(v->name, "faxdetect")) { - if (!strcasecmp(v->value, "incoming")) { - confp->chan.callprogress |= 4; - confp->chan.callprogress &= ~2; - } else if (!strcasecmp(v->value, "outgoing")) { - confp->chan.callprogress &= ~4; - confp->chan.callprogress |= 2; - } else if (!strcasecmp(v->value, "both") || ast_true(v->value)) - confp->chan.callprogress |= 6; - else - confp->chan.callprogress &= ~6; - } else if (!strcasecmp(v->name, "echocancel")) { - if (!ast_strlen_zero(v->value)) { - y = atoi(v->value); - } else - y = 0; - if ((y == 32) || (y == 64) || (y == 128) || (y == 256) || (y == 512) || (y == 1024)) - confp->chan.echocancel = y; - else { - confp->chan.echocancel = ast_true(v->value); - if (confp->chan.echocancel) - confp->chan.echocancel=128; - } - } else if (!strcasecmp(v->name, "echotraining")) { - if (sscanf(v->value, "%d", &y) == 1) { - if ((y < 10) || (y > 4000)) { - ast_log(LOG_WARNING, "Echo training time must be within the range of 10 to 4000 ms at line %d\n", v->lineno); - } else { - confp->chan.echotraining = y; - } - } else if (ast_true(v->value)) { - confp->chan.echotraining = 400; - } else - confp->chan.echotraining = 0; - } else if (!strcasecmp(v->name, "hidecallerid")) { - confp->chan.hidecallerid = ast_true(v->value); - } else if (!strcasecmp(v->name, "hidecalleridname")) { - confp->chan.hidecalleridname = ast_true(v->value); - } else if (!strcasecmp(v->name, "pulsedial")) { - confp->chan.pulse = ast_true(v->value); - } else if (!strcasecmp(v->name, "callreturn")) { - confp->chan.callreturn = ast_true(v->value); - } else if (!strcasecmp(v->name, "callwaiting")) { - confp->chan.callwaiting = ast_true(v->value); - } else if (!strcasecmp(v->name, "callwaitingcallerid")) { - confp->chan.callwaitingcallerid = ast_true(v->value); - } else if (!strcasecmp(v->name, "context")) { - ast_copy_string(confp->chan.context, v->value, sizeof(confp->chan.context)); - } else if (!strcasecmp(v->name, "language")) { - ast_copy_string(confp->chan.language, v->value, sizeof(confp->chan.language)); - } else if (!strcasecmp(v->name, "progzone")) { - ast_copy_string(progzone, v->value, sizeof(progzone)); - } else if (!strcasecmp(v->name, "mohinterpret") - ||!strcasecmp(v->name, "musiconhold") || !strcasecmp(v->name, "musicclass")) { - ast_copy_string(confp->chan.mohinterpret, v->value, sizeof(confp->chan.mohinterpret)); - } else if (!strcasecmp(v->name, "mohsuggest")) { - ast_copy_string(confp->chan.mohsuggest, v->value, sizeof(confp->chan.mohsuggest)); - } else if (!strcasecmp(v->name, "stripmsd")) { - confp->chan.stripmsd = atoi(v->value); - } else if (!strcasecmp(v->name, "jitterbuffers")) { - numbufs = atoi(v->value); - } else if (!strcasecmp(v->name, "group")) { - confp->chan.group = ast_get_group(v->value); - } else if (!strcasecmp(v->name, "callgroup")) { - confp->chan.callgroup = ast_get_group(v->value); - } else if (!strcasecmp(v->name, "pickupgroup")) { - confp->chan.pickupgroup = ast_get_group(v->value); - } else if (!strcasecmp(v->name, "immediate")) { - confp->chan.immediate = ast_true(v->value); - } else if (!strcasecmp(v->name, "transfertobusy")) { - confp->chan.transfertobusy = ast_true(v->value); - } else if (!strcasecmp(v->name, "rxgain")) { - if (sscanf(v->value, "%f", &confp->chan.rxgain) != 1) { - ast_log(LOG_WARNING, "Invalid rxgain: %s\n", v->value); - } - } else if (!strcasecmp(v->name, "txgain")) { - if (sscanf(v->value, "%f", &confp->chan.txgain) != 1) { - ast_log(LOG_WARNING, "Invalid txgain: %s\n", v->value); - } - } else if (!strcasecmp(v->name, "tonezone")) { - if (sscanf(v->value, "%d", &confp->chan.tonezone) != 1) { - ast_log(LOG_WARNING, "Invalid tonezone: %s\n", v->value); - } - } else if (!strcasecmp(v->name, "callerid")) { - if (!strcasecmp(v->value, "asreceived")) { - confp->chan.cid_num[0] = '\0'; - confp->chan.cid_name[0] = '\0'; - } else { - ast_callerid_split(v->value, confp->chan.cid_name, sizeof(confp->chan.cid_name), confp->chan.cid_num, sizeof(confp->chan.cid_num)); - } - } else if (!strcasecmp(v->name, "fullname")) { - ast_copy_string(confp->chan.cid_name, v->value, sizeof(confp->chan.cid_name)); - } else if (!strcasecmp(v->name, "cid_number")) { - ast_copy_string(confp->chan.cid_num, v->value, sizeof(confp->chan.cid_num)); - } else if (!strcasecmp(v->name, "useincomingcalleridondahditransfer") || !strcasecmp(v->name, "useincomingcalleridonzaptransfer")) { - confp->chan.dahditrcallerid = ast_true(v->value); - if (strstr(v->name, "zap")) { - ast_log(LOG_WARNING, "Option useincomingcalleridonzaptransfer has been deprecated in favor of useincomingcalleridondahditransfer (in [%s]).\n", cat); - } - } else if (!strcasecmp(v->name, "restrictcid")) { - confp->chan.restrictcid = ast_true(v->value); - } else if (!strcasecmp(v->name, "usecallingpres")) { - confp->chan.use_callingpres = ast_true(v->value); - } else if (!strcasecmp(v->name, "accountcode")) { - ast_copy_string(confp->chan.accountcode, v->value, sizeof(confp->chan.accountcode)); - } else if (!strcasecmp(v->name, "amaflags")) { - y = ast_cdr_amaflags2int(v->value); - if (y < 0) - ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno); - else - confp->chan.amaflags = y; - } else if (!strcasecmp(v->name, "polarityonanswerdelay")) { - confp->chan.polarityonanswerdelay = atoi(v->value); - } else if (!strcasecmp(v->name, "answeronpolarityswitch")) { - confp->chan.answeronpolarityswitch = ast_true(v->value); - } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) { - confp->chan.hanguponpolarityswitch = ast_true(v->value); - } else if (!strcasecmp(v->name, "sendcalleridafter")) { - confp->chan.sendcalleridafter = atoi(v->value); - } else if (reload != 1){ - if (!strcasecmp(v->name, "signalling")) { - confp->chan.outsigmod = -1; - if (!strcasecmp(v->value, "em")) { - confp->chan.sig = SIG_EM; - } else if (!strcasecmp(v->value, "em_e1")) { - confp->chan.sig = SIG_EM_E1; - } else if (!strcasecmp(v->value, "em_w")) { - confp->chan.sig = SIG_EMWINK; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "fxs_ls")) { - confp->chan.sig = SIG_FXSLS; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "fxs_gs")) { - confp->chan.sig = SIG_FXSGS; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "fxs_ks")) { - confp->chan.sig = SIG_FXSKS; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "fxo_ls")) { - confp->chan.sig = SIG_FXOLS; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "fxo_gs")) { - confp->chan.sig = SIG_FXOGS; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "fxo_ks")) { - confp->chan.sig = SIG_FXOKS; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "fxs_rx")) { - confp->chan.sig = SIG_FXSKS; - confp->chan.radio = 1; - } else if (!strcasecmp(v->value, "fxo_rx")) { - confp->chan.sig = SIG_FXOLS; - confp->chan.radio = 1; - } else if (!strcasecmp(v->value, "fxs_tx")) { - confp->chan.sig = SIG_FXSLS; - confp->chan.radio = 1; - } else if (!strcasecmp(v->value, "fxo_tx")) { - confp->chan.sig = SIG_FXOGS; - confp->chan.radio = 1; - } else if (!strcasecmp(v->value, "em_rx")) { - confp->chan.sig = SIG_EM; - confp->chan.radio = 1; - } else if (!strcasecmp(v->value, "em_tx")) { - confp->chan.sig = SIG_EM; - confp->chan.radio = 1; - } else if (!strcasecmp(v->value, "em_rxtx")) { - confp->chan.sig = SIG_EM; - confp->chan.radio = 2; - } else if (!strcasecmp(v->value, "em_txrx")) { - confp->chan.sig = SIG_EM; - confp->chan.radio = 2; - } else if (!strcasecmp(v->value, "sf")) { - confp->chan.sig = SIG_SF; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "sf_w")) { - confp->chan.sig = SIG_SFWINK; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "sf_featd")) { - confp->chan.sig = SIG_FEATD; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "sf_featdmf")) { - confp->chan.sig = SIG_FEATDMF; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "sf_featb")) { - confp->chan.sig = SIG_SF_FEATB; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "sf")) { - confp->chan.sig = SIG_SF; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "sf_rx")) { - confp->chan.sig = SIG_SF; - confp->chan.radio = 1; - } else if (!strcasecmp(v->value, "sf_tx")) { - confp->chan.sig = SIG_SF; - confp->chan.radio = 1; - } else if (!strcasecmp(v->value, "sf_rxtx")) { - confp->chan.sig = SIG_SF; - confp->chan.radio = 2; - } else if (!strcasecmp(v->value, "sf_txrx")) { - confp->chan.sig = SIG_SF; - confp->chan.radio = 2; - } else if (!strcasecmp(v->value, "featd")) { - confp->chan.sig = SIG_FEATD; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "featdmf")) { - confp->chan.sig = SIG_FEATDMF; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "featdmf_ta")) { - confp->chan.sig = SIG_FEATDMF_TA; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "e911")) { - confp->chan.sig = SIG_E911; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "fgccama")) { - confp->chan.sig = SIG_FGC_CAMA; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "fgccamamf")) { - confp->chan.sig = SIG_FGC_CAMAMF; - confp->chan.radio = 0; - } else if (!strcasecmp(v->value, "featb")) { - confp->chan.sig = SIG_FEATB; - confp->chan.radio = 0; -#ifdef HAVE_PRI - } else if (!strcasecmp(v->value, "pri_net")) { - confp->chan.radio = 0; - confp->chan.sig = SIG_PRI; - confp->pri.nodetype = PRI_NETWORK; - } else if (!strcasecmp(v->value, "pri_cpe")) { - confp->chan.sig = SIG_PRI; - confp->chan.radio = 0; - confp->pri.nodetype = PRI_CPE; - } else if (!strcasecmp(v->value, "gr303fxoks_net")) { - confp->chan.sig = SIG_GR303FXOKS; - confp->chan.radio = 0; - confp->pri.nodetype = PRI_NETWORK; - } else if (!strcasecmp(v->value, "gr303fxsks_cpe")) { - confp->chan.sig = SIG_GR303FXSKS; - confp->chan.radio = 0; - confp->pri.nodetype = PRI_CPE; -#endif - } else { - ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value); - } - } else if (!strcasecmp(v->name, "outsignalling")) { - if (!strcasecmp(v->value, "em")) { - confp->chan.outsigmod = SIG_EM; - } else if (!strcasecmp(v->value, "em_e1")) { - confp->chan.outsigmod = SIG_EM_E1; - } else if (!strcasecmp(v->value, "em_w")) { - confp->chan.outsigmod = SIG_EMWINK; - } else if (!strcasecmp(v->value, "sf")) { - confp->chan.outsigmod = SIG_SF; - } else if (!strcasecmp(v->value, "sf_w")) { - confp->chan.outsigmod = SIG_SFWINK; - } else if (!strcasecmp(v->value, "sf_featd")) { - confp->chan.outsigmod = SIG_FEATD; - } else if (!strcasecmp(v->value, "sf_featdmf")) { - confp->chan.outsigmod = SIG_FEATDMF; - } else if (!strcasecmp(v->value, "sf_featb")) { - confp->chan.outsigmod = SIG_SF_FEATB; - } else if (!strcasecmp(v->value, "sf")) { - confp->chan.outsigmod = SIG_SF; - } else if (!strcasecmp(v->value, "featd")) { - confp->chan.outsigmod = SIG_FEATD; - } else if (!strcasecmp(v->value, "featdmf")) { - confp->chan.outsigmod = SIG_FEATDMF; - } else if (!strcasecmp(v->value, "featdmf_ta")) { - confp->chan.outsigmod = SIG_FEATDMF_TA; - } else if (!strcasecmp(v->value, "e911")) { - confp->chan.outsigmod = SIG_E911; - } else if (!strcasecmp(v->value, "fgccama")) { - confp->chan.outsigmod = SIG_FGC_CAMA; - } else if (!strcasecmp(v->value, "fgccamamf")) { - confp->chan.outsigmod = SIG_FGC_CAMAMF; - } else if (!strcasecmp(v->value, "featb")) { - confp->chan.outsigmod = SIG_FEATB; + if (finish < start) { + ast_log(LOG_WARNING, "Sillyness: %d < %d\n", start, finish); + x = finish; + finish = start; + start = x; + } + + for (x = start; x <= finish; x++) { + #ifdef HAVE_PRI + tmp = mkintf(x, conf, pri, reload); + #else + tmp = mkintf(x, conf, NULL, reload); + #endif + + if (tmp) { + if (option_verbose > 2) { + #ifdef HAVE_PRI + if (pri) + ast_verbose(VERBOSE_PREFIX_3 "%s CRV %d:%d, %s signalling\n", reload ? "Reconfigured" : "Registered", trunkgroup, x, sig2str(tmp->sig)); + else + #endif + ast_verbose(VERBOSE_PREFIX_3 "%s channel %d, %s signalling\n", reload ? "Reconfigured" : "Registered", x, sig2str(tmp->sig)); + } } else { - ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value); + ast_log(LOG_ERROR, "Unable to %s channel '%s'\n", + (reload == 1) ? "reconfigure" : "register", value); + return -1; } -#ifdef HAVE_PRI - } else if (!strcasecmp(v->name, "pridialplan")) { - if (!strcasecmp(v->value, "national")) { - confp->pri.dialplan = PRI_NATIONAL_ISDN + 1; - } else if (!strcasecmp(v->value, "unknown")) { - confp->pri.dialplan = PRI_UNKNOWN + 1; - } else if (!strcasecmp(v->value, "private")) { - confp->pri.dialplan = PRI_PRIVATE + 1; - } else if (!strcasecmp(v->value, "international")) { - confp->pri.dialplan = PRI_INTERNATIONAL_ISDN + 1; - } else if (!strcasecmp(v->value, "local")) { - confp->pri.dialplan = PRI_LOCAL_ISDN + 1; - } else if (!strcasecmp(v->value, "dynamic")) { - confp->pri.dialplan = -1; - } else { - ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno); + } + } + + return 0; + } + + /** The length of the parameters list of 'dahdichan'. + * \todo Move definition of MAX_CHANLIST_LEN to a proper place. */ + #define MAX_CHANLIST_LEN 80 + static int process_dahdi(struct dahdi_chan_conf *confp, const char *cat, struct ast_variable *v, int reload, int skipchannels) + { + struct dahdi_pvt *tmp; + int y; + int found_pseudo = 0; + char dahdichan[MAX_CHANLIST_LEN] = {}; + + for (; v; v = v->next) { + if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) + continue; + + /* Create the interface list */ + if (!strcasecmp(v->name, "channel") + #ifdef HAVE_PRI + || !strcasecmp(v->name, "crv") + #endif + ) { + int iscrv; + if (skipchannels) + continue; + iscrv = !strcasecmp(v->name, "crv"); + if (build_channels(confp, iscrv, v->value, reload, v->lineno, &found_pseudo)) + return -1; + } else if (!strcasecmp(v->name, "buffers")) { + int res; + char policy[8] = ""; + res = sscanf(v->value, "%d,%s", &confp->chan.buf_no, policy); + if (res != 2) { + ast_log(LOG_WARNING, "Parsing buffers option data failed, using defaults.\n"); + confp->chan.buf_no = numbufs; + continue; } - } else if (!strcasecmp(v->name, "prilocaldialplan")) { - if (!strcasecmp(v->value, "national")) { - confp->pri.localdialplan = PRI_NATIONAL_ISDN + 1; - } else if (!strcasecmp(v->value, "unknown")) { - confp->pri.localdialplan = PRI_UNKNOWN + 1; - } else if (!strcasecmp(v->value, "private")) { - confp->pri.localdialplan = PRI_PRIVATE + 1; - } else if (!strcasecmp(v->value, "international")) { - confp->pri.localdialplan = PRI_INTERNATIONAL_ISDN + 1; - } else if (!strcasecmp(v->value, "local")) { - confp->pri.localdialplan = PRI_LOCAL_ISDN + 1; - } else if (!strcasecmp(v->value, "dynamic")) { - confp->pri.localdialplan = -1; + if (confp->chan.buf_no < 0) + confp->chan.buf_no = numbufs; + if (!strcasecmp(policy, "full")) { + confp->chan.buf_policy = DAHDI_POLICY_WHEN_FULL; + } else if (!strcasecmp(policy, "half")) { + confp->chan.buf_policy = DAHDI_POLICY_IMMEDIATE /* TODO: change to HALF_FULL */; + } else if (!strcasecmp(policy, "immediate")) { + confp->chan.buf_policy = DAHDI_POLICY_IMMEDIATE; } else { - ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno); - } - } else if (!strcasecmp(v->name, "switchtype")) { - if (!strcasecmp(v->value, "national")) - confp->pri.switchtype = PRI_SWITCH_NI2; - else if (!strcasecmp(v->value, "ni1")) - confp->pri.switchtype = PRI_SWITCH_NI1; - else if (!strcasecmp(v->value, "dms100")) - confp->pri.switchtype = PRI_SWITCH_DMS100; - else if (!strcasecmp(v->value, "4ess")) - confp->pri.switchtype = PRI_SWITCH_ATT4ESS; - else if (!strcasecmp(v->value, "5ess")) - confp->pri.switchtype = PRI_SWITCH_LUCENT5E; - else if (!strcasecmp(v->value, "euroisdn")) - confp->pri.switchtype = PRI_SWITCH_EUROISDN_E1; - else if (!strcasecmp(v->value, "qsig")) - confp->pri.switchtype = PRI_SWITCH_QSIG; - else { - ast_log(LOG_ERROR, "Unknown switchtype '%s'\n", v->value); - return -1; - } - } else if (!strcasecmp(v->name, "nsf")) { - if (!strcasecmp(v->value, "sdn")) - confp->pri.nsf = PRI_NSF_SDN; - else if (!strcasecmp(v->value, "megacom")) - confp->pri.nsf = PRI_NSF_MEGACOM; - else if (!strcasecmp(v->value, "tollfreemegacom")) - confp->pri.nsf = PRI_NSF_TOLL_FREE_MEGACOM; - else if (!strcasecmp(v->value, "accunet")) - confp->pri.nsf = PRI_NSF_ACCUNET; - else if (!strcasecmp(v->value, "none")) - confp->pri.nsf = PRI_NSF_NONE; - else { - ast_log(LOG_WARNING, "Unknown network-specific facility '%s'\n", v->value); - confp->pri.nsf = PRI_NSF_NONE; + ast_log(LOG_WARNING, "Invalid policy name given (%s).\n", policy); } - } else if (!strcasecmp(v->name, "priindication")) { - if (!strcasecmp(v->value, "outofband")) - confp->chan.priindication_oob = 1; - else if (!strcasecmp(v->value, "inband")) - confp->chan.priindication_oob = 0; + } else if (!strcasecmp(v->name, "zapchan") || !strcasecmp(v->name, "dahdichan")) { + ast_copy_string(dahdichan, v->value, sizeof(dahdichan)); + if (v->name[0] == 'z' || v->name[0] == 'Z') { + ast_log(LOG_WARNING, "Option zapchan has been deprecated in favor of dahdichan (found in [%s])\n", cat); + } + } else if (!strcasecmp(v->name, "usedistinctiveringdetection")) { + if (ast_true(v->value)) + confp->chan.usedistinctiveringdetection = 1; + } else if (!strcasecmp(v->name, "distinctiveringaftercid")) { + if (ast_true(v->value)) + distinctiveringaftercid = 1; + } else if (!strcasecmp(v->name, "dring1context")) { + ast_copy_string(drings.ringContext[0].contextData, v->value, sizeof(drings.ringContext[0].contextData)); + } else if (!strcasecmp(v->name, "dring2context")) { + ast_copy_string(drings.ringContext[1].contextData, v->value, sizeof(drings.ringContext[1].contextData)); + } else if (!strcasecmp(v->name, "dring3context")) { + ast_copy_string(drings.ringContext[2].contextData, v->value, sizeof(drings.ringContext[2].contextData)); + } else if (!strcasecmp(v->name, "dring1")) { + sscanf(v->value, "%d,%d,%d", &drings.ringnum[0].ring[0], &drings.ringnum[0].ring[1], &drings.ringnum[0].ring[2]); + } else if (!strcasecmp(v->name, "dring2")) { + sscanf(v->value, "%d,%d,%d", &drings.ringnum[1].ring[0], &drings.ringnum[1].ring[1], &drings.ringnum[1].ring[2]); + } else if (!strcasecmp(v->name, "dring3")) { + sscanf(v->value, "%d,%d,%d", &drings.ringnum[2].ring[0], &drings.ringnum[2].ring[1], &drings.ringnum[2].ring[2]); + } else if (!strcasecmp(v->name, "usecallerid")) { + confp->chan.use_callerid = ast_true(v->value); + } else if (!strcasecmp(v->name, "cidsignalling")) { + if (!strcasecmp(v->value, "bell")) + confp->chan.cid_signalling = CID_SIG_BELL; + else if (!strcasecmp(v->value, "v23")) + confp->chan.cid_signalling = CID_SIG_V23; + else if (!strcasecmp(v->value, "dtmf")) + confp->chan.cid_signalling = CID_SIG_DTMF; + else if (!strcasecmp(v->value, "smdi")) + confp->chan.cid_signalling = CID_SIG_SMDI; + else if (!strcasecmp(v->value, "v23_jp")) + confp->chan.cid_signalling = CID_SIG_V23_JP; + else if (ast_true(v->value)) + confp->chan.cid_signalling = CID_SIG_BELL; + } else if (!strcasecmp(v->name, "cidstart")) { + if (!strcasecmp(v->value, "ring")) + confp->chan.cid_start = CID_START_RING; + else if (!strcasecmp(v->value, "polarity")) + confp->chan.cid_start = CID_START_POLARITY; + else if (ast_true(v->value)) + confp->chan.cid_start = CID_START_RING; + } else if (!strcasecmp(v->name, "threewaycalling")) { + confp->chan.threewaycalling = ast_true(v->value); + } else if (!strcasecmp(v->name, "cancallforward")) { + confp->chan.cancallforward = ast_true(v->value); + } else if (!strcasecmp(v->name, "relaxdtmf")) { + if (ast_true(v->value)) + confp->chan.dtmfrelax = DSP_DIGITMODE_RELAXDTMF; else - ast_log(LOG_WARNING, "'%s' is not a valid pri indication value, should be 'inband' or 'outofband' at line %d\n", - v->value, v->lineno); - } else if (!strcasecmp(v->name, "priexclusive")) { - confp->chan.priexclusive = ast_true(v->value); - } else if (!strcasecmp(v->name, "internationalprefix")) { - ast_copy_string(confp->pri.internationalprefix, v->value, sizeof(confp->pri.internationalprefix)); - } else if (!strcasecmp(v->name, "nationalprefix")) { - ast_copy_string(confp->pri.nationalprefix, v->value, sizeof(confp->pri.nationalprefix)); - } else if (!strcasecmp(v->name, "localprefix")) { - ast_copy_string(confp->pri.localprefix, v->value, sizeof(confp->pri.localprefix)); - } else if (!strcasecmp(v->name, "privateprefix")) { - ast_copy_string(confp->pri.privateprefix, v->value, sizeof(confp->pri.privateprefix)); - } else if (!strcasecmp(v->name, "unknownprefix")) { - ast_copy_string(confp->pri.unknownprefix, v->value, sizeof(confp->pri.unknownprefix)); - } else if (!strcasecmp(v->name, "resetinterval")) { - if (!strcasecmp(v->value, "never")) - confp->pri.resetinterval = -1; - else if (atoi(v->value) >= 60) - confp->pri.resetinterval = atoi(v->value); + confp->chan.dtmfrelax = 0; + } else if (!strcasecmp(v->name, "mailbox")) { + ast_copy_string(confp->chan.mailbox, v->value, sizeof(confp->chan.mailbox)); + } else if (!strcasecmp(v->name, "hasvoicemail")) { + if (ast_true(v->value) && ast_strlen_zero(confp->chan.mailbox)) { + ast_copy_string(confp->chan.mailbox, cat, sizeof(confp->chan.mailbox)); + } + } else if (!strcasecmp(v->name, "adsi")) { + confp->chan.adsi = ast_true(v->value); + } else if (!strcasecmp(v->name, "usesmdi")) { + confp->chan.use_smdi = ast_true(v->value); + } else if (!strcasecmp(v->name, "smdiport")) { + ast_copy_string(confp->smdi_port, v->value, sizeof(confp->smdi_port)); + } else if (!strcasecmp(v->name, "transfer")) { + confp->chan.transfer = ast_true(v->value); + } else if (!strcasecmp(v->name, "canpark")) { + confp->chan.canpark = ast_true(v->value); + } else if (!strcasecmp(v->name, "echocancelwhenbridged")) { + confp->chan.echocanbridged = ast_true(v->value); + } else if (!strcasecmp(v->name, "busydetect")) { + confp->chan.busydetect = ast_true(v->value); + } else if (!strcasecmp(v->name, "busycount")) { + confp->chan.busycount = atoi(v->value); + } else if (!strcasecmp(v->name, "busypattern")) { + if (sscanf(v->value, "%d,%d", &confp->chan.busy_tonelength, &confp->chan.busy_quietlength) != 2) { + ast_log(LOG_ERROR, "busypattern= expects busypattern=tonelength,quietlength\n"); + } + } else if (!strcasecmp(v->name, "callprogress")) { + if (ast_true(v->value)) + confp->chan.callprogress |= 1; else - ast_log(LOG_WARNING, "'%s' is not a valid reset interval, should be >= 60 seconds or 'never' at line %d\n", - v->value, v->lineno); - } else if (!strcasecmp(v->name, "minunused")) { - confp->pri.minunused = atoi(v->value); - } else if (!strcasecmp(v->name, "minidle")) { - confp->pri.minidle = atoi(v->value); - } else if (!strcasecmp(v->name, "idleext")) { - ast_copy_string(confp->pri.idleext, v->value, sizeof(confp->pri.idleext)); - } else if (!strcasecmp(v->name, "idledial")) { - ast_copy_string(confp->pri.idledial, v->value, sizeof(confp->pri.idledial)); - } else if (!strcasecmp(v->name, "overlapdial")) { - confp->pri.overlapdial = ast_true(v->value); -#ifdef HAVE_PRI_INBANDDISCONNECT - } else if (!strcasecmp(v->name, "inbanddisconnect")) { - confp->pri.inbanddisconnect = ast_true(v->value); -#endif - } else if (!strcasecmp(v->name, "pritimer")) { -#ifdef PRI_GETSET_TIMERS - char *timerc, *c; - int timer, timeridx; - c = v->value; - timerc = strsep(&c, ","); - if (timerc) { - timer = atoi(c); - if (!timer) - ast_log(LOG_WARNING, "'%s' is not a valid value for an ISDN timer\n", timerc); + confp->chan.callprogress &= ~1; + } else if (!strcasecmp(v->name, "faxdetect")) { + if (!strcasecmp(v->value, "incoming")) { + confp->chan.callprogress |= 4; + confp->chan.callprogress &= ~2; + } else if (!strcasecmp(v->value, "outgoing")) { + confp->chan.callprogress &= ~4; + confp->chan.callprogress |= 2; + } else if (!strcasecmp(v->value, "both") || ast_true(v->value)) + confp->chan.callprogress |= 6; + else + confp->chan.callprogress &= ~6; + } else if (!strcasecmp(v->name, "echocancel")) { + if (!ast_strlen_zero(v->value)) { + y = atoi(v->value); + } else + y = 0; + if ((y == 32) || (y == 64) || (y == 128) || (y == 256) || (y == 512) || (y == 1024)) + confp->chan.echocancel = y; + else { + confp->chan.echocancel = ast_true(v->value); + if (confp->chan.echocancel) + confp->chan.echocancel=128; + } + } else if (!strcasecmp(v->name, "echotraining")) { + if (sscanf(v->value, "%d", &y) == 1) { + if ((y < 10) || (y > 4000)) { + ast_log(LOG_WARNING, "Echo training time must be within the range of 10 to 4000 ms at line %d\n", v->lineno); + } else { + confp->chan.echotraining = y; + } + } else if (ast_true(v->value)) { + confp->chan.echotraining = 400; + } else + confp->chan.echotraining = 0; + } else if (!strcasecmp(v->name, "hidecallerid")) { + confp->chan.hidecallerid = ast_true(v->value); + } else if (!strcasecmp(v->name, "hidecalleridname")) { + confp->chan.hidecalleridname = ast_true(v->value); + } else if (!strcasecmp(v->name, "pulsedial")) { + confp->chan.pulse = ast_true(v->value); + } else if (!strcasecmp(v->name, "callreturn")) { + confp->chan.callreturn = ast_true(v->value); + } else if (!strcasecmp(v->name, "callwaiting")) { + confp->chan.callwaiting = ast_true(v->value); + } else if (!strcasecmp(v->name, "callwaitingcallerid")) { + confp->chan.callwaitingcallerid = ast_true(v->value); + } else if (!strcasecmp(v->name, "context")) { + ast_copy_string(confp->chan.context, v->value, sizeof(confp->chan.context)); + } else if (!strcasecmp(v->name, "language")) { + ast_copy_string(confp->chan.language, v->value, sizeof(confp->chan.language)); + } else if (!strcasecmp(v->name, "progzone")) { + ast_copy_string(progzone, v->value, sizeof(progzone)); + } else if (!strcasecmp(v->name, "mohinterpret") + ||!strcasecmp(v->name, "musiconhold") || !strcasecmp(v->name, "musicclass")) { + ast_copy_string(confp->chan.mohinterpret, v->value, sizeof(confp->chan.mohinterpret)); + } else if (!strcasecmp(v->name, "mohsuggest")) { + ast_copy_string(confp->chan.mohsuggest, v->value, sizeof(confp->chan.mohsuggest)); + } else if (!strcasecmp(v->name, "stripmsd")) { + confp->chan.stripmsd = atoi(v->value); + } else if (!strcasecmp(v->name, "jitterbuffers")) { + numbufs = atoi(v->value); + } else if (!strcasecmp(v->name, "group")) { + confp->chan.group = ast_get_group(v->value); + } else if (!strcasecmp(v->name, "callgroup")) { + confp->chan.callgroup = ast_get_group(v->value); + } else if (!strcasecmp(v->name, "pickupgroup")) { + confp->chan.pickupgroup = ast_get_group(v->value); + } else if (!strcasecmp(v->name, "immediate")) { + confp->chan.immediate = ast_true(v->value); + } else if (!strcasecmp(v->name, "transfertobusy")) { + confp->chan.transfertobusy = ast_true(v->value); + } else if (!strcasecmp(v->name, "rxgain")) { + if (sscanf(v->value, "%f", &confp->chan.rxgain) != 1) { + ast_log(LOG_WARNING, "Invalid rxgain: %s\n", v->value); + } + } else if (!strcasecmp(v->name, "txgain")) { + if (sscanf(v->value, "%f", &confp->chan.txgain) != 1) { + ast_log(LOG_WARNING, "Invalid txgain: %s\n", v->value); + } + } else if (!strcasecmp(v->name, "tonezone")) { + if (sscanf(v->value, "%d", &confp->chan.tonezone) != 1) { + ast_log(LOG_WARNING, "Invalid tonezone: %s\n", v->value); + } + } else if (!strcasecmp(v->name, "callerid")) { + if (!strcasecmp(v->value, "asreceived")) { + confp->chan.cid_num[0] = '\0'; + confp->chan.cid_name[0] = '\0'; + } else { + ast_callerid_split(v->value, confp->chan.cid_name, sizeof(confp->chan.cid_name), confp->chan.cid_num, sizeof(confp->chan.cid_num)); + } + } else if (!strcasecmp(v->name, "fullname")) { + ast_copy_string(confp->chan.cid_name, v->value, sizeof(confp->chan.cid_name)); + } else if (!strcasecmp(v->name, "cid_number")) { + ast_copy_string(confp->chan.cid_num, v->value, sizeof(confp->chan.cid_num)); + } else if (!strcasecmp(v->name, "useincomingcalleridondahditransfer") || !strcasecmp(v->name, "useincomingcalleridonzaptransfer")) { + confp->chan.dahditrcallerid = ast_true(v->value); + if (strstr(v->name, "zap")) { + ast_log(LOG_WARNING, "Option useincomingcalleridonzaptransfer has been deprecated in favor of useincomingcalleridondahditransfer (in [%s]).\n", cat); + } + } else if (!strcasecmp(v->name, "restrictcid")) { + confp->chan.restrictcid = ast_true(v->value); + } else if (!strcasecmp(v->name, "usecallingpres")) { + confp->chan.use_callingpres = ast_true(v->value); + } else if (!strcasecmp(v->name, "accountcode")) { + ast_copy_string(confp->chan.accountcode, v->value, sizeof(confp->chan.accountcode)); + } else if (!strcasecmp(v->name, "amaflags")) { + y = ast_cdr_amaflags2int(v->value); + if (y < 0) + ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno); + else + confp->chan.amaflags = y; + } else if (!strcasecmp(v->name, "polarityonanswerdelay")) { + confp->chan.polarityonanswerdelay = atoi(v->value); + } else if (!strcasecmp(v->name, "answeronpolarityswitch")) { + confp->chan.answeronpolarityswitch = ast_true(v->value); + } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) { + confp->chan.hanguponpolarityswitch = ast_true(v->value); + } else if (!strcasecmp(v->name, "sendcalleridafter")) { + confp->chan.sendcalleridafter = atoi(v->value); + } else if (reload != 1){ + if (!strcasecmp(v->name, "signalling")) { + confp->chan.outsigmod = -1; + if (!strcasecmp(v->value, "em")) { + confp->chan.sig = SIG_EM; + } else if (!strcasecmp(v->value, "em_e1")) { + confp->chan.sig = SIG_EM_E1; + } else if (!strcasecmp(v->value, "em_w")) { + confp->chan.sig = SIG_EMWINK; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "fxs_ls")) { + confp->chan.sig = SIG_FXSLS; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "fxs_gs")) { + confp->chan.sig = SIG_FXSGS; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "fxs_ks")) { + confp->chan.sig = SIG_FXSKS; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "fxo_ls")) { + confp->chan.sig = SIG_FXOLS; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "fxo_gs")) { + confp->chan.sig = SIG_FXOGS; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "fxo_ks")) { + confp->chan.sig = SIG_FXOKS; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "fxs_rx")) { + confp->chan.sig = SIG_FXSKS; + confp->chan.radio = 1; + } else if (!strcasecmp(v->value, "fxo_rx")) { + confp->chan.sig = SIG_FXOLS; + confp->chan.radio = 1; + } else if (!strcasecmp(v->value, "fxs_tx")) { + confp->chan.sig = SIG_FXSLS; + confp->chan.radio = 1; + } else if (!strcasecmp(v->value, "fxo_tx")) { + confp->chan.sig = SIG_FXOGS; + confp->chan.radio = 1; + } else if (!strcasecmp(v->value, "em_rx")) { + confp->chan.sig = SIG_EM; + confp->chan.radio = 1; + } else if (!strcasecmp(v->value, "em_tx")) { + confp->chan.sig = SIG_EM; + confp->chan.radio = 1; + } else if (!strcasecmp(v->value, "em_rxtx")) { + confp->chan.sig = SIG_EM; + confp->chan.radio = 2; + } else if (!strcasecmp(v->value, "em_txrx")) { + confp->chan.sig = SIG_EM; + confp->chan.radio = 2; + } else if (!strcasecmp(v->value, "sf")) { + confp->chan.sig = SIG_SF; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "sf_w")) { + confp->chan.sig = SIG_SFWINK; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "sf_featd")) { + confp->chan.sig = SIG_FEATD; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "sf_featdmf")) { + confp->chan.sig = SIG_FEATDMF; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "sf_featb")) { + confp->chan.sig = SIG_SF_FEATB; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "sf")) { + confp->chan.sig = SIG_SF; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "sf_rx")) { + confp->chan.sig = SIG_SF; + confp->chan.radio = 1; + } else if (!strcasecmp(v->value, "sf_tx")) { + confp->chan.sig = SIG_SF; + confp->chan.radio = 1; + } else if (!strcasecmp(v->value, "sf_rxtx")) { + confp->chan.sig = SIG_SF; + confp->chan.radio = 2; + } else if (!strcasecmp(v->value, "sf_txrx")) { + confp->chan.sig = SIG_SF; + confp->chan.radio = 2; + } else if (!strcasecmp(v->value, "featd")) { + confp->chan.sig = SIG_FEATD; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "featdmf")) { + confp->chan.sig = SIG_FEATDMF; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "featdmf_ta")) { + confp->chan.sig = SIG_FEATDMF_TA; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "e911")) { + confp->chan.sig = SIG_E911; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "fgccama")) { + confp->chan.sig = SIG_FGC_CAMA; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "fgccamamf")) { + confp->chan.sig = SIG_FGC_CAMAMF; + confp->chan.radio = 0; + } else if (!strcasecmp(v->value, "featb")) { + confp->chan.sig = SIG_FEATB; + confp->chan.radio = 0; + #ifdef HAVE_PRI + } else if (!strcasecmp(v->value, "pri_net")) { + confp->chan.radio = 0; + confp->chan.sig = SIG_PRI; + confp->pri.nodetype = PRI_NETWORK; + } else if (!strcasecmp(v->value, "pri_cpe")) { + confp->chan.sig = SIG_PRI; + confp->chan.radio = 0; + confp->pri.nodetype = PRI_CPE; + } else if (!strcasecmp(v->value, "gr303fxoks_net")) { + confp->chan.sig = SIG_GR303FXOKS; + confp->chan.radio = 0; + confp->pri.nodetype = PRI_NETWORK; + } else if (!strcasecmp(v->value, "gr303fxsks_cpe")) { + confp->chan.sig = SIG_GR303FXSKS; + confp->chan.radio = 0; + confp->pri.nodetype = PRI_CPE; + #endif + } else { + ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value); + } + } else if (!strcasecmp(v->name, "outsignalling")) { + if (!strcasecmp(v->value, "em")) { + confp->chan.outsigmod = SIG_EM; + } else if (!strcasecmp(v->value, "em_e1")) { + confp->chan.outsigmod = SIG_EM_E1; + } else if (!strcasecmp(v->value, "em_w")) { + confp->chan.outsigmod = SIG_EMWINK; + } else if (!strcasecmp(v->value, "sf")) { + confp->chan.outsigmod = SIG_SF; + } else if (!strcasecmp(v->value, "sf_w")) { + confp->chan.outsigmod = SIG_SFWINK; + } else if (!strcasecmp(v->value, "sf_featd")) { + confp->chan.outsigmod = SIG_FEATD; + } else if (!strcasecmp(v->value, "sf_featdmf")) { + confp->chan.outsigmod = SIG_FEATDMF; + } else if (!strcasecmp(v->value, "sf_featb")) { + confp->chan.outsigmod = SIG_SF_FEATB; + } else if (!strcasecmp(v->value, "sf")) { + confp->chan.outsigmod = SIG_SF; + } else if (!strcasecmp(v->value, "featd")) { + confp->chan.outsigmod = SIG_FEATD; + } else if (!strcasecmp(v->value, "featdmf")) { + confp->chan.outsigmod = SIG_FEATDMF; + } else if (!strcasecmp(v->value, "featdmf_ta")) { + confp->chan.outsigmod = SIG_FEATDMF_TA; + } else if (!strcasecmp(v->value, "e911")) { + confp->chan.outsigmod = SIG_E911; + } else if (!strcasecmp(v->value, "fgccama")) { + confp->chan.outsigmod = SIG_FGC_CAMA; + } else if (!strcasecmp(v->value, "fgccamamf")) { + confp->chan.outsigmod = SIG_FGC_CAMAMF; + } else if (!strcasecmp(v->value, "featb")) { + confp->chan.outsigmod = SIG_FEATB; + } else { + ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value); + } + #ifdef HAVE_PRI + } else if (!strcasecmp(v->name, "pridialplan")) { + if (!strcasecmp(v->value, "national")) { + confp->pri.dialplan = PRI_NATIONAL_ISDN + 1; + } else if (!strcasecmp(v->value, "unknown")) { + confp->pri.dialplan = PRI_UNKNOWN + 1; + } else if (!strcasecmp(v->value, "private")) { + confp->pri.dialplan = PRI_PRIVATE + 1; + } else if (!strcasecmp(v->value, "international")) { + confp->pri.dialplan = PRI_INTERNATIONAL_ISDN + 1; + } else if (!strcasecmp(v->value, "local")) { + confp->pri.dialplan = PRI_LOCAL_ISDN + 1; + } else if (!strcasecmp(v->value, "dynamic")) { + confp->pri.dialplan = -1; + } else { + ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno); + } + } else if (!strcasecmp(v->name, "prilocaldialplan")) { + if (!strcasecmp(v->value, "national")) { + confp->pri.localdialplan = PRI_NATIONAL_ISDN + 1; + } else if (!strcasecmp(v->value, "unknown")) { + confp->pri.localdialplan = PRI_UNKNOWN + 1; + } else if (!strcasecmp(v->value, "private")) { + confp->pri.localdialplan = PRI_PRIVATE + 1; + } else if (!strcasecmp(v->value, "international")) { + confp->pri.localdialplan = PRI_INTERNATIONAL_ISDN + 1; + } else if (!strcasecmp(v->value, "local")) { + confp->pri.localdialplan = PRI_LOCAL_ISDN + 1; + } else if (!strcasecmp(v->value, "dynamic")) { + confp->pri.localdialplan = -1; + } else { + ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno); + } + } else if (!strcasecmp(v->name, "switchtype")) { + if (!strcasecmp(v->value, "national")) + confp->pri.switchtype = PRI_SWITCH_NI2; + else if (!strcasecmp(v->value, "ni1")) + confp->pri.switchtype = PRI_SWITCH_NI1; + else if (!strcasecmp(v->value, "dms100")) + confp->pri.switchtype = PRI_SWITCH_DMS100; + else if (!strcasecmp(v->value, "4ess")) + confp->pri.switchtype = PRI_SWITCH_ATT4ESS; + else if (!strcasecmp(v->value, "5ess")) + confp->pri.switchtype = PRI_SWITCH_LUCENT5E; + else if (!strcasecmp(v->value, "euroisdn")) + confp->pri.switchtype = PRI_SWITCH_EUROISDN_E1; + else if (!strcasecmp(v->value, "qsig")) + confp->pri.switchtype = PRI_SWITCH_QSIG; else { - if ((timeridx = pri_timer2idx(timerc)) >= 0) - pritimers[timeridx] = timer; - else - ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer\n", timerc); + ast_log(LOG_ERROR, "Unknown switchtype '%s'\n", v->value); + return -1; + } + } else if (!strcasecmp(v->name, "h324m")) { + if (!strcasecmp(v->value, "bc")) { + confp->pri.h324musellc = 0; + ast_log(LOG_WARNING, "h324m mode: confp->pri.h324musellc=%d\n",confp->pri.h324musellc); + } else if (!strcasecmp(v->value, "llc")) { + confp->pri.h324musellc = 1; + ast_log(LOG_WARNING, "h324m mode: confp->pri.h324musellc=%d\n",confp->pri.h324musellc); + } else { + ast_log(LOG_WARNING, "Unknown h324m mode '%s' at line %d. Using default=bc\n", v->value, v->lineno); + } + } else if (!strcasecmp(v->name, "nsf")) { + if (!strcasecmp(v->value, "sdn")) + confp->pri.nsf = PRI_NSF_SDN; + else if (!strcasecmp(v->value, "megacom")) + confp->pri.nsf = PRI_NSF_MEGACOM; + else if (!strcasecmp(v->value, "tollfreemegacom")) + confp->pri.nsf = PRI_NSF_TOLL_FREE_MEGACOM; + else if (!strcasecmp(v->value, "accunet")) + confp->pri.nsf = PRI_NSF_ACCUNET; + else if (!strcasecmp(v->value, "none")) + confp->pri.nsf = PRI_NSF_NONE; + else { + ast_log(LOG_WARNING, "Unknown network-specific facility '%s'\n", v->value); + confp->pri.nsf = PRI_NSF_NONE; + } + } else if (!strcasecmp(v->name, "priindication")) { + if (!strcasecmp(v->value, "outofband")) + confp->chan.priindication_oob = 1; + else if (!strcasecmp(v->value, "inband")) + confp->chan.priindication_oob = 0; + else + ast_log(LOG_WARNING, "'%s' is not a valid pri indication value, should be 'inband' or 'outofband' at line %d\n", + v->value, v->lineno); + } else if (!strcasecmp(v->name, "priexclusive")) { + confp->chan.priexclusive = ast_true(v->value); + } else if (!strcasecmp(v->name, "internationalprefix")) { + ast_copy_string(confp->pri.internationalprefix, v->value, sizeof(confp->pri.internationalprefix)); + } else if (!strcasecmp(v->name, "nationalprefix")) { + ast_copy_string(confp->pri.nationalprefix, v->value, sizeof(confp->pri.nationalprefix)); + } else if (!strcasecmp(v->name, "localprefix")) { + ast_copy_string(confp->pri.localprefix, v->value, sizeof(confp->pri.localprefix)); + } else if (!strcasecmp(v->name, "privateprefix")) { + ast_copy_string(confp->pri.privateprefix, v->value, sizeof(confp->pri.privateprefix)); + } else if (!strcasecmp(v->name, "unknownprefix")) { + ast_copy_string(confp->pri.unknownprefix, v->value, sizeof(confp->pri.unknownprefix)); + } else if (!strcasecmp(v->name, "resetinterval")) { + if (!strcasecmp(v->value, "never")) + confp->pri.resetinterval = -1; + else if (atoi(v->value) >= 60) + confp->pri.resetinterval = atoi(v->value); + else + ast_log(LOG_WARNING, "'%s' is not a valid reset interval, should be >= 60 seconds or 'never' at line %d\n", + v->value, v->lineno); + } else if (!strcasecmp(v->name, "minunused")) { + confp->pri.minunused = atoi(v->value); + } else if (!strcasecmp(v->name, "minidle")) { + confp->pri.minidle = atoi(v->value); + } else if (!strcasecmp(v->name, "idleext")) { + ast_copy_string(confp->pri.idleext, v->value, sizeof(confp->pri.idleext)); + } else if (!strcasecmp(v->name, "idledial")) { + ast_copy_string(confp->pri.idledial, v->value, sizeof(confp->pri.idledial)); + } else if (!strcasecmp(v->name, "overlapdial")) { + confp->pri.overlapdial = ast_true(v->value); + #ifdef HAVE_PRI_INBANDDISCONNECT + } else if (!strcasecmp(v->name, "inbanddisconnect")) { + confp->pri.inbanddisconnect = ast_true(v->value); + #endif + } else if (!strcasecmp(v->name, "pritimer")) { + #ifdef PRI_GETSET_TIMERS + char *timerc, *c; + int timer, timeridx; + c = v->value; + timerc = strsep(&c, ","); + if (timerc) { + timer = atoi(c); + if (!timer) + ast_log(LOG_WARNING, "'%s' is not a valid value for an ISDN timer\n", timerc); + else { + if ((timeridx = pri_timer2idx(timerc)) >= 0) + pritimers[timeridx] = timer; + else + ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer\n", timerc); + } + } else + ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer configuration string\n", v->value); + + } else if (!strcasecmp(v->name, "facilityenable")) { + confp->pri.facilityenable = ast_true(v->value); + #endif /* PRI_GETSET_TIMERS */ + #endif /* HAVE_PRI */ + } else if (!strcasecmp(v->name, "cadence")) { + /* setup to scan our argument */ + int element_count, c[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + int i; + struct dahdi_ring_cadence new_cadence; + int cid_location = -1; + int firstcadencepos = 0; + char original_args[80]; + int cadence_is_ok = 1; + + ast_copy_string(original_args, v->value, sizeof(original_args)); + /* 16 cadences allowed (8 pairs) */ + element_count = sscanf(v->value, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &c[0], &c[1], &c[2], &c[3], &c[4], &c[5], &c[6], &c[7], &c[8], &c[9], &c[10], &c[11], &c[12], &c[13], &c[14], &c[15]); + + /* Cadence must be even (on/off) */ + if (element_count % 2 == 1) { + ast_log(LOG_ERROR, "Must be a silence duration for each ring duration: %s\n",original_args); + cadence_is_ok = 0; + } + + /* Ring cadences cannot be negative */ + for (i = 0; i < element_count; i++) { + if (c[i] == 0) { + ast_log(LOG_ERROR, "Ring or silence duration cannot be zero: %s\n", original_args); + cadence_is_ok = 0; + break; + } else if (c[i] < 0) { + if (i % 2 == 1) { + /* Silence duration, negative possibly okay */ + if (cid_location == -1) { + cid_location = i; + c[i] *= -1; + } else { + ast_log(LOG_ERROR, "CID location specified twice: %s\n",original_args); + cadence_is_ok = 0; + break; + } + } else { + if (firstcadencepos == 0) { + firstcadencepos = i; /* only recorded to avoid duplicate specification */ + /* duration will be passed negative to the DAHDI driver */ + } else { + ast_log(LOG_ERROR, "First cadence position specified twice: %s\n",original_args); + cadence_is_ok = 0; + break; + } + } + } + } + + /* Substitute our scanned cadence */ + for (i = 0; i < 16; i++) { + new_cadence.ringcadence[i] = c[i]; } - } else - ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer configuration string\n", v->value); - - } else if (!strcasecmp(v->name, "facilityenable")) { - confp->pri.facilityenable = ast_true(v->value); -#endif /* PRI_GETSET_TIMERS */ -#endif /* HAVE_PRI */ - } else if (!strcasecmp(v->name, "cadence")) { - /* setup to scan our argument */ - int element_count, c[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; - int i; - struct dahdi_ring_cadence new_cadence; - int cid_location = -1; - int firstcadencepos = 0; - char original_args[80]; - int cadence_is_ok = 1; - - ast_copy_string(original_args, v->value, sizeof(original_args)); - /* 16 cadences allowed (8 pairs) */ - element_count = sscanf(v->value, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &c[0], &c[1], &c[2], &c[3], &c[4], &c[5], &c[6], &c[7], &c[8], &c[9], &c[10], &c[11], &c[12], &c[13], &c[14], &c[15]); - - /* Cadence must be even (on/off) */ - if (element_count % 2 == 1) { - ast_log(LOG_ERROR, "Must be a silence duration for each ring duration: %s\n",original_args); - cadence_is_ok = 0; - } - - /* Ring cadences cannot be negative */ - for (i = 0; i < element_count; i++) { - if (c[i] == 0) { - ast_log(LOG_ERROR, "Ring or silence duration cannot be zero: %s\n", original_args); - cadence_is_ok = 0; - break; - } else if (c[i] < 0) { - if (i % 2 == 1) { - /* Silence duration, negative possibly okay */ + + if (cadence_is_ok) { + /* ---we scanned it without getting annoyed; now some sanity checks--- */ + if (element_count < 2) { + ast_log(LOG_ERROR, "Minimum cadence is ring,pause: %s\n", original_args); + } else { if (cid_location == -1) { - cid_location = i; - c[i] *= -1; + /* user didn't say; default to first pause */ + cid_location = 1; } else { - ast_log(LOG_ERROR, "CID location specified twice: %s\n",original_args); - cadence_is_ok = 0; - break; + /* convert element_index to cidrings value */ + cid_location = (cid_location + 1) / 2; } - } else { - if (firstcadencepos == 0) { - firstcadencepos = i; /* only recorded to avoid duplicate specification */ - /* duration will be passed negative to the DAHDI driver */ - } else { - ast_log(LOG_ERROR, "First cadence position specified twice: %s\n",original_args); - cadence_is_ok = 0; - break; + /* ---we like their cadence; try to install it--- */ + if (!user_has_defined_cadences++) + /* this is the first user-defined cadence; clear the default user cadences */ + num_cadence = 0; + if ((num_cadence+1) >= NUM_CADENCE_MAX) + ast_log(LOG_ERROR, "Already %d cadences; can't add another: %s\n", NUM_CADENCE_MAX, original_args); + else { + cadences[num_cadence] = new_cadence; + cidrings[num_cadence++] = cid_location; + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "cadence 'r%d' added: %s\n",num_cadence,original_args); } } } - } + } else if (!strcasecmp(v->name, "ringtimeout")) { + ringt_base = (atoi(v->value) * 8) / READ_SIZE; + } else if (!strcasecmp(v->name, "prewink")) { + confp->timing.prewinktime = atoi(v->value); + } else if (!strcasecmp(v->name, "preflash")) { + confp->timing.preflashtime = atoi(v->value); + } else if (!strcasecmp(v->name, "wink")) { + confp->timing.winktime = atoi(v->value); + } else if (!strcasecmp(v->name, "flash")) { + confp->timing.flashtime = atoi(v->value); + } else if (!strcasecmp(v->name, "start")) { + confp->timing.starttime = atoi(v->value); + } else if (!strcasecmp(v->name, "rxwink")) { + confp->timing.rxwinktime = atoi(v->value); + } else if (!strcasecmp(v->name, "rxflash")) { + confp->timing.rxflashtime = atoi(v->value); + } else if (!strcasecmp(v->name, "debounce")) { + confp->timing.debouncetime = atoi(v->value); + } else if (!strcasecmp(v->name, "toneduration")) { + int toneduration; + int ctlfd; + int res; + struct dahdi_dialparams dps; + + #ifdef HAVE_ZAPTEL + ctlfd = open("/dev/zap/ctl", O_RDWR); + #else + ctlfd = open("/dev/dahdi/ctl", O_RDWR); + #endif - /* Substitute our scanned cadence */ - for (i = 0; i < 16; i++) { - new_cadence.ringcadence[i] = c[i]; - } + if (ctlfd == -1) { + ast_log(LOG_ERROR, "Unable to open /dev/dahdi/ctl to set toneduration\n"); + return -1; + } - if (cadence_is_ok) { - /* ---we scanned it without getting annoyed; now some sanity checks--- */ - if (element_count < 2) { - ast_log(LOG_ERROR, "Minimum cadence is ring,pause: %s\n", original_args); - } else { - if (cid_location == -1) { - /* user didn't say; default to first pause */ - cid_location = 1; - } else { - /* convert element_index to cidrings value */ - cid_location = (cid_location + 1) / 2; - } - /* ---we like their cadence; try to install it--- */ - if (!user_has_defined_cadences++) - /* this is the first user-defined cadence; clear the default user cadences */ - num_cadence = 0; - if ((num_cadence+1) >= NUM_CADENCE_MAX) - ast_log(LOG_ERROR, "Already %d cadences; can't add another: %s\n", NUM_CADENCE_MAX, original_args); - else { - cadences[num_cadence] = new_cadence; - cidrings[num_cadence++] = cid_location; - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "cadence 'r%d' added: %s\n",num_cadence,original_args); + toneduration = atoi(v->value); + if (toneduration > -1) { + memset(&dps, 0, sizeof(dps)); + + dps.dtmf_tonelen = dps.mfv1_tonelen = toneduration; + res = ioctl(ctlfd, DAHDI_SET_DIALPARAMS, &dps); + if (res < 0) { + ast_log(LOG_ERROR, "Invalid tone duration: %d ms: %s\n", toneduration, strerror(errno)); + return -1; } } - } - } else if (!strcasecmp(v->name, "ringtimeout")) { - ringt_base = (atoi(v->value) * 8) / READ_SIZE; - } else if (!strcasecmp(v->name, "prewink")) { - confp->timing.prewinktime = atoi(v->value); - } else if (!strcasecmp(v->name, "preflash")) { - confp->timing.preflashtime = atoi(v->value); - } else if (!strcasecmp(v->name, "wink")) { - confp->timing.winktime = atoi(v->value); - } else if (!strcasecmp(v->name, "flash")) { - confp->timing.flashtime = atoi(v->value); - } else if (!strcasecmp(v->name, "start")) { - confp->timing.starttime = atoi(v->value); - } else if (!strcasecmp(v->name, "rxwink")) { - confp->timing.rxwinktime = atoi(v->value); - } else if (!strcasecmp(v->name, "rxflash")) { - confp->timing.rxflashtime = atoi(v->value); - } else if (!strcasecmp(v->name, "debounce")) { - confp->timing.debouncetime = atoi(v->value); - } else if (!strcasecmp(v->name, "toneduration")) { - int toneduration; - int ctlfd; - int res; - struct dahdi_dialparams dps; - -#ifdef HAVE_ZAPTEL - ctlfd = open("/dev/zap/ctl", O_RDWR); -#else - ctlfd = open("/dev/dahdi/ctl", O_RDWR); -#endif - - if (ctlfd == -1) { - ast_log(LOG_ERROR, "Unable to open /dev/dahdi/ctl to set toneduration\n"); - return -1; - } - - toneduration = atoi(v->value); - if (toneduration > -1) { - memset(&dps, 0, sizeof(dps)); - - dps.dtmf_tonelen = dps.mfv1_tonelen = toneduration; - res = ioctl(ctlfd, DAHDI_SET_DIALPARAMS, &dps); - if (res < 0) { - ast_log(LOG_ERROR, "Invalid tone duration: %d ms: %s\n", toneduration, strerror(errno)); - return -1; - } - } - close(ctlfd); - } else if (!strcasecmp(v->name, "defaultcic")) { - ast_copy_string(defaultcic, v->value, sizeof(defaultcic)); - } else if (!strcasecmp(v->name, "defaultozz")) { - ast_copy_string(defaultozz, v->value, sizeof(defaultozz)); - } - } else if (!skipchannels) - ast_log(LOG_WARNING, "Ignoring %s\n", v->name); - } - if (dahdichan[0]) { - /* The user has set 'dahdichan' */ - /*< \todo pass proper line number instead of 0 */ - if (build_channels(confp, 0, dahdichan, reload, 0, &found_pseudo)) { - return -1; - } - } - /*< \todo why check for the pseudo in the per-channel section. - * Any actual use for manual setup of the pseudo channel? */ - if (!found_pseudo && reload == 0) { - /* Make sure pseudo isn't a member of any groups if - we're automatically making it. */ - - confp->chan.group = 0; - confp->chan.callgroup = 0; - confp->chan.pickupgroup = 0; - - tmp = mkintf(CHAN_PSEUDO, confp, NULL, reload); - - if (tmp) { - if (option_verbose > 2) - ast_verbose(VERBOSE_PREFIX_3 "Automatically generated pseudo channel\n"); - } else { - ast_log(LOG_WARNING, "Unable to register pseudo channel!\n"); + close(ctlfd); + } else if (!strcasecmp(v->name, "defaultcic")) { + ast_copy_string(defaultcic, v->value, sizeof(defaultcic)); + } else if (!strcasecmp(v->name, "defaultozz")) { + ast_copy_string(defaultozz, v->value, sizeof(defaultozz)); + } + } else if (!skipchannels) + ast_log(LOG_WARNING, "Ignoring %s\n", v->name); } - } - return 0; -} - -static int setup_dahdi(int reload) -{ - struct ast_config *cfg; - struct ast_variable *v; - struct dahdi_chan_conf conf = dahdi_chan_conf_default(); - int res; - -#ifdef HAVE_PRI - char *c; - int spanno; - int i, x; - int logicalspan; - int trunkgroup; - int dchannels[NUM_DCHANS]; -#endif - -#ifdef HAVE_ZAPTEL - int load_from_zapata_conf = 1; -#else - int load_from_zapata_conf = (dahdi_chan_mode == CHAN_ZAP_MODE); -#endif - - if (load_from_zapata_conf) { - if (!(cfg = ast_config_load("zapata.conf"))) { - ast_log(LOG_ERROR, "Unable to load zapata.conf\n"); - return 0; + if (dahdichan[0]) { + /* The user has set 'dahdichan' */ + /*< \todo pass proper line number instead of 0 */ + if (build_channels(confp, 0, dahdichan, reload, 0, &found_pseudo)) { + return -1; + } } - } else { - if (!(cfg = ast_config_load("chan_dahdi.conf"))) { - ast_log(LOG_ERROR, "Unable to load chan_dahdi.conf\n"); - return 0; + /*< \todo why check for the pseudo in the per-channel section. + * Any actual use for manual setup of the pseudo channel? */ + if (!found_pseudo && reload == 0) { + /* Make sure pseudo isn't a member of any groups if + we're automatically making it. */ + + confp->chan.group = 0; + confp->chan.callgroup = 0; + confp->chan.pickupgroup = 0; + + tmp = mkintf(CHAN_PSEUDO, confp, NULL, reload); + + if (tmp) { + if (option_verbose > 2) + ast_verbose(VERBOSE_PREFIX_3 "Automatically generated pseudo channel\n"); + } else { + ast_log(LOG_WARNING, "Unable to register pseudo channel!\n"); + } } + return 0; } - - /* It's a little silly to lock it, but we mind as well just to be sure */ - ast_mutex_lock(&iflock); -#ifdef HAVE_PRI - if (reload != 1) { - /* Process trunkgroups first */ - v = ast_variable_browse(cfg, "trunkgroups"); - while (v) { - if (!strcasecmp(v->name, "trunkgroup")) { - trunkgroup = atoi(v->value); - if (trunkgroup > 0) { - if ((c = strchr(v->value, ','))) { - i = 0; - memset(dchannels, 0, sizeof(dchannels)); - while (c && (i < NUM_DCHANS)) { - dchannels[i] = atoi(c + 1); - if (dchannels[i] < 0) { - ast_log(LOG_WARNING, "D-channel for trunk group %d must be a postiive number at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno); + + static int setup_dahdi(int reload) + { + struct ast_config *cfg; + struct ast_variable *v; + struct dahdi_chan_conf conf = dahdi_chan_conf_default(); + int res; + + #ifdef HAVE_PRI + char *c; + int spanno; + int i, x; + int logicalspan; + int trunkgroup; + int dchannels[NUM_DCHANS]; + #endif + + #ifdef HAVE_ZAPTEL + int load_from_zapata_conf = 1; + #else + int load_from_zapata_conf = (dahdi_chan_mode == CHAN_ZAP_MODE); + #endif + + if (load_from_zapata_conf) { + if (!(cfg = ast_config_load("zapata.conf"))) { + ast_log(LOG_ERROR, "Unable to load zapata.conf\n"); + return 0; + } + } else { + if (!(cfg = ast_config_load("chan_dahdi.conf"))) { + ast_log(LOG_ERROR, "Unable to load chan_dahdi.conf\n"); + return 0; + } + } + + /* It's a little silly to lock it, but we mind as well just to be sure */ + ast_mutex_lock(&iflock); + #ifdef HAVE_PRI + if (reload != 1) { + /* Process trunkgroups first */ + v = ast_variable_browse(cfg, "trunkgroups"); + while (v) { + if (!strcasecmp(v->name, "trunkgroup")) { + trunkgroup = atoi(v->value); + if (trunkgroup > 0) { + if ((c = strchr(v->value, ','))) { + i = 0; + memset(dchannels, 0, sizeof(dchannels)); + while (c && (i < NUM_DCHANS)) { + dchannels[i] = atoi(c + 1); + if (dchannels[i] < 0) { + ast_log(LOG_WARNING, "D-channel for trunk group %d must be a postiive number at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno); + } else + i++; + c = strchr(c + 1, ','); + } + if (i) { + if (pri_create_trunkgroup(trunkgroup, dchannels)) { + ast_log(LOG_WARNING, "Unable to create trunk group %d with Primary D-channel %d at line %d of chan_dahdi.conf\n", trunkgroup, dchannels[0], v->lineno); + } else if (option_verbose > 1) + ast_verbose(VERBOSE_PREFIX_2 "Created trunk group %d with Primary D-channel %d and %d backup%s\n", trunkgroup, dchannels[0], i - 1, (i == 1) ? "" : "s"); } else - i++; - c = strchr(c + 1, ','); - } - if (i) { - if (pri_create_trunkgroup(trunkgroup, dchannels)) { - ast_log(LOG_WARNING, "Unable to create trunk group %d with Primary D-channel %d at line %d of chan_dahdi.conf\n", trunkgroup, dchannels[0], v->lineno); - } else if (option_verbose > 1) - ast_verbose(VERBOSE_PREFIX_2 "Created trunk group %d with Primary D-channel %d and %d backup%s\n", trunkgroup, dchannels[0], i - 1, (i == 1) ? "" : "s"); + ast_log(LOG_WARNING, "Trunk group %d lacks any valid D-channels at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno); } else - ast_log(LOG_WARNING, "Trunk group %d lacks any valid D-channels at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno); + ast_log(LOG_WARNING, "Trunk group %d lacks a primary D-channel at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno); } else - ast_log(LOG_WARNING, "Trunk group %d lacks a primary D-channel at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno); - } else - ast_log(LOG_WARNING, "Trunk group identifier must be a positive integer at line %d of chan_dahdi.conf\n", v->lineno); - } else if (!strcasecmp(v->name, "spanmap")) { - spanno = atoi(v->value); - if (spanno > 0) { - if ((c = strchr(v->value, ','))) { - trunkgroup = atoi(c + 1); - if (trunkgroup > 0) { - if ((c = strchr(c + 1, ','))) - logicalspan = atoi(c + 1); - else - logicalspan = 0; - if (logicalspan >= 0) { - if (pri_create_spanmap(spanno - 1, trunkgroup, logicalspan)) { - ast_log(LOG_WARNING, "Failed to map span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan); - } else if (option_verbose > 1) - ast_verbose(VERBOSE_PREFIX_2 "Mapped span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan); + ast_log(LOG_WARNING, "Trunk group identifier must be a positive integer at line %d of chan_dahdi.conf\n", v->lineno); + } else if (!strcasecmp(v->name, "spanmap")) { + spanno = atoi(v->value); + if (spanno > 0) { + if ((c = strchr(v->value, ','))) { + trunkgroup = atoi(c + 1); + if (trunkgroup > 0) { + if ((c = strchr(c + 1, ','))) + logicalspan = atoi(c + 1); + else + logicalspan = 0; + if (logicalspan >= 0) { + if (pri_create_spanmap(spanno - 1, trunkgroup, logicalspan)) { + ast_log(LOG_WARNING, "Failed to map span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan); + } else if (option_verbose > 1) + ast_verbose(VERBOSE_PREFIX_2 "Mapped span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan); + } else + ast_log(LOG_WARNING, "Logical span must be a postive number, or '0' (for unspecified) at line %d of chan_dahdi.conf\n", v->lineno); } else - ast_log(LOG_WARNING, "Logical span must be a postive number, or '0' (for unspecified) at line %d of chan_dahdi.conf\n", v->lineno); + ast_log(LOG_WARNING, "Trunk group must be a postive number at line %d of chan_dahdi.conf\n", v->lineno); } else - ast_log(LOG_WARNING, "Trunk group must be a postive number at line %d of chan_dahdi.conf\n", v->lineno); + ast_log(LOG_WARNING, "Missing trunk group for span map at line %d of chan_dahdi.conf\n", v->lineno); } else - ast_log(LOG_WARNING, "Missing trunk group for span map at line %d of chan_dahdi.conf\n", v->lineno); - } else - ast_log(LOG_WARNING, "Span number must be a postive integer at line %d of chan_dahdi.conf\n", v->lineno); - } else { - ast_log(LOG_NOTICE, "Ignoring unknown keyword '%s' in trunkgroups\n", v->name); + ast_log(LOG_WARNING, "Span number must be a postive integer at line %d of chan_dahdi.conf\n", v->lineno); + } else { + ast_log(LOG_NOTICE, "Ignoring unknown keyword '%s' in trunkgroups\n", v->name); + } + v = v->next; } - v = v->next; } - } -#endif + #endif + + /* Copy the default jb config over global_jbconf */ + memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf)); - /* Copy the default jb config over global_jbconf */ - memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf)); - - v = ast_variable_browse(cfg, "channels"); - res = process_dahdi(&conf, "", v, reload, 0); - ast_mutex_unlock(&iflock); - ast_config_destroy(cfg); - if (res) - return res; - cfg = ast_config_load("users.conf"); - if (cfg) { - char *cat; - const char *chans; - process_dahdi(&conf, "", ast_variable_browse(cfg, "general"), 1, 1); - for (cat = ast_category_browse(cfg, NULL); cat ; cat = ast_category_browse(cfg, cat)) { - if (!strcasecmp(cat, "general")) - continue; - chans = ast_variable_retrieve(cfg, cat, "dahdichan"); - if (!ast_strlen_zero(chans)) { - struct dahdi_chan_conf sect_conf; - memcpy(§_conf, &conf, sizeof(sect_conf)); - - process_dahdi(§_conf, cat, ast_variable_browse(cfg, cat), reload, 0); + v = ast_variable_browse(cfg, "channels"); + res = process_dahdi(&conf, "", v, reload, 0); + ast_mutex_unlock(&iflock); + ast_config_destroy(cfg); + if (res) + return res; + cfg = ast_config_load("users.conf"); + if (cfg) { + char *cat; + const char *chans; + process_dahdi(&conf, "", ast_variable_browse(cfg, "general"), 1, 1); + for (cat = ast_category_browse(cfg, NULL); cat ; cat = ast_category_browse(cfg, cat)) { + if (!strcasecmp(cat, "general")) + continue; + chans = ast_variable_retrieve(cfg, cat, "dahdichan"); + if (!ast_strlen_zero(chans)) { + struct dahdi_chan_conf sect_conf; + memcpy(§_conf, &conf, sizeof(sect_conf)); + + process_dahdi(§_conf, cat, ast_variable_browse(cfg, cat), reload, 0); + } } + ast_config_destroy(cfg); } - ast_config_destroy(cfg); - } -#ifdef HAVE_PRI - if (reload != 1) { - for (x = 0; x < NUM_SPANS; x++) { - if (pris[x].pvts[0]) { - if (start_pri(pris + x)) { - ast_log(LOG_ERROR, "Unable to start D-channel on span %d\n", x + 1); - return -1; - } else if (option_verbose > 1) - ast_verbose(VERBOSE_PREFIX_2 "Starting D-Channel on span %d\n", x + 1); + #ifdef HAVE_PRI + if (reload != 1) { + for (x = 0; x < NUM_SPANS; x++) { + if (pris[x].pvts[0]) { + if (start_pri(pris + x)) { + ast_log(LOG_ERROR, "Unable to start D-channel on span %d\n", x + 1); + return -1; + } else if (option_verbose > 1) + ast_verbose(VERBOSE_PREFIX_2 "Starting D-Channel on span %d\n", x + 1); + } } } + #endif + /* And start the monitor for the first time */ + restart_monitor(); + return 0; } -#endif - /* And start the monitor for the first time */ - restart_monitor(); - return 0; -} - -#define local_astman_register(a, b, c, d) do { \ - if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) { \ - ast_manager_register("DAHDI" a, b, dahdi_ ## c, d); \ - } \ - ast_manager_register("Zap" a, b, zap_ ## c, d); \ - } while (0) - -static int load_module(void) -{ - int res; - -#ifdef HAVE_PRI - int y,i; - memset(pris, 0, sizeof(pris)); - for (y = 0; y < NUM_SPANS; y++) { - ast_mutex_init(&pris[y].lock); - pris[y].offset = -1; - pris[y].master = AST_PTHREADT_NULL; - for (i = 0; i < NUM_DCHANS; i++) - pris[y].fds[i] = -1; - } - pri_set_error(dahdi_pri_error); - pri_set_message(dahdi_pri_message); - if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) { - ast_register_application(dahdi_send_keypad_facility_app, dahdi_send_keypad_facility_exec, - dahdi_send_keypad_facility_synopsis, dahdi_send_keypad_facility_descrip); - } - ast_register_application(zap_send_keypad_facility_app, zap_send_keypad_facility_exec, - zap_send_keypad_facility_synopsis, zap_send_keypad_facility_descrip); -#endif - if ((res = setup_dahdi(0))) { - return AST_MODULE_LOAD_DECLINE; - } - if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) { - chan_tech = &dahdi_tech; - } else { - chan_tech = &zap_tech; - } - if (ast_channel_register(chan_tech)) { - ast_log(LOG_ERROR, "Unable to register channel class '%s'\n", chan_tech->type); - __unload_module(); - return -1; - } -#ifdef HAVE_PRI - ast_string_field_init(&inuse, 16); - ast_string_field_set(&inuse, name, "GR-303InUse"); - ast_cli_register_multiple(dahdi_pri_cli, sizeof(dahdi_pri_cli) / sizeof(struct ast_cli_entry)); -#endif - ast_cli_register_multiple(dahdi_cli, sizeof(dahdi_cli) / sizeof(struct ast_cli_entry)); - memset(round_robin, 0, sizeof(round_robin)); - local_astman_register("Transfer", 0, action_transfer, "Transfer Channel"); - local_astman_register("Hangup", 0, action_transferhangup, "Hangup Channel"); - local_astman_register("DialOffHook", 0, action_dialoffhook, "Dial over channel while offhook"); - local_astman_register("DNDon", 0, action_dndon, "Toggle channel Do Not Disturb status ON"); - local_astman_register("DNDoff", 0, action_dndoff, "Toggle channel Do Not Disturb status OFF"); - local_astman_register("ShowChannels", 0, action_showchannels, "Show status channels"); - local_astman_register("Restart", 0, action_restart, "Fully Restart channels (terminates calls)"); - - ast_cond_init(&ss_thread_complete, NULL); - - return res; -} - -static int dahdi_sendtext(struct ast_channel *c, const char *text) -{ -#define END_SILENCE_LEN 400 -#define HEADER_MS 50 -#define TRAILER_MS 5 -#define HEADER_LEN ((HEADER_MS + TRAILER_MS) * 8) -#define ASCII_BYTES_PER_CHAR 80 - - unsigned char *buf,*mybuf; - struct dahdi_pvt *p = c->tech_pvt; - struct pollfd fds[1]; - int size,res,fd,len,x; - int bytes=0; - /* Initial carrier (imaginary) */ - float cr = 1.0; - float ci = 0.0; - float scont = 0.0; - int index; - - index = dahdi_get_index(c, p, 0); - if (index < 0) { - ast_log(LOG_WARNING, "Huh? I don't exist?\n"); - return -1; - } - if (!text[0]) return(0); /* if nothing to send, dont */ - if ((!p->tdd) && (!p->mate)) return(0); /* if not in TDD mode, just return */ - if (p->mate) - buf = ast_malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN); - else - buf = ast_malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN); - if (!buf) - return -1; - mybuf = buf; - if (p->mate) { - int codec = AST_LAW(p); - for (x = 0; x < HEADER_MS; x++) { /* 50 ms of Mark */ - PUT_CLID_MARKMS; - } - /* Put actual message */ - for (x = 0; text[x]; x++) { - PUT_CLID(text[x]); + #define local_astman_register(a, b, c, d) do { \ + if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) { \ + ast_manager_register("DAHDI" a, b, dahdi_ ## c, d); \ + } \ + ast_manager_register("Zap" a, b, zap_ ## c, d); \ + } while (0) + + static int load_module(void) + { + int res; + + #ifdef HAVE_PRI + int y,i; + memset(pris, 0, sizeof(pris)); + for (y = 0; y < NUM_SPANS; y++) { + ast_mutex_init(&pris[y].lock); + pris[y].offset = -1; + pris[y].master = AST_PTHREADT_NULL; + for (i = 0; i < NUM_DCHANS; i++) + pris[y].fds[i] = -1; + } + pri_set_error(dahdi_pri_error); + pri_set_message(dahdi_pri_message); + if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) { + ast_register_application(dahdi_send_keypad_facility_app, dahdi_send_keypad_facility_exec, + dahdi_send_keypad_facility_synopsis, dahdi_send_keypad_facility_descrip); } - for (x = 0; x < TRAILER_MS; x++) { /* 5 ms of Mark */ - PUT_CLID_MARKMS; + ast_register_application(zap_send_keypad_facility_app, zap_send_keypad_facility_exec, + zap_send_keypad_facility_synopsis, zap_send_keypad_facility_descrip); + #endif + if ((res = setup_dahdi(0))) { + return AST_MODULE_LOAD_DECLINE; } - len = bytes; - buf = mybuf; - } else { - len = tdd_generate(p->tdd, buf, text); - if (len < 1) { - ast_log(LOG_ERROR, "TDD generate (len %d) failed!!\n", (int)strlen(text)); - free(mybuf); + if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) { + chan_tech = &dahdi_tech; + } else { + chan_tech = &zap_tech; + } + if (ast_channel_register(chan_tech)) { + ast_log(LOG_ERROR, "Unable to register channel class '%s'\n", chan_tech->type); + __unload_module(); return -1; } + #ifdef HAVE_PRI + ast_string_field_init(&inuse, 16); + ast_string_field_set(&inuse, name, "GR-303InUse"); + ast_cli_register_multiple(dahdi_pri_cli, sizeof(dahdi_pri_cli) / sizeof(struct ast_cli_entry)); + #endif + ast_cli_register_multiple(dahdi_cli, sizeof(dahdi_cli) / sizeof(struct ast_cli_entry)); + + memset(round_robin, 0, sizeof(round_robin)); + local_astman_register("Transfer", 0, action_transfer, "Transfer Channel"); + local_astman_register("Hangup", 0, action_transferhangup, "Hangup Channel"); + local_astman_register("DialOffHook", 0, action_dialoffhook, "Dial over channel while offhook"); + local_astman_register("DNDon", 0, action_dndon, "Toggle channel Do Not Disturb status ON"); + local_astman_register("DNDoff", 0, action_dndoff, "Toggle channel Do Not Disturb status OFF"); + local_astman_register("ShowChannels", 0, action_showchannels, "Show status channels"); + local_astman_register("Restart", 0, action_restart, "Fully Restart channels (terminates calls)"); + + ast_cond_init(&ss_thread_complete, NULL); + + return res; } - memset(buf + len, 0x7f, END_SILENCE_LEN); - len += END_SILENCE_LEN; - fd = p->subs[index].dfd; - while (len) { - if (ast_check_hangup(c)) { - free(mybuf); + + static int dahdi_sendtext(struct ast_channel *c, const char *text) + { + #define END_SILENCE_LEN 400 + #define HEADER_MS 50 + #define TRAILER_MS 5 + #define HEADER_LEN ((HEADER_MS + TRAILER_MS) * 8) + #define ASCII_BYTES_PER_CHAR 80 + + unsigned char *buf,*mybuf; + struct dahdi_pvt *p = c->tech_pvt; + struct pollfd fds[1]; + int size,res,fd,len,x; + int bytes=0; + /* Initial carrier (imaginary) */ + float cr = 1.0; + float ci = 0.0; + float scont = 0.0; + int index; + + index = dahdi_get_index(c, p, 0); + if (index < 0) { + ast_log(LOG_WARNING, "Huh? I don't exist?\n"); return -1; } - size = len; - if (size > READ_SIZE) - size = READ_SIZE; - fds[0].fd = fd; - fds[0].events = POLLOUT | POLLPRI; - fds[0].revents = 0; - res = poll(fds, 1, -1); - if (!res) { - ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel); - continue; - } - /* if got exception */ - if (fds[0].revents & POLLPRI) { - ast_free(mybuf); + if (!text[0]) return(0); /* if nothing to send, dont */ + if ((!p->tdd) && (!p->mate)) return(0); /* if not in TDD mode, just return */ + if (p->mate) + buf = ast_malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN); + else + buf = ast_malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN); + if (!buf) return -1; + mybuf = buf; + if (p->mate) { + int codec = AST_LAW(p); + for (x = 0; x < HEADER_MS; x++) { /* 50 ms of Mark */ + PUT_CLID_MARKMS; + } + /* Put actual message */ + for (x = 0; text[x]; x++) { + PUT_CLID(text[x]); + } + for (x = 0; x < TRAILER_MS; x++) { /* 5 ms of Mark */ + PUT_CLID_MARKMS; + } + len = bytes; + buf = mybuf; + } else { + len = tdd_generate(p->tdd, buf, text); + if (len < 1) { + ast_log(LOG_ERROR, "TDD generate (len %d) failed!!\n", (int)strlen(text)); + free(mybuf); + return -1; + } } - if (!(fds[0].revents & POLLOUT)) { - ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel); - continue; - } - res = write(fd, buf, size); - if (res != size) { - if (res == -1) { + memset(buf + len, 0x7f, END_SILENCE_LEN); + len += END_SILENCE_LEN; + fd = p->subs[index].dfd; + while (len) { + if (ast_check_hangup(c)) { free(mybuf); return -1; } - if (option_debug) - ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel); - break; + size = len; + if (size > READ_SIZE) + size = READ_SIZE; + fds[0].fd = fd; + fds[0].events = POLLOUT | POLLPRI; + fds[0].revents = 0; + res = poll(fds, 1, -1); + if (!res) { + ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel); + continue; + } + /* if got exception */ + if (fds[0].revents & POLLPRI) { + ast_free(mybuf); + return -1; + } + if (!(fds[0].revents & POLLOUT)) { + ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel); + continue; + } + res = write(fd, buf, size); + if (res != size) { + if (res == -1) { + free(mybuf); + return -1; + } + if (option_debug) + ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel); + break; + } + len -= size; + buf += size; } - len -= size; - buf += size; + free(mybuf); + return(0); } - free(mybuf); - return(0); -} - - -static int reload(void) -{ - int res = 0; - - res = setup_dahdi(1); - if (res) { - ast_log(LOG_WARNING, "Reload of chan_dahdi.so is unsuccessful!\n"); - return -1; + + + static int reload(void) + { + int res = 0; + + res = setup_dahdi(1); + if (res) { + ast_log(LOG_WARNING, "Reload of chan_dahdi.so is unsuccessful!\n"); + return -1; + } + return 0; } - return 0; -} - -/* This is a workaround so that menuselect displays a proper description - * AST_MODULE_INFO(, , "DAHDI Telephony" - */ - -#ifdef DAHDI_PRI -#define tdesc "DAHDI Telephony w/PRI" -#else -#define tdesc "DAHDI Telephony" -#endif - -AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, tdesc, - .load = load_module, - .unload = unload_module, - .reload = reload, - ); + + /* This is a workaround so that menuselect displays a proper description + * AST_MODULE_INFO(, , "DAHDI Telephony" + */ + + #ifdef DAHDI_PRI + #define tdesc "DAHDI Telephony w/PRI" + #else + #define tdesc "DAHDI Telephony" + #endif + + AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, tdesc, + .load = load_module, + .unload = unload_module, + .reload = reload, + ); + --- /tmp/asterisk/apps/app_dial.c 2008-11-14 02:10:18.000000000 +0200 +++ apps/app_dial.c 2008-11-14 01:19:54.000000000 +0200 @@ -1289,6 +1289,8 @@ tmp->chan->adsicpe = chan->adsicpe; /* Pass the transfer capability */ tmp->chan->transfercapability = chan->transfercapability; + /* Pass the user information layer 1 */ + tmp->chan->userinformationlayer1 = chan->userinformationlayer1; /* If we have an outbound group, set this peer channel to it */ if (outbound_group) --- /tmp/asterisk/funcs/func_channel.c 2008-11-14 02:10:21.000000000 +0200 +++ funcs/func_channel.c 2008-11-14 01:29:04.000000000 +0200 @@ -88,7 +88,11 @@ locked_copy_string(chan, buf, chan->tech->type, len); else if (!strcasecmp(data, "transfercapability")) locked_copy_string(chan, buf, transfercapability_table[chan->transfercapability & 0x1f], len); - else if (!strcasecmp(data, "callgroup")) { + else if (!strcasecmp(data, "userinformationlayer1")) { + char ul1[10]; + snprintf(ul1,9,"%d",chan->userinformationlayer1); + locked_copy_string(chan, buf, ul1, len); + } else if (!strcasecmp(data, "callgroup")) { char groupbuf[256]; locked_copy_string(chan, buf, ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len); } else if (!chan->tech->func_channel_read @@ -133,10 +137,11 @@ break; } } + } else if (!strcasecmp(data, "userinformationlayer1")) { + chan->userinformationlayer1 = atoi(value); } else if (!chan->tech->func_channel_write - || chan->tech->func_channel_write(chan, function, data, value)) { - ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", - data); + || chan->tech->func_channel_write(chan, function, data, value)) { + ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data); ret = -1; } @@ -158,7 +163,9 @@ "R/W musicclass class (from musiconhold.conf) for hold music\n" "R/W rxgain set rxgain level on channel drivers that support it\n" "R/O state state for channel\n" + "R/W transfercapability ISDN transfer capability (part of ISDN transfer capability)\n" "R/W tonezone zone for indications played\n" + "R/W userinformationlayer1 ISDN User Information Layer 1 (part of ISDN transfer capability)\n" "R/W txgain set txgain level on channel drivers that support it\n" "R/O videonativeformat format used natively for video\n" "\n" --- /tmp/asterisk/include/asterisk/channel.h 2008-11-14 02:10:22.000000000 +0200 +++ include/asterisk/channel.h 2008-11-14 01:22:03.000000000 +0200 @@ -434,6 +434,7 @@ ast_group_t pickupgroup; /*!< Pickup group - which calls groups can be picked up? */ unsigned int flags; /*!< channel flags of AST_FLAG_ type */ unsigned short transfercapability; /*!< ISDN Transfer Capbility - AST_FLAG_DIGITAL is not enough */ + unsigned short userinformationlayer1; /*!< ISDN User Information Layer 1 - transfercapability is not enough */ AST_LIST_HEAD_NOLOCK(, ast_frame) readq; int alertpipe[2]; --- /tmp/asterisk/configs/chan_dahdi.conf.sample 2008-11-14 02:10:23.000000000 +0200 +++ configs/chan_dahdi.conf.sample 2008-11-14 01:31:05.000000000 +0200 @@ -253,6 +253,15 @@ ; dtmf = DTMF as used in Denmark, Sweden and Netherlands ; smdi = Use SMDI for callerid. Requires SMDI to be enabled (usesmdi). ; +; H324M signalling. 3G UMTS video calls require to set a certain User Information Layer 1 +; value. The UL1 can be set either in the Bearer Capability Information Element or in the +; Low Layer Compatiblity Information Element. Depending on the switch were your PRI is +; connected to, you need one or the other. +; Allowed values: bc or llc +; Default value: bc +; +;h324m=lcc +; ;cidsignalling=bell ; ; What signals the start of caller ID