0f1a6563d5f4772d9263745fa3b1a0c55e52b240
[asterisk/asterisk.git] / channels / sig_analog.h
1 #ifndef _SIG_ANALOG_H
2 #define _SIG_ANALOG_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 analog signaling module
24  *
25  * \author Matthew Fredrickson <creslin@digium.com>
26  */
27
28 #include "asterisk/channel.h"
29 #include "asterisk/frame.h"
30 #include "asterisk/smdi.h"
31
32 #define ANALOG_SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
33 #define ANALOG_MAX_CID 300
34 #define READ_SIZE 160
35 #define RING_PATTERNS 3
36
37 /* Signalling types supported */
38 enum analog_sigtype {
39         ANALOG_SIG_NONE = -1,
40         ANALOG_SIG_FXOLS = 1,
41         ANALOG_SIG_FXOKS,
42         ANALOG_SIG_FXOGS,
43         ANALOG_SIG_FXSLS,
44         ANALOG_SIG_FXSKS,
45         ANALOG_SIG_FXSGS,
46         ANALOG_SIG_EMWINK,
47         ANALOG_SIG_EM,
48         ANALOG_SIG_EM_E1,
49         ANALOG_SIG_FEATD,
50         ANALOG_SIG_FEATDMF,
51         ANALOG_SIG_E911,
52         ANALOG_SIG_FGC_CAMA,
53         ANALOG_SIG_FGC_CAMAMF,
54         ANALOG_SIG_FEATB,
55         ANALOG_SIG_SFWINK,
56         ANALOG_SIG_SF,
57         ANALOG_SIG_SF_FEATD,
58         ANALOG_SIG_SF_FEATDMF,
59         ANALOG_SIG_FEATDMF_TA,
60         ANALOG_SIG_SF_FEATB,
61 };
62
63 enum analog_tone {
64         ANALOG_TONE_RINGTONE = 0,
65         ANALOG_TONE_STUTTER,
66         ANALOG_TONE_CONGESTION,
67         ANALOG_TONE_DIALTONE,
68         ANALOG_TONE_DIALRECALL,
69         ANALOG_TONE_INFO,
70 };
71
72 enum analog_event {
73         ANALOG_EVENT_NONE = 0,
74         ANALOG_EVENT_ONHOOK,
75         ANALOG_EVENT_RINGOFFHOOK,
76         ANALOG_EVENT_WINKFLASH,
77         ANALOG_EVENT_ALARM,
78         ANALOG_EVENT_NOALARM,
79         ANALOG_EVENT_DIALCOMPLETE,
80         ANALOG_EVENT_RINGERON,
81         ANALOG_EVENT_RINGEROFF,
82         ANALOG_EVENT_HOOKCOMPLETE,
83         ANALOG_EVENT_PULSE_START,
84         ANALOG_EVENT_POLARITY,
85         ANALOG_EVENT_RINGBEGIN,
86         ANALOG_EVENT_EC_DISABLED,
87         ANALOG_EVENT_REMOVED,
88         ANALOG_EVENT_NEONMWI_ACTIVE,
89         ANALOG_EVENT_NEONMWI_INACTIVE,
90         ANALOG_EVENT_TX_CED_DETECTED,
91         ANALOG_EVENT_RX_CED_DETECTED,
92         ANALOG_EVENT_EC_NLP_DISABLED,
93         ANALOG_EVENT_EC_NLP_ENABLED,
94         ANALOG_EVENT_ERROR, /* not a DAHDI event */
95         ANALOG_EVENT_DTMFCID, /* not a DAHDI event */
96         ANALOG_EVENT_PULSEDIGIT = (1 << 16), 
97         ANALOG_EVENT_DTMFDOWN = (1 << 17),
98         ANALOG_EVENT_DTMFUP = (1 << 18),
99 };
100
101 enum analog_sub {
102         ANALOG_SUB_REAL = 0,                    /*!< Active call */
103         ANALOG_SUB_CALLWAIT,                    /*!< Call-Waiting call on hold */
104         ANALOG_SUB_THREEWAY,                    /*!< Three-way call */
105 };
106
107 enum analog_dsp_digitmode {
108         ANALOG_DIGITMODE_DTMF = 1,
109         ANALOG_DIGITMODE_MF,
110 };
111
112 enum analog_cid_start {
113         ANALOG_CID_START_POLARITY = 1,
114         ANALOG_CID_START_POLARITY_IN,
115         ANALOG_CID_START_RING,
116         ANALOG_CID_START_DTMF_NOALERT,
117 };
118
119 enum dialop {
120         ANALOG_DIAL_OP_REPLACE = 2,
121 };
122
123
124 struct analog_dialoperation {
125         enum dialop op;
126         char dialstr[256];
127 };
128
129 struct analog_callback {
130         /* Unlock the private in the signalling private structure.  This is used for three way calling madness. */
131         void (* const unlock_private)(void *pvt);
132         /* Lock the private in the signalling private structure.  ... */
133         void (* const lock_private)(void *pvt);
134         /* Do deadlock avoidance for the private signaling structure lock.  */
135         void (* const deadlock_avoidance_private)(void *pvt);
136
137         /* Function which is called back to handle any other DTMF events that are received.  Called by analog_handle_event.  Why is this
138          * important to use, instead of just directly using events received before they are passed into the library?  Because sometimes,
139          * (CWCID) the library absorbs DTMF events received. */
140         void (* const handle_dtmf)(void *pvt, struct ast_channel *ast, enum analog_sub analog_index, struct ast_frame **dest);
141
142         int (* const get_event)(void *pvt);
143         int (* const wait_event)(void *pvt);
144         int (* const is_off_hook)(void *pvt);
145         int (* const is_dialing)(void *pvt, enum analog_sub sub);
146         /* Start a trunk type signalling protocol (everything except phone ports basically */
147         int (* const start)(void *pvt);
148         int (* const ring)(void *pvt);
149         int (* const flash)(void *pvt);
150         /*! \brief Set channel on hook */
151         int (* const on_hook)(void *pvt);
152         /*! \brief Set channel off hook */
153         int (* const off_hook)(void *pvt);
154         void (* const set_needringing)(void *pvt, int value);
155         /*! \brief Set FXS line polarity to 0=IDLE NZ=REVERSED */
156         void (* const set_polarity)(void *pvt, int value);
157         /*! \brief Reset FXS line polarity to IDLE, based on answeronpolarityswitch and hanguponpolarityswitch */
158         void (* const start_polarityswitch)(void *pvt);
159         /*! \brief Switch FXS line polarity, based on answeronpolarityswitch=yes */
160         void (* const answer_polarityswitch)(void *pvt);
161         /*! \brief Switch FXS line polarity, based on answeronpolarityswitch and hanguponpolarityswitch */
162         void (* const hangup_polarityswitch)(void *pvt);
163         /* We're assuming that we're going to only wink on ANALOG_SUB_REAL - even though in the code there's an argument to the index
164          * function */
165         int (* const wink)(void *pvt, enum analog_sub sub);
166         int (* const dial_digits)(void *pvt, enum analog_sub sub, struct analog_dialoperation *dop);
167         int (* const send_fsk)(void *pvt, struct ast_channel *ast, char *fsk);
168         int (* const play_tone)(void *pvt, enum analog_sub sub, enum analog_tone tone);
169
170         int (* const set_echocanceller)(void *pvt, int enable);
171         int (* const train_echocanceller)(void *pvt);
172         int (* const dsp_set_digitmode)(void *pvt, enum analog_dsp_digitmode mode);
173         int (* const dsp_reset_and_flush_digits)(void *pvt);
174         int (* const send_callerid)(void *pvt, int cwcid, struct ast_party_caller *caller);
175         /* Returns 0 if CID received.  Returns 1 if event received, and -1 if error.  name and num are size ANALOG_MAX_CID */
176         int (* const get_callerid)(void *pvt, char *name, char *num, enum analog_event *ev, size_t timeout);
177         /* Start CID detection */
178         int (* const start_cid_detect)(void *pvt, int cid_signalling);
179         /* Stop CID detection */
180         int (* const stop_cid_detect)(void *pvt);
181
182         /* Play the CAS callwait tone on the REAL sub, then repeat after 10 seconds, and then stop */
183         int (* const callwait)(void *pvt);
184         /* Stop playing any CAS call waiting announcement tones that might be running on the REAL sub */
185         int (* const stop_callwait)(void *pvt);
186
187         /* Bearer control related (non signalling) callbacks */
188         int (* const allocate_sub)(void *pvt, enum analog_sub sub);
189         int (* const unallocate_sub)(void *pvt, enum analog_sub sub);
190         /*! This function is for swapping of the owners with the underlying subs.  Typically it means you need to change the fds
191          * of the new owner to be the fds of the sub specified, for each of the two subs given */
192         void (* const swap_subs)(void *pvt, enum analog_sub a, struct ast_channel *new_a_owner, enum analog_sub b, struct ast_channel *new_b_owner);
193         struct ast_channel * (* const new_ast_channel)(void *pvt, int state, int startpbx, enum analog_sub sub, const struct ast_channel *requestor);
194
195         /* Add the given sub to a conference */
196         int (* const conf_add)(void *pvt, enum analog_sub sub);
197         /* Delete the given sub from any conference that might be running on the channels */
198         int (* const conf_del)(void *pvt, enum analog_sub sub);
199
200         /* If you would like to do any optimizations after the conference members have been added and removed,
201          * you can do so here */
202         int (* const complete_conference_update)(void *pvt, int needconf);
203
204         /* This is called when there are no more subchannels on the given private that are left up,
205          * for any cleanup or whatever else you would like to do.  Called from analog_hangup() */
206         void (* const all_subchannels_hungup)(void *pvt);
207
208         int (* const has_voicemail)(void *pvt);
209         int (* const check_for_conference)(void *pvt);
210         void (* const handle_notify_message)(struct ast_channel *chan, void *pvt, int cid_flags, int neon_mwievent);
211
212         /* callbacks for increasing and decreasing ss_thread_count, will handle locking and condition signal */
213         void (* const increase_ss_count)(void);
214         void (* const decrease_ss_count)(void);
215
216         int (* const distinctive_ring)(struct ast_channel *chan, void *pvt, int idx, int *ringdata);
217         /* Sets the specified sub-channel in and out of signed linear mode, returns the value that was overwritten */
218         int (* const set_linear_mode)(void *pvt, enum analog_sub sub, int linear_mode);
219         void (* const set_inthreeway)(void *pvt, enum analog_sub sub, int inthreeway);
220         void (* const get_and_handle_alarms)(void *pvt);
221         void * (* const get_sigpvt_bridged_channel)(struct ast_channel *chan);
222         int (* const get_sub_fd)(void *pvt, enum analog_sub sub);
223         void (* const set_cadence)(void *pvt, int *cidrings, struct ast_channel *chan);
224         void (* const set_alarm)(void *pvt, int in_alarm);
225         void (* const set_dialing)(void *pvt, int is_dialing);
226         void (* const set_ringtimeout)(void *pvt, int ringt);
227         void (* const set_waitingfordt)(void *pvt, struct ast_channel *ast);
228         int (* const check_waitingfordt)(void *pvt);
229         void (* const set_confirmanswer)(void *pvt, int flag);
230         int (* const check_confirmanswer)(void *pvt);
231         void (* const set_callwaiting)(void *pvt, int callwaiting_enable);
232         void (* const cancel_cidspill)(void *pvt);
233         int (* const confmute)(void *pvt, int mute);    
234         void (* const set_pulsedial)(void *pvt, int flag);
235         void (* const set_new_owner)(void *pvt, struct ast_channel *new_owner);
236
237         const char *(* const get_orig_dialstring)(void *pvt);
238 };
239
240
241
242 struct analog_subchannel {
243         struct ast_channel *owner;
244         struct ast_frame f;             /*!< One frame for each channel.  How did this ever work before? */
245         unsigned int inthreeway:1;
246         /* Have we allocated a subchannel yet or not */
247         unsigned int allocd:1;
248 };
249
250 struct analog_pvt {
251         /* Analog signalling type used in this private */
252         enum analog_sigtype sig;
253         /* To contain the private structure passed into the channel callbacks */
254         void *chan_pvt;
255         /* Callbacks for various functions needed by the analog API */
256         struct analog_callback *calls;
257         /* All members after this are giong to be transient, and most will probably change */
258         struct ast_channel *owner;                      /*!< Our current active owner (if applicable) */
259
260         struct analog_subchannel subs[3];               /*!< Sub-channels */
261         struct analog_dialoperation dop;
262         int onhooktime;                                                 /*< Time the interface went on-hook. */
263         int fxsoffhookstate;                                    /*< TRUE if the FXS port is off-hook */
264         /*! \brief -1 = unknown, 0 = no messages, 1 = new messages available */
265         int msgstate;
266
267         /* XXX: Option Variables - Set by allocator of private structure */
268         unsigned int answeronpolarityswitch:1;
269         unsigned int callreturn:1;
270         unsigned int cancallforward:1;
271         unsigned int canpark:1;
272         unsigned int dahditrcallerid:1;                 /*!< should we use the callerid from incoming call on dahdi transfer or not */
273         unsigned int hanguponpolarityswitch:1;
274         unsigned int immediate:1;
275         unsigned int permcallwaiting:1;                 /*!< TRUE if call waiting is enabled. (Configured option) */
276         unsigned int permhidecallerid:1;                /*!< Whether to hide our outgoing caller ID or not */
277         unsigned int pulse:1;
278         unsigned int threewaycalling:1;
279         unsigned int transfer:1;
280         unsigned int transfertobusy:1;                  /*!< allow flash-transfers to busy channels */
281         unsigned int use_callerid:1;                    /*!< Whether or not to use caller id on this channel */
282         unsigned int callwaitingcallerid:1;             /*!< TRUE if send caller ID for Call Waiting */
283         /*!
284          * \brief TRUE if SMDI (Simplified Message Desk Interface) is enabled
285          */
286         unsigned int use_smdi:1;
287         /*! \brief The SMDI interface to get SMDI messages from. */
288         struct ast_smdi_interface *smdi_iface;
289         const struct ast_channel_tech *chan_tech;
290
291         /* Not used for anything but log messages.  Could be just the TCID */
292         int channel;                                    /*!< Channel Number */
293
294         enum analog_sigtype outsigmod;
295         int echotraining;
296         int cid_signalling;                             /*!< Asterisk callerid type we're using */
297         int polarityonanswerdelay;
298         int stripmsd;
299         enum analog_cid_start cid_start;
300         char mohsuggest[MAX_MUSICCLASS];
301         char cid_num[AST_MAX_EXTENSION];
302         char cid_name[AST_MAX_EXTENSION];
303
304
305         /* XXX: All variables after this are internal */
306         unsigned int callwaiting:1;             /*!< TRUE if call waiting is enabled. (Active option) */
307         unsigned int dialednone:1;
308         unsigned int dialing:1;                 /*!< TRUE if in the process of dialing digits or sending something */
309         unsigned int dnd:1;                             /*!< TRUE if Do-Not-Disturb is enabled. */
310         unsigned int echobreak:1;
311         unsigned int hidecallerid:1;
312         unsigned int outgoing:1;
313         unsigned int inalarm:1;
314         /*!
315          * \brief TRUE if Call Waiting (CW) CPE Alert Signal (CAS) is being sent.
316          * \note
317          * After CAS is sent, the call waiting caller id will be sent if the phone
318          * gives a positive reply.
319          */
320         unsigned int callwaitcas:1;
321
322         char callwait_num[AST_MAX_EXTENSION];
323         char callwait_name[AST_MAX_EXTENSION];
324         char lastcid_num[AST_MAX_EXTENSION];
325         char lastcid_name[AST_MAX_EXTENSION];
326         struct ast_party_caller caller;
327         int cidrings;                                   /*!< Which ring to deliver CID on */
328         char echorest[20];
329         int polarity;
330         struct timeval polaritydelaytv;
331         char dialdest[256];
332         time_t guardtime;                               /*!< Must wait this much time before using for new call */
333         struct timeval flashtime;                       /*!< Last flash-hook time */
334         int whichwink;                                  /*!< SIG_FEATDMF_TA Which wink are we on? */
335         char finaldial[64];
336         char *origcid_num;                              /*!< malloced original callerid */
337         char *origcid_name;                             /*!< malloced original callerid */
338         char call_forward[AST_MAX_EXTENSION];
339
340         /* Ast channel to pass to __ss_analog_thread */
341         struct ast_channel *ss_astchan;
342
343         /* All variables after this are definitely going to be audited */
344         int ringt;
345         int ringt_base;
346 };
347
348 struct analog_pvt *analog_new(enum analog_sigtype signallingtype, struct analog_callback *c, void *private_data);
349 void analog_delete(struct analog_pvt *doomed);
350
351 void analog_free(struct analog_pvt *p);
352
353 int analog_call(struct analog_pvt *p, struct ast_channel *ast, char *rdest, int timeout);
354
355 int analog_hangup(struct analog_pvt *p, struct ast_channel *ast);
356
357 int analog_answer(struct analog_pvt *p, struct ast_channel *ast);
358
359 struct ast_frame *analog_exception(struct analog_pvt *p, struct ast_channel *ast);
360
361 struct ast_channel * analog_request(struct analog_pvt *p, int *callwait, const struct ast_channel *requestor);
362
363 int analog_available(struct analog_pvt *p);
364
365 void *analog_handle_init_event(struct analog_pvt *i, int event);
366
367 int analog_config_complete(struct analog_pvt *p);
368
369 void analog_handle_dtmf(struct analog_pvt *p, struct ast_channel *ast, enum analog_sub index, struct ast_frame **dest);
370
371 enum analog_cid_start analog_str_to_cidstart(const char *value);
372
373 const char *analog_cidstart_to_str(enum analog_cid_start cid_start);
374
375 enum analog_sigtype analog_str_to_sigtype(const char *name);
376
377 const char *analog_sigtype_to_str(enum analog_sigtype sigtype);
378
379 unsigned int analog_str_to_cidtype(const char *name);
380
381 const char *analog_cidtype_to_str(unsigned int cid_type);
382
383 int analog_ss_thread_start(struct analog_pvt *p, struct ast_channel *ast);
384
385 int analog_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, void *newp);
386
387 int analog_dnd(struct analog_pvt *p, int flag);
388
389 #endif /* _SIG_ANSLOG_H */