Sun Oct 16 2011 08:41:47

Asterisk developer's documentation


sig_analog.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2009, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Analog signaling module
00022  *
00023  * \author Matthew Fredrickson <creslin@digium.com>
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 #include <errno.h>
00029 #include <ctype.h>
00030 
00031 #include "asterisk/utils.h"
00032 #include "asterisk/options.h"
00033 #include "asterisk/pbx.h"
00034 #include "asterisk/file.h"
00035 #include "asterisk/callerid.h"
00036 #include "asterisk/say.h"
00037 #include "asterisk/manager.h"
00038 #include "asterisk/astdb.h"
00039 #include "asterisk/features.h"
00040 #include "asterisk/cel.h"
00041 #include "asterisk/causes.h"
00042 
00043 #include "sig_analog.h"
00044 
00045 /*! \note
00046  * Define if you want to check the hook state for an FXO (FXS signalled) interface
00047  * before dialing on it.  Certain FXO interfaces always think they're out of
00048  * service with this method however.
00049  */
00050 /* #define DAHDI_CHECK_HOOKSTATE */
00051 
00052 #define POLARITY_IDLE 0
00053 #define POLARITY_REV    1
00054 #define MIN_MS_SINCE_FLASH       ( (2000) )  /*!< 2000 ms */
00055 static int analog_matchdigittimeout = 3000;
00056 static int analog_gendigittimeout = 8000;
00057 static int analog_firstdigittimeout = 16000;
00058 static char analog_defaultcic[64] = "";
00059 static char analog_defaultozz[64] = "";
00060 
00061 static const struct {
00062    enum analog_sigtype sigtype;
00063    const char const *name;
00064 } sigtypes[] = {
00065    { ANALOG_SIG_FXOLS, "fxo_ls" },
00066    { ANALOG_SIG_FXOKS, "fxo_ks" },
00067    { ANALOG_SIG_FXOGS, "fxo_gs" },
00068    { ANALOG_SIG_FXSLS, "fxs_ls" },
00069    { ANALOG_SIG_FXSKS, "fxs_ks" },
00070    { ANALOG_SIG_FXSGS, "fxs_gs" },
00071    { ANALOG_SIG_EMWINK, "em_w" },
00072    { ANALOG_SIG_EM, "em" },
00073    { ANALOG_SIG_EM_E1, "em_e1" },
00074    { ANALOG_SIG_FEATD, "featd" },
00075    { ANALOG_SIG_FEATDMF, "featdmf" },
00076    { ANALOG_SIG_FEATDMF_TA, "featdmf_ta" },
00077    { ANALOG_SIG_FEATB, "featb" },
00078    { ANALOG_SIG_FGC_CAMA, "fgccama" },
00079    { ANALOG_SIG_FGC_CAMAMF, "fgccamamf" },
00080    { ANALOG_SIG_SF, "sf" },
00081    { ANALOG_SIG_SFWINK, "sf_w" },
00082    { ANALOG_SIG_SF_FEATD, "sf_featd" },
00083    { ANALOG_SIG_SF_FEATDMF, "sf_featdmf" },
00084    { ANALOG_SIG_SF_FEATB, "sf_featb" },
00085    { ANALOG_SIG_E911, "e911" },
00086 };
00087 
00088 static const struct {
00089    unsigned int cid_type;
00090    const char const *name;
00091 } cidtypes[] = {
00092    { CID_SIG_BELL,   "bell" },
00093    { CID_SIG_V23,    "v23" },
00094    { CID_SIG_V23_JP, "v23_jp" },
00095    { CID_SIG_DTMF,   "dtmf" },
00096    /* "smdi" is intentionally not supported here, as there is a much better
00097     * way to do this in the dialplan now. */
00098 };
00099 
00100 #define ISTRUNK(p) ((p->sig == ANALOG_SIG_FXSLS) || (p->sig == ANALOG_SIG_FXSKS) || \
00101                (p->sig == ANALOG_SIG_FXSGS))
00102 
00103 enum analog_sigtype analog_str_to_sigtype(const char *name)
00104 {
00105    int i;
00106 
00107    for (i = 0; i < ARRAY_LEN(sigtypes); i++) {
00108       if (!strcasecmp(sigtypes[i].name, name)) {
00109          return sigtypes[i].sigtype;
00110       }
00111    }
00112 
00113    return 0;
00114 }
00115 
00116 const char *analog_sigtype_to_str(enum analog_sigtype sigtype)
00117 {
00118    int i;
00119 
00120    for (i = 0; i < ARRAY_LEN(sigtypes); i++) {
00121       if (sigtype == sigtypes[i].sigtype) {
00122          return sigtypes[i].name;
00123       }
00124    }
00125 
00126    return "Unknown";
00127 }
00128 
00129 unsigned int analog_str_to_cidtype(const char *name)
00130 {
00131    int i;
00132 
00133    for (i = 0; i < ARRAY_LEN(cidtypes); i++) {
00134       if (!strcasecmp(cidtypes[i].name, name)) {
00135          return cidtypes[i].cid_type;
00136       }
00137    }
00138 
00139    return 0;
00140 }
00141 
00142 const char *analog_cidtype_to_str(unsigned int cid_type)
00143 {
00144    int i;
00145 
00146    for (i = 0; i < ARRAY_LEN(cidtypes); i++) {
00147       if (cid_type == cidtypes[i].cid_type) {
00148          return cidtypes[i].name;
00149       }
00150    }
00151 
00152    return "Unknown";
00153 }
00154 
00155 static int analog_start_cid_detect(struct analog_pvt *p, int cid_signalling)
00156 {
00157    if (p->calls->start_cid_detect) {
00158       return p->calls->start_cid_detect(p->chan_pvt, cid_signalling);
00159    }
00160    return -1;
00161 }
00162 
00163 static int analog_stop_cid_detect(struct analog_pvt *p)
00164 {
00165    if (p->calls->stop_cid_detect) {
00166       return p->calls->stop_cid_detect(p->chan_pvt);
00167    }
00168    return -1;
00169 }
00170 
00171 static int analog_get_callerid(struct analog_pvt *p, char *name, char *number, enum analog_event *ev, size_t timeout)
00172 {
00173    if (p->calls->get_callerid) {
00174       return p->calls->get_callerid(p->chan_pvt, name, number, ev, timeout);
00175    }
00176    return -1;
00177 }
00178 
00179 static const char *analog_get_orig_dialstring(struct analog_pvt *p)
00180 {
00181    if (p->calls->get_orig_dialstring) {
00182       return p->calls->get_orig_dialstring(p->chan_pvt);
00183    }
00184    return "";
00185 }
00186 
00187 static int analog_get_event(struct analog_pvt *p)
00188 {
00189    if (p->calls->get_event) {
00190       return p->calls->get_event(p->chan_pvt);
00191    }
00192    return -1;
00193 }
00194 
00195 static int analog_wait_event(struct analog_pvt *p)
00196 {
00197    if (p->calls->wait_event) {
00198       return p->calls->wait_event(p->chan_pvt);
00199    }
00200    return -1;
00201 }
00202 
00203 enum analog_cid_start analog_str_to_cidstart(const char *value)
00204 {
00205    if (!strcasecmp(value, "ring")) {
00206       return ANALOG_CID_START_RING;
00207    } else if (!strcasecmp(value, "polarity")) {
00208       return ANALOG_CID_START_POLARITY;
00209    } else if (!strcasecmp(value, "polarity_in")) {
00210       return ANALOG_CID_START_POLARITY_IN;
00211    } else if (!strcasecmp(value, "dtmf")) {
00212       return ANALOG_CID_START_DTMF_NOALERT;
00213    }
00214 
00215    return 0;
00216 }
00217 
00218 const char *analog_cidstart_to_str(enum analog_cid_start cid_start)
00219 {
00220    switch (cid_start) {
00221    case ANALOG_CID_START_RING:
00222       return "Ring";
00223    case ANALOG_CID_START_POLARITY:
00224       return "Polarity";
00225    case ANALOG_CID_START_POLARITY_IN:
00226       return "Polarity_In";
00227    case ANALOG_CID_START_DTMF_NOALERT:
00228       return "DTMF";
00229    }
00230 
00231    return "Unknown";
00232 }
00233 
00234 static char *analog_event2str(enum analog_event event)
00235 {
00236    char *res;
00237    switch (event) {
00238    case ANALOG_EVENT_ONHOOK:
00239       res = "ANALOG_EVENT_ONHOOK";
00240       break;
00241    case ANALOG_EVENT_RINGOFFHOOK:
00242       res = "ANALOG_EVENT_RINGOFFHOOK";
00243       break;
00244    case ANALOG_EVENT_WINKFLASH:
00245       res = "ANALOG_EVENT_WINKFLASH";
00246       break;
00247    case ANALOG_EVENT_ALARM:
00248       res = "ANALOG_EVENT_ALARM";
00249       break;
00250    case ANALOG_EVENT_NOALARM:
00251       res = "ANALOG_EVENT_NOALARM";
00252       break;
00253    case ANALOG_EVENT_DIALCOMPLETE:
00254       res = "ANALOG_EVENT_DIALCOMPLETE";
00255       break;
00256    case ANALOG_EVENT_HOOKCOMPLETE:
00257       res = "ANALOG_EVENT_HOOKCOMPLETE";
00258       break;
00259    case ANALOG_EVENT_PULSE_START:
00260       res = "ANALOG_EVENT_PULSE_START";
00261       break;
00262    case ANALOG_EVENT_POLARITY:
00263       res = "ANALOG_EVENT_POLARITY";
00264       break;
00265    case ANALOG_EVENT_RINGBEGIN:
00266       res = "ANALOG_EVENT_RINGBEGIN";
00267       break;
00268    case ANALOG_EVENT_EC_DISABLED:
00269       res = "ANALOG_EVENT_EC_DISABLED";
00270       break;
00271    case ANALOG_EVENT_RINGERON:
00272       res = "ANALOG_EVENT_RINGERON";
00273       break;
00274    case ANALOG_EVENT_RINGEROFF:
00275       res = "ANALOG_EVENT_RINGEROFF";
00276       break;
00277    case ANALOG_EVENT_REMOVED:
00278       res = "ANALOG_EVENT_REMOVED";
00279       break;
00280    case ANALOG_EVENT_NEONMWI_ACTIVE:
00281       res = "ANALOG_EVENT_NEONMWI_ACTIVE";
00282       break;
00283    case ANALOG_EVENT_NEONMWI_INACTIVE:
00284       res = "ANALOG_EVENT_NEONMWI_INACTIVE";
00285       break;
00286 #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
00287    case ANALOG_EVENT_TX_CED_DETECTED:
00288       res = "ANALOG_EVENT_TX_CED_DETECTED";
00289       break;
00290    case ANALOG_EVENT_RX_CED_DETECTED:
00291       res = "ANALOG_EVENT_RX_CED_DETECTED";
00292       break;
00293    case ANALOG_EVENT_EC_NLP_DISABLED:
00294       res = "ANALOG_EVENT_EC_NLP_DISABLED";
00295       break;
00296    case ANALOG_EVENT_EC_NLP_ENABLED:
00297       res = "ANALOG_EVENT_EC_NLP_ENABLED";
00298       break;
00299 #endif
00300    case ANALOG_EVENT_PULSEDIGIT:
00301       res = "ANALOG_EVENT_PULSEDIGIT";
00302       break;
00303    case ANALOG_EVENT_DTMFDOWN:
00304       res = "ANALOG_EVENT_DTMFDOWN";
00305       break;
00306    case ANALOG_EVENT_DTMFUP:
00307       res = "ANALOG_EVENT_DTMFUP";
00308       break;
00309    default:
00310       res = "UNKNOWN/OTHER";
00311       break;
00312    }
00313 
00314    return res;
00315 }
00316 
00317 static void analog_swap_subs(struct analog_pvt *p, enum analog_sub a, enum analog_sub b)
00318 {
00319    int tinthreeway;
00320    struct ast_channel *towner;
00321 
00322    ast_debug(1, "Swapping %d and %d\n", a, b);
00323 
00324    towner = p->subs[a].owner;
00325    p->subs[a].owner = p->subs[b].owner;
00326    p->subs[b].owner = towner;
00327 
00328    tinthreeway = p->subs[a].inthreeway;
00329    p->subs[a].inthreeway = p->subs[b].inthreeway;
00330    p->subs[b].inthreeway = tinthreeway;
00331 
00332    if (p->calls->swap_subs) {
00333       p->calls->swap_subs(p->chan_pvt, a, p->subs[a].owner, b, p->subs[b].owner);
00334    }
00335 }
00336 
00337 static int analog_alloc_sub(struct analog_pvt *p, enum analog_sub x)
00338 {
00339    if (p->calls->allocate_sub) {
00340       int res;
00341       res = p->calls->allocate_sub(p->chan_pvt, x);
00342       if (!res) {
00343          p->subs[x].allocd = 1;
00344       }
00345       return res;
00346    }
00347    return 0;
00348 }
00349 
00350 static int analog_unalloc_sub(struct analog_pvt *p, enum analog_sub x)
00351 {
00352    p->subs[x].allocd = 0;
00353    p->subs[x].owner = NULL;
00354    if (p->calls->unallocate_sub) {
00355       return p->calls->unallocate_sub(p->chan_pvt, x);
00356    }
00357    return 0;
00358 }
00359 
00360 static int analog_send_callerid(struct analog_pvt *p, int cwcid, struct ast_party_caller *caller)
00361 {
00362    ast_debug(1, "Sending callerid.  CID_NAME: '%s' CID_NUM: '%s'\n",
00363       caller->id.name.str,
00364       caller->id.number.str);
00365 
00366    if (cwcid) {
00367       p->callwaitcas = 0;
00368    }
00369 
00370    if (p->calls->send_callerid) {
00371       return p->calls->send_callerid(p->chan_pvt, cwcid, caller);
00372    }
00373    return 0;
00374 }
00375 
00376 #define analog_get_index(ast, p, nullok)  _analog_get_index(ast, p, nullok, __PRETTY_FUNCTION__, __LINE__)
00377 static int _analog_get_index(struct ast_channel *ast, struct analog_pvt *p, int nullok, const char *fname, unsigned long line)
00378 {
00379    int res;
00380    if (p->subs[ANALOG_SUB_REAL].owner == ast) {
00381       res = ANALOG_SUB_REAL;
00382    } else if (p->subs[ANALOG_SUB_CALLWAIT].owner == ast) {
00383       res = ANALOG_SUB_CALLWAIT;
00384    } else if (p->subs[ANALOG_SUB_THREEWAY].owner == ast) {
00385       res = ANALOG_SUB_THREEWAY;
00386    } else {
00387       res = -1;
00388       if (!nullok) {
00389          ast_log(LOG_WARNING,
00390             "Unable to get index for '%s' on channel %d (%s(), line %lu)\n",
00391             ast ? ast->name : "", p->channel, fname, line);
00392       }
00393    }
00394    return res;
00395 }
00396 
00397 static int analog_dsp_reset_and_flush_digits(struct analog_pvt *p)
00398 {
00399    if (p->calls->dsp_reset_and_flush_digits) {
00400       return p->calls->dsp_reset_and_flush_digits(p->chan_pvt);
00401    }
00402 
00403    /* Return 0 since I think this is unnecessary to do in most cases it is used.  Mostly only for ast_dsp */
00404    return 0;
00405 }
00406 
00407 static int analog_play_tone(struct analog_pvt *p, enum analog_sub sub, enum analog_tone tone)
00408 {
00409    if (p->calls->play_tone) {
00410       return p->calls->play_tone(p->chan_pvt, sub, tone);
00411    }
00412    return -1;
00413 }
00414 
00415 static void analog_set_new_owner(struct analog_pvt *p, struct ast_channel *new_owner)
00416 {
00417    p->owner = new_owner;
00418    if (p->calls->set_new_owner) {
00419       p->calls->set_new_owner(p->chan_pvt, new_owner);
00420    }
00421 }
00422 
00423 static struct ast_channel * analog_new_ast_channel(struct analog_pvt *p, int state, int startpbx, enum analog_sub sub, const struct ast_channel *requestor)
00424 {
00425    struct ast_channel *c;
00426 
00427    if (!p->calls->new_ast_channel) {
00428       return NULL;
00429    }
00430 
00431    c = p->calls->new_ast_channel(p->chan_pvt, state, startpbx, sub, requestor);
00432    if (c) {
00433       ast_string_field_set(c, call_forward, p->call_forward);
00434    }
00435    p->subs[sub].owner = c;
00436    if (!p->owner) {
00437       analog_set_new_owner(p, c);
00438    }
00439    return c;
00440 }
00441 
00442 static int analog_set_echocanceller(struct analog_pvt *p, int enable)
00443 {
00444    if (p->calls->set_echocanceller) {
00445       return p->calls->set_echocanceller(p->chan_pvt, enable);
00446    }
00447    return -1;
00448 }
00449 
00450 static int analog_train_echocanceller(struct analog_pvt *p)
00451 {
00452    if (p->calls->train_echocanceller) {
00453       return p->calls->train_echocanceller(p->chan_pvt);
00454    }
00455    return -1;
00456 }
00457 
00458 static int analog_is_off_hook(struct analog_pvt *p)
00459 {
00460    if (p->calls->is_off_hook) {
00461       return p->calls->is_off_hook(p->chan_pvt);
00462    }
00463    return -1;
00464 }
00465 
00466 static int analog_ring(struct analog_pvt *p)
00467 {
00468    if (p->calls->ring) {
00469       return p->calls->ring(p->chan_pvt);
00470    }
00471    return -1;
00472 }
00473 
00474 static int analog_flash(struct analog_pvt *p)
00475 {
00476    if (p->calls->flash) {
00477       return p->calls->flash(p->chan_pvt);
00478    }
00479    return -1;
00480 }
00481 
00482 static int analog_start(struct analog_pvt *p)
00483 {
00484    if (p->calls->start) {
00485       return p->calls->start(p->chan_pvt);
00486    }
00487    return -1;
00488 }
00489 
00490 static int analog_dial_digits(struct analog_pvt *p, enum analog_sub sub, struct analog_dialoperation *dop)
00491 {
00492    if (p->calls->dial_digits) {
00493       return p->calls->dial_digits(p->chan_pvt, sub, dop);
00494    }
00495    return -1;
00496 }
00497 
00498 static int analog_on_hook(struct analog_pvt *p)
00499 {
00500    if (p->calls->on_hook) {
00501       return p->calls->on_hook(p->chan_pvt);
00502    }
00503    return -1;
00504 }
00505 
00506 static int analog_check_for_conference(struct analog_pvt *p)
00507 {
00508    if (p->calls->check_for_conference) {
00509       return p->calls->check_for_conference(p->chan_pvt);
00510    }
00511    return -1;
00512 }
00513 
00514 static void analog_all_subchannels_hungup(struct analog_pvt *p)
00515 {
00516    if (p->calls->all_subchannels_hungup) {
00517       p->calls->all_subchannels_hungup(p->chan_pvt);
00518    }
00519 }
00520 
00521 #if 0
00522 static void analog_unlock_private(struct analog_pvt *p)
00523 {
00524    if (p->calls->unlock_private) {
00525       p->calls->unlock_private(p->chan_pvt);
00526    }
00527 }
00528 #endif
00529 
00530 #if 0
00531 static void analog_lock_private(struct analog_pvt *p)
00532 {
00533    if (p->calls->lock_private) {
00534       p->calls->lock_private(p->chan_pvt);
00535    }
00536 }
00537 #endif
00538 
00539 /*!
00540  * \internal
00541  * \brief Obtain the specified subchannel owner lock if the owner exists.
00542  *
00543  * \param pvt Analog private struct.
00544  * \param sub_idx Subchannel owner to lock.
00545  *
00546  * \note Assumes the analog_lock_private(pvt->chan_pvt) is already obtained.
00547  *
00548  * \note
00549  * Because deadlock avoidance may have been necessary, you need to confirm
00550  * the state of things before continuing.
00551  *
00552  * \return Nothing
00553  */
00554 static void analog_lock_sub_owner(struct analog_pvt *pvt, enum analog_sub sub_idx)
00555 {
00556    for (;;) {
00557       if (!pvt->subs[sub_idx].owner) {
00558          /* No subchannel owner pointer */
00559          break;
00560       }
00561       if (!ast_channel_trylock(pvt->subs[sub_idx].owner)) {
00562          /* Got subchannel owner lock */
00563          break;
00564       }
00565       /* We must unlock the private to avoid the possibility of a deadlock */
00566       if (pvt->calls->deadlock_avoidance_private) {
00567          pvt->calls->deadlock_avoidance_private(pvt->chan_pvt);
00568       } else {
00569          /* Don't use 100% CPU if required callback not present. */
00570          usleep(1);
00571       }
00572    }
00573 }
00574 
00575 static int analog_off_hook(struct analog_pvt *p)
00576 {
00577    if (p->calls->off_hook) {
00578       return p->calls->off_hook(p->chan_pvt);
00579    }
00580    return -1;
00581 }
00582 
00583 static void analog_set_needringing(struct analog_pvt *p, int value)
00584 {
00585    if (p->calls->set_needringing) {
00586       return p->calls->set_needringing(p->chan_pvt, value);
00587    }
00588 }
00589 
00590 #if 0
00591 static void analog_set_polarity(struct analog_pvt *p, int value)
00592 {
00593    if (p->calls->set_polarity) {
00594       return p->calls->set_polarity(p->chan_pvt, value);
00595    }
00596 }
00597 #endif
00598 
00599 static void analog_start_polarityswitch(struct analog_pvt *p)
00600 {
00601    if (p->calls->start_polarityswitch) {
00602       return p->calls->start_polarityswitch(p->chan_pvt);
00603    }
00604 }
00605 static void analog_answer_polarityswitch(struct analog_pvt *p)
00606 {
00607    if (p->calls->answer_polarityswitch) {
00608       return p->calls->answer_polarityswitch(p->chan_pvt);
00609    }
00610 }
00611 
00612 static void analog_hangup_polarityswitch(struct analog_pvt *p)
00613 {
00614    if (p->calls->hangup_polarityswitch) {
00615       return p->calls->hangup_polarityswitch(p->chan_pvt);
00616    }
00617 }
00618 
00619 static int analog_dsp_set_digitmode(struct analog_pvt *p, enum analog_dsp_digitmode mode)
00620 {
00621    if (p->calls->dsp_set_digitmode) {
00622       return p->calls->dsp_set_digitmode(p->chan_pvt, mode);
00623    }
00624    return -1;
00625 }
00626 
00627 static void analog_cb_handle_dtmf(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub analog_index, struct ast_frame **dest)
00628 {
00629    if (p->calls->handle_dtmf) {
00630       p->calls->handle_dtmf(p->chan_pvt, ast, analog_index, dest);
00631    }
00632 }
00633 
00634 static int analog_wink(struct analog_pvt *p, enum analog_sub index)
00635 {
00636    if (p->calls->wink) {
00637       return p->calls->wink(p->chan_pvt, index);
00638    }
00639    return -1;
00640 }
00641 
00642 static int analog_has_voicemail(struct analog_pvt *p)
00643 {
00644    if (p->calls->has_voicemail) {
00645       return p->calls->has_voicemail(p->chan_pvt);
00646    }
00647    return -1;
00648 }
00649 
00650 static int analog_is_dialing(struct analog_pvt *p, enum analog_sub index)
00651 {
00652    if (p->calls->is_dialing) {
00653       return p->calls->is_dialing(p->chan_pvt, index);
00654    }
00655    return -1;
00656 }
00657 
00658 /*!
00659  * \internal
00660  * \brief Attempt to transfer 3-way call.
00661  *
00662  * \param p Analog private structure.
00663  * \param inthreeway TRUE if the 3-way call is conferenced.
00664  *
00665  * \note
00666  * On entry these locks are held: real-call, private, 3-way call.
00667  *
00668  * \retval 1 Transfer successful.  3-way call is unlocked and subchannel is unalloced.
00669  *         Swapped real and 3-way subchannel.
00670  * \retval 0 Transfer successful.  3-way call is unlocked and subchannel is unalloced.
00671  * \retval -1 on error.  Caller must unlock 3-way call.
00672  */
00673 static int analog_attempt_transfer(struct analog_pvt *p, int inthreeway)
00674 {
00675    struct ast_channel *owner_real;
00676    struct ast_channel *owner_3way;
00677    struct ast_channel *bridge_real;
00678    struct ast_channel *bridge_3way;
00679 
00680    owner_real = p->subs[ANALOG_SUB_REAL].owner;
00681    owner_3way = p->subs[ANALOG_SUB_THREEWAY].owner;
00682    bridge_real = ast_bridged_channel(owner_real);
00683    bridge_3way = ast_bridged_channel(owner_3way);
00684 
00685    /*
00686     * In order to transfer, we need at least one of the channels to
00687     * actually be in a call bridge.  We can't conference two
00688     * applications together.  Why would we want to?
00689     */
00690    if (bridge_3way) {
00691       ast_verb(3, "TRANSFERRING %s to %s\n", owner_3way->name, owner_real->name);
00692       ast_cel_report_event(owner_3way,
00693          (owner_real->_state == AST_STATE_RINGING
00694             || owner_3way->_state == AST_STATE_RINGING)
00695             ? AST_CEL_BLINDTRANSFER : AST_CEL_ATTENDEDTRANSFER,
00696          NULL, owner_3way->linkedid, NULL);
00697 
00698       /*
00699        * The three-way party we're about to transfer is on hold if he
00700        * is not in a three way conference.
00701        */
00702       if (ast_channel_transfer_masquerade(owner_real, &owner_real->connected, 0,
00703          bridge_3way, &owner_3way->connected, !inthreeway)) {
00704          ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
00705             bridge_3way->name, owner_real->name);
00706          return -1;
00707       }
00708 
00709       /* Three-way is now the REAL */
00710       analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
00711       ast_channel_unlock(owner_3way);
00712       analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
00713       /* Tell the caller not to hangup */
00714       return 1;
00715    } else if (bridge_real) {
00716       /* Try transferring the other way. */
00717       ast_verb(3, "TRANSFERRING %s to %s\n", owner_real->name, owner_3way->name);
00718       ast_cel_report_event(owner_3way,
00719          (owner_real->_state == AST_STATE_RINGING
00720             || owner_3way->_state == AST_STATE_RINGING)
00721             ? AST_CEL_BLINDTRANSFER : AST_CEL_ATTENDEDTRANSFER,
00722          NULL, owner_3way->linkedid, NULL);
00723 
00724       /*
00725        * The three-way party we're about to transfer is on hold if he
00726        * is not in a three way conference.
00727        */
00728       if (ast_channel_transfer_masquerade(owner_3way, &owner_3way->connected,
00729          !inthreeway, bridge_real, &owner_real->connected, 0)) {
00730          ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
00731             bridge_real->name, owner_3way->name);
00732          return -1;
00733       }
00734 
00735       /* Orphan the channel after releasing the lock */
00736       ast_channel_unlock(owner_3way);
00737       analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
00738       return 0;
00739    } else {
00740       ast_debug(1, "Neither %s nor %s are in a bridge, nothing to transfer\n",
00741          owner_real->name, owner_3way->name);
00742       return -1;
00743    }
00744 }
00745 
00746 static int analog_update_conf(struct analog_pvt *p)
00747 {
00748    int x;
00749    int needconf = 0;
00750 
00751    /* Start with the obvious, general stuff */
00752    for (x = 0; x < 3; x++) {
00753       /* Look for three way calls */
00754       if ((p->subs[x].allocd) && p->subs[x].inthreeway) {
00755          if (p->calls->conf_add) {
00756             p->calls->conf_add(p->chan_pvt, x);
00757          }
00758          needconf++;
00759       } else {
00760          if (p->calls->conf_del) {
00761             p->calls->conf_del(p->chan_pvt, x);
00762          }
00763       }
00764    }
00765    ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
00766 
00767    if (p->calls->complete_conference_update) {
00768       p->calls->complete_conference_update(p->chan_pvt, needconf);
00769    }
00770    return 0;
00771 }
00772 
00773 struct ast_channel * analog_request(struct analog_pvt *p, int *callwait, const struct ast_channel *requestor)
00774 {
00775    struct ast_channel *ast;
00776 
00777    ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
00778    *callwait = (p->owner != NULL);
00779 
00780    if (p->owner) {
00781       if (analog_alloc_sub(p, ANALOG_SUB_CALLWAIT)) {
00782          ast_log(LOG_ERROR, "Unable to alloc subchannel\n");
00783          return NULL;
00784       }
00785    }
00786 
00787    p->outgoing = 1;
00788    ast = analog_new_ast_channel(p, AST_STATE_RESERVED, 0,
00789       p->owner ? ANALOG_SUB_CALLWAIT : ANALOG_SUB_REAL, requestor);
00790    if (!ast) {
00791       p->outgoing = 0;
00792    }
00793    return ast;
00794 }
00795 
00796 int analog_available(struct analog_pvt *p)
00797 {
00798    int offhook;
00799 
00800    ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
00801 
00802    /* If do not disturb, definitely not */
00803    if (p->dnd) {
00804       return 0;
00805    }
00806    /* If guard time, definitely not */
00807    if (p->guardtime && (time(NULL) < p->guardtime)) {
00808       return 0;
00809    }
00810 
00811    /* If no owner definitely available */
00812    if (!p->owner) {
00813       offhook = analog_is_off_hook(p);
00814 
00815       /* TDM FXO card, "onhook" means out of service (no battery on the line) */
00816       if ((p->sig == ANALOG_SIG_FXSLS) || (p->sig == ANALOG_SIG_FXSKS) || (p->sig == ANALOG_SIG_FXSGS)) {
00817 #ifdef DAHDI_CHECK_HOOKSTATE
00818          if (offhook) {
00819             return 1;
00820          }
00821          return 0;
00822 #endif
00823       /* TDM FXS card, "offhook" means someone took the hook off so it's unavailable! */
00824       } else if (offhook) {
00825          ast_debug(1, "Channel %d off hook, can't use\n", p->channel);
00826          /* Not available when the other end is off hook */
00827          return 0;
00828       }
00829       return 1;
00830    }
00831 
00832    /* If it's not an FXO, forget about call wait */
00833    if ((p->sig != ANALOG_SIG_FXOKS) && (p->sig != ANALOG_SIG_FXOLS) && (p->sig != ANALOG_SIG_FXOGS)) {
00834       return 0;
00835    }
00836 
00837    if (!p->callwaiting) {
00838       /* If they don't have call waiting enabled, then for sure they're unavailable at this point */
00839       return 0;
00840    }
00841 
00842    if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
00843       /* If there is already a call waiting call, then we can't take a second one */
00844       return 0;
00845    }
00846 
00847    if ((p->owner->_state != AST_STATE_UP) &&
00848        ((p->owner->_state != AST_STATE_RINGING) || p->outgoing)) {
00849       /* If the current call is not up, then don't allow the call */
00850       return 0;
00851    }
00852    if ((p->subs[ANALOG_SUB_THREEWAY].owner) && (!p->subs[ANALOG_SUB_THREEWAY].inthreeway)) {
00853       /* Can't take a call wait when the three way calling hasn't been merged yet. */
00854       return 0;
00855    }
00856    /* We're cool */
00857    return 1;
00858 }
00859 
00860 static int analog_stop_callwait(struct analog_pvt *p)
00861 {
00862    p->callwaitcas = 0;
00863    if (p->calls->stop_callwait) {
00864       return p->calls->stop_callwait(p->chan_pvt);
00865    }
00866    return 0;
00867 }
00868 
00869 static int analog_callwait(struct analog_pvt *p)
00870 {
00871    p->callwaitcas = p->callwaitingcallerid;
00872    if (p->calls->callwait) {
00873       return p->calls->callwait(p->chan_pvt);
00874    }
00875    return 0;
00876 }
00877 
00878 static void analog_set_callwaiting(struct analog_pvt *p, int callwaiting_enable)
00879 {
00880    p->callwaiting = callwaiting_enable;
00881    if (p->calls->set_callwaiting) {
00882       p->calls->set_callwaiting(p->chan_pvt, callwaiting_enable);
00883    }
00884 }
00885 
00886 static void analog_set_cadence(struct analog_pvt *p, struct ast_channel *chan)
00887 {
00888    if (p->calls->set_cadence) {
00889       return p->calls->set_cadence(p->chan_pvt, &p->cidrings, chan);
00890    }
00891 }
00892 
00893 static void analog_set_dialing(struct analog_pvt *p, int is_dialing)
00894 {
00895    p->dialing = is_dialing;
00896    if (p->calls->set_dialing) {
00897       return p->calls->set_dialing(p->chan_pvt, is_dialing);
00898    }
00899 }
00900 
00901 static void analog_set_alarm(struct analog_pvt *p, int in_alarm)
00902 {
00903    p->inalarm = in_alarm;
00904    if (p->calls->set_alarm) {
00905       return p->calls->set_alarm(p->chan_pvt, in_alarm);
00906    }
00907 }
00908 
00909 static void analog_set_ringtimeout(struct analog_pvt *p, int ringt)
00910 {
00911    p->ringt = ringt;
00912    if (!p->calls->set_ringtimeout) {
00913       return;
00914    }
00915    p->calls->set_ringtimeout(p->chan_pvt, ringt);
00916 }
00917 
00918 static void analog_set_waitingfordt(struct analog_pvt *p, struct ast_channel *ast)
00919 {
00920    if (p->calls->set_waitingfordt) {
00921       return p->calls->set_waitingfordt(p->chan_pvt, ast);
00922    }
00923 }
00924 
00925 static int analog_check_waitingfordt(struct analog_pvt *p)
00926 {
00927    if (p->calls->check_waitingfordt) {
00928       return p->calls->check_waitingfordt(p->chan_pvt);
00929    }
00930 
00931    return 0;
00932 }
00933 
00934 static void analog_set_confirmanswer(struct analog_pvt *p, int flag)
00935 {
00936    if (!p->calls->set_confirmanswer) {
00937       return;
00938    }
00939    p->calls->set_confirmanswer(p->chan_pvt, flag);
00940 }
00941 
00942 static int analog_check_confirmanswer(struct analog_pvt *p)
00943 {
00944    if (p->calls->check_confirmanswer) {
00945       return p->calls->check_confirmanswer(p->chan_pvt);
00946    }
00947 
00948    return 0;
00949 }
00950 
00951 static void analog_cancel_cidspill(struct analog_pvt *p)
00952 {
00953    if (!p->calls->cancel_cidspill) {
00954       return;
00955    }
00956 
00957    p->calls->cancel_cidspill(p->chan_pvt);
00958 }
00959 
00960 static int analog_confmute(struct analog_pvt *p, int mute)
00961 {
00962    if (p->calls->confmute) {
00963       return p->calls->confmute(p->chan_pvt, mute);
00964    }
00965    return 0;
00966 }
00967 
00968 static void analog_set_pulsedial(struct analog_pvt *p, int flag)
00969 {
00970    if (!p->calls->set_pulsedial) {
00971       return;
00972    }
00973    p->calls->set_pulsedial(p->chan_pvt, flag);
00974 }
00975 
00976 static int analog_set_linear_mode(struct analog_pvt *p, enum analog_sub sub, int linear_mode)
00977 {
00978    if (p->calls->set_linear_mode) {
00979       /* Return provides old linear_mode setting or error indication */
00980       return p->calls->set_linear_mode(p->chan_pvt, sub, linear_mode);
00981    }
00982    return -1;
00983 }
00984 
00985 static void analog_set_inthreeway(struct analog_pvt *p, enum analog_sub sub, int inthreeway)
00986 {
00987    p->subs[sub].inthreeway = inthreeway;
00988    if (p->calls->set_inthreeway) {
00989       p->calls->set_inthreeway(p->chan_pvt, sub, inthreeway);
00990    }
00991 }
00992 
00993 int analog_call(struct analog_pvt *p, struct ast_channel *ast, char *rdest, int timeout)
00994 {
00995    int res, idx, mysig;
00996    char *c, *n, *l;
00997    char dest[256]; /* must be same length as p->dialdest */
00998 
00999    ast_debug(1, "CALLING CID_NAME: %s CID_NUM:: %s\n",
01000       S_COR(ast->connected.id.name.valid, ast->connected.id.name.str, ""),
01001       S_COR(ast->connected.id.number.valid, ast->connected.id.number.str, ""));
01002 
01003    ast_copy_string(dest, rdest, sizeof(dest));
01004    ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
01005 
01006    if ((ast->_state == AST_STATE_BUSY)) {
01007       ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_BUSY);
01008       return 0;
01009    }
01010 
01011    if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
01012       ast_log(LOG_WARNING, "analog_call called on %s, neither down nor reserved\n", ast->name);
01013       return -1;
01014    }
01015 
01016    p->dialednone = 0;
01017    p->outgoing = 1;
01018 
01019    mysig = p->sig;
01020    if (p->outsigmod > -1) {
01021       mysig = p->outsigmod;
01022    }
01023 
01024    switch (mysig) {
01025    case ANALOG_SIG_FXOLS:
01026    case ANALOG_SIG_FXOGS:
01027    case ANALOG_SIG_FXOKS:
01028       if (p->owner == ast) {
01029          /* Normal ring, on hook */
01030 
01031          /* Don't send audio while on hook, until the call is answered */
01032          analog_set_dialing(p, 1);
01033          analog_set_cadence(p, ast); /* and set p->cidrings */
01034 
01035          /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
01036          c = strchr(dest, '/');
01037          if (c) {
01038             c++;
01039          }
01040          if (c && (strlen(c) < p->stripmsd)) {
01041             ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01042             c = NULL;
01043          }
01044          if (c) {
01045             p->dop.op = ANALOG_DIAL_OP_REPLACE;
01046             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
01047             ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
01048          } else {
01049             p->dop.dialstr[0] = '\0';
01050          }
01051 
01052          if (analog_ring(p)) {
01053             ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
01054             return -1;
01055          }
01056          analog_set_dialing(p, 1);
01057       } else {
01058          /* Call waiting call */
01059          if (ast->connected.id.number.valid && ast->connected.id.number.str) {
01060             ast_copy_string(p->callwait_num, ast->connected.id.number.str, sizeof(p->callwait_num));
01061          } else {
01062             p->callwait_num[0] = '\0';
01063          }
01064          if (ast->connected.id.name.valid && ast->connected.id.name.str) {
01065             ast_copy_string(p->callwait_name, ast->connected.id.name.str, sizeof(p->callwait_name));
01066          } else {
01067             p->callwait_name[0] = '\0';
01068          }
01069 
01070          /* Call waiting tone instead */
01071          if (analog_callwait(p)) {
01072             return -1;
01073          }
01074          /* Make ring-back */
01075          if (analog_play_tone(p, ANALOG_SUB_CALLWAIT, ANALOG_TONE_RINGTONE)) {
01076             ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
01077          }
01078 
01079       }
01080       n = ast->connected.id.name.valid ? ast->connected.id.name.str : NULL;
01081       l = ast->connected.id.number.valid ? ast->connected.id.number.str : NULL;
01082       if (l) {
01083          ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
01084       } else {
01085          p->lastcid_num[0] = '\0';
01086       }
01087       if (n) {
01088          ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
01089       } else {
01090          p->lastcid_name[0] = '\0';
01091       }
01092 
01093       if (p->use_callerid) {
01094          p->caller.id.name.str = p->lastcid_name;
01095          p->caller.id.number.str = p->lastcid_num;
01096       }
01097 
01098       ast_setstate(ast, AST_STATE_RINGING);
01099       idx = analog_get_index(ast, p, 0);
01100       if (idx > -1) {
01101          struct ast_cc_config_params *cc_params;
01102 
01103          /* This is where the initial ringing frame is queued for an analog call.
01104           * As such, this is a great time to offer CCNR to the caller if it's available.
01105           */
01106          cc_params = ast_channel_get_cc_config_params(p->subs[idx].owner);
01107          if (cc_params) {
01108             switch (ast_get_cc_monitor_policy(cc_params)) {
01109             case AST_CC_MONITOR_NEVER:
01110                break;
01111             case AST_CC_MONITOR_NATIVE:
01112             case AST_CC_MONITOR_ALWAYS:
01113             case AST_CC_MONITOR_GENERIC:
01114                ast_queue_cc_frame(p->subs[idx].owner, AST_CC_GENERIC_MONITOR_TYPE,
01115                   analog_get_orig_dialstring(p), AST_CC_CCNR, NULL);
01116                break;
01117             }
01118          }
01119          ast_queue_control(p->subs[idx].owner, AST_CONTROL_RINGING);
01120       }
01121       break;
01122    case ANALOG_SIG_FXSLS:
01123    case ANALOG_SIG_FXSGS:
01124    case ANALOG_SIG_FXSKS:
01125       if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
01126          ast_debug(1, "Ignore possible polarity reversal on line seizure\n");
01127          p->polaritydelaytv = ast_tvnow();
01128       }
01129       /* fall through */
01130    case ANALOG_SIG_EMWINK:
01131    case ANALOG_SIG_EM:
01132    case ANALOG_SIG_EM_E1:
01133    case ANALOG_SIG_FEATD:
01134    case ANALOG_SIG_FEATDMF:
01135    case ANALOG_SIG_E911:
01136    case ANALOG_SIG_FGC_CAMA:
01137    case ANALOG_SIG_FGC_CAMAMF:
01138    case ANALOG_SIG_FEATB:
01139    case ANALOG_SIG_SFWINK:
01140    case ANALOG_SIG_SF:
01141    case ANALOG_SIG_SF_FEATD:
01142    case ANALOG_SIG_SF_FEATDMF:
01143    case ANALOG_SIG_FEATDMF_TA:
01144    case ANALOG_SIG_SF_FEATB:
01145       c = strchr(dest, '/');
01146       if (c) {
01147          c++;
01148       } else {
01149          c = "";
01150       }
01151       if (strlen(c) < p->stripmsd) {
01152          ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01153          return -1;
01154       }
01155       res = analog_start(p);
01156       if (res < 0) {
01157          if (errno != EINPROGRESS) {
01158             return -1;
01159          }
01160       }
01161       ast_debug(1, "Dialing '%s'\n", c);
01162       p->dop.op = ANALOG_DIAL_OP_REPLACE;
01163 
01164       c += p->stripmsd;
01165 
01166       switch (mysig) {
01167       case ANALOG_SIG_FEATD:
01168          l = ast->connected.id.number.valid ? ast->connected.id.number.str : NULL;
01169          if (l) {
01170             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
01171          } else {
01172             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
01173          }
01174          break;
01175       case ANALOG_SIG_FEATDMF:
01176          l = ast->connected.id.number.valid ? ast->connected.id.number.str : NULL;
01177          if (l) {
01178             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
01179          } else {
01180             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
01181          }
01182          break;
01183       case ANALOG_SIG_FEATDMF_TA:
01184       {
01185          const char *cic = "", *ozz = "";
01186 
01187          /* If you have to go through a Tandem Access point you need to use this */
01188 #ifndef STANDALONE
01189          ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
01190          if (!ozz) {
01191             ozz = analog_defaultozz;
01192          }
01193          cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
01194          if (!cic) {
01195             cic = analog_defaultcic;
01196          }
01197 #endif
01198          if (!ozz || !cic) {
01199             ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
01200             return -1;
01201          }
01202          snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
01203          snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
01204          p->whichwink = 0;
01205       }
01206          break;
01207       case ANALOG_SIG_E911:
01208          ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
01209          break;
01210       case ANALOG_SIG_FGC_CAMA:
01211          snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
01212          break;
01213       case ANALOG_SIG_FGC_CAMAMF:
01214       case ANALOG_SIG_FEATB:
01215          snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
01216          break;
01217       default:
01218          if (p->pulse) {
01219             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
01220          } else {
01221             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
01222          }
01223          break;
01224       }
01225 
01226       if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
01227          memset(p->echorest, 'w', sizeof(p->echorest) - 1);
01228          strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
01229          p->echorest[sizeof(p->echorest) - 1] = '\0';
01230          p->echobreak = 1;
01231          p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
01232       } else {
01233          p->echobreak = 0;
01234       }
01235       analog_set_waitingfordt(p, ast);
01236       if (!res) {
01237          if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
01238             int saveerr = errno;
01239 
01240             analog_on_hook(p);
01241             ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
01242             return -1;
01243          }
01244       } else {
01245          ast_debug(1, "Deferring dialing...\n");
01246       }
01247       analog_set_dialing(p, 1);
01248       if (ast_strlen_zero(c)) {
01249          p->dialednone = 1;
01250       }
01251       ast_setstate(ast, AST_STATE_DIALING);
01252       break;
01253    default:
01254       ast_debug(1, "not yet implemented\n");
01255       return -1;
01256    }
01257    return 0;
01258 }
01259 
01260 int analog_hangup(struct analog_pvt *p, struct ast_channel *ast)
01261 {
01262    int res;
01263    int idx, x;
01264 
01265    ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
01266    if (!ast->tech_pvt) {
01267       ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
01268       return 0;
01269    }
01270 
01271    idx = analog_get_index(ast, p, 1);
01272 
01273    x = 0;
01274    if (p->origcid_num) {
01275       ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
01276       ast_free(p->origcid_num);
01277       p->origcid_num = NULL;
01278    }
01279    if (p->origcid_name) {
01280       ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
01281       ast_free(p->origcid_name);
01282       p->origcid_name = NULL;
01283    }
01284 
01285    analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
01286 
01287    ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
01288       p->channel, idx, p->subs[ANALOG_SUB_REAL].allocd, p->subs[ANALOG_SUB_CALLWAIT].allocd, p->subs[ANALOG_SUB_THREEWAY].allocd);
01289    if (idx > -1) {
01290       /* Real channel, do some fixup */
01291       p->subs[idx].owner = NULL;
01292       p->polarity = POLARITY_IDLE;
01293       analog_set_linear_mode(p, idx, 0);
01294       switch (idx) {
01295       case ANALOG_SUB_REAL:
01296          if (p->subs[ANALOG_SUB_CALLWAIT].allocd && p->subs[ANALOG_SUB_THREEWAY].allocd) {
01297             ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
01298             if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
01299                /* We had flipped over to answer a callwait and now it's gone */
01300                ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
01301                /* Move to the call-wait, but un-own us until they flip back. */
01302                analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
01303                analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
01304                analog_set_new_owner(p, NULL);
01305             } else {
01306                /* The three way hung up, but we still have a call wait */
01307                ast_debug(1, "We were in the threeway and have a callwait still.  Ditching the threeway.\n");
01308                analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
01309                analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
01310                if (p->subs[ANALOG_SUB_REAL].inthreeway) {
01311                   /* This was part of a three way call.  Immediately make way for
01312                      another call */
01313                   ast_debug(1, "Call was complete, setting owner to former third call\n");
01314                   analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
01315                   analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
01316                } else {
01317                   /* This call hasn't been completed yet...  Set owner to NULL */
01318                   ast_debug(1, "Call was incomplete, setting owner to NULL\n");
01319                   analog_set_new_owner(p, NULL);
01320                }
01321             }
01322          } else if (p->subs[ANALOG_SUB_CALLWAIT].allocd) {
01323             /* Need to hold the lock for real-call, private, and call-waiting call */
01324             analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
01325             if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
01326                /* The call waiting call dissappeared. */
01327                analog_set_new_owner(p, NULL);
01328                break;
01329             }
01330 
01331             /* Move to the call-wait and switch back to them. */
01332             analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
01333             analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
01334             analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
01335             if (p->owner->_state != AST_STATE_UP) {
01336                ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_ANSWER);
01337             }
01338             if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner)) {
01339                ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
01340             }
01341             /* Unlock the call-waiting call that we swapped to real-call. */
01342             ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
01343          } else if (p->subs[ANALOG_SUB_THREEWAY].allocd) {
01344             analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
01345             analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
01346             if (p->subs[ANALOG_SUB_REAL].inthreeway) {
01347                /* This was part of a three way call.  Immediately make way for
01348                   another call */
01349                ast_debug(1, "Call was complete, setting owner to former third call\n");
01350                analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
01351                analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
01352             } else {
01353                /* This call hasn't been completed yet...  Set owner to NULL */
01354                ast_debug(1, "Call was incomplete, setting owner to NULL\n");
01355                analog_set_new_owner(p, NULL);
01356             }
01357          }
01358          break;
01359       case ANALOG_SUB_CALLWAIT:
01360          /* Ditch the holding callwait call, and immediately make it available */
01361          if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
01362             /* Need to hold the lock for call-waiting call, private, and 3-way call */
01363             analog_lock_sub_owner(p, ANALOG_SUB_THREEWAY);
01364 
01365             /* This is actually part of a three way, placed on hold.  Place the third part
01366                on music on hold now */
01367             if (p->subs[ANALOG_SUB_THREEWAY].owner && ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
01368                ast_queue_control_data(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CONTROL_HOLD,
01369                   S_OR(p->mohsuggest, NULL),
01370                   !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
01371             }
01372             analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 0);
01373             /* Make it the call wait now */
01374             analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
01375             analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
01376             if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
01377                /* Unlock the 3-way call that we swapped to call-waiting call. */
01378                ast_channel_unlock(p->subs[ANALOG_SUB_CALLWAIT].owner);
01379             }
01380          } else {
01381             analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
01382          }
01383          break;
01384       case ANALOG_SUB_THREEWAY:
01385          /* Need to hold the lock for 3-way call, private, and call-waiting call */
01386          analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
01387          if (p->subs[ANALOG_SUB_CALLWAIT].inthreeway) {
01388             /* The other party of the three way call is currently in a call-wait state.
01389                Start music on hold for them, and take the main guy out of the third call */
01390             analog_set_inthreeway(p, ANALOG_SUB_CALLWAIT, 0);
01391             if (p->subs[ANALOG_SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[ANALOG_SUB_CALLWAIT].owner)) {
01392                ast_queue_control_data(p->subs[ANALOG_SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
01393                   S_OR(p->mohsuggest, NULL),
01394                   !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
01395             }
01396          }
01397          if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
01398             ast_channel_unlock(p->subs[ANALOG_SUB_CALLWAIT].owner);
01399          }
01400          analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
01401          /* If this was part of a three way call index, let us make
01402             another three way call */
01403          analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
01404          break;
01405       default:
01406          /*
01407           * Should never happen.
01408           * This wasn't any sort of call, so how are we an index?
01409           */
01410          ast_log(LOG_ERROR, "Index found but not any type of call?\n");
01411          break;
01412       }
01413    }
01414 
01415    if (!p->subs[ANALOG_SUB_REAL].owner && !p->subs[ANALOG_SUB_CALLWAIT].owner && !p->subs[ANALOG_SUB_THREEWAY].owner) {
01416       analog_set_new_owner(p, NULL);
01417       analog_set_ringtimeout(p, 0);
01418       analog_set_confirmanswer(p, 0);
01419       analog_set_pulsedial(p, 0);
01420       p->outgoing = 0;
01421       p->onhooktime = time(NULL);
01422       p->cidrings = 1;
01423 
01424       /* Perform low level hangup if no owner left */
01425       res = analog_on_hook(p);
01426       if (res < 0) {
01427          ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
01428       }
01429       switch (p->sig) {
01430       case ANALOG_SIG_FXOGS:
01431       case ANALOG_SIG_FXOLS:
01432       case ANALOG_SIG_FXOKS:
01433          /* If they're off hook, try playing congestion */
01434          if (analog_is_off_hook(p)) {
01435             analog_hangup_polarityswitch(p);
01436             analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
01437          } else {
01438             analog_play_tone(p, ANALOG_SUB_REAL, -1);
01439          }
01440          break;
01441       case ANALOG_SIG_FXSGS:
01442       case ANALOG_SIG_FXSLS:
01443       case ANALOG_SIG_FXSKS:
01444          /* Make sure we're not made available for at least two seconds assuming
01445             we were actually used for an inbound or outbound call. */
01446          if (ast->_state != AST_STATE_RESERVED) {
01447             time(&p->guardtime);
01448             p->guardtime += 2;
01449          }
01450          break;
01451       default:
01452          analog_play_tone(p, ANALOG_SUB_REAL, -1);
01453          break;
01454       }
01455 
01456       analog_set_echocanceller(p, 0);
01457 
01458       x = 0;
01459       ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
01460       ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
01461       p->callwaitcas = 0;
01462       analog_set_callwaiting(p, p->permcallwaiting);
01463       p->hidecallerid = p->permhidecallerid;
01464       analog_set_dialing(p, 0);
01465       analog_update_conf(p);
01466       analog_all_subchannels_hungup(p);
01467    }
01468 
01469    analog_stop_callwait(p);
01470 
01471    ast_verb(3, "Hanging up on '%s'\n", ast->name);
01472 
01473    return 0;
01474 }
01475 
01476 int analog_answer(struct analog_pvt *p, struct ast_channel *ast)
01477 {
01478    int res = 0;
01479    int idx;
01480    int oldstate = ast->_state;
01481 
01482    ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
01483    ast_setstate(ast, AST_STATE_UP);
01484    idx = analog_get_index(ast, p, 1);
01485    if (idx < 0) {
01486       idx = ANALOG_SUB_REAL;
01487    }
01488    switch (p->sig) {
01489    case ANALOG_SIG_FXSLS:
01490    case ANALOG_SIG_FXSGS:
01491    case ANALOG_SIG_FXSKS:
01492       analog_set_ringtimeout(p, 0);
01493       /* Fall through */
01494    case ANALOG_SIG_EM:
01495    case ANALOG_SIG_EM_E1:
01496    case ANALOG_SIG_EMWINK:
01497    case ANALOG_SIG_FEATD:
01498    case ANALOG_SIG_FEATDMF:
01499    case ANALOG_SIG_FEATDMF_TA:
01500    case ANALOG_SIG_E911:
01501    case ANALOG_SIG_FGC_CAMA:
01502    case ANALOG_SIG_FGC_CAMAMF:
01503    case ANALOG_SIG_FEATB:
01504    case ANALOG_SIG_SF:
01505    case ANALOG_SIG_SFWINK:
01506    case ANALOG_SIG_SF_FEATD:
01507    case ANALOG_SIG_SF_FEATDMF:
01508    case ANALOG_SIG_SF_FEATB:
01509    case ANALOG_SIG_FXOLS:
01510    case ANALOG_SIG_FXOGS:
01511    case ANALOG_SIG_FXOKS:
01512       /* Pick up the line */
01513       ast_debug(1, "Took %s off hook\n", ast->name);
01514       if (p->hanguponpolarityswitch) {
01515          gettimeofday(&p->polaritydelaytv, NULL);
01516       }
01517       res = analog_off_hook(p);
01518       analog_play_tone(p, idx, -1);
01519       analog_set_dialing(p, 0);
01520       if ((idx == ANALOG_SUB_REAL) && p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
01521          if (oldstate == AST_STATE_RINGING) {
01522             ast_debug(1, "Finally swapping real and threeway\n");
01523             analog_play_tone(p, ANALOG_SUB_THREEWAY, -1);
01524             analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
01525             analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
01526          }
01527       }
01528 
01529       switch (p->sig) {
01530       case ANALOG_SIG_FXSLS:
01531       case ANALOG_SIG_FXSKS:
01532       case ANALOG_SIG_FXSGS:
01533          analog_set_echocanceller(p, 1);
01534          analog_train_echocanceller(p);
01535          break;
01536       case ANALOG_SIG_FXOLS:
01537       case ANALOG_SIG_FXOKS:
01538       case ANALOG_SIG_FXOGS:
01539          analog_answer_polarityswitch(p);
01540          break;
01541       default:
01542          break;
01543       }
01544       break;
01545    default:
01546       ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
01547       res = -1;
01548       break;
01549    }
01550    ast_setstate(ast, AST_STATE_UP);
01551    return res;
01552 }
01553 
01554 static int analog_handles_digit(struct ast_frame *f)
01555 {
01556    char subclass = toupper(f->subclass.integer);
01557 
01558    switch (subclass) {
01559    case '1':
01560    case '2':
01561    case '3':
01562    case '4':
01563    case '5':
01564    case '6':
01565    case '7':
01566    case '9':
01567    case 'A':
01568    case 'B':
01569    case 'C':
01570    case 'D':
01571    case 'E':
01572    case 'F':
01573       return 1;
01574    default:
01575       return 0;
01576    }
01577 }
01578 
01579 void analog_handle_dtmf(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub idx, struct ast_frame **dest)
01580 {
01581    struct ast_frame *f = *dest;
01582 
01583    ast_debug(1, "%s DTMF digit: 0x%02X '%c' on %s\n",
01584       f->frametype == AST_FRAME_DTMF_BEGIN ? "Begin" : "End",
01585       f->subclass.integer, f->subclass.integer, ast->name);
01586 
01587    if (analog_check_confirmanswer(p)) {
01588       if (f->frametype == AST_FRAME_DTMF_END) {
01589          ast_debug(1, "Confirm answer on %s!\n", ast->name);
01590          /* Upon receiving a DTMF digit, consider this an answer confirmation instead
01591          of a DTMF digit */
01592          p->subs[idx].f.frametype = AST_FRAME_CONTROL;
01593          p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
01594          /* Reset confirmanswer so DTMF's will behave properly for the duration of the call */
01595          analog_set_confirmanswer(p, 0);
01596       } else {
01597          p->subs[idx].f.frametype = AST_FRAME_NULL;
01598          p->subs[idx].f.subclass.integer = 0;
01599       }
01600       *dest = &p->subs[idx].f;
01601    } else if (p->callwaitcas) {
01602       if (f->frametype == AST_FRAME_DTMF_END) {
01603          if ((f->subclass.integer == 'A') || (f->subclass.integer == 'D')) {
01604             ast_debug(1, "Got some DTMF, but it's for the CAS\n");
01605             p->caller.id.name.str = p->callwait_name;
01606             p->caller.id.number.str = p->callwait_num;
01607             analog_send_callerid(p, 1, &p->caller);
01608          }
01609          if (analog_handles_digit(f)) {
01610             p->callwaitcas = 0;
01611          }
01612       }
01613       p->subs[idx].f.frametype = AST_FRAME_NULL;
01614       p->subs[idx].f.subclass.integer = 0;
01615       *dest = &p->subs[idx].f;
01616    } else {
01617       analog_cb_handle_dtmf(p, ast, idx, dest);
01618    }
01619 }
01620 
01621 static int analog_my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
01622 {
01623    char c;
01624 
01625    *str = 0; /* start with empty output buffer */
01626    for (;;) {
01627       /* Wait for the first digit (up to specified ms). */
01628       c = ast_waitfordigit(chan, ms);
01629       /* if timeout, hangup or error, return as such */
01630       if (c < 1) {
01631          return c;
01632       }
01633       *str++ = c;
01634       *str = 0;
01635       if (strchr(term, c)) {
01636          return 1;
01637       }
01638    }
01639 }
01640 
01641 static int analog_handle_notify_message(struct ast_channel *chan, struct analog_pvt *p, int cid_flags, int neon_mwievent)
01642 {
01643    if (p->calls->handle_notify_message) {
01644       p->calls->handle_notify_message(chan, p->chan_pvt, cid_flags, neon_mwievent);
01645       return 0;
01646    }
01647    return -1;
01648 }
01649 
01650 static int analog_increase_ss_count(struct analog_pvt *p)
01651 {
01652    if (p->calls->increase_ss_count) {
01653       p->calls->increase_ss_count();
01654       return 0;
01655    }
01656    return -1;
01657 }
01658 
01659 static int analog_decrease_ss_count(struct analog_pvt *p)
01660 {
01661    if (p->calls->decrease_ss_count) {
01662       p->calls->decrease_ss_count();
01663       return 0;
01664    }
01665    return -1;
01666 }
01667 
01668 static int analog_distinctive_ring(struct ast_channel *chan, struct analog_pvt *p, int idx, int *ringdata)
01669 {
01670    if (p->calls->distinctive_ring) {
01671       return p->calls->distinctive_ring(chan, p->chan_pvt, idx, ringdata);
01672    }
01673    return -1;
01674 
01675 }
01676 
01677 static void analog_get_and_handle_alarms(struct analog_pvt *p)
01678 {
01679    if (p->calls->get_and_handle_alarms) {
01680       return p->calls->get_and_handle_alarms(p->chan_pvt);
01681    }
01682 }
01683 
01684 static void *analog_get_bridged_channel(struct analog_pvt *p, struct ast_channel *chan)
01685 {
01686    if (p->calls->get_sigpvt_bridged_channel) {
01687       return p->calls->get_sigpvt_bridged_channel;
01688    }
01689    return NULL;
01690 }
01691 
01692 static int analog_get_sub_fd(struct analog_pvt *p, enum analog_sub sub)
01693 {
01694    if (p->calls->get_sub_fd) {
01695       return p->calls->get_sub_fd(p->chan_pvt, sub);
01696    }
01697    return -1;
01698 }
01699 
01700 #define ANALOG_NEED_MFDETECT(p) (((p)->sig == ANALOG_SIG_FEATDMF) || ((p)->sig == ANALOG_SIG_FEATDMF_TA) || ((p)->sig == ANALOG_SIG_E911) || ((p)->sig == ANALOG_SIG_FGC_CAMA) || ((p)->sig == ANALOG_SIG_FGC_CAMAMF) || ((p)->sig == ANALOG_SIG_FEATB))
01701 
01702 static int analog_canmatch_featurecode(const char *exten)
01703 {
01704    int extlen = strlen(exten);
01705    const char *pickup_ext;
01706    if (!extlen) {
01707       return 1;
01708    }
01709    pickup_ext = ast_pickup_ext();
01710    if (extlen < strlen(pickup_ext) && !strncmp(pickup_ext, exten, extlen)) {
01711       return 1;
01712    }
01713    /* hardcoded features are *60, *67, *69, *70, *72, *73, *78, *79, *82, *0 */
01714    if (exten[0] == '*' && extlen < 3) {
01715       if (extlen == 1) {
01716          return 1;
01717       }
01718       /* "*0" should be processed before it gets here */
01719       switch (exten[1]) {
01720       case '6':
01721       case '7':
01722       case '8':
01723          return 1;
01724       }
01725    }
01726    return 0;
01727 }
01728 
01729 static void *__analog_ss_thread(void *data)
01730 {
01731    struct analog_pvt *p = data;
01732    struct ast_channel *chan = p->ss_astchan;
01733    char exten[AST_MAX_EXTENSION] = "";
01734    char exten2[AST_MAX_EXTENSION] = "";
01735    char dtmfcid[300];
01736    char dtmfbuf[300];
01737    char namebuf[ANALOG_MAX_CID];
01738    char numbuf[ANALOG_MAX_CID];
01739    struct callerid_state *cs = NULL;
01740    char *name = NULL, *number = NULL;
01741    int flags = 0;
01742    struct ast_smdi_md_message *smdi_msg = NULL;
01743    int timeout;
01744    int getforward = 0;
01745    char *s1, *s2;
01746    int len = 0;
01747    int res;
01748    int idx;
01749 
01750    analog_increase_ss_count(p);
01751 
01752    ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
01753 
01754    if (!chan) {
01755       /* What happened to the channel? */
01756       goto quit;
01757    }
01758    /* in the bizarre case where the channel has become a zombie before we
01759       even get started here, abort safely
01760    */
01761    if (!chan->tech_pvt) {
01762       ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
01763       ast_hangup(chan);
01764       goto quit;
01765    }
01766 
01767    ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
01768    idx = analog_get_index(chan, p, 0);
01769    if (idx < 0) {
01770       ast_hangup(chan);
01771       goto quit;
01772    }
01773    analog_dsp_reset_and_flush_digits(p);
01774    switch (p->sig) {
01775    case ANALOG_SIG_FEATD:
01776    case ANALOG_SIG_FEATDMF:
01777    case ANALOG_SIG_FEATDMF_TA:
01778    case ANALOG_SIG_E911:
01779    case ANALOG_SIG_FGC_CAMAMF:
01780    case ANALOG_SIG_FEATB:
01781    case ANALOG_SIG_EMWINK:
01782    case ANALOG_SIG_SF_FEATD:
01783    case ANALOG_SIG_SF_FEATDMF:
01784    case ANALOG_SIG_SF_FEATB:
01785    case ANALOG_SIG_SFWINK:
01786       if (analog_wink(p, idx))
01787          goto quit;
01788       /* Fall through */
01789    case ANALOG_SIG_EM:
01790    case ANALOG_SIG_EM_E1:
01791    case ANALOG_SIG_SF:
01792    case ANALOG_SIG_FGC_CAMA:
01793       res = analog_play_tone(p, idx, -1);
01794 
01795       analog_dsp_reset_and_flush_digits(p);
01796 
01797       /* set digit mode appropriately */
01798       if (ANALOG_NEED_MFDETECT(p)) {
01799          analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
01800       } else {
01801          analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
01802       }
01803 
01804       memset(dtmfbuf, 0, sizeof(dtmfbuf));
01805       /* Wait for the first digit only if immediate=no */
01806       if (!p->immediate) {
01807          /* Wait for the first digit (up to 5 seconds). */
01808          res = ast_waitfordigit(chan, 5000);
01809       } else {
01810          res = 0;
01811       }
01812       if (res > 0) {
01813          /* save first char */
01814          dtmfbuf[0] = res;
01815          switch (p->sig) {
01816          case ANALOG_SIG_FEATD:
01817          case ANALOG_SIG_SF_FEATD:
01818             res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
01819             if (res > 0) {
01820                res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
01821             }
01822             if (res < 1) {
01823                analog_dsp_reset_and_flush_digits(p);
01824             }
01825             break;
01826          case ANALOG_SIG_FEATDMF_TA:
01827             res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
01828             if (res < 1) {
01829                analog_dsp_reset_and_flush_digits(p);
01830             }
01831             if (analog_wink(p, idx)) {
01832                goto quit;
01833             }
01834             dtmfbuf[0] = 0;
01835             /* Wait for the first digit (up to 5 seconds). */
01836             res = ast_waitfordigit(chan, 5000);
01837             if (res <= 0) {
01838                break;
01839             }
01840             dtmfbuf[0] = res;
01841             /* fall through intentionally */
01842          case ANALOG_SIG_FEATDMF:
01843          case ANALOG_SIG_E911:
01844          case ANALOG_SIG_FGC_CAMAMF:
01845          case ANALOG_SIG_SF_FEATDMF:
01846             res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
01847             /* if international caca, do it again to get real ANO */
01848             if ((p->sig == ANALOG_SIG_FEATDMF) && (dtmfbuf[1] != '0') 
01849                && (strlen(dtmfbuf) != 14)) {
01850                if (analog_wink(p, idx)) {
01851                   goto quit;
01852                }
01853                dtmfbuf[0] = 0;
01854                /* Wait for the first digit (up to 5 seconds). */
01855                res = ast_waitfordigit(chan, 5000);
01856                if (res <= 0) {
01857                   break;
01858                }
01859                dtmfbuf[0] = res;
01860                res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
01861             }
01862             if (res > 0) {
01863                /* if E911, take off hook */
01864                if (p->sig == ANALOG_SIG_E911) {
01865                   analog_off_hook(p);
01866                }
01867                res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
01868             }
01869             if (res < 1) {
01870                analog_dsp_reset_and_flush_digits(p);
01871             }
01872             break;
01873          case ANALOG_SIG_FEATB:
01874          case ANALOG_SIG_SF_FEATB:
01875             res = analog_my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
01876             if (res < 1) {
01877                analog_dsp_reset_and_flush_digits(p);
01878             }
01879             break;
01880          case ANALOG_SIG_EMWINK:
01881             /* if we received a '*', we are actually receiving Feature Group D
01882                dial syntax, so use that mode; otherwise, fall through to normal
01883                mode
01884             */
01885             if (res == '*') {
01886                res = analog_my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
01887                if (res > 0) {
01888                   res = analog_my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
01889                }
01890                if (res < 1) {
01891                   analog_dsp_reset_and_flush_digits(p);
01892                }
01893                break;
01894             }
01895          default:
01896             /* If we got the first digit, get the rest */
01897             len = 1;
01898             dtmfbuf[len] = '\0';
01899             while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
01900                if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
01901                   timeout = analog_matchdigittimeout;
01902                } else {
01903                   timeout = analog_gendigittimeout;
01904                }
01905                res = ast_waitfordigit(chan, timeout);
01906                if (res < 0) {
01907                   ast_debug(1, "waitfordigit returned < 0...\n");
01908                   ast_hangup(chan);
01909                   goto quit;
01910                } else if (res) {
01911                   dtmfbuf[len++] = res;
01912                   dtmfbuf[len] = '\0';
01913                } else {
01914                   break;
01915                }
01916             }
01917             break;
01918          }
01919       }
01920       if (res == -1) {
01921          ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
01922          ast_hangup(chan);
01923          goto quit;
01924       } else if (res < 0) {
01925          ast_debug(1, "Got hung up before digits finished\n");
01926          ast_hangup(chan);
01927          goto quit;
01928       }
01929 
01930       if (p->sig == ANALOG_SIG_FGC_CAMA) {
01931          char anibuf[100];
01932 
01933          if (ast_safe_sleep(chan,1000) == -1) {
01934             ast_hangup(chan);
01935             goto quit;
01936          }
01937          analog_off_hook(p);
01938          analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_MF);
01939          res = analog_my_getsigstr(chan, anibuf, "#", 10000);
01940          if ((res > 0) && (strlen(anibuf) > 2)) {
01941             if (anibuf[strlen(anibuf) - 1] == '#') {
01942                anibuf[strlen(anibuf) - 1] = 0;
01943             }
01944             ast_set_callerid(chan, anibuf + 2, NULL, anibuf + 2);
01945          }
01946          analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
01947       }
01948 
01949       ast_copy_string(exten, dtmfbuf, sizeof(exten));
01950       if (ast_strlen_zero(exten)) {
01951          ast_copy_string(exten, "s", sizeof(exten));
01952       }
01953       if (p->sig == ANALOG_SIG_FEATD || p->sig == ANALOG_SIG_EMWINK) {
01954          /* Look for Feature Group D on all E&M Wink and Feature Group D trunks */
01955          if (exten[0] == '*') {
01956             char *stringp=NULL;
01957             ast_copy_string(exten2, exten, sizeof(exten2));
01958             /* Parse out extension and callerid */
01959             stringp=exten2 +1;
01960             s1 = strsep(&stringp, "*");
01961             s2 = strsep(&stringp, "*");
01962             if (s2) {
01963                if (!ast_strlen_zero(p->cid_num)) {
01964                   ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
01965                } else {
01966                   ast_set_callerid(chan, s1, NULL, s1);
01967                }
01968                ast_copy_string(exten, s2, sizeof(exten));
01969             } else {
01970                ast_copy_string(exten, s1, sizeof(exten));
01971             }
01972          } else if (p->sig == ANALOG_SIG_FEATD) {
01973             ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d.  Assuming E&M Wink instead\n", p->channel);
01974          }
01975       }
01976       if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
01977          if (exten[0] == '*') {
01978             char *stringp=NULL;
01979             ast_copy_string(exten2, exten, sizeof(exten2));
01980             /* Parse out extension and callerid */
01981             stringp=exten2 +1;
01982             s1 = strsep(&stringp, "#");
01983             s2 = strsep(&stringp, "#");
01984             if (s2) {
01985                if (!ast_strlen_zero(p->cid_num)) {
01986                   ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
01987                } else {
01988                   if (*(s1 + 2)) {
01989                      ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
01990                   }
01991                }
01992                ast_copy_string(exten, s2 + 1, sizeof(exten));
01993             } else {
01994                ast_copy_string(exten, s1 + 2, sizeof(exten));
01995             }
01996          } else {
01997             ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d.  Assuming E&M Wink instead\n", p->channel);
01998          }
01999       }
02000       if ((p->sig == ANALOG_SIG_E911) || (p->sig == ANALOG_SIG_FGC_CAMAMF)) {
02001          if (exten[0] == '*') {
02002             char *stringp=NULL;
02003             ast_copy_string(exten2, exten, sizeof(exten2));
02004             /* Parse out extension and callerid */
02005             stringp=exten2 +1;
02006             s1 = strsep(&stringp, "#");
02007             s2 = strsep(&stringp, "#");
02008             if (s2 && (*(s2 + 1) == '0')) {
02009                if (*(s2 + 2)) {
02010                   ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
02011                }
02012             }
02013             if (s1) {
02014                ast_copy_string(exten, s1, sizeof(exten));
02015             } else {
02016                ast_copy_string(exten, "911", sizeof(exten));
02017             }
02018          } else {
02019             ast_log(LOG_WARNING, "Got a non-E911/FGC CAMA input on channel %d.  Assuming E&M Wink instead\n", p->channel);
02020          }
02021       }
02022       if (p->sig == ANALOG_SIG_FEATB) {
02023          if (exten[0] == '*') {
02024             char *stringp=NULL;
02025             ast_copy_string(exten2, exten, sizeof(exten2));
02026             /* Parse out extension and callerid */
02027             stringp=exten2 +1;
02028             s1 = strsep(&stringp, "#");
02029             ast_copy_string(exten, exten2 + 1, sizeof(exten));
02030          } else {
02031             ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d.  Assuming E&M Wink instead\n", p->channel);
02032          }
02033       }
02034       if ((p->sig == ANALOG_SIG_FEATDMF) || (p->sig == ANALOG_SIG_FEATDMF_TA)) {
02035          analog_wink(p, idx);
02036          /*
02037           * Some switches require a minimum guard time between the last
02038           * FGD wink and something that answers immediately.  This
02039           * ensures it.
02040           */
02041          if (ast_safe_sleep(chan, 100)) {
02042             ast_hangup(chan);
02043             goto quit;
02044          }
02045       }
02046       analog_set_echocanceller(p, 1);
02047 
02048       analog_dsp_set_digitmode(p, ANALOG_DIGITMODE_DTMF);
02049 
02050       if (ast_exists_extension(chan, chan->context, exten, 1,
02051          chan->caller.id.number.valid ? chan->caller.id.number.str : NULL)) {
02052          ast_copy_string(chan->exten, exten, sizeof(chan->exten));
02053          analog_dsp_reset_and_flush_digits(p);
02054          res = ast_pbx_run(chan);
02055          if (res) {
02056             ast_log(LOG_WARNING, "PBX exited non-zero\n");
02057             res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
02058          }
02059          goto quit;
02060       } else {
02061          ast_verb(3, "Unknown extension '%s' in context '%s' requested\n", exten, chan->context);
02062          sleep(2);
02063          res = analog_play_tone(p, idx, ANALOG_TONE_INFO);
02064          if (res < 0) {
02065             ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
02066          } else {
02067             sleep(1);
02068          }
02069          res = ast_streamfile(chan, "ss-noservice", chan->language);
02070          if (res >= 0) {
02071             ast_waitstream(chan, "");
02072          }
02073          res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
02074          ast_hangup(chan);
02075          goto quit;
02076       }
02077       break;
02078    case ANALOG_SIG_FXOLS:
02079    case ANALOG_SIG_FXOGS:
02080    case ANALOG_SIG_FXOKS:
02081       /* Read the first digit */
02082       timeout = analog_firstdigittimeout;
02083       /* If starting a threeway call, never timeout on the first digit so someone
02084          can use flash-hook as a "hold" feature */
02085       if (p->subs[ANALOG_SUB_THREEWAY].owner) {
02086          timeout = 999999;
02087       }
02088       while (len < AST_MAX_EXTENSION-1) {
02089          /* Read digit unless it's supposed to be immediate, in which case the
02090             only answer is 's' */
02091          if (p->immediate) {
02092             res = 's';
02093          } else {
02094             res = ast_waitfordigit(chan, timeout);
02095          }
02096          timeout = 0;
02097          if (res < 0) {
02098             ast_debug(1, "waitfordigit returned < 0...\n");
02099             res = analog_play_tone(p, idx, -1);
02100             ast_hangup(chan);
02101             goto quit;
02102          } else if (res) {
02103             ast_debug(1,"waitfordigit returned '%c' (%d), timeout = %d\n", res, res, timeout);
02104             exten[len++]=res;
02105             exten[len] = '\0';
02106          }
02107          if (!ast_ignore_pattern(chan->context, exten)) {
02108             analog_play_tone(p, idx, -1);
02109          } else {
02110             analog_play_tone(p, idx, ANALOG_TONE_DIALTONE);
02111          }
02112          if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && !ast_parking_ext_valid(exten, chan, chan->context)) {
02113             if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
02114                if (getforward) {
02115                   /* Record this as the forwarding extension */
02116                   ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
02117                   ast_verb(3, "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
02118                   res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
02119                   if (res) {
02120                      break;
02121                   }
02122                   usleep(500000);
02123                   res = analog_play_tone(p, idx, -1);
02124                   sleep(1);
02125                   memset(exten, 0, sizeof(exten));
02126                   res = analog_play_tone(p, idx, ANALOG_TONE_DIALTONE);
02127                   len = 0;
02128                   getforward = 0;
02129                } else {
02130                   res = analog_play_tone(p, idx, -1);
02131                   ast_copy_string(chan->exten, exten, sizeof(chan->exten));
02132                   if (!ast_strlen_zero(p->cid_num)) {
02133                      if (!p->hidecallerid) {
02134                         ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
02135                      } else {
02136                         ast_set_callerid(chan, NULL, NULL, p->cid_num);
02137                      }
02138                   }
02139                   if (!ast_strlen_zero(p->cid_name)) {
02140                      if (!p->hidecallerid) {
02141                         ast_set_callerid(chan, NULL, p->cid_name, NULL);
02142                      }
02143                   }
02144                   ast_setstate(chan, AST_STATE_RING);
02145                   analog_set_echocanceller(p, 1);
02146                   res = ast_pbx_run(chan);
02147                   if (res) {
02148                      ast_log(LOG_WARNING, "PBX exited non-zero\n");
02149                      res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
02150                   }
02151                   goto quit;
02152                }
02153             } else {
02154                /* It's a match, but they just typed a digit, and there is an ambiguous match,
02155                   so just set the timeout to analog_matchdigittimeout and wait some more */
02156                timeout = analog_matchdigittimeout;
02157             }
02158          } else if (res == 0) {
02159             ast_debug(1, "not enough digits (and no ambiguous match)...\n");
02160             res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
02161             analog_wait_event(p);
02162             ast_hangup(chan);
02163             goto quit;
02164          } else if (p->callwaiting && !strcmp(exten, "*70")) {
02165             ast_verb(3, "Disabling call waiting on %s\n", chan->name);
02166             /* Disable call waiting if enabled */
02167             analog_set_callwaiting(p, 0);
02168             res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
02169             if (res) {
02170                ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
02171                   chan->name, strerror(errno));
02172             }
02173             len = 0;
02174             memset(exten, 0, sizeof(exten));
02175             timeout = analog_firstdigittimeout;
02176 
02177          } else if (!strcmp(exten,ast_pickup_ext())) {
02178             /* Scan all channels and see if there are any
02179              * ringing channels that have call groups
02180              * that equal this channels pickup group
02181              */
02182             if (idx == ANALOG_SUB_REAL) {
02183                /* Switch us from Third call to Call Wait */
02184                if (p->subs[ANALOG_SUB_THREEWAY].owner) {
02185                   /* If you make a threeway call and the *8# a call, it should actually
02186                      look like a callwait */
02187                   analog_alloc_sub(p, ANALOG_SUB_CALLWAIT);
02188                   analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_THREEWAY);
02189                   analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
02190                }
02191                analog_set_echocanceller(p, 1);
02192                if (ast_pickup_call(chan)) {
02193                   ast_debug(1, "No call pickup possible...\n");
02194                   res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
02195                   analog_wait_event(p);
02196                }
02197                ast_hangup(chan);
02198                goto quit;
02199             } else {
02200                ast_log(LOG_WARNING, "Huh?  Got *8# on call not on real\n");
02201                ast_hangup(chan);
02202                goto quit;
02203             }
02204 
02205          } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
02206             ast_verb(3, "Disabling Caller*ID on %s\n", chan->name);
02207             /* Disable Caller*ID if enabled */
02208             p->hidecallerid = 1;
02209             ast_party_number_free(&chan->caller.id.number);
02210             ast_party_number_init(&chan->caller.id.number);
02211             ast_party_name_free(&chan->caller.id.name);
02212             ast_party_name_init(&chan->caller.id.name);
02213             res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
02214             if (res) {
02215                ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
02216                   chan->name, strerror(errno));
02217             }
02218             len = 0;
02219             memset(exten, 0, sizeof(exten));
02220             timeout = analog_firstdigittimeout;
02221          } else if (p->callreturn && !strcmp(exten, "*69")) {
02222             res = 0;
02223             if (!ast_strlen_zero(p->lastcid_num)) {
02224                res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language);
02225             }
02226             if (!res) {
02227                res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
02228             }
02229             break;
02230          } else if (!strcmp(exten, "*78")) {
02231             /* Do not disturb enabled */
02232             analog_dnd(p, 1);
02233             res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
02234             getforward = 0;
02235             memset(exten, 0, sizeof(exten));
02236             len = 0;
02237          } else if (!strcmp(exten, "*79")) {
02238             /* Do not disturb disabled */
02239             analog_dnd(p, 0);
02240             res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
02241             getforward = 0;
02242             memset(exten, 0, sizeof(exten));
02243             len = 0;
02244          } else if (p->cancallforward && !strcmp(exten, "*72")) {
02245             res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
02246             getforward = 1;
02247             memset(exten, 0, sizeof(exten));
02248             len = 0;
02249          } else if (p->cancallforward && !strcmp(exten, "*73")) {
02250             ast_verb(3, "Cancelling call forwarding on channel %d\n", p->channel);
02251             res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
02252             memset(p->call_forward, 0, sizeof(p->call_forward));
02253             getforward = 0;
02254             memset(exten, 0, sizeof(exten));
02255             len = 0;
02256          } else if ((p->transfer || p->canpark) && ast_parking_ext_valid(exten, chan, chan->context) &&
02257                   p->subs[ANALOG_SUB_THREEWAY].owner &&
02258                   ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
02259             /* This is a three way call, the main call being a real channel,
02260                and we're parking the first call. */
02261             ast_masq_park_call(ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner), chan, 0, NULL);
02262             ast_verb(3, "Parking call to '%s'\n", chan->name);
02263             break;
02264          } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
02265             ast_verb(3, "Blacklisting number %s\n", p->lastcid_num);
02266             res = ast_db_put("blacklist", p->lastcid_num, "1");
02267             if (!res) {
02268                res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
02269                memset(exten, 0, sizeof(exten));
02270                len = 0;
02271             }
02272          } else if (p->hidecallerid && !strcmp(exten, "*82")) {
02273             ast_verb(3, "Enabling Caller*ID on %s\n", chan->name);
02274             /* Enable Caller*ID if enabled */
02275             p->hidecallerid = 0;
02276             ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
02277             res = analog_play_tone(p, idx, ANALOG_TONE_DIALRECALL);
02278             if (res) {
02279                ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
02280                   chan->name, strerror(errno));
02281             }
02282             len = 0;
02283             memset(exten, 0, sizeof(exten));
02284             timeout = analog_firstdigittimeout;
02285          } else if (!strcmp(exten, "*0")) {
02286             struct ast_channel *nbridge = p->subs[ANALOG_SUB_THREEWAY].owner;
02287             struct analog_pvt *pbridge = NULL;
02288             /* set up the private struct of the bridged one, if any */
02289             if (nbridge && ast_bridged_channel(nbridge)) {
02290                pbridge = analog_get_bridged_channel(p, nbridge);
02291             }
02292             if (nbridge && pbridge &&
02293                 (nbridge->tech == p->chan_tech) &&
02294                 (ast_bridged_channel(nbridge)->tech == p->chan_tech) &&
02295                 ISTRUNK(pbridge)) {
02296                /* Clear out the dial buffer */
02297                p->dop.dialstr[0] = '\0';
02298                /* flash hookswitch */
02299                if ((analog_flash(p) == -1) && (errno != EINPROGRESS)) {
02300                   ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
02301                      nbridge->name, strerror(errno));
02302                }
02303                analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
02304                analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
02305                analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
02306                if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner)) {
02307                   ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
02308                }
02309                ast_hangup(chan);
02310                goto quit;
02311             } else {
02312                analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
02313                analog_wait_event(p);
02314                analog_play_tone(p, idx, -1);
02315                analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_THREEWAY);
02316                analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
02317                analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
02318                ast_hangup(chan);
02319                goto quit;
02320             }
02321          } else if (!ast_canmatch_extension(chan, chan->context, exten, 1,
02322             chan->caller.id.number.valid ? chan->caller.id.number.str : NULL)
02323             && !analog_canmatch_featurecode(exten)) {
02324             ast_debug(1, "Can't match %s from '%s' in context %s\n", exten,
02325                chan->caller.id.number.valid && chan->caller.id.number.str
02326                   ? chan->caller.id.number.str : "<Unknown Caller>",
02327                chan->context);
02328             break;
02329          }
02330          if (!timeout) {
02331             timeout = analog_gendigittimeout;
02332          }
02333          if (len && !ast_ignore_pattern(chan->context, exten)) {
02334             analog_play_tone(p, idx, -1);
02335          }
02336       }
02337       break;
02338    case ANALOG_SIG_FXSLS:
02339    case ANALOG_SIG_FXSGS:
02340    case ANALOG_SIG_FXSKS:
02341       /* check for SMDI messages */
02342       if (p->use_smdi && p->smdi_iface) {
02343          smdi_msg = ast_smdi_md_message_wait(p->smdi_iface, ANALOG_SMDI_MD_WAIT_TIMEOUT);
02344          if (smdi_msg != NULL) {
02345             ast_copy_string(chan->exten, smdi_msg->fwd_st, sizeof(chan->exten));
02346 
02347             if (smdi_msg->type == 'B')
02348                pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "b");
02349             else if (smdi_msg->type == 'N')
02350                pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "u");
02351 
02352             ast_debug(1, "Received SMDI message on %s\n", chan->name);
02353          } else {
02354             ast_log(LOG_WARNING, "SMDI enabled but no SMDI message present\n");
02355          }
02356       }
02357 
02358       if (p->use_callerid && (p->cid_signalling == CID_SIG_SMDI && smdi_msg)) {
02359          number = smdi_msg->calling_st;
02360 
02361       /* If we want caller id, we're in a prering state due to a polarity reversal
02362        * and we're set to use a polarity reversal to trigger the start of caller id,
02363        * grab the caller id and wait for ringing to start... */
02364       } else if (p->use_callerid && (chan->_state == AST_STATE_PRERING
02365          && (p->cid_start == ANALOG_CID_START_POLARITY
02366             || p->cid_start == ANALOG_CID_START_POLARITY_IN
02367             || p->cid_start == ANALOG_CID_START_DTMF_NOALERT))) {
02368          /* If set to use DTMF CID signalling, listen for DTMF */
02369          if (p->cid_signalling == CID_SIG_DTMF) {
02370             int k = 0;
02371             int oldlinearity; 
02372             cs = NULL;
02373             ast_debug(1, "Receiving DTMF cid on channel %s\n", chan->name);
02374 
02375             oldlinearity = analog_set_linear_mode(p, idx, 0);
02376 
02377             /*
02378              * We are the only party interested in the Rx stream since
02379              * we have not answered yet.  We don't need or even want DTMF
02380              * emulation.  The DTMF digits can come so fast that emulation
02381              * can drop some of them.
02382              */
02383             ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
02384             res = 4000;/* This is a typical OFF time between rings. */
02385             for (;;) {
02386                struct ast_frame *f;
02387                res = ast_waitfor(chan, res);
02388                if (res <= 0) {
02389                   /*
02390                    * We do not need to restore the analog_set_linear_mode()
02391                    * or AST_FLAG_END_DTMF_ONLY flag settings since we
02392                    * are hanging up the channel.
02393                    */
02394                   ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
02395                      "Exiting simple switch\n");
02396                   ast_hangup(chan);
02397                   goto quit;
02398                }
02399                f = ast_read(chan);
02400                if (!f) {
02401                   break;
02402                }
02403                if (f->frametype == AST_FRAME_DTMF) {
02404                   if (k < ARRAY_LEN(dtmfbuf) - 1) {
02405                      dtmfbuf[k++] = f->subclass.integer;
02406                   }
02407                   ast_debug(1, "CID got digit '%c'\n", f->subclass.integer);
02408                   res = 4000;/* This is a typical OFF time between rings. */
02409                }
02410                ast_frfree(f);
02411                if (chan->_state == AST_STATE_RING ||
02412                   chan->_state == AST_STATE_RINGING) {
02413                   break; /* Got ring */
02414                }
02415             }
02416             ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
02417             dtmfbuf[k] = '\0';
02418 
02419             analog_set_linear_mode(p, idx, oldlinearity);
02420 
02421             /* Got cid and ring. */
02422             ast_debug(1, "CID got string '%s'\n", dtmfbuf);
02423             callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
02424             ast_debug(1, "CID is '%s', flags %d\n", dtmfcid, flags);
02425             /* If first byte is NULL, we have no cid */
02426             if (!ast_strlen_zero(dtmfcid)) {
02427                number = dtmfcid;
02428             } else {
02429                number = NULL;
02430             }
02431 
02432          /* If set to use V23 Signalling, launch our FSK gubbins and listen for it */
02433          } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) {
02434             int timeout = 10000;  /* Ten seconds */
02435             struct timeval start = ast_tvnow();
02436             enum analog_event ev;
02437 
02438             namebuf[0] = 0;
02439             numbuf[0] = 0;
02440 
02441             if (!analog_start_cid_detect(p, p->cid_signalling)) {
02442                while (1) {
02443                   res = analog_get_callerid(p, namebuf, numbuf, &ev, timeout - ast_tvdiff_ms(ast_tvnow(), start));
02444 
02445                   if (res == 0) {
02446                      break;
02447                   }
02448 
02449                   if (res == 1) {
02450                      if (ev == ANALOG_EVENT_NOALARM) {
02451                         analog_set_alarm(p, 0);
02452                      }
02453                      if (p->cid_signalling == CID_SIG_V23_JP) {
02454                         if (ev == ANALOG_EVENT_RINGBEGIN) {
02455                            analog_off_hook(p);
02456                            usleep(1);
02457                         }
02458                      } else {
02459                         ev = ANALOG_EVENT_NONE;
02460                         break;
02461                      }
02462                   }
02463 
02464                   if (ast_tvdiff_ms(ast_tvnow(), start) > timeout)
02465                      break;
02466 
02467                }
02468                name = namebuf;
02469                number = numbuf;
02470 
02471                analog_stop_cid_detect(p);
02472 
02473                if (p->cid_signalling == CID_SIG_V23_JP) {
02474                   res = analog_on_hook(p);
02475                   usleep(1);
02476                }
02477 
02478                /* Finished with Caller*ID, now wait for a ring to make sure there really is a call coming */
02479                res = 4000;/* This is a typical OFF time between rings. */
02480                for (;;) {
02481                   struct ast_frame *f;
02482                   res = ast_waitfor(chan, res);
02483                   if (res <= 0) {
02484                      ast_log(LOG_WARNING, "CID timed out waiting for ring. "
02485                         "Exiting simple switch\n");
02486                      ast_hangup(chan);
02487                      goto quit;
02488                   }
02489                   if (!(f = ast_read(chan))) {
02490                      ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n");
02491                      ast_hangup(chan);
02492                      goto quit;
02493                   }
02494                   ast_frfree(f);
02495                   if (chan->_state == AST_STATE_RING ||
02496                      chan->_state == AST_STATE_RINGING)
02497                      break; /* Got ring */
02498                }
02499 
02500                if (analog_distinctive_ring(chan, p, idx, NULL)) {
02501                   goto quit;
02502                }
02503 
02504                if (res < 0) {
02505                   ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
02506                }
02507             } else {
02508                ast_log(LOG_WARNING, "Unable to get caller ID space\n");
02509             }
02510          } else {
02511             ast_log(LOG_WARNING, "Channel %s in prering "
02512                "state, but I have nothing to do. "
02513                "Terminating simple switch, should be "
02514                "restarted by the actual ring.\n",
02515                chan->name);
02516             ast_hangup(chan);
02517             goto quit;
02518          }
02519       } else if (p->use_callerid && p->cid_start == ANALOG_CID_START_RING) {
02520          int timeout = 10000;  /* Ten seconds */
02521          struct timeval start = ast_tvnow();
02522          enum analog_event ev;
02523          int curRingData[RING_PATTERNS] = { 0 };
02524          int receivedRingT = 0;
02525 
02526          namebuf[0] = 0;
02527          numbuf[0] = 0;
02528 
02529          if (!analog_start_cid_detect(p, p->cid_signalling)) {
02530             while (1) {
02531                res = analog_get_callerid(p, namebuf, numbuf, &ev, timeout - ast_tvdiff_ms(ast_tvnow(), start));
02532 
02533                if (res == 0) {
02534                   break;
02535                }
02536 
02537                if (res == 1 || res == 2) {
02538                   if (ev == ANALOG_EVENT_NOALARM) {
02539                      analog_set_alarm(p, 0);
02540                   } else if (ev == ANALOG_EVENT_POLARITY && p->hanguponpolarityswitch && p->polarity == POLARITY_REV) {
02541                      ast_debug(1, "Hanging up due to polarity reversal on channel %d while detecting callerid\n", p->channel);
02542                      p->polarity = POLARITY_IDLE;
02543                      ast_hangup(chan);
02544                      goto quit;
02545                   } else if (ev != ANALOG_EVENT_NONE && ev != ANALOG_EVENT_RINGBEGIN && ev != ANALOG_EVENT_RINGOFFHOOK) {
02546                      break;
02547                   }
02548                   if (res != 2) {
02549                      /* Let us detect callerid when the telco uses distinctive ring */
02550                      curRingData[receivedRingT] = p->ringt;
02551 
02552                      if (p->ringt < p->ringt_base/2) {
02553                         break;
02554                      }
02555                      /* Increment the ringT counter so we can match it against
02556                         values in chan_dahdi.conf for distinctive ring */
02557                      if (++receivedRingT == RING_PATTERNS) {
02558                         break;
02559                      }
02560                   }
02561                }
02562 
02563                if (ast_tvdiff_ms(ast_tvnow(), start) > timeout) {
02564                   break;
02565                }
02566 
02567             }
02568             name = namebuf;
02569             number = numbuf;
02570 
02571             analog_stop_cid_detect(p);
02572 
02573             if (analog_distinctive_ring(chan, p, idx, curRingData)) {
02574                goto quit;
02575             }
02576 
02577             if (res < 0) {
02578                ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
02579             }
02580          } else {
02581             ast_log(LOG_WARNING, "Unable to get caller ID space\n");
02582          }
02583       } else {
02584          cs = NULL;
02585       }
02586 
02587       if (number) {
02588          ast_shrink_phone_number(number);
02589       }
02590       ast_set_callerid(chan, number, name, number);
02591 
02592       if (cs) {
02593          callerid_free(cs);
02594       }
02595 
02596       analog_handle_notify_message(chan, p, flags, -1);
02597 
02598       ast_setstate(chan, AST_STATE_RING);
02599       chan->rings = 1;
02600       analog_set_ringtimeout(p, p->ringt_base);
02601       res = ast_pbx_run(chan);
02602       if (res) {
02603          ast_hangup(chan);
02604          ast_log(LOG_WARNING, "PBX exited non-zero\n");
02605       }
02606       goto quit;
02607    default:
02608       ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", analog_sigtype_to_str(p->sig), p->channel);
02609       break;
02610    }
02611    res = analog_play_tone(p, idx, ANALOG_TONE_CONGESTION);
02612    if (res < 0) {
02613       ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
02614    }
02615    ast_hangup(chan);
02616 quit:
02617    if (smdi_msg) {
02618       ASTOBJ_UNREF(smdi_msg, ast_smdi_md_message_destroy);
02619    }
02620    analog_decrease_ss_count(p);
02621    return NULL;
02622 }
02623 
02624 int analog_ss_thread_start(struct analog_pvt *p, struct ast_channel *chan)
02625 {
02626    pthread_t threadid;
02627 
02628    return ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, p);
02629 }
02630 
02631 static struct ast_frame *__analog_handle_event(struct analog_pvt *p, struct ast_channel *ast)
02632 {
02633    int res, x;
02634    int mysig;
02635    enum analog_sub idx;
02636    char *c;
02637    pthread_t threadid;
02638    struct ast_channel *chan;
02639    struct ast_frame *f;
02640 
02641    ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
02642 
02643    idx = analog_get_index(ast, p, 0);
02644    if (idx < 0) {
02645       return &ast_null_frame;
02646    }
02647    if (idx != ANALOG_SUB_REAL) {
02648       ast_log(LOG_ERROR, "We got an event on a non real sub.  Fix it!\n");
02649    }
02650 
02651    mysig = p->sig;
02652    if (p->outsigmod > -1) {
02653       mysig = p->outsigmod;
02654    }
02655 
02656    p->subs[idx].f.frametype = AST_FRAME_NULL;
02657    p->subs[idx].f.subclass.integer = 0;
02658    p->subs[idx].f.datalen = 0;
02659    p->subs[idx].f.samples = 0;
02660    p->subs[idx].f.mallocd = 0;
02661    p->subs[idx].f.offset = 0;
02662    p->subs[idx].f.src = "dahdi_handle_event";
02663    p->subs[idx].f.data.ptr = NULL;
02664    f = &p->subs[idx].f;
02665 
02666    res = analog_get_event(p);
02667 
02668    ast_debug(1, "Got event %s(%d) on channel %d (index %d)\n", analog_event2str(res), res, p->channel, idx);
02669 
02670    if (res & (ANALOG_EVENT_PULSEDIGIT | ANALOG_EVENT_DTMFUP)) {
02671       analog_set_pulsedial(p, (res & ANALOG_EVENT_PULSEDIGIT) ? 1 : 0);
02672       ast_debug(1, "Detected %sdigit '%c'\n", (res & ANALOG_EVENT_PULSEDIGIT) ? "pulse ": "", res & 0xff);
02673       analog_confmute(p, 0);
02674       p->subs[idx].f.frametype = AST_FRAME_DTMF_END;
02675       p->subs[idx].f.subclass.integer = res & 0xff;
02676       analog_handle_dtmf(p, ast, idx, &f);
02677       return f;
02678    }
02679 
02680    if (res & ANALOG_EVENT_DTMFDOWN) {
02681       ast_debug(1, "DTMF Down '%c'\n", res & 0xff);
02682       /* Mute conference */
02683       analog_confmute(p, 1);
02684       p->subs[idx].f.frametype = AST_FRAME_DTMF_BEGIN;
02685       p->subs[idx].f.subclass.integer = res & 0xff;
02686       analog_handle_dtmf(p, ast, idx, &f);
02687       return f;
02688    }
02689 
02690    switch (res) {
02691    case ANALOG_EVENT_EC_DISABLED:
02692       ast_verb(3, "Channel %d echo canceler disabled due to CED detection\n", p->channel);
02693       analog_set_echocanceller(p, 0);
02694       break;
02695 #ifdef HAVE_DAHDI_ECHOCANCEL_FAX_MODE
02696    case ANALOG_EVENT_TX_CED_DETECTED:
02697       ast_verb(3, "Channel %d detected a CED tone towards the network.\n", p->channel);
02698       break;
02699    case ANALOG_EVENT_RX_CED_DETECTED:
02700       ast_verb(3, "Channel %d detected a CED tone from the network.\n", p->channel);
02701       break;
02702    case ANALOG_EVENT_EC_NLP_DISABLED:
02703       ast_verb(3, "Channel %d echo canceler disabled its NLP.\n", p->channel);
02704       break;
02705    case ANALOG_EVENT_EC_NLP_ENABLED:
02706       ast_verb(3, "Channel %d echo canceler enabled its NLP.\n", p->channel);
02707       break;
02708 #endif
02709    case ANALOG_EVENT_PULSE_START:
02710       /* Stop tone if there's a pulse start and the PBX isn't started */
02711       if (!ast->pbx)
02712          analog_play_tone(p, ANALOG_SUB_REAL, -1);
02713       break;
02714    case ANALOG_EVENT_DIALCOMPLETE:
02715       if (p->inalarm) {
02716          break;
02717       }
02718       x = analog_is_dialing(p, idx);
02719       if (!x) { /* if not still dialing in driver */
02720          analog_set_echocanceller(p, 1);
02721          if (p->echobreak) {
02722             analog_train_echocanceller(p);
02723             ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
02724             p->dop.op = ANALOG_DIAL_OP_REPLACE;
02725             analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
02726             p->echobreak = 0;
02727          } else {
02728             analog_set_dialing(p, 0);
02729             if ((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) {
02730                /* if thru with dialing after offhook */
02731                if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
02732                   ast_setstate(ast, AST_STATE_UP);
02733                   p->subs[idx].f.frametype = AST_FRAME_CONTROL;
02734                   p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
02735                   break;
02736                } else { /* if to state wait for offhook to dial rest */
02737                   /* we now wait for off hook */
02738                   ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
02739                }
02740             }
02741             if (ast->_state == AST_STATE_DIALING) {
02742                if (analog_check_confirmanswer(p) || (!p->dialednone
02743                   && ((mysig == ANALOG_SIG_EM) || (mysig == ANALOG_SIG_EM_E1)
02744                      || (mysig == ANALOG_SIG_EMWINK) || (mysig == ANALOG_SIG_FEATD)
02745                      || (mysig == ANALOG_SIG_FEATDMF_TA) || (mysig == ANALOG_SIG_FEATDMF)
02746                      || (mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA)
02747                      || (mysig == ANALOG_SIG_FGC_CAMAMF) || (mysig == ANALOG_SIG_FEATB)
02748                      || (mysig == ANALOG_SIG_SF) || (mysig == ANALOG_SIG_SFWINK)
02749                      || (mysig == ANALOG_SIG_SF_FEATD) || (mysig == ANALOG_SIG_SF_FEATDMF)
02750                      || (mysig == ANALOG_SIG_SF_FEATB)))) {
02751                   ast_setstate(ast, AST_STATE_RINGING);
02752                } else if (!p->answeronpolarityswitch) {
02753                   ast_setstate(ast, AST_STATE_UP);
02754                   p->subs[idx].f.frametype = AST_FRAME_CONTROL;
02755                   p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
02756                   /* If aops=0 and hops=1, this is necessary */
02757                   p->polarity = POLARITY_REV;
02758                } else {
02759                   /* Start clean, so we can catch the change to REV polarity when party answers */
02760                   p->polarity = POLARITY_IDLE;
02761                }
02762             }
02763          }
02764       }
02765       break;
02766    case ANALOG_EVENT_ALARM:
02767       analog_set_alarm(p, 1);
02768       analog_get_and_handle_alarms(p);
02769    case ANALOG_EVENT_ONHOOK:
02770       switch (p->sig) {
02771       case ANALOG_SIG_FXOLS:
02772       case ANALOG_SIG_FXOGS:
02773       case ANALOG_SIG_FXOKS:
02774          analog_start_polarityswitch(p);
02775          p->fxsoffhookstate = 0;
02776          p->onhooktime = time(NULL);
02777          p->msgstate = -1;
02778          /* Check for some special conditions regarding call waiting */
02779          if (idx == ANALOG_SUB_REAL) {
02780             /* The normal line was hung up */
02781             if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
02782                /* Need to hold the lock for real-call, private, and call-waiting call */
02783                analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
02784                if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
02785                   /*
02786                    * The call waiting call dissappeared.
02787                    * This is now a normal hangup.
02788                    */
02789                   analog_set_echocanceller(p, 0);
02790                   return NULL;
02791                }
02792 
02793                /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
02794                analog_swap_subs(p, ANALOG_SUB_CALLWAIT, ANALOG_SUB_REAL);
02795                ast_verb(3, "Channel %d still has (callwait) call, ringing phone\n", p->channel);
02796                analog_unalloc_sub(p, ANALOG_SUB_CALLWAIT);
02797                analog_stop_callwait(p);
02798                analog_set_new_owner(p, NULL);
02799                /* Don't start streaming audio yet if the incoming call isn't up yet */
02800                if (p->subs[ANALOG_SUB_REAL].owner->_state != AST_STATE_UP) {
02801                   analog_set_dialing(p, 1);
02802                }
02803                /* Unlock the call-waiting call that we swapped to real-call. */
02804                ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
02805                analog_ring(p);
02806             } else if (p->subs[ANALOG_SUB_THREEWAY].owner) {
02807                unsigned int mssinceflash;
02808 
02809                /* Need to hold the lock for real-call, private, and 3-way call */
02810                analog_lock_sub_owner(p, ANALOG_SUB_THREEWAY);
02811                if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
02812                   ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
02813                   /* Just hangup */
02814                   return NULL;
02815                }
02816                if (p->owner != ast) {
02817                   ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
02818                   ast_log(LOG_WARNING, "This isn't good...\n");
02819                   /* Just hangup */
02820                   return NULL;
02821                }
02822 
02823                mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
02824                ast_debug(1, "Last flash was %d ms ago\n", mssinceflash);
02825                if (mssinceflash < MIN_MS_SINCE_FLASH) {
02826                   /* It hasn't been long enough since the last flashook.  This is probably a bounce on
02827                      hanging up.  Hangup both channels now */
02828                   ast_debug(1, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
02829                   ast_queue_hangup_with_cause(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CAUSE_NO_ANSWER);
02830                   ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
02831                   ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
02832                } else if ((ast->pbx) || (ast->_state == AST_STATE_UP)) {
02833                   if (p->transfer) {
02834                      int inthreeway;
02835 
02836                      inthreeway = p->subs[ANALOG_SUB_THREEWAY].inthreeway;
02837 
02838                      /* In any case this isn't a threeway call anymore */
02839                      analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
02840                      analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 0);
02841 
02842                      /* Only attempt transfer if the phone is ringing; why transfer to busy tone eh? */
02843                      if (!p->transfertobusy && ast->_state == AST_STATE_BUSY) {
02844                         /* Swap subs and dis-own channel */
02845                         analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
02846                         /* Unlock the 3-way call that we swapped to real-call. */
02847                         ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
02848                         analog_set_new_owner(p, NULL);
02849                         /* Ring the phone */
02850                         analog_ring(p);
02851                      } else {
02852                         res = analog_attempt_transfer(p, inthreeway);
02853                         if (res < 0) {
02854                            /* Transfer attempt failed. */
02855                            ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
02856                            ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
02857                         } else if (res) {
02858                            /* Don't actually hang up at this point */
02859                            break;
02860                         }
02861                      }
02862                   } else {
02863                      ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
02864                      ast_channel_unlock(p->subs[ANALOG_SUB_THREEWAY].owner);
02865                   }
02866                } else {
02867                   /* Swap subs and dis-own channel */
02868                   analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
02869                   /* Unlock the 3-way call that we swapped to real-call. */
02870                   ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
02871                   analog_set_new_owner(p, NULL);
02872                   /* Ring the phone */
02873                   analog_ring(p);
02874                }
02875             }
02876          } else {
02877             ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", idx);
02878          }
02879          /* Fall through */
02880       default:
02881          analog_set_echocanceller(p, 0);
02882          return NULL;
02883       }
02884       break;
02885    case ANALOG_EVENT_RINGOFFHOOK:
02886       if (p->inalarm) {
02887          break;
02888       }
02889       /* for E911, its supposed to wait for offhook then dial
02890          the second half of the dial string */
02891       if (((mysig == ANALOG_SIG_E911) || (mysig == ANALOG_SIG_FGC_CAMA) || (mysig == ANALOG_SIG_FGC_CAMAMF)) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
02892          c = strchr(p->dialdest, '/');
02893          if (c) {
02894             c++;
02895          } else {
02896             c = p->dialdest;
02897          }
02898          if (*c) {
02899             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
02900          } else {
02901             ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
02902          }
02903          if (strlen(p->dop.dialstr) > 4) {
02904             memset(p->echorest, 'w', sizeof(p->echorest) - 1);
02905             strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
02906             p->echorest[sizeof(p->echorest) - 1] = '\0';
02907             p->echobreak = 1;
02908             p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
02909          } else {
02910             p->echobreak = 0;
02911          }
02912          if (analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop)) {
02913             int saveerr = errno;
02914             analog_on_hook(p);
02915             ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
02916             return NULL;
02917          }
02918          analog_set_dialing(p, 1);
02919          return &p->subs[idx].f;
02920       }
02921       switch (p->sig) {
02922       case ANALOG_SIG_FXOLS:
02923       case ANALOG_SIG_FXOGS:
02924       case ANALOG_SIG_FXOKS:
02925          p->fxsoffhookstate = 1;
02926          switch (ast->_state) {
02927          case AST_STATE_RINGING:
02928             analog_set_echocanceller(p, 1);
02929             analog_train_echocanceller(p);
02930             p->subs[idx].f.frametype = AST_FRAME_CONTROL;
02931             p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
02932             /* Make sure it stops ringing */
02933             analog_set_needringing(p, 0);
02934             analog_off_hook(p);
02935             ast_debug(1, "channel %d answered\n", p->channel);
02936 
02937             /* Cancel any running CallerID spill */
02938             analog_cancel_cidspill(p);
02939 
02940             analog_set_dialing(p, 0);
02941             p->callwaitcas = 0;
02942             if (analog_check_confirmanswer(p)) {
02943                /* Ignore answer if "confirm answer" is enabled */
02944                p->subs[idx].f.frametype = AST_FRAME_NULL;
02945                p->subs[idx].f.subclass.integer = 0;
02946             } else if (!ast_strlen_zero(p->dop.dialstr)) {
02947                /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */
02948                res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
02949                if (res < 0) {
02950                   ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
02951                   p->dop.dialstr[0] = '\0';
02952                   return NULL;
02953                } else {
02954                   ast_debug(1, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
02955                   p->subs[idx].f.frametype = AST_FRAME_NULL;
02956                   p->subs[idx].f.subclass.integer = 0;
02957                   analog_set_dialing(p, 1);
02958                }
02959                p->dop.dialstr[0] = '\0';
02960                ast_setstate(ast, AST_STATE_DIALING);
02961             } else {
02962                ast_setstate(ast, AST_STATE_UP);
02963                analog_answer_polarityswitch(p);
02964             }
02965             return &p->subs[idx].f;
02966          case AST_STATE_DOWN:
02967             ast_setstate(ast, AST_STATE_RING);
02968             ast->rings = 1;
02969             p->subs[idx].f.frametype = AST_FRAME_CONTROL;
02970             p->subs[idx].f.subclass.integer = AST_CONTROL_OFFHOOK;
02971             ast_debug(1, "channel %d picked up\n", p->channel);
02972             return &p->subs[idx].f;
02973          case AST_STATE_UP:
02974             /* Make sure it stops ringing */
02975             analog_off_hook(p);
02976             /* Okay -- probably call waiting*/
02977             if (ast_bridged_channel(p->owner)) {
02978                ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
02979             }
02980             break;
02981          case AST_STATE_RESERVED:
02982             /* Start up dialtone */
02983             if (analog_has_voicemail(p)) {
02984                res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_STUTTER);
02985             } else {
02986                res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_DIALTONE);
02987             }
02988             break;
02989          default:
02990             ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
02991          }
02992          break;
02993       case ANALOG_SIG_FXSLS:
02994       case ANALOG_SIG_FXSGS:
02995       case ANALOG_SIG_FXSKS:
02996          if (ast->_state == AST_STATE_RING) {
02997             analog_set_ringtimeout(p, p->ringt_base);
02998          }
02999 
03000          /* Fall through */
03001       case ANALOG_SIG_EM:
03002       case ANALOG_SIG_EM_E1:
03003       case ANALOG_SIG_EMWINK:
03004       case ANALOG_SIG_FEATD:
03005       case ANALOG_SIG_FEATDMF:
03006       case ANALOG_SIG_FEATDMF_TA:
03007       case ANALOG_SIG_E911:
03008       case ANALOG_SIG_FGC_CAMA:
03009       case ANALOG_SIG_FGC_CAMAMF:
03010       case ANALOG_SIG_FEATB:
03011       case ANALOG_SIG_SF:
03012       case ANALOG_SIG_SFWINK:
03013       case ANALOG_SIG_SF_FEATD:
03014       case ANALOG_SIG_SF_FEATDMF:
03015       case ANALOG_SIG_SF_FEATB:
03016          switch (ast->_state) {
03017          case AST_STATE_PRERING:
03018             ast_setstate(ast, AST_STATE_RING);
03019             /* Fall through */
03020          case AST_STATE_DOWN:
03021          case AST_STATE_RING:
03022             ast_debug(1, "Ring detected\n");
03023             p->subs[idx].f.frametype = AST_FRAME_CONTROL;
03024             p->subs[idx].f.subclass.integer = AST_CONTROL_RING;
03025             break;
03026          case AST_STATE_RINGING:
03027          case AST_STATE_DIALING:
03028             if (p->outgoing) {
03029                ast_debug(1, "Line answered\n");
03030                if (analog_check_confirmanswer(p)) {
03031                   p->subs[idx].f.frametype = AST_FRAME_NULL;
03032                   p->subs[idx].f.subclass.integer = 0;
03033                } else {
03034                   p->subs[idx].f.frametype = AST_FRAME_CONTROL;
03035                   p->subs[idx].f.subclass.integer = AST_CONTROL_ANSWER;
03036                   ast_setstate(ast, AST_STATE_UP);
03037                }
03038                break;
03039             }
03040             /* Fall through */
03041          default:
03042             ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
03043             break;
03044          }
03045          break;
03046       default:
03047          ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
03048          break;
03049       }
03050       break;
03051    case ANALOG_EVENT_RINGBEGIN:
03052       switch (p->sig) {
03053       case ANALOG_SIG_FXSLS:
03054       case ANALOG_SIG_FXSGS:
03055       case ANALOG_SIG_FXSKS:
03056          if (ast->_state == AST_STATE_RING) {
03057             analog_set_ringtimeout(p, p->ringt_base);
03058          }
03059          break;
03060       default:
03061          break;
03062       }
03063       break;
03064    case ANALOG_EVENT_RINGEROFF:
03065       if (p->inalarm) break;
03066       ast->rings++;
03067       if (ast->rings == p->cidrings) {
03068          analog_send_callerid(p, 0, &p->caller);
03069       }
03070 
03071       if (ast->rings > p->cidrings) {
03072          analog_cancel_cidspill(p);
03073          p->callwaitcas = 0;
03074       }
03075       p->subs[idx].f.frametype = AST_FRAME_CONTROL;
03076       p->subs[idx].f.subclass.integer = AST_CONTROL_RINGING;
03077       break;
03078    case ANALOG_EVENT_RINGERON:
03079       break;
03080    case ANALOG_EVENT_NOALARM:
03081       analog_set_alarm(p, 0);
03082       ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
03083       manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
03084          "Channel: %d\r\n", p->channel);
03085       break;
03086    case ANALOG_EVENT_WINKFLASH:
03087       if (p->inalarm) {
03088          break;
03089       }
03090       /* Remember last time we got a flash-hook */
03091       gettimeofday(&p->flashtime, NULL);
03092       switch (mysig) {
03093       case ANALOG_SIG_FXOLS:
03094       case ANALOG_SIG_FXOGS:
03095       case ANALOG_SIG_FXOKS:
03096          ast_debug(1, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
03097             idx, analog_get_sub_fd(p, ANALOG_SUB_REAL), analog_get_sub_fd(p, ANALOG_SUB_CALLWAIT), analog_get_sub_fd(p, ANALOG_SUB_THREEWAY));
03098 
03099          /* Cancel any running CallerID spill */
03100          analog_cancel_cidspill(p);
03101          p->callwaitcas = 0;
03102 
03103          if (idx != ANALOG_SUB_REAL) {
03104             ast_log(LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", idx, p->channel);
03105             goto winkflashdone;
03106          }
03107 
03108          if (p->subs[ANALOG_SUB_CALLWAIT].owner) {
03109             /* Need to hold the lock for real-call, private, and call-waiting call */
03110             analog_lock_sub_owner(p, ANALOG_SUB_CALLWAIT);
03111             if (!p->subs[ANALOG_SUB_CALLWAIT].owner) {
03112                /*
03113                 * The call waiting call dissappeared.
03114                 * Let's just ignore this flash-hook.
03115                 */
03116                ast_log(LOG_NOTICE, "Whoa, the call-waiting call disappeared.\n");
03117                goto winkflashdone;
03118             }
03119 
03120             /* Swap to call-wait */
03121             analog_swap_subs(p, ANALOG_SUB_REAL, ANALOG_SUB_CALLWAIT);
03122             analog_play_tone(p, ANALOG_SUB_REAL, -1);
03123             analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
03124             ast_debug(1, "Making %s the new owner\n", p->owner->name);
03125             if (p->subs[ANALOG_SUB_REAL].owner->_state == AST_STATE_RINGING) {
03126                ast_setstate(p->subs[ANALOG_SUB_REAL].owner, AST_STATE_UP);
03127                ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_ANSWER);
03128             }
03129             analog_stop_callwait(p);
03130 
03131             /* Start music on hold if appropriate */
03132             if (!p->subs[ANALOG_SUB_CALLWAIT].inthreeway && ast_bridged_channel(p->subs[ANALOG_SUB_CALLWAIT].owner)) {
03133                ast_queue_control_data(p->subs[ANALOG_SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
03134                   S_OR(p->mohsuggest, NULL),
03135                   !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
03136             }
03137             if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner)) {
03138                ast_queue_control_data(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_HOLD,
03139                   S_OR(p->mohsuggest, NULL),
03140                   !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
03141             }
03142             ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
03143 
03144             /* Unlock the call-waiting call that we swapped to real-call. */
03145             ast_channel_unlock(p->subs[ANALOG_SUB_REAL].owner);
03146          } else if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
03147             if (!p->threewaycalling) {
03148                /* Just send a flash if no 3-way calling */
03149                ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_FLASH);
03150                goto winkflashdone;
03151             } else if (!analog_check_for_conference(p)) {
03152                char cid_num[256];
03153                char cid_name[256];
03154 
03155                cid_num[0] = '\0';
03156                cid_name[0] = '\0';
03157                if (p->dahditrcallerid && p->owner) {
03158                   if (p->owner->caller.id.number.valid
03159                      && p->owner->caller.id.number.str) {
03160                      ast_copy_string(cid_num, p->owner->caller.id.number.str,
03161                         sizeof(cid_num));
03162                   }
03163                   if (p->owner->caller.id.name.valid
03164                      && p->owner->caller.id.name.str) {
03165                      ast_copy_string(cid_name, p->owner->caller.id.name.str,
03166                         sizeof(cid_name));
03167                   }
03168                }
03169                /* XXX This section needs much more error checking!!! XXX */
03170                /* Start a 3-way call if feasible */
03171                if (!((ast->pbx) ||
03172                   (ast->_state == AST_STATE_UP) ||
03173                   (ast->_state == AST_STATE_RING))) {
03174                   ast_debug(1, "Flash when call not up or ringing\n");
03175                   goto winkflashdone;
03176                }
03177                if (analog_alloc_sub(p, ANALOG_SUB_THREEWAY)) {
03178                   ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
03179                   goto winkflashdone;
03180                }
03181                /* Make new channel */
03182                chan = analog_new_ast_channel(p, AST_STATE_RESERVED, 0, ANALOG_SUB_THREEWAY, NULL);
03183                if (!chan) {
03184                   ast_log(LOG_WARNING,
03185                      "Cannot allocate new call structure on channel %d\n",
03186                      p->channel);
03187                   analog_unalloc_sub(p, ANALOG_SUB_THREEWAY);
03188                   goto winkflashdone;
03189                }
03190                if (p->dahditrcallerid) {
03191                   if (!p->origcid_num) {
03192                      p->origcid_num = ast_strdup(p->cid_num);
03193                   }
03194                   if (!p->origcid_name) {
03195                      p->origcid_name = ast_strdup(p->cid_name);
03196                   }
03197                   ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
03198                   ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
03199                }
03200                /* Swap things around between the three-way and real call */
03201                analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
03202                /* Disable echo canceller for better dialing */
03203                analog_set_echocanceller(p, 0);
03204                res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_DIALRECALL);
03205                if (res) {
03206                   ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
03207                }
03208                analog_set_new_owner(p, chan);
03209                p->ss_astchan = chan;
03210                if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, p)) {
03211                   ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
03212                   res = analog_play_tone(p, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
03213                   analog_set_echocanceller(p, 1);
03214                   ast_hangup(chan);
03215                } else {
03216                   ast_verb(3, "Started three way call on channel %d\n", p->channel);
03217 
03218                   /* Start music on hold if appropriate */
03219                   if (ast_bridged_channel(p->subs[ANALOG_SUB_THREEWAY].owner)) {
03220                      ast_queue_control_data(p->subs[ANALOG_SUB_THREEWAY].owner, AST_CONTROL_HOLD,
03221                         S_OR(p->mohsuggest, NULL),
03222                         !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
03223                   }
03224                }
03225             }
03226          } else {
03227             /* Already have a 3 way call */
03228             enum analog_sub orig_3way_sub;
03229 
03230             /* Need to hold the lock for real-call, private, and 3-way call */
03231             analog_lock_sub_owner(p, ANALOG_SUB_THREEWAY);
03232             if (!p->subs[ANALOG_SUB_THREEWAY].owner) {
03233                /*
03234                 * The 3-way call dissappeared.
03235                 * Let's just ignore this flash-hook.
03236                 */
03237                ast_log(LOG_NOTICE, "Whoa, the 3-way call disappeared.\n");
03238                goto winkflashdone;
03239             }
03240             orig_3way_sub = ANALOG_SUB_THREEWAY;
03241 
03242             if (p->subs[ANALOG_SUB_THREEWAY].inthreeway) {
03243                /* Call is already up, drop the last person */
03244                ast_debug(1, "Got flash with three way call up, dropping last call on %d\n", p->channel);
03245                /* If the primary call isn't answered yet, use it */
03246                if ((p->subs[ANALOG_SUB_REAL].owner->_state != AST_STATE_UP) &&
03247                   (p->subs[ANALOG_SUB_THREEWAY].owner->_state == AST_STATE_UP)) {
03248                   /* Swap back -- we're dropping the real 3-way that isn't finished yet*/
03249                   analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
03250                   orig_3way_sub = ANALOG_SUB_REAL;
03251                   analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
03252                }
03253                /* Drop the last call and stop the conference */
03254                ast_verb(3, "Dropping three-way call on %s\n", p->subs[ANALOG_SUB_THREEWAY].owner->name);
03255                ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
03256                analog_set_inthreeway(p, ANALOG_SUB_REAL, 0);
03257                analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 0);
03258             } else {
03259                /* Lets see what we're up to */
03260                if (((ast->pbx) || (ast->_state == AST_STATE_UP)) &&
03261                   (p->transfertobusy || (ast->_state != AST_STATE_BUSY))) {
03262                   ast_verb(3, "Building conference call with %s and %s\n",
03263                      p->subs[ANALOG_SUB_THREEWAY].owner->name,
03264                      p->subs[ANALOG_SUB_REAL].owner->name);
03265                   /* Put them in the threeway, and flip */
03266                   analog_set_inthreeway(p, ANALOG_SUB_THREEWAY, 1);
03267                   analog_set_inthreeway(p, ANALOG_SUB_REAL, 1);
03268                   if (ast->_state == AST_STATE_UP) {
03269                      analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
03270                      orig_3way_sub = ANALOG_SUB_REAL;
03271                   }
03272                   if (ast_bridged_channel(p->subs[orig_3way_sub].owner)) {
03273                      ast_queue_control(p->subs[orig_3way_sub].owner, AST_CONTROL_UNHOLD);
03274                   }
03275                   analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
03276                } else {
03277                   ast_verb(3, "Dumping incomplete call on %s\n", p->subs[ANALOG_SUB_THREEWAY].owner->name);
03278                   analog_swap_subs(p, ANALOG_SUB_THREEWAY, ANALOG_SUB_REAL);
03279                   orig_3way_sub = ANALOG_SUB_REAL;
03280                   ast_softhangup_nolock(p->subs[ANALOG_SUB_THREEWAY].owner, AST_SOFTHANGUP_DEV);
03281                   analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
03282                   if (ast_bridged_channel(p->subs[ANALOG_SUB_REAL].owner)) {
03283                      ast_queue_control(p->subs[ANALOG_SUB_REAL].owner, AST_CONTROL_UNHOLD);
03284                   }
03285                   analog_set_echocanceller(p, 1);
03286                }
03287             }
03288             ast_channel_unlock(p->subs[orig_3way_sub].owner);
03289          }
03290 winkflashdone:
03291          analog_update_conf(p);
03292          break;
03293       case ANALOG_SIG_EM:
03294       case ANALOG_SIG_EM_E1:
03295       case ANALOG_SIG_FEATD:
03296       case ANALOG_SIG_SF:
03297       case ANALOG_SIG_SFWINK:
03298       case ANALOG_SIG_SF_FEATD:
03299       case ANALOG_SIG_FXSLS:
03300       case ANALOG_SIG_FXSGS:
03301          if (p->dialing) {
03302             ast_debug(1, "Ignoring wink on channel %d\n", p->channel);
03303          } else {
03304             ast_debug(1, "Got wink in weird state %d on channel %d\n", ast->_state, p->channel);
03305          }
03306          break;
03307       case ANALOG_SIG_FEATDMF_TA:
03308          switch (p->whichwink) {
03309          case 0:
03310             ast_debug(1, "ANI2 set to '%d' and ANI is '%s'\n", p->owner->caller.ani2,
03311                S_COR(p->owner->caller.ani.number.valid,
03312                   p->owner->caller.ani.number.str, ""));
03313             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#",
03314                p->owner->caller.ani2,
03315                S_COR(p->owner->caller.ani.number.valid,
03316                   p->owner->caller.ani.number.str, ""));
03317             break;
03318          case 1:
03319             ast_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr));
03320             break;
03321          case 2:
03322             ast_log(LOG_WARNING, "Received unexpected wink on channel of type ANALOG_SIG_FEATDMF_TA\n");
03323             return NULL;
03324          }
03325          p->whichwink++;
03326          /* Fall through */
03327       case ANALOG_SIG_FEATDMF:
03328       case ANALOG_SIG_E911:
03329       case ANALOG_SIG_FGC_CAMAMF:
03330       case ANALOG_SIG_FGC_CAMA:
03331       case ANALOG_SIG_FEATB:
03332       case ANALOG_SIG_SF_FEATDMF:
03333       case ANALOG_SIG_SF_FEATB:
03334       case ANALOG_SIG_EMWINK:
03335          /* FGD MF and EMWINK *Must* wait for wink */
03336          if (!ast_strlen_zero(p->dop.dialstr)) {
03337             res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
03338             if (res < 0) {
03339                ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
03340                p->dop.dialstr[0] = '\0';
03341                return NULL;
03342             } else {
03343                ast_debug(1, "Sent deferred digit string on channel %d: %s\n", p->channel, p->dop.dialstr);
03344             }
03345          }
03346          p->dop.dialstr[0] = '\0';
03347          break;
03348       default:
03349          ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
03350       }
03351       break;
03352    case ANALOG_EVENT_HOOKCOMPLETE:
03353       if (p->inalarm) break;
03354       if (analog_check_waitingfordt(p)) {
03355          break;
03356       }
03357       switch (mysig) {
03358       case ANALOG_SIG_FXSLS:  /* only interesting for FXS */
03359       case ANALOG_SIG_FXSGS:
03360       case ANALOG_SIG_FXSKS:
03361       case ANALOG_SIG_EM:
03362       case ANALOG_SIG_EM_E1:
03363       case ANALOG_SIG_EMWINK:
03364       case ANALOG_SIG_FEATD:
03365       case ANALOG_SIG_SF:
03366       case ANALOG_SIG_SFWINK:
03367       case ANALOG_SIG_SF_FEATD:
03368          if (!ast_strlen_zero(p->dop.dialstr)) {
03369             res = analog_dial_digits(p, ANALOG_SUB_REAL, &p->dop);
03370             if (res < 0) {
03371                ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
03372                p->dop.dialstr[0] = '\0';
03373                return NULL;
03374             } else {
03375                ast_debug(1, "Sent deferred digit string on channel %d: %s\n", p->channel, p->dop.dialstr);
03376             }
03377          }
03378          p->dop.dialstr[0] = '\0';
03379          p->dop.op = ANALOG_DIAL_OP_REPLACE;
03380          break;
03381       case ANALOG_SIG_FEATDMF:
03382       case ANALOG_SIG_FEATDMF_TA:
03383       case ANALOG_SIG_E911:
03384       case ANALOG_SIG_FGC_CAMA:
03385       case ANALOG_SIG_FGC_CAMAMF:
03386       case ANALOG_SIG_FEATB:
03387       case ANALOG_SIG_SF_FEATDMF:
03388       case ANALOG_SIG_SF_FEATB:
03389          ast_debug(1, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
03390          break;
03391       default:
03392          break;
03393       }
03394       break;
03395    case ANALOG_EVENT_POLARITY:
03396       /*
03397        * If we get a Polarity Switch event, this could be
03398        * due to line seizure, remote end connect or remote end disconnect.
03399        *
03400        * Check to see if we should change the polarity state and
03401        * mark the channel as UP or if this is an indication
03402        * of remote end disconnect.
03403        */
03404 
03405       if (p->polarityonanswerdelay > 0) {
03406          /* check if event is not too soon after OffHook or Answer */
03407          if (ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
03408             switch (ast->_state) {
03409             case AST_STATE_DIALING:       /*!< Digits (or equivalent) have been dialed */
03410             case AST_STATE_RINGING:       /*!< Remote end is ringing */
03411                if (p->answeronpolarityswitch) {
03412                   ast_debug(1, "Answering on polarity switch! channel %d\n", p->channel);
03413                   ast_setstate(p->owner, AST_STATE_UP);
03414                   p->polarity = POLARITY_REV;
03415                   if (p->hanguponpolarityswitch) {
03416                      p->polaritydelaytv = ast_tvnow();
03417                   }
03418                } else {
03419                   ast_debug(1, "Ignore Answer on polarity switch, channel %d\n", p->channel);
03420                }
03421                break;
03422 
03423             case AST_STATE_UP:            /*!< Line is up */
03424             case AST_STATE_RING:       /*!< Line is ringing */
03425                if (p->hanguponpolarityswitch) {
03426                   ast_debug(1, "HangingUp on polarity switch! channel %d\n", p->channel);
03427                   ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
03428                   p->polarity = POLARITY_IDLE;
03429                } else {
03430                   ast_debug(1, "Ignore Hangup on polarity switch, channel %d\n", p->channel);
03431                }
03432                break;
03433 
03434             case AST_STATE_DOWN:          /*!< Channel is down and available */
03435             case AST_STATE_RESERVED:         /*!< Channel is down, but reserved */
03436             case AST_STATE_OFFHOOK:          /*!< Channel is off hook */
03437             case AST_STATE_BUSY:          /*!< Line is busy */
03438             case AST_STATE_DIALING_OFFHOOK:     /*!< Digits (or equivalent) have been dialed while offhook */
03439             case AST_STATE_PRERING:          /*!< Channel has detected an incoming call and is waiting for ring */
03440             default:
03441                if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
03442                   ast_debug(1, "Ignoring Polarity switch on channel %d, state %d\n", p->channel, ast->_state);
03443                }
03444                break;
03445             }
03446 
03447          } else {
03448             /* event is too soon after OffHook or Answer */
03449             switch (ast->_state) {
03450             case AST_STATE_DIALING:    /*!< Digits (or equivalent) have been dialed */
03451             case AST_STATE_RINGING:    /*!< Remote end is ringing */
03452                if (p->answeronpolarityswitch) {
03453                   ast_debug(1, "Polarity switch detected but NOT answering (too close to OffHook event) on channel %d, state %d\n", p->channel, ast->_state);
03454                }
03455                break;
03456 
03457             case AST_STATE_UP:         /*!< Line is up */
03458             case AST_STATE_RING:    /*!< Line is ringing */
03459                if (p->hanguponpolarityswitch) {
03460                   ast_debug(1, "Polarity switch detected but NOT hanging up (too close to Answer event) on channel %d, state %d\n", p->channel, ast->_state);
03461                }
03462                break;
03463 
03464             default:
03465                if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
03466                   ast_debug(1, "Polarity switch detected (too close to previous event) on channel %d, state %d\n", p->channel, ast->_state);
03467                }
03468                break;
03469             }
03470          }
03471       }
03472 
03473       /* Added more log_debug information below to provide a better indication of what is going on */
03474       ast_debug(1, "Polarity Reversal event occured - DEBUG 2: channel %d, state %d, pol= %d, aonp= %d, honp= %d, pdelay= %d, tv= %" PRIi64 "\n", p->channel, ast->_state, p->polarity, p->answeronpolarityswitch, p->hanguponpolarityswitch, p->polarityonanswerdelay, ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) );
03475       break;
03476    default:
03477       ast_debug(1, "Dunno what to do with event %d on channel %d\n", res, p->channel);
03478    }
03479    return &p->subs[idx].f;
03480 }
03481 
03482 struct ast_frame *analog_exception(struct analog_pvt *p, struct ast_channel *ast)
03483 {
03484    int res;
03485    int idx;
03486    struct ast_frame *f;
03487 
03488    ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
03489 
03490    idx = analog_get_index(ast, p, 1);
03491    if (idx < 0) {
03492       idx = ANALOG_SUB_REAL;
03493    }
03494 
03495    p->subs[idx].f.frametype = AST_FRAME_NULL;
03496    p->subs[idx].f.datalen = 0;
03497    p->subs[idx].f.samples = 0;
03498    p->subs[idx].f.mallocd = 0;
03499    p->subs[idx].f.offset = 0;
03500    p->subs[idx].f.subclass.integer = 0;
03501    p->subs[idx].f.delivery = ast_tv(0,0);
03502    p->subs[idx].f.src = "dahdi_exception";
03503    p->subs[idx].f.data.ptr = NULL;
03504 
03505    if (!p->owner) {
03506       /* If nobody owns us, absorb the event appropriately, otherwise
03507          we loop indefinitely.  This occurs when, during call waiting, the
03508          other end hangs up our channel so that it no longer exists, but we
03509          have neither FLASH'd nor ONHOOK'd to signify our desire to
03510          change to the other channel. */
03511       res = analog_get_event(p);
03512 
03513       /* Switch to real if there is one and this isn't something really silly... */
03514       if ((res != ANALOG_EVENT_RINGEROFF) && (res != ANALOG_EVENT_RINGERON) &&
03515          (res != ANALOG_EVENT_HOOKCOMPLETE)) {
03516          ast_debug(1, "Restoring owner of channel %d on event %d\n", p->channel, res);
03517          analog_set_new_owner(p, p->subs[ANALOG_SUB_REAL].owner);
03518          if (p->owner && ast != p->owner) {
03519             /*
03520              * Could this even happen?
03521              * Possible deadlock because we do not have the real-call lock.
03522              */
03523             ast_log(LOG_WARNING, "Event %s on %s is not restored owner %s\n",
03524                analog_event2str(res), ast->name, p->owner->name);
03525          }
03526          if (p->owner && ast_bridged_channel(p->owner)) {
03527             ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
03528          }
03529       }
03530       switch (res) {
03531       case ANALOG_EVENT_ONHOOK:
03532          analog_set_echocanceller(p, 0);
03533          if (p->owner) {
03534             ast_verb(3, "Channel %s still has call, ringing phone\n", p->owner->name);
03535             analog_ring(p);
03536             analog_stop_callwait(p);
03537          } else {
03538             ast_log(LOG_WARNING, "Absorbed %s, but nobody is left!?!?\n",
03539                analog_event2str(res));
03540          }
03541          analog_update_conf(p);
03542          break;
03543       case ANALOG_EVENT_RINGOFFHOOK:
03544          analog_set_echocanceller(p, 1);
03545          analog_off_hook(p);
03546          if (p->owner && (p->owner->_state == AST_STATE_RINGING)) {
03547             ast_queue_control(p->owner, AST_CONTROL_ANSWER);
03548             analog_set_dialing(p, 0);
03549          }
03550          break;
03551       case ANALOG_EVENT_HOOKCOMPLETE:
03552       case ANALOG_EVENT_RINGERON:
03553       case ANALOG_EVENT_RINGEROFF:
03554          /* Do nothing */
03555          break;
03556       case ANALOG_EVENT_WINKFLASH:
03557          gettimeofday(&p->flashtime, NULL);
03558          if (p->owner) {
03559             ast_verb(3, "Channel %d flashed to other channel %s\n", p->channel, p->owner->name);
03560             if (p->owner->_state != AST_STATE_UP) {
03561                /* Answer if necessary */
03562                ast_queue_control(p->owner, AST_CONTROL_ANSWER);
03563                ast_setstate(p->owner, AST_STATE_UP);
03564             }
03565             analog_stop_callwait(p);
03566             if (ast_bridged_channel(p->owner)) {
03567                ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
03568             }
03569          } else {
03570             ast_log(LOG_WARNING, "Absorbed %s, but nobody is left!?!?\n",
03571                analog_event2str(res));
03572          }
03573          analog_update_conf(p);
03574          break;
03575       default:
03576          ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", analog_event2str(res));
03577          break;
03578       }
03579       f = &p->subs[idx].f;
03580       return f;
03581    }
03582    ast_debug(1, "Exception on %d, channel %d\n", ast->fds[0],p->channel);
03583    /* If it's not us, return NULL immediately */
03584    if (ast != p->owner) {
03585       ast_log(LOG_WARNING, "We're %s, not %s\n", ast->name, p->owner->name);
03586       f = &p->subs[idx].f;
03587       return f;
03588    }
03589    f = __analog_handle_event(p, ast);
03590    return f;
03591 }
03592 
03593 void *analog_handle_init_event(struct analog_pvt *i, int event)
03594 {
03595    int res;
03596    pthread_t threadid;
03597    struct ast_channel *chan;
03598 
03599    ast_debug(1, "channel (%d) - signaling (%d) - event (%s)\n",
03600             i->channel, i->sig, analog_event2str(event));
03601 
03602    /* Handle an event on a given channel for the monitor thread. */
03603    switch (event) {
03604    case ANALOG_EVENT_WINKFLASH:
03605    case ANALOG_EVENT_RINGOFFHOOK:
03606       if (i->inalarm) {
03607          break;
03608       }
03609       /* Got a ring/answer.  What kind of channel are we? */
03610       switch (i->sig) {
03611       case ANALOG_SIG_FXOLS:
03612       case ANALOG_SIG_FXOGS:
03613       case ANALOG_SIG_FXOKS:
03614          res = analog_off_hook(i);
03615          i->fxsoffhookstate = 1;
03616          if (res && (errno == EBUSY)) {
03617             break;
03618          }
03619 
03620          /* Cancel VMWI spill */
03621          analog_cancel_cidspill(i);
03622 
03623          if (i->immediate) {
03624             analog_set_echocanceller(i, 1);
03625             /* The channel is immediately up.  Start right away */
03626             res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_RINGTONE);
03627             chan = analog_new_ast_channel(i, AST_STATE_RING, 1, ANALOG_SUB_REAL, NULL);
03628             if (!chan) {
03629                ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
03630                res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
03631                if (res < 0) {
03632                   ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
03633                }
03634             }
03635          } else {
03636             /* Check for callerid, digits, etc */
03637             chan = analog_new_ast_channel(i, AST_STATE_RESERVED, 0, ANALOG_SUB_REAL, NULL);
03638             i->ss_astchan = chan;
03639             if (chan) {
03640                if (analog_has_voicemail(i)) {
03641                   res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_STUTTER);
03642                } else {
03643                   res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_DIALTONE);
03644                }
03645                if (res < 0)
03646                   ast_log(LOG_WARNING, "Unable to play dialtone on channel %d, do you have defaultzone and loadzone defined?\n", i->channel);
03647 
03648                if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
03649                   ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
03650                   res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
03651                   if (res < 0) {
03652                      ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
03653                   }
03654                   ast_hangup(chan);
03655                }
03656             } else
03657                ast_log(LOG_WARNING, "Unable to create channel\n");
03658          }
03659          break;
03660       case ANALOG_SIG_FXSLS:
03661       case ANALOG_SIG_FXSGS:
03662       case ANALOG_SIG_FXSKS:
03663          analog_set_ringtimeout(i, i->ringt_base);
03664          /* Fall through */
03665       case ANALOG_SIG_EMWINK:
03666       case ANALOG_SIG_FEATD:
03667       case ANALOG_SIG_FEATDMF:
03668       case ANALOG_SIG_FEATDMF_TA:
03669       case ANALOG_SIG_E911:
03670       case ANALOG_SIG_FGC_CAMA:
03671       case ANALOG_SIG_FGC_CAMAMF:
03672       case ANALOG_SIG_FEATB:
03673       case ANALOG_SIG_EM:
03674       case ANALOG_SIG_EM_E1:
03675       case ANALOG_SIG_SFWINK:
03676       case ANALOG_SIG_SF_FEATD:
03677       case ANALOG_SIG_SF_FEATDMF:
03678       case ANALOG_SIG_SF_FEATB:
03679       case ANALOG_SIG_SF:
03680          /* Check for callerid, digits, etc */
03681          if (i->cid_start == ANALOG_CID_START_POLARITY_IN || i->cid_start == ANALOG_CID_START_DTMF_NOALERT) {
03682             chan = analog_new_ast_channel(i, AST_STATE_PRERING, 0, ANALOG_SUB_REAL, NULL);
03683          } else {
03684             chan = analog_new_ast_channel(i, AST_STATE_RING, 0, ANALOG_SUB_REAL, NULL);
03685          }
03686          i->ss_astchan = chan;
03687          if (!chan) {
03688             ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
03689          } else if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
03690             ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
03691             res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
03692             if (res < 0) {
03693                ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
03694             }
03695             ast_hangup(chan);
03696          }
03697          break;
03698       default:
03699          ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", analog_sigtype_to_str(i->sig), i->channel);
03700          res = analog_play_tone(i, ANALOG_SUB_REAL, ANALOG_TONE_CONGESTION);
03701          if (res < 0) {
03702             ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
03703          }
03704          return NULL;
03705       }
03706       break;
03707    case ANALOG_EVENT_NOALARM:
03708       analog_set_alarm(i, 0);
03709       ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", i->channel);
03710       manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
03711          "Channel: %d\r\n", i->channel);
03712       break;
03713    case ANALOG_EVENT_ALARM:
03714       analog_set_alarm(i, 1);
03715       analog_get_and_handle_alarms(i);
03716       /* fall thru intentionally */
03717    case ANALOG_EVENT_ONHOOK:
03718       /* Back on hook.  Hang up. */
03719       switch (i->sig) {
03720       case ANALOG_SIG_FXOLS:
03721       case ANALOG_SIG_FXOGS:
03722          i->fxsoffhookstate = 0;
03723          analog_start_polarityswitch(i);
03724          /* Fall through */
03725       case ANALOG_SIG_FEATD:
03726       case ANALOG_SIG_FEATDMF:
03727       case ANALOG_SIG_FEATDMF_TA:
03728       case ANALOG_SIG_E911:
03729       case ANALOG_SIG_FGC_CAMA:
03730       case ANALOG_SIG_FGC_CAMAMF:
03731       case ANALOG_SIG_FEATB:
03732       case ANALOG_SIG_EM:
03733       case ANALOG_SIG_EM_E1:
03734       case ANALOG_SIG_EMWINK:
03735       case ANALOG_SIG_SF_FEATD:
03736       case ANALOG_SIG_SF_FEATDMF:
03737       case ANALOG_SIG_SF_FEATB:
03738       case ANALOG_SIG_SF:
03739       case ANALOG_SIG_SFWINK:
03740       case ANALOG_SIG_FXSLS:
03741       case ANALOG_SIG_FXSGS:
03742       case ANALOG_SIG_FXSKS:
03743          analog_set_echocanceller(i, 0);
03744          res = analog_play_tone(i, ANALOG_SUB_REAL, -1);
03745          analog_on_hook(i);
03746          break;
03747       case ANALOG_SIG_FXOKS:
03748          i->fxsoffhookstate = 0;
03749          analog_start_polarityswitch(i);
03750          analog_set_echocanceller(i, 0);
03751          /* Diddle the battery for the zhone */
03752 #ifdef ZHONE_HACK
03753          analog_off_hook(i);
03754          usleep(1);
03755 #endif
03756          res = analog_play_tone(i, ANALOG_SUB_REAL, -1);
03757          analog_on_hook(i);
03758          break;
03759       default:
03760          ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", analog_sigtype_to_str(i->sig), i->channel);
03761          res = analog_play_tone(i, ANALOG_SUB_REAL, -1);
03762          return NULL;
03763       }
03764       break;
03765    case ANALOG_EVENT_POLARITY:
03766       switch (i->sig) {
03767       case ANALOG_SIG_FXSLS:
03768       case ANALOG_SIG_FXSKS:
03769       case ANALOG_SIG_FXSGS:
03770          /* We have already got a PR before the channel was
03771             created, but it wasn't handled. We need polarity
03772             to be REV for remote hangup detection to work.
03773             At least in Spain */
03774          if (i->hanguponpolarityswitch) {
03775             i->polarity = POLARITY_REV;
03776          }
03777          if (i->cid_start == ANALOG_CID_START_POLARITY || i->cid_start == ANALOG_CID_START_POLARITY_IN) {
03778             i->polarity = POLARITY_REV;
03779             ast_verb(2, "Starting post polarity "
03780                "CID detection on channel %d\n",
03781                i->channel);
03782             chan = analog_new_ast_channel(i, AST_STATE_PRERING, 0, ANALOG_SUB_REAL, NULL);
03783             i->ss_astchan = chan;
03784             if (!chan) {
03785                ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
03786             } else if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
03787                ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
03788             }
03789          }
03790          break;
03791       default:
03792          ast_log(LOG_WARNING, "handle_init_event detected "
03793             "polarity reversal on non-FXO (ANALOG_SIG_FXS) "
03794             "interface %d\n", i->channel);
03795          break;
03796       }
03797       break;
03798    case ANALOG_EVENT_DTMFCID:
03799       switch (i->sig) {
03800       case ANALOG_SIG_FXSLS:
03801       case ANALOG_SIG_FXSKS:
03802       case ANALOG_SIG_FXSGS:
03803          if (i->cid_start == ANALOG_CID_START_DTMF_NOALERT) {
03804             ast_verb(2, "Starting DTMF CID detection on channel %d\n",
03805                i->channel);
03806             chan = analog_new_ast_channel(i, AST_STATE_PRERING, 0, ANALOG_SUB_REAL, NULL);
03807             i->ss_astchan = chan;
03808             if (!chan) {
03809                ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
03810             } else if (ast_pthread_create_detached(&threadid, NULL, __analog_ss_thread, i)) {
03811                ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
03812             }
03813          }
03814          break;
03815       default:
03816          ast_log(LOG_WARNING, "handle_init_event detected "
03817             "dtmfcid generation event on non-FXO (ANALOG_SIG_FXS) "
03818             "interface %d\n", i->channel);
03819          break;
03820       }
03821       break;
03822    case ANALOG_EVENT_REMOVED: /* destroy channel, will actually do so in do_monitor */
03823       ast_log(LOG_NOTICE, "Got ANALOG_EVENT_REMOVED. Destroying channel %d\n",
03824          i->channel);
03825       return i->chan_pvt;
03826    case ANALOG_EVENT_NEONMWI_ACTIVE:
03827       analog_handle_notify_message(NULL, i, -1, ANALOG_EVENT_NEONMWI_ACTIVE);
03828       break;
03829    case ANALOG_EVENT_NEONMWI_INACTIVE:
03830       analog_handle_notify_message(NULL, i, -1, ANALOG_EVENT_NEONMWI_INACTIVE);
03831       break;
03832    }
03833    return NULL;
03834 }
03835 
03836 
03837 struct analog_pvt *analog_new(enum analog_sigtype signallingtype, struct analog_callback *c, void *private_data)
03838 {
03839    struct analog_pvt *p;
03840 
03841    p = ast_calloc(1, sizeof(*p));
03842    if (!p) {
03843       return p;
03844    }
03845 
03846    p->calls = c;
03847    p->outsigmod = ANALOG_SIG_NONE;
03848    p->sig = signallingtype;
03849    p->chan_pvt = private_data;
03850 
03851    /* Some defaults for values */
03852    p->cid_start = ANALOG_CID_START_RING;
03853    p->cid_signalling = CID_SIG_BELL;
03854    /* Sub real is assumed to always be alloc'd */
03855    p->subs[ANALOG_SUB_REAL].allocd = 1;
03856 
03857    return p;
03858 }
03859 
03860 /*!
03861  * \brief Delete the analog private structure.
03862  * \since 1.8
03863  *
03864  * \param doomed Analog private structure to delete.
03865  *
03866  * \return Nothing
03867  */
03868 void analog_delete(struct analog_pvt *doomed)
03869 {
03870    ast_free(doomed);
03871 }
03872 
03873 int analog_config_complete(struct analog_pvt *p)
03874 {
03875    /* No call waiting on non FXS channels */
03876    if ((p->sig != ANALOG_SIG_FXOKS) && (p->sig != ANALOG_SIG_FXOLS) && (p->sig != ANALOG_SIG_FXOGS)) {
03877       p->permcallwaiting = 0;
03878    }
03879 
03880    analog_set_callwaiting(p, p->permcallwaiting);
03881 
03882    return 0;
03883 }
03884 
03885 void analog_free(struct analog_pvt *p)
03886 {
03887    ast_free(p);
03888 }
03889 
03890 /* called while dahdi_pvt is locked in dahdi_fixup */
03891 int analog_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, void *newp)
03892 {
03893    struct analog_pvt *new_pvt = newp;
03894    int x;
03895    ast_debug(1, "New owner for channel %d is %s\n", new_pvt->channel, newchan->name);
03896    if (new_pvt->owner == oldchan) {
03897       analog_set_new_owner(new_pvt, newchan);
03898    }
03899    for (x = 0; x < 3; x++) {
03900       if (new_pvt->subs[x].owner == oldchan) {
03901          new_pvt->subs[x].owner = newchan;
03902       }
03903    }
03904 
03905    analog_update_conf(new_pvt);
03906    return 0;
03907 }
03908 
03909 int analog_dnd(struct analog_pvt *p, int flag)
03910 {
03911    if (flag == -1) {
03912       return p->dnd;
03913    }
03914 
03915    p->dnd = flag;
03916 
03917    ast_verb(3, "%s DND on channel %d\n",
03918          flag ? "Enabled" : "Disabled",
03919          p->channel);
03920    manager_event(EVENT_FLAG_SYSTEM, "DNDState",
03921          "Channel: DAHDI/%d\r\n"
03922          "Status: %s\r\n", p->channel,
03923          flag ? "enabled" : "disabled");
03924 
03925    return 0;
03926 }