2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2009, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief PRI signaling module
23 * \author Matthew Fredrickson <creslin@digium.com>
35 #include "asterisk/utils.h"
36 #include "asterisk/options.h"
37 #include "asterisk/pbx.h"
38 #include "asterisk/app.h"
39 #include "asterisk/file.h"
40 #include "asterisk/callerid.h"
41 #include "asterisk/say.h"
42 #include "asterisk/manager.h"
43 #include "asterisk/astdb.h"
44 #include "asterisk/causes.h"
45 #include "asterisk/musiconhold.h"
46 #include "asterisk/cli.h"
47 #include "asterisk/transcap.h"
48 #include "asterisk/features.h"
49 #include "asterisk/aoc.h"
52 #ifndef PRI_EVENT_FACILITY
53 #error please update libpri
56 /* define this to send PRI user-user information elements */
57 #undef SUPPORT_USERUSER
60 * Define to make always pick a channel if allowed. Useful for
61 * testing channel shifting.
63 //#define ALWAYS_PICK_CHANNEL 1
66 * Define to force a RESTART on a channel that returns a cause
67 * code of PRI_CAUSE_REQUESTED_CHAN_UNAVAIL(44). If the cause
68 * is because of a stuck channel on the peer and the channel is
69 * always the next channel we pick for an outgoing call then
72 #define FORCE_RESTART_UNAVAIL_CHANS 1
74 #if defined(HAVE_PRI_CCSS)
75 struct sig_pri_cc_agent_prv {
76 /*! Asterisk span D channel control structure. */
77 struct sig_pri_span *pri;
78 /*! CC id value to use with libpri. -1 if invalid. */
80 /*! TRUE if CC has been requested and we are waiting for the response. */
81 unsigned char cc_request_response_pending;
84 struct sig_pri_cc_monitor_instance {
85 /*! \brief Asterisk span D channel control structure. */
86 struct sig_pri_span *pri;
87 /*! CC id value to use with libpri. (-1 if already canceled). */
89 /*! CC core id value. */
91 /*! Device name(Channel name less sequence number) */
95 /*! Upper level agent/monitor type name. */
96 static const char *sig_pri_cc_type_name;
97 /*! Container of sig_pri monitor instances. */
98 static struct ao2_container *sig_pri_cc_monitors;
99 #endif /* defined(HAVE_PRI_CCSS) */
101 static int pri_matchdigittimeout = 3000;
103 static int pri_gendigittimeout = 8000;
105 #define DCHAN_NOTINALARM (1 << 0)
106 #define DCHAN_UP (1 << 1)
108 /* Defines to help decode the encoded event channel id. */
109 #define PRI_CHANNEL(p) ((p) & 0xff)
110 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
111 #define PRI_EXPLICIT (1 << 16)
112 #define PRI_CIS_CALL (1 << 17) /* Call is using the D channel only. */
113 #define PRI_HELD_CALL (1 << 18)
116 #define DCHAN_AVAILABLE (DCHAN_NOTINALARM | DCHAN_UP)
118 #define PRI_DEADLOCK_AVOIDANCE(p) \
120 sig_pri_unlock_private(p); \
122 sig_pri_lock_private(p); \
125 static int pri_active_dchan_index(struct sig_pri_span *pri);
127 static const char *sig_pri_call_level2str(enum sig_pri_call_level level)
130 case SIG_PRI_CALL_LEVEL_IDLE:
132 case SIG_PRI_CALL_LEVEL_SETUP:
134 case SIG_PRI_CALL_LEVEL_OVERLAP:
136 case SIG_PRI_CALL_LEVEL_PROCEEDING:
138 case SIG_PRI_CALL_LEVEL_ALERTING:
140 case SIG_PRI_CALL_LEVEL_CONNECT:
146 static inline void pri_rel(struct sig_pri_span *pri)
148 ast_mutex_unlock(&pri->lock);
151 static unsigned int PVT_TO_CHANNEL(struct sig_pri_chan *p)
153 int res = (((p)->prioffset) | ((p)->logicalspan << 8) | (p->mastertrunkgroup ? PRI_EXPLICIT : 0));
154 ast_debug(5, "prioffset: %d mastertrunkgroup: %d logicalspan: %d result: %d\n",
155 p->prioffset, p->mastertrunkgroup, p->logicalspan, res);
160 static void sig_pri_handle_dchan_exception(struct sig_pri_span *pri, int index)
162 if (pri->calls->handle_dchan_exception)
163 pri->calls->handle_dchan_exception(pri, index);
166 static void sig_pri_set_dialing(struct sig_pri_chan *p, int is_dialing)
168 if (p->calls->set_dialing) {
169 p->calls->set_dialing(p->chan_pvt, is_dialing);
173 static void sig_pri_set_digital(struct sig_pri_chan *p, int is_digital)
175 p->digital = is_digital;
176 if (p->calls->set_digital) {
177 p->calls->set_digital(p->chan_pvt, is_digital);
181 void sig_pri_set_alarm(struct sig_pri_chan *p, int in_alarm)
184 * Clear the channel restart flag when the channel alarm changes
185 * to prevent the flag from getting stuck when the link goes
190 p->inalarm = in_alarm;
191 if (p->calls->set_alarm) {
192 p->calls->set_alarm(p->chan_pvt, in_alarm);
196 static const char *sig_pri_get_orig_dialstring(struct sig_pri_chan *p)
198 if (p->calls->get_orig_dialstring) {
199 return p->calls->get_orig_dialstring(p->chan_pvt);
201 ast_log(LOG_ERROR, "get_orig_dialstring callback not defined\n");
205 #if defined(HAVE_PRI_CCSS)
206 static void sig_pri_make_cc_dialstring(struct sig_pri_chan *p, char *buf, size_t buf_size)
208 if (p->calls->make_cc_dialstring) {
209 p->calls->make_cc_dialstring(p->chan_pvt, buf, buf_size);
211 ast_log(LOG_ERROR, "make_cc_dialstring callback not defined\n");
215 #endif /* defined(HAVE_PRI_CCSS) */
219 * \brief Reevaluate the PRI span device state.
222 * \param pri PRI span control structure.
226 * \note Assumes the pri->lock is already obtained.
228 static void sig_pri_span_devstate_changed(struct sig_pri_span *pri)
230 if (pri->calls->update_span_devstate) {
231 pri->calls->update_span_devstate(pri);
237 * \brief Set the caller id information in the parent module.
240 * \param p sig_pri channel structure.
244 static void sig_pri_set_caller_id(struct sig_pri_chan *p)
246 struct ast_party_caller caller;
248 if (p->calls->set_callerid) {
249 ast_party_caller_init(&caller);
251 caller.id.name.str = p->cid_name;
252 caller.id.name.presentation = p->callingpres;
253 caller.id.name.valid = 1;
255 caller.id.number.str = p->cid_num;
256 caller.id.number.plan = p->cid_ton;
257 caller.id.number.presentation = p->callingpres;
258 caller.id.number.valid = 1;
260 if (!ast_strlen_zero(p->cid_subaddr)) {
261 caller.id.subaddress.valid = 1;
262 //caller.id.subaddress.type = 0;/* nsap */
263 //caller.id.subaddress.odd_even_indicator = 0;
264 caller.id.subaddress.str = p->cid_subaddr;
266 caller.id.tag = p->user_tag;
268 caller.ani.number.str = p->cid_ani;
269 //caller.ani.number.plan = p->xxx;
270 //caller.ani.number.presentation = p->xxx;
271 caller.ani.number.valid = 1;
273 caller.ani2 = p->cid_ani2;
274 p->calls->set_callerid(p->chan_pvt, &caller);
280 * \brief Set the Dialed Number Identifier.
283 * \param p sig_pri channel structure.
284 * \param dnid Dialed Number Identifier string.
288 static void sig_pri_set_dnid(struct sig_pri_chan *p, const char *dnid)
290 if (p->calls->set_dnid) {
291 p->calls->set_dnid(p->chan_pvt, dnid);
297 * \brief Set the Redirecting Directory Number Information Service (RDNIS).
300 * \param p sig_pri channel structure.
301 * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
305 static void sig_pri_set_rdnis(struct sig_pri_chan *p, const char *rdnis)
307 if (p->calls->set_rdnis) {
308 p->calls->set_rdnis(p->chan_pvt, rdnis);
312 static void sig_pri_unlock_private(struct sig_pri_chan *p)
314 if (p->calls->unlock_private)
315 p->calls->unlock_private(p->chan_pvt);
318 static void sig_pri_lock_private(struct sig_pri_chan *p)
320 if (p->calls->lock_private)
321 p->calls->lock_private(p->chan_pvt);
324 static void sig_pri_deadlock_avoidance_private(struct sig_pri_chan *p)
326 if (p->calls->deadlock_avoidance_private) {
327 p->calls->deadlock_avoidance_private(p->chan_pvt);
329 /* Fallback to the old way if callback not present. */
330 PRI_DEADLOCK_AVOIDANCE(p);
334 static inline int pri_grab(struct sig_pri_chan *p, struct sig_pri_span *pri)
337 /* Grab the lock first */
339 res = ast_mutex_trylock(&pri->lock);
341 sig_pri_deadlock_avoidance_private(p);
344 /* Then break the poll */
345 pthread_kill(pri->master, SIGURG);
351 * \brief Convert PRI redirecting reason to asterisk version.
354 * \param pri_reason PRI redirecting reason.
356 * \return Equivalent asterisk redirecting reason value.
358 static enum AST_REDIRECTING_REASON pri_to_ast_reason(int pri_reason)
360 enum AST_REDIRECTING_REASON ast_reason;
362 switch (pri_reason) {
363 case PRI_REDIR_FORWARD_ON_BUSY:
364 ast_reason = AST_REDIRECTING_REASON_USER_BUSY;
366 case PRI_REDIR_FORWARD_ON_NO_REPLY:
367 ast_reason = AST_REDIRECTING_REASON_NO_ANSWER;
369 case PRI_REDIR_DEFLECTION:
370 ast_reason = AST_REDIRECTING_REASON_DEFLECTION;
372 case PRI_REDIR_UNCONDITIONAL:
373 ast_reason = AST_REDIRECTING_REASON_UNCONDITIONAL;
375 case PRI_REDIR_UNKNOWN:
377 ast_reason = AST_REDIRECTING_REASON_UNKNOWN;
386 * \brief Convert asterisk redirecting reason to PRI version.
389 * \param ast_reason Asterisk redirecting reason.
391 * \return Equivalent PRI redirecting reason value.
393 static int ast_to_pri_reason(enum AST_REDIRECTING_REASON ast_reason)
397 switch (ast_reason) {
398 case AST_REDIRECTING_REASON_USER_BUSY:
399 pri_reason = PRI_REDIR_FORWARD_ON_BUSY;
401 case AST_REDIRECTING_REASON_NO_ANSWER:
402 pri_reason = PRI_REDIR_FORWARD_ON_NO_REPLY;
404 case AST_REDIRECTING_REASON_UNCONDITIONAL:
405 pri_reason = PRI_REDIR_UNCONDITIONAL;
407 case AST_REDIRECTING_REASON_DEFLECTION:
408 pri_reason = PRI_REDIR_DEFLECTION;
410 case AST_REDIRECTING_REASON_UNKNOWN:
412 pri_reason = PRI_REDIR_UNKNOWN;
421 * \brief Convert PRI number presentation to asterisk version.
424 * \param pri_presentation PRI number presentation.
426 * \return Equivalent asterisk number presentation value.
428 static int pri_to_ast_presentation(int pri_presentation)
430 int ast_presentation;
432 switch (pri_presentation) {
433 case PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
434 ast_presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
436 case PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
437 ast_presentation = AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
439 case PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
440 ast_presentation = AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN;
442 case PRES_ALLOWED_NETWORK_NUMBER:
443 ast_presentation = AST_PRES_ALLOWED_NETWORK_NUMBER;
445 case PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
446 ast_presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
448 case PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
449 ast_presentation = AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
451 case PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
452 ast_presentation = AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN;
454 case PRES_PROHIB_NETWORK_NUMBER:
455 ast_presentation = AST_PRES_PROHIB_NETWORK_NUMBER;
457 case PRES_NUMBER_NOT_AVAILABLE:
458 ast_presentation = AST_PRES_NUMBER_NOT_AVAILABLE;
461 ast_presentation = AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
465 return ast_presentation;
470 * \brief Convert asterisk number presentation to PRI version.
473 * \param ast_presentation Asterisk number presentation.
475 * \return Equivalent PRI number presentation value.
477 static int ast_to_pri_presentation(int ast_presentation)
479 int pri_presentation;
481 switch (ast_presentation) {
482 case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
483 pri_presentation = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
485 case AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN:
486 pri_presentation = PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN;
488 case AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN:
489 pri_presentation = PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN;
491 case AST_PRES_ALLOWED_NETWORK_NUMBER:
492 pri_presentation = PRES_ALLOWED_NETWORK_NUMBER;
494 case AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED:
495 pri_presentation = PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
497 case AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN:
498 pri_presentation = PRES_PROHIB_USER_NUMBER_PASSED_SCREEN;
500 case AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN:
501 pri_presentation = PRES_PROHIB_USER_NUMBER_FAILED_SCREEN;
503 case AST_PRES_PROHIB_NETWORK_NUMBER:
504 pri_presentation = PRES_PROHIB_NETWORK_NUMBER;
506 case AST_PRES_NUMBER_NOT_AVAILABLE:
507 pri_presentation = PRES_NUMBER_NOT_AVAILABLE;
510 pri_presentation = PRES_PROHIB_USER_NUMBER_NOT_SCREENED;
514 return pri_presentation;
519 * \brief Convert PRI name char_set to asterisk version.
522 * \param pri_char_set PRI name char_set.
524 * \return Equivalent asterisk name char_set value.
526 static enum AST_PARTY_CHAR_SET pri_to_ast_char_set(int pri_char_set)
528 enum AST_PARTY_CHAR_SET ast_char_set;
530 switch (pri_char_set) {
532 case PRI_CHAR_SET_UNKNOWN:
533 ast_char_set = AST_PARTY_CHAR_SET_UNKNOWN;
535 case PRI_CHAR_SET_ISO8859_1:
536 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_1;
538 case PRI_CHAR_SET_WITHDRAWN:
539 ast_char_set = AST_PARTY_CHAR_SET_WITHDRAWN;
541 case PRI_CHAR_SET_ISO8859_2:
542 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_2;
544 case PRI_CHAR_SET_ISO8859_3:
545 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_3;
547 case PRI_CHAR_SET_ISO8859_4:
548 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_4;
550 case PRI_CHAR_SET_ISO8859_5:
551 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_5;
553 case PRI_CHAR_SET_ISO8859_7:
554 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_7;
556 case PRI_CHAR_SET_ISO10646_BMPSTRING:
557 ast_char_set = AST_PARTY_CHAR_SET_ISO10646_BMPSTRING;
559 case PRI_CHAR_SET_ISO10646_UTF_8STRING:
560 ast_char_set = AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING;
569 * \brief Convert asterisk name char_set to PRI version.
572 * \param ast_char_set Asterisk name char_set.
574 * \return Equivalent PRI name char_set value.
576 static int ast_to_pri_char_set(enum AST_PARTY_CHAR_SET ast_char_set)
580 switch (ast_char_set) {
582 case AST_PARTY_CHAR_SET_UNKNOWN:
583 pri_char_set = PRI_CHAR_SET_UNKNOWN;
585 case AST_PARTY_CHAR_SET_ISO8859_1:
586 pri_char_set = PRI_CHAR_SET_ISO8859_1;
588 case AST_PARTY_CHAR_SET_WITHDRAWN:
589 pri_char_set = PRI_CHAR_SET_WITHDRAWN;
591 case AST_PARTY_CHAR_SET_ISO8859_2:
592 pri_char_set = PRI_CHAR_SET_ISO8859_2;
594 case AST_PARTY_CHAR_SET_ISO8859_3:
595 pri_char_set = PRI_CHAR_SET_ISO8859_3;
597 case AST_PARTY_CHAR_SET_ISO8859_4:
598 pri_char_set = PRI_CHAR_SET_ISO8859_4;
600 case AST_PARTY_CHAR_SET_ISO8859_5:
601 pri_char_set = PRI_CHAR_SET_ISO8859_5;
603 case AST_PARTY_CHAR_SET_ISO8859_7:
604 pri_char_set = PRI_CHAR_SET_ISO8859_7;
606 case AST_PARTY_CHAR_SET_ISO10646_BMPSTRING:
607 pri_char_set = PRI_CHAR_SET_ISO10646_BMPSTRING;
609 case AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING:
610 pri_char_set = PRI_CHAR_SET_ISO10646_UTF_8STRING;
617 #if defined(HAVE_PRI_SUBADDR)
620 * \brief Fill in the asterisk party subaddress from the given PRI party subaddress.
623 * \param ast_subaddress Asterisk party subaddress structure.
624 * \param pri_subaddress PRI party subaddress structure.
629 static void sig_pri_set_subaddress(struct ast_party_subaddress *ast_subaddress, const struct pri_party_subaddress *pri_subaddress)
634 if (ast_subaddress->str) {
635 ast_free(ast_subaddress->str);
637 if (pri_subaddress->length <= 0) {
638 ast_party_subaddress_init(ast_subaddress);
642 if (!pri_subaddress->type) {
644 ast_subaddress->str = ast_strdup((char *) pri_subaddress->data);
647 if (!(cnum = ast_malloc(2 * pri_subaddress->length + 1))) {
648 ast_party_subaddress_init(ast_subaddress);
653 len = pri_subaddress->length - 1; /* -1 account for zero based indexing */
654 for (x = 0; x < len; ++x) {
655 ptr += sprintf(ptr, "%02x", pri_subaddress->data[x]);
658 if (pri_subaddress->odd_even_indicator) {
660 sprintf(ptr, "%01x", (pri_subaddress->data[len]) >> 4);
663 sprintf(ptr, "%02x", pri_subaddress->data[len]);
665 ast_subaddress->str = cnum;
667 ast_subaddress->type = pri_subaddress->type;
668 ast_subaddress->odd_even_indicator = pri_subaddress->odd_even_indicator;
669 ast_subaddress->valid = 1;
671 #endif /* defined(HAVE_PRI_SUBADDR) */
673 #if defined(HAVE_PRI_SUBADDR)
674 static unsigned char ast_pri_pack_hex_char(char c)
680 } else if (c < ('9' + 1)) {
682 } else if (c < 'A') {
684 } else if (c < ('F' + 1)) {
686 } else if (c < 'a') {
688 } else if (c < ('f' + 1)) {
695 #endif /* defined(HAVE_PRI_SUBADDR) */
697 #if defined(HAVE_PRI_SUBADDR)
700 * \brief Convert a null terminated hexadecimal string to a packed hex byte array.
701 * \details left justified, with 0 padding if odd length.
704 * \param dst pointer to packed byte array.
705 * \param src pointer to null terminated hexadecimal string.
706 * \param maxlen destination array size.
708 * \return Length of byte array
710 * \note The dst is not an ASCIIz string.
711 * \note The src is an ASCIIz hex string.
713 static int ast_pri_pack_hex_string(unsigned char *dst, char *src, int maxlen)
716 int len = strlen(src);
718 if (len > (2 * maxlen)) {
722 res = len / 2 + len % 2;
725 *dst = ast_pri_pack_hex_char(*src) << 4;
727 *dst |= ast_pri_pack_hex_char(*src);
731 if (len) { /* 1 left */
732 *dst = ast_pri_pack_hex_char(*src) << 4;
736 #endif /* defined(HAVE_PRI_SUBADDR) */
738 #if defined(HAVE_PRI_SUBADDR)
741 * \brief Fill in the PRI party subaddress from the given asterisk party subaddress.
744 * \param pri_subaddress PRI party subaddress structure.
745 * \param ast_subaddress Asterisk party subaddress structure.
749 * \note Assumes that pri_subaddress has been previously memset to zero.
751 static void sig_pri_party_subaddress_from_ast(struct pri_party_subaddress *pri_subaddress, const struct ast_party_subaddress *ast_subaddress)
753 if (ast_subaddress->valid && !ast_strlen_zero(ast_subaddress->str)) {
754 pri_subaddress->type = ast_subaddress->type;
755 if (!ast_subaddress->type) {
757 ast_copy_string((char *) pri_subaddress->data, ast_subaddress->str,
758 sizeof(pri_subaddress->data));
759 pri_subaddress->length = strlen((char *) pri_subaddress->data);
760 pri_subaddress->odd_even_indicator = 0;
761 pri_subaddress->valid = 1;
763 /* 2 = User Specified */
765 * Copy HexString to packed HexData,
766 * if odd length then right pad trailing byte with 0
768 int length = ast_pri_pack_hex_string(pri_subaddress->data,
769 ast_subaddress->str, sizeof(pri_subaddress->data));
771 pri_subaddress->length = length; /* packed data length */
773 length = strlen(ast_subaddress->str);
774 if (length > 2 * sizeof(pri_subaddress->data)) {
775 pri_subaddress->odd_even_indicator = 0;
777 pri_subaddress->odd_even_indicator = (length & 1);
779 pri_subaddress->valid = 1;
783 #endif /* defined(HAVE_PRI_SUBADDR) */
787 * \brief Fill in the PRI party name from the given asterisk party name.
790 * \param pri_name PRI party name structure.
791 * \param ast_name Asterisk party name structure.
795 * \note Assumes that pri_name has been previously memset to zero.
797 static void sig_pri_party_name_from_ast(struct pri_party_name *pri_name, const struct ast_party_name *ast_name)
799 if (!ast_name->valid) {
803 pri_name->presentation = ast_to_pri_presentation(ast_name->presentation);
804 pri_name->char_set = ast_to_pri_char_set(ast_name->char_set);
805 if (!ast_strlen_zero(ast_name->str)) {
806 ast_copy_string(pri_name->str, ast_name->str, sizeof(pri_name->str));
812 * \brief Fill in the PRI party number from the given asterisk party number.
815 * \param pri_number PRI party number structure.
816 * \param ast_number Asterisk party number structure.
820 * \note Assumes that pri_number has been previously memset to zero.
822 static void sig_pri_party_number_from_ast(struct pri_party_number *pri_number, const struct ast_party_number *ast_number)
824 if (!ast_number->valid) {
827 pri_number->valid = 1;
828 pri_number->presentation = ast_to_pri_presentation(ast_number->presentation);
829 pri_number->plan = ast_number->plan;
830 if (!ast_strlen_zero(ast_number->str)) {
831 ast_copy_string(pri_number->str, ast_number->str, sizeof(pri_number->str));
837 * \brief Fill in the PRI party id from the given asterisk party id.
840 * \param pri_id PRI party id structure.
841 * \param ast_id Asterisk party id structure.
845 * \note Assumes that pri_id has been previously memset to zero.
847 static void sig_pri_party_id_from_ast(struct pri_party_id *pri_id, const struct ast_party_id *ast_id)
849 sig_pri_party_name_from_ast(&pri_id->name, &ast_id->name);
850 sig_pri_party_number_from_ast(&pri_id->number, &ast_id->number);
851 #if defined(HAVE_PRI_SUBADDR)
852 sig_pri_party_subaddress_from_ast(&pri_id->subaddress, &ast_id->subaddress);
853 #endif /* defined(HAVE_PRI_SUBADDR) */
858 * \brief Update the PRI redirecting information for the current call.
861 * \param pvt sig_pri private channel structure.
862 * \param ast Asterisk channel
866 * \note Assumes that the PRI lock is already obtained.
868 static void sig_pri_redirecting_update(struct sig_pri_chan *pvt, struct ast_channel *ast)
870 struct pri_party_redirecting pri_redirecting;
872 /*! \todo XXX Original called data can be put in a channel data store that is inherited. */
874 memset(&pri_redirecting, 0, sizeof(pri_redirecting));
875 sig_pri_party_id_from_ast(&pri_redirecting.from, &ast->redirecting.from);
876 sig_pri_party_id_from_ast(&pri_redirecting.to, &ast->redirecting.to);
877 pri_redirecting.count = ast->redirecting.count;
878 pri_redirecting.reason = ast_to_pri_reason(ast->redirecting.reason);
880 pri_redirecting_update(pvt->pri->pri, pvt->call, &pri_redirecting);
885 * \brief Reset DTMF detector.
888 * \param p sig_pri channel structure.
892 static void sig_pri_dsp_reset_and_flush_digits(struct sig_pri_chan *p)
894 if (p->calls->dsp_reset_and_flush_digits) {
895 p->calls->dsp_reset_and_flush_digits(p->chan_pvt);
899 static int sig_pri_set_echocanceller(struct sig_pri_chan *p, int enable)
901 if (p->calls->set_echocanceller)
902 return p->calls->set_echocanceller(p->chan_pvt, enable);
907 static void sig_pri_fixup_chans(struct sig_pri_chan *old_chan, struct sig_pri_chan *new_chan)
909 if (old_chan->calls->fixup_chans)
910 old_chan->calls->fixup_chans(old_chan->chan_pvt, new_chan->chan_pvt);
913 static int sig_pri_play_tone(struct sig_pri_chan *p, enum sig_pri_tone tone)
915 if (p->calls->play_tone)
916 return p->calls->play_tone(p->chan_pvt, tone);
921 static struct ast_channel *sig_pri_new_ast_channel(struct sig_pri_chan *p, int state, int ulaw, int transfercapability, char *exten, const struct ast_channel *requestor)
923 struct ast_channel *c;
925 if (p->calls->new_ast_channel) {
926 c = p->calls->new_ast_channel(p->chan_pvt, state, ulaw, exten, requestor);
937 p->alreadyhungup = 0;
938 c->transfercapability = transfercapability;
939 pbx_builtin_setvar_helper(c, "TRANSFERCAPABILITY",
940 ast_transfercapability2str(transfercapability));
941 if (transfercapability & AST_TRANS_CAP_DIGITAL) {
942 sig_pri_set_digital(p, 1);
945 ast_mutex_lock(&p->pri->lock);
946 sig_pri_span_devstate_changed(p->pri);
947 ast_mutex_unlock(&p->pri->lock);
955 * \brief Open the PRI channel media path.
958 * \param p Channel private control structure.
962 static void sig_pri_open_media(struct sig_pri_chan *p)
964 if (p->no_b_channel) {
968 if (p->calls->open_media) {
969 p->calls->open_media(p->chan_pvt);
975 * \brief Post an AMI B channel association event.
978 * \param p Channel private control structure.
980 * \note Assumes the private and owner are locked.
984 static void sig_pri_ami_channel_event(struct sig_pri_chan *p)
986 if (p->calls->ami_channel_event) {
987 p->calls->ami_channel_event(p->chan_pvt, p->owner);
991 struct ast_channel *sig_pri_request(struct sig_pri_chan *p, enum sig_pri_law law, const struct ast_channel *requestor, int transfercapability)
993 struct ast_channel *ast;
995 ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
998 ast = sig_pri_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability, p->exten, requestor);
1005 int pri_is_up(struct sig_pri_span *pri)
1008 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
1009 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
1015 static const char *pri_order(int level)
1025 return "Quaternary";
1031 /* Returns index of the active dchan */
1032 static int pri_active_dchan_index(struct sig_pri_span *pri)
1036 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
1037 if ((pri->dchans[x] == pri->pri))
1041 ast_log(LOG_WARNING, "No active dchan found!\n");
1045 static void pri_find_dchan(struct sig_pri_span *pri)
1053 for (idx = 0; idx < SIG_PRI_NUM_DCHANS; ++idx) {
1054 if (!pri->dchans[idx]) {
1055 /* No more D channels defined on the span. */
1058 if (pri->dchans[idx] == old) {
1061 if (newslot < 0 && pri->dchanavail[idx] == DCHAN_AVAILABLE) {
1065 /* At this point, idx is a count of how many D-channels are defined on the span. */
1068 /* We have several D-channels defined on the span. (NFAS PRI setup) */
1070 /* No D-channels available. Default to the primary D-channel. */
1073 if (!pri->no_d_channels) {
1074 pri->no_d_channels = 1;
1075 if (old && oldslot != newslot) {
1076 ast_log(LOG_WARNING,
1077 "Span %d: No D-channels up! Switching selected D-channel from %s to %s.\n",
1078 pri->span, pri_order(oldslot), pri_order(newslot));
1080 ast_log(LOG_WARNING, "Span %d: No D-channels up!\n", pri->span);
1084 pri->no_d_channels = 0;
1086 if (old && oldslot != newslot) {
1088 "Switching selected D-channel from %s (fd %d) to %s (fd %d)!\n",
1089 pri_order(oldslot), pri->fds[oldslot],
1090 pri_order(newslot), pri->fds[newslot]);
1094 /* The only D-channel is not up. */
1097 if (!pri->no_d_channels) {
1098 pri->no_d_channels = 1;
1101 * This is annoying to see on non-persistent layer 2
1102 * connections. Let's not complain in that case.
1104 if (pri->sig != SIG_BRI_PTMP) {
1105 ast_log(LOG_WARNING, "Span %d: D-channel is down!\n", pri->span);
1109 pri->no_d_channels = 0;
1112 pri->pri = pri->dchans[newslot];
1117 * \brief Determine if a private channel structure is in use.
1120 * \param pvt Channel to determine if in use.
1122 * \return TRUE if the channel is in use.
1124 static int sig_pri_is_chan_in_use(struct sig_pri_chan *pvt)
1126 return pvt->owner || pvt->call || pvt->allocated || pvt->resetting || pvt->inalarm;
1130 * \brief Determine if a private channel structure is available.
1133 * \param pvt Channel to determine if available.
1135 * \return TRUE if the channel is available.
1137 int sig_pri_is_chan_available(struct sig_pri_chan *pvt)
1139 return !sig_pri_is_chan_in_use(pvt)
1140 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1141 /* And not out-of-service */
1142 && !pvt->service_status
1143 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1149 * \brief Obtain the sig_pri owner channel lock if the owner exists.
1152 * \param pri PRI span control structure.
1153 * \param chanpos Channel position in the span.
1155 * \note Assumes the pri->lock is already obtained.
1156 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1160 static void sig_pri_lock_owner(struct sig_pri_span *pri, int chanpos)
1163 if (!pri->pvts[chanpos]->owner) {
1164 /* There is no owner lock to get. */
1167 if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
1168 /* We got the lock */
1171 /* We must unlock the PRI to avoid the possibility of a deadlock */
1172 ast_mutex_unlock(&pri->lock);
1173 sig_pri_deadlock_avoidance_private(pri->pvts[chanpos]);
1174 ast_mutex_lock(&pri->lock);
1180 * \brief Queue the given frame onto the owner channel.
1183 * \param pri PRI span control structure.
1184 * \param chanpos Channel position in the span.
1185 * \param frame Frame to queue onto the owner channel.
1187 * \note Assumes the pri->lock is already obtained.
1188 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1192 static void pri_queue_frame(struct sig_pri_span *pri, int chanpos, struct ast_frame *frame)
1194 sig_pri_lock_owner(pri, chanpos);
1195 if (pri->pvts[chanpos]->owner) {
1196 ast_queue_frame(pri->pvts[chanpos]->owner, frame);
1197 ast_channel_unlock(pri->pvts[chanpos]->owner);
1203 * \brief Queue a control frame of the specified subclass onto the owner channel.
1206 * \param pri PRI span control structure.
1207 * \param chanpos Channel position in the span.
1208 * \param subclass Control frame subclass to queue onto the owner channel.
1210 * \note Assumes the pri->lock is already obtained.
1211 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1215 static void pri_queue_control(struct sig_pri_span *pri, int chanpos, int subclass)
1217 struct ast_frame f = {AST_FRAME_CONTROL, };
1218 struct sig_pri_chan *p = pri->pvts[chanpos];
1220 if (p->calls->queue_control) {
1221 p->calls->queue_control(p->chan_pvt, subclass);
1224 f.subclass.integer = subclass;
1225 pri_queue_frame(pri, chanpos, &f);
1230 * \brief Find the channel associated with the libpri call.
1233 * \param pri PRI span control structure.
1234 * \param call LibPRI opaque call pointer to find.
1236 * \note Assumes the pri->lock is already obtained.
1238 * \retval array-index into private pointer array on success.
1239 * \retval -1 on error.
1241 static int pri_find_principle_by_call(struct sig_pri_span *pri, q931_call *call)
1246 /* Cannot find a call without a call. */
1249 for (idx = 0; idx < pri->numchans; ++idx) {
1250 if (pri->pvts[idx] && pri->pvts[idx]->call == call) {
1251 /* Found the principle */
1260 * \brief Kill the call.
1263 * \param pri PRI span control structure.
1264 * \param call LibPRI opaque call pointer to find.
1265 * \param cause Reason call was killed.
1267 * \note Assumes the pvt->pri->lock is already obtained.
1271 static void sig_pri_kill_call(struct sig_pri_span *pri, q931_call *call, int cause)
1275 chanpos = pri_find_principle_by_call(pri, call);
1277 pri_hangup(pri->pri, call, cause);
1280 sig_pri_lock_private(pri->pvts[chanpos]);
1281 if (!pri->pvts[chanpos]->owner) {
1282 pri_hangup(pri->pri, call, cause);
1283 pri->pvts[chanpos]->call = NULL;
1284 sig_pri_unlock_private(pri->pvts[chanpos]);
1285 sig_pri_span_devstate_changed(pri);
1288 pri->pvts[chanpos]->owner->hangupcause = cause;
1289 pri_queue_control(pri, chanpos, AST_CONTROL_HANGUP);
1290 sig_pri_unlock_private(pri->pvts[chanpos]);
1295 * \brief Find the private structure for the libpri call.
1297 * \param pri PRI span control structure.
1298 * \param channel LibPRI encoded channel ID.
1299 * \param call LibPRI opaque call pointer.
1301 * \note Assumes the pri->lock is already obtained.
1303 * \retval array-index into private pointer array on success.
1304 * \retval -1 on error.
1306 static int pri_find_principle(struct sig_pri_span *pri, int channel, q931_call *call)
1314 /* Channel is not picked yet. */
1318 prioffset = PRI_CHANNEL(channel);
1319 if (!prioffset || (channel & PRI_HELD_CALL)) {
1320 /* Find the call waiting call or held call. */
1321 return pri_find_principle_by_call(pri, call);
1324 span = PRI_SPAN(channel);
1325 if (!(channel & PRI_EXPLICIT)) {
1328 index = pri_active_dchan_index(pri);
1332 span = pri->dchan_logical_span[index];
1336 for (x = 0; x < pri->numchans; x++) {
1338 && pri->pvts[x]->prioffset == prioffset
1339 && pri->pvts[x]->logicalspan == span
1340 && !pri->pvts[x]->no_b_channel) {
1351 * \brief Fixup the private structure associated with the libpri call.
1353 * \param pri PRI span control structure.
1354 * \param principle Array-index into private array to move call to if not already there.
1355 * \param call LibPRI opaque call pointer to find if need to move call.
1357 * \note Assumes the pri->lock is already obtained.
1359 * \retval principle on success.
1360 * \retval -1 on error.
1362 static int pri_fixup_principle(struct sig_pri_span *pri, int principle, q931_call *call)
1366 if (principle < 0 || pri->numchans <= principle) {
1374 if (pri->pvts[principle] && pri->pvts[principle]->call == call) {
1375 /* Call is already on the specified principle. */
1379 /* Find the old principle location. */
1380 for (x = 0; x < pri->numchans; x++) {
1381 struct sig_pri_chan *new_chan;
1382 struct sig_pri_chan *old_chan;
1384 if (!pri->pvts[x] || pri->pvts[x]->call != call) {
1388 /* Found our call */
1389 new_chan = pri->pvts[principle];
1390 old_chan = pri->pvts[x];
1392 /* Get locks to safely move to the new private structure. */
1393 sig_pri_lock_private(old_chan);
1394 sig_pri_lock_owner(pri, x);
1395 sig_pri_lock_private(new_chan);
1397 ast_verb(3, "Moving call (%s) from channel %d to %d.\n",
1398 old_chan->owner ? old_chan->owner->name : "",
1399 old_chan->channel, new_chan->channel);
1400 if (!sig_pri_is_chan_available(new_chan)) {
1401 ast_log(LOG_WARNING,
1402 "Can't move call (%s) from channel %d to %d. It is already in use.\n",
1403 old_chan->owner ? old_chan->owner->name : "",
1404 old_chan->channel, new_chan->channel);
1405 sig_pri_unlock_private(new_chan);
1406 if (old_chan->owner) {
1407 ast_channel_unlock(old_chan->owner);
1409 sig_pri_unlock_private(old_chan);
1413 sig_pri_fixup_chans(old_chan, new_chan);
1415 /* Fix it all up now */
1416 new_chan->owner = old_chan->owner;
1417 old_chan->owner = NULL;
1419 new_chan->call = old_chan->call;
1420 old_chan->call = NULL;
1422 /* Transfer flags from the old channel. */
1423 #if defined(HAVE_PRI_AOC_EVENTS)
1424 new_chan->aoc_s_request_invoke_id_valid = old_chan->aoc_s_request_invoke_id_valid;
1425 new_chan->waiting_for_aoce = old_chan->waiting_for_aoce;
1426 new_chan->holding_aoce = old_chan->holding_aoce;
1427 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1428 new_chan->alreadyhungup = old_chan->alreadyhungup;
1429 new_chan->isidlecall = old_chan->isidlecall;
1430 new_chan->progress = old_chan->progress;
1431 new_chan->allocated = old_chan->allocated;
1432 new_chan->outgoing = old_chan->outgoing;
1433 new_chan->digital = old_chan->digital;
1434 #if defined(HAVE_PRI_CALL_WAITING)
1435 new_chan->is_call_waiting = old_chan->is_call_waiting;
1436 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1438 #if defined(HAVE_PRI_AOC_EVENTS)
1439 old_chan->aoc_s_request_invoke_id_valid = 0;
1440 old_chan->waiting_for_aoce = 0;
1441 old_chan->holding_aoce = 0;
1442 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1443 old_chan->alreadyhungup = 0;
1444 old_chan->isidlecall = 0;
1445 old_chan->progress = 0;
1446 old_chan->allocated = 0;
1447 old_chan->outgoing = 0;
1448 old_chan->digital = 0;
1449 #if defined(HAVE_PRI_CALL_WAITING)
1450 old_chan->is_call_waiting = 0;
1451 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1453 /* More stuff to transfer to the new channel. */
1454 new_chan->call_level = old_chan->call_level;
1455 old_chan->call_level = SIG_PRI_CALL_LEVEL_IDLE;
1456 #if defined(HAVE_PRI_REVERSE_CHARGE)
1457 new_chan->reverse_charging_indication = old_chan->reverse_charging_indication;
1458 #endif /* defined(HAVE_PRI_REVERSE_CHARGE) */
1459 #if defined(HAVE_PRI_SETUP_KEYPAD)
1460 strcpy(new_chan->keypad_digits, old_chan->keypad_digits);
1461 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
1462 strcpy(new_chan->moh_suggested, old_chan->moh_suggested);
1463 new_chan->moh_state = old_chan->moh_state;
1464 old_chan->moh_state = SIG_PRI_MOH_STATE_IDLE;
1466 #if defined(HAVE_PRI_AOC_EVENTS)
1467 new_chan->aoc_s_request_invoke_id = old_chan->aoc_s_request_invoke_id;
1468 new_chan->aoc_e = old_chan->aoc_e;
1469 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1470 strcpy(new_chan->user_tag, old_chan->user_tag);
1472 if (new_chan->no_b_channel) {
1473 /* Copy the real channel configuration to the no B channel interface. */
1474 new_chan->hidecallerid = old_chan->hidecallerid;
1475 new_chan->hidecalleridname = old_chan->hidecalleridname;
1476 new_chan->immediate = old_chan->immediate;
1477 new_chan->priexclusive = old_chan->priexclusive;
1478 new_chan->priindication_oob = old_chan->priindication_oob;
1479 new_chan->use_callerid = old_chan->use_callerid;
1480 new_chan->use_callingpres = old_chan->use_callingpres;
1481 new_chan->stripmsd = old_chan->stripmsd;
1482 strcpy(new_chan->context, old_chan->context);
1483 strcpy(new_chan->mohinterpret, old_chan->mohinterpret);
1485 /* Become a member of the old channel span/trunk-group. */
1486 new_chan->logicalspan = old_chan->logicalspan;
1487 new_chan->mastertrunkgroup = old_chan->mastertrunkgroup;
1488 } else if (old_chan->no_b_channel) {
1490 * We are transitioning from a held/call-waiting channel to a
1491 * real channel so we need to make sure that the media path is
1492 * open. (Needed especially if the channel is natively
1495 sig_pri_open_media(new_chan);
1498 if (new_chan->owner) {
1499 sig_pri_ami_channel_event(new_chan);
1502 sig_pri_unlock_private(old_chan);
1503 if (new_chan->owner) {
1504 ast_channel_unlock(new_chan->owner);
1506 sig_pri_unlock_private(new_chan);
1510 ast_verb(3, "Call specified, but not found.\n");
1516 * \brief Find and fixup the private structure associated with the libpri call.
1518 * \param pri PRI span control structure.
1519 * \param channel LibPRI encoded channel ID.
1520 * \param call LibPRI opaque call pointer.
1523 * This is a combination of pri_find_principle() and pri_fixup_principle()
1524 * to reduce code redundancy and to make handling several PRI_EVENT_xxx's
1525 * consistent for the current architecture.
1527 * \note Assumes the pri->lock is already obtained.
1529 * \retval array-index into private pointer array on success.
1530 * \retval -1 on error.
1532 static int pri_find_fixup_principle(struct sig_pri_span *pri, int channel, q931_call *call)
1536 chanpos = pri_find_principle(pri, channel, call);
1538 ast_log(LOG_WARNING, "Span %d: PRI requested channel %d/%d is unconfigured.\n",
1539 pri->span, PRI_SPAN(channel), PRI_CHANNEL(channel));
1540 sig_pri_kill_call(pri, call, PRI_CAUSE_IDENTIFIED_CHANNEL_NOTEXIST);
1543 chanpos = pri_fixup_principle(pri, chanpos, call);
1545 ast_log(LOG_WARNING, "Span %d: PRI requested channel %d/%d is not available.\n",
1546 pri->span, PRI_SPAN(channel), PRI_CHANNEL(channel));
1548 * Using Q.931 section 5.2.3.1 b) as the reason for picking
1549 * PRI_CAUSE_CHANNEL_UNACCEPTABLE. Receiving a
1550 * PRI_CAUSE_REQUESTED_CHAN_UNAVAIL would cause us to restart
1551 * that channel (which is not specified by Q.931) and kill some
1552 * other call which would be bad.
1554 sig_pri_kill_call(pri, call, PRI_CAUSE_CHANNEL_UNACCEPTABLE);
1560 static char * redirectingreason2str(int redirectingreason)
1562 switch (redirectingreason) {
1570 return "UNCONDITIONAL";
1572 return "NOREDIRECT";
1576 static char *dialplan2str(int dialplan)
1578 if (dialplan == -1) {
1579 return("Dynamically set dialplan in ISDN");
1581 return (pri_plan2str(dialplan));
1586 * \brief Apply numbering plan prefix to the given number.
1588 * \param buf Buffer to put number into.
1589 * \param size Size of given buffer.
1590 * \param pri PRI span control structure.
1591 * \param number Number to apply numbering plan.
1592 * \param plan Numbering plan to apply.
1596 static void apply_plan_to_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, int plan)
1599 case PRI_INTERNATIONAL_ISDN: /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */
1600 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
1602 case PRI_NATIONAL_ISDN: /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */
1603 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
1605 case PRI_LOCAL_ISDN: /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */
1606 snprintf(buf, size, "%s%s", pri->localprefix, number);
1608 case PRI_PRIVATE: /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */
1609 snprintf(buf, size, "%s%s", pri->privateprefix, number);
1611 case PRI_UNKNOWN: /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */
1612 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
1614 default: /* other Q.931 dialplan => don't twiddle with callingnum */
1615 snprintf(buf, size, "%s", number);
1622 * \brief Apply numbering plan prefix to the given number if the number exists.
1624 * \param buf Buffer to put number into.
1625 * \param size Size of given buffer.
1626 * \param pri PRI span control structure.
1627 * \param number Number to apply numbering plan.
1628 * \param plan Numbering plan to apply.
1632 static void apply_plan_to_existing_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, int plan)
1634 /* Make sure a number exists so the prefix isn't placed on an empty string. */
1635 if (ast_strlen_zero(number)) {
1641 apply_plan_to_number(buf, size, pri, number, plan);
1646 * \brief Restart the next channel we think is idle on the span.
1648 * \param pri PRI span control structure.
1650 * \note Assumes the pri->lock is already obtained.
1654 static void pri_check_restart(struct sig_pri_span *pri)
1656 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1658 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1660 for (++pri->resetpos; pri->resetpos < pri->numchans; ++pri->resetpos) {
1661 if (!pri->pvts[pri->resetpos]
1662 || pri->pvts[pri->resetpos]->no_b_channel
1663 || sig_pri_is_chan_in_use(pri->pvts[pri->resetpos])) {
1666 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1667 why = pri->pvts[pri->resetpos]->service_status;
1670 "Span %d: channel %d out-of-service (reason: %s), not sending RESTART\n",
1671 pri->span, pri->pvts[pri->resetpos]->channel,
1672 (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
1675 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1678 if (pri->resetpos < pri->numchans) {
1679 /* Mark the channel as resetting and restart it */
1680 pri->pvts[pri->resetpos]->resetting = 1;
1681 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
1684 time(&pri->lastreset);
1685 sig_pri_span_devstate_changed(pri);
1689 #if defined(HAVE_PRI_CALL_WAITING)
1692 * \brief Init the private channel configuration using the span controller.
1695 * \param pvt Channel to init the configuration.
1696 * \param pri PRI span control structure.
1698 * \note Assumes the pri->lock is already obtained.
1702 static void sig_pri_init_config(struct sig_pri_chan *pvt, struct sig_pri_span *pri)
1704 pvt->stripmsd = pri->ch_cfg.stripmsd;
1705 pvt->hidecallerid = pri->ch_cfg.hidecallerid;
1706 pvt->hidecalleridname = pri->ch_cfg.hidecalleridname;
1707 pvt->immediate = pri->ch_cfg.immediate;
1708 pvt->priexclusive = pri->ch_cfg.priexclusive;
1709 pvt->priindication_oob = pri->ch_cfg.priindication_oob;
1710 pvt->use_callerid = pri->ch_cfg.use_callerid;
1711 pvt->use_callingpres = pri->ch_cfg.use_callingpres;
1712 ast_copy_string(pvt->context, pri->ch_cfg.context, sizeof(pvt->context));
1713 ast_copy_string(pvt->mohinterpret, pri->ch_cfg.mohinterpret, sizeof(pvt->mohinterpret));
1715 if (pri->calls->init_config) {
1716 pri->calls->init_config(pvt->chan_pvt, pri);
1719 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1723 * \brief Find an empty B-channel interface to use.
1725 * \param pri PRI span control structure.
1726 * \param backwards TRUE if the search starts from higher channels.
1728 * \note Assumes the pri->lock is already obtained.
1730 * \retval array-index into private pointer array on success.
1731 * \retval -1 on error.
1733 static int pri_find_empty_chan(struct sig_pri_span *pri, int backwards)
1741 if (backwards && (x < 0))
1743 if (!backwards && (x >= pri->numchans))
1746 && !pri->pvts[x]->no_b_channel
1747 && sig_pri_is_chan_available(pri->pvts[x])) {
1748 ast_debug(1, "Found empty available channel %d/%d\n",
1749 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
1760 #if defined(HAVE_PRI_CALL_HOLD)
1763 * \brief Find or create an empty no-B-channel interface to use.
1766 * \param pri PRI span control structure.
1768 * \note Assumes the pri->lock is already obtained.
1770 * \retval array-index into private pointer array on success.
1771 * \retval -1 on error.
1773 static int pri_find_empty_nobch(struct sig_pri_span *pri)
1777 for (idx = 0; idx < pri->numchans; ++idx) {
1779 && pri->pvts[idx]->no_b_channel
1780 && sig_pri_is_chan_available(pri->pvts[idx])) {
1781 ast_debug(1, "Found empty available no B channel interface\n");
1786 /* Need to create a new interface. */
1787 if (pri->calls->new_nobch_intf) {
1788 idx = pri->calls->new_nobch_intf(pri);
1794 #endif /* defined(HAVE_PRI_CALL_HOLD) */
1796 static void *do_idle_thread(void *v_pvt)
1798 struct sig_pri_chan *pvt = v_pvt;
1799 struct ast_channel *chan = pvt->owner;
1800 struct ast_frame *f;
1802 /* Wait up to 30 seconds for an answer */
1803 int newms, ms = 30000;
1805 ast_verb(3, "Initiating idle call on channel %s\n", chan->name);
1806 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
1807 if (ast_call(chan, ex, 0)) {
1808 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
1812 while ((newms = ast_waitfor(chan, ms)) > 0) {
1818 if (f->frametype == AST_FRAME_CONTROL) {
1819 switch (f->subclass.integer) {
1820 case AST_CONTROL_ANSWER:
1821 /* Launch the PBX */
1822 ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
1823 ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
1825 ast_verb(4, "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
1827 /* It's already hungup, return immediately */
1829 case AST_CONTROL_BUSY:
1830 ast_verb(4, "Idle channel '%s' busy, waiting...\n", chan->name);
1832 case AST_CONTROL_CONGESTION:
1833 ast_verb(4, "Idle channel '%s' congested, waiting...\n", chan->name);
1840 /* Hangup the channel since nothing happend */
1845 static void *pri_ss_thread(void *data)
1847 struct sig_pri_chan *p = data;
1848 struct ast_channel *chan = p->owner;
1849 char exten[AST_MAX_EXTENSION];
1855 /* We lost the owner before we could get started. */
1860 * In the bizarre case where the channel has become a zombie before we
1861 * even get started here, abort safely.
1863 if (!chan->tech_pvt) {
1864 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
1869 ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
1871 sig_pri_dsp_reset_and_flush_digits(p);
1873 /* Now loop looking for an extension */
1874 ast_copy_string(exten, p->exten, sizeof(exten));
1875 len = strlen(exten);
1877 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
1878 if (len && !ast_ignore_pattern(chan->context, exten))
1879 sig_pri_play_tone(p, -1);
1881 sig_pri_play_tone(p, SIG_PRI_TONE_DIALTONE);
1882 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
1883 timeout = pri_matchdigittimeout;
1885 timeout = pri_gendigittimeout;
1886 res = ast_waitfordigit(chan, timeout);
1888 ast_debug(1, "waitfordigit returned < 0...\n");
1897 /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
1898 if (ast_strlen_zero(exten)) {
1899 ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
1903 ast_free(chan->dialed.number.str);
1904 chan->dialed.number.str = ast_strdup(exten);
1906 if (p->pri->append_msn_to_user_tag && p->pri->nodetype != PRI_NETWORK) {
1908 * Update the user tag for party id's from this device for this call
1909 * now that we have a complete MSN from the network.
1911 snprintf(p->user_tag, sizeof(p->user_tag), "%s_%s", p->pri->initial_user_tag,
1913 ast_free(chan->caller.id.tag);
1914 chan->caller.id.tag = ast_strdup(p->user_tag);
1917 sig_pri_play_tone(p, -1);
1918 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
1919 /* Start the real PBX */
1920 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
1921 sig_pri_dsp_reset_and_flush_digits(p);
1922 #if defined(ISSUE_16789)
1924 * Conditionaled out this code to effectively revert the Mantis
1925 * issue 16789 change. It breaks overlap dialing through
1926 * Asterisk. There is not enough information available at this
1927 * point to know if dialing is complete. The
1928 * ast_exists_extension(), ast_matchmore_extension(), and
1929 * ast_canmatch_extension() calls are not adequate to detect a
1930 * dial through extension pattern of "_9!".
1932 * Workaround is to use the dialplan Proceeding() application
1933 * early on non-dial through extensions.
1935 if ((p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
1936 && !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
1937 sig_pri_lock_private(p);
1939 if (!pri_grab(p, p->pri)) {
1940 if (p->call_level < SIG_PRI_CALL_LEVEL_PROCEEDING) {
1941 p->call_level = SIG_PRI_CALL_LEVEL_PROCEEDING;
1943 pri_proceeding(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 0);
1946 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->pri->span);
1949 sig_pri_unlock_private(p);
1951 #endif /* defined(ISSUE_16789) */
1953 sig_pri_set_echocanceller(p, 1);
1954 ast_setstate(chan, AST_STATE_RING);
1955 res = ast_pbx_run(chan);
1957 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
1960 ast_debug(1, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
1961 chan->hangupcause = AST_CAUSE_UNALLOCATED;
1964 /* Since we send release complete here, we won't get one */
1966 ast_mutex_lock(&p->pri->lock);
1967 sig_pri_span_devstate_changed(p->pri);
1968 ast_mutex_unlock(&p->pri->lock);
1973 void pri_event_alarm(struct sig_pri_span *pri, int index, int before_start_pri)
1975 pri->dchanavail[index] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
1976 if (!before_start_pri) {
1977 pri_find_dchan(pri);
1981 void pri_event_noalarm(struct sig_pri_span *pri, int index, int before_start_pri)
1983 pri->dchanavail[index] |= DCHAN_NOTINALARM;
1984 if (!before_start_pri)
1985 pri_restart(pri->dchans[index]);
1990 * \brief Convert libpri party name into asterisk party name.
1993 * \param ast_name Asterisk party name structure to fill. Must already be set initialized.
1994 * \param pri_name libpri party name structure containing source information.
1996 * \note The filled in ast_name structure needs to be destroyed by
1997 * ast_party_name_free() when it is no longer needed.
2001 static void sig_pri_party_name_convert(struct ast_party_name *ast_name, const struct pri_party_name *pri_name)
2003 ast_name->str = ast_strdup(pri_name->str);
2004 ast_name->char_set = pri_to_ast_char_set(pri_name->char_set);
2005 ast_name->presentation = pri_to_ast_presentation(pri_name->presentation);
2006 ast_name->valid = 1;
2011 * \brief Convert libpri party number into asterisk party number.
2014 * \param ast_number Asterisk party number structure to fill. Must already be set initialized.
2015 * \param pri_number libpri party number structure containing source information.
2016 * \param pri PRI span control structure.
2018 * \note The filled in ast_number structure needs to be destroyed by
2019 * ast_party_number_free() when it is no longer needed.
2023 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)
2025 char number[AST_MAX_EXTENSION];
2027 apply_plan_to_existing_number(number, sizeof(number), pri, pri_number->str,
2029 ast_number->str = ast_strdup(number);
2030 ast_number->plan = pri_number->plan;
2031 ast_number->presentation = pri_to_ast_presentation(pri_number->presentation);
2032 ast_number->valid = 1;
2037 * \brief Convert libpri party id into asterisk party id.
2040 * \param ast_id Asterisk party id structure to fill. Must already be set initialized.
2041 * \param pri_id libpri party id structure containing source information.
2042 * \param pri PRI span control structure.
2044 * \note The filled in ast_id structure needs to be destroyed by
2045 * ast_party_id_free() when it is no longer needed.
2049 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)
2051 if (pri_id->name.valid) {
2052 sig_pri_party_name_convert(&ast_id->name, &pri_id->name);
2054 if (pri_id->number.valid) {
2055 sig_pri_party_number_convert(&ast_id->number, &pri_id->number, pri);
2057 #if defined(HAVE_PRI_SUBADDR)
2058 if (pri_id->subaddress.valid) {
2059 sig_pri_set_subaddress(&ast_id->subaddress, &pri_id->subaddress);
2061 #endif /* defined(HAVE_PRI_SUBADDR) */
2066 * \brief Convert libpri redirecting information into asterisk redirecting information.
2069 * \param ast_redirecting Asterisk redirecting structure to fill.
2070 * \param pri_redirecting libpri redirecting structure containing source information.
2071 * \param ast_guide Asterisk redirecting structure to use as an initialization guide.
2072 * \param pri PRI span control structure.
2074 * \note The filled in ast_redirecting structure needs to be destroyed by
2075 * ast_party_redirecting_free() when it is no longer needed.
2079 static void sig_pri_redirecting_convert(struct ast_party_redirecting *ast_redirecting,
2080 const struct pri_party_redirecting *pri_redirecting,
2081 const struct ast_party_redirecting *ast_guide,
2082 struct sig_pri_span *pri)
2084 ast_party_redirecting_set_init(ast_redirecting, ast_guide);
2086 sig_pri_party_id_convert(&ast_redirecting->from, &pri_redirecting->from, pri);
2087 sig_pri_party_id_convert(&ast_redirecting->to, &pri_redirecting->to, pri);
2088 ast_redirecting->count = pri_redirecting->count;
2089 ast_redirecting->reason = pri_to_ast_reason(pri_redirecting->reason);
2094 * \brief Determine if the given extension matches one of the MSNs in the pattern list.
2097 * \param msn_patterns Comma separated list of MSN patterns to match.
2098 * \param exten Extension to match in the MSN list.
2100 * \retval 1 if matches.
2101 * \retval 0 if no match.
2103 static int sig_pri_msn_match(const char *msn_patterns, const char *exten)
2109 msn_list = ast_strdupa(msn_patterns);
2112 pattern = strtok_r(msn_list, ",", &list_tail);
2114 pattern = ast_strip(pattern);
2115 if (!ast_strlen_zero(pattern) && ast_extension_match(pattern, exten)) {
2116 /* Extension matched the pattern. */
2119 pattern = strtok_r(NULL, ",", &list_tail);
2121 /* Did not match any pattern in the list. */
2125 #if defined(HAVE_PRI_MCID)
2128 * \brief Append the given party id to the event string.
2131 * \param msg Event message string being built.
2132 * \param prefix Prefix to add to the party id lines.
2133 * \param party Party information to encode.
2137 static void sig_pri_event_party_id(struct ast_str **msg, const char *prefix, struct ast_party_id *party)
2141 /* Combined party presentation */
2142 pres = ast_party_id_presentation(party);
2143 ast_str_append(msg, 0, "%sPres: %d (%s)\r\n", prefix, pres,
2144 ast_describe_caller_presentation(pres));
2147 ast_str_append(msg, 0, "%sNumValid: %d\r\n", prefix,
2148 (unsigned) party->number.valid);
2149 ast_str_append(msg, 0, "%sNum: %s\r\n", prefix,
2150 S_COR(party->number.valid, party->number.str, ""));
2151 ast_str_append(msg, 0, "%ston: %d\r\n", prefix, party->number.plan);
2152 if (party->number.valid) {
2153 ast_str_append(msg, 0, "%sNumPlan: %d\r\n", prefix, party->number.plan);
2154 ast_str_append(msg, 0, "%sNumPres: %d (%s)\r\n", prefix,
2155 party->number.presentation,
2156 ast_describe_caller_presentation(party->number.presentation));
2160 ast_str_append(msg, 0, "%sNameValid: %d\r\n", prefix,
2161 (unsigned) party->name.valid);
2162 ast_str_append(msg, 0, "%sName: %s\r\n", prefix,
2163 S_COR(party->name.valid, party->name.str, ""));
2164 if (party->name.valid) {
2165 ast_str_append(msg, 0, "%sNameCharSet: %s\r\n", prefix,
2166 ast_party_name_charset_describe(party->name.char_set));
2167 ast_str_append(msg, 0, "%sNamePres: %d (%s)\r\n", prefix,
2168 party->name.presentation,
2169 ast_describe_caller_presentation(party->name.presentation));
2172 #if defined(HAVE_PRI_SUBADDR)
2173 /* Party subaddress */
2174 if (party->subaddress.valid) {
2175 static const char subaddress[] = "Subaddr";
2177 ast_str_append(msg, 0, "%s%s: %s\r\n", prefix, subaddress,
2178 S_OR(party->subaddress.str, ""));
2179 ast_str_append(msg, 0, "%s%sType: %d\r\n", prefix, subaddress,
2180 party->subaddress.type);
2181 ast_str_append(msg, 0, "%s%sOdd: %d\r\n", prefix, subaddress,
2182 party->subaddress.odd_even_indicator);
2184 #endif /* defined(HAVE_PRI_SUBADDR) */
2186 #endif /* defined(HAVE_PRI_MCID) */
2188 #if defined(HAVE_PRI_MCID)
2191 * \brief Handle the MCID event.
2194 * \param pri PRI span control structure.
2195 * \param mcid MCID event parameters.
2196 * \param owner Asterisk channel associated with the call.
2197 * NULL if Asterisk no longer has the ast_channel struct.
2199 * \note Assumes the pri->lock is already obtained.
2200 * \note Assumes the owner channel lock is already obtained if still present.
2204 static void sig_pri_mcid_event(struct sig_pri_span *pri, const struct pri_subcmd_mcid_req *mcid, struct ast_channel *owner)
2206 struct ast_channel *chans[1];
2207 struct ast_str *msg;
2208 struct ast_party_id party;
2210 msg = ast_str_create(4096);
2217 * The owner channel is present.
2218 * Pass the event to the peer as well.
2220 ast_queue_control(owner, AST_CONTROL_MCID);
2222 ast_str_append(&msg, 0, "Channel: %s\r\n", owner->name);
2223 ast_str_append(&msg, 0, "UniqueID: %s\r\n", owner->uniqueid);
2225 sig_pri_event_party_id(&msg, "CallerID", &owner->connected.id);
2228 * Since we no longer have an owner channel,
2229 * we have to use the caller information supplied by libpri.
2231 ast_party_id_init(&party);
2232 sig_pri_party_id_convert(&party, &mcid->originator, pri);
2233 sig_pri_event_party_id(&msg, "CallerID", &party);
2234 ast_party_id_free(&party);
2237 /* Always use libpri's called party information. */
2238 ast_party_id_init(&party);
2239 sig_pri_party_id_convert(&party, &mcid->answerer, pri);
2240 sig_pri_event_party_id(&msg, "ConnectedID", &party);
2241 ast_party_id_free(&party);
2244 ast_manager_event_multichan(EVENT_FLAG_CALL, "MCID", owner ? 1 : 0, chans, "%s",
2245 ast_str_buffer(msg));
2248 #endif /* defined(HAVE_PRI_MCID) */
2250 #if defined(HAVE_PRI_TRANSFER)
2251 struct xfer_rsp_data {
2252 struct sig_pri_span *pri;
2253 /*! Call to send transfer success/fail response over. */
2255 /*! Invocation ID to use when sending a reply to the transfer request. */
2258 #endif /* defined(HAVE_PRI_TRANSFER) */
2260 #if defined(HAVE_PRI_TRANSFER)
2263 * \brief Send the transfer success/fail response message.
2266 * \param data Callback user data pointer
2267 * \param is_successful TRUE if the transfer was successful.
2271 static void sig_pri_transfer_rsp(void *data, int is_successful)
2273 struct xfer_rsp_data *rsp = data;
2275 pri_transfer_rsp(rsp->pri->pri, rsp->call, rsp->invoke_id, is_successful);
2277 #endif /* defined(HAVE_PRI_TRANSFER) */
2279 #if defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER)
2281 * \brief Protocol callback to indicate if transfer will happen.
2284 * \param data Callback user data pointer
2285 * \param is_successful TRUE if the transfer will happen.
2289 typedef void (*xfer_rsp_callback)(void *data, int is_successful);
2290 #endif /* defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER) */
2292 #if defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER)
2295 * \brief Attempt to transfer the two calls to each other.
2298 * \param pri PRI span control structure.
2299 * \param call_1_pri First call involved in the transfer. (transferee; usually on hold)
2300 * \param call_1_held TRUE if call_1_pri is on hold.
2301 * \param call_2_pri Second call involved in the transfer. (target; usually active/ringing)
2302 * \param call_2_held TRUE if call_2_pri is on hold.
2303 * \param rsp_callback Protocol callback to indicate if transfer will happen. NULL if not used.
2304 * \param data Callback user data pointer
2306 * \note Assumes the pri->lock is already obtained.
2308 * \retval 0 on success.
2309 * \retval -1 on error.
2311 static int sig_pri_attempt_transfer(struct sig_pri_span *pri, q931_call *call_1_pri, int call_1_held, q931_call *call_2_pri, int call_2_held, xfer_rsp_callback rsp_callback, void *data)
2313 struct attempt_xfer_call {
2315 struct ast_channel *ast;
2320 struct ast_channel *transferee;
2321 struct attempt_xfer_call *call_1;
2322 struct attempt_xfer_call *call_2;
2323 struct attempt_xfer_call *swap_call;
2324 struct attempt_xfer_call c1;
2325 struct attempt_xfer_call c2;
2327 c1.pri = call_1_pri;
2328 c1.held = call_1_held;
2331 c2.pri = call_2_pri;
2332 c2.held = call_2_held;
2335 call_1->chanpos = pri_find_principle_by_call(pri, call_1->pri);
2336 call_2->chanpos = pri_find_principle_by_call(pri, call_2->pri);
2337 if (call_1->chanpos < 0 || call_2->chanpos < 0) {
2338 /* Calls not found in span control. */
2340 /* Transfer failed. */
2341 rsp_callback(data, 0);
2346 /* Attempt to make transferee and target consistent. */
2347 if (!call_1->held && call_2->held) {
2349 * Swap call_1 and call_2 to make call_1 the transferee(held call)
2350 * and call_2 the target(active call).
2357 /* Deadlock avoidance is attempted. */
2358 sig_pri_lock_private(pri->pvts[call_1->chanpos]);
2359 sig_pri_lock_owner(pri, call_1->chanpos);
2360 sig_pri_lock_private(pri->pvts[call_2->chanpos]);
2361 sig_pri_lock_owner(pri, call_2->chanpos);
2363 call_1->ast = pri->pvts[call_1->chanpos]->owner;
2364 call_2->ast = pri->pvts[call_2->chanpos]->owner;
2365 if (!call_1->ast || !call_2->ast) {
2366 /* At least one owner is not present. */
2368 ast_channel_unlock(call_1->ast);
2371 ast_channel_unlock(call_2->ast);
2373 sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
2374 sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
2376 /* Transfer failed. */
2377 rsp_callback(data, 0);
2383 transferee = ast_bridged_channel(call_1->ast);
2388 /* Try masquerading the other way. */
2393 transferee = ast_bridged_channel(call_1->ast);
2398 /* Could not transfer. Neither call is bridged. */
2399 ast_channel_unlock(call_1->ast);
2400 ast_channel_unlock(call_2->ast);
2401 sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
2402 sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
2405 /* Transfer failed. */
2406 rsp_callback(data, 0);
2411 ast_verb(3, "TRANSFERRING %s to %s\n", call_1->ast->name, call_2->ast->name);
2414 * Setup transfer masquerade.
2416 * Note: There is an extremely nasty deadlock avoidance issue
2417 * with ast_channel_transfer_masquerade(). Deadlock may be possible if
2418 * the channels involved are proxies (chan_agent channels) and
2419 * it is called with locks. Unfortunately, there is no simple
2420 * or even merely difficult way to guarantee deadlock avoidance
2421 * and still be able to send an ECT success response without the
2422 * possibility of the bridged channel hanging up on us.
2424 ast_mutex_unlock(&pri->lock);
2425 retval = ast_channel_transfer_masquerade(
2427 &call_2->ast->connected,
2430 &call_1->ast->connected,
2433 /* Reacquire the pri->lock to hold off completion of the transfer masquerade. */
2434 ast_mutex_lock(&pri->lock);
2436 ast_channel_unlock(call_1->ast);
2437 ast_channel_unlock(call_2->ast);
2438 sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
2439 sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
2443 * Report transfer status.
2445 * Must do the callback before the masquerade completes to ensure
2446 * that the protocol message goes out before the call leg is
2449 rsp_callback(data, retval ? 0 : 1);
2453 #endif /* defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER) */
2455 #if defined(HAVE_PRI_CCSS)
2458 * \brief Compare the CC agent private data by libpri cc_id.
2461 * \param obj pointer to the (user-defined part) of an object.
2462 * \param arg callback argument from ao2_callback()
2463 * \param flags flags from ao2_callback()
2465 * \return values are a combination of enum _cb_results.
2467 static int sig_pri_cc_agent_cmp_cc_id(void *obj, void *arg, int flags)
2469 struct ast_cc_agent *agent_1 = obj;
2470 struct sig_pri_cc_agent_prv *agent_prv_1 = agent_1->private_data;
2471 struct sig_pri_cc_agent_prv *agent_prv_2 = arg;
2473 return (agent_prv_1 && agent_prv_1->pri == agent_prv_2->pri
2474 && agent_prv_1->cc_id == agent_prv_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
2476 #endif /* defined(HAVE_PRI_CCSS) */
2478 #if defined(HAVE_PRI_CCSS)
2481 * \brief Find the CC agent by libpri cc_id.
2484 * \param pri PRI span control structure.
2485 * \param cc_id CC record ID to find.
2488 * Since agents are refcounted, and this function returns
2489 * a reference to the agent, it is imperative that you decrement
2490 * the refcount of the agent once you have finished using it.
2492 * \retval agent on success.
2493 * \retval NULL not found.
2495 static struct ast_cc_agent *sig_pri_find_cc_agent_by_cc_id(struct sig_pri_span *pri, long cc_id)
2497 struct sig_pri_cc_agent_prv finder = {
2502 return ast_cc_agent_callback(0, sig_pri_cc_agent_cmp_cc_id, &finder,
2503 sig_pri_cc_type_name);
2505 #endif /* defined(HAVE_PRI_CCSS) */
2507 #if defined(HAVE_PRI_CCSS)
2510 * \brief Compare the CC monitor instance by libpri cc_id.
2513 * \param obj pointer to the (user-defined part) of an object.
2514 * \param arg callback argument from ao2_callback()
2515 * \param flags flags from ao2_callback()
2517 * \return values are a combination of enum _cb_results.
2519 static int sig_pri_cc_monitor_cmp_cc_id(void *obj, void *arg, int flags)
2521 struct sig_pri_cc_monitor_instance *monitor_1 = obj;
2522 struct sig_pri_cc_monitor_instance *monitor_2 = arg;
2524 return (monitor_1->pri == monitor_2->pri
2525 && monitor_1->cc_id == monitor_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
2527 #endif /* defined(HAVE_PRI_CCSS) */
2529 #if defined(HAVE_PRI_CCSS)
2532 * \brief Find the CC monitor instance by libpri cc_id.
2535 * \param pri PRI span control structure.
2536 * \param cc_id CC record ID to find.
2539 * Since monitor_instances are refcounted, and this function returns
2540 * a reference to the instance, it is imperative that you decrement
2541 * the refcount of the instance once you have finished using it.
2543 * \retval monitor_instance on success.
2544 * \retval NULL not found.
2546 static struct sig_pri_cc_monitor_instance *sig_pri_find_cc_monitor_by_cc_id(struct sig_pri_span *pri, long cc_id)
2548 struct sig_pri_cc_monitor_instance finder = {
2553 return ao2_callback(sig_pri_cc_monitors, 0, sig_pri_cc_monitor_cmp_cc_id, &finder);
2555 #endif /* defined(HAVE_PRI_CCSS) */
2557 #if defined(HAVE_PRI_CCSS)
2560 * \brief Destroy the given monitor instance.
2563 * \param data Monitor instance to destroy.
2567 static void sig_pri_cc_monitor_instance_destroy(void *data)
2569 struct sig_pri_cc_monitor_instance *monitor_instance = data;
2571 if (monitor_instance->cc_id != -1) {
2572 ast_mutex_lock(&monitor_instance->pri->lock);
2573 pri_cc_cancel(monitor_instance->pri->pri, monitor_instance->cc_id);
2574 ast_mutex_unlock(&monitor_instance->pri->lock);
2576 monitor_instance->pri->calls->module_unref();
2578 #endif /* defined(HAVE_PRI_CCSS) */
2580 #if defined(HAVE_PRI_CCSS)
2583 * \brief Construct a new monitor instance.
2586 * \param core_id CC core ID.
2587 * \param pri PRI span control structure.
2588 * \param cc_id CC record ID.
2589 * \param device_name Name of device (Asterisk channel name less sequence number).
2592 * Since monitor_instances are refcounted, and this function returns
2593 * a reference to the instance, it is imperative that you decrement
2594 * the refcount of the instance once you have finished using it.
2596 * \retval monitor_instance on success.
2597 * \retval NULL on error.
2599 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)
2601 struct sig_pri_cc_monitor_instance *monitor_instance;
2603 if (!pri->calls->module_ref || !pri->calls->module_unref) {
2607 monitor_instance = ao2_alloc(sizeof(*monitor_instance) + strlen(device_name),
2608 sig_pri_cc_monitor_instance_destroy);
2609 if (!monitor_instance) {
2613 monitor_instance->cc_id = cc_id;
2614 monitor_instance->pri = pri;
2615 monitor_instance->core_id = core_id;
2616 strcpy(monitor_instance->name, device_name);
2618 pri->calls->module_ref();
2620 ao2_link(sig_pri_cc_monitors, monitor_instance);
2621 return monitor_instance;
2623 #endif /* defined(HAVE_PRI_CCSS) */
2625 #if defined(HAVE_PRI_CCSS)
2628 * \brief Announce to the CC core that protocol CC monitor is available for this call.
2631 * \param pri PRI span control structure.
2632 * \param chanpos Channel position in the span.
2633 * \param cc_id CC record ID.
2634 * \param service CCBS/CCNR indication.
2636 * \note Assumes the pri->lock is already obtained.
2637 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
2638 * \note Assumes the sig_pri_lock_owner(pri, chanpos) is already obtained.
2640 * \retval 0 on success.
2641 * \retval -1 on error.
2643 static int sig_pri_cc_available(struct sig_pri_span *pri, int chanpos, long cc_id, enum ast_cc_service_type service)
2645 struct sig_pri_chan *pvt;
2646 struct ast_cc_config_params *cc_params;
2647 struct sig_pri_cc_monitor_instance *monitor;
2648 enum ast_cc_monitor_policies monitor_policy;
2651 char device_name[AST_CHANNEL_NAME];
2652 char dialstring[AST_CHANNEL_NAME];
2654 pvt = pri->pvts[chanpos];
2656 core_id = ast_cc_get_current_core_id(pvt->owner);
2657 if (core_id == -1) {
2661 cc_params = ast_channel_get_cc_config_params(pvt->owner);
2667 monitor_policy = ast_get_cc_monitor_policy(cc_params);
2668 switch (monitor_policy) {
2669 case AST_CC_MONITOR_NEVER:
2670 /* CCSS is not enabled. */
2672 case AST_CC_MONITOR_NATIVE:
2673 case AST_CC_MONITOR_ALWAYS:
2675 * If it is AST_CC_MONITOR_ALWAYS and native fails we will attempt the fallback
2676 * later in the call to sig_pri_cc_generic_check().
2678 ast_channel_get_device_name(pvt->owner, device_name, sizeof(device_name));
2679 sig_pri_make_cc_dialstring(pvt, dialstring, sizeof(dialstring));
2680 monitor = sig_pri_cc_monitor_instance_init(core_id, pri, cc_id, device_name);
2684 res = ast_queue_cc_frame(pvt->owner, sig_pri_cc_type_name, dialstring, service,
2687 monitor->cc_id = -1;
2688 ao2_unlink(sig_pri_cc_monitors, monitor);
2689 ao2_ref(monitor, -1);
2692 case AST_CC_MONITOR_GENERIC:
2693 ast_queue_cc_frame(pvt->owner, AST_CC_GENERIC_MONITOR_TYPE,
2694 sig_pri_get_orig_dialstring(pvt), service, NULL);
2695 /* Say it failed to force caller to cancel native CC. */
2700 #endif /* defined(HAVE_PRI_CCSS) */
2704 * \brief Check if generic CC monitor is needed and request it.
2707 * \param pri PRI span control structure.
2708 * \param chanpos Channel position in the span.
2709 * \param service CCBS/CCNR indication.
2711 * \note Assumes the pri->lock is already obtained.
2712 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
2716 static void sig_pri_cc_generic_check(struct sig_pri_span *pri, int chanpos, enum ast_cc_service_type service)
2718 struct ast_channel *owner;
2719 struct ast_cc_config_params *cc_params;
2720 #if defined(HAVE_PRI_CCSS)
2721 struct ast_cc_monitor *monitor;
2722 char device_name[AST_CHANNEL_NAME];
2723 #endif /* defined(HAVE_PRI_CCSS) */
2724 enum ast_cc_monitor_policies monitor_policy;
2727 if (!pri->pvts[chanpos]->outgoing) {
2728 /* This is not an outgoing call so it cannot be CC monitor. */
2732 sig_pri_lock_owner(pri, chanpos);
2733 owner = pri->pvts[chanpos]->owner;
2737 core_id = ast_cc_get_current_core_id(owner);
2738 if (core_id == -1) {
2739 /* No CC core setup */
2743 cc_params = ast_channel_get_cc_config_params(owner);
2745 /* Could not get CC config parameters. */
2749 #if defined(HAVE_PRI_CCSS)
2750 ast_channel_get_device_name(owner, device_name, sizeof(device_name));
2751 monitor = ast_cc_get_monitor_by_recall_core_id(core_id, device_name);
2753 /* CC monitor is already present so no need for generic CC. */
2754 ao2_ref(monitor, -1);
2757 #endif /* defined(HAVE_PRI_CCSS) */
2759 monitor_policy = ast_get_cc_monitor_policy(cc_params);
2760 switch (monitor_policy) {
2761 case AST_CC_MONITOR_NEVER:
2762 /* CCSS is not enabled. */
2764 case AST_CC_MONITOR_NATIVE:
2765 if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
2766 /* Request generic CC monitor. */
2767 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
2768 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
2771 case AST_CC_MONITOR_ALWAYS:
2772 if (pri->sig == SIG_BRI_PTMP && pri->nodetype != PRI_NETWORK) {
2774 * Cannot monitor PTMP TE side since this is not defined.
2775 * We are playing the roll of a phone in this case and
2776 * a phone cannot monitor a party over the network without
2782 * We are either falling back or this is a PTMP NT span.
2783 * Request generic CC monitor.
2785 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
2786 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
2788 case AST_CC_MONITOR_GENERIC:
2789 if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
2790 /* Request generic CC monitor. */
2791 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
2792 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
2798 ast_channel_unlock(owner);
2801 #if defined(HAVE_PRI_CCSS)
2804 * \brief The CC link canceled the CC instance.
2807 * \param pri PRI span control structure.
2808 * \param cc_id CC record ID.
2809 * \param is_agent TRUE if the cc_id is for an agent.
2813 static void sig_pri_cc_link_canceled(struct sig_pri_span *pri, long cc_id, int is_agent)
2816 struct ast_cc_agent *agent;
2818 agent = sig_pri_find_cc_agent_by_cc_id(pri, cc_id);
2822 ast_cc_failed(agent->core_id, "%s agent got canceled by link",
2823 sig_pri_cc_type_name);
2826 struct sig_pri_cc_monitor_instance *monitor;
2828 monitor = sig_pri_find_cc_monitor_by_cc_id(pri, cc_id);
2832 monitor->cc_id = -1;
2833 ast_cc_monitor_failed(monitor->core_id, monitor->name,
2834 "%s monitor got canceled by link", sig_pri_cc_type_name);
2835 ao2_ref(monitor, -1);
2838 #endif /* defined(HAVE_PRI_CCSS) */
2840 #if defined(HAVE_PRI_AOC_EVENTS)
2843 * \brief Convert ast_aoc_charged_item to PRI_AOC_CHARGED_ITEM .
2846 * \param value Value to convert to string.
2848 * \return PRI_AOC_CHARGED_ITEM
2850 static enum PRI_AOC_CHARGED_ITEM sig_pri_aoc_charged_item_to_pri(enum PRI_AOC_CHARGED_ITEM value)
2853 case AST_AOC_CHARGED_ITEM_NA:
2854 return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
2855 case AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
2856 return PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
2857 case AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
2858 return PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
2859 case AST_AOC_CHARGED_ITEM_CALL_ATTEMPT:
2860 return PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT;
2861 case AST_AOC_CHARGED_ITEM_CALL_SETUP:
2862 return PRI_AOC_CHARGED_ITEM_CALL_SETUP;
2863 case AST_AOC_CHARGED_ITEM_USER_USER_INFO:
2864 return PRI_AOC_CHARGED_ITEM_USER_USER_INFO;
2865 case AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
2866 return PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
2868 return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
2870 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
2872 #if defined(HAVE_PRI_AOC_EVENTS)
2875 * \brief Convert PRI_AOC_CHARGED_ITEM to ast_aoc_charged_item.
2878 * \param value Value to convert to string.
2880 * \return ast_aoc_charged_item
2882 static enum ast_aoc_s_charged_item sig_pri_aoc_charged_item_to_ast(enum PRI_AOC_CHARGED_ITEM value)
2885 case PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE:
2886 return AST_AOC_CHARGED_ITEM_NA;
2887 case PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
2888 return AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
2889 case PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
2890 return AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
2891 case PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT:
2892 return AST_AOC_CHARGED_ITEM_CALL_ATTEMPT;
2893 case PRI_AOC_CHARGED_ITEM_CALL_SETUP:
2894 return AST_AOC_CHARGED_ITEM_CALL_SETUP;
2895 case PRI_AOC_CHARGED_ITEM_USER_USER_INFO:
2896 return AST_AOC_CHARGED_ITEM_USER_USER_INFO;
2897 case PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
2898 return AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
2900 return AST_AOC_CHARGED_ITEM_NA;
2902 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
2904 #if defined(HAVE_PRI_AOC_EVENTS)
2907 * \brief Convert AST_AOC_MULTIPLER to PRI_AOC_MULTIPLIER.
2910 * \return pri enum equivalent.
2912 static int sig_pri_aoc_multiplier_from_ast(enum ast_aoc_currency_multiplier mult)
2915 case AST_AOC_MULT_ONETHOUSANDTH:
2916 return PRI_AOC_MULTIPLIER_THOUSANDTH;
2917 case AST_AOC_MULT_ONEHUNDREDTH:
2918 return PRI_AOC_MULTIPLIER_HUNDREDTH;
2919 case AST_AOC_MULT_ONETENTH:
2920 return PRI_AOC_MULTIPLIER_TENTH;
2921 case AST_AOC_MULT_ONE:
2922 return PRI_AOC_MULTIPLIER_ONE;
2923 case AST_AOC_MULT_TEN:
2924 return PRI_AOC_MULTIPLIER_TEN;
2925 case AST_AOC_MULT_HUNDRED:
2926 return PRI_AOC_MULTIPLIER_HUNDRED;
2927 case AST_AOC_MULT_THOUSAND:
2928 return PRI_AOC_MULTIPLIER_THOUSAND;
2930 return PRI_AOC_MULTIPLIER_ONE;
2933 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
2935 #if defined(HAVE_PRI_AOC_EVENTS)
2938 * \brief Convert PRI_AOC_MULTIPLIER to AST_AOC_MULTIPLIER
2941 * \return ast enum equivalent.
2943 static int sig_pri_aoc_multiplier_from_pri(const int mult)
2946 case PRI_AOC_MULTIPLIER_THOUSANDTH:
2947 return AST_AOC_MULT_ONETHOUSANDTH;
2948 case PRI_AOC_MULTIPLIER_HUNDREDTH:
2949 return AST_AOC_MULT_ONEHUNDREDTH;
2950 case PRI_AOC_MULTIPLIER_TENTH:
2951 return AST_AOC_MULT_ONETENTH;
2952 case PRI_AOC_MULTIPLIER_ONE:
2953 return AST_AOC_MULT_ONE;
2954 case PRI_AOC_MULTIPLIER_TEN:
2955 return AST_AOC_MULT_TEN;
2956 case PRI_AOC_MULTIPLIER_HUNDRED:
2957 return AST_AOC_MULT_HUNDRED;
2958 case PRI_AOC_MULTIPLIER_THOUSAND:
2959 return AST_AOC_MULT_THOUSAND;
2961 return AST_AOC_MULT_ONE;
2964 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
2966 #if defined(HAVE_PRI_AOC_EVENTS)
2969 * \brief Convert ast_aoc_time_scale representation to PRI_AOC_TIME_SCALE
2972 * \param value Value to convert to ast representation
2974 * \return PRI_AOC_TIME_SCALE
2976 static enum PRI_AOC_TIME_SCALE sig_pri_aoc_scale_to_pri(enum ast_aoc_time_scale value)
2980 case AST_AOC_TIME_SCALE_HUNDREDTH_SECOND:
2981 return PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND;
2982 case AST_AOC_TIME_SCALE_TENTH_SECOND:
2983 return PRI_AOC_TIME_SCALE_TENTH_SECOND;
2984 case AST_AOC_TIME_SCALE_SECOND:
2985 return PRI_AOC_TIME_SCALE_SECOND;
2986 case AST_AOC_TIME_SCALE_TEN_SECOND:
2987 return PRI_AOC_TIME_SCALE_TEN_SECOND;
2988 case AST_AOC_TIME_SCALE_MINUTE:
2989 return PRI_AOC_TIME_SCALE_MINUTE;
2990 case AST_AOC_TIME_SCALE_HOUR:
2991 return PRI_AOC_TIME_SCALE_HOUR;
2992 case AST_AOC_TIME_SCALE_DAY:
2993 return PRI_AOC_TIME_SCALE_DAY;
2996 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
2998 #if defined(HAVE_PRI_AOC_EVENTS)
3001 * \brief Convert PRI_AOC_TIME_SCALE to ast aoc representation
3004 * \param value Value to convert to ast representation
3006 * \return ast aoc time scale
3008 static enum ast_aoc_time_scale sig_pri_aoc_scale_to_ast(enum PRI_AOC_TIME_SCALE value)
3012 case PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND:
3013 return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3014 case PRI_AOC_TIME_SCALE_TENTH_SECOND:
3015 return AST_AOC_TIME_SCALE_TENTH_SECOND;
3016 case PRI_AOC_TIME_SCALE_SECOND:
3017 return AST_AOC_TIME_SCALE_SECOND;
3018 case PRI_AOC_TIME_SCALE_TEN_SECOND:
3019 return AST_AOC_TIME_SCALE_TEN_SECOND;
3020 case PRI_AOC_TIME_SCALE_MINUTE:
3021 return AST_AOC_TIME_SCALE_MINUTE;
3022 case PRI_AOC_TIME_SCALE_HOUR:
3023 return AST_AOC_TIME_SCALE_HOUR;
3024 case PRI_AOC_TIME_SCALE_DAY:
3025 return AST_AOC_TIME_SCALE_DAY;
3027 return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3029 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3031 #if defined(HAVE_PRI_AOC_EVENTS)
3034 * \brief Handle AOC-S control frame
3037 * \param aoc_s AOC-S event parameters.
3038 * \param owner Asterisk channel associated with the call.
3039 * \param passthrough indicating if this message should be queued on the ast channel
3041 * \note Assumes the pri->lock is already obtained.
3042 * \note Assumes the sig_pri private is locked
3043 * \note Assumes the owner channel lock is already obtained.
3047 static void sig_pri_aoc_s_from_pri(const struct pri_subcmd_aoc_s *aoc_s, struct ast_channel *owner, int passthrough)
3049 struct ast_aoc_decoded *decoded = NULL;
3050 struct ast_aoc_encoded *encoded = NULL;
3051 size_t encoded_size = 0;
3054 if (!owner || !aoc_s) {
3058 if (!(decoded = ast_aoc_create(AST_AOC_S, 0, 0))) {
3062 for (idx = 0; idx < aoc_s->num_items; ++idx) {
3063 enum ast_aoc_s_charged_item charged_item;
3065 charged_item = sig_pri_aoc_charged_item_to_ast(aoc_s->item[idx].chargeable);
3066 if (charged_item == AST_AOC_CHARGED_ITEM_NA) {
3067 /* Delete the unknown charged item from the list. */
3070 switch (aoc_s->item[idx].rate_type) {
3071 case PRI_AOC_RATE_TYPE_DURATION:
3072 ast_aoc_s_add_rate_duration(decoded,
3074 aoc_s->item[idx].rate.duration.amount.cost,
3075 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.duration.amount.multiplier),
3076 aoc_s->item[idx].rate.duration.currency,
3077 aoc_s->item[idx].rate.duration.time.length,
3078 sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.time.scale),
3079 aoc_s->item[idx].rate.duration.granularity.length,
3080 sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.granularity.scale),
3081 aoc_s->item[idx].rate.duration.charging_type);
3083 case PRI_AOC_RATE_TYPE_FLAT:
3084 ast_aoc_s_add_rate_flat(decoded,
3086 aoc_s->item[idx].rate.flat.amount.cost,
3087 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.flat.amount.multiplier),
3088 aoc_s->item[idx].rate.flat.currency);
3090 case PRI_AOC_RATE_TYPE_VOLUME:
3091 ast_aoc_s_add_rate_volume(decoded,
3093 aoc_s->item[idx].rate.volume.unit,
3094 aoc_s->item[idx].rate.volume.amount.cost,
3095 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.volume.amount.multiplier),
3096 aoc_s->item[idx].rate.volume.currency);
3098 case PRI_AOC_RATE_TYPE_SPECIAL_CODE:
3099 ast_aoc_s_add_rate_special_charge_code(decoded,
3101 aoc_s->item[idx].rate.special);
3103 case PRI_AOC_RATE_TYPE_FREE:
3104 ast_aoc_s_add_rate_free(decoded, charged_item, 0);
3106 case PRI_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
3107 ast_aoc_s_add_rate_free(decoded, charged_item, 1);
3110 ast_aoc_s_add_rate_na(decoded, charged_item);
3115 if (passthrough && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
3116 ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
3119 ast_aoc_manager_event(decoded, owner);
3121 ast_aoc_destroy_decoded(decoded);
3122 ast_aoc_destroy_encoded(encoded);
3124 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3126 #if defined(HAVE_PRI_AOC_EVENTS)
3129 * \brief Generate AOC Request Response
3132 * \param aoc_request
3134 * \note Assumes the pri->lock is already obtained.
3135 * \note Assumes the sig_pri private is locked
3136 * \note Assumes the owner channel lock is already obtained.
3140 static void sig_pri_aoc_request_from_pri(const struct pri_subcmd_aoc_request *aoc_request, struct sig_pri_chan *pvt, q931_call *call)
3148 request = aoc_request->charging_request;
3150 if (request & PRI_AOC_REQUEST_S) {
3151 if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S) {
3152 /* An AOC-S response must come from the other side, so save off this invoke_id
3153 * and see if an AOC-S message comes in before the call is answered. */
3154 pvt->aoc_s_request_invoke_id = aoc_request->invoke_id;
3155 pvt->aoc_s_request_invoke_id_valid = 1;
3158 pri_aoc_s_request_response_send(pvt->pri->pri,
3160 aoc_request->invoke_id,
3165 if (request & PRI_AOC_REQUEST_D) {
3166 if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_D) {
3167 pri_aoc_de_request_response_send(pvt->pri->pri,
3169 PRI_AOC_REQ_RSP_CHARGING_INFO_FOLLOWS,
3170 aoc_request->invoke_id);
3172 pri_aoc_de_request_response_send(pvt->pri->pri,
3174 PRI_AOC_REQ_RSP_ERROR_NOT_AVAILABLE,
3175 aoc_request->invoke_id);
3179 if (request & PRI_AOC_REQUEST_E) {
3180 if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_E) {
3181 pri_aoc_de_request_response_send(pvt->pri->pri,
3183 PRI_AOC_REQ_RSP_CHARGING_INFO_FOLLOWS,
3184 aoc_request->invoke_id);
3186 pri_aoc_de_request_response_send(pvt->pri->pri,
3188 PRI_AOC_REQ_RSP_ERROR_NOT_AVAILABLE,
3189 aoc_request->invoke_id);
3193 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3195 #if defined(HAVE_PRI_AOC_EVENTS)
3198 * \brief Generate AOC-D AST_CONTROL_AOC frame
3201 * \param aoc_e AOC-D event parameters.
3202 * \param owner Asterisk channel associated with the call.
3203 * \param passthrough indicating if this message should be queued on the ast channel
3205 * \note Assumes the pri->lock is already obtained.
3206 * \note Assumes the sig_pri private is locked
3207 * \note Assumes the owner channel lock is already obtained.
3211 static void sig_pri_aoc_d_from_pri(const struct pri_subcmd_aoc_d *aoc_d, struct ast_channel *owner, int passthrough)
3213 struct ast_aoc_decoded *decoded = NULL;
3214 struct ast_aoc_encoded *encoded = NULL;
3215 size_t encoded_size = 0;
3216 enum ast_aoc_charge_type type;
3218 if (!owner || !aoc_d) {
3222 switch (aoc_d->charge) {
3223 case PRI_AOC_DE_CHARGE_CURRENCY:
3224 type = AST_AOC_CHARGE_CURRENCY;
3226 case PRI_AOC_DE_CHARGE_UNITS:
3227 type = AST_AOC_CHARGE_UNIT;
3229 case PRI_AOC_DE_CHARGE_FREE:
3230 type = AST_AOC_CHARGE_FREE;
3233 type = AST_AOC_CHARGE_NA;
3237 if (!(decoded = ast_aoc_create(AST_AOC_D, type, 0))) {
3241 switch (aoc_d->billing_accumulation) {
3243 ast_debug(1, "AOC-D billing accumulation has unknown value: %d\n",
3244 aoc_d->billing_accumulation);
3246 case 0:/* subTotal */
3247 ast_aoc_set_total_type(decoded, AST_AOC_SUBTOTAL);
3250 ast_aoc_set_total_type(decoded, AST_AOC_TOTAL);
3254 switch (aoc_d->billing_id) {
3255 case PRI_AOC_D_BILLING_ID_NORMAL:
3256 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL);
3258 case PRI_AOC_D_BILLING_ID_REVERSE:
3259 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_REVERSE_CHARGE);
3261 case PRI_AOC_D_BILLING_ID_CREDIT_CARD:
3262 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
3264 case PRI_AOC_D_BILLING_ID_NOT_AVAILABLE:
3266 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NA);
3270 switch (aoc_d->charge) {
3271 case PRI_AOC_DE_CHARGE_CURRENCY:
3272 ast_aoc_set_currency_info(decoded,
3273 aoc_d->recorded.money.amount.cost,
3274 sig_pri_aoc_multiplier_from_pri(aoc_d->recorded.money.amount.multiplier),
3275 aoc_d->recorded.money.currency);
3277 case PRI_AOC_DE_CHARGE_UNITS:
3280 for (i = 0; i < aoc_d->recorded.unit.num_items; ++i) {
3281 /* if type or number are negative, then they are not present */
3282 ast_aoc_add_unit_entry(decoded,
3283 (aoc_d->recorded.unit.item[i].number >= 0 ? 1 : 0),
3284 aoc_d->recorded.unit.item[i].number,
3285 (aoc_d->recorded.unit.item[i].type >= 0 ? 1 : 0),
3286 aoc_d->recorded.unit.item[i].type);
3292 if (passthrough && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
3293 ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
3296 ast_aoc_manager_event(decoded, owner);
3298 ast_aoc_destroy_decoded(decoded);
3299 ast_aoc_destroy_encoded(encoded);
3301 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3303 #if defined(HAVE_PRI_AOC_EVENTS)
3306 * \brief Generate AOC-E AST_CONTROL_AOC frame
3309 * \param aoc_e AOC-E event parameters.
3310 * \param owner Asterisk channel associated with the call.
3311 * \param passthrough indicating if this message should be queued on the ast channel
3313 * \note Assumes the pri->lock is already obtained.
3314 * \note Assumes the sig_pri private is locked
3315 * \note Assumes the owner channel lock is already obtained.
3316 * \note owner channel may be NULL. In that case, generate event only
3320 static void sig_pri_aoc_e_from_pri(const struct pri_subcmd_aoc_e *aoc_e, struct ast_channel *owner, int passthrough)
3322 struct ast_aoc_decoded *decoded = NULL;
3323 struct ast_aoc_encoded *encoded = NULL;
3324 size_t encoded_size = 0;
3325 enum ast_aoc_charge_type type;
3331 switch (aoc_e->charge) {
3332 case PRI_AOC_DE_CHARGE_CURRENCY:
3333 type = AST_AOC_CHARGE_CURRENCY;
3335 case PRI_AOC_DE_CHARGE_UNITS:
3336 type = AST_AOC_CHARGE_UNIT;
3338 case PRI_AOC_DE_CHARGE_FREE:
3339 type = AST_AOC_CHARGE_FREE;
3342 type = AST_AOC_CHARGE_NA;
3346 if (!(decoded = ast_aoc_create(AST_AOC_E, type, 0))) {
3350 switch (aoc_e->associated.charging_type) {
3351 case PRI_AOC_E_CHARGING_ASSOCIATION_NUMBER:
3352 if (!aoc_e->associated.charge.number.valid) {
3355 ast_aoc_set_association_number(decoded, aoc_e->associated.charge.number.str, aoc_e->associated.charge.number.plan);
3357 case PRI_AOC_E_CHARGING_ASSOCIATION_ID:
3358 ast_aoc_set_association_id(decoded, aoc_e->associated.charge.id);
3364 switch (aoc_e->billing_id) {
3365 case PRI_AOC_E_BILLING_ID_NORMAL:
3366 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL);
3368 case PRI_AOC_E_BILLING_ID_REVERSE:
3369 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_REVERSE_CHARGE);
3371 case PRI_AOC_E_BILLING_ID_CREDIT_CARD:
3372 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
3374 case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_UNCONDITIONAL:
3375 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL);
3377 case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_BUSY:
3378 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_BUSY);
3380 case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_NO_REPLY:
3381 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_NO_REPLY);
3383 case PRI_AOC_E_BILLING_ID_CALL_DEFLECTION:
3384 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_DEFLECTION);
3386 case PRI_AOC_E_BILLING_ID_CALL_TRANSFER:
3387 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_TRANSFER);
3389 case PRI_AOC_E_BILLING_ID_NOT_AVAILABLE:
3391 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NA);
3395 switch (aoc_e->charge) {
3396 case PRI_AOC_DE_CHARGE_CURRENCY:
3397 ast_aoc_set_currency_info(decoded,
3398 aoc_e->recorded.money.amount.cost,
3399 sig_pri_aoc_multiplier_from_pri(aoc_e->recorded.money.amount.multiplier),
3400 aoc_e->recorded.money.currency);
3402 case PRI_AOC_DE_CHARGE_UNITS:
3405 for (i = 0; i < aoc_e->recorded.unit.num_items; ++i) {
3406 /* if type or number are negative, then they are not present */
3407 ast_aoc_add_unit_entry(decoded,
3408 (aoc_e->recorded.unit.item[i].number >= 0 ? 1 : 0),
3409 aoc_e->recorded.unit.item[i].number,
3410 (aoc_e->recorded.unit.item[i].type >= 0 ? 1 : 0),
3411 aoc_e->recorded.unit.item[i].type);
3416 if (passthrough && owner && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
3417 ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
3420 ast_aoc_manager_event(decoded, owner);
3422 ast_aoc_destroy_decoded(decoded);
3423 ast_aoc_destroy_encoded(encoded);
3425 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3427 #if defined(HAVE_PRI_AOC_EVENTS)
3430 * \brief send an AOC-S message on the current call
3432 * \param pvt sig_pri private channel structure.
3433 * \param generic decoded ast AOC message
3437 * \note Assumes that the PRI lock is already obtained.
3439 static void sig_pri_aoc_s_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
3441 struct pri_subcmd_aoc_s aoc_s = { 0, };
3442 const struct ast_aoc_s_entry *entry;
3445 for (idx = 0; idx < ast_aoc_s_get_count(decoded); idx++) {
3446 if (!(entry = ast_aoc_s_get_rate_info(decoded, idx))) {
3450 aoc_s.item[idx].chargeable = sig_pri_aoc_charged_item_to_pri(entry->charged_item);
3452 switch (entry->rate_type) {
3453 case AST_AOC_RATE_TYPE_DURATION:
3454 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_DURATION;
3455 aoc_s.item[idx].rate.duration.amount.cost = entry->rate.duration.amount;
3456 aoc_s.item[idx].rate.duration.amount.multiplier =
3457 sig_pri_aoc_multiplier_from_ast(entry->rate.duration.multiplier);
3458 aoc_s.item[idx].rate.duration.time.length = entry->rate.duration.time;
3459 aoc_s.item[idx].rate.duration.time.scale =
3460 sig_pri_aoc_scale_to_pri(entry->rate.duration.time_scale);
3461 aoc_s.item[idx].rate.duration.granularity.length = entry->rate.duration.granularity_time;
3462 aoc_s.item[idx].rate.duration.granularity.scale =
3463 sig_pri_aoc_scale_to_pri(entry->rate.duration.granularity_time_scale);
3464 aoc_s.item[idx].rate.duration.charging_type = entry->rate.duration.charging_type;
3466 if (!ast_strlen_zero(entry->rate.duration.currency_name)) {
3467 ast_copy_string(aoc_s.item[idx].rate.duration.currency,
3468 entry->rate.duration.currency_name,
3469 sizeof(aoc_s.item[idx].rate.duration.currency));
3472 case AST_AOC_RATE_TYPE_FLAT:
3473 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FLAT;
3474 aoc_s.item[idx].rate.flat.amount.cost = entry->rate.flat.amount;
3475 aoc_s.item[idx].rate.flat.amount.multiplier =
3476 sig_pri_aoc_multiplier_from_ast(entry->rate.flat.multiplier);
3478 if (!ast_strlen_zero(entry->rate.flat.currency_name)) {
3479 ast_copy_string(aoc_s.item[idx].rate.flat.currency,
3480 entry->rate.flat.currency_name,
3481 sizeof(aoc_s.item[idx].rate.flat.currency));
3484 case AST_AOC_RATE_TYPE_VOLUME:
3485 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_VOLUME;
3486 aoc_s.item[idx].rate.volume.unit = entry->rate.volume.volume_unit;
3487 aoc_s.item[idx].rate.volume.amount.cost = entry->rate.volume.amount;
3488 aoc_s.item[idx].rate.volume.amount.multiplier =
3489 sig_pri_aoc_multiplier_from_ast(entry->rate.volume.multiplier);
3491 if (!ast_strlen_zero(entry->rate.volume.currency_name)) {
3492 ast_copy_string(aoc_s.item[idx].rate.volume.currency,
3493 entry->rate.volume.currency_name,
3494 sizeof(aoc_s.item[idx].rate.volume.currency));
3497 case AST_AOC_RATE_TYPE_SPECIAL_CODE:
3498 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_SPECIAL_CODE;
3499 aoc_s.item[idx].rate.special = entry->rate.special_code;
3501 case AST_AOC_RATE_TYPE_FREE:
3502 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FREE;
3504 case AST_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
3505 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FREE_FROM_BEGINNING;
3508 case AST_AOC_RATE_TYPE_NA:
3509 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_NOT_AVAILABLE;
3513 aoc_s.num_items = idx;
3515 /* if this rate should be sent as a response to an AOC-S request we will
3516 * have an aoc_s_request_invoke_id associated with this pvt */
3517 if (pvt->aoc_s_request_invoke_id_valid) {
3518 pri_aoc_s_request_response_send(pvt->pri->pri, pvt->call, pvt->aoc_s_request_invoke_id, &aoc_s);
3519 pvt->aoc_s_request_invoke_id_valid = 0;
3521 pri_aoc_s_send(pvt->pri->pri, pvt->call, &aoc_s);
3524 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3526 #if defined(HAVE_PRI_AOC_EVENTS)
3529 * \brief send an AOC-D message on the current call
3531 * \param pvt sig_pri private channel structure.
3532 * \param generic decoded ast AOC message
3536 * \note Assumes that the PRI lock is already obtained.
3538 static void sig_pri_aoc_d_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
3540 struct pri_subcmd_aoc_d aoc_d = { 0, };
3542 aoc_d.billing_accumulation = (ast_aoc_get_total_type(decoded) == AST_AOC_TOTAL) ? 1 : 0;
3544 switch (ast_aoc_get_billing_id(decoded)) {
3545 case AST_AOC_BILLING_NORMAL:
3546 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_NORMAL;
3548 case AST_AOC_BILLING_REVERSE_CHARGE:
3549 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_REVERSE;
3551 case AST_AOC_BILLING_CREDIT_CARD:
3552 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_CREDIT_CARD;
3554 case AST_AOC_BILLING_NA:
3556 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_NOT_AVAILABLE;
3560 switch (ast_aoc_get_charge_type(decoded)) {
3561 case AST_AOC_CHARGE_FREE:
3562 aoc_d.charge = PRI_AOC_DE_CHARGE_FREE;
3564 case AST_AOC_CHARGE_CURRENCY:
3566 const char *currency_name = ast_aoc_get_currency_name(decoded);
3567 aoc_d.charge = PRI_AOC_DE_CHARGE_CURRENCY;
3568 aoc_d.recorded.money.amount.cost = ast_aoc_get_currency_amount(decoded);
3569 aoc_d.recorded.money.amount.multiplier = sig_pri_aoc_multiplier_from_ast(ast_aoc_get_currency_multiplier(decoded));
3570 if (!ast_strlen_zero(currency_name)) {
3571 ast_copy_string(aoc_d.recorded.money.currency, currency_name, sizeof(aoc_d.recorded.money.currency));
3575 case AST_AOC_CHARGE_UNIT:
3577 const struct ast_aoc_unit_entry *entry;
3579 aoc_d.charge = PRI_AOC_DE_CHARGE_UNITS;
3580 for (i = 0; i < ast_aoc_get_unit_count(decoded); i++) {
3581 if ((entry = ast_aoc_get_unit_info(decoded, i)) && i < ARRAY_LEN(aoc_d.recorded.unit.item)) {
3582 if (entry->valid_amount) {
3583 aoc_d.recorded.unit.item[i].number = entry->amount;
3585 aoc_d.recorded.unit.item[i].number = -1;
3587 if (entry->valid_type) {
3588 aoc_d.recorded.unit.item[i].type = entry->type;
3590 aoc_d.recorded.unit.item[i].type = -1;
3592 aoc_d.recorded.unit.num_items++;
3599 case AST_AOC_CHARGE_NA:
3601 aoc_d.charge = PRI_AOC_DE_CHARGE_NOT_AVAILABLE;
3605 pri_aoc_d_send(pvt->pri->pri, pvt->call, &aoc_d);
3607 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3609 #if defined(HAVE_PRI_AOC_EVENTS)
3612 * \brief send an AOC-E message on the current call
3614 * \param pvt sig_pri private channel structure.
3615 * \param generic decoded ast AOC message
3619 * \note Assumes that the PRI lock is already obtained.
3621 static void sig_pri_aoc_e_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
3623 struct pri_subcmd_aoc_e *aoc_e = &pvt->aoc_e;
3624 const struct ast_aoc_charging_association *ca = ast_aoc_get_association_info(decoded);
3626 memset(aoc_e, 0, sizeof(*aoc_e));
3627 pvt->holding_aoce = 1;
3629 switch (ca->charging_type) {
3630 case AST_AOC_CHARGING_ASSOCIATION_NUMBER:
3631 aoc_e->associated.charge.number.valid = 1;
3632 ast_copy_string(aoc_e->associated.charge.number.str,
3633 ca->charge.number.number,
3634 sizeof(aoc_e->associated.charge.number.str));
3635 aoc_e->associated.charge.number.plan = ca->charge.number.plan;
3636 aoc_e->associated.charging_type = PRI_AOC_E_CHARGING_ASSOCIATION_NUMBER;
3638 case AST_AOC_CHARGING_ASSOCIATION_ID:
3639 aoc_e->associated.charge.id = ca->charge.id;
3640 aoc_e->associated.charging_type = PRI_AOC_E_CHARGING_ASSOCIATION_ID;
3642 case AST_AOC_CHARGING_ASSOCIATION_NA:
3647 switch (ast_aoc_get_billing_id(decoded)) {
3648 case AST_AOC_BILLING_NORMAL:
3649 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_NORMAL;
3651 case AST_AOC_BILLING_REVERSE_CHARGE:
3652 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_REVERSE;
3654 case AST_AOC_BILLING_CREDIT_CARD:
3655 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CREDIT_CARD;
3657 case AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL:
3658 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_UNCONDITIONAL;
3660 case AST_AOC_BILLING_CALL_FWD_BUSY:
3661 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_BUSY;
3663 case AST_AOC_BILLING_CALL_FWD_NO_REPLY:
3664 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_NO_REPLY;
3666 case AST_AOC_BILLING_CALL_DEFLECTION:
3667 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_DEFLECTION;
3669 case AST_AOC_BILLING_CALL_TRANSFER:
3670 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_TRANSFER;
3672 case AST_AOC_BILLING_NA:
3674 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_NOT_AVAILABLE;
3678 switch (ast_aoc_get_charge_type(decoded)) {
3679 case AST_AOC_CHARGE_FREE:
3680 aoc_e->charge = PRI_AOC_DE_CHARGE_FREE;
3682 case AST_AOC_CHARGE_CURRENCY:
3684 const char *currency_name = ast_aoc_get_currency_name(decoded);
3685 aoc_e->charge = PRI_AOC_DE_CHARGE_CURRENCY;
3686 aoc_e->recorded.money.amount.cost = ast_aoc_get_currency_amount(decoded);
3687 aoc_e->recorded.money.amount.multiplier = sig_pri_aoc_multiplier_from_ast(ast_aoc_get_currency_multiplier(decoded));
3688 if (!ast_strlen_zero(currency_name)) {
3689 ast_copy_string(aoc_e->recorded.money.currency, currency_name, sizeof(aoc_e->recorded.money.currency));
3693 case AST_AOC_CHARGE_UNIT:
3695 const struct ast_aoc_unit_entry *entry;
3697 aoc_e->charge = PRI_AOC_DE_CHARGE_UNITS;
3698 for (i = 0; i < ast_aoc_get_unit_count(decoded); i++) {
3699 if ((entry = ast_aoc_get_unit_info(decoded, i)) && i < ARRAY_LEN(aoc_e->recorded.unit.item)) {
3700 if (entry->valid_amount) {
3701 aoc_e->recorded.unit.item[i].number = entry->amount;
3703 aoc_e->recorded.unit.item[i].number = -1;
3705 if (entry->valid_type) {
3706 aoc_e->recorded.unit.item[i].type = entry->type;
3708 aoc_e->recorded.unit.item[i].type = -1;
3710 aoc_e->recorded.unit.num_items++;
3715 case AST_AOC_CHARGE_NA:
3717 aoc_e->charge = PRI_AOC_DE_CHARGE_NOT_AVAILABLE;
3721 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3723 #if defined(HAVE_PRI_AOC_EVENTS)
3726 * \brief send an AOC-E termination request on ast_channel and set
3729 * \param pri PRI span control structure.
3730 * \param chanpos Channel position in the span.
3731 * \param ms to delay hangup
3733 * \note Assumes the pri->lock is already obtained.
3734 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
3738 static void sig_pri_send_aoce_termination_request(struct sig_pri_span *pri, int chanpos, unsigned int ms)
3740 struct sig_pri_chan *pvt;
3741 struct ast_aoc_decoded *decoded = NULL;
3742 struct ast_aoc_encoded *encoded = NULL;
3743 size_t encoded_size;
3744 struct timeval whentohangup = { 0, };
3746 sig_pri_lock_owner(pri, chanpos);
3747 pvt = pri->pvts[chanpos];
3752 if (!(decoded = ast_aoc_create(AST_AOC_REQUEST, 0, AST_AOC_REQUEST_E))) {
3753 ast_softhangup_nolock(pvt->owner, AST_SOFTHANGUP_DEV);
3754 goto cleanup_termination_request;
3757 ast_aoc_set_termination_request(decoded);
3759 if (!(encoded = ast_aoc_encode(decoded, &encoded_size, pvt->owner))) {
3760 ast_softhangup_nolock(pvt->owner, AST_SOFTHANGUP_DEV);
3761 goto cleanup_termination_request;
3764 /* convert ms to timeval */
3765 whentohangup.tv_usec = (ms % 1000) * 1000;
3766 whentohangup.tv_sec = ms / 1000;
3768 if (ast_queue_control_data(pvt->owner, AST_CONTROL_AOC, encoded, encoded_size)) {
3769 ast_softhangup_nolock(pvt->owner, AST_SOFTHANGUP_DEV);
3770 goto cleanup_termination_request;
3773 pvt->waiting_for_aoce = 1;
3774 ast_channel_setwhentohangup_tv(pvt->owner, whentohangup);
3775 ast_debug(1, "Delaying hangup on %s for aoc-e msg\n", pvt->owner->name);
3777 cleanup_termination_request:
3778 ast_channel_unlock(pvt->owner);
3779 ast_aoc_destroy_decoded(decoded);
3780 ast_aoc_destroy_encoded(encoded);
3782 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3786 * \brief TRUE if PRI event came in on a CIS call.
3789 * \param channel PRI encoded span/channel
3791 * \retval non-zero if CIS call.
3793 static int sig_pri_is_cis_call(int channel)
3795 return channel != -1 && (channel & PRI_CIS_CALL);
3800 * \brief Handle the CIS associated PRI subcommand events.
3803 * \param pri PRI span control structure.
3804 * \param event_id PRI event id
3805 * \param subcmds Subcommands to process if any. (Could be NULL).
3806 * \param call_rsp libpri opaque call structure to send any responses toward.
3807 * Could be NULL either because it is not available or the call is for the
3808 * dummy call reference. However, this should not be NULL in the cases that
3809 * need to use the pointer to send a response message back.
3811 * \note Assumes the pri->lock is already obtained.
3815 static void sig_pri_handle_cis_subcmds(struct sig_pri_span *pri, int event_id,
3816 const struct pri_subcommands *subcmds, q931_call *call_rsp)
3819 #if defined(HAVE_PRI_CCSS)
3820 struct ast_cc_agent *agent;
3821 struct sig_pri_cc_agent_prv *agent_prv;
3822 struct sig_pri_cc_monitor_instance *monitor;
3823 #endif /* defined(HAVE_PRI_CCSS) */
3828 for (index = 0; index < subcmds->counter_subcmd; ++index) {
3829 const struct pri_subcommand *subcmd = &subcmds->subcmd[index];
3831 switch (subcmd->cmd) {
3832 #if defined(STATUS_REQUEST_PLACE_HOLDER)
3833 case PRI_SUBCMD_STATUS_REQ:
3834 case PRI_SUBCMD_STATUS_REQ_RSP:
3835 /* Ignore for now. */
3837 #endif /* defined(STATUS_REQUEST_PLACE_HOLDER) */
3838 #if defined(HAVE_PRI_CCSS)
3839 case PRI_SUBCMD_CC_REQ:
3840 agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_request.cc_id);
3842 pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
3845 if (!ast_cc_request_is_within_limits()) {
3846 if (pri_cc_req_rsp(pri->pri, subcmd->u.cc_request.cc_id,
3847 5/* queue_full */)) {
3848 pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
3850 ast_cc_failed(agent->core_id, "%s agent system CC queue full",
3851 sig_pri_cc_type_name);
3855 agent_prv = agent->private_data;
3856 agent_prv->cc_request_response_pending = 1;
3857 if (ast_cc_agent_accept_request(agent->core_id,
3858 "%s caller accepted CC offer.", sig_pri_cc_type_name)) {
3859 agent_prv->cc_request_response_pending = 0;
3860 if (pri_cc_req_rsp(pri->pri, subcmd->u.cc_request.cc_id,
3861 2/* short_term_denial */)) {
3862 pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
3864 ast_cc_failed(agent->core_id, "%s agent CC core request accept failed"