Sat Apr 26 2014 22:01:26

Asterisk developer's documentation


app.h
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  * See http://www.asterisk.org for more information about
00008  * the Asterisk project. Please do not directly contact
00009  * any of the maintainers of this project for assistance;
00010  * the project provides a web site, mailing lists and IRC
00011  * channels for your use.
00012  *
00013  * This program is free software, distributed under the terms of
00014  * the GNU General Public License Version 2. See the LICENSE file
00015  * at the top of the source tree.
00016  */
00017 
00018 /*! \file
00019  * \brief Application convenience functions, designed to give consistent
00020  *        look and feel to Asterisk apps.
00021  */
00022 
00023 #ifndef _ASTERISK_APP_H
00024 #define _ASTERISK_APP_H
00025 
00026 #include "asterisk/stringfields.h"
00027 #include "asterisk/strings.h"
00028 #include "asterisk/threadstorage.h"
00029 #include "asterisk/file.h"
00030 #include "asterisk/linkedlists.h"
00031 
00032 struct ast_flags64;
00033 
00034 #if defined(__cplusplus) || defined(c_plusplus)
00035 extern "C" {
00036 #endif
00037 
00038 AST_THREADSTORAGE_EXTERNAL(ast_str_thread_global_buf);
00039 
00040 /* IVR stuff */
00041 
00042 /*! \brief Callback function for IVR
00043     \return returns 0 on completion, -1 on hangup or digit if interrupted
00044   */
00045 typedef int (*ast_ivr_callback)(struct ast_channel *chan, char *option, void *cbdata);
00046 
00047 typedef enum {
00048    AST_ACTION_UPONE, /*!< adata is unused */
00049    AST_ACTION_EXIT,  /*!< adata is the return value for ast_ivr_menu_run if channel was not hungup */
00050    AST_ACTION_CALLBACK, /*!< adata is an ast_ivr_callback */
00051    AST_ACTION_PLAYBACK, /*!< adata is file to play */
00052    AST_ACTION_BACKGROUND,  /*!< adata is file to play */
00053    AST_ACTION_PLAYLIST, /*!< adata is list of files, separated by ; to play */
00054    AST_ACTION_MENU,  /*!< adata is a pointer to an ast_ivr_menu */
00055    AST_ACTION_REPEAT,   /*!< adata is max # of repeats, cast to a pointer */
00056    AST_ACTION_RESTART,  /*!< adata is like repeat, but resets repeats to 0 */
00057    AST_ACTION_TRANSFER, /*!< adata is a string with exten\verbatim[@context]\endverbatim */
00058    AST_ACTION_WAITOPTION,  /*!< adata is a timeout, or 0 for defaults */
00059    AST_ACTION_NOOP,  /*!< adata is unused */
00060    AST_ACTION_BACKLIST, /*!< adata is list of files separated by ; allows interruption */
00061 } ast_ivr_action;
00062 
00063 /*!
00064     Special "options" are:
00065    \arg "s" - "start here (one time greeting)"
00066    \arg "g" - "greeting/instructions"
00067    \arg "t" - "timeout"
00068    \arg "h" - "hangup"
00069    \arg "i" - "invalid selection"
00070 
00071 */
00072 struct ast_ivr_option {
00073    char *option;
00074    ast_ivr_action action;
00075    void *adata;
00076 };
00077 
00078 struct ast_ivr_menu {
00079    char *title;      /*!< Title of menu */
00080    unsigned int flags;  /*!< Flags */
00081    struct ast_ivr_option *options;  /*!< All options */
00082 };
00083  
00084 /*!
00085  * \brief Structure used for ast_copy_recording_to_vm in order to cleanly supply
00086  * data needed for making the recording from the recorded file.
00087  */
00088 struct ast_vm_recording_data {
00089    AST_DECLARE_STRING_FIELDS(
00090       AST_STRING_FIELD(context);
00091       AST_STRING_FIELD(mailbox);
00092       AST_STRING_FIELD(folder);
00093       AST_STRING_FIELD(recording_file);
00094       AST_STRING_FIELD(recording_ext);
00095 
00096       AST_STRING_FIELD(call_context);
00097       AST_STRING_FIELD(call_macrocontext);
00098       AST_STRING_FIELD(call_extension);
00099       AST_STRING_FIELD(call_callerchan);
00100       AST_STRING_FIELD(call_callerid);
00101       );
00102    int call_priority;
00103 };
00104 
00105 #define AST_IVR_FLAG_AUTORESTART (1 << 0)
00106 
00107 #define AST_IVR_DECLARE_MENU(holder, title, flags, foo...) \
00108    static struct ast_ivr_option __options_##holder[] = foo;\
00109    static struct ast_ivr_menu holder = { title, flags, __options_##holder }
00110 
00111 enum ast_timelen {
00112    TIMELEN_HOURS,
00113    TIMELEN_MINUTES,
00114    TIMELEN_SECONDS,
00115    TIMELEN_MILLISECONDS,
00116 };
00117 
00118 /*!   \brief Runs an IVR menu
00119    \return returns 0 on successful completion, -1 on hangup, or -2 on user error in menu */
00120 int ast_ivr_menu_run(struct ast_channel *c, struct ast_ivr_menu *menu, void *cbdata);
00121 
00122 /*! \brief Plays a stream and gets DTMF data from a channel
00123  * \param c Which channel one is interacting with
00124  * \param prompt File to pass to ast_streamfile (the one that you wish to play).
00125  *        It is also valid for this to be multiple files concatenated by "&".
00126  *        For example, "file1&file2&file3".
00127  * \param s The location where the DTMF data will be stored
00128  * \param maxlen Max Length of the data
00129  * \param timeout Timeout length waiting for data(in milliseconds).  Set to 0 for standard timeout(six seconds), or -1 for no time out.
00130  *
00131  *  This function was designed for application programmers for situations where they need
00132  *  to play a message and then get some DTMF data in response to the message.  If a digit
00133  *  is pressed during playback, it will immediately break out of the message and continue
00134  *  execution of your code.
00135  */
00136 int ast_app_getdata(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout);
00137 
00138 /*! \brief Full version with audiofd and controlfd.  NOTE: returns '2' on ctrlfd available, not '1' like other full functions */
00139 int ast_app_getdata_full(struct ast_channel *c, const char *prompt, char *s, int maxlen, int timeout, int audiofd, int ctrlfd);
00140 
00141 /*!
00142  * \brief Run a macro on a channel, placing an optional second channel into autoservice.
00143  * \since 11.0
00144  *
00145  * \details
00146  * This is a shorthand method that makes it very easy to run a
00147  * macro on any given channel.  It is perfectly reasonable to
00148  * supply a NULL autoservice_chan here in case there is no
00149  * channel to place into autoservice.
00150  *
00151  * \note Absolutely _NO_ channel locks should be held before calling this function.
00152  *
00153  * \param autoservice_chan A channel to place into autoservice while the macro is run
00154  * \param macro_chan Channel to execute macro on.
00155  * \param macro_args Macro application argument string.
00156  *
00157  * \retval 0 success
00158  * \retval -1 on error
00159  */
00160 int ast_app_exec_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, const char *macro_args);
00161 
00162 /*!
00163  * \since 1.8
00164  * \brief Run a macro on a channel, placing an optional second channel into autoservice.
00165  *
00166  * \details
00167  * This is a shorthand method that makes it very easy to run a
00168  * macro on any given channel.  It is perfectly reasonable to
00169  * supply a NULL autoservice_chan here in case there is no
00170  * channel to place into autoservice.
00171  *
00172  * \note Absolutely _NO_ channel locks should be held before calling this function.
00173  *
00174  * \param autoservice_chan A channel to place into autoservice while the macro is run
00175  * \param macro_chan Channel to execute macro on.
00176  * \param macro_name The name of the macro to run.
00177  * \param macro_args The arguments to pass to the macro.
00178  *
00179  * \retval 0 success
00180  * \retval -1 on error
00181  */
00182 int ast_app_run_macro(struct ast_channel *autoservice_chan,
00183    struct ast_channel *macro_chan, const char *macro_name, const char *macro_args);
00184 
00185 /*!
00186  * \brief Stack applications callback functions.
00187  */
00188 struct ast_app_stack_funcs {
00189    /*!
00190     * Module reference pointer so the module will stick around
00191     * while a callback is active.
00192     */
00193    void *module;
00194 
00195    /*!
00196     * \brief Callback for the routine to run a subroutine on a channel.
00197     *
00198     * \note Absolutely _NO_ channel locks should be held before calling this function.
00199     *
00200     * \param chan Channel to execute subroutine on.
00201     * \param args Gosub application argument string.
00202     * \param ignore_hangup TRUE if a hangup does not stop execution of the routine.
00203     *
00204     * \retval 0 success
00205     * \retval -1 on error
00206     */
00207    int (*run_sub)(struct ast_channel *chan, const char *args, int ignore_hangup);
00208 
00209    /*!
00210     * \brief Add missing context/exten to Gosub application argument string.
00211     *
00212     * \param chan Channel to obtain context/exten.
00213     * \param args Gosub application argument string.
00214     *
00215     * \details
00216     * Fills in the optional context and exten from the given channel.
00217     *
00218     * \retval New-args Gosub argument string on success.  Must be freed.
00219     * \retval NULL on error.
00220     */
00221    const char *(*expand_sub_args)(struct ast_channel *chan, const char *args);
00222 
00223    /* Add new API calls to the end here. */
00224 };
00225 
00226 /*!
00227  * \since 11
00228  * \brief Set stack application function callbacks
00229  * \param funcs Stack applications callback functions.
00230  */
00231 void ast_install_stack_functions(const struct ast_app_stack_funcs *funcs);
00232 
00233 /*!
00234  * \brief Add missing context/exten to subroutine argument string.
00235  *
00236  * \param chan Channel to obtain context/exten.
00237  * \param args Gosub application argument string.
00238  *
00239  * \details
00240  * Fills in the optional context and exten from the given channel.
00241  *
00242  * \retval New-args Gosub argument string on success.  Must be freed.
00243  * \retval NULL on error.
00244  */
00245 const char *ast_app_expand_sub_args(struct ast_channel *chan, const char *args);
00246 
00247 /*!
00248  * \since 11
00249  * \brief Run a subroutine on a channel, placing an optional second channel into autoservice.
00250  *
00251  * \details
00252  * This is a shorthand method that makes it very easy to run a
00253  * subroutine on any given channel.  It is perfectly reasonable
00254  * to supply a NULL autoservice_chan here in case there is no
00255  * channel to place into autoservice.
00256  *
00257  * \note Absolutely _NO_ channel locks should be held before calling this function.
00258  *
00259  * \param autoservice_chan A channel to place into autoservice while the subroutine is run
00260  * \param sub_chan Channel to execute subroutine on.
00261  * \param sub_args Gosub application argument string.
00262  * \param ignore_hangup TRUE if a hangup does not stop execution of the routine.
00263  *
00264  * \retval 0 success
00265  * \retval -1 on error
00266  */
00267 int ast_app_exec_sub(struct ast_channel *autoservice_chan, struct ast_channel *sub_chan, const char *sub_args, int ignore_hangup);
00268 
00269 /*!
00270  * \since 11
00271  * \brief Run a subroutine on a channel, placing an optional second channel into autoservice.
00272  *
00273  * \details
00274  * This is a shorthand method that makes it very easy to run a
00275  * subroutine on any given channel.  It is perfectly reasonable
00276  * to supply a NULL autoservice_chan here in case there is no
00277  * channel to place into autoservice.
00278  *
00279  * \note Absolutely _NO_ channel locks should be held before calling this function.
00280  *
00281  * \param autoservice_chan A channel to place into autoservice while the subroutine is run
00282  * \param sub_chan Channel to execute subroutine on.
00283  * \param sub_location The location of the subroutine to run.
00284  * \param sub_args The arguments to pass to the subroutine.
00285  * \param ignore_hangup TRUE if a hangup does not stop execution of the routine.
00286  *
00287  * \retval 0 success
00288  * \retval -1 on error
00289  */
00290 int ast_app_run_sub(struct ast_channel *autoservice_chan,
00291    struct ast_channel *sub_chan, const char *sub_location, const char *sub_args, int ignore_hangup);
00292 
00293 enum ast_vm_snapshot_sort_val {
00294    AST_VM_SNAPSHOT_SORT_BY_ID = 0,
00295    AST_VM_SNAPSHOT_SORT_BY_TIME,
00296 };
00297 
00298 struct ast_vm_msg_snapshot {
00299    AST_DECLARE_STRING_FIELDS(
00300       AST_STRING_FIELD(msg_id);
00301       AST_STRING_FIELD(callerid);
00302       AST_STRING_FIELD(callerchan);
00303       AST_STRING_FIELD(exten);
00304       AST_STRING_FIELD(origdate);
00305       AST_STRING_FIELD(origtime);
00306       AST_STRING_FIELD(duration);
00307       AST_STRING_FIELD(folder_name);
00308       AST_STRING_FIELD(flag);
00309    );
00310    unsigned int msg_number;
00311 
00312    AST_LIST_ENTRY(ast_vm_msg_snapshot) msg;
00313 };
00314 
00315 struct ast_vm_mailbox_snapshot {
00316    int total_msg_num;
00317    int folders;
00318    /* Things are not quite as they seem here.  This points to an allocated array of lists. */
00319    AST_LIST_HEAD_NOLOCK(, ast_vm_msg_snapshot) *snapshots;
00320 };
00321 
00322 /*!
00323  * \brief Voicemail playback callback function definition
00324  *
00325  * \param channel to play the file back on.
00326  * \param location of file on disk
00327  * \param duration of file in seconds. This will be zero if msg is very short or
00328  * has an unknown duration.
00329  */
00330 typedef void (ast_vm_msg_play_cb)(struct ast_channel *chan, const char *playfile, int duration);
00331 
00332 /*!
00333  * \brief Set voicemail function callbacks
00334  * \param[in] has_voicemail_func set function pointer
00335  * \param[in] inboxcount_func set function pointer
00336  * \param[in] inboxcount2_func set function pointer
00337  * \param[in] messagecount_func set function pointer
00338  * \param[in] sayname_func set function pointer
00339  * \version 1.6.1 Added inboxcount2_func, sayname_func
00340  */
00341 void ast_install_vm_functions(int (*has_voicemail_func)(const char *mailbox, const char *folder),
00342                int (*inboxcount_func)(const char *mailbox, int *newmsgs, int *oldmsgs),
00343                int (*inboxcount2_func)(const char *mailbox, int *urgentmsgs, int *newmsgs, int *oldmsgs),
00344                int (*messagecount_func)(const char *context, const char *mailbox, const char *folder),
00345                int (*sayname_func)(struct ast_channel *chan, const char *mailbox, const char *context),
00346                int (*copy_recording_to_vm_func)(struct ast_vm_recording_data *vm_rec_data),
00347                const char *vm_index_to_foldername(int id),
00348                struct ast_vm_mailbox_snapshot *(*vm_mailbox_snapshot_create)(const char *mailbox,
00349             const char *context,
00350             const char *folder,
00351             int descending,
00352             enum ast_vm_snapshot_sort_val sort_val,
00353             int combine_INBOX_and_OLD),
00354                struct ast_vm_mailbox_snapshot *(*vm_mailbox_snapshot_destroy)(struct ast_vm_mailbox_snapshot *mailbox_snapshot),
00355                int (*vm_msg_move)(const char *mailbox,
00356             const char *context,
00357             size_t num_msgs,
00358             const char *oldfolder,
00359             const char *old_msg_ids[],
00360             const char *newfolder),
00361                int (*vm_msg_remove)(const char *mailbox,
00362             const char *context,
00363             size_t num_msgs,
00364             const char *folder,
00365             const char *msgs[]),
00366                int (*vm_msg_forward)(const char *from_mailbox,
00367             const char *from_context,
00368             const char *from_folder,
00369             const char *to_mailbox,
00370             const char *to_context,
00371             const char *to_folder,
00372             size_t num_msgs,
00373             const char *msg_ids[],
00374             int delete_old),
00375                int (*vm_msg_play)(struct ast_channel *chan,
00376             const char *mailbox,
00377             const char *context,
00378             const char *folder,
00379             const char *msg_num,
00380             ast_vm_msg_play_cb cb));
00381 
00382 
00383 void ast_uninstall_vm_functions(void);
00384 
00385 #ifdef TEST_FRAMEWORK
00386 void ast_install_vm_test_functions(int (*vm_test_destroy_user)(const char *context, const char *mailbox),
00387                int (*vm_test_create_user)(const char *context, const char *mailbox));
00388 
00389 void ast_uninstall_vm_test_functions(void);
00390 #endif
00391 
00392 /*!
00393  * \brief
00394  * param[in] vm_rec_data Contains data needed to make the recording.
00395  * retval 0 voicemail successfully created from recording.
00396  * retval -1 Failure
00397  */
00398 int ast_app_copy_recording_to_vm(struct ast_vm_recording_data *vm_rec_data);
00399 
00400 /*!
00401  * \brief Determine if a given mailbox has any voicemail
00402  * If folder is NULL, defaults to "INBOX".  If folder is "INBOX", includes the
00403  * number of messages in the "Urgent" folder.
00404  * \retval 1 Mailbox has voicemail
00405  * \retval 0 No new voicemail in specified mailbox
00406  * \retval -1 Failure
00407  * \since 1.0
00408  */
00409 int ast_app_has_voicemail(const char *mailbox, const char *folder);
00410 
00411 /*!
00412  * \brief Determine number of new/old messages in a mailbox
00413  * \since 1.0
00414  * \param[in] mailbox Mailbox specification in the format mbox[@context][&mbox2[@context2]][...]
00415  * \param[out] newmsgs Number of messages in the "INBOX" folder.  Includes number of messages in the "Urgent" folder, if any.
00416  * \param[out] oldmsgs Number of messages in the "Old" folder.
00417  * \retval 0 Success
00418  * \retval -1 Failure
00419  */
00420 int ast_app_inboxcount(const char *mailbox, int *newmsgs, int *oldmsgs);
00421 
00422 /*!
00423  * \brief Determine number of urgent/new/old messages in a mailbox
00424  * \param[in] mailbox the mailbox context to use
00425  * \param[out] urgentmsgs the urgent message count
00426  * \param[out] newmsgs the new message count
00427  * \param[out] oldmsgs the old message count
00428  * \return Returns 0 for success, negative upon error
00429  * \since 1.6.1
00430  */
00431 int ast_app_inboxcount2(const char *mailbox, int *urgentmsgs, int *newmsgs, int *oldmsgs);
00432 
00433 /*!
00434  * \brief Given a mailbox and context, play that mailbox owner's name to the channel specified
00435  * \param[in] chan Channel on which to play the name
00436  * \param[in] mailbox Mailbox number from which to retrieve the recording
00437  * \param[in] context Mailbox context from which to locate the mailbox number
00438  * \retval 0 Name played without interruption
00439  * \retval dtmf ASCII value of the DTMF which interrupted playback.
00440  * \retval -1 Unable to locate mailbox or hangup occurred.
00441  * \since 1.6.1
00442  */
00443 int ast_app_sayname(struct ast_channel *chan, const char *mailbox, const char *context);
00444 
00445 /*!
00446  * \brief Check number of messages in a given context, mailbox, and folder
00447  * \since 1.4
00448  * \param[in] context Mailbox context
00449  * \param[in] mailbox Mailbox number
00450  * \param[in] folder Mailbox folder
00451  * \return Number of messages in the given context, mailbox, and folder.  If folder is NULL, folder "INBOX" is assumed.  If folder is "INBOX", includes number of messages in the "Urgent" folder.
00452  */
00453 int ast_app_messagecount(const char *context, const char *mailbox, const char *folder);
00454 
00455 /*!
00456  * \brief Return name of folder, given an id
00457  * \param[in] id Folder id
00458  * \return Name of folder
00459  */
00460 const char *ast_vm_index_to_foldername(int id);
00461 
00462 /*
00463  * \brief Create a snapshot of a mailbox which contains information about every msg.
00464  *
00465  * \param mailbox, the mailbox to look for
00466  * \param context, the context to look for the mailbox in
00467  * \param folder, OPTIONAL.  When not NULL only msgs from the specified folder will be included.
00468  * \param desending, list the msgs in descending order rather than ascending order.
00469  * \param combine_INBOX_and_OLD, When this argument is set, The OLD folder will be represented
00470  *        in the INBOX folder of the snapshot. This allows the snapshot to represent the
00471  *        OLD and INBOX messages in sorted order merged together.
00472  *
00473  * \retval snapshot on success
00474  * \retval NULL on failure
00475  */
00476 struct ast_vm_mailbox_snapshot *ast_vm_mailbox_snapshot_create(const char *mailbox,
00477    const char *context,
00478    const char *folder,
00479    int descending,
00480    enum ast_vm_snapshot_sort_val sort_val,
00481    int combine_INBOX_and_OLD);
00482 
00483 /*
00484  * \brief destroy a snapshot
00485  *
00486  * \param mailbox_snapshot The snapshot to destroy.
00487  * \retval NULL
00488  */
00489 struct ast_vm_mailbox_snapshot *ast_vm_mailbox_snapshot_destroy(struct ast_vm_mailbox_snapshot *mailbox_snapshot);
00490 
00491 /*!
00492  * \brief Move messages from one folder to another
00493  *
00494  * \param mailbox The mailbox to which the folders belong
00495  * \param context The voicemail context for the mailbox
00496  * \param num_msgs The number of messages to move
00497  * \param oldfolder The folder from where messages should be moved
00498  * \param old_msg_nums The message IDs of the messages to move
00499  * \param newfolder The folder to which messages should be moved
00500  * new folder. This array must be num_msgs sized.
00501  *
00502  * \retval -1 Failure
00503  * \retval 0 Success
00504  */
00505 int ast_vm_msg_move(const char *mailbox,
00506    const char *context,
00507    size_t num_msgs,
00508    const char *oldfolder,
00509    const char *old_msg_ids[],
00510    const char *newfolder);
00511 
00512 /*!
00513  * \brief Remove/delete messages from a mailbox folder.
00514  *
00515  * \param mailbox The mailbox from which to delete messages
00516  * \param context The voicemail context for the mailbox
00517  * \param num_msgs The number of messages to delete
00518  * \param folder The folder from which to remove messages
00519  * \param msgs The message IDs of the messages to delete
00520  *
00521  * \retval -1 Failure
00522  * \retval 0 Success
00523  */
00524 int ast_vm_msg_remove(const char *mailbox,
00525    const char *context,
00526    size_t num_msgs,
00527    const char *folder,
00528    const char *msgs[]);
00529 
00530 /*!
00531  * \brief forward a message from one mailbox to another.
00532  *
00533  * \brief from_mailbox The original mailbox the message is being forwarded from
00534  * \brief from_context The voicemail context of the from_mailbox
00535  * \brief from_folder The folder from which the message is being forwarded
00536  * \brief to_mailbox The mailbox to forward the message to
00537  * \brief to_context The voicemail context of the to_mailbox
00538  * \brief to_folder The folder to which the message is being forwarded
00539  * \brief num_msgs The number of messages being forwarded
00540  * \brief msg_ids The message IDs of the messages in from_mailbox to forward
00541  * \brief delete_old If non-zero, the forwarded messages are also deleted from from_mailbox.
00542  * Otherwise, the messages will remain in the from_mailbox.
00543  *
00544  * \retval -1 Failure
00545  * \retval 0 Success
00546  */
00547 int ast_vm_msg_forward(const char *from_mailbox,
00548    const char *from_context,
00549    const char *from_folder,
00550    const char *to_mailbox,
00551    const char *to_context,
00552    const char *to_folder,
00553    size_t num_msgs,
00554    const char *msg_ids[],
00555    int delete_old);
00556 
00557 /*!
00558  * \brief Play a voicemail msg back on a channel.
00559  *
00560  * \param mailbox msg is in.
00561  * \param context of mailbox.
00562  * \param voicemail folder to look in.
00563  * \param message number in the voicemailbox to playback to the channel.
00564  *
00565  * \retval 0 success
00566  * \retval -1 failure
00567  */
00568 int ast_vm_msg_play(struct ast_channel *chan,
00569    const char *mailbox,
00570    const char *context,
00571    const char *folder,
00572    const char *msg_num,
00573    ast_vm_msg_play_cb cb);
00574 
00575 #ifdef TEST_FRAMEWORK
00576 int ast_vm_test_destroy_user(const char *context, const char *mailbox);
00577 int ast_vm_test_create_user(const char *context, const char *mailbox);
00578 #endif
00579 
00580 /*! \brief Safely spawn an external program while closing file descriptors
00581    \note This replaces the \b system call in all Asterisk modules
00582 */
00583 int ast_safe_system(const char *s);
00584 
00585 /*!
00586  * \brief Replace the SIGCHLD handler
00587  *
00588  * Normally, Asterisk has a SIGCHLD handler that is cleaning up all zombie
00589  * processes from forking elsewhere in Asterisk.  However, if you want to
00590  * wait*() on the process to retrieve information about it's exit status,
00591  * then this signal handler needs to be temporarily replaced.
00592  *
00593  * Code that executes this function *must* call ast_unreplace_sigchld()
00594  * after it is finished doing the wait*().
00595  */
00596 void ast_replace_sigchld(void);
00597 
00598 /*!
00599  * \brief Restore the SIGCHLD handler
00600  *
00601  * This function is called after a call to ast_replace_sigchld.  It restores
00602  * the SIGCHLD handler that cleans up any zombie processes.
00603  */
00604 void ast_unreplace_sigchld(void);
00605 
00606 /*!
00607   \brief Send DTMF to a channel
00608 
00609   \param chan    The channel that will receive the DTMF frames
00610   \param peer    (optional) Peer channel that will be autoserviced while the
00611                  primary channel is receiving DTMF
00612   \param digits  This is a string of characters representing the DTMF digits
00613                  to be sent to the channel.  Valid characters are
00614                  "0123456789*#abcdABCD".  Note: You can pass arguments 'f' or
00615                  'F', if you want to Flash the channel (if supported by the
00616                  channel), or 'w' to add a 500 millisecond pause to the DTMF
00617                  sequence.
00618   \param between This is the number of milliseconds to wait in between each
00619                  DTMF digit.  If zero milliseconds is specified, then the
00620                  default value of 100 will be used.
00621   \param duration This is the duration that each DTMF digit should have.
00622 */
00623 int ast_dtmf_stream(struct ast_channel *chan, struct ast_channel *peer, const char *digits, int between, unsigned int duration);
00624 
00625 /*! \brief Stream a filename (or file descriptor) as a generator. */
00626 int ast_linear_stream(struct ast_channel *chan, const char *filename, int fd, int allowoverride);
00627 
00628 /*!
00629  * \brief Stream a file with fast forward, pause, reverse, restart.
00630  * \param chan
00631  * \param file filename
00632  * \param fwd, rev, stop, pause, restart, skipms, offsetms
00633  *
00634  * Before calling this function, set this to be the number
00635  * of ms to start from the beginning of the file.  When the function
00636  * returns, it will be the number of ms from the beginning where the
00637  * playback stopped.  Pass NULL if you don't care.
00638  */
00639 int ast_control_streamfile(struct ast_channel *chan, const char *file, const char *fwd, const char *rev, const char *stop, const char *pause, const char *restart, int skipms, long *offsetms);
00640 
00641 /*!
00642  * \brief Stream a file with fast forward, pause, reverse, restart.
00643  * \param chan
00644  * \param file filename
00645  * \param fwd, rev, stop, pause, restart, skipms, offsetms
00646  * \param waitstream callback to invoke when fastforward or rewind occurrs.
00647  *
00648  * Before calling this function, set this to be the number
00649  * of ms to start from the beginning of the file.  When the function
00650  * returns, it will be the number of ms from the beginning where the
00651  * playback stopped.  Pass NULL if you don't care.
00652  */
00653 int ast_control_streamfile_w_cb(struct ast_channel *chan,
00654    const char *file,
00655    const char *fwd,
00656    const char *rev,
00657    const char *stop,
00658    const char *pause,
00659    const char *restart,
00660    int skipms,
00661    long *offsetms,
00662    ast_waitstream_fr_cb cb);
00663 
00664 /*! \brief Play a stream and wait for a digit, returning the digit that was pressed */
00665 int ast_play_and_wait(struct ast_channel *chan, const char *fn);
00666 
00667 /*!
00668  * \brief Record a file based on input from a channel
00669  *        This function will play "auth-thankyou" upon successful recording.
00670  *
00671  * \param chan the channel being recorded
00672  * \param playfile Filename of sound to play before recording begins
00673  * \param recordfile Filename to save the recording
00674  * \param maxtime_sec Longest possible message length in seconds
00675  * \param fmt string containing all formats to be recorded delimited by '|'
00676  * \param duration pointer to integer for storing length of the recording
00677  * \param sound_duration pointer to integer for storing length of the recording minus all silence
00678  * \param silencethreshold tolerance of noise levels that can be considered silence for the purpose of silence timeout, -1 for default
00679  * \param maxsilence_ms Length of time in milliseconds which will trigger a timeout from silence, -1 for default
00680  * \param path Optional filesystem path to unlock
00681  * \param acceptdtmf Character of DTMF to end and accept the recording
00682  * \param canceldtmf Character of DTMF to end and cancel the recording
00683  *
00684  * \retval -1 failure or hangup
00685  * \retval 'S' Recording ended from silence timeout
00686  * \retval 't' Recording ended from the message exceeding the maximum duration
00687  * \retval dtmfchar Recording ended via the return value's DTMF character for either cancel or accept.
00688  */
00689 int ast_play_and_record_full(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime_sec, const char *fmt, int *duration, int *sound_duration, int silencethreshold, int maxsilence_ms, const char *path, const char *acceptdtmf, const char *canceldtmf);
00690 
00691 /*!
00692  * \brief Record a file based on input from a channel. Use default accept and cancel DTMF.
00693  *        This function will play "auth-thankyou" upon successful recording.
00694  *
00695  * \param chan the channel being recorded
00696  * \param playfile Filename of sound to play before recording begins
00697  * \param recordfile Filename to save the recording
00698  * \param maxtime_sec Longest possible message length in seconds
00699  * \param fmt string containing all formats to be recorded delimited by '|'
00700  * \param duration pointer to integer for storing length of the recording
00701  * \param sound_duration pointer to integer for storing length of the recording minus all silence
00702  * \param silencethreshold tolerance of noise levels that can be considered silence for the purpose of silence timeout, -1 for default
00703  * \param maxsilence_ms length of time in milliseconds which will trigger a timeout from silence, -1 for default
00704  * \param path Optional filesystem path to unlock
00705  *
00706  * \retval -1 failure or hangup
00707  * \retval 'S' Recording ended from silence timeout
00708  * \retval 't' Recording ended from the message exceeding the maximum duration
00709  * \retval dtmfchar Recording ended via the return value's DTMF character for either cancel or accept.
00710  */
00711 int ast_play_and_record(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime_sec, const char *fmt, int *duration, int *sound_duration, int silencethreshold, int maxsilence_ms, const char *path);
00712 
00713 /*!
00714  * \brief Record a file based on input frm a channel. Recording is performed in 'prepend' mode which works a little differently from normal recordings
00715  *        This function will not play a success message due to post-recording control in the application this was added for.
00716  *
00717  * \param chan the channel being recorded
00718  * \param playfile Filename of sound to play before recording begins
00719  * \param recordfile Filename to save the recording
00720  * \param maxtime_sec Longest possible message length in seconds
00721  * \param fmt string containing all formats to be recorded delimited by '|'
00722  * \param duration pointer to integer for storing length of the recording
00723  * \param sound_duration pointer to integer for storing length of the recording minus all silence
00724  * \param beep whether to play a beep to prompt the recording
00725  * \param silencethreshold tolerance of noise levels that can be considered silence for the purpose of silence timeout, -1 for default
00726  * \param maxsilence_ms length of time in milliseconds which will trigger a timeout from silence, -1 for default.
00727  *
00728  * \retval -1 failure or hangup
00729  * \retval 'S' Recording ended from silence timeout
00730  * \retval 't' Recording either exceeded maximum duration or the call was ended via DTMF
00731  */
00732 int ast_play_and_prepend(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime_sec, char *fmt, int *duration, int *sound_duration, int beep, int silencethreshold, int maxsilence_ms);
00733 
00734 enum ast_getdata_result {
00735    AST_GETDATA_FAILED = -1,
00736    AST_GETDATA_COMPLETE = 0,
00737    AST_GETDATA_TIMEOUT = 1,
00738    AST_GETDATA_INTERRUPTED = 2,
00739    /*! indicates a user terminated empty string rather than an empty string resulting 
00740     * from a timeout or other factors */
00741    AST_GETDATA_EMPTY_END_TERMINATED = 3,
00742 };
00743 
00744 enum AST_LOCK_RESULT {
00745    AST_LOCK_SUCCESS = 0,
00746    AST_LOCK_TIMEOUT = -1,
00747    AST_LOCK_PATH_NOT_FOUND = -2,
00748    AST_LOCK_FAILURE = -3,
00749 };
00750 
00751 /*! \brief Type of locking to use in ast_lock_path / ast_unlock_path */
00752 enum AST_LOCK_TYPE {
00753    AST_LOCK_TYPE_LOCKFILE = 0,
00754    AST_LOCK_TYPE_FLOCK = 1,
00755 };
00756 
00757 /*!
00758  * \brief Set the type of locks used by ast_lock_path()
00759  * \param type the locking type to use
00760  */
00761 void ast_set_lock_type(enum AST_LOCK_TYPE type);
00762 
00763 /*!
00764  * \brief Lock a filesystem path.
00765  * \param path the path to be locked
00766  * \return one of \ref AST_LOCK_RESULT values
00767  */
00768 enum AST_LOCK_RESULT ast_lock_path(const char *path);
00769 
00770 /*! \brief Unlock a path */
00771 int ast_unlock_path(const char *path);
00772 
00773 /*! \brief Read a file into asterisk*/
00774 char *ast_read_textfile(const char *file);
00775 
00776 struct ast_group_info;
00777 
00778 /*! \brief Split a group string into group and category, returning a default category if none is provided. */
00779 int ast_app_group_split_group(const char *data, char *group, int group_max, char *category, int category_max);
00780 
00781 /*! \brief Set the group for a channel, splitting the provided data into group and category, if specified. */
00782 int ast_app_group_set_channel(struct ast_channel *chan, const char *data);
00783 
00784 /*! \brief Get the current channel count of the specified group and category. */
00785 int ast_app_group_get_count(const char *group, const char *category);
00786 
00787 /*! \brief Get the current channel count of all groups that match the specified pattern and category. */
00788 int ast_app_group_match_get_count(const char *groupmatch, const char *category);
00789 
00790 /*! \brief Discard all group counting for a channel */
00791 int ast_app_group_discard(struct ast_channel *chan);
00792 
00793 /*! \brief Update all group counting for a channel to a new one */
00794 int ast_app_group_update(struct ast_channel *oldchan, struct ast_channel *newchan);
00795 
00796 /*! \brief Write Lock the group count list */
00797 int ast_app_group_list_wrlock(void);
00798 
00799 /*! \brief Read Lock the group count list */
00800 int ast_app_group_list_rdlock(void);
00801 
00802 /*! \brief Get the head of the group count list */
00803 struct ast_group_info *ast_app_group_list_head(void);
00804 
00805 /*! \brief Unlock the group count list */
00806 int ast_app_group_list_unlock(void);
00807 
00808 /*!
00809   \brief Define an application argument
00810   \param name The name of the argument
00811 */
00812 #define AST_APP_ARG(name) char *name
00813 
00814 /*!
00815   \brief Declare a structure to hold an application's arguments.
00816   \param name The name of the structure
00817   \param arglist The list of arguments, defined using AST_APP_ARG
00818 
00819   This macro declares a structure intended to be used in a call
00820   to ast_app_separate_args(). The structure includes all the
00821   arguments specified, plus an argv array that overlays them and an
00822   argc argument counter. The arguments must be declared using AST_APP_ARG,
00823   and they will all be character pointers (strings).
00824 
00825   \note The structure is <b>not</b> initialized, as the call to
00826   ast_app_separate_args() will perform that function before parsing
00827   the arguments.
00828  */
00829 #define AST_DECLARE_APP_ARGS(name, arglist) AST_DEFINE_APP_ARGS_TYPE(, arglist) name = { 0, }
00830 
00831 /*!
00832   \brief Define a structure type to hold an application's arguments.
00833   \param type The name of the structure type
00834   \param arglist The list of arguments, defined using AST_APP_ARG
00835 
00836   This macro defines a structure type intended to be used in a call
00837   to ast_app_separate_args(). The structure includes all the
00838   arguments specified, plus an argv array that overlays them and an
00839   argc argument counter. The arguments must be declared using AST_APP_ARG,
00840   and they will all be character pointers (strings).
00841 
00842   \note This defines a structure type, but does not declare an instance
00843   of the structure. That must be done separately.
00844  */
00845 #define AST_DEFINE_APP_ARGS_TYPE(type, arglist) \
00846    struct type { \
00847       unsigned int argc; \
00848       char *argv[0]; \
00849       arglist \
00850    }
00851 
00852 /*!
00853   \brief Performs the 'standard' argument separation process for an application.
00854   \param args An argument structure defined using AST_DECLARE_APP_ARGS
00855   \param parse A modifiable buffer containing the input to be parsed
00856 
00857   This function will separate the input string using the standard argument
00858   separator character ',' and fill in the provided structure, including
00859   the argc argument counter field.
00860  */
00861 #define AST_STANDARD_APP_ARGS(args, parse) \
00862    args.argc = __ast_app_separate_args(parse, ',', 1, args.argv, ((sizeof(args) - offsetof(typeof(args), argv)) / sizeof(args.argv[0])))
00863 #define AST_STANDARD_RAW_ARGS(args, parse) \
00864    args.argc = __ast_app_separate_args(parse, ',', 0, args.argv, ((sizeof(args) - offsetof(typeof(args), argv)) / sizeof(args.argv[0])))
00865 
00866 /*!
00867   \brief Performs the 'nonstandard' argument separation process for an application.
00868   \param args An argument structure defined using AST_DECLARE_APP_ARGS
00869   \param parse A modifiable buffer containing the input to be parsed
00870   \param sep A nonstandard separator character
00871 
00872   This function will separate the input string using the nonstandard argument
00873   separator character and fill in the provided structure, including
00874   the argc argument counter field.
00875  */
00876 #define AST_NONSTANDARD_APP_ARGS(args, parse, sep) \
00877    args.argc = __ast_app_separate_args(parse, sep, 1, args.argv, ((sizeof(args) - offsetof(typeof(args), argv)) / sizeof(args.argv[0])))
00878 #define AST_NONSTANDARD_RAW_ARGS(args, parse, sep) \
00879    args.argc = __ast_app_separate_args(parse, sep, 0, args.argv, ((sizeof(args) - offsetof(typeof(args), argv)) / sizeof(args.argv[0])))
00880 
00881 /*!
00882   \brief Separate a string into arguments in an array
00883   \param buf The string to be parsed (this must be a writable copy, as it will be modified)
00884   \param delim The character to be used to delimit arguments
00885   \param remove_chars Remove backslashes and quote characters, while parsing
00886   \param array An array of 'char *' to be filled in with pointers to the found arguments
00887   \param arraylen The number of elements in the array (i.e. the number of arguments you will accept)
00888 
00889   Note: if there are more arguments in the string than the array will hold, the last element of
00890   the array will contain the remaining arguments, not separated.
00891 
00892   The array will be completely zeroed by this function before it populates any entries.
00893 
00894   \return The number of arguments found, or zero if the function arguments are not valid.
00895 */
00896 unsigned int __ast_app_separate_args(char *buf, char delim, int remove_chars, char **array, int arraylen);
00897 #define ast_app_separate_args(a,b,c,d) __ast_app_separate_args(a,b,1,c,d)
00898 
00899 /*!
00900   \brief A structure to hold the description of an application 'option'.
00901 
00902   Application 'options' are single-character flags that can be supplied
00903   to the application to affect its behavior; they can also optionally
00904   accept arguments enclosed in parenthesis.
00905 
00906   These structures are used by the ast_app_parse_options function, uses
00907   this data to fill in a flags structure (to indicate which options were
00908   supplied) and array of argument pointers (for those options that had
00909   arguments supplied).
00910  */
00911 struct ast_app_option {
00912    /*! \brief The flag bit that represents this option. */
00913    uint64_t flag;
00914    /*! \brief The index of the entry in the arguments array
00915      that should be used for this option's argument. */
00916    unsigned int arg_index;
00917 };
00918 
00919 #define BEGIN_OPTIONS {
00920 #define END_OPTIONS }
00921 
00922 /*!
00923   \brief Declares an array of options for an application.
00924   \param holder The name of the array to be created
00925   \param options The actual options to be placed into the array
00926   \sa ast_app_parse_options
00927 
00928   This macro declares a 'static const' array of \c struct \c ast_option
00929   elements to hold the list of available options for an application.
00930   Each option must be declared using either the AST_APP_OPTION()
00931   or AST_APP_OPTION_ARG() macros.
00932 
00933   Example usage:
00934   \code
00935   enum my_app_option_flags {
00936         OPT_JUMP = (1 << 0),
00937         OPT_BLAH = (1 << 1),
00938         OPT_BLORT = (1 << 2),
00939   };
00940 
00941   enum my_app_option_args {
00942         OPT_ARG_BLAH = 0,
00943         OPT_ARG_BLORT,
00944         !! this entry tells how many possible arguments there are,
00945            and must be the last entry in the list
00946         OPT_ARG_ARRAY_SIZE,
00947   };
00948 
00949   AST_APP_OPTIONS(my_app_options, {
00950         AST_APP_OPTION('j', OPT_JUMP),
00951         AST_APP_OPTION_ARG('b', OPT_BLAH, OPT_ARG_BLAH),
00952         AST_APP_OPTION_BLORT('B', OPT_BLORT, OPT_ARG_BLORT),
00953   });
00954 
00955   static int my_app_exec(struct ast_channel *chan, void *data)
00956   {
00957    char *options;
00958    struct ast_flags opts = { 0, };
00959    char *opt_args[OPT_ARG_ARRAY_SIZE];
00960 
00961    ... do any argument parsing here ...
00962 
00963    if (ast_app_parse_options(my_app_options, &opts, opt_args, options)) {
00964       return -1;
00965    }
00966   }
00967   \endcode
00968  */
00969 #define AST_APP_OPTIONS(holder, options...) \
00970    static const struct ast_app_option holder[128] = options
00971 
00972 /*!
00973   \brief Declares an application option that does not accept an argument.
00974   \param option The single character representing the option
00975   \param flagno The flag index to be set if this option is present
00976   \sa AST_APP_OPTIONS, ast_app_parse_options
00977  */
00978 #define AST_APP_OPTION(option, flagno) \
00979    [option] = { .flag = flagno }
00980 
00981 /*!
00982   \brief Declares an application option that accepts an argument.
00983   \param option The single character representing the option
00984   \param flagno The flag index to be set if this option is present
00985   \param argno The index into the argument array where the argument should
00986   be placed
00987   \sa AST_APP_OPTIONS, ast_app_parse_options
00988  */
00989 #define AST_APP_OPTION_ARG(option, flagno, argno) \
00990    [option] = { .flag = flagno, .arg_index = argno + 1 }
00991 
00992 /*!
00993   \brief Parses a string containing application options and sets flags/arguments.
00994   \param options The array of possible options declared with AST_APP_OPTIONS
00995   \param flags The flag structure to have option flags set
00996   \param args The array of argument pointers to hold arguments found
00997   \param optstr The string containing the options to be parsed
00998   \return zero for success, non-zero if an error occurs
00999   \sa AST_APP_OPTIONS
01000  */
01001 int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr);
01002 
01003    /*!
01004   \brief Parses a string containing application options and sets flags/arguments.
01005   \param options The array of possible options declared with AST_APP_OPTIONS
01006   \param flags The 64-bit flag structure to have option flags set
01007   \param args The array of argument pointers to hold arguments found
01008   \param optstr The string containing the options to be parsed
01009   \return zero for success, non-zero if an error occurs
01010   \sa AST_APP_OPTIONS
01011  */
01012 int ast_app_parse_options64(const struct ast_app_option *options, struct ast_flags64 *flags, char **args, char *optstr);
01013 
01014 /*! \brief Given a list of options array, return an option string based on passed flags
01015    \param options The array of possible options declared with AST_APP_OPTIONS
01016    \param flags The flags of the options that you wish to populate the buffer with
01017    \param buf The buffer to fill with the string of options
01018    \param len The maximum length of buf
01019 */
01020 void ast_app_options2str64(const struct ast_app_option *options, struct ast_flags64 *flags, char *buf, size_t len);
01021 
01022 /*! \brief Present a dialtone and collect a certain length extension.
01023     \return Returns 1 on valid extension entered, -1 on hangup, or 0 on invalid extension.
01024 \note Note that if 'collect' holds digits already, new digits will be appended, so be sure it's initialized properly */
01025 int ast_app_dtget(struct ast_channel *chan, const char *context, char *collect, size_t size, int maxlen, int timeout);
01026 
01027 /*! \brief Allow to record message and have a review option */
01028 int ast_record_review(struct ast_channel *chan, const char *playfile, const char *recordfile, int maxtime, const char *fmt, int *duration, const char *path);
01029 
01030 /*!\brief Decode an encoded control or extended ASCII character 
01031  * \param[in] stream String to decode
01032  * \param[out] result Decoded character
01033  * \param[out] consumed Number of characters used in stream to encode the character
01034  * \retval -1 Stream is of zero length
01035  * \retval 0 Success
01036  */
01037 int ast_get_encoded_char(const char *stream, char *result, size_t *consumed);
01038 
01039 /*!\brief Decode a stream of encoded control or extended ASCII characters
01040  * \param[in] stream Encoded string
01041  * \param[out] result Decoded string
01042  * \param[in] result_len Maximum size of the result buffer
01043  * \return A pointer to the result string
01044  */
01045 char *ast_get_encoded_str(const char *stream, char *result, size_t result_len);
01046 
01047 /*! \brief Decode a stream of encoded control or extended ASCII characters */
01048 int ast_str_get_encoded_str(struct ast_str **str, int maxlen, const char *stream);
01049 
01050 /*!
01051  * \brief Common routine for child processes, to close all fds prior to exec(2)
01052  * \param[in] n starting file descriptor number for closing all higher file descriptors
01053  * \since 1.6.1
01054  */
01055 void ast_close_fds_above_n(int n);
01056 
01057 /*!
01058  * \brief Common routine to safely fork without a chance of a signal handler firing badly in the child
01059  * \param[in] stop_reaper flag to determine if sigchld handler is replaced or not
01060  * \since 1.6.1
01061  */
01062 int ast_safe_fork(int stop_reaper);
01063 
01064 /*!
01065  * \brief Common routine to cleanup after fork'ed process is complete (if reaping was stopped)
01066  * \since 1.6.1
01067  */
01068 void ast_safe_fork_cleanup(void);
01069 
01070 /*!
01071  * \brief Common routine to parse time lengths, with optional time unit specifier
01072  * \param[in] timestr String to parse
01073  * \param[in] defunit Default unit type
01074  * \param[out] result Resulting value, specified in milliseconds
01075  * \retval 0 Success
01076  * \retval -1 Failure
01077  * \since 1.8
01078  */
01079 int ast_app_parse_timelen(const char *timestr, int *result, enum ast_timelen defunit);
01080 
01081 #if defined(__cplusplus) || defined(c_plusplus)
01082 }
01083 #endif
01084 
01085 #endif /* _ASTERISK_APP_H */