Debian Codegraph @

channels/sig_pri.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2009, Digium, Inc.
5 *
6 * Mark Spencer <[email protected]>
7 *
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.
13 *
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.
17 */
18
19 /*! \file
20 *
21 * \brief PRI signaling module
22 *
23 * \author Matthew Fredrickson <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29 /*** DOCUMENTATION
30 <managerEvent language="en_US" name="MCID">
31 <managerEventInstance class="EVENT_FLAG_CALL">
32 <since>
33 <version>12.0.0</version>
34 </since>
35 <synopsis>Published when a malicious call ID request arrives.</synopsis>
36 <syntax>
37 <channel_snapshot/>
38 <parameter name="MCallerIDNumValid">
39 </parameter>
40 <parameter name="MCallerIDNum">
41 </parameter>
42 <parameter name="MCallerIDton">
43 </parameter>
44 <parameter name="MCallerIDNumPlan">
45 </parameter>
46 <parameter name="MCallerIDNumPres">
47 </parameter>
48 <parameter name="MCallerIDNameValid">
49 </parameter>
50 <parameter name="MCallerIDName">
51 </parameter>
52 <parameter name="MCallerIDNameCharSet">
53 </parameter>
54 <parameter name="MCallerIDNamePres">
55 </parameter>
56 <parameter name="MCallerIDSubaddr">
57 </parameter>
58 <parameter name="MCallerIDSubaddrType">
59 </parameter>
60 <parameter name="MCallerIDSubaddrOdd">
61 </parameter>
62 <parameter name="MCallerIDPres">
63 </parameter>
64 <parameter name="MConnectedIDNumValid">
65 </parameter>
66 <parameter name="MConnectedIDNum">
67 </parameter>
68 <parameter name="MConnectedIDton">
69 </parameter>
70 <parameter name="MConnectedIDNumPlan">
71 </parameter>
72 <parameter name="MConnectedIDNumPres">
73 </parameter>
74 <parameter name="MConnectedIDNameValid">
75 </parameter>
76 <parameter name="MConnectedIDName">
77 </parameter>
78 <parameter name="MConnectedIDNameCharSet">
79 </parameter>
80 <parameter name="MConnectedIDNamePres">
81 </parameter>
82 <parameter name="MConnectedIDSubaddr">
83 </parameter>
84 <parameter name="MConnectedIDSubaddrType">
85 </parameter>
86 <parameter name="MConnectedIDSubaddrOdd">
87 </parameter>
88 <parameter name="MConnectedIDPres">
89 </parameter>
90 </syntax>
91 </managerEventInstance>
92 </managerEvent>
93 ***/
94
95 #include "asterisk.h"
96
97 #ifdef HAVE_PRI
98
99 #include <errno.h>
100 #include <ctype.h>
101 #include <signal.h>
102
103 #include "asterisk/utils.h"
104 #include "asterisk/options.h"
105 #include "asterisk/pbx.h"
106 #include "asterisk/app.h"
107 #include "asterisk/mwi.h"
108 #include "asterisk/file.h"
109 #include "asterisk/callerid.h"
110 #include "asterisk/say.h"
111 #include "asterisk/manager.h"
112 #include "asterisk/astdb.h"
113 #include "asterisk/causes.h"
114 #include "asterisk/musiconhold.h"
115 #include "asterisk/cli.h"
116 #include "asterisk/transcap.h"
117 #include "asterisk/features.h"
118 #include "asterisk/aoc.h"
119 #include "asterisk/bridge.h"
120 #include "asterisk/stasis_channels.h"
121
122 #include "sig_pri.h"
123 #ifndef PRI_EVENT_FACILITY
124 #error "Upgrade your libpri"
125 #endif
126
127 /*** DOCUMENTATION
128 ***/
129
130
131 /* define this to send PRI user-user information elements */
132 #undef SUPPORT_USERUSER
133
134 /*!
135 * Define to make always pick a channel if allowed. Useful for
136 * testing channel shifting.
137 */
138 //#define ALWAYS_PICK_CHANNEL 1
139
140 #if defined(HAVE_PRI_CCSS)
141 struct sig_pri_cc_agent_prv {
142 /*! Asterisk span D channel control structure. */
143 struct sig_pri_span *pri;
144 /*! CC id value to use with libpri. -1 if invalid. */
145 long cc_id;
146 /*! TRUE if CC has been requested and we are waiting for the response. */
147 unsigned char cc_request_response_pending;
148 };
149
150 struct sig_pri_cc_monitor_instance {
151 /*! \brief Asterisk span D channel control structure. */
152 struct sig_pri_span *pri;
153 /*! CC id value to use with libpri. (-1 if already canceled). */
154 long cc_id;
155 /*! CC core id value. */
156 int core_id;
157 /*! Device name(Channel name less sequence number) */
158 char name[1];
159 };
160
161 /*! Upper level agent/monitor type name. */
162 static const char *sig_pri_cc_type_name;
163 /*! Container of sig_pri monitor instances. */
164 static struct ao2_container *sig_pri_cc_monitors;
165 #endif /* defined(HAVE_PRI_CCSS) */
166
167 static int pri_matchdigittimeout = 3000;
168
169 static int pri_gendigittimeout = 8000;
170
171 #define DCHAN_NOTINALARM (1 << 0)
172 #define DCHAN_UP (1 << 1)
173
174 /* Defines to help decode the encoded event channel id. */
175 #define PRI_CHANNEL(p) ((p) & 0xff)
176 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
177 #define PRI_EXPLICIT (1 << 16)
178 #define PRI_CIS_CALL (1 << 17) /* Call is using the D channel only. */
179 #define PRI_HELD_CALL (1 << 18)
180
181
182 #define DCHAN_AVAILABLE (DCHAN_NOTINALARM | DCHAN_UP)
183
184 static int pri_active_dchan_index(struct sig_pri_span *pri);
185
186 static const char *sig_pri_call_level2str(enum sig_pri_call_level level)
187 {
188 switch (level) {
189 case SIG_PRI_CALL_LEVEL_IDLE:
190 return "Idle";
191 case SIG_PRI_CALL_LEVEL_SETUP:
192 return "Setup";
193 case SIG_PRI_CALL_LEVEL_OVERLAP:
194 return "Overlap";
195 case SIG_PRI_CALL_LEVEL_PROCEEDING:
196 return "Proceeding";
197 case SIG_PRI_CALL_LEVEL_ALERTING:
198 return "Alerting";
199 case SIG_PRI_CALL_LEVEL_DEFER_DIAL:
200 return "DeferDial";
201 case SIG_PRI_CALL_LEVEL_CONNECT:
202 return "Connect";
203 }
204 return "Unknown";
205 }
206
207 static inline void pri_rel(struct sig_pri_span *pri)
208 {
209 ast_mutex_unlock(&pri->lock);
210 }
211
212 static unsigned int PVT_TO_CHANNEL(struct sig_pri_chan *p)
213 {
214 int res = (((p)->prioffset) | ((p)->logicalspan << 8) | (p->mastertrunkgroup ? PRI_EXPLICIT : 0));
215 ast_debug(5, "prioffset: %d mastertrunkgroup: %d logicalspan: %d result: %d\n",
216 p->prioffset, p->mastertrunkgroup, p->logicalspan, res);
217
218 return res;
219 }
220
221 static void sig_pri_handle_dchan_exception(struct sig_pri_span *pri, int index)
222 {
223 if (sig_pri_callbacks.handle_dchan_exception) {
224 sig_pri_callbacks.handle_dchan_exception(pri, index);
225 }
226 }
227
228 static void sig_pri_set_dialing(struct sig_pri_chan *p, int is_dialing)
229 {
230 if (sig_pri_callbacks.set_dialing) {
231 sig_pri_callbacks.set_dialing(p->chan_pvt, is_dialing);
232 }
233 }
234
235 static void sig_pri_set_digital(struct sig_pri_chan *p, int is_digital)
236 {
237 p->digital = is_digital;
238 if (sig_pri_callbacks.set_digital) {
239 sig_pri_callbacks.set_digital(p->chan_pvt, is_digital);
240 }
241 }
242
243 static void sig_pri_set_outgoing(struct sig_pri_chan *p, int is_outgoing)
244 {
245 p->outgoing = is_outgoing;
246 if (sig_pri_callbacks.set_outgoing) {
247 sig_pri_callbacks.set_outgoing(p->chan_pvt, is_outgoing);
248 }
249 }
250
251 void sig_pri_set_alarm(struct sig_pri_chan *p, int in_alarm)
252 {
253 if (sig_pri_is_alarm_ignored(p->pri)) {
254 /* Always set not in alarm */
255 in_alarm = 0;
256 }
257
258 /*
259 * Clear the channel restart state when the channel alarm
260 * changes to prevent the state from getting stuck when the link
261 * goes down.
262 */
263 p->resetting = SIG_PRI_RESET_IDLE;
264
265 p->inalarm = in_alarm;
266 if (sig_pri_callbacks.set_alarm) {
267 sig_pri_callbacks.set_alarm(p->chan_pvt, in_alarm);
268 }
269 }
270
271 static const char *sig_pri_get_orig_dialstring(struct sig_pri_chan *p)
272 {
273 if (sig_pri_callbacks.get_orig_dialstring) {
274 return sig_pri_callbacks.get_orig_dialstring(p->chan_pvt);
275 }
276 ast_log(LOG_ERROR, "get_orig_dialstring callback not defined\n");
277 return "";
278 }
279
280 #if defined(HAVE_PRI_CCSS)
281 static void sig_pri_make_cc_dialstring(struct sig_pri_chan *p, char *buf, size_t buf_size)
282 {
283 if (sig_pri_callbacks.make_cc_dialstring) {
284 sig_pri_callbacks.make_cc_dialstring(p->chan_pvt, buf, buf_size);
285 } else {
286 ast_log(LOG_ERROR, "make_cc_dialstring callback not defined\n");
287 buf[0] = '\0';
288 }
289 }
290 #endif /* defined(HAVE_PRI_CCSS) */
291
292 static void sig_pri_dial_digits(struct sig_pri_chan *p, const char *dial_string)
293 {
294 if (sig_pri_callbacks.dial_digits) {
295 sig_pri_callbacks.dial_digits(p->chan_pvt, dial_string);
296 }
297 }
298
299 /*!
300 * \internal
301 * \brief Reevaluate the PRI span device state.
302 * \since 1.8
303 *
304 * \param pri PRI span control structure.
305 *
306 * \note Assumes the pri->lock is already obtained.
307 */
308 static void sig_pri_span_devstate_changed(struct sig_pri_span *pri)
309 {
310 if (sig_pri_callbacks.update_span_devstate) {
311 sig_pri_callbacks.update_span_devstate(pri);
312 }
313 }
314
315 /*!
316 * \internal
317 * \brief Set the caller id information in the parent module.
318 * \since 1.8
319 *
320 * \param p sig_pri channel structure.
321 */
322 static void sig_pri_set_caller_id(struct sig_pri_chan *p)
323 {
324 struct ast_party_caller caller;
325
326 if (sig_pri_callbacks.set_callerid) {
327 ast_party_caller_init(&caller);
328
329 caller.id.name.str = p->cid_name;
330 caller.id.name.presentation = p->callingpres;
331 caller.id.name.valid = 1;
332
333 caller.id.number.str = p->cid_num;
334 caller.id.number.plan = p->cid_ton;
335 caller.id.number.presentation = p->callingpres;
336 caller.id.number.valid = 1;
337
338 if (!ast_strlen_zero(p->cid_subaddr)) {
339 caller.id.subaddress.valid = 1;
340 //caller.id.subaddress.type = 0;/* nsap */
341 //caller.id.subaddress.odd_even_indicator = 0;
342 caller.id.subaddress.str = p->cid_subaddr;
343 }
344 caller.id.tag = p->user_tag;
345
346 caller.ani.number.str = p->cid_ani;
347 //caller.ani.number.plan = p->xxx;
348 //caller.ani.number.presentation = p->xxx;
349 caller.ani.number.valid = 1;
350
351 caller.ani2 = p->cid_ani2;
352 sig_pri_callbacks.set_callerid(p->chan_pvt, &caller);
353 }
354 }
355
356 /*!
357 * \internal
358 * \brief Set the Dialed Number Identifier.
359 * \since 1.8
360 *
361 * \param p sig_pri channel structure.
362 * \param dnid Dialed Number Identifier string.
363 */
364 static void sig_pri_set_dnid(struct sig_pri_chan *p, const char *dnid)
365 {
366 if (sig_pri_callbacks.set_dnid) {
367 sig_pri_callbacks.set_dnid(p->chan_pvt, dnid);
368 }
369 }
370
371 /*!
372 * \internal
373 * \brief Set the Redirecting Directory Number Information Service (RDNIS).
374 * \since 1.8
375 *
376 * \param p sig_pri channel structure.
377 * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
378 */
379 static void sig_pri_set_rdnis(struct sig_pri_chan *p, const char *rdnis)
380 {
381 if (sig_pri_callbacks.set_rdnis) {
382 sig_pri_callbacks.set_rdnis(p->chan_pvt, rdnis);
383 }
384 }
385
386 static void sig_pri_unlock_private(struct sig_pri_chan *p)
387 {
388 if (sig_pri_callbacks.unlock_private) {
389 sig_pri_callbacks.unlock_private(p->chan_pvt);
390 }
391 }
392
393 static void sig_pri_lock_private(struct sig_pri_chan *p)
394 {
395 if (sig_pri_callbacks.lock_private) {
396 sig_pri_callbacks.lock_private(p->chan_pvt);
397 }
398 }
399
400 static void sig_pri_deadlock_avoidance_private(struct sig_pri_chan *p)
401 {
402 if (sig_pri_callbacks.deadlock_avoidance_private) {
403 sig_pri_callbacks.deadlock_avoidance_private(p->chan_pvt);
404 } else {
405 /* Fallback to the old way if callback not present. */
406 sig_pri_unlock_private(p);
407 sched_yield();
408 sig_pri_lock_private(p);
409 }
410 }
411
412 static void pri_grab(struct sig_pri_chan *p, struct sig_pri_span *pri)
413 {
414 /* Grab the lock first */
415 while (ast_mutex_trylock(&pri->lock)) {
416 /* Avoid deadlock */
417 sig_pri_deadlock_avoidance_private(p);
418 }
419 /* Then break the poll */
420 if (pri->master != AST_PTHREADT_NULL) {
421 pthread_kill(pri->master, SIGURG);
422 }
423 }
424
425 /*!
426 * \internal
427 * \brief Convert PRI redirecting reason to asterisk version.
428 * \since 1.8
429 *
430 * \param pri_reason PRI redirecting reason.
431 *
432 * \return Equivalent asterisk redirecting reason value.
433 */
434 static enum AST_REDIRECTING_REASON pri_to_ast_reason(int pri_reason)
435 {
436 enum AST_REDIRECTING_REASON ast_reason;
437
438 switch (pri_reason) {
439 case PRI_REDIR_FORWARD_ON_BUSY:
440 ast_reason = AST_REDIRECTING_REASON_USER_BUSY;
441 break;
442 case PRI_REDIR_FORWARD_ON_NO_REPLY:
443 ast_reason = AST_REDIRECTING_REASON_NO_ANSWER;
444 break;
445 case PRI_REDIR_DEFLECTION:
446 ast_reason = AST_REDIRECTING_REASON_DEFLECTION;
447 break;
448 case PRI_REDIR_UNCONDITIONAL:
449 ast_reason = AST_REDIRECTING_REASON_UNCONDITIONAL;
450 break;
451 case PRI_REDIR_UNKNOWN:
452 default:
453 ast_reason = AST_REDIRECTING_REASON_UNKNOWN;
454 break;
455 }
456
457 return ast_reason;
458 }
459
460 /*!
461 * \internal
462 * \brief Convert asterisk redirecting reason to PRI version.
463 * \since 1.8
464 *
465 * \param ast_reason Asterisk redirecting reason.
466 *
467 * \return Equivalent PRI redirecting reason value.
468 */
469 static int ast_to_pri_reason(enum AST_REDIRECTING_REASON ast_reason)
470 {
471 int pri_reason;
472
473 switch (ast_reason) {
474 case AST_REDIRECTING_REASON_USER_BUSY:
475 pri_reason = PRI_REDIR_FORWARD_ON_BUSY;
476 break;
477 case AST_REDIRECTING_REASON_NO_ANSWER:
478 pri_reason = PRI_REDIR_FORWARD_ON_NO_REPLY;
479 break;
480 case AST_REDIRECTING_REASON_UNCONDITIONAL:
481 pri_reason = PRI_REDIR_UNCONDITIONAL;
482 break;
483 case AST_REDIRECTING_REASON_DEFLECTION:
484 pri_reason = PRI_REDIR_DEFLECTION;
485 break;
486 case AST_REDIRECTING_REASON_UNKNOWN:
487 default:
488 pri_reason = PRI_REDIR_UNKNOWN;
489 break;
490 }
491
492 return pri_reason;
493 }
494
495 /*!
496 * \internal
497 * \brief Convert PRI number presentation to asterisk version.
498 * \since 1.8
499 *
500 * \param pri_presentation PRI number presentation.
501 *
502 * \return Equivalent asterisk number presentation value.
503 */
504 static int pri_to_ast_presentation(int pri_presentation)
505 {
506 int ast_presentation;
507
508 switch (pri_presentation) {
509 case PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED:
510 ast_presentation = AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED;
511 break;
512 case PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_PASSED_SCREEN:
513 ast_presentation = AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_PASSED_SCREEN;
514 break;
515 case PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_FAILED_SCREEN:
516 ast_presentation = AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_FAILED_SCREEN;
517 break;
518 case PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER:
519 ast_presentation = AST_PRES_ALLOWED | AST_PRES_NETWORK_NUMBER;
520 break;
521
522 case PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED:
523 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
524 break;
525 case PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_PASSED_SCREEN:
526 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_PASSED_SCREEN;
527 break;
528 case PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_FAILED_SCREEN:
529 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_FAILED_SCREEN;
530 break;
531 case PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER:
532 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_NETWORK_NUMBER;
533 break;
534
535 case PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_UNSCREENED:
536 case PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_PASSED_SCREEN:
537 case PRI_PRES_UNAVAILABLE | PRI_PRES_USER_NUMBER_FAILED_SCREEN:
538 case PRI_PRES_UNAVAILABLE | PRI_PRES_NETWORK_NUMBER:
539 ast_presentation = AST_PRES_NUMBER_NOT_AVAILABLE;
540 break;
541
542 default:
543 ast_presentation = AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED;
544 break;
545 }
546
547 return ast_presentation;
548 }
549
550 /*!
551 * \internal
552 * \brief Convert asterisk number presentation to PRI version.
553 * \since 1.8
554 *
555 * \param ast_presentation Asterisk number presentation.
556 *
557 * \return Equivalent PRI number presentation value.
558 */
559 static int ast_to_pri_presentation(int ast_presentation)
560 {
561 int pri_presentation;
562
563 switch (ast_presentation) {
564 case AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_UNSCREENED:
565 pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_UNSCREENED;
566 break;
567 case AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_PASSED_SCREEN:
568 pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_PASSED_SCREEN;
569 break;
570 case AST_PRES_ALLOWED | AST_PRES_USER_NUMBER_FAILED_SCREEN:
571 pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_USER_NUMBER_FAILED_SCREEN;
572 break;
573 case AST_PRES_ALLOWED | AST_PRES_NETWORK_NUMBER:
574 pri_presentation = PRI_PRES_ALLOWED | PRI_PRES_NETWORK_NUMBER;
575 break;
576
577 case AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_UNSCREENED:
578 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
579 break;
580 case AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_PASSED_SCREEN:
581 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_PASSED_SCREEN;
582 break;
583 case AST_PRES_RESTRICTED | AST_PRES_USER_NUMBER_FAILED_SCREEN:
584 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_FAILED_SCREEN;
585 break;
586 case AST_PRES_RESTRICTED | AST_PRES_NETWORK_NUMBER:
587 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_NETWORK_NUMBER;
588 break;
589
590 case AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_UNSCREENED:
591 case AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_PASSED_SCREEN:
592 case AST_PRES_UNAVAILABLE | AST_PRES_USER_NUMBER_FAILED_SCREEN:
593 case AST_PRES_UNAVAILABLE | AST_PRES_NETWORK_NUMBER:
594 pri_presentation = PRES_NUMBER_NOT_AVAILABLE;
595 break;
596
597 default:
598 pri_presentation = PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
599 break;
600 }
601
602 return pri_presentation;
603 }
604
605 /*!
606 * \internal
607 * \brief Convert PRI name char_set to asterisk version.
608 * \since 1.8
609 *
610 * \param pri_char_set PRI name char_set.
611 *
612 * \return Equivalent asterisk name char_set value.
613 */
614 static enum AST_PARTY_CHAR_SET pri_to_ast_char_set(int pri_char_set)
615 {
616 enum AST_PARTY_CHAR_SET ast_char_set;
617
618 switch (pri_char_set) {
619 default:
620 case PRI_CHAR_SET_UNKNOWN:
621 ast_char_set = AST_PARTY_CHAR_SET_UNKNOWN;
622 break;
623 case PRI_CHAR_SET_ISO8859_1:
624 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_1;
625 break;
626 case PRI_CHAR_SET_WITHDRAWN:
627 ast_char_set = AST_PARTY_CHAR_SET_WITHDRAWN;
628 break;
629 case PRI_CHAR_SET_ISO8859_2:
630 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_2;
631 break;
632 case PRI_CHAR_SET_ISO8859_3:
633 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_3;
634 break;
635 case PRI_CHAR_SET_ISO8859_4:
636 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_4;
637 break;
638 case PRI_CHAR_SET_ISO8859_5:
639 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_5;
640 break;
641 case PRI_CHAR_SET_ISO8859_7:
642 ast_char_set = AST_PARTY_CHAR_SET_ISO8859_7;
643 break;
644 case PRI_CHAR_SET_ISO10646_BMPSTRING:
645 ast_char_set = AST_PARTY_CHAR_SET_ISO10646_BMPSTRING;
646 break;
647 case PRI_CHAR_SET_ISO10646_UTF_8STRING:
648 ast_char_set = AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING;
649 break;
650 }
651
652 return ast_char_set;
653 }
654
655 /*!
656 * \internal
657 * \brief Convert asterisk name char_set to PRI version.
658 * \since 1.8
659 *
660 * \param ast_char_set Asterisk name char_set.
661 *
662 * \return Equivalent PRI name char_set value.
663 */
664 static int ast_to_pri_char_set(enum AST_PARTY_CHAR_SET ast_char_set)
665 {
666 int pri_char_set;
667
668 switch (ast_char_set) {
669 default:
670 case AST_PARTY_CHAR_SET_UNKNOWN:
671 pri_char_set = PRI_CHAR_SET_UNKNOWN;
672 break;
673 case AST_PARTY_CHAR_SET_ISO8859_1:
674 pri_char_set = PRI_CHAR_SET_ISO8859_1;
675 break;
676 case AST_PARTY_CHAR_SET_WITHDRAWN:
677 pri_char_set = PRI_CHAR_SET_WITHDRAWN;
678 break;
679 case AST_PARTY_CHAR_SET_ISO8859_2:
680 pri_char_set = PRI_CHAR_SET_ISO8859_2;
681 break;
682 case AST_PARTY_CHAR_SET_ISO8859_3:
683 pri_char_set = PRI_CHAR_SET_ISO8859_3;
684 break;
685 case AST_PARTY_CHAR_SET_ISO8859_4:
686 pri_char_set = PRI_CHAR_SET_ISO8859_4;
687 break;
688 case AST_PARTY_CHAR_SET_ISO8859_5:
689 pri_char_set = PRI_CHAR_SET_ISO8859_5;
690 break;
691 case AST_PARTY_CHAR_SET_ISO8859_7:
692 pri_char_set = PRI_CHAR_SET_ISO8859_7;
693 break;
694 case AST_PARTY_CHAR_SET_ISO10646_BMPSTRING:
695 pri_char_set = PRI_CHAR_SET_ISO10646_BMPSTRING;
696 break;
697 case AST_PARTY_CHAR_SET_ISO10646_UTF_8STRING:
698 pri_char_set = PRI_CHAR_SET_ISO10646_UTF_8STRING;
699 break;
700 }
701
702 return pri_char_set;
703 }
704
705 #if defined(HAVE_PRI_SUBADDR)
706 /*!
707 * \internal
708 * \brief Fill in the asterisk party subaddress from the given PRI party subaddress.
709 * \since 1.8
710 *
711 * \param ast_subaddress Asterisk party subaddress structure.
712 * \param pri_subaddress PRI party subaddress structure.
713 */
714 static void sig_pri_set_subaddress(struct ast_party_subaddress *ast_subaddress, const struct pri_party_subaddress *pri_subaddress)
715 {
716 ast_free(ast_subaddress->str);
717 if (pri_subaddress->length <= 0) {
718 ast_party_subaddress_init(ast_subaddress);
719 return;
720 }
721
722 if (!pri_subaddress->type) {
723 /* NSAP */
724 ast_subaddress->str = ast_strdup((char *) pri_subaddress->data);
725 } else {
726 char *cnum;
727 char *ptr;
728 int x;
729 int len;
730
731 /* User Specified */
732 cnum = ast_malloc(2 * pri_subaddress->length + 1);
733 if (!cnum) {
734 ast_party_subaddress_init(ast_subaddress);
735 return;
736 }
737
738 ptr = cnum;
739 len = pri_subaddress->length - 1; /* -1 account for zero based indexing */
740 for (x = 0; x < len; ++x) {
741 ptr += sprintf(ptr, "%02hhx", (unsigned char)pri_subaddress->data[x]);
742 }
743
744 if (pri_subaddress->odd_even_indicator) {
745 /* ODD */
746 sprintf(ptr, "%01hhx", (unsigned char)((pri_subaddress->data[len]) >> 4));
747 } else {
748 /* EVEN */
749 sprintf(ptr, "%02hhx", (unsigned char)pri_subaddress->data[len]);
750 }
751 ast_subaddress->str = cnum;
752 }
753 ast_subaddress->type = pri_subaddress->type;
754 ast_subaddress->odd_even_indicator = pri_subaddress->odd_even_indicator;
755 ast_subaddress->valid = 1;
756 }
757 #endif /* defined(HAVE_PRI_SUBADDR) */
758
759 #if defined(HAVE_PRI_SUBADDR)
760 static unsigned char ast_pri_pack_hex_char(char c)
761 {
762 unsigned char res;
763
764 if (c < '0') {
765 res = 0;
766 } else if (c < ('9' + 1)) {
767 res = c - '0';
768 } else if (c < 'A') {
769 res = 0;
770 } else if (c < ('F' + 1)) {
771 res = c - 'A' + 10;
772 } else if (c < 'a') {
773 res = 0;
774 } else if (c < ('f' + 1)) {
775 res = c - 'a' + 10;
776 } else {
777 res = 0;
778 }
779 return res;
780 }
781 #endif /* defined(HAVE_PRI_SUBADDR) */
782
783 #if defined(HAVE_PRI_SUBADDR)
784 /*!
785 * \internal
786 * \brief Convert a null terminated hexadecimal string to a packed hex byte array.
787 * \details left justified, with 0 padding if odd length.
788 * \since 1.8
789 *
790 * \param dst pointer to packed byte array.
791 * \param src pointer to null terminated hexadecimal string.
792 * \param maxlen destination array size.
793 *
794 * \return Length of byte array
795 *
796 * \note The dst is not an ASCIIz string.
797 * \note The src is an ASCIIz hex string.
798 */
799 static int ast_pri_pack_hex_string(unsigned char *dst, char *src, int maxlen)
800 {
801 int res = 0;
802 int len = strlen(src);
803
804 if (len > (2 * maxlen)) {
805 len = 2 * maxlen;
806 }
807
808 res = len / 2 + len % 2;
809
810 while (len > 1) {
811 *dst = ast_pri_pack_hex_char(*src) << 4;
812 src++;
813 *dst |= ast_pri_pack_hex_char(*src);
814 dst++, src++;
815 len -= 2;
816 }
817 if (len) { /* 1 left */
818 *dst = ast_pri_pack_hex_char(*src) << 4;
819 }
820 return res;
821 }
822 #endif /* defined(HAVE_PRI_SUBADDR) */
823
824 #if defined(HAVE_PRI_SUBADDR)
825 /*!
826 * \internal
827 * \brief Fill in the PRI party subaddress from the given asterisk party subaddress.
828 * \since 1.8
829 *
830 * \param pri_subaddress PRI party subaddress structure.
831 * \param ast_subaddress Asterisk party subaddress structure.
832 *
833 * \note Assumes that pri_subaddress has been previously memset to zero.
834 */
835 static void sig_pri_party_subaddress_from_ast(struct pri_party_subaddress *pri_subaddress, const struct ast_party_subaddress *ast_subaddress)
836 {
837 if (ast_subaddress->valid && !ast_strlen_zero(ast_subaddress->str)) {
838 pri_subaddress->type = ast_subaddress->type;
839 if (!ast_subaddress->type) {
840 /* 0 = NSAP */
841 ast_copy_string((char *) pri_subaddress->data, ast_subaddress->str,
842 sizeof(pri_subaddress->data));
843 pri_subaddress->length = strlen((char *) pri_subaddress->data);
844 pri_subaddress->odd_even_indicator = 0;
845 pri_subaddress->valid = 1;
846 } else {
847 /* 2 = User Specified */
848 /*
849 * Copy HexString to packed HexData,
850 * if odd length then right pad trailing byte with 0
851 */
852 int length = ast_pri_pack_hex_string(pri_subaddress->data,
853 ast_subaddress->str, sizeof(pri_subaddress->data));
854
855 pri_subaddress->length = length; /* packed data length */
856
857 length = strlen(ast_subaddress->str);
858 if (length > 2 * sizeof(pri_subaddress->data)) {
859 pri_subaddress->odd_even_indicator = 0;
860 } else {
861 pri_subaddress->odd_even_indicator = (length & 1);
862 }
863 pri_subaddress->valid = 1;
864 }
865 }
866 }
867 #endif /* defined(HAVE_PRI_SUBADDR) */
868
869 /*!
870 * \internal
871 * \brief Fill in the PRI party name from the given asterisk party name.
872 * \since 1.8
873 *
874 * \param pri_name PRI party name structure.
875 * \param ast_name Asterisk party name structure.
876 *
877 * \note Assumes that pri_name has been previously memset to zero.
878 */
879 static void sig_pri_party_name_from_ast(struct pri_party_name *pri_name, const struct ast_party_name *ast_name)
880 {
881 if (!ast_name->valid) {
882 return;
883 }
884 pri_name->valid = 1;
885 pri_name->presentation = ast_to_pri_presentation(ast_name->presentation);
886 pri_name->char_set = ast_to_pri_char_set(ast_name->char_set);
887 if (!ast_strlen_zero(ast_name->str)) {
888 ast_copy_string(pri_name->str, ast_name->str, sizeof(pri_name->str));
889 }
890 }
891
892 /*!
893 * \internal
894 * \brief Fill in the PRI party number from the given asterisk party number.
895 * \since 1.8
896 *
897 * \param pri_number PRI party number structure.
898 * \param ast_number Asterisk party number structure.
899 *
900 * \note Assumes that pri_number has been previously memset to zero.
901 */
902 static void sig_pri_party_number_from_ast(struct pri_party_number *pri_number, const struct ast_party_number *ast_number)
903 {
904 if (!ast_number->valid) {
905 return;
906 }
907 pri_number->valid = 1;
908 pri_number->presentation = ast_to_pri_presentation(ast_number->presentation);
909 pri_number->plan = ast_number->plan;
910 if (!ast_strlen_zero(ast_number->str)) {
911 ast_copy_string(pri_number->str, ast_number->str, sizeof(pri_number->str));
912 }
913 }
914
915 /*!
916 * \internal
917 * \brief Fill in the PRI party id from the given asterisk party id.
918 * \since 1.8
919 *
920 * \param pri_id PRI party id structure.
921 * \param ast_id Asterisk party id structure.
922 *
923 * \note Assumes that pri_id has been previously memset to zero.
924 */
925 static void sig_pri_party_id_from_ast(struct pri_party_id *pri_id, const struct ast_party_id *ast_id)
926 {
927 sig_pri_party_name_from_ast(&pri_id->name, &ast_id->name);
928 sig_pri_party_number_from_ast(&pri_id->number, &ast_id->number);
929 #if defined(HAVE_PRI_SUBADDR)
930 sig_pri_party_subaddress_from_ast(&pri_id->subaddress, &ast_id->subaddress);
931 #endif /* defined(HAVE_PRI_SUBADDR) */
932 }
933
934 /*!
935 * \internal
936 * \brief Update the PRI redirecting information for the current call.
937 * \since 1.8
938 *
939 * \param pvt sig_pri private channel structure.
940 * \param ast Asterisk channel
941 *
942 * \note Assumes that the PRI lock is already obtained.
943 */
944 static void sig_pri_redirecting_update(struct sig_pri_chan *pvt, struct ast_channel *ast)
945 {
946 struct pri_party_redirecting pri_redirecting;
947 const struct ast_party_redirecting *ast_redirecting;
948 struct ast_party_id redirecting_from = ast_channel_redirecting_effective_from(ast);
949 struct ast_party_id redirecting_to = ast_channel_redirecting_effective_to(ast);
950 struct ast_party_id redirecting_orig = ast_channel_redirecting_effective_orig(ast);
951
952 memset(&pri_redirecting, 0, sizeof(pri_redirecting));
953 ast_redirecting = ast_channel_redirecting(ast);
954 sig_pri_party_id_from_ast(&pri_redirecting.from, &redirecting_from);
955 sig_pri_party_id_from_ast(&pri_redirecting.to, &redirecting_to);
956 sig_pri_party_id_from_ast(&pri_redirecting.orig_called, &redirecting_orig);
957 pri_redirecting.count = ast_redirecting->count;
958 pri_redirecting.orig_reason = ast_to_pri_reason(ast_redirecting->orig_reason.code);
959 pri_redirecting.reason = ast_to_pri_reason(ast_redirecting->reason.code);
960
961 pri_redirecting_update(pvt->pri->pri, pvt->call, &pri_redirecting);
962 }
963
964 /*!
965 * \internal
966 * \brief Reset DTMF detector.
967 * \since 1.8
968 *
969 * \param p sig_pri channel structure.
970 */
971 static void sig_pri_dsp_reset_and_flush_digits(struct sig_pri_chan *p)
972 {
973 if (sig_pri_callbacks.dsp_reset_and_flush_digits) {
974 sig_pri_callbacks.dsp_reset_and_flush_digits(p->chan_pvt);
975 }
976 }
977
978 static int sig_pri_set_echocanceller(struct sig_pri_chan *p, int enable)
979 {
980 if (sig_pri_callbacks.set_echocanceller) {
981 return sig_pri_callbacks.set_echocanceller(p->chan_pvt, enable);
982 } else {
983 return -1;
984 }
985 }
986
987 static void sig_pri_fixup_chans(struct sig_pri_chan *old_chan, struct sig_pri_chan *new_chan)
988 {
989 if (sig_pri_callbacks.fixup_chans) {
990 sig_pri_callbacks.fixup_chans(old_chan->chan_pvt, new_chan->chan_pvt);
991 }
992 }
993
994 static int sig_pri_play_tone(struct sig_pri_chan *p, enum sig_pri_tone tone)
995 {
996 if (sig_pri_callbacks.play_tone) {
997 return sig_pri_callbacks.play_tone(p->chan_pvt, tone);
998 } else {
999 return -1;
1000 }
1001 }
1002
1003 static struct ast_channel *sig_pri_new_ast_channel(struct sig_pri_chan *p, int state,
1004 enum sig_pri_law law, int transfercapability, char *exten,
1005 const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
1006 {
1007 struct ast_channel *c;
1008
1009 if (sig_pri_callbacks.new_ast_channel) {
1010 c = sig_pri_callbacks.new_ast_channel(p->chan_pvt, state, law, exten, assignedids, requestor);
1011 } else {
1012 return NULL;
1013 }
1014 if (!c) {
1015 return NULL;
1016 }
1017
1018 ast_assert(p->owner == NULL || p->owner == c);
1019 p->owner = c;
1020 p->isidlecall = 0;
1021 p->alreadyhungup = 0;
1022 ast_channel_transfercapability_set(c, transfercapability);
1023 pbx_builtin_setvar_helper(c, "TRANSFERCAPABILITY",
1024 ast_transfercapability2str(transfercapability));
1025 if (transfercapability & AST_TRANS_CAP_DIGITAL) {
1026 sig_pri_set_digital(p, 1);
1027 }
1028 if (p->pri) {
1029 ast_mutex_lock(&p->pri->lock);
1030 sig_pri_span_devstate_changed(p->pri);
1031 ast_mutex_unlock(&p->pri->lock);
1032 }
1033
1034 return c;
1035 }
1036
1037 /*!
1038 * \internal
1039 * \brief Open the PRI channel media path.
1040 * \since 1.8
1041 *
1042 * \param p Channel private control structure.
1043 */
1044 static void sig_pri_open_media(struct sig_pri_chan *p)
1045 {
1046 if (p->no_b_channel) {
1047 return;
1048 }
1049
1050 if (sig_pri_callbacks.open_media) {
1051 sig_pri_callbacks.open_media(p->chan_pvt);
1052 }
1053 }
1054
1055 /*!
1056 * \internal
1057 * \brief Post an AMI B channel association event.
1058 * \since 1.8
1059 *
1060 * \param p Channel private control structure.
1061 *
1062 * \note Assumes the private and owner are locked.
1063 */
1064 static void sig_pri_ami_channel_event(struct sig_pri_chan *p)
1065 {
1066 if (sig_pri_callbacks.ami_channel_event) {
1067 sig_pri_callbacks.ami_channel_event(p->chan_pvt, p->owner);
1068 }
1069 }
1070
1071 struct ast_channel *sig_pri_request(struct sig_pri_chan *p, enum sig_pri_law law,
1072 const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor,
1073 int transfercapability)
1074 {
1075 struct ast_channel *ast;
1076
1077 ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
1078
1079 sig_pri_set_outgoing(p, 1);
1080 ast = sig_pri_new_ast_channel(p, AST_STATE_RESERVED, law, transfercapability,
1081 p->exten, assignedids, requestor);
1082 if (!ast) {
1083 sig_pri_set_outgoing(p, 0);
1084 }
1085 return ast;
1086 }
1087
1088 int pri_is_up(struct sig_pri_span *pri)
1089 {
1090 int x;
1091 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
1092 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
1093 return 1;
1094 }
1095 return 0;
1096 }
1097
1098 static const char *pri_order(int level)
1099 {
1100 switch (level) {
1101 case 0:
1102 return "Primary";
1103 case 1:
1104 return "Secondary";
1105 case 2:
1106 return "Tertiary";
1107 case 3:
1108 return "Quaternary";
1109 default:
1110 return "<Unknown>";
1111 }
1112 }
1113
1114 /* Returns index of the active dchan */
1115 static int pri_active_dchan_index(struct sig_pri_span *pri)
1116 {
1117 int x;
1118
1119 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
1120 if ((pri->dchans[x] == pri->pri))
1121 return x;
1122 }
1123
1124 ast_log(LOG_WARNING, "No active dchan found!\n");
1125 return -1;
1126 }
1127
1128 static void pri_find_dchan(struct sig_pri_span *pri)
1129 {
1130 struct pri *old;
1131 int oldslot = -1;
1132 int newslot = -1;
1133 int idx;
1134
1135 old = pri->pri;
1136 for (idx = 0; idx < SIG_PRI_NUM_DCHANS; ++idx) {
1137 if (!pri->dchans[idx]) {
1138 /* No more D channels defined on the span. */
1139 break;
1140 }
1141 if (pri->dchans[idx] == old) {
1142 oldslot = idx;
1143 }
1144 if (newslot < 0 && pri->dchanavail[idx] == DCHAN_AVAILABLE) {
1145 newslot = idx;
1146 }
1147 }
1148 /* At this point, idx is a count of how many D-channels are defined on the span. */
1149
1150 if (1 < idx) {
1151 /* We have several D-channels defined on the span. (NFAS PRI setup) */
1152 if (newslot < 0) {
1153 /* No D-channels available. Default to the primary D-channel. */
1154 newslot = 0;
1155
1156 if (!pri->no_d_channels) {
1157 pri->no_d_channels = 1;
1158 if (old && oldslot != newslot) {
1159 ast_log(LOG_WARNING,
1160 "Span %d: No D-channels up! Switching selected D-channel from %s to %s.\n",
1161 pri->span, pri_order(oldslot), pri_order(newslot));
1162 } else {
1163 ast_log(LOG_WARNING, "Span %d: No D-channels up!\n", pri->span);
1164 }
1165 }
1166 } else {
1167 pri->no_d_channels = 0;
1168 }
1169 if (old && oldslot != newslot) {
1170 ast_log(LOG_NOTICE,
1171 "Switching selected D-channel from %s (fd %d) to %s (fd %d)!\n",
1172 pri_order(oldslot), pri->fds[oldslot],
1173 pri_order(newslot), pri->fds[newslot]);
1174 }
1175 } else {
1176 if (newslot < 0) {
1177 /* The only D-channel is not up. */
1178 newslot = 0;
1179
1180 if (!pri->no_d_channels) {
1181 pri->no_d_channels = 1;
1182
1183 /*
1184 * This is annoying to see on non-persistent layer 2
1185 * connections. Let's not complain in that case.
1186 */
1187 if (pri->sig != SIG_BRI_PTMP) {
1188 ast_log(LOG_WARNING, "Span %d: D-channel is down!\n", pri->span);
1189 }
1190 }
1191 } else {
1192 pri->no_d_channels = 0;
1193 }
1194 }
1195 pri->pri = pri->dchans[newslot];
1196 }
1197
1198 /*!
1199 * \internal
1200 * \brief Determine if a private channel structure is in use.
1201 * \since 1.8
1202 *
1203 * \param pvt Channel to determine if in use.
1204 *
1205 * \return TRUE if the channel is in use.
1206 */
1207 static int sig_pri_is_chan_in_use(struct sig_pri_chan *pvt)
1208 {
1209 return pvt->owner || pvt->call || pvt->allocated || pvt->inalarm
1210 || pvt->resetting != SIG_PRI_RESET_IDLE;
1211 }
1212
1213 /*!
1214 * \brief Determine if a private channel structure is available.
1215 * \since 1.8
1216 *
1217 * \param pvt Channel to determine if available.
1218 *
1219 * \return TRUE if the channel is available.
1220 */
1221 int sig_pri_is_chan_available(struct sig_pri_chan *pvt)
1222 {
1223 return !sig_pri_is_chan_in_use(pvt)
1224 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1225 /* And not out-of-service */
1226 && !pvt->service_status
1227 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1228 ;
1229 }
1230
1231 /*!
1232 * \internal
1233 * \brief Obtain the sig_pri owner channel lock if the owner exists.
1234 * \since 1.8
1235 *
1236 * \param pri PRI span control structure.
1237 * \param chanpos Channel position in the span.
1238 *
1239 * \note Assumes the pri->lock is already obtained.
1240 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1241 */
1242 static void sig_pri_lock_owner(struct sig_pri_span *pri, int chanpos)
1243 {
1244 for (;;) {
1245 if (!pri->pvts[chanpos]->owner) {
1246 /* There is no owner lock to get. */
1247 break;
1248 }
1249 if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
1250 /* We got the lock */
1251 break;
1252 }
1253
1254 /* Avoid deadlock */
1255 sig_pri_unlock_private(pri->pvts[chanpos]);
1256 DEADLOCK_AVOIDANCE(&pri->lock);
1257 sig_pri_lock_private(pri->pvts[chanpos]);
1258 }
1259 }
1260
1261 /*!
1262 * \internal
1263 * \brief Queue the given frame onto the owner channel.
1264 * \since 1.8
1265 *
1266 * \param pri PRI span control structure.
1267 * \param chanpos Channel position in the span.
1268 * \param frame Frame to queue onto the owner channel.
1269 *
1270 * \note Assumes the pri->lock is already obtained.
1271 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1272 */
1273 static void pri_queue_frame(struct sig_pri_span *pri, int chanpos, struct ast_frame *frame)
1274 {
1275 sig_pri_lock_owner(pri, chanpos);
1276 if (pri->pvts[chanpos]->owner) {
1277 ast_queue_frame(pri->pvts[chanpos]->owner, frame);
1278 ast_channel_unlock(pri->pvts[chanpos]->owner);
1279 }
1280 }
1281
1282 /*!
1283 * \internal
1284 * \brief Queue a hold frame onto the owner channel.
1285 * \since 12
1286 *
1287 * \param pri PRI span control structure.
1288 * \param chanpos Channel position in the span.
1289 *
1290 * \note Assumes the pri->lock is already obtained.
1291 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1292 */
1293 static void sig_pri_queue_hold(struct sig_pri_span *pri, int chanpos)
1294 {
1295 sig_pri_lock_owner(pri, chanpos);
1296 if (pri->pvts[chanpos]->owner) {
1297 ast_queue_hold(pri->pvts[chanpos]->owner, NULL);
1298 ast_channel_unlock(pri->pvts[chanpos]->owner);
1299 }
1300 }
1301
1302 /*!
1303 * \internal
1304 * \brief Queue an unhold frame onto the owner channel.
1305 * \since 12
1306 *
1307 * \param pri PRI span control structure.
1308 * \param chanpos Channel position in the span.
1309 *
1310 * \note Assumes the pri->lock is already obtained.
1311 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1312 */
1313 static void sig_pri_queue_unhold(struct sig_pri_span *pri, int chanpos)
1314 {
1315 sig_pri_lock_owner(pri, chanpos);
1316 if (pri->pvts[chanpos]->owner) {
1317 ast_queue_unhold(pri->pvts[chanpos]->owner);
1318 ast_channel_unlock(pri->pvts[chanpos]->owner);
1319 }
1320 }
1321
1322 /*!
1323 * \internal
1324 * \brief Queue a control frame of the specified subclass onto the owner channel.
1325 * \since 1.8
1326 *
1327 * \param pri PRI span control structure.
1328 * \param chanpos Channel position in the span.
1329 * \param subclass Control frame subclass to queue onto the owner channel.
1330 *
1331 * \note Assumes the pri->lock is already obtained.
1332 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1333 */
1334 static void pri_queue_control(struct sig_pri_span *pri, int chanpos, int subclass)
1335 {
1336 struct ast_frame f = {AST_FRAME_CONTROL, };
1337
1338 if (sig_pri_callbacks.queue_control) {
1339 sig_pri_callbacks.queue_control(pri->pvts[chanpos]->chan_pvt, subclass);
1340 }
1341
1342 f.subclass.integer = subclass;
1343 pri_queue_frame(pri, chanpos, &f);
1344 }
1345
1346 /*!
1347 * \internal
1348 * \brief Queue a request to hangup control frame onto the owner channel.
1349 *
1350 * \param pri PRI span control structure.
1351 * \param chanpos Channel position in the span.
1352 *
1353 * \note Assumes the pri->lock is already obtained.
1354 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1355 *
1356 * \note The unlocking/locking sequence now present has been stress tested
1357 * without deadlocks. Please don't change it without consulting
1358 * core development team members.
1359 */
1360 static void sig_pri_queue_hangup(struct sig_pri_span *pri, int chanpos)
1361 {
1362 struct ast_channel *owner;
1363
1364 if (sig_pri_callbacks.queue_control) {
1365 sig_pri_callbacks.queue_control(pri->pvts[chanpos]->chan_pvt, AST_CONTROL_HANGUP);
1366 }
1367
1368 sig_pri_lock_owner(pri, chanpos);
1369 owner = pri->pvts[chanpos]->owner;
1370 if (owner) {
1371 ao2_ref(owner, +1);
1372 ast_queue_hangup(owner);
1373 ast_channel_unlock(owner);
1374
1375 sig_pri_unlock_private(pri->pvts[chanpos]);
1376 ast_mutex_unlock(&pri->lock);
1377 /* Tell the CDR this DAHDI channel hung up */
1378 ast_set_hangupsource(owner, ast_channel_name(owner), 0);
1379 ast_mutex_lock(&pri->lock);
1380 sig_pri_lock_private(pri->pvts[chanpos]);
1381
1382 ao2_ref(owner, -1);
1383 }
1384 }
1385
1386 /*!
1387 * \internal
1388 * \brief Queue a PVT_CAUSE_CODE frame onto the owner channel.
1389 * \since 11
1390 *
1391 * \param pri PRI span control structure.
1392 * \param chanpos Channel position in the span.
1393 * \param cause String describing the cause to be placed into the frame.
1394 * \param ast_cause
1395 *
1396 * \note Assumes the pri->lock is already obtained.
1397 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
1398 */
1399 static void pri_queue_pvt_cause_data(struct sig_pri_span *pri, int chanpos, const char *cause, int ast_cause)
1400 {
1401 struct ast_channel *chan;
1402 struct ast_control_pvt_cause_code *cause_code;
1403
1404 sig_pri_lock_owner(pri, chanpos);
1405 chan = pri->pvts[chanpos]->owner;
1406 if (chan) {
1407 int datalen = sizeof(*cause_code) + strlen(cause);
1408 cause_code = ast_alloca(datalen);
1409 memset(cause_code, 0, datalen);
1410 cause_code->ast_cause = ast_cause;
1411 ast_copy_string(cause_code->chan_name, ast_channel_name(chan), AST_CHANNEL_NAME);
1412 ast_copy_string(cause_code->code, cause, datalen + 1 - sizeof(*cause_code));
1413 ast_queue_control_data(chan, AST_CONTROL_PVT_CAUSE_CODE, cause_code, datalen);
1414 ast_channel_hangupcause_hash_set(chan, cause_code, datalen);
1415 ast_channel_unlock(chan);
1416 }
1417 }
1418
1419 /*!
1420 * \internal
1421 * \brief Find the channel associated with the libpri call.
1422 * \since 10.0
1423 *
1424 * \param pri PRI span control structure.
1425 * \param call LibPRI opaque call pointer to find.
1426 *
1427 * \note Assumes the pri->lock is already obtained.
1428 *
1429 * \retval array-index into private pointer array on success.
1430 * \retval -1 on error.
1431 */
1432 static int pri_find_principle_by_call(struct sig_pri_span *pri, q931_call *call)
1433 {
1434 int idx;
1435
1436 if (!call) {
1437 /* Cannot find a call without a call. */
1438 return -1;
1439 }
1440 for (idx = 0; idx < pri->numchans; ++idx) {
1441 if (pri->pvts[idx] && pri->pvts[idx]->call == call) {
1442 /* Found the principle */
1443 return idx;
1444 }
1445 }
1446 return -1;
1447 }
1448
1449 /*!
1450 * \internal
1451 * \brief Queue the span for destruction
1452 * \since 13.0
1453 *
1454 * \param pri PRI span control structure.
1455 *
1456 * Asks the channel driver to queue the span for destruction at a
1457 * possibly later time, if (e.g.) locking considerations don't allow
1458 * destroying it right now.
1459 */
1460 static void pri_destroy_later(struct sig_pri_span *pri)
1461 {
1462 if (!sig_pri_callbacks.destroy_later) {
1463 return;
1464 }
1465 sig_pri_callbacks.destroy_later(pri);
1466 }
1467
1468 /*!
1469 * \internal
1470 * \brief Kill the call.
1471 * \since 10.0
1472 *
1473 * \param pri PRI span control structure.
1474 * \param call LibPRI opaque call pointer to find.
1475 * \param cause Reason call was killed.
1476 *
1477 * \note Assumes the pvt->pri->lock is already obtained.
1478 */
1479 static void sig_pri_kill_call(struct sig_pri_span *pri, q931_call *call, int cause)
1480 {
1481 int chanpos;
1482
1483 chanpos = pri_find_principle_by_call(pri, call);
1484 if (chanpos < 0) {
1485 pri_hangup(pri->pri, call, cause);
1486 return;
1487 }
1488 sig_pri_lock_private(pri->pvts[chanpos]);
1489 if (!pri->pvts[chanpos]->owner) {
1490 pri_hangup(pri->pri, call, cause);
1491 pri->pvts[chanpos]->call = NULL;
1492 sig_pri_unlock_private(pri->pvts[chanpos]);
1493 sig_pri_span_devstate_changed(pri);
1494 return;
1495 }
1496 ast_channel_hangupcause_set(pri->pvts[chanpos]->owner, cause);
1497 pri_queue_control(pri, chanpos, AST_CONTROL_HANGUP);
1498 sig_pri_unlock_private(pri->pvts[chanpos]);
1499 }
1500
1501 /*!
1502 * \internal
1503 * \brief Find the private structure for the libpri call.
1504 *
1505 * \param pri PRI span control structure.
1506 * \param channel LibPRI encoded channel ID.
1507 * \param call LibPRI opaque call pointer.
1508 *
1509 * \note Assumes the pri->lock is already obtained.
1510 *
1511 * \retval array-index into private pointer array on success.
1512 * \retval -1 on error.
1513 */
1514 static int pri_find_principle(struct sig_pri_span *pri, int channel, q931_call *call)
1515 {
1516 int x;
1517 int span;
1518 int principle;
1519 int prioffset;
1520
1521 if (channel < 0) {
1522 /* Channel is not picked yet. */
1523 return -1;
1524 }
1525
1526 prioffset = PRI_CHANNEL(channel);
1527 if (!prioffset || (channel & PRI_HELD_CALL)) {
1528 /* Find the call waiting call or held call. */
1529 return pri_find_principle_by_call(pri, call);
1530 }
1531
1532 span = PRI_SPAN(channel);
1533 if (!(channel & PRI_EXPLICIT)) {
1534 int index;
1535
1536 index = pri_active_dchan_index(pri);
1537 if (index == -1) {
1538 return -1;
1539 }
1540 span = pri->dchan_logical_span[index];
1541 }
1542
1543 principle = -1;
1544 for (x = 0; x < pri->numchans; x++) {
1545 if (pri->pvts[x]
1546 && pri->pvts[x]->prioffset == prioffset
1547 && pri->pvts[x]->logicalspan == span
1548 && !pri->pvts[x]->no_b_channel) {
1549 principle = x;
1550 break;
1551 }
1552 }
1553
1554 return principle;
1555 }
1556
1557 /*!
1558 * \internal
1559 * \brief Fixup the private structure associated with the libpri call.
1560 *
1561 * \param pri PRI span control structure.
1562 * \param principle Array-index into private array to move call to if not already there.
1563 * \param call LibPRI opaque call pointer to find if need to move call.
1564 *
1565 * \note Assumes the pri->lock is already obtained.
1566 *
1567 * \retval principle on success.
1568 * \retval -1 on error.
1569 */
1570 static int pri_fixup_principle(struct sig_pri_span *pri, int principle, q931_call *call)
1571 {
1572 int x;
1573
1574 if (principle < 0 || pri->numchans <= principle) {
1575 /* Out of range */
1576 return -1;
1577 }
1578 if (!call) {
1579 /* No call */
1580 return principle;
1581 }
1582 if (pri->pvts[principle] && pri->pvts[principle]->call == call) {
1583 /* Call is already on the specified principle. */
1584 return principle;
1585 }
1586
1587 /* Find the old principle location. */
1588 for (x = 0; x < pri->numchans; x++) {
1589 struct sig_pri_chan *new_chan;
1590 struct sig_pri_chan *old_chan;
1591
1592 if (!pri->pvts[x] || pri->pvts[x]->call != call) {
1593 continue;
1594 }
1595
1596 /* Found our call */
1597 new_chan = pri->pvts[principle];
1598 old_chan = pri->pvts[x];
1599
1600 /* Get locks to safely move to the new private structure. */
1601 sig_pri_lock_private(old_chan);
1602 sig_pri_lock_owner(pri, x);
1603 sig_pri_lock_private(new_chan);
1604
1605 ast_verb(3, "Moving call (%s) from channel %d to %d.\n",
1606 old_chan->owner ? ast_channel_name(old_chan->owner) : "",
1607 old_chan->channel, new_chan->channel);
1608 if (!sig_pri_is_chan_available(new_chan)) {
1609 ast_log(LOG_WARNING,
1610 "Can't move call (%s) from channel %d to %d. It is already in use.\n",
1611 old_chan->owner ? ast_channel_name(old_chan->owner) : "",
1612 old_chan->channel, new_chan->channel);
1613 sig_pri_unlock_private(new_chan);
1614 if (old_chan->owner) {
1615 ast_channel_unlock(old_chan->owner);
1616 }
1617 sig_pri_unlock_private(old_chan);
1618 return -1;
1619 }
1620
1621 sig_pri_fixup_chans(old_chan, new_chan);
1622
1623 /* Fix it all up now */
1624 new_chan->owner = old_chan->owner;
1625 old_chan->owner = NULL;
1626
1627 new_chan->call = old_chan->call;
1628 old_chan->call = NULL;
1629
1630 /* Transfer flags from the old channel. */
1631 #if defined(HAVE_PRI_AOC_EVENTS)
1632 new_chan->aoc_s_request_invoke_id_valid = old_chan->aoc_s_request_invoke_id_valid;
1633 new_chan->waiting_for_aoce = old_chan->waiting_for_aoce;
1634 new_chan->holding_aoce = old_chan->holding_aoce;
1635 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1636 new_chan->alreadyhungup = old_chan->alreadyhungup;
1637 new_chan->isidlecall = old_chan->isidlecall;
1638 new_chan->progress = old_chan->progress;
1639 new_chan->allocated = old_chan->allocated;
1640 new_chan->outgoing = old_chan->outgoing;
1641 new_chan->digital = old_chan->digital;
1642 #if defined(HAVE_PRI_CALL_WAITING)
1643 new_chan->is_call_waiting = old_chan->is_call_waiting;
1644 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1645 #if defined(HAVE_PRI_SETUP_ACK_INBAND)
1646 new_chan->no_dialed_digits = old_chan->no_dialed_digits;
1647 #endif /* defined(HAVE_PRI_SETUP_ACK_INBAND) */
1648
1649 #if defined(HAVE_PRI_AOC_EVENTS)
1650 old_chan->aoc_s_request_invoke_id_valid = 0;
1651 old_chan->waiting_for_aoce = 0;
1652 old_chan->holding_aoce = 0;
1653 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1654 old_chan->alreadyhungup = 0;
1655 old_chan->isidlecall = 0;
1656 old_chan->progress = 0;
1657 old_chan->allocated = 0;
1658 old_chan->outgoing = 0;
1659 old_chan->digital = 0;
1660 #if defined(HAVE_PRI_CALL_WAITING)
1661 old_chan->is_call_waiting = 0;
1662 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1663 #if defined(HAVE_PRI_SETUP_ACK_INBAND)
1664 old_chan->no_dialed_digits = 0;
1665 #endif /* defined(HAVE_PRI_SETUP_ACK_INBAND) */
1666
1667 /* More stuff to transfer to the new channel. */
1668 new_chan->call_level = old_chan->call_level;
1669 old_chan->call_level = SIG_PRI_CALL_LEVEL_IDLE;
1670 #if defined(HAVE_PRI_REVERSE_CHARGE)
1671 new_chan->reverse_charging_indication = old_chan->reverse_charging_indication;
1672 #endif /* defined(HAVE_PRI_REVERSE_CHARGE) */
1673 #if defined(HAVE_PRI_SETUP_KEYPAD)
1674 strcpy(new_chan->keypad_digits, old_chan->keypad_digits);
1675 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
1676 strcpy(new_chan->deferred_digits, old_chan->deferred_digits);
1677 strcpy(new_chan->moh_suggested, old_chan->moh_suggested);
1678 new_chan->moh_state = old_chan->moh_state;
1679 old_chan->moh_state = SIG_PRI_MOH_STATE_IDLE;
1680 #if defined(HAVE_PRI_TRANSFER)
1681 new_chan->xfer_data = old_chan->xfer_data;
1682 old_chan->xfer_data = NULL;
1683 #endif /* defined(HAVE_PRI_TRANSFER) */
1684
1685 #if defined(HAVE_PRI_AOC_EVENTS)
1686 new_chan->aoc_s_request_invoke_id = old_chan->aoc_s_request_invoke_id;
1687 new_chan->aoc_e = old_chan->aoc_e;
1688 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
1689 strcpy(new_chan->user_tag, old_chan->user_tag);
1690
1691 if (new_chan->no_b_channel) {
1692 /* Copy the real channel configuration to the no B channel interface. */
1693 new_chan->hidecallerid = old_chan->hidecallerid;
1694 new_chan->hidecalleridname = old_chan->hidecalleridname;
1695 new_chan->immediate = old_chan->immediate;
1696 new_chan->priexclusive = old_chan->priexclusive;
1697 new_chan->priindication_oob = old_chan->priindication_oob;
1698 new_chan->use_callerid = old_chan->use_callerid;
1699 new_chan->use_callingpres = old_chan->use_callingpres;
1700 new_chan->stripmsd = old_chan->stripmsd;
1701 strcpy(new_chan->context, old_chan->context);
1702 strcpy(new_chan->mohinterpret, old_chan->mohinterpret);
1703
1704 /* Become a member of the old channel span/trunk-group. */
1705 new_chan->logicalspan = old_chan->logicalspan;
1706 new_chan->mastertrunkgroup = old_chan->mastertrunkgroup;
1707 } else if (old_chan->no_b_channel) {
1708 /*
1709 * We are transitioning from a held/call-waiting channel to a
1710 * real channel so we need to make sure that the media path is
1711 * open. (Needed especially if the channel is natively
1712 * bridged.)
1713 */
1714 sig_pri_open_media(new_chan);
1715 }
1716
1717 if (new_chan->owner) {
1718 sig_pri_ami_channel_event(new_chan);
1719 }
1720
1721 sig_pri_unlock_private(old_chan);
1722 if (new_chan->owner) {
1723 ast_channel_unlock(new_chan->owner);
1724 }
1725 sig_pri_unlock_private(new_chan);
1726
1727 return principle;
1728 }
1729 ast_verb(3, "Call specified, but not found.\n");
1730 return -1;
1731 }
1732
1733 /*!
1734 * \internal
1735 * \brief Find and fixup the private structure associated with the libpri call.
1736 *
1737 * \param pri PRI span control structure.
1738 * \param channel LibPRI encoded channel ID.
1739 * \param call LibPRI opaque call pointer.
1740 *
1741 * \details
1742 * This is a combination of pri_find_principle() and pri_fixup_principle()
1743 * to reduce code redundancy and to make handling several PRI_EVENT_xxx's
1744 * consistent for the current architecture.
1745 *
1746 * \note Assumes the pri->lock is already obtained.
1747 *
1748 * \retval array-index into private pointer array on success.
1749 * \retval -1 on error.
1750 */
1751 static int pri_find_fixup_principle(struct sig_pri_span *pri, int channel, q931_call *call)
1752 {
1753 int chanpos;
1754
1755 chanpos = pri_find_principle(pri, channel, call);
1756 if (chanpos < 0) {
1757 ast_log(LOG_WARNING, "Span %d: PRI requested channel %d/%d is unconfigured.\n",
1758 pri->span, PRI_SPAN(channel), PRI_CHANNEL(channel));
1759 sig_pri_kill_call(pri, call, PRI_CAUSE_IDENTIFIED_CHANNEL_NOTEXIST);
1760 return -1;
1761 }
1762 chanpos = pri_fixup_principle(pri, chanpos, call);
1763 if (chanpos < 0) {
1764 ast_log(LOG_WARNING, "Span %d: PRI requested channel %d/%d is not available.\n",
1765 pri->span, PRI_SPAN(channel), PRI_CHANNEL(channel));
1766 /*
1767 * Using Q.931 section 5.2.3.1 b) as the reason for picking
1768 * PRI_CAUSE_CHANNEL_UNACCEPTABLE. Receiving a
1769 * PRI_CAUSE_REQUESTED_CHAN_UNAVAIL would cause us to restart
1770 * that channel (which is not specified by Q.931) and kill some
1771 * other call which would be bad.
1772 */
1773 sig_pri_kill_call(pri, call, PRI_CAUSE_CHANNEL_UNACCEPTABLE);
1774 return -1;
1775 }
1776 return chanpos;
1777 }
1778
1779 static char * redirectingreason2str(int redirectingreason)
1780 {
1781 switch (redirectingreason) {
1782 case 0:
1783 return "UNKNOWN";
1784 case 1:
1785 return "BUSY";
1786 case 2:
1787 return "NO_REPLY";
1788 case 0xF:
1789 return "UNCONDITIONAL";
1790 default:
1791 return "NOREDIRECT";
1792 }
1793 }
1794
1795 static char *dialplan2str(int dialplan)
1796 {
1797 if (dialplan == -1) {
1798 return("Dynamically set dialplan in ISDN");
1799 }
1800 return (pri_plan2str(dialplan));
1801 }
1802
1803 /*!
1804 * \internal
1805 * \brief Apply numbering plan prefix to the given number.
1806 *
1807 * \param buf Buffer to put number into.
1808 * \param size Size of given buffer.
1809 * \param pri PRI span control structure.
1810 * \param number Number to apply numbering plan.
1811 * \param plan Numbering plan to apply.
1812 */
1813 static void apply_plan_to_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, int plan)
1814 {
1815 switch (plan) {
1816 case PRI_INTERNATIONAL_ISDN: /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */
1817 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
1818 break;
1819 case PRI_NATIONAL_ISDN: /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */
1820 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
1821 break;
1822 case PRI_LOCAL_ISDN: /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */
1823 snprintf(buf, size, "%s%s", pri->localprefix, number);
1824 break;
1825 case PRI_PRIVATE: /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */
1826 snprintf(buf, size, "%s%s", pri->privateprefix, number);
1827 break;
1828 case PRI_UNKNOWN: /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */
1829 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
1830 break;
1831 default: /* other Q.931 dialplan => don't twiddle with callingnum */
1832 snprintf(buf, size, "%s", number);
1833 break;
1834 }
1835 }
1836
1837 /*!
1838 * \internal
1839 * \brief Apply numbering plan prefix to the given number if the number exists.
1840 *
1841 * \param buf Buffer to put number into.
1842 * \param size Size of given buffer.
1843 * \param pri PRI span control structure.
1844 * \param number Number to apply numbering plan.
1845 * \param plan Numbering plan to apply.
1846 */
1847 static void apply_plan_to_existing_number(char *buf, size_t size, const struct sig_pri_span *pri, const char *number, int plan)
1848 {
1849 /* Make sure a number exists so the prefix isn't placed on an empty string. */
1850 if (ast_strlen_zero(number)) {
1851 if (size) {
1852 *buf = '\0';
1853 }
1854 return;
1855 }
1856 apply_plan_to_number(buf, size, pri, number, plan);
1857 }
1858
1859 /*!
1860 * \internal
1861 * \brief Restart the next channel we think is idle on the span.
1862 *
1863 * \param pri PRI span control structure.
1864 *
1865 * \note Assumes the pri->lock is already obtained.
1866 */
1867 static void pri_check_restart(struct sig_pri_span *pri)
1868 {
1869 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1870 unsigned why;
1871 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1872
1873 for (++pri->resetpos; pri->resetpos < pri->numchans; ++pri->resetpos) {
1874 if (!pri->pvts[pri->resetpos]
1875 || pri->pvts[pri->resetpos]->no_b_channel
1876 || sig_pri_is_chan_in_use(pri->pvts[pri->resetpos])) {
1877 continue;
1878 }
1879 #if defined(HAVE_PRI_SERVICE_MESSAGES)
1880 why = pri->pvts[pri->resetpos]->service_status;
1881 if (why) {
1882 ast_log(LOG_NOTICE,
1883 "Span %d: channel %d out-of-service (reason: %s), not sending RESTART\n",
1884 pri->span, pri->pvts[pri->resetpos]->channel,
1885 (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
1886 continue;
1887 }
1888 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
1889 break;
1890 }
1891 if (pri->resetpos < pri->numchans) {
1892 /* Mark the channel as resetting and restart it */
1893 pri->pvts[pri->resetpos]->resetting = SIG_PRI_RESET_ACTIVE;
1894 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
1895 } else {
1896 pri->resetting = 0;
1897 time(&pri->lastreset);
1898 sig_pri_span_devstate_changed(pri);
1899 }
1900 }
1901
1902 #if defined(HAVE_PRI_CALL_WAITING)
1903 /*!
1904 * \internal
1905 * \brief Init the private channel configuration using the span controller.
1906 * \since 1.8
1907 *
1908 * \param pvt Channel to init the configuration.
1909 * \param pri PRI span control structure.
1910 *
1911 * \note Assumes the pri->lock is already obtained.
1912 */
1913 static void sig_pri_init_config(struct sig_pri_chan *pvt, struct sig_pri_span *pri)
1914 {
1915 pvt->stripmsd = pri->ch_cfg.stripmsd;
1916 pvt->hidecallerid = pri->ch_cfg.hidecallerid;
1917 pvt->hidecalleridname = pri->ch_cfg.hidecalleridname;
1918 pvt->immediate = pri->ch_cfg.immediate;
1919 pvt->priexclusive = pri->ch_cfg.priexclusive;
1920 pvt->priindication_oob = pri->ch_cfg.priindication_oob;
1921 pvt->use_callerid = pri->ch_cfg.use_callerid;
1922 pvt->use_callingpres = pri->ch_cfg.use_callingpres;
1923 ast_copy_string(pvt->context, pri->ch_cfg.context, sizeof(pvt->context));
1924 ast_copy_string(pvt->mohinterpret, pri->ch_cfg.mohinterpret, sizeof(pvt->mohinterpret));
1925
1926 if (sig_pri_callbacks.init_config) {
1927 sig_pri_callbacks.init_config(pvt->chan_pvt, pri);
1928 }
1929 }
1930 #endif /* defined(HAVE_PRI_CALL_WAITING) */
1931
1932 /*!
1933 * \internal
1934 * \brief Find an empty B-channel interface to use.
1935 *
1936 * \param pri PRI span control structure.
1937 * \param backwards TRUE if the search starts from higher channels.
1938 *
1939 * \note Assumes the pri->lock is already obtained.
1940 *
1941 * \retval array-index into private pointer array on success.
1942 * \retval -1 on error.
1943 */
1944 static int pri_find_empty_chan(struct sig_pri_span *pri, int backwards)
1945 {
1946 int x;
1947 if (backwards)
1948 x = pri->numchans;
1949 else
1950 x = 0;
1951 for (;;) {
1952 if (backwards && (x < 0))
1953 break;
1954 if (!backwards && (x >= pri->numchans))
1955 break;
1956 if (pri->pvts[x]
1957 && !pri->pvts[x]->no_b_channel
1958 && sig_pri_is_chan_available(pri->pvts[x])) {
1959 ast_debug(1, "Found empty available channel %d/%d\n",
1960 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
1961 return x;
1962 }
1963 if (backwards)
1964 x--;
1965 else
1966 x++;
1967 }
1968 return -1;
1969 }
1970
1971 #if defined(HAVE_PRI_CALL_HOLD)
1972 /*!
1973 * \internal
1974 * \brief Find or create an empty no-B-channel interface to use.
1975 * \since 1.8
1976 *
1977 * \param pri PRI span control structure.
1978 *
1979 * \note Assumes the pri->lock is already obtained.
1980 *
1981 * \retval array-index into private pointer array on success.
1982 * \retval -1 on error.
1983 */
1984 static int pri_find_empty_nobch(struct sig_pri_span *pri)
1985 {
1986 int idx;
1987
1988 for (idx = 0; idx < pri->numchans; ++idx) {
1989 if (pri->pvts[idx]
1990 && pri->pvts[idx]->no_b_channel
1991 && sig_pri_is_chan_available(pri->pvts[idx])) {
1992 ast_debug(1, "Found empty available no B channel interface\n");
1993 return idx;
1994 }
1995 }
1996
1997 /* Need to create a new interface. */
1998 if (sig_pri_callbacks.new_nobch_intf) {
1999 idx = sig_pri_callbacks.new_nobch_intf(pri);
2000 } else {
2001 idx = -1;
2002 }
2003 return idx;
2004 }
2005 #endif /* defined(HAVE_PRI_CALL_HOLD) */
2006
2007 static void *do_idle_thread(void *v_pvt)
2008 {
2009 struct sig_pri_chan *pvt = v_pvt;
2010 struct ast_channel *chan = pvt->owner;
2011 struct ast_frame *f;
2012 char ex[128];
2013 /* Wait up to 30 seconds for an answer */
2014 int timeout_ms = 30000;
2015 int ms;
2016 struct timeval start;
2017 ast_callid callid;
2018
2019 if ((callid = ast_channel_callid(chan))) {
2020 ast_callid_threadassoc_add(callid);
2021 }
2022
2023 ast_verb(3, "Initiating idle call on channel %s\n", ast_channel_name(chan));
2024 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
2025 if (ast_call(chan, ex, 0)) {
2026 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", ast_channel_name(chan), ex);
2027 ast_hangup(chan);
2028 return NULL;
2029 }
2030 start = ast_tvnow();
2031 while ((ms = ast_remaining_ms(start, timeout_ms))) {
2032 if (ast_waitfor(chan, ms) <= 0) {
2033 break;
2034 }
2035
2036 f = ast_read(chan);
2037 if (!f) {
2038 /* Got hangup */
2039 break;
2040 }
2041 if (f->frametype == AST_FRAME_CONTROL) {
2042 switch (f->subclass.integer) {
2043 case AST_CONTROL_ANSWER:
2044 /* Launch the PBX */
2045 ast_channel_exten_set(chan, pvt->pri->idleext);
2046 ast_channel_context_set(chan, pvt->pri->idlecontext);
2047 ast_channel_priority_set(chan, 1);
2048 ast_verb(4, "Idle channel '%s' answered, sending to %s@%s\n", ast_channel_name(chan), ast_channel_exten(chan), ast_channel_context(chan));
2049 ast_pbx_run(chan);
2050 /* It's already hungup, return immediately */
2051 return NULL;
2052 case AST_CONTROL_BUSY:
2053 ast_verb(4, "Idle channel '%s' busy, waiting...\n", ast_channel_name(chan));
2054 break;
2055 case AST_CONTROL_CONGESTION:
2056 ast_verb(4, "Idle channel '%s' congested, waiting...\n", ast_channel_name(chan));
2057 break;
2058 };
2059 }
2060 ast_frfree(f);
2061 }
2062 /* Hangup the channel since nothing happened */
2063 ast_hangup(chan);
2064 return NULL;
2065 }
2066
2067 static void *pri_ss_thread(void *data)
2068 {
2069 struct sig_pri_chan *p = data;
2070 struct ast_channel *chan = p->owner;
2071 char exten[AST_MAX_EXTENSION];
2072 int res;
2073 int len;
2074 int timeout;
2075 ast_callid callid;
2076
2077 if (!chan) {
2078 /* We lost the owner before we could get started. */
2079 return NULL;
2080 }
2081
2082 if ((callid = ast_channel_callid(chan))) {
2083 ast_callid_threadassoc_add(callid);
2084 }
2085
2086 /*
2087 * In the bizarre case where the channel has become a zombie before we
2088 * even get started here, abort safely.
2089 */
2090 if (!ast_channel_tech_pvt(chan)) {
2091 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", ast_channel_name(chan));
2092 ast_hangup(chan);
2093 return NULL;
2094 }
2095
2096 ast_verb(3, "Starting simple switch on '%s'\n", ast_channel_name(chan));
2097
2098 sig_pri_dsp_reset_and_flush_digits(p);
2099
2100 /* Now loop looking for an extension */
2101 ast_copy_string(exten, p->exten, sizeof(exten));
2102 len = strlen(exten);
2103 res = 0;
2104 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2105 if (len && !ast_ignore_pattern(ast_channel_context(chan), exten))
2106 sig_pri_play_tone(p, -1);
2107 else
2108 sig_pri_play_tone(p, SIG_PRI_TONE_DIALTONE);
2109 if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num))
2110 timeout = pri_matchdigittimeout;
2111 else
2112 timeout = pri_gendigittimeout;
2113 res = ast_waitfordigit(chan, timeout);
2114 if (res < 0) {
2115 ast_debug(1, "waitfordigit returned < 0...\n");
2116 ast_hangup(chan);
2117 return NULL;
2118 } else if (res) {
2119 exten[len++] = res;
2120 exten[len] = '\0';
2121 } else
2122 break;
2123 }
2124 /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
2125 if (ast_strlen_zero(exten)) {
2126 ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
2127 exten[0] = 's';
2128 exten[1] = '\0';
2129 } else {
2130 ast_free(ast_channel_dialed(chan)->number.str);
2131 ast_channel_dialed(chan)->number.str = ast_strdup(exten);
2132
2133 if (p->pri->append_msn_to_user_tag && p->pri->nodetype != PRI_NETWORK) {
2134 /*
2135 * Update the user tag for party id's from this device for this call
2136 * now that we have a complete MSN from the network.
2137 */
2138 snprintf(p->user_tag, sizeof(p->user_tag), "%s_%s", p->pri->initial_user_tag,
2139 exten);
2140 ast_free(ast_channel_caller(chan)->id.tag);
2141 ast_channel_caller(chan)->id.tag = ast_strdup(p->user_tag);
2142 }
2143 }
2144 sig_pri_play_tone(p, -1);
2145 if (ast_exists_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2146 /* Start the real PBX */
2147 ast_channel_exten_set(chan, exten);
2148 sig_pri_dsp_reset_and_flush_digits(p);
2149 #if defined(JIRA_ASTERISK_15594)
2150 /*
2151 * Conditionaled out this code to effectively revert the JIRA
2152 * ASTERISK-15594 change. It breaks overlap dialing through
2153 * Asterisk. There is not enough information available at this
2154 * point to know if dialing is complete. The
2155 * ast_exists_extension(), ast_matchmore_extension(), and
2156 * ast_canmatch_extension() calls are not adequate to detect a
2157 * dial through extension pattern of "_9!".
2158 *
2159 * Workaround is to use the dialplan Proceeding() application
2160 * early on non-dial through extensions.
2161 */
2162 if ((p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
2163 && !ast_matchmore_extension(chan, ast_channel_context(chan), exten, 1, p->cid_num)) {
2164 sig_pri_lock_private(p);
2165 if (p->pri->pri) {
2166 pri_grab(p, p->pri);
2167 if (p->call_level < SIG_PRI_CALL_LEVEL_PROCEEDING) {
2168 p->call_level = SIG_PRI_CALL_LEVEL_PROCEEDING;
2169 }
2170 pri_proceeding(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 0);
2171 pri_rel(p->pri);
2172 }
2173 sig_pri_unlock_private(p);
2174 }
2175 #endif /* defined(JIRA_ASTERISK_15594) */
2176
2177 sig_pri_set_echocanceller(p, 1);
2178 ast_channel_lock(chan);
2179 ast_setstate(chan, AST_STATE_RING);
2180 ast_channel_unlock(chan);
2181 res = ast_pbx_run(chan);
2182 if (res) {
2183 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
2184 }
2185 } else {
2186 ast_debug(1, "No such possible extension '%s' in context '%s'\n", exten, ast_channel_context(chan));
2187 ast_channel_hangupcause_set(chan, AST_CAUSE_UNALLOCATED);
2188 ast_hangup(chan);
2189 p->exten[0] = '\0';
2190 /* Since we send release complete here, we won't get one */
2191 p->call = NULL;
2192 ast_mutex_lock(&p->pri->lock);
2193 sig_pri_span_devstate_changed(p->pri);
2194 ast_mutex_unlock(&p->pri->lock);
2195 }
2196 return NULL;
2197 }
2198
2199 void pri_event_alarm(struct sig_pri_span *pri, int index, int before_start_pri)
2200 {
2201 pri->dchanavail[index] &= ~DCHAN_NOTINALARM;
2202 if (!before_start_pri) {
2203 pri_find_dchan(pri);
2204 }
2205 }
2206
2207 void pri_event_noalarm(struct sig_pri_span *pri, int index, int before_start_pri)
2208 {
2209 pri->dchanavail[index] |= DCHAN_NOTINALARM;
2210 if (!before_start_pri)
2211 pri_restart(pri->dchans[index]);
2212 }
2213
2214 /*!
2215 * \internal
2216 * \brief Convert libpri party name into asterisk party name.
2217 * \since 1.8
2218 *
2219 * \param ast_name Asterisk party name structure to fill. Must already be set initialized.
2220 * \param pri_name libpri party name structure containing source information.
2221 *
2222 * \note The filled in ast_name structure needs to be destroyed by
2223 * ast_party_name_free() when it is no longer needed.
2224 */
2225 static void sig_pri_party_name_convert(struct ast_party_name *ast_name, const struct pri_party_name *pri_name)
2226 {
2227 ast_name->str = ast_strdup(pri_name->str);
2228 ast_name->char_set = pri_to_ast_char_set(pri_name->char_set);
2229 ast_name->presentation = pri_to_ast_presentation(pri_name->presentation);
2230 ast_name->valid = 1;
2231 }
2232
2233 /*!
2234 * \internal
2235 * \brief Convert libpri party number into asterisk party number.
2236 * \since 1.8
2237 *
2238 * \param ast_number Asterisk party number structure to fill. Must already be set initialized.
2239 * \param pri_number libpri party number structure containing source information.
2240 * \param pri PRI span control structure.
2241 *
2242 * \note The filled in ast_number structure needs to be destroyed by
2243 * ast_party_number_free() when it is no longer needed.
2244 */
2245 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)
2246 {
2247 char number[AST_MAX_EXTENSION * 2];
2248
2249 apply_plan_to_existing_number(number, sizeof(number), pri, pri_number->str,
2250 pri_number->plan);
2251 ast_number->str = ast_strdup(number);
2252 ast_number->plan = pri_number->plan;
2253 ast_number->presentation = pri_to_ast_presentation(pri_number->presentation);
2254 ast_number->valid = 1;
2255 }
2256
2257 /*!
2258 * \internal
2259 * \brief Convert libpri party id into asterisk party id.
2260 * \since 1.8
2261 *
2262 * \param ast_id Asterisk party id structure to fill. Must already be set initialized.
2263 * \param pri_id libpri party id structure containing source information.
2264 * \param pri PRI span control structure.
2265 *
2266 * \note The filled in ast_id structure needs to be destroyed by
2267 * ast_party_id_free() when it is no longer needed.
2268 */
2269 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)
2270 {
2271 if (pri_id->name.valid) {
2272 sig_pri_party_name_convert(&ast_id->name, &pri_id->name);
2273 }
2274 if (pri_id->number.valid) {
2275 sig_pri_party_number_convert(&ast_id->number, &pri_id->number, pri);
2276 }
2277 #if defined(HAVE_PRI_SUBADDR)
2278 if (pri_id->subaddress.valid) {
2279 sig_pri_set_subaddress(&ast_id->subaddress, &pri_id->subaddress);
2280 }
2281 #endif /* defined(HAVE_PRI_SUBADDR) */
2282 }
2283
2284 /*!
2285 * \internal
2286 * \brief Convert libpri redirecting information into asterisk redirecting information.
2287 * \since 1.8
2288 *
2289 * \param ast_redirecting Asterisk redirecting structure to fill.
2290 * \param pri_redirecting libpri redirecting structure containing source information.
2291 * \param ast_guide Asterisk redirecting structure to use as an initialization guide.
2292 * \param pri PRI span control structure.
2293 *
2294 * \note The filled in ast_redirecting structure needs to be destroyed by
2295 * ast_party_redirecting_free() when it is no longer needed.
2296 */
2297 static void sig_pri_redirecting_convert(struct ast_party_redirecting *ast_redirecting,
2298 const struct pri_party_redirecting *pri_redirecting,
2299 const struct ast_party_redirecting *ast_guide,
2300 struct sig_pri_span *pri)
2301 {
2302 ast_party_redirecting_set_init(ast_redirecting, ast_guide);
2303
2304 sig_pri_party_id_convert(&ast_redirecting->orig, &pri_redirecting->orig_called, pri);
2305 sig_pri_party_id_convert(&ast_redirecting->from, &pri_redirecting->from, pri);
2306 sig_pri_party_id_convert(&ast_redirecting->to, &pri_redirecting->to, pri);
2307 ast_redirecting->count = pri_redirecting->count;
2308 ast_redirecting->reason.code = pri_to_ast_reason(pri_redirecting->reason);
2309 ast_redirecting->orig_reason.code = pri_to_ast_reason(pri_redirecting->orig_reason);
2310 }
2311
2312 /*!
2313 * \internal
2314 * \brief Determine if the given extension matches one of the MSNs in the pattern list.
2315 * \since 1.8
2316 *
2317 * \param msn_patterns Comma separated list of MSN patterns to match.
2318 * \param exten Extension to match in the MSN list.
2319 *
2320 * \retval 1 if matches.
2321 * \retval 0 if no match.
2322 */
2323 static int sig_pri_msn_match(const char *msn_patterns, const char *exten)
2324 {
2325 char *pattern;
2326 char *msn_list;
2327 char *list_tail;
2328
2329 msn_list = ast_strdupa(msn_patterns);
2330
2331 list_tail = NULL;
2332 pattern = strtok_r(msn_list, ",", &list_tail);
2333 while (pattern) {
2334 pattern = ast_strip(pattern);
2335 if (!ast_strlen_zero(pattern) && ast_extension_match(pattern, exten)) {
2336 /* Extension matched the pattern. */
2337 return 1;
2338 }
2339 pattern = strtok_r(NULL, ",", &list_tail);
2340 }
2341 /* Did not match any pattern in the list. */
2342 return 0;
2343 }
2344
2345 #if defined(HAVE_PRI_MCID)
2346 static void party_number_json_to_ami(struct ast_str **msg, const char *prefix, struct ast_json *number)
2347 {
2348 const char *num_txt, *pres_txt;
2349 int plan, pres;
2350 if (!number) {
2351 ast_str_append(msg, 0,
2352 "%sNumValid: 0\r\n"
2353 "%sNum: \r\n"
2354 "%ston: 0\r\n",
2355 prefix, prefix, prefix);
2356 return;
2357 }
2358
2359 num_txt = ast_json_string_get(ast_json_object_get(number, "number"));
2360 plan = ast_json_integer_get(ast_json_object_get(number, "plan"));
2361 pres = ast_json_integer_get(ast_json_object_get(number, "presentation"));
2362 pres_txt = ast_json_string_get(ast_json_object_get(number, "presentation_txt"));
2363
2364 ast_str_append(msg, 0, "%sNumValid: 1\r\n", prefix);
2365 ast_str_append(msg, 0, "%sNum: %s\r\n", prefix, num_txt);
2366 ast_str_append(msg, 0, "%ston: %d\r\n", prefix, plan);
2367 ast_str_append(msg, 0, "%sNumPlan: %d\r\n", prefix, plan);
2368 ast_str_append(msg, 0, "%sNumPres: %d (%s)\r\n", prefix, pres, pres_txt);
2369 }
2370
2371 static void party_name_json_to_ami(struct ast_str **msg, const char *prefix, struct ast_json *name)
2372 {
2373 const char *name_txt, *pres_txt, *charset;
2374 int pres;
2375 if (!name) {
2376 ast_str_append(msg, 0,
2377 "%sNameValid: 0\r\n"
2378 "%sName: \r\n",
2379 prefix, prefix);
2380 return;
2381 }
2382
2383 name_txt = ast_json_string_get(ast_json_object_get(name, "name"));
2384 charset = ast_json_string_get(ast_json_object_get(name, "character_set"));
2385 pres = ast_json_integer_get(ast_json_object_get(name, "presentation"));
2386 pres_txt = ast_json_string_get(ast_json_object_get(name, "presentation_txt"));
2387
2388 ast_str_append(msg, 0, "%sNameValid: 1\r\n", prefix);
2389 ast_str_append(msg, 0, "%sName: %s\r\n", prefix, name_txt);
2390 ast_str_append(msg, 0, "%sNameCharSet: %s\r\n", prefix, charset);
2391 ast_str_append(msg, 0, "%sNamePres: %d (%s)\r\n", prefix, pres, pres_txt);
2392 }
2393
2394 static void party_subaddress_json_to_ami(struct ast_str **msg, const char *prefix, struct ast_json *subaddress)
2395 {
2396 const char *subaddress_txt, *type_txt;
2397 int odd;
2398 if (!subaddress) {
2399 return;
2400 }
2401
2402 subaddress_txt = ast_json_string_get(ast_json_object_get(subaddress, "subaddress"));
2403 type_txt = ast_json_string_get(ast_json_object_get(subaddress, "type"));
2404 odd = ast_json_is_true(ast_json_object_get(subaddress, "odd")) ? 1 : 0;
2405
2406 ast_str_append(msg, 0, "%sSubaddr: %s\r\n", prefix, subaddress_txt);
2407 ast_str_append(msg, 0, "%sSubaddrType: %s\r\n", prefix, type_txt);
2408 ast_str_append(msg, 0, "%sSubaddrOdd: %d\r\n", prefix, odd);
2409 }
2410
2411 /*!
2412 * \internal
2413 * \brief Append the given JSON party id to the event string.
2414 * \since 1.8
2415 *
2416 * \param msg Event message string being built.
2417 * \param prefix Prefix to add to the party id lines.
2418 * \param party Party information to encode.
2419 */
2420 static void party_json_to_ami(struct ast_str **msg, const char *prefix, struct ast_json *party)
2421 {
2422 struct ast_json *presentation = ast_json_object_get(party, "presentation");
2423 struct ast_json *presentation_txt = ast_json_object_get(party, "presentation_txt");
2424 struct ast_json *name = ast_json_object_get(party, "name");
2425 struct ast_json *number = ast_json_object_get(party, "number");
2426 struct ast_json *subaddress = ast_json_object_get(party, "subaddress");
2427
2428 /* Combined party presentation */
2429 ast_str_append(msg, 0, "%sPres: %jd (%s)\r\n", prefix,
2430 ast_json_integer_get(presentation),
2431 ast_json_string_get(presentation_txt));
2432
2433 /* Party number */
2434 party_number_json_to_ami(msg, prefix, number);
2435
2436 /* Party name */
2437 party_name_json_to_ami(msg, prefix, name);
2438
2439 /* Party subaddress */
2440 party_subaddress_json_to_ami(msg, prefix, subaddress);
2441 }
2442
2443 static struct ast_manager_event_blob *mcid_to_ami(struct stasis_message *msg)
2444 {
2445 RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
2446 RAII_VAR(struct ast_str *, party_string, ast_str_create(256), ast_free);
2447 struct ast_channel_blob *obj = stasis_message_data(msg);
2448
2449 if (obj->snapshot) {
2450 channel_string = ast_manager_build_channel_state_string(obj->snapshot);
2451 if (!channel_string) {
2452 return NULL;
2453 }
2454 }
2455
2456 party_json_to_ami(&party_string, "MCallerID", ast_json_object_get(obj->blob, "caller"));
2457 party_json_to_ami(&party_string, "MConnectedID", ast_json_object_get(obj->blob, "connected"));
2458
2459 return ast_manager_event_blob_create(EVENT_FLAG_CALL, "MCID",
2460 "%s"
2461 "%s",
2462 S_COR(obj->snapshot, ast_str_buffer(channel_string), ""), ast_str_buffer(party_string));
2463 }
2464
2465 STASIS_MESSAGE_TYPE_DEFN_LOCAL(mcid_type,
2466 .to_ami = mcid_to_ami,
2467 );
2468
2469 static void send_mcid(struct ast_channel *chan, struct ast_party_id *caller, struct ast_party_id *connected)
2470 {
2471 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2472
2473 ast_assert(caller != NULL);
2474 ast_assert(connected != NULL);
2475
2476 blob = ast_json_pack("{s: o, s: o}",
2477 "caller", ast_json_party_id(caller),
2478 "connected", ast_json_party_id(connected));
2479 if (!blob) {
2480 return;
2481 }
2482
2483 ast_channel_publish_blob(chan, mcid_type(), blob);
2484 }
2485
2486 /*!
2487 * \internal
2488 * \brief Handle the MCID event.
2489 * \since 1.8
2490 *
2491 * \param pri PRI span control structure.
2492 * \param mcid MCID event parameters.
2493 * \param owner Asterisk channel associated with the call.
2494 * NULL if Asterisk no longer has the ast_channel struct.
2495 *
2496 * \note Assumes the pri->lock is already obtained.
2497 * \note Assumes the owner channel lock is already obtained if still present.
2498 */
2499 static void sig_pri_mcid_event(struct sig_pri_span *pri, const struct pri_subcmd_mcid_req *mcid, struct ast_channel *owner)
2500 {
2501 struct ast_party_id caller_party;
2502 struct ast_party_id connected_party;
2503
2504 /* Always use libpri's called party information. */
2505 ast_party_id_init(&connected_party);
2506 sig_pri_party_id_convert(&connected_party, &mcid->answerer, pri);
2507 if (owner) {
2508 /*
2509 * The owner channel is present.
2510 * Pass the event to the peer as well.
2511 */
2512 ast_queue_control(owner, AST_CONTROL_MCID);
2513
2514 send_mcid(owner, &ast_channel_connected(owner)->id, &connected_party);
2515 } else {
2516 /*
2517 * Since we no longer have an owner channel,
2518 * we have to use the caller information supplied by libpri.
2519 */
2520 ast_party_id_init(&caller_party);
2521 sig_pri_party_id_convert(&caller_party, &mcid->originator, pri);
2522 send_mcid(owner, &caller_party, &connected_party);
2523 ast_party_id_free(&caller_party);
2524 }
2525 ast_party_id_free(&connected_party);
2526 }
2527 #endif /* defined(HAVE_PRI_MCID) */
2528
2529 #if defined(HAVE_PRI_TRANSFER)
2530 struct xfer_rsp_data {
2531 struct sig_pri_span *pri;
2532 /*! Call to send transfer success/fail response over. */
2533 q931_call *call;
2534 /*! Invocation ID to use when sending a reply to the transfer request. */
2535 int invoke_id;
2536 /*! TRUE if the transfer response has been made. */
2537 int responded;
2538 };
2539 #endif /* defined(HAVE_PRI_TRANSFER) */
2540
2541 #if defined(HAVE_PRI_TRANSFER)
2542 /*!
2543 * \internal
2544 * \brief Send the transfer success/fail response message.
2545 * \since 1.8
2546 *
2547 * \param rsp Transfer response data.
2548 * \param is_successful TRUE if the transfer was successful.
2549 *
2550 * \note Assumes the rsp->pri->lock is already obtained.
2551 */
2552 static void sig_pri_transfer_rsp(struct xfer_rsp_data *rsp, int is_successful)
2553 {
2554 if (rsp->responded) {
2555 return;
2556 }
2557 rsp->responded = 1;
2558
2559 pri_transfer_rsp(rsp->pri->pri, rsp->call, rsp->invoke_id, is_successful);
2560 }
2561 #endif /* defined(HAVE_PRI_TRANSFER) */
2562
2563 #if defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER)
2564 /*!
2565 * \internal
2566 * \brief Attempt to transfer the two calls to each other.
2567 * \since 1.8
2568 *
2569 * \param pri PRI span control structure.
2570 * \param call_1_pri First call involved in the transfer. (transferee; usually on hold)
2571 * \param call_1_held TRUE if call_1_pri is on hold.
2572 * \param call_2_pri Second call involved in the transfer. (target; usually active/ringing)
2573 * \param call_2_held TRUE if call_2_pri is on hold.
2574 * \param xfer_data Transfer response data if non-NULL.
2575 *
2576 * \note Assumes the pri->lock is already obtained.
2577 *
2578 * \retval 0 on success.
2579 * \retval -1 on error.
2580 */
2581 static int sig_pri_attempt_transfer(struct sig_pri_span *pri, q931_call *call_1_pri, int call_1_held, q931_call *call_2_pri, int call_2_held, struct xfer_rsp_data *xfer_data)
2582 {
2583 struct attempt_xfer_call {
2584 q931_call *pri;
2585 struct ast_channel *ast;
2586 int held;
2587 int chanpos;
2588 };
2589 int retval;
2590 enum ast_transfer_result xfer_res;
2591 struct attempt_xfer_call *call_1;
2592 struct attempt_xfer_call *call_2;
2593 struct attempt_xfer_call c1;
2594 struct attempt_xfer_call c2;
2595
2596 c1.pri = call_1_pri;
2597 c1.held = call_1_held;
2598 call_1 = &c1;
2599
2600 c2.pri = call_2_pri;
2601 c2.held = call_2_held;
2602 call_2 = &c2;
2603
2604 call_1->chanpos = pri_find_principle_by_call(pri, call_1->pri);
2605 call_2->chanpos = pri_find_principle_by_call(pri, call_2->pri);
2606 if (call_1->chanpos < 0 || call_2->chanpos < 0) {
2607 /* Calls not found in span control. */
2608 #if defined(HAVE_PRI_TRANSFER)
2609 if (xfer_data) {
2610 /* Transfer failed. */
2611 sig_pri_transfer_rsp(xfer_data, 0);
2612 }
2613 #endif /* defined(HAVE_PRI_TRANSFER) */
2614 return -1;
2615 }
2616
2617 /* Get call_1 owner. */
2618 sig_pri_lock_private(pri->pvts[call_1->chanpos]);
2619 sig_pri_lock_owner(pri, call_1->chanpos);
2620 call_1->ast = pri->pvts[call_1->chanpos]->owner;
2621 if (call_1->ast) {
2622 ast_channel_ref(call_1->ast);
2623 ast_channel_unlock(call_1->ast);
2624 }
2625 sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
2626
2627 /* Get call_2 owner. */
2628 sig_pri_lock_private(pri->pvts[call_2->chanpos]);
2629 sig_pri_lock_owner(pri, call_2->chanpos);
2630 call_2->ast = pri->pvts[call_2->chanpos]->owner;
2631 if (call_2->ast) {
2632 ast_channel_ref(call_2->ast);
2633 ast_channel_unlock(call_2->ast);
2634 }
2635 sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
2636
2637 if (!call_1->ast || !call_2->ast) {
2638 /* At least one owner is not present. */
2639 if (call_1->ast) {
2640 ast_channel_unref(call_1->ast);
2641 }
2642 if (call_2->ast) {
2643 ast_channel_unref(call_2->ast);
2644 }
2645 #if defined(HAVE_PRI_TRANSFER)
2646 if (xfer_data) {
2647 /* Transfer failed. */
2648 sig_pri_transfer_rsp(xfer_data, 0);
2649 }
2650 #endif /* defined(HAVE_PRI_TRANSFER) */
2651 return -1;
2652 }
2653
2654 ast_verb(3, "TRANSFERRING %s to %s\n",
2655 ast_channel_name(call_1->ast), ast_channel_name(call_2->ast));
2656
2657 #if defined(HAVE_PRI_TRANSFER)
2658 if (xfer_data) {
2659 /*
2660 * Add traps on the transferer channels in case threading causes
2661 * them to hangup before ast_bridge_transfer_attended() returns
2662 * and we can get the pri->lock back.
2663 */
2664 sig_pri_lock_private(pri->pvts[call_1->chanpos]);
2665 pri->pvts[call_1->chanpos]->xfer_data = xfer_data;
2666 sig_pri_unlock_private(pri->pvts[call_1->chanpos]);
2667 sig_pri_lock_private(pri->pvts[call_2->chanpos]);
2668 pri->pvts[call_2->chanpos]->xfer_data = xfer_data;
2669 sig_pri_unlock_private(pri->pvts[call_2->chanpos]);
2670 }
2671 #endif /* defined(HAVE_PRI_TRANSFER) */
2672
2673 ast_mutex_unlock(&pri->lock);
2674 xfer_res = ast_bridge_transfer_attended(call_1->ast, call_2->ast);
2675 ast_mutex_lock(&pri->lock);
2676 retval = (xfer_res != AST_BRIDGE_TRANSFER_SUCCESS) ? -1 : 0;
2677
2678 #if defined(HAVE_PRI_TRANSFER)
2679 if (xfer_data) {
2680 int rsp_chanpos;
2681
2682 /*
2683 * Remove the transferrer channel traps.
2684 *
2685 * We must refind chanpos because we released pri->lock.
2686 */
2687 rsp_chanpos = pri_find_principle_by_call(pri, call_1->pri);
2688 if (0 <= rsp_chanpos) {
2689 sig_pri_lock_private(pri->pvts[rsp_chanpos]);
2690 pri->pvts[rsp_chanpos]->xfer_data = NULL;
2691 sig_pri_unlock_private(pri->pvts[rsp_chanpos]);
2692 }
2693 rsp_chanpos = pri_find_principle_by_call(pri, call_2->pri);
2694 if (0 <= rsp_chanpos) {
2695 sig_pri_lock_private(pri->pvts[rsp_chanpos]);
2696 pri->pvts[rsp_chanpos]->xfer_data = NULL;
2697 sig_pri_unlock_private(pri->pvts[rsp_chanpos]);
2698 }
2699
2700 /* Report transfer status. */
2701 sig_pri_transfer_rsp(xfer_data, retval ? 0 : 1);
2702 }
2703 #endif /* defined(HAVE_PRI_TRANSFER) */
2704 ast_channel_unref(call_1->ast);
2705 ast_channel_unref(call_2->ast);
2706 return retval;
2707 }
2708 #endif /* defined(HAVE_PRI_CALL_HOLD) || defined(HAVE_PRI_TRANSFER) */
2709
2710 #if defined(HAVE_PRI_CCSS)
2711 /*!
2712 * \internal
2713 * \brief Compare the CC agent private data by libpri cc_id.
2714 * \since 1.8
2715 *
2716 * \param obj pointer to the (user-defined part) of an object.
2717 * \param arg callback argument from ao2_callback()
2718 * \param flags flags from ao2_callback()
2719 *
2720 * \return values are a combination of enum _cb_results.
2721 */
2722 static int sig_pri_cc_agent_cmp_cc_id(void *obj, void *arg, int flags)
2723 {
2724 struct ast_cc_agent *agent_1 = obj;
2725 struct sig_pri_cc_agent_prv *agent_prv_1 = agent_1->private_data;
2726 struct sig_pri_cc_agent_prv *agent_prv_2 = arg;
2727
2728 return (agent_prv_1 && agent_prv_1->pri == agent_prv_2->pri
2729 && agent_prv_1->cc_id == agent_prv_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
2730 }
2731 #endif /* defined(HAVE_PRI_CCSS) */
2732
2733 #if defined(HAVE_PRI_CCSS)
2734 /*!
2735 * \internal
2736 * \brief Find the CC agent by libpri cc_id.
2737 * \since 1.8
2738 *
2739 * \param pri PRI span control structure.
2740 * \param cc_id CC record ID to find.
2741 *
2742 * \note
2743 * Since agents are refcounted, and this function returns
2744 * a reference to the agent, it is imperative that you decrement
2745 * the refcount of the agent once you have finished using it.
2746 *
2747 * \retval agent on success.
2748 * \retval NULL not found.
2749 */
2750 static struct ast_cc_agent *sig_pri_find_cc_agent_by_cc_id(struct sig_pri_span *pri, long cc_id)
2751 {
2752 struct sig_pri_cc_agent_prv finder = {
2753 .pri = pri,
2754 .cc_id = cc_id,
2755 };
2756
2757 return ast_cc_agent_callback(0, sig_pri_cc_agent_cmp_cc_id, &finder,
2758 sig_pri_cc_type_name);
2759 }
2760 #endif /* defined(HAVE_PRI_CCSS) */
2761
2762 #if defined(HAVE_PRI_CCSS)
2763 /*!
2764 * \internal
2765 * \brief Compare the CC monitor instance by libpri cc_id.
2766 * \since 1.8
2767 *
2768 * \param obj pointer to the (user-defined part) of an object.
2769 * \param arg callback argument from ao2_callback()
2770 * \param flags flags from ao2_callback()
2771 *
2772 * \return values are a combination of enum _cb_results.
2773 */
2774 static int sig_pri_cc_monitor_cmp_cc_id(void *obj, void *arg, int flags)
2775 {
2776 struct sig_pri_cc_monitor_instance *monitor_1 = obj;
2777 struct sig_pri_cc_monitor_instance *monitor_2 = arg;
2778
2779 return (monitor_1->pri == monitor_2->pri
2780 && monitor_1->cc_id == monitor_2->cc_id) ? CMP_MATCH | CMP_STOP : 0;
2781 }
2782 #endif /* defined(HAVE_PRI_CCSS) */
2783
2784 #if defined(HAVE_PRI_CCSS)
2785 /*!
2786 * \internal
2787 * \brief Find the CC monitor instance by libpri cc_id.
2788 * \since 1.8
2789 *
2790 * \param pri PRI span control structure.
2791 * \param cc_id CC record ID to find.
2792 *
2793 * \note
2794 * Since monitor_instances are refcounted, and this function returns
2795 * a reference to the instance, it is imperative that you decrement
2796 * the refcount of the instance once you have finished using it.
2797 *
2798 * \retval monitor_instance on success.
2799 * \retval NULL not found.
2800 */
2801 static struct sig_pri_cc_monitor_instance *sig_pri_find_cc_monitor_by_cc_id(struct sig_pri_span *pri, long cc_id)
2802 {
2803 struct sig_pri_cc_monitor_instance finder = {
2804 .pri = pri,
2805 .cc_id = cc_id,
2806 };
2807
2808 return ao2_callback(sig_pri_cc_monitors, 0, sig_pri_cc_monitor_cmp_cc_id, &finder);
2809 }
2810 #endif /* defined(HAVE_PRI_CCSS) */
2811
2812 #if defined(HAVE_PRI_CCSS)
2813 /*!
2814 * \internal
2815 * \brief Destroy the given monitor instance.
2816 * \since 1.8
2817 *
2818 * \param data Monitor instance to destroy.
2819 */
2820 static void sig_pri_cc_monitor_instance_destroy(void *data)
2821 {
2822 struct sig_pri_cc_monitor_instance *monitor_instance = data;
2823
2824 if (monitor_instance->cc_id != -1) {
2825 ast_mutex_lock(&monitor_instance->pri->lock);
2826 pri_cc_cancel(monitor_instance->pri->pri, monitor_instance->cc_id);
2827 ast_mutex_unlock(&monitor_instance->pri->lock);
2828 }
2829 sig_pri_callbacks.module_unref();
2830 }
2831 #endif /* defined(HAVE_PRI_CCSS) */
2832
2833 #if defined(HAVE_PRI_CCSS)
2834 /*!
2835 * \internal
2836 * \brief Construct a new monitor instance.
2837 * \since 1.8
2838 *
2839 * \param core_id CC core ID.
2840 * \param pri PRI span control structure.
2841 * \param cc_id CC record ID.
2842 * \param device_name Name of device (Asterisk channel name less sequence number).
2843 *
2844 * \note
2845 * Since monitor_instances are refcounted, and this function returns
2846 * a reference to the instance, it is imperative that you decrement
2847 * the refcount of the instance once you have finished using it.
2848 *
2849 * \retval monitor_instance on success.
2850 * \retval NULL on error.
2851 */
2852 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)
2853 {
2854 struct sig_pri_cc_monitor_instance *monitor_instance;
2855
2856 if (!sig_pri_callbacks.module_ref || !sig_pri_callbacks.module_unref) {
2857 return NULL;
2858 }
2859
2860 monitor_instance = ao2_alloc(sizeof(*monitor_instance) + strlen(device_name),
2861 sig_pri_cc_monitor_instance_destroy);
2862 if (!monitor_instance) {
2863 return NULL;
2864 }
2865
2866 monitor_instance->cc_id = cc_id;
2867 monitor_instance->pri = pri;
2868 monitor_instance->core_id = core_id;
2869 strcpy(monitor_instance->name, device_name);
2870
2871 sig_pri_callbacks.module_ref();
2872
2873 ao2_link(sig_pri_cc_monitors, monitor_instance);
2874 return monitor_instance;
2875 }
2876 #endif /* defined(HAVE_PRI_CCSS) */
2877
2878 #if defined(HAVE_PRI_CCSS)
2879 /*!
2880 * \internal
2881 * \brief Announce to the CC core that protocol CC monitor is available for this call.
2882 * \since 1.8
2883 *
2884 * \param pri PRI span control structure.
2885 * \param chanpos Channel position in the span.
2886 * \param cc_id CC record ID.
2887 * \param service CCBS/CCNR indication.
2888 *
2889 * \note Assumes the pri->lock is already obtained.
2890 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
2891 * \note Assumes the sig_pri_lock_owner(pri, chanpos) is already obtained.
2892 *
2893 * \retval 0 on success.
2894 * \retval -1 on error.
2895 */
2896 static int sig_pri_cc_available(struct sig_pri_span *pri, int chanpos, long cc_id, enum ast_cc_service_type service)
2897 {
2898 struct sig_pri_chan *pvt;
2899 struct ast_cc_config_params *cc_params;
2900 struct sig_pri_cc_monitor_instance *monitor;
2901 enum ast_cc_monitor_policies monitor_policy;
2902 int core_id;
2903 int res;
2904 char device_name[AST_CHANNEL_NAME];
2905 char dialstring[AST_CHANNEL_NAME];
2906
2907 pvt = pri->pvts[chanpos];
2908
2909 core_id = ast_cc_get_current_core_id(pvt->owner);
2910 if (core_id == -1) {
2911 return -1;
2912 }
2913
2914 cc_params = ast_channel_get_cc_config_params(pvt->owner);
2915 if (!cc_params) {
2916 return -1;
2917 }
2918
2919 res = -1;
2920 monitor_policy = ast_get_cc_monitor_policy(cc_params);
2921 switch (monitor_policy) {
2922 case AST_CC_MONITOR_NEVER:
2923 /* CCSS is not enabled. */
2924 break;
2925 case AST_CC_MONITOR_NATIVE:
2926 case AST_CC_MONITOR_ALWAYS:
2927 /*
2928 * If it is AST_CC_MONITOR_ALWAYS and native fails we will attempt the fallback
2929 * later in the call to sig_pri_cc_generic_check().
2930 */
2931 ast_channel_get_device_name(pvt->owner, device_name, sizeof(device_name));
2932 sig_pri_make_cc_dialstring(pvt, dialstring, sizeof(dialstring));
2933 monitor = sig_pri_cc_monitor_instance_init(core_id, pri, cc_id, device_name);
2934 if (!monitor) {
2935 break;
2936 }
2937 res = ast_queue_cc_frame(pvt->owner, sig_pri_cc_type_name, dialstring, service,
2938 monitor);
2939 if (res) {
2940 monitor->cc_id = -1;
2941 ao2_unlink(sig_pri_cc_monitors, monitor);
2942 ao2_ref(monitor, -1);
2943 }
2944 break;
2945 case AST_CC_MONITOR_GENERIC:
2946 ast_queue_cc_frame(pvt->owner, AST_CC_GENERIC_MONITOR_TYPE,
2947 sig_pri_get_orig_dialstring(pvt), service, NULL);
2948 /* Say it failed to force caller to cancel native CC. */
2949 break;
2950 }
2951 return res;
2952 }
2953 #endif /* defined(HAVE_PRI_CCSS) */
2954
2955 /*!
2956 * \internal
2957 * \brief Check if generic CC monitor is needed and request it.
2958 * \since 1.8
2959 *
2960 * \param pri PRI span control structure.
2961 * \param chanpos Channel position in the span.
2962 * \param service CCBS/CCNR indication.
2963 *
2964 * \note Assumes the pri->lock is already obtained.
2965 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
2966 */
2967 static void sig_pri_cc_generic_check(struct sig_pri_span *pri, int chanpos, enum ast_cc_service_type service)
2968 {
2969 struct ast_channel *owner;
2970 struct ast_cc_config_params *cc_params;
2971 #if defined(HAVE_PRI_CCSS)
2972 struct ast_cc_monitor *monitor;
2973 char device_name[AST_CHANNEL_NAME];
2974 #endif /* defined(HAVE_PRI_CCSS) */
2975 enum ast_cc_monitor_policies monitor_policy;
2976 int core_id;
2977
2978 if (!pri->pvts[chanpos]->outgoing) {
2979 /* This is not an outgoing call so it cannot be CC monitor. */
2980 return;
2981 }
2982
2983 sig_pri_lock_owner(pri, chanpos);
2984 owner = pri->pvts[chanpos]->owner;
2985 if (!owner) {
2986 return;
2987 }
2988 core_id = ast_cc_get_current_core_id(owner);
2989 if (core_id == -1) {
2990 /* No CC core setup */
2991 goto done;
2992 }
2993
2994 cc_params = ast_channel_get_cc_config_params(owner);
2995 if (!cc_params) {
2996 /* Could not get CC config parameters. */
2997 goto done;
2998 }
2999
3000 #if defined(HAVE_PRI_CCSS)
3001 ast_channel_get_device_name(owner, device_name, sizeof(device_name));
3002 monitor = ast_cc_get_monitor_by_recall_core_id(core_id, device_name);
3003 if (monitor) {
3004 /* CC monitor is already present so no need for generic CC. */
3005 ao2_ref(monitor, -1);
3006 goto done;
3007 }
3008 #endif /* defined(HAVE_PRI_CCSS) */
3009
3010 monitor_policy = ast_get_cc_monitor_policy(cc_params);
3011 switch (monitor_policy) {
3012 case AST_CC_MONITOR_NEVER:
3013 /* CCSS is not enabled. */
3014 break;
3015 case AST_CC_MONITOR_NATIVE:
3016 if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
3017 /* Request generic CC monitor. */
3018 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
3019 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
3020 }
3021 break;
3022 case AST_CC_MONITOR_ALWAYS:
3023 if (pri->sig == SIG_BRI_PTMP && pri->nodetype != PRI_NETWORK) {
3024 /*
3025 * Cannot monitor PTMP TE side since this is not defined.
3026 * We are playing the roll of a phone in this case and
3027 * a phone cannot monitor a party over the network without
3028 * protocol help.
3029 */
3030 break;
3031 }
3032 /*
3033 * We are either falling back or this is a PTMP NT span.
3034 * Request generic CC monitor.
3035 */
3036 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
3037 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
3038 break;
3039 case AST_CC_MONITOR_GENERIC:
3040 if (pri->sig == SIG_BRI_PTMP && pri->nodetype == PRI_NETWORK) {
3041 /* Request generic CC monitor. */
3042 ast_queue_cc_frame(owner, AST_CC_GENERIC_MONITOR_TYPE,
3043 sig_pri_get_orig_dialstring(pri->pvts[chanpos]), service, NULL);
3044 }
3045 break;
3046 }
3047
3048 done:
3049 ast_channel_unlock(owner);
3050 }
3051
3052 #if defined(HAVE_PRI_CCSS)
3053 /*!
3054 * \internal
3055 * \brief The CC link canceled the CC instance.
3056 * \since 1.8
3057 *
3058 * \param pri PRI span control structure.
3059 * \param cc_id CC record ID.
3060 * \param is_agent TRUE if the cc_id is for an agent.
3061 */
3062 static void sig_pri_cc_link_canceled(struct sig_pri_span *pri, long cc_id, int is_agent)
3063 {
3064 if (is_agent) {
3065 struct ast_cc_agent *agent;
3066
3067 agent = sig_pri_find_cc_agent_by_cc_id(pri, cc_id);
3068 if (!agent) {
3069 return;
3070 }
3071 ast_cc_failed(agent->core_id, "%s agent got canceled by link",
3072 sig_pri_cc_type_name);
3073 ao2_ref(agent, -1);
3074 } else {
3075 struct sig_pri_cc_monitor_instance *monitor;
3076
3077 monitor = sig_pri_find_cc_monitor_by_cc_id(pri, cc_id);
3078 if (!monitor) {
3079 return;
3080 }
3081 monitor->cc_id = -1;
3082 ast_cc_monitor_failed(monitor->core_id, monitor->name,
3083 "%s monitor got canceled by link", sig_pri_cc_type_name);
3084 ao2_ref(monitor, -1);
3085 }
3086 }
3087 #endif /* defined(HAVE_PRI_CCSS) */
3088
3089 #if defined(HAVE_PRI_AOC_EVENTS)
3090 /*!
3091 * \internal
3092 * \brief Convert ast_aoc_charged_item to PRI_AOC_CHARGED_ITEM .
3093 * \since 1.8
3094 *
3095 * \param value Value to convert to string.
3096 *
3097 * \return PRI_AOC_CHARGED_ITEM
3098 */
3099 static enum PRI_AOC_CHARGED_ITEM sig_pri_aoc_charged_item_to_pri(enum PRI_AOC_CHARGED_ITEM value)
3100 {
3101 switch (value) {
3102 case AST_AOC_CHARGED_ITEM_NA:
3103 return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
3104 case AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
3105 return PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
3106 case AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
3107 return PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
3108 case AST_AOC_CHARGED_ITEM_CALL_ATTEMPT:
3109 return PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT;
3110 case AST_AOC_CHARGED_ITEM_CALL_SETUP:
3111 return PRI_AOC_CHARGED_ITEM_CALL_SETUP;
3112 case AST_AOC_CHARGED_ITEM_USER_USER_INFO:
3113 return PRI_AOC_CHARGED_ITEM_USER_USER_INFO;
3114 case AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
3115 return PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
3116 }
3117 return PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE;
3118 }
3119 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3120
3121 #if defined(HAVE_PRI_AOC_EVENTS)
3122 /*!
3123 * \internal
3124 * \brief Convert PRI_AOC_CHARGED_ITEM to ast_aoc_charged_item.
3125 * \since 1.8
3126 *
3127 * \param value Value to convert to string.
3128 *
3129 * \return ast_aoc_charged_item
3130 */
3131 static enum ast_aoc_s_charged_item sig_pri_aoc_charged_item_to_ast(enum PRI_AOC_CHARGED_ITEM value)
3132 {
3133 switch (value) {
3134 case PRI_AOC_CHARGED_ITEM_NOT_AVAILABLE:
3135 return AST_AOC_CHARGED_ITEM_NA;
3136 case PRI_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT:
3137 return AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT;
3138 case PRI_AOC_CHARGED_ITEM_BASIC_COMMUNICATION:
3139 return AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION;
3140 case PRI_AOC_CHARGED_ITEM_CALL_ATTEMPT:
3141 return AST_AOC_CHARGED_ITEM_CALL_ATTEMPT;
3142 case PRI_AOC_CHARGED_ITEM_CALL_SETUP:
3143 return AST_AOC_CHARGED_ITEM_CALL_SETUP;
3144 case PRI_AOC_CHARGED_ITEM_USER_USER_INFO:
3145 return AST_AOC_CHARGED_ITEM_USER_USER_INFO;
3146 case PRI_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE:
3147 return AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE;
3148 }
3149 return AST_AOC_CHARGED_ITEM_NA;
3150 }
3151 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3152
3153 #if defined(HAVE_PRI_AOC_EVENTS)
3154 /*!
3155 * \internal
3156 * \brief Convert AST_AOC_MULTIPLER to PRI_AOC_MULTIPLIER.
3157 * \since 1.8
3158 *
3159 * \return pri enum equivalent.
3160 */
3161 static int sig_pri_aoc_multiplier_from_ast(enum ast_aoc_currency_multiplier mult)
3162 {
3163 switch (mult) {
3164 case AST_AOC_MULT_ONETHOUSANDTH:
3165 return PRI_AOC_MULTIPLIER_THOUSANDTH;
3166 case AST_AOC_MULT_ONEHUNDREDTH:
3167 return PRI_AOC_MULTIPLIER_HUNDREDTH;
3168 case AST_AOC_MULT_ONETENTH:
3169 return PRI_AOC_MULTIPLIER_TENTH;
3170 case AST_AOC_MULT_ONE:
3171 return PRI_AOC_MULTIPLIER_ONE;
3172 case AST_AOC_MULT_TEN:
3173 return PRI_AOC_MULTIPLIER_TEN;
3174 case AST_AOC_MULT_HUNDRED:
3175 return PRI_AOC_MULTIPLIER_HUNDRED;
3176 case AST_AOC_MULT_THOUSAND:
3177 return PRI_AOC_MULTIPLIER_THOUSAND;
3178 default:
3179 return PRI_AOC_MULTIPLIER_ONE;
3180 }
3181 }
3182 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3183
3184 #if defined(HAVE_PRI_AOC_EVENTS)
3185 /*!
3186 * \internal
3187 * \brief Convert PRI_AOC_MULTIPLIER to AST_AOC_MULTIPLIER
3188 * \since 1.8
3189 *
3190 * \return ast enum equivalent.
3191 */
3192 static int sig_pri_aoc_multiplier_from_pri(const int mult)
3193 {
3194 switch (mult) {
3195 case PRI_AOC_MULTIPLIER_THOUSANDTH:
3196 return AST_AOC_MULT_ONETHOUSANDTH;
3197 case PRI_AOC_MULTIPLIER_HUNDREDTH:
3198 return AST_AOC_MULT_ONEHUNDREDTH;
3199 case PRI_AOC_MULTIPLIER_TENTH:
3200 return AST_AOC_MULT_ONETENTH;
3201 case PRI_AOC_MULTIPLIER_ONE:
3202 return AST_AOC_MULT_ONE;
3203 case PRI_AOC_MULTIPLIER_TEN:
3204 return AST_AOC_MULT_TEN;
3205 case PRI_AOC_MULTIPLIER_HUNDRED:
3206 return AST_AOC_MULT_HUNDRED;
3207 case PRI_AOC_MULTIPLIER_THOUSAND:
3208 return AST_AOC_MULT_THOUSAND;
3209 default:
3210 return AST_AOC_MULT_ONE;
3211 }
3212 }
3213 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3214
3215 #if defined(HAVE_PRI_AOC_EVENTS)
3216 /*!
3217 * \internal
3218 * \brief Convert ast_aoc_time_scale representation to PRI_AOC_TIME_SCALE
3219 * \since 1.8
3220 *
3221 * \param value Value to convert to ast representation
3222 *
3223 * \return PRI_AOC_TIME_SCALE
3224 */
3225 static enum PRI_AOC_TIME_SCALE sig_pri_aoc_scale_to_pri(enum ast_aoc_time_scale value)
3226 {
3227 switch (value) {
3228 default:
3229 case AST_AOC_TIME_SCALE_HUNDREDTH_SECOND:
3230 return PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3231 case AST_AOC_TIME_SCALE_TENTH_SECOND:
3232 return PRI_AOC_TIME_SCALE_TENTH_SECOND;
3233 case AST_AOC_TIME_SCALE_SECOND:
3234 return PRI_AOC_TIME_SCALE_SECOND;
3235 case AST_AOC_TIME_SCALE_TEN_SECOND:
3236 return PRI_AOC_TIME_SCALE_TEN_SECOND;
3237 case AST_AOC_TIME_SCALE_MINUTE:
3238 return PRI_AOC_TIME_SCALE_MINUTE;
3239 case AST_AOC_TIME_SCALE_HOUR:
3240 return PRI_AOC_TIME_SCALE_HOUR;
3241 case AST_AOC_TIME_SCALE_DAY:
3242 return PRI_AOC_TIME_SCALE_DAY;
3243 }
3244 }
3245 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3246
3247 #if defined(HAVE_PRI_AOC_EVENTS)
3248 /*!
3249 * \internal
3250 * \brief Convert PRI_AOC_TIME_SCALE to ast aoc representation
3251 * \since 1.8
3252 *
3253 * \param value Value to convert to ast representation
3254 *
3255 * \return ast aoc time scale
3256 */
3257 static enum ast_aoc_time_scale sig_pri_aoc_scale_to_ast(enum PRI_AOC_TIME_SCALE value)
3258 {
3259 switch (value) {
3260 default:
3261 case PRI_AOC_TIME_SCALE_HUNDREDTH_SECOND:
3262 return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3263 case PRI_AOC_TIME_SCALE_TENTH_SECOND:
3264 return AST_AOC_TIME_SCALE_TENTH_SECOND;
3265 case PRI_AOC_TIME_SCALE_SECOND:
3266 return AST_AOC_TIME_SCALE_SECOND;
3267 case PRI_AOC_TIME_SCALE_TEN_SECOND:
3268 return AST_AOC_TIME_SCALE_TEN_SECOND;
3269 case PRI_AOC_TIME_SCALE_MINUTE:
3270 return AST_AOC_TIME_SCALE_MINUTE;
3271 case PRI_AOC_TIME_SCALE_HOUR:
3272 return AST_AOC_TIME_SCALE_HOUR;
3273 case PRI_AOC_TIME_SCALE_DAY:
3274 return AST_AOC_TIME_SCALE_DAY;
3275 }
3276 return AST_AOC_TIME_SCALE_HUNDREDTH_SECOND;
3277 }
3278 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3279
3280 #if defined(HAVE_PRI_AOC_EVENTS)
3281 /*!
3282 * \internal
3283 * \brief Handle AOC-S control frame
3284 * \since 1.8
3285 *
3286 * \param aoc_s AOC-S event parameters.
3287 * \param owner Asterisk channel associated with the call.
3288 * \param passthrough indicating if this message should be queued on the ast channel
3289 *
3290 * \note Assumes the pri->lock is already obtained.
3291 * \note Assumes the sig_pri private is locked
3292 * \note Assumes the owner channel lock is already obtained.
3293 */
3294 static void sig_pri_aoc_s_from_pri(const struct pri_subcmd_aoc_s *aoc_s, struct ast_channel *owner, int passthrough)
3295 {
3296 struct ast_aoc_decoded *decoded = NULL;
3297 struct ast_aoc_encoded *encoded = NULL;
3298 size_t encoded_size = 0;
3299 int idx;
3300
3301 if (!owner || !aoc_s) {
3302 return;
3303 }
3304
3305 if (!(decoded = ast_aoc_create(AST_AOC_S, 0, 0))) {
3306 return;
3307 }
3308
3309 for (idx = 0; idx < aoc_s->num_items; ++idx) {
3310 enum ast_aoc_s_charged_item charged_item;
3311
3312 charged_item = sig_pri_aoc_charged_item_to_ast(aoc_s->item[idx].chargeable);
3313 if (charged_item == AST_AOC_CHARGED_ITEM_NA) {
3314 /* Delete the unknown charged item from the list. */
3315 continue;
3316 }
3317 switch (aoc_s->item[idx].rate_type) {
3318 case PRI_AOC_RATE_TYPE_DURATION:
3319 ast_aoc_s_add_rate_duration(decoded,
3320 charged_item,
3321 aoc_s->item[idx].rate.duration.amount.cost,
3322 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.duration.amount.multiplier),
3323 aoc_s->item[idx].rate.duration.currency,
3324 aoc_s->item[idx].rate.duration.time.length,
3325 sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.time.scale),
3326 aoc_s->item[idx].rate.duration.granularity.length,
3327 sig_pri_aoc_scale_to_ast(aoc_s->item[idx].rate.duration.granularity.scale),
3328 aoc_s->item[idx].rate.duration.charging_type);
3329 break;
3330 case PRI_AOC_RATE_TYPE_FLAT:
3331 ast_aoc_s_add_rate_flat(decoded,
3332 charged_item,
3333 aoc_s->item[idx].rate.flat.amount.cost,
3334 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.flat.amount.multiplier),
3335 aoc_s->item[idx].rate.flat.currency);
3336 break;
3337 case PRI_AOC_RATE_TYPE_VOLUME:
3338 ast_aoc_s_add_rate_volume(decoded,
3339 charged_item,
3340 aoc_s->item[idx].rate.volume.unit,
3341 aoc_s->item[idx].rate.volume.amount.cost,
3342 sig_pri_aoc_multiplier_from_pri(aoc_s->item[idx].rate.volume.amount.multiplier),
3343 aoc_s->item[idx].rate.volume.currency);
3344 break;
3345 case PRI_AOC_RATE_TYPE_SPECIAL_CODE:
3346 ast_aoc_s_add_rate_special_charge_code(decoded,
3347 charged_item,
3348 aoc_s->item[idx].rate.special);
3349 break;
3350 case PRI_AOC_RATE_TYPE_FREE:
3351 ast_aoc_s_add_rate_free(decoded, charged_item, 0);
3352 break;
3353 case PRI_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
3354 ast_aoc_s_add_rate_free(decoded, charged_item, 1);
3355 break;
3356 default:
3357 ast_aoc_s_add_rate_na(decoded, charged_item);
3358 break;
3359 }
3360 }
3361
3362 if (passthrough && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
3363 ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
3364 }
3365
3366 ast_aoc_manager_event(decoded, owner);
3367
3368 ast_aoc_destroy_decoded(decoded);
3369 ast_aoc_destroy_encoded(encoded);
3370 }
3371 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3372
3373 #if defined(HAVE_PRI_AOC_EVENTS)
3374 /*!
3375 * \internal
3376 * \brief Generate AOC Request Response
3377 * \since 1.8
3378 *
3379 * \param aoc_request, pvt, call
3380 *
3381 * \note Assumes the pri->lock is already obtained.
3382 * \note Assumes the sig_pri private is locked
3383 * \note Assumes the owner channel lock is already obtained.
3384 */
3385 static void sig_pri_aoc_request_from_pri(const struct pri_subcmd_aoc_request *aoc_request, struct sig_pri_chan *pvt, q931_call *call)
3386 {
3387 int request;
3388
3389 if (!aoc_request) {
3390 return;
3391 }
3392
3393 request = aoc_request->charging_request;
3394
3395 if (request & PRI_AOC_REQUEST_S) {
3396 if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S) {
3397 /* An AOC-S response must come from the other side, so save off this invoke_id
3398 * and see if an AOC-S message comes in before the call is answered. */
3399 pvt->aoc_s_request_invoke_id = aoc_request->invoke_id;
3400 pvt->aoc_s_request_invoke_id_valid = 1;
3401
3402 } else {
3403 pri_aoc_s_request_response_send(pvt->pri->pri,
3404 call,
3405 aoc_request->invoke_id,
3406 NULL);
3407 }
3408 }
3409
3410 if (request & PRI_AOC_REQUEST_D) {
3411 if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_D) {
3412 pri_aoc_de_request_response_send(pvt->pri->pri,
3413 call,
3414 PRI_AOC_REQ_RSP_CHARGING_INFO_FOLLOWS,
3415 aoc_request->invoke_id);
3416 } else {
3417 pri_aoc_de_request_response_send(pvt->pri->pri,
3418 call,
3419 PRI_AOC_REQ_RSP_ERROR_NOT_AVAILABLE,
3420 aoc_request->invoke_id);
3421 }
3422 }
3423
3424 if (request & PRI_AOC_REQUEST_E) {
3425 if (pvt->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_E) {
3426 pri_aoc_de_request_response_send(pvt->pri->pri,
3427 call,
3428 PRI_AOC_REQ_RSP_CHARGING_INFO_FOLLOWS,
3429 aoc_request->invoke_id);
3430 } else {
3431 pri_aoc_de_request_response_send(pvt->pri->pri,
3432 call,
3433 PRI_AOC_REQ_RSP_ERROR_NOT_AVAILABLE,
3434 aoc_request->invoke_id);
3435 }
3436 }
3437 }
3438 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3439
3440 #if defined(HAVE_PRI_AOC_EVENTS)
3441 /*!
3442 * \internal
3443 * \brief Generate AOC-D AST_CONTROL_AOC frame
3444 * \since 1.8
3445 *
3446 * \param aoc_d AOC-D event parameters.
3447 * \param owner Asterisk channel associated with the call.
3448 * \param passthrough indicating if this message should be queued on the ast channel
3449 *
3450 * \note Assumes the pri->lock is already obtained.
3451 * \note Assumes the sig_pri private is locked
3452 * \note Assumes the owner channel lock is already obtained.
3453 */
3454 static void sig_pri_aoc_d_from_pri(const struct pri_subcmd_aoc_d *aoc_d, struct ast_channel *owner, int passthrough)
3455 {
3456 struct ast_aoc_decoded *decoded = NULL;
3457 struct ast_aoc_encoded *encoded = NULL;
3458 size_t encoded_size = 0;
3459 enum ast_aoc_charge_type type;
3460
3461 if (!owner || !aoc_d) {
3462 return;
3463 }
3464
3465 switch (aoc_d->charge) {
3466 case PRI_AOC_DE_CHARGE_CURRENCY:
3467 type = AST_AOC_CHARGE_CURRENCY;
3468 break;
3469 case PRI_AOC_DE_CHARGE_UNITS:
3470 type = AST_AOC_CHARGE_UNIT;
3471 break;
3472 case PRI_AOC_DE_CHARGE_FREE:
3473 type = AST_AOC_CHARGE_FREE;
3474 break;
3475 default:
3476 type = AST_AOC_CHARGE_NA;
3477 break;
3478 }
3479
3480 if (!(decoded = ast_aoc_create(AST_AOC_D, type, 0))) {
3481 return;
3482 }
3483
3484 switch (aoc_d->billing_accumulation) {
3485 default:
3486 ast_debug(1, "AOC-D billing accumulation has unknown value: %d\n",
3487 aoc_d->billing_accumulation);
3488 /* Fall through */
3489 case 0:/* subTotal */
3490 ast_aoc_set_total_type(decoded, AST_AOC_SUBTOTAL);
3491 break;
3492 case 1:/* total */
3493 ast_aoc_set_total_type(decoded, AST_AOC_TOTAL);
3494 break;
3495 }
3496
3497 switch (aoc_d->billing_id) {
3498 case PRI_AOC_D_BILLING_ID_NORMAL:
3499 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL);
3500 break;
3501 case PRI_AOC_D_BILLING_ID_REVERSE:
3502 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_REVERSE_CHARGE);
3503 break;
3504 case PRI_AOC_D_BILLING_ID_CREDIT_CARD:
3505 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
3506 break;
3507 case PRI_AOC_D_BILLING_ID_NOT_AVAILABLE:
3508 default:
3509 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NA);
3510 break;
3511 }
3512
3513 switch (aoc_d->charge) {
3514 case PRI_AOC_DE_CHARGE_CURRENCY:
3515 ast_aoc_set_currency_info(decoded,
3516 aoc_d->recorded.money.amount.cost,
3517 sig_pri_aoc_multiplier_from_pri(aoc_d->recorded.money.amount.multiplier),
3518 aoc_d->recorded.money.currency);
3519 break;
3520 case PRI_AOC_DE_CHARGE_UNITS:
3521 {
3522 int i;
3523 for (i = 0; i < aoc_d->recorded.unit.num_items; ++i) {
3524 /* if type or number are negative, then they are not present */
3525 ast_aoc_add_unit_entry(decoded,
3526 (aoc_d->recorded.unit.item[i].number >= 0 ? 1 : 0),
3527 aoc_d->recorded.unit.item[i].number,
3528 (aoc_d->recorded.unit.item[i].type >= 0 ? 1 : 0),
3529 aoc_d->recorded.unit.item[i].type);
3530 }
3531 }
3532 break;
3533 }
3534
3535 if (passthrough && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
3536 ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
3537 }
3538
3539 ast_aoc_manager_event(decoded, owner);
3540
3541 ast_aoc_destroy_decoded(decoded);
3542 ast_aoc_destroy_encoded(encoded);
3543 }
3544 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3545
3546 #if defined(HAVE_PRI_AOC_EVENTS)
3547 /*!
3548 * \internal
3549 * \brief Generate AOC-E AST_CONTROL_AOC frame
3550 * \since 1.8
3551 *
3552 * \param aoc_e AOC-E event parameters.
3553 * \param owner Asterisk channel associated with the call.
3554 * \param passthrough indicating if this message should be queued on the ast channel
3555 *
3556 * \note Assumes the pri->lock is already obtained.
3557 * \note Assumes the sig_pri private is locked
3558 * \note Assumes the owner channel lock is already obtained.
3559 * \note owner channel may be NULL. In that case, generate event only
3560 */
3561 static void sig_pri_aoc_e_from_pri(const struct pri_subcmd_aoc_e *aoc_e, struct ast_channel *owner, int passthrough)
3562 {
3563 struct ast_aoc_decoded *decoded = NULL;
3564 struct ast_aoc_encoded *encoded = NULL;
3565 size_t encoded_size = 0;
3566 enum ast_aoc_charge_type type;
3567
3568 if (!aoc_e) {
3569 return;
3570 }
3571
3572 switch (aoc_e->charge) {
3573 case PRI_AOC_DE_CHARGE_CURRENCY:
3574 type = AST_AOC_CHARGE_CURRENCY;
3575 break;
3576 case PRI_AOC_DE_CHARGE_UNITS:
3577 type = AST_AOC_CHARGE_UNIT;
3578 break;
3579 case PRI_AOC_DE_CHARGE_FREE:
3580 type = AST_AOC_CHARGE_FREE;
3581 break;
3582 default:
3583 type = AST_AOC_CHARGE_NA;
3584 break;
3585 }
3586
3587 if (!(decoded = ast_aoc_create(AST_AOC_E, type, 0))) {
3588 return;
3589 }
3590
3591 switch (aoc_e->associated.charging_type) {
3592 case PRI_AOC_E_CHARGING_ASSOCIATION_NUMBER:
3593 if (!aoc_e->associated.charge.number.valid) {
3594 break;
3595 }
3596 ast_aoc_set_association_number(decoded, aoc_e->associated.charge.number.str, aoc_e->associated.charge.number.plan);
3597 break;
3598 case PRI_AOC_E_CHARGING_ASSOCIATION_ID:
3599 ast_aoc_set_association_id(decoded, aoc_e->associated.charge.id);
3600 break;
3601 default:
3602 break;
3603 }
3604
3605 switch (aoc_e->billing_id) {
3606 case PRI_AOC_E_BILLING_ID_NORMAL:
3607 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL);
3608 break;
3609 case PRI_AOC_E_BILLING_ID_REVERSE:
3610 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_REVERSE_CHARGE);
3611 break;
3612 case PRI_AOC_E_BILLING_ID_CREDIT_CARD:
3613 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
3614 break;
3615 case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_UNCONDITIONAL:
3616 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL);
3617 break;
3618 case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_BUSY:
3619 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_BUSY);
3620 break;
3621 case PRI_AOC_E_BILLING_ID_CALL_FORWARDING_NO_REPLY:
3622 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_FWD_NO_REPLY);
3623 break;
3624 case PRI_AOC_E_BILLING_ID_CALL_DEFLECTION:
3625 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_DEFLECTION);
3626 break;
3627 case PRI_AOC_E_BILLING_ID_CALL_TRANSFER:
3628 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CALL_TRANSFER);
3629 break;
3630 case PRI_AOC_E_BILLING_ID_NOT_AVAILABLE:
3631 default:
3632 ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NA);
3633 break;
3634 }
3635
3636 switch (aoc_e->charge) {
3637 case PRI_AOC_DE_CHARGE_CURRENCY:
3638 ast_aoc_set_currency_info(decoded,
3639 aoc_e->recorded.money.amount.cost,
3640 sig_pri_aoc_multiplier_from_pri(aoc_e->recorded.money.amount.multiplier),
3641 aoc_e->recorded.money.currency);
3642 break;
3643 case PRI_AOC_DE_CHARGE_UNITS:
3644 {
3645 int i;
3646 for (i = 0; i < aoc_e->recorded.unit.num_items; ++i) {
3647 /* if type or number are negative, then they are not present */
3648 ast_aoc_add_unit_entry(decoded,
3649 (aoc_e->recorded.unit.item[i].number >= 0 ? 1 : 0),
3650 aoc_e->recorded.unit.item[i].number,
3651 (aoc_e->recorded.unit.item[i].type >= 0 ? 1 : 0),
3652 aoc_e->recorded.unit.item[i].type);
3653 }
3654 }
3655 }
3656
3657 if (passthrough && owner && (encoded = ast_aoc_encode(decoded, &encoded_size, owner))) {
3658 ast_queue_control_data(owner, AST_CONTROL_AOC, encoded, encoded_size);
3659 }
3660
3661 ast_aoc_manager_event(decoded, owner);
3662
3663 ast_aoc_destroy_decoded(decoded);
3664 ast_aoc_destroy_encoded(encoded);
3665 }
3666 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3667
3668 #if defined(HAVE_PRI_AOC_EVENTS)
3669 /*!
3670 * \internal
3671 * \brief send an AOC-S message on the current call
3672 *
3673 * \param pvt sig_pri private channel structure.
3674 * \param decoded decoded ast AOC message
3675 *
3676 * \note Assumes that the PRI lock is already obtained.
3677 */
3678 static void sig_pri_aoc_s_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
3679 {
3680 struct pri_subcmd_aoc_s aoc_s = { 0, };
3681 const struct ast_aoc_s_entry *entry;
3682 int idx;
3683
3684 for (idx = 0; idx < ast_aoc_s_get_count(decoded); idx++) {
3685 if (!(entry = ast_aoc_s_get_rate_info(decoded, idx))) {
3686 break;
3687 }
3688
3689 aoc_s.item[idx].chargeable = sig_pri_aoc_charged_item_to_pri(entry->charged_item);
3690
3691 switch (entry->rate_type) {
3692 case AST_AOC_RATE_TYPE_DURATION:
3693 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_DURATION;
3694 aoc_s.item[idx].rate.duration.amount.cost = entry->rate.duration.amount;
3695 aoc_s.item[idx].rate.duration.amount.multiplier =
3696 sig_pri_aoc_multiplier_from_ast(entry->rate.duration.multiplier);
3697 aoc_s.item[idx].rate.duration.time.length = entry->rate.duration.time;
3698 aoc_s.item[idx].rate.duration.time.scale =
3699 sig_pri_aoc_scale_to_pri(entry->rate.duration.time_scale);
3700 aoc_s.item[idx].rate.duration.granularity.length = entry->rate.duration.granularity_time;
3701 aoc_s.item[idx].rate.duration.granularity.scale =
3702 sig_pri_aoc_scale_to_pri(entry->rate.duration.granularity_time_scale);
3703 aoc_s.item[idx].rate.duration.charging_type = entry->rate.duration.charging_type;
3704
3705 if (!ast_strlen_zero(entry->rate.duration.currency_name)) {
3706 ast_copy_string(aoc_s.item[idx].rate.duration.currency,
3707 entry->rate.duration.currency_name,
3708 sizeof(aoc_s.item[idx].rate.duration.currency));
3709 }
3710 break;
3711 case AST_AOC_RATE_TYPE_FLAT:
3712 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FLAT;
3713 aoc_s.item[idx].rate.flat.amount.cost = entry->rate.flat.amount;
3714 aoc_s.item[idx].rate.flat.amount.multiplier =
3715 sig_pri_aoc_multiplier_from_ast(entry->rate.flat.multiplier);
3716
3717 if (!ast_strlen_zero(entry->rate.flat.currency_name)) {
3718 ast_copy_string(aoc_s.item[idx].rate.flat.currency,
3719 entry->rate.flat.currency_name,
3720 sizeof(aoc_s.item[idx].rate.flat.currency));
3721 }
3722 break;
3723 case AST_AOC_RATE_TYPE_VOLUME:
3724 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_VOLUME;
3725 aoc_s.item[idx].rate.volume.unit = entry->rate.volume.volume_unit;
3726 aoc_s.item[idx].rate.volume.amount.cost = entry->rate.volume.amount;
3727 aoc_s.item[idx].rate.volume.amount.multiplier =
3728 sig_pri_aoc_multiplier_from_ast(entry->rate.volume.multiplier);
3729
3730 if (!ast_strlen_zero(entry->rate.volume.currency_name)) {
3731 ast_copy_string(aoc_s.item[idx].rate.volume.currency,
3732 entry->rate.volume.currency_name,
3733 sizeof(aoc_s.item[idx].rate.volume.currency));
3734 }
3735 break;
3736 case AST_AOC_RATE_TYPE_SPECIAL_CODE:
3737 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_SPECIAL_CODE;
3738 aoc_s.item[idx].rate.special = entry->rate.special_code;
3739 break;
3740 case AST_AOC_RATE_TYPE_FREE:
3741 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FREE;
3742 break;
3743 case AST_AOC_RATE_TYPE_FREE_FROM_BEGINNING:
3744 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_FREE_FROM_BEGINNING;
3745 break;
3746 default:
3747 case AST_AOC_RATE_TYPE_NA:
3748 aoc_s.item[idx].rate_type = PRI_AOC_RATE_TYPE_NOT_AVAILABLE;
3749 break;
3750 }
3751 }
3752 aoc_s.num_items = idx;
3753
3754 /* if this rate should be sent as a response to an AOC-S request we will
3755 * have an aoc_s_request_invoke_id associated with this pvt */
3756 if (pvt->aoc_s_request_invoke_id_valid) {
3757 pri_aoc_s_request_response_send(pvt->pri->pri, pvt->call, pvt->aoc_s_request_invoke_id, &aoc_s);
3758 pvt->aoc_s_request_invoke_id_valid = 0;
3759 } else {
3760 pri_aoc_s_send(pvt->pri->pri, pvt->call, &aoc_s);
3761 }
3762 }
3763 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3764
3765 #if defined(HAVE_PRI_AOC_EVENTS)
3766 /*!
3767 * \internal
3768 * \brief send an AOC-D message on the current call
3769 *
3770 * \param pvt sig_pri private channel structure.
3771 * \param decoded decoded ast AOC message
3772 *
3773 * \note Assumes that the PRI lock is already obtained.
3774 */
3775 static void sig_pri_aoc_d_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
3776 {
3777 struct pri_subcmd_aoc_d aoc_d = { 0, };
3778
3779 aoc_d.billing_accumulation = (ast_aoc_get_total_type(decoded) == AST_AOC_TOTAL) ? 1 : 0;
3780
3781 switch (ast_aoc_get_billing_id(decoded)) {
3782 case AST_AOC_BILLING_NORMAL:
3783 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_NORMAL;
3784 break;
3785 case AST_AOC_BILLING_REVERSE_CHARGE:
3786 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_REVERSE;
3787 break;
3788 case AST_AOC_BILLING_CREDIT_CARD:
3789 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_CREDIT_CARD;
3790 break;
3791 case AST_AOC_BILLING_NA:
3792 default:
3793 aoc_d.billing_id = PRI_AOC_D_BILLING_ID_NOT_AVAILABLE;
3794 break;
3795 }
3796
3797 switch (ast_aoc_get_charge_type(decoded)) {
3798 case AST_AOC_CHARGE_FREE:
3799 aoc_d.charge = PRI_AOC_DE_CHARGE_FREE;
3800 break;
3801 case AST_AOC_CHARGE_CURRENCY:
3802 {
3803 const char *currency_name = ast_aoc_get_currency_name(decoded);
3804 aoc_d.charge = PRI_AOC_DE_CHARGE_CURRENCY;
3805 aoc_d.recorded.money.amount.cost = ast_aoc_get_currency_amount(decoded);
3806 aoc_d.recorded.money.amount.multiplier = sig_pri_aoc_multiplier_from_ast(ast_aoc_get_currency_multiplier(decoded));
3807 if (!ast_strlen_zero(currency_name)) {
3808 ast_copy_string(aoc_d.recorded.money.currency, currency_name, sizeof(aoc_d.recorded.money.currency));
3809 }
3810 }
3811 break;
3812 case AST_AOC_CHARGE_UNIT:
3813 {
3814 const struct ast_aoc_unit_entry *entry;
3815 int i;
3816 aoc_d.charge = PRI_AOC_DE_CHARGE_UNITS;
3817 for (i = 0; i < ast_aoc_get_unit_count(decoded); i++) {
3818 if ((entry = ast_aoc_get_unit_info(decoded, i)) && i < ARRAY_LEN(aoc_d.recorded.unit.item)) {
3819 if (entry->valid_amount) {
3820 aoc_d.recorded.unit.item[i].number = entry->amount;
3821 } else {
3822 aoc_d.recorded.unit.item[i].number = -1;
3823 }
3824 if (entry->valid_type) {
3825 aoc_d.recorded.unit.item[i].type = entry->type;
3826 } else {
3827 aoc_d.recorded.unit.item[i].type = -1;
3828 }
3829 aoc_d.recorded.unit.num_items++;
3830 } else {
3831 break;
3832 }
3833 }
3834 }
3835 break;
3836 case AST_AOC_CHARGE_NA:
3837 default:
3838 aoc_d.charge = PRI_AOC_DE_CHARGE_NOT_AVAILABLE;
3839 break;
3840 }
3841
3842 pri_aoc_d_send(pvt->pri->pri, pvt->call, &aoc_d);
3843 }
3844 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3845
3846 #if defined(HAVE_PRI_AOC_EVENTS)
3847 /*!
3848 * \internal
3849 * \brief send an AOC-E message on the current call
3850 *
3851 * \param pvt sig_pri private channel structure.
3852 * \param decoded decoded ast AOC message
3853 *
3854 * \note Assumes that the PRI lock is already obtained.
3855 */
3856 static void sig_pri_aoc_e_from_ast(struct sig_pri_chan *pvt, struct ast_aoc_decoded *decoded)
3857 {
3858 struct pri_subcmd_aoc_e *aoc_e = &pvt->aoc_e;
3859 const struct ast_aoc_charging_association *ca = ast_aoc_get_association_info(decoded);
3860
3861 memset(aoc_e, 0, sizeof(*aoc_e));
3862 pvt->holding_aoce = 1;
3863
3864 switch (ca->charging_type) {
3865 case AST_AOC_CHARGING_ASSOCIATION_NUMBER:
3866 aoc_e->associated.charge.number.valid = 1;
3867 ast_copy_string(aoc_e->associated.charge.number.str,
3868 ca->charge.number.number,
3869 sizeof(aoc_e->associated.charge.number.str));
3870 aoc_e->associated.charge.number.plan = ca->charge.number.plan;
3871 aoc_e->associated.charging_type = PRI_AOC_E_CHARGING_ASSOCIATION_NUMBER;
3872 break;
3873 case AST_AOC_CHARGING_ASSOCIATION_ID:
3874 aoc_e->associated.charge.id = ca->charge.id;
3875 aoc_e->associated.charging_type = PRI_AOC_E_CHARGING_ASSOCIATION_ID;
3876 break;
3877 case AST_AOC_CHARGING_ASSOCIATION_NA:
3878 default:
3879 break;
3880 }
3881
3882 switch (ast_aoc_get_billing_id(decoded)) {
3883 case AST_AOC_BILLING_NORMAL:
3884 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_NORMAL;
3885 break;
3886 case AST_AOC_BILLING_REVERSE_CHARGE:
3887 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_REVERSE;
3888 break;
3889 case AST_AOC_BILLING_CREDIT_CARD:
3890 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CREDIT_CARD;
3891 break;
3892 case AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL:
3893 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_UNCONDITIONAL;
3894 break;
3895 case AST_AOC_BILLING_CALL_FWD_BUSY:
3896 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_BUSY;
3897 break;
3898 case AST_AOC_BILLING_CALL_FWD_NO_REPLY:
3899 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_FORWARDING_NO_REPLY;
3900 break;
3901 case AST_AOC_BILLING_CALL_DEFLECTION:
3902 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_DEFLECTION;
3903 break;
3904 case AST_AOC_BILLING_CALL_TRANSFER:
3905 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_CALL_TRANSFER;
3906 break;
3907 case AST_AOC_BILLING_NA:
3908 default:
3909 aoc_e->billing_id = PRI_AOC_E_BILLING_ID_NOT_AVAILABLE;
3910 break;
3911 }
3912
3913 switch (ast_aoc_get_charge_type(decoded)) {
3914 case AST_AOC_CHARGE_FREE:
3915 aoc_e->charge = PRI_AOC_DE_CHARGE_FREE;
3916 break;
3917 case AST_AOC_CHARGE_CURRENCY:
3918 {
3919 const char *currency_name = ast_aoc_get_currency_name(decoded);
3920 aoc_e->charge = PRI_AOC_DE_CHARGE_CURRENCY;
3921 aoc_e->recorded.money.amount.cost = ast_aoc_get_currency_amount(decoded);
3922 aoc_e->recorded.money.amount.multiplier = sig_pri_aoc_multiplier_from_ast(ast_aoc_get_currency_multiplier(decoded));
3923 if (!ast_strlen_zero(currency_name)) {
3924 ast_copy_string(aoc_e->recorded.money.currency, currency_name, sizeof(aoc_e->recorded.money.currency));
3925 }
3926 }
3927 break;
3928 case AST_AOC_CHARGE_UNIT:
3929 {
3930 const struct ast_aoc_unit_entry *entry;
3931 int i;
3932 aoc_e->charge = PRI_AOC_DE_CHARGE_UNITS;
3933 for (i = 0; i < ast_aoc_get_unit_count(decoded); i++) {
3934 if ((entry = ast_aoc_get_unit_info(decoded, i)) && i < ARRAY_LEN(aoc_e->recorded.unit.item)) {
3935 if (entry->valid_amount) {
3936 aoc_e->recorded.unit.item[i].number = entry->amount;
3937 } else {
3938 aoc_e->recorded.unit.item[i].number = -1;
3939 }
3940 if (entry->valid_type) {
3941 aoc_e->recorded.unit.item[i].type = entry->type;
3942 } else {
3943 aoc_e->recorded.unit.item[i].type = -1;
3944 }
3945 aoc_e->recorded.unit.num_items++;
3946 }
3947 }
3948 }
3949 break;
3950 case AST_AOC_CHARGE_NA:
3951 default:
3952 aoc_e->charge = PRI_AOC_DE_CHARGE_NOT_AVAILABLE;
3953 break;
3954 }
3955 }
3956 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
3957
3958 #if defined(HAVE_PRI_AOC_EVENTS)
3959 /*!
3960 * \internal
3961 * \brief send an AOC-E termination request on ast_channel and set
3962 * hangup delay.
3963 *
3964 * \param pri PRI span control structure.
3965 * \param chanpos Channel position in the span.
3966 * \param ms to delay hangup
3967 *
3968 * \note Assumes the pri->lock is already obtained.
3969 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
3970 */
3971 static void sig_pri_send_aoce_termination_request(struct sig_pri_span *pri, int chanpos, unsigned int ms)
3972 {
3973 struct sig_pri_chan *pvt;
3974 struct ast_aoc_decoded *decoded = NULL;
3975 struct ast_aoc_encoded *encoded = NULL;
3976 size_t encoded_size;
3977 struct timeval whentohangup = { 0, };
3978
3979 sig_pri_lock_owner(pri, chanpos);
3980 pvt = pri->pvts[chanpos];
3981 if (!pvt->owner) {
3982 return;
3983 }
3984
3985 if (!(decoded = ast_aoc_create(AST_AOC_REQUEST, 0, AST_AOC_REQUEST_E))) {
3986 ast_queue_hangup(pvt->owner);
3987 goto cleanup_termination_request;
3988 }
3989
3990 ast_aoc_set_termination_request(decoded);
3991
3992 if (!(encoded = ast_aoc_encode(decoded, &encoded_size, pvt->owner))) {
3993 ast_queue_hangup(pvt->owner);
3994 goto cleanup_termination_request;
3995 }
3996
3997 /* convert ms to timeval */
3998 whentohangup.tv_usec = (ms % 1000) * 1000;
3999 whentohangup.tv_sec = ms / 1000;
4000
4001 if (ast_queue_control_data(pvt->owner, AST_CONTROL_AOC, encoded, encoded_size)) {
4002 ast_queue_hangup(pvt->owner);
4003 goto cleanup_termination_request;
4004 }
4005
4006 pvt->waiting_for_aoce = 1;
4007 ast_channel_setwhentohangup_tv(pvt->owner, whentohangup);
4008 ast_debug(1, "Delaying hangup on %s for aoc-e msg\n", ast_channel_name(pvt->owner));
4009
4010 cleanup_termination_request:
4011 ast_channel_unlock(pvt->owner);
4012 ast_aoc_destroy_decoded(decoded);
4013 ast_aoc_destroy_encoded(encoded);
4014 }
4015 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
4016
4017 /*!
4018 * \internal
4019 * \brief TRUE if PRI event came in on a CIS call.
4020 * \since 1.8
4021 *
4022 * \param channel PRI encoded span/channel
4023 *
4024 * \retval non-zero if CIS call.
4025 */
4026 static int sig_pri_is_cis_call(int channel)
4027 {
4028 return channel != -1 && (channel & PRI_CIS_CALL);
4029 }
4030
4031 /*!
4032 * \internal
4033 * \brief Handle the CIS associated PRI subcommand events.
4034 * \since 1.8
4035 *
4036 * \param pri PRI span control structure.
4037 * \param event_id PRI event id
4038 * \param subcmds Subcommands to process if any. (Could be NULL).
4039 * \param call_rsp libpri opaque call structure to send any responses toward.
4040 * Could be NULL either because it is not available or the call is for the
4041 * dummy call reference. However, this should not be NULL in the cases that
4042 * need to use the pointer to send a response message back.
4043 *
4044 * \note Assumes the pri->lock is already obtained.
4045 */
4046 static void sig_pri_handle_cis_subcmds(struct sig_pri_span *pri, int event_id,
4047 const struct pri_subcommands *subcmds, q931_call *call_rsp)
4048 {
4049 int index;
4050 #if defined(HAVE_PRI_CCSS)
4051 struct ast_cc_agent *agent;
4052 struct sig_pri_cc_agent_prv *agent_prv;
4053 struct sig_pri_cc_monitor_instance *monitor;
4054 #endif /* defined(HAVE_PRI_CCSS) */
4055
4056 if (!subcmds) {
4057 return;
4058 }
4059 for (index = 0; index < subcmds->counter_subcmd; ++index) {
4060 const struct pri_subcommand *subcmd = &subcmds->subcmd[index];
4061
4062 switch (subcmd->cmd) {
4063 #if defined(STATUS_REQUEST_PLACE_HOLDER)
4064 case PRI_SUBCMD_STATUS_REQ:
4065 case PRI_SUBCMD_STATUS_REQ_RSP:
4066 /* Ignore for now. */
4067 break;
4068 #endif /* defined(STATUS_REQUEST_PLACE_HOLDER) */
4069 #if defined(HAVE_PRI_CCSS)
4070 case PRI_SUBCMD_CC_REQ:
4071 agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_request.cc_id);
4072 if (!agent) {
4073 pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
4074 break;
4075 }
4076 if (!ast_cc_request_is_within_limits()) {
4077 if (pri_cc_req_rsp(pri->pri, subcmd->u.cc_request.cc_id,
4078 5/* queue_full */)) {
4079 pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
4080 }
4081 ast_cc_failed(agent->core_id, "%s agent system CC queue full",
4082 sig_pri_cc_type_name);
4083 ao2_ref(agent, -1);
4084 break;
4085 }
4086 agent_prv = agent->private_data;
4087 agent_prv->cc_request_response_pending = 1;
4088 if (ast_cc_agent_accept_request(agent->core_id,
4089 "%s caller accepted CC offer.", sig_pri_cc_type_name)) {
4090 agent_prv->cc_request_response_pending = 0;
4091 if (pri_cc_req_rsp(pri->pri, subcmd->u.cc_request.cc_id,
4092 2/* short_term_denial */)) {
4093 pri_cc_cancel(pri->pri, subcmd->u.cc_request.cc_id);
4094 }
4095 ast_cc_failed(agent->core_id, "%s agent CC core request accept failed",
4096 sig_pri_cc_type_name);
4097 }
4098 ao2_ref(agent, -1);
4099 break;
4100 #endif /* defined(HAVE_PRI_CCSS) */
4101 #if defined(HAVE_PRI_CCSS)
4102 case PRI_SUBCMD_CC_REQ_RSP:
4103 monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
4104 subcmd->u.cc_request_rsp.cc_id);
4105 if (!monitor) {
4106 pri_cc_cancel(pri->pri, subcmd->u.cc_request_rsp.cc_id);
4107 break;
4108 }
4109 switch (subcmd->u.cc_request_rsp.status) {
4110 case 0:/* success */
4111 ast_cc_monitor_request_acked(monitor->core_id,
4112 "%s far end accepted CC request", sig_pri_cc_type_name);
4113 break;
4114 case 1:/* timeout */
4115 ast_verb(2, "core_id:%d %s CC request timeout\n", monitor->core_id,
4116 sig_pri_cc_type_name);
4117 ast_cc_monitor_failed(monitor->core_id, monitor->name,
4118 "%s CC request timeout", sig_pri_cc_type_name);
4119 break;
4120 case 2:/* error */
4121 ast_verb(2, "core_id:%d %s CC request error: %s\n", monitor->core_id,
4122 sig_pri_cc_type_name,
4123 pri_facility_error2str(subcmd->u.cc_request_rsp.fail_code));
4124 ast_cc_monitor_failed(monitor->core_id, monitor->name,
4125 "%s CC request error", sig_pri_cc_type_name);
4126 break;
4127 case 3:/* reject */
4128 ast_verb(2, "core_id:%d %s CC request reject: %s\n", monitor->core_id,
4129 sig_pri_cc_type_name,
4130 pri_facility_reject2str(subcmd->u.cc_request_rsp.fail_code));
4131 ast_cc_monitor_failed(monitor->core_id, monitor->name,
4132 "%s CC request reject", sig_pri_cc_type_name);
4133 break;
4134 default:
4135 ast_verb(2, "core_id:%d %s CC request unknown status %d\n",
4136 monitor->core_id, sig_pri_cc_type_name,
4137 subcmd->u.cc_request_rsp.status);
4138 ast_cc_monitor_failed(monitor->core_id, monitor->name,
4139 "%s CC request unknown status", sig_pri_cc_type_name);
4140 break;
4141 }
4142 ao2_ref(monitor, -1);
4143 break;
4144 #endif /* defined(HAVE_PRI_CCSS) */
4145 #if defined(HAVE_PRI_CCSS)
4146 case PRI_SUBCMD_CC_REMOTE_USER_FREE:
4147 monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
4148 subcmd->u.cc_remote_user_free.cc_id);
4149 if (!monitor) {
4150 pri_cc_cancel(pri->pri, subcmd->u.cc_remote_user_free.cc_id);
4151 break;
4152 }
4153 ast_cc_monitor_callee_available(monitor->core_id,
4154 "%s callee has become available", sig_pri_cc_type_name);
4155 ao2_ref(monitor, -1);
4156 break;
4157 #endif /* defined(HAVE_PRI_CCSS) */
4158 #if defined(HAVE_PRI_CCSS)
4159 case PRI_SUBCMD_CC_B_FREE:
4160 monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
4161 subcmd->u.cc_b_free.cc_id);
4162 if (!monitor) {
4163 pri_cc_cancel(pri->pri, subcmd->u.cc_b_free.cc_id);
4164 break;
4165 }
4166 ast_cc_monitor_party_b_free(monitor->core_id);
4167 ao2_ref(monitor, -1);
4168 break;
4169 #endif /* defined(HAVE_PRI_CCSS) */
4170 #if defined(HAVE_PRI_CCSS)
4171 case PRI_SUBCMD_CC_STATUS_REQ:
4172 monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
4173 subcmd->u.cc_status_req.cc_id);
4174 if (!monitor) {
4175 pri_cc_cancel(pri->pri, subcmd->u.cc_status_req.cc_id);
4176 break;
4177 }
4178 ast_cc_monitor_status_request(monitor->core_id);
4179 ao2_ref(monitor, -1);
4180 break;
4181 #endif /* defined(HAVE_PRI_CCSS) */
4182 #if defined(HAVE_PRI_CCSS)
4183 case PRI_SUBCMD_CC_STATUS_REQ_RSP:
4184 agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_status_req_rsp.cc_id);
4185 if (!agent) {
4186 pri_cc_cancel(pri->pri, subcmd->u.cc_status_req_rsp.cc_id);
4187 break;
4188 }
4189 ast_cc_agent_status_response(agent->core_id,
4190 subcmd->u.cc_status_req_rsp.status ? AST_DEVICE_INUSE
4191 : AST_DEVICE_NOT_INUSE);
4192 ao2_ref(agent, -1);
4193 break;
4194 #endif /* defined(HAVE_PRI_CCSS) */
4195 #if defined(HAVE_PRI_CCSS)
4196 case PRI_SUBCMD_CC_STATUS:
4197 agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_status.cc_id);
4198 if (!agent) {
4199 pri_cc_cancel(pri->pri, subcmd->u.cc_status.cc_id);
4200 break;
4201 }
4202 if (subcmd->u.cc_status.status) {
4203 ast_cc_agent_caller_busy(agent->core_id, "%s agent caller is busy",
4204 sig_pri_cc_type_name);
4205 } else {
4206 ast_cc_agent_caller_available(agent->core_id,
4207 "%s agent caller is available", sig_pri_cc_type_name);
4208 }
4209 ao2_ref(agent, -1);
4210 break;
4211 #endif /* defined(HAVE_PRI_CCSS) */
4212 #if defined(HAVE_PRI_CCSS)
4213 case PRI_SUBCMD_CC_CANCEL:
4214 sig_pri_cc_link_canceled(pri, subcmd->u.cc_cancel.cc_id,
4215 subcmd->u.cc_cancel.is_agent);
4216 break;
4217 #endif /* defined(HAVE_PRI_CCSS) */
4218 #if defined(HAVE_PRI_CCSS)
4219 case PRI_SUBCMD_CC_STOP_ALERTING:
4220 monitor = sig_pri_find_cc_monitor_by_cc_id(pri,
4221 subcmd->u.cc_stop_alerting.cc_id);
4222 if (!monitor) {
4223 pri_cc_cancel(pri->pri, subcmd->u.cc_stop_alerting.cc_id);
4224 break;
4225 }
4226 ast_cc_monitor_stop_ringing(monitor->core_id);
4227 ao2_ref(monitor, -1);
4228 break;
4229 #endif /* defined(HAVE_PRI_CCSS) */
4230 #if defined(HAVE_PRI_AOC_EVENTS)
4231 case PRI_SUBCMD_AOC_E:
4232 /* Queue AST_CONTROL_AOC frame */
4233 sig_pri_aoc_e_from_pri(&subcmd->u.aoc_e, NULL, 0);
4234 break;
4235 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
4236 default:
4237 ast_debug(2, "Span %d: Unknown CIS subcommand(%d) in %s event.\n", pri->span,
4238 subcmd->cmd, pri_event2str(event_id));
4239 break;
4240 }
4241 }
4242 }
4243
4244 /*!
4245 * \internal
4246 * \brief Handle the call associated PRI subcommand events.
4247 * \since 1.8
4248 *
4249 * \param pri PRI span control structure.
4250 * \param chanpos Channel position in the span.
4251 * \param event_id PRI event id
4252 * \param subcmds Subcommands to process if any. (Could be NULL).
4253 * \param call_rsp libpri opaque call structure to send any responses toward.
4254 * Could be NULL either because it is not available or the call is for the
4255 * dummy call reference. However, this should not be NULL in the cases that
4256 * need to use the pointer to send a response message back.
4257 *
4258 * \note Assumes the pri->lock is already obtained.
4259 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
4260 */
4261 static void sig_pri_handle_subcmds(struct sig_pri_span *pri, int chanpos, int event_id,
4262 const struct pri_subcommands *subcmds, q931_call *call_rsp)
4263 {
4264 int index;
4265 struct ast_channel *owner;
4266 struct ast_party_redirecting ast_redirecting;
4267 #if defined(HAVE_PRI_TRANSFER)
4268 struct xfer_rsp_data xfer_rsp;
4269 #endif /* defined(HAVE_PRI_TRANSFER) */
4270
4271 if (!subcmds) {
4272 return;
4273 }
4274 for (index = 0; index < subcmds->counter_subcmd; ++index) {
4275 const struct pri_subcommand *subcmd = &subcmds->subcmd[index];
4276
4277 switch (subcmd->cmd) {
4278 case PRI_SUBCMD_CONNECTED_LINE:
4279 sig_pri_lock_owner(pri, chanpos);
4280 owner = pri->pvts[chanpos]->owner;
4281 if (owner) {
4282 struct ast_party_connected_line ast_connected;
4283 int caller_id_update;
4284
4285 /* Extract the connected line information */
4286 ast_party_connected_line_init(&ast_connected);
4287 sig_pri_party_id_convert(&ast_connected.id, &subcmd->u.connected_line.id,
4288 pri);
4289 ast_connected.id.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
4290
4291 caller_id_update = 0;
4292 if (ast_connected.id.name.str) {
4293 /* Save name for Caller-ID update */
4294 ast_copy_string(pri->pvts[chanpos]->cid_name,
4295 ast_connected.id.name.str, sizeof(pri->pvts[chanpos]->cid_name));
4296 caller_id_update = 1;
4297 }
4298 if (ast_connected.id.number.str) {
4299 /* Save number for Caller-ID update */
4300 ast_copy_string(pri->pvts[chanpos]->cid_num,
4301 ast_connected.id.number.str, sizeof(pri->pvts[chanpos]->cid_num));
4302 pri->pvts[chanpos]->cid_ton = ast_connected.id.number.plan;
4303 caller_id_update = 1;
4304 }
4305 ast_connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
4306
4307 pri->pvts[chanpos]->cid_subaddr[0] = '\0';
4308 #if defined(HAVE_PRI_SUBADDR)
4309 if (ast_connected.id.subaddress.str) {
4310 ast_copy_string(pri->pvts[chanpos]->cid_subaddr,
4311 ast_connected.id.subaddress.str,
4312 sizeof(pri->pvts[chanpos]->cid_subaddr));
4313 caller_id_update = 1;
4314 }
4315 #endif /* defined(HAVE_PRI_SUBADDR) */
4316 if (caller_id_update) {
4317 struct ast_party_caller ast_caller;
4318
4319 pri->pvts[chanpos]->callingpres =
4320 ast_party_id_presentation(&ast_connected.id);
4321 sig_pri_set_caller_id(pri->pvts[chanpos]);
4322
4323 ast_party_caller_set_init(&ast_caller, ast_channel_caller(owner));
4324 ast_caller.id = ast_connected.id;
4325 ast_caller.ani = ast_connected.id;
4326 ast_channel_set_caller_event(owner, &ast_caller, NULL);
4327
4328 /* Update the connected line information on the other channel */
4329 if (event_id != PRI_EVENT_RING) {
4330 /* This connected_line update was not from a SETUP message. */
4331 ast_channel_queue_connected_line_update(owner, &ast_connected,
4332 NULL);
4333 }
4334 }
4335
4336 ast_party_connected_line_free(&ast_connected);
4337 ast_channel_unlock(owner);
4338 }
4339 break;
4340 case PRI_SUBCMD_REDIRECTING:
4341 sig_pri_lock_owner(pri, chanpos);
4342 owner = pri->pvts[chanpos]->owner;
4343 if (owner) {
4344 sig_pri_redirecting_convert(&ast_redirecting, &subcmd->u.redirecting,
4345 ast_channel_redirecting(owner), pri);
4346 ast_redirecting.orig.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
4347 ast_redirecting.from.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
4348 ast_redirecting.to.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
4349 ast_channel_set_redirecting(owner, &ast_redirecting, NULL);
4350 if (event_id != PRI_EVENT_RING) {
4351 /* This redirection was not from a SETUP message. */
4352
4353 /* Invalidate any earlier private redirecting id representations */
4354 ast_party_id_invalidate(&ast_redirecting.priv_orig);
4355 ast_party_id_invalidate(&ast_redirecting.priv_from);
4356 ast_party_id_invalidate(&ast_redirecting.priv_to);
4357
4358 ast_channel_queue_redirecting_update(owner, &ast_redirecting, NULL);
4359 }
4360 ast_party_redirecting_free(&ast_redirecting);
4361
4362 ast_channel_unlock(owner);
4363 }
4364 break;
4365 #if defined(HAVE_PRI_CALL_REROUTING)
4366 case PRI_SUBCMD_REROUTING:
4367 sig_pri_lock_owner(pri, chanpos);
4368 owner = pri->pvts[chanpos]->owner;
4369 if (owner) {
4370 struct pri_party_redirecting pri_deflection;
4371
4372 if (!call_rsp) {
4373 ast_log(LOG_WARNING,
4374 "Span %d: %s tried CallRerouting/CallDeflection to '%s' without call!\n",
4375 pri->span, ast_channel_name(owner), subcmd->u.rerouting.deflection.to.number.str);
4376 ast_channel_unlock(owner);
4377 break;
4378 }
4379 if (ast_strlen_zero(subcmd->u.rerouting.deflection.to.number.str)) {
4380 ast_log(LOG_WARNING,
4381 "Span %d: %s tried CallRerouting/CallDeflection to empty number!\n",
4382 pri->span, ast_channel_name(owner));
4383 pri_rerouting_rsp(pri->pri, call_rsp, subcmd->u.rerouting.invoke_id,
4384 PRI_REROUTING_RSP_INVALID_NUMBER);
4385 ast_channel_unlock(owner);
4386 break;
4387 }
4388
4389 ast_verb(3, "Span %d: %s is CallRerouting/CallDeflection to '%s'.\n",
4390 pri->span, ast_channel_name(owner), subcmd->u.rerouting.deflection.to.number.str);
4391
4392 /*
4393 * Send back positive ACK to CallRerouting/CallDeflection.
4394 *
4395 * Note: This call will be hungup by the core when it processes
4396 * the call_forward string.
4397 */
4398 pri_rerouting_rsp(pri->pri, call_rsp, subcmd->u.rerouting.invoke_id,
4399 PRI_REROUTING_RSP_OK_CLEAR);
4400
4401 pri_deflection = subcmd->u.rerouting.deflection;
4402
4403 /* Adjust the deflecting to number based upon the subscription option. */
4404 switch (subcmd->u.rerouting.subscription_option) {
4405 case 0: /* noNotification */
4406 case 1: /* notificationWithoutDivertedToNr */
4407 /* Delete the number because the far end is not supposed to see it. */
4408 pri_deflection.to.number.presentation =
4409 PRI_PRES_RESTRICTED | PRI_PRES_USER_NUMBER_UNSCREENED;
4410 pri_deflection.to.number.plan =
4411 (PRI_TON_UNKNOWN << 4) | PRI_NPI_E163_E164;
4412 pri_deflection.to.number.str[0] = '\0';
4413 break;
4414 case 2: /* notificationWithDivertedToNr */
4415 break;
4416 case 3: /* notApplicable */
4417 default:
4418 break;
4419 }
4420 sig_pri_redirecting_convert(&ast_redirecting, &pri_deflection,
4421 ast_channel_redirecting(owner), pri);
4422 ast_redirecting.orig.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
4423 ast_redirecting.from.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
4424 ast_redirecting.to.tag = ast_strdup(pri->pvts[chanpos]->user_tag);
4425 ast_channel_set_redirecting(owner, &ast_redirecting, NULL);
4426 ast_party_redirecting_free(&ast_redirecting);
4427
4428 /* Request the core to forward to the new number. */
4429 ast_channel_call_forward_set(owner, subcmd->u.rerouting.deflection.to.number.str);
4430
4431 /* Wake up the channel. */
4432 ast_queue_frame(owner, &ast_null_frame);
4433
4434 ast_channel_unlock(owner);
4435 }
4436 break;
4437 #endif /* defined(HAVE_PRI_CALL_REROUTING) */
4438 #if defined(HAVE_PRI_CCSS)
4439 case PRI_SUBCMD_CC_AVAILABLE:
4440 sig_pri_lock_owner(pri, chanpos);
4441 owner = pri->pvts[chanpos]->owner;
4442 if (owner) {
4443 enum ast_cc_service_type service;
4444
4445 switch (event_id) {
4446 case PRI_EVENT_RINGING:
4447 service = AST_CC_CCNR;
4448 break;
4449 case PRI_EVENT_HANGUP_REQ:
4450 /* We will assume that the cause was busy/congestion. */
4451 service = AST_CC_CCBS;
4452 break;
4453 default:
4454 service = AST_CC_NONE;
4455 break;
4456 }
4457 if (service == AST_CC_NONE
4458 || sig_pri_cc_available(pri, chanpos, subcmd->u.cc_available.cc_id,
4459 service)) {
4460 pri_cc_cancel(pri->pri, subcmd->u.cc_available.cc_id);
4461 }
4462 ast_channel_unlock(owner);
4463 } else {
4464 /* No asterisk channel. */
4465 pri_cc_cancel(pri->pri, subcmd->u.cc_available.cc_id);
4466 }
4467 break;
4468 #endif /* defined(HAVE_PRI_CCSS) */
4469 #if defined(HAVE_PRI_CCSS)
4470 case PRI_SUBCMD_CC_CALL:
4471 sig_pri_lock_owner(pri, chanpos);
4472 owner = pri->pvts[chanpos]->owner;
4473 if (owner) {
4474 struct ast_cc_agent *agent;
4475
4476 agent = sig_pri_find_cc_agent_by_cc_id(pri, subcmd->u.cc_call.cc_id);
4477 if (agent) {
4478 ast_setup_cc_recall_datastore(owner, agent->core_id);
4479 ast_cc_agent_set_interfaces_chanvar(owner);
4480 ast_cc_agent_recalling(agent->core_id,
4481 "%s caller is attempting recall", sig_pri_cc_type_name);
4482 ao2_ref(agent, -1);
4483 }
4484
4485 ast_channel_unlock(owner);
4486 }
4487 break;
4488 #endif /* defined(HAVE_PRI_CCSS) */
4489 #if defined(HAVE_PRI_CCSS)
4490 case PRI_SUBCMD_CC_CANCEL:
4491 sig_pri_cc_link_canceled(pri, subcmd->u.cc_cancel.cc_id,
4492 subcmd->u.cc_cancel.is_agent);
4493 break;
4494 #endif /* defined(HAVE_PRI_CCSS) */
4495 #if defined(HAVE_PRI_TRANSFER)
4496 case PRI_SUBCMD_TRANSFER_CALL:
4497 if (!call_rsp) {
4498 /* Should never happen. */
4499 ast_log(LOG_ERROR,
4500 "Call transfer subcommand without call to send response!\n");
4501 break;
4502 }
4503
4504 sig_pri_unlock_private(pri->pvts[chanpos]);
4505 xfer_rsp.pri = pri;
4506 xfer_rsp.call = call_rsp;
4507 xfer_rsp.invoke_id = subcmd->u.transfer.invoke_id;
4508 xfer_rsp.responded = 0;
4509 sig_pri_attempt_transfer(pri,
4510 subcmd->u.transfer.call_1, subcmd->u.transfer.is_call_1_held,
4511 subcmd->u.transfer.call_2, subcmd->u.transfer.is_call_2_held,
4512 &xfer_rsp);
4513 sig_pri_lock_private(pri->pvts[chanpos]);
4514 break;
4515 #endif /* defined(HAVE_PRI_TRANSFER) */
4516 #if defined(HAVE_PRI_AOC_EVENTS)
4517 case PRI_SUBCMD_AOC_S:
4518 sig_pri_lock_owner(pri, chanpos);
4519 owner = pri->pvts[chanpos]->owner;
4520 if (owner) {
4521 sig_pri_aoc_s_from_pri(&subcmd->u.aoc_s, owner,
4522 (pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S));
4523 ast_channel_unlock(owner);
4524 }
4525 break;
4526 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
4527 #if defined(HAVE_PRI_AOC_EVENTS)
4528 case PRI_SUBCMD_AOC_D:
4529 sig_pri_lock_owner(pri, chanpos);
4530 owner = pri->pvts[chanpos]->owner;
4531 if (owner) {
4532 /* Queue AST_CONTROL_AOC frame on channel */
4533 sig_pri_aoc_d_from_pri(&subcmd->u.aoc_d, owner,
4534 (pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_D));
4535 ast_channel_unlock(owner);
4536 }
4537 break;
4538 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
4539 #if defined(HAVE_PRI_AOC_EVENTS)
4540 case PRI_SUBCMD_AOC_E:
4541 sig_pri_lock_owner(pri, chanpos);
4542 owner = pri->pvts[chanpos]->owner;
4543 /* Queue AST_CONTROL_AOC frame */
4544 sig_pri_aoc_e_from_pri(&subcmd->u.aoc_e, owner,
4545 (pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_E));
4546 if (owner) {
4547 ast_channel_unlock(owner);
4548 }
4549 break;
4550 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
4551 #if defined(HAVE_PRI_AOC_EVENTS)
4552 case PRI_SUBCMD_AOC_CHARGING_REQ:
4553 sig_pri_lock_owner(pri, chanpos);
4554 owner = pri->pvts[chanpos]->owner;
4555 if (owner) {
4556 sig_pri_aoc_request_from_pri(&subcmd->u.aoc_request, pri->pvts[chanpos],
4557 call_rsp);
4558 ast_channel_unlock(owner);
4559 }
4560 break;
4561 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
4562 #if defined(HAVE_PRI_AOC_EVENTS)
4563 case PRI_SUBCMD_AOC_CHARGING_REQ_RSP:
4564 /*
4565 * An AOC request response may contain an AOC-S rate list.
4566 * If this is the case handle this just like we
4567 * would an incoming AOC-S msg.
4568 */
4569 if (subcmd->u.aoc_request_response.valid_aoc_s) {
4570 sig_pri_lock_owner(pri, chanpos);
4571 owner = pri->pvts[chanpos]->owner;
4572 if (owner) {
4573 sig_pri_aoc_s_from_pri(&subcmd->u.aoc_request_response.aoc_s, owner,
4574 (pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S));
4575 ast_channel_unlock(owner);
4576 }
4577 }
4578 break;
4579 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
4580 #if defined(HAVE_PRI_MCID)
4581 case PRI_SUBCMD_MCID_REQ:
4582 sig_pri_lock_owner(pri, chanpos);
4583 owner = pri->pvts[chanpos]->owner;
4584 sig_pri_mcid_event(pri, &subcmd->u.mcid_req, owner);
4585 if (owner) {
4586 ast_channel_unlock(owner);
4587 }
4588 break;
4589 #endif /* defined(HAVE_PRI_MCID) */
4590 #if defined(HAVE_PRI_MCID)
4591 case PRI_SUBCMD_MCID_RSP:
4592 /* Ignore for now. */
4593 break;
4594 #endif /* defined(HAVE_PRI_MCID) */
4595 #if defined(HAVE_PRI_DISPLAY_TEXT)
4596 case PRI_SUBCMD_DISPLAY_TEXT:
4597 if (event_id != PRI_EVENT_RING) {
4598 /*
4599 * This display text was not from a SETUP message. We can do
4600 * something with this display text string.
4601 */
4602 sig_pri_lock_owner(pri, chanpos);
4603 owner = pri->pvts[chanpos]->owner;
4604 if (owner) {
4605 struct ast_frame f;
4606
4607 /* Pass the display text to the peer channel. */
4608 memset(&f, 0, sizeof(f));
4609 f.frametype = AST_FRAME_TEXT;
4610 f.subclass.integer = 0;
4611 f.offset = 0;
4612 f.data.ptr = (void *)&subcmd->u.display.text;
4613 f.datalen = subcmd->u.display.length + 1;
4614 ast_queue_frame(owner, &f);
4615 ast_channel_unlock(owner);
4616 }
4617 }
4618 break;
4619 #endif /* defined(HAVE_PRI_DISPLAY_TEXT) */
4620 default:
4621 ast_debug(2, "Span %d: Unknown call subcommand(%d) in %s event.\n",
4622 pri->span, subcmd->cmd, pri_event2str(event_id));
4623 break;
4624 }
4625 }
4626 }
4627
4628 /*!
4629 * \internal
4630 * \brief Convert the MOH state to string.
4631 * \since 10.0
4632 *
4633 * \param state MOH state to process.
4634 *
4635 * \return String version of MOH state.
4636 */
4637 static const char *sig_pri_moh_state_str(enum sig_pri_moh_state state)
4638 {
4639 const char *str;
4640
4641 str = "Unknown";
4642 switch (state) {
4643 case SIG_PRI_MOH_STATE_IDLE:
4644 str = "SIG_PRI_MOH_STATE_IDLE";
4645 break;
4646 case SIG_PRI_MOH_STATE_NOTIFY:
4647 str = "SIG_PRI_MOH_STATE_NOTIFY";
4648 break;
4649 case SIG_PRI_MOH_STATE_MOH:
4650 str = "SIG_PRI_MOH_STATE_MOH";
4651 break;
4652 #if defined(HAVE_PRI_CALL_HOLD)
4653 case SIG_PRI_MOH_STATE_HOLD_REQ:
4654 str = "SIG_PRI_MOH_STATE_HOLD_REQ";
4655 break;
4656 case SIG_PRI_MOH_STATE_PEND_UNHOLD:
4657 str = "SIG_PRI_MOH_STATE_PEND_UNHOLD";
4658 break;
4659 case SIG_PRI_MOH_STATE_HOLD:
4660 str = "SIG_PRI_MOH_STATE_HOLD";
4661 break;
4662 case SIG_PRI_MOH_STATE_RETRIEVE_REQ:
4663 str = "SIG_PRI_MOH_STATE_RETRIEVE_REQ";
4664 break;
4665 case SIG_PRI_MOH_STATE_PEND_HOLD:
4666 str = "SIG_PRI_MOH_STATE_PEND_HOLD";
4667 break;
4668 case SIG_PRI_MOH_STATE_RETRIEVE_FAIL:
4669 str = "SIG_PRI_MOH_STATE_RETRIEVE_FAIL";
4670 break;
4671 #endif /* defined(HAVE_PRI_CALL_HOLD) */
4672 case SIG_PRI_MOH_STATE_NUM:
4673 /* Not a real state. */
4674 break;
4675 }
4676 return str;
4677 }
4678
4679 /*!
4680 * \internal
4681 * \brief Convert the MOH event to string.
4682 * \since 10.0
4683 *
4684 * \param event MOH event to process.
4685 *
4686 * \return String version of MOH event.
4687 */
4688 static const char *sig_pri_moh_event_str(enum sig_pri_moh_event event)
4689 {
4690 const char *str;
4691
4692 str = "Unknown";
4693 switch (event) {
4694 case SIG_PRI_MOH_EVENT_RESET:
4695 str = "SIG_PRI_MOH_EVENT_RESET";
4696 break;
4697 case SIG_PRI_MOH_EVENT_HOLD:
4698 str = "SIG_PRI_MOH_EVENT_HOLD";
4699 break;
4700 case SIG_PRI_MOH_EVENT_UNHOLD:
4701 str = "SIG_PRI_MOH_EVENT_UNHOLD";
4702 break;
4703 #if defined(HAVE_PRI_CALL_HOLD)
4704 case SIG_PRI_MOH_EVENT_HOLD_ACK:
4705 str = "SIG_PRI_MOH_EVENT_HOLD_ACK";
4706 break;
4707 case SIG_PRI_MOH_EVENT_HOLD_REJ:
4708 str = "SIG_PRI_MOH_EVENT_HOLD_REJ";
4709 break;
4710 case SIG_PRI_MOH_EVENT_RETRIEVE_ACK:
4711 str = "SIG_PRI_MOH_EVENT_RETRIEVE_ACK";
4712 break;
4713 case SIG_PRI_MOH_EVENT_RETRIEVE_REJ:
4714 str = "SIG_PRI_MOH_EVENT_RETRIEVE_REJ";
4715 break;
4716 case SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK:
4717 str = "SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK";
4718 break;
4719 #endif /* defined(HAVE_PRI_CALL_HOLD) */
4720 case SIG_PRI_MOH_EVENT_NUM:
4721 /* Not a real event. */
4722 break;
4723 }
4724 return str;
4725 }
4726
4727 #if defined(HAVE_PRI_CALL_HOLD)
4728 /*!
4729 * \internal
4730 * \brief Retrieve a call that was placed on hold by the HOLD message.
4731 * \since 10.0
4732 *
4733 * \param pvt Channel private control structure.
4734 *
4735 * \note Assumes the pvt->pri->lock is already obtained.
4736 * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
4737 *
4738 * \return Next MOH state
4739 */
4740 static enum sig_pri_moh_state sig_pri_moh_retrieve_call(struct sig_pri_chan *pvt)
4741 {
4742 int chanpos;
4743 int channel;
4744
4745 if (pvt->pri->nodetype == PRI_NETWORK) {
4746 /* Find an available channel to propose */
4747 chanpos = pri_find_empty_chan(pvt->pri, 1);
4748 if (chanpos < 0) {
4749 /* No channels available. */
4750 return SIG_PRI_MOH_STATE_RETRIEVE_FAIL;
4751 }
4752 channel = PVT_TO_CHANNEL(pvt->pri->pvts[chanpos]);
4753
4754 /*
4755 * We cannot occupy or reserve this channel at this time because
4756 * the retrieve may fail or we could have a RETRIEVE collision.
4757 */
4758 } else {
4759 /* Let the network pick the channel. */
4760 channel = 0;
4761 }
4762
4763 if (pri_retrieve(pvt->pri->pri, pvt->call, channel)) {
4764 return SIG_PRI_MOH_STATE_RETRIEVE_FAIL;
4765 }
4766 return SIG_PRI_MOH_STATE_RETRIEVE_REQ;
4767 }
4768 #endif /* defined(HAVE_PRI_CALL_HOLD) */
4769
4770 /*!
4771 * \internal
4772 * \brief MOH FSM state idle.
4773 * \since 10.0
4774 *
4775 * \param chan Channel to post event to (Usually pvt->owner)
4776 * \param pvt Channel private control structure.
4777 * \param event MOH event to process.
4778 *
4779 * \note Assumes the pvt->pri->lock is already obtained.
4780 * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
4781 *
4782 * \return Next MOH state
4783 */
4784 static enum sig_pri_moh_state sig_pri_moh_fsm_idle(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
4785 {
4786 enum sig_pri_moh_state next_state;
4787
4788 next_state = pvt->moh_state;
4789 switch (event) {
4790 case SIG_PRI_MOH_EVENT_HOLD:
4791 if (!strcasecmp(pvt->mohinterpret, "passthrough")) {
4792 /*
4793 * This config setting is deprecated.
4794 * The old way did not send MOH just in case the notification was ignored.
4795 */
4796 pri_notify(pvt->pri->pri, pvt->call, pvt->prioffset, PRI_NOTIFY_REMOTE_HOLD);
4797 next_state = SIG_PRI_MOH_STATE_NOTIFY;
4798 break;
4799 }
4800
4801 switch (pvt->pri->moh_signaling) {
4802 default:
4803 case SIG_PRI_MOH_SIGNALING_MOH:
4804 ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
4805 next_state = SIG_PRI_MOH_STATE_MOH;
4806 break;
4807 case SIG_PRI_MOH_SIGNALING_NOTIFY:
4808 /* Send MOH anyway in case the far end does not interpret the notification. */
4809 ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
4810
4811 pri_notify(pvt->pri->pri, pvt->call, pvt->prioffset, PRI_NOTIFY_REMOTE_HOLD);
4812 next_state = SIG_PRI_MOH_STATE_NOTIFY;
4813 break;
4814 #if defined(HAVE_PRI_CALL_HOLD)
4815 case SIG_PRI_MOH_SIGNALING_HOLD:
4816 if (pri_hold(pvt->pri->pri, pvt->call)) {
4817 /* Fall back to MOH instead */
4818 ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
4819 next_state = SIG_PRI_MOH_STATE_MOH;
4820 } else {
4821 next_state = SIG_PRI_MOH_STATE_HOLD_REQ;
4822 }
4823 break;
4824 #endif /* defined(HAVE_PRI_CALL_HOLD) */
4825 }
4826 break;
4827 default:
4828 break;
4829 }
4830 pvt->moh_state = next_state;
4831 return next_state;
4832 }
4833
4834 /*!
4835 * \internal
4836 * \brief MOH FSM state notify remote party.
4837 * \since 10.0
4838 *
4839 * \param chan Channel to post event to (Usually pvt->owner)
4840 * \param pvt Channel private control structure.
4841 * \param event MOH event to process.
4842 *
4843 * \note Assumes the pvt->pri->lock is already obtained.
4844 * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
4845 *
4846 * \return Next MOH state
4847 */
4848 static enum sig_pri_moh_state sig_pri_moh_fsm_notify(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
4849 {
4850 enum sig_pri_moh_state next_state;
4851
4852 next_state = pvt->moh_state;
4853 switch (event) {
4854 case SIG_PRI_MOH_EVENT_HOLD:
4855 if (strcasecmp(pvt->mohinterpret, "passthrough")) {
4856 /* Restart MOH in case it was stopped by other means. */
4857 ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
4858 }
4859 break;
4860 case SIG_PRI_MOH_EVENT_UNHOLD:
4861 pri_notify(pvt->pri->pri, pvt->call, pvt->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
4862 /* Fall through */
4863 case SIG_PRI_MOH_EVENT_RESET:
4864 ast_moh_stop(chan);
4865 next_state = SIG_PRI_MOH_STATE_IDLE;
4866 break;
4867 default:
4868 break;
4869 }
4870 pvt->moh_state = next_state;
4871 return next_state;
4872 }
4873
4874 /*!
4875 * \internal
4876 * \brief MOH FSM state generate moh.
4877 * \since 10.0
4878 *
4879 * \param chan Channel to post event to (Usually pvt->owner)
4880 * \param pvt Channel private control structure.
4881 * \param event MOH event to process.
4882 *
4883 * \note Assumes the pvt->pri->lock is already obtained.
4884 * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
4885 *
4886 * \return Next MOH state
4887 */
4888 static enum sig_pri_moh_state sig_pri_moh_fsm_moh(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
4889 {
4890 enum sig_pri_moh_state next_state;
4891
4892 next_state = pvt->moh_state;
4893 switch (event) {
4894 case SIG_PRI_MOH_EVENT_HOLD:
4895 /* Restart MOH in case it was stopped by other means. */
4896 ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
4897 break;
4898 case SIG_PRI_MOH_EVENT_RESET:
4899 case SIG_PRI_MOH_EVENT_UNHOLD:
4900 ast_moh_stop(chan);
4901 next_state = SIG_PRI_MOH_STATE_IDLE;
4902 break;
4903 default:
4904 break;
4905 }
4906 pvt->moh_state = next_state;
4907 return next_state;
4908 }
4909
4910 #if defined(HAVE_PRI_CALL_HOLD)
4911 /*!
4912 * \internal
4913 * \brief MOH FSM state hold requested.
4914 * \since 10.0
4915 *
4916 * \param chan Channel to post event to (Usually pvt->owner)
4917 * \param pvt Channel private control structure.
4918 * \param event MOH event to process.
4919 *
4920 * \note Assumes the pvt->pri->lock is already obtained.
4921 * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
4922 *
4923 * \return Next MOH state
4924 */
4925 static enum sig_pri_moh_state sig_pri_moh_fsm_hold_req(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
4926 {
4927 enum sig_pri_moh_state next_state;
4928
4929 next_state = pvt->moh_state;
4930 switch (event) {
4931 case SIG_PRI_MOH_EVENT_RESET:
4932 next_state = SIG_PRI_MOH_STATE_IDLE;
4933 break;
4934 case SIG_PRI_MOH_EVENT_UNHOLD:
4935 next_state = SIG_PRI_MOH_STATE_PEND_UNHOLD;
4936 break;
4937 case SIG_PRI_MOH_EVENT_HOLD_REJ:
4938 /* Fall back to MOH */
4939 if (chan) {
4940 ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
4941 }
4942 next_state = SIG_PRI_MOH_STATE_MOH;
4943 break;
4944 case SIG_PRI_MOH_EVENT_HOLD_ACK:
4945 next_state = SIG_PRI_MOH_STATE_HOLD;
4946 break;
4947 default:
4948 break;
4949 }
4950 pvt->moh_state = next_state;
4951 return next_state;
4952 }
4953 #endif /* defined(HAVE_PRI_CALL_HOLD) */
4954
4955 #if defined(HAVE_PRI_CALL_HOLD)
4956 /*!
4957 * \internal
4958 * \brief MOH FSM state hold requested with pending unhold.
4959 * \since 10.0
4960 *
4961 * \param chan Channel to post event to (Usually pvt->owner)
4962 * \param pvt Channel private control structure.
4963 * \param event MOH event to process.
4964 *
4965 * \note Assumes the pvt->pri->lock is already obtained.
4966 * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
4967 *
4968 * \return Next MOH state
4969 */
4970 static enum sig_pri_moh_state sig_pri_moh_fsm_pend_unhold(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
4971 {
4972 enum sig_pri_moh_state next_state;
4973
4974 next_state = pvt->moh_state;
4975 switch (event) {
4976 case SIG_PRI_MOH_EVENT_RESET:
4977 next_state = SIG_PRI_MOH_STATE_IDLE;
4978 break;
4979 case SIG_PRI_MOH_EVENT_HOLD:
4980 next_state = SIG_PRI_MOH_STATE_HOLD_REQ;
4981 break;
4982 case SIG_PRI_MOH_EVENT_HOLD_REJ:
4983 next_state = SIG_PRI_MOH_STATE_IDLE;
4984 break;
4985 case SIG_PRI_MOH_EVENT_HOLD_ACK:
4986 next_state = sig_pri_moh_retrieve_call(pvt);
4987 break;
4988 default:
4989 break;
4990 }
4991 pvt->moh_state = next_state;
4992 return next_state;
4993 }
4994 #endif /* defined(HAVE_PRI_CALL_HOLD) */
4995
4996 #if defined(HAVE_PRI_CALL_HOLD)
4997 /*!
4998 * \internal
4999 * \brief MOH FSM state hold.
5000 * \since 10.0
5001 *
5002 * \param chan Channel to post event to (Usually pvt->owner)
5003 * \param pvt Channel private control structure.
5004 * \param event MOH event to process.
5005 *
5006 * \note Assumes the pvt->pri->lock is already obtained.
5007 * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
5008 *
5009 * \return Next MOH state
5010 */
5011 static enum sig_pri_moh_state sig_pri_moh_fsm_hold(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
5012 {
5013 enum sig_pri_moh_state next_state;
5014
5015 next_state = pvt->moh_state;
5016 switch (event) {
5017 case SIG_PRI_MOH_EVENT_RESET:
5018 next_state = SIG_PRI_MOH_STATE_IDLE;
5019 break;
5020 case SIG_PRI_MOH_EVENT_UNHOLD:
5021 next_state = sig_pri_moh_retrieve_call(pvt);
5022 break;
5023 case SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK:
5024 /* Fall back to MOH */
5025 if (chan) {
5026 ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
5027 }
5028 next_state = SIG_PRI_MOH_STATE_MOH;
5029 break;
5030 default:
5031 break;
5032 }
5033 pvt->moh_state = next_state;
5034 return next_state;
5035 }
5036 #endif /* defined(HAVE_PRI_CALL_HOLD) */
5037
5038 #if defined(HAVE_PRI_CALL_HOLD)
5039 /*!
5040 * \internal
5041 * \brief MOH FSM state retrieve requested.
5042 * \since 10.0
5043 *
5044 * \param chan Channel to post event to (Usually pvt->owner)
5045 * \param pvt Channel private control structure.
5046 * \param event MOH event to process.
5047 *
5048 * \note Assumes the pvt->pri->lock is already obtained.
5049 * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
5050 *
5051 * \return Next MOH state
5052 */
5053 static enum sig_pri_moh_state sig_pri_moh_fsm_retrieve_req(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
5054 {
5055 enum sig_pri_moh_state next_state;
5056
5057 next_state = pvt->moh_state;
5058 switch (event) {
5059 case SIG_PRI_MOH_EVENT_RESET:
5060 next_state = SIG_PRI_MOH_STATE_IDLE;
5061 break;
5062 case SIG_PRI_MOH_EVENT_HOLD:
5063 next_state = SIG_PRI_MOH_STATE_PEND_HOLD;
5064 break;
5065 case SIG_PRI_MOH_EVENT_RETRIEVE_ACK:
5066 case SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK:
5067 next_state = SIG_PRI_MOH_STATE_IDLE;
5068 break;
5069 case SIG_PRI_MOH_EVENT_RETRIEVE_REJ:
5070 next_state = SIG_PRI_MOH_STATE_RETRIEVE_FAIL;
5071 break;
5072 default:
5073 break;
5074 }
5075 pvt->moh_state = next_state;
5076 return next_state;
5077 }
5078 #endif /* defined(HAVE_PRI_CALL_HOLD) */
5079
5080 #if defined(HAVE_PRI_CALL_HOLD)
5081 /*!
5082 * \internal
5083 * \brief MOH FSM state retrieve requested with pending hold.
5084 * \since 10.0
5085 *
5086 * \param chan Channel to post event to (Usually pvt->owner)
5087 * \param pvt Channel private control structure.
5088 * \param event MOH event to process.
5089 *
5090 * \note Assumes the pvt->pri->lock is already obtained.
5091 * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
5092 *
5093 * \return Next MOH state
5094 */
5095 static enum sig_pri_moh_state sig_pri_moh_fsm_pend_hold(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
5096 {
5097 enum sig_pri_moh_state next_state;
5098
5099 next_state = pvt->moh_state;
5100 switch (event) {
5101 case SIG_PRI_MOH_EVENT_RESET:
5102 next_state = SIG_PRI_MOH_STATE_IDLE;
5103 break;
5104 case SIG_PRI_MOH_EVENT_UNHOLD:
5105 next_state = SIG_PRI_MOH_STATE_RETRIEVE_REQ;
5106 break;
5107 case SIG_PRI_MOH_EVENT_RETRIEVE_ACK:
5108 case SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK:
5109 /*
5110 * Successfully came off of hold. Now we can reinterpret the
5111 * MOH signaling option to handle the pending HOLD request.
5112 */
5113 switch (pvt->pri->moh_signaling) {
5114 default:
5115 case SIG_PRI_MOH_SIGNALING_MOH:
5116 if (chan) {
5117 ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
5118 }
5119 next_state = SIG_PRI_MOH_STATE_MOH;
5120 break;
5121 case SIG_PRI_MOH_SIGNALING_NOTIFY:
5122 /* Send MOH anyway in case the far end does not interpret the notification. */
5123 if (chan) {
5124 ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
5125 }
5126
5127 pri_notify(pvt->pri->pri, pvt->call, pvt->prioffset, PRI_NOTIFY_REMOTE_HOLD);
5128 next_state = SIG_PRI_MOH_STATE_NOTIFY;
5129 break;
5130 case SIG_PRI_MOH_SIGNALING_HOLD:
5131 if (pri_hold(pvt->pri->pri, pvt->call)) {
5132 /* Fall back to MOH instead */
5133 if (chan) {
5134 ast_moh_start(chan, pvt->moh_suggested, pvt->mohinterpret);
5135 }
5136 next_state = SIG_PRI_MOH_STATE_MOH;
5137 } else {
5138 next_state = SIG_PRI_MOH_STATE_HOLD_REQ;
5139 }
5140 break;
5141 }
5142 break;
5143 case SIG_PRI_MOH_EVENT_RETRIEVE_REJ:
5144 /*
5145 * We cannot reinterpret the MOH signaling option because we
5146 * failed to come off of hold.
5147 */
5148 next_state = SIG_PRI_MOH_STATE_HOLD;
5149 break;
5150 default:
5151 break;
5152 }
5153 pvt->moh_state = next_state;
5154 return next_state;
5155 }
5156 #endif /* defined(HAVE_PRI_CALL_HOLD) */
5157
5158 #if defined(HAVE_PRI_CALL_HOLD)
5159 /*!
5160 * \internal
5161 * \brief MOH FSM state retrieve failed.
5162 * \since 10.0
5163 *
5164 * \param chan Channel to post event to (Usually pvt->owner)
5165 * \param pvt Channel private control structure.
5166 * \param event MOH event to process.
5167 *
5168 * \note Assumes the pvt->pri->lock is already obtained.
5169 * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
5170 *
5171 * \return Next MOH state
5172 */
5173 static enum sig_pri_moh_state sig_pri_moh_fsm_retrieve_fail(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
5174 {
5175 enum sig_pri_moh_state next_state;
5176
5177 next_state = pvt->moh_state;
5178 switch (event) {
5179 case SIG_PRI_MOH_EVENT_RESET:
5180 next_state = SIG_PRI_MOH_STATE_IDLE;
5181 break;
5182 case SIG_PRI_MOH_EVENT_HOLD:
5183 next_state = SIG_PRI_MOH_STATE_HOLD;
5184 break;
5185 case SIG_PRI_MOH_EVENT_UNHOLD:
5186 next_state = sig_pri_moh_retrieve_call(pvt);
5187 break;
5188 case SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK:
5189 next_state = SIG_PRI_MOH_STATE_IDLE;
5190 break;
5191 default:
5192 break;
5193 }
5194 pvt->moh_state = next_state;
5195 return next_state;
5196 }
5197 #endif /* defined(HAVE_PRI_CALL_HOLD) */
5198
5199 /*!
5200 * \internal
5201 * \brief MOH FSM state function type.
5202 * \since 10.0
5203 *
5204 * \param chan Channel to post event to (Usually pvt->owner)
5205 * \param pvt Channel private control structure.
5206 * \param event MOH event to process.
5207 *
5208 * \note Assumes the pvt->pri->lock is already obtained.
5209 * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
5210 *
5211 * \return Next MOH state
5212 */
5213 typedef enum sig_pri_moh_state (*sig_pri_moh_fsm_state)(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event);
5214
5215 /*! MOH FSM state table. */
5216 static const sig_pri_moh_fsm_state sig_pri_moh_fsm[SIG_PRI_MOH_STATE_NUM] = {
5217 /* *INDENT-OFF* */
5218 [SIG_PRI_MOH_STATE_IDLE] = sig_pri_moh_fsm_idle,
5219 [SIG_PRI_MOH_STATE_NOTIFY] = sig_pri_moh_fsm_notify,
5220 [SIG_PRI_MOH_STATE_MOH] = sig_pri_moh_fsm_moh,
5221 #if defined(HAVE_PRI_CALL_HOLD)
5222 [SIG_PRI_MOH_STATE_HOLD_REQ] = sig_pri_moh_fsm_hold_req,
5223 [SIG_PRI_MOH_STATE_PEND_UNHOLD] = sig_pri_moh_fsm_pend_unhold,
5224 [SIG_PRI_MOH_STATE_HOLD] = sig_pri_moh_fsm_hold,
5225 [SIG_PRI_MOH_STATE_RETRIEVE_REQ] = sig_pri_moh_fsm_retrieve_req,
5226 [SIG_PRI_MOH_STATE_PEND_HOLD] = sig_pri_moh_fsm_pend_hold,
5227 [SIG_PRI_MOH_STATE_RETRIEVE_FAIL] = sig_pri_moh_fsm_retrieve_fail,
5228 #endif /* defined(HAVE_PRI_CALL_HOLD) */
5229 /* *INDENT-ON* */
5230 };
5231
5232 /*!
5233 * \internal
5234 * \brief Send an event to the MOH FSM.
5235 * \since 10.0
5236 *
5237 * \param chan Channel to post event to (Usually pvt->owner)
5238 * \param pvt Channel private control structure.
5239 * \param event MOH event to process.
5240 *
5241 * \note Assumes the pvt->pri->lock is already obtained.
5242 * \note Assumes the sig_pri_lock_private(pvt) is already obtained.
5243 */
5244 static void sig_pri_moh_fsm_event(struct ast_channel *chan, struct sig_pri_chan *pvt, enum sig_pri_moh_event event)
5245 {
5246 enum sig_pri_moh_state orig_state;
5247 enum sig_pri_moh_state next_state;
5248 const char *chan_name;
5249
5250 if (chan) {
5251 chan_name = ast_strdupa(ast_channel_name(chan));
5252 } else {
5253 chan_name = "Unknown";
5254 }
5255 orig_state = pvt->moh_state;
5256 ast_debug(2, "Channel '%s' MOH-Event: %s in state %s\n", chan_name,
5257 sig_pri_moh_event_str(event), sig_pri_moh_state_str(orig_state));
5258 if (orig_state < SIG_PRI_MOH_STATE_IDLE || SIG_PRI_MOH_STATE_NUM <= orig_state
5259 || !sig_pri_moh_fsm[orig_state]) {
5260 /* Programming error: State not implemented. */
5261 ast_log(LOG_ERROR, "MOH state not implemented: %s(%u)\n",
5262 sig_pri_moh_state_str(orig_state), orig_state);
5263 return;
5264 }
5265 /* Execute the state. */
5266 next_state = sig_pri_moh_fsm[orig_state](chan, pvt, event);
5267 ast_debug(2, "Channel '%s' MOH-Next-State: %s\n", chan_name,
5268 (orig_state == next_state) ? "$" : sig_pri_moh_state_str(next_state));
5269 }
5270
5271 /*!
5272 * \internal
5273 * \brief Set callid threadstorage for the pri_dchannel thread when a new call is created
5274 *
5275 * \return A new callid which has been bound to threadstorage. The threadstorage
5276 * should be unbound when the pri_dchannel primary loop wraps.
5277 */
5278 static ast_callid func_pri_dchannel_new_callid(void)
5279 {
5280 ast_callid callid = ast_create_callid();
5281
5282 if (callid) {
5283 ast_callid_threadassoc_add(callid);
5284 }
5285
5286 return callid;
5287 }
5288
5289 /*!
5290 * \internal
5291 * \brief Set callid threadstorage for the pri_dchannel thread to that of an existing channel
5292 *
5293 * \param pri PRI span control structure.
5294 * \param chanpos channel position in the span
5295 *
5296 * \note Assumes the pri->lock is already obtained.
5297 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
5298 *
5299 * \return The callid which has also been bound to threadstorage if it exists.
5300 * The threadstorage should be unbound when the pri_dchannel primary loop wraps.
5301 */
5302 static ast_callid func_pri_dchannel_chanpos_callid(struct sig_pri_span *pri, int chanpos)
5303 {
5304 if (chanpos < 0) {
5305 return 0;
5306 }
5307
5308 sig_pri_lock_owner(pri, chanpos);
5309 if (pri->pvts[chanpos]->owner) {
5310 ast_callid callid;
5311 callid = ast_channel_callid(pri->pvts[chanpos]->owner);
5312 ast_channel_unlock(pri->pvts[chanpos]->owner);
5313 if (callid) {
5314 ast_callid_threadassoc_add(callid);
5315 return callid;
5316 }
5317 }
5318
5319 return 0;
5320 }
5321
5322 #if defined(HAVE_PRI_CALL_HOLD)
5323 /*!
5324 * \internal
5325 * \brief Handle the hold event from libpri.
5326 * \since 1.8
5327 *
5328 * \param pri PRI span control structure.
5329 * \param ev Hold event received.
5330 *
5331 * \note Assumes the pri->lock is already obtained.
5332 *
5333 * \retval 0 on success.
5334 * \retval -1 on error.
5335 */
5336 static int sig_pri_handle_hold(struct sig_pri_span *pri, pri_event *ev)
5337 {
5338 int retval;
5339 int chanpos_old;
5340 int chanpos_new;
5341 struct ast_channel *owner;
5342 ast_callid callid = 0;
5343
5344 chanpos_old = pri_find_principle_by_call(pri, ev->hold.call);
5345 if (chanpos_old < 0) {
5346 ast_log(LOG_WARNING, "Span %d: Received HOLD for unknown call.\n", pri->span);
5347 return -1;
5348 }
5349 if (pri->pvts[chanpos_old]->no_b_channel) {
5350 /* Call is already on hold or is call waiting call. */
5351 return -1;
5352 }
5353
5354 chanpos_new = -1;
5355
5356 sig_pri_lock_private(pri->pvts[chanpos_old]);
5357 sig_pri_lock_owner(pri, chanpos_old);
5358 owner = pri->pvts[chanpos_old]->owner;
5359 if (!owner) {
5360 goto done_with_private;
5361 }
5362
5363 callid = ast_channel_callid(owner);
5364
5365 if (callid) {
5366 ast_callid_threadassoc_add(callid);
5367 }
5368
5369 if (pri->pvts[chanpos_old]->call_level != SIG_PRI_CALL_LEVEL_CONNECT) {
5370 /*
5371 * Make things simple. Don't allow placing a call on hold that
5372 * is not connected.
5373 */
5374 goto done_with_owner;
5375 }
5376 chanpos_new = pri_find_empty_nobch(pri);
5377 if (chanpos_new < 0) {
5378 /* No hold channel available. */
5379 goto done_with_owner;
5380 }
5381 sig_pri_handle_subcmds(pri, chanpos_old, ev->e, ev->hold.subcmds, ev->hold.call);
5382 sig_pri_queue_hold(pri, chanpos_old);
5383 chanpos_new = pri_fixup_principle(pri, chanpos_new, ev->hold.call);
5384 if (chanpos_new < 0) {
5385 /* Should never happen. */
5386 sig_pri_queue_unhold(pri, chanpos_old);
5387 }
5388
5389 done_with_owner:;
5390 ast_channel_unlock(owner);
5391 done_with_private:;
5392 sig_pri_unlock_private(pri->pvts[chanpos_old]);
5393
5394 if (chanpos_new < 0) {
5395 retval = -1;
5396 } else {
5397 sig_pri_span_devstate_changed(pri);
5398 retval = 0;
5399 }
5400
5401 if (callid) {
5402 ast_callid_threadassoc_remove();
5403 }
5404
5405 return retval;
5406 }
5407 #endif /* defined(HAVE_PRI_CALL_HOLD) */
5408
5409 #if defined(HAVE_PRI_CALL_HOLD)
5410 /*!
5411 * \internal
5412 * \brief Handle the hold acknowledge event from libpri.
5413 * \since 10.0
5414 *
5415 * \param pri PRI span control structure.
5416 * \param ev Hold acknowledge event received.
5417 *
5418 * \note Assumes the pri->lock is already obtained.
5419 */
5420 static void sig_pri_handle_hold_ack(struct sig_pri_span *pri, pri_event *ev)
5421 {
5422 int chanpos;
5423 ast_callid callid;
5424
5425 /*
5426 * We were successfully put on hold by the remote party
5427 * so we just need to switch to a no_b_channel channel.
5428 */
5429 chanpos = pri_find_empty_nobch(pri);
5430 if (chanpos < 0) {
5431 /* Very bad news. No hold channel available. */
5432 ast_log(LOG_ERROR,
5433 "Span %d: No hold channel available for held call that is on %d/%d\n",
5434 pri->span, PRI_SPAN(ev->hold_ack.channel), PRI_CHANNEL(ev->hold_ack.channel));
5435 sig_pri_kill_call(pri, ev->hold_ack.call, PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED);
5436 return;
5437 }
5438 chanpos = pri_fixup_principle(pri, chanpos, ev->hold_ack.call);
5439 if (chanpos < 0) {
5440 /* Should never happen. */
5441 sig_pri_kill_call(pri, ev->hold_ack.call, PRI_CAUSE_NORMAL_TEMPORARY_FAILURE);
5442 return;
5443 }
5444
5445 sig_pri_lock_private(pri->pvts[chanpos]);
5446 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
5447
5448 sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->hold_ack.subcmds, ev->hold_ack.call);
5449 sig_pri_moh_fsm_event(pri->pvts[chanpos]->owner, pri->pvts[chanpos],
5450 SIG_PRI_MOH_EVENT_HOLD_ACK);
5451 sig_pri_unlock_private(pri->pvts[chanpos]);
5452 sig_pri_span_devstate_changed(pri);
5453
5454 if (callid) {
5455 ast_callid_threadassoc_remove();
5456 }
5457 }
5458 #endif /* defined(HAVE_PRI_CALL_HOLD) */
5459
5460 #if defined(HAVE_PRI_CALL_HOLD)
5461 /*!
5462 * \internal
5463 * \brief Handle the hold reject event from libpri.
5464 * \since 10.0
5465 *
5466 * \param pri PRI span control structure.
5467 * \param ev Hold reject event received.
5468 *
5469 * \note Assumes the pri->lock is already obtained.
5470 */
5471 static void sig_pri_handle_hold_rej(struct sig_pri_span *pri, pri_event *ev)
5472 {
5473 int chanpos;
5474 ast_callid callid;
5475
5476 chanpos = pri_find_principle(pri, ev->hold_rej.channel, ev->hold_rej.call);
5477 if (chanpos < 0) {
5478 ast_log(LOG_WARNING, "Span %d: Could not find principle for HOLD_REJECT\n",
5479 pri->span);
5480 sig_pri_kill_call(pri, ev->hold_rej.call, PRI_CAUSE_NORMAL_TEMPORARY_FAILURE);
5481 return;
5482 }
5483 chanpos = pri_fixup_principle(pri, chanpos, ev->hold_rej.call);
5484 if (chanpos < 0) {
5485 /* Should never happen. */
5486 sig_pri_kill_call(pri, ev->hold_rej.call, PRI_CAUSE_NORMAL_TEMPORARY_FAILURE);
5487 return;
5488 }
5489
5490 ast_debug(1, "Span %d: HOLD_REJECT cause: %d(%s)\n", pri->span,
5491 ev->hold_rej.cause, pri_cause2str(ev->hold_rej.cause));
5492
5493 sig_pri_lock_private(pri->pvts[chanpos]);
5494 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
5495
5496 sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->hold_rej.subcmds, ev->hold_rej.call);
5497 sig_pri_moh_fsm_event(pri->pvts[chanpos]->owner, pri->pvts[chanpos],
5498 SIG_PRI_MOH_EVENT_HOLD_REJ);
5499 sig_pri_unlock_private(pri->pvts[chanpos]);
5500
5501 if (callid) {
5502 ast_callid_threadassoc_remove();
5503 }
5504 }
5505 #endif /* defined(HAVE_PRI_CALL_HOLD) */
5506
5507 #if defined(HAVE_PRI_CALL_HOLD)
5508 /*!
5509 * \internal
5510 * \brief Handle the retrieve event from libpri.
5511 * \since 1.8
5512 *
5513 * \param pri PRI span control structure.
5514 * \param ev Retrieve event received.
5515 *
5516 * \note Assumes the pri->lock is already obtained.
5517 */
5518 static void sig_pri_handle_retrieve(struct sig_pri_span *pri, pri_event *ev)
5519 {
5520 int chanpos;
5521 ast_callid callid;
5522
5523 if (!(ev->retrieve.channel & PRI_HELD_CALL)) {
5524 /* The call is not currently held. */
5525 pri_retrieve_rej(pri->pri, ev->retrieve.call,
5526 PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED);
5527 return;
5528 }
5529 if (pri_find_principle_by_call(pri, ev->retrieve.call) < 0) {
5530 ast_log(LOG_WARNING, "Span %d: Received RETRIEVE for unknown call.\n", pri->span);
5531 pri_retrieve_rej(pri->pri, ev->retrieve.call,
5532 PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED);
5533 return;
5534 }
5535 if (PRI_CHANNEL(ev->retrieve.channel) == 0xFF) {
5536 chanpos = pri_find_empty_chan(pri, 1);
5537 } else {
5538 chanpos = pri_find_principle(pri,
5539 ev->retrieve.channel & ~PRI_HELD_CALL, ev->retrieve.call);
5540 if (ev->retrieve.flexible
5541 && (chanpos < 0 || !sig_pri_is_chan_available(pri->pvts[chanpos]))) {
5542 /*
5543 * Channel selection is flexible and the requested channel
5544 * is bad or not available. Pick another channel.
5545 */
5546 chanpos = pri_find_empty_chan(pri, 1);
5547 }
5548 }
5549 if (chanpos < 0) {
5550 pri_retrieve_rej(pri->pri, ev->retrieve.call,
5551 ev->retrieve.flexible ? PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION
5552 : PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
5553 return;
5554 }
5555 chanpos = pri_fixup_principle(pri, chanpos, ev->retrieve.call);
5556 if (chanpos < 0) {
5557 /* Channel is already in use. */
5558 pri_retrieve_rej(pri->pri, ev->retrieve.call,
5559 PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
5560 return;
5561 }
5562 sig_pri_lock_private(pri->pvts[chanpos]);
5563 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
5564 sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->retrieve.subcmds, ev->retrieve.call);
5565 sig_pri_queue_unhold(pri, chanpos);
5566 pri_retrieve_ack(pri->pri, ev->retrieve.call,
5567 PVT_TO_CHANNEL(pri->pvts[chanpos]));
5568 sig_pri_moh_fsm_event(pri->pvts[chanpos]->owner, pri->pvts[chanpos],
5569 SIG_PRI_MOH_EVENT_REMOTE_RETRIEVE_ACK);
5570 sig_pri_unlock_private(pri->pvts[chanpos]);
5571 sig_pri_span_devstate_changed(pri);
5572
5573 if (callid) {
5574 ast_callid_threadassoc_remove();
5575 }
5576 }
5577 #endif /* defined(HAVE_PRI_CALL_HOLD) */
5578
5579 #if defined(HAVE_PRI_CALL_HOLD)
5580 /*!
5581 * \internal
5582 * \brief Handle the retrieve acknowledge event from libpri.
5583 * \since 10.0
5584 *
5585 * \param pri PRI span control structure.
5586 * \param ev Retrieve acknowledge event received.
5587 *
5588 * \note Assumes the pri->lock is already obtained.
5589 */
5590 static void sig_pri_handle_retrieve_ack(struct sig_pri_span *pri, pri_event *ev)
5591 {
5592 int chanpos;
5593 ast_callid callid;
5594
5595 chanpos = pri_find_fixup_principle(pri, ev->retrieve_ack.channel,
5596 ev->retrieve_ack.call);
5597 if (chanpos < 0) {
5598 return;
5599 }
5600
5601 sig_pri_lock_private(pri->pvts[chanpos]);
5602 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
5603
5604 sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->retrieve_ack.subcmds,
5605 ev->retrieve_ack.call);
5606 sig_pri_moh_fsm_event(pri->pvts[chanpos]->owner, pri->pvts[chanpos],
5607 SIG_PRI_MOH_EVENT_RETRIEVE_ACK);
5608 sig_pri_unlock_private(pri->pvts[chanpos]);
5609 sig_pri_span_devstate_changed(pri);
5610
5611 if (callid) {
5612 ast_callid_threadassoc_remove();
5613 }
5614 }
5615 #endif /* defined(HAVE_PRI_CALL_HOLD) */
5616
5617 #if defined(HAVE_PRI_CALL_HOLD)
5618 /*!
5619 * \internal
5620 * \brief Handle the retrieve reject event from libpri.
5621 * \since 10.0
5622 *
5623 * \param pri PRI span control structure.
5624 * \param ev Retrieve reject event received.
5625 *
5626 * \note Assumes the pri->lock is already obtained.
5627 */
5628 static void sig_pri_handle_retrieve_rej(struct sig_pri_span *pri, pri_event *ev)
5629 {
5630 int chanpos;
5631 ast_callid callid;
5632
5633 chanpos = pri_find_principle(pri, ev->retrieve_rej.channel, ev->retrieve_rej.call);
5634 if (chanpos < 0) {
5635 ast_log(LOG_WARNING, "Span %d: Could not find principle for RETRIEVE_REJECT\n",
5636 pri->span);
5637 sig_pri_kill_call(pri, ev->retrieve_rej.call, PRI_CAUSE_NORMAL_TEMPORARY_FAILURE);
5638 return;
5639 }
5640 chanpos = pri_fixup_principle(pri, chanpos, ev->retrieve_rej.call);
5641 if (chanpos < 0) {
5642 /* Should never happen. */
5643 sig_pri_kill_call(pri, ev->retrieve_rej.call, PRI_CAUSE_NORMAL_TEMPORARY_FAILURE);
5644 return;
5645 }
5646
5647 ast_debug(1, "Span %d: RETRIEVE_REJECT cause: %d(%s)\n", pri->span,
5648 ev->retrieve_rej.cause, pri_cause2str(ev->retrieve_rej.cause));
5649
5650 sig_pri_lock_private(pri->pvts[chanpos]);
5651 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
5652
5653 sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->retrieve_rej.subcmds,
5654 ev->retrieve_rej.call);
5655 sig_pri_moh_fsm_event(pri->pvts[chanpos]->owner, pri->pvts[chanpos],
5656 SIG_PRI_MOH_EVENT_RETRIEVE_REJ);
5657 sig_pri_unlock_private(pri->pvts[chanpos]);
5658
5659 if (callid) {
5660 ast_callid_threadassoc_remove();
5661 }
5662 }
5663 #endif /* defined(HAVE_PRI_CALL_HOLD) */
5664
5665 /*!
5666 * \internal
5667 * \brief Setup channel variables on the owner.
5668 *
5669 * \param pri PRI span control structure.
5670 * \param chanpos Channel position in the span.
5671 * \param ev SETUP event received.
5672 *
5673 * \note Assumes the pri->lock is already obtained.
5674 * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
5675 */
5676 static void setup_incoming_channel(struct sig_pri_span *pri, int chanpos, pri_event *ev)
5677 {
5678 struct ast_channel *owner;
5679 char ani2str[6];
5680 char calledtonstr[10];
5681
5682 sig_pri_lock_owner(pri, chanpos);
5683 owner = pri->pvts[chanpos]->owner;
5684 if (!owner) {
5685 return;
5686 }
5687
5688 ast_channel_stage_snapshot(owner);
5689
5690 #if defined(HAVE_PRI_SUBADDR)
5691 if (ev->ring.calling.subaddress.valid) {
5692 /* Set Calling Subaddress */
5693 sig_pri_set_subaddress(&ast_channel_caller(owner)->id.subaddress,
5694 &ev->ring.calling.subaddress);
5695 if (!ev->ring.calling.subaddress.type
5696 && !ast_strlen_zero((char *) ev->ring.calling.subaddress.data)) {
5697 /* NSAP */
5698 pbx_builtin_setvar_helper(owner, "CALLINGSUBADDR",
5699 (char *) ev->ring.calling.subaddress.data);
5700 }
5701 }
5702 if (ev->ring.called_subaddress.valid) {
5703 /* Set Called Subaddress */
5704 sig_pri_set_subaddress(&ast_channel_dialed(owner)->subaddress,
5705 &ev->ring.called_subaddress);
5706 if (!ev->ring.called_subaddress.type
5707 && !ast_strlen_zero((char *) ev->ring.called_subaddress.data)) {
5708 /* NSAP */
5709 pbx_builtin_setvar_helper(owner, "CALLEDSUBADDR",
5710 (char *) ev->ring.called_subaddress.data);
5711 }
5712 }
5713 #else
5714 if (!ast_strlen_zero(ev->ring.callingsubaddr)) {
5715 pbx_builtin_setvar_helper(owner, "CALLINGSUBADDR", ev->ring.callingsubaddr);
5716 }
5717 #endif /* !defined(HAVE_PRI_SUBADDR) */
5718 if (ev->ring.ani2 >= 0) {
5719 ast_channel_caller(owner)->ani2 = ev->ring.ani2;
5720 snprintf(ani2str, sizeof(ani2str), "%d", ev->ring.ani2);
5721 pbx_builtin_setvar_helper(owner, "ANI2", ani2str);
5722 }
5723
5724 #ifdef SUPPORT_USERUSER
5725 if (!ast_strlen_zero(ev->ring.useruserinfo)) {
5726 pbx_builtin_setvar_helper(owner, "USERUSERINFO", ev->ring.useruserinfo);
5727 }
5728 #endif
5729
5730 snprintf(calledtonstr, sizeof(calledtonstr), "%d", ev->ring.calledplan);
5731 pbx_builtin_setvar_helper(owner, "CALLEDTON", calledtonstr);
5732 ast_channel_dialed(owner)->number.plan = ev->ring.calledplan;
5733
5734 if (ev->ring.redirectingreason >= 0) {
5735 /* This is now just a status variable. Use REDIRECTING() dialplan function. */
5736 pbx_builtin_setvar_helper(owner, "PRIREDIRECTREASON",
5737 redirectingreason2str(ev->ring.redirectingreason));
5738 }
5739 #if defined(HAVE_PRI_REVERSE_CHARGE)
5740 pri->pvts[chanpos]->reverse_charging_indication = ev->ring.reversecharge;
5741 #endif
5742 #if defined(HAVE_PRI_SETUP_KEYPAD)
5743 ast_copy_string(pri->pvts[chanpos]->keypad_digits,
5744 ev->ring.keypad_digits, sizeof(pri->pvts[chanpos]->keypad_digits));
5745 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
5746
5747 /*
5748 * It's ok to call this with the owner already locked here
5749 * since it will want to do this anyway if there are any
5750 * subcmds.
5751 */
5752 sig_pri_handle_subcmds(pri, chanpos, ev->e, ev->ring.subcmds,
5753 ev->ring.call);
5754
5755 ast_channel_stage_snapshot_done(owner);
5756 ast_channel_unlock(owner);
5757 }
5758
5759 /*!
5760 * \internal
5761 * \brief Handle the incoming SETUP event from libpri.
5762 *
5763 * \param pri PRI span control structure.
5764 * \param e SETUP event received.
5765 *
5766 * \note Assumes the pri->lock is already obtained.
5767 */
5768 static void sig_pri_handle_setup(struct sig_pri_span *pri, pri_event *e)
5769 {
5770 int exten_exists_or_can_exist;
5771 int could_match_more;
5772 int need_dialtone;
5773 enum sig_pri_law law;
5774 int chanpos = -1;
5775 ast_callid callid = 0;
5776 struct ast_channel *c;
5777 char plancallingnum[AST_MAX_EXTENSION];
5778 char plancallingani[AST_MAX_EXTENSION];
5779 pthread_t threadid;
5780
5781 if (!ast_strlen_zero(pri->msn_list)
5782 && !sig_pri_msn_match(pri->msn_list, e->ring.callednum)) {
5783 /* The call is not for us so ignore it. */
5784 ast_verb(3,
5785 "Ignoring call to '%s' on span %d. Its not in the MSN list: %s\n",
5786 e->ring.callednum, pri->span, pri->msn_list);
5787 pri_destroycall(pri->pri, e->ring.call);
5788 goto setup_exit;
5789 }
5790 if (sig_pri_is_cis_call(e->ring.channel)) {
5791 sig_pri_handle_cis_subcmds(pri, e->e, e->ring.subcmds, e->ring.call);
5792 goto setup_exit;
5793 }
5794 chanpos = pri_find_principle_by_call(pri, e->ring.call);
5795 if (-1 < chanpos) {
5796 /* Libpri has already filtered out duplicate SETUPs. */
5797 ast_log(LOG_WARNING,
5798 "Span %d: Got SETUP with duplicate call ptr (%p). Dropping call.\n",
5799 pri->span, e->ring.call);
5800 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_TEMPORARY_FAILURE);
5801 goto setup_exit;
5802 }
5803 if (e->ring.channel == -1 || PRI_CHANNEL(e->ring.channel) == 0xFF) {
5804 /* Any channel requested. */
5805 chanpos = pri_find_empty_chan(pri, 1);
5806 if (-1 < chanpos) {
5807 callid = func_pri_dchannel_new_callid();
5808 }
5809 } else if (PRI_CHANNEL(e->ring.channel) == 0x00) {
5810 /* No channel specified. */
5811 #if defined(HAVE_PRI_CALL_WAITING)
5812 if (!pri->allow_call_waiting_calls)
5813 #endif /* defined(HAVE_PRI_CALL_WAITING) */
5814 {
5815 /* We will not accept incoming call waiting calls. */
5816 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
5817 goto setup_exit;
5818 }
5819 #if defined(HAVE_PRI_CALL_WAITING)
5820 chanpos = pri_find_empty_nobch(pri);
5821 if (chanpos < 0) {
5822 /* We could not find/create a call interface. */
5823 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
5824 goto setup_exit;
5825 }
5826
5827 callid = func_pri_dchannel_new_callid();
5828
5829 /* Setup the call interface to use. */
5830 sig_pri_init_config(pri->pvts[chanpos], pri);
5831 #endif /* defined(HAVE_PRI_CALL_WAITING) */
5832 } else {
5833 /* A channel is specified. */
5834 callid = func_pri_dchannel_new_callid();
5835 chanpos = pri_find_principle(pri, e->ring.channel, e->ring.call);
5836 if (chanpos < 0) {
5837 ast_log(LOG_WARNING,
5838 "Span %d: SETUP on unconfigured channel %d/%d\n",
5839 pri->span, PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel));
5840 } else {
5841 switch (pri->pvts[chanpos]->resetting) {
5842 case SIG_PRI_RESET_IDLE:
5843 break;
5844 case SIG_PRI_RESET_ACTIVE:
5845 /*
5846 * The peer may have lost the expected ack or not received the
5847 * RESTART yet.
5848 */
5849 pri->pvts[chanpos]->resetting = SIG_PRI_RESET_NO_ACK;
5850 break;
5851 case SIG_PRI_RESET_NO_ACK:
5852 /* The peer likely is not going to ack the RESTART. */
5853 ast_debug(1,
5854 "Span %d: Second SETUP while waiting for RESTART ACKNOWLEDGE on channel %d/%d\n",
5855 pri->span, PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel));
5856
5857 /* Assume we got the ack. */
5858 pri->pvts[chanpos]->resetting = SIG_PRI_RESET_IDLE;
5859 if (pri->resetting) {
5860 /* Go on to the next idle channel to RESTART. */
5861 pri_check_restart(pri);
5862 }
5863 break;
5864 }
5865 if (!sig_pri_is_chan_available(pri->pvts[chanpos])) {
5866 /* This is where we handle initial glare */
5867 ast_debug(1,
5868 "Span %d: SETUP requested unavailable channel %d/%d. Attempting to renegotiate.\n",
5869 pri->span, PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel));
5870 chanpos = -1;
5871 }
5872 }
5873 #if defined(ALWAYS_PICK_CHANNEL)
5874 if (e->ring.flexible) {
5875 chanpos = -1;
5876 }
5877 #endif /* defined(ALWAYS_PICK_CHANNEL) */
5878 if (chanpos < 0 && e->ring.flexible) {
5879 /* We can try to pick another channel. */
5880 chanpos = pri_find_empty_chan(pri, 1);
5881 }
5882 }
5883 if (chanpos < 0) {
5884 if (e->ring.flexible) {
5885 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
5886 } else {
5887 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
5888 }
5889 goto setup_exit;
5890 }
5891
5892 sig_pri_lock_private(pri->pvts[chanpos]);
5893
5894 /* Mark channel as in use so noone else will steal it. */
5895 pri->pvts[chanpos]->call = e->ring.call;
5896
5897 /* Use plancallingnum as a scratch buffer since it is initialized next. */
5898 apply_plan_to_existing_number(plancallingnum, sizeof(plancallingnum), pri,
5899 e->ring.redirectingnum, e->ring.callingplanrdnis);
5900 sig_pri_set_rdnis(pri->pvts[chanpos], plancallingnum);
5901
5902 /* Setup caller-id info */
5903 apply_plan_to_existing_number(plancallingnum, sizeof(plancallingnum), pri,
5904 e->ring.callingnum, e->ring.callingplan);
5905 pri->pvts[chanpos]->cid_ani2 = 0;
5906 if (pri->pvts[chanpos]->use_callerid) {
5907 ast_shrink_phone_number(plancallingnum);
5908 ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum,
5909 sizeof(pri->pvts[chanpos]->cid_num));
5910 #ifdef PRI_ANI
5911 apply_plan_to_existing_number(plancallingani, sizeof(plancallingani),
5912 pri, e->ring.callingani, e->ring.callingplanani);
5913 ast_shrink_phone_number(plancallingani);
5914 ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani,
5915 sizeof(pri->pvts[chanpos]->cid_ani));
5916 #endif
5917 pri->pvts[chanpos]->cid_subaddr[0] = '\0';
5918 #if defined(HAVE_PRI_SUBADDR)
5919 if (e->ring.calling.subaddress.valid) {
5920 struct ast_party_subaddress calling_subaddress;
5921
5922 ast_party_subaddress_init(&calling_subaddress);
5923 sig_pri_set_subaddress(&calling_subaddress,
5924 &e->ring.calling.subaddress);
5925 if (calling_subaddress.str) {
5926 ast_copy_string(pri->pvts[chanpos]->cid_subaddr,
5927 calling_subaddress.str,
5928 sizeof(pri->pvts[chanpos]->cid_subaddr));
5929 }
5930 ast_party_subaddress_free(&calling_subaddress);
5931 }
5932 #endif /* defined(HAVE_PRI_SUBADDR) */
5933 ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname,
5934 sizeof(pri->pvts[chanpos]->cid_name));
5935 /* this is the callingplan (TON/NPI), e->ring.callingplan>>4 would be the TON */
5936 pri->pvts[chanpos]->cid_ton = e->ring.callingplan;
5937 pri->pvts[chanpos]->callingpres = e->ring.callingpres;
5938 if (e->ring.ani2 >= 0) {
5939 pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
5940 }
5941 } else {
5942 pri->pvts[chanpos]->cid_num[0] = '\0';
5943 pri->pvts[chanpos]->cid_subaddr[0] = '\0';
5944 pri->pvts[chanpos]->cid_ani[0] = '\0';
5945 pri->pvts[chanpos]->cid_name[0] = '\0';
5946 pri->pvts[chanpos]->cid_ton = 0;
5947 pri->pvts[chanpos]->callingpres = 0;
5948 }
5949
5950 /* Setup the user tag for party id's from this device for this call. */
5951 if (pri->append_msn_to_user_tag) {
5952 int len = snprintf(pri->pvts[chanpos]->user_tag,
5953 sizeof(pri->pvts[chanpos]->user_tag), "%s_%s",
5954 pri->initial_user_tag,
5955 pri->nodetype == PRI_NETWORK
5956 ? plancallingnum : e->ring.callednum);
5957 if (len >= sizeof(pri->pvts[chanpos]->user_tag)) {
5958 ast_log(LOG_WARNING, "user_tag '%s' truncated\n", pri->pvts[chanpos]->user_tag);
5959 }
5960 } else {
5961 ast_copy_string(pri->pvts[chanpos]->user_tag,
5962 pri->initial_user_tag, sizeof(pri->pvts[chanpos]->user_tag));
5963 }
5964
5965 sig_pri_set_caller_id(pri->pvts[chanpos]);
5966
5967 /* Set DNID on all incoming calls -- even immediate */
5968 sig_pri_set_dnid(pri->pvts[chanpos], e->ring.callednum);
5969
5970 if (pri->pvts[chanpos]->immediate) {
5971 /* immediate=yes go to s|1 */
5972 ast_verb(3, "Going to extension s|1 because of immediate=yes\n");
5973 pri->pvts[chanpos]->exten[0] = 's';
5974 pri->pvts[chanpos]->exten[1] = '\0';
5975 } else if (!ast_strlen_zero(e->ring.callednum)) {
5976 /* Get called number */
5977 ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum,
5978 sizeof(pri->pvts[chanpos]->exten));
5979 } else if (pri->overlapdial) {
5980 pri->pvts[chanpos]->exten[0] = '\0';
5981 } else {
5982 /* Some PRI circuits are set up to send _no_ digits. Handle them as 's'. */
5983 pri->pvts[chanpos]->exten[0] = 's';
5984 pri->pvts[chanpos]->exten[1] = '\0';
5985 }
5986 /* No number yet, but received "sending complete"? */
5987 if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
5988 ast_verb(3, "Going to extension s|1 because of Complete received\n");
5989 pri->pvts[chanpos]->exten[0] = 's';
5990 pri->pvts[chanpos]->exten[1] = '\0';
5991 }
5992
5993 /* Make sure extension exists (or in overlap dial mode, can exist) */
5994 exten_exists_or_can_exist = ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
5995 && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context,
5996 pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num))
5997 || ast_exists_extension(NULL, pri->pvts[chanpos]->context,
5998 pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num);
5999 if (!exten_exists_or_can_exist) {
6000 ast_verb(3,
6001 "Span %d: Extension %s@%s does not exist. Rejecting call from '%s'.\n",
6002 pri->span, pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context,
6003 pri->pvts[chanpos]->cid_num);
6004 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
6005 pri->pvts[chanpos]->call = NULL;
6006 pri->pvts[chanpos]->exten[0] = '\0';
6007 sig_pri_unlock_private(pri->pvts[chanpos]);
6008 sig_pri_span_devstate_changed(pri);
6009 goto setup_exit;
6010 }
6011
6012 /* Select audio companding mode. */
6013 switch (e->ring.layer1) {
6014 case PRI_LAYER_1_ALAW:
6015 law = SIG_PRI_ALAW;
6016 break;
6017 case PRI_LAYER_1_ULAW:
6018 law = SIG_PRI_ULAW;
6019 break;
6020 default:
6021 /* This is a data call to us. */
6022 law = SIG_PRI_DEFLAW;
6023 break;
6024 }
6025
6026 could_match_more = !e->ring.complete
6027 && (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
6028 && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context,
6029 pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num);
6030
6031 need_dialtone = could_match_more
6032 /*
6033 * Must explicitly check the digital capability this
6034 * way instead of checking the pvt->digital flag
6035 * because the flag hasn't been set yet.
6036 */
6037 && !(e->ring.ctype & AST_TRANS_CAP_DIGITAL)
6038 && !pri->pvts[chanpos]->no_b_channel
6039 && (!strlen(pri->pvts[chanpos]->exten)
6040 || ast_ignore_pattern(pri->pvts[chanpos]->context,
6041 pri->pvts[chanpos]->exten));
6042
6043 if (e->ring.complete || !(pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
6044 /* Just announce proceeding */
6045 pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_PROCEEDING;
6046 pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
6047 } else if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
6048 pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_CONNECT;
6049 pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
6050 } else {
6051 pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_OVERLAP;
6052 #if defined(HAVE_PRI_SETUP_ACK_INBAND)
6053 pri_setup_ack(pri->pri, e->ring.call,
6054 PVT_TO_CHANNEL(pri->pvts[chanpos]), 1, need_dialtone);
6055 #else /* !defined(HAVE_PRI_SETUP_ACK_INBAND) */
6056 pri_need_more_info(pri->pri, e->ring.call,
6057 PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
6058 #endif /* !defined(HAVE_PRI_SETUP_ACK_INBAND) */
6059 }
6060
6061 /*
6062 * Release the PRI lock while we create the channel so other
6063 * threads can send D channel messages. We must also release
6064 * the private lock to prevent deadlock while creating the
6065 * channel.
6066 */
6067 sig_pri_unlock_private(pri->pvts[chanpos]);
6068 ast_mutex_unlock(&pri->lock);
6069 c = sig_pri_new_ast_channel(pri->pvts[chanpos],
6070 could_match_more ? AST_STATE_RESERVED : AST_STATE_RING, law, e->ring.ctype,
6071 pri->pvts[chanpos]->exten, NULL, NULL);
6072 ast_mutex_lock(&pri->lock);
6073 sig_pri_lock_private(pri->pvts[chanpos]);
6074
6075 if (c) {
6076 setup_incoming_channel(pri, chanpos, e);
6077
6078 /* Start PBX */
6079 if (could_match_more) {
6080 #if !defined(HAVE_PRI_SETUP_ACK_INBAND)
6081 if (need_dialtone) {
6082 /* Indicate that we are providing dialtone. */
6083 pri->pvts[chanpos]->progress = 1;/* No need to send plain PROGRESS again. */
6084 #ifdef HAVE_PRI_PROG_W_CAUSE
6085 pri_progress_with_cause(pri->pri, e->ring.call,
6086 PVT_TO_CHANNEL(pri->pvts[chanpos]), 1, -1);/* no cause at all */
6087 #else
6088 pri_progress(pri->pri, e->ring.call,
6089 PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
6090 #endif
6091 }
6092 #endif /* !defined(HAVE_PRI_SETUP_ACK_INBAND) */
6093
6094 if (!ast_pthread_create_detached(&threadid, NULL, pri_ss_thread,
6095 pri->pvts[chanpos])) {
6096 ast_verb(3, "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
6097 plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
6098 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,
6099 pri->span);
6100 sig_pri_unlock_private(pri->pvts[chanpos]);
6101 goto setup_exit;
6102 }
6103 } else {
6104 if (!ast_pbx_start(c)) {
6105 ast_verb(3, "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
6106 plancallingnum, pri->pvts[chanpos]->exten,
6107 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,
6108 pri->span);
6109 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
6110 sig_pri_unlock_private(pri->pvts[chanpos]);
6111 goto setup_exit;
6112 }
6113 }
6114 }
6115 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
6116 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
6117 if (c) {
6118 /* Avoid deadlock while destroying channel */
6119 sig_pri_unlock_private(pri->pvts[chanpos]);
6120 ast_mutex_unlock(&pri->lock);
6121 ast_hangup(c);
6122 ast_mutex_lock(&pri->lock);
6123 } else {
6124 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
6125 pri->pvts[chanpos]->call = NULL;
6126 sig_pri_unlock_private(pri->pvts[chanpos]);
6127 sig_pri_span_devstate_changed(pri);
6128 }
6129
6130 setup_exit:;
6131 if (callid) {
6132 ast_callid_threadassoc_remove();
6133 }
6134 }
6135
6136 static void *pri_dchannel(void *vpri)
6137 {
6138 struct sig_pri_span *pri = vpri;
6139 pri_event *e;
6140 struct pollfd fds[SIG_PRI_NUM_DCHANS];
6141 int res;
6142 int x;
6143 struct timeval tv, lowest, *next;
6144 int doidling=0;
6145 char *cc;
6146 time_t t;
6147 int i, which=-1;
6148 int numdchans;
6149 struct timeval lastidle = { 0, 0 };
6150 pthread_t p;
6151 struct ast_channel *idle;
6152 char idlen[128];
6153 int nextidle = -1;
6154 int haveidles;
6155 int activeidles;
6156 unsigned int len;
6157
6158 gettimeofday(&lastidle, NULL);
6159 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
6160
6161 if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
6162 /* Need to do idle dialing, check to be sure though */
6163 cc = strchr(pri->idleext, '@');
6164 if (cc) {
6165 *cc = '\0';
6166 cc++;
6167 ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
6168 #if 0
6169 /* Extensions may not be loaded yet */
6170 if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
6171 ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
6172 else
6173 #endif
6174 doidling = 1;
6175 } else
6176 ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
6177 }
6178 for (;;) {
6179 ast_callid callid = 0;
6180
6181 for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
6182 if (!pri->dchans[i])
6183 break;
6184 fds[i].fd = pri->fds[i];
6185 fds[i].events = POLLIN | POLLPRI;
6186 fds[i].revents = 0;
6187 }
6188 numdchans = i;
6189 time(&t);
6190 ast_mutex_lock(&pri->lock);
6191 if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->sig != SIG_BRI_PTMP) && (pri->resetinterval > 0)) {
6192 if (pri->resetting && pri_is_up(pri)) {
6193 if (pri->resetpos < 0) {
6194 pri_check_restart(pri);
6195 if (pri->resetting) {
6196 sig_pri_span_devstate_changed(pri);
6197 }
6198 }
6199 } else {
6200 if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) {
6201 pri->resetting = 1;
6202 pri->resetpos = -1;
6203 }
6204 }
6205 }
6206 /* Look for any idle channels if appropriate */
6207 if (doidling && pri_is_up(pri)) {
6208 nextidle = -1;
6209 haveidles = 0;
6210 activeidles = 0;
6211 for (x = pri->numchans; x >= 0; x--) {
6212 if (pri->pvts[x] && !pri->pvts[x]->no_b_channel) {
6213 if (sig_pri_is_chan_available(pri->pvts[x])) {
6214 if (haveidles < pri->minunused) {
6215 haveidles++;
6216 } else {
6217 nextidle = x;
6218 break;
6219 }
6220 } else if (pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
6221 activeidles++;
6222 }
6223 }
6224 }
6225 if (nextidle > -1) {
6226 if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
6227 /* Don't create a new idle call more than once per second */
6228 snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
6229 pri->pvts[nextidle]->allocated = 1;
6230 /*
6231 * Release the PRI lock while we create the channel so other
6232 * threads can send D channel messages.
6233 */
6234 ast_mutex_unlock(&pri->lock);
6235 /*
6236 * We already have the B channel reserved for this call. We
6237 * just need to make sure that sig_pri_hangup() has completed
6238 * cleaning up before continuing.
6239 */
6240 sig_pri_lock_private(pri->pvts[nextidle]);
6241 sig_pri_unlock_private(pri->pvts[nextidle]);
6242 idle = sig_pri_request(pri->pvts[nextidle], SIG_PRI_ULAW, NULL, NULL, 0);
6243 ast_mutex_lock(&pri->lock);
6244 if (idle) {
6245 pri->pvts[nextidle]->isidlecall = 1;
6246 if (ast_pthread_create_background(&p, NULL, do_idle_thread, pri->pvts[nextidle])) {
6247 ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", ast_channel_name(idle));
6248 ast_mutex_unlock(&pri->lock);
6249 ast_hangup(idle);
6250 ast_mutex_lock(&pri->lock);
6251 }
6252 } else {
6253 pri->pvts[nextidle]->allocated = 0;
6254 ast_log(LOG_WARNING, "Unable to request channel 'DAHDI/%s' for idle call\n", idlen);
6255 }
6256 gettimeofday(&lastidle, NULL);
6257 }
6258 } else if ((haveidles < pri->minunused) &&
6259 (activeidles > pri->minidle)) {
6260 /* Mark something for hangup if there is something
6261 that can be hungup */
6262 for (x = pri->numchans; x >= 0; x--) {
6263 /* find a candidate channel */
6264 if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
6265 ast_channel_softhangup_internal_flag_add(pri->pvts[x]->owner, AST_SOFTHANGUP_DEV);
6266 haveidles++;
6267 /* Stop if we have enough idle channels or
6268 can't spare any more active idle ones */
6269 if ((haveidles >= pri->minunused) ||
6270 (activeidles <= pri->minidle))
6271 break;
6272 }
6273 }
6274 }
6275 }
6276 /* Start with reasonable max */
6277 if (doidling || pri->resetting) {
6278 /*
6279 * Make sure we stop at least once per second if we're
6280 * monitoring idle channels
6281 */
6282 lowest = ast_tv(1, 0);
6283 } else {
6284 /* Don't poll for more than 60 seconds */
6285 lowest = ast_tv(60, 0);
6286 }
6287 for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
6288 if (!pri->dchans[i]) {
6289 /* We scanned all D channels on this span. */
6290 break;
6291 }
6292 next = pri_schedule_next(pri->dchans[i]);
6293 if (next) {
6294 /* We need relative time here */
6295 tv = ast_tvsub(*next, ast_tvnow());
6296 if (tv.tv_sec < 0) {
6297 /*
6298 * A timer has already expired.
6299 * By definition zero time is the lowest so we can quit early.
6300 */
6301 lowest = ast_tv(0, 0);
6302 break;
6303 }
6304 if (ast_tvcmp(tv, lowest) < 0) {
6305 lowest = tv;
6306 }
6307 }
6308 }
6309 ast_mutex_unlock(&pri->lock);
6310
6311 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
6312 pthread_testcancel();
6313 e = NULL;
6314 res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
6315 pthread_testcancel();
6316 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
6317
6318 ast_mutex_lock(&pri->lock);
6319 if (!res) {
6320 for (which = 0; which < SIG_PRI_NUM_DCHANS; which++) {
6321 if (!pri->dchans[which])
6322 break;
6323 /* Just a timeout, run the scheduler */
6324 e = pri_schedule_run(pri->dchans[which]);
6325 if (e)
6326 break;
6327 }
6328 } else if (res > -1) {
6329 for (which = 0; which < SIG_PRI_NUM_DCHANS; which++) {
6330 if (!pri->dchans[which])
6331 break;
6332 if (fds[which].revents & POLLPRI) {
6333 sig_pri_handle_dchan_exception(pri, which);
6334 } else if (fds[which].revents & POLLIN) {
6335 e = pri_check_event(pri->dchans[which]);
6336 }
6337 if (e)
6338 break;
6339
6340 if ((errno != 0) && (errno != EINTR)) {
6341 ast_log(LOG_NOTICE, "pri_check_event returned error %d (%s)\n",
6342 errno, strerror(errno));
6343 }
6344 if (errno == ENODEV) {
6345 pri_destroy_later(pri);
6346 }
6347 }
6348 } else if (errno != EINTR)
6349 ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
6350
6351 if (e) {
6352 int chanpos = -1;
6353 char cause_str[36];
6354
6355 if (pri->debug) {
6356 ast_verbose("Span %d: Processing event %s(%d)\n",
6357 pri->span, pri_event2str(e->e), e->e);
6358 }
6359
6360 if (e->e != PRI_EVENT_DCHAN_DOWN) {
6361 if (!(pri->dchanavail[which] & DCHAN_UP)) {
6362 ast_verb(2, "%s D-Channel on span %d up\n", pri_order(which), pri->span);
6363 }
6364 pri->dchanavail[which] |= DCHAN_UP;
6365 } else {
6366 if (pri->dchanavail[which] & DCHAN_UP) {
6367 ast_verb(2, "%s D-Channel on span %d down\n", pri_order(which), pri->span);
6368 }
6369 pri->dchanavail[which] &= ~DCHAN_UP;
6370 }
6371
6372 if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which]))
6373 /* Must be an NFAS group that has the secondary dchan active */
6374 pri->pri = pri->dchans[which];
6375
6376 switch (e->e) {
6377 case PRI_EVENT_DCHAN_UP:
6378 pri->no_d_channels = 0;
6379 if (!pri->pri) {
6380 pri_find_dchan(pri);
6381 }
6382
6383 /* Note presence of D-channel */
6384 time(&pri->lastreset);
6385
6386 /* Restart in 5 seconds */
6387 if (pri->resetinterval > -1) {
6388 pri->lastreset -= pri->resetinterval;
6389 pri->lastreset += 5;
6390 }
6391 /* Take the channels from inalarm condition */
6392 pri->resetting = 0;
6393 for (i = 0; i < pri->numchans; i++) {
6394 if (pri->pvts[i]) {
6395 sig_pri_set_alarm(pri->pvts[i], 0);
6396 }
6397 }
6398 sig_pri_span_devstate_changed(pri);
6399 break;
6400 case PRI_EVENT_DCHAN_DOWN:
6401 pri_find_dchan(pri);
6402 if (!pri_is_up(pri)) {
6403 if (pri->sig == SIG_BRI_PTMP) {
6404 /*
6405 * For PTMP connections with non-persistent layer 2 we want to
6406 * *not* declare inalarm unless there actually is an alarm.
6407 */
6408 break;
6409 }
6410 /* Hangup active channels and put them in alarm mode */
6411 pri->resetting = 0;
6412 for (i = 0; i < pri->numchans; i++) {
6413 struct sig_pri_chan *p = pri->pvts[i];
6414
6415 if (p) {
6416 if (pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
6417 /* T309 is not enabled : destroy calls when alarm occurs */
6418 if (p->call) {
6419 pri_destroycall(p->pri->pri, p->call);
6420 p->call = NULL;
6421 }
6422 if (p->owner)
6423 ast_channel_softhangup_internal_flag_add(p->owner, AST_SOFTHANGUP_DEV);
6424 }
6425 sig_pri_set_alarm(p, 1);
6426 }
6427 }
6428 sig_pri_span_devstate_changed(pri);
6429 }
6430 break;
6431 case PRI_EVENT_RESTART:
6432 if (e->restart.channel > -1 && PRI_CHANNEL(e->restart.channel) != 0xFF) {
6433 chanpos = pri_find_principle(pri, e->restart.channel, NULL);
6434 if (chanpos < 0)
6435 ast_log(LOG_WARNING,
6436 "Span %d: Restart requested on odd/unavailable channel number %d/%d\n",
6437 pri->span, PRI_SPAN(e->restart.channel),
6438 PRI_CHANNEL(e->restart.channel));
6439 else {
6440 int skipit = 0;
6441 #if defined(HAVE_PRI_SERVICE_MESSAGES)
6442 unsigned why;
6443
6444 why = pri->pvts[chanpos]->service_status;
6445 if (why) {
6446 ast_log(LOG_NOTICE,
6447 "Span %d: Channel %d/%d out-of-service (reason: %s), ignoring RESTART\n",
6448 pri->span, PRI_SPAN(e->restart.channel),
6449 PRI_CHANNEL(e->restart.channel),
6450 (why & SRVST_FAREND) ? (why & SRVST_NEAREND) ? "both ends" : "far end" : "near end");
6451 skipit = 1;
6452 }
6453 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
6454 sig_pri_lock_private(pri->pvts[chanpos]);
6455 if (!skipit) {
6456 ast_verb(3, "Span %d: Channel %d/%d restarted\n", pri->span,
6457 PRI_SPAN(e->restart.channel),
6458 PRI_CHANNEL(e->restart.channel));
6459 if (pri->pvts[chanpos]->call) {
6460 pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
6461 pri->pvts[chanpos]->call = NULL;
6462 }
6463 }
6464 /* Force hangup if appropriate */
6465 sig_pri_queue_hangup(pri, chanpos);
6466 sig_pri_unlock_private(pri->pvts[chanpos]);
6467 }
6468 } else {
6469 ast_verb(3, "Restart requested on entire span %d\n", pri->span);
6470 for (x = 0; x < pri->numchans; x++)
6471 if (pri->pvts[x]) {
6472 sig_pri_lock_private(pri->pvts[x]);
6473 if (pri->pvts[x]->call) {
6474 pri_destroycall(pri->pri, pri->pvts[x]->call);
6475 pri->pvts[x]->call = NULL;
6476 }
6477 /* Force hangup if appropriate */
6478 sig_pri_queue_hangup(pri, x);
6479 sig_pri_unlock_private(pri->pvts[x]);
6480 }
6481 }
6482 sig_pri_span_devstate_changed(pri);
6483 break;
6484 case PRI_EVENT_KEYPAD_DIGIT:
6485 if (sig_pri_is_cis_call(e->digit.channel)) {
6486 sig_pri_handle_cis_subcmds(pri, e->e, e->digit.subcmds,
6487 e->digit.call);
6488 break;
6489 }
6490 chanpos = pri_find_principle_by_call(pri, e->digit.call);
6491 if (chanpos < 0) {
6492 ast_log(LOG_WARNING,
6493 "Span %d: Received keypad digits for unknown call.\n", pri->span);
6494 break;
6495 }
6496 sig_pri_lock_private(pri->pvts[chanpos]);
6497
6498 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
6499
6500 sig_pri_handle_subcmds(pri, chanpos, e->e, e->digit.subcmds,
6501 e->digit.call);
6502 /* queue DTMF frame if the PBX for this call was already started (we're forwarding KEYPAD_DIGITs further on */
6503 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
6504 && pri->pvts[chanpos]->owner) {
6505 /* how to do that */
6506 int digitlen = strlen(e->digit.digits);
6507 int i;
6508
6509 for (i = 0; i < digitlen; i++) {
6510 struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = e->digit.digits[i], };
6511
6512 pri_queue_frame(pri, chanpos, &f);
6513 }
6514 }
6515 sig_pri_unlock_private(pri->pvts[chanpos]);
6516 break;
6517
6518 case PRI_EVENT_INFO_RECEIVED:
6519 if (sig_pri_is_cis_call(e->ring.channel)) {
6520 sig_pri_handle_cis_subcmds(pri, e->e, e->ring.subcmds,
6521 e->ring.call);
6522 break;
6523 }
6524 chanpos = pri_find_principle_by_call(pri, e->ring.call);
6525 if (chanpos < 0) {
6526 ast_log(LOG_WARNING,
6527 "Span %d: Received INFORMATION for unknown call.\n", pri->span);
6528 break;
6529 }
6530 sig_pri_lock_private(pri->pvts[chanpos]);
6531
6532 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
6533
6534 sig_pri_handle_subcmds(pri, chanpos, e->e, e->ring.subcmds, e->ring.call);
6535 /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */
6536 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
6537 && pri->pvts[chanpos]->owner) {
6538 /* how to do that */
6539 int digitlen = strlen(e->ring.callednum);
6540 int i;
6541
6542 for (i = 0; i < digitlen; i++) {
6543 struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = e->ring.callednum[i], };
6544
6545 pri_queue_frame(pri, chanpos, &f);
6546 }
6547 }
6548 sig_pri_unlock_private(pri->pvts[chanpos]);
6549 break;
6550 #if defined(HAVE_PRI_SERVICE_MESSAGES)
6551 case PRI_EVENT_SERVICE:
6552 chanpos = pri_find_principle(pri, e->service.channel, NULL);
6553 if (chanpos < 0) {
6554 ast_log(LOG_WARNING, "Received service change status %d on unconfigured channel %d/%d span %d\n",
6555 e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
6556 } else {
6557 char db_chan_name[20];
6558 char db_answer[15];
6559 int ch;
6560 unsigned *why;
6561
6562 ch = pri->pvts[chanpos]->channel;
6563 snprintf(db_chan_name, sizeof(db_chan_name), "%s/%d:%d", dahdi_db, pri->span, ch);
6564 why = &pri->pvts[chanpos]->service_status;
6565 switch (e->service.changestatus) {
6566 case 0: /* in-service */
6567 /* Far end wants to be in service now. */
6568 ast_db_del(db_chan_name, SRVST_DBKEY);
6569 *why &= ~SRVST_FAREND;
6570 if (*why) {
6571 snprintf(db_answer, sizeof(db_answer), "%s:%u",
6572 SRVST_TYPE_OOS, *why);
6573 ast_db_put(db_chan_name, SRVST_DBKEY, db_answer);
6574 } else {
6575 sig_pri_span_devstate_changed(pri);
6576 }
6577 break;
6578 case 2: /* out-of-service */
6579 /* Far end wants to be out-of-service now. */
6580 ast_db_del(db_chan_name, SRVST_DBKEY);
6581 *why |= SRVST_FAREND;
6582 snprintf(db_answer, sizeof(db_answer), "%s:%u", SRVST_TYPE_OOS,
6583 *why);
6584 ast_db_put(db_chan_name, SRVST_DBKEY, db_answer);
6585 sig_pri_span_devstate_changed(pri);
6586 break;
6587 default:
6588 ast_log(LOG_ERROR, "Huh? changestatus is: %d\n", e->service.changestatus);
6589 break;
6590 }
6591 ast_log(LOG_NOTICE, "Channel %d/%d span %d (logical: %d) received a change of service message, status '%d'\n",
6592 PRI_SPAN(e->service.channel), PRI_CHANNEL(e->service.channel), pri->span, ch, e->service.changestatus);
6593 }
6594 break;
6595 case PRI_EVENT_SERVICE_ACK:
6596 chanpos = pri_find_principle(pri, e->service_ack.channel, NULL);
6597 if (chanpos < 0) {
6598 ast_log(LOG_WARNING, "Received service acknowledge change status '%d' on unconfigured channel %d/%d span %d\n",
6599 e->service_ack.changestatus, PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span);
6600 } else {
6601 ast_debug(2, "Channel %d/%d span %d received a change os service acknowledgement message, status '%d'\n",
6602 PRI_SPAN(e->service_ack.channel), PRI_CHANNEL(e->service_ack.channel), pri->span, e->service_ack.changestatus);
6603 }
6604 break;
6605 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
6606 case PRI_EVENT_RING:
6607 sig_pri_handle_setup(pri, e);
6608 break;
6609 case PRI_EVENT_RINGING:
6610 if (sig_pri_is_cis_call(e->ringing.channel)) {
6611 sig_pri_handle_cis_subcmds(pri, e->e, e->ringing.subcmds,
6612 e->ringing.call);
6613 break;
6614 }
6615 chanpos = pri_find_fixup_principle(pri, e->ringing.channel,
6616 e->ringing.call);
6617 if (chanpos < 0) {
6618 break;
6619 }
6620 sig_pri_lock_private(pri->pvts[chanpos]);
6621
6622 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
6623
6624 sig_pri_handle_subcmds(pri, chanpos, e->e, e->ringing.subcmds,
6625 e->ringing.call);
6626 sig_pri_cc_generic_check(pri, chanpos, AST_CC_CCNR);
6627 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
6628 sig_pri_lock_owner(pri, chanpos);
6629 if (pri->pvts[chanpos]->owner) {
6630 ast_setstate(pri->pvts[chanpos]->owner, AST_STATE_RINGING);
6631 ast_channel_unlock(pri->pvts[chanpos]->owner);
6632 }
6633 pri_queue_control(pri, chanpos, AST_CONTROL_RINGING);
6634 if (pri->pvts[chanpos]->call_level < SIG_PRI_CALL_LEVEL_ALERTING) {
6635 pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_ALERTING;
6636 }
6637
6638 if (!pri->pvts[chanpos]->progress
6639 && !pri->pvts[chanpos]->no_b_channel
6640 #ifdef PRI_PROGRESS_MASK
6641 && (e->ringing.progressmask
6642 & (PRI_PROG_CALL_NOT_E2E_ISDN | PRI_PROG_INBAND_AVAILABLE))
6643 #else
6644 && e->ringing.progress == 8
6645 #endif
6646 ) {
6647 /* Bring voice path up */
6648 pri_queue_control(pri, chanpos, AST_CONTROL_PROGRESS);
6649 pri->pvts[chanpos]->progress = 1;
6650 sig_pri_set_dialing(pri->pvts[chanpos], 0);
6651 sig_pri_open_media(pri->pvts[chanpos]);
6652 }
6653
6654 #ifdef SUPPORT_USERUSER
6655 if (!ast_strlen_zero(e->ringing.useruserinfo)) {
6656 struct ast_channel *owner;
6657
6658 sig_pri_lock_owner(pri, chanpos);
6659 owner = pri->pvts[chanpos]->owner;
6660 if (owner) {
6661 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
6662 e->ringing.useruserinfo);
6663 ast_channel_unlock(owner);
6664 }
6665 }
6666 #endif
6667
6668 sig_pri_unlock_private(pri->pvts[chanpos]);
6669 break;
6670 case PRI_EVENT_PROGRESS:
6671 if (sig_pri_is_cis_call(e->proceeding.channel)) {
6672 sig_pri_handle_cis_subcmds(pri, e->e, e->proceeding.subcmds,
6673 e->proceeding.call);
6674 break;
6675 }
6676 chanpos = pri_find_fixup_principle(pri, e->proceeding.channel,
6677 e->proceeding.call);
6678 if (chanpos < 0) {
6679 break;
6680 }
6681 sig_pri_lock_private(pri->pvts[chanpos]);
6682
6683 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
6684
6685 sig_pri_handle_subcmds(pri, chanpos, e->e, e->proceeding.subcmds,
6686 e->proceeding.call);
6687
6688 if (e->proceeding.cause > -1) {
6689 if (pri->pvts[chanpos]->owner) {
6690 snprintf(cause_str, sizeof(cause_str), "PRI PRI_EVENT_PROGRESS (%d)", e->proceeding.cause);
6691 pri_queue_pvt_cause_data(pri, chanpos, cause_str, e->proceeding.cause);
6692 }
6693
6694 ast_verb(3, "PROGRESS with cause code %d received\n", e->proceeding.cause);
6695
6696 /* Work around broken, out of spec USER_BUSY cause in a progress message */
6697 if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
6698 if (pri->pvts[chanpos]->owner) {
6699 ast_verb(3, "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
6700
6701 ast_channel_hangupcause_set(pri->pvts[chanpos]->owner, e->proceeding.cause);
6702 pri_queue_control(pri, chanpos, AST_CONTROL_BUSY);
6703 }
6704 }
6705 }
6706
6707 if (!pri->pvts[chanpos]->progress
6708 && !pri->pvts[chanpos]->no_b_channel
6709 #ifdef PRI_PROGRESS_MASK
6710 && (e->proceeding.progressmask
6711 & (PRI_PROG_CALL_NOT_E2E_ISDN | PRI_PROG_INBAND_AVAILABLE))
6712 #else
6713 && e->proceeding.progress == 8
6714 #endif
6715 ) {
6716 /* Bring voice path up */
6717 ast_debug(1,
6718 "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
6719 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,
6720 pri->span);
6721 pri_queue_control(pri, chanpos, AST_CONTROL_PROGRESS);
6722 pri->pvts[chanpos]->progress = 1;
6723 sig_pri_set_dialing(pri->pvts[chanpos], 0);
6724 sig_pri_open_media(pri->pvts[chanpos]);
6725 }
6726 sig_pri_unlock_private(pri->pvts[chanpos]);
6727 break;
6728 case PRI_EVENT_PROCEEDING:
6729 if (sig_pri_is_cis_call(e->proceeding.channel)) {
6730 sig_pri_handle_cis_subcmds(pri, e->e, e->proceeding.subcmds,
6731 e->proceeding.call);
6732 break;
6733 }
6734 chanpos = pri_find_fixup_principle(pri, e->proceeding.channel,
6735 e->proceeding.call);
6736 if (chanpos < 0) {
6737 break;
6738 }
6739 sig_pri_lock_private(pri->pvts[chanpos]);
6740
6741 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
6742
6743 sig_pri_handle_subcmds(pri, chanpos, e->e, e->proceeding.subcmds,
6744 e->proceeding.call);
6745 if (pri->pvts[chanpos]->call_level < SIG_PRI_CALL_LEVEL_PROCEEDING) {
6746 pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_PROCEEDING;
6747 ast_debug(1,
6748 "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
6749 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,
6750 pri->span);
6751 pri_queue_control(pri, chanpos, AST_CONTROL_PROCEEDING);
6752 }
6753 if (!pri->pvts[chanpos]->progress
6754 && !pri->pvts[chanpos]->no_b_channel
6755 #ifdef PRI_PROGRESS_MASK
6756 /*
6757 * We only care about PRI_PROG_INBAND_AVAILABLE to open the
6758 * voice path.
6759 *
6760 * We explicitly DO NOT want to check PRI_PROG_CALL_NOT_E2E_ISDN
6761 * because it will mess up ISDN to SIP interoperability for
6762 * the ALERTING message.
6763 */
6764 && (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)
6765 #else
6766 && e->proceeding.progress == 8
6767 #endif
6768 ) {
6769 /* Bring voice path up */
6770 pri_queue_control(pri, chanpos, AST_CONTROL_PROGRESS);
6771 pri->pvts[chanpos]->progress = 1;
6772 sig_pri_set_dialing(pri->pvts[chanpos], 0);
6773 sig_pri_open_media(pri->pvts[chanpos]);
6774 } else if (pri->inband_on_proceeding) {
6775 /*
6776 * XXX This is to accommodate a broken switch that sends a
6777 * PROCEEDING without any progress indication ie for
6778 * inband audio. This should be part of the conditional
6779 * test above to bring the voice path up.
6780 */
6781 sig_pri_set_dialing(pri->pvts[chanpos], 0);
6782 }
6783 sig_pri_unlock_private(pri->pvts[chanpos]);
6784 break;
6785 case PRI_EVENT_FACILITY:
6786 if (!e->facility.call || sig_pri_is_cis_call(e->facility.channel)) {
6787 /* Event came in on the dummy channel or a CIS call. */
6788 #if defined(HAVE_PRI_CALL_REROUTING)
6789 sig_pri_handle_cis_subcmds(pri, e->e, e->facility.subcmds,
6790 e->facility.subcall);
6791 #else
6792 sig_pri_handle_cis_subcmds(pri, e->e, e->facility.subcmds,
6793 e->facility.call);
6794 #endif /* !defined(HAVE_PRI_CALL_REROUTING) */
6795 break;
6796 }
6797 chanpos = pri_find_principle_by_call(pri, e->facility.call);
6798 if (chanpos < 0) {
6799 ast_log(LOG_WARNING, "Span %d: Received facility for unknown call.\n",
6800 pri->span);
6801 break;
6802 }
6803 sig_pri_lock_private(pri->pvts[chanpos]);
6804
6805 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
6806
6807 #if defined(HAVE_PRI_CALL_REROUTING)
6808 sig_pri_handle_subcmds(pri, chanpos, e->e, e->facility.subcmds,
6809 e->facility.subcall);
6810 #else
6811 sig_pri_handle_subcmds(pri, chanpos, e->e, e->facility.subcmds,
6812 e->facility.call);
6813 #endif /* !defined(HAVE_PRI_CALL_REROUTING) */
6814 sig_pri_unlock_private(pri->pvts[chanpos]);
6815 break;
6816 case PRI_EVENT_ANSWER:
6817 if (sig_pri_is_cis_call(e->answer.channel)) {
6818 #if defined(HAVE_PRI_CALL_WAITING)
6819 /* Call is CIS so do normal CONNECT_ACKNOWLEDGE. */
6820 pri_connect_ack(pri->pri, e->answer.call, 0);
6821 #endif /* defined(HAVE_PRI_CALL_WAITING) */
6822 sig_pri_handle_cis_subcmds(pri, e->e, e->answer.subcmds,
6823 e->answer.call);
6824 break;
6825 }
6826 chanpos = pri_find_fixup_principle(pri, e->answer.channel, e->answer.call);
6827 if (chanpos < 0) {
6828 break;
6829 }
6830 #if defined(HAVE_PRI_CALL_WAITING)
6831 if (pri->pvts[chanpos]->is_call_waiting) {
6832 if (pri->pvts[chanpos]->no_b_channel) {
6833 int new_chanpos;
6834
6835 /*
6836 * Need to find a free channel now or
6837 * kill the call with PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION.
6838 */
6839 new_chanpos = pri_find_empty_chan(pri, 1);
6840 if (0 <= new_chanpos) {
6841 new_chanpos = pri_fixup_principle(pri, new_chanpos,
6842 e->answer.call);
6843 }
6844 if (new_chanpos < 0) {
6845 /*
6846 * Either no channel was available or someone stole
6847 * the channel!
6848 */
6849 ast_verb(3,
6850 "Span %d: Channel not available for call waiting call.\n",
6851 pri->span);
6852 sig_pri_lock_private(pri->pvts[chanpos]);
6853 sig_pri_handle_subcmds(pri, chanpos, e->e, e->answer.subcmds,
6854 e->answer.call);
6855 sig_pri_cc_generic_check(pri, chanpos, AST_CC_CCBS);
6856 sig_pri_lock_owner(pri, chanpos);
6857 if (pri->pvts[chanpos]->owner) {
6858 ast_channel_hangupcause_set(pri->pvts[chanpos]->owner, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
6859 switch (ast_channel_state(pri->pvts[chanpos]->owner)) {
6860 case AST_STATE_BUSY:
6861 case AST_STATE_UP:
6862 ast_softhangup_nolock(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
6863 break;
6864 default:
6865 pri_queue_control(pri, chanpos, AST_CONTROL_CONGESTION);
6866 break;
6867 }
6868 ast_channel_unlock(pri->pvts[chanpos]->owner);
6869 } else {
6870 pri->pvts[chanpos]->is_call_waiting = 0;
6871 ast_atomic_fetchadd_int(&pri->num_call_waiting_calls, -1);
6872 pri_hangup(pri->pri, e->answer.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
6873 pri->pvts[chanpos]->call = NULL;
6874 }
6875 sig_pri_unlock_private(pri->pvts[chanpos]);
6876 sig_pri_span_devstate_changed(pri);
6877 break;
6878 }
6879 chanpos = new_chanpos;
6880 }
6881 pri_connect_ack(pri->pri, e->answer.call, PVT_TO_CHANNEL(pri->pvts[chanpos]));
6882 sig_pri_span_devstate_changed(pri);
6883 } else {
6884 /* Call is normal so do normal CONNECT_ACKNOWLEDGE. */
6885 pri_connect_ack(pri->pri, e->answer.call, 0);
6886 }
6887 #endif /* defined(HAVE_PRI_CALL_WAITING) */
6888 sig_pri_lock_private(pri->pvts[chanpos]);
6889
6890 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
6891
6892 #if defined(HAVE_PRI_CALL_WAITING)
6893 if (pri->pvts[chanpos]->is_call_waiting) {
6894 pri->pvts[chanpos]->is_call_waiting = 0;
6895 ast_atomic_fetchadd_int(&pri->num_call_waiting_calls, -1);
6896 }
6897 #endif /* defined(HAVE_PRI_CALL_WAITING) */
6898 sig_pri_handle_subcmds(pri, chanpos, e->e, e->answer.subcmds,
6899 e->answer.call);
6900 if (!ast_strlen_zero(pri->pvts[chanpos]->deferred_digits)) {
6901 /* We have some 'w' deferred digits to dial now. */
6902 ast_verb(3,
6903 "Span %d: Channel %d/%d dialing deferred digit string: %s\n",
6904 pri->span, pri->pvts[chanpos]->logicalspan,
6905 pri->pvts[chanpos]->prioffset,
6906 pri->pvts[chanpos]->deferred_digits);
6907 if (pri->pvts[chanpos]->call_level < SIG_PRI_CALL_LEVEL_DEFER_DIAL) {
6908 pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_DEFER_DIAL;
6909 }
6910 sig_pri_dial_digits(pri->pvts[chanpos],
6911 pri->pvts[chanpos]->deferred_digits);
6912 } else {
6913 if (pri->pvts[chanpos]->call_level < SIG_PRI_CALL_LEVEL_CONNECT) {
6914 pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_CONNECT;
6915 }
6916 sig_pri_open_media(pri->pvts[chanpos]);
6917 pri_queue_control(pri, chanpos, AST_CONTROL_ANSWER);
6918 sig_pri_set_dialing(pri->pvts[chanpos], 0);
6919 /* Enable echo cancellation if it's not on already */
6920 sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
6921 }
6922
6923 #ifdef SUPPORT_USERUSER
6924 if (!ast_strlen_zero(e->answer.useruserinfo)) {
6925 struct ast_channel *owner;
6926
6927 sig_pri_lock_owner(pri, chanpos);
6928 owner = pri->pvts[chanpos]->owner;
6929 if (owner) {
6930 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
6931 e->answer.useruserinfo);
6932 ast_channel_unlock(owner);
6933 }
6934 }
6935 #endif
6936
6937 sig_pri_unlock_private(pri->pvts[chanpos]);
6938 break;
6939 #if defined(HAVE_PRI_CALL_WAITING)
6940 case PRI_EVENT_CONNECT_ACK:
6941 if (sig_pri_is_cis_call(e->connect_ack.channel)) {
6942 sig_pri_handle_cis_subcmds(pri, e->e, e->connect_ack.subcmds,
6943 e->connect_ack.call);
6944 break;
6945 }
6946 chanpos = pri_find_fixup_principle(pri, e->connect_ack.channel,
6947 e->connect_ack.call);
6948 if (chanpos < 0) {
6949 break;
6950 }
6951
6952 sig_pri_lock_private(pri->pvts[chanpos]);
6953
6954 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
6955
6956 sig_pri_handle_subcmds(pri, chanpos, e->e, e->connect_ack.subcmds,
6957 e->connect_ack.call);
6958 sig_pri_open_media(pri->pvts[chanpos]);
6959 sig_pri_unlock_private(pri->pvts[chanpos]);
6960 sig_pri_span_devstate_changed(pri);
6961 break;
6962 #endif /* defined(HAVE_PRI_CALL_WAITING) */
6963 case PRI_EVENT_HANGUP:
6964 if (sig_pri_is_cis_call(e->hangup.channel)) {
6965 sig_pri_handle_cis_subcmds(pri, e->e, e->hangup.subcmds,
6966 e->hangup.call);
6967 pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
6968 break;
6969 }
6970 chanpos = pri_find_principle_by_call(pri, e->hangup.call);
6971 if (chanpos < 0) {
6972 /*
6973 * Continue hanging up the call even though
6974 * we do not remember it (if we ever did).
6975 */
6976 pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
6977 break;
6978 }
6979 sig_pri_lock_private(pri->pvts[chanpos]);
6980
6981 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
6982
6983 sig_pri_handle_subcmds(pri, chanpos, e->e, e->hangup.subcmds,
6984 e->hangup.call);
6985 switch (e->hangup.cause) {
6986 case PRI_CAUSE_INVALID_CALL_REFERENCE:
6987 /*
6988 * The peer denies the existence of this call so we must
6989 * continue hanging it up and forget about it.
6990 */
6991 pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
6992 pri->pvts[chanpos]->call = NULL;
6993 break;
6994 default:
6995 break;
6996 }
6997 if (!pri->pvts[chanpos]->alreadyhungup) {
6998 /* we're calling here dahdi_hangup so once we get there we need to clear p->call after calling pri_hangup */
6999 pri->pvts[chanpos]->alreadyhungup = 1;
7000 switch (e->hangup.cause) {
7001 case PRI_CAUSE_USER_BUSY:
7002 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
7003 sig_pri_cc_generic_check(pri, chanpos, AST_CC_CCBS);
7004 break;
7005 default:
7006 break;
7007 }
7008 if (pri->pvts[chanpos]->owner) {
7009 snprintf(cause_str, sizeof(cause_str), "PRI PRI_EVENT_HANGUP (%d)", e->hangup.cause);
7010 pri_queue_pvt_cause_data(pri, chanpos, cause_str, e->hangup.cause);
7011 }
7012 if (pri->pvts[chanpos]->owner) {
7013 int do_hangup = 0;
7014
7015 /* Queue a BUSY instead of a hangup if our cause is appropriate */
7016 ast_channel_hangupcause_set(pri->pvts[chanpos]->owner, e->hangup.cause);
7017 switch (ast_channel_state(pri->pvts[chanpos]->owner)) {
7018 case AST_STATE_BUSY:
7019 case AST_STATE_UP:
7020 do_hangup = 1;
7021 break;
7022 default:
7023 if (!pri->pvts[chanpos]->outgoing) {
7024 /*
7025 * The incoming call leg hung up before getting
7026 * connected so just hangup the call.
7027 */
7028 do_hangup = 1;
7029 break;
7030 }
7031 switch (e->hangup.cause) {
7032 case PRI_CAUSE_USER_BUSY:
7033 pri_queue_control(pri, chanpos, AST_CONTROL_BUSY);
7034 break;
7035 case PRI_CAUSE_CALL_REJECTED:
7036 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
7037 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
7038 case PRI_CAUSE_SWITCH_CONGESTION:
7039 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
7040 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
7041 pri_queue_control(pri, chanpos, AST_CONTROL_CONGESTION);
7042 break;
7043 default:
7044 do_hangup = 1;
7045 break;
7046 }
7047 break;
7048 }
7049
7050 if (do_hangup) {
7051 sig_pri_queue_hangup(pri, chanpos);
7052 }
7053 } else {
7054 /*
7055 * Continue hanging up the call even though
7056 * we do not have an owner.
7057 */
7058 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
7059 pri->pvts[chanpos]->call = NULL;
7060 }
7061 ast_verb(3, "Span %d: Channel %d/%d got hangup, cause %d\n",
7062 pri->span, pri->pvts[chanpos]->logicalspan,
7063 pri->pvts[chanpos]->prioffset, e->hangup.cause);
7064 } else {
7065 /* Continue hanging up the call. */
7066 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
7067 pri->pvts[chanpos]->call = NULL;
7068 }
7069 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL
7070 && pri->sig != SIG_BRI_PTMP && !pri->resetting
7071 && pri->force_restart_unavailable_chans
7072 && pri->pvts[chanpos]->resetting == SIG_PRI_RESET_IDLE) {
7073 ast_verb(3,
7074 "Span %d: Forcing restart of channel %d/%d since channel reported in use\n",
7075 pri->span, pri->pvts[chanpos]->logicalspan,
7076 pri->pvts[chanpos]->prioffset);
7077 pri->pvts[chanpos]->resetting = SIG_PRI_RESET_ACTIVE;
7078 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
7079 }
7080 if (e->hangup.aoc_units > -1)
7081 ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
7082 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
7083
7084 #ifdef SUPPORT_USERUSER
7085 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
7086 struct ast_channel *owner;
7087
7088 sig_pri_lock_owner(pri, chanpos);
7089 owner = pri->pvts[chanpos]->owner;
7090 if (owner) {
7091 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
7092 e->hangup.useruserinfo);
7093 ast_channel_unlock(owner);
7094 }
7095 }
7096 #endif
7097
7098 sig_pri_unlock_private(pri->pvts[chanpos]);
7099 sig_pri_span_devstate_changed(pri);
7100 break;
7101 case PRI_EVENT_HANGUP_REQ:
7102 if (sig_pri_is_cis_call(e->hangup.channel)) {
7103 sig_pri_handle_cis_subcmds(pri, e->e, e->hangup.subcmds,
7104 e->hangup.call);
7105 pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
7106 break;
7107 }
7108 chanpos = pri_find_principle_by_call(pri, e->hangup.call);
7109 if (chanpos < 0) {
7110 /*
7111 * Continue hanging up the call even though
7112 * we do not remember it (if we ever did).
7113 */
7114 pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
7115 break;
7116 }
7117 sig_pri_lock_private(pri->pvts[chanpos]);
7118
7119 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
7120
7121 sig_pri_handle_subcmds(pri, chanpos, e->e, e->hangup.subcmds,
7122 e->hangup.call);
7123 #if defined(HAVE_PRI_CALL_HOLD)
7124 if (e->hangup.call_active && e->hangup.call_held
7125 && pri->hold_disconnect_transfer) {
7126 /* We are to transfer the call instead of simply hanging up. */
7127 sig_pri_unlock_private(pri->pvts[chanpos]);
7128 if (!sig_pri_attempt_transfer(pri, e->hangup.call_held, 1,
7129 e->hangup.call_active, 0, NULL)) {
7130 break;
7131 }
7132 sig_pri_lock_private(pri->pvts[chanpos]);
7133 }
7134 #endif /* defined(HAVE_PRI_CALL_HOLD) */
7135 switch (e->hangup.cause) {
7136 case PRI_CAUSE_USER_BUSY:
7137 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
7138 sig_pri_cc_generic_check(pri, chanpos, AST_CC_CCBS);
7139 break;
7140 case PRI_CAUSE_INVALID_CALL_REFERENCE:
7141 /*
7142 * The peer denies the existence of this call so we must
7143 * continue hanging it up and forget about it. We should not
7144 * get this cause here, but for completeness we will handle it
7145 * anyway.
7146 */
7147 pri_hangup(pri->pri, e->hangup.call, e->hangup.cause);
7148 pri->pvts[chanpos]->call = NULL;
7149 break;
7150 default:
7151 break;
7152 }
7153 if (pri->pvts[chanpos]->owner) {
7154 snprintf(cause_str, sizeof(cause_str), "PRI PRI_EVENT_HANGUP_REQ (%d)", e->hangup.cause);
7155 pri_queue_pvt_cause_data(pri, chanpos, cause_str, e->hangup.cause);
7156 }
7157 if (pri->pvts[chanpos]->owner) {
7158 int do_hangup = 0;
7159
7160 ast_channel_hangupcause_set(pri->pvts[chanpos]->owner, e->hangup.cause);
7161 switch (ast_channel_state(pri->pvts[chanpos]->owner)) {
7162 case AST_STATE_BUSY:
7163 case AST_STATE_UP:
7164 do_hangup = 1;
7165 break;
7166 default:
7167 if (!pri->pvts[chanpos]->outgoing) {
7168 /*
7169 * The incoming call leg hung up before getting
7170 * connected so just hangup the call.
7171 */
7172 do_hangup = 1;
7173 break;
7174 }
7175 switch (e->hangup.cause) {
7176 case PRI_CAUSE_USER_BUSY:
7177 pri_queue_control(pri, chanpos, AST_CONTROL_BUSY);
7178 break;
7179 case PRI_CAUSE_CALL_REJECTED:
7180 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
7181 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
7182 case PRI_CAUSE_SWITCH_CONGESTION:
7183 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
7184 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
7185 pri_queue_control(pri, chanpos, AST_CONTROL_CONGESTION);
7186 break;
7187 default:
7188 do_hangup = 1;
7189 break;
7190 }
7191 break;
7192 }
7193
7194 if (do_hangup) {
7195 #if defined(HAVE_PRI_AOC_EVENTS)
7196 if (!pri->pvts[chanpos]->holding_aoce
7197 && pri->aoce_delayhangup
7198 && ast_channel_is_bridged(pri->pvts[chanpos]->owner)) {
7199 sig_pri_send_aoce_termination_request(pri, chanpos,
7200 pri_get_timer(pri->pri, PRI_TIMER_T305) / 2);
7201 } else
7202 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
7203 {
7204 sig_pri_queue_hangup(pri, chanpos);
7205 }
7206 }
7207 ast_verb(3, "Span %d: Channel %d/%d got hangup request, cause %d\n",
7208 pri->span, pri->pvts[chanpos]->logicalspan,
7209 pri->pvts[chanpos]->prioffset, e->hangup.cause);
7210 } else {
7211 /*
7212 * Continue hanging up the call even though
7213 * we do not have an owner.
7214 */
7215 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
7216 pri->pvts[chanpos]->call = NULL;
7217 }
7218 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL
7219 && pri->sig != SIG_BRI_PTMP && !pri->resetting
7220 && pri->force_restart_unavailable_chans
7221 && pri->pvts[chanpos]->resetting == SIG_PRI_RESET_IDLE) {
7222 ast_verb(3,
7223 "Span %d: Forcing restart of channel %d/%d since channel reported in use\n",
7224 pri->span, pri->pvts[chanpos]->logicalspan,
7225 pri->pvts[chanpos]->prioffset);
7226 pri->pvts[chanpos]->resetting = SIG_PRI_RESET_ACTIVE;
7227 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
7228 }
7229
7230 #ifdef SUPPORT_USERUSER
7231 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
7232 struct ast_channel *owner;
7233
7234 sig_pri_lock_owner(pri, chanpos);
7235 owner = pri->pvts[chanpos]->owner;
7236 if (owner) {
7237 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
7238 e->hangup.useruserinfo);
7239 ast_channel_unlock(owner);
7240 }
7241 }
7242 #endif
7243
7244 sig_pri_unlock_private(pri->pvts[chanpos]);
7245 sig_pri_span_devstate_changed(pri);
7246 break;
7247 case PRI_EVENT_HANGUP_ACK:
7248 if (sig_pri_is_cis_call(e->hangup.channel)) {
7249 sig_pri_handle_cis_subcmds(pri, e->e, e->hangup.subcmds,
7250 e->hangup.call);
7251 break;
7252 }
7253 chanpos = pri_find_principle_by_call(pri, e->hangup.call);
7254 if (chanpos < 0) {
7255 break;
7256 }
7257 sig_pri_lock_private(pri->pvts[chanpos]);
7258
7259 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
7260
7261 pri->pvts[chanpos]->call = NULL;
7262 if (pri->pvts[chanpos]->owner) {
7263 ast_verb(3, "Span %d: Channel %d/%d got hangup ACK\n", pri->span,
7264 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset);
7265 }
7266 #ifdef SUPPORT_USERUSER
7267 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
7268 struct ast_channel *owner;
7269
7270 sig_pri_lock_owner(pri, chanpos);
7271 owner = pri->pvts[chanpos]->owner;
7272 if (owner) {
7273 pbx_builtin_setvar_helper(owner, "USERUSERINFO",
7274 e->hangup.useruserinfo);
7275 ast_channel_unlock(owner);
7276 }
7277 }
7278 #endif
7279 sig_pri_unlock_private(pri->pvts[chanpos]);
7280 sig_pri_span_devstate_changed(pri);
7281 break;
7282 case PRI_EVENT_CONFIG_ERR:
7283 ast_log(LOG_WARNING, "PRI Error on span %d: %s\n", pri->span, e->err.err);
7284 break;
7285 case PRI_EVENT_RESTART_ACK:
7286 chanpos = pri_find_principle(pri, e->restartack.channel, NULL);
7287 if (chanpos < 0) {
7288 /* Sometime switches (e.g. I421 / British Telecom) don't give us the
7289 channel number, so we have to figure it out... This must be why
7290 everybody resets exactly a channel at a time. */
7291 for (x = 0; x < pri->numchans; x++) {
7292 if (pri->pvts[x]
7293 && pri->pvts[x]->resetting != SIG_PRI_RESET_IDLE) {
7294 chanpos = x;
7295 sig_pri_lock_private(pri->pvts[chanpos]);
7296 ast_debug(1,
7297 "Span %d: Assuming restart ack is for channel %d/%d\n",
7298 pri->span, pri->pvts[chanpos]->logicalspan,
7299 pri->pvts[chanpos]->prioffset);
7300 if (pri->pvts[chanpos]->owner) {
7301 ast_log(LOG_WARNING,
7302 "Span %d: Got restart ack on channel %d/%d with owner\n",
7303 pri->span, pri->pvts[chanpos]->logicalspan,
7304 pri->pvts[chanpos]->prioffset);
7305 ast_channel_softhangup_internal_flag_add(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
7306 }
7307 pri->pvts[chanpos]->resetting = SIG_PRI_RESET_IDLE;
7308 ast_verb(3,
7309 "Span %d: Channel %d/%d successfully restarted\n",
7310 pri->span, pri->pvts[chanpos]->logicalspan,
7311 pri->pvts[chanpos]->prioffset);
7312 sig_pri_unlock_private(pri->pvts[chanpos]);
7313 if (pri->resetting)
7314 pri_check_restart(pri);
7315 break;
7316 }
7317 }
7318 if (chanpos < 0) {
7319 ast_log(LOG_WARNING,
7320 "Span %d: Restart ACK on strange channel %d/%d\n",
7321 pri->span, PRI_SPAN(e->restartack.channel),
7322 PRI_CHANNEL(e->restartack.channel));
7323 }
7324 } else {
7325 sig_pri_lock_private(pri->pvts[chanpos]);
7326 if (pri->pvts[chanpos]->resetting == SIG_PRI_RESET_IDLE) {
7327 /* The channel is not in the resetting state. */
7328 ast_debug(1,
7329 "Span %d: Unexpected or late restart ack on channel %d/%d (Ignoring)\n",
7330 pri->span, pri->pvts[chanpos]->logicalspan,
7331 pri->pvts[chanpos]->prioffset);
7332 sig_pri_unlock_private(pri->pvts[chanpos]);
7333 break;
7334 }
7335 if (pri->pvts[chanpos]->owner) {
7336 ast_log(LOG_WARNING,
7337 "Span %d: Got restart ack on channel %d/%d with owner\n",
7338 pri->span, pri->pvts[chanpos]->logicalspan,
7339 pri->pvts[chanpos]->prioffset);
7340 ast_channel_softhangup_internal_flag_add(pri->pvts[chanpos]->owner, AST_SOFTHANGUP_DEV);
7341 }
7342 pri->pvts[chanpos]->resetting = SIG_PRI_RESET_IDLE;
7343 ast_verb(3,
7344 "Span %d: Channel %d/%d successfully restarted\n",
7345 pri->span, pri->pvts[chanpos]->logicalspan,
7346 pri->pvts[chanpos]->prioffset);
7347 sig_pri_unlock_private(pri->pvts[chanpos]);
7348 if (pri->resetting)
7349 pri_check_restart(pri);
7350 }
7351 break;
7352 case PRI_EVENT_SETUP_ACK:
7353 if (sig_pri_is_cis_call(e->setup_ack.channel)) {
7354 sig_pri_handle_cis_subcmds(pri, e->e, e->setup_ack.subcmds,
7355 e->setup_ack.call);
7356 break;
7357 }
7358 chanpos = pri_find_fixup_principle(pri, e->setup_ack.channel,
7359 e->setup_ack.call);
7360 if (chanpos < 0) {
7361 break;
7362 }
7363 sig_pri_lock_private(pri->pvts[chanpos]);
7364
7365 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
7366
7367 sig_pri_handle_subcmds(pri, chanpos, e->e, e->setup_ack.subcmds,
7368 e->setup_ack.call);
7369 if (pri->pvts[chanpos]->call_level < SIG_PRI_CALL_LEVEL_OVERLAP) {
7370 pri->pvts[chanpos]->call_level = SIG_PRI_CALL_LEVEL_OVERLAP;
7371 }
7372
7373 /* Send any queued digits */
7374 len = strlen(pri->pvts[chanpos]->dialdest);
7375 for (x = 0; x < len; ++x) {
7376 ast_debug(1, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
7377 pri_information(pri->pri, pri->pvts[chanpos]->call,
7378 pri->pvts[chanpos]->dialdest[x]);
7379 }
7380
7381 if (!pri->pvts[chanpos]->progress
7382 && (pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING)
7383 && !pri->pvts[chanpos]->digital
7384 && !pri->pvts[chanpos]->no_b_channel
7385 #if defined(HAVE_PRI_SETUP_ACK_INBAND)
7386 /*
7387 * We only care about PRI_PROG_INBAND_AVAILABLE to open the
7388 * voice path.
7389 *
7390 * We explicitly DO NOT want to check PRI_PROG_CALL_NOT_E2E_ISDN
7391 * because it will mess up ISDN to SIP interoperability for
7392 * the ALERTING message.
7393 *
7394 * Q.931 Section 5.1.3 says that in scenarios with overlap
7395 * dialing where no called digits are received and the tone
7396 * option requires dialtone, the switch MAY send an inband
7397 * progress indication ie to indicate dialtone presence in
7398 * the SETUP ACKNOWLEDGE. Therefore, if we did not send any
7399 * digits with the SETUP then we must assume that dialtone
7400 * is present and open the voice path. Fortunately when
7401 * interoperating with SIP, we should be sending digits.
7402 */
7403 && ((e->setup_ack.progressmask & PRI_PROG_INBAND_AVAILABLE)
7404 || pri->inband_on_setup_ack
7405 || pri->pvts[chanpos]->no_dialed_digits)
7406 #endif /* defined(HAVE_PRI_SETUP_ACK_INBAND) */
7407 ) {
7408 /*
7409 * Call has a channel.
7410 * Indicate for overlap dialing that dialtone may be present.
7411 */
7412 pri_queue_control(pri, chanpos, AST_CONTROL_PROGRESS);
7413 pri->pvts[chanpos]->progress = 1;/* Claim to have seen inband-information */
7414 sig_pri_set_dialing(pri->pvts[chanpos], 0);
7415 sig_pri_open_media(pri->pvts[chanpos]);
7416 }
7417 sig_pri_unlock_private(pri->pvts[chanpos]);
7418 break;
7419 case PRI_EVENT_NOTIFY:
7420 if (sig_pri_is_cis_call(e->notify.channel)) {
7421 #if defined(HAVE_PRI_CALL_HOLD)
7422 sig_pri_handle_cis_subcmds(pri, e->e, e->notify.subcmds,
7423 e->notify.call);
7424 #else
7425 sig_pri_handle_cis_subcmds(pri, e->e, e->notify.subcmds, NULL);
7426 #endif /* !defined(HAVE_PRI_CALL_HOLD) */
7427 break;
7428 }
7429 #if defined(HAVE_PRI_CALL_HOLD)
7430 chanpos = pri_find_principle_by_call(pri, e->notify.call);
7431 if (chanpos < 0) {
7432 ast_log(LOG_WARNING, "Span %d: Received NOTIFY for unknown call.\n",
7433 pri->span);
7434 break;
7435 }
7436 #else
7437 /*
7438 * This version of libpri does not supply a call pointer for
7439 * this message. We are just going to have to trust that the
7440 * correct principle is found.
7441 */
7442 chanpos = pri_find_principle(pri, e->notify.channel, NULL);
7443 if (chanpos < 0) {
7444 ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
7445 PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
7446 break;
7447 }
7448 #endif /* !defined(HAVE_PRI_CALL_HOLD) */
7449 sig_pri_lock_private(pri->pvts[chanpos]);
7450
7451 callid = func_pri_dchannel_chanpos_callid(pri, chanpos);
7452
7453 #if defined(HAVE_PRI_CALL_HOLD)
7454 sig_pri_handle_subcmds(pri, chanpos, e->e, e->notify.subcmds,
7455 e->notify.call);
7456 #else
7457 sig_pri_handle_subcmds(pri, chanpos, e->e, e->notify.subcmds, NULL);
7458 #endif /* !defined(HAVE_PRI_CALL_HOLD) */
7459 switch (e->notify.info) {
7460 case PRI_NOTIFY_REMOTE_HOLD:
7461 if (!pri->discardremoteholdretrieval) {
7462 sig_pri_queue_hold(pri, chanpos);
7463 }
7464 break;
7465 case PRI_NOTIFY_REMOTE_RETRIEVAL:
7466 if (!pri->discardremoteholdretrieval) {
7467 sig_pri_queue_unhold(pri, chanpos);
7468 }
7469 break;
7470 }
7471 sig_pri_unlock_private(pri->pvts[chanpos]);
7472 break;
7473 #if defined(HAVE_PRI_CALL_HOLD)
7474 case PRI_EVENT_HOLD:
7475 /* We should not be getting any CIS calls with this message type. */
7476 if (sig_pri_handle_hold(pri, e)) {
7477 pri_hold_rej(pri->pri, e->hold.call,
7478 PRI_CAUSE_RESOURCE_UNAVAIL_UNSPECIFIED);
7479 } else {
7480 pri_hold_ack(pri->pri, e->hold.call);
7481 }
7482 break;
7483 #endif /* defined(HAVE_PRI_CALL_HOLD) */
7484 #if defined(HAVE_PRI_CALL_HOLD)
7485 case PRI_EVENT_HOLD_ACK:
7486 /* We should not be getting any CIS calls with this message type. */
7487 sig_pri_handle_hold_ack(pri, e);
7488 break;
7489 #endif /* defined(HAVE_PRI_CALL_HOLD) */
7490 #if defined(HAVE_PRI_CALL_HOLD)
7491 case PRI_EVENT_HOLD_REJ:
7492 /* We should not be getting any CIS calls with this message type. */
7493 sig_pri_handle_hold_rej(pri, e);
7494 break;
7495 #endif /* defined(HAVE_PRI_CALL_HOLD) */
7496 #if defined(HAVE_PRI_CALL_HOLD)
7497 case PRI_EVENT_RETRIEVE:
7498 /* We should not be getting any CIS calls with this message type. */
7499 sig_pri_handle_retrieve(pri, e);
7500 break;
7501 #endif /* defined(HAVE_PRI_CALL_HOLD) */
7502 #if defined(HAVE_PRI_CALL_HOLD)
7503 case PRI_EVENT_RETRIEVE_ACK:
7504 /* We should not be getting any CIS calls with this message type. */
7505 sig_pri_handle_retrieve_ack(pri, e);
7506 break;
7507 #endif /* defined(HAVE_PRI_CALL_HOLD) */
7508 #if defined(HAVE_PRI_CALL_HOLD)
7509 case PRI_EVENT_RETRIEVE_REJ:
7510 /* We should not be getting any CIS calls with this message type. */
7511 sig_pri_handle_retrieve_rej(pri, e);
7512 break;
7513 #endif /* defined(HAVE_PRI_CALL_HOLD) */
7514 default:
7515 ast_debug(1, "Span: %d Unhandled event: %s(%d)\n",
7516 pri->span, pri_event2str(e->e), e->e);
7517 break;
7518 }
7519
7520 /* If a callid was set, we need to remove it from thread storage. */
7521 if (callid) {
7522 ast_callid_threadassoc_remove();
7523 }
7524 }
7525 ast_mutex_unlock(&pri->lock);
7526 }
7527 /* Never reached */
7528 return NULL;
7529 }
7530
7531 /*!
7532 * \brief Output AMI show spans response events for the given PRI span.
7533 * \since 10.0
7534 *
7535 * \param show_cmd AMI command name
7536 * \param s AMI session to output span information.
7537 * \param pri PRI span control structure.
7538 * \param dchannels Array of D channel channel numbers.
7539 * \param action_id Action ID line to use.
7540 *
7541 * \return Number of D channels on this span.
7542 */
7543 int sig_pri_ami_show_spans(struct mansession *s, const char *show_cmd, struct sig_pri_span *pri, const int *dchannels, const char *action_id)
7544 {
7545 int count;
7546 int x;
7547
7548 count = 0;
7549 for (x = 0; x < ARRAY_LEN(pri->dchans); ++x) {
7550 if (pri->dchans[x]) {
7551 ++count;
7552
7553 astman_append(s,
7554 "Event: %s\r\n"
7555 "Span: %d\r\n"
7556 "DChannel: %d\r\n"
7557 "Order: %s\r\n"
7558 "Active: %s\r\n"
7559 "Alarm: %s\r\n"
7560 "Up: %s\r\n"
7561 "%s"
7562 "\r\n",
7563 show_cmd,
7564 pri->span,
7565 dchannels[x],
7566 pri_order(x),
7567 (pri->dchans[x] == pri->pri) ? "Yes" : "No",
7568 (pri->dchanavail[x] & DCHAN_NOTINALARM) ? "No" : "Yes",
7569 (pri->dchanavail[x] & DCHAN_UP) ? "Yes" : "No",
7570 action_id
7571 );
7572 }
7573 }
7574 return count;
7575 }
7576
7577 void sig_pri_init_pri(struct sig_pri_span *pri)
7578 {
7579 int i;
7580
7581 memset(pri, 0, sizeof(*pri));
7582
7583 ast_mutex_init(&pri->lock);
7584
7585 pri->master = AST_PTHREADT_NULL;
7586 for (i = 0; i < SIG_PRI_NUM_DCHANS; i++)
7587 pri->fds[i] = -1;
7588 }
7589
7590 int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast)
7591 {
7592 ast_debug(1, "%s %d\n", __FUNCTION__, p->channel);
7593 if (!ast_channel_tech_pvt(ast)) {
7594 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
7595 return 0;
7596 }
7597
7598 sig_pri_set_outgoing(p, 0);
7599 sig_pri_set_digital(p, 0); /* push up to parent for EC*/
7600 #if defined(HAVE_PRI_CALL_WAITING)
7601 if (p->is_call_waiting) {
7602 p->is_call_waiting = 0;
7603 ast_atomic_fetchadd_int(&p->pri->num_call_waiting_calls, -1);
7604 }
7605 #endif /* defined(HAVE_PRI_CALL_WAITING) */
7606 p->call_level = SIG_PRI_CALL_LEVEL_IDLE;
7607 p->progress = 0;
7608 p->cid_num[0] = '\0';
7609 p->cid_subaddr[0] = '\0';
7610 p->cid_name[0] = '\0';
7611 p->user_tag[0] = '\0';
7612 p->exten[0] = '\0';
7613 sig_pri_set_dialing(p, 0);
7614
7615 /* Make sure we really have a call */
7616 pri_grab(p, p->pri);
7617 sig_pri_moh_fsm_event(ast, p, SIG_PRI_MOH_EVENT_RESET);
7618 if (p->call) {
7619 #if defined(SUPPORT_USERUSER)
7620 const char *useruser = pbx_builtin_getvar_helper(ast, "USERUSERINFO");
7621
7622 if (!ast_strlen_zero(useruser)) {
7623 pri_call_set_useruser(p->call, useruser);
7624 }
7625 #endif /* defined(SUPPORT_USERUSER) */
7626
7627 #if defined(HAVE_PRI_TRANSFER)
7628 if (p->xfer_data) {
7629 /*
7630 * The transferrer call leg is disconnecting. It must mean that
7631 * the transfer was successful and the core is disconnecting the
7632 * call legs involved.
7633 *
7634 * The transfer protocol response message must go out before the
7635 * call leg is disconnected.
7636 */
7637 sig_pri_transfer_rsp(p->xfer_data, 1);
7638 }
7639 #endif /* defined(HAVE_PRI_TRANSFER) */
7640
7641 #if defined(HAVE_PRI_AOC_EVENTS)
7642 if (p->holding_aoce) {
7643 pri_aoc_e_send(p->pri->pri, p->call, &p->aoc_e);
7644 }
7645 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
7646
7647 if (p->alreadyhungup) {
7648 ast_debug(1, "Already hungup... Calling hangup once, and clearing call\n");
7649
7650 pri_hangup(p->pri->pri, p->call, -1);
7651 p->call = NULL;
7652 } else {
7653 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
7654 int icause = ast_channel_hangupcause(ast) ? ast_channel_hangupcause(ast) : -1;
7655
7656 p->alreadyhungup = 1;
7657 if (!ast_strlen_zero(cause)) {
7658 if (atoi(cause)) {
7659 icause = atoi(cause);
7660 }
7661 }
7662 ast_debug(1,
7663 "Not yet hungup... Calling hangup with cause %d, and clearing call\n",
7664 icause);
7665
7666 pri_hangup(p->pri->pri, p->call, icause);
7667 }
7668 }
7669 #if defined(HAVE_PRI_TRANSFER)
7670 p->xfer_data = NULL;
7671 #endif /* defined(HAVE_PRI_TRANSFER) */
7672 #if defined(HAVE_PRI_AOC_EVENTS)
7673 p->aoc_s_request_invoke_id_valid = 0;
7674 p->holding_aoce = 0;
7675 p->waiting_for_aoce = 0;
7676 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
7677
7678 p->allocated = 0;
7679 p->owner = NULL;
7680
7681 sig_pri_span_devstate_changed(p->pri);
7682 pri_rel(p->pri);
7683 return 0;
7684 }
7685
7686 /*!
7687 * \brief Extract the called number and subaddress from the dial string.
7688 * \since 1.8
7689 *
7690 * \param p sig_pri channel structure.
7691 * \param rdest Dial string buffer to extract called number and subaddress.
7692 * \param called Buffer to fill with extracted \<number\>[:\<subaddress\>]
7693 * \param called_buff_size Size of buffer to fill.
7694 *
7695 * \note Parsing must remain in sync with sig_pri_call().
7696 */
7697 void sig_pri_extract_called_num_subaddr(struct sig_pri_chan *p, const char *rdest, char *called, size_t called_buff_size)
7698 {
7699 char *dial;
7700 char *number;
7701 char *subaddr;
7702 AST_DECLARE_APP_ARGS(args,
7703 AST_APP_ARG(group); /* channel/group token */
7704 AST_APP_ARG(ext); /* extension token */
7705 //AST_APP_ARG(opts); /* options token */
7706 AST_APP_ARG(other); /* Any remaining unused arguments */
7707 );
7708
7709 /* Get private copy of dial string and break it up. */
7710 dial = ast_strdupa(rdest);
7711 AST_NONSTANDARD_APP_ARGS(args, dial, '/');
7712
7713 number = args.ext;
7714 if (!number) {
7715 number = "";
7716 }
7717
7718 /* Find and extract dialed_subaddress */
7719 subaddr = strchr(number, ':');
7720 if (subaddr) {
7721 *subaddr++ = '\0';
7722
7723 /* Skip subaddress type prefix. */
7724 switch (*subaddr) {
7725 case 'U':
7726 case 'u':
7727 case 'N':
7728 case 'n':
7729 ++subaddr;
7730 break;
7731 default:
7732 break;
7733 }
7734 }
7735
7736 /* Skip type-of-number/dial-plan prefix characters. */
7737 if (strlen(number) < p->stripmsd) {
7738 number = "";
7739 } else {
7740 char *deferred;
7741
7742 number += p->stripmsd;
7743 deferred = strchr(number, 'w');
7744 if (deferred) {
7745 /* Remove any 'w' deferred digits. */
7746 *deferred = '\0';
7747 }
7748 while (isalpha(*number)) {
7749 ++number;
7750 }
7751 }
7752
7753 /* Fill buffer with extracted number and subaddress. */
7754 if (ast_strlen_zero(subaddr)) {
7755 /* Put in called number only since there is no subaddress. */
7756 snprintf(called, called_buff_size, "%s", number);
7757 } else {
7758 /* Put in called number and subaddress. */
7759 snprintf(called, called_buff_size, "%s:%s", number, subaddr);
7760 }
7761 }
7762
7763 enum SIG_PRI_CALL_OPT_FLAGS {
7764 OPT_KEYPAD = (1 << 0),
7765 OPT_REVERSE_CHARGE = (1 << 1), /* Collect call */
7766 OPT_AOC_REQUEST = (1 << 2), /* AOC Request */
7767 };
7768 enum SIG_PRI_CALL_OPT_ARGS {
7769 OPT_ARG_KEYPAD = 0,
7770 OPT_ARG_AOC_REQUEST,
7771
7772 /* note: this entry _MUST_ be the last one in the enum */
7773 OPT_ARG_ARRAY_SIZE,
7774 };
7775
7776 AST_APP_OPTIONS(sig_pri_call_opts, BEGIN_OPTIONS
7777 AST_APP_OPTION_ARG('K', OPT_KEYPAD, OPT_ARG_KEYPAD),
7778 AST_APP_OPTION('R', OPT_REVERSE_CHARGE),
7779 AST_APP_OPTION_ARG('A', OPT_AOC_REQUEST, OPT_ARG_AOC_REQUEST),
7780 END_OPTIONS);
7781
7782 /*! \note Parsing must remain in sync with sig_pri_extract_called_num_subaddr(). */
7783 int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, const char *rdest, int timeout, int layer1)
7784 {
7785 char dest[256]; /* must be same length as p->dialdest */
7786 struct ast_party_subaddress dialed_subaddress; /* Called subaddress */
7787 struct pri_sr *sr;
7788 char *c, *l, *n, *s;
7789 #ifdef SUPPORT_USERUSER
7790 const char *useruser;
7791 #endif
7792 int core_id;
7793 int pridialplan;
7794 int dp_strip;
7795 int prilocaldialplan;
7796 int ldp_strip;
7797 int exclusive;
7798 #if defined(HAVE_PRI_SETUP_KEYPAD)
7799 const char *keypad;
7800 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
7801 AST_DECLARE_APP_ARGS(args,
7802 AST_APP_ARG(group); /* channel/group token */
7803 AST_APP_ARG(ext); /* extension token */
7804 AST_APP_ARG(opts); /* options token */
7805 AST_APP_ARG(other); /* Any remaining unused arguments */
7806 );
7807 struct ast_flags opts;
7808 char *opt_args[OPT_ARG_ARRAY_SIZE];
7809 struct ast_party_id connected_id = ast_channel_connected_effective_id(ast);
7810
7811 ast_debug(1, "CALLER NAME: %s NUM: %s\n",
7812 S_COR(connected_id.name.valid, connected_id.name.str, ""),
7813 S_COR(connected_id.number.valid, connected_id.number.str, ""));
7814
7815 if (!p->pri) {
7816 ast_log(LOG_ERROR, "Could not find pri on channel %d\n", p->channel);
7817 return -1;
7818 }
7819
7820 if ((ast_channel_state(ast) != AST_STATE_DOWN) && (ast_channel_state(ast) != AST_STATE_RESERVED)) {
7821 ast_log(LOG_WARNING, "sig_pri_call called on %s, neither down nor reserved\n", ast_channel_name(ast));
7822 return -1;
7823 }
7824
7825 p->dialdest[0] = '\0';
7826 sig_pri_set_outgoing(p, 1);
7827
7828 ast_copy_string(dest, rdest, sizeof(dest));
7829 AST_NONSTANDARD_APP_ARGS(args, dest, '/');
7830 if (ast_app_parse_options(sig_pri_call_opts, &opts, opt_args, args.opts)) {
7831 /* General invalid option syntax. */
7832 return -1;
7833 }
7834
7835 c = args.ext;
7836 if (!c) {
7837 c = "";
7838 }
7839
7840 /* setup dialed_subaddress if found */
7841 ast_party_subaddress_init(&dialed_subaddress);
7842 s = strchr(c, ':');
7843 if (s) {
7844 *s = '\0';
7845 s++;
7846 /* prefix */
7847 /* 'n' = NSAP */
7848 /* 'u' = User Specified */
7849 /* Default = NSAP */
7850 switch (*s) {
7851 case 'U':
7852 case 'u':
7853 s++;
7854 dialed_subaddress.type = 2;
7855 break;
7856 case 'N':
7857 case 'n':
7858 s++;
7859 /* default already covered with ast_party_subaddress_init */
7860 break;
7861 }
7862 dialed_subaddress.str = s;
7863 dialed_subaddress.valid = 1;
7864 }
7865
7866 l = NULL;
7867 n = NULL;
7868 if (!p->hidecallerid) {
7869 if (connected_id.number.valid) {
7870 /* If we get to the end of this loop without breaking, there's no
7871 * calleridnum. This is done instead of testing for "unknown" or
7872 * the thousands of other ways that the calleridnum could be
7873 * invalid. */
7874 for (l = connected_id.number.str; l && *l; l++) {
7875 if (strchr("0123456789", *l)) {
7876 l = connected_id.number.str;
7877 break;
7878 }
7879 }
7880 } else {
7881 l = NULL;
7882 }
7883 if (!p->hidecalleridname) {
7884 n = connected_id.name.valid ? connected_id.name.str : NULL;
7885 }
7886 }
7887
7888 if (strlen(c) < p->stripmsd) {
7889 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
7890 return -1;
7891 }
7892
7893 /* Extract any 'w' deferred digits. */
7894 s = strchr(c + p->stripmsd, 'w');
7895 if (s) {
7896 *s++ = '\0';
7897 ast_copy_string(p->deferred_digits, s, sizeof(p->deferred_digits));
7898 /*
7899 * Since we have a 'w', this means that there will not be any
7900 * more normal dialed digits. Therefore, the sending complete
7901 * ie needs to be sent with any normal digits.
7902 */
7903 } else {
7904 p->deferred_digits[0] = '\0';
7905 }
7906
7907 pri_grab(p, p->pri);
7908 if (!(p->call = pri_new_call(p->pri->pri))) {
7909 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
7910 pri_rel(p->pri);
7911 return -1;
7912 }
7913 if (!(sr = pri_sr_new())) {
7914 ast_log(LOG_WARNING, "Failed to allocate setup request on channel %d\n",
7915 p->channel);
7916 pri_destroycall(p->pri->pri, p->call);
7917 p->call = NULL;
7918 pri_rel(p->pri);
7919 return -1;
7920 }
7921
7922 sig_pri_set_digital(p, IS_DIGITAL(ast_channel_transfercapability(ast))); /* push up to parent for EC */
7923
7924 #if defined(HAVE_PRI_CALL_WAITING)
7925 if (p->is_call_waiting) {
7926 /*
7927 * Indicate that this is a call waiting call.
7928 * i.e., Normal call but with no B channel.
7929 */
7930 pri_sr_set_channel(sr, 0, 0, 1);
7931 } else
7932 #endif /* defined(HAVE_PRI_CALL_WAITING) */
7933 {
7934 /* Should the picked channel be used exclusively? */
7935 if (p->priexclusive || p->pri->nodetype == PRI_NETWORK) {
7936 exclusive = 1;
7937 } else {
7938 exclusive = 0;
7939 }
7940 pri_sr_set_channel(sr, PVT_TO_CHANNEL(p), exclusive, 1);
7941 }
7942
7943 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast_channel_transfercapability(ast),
7944 (p->digital ? -1 : layer1));
7945
7946 if (p->pri->facilityenable)
7947 pri_facility_enable(p->pri->pri);
7948
7949 ast_verb(3, "Requested transfer capability: 0x%02hx - %s\n", ast_channel_transfercapability(ast), ast_transfercapability2str(ast_channel_transfercapability(ast)));
7950 dp_strip = 0;
7951 pridialplan = p->pri->dialplan - 1;
7952 if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
7953 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
7954 if (pridialplan == -2) {
7955 dp_strip = strlen(p->pri->internationalprefix);
7956 }
7957 pridialplan = PRI_INTERNATIONAL_ISDN;
7958 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
7959 if (pridialplan == -2) {
7960 dp_strip = strlen(p->pri->nationalprefix);
7961 }
7962 pridialplan = PRI_NATIONAL_ISDN;
7963 } else {
7964 pridialplan = PRI_LOCAL_ISDN;
7965 }
7966 }
7967 while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
7968 switch (c[p->stripmsd]) {
7969 case 'U':
7970 pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
7971 break;
7972 case 'I':
7973 pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
7974 break;
7975 case 'N':
7976 pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
7977 break;
7978 case 'L':
7979 pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
7980 break;
7981 case 'S':
7982 pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
7983 break;
7984 case 'V':
7985 pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
7986 break;
7987 case 'R':
7988 pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
7989 break;
7990 case 'u':
7991 pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
7992 break;
7993 case 'e':
7994 pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
7995 break;
7996 case 'x':
7997 pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
7998 break;
7999 case 'f':
8000 pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
8001 break;
8002 case 'n':
8003 pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
8004 break;
8005 case 'p':
8006 pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
8007 break;
8008 case 'r':
8009 pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
8010 break;
8011 default:
8012 if (isalpha(c[p->stripmsd])) {
8013 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n",
8014 c[p->stripmsd] > 'Z' ? "NPI" : "TON", c[p->stripmsd]);
8015 }
8016 break;
8017 }
8018 c++;
8019 }
8020 #if defined(HAVE_PRI_SETUP_KEYPAD)
8021 if (ast_test_flag(&opts, OPT_KEYPAD)
8022 && !ast_strlen_zero(opt_args[OPT_ARG_KEYPAD])) {
8023 /* We have a keypad facility digits option with digits. */
8024 keypad = opt_args[OPT_ARG_KEYPAD];
8025 pri_sr_set_keypad_digits(sr, keypad);
8026 } else {
8027 keypad = NULL;
8028 }
8029 if (!keypad || !ast_strlen_zero(c + p->stripmsd + dp_strip))
8030 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
8031 {
8032 char *called = c + p->stripmsd + dp_strip;
8033
8034 pri_sr_set_called(sr, called, pridialplan, s ? 1 : 0);
8035 #if defined(HAVE_PRI_SETUP_ACK_INBAND)
8036 p->no_dialed_digits = !called[0];
8037 #endif /* defined(HAVE_PRI_SETUP_ACK_INBAND) */
8038 }
8039
8040 #if defined(HAVE_PRI_SUBADDR)
8041 if (dialed_subaddress.valid) {
8042 struct pri_party_subaddress subaddress;
8043
8044 memset(&subaddress, 0, sizeof(subaddress));
8045 sig_pri_party_subaddress_from_ast(&subaddress, &dialed_subaddress);
8046 pri_sr_set_called_subaddress(sr, &subaddress);
8047 }
8048 #endif /* defined(HAVE_PRI_SUBADDR) */
8049 #if defined(HAVE_PRI_REVERSE_CHARGE)
8050 if (ast_test_flag(&opts, OPT_REVERSE_CHARGE)) {
8051 pri_sr_set_reversecharge(sr, PRI_REVERSECHARGE_REQUESTED);
8052 }
8053 #endif /* defined(HAVE_PRI_REVERSE_CHARGE) */
8054 #if defined(HAVE_PRI_AOC_EVENTS)
8055 if (ast_test_flag(&opts, OPT_AOC_REQUEST)
8056 && !ast_strlen_zero(opt_args[OPT_ARG_AOC_REQUEST])) {
8057 if (strchr(opt_args[OPT_ARG_AOC_REQUEST], 's')) {
8058 pri_sr_set_aoc_charging_request(sr, PRI_AOC_REQUEST_S);
8059 }
8060 if (strchr(opt_args[OPT_ARG_AOC_REQUEST], 'd')) {
8061 pri_sr_set_aoc_charging_request(sr, PRI_AOC_REQUEST_D);
8062 }
8063 if (strchr(opt_args[OPT_ARG_AOC_REQUEST], 'e')) {
8064 pri_sr_set_aoc_charging_request(sr, PRI_AOC_REQUEST_E);
8065 }
8066 }
8067 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
8068
8069 /* Setup the user tag for party id's from this device for this call. */
8070 if (p->pri->append_msn_to_user_tag) {
8071 snprintf(p->user_tag, sizeof(p->user_tag), "%s_%s", p->pri->initial_user_tag,
8072 p->pri->nodetype == PRI_NETWORK
8073 ? c + p->stripmsd + dp_strip
8074 : S_COR(ast_channel_connected(ast)->id.number.valid,
8075 ast_channel_connected(ast)->id.number.str, ""));
8076 } else {
8077 ast_copy_string(p->user_tag, p->pri->initial_user_tag, sizeof(p->user_tag));
8078 }
8079
8080 /*
8081 * Replace the caller id tag from the channel creation
8082 * with the actual tag value.
8083 */
8084 ast_free(ast_channel_caller(ast)->id.tag);
8085 ast_channel_caller(ast)->id.tag = ast_strdup(p->user_tag);
8086
8087 ldp_strip = 0;
8088 prilocaldialplan = p->pri->localdialplan - 1;
8089 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
8090 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
8091 if (prilocaldialplan == -2) {
8092 ldp_strip = strlen(p->pri->internationalprefix);
8093 }
8094 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
8095 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
8096 if (prilocaldialplan == -2) {
8097 ldp_strip = strlen(p->pri->nationalprefix);
8098 }
8099 prilocaldialplan = PRI_NATIONAL_ISDN;
8100 } else {
8101 prilocaldialplan = PRI_LOCAL_ISDN;
8102 }
8103 } else if (prilocaldialplan == -1) {
8104 /* Use the numbering plan passed in. */
8105 prilocaldialplan = connected_id.number.plan;
8106 }
8107 if (l != NULL) {
8108 while (*l > '9' && *l != '*' && *l != '#') {
8109 switch (*l) {
8110 case 'U':
8111 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
8112 break;
8113 case 'I':
8114 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
8115 break;
8116 case 'N':
8117 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
8118 break;
8119 case 'L':
8120 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
8121 break;
8122 case 'S':
8123 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
8124 break;
8125 case 'V':
8126 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
8127 break;
8128 case 'R':
8129 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
8130 break;
8131 case 'u':
8132 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
8133 break;
8134 case 'e':
8135 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
8136 break;
8137 case 'x':
8138 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
8139 break;
8140 case 'f':
8141 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
8142 break;
8143 case 'n':
8144 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
8145 break;
8146 case 'p':
8147 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
8148 break;
8149 case 'r':
8150 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
8151 break;
8152 default:
8153 if (isalpha(*l)) {
8154 ast_log(LOG_WARNING,
8155 "Unrecognized prilocaldialplan %s modifier: %c\n",
8156 *l > 'Z' ? "NPI" : "TON", *l);
8157 }
8158 break;
8159 }
8160 l++;
8161 }
8162 }
8163 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
8164 p->use_callingpres ? connected_id.number.presentation : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
8165
8166 #if defined(HAVE_PRI_SUBADDR)
8167 if (connected_id.subaddress.valid) {
8168 struct pri_party_subaddress subaddress;
8169
8170 memset(&subaddress, 0, sizeof(subaddress));
8171 sig_pri_party_subaddress_from_ast(&subaddress, &connected_id.subaddress);
8172 pri_sr_set_caller_subaddress(sr, &subaddress);
8173 }
8174 #endif /* defined(HAVE_PRI_SUBADDR) */
8175
8176 sig_pri_redirecting_update(p, ast);
8177
8178 #ifdef SUPPORT_USERUSER
8179 /* User-user info */
8180 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
8181 if (useruser)
8182 pri_sr_set_useruser(sr, useruser);
8183 #endif
8184
8185 #if defined(HAVE_PRI_CCSS)
8186 if (ast_cc_is_recall(ast, &core_id, sig_pri_cc_type_name)) {
8187 struct ast_cc_monitor *monitor;
8188 char device_name[AST_CHANNEL_NAME];
8189
8190 /* This is a CC recall call. */
8191 ast_channel_get_device_name(ast, device_name, sizeof(device_name));
8192 monitor = ast_cc_get_monitor_by_recall_core_id(core_id, device_name);
8193 if (monitor) {
8194 struct sig_pri_cc_monitor_instance *instance;
8195
8196 instance = monitor->private_data;
8197
8198 /* If this fails then we have monitor instance ambiguity. */
8199 ast_assert(p->pri == instance->pri);
8200
8201 if (pri_cc_call(p->pri->pri, instance->cc_id, p->call, sr)) {
8202 /* The CC recall call failed for some reason. */
8203 ast_log(LOG_WARNING, "Unable to setup CC recall call to device %s\n",
8204 device_name);
8205 ao2_ref(monitor, -1);
8206 pri_destroycall(p->pri->pri, p->call);
8207 p->call = NULL;
8208 pri_rel(p->pri);
8209 pri_sr_free(sr);
8210 return -1;
8211 }
8212 ao2_ref(monitor, -1);
8213 } else {
8214 core_id = -1;
8215 }
8216 } else
8217 #endif /* defined(HAVE_PRI_CCSS) */
8218 {
8219 core_id = -1;
8220 }
8221 if (core_id == -1 && pri_setup(p->pri->pri, p->call, sr)) {
8222 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
8223 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
8224 pri_destroycall(p->pri->pri, p->call);
8225 p->call = NULL;
8226 pri_rel(p->pri);
8227 pri_sr_free(sr);
8228 return -1;
8229 }
8230 p->call_level = SIG_PRI_CALL_LEVEL_SETUP;
8231 pri_sr_free(sr);
8232 ast_setstate(ast, AST_STATE_DIALING);
8233 sig_pri_set_dialing(p, 1);
8234 pri_rel(p->pri);
8235 return 0;
8236 }
8237
8238 int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen)
8239 {
8240 int res = -1;
8241
8242 switch (condition) {
8243 case AST_CONTROL_BUSY:
8244 if (p->priindication_oob || p->no_b_channel) {
8245 ast_channel_hangupcause_set(chan, AST_CAUSE_USER_BUSY);
8246 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
8247 res = 0;
8248 break;
8249 }
8250 res = sig_pri_play_tone(p, SIG_PRI_TONE_BUSY);
8251 if (p->call_level < SIG_PRI_CALL_LEVEL_ALERTING && !p->outgoing) {
8252 ast_channel_hangupcause_set(chan, AST_CAUSE_USER_BUSY);
8253 p->progress = 1;/* No need to send plain PROGRESS after this. */
8254 if (p->pri && p->pri->pri) {
8255 pri_grab(p, p->pri);
8256 #ifdef HAVE_PRI_PROG_W_CAUSE
8257 pri_progress_with_cause(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 1, ast_channel_hangupcause(chan));
8258 #else
8259 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
8260 #endif
8261 pri_rel(p->pri);
8262 }
8263 }
8264 break;
8265 case AST_CONTROL_RINGING:
8266 if (p->call_level < SIG_PRI_CALL_LEVEL_ALERTING && !p->outgoing) {
8267 p->call_level = SIG_PRI_CALL_LEVEL_ALERTING;
8268 if (p->pri && p->pri->pri) {
8269 pri_grab(p, p->pri);
8270 pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p),
8271 p->no_b_channel || p->digital ? 0 : 1);
8272 pri_rel(p->pri);
8273 }
8274 }
8275 res = sig_pri_play_tone(p, SIG_PRI_TONE_RINGTONE);
8276 if (ast_channel_state(chan) != AST_STATE_UP) {
8277 if (ast_channel_state(chan) != AST_STATE_RING)
8278 ast_setstate(chan, AST_STATE_RINGING);
8279 }
8280 break;
8281 case AST_CONTROL_PROCEEDING:
8282 ast_debug(1, "Received AST_CONTROL_PROCEEDING on %s\n",ast_channel_name(chan));
8283 if (p->call_level < SIG_PRI_CALL_LEVEL_PROCEEDING && !p->outgoing) {
8284 p->call_level = SIG_PRI_CALL_LEVEL_PROCEEDING;
8285 if (p->pri && p->pri->pri) {
8286 pri_grab(p, p->pri);
8287 pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 0);
8288 pri_rel(p->pri);
8289 }
8290 }
8291 /* don't continue in ast_indicate */
8292 res = 0;
8293 break;
8294 case AST_CONTROL_PROGRESS:
8295 ast_debug(1, "Received AST_CONTROL_PROGRESS on %s\n",ast_channel_name(chan));
8296 sig_pri_set_digital(p, 0); /* Digital-only calls isn't allowing any inband progress messages */
8297 if (!p->progress && p->call_level < SIG_PRI_CALL_LEVEL_ALERTING && !p->outgoing
8298 && !p->no_b_channel) {
8299 p->progress = 1;/* No need to send plain PROGRESS again. */
8300 if (p->pri && p->pri->pri) {
8301 pri_grab(p, p->pri);
8302 #ifdef HAVE_PRI_PROG_W_CAUSE
8303 pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, -1); /* no cause at all */
8304 #else
8305 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
8306 #endif
8307 pri_rel(p->pri);
8308 }
8309 }
8310 /* don't continue in ast_indicate */
8311 res = 0;
8312 break;
8313 case AST_CONTROL_INCOMPLETE:
8314 /* If we are connected or if we support overlap dialing, wait for additional digits */
8315 if (p->call_level == SIG_PRI_CALL_LEVEL_CONNECT || (p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
8316 res = 0;
8317 break;
8318 }
8319 /* Otherwise, treat as congestion */
8320 ast_channel_hangupcause_set(chan, AST_CAUSE_INVALID_NUMBER_FORMAT);
8321 /* Falls through */
8322 case AST_CONTROL_CONGESTION:
8323 if (p->priindication_oob || p->no_b_channel) {
8324 /* There are many cause codes that generate an AST_CONTROL_CONGESTION. */
8325 switch (ast_channel_hangupcause(chan)) {
8326 case AST_CAUSE_USER_BUSY:
8327 case AST_CAUSE_NORMAL_CLEARING:
8328 case 0:/* Cause has not been set. */
8329 /* Supply a more appropriate cause. */
8330 ast_channel_hangupcause_set(chan, AST_CAUSE_SWITCH_CONGESTION);
8331 break;
8332 default:
8333 break;
8334 }
8335 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
8336 res = 0;
8337 break;
8338 }
8339 res = sig_pri_play_tone(p, SIG_PRI_TONE_CONGESTION);
8340 if (p->call_level < SIG_PRI_CALL_LEVEL_ALERTING && !p->outgoing) {
8341 /* There are many cause codes that generate an AST_CONTROL_CONGESTION. */
8342 switch (ast_channel_hangupcause(chan)) {
8343 case AST_CAUSE_USER_BUSY:
8344 case AST_CAUSE_NORMAL_CLEARING:
8345 case 0:/* Cause has not been set. */
8346 /* Supply a more appropriate cause. */
8347 ast_channel_hangupcause_set(chan, AST_CAUSE_SWITCH_CONGESTION);
8348 break;
8349 default:
8350 break;
8351 }
8352 p->progress = 1;/* No need to send plain PROGRESS after this. */
8353 if (p->pri && p->pri->pri) {
8354 pri_grab(p, p->pri);
8355 #ifdef HAVE_PRI_PROG_W_CAUSE
8356 pri_progress_with_cause(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 1, ast_channel_hangupcause(chan));
8357 #else
8358 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
8359 #endif
8360 pri_rel(p->pri);
8361 }
8362 }
8363 break;
8364 case AST_CONTROL_HOLD:
8365 ast_copy_string(p->moh_suggested, S_OR(data, ""), sizeof(p->moh_suggested));
8366 if (p->pri) {
8367 pri_grab(p, p->pri);
8368 sig_pri_moh_fsm_event(chan, p, SIG_PRI_MOH_EVENT_HOLD);
8369 pri_rel(p->pri);
8370 } else {
8371 /* Something is wrong here. A PRI channel without the pri pointer? */
8372 ast_moh_start(chan, data, p->mohinterpret);
8373 }
8374 break;
8375 case AST_CONTROL_UNHOLD:
8376 if (p->pri) {
8377 pri_grab(p, p->pri);
8378 sig_pri_moh_fsm_event(chan, p, SIG_PRI_MOH_EVENT_UNHOLD);
8379 pri_rel(p->pri);
8380 } else {
8381 /* Something is wrong here. A PRI channel without the pri pointer? */
8382 ast_moh_stop(chan);
8383 }
8384 break;
8385 case AST_CONTROL_SRCUPDATE:
8386 res = 0;
8387 break;
8388 case -1:
8389 res = sig_pri_play_tone(p, -1);
8390 break;
8391 case AST_CONTROL_CONNECTED_LINE:
8392 ast_debug(1, "Received AST_CONTROL_CONNECTED_LINE on %s\n", ast_channel_name(chan));
8393 if (p->pri) {
8394 struct pri_party_connected_line connected;
8395 int dialplan;
8396 int prefix_strip;
8397 int colp_allowed = 0;
8398 struct ast_party_id connected_id = ast_channel_connected_effective_id(chan);
8399
8400 pri_grab(p, p->pri);
8401
8402 /* Check if a connected line update is allowed at this time. */
8403 switch (p->pri->colp_send) {
8404 case SIG_PRI_COLP_BLOCK:
8405 break;
8406 case SIG_PRI_COLP_CONNECT:
8407 /*
8408 * Outgoing calls receive CONNECT and act like an update before
8409 * the call is connected.
8410 */
8411 if (p->call_level <= SIG_PRI_CALL_LEVEL_ALERTING && !p->outgoing) {
8412 colp_allowed = 1;
8413 }
8414 break;
8415 case SIG_PRI_COLP_UPDATE:
8416 colp_allowed = 1;
8417 break;
8418 }
8419 if (!colp_allowed) {
8420 pri_rel(p->pri);
8421 ast_debug(1, "Blocked AST_CONTROL_CONNECTED_LINE on %s\n",
8422 ast_channel_name(chan));
8423 break;
8424 }
8425
8426 memset(&connected, 0, sizeof(connected));
8427 sig_pri_party_id_from_ast(&connected.id, &connected_id);
8428
8429 /* Determine the connected line numbering plan to actually use. */
8430 switch (p->pri->cpndialplan) {
8431 case -2:/* redundant */
8432 case -1:/* dynamic */
8433 /* compute dynamically */
8434 prefix_strip = 0;
8435 if (!strncmp(connected.id.number.str, p->pri->internationalprefix,
8436 strlen(p->pri->internationalprefix))) {
8437 prefix_strip = strlen(p->pri->internationalprefix);
8438 dialplan = PRI_INTERNATIONAL_ISDN;
8439 } else if (!strncmp(connected.id.number.str, p->pri->nationalprefix,
8440 strlen(p->pri->nationalprefix))) {
8441 prefix_strip = strlen(p->pri->nationalprefix);
8442 dialplan = PRI_NATIONAL_ISDN;
8443 } else {
8444 dialplan = PRI_LOCAL_ISDN;
8445 }
8446 connected.id.number.plan = dialplan;
8447
8448 if (prefix_strip && p->pri->cpndialplan != -2) {
8449 /* Strip the prefix from the connected line number. */
8450 memmove(connected.id.number.str,
8451 connected.id.number.str + prefix_strip,
8452 strlen(connected.id.number.str + prefix_strip) + 1);
8453 }
8454 break;
8455 case 0:/* from_channel */
8456 /* Use the numbering plan passed in. */
8457 break;
8458 default:
8459 connected.id.number.plan = p->pri->cpndialplan - 1;
8460 break;
8461 }
8462
8463 pri_connected_line_update(p->pri->pri, p->call, &connected);
8464 pri_rel(p->pri);
8465 }
8466 break;
8467 case AST_CONTROL_REDIRECTING:
8468 ast_debug(1, "Received AST_CONTROL_REDIRECTING on %s\n", ast_channel_name(chan));
8469 if (p->pri) {
8470 pri_grab(p, p->pri);
8471 sig_pri_redirecting_update(p, chan);
8472 pri_rel(p->pri);
8473 }
8474 break;
8475 case AST_CONTROL_AOC:
8476 #if defined(HAVE_PRI_AOC_EVENTS)
8477 {
8478 struct ast_aoc_decoded *decoded
8479 = ast_aoc_decode((struct ast_aoc_encoded *) data, datalen, chan);
8480 ast_debug(1, "Received AST_CONTROL_AOC on %s\n", ast_channel_name(chan));
8481 if (decoded && p->pri) {
8482 pri_grab(p, p->pri);
8483 switch (ast_aoc_get_msg_type(decoded)) {
8484 case AST_AOC_S:
8485 if (p->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_S) {
8486 sig_pri_aoc_s_from_ast(p, decoded);
8487 }
8488 break;
8489 case AST_AOC_D:
8490 if (p->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_D) {
8491 sig_pri_aoc_d_from_ast(p, decoded);
8492 }
8493 break;
8494 case AST_AOC_E:
8495 if (p->pri->aoc_passthrough_flag & SIG_PRI_AOC_GRANT_E) {
8496 sig_pri_aoc_e_from_ast(p, decoded);
8497 }
8498 /*
8499 * If hangup was delayed for this AOC-E msg, waiting_for_aoc
8500 * will be set. A hangup is already occurring via a timeout during
8501 * this delay. Instead of waiting for that timeout to occur, go ahead
8502 * and initiate the hangup since the delay is no longer necessary.
8503 */
8504 if (p->waiting_for_aoce) {
8505 p->waiting_for_aoce = 0;
8506 ast_debug(1,
8507 "Received final AOC-E msg, continue with hangup on %s\n",
8508 ast_channel_name(chan));
8509 ast_queue_hangup(chan);
8510 }
8511 break;
8512 case AST_AOC_REQUEST:
8513 /* We do not pass through AOC requests, So unless this
8514 * is an AOC termination request it will be ignored */
8515 if (ast_aoc_get_termination_request(decoded)) {
8516 pri_hangup(p->pri->pri, p->call, -1);
8517 }
8518 break;
8519 default:
8520 break;
8521 }
8522 pri_rel(p->pri);
8523 }
8524 ast_aoc_destroy_decoded(decoded);
8525 }
8526 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
8527 break;
8528 #if defined(HAVE_PRI_MCID)
8529 case AST_CONTROL_MCID:
8530 if (p->pri && p->pri->pri && p->pri->mcid_send) {
8531 pri_grab(p, p->pri);
8532 pri_mcid_req_send(p->pri->pri, p->call);
8533 pri_rel(p->pri);
8534 }
8535 break;
8536 #endif /* defined(HAVE_PRI_MCID) */
8537 }
8538
8539 return res;
8540 }
8541
8542 int sig_pri_answer(struct sig_pri_chan *p, struct ast_channel *ast)
8543 {
8544 int res;
8545
8546 /* Send a pri acknowledge */
8547 pri_grab(p, p->pri);
8548 #if defined(HAVE_PRI_AOC_EVENTS)
8549 if (p->aoc_s_request_invoke_id_valid) {
8550 /* if AOC-S was requested and the invoke id is still present on answer. That means
8551 * no AOC-S rate list was provided, so send a NULL response which will indicate that
8552 * AOC-S is not available */
8553 pri_aoc_s_request_response_send(p->pri->pri, p->call,
8554 p->aoc_s_request_invoke_id, NULL);
8555 p->aoc_s_request_invoke_id_valid = 0;
8556 }
8557 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
8558 if (p->call_level < SIG_PRI_CALL_LEVEL_CONNECT) {
8559 p->call_level = SIG_PRI_CALL_LEVEL_CONNECT;
8560 }
8561 sig_pri_set_dialing(p, 0);
8562 sig_pri_open_media(p);
8563 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
8564 pri_rel(p->pri);
8565 ast_setstate(ast, AST_STATE_UP);
8566 return res;
8567 }
8568
8569 /*!
8570 * \internal
8571 * \brief Simple check if the channel is available to use.
8572 * \since 1.8
8573 *
8574 * \param pvt Private channel control structure.
8575 *
8576 * \retval 0 Interface not available.
8577 * \retval 1 Interface is available.
8578 */
8579 static int sig_pri_available_check(struct sig_pri_chan *pvt)
8580 {
8581 /*
8582 * If interface has a B channel and is available for use
8583 * then the channel is available.
8584 */
8585 if (!pvt->no_b_channel && sig_pri_is_chan_available(pvt)) {
8586 return 1;
8587 }
8588 return 0;
8589 }
8590
8591 #if defined(HAVE_PRI_CALL_WAITING)
8592 /*!
8593 * \internal
8594 * \brief Get an available call waiting interface.
8595 * \since 1.8
8596 *
8597 * \param pri PRI span control structure.
8598 *
8599 * \note Assumes the pri->lock is already obtained.
8600 *
8601 * \retval cw Call waiting interface to use.
8602 * \retval NULL if no call waiting interface available.
8603 */
8604 static struct sig_pri_chan *sig_pri_cw_available(struct sig_pri_span *pri)
8605 {
8606 struct sig_pri_chan *cw;
8607 int idx;
8608
8609 cw = NULL;
8610 if (pri->num_call_waiting_calls < pri->max_call_waiting_calls) {
8611 if (!pri->num_call_waiting_calls) {
8612 /*
8613 * There are no outstanding call waiting calls. Check to see
8614 * if the span is in a congested state for the first call
8615 * waiting call.
8616 */
8617 for (idx = 0; idx < pri->numchans; ++idx) {
8618 if (pri->pvts[idx] && sig_pri_available_check(pri->pvts[idx])) {
8619 /* There is another channel that is available on this span. */
8620 return cw;
8621 }
8622 }
8623 }
8624 idx = pri_find_empty_nobch(pri);
8625 if (0 <= idx) {
8626 /* Setup the call waiting interface to use. */
8627 cw = pri->pvts[idx];
8628 cw->is_call_waiting = 1;
8629 sig_pri_init_config(cw, pri);
8630 ast_atomic_fetchadd_int(&pri->num_call_waiting_calls, 1);
8631 }
8632 }
8633 return cw;
8634 }
8635 #endif /* defined(HAVE_PRI_CALL_WAITING) */
8636
8637 int sig_pri_available(struct sig_pri_chan **pvt, int is_specific_channel)
8638 {
8639 struct sig_pri_chan *p = *pvt;
8640 struct sig_pri_span *pri;
8641
8642 if (!p->pri) {
8643 /* Something is wrong here. A PRI channel without the pri pointer? */
8644 return 0;
8645 }
8646 pri = p->pri;
8647
8648 ast_mutex_lock(&pri->lock);
8649 if (
8650 #if defined(HAVE_PRI_CALL_WAITING)
8651 /*
8652 * Only do call waiting calls if we have any
8653 * call waiting call outstanding. We do not
8654 * want new calls to steal a B channel
8655 * freed for an earlier call waiting call.
8656 */
8657 !pri->num_call_waiting_calls &&
8658 #endif /* defined(HAVE_PRI_CALL_WAITING) */
8659 sig_pri_available_check(p)) {
8660 p->allocated = 1;
8661 ast_mutex_unlock(&pri->lock);
8662 return 1;
8663 }
8664
8665 #if defined(HAVE_PRI_CALL_WAITING)
8666 if (!is_specific_channel) {
8667 struct sig_pri_chan *cw;
8668
8669 cw = sig_pri_cw_available(pri);
8670 if (cw) {
8671 /* We have a call waiting interface to use instead. */
8672 cw->allocated = 1;
8673 *pvt = cw;
8674 ast_mutex_unlock(&pri->lock);
8675 return 1;
8676 }
8677 }
8678 #endif /* defined(HAVE_PRI_CALL_WAITING) */
8679 ast_mutex_unlock(&pri->lock);
8680 return 0;
8681 }
8682
8683 /* If return 0, it means this function was able to handle it (pre setup digits). If non zero, the user of this
8684 * functions should handle it normally (generate inband DTMF) */
8685 int sig_pri_digit_begin(struct sig_pri_chan *pvt, struct ast_channel *ast, char digit)
8686 {
8687 if (ast_channel_state(ast) == AST_STATE_DIALING) {
8688 if (pvt->call_level < SIG_PRI_CALL_LEVEL_OVERLAP) {
8689 unsigned int len;
8690
8691 len = strlen(pvt->dialdest);
8692 if (len < sizeof(pvt->dialdest) - 1) {
8693 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n",
8694 digit);
8695 pvt->dialdest[len++] = digit;
8696 pvt->dialdest[len] = '\0';
8697 } else {
8698 ast_log(LOG_WARNING,
8699 "Span %d: Deferred digit buffer overflow for digit '%c'.\n",
8700 pvt->pri->span, digit);
8701 }
8702 return 0;
8703 }
8704 if (pvt->call_level < SIG_PRI_CALL_LEVEL_PROCEEDING) {
8705 pri_grab(pvt, pvt->pri);
8706 pri_information(pvt->pri->pri, pvt->call, digit);
8707 pri_rel(pvt->pri);
8708 return 0;
8709 }
8710 if (pvt->call_level < SIG_PRI_CALL_LEVEL_CONNECT) {
8711 ast_log(LOG_WARNING,
8712 "Span %d: Digit '%c' may be ignored by peer. (Call level:%u(%s))\n",
8713 pvt->pri->span, digit, pvt->call_level,
8714 sig_pri_call_level2str(pvt->call_level));
8715 }
8716 }
8717 return 1;
8718 }
8719
8720 /*!
8721 * \brief DTMF dial string complete.
8722 * \since 1.8.11
8723 *
8724 * \param pvt sig_pri private channel structure.
8725 * \param ast Asterisk channel
8726 *
8727 * \note Channel and private lock are already held.
8728 */
8729 void sig_pri_dial_complete(struct sig_pri_chan *pvt, struct ast_channel *ast)
8730 {
8731 /* If we just completed 'w' deferred dialing digits, we need to answer now. */
8732 if (pvt->call_level == SIG_PRI_CALL_LEVEL_DEFER_DIAL) {
8733 pvt->call_level = SIG_PRI_CALL_LEVEL_CONNECT;
8734
8735 sig_pri_open_media(pvt);
8736 {
8737 struct ast_frame f = {AST_FRAME_CONTROL, };
8738
8739 if (sig_pri_callbacks.queue_control) {
8740 sig_pri_callbacks.queue_control(pvt->chan_pvt, AST_CONTROL_ANSWER);
8741 }
8742
8743 f.subclass.integer = AST_CONTROL_ANSWER;
8744 ast_queue_frame(ast, &f);
8745 }
8746 sig_pri_set_dialing(pvt, 0);
8747 /* Enable echo cancellation if it's not on already */
8748 sig_pri_set_echocanceller(pvt, 1);
8749 }
8750 }
8751
8752 #if defined(HAVE_PRI_MWI)
8753 /*!
8754 * \internal
8755 * \brief Send a MWI indication to the given span.
8756 * \since 1.8
8757 *
8758 * \param pri PRI span control structure.
8759 * \param vm_number Voicemail controlling number (NULL if not present).
8760 * \param vm_box Voicemail mailbox number
8761 * \param mbox_id Mailbox id
8762 * \param num_messages Number of messages waiting.
8763 */
8764 static void sig_pri_send_mwi_indication(struct sig_pri_span *pri, const char *vm_number, const char *vm_box, const char *mbox_id, int num_messages)
8765 {
8766 struct pri_party_id voicemail;
8767 struct pri_party_id mailbox;
8768
8769 ast_debug(1, "Send MWI indication for %s(%s) vm_number:%s num_messages:%d\n",
8770 vm_box, mbox_id, S_OR(vm_number, "<not-present>"), num_messages);
8771
8772 memset(&mailbox, 0, sizeof(mailbox));
8773 mailbox.number.valid = 1;
8774 mailbox.number.presentation = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
8775 mailbox.number.plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_UNKNOWN;
8776 ast_copy_string(mailbox.number.str, vm_box, sizeof(mailbox.number.str));
8777
8778 memset(&voicemail, 0, sizeof(voicemail));
8779 voicemail.number.valid = 1;
8780 voicemail.number.presentation = PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
8781 voicemail.number.plan = (PRI_TON_UNKNOWN << 4) | PRI_NPI_UNKNOWN;
8782 if (vm_number) {
8783 ast_copy_string(voicemail.number.str, vm_number, sizeof(voicemail.number.str));
8784 }
8785
8786 ast_mutex_lock(&pri->lock);
8787 #if defined(HAVE_PRI_MWI_V2)
8788 pri_mwi_indicate_v2(pri->pri, &mailbox, &voicemail, 1 /* speech */, num_messages,
8789 NULL, NULL, -1, 0);
8790 #else /* !defined(HAVE_PRI_MWI_V2) */
8791 pri_mwi_indicate(pri->pri, &mailbox, 1 /* speech */, num_messages, NULL, NULL, -1, 0);
8792 #endif /* !defined(HAVE_PRI_MWI_V2) */
8793 ast_mutex_unlock(&pri->lock);
8794 }
8795 #endif /* defined(HAVE_PRI_MWI) */
8796
8797 #if defined(HAVE_PRI_MWI)
8798 /*!
8799 * \internal
8800 * \brief MWI subscription event callback.
8801 * \since 1.8
8802 *
8803 * \param userdata the data provider in the call to stasis_subscribe()
8804 * \param sub the subscription to which the message was delivered for this callback
8805 * \param msg the message being passed to the subscriber
8806 */
8807 static void sig_pri_mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
8808 {
8809 struct sig_pri_span *pri = userdata;
8810 int idx;
8811 struct ast_mwi_state *mwi_state;
8812
8813 if (ast_mwi_state_type() != stasis_message_type(msg)) {
8814 return;
8815 }
8816
8817 mwi_state = stasis_message_data(msg);
8818
8819 for (idx = 0; idx < ARRAY_LEN(pri->mbox); ++idx) {
8820 if (!pri->mbox[idx].sub) {
8821 /* Mailbox slot is empty */
8822 continue;
8823 }
8824
8825 if (!strcmp(pri->mbox[idx].uniqueid, mwi_state->uniqueid)) {
8826 /* Found the mailbox. */
8827 sig_pri_send_mwi_indication(pri, pri->mbox[idx].vm_number,
8828 pri->mbox[idx].vm_box, pri->mbox[idx].uniqueid, mwi_state->new_msgs);
8829 break;
8830 }
8831 }
8832 }
8833 #endif /* defined(HAVE_PRI_MWI) */
8834
8835 #if defined(HAVE_PRI_MWI)
8836 /*!
8837 * \internal
8838 * \brief Send update MWI indications from the event cache.
8839 * \since 1.8
8840 *
8841 * \param pri PRI span control structure.
8842 */
8843 static void sig_pri_mwi_cache_update(struct sig_pri_span *pri)
8844 {
8845 int idx;
8846 struct ast_mwi_state *mwi_state;
8847
8848 for (idx = 0; idx < ARRAY_LEN(pri->mbox); ++idx) {
8849 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
8850 if (!pri->mbox[idx].sub) {
8851 /* Mailbox slot is empty */
8852 continue;
8853 }
8854
8855 msg = stasis_cache_get(ast_mwi_state_cache(), ast_mwi_state_type(),
8856 pri->mbox[idx].uniqueid);
8857 if (!msg) {
8858 /* No cached event for this mailbox. */
8859 continue;
8860 }
8861
8862 mwi_state = stasis_message_data(msg);
8863 sig_pri_send_mwi_indication(pri, pri->mbox[idx].vm_number, pri->mbox[idx].vm_box,
8864 pri->mbox[idx].uniqueid, mwi_state->new_msgs);
8865 }
8866 }
8867 #endif /* defined(HAVE_PRI_MWI) */
8868
8869 /*!
8870 * \brief Stop PRI span.
8871 * \since 1.8
8872 *
8873 * \param pri PRI span control structure.
8874 */
8875 void sig_pri_stop_pri(struct sig_pri_span *pri)
8876 {
8877 #if defined(HAVE_PRI_MWI)
8878 int idx;
8879 #endif /* defined(HAVE_PRI_MWI) */
8880
8881 #if defined(HAVE_PRI_MWI)
8882 for (idx = 0; idx < ARRAY_LEN(pri->mbox); ++idx) {
8883 if (pri->mbox[idx].sub) {
8884 pri->mbox[idx].sub = ast_mwi_unsubscribe_and_join(pri->mbox[idx].sub);
8885 }
8886 }
8887 #endif /* defined(HAVE_PRI_MWI) */
8888 }
8889
8890 /*!
8891 * \internal
8892 * \brief qsort comparison function.
8893 * \since 1.8
8894 *
8895 * \param left Ptr to sig_pri_chan ptr to compare.
8896 * \param right Ptr to sig_pri_chan ptr to compare.
8897 *
8898 * \retval <0 if left < right.
8899 * \retval =0 if left == right.
8900 * \retval >0 if left > right.
8901 */
8902 static int sig_pri_cmp_pri_chans(const void *left, const void *right)
8903 {
8904 const struct sig_pri_chan *pvt_left;
8905 const struct sig_pri_chan *pvt_right;
8906
8907 pvt_left = *(struct sig_pri_chan **) left;
8908 pvt_right = *(struct sig_pri_chan **) right;
8909 if (!pvt_left) {
8910 if (!pvt_right) {
8911 return 0;
8912 }
8913 return 1;
8914 }
8915 if (!pvt_right) {
8916 return -1;
8917 }
8918
8919 return pvt_left->channel - pvt_right->channel;
8920 }
8921
8922 /*!
8923 * \internal
8924 * \brief Sort the PRI B channel private pointer array.
8925 * \since 1.8
8926 *
8927 * \param pri PRI span control structure.
8928 *
8929 * \details
8930 * Since the chan_dahdi.conf file can declare channels in any order, we need to sort
8931 * the private channel pointer array.
8932 */
8933 static void sig_pri_sort_pri_chans(struct sig_pri_span *pri)
8934 {
8935 qsort(&pri->pvts, pri->numchans, sizeof(pri->pvts[0]), sig_pri_cmp_pri_chans);
8936 }
8937
8938 int sig_pri_start_pri(struct sig_pri_span *pri)
8939 {
8940 int x;
8941 int i;
8942 #if defined(HAVE_PRI_MWI)
8943 char *saveptr;
8944 char *prev_vm_number;
8945 #endif /* defined(HAVE_PRI_MWI) */
8946
8947 #if defined(HAVE_PRI_MWI)
8948 /* Prepare the mbox[] for use. */
8949 for (i = 0; i < ARRAY_LEN(pri->mbox); ++i) {
8950 if (pri->mbox[i].sub) {
8951 pri->mbox[i].sub = ast_mwi_unsubscribe(pri->mbox[i].sub);
8952 }
8953 }
8954 #endif /* defined(HAVE_PRI_MWI) */
8955
8956 ast_mutex_init(&pri->lock);
8957 sig_pri_sort_pri_chans(pri);
8958
8959 #if defined(HAVE_PRI_MWI)
8960 /*
8961 * Split the mwi_vm_numbers configuration string into the mbox[].vm_number:
8962 * vm_number{,vm_number}
8963 */
8964 prev_vm_number = NULL;
8965 saveptr = pri->mwi_vm_numbers;
8966 for (i = 0; i < ARRAY_LEN(pri->mbox); ++i) {
8967 char *vm_number;
8968
8969 vm_number = strsep(&saveptr, ",");
8970 if (vm_number) {
8971 vm_number = ast_strip(vm_number);
8972 }
8973 if (ast_strlen_zero(vm_number)) {
8974 /* There was no number so reuse the previous number. */
8975 vm_number = prev_vm_number;
8976 } else {
8977 /* We have a new number. */
8978 prev_vm_number = vm_number;
8979 }
8980 pri->mbox[i].vm_number = vm_number;
8981 }
8982
8983 /*
8984 * Split the mwi_vm_boxes configuration string into the mbox[].vm_box:
8985 * vm_box{,vm_box}
8986 */
8987 saveptr = pri->mwi_vm_boxes;
8988 for (i = 0; i < ARRAY_LEN(pri->mbox); ++i) {
8989 char *vm_box;
8990
8991 vm_box = strsep(&saveptr, ",");
8992 if (vm_box) {
8993 vm_box = ast_strip(vm_box);
8994 if (ast_strlen_zero(vm_box)) {
8995 vm_box = NULL;
8996 }
8997 }
8998 pri->mbox[i].vm_box = vm_box;
8999 }
9000
9001 /*
9002 * Split the mwi_mailboxes configuration string into the mbox[]:
9003 * vm_mailbox{,vm_mailbox}
9004 */
9005 saveptr = pri->mwi_mailboxes;
9006 for (i = 0; i < ARRAY_LEN(pri->mbox); ++i) {
9007 char *mbox_id;
9008
9009 mbox_id = strsep(&saveptr, ",");
9010 if (mbox_id) {
9011 mbox_id = ast_strip(mbox_id);
9012 if (ast_strlen_zero(mbox_id)) {
9013 mbox_id = NULL;
9014 }
9015 }
9016 pri->mbox[i].uniqueid = mbox_id;
9017 if (!pri->mbox[i].vm_box || !mbox_id) {
9018 /* The mailbox position is disabled. */
9019 ast_debug(1, "%s span %d MWI position %d disabled. vm_box:%s mbox_id:%s.\n",
9020 sig_pri_cc_type_name, pri->span, i,
9021 pri->mbox[i].vm_box ?: "<missing>",
9022 mbox_id ?: "<missing>");
9023 continue;
9024 }
9025
9026 pri->mbox[i].sub = ast_mwi_subscribe_pool(mbox_id, sig_pri_mwi_event_cb, pri);
9027 if (!pri->mbox[i].sub) {
9028 ast_log(LOG_ERROR, "%s span %d could not subscribe to MWI events for %s(%s).\n",
9029 sig_pri_cc_type_name, pri->span, pri->mbox[i].vm_box, mbox_id);
9030 }
9031 #if defined(HAVE_PRI_MWI_V2)
9032 if (ast_strlen_zero(pri->mbox[i].vm_number)) {
9033 ast_log(LOG_WARNING, "%s span %d MWI voicemail number for %s(%s) is empty.\n",
9034 sig_pri_cc_type_name, pri->span, pri->mbox[i].vm_box, mbox_id);
9035 }
9036 #endif /* defined(HAVE_PRI_MWI_V2) */
9037 }
9038 #endif /* defined(HAVE_PRI_MWI) */
9039
9040 for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
9041 if (pri->fds[i] == -1) {
9042 break;
9043 }
9044
9045 switch (pri->sig) {
9046 case SIG_BRI:
9047 pri->dchans[i] = pri_new_bri(pri->fds[i], 1, pri->nodetype, pri->switchtype);
9048 break;
9049 case SIG_BRI_PTMP:
9050 pri->dchans[i] = pri_new_bri(pri->fds[i], 0, pri->nodetype, pri->switchtype);
9051 break;
9052 default:
9053 pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
9054 #if defined(HAVE_PRI_SERVICE_MESSAGES)
9055 if (pri->enable_service_message_support) {
9056 pri_set_service_message_support(pri->dchans[i], 1);
9057 }
9058 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
9059 break;
9060 }
9061
9062 pri_set_overlapdial(pri->dchans[i], (pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING) ? 1 : 0);
9063 #ifdef HAVE_PRI_PROG_W_CAUSE
9064 pri_set_chan_mapping_logical(pri->dchans[i], pri->qsigchannelmapping == DAHDI_CHAN_MAPPING_LOGICAL);
9065 #endif
9066 #ifdef HAVE_PRI_INBANDDISCONNECT
9067 pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect);
9068 #endif
9069 /* Enslave to master if appropriate */
9070 if (i)
9071 pri_enslave(pri->dchans[0], pri->dchans[i]);
9072 if (!pri->dchans[i]) {
9073 if (pri->fds[i] > 0)
9074 close(pri->fds[i]);
9075 pri->fds[i] = -1;
9076 ast_log(LOG_ERROR, "Unable to create PRI structure\n");
9077 return -1;
9078 }
9079 pri_set_debug(pri->dchans[i], SIG_PRI_DEBUG_DEFAULT);
9080 pri_set_nsf(pri->dchans[i], pri->nsf);
9081 #ifdef PRI_GETSET_TIMERS
9082 for (x = 0; x < PRI_MAX_TIMERS; x++) {
9083 if (pri->pritimers[x] != 0)
9084 pri_set_timer(pri->dchans[i], x, pri->pritimers[x]);
9085 }
9086 #endif
9087 }
9088
9089 /* Assume primary is the one we use */
9090 pri->pri = pri->dchans[0];
9091
9092 #if defined(HAVE_PRI_CALL_HOLD)
9093 pri_hold_enable(pri->pri, 1);
9094 #endif /* defined(HAVE_PRI_CALL_HOLD) */
9095 #if defined(HAVE_PRI_CALL_REROUTING)
9096 pri_reroute_enable(pri->pri, 1);
9097 #endif /* defined(HAVE_PRI_CALL_REROUTING) */
9098 #if defined(HAVE_PRI_HANGUP_FIX)
9099 pri_hangup_fix_enable(pri->pri, 1);
9100 #endif /* defined(HAVE_PRI_HANGUP_FIX) */
9101 #if defined(HAVE_PRI_CCSS)
9102 pri_cc_enable(pri->pri, 1);
9103 pri_cc_recall_mode(pri->pri, pri->cc_ptmp_recall_mode);
9104 pri_cc_retain_signaling_req(pri->pri, pri->cc_qsig_signaling_link_req);
9105 pri_cc_retain_signaling_rsp(pri->pri, pri->cc_qsig_signaling_link_rsp);
9106 #endif /* defined(HAVE_PRI_CCSS) */
9107 #if defined(HAVE_PRI_TRANSFER)
9108 pri_transfer_enable(pri->pri, 1);
9109 #endif /* defined(HAVE_PRI_TRANSFER) */
9110 #if defined(HAVE_PRI_AOC_EVENTS)
9111 pri_aoc_events_enable(pri->pri, 1);
9112 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
9113 #if defined(HAVE_PRI_CALL_WAITING)
9114 pri_connect_ack_enable(pri->pri, 1);
9115 #endif /* defined(HAVE_PRI_CALL_WAITING) */
9116 #if defined(HAVE_PRI_MCID)
9117 pri_mcid_enable(pri->pri, 1);
9118 #endif /* defined(HAVE_PRI_MCID) */
9119 #if defined(HAVE_PRI_DISPLAY_TEXT)
9120 pri_display_options_send(pri->pri, pri->display_flags_send);
9121 pri_display_options_receive(pri->pri, pri->display_flags_receive);
9122 #endif /* defined(HAVE_PRI_DISPLAY_TEXT) */
9123 #if defined(HAVE_PRI_DATETIME_SEND)
9124 pri_date_time_send_option(pri->pri, pri->datetime_send);
9125 #endif /* defined(HAVE_PRI_DATETIME_SEND) */
9126 #if defined(HAVE_PRI_L2_PERSISTENCE)
9127 pri_persistent_layer2_option(pri->pri, pri->l2_persistence);
9128 #endif /* defined(HAVE_PRI_L2_PERSISTENCE) */
9129
9130 pri->resetpos = -1;
9131 if (ast_pthread_create_background(&pri->master, NULL, pri_dchannel, pri)) {
9132 for (i = 0; i < SIG_PRI_NUM_DCHANS; i++) {
9133 if (!pri->dchans[i])
9134 break;
9135 if (pri->fds[i] > 0)
9136 close(pri->fds[i]);
9137 pri->fds[i] = -1;
9138 }
9139 ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
9140 return -1;
9141 }
9142
9143 #if defined(HAVE_PRI_MWI)
9144 /*
9145 * Send the initial MWI indications from the event cache for this span.
9146 *
9147 * If we were loaded after app_voicemail the event would already be in
9148 * the cache. If we were loaded before app_voicemail the event would not
9149 * be in the cache yet and app_voicemail will send the event when it
9150 * gets loaded.
9151 */
9152 sig_pri_mwi_cache_update(pri);
9153 #endif /* defined(HAVE_PRI_MWI) */
9154
9155 return 0;
9156 }
9157
9158 /*!
9159 * \brief Notify new alarm status.
9160 *
9161 * \param p Channel private pointer.
9162 * \param noalarm Non-zero if not in alarm mode.
9163 *
9164 * \note Assumes the sig_pri_lock_private(p) is already obtained.
9165 */
9166 void sig_pri_chan_alarm_notify(struct sig_pri_chan *p, int noalarm)
9167 {
9168 pri_grab(p, p->pri);
9169 sig_pri_set_alarm(p, !noalarm);
9170 if (!noalarm) {
9171 if (pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
9172 /* T309 is not enabled : destroy calls when alarm occurs */
9173 if (p->call) {
9174 pri_destroycall(p->pri->pri, p->call);
9175 p->call = NULL;
9176 }
9177 if (p->owner)
9178 ast_channel_softhangup_internal_flag_add(p->owner, AST_SOFTHANGUP_DEV);
9179 }
9180 }
9181 sig_pri_span_devstate_changed(p->pri);
9182 pri_rel(p->pri);
9183 }
9184
9185 /*!
9186 * \brief Determine if layer 1 alarms are ignored.
9187 *
9188 * \param pri Channel private pointer.
9189 *
9190 * \return TRUE if the alarm is ignored.
9191 */
9192 int sig_pri_is_alarm_ignored(struct sig_pri_span *pri)
9193 {
9194 return pri->layer1_ignored;
9195 }
9196
9197 struct sig_pri_chan *sig_pri_chan_new(void *pvt_data, struct sig_pri_span *pri, int logicalspan, int channo, int trunkgroup)
9198 {
9199 struct sig_pri_chan *p;
9200
9201 p = ast_calloc(1, sizeof(*p));
9202 if (!p)
9203 return p;
9204
9205 p->logicalspan = logicalspan;
9206 p->prioffset = channo;
9207 p->mastertrunkgroup = trunkgroup;
9208
9209 p->chan_pvt = pvt_data;
9210
9211 p->pri = pri;
9212
9213 return p;
9214 }
9215
9216 /*!
9217 * \brief Delete the sig_pri private channel structure.
9218 * \since 1.8
9219 *
9220 * \param doomed sig_pri private channel structure to delete.
9221 */
9222 void sig_pri_chan_delete(struct sig_pri_chan *doomed)
9223 {
9224 ast_free(doomed);
9225 }
9226
9227 #define SIG_PRI_SC_HEADER "%-4s %4s %-4s %-4s %-10s %-4s %s\n"
9228 #define SIG_PRI_SC_LINE "%4d %4d %-4s %-4s %-10s %-4s %s"
9229 void sig_pri_cli_show_channels_header(int fd)
9230 {
9231 ast_cli(fd, SIG_PRI_SC_HEADER, "PRI", "", "B", "Chan", "Call", "PRI", "Channel");
9232 ast_cli(fd, SIG_PRI_SC_HEADER, "Span", "Chan", "Chan", "Idle", "Level", "Call", "Name");
9233 }
9234
9235 void sig_pri_cli_show_channels(int fd, struct sig_pri_span *pri)
9236 {
9237 char line[256];
9238 int idx;
9239 struct sig_pri_chan *pvt;
9240
9241 ast_mutex_lock(&pri->lock);
9242 for (idx = 0; idx < pri->numchans; ++idx) {
9243 if (!pri->pvts[idx]) {
9244 continue;
9245 }
9246 pvt = pri->pvts[idx];
9247 sig_pri_lock_private(pvt);
9248 sig_pri_lock_owner(pri, idx);
9249 if (pvt->no_b_channel && sig_pri_is_chan_available(pvt)) {
9250 /* Don't show held/call-waiting channels if they are not in use. */
9251 sig_pri_unlock_private(pvt);
9252 continue;
9253 }
9254
9255 snprintf(line, sizeof(line), SIG_PRI_SC_LINE,
9256 pri->span,
9257 pvt->channel,
9258 pvt->no_b_channel ? "No" : "Yes",/* Has media */
9259 sig_pri_is_chan_available(pvt) ? "Yes" : "No",
9260 sig_pri_call_level2str(pvt->call_level),
9261 pvt->call ? "Yes" : "No",
9262 pvt->owner ? ast_channel_name(pvt->owner) : "");
9263
9264 if (pvt->owner) {
9265 ast_channel_unlock(pvt->owner);
9266 }
9267 sig_pri_unlock_private(pvt);
9268
9269 ast_mutex_unlock(&pri->lock);
9270 ast_cli(fd, "%s\n", line);
9271 ast_mutex_lock(&pri->lock);
9272 }
9273 ast_mutex_unlock(&pri->lock);
9274 }
9275
9276 static void build_status(char *s, size_t len, int status, int active)
9277 {
9278 if (!s || len < 1) {
9279 return;
9280 }
9281 snprintf(s, len, "%s%s, %s",
9282 (status & DCHAN_NOTINALARM) ? "" : "In Alarm, ",
9283 (status & DCHAN_UP) ? "Up" : "Down",
9284 (active) ? "Active" : "Standby");
9285 }
9286
9287 void sig_pri_cli_show_spans(int fd, int span, struct sig_pri_span *pri)
9288 {
9289 char status[256];
9290 int x;
9291 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
9292 if (pri->dchans[x]) {
9293 build_status(status, sizeof(status), pri->dchanavail[x], pri->dchans[x] == pri->pri);
9294 ast_cli(fd, "PRI span %d/%d: %s\n", span, x, status);
9295 }
9296 }
9297 }
9298
9299 void sig_pri_cli_show_span(int fd, int *dchannels, struct sig_pri_span *pri)
9300 {
9301 int x;
9302 char status[256];
9303
9304 for (x = 0; x < SIG_PRI_NUM_DCHANS; x++) {
9305 if (pri->dchans[x]) {
9306 #ifdef PRI_DUMP_INFO_STR
9307 char *info_str = NULL;
9308 #endif
9309 ast_cli(fd, "%s D-channel: %d\n", pri_order(x), dchannels[x]);
9310 build_status(status, sizeof(status), pri->dchanavail[x], pri->dchans[x] == pri->pri);
9311 ast_cli(fd, "Status: %s\n", status);
9312 ast_mutex_lock(&pri->lock);
9313 #ifdef PRI_DUMP_INFO_STR
9314 info_str = pri_dump_info_str(pri->pri);
9315 if (info_str) {
9316 ast_cli(fd, "%s", info_str);
9317 ast_std_free(info_str);
9318 }
9319 #else
9320 pri_dump_info(pri->pri);
9321 #endif
9322 ast_mutex_unlock(&pri->lock);
9323 ast_cli(fd, "Overlap Recv: %s\n\n", (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)?"Yes":"No");
9324 ast_cli(fd, "\n");
9325 }
9326 }
9327 }
9328
9329 int pri_send_keypad_facility_exec(struct sig_pri_chan *p, const char *digits)
9330 {
9331 sig_pri_lock_private(p);
9332
9333 if (!p->pri || !p->call) {
9334 ast_debug(1, "Unable to find pri or call on channel!\n");
9335 sig_pri_unlock_private(p);
9336 return -1;
9337 }
9338
9339 pri_grab(p, p->pri);
9340 pri_keypad_facility(p->pri->pri, p->call, digits);
9341 pri_rel(p->pri);
9342
9343 sig_pri_unlock_private(p);
9344
9345 return 0;
9346 }
9347
9348 int pri_send_callrerouting_facility_exec(struct sig_pri_chan *p, enum ast_channel_state chanstate, const char *destination, const char *original, const char *reason)
9349 {
9350 int res;
9351
9352 sig_pri_lock_private(p);
9353
9354 if (!p->pri || !p->call) {
9355 ast_debug(1, "Unable to find pri or call on channel!\n");
9356 sig_pri_unlock_private(p);
9357 return -1;
9358 }
9359
9360 pri_grab(p, p->pri);
9361 res = pri_callrerouting_facility(p->pri->pri, p->call, destination, original, reason);
9362 pri_rel(p->pri);
9363
9364 sig_pri_unlock_private(p);
9365
9366 return res;
9367 }
9368
9369 #if defined(HAVE_PRI_SERVICE_MESSAGES)
9370 int pri_maintenance_bservice(struct pri *pri, struct sig_pri_chan *p, int changestatus)
9371 {
9372 int channel = PVT_TO_CHANNEL(p);
9373 int span = PRI_SPAN(channel);
9374
9375 return pri_maintenance_service(pri, span, channel, changestatus);
9376 }
9377 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
9378
9379 void sig_pri_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_pri_chan *pchan)
9380 {
9381 if (pchan->owner == oldchan) {
9382 pchan->owner = newchan;
9383 }
9384 }
9385
9386 #if defined(HAVE_PRI_DISPLAY_TEXT)
9387 /*!
9388 * \brief Send display text.
9389 * \since 10.0
9390 *
9391 * \param p Channel to send text over
9392 * \param text Text to send.
9393 */
9394 void sig_pri_sendtext(struct sig_pri_chan *p, const char *text)
9395 {
9396 struct pri_subcmd_display_txt display;
9397
9398 if (p->pri && p->pri->pri) {
9399 ast_copy_string(display.text, text, sizeof(display.text));
9400 display.length = strlen(display.text);
9401 display.char_set = 0;/* unknown(0) */
9402 pri_grab(p, p->pri);
9403 pri_display_text(p->pri->pri, p->call, &display);
9404 pri_rel(p->pri);
9405 }
9406 }
9407 #endif /* defined(HAVE_PRI_DISPLAY_TEXT) */
9408
9409 #if defined(HAVE_PRI_CCSS)
9410 /*!
9411 * \brief PRI CC agent initialization.
9412 * \since 1.8
9413 *
9414 * \param agent CC core agent control.
9415 * \param pvt_chan Original channel the agent will attempt to recall.
9416 *
9417 * \details
9418 * This callback is called when the CC core is initialized. Agents should allocate
9419 * any private data necessary for the call and assign it to the private_data
9420 * on the agent. Additionally, if any ast_cc_agent_flags are pertinent to the
9421 * specific agent type, they should be set in this function as well.
9422 *
9423 * \retval 0 on success.
9424 * \retval -1 on error.
9425 */
9426 int sig_pri_cc_agent_init(struct ast_cc_agent *agent, struct sig_pri_chan *pvt_chan)
9427 {
9428 struct sig_pri_cc_agent_prv *cc_pvt;
9429
9430 cc_pvt = ast_calloc(1, sizeof(*cc_pvt));
9431 if (!cc_pvt) {
9432 return -1;
9433 }
9434
9435 ast_mutex_lock(&pvt_chan->pri->lock);
9436 cc_pvt->pri = pvt_chan->pri;
9437 cc_pvt->cc_id = pri_cc_available(pvt_chan->pri->pri, pvt_chan->call);
9438 ast_mutex_unlock(&pvt_chan->pri->lock);
9439 if (cc_pvt->cc_id == -1) {
9440 ast_free(cc_pvt);
9441 return -1;
9442 }
9443 agent->private_data = cc_pvt;
9444 return 0;
9445 }
9446 #endif /* defined(HAVE_PRI_CCSS) */
9447
9448 #if defined(HAVE_PRI_CCSS)
9449 /*!
9450 * \brief Start the offer timer.
9451 * \since 1.8
9452 *
9453 * \param agent CC core agent control.
9454 *
9455 * \details
9456 * This is called by the core when the caller hangs up after
9457 * a call for which CC may be requested. The agent should
9458 * begin the timer as configured.
9459 *
9460 * The primary reason why this functionality is left to
9461 * the specific agent implementations is due to the differing
9462 * use of schedulers throughout the code. Some channel drivers
9463 * may already have a scheduler context they wish to use, and
9464 * amongst those, some may use the ast_sched API while others
9465 * may use the ast_sched_thread API, which are incompatible.
9466 *
9467 * \retval 0 on success.
9468 * \retval -1 on error.
9469 */
9470 int sig_pri_cc_agent_start_offer_timer(struct ast_cc_agent *agent)
9471 {
9472 /* libpri maintains it's own offer timer in the form of T_RETENTION. */
9473 return 0;
9474 }
9475 #endif /* defined(HAVE_PRI_CCSS) */
9476
9477 #if defined(HAVE_PRI_CCSS)
9478 /*!
9479 * \brief Stop the offer timer.
9480 * \since 1.8
9481 *
9482 * \param agent CC core agent control.
9483 *
9484 * \details
9485 * This callback is called by the CC core when the caller
9486 * has requested CC.
9487 *
9488 * \retval 0 on success.
9489 * \retval -1 on error.
9490 */
9491 int sig_pri_cc_agent_stop_offer_timer(struct ast_cc_agent *agent)
9492 {
9493 /* libpri maintains it's own offer timer in the form of T_RETENTION. */
9494 return 0;
9495 }
9496 #endif /* defined(HAVE_PRI_CCSS) */
9497
9498 #if defined(HAVE_PRI_CCSS)
9499 /*!
9500 * \brief Response to a CC request.
9501 * \since 1.8
9502 *
9503 * \param agent CC core agent control.
9504 * \param reason CC request response status.
9505 *
9506 * \details
9507 * When the core receives knowledge that a called
9508 * party has accepted a CC request, it will call
9509 * this callback. The core may also call this
9510 * if there is some error when attempting to process
9511 * the incoming CC request.
9512 *
9513 * The duty of this is to issue a proper response to a
9514 * CC request from the caller by acknowledging receipt
9515 * of that request or rejecting it.
9516 */
9517 void sig_pri_cc_agent_req_rsp(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason)
9518 {
9519 struct sig_pri_cc_agent_prv *cc_pvt;
9520 int res;
9521 int status;
9522 const char *failed_msg;
9523 static const char *failed_to_send = "Failed to send the CC request response.";
9524 static const char *not_accepted = "The core declined the CC request.";
9525
9526 cc_pvt = agent->private_data;
9527 ast_mutex_lock(&cc_pvt->pri->lock);
9528 if (cc_pvt->cc_request_response_pending) {
9529 cc_pvt->cc_request_response_pending = 0;
9530
9531 /* Convert core response reason to ISDN response status. */
9532 status = 2;/* short_term_denial */
9533 switch (reason) {
9534 case AST_CC_AGENT_RESPONSE_SUCCESS:
9535 status = 0;/* success */
9536 break;
9537 case AST_CC_AGENT_RESPONSE_FAILURE_INVALID:
9538 status = 2;/* short_term_denial */
9539 break;
9540 case AST_CC_AGENT_RESPONSE_FAILURE_TOO_MANY:
9541 status = 5;/* queue_full */
9542 break;
9543 }
9544
9545 res = pri_cc_req_rsp(cc_pvt->pri->pri, cc_pvt->cc_id, status);
9546 if (!status) {
9547 /* CC core request was accepted. */
9548 if (res) {
9549 failed_msg = failed_to_send;
9550 } else {
9551 failed_msg = NULL;
9552 }
9553 } else {
9554 /* CC core request was declined. */
9555 if (res) {
9556 failed_msg = failed_to_send;
9557 } else {
9558 failed_msg = not_accepted;
9559 }
9560 }
9561 } else {
9562 failed_msg = NULL;
9563 }
9564 ast_mutex_unlock(&cc_pvt->pri->lock);
9565 if (failed_msg) {
9566 ast_cc_failed(agent->core_id, "%s agent: %s", sig_pri_cc_type_name, failed_msg);
9567 }
9568 }
9569 #endif /* defined(HAVE_PRI_CCSS) */
9570
9571 #if defined(HAVE_PRI_CCSS)
9572 /*!
9573 * \brief Request the status of the agent's device.
9574 * \since 1.8
9575 *
9576 * \param agent CC core agent control.
9577 *
9578 * \details
9579 * Asynchronous request for the status of any caller
9580 * which may be a valid caller for the CC transaction.
9581 * Status responses should be made using the
9582 * ast_cc_status_response function.
9583 *
9584 * \retval 0 on success.
9585 * \retval -1 on error.
9586 */
9587 int sig_pri_cc_agent_status_req(struct ast_cc_agent *agent)
9588 {
9589 struct sig_pri_cc_agent_prv *cc_pvt;
9590
9591 cc_pvt = agent->private_data;
9592 ast_mutex_lock(&cc_pvt->pri->lock);
9593 pri_cc_status_req(cc_pvt->pri->pri, cc_pvt->cc_id);
9594 ast_mutex_unlock(&cc_pvt->pri->lock);
9595 return 0;
9596 }
9597 #endif /* defined(HAVE_PRI_CCSS) */
9598
9599 #if defined(HAVE_PRI_CCSS)
9600 /*!
9601 * \brief Request for an agent's phone to stop ringing.
9602 * \since 1.8
9603 *
9604 * \param agent CC core agent control.
9605 *
9606 * \details
9607 * The usefulness of this is quite limited. The only specific
9608 * known case for this is if Asterisk requests CC over an ISDN
9609 * PTMP link as the TE side. If other phones are in the same
9610 * recall group as the Asterisk server, and one of those phones
9611 * picks up the recall notice, then Asterisk will receive a
9612 * "stop ringing" notification from the NT side of the PTMP
9613 * link. This indication needs to be passed to the phone
9614 * on the other side of the Asterisk server which originally
9615 * placed the call so that it will stop ringing. Since the
9616 * phone may be of any type, it is necessary to have a callback
9617 * that the core can know about.
9618 *
9619 * \retval 0 on success.
9620 * \retval -1 on error.
9621 */
9622 int sig_pri_cc_agent_stop_ringing(struct ast_cc_agent *agent)
9623 {
9624 struct sig_pri_cc_agent_prv *cc_pvt;
9625
9626 cc_pvt = agent->private_data;
9627 ast_mutex_lock(&cc_pvt->pri->lock);
9628 pri_cc_stop_alerting(cc_pvt->pri->pri, cc_pvt->cc_id);
9629 ast_mutex_unlock(&cc_pvt->pri->lock);
9630 return 0;
9631 }
9632 #endif /* defined(HAVE_PRI_CCSS) */
9633
9634 #if defined(HAVE_PRI_CCSS)
9635 /*!
9636 * \brief Let the caller know that the callee has become free
9637 * but that the caller cannot attempt to call back because
9638 * he is either busy or there is congestion on his line.
9639 * \since 1.8
9640 *
9641 * \param agent CC core agent control.
9642 *
9643 * \details
9644 * This is something that really only affects a scenario where
9645 * a phone places a call over ISDN PTMP to Asterisk, who then
9646 * connects over PTMP again to the ISDN network. For most agent
9647 * types, there is no need to implement this callback at all
9648 * because they don't really need to actually do anything in
9649 * this situation. If you're having trouble understanding what
9650 * the purpose of this callback is, then you can be safe simply
9651 * not implementing it.
9652 *
9653 * \retval 0 on success.
9654 * \retval -1 on error.
9655 */
9656 int sig_pri_cc_agent_party_b_free(struct ast_cc_agent *agent)
9657 {
9658 struct sig_pri_cc_agent_prv *cc_pvt;
9659
9660 cc_pvt = agent->private_data;
9661 ast_mutex_lock(&cc_pvt->pri->lock);
9662 pri_cc_b_free(cc_pvt->pri->pri, cc_pvt->cc_id);
9663 ast_mutex_unlock(&cc_pvt->pri->lock);
9664 return 0;
9665 }
9666 #endif /* defined(HAVE_PRI_CCSS) */
9667
9668 #if defined(HAVE_PRI_CCSS)
9669 /*!
9670 * \brief Begin monitoring a busy device.
9671 * \since 1.8
9672 *
9673 * \param agent CC core agent control.
9674 *
9675 * \details
9676 * The core will call this callback if the callee becomes
9677 * available but the caller has reported that he is busy.
9678 * The agent should begin monitoring the caller's device.
9679 * When the caller becomes available again, the agent should
9680 * call ast_cc_agent_caller_available.
9681 *
9682 * \retval 0 on success.
9683 * \retval -1 on error.
9684 */
9685 int sig_pri_cc_agent_start_monitoring(struct ast_cc_agent *agent)
9686 {
9687 /* libpri already knows when and how it needs to monitor Party A. */
9688 return 0;
9689 }
9690 #endif /* defined(HAVE_PRI_CCSS) */
9691
9692 #if defined(HAVE_PRI_CCSS)
9693 /*!
9694 * \brief Alert the caller that it is time to try recalling.
9695 * \since 1.8
9696 *
9697 * \param agent CC core agent control.
9698 *
9699 * \details
9700 * The core will call this function when it receives notice
9701 * that a monitored party has become available.
9702 *
9703 * The agent's job is to send a message to the caller to
9704 * notify it of such a change. If the agent is able to
9705 * discern that the caller is currently unavailable, then
9706 * the agent should react by calling the ast_cc_caller_unavailable
9707 * function.
9708 *
9709 * \retval 0 on success.
9710 * \retval -1 on error.
9711 */
9712 int sig_pri_cc_agent_callee_available(struct ast_cc_agent *agent)
9713 {
9714 struct sig_pri_cc_agent_prv *cc_pvt;
9715
9716 cc_pvt = agent->private_data;
9717 ast_mutex_lock(&cc_pvt->pri->lock);
9718 pri_cc_remote_user_free(cc_pvt->pri->pri, cc_pvt->cc_id);
9719 ast_mutex_unlock(&cc_pvt->pri->lock);
9720 return 0;
9721 }
9722 #endif /* defined(HAVE_PRI_CCSS) */
9723
9724 #if defined(HAVE_PRI_CCSS)
9725 /*!
9726 * \brief Destroy private data on the agent.
9727 * \since 1.8
9728 *
9729 * \param agent CC core agent control.
9730 *
9731 * \details
9732 * The core will call this function upon completion
9733 * or failure of CC.
9734 *
9735 * \note
9736 * The agent private_data pointer may be NULL if the agent
9737 * constructor failed.
9738 */
9739 void sig_pri_cc_agent_destructor(struct ast_cc_agent *agent)
9740 {
9741 struct sig_pri_cc_agent_prv *cc_pvt;
9742 int res;
9743
9744 cc_pvt = agent->private_data;
9745 if (!cc_pvt) {
9746 /* The agent constructor probably failed. */
9747 return;
9748 }
9749 ast_mutex_lock(&cc_pvt->pri->lock);
9750 res = -1;
9751 if (cc_pvt->cc_request_response_pending) {
9752 res = pri_cc_req_rsp(cc_pvt->pri->pri, cc_pvt->cc_id, 2/* short_term_denial */);
9753 }
9754 if (res) {
9755 pri_cc_cancel(cc_pvt->pri->pri, cc_pvt->cc_id);
9756 }
9757 ast_mutex_unlock(&cc_pvt->pri->lock);
9758 ast_free(cc_pvt);
9759 }
9760 #endif /* defined(HAVE_PRI_CCSS) */
9761
9762 #if defined(HAVE_PRI_CCSS)
9763 /*!
9764 * \internal
9765 * \brief Return the hash value of the given CC monitor instance object.
9766 * \since 1.8
9767 *
9768 * \param obj pointer to the (user-defined part) of an object.
9769 * \param flags flags from ao2_callback(). Ignored at the moment.
9770 *
9771 * \retval core_id
9772 */
9773 static int sig_pri_cc_monitor_instance_hash_fn(const void *obj, const int flags)
9774 {
9775 const struct sig_pri_cc_monitor_instance *monitor_instance = obj;
9776
9777 return monitor_instance->core_id;
9778 }
9779 #endif /* defined(HAVE_PRI_CCSS) */
9780
9781 #if defined(HAVE_PRI_CCSS)
9782 /*!
9783 * \internal
9784 * \brief Compere the monitor instance core_id key value.
9785 * \since 1.8
9786 *
9787 * \param obj pointer to the (user-defined part) of an object.
9788 * \param arg callback argument from ao2_callback()
9789 * \param flags flags from ao2_callback()
9790 *
9791 * \return values are a combination of enum _cb_results.
9792 */
9793 static int sig_pri_cc_monitor_instance_cmp_fn(void *obj, void *arg, int flags)
9794 {
9795 struct sig_pri_cc_monitor_instance *monitor_1 = obj;
9796 struct sig_pri_cc_monitor_instance *monitor_2 = arg;
9797
9798 return monitor_1->core_id == monitor_2->core_id ? CMP_MATCH | CMP_STOP : 0;
9799 }
9800 #endif /* defined(HAVE_PRI_CCSS) */
9801
9802 #if defined(HAVE_PRI_CCSS)
9803 /*!
9804 * \brief Request CCSS.
9805 * \since 1.8
9806 *
9807 * \param monitor CC core monitor control.
9808 * \param available_timer_id Where to put the available timer scheduler id.
9809 * Will never be NULL for a device monitor.
9810 *
9811 * \details
9812 * Perform whatever steps are necessary in order to request CC.
9813 * In addition, the monitor implementation is responsible for
9814 * starting the available timer in this callback. The scheduler
9815 * ID for the callback must be stored in the parent_link's child_avail_id
9816 * field.
9817 *
9818 * \retval 0 on success
9819 * \retval -1 on failure.
9820 */
9821 int sig_pri_cc_monitor_req_cc(struct ast_cc_monitor *monitor, int *available_timer_id)
9822 {
9823 struct sig_pri_cc_monitor_instance *instance;
9824 int cc_mode;
9825 int res;
9826
9827 switch (monitor->service_offered) {
9828 case AST_CC_CCBS:
9829 cc_mode = 0;/* CCBS */
9830 break;
9831 case AST_CC_CCNR:
9832 cc_mode = 1;/* CCNR */
9833 break;
9834 default:
9835 /* CC service not supported by ISDN. */
9836 return -1;
9837 }
9838
9839 instance = monitor->private_data;
9840
9841 /* libpri handles it's own available timer. */
9842 ast_mutex_lock(&instance->pri->lock);
9843 res = pri_cc_req(instance->pri->pri, instance->cc_id, cc_mode);
9844 ast_mutex_unlock(&instance->pri->lock);
9845
9846 return res;
9847 }
9848 #endif /* defined(HAVE_PRI_CCSS) */
9849
9850 #if defined(HAVE_PRI_CCSS)
9851 /*!
9852 * \brief Suspend monitoring.
9853 * \since 1.8
9854 *
9855 * \param monitor CC core monitor control.
9856 *
9857 * \details
9858 * Implementers must perform the necessary steps to suspend
9859 * monitoring.
9860 *
9861 * \retval 0 on success
9862 * \retval -1 on failure.
9863 */
9864 int sig_pri_cc_monitor_suspend(struct ast_cc_monitor *monitor)
9865 {
9866 struct sig_pri_cc_monitor_instance *instance;
9867
9868 instance = monitor->private_data;
9869 ast_mutex_lock(&instance->pri->lock);
9870 pri_cc_status(instance->pri->pri, instance->cc_id, 1/* busy */);
9871 ast_mutex_unlock(&instance->pri->lock);
9872
9873 return 0;
9874 }
9875 #endif /* defined(HAVE_PRI_CCSS) */
9876
9877 #if defined(HAVE_PRI_CCSS)
9878 /*!
9879 * \brief Unsuspend monitoring.
9880 * \since 1.8
9881 *
9882 * \param monitor CC core monitor control.
9883 *
9884 * \details
9885 * Perform the necessary steps to unsuspend monitoring.
9886 *
9887 * \retval 0 on success
9888 * \retval -1 on failure.
9889 */
9890 int sig_pri_cc_monitor_unsuspend(struct ast_cc_monitor *monitor)
9891 {
9892 struct sig_pri_cc_monitor_instance *instance;
9893
9894 instance = monitor->private_data;
9895 ast_mutex_lock(&instance->pri->lock);
9896 pri_cc_status(instance->pri->pri, instance->cc_id, 0/* free */);
9897 ast_mutex_unlock(&instance->pri->lock);
9898
9899 return 0;
9900 }
9901 #endif /* defined(HAVE_PRI_CCSS) */
9902
9903 #if defined(HAVE_PRI_CCSS)
9904 /*!
9905 * \brief Status response to an ast_cc_monitor_status_request().
9906 * \since 1.8
9907 *
9908 * \param monitor CC core monitor control.
9909 * \param devstate Current status of a Party A device.
9910 *
9911 * \details
9912 * Alert a monitor as to the status of the agent for which
9913 * the monitor had previously requested a status request.
9914 *
9915 * \note Zero or more responses may come as a result.
9916 *
9917 * \retval 0 on success
9918 * \retval -1 on failure.
9919 */
9920 int sig_pri_cc_monitor_status_rsp(struct ast_cc_monitor *monitor, enum ast_device_state devstate)
9921 {
9922 struct sig_pri_cc_monitor_instance *instance;
9923 int cc_status;
9924
9925 switch (devstate) {
9926 case AST_DEVICE_UNKNOWN:
9927 case AST_DEVICE_NOT_INUSE:
9928 cc_status = 0;/* free */
9929 break;
9930 case AST_DEVICE_BUSY:
9931 case AST_DEVICE_INUSE:
9932 cc_status = 1;/* busy */
9933 break;
9934 default:
9935 /* Don't know how to interpret this device state into free/busy status. */
9936 return 0;
9937 }
9938 instance = monitor->private_data;
9939 ast_mutex_lock(&instance->pri->lock);
9940 pri_cc_status_req_rsp(instance->pri->pri, instance->cc_id, cc_status);
9941 ast_mutex_unlock(&instance->pri->lock);
9942
9943 return 0;
9944 }
9945 #endif /* defined(HAVE_PRI_CCSS) */
9946
9947 #if defined(HAVE_PRI_CCSS)
9948 /*!
9949 * \brief Cancel the running available timer.
9950 * \since 1.8
9951 *
9952 * \param monitor CC core monitor control.
9953 * \param sched_id Available timer scheduler id to cancel.
9954 * Will never be NULL for a device monitor.
9955 *
9956 * \details
9957 * In most cases, this function will likely consist of just a
9958 * call to AST_SCHED_DEL. It might have been possible to do this
9959 * within the core, but unfortunately the mixture of sched_thread
9960 * and sched usage in Asterisk prevents such usage.
9961 *
9962 * \retval 0 on success
9963 * \retval -1 on failure.
9964 */
9965 int sig_pri_cc_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id)
9966 {
9967 /*
9968 * libpri maintains it's own available timer as one of:
9969 * T_CCBS2/T_CCBS5/T_CCBS6/QSIG_CCBS_T2
9970 * T_CCNR2/T_CCNR5/T_CCNR6/QSIG_CCNR_T2
9971 */
9972 return 0;
9973 }
9974 #endif /* defined(HAVE_PRI_CCSS) */
9975
9976 #if defined(HAVE_PRI_CCSS)
9977 /*!
9978 * \brief Destroy PRI private data on the monitor.
9979 * \since 1.8
9980 *
9981 * \param monitor_pvt CC device monitor private data pointer.
9982 *
9983 * \details
9984 * Implementers of this callback are responsible for destroying
9985 * all heap-allocated data in the monitor's private_data pointer, including
9986 * the private_data itself.
9987 */
9988 void sig_pri_cc_monitor_destructor(void *monitor_pvt)
9989 {
9990 struct sig_pri_cc_monitor_instance *instance;
9991
9992 instance = monitor_pvt;
9993 if (!instance) {
9994 return;
9995 }
9996 ao2_unlink(sig_pri_cc_monitors, instance);
9997 ao2_ref(instance, -1);
9998 }
9999 #endif /* defined(HAVE_PRI_CCSS) */
10000
10001 /*!
10002 * \brief Load the sig_pri submodule.
10003 * \since 1.8
10004 *
10005 * \param cc_type_name CC type name to use when looking up agent/monitor.
10006 *
10007 * \retval 0 on success.
10008 * \retval -1 on error.
10009 */
10010 int sig_pri_load(const char *cc_type_name)
10011 {
10012 #if defined(HAVE_PRI_MCID)
10013 if (STASIS_MESSAGE_TYPE_INIT(mcid_type)) {
10014 return -1;
10015 }
10016 #endif /* defined(HAVE_PRI_MCID) */
10017
10018 #if defined(HAVE_PRI_CCSS)
10019 sig_pri_cc_type_name = cc_type_name;
10020 sig_pri_cc_monitors = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 37,
10021 sig_pri_cc_monitor_instance_hash_fn, NULL, sig_pri_cc_monitor_instance_cmp_fn);
10022 if (!sig_pri_cc_monitors) {
10023 return -1;
10024 }
10025 #endif /* defined(HAVE_PRI_CCSS) */
10026 return 0;
10027 }
10028
10029 /*!
10030 * \brief Unload the sig_pri submodule.
10031 * \since 1.8
10032 */
10033 void sig_pri_unload(void)
10034 {
10035 #if defined(HAVE_PRI_CCSS)
10036 if (sig_pri_cc_monitors) {
10037 ao2_ref(sig_pri_cc_monitors, -1);
10038 sig_pri_cc_monitors = NULL;
10039 }
10040 #endif /* defined(HAVE_PRI_CCSS) */
10041
10042 #if defined(HAVE_PRI_MCID)
10043 STASIS_MESSAGE_TYPE_CLEANUP(mcid_type);
10044 #endif /* defined(HAVE_PRI_MCID) */
10045 }
10046
10047 #endif /* HAVE_PRI */