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

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