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