Sat Mar 26 11:08:16 2011

Asterisk developer's documentation


sig_analog.c

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

Generated on Sat Mar 26 11:08:17 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.9