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, "%02x", (unsigned)pri_subaddress->data[x]);
760 if (pri_subaddress->odd_even_indicator) {
762 sprintf(ptr, "%01x", (unsigned)((pri_subaddress->data[len]) >> 4));
765 sprintf(ptr, "%02x", (unsigned)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 int ulaw, 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, ulaw, exten, assignedids, requestor);
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 Kill the call.
1458 * \param pri PRI span control structure.
1459 * \param call LibPRI opaque call pointer to find.
1460 * \param cause Reason call was killed.
1462 * \note Assumes the pvt->pri->lock is already obtained.
1466 static void sig_pri_kill_call(struct sig_pri_span *pri, q931_call *call, int cause)
1470 chanpos = pri_find_principle_by_call(pri, call);
1472 pri_hangup(pri->pri, call, cause);
1475 sig_pri_lock_private(pri->pvts[chanpos]);
1476 if (!pri->pvts[chanpos]->owner) {
1477 pri_hangup(pri->pri, call, cause);
1478 pri->pvts[chanpos]->call = NULL;
1479 sig_pri_unlock_private(pri->pvts[chanpos]);
1480 sig_pri_span_devstate_changed(pri);
1483 ast_channel_hangupcause_set(pri->pvts[chanpos]->owner, cause);
1484 pri_queue_control(pri, chanpos, AST_CONTROL_HANGUP);
1485 sig_pri_unlock_private(pri->pvts[chanpos]);
1490 * \brief Find the private structure for the libpri call.
1492 * \param pri PRI span control structure.
1493 * \param channel LibPRI encoded channel ID.
1494 * \param call LibPRI opaque call pointer.
1496 * \note Assumes the pri->lock is already obtained.
1498 * \retval array-index into private pointer array on success.
1499 * \retval -1 on error.
1501 static int pri_find_principle(struct sig_pri_span *pri, int channel, q931_call *call)
1509 /* Channel is not picked yet. */
1513 prioffset = PRI_CHANNEL(channel);
1514 if (!prioffset || (channel & PRI_HELD_CALL)) {
1515 /* Find the call waiting call or held call. */
1516 return pri_find_principle_by_call(pri, call);
1519 span = PRI_SPAN(channel);
1520 if (!(channel & PRI_EXPLICIT)) {
1523 index = pri_active_dchan_index(pri);
1527 span = pri->dchan_logical_span[index];
1531 for (x = 0; x < pri->numchans; x++) {
1533 && pri->pvts[x]->prioffset == prioffset
1534 && pri->pvts[x]->logicalspan == span
1535 && !pri->pvts[x]->no_b_channel) {
1546 * \brief Fixup the private structure associated with the libpri call.
1548 * \param pri PRI span control structure.
1549 * \param principle Array-index into private array to move call to if not already there.
1550 * \param call LibPRI opaque call pointer to find if need to move call.
1552 * \note Assumes the pri->lock is already obtained.
1554 * \retval principle on success.
1555 * \retval -1 on error.
1557 static int pri_fixup_principle(struct sig_pri_span *pri, int principle, q931_call *call)
1561 if (principle < 0 || pri->numchans <= principle) {
1569 if (pri->pvts[principle] && pri->pvts[principle]->call == call) {
1570 /* Call is already on the specified principle. */
1574 /* Find the old principle location. */
1575 for (x = 0; x < pri->numchans; x++) {
1576 struct sig_pri_chan *new_chan;
1577 struct sig_pri_chan *old_chan;
1579 if (!pri->pvts[x] || pri->pvts[x]->call != call) {
1583 /* Found our call */
1584 new_chan = pri->pvts[principle];
1585 old_chan = pri->pvts[x];
1587 /* Get locks to safely move to the new private structure. */
1588 sig_pri_lock_private(old_chan);
1589 sig_pri_lock_owner(pri, x);
1590 sig_pri_lock_private(new_chan);
1592 ast_verb(3, "Moving call (%s) from channel %d to %d.\n",
1593 old_chan->owner ? ast_channel_name(old_chan->owner) : "",
1594 old_chan->channel, new_chan->channel);
1595 if (!sig_pri_is_chan_available(new_chan)) {
1596 ast_log(LOG_WARNING,
1597 "Can't move call (%s) from channel %d to %d. It is already in use.\n",
1598 old_chan->owner ? ast_channel_name(old_chan->owner) : "",
1599 old_chan->channel, new_chan->channel);
1600 sig_pri_unlock_private(new_chan);
1601 if (old_chan->owner) {
1602 ast_channel_unlock(old_chan->owner);
1604 sig_pri_unlock_private(old_chan);
1608 sig_pri_fixup_chans(old_chan, new_chan);
1610 /* Fix it all up now */
1611 new_chan->owner = old_chan->owner;
1612 old_chan->owner = NULL;
1614 new_chan->call = old_chan->call;
1615 old_chan->call = NULL;
1617 /* Transfer flags from the old channel. */
1618 #if defined(HAVE_PRI_AOC_EVENTS)
1619 new_chan->aoc_s_request_invoke_id_valid = old_chan->aoc_s_request_invoke_id_valid;
1620 new_chan->waiting_for_aoce = old_chan->waiting_for_aoce;
1621 new_chan->holding_aoce = old_chan->holding_aoce;
1622 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1623 new_chan->alreadyhungup = old_chan->alreadyhungup;
1624 new_chan->isidlecall = old_chan->isidlecall;
1625 new_chan->progress = old_chan->progress;
1626 new_chan->allocated = old_chan->allocated;
1627 new_chan->outgoing = old_chan->outgoing;
1628 new_chan->digital = old_chan->digital;
1629 #if defined(HAVE_PRI_CALL_WAITING)
1630 new_chan->is_call_waiting = old_chan->is_call_waiting;
1631 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1633 #if defined(HAVE_PRI_AOC_EVENTS)
1634 old_chan->aoc_s_request_invoke_id_valid = 0;
1635 old_chan->waiting_for_aoce = 0;
1636 old_chan->holding_aoce = 0;
1637 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1638 old_chan->alreadyhungup = 0;
1639 old_chan->isidlecall = 0;
1640 old_chan->progress = 0;
1641 old_chan->allocated = 0;
1642 old_chan->outgoing = 0;
1643 old_chan->digital = 0;
1644 #if defined(HAVE_PRI_CALL_WAITING)
1645 old_chan->is_call_waiting = 0;
1646 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1648 /* More stuff to transfer to the new channel. */
1649 new_chan->call_level = old_chan->call_level;
1650 old_chan->call_level = SIG_PRI_CALL_LEVEL_IDLE;
1651 #if defined(HAVE_PRI_REVERSE_CHARGE)
1652 new_chan->reverse_charging_indication = old_chan->reverse_charging_indication;
1653 #endif /* defined(HAVE_PRI_REVERSE_CHARGE) */
1654 #if defined(HAVE_PRI_SETUP_KEYPAD)
1655 strcpy(new_chan->keypad_digits, old_chan->keypad_digits);
1656 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
1657 strcpy(new_chan->deferred_digits, old_chan->deferred_digits);
1658 strcpy(new_chan->moh_suggested, old_chan->moh_suggested);
1659 new_chan->moh_state = old_chan->moh_state;
1660 old_chan->moh_state = SIG_PRI_MOH_STATE_IDLE;
1661 #if defined(HAVE_PRI_TRANSFER)
1662 new_chan->xfer_data = old_chan->xfer_data;
1663 old_chan->xfer_data = NULL;
1664 #endif /* defined(HAVE_PRI_TRANSFER) */
1666 #if defined(HAVE_PRI_AOC_EVENTS)
1667 new_chan->aoc_s_request_invoke_id = old_chan->aoc_s_request_invoke_id;
1668 new_chan->aoc_e = old_chan->aoc_e;
1669 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1670 strcpy(new_chan->user_tag, old_chan->user_tag);
1672 if (new_chan->no_b_channel) {
1673 /* Copy the real channel configuration to the no B channel interface. */
1674 new_chan->hidecallerid = old_chan->hidecallerid;
1675 new_chan->hidecalleridname = old_chan->hidecalleridname;
1676 new_chan->immediate = old_chan->immediate;
1677 new_chan->priexclusive = old_chan->priexclusive;
1678 new_chan->priindication_oob = old_chan->priindication_oob;
1679 new_chan->use_callerid = old_chan->use_callerid;
1680 new_chan->use_callingpres = old_chan->use_callingpres;
1681 new_chan->stripmsd = old_chan->stripmsd;
1682 strcpy(new_chan->context, old_chan->context);
1683 strcpy(new_chan->mohinterpret, old_chan->mohinterpret);
1685 /* Become a member of the old channel span/trunk-group. */
1686 new_chan->logicalspan = old_chan->logicalspan;
1687 new_chan->mastertrunkgroup = old_chan->mastertrunkgroup;
1688 } else if (old_chan->no_b_channel) {
1690 * We are transitioning from a held/call-waiting channel to a
1691 * real channel so we need to make sure that the media path is
1692 * open. (Needed especially if the channel is natively
1695 sig_pri_open_media(new_chan);
1698 if (new_chan->owner) {
1699 sig_pri_ami_channel_event(new_chan);
1702 sig_pri_unlock_private(old_chan);
1703 if (new_chan->owner) {
1704 ast_channel_unlock(new_chan->owner);
1706 sig_pri_unlock_private(new_chan);
1710 ast_verb(3, "Call specified, but not found.\n");
1716 * \brief Find and fixup the private structure associated with the libpri call.
1718 * \param pri PRI span control structure.
1719 * \param channel LibPRI encoded channel ID.
1720 * \param call LibPRI opaque call pointer.
1723 * This is a combination of pri_find_principle() and pri_fixup_principle()
1724 * to reduce code redundancy and to make handling several PRI_EVENT_xxx's
1725 * consistent for the current architecture.
1727 * \note Assumes the pri->lock is already obtained.
1729 * \retval array-index into private pointer array on success.
1730 * \retval -1 on error.
1732 static int pri_find_fixup_principle(struct sig_pri_span *pri, int channel, q931_call *call)
1736 chanpos = pri_find_principle(pri, channel, call);
1738 ast_log(LOG_WARNING, "Span %d: PRI requested channel %d/%d is unconfigured.\n",
1739 pri->span, PRI_SPAN(channel), PRI_CHANNEL(channel));
1740 sig_pri_kill_call(pri, call, PRI_CAUSE_IDENTIFIED_CHANNEL_NOTEXIST);
1743 chanpos = pri_fixup_principle(pri, chanpos, call);
1745 ast_log(LOG_WARNING, "Span %d: PRI requested channel %d/%d is not available.\n",
1746 pri->span, PRI_SPAN(channel), PRI_CHANNEL(channel));
1748 * Using Q.931 section 5.2.3.1 b) as the reason for picking
1749 * PRI_CAUSE_CHANNEL_UNACCEPTABLE. Receiving a
1750 * PRI_CAUSE_REQUESTED_CHAN_UNAVAIL would cause us to restart
1751 * that channel (which is not specified by Q.931) and kill some
1752 * other call which would be bad.
1754 sig_pri_kill_call(pri, call, PRI_CAUSE_CHANNEL_UNACCEPTABLE);
1760 static char * redirectingreason2str(int redirectingreason)
1762 switch (redirectingreason) {
1770 return "UNCONDITIONAL";
1772 return "NOREDIRECT";
1776 static char *dialplan2str(int dialplan)
1778 if (dialplan == -1) {
1779 return("Dynamically set dialplan in ISDN");
1781 return (pri_plan2str(dialplan));
1786 * \brief Apply numbering plan prefix to the given number.
1788 * \param buf Buffer to put number into.
1789 * \param size Size of given buffer.
1790 * \param pri PRI span control structure.
1791 * \param number Number to apply numbering plan.
1792 * \param plan Numbering plan to apply.
1796 static void apply_plan_to_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, int plan)
1799 case PRI_INTERNATIONAL_ISDN: /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */
1800 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
1802 case PRI_NATIONAL_ISDN: /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */
1803 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
1805 case PRI_LOCAL_ISDN: /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */
1806 snprintf(buf, size, "%s%s", pri->localprefix, number);
1808 case PRI_PRIVATE: /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */
1809 snprintf(buf, size, "%s%s", pri->privateprefix, number);
1811 case PRI_UNKNOWN: /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */
1812 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
1814 default: /* other Q.931 dialplan => don't twiddle with callingnum */
1815 snprintf(buf, size, "%s", number);
1822 * \brief Apply numbering plan prefix to the given number if the number exists.
1824 * \param buf Buffer to put number into.
1825 * \param size Size of given buffer.
1826 * \param pri PRI span control structure.
1827 * \param number Number to apply numbering plan.
1828 * \param plan Numbering plan to apply.
1832 static void apply_plan_to_existing_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, int plan)
1834 /* Make sure a number exists so the prefix isn't placed on an empty string. */
1835 if (ast_strlen_zero(number)) {
1841 apply_plan_to_number(buf, size, pri, number, plan);
1846 * \brief Restart the next channel we think is idle on the span.
1848 * \param pri PRI span control structure.
1850 * \note Assumes the pri->lock is already obtained.
1854 static void pri_check_restart(struct sig_pri_span *pri)
1856 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1858 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1860 for (++pri->resetpos; pri->resetpos < pri->numchans; ++pri->resetpos) {
1861 if (!pri->pvts[pri->resetpos]
1862 || pri->pvts[pri->resetpos]->no_b_channel
1863 || sig_pri_is_chan_in_use(pri->pvts[pri->resetpos])) {
1866 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1867 why = pri->pvts[pri->resetpos]->service_status;
1870 "Span %d: channel %d out-of-service (reason: %s), not sending RESTART\n",
1871 pri->span, pri->pvts[pri->resetpos]->channel,
1872 (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
1875 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1878 if (pri->resetpos < pri->numchans) {
1879 /* Mark the channel as resetting and restart it */
1880 pri->pvts[pri->resetpos]->resetting = SIG_PRI_RESET_ACTIVE;
1881 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
1884 time(&pri->lastreset);
1885 sig_pri_span_devstate_changed(pri);
1889 #if defined(HAVE_PRI_CALL_WAITING)
1892 * \brief Init the private channel configuration using the span controller.
1895 * \param pvt Channel to init the configuration.
1896 * \param pri PRI span control structure.
1898 * \note Assumes the pri->lock is already obtained.
1902 static void sig_pri_init_config(struct sig_pri_chan *pvt, struct sig_pri_span *pri)
1904 pvt->stripmsd = pri->ch_cfg.stripmsd;
1905 pvt->hidecallerid = pri->ch_cfg.hidecallerid;
1906 pvt->hidecalleridname = pri->ch_cfg.hidecalleridname;
1907 pvt->immediate = pri->ch_cfg.immediate;
1908 pvt->priexclusive = pri->ch_cfg.priexclusive;
1909 pvt->priindication_oob = pri->ch_cfg.priindication_oob;
1910 pvt->use_callerid = pri->ch_cfg.use_callerid;
1911 pvt->use_callingpres = pri->ch_cfg.use_callingpres;
1912 ast_copy_string(pvt->context, pri->ch_cfg.context, sizeof(pvt->context));
1913 ast_copy_string(pvt->mohinterpret, pri->ch_cfg.mohinterpret, sizeof(pvt->mohinterpret));
1915 if (sig_pri_callbacks.init_config) {
1916 sig_pri_callbacks.init_config(pvt->chan_pvt, pri);
1919 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1923 * \brief Find an empty B-channel interface to use.
1925 * \param pri PRI span control structure.
1926 * \param backwards TRUE if the search starts from higher channels.
1928 * \note Assumes the pri->lock is already obtained.
1930 * \retval array-index into private pointer array on success.
1931 * \retval -1 on error.
1933 static int pri_find_empty_chan(struct sig_pri_span *pri, int backwards)
1941 if (backwards && (x < 0))
1943 if (!backwards && (x >= pri->numchans))
1946 && !pri->pvts[x]->no_b_channel
1947 && sig_pri_is_chan_available(pri->pvts[x])) {
1948 ast_debug(1, "Found empty available channel %d/%d\n",
1949 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
1960 #if defined(HAVE_PRI_CALL_HOLD)
1963 * \brief Find or create an empty no-B-channel interface to use.
1966 * \param pri PRI span control structure.
1968 * \note Assumes the pri->lock is already obtained.
1970 * \retval array-index into private pointer array on success.
1971 * \retval -1 on error.
1973 static int pri_find_empty_nobch(struct sig_pri_span *pri)
1977 for (idx = 0; idx < pri->numchans; ++idx) {
1979 && pri->pvts[idx]->no_b_channel
1980 && sig_pri_is_chan_available(pri->pvts[idx])) {
1981 ast_debug(1, "Found empty available no B channel interface\n");
1986 /* Need to create a new interface. */
1987 if (sig_pri_callbacks.new_nobch_intf) {
1988 idx = sig_pri_callbacks.new_nobch_intf(pri);
1994 #endif /* defined(HAVE_PRI_CALL_HOLD) */
1996 static void *do_idle_thread(void *v_pvt)
1998 struct sig_pri_chan *pvt = v_pvt;
1999 struct ast_channel *chan = pvt->owner;
2000 struct ast_frame *f;
2002 /* Wait up to 30 seconds for an answer */
2003 int timeout_ms = 30000;
2005 struct timeval start;
2006 struct ast_callid *callid;
2008 if ((callid = ast_channel_callid(chan))) {
2009 ast_callid_threadassoc_add(callid);
2010 callid = ast_callid_unref(callid);
2013 ast_verb(3, "Initiating idle call on channel %s\n", ast_channel_name(chan));
2014 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
2015 if (ast_call(chan, ex, 0)) {
2016 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", ast_channel_name(chan), ex);
2020 start = ast_tvnow();
2021 while ((ms = ast_remaining_ms(start, timeout_ms))) {
2022 if (ast_waitfor(chan, ms) <= 0) {
2031 if (f->frametype == AST_FRAME_CONTROL) {
2032 switch (f->subclass.integer) {
2033 case AST_CONTROL_ANSWER:
2034 /* Launch the PBX */
2035 ast_channel_exten_set(chan, pvt->pri->idleext);
2036 ast_channel_context_set(chan, pvt->pri->idlecontext);
2037 ast_channel_priority_set(chan, 1);
2038 ast_verb(4, "Idle channel '%s' answered, sending to %s@%s\n", ast_channel_name(chan), ast_channel_exten(chan), ast_channel_context(chan));
2040 /* It's already hungup, return immediately */
2042 case AST_CONTROL_BUSY:
2043 ast_verb(4, "Idle channel '%s' busy, waiting...\n", ast_channel_name(chan));
2045 case AST_CONTROL_CONGESTION:
2046 ast_verb(4, "Idle channel '%s' congested, waiting...\n", ast_channel_name(chan));
2052 /* Hangup the channel since nothing happend */
2057 static void *pri_ss_thread(void *data)
2059 struct sig_pri_chan *p = data;
2060 struct ast_channel *chan = p->owner;
2061 char exten[AST_MAX_EXTENSION];
2065 struct ast_callid *callid;
2068 /* We lost the owner before we could get started. */
2072 if ((callid = ast_channel_callid(chan))) {
2073 ast_callid_threadassoc_add(callid);
2074 ast_callid_unref(callid);
2078 * In the bizarre case where the channel has become a zombie before we
2079 * even get started here, abort safely.
2081 if (!ast_channel_tech_pvt(chan)) {
2082 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", ast_channel_name(chan));
2087 ast_verb(3, "Starting simple switch on '%s'\n", ast_channel_name(chan));
2089 sig_pri_dsp_reset_and_flush_digits(p);
2091 /* Now loop looking for an extension */
2092 ast_copy_string(exten, p->exten, sizeof(exten));
2093 len = strlen(exten);
2095 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2096 if (len && !ast_ignore_pattern(ast_channel_context(chan), exten))
2097 sig_pri_play_tone(p, -1);
2099 sig_pri_play_tone(p, SIG_PRI_TONE_DIALTONE);
2100 if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num))
2101 timeout = pri_matchdigittimeout;
2103 timeout = pri_gendigittimeout;
2104 res = ast_waitfordigit(chan, timeout);
2106 ast_debug(1, "waitfordigit returned < 0...\n");
2115 /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
2116 if (ast_strlen_zero(exten)) {
2117 ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
2121 ast_free(ast_channel_dialed(chan)->number.str);
2122 ast_channel_dialed(chan)->number.str = ast_strdup(exten);
2124 if (p->pri->append_msn_to_user_tag && p->pri->nodetype != PRI_NETWORK) {
2126 * Update the user tag for party id's from this device for this call
2127 * now that we have a complete MSN from the network.
2129 snprintf(p->user_tag, sizeof(p->user_tag), "%s_%s", p->pri->initial_user_tag,
2131 ast_free(ast_channel_caller(chan)->id.tag);
2132 ast_channel_caller(chan)->id.tag = ast_strdup(p->user_tag);
2135 sig_pri_play_tone(p, -1);
2136 if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2137 /* Start the real PBX */
2138 ast_channel_exten_set(chan, exten);
2139 sig_pri_dsp_reset_and_flush_digits(p);
2140 #if defined(ISSUE_16789)
2142 * Conditionaled out this code to effectively revert the Mantis
2143 * issue 16789 change. It breaks overlap dialing through
2144 * Asterisk. There is not enough information available at this
2145 * point to know if dialing is complete. The
2146 * ast_exists_extension(), ast_matchmore_extension(), and
2147 * ast_canmatch_extension() calls are not adequate to detect a
2148 * dial through extension pattern of "_9!".
2150 * Workaround is to use the dialplan Proceeding() application
2151 * early on non-dial through extensions.
2153 if ((p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
2154 && !ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2155 sig_pri_lock_private(p);
2157 pri_grab(p, p->pri);
2158 if (p->call_level < SIG_PRI_CALL_LEVEL_PROCEEDING) {
2159 p->call_level = SIG_PRI_CALL_LEVEL_PROCEEDING;
2161 pri_proceeding(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 0);
2164 sig_pri_unlock_private(p);
2166 #endif /* defined(ISSUE_16789) */
2168 sig_pri_set_echocanceller(p, 1);
2169 ast_channel_lock(chan);
2170 ast_setstate(chan, AST_STATE_RING);
2171 ast_channel_unlock(chan);
2172 res = ast_pbx_run(chan);
2174 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
2177 ast_debug(1, "No such possible extension '%s' in context '%s'\n", exten, ast_channel_context(chan));
2178 ast_channel_hangupcause_set(chan, AST_CAUSE_UNALLOCATED);
2181 /* Since we send release complete here, we won't get one */
2183 ast_mutex_lock(&p->pri->lock);
2184 sig_pri_span_devstate_changed(p->pri);
2185 ast_mutex_unlock(&p->pri->lock);
2190 void pri_event_alarm(struct sig_pri_span *pri, int index, int before_start_pri)
2192 pri->dchanavail[index] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
2193 if (!before_start_pri) {
2194 pri_find_dchan(pri);
2198 void pri_event_noalarm(struct sig_pri_span *pri, int index, int before_start_pri)
2200 pri->dchanavail[index] |= DCHAN_NOTINALARM;
2201 if (!before_start_pri)
2202 pri_restart(pri->dchans[index]);
2207 * \brief Convert libpri party name into asterisk party name.
2210 * \param ast_name Asterisk party name structure to fill. Must already be set initialized.
2211 * \param pri_name libpri party name structure containing source information.
2213 * \note The filled in ast_name structure needs to be destroyed by
2214 * ast_party_name_free() when it is no longer needed.
2218 static void sig_pri_party_name_convert(struct ast_party_name *ast_name, const struct pri_party_name *pri_name)
2220 ast_name->str = ast_strdup(pri_name->str);
2221 ast_name->char_set = pri_to_ast_char_set(pri_name->char_set);
2222 ast_name->presentation = pri_to_ast_presentation(pri_name->presentation);
2223 ast_name->valid = 1;
2228 * \brief Convert libpri party number into asterisk party number.
2231 * \param ast_number Asterisk party number structure to fill. Must already be set initialized.
2232 * \param pri_number libpri party number structure containing source information.
2233 * \param pri PRI span control structure.
2235 * \note The filled in ast_number structure needs to be destroyed by
2236 * ast_party_number_free() when it is no longer needed.
2240 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)
2242 char number[AST_MAX_EXTENSION];
2244 apply_plan_to_existing_number(number, sizeof(number), pri, pri_number->str,
2246 ast_number->str = ast_strdup(number);
2247 ast_number->plan = pri_number->plan;
2248 ast_number->presentation = pri_to_ast_presentation(pri_number->presentation);
2249 ast_number->valid = 1;
2254 * \brief Convert libpri party id into asterisk party id.
2257 * \param ast_id Asterisk party id structure to fill. Must already be set initialized.
2258 * \param pri_id libpri party id structure containing source information.
2259 * \param pri PRI span control structure.
2261 * \note The filled in ast_id structure needs to be destroyed by
2262 * ast_party_id_free() when it is no longer needed.
2266 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)
2268 if (pri_id->name.valid) {
2269 sig_pri_party_name_convert(&ast_id->name, &pri_id->name);
2271 if (pri_id->number.valid) {
2272 sig_pri_party_number_convert(&ast_id->number, &pri_id->number, pri);
2274 #if defined(HAVE_PRI_SUBADDR)
2275 if (pri_id->subaddress.valid) {
2276 sig_pri_set_subaddress(&ast_id->subaddress, &pri_id->subaddress);
2278 #endif /* defined(HAVE_PRI_SUBADDR) */
2283 * \brief Convert libpri redirecting information into asterisk redirecting information.
2286 * \param ast_redirecting Asterisk redirecting structure to fill.
2287 * \param pri_redirecting libpri redirecting structure containing source information.
2288 * \param ast_guide Asterisk redirecting structure to use as an initialization guide.
2289 * \param pri PRI span control structure.
2291 * \note The filled in ast_redirecting structure needs to be destroyed by
2292 * ast_party_redirecting_free() when it is no longer needed.
2296 static void sig_pri_redirecting_convert(struct ast_party_redirecting *ast_redirecting,
2297 const struct pri_party_redirecting *pri_redirecting,
2298 const struct ast_party_redirecting *ast_guide,
2299 struct sig_pri_span *pri)
2301 ast_party_redirecting_set_init(ast_redirecting, ast_guide);
2303 sig_pri_party_id_convert(&ast_redirecting->orig, &pri_redirecting->orig_called, pri);
2304 sig_pri_party_id_convert(&ast_redirecting->from, &pri_redirecting->from, pri);
2305 sig_pri_party_id_convert(&ast_redirecting->to, &pri_redirecting->to, pri);
2306 ast_redirecting->count = pri_redirecting->count;
2307 ast_redirecting->reason.code = pri_to_ast_reason(pri_redirecting->reason);
2308 ast_redirecting->orig_reason.code = pri_to_ast_reason(pri_redirecting->orig_reason);
2313 * \brief Determine if the given extension matches one of the MSNs in the pattern list.
2316 * \param msn_patterns Comma separated list of MSN patterns to match.
2317 * \param exten Extension to match in the MSN list.
2319 * \retval 1 if matches.
2320 * \retval 0 if no match.
2322 static int sig_pri_msn_match(const char *msn_patterns, const char *exten)
2328 msn_list = ast_strdupa(msn_patterns);
2331 pattern = strtok_r(msn_list, ",", &list_tail);
2333 pattern = ast_strip(pattern);
2334 if (!ast_strlen_zero(pattern) && ast_extension_match(pattern, exten)) {
2335 /* Extension matched the pattern. */
2338 pattern = strtok_r(NULL, ",", &list_tail);
2340 /* Did not match any pattern in the list. */
2344 #if defined(HAVE_PRI_MCID)
2345 static void party_number_json_to_ami(struct ast_str **msg, const char *prefix, struct ast_json *number)
2347 const char *num_txt, *pres_txt;
2350 ast_str_append(msg, 0,
2354 prefix, prefix, prefix);
2358 num_txt = ast_json_string_get(ast_json_object_get(number, "number"));
2359 plan = ast_json_integer_get(ast_json_object_get(number, "plan"));
2360 pres = ast_json_integer_get(ast_json_object_get(number, "presentation"));
2361 pres_txt = ast_json_string_get(ast_json_object_get(number, "presentation_txt"));
2363 ast_str_append(msg, 0, "%sNumValid: 1\r\n", prefix);
2364 ast_str_append(msg, 0, "%sNum: %s\r\n", prefix, num_txt);
2365 ast_str_append(msg, 0, "%ston: %d\r\n", prefix, plan);
2366 ast_str_append(msg, 0, "%sNumPlan: %d\r\n", prefix, plan);
2367 ast_str_append(msg, 0, "%sNumPres: %d (%s)\r\n", prefix, pres, pres_txt);
2370 static void party_name_json_to_ami(struct ast_str **msg, const char *prefix, struct ast_json *name)
2372 const char *name_txt, *pres_txt, *charset;
2375 ast_str_append(msg, 0,
2376 "%sNameValid: 0\r\n"
2382 name_txt = ast_json_string_get(ast_json_object_get(name, "name"));
2383 charset = ast_json_string_get(ast_json_object_get(name, "character_set"));
2384 pres = ast_json_integer_get(ast_json_object_get(name, "presentation"));
2385 pres_txt = ast_json_string_get(ast_json_object_get(name, "presentation_txt"));
2387 ast_str_append(msg, 0, "%sNameValid: 1\r\n", prefix);
2388 ast_str_append(msg, 0, "%sName: %s\r\n", prefix, name_txt);
2389 ast_str_append(msg, 0, "%sNameCharSet: %s\r\n", prefix, charset);
2390 ast_str_append(msg, 0, "%sNamePres: %d (%s)\r\n", prefix, pres, pres_txt);
2393 static void party_subaddress_json_to_ami(struct ast_str **msg, const char *prefix, struct ast_json *subaddress)
2395 const char *subaddress_txt, *type_txt;
2401 subaddress_txt = ast_json_string_get(ast_json_object_get(subaddress, "subaddress"));
2402 type_txt = ast_json_string_get(ast_json_object_get(subaddress, "type"));
2403 odd = ast_json_is_true(ast_json_object_get(subaddress, "odd")) ? 1 : 0;
2405 ast_str_append(msg, 0, "%sSubaddr: %s\r\n", prefix, subaddress_txt);
2406 ast_str_append(msg, 0, "%sSubaddrType: %s\r\n", prefix, type_txt);
2407 ast_str_append(msg, 0, "%sSubaddrOdd: %d\r\n", prefix, odd);
2412 * \brief Append the given JSON party id to the event string.
2415 * \param msg Event message string being built.
2416 * \param prefix Prefix to add to the party id lines.
2417 * \param party Party information to encode.
2421 static void party_json_to_ami(struct ast_str **msg, const char *prefix, struct ast_json *party)
2423 struct ast_json *presentation = ast_json_object_get(party, "presentation");
2424 struct ast_json *presentation_txt = ast_json_object_get(party, "presentation_txt");
2425 struct ast_json *name = ast_json_object_get(party, "name");
2426 struct ast_json *number = ast_json_object_get(party, "number");
2427 struct ast_json *subaddress = ast_json_object_get(party, "subaddress");
2429 /* Combined party presentation */
2430 ast_str_append(msg, 0, "%sPres: %zd (%s)\r\n", prefix,
2431 ast_json_integer_get(presentation),
2432 ast_json_string_get(presentation_txt));
2435 party_number_json_to_ami(msg, prefix, number);
2438 party_name_json_to_ami(msg, prefix, name);
2440 /* Party subaddress */
2441 party_subaddress_json_to_ami(msg, prefix, subaddress);
2444 static struct ast_manager_event_blob *mcid_to_ami(struct stasis_message *msg)
2446 RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
2447 RAII_VAR(struct ast_str *, party_string, ast_str_create(256), ast_free);
2448 struct ast_channel_blob *obj = stasis_message_data(msg);
2450 if (obj->snapshot) {
2451 channel_string = ast_manager_build_channel_state_string(obj->snapshot);
2452 if (!channel_string) {
2457 party_json_to_ami(&party_string, "MCallerID", ast_json_object_get(obj->blob, "caller"));
2458 party_json_to_ami(&party_string, "MConnectedID", ast_json_object_get(obj->blob, "connected"));
2460 return ast_manager_event_blob_create(EVENT_FLAG_CALL, "MCID",
2463 S_COR(obj->snapshot, ast_str_buffer(channel_string), ""), ast_str_buffer(party_string));
2466 STASIS_MESSAGE_TYPE_DEFN_LOCAL(mcid_type,
2467 .to_ami = mcid_to_ami,
2470 static void send_mcid(struct ast_channel *chan, struct ast_party_id *caller, struct ast_party_id *connected)
2472 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2474 ast_assert(caller != NULL);
2475 ast_assert(connected != NULL);
2477 blob = ast_json_pack("{s: o, s: o}",
2478 "caller", ast_json_party_id(caller),
2479 "connected", ast_json_party_id(connected));
2484 ast_channel_publish_blob(chan, mcid_type(), blob);
2489 * \brief Handle the MCID event.
2492 * \param pri PRI span control structure.
2493 * \param mcid MCID event parameters.
2494 * \param owner Asterisk channel associated with the call.
2495 * NULL if Asterisk no longer has the ast_channel struct.
2497 * \note Assumes the pri->lock is already obtained.
2498 * \note Assumes the owner channel lock is already obtained if still present.
2502 static void sig_pri_mcid_event(struct sig_pri_span *pri, const struct pri_subcmd_mcid_req *mcid, struct ast_channel *owner)
2504 struct ast_party_id caller_party;
2505 struct ast_party_id connected_party;
2507 /* Always use libpri's called party information. */
2508 ast_party_id_init(&connected_party);
2509 sig_pri_party_id_convert(&connected_party, &mcid->answerer, pri);
2512 * The owner channel is present.
2513 * Pass the event to the peer as well.
2515 ast_queue_control(owner, AST_CONTROL_MCID);
2517 send_mcid(owner, &ast_channel_connected(owner)->id, &connected_party);
2520 * Since we no longer have an owner channel,
2521 * we have to use the caller information supplied by libpri.
2523 ast_party_id_init(&caller_party);
2524 sig_pri_party_id_convert(&caller_party, &mcid->originator, pri);
2525 send_mcid(owner, &caller_party, &connected_party);
2526 ast_party_id_free(&caller_party);
2528 ast_party_id_free(&connected_party);
2530 #endif /* defined(HAVE_PRI_MCID) */
2532 #if defined(HAVE_PRI_TRANSFER)
2533 struct xfer_rsp_data {
2534 struct sig_pri_span *pri;
2535 /*! Call to send transfer success/fail response over. */
2537 /*! Invocation ID to use when sending a reply to the transfer request. */
2539 /*! TRUE if the transfer response has been made. */
2542 #endif /* defined(HAVE_PRI_TRANSFER) */
2544 #if defined(HAVE_PRI_TRANSFER)
2547 * \brief Send the transfer success/fail response message.
2550 * \param rsp Transfer response data.
2551 * \param is_successful TRUE if the transfer was successful.
2553 * \note Assumes the rsp->pri->lock is already obtained.
2557 static void sig_pri_transfer_rsp(struct xfer_rsp_data *rsp, int is_successful)
2559 if (rsp->responded) {
2564 pri_transfer_rsp(rsp->pri->pri, rsp->call, rsp->invoke_id, is_successful);
2566 #endif /* defined(HAVE_PRI_TRANSFER) */
2568 #if defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER)
2571 * \brief Attempt to transfer the two calls to each other.
2574 * \param pri PRI span control structure.
2575 * \param call_1_pri First call involved in the transfer. (transferee; usually on hold)
2576 * \param call_1_held TRUE if call_1_pri is on hold.
2577 * \param call_2_pri Second call involved in the transfer. (target; usually active/ringing)
2578 * \param call_2_held TRUE if call_2_pri is on hold.
2579 * \param xfer_data Transfer response data if non-NULL.
2581 * \note Assumes the pri->lock is already obtained.
2583 * \retval 0 on success.
2584 * \retval -1 on error.
2586 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)
2588 struct attempt_xfer_call {
2590 struct ast_channel *ast;
2595 enum ast_transfer_result xfer_res;
2596 struct attempt_xfer_call *call_1;
2597 struct attempt_xfer_call *call_2;
2598 struct attempt_xfer_call c1;
2599 struct attempt_xfer_call c2;
2601 c1.pri = call_1_pri;
2602 c1.held = call_1_held;
2605 c2.pri = call_2_pri;
2606 c2.held = call_2_held;
2609 call_1->chanpos = pri_find_principle_by_call(pri, call_1->pri);
2610 call_2->chanpos = pri_find_principle_by_call(pri, call_2->pri);
2611 if (call_1->chanpos < 0 || call_2->chanpos < 0) {
2612 /* Calls not found in span control. */
2613 #if defined(HAVE_PRI_TRANSFER)
2615 /* Transfer failed. */
2616 sig_pri_transfer_rsp(xfer_data, 0);
2618 #endif /* defined(HAVE_PRI_TRANSFER) */
2622 /* Get call_1 owner. */
2623 sig_pri_lock_private(pri->pvts[call_1->chanpos]);
2624 sig_pri_lock_owner(pri, call_1->chanpos);
2625 call_1->ast = pri->pvts[call_1->chanpos]->owner;
2627 ast_channel_ref(call_1->ast);
2628 ast_channel_unlock(call_1->ast);
2630 sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
2632 /* Get call_2 owner. */
2633 sig_pri_lock_private(pri->pvts[call_2->chanpos]);
2634 sig_pri_lock_owner(pri, call_2->chanpos);
2635 call_2->ast = pri->pvts[call_2->chanpos]->owner;
2637 ast_channel_ref(call_2->ast);
2638 ast_channel_unlock(call_2->ast);
2640 sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
2642 if (!call_1->ast || !call_2->ast) {
2643 /* At least one owner is not present. */
2645 ast_channel_unref(call_1->ast);
2648 ast_channel_unref(call_2->ast);
2650 #if defined(HAVE_PRI_TRANSFER)
2652 /* Transfer failed. */
2653 sig_pri_transfer_rsp(xfer_data, 0);
2655 #endif /* defined(HAVE_PRI_TRANSFER) */
2659 ast_verb(3, "TRANSFERRING %s to %s\n",
2660 ast_channel_name(call_1->ast), ast_channel_name(call_2->ast));
2662 #if defined(HAVE_PRI_TRANSFER)
2665 * Add traps on the transferer channels in case threading causes
2666 * them to hangup before ast_bridge_transfer_attended() returns
2667 * and we can get the pri->lock back.
2669 sig_pri_lock_private(pri->pvts[call_1->chanpos]);
2670 pri->pvts[call_1->chanpos]->xfer_data = xfer_data;
2671 sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
2672 sig_pri_lock_private(pri->pvts[call_2->chanpos]);
2673 pri->pvts[call_2->chanpos]->xfer_data = xfer_data;
2674 sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
2676 #endif /* defined(HAVE_PRI_TRANSFER) */
2678 ast_mutex_unlock(&pri->lock);
2679 xfer_res = ast_bridge_transfer_attended(call_1->ast, call_2->ast);
2680 ast_mutex_lock(&pri->lock);
2681 retval = (xfer_res != AST_BRIDGE_TRANSFER_SUCCESS) ? -1 : 0;
2683 #if defined(HAVE_PRI_TRANSFER)
2688 * Remove the transferrer channel traps.
2690 * We must refind chanpos because we released pri->lock.
2692 rsp_chanpos = pri_find_principle_by_call(pri, call_1->pri);
2693 if (0 <= rsp_chanpos) {
2694 sig_pri_lock_private(pri->pvts[rsp_chanpos]);
2695 pri->pvts[rsp_chanpos]->xfer_data = NULL;
2696 sig_pri_unlock_private(pri->pvts[rsp_chanpos]);
2698 rsp_chanpos = pri_find_principle_by_call(pri, call_2->pri);
2699 if (0 <= rsp_chanpos) {
2700 sig_pri_lock_private(pri->pvts[rsp_chanpos]);
2701 pri->pvts[rsp_chanpos]->xfer_data = NULL;
2702 sig_pri_unlock_private(pri->pvts[rsp_chanpos]);
2705 /* Report transfer status. */
2706 sig_pri_transfer_rsp(xfer_data, retval ? 0 : 1);
2708 #endif /* defined(HAVE_PRI_TRANSFER) */
2709 ast_channel_unref(call_1->ast);
2710 ast_channel_unref(call_2->ast);
2713 #endif /* defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER) */
2715 #if defined(HAVE_PRI_CCSS)
2718 * \brief Compare the CC agent private data by libpri cc_id.
2721 * \param obj pointer to the (user-defined part) of an object.
2722 * \param arg callback argument from ao2_callback()
2723 * \param flags flags from ao2_callback()
2725 * \return values are a combination of enum _cb_results.
2727 static int sig_pri_cc_agent_cmp_cc_id(void *obj, void *arg, int flags)
2729 struct ast_cc_agent *agent_1 = obj;
2730 struct sig_pri_cc_agent_prv *agent_prv_1 = agent_1->private_data;
2731 struct sig_pri_cc_agent_prv *agent_prv_2 = arg;
2733 return (agent_prv_1 && agent_prv_1->pri == agent_prv_2->pri
2734 && agent_prv_1->cc_id == agent_prv_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
2736 #endif /* defined(HAVE_PRI_CCSS) */
2738 #if defined(HAVE_PRI_CCSS)
2741 * \brief Find the CC agent by libpri cc_id.
2744 * \param pri PRI span control structure.
2745 * \param cc_id CC record ID to find.
2748 * Since agents are refcounted, and this function returns
2749 * a reference to the agent, it is imperative that you decrement
2750 * the refcount of the agent once you have finished using it.
2752 * \retval agent on success.
2753 * \retval NULL not found.
2755 static struct ast_cc_agent *sig_pri_find_cc_agent_by_cc_id(struct sig_pri_span *pri, long cc_id)
2757 struct sig_pri_cc_agent_prv finder = {
2762 return ast_cc_agent_callback(0, sig_pri_cc_agent_cmp_cc_id, &finder,
2763 sig_pri_cc_type_name);
2765 #endif /* defined(HAVE_PRI_CCSS) */
2767 #if defined(HAVE_PRI_CCSS)
2770 * \brief Compare the CC monitor instance by libpri cc_id.
2773 * \param obj pointer to the (user-defined part) of an object.
2774 * \param arg callback argument from ao2_callback()
2775 * \param flags flags from ao2_callback()
2777 * \return values are a combination of enum _cb_results.
2779 static int sig_pri_cc_monitor_cmp_cc_id(void *obj, void *arg, int flags)
2781 struct sig_pri_cc_monitor_instance *monitor_1 = obj;
2782 struct sig_pri_cc_monitor_instance *monitor_2 = arg;
2784 return (monitor_1->pri == monitor_2->pri
2785 && monitor_1->cc_id == monitor_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
2787 #endif /* defined(HAVE_PRI_CCSS) */
2789 #if defined(HAVE_PRI_CCSS)
2792 * \brief Find the CC monitor instance by libpri cc_id.
2795 * \param pri PRI span control structure.
2796 * \param cc_id CC record ID to find.
2799 * Since monitor_instances are refcounted, and this function returns
2800 * a reference to the instance, it is imperative that you decrement
2801 * the refcount of the instance once you have finished using it.
2803 * \retval monitor_instance on success.
2804 * \retval NULL not found.
2806 static struct sig_pri_cc_monitor_instance *sig_pri_find_cc_monitor_by_cc_id(struct sig_pri_span *pri, long cc_id)
2808 struct sig_pri_cc_monitor_instance finder = {
2813 return ao2_callback(sig_pri_cc_monitors, 0, sig_pri_cc_monitor_cmp_cc_id, &finder);
2815 #endif /* defined(HAVE_PRI_CCSS) */
2817 #if defined(HAVE_PRI_CCSS)
2820 * \brief Destroy the given monitor instance.
2823 * \param data Monitor instance to destroy.
2827 static void sig_pri_cc_monitor_instance_destroy(void *data)
2829 struct sig_pri_cc_monitor_instance *monitor_instance = data;
2831 if (monitor_instance->cc_id != -1) {
2832 ast_mutex_lock(&monitor_instance->pri->lock);
2833 pri_cc_cancel(monitor_instance->pri->pri, monitor_instance->cc_id);
2834 ast_mutex_unlock(&monitor_instance->pri->lock);
2836 sig_pri_callbacks.module_unref();
2838 #endif /* defined(HAVE_PRI_CCSS) */
2840 #if defined(HAVE_PRI_CCSS)
2843 * \brief Construct a new monitor instance.
2846 * \param core_id CC core ID.
2847 * \param pri PRI span control structure.
2848 * \param cc_id CC record ID.
2849 * \param device_name Name of device (Asterisk channel name less sequence number).
2852 * Since monitor_instances are refcounted, and this function returns
2853 * a reference to the instance, it is imperative that you decrement
2854 * the refcount of the instance once you have finished using it.
2856 * \retval monitor_instance on success.
2857 * \retval NULL on error.
2859 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)
2861 struct sig_pri_cc_monitor_instance *monitor_instance;
2863 if (!sig_pri_callbacks.module_ref || !sig_pri_callbacks.module_unref) {
2867 monitor_instance = ao2_alloc(sizeof(*monitor_instance) + strlen(device_name),
2868 sig_pri_cc_monitor_instance_destroy);
2869 if (!monitor_instance) {
2873 monitor_instance->cc_id = cc_id;
2874 monitor_instance->pri = pri;
2875 monitor_instance->core_id = core_id;
2876 strcpy(monitor_instance->name, device_name);
2878 sig_pri_callbacks.module_ref();
2880 ao2_link(sig_pri_cc_monitors, monitor_instance);
2881 return monitor_instance;
2883 #endif /* defined(HAVE_PRI_CCSS) */
2885 #if defined(HAVE_PRI_CCSS)
2888 * \brief Announce to the CC core that protocol CC monitor is available for this call.
2891 * \param pri PRI span control structure.
2892 * \param chanpos Channel position in the span.
2893 * \param cc_id CC record ID.
2894 * \param service CCBS/CCNR indication.
2896 * \note Assumes the pri->lock is already obtained.
2897 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
2898 * \note Assumes the sig_pri_lock_owner(pri, chanpos) is already obtained.
2900 * \retval 0 on success.
2901 * \retval -1 on error.
2903 static int sig_pri_cc_available(struct sig_pri_span *pri, int chanpos, long cc_id, enum ast_cc_service_type service)
2905 struct sig_pri_chan *pvt;
2906 struct ast_cc_config_params *cc_params;
2907 struct sig_pri_cc_monitor_instance *monitor;
2908 enum ast_cc_monitor_policies monitor_policy;
2911 char device_name[AST_CHANNEL_NAME];
2912 char dialstring[AST_CHANNEL_NAME];
2914 pvt = pri->pvts[chanpos];
2916 core_id = ast_cc_get_current_core_id(pvt->owner);
2917 if (core_id == -1) {
2921 cc_params = ast_channel_get_cc_config_params(pvt->owner);
2927 monitor_policy = ast_get_cc_monitor_policy(cc_params);
2928 switch (monitor_policy) {
2929 case AST_CC_MONITOR_NEVER:
2930 /* CCSS is not enabled. */
2932 case AST_CC_MONITOR_NATIVE:
2933 case AST_CC_MONITOR_ALWAYS:
2935 * If it is AST_CC_MONITOR_ALWAYS and native fails we will attempt the fallback
2936 * later in the call to sig_pri_cc_generic_check().
2938 ast_channel_get_device_name(pvt->owner, device_name, sizeof(device_name));
2939 sig_pri_make_cc_dialstring(pvt, dialstring, sizeof(dialstring));
2940 monitor = sig_pri_cc_monitor_instance_init(core_id, pri, cc_id, device_name);
2944 res = ast_queue_cc_frame(pvt->owner, sig_pri_cc_type_name, dialstring, service,
2947 monitor->cc_id = -1;
2948 ao2_unlink(sig_pri_cc_monitors, monitor);
2949 ao2_ref(monitor, -1);
2952 case AST_CC_MONITOR_GENERIC:
2953 ast_queue_cc_frame(pvt->owner, AST_CC_GENERIC_MONITOR_TYPE,
2954 sig_pri_get_orig_dialstring(pvt), service, NULL);
2955 /* Say it failed to force caller to cancel native CC. */
2960 #endif /* defined(HAVE_PRI_CCSS) */
2964 * \brief Check if generic CC monitor is needed and request it.
2967 * \param pri PRI span control structure.
2968 * \param chanpos Channel position in the span.
2969 * \param service CCBS/CCNR indication.
2971 * \note Assumes the pri->lock is already obtained.
2972 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
2976 static void sig_pri_cc_generic_check(struct sig_pri_span *pri, int chanpos, enum ast_cc_service_type service)
2978 struct ast_channel *owner;
2979 struct ast_cc_config_params *cc_params;
2980 #if defined(HAVE_PRI_CCSS)
2981 struct ast_cc_monitor *monitor;
2982 char device_name[AST_CHANNEL_NAME];
2983 #endif /* defined(HAVE_PRI_CCSS) */
2984 enum ast_cc_monitor_policies monitor_policy;
2987 if (!pri->pvts[chanpos]->outgoing) {
2988 /* This is not an outgoing call so it cannot be CC monitor. */
2992 sig_pri_lock_owner(pri, chanpos);
2993 owner = pri->pvts[chanpos]->owner;
2997 core_id = ast_cc_get_current_core_id(owner);
2998 if (core_id == -1) {
2999 /* No CC core setup */
3003 cc_params = ast_channel_get_cc_config_params(owner);
3005 /* Could not get CC config parameters. */
3009 #if defined(HAVE_PRI_CCSS)
3010 ast_channel_get_device_name(owner, device_name, sizeof(device_name));
3011 monitor = ast_cc_get_monitor_by_recall_core_id(core_id, device_name);
3013 /* CC monitor is already present so no need for generic CC. */
3014 ao2_ref(monitor, -1);
3017 #endif /* defined(HAVE_PRI_CCSS) */
3019 monitor_policy = ast_get_cc_monitor_policy(cc_params);
3020 switch (monitor_policy) {
3021 case AST_CC_MONITOR_NEVER:
3022 /* CCSS is not enabled. */
3024 case AST_CC_MONITOR_NATIVE:
3025 if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
3026 /* Request generic CC monitor. */
3027 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
3028 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
3031 case AST_CC_MONITOR_ALWAYS:
3032 if (pri->sig == SIG_BRI_PTMP && pri->nodetype != PRI_NETWORK) {
3034 * Cannot monitor PTMP TE side since this is not defined.
3035 * We are playing the roll of a phone in this case and
3036 * a phone cannot monitor a party over the network without
3042 * We are either falling back or this is a PTMP NT span.
3043 * Request generic CC monitor.
3045 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
3046 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
3048 case AST_CC_MONITOR_GENERIC:
3049 if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
3050 /* Request generic CC monitor. */
3051 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
3052 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
3058 ast_channel_unlock(owner);
3061 #if defined(HAVE_PRI_CCSS)
3064 * \brief The CC link canceled the CC instance.
3067 * \param pri PRI span control structure.
3068 * \param cc_id CC record ID.
3069 * \param is_agent TRUE if the cc_id is for an agent.
3073 static void sig_pri_cc_link_canceled(struct sig_pri_span *pri, long cc_id, int is_agent)
3076 struct ast_cc_agent *agent;
3078 agent = sig_pri_find_cc_agent_by_cc_id(pri, cc_id);
3082 ast_cc_failed(agent->core_id, "%s agent got canceled by link",
3083 sig_pri_cc_type_name);
3086 struct sig_pri_cc_monitor_instance *monitor;
3088 monitor = sig_pri_find_cc_monitor_by_cc_id(pri, cc_id);
3092 monitor->cc_id = -1;
3093 ast_cc_monitor_failed(monitor->core_id, monitor->name,
3094 "%s monitor got canceled by link", sig_pri_cc_type_name);
3095 ao2_ref(monitor, -1);
3098 #endif /* defined(HAVE_PRI_CCSS) */
3100 #if defined(HAVE_PRI_AOC_EVENTS)
3103 * \brief Convert ast_aoc_charged_item to PRI_AOC_CHARGED_ITEM .
3106 * \param value Value to convert to string.
3108 * \return PRI_AOC_CHARGED_ITEM
3110 static enum PRI_AOC_CHARGED_ITEM sig_pri_aoc_charged_item_to_pri(enum PRI_AOC_CHARGED_ITEM value)
3113 case AST_AOC_CHARGED_ITEM_NA:
3114 return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
3115 case AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
3116 return PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
3117 case AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
3118 return PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
3119 case AST_AOC_CHARGED_ITEM_CALL_ATTEMPT:
3120 return PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT;
3121 case AST_AOC_CHARGED_ITEM_CALL_SETUP:
3122 return PRI_AOC_CHARGED_ITEM_CALL_SETUP;
3123 case AST_AOC_CHARGED_ITEM_USER_USER_INFO:
3124 return PRI_AOC_CHARGED_ITEM_USER_USER_INFO;
3125 case AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
3126 return PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
3128 return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
3130 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3132 #if defined(HAVE_PRI_AOC_EVENTS)
3135 * \brief Convert PRI_AOC_CHARGED_ITEM to ast_aoc_charged_item.
3138 * \param value Value to convert to string.
3140 * \return ast_aoc_charged_item
3142 static enum ast_aoc_s_charged_item sig_pri_aoc_charged_item_to_ast(enum PRI_AOC_CHARGED_ITEM value)
3145 case PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE:
3146 return AST_AOC_CHARGED_ITEM_NA;
3147 case PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
3148 return AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
3149 case PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
3150 return AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
3151 case PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT:
3152 return AST_AOC_CHARGED_ITEM_CALL_ATTEMPT;
3153 case PRI_AOC_CHARGED_ITEM_CALL_SETUP:
3154 return AST_AOC_CHARGED_ITEM_CALL_SETUP;
3155 case PRI_AOC_CHARGED_ITEM_USER_USER_INFO:
3156 return AST_AOC_CHARGED_ITEM_USER_USER_INFO;
3157 case PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
3158 return AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
3160 return AST_AOC_CHARGED_ITEM_NA;
3162 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3164 #if defined(HAVE_PRI_AOC_EVENTS)
3167 * \brief Convert AST_AOC_MULTIPLER to PRI_AOC_MULTIPLIER.
3170 * \return pri enum equivalent.
3172 static int sig_pri_aoc_multiplier_from_ast(enum ast_aoc_currency_multiplier mult)
3175 case AST_AOC_MULT_ONETHOUSANDTH:
3176 return PRI_AOC_MULTIPLIER_THOUSANDTH;
3177 case AST_AOC_MULT_ONEHUNDREDTH:
3178 return PRI_AOC_MULTIPLIER_HUNDREDTH;
3179 case AST_AOC_MULT_ONETENTH:
3180 return PRI_AOC_MULTIPLIER_TENTH;
3181 case AST_AOC_MULT_ONE:
3182 return PRI_AOC_MULTIPLIER_ONE;
3183 case AST_AOC_MULT_TEN:
3184 return PRI_AOC_MULTIPLIER_TEN;
3185 case AST_AOC_MULT_HUNDRED:
3186 return PRI_AOC_MULTIPLIER_HUNDRED;
3187 case AST_AOC_MULT_THOUSAND:
3188 return PRI_AOC_MULTIPLIER_THOUSAND;
3190 return PRI_AOC_MULTIPLIER_ONE;
3193 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3195 #if defined(HAVE_PRI_AOC_EVENTS)
3198 * \brief Convert PRI_AOC_MULTIPLIER to AST_AOC_MULTIPLIER
3201 * \return ast enum equivalent.
3203 static int sig_pri_aoc_multiplier_from_pri(const int mult)
3206 case PRI_AOC_MULTIPLIER_THOUSANDTH:
3207 return AST_AOC_MULT_ONETHOUSANDTH;
3208 case PRI_AOC_MULTIPLIER_HUNDREDTH:
3209 return AST_AOC_MULT_ONEHUNDREDTH;
3210 case PRI_AOC_MULTIPLIER_TENTH:
3211 return AST_AOC_MULT_ONETENTH;
3212 case PRI_AOC_MULTIPLIER_ONE:
3213 return AST_AOC_MULT_ONE;
3214 case PRI_AOC_MULTIPLIER_TEN:
3215 return AST_AOC_MULT_TEN;
3216 case PRI_AOC_MULTIPLIER_HUNDRED:
3217 return AST_AOC_MULT_HUNDRED;
3218 case PRI_AOC_MULTIPLIER_THOUSAND:
3219 return AST_AOC_MULT_THOUSAND;
3221 return AST_AOC_MULT_ONE;
3224 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3226 #if defined(HAVE_PRI_AOC_EVENTS)
3229 * \brief Convert ast_aoc_time_scale representation to PRI_AOC_TIME_SCALE
3232 * \param value Value to convert to ast representation
3234 * \return PRI_AOC_TIME_SCALE
3236 static enum PRI_AOC_TIME_SCALE sig_pri_aoc_scale_to_pri(enum ast_aoc_time_scale value)
3240 case AST_AOC_TIME_SCALE_HUNDREDTH_SECOND:
3241 return PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3242 case AST_AOC_TIME_SCALE_TENTH_SECOND:
3243 return PRI_AOC_TIME_SCALE_TENTH_SECOND;
3244 case AST_AOC_TIME_SCALE_SECOND:
3245 return PRI_AOC_TIME_SCALE_SECOND;
3246 case AST_AOC_TIME_SCALE_TEN_SECOND:
3247 return PRI_AOC_TIME_SCALE_TEN_SECOND;
3248 case AST_AOC_TIME_SCALE_MINUTE:
3249 return PRI_AOC_TIME_SCALE_MINUTE;
3250 case AST_AOC_TIME_SCALE_HOUR:
3251 return PRI_AOC_TIME_SCALE_HOUR;
3252 case AST_AOC_TIME_SCALE_DAY:
3253 return PRI_AOC_TIME_SCALE_DAY;
3256 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3258 #if defined(HAVE_PRI_AOC_EVENTS)
3261 * \brief Convert PRI_AOC_TIME_SCALE to ast aoc representation
3264 * \param value Value to convert to ast representation
3266 * \return ast aoc time scale
3268 static enum ast_aoc_time_scale sig_pri_aoc_scale_to_ast(enum PRI_AOC_TIME_SCALE value)
3272 case PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND:
3273 return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3274 case PRI_AOC_TIME_SCALE_TENTH_SECOND:
3275 return AST_AOC_TIME_SCALE_TENTH_SECOND;
3276 case PRI_AOC_TIME_SCALE_SECOND:
3277 return AST_AOC_TIME_SCALE_SECOND;
3278 case PRI_AOC_TIME_SCALE_TEN_SECOND:
3279 return AST_AOC_TIME_SCALE_TEN_SECOND;
3280 case PRI_AOC_TIME_SCALE_MINUTE:
3281 return AST_AOC_TIME_SCALE_MINUTE;
3282 case PRI_AOC_TIME_SCALE_HOUR:
3283 return AST_AOC_TIME_SCALE_HOUR;
3284 case PRI_AOC_TIME_SCALE_DAY:
3285 return AST_AOC_TIME_SCALE_DAY;
3287 return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3289 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3291 #if defined(HAVE_PRI_AOC_EVENTS)
3294 * \brief Handle AOC-S control frame
3297 * \param aoc_s AOC-S event parameters.
3298 * \param owner Asterisk channel associated with the call.
3299 * \param passthrough indicating if this message should be queued on the ast channel
3301 * \note Assumes the pri->lock is already obtained.
3302 * \note Assumes the sig_pri private is locked
3303 * \note Assumes the owner channel lock is already obtained.
3307 static void sig_pri_aoc_s_from_pri(const struct pri_subcmd_aoc_s *aoc_s, struct ast_channel *owner, int passthrough)
3309 struct ast_aoc_decoded *decoded = NULL;
3310 struct ast_aoc_encoded *encoded = NULL;
3311 size_t encoded_size = 0;
3314 if (!owner || !aoc_s) {
3318 if (!(decoded = ast_aoc_create(AST_AOC_S, 0, 0))) {
3322 for (idx = 0; idx < aoc_s->num_items; ++idx) {
3323 enum ast_aoc_s_charged_item charged_item;
3325 charged_item = sig_pri_aoc_charged_item_to_ast(aoc_s->item[idx].chargeable);
3326 if (charged_item == AST_AOC_CHARGED_ITEM_NA) {
3327 /* Delete the unknown charged item from the list. */
3330 switch (aoc_s->item[idx].rate_type) {
3331 case PRI_AOC_RATE_TYPE_DURATION:
3332 ast_aoc_s_add_rate_duration(decoded,
3334 aoc_s->item[idx].rate.duration.amount.cost,
3335 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.duration.amount.multiplier),
3336 aoc_s->item[idx].rate.duration.currency,
3337 aoc_s->item[idx].rate.duration.time.length,
3338 sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.time.scale),
3339 aoc_s->item[idx].rate.duration.granularity.length,
3340 sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.granularity.scale),
3341 aoc_s->item[idx].rate.duration.charging_type);
3343 case PRI_AOC_RATE_TYPE_FLAT:
3344 ast_aoc_s_add_rate_flat(decoded,
3346 aoc_s->item[idx].rate.flat.amount.cost,
3347 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.flat.amount.multiplier),
3348 aoc_s->item[idx].rate.flat.currency);
3350 case PRI_AOC_RATE_TYPE_VOLUME:
3351 ast_aoc_s_add_rate_volume(decoded,
3353 aoc_s->item[idx].rate.volume.unit,
3354 aoc_s->item[idx].rate.volume.amount.cost,
3355 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.volume.amount.multiplier),
3356 aoc_s->item[idx].rate.volume.currency);
3358 case PRI_AOC_RATE_TYPE_SPECIAL_CODE:
3359 ast_aoc_s_add_rate_special_charge_code(decoded,
3361 aoc_s->item[idx].rate.special);
3363 case PRI_AOC_RATE_TYPE_FREE:
3364 ast_aoc_s_add_rate_free(decoded, charged_item, 0);
3366 case PRI_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
3367 ast_aoc_s_add_rate_free(decoded, charged_item, 1);
3370 ast_aoc_s_add_rate_na(decoded, charged_item);
3375 if (passthrough && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
3376 ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
3379 ast_aoc_manager_event(decoded, owner);
3381 ast_aoc_destroy_decoded(decoded);
3382 ast_aoc_destroy_encoded(encoded);
3384 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3386 #if defined(HAVE_PRI_AOC_EVENTS)
3389 * \brief Generate AOC Request Response
3392 * \param aoc_request
3394 * \note Assumes the pri->lock is already obtained.
3395 * \note Assumes the sig_pri private is locked
3396 * \note Assumes the owner channel lock is already obtained.
3400 static void sig_pri_aoc_request_from_pri(const struct pri_subcmd_aoc_request *aoc_request, struct sig_pri_chan *pvt, q931_call *call)
3408 request = aoc_request->charging_request;
3410 if (request & PRI_AOC_REQUEST_S) {
3411 if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S) {
3412 /* An AOC-S response must come from the other side, so save off this invoke_id
3413 * and see if an AOC-S message comes in before the call is answered. */
3414 pvt->aoc_s_request_invoke_id = aoc_request->invoke_id;
3415 pvt->aoc_s_request_invoke_id_valid = 1;
3418 pri_aoc_s_request_response_send(pvt->pri->pri,
3420 aoc_request->invoke_id,
3425 if (request & PRI_AOC_REQUEST_D) {
3426 if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_D) {
3427 pri_aoc_de_request_response_send(pvt->pri->pri,
3429 PRI_AOC_REQ_RSP_CHARGING_INFO_FOLLOWS,
3430 aoc_request->invoke_id);
3432 pri_aoc_de_request_response_send(pvt->pri->pri,
3434 PRI_AOC_REQ_RSP_ERROR_NOT_AVAILABLE,
3435 aoc_request->invoke_id);
3439 if (request & PRI_AOC_REQUEST_E) {
3440 if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_E) {
3441 pri_aoc_de_request_response_send(pvt->pri->pri,
3443 PRI_AOC_REQ_RSP_CHARGING_INFO_FOLLOWS,
3444 aoc_request->invoke_id);
3446 pri_aoc_de_request_response_send(pvt->pri->pri,
3448 PRI_AOC_REQ_RSP_ERROR_NOT_AVAILABLE,
3449 aoc_request->invoke_id);
3453 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3455 #if defined(HAVE_PRI_AOC_EVENTS)
3458 * \brief Generate AOC-D AST_CONTROL_AOC frame
3461 * \param aoc_e AOC-D event parameters.
3462 * \param owner Asterisk channel associated with the call.
3463 * \param passthrough indicating if this message should be queued on the ast channel
3465 * \note Assumes the pri->lock is already obtained.
3466 * \note Assumes the sig_pri private is locked
3467 * \note Assumes the owner channel lock is already obtained.
3471 static void sig_pri_aoc_d_from_pri(const struct pri_subcmd_aoc_d *aoc_d, struct ast_channel *owner, int passthrough)
3473 struct ast_aoc_decoded *decoded = NULL;
3474 struct ast_aoc_encoded *encoded = NULL;
3475 size_t encoded_size = 0;
3476 enum ast_aoc_charge_type type;
3478 if (!owner || !aoc_d) {
3482 switch (aoc_d->charge) {
3483 case PRI_AOC_DE_CHARGE_CURRENCY:
3484 type = AST_AOC_CHARGE_CURRENCY;
3486 case PRI_AOC_DE_CHARGE_UNITS:
3487 type = AST_AOC_CHARGE_UNIT;
3489 case PRI_AOC_DE_CHARGE_FREE:
3490 type = AST_AOC_CHARGE_FREE;
3493 type = AST_AOC_CHARGE_NA;
3497 if (!(decoded = ast_aoc_create(AST_AOC_D, type, 0))) {
3501 switch (aoc_d->billing_accumulation) {
3503 ast_debug(1, "AOC-D billing accumulation has unknown value: %d\n",
3504 aoc_d->billing_accumulation);
3506 case 0:/* subTotal */
3507 ast_aoc_set_total_type(decoded, AST_AOC_SUBTOTAL);
3510 ast_aoc_set_total_type(decoded, AST_AOC_TOTAL);
3514 switch (aoc_d->billing_id) {
3515 case PRI_AOC_D_BILLING_ID_NORMAL:
3516 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL);
3518 case PRI_AOC_D_BILLING_ID_REVERSE:
3519 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_REVERSE_CHARGE);
3521 case PRI_AOC_D_BILLING_ID_CREDIT_CARD:
3522 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
3524 case PRI_AOC_D_BILLING_ID_NOT_AVAILABLE:
3526 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NA);
3530 switch (aoc_d->charge) {
3531 case PRI_AOC_DE_CHARGE_CURRENCY:
3532 ast_aoc_set_currency_info(decoded,
3533 aoc_d->recorded.money.amount.cost,
3534 sig_pri_aoc_multiplier_from_pri(aoc_d->recorded.money.amount.multiplier),
3535 aoc_d->recorded.money.currency);
3537 case PRI_AOC_DE_CHARGE_UNITS:
3540 for (i = 0; i < aoc_d->recorded.unit.num_items; ++i) {
3541 /* if type or number are negative, then they are not present */
3542 ast_aoc_add_unit_entry(decoded,
3543 (aoc_d->recorded.unit.item[i].number >= 0 ? 1 : 0),
3544 aoc_d->recorded.unit.item[i].number,
3545 (aoc_d->recorded.unit.item[i].type >= 0 ? 1 : 0),
3546 aoc_d->recorded.unit.item[i].type);
3552 if (passthrough && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
3553 ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
3556 ast_aoc_manager_event(decoded, owner);
3558 ast_aoc_destroy_decoded(decoded);
3559 ast_aoc_destroy_encoded(encoded);
3561 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3563 #if defined(HAVE_PRI_AOC_EVENTS)
3566 * \brief Generate AOC-E AST_CONTROL_AOC frame
3569 * \param aoc_e AOC-E event parameters.
3570 * \param owner Asterisk channel associated with the call.
3571 * \param passthrough indicating if this message should be queued on the ast channel
3573 * \note Assumes the pri->lock is already obtained.
3574 * \note Assumes the sig_pri private is locked
3575 * \note Assumes the owner channel lock is already obtained.
3576 * \note owner channel may be NULL. In that case, generate event only
3580 static void sig_pri_aoc_e_from_pri(const struct pri_subcmd_aoc_e *aoc_e, struct ast_channel *owner, int passthrough)
3582 struct ast_aoc_decoded *decoded = NULL;
3583 struct ast_aoc_encoded *encoded = NULL;
3584 size_t encoded_size = 0;
3585 enum ast_aoc_charge_type type;
3591 switch (aoc_e->charge) {
3592 case PRI_AOC_DE_CHARGE_CURRENCY:
3593 type = AST_AOC_CHARGE_CURRENCY;
3595 case PRI_AOC_DE_CHARGE_UNITS:
3596 type = AST_AOC_CHARGE_UNIT;
3598 case PRI_AOC_DE_CHARGE_FREE:
3599 type = AST_AOC_CHARGE_FREE;
3602 type = AST_AOC_CHARGE_NA;
3606 if (!(decoded = ast_aoc_create(AST_AOC_E, type, 0))) {
3610 switch (aoc_e->associated.charging_type) {
3611 case PRI_AOC_E_CHARGING_ASSOCIATION_NUMBER:
3612 if (!aoc_e->associated.charge.number.valid) {
3615 ast_aoc_set_association_number(decoded, aoc_e->associated.charge.number.str, aoc_e->associated.charge.number.plan);
3617 case PRI_AOC_E_CHARGING_ASSOCIATION_ID:
3618 ast_aoc_set_association_id(decoded, aoc_e->associated.charge.id);
3624 switch (aoc_e->billing_id) {
3625 case PRI_AOC_E_BILLING_ID_NORMAL:
3626 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL);
3628 case PRI_AOC_E_BILLING_ID_REVERSE:
3629 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_REVERSE_CHARGE);
3631 case PRI_AOC_E_BILLING_ID_CREDIT_CARD:
3632 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
3634 case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_UNCONDITIONAL:
3635 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL);
3637 case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_BUSY:
3638 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_BUSY);
3640 case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_NO_REPLY:
3641 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_NO_REPLY);
3643 case PRI_AOC_E_BILLING_ID_CALL_DEFLECTION:
3644 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_DEFLECTION);
3646 case PRI_AOC_E_BILLING_ID_CALL_TRANSFER:
3647 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_TRANSFER);
3649 case PRI_AOC_E_BILLING_ID_NOT_AVAILABLE:
3651 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NA);
3655 switch (aoc_e->charge) {
3656 case PRI_AOC_DE_CHARGE_CURRENCY:
3657 ast_aoc_set_currency_info(decoded,
3658 aoc_e->recorded.money.amount.cost,
3659 sig_pri_aoc_multiplier_from_pri(aoc_e->recorded.money.amount.multiplier),
3660 aoc_e->recorded.money.currency);
3662 case PRI_AOC_DE_CHARGE_UNITS:
3665 for (i = 0; i < aoc_e->recorded.unit.num_items; ++i) {
3666 /* if type or number are negative, then they are not present */
3667 ast_aoc_add_unit_entry(decoded,
3668 (aoc_e->recorded.unit.item[i].number >= 0 ? 1 : 0),
3669 aoc_e->recorded.unit.item[i].number,
3670 (aoc_e->recorded.unit.item[i].type >= 0 ? 1 : 0),
3671 aoc_e->recorded.unit.item[i].type);
3676 if (passthrough && owner && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
3677 ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
3680 ast_aoc_manager_event(decoded, owner);
3682 ast_aoc_destroy_decoded(decoded);
3683 ast_aoc_destroy_encoded(encoded);
3685 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3687 #if defined(HAVE_PRI_AOC_EVENTS)
3690 * \brief send an AOC-S message on the current call
3692 * \param pvt sig_pri private channel structure.
3693 * \param generic decoded ast AOC message
3697 * \note Assumes that the PRI lock is already obtained.
3699 static void sig_pri_aoc_s_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
3701 struct pri_subcmd_aoc_s aoc_s = { 0, };
3702 const struct ast_aoc_s_entry *entry;
3705 for (idx = 0; idx < ast_aoc_s_get_count(decoded); idx++) {
3706 if (!(entry = ast_aoc_s_get_rate_info(decoded, idx))) {
3710 aoc_s.item[idx].chargeable = sig_pri_aoc_charged_item_to_pri(entry->charged_item);
3712 switch (entry->rate_type) {
3713 case AST_AOC_RATE_TYPE_DURATION:
3714 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_DURATION;
3715 aoc_s.item[idx].rate.duration.amount.cost = entry->rate.duration.amount;
3716 aoc_s.item[idx].rate.duration.amount.multiplier =
3717 sig_pri_aoc_multiplier_from_ast(entry->rate.duration.multiplier);
3718 aoc_s.item[idx].rate.duration.time.length = entry->rate.duration.time;
3719 aoc_s.item[idx].rate.duration.time.scale =
3720 sig_pri_aoc_scale_to_pri(entry->rate.duration.time_scale);
3721 aoc_s.item[idx].rate.duration.granularity.length = entry->rate.duration.granularity_time;
3722 aoc_s.item[idx].rate.duration.granularity.scale =
3723 sig_pri_aoc_scale_to_pri(entry->rate.duration.granularity_time_scale);
3724 aoc_s.item[idx].rate.duration.charging_type = entry->rate.duration.charging_type;
3726 if (!ast_strlen_zero(entry->rate.duration.currency_name)) {
3727 ast_copy_string(aoc_s.item[idx].rate.duration.currency,
3728 entry->rate.duration.currency_name,
3729 sizeof(aoc_s.item[idx].rate.duration.currency));
3732 case AST_AOC_RATE_TYPE_FLAT:
3733 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FLAT;
3734 aoc_s.item[idx].rate.flat.amount.cost = entry->rate.flat.amount;
3735 aoc_s.item[idx].rate.flat.amount.multiplier =
3736 sig_pri_aoc_multiplier_from_ast(entry->rate.flat.multiplier);
3738 if (!ast_strlen_zero(entry->rate.flat.currency_name)) {
3739 ast_copy_string(aoc_s.item[idx].rate.flat.currency,
3740 entry->rate.flat.currency_name,
3741 sizeof(aoc_s.item[idx].rate.flat.currency));
3744 case AST_AOC_RATE_TYPE_VOLUME:
3745 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_VOLUME;
3746 aoc_s.item[idx].rate.volume.unit = entry->rate.volume.volume_unit;
3747 aoc_s.item[idx].rate.volume.amount.cost = entry->rate.volume.amount;
3748 aoc_s.item[idx].rate.volume.amount.multiplier =
3749 sig_pri_aoc_multiplier_from_ast(entry->rate.volume.multiplier);
3751 if (!ast_strlen_zero(entry->rate.volume.currency_name)) {
3752 ast_copy_string(aoc_s.item[idx].rate.volume.currency,
3753 entry->rate.volume.currency_name,
3754 sizeof(aoc_s.item[idx].rate.volume.currency));
3757 case AST_AOC_RATE_TYPE_SPECIAL_CODE:
3758 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_SPECIAL_CODE;
3759 aoc_s.item[idx].rate.special = entry->rate.special_code;
3761 case AST_AOC_RATE_TYPE_FREE:
3762 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FREE;
3764 case AST_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
3765 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FREE_FROM_BEGINNING;
3768 case AST_AOC_RATE_TYPE_NA:
3769 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_NOT_AVAILABLE;
3773 aoc_s.num_items = idx;
3775 /* if this rate should be sent as a response to an AOC-S request we will
3776 * have an aoc_s_request_invoke_id associated with this pvt */
3777 if (pvt->aoc_s_request_invoke_id_valid) {
3778 pri_aoc_s_request_response_send(pvt->pri->pri, pvt->call, pvt->aoc_s_request_invoke_id, &aoc_s);
3779 pvt->aoc_s_request_invoke_id_valid = 0;
3781 pri_aoc_s_send(pvt->pri->pri, pvt->call, &aoc_s);
3784 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3786 #if defined(HAVE_PRI_AOC_EVENTS)
3789 * \brief send an AOC-D message on the current call
3791 * \param pvt sig_pri private channel structure.
3792 * \param generic decoded ast AOC message
3796 * \note Assumes that the PRI lock is already obtained.
3798 static void sig_pri_aoc_d_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
3800 struct pri_subcmd_aoc_d aoc_d = { 0, };
3802 aoc_d.billing_accumulation = (ast_aoc_get_total_type(decoded) == AST_AOC_TOTAL) ? 1 : 0;
3804 switch (ast_aoc_get_billing_id(decoded)) {
3805 case AST_AOC_BILLING_NORMAL:
3806 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_NORMAL;
3808 case AST_AOC_BILLING_REVERSE_CHARGE:
3809 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_REVERSE;
3811 case AST_AOC_BILLING_CREDIT_CARD:
3812 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_CREDIT_CARD;
3814 case AST_AOC_BILLING_NA:
3816 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_NOT_AVAILABLE;
3820 switch (ast_aoc_get_charge_type(decoded)) {
3821 case AST_AOC_CHARGE_FREE:
3822 aoc_d.charge = PRI_AOC_DE_CHARGE_FREE;
3824 case AST_AOC_CHARGE_CURRENCY:
3826 const char *currency_name = ast_aoc_get_currency_name(decoded);
3827 aoc_d.charge = PRI_AOC_DE_CHARGE_CURRENCY;
3828 aoc_d.recorded.money.amount.cost = ast_aoc_get_currency_amount(decoded);
3829 aoc_d.recorded.money.amount.multiplier = sig_pri_aoc_multiplier_from_ast(ast_aoc_get_currency_multiplier(decoded));
3830 if (!ast_strlen_zero(currency_name)) {
3831 ast_copy_string(aoc_d.recorded.money.currency, currency_name, sizeof(aoc_d.recorded.money.currency));
3835 case AST_AOC_CHARGE_UNIT:
3837 const struct ast_aoc_unit_entry *entry;
3839 aoc_d.charge = PRI_AOC_DE_CHARGE_UNITS;
3840 for (i = 0; i < ast_aoc_get_unit_count(decoded); i++) {
3841 if ((entry = ast_aoc_get_unit_info(decoded, i)) && i < ARRAY_LEN(aoc_d.recorded.unit.item)) {
3842 if (entry->valid_amount) {
3843 aoc_d.recorded.unit.item[i].number = entry->amount;
3845 aoc_d.recorded.unit.item[i].number = -1;
3847 if (entry->valid_type) {
3848 aoc_d.recorded.unit.item[i].type = entry->type;
3850 aoc_d.recorded.unit.item[i].type = -1;