2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
20 * \brief Core PBX routines and definitions.
23 #ifndef _ASTERISK_PBX_H
24 #define _ASTERISK_PBX_H
26 #include "asterisk/sched.h"
27 #include "asterisk/channel.h"
28 #include "asterisk/linkedlists.h"
30 #if defined(__cplusplus) || defined(c_plusplus)
34 #define AST_MAX_APP 32 /*!< Max length of an application */
36 #define AST_PBX_KEEP 0
37 #define AST_PBX_REPLACE 1
39 /*! \brief Special return values from applications to the PBX { */
40 #define AST_PBX_KEEPALIVE 10 /*!< Destroy the thread, but don't hang up the channel */
41 #define AST_PBX_NO_HANGUP_PEER 11
44 #define PRIORITY_HINT -1 /*!< Special Priority for a hint */
46 /*! \brief Extension states */
47 enum ast_extension_states {
48 AST_EXTENSION_REMOVED = -2, /*!< Extension removed */
49 AST_EXTENSION_DEACTIVATED = -1, /*!< Extension hint removed */
50 AST_EXTENSION_NOT_INUSE = 0, /*!< No device INUSE or BUSY */
51 AST_EXTENSION_INUSE = 1 << 0, /*!< One or more devices INUSE */
52 AST_EXTENSION_BUSY = 1 << 1, /*!< All devices BUSY */
53 AST_EXTENSION_UNAVAILABLE = 1 << 2, /*!< All devices UNAVAILABLE/UNREGISTERED */
54 AST_EXTENSION_RINGING = 1 << 3, /*!< All devices RINGING */
55 AST_EXTENSION_ONHOLD = 1 << 4, /*!< All devices ONHOLD */
65 /*! \brief Typedef for devicestate and hint callbacks */
66 typedef int (*ast_state_cb_type)(char *context, char* id, enum ast_extension_states state, void *data);
68 /*! \brief Data structure associated with a custom dialplan function */
69 struct ast_custom_function {
70 const char *name; /*!< Name */
71 const char *synopsis; /*!< Short description for "show functions" */
72 const char *desc; /*!< Help text that explains it all */
73 const char *syntax; /*!< Syntax description */
74 int (*read)(struct ast_channel *, char *, char *, char *, size_t); /*!< Read function, if read is supported */
75 int (*write)(struct ast_channel *, char *, char *, const char *); /*!< Write function, if write is supported */
76 AST_LIST_ENTRY(ast_custom_function) acflist;
79 /*! \brief All switch functions have the same interface, so define a type for them */
80 typedef int (ast_switch_f)(struct ast_channel *chan, const char *context,
81 const char *exten, int priority, const char *callerid, const char *data);
83 /*!< Data structure associated with an Asterisk switch */
85 AST_LIST_ENTRY(ast_switch) list;
86 const char *name; /*!< Name of the switch */
87 const char *description; /*!< Description of the switch */
90 ast_switch_f *canmatch;
92 ast_switch_f *matchmore;
96 int hastime; /*!< If time construct exists */
97 unsigned int monthmask; /*!< Mask for month */
98 unsigned int daymask; /*!< Mask for date */
99 unsigned int dowmask; /*!< Mask for day of week (mon-sun) */
100 unsigned int minmask[24]; /*!< Mask for minute */
103 int ast_build_timing(struct ast_timing *i, const char *info);
104 int ast_check_timing(const struct ast_timing *i);
107 int dtimeout; /*!< Timeout between digits (seconds) */
108 int rtimeout; /*!< Timeout for response (seconds) */
113 * \brief Register an alternative dialplan switch
115 * \param sw switch to register
117 * This function registers a populated ast_switch structure with the
118 * asterisk switching architecture.
120 * \return 0 on success, and other than 0 on failure
122 int ast_register_switch(struct ast_switch *sw);
125 * \brief Unregister an alternative switch
127 * \param sw switch to unregister
129 * Unregisters a switch from asterisk.
133 void ast_unregister_switch(struct ast_switch *sw);
136 * \brief Look up an application
138 * \param app name of the app
140 * This function searches for the ast_app structure within
141 * the apps that are registered for the one with the name
144 * \return the ast_app structure that matches on success, or NULL on failure
146 struct ast_app *pbx_findapp(const char *app);
149 * \brief Execute an application
151 * \param c channel to execute on
152 * \param app which app to execute
153 * \param data the data passed into the app
155 * This application executes an application on a given channel. It
156 * saves the stack and executes the given appliation passing in
159 * \return 0 on success, and -1 on failure
161 int pbx_exec(struct ast_channel *c, struct ast_app *app, void *data);
164 * \brief Register a new context
166 * \param extcontexts pointer to the ast_context structure pointer
167 * \param name name of the new context
168 * \param registrar registrar of the context
170 * This will first search for a context with your name. If it exists already, it will not
171 * create a new one. If it does not exist, it will create a new one with the given name
174 * \return NULL on failure, and an ast_context structure on success
176 struct ast_context *ast_context_create(struct ast_context **extcontexts, const char *name, const char *registrar);
179 * \brief Merge the temporary contexts into a global contexts list and delete from the
180 * global list the ones that are being added
182 * \param extcontexts pointer to the ast_context structure pointer
183 * \param registrar of the context; if it's set the routine will delete all contexts
184 * that belong to that registrar; if NULL only the contexts that are specified
187 void ast_merge_contexts_and_delete(struct ast_context **extcontexts, const char *registrar);
190 * \brief Destroy a context (matches the specified context (or ANY context if NULL)
192 * \param con context to destroy
193 * \param registrar who registered it
195 * You can optionally leave out either parameter. It will find it
196 * based on either the ast_context or the registrar name.
200 void ast_context_destroy(struct ast_context *con, const char *registrar);
203 * \brief Find a context
205 * \param name name of the context to find
207 * Will search for the context with the given name.
209 * \return the ast_context on success, NULL on failure.
211 struct ast_context *ast_context_find(const char *name);
213 enum ast_pbx_result {
216 AST_PBX_CALL_LIMIT = -2,
220 * \brief Create a new thread and start the PBX
222 * \param c channel to start the pbx on
224 * See ast_pbx_run for a synchronous function to run the PBX in the
225 * current thread, as opposed to starting a new one.
227 * \return Zero on success, non-zero on failure
229 enum ast_pbx_result ast_pbx_start(struct ast_channel *c);
232 * \brief Execute the PBX in the current thread
234 * \param c channel to run the pbx on
236 * This executes the PBX on a given channel. It allocates a new
237 * PBX structure for the channel, and provides all PBX functionality.
238 * See ast_pbx_start for an asynchronous function to run the PBX in a
239 * new thread as opposed to the current one.
241 * \return Zero on success, non-zero on failure
243 enum ast_pbx_result ast_pbx_run(struct ast_channel *c);
246 * \brief Add and extension to an extension context.
248 * \param context context to add the extension to
250 * \param extension extension to add
251 * \param priority priority level of extension addition
252 * \param label extension label
253 * \param callerid pattern to match CallerID, or NULL to match any CallerID
254 * \param application application to run on the extension with that priority level
255 * \param data data to pass to the application
257 * \param registrar who registered the extension
262 int ast_add_extension(const char *context, int replace, const char *extension,
263 int priority, const char *label, const char *callerid,
264 const char *application, void *data, void (*datad)(void *), const char *registrar);
267 * \brief Add an extension to an extension context, this time with an ast_context *.
269 * \note For details about the arguments, check ast_add_extension()
271 int ast_add_extension2(struct ast_context *con, int replace, const char *extension,
272 int priority, const char *label, const char *callerid,
273 const char *application, void *data, void (*datad)(void *), const char *registrar);
277 * \brief Register an application.
279 * \param app Short name of the application
280 * \param execute a function callback to execute the application. It should return
281 * non-zero if the channel needs to be hung up.
282 * \param synopsis a short description (one line synopsis) of the application
283 * \param description long description with all of the details about the use of
286 * This registers an application with Asterisk's internal application list.
287 * \note The individual applications themselves are responsible for registering and unregistering
288 * and unregistering their own CLI commands.
291 * \retval -1 failure.
293 int ast_register_application(const char *app, int (*execute)(struct ast_channel *, void *),
294 const char *synopsis, const char *description);
297 * \brief Unregister an application
299 * \param app name of the application (does not have to be the same string as the one that was registered)
301 * This unregisters an application from Asterisk's internal application list.
306 int ast_unregister_application(const char *app);
309 * \brief Uses hint and devicestate callback to get the state of an extension
311 * \param c this is not important
312 * \param context which context to look in
313 * \param exten which extension to get state
315 * \return extension state as defined in the ast_extension_states enum
317 int ast_extension_state(struct ast_channel *c, const char *context, const char *exten);
320 * \brief Return string representation of the state of an extension
322 * \param extension_state is the numerical state delivered by ast_extension_state
324 * \return the state of an extension as string
326 const char *ast_extension_state2str(int extension_state);
329 * \brief Registers a state change callback
331 * \param context which context to look in
332 * \param exten which extension to get state
333 * \param callback callback to call if state changed
334 * \param data to pass to callback
336 * The callback is called if the state of an extension is changed.
338 * \retval -1 on failure
339 * \retval ID on success
341 int ast_extension_state_add(const char *context, const char *exten,
342 ast_state_cb_type callback, void *data);
345 * \brief Deletes a registered state change callback by ID
347 * \param id of the callback to delete
348 * \param callback callback
350 * Removes the callback from list of callbacks
355 int ast_extension_state_del(int id, ast_state_cb_type callback);
358 * \brief If an extension exists, return non-zero
360 * \param hint buffer for hint
361 * \param maxlen size of hint buffer
362 * \param name buffer for name portion of hint
363 * \param maxnamelen size of name buffer
364 * \param c this is not important
365 * \param context which context to look in
366 * \param exten which extension to search for
368 * \return If an extension within the given context with the priority PRIORITY_HINT
369 * is found a non zero value will be returned.
370 * Otherwise, 0 is returned.
372 int ast_get_hint(char *hint, int maxlen, char *name, int maxnamelen,
373 struct ast_channel *c, const char *context, const char *exten);
376 * \brief Determine whether an extension exists
378 * \param c this is not important
379 * \param context which context to look in
380 * \param exten which extension to search for
381 * \param priority priority of the action within the extension
382 * \param callerid callerid to search for
384 * \return If an extension within the given context(or callerid) with the given priority
385 * is found a non zero value will be returned. Otherwise, 0 is returned.
387 int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten,
388 int priority, const char *callerid);
391 * \brief Find the priority of an extension that has the specified label
393 * \param c this is not important
394 * \param context which context to look in
395 * \param exten which extension to search for
396 * \param label label of the action within the extension to match to priority
397 * \param callerid callerid to search for
399 * \return the priority which matches the given label in the extension or -1 if not found.
401 int ast_findlabel_extension(struct ast_channel *c, const char *context,
402 const char *exten, const char *label, const char *callerid);
405 * \brief Find the priority of an extension that has the specified label
407 * \note This function is the same as ast_findlabel_extension, except that it accepts
408 * a pointer to an ast_context structure to specify the context instead of the
409 * name of the context. Otherwise, the functions behave the same.
411 int ast_findlabel_extension2(struct ast_channel *c, struct ast_context *con,
412 const char *exten, const char *label, const char *callerid);
415 * \brief Looks for a valid matching extension
417 * \param c not really important
418 * \param context context to serach within
419 * \param exten extension to check
420 * \param priority priority of extension path
421 * \param callerid callerid of extension being searched for
423 * \return If "exten" *could be* a valid extension in this context with or without
424 * some more digits, return non-zero. Basically, when this returns 0, no matter
425 * what you add to exten, it's not going to be a valid extension anymore
427 int ast_canmatch_extension(struct ast_channel *c, const char *context,
428 const char *exten, int priority, const char *callerid);
431 * \brief Looks to see if adding anything to this extension might match something. (exists ^ canmatch)
433 * \param c not really important XXX
434 * \param context context to serach within
435 * \param exten extension to check
436 * \param priority priority of extension path
437 * \param callerid callerid of extension being searched for
439 * \return If "exten" *could match* a valid extension in this context with
440 * some more digits, return non-zero. Does NOT return non-zero if this is
441 * an exact-match only. Basically, when this returns 0, no matter
442 * what you add to exten, it's not going to be a valid extension anymore
444 int ast_matchmore_extension(struct ast_channel *c, const char *context,
445 const char *exten, int priority, const char *callerid);
448 * \brief Determine if a given extension matches a given pattern (in NXX format)
450 * \param pattern pattern to match
451 * \param extension extension to check against the pattern.
453 * Checks whether or not the given extension matches the given pattern.
456 * \retval 0 on failure
458 int ast_extension_match(const char *pattern, const char *extension);
460 int ast_extension_close(const char *pattern, const char *data, int needmore);
463 * \brief Launch a new extension (i.e. new stack)
465 * \param c not important
466 * \param context which context to generate the extension within
467 * \param exten new extension to add
468 * \param priority priority of new extension
469 * \param callerid callerid of extension
471 * This adds a new extension to the asterisk extension list.
473 * \retval 0 on success
474 * \retval -1 on failure.
476 int ast_spawn_extension(struct ast_channel *c, const char *context,
477 const char *exten, int priority, const char *callerid);
480 * \brief Add a context include
482 * \param context context to add include to
483 * \param include new include to add
484 * \param registrar who's registering it
486 * Adds an include taking a char * string as the context parameter
488 * \retval 0 on success
489 * \retval -1 on error
491 int ast_context_add_include(const char *context, const char *include,
492 const char *registrar);
495 * \brief Add a context include
497 * \param con context to add the include to
498 * \param include include to add
499 * \param registrar who registered the context
501 * Adds an include taking a struct ast_context as the first parameter
503 * \retval 0 on success
504 * \retval -1 on failure
506 int ast_context_add_include2(struct ast_context *con, const char *include,
507 const char *registrar);
510 * \brief Remove a context include
512 * \note See ast_context_add_include for information on arguments
514 * \retval 0 on success
515 * \retval -1 on failure
517 int ast_context_remove_include(const char *context, const char *include,
518 const char *registrar);
521 * \brief Removes an include by an ast_context structure
523 * \note See ast_context_add_include2 for information on arguments
525 * \retval 0 on success
526 * \retval -1 on success
528 int ast_context_remove_include2(struct ast_context *con, const char *include,
529 const char *registrar);
532 * \brief Verifies includes in an ast_contect structure
534 * \param con context in which to verify the includes
536 * \retval 0 if no problems found
537 * \retval -1 if there were any missing context
539 int ast_context_verify_includes(struct ast_context *con);
542 * \brief Add a switch
544 * \param context context to which to add the switch
545 * \param sw switch to add
546 * \param data data to pass to switch
547 * \param eval whether to evaluate variables when running switch
548 * \param registrar whoever registered the switch
550 * This function registers a switch with the asterisk switch architecture
552 * \retval 0 on success
553 * \retval -1 on failure
555 int ast_context_add_switch(const char *context, const char *sw, const char *data,
556 int eval, const char *registrar);
559 * \brief Adds a switch (first param is a ast_context)
561 * \note See ast_context_add_switch() for argument information, with the exception of
562 * the first argument. In this case, it's a pointer to an ast_context structure
563 * as opposed to the name.
565 int ast_context_add_switch2(struct ast_context *con, const char *sw, const char *data,
566 int eval, const char *registrar);
569 * \brief Remove a switch
571 * Removes a switch with the given parameters
573 * \retval 0 on success
574 * \retval -1 on failure
576 int ast_context_remove_switch(const char *context, const char *sw,
577 const char *data, const char *registrar);
579 int ast_context_remove_switch2(struct ast_context *con, const char *sw,
580 const char *data, const char *registrar);
583 * \brief Simply remove extension from context
585 * \param context context to remove extension from
586 * \param extension which extension to remove
587 * \param priority priority of extension to remove
588 * \param registrar registrar of the extension
590 * This function removes an extension from a given context.
592 * \retval 0 on success
593 * \retval -1 on failure
595 int ast_context_remove_extension(const char *context, const char *extension, int priority,
596 const char *registrar);
598 int ast_context_remove_extension2(struct ast_context *con, const char *extension,
599 int priority, const char *registrar);
602 * \brief Add an ignorepat
604 * \param context which context to add the ignorpattern to
605 * \param ignorepat ignorepattern to set up for the extension
606 * \param registrar registrar of the ignore pattern
608 * Adds an ignore pattern to a particular context.
610 * \retval 0 on success
611 * \retval -1 on failure
613 int ast_context_add_ignorepat(const char *context, const char *ignorepat, const char *registrar);
615 int ast_context_add_ignorepat2(struct ast_context *con, const char *ignorepat, const char *registrar);
618 * \brief Remove an ignorepat
620 * \param context context from which to remove the pattern
621 * \param ignorepat the pattern to remove
622 * \param registrar the registrar of the ignore pattern
624 * This removes the given ignorepattern
626 * \retval 0 on success
627 * \retval -1 on failure
629 int ast_context_remove_ignorepat(const char *context, const char *ignorepat, const char *registrar);
631 int ast_context_remove_ignorepat2(struct ast_context *con, const char *ignorepat, const char *registrar);
634 * \brief Checks to see if a number should be ignored
636 * \param context context to search within
637 * \param pattern to check whether it should be ignored or not
639 * Check if a number should be ignored with respect to dialtone cancellation.
641 * \retval 0 if the pattern should not be ignored
642 * \retval non-zero if the pattern should be ignored
644 int ast_ignore_pattern(const char *context, const char *pattern);
646 /* Locking functions for outer modules, especially for completion functions */
649 * \brief Locks the context list
651 * \retval 0 on success
652 * \retval -1 on error
654 int ast_lock_contexts(void);
657 * \brief Unlocks contexts
659 * \retval 0 on success
660 * \retval -1 on failure
662 int ast_unlock_contexts(void);
665 * \brief Locks a given context
667 * \param con context to lock
669 * \retval 0 on success
670 * \retval -1 on failure
672 int ast_lock_context(struct ast_context *con);
675 * \retval Unlocks the given context
677 * \param con context to unlock
679 * \retval 0 on success
680 * \retval -1 on failure
682 int ast_unlock_context(struct ast_context *con);
685 int ast_async_goto(struct ast_channel *chan, const char *context, const char *exten, int priority);
687 int ast_async_goto_by_name(const char *chan, const char *context, const char *exten, int priority);
689 /*! Synchronously or asynchronously make an outbound call and send it to a
690 particular extension */
691 int ast_pbx_outgoing_exten(const char *type, int format, void *data, int timeout, const char *context, const char *exten, int priority, int *reason, int sync, const char *cid_num, const char *cid_name, struct ast_variable *vars, const char *account, struct ast_channel **locked_channel);
693 /*! Synchronously or asynchronously make an outbound call and send it to a
694 particular application with given extension */
695 int ast_pbx_outgoing_app(const char *type, int format, void *data, int timeout, const char *app, const char *appdata, int *reason, int sync, const char *cid_num, const char *cid_name, struct ast_variable *vars, const char *account, struct ast_channel **locked_channel);
698 * \brief Evaluate a condition
700 * \retval 0 if the condition is NULL or of zero length
701 * \retval int If the string is an integer, the integer representation of
702 * the integer is returned
703 * \retval 1 Any other non-empty string
705 int pbx_checkcondition(const char *condition);
707 /* Functions for returning values from structures */
708 const char *ast_get_context_name(struct ast_context *con);
709 const char *ast_get_extension_name(struct ast_exten *exten);
710 struct ast_context *ast_get_extension_context(struct ast_exten *exten);
711 const char *ast_get_include_name(struct ast_include *include);
712 const char *ast_get_ignorepat_name(struct ast_ignorepat *ip);
713 const char *ast_get_switch_name(struct ast_sw *sw);
714 const char *ast_get_switch_data(struct ast_sw *sw);
716 /* Other extension stuff */
717 int ast_get_extension_priority(struct ast_exten *exten);
718 int ast_get_extension_matchcid(struct ast_exten *e);
719 const char *ast_get_extension_cidmatch(struct ast_exten *e);
720 const char *ast_get_extension_app(struct ast_exten *e);
721 const char *ast_get_extension_label(struct ast_exten *e);
722 void *ast_get_extension_app_data(struct ast_exten *e);
724 /* Registrar info functions ... */
725 const char *ast_get_context_registrar(struct ast_context *c);
726 const char *ast_get_extension_registrar(struct ast_exten *e);
727 const char *ast_get_include_registrar(struct ast_include *i);
728 const char *ast_get_ignorepat_registrar(struct ast_ignorepat *ip);
729 const char *ast_get_switch_registrar(struct ast_sw *sw);
731 /* Walking functions ... */
732 struct ast_context *ast_walk_contexts(struct ast_context *con);
733 struct ast_exten *ast_walk_context_extensions(struct ast_context *con,
734 struct ast_exten *priority);
735 struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten,
736 struct ast_exten *priority);
737 struct ast_include *ast_walk_context_includes(struct ast_context *con,
738 struct ast_include *inc);
739 struct ast_ignorepat *ast_walk_context_ignorepats(struct ast_context *con,
740 struct ast_ignorepat *ip);
741 struct ast_sw *ast_walk_context_switches(struct ast_context *con, struct ast_sw *sw);
743 int pbx_builtin_serialize_variables(struct ast_channel *chan, char *buf, size_t size);
744 const char *pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name);
745 void pbx_builtin_pushvar_helper(struct ast_channel *chan, const char *name, const char *value);
746 void pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value);
747 void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, char *workspace, int workspacelen, struct varshead *headp);
748 void pbx_builtin_clear_globals(void);
749 int pbx_builtin_setvar(struct ast_channel *chan, void *data);
750 void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count);
751 void pbx_substitute_variables_varshead(struct varshead *headp, const char *cp1, char *cp2, int count);
753 int ast_extension_patmatch(const char *pattern, const char *data);
755 /*! Set "autofallthrough" flag, if newval is <0, does not acutally set. If
756 set to 1, sets to auto fall through. If newval set to 0, sets to no auto
757 fall through (reads extension instead). Returns previous value. */
758 int pbx_set_autofallthrough(int newval);
759 int ast_goto_if_exists(struct ast_channel *chan, const char *context, const char *exten, int priority);
760 /* I can find neither parsable nor parseable at dictionary.com, but google gives me 169000 hits for parseable and only 49,800 for parsable */
761 int ast_parseable_goto(struct ast_channel *chan, const char *goto_string);
762 int ast_explicit_goto(struct ast_channel *chan, const char *context, const char *exten, int priority);
763 int ast_async_goto_if_exists(struct ast_channel *chan, const char *context, const char *exten, int priority);
765 struct ast_custom_function* ast_custom_function_find(const char *name);
768 * \brief Unregister a custom function
770 int ast_custom_function_unregister(struct ast_custom_function *acf);
773 * \brief Reigster a custom function
775 int ast_custom_function_register(struct ast_custom_function *acf);
778 * \brief Retrieve the number of active calls
780 int ast_active_calls(void);
783 * \brief executes a read operation on a function
785 * \param chan Channel to execute on
786 * \param function Data containing the function call string (will be modified)
787 * \param workspace A pointer to safe memory to use for a return value
788 * \param len the number of bytes in workspace
790 * This application executes a function in read mode on a given channel.
792 * \return zero on success, non-zero on failure
794 int ast_func_read(struct ast_channel *chan, char *function, char *workspace, size_t len);
797 * \brief executes a write operation on a function
799 * \param chan Channel to execute on
800 * \param function Data containing the function call string (will be modified)
801 * \param value A value parameter to pass for writing
803 * This application executes a function in write mode on a given channel.
805 * \return zero on success, non-zero on failure
807 int ast_func_write(struct ast_channel *chan, char *function, const char *value);
809 void ast_hint_state_changed(const char *device);
811 #if defined(__cplusplus) || defined(c_plusplus)
815 #endif /* _ASTERISK_PBX_H */