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>
27 <support_level>core</support_level>
30 <managerEvent language="en_US" name="MCID">
31 <managerEventInstance class="EVENT_FLAG_CALL">
32 <synopsis>Published when a malicious call ID request arrives.</synopsis>
35 <parameter name="MCallerIDNumValid">
37 <parameter name="MCallerIDNum">
39 <parameter name="MCallerIDton">
41 <parameter name="MCallerIDNumPlan">
43 <parameter name="MCallerIDNumPres">
45 <parameter name="MCallerIDNameValid">
47 <parameter name="MCallerIDName">
49 <parameter name="MCallerIDNameCharSet">
51 <parameter name="MCallerIDNamePres">
53 <parameter name="MCallerIDSubaddr">
55 <parameter name="MCallerIDSubaddrType">
57 <parameter name="MCallerIDSubaddrOdd">
59 <parameter name="MCallerIDPres">
61 <parameter name="MConnectedIDNumValid">
63 <parameter name="MConnectedIDNum">
65 <parameter name="MConnectedIDton">
67 <parameter name="MConnectedIDNumPlan">
69 <parameter name="MConnectedIDNumPres">
71 <parameter name="MConnectedIDNameValid">
73 <parameter name="MConnectedIDName">
75 <parameter name="MConnectedIDNameCharSet">
77 <parameter name="MConnectedIDNamePres">
79 <parameter name="MConnectedIDSubaddr">
81 <parameter name="MConnectedIDSubaddrType">
83 <parameter name="MConnectedIDSubaddrOdd">
85 <parameter name="MConnectedIDPres">
88 </managerEventInstance>
100 #include "asterisk/utils.h"
101 #include "asterisk/options.h"
102 #include "asterisk/pbx.h"
103 #include "asterisk/app.h"
104 #include "asterisk/file.h"
105 #include "asterisk/callerid.h"
106 #include "asterisk/say.h"
107 #include "asterisk/manager.h"
108 #include "asterisk/astdb.h"
109 #include "asterisk/causes.h"
110 #include "asterisk/musiconhold.h"
111 #include "asterisk/cli.h"
112 #include "asterisk/transcap.h"
113 #include "asterisk/features.h"
114 #include "asterisk/aoc.h"
115 #include "asterisk/bridge.h"
116 #include "asterisk/stasis_channels.h"
119 #ifndef PRI_EVENT_FACILITY
120 #error "Upgrade your libpri"
127 /* define this to send PRI user-user information elements */
128 #undef SUPPORT_USERUSER
131 * Define to make always pick a channel if allowed. Useful for
132 * testing channel shifting.
134 //#define ALWAYS_PICK_CHANNEL 1
137 * Define to force a RESTART on a channel that returns a cause
138 * code of PRI_CAUSE_REQUESTED_CHAN_UNAVAIL(44). If the cause
139 * is because of a stuck channel on the peer and the channel is
140 * always the next channel we pick for an outgoing call then
143 #define FORCE_RESTART_UNAVAIL_CHANS 1
145 #if defined(HAVE_PRI_CCSS)
146 struct sig_pri_cc_agent_prv {
147 /*! Asterisk span D channel control structure. */
148 struct sig_pri_span *pri;
149 /*! CC id value to use with libpri. -1 if invalid. */
151 /*! TRUE if CC has been requested and we are waiting for the response. */
152 unsigned char cc_request_response_pending;
155 struct sig_pri_cc_monitor_instance {
156 /*! \brief Asterisk span D channel control structure. */
157 struct sig_pri_span *pri;
158 /*! CC id value to use with libpri. (-1 if already canceled). */
160 /*! CC core id value. */
162 /*! Device name(Channel name less sequence number) */
166 /*! Upper level agent/monitor type name. */
167 static const char *sig_pri_cc_type_name;
168 /*! Container of sig_pri monitor instances. */
169 static struct ao2_container *sig_pri_cc_monitors;
170 #endif /* defined(HAVE_PRI_CCSS) */
172 static int pri_matchdigittimeout = 3000;
174 static int pri_gendigittimeout = 8000;
176 #define DCHAN_NOTINALARM (1 << 0)
177 #define DCHAN_UP (1 << 1)
179 /* Defines to help decode the encoded event channel id. */
180 #define PRI_CHANNEL(p) ((p) & 0xff)
181 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
182 #define PRI_EXPLICIT (1 << 16)
183 #define PRI_CIS_CALL (1 << 17) /* Call is using the D channel only. */
184 #define PRI_HELD_CALL (1 << 18)
187 #define DCHAN_AVAILABLE (DCHAN_NOTINALARM | DCHAN_UP)
189 static int pri_active_dchan_index(struct sig_pri_span *pri);
191 static const char *sig_pri_call_level2str(enum sig_pri_call_level level)
194 case SIG_PRI_CALL_LEVEL_IDLE:
196 case SIG_PRI_CALL_LEVEL_SETUP:
198 case SIG_PRI_CALL_LEVEL_OVERLAP:
200 case SIG_PRI_CALL_LEVEL_PROCEEDING:
202 case SIG_PRI_CALL_LEVEL_ALERTING:
204 case SIG_PRI_CALL_LEVEL_DEFER_DIAL:
206 case SIG_PRI_CALL_LEVEL_CONNECT:
212 static inline void pri_rel(struct sig_pri_span *pri)
214 ast_mutex_unlock(&pri->lock);
217 static unsigned int PVT_TO_CHANNEL(struct sig_pri_chan *p)
219 int res = (((p)->prioffset) | ((p)->logicalspan << 8) | (p->mastertrunkgroup ? PRI_EXPLICIT : 0));
220 ast_debug(5, "prioffset: %d mastertrunkgroup: %d logicalspan: %d result: %d\n",
221 p->prioffset, p->mastertrunkgroup, p->logicalspan, res);
226 static void sig_pri_handle_dchan_exception(struct sig_pri_span *pri, int index)
228 if (sig_pri_callbacks.handle_dchan_exception) {
229 sig_pri_callbacks.handle_dchan_exception(pri, index);
233 static void sig_pri_set_dialing(struct sig_pri_chan *p, int is_dialing)
235 if (sig_pri_callbacks.set_dialing) {
236 sig_pri_callbacks.set_dialing(p->chan_pvt, is_dialing);
240 static void sig_pri_set_digital(struct sig_pri_chan *p, int is_digital)
242 p->digital = is_digital;
243 if (sig_pri_callbacks.set_digital) {
244 sig_pri_callbacks.set_digital(p->chan_pvt, is_digital);
248 static void sig_pri_set_outgoing(struct sig_pri_chan *p, int is_outgoing)
250 p->outgoing = is_outgoing;
251 if (sig_pri_callbacks.set_outgoing) {
252 sig_pri_callbacks.set_outgoing(p->chan_pvt, is_outgoing);
256 void sig_pri_set_alarm(struct sig_pri_chan *p, int in_alarm)
258 if (sig_pri_is_alarm_ignored(p->pri)) {
259 /* Always set not in alarm */
264 * Clear the channel restart state when the channel alarm
265 * changes to prevent the state from getting stuck when the link
268 p->resetting = SIG_PRI_RESET_IDLE;
270 p->inalarm = in_alarm;
271 if (sig_pri_callbacks.set_alarm) {
272 sig_pri_callbacks.set_alarm(p->chan_pvt, in_alarm);
276 static const char *sig_pri_get_orig_dialstring(struct sig_pri_chan *p)
278 if (sig_pri_callbacks.get_orig_dialstring) {
279 return sig_pri_callbacks.get_orig_dialstring(p->chan_pvt);
281 ast_log(LOG_ERROR, "get_orig_dialstring callback not defined\n");
285 #if defined(HAVE_PRI_CCSS)
286 static void sig_pri_make_cc_dialstring(struct sig_pri_chan *p, char *buf, size_t buf_size)
288 if (sig_pri_callbacks.make_cc_dialstring) {
289 sig_pri_callbacks.make_cc_dialstring(p->chan_pvt, buf, buf_size);
291 ast_log(LOG_ERROR, "make_cc_dialstring callback not defined\n");
295 #endif /* defined(HAVE_PRI_CCSS) */
297 static void sig_pri_dial_digits(struct sig_pri_chan *p, const char *dial_string)
299 if (sig_pri_callbacks.dial_digits) {
300 sig_pri_callbacks.dial_digits(p->chan_pvt, dial_string);
306 * \brief Reevaluate the PRI span device state.
309 * \param pri PRI span control structure.
313 * \note Assumes the pri->lock is already obtained.
315 static void sig_pri_span_devstate_changed(struct sig_pri_span *pri)
317 if (sig_pri_callbacks.update_span_devstate) {
318 sig_pri_callbacks.update_span_devstate(pri);
324 * \brief Set the caller id information in the parent module.
327 * \param p sig_pri channel structure.
331 static void sig_pri_set_caller_id(struct sig_pri_chan *p)
333 struct ast_party_caller caller;
335 if (sig_pri_callbacks.set_callerid) {
336 ast_party_caller_init(&caller);
338 caller.id.name.str = p->cid_name;
339 caller.id.name.presentation = p->callingpres;
340 caller.id.name.valid = 1;
342 caller.id.number.str = p->cid_num;
343 caller.id.number.plan = p->cid_ton;
344 caller.id.number.presentation = p->callingpres;
345 caller.id.number.valid = 1;
347 if (!ast_strlen_zero(p->cid_subaddr)) {
348 caller.id.subaddress.valid = 1;
349 //caller.id.subaddress.type = 0;/* nsap */
350 //caller.id.subaddress.odd_even_indicator = 0;
351 caller.id.subaddress.str = p->cid_subaddr;
353 caller.id.tag = p->user_tag;
355 caller.ani.number.str = p->cid_ani;
356 //caller.ani.number.plan = p->xxx;
357 //caller.ani.number.presentation = p->xxx;
358 caller.ani.number.valid = 1;
360 caller.ani2 = p->cid_ani2;
361 sig_pri_callbacks.set_callerid(p->chan_pvt, &caller);
367 * \brief Set the Dialed Number Identifier.
370 * \param p sig_pri channel structure.
371 * \param dnid Dialed Number Identifier string.
375 static void sig_pri_set_dnid(struct sig_pri_chan *p, const char *dnid)
377 if (sig_pri_callbacks.set_dnid) {
378 sig_pri_callbacks.set_dnid(p->chan_pvt, dnid);
384 * \brief Set the Redirecting Directory Number Information Service (RDNIS).
387 * \param p sig_pri channel structure.
388 * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
392 static void sig_pri_set_rdnis(struct sig_pri_chan *p, const char *rdnis)
394 if (sig_pri_callbacks.set_rdnis) {
395 sig_pri_callbacks.set_rdnis(p->chan_pvt, rdnis);
399 static void sig_pri_unlock_private(struct sig_pri_chan *p)
401 if (sig_pri_callbacks.unlock_private) {
402 sig_pri_callbacks.unlock_private(p->chan_pvt);
406 static void sig_pri_lock_private(struct sig_pri_chan *p)
408 if (sig_pri_callbacks.lock_private) {
409 sig_pri_callbacks.lock_private(p->chan_pvt);
413 static void sig_pri_deadlock_avoidance_private(struct sig_pri_chan *p)
415 if (sig_pri_callbacks.deadlock_avoidance_private) {
416 sig_pri_callbacks.deadlock_avoidance_private(p->chan_pvt);
418 /* Fallback to the old way if callback not present. */
419 sig_pri_unlock_private(p);
421 sig_pri_lock_private(p);
425 static void pri_grab(struct sig_pri_chan *p, struct sig_pri_span *pri)
427 /* Grab the lock first */
428 while (ast_mutex_trylock(&pri->lock)) {
430 sig_pri_deadlock_avoidance_private(p);
432 /* Then break the poll */
433 if (pri->master != AST_PTHREADT_NULL) {
434 pthread_kill(pri->master, SIGURG);
440 * \brief Convert PRI redirecting reason to asterisk version.
443 * \param pri_reason PRI redirecting reason.
445 * \return Equivalent asterisk redirecting reason value.
447 static enum AST_REDIRECTING_REASON pri_to_ast_reason(int pri_reason)
449 enum AST_REDIRECTING_REASON ast_reason;
451 switch (pri_reason) {
452 case PRI_REDIR_FORWARD_ON_BUSY:
453 ast_reason = AST_REDIRECTING_REASON_USER_BUSY;
455 case PRI_REDIR_FORWARD_ON_NO_REPLY:
456 ast_reason = AST_REDIRECTING_REASON_NO_ANSWER;
458 case PRI_REDIR_DEFLECTION:
459 ast_reason = AST_REDIRECTING_REASON_DEFLECTION;
461 case PRI_REDIR_UNCONDITIONAL:
462 ast_reason = AST_REDIRECTING_REASON_UNCONDITIONAL;
464 case PRI_REDIR_UNKNOWN:
466 ast_reason = AST_REDIRECTING_REASON_UNKNOWN;
475 * \brief Convert asterisk redirecting reason to PRI version.
478 * \param ast_reason Asterisk redirecting reason.
480 * \return Equivalent PRI redirecting reason value.
482 static int ast_to_pri_reason(enum AST_REDIRECTING_REASON ast_reason)
486 switch (ast_reason) {
487 case AST_REDIRECTING_REASON_USER_BUSY:
488 pri_reason = PRI_REDIR_FORWARD_ON_BUSY;
490 case AST_REDIRECTING_REASON_NO_ANSWER:
491 pri_reason = PRI_REDIR_FORWARD_ON_NO_REPLY;
493 case AST_REDIRECTING_REASON_UNCONDITIONAL:
494 pri_reason = PRI_REDIR_UNCONDITIONAL;
496 case AST_REDIRECTING_REASON_DEFLECTION:
497 pri_reason = PRI_REDIR_DEFLECTION;
499 case AST_REDIRECTING_REASON_UNKNOWN:
501 pri_reason = PRI_REDIR_UNKNOWN;
510 * \brief Convert PRI number presentation to asterisk version.
513 * \param pri_presentation PRI number presentation.
515 * \return Equivalent asterisk number presentation value.
517 static int pri_to_ast_presentation(int pri_presentation)
519 int ast_presentation;
521 switch (pri_presentation) {
522 case PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED:
523 ast_presentation = AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED;
525 case PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_PASSED_SCREEN:
526 ast_presentation = AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_PASSED_SCREEN;
528 case PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_FAILED_SCREEN:
529 ast_presentation = AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_FAILED_SCREEN;
531 case PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER:
532 ast_presentation = AST_PRES_ALLOWED | AST_PRES_NETWORK_NUMBER;
535 case PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED:
536 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
538 case PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_PASSED_SCREEN:
539 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_PASSED_SCREEN;
541 case PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_FAILED_SCREEN:
542 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_FAILED_SCREEN;
544 case PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER:
545 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_NETWORK_NUMBER;
548 case PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED:
549 case PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_PASSED_SCREEN:
550 case PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_FAILED_SCREEN:
551 case PRI_PRES_UNAVAILABLE | PRI_PRES_NETWORK_NUMBER:
552 ast_presentation = AST_PRES_NUMBER_NOT_AVAILABLE;
556 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
560 return ast_presentation;
565 * \brief Convert asterisk number presentation to PRI version.
568 * \param ast_presentation Asterisk number presentation.
570 * \return Equivalent PRI number presentation value.
572 static int ast_to_pri_presentation(int ast_presentation)
574 int pri_presentation;
576 switch (ast_presentation) {
577 case AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED:
578 pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
580 case AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_PASSED_SCREEN:
581 pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_PASSED_SCREEN;
583 case AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_FAILED_SCREEN:
584 pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_FAILED_SCREEN;
586 case AST_PRES_ALLOWED | AST_PRES_NETWORK_NUMBER:
587 pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER;
590 case AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED:
591 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
593 case AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_PASSED_SCREEN:
594 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_PASSED_SCREEN;
596 case AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_FAILED_SCREEN:
597 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_FAILED_SCREEN;
599 case AST_PRES_RESTRICTED | AST_PRES_NETWORK_NUMBER:
600 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER;
603 case AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_UNSCREENED:
604 case AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_PASSED_SCREEN:
605 case AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_FAILED_SCREEN:
606 case AST_PRES_UNAVAILABLE | AST_PRES_NETWORK_NUMBER:
607 pri_presentation = PRES_NUMBER_NOT_AVAILABLE;
611 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
615 return pri_presentation;
620 * \brief Convert PRI name char_set to asterisk version.
623 * \param pri_char_set PRI name char_set.
625 * \return Equivalent asterisk name char_set value.
627 static enum AST_PARTY_CHAR_SET pri_to_ast_char_set(int pri_char_set)
629 enum AST_PARTY_CHAR_SET ast_char_set;
631 switch (pri_char_set) {
633 case PRI_CHAR_SET_UNKNOWN:
634 ast_char_set = AST_PARTY_CHAR_SET_UNKNOWN;
636 case PRI_CHAR_SET_ISO8859_1:
637 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_1;
639 case PRI_CHAR_SET_WITHDRAWN:
640 ast_char_set = AST_PARTY_CHAR_SET_WITHDRAWN;
642 case PRI_CHAR_SET_ISO8859_2:
643 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_2;
645 case PRI_CHAR_SET_ISO8859_3:
646 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_3;
648 case PRI_CHAR_SET_ISO8859_4:
649 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_4;
651 case PRI_CHAR_SET_ISO8859_5:
652 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_5;
654 case PRI_CHAR_SET_ISO8859_7:
655 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_7;
657 case PRI_CHAR_SET_ISO10646_BMPSTRING:
658 ast_char_set = AST_PARTY_CHAR_SET_ISO10646_BMPSTRING;
660 case PRI_CHAR_SET_ISO10646_UTF_8STRING:
661 ast_char_set = AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING;
670 * \brief Convert asterisk name char_set to PRI version.
673 * \param ast_char_set Asterisk name char_set.
675 * \return Equivalent PRI name char_set value.
677 static int ast_to_pri_char_set(enum AST_PARTY_CHAR_SET ast_char_set)
681 switch (ast_char_set) {
683 case AST_PARTY_CHAR_SET_UNKNOWN:
684 pri_char_set = PRI_CHAR_SET_UNKNOWN;
686 case AST_PARTY_CHAR_SET_ISO8859_1:
687 pri_char_set = PRI_CHAR_SET_ISO8859_1;
689 case AST_PARTY_CHAR_SET_WITHDRAWN:
690 pri_char_set = PRI_CHAR_SET_WITHDRAWN;
692 case AST_PARTY_CHAR_SET_ISO8859_2:
693 pri_char_set = PRI_CHAR_SET_ISO8859_2;
695 case AST_PARTY_CHAR_SET_ISO8859_3:
696 pri_char_set = PRI_CHAR_SET_ISO8859_3;
698 case AST_PARTY_CHAR_SET_ISO8859_4:
699 pri_char_set = PRI_CHAR_SET_ISO8859_4;
701 case AST_PARTY_CHAR_SET_ISO8859_5:
702 pri_char_set = PRI_CHAR_SET_ISO8859_5;
704 case AST_PARTY_CHAR_SET_ISO8859_7:
705 pri_char_set = PRI_CHAR_SET_ISO8859_7;
707 case AST_PARTY_CHAR_SET_ISO10646_BMPSTRING:
708 pri_char_set = PRI_CHAR_SET_ISO10646_BMPSTRING;
710 case AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING:
711 pri_char_set = PRI_CHAR_SET_ISO10646_UTF_8STRING;
718 #if defined(HAVE_PRI_SUBADDR)
721 * \brief Fill in the asterisk party subaddress from the given PRI party subaddress.
724 * \param ast_subaddress Asterisk party subaddress structure.
725 * \param pri_subaddress PRI party subaddress structure.
730 static void sig_pri_set_subaddress(struct ast_party_subaddress *ast_subaddress, const struct pri_party_subaddress *pri_subaddress)
732 ast_free(ast_subaddress->str);
733 if (pri_subaddress->length <= 0) {
734 ast_party_subaddress_init(ast_subaddress);
738 if (!pri_subaddress->type) {
740 ast_subaddress->str = ast_strdup((char *) pri_subaddress->data);
748 cnum = ast_malloc(2 * pri_subaddress->length + 1);
750 ast_party_subaddress_init(ast_subaddress);
755 len = pri_subaddress->length - 1; /* -1 account for zero based indexing */
756 for (x = 0; x < len; ++x) {
757 ptr += sprintf(ptr, "%02hhx", (unsigned char)pri_subaddress->data[x]);
760 if (pri_subaddress->odd_even_indicator) {
762 sprintf(ptr, "%01hhx", (unsigned char)((pri_subaddress->data[len]) >> 4));
765 sprintf(ptr, "%02hhx", (unsigned char)pri_subaddress->data[len]);
767 ast_subaddress->str = cnum;
769 ast_subaddress->type = pri_subaddress->type;
770 ast_subaddress->odd_even_indicator = pri_subaddress->odd_even_indicator;
771 ast_subaddress->valid = 1;
773 #endif /* defined(HAVE_PRI_SUBADDR) */
775 #if defined(HAVE_PRI_SUBADDR)
776 static unsigned char ast_pri_pack_hex_char(char c)
782 } else if (c < ('9' + 1)) {
784 } else if (c < 'A') {
786 } else if (c < ('F' + 1)) {
788 } else if (c < 'a') {
790 } else if (c < ('f' + 1)) {
797 #endif /* defined(HAVE_PRI_SUBADDR) */
799 #if defined(HAVE_PRI_SUBADDR)
802 * \brief Convert a null terminated hexadecimal string to a packed hex byte array.
803 * \details left justified, with 0 padding if odd length.
806 * \param dst pointer to packed byte array.
807 * \param src pointer to null terminated hexadecimal string.
808 * \param maxlen destination array size.
810 * \return Length of byte array
812 * \note The dst is not an ASCIIz string.
813 * \note The src is an ASCIIz hex string.
815 static int ast_pri_pack_hex_string(unsigned char *dst, char *src, int maxlen)
818 int len = strlen(src);
820 if (len > (2 * maxlen)) {
824 res = len / 2 + len % 2;
827 *dst = ast_pri_pack_hex_char(*src) << 4;
829 *dst |= ast_pri_pack_hex_char(*src);
833 if (len) { /* 1 left */
834 *dst = ast_pri_pack_hex_char(*src) << 4;
838 #endif /* defined(HAVE_PRI_SUBADDR) */
840 #if defined(HAVE_PRI_SUBADDR)
843 * \brief Fill in the PRI party subaddress from the given asterisk party subaddress.
846 * \param pri_subaddress PRI party subaddress structure.
847 * \param ast_subaddress Asterisk party subaddress structure.
851 * \note Assumes that pri_subaddress has been previously memset to zero.
853 static void sig_pri_party_subaddress_from_ast(struct pri_party_subaddress *pri_subaddress, const struct ast_party_subaddress *ast_subaddress)
855 if (ast_subaddress->valid && !ast_strlen_zero(ast_subaddress->str)) {
856 pri_subaddress->type = ast_subaddress->type;
857 if (!ast_subaddress->type) {
859 ast_copy_string((char *) pri_subaddress->data, ast_subaddress->str,
860 sizeof(pri_subaddress->data));
861 pri_subaddress->length = strlen((char *) pri_subaddress->data);
862 pri_subaddress->odd_even_indicator = 0;
863 pri_subaddress->valid = 1;
865 /* 2 = User Specified */
867 * Copy HexString to packed HexData,
868 * if odd length then right pad trailing byte with 0
870 int length = ast_pri_pack_hex_string(pri_subaddress->data,
871 ast_subaddress->str, sizeof(pri_subaddress->data));
873 pri_subaddress->length = length; /* packed data length */
875 length = strlen(ast_subaddress->str);
876 if (length > 2 * sizeof(pri_subaddress->data)) {
877 pri_subaddress->odd_even_indicator = 0;
879 pri_subaddress->odd_even_indicator = (length & 1);
881 pri_subaddress->valid = 1;
885 #endif /* defined(HAVE_PRI_SUBADDR) */
889 * \brief Fill in the PRI party name from the given asterisk party name.
892 * \param pri_name PRI party name structure.
893 * \param ast_name Asterisk party name structure.
897 * \note Assumes that pri_name has been previously memset to zero.
899 static void sig_pri_party_name_from_ast(struct pri_party_name *pri_name, const struct ast_party_name *ast_name)
901 if (!ast_name->valid) {
905 pri_name->presentation = ast_to_pri_presentation(ast_name->presentation);
906 pri_name->char_set = ast_to_pri_char_set(ast_name->char_set);
907 if (!ast_strlen_zero(ast_name->str)) {
908 ast_copy_string(pri_name->str, ast_name->str, sizeof(pri_name->str));
914 * \brief Fill in the PRI party number from the given asterisk party number.
917 * \param pri_number PRI party number structure.
918 * \param ast_number Asterisk party number structure.
922 * \note Assumes that pri_number has been previously memset to zero.
924 static void sig_pri_party_number_from_ast(struct pri_party_number *pri_number, const struct ast_party_number *ast_number)
926 if (!ast_number->valid) {
929 pri_number->valid = 1;
930 pri_number->presentation = ast_to_pri_presentation(ast_number->presentation);
931 pri_number->plan = ast_number->plan;
932 if (!ast_strlen_zero(ast_number->str)) {
933 ast_copy_string(pri_number->str, ast_number->str, sizeof(pri_number->str));
939 * \brief Fill in the PRI party id from the given asterisk party id.
942 * \param pri_id PRI party id structure.
943 * \param ast_id Asterisk party id structure.
947 * \note Assumes that pri_id has been previously memset to zero.
949 static void sig_pri_party_id_from_ast(struct pri_party_id *pri_id, const struct ast_party_id *ast_id)
951 sig_pri_party_name_from_ast(&pri_id->name, &ast_id->name);
952 sig_pri_party_number_from_ast(&pri_id->number, &ast_id->number);
953 #if defined(HAVE_PRI_SUBADDR)
954 sig_pri_party_subaddress_from_ast(&pri_id->subaddress, &ast_id->subaddress);
955 #endif /* defined(HAVE_PRI_SUBADDR) */
960 * \brief Update the PRI redirecting information for the current call.
963 * \param pvt sig_pri private channel structure.
964 * \param ast Asterisk channel
968 * \note Assumes that the PRI lock is already obtained.
970 static void sig_pri_redirecting_update(struct sig_pri_chan *pvt, struct ast_channel *ast)
972 struct pri_party_redirecting pri_redirecting;
973 const struct ast_party_redirecting *ast_redirecting;
974 struct ast_party_id redirecting_from = ast_channel_redirecting_effective_from(ast);
975 struct ast_party_id redirecting_to = ast_channel_redirecting_effective_to(ast);
976 struct ast_party_id redirecting_orig = ast_channel_redirecting_effective_orig(ast);
978 memset(&pri_redirecting, 0, sizeof(pri_redirecting));
979 ast_redirecting = ast_channel_redirecting(ast);
980 sig_pri_party_id_from_ast(&pri_redirecting.from, &redirecting_from);
981 sig_pri_party_id_from_ast(&pri_redirecting.to, &redirecting_to);
982 sig_pri_party_id_from_ast(&pri_redirecting.orig_called, &redirecting_orig);
983 pri_redirecting.count = ast_redirecting->count;
984 pri_redirecting.orig_reason = ast_to_pri_reason(ast_redirecting->orig_reason.code);
985 pri_redirecting.reason = ast_to_pri_reason(ast_redirecting->reason.code);
987 pri_redirecting_update(pvt->pri->pri, pvt->call, &pri_redirecting);
992 * \brief Reset DTMF detector.
995 * \param p sig_pri channel structure.
999 static void sig_pri_dsp_reset_and_flush_digits(struct sig_pri_chan *p)
1001 if (sig_pri_callbacks.dsp_reset_and_flush_digits) {
1002 sig_pri_callbacks.dsp_reset_and_flush_digits(p->chan_pvt);
1006 static int sig_pri_set_echocanceller(struct sig_pri_chan *p, int enable)
1008 if (sig_pri_callbacks.set_echocanceller) {
1009 return sig_pri_callbacks.set_echocanceller(p->chan_pvt, enable);
1015 static void sig_pri_fixup_chans(struct sig_pri_chan *old_chan, struct sig_pri_chan *new_chan)
1017 if (sig_pri_callbacks.fixup_chans) {
1018 sig_pri_callbacks.fixup_chans(old_chan->chan_pvt, new_chan->chan_pvt);
1022 static int sig_pri_play_tone(struct sig_pri_chan *p, enum sig_pri_tone tone)
1024 if (sig_pri_callbacks.play_tone) {
1025 return sig_pri_callbacks.play_tone(p->chan_pvt, tone);
1031 static struct ast_channel *sig_pri_new_ast_channel(struct sig_pri_chan *p, int state,
1032 enum sig_pri_law law, int transfercapability, char *exten,
1033 const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
1035 struct ast_channel *c;
1037 if (sig_pri_callbacks.new_ast_channel) {
1038 c = sig_pri_callbacks.new_ast_channel(p->chan_pvt, state, law, exten, assignedids, requestor);
1046 ast_assert(p->owner == NULL || p->owner == c);
1049 p->alreadyhungup = 0;
1050 ast_channel_transfercapability_set(c, transfercapability);
1051 pbx_builtin_setvar_helper(c, "TRANSFERCAPABILITY",
1052 ast_transfercapability2str(transfercapability));
1053 if (transfercapability & AST_TRANS_CAP_DIGITAL) {
1054 sig_pri_set_digital(p, 1);
1057 ast_mutex_lock(&p->pri->lock);
1058 sig_pri_span_devstate_changed(p->pri);
1059 ast_mutex_unlock(&p->pri->lock);
1067 * \brief Open the PRI channel media path.
1070 * \param p Channel private control structure.
1074 static void sig_pri_open_media(struct sig_pri_chan *p)
1076 if (p->no_b_channel) {
1080 if (sig_pri_callbacks.open_media) {
1081 sig_pri_callbacks.open_media(p->chan_pvt);
1087 * \brief Post an AMI B channel association event.
1090 * \param p Channel private control structure.
1092 * \note Assumes the private and owner are locked.
1096 static void sig_pri_ami_channel_event(struct sig_pri_chan *p)
1098 if (sig_pri_callbacks.ami_channel_event) {
1099 sig_pri_callbacks.ami_channel_event(p->chan_pvt, p->owner);
1103 struct ast_channel *sig_pri_request(struct sig_pri_chan *p, enum sig_pri_law law,
1104 const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor,
1105 int transfercapability)
1107 struct ast_channel *ast;
1109 ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1111 sig_pri_set_outgoing(p, 1);
1112 ast = sig_pri_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability,
1113 p->exten, assignedids, requestor);
1115 sig_pri_set_outgoing(p, 0);
1120 int pri_is_up(struct sig_pri_span *pri)
1123 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
1124 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
1130 static const char *pri_order(int level)
1140 return "Quaternary";
1146 /* Returns index of the active dchan */
1147 static int pri_active_dchan_index(struct sig_pri_span *pri)
1151 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
1152 if ((pri->dchans[x] == pri->pri))
1156 ast_log(LOG_WARNING, "No active dchan found!\n");
1160 static void pri_find_dchan(struct sig_pri_span *pri)
1168 for (idx = 0; idx < SIG_PRI_NUM_DCHANS; ++idx) {
1169 if (!pri->dchans[idx]) {
1170 /* No more D channels defined on the span. */
1173 if (pri->dchans[idx] == old) {
1176 if (newslot < 0 && pri->dchanavail[idx] == DCHAN_AVAILABLE) {
1180 /* At this point, idx is a count of how many D-channels are defined on the span. */
1183 /* We have several D-channels defined on the span. (NFAS PRI setup) */
1185 /* No D-channels available. Default to the primary D-channel. */
1188 if (!pri->no_d_channels) {
1189 pri->no_d_channels = 1;
1190 if (old && oldslot != newslot) {
1191 ast_log(LOG_WARNING,
1192 "Span %d: No D-channels up! Switching selected D-channel from %s to %s.\n",
1193 pri->span, pri_order(oldslot), pri_order(newslot));
1195 ast_log(LOG_WARNING, "Span %d: No D-channels up!\n", pri->span);
1199 pri->no_d_channels = 0;
1201 if (old && oldslot != newslot) {
1203 "Switching selected D-channel from %s (fd %d) to %s (fd %d)!\n",
1204 pri_order(oldslot), pri->fds[oldslot],
1205 pri_order(newslot), pri->fds[newslot]);
1209 /* The only D-channel is not up. */
1212 if (!pri->no_d_channels) {
1213 pri->no_d_channels = 1;
1216 * This is annoying to see on non-persistent layer 2
1217 * connections. Let's not complain in that case.
1219 if (pri->sig != SIG_BRI_PTMP) {
1220 ast_log(LOG_WARNING, "Span %d: D-channel is down!\n", pri->span);
1224 pri->no_d_channels = 0;
1227 pri->pri = pri->dchans[newslot];
1232 * \brief Determine if a private channel structure is in use.
1235 * \param pvt Channel to determine if in use.
1237 * \return TRUE if the channel is in use.
1239 static int sig_pri_is_chan_in_use(struct sig_pri_chan *pvt)
1241 return pvt->owner || pvt->call || pvt->allocated || pvt->inalarm
1242 || pvt->resetting != SIG_PRI_RESET_IDLE;
1246 * \brief Determine if a private channel structure is available.
1249 * \param pvt Channel to determine if available.
1251 * \return TRUE if the channel is available.
1253 int sig_pri_is_chan_available(struct sig_pri_chan *pvt)
1255 return !sig_pri_is_chan_in_use(pvt)
1256 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1257 /* And not out-of-service */
1258 && !pvt->service_status
1259 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1265 * \brief Obtain the sig_pri owner channel lock if the owner exists.
1268 * \param pri PRI span control structure.
1269 * \param chanpos Channel position in the span.
1271 * \note Assumes the pri->lock is already obtained.
1272 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1276 static void sig_pri_lock_owner(struct sig_pri_span *pri, int chanpos)
1279 if (!pri->pvts[chanpos]->owner) {
1280 /* There is no owner lock to get. */
1283 if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
1284 /* We got the lock */
1288 /* Avoid deadlock */
1289 sig_pri_unlock_private(pri->pvts[chanpos]);
1290 DEADLOCK_AVOIDANCE(&pri->lock);
1291 sig_pri_lock_private(pri->pvts[chanpos]);
1297 * \brief Queue the given frame onto the owner channel.
1300 * \param pri PRI span control structure.
1301 * \param chanpos Channel position in the span.
1302 * \param frame Frame to queue onto the owner channel.
1304 * \note Assumes the pri->lock is already obtained.
1305 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1309 static void pri_queue_frame(struct sig_pri_span *pri, int chanpos, struct ast_frame *frame)
1311 sig_pri_lock_owner(pri, chanpos);
1312 if (pri->pvts[chanpos]->owner) {
1313 ast_queue_frame(pri->pvts[chanpos]->owner, frame);
1314 ast_channel_unlock(pri->pvts[chanpos]->owner);
1320 * \brief Queue a hold frame onto the owner channel.
1323 * \param pri PRI span control structure.
1324 * \param chanpos Channel position in the span.
1326 * \note Assumes the pri->lock is already obtained.
1327 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1331 static void sig_pri_queue_hold(struct sig_pri_span *pri, int chanpos)
1333 sig_pri_lock_owner(pri, chanpos);
1334 if (pri->pvts[chanpos]->owner) {
1335 ast_queue_hold(pri->pvts[chanpos]->owner, NULL);
1336 ast_channel_unlock(pri->pvts[chanpos]->owner);
1342 * \brief Queue an unhold frame onto the owner channel.
1345 * \param pri PRI span control structure.
1346 * \param chanpos Channel position in the span.
1348 * \note Assumes the pri->lock is already obtained.
1349 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1353 static void sig_pri_queue_unhold(struct sig_pri_span *pri, int chanpos)
1355 sig_pri_lock_owner(pri, chanpos);
1356 if (pri->pvts[chanpos]->owner) {
1357 ast_queue_unhold(pri->pvts[chanpos]->owner);
1358 ast_channel_unlock(pri->pvts[chanpos]->owner);
1364 * \brief Queue a control frame of the specified subclass onto the owner channel.
1367 * \param pri PRI span control structure.
1368 * \param chanpos Channel position in the span.
1369 * \param subclass Control frame subclass to queue onto the owner channel.
1371 * \note Assumes the pri->lock is already obtained.
1372 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1376 static void pri_queue_control(struct sig_pri_span *pri, int chanpos, int subclass)
1378 struct ast_frame f = {AST_FRAME_CONTROL, };
1379 struct sig_pri_chan *p = pri->pvts[chanpos];
1381 if (sig_pri_callbacks.queue_control) {
1382 sig_pri_callbacks.queue_control(p->chan_pvt, subclass);
1385 f.subclass.integer = subclass;
1386 pri_queue_frame(pri, chanpos, &f);
1391 * \brief Queue a PVT_CAUSE_CODE frame onto the owner channel.
1394 * \param pri PRI span control structure.
1395 * \param chanpos Channel position in the span.
1396 * \param cause String describing the cause to be placed into the frame.
1398 * \note Assumes the pri->lock is already obtained.
1399 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1403 static void pri_queue_pvt_cause_data(struct sig_pri_span *pri, int chanpos, const char *cause, int ast_cause)
1405 struct ast_channel *chan;
1406 struct ast_control_pvt_cause_code *cause_code;
1408 sig_pri_lock_owner(pri, chanpos);
1409 chan = pri->pvts[chanpos]->owner;
1411 int datalen = sizeof(*cause_code) + strlen(cause);
1412 cause_code = ast_alloca(datalen);
1413 memset(cause_code, 0, datalen);
1414 cause_code->ast_cause = ast_cause;
1415 ast_copy_string(cause_code->chan_name, ast_channel_name(chan), AST_CHANNEL_NAME);
1416 ast_copy_string(cause_code->code, cause, datalen + 1 - sizeof(*cause_code));
1417 ast_queue_control_data(chan, AST_CONTROL_PVT_CAUSE_CODE, cause_code, datalen);
1418 ast_channel_hangupcause_hash_set(chan, cause_code, datalen);
1419 ast_channel_unlock(chan);
1425 * \brief Find the channel associated with the libpri call.
1428 * \param pri PRI span control structure.
1429 * \param call LibPRI opaque call pointer to find.
1431 * \note Assumes the pri->lock is already obtained.
1433 * \retval array-index into private pointer array on success.
1434 * \retval -1 on error.
1436 static int pri_find_principle_by_call(struct sig_pri_span *pri, q931_call *call)
1441 /* Cannot find a call without a call. */
1444 for (idx = 0; idx < pri->numchans; ++idx) {
1445 if (pri->pvts[idx] && pri->pvts[idx]->call == call) {
1446 /* Found the principle */
1455 * \brief Queue the span for destruction
1458 * \param pri PRI span control structure.
1460 * Asks the channel driver to queue the span for destruction at a
1461 * possibly later time, if (e.g.) locking considerations don't allow
1462 * destroying it right now.
1466 static void pri_destroy_later(struct sig_pri_span *pri)
1468 if (!sig_pri_callbacks.destroy_later) {
1471 sig_pri_callbacks.destroy_later(pri);
1476 * \brief Kill the call.
1479 * \param pri PRI span control structure.
1480 * \param call LibPRI opaque call pointer to find.
1481 * \param cause Reason call was killed.
1483 * \note Assumes the pvt->pri->lock is already obtained.
1487 static void sig_pri_kill_call(struct sig_pri_span *pri, q931_call *call, int cause)
1491 chanpos = pri_find_principle_by_call(pri, call);
1493 pri_hangup(pri->pri, call, cause);
1496 sig_pri_lock_private(pri->pvts[chanpos]);
1497 if (!pri->pvts[chanpos]->owner) {
1498 pri_hangup(pri->pri, call, cause);
1499 pri->pvts[chanpos]->call = NULL;
1500 sig_pri_unlock_private(pri->pvts[chanpos]);
1501 sig_pri_span_devstate_changed(pri);
1504 ast_channel_hangupcause_set(pri->pvts[chanpos]->owner, cause);
1505 pri_queue_control(pri, chanpos, AST_CONTROL_HANGUP);
1506 sig_pri_unlock_private(pri->pvts[chanpos]);
1511 * \brief Find the private structure for the libpri call.
1513 * \param pri PRI span control structure.
1514 * \param channel LibPRI encoded channel ID.
1515 * \param call LibPRI opaque call pointer.
1517 * \note Assumes the pri->lock is already obtained.
1519 * \retval array-index into private pointer array on success.
1520 * \retval -1 on error.
1522 static int pri_find_principle(struct sig_pri_span *pri, int channel, q931_call *call)
1530 /* Channel is not picked yet. */
1534 prioffset = PRI_CHANNEL(channel);
1535 if (!prioffset || (channel & PRI_HELD_CALL)) {
1536 /* Find the call waiting call or held call. */
1537 return pri_find_principle_by_call(pri, call);
1540 span = PRI_SPAN(channel);
1541 if (!(channel & PRI_EXPLICIT)) {
1544 index = pri_active_dchan_index(pri);
1548 span = pri->dchan_logical_span[index];
1552 for (x = 0; x < pri->numchans; x++) {
1554 && pri->pvts[x]->prioffset == prioffset
1555 && pri->pvts[x]->logicalspan == span
1556 && !pri->pvts[x]->no_b_channel) {
1567 * \brief Fixup the private structure associated with the libpri call.
1569 * \param pri PRI span control structure.
1570 * \param principle Array-index into private array to move call to if not already there.
1571 * \param call LibPRI opaque call pointer to find if need to move call.
1573 * \note Assumes the pri->lock is already obtained.
1575 * \retval principle on success.
1576 * \retval -1 on error.
1578 static int pri_fixup_principle(struct sig_pri_span *pri, int principle, q931_call *call)
1582 if (principle < 0 || pri->numchans <= principle) {
1590 if (pri->pvts[principle] && pri->pvts[principle]->call == call) {
1591 /* Call is already on the specified principle. */
1595 /* Find the old principle location. */
1596 for (x = 0; x < pri->numchans; x++) {
1597 struct sig_pri_chan *new_chan;
1598 struct sig_pri_chan *old_chan;
1600 if (!pri->pvts[x] || pri->pvts[x]->call != call) {
1604 /* Found our call */
1605 new_chan = pri->pvts[principle];
1606 old_chan = pri->pvts[x];
1608 /* Get locks to safely move to the new private structure. */
1609 sig_pri_lock_private(old_chan);
1610 sig_pri_lock_owner(pri, x);
1611 sig_pri_lock_private(new_chan);
1613 ast_verb(3, "Moving call (%s) from channel %d to %d.\n",
1614 old_chan->owner ? ast_channel_name(old_chan->owner) : "",
1615 old_chan->channel, new_chan->channel);
1616 if (!sig_pri_is_chan_available(new_chan)) {
1617 ast_log(LOG_WARNING,
1618 "Can't move call (%s) from channel %d to %d. It is already in use.\n",
1619 old_chan->owner ? ast_channel_name(old_chan->owner) : "",
1620 old_chan->channel, new_chan->channel);
1621 sig_pri_unlock_private(new_chan);
1622 if (old_chan->owner) {
1623 ast_channel_unlock(old_chan->owner);
1625 sig_pri_unlock_private(old_chan);
1629 sig_pri_fixup_chans(old_chan, new_chan);
1631 /* Fix it all up now */
1632 new_chan->owner = old_chan->owner;
1633 old_chan->owner = NULL;
1635 new_chan->call = old_chan->call;
1636 old_chan->call = NULL;
1638 /* Transfer flags from the old channel. */
1639 #if defined(HAVE_PRI_AOC_EVENTS)
1640 new_chan->aoc_s_request_invoke_id_valid = old_chan->aoc_s_request_invoke_id_valid;
1641 new_chan->waiting_for_aoce = old_chan->waiting_for_aoce;
1642 new_chan->holding_aoce = old_chan->holding_aoce;
1643 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1644 new_chan->alreadyhungup = old_chan->alreadyhungup;
1645 new_chan->isidlecall = old_chan->isidlecall;
1646 new_chan->progress = old_chan->progress;
1647 new_chan->allocated = old_chan->allocated;
1648 new_chan->outgoing = old_chan->outgoing;
1649 new_chan->digital = old_chan->digital;
1650 #if defined(HAVE_PRI_CALL_WAITING)
1651 new_chan->is_call_waiting = old_chan->is_call_waiting;
1652 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1653 #if defined(HAVE_PRI_SETUP_ACK_INBAND)
1654 new_chan->no_dialed_digits = old_chan->no_dialed_digits;
1655 #endif /* defined(HAVE_PRI_SETUP_ACK_INBAND) */
1657 #if defined(HAVE_PRI_AOC_EVENTS)
1658 old_chan->aoc_s_request_invoke_id_valid = 0;
1659 old_chan->waiting_for_aoce = 0;
1660 old_chan->holding_aoce = 0;
1661 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1662 old_chan->alreadyhungup = 0;
1663 old_chan->isidlecall = 0;
1664 old_chan->progress = 0;
1665 old_chan->allocated = 0;
1666 old_chan->outgoing = 0;
1667 old_chan->digital = 0;
1668 #if defined(HAVE_PRI_CALL_WAITING)
1669 old_chan->is_call_waiting = 0;
1670 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1671 #if defined(HAVE_PRI_SETUP_ACK_INBAND)
1672 old_chan->no_dialed_digits = 0;
1673 #endif /* defined(HAVE_PRI_SETUP_ACK_INBAND) */
1675 /* More stuff to transfer to the new channel. */
1676 new_chan->call_level = old_chan->call_level;
1677 old_chan->call_level = SIG_PRI_CALL_LEVEL_IDLE;
1678 #if defined(HAVE_PRI_REVERSE_CHARGE)
1679 new_chan->reverse_charging_indication = old_chan->reverse_charging_indication;
1680 #endif /* defined(HAVE_PRI_REVERSE_CHARGE) */
1681 #if defined(HAVE_PRI_SETUP_KEYPAD)
1682 strcpy(new_chan->keypad_digits, old_chan->keypad_digits);
1683 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
1684 strcpy(new_chan->deferred_digits, old_chan->deferred_digits);
1685 strcpy(new_chan->moh_suggested, old_chan->moh_suggested);
1686 new_chan->moh_state = old_chan->moh_state;
1687 old_chan->moh_state = SIG_PRI_MOH_STATE_IDLE;
1688 #if defined(HAVE_PRI_TRANSFER)
1689 new_chan->xfer_data = old_chan->xfer_data;
1690 old_chan->xfer_data = NULL;
1691 #endif /* defined(HAVE_PRI_TRANSFER) */
1693 #if defined(HAVE_PRI_AOC_EVENTS)
1694 new_chan->aoc_s_request_invoke_id = old_chan->aoc_s_request_invoke_id;
1695 new_chan->aoc_e = old_chan->aoc_e;
1696 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1697 strcpy(new_chan->user_tag, old_chan->user_tag);
1699 if (new_chan->no_b_channel) {
1700 /* Copy the real channel configuration to the no B channel interface. */
1701 new_chan->hidecallerid = old_chan->hidecallerid;
1702 new_chan->hidecalleridname = old_chan->hidecalleridname;
1703 new_chan->immediate = old_chan->immediate;
1704 new_chan->priexclusive = old_chan->priexclusive;
1705 new_chan->priindication_oob = old_chan->priindication_oob;
1706 new_chan->use_callerid = old_chan->use_callerid;
1707 new_chan->use_callingpres = old_chan->use_callingpres;
1708 new_chan->stripmsd = old_chan->stripmsd;
1709 strcpy(new_chan->context, old_chan->context);
1710 strcpy(new_chan->mohinterpret, old_chan->mohinterpret);
1712 /* Become a member of the old channel span/trunk-group. */
1713 new_chan->logicalspan = old_chan->logicalspan;
1714 new_chan->mastertrunkgroup = old_chan->mastertrunkgroup;
1715 } else if (old_chan->no_b_channel) {
1717 * We are transitioning from a held/call-waiting channel to a
1718 * real channel so we need to make sure that the media path is
1719 * open. (Needed especially if the channel is natively
1722 sig_pri_open_media(new_chan);
1725 if (new_chan->owner) {
1726 sig_pri_ami_channel_event(new_chan);
1729 sig_pri_unlock_private(old_chan);
1730 if (new_chan->owner) {
1731 ast_channel_unlock(new_chan->owner);
1733 sig_pri_unlock_private(new_chan);
1737 ast_verb(3, "Call specified, but not found.\n");
1743 * \brief Find and fixup the private structure associated with the libpri call.
1745 * \param pri PRI span control structure.
1746 * \param channel LibPRI encoded channel ID.
1747 * \param call LibPRI opaque call pointer.
1750 * This is a combination of pri_find_principle() and pri_fixup_principle()
1751 * to reduce code redundancy and to make handling several PRI_EVENT_xxx's
1752 * consistent for the current architecture.
1754 * \note Assumes the pri->lock is already obtained.
1756 * \retval array-index into private pointer array on success.
1757 * \retval -1 on error.
1759 static int pri_find_fixup_principle(struct sig_pri_span *pri, int channel, q931_call *call)
1763 chanpos = pri_find_principle(pri, channel, call);
1765 ast_log(LOG_WARNING, "Span %d: PRI requested channel %d/%d is unconfigured.\n",
1766 pri->span, PRI_SPAN(channel), PRI_CHANNEL(channel));
1767 sig_pri_kill_call(pri, call, PRI_CAUSE_IDENTIFIED_CHANNEL_NOTEXIST);
1770 chanpos = pri_fixup_principle(pri, chanpos, call);
1772 ast_log(LOG_WARNING, "Span %d: PRI requested channel %d/%d is not available.\n",
1773 pri->span, PRI_SPAN(channel), PRI_CHANNEL(channel));
1775 * Using Q.931 section 5.2.3.1 b) as the reason for picking
1776 * PRI_CAUSE_CHANNEL_UNACCEPTABLE. Receiving a
1777 * PRI_CAUSE_REQUESTED_CHAN_UNAVAIL would cause us to restart
1778 * that channel (which is not specified by Q.931) and kill some
1779 * other call which would be bad.
1781 sig_pri_kill_call(pri, call, PRI_CAUSE_CHANNEL_UNACCEPTABLE);
1787 static char * redirectingreason2str(int redirectingreason)
1789 switch (redirectingreason) {
1797 return "UNCONDITIONAL";
1799 return "NOREDIRECT";
1803 static char *dialplan2str(int dialplan)
1805 if (dialplan == -1) {
1806 return("Dynamically set dialplan in ISDN");
1808 return (pri_plan2str(dialplan));
1813 * \brief Apply numbering plan prefix to the given number.
1815 * \param buf Buffer to put number into.
1816 * \param size Size of given buffer.
1817 * \param pri PRI span control structure.
1818 * \param number Number to apply numbering plan.
1819 * \param plan Numbering plan to apply.
1823 static void apply_plan_to_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, int plan)
1826 case PRI_INTERNATIONAL_ISDN: /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */
1827 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
1829 case PRI_NATIONAL_ISDN: /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */
1830 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
1832 case PRI_LOCAL_ISDN: /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */
1833 snprintf(buf, size, "%s%s", pri->localprefix, number);
1835 case PRI_PRIVATE: /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */
1836 snprintf(buf, size, "%s%s", pri->privateprefix, number);
1838 case PRI_UNKNOWN: /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */
1839 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
1841 default: /* other Q.931 dialplan => don't twiddle with callingnum */
1842 snprintf(buf, size, "%s", number);
1849 * \brief Apply numbering plan prefix to the given number if the number exists.
1851 * \param buf Buffer to put number into.
1852 * \param size Size of given buffer.
1853 * \param pri PRI span control structure.
1854 * \param number Number to apply numbering plan.
1855 * \param plan Numbering plan to apply.
1859 static void apply_plan_to_existing_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, int plan)
1861 /* Make sure a number exists so the prefix isn't placed on an empty string. */
1862 if (ast_strlen_zero(number)) {
1868 apply_plan_to_number(buf, size, pri, number, plan);
1873 * \brief Restart the next channel we think is idle on the span.
1875 * \param pri PRI span control structure.
1877 * \note Assumes the pri->lock is already obtained.
1881 static void pri_check_restart(struct sig_pri_span *pri)
1883 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1885 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1887 for (++pri->resetpos; pri->resetpos < pri->numchans; ++pri->resetpos) {
1888 if (!pri->pvts[pri->resetpos]
1889 || pri->pvts[pri->resetpos]->no_b_channel
1890 || sig_pri_is_chan_in_use(pri->pvts[pri->resetpos])) {
1893 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1894 why = pri->pvts[pri->resetpos]->service_status;
1897 "Span %d: channel %d out-of-service (reason: %s), not sending RESTART\n",
1898 pri->span, pri->pvts[pri->resetpos]->channel,
1899 (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
1902 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1905 if (pri->resetpos < pri->numchans) {
1906 /* Mark the channel as resetting and restart it */
1907 pri->pvts[pri->resetpos]->resetting = SIG_PRI_RESET_ACTIVE;
1908 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
1911 time(&pri->lastreset);
1912 sig_pri_span_devstate_changed(pri);
1916 #if defined(HAVE_PRI_CALL_WAITING)
1919 * \brief Init the private channel configuration using the span controller.
1922 * \param pvt Channel to init the configuration.
1923 * \param pri PRI span control structure.
1925 * \note Assumes the pri->lock is already obtained.
1929 static void sig_pri_init_config(struct sig_pri_chan *pvt, struct sig_pri_span *pri)
1931 pvt->stripmsd = pri->ch_cfg.stripmsd;
1932 pvt->hidecallerid = pri->ch_cfg.hidecallerid;
1933 pvt->hidecalleridname = pri->ch_cfg.hidecalleridname;
1934 pvt->immediate = pri->ch_cfg.immediate;
1935 pvt->priexclusive = pri->ch_cfg.priexclusive;
1936 pvt->priindication_oob = pri->ch_cfg.priindication_oob;
1937 pvt->use_callerid = pri->ch_cfg.use_callerid;
1938 pvt->use_callingpres = pri->ch_cfg.use_callingpres;
1939 ast_copy_string(pvt->context, pri->ch_cfg.context, sizeof(pvt->context));
1940 ast_copy_string(pvt->mohinterpret, pri->ch_cfg.mohinterpret, sizeof(pvt->mohinterpret));
1942 if (sig_pri_callbacks.init_config) {
1943 sig_pri_callbacks.init_config(pvt->chan_pvt, pri);
1946 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1950 * \brief Find an empty B-channel interface to use.
1952 * \param pri PRI span control structure.
1953 * \param backwards TRUE if the search starts from higher channels.
1955 * \note Assumes the pri->lock is already obtained.
1957 * \retval array-index into private pointer array on success.
1958 * \retval -1 on error.
1960 static int pri_find_empty_chan(struct sig_pri_span *pri, int backwards)
1968 if (backwards && (x < 0))
1970 if (!backwards && (x >= pri->numchans))
1973 && !pri->pvts[x]->no_b_channel
1974 && sig_pri_is_chan_available(pri->pvts[x])) {
1975 ast_debug(1, "Found empty available channel %d/%d\n",
1976 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
1987 #if defined(HAVE_PRI_CALL_HOLD)
1990 * \brief Find or create an empty no-B-channel interface to use.
1993 * \param pri PRI span control structure.
1995 * \note Assumes the pri->lock is already obtained.
1997 * \retval array-index into private pointer array on success.
1998 * \retval -1 on error.
2000 static int pri_find_empty_nobch(struct sig_pri_span *pri)
2004 for (idx = 0; idx < pri->numchans; ++idx) {
2006 && pri->pvts[idx]->no_b_channel
2007 && sig_pri_is_chan_available(pri->pvts[idx])) {
2008 ast_debug(1, "Found empty available no B channel interface\n");
2013 /* Need to create a new interface. */
2014 if (sig_pri_callbacks.new_nobch_intf) {
2015 idx = sig_pri_callbacks.new_nobch_intf(pri);
2021 #endif /* defined(HAVE_PRI_CALL_HOLD) */
2023 static void *do_idle_thread(void *v_pvt)
2025 struct sig_pri_chan *pvt = v_pvt;
2026 struct ast_channel *chan = pvt->owner;
2027 struct ast_frame *f;
2029 /* Wait up to 30 seconds for an answer */
2030 int timeout_ms = 30000;
2032 struct timeval start;
2035 if ((callid = ast_channel_callid(chan))) {
2036 ast_callid_threadassoc_add(callid);
2039 ast_verb(3, "Initiating idle call on channel %s\n", ast_channel_name(chan));
2040 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
2041 if (ast_call(chan, ex, 0)) {
2042 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", ast_channel_name(chan), ex);
2046 start = ast_tvnow();
2047 while ((ms = ast_remaining_ms(start, timeout_ms))) {
2048 if (ast_waitfor(chan, ms) <= 0) {
2057 if (f->frametype == AST_FRAME_CONTROL) {
2058 switch (f->subclass.integer) {
2059 case AST_CONTROL_ANSWER:
2060 /* Launch the PBX */
2061 ast_channel_exten_set(chan, pvt->pri->idleext);
2062 ast_channel_context_set(chan, pvt->pri->idlecontext);
2063 ast_channel_priority_set(chan, 1);
2064 ast_verb(4, "Idle channel '%s' answered, sending to %s@%s\n", ast_channel_name(chan), ast_channel_exten(chan), ast_channel_context(chan));
2066 /* It's already hungup, return immediately */
2068 case AST_CONTROL_BUSY:
2069 ast_verb(4, "Idle channel '%s' busy, waiting...\n", ast_channel_name(chan));
2071 case AST_CONTROL_CONGESTION:
2072 ast_verb(4, "Idle channel '%s' congested, waiting...\n", ast_channel_name(chan));
2078 /* Hangup the channel since nothing happend */
2083 static void *pri_ss_thread(void *data)
2085 struct sig_pri_chan *p = data;
2086 struct ast_channel *chan = p->owner;
2087 char exten[AST_MAX_EXTENSION];
2094 /* We lost the owner before we could get started. */
2098 if ((callid = ast_channel_callid(chan))) {
2099 ast_callid_threadassoc_add(callid);
2103 * In the bizarre case where the channel has become a zombie before we
2104 * even get started here, abort safely.
2106 if (!ast_channel_tech_pvt(chan)) {
2107 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", ast_channel_name(chan));
2112 ast_verb(3, "Starting simple switch on '%s'\n", ast_channel_name(chan));
2114 sig_pri_dsp_reset_and_flush_digits(p);
2116 /* Now loop looking for an extension */
2117 ast_copy_string(exten, p->exten, sizeof(exten));
2118 len = strlen(exten);
2120 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2121 if (len && !ast_ignore_pattern(ast_channel_context(chan), exten))
2122 sig_pri_play_tone(p, -1);
2124 sig_pri_play_tone(p, SIG_PRI_TONE_DIALTONE);
2125 if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num))
2126 timeout = pri_matchdigittimeout;
2128 timeout = pri_gendigittimeout;
2129 res = ast_waitfordigit(chan, timeout);
2131 ast_debug(1, "waitfordigit returned < 0...\n");
2140 /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
2141 if (ast_strlen_zero(exten)) {
2142 ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
2146 ast_free(ast_channel_dialed(chan)->number.str);
2147 ast_channel_dialed(chan)->number.str = ast_strdup(exten);
2149 if (p->pri->append_msn_to_user_tag && p->pri->nodetype != PRI_NETWORK) {
2151 * Update the user tag for party id's from this device for this call
2152 * now that we have a complete MSN from the network.
2154 snprintf(p->user_tag, sizeof(p->user_tag), "%s_%s", p->pri->initial_user_tag,
2156 ast_free(ast_channel_caller(chan)->id.tag);
2157 ast_channel_caller(chan)->id.tag = ast_strdup(p->user_tag);
2160 sig_pri_play_tone(p, -1);
2161 if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2162 /* Start the real PBX */
2163 ast_channel_exten_set(chan, exten);
2164 sig_pri_dsp_reset_and_flush_digits(p);
2165 #if defined(JIRA_ASTERISK_15594)
2167 * Conditionaled out this code to effectively revert the JIRA
2168 * ASTERISK-15594 change. It breaks overlap dialing through
2169 * Asterisk. There is not enough information available at this
2170 * point to know if dialing is complete. The
2171 * ast_exists_extension(), ast_matchmore_extension(), and
2172 * ast_canmatch_extension() calls are not adequate to detect a
2173 * dial through extension pattern of "_9!".
2175 * Workaround is to use the dialplan Proceeding() application
2176 * early on non-dial through extensions.
2178 if ((p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
2179 && !ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2180 sig_pri_lock_private(p);
2182 pri_grab(p, p->pri);
2183 if (p->call_level < SIG_PRI_CALL_LEVEL_PROCEEDING) {
2184 p->call_level = SIG_PRI_CALL_LEVEL_PROCEEDING;
2186 pri_proceeding(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 0);
2189 sig_pri_unlock_private(p);
2191 #endif /* defined(JIRA_ASTERISK_15594) */
2193 sig_pri_set_echocanceller(p, 1);
2194 ast_channel_lock(chan);
2195 ast_setstate(chan, AST_STATE_RING);
2196 ast_channel_unlock(chan);
2197 res = ast_pbx_run(chan);
2199 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
2202 ast_debug(1, "No such possible extension '%s' in context '%s'\n", exten, ast_channel_context(chan));
2203 ast_channel_hangupcause_set(chan, AST_CAUSE_UNALLOCATED);
2206 /* Since we send release complete here, we won't get one */
2208 ast_mutex_lock(&p->pri->lock);
2209 sig_pri_span_devstate_changed(p->pri);
2210 ast_mutex_unlock(&p->pri->lock);
2215 void pri_event_alarm(struct sig_pri_span *pri, int index, int before_start_pri)
2217 pri->dchanavail[index] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
2218 if (!before_start_pri) {
2219 pri_find_dchan(pri);
2223 void pri_event_noalarm(struct sig_pri_span *pri, int index, int before_start_pri)
2225 pri->dchanavail[index] |= DCHAN_NOTINALARM;
2226 if (!before_start_pri)
2227 pri_restart(pri->dchans[index]);
2232 * \brief Convert libpri party name into asterisk party name.
2235 * \param ast_name Asterisk party name structure to fill. Must already be set initialized.
2236 * \param pri_name libpri party name structure containing source information.
2238 * \note The filled in ast_name structure needs to be destroyed by
2239 * ast_party_name_free() when it is no longer needed.
2243 static void sig_pri_party_name_convert(struct ast_party_name *ast_name, const struct pri_party_name *pri_name)
2245 ast_name->str = ast_strdup(pri_name->str);
2246 ast_name->char_set = pri_to_ast_char_set(pri_name->char_set);
2247 ast_name->presentation = pri_to_ast_presentation(pri_name->presentation);
2248 ast_name->valid = 1;
2253 * \brief Convert libpri party number into asterisk party number.
2256 * \param ast_number Asterisk party number structure to fill. Must already be set initialized.
2257 * \param pri_number libpri party number structure containing source information.
2258 * \param pri PRI span control structure.
2260 * \note The filled in ast_number structure needs to be destroyed by
2261 * ast_party_number_free() when it is no longer needed.
2265 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)
2267 char number[AST_MAX_EXTENSION];
2269 apply_plan_to_existing_number(number, sizeof(number), pri, pri_number->str,
2271 ast_number->str = ast_strdup(number);
2272 ast_number->plan = pri_number->plan;
2273 ast_number->presentation = pri_to_ast_presentation(pri_number->presentation);
2274 ast_number->valid = 1;
2279 * \brief Convert libpri party id into asterisk party id.
2282 * \param ast_id Asterisk party id structure to fill. Must already be set initialized.
2283 * \param pri_id libpri party id structure containing source information.
2284 * \param pri PRI span control structure.
2286 * \note The filled in ast_id structure needs to be destroyed by
2287 * ast_party_id_free() when it is no longer needed.
2291 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)
2293 if (pri_id->name.valid) {
2294 sig_pri_party_name_convert(&ast_id->name, &pri_id->name);
2296 if (pri_id->number.valid) {
2297 sig_pri_party_number_convert(&ast_id->number, &pri_id->number, pri);
2299 #if defined(HAVE_PRI_SUBADDR)
2300 if (pri_id->subaddress.valid) {
2301 sig_pri_set_subaddress(&ast_id->subaddress, &pri_id->subaddress);
2303 #endif /* defined(HAVE_PRI_SUBADDR) */
2308 * \brief Convert libpri redirecting information into asterisk redirecting information.
2311 * \param ast_redirecting Asterisk redirecting structure to fill.
2312 * \param pri_redirecting libpri redirecting structure containing source information.
2313 * \param ast_guide Asterisk redirecting structure to use as an initialization guide.
2314 * \param pri PRI span control structure.
2316 * \note The filled in ast_redirecting structure needs to be destroyed by
2317 * ast_party_redirecting_free() when it is no longer needed.
2321 static void sig_pri_redirecting_convert(struct ast_party_redirecting *ast_redirecting,
2322 const struct pri_party_redirecting *pri_redirecting,
2323 const struct ast_party_redirecting *ast_guide,
2324 struct sig_pri_span *pri)
2326 ast_party_redirecting_set_init(ast_redirecting, ast_guide);
2328 sig_pri_party_id_convert(&ast_redirecting->orig, &pri_redirecting->orig_called, pri);
2329 sig_pri_party_id_convert(&ast_redirecting->from, &pri_redirecting->from, pri);
2330 sig_pri_party_id_convert(&ast_redirecting->to, &pri_redirecting->to, pri);
2331 ast_redirecting->count = pri_redirecting->count;
2332 ast_redirecting->reason.code = pri_to_ast_reason(pri_redirecting->reason);
2333 ast_redirecting->orig_reason.code = pri_to_ast_reason(pri_redirecting->orig_reason);
2338 * \brief Determine if the given extension matches one of the MSNs in the pattern list.
2341 * \param msn_patterns Comma separated list of MSN patterns to match.
2342 * \param exten Extension to match in the MSN list.
2344 * \retval 1 if matches.
2345 * \retval 0 if no match.
2347 static int sig_pri_msn_match(const char *msn_patterns, const char *exten)
2353 msn_list = ast_strdupa(msn_patterns);
2356 pattern = strtok_r(msn_list, ",", &list_tail);
2358 pattern = ast_strip(pattern);
2359 if (!ast_strlen_zero(pattern) && ast_extension_match(pattern, exten)) {
2360 /* Extension matched the pattern. */
2363 pattern = strtok_r(NULL, ",", &list_tail);
2365 /* Did not match any pattern in the list. */
2369 #if defined(HAVE_PRI_MCID)
2370 static void party_number_json_to_ami(struct ast_str **msg, const char *prefix, struct ast_json *number)
2372 const char *num_txt, *pres_txt;
2375 ast_str_append(msg, 0,
2379 prefix, prefix, prefix);
2383 num_txt = ast_json_string_get(ast_json_object_get(number, "number"));
2384 plan = ast_json_integer_get(ast_json_object_get(number, "plan"));
2385 pres = ast_json_integer_get(ast_json_object_get(number, "presentation"));
2386 pres_txt = ast_json_string_get(ast_json_object_get(number, "presentation_txt"));
2388 ast_str_append(msg, 0, "%sNumValid: 1\r\n", prefix);
2389 ast_str_append(msg, 0, "%sNum: %s\r\n", prefix, num_txt);
2390 ast_str_append(msg, 0, "%ston: %d\r\n", prefix, plan);
2391 ast_str_append(msg, 0, "%sNumPlan: %d\r\n", prefix, plan);
2392 ast_str_append(msg, 0, "%sNumPres: %d (%s)\r\n", prefix, pres, pres_txt);
2395 static void party_name_json_to_ami(struct ast_str **msg, const char *prefix, struct ast_json *name)
2397 const char *name_txt, *pres_txt, *charset;
2400 ast_str_append(msg, 0,
2401 "%sNameValid: 0\r\n"
2407 name_txt = ast_json_string_get(ast_json_object_get(name, "name"));
2408 charset = ast_json_string_get(ast_json_object_get(name, "character_set"));
2409 pres = ast_json_integer_get(ast_json_object_get(name, "presentation"));
2410 pres_txt = ast_json_string_get(ast_json_object_get(name, "presentation_txt"));
2412 ast_str_append(msg, 0, "%sNameValid: 1\r\n", prefix);
2413 ast_str_append(msg, 0, "%sName: %s\r\n", prefix, name_txt);
2414 ast_str_append(msg, 0, "%sNameCharSet: %s\r\n", prefix, charset);
2415 ast_str_append(msg, 0, "%sNamePres: %d (%s)\r\n", prefix, pres, pres_txt);
2418 static void party_subaddress_json_to_ami(struct ast_str **msg, const char *prefix, struct ast_json *subaddress)
2420 const char *subaddress_txt, *type_txt;
2426 subaddress_txt = ast_json_string_get(ast_json_object_get(subaddress, "subaddress"));
2427 type_txt = ast_json_string_get(ast_json_object_get(subaddress, "type"));
2428 odd = ast_json_is_true(ast_json_object_get(subaddress, "odd")) ? 1 : 0;
2430 ast_str_append(msg, 0, "%sSubaddr: %s\r\n", prefix, subaddress_txt);
2431 ast_str_append(msg, 0, "%sSubaddrType: %s\r\n", prefix, type_txt);
2432 ast_str_append(msg, 0, "%sSubaddrOdd: %d\r\n", prefix, odd);
2437 * \brief Append the given JSON party id to the event string.
2440 * \param msg Event message string being built.
2441 * \param prefix Prefix to add to the party id lines.
2442 * \param party Party information to encode.
2446 static void party_json_to_ami(struct ast_str **msg, const char *prefix, struct ast_json *party)
2448 struct ast_json *presentation = ast_json_object_get(party, "presentation");
2449 struct ast_json *presentation_txt = ast_json_object_get(party, "presentation_txt");
2450 struct ast_json *name = ast_json_object_get(party, "name");
2451 struct ast_json *number = ast_json_object_get(party, "number");
2452 struct ast_json *subaddress = ast_json_object_get(party, "subaddress");
2454 /* Combined party presentation */
2455 ast_str_append(msg, 0, "%sPres: %jd (%s)\r\n", prefix,
2456 ast_json_integer_get(presentation),
2457 ast_json_string_get(presentation_txt));
2460 party_number_json_to_ami(msg, prefix, number);
2463 party_name_json_to_ami(msg, prefix, name);
2465 /* Party subaddress */
2466 party_subaddress_json_to_ami(msg, prefix, subaddress);
2469 static struct ast_manager_event_blob *mcid_to_ami(struct stasis_message *msg)
2471 RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
2472 RAII_VAR(struct ast_str *, party_string, ast_str_create(256), ast_free);
2473 struct ast_channel_blob *obj = stasis_message_data(msg);
2475 if (obj->snapshot) {
2476 channel_string = ast_manager_build_channel_state_string(obj->snapshot);
2477 if (!channel_string) {
2482 party_json_to_ami(&party_string, "MCallerID", ast_json_object_get(obj->blob, "caller"));
2483 party_json_to_ami(&party_string, "MConnectedID", ast_json_object_get(obj->blob, "connected"));
2485 return ast_manager_event_blob_create(EVENT_FLAG_CALL, "MCID",
2488 S_COR(obj->snapshot, ast_str_buffer(channel_string), ""), ast_str_buffer(party_string));
2491 STASIS_MESSAGE_TYPE_DEFN_LOCAL(mcid_type,
2492 .to_ami = mcid_to_ami,
2495 static void send_mcid(struct ast_channel *chan, struct ast_party_id *caller, struct ast_party_id *connected)
2497 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2499 ast_assert(caller != NULL);
2500 ast_assert(connected != NULL);
2502 blob = ast_json_pack("{s: o, s: o}",
2503 "caller", ast_json_party_id(caller),
2504 "connected", ast_json_party_id(connected));
2509 ast_channel_publish_blob(chan, mcid_type(), blob);
2514 * \brief Handle the MCID event.
2517 * \param pri PRI span control structure.
2518 * \param mcid MCID event parameters.
2519 * \param owner Asterisk channel associated with the call.
2520 * NULL if Asterisk no longer has the ast_channel struct.
2522 * \note Assumes the pri->lock is already obtained.
2523 * \note Assumes the owner channel lock is already obtained if still present.
2527 static void sig_pri_mcid_event(struct sig_pri_span *pri, const struct pri_subcmd_mcid_req *mcid, struct ast_channel *owner)
2529 struct ast_party_id caller_party;
2530 struct ast_party_id connected_party;
2532 /* Always use libpri's called party information. */
2533 ast_party_id_init(&connected_party);
2534 sig_pri_party_id_convert(&connected_party, &mcid->answerer, pri);
2537 * The owner channel is present.
2538 * Pass the event to the peer as well.
2540 ast_queue_control(owner, AST_CONTROL_MCID);
2542 send_mcid(owner, &ast_channel_connected(owner)->id, &connected_party);
2545 * Since we no longer have an owner channel,
2546 * we have to use the caller information supplied by libpri.
2548 ast_party_id_init(&caller_party);
2549 sig_pri_party_id_convert(&caller_party, &mcid->originator, pri);
2550 send_mcid(owner, &caller_party, &connected_party);
2551 ast_party_id_free(&caller_party);
2553 ast_party_id_free(&connected_party);
2555 #endif /* defined(HAVE_PRI_MCID) */
2557 #if defined(HAVE_PRI_TRANSFER)
2558 struct xfer_rsp_data {
2559 struct sig_pri_span *pri;
2560 /*! Call to send transfer success/fail response over. */
2562 /*! Invocation ID to use when sending a reply to the transfer request. */
2564 /*! TRUE if the transfer response has been made. */
2567 #endif /* defined(HAVE_PRI_TRANSFER) */
2569 #if defined(HAVE_PRI_TRANSFER)
2572 * \brief Send the transfer success/fail response message.
2575 * \param rsp Transfer response data.
2576 * \param is_successful TRUE if the transfer was successful.
2578 * \note Assumes the rsp->pri->lock is already obtained.
2582 static void sig_pri_transfer_rsp(struct xfer_rsp_data *rsp, int is_successful)
2584 if (rsp->responded) {
2589 pri_transfer_rsp(rsp->pri->pri, rsp->call, rsp->invoke_id, is_successful);
2591 #endif /* defined(HAVE_PRI_TRANSFER) */
2593 #if defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER)
2596 * \brief Attempt to transfer the two calls to each other.
2599 * \param pri PRI span control structure.
2600 * \param call_1_pri First call involved in the transfer. (transferee; usually on hold)
2601 * \param call_1_held TRUE if call_1_pri is on hold.
2602 * \param call_2_pri Second call involved in the transfer. (target; usually active/ringing)
2603 * \param call_2_held TRUE if call_2_pri is on hold.
2604 * \param xfer_data Transfer response data if non-NULL.
2606 * \note Assumes the pri->lock is already obtained.
2608 * \retval 0 on success.
2609 * \retval -1 on error.
2611 static int sig_pri_attempt_transfer(struct sig_pri_span *pri, q931_call *call_1_pri, int call_1_held, q931_call *call_2_pri, int call_2_held, struct xfer_rsp_data *xfer_data)
2613 struct attempt_xfer_call {
2615 struct ast_channel *ast;
2620 enum ast_transfer_result xfer_res;
2621 struct attempt_xfer_call *call_1;
2622 struct attempt_xfer_call *call_2;
2623 struct attempt_xfer_call c1;
2624 struct attempt_xfer_call c2;
2626 c1.pri = call_1_pri;
2627 c1.held = call_1_held;
2630 c2.pri = call_2_pri;
2631 c2.held = call_2_held;
2634 call_1->chanpos = pri_find_principle_by_call(pri, call_1->pri);
2635 call_2->chanpos = pri_find_principle_by_call(pri, call_2->pri);
2636 if (call_1->chanpos < 0 || call_2->chanpos < 0) {
2637 /* Calls not found in span control. */
2638 #if defined(HAVE_PRI_TRANSFER)
2640 /* Transfer failed. */
2641 sig_pri_transfer_rsp(xfer_data, 0);
2643 #endif /* defined(HAVE_PRI_TRANSFER) */
2647 /* Get call_1 owner. */
2648 sig_pri_lock_private(pri->pvts[call_1->chanpos]);
2649 sig_pri_lock_owner(pri, call_1->chanpos);
2650 call_1->ast = pri->pvts[call_1->chanpos]->owner;
2652 ast_channel_ref(call_1->ast);
2653 ast_channel_unlock(call_1->ast);
2655 sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
2657 /* Get call_2 owner. */
2658 sig_pri_lock_private(pri->pvts[call_2->chanpos]);
2659 sig_pri_lock_owner(pri, call_2->chanpos);
2660 call_2->ast = pri->pvts[call_2->chanpos]->owner;
2662 ast_channel_ref(call_2->ast);
2663 ast_channel_unlock(call_2->ast);
2665 sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
2667 if (!call_1->ast || !call_2->ast) {
2668 /* At least one owner is not present. */
2670 ast_channel_unref(call_1->ast);
2673 ast_channel_unref(call_2->ast);
2675 #if defined(HAVE_PRI_TRANSFER)
2677 /* Transfer failed. */
2678 sig_pri_transfer_rsp(xfer_data, 0);
2680 #endif /* defined(HAVE_PRI_TRANSFER) */
2684 ast_verb(3, "TRANSFERRING %s to %s\n",
2685 ast_channel_name(call_1->ast), ast_channel_name(call_2->ast));
2687 #if defined(HAVE_PRI_TRANSFER)
2690 * Add traps on the transferer channels in case threading causes
2691 * them to hangup before ast_bridge_transfer_attended() returns
2692 * and we can get the pri->lock back.
2694 sig_pri_lock_private(pri->pvts[call_1->chanpos]);
2695 pri->pvts[call_1->chanpos]->xfer_data = xfer_data;
2696 sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
2697 sig_pri_lock_private(pri->pvts[call_2->chanpos]);
2698 pri->pvts[call_2->chanpos]->xfer_data = xfer_data;
2699 sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
2701 #endif /* defined(HAVE_PRI_TRANSFER) */
2703 ast_mutex_unlock(&pri->lock);
2704 xfer_res = ast_bridge_transfer_attended(call_1->ast, call_2->ast);
2705 ast_mutex_lock(&pri->lock);
2706 retval = (xfer_res != AST_BRIDGE_TRANSFER_SUCCESS) ? -1 : 0;
2708 #if defined(HAVE_PRI_TRANSFER)
2713 * Remove the transferrer channel traps.
2715 * We must refind chanpos because we released pri->lock.
2717 rsp_chanpos = pri_find_principle_by_call(pri, call_1->pri);
2718 if (0 <= rsp_chanpos) {
2719 sig_pri_lock_private(pri->pvts[rsp_chanpos]);
2720 pri->pvts[rsp_chanpos]->xfer_data = NULL;
2721 sig_pri_unlock_private(pri->pvts[rsp_chanpos]);
2723 rsp_chanpos = pri_find_principle_by_call(pri, call_2->pri);
2724 if (0 <= rsp_chanpos) {
2725 sig_pri_lock_private(pri->pvts[rsp_chanpos]);
2726 pri->pvts[rsp_chanpos]->xfer_data = NULL;
2727 sig_pri_unlock_private(pri->pvts[rsp_chanpos]);
2730 /* Report transfer status. */
2731 sig_pri_transfer_rsp(xfer_data, retval ? 0 : 1);
2733 #endif /* defined(HAVE_PRI_TRANSFER) */
2734 ast_channel_unref(call_1->ast);
2735 ast_channel_unref(call_2->ast);
2738 #endif /* defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER) */
2740 #if defined(HAVE_PRI_CCSS)
2743 * \brief Compare the CC agent private data by libpri cc_id.
2746 * \param obj pointer to the (user-defined part) of an object.
2747 * \param arg callback argument from ao2_callback()
2748 * \param flags flags from ao2_callback()
2750 * \return values are a combination of enum _cb_results.
2752 static int sig_pri_cc_agent_cmp_cc_id(void *obj, void *arg, int flags)
2754 struct ast_cc_agent *agent_1 = obj;
2755 struct sig_pri_cc_agent_prv *agent_prv_1 = agent_1->private_data;
2756 struct sig_pri_cc_agent_prv *agent_prv_2 = arg;
2758 return (agent_prv_1 && agent_prv_1->pri == agent_prv_2->pri
2759 && agent_prv_1->cc_id == agent_prv_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
2761 #endif /* defined(HAVE_PRI_CCSS) */
2763 #if defined(HAVE_PRI_CCSS)
2766 * \brief Find the CC agent by libpri cc_id.
2769 * \param pri PRI span control structure.
2770 * \param cc_id CC record ID to find.
2773 * Since agents are refcounted, and this function returns
2774 * a reference to the agent, it is imperative that you decrement
2775 * the refcount of the agent once you have finished using it.
2777 * \retval agent on success.
2778 * \retval NULL not found.
2780 static struct ast_cc_agent *sig_pri_find_cc_agent_by_cc_id(struct sig_pri_span *pri, long cc_id)
2782 struct sig_pri_cc_agent_prv finder = {
2787 return ast_cc_agent_callback(0, sig_pri_cc_agent_cmp_cc_id, &finder,
2788 sig_pri_cc_type_name);
2790 #endif /* defined(HAVE_PRI_CCSS) */
2792 #if defined(HAVE_PRI_CCSS)
2795 * \brief Compare the CC monitor instance by libpri cc_id.
2798 * \param obj pointer to the (user-defined part) of an object.
2799 * \param arg callback argument from ao2_callback()
2800 * \param flags flags from ao2_callback()
2802 * \return values are a combination of enum _cb_results.
2804 static int sig_pri_cc_monitor_cmp_cc_id(void *obj, void *arg, int flags)
2806 struct sig_pri_cc_monitor_instance *monitor_1 = obj;
2807 struct sig_pri_cc_monitor_instance *monitor_2 = arg;
2809 return (monitor_1->pri == monitor_2->pri
2810 && monitor_1->cc_id == monitor_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
2812 #endif /* defined(HAVE_PRI_CCSS) */
2814 #if defined(HAVE_PRI_CCSS)
2817 * \brief Find the CC monitor instance by libpri cc_id.
2820 * \param pri PRI span control structure.
2821 * \param cc_id CC record ID to find.
2824 * Since monitor_instances are refcounted, and this function returns
2825 * a reference to the instance, it is imperative that you decrement
2826 * the refcount of the instance once you have finished using it.
2828 * \retval monitor_instance on success.
2829 * \retval NULL not found.
2831 static struct sig_pri_cc_monitor_instance *sig_pri_find_cc_monitor_by_cc_id(struct sig_pri_span *pri, long cc_id)
2833 struct sig_pri_cc_monitor_instance finder = {
2838 return ao2_callback(sig_pri_cc_monitors, 0, sig_pri_cc_monitor_cmp_cc_id, &finder);
2840 #endif /* defined(HAVE_PRI_CCSS) */
2842 #if defined(HAVE_PRI_CCSS)
2845 * \brief Destroy the given monitor instance.
2848 * \param data Monitor instance to destroy.
2852 static void sig_pri_cc_monitor_instance_destroy(void *data)
2854 struct sig_pri_cc_monitor_instance *monitor_instance = data;
2856 if (monitor_instance->cc_id != -1) {
2857 ast_mutex_lock(&monitor_instance->pri->lock);
2858 pri_cc_cancel(monitor_instance->pri->pri, monitor_instance->cc_id);
2859 ast_mutex_unlock(&monitor_instance->pri->lock);
2861 sig_pri_callbacks.module_unref();
2863 #endif /* defined(HAVE_PRI_CCSS) */
2865 #if defined(HAVE_PRI_CCSS)
2868 * \brief Construct a new monitor instance.
2871 * \param core_id CC core ID.
2872 * \param pri PRI span control structure.
2873 * \param cc_id CC record ID.
2874 * \param device_name Name of device (Asterisk channel name less sequence number).
2877 * Since monitor_instances are refcounted, and this function returns
2878 * a reference to the instance, it is imperative that you decrement
2879 * the refcount of the instance once you have finished using it.
2881 * \retval monitor_instance on success.
2882 * \retval NULL on error.
2884 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)
2886 struct sig_pri_cc_monitor_instance *monitor_instance;
2888 if (!sig_pri_callbacks.module_ref || !sig_pri_callbacks.module_unref) {
2892 monitor_instance = ao2_alloc(sizeof(*monitor_instance) + strlen(device_name),
2893 sig_pri_cc_monitor_instance_destroy);
2894 if (!monitor_instance) {
2898 monitor_instance->cc_id = cc_id;
2899 monitor_instance->pri = pri;
2900 monitor_instance->core_id = core_id;
2901 strcpy(monitor_instance->name, device_name);
2903 sig_pri_callbacks.module_ref();
2905 ao2_link(sig_pri_cc_monitors, monitor_instance);
2906 return monitor_instance;
2908 #endif /* defined(HAVE_PRI_CCSS) */
2910 #if defined(HAVE_PRI_CCSS)
2913 * \brief Announce to the CC core that protocol CC monitor is available for this call.
2916 * \param pri PRI span control structure.
2917 * \param chanpos Channel position in the span.
2918 * \param cc_id CC record ID.
2919 * \param service CCBS/CCNR indication.
2921 * \note Assumes the pri->lock is already obtained.
2922 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
2923 * \note Assumes the sig_pri_lock_owner(pri, chanpos) is already obtained.
2925 * \retval 0 on success.
2926 * \retval -1 on error.
2928 static int sig_pri_cc_available(struct sig_pri_span *pri, int chanpos, long cc_id, enum ast_cc_service_type service)
2930 struct sig_pri_chan *pvt;
2931 struct ast_cc_config_params *cc_params;
2932 struct sig_pri_cc_monitor_instance *monitor;
2933 enum ast_cc_monitor_policies monitor_policy;
2936 char device_name[AST_CHANNEL_NAME];
2937 char dialstring[AST_CHANNEL_NAME];
2939 pvt = pri->pvts[chanpos];
2941 core_id = ast_cc_get_current_core_id(pvt->owner);
2942 if (core_id == -1) {
2946 cc_params = ast_channel_get_cc_config_params(pvt->owner);
2952 monitor_policy = ast_get_cc_monitor_policy(cc_params);
2953 switch (monitor_policy) {
2954 case AST_CC_MONITOR_NEVER:
2955 /* CCSS is not enabled. */
2957 case AST_CC_MONITOR_NATIVE:
2958 case AST_CC_MONITOR_ALWAYS:
2960 * If it is AST_CC_MONITOR_ALWAYS and native fails we will attempt the fallback
2961 * later in the call to sig_pri_cc_generic_check().
2963 ast_channel_get_device_name(pvt->owner, device_name, sizeof(device_name));
2964 sig_pri_make_cc_dialstring(pvt, dialstring, sizeof(dialstring));
2965 monitor = sig_pri_cc_monitor_instance_init(core_id, pri, cc_id, device_name);
2969 res = ast_queue_cc_frame(pvt->owner, sig_pri_cc_type_name, dialstring, service,
2972 monitor->cc_id = -1;
2973 ao2_unlink(sig_pri_cc_monitors, monitor);
2974 ao2_ref(monitor, -1);
2977 case AST_CC_MONITOR_GENERIC:
2978 ast_queue_cc_frame(pvt->owner, AST_CC_GENERIC_MONITOR_TYPE,
2979 sig_pri_get_orig_dialstring(pvt), service, NULL);
2980 /* Say it failed to force caller to cancel native CC. */
2985 #endif /* defined(HAVE_PRI_CCSS) */
2989 * \brief Check if generic CC monitor is needed and request it.
2992 * \param pri PRI span control structure.
2993 * \param chanpos Channel position in the span.
2994 * \param service CCBS/CCNR indication.
2996 * \note Assumes the pri->lock is already obtained.
2997 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
3001 static void sig_pri_cc_generic_check(struct sig_pri_span *pri, int chanpos, enum ast_cc_service_type service)
3003 struct ast_channel *owner;
3004 struct ast_cc_config_params *cc_params;
3005 #if defined(HAVE_PRI_CCSS)
3006 struct ast_cc_monitor *monitor;
3007 char device_name[AST_CHANNEL_NAME];
3008 #endif /* defined(HAVE_PRI_CCSS) */
3009 enum ast_cc_monitor_policies monitor_policy;
3012 if (!pri->pvts[chanpos]->outgoing) {
3013 /* This is not an outgoing call so it cannot be CC monitor. */
3017 sig_pri_lock_owner(pri, chanpos);
3018 owner = pri->pvts[chanpos]->owner;
3022 core_id = ast_cc_get_current_core_id(owner);
3023 if (core_id == -1) {
3024 /* No CC core setup */
3028 cc_params = ast_channel_get_cc_config_params(owner);
3030 /* Could not get CC config parameters. */
3034 #if defined(HAVE_PRI_CCSS)
3035 ast_channel_get_device_name(owner, device_name, sizeof(device_name));
3036 monitor = ast_cc_get_monitor_by_recall_core_id(core_id, device_name);
3038 /* CC monitor is already present so no need for generic CC. */
3039 ao2_ref(monitor, -1);
3042 #endif /* defined(HAVE_PRI_CCSS) */
3044 monitor_policy = ast_get_cc_monitor_policy(cc_params);
3045 switch (monitor_policy) {
3046 case AST_CC_MONITOR_NEVER:
3047 /* CCSS is not enabled. */
3049 case AST_CC_MONITOR_NATIVE:
3050 if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
3051 /* Request generic CC monitor. */
3052 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
3053 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
3056 case AST_CC_MONITOR_ALWAYS:
3057 if (pri->sig == SIG_BRI_PTMP && pri->nodetype != PRI_NETWORK) {
3059 * Cannot monitor PTMP TE side since this is not defined.
3060 * We are playing the roll of a phone in this case and
3061 * a phone cannot monitor a party over the network without
3067 * We are either falling back or this is a PTMP NT span.
3068 * Request generic CC monitor.
3070 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
3071 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
3073 case AST_CC_MONITOR_GENERIC:
3074 if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
3075 /* Request generic CC monitor. */
3076 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
3077 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
3083 ast_channel_unlock(owner);
3086 #if defined(HAVE_PRI_CCSS)
3089 * \brief The CC link canceled the CC instance.
3092 * \param pri PRI span control structure.
3093 * \param cc_id CC record ID.
3094 * \param is_agent TRUE if the cc_id is for an agent.
3098 static void sig_pri_cc_link_canceled(struct sig_pri_span *pri, long cc_id, int is_agent)
3101 struct ast_cc_agent *agent;
3103 agent = sig_pri_find_cc_agent_by_cc_id(pri, cc_id);
3107 ast_cc_failed(agent->core_id, "%s agent got canceled by link",
3108 sig_pri_cc_type_name);
3111 struct sig_pri_cc_monitor_instance *monitor;
3113 monitor = sig_pri_find_cc_monitor_by_cc_id(pri, cc_id);
3117 monitor->cc_id = -1;
3118 ast_cc_monitor_failed(monitor->core_id, monitor->name,
3119 "%s monitor got canceled by link", sig_pri_cc_type_name);
3120 ao2_ref(monitor, -1);
3123 #endif /* defined(HAVE_PRI_CCSS) */
3125 #if defined(HAVE_PRI_AOC_EVENTS)
3128 * \brief Convert ast_aoc_charged_item to PRI_AOC_CHARGED_ITEM .
3131 * \param value Value to convert to string.
3133 * \return PRI_AOC_CHARGED_ITEM
3135 static enum PRI_AOC_CHARGED_ITEM sig_pri_aoc_charged_item_to_pri(enum PRI_AOC_CHARGED_ITEM value)
3138 case AST_AOC_CHARGED_ITEM_NA:
3139 return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
3140 case AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
3141 return PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
3142 case AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
3143 return PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
3144 case AST_AOC_CHARGED_ITEM_CALL_ATTEMPT:
3145 return PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT;
3146 case AST_AOC_CHARGED_ITEM_CALL_SETUP:
3147 return PRI_AOC_CHARGED_ITEM_CALL_SETUP;
3148 case AST_AOC_CHARGED_ITEM_USER_USER_INFO:
3149 return PRI_AOC_CHARGED_ITEM_USER_USER_INFO;
3150 case AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
3151 return PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
3153 return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
3155 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3157 #if defined(HAVE_PRI_AOC_EVENTS)
3160 * \brief Convert PRI_AOC_CHARGED_ITEM to ast_aoc_charged_item.
3163 * \param value Value to convert to string.
3165 * \return ast_aoc_charged_item
3167 static enum ast_aoc_s_charged_item sig_pri_aoc_charged_item_to_ast(enum PRI_AOC_CHARGED_ITEM value)
3170 case PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE:
3171 return AST_AOC_CHARGED_ITEM_NA;
3172 case PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
3173 return AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
3174 case PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
3175 return AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
3176 case PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT:
3177 return AST_AOC_CHARGED_ITEM_CALL_ATTEMPT;
3178 case PRI_AOC_CHARGED_ITEM_CALL_SETUP:
3179 return AST_AOC_CHARGED_ITEM_CALL_SETUP;
3180 case PRI_AOC_CHARGED_ITEM_USER_USER_INFO:
3181 return AST_AOC_CHARGED_ITEM_USER_USER_INFO;
3182 case PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
3183 return AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
3185 return AST_AOC_CHARGED_ITEM_NA;
3187 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3189 #if defined(HAVE_PRI_AOC_EVENTS)
3192 * \brief Convert AST_AOC_MULTIPLER to PRI_AOC_MULTIPLIER.
3195 * \return pri enum equivalent.
3197 static int sig_pri_aoc_multiplier_from_ast(enum ast_aoc_currency_multiplier mult)
3200 case AST_AOC_MULT_ONETHOUSANDTH:
3201 return PRI_AOC_MULTIPLIER_THOUSANDTH;
3202 case AST_AOC_MULT_ONEHUNDREDTH:
3203 return PRI_AOC_MULTIPLIER_HUNDREDTH;
3204 case AST_AOC_MULT_ONETENTH:
3205 return PRI_AOC_MULTIPLIER_TENTH;
3206 case AST_AOC_MULT_ONE:
3207 return PRI_AOC_MULTIPLIER_ONE;
3208 case AST_AOC_MULT_TEN:
3209 return PRI_AOC_MULTIPLIER_TEN;
3210 case AST_AOC_MULT_HUNDRED:
3211 return PRI_AOC_MULTIPLIER_HUNDRED;
3212 case AST_AOC_MULT_THOUSAND:
3213 return PRI_AOC_MULTIPLIER_THOUSAND;
3215 return PRI_AOC_MULTIPLIER_ONE;
3218 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3220 #if defined(HAVE_PRI_AOC_EVENTS)
3223 * \brief Convert PRI_AOC_MULTIPLIER to AST_AOC_MULTIPLIER
3226 * \return ast enum equivalent.
3228 static int sig_pri_aoc_multiplier_from_pri(const int mult)
3231 case PRI_AOC_MULTIPLIER_THOUSANDTH:
3232 return AST_AOC_MULT_ONETHOUSANDTH;
3233 case PRI_AOC_MULTIPLIER_HUNDREDTH:
3234 return AST_AOC_MULT_ONEHUNDREDTH;
3235 case PRI_AOC_MULTIPLIER_TENTH:
3236 return AST_AOC_MULT_ONETENTH;
3237 case PRI_AOC_MULTIPLIER_ONE:
3238 return AST_AOC_MULT_ONE;
3239 case PRI_AOC_MULTIPLIER_TEN:
3240 return AST_AOC_MULT_TEN;
3241 case PRI_AOC_MULTIPLIER_HUNDRED:
3242 return AST_AOC_MULT_HUNDRED;
3243 case PRI_AOC_MULTIPLIER_THOUSAND:
3244 return AST_AOC_MULT_THOUSAND;
3246 return AST_AOC_MULT_ONE;
3249 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3251 #if defined(HAVE_PRI_AOC_EVENTS)
3254 * \brief Convert ast_aoc_time_scale representation to PRI_AOC_TIME_SCALE
3257 * \param value Value to convert to ast representation
3259 * \return PRI_AOC_TIME_SCALE
3261 static enum PRI_AOC_TIME_SCALE sig_pri_aoc_scale_to_pri(enum ast_aoc_time_scale value)
3265 case AST_AOC_TIME_SCALE_HUNDREDTH_SECOND:
3266 return PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3267 case AST_AOC_TIME_SCALE_TENTH_SECOND:
3268 return PRI_AOC_TIME_SCALE_TENTH_SECOND;
3269 case AST_AOC_TIME_SCALE_SECOND:
3270 return PRI_AOC_TIME_SCALE_SECOND;
3271 case AST_AOC_TIME_SCALE_TEN_SECOND:
3272 return PRI_AOC_TIME_SCALE_TEN_SECOND;
3273 case AST_AOC_TIME_SCALE_MINUTE:
3274 return PRI_AOC_TIME_SCALE_MINUTE;
3275 case AST_AOC_TIME_SCALE_HOUR:
3276 return PRI_AOC_TIME_SCALE_HOUR;
3277 case AST_AOC_TIME_SCALE_DAY:
3278 return PRI_AOC_TIME_SCALE_DAY;
3281 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3283 #if defined(HAVE_PRI_AOC_EVENTS)
3286 * \brief Convert PRI_AOC_TIME_SCALE to ast aoc representation
3289 * \param value Value to convert to ast representation
3291 * \return ast aoc time scale
3293 static enum ast_aoc_time_scale sig_pri_aoc_scale_to_ast(enum PRI_AOC_TIME_SCALE value)
3297 case PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND:
3298 return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3299 case PRI_AOC_TIME_SCALE_TENTH_SECOND:
3300 return AST_AOC_TIME_SCALE_TENTH_SECOND;
3301 case PRI_AOC_TIME_SCALE_SECOND:
3302 return AST_AOC_TIME_SCALE_SECOND;
3303 case PRI_AOC_TIME_SCALE_TEN_SECOND:
3304 return AST_AOC_TIME_SCALE_TEN_SECOND;
3305 case PRI_AOC_TIME_SCALE_MINUTE:
3306 return AST_AOC_TIME_SCALE_MINUTE;
3307 case PRI_AOC_TIME_SCALE_HOUR:
3308 return AST_AOC_TIME_SCALE_HOUR;
3309 case PRI_AOC_TIME_SCALE_DAY:
3310 return AST_AOC_TIME_SCALE_DAY;
3312 return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3314 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3316 #if defined(HAVE_PRI_AOC_EVENTS)
3319 * \brief Handle AOC-S control frame
3322 * \param aoc_s AOC-S event parameters.
3323 * \param owner Asterisk channel associated with the call.
3324 * \param passthrough indicating if this message should be queued on the ast channel
3326 * \note Assumes the pri->lock is already obtained.
3327 * \note Assumes the sig_pri private is locked
3328 * \note Assumes the owner channel lock is already obtained.
3332 static void sig_pri_aoc_s_from_pri(const struct pri_subcmd_aoc_s *aoc_s, struct ast_channel *owner, int passthrough)
3334 struct ast_aoc_decoded *decoded = NULL;
3335 struct ast_aoc_encoded *encoded = NULL;
3336 size_t encoded_size = 0;
3339 if (!owner || !aoc_s) {
3343 if (!(decoded = ast_aoc_create(AST_AOC_S, 0, 0))) {
3347 for (idx = 0; idx < aoc_s->num_items; ++idx) {
3348 enum ast_aoc_s_charged_item charged_item;
3350 charged_item = sig_pri_aoc_charged_item_to_ast(aoc_s->item[idx].chargeable);
3351 if (charged_item == AST_AOC_CHARGED_ITEM_NA) {
3352 /* Delete the unknown charged item from the list. */
3355 switch (aoc_s->item[idx].rate_type) {
3356 case PRI_AOC_RATE_TYPE_DURATION:
3357 ast_aoc_s_add_rate_duration(decoded,
3359 aoc_s->item[idx].rate.duration.amount.cost,
3360 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.duration.amount.multiplier),
3361 aoc_s->item[idx].rate.duration.currency,
3362 aoc_s->item[idx].rate.duration.time.length,
3363 sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.time.scale),
3364 aoc_s->item[idx].rate.duration.granularity.length,
3365 sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.granularity.scale),
3366 aoc_s->item[idx].rate.duration.charging_type);
3368 case PRI_AOC_RATE_TYPE_FLAT:
3369 ast_aoc_s_add_rate_flat(decoded,
3371 aoc_s->item[idx].rate.flat.amount.cost,