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