Sun Oct 16 2011 08:41:48

Asterisk developer's documentation


sig_ss7.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2010 Digium, Inc.
00005  *
00006  * Richard Mudgett <rmudgett@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*!
00020  * \file
00021  * \brief SS7 signaling module.
00022  *
00023  * \author Matthew Fredrickson <creslin@digium.com>
00024  * \author Richard Mudgett <rmudgett@digium.com>
00025  *
00026  * See Also:
00027  * \arg \ref AstCREDITS
00028  */
00029 
00030 
00031 #include "asterisk.h"
00032 
00033 #if defined(HAVE_SS7)
00034 
00035 #include <signal.h>
00036 
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/causes.h"
00039 #include "asterisk/musiconhold.h"
00040 #include "asterisk/transcap.h"
00041 
00042 #include "sig_ss7.h"
00043 
00044 /* ------------------------------------------------------------------- */
00045 
00046 #define SIG_SS7_DEADLOCK_AVOIDANCE(p) \
00047    do { \
00048       sig_ss7_unlock_private(p); \
00049       usleep(1); \
00050       sig_ss7_lock_private(p); \
00051    } while (0)
00052 
00053 static void sig_ss7_unlock_private(struct sig_ss7_chan *p)
00054 {
00055    if (p->calls->unlock_private) {
00056       p->calls->unlock_private(p->chan_pvt);
00057    }
00058 }
00059 
00060 static void sig_ss7_lock_private(struct sig_ss7_chan *p)
00061 {
00062    if (p->calls->lock_private) {
00063       p->calls->lock_private(p->chan_pvt);
00064    }
00065 }
00066 
00067 void sig_ss7_set_alarm(struct sig_ss7_chan *p, int in_alarm)
00068 {
00069    p->inalarm = in_alarm;
00070    if (p->calls->set_alarm) {
00071       p->calls->set_alarm(p->chan_pvt, in_alarm);
00072    }
00073 }
00074 
00075 static void sig_ss7_set_dialing(struct sig_ss7_chan *p, int is_dialing)
00076 {
00077    if (p->calls->set_dialing) {
00078       p->calls->set_dialing(p->chan_pvt, is_dialing);
00079    }
00080 }
00081 
00082 static void sig_ss7_set_digital(struct sig_ss7_chan *p, int is_digital)
00083 {
00084    if (p->calls->set_digital) {
00085       p->calls->set_digital(p->chan_pvt, is_digital);
00086    }
00087 }
00088 
00089 static void sig_ss7_set_inservice(struct sig_ss7_chan *p, int is_inservice)
00090 {
00091    if (p->calls->set_inservice) {
00092       p->calls->set_inservice(p->chan_pvt, is_inservice);
00093    }
00094 }
00095 
00096 static void sig_ss7_set_locallyblocked(struct sig_ss7_chan *p, int is_blocked)
00097 {
00098    p->locallyblocked = is_blocked;
00099    if (p->calls->set_locallyblocked) {
00100       p->calls->set_locallyblocked(p->chan_pvt, is_blocked);
00101    }
00102 }
00103 
00104 static void sig_ss7_set_remotelyblocked(struct sig_ss7_chan *p, int is_blocked)
00105 {
00106    p->remotelyblocked = is_blocked;
00107    if (p->calls->set_remotelyblocked) {
00108       p->calls->set_remotelyblocked(p->chan_pvt, is_blocked);
00109    }
00110 }
00111 
00112 /*!
00113  * \internal
00114  * \brief Set the caller id information in the parent module.
00115  * \since 1.8
00116  *
00117  * \param p sig_ss7 channel structure.
00118  *
00119  * \return Nothing
00120  */
00121 static void sig_ss7_set_caller_id(struct sig_ss7_chan *p)
00122 {
00123    struct ast_party_caller caller;
00124 
00125    if (p->calls->set_callerid) {
00126       ast_party_caller_init(&caller);
00127 
00128       caller.id.name.str = p->cid_name;
00129       caller.id.name.presentation = p->callingpres;
00130       caller.id.name.valid = 1;
00131 
00132       caller.id.number.str = p->cid_num;
00133       caller.id.number.plan = p->cid_ton;
00134       caller.id.number.presentation = p->callingpres;
00135       caller.id.number.valid = 1;
00136 
00137       if (!ast_strlen_zero(p->cid_subaddr)) {
00138          caller.id.subaddress.valid = 1;
00139          //caller.id.subaddress.type = 0;/* nsap */
00140          //caller.id.subaddress.odd_even_indicator = 0;
00141          caller.id.subaddress.str = p->cid_subaddr;
00142       }
00143 
00144       caller.ani.number.str = p->cid_ani;
00145       //caller.ani.number.plan = p->xxx;
00146       //caller.ani.number.presentation = p->xxx;
00147       caller.ani.number.valid = 1;
00148 
00149       caller.ani2 = p->cid_ani2;
00150       p->calls->set_callerid(p->chan_pvt, &caller);
00151    }
00152 }
00153 
00154 /*!
00155  * \internal
00156  * \brief Set the Dialed Number Identifier.
00157  * \since 1.8
00158  *
00159  * \param p sig_ss7 channel structure.
00160  * \param dnid Dialed Number Identifier string.
00161  *
00162  * \return Nothing
00163  */
00164 static void sig_ss7_set_dnid(struct sig_ss7_chan *p, const char *dnid)
00165 {
00166    if (p->calls->set_dnid) {
00167       p->calls->set_dnid(p->chan_pvt, dnid);
00168    }
00169 }
00170 
00171 static int sig_ss7_play_tone(struct sig_ss7_chan *p, enum sig_ss7_tone tone)
00172 {
00173    int res;
00174 
00175    if (p->calls->play_tone) {
00176       res = p->calls->play_tone(p->chan_pvt, tone);
00177    } else {
00178       res = -1;
00179    }
00180    return res;
00181 }
00182 
00183 static int sig_ss7_set_echocanceller(struct sig_ss7_chan *p, int enable)
00184 {
00185    if (p->calls->set_echocanceller) {
00186       return p->calls->set_echocanceller(p->chan_pvt, enable);
00187    }
00188    return -1;
00189 }
00190 
00191 static void sig_ss7_loopback(struct sig_ss7_chan *p, int enable)
00192 {
00193    if (p->loopedback != enable) {
00194       p->loopedback = enable;
00195       if (p->calls->set_loopback) {
00196          p->calls->set_loopback(p->chan_pvt, enable);
00197       }
00198    }
00199 }
00200 
00201 static struct ast_channel *sig_ss7_new_ast_channel(struct sig_ss7_chan *p, int state, int ulaw, int transfercapability, char *exten, const struct ast_channel *requestor)
00202 {
00203    struct ast_channel *ast;
00204 
00205    if (p->calls->new_ast_channel) {
00206       ast = p->calls->new_ast_channel(p->chan_pvt, state, ulaw, exten, requestor);
00207    } else {
00208       return NULL;
00209    }
00210 
00211    if (!p->owner) {
00212       p->owner = ast;
00213    }
00214    p->alreadyhungup = 0;
00215    ast->transfercapability = transfercapability;
00216    pbx_builtin_setvar_helper(ast, "TRANSFERCAPABILITY",
00217       ast_transfercapability2str(transfercapability));
00218    if (transfercapability & AST_TRANS_CAP_DIGITAL) {
00219       sig_ss7_set_digital(p, 1);
00220    }
00221 
00222    return ast;
00223 }
00224 
00225 static void sig_ss7_handle_link_exception(struct sig_ss7_linkset *linkset, int which)
00226 {
00227    if (linkset->calls->handle_link_exception) {
00228       linkset->calls->handle_link_exception(linkset, which);
00229    }
00230 }
00231 
00232 /*!
00233  * \internal
00234  * \brief Obtain the sig_ss7 owner channel lock if the owner exists.
00235  * \since 1.8
00236  *
00237  * \param ss7 sig_ss7 SS7 control structure.
00238  * \param chanpos Channel position in the span.
00239  *
00240  * \note Assumes the ss7->lock is already obtained.
00241  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
00242  *
00243  * \return Nothing
00244  */
00245 static void sig_ss7_lock_owner(struct sig_ss7_linkset *ss7, int chanpos)
00246 {
00247    for (;;) {
00248       if (!ss7->pvts[chanpos]->owner) {
00249          /* There is no owner lock to get. */
00250          break;
00251       }
00252       if (!ast_channel_trylock(ss7->pvts[chanpos]->owner)) {
00253          /* We got the lock */
00254          break;
00255       }
00256       /* We must unlock the SS7 to avoid the possibility of a deadlock */
00257       ast_mutex_unlock(&ss7->lock);
00258       SIG_SS7_DEADLOCK_AVOIDANCE(ss7->pvts[chanpos]);
00259       ast_mutex_lock(&ss7->lock);
00260    }
00261 }
00262 
00263 /*!
00264  * \internal
00265  * \brief Queue the given frame onto the owner channel.
00266  * \since 1.8
00267  *
00268  * \param ss7 sig_ss7 SS7 control structure.
00269  * \param chanpos Channel position in the span.
00270  * \param frame Frame to queue onto the owner channel.
00271  *
00272  * \note Assumes the ss7->lock is already obtained.
00273  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
00274  *
00275  * \return Nothing
00276  */
00277 static void sig_ss7_queue_frame(struct sig_ss7_linkset *ss7, int chanpos, struct ast_frame *frame)
00278 {
00279    sig_ss7_lock_owner(ss7, chanpos);
00280    if (ss7->pvts[chanpos]->owner) {
00281       ast_queue_frame(ss7->pvts[chanpos]->owner, frame);
00282       ast_channel_unlock(ss7->pvts[chanpos]->owner);
00283    }
00284 }
00285 
00286 /*!
00287  * \internal
00288  * \brief Queue a control frame of the specified subclass onto the owner channel.
00289  * \since 1.8
00290  *
00291  * \param ss7 sig_ss7 SS7 control structure.
00292  * \param chanpos Channel position in the span.
00293  * \param subclass Control frame subclass to queue onto the owner channel.
00294  *
00295  * \note Assumes the ss7->lock is already obtained.
00296  * \note Assumes the sig_ss7_lock_private(ss7->pvts[chanpos]) is already obtained.
00297  *
00298  * \return Nothing
00299  */
00300 static void sig_ss7_queue_control(struct sig_ss7_linkset *ss7, int chanpos, int subclass)
00301 {
00302    struct ast_frame f = {AST_FRAME_CONTROL, };
00303    struct sig_ss7_chan *p = ss7->pvts[chanpos];
00304 
00305    if (p->calls->queue_control) {
00306       p->calls->queue_control(p->chan_pvt, subclass);
00307    }
00308 
00309    f.subclass.integer = subclass;
00310    sig_ss7_queue_frame(ss7, chanpos, &f);
00311 }
00312 
00313 static int ss7_find_cic(struct sig_ss7_linkset *linkset, int cic, unsigned int dpc)
00314 {
00315    int i;
00316    int winner = -1;
00317    for (i = 0; i < linkset->numchans; i++) {
00318       if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && linkset->pvts[i]->cic == cic)) {
00319          winner = i;
00320          break;
00321       }
00322    }
00323    return winner;
00324 }
00325 
00326 static void ss7_handle_cqm(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
00327 {
00328    unsigned char status[32];
00329    struct sig_ss7_chan *p = NULL;
00330    int i, offset;
00331 
00332    for (i = 0; i < linkset->numchans; i++) {
00333       if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
00334          p = linkset->pvts[i];
00335          offset = p->cic - startcic;
00336          status[offset] = 0;
00337          if (p->locallyblocked)
00338             status[offset] |= (1 << 0) | (1 << 4);
00339          if (p->remotelyblocked)
00340             status[offset] |= (1 << 1) | (1 << 5);
00341          if (p->ss7call) {
00342             if (p->outgoing)
00343                status[offset] |= (1 << 3);
00344             else
00345                status[offset] |= (1 << 2);
00346          } else
00347             status[offset] |= 0x3 << 2;
00348       }
00349    }
00350 
00351    if (p)
00352       isup_cqr(linkset->ss7, startcic, endcic, dpc, status);
00353    else
00354       ast_log(LOG_WARNING, "Could not find any equipped circuits within CQM CICs\n");
00355 
00356 }
00357 
00358 static inline void ss7_hangup_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
00359 {
00360    int i;
00361 
00362    for (i = 0; i < linkset->numchans; i++) {
00363       if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
00364          sig_ss7_lock_private(linkset->pvts[i]);
00365          if (linkset->pvts[i]->owner)
00366             linkset->pvts[i]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
00367          sig_ss7_unlock_private(linkset->pvts[i]);
00368       }
00369    }
00370 }
00371 
00372 static inline void ss7_block_cics(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc, unsigned char state[], int block)
00373 {
00374    int i;
00375 
00376    for (i = 0; i < linkset->numchans; i++) {
00377       if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
00378          if (state) {
00379             if (state[i])
00380                sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
00381          } else
00382             sig_ss7_set_remotelyblocked(linkset->pvts[i], block);
00383       }
00384    }
00385 }
00386 
00387 static void ss7_inservice(struct sig_ss7_linkset *linkset, int startcic, int endcic, unsigned int dpc)
00388 {
00389    int i;
00390 
00391    for (i = 0; i < linkset->numchans; i++) {
00392       if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic))))
00393          sig_ss7_set_inservice(linkset->pvts[i], 1);
00394    }
00395 }
00396 
00397 static void ss7_reset_linkset(struct sig_ss7_linkset *linkset)
00398 {
00399    int i, startcic = -1, endcic, dpc;
00400 
00401    if (linkset->numchans <= 0)
00402       return;
00403 
00404    startcic = linkset->pvts[0]->cic;
00405    /* DB: CIC's DPC fix */
00406    dpc = linkset->pvts[0]->dpc;
00407 
00408    for (i = 0; i < linkset->numchans; i++) {
00409       if (linkset->pvts[i+1] && linkset->pvts[i+1]->dpc == dpc && ((linkset->pvts[i+1]->cic - linkset->pvts[i]->cic) == 1) && (linkset->pvts[i]->cic - startcic < 31)) {
00410          continue;
00411       } else {
00412          endcic = linkset->pvts[i]->cic;
00413          ast_verbose("Resetting CICs %d to %d\n", startcic, endcic);
00414          isup_grs(linkset->ss7, startcic, endcic, dpc);
00415 
00416          /* DB: CIC's DPC fix */
00417          if (linkset->pvts[i+1]) {
00418             startcic = linkset->pvts[i+1]->cic;
00419             dpc = linkset->pvts[i+1]->dpc;
00420          }
00421       }
00422    }
00423 }
00424 
00425 /* This function is assumed to be called with the private channel lock and linkset lock held */
00426 static void ss7_start_call(struct sig_ss7_chan *p, struct sig_ss7_linkset *linkset)
00427 {
00428    struct ss7 *ss7 = linkset->ss7;
00429    int law;
00430    struct ast_channel *c;
00431    char tmp[256];
00432 
00433    if (!(linkset->flags & LINKSET_FLAG_EXPLICITACM)) {
00434       p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
00435       isup_acm(ss7, p->ss7call);
00436    } else {
00437       p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
00438    }
00439 
00440    if (linkset->type == SS7_ITU) {
00441       law = SIG_SS7_ALAW;
00442    } else {
00443       law = SIG_SS7_ULAW;
00444    }
00445 
00446    /*
00447     * Release the SS7 lock while we create the channel
00448     * so other threads can send messages.
00449     */
00450    ast_mutex_unlock(&linkset->lock);
00451    c = sig_ss7_new_ast_channel(p, AST_STATE_RING, law, 0, p->exten, NULL);
00452    if (!c) {
00453       ast_log(LOG_WARNING, "Unable to start PBX on CIC %d\n", p->cic);
00454       ast_mutex_lock(&linkset->lock);
00455       isup_rel(linkset->ss7, p->ss7call, -1);
00456       p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
00457       p->alreadyhungup = 1;
00458       return;
00459    }
00460 
00461    sig_ss7_set_echocanceller(p, 1);
00462 
00463    /*
00464     * It is reasonably safe to set the following
00465     * channel variables while the channel private
00466     * structure is locked.  The PBX has not been
00467     * started yet and it is unlikely that any other task
00468     * will do anything with the channel we have just
00469     * created.
00470     *
00471     * We only reference these variables in the context of the ss7_linkset function
00472     * when receiving either and IAM or a COT message.
00473     */
00474    if (!ast_strlen_zero(p->charge_number)) {
00475       pbx_builtin_setvar_helper(c, "SS7_CHARGE_NUMBER", p->charge_number);
00476       /* Clear this after we set it */
00477       p->charge_number[0] = 0;
00478    }
00479    if (!ast_strlen_zero(p->gen_add_number)) {
00480       pbx_builtin_setvar_helper(c, "SS7_GENERIC_ADDRESS", p->gen_add_number);
00481       /* Clear this after we set it */
00482       p->gen_add_number[0] = 0;
00483    }
00484    if (!ast_strlen_zero(p->jip_number)) {
00485       pbx_builtin_setvar_helper(c, "SS7_JIP", p->jip_number);
00486       /* Clear this after we set it */
00487       p->jip_number[0] = 0;
00488    }
00489    if (!ast_strlen_zero(p->gen_dig_number)) {
00490       pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGITS", p->gen_dig_number);
00491       /* Clear this after we set it */
00492       p->gen_dig_number[0] = 0;
00493    }
00494    if (!ast_strlen_zero(p->orig_called_num)) {
00495       pbx_builtin_setvar_helper(c, "SS7_ORIG_CALLED_NUM", p->orig_called_num);
00496       /* Clear this after we set it */
00497       p->orig_called_num[0] = 0;
00498    }
00499 
00500    snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_type);
00501    pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGTYPE", tmp);
00502    /* Clear this after we set it */
00503    p->gen_dig_type = 0;
00504 
00505    snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_scheme);
00506    pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGSCHEME", tmp);
00507    /* Clear this after we set it */
00508    p->gen_dig_scheme = 0;
00509 
00510    if (!ast_strlen_zero(p->lspi_ident)) {
00511       pbx_builtin_setvar_helper(c, "SS7_LSPI_IDENT", p->lspi_ident);
00512       /* Clear this after we set it */
00513       p->lspi_ident[0] = 0;
00514    }
00515 
00516    snprintf(tmp, sizeof(tmp), "%d", p->call_ref_ident);
00517    pbx_builtin_setvar_helper(c, "SS7_CALLREF_IDENT", tmp);
00518    /* Clear this after we set it */
00519    p->call_ref_ident = 0;
00520 
00521    snprintf(tmp, sizeof(tmp), "%d", p->call_ref_pc);
00522    pbx_builtin_setvar_helper(c, "SS7_CALLREF_PC", tmp);
00523    /* Clear this after we set it */
00524    p->call_ref_pc = 0;
00525 
00526    snprintf(tmp, sizeof(tmp), "%d", p->calling_party_cat);
00527    pbx_builtin_setvar_helper(c, "SS7_CALLING_PARTY_CATEGORY", tmp);
00528    /* Clear this after we set it */
00529    p->calling_party_cat = 0;
00530 
00531    if (!ast_strlen_zero(p->redirecting_num)) {
00532       pbx_builtin_setvar_helper(c, "SS7_REDIRECTING_NUMBER", p->redirecting_num);
00533       /* Clear this after we set it */
00534       p->redirecting_num[0] = 0;
00535    }
00536    if (!ast_strlen_zero(p->generic_name)) {
00537       pbx_builtin_setvar_helper(c, "SS7_GENERIC_NAME", p->generic_name);
00538       /* Clear this after we set it */
00539       p->generic_name[0] = 0;
00540    }
00541 
00542    if (ast_pbx_start(c)) {
00543       ast_log(LOG_WARNING, "Unable to start PBX on %s (CIC %d)\n", c->name, p->cic);
00544       ast_hangup(c);
00545    } else {
00546       ast_verb(3, "Accepting call to '%s' on CIC %d\n", p->exten, p->cic);
00547    }
00548    ast_mutex_lock(&linkset->lock);
00549 }
00550 
00551 static void ss7_apply_plan_to_number(char *buf, size_t size, const struct sig_ss7_linkset *ss7, const char *number, const unsigned nai)
00552 {
00553    if (ast_strlen_zero(number)) { /* make sure a number exists so prefix isn't placed on an empty string */
00554       if (size) {
00555          *buf = '\0';
00556       }
00557       return;
00558    }
00559    switch (nai) {
00560    case SS7_NAI_INTERNATIONAL:
00561       snprintf(buf, size, "%s%s", ss7->internationalprefix, number);
00562       break;
00563    case SS7_NAI_NATIONAL:
00564       snprintf(buf, size, "%s%s", ss7->nationalprefix, number);
00565       break;
00566    case SS7_NAI_SUBSCRIBER:
00567       snprintf(buf, size, "%s%s", ss7->subscriberprefix, number);
00568       break;
00569    case SS7_NAI_UNKNOWN:
00570       snprintf(buf, size, "%s%s", ss7->unknownprefix, number);
00571       break;
00572    default:
00573       snprintf(buf, size, "%s", number);
00574       break;
00575    }
00576 }
00577 
00578 static int ss7_pres_scr2cid_pres(char presentation_ind, char screening_ind)
00579 {
00580    return ((presentation_ind & 0x3) << 5) | (screening_ind & 0x3);
00581 }
00582 
00583 /* This is a thread per linkset that handles all received events from libss7. */
00584 void *ss7_linkset(void *data)
00585 {
00586    int res, i;
00587    struct timeval *next = NULL, tv;
00588    struct sig_ss7_linkset *linkset = (struct sig_ss7_linkset *) data;
00589    struct ss7 *ss7 = linkset->ss7;
00590    ss7_event *e = NULL;
00591    struct sig_ss7_chan *p;
00592    int chanpos;
00593    struct pollfd pollers[SIG_SS7_NUM_DCHANS];
00594    int cic;
00595    unsigned int dpc;
00596    int nextms = 0;
00597 
00598    ss7_set_debug(ss7, SIG_SS7_DEBUG_DEFAULT);
00599    ss7_start(ss7);
00600 
00601    for (;;) {
00602       ast_mutex_lock(&linkset->lock);
00603       if ((next = ss7_schedule_next(ss7))) {
00604          tv = ast_tvnow();
00605          tv.tv_sec = next->tv_sec - tv.tv_sec;
00606          tv.tv_usec = next->tv_usec - tv.tv_usec;
00607          if (tv.tv_usec < 0) {
00608             tv.tv_usec += 1000000;
00609             tv.tv_sec -= 1;
00610          }
00611          if (tv.tv_sec < 0) {
00612             tv.tv_sec = 0;
00613             tv.tv_usec = 0;
00614          }
00615          nextms = tv.tv_sec * 1000;
00616          nextms += tv.tv_usec / 1000;
00617       }
00618       ast_mutex_unlock(&linkset->lock);
00619 
00620       for (i = 0; i < linkset->numsigchans; i++) {
00621          pollers[i].fd = linkset->fds[i];
00622          pollers[i].events = ss7_pollflags(ss7, linkset->fds[i]);
00623          pollers[i].revents = 0;
00624       }
00625 
00626       res = poll(pollers, linkset->numsigchans, nextms);
00627       if ((res < 0) && (errno != EINTR)) {
00628          ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
00629       } else if (!res) {
00630          ast_mutex_lock(&linkset->lock);
00631          ss7_schedule_run(ss7);
00632          ast_mutex_unlock(&linkset->lock);
00633          continue;
00634       }
00635 
00636       ast_mutex_lock(&linkset->lock);
00637       for (i = 0; i < linkset->numsigchans; i++) {
00638          if (pollers[i].revents & POLLPRI) {
00639             sig_ss7_handle_link_exception(linkset, i);
00640          }
00641          if (pollers[i].revents & POLLIN) {
00642             res = ss7_read(ss7, pollers[i].fd);
00643          }
00644          if (pollers[i].revents & POLLOUT) {
00645             res = ss7_write(ss7, pollers[i].fd);
00646             if (res < 0) {
00647                ast_debug(1, "Error in write %s\n", strerror(errno));
00648             }
00649          }
00650       }
00651 
00652       while ((e = ss7_check_event(ss7))) {
00653          switch (e->e) {
00654          case SS7_EVENT_UP:
00655             if (linkset->state != LINKSET_STATE_UP) {
00656                ast_verbose("--- SS7 Up ---\n");
00657                ss7_reset_linkset(linkset);
00658             }
00659             linkset->state = LINKSET_STATE_UP;
00660             break;
00661          case SS7_EVENT_DOWN:
00662             ast_verbose("--- SS7 Down ---\n");
00663             linkset->state = LINKSET_STATE_DOWN;
00664             for (i = 0; i < linkset->numchans; i++) {
00665                p = linkset->pvts[i];
00666                if (p) {
00667                   sig_ss7_set_alarm(p, 1);
00668                }
00669             }
00670             break;
00671          case MTP2_LINK_UP:
00672             ast_verbose("MTP2 link up (SLC %d)\n", e->gen.data);
00673             break;
00674          case MTP2_LINK_DOWN:
00675             ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
00676             break;
00677          case ISUP_EVENT_CPG:
00678             chanpos = ss7_find_cic(linkset, e->cpg.cic, e->cpg.opc);
00679             if (chanpos < 0) {
00680                ast_log(LOG_WARNING, "CPG on unconfigured CIC %d\n", e->cpg.cic);
00681                break;
00682             }
00683             p = linkset->pvts[chanpos];
00684             sig_ss7_lock_private(p);
00685             switch (e->cpg.event) {
00686             case CPG_EVENT_ALERTING:
00687                if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
00688                   p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
00689                }
00690                sig_ss7_lock_owner(linkset, chanpos);
00691                if (p->owner) {
00692                   ast_setstate(p->owner, AST_STATE_RINGING);
00693                   ast_channel_unlock(p->owner);
00694                }
00695                sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
00696                break;
00697             case CPG_EVENT_PROGRESS:
00698             case CPG_EVENT_INBANDINFO:
00699                {
00700                   ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
00701                   sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROGRESS);
00702                   p->progress = 1;
00703                   sig_ss7_set_dialing(p, 0);
00704 #if 0 /* This code no longer seems to be necessary so I did not convert it. */
00705                   if (p->dsp && p->dsp_features) {
00706                      ast_dsp_set_features(p->dsp, p->dsp_features);
00707                      p->dsp_features = 0;
00708                   }
00709 #endif
00710                }
00711                break;
00712             default:
00713                ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
00714                break;
00715             }
00716 
00717             sig_ss7_unlock_private(p);
00718             break;
00719          case ISUP_EVENT_RSC:
00720             ast_verbose("Resetting CIC %d\n", e->rsc.cic);
00721             chanpos = ss7_find_cic(linkset, e->rsc.cic, e->rsc.opc);
00722             if (chanpos < 0) {
00723                ast_log(LOG_WARNING, "RSC on unconfigured CIC %d\n", e->rsc.cic);
00724                break;
00725             }
00726             p = linkset->pvts[chanpos];
00727             sig_ss7_lock_private(p);
00728             sig_ss7_set_inservice(p, 1);
00729             sig_ss7_set_remotelyblocked(p, 0);
00730             dpc = p->dpc;
00731             isup_set_call_dpc(e->rsc.call, dpc);
00732             p->ss7call = NULL;
00733             if (p->owner)
00734                p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
00735             sig_ss7_unlock_private(p);
00736             isup_rlc(ss7, e->rsc.call);
00737             break;
00738          case ISUP_EVENT_GRS:
00739             ast_debug(1, "Got Reset for CICs %d to %d: Acknowledging\n", e->grs.startcic, e->grs.endcic);
00740             chanpos = ss7_find_cic(linkset, e->grs.startcic, e->grs.opc);
00741             if (chanpos < 0) {
00742                ast_log(LOG_WARNING, "GRS on unconfigured CIC %d\n", e->grs.startcic);
00743                break;
00744             }
00745             p = linkset->pvts[chanpos];
00746             isup_gra(ss7, e->grs.startcic, e->grs.endcic, e->grs.opc);
00747             ss7_block_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc, NULL, 0);
00748             ss7_hangup_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc);
00749             break;
00750          case ISUP_EVENT_CQM:
00751             ast_debug(1, "Got Circuit group query message from CICs %d to %d\n", e->cqm.startcic, e->cqm.endcic);
00752             ss7_handle_cqm(linkset, e->cqm.startcic, e->cqm.endcic, e->cqm.opc);
00753             break;
00754          case ISUP_EVENT_GRA:
00755             ast_verbose("Got reset acknowledgement from CIC %d to %d.\n", e->gra.startcic, e->gra.endcic);
00756             ss7_inservice(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc);
00757             ss7_block_cics(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc, e->gra.status, 1);
00758             break;
00759          case ISUP_EVENT_IAM:
00760             ast_debug(1, "Got IAM for CIC %d and called number %s, calling number %s\n", e->iam.cic, e->iam.called_party_num, e->iam.calling_party_num);
00761             chanpos = ss7_find_cic(linkset, e->iam.cic, e->iam.opc);
00762             if (chanpos < 0) {
00763                ast_log(LOG_WARNING, "IAM on unconfigured CIC %d\n", e->iam.cic);
00764                isup_rel(ss7, e->iam.call, -1);
00765                break;
00766             }
00767             p = linkset->pvts[chanpos];
00768             sig_ss7_lock_private(p);
00769             if (p->owner) {
00770                if (p->ss7call == e->iam.call) {
00771                   sig_ss7_unlock_private(p);
00772                   ast_log(LOG_WARNING, "Duplicate IAM requested on CIC %d\n", e->iam.cic);
00773                   break;
00774                } else {
00775                   sig_ss7_unlock_private(p);
00776                   ast_log(LOG_WARNING, "Ring requested on CIC %d already in use!\n", e->iam.cic);
00777                   break;
00778                }
00779             }
00780 
00781             dpc = p->dpc;
00782             p->ss7call = e->iam.call;
00783             isup_set_call_dpc(p->ss7call, dpc);
00784 
00785             if ((p->use_callerid) && (!ast_strlen_zero(e->iam.calling_party_num))) {
00786                ss7_apply_plan_to_number(p->cid_num, sizeof(p->cid_num), linkset, e->iam.calling_party_num, e->iam.calling_nai);
00787                p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
00788             } else
00789                p->cid_num[0] = 0;
00790 
00791             /* Set DNID */
00792             if (!ast_strlen_zero(e->iam.called_party_num)) {
00793                ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset,
00794                   e->iam.called_party_num, e->iam.called_nai);
00795                sig_ss7_set_dnid(p, p->exten);
00796             }
00797 
00798             if (p->immediate) {
00799                p->exten[0] = 's';
00800                p->exten[1] = '\0';
00801             } else if (!ast_strlen_zero(e->iam.called_party_num)) {
00802                char *st;
00803                ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset, e->iam.called_party_num, e->iam.called_nai);
00804                st = strchr(p->exten, '#');
00805                if (st) {
00806                   *st = '\0';
00807                }
00808             } else {
00809                p->exten[0] = '\0';
00810             }
00811 
00812             p->cid_ani[0] = '\0';
00813             if ((p->use_callerid) && (!ast_strlen_zero(e->iam.generic_name)))
00814                ast_copy_string(p->cid_name, e->iam.generic_name, sizeof(p->cid_name));
00815             else
00816                p->cid_name[0] = '\0';
00817 
00818             p->cid_ani2 = e->iam.oli_ani2;
00819             p->cid_ton = 0;
00820             ast_copy_string(p->charge_number, e->iam.charge_number, sizeof(p->charge_number));
00821             ast_copy_string(p->gen_add_number, e->iam.gen_add_number, sizeof(p->gen_add_number));
00822             p->gen_add_type = e->iam.gen_add_type;
00823             p->gen_add_nai = e->iam.gen_add_nai;
00824             p->gen_add_pres_ind = e->iam.gen_add_pres_ind;
00825             p->gen_add_num_plan = e->iam.gen_add_num_plan;
00826             ast_copy_string(p->gen_dig_number, e->iam.gen_dig_number, sizeof(p->gen_dig_number));
00827             p->gen_dig_type = e->iam.gen_dig_type;
00828             p->gen_dig_scheme = e->iam.gen_dig_scheme;
00829             ast_copy_string(p->jip_number, e->iam.jip_number, sizeof(p->jip_number));
00830             ast_copy_string(p->orig_called_num, e->iam.orig_called_num, sizeof(p->orig_called_num));
00831             ast_copy_string(p->redirecting_num, e->iam.redirecting_num, sizeof(p->redirecting_num));
00832             ast_copy_string(p->generic_name, e->iam.generic_name, sizeof(p->generic_name));
00833             p->calling_party_cat = e->iam.calling_party_cat;
00834 
00835             sig_ss7_set_caller_id(p);
00836 
00837             if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
00838                if (e->iam.cot_check_required) {
00839                   sig_ss7_loopback(p, 1);
00840                } else
00841                   ss7_start_call(p, linkset);
00842             } else {
00843                ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
00844                p->alreadyhungup = 1;
00845                isup_rel(ss7, e->iam.call, AST_CAUSE_UNALLOCATED);
00846             }
00847             sig_ss7_unlock_private(p);
00848             break;
00849          case ISUP_EVENT_COT:
00850             chanpos = ss7_find_cic(linkset, e->cot.cic, e->cot.opc);
00851             if (chanpos < 0) {
00852                ast_log(LOG_WARNING, "COT on unconfigured CIC %d\n", e->cot.cic);
00853                isup_rel(ss7, e->cot.call, -1);
00854                break;
00855             }
00856             p = linkset->pvts[chanpos];
00857 
00858             sig_ss7_lock_private(p);
00859             if (p->loopedback) {
00860                sig_ss7_loopback(p, 0);
00861                ss7_start_call(p, linkset);
00862             }
00863             sig_ss7_unlock_private(p);
00864             break;
00865          case ISUP_EVENT_CCR:
00866             ast_debug(1, "Got CCR request on CIC %d\n", e->ccr.cic);
00867             chanpos = ss7_find_cic(linkset, e->ccr.cic, e->ccr.opc);
00868             if (chanpos < 0) {
00869                ast_log(LOG_WARNING, "CCR on unconfigured CIC %d\n", e->ccr.cic);
00870                break;
00871             }
00872 
00873             p = linkset->pvts[chanpos];
00874 
00875             sig_ss7_lock_private(p);
00876             sig_ss7_loopback(p, 1);
00877             sig_ss7_unlock_private(p);
00878 
00879             isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
00880             break;
00881          case ISUP_EVENT_CVT:
00882             ast_debug(1, "Got CVT request on CIC %d\n", e->cvt.cic);
00883             chanpos = ss7_find_cic(linkset, e->cvt.cic, e->cvt.opc);
00884             if (chanpos < 0) {
00885                ast_log(LOG_WARNING, "CVT on unconfigured CIC %d\n", e->cvt.cic);
00886                break;
00887             }
00888 
00889             p = linkset->pvts[chanpos];
00890 
00891             sig_ss7_lock_private(p);
00892             sig_ss7_loopback(p, 1);
00893             sig_ss7_unlock_private(p);
00894 
00895             isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
00896             break;
00897          case ISUP_EVENT_REL:
00898             chanpos = ss7_find_cic(linkset, e->rel.cic, e->rel.opc);
00899             if (chanpos < 0) {
00900                ast_log(LOG_WARNING, "REL on unconfigured CIC %d\n", e->rel.cic);
00901                break;
00902             }
00903             p = linkset->pvts[chanpos];
00904             sig_ss7_lock_private(p);
00905             if (p->owner) {
00906                p->owner->hangupcause = e->rel.cause;
00907                p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
00908             } else {
00909                ast_log(LOG_WARNING, "REL on channel (CIC %d) without owner!\n", p->cic);
00910             }
00911 
00912             /* End the loopback if we have one */
00913             sig_ss7_loopback(p, 0);
00914 
00915             isup_rlc(ss7, e->rel.call);
00916             p->ss7call = NULL;
00917 
00918             sig_ss7_unlock_private(p);
00919             break;
00920          case ISUP_EVENT_ACM:
00921             chanpos = ss7_find_cic(linkset, e->acm.cic, e->acm.opc);
00922             if (chanpos < 0) {
00923                ast_log(LOG_WARNING, "ACM on unconfigured CIC %d\n", e->acm.cic);
00924                isup_rel(ss7, e->acm.call, -1);
00925                break;
00926             } else {
00927                p = linkset->pvts[chanpos];
00928 
00929                ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
00930 
00931                if (e->acm.call_ref_ident > 0) {
00932                   p->rlt = 1; /* Setting it but not using it here*/
00933                }
00934 
00935                sig_ss7_lock_private(p);
00936                sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_PROCEEDING);
00937                if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING) {
00938                   p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
00939                }
00940                sig_ss7_set_dialing(p, 0);
00941                /* Send alerting if subscriber is free */
00942                if (e->acm.called_party_status_ind == 1) {
00943                   if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING) {
00944                      p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
00945                   }
00946                   sig_ss7_lock_owner(linkset, chanpos);
00947                   if (p->owner) {
00948                      ast_setstate(p->owner, AST_STATE_RINGING);
00949                      ast_channel_unlock(p->owner);
00950                   }
00951                   sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_RINGING);
00952                }
00953                sig_ss7_unlock_private(p);
00954             }
00955             break;
00956          case ISUP_EVENT_CGB:
00957             chanpos = ss7_find_cic(linkset, e->cgb.startcic, e->cgb.opc);
00958             if (chanpos < 0) {
00959                ast_log(LOG_WARNING, "CGB on unconfigured CIC %d\n", e->cgb.startcic);
00960                break;
00961             }
00962             p = linkset->pvts[chanpos];
00963             ss7_block_cics(linkset, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, 1);
00964             isup_cgba(linkset->ss7, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, e->cgb.type);
00965             break;
00966          case ISUP_EVENT_CGU:
00967             chanpos = ss7_find_cic(linkset, e->cgu.startcic, e->cgu.opc);
00968             if (chanpos < 0) {
00969                ast_log(LOG_WARNING, "CGU on unconfigured CIC %d\n", e->cgu.startcic);
00970                break;
00971             }
00972             p = linkset->pvts[chanpos];
00973             ss7_block_cics(linkset, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, 0);
00974             isup_cgua(linkset->ss7, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, e->cgu.type);
00975             break;
00976          case ISUP_EVENT_UCIC:
00977             chanpos = ss7_find_cic(linkset, e->ucic.cic, e->ucic.opc);
00978             if (chanpos < 0) {
00979                ast_log(LOG_WARNING, "UCIC on unconfigured CIC %d\n", e->ucic.cic);
00980                break;
00981             }
00982             p = linkset->pvts[chanpos];
00983             ast_debug(1, "Unequiped Circuit Id Code on CIC %d\n", e->ucic.cic);
00984             sig_ss7_lock_private(p);
00985             sig_ss7_set_remotelyblocked(p, 1);
00986             sig_ss7_set_inservice(p, 0);
00987             sig_ss7_unlock_private(p);/* doesn't require a SS7 acknowledgement */
00988             break;
00989          case ISUP_EVENT_BLO:
00990             chanpos = ss7_find_cic(linkset, e->blo.cic, e->blo.opc);
00991             if (chanpos < 0) {
00992                ast_log(LOG_WARNING, "BLO on unconfigured CIC %d\n", e->blo.cic);
00993                break;
00994             }
00995             p = linkset->pvts[chanpos];
00996             ast_debug(1, "Blocking CIC %d\n", e->blo.cic);
00997             sig_ss7_lock_private(p);
00998             sig_ss7_set_remotelyblocked(p, 1);
00999             sig_ss7_unlock_private(p);
01000             isup_bla(linkset->ss7, e->blo.cic, p->dpc);
01001             break;
01002          case ISUP_EVENT_BLA:
01003             chanpos = ss7_find_cic(linkset, e->bla.cic, e->bla.opc);
01004             if (chanpos < 0) {
01005                ast_log(LOG_WARNING, "BLA on unconfigured CIC %d\n", e->bla.cic);
01006                break;
01007             }
01008             ast_debug(1, "Blocking CIC %d\n", e->bla.cic);
01009             p = linkset->pvts[chanpos];
01010             sig_ss7_lock_private(p);
01011             sig_ss7_set_locallyblocked(p, 1);
01012             sig_ss7_unlock_private(p);
01013             break;
01014          case ISUP_EVENT_UBL:
01015             chanpos = ss7_find_cic(linkset, e->ubl.cic, e->ubl.opc);
01016             if (chanpos < 0) {
01017                ast_log(LOG_WARNING, "UBL on unconfigured CIC %d\n", e->ubl.cic);
01018                break;
01019             }
01020             p = linkset->pvts[chanpos];
01021             ast_debug(1, "Unblocking CIC %d\n", e->ubl.cic);
01022             sig_ss7_lock_private(p);
01023             sig_ss7_set_remotelyblocked(p, 0);
01024             sig_ss7_unlock_private(p);
01025             isup_uba(linkset->ss7, e->ubl.cic, p->dpc);
01026             break;
01027          case ISUP_EVENT_UBA:
01028             chanpos = ss7_find_cic(linkset, e->uba.cic, e->uba.opc);
01029             if (chanpos < 0) {
01030                ast_log(LOG_WARNING, "UBA on unconfigured CIC %d\n", e->uba.cic);
01031                break;
01032             }
01033             p = linkset->pvts[chanpos];
01034             ast_debug(1, "Unblocking CIC %d\n", e->uba.cic);
01035             sig_ss7_lock_private(p);
01036             sig_ss7_set_locallyblocked(p, 0);
01037             sig_ss7_unlock_private(p);
01038             break;
01039          case ISUP_EVENT_CON:
01040          case ISUP_EVENT_ANM:
01041             if (e->e == ISUP_EVENT_CON)
01042                cic = e->con.cic;
01043             else
01044                cic = e->anm.cic;
01045 
01046             chanpos = ss7_find_cic(linkset, cic, (e->e == ISUP_EVENT_ANM) ? e->anm.opc : e->con.opc);
01047             if (chanpos < 0) {
01048                ast_log(LOG_WARNING, "ANM/CON on unconfigured CIC %d\n", cic);
01049                isup_rel(ss7, (e->e == ISUP_EVENT_ANM) ? e->anm.call : e->con.call, -1);
01050                break;
01051             } else {
01052                p = linkset->pvts[chanpos];
01053                sig_ss7_lock_private(p);
01054                if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
01055                   p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
01056                }
01057                sig_ss7_queue_control(linkset, chanpos, AST_CONTROL_ANSWER);
01058 #if 0 /* This code no longer seems to be necessary so I did not convert it. */
01059                if (p->dsp && p->dsp_features) {
01060                   ast_dsp_set_features(p->dsp, p->dsp_features);
01061                   p->dsp_features = 0;
01062                }
01063 #endif
01064                sig_ss7_set_echocanceller(p, 1);
01065                sig_ss7_unlock_private(p);
01066             }
01067             break;
01068          case ISUP_EVENT_RLC:
01069             chanpos = ss7_find_cic(linkset, e->rlc.cic, e->rlc.opc);
01070             if (chanpos < 0) {
01071                ast_log(LOG_WARNING, "RLC on unconfigured CIC %d\n", e->rlc.cic);
01072                break;
01073             } else {
01074                p = linkset->pvts[chanpos];
01075                sig_ss7_lock_private(p);
01076                if (p->alreadyhungup)
01077                   p->ss7call = NULL;
01078                else
01079                   ast_log(LOG_NOTICE, "Received RLC out and we haven't sent REL.  Ignoring.\n");
01080                sig_ss7_unlock_private(p);
01081                }
01082                break;
01083          case ISUP_EVENT_FAA:
01084             chanpos = ss7_find_cic(linkset, e->faa.cic, e->faa.opc);
01085             if (chanpos < 0) {
01086                ast_log(LOG_WARNING, "FAA on unconfigured CIC %d\n", e->faa.cic);
01087                break;
01088             } else {
01089                p = linkset->pvts[chanpos];
01090                ast_debug(1, "FAA received on CIC %d\n", e->faa.cic);
01091                sig_ss7_lock_private(p);
01092                if (p->alreadyhungup){
01093                   p->ss7call = NULL;
01094                   ast_log(LOG_NOTICE, "Received FAA and we haven't sent FAR.  Ignoring.\n");
01095                }
01096                sig_ss7_unlock_private(p);
01097             }
01098             break;
01099          default:
01100             ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
01101             break;
01102          }
01103       }
01104       ast_mutex_unlock(&linkset->lock);
01105    }
01106 
01107    return 0;
01108 }
01109 
01110 static inline void ss7_rel(struct sig_ss7_linkset *ss7)
01111 {
01112    ast_mutex_unlock(&ss7->lock);
01113 }
01114 
01115 static inline int ss7_grab(struct sig_ss7_chan *pvt, struct sig_ss7_linkset *ss7)
01116 {
01117    int res;
01118    /* Grab the lock first */
01119    do {
01120       res = ast_mutex_trylock(&ss7->lock);
01121       if (res) {
01122          SIG_SS7_DEADLOCK_AVOIDANCE(pvt);
01123       }
01124    } while (res);
01125    /* Then break the poll */
01126    if (ss7->master != AST_PTHREADT_NULL)
01127       pthread_kill(ss7->master, SIGURG);
01128    return 0;
01129 }
01130 
01131 /*!
01132  * \brief Notify the SS7 layer that the link is in alarm.
01133  * \since 1.8
01134  *
01135  * \param linkset Controlling linkset for the channel.
01136  * \param which Link index of the signaling channel.
01137  *
01138  * \return Nothing
01139  */
01140 void sig_ss7_link_alarm(struct sig_ss7_linkset *linkset, int which)
01141 {
01142    linkset->linkstate[which] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
01143    linkset->linkstate[which] &= ~LINKSTATE_UP;
01144    ss7_link_alarm(linkset->ss7, linkset->fds[which]);
01145 }
01146 
01147 /*!
01148  * \brief Notify the SS7 layer that the link is no longer in alarm.
01149  * \since 1.8
01150  *
01151  * \param linkset Controlling linkset for the channel.
01152  * \param which Link index of the signaling channel.
01153  *
01154  * \return Nothing
01155  */
01156 void sig_ss7_link_noalarm(struct sig_ss7_linkset *linkset, int which)
01157 {
01158    linkset->linkstate[which] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
01159    linkset->linkstate[which] |= LINKSTATE_STARTING;
01160    ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
01161 }
01162 
01163 /*!
01164  * \brief Setup and add a SS7 link channel.
01165  * \since 1.8
01166  *
01167  * \param linkset Controlling linkset for the channel.
01168  * \param which Link index of the signaling channel.
01169  * \param ss7type Switch type of the linkset
01170  * \param transport Signaling transport of channel.
01171  * \param inalarm Non-zero if the channel is in alarm.
01172  * \param networkindicator User configuration parameter.
01173  * \param pointcode User configuration parameter.
01174  * \param adjpointcode User configuration parameter.
01175  *
01176  * \retval 0 on success.
01177  * \retval -1 on error.
01178  */
01179 int sig_ss7_add_sigchan(struct sig_ss7_linkset *linkset, int which, int ss7type, int transport, int inalarm, int networkindicator, int pointcode, int adjpointcode)
01180 {
01181    if (!linkset->ss7) {
01182       linkset->type = ss7type;
01183       linkset->ss7 = ss7_new(ss7type);
01184       if (!linkset->ss7) {
01185          ast_log(LOG_ERROR, "Can't create new SS7!\n");
01186          return -1;
01187       }
01188    }
01189 
01190    ss7_set_network_ind(linkset->ss7, networkindicator);
01191    ss7_set_pc(linkset->ss7, pointcode);
01192 
01193    if (ss7_add_link(linkset->ss7, transport, linkset->fds[which])) {
01194       ast_log(LOG_WARNING, "Could not add SS7 link!\n");
01195    }
01196 
01197    if (inalarm) {
01198       linkset->linkstate[which] = LINKSTATE_DOWN | LINKSTATE_INALARM;
01199       ss7_link_alarm(linkset->ss7, linkset->fds[which]);
01200    } else {
01201       linkset->linkstate[which] = LINKSTATE_DOWN;
01202       ss7_link_noalarm(linkset->ss7, linkset->fds[which]);
01203    }
01204 
01205    ss7_set_adjpc(linkset->ss7, linkset->fds[which], adjpointcode);
01206 
01207    return 0;
01208 }
01209 
01210 /*!
01211  * \brief Determine if the specified channel is available for an outgoing call.
01212  * \since 1.8
01213  *
01214  * \param p Signaling private structure pointer.
01215  *
01216  * \retval TRUE if the channel is available.
01217  */
01218 int sig_ss7_available(struct sig_ss7_chan *p)
01219 {
01220    if (!p->ss7) {
01221       /* Something is wrong here.  A SS7 channel without the ss7 pointer? */
01222       return 0;
01223    }
01224 
01225    if (!p->inalarm && !p->owner && !p->ss7call
01226       && !p->locallyblocked && !p->remotelyblocked) {
01227       return 1;
01228    }
01229 
01230    return 0;
01231 }
01232 
01233 static unsigned char cid_pres2ss7pres(int cid_pres)
01234 {
01235     return (cid_pres >> 5) & 0x03;
01236 }
01237 
01238 static unsigned char cid_pres2ss7screen(int cid_pres)
01239 {
01240    return cid_pres & 0x03;
01241 }
01242 
01243 /*!
01244  * \brief Dial out using the specified SS7 channel.
01245  * \since 1.8
01246  *
01247  * \param p Signaling private structure pointer.
01248  * \param ast Asterisk channel structure pointer.
01249  * \param rdest Dialstring.
01250  *
01251  * \retval 0 on success.
01252  * \retval -1 on error.
01253  */
01254 int sig_ss7_call(struct sig_ss7_chan *p, struct ast_channel *ast, char *rdest)
01255 {
01256    char ss7_called_nai;
01257    int called_nai_strip;
01258    char ss7_calling_nai;
01259    int calling_nai_strip;
01260    const char *charge_str = NULL;
01261    const char *gen_address = NULL;
01262    const char *gen_digits = NULL;
01263    const char *gen_dig_type = NULL;
01264    const char *gen_dig_scheme = NULL;
01265    const char *gen_name = NULL;
01266    const char *jip_digits = NULL;
01267    const char *lspi_ident = NULL;
01268    const char *rlt_flag = NULL;
01269    const char *call_ref_id = NULL;
01270    const char *call_ref_pc = NULL;
01271    const char *send_far = NULL;
01272    char *c;
01273    char *l;
01274    char dest[256];
01275 
01276    ast_copy_string(dest, rdest, sizeof(dest));
01277 
01278    c = strchr(dest, '/');
01279    if (c) {
01280       c++;
01281    } else {
01282       c = "";
01283    }
01284    if (strlen(c) < p->stripmsd) {
01285       ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01286       return -1;
01287    }
01288 
01289    if (!p->hidecallerid) {
01290       l = ast->connected.id.number.valid ? ast->connected.id.number.str : NULL;
01291    } else {
01292       l = NULL;
01293    }
01294 
01295    if (ss7_grab(p, p->ss7)) {
01296       ast_log(LOG_WARNING, "Failed to grab SS7!\n");
01297       return -1;
01298    }
01299 
01300    p->ss7call = isup_new_call(p->ss7->ss7);
01301    if (!p->ss7call) {
01302       ss7_rel(p->ss7);
01303       ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
01304       return -1;
01305    }
01306 
01307    called_nai_strip = 0;
01308    ss7_called_nai = p->ss7->called_nai;
01309    if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
01310       if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
01311          called_nai_strip = strlen(p->ss7->internationalprefix);
01312          ss7_called_nai = SS7_NAI_INTERNATIONAL;
01313       } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
01314          called_nai_strip = strlen(p->ss7->nationalprefix);
01315          ss7_called_nai = SS7_NAI_NATIONAL;
01316       } else {
01317          ss7_called_nai = SS7_NAI_SUBSCRIBER;
01318       }
01319    }
01320    isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
01321 
01322    calling_nai_strip = 0;
01323    ss7_calling_nai = p->ss7->calling_nai;
01324    if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
01325       if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
01326          calling_nai_strip = strlen(p->ss7->internationalprefix);
01327          ss7_calling_nai = SS7_NAI_INTERNATIONAL;
01328       } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
01329          calling_nai_strip = strlen(p->ss7->nationalprefix);
01330          ss7_calling_nai = SS7_NAI_NATIONAL;
01331       } else {
01332          ss7_calling_nai = SS7_NAI_SUBSCRIBER;
01333       }
01334    }
01335    isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
01336       p->use_callingpres ? cid_pres2ss7pres(ast->connected.id.number.presentation) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
01337       p->use_callingpres ? cid_pres2ss7screen(ast->connected.id.number.presentation) : SS7_SCREENING_USER_PROVIDED);
01338 
01339    isup_set_oli(p->ss7call, ast->connected.ani2);
01340    isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
01341 
01342    /* Set the charge number if it is set */
01343    charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
01344    if (charge_str)
01345       isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
01346 
01347    gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
01348    if (gen_address)
01349       isup_set_gen_address(p->ss7call, gen_address, p->gen_add_nai,p->gen_add_pres_ind, p->gen_add_num_plan,p->gen_add_type); /* need to add some types here for NAI,PRES,TYPE */
01350 
01351    gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
01352    gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
01353    gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
01354    if (gen_digits)
01355       isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
01356 
01357    gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
01358    if (gen_name)
01359       isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
01360 
01361    jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
01362    if (jip_digits)
01363       isup_set_jip_digits(p->ss7call, jip_digits);
01364 
01365    lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
01366    if (lspi_ident)
01367       isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
01368 
01369    rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
01370    if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
01371       isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
01372    }
01373 
01374    call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
01375    call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
01376    if (call_ref_id && call_ref_pc) {
01377       isup_set_callref(p->ss7call, atoi(call_ref_id),
01378              call_ref_pc ? atoi(call_ref_pc) : 0);
01379    }
01380 
01381    send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
01382    if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
01383       (isup_far(p->ss7->ss7, p->ss7call));
01384 
01385    p->call_level = SIG_SS7_CALL_LEVEL_SETUP;
01386    isup_iam(p->ss7->ss7, p->ss7call);
01387    sig_ss7_set_dialing(p, 1);
01388    ast_setstate(ast, AST_STATE_DIALING);
01389    ss7_rel(p->ss7);
01390    return 0;
01391 }
01392 
01393 /*!
01394  * \brief SS7 hangup channel.
01395  * \since 1.8
01396  *
01397  * \param p Signaling private structure pointer.
01398  * \param ast Asterisk channel structure pointer.
01399  *
01400  * \retval 0 on success.
01401  * \retval -1 on error.
01402  */
01403 int sig_ss7_hangup(struct sig_ss7_chan *p, struct ast_channel *ast)
01404 {
01405    int res = 0;
01406 
01407    if (!ast->tech_pvt) {
01408       ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
01409       return 0;
01410    }
01411 
01412    p->owner = NULL;
01413    sig_ss7_set_dialing(p, 0);
01414    p->call_level = SIG_SS7_CALL_LEVEL_IDLE;
01415    p->outgoing = 0;
01416    p->progress = 0;
01417    p->rlt = 0;
01418    p->exten[0] = '\0';
01419    /* Perform low level hangup if no owner left */
01420    if (p->ss7call) {
01421       if (!ss7_grab(p, p->ss7)) {
01422          if (!p->alreadyhungup) {
01423             const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
01424             int icause = ast->hangupcause ? ast->hangupcause : -1;
01425 
01426             if (cause) {
01427                if (atoi(cause))
01428                   icause = atoi(cause);
01429             }
01430             isup_rel(p->ss7->ss7, p->ss7call, icause);
01431             ss7_rel(p->ss7);
01432             p->alreadyhungup = 1;
01433          } else
01434             ast_log(LOG_WARNING, "Trying to hangup twice!\n");
01435       } else {
01436          ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
01437          res = -1;
01438       }
01439    }
01440 
01441    return res;
01442 }
01443 
01444 /*!
01445  * \brief SS7 answer channel.
01446  * \since 1.8
01447  *
01448  * \param p Signaling private structure pointer.
01449  * \param ast Asterisk channel structure pointer.
01450  *
01451  * \retval 0 on success.
01452  * \retval -1 on error.
01453  */
01454 int sig_ss7_answer(struct sig_ss7_chan *p, struct ast_channel *ast)
01455 {
01456    int res;
01457 
01458    if (!ss7_grab(p, p->ss7)) {
01459       if (p->call_level < SIG_SS7_CALL_LEVEL_CONNECT) {
01460          p->call_level = SIG_SS7_CALL_LEVEL_CONNECT;
01461       }
01462       res = isup_anm(p->ss7->ss7, p->ss7call);
01463       ss7_rel(p->ss7);
01464    } else {
01465       ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->ss7->span);
01466       res = -1;
01467    }
01468    return res;
01469 }
01470 
01471 /*!
01472  * \brief Fix up a channel:  If a channel is consumed, this is called.  Basically update any ->owner links.
01473  * \since 1.8
01474  *
01475  * \param oldchan Old channel pointer to replace.
01476  * \param newchan New channel pointer to set.
01477  * \param pchan Signaling private structure pointer.
01478  *
01479  * \return Nothing
01480  */
01481 void sig_ss7_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_ss7_chan *pchan)
01482 {
01483    if (pchan->owner == oldchan) {
01484       pchan->owner = newchan;
01485    }
01486 }
01487 
01488 /*!
01489  * \brief SS7 answer channel.
01490  * \since 1.8
01491  *
01492  * \param p Signaling private structure pointer.
01493  * \param chan Asterisk channel structure pointer.
01494  * \param condition AST control frame subtype.
01495  * \param data AST control frame payload contents.
01496  * \param datalen Length of payload contents.
01497  *
01498  * \retval 0 on success.
01499  * \retval -1 on error or indication condition not handled.
01500  */
01501 int sig_ss7_indicate(struct sig_ss7_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
01502 {
01503    int res = -1;
01504 
01505    switch (condition) {
01506    case AST_CONTROL_BUSY:
01507       res = sig_ss7_play_tone(p, SIG_SS7_TONE_BUSY);
01508       break;
01509    case AST_CONTROL_RINGING:
01510       if (p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
01511          p->call_level = SIG_SS7_CALL_LEVEL_ALERTING;
01512          if (p->ss7 && p->ss7->ss7) {
01513             ss7_grab(p, p->ss7);
01514             if ((isup_far(p->ss7->ss7, p->ss7call)) != -1)
01515                p->rlt = 1;
01516             if (p->rlt != 1) /* No need to send CPG if call will be RELEASE */
01517                isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_ALERTING);
01518             ss7_rel(p->ss7);
01519          }
01520       }
01521 
01522       res = sig_ss7_play_tone(p, SIG_SS7_TONE_RINGTONE);
01523 
01524       if (chan->_state != AST_STATE_UP && chan->_state != AST_STATE_RING) {
01525          ast_setstate(chan, AST_STATE_RINGING);
01526       }
01527       break;
01528    case AST_CONTROL_PROCEEDING:
01529       ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
01530       /* This IF sends the FAR for an answered ALEG call */
01531       if (chan->_state == AST_STATE_UP && (p->rlt != 1)){
01532          if ((isup_far(p->ss7->ss7, p->ss7call)) != -1)
01533             p->rlt = 1;
01534       }
01535 
01536       if (p->call_level < SIG_SS7_CALL_LEVEL_PROCEEDING && !p->outgoing) {
01537          p->call_level = SIG_SS7_CALL_LEVEL_PROCEEDING;
01538          if (p->ss7 && p->ss7->ss7) {
01539             ss7_grab(p, p->ss7);
01540             isup_acm(p->ss7->ss7, p->ss7call);
01541             ss7_rel(p->ss7);
01542          }
01543       }
01544       /* don't continue in ast_indicate */
01545       res = 0;
01546       break;
01547    case AST_CONTROL_PROGRESS:
01548       ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
01549       if (!p->progress && p->call_level < SIG_SS7_CALL_LEVEL_ALERTING && !p->outgoing) {
01550          p->progress = 1;/* No need to send inband-information progress again. */
01551          if (p->ss7 && p->ss7->ss7) {
01552             ss7_grab(p, p->ss7);
01553             isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_INBANDINFO);
01554             ss7_rel(p->ss7);
01555             /* enable echo canceler here on SS7 calls */
01556             sig_ss7_set_echocanceller(p, 1);
01557          }
01558       }
01559       /* don't continue in ast_indicate */
01560       res = 0;
01561       break;
01562    case AST_CONTROL_CONGESTION:
01563       chan->hangupcause = AST_CAUSE_CONGESTION;
01564       break;
01565    case AST_CONTROL_HOLD:
01566       ast_moh_start(chan, data, p->mohinterpret);
01567       break;
01568    case AST_CONTROL_UNHOLD:
01569       ast_moh_stop(chan);
01570       break;
01571    case AST_CONTROL_SRCUPDATE:
01572       res = 0;
01573       break;
01574    case -1:
01575       res = sig_ss7_play_tone(p, -1);
01576       break;
01577    }
01578    return res;
01579 }
01580 
01581 /*!
01582  * \brief SS7 channel request.
01583  * \since 1.8
01584  *
01585  * \param p Signaling private structure pointer.
01586  * \param law Companding law preferred
01587  * \param requestor Asterisk channel requesting a channel to dial (Can be NULL)
01588  * \param transfercapability
01589  *
01590  * \retval ast_channel on success.
01591  * \retval NULL on error.
01592  */
01593 struct ast_channel *sig_ss7_request(struct sig_ss7_chan *p, enum sig_ss7_law law, const struct ast_channel *requestor, int transfercapability)
01594 {
01595    struct ast_channel *ast;
01596 
01597    p->outgoing = 1;
01598    ast = sig_ss7_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability, p->exten, requestor);
01599    if (!ast) {
01600       p->outgoing = 0;
01601    }
01602    return ast;
01603 }
01604 
01605 /*!
01606  * \brief Delete the sig_ss7 private channel structure.
01607  * \since 1.8
01608  *
01609  * \param doomed sig_ss7 private channel structure to delete.
01610  *
01611  * \return Nothing
01612  */
01613 void sig_ss7_chan_delete(struct sig_ss7_chan *doomed)
01614 {
01615    ast_free(doomed);
01616 }
01617 
01618 /*!
01619  * \brief Create a new sig_ss7 private channel structure.
01620  * \since 1.8
01621  *
01622  * \param pvt_data Upper layer private data structure.
01623  * \param callback Callbacks to the upper layer.
01624  * \param ss7 Controlling linkset for the channel.
01625  *
01626  * \retval sig_ss7_chan on success.
01627  * \retval NULL on error.
01628  */
01629 struct sig_ss7_chan *sig_ss7_chan_new(void *pvt_data, struct sig_ss7_callback *callback, struct sig_ss7_linkset *ss7)
01630 {
01631    struct sig_ss7_chan *pvt;
01632 
01633    pvt = ast_calloc(1, sizeof(*pvt));
01634    if (!pvt) {
01635       return pvt;
01636    }
01637 
01638    pvt->calls = callback;
01639    pvt->chan_pvt = pvt_data;
01640    pvt->ss7 = ss7;
01641 
01642    return pvt;
01643 }
01644 
01645 /*!
01646  * \brief Initialize the SS7 linkset control.
01647  * \since 1.8
01648  *
01649  * \param ss7 sig_ss7 SS7 control structure.
01650  *
01651  * \return Nothing
01652  */
01653 void sig_ss7_init_linkset(struct sig_ss7_linkset *ss7)
01654 {
01655    int idx;
01656 
01657    memset(ss7, 0, sizeof(*ss7));
01658 
01659    ast_mutex_init(&ss7->lock);
01660 
01661    ss7->master = AST_PTHREADT_NULL;
01662    for (idx = 0; idx < ARRAY_LEN(ss7->fds); ++idx) {
01663       ss7->fds[idx] = -1;
01664    }
01665 }
01666 
01667 /* ------------------------------------------------------------------- */
01668 
01669 #endif   /* defined(HAVE_SS7) */
01670 /* end sig_ss7.c */