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