2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2009, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief PRI signaling module
23 * \author Matthew Fredrickson <creslin@digium.com>
35 #include "asterisk/utils.h"
36 #include "asterisk/options.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/app.h"
39 #include "asterisk/file.h"
40 #include "asterisk/callerid.h"
41 #include "asterisk/say.h"
42 #include "asterisk/manager.h"
43 #include "asterisk/astdb.h"
44 #include "asterisk/causes.h"
45 #include "asterisk/musiconhold.h"
46 #include "asterisk/cli.h"
47 #include "asterisk/transcap.h"
48 #include "asterisk/features.h"
51 #ifndef PRI_EVENT_FACILITY
52 #error please update libpri
55 /* define this to send PRI user-user information elements */
56 #undef SUPPORT_USERUSER
58 #if defined(HAVE_PRI_CCSS)
59 struct sig_pri_cc_agent_prv {
60 /*! Asterisk span D channel control structure. */
61 struct sig_pri_pri *pri;
62 /*! CC id value to use with libpri. -1 if invalid. */
64 /*! TRUE if CC has been requested and we are waiting for the response. */
65 unsigned char cc_request_response_pending;
68 struct sig_pri_cc_monitor_instance {
69 /*! \brief Asterisk span D channel control structure. */
70 struct sig_pri_pri *pri;
71 /*! CC id value to use with libpri. (-1 if already canceled). */
73 /*! CC core id value. */
75 /*! Device name(Channel name less sequence number) */
79 /*! Upper level agent/monitor type name. */
80 static const char *sig_pri_cc_type_name;
81 /*! Container of sig_pri monitor instances. */
82 static struct ao2_container *sig_pri_cc_monitors;
83 #endif /* defined(HAVE_PRI_CCSS) */
85 static int pri_matchdigittimeout = 3000;
87 static int pri_gendigittimeout = 8000;
89 #define DCHAN_NOTINALARM (1 << 0)
90 #define DCHAN_UP (1 << 1)
92 /* Defines to help decode the encoded event channel id. */
93 #define PRI_CHANNEL(p) ((p) & 0xff)
94 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
95 #define PRI_EXPLICIT (1 << 16)
96 #define PRI_CIS_CALL (1 << 17) /* Call is using the D channel only. */
97 #define PRI_HELD_CALL (1 << 18)
100 #define DCHAN_AVAILABLE (DCHAN_NOTINALARM | DCHAN_UP)
102 #define PRI_DEADLOCK_AVOIDANCE(p) \
104 sig_pri_unlock_private(p); \
106 sig_pri_lock_private(p); \
109 static int pri_active_dchan_index(struct sig_pri_pri *pri);
111 static inline void pri_rel(struct sig_pri_pri *pri)
113 ast_mutex_unlock(&pri->lock);
116 static unsigned int PVT_TO_CHANNEL(struct sig_pri_chan *p)
118 int res = (((p)->prioffset) | ((p)->logicalspan << 8) | (p->mastertrunkgroup ? PRI_EXPLICIT : 0));
119 ast_debug(5, "prioffset: %d mastertrunkgroup: %d logicalspan: %d result: %d\n",
120 p->prioffset, p->mastertrunkgroup, p->logicalspan, res);
125 static void sig_pri_handle_dchan_exception(struct sig_pri_pri *pri, int index)
127 if (pri->calls->handle_dchan_exception)
128 pri->calls->handle_dchan_exception(pri, index);
131 static void sig_pri_set_dialing(struct sig_pri_chan *p, int is_dialing)
133 if (p->calls->set_dialing) {
134 p->calls->set_dialing(p->chan_pvt, is_dialing);
138 static void sig_pri_set_digital(struct sig_pri_chan *p, int is_digital)
140 p->digital = is_digital;
141 if (p->calls->set_digital) {
142 p->calls->set_digital(p->chan_pvt, is_digital);
146 static void sig_pri_set_alarm(struct sig_pri_chan *p, int in_alarm)
148 p->inalarm = in_alarm;
149 if (p->calls->set_alarm) {
150 p->calls->set_alarm(p->chan_pvt, in_alarm);
154 static const char *sig_pri_get_orig_dialstring(struct sig_pri_chan *p)
156 if (p->calls->get_orig_dialstring) {
157 return p->calls->get_orig_dialstring(p->chan_pvt);
159 ast_log(LOG_ERROR, "get_orig_dialstring callback not defined\n");
163 #if defined(HAVE_PRI_CCSS)
164 static void sig_pri_make_cc_dialstring(struct sig_pri_chan *p, char *buf, size_t buf_size)
166 if (p->calls->make_cc_dialstring) {
167 p->calls->make_cc_dialstring(p->chan_pvt, buf, buf_size);
169 ast_log(LOG_ERROR, "make_cc_dialstring callback not defined\n");
173 #endif /* defined(HAVE_PRI_CCSS) */
177 * \brief Reevaluate the PRI span device state.
180 * \param pri Asterisk D channel control structure.
184 * \note Assumes the pri->lock is already obtained.
186 static void sig_pri_span_devstate_changed(struct sig_pri_pri *pri)
188 if (pri->calls->update_span_devstate) {
189 pri->calls->update_span_devstate(pri);
195 * \brief Set the caller id information in the parent module.
198 * \param p sig_pri channel structure.
202 static void sig_pri_set_caller_id(struct sig_pri_chan *p)
204 struct ast_party_caller caller;
206 if (p->calls->set_callerid) {
207 ast_party_caller_init(&caller);
208 caller.id.number = p->cid_num;
209 caller.id.name = p->cid_name;
210 if (!ast_strlen_zero(p->cid_subaddr)) {
211 caller.id.subaddress.valid = 1;
212 //caller.id.subaddress.type = 0;/* nsap */
213 //caller.id.subaddress.odd_even_indicator = 0;
214 caller.id.subaddress.str = p->cid_subaddr;
216 caller.id.number_type = p->cid_ton;
217 caller.id.number_presentation = p->callingpres;
218 caller.ani = p->cid_ani;
219 caller.ani2 = p->cid_ani2;
220 p->calls->set_callerid(p->chan_pvt, &caller);
226 * \brief Set the Dialed Number Identifier.
229 * \param p sig_pri channel structure.
230 * \param dnid Dialed Number Identifier string.
234 static void sig_pri_set_dnid(struct sig_pri_chan *p, const char *dnid)
236 if (p->calls->set_dnid) {
237 p->calls->set_dnid(p->chan_pvt, dnid);
243 * \brief Set the Redirecting Directory Number Information Service (RDNIS).
246 * \param p sig_pri channel structure.
247 * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
251 static void sig_pri_set_rdnis(struct sig_pri_chan *p, const char *rdnis)
253 if (p->calls->set_rdnis) {
254 p->calls->set_rdnis(p->chan_pvt, rdnis);
258 static void sig_pri_unlock_private(struct sig_pri_chan *p)
260 if (p->calls->unlock_private)
261 p->calls->unlock_private(p->chan_pvt);
264 static void sig_pri_lock_private(struct sig_pri_chan *p)
266 if (p->calls->lock_private)
267 p->calls->lock_private(p->chan_pvt);
270 static inline int pri_grab(struct sig_pri_chan *p, struct sig_pri_pri *pri)
273 /* Grab the lock first */
275 res = ast_mutex_trylock(&pri->lock);
277 PRI_DEADLOCK_AVOIDANCE(p);
280 /* Then break the poll */
281 pthread_kill(pri->master, SIGURG);
287 * \brief Convert PRI redirecting reason to asterisk version.
290 * \param pri_reason PRI redirecting reason.
292 * \return Equivalent asterisk redirecting reason value.
294 static enum AST_REDIRECTING_REASON pri_to_ast_reason(int pri_reason)
296 enum AST_REDIRECTING_REASON ast_reason;
298 switch (pri_reason) {
299 case PRI_REDIR_FORWARD_ON_BUSY:
300 ast_reason = AST_REDIRECTING_REASON_USER_BUSY;
302 case PRI_REDIR_FORWARD_ON_NO_REPLY:
303 ast_reason = AST_REDIRECTING_REASON_NO_ANSWER;
305 case PRI_REDIR_DEFLECTION:
306 ast_reason = AST_REDIRECTING_REASON_DEFLECTION;
308 case PRI_REDIR_UNCONDITIONAL:
309 ast_reason = AST_REDIRECTING_REASON_UNCONDITIONAL;
311 case PRI_REDIR_UNKNOWN:
313 ast_reason = AST_REDIRECTING_REASON_UNKNOWN;
322 * \brief Convert asterisk redirecting reason to PRI version.
325 * \param ast_reason Asterisk redirecting reason.
327 * \return Equivalent PRI redirecting reason value.
329 static int ast_to_pri_reason(enum AST_REDIRECTING_REASON ast_reason)
333 switch (ast_reason) {
334 case AST_REDIRECTING_REASON_USER_BUSY:
335 pri_reason = PRI_REDIR_FORWARD_ON_BUSY;
337 case AST_REDIRECTING_REASON_NO_ANSWER:
338 pri_reason = PRI_REDIR_FORWARD_ON_NO_REPLY;
340 case AST_REDIRECTING_REASON_UNCONDITIONAL:
341 pri_reason = PRI_REDIR_UNCONDITIONAL;
343 case AST_REDIRECTING_REASON_DEFLECTION:
344 pri_reason = PRI_REDIR_DEFLECTION;
346 case AST_REDIRECTING_REASON_UNKNOWN:
348 pri_reason = PRI_REDIR_UNKNOWN;
357 * \brief Convert PRI number presentation to asterisk version.
360 * \param pri_presentation PRI number presentation.
362 * \return Equivalent asterisk number presentation value.
364 static int pri_to_ast_presentation(int pri_presentation)
366 int ast_presentation;
368 switch (pri_presentation) {
369 case PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
370 ast_presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
372 case PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
373 ast_presentation = AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
375 case PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
376 ast_presentation = AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN;
378 case PRES_ALLOWED_NETWORK_NUMBER:
379 ast_presentation = AST_PRES_ALLOWED_NETWORK_NUMBER;
381 case PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
382 ast_presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
384 case PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
385 ast_presentation = AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
387 case PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
388 ast_presentation = AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN;
390 case PRES_PROHIB_NETWORK_NUMBER:
391 ast_presentation = AST_PRES_PROHIB_NETWORK_NUMBER;
393 case PRES_NUMBER_NOT_AVAILABLE:
394 ast_presentation = AST_PRES_NUMBER_NOT_AVAILABLE;
397 ast_presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
401 return ast_presentation;
406 * \brief Convert asterisk number presentation to PRI version.
409 * \param ast_presentation Asterisk number presentation.
411 * \return Equivalent PRI number presentation value.
413 static int ast_to_pri_presentation(int ast_presentation)
415 int pri_presentation;
417 switch (ast_presentation) {
418 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
419 pri_presentation = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
421 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
422 pri_presentation = PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
424 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
425 pri_presentation = PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN;
427 case AST_PRES_ALLOWED_NETWORK_NUMBER:
428 pri_presentation = PRES_ALLOWED_NETWORK_NUMBER;
430 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
431 pri_presentation = PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
433 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
434 pri_presentation = PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
436 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
437 pri_presentation = PRES_PROHIB_USER_NUMBER_FAILED_SCREEN;
439 case AST_PRES_PROHIB_NETWORK_NUMBER:
440 pri_presentation = PRES_PROHIB_NETWORK_NUMBER;
442 case AST_PRES_NUMBER_NOT_AVAILABLE:
443 pri_presentation = PRES_NUMBER_NOT_AVAILABLE;
446 pri_presentation = PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
450 return pri_presentation;
455 * \brief Determine the overall presentation value for the given party.
458 * \param id Party to determine the overall presentation value.
460 * \return Overall presentation value for the given party converted to ast values.
462 static int overall_ast_presentation(const struct pri_party_id *id)
466 int number_screening;
470 /* Determine name presentation priority. */
471 if (!id->name.valid) {
472 name_value = PRI_PRES_UNAVAILABLE;
475 name_value = id->name.presentation & PRI_PRES_RESTRICTION;
476 switch (name_value) {
477 case PRI_PRES_RESTRICTED:
480 case PRI_PRES_ALLOWED:
483 case PRI_PRES_UNAVAILABLE:
487 name_value = PRI_PRES_UNAVAILABLE;
493 /* Determine number presentation priority. */
494 if (!id->number.valid) {
495 number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
496 number_value = PRI_PRES_UNAVAILABLE;
499 number_screening = id->number.presentation & PRI_PRES_NUMBER_TYPE;
500 number_value = id->number.presentation & PRI_PRES_RESTRICTION;
501 switch (number_value) {
502 case PRI_PRES_RESTRICTED:
505 case PRI_PRES_ALLOWED:
508 case PRI_PRES_UNAVAILABLE:
512 number_screening = PRI_PRES_USER_NUMBER_UNSCREENED;
513 number_value = PRI_PRES_UNAVAILABLE;
519 /* Select the wining presentation value. */
520 if (name_priority < number_priority) {
521 number_value = name_value;
524 return pri_to_ast_presentation(number_value | number_screening);
527 #if defined(HAVE_PRI_SUBADDR)
530 * \brief Fill in the asterisk party subaddress from the given PRI party subaddress.
533 * \param ast_subaddress Asterisk party subaddress structure.
534 * \param pri_subaddress PRI party subaddress structure.
539 static void sig_pri_set_subaddress(struct ast_party_subaddress *ast_subaddress, const struct pri_party_subaddress *pri_subaddress)
544 if (ast_subaddress->str) {
545 ast_free(ast_subaddress->str);
547 if (pri_subaddress->length <= 0) {
548 ast_party_subaddress_init(ast_subaddress);
552 if (!pri_subaddress->type) {
554 ast_subaddress->str = ast_strdup((char *) pri_subaddress->data);
557 if (!(cnum = ast_malloc(2 * pri_subaddress->length + 1))) {
558 ast_party_subaddress_init(ast_subaddress);
563 len = pri_subaddress->length - 1; /* -1 account for zero based indexing */
564 for (x = 0; x < len; ++x) {
565 ptr += sprintf(ptr, "%02x", pri_subaddress->data[x]);
568 if (pri_subaddress->odd_even_indicator) {
570 sprintf(ptr, "%01x", (pri_subaddress->data[len]) >> 4);
573 sprintf(ptr, "%02x", pri_subaddress->data[len]);
575 ast_subaddress->str = cnum;
577 ast_subaddress->type = pri_subaddress->type;
578 ast_subaddress->odd_even_indicator = pri_subaddress->odd_even_indicator;
579 ast_subaddress->valid = 1;
581 #endif /* defined(HAVE_PRI_SUBADDR) */
583 #if defined(HAVE_PRI_SUBADDR)
584 static unsigned char ast_pri_pack_hex_char(char c)
590 } else if (c < ('9' + 1)) {
592 } else if (c < 'A') {
594 } else if (c < ('F' + 1)) {
596 } else if (c < 'a') {
598 } else if (c < ('f' + 1)) {
605 #endif /* defined(HAVE_PRI_SUBADDR) */
607 #if defined(HAVE_PRI_SUBADDR)
610 * \brief Convert a null terminated hexadecimal string to a packed hex byte array.
611 * \details left justified, with 0 padding if odd length.
614 * \param dst pointer to packed byte array.
615 * \param src pointer to null terminated hexadecimal string.
616 * \param maxlen destination array size.
618 * \return Length of byte array
620 * \note The dst is not an ASCIIz string.
621 * \note The src is an ASCIIz hex string.
623 static int ast_pri_pack_hex_string(unsigned char *dst, char *src, int maxlen)
626 int len = strlen(src);
628 if (len > (2 * maxlen)) {
632 res = len / 2 + len % 2;
635 *dst = ast_pri_pack_hex_char(*src) << 4;
637 *dst |= ast_pri_pack_hex_char(*src);
641 if (len) { /* 1 left */
642 *dst = ast_pri_pack_hex_char(*src) << 4;
646 #endif /* defined(HAVE_PRI_SUBADDR) */
648 #if defined(HAVE_PRI_SUBADDR)
651 * \brief Fill in the PRI party subaddress from the given asterisk party subaddress.
654 * \param pri_subaddress PRI party subaddress structure.
655 * \param ast_subaddress Asterisk party subaddress structure.
659 * \note Assumes that pri_subaddress has been previously memset to zero.
661 static void sig_pri_party_subaddress_from_ast(struct pri_party_subaddress *pri_subaddress, const struct ast_party_subaddress *ast_subaddress)
663 if (ast_subaddress->valid && !ast_strlen_zero(ast_subaddress->str)) {
664 pri_subaddress->type = ast_subaddress->type;
665 if (!ast_subaddress->type) {
667 ast_copy_string((char *) pri_subaddress->data, ast_subaddress->str,
668 sizeof(pri_subaddress->data));
669 pri_subaddress->length = strlen((char *) pri_subaddress->data);
670 pri_subaddress->odd_even_indicator = 0;
671 pri_subaddress->valid = 1;
673 /* 2 = User Specified */
675 * Copy HexString to packed HexData,
676 * if odd length then right pad trailing byte with 0
678 int length = ast_pri_pack_hex_string(pri_subaddress->data,
679 ast_subaddress->str, sizeof(pri_subaddress->data));
681 pri_subaddress->length = length;
682 pri_subaddress->odd_even_indicator = (length & 1);
683 pri_subaddress->valid = 1;
687 #endif /* defined(HAVE_PRI_SUBADDR) */
691 * \brief Fill in the PRI party id from the given asterisk party id.
694 * \param pri_id PRI party id structure.
695 * \param ast_id Asterisk party id structure.
699 * \note Assumes that pri_id has been previously memset to zero.
701 static void sig_pri_party_id_from_ast(struct pri_party_id *pri_id, const struct ast_party_id *ast_id)
705 presentation = ast_to_pri_presentation(ast_id->number_presentation);
706 if (!ast_strlen_zero(ast_id->name)) {
707 pri_id->name.valid = 1;
708 pri_id->name.presentation = presentation;
709 pri_id->name.char_set = PRI_CHAR_SET_ISO8859_1;
710 ast_copy_string(pri_id->name.str, ast_id->name, sizeof(pri_id->name.str));
712 if (!ast_strlen_zero(ast_id->number)) {
713 pri_id->number.valid = 1;
714 pri_id->number.presentation = presentation;
715 pri_id->number.plan = ast_id->number_type;
716 ast_copy_string(pri_id->number.str, ast_id->number, sizeof(pri_id->number.str));
718 #if defined(HAVE_PRI_SUBADDR)
719 sig_pri_party_subaddress_from_ast(&pri_id->subaddress, &ast_id->subaddress);
720 #endif /* defined(HAVE_PRI_SUBADDR) */
725 * \brief Update the PRI redirecting information for the current call.
728 * \param pvt sig_pri private channel structure.
729 * \param ast Asterisk channel
733 * \note Assumes that the PRI lock is already obtained.
735 static void sig_pri_redirecting_update(struct sig_pri_chan *pvt, struct ast_channel *ast)
737 struct pri_party_redirecting pri_redirecting;
739 /*! \todo XXX Original called data can be put in a channel data store that is inherited. */
741 memset(&pri_redirecting, 0, sizeof(pri_redirecting));
742 sig_pri_party_id_from_ast(&pri_redirecting.from, &ast->redirecting.from);
743 sig_pri_party_id_from_ast(&pri_redirecting.to, &ast->redirecting.to);
744 pri_redirecting.count = ast->redirecting.count;
745 pri_redirecting.reason = ast_to_pri_reason(ast->redirecting.reason);
747 pri_redirecting_update(pvt->pri->pri, pvt->call, &pri_redirecting);
752 * \brief Reset DTMF detector.
755 * \param p sig_pri channel structure.
759 static void sig_pri_dsp_reset_and_flush_digits(struct sig_pri_chan *p)
761 if (p->calls->dsp_reset_and_flush_digits) {
762 p->calls->dsp_reset_and_flush_digits(p->chan_pvt);
766 static int sig_pri_set_echocanceller(struct sig_pri_chan *p, int enable)
768 if (p->calls->set_echocanceller)
769 return p->calls->set_echocanceller(p->chan_pvt, enable);
774 static void sig_pri_fixup_chans(struct sig_pri_chan *old_chan, struct sig_pri_chan *new_chan)
776 if (old_chan->calls->fixup_chans)
777 old_chan->calls->fixup_chans(old_chan->chan_pvt, new_chan->chan_pvt);
780 static int sig_pri_play_tone(struct sig_pri_chan *p, enum sig_pri_tone tone)
782 if (p->calls->play_tone)
783 return p->calls->play_tone(p->chan_pvt, tone);
788 static struct ast_channel *sig_pri_new_ast_channel(struct sig_pri_chan *p, int state, int startpbx, int ulaw, int transfercapability, char *exten, const struct ast_channel *requestor)
790 struct ast_channel *c;
792 if (p->calls->new_ast_channel)
793 c = p->calls->new_ast_channel(p->chan_pvt, state, startpbx, ulaw, transfercapability, exten, requestor);
800 p->alreadyhungup = 0;
801 if (transfercapability & AST_TRANS_CAP_DIGITAL) {
802 c->transfercapability = transfercapability;
803 pbx_builtin_setvar_helper(c, "TRANSFERCAPABILITY", ast_transfercapability2str(transfercapability));
804 sig_pri_set_digital(p, 1);
806 if (p->pri && !pri_grab(p, p->pri)) {
807 sig_pri_span_devstate_changed(p->pri);
810 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
816 struct ast_channel *sig_pri_request(struct sig_pri_chan *p, enum sig_pri_law law, const struct ast_channel *requestor, int transfercapability)
818 struct ast_channel *ast;
820 ast_log(LOG_DEBUG, "%s %d\n", __FUNCTION__, p->channel);
823 ast = sig_pri_new_ast_channel(p, AST_STATE_RESERVED, 0, law, transfercapability, p->exten, requestor);
830 int pri_is_up(struct sig_pri_pri *pri)
833 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
834 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
840 static char *pri_order(int level)
856 /* Returns index of the active dchan */
857 static int pri_active_dchan_index(struct sig_pri_pri *pri)
861 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
862 if ((pri->dchans[x] == pri->pri))
866 ast_log(LOG_WARNING, "No active dchan found!\n");
870 static int pri_find_dchan(struct sig_pri_pri *pri)
877 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
878 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
880 if (pri->dchans[x] == old) {
886 /* This is annoying to see on non persistent layer 2 connections. Let's not complain in that case */
887 if (pri->sig != SIG_BRI_PTMP) {
888 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel as D-channel anyway!\n");
891 if (old && (oldslot != newslot))
892 ast_log(LOG_NOTICE, "Switching from d-channel fd %d to fd %d!\n",
893 pri->fds[oldslot], pri->fds[newslot]);
894 pri->pri = pri->dchans[newslot];
900 * \brief Obtain the sig_pri owner channel lock if the owner exists.
903 * \param pri sig_pri PRI control structure.
904 * \param chanpos Channel position in the span.
906 * \note Assumes the pri->lock is already obtained.
907 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
911 static void sig_pri_lock_owner(struct sig_pri_pri *pri, int chanpos)
914 if (!pri->pvts[chanpos]->owner) {
915 /* There is no owner lock to get. */
918 if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
919 /* We got the lock */
922 /* We must unlock the PRI to avoid the possibility of a deadlock */
923 ast_mutex_unlock(&pri->lock);
924 PRI_DEADLOCK_AVOIDANCE(pri->pvts[chanpos]);
925 ast_mutex_lock(&pri->lock);
931 * \brief Queue the given frame onto the owner channel.
934 * \param pri sig_pri PRI control structure.
935 * \param chanpos Channel position in the span.
936 * \param frame Frame to queue onto the owner channel.
938 * \note Assumes the pri->lock is already obtained.
939 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
943 static void pri_queue_frame(struct sig_pri_pri *pri, int chanpos, struct ast_frame *frame)
945 sig_pri_lock_owner(pri, chanpos);
946 if (pri->pvts[chanpos]->owner) {
947 ast_queue_frame(pri->pvts[chanpos]->owner, frame);
948 ast_channel_unlock(pri->pvts[chanpos]->owner);
954 * \brief Queue a control frame of the specified subclass onto the owner channel.
957 * \param pri sig_pri PRI control structure.
958 * \param chanpos Channel position in the span.
959 * \param subclass Control frame subclass to queue onto the owner channel.
961 * \note Assumes the pri->lock is already obtained.
962 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
966 static void pri_queue_control(struct sig_pri_pri *pri, int chanpos, int subclass)
968 struct ast_frame f = {AST_FRAME_CONTROL, };
969 struct sig_pri_chan *p = pri->pvts[chanpos];
971 if (p->calls->queue_control) {
972 p->calls->queue_control(p->chan_pvt, subclass);
975 f.subclass.integer = subclass;
976 pri_queue_frame(pri, chanpos, &f);
979 static int pri_find_principle(struct sig_pri_pri *pri, int channel, q931_call *call)
986 /* Channel is not picked yet. */
990 if (channel & PRI_HELD_CALL) {
992 /* Cannot find a held call without a call. */
996 for (x = 0; x < pri->numchans; ++x) {
998 && pri->pvts[x]->call == call) {
1006 span = PRI_SPAN(channel);
1007 if (!(channel & PRI_EXPLICIT)) {
1010 index = pri_active_dchan_index(pri);
1014 span = pri->dchan_logical_span[index];
1017 channel = PRI_CHANNEL(channel);
1019 for (x = 0; x < pri->numchans; x++) {
1021 && pri->pvts[x]->prioffset == channel
1022 && pri->pvts[x]->logicalspan == span
1023 && !pri->pvts[x]->no_b_channel) {
1032 static int pri_fixup_principle(struct sig_pri_pri *pri, int principle, q931_call *call)
1036 if (principle < 0 || pri->numchans <= principle) {
1044 if (pri->pvts[principle] && pri->pvts[principle]->call == call) {
1045 /* Call is already on the specified principle. */
1049 /* Find the old principle location. */
1050 for (x = 0; x < pri->numchans; x++) {
1051 struct sig_pri_chan *new_chan;
1052 struct sig_pri_chan *old_chan;
1054 if (!pri->pvts[x] || pri->pvts[x]->call != call) {
1058 /* Found our call */
1059 new_chan = pri->pvts[principle];
1060 old_chan = pri->pvts[x];
1062 ast_verb(3, "Moving call from channel %d to channel %d\n",
1063 old_chan->channel, new_chan->channel);
1064 if (new_chan->owner) {
1065 ast_log(LOG_WARNING,
1066 "Can't fix up channel from %d to %d because %d is already in use\n",
1067 old_chan->channel, new_chan->channel, new_chan->channel);
1071 sig_pri_fixup_chans(old_chan, new_chan);
1073 /* Fix it all up now */
1074 new_chan->owner = old_chan->owner;
1075 old_chan->owner = NULL;
1077 new_chan->call = old_chan->call;
1078 old_chan->call = NULL;
1080 /* Transfer flags from the old channel. */
1081 new_chan->alerting = old_chan->alerting;
1082 new_chan->alreadyhungup = old_chan->alreadyhungup;
1083 new_chan->isidlecall = old_chan->isidlecall;
1084 new_chan->proceeding = old_chan->proceeding;
1085 new_chan->progress = old_chan->progress;
1086 new_chan->setup_ack = old_chan->setup_ack;
1087 new_chan->outgoing = old_chan->outgoing;
1088 new_chan->digital = old_chan->digital;
1089 old_chan->alerting = 0;
1090 old_chan->alreadyhungup = 0;
1091 old_chan->isidlecall = 0;
1092 old_chan->proceeding = 0;
1093 old_chan->progress = 0;
1094 old_chan->setup_ack = 0;
1095 old_chan->outgoing = 0;
1096 old_chan->digital = 0;
1098 /* More stuff to transfer to the new channel. */
1099 #if defined(HAVE_PRI_REVERSE_CHARGE)
1100 new_chan->reverse_charging_indication = old_chan->reverse_charging_indication;
1101 #endif /* defined(HAVE_PRI_REVERSE_CHARGE) */
1102 #if defined(HAVE_PRI_SETUP_KEYPAD)
1103 strcpy(new_chan->keypad_digits, old_chan->keypad_digits);
1104 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
1106 if (new_chan->no_b_channel) {
1107 /* Copy the real channel configuration to the no B channel interface. */
1108 new_chan->hidecallerid = old_chan->hidecallerid;
1109 new_chan->hidecalleridname = old_chan->hidecalleridname;
1110 new_chan->immediate = old_chan->immediate;
1111 new_chan->priexclusive = old_chan->priexclusive;
1112 new_chan->priindication_oob = old_chan->priindication_oob;
1113 new_chan->use_callerid = old_chan->use_callerid;
1114 new_chan->use_callingpres = old_chan->use_callingpres;
1115 new_chan->stripmsd = old_chan->stripmsd;
1116 strcpy(new_chan->context, old_chan->context);
1117 strcpy(new_chan->mohinterpret, old_chan->mohinterpret);
1119 /* Become a member of the old channel span/trunk-group. */
1120 new_chan->logicalspan = old_chan->logicalspan;
1121 new_chan->mastertrunkgroup = old_chan->mastertrunkgroup;
1126 ast_log(LOG_WARNING, "Call specified, but not found?\n");
1130 static char * redirectingreason2str(int redirectingreason)
1132 switch (redirectingreason) {
1140 return "UNCONDITIONAL";
1142 return "NOREDIRECT";
1146 static char *dialplan2str(int dialplan)
1148 if (dialplan == -1) {
1149 return("Dynamically set dialplan in ISDN");
1151 return (pri_plan2str(dialplan));
1154 static void apply_plan_to_number(char *buf, size_t size, const struct sig_pri_pri *pri, const char *number, const int plan)
1157 case PRI_INTERNATIONAL_ISDN: /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */
1158 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
1160 case PRI_NATIONAL_ISDN: /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */
1161 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
1163 case PRI_LOCAL_ISDN: /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */
1164 snprintf(buf, size, "%s%s", pri->localprefix, number);
1166 case PRI_PRIVATE: /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */
1167 snprintf(buf, size, "%s%s", pri->privateprefix, number);
1169 case PRI_UNKNOWN: /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */
1170 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
1172 default: /* other Q.931 dialplan => don't twiddle with callingnum */
1173 snprintf(buf, size, "%s", number);
1178 /*! \note Assumes the pri->lock is already obtained. */
1179 static int pri_check_restart(struct sig_pri_pri *pri)
1181 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1183 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1186 } while (pri->resetpos < pri->numchans
1187 && (!pri->pvts[pri->resetpos]
1188 || pri->pvts[pri->resetpos]->no_b_channel
1189 || pri->pvts[pri->resetpos]->call
1190 || pri->pvts[pri->resetpos]->resetting));
1191 if (pri->resetpos < pri->numchans) {
1192 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1195 why = pri->pvts[pri->resetpos]->service_status;
1197 ast_log(LOG_NOTICE, "span '%d' channel '%d' out-of-service (reason: %s), not sending RESTART\n", pri->span,
1198 pri->pvts[pri->resetpos]->channel, (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
1201 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1203 /* Mark the channel as resetting and restart it */
1204 pri->pvts[pri->resetpos]->resetting = 1;
1205 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
1208 time(&pri->lastreset);
1213 static int pri_find_empty_chan(struct sig_pri_pri *pri, int backwards)
1221 if (backwards && (x < 0))
1223 if (!backwards && (x >= pri->numchans))
1226 && !pri->pvts[x]->no_b_channel
1227 && !pri->pvts[x]->inalarm
1228 && !pri->pvts[x]->owner) {
1229 ast_debug(1, "Found empty available channel %d/%d\n",
1230 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
1241 #if defined(HAVE_PRI_CALL_HOLD)
1244 * \brief Find or create an empty no-B-channel interface to use.
1247 * \param pri sig_pri span controller to find interface.
1249 * \note Assumes the pri->lock is already obtained.
1251 * \retval array-index into private pointer array on success.
1252 * \retval -1 on error.
1254 static int pri_find_empty_nobch(struct sig_pri_pri *pri)
1258 for (idx = 0; idx < pri->numchans; ++idx) {
1260 && pri->pvts[idx]->no_b_channel
1261 && !pri->pvts[idx]->inalarm
1262 && !pri->pvts[idx]->owner) {
1263 ast_debug(1, "Found empty available no B channel interface\n");
1268 /* Need to create a new interface. */
1269 if (pri->calls->new_nobch_intf) {
1270 idx = pri->calls->new_nobch_intf(pri);
1276 #endif /* defined(HAVE_PRI_CALL_HOLD) */
1278 #if defined(HAVE_PRI_CALL_HOLD)
1281 * \brief Find the channel associated with the libpri call.
1284 * \param pri sig_pri span controller to find interface.
1285 * \param call LibPRI opaque call pointer to find.
1287 * \note Assumes the pri->lock is already obtained.
1289 * \retval array-index into private pointer array on success.
1290 * \retval -1 on error.
1292 static int pri_find_pri_call(struct sig_pri_pri *pri, q931_call *call)
1296 for (idx = 0; idx < pri->numchans; ++idx) {
1297 if (pri->pvts[idx] && pri->pvts[idx]->call == call) {
1298 /* Found the channel */
1304 #endif /* defined(HAVE_PRI_CALL_HOLD) */
1306 static void *do_idle_thread(void *v_pvt)
1308 struct sig_pri_chan *pvt = v_pvt;
1309 struct ast_channel *chan = pvt->owner;
1310 struct ast_frame *f;
1312 /* Wait up to 30 seconds for an answer */
1313 int newms, ms = 30000;
1315 ast_verb(3, "Initiating idle call on channel %s\n", chan->name);
1316 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
1317 if (ast_call(chan, ex, 0)) {
1318 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
1322 while ((newms = ast_waitfor(chan, ms)) > 0) {
1328 if (f->frametype == AST_FRAME_CONTROL) {
1329 switch (f->subclass.integer) {
1330 case AST_CONTROL_ANSWER:
1331 /* Launch the PBX */
1332 ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
1333 ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
1335 ast_verb(4, "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
1337 /* It's already hungup, return immediately */
1339 case AST_CONTROL_BUSY:
1340 ast_verb(4, "Idle channel '%s' busy, waiting...\n", chan->name);
1342 case AST_CONTROL_CONGESTION:
1343 ast_verb(4, "Idle channel '%s' congested, waiting...\n", chan->name);
1350 /* Hangup the channel since nothing happend */
1355 static void *pri_ss_thread(void *data)
1357 struct sig_pri_chan *p = data;
1358 struct ast_channel *chan = p->owner;
1359 char exten[AST_MAX_EXTENSION];
1365 /* We lost the owner before we could get started. */
1370 * In the bizarre case where the channel has become a zombie before we
1371 * even get started here, abort safely.
1373 if (!chan->tech_pvt) {
1374 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
1379 ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
1381 sig_pri_dsp_reset_and_flush_digits(p);
1383 /* Now loop looking for an extension */
1384 ast_copy_string(exten, p->exten, sizeof(exten));
1385 len = strlen(exten);
1387 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
1388 if (len && !ast_ignore_pattern(chan->context, exten))
1389 sig_pri_play_tone(p, -1);
1391 sig_pri_play_tone(p, SIG_PRI_TONE_DIALTONE);
1392 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
1393 timeout = pri_matchdigittimeout;
1395 timeout = pri_gendigittimeout;
1396 res = ast_waitfordigit(chan, timeout);
1398 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
1407 /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
1408 if (ast_strlen_zero(exten)) {
1409 ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
1413 if (chan->cid.cid_dnid) {
1414 ast_free(chan->cid.cid_dnid);
1416 chan->cid.cid_dnid = ast_strdup(exten);
1418 sig_pri_play_tone(p, -1);
1419 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
1420 /* Start the real PBX */
1421 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
1422 sig_pri_dsp_reset_and_flush_digits(p);
1423 if (p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) {
1425 if (!pri_grab(p, p->pri)) {
1426 pri_proceeding(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 0);
1430 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
1435 sig_pri_set_echocanceller(p, 1);
1436 ast_setstate(chan, AST_STATE_RING);
1437 res = ast_pbx_run(chan);
1439 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
1442 ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
1443 chan->hangupcause = AST_CAUSE_UNALLOCATED;
1446 /* Since we send release complete here, we won't get one */
1452 res = sig_pri_play_tone(p, SIG_PRI_TONE_CONGESTION);
1454 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
1459 void pri_event_alarm(struct sig_pri_pri *pri, int index, int before_start_pri)
1461 pri->dchanavail[index] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
1462 if (!before_start_pri)
1463 pri_find_dchan(pri);
1466 void pri_event_noalarm(struct sig_pri_pri *pri, int index, int before_start_pri)
1468 pri->dchanavail[index] |= DCHAN_NOTINALARM;
1469 if (!before_start_pri)
1470 pri_restart(pri->dchans[index]);
1475 * \brief Convert libpri party id into asterisk party id.
1478 * \param ast_id Asterisk party id structure to fill. Must already be set initialized.
1479 * \param pri_id libpri party id structure containing source information.
1480 * \param pri Span controlling structure.
1482 * \note The filled in ast_id structure needs to be destroyed by
1483 * ast_party_id_free() when it is no longer needed.
1487 static void sig_pri_party_id_convert(struct ast_party_id *ast_id,
1488 const struct pri_party_id *pri_id, struct sig_pri_pri *pri)
1490 char number[AST_MAX_EXTENSION];
1492 if (pri_id->name.valid) {
1493 ast_id->name = ast_strdup(pri_id->name.str);
1495 if (pri_id->number.valid) {
1496 apply_plan_to_number(number, sizeof(number), pri, pri_id->number.str,
1497 pri_id->number.plan);
1498 ast_id->number = ast_strdup(number);
1499 ast_id->number_type = pri_id->number.plan;
1501 if (pri_id->name.valid || pri_id->number.valid) {
1502 ast_id->number_presentation = overall_ast_presentation(pri_id);
1504 #if defined(HAVE_PRI_SUBADDR)
1505 if (pri_id->subaddress.valid) {
1506 sig_pri_set_subaddress(&ast_id->subaddress, &pri_id->subaddress);
1508 #endif /* defined(HAVE_PRI_SUBADDR) */
1513 * \brief Convert libpri redirecting information into asterisk redirecting information.
1516 * \param ast_redirecting Asterisk redirecting structure to fill.
1517 * \param pri_redirecting libpri redirecting structure containing source information.
1518 * \param ast_guide Asterisk redirecting structure to use as an initialization guide.
1519 * \param pri Span controlling structure.
1521 * \note The filled in ast_redirecting structure needs to be destroyed by
1522 * ast_party_redirecting_free() when it is no longer needed.
1526 static void sig_pri_redirecting_convert(struct ast_party_redirecting *ast_redirecting,
1527 const struct pri_party_redirecting *pri_redirecting,
1528 const struct ast_party_redirecting *ast_guide,
1529 struct sig_pri_pri *pri)
1531 ast_party_redirecting_set_init(ast_redirecting, ast_guide);
1533 sig_pri_party_id_convert(&ast_redirecting->from, &pri_redirecting->from, pri);
1534 sig_pri_party_id_convert(&ast_redirecting->to, &pri_redirecting->to, pri);
1535 ast_redirecting->count = pri_redirecting->count;
1536 ast_redirecting->reason = pri_to_ast_reason(pri_redirecting->reason);
1541 * \brief Determine if the given extension matches one of the MSNs in the pattern list.
1544 * \param msn_patterns Comma separated list of MSN patterns to match.
1545 * \param exten Extension to match in the MSN list.
1547 * \retval 1 if matches.
1548 * \retval 0 if no match.
1550 static int sig_pri_msn_match(const char *msn_patterns, const char *exten)
1556 msn_list = strdupa(msn_patterns);
1559 pattern = strtok_r(msn_list, ",", &list_tail);
1561 pattern = ast_strip(pattern);
1562 if (!ast_strlen_zero(pattern) && ast_extension_match(pattern, exten)) {
1563 /* Extension matched the pattern. */
1566 pattern = strtok_r(NULL, ",", &list_tail);
1568 /* Did not match any pattern in the list. */
1572 #if defined(HAVE_PRI_CCSS)
1575 * \brief Compare the CC agent private data by libpri cc_id.
1578 * \param obj pointer to the (user-defined part) of an object.
1579 * \param arg callback argument from ao2_callback()
1580 * \param flags flags from ao2_callback()
1582 * \return values are a combination of enum _cb_results.
1584 static int sig_pri_cc_agent_cmp_cc_id(void *obj, void *arg, int flags)
1586 struct ast_cc_agent *agent_1 = obj;
1587 struct sig_pri_cc_agent_prv *agent_prv_1 = agent_1->private_data;
1588 struct sig_pri_cc_agent_prv *agent_prv_2 = arg;
1590 return (agent_prv_1 && agent_prv_1->pri == agent_prv_2->pri
1591 && agent_prv_1->cc_id == agent_prv_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
1593 #endif /* defined(HAVE_PRI_CCSS) */
1595 #if defined(HAVE_PRI_CCSS)
1598 * \brief Find the CC agent by libpri cc_id.
1601 * \param pri sig_pri PRI control structure.
1602 * \param cc_id CC record ID to find.
1605 * Since agents are refcounted, and this function returns
1606 * a reference to the agent, it is imperative that you decrement
1607 * the refcount of the agent once you have finished using it.
1609 * \retval agent on success.
1610 * \retval NULL not found.
1612 static struct ast_cc_agent *sig_pri_find_cc_agent_by_cc_id(struct sig_pri_pri *pri, long cc_id)
1614 struct sig_pri_cc_agent_prv finder = {
1619 return ast_cc_agent_callback(0, sig_pri_cc_agent_cmp_cc_id, &finder,
1620 sig_pri_cc_type_name);
1622 #endif /* defined(HAVE_PRI_CCSS) */
1624 #if defined(HAVE_PRI_CCSS)
1627 * \brief Compare the CC monitor instance by libpri cc_id.
1630 * \param obj pointer to the (user-defined part) of an object.
1631 * \param arg callback argument from ao2_callback()
1632 * \param flags flags from ao2_callback()
1634 * \return values are a combination of enum _cb_results.
1636 static int sig_pri_cc_monitor_cmp_cc_id(void *obj, void *arg, int flags)
1638 struct sig_pri_cc_monitor_instance *monitor_1 = obj;
1639 struct sig_pri_cc_monitor_instance *monitor_2 = arg;
1641 return (monitor_1->pri == monitor_2->pri
1642 && monitor_1->cc_id == monitor_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
1644 #endif /* defined(HAVE_PRI_CCSS) */
1646 #if defined(HAVE_PRI_CCSS)
1649 * \brief Find the CC monitor instance by libpri cc_id.
1652 * \param pri sig_pri PRI control structure.
1653 * \param cc_id CC record ID to find.
1656 * Since monitor_instances are refcounted, and this function returns
1657 * a reference to the instance, it is imperative that you decrement
1658 * the refcount of the instance once you have finished using it.
1660 * \retval monitor_instance on success.
1661 * \retval NULL not found.
1663 static struct sig_pri_cc_monitor_instance *sig_pri_find_cc_monitor_by_cc_id(struct sig_pri_pri *pri, long cc_id)
1665 struct sig_pri_cc_monitor_instance finder = {
1670 return ao2_callback(sig_pri_cc_monitors, 0, sig_pri_cc_monitor_cmp_cc_id, &finder);
1672 #endif /* defined(HAVE_PRI_CCSS) */
1674 #if defined(HAVE_PRI_CCSS)
1677 * \brief Destroy the given monitor instance.
1680 * \param data Monitor instance to destroy.
1684 static void sig_pri_cc_monitor_instance_destroy(void *data)
1686 struct sig_pri_cc_monitor_instance *monitor_instance = data;
1688 if (monitor_instance->cc_id != -1) {
1689 ast_mutex_lock(&monitor_instance->pri->lock);
1690 pri_cc_cancel(monitor_instance->pri->pri, monitor_instance->cc_id);
1691 ast_mutex_unlock(&monitor_instance->pri->lock);
1693 monitor_instance->pri->calls->module_unref();
1695 #endif /* defined(HAVE_PRI_CCSS) */
1697 #if defined(HAVE_PRI_CCSS)
1700 * \brief Construct a new monitor instance.
1703 * \param core_id CC core ID.
1704 * \param pri sig_pri PRI control structure.
1705 * \param cc_id CC record ID.
1706 * \param device_name Name of device (Asterisk channel name less sequence number).
1709 * Since monitor_instances are refcounted, and this function returns
1710 * a reference to the instance, it is imperative that you decrement
1711 * the refcount of the instance once you have finished using it.
1713 * \retval monitor_instance on success.
1714 * \retval NULL on error.
1716 static struct sig_pri_cc_monitor_instance *sig_pri_cc_monitor_instance_init(int core_id, struct sig_pri_pri *pri, long cc_id, const char *device_name)
1718 struct sig_pri_cc_monitor_instance *monitor_instance;
1720 if (!pri->calls->module_ref || !pri->calls->module_unref) {
1724 monitor_instance = ao2_alloc(sizeof(*monitor_instance) + strlen(device_name),
1725 sig_pri_cc_monitor_instance_destroy);
1726 if (!monitor_instance) {
1730 monitor_instance->cc_id = cc_id;
1731 monitor_instance->pri = pri;
1732 monitor_instance->core_id = core_id;
1733 strcpy(monitor_instance->name, device_name);
1735 pri->calls->module_ref();
1737 ao2_link(sig_pri_cc_monitors, monitor_instance);
1738 return monitor_instance;
1740 #endif /* defined(HAVE_PRI_CCSS) */
1742 #if defined(HAVE_PRI_CCSS)
1745 * \brief Announce to the CC core that protocol CC monitor is available for this call.
1748 * \param pri sig_pri PRI control structure.
1749 * \param chanpos Channel position in the span.
1750 * \param cc_id CC record ID.
1751 * \param service CCBS/CCNR indication.
1753 * \note Assumes the pri->lock is already obtained.
1754 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1755 * \note Assumes the sig_pri_lock_owner(pri, chanpos) is already obtained.
1757 * \retval 0 on success.
1758 * \retval -1 on error.
1760 static int sig_pri_cc_available(struct sig_pri_pri *pri, int chanpos, long cc_id, enum ast_cc_service_type service)
1762 struct sig_pri_chan *pvt;
1763 struct ast_cc_config_params *cc_params;
1764 struct sig_pri_cc_monitor_instance *monitor;
1765 enum ast_cc_monitor_policies monitor_policy;
1768 char device_name[AST_CHANNEL_NAME];
1769 char dialstring[AST_CHANNEL_NAME];
1771 pvt = pri->pvts[chanpos];
1773 core_id = ast_cc_get_current_core_id(pvt->owner);
1774 if (core_id == -1) {
1778 cc_params = ast_channel_get_cc_config_params(pvt->owner);
1784 monitor_policy = ast_get_cc_monitor_policy(cc_params);
1785 switch (monitor_policy) {
1786 case AST_CC_MONITOR_NEVER:
1787 /* CCSS is not enabled. */
1789 case AST_CC_MONITOR_NATIVE:
1790 case AST_CC_MONITOR_ALWAYS:
1792 * If it is AST_CC_MONITOR_ALWAYS and native fails we will attempt the fallback
1793 * later in the call to sig_pri_cc_generic_check().
1795 ast_channel_get_device_name(pvt->owner, device_name, sizeof(device_name));
1796 sig_pri_make_cc_dialstring(pvt, dialstring, sizeof(dialstring));
1797 monitor = sig_pri_cc_monitor_instance_init(core_id, pri, cc_id, device_name);
1801 res = ast_queue_cc_frame(pvt->owner, sig_pri_cc_type_name, dialstring, service,
1804 monitor->cc_id = -1;
1805 ao2_unlink(sig_pri_cc_monitors, monitor);
1806 ao2_ref(monitor, -1);
1809 case AST_CC_MONITOR_GENERIC:
1810 ast_queue_cc_frame(pvt->owner, AST_CC_GENERIC_MONITOR_TYPE,
1811 sig_pri_get_orig_dialstring(pvt), service, NULL);
1812 /* Say it failed to force caller to cancel native CC. */
1817 #endif /* defined(HAVE_PRI_CCSS) */
1821 * \brief Check if generic CC monitor is needed and request it.
1824 * \param pri sig_pri PRI control structure.
1825 * \param chanpos Channel position in the span.
1826 * \param service CCBS/CCNR indication.
1828 * \note Assumes the pri->lock is already obtained.
1829 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1833 static void sig_pri_cc_generic_check(struct sig_pri_pri *pri, int chanpos, enum ast_cc_service_type service)
1835 struct ast_channel *owner;
1836 struct ast_cc_config_params *cc_params;
1837 #if defined(HAVE_PRI_CCSS)
1838 struct ast_cc_monitor *monitor;
1839 char device_name[AST_CHANNEL_NAME];
1840 #endif /* defined(HAVE_PRI_CCSS) */
1841 enum ast_cc_monitor_policies monitor_policy;
1844 if (!pri->pvts[chanpos]->outgoing) {
1845 /* This is not an outgoing call so it cannot be CC monitor. */
1849 sig_pri_lock_owner(pri, chanpos);
1850 owner = pri->pvts[chanpos]->owner;
1854 core_id = ast_cc_get_current_core_id(owner);
1855 if (core_id == -1) {
1856 /* No CC core setup */
1860 cc_params = ast_channel_get_cc_config_params(owner);
1862 /* Could not get CC config parameters. */
1866 #if defined(HAVE_PRI_CCSS)
1867 ast_channel_get_device_name(owner, device_name, sizeof(device_name));
1868 monitor = ast_cc_get_monitor_by_recall_core_id(core_id, device_name);
1870 /* CC monitor is already present so no need for generic CC. */
1871 ao2_ref(monitor, -1);
1874 #endif /* defined(HAVE_PRI_CCSS) */
1876 monitor_policy = ast_get_cc_monitor_policy(cc_params);
1877 switch (monitor_policy) {
1878 case AST_CC_MONITOR_NEVER:
1879 /* CCSS is not enabled. */
1881 case AST_CC_MONITOR_NATIVE:
1882 if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
1883 /* Request generic CC monitor. */
1884 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
1885 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
1888 case AST_CC_MONITOR_ALWAYS:
1889 if (pri->sig == SIG_BRI_PTMP && pri->nodetype != PRI_NETWORK) {
1891 * Cannot monitor PTMP TE side since this is not defined.
1892 * We are playing the roll of a phone in this case and
1893 * a phone cannot monitor a party over the network without
1899 * We are either falling back or this is a PTMP NT span.
1900 * Request generic CC monitor.
1902 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
1903 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
1905 case AST_CC_MONITOR_GENERIC:
1906 if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
1907 /* Request generic CC monitor. */
1908 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
1909 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
1915 ast_channel_unlock(owner);
1918 #if defined(HAVE_PRI_CCSS)
1921 * \brief The CC link canceled the CC instance.
1924 * \param pri sig_pri PRI control structure.
1925 * \param cc_id CC record ID.
1926 * \param is_agent TRUE if the cc_id is for an agent.
1930 static void sig_pri_cc_link_canceled(struct sig_pri_pri *pri, long cc_id, int is_agent)
1933 struct ast_cc_agent *agent;
1935 agent = sig_pri_find_cc_agent_by_cc_id(pri, cc_id);
1939 ast_cc_failed(agent->core_id, "%s agent got canceled by link",
1940 sig_pri_cc_type_name);
1943 struct sig_pri_cc_monitor_instance *monitor;
1945 monitor = sig_pri_find_cc_monitor_by_cc_id(pri, cc_id);
1949 monitor->cc_id = -1;
1950 ast_cc_monitor_failed(monitor->core_id, monitor->name,
1951 "%s monitor got canceled by link", sig_pri_cc_type_name);
1952 ao2_ref(monitor, -1);
1955 #endif /* defined(HAVE_PRI_CCSS) */
1959 * \brief TRUE if PRI event came in on a CIS call.
1962 * \param channel PRI encoded span/channel
1964 * \retval non-zero if CIS call.
1966 static int sig_pri_is_cis_call(int channel)
1968 return channel != -1 && (channel & PRI_CIS_CALL);
1973 * \brief Handle the CIS associated PRI subcommand events.
1976 * \param pri sig_pri PRI control structure.
1977 * \param event_id PRI event id
1978 * \param subcmds Subcommands to process if any. (Could be NULL).
1979 * \param call_rsp libpri opaque call structure to send any responses toward.
1980 * Could be NULL either because it is not available or the call is for the
1981 * dummy call reference. However, this should not be NULL in the cases that
1982 * need to use the pointer to send a response message back.
1984 * \note Assumes the pri->lock is already obtained.
1988 static void sig_pri_handle_cis_subcmds(struct sig_pri_pri *pri, int event_id,
1989 const struct pri_subcommands *subcmds, q931_call *call_rsp)
1992 #if defined(HAVE_PRI_CCSS)
1993 struct ast_cc_agent *agent;
1994 struct sig_pri_cc_agent_prv *agent_prv;
1995 struct sig_pri_cc_monitor_instance *monitor;
1996 #endif /* defined(HAVE_PRI_CCSS) */
2001 for (index = 0; index < subcmds->counter_subcmd; ++index) {
2002 const struct pri_subcommand *subcmd = &subcmds->subcmd[index];
2004 switch (subcmd->cmd) {
2005 #if defined(STATUS_REQUEST_PLACE_HOLDER)
2006 case PRI_SUBCMD_STATUS_REQ:
2007 case PRI_SUBCMD_STATUS_REQ_RSP:
2008 /* Ignore for now. */
2010 #endif /* defined(STATUS_REQUEST_PLACE_HOLDER) */
2011 #if defined(HAVE_PRI_CCSS)
2012 case PRI_SUBCMD_CC_REQ:
2013 agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_request.cc_id);
2015 pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
2018 if (!ast_cc_request_is_within_limits()) {
2019 if (pri_cc_req_rsp(pri->pri, subcmd->u.cc_request.cc_id,
2020 5/* queue_full */)) {
2021 pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
2023 ast_cc_failed(agent->core_id, "%s agent system CC queue full",
2024 sig_pri_cc_type_name);
2028 agent_prv = agent->private_data;
2029 agent_prv->cc_request_response_pending = 1;
2030 if (ast_cc_agent_accept_request(agent->core_id,
2031 "%s caller accepted CC offer.", sig_pri_cc_type_name)) {
2032 agent_prv->cc_request_response_pending = 0;
2033 if (pri_cc_req_rsp(pri->pri, subcmd->u.cc_request.cc_id,
2034 2/* short_term_denial */)) {
2035 pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
2037 ast_cc_failed(agent->core_id, "%s agent CC core request accept failed",
2038 sig_pri_cc_type_name);
2042 #endif /* defined(HAVE_PRI_CCSS) */
2043 #if defined(HAVE_PRI_CCSS)
2044 case PRI_SUBCMD_CC_REQ_RSP:
2045 monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
2046 subcmd->u.cc_request_rsp.cc_id);
2048 pri_cc_cancel(pri->pri, subcmd->u.cc_request_rsp.cc_id);
2051 switch (subcmd->u.cc_request_rsp.status) {
2052 case 0:/* success */
2053 ast_cc_monitor_request_acked(monitor->core_id,
2054 "%s far end accepted CC request", sig_pri_cc_type_name);
2056 case 1:/* timeout */
2057 ast_verb(2, "core_id:%d %s CC request timeout\n", monitor->core_id,
2058 sig_pri_cc_type_name);
2059 ast_cc_monitor_failed(monitor->core_id, monitor->name,
2060 "%s CC request timeout", sig_pri_cc_type_name);
2063 ast_verb(2, "core_id:%d %s CC request error: %s\n", monitor->core_id,
2064 sig_pri_cc_type_name,
2065 pri_facility_error2str(subcmd->u.cc_request_rsp.fail_code));
2066 ast_cc_monitor_failed(monitor->core_id, monitor->name,
2067 "%s CC request error", sig_pri_cc_type_name);
2070 ast_verb(2, "core_id:%d %s CC request reject: %s\n", monitor->core_id,
2071 sig_pri_cc_type_name,
2072 pri_facility_reject2str(subcmd->u.cc_request_rsp.fail_code));
2073 ast_cc_monitor_failed(monitor->core_id, monitor->name,
2074 "%s CC request reject", sig_pri_cc_type_name);
2077 ast_verb(2, "core_id:%d %s CC request unknown status %d\n",
2078 monitor->core_id, sig_pri_cc_type_name,
2079 subcmd->u.cc_request_rsp.status);
2080 ast_cc_monitor_failed(monitor->core_id, monitor->name,
2081 "%s CC request unknown status", sig_pri_cc_type_name);
2084 ao2_ref(monitor, -1);
2086 #endif /* defined(HAVE_PRI_CCSS) */
2087 #if defined(HAVE_PRI_CCSS)
2088 case PRI_SUBCMD_CC_REMOTE_USER_FREE:
2089 monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
2090 subcmd->u.cc_remote_user_free.cc_id);
2092 pri_cc_cancel(pri->pri, subcmd->u.cc_remote_user_free.cc_id);
2095 ast_cc_monitor_callee_available(monitor->core_id,
2096 "%s callee has become available", sig_pri_cc_type_name);
2097 ao2_ref(monitor, -1);
2099 #endif /* defined(HAVE_PRI_CCSS) */
2100 #if defined(HAVE_PRI_CCSS)
2101 case PRI_SUBCMD_CC_B_FREE:
2102 monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
2103 subcmd->u.cc_b_free.cc_id);
2105 pri_cc_cancel(pri->pri, subcmd->u.cc_b_free.cc_id);
2108 ast_cc_monitor_party_b_free(monitor->core_id);
2109 ao2_ref(monitor, -1);
2111 #endif /* defined(HAVE_PRI_CCSS) */
2112 #if defined(HAVE_PRI_CCSS)
2113 case PRI_SUBCMD_CC_STATUS_REQ:
2114 monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
2115 subcmd->u.cc_status_req.cc_id);
2117 pri_cc_cancel(pri->pri, subcmd->u.cc_status_req.cc_id);
2120 ast_cc_monitor_status_request(monitor->core_id);
2121 ao2_ref(monitor, -1);
2123 #endif /* defined(HAVE_PRI_CCSS) */
2124 #if defined(HAVE_PRI_CCSS)
2125 case PRI_SUBCMD_CC_STATUS_REQ_RSP:
2126 agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_status_req_rsp.cc_id);
2128 pri_cc_cancel(pri->pri, subcmd->u.cc_status_req_rsp.cc_id);
2131 ast_cc_agent_status_response(agent->core_id,
2132 subcmd->u.cc_status_req_rsp.status ? AST_DEVICE_INUSE
2133 : AST_DEVICE_NOT_INUSE);
2136 #endif /* defined(HAVE_PRI_CCSS) */
2137 #if defined(HAVE_PRI_CCSS)
2138 case PRI_SUBCMD_CC_STATUS:
2139 agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_status.cc_id);
2141 pri_cc_cancel(pri->pri, subcmd->u.cc_status.cc_id);
2144 if (subcmd->u.cc_status.status) {
2145 ast_cc_agent_caller_busy(agent->core_id, "%s agent caller is busy",
2146 sig_pri_cc_type_name);
2148 ast_cc_agent_caller_available(agent->core_id,
2149 "%s agent caller is available", sig_pri_cc_type_name);
2153 #endif /* defined(HAVE_PRI_CCSS) */
2154 #if defined(HAVE_PRI_CCSS)
2155 case PRI_SUBCMD_CC_CANCEL:
2156 sig_pri_cc_link_canceled(pri, subcmd->u.cc_cancel.cc_id,
2157 subcmd->u.cc_cancel.is_agent);
2159 #endif /* defined(HAVE_PRI_CCSS) */
2160 #if defined(HAVE_PRI_CCSS)
2161 case PRI_SUBCMD_CC_STOP_ALERTING:
2162 monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
2163 subcmd->u.cc_stop_alerting.cc_id);
2165 pri_cc_cancel(pri->pri, subcmd->u.cc_stop_alerting.cc_id);
2168 ast_cc_monitor_stop_ringing(monitor->core_id);
2169 ao2_ref(monitor, -1);
2171 #endif /* defined(HAVE_PRI_CCSS) */
2174 "Unknown CIS subcommand(%d) in %s event on span %d.\n",
2175 subcmd->cmd, pri_event2str(event_id), pri->span);
2183 * \brief Handle the call associated PRI subcommand events.
2186 * \param pri sig_pri PRI control structure.
2187 * \param chanpos Channel position in the span.
2188 * \param event_id PRI event id
2189 * \param channel PRI encoded span/channel
2190 * \param subcmds Subcommands to process if any. (Could be NULL).
2191 * \param call_rsp libpri opaque call structure to send any responses toward.
2192 * Could be NULL either because it is not available or the call is for the
2193 * dummy call reference. However, this should not be NULL in the cases that
2194 * need to use the pointer to send a response message back.
2196 * \note Assumes the pri->lock is already obtained.
2197 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
2201 static void sig_pri_handle_subcmds(struct sig_pri_pri *pri, int chanpos, int event_id,
2202 int channel, const struct pri_subcommands *subcmds, q931_call *call_rsp)
2205 struct ast_channel *owner;
2206 struct ast_party_redirecting ast_redirecting;
2211 for (index = 0; index < subcmds->counter_subcmd; ++index) {
2212 const struct pri_subcommand *subcmd = &subcmds->subcmd[index];
2214 switch (subcmd->cmd) {
2215 case PRI_SUBCMD_CONNECTED_LINE:
2216 sig_pri_lock_owner(pri, chanpos);
2217 owner = pri->pvts[chanpos]->owner;
2219 struct ast_party_connected_line ast_connected;
2220 int caller_id_update;
2222 /* Extract the connected line information */
2223 ast_party_connected_line_init(&ast_connected);
2224 sig_pri_party_id_convert(&ast_connected.id, &subcmd->u.connected_line.id,
2227 caller_id_update = 0;
2228 if (ast_connected.id.name) {
2229 /* Save name for Caller-ID update */
2230 ast_copy_string(pri->pvts[chanpos]->cid_name,
2231 ast_connected.id.name, sizeof(pri->pvts[chanpos]->cid_name));
2232 caller_id_update = 1;
2234 if (ast_connected.id.number) {
2235 /* Save number for Caller-ID update */
2236 ast_copy_string(pri->pvts[chanpos]->cid_num, ast_connected.id.number,
2237 sizeof(pri->pvts[chanpos]->cid_num));
2238 pri->pvts[chanpos]->cid_ton = ast_connected.id.number_type;
2239 caller_id_update = 1;
2241 ast_connected.id.number = ast_strdup("");
2243 ast_connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
2245 pri->pvts[chanpos]->cid_subaddr[0] = '\0';
2246 #if defined(HAVE_PRI_SUBADDR)
2247 if (ast_connected.id.subaddress.valid) {
2248 ast_party_subaddress_set(&owner->cid.subaddress,
2249 &ast_connected.id.subaddress);
2250 if (ast_connected.id.subaddress.str) {
2251 ast_copy_string(pri->pvts[chanpos]->cid_subaddr,
2252 ast_connected.id.subaddress.str,
2253 sizeof(pri->pvts[chanpos]->cid_subaddr));
2256 #endif /* defined(HAVE_PRI_SUBADDR) */
2257 if (caller_id_update) {
2258 pri->pvts[chanpos]->callingpres =
2259 ast_connected.id.number_presentation;
2260 sig_pri_set_caller_id(pri->pvts[chanpos]);
2261 ast_set_callerid(owner, S_OR(ast_connected.id.number, NULL),
2262 S_OR(ast_connected.id.name, NULL),
2263 S_OR(ast_connected.id.number, NULL));
2266 /* Update the connected line information on the other channel */
2267 if (event_id != PRI_EVENT_RING) {
2268 /* This connected_line update was not from a SETUP message. */
2269 ast_channel_queue_connected_line_update(owner, &ast_connected);
2272 ast_party_connected_line_free(&ast_connected);
2273 ast_channel_unlock(owner);
2276 case PRI_SUBCMD_REDIRECTING:
2277 sig_pri_lock_owner(pri, chanpos);
2278 owner = pri->pvts[chanpos]->owner;
2280 sig_pri_redirecting_convert(&ast_redirecting, &subcmd->u.redirecting,
2281 &owner->redirecting, pri);
2283 /*! \todo XXX Original called data can be put in a channel data store that is inherited. */
2285 ast_channel_set_redirecting(owner, &ast_redirecting);
2286 if (event_id != PRI_EVENT_RING) {
2287 /* This redirection was not from a SETUP message. */
2288 ast_channel_queue_redirecting_update(owner, &ast_redirecting);
2290 ast_party_redirecting_free(&ast_redirecting);
2292 ast_channel_unlock(owner);
2295 #if defined(HAVE_PRI_CALL_REROUTING)
2296 case PRI_SUBCMD_REROUTING:
2297 sig_pri_lock_owner(pri, chanpos);
2298 owner = pri->pvts[chanpos]->owner;
2300 struct pri_party_redirecting pri_deflection;
2303 ast_channel_unlock(owner);
2304 ast_log(LOG_WARNING,
2305 "CallRerouting/CallDeflection to '%s' without call!\n",
2306 subcmd->u.rerouting.deflection.to.number.str);
2310 pri_deflection = subcmd->u.rerouting.deflection;
2312 ast_string_field_set(owner, call_forward, pri_deflection.to.number.str);
2314 /* Adjust the deflecting to number based upon the subscription option. */
2315 switch (subcmd->u.rerouting.subscription_option) {
2316 case 0: /* noNotification */
2317 case 1: /* notificationWithoutDivertedToNr */
2318 /* Delete the number because the far end is not supposed to see it. */
2319 pri_deflection.to.number.presentation =
2320 PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
2321 pri_deflection.to.number.plan =
2322 (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
2323 pri_deflection.to.number.str[0] = '\0';
2325 case 2: /* notificationWithDivertedToNr */
2327 case 3: /* notApplicable */
2331 sig_pri_redirecting_convert(&ast_redirecting, &pri_deflection,
2332 &owner->redirecting, pri);
2333 ast_channel_set_redirecting(owner, &ast_redirecting);
2334 ast_party_redirecting_free(&ast_redirecting);
2337 * Send back positive ACK to CallRerouting/CallDeflection.
2339 * Note: This call will be hungup by the dial application when
2340 * it processes the call_forward string set above.
2342 pri_rerouting_rsp(pri->pri, call_rsp, subcmd->u.rerouting.invoke_id,
2343 PRI_REROUTING_RSP_OK_CLEAR);
2345 /* This line is BUSY to further attempts by this dialing attempt. */
2346 ast_queue_control(owner, AST_CONTROL_BUSY);
2348 ast_channel_unlock(owner);
2351 #endif /* defined(HAVE_PRI_CALL_REROUTING) */
2352 #if defined(HAVE_PRI_CCSS)
2353 case PRI_SUBCMD_CC_AVAILABLE:
2354 sig_pri_lock_owner(pri, chanpos);
2355 owner = pri->pvts[chanpos]->owner;
2357 enum ast_cc_service_type service;
2360 case PRI_EVENT_RINGING:
2361 service = AST_CC_CCNR;
2363 case PRI_EVENT_HANGUP_REQ:
2364 /* We will assume that the cause was busy/congestion. */
2365 service = AST_CC_CCBS;
2368 service = AST_CC_NONE;
2371 if (service == AST_CC_NONE
2372 || sig_pri_cc_available(pri, chanpos, subcmd->u.cc_available.cc_id,
2374 pri_cc_cancel(pri->pri, subcmd->u.cc_available.cc_id);
2376 ast_channel_unlock(owner);
2378 /* No asterisk channel. */
2379 pri_cc_cancel(pri->pri, subcmd->u.cc_available.cc_id);
2382 #endif /* defined(HAVE_PRI_CCSS) */
2383 #if defined(HAVE_PRI_CCSS)
2384 case PRI_SUBCMD_CC_CALL:
2385 sig_pri_lock_owner(pri, chanpos);
2386 owner = pri->pvts[chanpos]->owner;
2388 struct ast_cc_agent *agent;
2390 agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_call.cc_id);
2392 ast_setup_cc_recall_datastore(owner, agent->core_id);
2393 ast_cc_agent_set_interfaces_chanvar(owner);
2394 ast_cc_agent_recalling(agent->core_id,
2395 "%s caller is attempting recall", sig_pri_cc_type_name);
2399 ast_channel_unlock(owner);
2402 #endif /* defined(HAVE_PRI_CCSS) */
2403 #if defined(HAVE_PRI_CCSS)
2404 case PRI_SUBCMD_CC_CANCEL:
2405 sig_pri_cc_link_canceled(pri, subcmd->u.cc_cancel.cc_id,
2406 subcmd->u.cc_cancel.is_agent);
2408 #endif /* defined(HAVE_PRI_CCSS) */
2411 "Unknown call subcommand(%d) in %s event on channel %d/%d on span %d.\n",
2412 subcmd->cmd, pri_event2str(event_id), PRI_SPAN(channel),
2413 PRI_CHANNEL(channel), pri->span);
2419 #if defined(HAVE_PRI_CALL_HOLD)
2422 * \brief Attempt to transfer the active call to the held call.
2425 * \param pri sig_pri PRI control structure.
2426 * \param active_call Active call to transfer.
2427 * \param held_call Held call to transfer.
2429 * \note Assumes the pri->lock is already obtained.
2431 * \retval 0 on success.
2432 * \retval -1 on error.
2434 static int sig_pri_attempt_transfer(struct sig_pri_pri *pri, q931_call *active_call, q931_call *held_call)
2439 struct ast_channel *active_ast;
2440 struct ast_channel *held_ast;
2441 struct ast_channel *bridged;
2443 active_chanpos = pri_find_pri_call(pri, active_call);
2444 held_chanpos = pri_find_pri_call(pri, held_call);
2445 if (active_chanpos < 0 || held_chanpos < 0) {
2449 sig_pri_lock_private(pri->pvts[active_chanpos]);
2450 sig_pri_lock_private(pri->pvts[held_chanpos]);
2451 sig_pri_lock_owner(pri, active_chanpos);
2452 sig_pri_lock_owner(pri, held_chanpos);
2454 active_ast = pri->pvts[active_chanpos]->owner;
2455 held_ast = pri->pvts[held_chanpos]->owner;
2456 if (!active_ast || !held_ast) {
2458 ast_channel_unlock(active_ast);
2461 ast_channel_unlock(held_ast);
2463 sig_pri_unlock_private(pri->pvts[active_chanpos]);
2464 sig_pri_unlock_private(pri->pvts[held_chanpos]);
2468 bridged = ast_bridged_channel(held_ast);
2470 ast_queue_control(held_ast, AST_CONTROL_UNHOLD);
2472 ast_verb(3, "TRANSFERRING %s to %s\n", held_ast->name, active_ast->name);
2473 retval = ast_channel_masquerade(active_ast, bridged);
2476 * Could not transfer. Held channel is not bridged anymore.
2477 * Held party probably got tired of waiting and hung up.
2482 ast_channel_unlock(active_ast);
2483 ast_channel_unlock(held_ast);
2484 sig_pri_unlock_private(pri->pvts[active_chanpos]);
2485 sig_pri_unlock_private(pri->pvts[held_chanpos]);
2489 #endif /* defined(HAVE_PRI_CALL_HOLD) */
2491 #if defined(HAVE_PRI_CALL_HOLD)
2494 * \brief Handle the hold event from libpri.
2497 * \param pri sig_pri PRI control structure.
2498 * \param ev Hold event received.
2500 * \note Assumes the pri->lock is already obtained.
2502 * \retval 0 on success.
2503 * \retval -1 on error.
2505 static int sig_pri_handle_hold(struct sig_pri_pri *pri, pri_event *ev)
2510 struct ast_channel *bridged;
2511 struct ast_channel *owner;
2513 chanpos_old = pri_find_principle(pri, ev->hold.channel, ev->hold.call);
2514 if (chanpos_old < 0) {
2515 ast_log(LOG_WARNING,
2516 "Received HOLD on unconfigured channel %d/%d span %d\n",
2517 PRI_SPAN(ev->hold.channel), PRI_CHANNEL(ev->hold.channel), pri->span);
2520 if (pri->pvts[chanpos_old]->no_b_channel) {
2521 /* Call is already on hold or is call waiting call. */
2525 sig_pri_lock_private(pri->pvts[chanpos_old]);
2526 sig_pri_lock_owner(pri, chanpos_old);
2527 owner = pri->pvts[chanpos_old]->owner;
2530 goto done_with_private;
2532 bridged = ast_bridged_channel(owner);
2534 /* Cannot hold a call that is not bridged. */
2536 goto done_with_owner;
2538 chanpos_new = pri_find_empty_nobch(pri);
2539 if (chanpos_new < 0) {
2540 /* No hold channel available. */
2542 goto done_with_owner;
2544 sig_pri_handle_subcmds(pri, chanpos_old, ev->e, ev->hold.channel, ev->hold.subcmds,
2546 chanpos_new = pri_fixup_principle(pri, chanpos_new, ev->hold.call);
2547 if (chanpos_new < 0) {
2548 /* Should never happen. */
2551 struct ast_frame f = { AST_FRAME_CONTROL, };
2554 * Things are in an odd state here so we cannot use pri_queue_control().
2555 * However, we already have the owner lock so we can simply queue the frame.
2557 f.subclass.integer = AST_CONTROL_HOLD;
2558 ast_queue_frame(owner, &f);
2560 sig_pri_span_devstate_changed(pri);
2565 ast_channel_unlock(owner);
2567 sig_pri_unlock_private(pri->pvts[chanpos_old]);
2571 #endif /* defined(HAVE_PRI_CALL_HOLD) */
2573 #if defined(HAVE_PRI_CALL_HOLD)
2576 * \brief Handle the retrieve event from libpri.
2579 * \param pri sig_pri PRI control structure.
2580 * \param ev Retrieve event received.
2582 * \note Assumes the pri->lock is already obtained.
2586 static void sig_pri_handle_retrieve(struct sig_pri_pri *pri, pri_event *ev)
2590 if (!(ev->retrieve.channel & PRI_HELD_CALL)
2591 || pri_find_principle(pri, ev->retrieve.channel, ev->retrieve.call) < 0) {
2592 /* The call is not currently held. */
2593 pri_retrieve_rej(pri->pri, ev->retrieve.call,
2594 PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED);
2597 if (PRI_CHANNEL(ev->retrieve.channel) == 0xFF) {
2598 chanpos = pri_find_empty_chan(pri, 1);
2600 chanpos = pri_find_principle(pri,
2601 ev->retrieve.channel & ~PRI_HELD_CALL, ev->retrieve.call);
2602 if (ev->retrieve.flexible
2603 && (chanpos < 0 || pri->pvts[chanpos]->owner)) {
2605 * Channel selection is flexible and the requested channel
2606 * is bad or already in use. Pick another channel.
2608 chanpos = pri_find_empty_chan(pri, 1);
2612 pri_retrieve_rej(pri->pri, ev->retrieve.call,
2613 ev->retrieve.flexible ? PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION
2614 : PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
2617 chanpos = pri_fixup_principle(pri, chanpos, ev->retrieve.call);
2619 /* Channel is already in use. */
2620 pri_retrieve_rej(pri->pri, ev->retrieve.call,
2621 PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
2624 sig_pri_lock_private(pri->pvts[chanpos]);
2625 sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->retrieve.channel,
2626 ev->retrieve.subcmds, ev->retrieve.call);
2627 pri_queue_control(pri, chanpos, AST_CONTROL_UNHOLD);
2628 sig_pri_unlock_private(pri->pvts[chanpos]);
2629 sig_pri_span_devstate_changed(pri);
2630 pri_retrieve_ack(pri->pri, ev->retrieve.call,
2631 PVT_TO_CHANNEL(pri->pvts[chanpos]));
2633 #endif /* defined(HAVE_PRI_CALL_HOLD) */
2635 static void *pri_dchannel(void *vpri)
2637 struct sig_pri_pri *pri = vpri;
2639 struct pollfd fds[SIG_PRI_NUM_DCHANS];
2643 struct ast_channel *c;
2644 struct timeval tv, lowest, *next;
2652 char plancallingnum[AST_MAX_EXTENSION];
2653 char plancallingani[AST_MAX_EXTENSION];
2654 char calledtonstr[10];
2655 struct timeval lastidle = { 0, 0 };
2657 struct ast_channel *idle;
2663 gettimeofday(&lastidle, NULL);
2664 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2666 if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
2667 /* Need to do idle dialing, check to be sure though */
2668 cc = strchr(pri->idleext, '@');
2672 ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
2674 /* Extensions may not be loaded yet */
2675 if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
2676 ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
2681 ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
2684 for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
2685 if (!pri->dchans[i])
2687 fds[i].fd = pri->fds[i];
2688 fds[i].events = POLLIN | POLLPRI;
2693 ast_mutex_lock(&pri->lock);
2694 if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->sig != SIG_BRI_PTMP) && (pri->resetinterval > 0)) {
2695 if (pri->resetting && pri_is_up(pri)) {
2696 if (pri->resetpos < 0)
2697 pri_check_restart(pri);
2699 if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) {
2705 /* Look for any idle channels if appropriate */
2706 if (doidling && pri_is_up(pri)) {
2710 for (x = pri->numchans; x >= 0; x--) {
2712 && !pri->pvts[x]->owner
2713 && !pri->pvts[x]->call
2714 && !pri->pvts[x]->no_b_channel) {
2715 if (haveidles < pri->minunused) {
2717 } else if (!pri->pvts[x]->resetting) {
2721 } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
2724 if (nextidle > -1) {
2725 if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
2726 /* Don't create a new idle call more than once per second */
2727 snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
2728 idle = sig_pri_request(pri->pvts[nextidle], AST_FORMAT_ULAW, NULL, 0);
2730 pri->pvts[nextidle]->isidlecall = 1;
2731 if (ast_pthread_create_background(&p, NULL, do_idle_thread, pri->pvts[nextidle])) {
2732 ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
2736 ast_log(LOG_WARNING, "Unable to request channel 'DAHDI/%s' for idle call\n", idlen);
2737 gettimeofday(&lastidle, NULL);
2739 } else if ((haveidles < pri->minunused) &&
2740 (activeidles > pri->minidle)) {
2741 /* Mark something for hangup if there is something
2742 that can be hungup */
2743 for (x = pri->numchans; x >= 0; x--) {
2744 /* find a candidate channel */
2745 if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
2746 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2748 /* Stop if we have enough idle channels or
2749 can't spare any more active idle ones */
2750 if ((haveidles >= pri->minunused) ||
2751 (activeidles <= pri->minidle))
2757 /* Start with reasonable max */
2758 lowest = ast_tv(60, 0);
2759 for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
2760 /* Find lowest available d-channel */
2761 if (!pri->dchans[i])
2763 if ((next = pri_schedule_next(pri->dchans[i]))) {
2764 /* We need relative time here */
2765 tv = ast_tvsub(*next, ast_tvnow());
2766 if (tv.tv_sec < 0) {
2769 if (doidling || pri->resetting) {
2770 if (tv.tv_sec > 1) {
2774 if (tv.tv_sec > 60) {
2778 } else if (doidling || pri->resetting) {
2779 /* Make sure we stop at least once per second if we're
2780 monitoring idle channels */
2783 /* Don't poll for more than 60 seconds */
2786 if (!i || ast_tvcmp(tv, lowest) < 0) {
2790 ast_mutex_unlock(&pri->lock);
2792 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
2793 pthread_testcancel();
2795 res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
2796 pthread_testcancel();
2797 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2799 ast_mutex_lock(&pri->lock);
2801 for (which = 0; which < SIG_PRI_NUM_DCHANS; which++) {
2802 if (!pri->dchans[which])
2804 /* Just a timeout, run the scheduler */
2805 e = pri_schedule_run(pri->dchans[which]);
2809 } else if (res > -1) {
2810 for (which = 0; which < SIG_PRI_NUM_DCHANS; which++) {
2811 if (!pri->dchans[which])
2813 if (fds[which].revents & POLLPRI) {
2814 sig_pri_handle_dchan_exception(pri, which);
2815 } else if (fds[which].revents & POLLIN) {
2816 e = pri_check_event(pri->dchans[which]);
2821 } else if (errno != EINTR)
2822 ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
2826 pri_dump_event(pri->dchans[which], e);
2828 if (e->e != PRI_EVENT_DCHAN_DOWN) {
2829 if (!(pri->dchanavail[which] & DCHAN_UP)) {
2830 ast_verb(2, "%s D-Channel on span %d up\n", pri_order(which), pri->span);
2832 pri->dchanavail[which] |= DCHAN_UP;
2834 if (pri->dchanavail[which] & DCHAN_UP) {
2835 ast_verb(2, "%s D-Channel on span %d down\n", pri_order(which), pri->span);
2837 pri->dchanavail[which] &= ~DCHAN_UP;
2840 if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which]))
2841 /* Must be an NFAS group that has the secondary dchan active */
2842 pri->pri = pri->dchans[which];
2845 case PRI_EVENT_DCHAN_UP:
2846 if (!pri->pri) pri_find_dchan(pri);
2848 /* Note presense of D-channel */
2849 time(&pri->lastreset);
2851 /* Restart in 5 seconds */
2852 if (pri->resetinterval > -1) {
2853 pri->lastreset -= pri->resetinterval;
2854 pri->lastreset += 5;
2857 /* Take the channels from inalarm condition */
2858 for (i = 0; i < pri->numchans; i++) {
2860 sig_pri_set_alarm(pri->pvts[i], 0);
2863 sig_pri_span_devstate_changed(pri);
2865 case PRI_EVENT_DCHAN_DOWN:
2866 pri_find_dchan(pri);
2867 if (!pri_is_up(pri)) {
2869 if (pri->sig == SIG_BRI_PTMP) {
2870 /* For PTMP connections with non persistent layer 2 we want
2871 * to *not* declare inalarm unless there actually is an alarm */
2874 /* Hangup active channels and put them in alarm mode */
2875 for (i = 0; i < pri->numchans; i++) {
2876 struct sig_pri_chan *p = pri->pvts[i];
2878 if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
2879 /* T309 is not enabled : hangup calls when alarm occurs */
2881 if (p->pri && p->pri->pri) {
2882 pri_hangup(p->pri->pri, p->call, -1);
2883 pri_destroycall(p->pri->pri, p->call);
2886 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
2889 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2891 sig_pri_set_alarm(p, 1);
2894 sig_pri_span_devstate_changed(pri);
2897 case PRI_EVENT_RESTART:
2898 if (e->restart.channel > -1) {
2899 chanpos = pri_find_principle(pri, e->restart.channel, NULL);
2901 ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
2902 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
2905 #if defined(HAVE_PRI_SERVICE_MESSAGES)
2908 why = pri->pvts[chanpos]->service_status;
2911 "span '%d' channel '%d' out-of-service (reason: %s), ignoring RESTART\n",
2912 pri->span, PRI_CHANNEL(e->restart.channel),
2913 (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
2916 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
2917 sig_pri_lock_private(pri->pvts[chanpos]);
2919 ast_verb(3, "B-channel %d/%d restarted on span %d\n",
2920 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
2921 if (pri->pvts[chanpos]->call) {
2922 pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
2923 pri->pvts[chanpos]->call = NULL;
2926 /* Force soft hangup if appropriate */
2927 if (pri->pvts[chanpos]->owner)
2928 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2929 sig_pri_unlock_private(pri->pvts[chanpos]);
2932 ast_verb(3, "Restart requested on entire span %d\n", pri->span);
2933 for (x = 0; x < pri->numchans; x++)
2935 sig_pri_lock_private(pri->pvts[x]);
2936 if (pri->pvts[x]->call) {
2937 pri_destroycall(pri->pri, pri->pvts[x]->call);
2938 pri->pvts[x]->call = NULL;
2940 if (pri->pvts[x]->owner)
2941 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2942 sig_pri_unlock_private(pri->pvts[x]);
2946 case PRI_EVENT_KEYPAD_DIGIT:
2947 if (sig_pri_is_cis_call(e->digit.channel)) {
2948 sig_pri_handle_cis_subcmds(pri, e->e, e->digit.subcmds,
2952 chanpos = pri_find_principle(pri, e->digit.channel, e->digit.call);
2954 ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
2955 PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
2957 chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
2959 sig_pri_lock_private(pri->pvts[chanpos]);
2960 sig_pri_handle_subcmds(pri, chanpos, e->e, e->digit.channel,
2961 e->digit.subcmds, e->digit.call);
2962 /* queue DTMF frame if the PBX for this call was already started (we're forwarding KEYPAD_DIGITs further on */
2963 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
2964 && pri->pvts[chanpos]->call == e->digit.call
2965 && pri->pvts[chanpos]->owner) {
2966 /* how to do that */
2967 int digitlen = strlen(e->digit.digits);
2970 for (i = 0; i < digitlen; i++) {
2971 struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = e->digit.digits[i], };
2973 pri_queue_frame(pri, chanpos, &f);
2976 sig_pri_unlock_private(pri->pvts[chanpos]);
2981 case PRI_EVENT_INFO_RECEIVED:
2982 if (sig_pri_is_cis_call(e->ring.channel)) {
2983 sig_pri_handle_cis_subcmds(pri, e->e, e->ring.subcmds,
2987 chanpos = pri_find_principle(pri, e->ring.channel, e->ring.call);
2989 ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
2990 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
2992 chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
2994 sig_pri_lock_private(pri->pvts[chanpos]);
2995 sig_pri_handle_subcmds(pri, chanpos, e->e, e->ring.channel,
2996 e->ring.subcmds, e->ring.call);
2997 /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */
2998 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
2999 && pri->pvts[chanpos]->call == e->ring.call
3000 && pri->pvts[chanpos]->owner) {
3001 /* how to do that */
3002 int digitlen = strlen(e->ring.callednum);
3005 for (i = 0; i < digitlen; i++) {
3006 struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = e->ring.callednum[i], };
3008 pri_queue_frame(pri, chanpos, &f);
3011 sig_pri_unlock_private(pri->pvts[chanpos]);
3015 #if defined(HAVE_PRI_SERVICE_MESSAGES)
3016 case PRI_EVENT_SERVICE:
3017 chanpos = pri_find_principle(pri, e->service.channel, NULL);
3019 ast_log(LOG_WARNING, "Received service change status %d on unconfigured channel %d/%d span %d\n",
3020 e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
3022 char db_chan_name[20];
3027 ch = pri->pvts[chanpos]->channel;
3028 snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->span, ch);
3029 why = &pri->pvts[chanpos]->service_status;
3030 switch (e->service.changestatus) {
3031 case 0: /* in-service */
3032 /* Far end wants to be in service now. */
3033 ast_db_del(db_chan_name, SRVST_DBKEY);
3034 *why &= ~SRVST_FAREND;
3036 snprintf(db_answer, sizeof(db_answer), "%s:%u",