Wed Apr 6 11:29:48 2011

Asterisk developer's documentation


sig_pri.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2009, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief PRI signaling module
00022  *
00023  * \author Matthew Fredrickson <creslin@digium.com>
00024  */
00025 
00026 
00027 #include "asterisk.h"
00028 
00029 #ifdef HAVE_PRI
00030 
00031 #include <errno.h>
00032 #include <ctype.h>
00033 #include <signal.h>
00034 
00035 #include "asterisk/utils.h"
00036 #include "asterisk/options.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/app.h"
00039 #include "asterisk/file.h"
00040 #include "asterisk/callerid.h"
00041 #include "asterisk/say.h"
00042 #include "asterisk/manager.h"
00043 #include "asterisk/astdb.h"
00044 #include "asterisk/causes.h"
00045 #include "asterisk/musiconhold.h"
00046 #include "asterisk/cli.h"
00047 #include "asterisk/transcap.h"
00048 #include "asterisk/features.h"
00049 #include "asterisk/aoc.h"
00050 
00051 #include "sig_pri.h"
00052 #ifndef PRI_EVENT_FACILITY
00053 #error please update libpri
00054 #endif
00055 
00056 /* define this to send PRI user-user information elements */
00057 #undef SUPPORT_USERUSER
00058 
00059 #if defined(HAVE_PRI_CCSS)
00060 struct sig_pri_cc_agent_prv {
00061    /*! Asterisk span D channel control structure. */
00062    struct sig_pri_span *pri;
00063    /*! CC id value to use with libpri. -1 if invalid. */
00064    long cc_id;
00065    /*! TRUE if CC has been requested and we are waiting for the response. */
00066    unsigned char cc_request_response_pending;
00067 };
00068 
00069 struct sig_pri_cc_monitor_instance {
00070    /*! \brief Asterisk span D channel control structure. */
00071    struct sig_pri_span *pri;
00072    /*! CC id value to use with libpri. (-1 if already canceled). */
00073    long cc_id;
00074    /*! CC core id value. */
00075    int core_id;
00076    /*! Device name(Channel name less sequence number) */
00077    char name[1];
00078 };
00079 
00080 /*! Upper level agent/monitor type name. */
00081 static const char *sig_pri_cc_type_name;
00082 /*! Container of sig_pri monitor instances. */
00083 static struct ao2_container *sig_pri_cc_monitors;
00084 #endif   /* defined(HAVE_PRI_CCSS) */
00085 
00086 static int pri_matchdigittimeout = 3000;
00087 
00088 static int pri_gendigittimeout = 8000;
00089 
00090 #define DCHAN_NOTINALARM  (1 << 0)
00091 #define DCHAN_UP          (1 << 1)
00092 
00093 /* Defines to help decode the encoded event channel id. */
00094 #define PRI_CHANNEL(p)  ((p) & 0xff)
00095 #define PRI_SPAN(p)     (((p) >> 8) & 0xff)
00096 #define PRI_EXPLICIT (1 << 16)
00097 #define PRI_CIS_CALL (1 << 17)   /* Call is using the D channel only. */
00098 #define PRI_HELD_CALL   (1 << 18)
00099 
00100 
00101 #define DCHAN_AVAILABLE (DCHAN_NOTINALARM | DCHAN_UP)
00102 
00103 #define PRI_DEADLOCK_AVOIDANCE(p) \
00104    do { \
00105       sig_pri_unlock_private(p); \
00106       usleep(1); \
00107       sig_pri_lock_private(p); \
00108    } while (0)
00109 
00110 static int pri_active_dchan_index(struct sig_pri_span *pri);
00111 
00112 static inline void pri_rel(struct sig_pri_span *pri)
00113 {
00114    ast_mutex_unlock(&pri->lock);
00115 }
00116 
00117 static unsigned int PVT_TO_CHANNEL(struct sig_pri_chan *p)
00118 {
00119    int res = (((p)->prioffset) | ((p)->logicalspan << 8) | (p->mastertrunkgroup ? PRI_EXPLICIT : 0));
00120    ast_debug(5, "prioffset: %d mastertrunkgroup: %d logicalspan: %d result: %d\n",
00121       p->prioffset, p->mastertrunkgroup, p->logicalspan, res);
00122 
00123    return res;
00124 }
00125 
00126 static void sig_pri_handle_dchan_exception(struct sig_pri_span *pri, int index)
00127 {
00128    if (pri->calls->handle_dchan_exception)
00129       pri->calls->handle_dchan_exception(pri, index);
00130 }
00131 
00132 static void sig_pri_set_dialing(struct sig_pri_chan *p, int is_dialing)
00133 {
00134    if (p->calls->set_dialing) {
00135       p->calls->set_dialing(p->chan_pvt, is_dialing);
00136    }
00137 }
00138 
00139 static void sig_pri_set_digital(struct sig_pri_chan *p, int is_digital)
00140 {
00141    p->digital = is_digital;
00142    if (p->calls->set_digital) {
00143       p->calls->set_digital(p->chan_pvt, is_digital);
00144    }
00145 }
00146 
00147 static void sig_pri_set_alarm(struct sig_pri_chan *p, int in_alarm)
00148 {
00149    p->inalarm = in_alarm;
00150    if (p->calls->set_alarm) {
00151       p->calls->set_alarm(p->chan_pvt, in_alarm);
00152    }
00153 }
00154 
00155 static const char *sig_pri_get_orig_dialstring(struct sig_pri_chan *p)
00156 {
00157    if (p->calls->get_orig_dialstring) {
00158       return p->calls->get_orig_dialstring(p->chan_pvt);
00159    }
00160    ast_log(LOG_ERROR, "get_orig_dialstring callback not defined\n");
00161    return "";
00162 }
00163 
00164 #if defined(HAVE_PRI_CCSS)
00165 static void sig_pri_make_cc_dialstring(struct sig_pri_chan *p, char *buf, size_t buf_size)
00166 {
00167    if (p->calls->make_cc_dialstring) {
00168       p->calls->make_cc_dialstring(p->chan_pvt, buf, buf_size);
00169    } else {
00170       ast_log(LOG_ERROR, "make_cc_dialstring callback not defined\n");
00171       buf[0] = '\0';
00172    }
00173 }
00174 #endif   /* defined(HAVE_PRI_CCSS) */
00175 
00176 /*!
00177  * \internal
00178  * \brief Reevaluate the PRI span device state.
00179  * \since 1.8
00180  *
00181  * \param pri Asterisk D channel control structure.
00182  *
00183  * \return Nothing
00184  *
00185  * \note Assumes the pri->lock is already obtained.
00186  */
00187 static void sig_pri_span_devstate_changed(struct sig_pri_span *pri)
00188 {
00189    if (pri->calls->update_span_devstate) {
00190       pri->calls->update_span_devstate(pri);
00191    }
00192 }
00193 
00194 /*!
00195  * \internal
00196  * \brief Set the caller id information in the parent module.
00197  * \since 1.8
00198  *
00199  * \param p sig_pri channel structure.
00200  *
00201  * \return Nothing
00202  */
00203 static void sig_pri_set_caller_id(struct sig_pri_chan *p)
00204 {
00205    struct ast_party_caller caller;
00206 
00207    if (p->calls->set_callerid) {
00208       ast_party_caller_init(&caller);
00209 
00210       caller.id.name.str = p->cid_name;
00211       caller.id.name.presentation = p->callingpres;
00212       caller.id.name.valid = 1;
00213 
00214       caller.id.number.str = p->cid_num;
00215       caller.id.number.plan = p->cid_ton;
00216       caller.id.number.presentation = p->callingpres;
00217       caller.id.number.valid = 1;
00218 
00219       if (!ast_strlen_zero(p->cid_subaddr)) {
00220          caller.id.subaddress.valid = 1;
00221          //caller.id.subaddress.type = 0;/* nsap */
00222          //caller.id.subaddress.odd_even_indicator = 0;
00223          caller.id.subaddress.str = p->cid_subaddr;
00224       }
00225       caller.id.tag = p->user_tag;
00226 
00227       caller.ani.number.str = p->cid_ani;
00228       //caller.ani.number.plan = p->xxx;
00229       //caller.ani.number.presentation = p->xxx;
00230       caller.ani.number.valid = 1;
00231 
00232       caller.ani2 = p->cid_ani2;
00233       p->calls->set_callerid(p->chan_pvt, &caller);
00234    }
00235 }
00236 
00237 /*!
00238  * \internal
00239  * \brief Set the Dialed Number Identifier.
00240  * \since 1.8
00241  *
00242  * \param p sig_pri channel structure.
00243  * \param dnid Dialed Number Identifier string.
00244  *
00245  * \return Nothing
00246  */
00247 static void sig_pri_set_dnid(struct sig_pri_chan *p, const char *dnid)
00248 {
00249    if (p->calls->set_dnid) {
00250       p->calls->set_dnid(p->chan_pvt, dnid);
00251    }
00252 }
00253 
00254 /*!
00255  * \internal
00256  * \brief Set the Redirecting Directory Number Information Service (RDNIS).
00257  * \since 1.8
00258  *
00259  * \param p sig_pri channel structure.
00260  * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
00261  *
00262  * \return Nothing
00263  */
00264 static void sig_pri_set_rdnis(struct sig_pri_chan *p, const char *rdnis)
00265 {
00266    if (p->calls->set_rdnis) {
00267       p->calls->set_rdnis(p->chan_pvt, rdnis);
00268    }
00269 }
00270 
00271 static void sig_pri_unlock_private(struct sig_pri_chan *p)
00272 {
00273    if (p->calls->unlock_private)
00274       p->calls->unlock_private(p->chan_pvt);
00275 }
00276 
00277 static void sig_pri_lock_private(struct sig_pri_chan *p)
00278 {
00279    if (p->calls->lock_private)
00280       p->calls->lock_private(p->chan_pvt);
00281 }
00282 
00283 static inline int pri_grab(struct sig_pri_chan *p, struct sig_pri_span *pri)
00284 {
00285    int res;
00286    /* Grab the lock first */
00287    do {
00288       res = ast_mutex_trylock(&pri->lock);
00289       if (res) {
00290          PRI_DEADLOCK_AVOIDANCE(p);
00291       }
00292    } while (res);
00293    /* Then break the poll */
00294    pthread_kill(pri->master, SIGURG);
00295    return 0;
00296 }
00297 
00298 /*!
00299  * \internal
00300  * \brief Convert PRI redirecting reason to asterisk version.
00301  * \since 1.8
00302  *
00303  * \param pri_reason PRI redirecting reason.
00304  *
00305  * \return Equivalent asterisk redirecting reason value.
00306  */
00307 static enum AST_REDIRECTING_REASON pri_to_ast_reason(int pri_reason)
00308 {
00309    enum AST_REDIRECTING_REASON ast_reason;
00310 
00311    switch (pri_reason) {
00312    case PRI_REDIR_FORWARD_ON_BUSY:
00313       ast_reason = AST_REDIRECTING_REASON_USER_BUSY;
00314       break;
00315    case PRI_REDIR_FORWARD_ON_NO_REPLY:
00316       ast_reason = AST_REDIRECTING_REASON_NO_ANSWER;
00317       break;
00318    case PRI_REDIR_DEFLECTION:
00319       ast_reason = AST_REDIRECTING_REASON_DEFLECTION;
00320       break;
00321    case PRI_REDIR_UNCONDITIONAL:
00322       ast_reason = AST_REDIRECTING_REASON_UNCONDITIONAL;
00323       break;
00324    case PRI_REDIR_UNKNOWN:
00325    default:
00326       ast_reason = AST_REDIRECTING_REASON_UNKNOWN;
00327       break;
00328    }
00329 
00330    return ast_reason;
00331 }
00332 
00333 /*!
00334  * \internal
00335  * \brief Convert asterisk redirecting reason to PRI version.
00336  * \since 1.8
00337  *
00338  * \param ast_reason Asterisk redirecting reason.
00339  *
00340  * \return Equivalent PRI redirecting reason value.
00341  */
00342 static int ast_to_pri_reason(enum AST_REDIRECTING_REASON ast_reason)
00343 {
00344    int pri_reason;
00345 
00346    switch (ast_reason) {
00347    case AST_REDIRECTING_REASON_USER_BUSY:
00348       pri_reason = PRI_REDIR_FORWARD_ON_BUSY;
00349       break;
00350    case AST_REDIRECTING_REASON_NO_ANSWER:
00351       pri_reason = PRI_REDIR_FORWARD_ON_NO_REPLY;
00352       break;
00353    case AST_REDIRECTING_REASON_UNCONDITIONAL:
00354       pri_reason = PRI_REDIR_UNCONDITIONAL;
00355       break;
00356    case AST_REDIRECTING_REASON_DEFLECTION:
00357       pri_reason = PRI_REDIR_DEFLECTION;
00358       break;
00359    case AST_REDIRECTING_REASON_UNKNOWN:
00360    default:
00361       pri_reason = PRI_REDIR_UNKNOWN;
00362       break;
00363    }
00364 
00365    return pri_reason;
00366 }
00367 
00368 /*!
00369  * \internal
00370  * \brief Convert PRI number presentation to asterisk version.
00371  * \since 1.8
00372  *
00373  * \param pri_presentation PRI number presentation.
00374  *
00375  * \return Equivalent asterisk number presentation value.
00376  */
00377 static int pri_to_ast_presentation(int pri_presentation)
00378 {
00379    int ast_presentation;
00380 
00381    switch (pri_presentation) {
00382    case PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
00383       ast_presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
00384       break;
00385    case PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
00386       ast_presentation = AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
00387       break;
00388    case PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
00389       ast_presentation = AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN;
00390       break;
00391    case PRES_ALLOWED_NETWORK_NUMBER:
00392       ast_presentation = AST_PRES_ALLOWED_NETWORK_NUMBER;
00393       break;
00394    case PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
00395       ast_presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
00396       break;
00397    case PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
00398       ast_presentation = AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
00399       break;
00400    case PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
00401       ast_presentation = AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN;
00402       break;
00403    case PRES_PROHIB_NETWORK_NUMBER:
00404       ast_presentation = AST_PRES_PROHIB_NETWORK_NUMBER;
00405       break;
00406    case PRES_NUMBER_NOT_AVAILABLE:
00407       ast_presentation = AST_PRES_NUMBER_NOT_AVAILABLE;
00408       break;
00409    default:
00410       ast_presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
00411       break;
00412    }
00413 
00414    return ast_presentation;
00415 }
00416 
00417 /*!
00418  * \internal
00419  * \brief Convert asterisk number presentation to PRI version.
00420  * \since 1.8
00421  *
00422  * \param ast_presentation Asterisk number presentation.
00423  *
00424  * \return Equivalent PRI number presentation value.
00425  */
00426 static int ast_to_pri_presentation(int ast_presentation)
00427 {
00428    int pri_presentation;
00429 
00430    switch (ast_presentation) {
00431    case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
00432       pri_presentation = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
00433       break;
00434    case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
00435       pri_presentation = PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
00436       break;
00437    case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
00438       pri_presentation = PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN;
00439       break;
00440    case AST_PRES_ALLOWED_NETWORK_NUMBER:
00441       pri_presentation = PRES_ALLOWED_NETWORK_NUMBER;
00442       break;
00443    case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
00444       pri_presentation = PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
00445       break;
00446    case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
00447       pri_presentation = PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
00448       break;
00449    case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
00450       pri_presentation = PRES_PROHIB_USER_NUMBER_FAILED_SCREEN;
00451       break;
00452    case AST_PRES_PROHIB_NETWORK_NUMBER:
00453       pri_presentation = PRES_PROHIB_NETWORK_NUMBER;
00454       break;
00455    case AST_PRES_NUMBER_NOT_AVAILABLE:
00456       pri_presentation = PRES_NUMBER_NOT_AVAILABLE;
00457       break;
00458    default:
00459       pri_presentation = PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
00460       break;
00461    }
00462 
00463    return pri_presentation;
00464 }
00465 
00466 /*!
00467  * \internal
00468  * \brief Convert PRI name char_set to asterisk version.
00469  * \since 1.8
00470  *
00471  * \param pri_char_set PRI name char_set.
00472  *
00473  * \return Equivalent asterisk name char_set value.
00474  */
00475 static enum AST_PARTY_CHAR_SET pri_to_ast_char_set(int pri_char_set)
00476 {
00477    enum AST_PARTY_CHAR_SET ast_char_set;
00478 
00479    switch (pri_char_set) {
00480    default:
00481    case PRI_CHAR_SET_UNKNOWN:
00482       ast_char_set = AST_PARTY_CHAR_SET_UNKNOWN;
00483       break;
00484    case PRI_CHAR_SET_ISO8859_1:
00485       ast_char_set = AST_PARTY_CHAR_SET_ISO8859_1;
00486       break;
00487    case PRI_CHAR_SET_WITHDRAWN:
00488       ast_char_set = AST_PARTY_CHAR_SET_WITHDRAWN;
00489       break;
00490    case PRI_CHAR_SET_ISO8859_2:
00491       ast_char_set = AST_PARTY_CHAR_SET_ISO8859_2;
00492       break;
00493    case PRI_CHAR_SET_ISO8859_3:
00494       ast_char_set = AST_PARTY_CHAR_SET_ISO8859_3;
00495       break;
00496    case PRI_CHAR_SET_ISO8859_4:
00497       ast_char_set = AST_PARTY_CHAR_SET_ISO8859_4;
00498       break;
00499    case PRI_CHAR_SET_ISO8859_5:
00500       ast_char_set = AST_PARTY_CHAR_SET_ISO8859_5;
00501       break;
00502    case PRI_CHAR_SET_ISO8859_7:
00503       ast_char_set = AST_PARTY_CHAR_SET_ISO8859_7;
00504       break;
00505    case PRI_CHAR_SET_ISO10646_BMPSTRING:
00506       ast_char_set = AST_PARTY_CHAR_SET_ISO10646_BMPSTRING;
00507       break;
00508    case PRI_CHAR_SET_ISO10646_UTF_8STRING:
00509       ast_char_set = AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING;
00510       break;
00511    }
00512 
00513    return ast_char_set;
00514 }
00515 
00516 /*!
00517  * \internal
00518  * \brief Convert asterisk name char_set to PRI version.
00519  * \since 1.8
00520  *
00521  * \param ast_char_set Asterisk name char_set.
00522  *
00523  * \return Equivalent PRI name char_set value.
00524  */
00525 static int ast_to_pri_char_set(enum AST_PARTY_CHAR_SET ast_char_set)
00526 {
00527    int pri_char_set;
00528 
00529    switch (ast_char_set) {
00530    default:
00531    case AST_PARTY_CHAR_SET_UNKNOWN:
00532       pri_char_set = PRI_CHAR_SET_UNKNOWN;
00533       break;
00534    case AST_PARTY_CHAR_SET_ISO8859_1:
00535       pri_char_set = PRI_CHAR_SET_ISO8859_1;
00536       break;
00537    case AST_PARTY_CHAR_SET_WITHDRAWN:
00538       pri_char_set = PRI_CHAR_SET_WITHDRAWN;
00539       break;
00540    case AST_PARTY_CHAR_SET_ISO8859_2:
00541       pri_char_set = PRI_CHAR_SET_ISO8859_2;
00542       break;
00543    case AST_PARTY_CHAR_SET_ISO8859_3:
00544       pri_char_set = PRI_CHAR_SET_ISO8859_3;
00545       break;
00546    case AST_PARTY_CHAR_SET_ISO8859_4:
00547       pri_char_set = PRI_CHAR_SET_ISO8859_4;
00548       break;
00549    case AST_PARTY_CHAR_SET_ISO8859_5:
00550       pri_char_set = PRI_CHAR_SET_ISO8859_5;
00551       break;
00552    case AST_PARTY_CHAR_SET_ISO8859_7:
00553       pri_char_set = PRI_CHAR_SET_ISO8859_7;
00554       break;
00555    case AST_PARTY_CHAR_SET_ISO10646_BMPSTRING:
00556       pri_char_set = PRI_CHAR_SET_ISO10646_BMPSTRING;
00557       break;
00558    case AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING:
00559       pri_char_set = PRI_CHAR_SET_ISO10646_UTF_8STRING;
00560       break;
00561    }
00562 
00563    return pri_char_set;
00564 }
00565 
00566 #if defined(HAVE_PRI_SUBADDR)
00567 /*!
00568  * \internal
00569  * \brief Fill in the asterisk party subaddress from the given PRI party subaddress.
00570  * \since 1.8
00571  *
00572  * \param ast_subaddress Asterisk party subaddress structure.
00573  * \param pri_subaddress PRI party subaddress structure.
00574  *
00575  * \return Nothing
00576  *
00577  */
00578 static void sig_pri_set_subaddress(struct ast_party_subaddress *ast_subaddress, const struct pri_party_subaddress *pri_subaddress)
00579 {
00580    char *cnum, *ptr;
00581    int x, len;
00582 
00583    if (ast_subaddress->str) {
00584       ast_free(ast_subaddress->str);
00585    }
00586    if (pri_subaddress->length <= 0) {
00587       ast_party_subaddress_init(ast_subaddress);
00588       return;
00589    }
00590 
00591    if (!pri_subaddress->type) {
00592       /* NSAP */
00593       ast_subaddress->str = ast_strdup((char *) pri_subaddress->data);
00594    } else {
00595       /* User Specified */
00596       if (!(cnum = ast_malloc(2 * pri_subaddress->length + 1))) {
00597          ast_party_subaddress_init(ast_subaddress);
00598          return;
00599       }
00600 
00601       ptr = cnum;
00602       len = pri_subaddress->length - 1; /* -1 account for zero based indexing */
00603       for (x = 0; x < len; ++x) {
00604          ptr += sprintf(ptr, "%02x", pri_subaddress->data[x]);
00605       }
00606 
00607       if (pri_subaddress->odd_even_indicator) {
00608          /* ODD */
00609          sprintf(ptr, "%01x", (pri_subaddress->data[len]) >> 4);
00610       } else {
00611          /* EVEN */
00612          sprintf(ptr, "%02x", pri_subaddress->data[len]);
00613       }
00614       ast_subaddress->str = cnum;
00615    }
00616    ast_subaddress->type = pri_subaddress->type;
00617    ast_subaddress->odd_even_indicator = pri_subaddress->odd_even_indicator;
00618    ast_subaddress->valid = 1;
00619 }
00620 #endif   /* defined(HAVE_PRI_SUBADDR) */
00621 
00622 #if defined(HAVE_PRI_SUBADDR)
00623 static unsigned char ast_pri_pack_hex_char(char c)
00624 {
00625    unsigned char res;
00626 
00627    if (c < '0') {
00628       res = 0;
00629    } else if (c < ('9' + 1)) {
00630       res = c - '0';
00631    } else if (c < 'A') {
00632       res = 0;
00633    } else if (c < ('F' + 1)) {
00634       res = c - 'A' + 10;
00635    } else if (c < 'a') {
00636       res = 0;
00637    } else if (c < ('f' + 1)) {
00638       res = c - 'a' + 10;
00639    } else {
00640       res = 0;
00641    }
00642    return res;
00643 }
00644 #endif   /* defined(HAVE_PRI_SUBADDR) */
00645 
00646 #if defined(HAVE_PRI_SUBADDR)
00647 /*!
00648  * \internal
00649  * \brief Convert a null terminated hexadecimal string to a packed hex byte array.
00650  * \details left justified, with 0 padding if odd length.
00651  * \since 1.8
00652  *
00653  * \param dst pointer to packed byte array.
00654  * \param src pointer to null terminated hexadecimal string.
00655  * \param maxlen destination array size.
00656  *
00657  * \return Length of byte array
00658  *
00659  * \note The dst is not an ASCIIz string.
00660  * \note The src is an ASCIIz hex string.
00661  */
00662 static int ast_pri_pack_hex_string(unsigned char *dst, char *src, int maxlen)
00663 {
00664    int res = 0;
00665    int len = strlen(src);
00666 
00667    if (len > (2 * maxlen)) {
00668       len = 2 * maxlen;
00669    }
00670 
00671    res = len / 2 + len % 2;
00672 
00673    while (len > 1) {
00674       *dst = ast_pri_pack_hex_char(*src) << 4;
00675       src++;
00676       *dst |= ast_pri_pack_hex_char(*src);
00677       dst++, src++;
00678       len -= 2;
00679    }
00680    if (len) { /* 1 left */
00681       *dst = ast_pri_pack_hex_char(*src) << 4;
00682    }
00683    return res;
00684 }
00685 #endif   /* defined(HAVE_PRI_SUBADDR) */
00686 
00687 #if defined(HAVE_PRI_SUBADDR)
00688 /*!
00689  * \internal
00690  * \brief Fill in the PRI party subaddress from the given asterisk party subaddress.
00691  * \since 1.8
00692  *
00693  * \param pri_subaddress PRI party subaddress structure.
00694  * \param ast_subaddress Asterisk party subaddress structure.
00695  *
00696  * \return Nothing
00697  *
00698  * \note Assumes that pri_subaddress has been previously memset to zero.
00699  */
00700 static void sig_pri_party_subaddress_from_ast(struct pri_party_subaddress *pri_subaddress, const struct ast_party_subaddress *ast_subaddress)
00701 {
00702    if (ast_subaddress->valid && !ast_strlen_zero(ast_subaddress->str)) {
00703       pri_subaddress->type = ast_subaddress->type;
00704       if (!ast_subaddress->type) {
00705          /* 0 = NSAP */
00706          ast_copy_string((char *) pri_subaddress->data, ast_subaddress->str,
00707             sizeof(pri_subaddress->data));
00708          pri_subaddress->length = strlen((char *) pri_subaddress->data);
00709          pri_subaddress->odd_even_indicator = 0;
00710          pri_subaddress->valid = 1;
00711       } else {
00712          /* 2 = User Specified */
00713          /*
00714           * Copy HexString to packed HexData,
00715           * if odd length then right pad trailing byte with 0
00716           */
00717          int length = ast_pri_pack_hex_string(pri_subaddress->data,
00718             ast_subaddress->str, sizeof(pri_subaddress->data));
00719 
00720          pri_subaddress->length = length;
00721          pri_subaddress->odd_even_indicator = (length & 1);
00722          pri_subaddress->valid = 1;
00723       }
00724    }
00725 }
00726 #endif   /* defined(HAVE_PRI_SUBADDR) */
00727 
00728 /*!
00729  * \internal
00730  * \brief Fill in the PRI party name from the given asterisk party name.
00731  * \since 1.8
00732  *
00733  * \param pri_name PRI party name structure.
00734  * \param ast_name Asterisk party name structure.
00735  *
00736  * \return Nothing
00737  *
00738  * \note Assumes that pri_name has been previously memset to zero.
00739  */
00740 static void sig_pri_party_name_from_ast(struct pri_party_name *pri_name, const struct ast_party_name *ast_name)
00741 {
00742    if (!ast_name->valid) {
00743       return;
00744    }
00745    pri_name->valid = 1;
00746    pri_name->presentation = ast_to_pri_presentation(ast_name->presentation);
00747    pri_name->char_set = ast_to_pri_char_set(ast_name->char_set);
00748    if (!ast_strlen_zero(ast_name->str)) {
00749       ast_copy_string(pri_name->str, ast_name->str, sizeof(pri_name->str));
00750    }
00751 }
00752 
00753 /*!
00754  * \internal
00755  * \brief Fill in the PRI party number from the given asterisk party number.
00756  * \since 1.8
00757  *
00758  * \param pri_number PRI party number structure.
00759  * \param ast_number Asterisk party number structure.
00760  *
00761  * \return Nothing
00762  *
00763  * \note Assumes that pri_number has been previously memset to zero.
00764  */
00765 static void sig_pri_party_number_from_ast(struct pri_party_number *pri_number, const struct ast_party_number *ast_number)
00766 {
00767    if (!ast_number->valid) {
00768       return;
00769    }
00770    pri_number->valid = 1;
00771    pri_number->presentation = ast_to_pri_presentation(ast_number->presentation);
00772    pri_number->plan = ast_number->plan;
00773    if (!ast_strlen_zero(ast_number->str)) {
00774       ast_copy_string(pri_number->str, ast_number->str, sizeof(pri_number->str));
00775    }
00776 }
00777 
00778 /*!
00779  * \internal
00780  * \brief Fill in the PRI party id from the given asterisk party id.
00781  * \since 1.8
00782  *
00783  * \param pri_id PRI party id structure.
00784  * \param ast_id Asterisk party id structure.
00785  *
00786  * \return Nothing
00787  *
00788  * \note Assumes that pri_id has been previously memset to zero.
00789  */
00790 static void sig_pri_party_id_from_ast(struct pri_party_id *pri_id, const struct ast_party_id *ast_id)
00791 {
00792    sig_pri_party_name_from_ast(&pri_id->name, &ast_id->name);
00793    sig_pri_party_number_from_ast(&pri_id->number, &ast_id->number);
00794 #if defined(HAVE_PRI_SUBADDR)
00795    sig_pri_party_subaddress_from_ast(&pri_id->subaddress, &ast_id->subaddress);
00796 #endif   /* defined(HAVE_PRI_SUBADDR) */
00797 }
00798 
00799 /*!
00800  * \internal
00801  * \brief Update the PRI redirecting information for the current call.
00802  * \since 1.8
00803  *
00804  * \param pvt sig_pri private channel structure.
00805  * \param ast Asterisk channel
00806  *
00807  * \return Nothing
00808  *
00809  * \note Assumes that the PRI lock is already obtained.
00810  */
00811 static void sig_pri_redirecting_update(struct sig_pri_chan *pvt, struct ast_channel *ast)
00812 {
00813    struct pri_party_redirecting pri_redirecting;
00814 
00815 /*! \todo XXX Original called data can be put in a channel data store that is inherited. */
00816 
00817    memset(&pri_redirecting, 0, sizeof(pri_redirecting));
00818    sig_pri_party_id_from_ast(&pri_redirecting.from, &ast->redirecting.from);
00819    sig_pri_party_id_from_ast(&pri_redirecting.to, &ast->redirecting.to);
00820    pri_redirecting.count = ast->redirecting.count;
00821    pri_redirecting.reason = ast_to_pri_reason(ast->redirecting.reason);
00822 
00823    pri_redirecting_update(pvt->pri->pri, pvt->call, &pri_redirecting);
00824 }
00825 
00826 /*!
00827  * \internal
00828  * \brief Reset DTMF detector.
00829  * \since 1.8
00830  *
00831  * \param p sig_pri channel structure.
00832  *
00833  * \return Nothing
00834  */
00835 static void sig_pri_dsp_reset_and_flush_digits(struct sig_pri_chan *p)
00836 {
00837    if (p->calls->dsp_reset_and_flush_digits) {
00838       p->calls->dsp_reset_and_flush_digits(p->chan_pvt);
00839    }
00840 }
00841 
00842 static int sig_pri_set_echocanceller(struct sig_pri_chan *p, int enable)
00843 {
00844    if (p->calls->set_echocanceller)
00845       return p->calls->set_echocanceller(p->chan_pvt, enable);
00846    else
00847       return -1;
00848 }
00849 
00850 static void sig_pri_fixup_chans(struct sig_pri_chan *old_chan, struct sig_pri_chan *new_chan)
00851 {
00852    if (old_chan->calls->fixup_chans)
00853       old_chan->calls->fixup_chans(old_chan->chan_pvt, new_chan->chan_pvt);
00854 }
00855 
00856 static int sig_pri_play_tone(struct sig_pri_chan *p, enum sig_pri_tone tone)
00857 {
00858    if (p->calls->play_tone)
00859       return p->calls->play_tone(p->chan_pvt, tone);
00860    else
00861       return -1;
00862 }
00863 
00864 static struct ast_channel *sig_pri_new_ast_channel(struct sig_pri_chan *p, int state, int ulaw, int transfercapability, char *exten, const struct ast_channel *requestor)
00865 {
00866    struct ast_channel *c;
00867 
00868    if (p->calls->new_ast_channel)
00869       c = p->calls->new_ast_channel(p->chan_pvt, state, ulaw, exten, requestor);
00870    else
00871       return NULL;
00872 
00873    if (!p->owner)
00874       p->owner = c;
00875    p->isidlecall = 0;
00876    p->alreadyhungup = 0;
00877    c->transfercapability = transfercapability;
00878    pbx_builtin_setvar_helper(c, "TRANSFERCAPABILITY",
00879       ast_transfercapability2str(transfercapability));
00880    if (transfercapability & AST_TRANS_CAP_DIGITAL) {
00881       sig_pri_set_digital(p, 1);
00882    }
00883    if (p->pri) {
00884       ast_mutex_lock(&p->pri->lock);
00885       sig_pri_span_devstate_changed(p->pri);
00886       ast_mutex_unlock(&p->pri->lock);
00887    }
00888 
00889    return c;
00890 }
00891 
00892 /*!
00893  * \internal
00894  * \brief Open the PRI channel media path.
00895  * \since 1.8
00896  *
00897  * \param p Channel private control structure.
00898  *
00899  * \return Nothing
00900  */
00901 static void sig_pri_open_media(struct sig_pri_chan *p)
00902 {
00903    if (p->no_b_channel) {
00904       return;
00905    }
00906 
00907    if (p->calls->open_media) {
00908       p->calls->open_media(p->chan_pvt);
00909    }
00910 }
00911 
00912 struct ast_channel *sig_pri_request(struct sig_pri_chan *p, enum sig_pri_law law, const struct ast_channel *requestor, int transfercapability)
00913 {
00914    struct ast_channel *ast;
00915 
00916    ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
00917 
00918    p->outgoing = 1;
00919    ast = sig_pri_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability, p->exten, requestor);
00920    if (!ast) {
00921       p->outgoing = 0;
00922    }
00923    return ast;
00924 }
00925 
00926 int pri_is_up(struct sig_pri_span *pri)
00927 {
00928    int x;
00929    for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
00930       if (pri->dchanavail[x] == DCHAN_AVAILABLE)
00931          return 1;
00932    }
00933    return 0;
00934 }
00935 
00936 static char *pri_order(int level)
00937 {
00938    switch (level) {
00939    case 0:
00940       return "Primary";
00941    case 1:
00942       return "Secondary";
00943    case 2:
00944       return "Tertiary";
00945    case 3:
00946       return "Quaternary";
00947    default:
00948       return "<Unknown>";
00949    }
00950 }
00951 
00952 /* Returns index of the active dchan */
00953 static int pri_active_dchan_index(struct sig_pri_span *pri)
00954 {
00955    int x;
00956 
00957    for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
00958       if ((pri->dchans[x] == pri->pri))
00959          return x;
00960    }
00961 
00962    ast_log(LOG_WARNING, "No active dchan found!\n");
00963    return -1;
00964 }
00965 
00966 static int pri_find_dchan(struct sig_pri_span *pri)
00967 {
00968    int oldslot = -1;
00969    struct pri *old;
00970    int newslot = -1;
00971    int x;
00972    old = pri->pri;
00973    for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
00974       if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
00975          newslot = x;
00976       if (pri->dchans[x] == old) {
00977          oldslot = x;
00978       }
00979    }
00980    if (newslot < 0) {
00981       newslot = 0;
00982       /* This is annoying to see on non persistent layer 2 connections.  Let's not complain in that case */
00983       if (pri->sig != SIG_BRI_PTMP && !pri->no_d_channels) {
00984          pri->no_d_channels = 1;
00985          ast_log(LOG_WARNING,
00986             "Span %d: No D-channels available!  Using Primary channel as D-channel anyway!\n",
00987             pri->span);
00988       }
00989    } else {
00990       pri->no_d_channels = 0;
00991    }
00992    if (old && (oldslot != newslot))
00993       ast_log(LOG_NOTICE, "Switching from d-channel fd %d to fd %d!\n",
00994          pri->fds[oldslot], pri->fds[newslot]);
00995    pri->pri = pri->dchans[newslot];
00996    return 0;
00997 }
00998 
00999 /*!
01000  * \internal
01001  * \brief Obtain the sig_pri owner channel lock if the owner exists.
01002  * \since 1.8
01003  *
01004  * \param pri sig_pri PRI control structure.
01005  * \param chanpos Channel position in the span.
01006  *
01007  * \note Assumes the pri->lock is already obtained.
01008  * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
01009  *
01010  * \return Nothing
01011  */
01012 static void sig_pri_lock_owner(struct sig_pri_span *pri, int chanpos)
01013 {
01014    for (;;) {
01015       if (!pri->pvts[chanpos]->owner) {
01016          /* There is no owner lock to get. */
01017          break;
01018       }
01019       if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
01020          /* We got the lock */
01021          break;
01022       }
01023       /* We must unlock the PRI to avoid the possibility of a deadlock */
01024       ast_mutex_unlock(&pri->lock);
01025       PRI_DEADLOCK_AVOIDANCE(pri->pvts[chanpos]);
01026       ast_mutex_lock(&pri->lock);
01027    }
01028 }
01029 
01030 /*!
01031  * \internal
01032  * \brief Queue the given frame onto the owner channel.
01033  * \since 1.8
01034  *
01035  * \param pri sig_pri PRI control structure.
01036  * \param chanpos Channel position in the span.
01037  * \param frame Frame to queue onto the owner channel.
01038  *
01039  * \note Assumes the pri->lock is already obtained.
01040  * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
01041  *
01042  * \return Nothing
01043  */
01044 static void pri_queue_frame(struct sig_pri_span *pri, int chanpos, struct ast_frame *frame)
01045 {
01046    sig_pri_lock_owner(pri, chanpos);
01047    if (pri->pvts[chanpos]->owner) {
01048       ast_queue_frame(pri->pvts[chanpos]->owner, frame);
01049       ast_channel_unlock(pri->pvts[chanpos]->owner);
01050    }
01051 }
01052 
01053 /*!
01054  * \internal
01055  * \brief Queue a control frame of the specified subclass onto the owner channel.
01056  * \since 1.8
01057  *
01058  * \param pri sig_pri PRI control structure.
01059  * \param chanpos Channel position in the span.
01060  * \param subclass Control frame subclass to queue onto the owner channel.
01061  *
01062  * \note Assumes the pri->lock is already obtained.
01063  * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
01064  *
01065  * \return Nothing
01066  */
01067 static void pri_queue_control(struct sig_pri_span *pri, int chanpos, int subclass)
01068 {
01069    struct ast_frame f = {AST_FRAME_CONTROL, };
01070    struct sig_pri_chan *p = pri->pvts[chanpos];
01071 
01072    if (p->calls->queue_control) {
01073       p->calls->queue_control(p->chan_pvt, subclass);
01074    }
01075 
01076    f.subclass.integer = subclass;
01077    pri_queue_frame(pri, chanpos, &f);
01078 }
01079 
01080 /*!
01081  * \internal
01082  * \brief Find the private structure for the libpri call.
01083  *
01084  * \param pri Span controller structure.
01085  * \param channel LibPRI encoded channel ID.
01086  * \param call LibPRI opaque call pointer.
01087  *
01088  * \note Assumes the pri->lock is already obtained.
01089  *
01090  * \retval array-index into private pointer array on success.
01091  * \retval -1 on error.
01092  */
01093 static int pri_find_principle(struct sig_pri_span *pri, int channel, q931_call *call)
01094 {
01095    int x;
01096    int span;
01097    int principle;
01098    int prioffset;
01099 
01100    if (channel < 0) {
01101       /* Channel is not picked yet. */
01102       return -1;
01103    }
01104 
01105    prioffset = PRI_CHANNEL(channel);
01106    if (!prioffset || (channel & PRI_HELD_CALL)) {
01107       if (!call) {
01108          /* Cannot find a call waiting call or held call without a call. */
01109          return -1;
01110       }
01111       principle = -1;
01112       for (x = 0; x < pri->numchans; ++x) {
01113          if (pri->pvts[x]
01114             && pri->pvts[x]->call == call) {
01115             principle = x;
01116             break;
01117          }
01118       }
01119       return principle;
01120    }
01121 
01122    span = PRI_SPAN(channel);
01123    if (!(channel & PRI_EXPLICIT)) {
01124       int index;
01125 
01126       index = pri_active_dchan_index(pri);
01127       if (index == -1) {
01128          return -1;
01129       }
01130       span = pri->dchan_logical_span[index];
01131    }
01132 
01133    principle = -1;
01134    for (x = 0; x < pri->numchans; x++) {
01135       if (pri->pvts[x]
01136          && pri->pvts[x]->prioffset == prioffset
01137          && pri->pvts[x]->logicalspan == span
01138          && !pri->pvts[x]->no_b_channel) {
01139          principle = x;
01140          break;
01141       }
01142    }
01143 
01144    return principle;
01145 }
01146 
01147 /*!
01148  * \internal
01149  * \brief Fixup the private structure associated with the libpri call.
01150  *
01151  * \param pri Span controller structure.
01152  * \param principle Array-index into private array to move call to if not already there.
01153  * \param call LibPRI opaque call pointer to find if need to move call.
01154  *
01155  * \note Assumes the pri->lock is already obtained.
01156  *
01157  * \retval principle on success.
01158  * \retval -1 on error.
01159  */
01160 static int pri_fixup_principle(struct sig_pri_span *pri, int principle, q931_call *call)
01161 {
01162    int x;
01163 
01164    if (principle < 0 || pri->numchans <= principle) {
01165       /* Out of rannge */
01166       return -1;
01167    }
01168    if (!call) {
01169       /* No call */
01170       return principle;
01171    }
01172    if (pri->pvts[principle] && pri->pvts[principle]->call == call) {
01173       /* Call is already on the specified principle. */
01174       return principle;
01175    }
01176 
01177    /* Find the old principle location. */
01178    for (x = 0; x < pri->numchans; x++) {
01179       struct sig_pri_chan *new_chan;
01180       struct sig_pri_chan *old_chan;
01181 
01182       if (!pri->pvts[x] || pri->pvts[x]->call != call) {
01183          continue;
01184       }
01185 
01186       /* Found our call */
01187       new_chan = pri->pvts[principle];
01188       old_chan = pri->pvts[x];
01189 
01190       /* Get locks to safely move to the new private structure. */
01191       sig_pri_lock_private(old_chan);
01192       sig_pri_lock_owner(pri, x);
01193       sig_pri_lock_private(new_chan);
01194 
01195       ast_verb(3, "Moving call (%s) from channel %d to %d.\n",
01196          old_chan->owner ? old_chan->owner->name : "",
01197          old_chan->channel, new_chan->channel);
01198       if (new_chan->owner) {
01199          ast_log(LOG_WARNING,
01200             "Can't move call (%s) from channel %d to %d.  It is already in use.\n",
01201             old_chan->owner ? old_chan->owner->name : "",
01202             old_chan->channel, new_chan->channel);
01203          sig_pri_unlock_private(new_chan);
01204          if (old_chan->owner) {
01205             ast_channel_unlock(old_chan->owner);
01206          }
01207          sig_pri_unlock_private(old_chan);
01208          return -1;
01209       }
01210 
01211       sig_pri_fixup_chans(old_chan, new_chan);
01212 
01213       /* Fix it all up now */
01214       new_chan->owner = old_chan->owner;
01215       old_chan->owner = NULL;
01216 
01217       new_chan->call = old_chan->call;
01218       old_chan->call = NULL;
01219 
01220       /* Transfer flags from the old channel. */
01221 #if defined(HAVE_PRI_AOC_EVENTS)
01222       new_chan->aoc_s_request_invoke_id_valid = old_chan->aoc_s_request_invoke_id_valid;
01223       new_chan->waiting_for_aoce = old_chan->waiting_for_aoce;
01224       new_chan->holding_aoce = old_chan->holding_aoce;
01225 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
01226       new_chan->alerting = old_chan->alerting;
01227       new_chan->alreadyhungup = old_chan->alreadyhungup;
01228       new_chan->isidlecall = old_chan->isidlecall;
01229       new_chan->proceeding = old_chan->proceeding;
01230       new_chan->progress = old_chan->progress;
01231       new_chan->setup_ack = old_chan->setup_ack;
01232       new_chan->outgoing = old_chan->outgoing;
01233       new_chan->digital = old_chan->digital;
01234 #if defined(HAVE_PRI_CALL_WAITING)
01235       new_chan->is_call_waiting = old_chan->is_call_waiting;
01236 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
01237 
01238 #if defined(HAVE_PRI_AOC_EVENTS)
01239       old_chan->aoc_s_request_invoke_id_valid = 0;
01240       old_chan->waiting_for_aoce = 0;
01241       old_chan->holding_aoce = 0;
01242 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
01243       old_chan->alerting = 0;
01244       old_chan->alreadyhungup = 0;
01245       old_chan->isidlecall = 0;
01246       old_chan->proceeding = 0;
01247       old_chan->progress = 0;
01248       old_chan->setup_ack = 0;
01249       old_chan->outgoing = 0;
01250       old_chan->digital = 0;
01251 #if defined(HAVE_PRI_CALL_WAITING)
01252       old_chan->is_call_waiting = 0;
01253 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
01254 
01255       /* More stuff to transfer to the new channel. */
01256 #if defined(HAVE_PRI_REVERSE_CHARGE)
01257       new_chan->reverse_charging_indication = old_chan->reverse_charging_indication;
01258 #endif   /* defined(HAVE_PRI_REVERSE_CHARGE) */
01259 #if defined(HAVE_PRI_SETUP_KEYPAD)
01260       strcpy(new_chan->keypad_digits, old_chan->keypad_digits);
01261 #endif   /* defined(HAVE_PRI_SETUP_KEYPAD) */
01262 #if defined(HAVE_PRI_AOC_EVENTS)
01263       new_chan->aoc_s_request_invoke_id = old_chan->aoc_s_request_invoke_id;
01264       new_chan->aoc_e = old_chan->aoc_e;
01265 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
01266       strcpy(new_chan->user_tag, old_chan->user_tag);
01267 
01268       if (new_chan->no_b_channel) {
01269          /* Copy the real channel configuration to the no B channel interface. */
01270          new_chan->hidecallerid = old_chan->hidecallerid;
01271          new_chan->hidecalleridname = old_chan->hidecalleridname;
01272          new_chan->immediate = old_chan->immediate;
01273          new_chan->priexclusive = old_chan->priexclusive;
01274          new_chan->priindication_oob = old_chan->priindication_oob;
01275          new_chan->use_callerid = old_chan->use_callerid;
01276          new_chan->use_callingpres = old_chan->use_callingpres;
01277          new_chan->stripmsd = old_chan->stripmsd;
01278          strcpy(new_chan->context, old_chan->context);
01279          strcpy(new_chan->mohinterpret, old_chan->mohinterpret);
01280 
01281          /* Become a member of the old channel span/trunk-group. */
01282          new_chan->logicalspan = old_chan->logicalspan;
01283          new_chan->mastertrunkgroup = old_chan->mastertrunkgroup;
01284       } else if (old_chan->no_b_channel) {
01285          /*
01286           * We are transitioning from a held/call-waiting channel to a
01287           * real channel so we need to make sure that the media path is
01288           * open.  (Needed especially if the channel is natively
01289           * bridged.)
01290           */
01291          sig_pri_open_media(new_chan);
01292       }
01293 
01294       sig_pri_unlock_private(old_chan);
01295       if (new_chan->owner) {
01296          ast_channel_unlock(new_chan->owner);
01297       }
01298       sig_pri_unlock_private(new_chan);
01299 
01300       return principle;
01301    }
01302    ast_verb(3, "Call specified, but not found.\n");
01303    return -1;
01304 }
01305 
01306 static char * redirectingreason2str(int redirectingreason)
01307 {
01308    switch (redirectingreason) {
01309    case 0:
01310       return "UNKNOWN";
01311    case 1:
01312       return "BUSY";
01313    case 2:
01314       return "NO_REPLY";
01315    case 0xF:
01316       return "UNCONDITIONAL";
01317    default:
01318       return "NOREDIRECT";
01319    }
01320 }
01321 
01322 static char *dialplan2str(int dialplan)
01323 {
01324    if (dialplan == -1) {
01325       return("Dynamically set dialplan in ISDN");
01326    }
01327    return (pri_plan2str(dialplan));
01328 }
01329 
01330 static void apply_plan_to_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, const int plan)
01331 {
01332    switch (plan) {
01333    case PRI_INTERNATIONAL_ISDN:     /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */
01334       snprintf(buf, size, "%s%s", pri->internationalprefix, number);
01335       break;
01336    case PRI_NATIONAL_ISDN:       /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */
01337       snprintf(buf, size, "%s%s", pri->nationalprefix, number);
01338       break;
01339    case PRI_LOCAL_ISDN:       /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */
01340       snprintf(buf, size, "%s%s", pri->localprefix, number);
01341       break;
01342    case PRI_PRIVATE:       /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */
01343       snprintf(buf, size, "%s%s", pri->privateprefix, number);
01344       break;
01345    case PRI_UNKNOWN:       /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */
01346       snprintf(buf, size, "%s%s", pri->unknownprefix, number);
01347       break;
01348    default:          /* other Q.931 dialplan => don't twiddle with callingnum */
01349       snprintf(buf, size, "%s", number);
01350       break;
01351    }
01352 }
01353 
01354 /*! \note Assumes the pri->lock is already obtained. */
01355 static int pri_check_restart(struct sig_pri_span *pri)
01356 {
01357 #if defined(HAVE_PRI_SERVICE_MESSAGES)
01358 tryanotherpos:
01359 #endif   /* defined(HAVE_PRI_SERVICE_MESSAGES) */
01360    do {
01361       pri->resetpos++;
01362    } while (pri->resetpos < pri->numchans
01363       && (!pri->pvts[pri->resetpos]
01364          || pri->pvts[pri->resetpos]->no_b_channel
01365          || pri->pvts[pri->resetpos]->call
01366          || pri->pvts[pri->resetpos]->resetting));
01367    if (pri->resetpos < pri->numchans) {
01368 #if defined(HAVE_PRI_SERVICE_MESSAGES)
01369       unsigned why;
01370 
01371       why = pri->pvts[pri->resetpos]->service_status;
01372       if (why) {
01373          ast_log(LOG_NOTICE, "span '%d' channel '%d' out-of-service (reason: %s), not sending RESTART\n", pri->span,
01374             pri->pvts[pri->resetpos]->channel, (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
01375          goto tryanotherpos;
01376       }
01377 #endif   /* defined(HAVE_PRI_SERVICE_MESSAGES) */
01378 
01379       /* Mark the channel as resetting and restart it */
01380       pri->pvts[pri->resetpos]->resetting = 1;
01381       pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
01382    } else {
01383       pri->resetting = 0;
01384       time(&pri->lastreset);
01385    }
01386    return 0;
01387 }
01388 
01389 #if defined(HAVE_PRI_CALL_WAITING)
01390 /*!
01391  * \internal
01392  * \brief Init the private channel configuration using the span controller.
01393  * \since 1.8
01394  *
01395  * \param pvt Channel to init the configuration.
01396  * \param pri sig_pri PRI control structure.
01397  *
01398  * \note Assumes the pri->lock is already obtained.
01399  *
01400  * \return Nothing
01401  */
01402 static void sig_pri_init_config(struct sig_pri_chan *pvt, struct sig_pri_span *pri)
01403 {
01404    pvt->stripmsd = pri->ch_cfg.stripmsd;
01405    pvt->hidecallerid = pri->ch_cfg.hidecallerid;
01406    pvt->hidecalleridname = pri->ch_cfg.hidecalleridname;
01407    pvt->immediate = pri->ch_cfg.immediate;
01408    pvt->priexclusive = pri->ch_cfg.priexclusive;
01409    pvt->priindication_oob = pri->ch_cfg.priindication_oob;
01410    pvt->use_callerid = pri->ch_cfg.use_callerid;
01411    pvt->use_callingpres = pri->ch_cfg.use_callingpres;
01412    ast_copy_string(pvt->context, pri->ch_cfg.context, sizeof(pvt->context));
01413    ast_copy_string(pvt->mohinterpret, pri->ch_cfg.mohinterpret, sizeof(pvt->mohinterpret));
01414 
01415    if (pri->calls->init_config) {
01416       pri->calls->init_config(pvt->chan_pvt, pri);
01417    }
01418 }
01419 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
01420 
01421 static int pri_find_empty_chan(struct sig_pri_span *pri, int backwards)
01422 {
01423    int x;
01424    if (backwards)
01425       x = pri->numchans;
01426    else
01427       x = 0;
01428    for (;;) {
01429       if (backwards && (x < 0))
01430          break;
01431       if (!backwards && (x >= pri->numchans))
01432          break;
01433       if (pri->pvts[x]
01434          && !pri->pvts[x]->no_b_channel
01435          && !pri->pvts[x]->inalarm
01436          && !pri->pvts[x]->owner) {
01437          ast_debug(1, "Found empty available channel %d/%d\n",
01438             pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
01439          return x;
01440       }
01441       if (backwards)
01442          x--;
01443       else
01444          x++;
01445    }
01446    return -1;
01447 }
01448 
01449 #if defined(HAVE_PRI_CALL_HOLD)
01450 /*!
01451  * \internal
01452  * \brief Find or create an empty no-B-channel interface to use.
01453  * \since 1.8
01454  *
01455  * \param pri sig_pri span controller to find interface.
01456  *
01457  * \note Assumes the pri->lock is already obtained.
01458  *
01459  * \retval array-index into private pointer array on success.
01460  * \retval -1 on error.
01461  */
01462 static int pri_find_empty_nobch(struct sig_pri_span *pri)
01463 {
01464    int idx;
01465 
01466    for (idx = 0; idx < pri->numchans; ++idx) {
01467       if (pri->pvts[idx]
01468          && pri->pvts[idx]->no_b_channel
01469          && !pri->pvts[idx]->inalarm
01470          && !pri->pvts[idx]->owner) {
01471          ast_debug(1, "Found empty available no B channel interface\n");
01472          return idx;
01473       }
01474    }
01475 
01476    /* Need to create a new interface. */
01477    if (pri->calls->new_nobch_intf) {
01478       idx = pri->calls->new_nobch_intf(pri);
01479    } else {
01480       idx = -1;
01481    }
01482    return idx;
01483 }
01484 #endif   /* defined(HAVE_PRI_CALL_HOLD) */
01485 
01486 #if defined(HAVE_PRI_CALL_HOLD)
01487 /*!
01488  * \internal
01489  * \brief Find the channel associated with the libpri call.
01490  * \since 1.8
01491  *
01492  * \param pri sig_pri span controller to find interface.
01493  * \param call LibPRI opaque call pointer to find.
01494  *
01495  * \note Assumes the pri->lock is already obtained.
01496  *
01497  * \retval array-index into private pointer array on success.
01498  * \retval -1 on error.
01499  */
01500 static int pri_find_pri_call(struct sig_pri_span *pri, q931_call *call)
01501 {
01502    int idx;
01503 
01504    for (idx = 0; idx < pri->numchans; ++idx) {
01505       if (pri->pvts[idx] && pri->pvts[idx]->call == call) {
01506          /* Found the channel */
01507          return idx;
01508       }
01509    }
01510    return -1;
01511 }
01512 #endif   /* defined(HAVE_PRI_CALL_HOLD) */
01513 
01514 static void *do_idle_thread(void *v_pvt)
01515 {
01516    struct sig_pri_chan *pvt = v_pvt;
01517    struct ast_channel *chan = pvt->owner;
01518    struct ast_frame *f;
01519    char ex[80];
01520    /* Wait up to 30 seconds for an answer */
01521    int newms, ms = 30000;
01522 
01523    ast_verb(3, "Initiating idle call on channel %s\n", chan->name);
01524    snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
01525    if (ast_call(chan, ex, 0)) {
01526       ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
01527       ast_hangup(chan);
01528       return NULL;
01529    }
01530    while ((newms = ast_waitfor(chan, ms)) > 0) {
01531       f = ast_read(chan);
01532       if (!f) {
01533          /* Got hangup */
01534          break;
01535       }
01536       if (f->frametype == AST_FRAME_CONTROL) {
01537          switch (f->subclass.integer) {
01538          case AST_CONTROL_ANSWER:
01539             /* Launch the PBX */
01540             ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
01541             ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
01542             chan->priority = 1;
01543             ast_verb(4, "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
01544             ast_pbx_run(chan);
01545             /* It's already hungup, return immediately */
01546             return NULL;
01547          case AST_CONTROL_BUSY:
01548             ast_verb(4, "Idle channel '%s' busy, waiting...\n", chan->name);
01549             break;
01550          case AST_CONTROL_CONGESTION:
01551             ast_verb(4, "Idle channel '%s' congested, waiting...\n", chan->name);
01552             break;
01553          };
01554       }
01555       ast_frfree(f);
01556       ms = newms;
01557    }
01558    /* Hangup the channel since nothing happend */
01559    ast_hangup(chan);
01560    return NULL;
01561 }
01562 
01563 static void *pri_ss_thread(void *data)
01564 {
01565    struct sig_pri_chan *p = data;
01566    struct ast_channel *chan = p->owner;
01567    char exten[AST_MAX_EXTENSION];
01568    int res;
01569    int len;
01570    int timeout;
01571 
01572    if (!chan) {
01573       /* We lost the owner before we could get started. */
01574       return NULL;
01575    }
01576 
01577    /*
01578     * In the bizarre case where the channel has become a zombie before we
01579     * even get started here, abort safely.
01580     */
01581    if (!chan->tech_pvt) {
01582       ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
01583       ast_hangup(chan);
01584       return NULL;
01585    }
01586 
01587    ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
01588 
01589    sig_pri_dsp_reset_and_flush_digits(p);
01590 
01591    /* Now loop looking for an extension */
01592    ast_copy_string(exten, p->exten, sizeof(exten));
01593    len = strlen(exten);
01594    res = 0;
01595    while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
01596       if (len && !ast_ignore_pattern(chan->context, exten))
01597          sig_pri_play_tone(p, -1);
01598       else
01599          sig_pri_play_tone(p, SIG_PRI_TONE_DIALTONE);
01600       if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
01601          timeout = pri_matchdigittimeout;
01602       else
01603          timeout = pri_gendigittimeout;
01604       res = ast_waitfordigit(chan, timeout);
01605       if (res < 0) {
01606          ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
01607          ast_hangup(chan);
01608          return NULL;
01609       } else if (res) {
01610          exten[len++] = res;
01611          exten[len] = '\0';
01612       } else
01613          break;
01614    }
01615    /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
01616    if (ast_strlen_zero(exten)) {
01617       ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
01618       exten[0] = 's';
01619       exten[1] = '\0';
01620    } else {
01621       ast_free(chan->dialed.number.str);
01622       chan->dialed.number.str = ast_strdup(exten);
01623 
01624       if (p->pri->append_msn_to_user_tag && p->pri->nodetype != PRI_NETWORK) {
01625          /*
01626           * Update the user tag for party id's from this device for this call
01627           * now that we have a complete MSN from the network.
01628           */
01629          snprintf(p->user_tag, sizeof(p->user_tag), "%s_%s", p->pri->initial_user_tag,
01630             exten);
01631          ast_free(chan->caller.id.tag);
01632          chan->caller.id.tag = ast_strdup(p->user_tag);
01633       }
01634    }
01635    sig_pri_play_tone(p, -1);
01636    if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
01637       /* Start the real PBX */
01638       ast_copy_string(chan->exten, exten, sizeof(chan->exten));
01639       sig_pri_dsp_reset_and_flush_digits(p);
01640       if (p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) {
01641          sig_pri_lock_private(p);
01642          if (p->pri->pri) {      
01643             if (!pri_grab(p, p->pri)) {
01644                pri_proceeding(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 0);
01645                p->proceeding = 1;
01646                pri_rel(p->pri);
01647             } else {
01648                ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
01649             }
01650          }
01651          sig_pri_unlock_private(p);
01652       }
01653 
01654       sig_pri_set_echocanceller(p, 1);
01655       ast_setstate(chan, AST_STATE_RING);
01656       res = ast_pbx_run(chan);
01657       if (res) {
01658          ast_log(LOG_WARNING, "PBX exited non-zero!\n");
01659       }
01660    } else {
01661       ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
01662       chan->hangupcause = AST_CAUSE_UNALLOCATED;
01663       ast_hangup(chan);
01664       p->exten[0] = '\0';
01665       /* Since we send release complete here, we won't get one */
01666       p->call = NULL;
01667    }
01668    return NULL;
01669 }
01670 
01671 void pri_event_alarm(struct sig_pri_span *pri, int index, int before_start_pri)
01672 {
01673    pri->dchanavail[index] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
01674    if (!before_start_pri)
01675       pri_find_dchan(pri);
01676 }
01677 
01678 void pri_event_noalarm(struct sig_pri_span *pri, int index, int before_start_pri)
01679 {
01680    pri->dchanavail[index] |= DCHAN_NOTINALARM;
01681    if (!before_start_pri)
01682       pri_restart(pri->dchans[index]);
01683 }
01684 
01685 /*!
01686  * \internal
01687  * \brief Convert libpri party name into asterisk party name.
01688  * \since 1.8
01689  *
01690  * \param ast_name Asterisk party name structure to fill.  Must already be set initialized.
01691  * \param pri_name libpri party name structure containing source information.
01692  *
01693  * \note The filled in ast_name structure needs to be destroyed by
01694  * ast_party_name_free() when it is no longer needed.
01695  *
01696  * \return Nothing
01697  */
01698 static void sig_pri_party_name_convert(struct ast_party_name *ast_name, const struct pri_party_name *pri_name)
01699 {
01700    ast_name->str = ast_strdup(pri_name->str);
01701    ast_name->char_set = pri_to_ast_char_set(pri_name->char_set);
01702    ast_name->presentation = pri_to_ast_presentation(pri_name->presentation);
01703    ast_name->valid = 1;
01704 }
01705 
01706 /*!
01707  * \internal
01708  * \brief Convert libpri party number into asterisk party number.
01709  * \since 1.8
01710  *
01711  * \param ast_number Asterisk party number structure to fill.  Must already be set initialized.
01712  * \param pri_number libpri party number structure containing source information.
01713  * \param pri Span controlling structure.
01714  *
01715  * \note The filled in ast_number structure needs to be destroyed by
01716  * ast_party_number_free() when it is no longer needed.
01717  *
01718  * \return Nothing
01719  */
01720 static void sig_pri_party_number_convert(struct ast_party_number *ast_number, const struct pri_party_number *pri_number, struct sig_pri_span *pri)
01721 {
01722    char number[AST_MAX_EXTENSION];
01723 
01724    apply_plan_to_number(number, sizeof(number), pri, pri_number->str, pri_number->plan);
01725    ast_number->str = ast_strdup(number);
01726    ast_number->plan = pri_number->plan;
01727    ast_number->presentation = pri_to_ast_presentation(pri_number->presentation);
01728    ast_number->valid = 1;
01729 }
01730 
01731 /*!
01732  * \internal
01733  * \brief Convert libpri party id into asterisk party id.
01734  * \since 1.8
01735  *
01736  * \param ast_id Asterisk party id structure to fill.  Must already be set initialized.
01737  * \param pri_id libpri party id structure containing source information.
01738  * \param pri Span controlling structure.
01739  *
01740  * \note The filled in ast_id structure needs to be destroyed by
01741  * ast_party_id_free() when it is no longer needed.
01742  *
01743  * \return Nothing
01744  */
01745 static void sig_pri_party_id_convert(struct ast_party_id *ast_id, const struct pri_party_id *pri_id, struct sig_pri_span *pri)
01746 {
01747    if (pri_id->name.valid) {
01748       sig_pri_party_name_convert(&ast_id->name, &pri_id->name);
01749    }
01750    if (pri_id->number.valid) {
01751       sig_pri_party_number_convert(&ast_id->number, &pri_id->number, pri);
01752    }
01753 #if defined(HAVE_PRI_SUBADDR)
01754    if (pri_id->subaddress.valid) {
01755       sig_pri_set_subaddress(&ast_id->subaddress, &pri_id->subaddress);
01756    }
01757 #endif   /* defined(HAVE_PRI_SUBADDR) */
01758 }
01759 
01760 /*!
01761  * \internal
01762  * \brief Convert libpri redirecting information into asterisk redirecting information.
01763  * \since 1.8
01764  *
01765  * \param ast_redirecting Asterisk redirecting structure to fill.
01766  * \param pri_redirecting libpri redirecting structure containing source information.
01767  * \param ast_guide Asterisk redirecting structure to use as an initialization guide.
01768  * \param pri Span controlling structure.
01769  *
01770  * \note The filled in ast_redirecting structure needs to be destroyed by
01771  * ast_party_redirecting_free() when it is no longer needed.
01772  *
01773  * \return Nothing
01774  */
01775 static void sig_pri_redirecting_convert(struct ast_party_redirecting *ast_redirecting,
01776    const struct pri_party_redirecting *pri_redirecting,
01777    const struct ast_party_redirecting *ast_guide,
01778    struct sig_pri_span *pri)
01779 {
01780    ast_party_redirecting_set_init(ast_redirecting, ast_guide);
01781 
01782    sig_pri_party_id_convert(&ast_redirecting->from, &pri_redirecting->from, pri);
01783    sig_pri_party_id_convert(&ast_redirecting->to, &pri_redirecting->to, pri);
01784    ast_redirecting->count = pri_redirecting->count;
01785    ast_redirecting->reason = pri_to_ast_reason(pri_redirecting->reason);
01786 }
01787 
01788 /*!
01789  * \internal
01790  * \brief Determine if the given extension matches one of the MSNs in the pattern list.
01791  * \since 1.8
01792  *
01793  * \param msn_patterns Comma separated list of MSN patterns to match.
01794  * \param exten Extension to match in the MSN list.
01795  *
01796  * \retval 1 if matches.
01797  * \retval 0 if no match.
01798  */
01799 static int sig_pri_msn_match(const char *msn_patterns, const char *exten)
01800 {
01801    char *pattern;
01802    char *msn_list;
01803    char *list_tail;
01804 
01805    msn_list = ast_strdupa(msn_patterns);
01806 
01807    list_tail = NULL;
01808    pattern = strtok_r(msn_list, ",", &list_tail);
01809    while (pattern) {
01810       pattern = ast_strip(pattern);
01811       if (!ast_strlen_zero(pattern) && ast_extension_match(pattern, exten)) {
01812          /* Extension matched the pattern. */
01813          return 1;
01814       }
01815       pattern = strtok_r(NULL, ",", &list_tail);
01816    }
01817    /* Did not match any pattern in the list. */
01818    return 0;
01819 }
01820 
01821 #if defined(HAVE_PRI_MCID)
01822 /*!
01823  * \internal
01824  * \brief Append the given party id to the event string.
01825  * \since 1.8
01826  *
01827  * \param msg Event message string being built.
01828  * \param prefix Prefix to add to the party id lines.
01829  * \param party Party information to encode.
01830  *
01831  * \return Nothing
01832  */
01833 static void sig_pri_event_party_id(struct ast_str **msg, const char *prefix, struct ast_party_id *party)
01834 {
01835    int pres;
01836 
01837    /* Combined party presentation */
01838    pres = ast_party_id_presentation(party);
01839    ast_str_append(msg, 0, "%sPres: %d (%s)\r\n", prefix, pres,
01840       ast_describe_caller_presentation(pres));
01841 
01842    /* Party number */
01843    ast_str_append(msg, 0, "%sNumValid: %d\r\n", prefix,
01844       (unsigned) party->number.valid);
01845    ast_str_append(msg, 0, "%sNum: %s\r\n", prefix,
01846       S_COR(party->number.valid, party->number.str, ""));
01847    ast_str_append(msg, 0, "%ston: %d\r\n", prefix, party->number.plan);
01848    if (party->number.valid) {
01849       ast_str_append(msg, 0, "%sNumPlan: %d\r\n", prefix, party->number.plan);
01850       ast_str_append(msg, 0, "%sNumPres: %d (%s)\r\n", prefix,
01851          party->number.presentation,
01852          ast_describe_caller_presentation(party->number.presentation));
01853    }
01854 
01855    /* Party name */
01856    ast_str_append(msg, 0, "%sNameValid: %d\r\n", prefix,
01857       (unsigned) party->name.valid);
01858    ast_str_append(msg, 0, "%sName: %s\r\n", prefix,
01859       S_COR(party->name.valid, party->name.str, ""));
01860    if (party->name.valid) {
01861       ast_str_append(msg, 0, "%sNameCharSet: %s\r\n", prefix,
01862          ast_party_name_charset_describe(party->name.char_set));
01863       ast_str_append(msg, 0, "%sNamePres: %d (%s)\r\n", prefix,
01864          party->name.presentation,
01865          ast_describe_caller_presentation(party->name.presentation));
01866    }
01867 
01868 #if defined(HAVE_PRI_SUBADDR)
01869    /* Party subaddress */
01870    if (party->subaddress.valid) {
01871       static const char subaddress[] = "Subaddr";
01872 
01873       ast_str_append(msg, 0, "%s%s: %s\r\n", prefix, subaddress,
01874          S_OR(party->subaddress.str, ""));
01875       ast_str_append(msg, 0, "%s%sType: %d\r\n", prefix, subaddress,
01876          party->subaddress.type);
01877       ast_str_append(msg, 0, "%s%sOdd: %d\r\n", prefix, subaddress,
01878          party->subaddress.odd_even_indicator);
01879    }
01880 #endif   /* defined(HAVE_PRI_SUBADDR) */
01881 }
01882 #endif   /* defined(HAVE_PRI_MCID) */
01883 
01884 #if defined(HAVE_PRI_MCID)
01885 /*!
01886  * \internal
01887  * \brief Handle the MCID event.
01888  * \since 1.8
01889  *
01890  * \param pri sig_pri PRI control structure.
01891  * \param mcid MCID event parameters.
01892  * \param owner Asterisk channel associated with the call.
01893  * NULL if Asterisk no longer has the ast_channel struct.
01894  *
01895  * \note Assumes the pri->lock is already obtained.
01896  * \note Assumes the owner channel lock is already obtained if still present.
01897  *
01898  * \return Nothing
01899  */
01900 static void sig_pri_mcid_event(struct sig_pri_span *pri, const struct pri_subcmd_mcid_req *mcid, struct ast_channel *owner)
01901 {
01902    struct ast_channel *chans[1];
01903    struct ast_str *msg;
01904    struct ast_party_id party;
01905 
01906    msg = ast_str_create(4096);
01907    if (!msg) {
01908       return;
01909    }
01910 
01911    if (owner) {
01912       /* The owner channel is present. */
01913       ast_str_append(&msg, 0, "Channel: %s\r\n", owner->name);
01914       ast_str_append(&msg, 0, "UniqueID: %s\r\n", owner->uniqueid);
01915 
01916       sig_pri_event_party_id(&msg, "CallerID", &owner->connected.id);
01917    } else {
01918       /*
01919        * Since we no longer have an owner channel,
01920        * we have to use the caller information supplied by libpri.
01921        */
01922       ast_party_id_init(&party);
01923       sig_pri_party_id_convert(&party, &mcid->originator, pri);
01924       sig_pri_event_party_id(&msg, "CallerID", &party);
01925       ast_party_id_free(&party);
01926    }
01927 
01928    /* Always use libpri's called party information. */
01929    ast_party_id_init(&party);
01930    sig_pri_party_id_convert(&party, &mcid->answerer, pri);
01931    sig_pri_event_party_id(&msg, "ConnectedID", &party);
01932    ast_party_id_free(&party);
01933 
01934    chans[0] = owner;
01935    ast_manager_event_multichan(EVENT_FLAG_CALL, "MCID", owner ? 1 : 0, chans, "%s",
01936       ast_str_buffer(msg));
01937    ast_free(msg);
01938 }
01939 #endif   /* defined(HAVE_PRI_MCID) */
01940 
01941 #if defined(HAVE_PRI_TRANSFER)
01942 struct xfer_rsp_data {
01943    struct sig_pri_span *pri;
01944    /*! Call to send transfer success/fail response over. */
01945    q931_call *call;
01946    /*! Invocation ID to use when sending a reply to the transfer request. */
01947    int invoke_id;
01948 };
01949 #endif   /* defined(HAVE_PRI_TRANSFER) */
01950 
01951 #if defined(HAVE_PRI_TRANSFER)
01952 /*!
01953  * \internal
01954  * \brief Send the transfer success/fail response message.
01955  * \since 1.8
01956  *
01957  * \param data Callback user data pointer
01958  * \param is_successful TRUE if the transfer was successful.
01959  *
01960  * \return Nothing
01961  */
01962 static void sig_pri_transfer_rsp(void *data, int is_successful)
01963 {
01964    struct xfer_rsp_data *rsp = data;
01965 
01966    pri_transfer_rsp(rsp->pri->pri, rsp->call, rsp->invoke_id, is_successful);
01967 }
01968 #endif   /* defined(HAVE_PRI_TRANSFER) */
01969 
01970 #if defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER)
01971 /*!
01972  * \brief Protocol callback to indicate if transfer will happen.
01973  * \since 1.8
01974  *
01975  * \param data Callback user data pointer
01976  * \param is_successful TRUE if the transfer will happen.
01977  *
01978  * \return Nothing
01979  */
01980 typedef void (*xfer_rsp_callback)(void *data, int is_successful);
01981 #endif   /* defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER) */
01982 
01983 #if defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER)
01984 /*!
01985  * \internal
01986  * \brief Attempt to transfer the two calls to each other.
01987  * \since 1.8
01988  *
01989  * \param pri sig_pri PRI control structure.
01990  * \param call_1_pri First call involved in the transfer. (transferee; usually on hold)
01991  * \param call_1_held TRUE if call_1_pri is on hold.
01992  * \param call_2_pri Second call involved in the transfer. (target; usually active/ringing)
01993  * \param call_2_held TRUE if call_2_pri is on hold.
01994  * \param rsp_callback Protocol callback to indicate if transfer will happen. NULL if not used.
01995  * \param data Callback user data pointer
01996  *
01997  * \note Assumes the pri->lock is already obtained.
01998  *
01999  * \retval 0 on success.
02000  * \retval -1 on error.
02001  */
02002 static int sig_pri_attempt_transfer(struct sig_pri_span *pri, q931_call *call_1_pri, int call_1_held, q931_call *call_2_pri, int call_2_held, xfer_rsp_callback rsp_callback, void *data)
02003 {
02004    struct attempt_xfer_call {
02005       q931_call *pri;
02006       struct ast_channel *ast;
02007       int held;
02008       int chanpos;
02009    };
02010    int retval;
02011    struct ast_channel *transferee;
02012    struct attempt_xfer_call *call_1;
02013    struct attempt_xfer_call *call_2;
02014    struct attempt_xfer_call *swap_call;
02015    struct attempt_xfer_call c1;
02016    struct attempt_xfer_call c2;
02017 
02018    c1.pri = call_1_pri;
02019    c1.held = call_1_held;
02020    call_1 = &c1;
02021 
02022    c2.pri = call_2_pri;
02023    c2.held = call_2_held;
02024    call_2 = &c2;
02025 
02026    call_1->chanpos = pri_find_pri_call(pri, call_1->pri);
02027    call_2->chanpos = pri_find_pri_call(pri, call_2->pri);
02028    if (call_1->chanpos < 0 || call_2->chanpos < 0) {
02029       /* Calls not found in span control. */
02030       if (rsp_callback) {
02031          /* Transfer failed. */
02032          rsp_callback(data, 0);
02033       }
02034       return -1;
02035    }
02036 
02037    /* Attempt to make transferee and target consistent. */
02038    if (!call_1->held && call_2->held) {
02039       /*
02040        * Swap call_1 and call_2 to make call_1 the transferee(held call)
02041        * and call_2 the target(active call).
02042        */
02043       swap_call = call_1;
02044       call_1 = call_2;
02045       call_2 = swap_call;
02046    }
02047 
02048    /* Deadlock avoidance is attempted. */
02049    sig_pri_lock_private(pri->pvts[call_1->chanpos]);
02050    sig_pri_lock_owner(pri, call_1->chanpos);
02051    sig_pri_lock_private(pri->pvts[call_2->chanpos]);
02052    sig_pri_lock_owner(pri, call_2->chanpos);
02053 
02054    call_1->ast = pri->pvts[call_1->chanpos]->owner;
02055    call_2->ast = pri->pvts[call_2->chanpos]->owner;
02056    if (!call_1->ast || !call_2->ast) {
02057       /* At least one owner is not present. */
02058       if (call_1->ast) {
02059          ast_channel_unlock(call_1->ast);
02060       }
02061       if (call_2->ast) {
02062          ast_channel_unlock(call_2->ast);
02063       }
02064       sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
02065       sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
02066       if (rsp_callback) {
02067          /* Transfer failed. */
02068          rsp_callback(data, 0);
02069       }
02070       return -1;
02071    }
02072 
02073    for (;;) {
02074       transferee = ast_bridged_channel(call_1->ast);
02075       if (transferee) {
02076          break;
02077       }
02078 
02079       /* Try masquerading the other way. */
02080       swap_call = call_1;
02081       call_1 = call_2;
02082       call_2 = swap_call;
02083 
02084       transferee = ast_bridged_channel(call_1->ast);
02085       if (transferee) {
02086          break;
02087       }
02088 
02089       /* Could not transfer.  Neither call is bridged. */
02090       ast_channel_unlock(call_1->ast);
02091       ast_channel_unlock(call_2->ast);
02092       sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
02093       sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
02094 
02095       if (rsp_callback) {
02096          /* Transfer failed. */
02097          rsp_callback(data, 0);
02098       }
02099       return -1;
02100    }
02101 
02102    ast_verb(3, "TRANSFERRING %s to %s\n", call_1->ast->name, call_2->ast->name);
02103 
02104    /*
02105     * Setup transfer masquerade.
02106     *
02107     * Note:  There is an extremely nasty deadlock avoidance issue
02108     * with ast_channel_transfer_masquerade().  Deadlock may be possible if
02109     * the channels involved are proxies (chan_agent channels) and
02110     * it is called with locks.  Unfortunately, there is no simple
02111     * or even merely difficult way to guarantee deadlock avoidance
02112     * and still be able to send an ECT success response without the
02113     * possibility of the bridged channel hanging up on us.
02114     */
02115    ast_mutex_unlock(&pri->lock);
02116    retval = ast_channel_transfer_masquerade(
02117       call_2->ast,
02118       &call_2->ast->connected,
02119       call_2->held,
02120       transferee,
02121       &call_1->ast->connected,
02122       call_1->held);
02123 
02124    /* Reacquire the pri->lock to hold off completion of the transfer masquerade. */
02125    ast_mutex_lock(&pri->lock);
02126 
02127    ast_channel_unlock(call_1->ast);
02128    ast_channel_unlock(call_2->ast);
02129    sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
02130    sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
02131 
02132    if (rsp_callback) {
02133       /*
02134        * Report transfer status.
02135        *
02136        * Must do the callback before the masquerade completes to ensure
02137        * that the protocol message goes out before the call leg is
02138        * disconnected.
02139        */
02140       rsp_callback(data, retval ? 0 : 1);
02141    }
02142    return retval;
02143 }
02144 #endif   /* defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER) */
02145 
02146 #if defined(HAVE_PRI_CCSS)
02147 /*!
02148  * \internal
02149  * \brief Compare the CC agent private data by libpri cc_id.
02150  * \since 1.8
02151  *
02152  * \param obj pointer to the (user-defined part) of an object.
02153  * \param arg callback argument from ao2_callback()
02154  * \param flags flags from ao2_callback()
02155  *
02156  * \return values are a combination of enum _cb_results.
02157  */
02158 static int sig_pri_cc_agent_cmp_cc_id(void *obj, void *arg, int flags)
02159 {
02160    struct ast_cc_agent *agent_1 = obj;
02161    struct sig_pri_cc_agent_prv *agent_prv_1 = agent_1->private_data;
02162    struct sig_pri_cc_agent_prv *agent_prv_2 = arg;
02163 
02164    return (agent_prv_1 && agent_prv_1->pri == agent_prv_2->pri
02165       && agent_prv_1->cc_id == agent_prv_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
02166 }
02167 #endif   /* defined(HAVE_PRI_CCSS) */
02168 
02169 #if defined(HAVE_PRI_CCSS)
02170 /*!
02171  * \internal
02172  * \brief Find the CC agent by libpri cc_id.
02173  * \since 1.8
02174  *
02175  * \param pri sig_pri PRI control structure.
02176  * \param cc_id CC record ID to find.
02177  *
02178  * \note
02179  * Since agents are refcounted, and this function returns
02180  * a reference to the agent, it is imperative that you decrement
02181  * the refcount of the agent once you have finished using it.
02182  *
02183  * \retval agent on success.
02184  * \retval NULL not found.
02185  */
02186 static struct ast_cc_agent *sig_pri_find_cc_agent_by_cc_id(struct sig_pri_span *pri, long cc_id)
02187 {
02188    struct sig_pri_cc_agent_prv finder = {
02189       .pri = pri,
02190       .cc_id = cc_id,
02191    };
02192 
02193    return ast_cc_agent_callback(0, sig_pri_cc_agent_cmp_cc_id, &finder,
02194       sig_pri_cc_type_name);
02195 }
02196 #endif   /* defined(HAVE_PRI_CCSS) */
02197 
02198 #if defined(HAVE_PRI_CCSS)
02199 /*!
02200  * \internal
02201  * \brief Compare the CC monitor instance by libpri cc_id.
02202  * \since 1.8
02203  *
02204  * \param obj pointer to the (user-defined part) of an object.
02205  * \param arg callback argument from ao2_callback()
02206  * \param flags flags from ao2_callback()
02207  *
02208  * \return values are a combination of enum _cb_results.
02209  */
02210 static int sig_pri_cc_monitor_cmp_cc_id(void *obj, void *arg, int flags)
02211 {
02212    struct sig_pri_cc_monitor_instance *monitor_1 = obj;
02213    struct sig_pri_cc_monitor_instance *monitor_2 = arg;
02214 
02215    return (monitor_1->pri == monitor_2->pri
02216       && monitor_1->cc_id == monitor_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
02217 }
02218 #endif   /* defined(HAVE_PRI_CCSS) */
02219 
02220 #if defined(HAVE_PRI_CCSS)
02221 /*!
02222  * \internal
02223  * \brief Find the CC monitor instance by libpri cc_id.
02224  * \since 1.8
02225  *
02226  * \param pri sig_pri PRI control structure.
02227  * \param cc_id CC record ID to find.
02228  *
02229  * \note
02230  * Since monitor_instances are refcounted, and this function returns
02231  * a reference to the instance, it is imperative that you decrement
02232  * the refcount of the instance once you have finished using it.
02233  *
02234  * \retval monitor_instance on success.
02235  * \retval NULL not found.
02236  */
02237 static struct sig_pri_cc_monitor_instance *sig_pri_find_cc_monitor_by_cc_id(struct sig_pri_span *pri, long cc_id)
02238 {
02239    struct sig_pri_cc_monitor_instance finder = {
02240       .pri = pri,
02241       .cc_id = cc_id,
02242    };
02243 
02244    return ao2_callback(sig_pri_cc_monitors, 0, sig_pri_cc_monitor_cmp_cc_id, &finder);
02245 }
02246 #endif   /* defined(HAVE_PRI_CCSS) */
02247 
02248 #if defined(HAVE_PRI_CCSS)
02249 /*!
02250  * \internal
02251  * \brief Destroy the given monitor instance.
02252  * \since 1.8
02253  *
02254  * \param data Monitor instance to destroy.
02255  *
02256  * \return Nothing
02257  */
02258 static void sig_pri_cc_monitor_instance_destroy(void *data)
02259 {
02260    struct sig_pri_cc_monitor_instance *monitor_instance = data;
02261 
02262    if (monitor_instance->cc_id != -1) {
02263       ast_mutex_lock(&monitor_instance->pri->lock);
02264       pri_cc_cancel(monitor_instance->pri->pri, monitor_instance->cc_id);
02265       ast_mutex_unlock(&monitor_instance->pri->lock);
02266    }
02267    monitor_instance->pri->calls->module_unref();
02268 }
02269 #endif   /* defined(HAVE_PRI_CCSS) */
02270 
02271 #if defined(HAVE_PRI_CCSS)
02272 /*!
02273  * \internal
02274  * \brief Construct a new monitor instance.
02275  * \since 1.8
02276  *
02277  * \param core_id CC core ID.
02278  * \param pri sig_pri PRI control structure.
02279  * \param cc_id CC record ID.
02280  * \param device_name Name of device (Asterisk channel name less sequence number).
02281  *
02282  * \note
02283  * Since monitor_instances are refcounted, and this function returns
02284  * a reference to the instance, it is imperative that you decrement
02285  * the refcount of the instance once you have finished using it.
02286  *
02287  * \retval monitor_instance on success.
02288  * \retval NULL on error.
02289  */
02290 static struct sig_pri_cc_monitor_instance *sig_pri_cc_monitor_instance_init(int core_id, struct sig_pri_span *pri, long cc_id, const char *device_name)
02291 {
02292    struct sig_pri_cc_monitor_instance *monitor_instance;
02293 
02294    if (!pri->calls->module_ref || !pri->calls->module_unref) {
02295       return NULL;
02296    }
02297 
02298    monitor_instance = ao2_alloc(sizeof(*monitor_instance) + strlen(device_name),
02299       sig_pri_cc_monitor_instance_destroy);
02300    if (!monitor_instance) {
02301       return NULL;
02302    }
02303 
02304    monitor_instance->cc_id = cc_id;
02305    monitor_instance->pri = pri;
02306    monitor_instance->core_id = core_id;
02307    strcpy(monitor_instance->name, device_name);
02308 
02309    pri->calls->module_ref();
02310 
02311    ao2_link(sig_pri_cc_monitors, monitor_instance);
02312    return monitor_instance;
02313 }
02314 #endif   /* defined(HAVE_PRI_CCSS) */
02315 
02316 #if defined(HAVE_PRI_CCSS)
02317 /*!
02318  * \internal
02319  * \brief Announce to the CC core that protocol CC monitor is available for this call.
02320  * \since 1.8
02321  *
02322  * \param pri sig_pri PRI control structure.
02323  * \param chanpos Channel position in the span.
02324  * \param cc_id CC record ID.
02325  * \param service CCBS/CCNR indication.
02326  *
02327  * \note Assumes the pri->lock is already obtained.
02328  * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
02329  * \note Assumes the sig_pri_lock_owner(pri, chanpos) is already obtained.
02330  *
02331  * \retval 0 on success.
02332  * \retval -1 on error.
02333  */
02334 static int sig_pri_cc_available(struct sig_pri_span *pri, int chanpos, long cc_id, enum ast_cc_service_type service)
02335 {
02336    struct sig_pri_chan *pvt;
02337    struct ast_cc_config_params *cc_params;
02338    struct sig_pri_cc_monitor_instance *monitor;
02339    enum ast_cc_monitor_policies monitor_policy;
02340    int core_id;
02341    int res;
02342    char device_name[AST_CHANNEL_NAME];
02343    char dialstring[AST_CHANNEL_NAME];
02344 
02345    pvt = pri->pvts[chanpos];
02346 
02347    core_id = ast_cc_get_current_core_id(pvt->owner);
02348    if (core_id == -1) {
02349       return -1;
02350    }
02351 
02352    cc_params = ast_channel_get_cc_config_params(pvt->owner);
02353    if (!cc_params) {
02354       return -1;
02355    }
02356 
02357    res = -1;
02358    monitor_policy = ast_get_cc_monitor_policy(cc_params);
02359    switch (monitor_policy) {
02360    case AST_CC_MONITOR_NEVER:
02361       /* CCSS is not enabled. */
02362       break;
02363    case AST_CC_MONITOR_NATIVE:
02364    case AST_CC_MONITOR_ALWAYS:
02365       /*
02366        * If it is AST_CC_MONITOR_ALWAYS and native fails we will attempt the fallback
02367        * later in the call to sig_pri_cc_generic_check().
02368        */
02369       ast_channel_get_device_name(pvt->owner, device_name, sizeof(device_name));
02370       sig_pri_make_cc_dialstring(pvt, dialstring, sizeof(dialstring));
02371       monitor = sig_pri_cc_monitor_instance_init(core_id, pri, cc_id, device_name);
02372       if (!monitor) {
02373          break;
02374       }
02375       res = ast_queue_cc_frame(pvt->owner, sig_pri_cc_type_name, dialstring, service,
02376          monitor);
02377       if (res) {
02378          monitor->cc_id = -1;
02379          ao2_unlink(sig_pri_cc_monitors, monitor);
02380          ao2_ref(monitor, -1);
02381       }
02382       break;
02383    case AST_CC_MONITOR_GENERIC:
02384       ast_queue_cc_frame(pvt->owner, AST_CC_GENERIC_MONITOR_TYPE,
02385          sig_pri_get_orig_dialstring(pvt), service, NULL);
02386       /* Say it failed to force caller to cancel native CC. */
02387       break;
02388    }
02389    return res;
02390 }
02391 #endif   /* defined(HAVE_PRI_CCSS) */
02392 
02393 /*!
02394  * \internal
02395  * \brief Check if generic CC monitor is needed and request it.
02396  * \since 1.8
02397  *
02398  * \param pri sig_pri PRI control structure.
02399  * \param chanpos Channel position in the span.
02400  * \param service CCBS/CCNR indication.
02401  *
02402  * \note Assumes the pri->lock is already obtained.
02403  * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
02404  *
02405  * \return Nothing
02406  */
02407 static void sig_pri_cc_generic_check(struct sig_pri_span *pri, int chanpos, enum ast_cc_service_type service)
02408 {
02409    struct ast_channel *owner;
02410    struct ast_cc_config_params *cc_params;
02411 #if defined(HAVE_PRI_CCSS)
02412    struct ast_cc_monitor *monitor;
02413    char device_name[AST_CHANNEL_NAME];
02414 #endif   /* defined(HAVE_PRI_CCSS) */
02415    enum ast_cc_monitor_policies monitor_policy;
02416    int core_id;
02417 
02418    if (!pri->pvts[chanpos]->outgoing) {
02419       /* This is not an outgoing call so it cannot be CC monitor. */
02420       return;
02421    }
02422 
02423    sig_pri_lock_owner(pri, chanpos);
02424    owner = pri->pvts[chanpos]->owner;
02425    if (!owner) {
02426       return;
02427    }
02428    core_id = ast_cc_get_current_core_id(owner);
02429    if (core_id == -1) {
02430       /* No CC core setup */
02431       goto done;
02432    }
02433 
02434    cc_params = ast_channel_get_cc_config_params(owner);
02435    if (!cc_params) {
02436       /* Could not get CC config parameters. */
02437       goto done;
02438    }
02439 
02440 #if defined(HAVE_PRI_CCSS)
02441    ast_channel_get_device_name(owner, device_name, sizeof(device_name));
02442    monitor = ast_cc_get_monitor_by_recall_core_id(core_id, device_name);
02443    if (monitor) {
02444       /* CC monitor is already present so no need for generic CC. */
02445       ao2_ref(monitor, -1);
02446       goto done;
02447    }
02448 #endif   /* defined(HAVE_PRI_CCSS) */
02449 
02450    monitor_policy = ast_get_cc_monitor_policy(cc_params);
02451    switch (monitor_policy) {
02452    case AST_CC_MONITOR_NEVER:
02453       /* CCSS is not enabled. */
02454       break;
02455    case AST_CC_MONITOR_NATIVE:
02456       if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
02457          /* Request generic CC monitor. */
02458          ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
02459             sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
02460       }
02461       break;
02462    case AST_CC_MONITOR_ALWAYS:
02463       if (pri->sig == SIG_BRI_PTMP && pri->nodetype != PRI_NETWORK) {
02464          /*
02465           * Cannot monitor PTMP TE side since this is not defined.
02466           * We are playing the roll of a phone in this case and
02467           * a phone cannot monitor a party over the network without
02468           * protocol help.
02469           */
02470          break;
02471       }
02472       /*
02473        * We are either falling back or this is a PTMP NT span.
02474        * Request generic CC monitor.
02475        */
02476       ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
02477          sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
02478       break;
02479    case AST_CC_MONITOR_GENERIC:
02480       if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
02481          /* Request generic CC monitor. */
02482          ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
02483             sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
02484       }
02485       break;
02486    }
02487 
02488 done:
02489    ast_channel_unlock(owner);
02490 }
02491 
02492 #if defined(HAVE_PRI_CCSS)
02493 /*!
02494  * \internal
02495  * \brief The CC link canceled the CC instance.
02496  * \since 1.8
02497  *
02498  * \param pri sig_pri PRI control structure.
02499  * \param cc_id CC record ID.
02500  * \param is_agent TRUE if the cc_id is for an agent.
02501  *
02502  * \return Nothing
02503  */
02504 static void sig_pri_cc_link_canceled(struct sig_pri_span *pri, long cc_id, int is_agent)
02505 {
02506    if (is_agent) {
02507       struct ast_cc_agent *agent;
02508 
02509       agent = sig_pri_find_cc_agent_by_cc_id(pri, cc_id);
02510       if (!agent) {
02511          return;
02512       }
02513       ast_cc_failed(agent->core_id, "%s agent got canceled by link",
02514          sig_pri_cc_type_name);
02515       ao2_ref(agent, -1);
02516    } else {
02517       struct sig_pri_cc_monitor_instance *monitor;
02518 
02519       monitor = sig_pri_find_cc_monitor_by_cc_id(pri, cc_id);
02520       if (!monitor) {
02521          return;
02522       }
02523       monitor->cc_id = -1;
02524       ast_cc_monitor_failed(monitor->core_id, monitor->name,
02525          "%s monitor got canceled by link", sig_pri_cc_type_name);
02526       ao2_ref(monitor, -1);
02527    }
02528 }
02529 #endif   /* defined(HAVE_PRI_CCSS) */
02530 
02531 #if defined(HAVE_PRI_AOC_EVENTS)
02532 /*!
02533  * \internal
02534  * \brief Convert ast_aoc_charged_item to PRI_AOC_CHARGED_ITEM .
02535  * \since 1.8
02536  *
02537  * \param value Value to convert to string.
02538  *
02539  * \return PRI_AOC_CHARGED_ITEM
02540  */
02541 static enum PRI_AOC_CHARGED_ITEM sig_pri_aoc_charged_item_to_pri(enum PRI_AOC_CHARGED_ITEM value)
02542 {
02543    switch (value) {
02544    case AST_AOC_CHARGED_ITEM_NA:
02545       return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
02546    case AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
02547       return PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
02548    case AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
02549       return PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
02550    case AST_AOC_CHARGED_ITEM_CALL_ATTEMPT:
02551       return PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT;
02552    case AST_AOC_CHARGED_ITEM_CALL_SETUP:
02553       return PRI_AOC_CHARGED_ITEM_CALL_SETUP;
02554    case AST_AOC_CHARGED_ITEM_USER_USER_INFO:
02555       return PRI_AOC_CHARGED_ITEM_USER_USER_INFO;
02556    case AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
02557       return PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
02558    }
02559    return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
02560 }
02561 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
02562 
02563 #if defined(HAVE_PRI_AOC_EVENTS)
02564 /*!
02565  * \internal
02566  * \brief Convert PRI_AOC_CHARGED_ITEM to ast_aoc_charged_item.
02567  * \since 1.8
02568  *
02569  * \param value Value to convert to string.
02570  *
02571  * \return ast_aoc_charged_item
02572  */
02573 static enum ast_aoc_s_charged_item sig_pri_aoc_charged_item_to_ast(enum PRI_AOC_CHARGED_ITEM value)
02574 {
02575    switch (value) {
02576    case PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE:
02577       return AST_AOC_CHARGED_ITEM_NA;
02578    case PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
02579       return AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
02580    case PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
02581       return AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
02582    case PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT:
02583       return AST_AOC_CHARGED_ITEM_CALL_ATTEMPT;
02584    case PRI_AOC_CHARGED_ITEM_CALL_SETUP:
02585       return AST_AOC_CHARGED_ITEM_CALL_SETUP;
02586    case PRI_AOC_CHARGED_ITEM_USER_USER_INFO:
02587       return AST_AOC_CHARGED_ITEM_USER_USER_INFO;
02588    case PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
02589       return AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
02590    }
02591    return AST_AOC_CHARGED_ITEM_NA;
02592 }
02593 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
02594 
02595 #if defined(HAVE_PRI_AOC_EVENTS)
02596 /*!
02597  * \internal
02598  * \brief Convert AST_AOC_MULTIPLER to PRI_AOC_MULTIPLIER.
02599  * \since 1.8
02600  *
02601  * \return pri enum equivalent.
02602  */
02603 static int sig_pri_aoc_multiplier_from_ast(enum ast_aoc_currency_multiplier mult)
02604 {
02605    switch (mult) {
02606    case AST_AOC_MULT_ONETHOUSANDTH:
02607       return PRI_AOC_MULTIPLIER_THOUSANDTH;
02608    case AST_AOC_MULT_ONEHUNDREDTH:
02609       return PRI_AOC_MULTIPLIER_HUNDREDTH;
02610    case AST_AOC_MULT_ONETENTH:
02611       return PRI_AOC_MULTIPLIER_TENTH;
02612    case AST_AOC_MULT_ONE:
02613       return PRI_AOC_MULTIPLIER_ONE;
02614    case AST_AOC_MULT_TEN:
02615       return PRI_AOC_MULTIPLIER_TEN;
02616    case AST_AOC_MULT_HUNDRED:
02617       return PRI_AOC_MULTIPLIER_HUNDRED;
02618    case AST_AOC_MULT_THOUSAND:
02619       return PRI_AOC_MULTIPLIER_THOUSAND;
02620    default:
02621       return PRI_AOC_MULTIPLIER_ONE;
02622    }
02623 }
02624 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
02625 
02626 #if defined(HAVE_PRI_AOC_EVENTS)
02627 /*!
02628  * \internal
02629  * \brief Convert PRI_AOC_MULTIPLIER to AST_AOC_MULTIPLIER
02630  * \since 1.8
02631  *
02632  * \return ast enum equivalent.
02633  */
02634 static int sig_pri_aoc_multiplier_from_pri(const int mult)
02635 {
02636    switch (mult) {
02637    case PRI_AOC_MULTIPLIER_THOUSANDTH:
02638       return AST_AOC_MULT_ONETHOUSANDTH;
02639    case PRI_AOC_MULTIPLIER_HUNDREDTH:
02640       return AST_AOC_MULT_ONEHUNDREDTH;
02641    case PRI_AOC_MULTIPLIER_TENTH:
02642       return AST_AOC_MULT_ONETENTH;
02643    case PRI_AOC_MULTIPLIER_ONE:
02644       return AST_AOC_MULT_ONE;
02645    case PRI_AOC_MULTIPLIER_TEN:
02646       return AST_AOC_MULT_TEN;
02647    case PRI_AOC_MULTIPLIER_HUNDRED:
02648       return AST_AOC_MULT_HUNDRED;
02649    case PRI_AOC_MULTIPLIER_THOUSAND:
02650       return AST_AOC_MULT_THOUSAND;
02651    default:
02652       return AST_AOC_MULT_ONE;
02653    }
02654 }
02655 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
02656 
02657 #if defined(HAVE_PRI_AOC_EVENTS)
02658 /*!
02659  * \internal
02660  * \brief Convert ast_aoc_time_scale representation to PRI_AOC_TIME_SCALE
02661  * \since 1.8
02662  *
02663  * \param value Value to convert to ast representation
02664  *
02665  * \return PRI_AOC_TIME_SCALE
02666  */
02667 static enum PRI_AOC_TIME_SCALE sig_pri_aoc_scale_to_pri(enum ast_aoc_time_scale value)
02668 {
02669    switch (value) {
02670    default:
02671    case AST_AOC_TIME_SCALE_HUNDREDTH_SECOND:
02672       return PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND;
02673    case AST_AOC_TIME_SCALE_TENTH_SECOND:
02674       return PRI_AOC_TIME_SCALE_TENTH_SECOND;
02675    case AST_AOC_TIME_SCALE_SECOND:
02676       return PRI_AOC_TIME_SCALE_SECOND;
02677    case AST_AOC_TIME_SCALE_TEN_SECOND:
02678       return PRI_AOC_TIME_SCALE_TEN_SECOND;
02679    case AST_AOC_TIME_SCALE_MINUTE:
02680       return PRI_AOC_TIME_SCALE_MINUTE;
02681    case AST_AOC_TIME_SCALE_HOUR:
02682       return PRI_AOC_TIME_SCALE_HOUR;
02683    case AST_AOC_TIME_SCALE_DAY:
02684       return PRI_AOC_TIME_SCALE_DAY;
02685    }
02686 }
02687 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
02688 
02689 #if defined(HAVE_PRI_AOC_EVENTS)
02690 /*!
02691  * \internal
02692  * \brief Convert PRI_AOC_TIME_SCALE to ast aoc representation
02693  * \since 1.8
02694  *
02695  * \param value Value to convert to ast representation
02696  *
02697  * \return ast aoc time scale
02698  */
02699 static enum ast_aoc_time_scale sig_pri_aoc_scale_to_ast(enum PRI_AOC_TIME_SCALE value)
02700 {
02701    switch (value) {
02702    default:
02703    case PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND:
02704       return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
02705    case PRI_AOC_TIME_SCALE_TENTH_SECOND:
02706       return AST_AOC_TIME_SCALE_TENTH_SECOND;
02707    case PRI_AOC_TIME_SCALE_SECOND:
02708       return AST_AOC_TIME_SCALE_SECOND;
02709    case PRI_AOC_TIME_SCALE_TEN_SECOND:
02710       return AST_AOC_TIME_SCALE_TEN_SECOND;
02711    case PRI_AOC_TIME_SCALE_MINUTE:
02712       return AST_AOC_TIME_SCALE_MINUTE;
02713    case PRI_AOC_TIME_SCALE_HOUR:
02714       return AST_AOC_TIME_SCALE_HOUR;
02715    case PRI_AOC_TIME_SCALE_DAY:
02716       return AST_AOC_TIME_SCALE_DAY;
02717    }
02718    return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
02719 }
02720 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
02721 
02722 #if defined(HAVE_PRI_AOC_EVENTS)
02723 /*!
02724  * \internal
02725  * \brief Handle AOC-S control frame
02726  * \since 1.8
02727  *
02728  * \param aoc_s AOC-S event parameters.
02729  * \param owner Asterisk channel associated with the call.
02730  * \param passthrough indicating if this message should be queued on the ast channel
02731  *
02732  * \note Assumes the pri->lock is already obtained.
02733  * \note Assumes the sig_pri private is locked
02734  * \note Assumes the owner channel lock is already obtained.
02735  *
02736  * \return Nothing
02737  */
02738 static void sig_pri_aoc_s_from_pri(const struct pri_subcmd_aoc_s *aoc_s, struct ast_channel *owner, int passthrough)
02739 {
02740    struct ast_aoc_decoded *decoded = NULL;
02741    struct ast_aoc_encoded *encoded = NULL;
02742    size_t encoded_size = 0;
02743    int idx;
02744 
02745    if (!owner || !aoc_s) {
02746       return;
02747    }
02748 
02749    if (!(decoded = ast_aoc_create(AST_AOC_S, 0, 0))) {
02750       return;
02751    }
02752 
02753    for (idx = 0; idx < aoc_s->num_items; ++idx) {
02754       enum ast_aoc_s_charged_item charged_item;
02755 
02756       charged_item = sig_pri_aoc_charged_item_to_ast(aoc_s->item[idx].chargeable);
02757       if (charged_item == AST_AOC_CHARGED_ITEM_NA) {
02758          /* Delete the unknown charged item from the list. */
02759          continue;
02760       }
02761       switch (aoc_s->item[idx].rate_type) {
02762       case PRI_AOC_RATE_TYPE_DURATION:
02763          ast_aoc_s_add_rate_duration(decoded,
02764             charged_item,
02765             aoc_s->item[idx].rate.duration.amount.cost,
02766             sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.duration.amount.multiplier),
02767             aoc_s->item[idx].rate.duration.currency,
02768             aoc_s->item[idx].rate.duration.time.length,
02769             sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.time.scale),
02770             aoc_s->item[idx].rate.duration.granularity.length,
02771             sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.granularity.scale),
02772             aoc_s->item[idx].rate.duration.charging_type);
02773          break;
02774       case PRI_AOC_RATE_TYPE_FLAT:
02775          ast_aoc_s_add_rate_flat(decoded,
02776             charged_item,
02777             aoc_s->item[idx].rate.flat.amount.cost,
02778             sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.flat.amount.multiplier),
02779             aoc_s->item[idx].rate.flat.currency);
02780          break;
02781       case PRI_AOC_RATE_TYPE_VOLUME:
02782          ast_aoc_s_add_rate_volume(decoded,
02783             charged_item,
02784             aoc_s->item[idx].rate.volume.unit,
02785             aoc_s->item[idx].rate.volume.amount.cost,
02786             sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.volume.amount.multiplier),
02787             aoc_s->item[idx].rate.volume.currency);
02788          break;
02789       case PRI_AOC_RATE_TYPE_SPECIAL_CODE:
02790          ast_aoc_s_add_rate_special_charge_code(decoded,
02791             charged_item,
02792             aoc_s->item[idx].rate.special);
02793          break;
02794       case PRI_AOC_RATE_TYPE_FREE:
02795          ast_aoc_s_add_rate_free(decoded, charged_item, 0);
02796          break;
02797       case PRI_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
02798          ast_aoc_s_add_rate_free(decoded, charged_item, 1);
02799          break;
02800       default:
02801          ast_aoc_s_add_rate_na(decoded, charged_item);
02802          break;
02803       }
02804    }
02805 
02806    if (passthrough && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
02807       ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
02808    }
02809 
02810    ast_aoc_manager_event(decoded, owner);
02811 
02812    ast_aoc_destroy_decoded(decoded);
02813    ast_aoc_destroy_encoded(encoded);
02814 }
02815 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
02816 
02817 #if defined(HAVE_PRI_AOC_EVENTS)
02818 /*!
02819  * \internal
02820  * \brief Generate AOC Request Response
02821  * \since 1.8
02822  *
02823  * \param aoc_request
02824  *
02825  * \note Assumes the pri->lock is already obtained.
02826  * \note Assumes the sig_pri private is locked
02827  * \note Assumes the owner channel lock is already obtained.
02828  *
02829  * \return Nothing
02830  */
02831 static void sig_pri_aoc_request_from_pri(const struct pri_subcmd_aoc_request *aoc_request, struct sig_pri_chan *pvt, q931_call *call)
02832 {
02833    int request;
02834 
02835    if (!aoc_request) {
02836       return;
02837    }
02838 
02839    request = aoc_request->charging_request;
02840 
02841    if (request & PRI_AOC_REQUEST_S) {
02842       if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S) {
02843          /* An AOC-S response must come from the other side, so save off this invoke_id
02844           * and see if an AOC-S message comes in before the call is answered. */
02845          pvt->aoc_s_request_invoke_id = aoc_request->invoke_id;
02846          pvt->aoc_s_request_invoke_id_valid = 1;
02847 
02848       } else {
02849          pri_aoc_s_request_response_send(pvt->pri->pri,
02850             call,
02851             aoc_request->invoke_id,
02852             NULL);
02853       }
02854    }
02855 
02856    if (request & PRI_AOC_REQUEST_D) {
02857       if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_D) {
02858          pri_aoc_de_request_response_send(pvt->pri->pri,
02859             call,
02860             PRI_AOC_REQ_RSP_CHARGING_INFO_FOLLOWS,
02861             aoc_request->invoke_id);
02862       } else {
02863          pri_aoc_de_request_response_send(pvt->pri->pri,
02864             call,
02865             PRI_AOC_REQ_RSP_ERROR_NOT_AVAILABLE,
02866             aoc_request->invoke_id);
02867       }
02868    }
02869 
02870    if (request & PRI_AOC_REQUEST_E) {
02871       if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_E) {
02872          pri_aoc_de_request_response_send(pvt->pri->pri,
02873             call,
02874             PRI_AOC_REQ_RSP_CHARGING_INFO_FOLLOWS,
02875             aoc_request->invoke_id);
02876       } else {
02877          pri_aoc_de_request_response_send(pvt->pri->pri,
02878             call,
02879             PRI_AOC_REQ_RSP_ERROR_NOT_AVAILABLE,
02880             aoc_request->invoke_id);
02881       }
02882    }
02883 }
02884 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
02885 
02886 #if defined(HAVE_PRI_AOC_EVENTS)
02887 /*!
02888  * \internal
02889  * \brief Generate AOC-D AST_CONTROL_AOC frame
02890  * \since 1.8
02891  *
02892  * \param aoc_e AOC-D event parameters.
02893  * \param owner Asterisk channel associated with the call.
02894  * \param passthrough indicating if this message should be queued on the ast channel
02895  *
02896  * \note Assumes the pri->lock is already obtained.
02897  * \note Assumes the sig_pri private is locked
02898  * \note Assumes the owner channel lock is already obtained.
02899  *
02900  * \return Nothing
02901  */
02902 static void sig_pri_aoc_d_from_pri(const struct pri_subcmd_aoc_d *aoc_d, struct ast_channel *owner, int passthrough)
02903 {
02904    struct ast_aoc_decoded *decoded = NULL;
02905    struct ast_aoc_encoded *encoded = NULL;
02906    size_t encoded_size = 0;
02907    enum ast_aoc_charge_type type;
02908 
02909    if (!owner || !aoc_d) {
02910       return;
02911    }
02912 
02913    switch (aoc_d->charge) {
02914    case PRI_AOC_DE_CHARGE_CURRENCY:
02915       type = AST_AOC_CHARGE_CURRENCY;
02916       break;
02917    case PRI_AOC_DE_CHARGE_UNITS:
02918       type = AST_AOC_CHARGE_UNIT;
02919       break;
02920    case PRI_AOC_DE_CHARGE_FREE:
02921       type = AST_AOC_CHARGE_FREE;
02922       break;
02923    default:
02924       type = AST_AOC_CHARGE_NA;
02925       break;
02926    }
02927 
02928    if (!(decoded = ast_aoc_create(AST_AOC_D, type, 0))) {
02929       return;
02930    }
02931 
02932    switch (aoc_d->billing_accumulation) {
02933    default:
02934       ast_debug(1, "AOC-D billing accumulation has unknown value: %d\n",
02935          aoc_d->billing_accumulation);
02936       /* Fall through */
02937    case 0:/* subTotal */
02938       ast_aoc_set_total_type(decoded, AST_AOC_SUBTOTAL);
02939       break;
02940    case 1:/* total */
02941       ast_aoc_set_total_type(decoded, AST_AOC_TOTAL);
02942       break;
02943    }
02944 
02945    switch (aoc_d->billing_id) {
02946    case PRI_AOC_D_BILLING_ID_NORMAL:
02947       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL);
02948       break;
02949    case PRI_AOC_D_BILLING_ID_REVERSE:
02950       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_REVERSE_CHARGE);
02951       break;
02952    case PRI_AOC_D_BILLING_ID_CREDIT_CARD:
02953       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
02954       break;
02955    case PRI_AOC_D_BILLING_ID_NOT_AVAILABLE:
02956    default:
02957       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NA);
02958       break;
02959    }
02960 
02961    switch (aoc_d->charge) {
02962    case PRI_AOC_DE_CHARGE_CURRENCY:
02963       ast_aoc_set_currency_info(decoded,
02964          aoc_d->recorded.money.amount.cost,
02965          sig_pri_aoc_multiplier_from_pri(aoc_d->recorded.money.amount.multiplier),
02966          aoc_d->recorded.money.currency);
02967       break;
02968    case PRI_AOC_DE_CHARGE_UNITS:
02969       {
02970          int i;
02971          for (i = 0; i < aoc_d->recorded.unit.num_items; ++i) {
02972             /* if type or number are negative, then they are not present */
02973             ast_aoc_add_unit_entry(decoded,
02974                (aoc_d->recorded.unit.item[i].number >= 0 ? 1 : 0),
02975                aoc_d->recorded.unit.item[i].number,
02976                (aoc_d->recorded.unit.item[i].type >= 0 ? 1 : 0),
02977                aoc_d->recorded.unit.item[i].type);
02978          }
02979       }
02980       break;
02981    }
02982 
02983    if (passthrough && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
02984       ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
02985    }
02986 
02987    ast_aoc_manager_event(decoded, owner);
02988 
02989    ast_aoc_destroy_decoded(decoded);
02990    ast_aoc_destroy_encoded(encoded);
02991 }
02992 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
02993 
02994 #if defined(HAVE_PRI_AOC_EVENTS)
02995 /*!
02996  * \internal
02997  * \brief Generate AOC-E AST_CONTROL_AOC frame
02998  * \since 1.8
02999  *
03000  * \param aoc_e AOC-E event parameters.
03001  * \param owner Asterisk channel associated with the call.
03002  * \param passthrough indicating if this message should be queued on the ast channel
03003  *
03004  * \note Assumes the pri->lock is already obtained.
03005  * \note Assumes the sig_pri private is locked
03006  * \note Assumes the owner channel lock is already obtained.
03007  * \note owner channel may be NULL. In that case, generate event only
03008  *
03009  * \return Nothing
03010  */
03011 static void sig_pri_aoc_e_from_pri(const struct pri_subcmd_aoc_e *aoc_e, struct ast_channel *owner, int passthrough)
03012 {
03013    struct ast_aoc_decoded *decoded = NULL;
03014    struct ast_aoc_encoded *encoded = NULL;
03015    size_t encoded_size = 0;
03016    enum ast_aoc_charge_type type;
03017 
03018    if (!aoc_e) {
03019       return;
03020    }
03021 
03022    switch (aoc_e->charge) {
03023    case PRI_AOC_DE_CHARGE_CURRENCY:
03024       type = AST_AOC_CHARGE_CURRENCY;
03025       break;
03026    case PRI_AOC_DE_CHARGE_UNITS:
03027       type = AST_AOC_CHARGE_UNIT;
03028       break;
03029    case PRI_AOC_DE_CHARGE_FREE:
03030       type = AST_AOC_CHARGE_FREE;
03031       break;
03032    default:
03033       type = AST_AOC_CHARGE_NA;
03034       break;
03035    }
03036 
03037    if (!(decoded = ast_aoc_create(AST_AOC_E, type, 0))) {
03038       return;
03039    }
03040 
03041    switch (aoc_e->associated.charging_type) {
03042    case PRI_AOC_E_CHARGING_ASSOCIATION_NUMBER:
03043       if (!aoc_e->associated.charge.number.valid) {
03044          break;
03045       }
03046       ast_aoc_set_association_number(decoded, aoc_e->associated.charge.number.str, aoc_e->associated.charge.number.plan);
03047       break;
03048    case PRI_AOC_E_CHARGING_ASSOCIATION_ID:
03049       ast_aoc_set_association_id(decoded, aoc_e->associated.charge.id);
03050       break;
03051    default:
03052       break;
03053    }
03054 
03055    switch (aoc_e->billing_id) {
03056    case PRI_AOC_E_BILLING_ID_NORMAL:
03057       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL);
03058       break;
03059    case PRI_AOC_E_BILLING_ID_REVERSE:
03060       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_REVERSE_CHARGE);
03061       break;
03062    case PRI_AOC_E_BILLING_ID_CREDIT_CARD:
03063       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
03064       break;
03065    case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_UNCONDITIONAL:
03066       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL);
03067       break;
03068    case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_BUSY:
03069       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_BUSY);
03070       break;
03071    case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_NO_REPLY:
03072       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_NO_REPLY);
03073       break;
03074    case PRI_AOC_E_BILLING_ID_CALL_DEFLECTION:
03075       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_DEFLECTION);
03076       break;
03077    case PRI_AOC_E_BILLING_ID_CALL_TRANSFER:
03078       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_TRANSFER);
03079       break;
03080    case PRI_AOC_E_BILLING_ID_NOT_AVAILABLE:
03081    default:
03082       ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NA);
03083       break;
03084    }
03085 
03086    switch (aoc_e->charge) {
03087    case PRI_AOC_DE_CHARGE_CURRENCY:
03088       ast_aoc_set_currency_info(decoded,
03089          aoc_e->recorded.money.amount.cost,
03090          sig_pri_aoc_multiplier_from_pri(aoc_e->recorded.money.amount.multiplier),
03091          aoc_e->recorded.money.currency);
03092       break;
03093    case PRI_AOC_DE_CHARGE_UNITS:
03094       {
03095          int i;
03096          for (i = 0; i < aoc_e->recorded.unit.num_items; ++i) {
03097             /* if type or number are negative, then they are not present */
03098             ast_aoc_add_unit_entry(decoded,
03099                (aoc_e->recorded.unit.item[i].number >= 0 ? 1 : 0),
03100                aoc_e->recorded.unit.item[i].number,
03101                (aoc_e->recorded.unit.item[i].type >= 0 ? 1 : 0),
03102                aoc_e->recorded.unit.item[i].type);
03103          }
03104       }
03105    }
03106 
03107    if (passthrough && owner && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
03108       ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
03109    }
03110 
03111    ast_aoc_manager_event(decoded, owner);
03112 
03113    ast_aoc_destroy_decoded(decoded);
03114    ast_aoc_destroy_encoded(encoded);
03115 }
03116 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
03117 
03118 #if defined(HAVE_PRI_AOC_EVENTS)
03119 /*!
03120  * \internal
03121  * \brief send an AOC-S message on the current call
03122  *
03123  * \param pvt sig_pri private channel structure.
03124  * \param generic decoded ast AOC message
03125  *
03126  * \return Nothing
03127  *
03128  * \note Assumes that the PRI lock is already obtained.
03129  */
03130 static void sig_pri_aoc_s_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
03131 {
03132    struct pri_subcmd_aoc_s aoc_s = { 0, };
03133    const struct ast_aoc_s_entry *entry;
03134    int idx;
03135 
03136    for (idx = 0; idx < ast_aoc_s_get_count(decoded); idx++) {
03137       if (!(entry = ast_aoc_s_get_rate_info(decoded, idx))) {
03138          break;
03139       }
03140 
03141       aoc_s.item[idx].chargeable = sig_pri_aoc_charged_item_to_pri(entry->charged_item);
03142 
03143       switch (entry->rate_type) {
03144       case AST_AOC_RATE_TYPE_DURATION:
03145          aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_DURATION;
03146          aoc_s.item[idx].rate.duration.amount.cost = entry->rate.duration.amount;
03147          aoc_s.item[idx].rate.duration.amount.multiplier =
03148             sig_pri_aoc_multiplier_from_ast(entry->rate.duration.multiplier);
03149          aoc_s.item[idx].rate.duration.time.length = entry->rate.duration.time;
03150          aoc_s.item[idx].rate.duration.time.scale =
03151             sig_pri_aoc_scale_to_pri(entry->rate.duration.time_scale);
03152          aoc_s.item[idx].rate.duration.granularity.length = entry->rate.duration.granularity_time;
03153          aoc_s.item[idx].rate.duration.granularity.scale =
03154             sig_pri_aoc_scale_to_pri(entry->rate.duration.granularity_time_scale);
03155          aoc_s.item[idx].rate.duration.charging_type = entry->rate.duration.charging_type;
03156 
03157          if (!ast_strlen_zero(entry->rate.duration.currency_name)) {
03158             ast_copy_string(aoc_s.item[idx].rate.duration.currency,
03159                entry->rate.duration.currency_name,
03160                sizeof(aoc_s.item[idx].rate.duration.currency));
03161          }
03162          break;
03163       case AST_AOC_RATE_TYPE_FLAT:
03164          aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FLAT;
03165          aoc_s.item[idx].rate.flat.amount.cost = entry->rate.flat.amount;
03166          aoc_s.item[idx].rate.flat.amount.multiplier =
03167             sig_pri_aoc_multiplier_from_ast(entry->rate.flat.multiplier);
03168 
03169          if (!ast_strlen_zero(entry->rate.flat.currency_name)) {
03170             ast_copy_string(aoc_s.item[idx].rate.flat.currency,
03171                entry->rate.flat.currency_name,
03172                sizeof(aoc_s.item[idx].rate.flat.currency));
03173          }
03174          break;
03175       case AST_AOC_RATE_TYPE_VOLUME:
03176          aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_VOLUME;
03177          aoc_s.item[idx].rate.volume.unit = entry->rate.volume.volume_unit;
03178          aoc_s.item[idx].rate.volume.amount.cost = entry->rate.volume.amount;
03179          aoc_s.item[idx].rate.volume.amount.multiplier =
03180             sig_pri_aoc_multiplier_from_ast(entry->rate.volume.multiplier);
03181 
03182          if (!ast_strlen_zero(entry->rate.volume.currency_name)) {
03183             ast_copy_string(aoc_s.item[idx].rate.volume.currency,
03184                entry->rate.volume.currency_name,
03185                sizeof(aoc_s.item[idx].rate.volume.currency));
03186          }
03187          break;
03188       case AST_AOC_RATE_TYPE_SPECIAL_CODE:
03189          aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_SPECIAL_CODE;
03190          aoc_s.item[idx].rate.special = entry->rate.special_code;
03191          break;
03192       case AST_AOC_RATE_TYPE_FREE:
03193          aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FREE;
03194          break;
03195       case AST_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
03196          aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FREE_FROM_BEGINNING;
03197          break;
03198       default:
03199       case AST_AOC_RATE_TYPE_NA:
03200          aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_NOT_AVAILABLE;
03201          break;
03202       }
03203    }
03204    aoc_s.num_items = idx;
03205 
03206    /* if this rate should be sent as a response to an AOC-S request we will
03207     * have an aoc_s_request_invoke_id associated with this pvt */
03208    if (pvt->aoc_s_request_invoke_id_valid) {
03209       pri_aoc_s_request_response_send(pvt->pri->pri, pvt->call, pvt->aoc_s_request_invoke_id, &aoc_s);
03210       pvt->aoc_s_request_invoke_id_valid = 0;
03211    } else {
03212       pri_aoc_s_send(pvt->pri->pri, pvt->call, &aoc_s);
03213    }
03214 }
03215 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
03216 
03217 #if defined(HAVE_PRI_AOC_EVENTS)
03218 /*!
03219  * \internal
03220  * \brief send an AOC-D message on the current call
03221  *
03222  * \param pvt sig_pri private channel structure.
03223  * \param generic decoded ast AOC message
03224  *
03225  * \return Nothing
03226  *
03227  * \note Assumes that the PRI lock is already obtained.
03228  */
03229 static void sig_pri_aoc_d_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
03230 {
03231    struct pri_subcmd_aoc_d aoc_d = { 0, };
03232 
03233    aoc_d.billing_accumulation = (ast_aoc_get_total_type(decoded) == AST_AOC_TOTAL) ? 1 : 0;
03234 
03235    switch (ast_aoc_get_billing_id(decoded)) {
03236    case AST_AOC_BILLING_NORMAL:
03237       aoc_d.billing_id = PRI_AOC_D_BILLING_ID_NORMAL;
03238       break;
03239    case AST_AOC_BILLING_REVERSE_CHARGE:
03240       aoc_d.billing_id = PRI_AOC_D_BILLING_ID_REVERSE;
03241       break;
03242    case AST_AOC_BILLING_CREDIT_CARD:
03243       aoc_d.billing_id = PRI_AOC_D_BILLING_ID_CREDIT_CARD;
03244       break;
03245    case AST_AOC_BILLING_NA:
03246    default:
03247       aoc_d.billing_id = PRI_AOC_D_BILLING_ID_NOT_AVAILABLE;
03248       break;
03249    }
03250 
03251    switch (ast_aoc_get_charge_type(decoded)) {
03252    case AST_AOC_CHARGE_FREE:
03253       aoc_d.charge = PRI_AOC_DE_CHARGE_FREE;
03254       break;
03255    case AST_AOC_CHARGE_CURRENCY:
03256       {
03257          const char *currency_name = ast_aoc_get_currency_name(decoded);
03258          aoc_d.charge = PRI_AOC_DE_CHARGE_CURRENCY;
03259          aoc_d.recorded.money.amount.cost = ast_aoc_get_currency_amount(decoded);
03260          aoc_d.recorded.money.amount.multiplier = sig_pri_aoc_multiplier_from_ast(ast_aoc_get_currency_multiplier(decoded));
03261          if (!ast_strlen_zero(currency_name)) {
03262             ast_copy_string(aoc_d.recorded.money.currency, currency_name, sizeof(aoc_d.recorded.money.currency));
03263          }
03264       }
03265       break;
03266    case AST_AOC_CHARGE_UNIT:
03267       {
03268          const struct ast_aoc_unit_entry *entry;
03269          int i;
03270          aoc_d.charge = PRI_AOC_DE_CHARGE_UNITS;
03271          for (i = 0; i < ast_aoc_get_unit_count(decoded); i++) {
03272             if ((entry = ast_aoc_get_unit_info(decoded, i)) && i < ARRAY_LEN(aoc_d.recorded.unit.item)) {
03273                if (entry->valid_amount) {
03274                   aoc_d.recorded.unit.item[i].number = entry->amount;
03275                } else {
03276                   aoc_d.recorded.unit.item[i].number = -1;
03277                }
03278                if (entry->valid_type) {
03279                   aoc_d.recorded.unit.item[i].type = entry->type;
03280                } else {
03281                   aoc_d.recorded.unit.item[i].type = -1;
03282                }
03283                aoc_d.recorded.unit.num_items++;
03284             } else {
03285                break;
03286             }
03287          }
03288       }
03289       break;
03290    case AST_AOC_CHARGE_NA:
03291    default:
03292       aoc_d.charge = PRI_AOC_DE_CHARGE_NOT_AVAILABLE;
03293       break;
03294    }
03295 
03296    pri_aoc_d_send(pvt->pri->pri, pvt->call, &aoc_d);
03297 }
03298 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
03299 
03300 #if defined(HAVE_PRI_AOC_EVENTS)
03301 /*!
03302  * \internal
03303  * \brief send an AOC-E message on the current call
03304  *
03305  * \param pvt sig_pri private channel structure.
03306  * \param generic decoded ast AOC message
03307  *
03308  * \return Nothing
03309  *
03310  * \note Assumes that the PRI lock is already obtained.
03311  */
03312 static void sig_pri_aoc_e_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
03313 {
03314    struct pri_subcmd_aoc_e *aoc_e = &pvt->aoc_e;
03315    const struct ast_aoc_charging_association *ca = ast_aoc_get_association_info(decoded);
03316 
03317    memset(aoc_e, 0, sizeof(*aoc_e));
03318    pvt->holding_aoce = 1;
03319 
03320    switch (ca->charging_type) {
03321    case AST_AOC_CHARGING_ASSOCIATION_NUMBER:
03322       aoc_e->associated.charge.number.valid = 1;
03323       ast_copy_string(aoc_e->associated.charge.number.str,
03324          ca->charge.number.number,
03325          sizeof(aoc_e->associated.charge.number.str));
03326       aoc_e->associated.charge.number.plan = ca->charge.number.plan;
03327       aoc_e->associated.charging_type = PRI_AOC_E_CHARGING_ASSOCIATION_NUMBER;
03328       break;
03329    case AST_AOC_CHARGING_ASSOCIATION_ID:
03330       aoc_e->associated.charge.id = ca->charge.id;
03331       aoc_e->associated.charging_type = PRI_AOC_E_CHARGING_ASSOCIATION_ID;
03332       break;
03333    case AST_AOC_CHARGING_ASSOCIATION_NA:
03334    default:
03335       break;
03336    }
03337 
03338    switch (ast_aoc_get_billing_id(decoded)) {
03339    case AST_AOC_BILLING_NORMAL:
03340       aoc_e->billing_id = PRI_AOC_E_BILLING_ID_NORMAL;
03341       break;
03342    case AST_AOC_BILLING_REVERSE_CHARGE:
03343       aoc_e->billing_id = PRI_AOC_E_BILLING_ID_REVERSE;
03344       break;
03345    case AST_AOC_BILLING_CREDIT_CARD:
03346       aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CREDIT_CARD;
03347       break;
03348    case AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL:
03349       aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_UNCONDITIONAL;
03350       break;
03351    case AST_AOC_BILLING_CALL_FWD_BUSY:
03352       aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_BUSY;
03353       break;
03354    case AST_AOC_BILLING_CALL_FWD_NO_REPLY:
03355       aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_NO_REPLY;
03356       break;
03357    case AST_AOC_BILLING_CALL_DEFLECTION:
03358       aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_DEFLECTION;
03359       break;
03360    case AST_AOC_BILLING_CALL_TRANSFER:
03361       aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_TRANSFER;
03362       break;
03363    case AST_AOC_BILLING_NA:
03364    default:
03365       aoc_e->billing_id = PRI_AOC_E_BILLING_ID_NOT_AVAILABLE;
03366       break;
03367    }
03368 
03369    switch (ast_aoc_get_charge_type(decoded)) {
03370    case AST_AOC_CHARGE_FREE:
03371       aoc_e->charge = PRI_AOC_DE_CHARGE_FREE;
03372       break;
03373    case AST_AOC_CHARGE_CURRENCY:
03374       {
03375          const char *currency_name = ast_aoc_get_currency_name(decoded);
03376          aoc_e->charge = PRI_AOC_DE_CHARGE_CURRENCY;
03377          aoc_e->recorded.money.amount.cost = ast_aoc_get_currency_amount(decoded);
03378          aoc_e->recorded.money.amount.multiplier = sig_pri_aoc_multiplier_from_ast(ast_aoc_get_currency_multiplier(decoded));
03379          if (!ast_strlen_zero(currency_name)) {
03380             ast_copy_string(aoc_e->recorded.money.currency, currency_name, sizeof(aoc_e->recorded.money.currency));
03381          }
03382       }
03383       break;
03384    case AST_AOC_CHARGE_UNIT:
03385       {
03386          const struct ast_aoc_unit_entry *entry;
03387          int i;
03388          aoc_e->charge = PRI_AOC_DE_CHARGE_UNITS;
03389          for (i = 0; i < ast_aoc_get_unit_count(decoded); i++) {
03390             if ((entry = ast_aoc_get_unit_info(decoded, i)) && i < ARRAY_LEN(aoc_e->recorded.unit.item)) {
03391                if (entry->valid_amount) {
03392                   aoc_e->recorded.unit.item[i].number = entry->amount;
03393                } else {
03394                   aoc_e->recorded.unit.item[i].number = -1;
03395                }
03396                if (entry->valid_type) {
03397                   aoc_e->recorded.unit.item[i].type = entry->type;
03398                } else {
03399                   aoc_e->recorded.unit.item[i].type = -1;
03400                }
03401                aoc_e->recorded.unit.num_items++;
03402             }
03403          }
03404       }
03405       break;
03406    case AST_AOC_CHARGE_NA:
03407    default:
03408       aoc_e->charge = PRI_AOC_DE_CHARGE_NOT_AVAILABLE;
03409       break;
03410    }
03411 }
03412 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
03413 
03414 #if defined(HAVE_PRI_AOC_EVENTS)
03415 /*!
03416  * \internal
03417  * \brief send an AOC-E termination request on ast_channel and set
03418  * hangup delay.
03419  *
03420  * \param pri sig_pri PRI control structure.
03421  * \param chanpos Channel position in the span.
03422  * \param ms to delay hangup
03423  *
03424  * \note Assumes the pri->lock is already obtained.
03425  * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
03426  *
03427  * \return Nothing
03428  */
03429 static void sig_pri_send_aoce_termination_request(struct sig_pri_span *pri, int chanpos, unsigned int ms)
03430 {
03431    struct sig_pri_chan *pvt;
03432    struct ast_aoc_decoded *decoded = NULL;
03433    struct ast_aoc_encoded *encoded = NULL;
03434    size_t encoded_size;
03435    struct timeval whentohangup = { 0, };
03436 
03437    sig_pri_lock_owner(pri, chanpos);
03438    pvt = pri->pvts[chanpos];
03439    if (!pvt->owner) {
03440       return;
03441    }
03442 
03443    if (!(decoded = ast_aoc_create(AST_AOC_REQUEST, 0, AST_AOC_REQUEST_E))) {
03444       ast_softhangup_nolock(pvt->owner, AST_SOFTHANGUP_DEV);
03445       goto cleanup_termination_request;
03446    }
03447 
03448    ast_aoc_set_termination_request(decoded);
03449 
03450    if (!(encoded = ast_aoc_encode(decoded, &encoded_size, pvt->owner))) {
03451       ast_softhangup_nolock(pvt->owner, AST_SOFTHANGUP_DEV);
03452       goto cleanup_termination_request;
03453    }
03454 
03455    /* convert ms to timeval */
03456    whentohangup.tv_usec = (ms % 1000) * 1000;
03457    whentohangup.tv_sec = ms / 1000;
03458 
03459    if (ast_queue_control_data(pvt->owner, AST_CONTROL_AOC, encoded, encoded_size)) {
03460       ast_softhangup_nolock(pvt->owner, AST_SOFTHANGUP_DEV);
03461       goto cleanup_termination_request;
03462    }
03463 
03464    pvt->waiting_for_aoce = 1;
03465    ast_channel_setwhentohangup_tv(pvt->owner, whentohangup);
03466    ast_log(LOG_DEBUG, "Delaying hangup on %s for aoc-e msg\n", pvt->owner->name);
03467 
03468 cleanup_termination_request:
03469    ast_channel_unlock(pvt->owner);
03470    ast_aoc_destroy_decoded(decoded);
03471    ast_aoc_destroy_encoded(encoded);
03472 }
03473 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
03474 
03475 /*!
03476  * \internal
03477  * \brief TRUE if PRI event came in on a CIS call.
03478  * \since 1.8
03479  *
03480  * \param channel PRI encoded span/channel
03481  *
03482  * \retval non-zero if CIS call.
03483  */
03484 static int sig_pri_is_cis_call(int channel)
03485 {
03486    return channel != -1 && (channel & PRI_CIS_CALL);
03487 }
03488 
03489 /*!
03490  * \internal
03491  * \brief Handle the CIS associated PRI subcommand events.
03492  * \since 1.8
03493  *
03494  * \param pri sig_pri PRI control structure.
03495  * \param event_id PRI event id
03496  * \param subcmds Subcommands to process if any. (Could be NULL).
03497  * \param call_rsp libpri opaque call structure to send any responses toward.
03498  * Could be NULL either because it is not available or the call is for the
03499  * dummy call reference.  However, this should not be NULL in the cases that
03500  * need to use the pointer to send a response message back.
03501  *
03502  * \note Assumes the pri->lock is already obtained.
03503  *
03504  * \return Nothing
03505  */
03506 static void sig_pri_handle_cis_subcmds(struct sig_pri_span *pri, int event_id,
03507    const struct pri_subcommands *subcmds, q931_call *call_rsp)
03508 {
03509    int index;
03510 #if defined(HAVE_PRI_CCSS)
03511    struct ast_cc_agent *agent;
03512    struct sig_pri_cc_agent_prv *agent_prv;
03513    struct sig_pri_cc_monitor_instance *monitor;
03514 #endif   /* defined(HAVE_PRI_CCSS) */
03515 
03516    if (!subcmds) {
03517       return;
03518    }
03519    for (index = 0; index < subcmds->counter_subcmd; ++index) {
03520       const struct pri_subcommand *subcmd = &subcmds->subcmd[index];
03521 
03522       switch (subcmd->cmd) {
03523 #if defined(STATUS_REQUEST_PLACE_HOLDER)
03524       case PRI_SUBCMD_STATUS_REQ:
03525       case PRI_SUBCMD_STATUS_REQ_RSP:
03526          /* Ignore for now. */
03527          break;
03528 #endif   /* defined(STATUS_REQUEST_PLACE_HOLDER) */
03529 #if defined(HAVE_PRI_CCSS)
03530       case PRI_SUBCMD_CC_REQ:
03531          agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_request.cc_id);
03532          if (!agent) {
03533             pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
03534             break;
03535          }
03536          if (!ast_cc_request_is_within_limits()) {
03537             if (pri_cc_req_rsp(pri->pri, subcmd->u.cc_request.cc_id,
03538                5/* queue_full */)) {
03539                pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
03540             }
03541             ast_cc_failed(agent->core_id, "%s agent system CC queue full",
03542                sig_pri_cc_type_name);
03543             ao2_ref(agent, -1);
03544             break;
03545          }
03546          agent_prv = agent->private_data;
03547          agent_prv->cc_request_response_pending = 1;
03548          if (ast_cc_agent_accept_request(agent->core_id,
03549             "%s caller accepted CC offer.", sig_pri_cc_type_name)) {
03550             agent_prv->cc_request_response_pending = 0;
03551             if (pri_cc_req_rsp(pri->pri, subcmd->u.cc_request.cc_id,
03552                2/* short_term_denial */)) {
03553                pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
03554             }
03555             ast_cc_failed(agent->core_id, "%s agent CC core request accept failed",
03556                sig_pri_cc_type_name);
03557          }
03558          ao2_ref(agent, -1);
03559          break;
03560 #endif   /* defined(HAVE_PRI_CCSS) */
03561 #if defined(HAVE_PRI_CCSS)
03562       case PRI_SUBCMD_CC_REQ_RSP:
03563          monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
03564             subcmd->u.cc_request_rsp.cc_id);
03565          if (!monitor) {
03566             pri_cc_cancel(pri->pri, subcmd->u.cc_request_rsp.cc_id);
03567             break;
03568          }
03569          switch (subcmd->u.cc_request_rsp.status) {
03570          case 0:/* success */
03571             ast_cc_monitor_request_acked(monitor->core_id,
03572                "%s far end accepted CC request", sig_pri_cc_type_name);
03573             break;
03574          case 1:/* timeout */
03575             ast_verb(2, "core_id:%d %s CC request timeout\n", monitor->core_id,
03576                sig_pri_cc_type_name);
03577             ast_cc_monitor_failed(monitor->core_id, monitor->name,
03578                "%s CC request timeout", sig_pri_cc_type_name);
03579             break;
03580          case 2:/* error */
03581             ast_verb(2, "core_id:%d %s CC request error: %s\n", monitor->core_id,
03582                sig_pri_cc_type_name,
03583                pri_facility_error2str(subcmd->u.cc_request_rsp.fail_code));
03584             ast_cc_monitor_failed(monitor->core_id, monitor->name,
03585                "%s CC request error", sig_pri_cc_type_name);
03586             break;
03587          case 3:/* reject */
03588             ast_verb(2, "core_id:%d %s CC request reject: %s\n", monitor->core_id,
03589                sig_pri_cc_type_name,
03590                pri_facility_reject2str(subcmd->u.cc_request_rsp.fail_code));
03591             ast_cc_monitor_failed(monitor->core_id, monitor->name,
03592                "%s CC request reject", sig_pri_cc_type_name);
03593             break;
03594          default:
03595             ast_verb(2, "core_id:%d %s CC request unknown status %d\n",
03596                monitor->core_id, sig_pri_cc_type_name,
03597                subcmd->u.cc_request_rsp.status);
03598             ast_cc_monitor_failed(monitor->core_id, monitor->name,
03599                "%s CC request unknown status", sig_pri_cc_type_name);
03600             break;
03601          }
03602          ao2_ref(monitor, -1);
03603          break;
03604 #endif   /* defined(HAVE_PRI_CCSS) */
03605 #if defined(HAVE_PRI_CCSS)
03606       case PRI_SUBCMD_CC_REMOTE_USER_FREE:
03607          monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
03608             subcmd->u.cc_remote_user_free.cc_id);
03609          if (!monitor) {
03610             pri_cc_cancel(pri->pri, subcmd->u.cc_remote_user_free.cc_id);
03611             break;
03612          }
03613          ast_cc_monitor_callee_available(monitor->core_id,
03614             "%s callee has become available", sig_pri_cc_type_name);
03615          ao2_ref(monitor, -1);
03616          break;
03617 #endif   /* defined(HAVE_PRI_CCSS) */
03618 #if defined(HAVE_PRI_CCSS)
03619       case PRI_SUBCMD_CC_B_FREE:
03620          monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
03621             subcmd->u.cc_b_free.cc_id);
03622          if (!monitor) {
03623             pri_cc_cancel(pri->pri, subcmd->u.cc_b_free.cc_id);
03624             break;
03625          }
03626          ast_cc_monitor_party_b_free(monitor->core_id);
03627          ao2_ref(monitor, -1);
03628          break;
03629 #endif   /* defined(HAVE_PRI_CCSS) */
03630 #if defined(HAVE_PRI_CCSS)
03631       case PRI_SUBCMD_CC_STATUS_REQ:
03632          monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
03633             subcmd->u.cc_status_req.cc_id);
03634          if (!monitor) {
03635             pri_cc_cancel(pri->pri, subcmd->u.cc_status_req.cc_id);
03636             break;
03637          }
03638          ast_cc_monitor_status_request(monitor->core_id);
03639          ao2_ref(monitor, -1);
03640          break;
03641 #endif   /* defined(HAVE_PRI_CCSS) */
03642 #if defined(HAVE_PRI_CCSS)
03643       case PRI_SUBCMD_CC_STATUS_REQ_RSP:
03644          agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_status_req_rsp.cc_id);
03645          if (!agent) {
03646             pri_cc_cancel(pri->pri, subcmd->u.cc_status_req_rsp.cc_id);
03647             break;
03648          }
03649          ast_cc_agent_status_response(agent->core_id,
03650             subcmd->u.cc_status_req_rsp.status ? AST_DEVICE_INUSE
03651             : AST_DEVICE_NOT_INUSE);
03652          ao2_ref(agent, -1);
03653          break;
03654 #endif   /* defined(HAVE_PRI_CCSS) */
03655 #if defined(HAVE_PRI_CCSS)
03656       case PRI_SUBCMD_CC_STATUS:
03657          agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_status.cc_id);
03658          if (!agent) {
03659             pri_cc_cancel(pri->pri, subcmd->u.cc_status.cc_id);
03660             break;
03661          }
03662          if (subcmd->u.cc_status.status) {
03663             ast_cc_agent_caller_busy(agent->core_id, "%s agent caller is busy",
03664                sig_pri_cc_type_name);
03665          } else {
03666             ast_cc_agent_caller_available(agent->core_id,
03667                "%s agent caller is available", sig_pri_cc_type_name);
03668          }
03669          ao2_ref(agent, -1);
03670          break;
03671 #endif   /* defined(HAVE_PRI_CCSS) */
03672 #if defined(HAVE_PRI_CCSS)
03673       case PRI_SUBCMD_CC_CANCEL:
03674          sig_pri_cc_link_canceled(pri, subcmd->u.cc_cancel.cc_id,
03675             subcmd->u.cc_cancel.is_agent);
03676          break;
03677 #endif   /* defined(HAVE_PRI_CCSS) */
03678 #if defined(HAVE_PRI_CCSS)
03679       case PRI_SUBCMD_CC_STOP_ALERTING:
03680          monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
03681             subcmd->u.cc_stop_alerting.cc_id);
03682          if (!monitor) {
03683             pri_cc_cancel(pri->pri, subcmd->u.cc_stop_alerting.cc_id);
03684             break;
03685          }
03686          ast_cc_monitor_stop_ringing(monitor->core_id);
03687          ao2_ref(monitor, -1);
03688          break;
03689 #endif   /* defined(HAVE_PRI_CCSS) */
03690 #if defined(HAVE_PRI_AOC_EVENTS)
03691       case PRI_SUBCMD_AOC_E:
03692          /* Queue AST_CONTROL_AOC frame */
03693          sig_pri_aoc_e_from_pri(&subcmd->u.aoc_e, NULL, 0);
03694          break;
03695 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
03696       default:
03697          ast_debug(2,
03698             "Unknown CIS subcommand(%d) in %s event on span %d.\n",
03699             subcmd->cmd, pri_event2str(event_id), pri->span);
03700          break;
03701       }
03702    }
03703 }
03704 
03705 #if defined(HAVE_PRI_AOC_EVENTS)
03706 /*!
03707  * \internal
03708  * \brief detect if AOC-S subcmd is present.
03709  * \since 1.8
03710  *
03711  * \param subcmds Subcommands to process if any. (Could be NULL).
03712  *
03713  * \note Knowing whether or not an AOC-E subcmd is present on certain
03714  * PRI hangup events is necessary to determine what method to use to hangup
03715  * the ast_channel.  If an AOC-E subcmd just came in, then a new AOC-E was queued
03716  * on the ast_channel.  If a soft hangup is used, the AOC-E msg will never make it
03717  * across the bridge, but if a AST_CONTROL_HANGUP frame is queued behind it
03718  * we can ensure the AOC-E frame makes it to it's destination before the hangup
03719  * frame is read.
03720  *
03721  *
03722  * \retval 0 AOC-E is not present in subcmd list
03723  * \retval 1 AOC-E is present in subcmd list
03724  */
03725 static int detect_aoc_e_subcmd(const struct pri_subcommands *subcmds)
03726 {
03727    int i;
03728 
03729    if (!subcmds) {
03730       return 0;
03731    }
03732    for (i = 0; i < subcmds->counter_subcmd; ++i) {
03733       const struct pri_subcommand *subcmd = &subcmds->subcmd[i];
03734       if (subcmd->cmd == PRI_SUBCMD_AOC_E) {
03735          return 1;
03736       }
03737    }
03738    return 0;
03739 }
03740 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
03741 
03742 /*!
03743  * \internal
03744  * \brief Handle the call associated PRI subcommand events.
03745  * \since 1.8
03746  *
03747  * \param pri sig_pri PRI control structure.
03748  * \param chanpos Channel position in the span.
03749  * \param event_id PRI event id
03750  * \param channel PRI encoded span/channel
03751  * \param subcmds Subcommands to process if any. (Could be NULL).
03752  * \param call_rsp libpri opaque call structure to send any responses toward.
03753  * Could be NULL either because it is not available or the call is for the
03754  * dummy call reference.  However, this should not be NULL in the cases that
03755  * need to use the pointer to send a response message back.
03756  *
03757  * \note Assumes the pri->lock is already obtained.
03758  * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
03759  *
03760  * \return Nothing
03761  */
03762 static void sig_pri_handle_subcmds(struct sig_pri_span *pri, int chanpos, int event_id,
03763    int channel, const struct pri_subcommands *subcmds, q931_call *call_rsp)
03764 {
03765    int index;
03766    struct ast_channel *owner;
03767    struct ast_party_redirecting ast_redirecting;
03768 #if defined(HAVE_PRI_TRANSFER)
03769    struct xfer_rsp_data xfer_rsp;
03770 #endif   /* defined(HAVE_PRI_TRANSFER) */
03771 
03772    if (!subcmds) {
03773       return;
03774    }
03775    for (index = 0; index < subcmds->counter_subcmd; ++index) {
03776       const struct pri_subcommand *subcmd = &subcmds->subcmd[index];
03777 
03778       switch (subcmd->cmd) {
03779       case PRI_SUBCMD_CONNECTED_LINE:
03780          sig_pri_lock_owner(pri, chanpos);
03781          owner = pri->pvts[chanpos]->owner;
03782          if (owner) {
03783             struct ast_party_connected_line ast_connected;
03784             int caller_id_update;
03785 
03786             /* Extract the connected line information */
03787             ast_party_connected_line_init(&ast_connected);
03788             sig_pri_party_id_convert(&ast_connected.id, &subcmd->u.connected_line.id,
03789                pri);
03790             ast_connected.id.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
03791 
03792             caller_id_update = 0;
03793             if (ast_connected.id.name.str) {
03794                /* Save name for Caller-ID update */
03795                ast_copy_string(pri->pvts[chanpos]->cid_name,
03796                   ast_connected.id.name.str, sizeof(pri->pvts[chanpos]->cid_name));
03797                caller_id_update = 1;
03798             }
03799             if (ast_connected.id.number.str) {
03800                /* Save number for Caller-ID update */
03801                ast_copy_string(pri->pvts[chanpos]->cid_num,
03802                   ast_connected.id.number.str, sizeof(pri->pvts[chanpos]->cid_num));
03803                pri->pvts[chanpos]->cid_ton = ast_connected.id.number.plan;
03804                caller_id_update = 1;
03805             }
03806             ast_connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
03807 
03808             pri->pvts[chanpos]->cid_subaddr[0] = '\0';
03809 #if defined(HAVE_PRI_SUBADDR)
03810             if (ast_connected.id.subaddress.valid) {
03811                ast_party_subaddress_set(&owner->caller.id.subaddress,
03812                   &ast_connected.id.subaddress);
03813                if (ast_connected.id.subaddress.str) {
03814                   ast_copy_string(pri->pvts[chanpos]->cid_subaddr,
03815                      ast_connected.id.subaddress.str,
03816                      sizeof(pri->pvts[chanpos]->cid_subaddr));
03817                }
03818             }
03819 #endif   /* defined(HAVE_PRI_SUBADDR) */
03820             if (caller_id_update) {
03821                struct ast_party_caller ast_caller;
03822 
03823                pri->pvts[chanpos]->callingpres =
03824                   ast_party_id_presentation(&ast_connected.id);
03825                sig_pri_set_caller_id(pri->pvts[chanpos]);
03826 
03827                ast_party_caller_set_init(&ast_caller, &owner->caller);
03828                ast_caller.id = ast_connected.id;
03829                ast_caller.ani = ast_connected.id;
03830                ast_channel_set_caller_event(owner, &ast_caller, NULL);
03831             }
03832 
03833             /* Update the connected line information on the other channel */
03834             if (event_id != PRI_EVENT_RING) {
03835                /* This connected_line update was not from a SETUP message. */
03836                ast_channel_queue_connected_line_update(owner, &ast_connected, NULL);
03837             }
03838 
03839             ast_party_connected_line_free(&ast_connected);
03840             ast_channel_unlock(owner);
03841          }
03842          break;
03843       case PRI_SUBCMD_REDIRECTING:
03844          sig_pri_lock_owner(pri, chanpos);
03845          owner = pri->pvts[chanpos]->owner;
03846          if (owner) {
03847             sig_pri_redirecting_convert(&ast_redirecting, &subcmd->u.redirecting,
03848                &owner->redirecting, pri);
03849             ast_redirecting.from.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
03850             ast_redirecting.to.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
03851 
03852 /*! \todo XXX Original called data can be put in a channel data store that is inherited. */
03853 
03854             ast_channel_set_redirecting(owner, &ast_redirecting, NULL);
03855             if (event_id != PRI_EVENT_RING) {
03856                /* This redirection was not from a SETUP message. */
03857                ast_channel_queue_redirecting_update(owner, &ast_redirecting, NULL);
03858             }
03859             ast_party_redirecting_free(&ast_redirecting);
03860 
03861             ast_channel_unlock(owner);
03862          }
03863          break;
03864 #if defined(HAVE_PRI_CALL_REROUTING)
03865       case PRI_SUBCMD_REROUTING:
03866          sig_pri_lock_owner(pri, chanpos);
03867          owner = pri->pvts[chanpos]->owner;
03868          if (owner) {
03869             struct pri_party_redirecting pri_deflection;
03870 
03871             if (!call_rsp) {
03872                ast_channel_unlock(owner);
03873                ast_log(LOG_WARNING,
03874                   "CallRerouting/CallDeflection to '%s' without call!\n",
03875                   subcmd->u.rerouting.deflection.to.number.str);
03876                break;
03877             }
03878 
03879             pri_deflection = subcmd->u.rerouting.deflection;
03880 
03881             ast_string_field_set(owner, call_forward, pri_deflection.to.number.str);
03882 
03883             /* Adjust the deflecting to number based upon the subscription option. */
03884             switch (subcmd->u.rerouting.subscription_option) {
03885             case 0:  /* noNotification */
03886             case 1:  /* notificationWithoutDivertedToNr */
03887                /* Delete the number because the far end is not supposed to see it. */
03888                pri_deflection.to.number.presentation =
03889                   PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
03890                pri_deflection.to.number.plan =
03891                   (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
03892                pri_deflection.to.number.str[0] = '\0';
03893                break;
03894             case 2:  /* notificationWithDivertedToNr */
03895                break;
03896             case 3:  /* notApplicable */
03897             default:
03898                break;
03899             }
03900             sig_pri_redirecting_convert(&ast_redirecting, &pri_deflection,
03901                &owner->redirecting, pri);
03902             ast_redirecting.from.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
03903             ast_redirecting.to.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
03904             ast_channel_set_redirecting(owner, &ast_redirecting, NULL);
03905             ast_party_redirecting_free(&ast_redirecting);
03906 
03907             /*
03908              * Send back positive ACK to CallRerouting/CallDeflection.
03909              *
03910              * Note:  This call will be hungup by the dial application when
03911              * it processes the call_forward string set above.
03912              */
03913             pri_rerouting_rsp(pri->pri, call_rsp, subcmd->u.rerouting.invoke_id,
03914                PRI_REROUTING_RSP_OK_CLEAR);
03915 
03916             /* This line is BUSY to further attempts by this dialing attempt. */
03917             ast_queue_control(owner, AST_CONTROL_BUSY);
03918 
03919             ast_channel_unlock(owner);
03920          }
03921          break;
03922 #endif   /* defined(HAVE_PRI_CALL_REROUTING) */
03923 #if defined(HAVE_PRI_CCSS)
03924       case PRI_SUBCMD_CC_AVAILABLE:
03925          sig_pri_lock_owner(pri, chanpos);
03926          owner = pri->pvts[chanpos]->owner;
03927          if (owner) {
03928             enum ast_cc_service_type service;
03929 
03930             switch (event_id) {
03931             case PRI_EVENT_RINGING:
03932                service = AST_CC_CCNR;
03933                break;
03934             case PRI_EVENT_HANGUP_REQ:
03935                /* We will assume that the cause was busy/congestion. */
03936                service = AST_CC_CCBS;
03937                break;
03938             default:
03939                service = AST_CC_NONE;
03940                break;
03941             }
03942             if (service == AST_CC_NONE
03943                || sig_pri_cc_available(pri, chanpos, subcmd->u.cc_available.cc_id,
03944                service)) {
03945                pri_cc_cancel(pri->pri, subcmd->u.cc_available.cc_id);
03946             }
03947             ast_channel_unlock(owner);
03948          } else {
03949             /* No asterisk channel. */
03950             pri_cc_cancel(pri->pri, subcmd->u.cc_available.cc_id);
03951          }
03952          break;
03953 #endif   /* defined(HAVE_PRI_CCSS) */
03954 #if defined(HAVE_PRI_CCSS)
03955       case PRI_SUBCMD_CC_CALL:
03956          sig_pri_lock_owner(pri, chanpos);
03957          owner = pri->pvts[chanpos]->owner;
03958          if (owner) {
03959             struct ast_cc_agent *agent;
03960 
03961             agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_call.cc_id);
03962             if (agent) {
03963                ast_setup_cc_recall_datastore(owner, agent->core_id);
03964                ast_cc_agent_set_interfaces_chanvar(owner);
03965                ast_cc_agent_recalling(agent->core_id,
03966                   "%s caller is attempting recall", sig_pri_cc_type_name);
03967                ao2_ref(agent, -1);
03968             }
03969 
03970             ast_channel_unlock(owner);
03971          }
03972          break;
03973 #endif   /* defined(HAVE_PRI_CCSS) */
03974 #if defined(HAVE_PRI_CCSS)
03975       case PRI_SUBCMD_CC_CANCEL:
03976          sig_pri_cc_link_canceled(pri, subcmd->u.cc_cancel.cc_id,
03977             subcmd->u.cc_cancel.is_agent);
03978          break;
03979 #endif   /* defined(HAVE_PRI_CCSS) */
03980 #if defined(HAVE_PRI_TRANSFER)
03981       case PRI_SUBCMD_TRANSFER_CALL:
03982          if (!call_rsp) {
03983             /* Should never happen. */
03984             ast_log(LOG_ERROR,
03985                "Call transfer subcommand without call to send response!\n");
03986             break;
03987          }
03988 
03989          sig_pri_unlock_private(pri->pvts[chanpos]);
03990          xfer_rsp.pri = pri;
03991          xfer_rsp.call = call_rsp;
03992          xfer_rsp.invoke_id = subcmd->u.transfer.invoke_id;
03993          sig_pri_attempt_transfer(pri,
03994             subcmd->u.transfer.call_1, subcmd->u.transfer.is_call_1_held,
03995             subcmd->u.transfer.call_2, subcmd->u.transfer.is_call_2_held,
03996             sig_pri_transfer_rsp, &xfer_rsp);
03997          sig_pri_lock_private(pri->pvts[chanpos]);
03998          break;
03999 #endif   /* defined(HAVE_PRI_TRANSFER) */
04000 #if defined(HAVE_PRI_AOC_EVENTS)
04001       case PRI_SUBCMD_AOC_S:
04002          sig_pri_lock_owner(pri, chanpos);
04003          owner = pri->pvts[chanpos]->owner;
04004          if (owner) {
04005             sig_pri_aoc_s_from_pri(&subcmd->u.aoc_s, owner,
04006                (pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S));
04007             ast_channel_unlock(owner);
04008          }
04009          break;
04010 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
04011 #if defined(HAVE_PRI_AOC_EVENTS)
04012       case PRI_SUBCMD_AOC_D:
04013          sig_pri_lock_owner(pri, chanpos);
04014          owner = pri->pvts[chanpos]->owner;
04015          if (owner) {
04016             /* Queue AST_CONTROL_AOC frame on channel */
04017             sig_pri_aoc_d_from_pri(&subcmd->u.aoc_d, owner,
04018                (pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_D));
04019             ast_channel_unlock(owner);
04020          }
04021          break;
04022 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
04023 #if defined(HAVE_PRI_AOC_EVENTS)
04024       case PRI_SUBCMD_AOC_E:
04025          sig_pri_lock_owner(pri, chanpos);
04026          owner = pri->pvts[chanpos]->owner;
04027          /* Queue AST_CONTROL_AOC frame */
04028          sig_pri_aoc_e_from_pri(&subcmd->u.aoc_e, owner,
04029             (pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_E));
04030          if (owner) {
04031             ast_channel_unlock(owner);
04032          }
04033          break;
04034 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
04035 #if defined(HAVE_PRI_AOC_EVENTS)
04036       case PRI_SUBCMD_AOC_CHARGING_REQ:
04037          sig_pri_lock_owner(pri, chanpos);
04038          owner = pri->pvts[chanpos]->owner;
04039          if (owner) {
04040             sig_pri_aoc_request_from_pri(&subcmd->u.aoc_request, pri->pvts[chanpos],
04041                call_rsp);
04042             ast_channel_unlock(owner);
04043          }
04044          break;
04045 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
04046 #if defined(HAVE_PRI_AOC_EVENTS)
04047       case PRI_SUBCMD_AOC_CHARGING_REQ_RSP:
04048          /*
04049           * An AOC request response may contain an AOC-S rate list.
04050           * If this is the case handle this just like we
04051           * would an incoming AOC-S msg.
04052           */
04053          if (subcmd->u.aoc_request_response.valid_aoc_s) {
04054             sig_pri_lock_owner(pri, chanpos);
04055             owner = pri->pvts[chanpos]->owner;
04056             if (owner) {
04057                sig_pri_aoc_s_from_pri(&subcmd->u.aoc_request_response.aoc_s, owner,
04058                   (pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S));
04059                ast_channel_unlock(owner);
04060             }
04061          }
04062          break;
04063 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
04064 #if defined(HAVE_PRI_MCID)
04065       case PRI_SUBCMD_MCID_REQ:
04066          sig_pri_lock_owner(pri, chanpos);
04067          owner = pri->pvts[chanpos]->owner;
04068          sig_pri_mcid_event(pri, &subcmd->u.mcid_req, owner);
04069          if (owner) {
04070             ast_channel_unlock(owner);
04071          }
04072          break;
04073 #endif   /* defined(HAVE_PRI_MCID) */
04074 #if defined(HAVE_PRI_MCID)
04075       case PRI_SUBCMD_MCID_RSP:
04076          /* Ignore for now. */
04077          break;
04078 #endif   /* defined(HAVE_PRI_MCID) */
04079       default:
04080          ast_debug(2,
04081             "Unknown call subcommand(%d) in %s event on channel %d/%d on span %d.\n",
04082             subcmd->cmd, pri_event2str(event_id), PRI_SPAN(channel),
04083             PRI_CHANNEL(channel), pri->span);
04084          break;
04085       }
04086    }
04087 }
04088 
04089 #if defined(HAVE_PRI_CALL_HOLD)
04090 /*!
04091  * \internal
04092  * \brief Handle the hold event from libpri.
04093  * \since 1.8
04094  *
04095  * \param pri sig_pri PRI control structure.
04096  * \param ev Hold event received.
04097  *
04098  * \note Assumes the pri->lock is already obtained.
04099  *
04100  * \retval 0 on success.
04101  * \retval -1 on error.
04102  */
04103 static int sig_pri_handle_hold(struct sig_pri_span *pri, pri_event *ev)
04104 {
04105    int retval;
04106    int chanpos_old;
04107    int chanpos_new;
04108    struct ast_channel *bridged;
04109    struct ast_channel *owner;
04110 
04111    chanpos_old = pri_find_principle(pri, ev->hold.channel, ev->hold.call);
04112    if (chanpos_old < 0) {
04113       ast_log(LOG_WARNING,
04114          "Received HOLD on unconfigured channel %d/%d span %d\n",
04115          PRI_SPAN(ev->hold.channel), PRI_CHANNEL(ev->hold.channel), pri->span);
04116       return -1;
04117    }
04118    if (pri->pvts[chanpos_old]->no_b_channel) {
04119       /* Call is already on hold or is call waiting call. */
04120       return -1;
04121    }
04122 
04123    sig_pri_lock_private(pri->pvts[chanpos_old]);
04124    sig_pri_lock_owner(pri, chanpos_old);
04125    owner = pri->pvts[chanpos_old]->owner;
04126    if (!owner) {
04127       retval = -1;
04128       goto done_with_private;
04129    }
04130    bridged = ast_bridged_channel(owner);
04131    if (!bridged) {
04132       /* Cannot hold a call that is not bridged. */
04133       retval = -1;
04134       goto done_with_owner;
04135    }
04136    chanpos_new = pri_find_empty_nobch(pri);
04137    if (chanpos_new < 0) {
04138       /* No hold channel available. */
04139       retval = -1;
04140       goto done_with_owner;
04141    }
04142    sig_pri_handle_subcmds(pri, chanpos_old, ev->e, ev->hold.channel, ev->hold.subcmds,
04143       ev->hold.call);
04144    chanpos_new = pri_fixup_principle(pri, chanpos_new, ev->hold.call);
04145    if (chanpos_new < 0) {
04146       /* Should never happen. */
04147       retval = -1;
04148    } else {
04149       struct ast_frame f = { AST_FRAME_CONTROL, };
04150 
04151       /*
04152        * Things are in an odd state here so we cannot use pri_queue_control().
04153        * However, we already have the owner lock so we can simply queue the frame.
04154        */
04155       f.subclass.integer = AST_CONTROL_HOLD;
04156       ast_queue_frame(owner, &f);
04157 
04158       sig_pri_span_devstate_changed(pri);
04159       retval = 0;
04160    }
04161 
04162 done_with_owner:;
04163    ast_channel_unlock(owner);
04164 done_with_private:;
04165    sig_pri_unlock_private(pri->pvts[chanpos_old]);
04166 
04167    return retval;
04168 }
04169 #endif   /* defined(HAVE_PRI_CALL_HOLD) */
04170 
04171 #if defined(HAVE_PRI_CALL_HOLD)
04172 /*!
04173  * \internal
04174  * \brief Handle the retrieve event from libpri.
04175  * \since 1.8
04176  *
04177  * \param pri sig_pri PRI control structure.
04178  * \param ev Retrieve event received.
04179  *
04180  * \note Assumes the pri->lock is already obtained.
04181  *
04182  * \return Nothing
04183  */
04184 static void sig_pri_handle_retrieve(struct sig_pri_span *pri, pri_event *ev)
04185 {
04186    int chanpos;
04187 
04188    if (!(ev->retrieve.channel & PRI_HELD_CALL)
04189       || pri_find_principle(pri, ev->retrieve.channel, ev->retrieve.call) < 0) {
04190       /* The call is not currently held. */
04191       pri_retrieve_rej(pri->pri, ev->retrieve.call,
04192          PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED);
04193       return;
04194    }
04195    if (PRI_CHANNEL(ev->retrieve.channel) == 0xFF) {
04196       chanpos = pri_find_empty_chan(pri, 1);
04197    } else {
04198       chanpos = pri_find_principle(pri,
04199          ev->retrieve.channel & ~PRI_HELD_CALL, ev->retrieve.call);
04200       if (ev->retrieve.flexible
04201          && (chanpos < 0 || pri->pvts[chanpos]->owner)) {
04202          /*
04203           * Channel selection is flexible and the requested channel
04204           * is bad or already in use.  Pick another channel.
04205           */
04206          chanpos = pri_find_empty_chan(pri, 1);
04207       }
04208    }
04209    if (chanpos < 0) {
04210       pri_retrieve_rej(pri->pri, ev->retrieve.call,
04211          ev->retrieve.flexible ? PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION
04212          : PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
04213       return;
04214    }
04215    chanpos = pri_fixup_principle(pri, chanpos, ev->retrieve.call);
04216    if (chanpos < 0) {
04217       /* Channel is already in use. */
04218       pri_retrieve_rej(pri->pri, ev->retrieve.call,
04219          PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
04220       return;
04221    }
04222    sig_pri_lock_private(pri->pvts[chanpos]);
04223    sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->retrieve.channel,
04224       ev->retrieve.subcmds, ev->retrieve.call);
04225    pri_queue_control(pri, chanpos, AST_CONTROL_UNHOLD);
04226    sig_pri_unlock_private(pri->pvts[chanpos]);
04227    sig_pri_span_devstate_changed(pri);
04228    pri_retrieve_ack(pri->pri, ev->retrieve.call,
04229       PVT_TO_CHANNEL(pri->pvts[chanpos]));
04230 }
04231 #endif   /* defined(HAVE_PRI_CALL_HOLD) */
04232 
04233 static void *pri_dchannel(void *vpri)
04234 {
04235    struct sig_pri_span *pri = vpri;
04236    pri_event *e;
04237    struct pollfd fds[SIG_PRI_NUM_DCHANS];
04238    int res;
04239    int chanpos = 0;
04240    int x;
04241    struct ast_channel *c;
04242    struct timeval tv, lowest, *next;
04243    int doidling=0;
04244    char *cc;
04245    time_t t;
04246    int i, which=-1;
04247    int numdchans;
04248    pthread_t threadid;
04249    char ani2str[6];
04250    char plancallingnum[AST_MAX_EXTENSION];
04251    char plancallingani[AST_MAX_EXTENSION];
04252    char calledtonstr[10];
04253    struct timeval lastidle = { 0, 0 };
04254    pthread_t p;
04255    struct ast_channel *idle;
04256    char idlen[80];
04257    int nextidle = -1;
04258    int haveidles;
04259    int activeidles;
04260 
04261    gettimeofday(&lastidle, NULL);
04262    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
04263 
04264    if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
04265       /* Need to do idle dialing, check to be sure though */
04266       cc = strchr(pri->idleext, '@');
04267       if (cc) {
04268          *cc = '\0';
04269          cc++;
04270          ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
04271 #if 0
04272          /* Extensions may not be loaded yet */
04273          if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
04274             ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
04275          else
04276 #endif
04277             doidling = 1;
04278       } else
04279          ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
04280    }
04281    for (;;) {
04282       for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
04283          if (!pri->dchans[i])
04284             break;
04285          fds[i].fd = pri->fds[i];
04286          fds[i].events = POLLIN | POLLPRI;
04287          fds[i].revents = 0;
04288       }
04289       numdchans = i;
04290       time(&t);
04291       ast_mutex_lock(&pri->lock);
04292       if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->sig != SIG_BRI_PTMP) && (pri->resetinterval > 0)) {
04293          if (pri->resetting && pri_is_up(pri)) {
04294             if (pri->resetpos < 0)
04295                pri_check_restart(pri);
04296          } else {
04297             if (!pri->resetting  && (t - pri->lastreset) >= pri->resetinterval) {
04298                pri->resetting = 1;
04299                pri->resetpos = -1;
04300             }
04301          }
04302       }
04303       /* Look for any idle channels if appropriate */
04304       if (doidling && pri_is_up(pri)) {
04305          nextidle = -1;
04306          haveidles = 0;
04307          activeidles = 0;
04308          for (x = pri->numchans; x >= 0; x--) {
04309             if (pri->pvts[x]
04310                && !pri->pvts[x]->owner
04311                && !pri->pvts[x]->call
04312                && !pri->pvts[x]->no_b_channel) {
04313                if (haveidles < pri->minunused) {
04314                   haveidles++;
04315                } else if (!pri->pvts[x]->resetting) {
04316                   nextidle = x;
04317                   break;
04318                }
04319             } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
04320                activeidles++;
04321          }
04322          if (nextidle > -1) {
04323             if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
04324                /* Don't create a new idle call more than once per second */
04325                snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
04326                idle = sig_pri_request(pri->pvts[nextidle], AST_FORMAT_ULAW, NULL, 0);
04327                if (idle) {
04328                   pri->pvts[nextidle]->isidlecall = 1;
04329                   if (ast_pthread_create_background(&p, NULL, do_idle_thread, pri->pvts[nextidle])) {
04330                      ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
04331                      ast_hangup(idle);
04332                   }
04333                } else
04334                   ast_log(LOG_WARNING, "Unable to request channel 'DAHDI/%s' for idle call\n", idlen);
04335                gettimeofday(&lastidle, NULL);
04336             }
04337          } else if ((haveidles < pri->minunused) &&
04338             (activeidles > pri->minidle)) {
04339             /* Mark something for hangup if there is something
04340                that can be hungup */
04341             for (x = pri->numchans; x >= 0; x--) {
04342                /* find a candidate channel */
04343                if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
04344                   pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
04345                   haveidles++;
04346                   /* Stop if we have enough idle channels or
04347                     can't spare any more active idle ones */
04348                   if ((haveidles >= pri->minunused) ||
04349                      (activeidles <= pri->minidle))
04350                      break;
04351                }
04352             }
04353          }
04354       }
04355       /* Start with reasonable max */
04356       if (doidling || pri->resetting) {
04357          /*
04358           * Make sure we stop at least once per second if we're
04359           * monitoring idle channels
04360           */
04361          lowest = ast_tv(1, 0);
04362       } else {
04363          /* Don't poll for more than 60 seconds */
04364          lowest = ast_tv(60, 0);
04365       }
04366       for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
04367          if (!pri->dchans[i]) {
04368             /* We scanned all D channels on this span. */
04369             break;
04370          }
04371          next = pri_schedule_next(pri->dchans[i]);
04372          if (next) {
04373             /* We need relative time here */
04374             tv = ast_tvsub(*next, ast_tvnow());
04375             if (tv.tv_sec < 0) {
04376                /*
04377                 * A timer has already expired.
04378                 * By definition zero time is the lowest so we can quit early.
04379                 */
04380                lowest = ast_tv(0, 0);
04381                break;
04382             }
04383             if (ast_tvcmp(tv, lowest) < 0) {
04384                lowest = tv;
04385             }
04386          }
04387       }
04388       ast_mutex_unlock(&pri->lock);
04389 
04390       pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
04391       pthread_testcancel();
04392       e = NULL;
04393       res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
04394       pthread_testcancel();
04395       pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
04396 
04397       ast_mutex_lock(&pri->lock);
04398       if (!res) {
04399          for (which = 0; which < SIG_PRI_NUM_DCHANS; which++) {
04400             if (!pri->dchans[which])
04401                break;
04402             /* Just a timeout, run the scheduler */
04403             e = pri_schedule_run(pri->dchans[which]);
04404             if (e)
04405                break;
04406          }
04407       } else if (res > -1) {
04408          for (which = 0; which < SIG_PRI_NUM_DCHANS; which++) {
04409             if (!pri->dchans[which])
04410                break;
04411             if (fds[which].revents & POLLPRI) {
04412                sig_pri_handle_dchan_exception(pri, which);
04413             } else if (fds[which].revents & POLLIN) {
04414                e = pri_check_event(pri->dchans[which]);
04415             }
04416             if (e)
04417                break;
04418          }
04419       } else if (errno != EINTR)
04420          ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
04421 
04422       if (e) {
04423          if (pri->debug) {
04424             ast_verbose("Span: %d Processing event: %s\n",
04425                pri->span, pri_event2str(e->e));
04426          }
04427 
04428          if (e->e != PRI_EVENT_DCHAN_DOWN) {
04429             if (!(pri->dchanavail[which] & DCHAN_UP)) {
04430                ast_verb(2, "%s D-Channel on span %d up\n", pri_order(which), pri->span);
04431             }
04432             pri->dchanavail[which] |= DCHAN_UP;
04433          } else {
04434             if (pri->dchanavail[which] & DCHAN_UP) {
04435                ast_verb(2, "%s D-Channel on span %d down\n", pri_order(which), pri->span);
04436             }
04437             pri->dchanavail[which] &= ~DCHAN_UP;
04438          }
04439 
04440          if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which]))
04441             /* Must be an NFAS group that has the secondary dchan active */
04442             pri->pri = pri->dchans[which];
04443 
04444          switch (e->e) {
04445          case PRI_EVENT_DCHAN_UP:
04446             pri->no_d_channels = 0;
04447             if (!pri->pri) pri_find_dchan(pri);
04448 
04449             /* Note presense of D-channel */
04450             time(&pri->lastreset);
04451 
04452             /* Restart in 5 seconds */
04453             if (pri->resetinterval > -1) {
04454                pri->lastreset -= pri->resetinterval;
04455                pri->lastreset += 5;
04456             }
04457             pri->resetting = 0;
04458             /* Take the channels from inalarm condition */
04459             for (i = 0; i < pri->numchans; i++) {
04460                if (pri->pvts[i]) {
04461                   sig_pri_set_alarm(pri->pvts[i], 0);
04462                }
04463             }
04464             sig_pri_span_devstate_changed(pri);
04465             break;
04466          case PRI_EVENT_DCHAN_DOWN:
04467             pri_find_dchan(pri);
04468             if (!pri_is_up(pri)) {
04469                pri->resetting = 0;
04470                if (pri->sig == SIG_BRI_PTMP) {
04471                   /* For PTMP connections with non persistent layer 2 we want
04472                    * to *not* declare inalarm unless there actually is an alarm */
04473                   break;
04474                }
04475                /* Hangup active channels and put them in alarm mode */
04476                for (i = 0; i < pri->numchans; i++) {
04477                   struct sig_pri_chan *p = pri->pvts[i];
04478                   if (p) {
04479                      if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
04480                         /* T309 is not enabled : hangup calls when alarm occurs */
04481                         if (p->call) {
04482                            if (p->pri && p->pri->pri) {
04483                               pri_hangup(p->pri->pri, p->call, -1);
04484                               pri_destroycall(p->pri->pri, p->call);
04485                               p->call = NULL;
04486                            } else
04487                               ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
04488                         }
04489                         if (p->owner)
04490                            p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
04491                      }
04492                      sig_pri_set_alarm(p, 1);
04493                   }
04494                }
04495                sig_pri_span_devstate_changed(pri);
04496             }
04497             break;
04498          case PRI_EVENT_RESTART:
04499             if (e->restart.channel > -1) {
04500                chanpos = pri_find_principle(pri, e->restart.channel, NULL);
04501                if (chanpos < 0)
04502                   ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
04503                      PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
04504                else {
04505                   int skipit = 0;
04506 #if defined(HAVE_PRI_SERVICE_MESSAGES)
04507                   unsigned why;
04508 
04509                   why = pri->pvts[chanpos]->service_status;
04510                   if (why) {
04511                      ast_log(LOG_NOTICE,
04512                         "span '%d' channel '%d' out-of-service (reason: %s), ignoring RESTART\n",
04513                         pri->span, PRI_CHANNEL(e->restart.channel),
04514                         (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
04515                      skipit = 1;
04516                   }
04517 #endif   /* defined(HAVE_PRI_SERVICE_MESSAGES) */
04518                   sig_pri_lock_private(pri->pvts[chanpos]);
04519                   if (!skipit) {
04520                      ast_verb(3, "B-channel %d/%d restarted on span %d\n",
04521                         PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
04522                      if (pri->pvts[chanpos]->call) {
04523                         pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
04524                         pri->pvts[chanpos]->call = NULL;
04525                      }
04526                   }
04527                   /* Force soft hangup if appropriate */
04528                   if (pri->pvts[chanpos]->owner)
04529                      pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
04530                   sig_pri_unlock_private(pri->pvts[chanpos]);
04531                }
04532             } else {
04533                ast_verb(3, "Restart requested on entire span %d\n", pri->span);
04534                for (x = 0; x < pri->numchans; x++)
04535                   if (pri->pvts[x]) {
04536                      sig_pri_lock_private(pri->pvts[x]);
04537                      if (pri->pvts[x]->call) {
04538                         pri_destroycall(pri->pri, pri->pvts[x]->call);
04539                         pri->pvts[x]->call = NULL;
04540                      }
04541                      if (pri->pvts[x]->owner)
04542                         pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
04543                      sig_pri_unlock_private(pri->pvts[x]);
04544                   }
04545             }
04546             break;
04547          case PRI_EVENT_KEYPAD_DIGIT:
04548             if (sig_pri_is_cis_call(e->digit.channel)) {
04549                sig_pri_handle_cis_subcmds(pri, e->e, e->digit.subcmds,
04550                   e->digit.call);
04551                break;
04552             }
04553             chanpos = pri_find_principle(pri, e->digit.channel, e->digit.call);
04554             if (chanpos < 0) {
04555                ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
04556                   PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
04557             } else {
04558                chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
04559                if (chanpos > -1) {
04560                   sig_pri_lock_private(pri->pvts[chanpos]);
04561                   sig_pri_handle_subcmds(pri, chanpos, e->e, e->digit.channel,
04562                      e->digit.subcmds, e->digit.call);
04563                   /* queue DTMF frame if the PBX for this call was already started (we're forwarding KEYPAD_DIGITs further on */
04564                   if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
04565                      && pri->pvts[chanpos]->call == e->digit.call
04566                      && pri->pvts[chanpos]->owner) {
04567                      /* how to do that */
04568                      int digitlen = strlen(e->digit.digits);
04569                      int i;
04570 
04571                      for (i = 0; i < digitlen; i++) {
04572                         struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = e->digit.digits[i], };
04573 
04574                         pri_queue_frame(pri, chanpos, &f);
04575                      }
04576                   }
04577                   sig_pri_unlock_private(pri->pvts[chanpos]);
04578                }
04579             }
04580             break;
04581 
04582          case PRI_EVENT_INFO_RECEIVED:
04583             if (sig_pri_is_cis_call(e->ring.channel)) {
04584                sig_pri_handle_cis_subcmds(pri, e->e, e->ring.subcmds,
04585                   e->ring.call);
04586                break;
04587             }
04588             chanpos = pri_find_principle(pri, e->ring.channel, e->ring.call);
04589             if (chanpos < 0) {
04590                ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
04591                   PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
04592             } else {
04593                chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
04594                if (chanpos > -1) {
04595                   sig_pri_lock_private(pri->pvts[chanpos]);
04596                   sig_pri_handle_subcmds(pri, chanpos, e->e, e->ring.channel,
04597                      e->ring.subcmds, e->ring.call);
04598                   /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */
04599                   if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
04600                      && pri->pvts[chanpos]->call == e->ring.call
04601                      && pri->pvts[chanpos]->owner) {
04602                      /* how to do that */
04603                      int digitlen = strlen(e->ring.callednum);
04604                      int i;
04605 
04606                      for (i = 0; i < digitlen; i++) {
04607                         struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = e->ring.callednum[i], };
04608 
04609                         pri_queue_frame(pri, chanpos, &f);
04610                      }
04611                   }
04612                   sig_pri_unlock_private(pri->pvts[chanpos]);
04613                }
04614             }
04615             break;
04616 #if defined(HAVE_PRI_SERVICE_MESSAGES)
04617          case PRI_EVENT_SERVICE:
04618             chanpos = pri_find_principle(pri, e->service.channel, NULL);
04619             if (chanpos < 0) {
04620                ast_log(LOG_WARNING, "Received service change status %d on unconfigured channel %d/%d span %d\n",
04621                   e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
04622             } else {
04623                char db_chan_name[20];
04624                char db_answer[5];
04625                int ch;
04626                unsigned *why;
04627 
04628                ch = pri->pvts[chanpos]->channel;
04629                snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->span, ch);
04630                why = &pri->pvts[chanpos]->service_status;
04631                switch (e->service.changestatus) {
04632                case 0: /* in-service */
04633                   /* Far end wants to be in service now. */
04634                   ast_db_del(db_chan_name, SRVST_DBKEY);
04635                   *why &= ~SRVST_FAREND;
04636                   if (*why) {
04637                      snprintf(db_answer, sizeof(db_answer), "%s:%u",
04638                         SRVST_TYPE_OOS, *why);
04639                      ast_db_put(db_chan_name, SRVST_DBKEY, db_answer);
04640                   } else {
04641                      sig_pri_span_devstate_changed(pri);
04642                   }
04643                   break;
04644                case 2: /* out-of-service */
04645                   /* Far end wants to be out-of-service now. */
04646                   ast_db_del(db_chan_name, SRVST_DBKEY);
04647                   *why |= SRVST_FAREND;
04648                   snprintf(db_answer, sizeof(db_answer), "%s:%u", SRVST_TYPE_OOS,
04649                      *why);
04650                   ast_db_put(db_chan_name, SRVST_DBKEY, db_answer);
04651                   sig_pri_span_devstate_changed(pri);
04652                   break;
04653                default:
04654                   ast_log(LOG_ERROR, "Huh?  changestatus is: %d\n", e->service.changestatus);
04655                   break;
04656                }
04657                ast_log(LOG_NOTICE, "Channel %d/%d span %d (logical: %d) received a change of service message, status '%d'\n",
04658                   PRI_SPAN(e->service.channel), PRI_CHANNEL(e->service.channel), pri->span, ch, e->service.changestatus);
04659             }
04660             break;
04661          case PRI_EVENT_SERVICE_ACK:
04662             chanpos = pri_find_principle(pri, e->service_ack.channel, NULL);
04663             if (chanpos < 0) {
04664                ast_log(LOG_WARNING, "Received service acknowledge change status '%d' on unconfigured channel %d/%d span %d\n",
04665                   e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
04666             } else {
04667                ast_debug(2, "Channel %d/%d span %d received a change os service acknowledgement message, status '%d'\n",
04668                   PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span, e->service_ack.changestatus);
04669             }
04670             break;
04671 #endif   /* defined(HAVE_PRI_SERVICE_MESSAGES) */
04672          case PRI_EVENT_RING:
04673             if (!ast_strlen_zero(pri->msn_list)
04674                && !sig_pri_msn_match(pri->msn_list, e->ring.callednum)) {
04675                /* The call is not for us so ignore it. */
04676                ast_verb(3,
04677                   "Ignoring call to '%s' on span %d.  Its not in the MSN list: %s\n",
04678                   e->ring.callednum, pri->span, pri->msn_list);
04679                pri_destroycall(pri->pri, e->ring.call);
04680                break;
04681             }
04682             if (sig_pri_is_cis_call(e->ring.channel)) {
04683                sig_pri_handle_cis_subcmds(pri, e->e, e->ring.subcmds,
04684                   e->ring.call);
04685                break;
04686             }
04687             if (e->ring.channel == -1 || PRI_CHANNEL(e->ring.channel) == 0xFF) {
04688                /* Any channel requested. */
04689                chanpos = pri_find_empty_chan(pri, 1);
04690             } else if (PRI_CHANNEL(e->ring.channel) == 0x00) {
04691                /* No channel specified. */
04692 #if defined(HAVE_PRI_CALL_WAITING)
04693                if (!pri->allow_call_waiting_calls)
04694 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
04695                {
04696                   /* We will not accept incoming call waiting calls. */
04697                   pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_INCOMPATIBLE_DESTINATION);
04698                   break;
04699                }
04700 #if defined(HAVE_PRI_CALL_WAITING)
04701                chanpos = pri_find_empty_nobch(pri);
04702                if (chanpos < 0) {
04703                   /* We could not find/create a call interface. */
04704                   pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
04705                   break;
04706                }
04707                /* Setup the call interface to use. */
04708                sig_pri_init_config(pri->pvts[chanpos], pri);
04709 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
04710             } else {
04711                /* A channel is specified. */
04712                chanpos = pri_find_principle(pri, e->ring.channel, e->ring.call);
04713             }
04714             /* if no channel specified find one empty */
04715             if (chanpos < 0) {
04716                ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
04717                   PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
04718             } else {
04719                sig_pri_lock_private(pri->pvts[chanpos]);
04720                if (pri->pvts[chanpos]->owner) {
04721                   if (pri->pvts[chanpos]->call == e->ring.call) {
04722                      ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
04723                         PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
04724                      sig_pri_unlock_private(pri->pvts[chanpos]);
04725                      break;
04726                   } else {
04727                      /* This is where we handle initial glare */
04728                      ast_debug(1, "Ring requested on channel %d/%d already in use or previously requested on span %d.  Attempting to renegotiating channel.\n",
04729                      PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
04730                      sig_pri_unlock_private(pri->pvts[chanpos]);
04731                      chanpos = -1;
04732                   }
04733                }
04734                if (chanpos > -1)
04735                   sig_pri_unlock_private(pri->pvts[chanpos]);
04736             }
04737             if ((chanpos < 0) && (e->ring.flexible))
04738                chanpos = pri_find_empty_chan(pri, 1);
04739             if (chanpos > -1) {
04740                sig_pri_lock_private(pri->pvts[chanpos]);
04741                pri->pvts[chanpos]->call = e->ring.call;
04742 
04743                /* Use plancallingnum as a scratch buffer since it is initialized next. */
04744                apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri,
04745                   e->ring.redirectingnum, e->ring.callingplanrdnis);
04746                sig_pri_set_rdnis(pri->pvts[chanpos], plancallingnum);
04747 
04748                /* Setup caller-id info */
04749                apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
04750                pri->pvts[chanpos]->cid_ani2 = 0;
04751                if (pri->pvts[chanpos]->use_callerid) {
04752                   ast_shrink_phone_number(plancallingnum);
04753                   ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
04754 #ifdef PRI_ANI
04755                   if (!ast_strlen_zero(e->ring.callingani)) {
04756                      apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani);
04757                      ast_shrink_phone_number(plancallingani);
04758                      ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani));
04759                   } else {
04760                      pri->pvts[chanpos]->cid_ani[0] = '\0';
04761                   }
04762 #endif
04763                   pri->pvts[chanpos]->cid_subaddr[0] = '\0';
04764 #if defined(HAVE_PRI_SUBADDR)
04765                   if (e->ring.calling.subaddress.valid) {
04766                      struct ast_party_subaddress calling_subaddress;
04767 
04768                      ast_party_subaddress_init(&calling_subaddress);
04769                      sig_pri_set_subaddress(&calling_subaddress,
04770                         &e->ring.calling.subaddress);
04771                      if (calling_subaddress.str) {
04772                         ast_copy_string(pri->pvts[chanpos]->cid_subaddr,
04773                            calling_subaddress.str,
04774                            sizeof(pri->pvts[chanpos]->cid_subaddr));
04775                      }
04776                      ast_party_subaddress_free(&calling_subaddress);
04777                   }
04778 #endif /* defined(HAVE_PRI_SUBADDR) */
04779                   ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
04780                   pri->pvts[chanpos]->cid_ton = e->ring.callingplan; /* this is the callingplan (TON/NPI), e->ring.callingplan>>4 would be the TON */
04781                   pri->pvts[chanpos]->callingpres = e->ring.callingpres;
04782                   if (e->ring.ani2 >= 0) {
04783                      pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
04784                   }
04785                } else {
04786                   pri->pvts[chanpos]->cid_num[0] = '\0';
04787                   pri->pvts[chanpos]->cid_subaddr[0] = '\0';
04788                   pri->pvts[chanpos]->cid_ani[0] = '\0';
04789                   pri->pvts[chanpos]->cid_name[0] = '\0';
04790                   pri->pvts[chanpos]->cid_ton = 0;
04791                   pri->pvts[chanpos]->callingpres = 0;
04792                }
04793 
04794                /* Setup the user tag for party id's from this device for this call. */
04795                if (pri->append_msn_to_user_tag) {
04796                   snprintf(pri->pvts[chanpos]->user_tag,
04797                      sizeof(pri->pvts[chanpos]->user_tag), "%s_%s",
04798                      pri->initial_user_tag,
04799                      pri->nodetype == PRI_NETWORK
04800                         ? plancallingnum : e->ring.callednum);
04801                } else {
04802                   ast_copy_string(pri->pvts[chanpos]->user_tag,
04803                      pri->initial_user_tag, sizeof(pri->pvts[chanpos]->user_tag));
04804                }
04805 
04806                sig_pri_set_caller_id(pri->pvts[chanpos]);
04807 
04808                /* Set DNID on all incoming calls -- even immediate */
04809                sig_pri_set_dnid(pri->pvts[chanpos], e->ring.callednum);
04810 
04811                /* If immediate=yes go to s|1 */
04812                if (pri->pvts[chanpos]->immediate) {
04813                   ast_verb(3, "Going to extension s|1 because of immediate=yes\n");
04814                   pri->pvts[chanpos]->exten[0] = 's';
04815                   pri->pvts[chanpos]->exten[1] = '\0';
04816                }
04817                /* Get called number */
04818                else if (!ast_strlen_zero(e->ring.callednum)) {
04819                   ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
04820                } else if (pri->overlapdial)
04821                   pri->pvts[chanpos]->exten[0] = '\0';
04822                else {
04823                   /* Some PRI circuits are set up to send _no_ digits.  Handle them as 's'. */
04824                   pri->pvts[chanpos]->exten[0] = 's';
04825                   pri->pvts[chanpos]->exten[1] = '\0';
04826                }
04827                /* No number yet, but received "sending complete"? */
04828                if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
04829                   ast_verb(3, "Going to extension s|1 because of Complete received\n");
04830                   pri->pvts[chanpos]->exten[0] = 's';
04831                   pri->pvts[chanpos]->exten[1] = '\0';
04832                }
04833 
04834                /* Make sure extension exists (or in overlap dial mode, can exist) */
04835                if (((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
04836                   ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
04837                   /* Setup law */
04838                   if (e->ring.complete || !(pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
04839                      /* Just announce proceeding */
04840                      pri->pvts[chanpos]->proceeding = 1;
04841                      pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
04842                   } else {
04843                      if (pri->switchtype != PRI_SWITCH_GR303_TMC)
04844                         pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
04845                      else
04846                         pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
04847                   }
04848 
04849                   /* Start PBX */
04850                   if (!e->ring.complete
04851                      && (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
04852                      && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
04853                      /*
04854                       * Release the PRI lock while we create the channel
04855                       * so other threads can send D channel messages.
04856                       */
04857                      ast_mutex_unlock(&pri->lock);
04858                      c = sig_pri_new_ast_channel(pri->pvts[chanpos],
04859                         AST_STATE_RESERVED,
04860                         (e->ring.layer1 == PRI_LAYER_1_ALAW)
04861                            ? SIG_PRI_ALAW : SIG_PRI_ULAW,
04862                         e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
04863                      ast_mutex_lock(&pri->lock);
04864                      if (c) {
04865 #if defined(HAVE_PRI_SUBADDR)
04866                         if (e->ring.calling.subaddress.valid) {
04867                            /* Set Calling Subaddress */
04868                            sig_pri_lock_owner(pri, chanpos);
04869                            sig_pri_set_subaddress(
04870                               &pri->pvts[chanpos]->owner->caller.id.subaddress,
04871                               &e->ring.calling.subaddress);
04872                            if (!e->ring.calling.subaddress.type
04873                               && !ast_strlen_zero(
04874                                  (char *) e->ring.calling.subaddress.data)) {
04875                               /* NSAP */
04876                               pbx_builtin_setvar_helper(c, "CALLINGSUBADDR",
04877                                  (char *) e->ring.calling.subaddress.data);
04878                            }
04879                            ast_channel_unlock(c);
04880                         }
04881                         if (e->ring.called_subaddress.valid) {
04882                            /* Set Called Subaddress */
04883                            sig_pri_lock_owner(pri, chanpos);
04884                            sig_pri_set_subaddress(
04885                               &pri->pvts[chanpos]->owner->dialed.subaddress,
04886                               &e->ring.called_subaddress);
04887                            if (!e->ring.called_subaddress.type
04888                               && !ast_strlen_zero(
04889                                  (char *) e->ring.called_subaddress.data)) {
04890                               /* NSAP */
04891                               pbx_builtin_setvar_helper(c, "CALLEDSUBADDR",
04892                                  (char *) e->ring.called_subaddress.data);
04893                            }
04894                            ast_channel_unlock(c);
04895                         }
04896 #else
04897                         if (!ast_strlen_zero(e->ring.callingsubaddr)) {
04898                            pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
04899                         }
04900 #endif /* !defined(HAVE_PRI_SUBADDR) */
04901                         if (e->ring.ani2 >= 0) {
04902                            snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
04903                            pbx_builtin_setvar_helper(c, "ANI2", ani2str);
04904                         }
04905 
04906 #ifdef SUPPORT_USERUSER
04907                         if (!ast_strlen_zero(e->ring.useruserinfo)) {
04908                            pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
04909                         }
04910 #endif
04911 
04912                         snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
04913                         pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
04914                         if (e->ring.redirectingreason >= 0) {
04915                            /* This is now just a status variable.  Use REDIRECTING() dialplan function. */
04916                            pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
04917                         }
04918 #if defined(HAVE_PRI_REVERSE_CHARGE)
04919                         pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
04920 #endif
04921 #if defined(HAVE_PRI_SETUP_KEYPAD)
04922                         ast_copy_string(pri->pvts[chanpos]->keypad_digits,
04923                            e->ring.keypad_digits,
04924                            sizeof(pri->pvts[chanpos]->keypad_digits));
04925 #endif   /* defined(HAVE_PRI_SETUP_KEYPAD) */
04926 
04927                         sig_pri_handle_subcmds(pri, chanpos, e->e, e->ring.channel,
04928                            e->ring.subcmds, e->ring.call);
04929 
04930                      }
04931                      if (c && !ast_pthread_create_detached(&threadid, NULL, pri_ss_thread, pri->pvts[chanpos])) {
04932                         ast_verb(3, "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
04933                            plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
04934                            pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
04935                      } else {
04936                         ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
04937                            pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
04938                         if (c)
04939                            ast_hangup(c);
04940                         else {
04941                            pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
04942                            pri->pvts[chanpos]->call = NULL;
04943                         }
04944                      }
04945                   } else {
04946                      /*
04947                       * Release the PRI lock while we create the channel
04948                       * so other threads can send D channel messages.
04949                       */
04950                      ast_mutex_unlock(&pri->lock);
04951                      c = sig_pri_new_ast_channel(pri->pvts[chanpos],
04952                         AST_STATE_RING,
04953                         (e->ring.layer1 == PRI_LAYER_1_ALAW)
04954                            ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype,
04955                         pri->pvts[chanpos]->exten, NULL);
04956                      ast_mutex_lock(&pri->lock);
04957                      if (c) {
04958                         /*
04959                          * It is reasonably safe to set the following
04960                          * channel variables while the PRI and DAHDI private
04961                          * structures are locked.  The PBX has not been
04962                          * started yet and it is unlikely that any other task
04963                          * will do anything with the channel we have just
04964                          * created.
04965                          */
04966 #if defined(HAVE_PRI_SUBADDR)
04967                         if (e->ring.calling.subaddress.valid) {
04968                            /* Set Calling Subaddress */
04969                            sig_pri_lock_owner(pri, chanpos);
04970                            sig_pri_set_subaddress(
04971                               &pri->pvts[chanpos]->owner->caller.id.subaddress,
04972                               &e->ring.calling.subaddress);
04973                            if (!e->ring.calling.subaddress.type
04974                               && !ast_strlen_zero(
04975                                  (char *) e->ring.calling.subaddress.data)) {
04976                               /* NSAP */
04977                               pbx_builtin_setvar_helper(c, "CALLINGSUBADDR",
04978                                  (char *) e->ring.calling.subaddress.data);
04979                            }
04980                            ast_channel_unlock(c);
04981                         }
04982                         if (e->ring.called_subaddress.valid) {
04983                            /* Set Called Subaddress */
04984                            sig_pri_lock_owner(pri, chanpos);
04985                            sig_pri_set_subaddress(
04986                               &pri->pvts[chanpos]->owner->dialed.subaddress,
04987                               &e->ring.called_subaddress);
04988                            if (!e->ring.called_subaddress.type
04989                               && !ast_strlen_zero(
04990                                  (char *) e->ring.called_subaddress.data)) {
04991                               /* NSAP */
04992                               pbx_builtin_setvar_helper(c, "CALLEDSUBADDR",
04993                                  (char *) e->ring.called_subaddress.data);
04994                            }
04995                            ast_channel_unlock(c);
04996                         }
04997 #else
04998                         if (!ast_strlen_zero(e->ring.callingsubaddr)) {
04999                            pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
05000                         }
05001 #endif /* !defined(HAVE_PRI_SUBADDR) */
05002                         if (e->ring.ani2 >= 0) {
05003                            snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
05004                            pbx_builtin_setvar_helper(c, "ANI2", ani2str);
05005                         }
05006 
05007 #ifdef SUPPORT_USERUSER
05008                         if (!ast_strlen_zero(e->ring.useruserinfo)) {
05009                            pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
05010                         }
05011 #endif
05012 
05013                         if (e->ring.redirectingreason >= 0) {
05014                            /* This is now just a status variable.  Use REDIRECTING() dialplan function. */
05015                            pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
05016                         }
05017 #if defined(HAVE_PRI_REVERSE_CHARGE)
05018                         pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
05019 #endif
05020 #if defined(HAVE_PRI_SETUP_KEYPAD)
05021                         ast_copy_string(pri->pvts[chanpos]->keypad_digits,
05022                            e->ring.keypad_digits,
05023                            sizeof(pri->pvts[chanpos]->keypad_digits));
05024 #endif   /* defined(HAVE_PRI_SETUP_KEYPAD) */
05025 
05026                         snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
05027                         pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
05028 
05029                         sig_pri_handle_subcmds(pri, chanpos, e->e, e->ring.channel,
05030                            e->ring.subcmds, e->ring.call);
05031 
05032                      }
05033                      if (c && !ast_pbx_start(c)) {
05034                         ast_verb(3, "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
05035                            plancallingnum, pri->pvts[chanpos]->exten,
05036                            pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
05037                         sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
05038                      } else {
05039                         ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
05040                            pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
05041                         if (c) {
05042                            ast_hangup(c);
05043                         } else {
05044                            pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
05045                            pri->pvts[chanpos]->call = NULL;
05046                         }
05047                      }
05048                   }
05049                } else {
05050                   ast_verb(3, "Extension '%s' in context '%s' from '%s' does not exist.  Rejecting call on channel %d/%d, span %d\n",
05051                      pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
05052                      pri->pvts[chanpos]->prioffset, pri->span);
05053                   pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
05054                   pri->pvts[chanpos]->call = NULL;
05055                   pri->pvts[chanpos]->exten[0] = '\0';
05056                }
05057                sig_pri_unlock_private(pri->pvts[chanpos]);
05058             } else {
05059                if (e->ring.flexible)
05060                   pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
05061                else
05062                   pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
05063             }
05064             break;
05065          case PRI_EVENT_RINGING:
05066             if (sig_pri_is_cis_call(e->ringing.channel)) {
05067                sig_pri_handle_cis_subcmds(pri, e->e, e->ringing.subcmds,
05068                   e->ringing.call);
05069                break;
05070             }
05071             chanpos = pri_find_principle(pri, e->ringing.channel, e->ringing.call);
05072             if (chanpos < 0) {
05073                ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
05074                   PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
05075             } else {
05076                chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
05077                if (chanpos < 0) {
05078                   ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
05079                      PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
05080                } else {
05081                   sig_pri_lock_private(pri->pvts[chanpos]);
05082 
05083                   sig_pri_handle_subcmds(pri, chanpos, e->e, e->ringing.channel,
05084                      e->ringing.subcmds, e->ringing.call);
05085                   sig_pri_cc_generic_check(pri, chanpos, AST_CC_CCNR);
05086                   sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
05087                   pri_queue_control(pri, chanpos, AST_CONTROL_RINGING);
05088                   pri->pvts[chanpos]->alerting = 1;
05089 
05090                   if (
05091 #ifdef PRI_PROGRESS_MASK
05092                      e->ringing.progressmask & PRI_PROG_INBAND_AVAILABLE
05093 #else
05094                      e->ringing.progress == 8
05095 #endif
05096                      ) {
05097                      sig_pri_open_media(pri->pvts[chanpos]);
05098                   }
05099 
05100 
05101 #ifdef SUPPORT_USERUSER
05102                   if (!ast_strlen_zero(e->ringing.useruserinfo)) {
05103                      struct ast_channel *owner;
05104 
05105                      sig_pri_lock_owner(pri, chanpos);
05106                      owner = pri->pvts[chanpos]->owner;
05107                      if (owner) {
05108                         pbx_builtin_setvar_helper(owner, "USERUSERINFO",
05109                            e->ringing.useruserinfo);
05110                         ast_channel_unlock(owner);
05111                      }
05112                   }
05113 #endif
05114 
05115                   sig_pri_unlock_private(pri->pvts[chanpos]);
05116                }
05117             }
05118             break;
05119          case PRI_EVENT_PROGRESS:
05120             if (sig_pri_is_cis_call(e->proceeding.channel)) {
05121                sig_pri_handle_cis_subcmds(pri, e->e, e->proceeding.subcmds,
05122                   e->proceeding.call);
05123                break;
05124             }
05125             chanpos = pri_find_principle(pri, e->proceeding.channel, e->proceeding.call);
05126             if (chanpos > -1) {
05127                sig_pri_lock_private(pri->pvts[chanpos]);
05128                sig_pri_handle_subcmds(pri, chanpos, e->e, e->proceeding.channel,
05129                   e->proceeding.subcmds, e->proceeding.call);
05130 
05131                if (e->proceeding.cause > -1) {
05132                   ast_verb(3, "PROGRESS with cause code %d received\n", e->proceeding.cause);
05133 
05134                   /* Work around broken, out of spec USER_BUSY cause in a progress message */
05135                   if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
05136                      if (pri->pvts[chanpos]->owner) {
05137                         ast_verb(3, "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
05138 
05139                         pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
05140                         pri_queue_control(pri, chanpos, AST_CONTROL_BUSY);
05141                      }
05142                   }
05143                }
05144 
05145                if (!pri->pvts[chanpos]->progress
05146                   && !pri->pvts[chanpos]->no_b_channel
05147 #ifdef PRI_PROGRESS_MASK
05148                   && (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)
05149 #else
05150                   && e->proceeding.progress == 8
05151 #endif
05152                   ) {
05153                   /* Bring voice path up */
05154                   ast_debug(1, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
05155                      pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
05156                   pri_queue_control(pri, chanpos, AST_CONTROL_PROGRESS);
05157                   pri->pvts[chanpos]->progress = 1;
05158                   sig_pri_set_dialing(pri->pvts[chanpos], 0);
05159                   sig_pri_open_media(pri->pvts[chanpos]);
05160                }
05161                sig_pri_unlock_private(pri->pvts[chanpos]);
05162             }
05163             break;
05164          case PRI_EVENT_PROCEEDING:
05165             if (sig_pri_is_cis_call(e->proceeding.channel)) {
05166                sig_pri_handle_cis_subcmds(pri, e->e, e->proceeding.subcmds,
05167                   e->proceeding.call);
05168                break;
05169             }
05170             chanpos = pri_find_principle(pri, e->proceeding.channel, e->proceeding.call);
05171             if (chanpos > -1) {
05172                sig_pri_lock_private(pri->pvts[chanpos]);
05173                sig_pri_handle_subcmds(pri, chanpos, e->e, e->proceeding.channel,
05174                   e->proceeding.subcmds, e->proceeding.call);
05175                if (!pri->pvts[chanpos]->proceeding) {
05176                   ast_debug(1, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
05177                      pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
05178                   pri_queue_control(pri, chanpos, AST_CONTROL_PROCEEDING);
05179                   pri->pvts[chanpos]->proceeding = 1;
05180                }
05181                if (!pri->pvts[chanpos]->progress
05182                   && !pri->pvts[chanpos]->no_b_channel
05183 #ifdef PRI_PROGRESS_MASK
05184                   && (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)
05185 #else
05186                   && e->proceeding.progress == 8
05187 #endif
05188                   ) {
05189                   /* Bring voice path up */
05190                   pri_queue_control(pri, chanpos, AST_CONTROL_PROGRESS);
05191                   pri->pvts[chanpos]->progress = 1;
05192                   sig_pri_set_dialing(pri->pvts[chanpos], 0);
05193                   sig_pri_open_media(pri->pvts[chanpos]);
05194                }
05195                sig_pri_unlock_private(pri->pvts[chanpos]);
05196             }
05197             break;
05198          case PRI_EVENT_FACILITY:
05199             if (!e->facility.call || sig_pri_is_cis_call(e->facility.channel)) {
05200                /* Event came in on the dummy channel or a CIS call. */
05201 #if defined(HAVE_PRI_CALL_REROUTING)
05202                sig_pri_handle_cis_subcmds(pri, e->e, e->facility.subcmds,
05203                   e->facility.subcall);
05204 #else
05205                sig_pri_handle_cis_subcmds(pri, e->e, e->facility.subcmds,
05206                   e->facility.call);
05207 #endif   /* !defined(HAVE_PRI_CALL_REROUTING) */
05208                break;
05209             }
05210             chanpos = pri_find_principle(pri, e->facility.channel, e->facility.call);
05211             if (chanpos < 0) {
05212                ast_log(LOG_WARNING, "Facility requested on unconfigured channel %d/%d span %d\n",
05213                   PRI_SPAN(e->facility.channel), PRI_CHANNEL(e->facility.channel), pri->span);
05214             } else {
05215                chanpos = pri_fixup_principle(pri, chanpos, e->facility.call);
05216                if (chanpos < 0) {
05217                   ast_log(LOG_WARNING, "Facility requested on channel %d/%d not in use on span %d\n",
05218                      PRI_SPAN(e->facility.channel), PRI_CHANNEL(e->facility.channel), pri->span);
05219                } else {
05220                   sig_pri_lock_private(pri->pvts[chanpos]);
05221 #if defined(HAVE_PRI_CALL_REROUTING)
05222                   sig_pri_handle_subcmds(pri, chanpos, e->e, e->facility.channel,
05223                      e->facility.subcmds, e->facility.subcall);
05224 #else
05225                   sig_pri_handle_subcmds(pri, chanpos, e->e, e->facility.channel,
05226                      e->facility.subcmds, e->facility.call);
05227 #endif   /* !defined(HAVE_PRI_CALL_REROUTING) */
05228                   sig_pri_unlock_private(pri->pvts[chanpos]);
05229                }
05230             }
05231             break;
05232          case PRI_EVENT_ANSWER:
05233             if (sig_pri_is_cis_call(e->answer.channel)) {
05234 #if defined(HAVE_PRI_CALL_WAITING)
05235                /* Call is CIS so do normal CONNECT_ACKNOWLEDGE. */
05236                pri_connect_ack(pri->pri, e->answer.call, 0);
05237 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
05238                sig_pri_handle_cis_subcmds(pri, e->e, e->answer.subcmds,
05239                   e->answer.call);
05240                break;
05241             }
05242             chanpos = pri_find_principle(pri, e->answer.channel, e->answer.call);
05243             if (chanpos < 0) {
05244                ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
05245                   PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
05246                break;
05247             }
05248             chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
05249             if (chanpos < 0) {
05250                ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
05251                   PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
05252                break;
05253             }
05254 #if defined(HAVE_PRI_CALL_WAITING)
05255             if (pri->pvts[chanpos]->is_call_waiting) {
05256                if (pri->pvts[chanpos]->no_b_channel) {
05257                   int new_chanpos;
05258 
05259                   /*
05260                    * Need to find a free channel now or
05261                    * kill the call with PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION.
05262                    */
05263                   new_chanpos = pri_find_empty_chan(pri, 1);
05264                   if (new_chanpos < 0) {
05265                      sig_pri_lock_private(pri->pvts[chanpos]);
05266                      sig_pri_handle_subcmds(pri, chanpos, e->e, e->answer.channel,
05267                         e->answer.subcmds, e->answer.call);
05268                      sig_pri_cc_generic_check(pri, chanpos, AST_CC_CCBS);
05269                      if (pri->pvts[chanpos]->owner) {
05270                         pri->pvts[chanpos]->owner->hangupcause = PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION;
05271                         switch (pri->pvts[chanpos]->owner->_state) {
05272                         case AST_STATE_BUSY:
05273                         case AST_STATE_UP:
05274                            ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
05275                            break;
05276                         default:
05277                            pri_queue_control(pri, chanpos, AST_CONTROL_CONGESTION);
05278                            break;
05279                         }
05280                      } else {
05281                         pri->pvts[chanpos]->is_call_waiting = 0;
05282                         ast_atomic_fetchadd_int(&pri->num_call_waiting_calls, -1);
05283                         pri_hangup(pri->pri, e->answer.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
05284                         pri->pvts[chanpos]->call = NULL;
05285                      }
05286                      sig_pri_unlock_private(pri->pvts[chanpos]);
05287                      break;
05288                   }
05289                   chanpos = pri_fixup_principle(pri, new_chanpos, e->answer.call);
05290                   if (chanpos < 0) {
05291                      ast_log(LOG_WARNING,
05292                         "Unable to move call waiting call channel on span %d\n",
05293                         pri->span);
05294                      break;
05295                   }
05296                }
05297                pri_connect_ack(pri->pri, e->answer.call, PVT_TO_CHANNEL(pri->pvts[chanpos]));
05298             } else {
05299                /* Call is normal so do normal CONNECT_ACKNOWLEDGE. */
05300                pri_connect_ack(pri->pri, e->answer.call, 0);
05301             }
05302 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
05303             sig_pri_lock_private(pri->pvts[chanpos]);
05304 
05305 #if defined(HAVE_PRI_CALL_WAITING)
05306             if (pri->pvts[chanpos]->is_call_waiting) {
05307                pri->pvts[chanpos]->is_call_waiting = 0;
05308                ast_atomic_fetchadd_int(&pri->num_call_waiting_calls, -1);
05309                sig_pri_span_devstate_changed(pri);
05310             }
05311 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
05312             sig_pri_handle_subcmds(pri, chanpos, e->e, e->answer.channel,
05313                e->answer.subcmds, e->answer.call);
05314             pri->pvts[chanpos]->proceeding = 1;
05315             sig_pri_open_media(pri->pvts[chanpos]);
05316             pri_queue_control(pri, chanpos, AST_CONTROL_ANSWER);
05317             /* Enable echo cancellation if it's not on already */
05318             sig_pri_set_dialing(pri->pvts[chanpos], 0);
05319             sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
05320 
05321 #ifdef SUPPORT_USERUSER
05322             if (!ast_strlen_zero(e->answer.useruserinfo)) {
05323                struct ast_channel *owner;
05324 
05325                sig_pri_lock_owner(pri, chanpos);
05326                owner = pri->pvts[chanpos]->owner;
05327                if (owner) {
05328                   pbx_builtin_setvar_helper(owner, "USERUSERINFO",
05329                      e->answer.useruserinfo);
05330                   ast_channel_unlock(owner);
05331                }
05332             }
05333 #endif
05334 
05335             sig_pri_unlock_private(pri->pvts[chanpos]);
05336             break;
05337 #if defined(HAVE_PRI_CALL_WAITING)
05338          case PRI_EVENT_CONNECT_ACK:
05339             if (sig_pri_is_cis_call(e->connect_ack.channel)) {
05340                sig_pri_handle_cis_subcmds(pri, e->e, e->connect_ack.subcmds,
05341                   e->connect_ack.call);
05342                break;
05343             }
05344             chanpos = pri_find_principle(pri, e->connect_ack.channel,
05345                e->connect_ack.call);
05346             if (chanpos < 0) {
05347                ast_log(LOG_WARNING, "Connect ACK on unconfigured channel %d/%d span %d\n",
05348                   PRI_SPAN(e->connect_ack.channel),
05349                   PRI_CHANNEL(e->connect_ack.channel), pri->span);
05350                break;
05351             }
05352             chanpos = pri_fixup_principle(pri, chanpos, e->connect_ack.call);
05353             if (chanpos < 0) {
05354                ast_log(LOG_WARNING, "Connect ACK requested on channel %d/%d not in use on span %d\n",
05355                   PRI_SPAN(e->connect_ack.channel),
05356                   PRI_CHANNEL(e->connect_ack.channel), pri->span);
05357                break;
05358             }
05359 
05360             sig_pri_lock_private(pri->pvts[chanpos]);
05361             sig_pri_span_devstate_changed(pri);
05362             sig_pri_handle_subcmds(pri, chanpos, e->e, e->connect_ack.channel,
05363                e->connect_ack.subcmds, e->connect_ack.call);
05364             sig_pri_open_media(pri->pvts[chanpos]);
05365             sig_pri_unlock_private(pri->pvts[chanpos]);
05366             break;
05367 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
05368          case PRI_EVENT_HANGUP:
05369             if (sig_pri_is_cis_call(e->hangup.channel)) {
05370                sig_pri_handle_cis_subcmds(pri, e->e, e->hangup.subcmds,
05371                   e->hangup.call);
05372                pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
05373                break;
05374             }
05375             chanpos = pri_find_principle(pri, e->hangup.channel, e->hangup.call);
05376             if (chanpos < 0) {
05377                ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
05378                   PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
05379                /*
05380                 * Continue hanging up the call even though
05381                 * it is on an unconfigured channel.
05382                 */
05383                pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
05384             } else {
05385                chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
05386                if (chanpos > -1) {
05387                   sig_pri_lock_private(pri->pvts[chanpos]);
05388                   sig_pri_handle_subcmds(pri, chanpos, e->e, e->hangup.channel,
05389                      e->hangup.subcmds, e->hangup.call);
05390                   if (!pri->pvts[chanpos]->alreadyhungup) {
05391                      /* we're calling here dahdi_hangup so once we get there we need to clear p->call after calling pri_hangup */
05392                      pri->pvts[chanpos]->alreadyhungup = 1;
05393                      switch (e->hangup.cause) {
05394                      case PRI_CAUSE_USER_BUSY:
05395                      case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
05396                         sig_pri_cc_generic_check(pri, chanpos, AST_CC_CCBS);
05397                         break;
05398                      default:
05399                         break;
05400                      }
05401                      if (pri->pvts[chanpos]->owner) {
05402                         int do_hangup = 0;
05403                         /* Queue a BUSY instead of a hangup if our cause is appropriate */
05404                         pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
05405                         switch (pri->pvts[chanpos]->owner->_state) {
05406                         case AST_STATE_BUSY:
05407                         case AST_STATE_UP:
05408                            do_hangup = 1;
05409                            break;
05410                         default:
05411                            if (!pri->pvts[chanpos]->outgoing) {
05412                               /*
05413                                * The incoming call leg hung up before getting
05414                                * connected so just hangup the call.
05415                                */
05416                               do_hangup = 1;
05417                               break;
05418                            }
05419                            switch (e->hangup.cause) {
05420                            case PRI_CAUSE_USER_BUSY:
05421                               pri_queue_control(pri, chanpos, AST_CONTROL_BUSY);
05422                               break;
05423                            case PRI_CAUSE_CALL_REJECTED:
05424                            case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
05425                            case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
05426                            case PRI_CAUSE_SWITCH_CONGESTION:
05427                            case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
05428                            case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
05429                               pri_queue_control(pri, chanpos, AST_CONTROL_CONGESTION);
05430                               break;
05431                            default:
05432                               do_hangup = 1;
05433                               break;
05434                            }
05435                            break;
05436                         }
05437 
05438                         if (do_hangup) {
05439 #if defined(HAVE_PRI_AOC_EVENTS)
05440                            if (detect_aoc_e_subcmd(e->hangup.subcmds)) {
05441                               /* If a AOC-E msg was sent during the release, we must use a
05442                                * AST_CONTROL_HANGUP frame to guarantee that frame gets read before hangup */
05443                               pri_queue_control(pri, chanpos, AST_CONTROL_HANGUP);
05444                            } else {
05445                               pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
05446                            }
05447 #else
05448                            pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
05449 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
05450                         }
05451                      } else {
05452                         /*
05453                          * Continue hanging up the call even though
05454                          * we do not have an owner.
05455                          */
05456                         pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
05457                         pri->pvts[chanpos]->call = NULL;
05458                      }
05459                      ast_verb(3, "Channel %d/%d, span %d got hangup, cause %d\n",
05460                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, e->hangup.cause);
05461                   } else {
05462                      /* Continue hanging up the call. */
05463                      pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
05464                      pri->pvts[chanpos]->call = NULL;
05465                   }
05466                   if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
05467                      ast_verb(3, "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
05468                         PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
05469                      pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
05470                      pri->pvts[chanpos]->resetting = 1;
05471                   }
05472                   if (e->hangup.aoc_units > -1)
05473                      ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
05474                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
05475 
05476 #ifdef SUPPORT_USERUSER
05477                   if (!ast_strlen_zero(e->hangup.useruserinfo)) {
05478                      struct ast_channel *owner;
05479 
05480                      sig_pri_lock_owner(pri, chanpos);
05481                      owner = pri->pvts[chanpos]->owner;
05482                      if (owner) {
05483                         pbx_builtin_setvar_helper(owner, "USERUSERINFO",
05484                            e->hangup.useruserinfo);
05485                         ast_channel_unlock(owner);
05486                      }
05487                   }
05488 #endif
05489 
05490                   sig_pri_unlock_private(pri->pvts[chanpos]);
05491                } else {
05492                   /*
05493                    * Continue hanging up the call even though
05494                    * we do not remember it (if we ever did).
05495                    */
05496                   pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
05497                }
05498             }
05499             break;
05500          case PRI_EVENT_HANGUP_REQ:
05501             if (sig_pri_is_cis_call(e->hangup.channel)) {
05502                sig_pri_handle_cis_subcmds(pri, e->e, e->hangup.subcmds,
05503                   e->hangup.call);
05504                pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
05505                break;
05506             }
05507             chanpos = pri_find_principle(pri, e->hangup.channel, e->hangup.call);
05508             if (chanpos < 0) {
05509                ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
05510                   PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
05511                /*
05512                 * Continue hanging up the call even though
05513                 * it is on an unconfigured channel.
05514                 */
05515                pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
05516             } else {
05517                chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
05518                if (chanpos > -1) {
05519                   sig_pri_lock_private(pri->pvts[chanpos]);
05520                   sig_pri_handle_subcmds(pri, chanpos, e->e, e->hangup.channel,
05521                      e->hangup.subcmds, e->hangup.call);
05522 #if defined(HAVE_PRI_CALL_HOLD)
05523                   if (e->hangup.call_active && e->hangup.call_held
05524                      && pri->hold_disconnect_transfer) {
05525                      /* We are to transfer the call instead of simply hanging up. */
05526                      sig_pri_unlock_private(pri->pvts[chanpos]);
05527                      if (!sig_pri_attempt_transfer(pri, e->hangup.call_held, 1,
05528                         e->hangup.call_active, 0, NULL, NULL)) {
05529                         break;
05530                      }
05531                      sig_pri_lock_private(pri->pvts[chanpos]);
05532                   }
05533 #endif   /* defined(HAVE_PRI_CALL_HOLD) */
05534                   switch (e->hangup.cause) {
05535                   case PRI_CAUSE_USER_BUSY:
05536                   case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
05537                      sig_pri_cc_generic_check(pri, chanpos, AST_CC_CCBS);
05538                      break;
05539                   default:
05540                      break;
05541                   }
05542                   if (pri->pvts[chanpos]->owner) {
05543                      int do_hangup = 0;
05544 
05545                      pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
05546                      switch (pri->pvts[chanpos]->owner->_state) {
05547                      case AST_STATE_BUSY:
05548                      case AST_STATE_UP:
05549                         do_hangup = 1;
05550                         break;
05551                      default:
05552                         if (!pri->pvts[chanpos]->outgoing) {
05553                            /*
05554                             * The incoming call leg hung up before getting
05555                             * connected so just hangup the call.
05556                             */
05557                            do_hangup = 1;
05558                            break;
05559                         }
05560                         switch (e->hangup.cause) {
05561                         case PRI_CAUSE_USER_BUSY:
05562                            pri_queue_control(pri, chanpos, AST_CONTROL_BUSY);
05563                            break;
05564                         case PRI_CAUSE_CALL_REJECTED:
05565                         case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
05566                         case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
05567                         case PRI_CAUSE_SWITCH_CONGESTION:
05568                         case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
05569                         case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
05570                            pri_queue_control(pri, chanpos, AST_CONTROL_CONGESTION);
05571                            break;
05572                         default:
05573                            do_hangup = 1;
05574                            break;
05575                         }
05576                         break;
05577                      }
05578 
05579                      if (do_hangup) {
05580 #if defined(HAVE_PRI_AOC_EVENTS)
05581                         if (!pri->pvts[chanpos]->holding_aoce
05582                            && pri->aoce_delayhangup
05583                            && ast_bridged_channel(pri->pvts[chanpos]->owner)) {
05584                            sig_pri_send_aoce_termination_request(pri, chanpos,
05585                               pri_get_timer(pri->pri, PRI_TIMER_T305) / 2);
05586                         } else if (detect_aoc_e_subcmd(e->hangup.subcmds)) {
05587                            /* If a AOC-E msg was sent during the Disconnect, we must use a AST_CONTROL_HANGUP frame
05588                             * to guarantee that frame gets read before hangup */
05589                            pri_queue_control(pri, chanpos, AST_CONTROL_HANGUP);
05590                         } else {
05591                            pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
05592                         }
05593 #else
05594                         pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
05595 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
05596                      }
05597                      ast_verb(3, "Channel %d/%d, span %d got hangup request, cause %d\n",
05598                         PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span, e->hangup.cause);
05599                   } else {
05600                      /*
05601                       * Continue hanging up the call even though
05602                       * we do not have an owner.
05603                       */
05604                      pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
05605                      pri->pvts[chanpos]->call = NULL;
05606                   }
05607                   if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
05608                      ast_verb(3, "Forcing restart of channel %d/%d span %d since channel reported in use\n",
05609                         PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
05610                      pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
05611                      pri->pvts[chanpos]->resetting = 1;
05612                   }
05613 
05614 #ifdef SUPPORT_USERUSER
05615                   if (!ast_strlen_zero(e->hangup.useruserinfo)) {
05616                      struct ast_channel *owner;
05617 
05618                      sig_pri_lock_owner(pri, chanpos);
05619                      owner = pri->pvts[chanpos]->owner;
05620                      if (owner) {
05621                         pbx_builtin_setvar_helper(owner, "USERUSERINFO",
05622                            e->hangup.useruserinfo);
05623                         ast_channel_unlock(owner);
05624                      }
05625                   }
05626 #endif
05627 
05628                   sig_pri_unlock_private(pri->pvts[chanpos]);
05629                } else {
05630                   /*
05631                    * Continue hanging up the call even though
05632                    * we do not remember it (if we ever did).
05633                    */
05634                   pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
05635                }
05636             }
05637             break;
05638          case PRI_EVENT_HANGUP_ACK:
05639             if (sig_pri_is_cis_call(e->hangup.channel)) {
05640                sig_pri_handle_cis_subcmds(pri, e->e, e->hangup.subcmds,
05641                   e->hangup.call);
05642                break;
05643             }
05644             chanpos = pri_find_principle(pri, e->hangup.channel, e->hangup.call);
05645             if (chanpos < 0) {
05646                ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
05647                   PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
05648             } else {
05649                chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
05650                if (chanpos > -1) {
05651                   sig_pri_lock_private(pri->pvts[chanpos]);
05652                   pri->pvts[chanpos]->call = NULL;
05653                   pri->pvts[chanpos]->resetting = 0;
05654                   if (pri->pvts[chanpos]->owner) {
05655                      ast_verb(3, "Channel %d/%d, span %d got hangup ACK\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
05656                   }
05657 #ifdef SUPPORT_USERUSER
05658                   if (!ast_strlen_zero(e->hangup.useruserinfo)) {
05659                      struct ast_channel *owner;
05660 
05661                      sig_pri_lock_owner(pri, chanpos);
05662                      owner = pri->pvts[chanpos]->owner;
05663                      if (owner) {
05664                         pbx_builtin_setvar_helper(owner, "USERUSERINFO",
05665                            e->hangup.useruserinfo);
05666                         ast_channel_unlock(owner);
05667                      }
05668                   }
05669 #endif
05670                   sig_pri_unlock_private(pri->pvts[chanpos]);
05671                }
05672             }
05673             break;
05674          case PRI_EVENT_CONFIG_ERR:
05675             ast_log(LOG_WARNING, "PRI Error on span %d: %s\n", pri->span, e->err.err);
05676             break;
05677          case PRI_EVENT_RESTART_ACK:
05678             chanpos = pri_find_principle(pri, e->restartack.channel, NULL);
05679             if (chanpos < 0) {
05680                /* Sometime switches (e.g. I421 / British Telecom) don't give us the
05681                   channel number, so we have to figure it out...  This must be why
05682                   everybody resets exactly a channel at a time. */
05683                for (x = 0; x < pri->numchans; x++) {
05684                   if (pri->pvts[x] && pri->pvts[x]->resetting) {
05685                      chanpos = x;
05686                      sig_pri_lock_private(pri->pvts[chanpos]);
05687                      ast_debug(1, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
05688                         pri->pvts[chanpos]->prioffset, pri->span);
05689                      if (pri->pvts[chanpos]->owner) {
05690                         ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
05691                            pri->pvts[chanpos]->prioffset, pri->span);
05692                         pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
05693                      }
05694                      pri->pvts[chanpos]->resetting = 0;
05695                      ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
05696                         pri->pvts[chanpos]->prioffset, pri->span);
05697                      sig_pri_unlock_private(pri->pvts[chanpos]);
05698                      if (pri->resetting)
05699                         pri_check_restart(pri);
05700                      break;
05701                   }
05702                }
05703                if (chanpos < 0) {
05704                   ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
05705                      PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
05706                }
05707             } else {
05708                if (pri->pvts[chanpos]) {
05709                   sig_pri_lock_private(pri->pvts[chanpos]);
05710                   if (pri->pvts[chanpos]->owner) {
05711                      ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
05712                         PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
05713                      pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
05714                   }
05715                   pri->pvts[chanpos]->resetting = 0;
05716                   ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
05717                      pri->pvts[chanpos]->prioffset, pri->span);
05718                   sig_pri_unlock_private(pri->pvts[chanpos]);
05719                   if (pri->resetting)
05720                      pri_check_restart(pri);
05721                }
05722             }
05723             break;
05724          case PRI_EVENT_SETUP_ACK:
05725             if (sig_pri_is_cis_call(e->setup_ack.channel)) {
05726                sig_pri_handle_cis_subcmds(pri, e->e, e->setup_ack.subcmds,
05727                   e->setup_ack.call);
05728                break;
05729             }
05730             chanpos = pri_find_principle(pri, e->setup_ack.channel, e->setup_ack.call);
05731             if (chanpos < 0) {
05732                ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
05733                   PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
05734             } else {
05735                chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call);
05736                if (chanpos > -1) {
05737                   sig_pri_lock_private(pri->pvts[chanpos]);
05738                   sig_pri_handle_subcmds(pri, chanpos, e->e, e->setup_ack.channel,
05739                      e->setup_ack.subcmds, e->setup_ack.call);
05740                   pri->pvts[chanpos]->setup_ack = 1;
05741                   /* Send any queued digits */
05742                   for (x = 0;x < strlen(pri->pvts[chanpos]->dialdest); x++) {
05743                      ast_debug(1, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
05744                      pri_information(pri->pri, pri->pvts[chanpos]->call,
05745                         pri->pvts[chanpos]->dialdest[x]);
05746                   }
05747                   sig_pri_unlock_private(pri->pvts[chanpos]);
05748                } else
05749                   ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel);
05750             }
05751             break;
05752          case PRI_EVENT_NOTIFY:
05753             if (sig_pri_is_cis_call(e->notify.channel)) {
05754 #if defined(HAVE_PRI_CALL_HOLD)
05755                sig_pri_handle_cis_subcmds(pri, e->e, e->notify.subcmds,
05756                   e->notify.call);
05757 #else
05758                sig_pri_handle_cis_subcmds(pri, e->e, e->notify.subcmds, NULL);
05759 #endif   /* !defined(HAVE_PRI_CALL_HOLD) */
05760                break;
05761             }
05762 #if defined(HAVE_PRI_CALL_HOLD)
05763             chanpos = pri_find_principle(pri, e->notify.channel, e->notify.call);
05764 #else
05765             chanpos = pri_find_principle(pri, e->notify.channel, NULL);
05766 #endif   /* !defined(HAVE_PRI_CALL_HOLD) */
05767             if (chanpos < 0) {
05768                ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
05769                   PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
05770             } else {
05771                sig_pri_lock_private(pri->pvts[chanpos]);
05772 #if defined(HAVE_PRI_CALL_HOLD)
05773                sig_pri_handle_subcmds(pri, chanpos, e->e, e->notify.channel,
05774                   e->notify.subcmds, e->notify.call);
05775 #else
05776                sig_pri_handle_subcmds(pri, chanpos, e->e, e->notify.channel,
05777                   e->notify.subcmds, NULL);
05778 #endif   /* !defined(HAVE_PRI_CALL_HOLD) */
05779                switch (e->notify.info) {
05780                case PRI_NOTIFY_REMOTE_HOLD:
05781                   if (!pri->discardremoteholdretrieval) {
05782                      pri_queue_control(pri, chanpos, AST_CONTROL_HOLD);
05783                   }
05784                   break;
05785                case PRI_NOTIFY_REMOTE_RETRIEVAL:
05786                   if (!pri->discardremoteholdretrieval) {
05787                      pri_queue_control(pri, chanpos, AST_CONTROL_UNHOLD);
05788                   }
05789                   break;
05790                }
05791                sig_pri_unlock_private(pri->pvts[chanpos]);
05792             }
05793             break;
05794 #if defined(HAVE_PRI_CALL_HOLD)
05795          case PRI_EVENT_HOLD:
05796             /* We should not be getting any CIS calls with this message type. */
05797             if (sig_pri_handle_hold(pri, e)) {
05798                pri_hold_rej(pri->pri, e->hold.call,
05799                   PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED);
05800             } else {
05801                pri_hold_ack(pri->pri, e->hold.call);
05802             }
05803             break;
05804 #endif   /* defined(HAVE_PRI_CALL_HOLD) */
05805 #if defined(HAVE_PRI_CALL_HOLD)
05806          case PRI_EVENT_HOLD_ACK:
05807             ast_debug(1, "Event: HOLD_ACK\n");
05808             break;
05809 #endif   /* defined(HAVE_PRI_CALL_HOLD) */
05810 #if defined(HAVE_PRI_CALL_HOLD)
05811          case PRI_EVENT_HOLD_REJ:
05812             ast_debug(1, "Event: HOLD_REJ\n");
05813             break;
05814 #endif   /* defined(HAVE_PRI_CALL_HOLD) */
05815 #if defined(HAVE_PRI_CALL_HOLD)
05816          case PRI_EVENT_RETRIEVE:
05817             /* We should not be getting any CIS calls with this message type. */
05818             sig_pri_handle_retrieve(pri, e);
05819             break;
05820 #endif   /* defined(HAVE_PRI_CALL_HOLD) */
05821 #if defined(HAVE_PRI_CALL_HOLD)
05822          case PRI_EVENT_RETRIEVE_ACK:
05823             ast_debug(1, "Event: RETRIEVE_ACK\n");
05824             break;
05825 #endif   /* defined(HAVE_PRI_CALL_HOLD) */
05826 #if defined(HAVE_PRI_CALL_HOLD)
05827          case PRI_EVENT_RETRIEVE_REJ:
05828             ast_debug(1, "Event: RETRIEVE_REJ\n");
05829             break;
05830 #endif   /* defined(HAVE_PRI_CALL_HOLD) */
05831          default:
05832             ast_debug(1, "Event: %d\n", e->e);
05833             break;
05834          }
05835       }
05836       ast_mutex_unlock(&pri->lock);
05837    }
05838    /* Never reached */
05839    return NULL;
05840 }
05841 
05842 void sig_pri_init_pri(struct sig_pri_span *pri)
05843 {
05844    int i;
05845 
05846    memset(pri, 0, sizeof(*pri));
05847 
05848    ast_mutex_init(&pri->lock);
05849 
05850    pri->master = AST_PTHREADT_NULL;
05851    for (i = 0; i < SIG_PRI_NUM_DCHANS; i++)
05852       pri->fds[i] = -1;
05853 }
05854 
05855 int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast)
05856 {
05857    int res;
05858 #ifdef SUPPORT_USERUSER
05859    const char *useruser = pbx_builtin_getvar_helper(ast, "USERUSERINFO");
05860 #endif
05861 
05862    ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
05863    if (!ast->tech_pvt) {
05864       ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
05865       return 0;
05866    }
05867 
05868    p->owner = NULL;
05869    p->outgoing = 0;
05870    sig_pri_set_digital(p, 0); /* push up to parent for EC*/
05871 #if defined(HAVE_PRI_CALL_WAITING)
05872    if (p->is_call_waiting) {
05873       p->is_call_waiting = 0;
05874       ast_atomic_fetchadd_int(&p->pri->num_call_waiting_calls, -1);
05875    }
05876 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
05877    p->proceeding = 0;
05878    p->progress = 0;
05879    p->alerting = 0;
05880    p->setup_ack = 0;
05881    p->cid_num[0] = '\0';
05882    p->cid_subaddr[0] = '\0';
05883    p->cid_name[0] = '\0';
05884    p->user_tag[0] = '\0';
05885    p->exten[0] = '\0';
05886    sig_pri_set_dialing(p, 0);
05887 
05888    /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
05889    if (!pri_grab(p, p->pri)) {
05890       if (p->call) {
05891          if (p->alreadyhungup) {
05892             ast_log(LOG_DEBUG, "Already hungup...  Calling hangup once, and clearing call\n");
05893 
05894 #ifdef SUPPORT_USERUSER
05895             pri_call_set_useruser(p->call, useruser);
05896 #endif
05897 
05898 #if defined(HAVE_PRI_AOC_EVENTS)
05899             if (p->holding_aoce) {
05900                pri_aoc_e_send(p->pri->pri, p->call, &p->aoc_e);
05901             }
05902 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
05903             pri_hangup(p->pri->pri, p->call, -1);
05904             p->call = NULL;
05905          } else {
05906             const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
05907             int icause = ast->hangupcause ? ast->hangupcause : -1;
05908             ast_log(LOG_DEBUG, "Not yet hungup...  Calling hangup once with icause, and clearing call\n");
05909 
05910 #ifdef SUPPORT_USERUSER
05911             pri_call_set_useruser(p->call, useruser);
05912 #endif
05913 
05914             p->alreadyhungup = 1;
05915             if (cause) {
05916                if (atoi(cause))
05917                   icause = atoi(cause);
05918             }
05919 #if defined(HAVE_PRI_AOC_EVENTS)
05920             if (p->holding_aoce) {
05921                pri_aoc_e_send(p->pri->pri, p->call, &p->aoc_e);
05922             }
05923 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
05924             pri_hangup(p->pri->pri, p->call, icause);
05925          }
05926       }
05927       sig_pri_span_devstate_changed(p->pri);
05928       pri_rel(p->pri);
05929       res = 0;
05930    } else {
05931       ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
05932       res = -1;
05933    }
05934 
05935 #if defined(HAVE_PRI_AOC_EVENTS)
05936    p->aoc_s_request_invoke_id_valid = 0;
05937    p->holding_aoce = 0;
05938    p->waiting_for_aoce = 0;
05939 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
05940    ast->tech_pvt = NULL;
05941    return res;
05942 }
05943 
05944 /*!
05945  * \brief Extract the called number and subaddress from the dial string.
05946  * \since 1.8
05947  *
05948  * \param p sig_pri channel structure.
05949  * \param rdest Dial string buffer to extract called number and subaddress.
05950  * \param called Buffer to fill with extracted <number>[:<subaddress>]
05951  * \param called_buff_size Size of buffer to fill.
05952  *
05953  * \note Parsing must remain in sync with sig_pri_call().
05954  *
05955  * \return Nothing
05956  */
05957 void sig_pri_extract_called_num_subaddr(struct sig_pri_chan *p, const char *rdest, char *called, size_t called_buff_size)
05958 {
05959    char *dial;
05960    char *number;
05961    char *subaddr;
05962    AST_DECLARE_APP_ARGS(args,
05963       AST_APP_ARG(group);  /* channel/group token */
05964       AST_APP_ARG(ext); /* extension token */
05965       //AST_APP_ARG(opts); /* options token */
05966       AST_APP_ARG(other);  /* Any remining unused arguments */
05967    );
05968 
05969    /* Get private copy of dial string and break it up. */
05970    dial = ast_strdupa(rdest);
05971    AST_NONSTANDARD_APP_ARGS(args, dial, '/');
05972 
05973    number = args.ext;
05974    if (!number) {
05975       number = "";
05976    }
05977 
05978    /* Find and extract dialed_subaddress */
05979    subaddr = strchr(number, ':');
05980    if (subaddr) {
05981       *subaddr++ = '\0';
05982 
05983       /* Skip subaddress type prefix. */
05984       switch (*subaddr) {
05985       case 'U':
05986       case 'u':
05987       case 'N':
05988       case 'n':
05989          ++subaddr;
05990          break;
05991       default:
05992          break;
05993       }
05994    }
05995 
05996    /* Skip type-of-number/dial-plan prefix characters. */
05997    if (strlen(number) < p->stripmsd) {
05998       number = "";
05999    } else {
06000       number += p->stripmsd;
06001       while (isalpha(*number)) {
06002          ++number;
06003       }
06004    }
06005 
06006    /* Fill buffer with extracted number and subaddress. */
06007    if (ast_strlen_zero(subaddr)) {
06008       /* Put in called number only since there is no subaddress. */
06009       snprintf(called, called_buff_size, "%s", number);
06010    } else {
06011       /* Put in called number and subaddress. */
06012       snprintf(called, called_buff_size, "%s:%s", number, subaddr);
06013    }
06014 }
06015 
06016 enum SIG_PRI_CALL_OPT_FLAGS {
06017    OPT_KEYPAD =         (1 << 0),
06018    OPT_REVERSE_CHARGE = (1 << 1),   /* Collect call */
06019    OPT_AOC_REQUEST =    (1 << 2),   /* AOC Request */
06020 };
06021 enum SIG_PRI_CALL_OPT_ARGS {
06022    OPT_ARG_KEYPAD = 0,
06023    OPT_ARG_AOC_REQUEST,
06024 
06025    /* note: this entry _MUST_ be the last one in the enum */
06026    OPT_ARG_ARRAY_SIZE,
06027 };
06028 
06029 AST_APP_OPTIONS(sig_pri_call_opts, BEGIN_OPTIONS
06030    AST_APP_OPTION_ARG('K', OPT_KEYPAD, OPT_ARG_KEYPAD),
06031    AST_APP_OPTION('R', OPT_REVERSE_CHARGE),
06032    AST_APP_OPTION_ARG('A', OPT_AOC_REQUEST, OPT_ARG_AOC_REQUEST),
06033 END_OPTIONS);
06034 
06035 /*! \note Parsing must remain in sync with sig_pri_extract_called_num_subaddr(). */
06036 int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, int timeout, int layer1)
06037 {
06038    char dest[256]; /* must be same length as p->dialdest */
06039    struct ast_party_subaddress dialed_subaddress; /* Called subaddress */
06040    struct pri_sr *sr;
06041    char *c, *l, *n, *s;
06042 #ifdef SUPPORT_USERUSER
06043    const char *useruser;
06044 #endif
06045    int core_id;
06046    int pridialplan;
06047    int dp_strip;
06048    int prilocaldialplan;
06049    int ldp_strip;
06050    int exclusive;
06051 #if defined(HAVE_PRI_SETUP_KEYPAD)
06052    const char *keypad;
06053 #endif   /* defined(HAVE_PRI_SETUP_KEYPAD) */
06054    AST_DECLARE_APP_ARGS(args,
06055       AST_APP_ARG(group);  /* channel/group token */
06056       AST_APP_ARG(ext); /* extension token */
06057       AST_APP_ARG(opts);   /* options token */
06058       AST_APP_ARG(other);  /* Any remining unused arguments */
06059    );
06060    struct ast_flags opts;
06061    char *opt_args[OPT_ARG_ARRAY_SIZE];
06062 
06063    ast_log(LOG_DEBUG, "CALLER NAME: %s NUM: %s\n",
06064       S_COR(ast->connected.id.name.valid, ast->connected.id.name.str, ""),
06065       S_COR(ast->connected.id.number.valid, ast->connected.id.number.str, ""));
06066 
06067    if (!p->pri) {
06068       ast_log(LOG_ERROR, "Could not find pri on channel %d\n", p->channel);
06069       return -1;
06070    }
06071 
06072    if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
06073       ast_log(LOG_WARNING, "sig_pri_call called on %s, neither down nor reserved\n", ast->name);
06074       return -1;
06075    }
06076 
06077    p->dialdest[0] = '\0';
06078    p->outgoing = 1;
06079 
06080    ast_copy_string(dest, rdest, sizeof(dest));
06081    AST_NONSTANDARD_APP_ARGS(args, dest, '/');
06082    if (ast_app_parse_options(sig_pri_call_opts, &opts, opt_args, args.opts)) {
06083       /* General invalid option syntax. */
06084       return -1;
06085    }
06086 
06087    c = args.ext;
06088    if (!c) {
06089       c = "";
06090    }
06091 
06092    /* setup dialed_subaddress if found */
06093    ast_party_subaddress_init(&dialed_subaddress);
06094    s = strchr(c, ':');
06095    if (s) {
06096       *s = '\0';
06097       s++;
06098       /* prefix */
06099       /* 'n' = NSAP */
06100       /* 'U' = odd, 'u'= even */
06101       /* Default = NSAP */
06102       switch (*s) {
06103       case 'U':
06104          dialed_subaddress.odd_even_indicator = 1;
06105          /* fall through */
06106       case 'u':
06107          s++;
06108          dialed_subaddress.type = 2;
06109          break;
06110       case 'N':
06111       case 'n':
06112          s++;
06113          /* default already covered with ast_party_subaddress_init */
06114          break;
06115       }
06116       dialed_subaddress.str = s;
06117       dialed_subaddress.valid = 1;
06118       s = NULL;
06119    }
06120 
06121    l = NULL;
06122    n = NULL;
06123    if (!p->hidecallerid) {
06124       if (ast->connected.id.number.valid) {
06125          /* If we get to the end of this loop without breaking, there's no
06126           * calleridnum.  This is done instead of testing for "unknown" or
06127           * the thousands of other ways that the calleridnum could be
06128           * invalid. */
06129          for (l = ast->connected.id.number.str; l && *l; l++) {
06130             if (strchr("0123456789", *l)) {
06131                l = ast->connected.id.number.str;
06132                break;
06133             }
06134          }
06135       } else {
06136          l = NULL;
06137       }
06138       if (!p->hidecalleridname) {
06139          n = ast->connected.id.name.valid ? ast->connected.id.name.str : NULL;
06140       }
06141    }
06142 
06143    if (strlen(c) < p->stripmsd) {
06144       ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
06145       return -1;
06146    }
06147    if (pri_grab(p, p->pri)) {
06148       ast_log(LOG_WARNING, "Failed to grab PRI!\n");
06149       return -1;
06150    }
06151    if (!(p->call = pri_new_call(p->pri->pri))) {
06152       ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
06153       pri_rel(p->pri);
06154       return -1;
06155    }
06156    if (!(sr = pri_sr_new())) {
06157       ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
06158       pri_destroycall(p->pri->pri, p->call);
06159       p->call = NULL;
06160       pri_rel(p->pri);
06161       return -1;
06162    }
06163 
06164    sig_pri_set_digital(p, IS_DIGITAL(ast->transfercapability));   /* push up to parent for EC */
06165 
06166 #if defined(HAVE_PRI_CALL_WAITING)
06167    if (p->is_call_waiting) {
06168       /*
06169        * Indicate that this is a call waiting call.
06170        * i.e., Normal call but with no B channel.
06171        */
06172       pri_sr_set_channel(sr, 0, 0, 1);
06173    } else
06174 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
06175    {
06176       /* Should the picked channel be used exclusively? */
06177       if (p->priexclusive || p->pri->nodetype == PRI_NETWORK) {
06178          exclusive = 1;
06179       } else {
06180          exclusive = 0;
06181       }
06182       pri_sr_set_channel(sr, PVT_TO_CHANNEL(p), exclusive, 1);
06183    }
06184 
06185    pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
06186       (p->digital ? -1 : layer1));
06187 
06188    if (p->pri->facilityenable)
06189       pri_facility_enable(p->pri->pri);
06190 
06191    ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
06192    dp_strip = 0;
06193    pridialplan = p->pri->dialplan - 1;
06194    if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
06195       if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
06196          if (pridialplan == -2) {
06197             dp_strip = strlen(p->pri->internationalprefix);
06198          }
06199          pridialplan = PRI_INTERNATIONAL_ISDN;
06200       } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
06201          if (pridialplan == -2) {
06202             dp_strip = strlen(p->pri->nationalprefix);
06203          }
06204          pridialplan = PRI_NATIONAL_ISDN;
06205       } else {
06206          pridialplan = PRI_LOCAL_ISDN;
06207       }
06208    }
06209    while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
06210       switch (c[p->stripmsd]) {
06211       case 'U':
06212          pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
06213          break;
06214       case 'I':
06215          pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
06216          break;
06217       case 'N':
06218          pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
06219          break;
06220       case 'L':
06221          pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
06222          break;
06223       case 'S':
06224          pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
06225          break;
06226       case 'V':
06227          pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
06228          break;
06229       case 'R':
06230          pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
06231          break;
06232       case 'u':
06233          pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
06234          break;
06235       case 'e':
06236          pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
06237          break;
06238       case 'x':
06239          pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
06240          break;
06241       case 'f':
06242          pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
06243          break;
06244       case 'n':
06245          pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
06246          break;
06247       case 'p':
06248          pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
06249          break;
06250       case 'r':
06251          pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
06252          break;
06253       default:
06254          if (isalpha(c[p->stripmsd])) {
06255             ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n",
06256                c[p->stripmsd] > 'Z' ? "NPI" : "TON", c[p->stripmsd]);
06257          }
06258          break;
06259       }
06260       c++;
06261    }
06262 #if defined(HAVE_PRI_SETUP_KEYPAD)
06263    if (ast_test_flag(&opts, OPT_KEYPAD)
06264       && !ast_strlen_zero(opt_args[OPT_ARG_KEYPAD])) {
06265       /* We have a keypad facility digits option with digits. */
06266       keypad = opt_args[OPT_ARG_KEYPAD];
06267       pri_sr_set_keypad_digits(sr, keypad);
06268    } else {
06269       keypad = NULL;
06270    }
06271    if (!keypad || !ast_strlen_zero(c + p->stripmsd + dp_strip))
06272 #endif   /* defined(HAVE_PRI_SETUP_KEYPAD) */
06273    {
06274       pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
06275    }
06276 
06277 #if defined(HAVE_PRI_SUBADDR)
06278    if (dialed_subaddress.valid) {
06279       struct pri_party_subaddress subaddress;
06280 
06281       memset(&subaddress, 0, sizeof(subaddress));
06282       sig_pri_party_subaddress_from_ast(&subaddress, &dialed_subaddress);
06283       pri_sr_set_called_subaddress(sr, &subaddress);
06284    }
06285 #endif   /* defined(HAVE_PRI_SUBADDR) */
06286 #if defined(HAVE_PRI_REVERSE_CHARGE)
06287    if (ast_test_flag(&opts, OPT_REVERSE_CHARGE)) {
06288       pri_sr_set_reversecharge(sr, PRI_REVERSECHARGE_REQUESTED);
06289    }
06290 #endif   /* defined(HAVE_PRI_REVERSE_CHARGE) */
06291 #if defined(HAVE_PRI_AOC_EVENTS)
06292    if (ast_test_flag(&opts, OPT_AOC_REQUEST)
06293       && !ast_strlen_zero(opt_args[OPT_ARG_AOC_REQUEST])) {
06294       if (strchr(opt_args[OPT_ARG_AOC_REQUEST], 's')) {
06295          pri_sr_set_aoc_charging_request(sr, PRI_AOC_REQUEST_S);
06296       }
06297       if (strchr(opt_args[OPT_ARG_AOC_REQUEST], 'd')) {
06298          pri_sr_set_aoc_charging_request(sr, PRI_AOC_REQUEST_D);
06299       }
06300       if (strchr(opt_args[OPT_ARG_AOC_REQUEST], 'e')) {
06301          pri_sr_set_aoc_charging_request(sr, PRI_AOC_REQUEST_E);
06302       }
06303    }
06304 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
06305 
06306    /* Setup the user tag for party id's from this device for this call. */
06307    if (p->pri->append_msn_to_user_tag) {
06308       snprintf(p->user_tag, sizeof(p->user_tag), "%s_%s", p->pri->initial_user_tag,
06309          p->pri->nodetype == PRI_NETWORK
06310             ? c + p->stripmsd + dp_strip
06311             : S_COR(ast->connected.id.number.valid,
06312                ast->connected.id.number.str, ""));
06313    } else {
06314       ast_copy_string(p->user_tag, p->pri->initial_user_tag, sizeof(p->user_tag));
06315    }
06316 
06317    /*
06318     * Replace the caller id tag from the channel creation
06319     * with the actual tag value.
06320     */
06321    ast_free(ast->caller.id.tag);
06322    ast->caller.id.tag = ast_strdup(p->user_tag);
06323 
06324    ldp_strip = 0;
06325    prilocaldialplan = p->pri->localdialplan - 1;
06326    if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
06327       if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
06328          if (prilocaldialplan == -2) {
06329             ldp_strip = strlen(p->pri->internationalprefix);
06330          }
06331          prilocaldialplan = PRI_INTERNATIONAL_ISDN;
06332       } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
06333          if (prilocaldialplan == -2) {
06334             ldp_strip = strlen(p->pri->nationalprefix);
06335          }
06336          prilocaldialplan = PRI_NATIONAL_ISDN;
06337       } else {
06338          prilocaldialplan = PRI_LOCAL_ISDN;
06339       }
06340    }
06341    if (l != NULL) {
06342       while (*l > '9' && *l != '*' && *l != '#') {
06343          switch (*l) {
06344          case 'U':
06345             prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
06346             break;
06347          case 'I':
06348             prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
06349             break;
06350          case 'N':
06351             prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
06352             break;
06353          case 'L':
06354             prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
06355             break;
06356          case 'S':
06357             prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
06358             break;
06359          case 'V':
06360             prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
06361             break;
06362          case 'R':
06363             prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
06364             break;
06365          case 'u':
06366             prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
06367             break;
06368          case 'e':
06369             prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
06370             break;
06371          case 'x':
06372             prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
06373             break;
06374          case 'f':
06375             prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
06376             break;
06377          case 'n':
06378             prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
06379             break;
06380          case 'p':
06381             prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
06382             break;
06383          case 'r':
06384             prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
06385             break;
06386          default:
06387             if (isalpha(*l)) {
06388                ast_log(LOG_WARNING,
06389                   "Unrecognized prilocaldialplan %s modifier: %c\n",
06390                   *l > 'Z' ? "NPI" : "TON", *l);
06391             }
06392             break;
06393          }
06394          l++;
06395       }
06396    }
06397    pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
06398       p->use_callingpres ? ast->connected.id.number.presentation : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
06399 
06400 #if defined(HAVE_PRI_SUBADDR)
06401    if (ast->connected.id.subaddress.valid) {
06402       struct pri_party_subaddress subaddress;
06403 
06404       memset(&subaddress, 0, sizeof(subaddress));
06405       sig_pri_party_subaddress_from_ast(&subaddress, &ast->connected.id.subaddress);
06406       pri_sr_set_caller_subaddress(sr, &subaddress);
06407    }
06408 #endif   /* defined(HAVE_PRI_SUBADDR) */
06409 
06410    sig_pri_redirecting_update(p, ast);
06411 
06412 #ifdef SUPPORT_USERUSER
06413    /* User-user info */
06414    useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
06415    if (useruser)
06416       pri_sr_set_useruser(sr, useruser);
06417 #endif
06418 
06419 #if defined(HAVE_PRI_CCSS)
06420    if (ast_cc_is_recall(ast, &core_id, sig_pri_cc_type_name)) {
06421       struct ast_cc_monitor *monitor;
06422       char device_name[AST_CHANNEL_NAME];
06423 
06424       /* This is a CC recall call. */
06425       ast_channel_get_device_name(ast, device_name, sizeof(device_name));
06426       monitor = ast_cc_get_monitor_by_recall_core_id(core_id, device_name);
06427       if (monitor) {
06428          struct sig_pri_cc_monitor_instance *instance;
06429 
06430          instance = monitor->private_data;
06431 
06432          /* If this fails then we have monitor instance ambiguity. */
06433          ast_assert(p->pri == instance->pri);
06434 
06435          if (pri_cc_call(p->pri->pri, instance->cc_id, p->call, sr)) {
06436             /* The CC recall call failed for some reason. */
06437             ast_log(LOG_WARNING, "Unable to setup CC recall call to device %s\n",
06438                device_name);
06439             ao2_ref(monitor, -1);
06440             pri_rel(p->pri);
06441             pri_sr_free(sr);
06442             return -1;
06443          }
06444          ao2_ref(monitor, -1);
06445       } else {
06446          core_id = -1;
06447       }
06448    } else
06449 #endif   /* defined(HAVE_PRI_CCSS) */
06450    {
06451       core_id = -1;
06452    }
06453    if (core_id == -1 && pri_setup(p->pri->pri, p->call, sr)) {
06454       ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
06455          c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
06456       pri_rel(p->pri);
06457       pri_sr_free(sr);
06458       return -1;
06459    }
06460    pri_sr_free(sr);
06461    ast_setstate(ast, AST_STATE_DIALING);
06462    sig_pri_set_dialing(p, 1);
06463    pri_rel(p->pri);
06464    return 0;
06465 }
06466 
06467 int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
06468 {
06469    int res = -1;
06470 
06471    switch (condition) {
06472    case AST_CONTROL_BUSY:
06473       if (p->priindication_oob || p->no_b_channel) {
06474          chan->hangupcause = AST_CAUSE_USER_BUSY;
06475          chan->_softhangup |= AST_SOFTHANGUP_DEV;
06476          res = 0;
06477       } else if (!p->progress && p->pri && !p->outgoing) {
06478          if (p->pri->pri) {
06479             if (!pri_grab(p, p->pri)) {
06480 #ifdef HAVE_PRI_PROG_W_CAUSE
06481                pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_USER_BUSY); /* cause = 17 */
06482 #else
06483                pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
06484 #endif
06485                pri_rel(p->pri);
06486             } else {
06487                ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
06488             }
06489          }
06490          p->progress = 1;
06491          res = sig_pri_play_tone(p, SIG_PRI_TONE_BUSY);
06492       }
06493       break;
06494    case AST_CONTROL_RINGING:
06495       if ((!p->alerting) && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
06496          if (p->pri->pri) {
06497             if (!pri_grab(p, p->pri)) {
06498                pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p),
06499                   p->no_b_channel || p->digital ? 0 : 1);
06500                pri_rel(p->pri);
06501             } else {
06502                ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
06503             }
06504          }
06505          p->alerting = 1;
06506       }
06507       res = sig_pri_play_tone(p, SIG_PRI_TONE_RINGTONE);
06508       if (chan->_state != AST_STATE_UP) {
06509          if (chan->_state != AST_STATE_RING)
06510             ast_setstate(chan, AST_STATE_RINGING);
06511       }
06512       break;
06513    case AST_CONTROL_PROCEEDING:
06514       ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
06515       if (!p->proceeding && p->pri && !p->outgoing) {
06516          if (p->pri->pri) {
06517             if (!pri_grab(p, p->pri)) {
06518                pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p),
06519                   p->no_b_channel || p->digital ? 0 : 1);
06520                p->proceeding = 1;
06521                if (!p->no_b_channel && !p->digital) {
06522                   sig_pri_set_dialing(p, 0);
06523                }
06524                pri_rel(p->pri);
06525             } else {
06526                ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
06527             }
06528          }
06529       }
06530       /* don't continue in ast_indicate */
06531       res = 0;
06532       break;
06533    case AST_CONTROL_PROGRESS:
06534       ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
06535       sig_pri_set_digital(p, 0); /* Digital-only calls isn't allowing any inband progress messages */
06536       if (!p->progress && !p->alerting && p->pri && !p->outgoing && !p->no_b_channel) {
06537          if (p->pri->pri) {
06538             if (!pri_grab(p, p->pri)) {
06539 #ifdef HAVE_PRI_PROG_W_CAUSE
06540                pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, -1);  /* no cause at all */
06541 #else
06542                pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
06543 #endif
06544                pri_rel(p->pri);
06545             } else {
06546                ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
06547             }
06548          }
06549          p->progress = 1;
06550       }
06551       /* don't continue in ast_indicate */
06552       res = 0;
06553       break;
06554    case AST_CONTROL_CONGESTION:
06555       chan->hangupcause = AST_CAUSE_CONGESTION;
06556       if (p->priindication_oob || p->no_b_channel) {
06557          chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
06558          chan->_softhangup |= AST_SOFTHANGUP_DEV;
06559          res = 0;
06560       } else if (!p->progress && p->pri && !p->outgoing) {
06561          if (p->pri->pri) {
06562             if (!pri_grab(p, p->pri)) {
06563 #ifdef HAVE_PRI_PROG_W_CAUSE
06564                pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_SWITCH_CONGESTION); /* cause = 42 */
06565 #else
06566                pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
06567 #endif
06568                pri_rel(p->pri);
06569             } else {
06570                ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
06571             }
06572          }
06573          p->progress = 1;
06574          res = sig_pri_play_tone(p, SIG_PRI_TONE_CONGESTION);
06575       }
06576       break;
06577    case AST_CONTROL_HOLD:
06578       if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
06579          if (!pri_grab(p, p->pri)) {
06580             res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_HOLD);
06581             pri_rel(p->pri);
06582          } else {
06583             ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
06584          }
06585       } else
06586          ast_moh_start(chan, data, p->mohinterpret);
06587       break;
06588    case AST_CONTROL_UNHOLD:
06589       if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
06590          if (!pri_grab(p, p->pri)) {
06591             res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
06592             pri_rel(p->pri);
06593          }
06594       } else
06595          ast_moh_stop(chan);
06596       break;
06597    case AST_CONTROL_SRCUPDATE:
06598       res = 0;
06599       break;
06600    case -1:
06601       res = sig_pri_play_tone(p, -1);
06602       break;
06603    case AST_CONTROL_CONNECTED_LINE:
06604       ast_debug(1, "Received AST_CONTROL_CONNECTED_LINE on %s\n", chan->name);
06605       if (p->pri && !pri_grab(p, p->pri)) {
06606          struct pri_party_connected_line connected;
06607 
06608          memset(&connected, 0, sizeof(connected));
06609          sig_pri_party_id_from_ast(&connected.id, &chan->connected.id);
06610 
06611          pri_connected_line_update(p->pri->pri, p->call, &connected);
06612          pri_rel(p->pri);
06613       }
06614       break;
06615    case AST_CONTROL_REDIRECTING:
06616       ast_debug(1, "Received AST_CONTROL_REDIRECTING on %s\n", chan->name);
06617       if (p->pri && !pri_grab(p, p->pri)) {
06618          sig_pri_redirecting_update(p, chan);
06619          pri_rel(p->pri);
06620       }
06621       break;
06622    case AST_CONTROL_AOC:
06623 #if defined(HAVE_PRI_AOC_EVENTS)
06624       {
06625          struct ast_aoc_decoded *decoded
06626             = ast_aoc_decode((struct ast_aoc_encoded *) data, datalen, chan);
06627          ast_debug(1, "Received AST_CONTROL_AOC on %s\n", chan->name);
06628          if (decoded && p->pri && !pri_grab(p, p->pri)) {
06629             switch (ast_aoc_get_msg_type(decoded)) {
06630             case AST_AOC_S:
06631                if (p->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S) {
06632                   sig_pri_aoc_s_from_ast(p, decoded);
06633                }
06634                break;
06635             case AST_AOC_D:
06636                if (p->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_D) {
06637                   sig_pri_aoc_d_from_ast(p, decoded);
06638                }
06639                break;
06640             case AST_AOC_E:
06641                if (p->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_E) {
06642                   sig_pri_aoc_e_from_ast(p, decoded);
06643                }
06644                /* if hangup was delayed for this AOC-E msg, waiting_for_aoc
06645                 * will be set.  A hangup is already occuring via a timeout during
06646                 * this delay.  Instead of waiting for that timeout to occur, go ahead
06647                 * and initiate the softhangup since the delay is no longer necessary */
06648                if (p->waiting_for_aoce) {
06649                   p->waiting_for_aoce = 0;
06650                   ast_log(LOG_DEBUG,
06651                      "Received final AOC-E msg, continue with hangup on %s\n",
06652                      chan->name);
06653                   ast_softhangup_nolock(chan, AST_SOFTHANGUP_DEV);
06654                }
06655                break;
06656             case AST_AOC_REQUEST:
06657                /* We do not pass through AOC requests, So unless this
06658                 * is an AOC termination request it will be ignored */
06659                if (ast_aoc_get_termination_request(decoded)) {
06660                   pri_hangup(p->pri->pri, p->call, -1);
06661                }
06662                break;
06663             default:
06664                break;
06665             }
06666             pri_rel(p->pri);
06667          }
06668          ast_aoc_destroy_decoded(decoded);
06669       }
06670 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
06671       break;
06672    }
06673 
06674    return res;
06675 }
06676 
06677 int sig_pri_answer(struct sig_pri_chan *p, struct ast_channel *ast)
06678 {
06679    int res = 0;
06680    /* Send a pri acknowledge */
06681    if (!pri_grab(p, p->pri)) {
06682 #if defined(HAVE_PRI_AOC_EVENTS)
06683       if (p->aoc_s_request_invoke_id_valid) {
06684          /* if AOC-S was requested and the invoke id is still present on answer.  That means
06685           * no AOC-S rate list was provided, so send a NULL response which will indicate that
06686           * AOC-S is not available */
06687          pri_aoc_s_request_response_send(p->pri->pri, p->call,
06688             p->aoc_s_request_invoke_id, NULL);
06689          p->aoc_s_request_invoke_id_valid = 0;
06690       }
06691 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
06692       p->proceeding = 1;
06693       sig_pri_set_dialing(p, 0);
06694       sig_pri_open_media(p);
06695       res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
06696       pri_rel(p->pri);
06697    } else {
06698       res = -1;
06699    }
06700    ast_setstate(ast, AST_STATE_UP);
06701    return res;
06702 }
06703 
06704 /*!
06705  * \internal
06706  * \brief Simple check if the channel is available to use.
06707  * \since 1.8
06708  *
06709  * \param pvt Private channel control structure.
06710  *
06711  * \retval 0 Interface not available.
06712  * \retval 1 Interface is available.
06713  */
06714 static int sig_pri_available_check(struct sig_pri_chan *pvt)
06715 {
06716    /*
06717     * If no owner, interface has a B channel, not resetting, not already with call,
06718     * not in alarm, and in-service then available.
06719     */
06720    if (!pvt->owner && !pvt->no_b_channel && !pvt->resetting && !pvt->call
06721       && !pvt->inalarm) {
06722 #if defined(HAVE_PRI_SERVICE_MESSAGES)
06723       if (pvt->service_status) {
06724          return 0;
06725       }
06726 #endif   /* defined(HAVE_PRI_SERVICE_MESSAGES) */
06727       return 1;
06728    }
06729    return 0;
06730 }
06731 
06732 #if defined(HAVE_PRI_CALL_WAITING)
06733 /*!
06734  * \internal
06735  * \brief Get an available call waiting interface.
06736  * \since 1.8
06737  *
06738  * \param pri sig_pri PRI control structure.
06739  *
06740  * \retval cw Call waiting interface to use.
06741  * \retval NULL if no call waiting interface available.
06742  */
06743 static struct sig_pri_chan *sig_pri_cw_available(struct sig_pri_span *pri)
06744 {
06745    struct sig_pri_chan *cw;
06746    int idx;
06747 
06748    cw = NULL;
06749    ast_mutex_lock(&pri->lock);
06750    if (pri->num_call_waiting_calls < pri->max_call_waiting_calls) {
06751       if (!pri->num_call_waiting_calls) {
06752          /*
06753           * There are no outstanding call waiting calls.  Check to see
06754           * if the span is in a congested state for the first call
06755           * waiting call.
06756           */
06757          for (idx = 0; idx < pri->numchans; ++idx) {
06758             if (pri->pvts[idx] && sig_pri_available_check(pri->pvts[idx])) {
06759                /* There is another channel that is available on this span. */
06760                ast_mutex_unlock(&pri->lock);
06761                return cw;
06762             }
06763          }
06764       }
06765       idx = pri_find_empty_nobch(pri);
06766       if (0 <= idx) {
06767          /* Setup the call waiting interface to use. */
06768          cw = pri->pvts[idx];
06769          cw->is_call_waiting = 1;
06770          sig_pri_init_config(cw, pri);
06771          ast_atomic_fetchadd_int(&pri->num_call_waiting_calls, 1);
06772       }
06773    }
06774    ast_mutex_unlock(&pri->lock);
06775    return cw;
06776 }
06777 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
06778 
06779 int sig_pri_available(struct sig_pri_chan **pvt, int is_specific_channel)
06780 {
06781    struct sig_pri_chan *p = *pvt;
06782 
06783    if (!p->pri) {
06784       /* Something is wrong here.  A PRI channel without the pri pointer? */
06785       return 0;
06786    }
06787 
06788    if (
06789 #if defined(HAVE_PRI_CALL_WAITING)
06790       /*
06791        * Only do call waiting calls if we have any
06792        * call waiting call outstanding.  We do not
06793        * want new calls to steal a B channel
06794        * freed for an earlier call waiting call.
06795        */
06796       !p->pri->num_call_waiting_calls &&
06797 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
06798       sig_pri_available_check(p)) {
06799       return 1;
06800    }
06801 
06802 #if defined(HAVE_PRI_CALL_WAITING)
06803    if (!is_specific_channel) {
06804       struct sig_pri_chan *cw;
06805 
06806       cw = sig_pri_cw_available(p->pri);
06807       if (cw) {
06808          /* We have a call waiting interface to use instead. */
06809          *pvt = cw;
06810          return 1;
06811       }
06812    }
06813 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
06814    return 0;
06815 }
06816 
06817 /* If return 0, it means this function was able to handle it (pre setup digits).  If non zero, the user of this
06818  * functions should handle it normally (generate inband DTMF) */
06819 int sig_pri_digit_begin(struct sig_pri_chan *pvt, struct ast_channel *ast, char digit)
06820 {
06821    if ((ast->_state == AST_STATE_DIALING) && !pvt->proceeding) {
06822       if (pvt->setup_ack) {
06823          if (!pri_grab(pvt, pvt->pri)) {
06824             pri_information(pvt->pri->pri, pvt->call, digit);
06825             pri_rel(pvt->pri);
06826          } else {
06827             ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->pri->span);
06828          }
06829       } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
06830          int res;
06831          ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
06832          res = strlen(pvt->dialdest);
06833          pvt->dialdest[res++] = digit;
06834          pvt->dialdest[res] = '\0';
06835       }
06836       return 0;
06837    }
06838    return 1;
06839 }
06840 
06841 #if defined(HAVE_PRI_MWI)
06842 /*!
06843  * \internal
06844  * \brief Send a MWI indication to the given span.
06845  * \since 1.8
06846  *
06847  * \param pri Asterisk D channel control structure.
06848  * \param mbox_number Mailbox number
06849  * \param mbox_context Mailbox context
06850  * \param num_messages Number of messages waiting.
06851  *
06852  * \return Nothing
06853  */
06854 static void sig_pri_send_mwi_indication(struct sig_pri_span *pri, const char *mbox_number, const char *mbox_context, int num_messages)
06855 {
06856    struct pri_party_id mailbox;
06857 
06858    ast_debug(1, "Send MWI indication for %s@%s num_messages:%d\n", mbox_number,
06859       mbox_context, num_messages);
06860 
06861    memset(&mailbox, 0, sizeof(mailbox));
06862    mailbox.number.valid = 1;
06863    mailbox.number.presentation = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
06864    mailbox.number.plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_UNKNOWN;
06865    ast_copy_string(mailbox.number.str, mbox_number, sizeof(mailbox.number.str));
06866 
06867    ast_mutex_lock(&pri->lock);
06868    pri_mwi_indicate(pri->pri, &mailbox, 1 /* speech */, num_messages, NULL, NULL, -1, 0);
06869    ast_mutex_unlock(&pri->lock);
06870 }
06871 #endif   /* defined(HAVE_PRI_MWI) */
06872 
06873 #if defined(HAVE_PRI_MWI)
06874 /*!
06875  * \internal
06876  * \brief MWI subscription event callback.
06877  * \since 1.8
06878  *
06879  * \param event the event being passed to the subscriber
06880  * \param userdata the data provider in the call to ast_event_subscribe()
06881  *
06882  * \return Nothing
06883  */
06884 static void sig_pri_mwi_event_cb(const struct ast_event *event, void *userdata)
06885 {
06886    struct sig_pri_span *pri = userdata;
06887    const char *mbox_context;
06888    const char *mbox_number;
06889    int num_messages;
06890 
06891    mbox_number = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
06892    if (ast_strlen_zero(mbox_number)) {
06893       return;
06894    }
06895    mbox_context = ast_event_get_ie_str(event, AST_EVENT_IE_CONTEXT);
06896    if (ast_strlen_zero(mbox_context)) {
06897       return;
06898    }
06899    num_messages = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
06900    sig_pri_send_mwi_indication(pri, mbox_number, mbox_context, num_messages);
06901 }
06902 #endif   /* defined(HAVE_PRI_MWI) */
06903 
06904 #if defined(HAVE_PRI_MWI)
06905 /*!
06906  * \internal
06907  * \brief Send update MWI indications from the event cache.
06908  * \since 1.8
06909  *
06910  * \param pri Asterisk D channel control structure.
06911  *
06912  * \return Nothing
06913  */
06914 static void sig_pri_mwi_cache_update(struct sig_pri_span *pri)
06915 {
06916    int idx;
06917    int num_messages;
06918    struct ast_event *event;
06919 
06920    for (idx = 0; idx < ARRAY_LEN(pri->mbox); ++idx) {
06921       if (!pri->mbox[idx].sub) {
06922          /* There are no more mailboxes on this span. */
06923          break;
06924       }
06925 
06926       event = ast_event_get_cached(AST_EVENT_MWI,
06927          AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, pri->mbox[idx].number,
06928          AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, pri->mbox[idx].context,
06929          AST_EVENT_IE_END);
06930       if (!event) {
06931          /* No cached event for this mailbox. */
06932          continue;
06933       }
06934       num_messages = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
06935       sig_pri_send_mwi_indication(pri, pri->mbox[idx].number, pri->mbox[idx].context,
06936          num_messages);
06937       ast_event_destroy(event);
06938    }
06939 }
06940 #endif   /* defined(HAVE_PRI_MWI) */
06941 
06942 /*!
06943  * \brief Stop PRI span.
06944  * \since 1.8
06945  *
06946  * \param pri Asterisk D channel control structure.
06947  *
06948  * \return Nothing
06949  */
06950 void sig_pri_stop_pri(struct sig_pri_span *pri)
06951 {
06952 #if defined(HAVE_PRI_MWI)
06953    int idx;
06954 #endif   /* defined(HAVE_PRI_MWI) */
06955 
06956 #if defined(HAVE_PRI_MWI)
06957    for (idx = 0; idx < ARRAY_LEN(pri->mbox); ++idx) {
06958       if (pri->mbox[idx].sub) {
06959          pri->mbox[idx].sub = ast_event_unsubscribe(pri->mbox[idx].sub);
06960       }
06961    }
06962 #endif   /* defined(HAVE_PRI_MWI) */
06963 }
06964 
06965 /*!
06966  * \internal
06967  * \brief qsort comparison function.
06968  * \since 1.8
06969  *
06970  * \param left Ptr to sig_pri_chan ptr to compare.
06971  * \param right Ptr to sig_pri_chan ptr to compare.
06972  *
06973  * \retval <0 if left < right.
06974  * \retval =0 if left == right.
06975  * \retval >0 if left > right.
06976  */
06977 static int sig_pri_cmp_pri_chans(const void *left, const void *right)
06978 {
06979    const struct sig_pri_chan *pvt_left;
06980    const struct sig_pri_chan *pvt_right;
06981 
06982    pvt_left = *(struct sig_pri_chan **) left;
06983    pvt_right = *(struct sig_pri_chan **) right;
06984    if (!pvt_left) {
06985       if (!pvt_right) {
06986          return 0;
06987       }
06988       return 1;
06989    }
06990    if (!pvt_right) {
06991       return -1;
06992    }
06993 
06994    return pvt_left->channel - pvt_right->channel;
06995 }
06996 
06997 /*!
06998  * \internal
06999  * \brief Sort the PRI B channel private pointer array.
07000  * \since 1.8
07001  *
07002  * \param pri PRI Span controlling structure.
07003  *
07004  * \details
07005  * Since the chan_dahdi.conf file can declare channels in any order, we need to sort
07006  * the private channel pointer array.
07007  *
07008  * \return Nothing
07009  */
07010 static void sig_pri_sort_pri_chans(struct sig_pri_span *pri)
07011 {
07012    qsort(&pri->pvts, pri->numchans, sizeof(pri->pvts[0]), sig_pri_cmp_pri_chans);
07013 }
07014 
07015 int sig_pri_start_pri(struct sig_pri_span *pri)
07016 {
07017    int x;
07018    int i;
07019 #if defined(HAVE_PRI_MWI)
07020    char *saveptr;
07021    char *mbox_number;
07022    char *mbox_context;
07023    struct ast_str *mwi_description = ast_str_alloca(64);
07024 #endif   /* defined(HAVE_PRI_MWI) */
07025 
07026 #if defined(HAVE_PRI_MWI)
07027    /* Prepare the mbox[] for use. */
07028    for (i = 0; i < ARRAY_LEN(pri->mbox); ++i) {
07029       if (pri->mbox[i].sub) {
07030          pri->mbox[i].sub = ast_event_unsubscribe(pri->mbox[i].sub);
07031       }
07032    }
07033 #endif   /* defined(HAVE_PRI_MWI) */
07034 
07035    ast_mutex_init(&pri->lock);
07036    sig_pri_sort_pri_chans(pri);
07037 
07038 #if defined(HAVE_PRI_MWI)
07039    /*
07040     * Split the mwi_mailboxes configuration string into the mbox[]:
07041     * mailbox_number[@context]{,mailbox_number[@context]}
07042     */
07043    i = 0;
07044    saveptr = pri->mwi_mailboxes;
07045    while (i < ARRAY_LEN(pri->mbox)) {
07046       mbox_number = strsep(&saveptr, ",");
07047       if (!mbox_number) {
07048          break;
07049       }
07050       /* Split the mailbox_number and context */
07051       mbox_context = strchr(mbox_number, '@');
07052       if (mbox_context) {
07053          *mbox_context++ = '\0';
07054          mbox_context = ast_strip(mbox_context);
07055       }
07056       mbox_number = ast_strip(mbox_number);
07057       if (ast_strlen_zero(mbox_number)) {
07058          /* There is no mailbox number.  Skip it. */
07059          continue;
07060       }
07061       if (ast_strlen_zero(mbox_context)) {
07062          /* There was no context so use the default. */
07063          mbox_context = "default";
07064       }
07065 
07066       /* Fill the mbox[] element. */
07067       ast_str_set(&mwi_description, -1, "%s span %d[%d] MWI mailbox %s@%s",
07068          sig_pri_cc_type_name, pri->span, i, mbox_number, mbox_context);
07069       pri->mbox[i].sub = ast_event_subscribe(AST_EVENT_MWI, sig_pri_mwi_event_cb,
07070          ast_str_buffer(mwi_description), pri,
07071          AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mbox_number,
07072          AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, mbox_context,
07073          AST_EVENT_IE_END);
07074       if (!pri->mbox[i].sub) {
07075          ast_log(LOG_ERROR, "%s span %d could not subscribe to MWI events for %s@%s.",
07076             sig_pri_cc_type_name, pri->span, mbox_number, mbox_context);
07077          continue;
07078       }
07079       pri->mbox[i].number = mbox_number;
07080       pri->mbox[i].context = mbox_context;
07081       ++i;
07082    }
07083 #endif   /* defined(HAVE_PRI_MWI) */
07084 
07085    for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
07086       if (pri->fds[i] == -1) {
07087          break;
07088       }
07089 
07090       switch (pri->sig) {
07091       case SIG_BRI:
07092          pri->dchans[i] = pri_new_bri(pri->fds[i], 1, pri->nodetype, pri->switchtype);
07093          break;
07094       case SIG_BRI_PTMP:
07095          pri->dchans[i] = pri_new_bri(pri->fds[i], 0, pri->nodetype, pri->switchtype);
07096          break;
07097       default:
07098          pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
07099 #if defined(HAVE_PRI_SERVICE_MESSAGES)
07100          if (pri->enable_service_message_support) {
07101             pri_set_service_message_support(pri->dchans[i], 1);
07102          }
07103 #endif   /* defined(HAVE_PRI_SERVICE_MESSAGES) */
07104          break;
07105       }
07106 
07107       pri_set_overlapdial(pri->dchans[i], (pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING) ? 1 : 0);
07108 #ifdef HAVE_PRI_PROG_W_CAUSE
07109       pri_set_chan_mapping_logical(pri->dchans[i], pri->qsigchannelmapping == DAHDI_CHAN_MAPPING_LOGICAL);
07110 #endif
07111 #ifdef HAVE_PRI_INBANDDISCONNECT
07112       pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect);
07113 #endif
07114       /* Enslave to master if appropriate */
07115       if (i)
07116          pri_enslave(pri->dchans[0], pri->dchans[i]);
07117       if (!pri->dchans[i]) {
07118          if (pri->fds[i] > 0)
07119             close(pri->fds[i]);
07120          pri->fds[i] = -1;
07121          ast_log(LOG_ERROR, "Unable to create PRI structure\n");
07122          return -1;
07123       }
07124       pri_set_debug(pri->dchans[i], SIG_PRI_DEBUG_DEFAULT);
07125       pri_set_nsf(pri->dchans[i], pri->nsf);
07126 #ifdef PRI_GETSET_TIMERS
07127       for (x = 0; x < PRI_MAX_TIMERS; x++) {
07128          if (pri->pritimers[x] != 0)
07129             pri_set_timer(pri->dchans[i], x, pri->pritimers[x]);
07130       }
07131 #endif
07132    }
07133 
07134    /* Assume primary is the one we use */
07135    pri->pri = pri->dchans[0];
07136 
07137 #if defined(HAVE_PRI_CALL_HOLD)
07138    pri_hold_enable(pri->pri, 1);
07139 #endif   /* defined(HAVE_PRI_CALL_HOLD) */
07140 #if defined(HAVE_PRI_CALL_REROUTING)
07141    pri_reroute_enable(pri->pri, 1);
07142 #endif   /* defined(HAVE_PRI_CALL_REROUTING) */
07143 #if defined(HAVE_PRI_HANGUP_FIX)
07144    pri_hangup_fix_enable(pri->pri, 1);
07145 #endif   /* defined(HAVE_PRI_HANGUP_FIX) */
07146 #if defined(HAVE_PRI_CCSS)
07147    pri_cc_enable(pri->pri, 1);
07148    pri_cc_recall_mode(pri->pri, pri->cc_ptmp_recall_mode);
07149    pri_cc_retain_signaling_req(pri->pri, pri->cc_qsig_signaling_link_req);
07150    pri_cc_retain_signaling_rsp(pri->pri, pri->cc_qsig_signaling_link_rsp);
07151 #endif   /* defined(HAVE_PRI_CCSS) */
07152 #if defined(HAVE_PRI_TRANSFER)
07153    pri_transfer_enable(pri->pri, 1);
07154 #endif   /* defined(HAVE_PRI_TRANSFER) */
07155 #if defined(HAVE_PRI_AOC_EVENTS)
07156    pri_aoc_events_enable(pri->pri, 1);
07157 #endif   /* defined(HAVE_PRI_AOC_EVENTS) */
07158 #if defined(HAVE_PRI_CALL_WAITING)
07159    pri_connect_ack_enable(pri->pri, 1);
07160 #endif   /* defined(HAVE_PRI_CALL_WAITING) */
07161 #if defined(HAVE_PRI_MCID)
07162    pri_mcid_enable(pri->pri, 1);
07163 #endif   /* defined(HAVE_PRI_MCID) */
07164 
07165    pri->resetpos = -1;
07166    if (ast_pthread_create_background(&pri->master, NULL, pri_dchannel, pri)) {
07167       for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
07168          if (!pri->dchans[i])
07169             break;
07170          if (pri->fds[i] > 0)
07171             close(pri->fds[i]);
07172          pri->fds[i] = -1;
07173       }
07174       ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
07175       return -1;
07176    }
07177 
07178 #if defined(HAVE_PRI_MWI)
07179    /*
07180     * Send the initial MWI indications from the event cache for this span.
07181     *
07182     * If we were loaded after app_voicemail the event would already be in
07183     * the cache.  If we were loaded before app_voicemail the event would not
07184     * be in the cache yet and app_voicemail will send the event when it
07185     * gets loaded.
07186     */
07187    sig_pri_mwi_cache_update(pri);
07188 #endif   /* defined(HAVE_PRI_MWI) */
07189 
07190    return 0;
07191 }
07192 
07193 void sig_pri_chan_alarm_notify(struct sig_pri_chan *p, int noalarm)
07194 {
07195    sig_pri_set_alarm(p, !noalarm);
07196    if (!noalarm) {
07197       if (!p->pri || !p->pri->pri || (pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0)) {
07198          /* T309 is not enabled : hangup calls when alarm occurs */
07199          if (p->call) {
07200             if (p->pri && p->pri->pri) {
07201                if (!pri_grab(p, p->pri)) {
07202                   pri_hangup(p->pri->pri, p->call, -1);
07203                   pri_destroycall(p->pri->pri, p->call);
07204                   p->call = NULL;
07205                   pri_rel(p->pri);
07206                } else
07207                   ast_log(LOG_WARNING, "Failed to grab PRI!\n");
07208             } else
07209                ast_log(LOG_WARNING, "The PRI Call has not been destroyed\n");
07210          }
07211          if (p->owner)
07212             p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
07213       }
07214    }
07215 }
07216 
07217 struct sig_pri_chan *sig_pri_chan_new(void *pvt_data, struct sig_pri_callback *callback, struct sig_pri_span *pri, int logicalspan, int channo, int trunkgroup)
07218 {
07219    struct sig_pri_chan *p;
07220 
07221    p = ast_calloc(1, sizeof(*p));
07222    if (!p)
07223       return p;
07224 
07225    p->logicalspan = logicalspan;
07226    p->prioffset = channo;
07227    p->mastertrunkgroup = trunkgroup;
07228 
07229    p->calls = callback;
07230    p->chan_pvt = pvt_data;
07231 
07232    p->pri = pri;
07233 
07234    return p;
07235 }
07236 
07237 /*!
07238  * \brief Delete the sig_pri private channel structure.
07239  * \since 1.8
07240  *
07241  * \param doomed sig_pri private channel structure to delete.
07242  *
07243  * \return Nothing
07244  */
07245 void sig_pri_chan_delete(struct sig_pri_chan *doomed)
07246 {
07247    ast_free(doomed);
07248 }
07249 
07250 static void build_status(char *s, size_t len, int status, int active)
07251 {
07252    if (!s || len < 1) {
07253       return;
07254    }
07255    s[0] = '\0';
07256    if (!(status & DCHAN_NOTINALARM))
07257       strncat(s, "In Alarm, ", len - strlen(s) - 1);
07258    if (status & DCHAN_UP)
07259       strncat(s, "Up", len - strlen(s) - 1);
07260    else
07261       strncat(s, "Down", len - strlen(s) - 1);
07262    if (active)
07263       strncat(s, ", Active", len - strlen(s) - 1);
07264    else
07265       strncat(s, ", Standby", len - strlen(s) - 1);
07266    s[len - 1] = '\0';
07267 }
07268 
07269 void sig_pri_cli_show_spans(int fd, int span, struct sig_pri_span *pri)
07270 {
07271    char status[256];
07272    int x;
07273    for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
07274       if (pri->dchans[x]) {
07275          build_status(status, sizeof(status), pri->dchanavail[x], pri->dchans[x] == pri->pri);
07276          ast_cli(fd, "PRI span %d/%d: %s\n", span, x, status);
07277       }
07278    }
07279 }
07280 
07281 void sig_pri_cli_show_span(int fd, int *dchannels, struct sig_pri_span *pri)
07282 {
07283    int x;
07284    char status[256];
07285 
07286    for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
07287       if (pri->dchans[x]) {
07288 #ifdef PRI_DUMP_INFO_STR
07289          char *info_str = NULL;
07290 #endif
07291          ast_cli(fd, "%s D-channel: %d\n", pri_order(x), dchannels[x]);
07292          build_status(status, sizeof(status), pri->dchanavail[x], pri->dchans[x] == pri->pri);
07293          ast_cli(fd, "Status: %s\n", status);
07294 #ifdef PRI_DUMP_INFO_STR
07295          info_str = pri_dump_info_str(pri->pri);
07296          if (info_str) {
07297             ast_cli(fd, "%s", info_str);
07298             free(info_str);
07299          }
07300 #else
07301          pri_dump_info(pri->pri);
07302 #endif
07303          ast_cli(fd, "Overlap Recv: %s\n\n", (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)?"Yes":"No");
07304          ast_cli(fd, "\n");
07305       }
07306    }
07307 }
07308 
07309 int pri_send_keypad_facility_exec(struct sig_pri_chan *p, const char *digits)
07310 {
07311    sig_pri_lock_private(p);
07312 
07313    if (!p->pri || !p->call) {
07314       ast_debug(1, "Unable to find pri or call on channel!\n");
07315       sig_pri_unlock_private(p);
07316       return -1;
07317    }
07318 
07319    if (!pri_grab(p, p->pri)) {
07320       pri_keypad_facility(p->pri->pri, p->call, digits);
07321       pri_rel(p->pri);
07322    } else {
07323       ast_debug(1, "Unable to grab pri to send keypad facility!\n");
07324       sig_pri_unlock_private(p);
07325       return -1;
07326    }
07327 
07328    sig_pri_unlock_private(p);
07329 
07330    return 0;
07331 }
07332 
07333 int pri_send_callrerouting_facility_exec(struct sig_pri_chan *p, enum ast_channel_state chanstate, const char *destination, const char *original, const char *reason)
07334 {
07335    int res = -1;
07336 
07337    sig_pri_lock_private(p);
07338 
07339    if (!p->pri || !p->call) {
07340       ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
07341       sig_pri_unlock_private(p);
07342       return -1;
07343    }
07344 
07345    if (!pri_grab(p, p->pri)) {
07346       res = pri_callrerouting_facility(p->pri->pri, p->call, destination, original, reason);
07347       pri_rel(p->pri);
07348    } else {
07349       ast_log(LOG_DEBUG, "Unable to grab pri to send callrerouting facility on span %d!\n", p->pri->span);
07350    }
07351 
07352    sig_pri_unlock_private(p);
07353 
07354    return res;
07355 }
07356 
07357 #if defined(HAVE_PRI_SERVICE_MESSAGES)
07358 int pri_maintenance_bservice(struct pri *pri, struct sig_pri_chan *p, int changestatus)
07359 {
07360    int channel = PVT_TO_CHANNEL(p);
07361    int span = PRI_SPAN(channel);
07362 
07363    return pri_maintenance_service(pri, span, channel, changestatus);
07364 }
07365 #endif   /* defined(HAVE_PRI_SERVICE_MESSAGES) */
07366 
07367 void sig_pri_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_pri_chan *pchan)
07368 {
07369    if (pchan->owner == oldchan) {
07370       pchan->owner = newchan;
07371    }
07372 }
07373 
07374 #if defined(HAVE_PRI_CCSS)
07375 /*!
07376  * \brief PRI CC agent initialization.
07377  * \since 1.8
07378  *
07379  * \param agent CC core agent control.
07380  * \param pvt_chan Original channel the agent will attempt to recall.
07381  *
07382  * \details
07383  * This callback is called when the CC core is initialized.  Agents should allocate
07384  * any private data necessary for the call and assign it to the private_data
07385  * on the agent.  Additionally, if any ast_cc_agent_flags are pertinent to the
07386  * specific agent type, they should be set in this function as well.
07387  *
07388  * \retval 0 on success.
07389  * \retval -1 on error.
07390  */
07391 int sig_pri_cc_agent_init(struct ast_cc_agent *agent, struct sig_pri_chan *pvt_chan)
07392 {
07393    struct sig_pri_cc_agent_prv *cc_pvt;
07394 
07395    cc_pvt = ast_calloc(1, sizeof(*cc_pvt));
07396    if (!cc_pvt) {
07397       return -1;
07398    }
07399 
07400    ast_mutex_lock(&pvt_chan->pri->lock);
07401    cc_pvt->pri = pvt_chan->pri;
07402    cc_pvt->cc_id = pri_cc_available(pvt_chan->pri->pri, pvt_chan->call);
07403    ast_mutex_unlock(&pvt_chan->pri->lock);
07404    if (cc_pvt->cc_id == -1) {
07405       ast_free(cc_pvt);
07406       return -1;
07407    }
07408    agent->private_data = cc_pvt;
07409    return 0;
07410 }
07411 #endif   /* defined(HAVE_PRI_CCSS) */
07412 
07413 #if defined(HAVE_PRI_CCSS)
07414 /*!
07415  * \brief Start the offer timer.
07416  * \since 1.8
07417  *
07418  * \param agent CC core agent control.
07419  *
07420  * \details
07421  * This is called by the core when the caller hangs up after
07422  * a call for which CC may be requested. The agent should
07423  * begin the timer as configured.
07424  *
07425  * The primary reason why this functionality is left to
07426  * the specific agent implementations is due to the differing
07427  * use of schedulers throughout the code. Some channel drivers
07428  * may already have a scheduler context they wish to use, and
07429  * amongst those, some may use the ast_sched API while others
07430  * may use the ast_sched_thread API, which are incompatible.
07431  *
07432  * \retval 0 on success.
07433  * \retval -1 on error.
07434  */
07435 int sig_pri_cc_agent_start_offer_timer(struct ast_cc_agent *agent)
07436 {
07437    /* libpri maintains it's own offer timer in the form of T_RETENTION. */
07438    return 0;
07439 }
07440 #endif   /* defined(HAVE_PRI_CCSS) */
07441 
07442 #if defined(HAVE_PRI_CCSS)
07443 /*!
07444  * \brief Stop the offer timer.
07445  * \since 1.8
07446  *
07447  * \param agent CC core agent control.
07448  *
07449  * \details
07450  * This callback is called by the CC core when the caller
07451  * has requested CC.
07452  *
07453  * \retval 0 on success.
07454  * \retval -1 on error.
07455  */
07456 int sig_pri_cc_agent_stop_offer_timer(struct ast_cc_agent *agent)
07457 {
07458    /* libpri maintains it's own offer timer in the form of T_RETENTION. */
07459    return 0;
07460 }
07461 #endif   /* defined(HAVE_PRI_CCSS) */
07462 
07463 #if defined(HAVE_PRI_CCSS)
07464 /*!
07465  * \brief Acknowledge CC request.
07466  * \since 1.8
07467  *
07468  * \param agent CC core agent control.
07469  *
07470  * \details
07471  * When the core receives knowledge that a called
07472  * party has accepted a CC request, it will call
07473  * this callback.
07474  *
07475  * The duty of this is to accept a CC request from
07476  * the caller by acknowledging receipt of that request.
07477  *
07478  * \return Nothing
07479  */
07480 void sig_pri_cc_agent_req_ack(struct ast_cc_agent *agent)
07481 {
07482    struct sig_pri_cc_agent_prv *cc_pvt;
07483    int res;
07484 
07485    cc_pvt = agent->private_data;
07486    ast_mutex_lock(&cc_pvt->pri->lock);
07487    if (cc_pvt->cc_request_response_pending) {
07488       cc_pvt->cc_request_response_pending = 0;
07489       res = pri_cc_req_rsp(cc_pvt->pri->pri, cc_pvt->cc_id, 0/* success */);
07490    } else {
07491       res = 0;
07492    }
07493    ast_mutex_unlock(&cc_pvt->pri->lock);
07494    if (res) {
07495       ast_cc_failed(agent->core_id, "%s agent failed to send the CC request ack.",
07496          sig_pri_cc_type_name);
07497    }
07498 }
07499 #endif   /* defined(HAVE_PRI_CCSS) */
07500 
07501 #if defined(HAVE_PRI_CCSS)
07502 /*!
07503  * \brief Request the status of the agent's device.
07504  * \since 1.8
07505  *
07506  * \param agent CC core agent control.
07507  *
07508  * \details
07509  * Asynchronous request for the status of any caller
07510  * which may be a valid caller for the CC transaction.
07511  * Status responses should be made using the
07512  * ast_cc_status_response function.
07513  *
07514  * \retval 0 on success.
07515  * \retval -1 on error.
07516  */
07517 int sig_pri_cc_agent_status_req(struct ast_cc_agent *agent)
07518 {
07519    struct sig_pri_cc_agent_prv *cc_pvt;
07520 
07521    cc_pvt = agent->private_data;
07522    ast_mutex_lock(&cc_pvt->pri->lock);
07523    pri_cc_status_req(cc_pvt->pri->pri, cc_pvt->cc_id);
07524    ast_mutex_unlock(&cc_pvt->pri->lock);
07525    return 0;
07526 }
07527 #endif   /* defined(HAVE_PRI_CCSS) */
07528 
07529 #if defined(HAVE_PRI_CCSS)
07530 /*!
07531  * \brief Request for an agent's phone to stop ringing.
07532  * \since 1.8
07533  *
07534  * \param agent CC core agent control.
07535  *
07536  * \details
07537  * The usefulness of this is quite limited. The only specific
07538  * known case for this is if Asterisk requests CC over an ISDN
07539  * PTMP link as the TE side. If other phones are in the same
07540  * recall group as the Asterisk server, and one of those phones
07541  * picks up the recall notice, then Asterisk will receive a
07542  * "stop ringing" notification from the NT side of the PTMP
07543  * link. This indication needs to be passed to the phone
07544  * on the other side of the Asterisk server which originally
07545  * placed the call so that it will stop ringing. Since the
07546  * phone may be of any type, it is necessary to have a callback
07547  * that the core can know about.
07548  *
07549  * \retval 0 on success.
07550  * \retval -1 on error.
07551  */
07552 int sig_pri_cc_agent_stop_ringing(struct ast_cc_agent *agent)
07553 {
07554    struct sig_pri_cc_agent_prv *cc_pvt;
07555 
07556    cc_pvt = agent->private_data;
07557    ast_mutex_lock(&cc_pvt->pri->lock);
07558    pri_cc_stop_alerting(cc_pvt->pri->pri, cc_pvt->cc_id);
07559    ast_mutex_unlock(&cc_pvt->pri->lock);
07560    return 0;
07561 }
07562 #endif   /* defined(HAVE_PRI_CCSS) */
07563 
07564 #if defined(HAVE_PRI_CCSS)
07565 /*!
07566  * \brief Let the caller know that the callee has become free
07567  * but that the caller cannot attempt to call back because
07568  * he is either busy or there is congestion on his line.
07569  * \since 1.8
07570  *
07571  * \param agent CC core agent control.
07572  *
07573  * \details
07574  * This is something that really only affects a scenario where
07575  * a phone places a call over ISDN PTMP to Asterisk, who then
07576  * connects over PTMP again to the ISDN network. For most agent
07577  * types, there is no need to implement this callback at all
07578  * because they don't really need to actually do anything in
07579  * this situation. If you're having trouble understanding what
07580  * the purpose of this callback is, then you can be safe simply
07581  * not implementing it.
07582  *
07583  * \retval 0 on success.
07584  * \retval -1 on error.
07585  */
07586 int sig_pri_cc_agent_party_b_free(struct ast_cc_agent *agent)
07587 {
07588    struct sig_pri_cc_agent_prv *cc_pvt;
07589 
07590    cc_pvt = agent->private_data;
07591    ast_mutex_lock(&cc_pvt->pri->lock);
07592    pri_cc_b_free(cc_pvt->pri->pri, cc_pvt->cc_id);
07593    ast_mutex_unlock(&cc_pvt->pri->lock);
07594    return 0;
07595 }
07596 #endif   /* defined(HAVE_PRI_CCSS) */
07597 
07598 #if defined(HAVE_PRI_CCSS)
07599 /*!
07600  * \brief Begin monitoring a busy device.
07601  * \since 1.8
07602  *
07603  * \param agent CC core agent control.
07604  *
07605  * \details
07606  * The core will call this callback if the callee becomes
07607  * available but the caller has reported that he is busy.
07608  * The agent should begin monitoring the caller's device.
07609  * When the caller becomes available again, the agent should
07610  * call ast_cc_agent_caller_available.
07611  *
07612  * \retval 0 on success.
07613  * \retval -1 on error.
07614  */
07615 int sig_pri_cc_agent_start_monitoring(struct ast_cc_agent *agent)
07616 {
07617    /* libpri already knows when and how it needs to monitor Party A. */
07618    return 0;
07619 }
07620 #endif   /* defined(HAVE_PRI_CCSS) */
07621 
07622 #if defined(HAVE_PRI_CCSS)
07623 /*!
07624  * \brief Alert the caller that it is time to try recalling.
07625  * \since 1.8
07626  *
07627  * \param agent CC core agent control.
07628  *
07629  * \details
07630  * The core will call this function when it receives notice
07631  * that a monitored party has become available.
07632  *
07633  * The agent's job is to send a message to the caller to
07634  * notify it of such a change. If the agent is able to
07635  * discern that the caller is currently unavailable, then
07636  * the agent should react by calling the ast_cc_caller_unavailable
07637  * function.
07638  *
07639  * \retval 0 on success.
07640  * \retval -1 on error.
07641  */
07642 int sig_pri_cc_agent_callee_available(struct ast_cc_agent *agent)
07643 {
07644    struct sig_pri_cc_agent_prv *cc_pvt;
07645 
07646    cc_pvt = agent->private_data;
07647    ast_mutex_lock(&cc_pvt->pri->lock);
07648    pri_cc_remote_user_free(cc_pvt->pri->pri, cc_pvt->cc_id);
07649    ast_mutex_unlock(&cc_pvt->pri->lock);
07650    return 0;
07651 }
07652 #endif   /* defined(HAVE_PRI_CCSS) */
07653 
07654 #if defined(HAVE_PRI_CCSS)
07655 /*!
07656  * \brief Destroy private data on the agent.
07657  * \since 1.8
07658  *
07659  * \param agent CC core agent control.
07660  *
07661  * \details
07662  * The core will call this function upon completion
07663  * or failure of CC.
07664  *
07665  * \note
07666  * The agent private_data pointer may be NULL if the agent
07667  * constructor failed.
07668  *
07669  * \return Nothing
07670  */
07671 void sig_pri_cc_agent_destructor(struct ast_cc_agent *agent)
07672 {
07673    struct sig_pri_cc_agent_prv *cc_pvt;
07674    int res;
07675 
07676    cc_pvt = agent->private_data;
07677    if (!cc_pvt) {
07678       /* The agent constructor probably failed. */
07679       return;
07680    }
07681    ast_mutex_lock(&cc_pvt->pri->lock);
07682    res = -1;
07683    if (cc_pvt->cc_request_response_pending) {
07684       res = pri_cc_req_rsp(cc_pvt->pri->pri, cc_pvt->cc_id, 2/* short_term_denial */);
07685    }
07686    if (res) {
07687       pri_cc_cancel(cc_pvt->pri->pri, cc_pvt->cc_id);
07688    }
07689    ast_mutex_unlock(&cc_pvt->pri->lock);
07690    ast_free(cc_pvt);
07691 }
07692 #endif   /* defined(HAVE_PRI_CCSS) */
07693 
07694 #if defined(HAVE_PRI_CCSS)
07695 /*!
07696  * \internal
07697  * \brief Return the hash value of the given CC monitor instance object.
07698  * \since 1.8
07699  *
07700  * \param obj pointer to the (user-defined part) of an object.
07701  * \param flags flags from ao2_callback().  Ignored at the moment.
07702  *
07703  * \retval core_id
07704  */
07705 static int sig_pri_cc_monitor_instance_hash_fn(const void *obj, const int flags)
07706 {
07707    const struct sig_pri_cc_monitor_instance *monitor_instance = obj;
07708 
07709    return monitor_instance->core_id;
07710 }
07711 #endif   /* defined(HAVE_PRI_CCSS) */
07712 
07713 #if defined(HAVE_PRI_CCSS)
07714 /*!
07715  * \internal
07716  * \brief Compere the monitor instance core_id key value.
07717  * \since 1.8
07718  *
07719  * \param obj pointer to the (user-defined part) of an object.
07720  * \param arg callback argument from ao2_callback()
07721  * \param flags flags from ao2_callback()
07722  *
07723  * \return values are a combination of enum _cb_results.
07724  */
07725 static int sig_pri_cc_monitor_instance_cmp_fn(void *obj, void *arg, int flags)
07726 {
07727    struct sig_pri_cc_monitor_instance *monitor_1 = obj;
07728    struct sig_pri_cc_monitor_instance *monitor_2 = arg;
07729 
07730    return monitor_1->core_id == monitor_2->core_id ? CMP_MATCH | CMP_STOP : 0;
07731 }
07732 #endif   /* defined(HAVE_PRI_CCSS) */
07733 
07734 #if defined(HAVE_PRI_CCSS)
07735 /*!
07736  * \brief Request CCSS.
07737  * \since 1.8
07738  *
07739  * \param monitor CC core monitor control.
07740  * \param available_timer_id Where to put the available timer scheduler id.
07741  * Will never be NULL for a device monitor.
07742  *
07743  * \details
07744  * Perform whatever steps are necessary in order to request CC.
07745  * In addition, the monitor implementation is responsible for
07746  * starting the available timer in this callback. The scheduler
07747  * ID for the callback must be stored in the parent_link's child_avail_id
07748  * field.
07749  *
07750  * \retval 0 on success
07751  * \retval -1 on failure.
07752  */
07753 int sig_pri_cc_monitor_req_cc(struct ast_cc_monitor *monitor, int *available_timer_id)
07754 {
07755    struct sig_pri_cc_monitor_instance *instance;
07756    int cc_mode;
07757    int res;
07758 
07759    switch (monitor->service_offered) {
07760    case AST_CC_CCBS:
07761       cc_mode = 0;/* CCBS */
07762       break;
07763    case AST_CC_CCNR:
07764       cc_mode = 1;/* CCNR */
07765       break;
07766    default:
07767       /* CC service not supported by ISDN. */
07768       return -1;
07769    }
07770 
07771    instance = monitor->private_data;
07772 
07773    /* libpri handles it's own available timer. */
07774    ast_mutex_lock(&instance->pri->lock);
07775    res = pri_cc_req(instance->pri->pri, instance->cc_id, cc_mode);
07776    ast_mutex_unlock(&instance->pri->lock);
07777 
07778    return res;
07779 }
07780 #endif   /* defined(HAVE_PRI_CCSS) */
07781 
07782 #if defined(HAVE_PRI_CCSS)
07783 /*!
07784  * \brief Suspend monitoring.
07785  * \since 1.8
07786  *
07787  * \param monitor CC core monitor control.
07788  *
07789  * \details
07790  * Implementers must perform the necessary steps to suspend
07791  * monitoring.
07792  *
07793  * \retval 0 on success
07794  * \retval -1 on failure.
07795  */
07796 int sig_pri_cc_monitor_suspend(struct ast_cc_monitor *monitor)
07797 {
07798    struct sig_pri_cc_monitor_instance *instance;
07799 
07800    instance = monitor->private_data;
07801    ast_mutex_lock(&instance->pri->lock);
07802    pri_cc_status(instance->pri->pri, instance->cc_id, 1/* busy */);
07803    ast_mutex_unlock(&instance->pri->lock);
07804 
07805    return 0;
07806 }
07807 #endif   /* defined(HAVE_PRI_CCSS) */
07808 
07809 #if defined(HAVE_PRI_CCSS)
07810 /*!
07811  * \brief Unsuspend monitoring.
07812  * \since 1.8
07813  *
07814  * \param monitor CC core monitor control.
07815  *
07816  * \details
07817  * Perform the necessary steps to unsuspend monitoring.
07818  *
07819  * \retval 0 on success
07820  * \retval -1 on failure.
07821  */
07822 int sig_pri_cc_monitor_unsuspend(struct ast_cc_monitor *monitor)
07823 {
07824    struct sig_pri_cc_monitor_instance *instance;
07825 
07826    instance = monitor->private_data;
07827    ast_mutex_lock(&instance->pri->lock);
07828    pri_cc_status(instance->pri->pri, instance->cc_id, 0/* free */);
07829    ast_mutex_unlock(&instance->pri->lock);
07830 
07831    return 0;
07832 }
07833 #endif   /* defined(HAVE_PRI_CCSS) */
07834 
07835 #if defined(HAVE_PRI_CCSS)
07836 /*!
07837  * \brief Status response to an ast_cc_monitor_status_request().
07838  * \since 1.8
07839  *
07840  * \param monitor CC core monitor control.
07841  * \param devstate Current status of a Party A device.
07842  *
07843  * \details
07844  * Alert a monitor as to the status of the agent for which
07845  * the monitor had previously requested a status request.
07846  *
07847  * \note Zero or more responses may come as a result.
07848  *
07849  * \retval 0 on success
07850  * \retval -1 on failure.
07851  */
07852 int sig_pri_cc_monitor_status_rsp(struct ast_cc_monitor *monitor, enum ast_device_state devstate)
07853 {
07854    struct sig_pri_cc_monitor_instance *instance;
07855    int cc_status;
07856 
07857    switch (devstate) {
07858    case AST_DEVICE_UNKNOWN:
07859    case AST_DEVICE_NOT_INUSE:
07860       cc_status = 0;/* free */
07861       break;
07862    case AST_DEVICE_BUSY:
07863    case AST_DEVICE_INUSE:
07864       cc_status = 1;/* busy */
07865       break;
07866    default:
07867       /* Don't know how to interpret this device state into free/busy status. */
07868       return 0;
07869    }
07870    instance = monitor->private_data;
07871    ast_mutex_lock(&instance->pri->lock);
07872    pri_cc_status_req_rsp(instance->pri->pri, instance->cc_id, cc_status);
07873    ast_mutex_unlock(&instance->pri->lock);
07874 
07875    return 0;
07876 }
07877 #endif   /* defined(HAVE_PRI_CCSS) */
07878 
07879 #if defined(HAVE_PRI_CCSS)
07880 /*!
07881  * \brief Cancel the running available timer.
07882  * \since 1.8
07883  *
07884  * \param monitor CC core monitor control.
07885  * \param sched_id Available timer scheduler id to cancel.
07886  * Will never be NULL for a device monitor.
07887  *
07888  * \details
07889  * In most cases, this function will likely consist of just a
07890  * call to AST_SCHED_DEL. It might have been possible to do this
07891  * within the core, but unfortunately the mixture of sched_thread
07892  * and sched usage in Asterisk prevents such usage.
07893  *
07894  * \retval 0 on success
07895  * \retval -1 on failure.
07896  */
07897 int sig_pri_cc_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id)
07898 {
07899    /*
07900     * libpri maintains it's own available timer as one of:
07901     * T_CCBS2/T_CCBS5/T_CCBS6/QSIG_CCBS_T2
07902     * T_CCNR2/T_CCNR5/T_CCNR6/QSIG_CCNR_T2
07903     */
07904    return 0;
07905 }
07906 #endif   /* defined(HAVE_PRI_CCSS) */
07907 
07908 #if defined(HAVE_PRI_CCSS)
07909 /*!
07910  * \brief Destroy PRI private data on the monitor.
07911  * \since 1.8
07912  *
07913  * \param monitor_pvt CC device monitor private data pointer.
07914  *
07915  * \details
07916  * Implementers of this callback are responsible for destroying
07917  * all heap-allocated data in the monitor's private_data pointer, including
07918  * the private_data itself.
07919  */
07920 void sig_pri_cc_monitor_destructor(void *monitor_pvt)
07921 {
07922    struct sig_pri_cc_monitor_instance *instance;
07923 
07924    instance = monitor_pvt;
07925    if (!instance) {
07926       return;
07927    }
07928    ao2_unlink(sig_pri_cc_monitors, instance);
07929    ao2_ref(instance, -1);
07930 }
07931 #endif   /* defined(HAVE_PRI_CCSS) */
07932 
07933 /*!
07934  * \brief Load the sig_pri submodule.
07935  * \since 1.8
07936  *
07937  * \param cc_type_name CC type name to use when looking up agent/monitor.
07938  *
07939  * \retval 0 on success.
07940  * \retval -1 on error.
07941  */
07942 int sig_pri_load(const char *cc_type_name)
07943 {
07944 #if defined(HAVE_PRI_CCSS)
07945    sig_pri_cc_type_name = cc_type_name;
07946    sig_pri_cc_monitors = ao2_container_alloc(37, sig_pri_cc_monitor_instance_hash_fn,
07947       sig_pri_cc_monitor_instance_cmp_fn);
07948    if (!sig_pri_cc_monitors) {
07949       return -1;
07950    }
07951 #endif   /* defined(HAVE_PRI_CCSS) */
07952    return 0;
07953 }
07954 
07955 /*!
07956  * \brief Unload the sig_pri submodule.
07957  * \since 1.8
07958  *
07959  * \return Nothing
07960  */
07961 void sig_pri_unload(void)
07962 {
07963 #if defined(HAVE_PRI_CCSS)
07964    if (sig_pri_cc_monitors) {
07965       ao2_ref(sig_pri_cc_monitors, -1);
07966       sig_pri_cc_monitors = NULL;
07967    }
07968 #endif   /* defined(HAVE_PRI_CCSS) */
07969 }
07970 
07971 #endif /* HAVE_PRI */

Generated on Wed Apr 6 11:29:48 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7