Merged revisions 90548 via svnmerge from
[asterisk/asterisk.git] / include / asterisk / channel.h
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
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  * \brief General Asterisk PBX channel definitions.
21  * \par See also:
22  *  \arg \ref Def_Channel
23  *  \arg \ref channel_drivers
24  */
25
26 /*! \page Def_Channel Asterisk Channels
27         \par What is a Channel?
28         A phone call through Asterisk consists of an incoming
29         connection and an outbound connection. Each call comes
30         in through a channel driver that supports one technology,
31         like SIP, ZAP, IAX2 etc. 
32         \par
33         Each channel driver, technology, has it's own private
34         channel or dialog structure, that is technology-dependent.
35         Each private structure is "owned" by a generic Asterisk
36         channel structure, defined in channel.h and handled by
37         channel.c .
38         \par Call scenario
39         This happens when an incoming call arrives to Asterisk
40         -# Call arrives on a channel driver interface
41         -# Channel driver creates a PBX channel and starts a 
42            pbx thread on the channel
43         -# The dial plan is executed
44         -# At this point at least two things can happen:
45                 -# The call is answered by Asterisk and 
46                    Asterisk plays a media stream or reads media
47                 -# The dial plan forces Asterisk to create an outbound 
48                    call somewhere with the dial (see \ref app_dial.c)
49                    application
50         .
51
52         \par Bridging channels
53         If Asterisk dials out this happens:
54         -# Dial creates an outbound PBX channel and asks one of the
55            channel drivers to create a call
56         -# When the call is answered, Asterisk bridges the media streams
57            so the caller on the first channel can speak with the callee
58            on the second, outbound channel
59         -# In some cases where we have the same technology on both
60            channels and compatible codecs, a native bridge is used.
61            In a native bridge, the channel driver handles forwarding
62            of incoming audio to the outbound stream internally, without
63            sending audio frames through the PBX.
64         -# In SIP, theres an "external native bridge" where Asterisk
65            redirects the endpoint, so audio flows directly between the
66            caller's phone and the callee's phone. Signalling stays in
67            Asterisk in order to be able to provide a proper CDR record
68            for the call.
69
70         
71         \par Masquerading channels
72         In some cases, a channel can masquerade itself into another
73         channel. This happens frequently in call transfers, where 
74         a new channel takes over a channel that is already involved
75         in a call. The new channel sneaks in and takes over the bridge
76         and the old channel, now a zombie, is hung up.
77         
78         \par Reference
79         \arg channel.c - generic functions
80         \arg channel.h - declarations of functions, flags and structures
81         \arg translate.h - Transcoding support functions
82         \arg \ref channel_drivers - Implemented channel drivers
83         \arg \ref Def_Frame Asterisk Multimedia Frames
84         \arg \ref Def_Bridge
85
86 */
87 /*! \page Def_Bridge Asterisk Channel Bridges
88
89         In Asterisk, there's several media bridges. 
90
91         The Core bridge handles two channels (a "phone call") and bridge
92         them together.
93         
94         The conference bridge (meetme) handles several channels simultaneously
95         with the support of an external timer (zaptel timer). This is used
96         not only by the Conference application (meetme) but also by the
97         page application and the SLA system introduced in 1.4.
98         The conference bridge does not handle video.
99
100         When two channels of the same type connect, the channel driver
101         or the media subsystem used by the channel driver (i.e. RTP)
102         can create a native bridge without sending media through the
103         core.
104
105         Native briding can be disabled by a number of reasons,
106         like DTMF being needed by the core or codecs being incompatible
107         so a transcoding module is needed.
108
109 References:
110         \li \see ast_channel_early_bridge()
111         \li \see ast_channel_bridge()
112         \li \see app_meetme.c
113         \li \ref AstRTPbridge
114         \li \see ast_rtp_bridge() 
115         \li \ref Def_Channel
116 */
117
118 /*! \page AstFileDesc File descriptors 
119         Asterisk File descriptors are connected to each channel (see \ref Def_Channel)
120         in the \ref ast_channel structure.
121 */
122
123 #ifndef _ASTERISK_CHANNEL_H
124 #define _ASTERISK_CHANNEL_H
125
126 #include "asterisk/abstract_jb.h"
127
128 #ifdef HAVE_SYS_POLL_H
129 #include <sys/poll.h>
130 #else
131 #include "asterisk/poll-compat.h"
132 #endif
133
134 #if defined(__cplusplus) || defined(c_plusplus)
135 extern "C" {
136 #endif
137
138 #define AST_MAX_EXTENSION       80      /*!< Max length of an extension */
139 #define AST_MAX_CONTEXT         80      /*!< Max length of a context */
140 #define AST_CHANNEL_NAME        80      /*!< Max length of an ast_channel name */
141 #define MAX_LANGUAGE            20      /*!< Max length of the language setting */
142 #define MAX_MUSICCLASS          80      /*!< Max length of the music class setting */
143
144 #include "asterisk/frame.h"
145 #include "asterisk/sched.h"
146 #include "asterisk/chanvars.h"
147 #include "asterisk/config.h"
148 #include "asterisk/lock.h"
149 #include "asterisk/cdr.h"
150 #include "asterisk/utils.h"
151 #include "asterisk/linkedlists.h"
152 #include "asterisk/stringfields.h"
153
154 #define DATASTORE_INHERIT_FOREVER       INT_MAX
155
156 #define AST_MAX_FDS             10
157 /*
158  * We have AST_MAX_FDS file descriptors in a channel.
159  * Some of them have a fixed use:
160  */
161 #define AST_ALERT_FD    (AST_MAX_FDS-1)         /*!< used for alertpipe */
162 #define AST_TIMING_FD   (AST_MAX_FDS-2)         /*!< used for timingfd */
163 #define AST_AGENT_FD    (AST_MAX_FDS-3)         /*!< used by agents for pass through */
164 #define AST_GENERATOR_FD        (AST_MAX_FDS-4) /*!< used by generator */
165
166 enum ast_bridge_result {
167         AST_BRIDGE_COMPLETE = 0,
168         AST_BRIDGE_FAILED = -1,
169         AST_BRIDGE_FAILED_NOWARN = -2,
170         AST_BRIDGE_RETRY = -3,
171 };
172
173 typedef unsigned long long ast_group_t;
174
175 /*! \todo Add an explanation of an Asterisk generator 
176 */
177 struct ast_generator {
178         void *(*alloc)(struct ast_channel *chan, void *params);
179         void (*release)(struct ast_channel *chan, void *data);
180         /*! This function gets called with the channel locked */
181         int (*generate)(struct ast_channel *chan, void *data, int len, int samples);
182         /*! This gets called when DTMF_END frames are read from the channel */
183         void (*digit)(struct ast_channel *chan, char digit);
184 };
185
186 /*! \brief Structure for a data store type */
187 struct ast_datastore_info {
188         const char *type;               /*!< Type of data store */
189         void *(*duplicate)(void *data); /*!< Duplicate item data (used for inheritance) */
190         void (*destroy)(void *data);    /*!< Destroy function */
191 };
192
193 /*! \brief Structure for a channel data store */
194 struct ast_datastore {
195         const char *uid;                /*!< Unique data store identifier */
196         void *data;             /*!< Contained data */
197         const struct ast_datastore_info *info;  /*!< Data store type information */
198         unsigned int inheritance;       /*!< Number of levels this item will continue to be inherited */
199         AST_LIST_ENTRY(ast_datastore) entry; /*!< Used for easy linking */
200 };
201
202 /*! \brief Structure for all kinds of caller ID identifications.
203  * \note All string fields here are malloc'ed, so they need to be
204  * freed when the structure is deleted.
205  * Also, NULL and "" must be considered equivalent.
206  * 
207  * SIP and IAX2 has utf8 encoded Unicode caller ID names.
208  * In some cases, we also have an alternative (RPID) E.164 number that can be used
209  * as caller ID on numeric E.164 phone networks (zaptel or SIP/IAX2 to pstn gateway).
210  *
211  * \todo Implement settings for transliteration between UTF8 caller ID names in
212  *       to Ascii Caller ID's (Zaptel). Ã–sten Ã…sklund might be transliterated into
213  *       Osten Asklund or Oesten Aasklund depending upon language and person...
214  *       We need automatic routines for incoming calls and static settings for
215  *       our own accounts.
216  */
217 struct ast_callerid {
218         char *cid_dnid;         /*!< Malloc'd Dialed Number Identifier */
219         char *cid_num;          /*!< Malloc'd Caller Number */
220         char *cid_name;         /*!< Malloc'd Caller Name (ASCII) */
221         char *cid_ani;          /*!< Malloc'd ANI */
222         char *cid_rdnis;        /*!< Malloc'd RDNIS */
223         int cid_pres;           /*!< Callerid presentation/screening */
224         int cid_ani2;           /*!< Callerid ANI 2 (Info digits) */
225         int cid_ton;            /*!< Callerid Type of Number */
226         int cid_tns;            /*!< Callerid Transit Network Select */
227 };
228
229 /*! \brief 
230         Structure to describe a channel "technology", ie a channel driver 
231         See for examples:
232         \arg chan_iax2.c - The Inter-Asterisk exchange protocol
233         \arg chan_sip.c - The SIP channel driver
234         \arg chan_zap.c - PSTN connectivity (TDM, PRI, T1/E1, FXO, FXS)
235
236         If you develop your own channel driver, this is where you
237         tell the PBX at registration of your driver what properties
238         this driver supports and where different callbacks are 
239         implemented.
240 */
241 struct ast_channel_tech {
242         const char * const type;
243         const char * const description;
244
245         int capabilities;               /*!< Bitmap of formats this channel can handle */
246
247         int properties;                 /*!< Technology Properties */
248
249         /*! \brief Requester - to set up call data structures (pvt's) */
250         struct ast_channel *(* const requester)(const char *type, int format, void *data, int *cause);
251
252         int (* const devicestate)(void *data);  /*!< Devicestate call back */
253
254         /*! 
255          * \brief Start sending a literal DTMF digit 
256          *
257          * \note The channel is not locked when this function gets called. 
258          */
259         int (* const send_digit_begin)(struct ast_channel *chan, char digit);
260
261         /*! 
262          * \brief Stop sending a literal DTMF digit 
263          *
264          * \note The channel is not locked when this function gets called. 
265          */
266         int (* const send_digit_end)(struct ast_channel *chan, char digit, unsigned int duration);
267
268         /*! \brief Call a given phone number (address, etc), but don't
269            take longer than timeout seconds to do so.  */
270         int (* const call)(struct ast_channel *chan, char *addr, int timeout);
271
272         /*! \brief Hangup (and possibly destroy) the channel */
273         int (* const hangup)(struct ast_channel *chan);
274
275         /*! \brief Answer the channel */
276         int (* const answer)(struct ast_channel *chan);
277
278         /*! \brief Read a frame, in standard format (see frame.h) */
279         struct ast_frame * (* const read)(struct ast_channel *chan);
280
281         /*! \brief Write a frame, in standard format (see frame.h) */
282         int (* const write)(struct ast_channel *chan, struct ast_frame *frame);
283
284         /*! \brief Display or transmit text */
285         int (* const send_text)(struct ast_channel *chan, const char *text);
286
287         /*! \brief Display or send an image */
288         int (* const send_image)(struct ast_channel *chan, struct ast_frame *frame);
289
290         /*! \brief Send HTML data */
291         int (* const send_html)(struct ast_channel *chan, int subclass, const char *data, int len);
292
293         /*! \brief Handle an exception, reading a frame */
294         struct ast_frame * (* const exception)(struct ast_channel *chan);
295
296         /*! \brief Bridge two channels of the same type together */
297         enum ast_bridge_result (* const bridge)(struct ast_channel *c0, struct ast_channel *c1, int flags,
298                                                 struct ast_frame **fo, struct ast_channel **rc, int timeoutms);
299
300         /*! \brief Bridge two channels of the same type together (early) */
301         enum ast_bridge_result (* const early_bridge)(struct ast_channel *c0, struct ast_channel *c1);
302
303         /*! \brief Indicate a particular condition (e.g. AST_CONTROL_BUSY or AST_CONTROL_RINGING or AST_CONTROL_CONGESTION */
304         int (* const indicate)(struct ast_channel *c, int condition, const void *data, size_t datalen);
305
306         /*! \brief Fix up a channel:  If a channel is consumed, this is called.  Basically update any ->owner links */
307         int (* const fixup)(struct ast_channel *oldchan, struct ast_channel *newchan);
308
309         /*! \brief Set a given option */
310         int (* const setoption)(struct ast_channel *chan, int option, void *data, int datalen);
311
312         /*! \brief Query a given option */
313         int (* const queryoption)(struct ast_channel *chan, int option, void *data, int *datalen);
314
315         /*! \brief Blind transfer other side (see app_transfer.c and ast_transfer() */
316         int (* const transfer)(struct ast_channel *chan, const char *newdest);
317
318         /*! \brief Write a frame, in standard format */
319         int (* const write_video)(struct ast_channel *chan, struct ast_frame *frame);
320
321         /*! \brief Write a text frame, in standard format */
322         int (* const write_text)(struct ast_channel *chan, struct ast_frame *frame);
323
324         /*! \brief Find bridged channel */
325         struct ast_channel *(* const bridged_channel)(struct ast_channel *chan, struct ast_channel *bridge);
326
327         /*! \brief Provide additional read items for CHANNEL() dialplan function */
328         int (* func_channel_read)(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
329
330         /*! \brief Provide additional write items for CHANNEL() dialplan function */
331         int (* func_channel_write)(struct ast_channel *chan, const char *function, char *data, const char *value);
332
333         /*! \brief Retrieve base channel (agent and local) */
334         struct ast_channel* (* get_base_channel)(struct ast_channel *chan);
335         
336         /*! \brief Set base channel (agent and local) */
337         int (* set_base_channel)(struct ast_channel *chan, struct ast_channel *base);
338 };
339
340 struct ast_epoll_data;
341
342 /*!
343  * The high bit of the frame count is used as a debug marker, so
344  * increments of the counters must be done with care.
345  * Please use c->fin = FRAMECOUNT_INC(c->fin) and the same for c->fout.
346  */
347 #define DEBUGCHAN_FLAG  0x80000000
348
349 /* XXX not ideal to evaluate x twice... */
350 #define FRAMECOUNT_INC(x)       ( ((x) & DEBUGCHAN_FLAG) | (((x)+1) & ~DEBUGCHAN_FLAG) )
351
352 /*!
353  * The current value of the debug flags is stored in the two
354  * variables global_fin and global_fout (declared in main/channel.c)
355  */
356 extern unsigned long global_fin, global_fout;
357
358 enum ast_channel_adsicpe {
359         AST_ADSI_UNKNOWN,
360         AST_ADSI_AVAILABLE,
361         AST_ADSI_UNAVAILABLE,
362         AST_ADSI_OFFHOOKONLY,
363 };
364
365 /*! 
366  * \brief ast_channel states
367  *
368  * \note Bits 0-15 of state are reserved for the state (up/down) of the line
369  *       Bits 16-32 of state are reserved for flags
370  */
371 enum ast_channel_state {
372         AST_STATE_DOWN,                 /*!< Channel is down and available */
373         AST_STATE_RESERVED,             /*!< Channel is down, but reserved */
374         AST_STATE_OFFHOOK,              /*!< Channel is off hook */
375         AST_STATE_DIALING,              /*!< Digits (or equivalent) have been dialed */
376         AST_STATE_RING,                 /*!< Line is ringing */
377         AST_STATE_RINGING,              /*!< Remote end is ringing */
378         AST_STATE_UP,                   /*!< Line is up */
379         AST_STATE_BUSY,                 /*!< Line is busy */
380         AST_STATE_DIALING_OFFHOOK,      /*!< Digits (or equivalent) have been dialed while offhook */
381         AST_STATE_PRERING,              /*!< Channel has detected an incoming call and is waiting for ring */
382
383         AST_STATE_MUTE = (1 << 16),     /*!< Do not transmit voice data */
384 };
385
386 /*! \brief Main Channel structure associated with a channel. 
387  * This is the side of it mostly used by the pbx and call management.
388  *
389  * \note XXX It is important to remember to increment .cleancount each time
390  *       this structure is changed. XXX
391  */
392
393 struct ast_channel {
394         const struct ast_channel_tech *tech;            /*!< Technology (point to channel driver) */
395
396         void *tech_pvt;                                 /*!< Private data used by the technology driver */
397
398         AST_DECLARE_STRING_FIELDS(
399                 AST_STRING_FIELD(name);                 /*!< ASCII unique channel name */
400                 AST_STRING_FIELD(language);             /*!< Language requested for voice prompts */
401                 AST_STRING_FIELD(musicclass);           /*!< Default music class */
402                 AST_STRING_FIELD(accountcode);          /*!< Account code for billing */
403                 AST_STRING_FIELD(call_forward);         /*!< Where to forward to if asked to dial on this interface */
404                 AST_STRING_FIELD(uniqueid);             /*!< Unique Channel Identifier */
405         );
406         
407         int fds[AST_MAX_FDS];                           /*!< File descriptors for channel -- Drivers will poll on
408                                                                 these file descriptors, so at least one must be non -1.
409                                                                  See \arg \ref AstFileDesc */
410
411         void *music_state;                              /*!< Music State*/
412         void *generatordata;                            /*!< Current generator data if there is any */
413         struct ast_generator *generator;                /*!< Current active data generator */
414
415         struct ast_channel *_bridge;                    /*!< Who are we bridged to, if we're bridged.
416                                                                 Who is proxying for us, if we are proxied (i.e. chan_agent).
417                                                                 Do not access directly, use ast_bridged_channel(chan) */
418
419         struct ast_channel *masq;                       /*!< Channel that will masquerade as us */
420         struct ast_channel *masqr;                      /*!< Who we are masquerading as */
421         int cdrflags;                                   /*!< Call Detail Record Flags */
422
423         int _softhangup;                                /*!< Whether or not we have been hung up...  Do not set this value
424                                                                 directly, use ast_softhangup() */
425         time_t  whentohangup;                           /*!< Non-zero, set to actual time when channel is to be hung up */
426         pthread_t blocker;                              /*!< If anyone is blocking, this is them */
427         ast_mutex_t lock_dont_use;                      /*!< Lock a channel for some operations. See ast_channel_lock() */
428         const char *blockproc;                          /*!< Procedure causing blocking */
429
430         const char *appl;                               /*!< Current application */
431         const char *data;                               /*!< Data passed to current application */
432         int fdno;                                       /*!< Which fd had an event detected on */
433         struct sched_context *sched;                    /*!< Schedule context */
434         int streamid;                                   /*!< For streaming playback, the schedule ID */
435         struct ast_filestream *stream;                  /*!< Stream itself. */
436         int vstreamid;                                  /*!< For streaming video playback, the schedule ID */
437         struct ast_filestream *vstream;                 /*!< Video Stream itself. */
438         int oldwriteformat;                             /*!< Original writer format */
439         
440         int timingfd;                                   /*!< Timing fd */
441         int (*timingfunc)(const void *data);
442         void *timingdata;
443
444         enum ast_channel_state _state;                  /*!< State of line -- Don't write directly, use ast_setstate() */
445         int rings;                                      /*!< Number of rings so far */
446         struct ast_callerid cid;                        /*!< Caller ID, name, presentation etc */
447         char dtmfq[AST_MAX_EXTENSION];                  /*!< Any/all queued DTMF characters */
448         struct ast_frame dtmff;                         /*!< DTMF frame */
449
450         char context[AST_MAX_CONTEXT];                  /*!< Dialplan: Current extension context */
451         char exten[AST_MAX_EXTENSION];                  /*!< Dialplan: Current extension number */
452         int priority;                                   /*!< Dialplan: Current extension priority */
453         char macrocontext[AST_MAX_CONTEXT];             /*!< Macro: Current non-macro context. See app_macro.c */
454         char macroexten[AST_MAX_EXTENSION];             /*!< Macro: Current non-macro extension. See app_macro.c */
455         int macropriority;                              /*!< Macro: Current non-macro priority. See app_macro.c */
456         char dialcontext[AST_MAX_CONTEXT];              /*!< Dial: Extension context that we were called from */
457
458         struct ast_pbx *pbx;                            /*!< PBX private structure for this channel */
459         int amaflags;                                   /*!< Set BEFORE PBX is started to determine AMA flags */
460         struct ast_cdr *cdr;                            /*!< Call Detail Record */
461         enum ast_channel_adsicpe adsicpe;               /*!< Whether or not ADSI is detected on CPE */
462
463         struct ind_tone_zone *zone;                     /*!< Tone zone as set in indications.conf or
464                                                                 in the CHANNEL dialplan function */
465
466         struct ast_channel_monitor *monitor;            /*!< Channel monitoring */
467
468         unsigned long insmpl;                           /*!< Track the read/written samples for monitor use */
469         unsigned long outsmpl;                          /*!< Track the read/written samples for monitor use */
470
471         unsigned int fin;                               /*!< Frames in counters. The high bit is a debug mask, so
472                                                                 the counter is only in the remaining bits */
473         unsigned int fout;                              /*!< Frames out counters. The high bit is a debug mask, so
474                                                                 the counter is only in the remaining bits */
475         int hangupcause;                                /*!< Why is the channel hanged up. See causes.h */
476         struct varshead varshead;                       /*!< A linked list for channel variables. See \ref AstChanVar */
477         ast_group_t callgroup;                          /*!< Call group for call pickups */
478         ast_group_t pickupgroup;                        /*!< Pickup group - which calls groups can be picked up? */
479         unsigned int flags;                             /*!< channel flags of AST_FLAG_ type */
480         unsigned short transfercapability;              /*!< ISDN Transfer Capbility - AST_FLAG_DIGITAL is not enough */
481         AST_LIST_HEAD_NOLOCK(, ast_frame) readq;
482         int alertpipe[2];
483
484         int nativeformats;                              /*!< Kinds of data this channel can natively handle */
485         int readformat;                                 /*!< Requested read format */
486         int writeformat;                                /*!< Requested write format */
487         struct ast_trans_pvt *writetrans;               /*!< Write translation path */
488         struct ast_trans_pvt *readtrans;                /*!< Read translation path */
489         int rawreadformat;                              /*!< Raw read format */
490         int rawwriteformat;                             /*!< Raw write format */
491
492         struct ast_audiohook_list *audiohooks;
493
494         AST_LIST_ENTRY(ast_channel) chan_list;          /*!< For easy linking */
495         
496         struct ast_jb jb;                               /*!< The jitterbuffer state  */
497
498         char emulate_dtmf_digit;                        /*!< Digit being emulated */
499         unsigned int emulate_dtmf_duration;             /*!< Number of ms left to emulate DTMF for */
500         struct timeval dtmf_tv;                         /*!< The time that an in process digit began, or the last digit ended */
501
502         AST_LIST_HEAD_NOLOCK(datastores, ast_datastore) datastores; /*!< Data stores on the channel */
503
504 #ifdef HAVE_EPOLL
505         int epfd;
506         struct ast_epoll_data *epfd_data[AST_MAX_FDS];
507 #endif
508         int visible_indication;                         /*!< Indication currently playing on the channel */
509 };
510
511 /*! \brief ast_channel_tech Properties */
512 enum {
513         /*! \brief Channels have this property if they can accept input with jitter; 
514          *         i.e. most VoIP channels */
515         AST_CHAN_TP_WANTSJITTER = (1 << 0),
516         /*! \brief Channels have this property if they can create jitter; 
517          *         i.e. most VoIP channels */
518         AST_CHAN_TP_CREATESJITTER = (1 << 1),
519 };
520
521 /*! \brief ast_channel flags */
522 enum {
523         /*! Queue incoming dtmf, to be released when this flag is turned off */
524         AST_FLAG_DEFER_DTMF =    (1 << 1),
525         /*! write should be interrupt generator */
526         AST_FLAG_WRITE_INT =     (1 << 2),
527         /*! a thread is blocking on this channel */
528         AST_FLAG_BLOCKING =      (1 << 3),
529         /*! This is a zombie channel */
530         AST_FLAG_ZOMBIE =        (1 << 4),
531         /*! There is an exception pending */
532         AST_FLAG_EXCEPTION =     (1 << 5),
533         /*! Listening to moh XXX anthm promises me this will disappear XXX */
534         AST_FLAG_MOH =           (1 << 6),
535         /*! This channel is spying on another channel */
536         AST_FLAG_SPYING =        (1 << 7),
537         /*! This channel is in a native bridge */
538         AST_FLAG_NBRIDGE =       (1 << 8),
539         /*! the channel is in an auto-incrementing dialplan processor,
540          *  so when ->priority is set, it will get incremented before
541          *  finding the next priority to run */
542         AST_FLAG_IN_AUTOLOOP =   (1 << 9),
543         /*! This is an outgoing call */
544         AST_FLAG_OUTGOING =      (1 << 10),
545         /*! A DTMF_BEGIN frame has been read from this channel, but not yet an END */
546         AST_FLAG_IN_DTMF =       (1 << 12),
547         /*! A DTMF_END was received when not IN_DTMF, so the length of the digit is 
548          *  currently being emulated */
549         AST_FLAG_EMULATE_DTMF =  (1 << 13),
550         /*! This is set to tell the channel not to generate DTMF begin frames, and
551          *  to instead only generate END frames. */
552         AST_FLAG_END_DTMF_ONLY = (1 << 14),
553         /*! Flag to show channels that this call is hangup due to the fact that the call
554             was indeed anwered, but in another channel */
555         AST_FLAG_ANSWERED_ELSEWHERE = (1 << 15),
556         /*! This flag indicates that on a masquerade, an active stream should not
557          *  be carried over */
558         AST_FLAG_MASQ_NOSTREAM = (1 << 16),
559 };
560
561 /*! \brief ast_bridge_config flags */
562 enum {
563         AST_FEATURE_PLAY_WARNING = (1 << 0),
564         AST_FEATURE_REDIRECT =     (1 << 1),
565         AST_FEATURE_DISCONNECT =   (1 << 2),
566         AST_FEATURE_ATXFER =       (1 << 3),
567         AST_FEATURE_AUTOMON =      (1 << 4),
568         AST_FEATURE_PARKCALL =     (1 << 5),
569         AST_FEATURE_AUTOMIXMON =   (1 << 6),
570 };
571
572 /*! \brief bridge configuration */
573 struct ast_bridge_config {
574         struct ast_flags features_caller;
575         struct ast_flags features_callee;
576         struct timeval start_time;
577         long feature_timer;
578         long timelimit;
579         long play_warning;
580         long warning_freq;
581         const char *warning_sound;
582         const char *end_sound;
583         const char *start_sound;
584         int firstpass;
585         unsigned int flags;
586 };
587
588 struct chanmon;
589
590 struct outgoing_helper {
591         const char *context;
592         const char *exten;
593         int priority;
594         const char *cid_num;
595         const char *cid_name;
596         const char *account;
597         struct ast_variable *vars;
598         struct ast_channel *parent_channel;
599 };
600
601 enum {
602         AST_CDR_TRANSFER =   (1 << 0),
603         AST_CDR_FORWARD =    (1 << 1),
604         AST_CDR_CALLWAIT =   (1 << 2),
605         AST_CDR_CONFERENCE = (1 << 3),
606 };
607
608 enum {
609         /*! Soft hangup by device */
610         AST_SOFTHANGUP_DEV =       (1 << 0),
611         /*! Soft hangup for async goto */
612         AST_SOFTHANGUP_ASYNCGOTO = (1 << 1),
613         AST_SOFTHANGUP_SHUTDOWN =  (1 << 2),
614         AST_SOFTHANGUP_TIMEOUT =   (1 << 3),
615         AST_SOFTHANGUP_APPUNLOAD = (1 << 4),
616         AST_SOFTHANGUP_EXPLICIT =  (1 << 5),
617         AST_SOFTHANGUP_UNBRIDGE =  (1 << 6),
618 };
619
620
621 /*! \brief Channel reload reasons for manager events at load or reload of configuration */
622 enum channelreloadreason {
623         CHANNEL_MODULE_LOAD,
624         CHANNEL_MODULE_RELOAD,
625         CHANNEL_CLI_RELOAD,
626         CHANNEL_MANAGER_RELOAD,
627 };
628
629 /*! 
630  * \brief Create a channel datastore structure 
631  *
632  * \note None of the datastore API calls lock the ast_channel they are using.
633  *       So, the channel should be locked before calling the functions that
634  *       take a channel argument.
635  */
636 struct ast_datastore *ast_channel_datastore_alloc(const struct ast_datastore_info *info, const char *uid);
637
638 /*! \brief Free a channel datastore structure */
639 int ast_channel_datastore_free(struct ast_datastore *datastore);
640
641 /*! \brief Inherit datastores from a parent to a child. */
642 int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to);
643
644 /*! 
645  * \brief Add a datastore to a channel 
646  *
647  * \note The channel should be locked before calling this function.
648  *
649  * \retval 0 success
650  * \retval non-zero failure
651  */
652 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore);
653
654 /*! 
655  * \brief Remove a datastore from a channel 
656  *
657  * \note The channel should be locked before calling this function.
658  *
659  * \retval 0 success
660  * \retval non-zero failure
661  */
662 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore);
663
664 /*! 
665  * \brief Find a datastore on a channel 
666  *
667  * \note The channel should be locked before calling this function.
668  *
669  * \note The datastore returned from this function must not be used if the
670  *       reference to the channel is released.
671  */
672 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid);
673
674 /*! \brief Change the state of a channel */
675 int ast_setstate(struct ast_channel *chan, enum ast_channel_state);
676
677 /*! 
678  * \brief Create a channel structure 
679  *
680  * \retval NULL failure
681  * \retval non-NULL successfully allocated channel
682  *
683  * \note By default, new channels are set to the "s" extension
684  *       and "default" context.
685  */
686 struct ast_channel *ast_channel_alloc(int needqueue, int state, const char *cid_num, const char *cid_name, const char *acctcode, const char *exten, const char *context, const int amaflag, const char *name_fmt, ...);
687
688 /*! 
689  * \brief Queue an outgoing frame 
690  *
691  * \note The channel does not need to be locked before calling this function.
692  */
693 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *f);
694
695 /*! 
696  * \brief Queue a hangup frame 
697  *
698  * \note The channel does not need to be locked before calling this function.
699  */
700 int ast_queue_hangup(struct ast_channel *chan);
701
702 /*!
703  * \brief Queue a control frame with payload
704  *
705  * \param chan channel to queue frame onto
706  * \param control type of control frame
707  *
708  * \note The channel does not need to be locked before calling this function.
709  *
710  * \retval zero on success
711  * \retval non-zero on failure
712  */
713 int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control);
714
715 /*!
716  * \brief Queue a control frame with payload
717  *
718  * \param chan channel to queue frame onto
719  * \param control type of control frame
720  * \param data pointer to payload data to be included in frame
721  * \param datalen number of bytes of payload data
722  *
723  * \retval 0 success
724  * \retval non-zero failure
725  *
726  * The supplied payload data is copied into the frame, so the caller's copy
727  * is not modified nor freed, and the resulting frame will retain a copy of
728  * the data even if the caller frees their local copy.
729  *
730  * \note This method should be treated as a 'network transport'; in other
731  * words, your frames may be transferred across an IAX2 channel to another
732  * system, which may be a different endianness than yours. Because of this,
733  * you should ensure that either your frames will never be expected to work
734  * across systems, or that you always put your payload data into 'network byte
735  * order' before calling this function.
736  *
737  * \note The channel does not need to be locked before calling this function.
738  */
739 int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control,
740                            const void *data, size_t datalen);
741
742 /*! 
743  * \brief Change channel name 
744  *
745  * \note The channel must be locked before calling this function.
746  */
747 void ast_change_name(struct ast_channel *chan, char *newname);
748
749 /*! \brief Free a channel structure */
750 void  ast_channel_free(struct ast_channel *);
751
752 /*! 
753  * \brief Requests a channel 
754  *
755  * \param type type of channel to request
756  * \param format requested channel format (codec)
757  * \param data data to pass to the channel requester
758  * \param status status
759  *
760  * Request a channel of a given type, with data as optional information used 
761  * by the low level module
762  *
763  * \retval NULL failure
764  * \retval non-NULL channel on success
765  */
766 struct ast_channel *ast_request(const char *type, int format, void *data, int *status);
767
768 /*!
769  * \brief Request a channel of a given type, with data as optional information used 
770  *        by the low level module and attempt to place a call on it
771  *
772  * \param type type of channel to request
773  * \param format requested channel format
774  * \param data data to pass to the channel requester
775  * \param timeout maximum amount of time to wait for an answer
776  * \param reason why unsuccessful (if unsuccessful)
777  * \param cid_num Caller-ID Number
778  * \param cid_name Caller-ID Name (ascii)
779  *
780  * \return Returns an ast_channel on success or no answer, NULL on failure.  Check the value of chan->_state
781  * to know if the call was answered or not.
782  */
783 struct ast_channel *ast_request_and_dial(const char *type, int format, void *data,
784         int timeout, int *reason, const char *cid_num, const char *cid_name);
785
786 /*!
787  * \brief Request a channel of a given type, with data as optional information used 
788  * by the low level module and attempt to place a call on it
789  * \param type type of channel to request
790  * \param format requested channel format
791  * \param data data to pass to the channel requester
792  * \param timeout maximum amount of time to wait for an answer
793  * \param reason why unsuccessful (if unsuccessful)
794  * \param cid_num Caller-ID Number
795  * \param cid_name Caller-ID Name (ascii)
796  * \param oh Outgoing helper
797  * \return Returns an ast_channel on success or no answer, NULL on failure.  Check the value of chan->_state
798  * to know if the call was answered or not.
799  */
800 struct ast_channel *__ast_request_and_dial(const char *type, int format, void *data,
801         int timeout, int *reason, const char *cid_num, const char *cid_name, struct outgoing_helper *oh);
802
803 /*!\brief Register a channel technology (a new channel driver)
804  * Called by a channel module to register the kind of channels it supports.
805  * \param tech Structure defining channel technology or "type"
806  * \return Returns 0 on success, -1 on failure.
807  */
808 int ast_channel_register(const struct ast_channel_tech *tech);
809
810 /*! \brief Unregister a channel technology 
811  * \param tech Structure defining channel technology or "type" that was previously registered
812  * \return No return value.
813  */
814 void ast_channel_unregister(const struct ast_channel_tech *tech);
815
816 /*! \brief Get a channel technology structure by name
817  * \param name name of technology to find
818  * \return a pointer to the structure, or NULL if no matching technology found
819  */
820 const struct ast_channel_tech *ast_get_channel_tech(const char *name);
821
822 /*! \brief Hang up a channel  
823  * \note This function performs a hard hangup on a channel.  Unlike the soft-hangup, this function
824  * performs all stream stopping, etc, on the channel that needs to end.
825  * chan is no longer valid after this call.
826  * \param chan channel to hang up
827  * \return Returns 0 on success, -1 on failure.
828  */
829 int ast_hangup(struct ast_channel *chan);
830
831 /*! 
832  * \brief Softly hangup up a channel 
833  *
834  * \param chan channel to be soft-hung-up
835  * \param cause Ast hangupcause for hangup
836  *
837  * Call the protocol layer, but don't destroy the channel structure 
838  * (use this if you are trying to
839  * safely hangup a channel managed by another thread.
840  *
841  * \note The channel passed to this function does not need to be locked.
842  *
843  * \return Returns 0 regardless
844  */
845 int ast_softhangup(struct ast_channel *chan, int cause);
846
847 /*! \brief Softly hangup up a channel (no channel lock) 
848  * \param chan channel to be soft-hung-up
849  * \param cause Ast hangupcause for hangup (see cause.h) */
850 int ast_softhangup_nolock(struct ast_channel *chan, int cause);
851
852 /*! \brief Check to see if a channel is needing hang up 
853  * \param chan channel on which to check for hang up
854  * This function determines if the channel is being requested to be hung up.
855  * \return Returns 0 if not, or 1 if hang up is requested (including time-out).
856  */
857 int ast_check_hangup(struct ast_channel *chan);
858
859 /*! \brief Compare a offset with the settings of when to hang a channel up 
860  * \param chan channel on which to check for hang up
861  * \param offset offset in seconds from current time
862  * \return 1, 0, or -1
863  * This function compares a offset from current time with the absolute time 
864  * out on a channel (when to hang up). If the absolute time out on a channel
865  * is earlier than current time plus the offset, it returns 1, if the two
866  * time values are equal, it return 0, otherwise, it return -1.
867  */
868 int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset);
869
870 /*! \brief Set when to hang a channel up 
871  *
872  * \param chan channel on which to check for hang up
873  * \param offset offset in seconds from current time of when to hang up
874  *
875  * This function sets the absolute time out on a channel (when to hang up).
876  *
877  * \note This function does not require that the channel is locked before
878  *       calling it.
879  *
880  * \return Nothing
881  */
882 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset);
883
884 /*! 
885  * \brief Answer a channel
886  * 
887  * \param chan channel to answer
888  *
889  * This function answers a channel and handles all necessary call
890  * setup functions.
891  *
892  * \note The channel passed does not need to be locked.
893  *
894  * \retval 0 on success
895  * \retval non-zero on failure
896  */
897 int ast_answer(struct ast_channel *chan);
898 int __ast_answer(struct ast_channel *chan, unsigned int delay);
899
900 /*! \brief Make a call 
901  * \param chan which channel to make the call on
902  * \param addr destination of the call
903  * \param timeout time to wait on for connect
904  * Place a call, take no longer than timeout ms. 
905    \return Returns -1 on failure, 0 on not enough time 
906    (does not automatically stop ringing), and  
907    the number of seconds the connect took otherwise.
908    */
909 int ast_call(struct ast_channel *chan, char *addr, int timeout);
910
911 /*! \brief Indicates condition of channel 
912  * \note Indicate a condition such as AST_CONTROL_BUSY, AST_CONTROL_RINGING, or AST_CONTROL_CONGESTION on a channel
913  * \param chan channel to change the indication
914  * \param condition which condition to indicate on the channel
915  * \return Returns 0 on success, -1 on failure
916  */
917 int ast_indicate(struct ast_channel *chan, int condition);
918
919 /*! \brief Indicates condition of channel, with payload
920  * \note Indicate a condition such as AST_CONTROL_HOLD with payload being music on hold class
921  * \param chan channel to change the indication
922  * \param condition which condition to indicate on the channel
923  * \param data pointer to payload data
924  * \param datalen size of payload data
925  * \return Returns 0 on success, -1 on failure
926  */
927 int ast_indicate_data(struct ast_channel *chan, int condition, const void *data, size_t datalen);
928
929 /* Misc stuff ------------------------------------------------ */
930
931 /*! \brief Wait for input on a channel 
932  * \param chan channel to wait on
933  * \param ms length of time to wait on the channel
934  * Wait for input on a channel for a given # of milliseconds (<0 for indefinite). 
935   \return Returns < 0 on  failure, 0 if nothing ever arrived, and the # of ms remaining otherwise */
936 int ast_waitfor(struct ast_channel *chan, int ms);
937
938 /*! \brief Wait for a specified amount of time, looking for hangups 
939  * \param chan channel to wait for
940  * \param ms length of time in milliseconds to sleep
941  * Waits for a specified amount of time, servicing the channel as required.
942  * \return returns -1 on hangup, otherwise 0.
943  */
944 int ast_safe_sleep(struct ast_channel *chan, int ms);
945
946 /*! \brief Wait for a specified amount of time, looking for hangups and a condition argument 
947  * \param chan channel to wait for
948  * \param ms length of time in milliseconds to sleep
949  * \param cond a function pointer for testing continue condition
950  * \param data argument to be passed to the condition test function
951  * \return returns -1 on hangup, otherwise 0.
952  * Waits for a specified amount of time, servicing the channel as required. If cond
953  * returns 0, this function returns.
954  */
955 int ast_safe_sleep_conditional(struct ast_channel *chan, int ms, int (*cond)(void*), void *data );
956
957 /*! \brief Waits for activity on a group of channels 
958  * \param chan an array of pointers to channels
959  * \param n number of channels that are to be waited upon
960  * \param fds an array of fds to wait upon
961  * \param nfds the number of fds to wait upon
962  * \param exception exception flag
963  * \param outfd fd that had activity on it
964  * \param ms how long the wait was
965  * Big momma function here.  Wait for activity on any of the n channels, or any of the nfds
966    file descriptors.
967    \return Returns the channel with activity, or NULL on error or if an FD
968    came first.  If the FD came first, it will be returned in outfd, otherwise, outfd
969    will be -1 */
970 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **chan, int n,
971         int *fds, int nfds, int *exception, int *outfd, int *ms);
972
973 /*! \brief Waits for input on a group of channels
974    Wait for input on an array of channels for a given # of milliseconds. 
975         \return Return channel with activity, or NULL if none has activity.  
976         \param chan an array of pointers to channels
977         \param n number of channels that are to be waited upon
978         \param ms time "ms" is modified in-place, if applicable */
979 struct ast_channel *ast_waitfor_n(struct ast_channel **chan, int n, int *ms);
980
981 /*! \brief Waits for input on an fd
982         This version works on fd's only.  Be careful with it. */
983 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception);
984
985
986 /*! \brief Reads a frame
987  * \param chan channel to read a frame from
988  * \return Returns a frame, or NULL on error.  If it returns NULL, you
989         best just stop reading frames and assume the channel has been
990         disconnected. */
991 struct ast_frame *ast_read(struct ast_channel *chan);
992
993 /*! \brief Reads a frame, returning AST_FRAME_NULL frame if audio. 
994         \param chan channel to read a frame from
995         \return  Returns a frame, or NULL on error.  If it returns NULL, you
996                 best just stop reading frames and assume the channel has been
997                 disconnected.  
998         \note Audio is replaced with AST_FRAME_NULL to avoid 
999         transcode when the resulting audio is not necessary. */
1000 struct ast_frame *ast_read_noaudio(struct ast_channel *chan);
1001
1002 /*! \brief Write a frame to a channel 
1003  * This function writes the given frame to the indicated channel.
1004  * \param chan destination channel of the frame
1005  * \param frame frame that will be written
1006  * \return It returns 0 on success, -1 on failure.
1007  */
1008 int ast_write(struct ast_channel *chan, struct ast_frame *frame);
1009
1010 /*! \brief Write video frame to a channel 
1011  * This function writes the given frame to the indicated channel.
1012  * \param chan destination channel of the frame
1013  * \param frame frame that will be written
1014  * \return It returns 1 on success, 0 if not implemented, and -1 on failure.
1015  */
1016 int ast_write_video(struct ast_channel *chan, struct ast_frame *frame);
1017
1018 /*! \brief Write text frame to a channel 
1019  * This function writes the given frame to the indicated channel.
1020  * \param chan destination channel of the frame
1021  * \param frame frame that will be written
1022  * \return It returns 1 on success, 0 if not implemented, and -1 on failure.
1023  */
1024 int ast_write_text(struct ast_channel *chan, struct ast_frame *frame);
1025
1026 /*! \brief Send empty audio to prime a channel driver */
1027 int ast_prod(struct ast_channel *chan);
1028
1029 /*! \brief Sets read format on channel chan
1030  * Set read format for channel to whichever component of "format" is best. 
1031  * \param chan channel to change
1032  * \param format format to change to
1033  * \return Returns 0 on success, -1 on failure
1034  */
1035 int ast_set_read_format(struct ast_channel *chan, int format);
1036
1037 /*! \brief Sets write format on channel chan
1038  * Set write format for channel to whichever component of "format" is best. 
1039  * \param chan channel to change
1040  * \param format new format for writing
1041  * \return Returns 0 on success, -1 on failure
1042  */
1043 int ast_set_write_format(struct ast_channel *chan, int format);
1044
1045 /*! 
1046  * \brief Sends text to a channel 
1047  *
1048  * \param chan channel to act upon
1049  * \param text string of text to send on the channel
1050  *
1051  * Write text to a display on a channel
1052  *
1053  * \note The channel does not need to be locked before calling this function.
1054  *
1055  * \retval 0 on success 
1056  * \retval -1 on failure
1057  */
1058 int ast_sendtext(struct ast_channel *chan, const char *text);
1059
1060 /*! \brief Receives a text character from a channel
1061  * \param chan channel to act upon
1062  * \param timeout timeout in milliseconds (0 for infinite wait)
1063  * Read a char of text from a channel
1064  * Returns 0 on success, -1 on failure
1065  */
1066 int ast_recvchar(struct ast_channel *chan, int timeout);
1067
1068 /*! \brief Send a DTMF digit to a channel
1069  * Send a DTMF digit to a channel.
1070  * \param chan channel to act upon
1071  * \param digit the DTMF digit to send, encoded in ASCII
1072  * \param duration the duration of the digit ending in ms
1073  * \return Returns 0 on success, -1 on failure
1074  */
1075 int ast_senddigit(struct ast_channel *chan, char digit, unsigned int duration);
1076
1077 /*! \brief Send a DTMF digit to a channel
1078  * Send a DTMF digit to a channel.
1079  * \param chan channel to act upon
1080  * \param digit the DTMF digit to send, encoded in ASCII
1081  * \return Returns 0 on success, -1 on failure
1082  */
1083 int ast_senddigit_begin(struct ast_channel *chan, char digit);
1084
1085 /*! \brief Send a DTMF digit to a channel
1086
1087  * Send a DTMF digit to a channel.
1088  * \param chan channel to act upon
1089  * \param digit the DTMF digit to send, encoded in ASCII
1090  * \param duration the duration of the digit ending in ms
1091  * \return Returns 0 on success, -1 on failure
1092  */
1093 int ast_senddigit_end(struct ast_channel *chan, char digit, unsigned int duration);
1094
1095 /*! \brief Receives a text string from a channel
1096  * Read a string of text from a channel
1097  * \param chan channel to act upon
1098  * \param timeout timeout in milliseconds (0 for infinite wait)
1099  * \return the received text, or NULL to signify failure.
1100  */
1101 char *ast_recvtext(struct ast_channel *chan, int timeout);
1102
1103 /*! \brief Browse channels in use
1104  * Browse the channels currently in use 
1105  * \param prev where you want to start in the channel list
1106  * \return Returns the next channel in the list, NULL on end.
1107  *      If it returns a channel, that channel *has been locked*!
1108  */
1109 struct ast_channel *ast_channel_walk_locked(const struct ast_channel *prev);
1110
1111 /*! \brief Get channel by name (locks channel) */
1112 struct ast_channel *ast_get_channel_by_name_locked(const char *chan);
1113
1114 /*! \brief Get channel by name prefix (locks channel) */
1115 struct ast_channel *ast_get_channel_by_name_prefix_locked(const char *name, const int namelen);
1116
1117 /*! \brief Get channel by name prefix (locks channel) */
1118 struct ast_channel *ast_walk_channel_by_name_prefix_locked(const struct ast_channel *chan, const char *name, const int namelen);
1119
1120 /*! \brief Get channel by exten (and optionally context) and lock it */
1121 struct ast_channel *ast_get_channel_by_exten_locked(const char *exten, const char *context);
1122
1123 /*! \brief Get next channel by exten (and optionally context) and lock it */
1124 struct ast_channel *ast_walk_channel_by_exten_locked(const struct ast_channel *chan, const char *exten,
1125                                                      const char *context);
1126
1127 /*! ! \brief Waits for a digit
1128  * \param c channel to wait for a digit on
1129  * \param ms how many milliseconds to wait
1130  * \return Returns <0 on error, 0 on no entry, and the digit on success. */
1131 int ast_waitfordigit(struct ast_channel *c, int ms);
1132
1133 /*! \brief Wait for a digit
1134  Same as ast_waitfordigit() with audio fd for outputting read audio and ctrlfd to monitor for reading. 
1135  * \param c channel to wait for a digit on
1136  * \param ms how many milliseconds to wait
1137  * \param audiofd audio file descriptor to write to if audio frames are received
1138  * \param ctrlfd control file descriptor to monitor for reading
1139  * \return Returns 1 if ctrlfd becomes available */
1140 int ast_waitfordigit_full(struct ast_channel *c, int ms, int audiofd, int ctrlfd);
1141
1142 /*! Reads multiple digits 
1143  * \param c channel to read from
1144  * \param s string to read in to.  Must be at least the size of your length
1145  * \param len how many digits to read (maximum)
1146  * \param timeout how long to timeout between digits
1147  * \param rtimeout timeout to wait on the first digit
1148  * \param enders digits to end the string
1149  * Read in a digit string "s", max length "len", maximum timeout between 
1150    digits "timeout" (-1 for none), terminated by anything in "enders".  Give them rtimeout
1151    for the first digit.  Returns 0 on normal return, or 1 on a timeout.  In the case of
1152    a timeout, any digits that were read before the timeout will still be available in s.  
1153    RETURNS 2 in full version when ctrlfd is available, NOT 1*/
1154 int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int rtimeout, char *enders);
1155 int ast_readstring_full(struct ast_channel *c, char *s, int len, int timeout, int rtimeout, char *enders, int audiofd, int ctrlfd);
1156
1157 /*! \brief Report DTMF on channel 0 */
1158 #define AST_BRIDGE_DTMF_CHANNEL_0               (1 << 0)                
1159 /*! \brief Report DTMF on channel 1 */
1160 #define AST_BRIDGE_DTMF_CHANNEL_1               (1 << 1)                
1161 /*! \brief Return all voice frames on channel 0 */
1162 #define AST_BRIDGE_REC_CHANNEL_0                (1 << 2)                
1163 /*! \brief Return all voice frames on channel 1 */
1164 #define AST_BRIDGE_REC_CHANNEL_1                (1 << 3)                
1165 /*! \brief Ignore all signal frames except NULL */
1166 #define AST_BRIDGE_IGNORE_SIGS                  (1 << 4)                
1167
1168
1169 /*! \brief Makes two channel formats compatible 
1170  * \param c0 first channel to make compatible
1171  * \param c1 other channel to make compatible
1172  * Set two channels to compatible formats -- call before ast_channel_bridge in general .  
1173  * \return Returns 0 on success and -1 if it could not be done */
1174 int ast_channel_make_compatible(struct ast_channel *c0, struct ast_channel *c1);
1175
1176 /*! Bridge two channels together (early)
1177  * \param c0 first channel to bridge
1178  * \param c1 second channel to bridge
1179  * Bridge two channels (c0 and c1) together early. This implies either side may not be answered yet.
1180  * \return Returns 0 on success and -1 if it could not be done */
1181 int ast_channel_early_bridge(struct ast_channel *c0, struct ast_channel *c1);
1182
1183 /*! Bridge two channels together 
1184  * \param c0 first channel to bridge
1185  * \param c1 second channel to bridge
1186  * \param config config for the channels
1187  * \param fo destination frame(?)
1188  * \param rc destination channel(?)
1189  * Bridge two channels (c0 and c1) together.  If an important frame occurs, we return that frame in
1190    *rf (remember, it could be NULL) and which channel (0 or 1) in rc */
1191 /* int ast_channel_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc); */
1192 int ast_channel_bridge(struct ast_channel *c0,struct ast_channel *c1,
1193         struct ast_bridge_config *config, struct ast_frame **fo, struct ast_channel **rc);
1194
1195 /*! 
1196  * \brief Weird function made for call transfers
1197  *
1198  * \param original channel to make a copy of
1199  * \param clone copy of the original channel
1200  *
1201  * This is a very strange and freaky function used primarily for transfer.  Suppose that
1202  * "original" and "clone" are two channels in random situations.  This function takes
1203  * the guts out of "clone" and puts them into the "original" channel, then alerts the
1204  * channel driver of the change, asking it to fixup any private information (like the
1205  * p->owner pointer) that is affected by the change.  The physical layer of the original
1206  * channel is hung up.  
1207  *
1208  * \note Neither channel passed here needs to be locked before calling this function.
1209  */
1210 int ast_channel_masquerade(struct ast_channel *original, struct ast_channel *clone);
1211
1212 /*! Gives the string form of a given cause code */
1213 /*! 
1214  * \param state cause to get the description of
1215  * Give a name to a cause code
1216  * Returns the text form of the binary cause code given
1217  */
1218 const char *ast_cause2str(int state) attribute_pure;
1219
1220 /*! Convert the string form of a cause code to a number */
1221 /*! 
1222  * \param name string form of the cause
1223  * Returns the cause code
1224  */
1225 int ast_str2cause(const char *name) attribute_pure;
1226
1227 /*! Gives the string form of a given channel state */
1228 /*! 
1229  * \param ast_channel_state state to get the name of
1230  * Give a name to a state 
1231  * Returns the text form of the binary state given
1232  */
1233 const char *ast_state2str(enum ast_channel_state);
1234
1235 /*! Gives the string form of a given transfer capability */
1236 /*!
1237  * \param transfercapability transfercapabilty to get the name of
1238  * Give a name to a transfercapbility
1239  * See above
1240  * Returns the text form of the binary transfer capbaility
1241  */
1242 char *ast_transfercapability2str(int transfercapability) attribute_const;
1243
1244 /* Options: Some low-level drivers may implement "options" allowing fine tuning of the
1245    low level channel.  See frame.h for options.  Note that many channel drivers may support
1246    none or a subset of those features, and you should not count on this if you want your
1247    asterisk application to be portable.  They're mainly useful for tweaking performance */
1248
1249 /*! Sets an option on a channel */
1250 /*! 
1251  * \param channel channel to set options on
1252  * \param option option to change
1253  * \param data data specific to option
1254  * \param datalen length of the data
1255  * \param block blocking or not
1256  * Set an option on a channel (see frame.h), optionally blocking awaiting the reply 
1257  * Returns 0 on success and -1 on failure
1258  */
1259 int ast_channel_setoption(struct ast_channel *channel, int option, void *data, int datalen, int block);
1260
1261 /*! Pick the best codec  */
1262 /* Choose the best codec...  Uhhh...   Yah. */
1263 int ast_best_codec(int fmts);
1264
1265
1266 /*! Checks the value of an option */
1267 /*! 
1268  * Query the value of an option, optionally blocking until a reply is received
1269  * Works similarly to setoption except only reads the options.
1270  */
1271 struct ast_frame *ast_channel_queryoption(struct ast_channel *channel, int option, void *data, int *datalen, int block);
1272
1273 /*! Checks for HTML support on a channel */
1274 /*! Returns 0 if channel does not support HTML or non-zero if it does */
1275 int ast_channel_supports_html(struct ast_channel *channel);
1276
1277 /*! Sends HTML on given channel */
1278 /*! Send HTML or URL on link.  Returns 0 on success or -1 on failure */
1279 int ast_channel_sendhtml(struct ast_channel *channel, int subclass, const char *data, int datalen);
1280
1281 /*! Sends a URL on a given link */
1282 /*! Send URL on link.  Returns 0 on success or -1 on failure */
1283 int ast_channel_sendurl(struct ast_channel *channel, const char *url);
1284
1285 /*! Defers DTMF */
1286 /*! Defer DTMF so that you only read things like hangups and audio.  Returns
1287    non-zero if channel was already DTMF-deferred or 0 if channel is just now
1288    being DTMF-deferred */
1289 int ast_channel_defer_dtmf(struct ast_channel *chan);
1290
1291 /*! Undo defer.  ast_read will return any dtmf characters that were queued */
1292 void ast_channel_undefer_dtmf(struct ast_channel *chan);
1293
1294 /*! Initiate system shutdown -- prevents new channels from being allocated.
1295     If "hangup" is non-zero, all existing channels will receive soft
1296      hangups */
1297 void ast_begin_shutdown(int hangup);
1298
1299 /*! Cancels an existing shutdown and returns to normal operation */
1300 void ast_cancel_shutdown(void);
1301
1302 /*! Returns number of active/allocated channels */
1303 int ast_active_channels(void);
1304
1305 /*! Returns non-zero if Asterisk is being shut down */
1306 int ast_shutting_down(void);
1307
1308 /*! Activate a given generator */
1309 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params);
1310
1311 /*! Deactivate an active generator */
1312 void ast_deactivate_generator(struct ast_channel *chan);
1313
1314 /*!
1315  * \brief Set caller ID number, name and ANI
1316  *
1317  * \note The channel does not need to be locked before calling this function.
1318  */
1319 void ast_set_callerid(struct ast_channel *chan, const char *cid_num, const char *cid_name, const char *cid_ani);
1320
1321 /*! Set the file descriptor on the channel */
1322 void ast_channel_set_fd(struct ast_channel *chan, int which, int fd);
1323
1324 /*! Add a channel to an optimized waitfor */
1325 void ast_poll_channel_add(struct ast_channel *chan0, struct ast_channel *chan1);
1326
1327 /*! Delete a channel from an optimized waitfor */
1328 void ast_poll_channel_del(struct ast_channel *chan0, struct ast_channel *chan1);
1329
1330 /*! Start a tone going */
1331 int ast_tonepair_start(struct ast_channel *chan, int freq1, int freq2, int duration, int vol);
1332 /*! Stop a tone from playing */
1333 void ast_tonepair_stop(struct ast_channel *chan);
1334 /*! Play a tone pair for a given amount of time */
1335 int ast_tonepair(struct ast_channel *chan, int freq1, int freq2, int duration, int vol);
1336
1337 /*!
1338  * \brief Automatically service a channel for us... 
1339  *
1340  * \retval 0 success
1341  * \retval -1 failure, or the channel is already being autoserviced
1342  */
1343 int ast_autoservice_start(struct ast_channel *chan);
1344
1345 /*! 
1346  * \brief Stop servicing a channel for us...  
1347  *
1348  * \retval 0 success
1349  * \retval -1 error, or the channel has been hungup 
1350  */
1351 int ast_autoservice_stop(struct ast_channel *chan);
1352
1353 /* If built with zaptel optimizations, force a scheduled expiration on the
1354    timer fd, at which point we call the callback function / data */
1355 int ast_settimeout(struct ast_channel *c, int samples, int (*func)(const void *data), void *data);
1356
1357 /*!     \brief Transfer a channel (if supported).  Returns -1 on error, 0 if not supported
1358    and 1 if supported and requested 
1359         \param chan current channel
1360         \param dest destination extension for transfer
1361 */
1362 int ast_transfer(struct ast_channel *chan, char *dest);
1363
1364 /*!     \brief  Start masquerading a channel
1365         XXX This is a seriously whacked out operation.  We're essentially putting the guts of
1366            the clone channel into the original channel.  Start by killing off the original
1367            channel's backend.   I'm not sure we're going to keep this function, because
1368            while the features are nice, the cost is very high in terms of pure nastiness. XXX
1369         \param chan     Channel to masquerade
1370 */
1371 int ast_do_masquerade(struct ast_channel *chan);
1372
1373 /*!     \brief Find bridged channel 
1374         \param chan Current channel
1375 */
1376 struct ast_channel *ast_bridged_channel(struct ast_channel *chan);
1377
1378 /*!
1379   \brief Inherits channel variable from parent to child channel
1380   \param parent Parent channel
1381   \param child Child channel
1382
1383   Scans all channel variables in the parent channel, looking for those
1384   that should be copied into the child channel.
1385   Variables whose names begin with a single '_' are copied into the
1386   child channel with the prefix removed.
1387   Variables whose names begin with '__' are copied into the child
1388   channel with their names unchanged.
1389 */
1390 void ast_channel_inherit_variables(const struct ast_channel *parent, struct ast_channel *child);
1391
1392 /*!
1393   \brief adds a list of channel variables to a channel
1394   \param chan the channel
1395   \param vars a linked list of variables
1396
1397   Variable names can be for a regular channel variable or a dialplan function
1398   that has the ability to be written to.
1399 */
1400 void ast_set_variables(struct ast_channel *chan, struct ast_variable *vars);
1401
1402 /*!
1403   \brief An opaque 'object' structure use by silence generators on channels.
1404  */
1405 struct ast_silence_generator;
1406
1407 /*!
1408   \brief Starts a silence generator on the given channel.
1409   \param chan The channel to generate silence on
1410   \return An ast_silence_generator pointer, or NULL if an error occurs
1411
1412   This function will cause SLINEAR silence to be generated on the supplied
1413   channel until it is disabled; if the channel cannot be put into SLINEAR
1414   mode then the function will fail.
1415
1416   The pointer returned by this function must be preserved and passed to
1417   ast_channel_stop_silence_generator when you wish to stop the silence
1418   generation.
1419  */
1420 struct ast_silence_generator *ast_channel_start_silence_generator(struct ast_channel *chan);
1421
1422 /*!
1423   \brief Stops a previously-started silence generator on the given channel.
1424   \param chan The channel to operate on
1425   \param state The ast_silence_generator pointer return by a previous call to
1426   ast_channel_start_silence_generator.
1427   \return nothing
1428
1429   This function will stop the operating silence generator and return the channel
1430   to its previous write format.
1431  */
1432 void ast_channel_stop_silence_generator(struct ast_channel *chan, struct ast_silence_generator *state);
1433
1434 /*!
1435   \brief Check if the channel can run in internal timing mode.
1436   \param chan The channel to check
1437   \return boolean
1438
1439   This function will return 1 if internal timing is enabled and the timing
1440   device is available.
1441  */
1442 int ast_internal_timing_enabled(struct ast_channel *chan);
1443
1444 /* Misc. functions below */
1445
1446 /*! \brief if fd is a valid descriptor, set *pfd with the descriptor
1447  * \return Return 1 (not -1!) if added, 0 otherwise (so we can add the
1448  * return value to the index into the array)
1449  */
1450 static inline int ast_add_fd(struct pollfd *pfd, int fd)
1451 {
1452         pfd->fd = fd;
1453         pfd->events = POLLIN | POLLPRI;
1454         return fd >= 0;
1455 }
1456
1457 /*! \brief Helper function for migrating select to poll */
1458 static inline int ast_fdisset(struct pollfd *pfds, int fd, int max, int *start)
1459 {
1460         int x;
1461         int dummy=0;
1462
1463         if (fd < 0)
1464                 return 0;
1465         if (!start)
1466                 start = &dummy;
1467         for (x = *start; x<max; x++)
1468                 if (pfds[x].fd == fd) {
1469                         if (x == *start)
1470                                 (*start)++;
1471                         return pfds[x].revents;
1472                 }
1473         return 0;
1474 }
1475
1476 #ifndef HAVE_TIMERSUB
1477 static inline void timersub(struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
1478 {
1479         tvdiff->tv_sec = tvend->tv_sec - tvstart->tv_sec;
1480         tvdiff->tv_usec = tvend->tv_usec - tvstart->tv_usec;
1481         if (tvdiff->tv_usec < 0) {
1482                 tvdiff->tv_sec --;
1483                 tvdiff->tv_usec += 1000000;
1484         }
1485
1486 }
1487 #endif
1488
1489 /*! \brief Waits for activity on a group of channels 
1490  * \param nfds the maximum number of file descriptors in the sets
1491  * \param rfds file descriptors to check for read availability
1492  * \param wfds file descriptors to check for write availability
1493  * \param efds file descriptors to check for exceptions (OOB data)
1494  * \param tvp timeout while waiting for events
1495  * This is the same as a standard select(), except it guarantees the
1496  * behaviour where the passed struct timeval is updated with how much
1497  * time was not slept while waiting for the specified events
1498  */
1499 static inline int ast_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tvp)
1500 {
1501 #ifdef __linux__
1502         return select(nfds, rfds, wfds, efds, tvp);
1503 #else
1504         if (tvp) {
1505                 struct timeval tv, tvstart, tvend, tvlen;
1506                 int res;
1507
1508                 tv = *tvp;
1509                 gettimeofday(&tvstart, NULL);
1510                 res = select(nfds, rfds, wfds, efds, tvp);
1511                 gettimeofday(&tvend, NULL);
1512                 timersub(&tvend, &tvstart, &tvlen);
1513                 timersub(&tv, &tvlen, tvp);
1514                 if (tvp->tv_sec < 0 || (tvp->tv_sec == 0 && tvp->tv_usec < 0)) {
1515                         tvp->tv_sec = 0;
1516                         tvp->tv_usec = 0;
1517                 }
1518                 return res;
1519         }
1520         else
1521                 return select(nfds, rfds, wfds, efds, NULL);
1522 #endif
1523 }
1524
1525 #ifdef DO_CRASH
1526 #define CRASH do { fprintf(stderr, "!! Forcing immediate crash a-la abort !!\n"); *((int *)0) = 0; } while(0)
1527 #else
1528 #define CRASH do { } while(0)
1529 #endif
1530
1531 #define CHECK_BLOCKING(c) do {   \
1532         if (ast_test_flag(c, AST_FLAG_BLOCKING)) {\
1533                 if (option_debug) \
1534                         ast_log(LOG_DEBUG, "Thread %ld Blocking '%s', already blocked by thread %ld in procedure %s\n", (long) pthread_self(), (c)->name, (long) (c)->blocker, (c)->blockproc); \
1535                 CRASH; \
1536         } else { \
1537                 (c)->blocker = pthread_self(); \
1538                 (c)->blockproc = __PRETTY_FUNCTION__; \
1539                 ast_set_flag(c, AST_FLAG_BLOCKING); \
1540         } } while (0)
1541
1542 ast_group_t ast_get_group(const char *s);
1543
1544 /*! \brief print call- and pickup groups into buffer */
1545 char *ast_print_group(char *buf, int buflen, ast_group_t group);
1546
1547 /*! \brief Convert enum channelreloadreason to text string for manager event
1548         \param reason   Enum channelreloadreason - reason for reload (manager, cli, start etc)
1549 */
1550 const char *channelreloadreason2txt(enum channelreloadreason reason);
1551
1552 /*! \brief return an ast_variable list of channeltypes */
1553 struct ast_variable *ast_channeltype_list(void);
1554
1555 /*!
1556   \brief return an english explanation of the code returned thru __ast_request_and_dial's 'outstate' argument
1557   \param reason  The integer argument, usually taken from AST_CONTROL_ macros
1558   \return char pointer explaining the code
1559  */
1560 const char *ast_channel_reason2str(int reason);
1561
1562 /*! \brief channel group info
1563  */
1564 struct ast_group_info {
1565         struct ast_channel *chan;
1566         char *category;
1567         char *group;
1568         AST_LIST_ENTRY(ast_group_info) list;   
1569 };
1570
1571
1572 #if defined(__cplusplus) || defined(c_plusplus)
1573 }
1574 #endif
1575
1576 #endif /* _ASTERISK_CHANNEL_H */