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
66 SIG_PRI_TONE_RINGTONE = 0,
68 SIG_PRI_TONE_CONGESTION,
69 SIG_PRI_TONE_DIALTONE,
70 SIG_PRI_TONE_DIALRECALL,
83 struct sig_pri_callback {
84 /* Unlock the private in the signalling private structure. This is used for three way calling madness. */
85 void (* const unlock_private)(void *pvt);
86 /* Lock the private in the signalling private structure. ... */
87 void (* const lock_private)(void *pvt);
88 /* Function which is called back to handle any other DTMF up events that are received. Called by analog_handle_event. Why is this
89 * important to use, instead of just directly using events received before they are passed into the library? Because sometimes,
90 * (CWCID) the library absorbs DTMF events received. */
91 //void (* const handle_dtmfup)(void *pvt, struct ast_channel *ast, enum analog_sub analog_index, struct ast_frame **dest);
93 //int (* const dial_digits)(void *pvt, enum analog_sub sub, struct analog_dialoperation *dop);
94 int (* const play_tone)(void *pvt, enum sig_pri_tone tone);
96 int (* const set_echocanceller)(void *pvt, int enable);
97 int (* const train_echocanceller)(void *pvt);
98 int (* const dsp_reset_and_flush_digits)(void *pvt);
100 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);
102 void (* const fixup_chans)(void *old_chan, void *new_chan);
104 /* Note: Called with PRI lock held */
105 void (* const handle_dchan_exception)(struct sig_pri_pri *pri, int index);
106 void (* const set_dialing)(void *pvt, int flag);
107 void (* const set_digital)(void *pvt, int flag);
108 void (* const set_callerid)(void *pvt, const struct ast_party_caller *caller);
109 void (* const set_dnid)(void *pvt, const char *dnid);
110 void (* const set_rdnis)(void *pvt, const char *rdnis);
111 void (* const queue_control)(void *pvt, int subclass);
112 int (* const new_nobch_intf)(struct sig_pri_pri *pri);
113 const char *(* const get_orig_dialstring)(void *pvt);
114 void (* const make_cc_dialstring)(void *pvt, char *buf, size_t buf_size);
115 void (* const update_span_devstate)(struct sig_pri_pri *pri);
117 /*! Reference the parent module. */
118 void (*module_ref)(void);
119 /*! Unreference the parent module. */
120 void (*module_unref)(void);
123 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
124 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
126 #define SIG_PRI DAHDI_SIG_CLEAR
127 #define SIG_BRI (0x2000000 | DAHDI_SIG_CLEAR)
128 #define SIG_BRI_PTMP (0X4000000 | DAHDI_SIG_CLEAR)
130 /* QSIG channel mapping option types */
131 #define DAHDI_CHAN_MAPPING_PHYSICAL 0
132 #define DAHDI_CHAN_MAPPING_LOGICAL 1
134 /* Overlap dialing option types */
135 #define DAHDI_OVERLAPDIAL_NONE 0
136 #define DAHDI_OVERLAPDIAL_OUTGOING 1
137 #define DAHDI_OVERLAPDIAL_INCOMING 2
138 #define DAHDI_OVERLAPDIAL_BOTH (DAHDI_OVERLAPDIAL_INCOMING|DAHDI_OVERLAPDIAL_OUTGOING)
140 #if defined(HAVE_PRI_SERVICE_MESSAGES)
141 /*! \brief Persistent Service State */
142 #define SRVST_DBKEY "service-state"
143 /*! \brief The out-of-service SERVICE state */
144 #define SRVST_TYPE_OOS "O"
145 /*! \brief SRVST_INITIALIZED is used to indicate a channel being out-of-service
146 * The SRVST_INITIALIZED is mostly used maintain backwards compatibility but also may
147 * mean that the channel has not yet received a RESTART message. If a channel is
148 * out-of-service with this reason a RESTART message will result in the channel
149 * being put into service. */
150 #define SRVST_INITIALIZED 0
151 /*! \brief SRVST_NEAREND is used to indicate that the near end was put out-of-service */
152 #define SRVST_NEAREND (1 << 0)
153 /*! \brief SRVST_FAREND is used to indicate that the far end was taken out-of-service */
154 #define SRVST_FAREND (1 << 1)
155 /*! \brief SRVST_BOTH is used to indicate that both sides of the channel are out-of-service */
156 #define SRVST_BOTH (SRVST_NEAREND | SRVST_FAREND)
158 /*! \brief The AstDB family */
159 static const char dahdi_db[] = "dahdi/registry";
160 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
162 struct sig_pri_chan {
163 /* Options to be set by user */
164 unsigned int hidecallerid:1;
165 unsigned int hidecalleridname:1; /*!< Hide just the name not the number for legacy PBX use */
166 unsigned int immediate:1; /*!< Answer before getting digits? */
167 unsigned int priexclusive:1; /*!< Whether or not to override and use exculsive mode for channel selection */
168 unsigned int priindication_oob:1;
169 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
170 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
171 char context[AST_MAX_CONTEXT];
172 char mohinterpret[MAX_MUSICCLASS];
174 int channel; /*!< Channel Number or CRV */
176 /* Options to be checked by user */
177 int cid_ani2; /*!< Automatic Number Identification number (Alternate PRI caller ID number) */
178 int cid_ton; /*!< Type Of Number (TON) */
179 int callingpres; /*!< The value of calling presentation that we're going to use when placing a PRI call */
180 char cid_num[AST_MAX_EXTENSION];
181 char cid_subaddr[AST_MAX_EXTENSION];
182 char cid_name[AST_MAX_EXTENSION];
183 char cid_ani[AST_MAX_EXTENSION];
184 char exten[AST_MAX_EXTENSION];
186 /* Internal variables -- Don't touch */
187 /* Probably will need DS0 number, DS1 number, and a few other things */
188 char dialdest[256]; /* Queued up digits for overlap dialing. They will be sent out as information messages when setup ACK is received */
189 #if defined(HAVE_PRI_SETUP_KEYPAD)
190 /*! \brief Keypad digits that came in with the SETUP message. */
191 char keypad_digits[AST_MAX_EXTENSION];
192 #endif /* defined(HAVE_PRI_SETUP_KEYPAD) */
194 unsigned int inalarm:1;
195 unsigned int alerting:1; /*!< TRUE if channel is alerting/ringing */
196 unsigned int alreadyhungup:1; /*!< TRUE if the call has already gone/hungup */
197 unsigned int isidlecall:1; /*!< TRUE if this is an idle call */
198 unsigned int proceeding:1; /*!< TRUE if call is in a proceeding state */
199 unsigned int progress:1; /*!< TRUE if the call has seen progress through the network */
200 unsigned int resetting:1; /*!< TRUE if this channel is being reset/restarted */
201 unsigned int setup_ack:1; /*!< TRUE if this channel has received a SETUP_ACKNOWLEDGE */
203 unsigned int outgoing:1;
204 unsigned int digital:1;
205 /*! \brief TRUE if this interface has no B channel. (call hold and call waiting) */
206 unsigned int no_b_channel:1;
208 struct ast_channel *owner;
210 struct sig_pri_pri *pri;
211 q931_call *call; /*!< opaque libpri call control structure */
213 int prioffset; /*!< channel number in span */
214 int logicalspan; /*!< logical span number within trunk group */
215 int mastertrunkgroup; /*!< what trunk group is our master */
216 #if defined(HAVE_PRI_SERVICE_MESSAGES)
217 /*! \brief Active SRVST_DBKEY out-of-service status value. */
218 unsigned service_status;
219 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
221 struct sig_pri_callback *calls;
222 void *chan_pvt; /*!< Private structure of the user of this module. */
223 #if defined(HAVE_PRI_REVERSE_CHARGE)
225 * \brief Reverse charging indication
227 * -1 - No reverse charging,
228 * 1 - Reverse charging,
229 * 0,2-7 - Reserved for future use
231 int reverse_charging_indication;
236 /* Should be set by user */
237 struct ast_cc_config_params *cc_params; /*!< CC config parameters for each new call. */
238 int pritimers[PRI_MAX_TIMERS];
239 int overlapdial; /*!< In overlap dialing mode */
240 int qsigchannelmapping; /*!< QSIG channel mapping type */
241 int discardremoteholdretrieval; /*!< shall remote hold or remote retrieval notifications be discarded? */
242 int facilityenable; /*!< Enable facility IEs */
243 int dchan_logical_span[NUM_DCHANS]; /*!< Logical offset the DCHAN sits in */
244 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
245 #if defined(HAVE_PRI_SERVICE_MESSAGES)
246 unsigned int enable_service_message_support:1; /*!< enable SERVICE message support */
247 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
248 #ifdef HAVE_PRI_INBANDDISCONNECT
249 unsigned int inbanddisconnect:1; /*!< Should we support inband audio after receiving DISCONNECT? */
251 #if defined(HAVE_PRI_CALL_HOLD)
252 /*! \brief TRUE if held calls are transferred on disconnect. */
253 unsigned int hold_disconnect_transfer:1;
254 #endif /* defined(HAVE_PRI_CALL_HOLD) */
255 int dialplan; /*!< Dialing plan */
256 int localdialplan; /*!< Local dialing plan */
257 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
258 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
259 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
260 char privateprefix[20]; /*!< for private dialplans */
261 char unknownprefix[20]; /*!< for unknown dialplans */
262 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
263 char msn_list[AST_MAX_EXTENSION]; /*!< Comma separated list of MSNs to handle. Empty if disabled. */
264 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
265 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
266 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
267 int minunused; /*!< Min # of channels to keep empty */
268 int minidle; /*!< Min # of "idling" calls to keep active */
269 int nodetype; /*!< Node type */
270 int switchtype; /*!< Type of switch to emulate */
271 int nsf; /*!< Network-Specific Facilities */
272 int trunkgroup; /*!< What our trunkgroup is */
273 #if defined(HAVE_PRI_CCSS)
274 int cc_ptmp_recall_mode; /*!< CC PTMP recall mode. globalRecall(0), specificRecall(1) */
275 int cc_qsig_signaling_link_req; /*!< CC Q.SIG signaling link retention (Party A) release(0), retain(1), do-not-care(2) */
276 int cc_qsig_signaling_link_rsp; /*!< CC Q.SIG signaling link retention (Party B) release(0), retain(1) */
277 #endif /* defined(HAVE_PRI_CCSS) */
279 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
280 int debug; /*!< set to true if to dump PRI event info (tested but never set) */
281 int span; /*!< span number put into user output messages */
282 int resetting; /*!< true if span is being reset/restarted */
283 int resetpos; /*!< current position during a reset (-1 if not started) */
284 int sig; /*!< ISDN signalling type (SIG_PRI, SIG_BRI, SIG_BRI_PTMP, etc...) */
285 int new_chan_seq; /*!< New struct ast_channel sequence number */
287 /* Everything after here is internally set */
288 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
289 struct pri *pri; /*!< Currently active D-channel */
291 * List of private structures of the user of this module for no B channel
292 * interfaces. (hold and call waiting interfaces)
294 void *no_b_chan_iflist;
296 * List of private structures of the user of this module for no B channel
297 * interfaces. (hold and call waiting interfaces)
300 int numchans; /*!< Num of channels we represent */
301 struct sig_pri_chan *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
302 pthread_t master; /*!< Thread of master */
303 ast_mutex_t lock; /*!< libpri access Mutex */
304 time_t lastreset; /*!< time when unused channels were last reset */
305 struct sig_pri_callback *calls;
307 * \brief Congestion device state of the span.
309 * AST_DEVICE_NOT_INUSE - Span does not have all B channels in use.
310 * AST_DEVICE_BUSY - All B channels are in use.
311 * AST_DEVICE_UNAVAILABLE - Span is in alarm.
313 * Device name: DAHDI/I<span>/congestion
315 int congestion_devstate;
316 #if defined(THRESHOLD_DEVSTATE_PLACEHOLDER)
317 /*! \todo An ISDN span threshold device state could be useful in determining how often a span utilization goes over a configurable threshold. */
319 * \brief User threshold device state of the span.
321 * AST_DEVICE_NOT_INUSE - There are no B channels in use.
322 * AST_DEVICE_INUSE - The number of B channels in use is less than
323 * the configured threshold but not zero.
324 * AST_DEVICE_BUSY - The number of B channels in use meets or exceeds
325 * the configured threshold.
326 * AST_DEVICE_UNAVAILABLE - Span is in alarm.
328 * Device name: DAHDI/I<span>/threshold
330 int threshold_devstate;
332 * \brief Number of B channels in use to consider the span in a busy state.
333 * \note Setting the threshold to zero is interpreted as all B channels.
335 int user_busy_threshold;
336 #endif /* defined(THRESHOLD_DEVSTATE_PLACEHOLDER) */
339 void sig_pri_extract_called_num_subaddr(struct sig_pri_chan *p, const char *rdest, char *called, size_t called_buff_size);
340 int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, int timeout, int layer1);
342 int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast);
344 int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen);
346 int sig_pri_answer(struct sig_pri_chan *p, struct ast_channel *ast);
348 int sig_pri_available(struct sig_pri_chan *p);
350 void sig_pri_init_pri(struct sig_pri_pri *pri);
352 /* If return 0, it means this function was able to handle it (pre setup digits). If non zero, the user of this
353 * functions should handle it normally (generate inband DTMF) */
354 int sig_pri_digit_begin(struct sig_pri_chan *pvt, struct ast_channel *ast, char digit);
356 int sig_pri_start_pri(struct sig_pri_pri *pri);
358 void sig_pri_chan_alarm_notify(struct sig_pri_chan *p, int noalarm);
360 void pri_event_alarm(struct sig_pri_pri *pri, int index, int before_start_pri);
362 void pri_event_noalarm(struct sig_pri_pri *pri, int index, int before_start_pri);
364 struct ast_channel *sig_pri_request(struct sig_pri_chan *p, enum sig_pri_law law, const struct ast_channel *requestor, int transfercapability);
366 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);
367 void sig_pri_chan_delete(struct sig_pri_chan *doomed);
369 int pri_is_up(struct sig_pri_pri *pri);
371 void sig_pri_cli_show_spans(int fd, int span, struct sig_pri_pri *pri);
373 void sig_pri_cli_show_span(int fd, int *dchannels, struct sig_pri_pri *pri);
375 int pri_send_keypad_facility_exec(struct sig_pri_chan *p, const char *digits);
376 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);
378 #if defined(HAVE_PRI_SERVICE_MESSAGES)
379 int pri_maintenance_bservice(struct pri *pri, struct sig_pri_chan *p, int changestatus);
380 #endif /* defined(HAVE_PRI_SERVICE_MESSAGES) */
382 void sig_pri_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, struct sig_pri_chan *pchan);
384 int sig_pri_cc_agent_init(struct ast_cc_agent *agent, struct sig_pri_chan *pvt_chan);
385 int sig_pri_cc_agent_start_offer_timer(struct ast_cc_agent *agent);
386 int sig_pri_cc_agent_stop_offer_timer(struct ast_cc_agent *agent);
387 void sig_pri_cc_agent_req_ack(struct ast_cc_agent *agent);
388 int sig_pri_cc_agent_status_req(struct ast_cc_agent *agent);
389 int sig_pri_cc_agent_stop_ringing(struct ast_cc_agent *agent);
390 int sig_pri_cc_agent_party_b_free(struct ast_cc_agent *agent);
391 int sig_pri_cc_agent_start_monitoring(struct ast_cc_agent *agent);
392 int sig_pri_cc_agent_callee_available(struct ast_cc_agent *agent);
393 void sig_pri_cc_agent_destructor(struct ast_cc_agent *agent);
395 int sig_pri_cc_monitor_req_cc(struct ast_cc_monitor *monitor, int *available_timer_id);
396 int sig_pri_cc_monitor_suspend(struct ast_cc_monitor *monitor);
397 int sig_pri_cc_monitor_unsuspend(struct ast_cc_monitor *monitor);
398 int sig_pri_cc_monitor_status_rsp(struct ast_cc_monitor *monitor, enum ast_device_state devstate);
399 int sig_pri_cc_monitor_cancel_available_timer(struct ast_cc_monitor *monitor, int *sched_id);
400 void sig_pri_cc_monitor_destructor(void *monitor_pvt);
402 int sig_pri_load(const char *cc_type_name);
403 void sig_pri_unload(void);
405 #endif /* _SIG_PRI_H */