Trim trailing whitespace.
[asterisk/asterisk.git] / channels / sig_pri.h
1 #ifndef _SIG_PRI_H
2 #define _SIG_PRI_H
3 /*
4  * Asterisk -- An open source telephony toolkit.
5  *
6  * Copyright (C) 1999 - 2009, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
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.
15  *
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.
19  */
20
21 /*! \file
22  *
23  * \brief Interface header for PRI signaling module
24  *
25  * \author Matthew Fredrickson <creslin@digium.com>
26  */
27
28 #include "asterisk/channel.h"
29 #include "asterisk/frame.h"
30 #include <libpri.h>
31 #include <dahdi/user.h>
32
33 enum sig_pri_tone {
34         SIG_PRI_TONE_RINGTONE = 0,
35         SIG_PRI_TONE_STUTTER,
36         SIG_PRI_TONE_CONGESTION,
37         SIG_PRI_TONE_DIALTONE,
38         SIG_PRI_TONE_DIALRECALL,
39         SIG_PRI_TONE_INFO,
40         SIG_PRI_TONE_BUSY,
41 };
42
43 enum sig_pri_law {
44         SIG_PRI_DEFLAW = 0,
45         SIG_PRI_ULAW,
46         SIG_PRI_ALAW
47 };
48
49 struct sig_pri_pri;
50
51 struct sig_pri_callback {
52         /* Unlock the private in the signalling private structure.  This is used for three way calling madness. */
53         void (* const unlock_private)(void *pvt);
54         /* Lock the private in the signalling private structure.  ... */
55         void (* const lock_private)(void *pvt);
56         /* Function which is called back to handle any other DTMF up events that are received.  Called by analog_handle_event.  Why is this
57          * important to use, instead of just directly using events received before they are passed into the library?  Because sometimes,
58          * (CWCID) the library absorbs DTMF events received. */
59         //void (* const handle_dtmfup)(void *pvt, struct ast_channel *ast, enum analog_sub analog_index, struct ast_frame **dest);
60
61         //int (* const dial_digits)(void *pvt, enum analog_sub sub, struct analog_dialoperation *dop);
62         int (* const play_tone)(void *pvt, enum sig_pri_tone tone);
63
64         int (* const set_echocanceller)(void *pvt, int enable);
65         int (* const train_echocanceller)(void *pvt);
66
67         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);
68
69         void (* const fixup_chans)(void *old_chan, void *new_chan);
70
71         /* Note: Called with PRI lock held */
72         void (* const handle_dchan_exception)(struct sig_pri_pri *pri, int index);
73         void (* const set_dialing)(void *pvt, int flag);
74 };
75
76 #define NUM_DCHANS              4       /*!< No more than 4 d-channels */
77 #define MAX_CHANNELS    672             /*!< No more than a DS3 per trunk group */
78
79 #define SIG_PRI         DAHDI_SIG_CLEAR
80 #define SIG_BRI         (0x2000000 | DAHDI_SIG_CLEAR)
81 #define SIG_BRI_PTMP    (0X4000000 | DAHDI_SIG_CLEAR)
82
83 /* Overlap dialing option types */
84 #define DAHDI_OVERLAPDIAL_NONE 0
85 #define DAHDI_OVERLAPDIAL_OUTGOING 1
86 #define DAHDI_OVERLAPDIAL_INCOMING 2
87 #define DAHDI_OVERLAPDIAL_BOTH (DAHDI_OVERLAPDIAL_INCOMING|DAHDI_OVERLAPDIAL_OUTGOING)
88
89 #ifdef HAVE_PRI_SERVICE_MESSAGES
90 /*! \brief Persistent Service State */
91 #define SRVST_DBKEY "service-state"
92 /*! \brief The out-of-service SERVICE state */
93 #define SRVST_TYPE_OOS "O"
94 /*! \brief SRVST_INITIALIZED is used to indicate a channel being out-of-service
95  *  The SRVST_INITIALIZED is mostly used maintain backwards compatibility but also may
96  *  mean that the channel has not yet received a RESTART message.  If a channel is
97  *  out-of-service with this reason a RESTART message will result in the channel
98  *  being put into service. */
99 #define SRVST_INITIALIZED 0
100 /*! \brief SRVST_NEAREND is used to indicate that the near end was put out-of-service */
101 #define SRVST_NEAREND  (1 << 0)
102 /*! \brief SRVST_FAREND is used to indicate that the far end was taken out-of-service */
103 #define SRVST_FAREND   (1 << 1)
104 /*! \brief SRVST_BOTH is used to indicate that both sides of the channel are out-of-service */
105 #define SRVST_BOTH (SRVST_NEAREND | SRVST_FAREND)
106
107 /*! \brief The AstDB family */
108 static const char dahdi_db[] = "dahdi/registry";
109 #endif
110
111 struct sig_pri_chan {
112         /* Options to be set by user */
113         unsigned int hidecallerid:1;
114         unsigned int hidecalleridname:1;      /*!< Hide just the name not the number for legacy PBX use */
115         unsigned int immediate:1;                       /*!< Answer before getting digits? */
116         unsigned int inalarm:1;
117         unsigned int priexclusive:1;                    /*!< Whether or not to override and use exculsive mode for channel selection */
118         unsigned int priindication_oob:1;
119         unsigned int use_callerid:1;                    /*!< Whether or not to use caller id on this channel */
120         unsigned int use_callingpres:1;                 /*!< Whether to use the callingpres the calling switch sends */
121         char context[AST_MAX_CONTEXT];
122         int channel;                                    /*!< Channel Number or CRV */
123         char mohinterpret[MAX_MUSICCLASS];
124         int stripmsd;
125
126         /* Options to be  checked by user */
127         int cid_ani2;                                           /*!< Automatic Number Identification number (Alternate PRI caller ID number) */
128         char cid_num[AST_MAX_EXTENSION];
129         int cid_ton;                                    /*!< Type Of Number (TON) */
130         char cid_name[AST_MAX_EXTENSION];
131         char cid_ani[AST_MAX_EXTENSION];
132         char rdnis[AST_MAX_EXTENSION];
133         char dnid[AST_MAX_EXTENSION];
134         char exten[AST_MAX_EXTENSION];
135         int callingpres;                                /*!< The value of calling presentation that we're going to use when placing a PRI call */
136         char lastcid_num[AST_MAX_EXTENSION];
137         char lastcid_name[AST_MAX_EXTENSION];
138
139         /* Internal variables -- Don't touch */
140         /* Probably will need DS0 number, DS1 number, and a few other things */
141         char dialdest[256];                             /* Queued up digits for overlap dialing.  They will be sent out as information messages when setup ACK is received */
142
143         unsigned int alerting:1;                /*!< TRUE if channel is alerting/ringing */
144         unsigned int alreadyhungup:1;   /*!< TRUE if the call has already gone/hungup */
145         unsigned int isidlecall:1;              /*!< TRUE if this is an idle call */
146         unsigned int proceeding:1;              /*!< TRUE if call is in a proceeding state */
147         unsigned int progress:1;                /*!< TRUE if the call has seen progress through the network */
148         unsigned int resetting:1;               /*!< TRUE if this channel is being reset/restarted */
149         unsigned int setup_ack:1;               /*!< TRUE if this channel has received a SETUP_ACKNOWLEDGE */
150
151         unsigned int outgoing:1;
152         unsigned int digital:1;
153
154         struct ast_channel *owner;
155
156         struct sig_pri_pri *pri;
157         q931_call *call;                                /*!< opaque libpri call control structure */
158
159         int prioffset;                                  /*!< channel number in span */
160         int logicalspan;                                /*!< logical span number within trunk group */
161         int mastertrunkgroup;                   /*!< what trunk group is our master */
162
163         struct sig_pri_callback *calls;
164         void *chan_pvt;
165         ast_mutex_t service_lock;                                               /*!< Mutex for service messages */
166 #if defined(HAVE_PRI_REVERSE_CHARGE)
167         int reverse_charging_indication;
168 #endif
169 };
170
171 struct sig_pri_pri {
172         /* Should be set by user */
173         int     pritimers[PRI_MAX_TIMERS];
174         int overlapdial;                                                                /*!< In overlap dialing mode */
175         int qsigchannelmapping;                         /*!< QSIG channel mapping type */
176     int discardremoteholdretrieval;                 /*!< shall remote hold or remote retrieval notifications be discarded? */
177         int facilityenable;                                                             /*!< Enable facility IEs */
178         int dchan_logical_span[NUM_DCHANS];                             /*!< Logical offset the DCHAN sits in */
179         int fds[NUM_DCHANS];                                                    /*!< FD's for d-channels */
180 #ifdef HAVE_PRI_SERVICE_MESSAGES
181         unsigned int enable_service_message_support:1;  /*!< enable SERVICE message support */
182 #endif
183 #ifdef HAVE_PRI_INBANDDISCONNECT
184         unsigned int inbanddisconnect:1;                                /*!< Should we support inband audio after receiving DISCONNECT? */
185 #endif
186         int dialplan;                                                   /*!< Dialing plan */
187         int localdialplan;                                              /*!< Local dialing plan */
188         char internationalprefix[10];                   /*!< country access code ('00' for european dialplans) */
189         char nationalprefix[10];                                /*!< area access code ('0' for european dialplans) */
190         char localprefix[20];                                   /*!< area access code + area code ('0'+area code for european dialplans) */
191         char privateprefix[20];                                 /*!< for private dialplans */
192         char unknownprefix[20];                                 /*!< for unknown dialplans */
193         long resetinterval;                                             /*!< Interval (in seconds) for resetting unused channels */
194         char idleext[AST_MAX_EXTENSION];                /*!< Where to idle extra calls */
195         char idlecontext[AST_MAX_CONTEXT];              /*!< What context to use for idle */
196         char idledial[AST_MAX_EXTENSION];               /*!< What to dial before dumping */
197         int minunused;                                                  /*!< Min # of channels to keep empty */
198         int minidle;                                                    /*!< Min # of "idling" calls to keep active */
199         int nodetype;                                                   /*!< Node type */
200         int switchtype;                                                 /*!< Type of switch to emulate */
201         int nsf;                                                                /*!< Network-Specific Facilities */
202         int trunkgroup;                                                 /*!< What our trunkgroup is */
203
204         int dchanavail[NUM_DCHANS];                             /*!< Whether each channel is available */
205         int debug;                                                              /*!< set to true if to dump PRI event info (tested but never set) */
206         int span;                               /*!< span number put into user output messages */
207         int resetting;                                                  /*!< true if span is being reset/restarted */
208         int resetpos;                                                   /*!< current position during a reset (-1 if not started) */
209         int sig;                                                                /*!< ISDN signalling type (SIG_PRI, SIG_BRI, SIG_BRI_PTMP, etc...) */
210
211         /* Everything after here is internally set */
212         struct pri *dchans[NUM_DCHANS];                         /*!< Actual d-channels */
213         struct pri *pri;                                                        /*!< Currently active D-channel */
214         int numchans;                                                           /*!< Num of channels we represent */
215         struct sig_pri_chan *pvts[MAX_CHANNELS];        /*!< Member channel pvt structs */
216         pthread_t master;                                                       /*!< Thread of master */
217         ast_mutex_t lock;                                                       /*!< Mutex */
218         time_t lastreset;                                                       /*!< time when unused channels were last reset */
219         struct sig_pri_callback *calls;
220 };
221
222 int sig_pri_call(struct sig_pri_chan *p, struct ast_channel *ast, char *rdest, int timeout, int layer1);
223
224 int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast);
225
226 int sig_pri_indicate(struct sig_pri_chan *p, struct ast_channel *chan, int condition, const void *data, size_t datalen);
227
228 int sig_pri_answer(struct sig_pri_chan *p, struct ast_channel *ast);
229
230 int sig_pri_available(struct sig_pri_chan *p, int channelmatch, ast_group_t groupmatch, int *busy, int *channelmatched, int *groupmatched);
231
232 void sig_pri_init_pri(struct sig_pri_pri *pri);
233
234 /* If return 0, it means this function was able to handle it (pre setup digits).  If non zero, the user of this
235  * functions should handle it normally (generate inband DTMF) */
236 int sig_pri_digit_begin(struct sig_pri_chan *pvt, struct ast_channel *ast, char digit);
237
238 int sig_pri_start_pri(struct sig_pri_pri *pri);
239
240 void sig_pri_chan_alarm_notify(struct sig_pri_chan *p, int noalarm);
241
242 void pri_event_alarm(struct sig_pri_pri *pri, int index, int before_start_pri);
243
244 void pri_event_noalarm(struct sig_pri_pri *pri, int index, int before_start_pri);
245
246 struct ast_channel *sig_pri_request(struct sig_pri_chan *p, enum sig_pri_law law, const struct ast_channel *requestor);
247
248 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);
249
250 int pri_is_up(struct sig_pri_pri *pri);
251
252 void sig_pri_cli_show_spans(int fd, int span, struct sig_pri_pri *pri);
253
254 void sig_pri_cli_show_span(int fd, int *dchannels, struct sig_pri_pri *pri);
255
256 int pri_send_keypad_facility_exec(struct sig_pri_chan *p, const char *digits);
257 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);
258
259 #ifdef HAVE_PRI_SERVICE_MESSAGES
260 int pri_maintenance_bservice(struct pri *pri, struct sig_pri_chan *p, int changestatus);
261 #endif
262
263 #endif /* _SIG_PRI_H */