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

Generated on Thu Apr 16 06:27:54 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6