Mon Jun 18 2012 03:55:41

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