summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorGeorge Joseph <george.joseph@fairview5.com>2014-03-06 22:39:54 +0000
committerGeorge Joseph <george.joseph@fairview5.com>2014-03-06 22:39:54 +0000
commita4906e9f867cf6db946fc273d5f83cd7503e5bb8 (patch)
treeadbd83f29fb3eb5c2bb6f9defb585fdfc287b60c /include
parentf0b8590c14d9f5b899f5edc1158ee90200ace6dc (diff)
sorcery: Create AST_SORCERY dialplan function.
This patch creates the AST_SORCERY dialplan function which allows someone to retrieve any value from a sorcery-based config file. It's similar to AST_CONFIG. The creation of the function itself was fairly straightforward but it required changes to the underlying sorcery infrastructure that rippled into individual sorcery objects. The changes stemmed from inconsistencies in how sorcery created ast_variable objectsets from sorcery objects and the inconsistency in how individual objects used that feature especially when it came to parameters that can be specified multiple times like contact in aor and match in identify. You can read more here... http://lists.digium.com/pipermail/asterisk-dev/2014-February/065202.html So, what this patch does, besides actually creating the AST_SORCERY function, is the following... * Creates ast_variable_list_append which is a helper to append one ast_variable list to another. * Modifies the ast_sorcery_object_field_register functions to accept the already-defined sorcery_fields_handler callback. * Modifies ast_sorcery_objectset_create to accept a parameter indicating return type preference...a single ast_variable with all values concatenated or an ast_variable list with multiple entries. Also fixed a few bugs. * Modifies individual sorcery object implementations to use the new function definition of the ast_sorcery_object_field_register functions. * Modifies location.c and res_pjsip_endpoint_identifier_ip.c to implement sorcery_fields_handler handlers so they return multiple occurrences as an ast_variable_list. * Added a whole bunch of tests to test_sorcery. (closes issue ASTERISK-22537) Review: http://reviewboard.asterisk.org/r/3254/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@410042 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'include')
-rw-r--r--include/asterisk/config.h29
-rw-r--r--include/asterisk/sorcery.h66
2 files changed, 84 insertions, 11 deletions
diff --git a/include/asterisk/config.h b/include/asterisk/config.h
index 6d341db61..1c10d176b 100644
--- a/include/asterisk/config.h
+++ b/include/asterisk/config.h
@@ -691,7 +691,34 @@ void ast_include_rename(struct ast_config *conf, const char *from_file, const ch
void ast_variable_append(struct ast_category *category, struct ast_variable *variable);
void ast_variable_insert(struct ast_category *category, struct ast_variable *variable, const char *line);
int ast_variable_delete(struct ast_category *category, const char *variable, const char *match, const char *line);
-struct ast_variable *ast_variable_list_sort(struct ast_variable *start);
+
+/*!
+ * \brief Performs an in-place sort on the variable list by ascending name
+ *
+ * \param head The variable list head
+ *
+ * \return The new list head
+ */
+struct ast_variable *ast_variable_list_sort(struct ast_variable *head);
+
+/*!
+ * \brief Appends a variable list to the end of another list
+ *
+ * \param head A pointer to an ast_variable * of the existing variable list head. May NOT be NULL
+ * but the content may be to initialize a new list. If so, upon return, this parameter will be updated
+ * with a pointer to the new list head.
+ * \param search_hint The place in the current list to start searching for the end of the list.
+ * Might help performance on longer lists. If NULL, it defaults to head.
+ * \param new_var The head of the new variable list to be appended
+ *
+ * \return The tail of the resulting list.
+ *
+ * \note If the existing *head is NULL, it will be updated to new_var. This allows you to call
+ * ast_variable_list_append in a loop or callback without initializing the list first.
+ */
+struct ast_variable *ast_variable_list_append_hint(struct ast_variable **head, struct ast_variable *search_hint,
+ struct ast_variable *new_var);
+#define ast_variable_list_append(head, new_var) ast_variable_list_append_hint(head, NULL, new_var)
/*!
* \brief Update variable value within a config
diff --git a/include/asterisk/sorcery.h b/include/asterisk/sorcery.h
index cf11528d1..d4d62bfdb 100644
--- a/include/asterisk/sorcery.h
+++ b/include/asterisk/sorcery.h
@@ -119,6 +119,24 @@ enum ast_sorcery_retrieve_flags {
AST_RETRIEVE_FLAG_ALL = (1 << 1),
};
+/*!
+ * \brief Field handler flags
+ */
+enum ast_sorcery_field_handler_flags {
+ /*! \brief Try both handlers, string first */
+ AST_HANDLER_PREFER_STRING,
+
+ /*! \brief Try both handlers, list first */
+ AST_HANDLER_PREFER_LIST,
+
+ /*! \brief Use string handler only */
+ AST_HANDLER_ONLY_STRING,
+
+ /*! \brief Use list handler only */
+ AST_HANDLER_ONLY_LIST,
+};
+
+
/*! \brief Forward declaration for the sorcery main structure */
struct ast_sorcery;
@@ -465,15 +483,19 @@ int ast_sorcery_object_fields_register(struct ast_sorcery *sorcery, const char *
* \param type Type of object
* \param name Name of the field
* \param default_val Default value of the field
+ * \param config_handler A custom handler for translating the string representation of the fields
+ * \param sorcery_handler A custom handler for translating the native representation of the fields
+ * \param multiple_handler A custom handler for translating the native representation of the fields
* \param opt_type Option type
* \param flags Option type specific flags
*
* \retval 0 success
* \retval -1 failure
*/
-int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char *type, const char *name, const char *default_val, enum aco_option_type opt_type,
- aco_option_handler config_handler, sorcery_field_handler sorcery_handler, unsigned int flags, unsigned int no_doc,
- size_t argc, ...);
+int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char *type,
+ const char *name, const char *default_val, enum aco_option_type opt_type,
+ aco_option_handler config_handler, sorcery_field_handler sorcery_handler,
+ sorcery_fields_handler multiple_handler, unsigned int flags, unsigned int no_doc, size_t argc, ...);
/*!
* \brief Register a field within an object
@@ -489,7 +511,7 @@ int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char
* \retval -1 failure
*/
#define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags, ...) \
- __ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, NULL, NULL, flags, 0, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
+ __ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, NULL, NULL, NULL, flags, 0, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
/*!
* \brief Register a field within an object without documentation
@@ -505,7 +527,7 @@ int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char
* \retval -1 failure
*/
#define ast_sorcery_object_field_register_nodoc(sorcery, type, name, default_val, opt_type, flags, ...) \
- __ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, NULL, NULL, flags, 1, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
+ __ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, NULL, NULL, NULL, flags, 1, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
/*!
* \brief Register a field within an object with custom handlers
@@ -516,13 +538,14 @@ int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char
* \param default_val Default value of the field
* \param config_handler Custom configuration handler
* \param sorcery_handler Custom sorcery handler
+ * \param multiple_handler Custom multiple handler
* \param flags Option type specific flags
*
* \retval 0 success
* \retval -1 failure
*/
-#define ast_sorcery_object_field_register_custom(sorcery, type, name, default_val, config_handler, sorcery_handler, flags, ...) \
- __ast_sorcery_object_field_register(sorcery, type, name, default_val, OPT_CUSTOM_T, config_handler, sorcery_handler, flags, 0, VA_NARGS(__VA_ARGS__), __VA_ARGS__);
+#define ast_sorcery_object_field_register_custom(sorcery, type, name, default_val, config_handler, sorcery_handler, multiple_handler, flags, ...) \
+ __ast_sorcery_object_field_register(sorcery, type, name, default_val, OPT_CUSTOM_T, config_handler, sorcery_handler, multiple_handler, flags, 0, VA_NARGS(__VA_ARGS__), __VA_ARGS__);
/*!
* \brief Register a field within an object with custom handlers without documentation
@@ -533,13 +556,14 @@ int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char
* \param default_val Default value of the field
* \param config_handler Custom configuration handler
* \param sorcery_handler Custom sorcery handler
+ * \param multiple_handler Custom multiple handler
* \param flags Option type specific flags
*
* \retval 0 success
* \retval -1 failure
*/
-#define ast_sorcery_object_field_register_custom_nodoc(sorcery, type, name, default_val, config_handler, sorcery_handler, flags, ...) \
- __ast_sorcery_object_field_register(sorcery, type, name, default_val, OPT_CUSTOM_T, config_handler, sorcery_handler, flags, 1, VA_NARGS(__VA_ARGS__), __VA_ARGS__);
+#define ast_sorcery_object_field_register_custom_nodoc(sorcery, type, name, default_val, config_handler, sorcery_handler, multiple_handler, flags, ...) \
+ __ast_sorcery_object_field_register(sorcery, type, name, default_val, OPT_CUSTOM_T, config_handler, sorcery_handler, multiple_handler, flags, 1, VA_NARGS(__VA_ARGS__), __VA_ARGS__);
/*!
* \brief Inform any wizards to load persistent objects
@@ -578,6 +602,22 @@ void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *ty
*/
void ast_sorcery_ref(struct ast_sorcery *sorcery);
+
+/*!
+ * \brief Create an object set (KVP list) for an object
+ *
+ * \param sorcery Pointer to a sorcery structure
+ * \param object Pointer to a sorcery object
+ * \param flags Flags indicating which handler to use and in what order.
+ *
+ * \retval non-NULL success
+ * \retval NULL if error occurred
+ *
+ * \note The returned ast_variable list must be destroyed using ast_variables_destroy
+ */
+struct ast_variable *ast_sorcery_objectset_create2(const struct ast_sorcery *sorcery,
+ const void *object, enum ast_sorcery_field_handler_flags flags);
+
/*!
* \brief Create an object set (KVP list) for an object
*
@@ -588,8 +628,14 @@ void ast_sorcery_ref(struct ast_sorcery *sorcery);
* \retval NULL if error occurred
*
* \note The returned ast_variable list must be destroyed using ast_variables_destroy
+ *
+ * \note This function attempts to use a field's sorcery_fields_handler first and if that
+ * doesn't exist or fails, a field's sorcery_field_handler is used. The difference is
+ * that the former may return multiple list entries for the same field and the latter will only
+ * return 1. It's up to the field itself to determine what the appropriate content is.
*/
-struct ast_variable *ast_sorcery_objectset_create(const struct ast_sorcery *sorcery, const void *object);
+#define ast_sorcery_objectset_create(sorcery, object) \
+ ast_sorcery_objectset_create2(sorcery, object, AST_HANDLER_PREFER_LIST)
/*!
* \brief Create an object set in JSON format for an object