4 * Asterisk -- An open source telephony toolkit.
6 * Copyright (C) 1999 - 2009, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
23 * \brief Interface header for PRI signaling module
25 * \author Matthew Fredrickson <creslin@digium.com>
28 #include "asterisk/channel.h"
29 #include "asterisk/frame.h"
30 #include "asterisk/ccss.h"
32 #include <dahdi/user.h>
34 #if defined(HAVE_PRI_CCSS)
35 /*! PRI debug message flags when normal PRI debugging is turned on at the command line. */
36 #define SIG_PRI_DEBUG_NORMAL \
37 (PRI_DEBUG_APDU | PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE | PRI_DEBUG_Q921_STATE \
40 /*! PRI debug message flags when intense PRI debugging is turned on at the command line. */
41 #define SIG_PRI_DEBUG_INTENSE \
42 (PRI_DEBUG_APDU | PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE | PRI_DEBUG_Q921_STATE \
43 | PRI_DEBUG_CC | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_DUMP)
47 /*! PRI debug message flags when normal PRI debugging is turned on at the command line. */
48 #define SIG_PRI_DEBUG_NORMAL \
49 (PRI_DEBUG_APDU | PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE | PRI_DEBUG_Q921_STATE)
51 /*! PRI debug message flags when intense PRI debugging is turned on at the command line. */
52 #define SIG_PRI_DEBUG_INTENSE \
53 (PRI_DEBUG_APDU | PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE | PRI_DEBUG_Q921_STATE \
54 | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_DUMP)
55 #endif /* !defined(HAVE_PRI_CCSS) */
58 /*! PRI debug message flags set on initial startup. */
59 #define SIG_PRI_DEBUG_DEFAULT SIG_PRI_DEBUG_NORMAL
61 /*! PRI debug message flags set on initial startup. */
62 #define SIG_PRI_DEBUG_DEFAULT 0
65 #define SIG_PRI_AOC_GRANT_S (1 << 0)
66 #define SIG_PRI_AOC_GRANT_D (1 << 1)
67 #define SIG_PRI_AOC_GRANT_E (1 << 2)
70 SIG_PRI_TONE_RINGTONE = 0,
72 SIG_PRI_TONE_CONGESTION,
73 SIG_PRI_TONE_DIALTONE,
74 SIG_PRI_TONE_DIALRECALL,
87 struct sig_pri_callback {
88 /* Unlock the private in the signalling private structure. This is used for three way calling madness. */
89 void (* const unlock_private)(void *pvt);
90 /* Lock the private in the signalling private structure. ... */
91 void (* const lock_private)(void *pvt);
92 /* Function which is called back to handle any other DTMF up events that are received. Called by analog_handle_event. Why is this
93 * important to use, instead of just directly using events received before they are passed into the library? Because sometimes,
94 * (CWCID) the library absorbs DTMF events received. */
95 //void (* const handle_dtmfup)(void *pvt, struct ast_channel *ast, enum analog_sub analog_index, struct ast_frame **dest);
97 //int (* const dial_digits)(void *pvt, enum analog_sub sub, struct analog_dialoperation *dop);
98 int (* const play_tone)(void *pvt, enum sig_pri_tone tone);
100 int (* const set_echocanceller)(void *pvt, int enable);
101 int (* const train_echocanceller)(void *pvt);
102 int (* const dsp_reset_and_flush_digits)(void *pvt);
104 struct ast_channel * (* const new_ast_channel)(void *pvt, int state, int startpbx, enum sig_pri_law law, int transfercapability, char *exten, const struct ast_channel *chan);
106 void (* const fixup_chans)(void *old_chan, void *new_chan);
108 /* Note: Called with PRI lock held */
109 void (* const handle_dchan_exception)(struct sig_pri_pri *pri, int index);
110 void (* const set_alarm)(void *pvt, int in_alarm);
111 void (* const set_dialing)(void *pvt, int is_dialing);
112 void (* const set_digital)(void *pvt, int is_digital);
113 void (* const set_callerid)(void *pvt, const struct ast_party_caller *caller);
114 void (* const set_dnid)(void *pvt, const char *dnid);
115 void (* const set_rdnis)(void *pvt, const char *rdnis);
116 void (* const queue_control)(void *pvt, int subclass);
117 int (* const new_nobch_intf)(struct sig_pri_pri *pri);
118 void (* const init_config)(void *pvt, struct sig_pri_pri *pri);
119 const char *(* const get_orig_dialstring)(void *pvt);
120 void (* const make_cc_dialstring)(void *pvt, char *buf, size_t buf_size);
121 void (* const update_span_devstate)(struct sig_pri_pri *pri);
123 void (* const open_media)(void *pvt);
125 /*! Reference the parent module. */
126 void (*module_ref)(void);
127 /*! Unreference the parent module. */
128 void (*module_unref)(void);
131 #define SIG_PRI_NUM_DCHANS 4 /*!< No more than 4 d-channels */
132 #define SIG_PRI_MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
134 #define SIG_PRI DAHDI_SIG_CLEAR
135 #define SIG_BRI (0x2000000 | DAHDI_SIG_CLEAR)
136 #define SIG_BRI_PTMP (0X4000000 | DAHDI_SIG_CLEAR)
138 /* QSIG channel mapping option types */
139 #define DAHDI_CHAN_MAPPING_PHYSICAL 0
140 #define DAHDI_CHAN_MAPPING_LOGICAL 1
142 /* Overlap dialing option types */
143 #define DAHDI_OVERLAPDIAL_NONE 0
144 #define DAHDI_OVERLAPDIAL_OUTGOING 1
145 #define DAHDI_OVERLAPDIAL_INCOMING 2
146 #define DAHDI_OVERLAPDIAL_BOTH (DAHDI_OVERLAPDIAL_INCOMING|DAHDI_OVERLAPDIAL_OUTGOING)
148 #if defined(HAVE_PRI_SERVICE_MESSAGES)
149 /*! \brief Persistent Service State */
150 #define SRVST_DBKEY "service-state"
151 /*! \brief The out-of-service SERVICE state */
152 #define SRVST_TYPE_OOS "O"
153 /*! \brief SRVST_INITIALIZED is used to indicate a channel being out-of-service
154 * The SRVST_INITIALIZED is mostly used maintain backwards compatibility but also may
155 * mean that the channel has not yet received a RESTART message. If a channel is
156 * out-of-service with this reason a RESTART message will result in the channel
157 * being put into service. */
158 #define SRVST_INITIALIZED 0
159 /*! \brief SRVST_NEAREND is used to indicate that the near end was put out-of-service */
160 #define SRVST_NEAREND (1 << 0)
161 /*! \brief SRVST_FAREND is used to indicate that the far end was taken out-of-service */
162 #define SRVST_FAREND (1 << 1)
163 /*! \brief SRVST_BOTH is used to indicate that both sides of the channel are out-of-service */
164 #define SRVST_BOTH (SRVST_NEAREND | SRVST_FAREND)
166 /*! \brief The AstDB family */
167 static const char dahdi_db[] = "dahdi/registry";
168 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
170 struct sig_pri_chan {
171 /* Options to be set by user */
172 unsigned int hidecallerid:1;
173 unsigned int hidecalleridname:1; /*!< Hide just the name not the number for legacy PBX use */
174 unsigned int immediate:1; /*!< Answer before getting digits? */
175 unsigned int priexclusive:1; /*!< Whether or not to override and use exculsive mode for channel selection */
176 unsigned int priindication_oob:1;
177 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
178 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
179 char context[AST_MAX_CONTEXT];
180 char mohinterpret[MAX_MUSICCLASS];
182 int channel; /*!< Channel Number or CRV */
184 /* Options to be checked by user */
185 int cid_ani2; /*!< Automatic Number Identification number (Alternate PRI caller ID number) */
186 int cid_ton; /*!< Type Of Number (TON) */
187 int callingpres; /*!< The value of calling presentation that we're going to use when placing a PRI call */
188 char cid_num[AST_MAX_EXTENSION];
189 char cid_subaddr[AST_MAX_EXTENSION];
190 char cid_name[AST_MAX_EXTENSION];
191 char cid_ani[AST_MAX_EXTENSION];
192 char exten[AST_MAX_EXTENSION];
194 /* Internal variables -- Don't touch */
195 /* Probably will need DS0 number, DS1 number, and a few other things */
196 char dialdest[256]; /* Queued up digits for overlap dialing. They will be sent out as information messages when setup ACK is received */
197 #if defined(HAVE_PRI_SETUP_KEYPAD)
198 /*! \brief Keypad digits that came in with the SETUP message. */
199 char keypad_digits[AST_MAX_EXTENSION];
200 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
202 #if defined(HAVE_PRI_AOC_EVENTS)
203 struct pri_subcmd_aoc_e aoc_e;
204 int aoc_s_request_invoke_id; /*!< If an AOC-S request was present for the call, this is the invoke_id to use for the response */
205 unsigned int aoc_s_request_invoke_id_valid:1; /*!< This is set when the AOC-S invoke id is present */
206 unsigned int waiting_for_aoce:1; /*!< Delaying hangup for AOC-E msg. If this is set and AOC-E is recieved, continue with hangup before timeout period. */
207 unsigned int holding_aoce:1; /*!< received AOC-E msg from asterisk. holding for disconnect/release */
208 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
209 unsigned int inalarm:1;
210 unsigned int alerting:1; /*!< TRUE if channel is alerting/ringing */
211 unsigned int alreadyhungup:1; /*!< TRUE if the call has already gone/hungup */
212 unsigned int isidlecall:1; /*!< TRUE if this is an idle call */
213 unsigned int proceeding:1; /*!< TRUE if call is in a proceeding state */
214 unsigned int progress:1; /*!< TRUE if the call has seen progress through the network */
215 unsigned int resetting:1; /*!< TRUE if this channel is being reset/restarted */
216 unsigned int setup_ack:1; /*!< TRUE if this channel has received a SETUP_ACKNOWLEDGE */
218 unsigned int outgoing:1;
219 unsigned int digital:1;
220 /*! \brief TRUE if this interface has no B channel. (call hold and call waiting) */
221 unsigned int no_b_channel:1;
222 #if defined(HAVE_PRI_CALL_WAITING)
223 /*! \brief TRUE if this is a call waiting call */
224 unsigned int is_call_waiting:1;
225 #endif /* defined(HAVE_PRI_CALL_WAITING) */
227 struct ast_channel *owner;
229 struct sig_pri_pri *pri;
230 q931_call *call; /*!< opaque libpri call control structure */
232 int prioffset; /*!< channel number in span */
233 int logicalspan; /*!< logical span number within trunk group */
234 int mastertrunkgroup; /*!< what trunk group is our master */
235 #if defined(HAVE_PRI_SERVICE_MESSAGES)
236 /*! \brief Active SRVST_DBKEY out-of-service status value. */
237 unsigned service_status;
238 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
240 struct sig_pri_callback *calls;
241 void *chan_pvt; /*!< Private structure of the user of this module. */
242 #if defined(HAVE_PRI_REVERSE_CHARGE)
244 * \brief Reverse charging indication
246 * -1 - No reverse charging,
247 * 1 - Reverse charging,
248 * 0,2-7 - Reserved for future use
250 int reverse_charging_indication;
255 /* Should be set by user */
256 struct ast_cc_config_params *cc_params; /*!< CC config parameters for each new call. */
257 int pritimers[PRI_MAX_TIMERS];
258 int overlapdial; /*!< In overlap dialing mode */
259 int qsigchannelmapping; /*!< QSIG channel mapping type */
260 int discardremoteholdretrieval; /*!< shall remote hold or remote retrieval notifications be discarded? */
261 int facilityenable; /*!< Enable facility IEs */
262 int dchan_logical_span[SIG_PRI_NUM_DCHANS]; /*!< Logical offset the DCHAN sits in */
263 int fds[SIG_PRI_NUM_DCHANS]; /*!< FD's for d-channels */
265 #if defined(HAVE_PRI_AOC_EVENTS)
266 int aoc_passthrough_flag; /*!< Represents what AOC messages (S,D,E) are allowed to pass-through */
267 int aoce_delayhangup:1; /*!< defines whether the aoce_delayhangup option is enabled or not */
268 #endif /* defined(HAVE_PRI_AOC_EVENTS) */
270 #if defined(HAVE_PRI_SERVICE_MESSAGES)
271 unsigned int enable_service_message_support:1; /*!< enable SERVICE message support */
272 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
273 #ifdef HAVE_PRI_INBANDDISCONNECT
274 unsigned int inbanddisconnect:1; /*!< Should we support inband audio after receiving DISCONNECT? */
276 #if defined(HAVE_PRI_CALL_HOLD)
277 /*! \brief TRUE if held calls are transferred on disconnect. */
278 unsigned int hold_disconnect_transfer:1;
279 #endif /* defined(HAVE_PRI_CALL_HOLD) */
281 * \brief TRUE if call transfer is enabled for the span.
282 * \note Support switch-side transfer (called 2BCT, RLT or other names)
284 unsigned int transfer:1;
285 #if defined(HAVE_PRI_CALL_WAITING)
286 /*! \brief TRUE if we will allow incoming ISDN call waiting calls. */
287 unsigned int allow_call_waiting_calls:1;
288 #endif /* defined(HAVE_PRI_CALL_WAITING) */
289 int dialplan; /*!< Dialing plan */
290 int localdialplan; /*!< Local dialing plan */
291 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
292 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
293 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
294 char privateprefix[20]; /*!< for private dialplans */
295 char unknownprefix[20]; /*!< for unknown dialplans */
296 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
297 char msn_list[AST_MAX_EXTENSION]; /*!< Comma separated list of MSNs to handle. Empty if disabled. */
298 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
299 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
300 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
301 int minunused; /*!< Min # of channels to keep empty */
302 int minidle; /*!< Min # of "idling" calls to keep active */
303 int nodetype; /*!< Node type */
304 int switchtype; /*!< Type of switch to emulate */
305 int nsf; /*!< Network-Specific Facilities */
306 int trunkgroup; /*!< What our trunkgroup is */
307 #if defined(HAVE_PRI_CCSS)
308 int cc_ptmp_recall_mode; /*!< CC PTMP recall mode. globalRecall(0), specificRecall(1) */
309 int cc_qsig_signaling_link_req; /*!< CC Q.SIG signaling link retention (Party A) release(0), retain(1), do-not-care(2) */
310 int cc_qsig_signaling_link_rsp; /*!< CC Q.SIG signaling link retention (Party B) release(0), retain(1) */
311 #endif /* defined(HAVE_PRI_CCSS) */
312 #if defined(HAVE_PRI_CALL_WAITING)
314 * \brief Number of extra outgoing calls to allow on a span before
315 * considering that span congested.
317 int max_call_waiting_calls;
320 unsigned int hidecallerid:1;
321 unsigned int hidecalleridname:1; /*!< Hide just the name not the number for legacy PBX use */
322 unsigned int immediate:1; /*!< Answer before getting digits? */
323 unsigned int priexclusive:1; /*!< Whether or not to override and use exculsive mode for channel selection */
324 unsigned int priindication_oob:1;
325 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
326 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
327 char context[AST_MAX_CONTEXT];
328 char mohinterpret[MAX_MUSICCLASS];
332 * \brief Number of outstanding call waiting calls.
333 * \note Must be zero to allow new calls from asterisk to
334 * immediately allocate a B channel.
336 int num_call_waiting_calls;
337 #endif /* defined(HAVE_PRI_CALL_WAITING) */
338 int dchanavail[SIG_PRI_NUM_DCHANS]; /*!< Whether each channel is available */
339 int debug; /*!< set to true if to dump PRI event info (tested but never set) */
340 int span; /*!< span number put into user output messages */
341 int resetting; /*!< true if span is being reset/restarted */
342 int resetpos; /*!< current position during a reset (-1 if not started) */
343 int sig; /*!< ISDN signalling type (SIG_PRI, SIG_BRI, SIG_BRI_PTMP, etc...) */
344 int new_chan_seq; /*!< New struct ast_channel sequence number */
346 /* Everything after here is internally set */
347 struct pri *dchans[SIG_PRI_NUM_DCHANS]; /*!< Actual d-channels */
348 struct pri *pri; /*!< Currently active D-channel */
350 * List of private structures of the user of this module for no B channel
351 * interfaces. (hold and call waiting interfaces)
353 void *no_b_chan_iflist;
355 * List of private structures of the user of this module for no B channel
356 * interfaces. (hold and call waiting interfaces)
359 int numchans; /*!< Num of channels we represent */
360 struct sig_pri_chan *pvts[SIG_PRI_MAX_CHANNELS];/*!< Member channel pvt structs */
361 pthread_t master; /*!< Thread of master */
362 ast_mutex_t lock; /*!< libpri access Mutex */
363 time_t lastreset; /*!< time when unused channels were last reset */
364 struct sig_pri_callback *calls;
366 * \brief Congestion device state of the span.
368 * AST_DEVICE_NOT_INUSE - Span does not have all B channels in use.
369 * AST_DEVICE_BUSY - All B channels are in use.
370 * AST_DEVICE_UNAVAILABLE - Span is in alarm.
372 * Device name: DAHDI/I<span>/congestion
374 int congestion_devstate;
375 #if defined(THRESHOLD_DEVSTATE_PLACEHOLDER)
376 /*! \todo An ISDN span threshold device state could be useful in determining how often a span utilization goes over a configurable threshold. */
378 * \brief User threshold device state of the span.
380 * AST_DEVICE_NOT_INUSE - There are no B channels in use.
381 * AST_DEVICE_INUSE - The number of B channels in use is less than
382 * the configured threshold but not zero.
383 * AST_DEVICE_BUSY - The number of B channels in use meets or exceeds
384 * the configured threshold.
385 * AST_DEVICE_UNAVAILABLE - Span is in alarm.
387 * Device name: DAHDI/I<span>/threshold
389 int threshold_devstate;
391 * \brief Number of B channels in use to consider the span in a busy state.
392 * \note Setting the threshold to zero is interpreted as all B channels.
394 int user_busy_threshold;
395 #endif /* defined(THRESHOLD_DEVSTATE_PLACEHOLDER) */
398 void sig_pri_extract_called_num_subaddr(struct sig_pri_chan *p, const char *rdest, char *called, size_t called_buff_size);
399 int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, int timeout, int layer1);
401 int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast);
403 int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen);
405 int sig_pri_answer(struct sig_pri_chan *p, struct ast_channel *ast);
407 int sig_pri_available(struct sig_pri_chan **pvt, int is_specific_channel);
409 void sig_pri_init_pri(struct sig_pri_pri *pri);
411 /* If return 0, it means this function was able to handle it (pre setup digits). If non zero, the user of this
412 * functions should handle it normally (generate inband DTMF) */
413 int sig_pri_digit_begin(struct sig_pri_chan *pvt, struct ast_channel *ast, char digit);
415 int sig_pri_start_pri(struct sig_pri_pri *pri);
417 void sig_pri_chan_alarm_notify(struct sig_pri_chan *p, int noalarm);
419 void pri_event_alarm(struct sig_pri_pri *pri, int index, int before_start_pri);
421 void pri_event_noalarm(struct sig_pri_pri *pri, int index, int before_start_pri);
423 struct ast_channel *sig_pri_request(struct sig_pri_chan *p, enum sig_pri_law law, const struct ast_channel *requestor, int transfercapability);
425 struct sig_pri_chan *sig_pri_chan_new(void *pvt_data, struct sig_pri_callback *callback, struct sig_pri_pri *pri, int logicalspan, int channo, int trunkgroup);
426 void sig_pri_chan_delete(struct sig_pri_chan *doomed);
428 int pri_is_up(struct sig_pri_pri *pri);
430 void sig_pri_cli_show_spans(int fd, int span, struct sig_pri_pri *pri);
432 void sig_pri_cli_show_span(int fd, int *dchannels, struct sig_pri_pri *pri);
434 int pri_send_keypad_facility_exec(struct sig_pri_chan *p, const char *digits);
435 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);
437 #if defined(HAVE_PRI_SERVICE_MESSAGES)
438 int pri_maintenance_bservice(struct pri *pri, struct sig_pri_chan *p, int changestatus);
439 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
441 void sig_pri_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_pri_chan *pchan);
443 int sig_pri_cc_agent_init(struct ast_cc_agent *agent, struct sig_pri_chan *pvt_chan);
444 int sig_pri_cc_agent_start_offer_timer(struct ast_cc_agent *agent);
445 int sig_pri_cc_agent_stop_offer_timer(struct ast_cc_agent *agent);
446 void sig_pri_cc_agent_req_ack(struct ast_cc_agent *agent);
447 int sig_pri_cc_agent_status_req(struct ast_cc_agent *agent);
448 int sig_pri_cc_agent_stop_ringing(struct ast_cc_agent *agent);
449 int sig_pri_cc_agent_party_b_free(struct ast_cc_agent *agent);
450 int sig_pri_cc_agent_start_monitoring(struct ast_cc_agent *agent);
451 int sig_pri_cc_agent_callee_available(struct ast_cc_agent *agent);
452 void sig_pri_cc_agent_destructor(struct ast_cc_agent *agent);
454 int sig_pri_cc_monitor_req_cc(struct ast_cc_monitor *monitor, int *available_timer_id);
455 int sig_pri_cc_monitor_suspend(struct ast_cc_monitor *monitor);
456 int sig_pri_cc_monitor_unsuspend(struct ast_cc_monitor *monitor);
457 int sig_pri_cc_monitor_status_rsp(struct ast_cc_monitor *monitor, enum ast_device_state devstate);
458 int sig_pri_cc_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id);
459 void sig_pri_cc_monitor_destructor(void *monitor_pvt);
461 int sig_pri_load(const char *cc_type_name);
462 void sig_pri_unload(void);
464 #endif /* _SIG_PRI_H */