[asterisk-commits] gtjoseph: branch 12 r410006 - in /branches/12: ./ funcs/ include/asterisk/ ma...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Mar 6 09:13:17 CST 2014


Author: gtjoseph
Date: Thu Mar  6 09:13:03 2014
New Revision: 410006

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=410006
Log:
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/

Added:
    branches/12/funcs/func_sorcery.c   (with props)
Modified:
    branches/12/CHANGES
    branches/12/include/asterisk/config.h
    branches/12/include/asterisk/sorcery.h
    branches/12/main/bucket.c
    branches/12/main/config.c
    branches/12/main/sorcery.c
    branches/12/res/res_pjsip/config_auth.c
    branches/12/res/res_pjsip/config_transport.c
    branches/12/res/res_pjsip/location.c
    branches/12/res/res_pjsip/pjsip_configuration.c
    branches/12/res/res_pjsip_acl.c
    branches/12/res/res_pjsip_endpoint_identifier_ip.c
    branches/12/res/res_pjsip_outbound_registration.c
    branches/12/tests/test_sorcery.c

Modified: branches/12/CHANGES
URL: http://svnview.digium.com/svn/asterisk/branches/12/CHANGES?view=diff&rev=410006&r1=410005&r2=410006
==============================================================================
--- branches/12/CHANGES (original)
+++ branches/12/CHANGES Thu Mar  6 09:13:03 2014
@@ -7,6 +7,16 @@
 === and the other UPGRADE files for older releases.
 ===
 ==============================================================================
+
+------------------------------------------------------------------------------
+--- Functionality changes from Asterisk 12.1.0 to Asterisk 12.2.0 ------------
+------------------------------------------------------------------------------
+
+Core
+------------------
+ * Exposed sorcery-based configuration files like pjsip.conf to dialplans via
+   the new AST_SORCERY diaplan function.
+
 
 ------------------------------------------------------------------------------
 --- Functionality changes from Asterisk 12.0.0 to Asterisk 12.1.0 ------------

Added: branches/12/funcs/func_sorcery.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/funcs/func_sorcery.c?view=auto&rev=410006
==============================================================================
--- branches/12/funcs/func_sorcery.c (added)
+++ branches/12/funcs/func_sorcery.c Thu Mar  6 09:13:03 2014
@@ -1,0 +1,221 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Fairview 5 Engineering, LLC
+ *
+ * George Joseph <george.joseph at fairview5.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Get a field from a sorcery object
+ *
+ * \author \verbatim George Joseph <george.joseph at fairview5.com> \endverbatim
+ *
+ * \ingroup functions
+ *
+ */
+
+/*** MODULEINFO
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "")
+
+#include "asterisk/app.h"
+#include "asterisk/pbx.h"
+#include "asterisk/module.h"
+#include "asterisk/sorcery.h"
+
+/*** DOCUMENTATION
+	<function name="AST_SORCERY" language="en_US">
+		<synopsis>
+			Get a field from a sorcery object
+		</synopsis>
+		<syntax>
+			<parameter name="module_name" required="true">
+				<para>The name of the module owning the sorcery instance.</para>
+			</parameter>
+			<parameter name="object_type" required="true">
+				<para>The type of object to query.</para>
+			</parameter>
+			<parameter name="object_id" required="true">
+				<para>The id of the object to query.</para>
+			</parameter>
+			<parameter name="field_name" required="true">
+				<para>The name of the field.</para>
+			</parameter>
+			<parameter name="retrieval_method" required="false">
+				<para>Fields that have multiple occurrences may be retrieved in two ways.</para>
+				<enumlist>
+					<enum name="concat"><para>Returns all matching fields concatenated
+					in a single string separated by <replaceable>separator</replaceable>
+					which defaults to <literal>,</literal>.</para></enum>
+
+					<enum name="single"><para>Returns the nth occurrence of the field
+					as specified by <replaceable>occurrence_number</replaceable> which defaults to <literal>1</literal>.
+					</para></enum>
+				</enumlist>
+				<para>The default is <literal>concat</literal> with separator <literal>,</literal>.</para>
+			</parameter>
+			<parameter name="retrieval_details" required="false">
+				<para>Specifies either the separator for <literal>concat</literal>
+				or the occurrence number for <literal>single</literal>.</para>
+			</parameter>
+		</syntax>
+	</function>
+***/
+
+static int sorcery_function_read(struct ast_channel *chan,
+	const char *cmd, char *data, struct ast_str **buf, ssize_t len)
+{
+	char *parsed_data = ast_strdupa(data);
+	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
+	RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
+	struct ast_variable *change_set;
+	struct ast_variable *it_change_set;
+	int found, field_number = 1, ix, method;
+	char *separator = ",";
+
+	enum methods {
+		CONCAT,
+		SINGLE,
+	};
+
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(module_name);
+		AST_APP_ARG(object_type);
+		AST_APP_ARG(object_id);
+		AST_APP_ARG(field_name);
+		AST_APP_ARG(method);
+		AST_APP_ARG(method_arg);
+	);
+
+	/* Check for zero arguments */
+	if (ast_strlen_zero(parsed_data)) {
+		ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, parsed_data);
+
+	if (ast_strlen_zero(args.module_name)) {
+		ast_log(AST_LOG_ERROR, "Cannot call %s without a module name to query\n", cmd);
+		return -1;
+	}
+
+	if (ast_strlen_zero(args.object_type)) {
+		ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object type\n", cmd);
+		return -1;
+	}
+
+	if (ast_strlen_zero(args.object_id)) {
+		ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object name\n", cmd);
+		return -1;
+	}
+
+	if (ast_strlen_zero(args.field_name)) {
+		ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name\n", cmd);
+		return -1;
+	}
+
+	if (ast_strlen_zero(args.method)) {
+		method = CONCAT;
+	} else {
+		if (strcmp(args.method, "concat") == 0) {
+			method = CONCAT;
+			if (ast_strlen_zero(args.method_arg)) {
+				separator = ",";
+			} else {
+				separator = args.method_arg;
+			}
+
+		} else if (strcmp(args.method, "single") == 0) {
+			method = SINGLE;
+			if (!ast_strlen_zero(args.method_arg)) {
+				if (sscanf(args.method_arg, "%30d", &field_number) <= 0 || field_number <= 0 ) {
+					ast_log(AST_LOG_ERROR, "occurrence_number must be a positive integer\n");
+					return -1;
+				}
+			}
+		} else {
+			ast_log(AST_LOG_ERROR, "Retrieval method must be 'concat' or 'single'\n");
+			return -1;
+		}
+	}
+
+	sorcery = ast_sorcery_retrieve_by_module_name(args.module_name);
+	if (!sorcery) {
+		ast_log(AST_LOG_ERROR, "Failed to retrieve sorcery instance for module %s\n", args.module_name);
+		return -1;
+	}
+
+	sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args.object_type, args.object_id);
+	if (!sorcery_obj) {
+		return -1;
+	}
+
+	change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj);
+	if (!change_set) {
+		return -1;
+	}
+
+	ix=1;
+	found = 0;
+	for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
+
+		if (method == CONCAT && strcmp(it_change_set->name, args.field_name) == 0) {
+			ast_str_append(buf, 0, "%s%s", it_change_set->value, separator);
+			found = 1;
+			continue;
+		}
+
+		if (method == SINGLE && strcmp(it_change_set->name, args.field_name) == 0  && ix++ == field_number) {
+			ast_str_set(buf, len, "%s", it_change_set->value);
+			found = 1;
+			break;
+		}
+	}
+
+	ast_variables_destroy(change_set);
+
+	if (!found) {
+		return -1;
+	}
+
+	if (method == CONCAT) {
+		ast_str_truncate(*buf, -1);
+	}
+
+	return 0;
+}
+
+static struct ast_custom_function sorcery_function = {
+	.name = "AST_SORCERY",
+	.read2 = sorcery_function_read,
+};
+
+static int unload_module(void)
+{
+	return ast_custom_function_unregister(&sorcery_function);
+}
+
+static int load_module(void)
+{
+	return ast_custom_function_register(&sorcery_function);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get a field from a sorcery object");
+

Propchange: branches/12/funcs/func_sorcery.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: branches/12/funcs/func_sorcery.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: branches/12/funcs/func_sorcery.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: branches/12/include/asterisk/config.h
URL: http://svnview.digium.com/svn/asterisk/branches/12/include/asterisk/config.h?view=diff&rev=410006&r1=410005&r2=410006
==============================================================================
--- branches/12/include/asterisk/config.h (original)
+++ branches/12/include/asterisk/config.h Thu Mar  6 09:13:03 2014
@@ -691,7 +691,34 @@
 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

Modified: branches/12/include/asterisk/sorcery.h
URL: http://svnview.digium.com/svn/asterisk/branches/12/include/asterisk/sorcery.h?view=diff&rev=410006&r1=410005&r2=410006
==============================================================================
--- branches/12/include/asterisk/sorcery.h (original)
+++ branches/12/include/asterisk/sorcery.h Thu Mar  6 09:13:03 2014
@@ -119,6 +119,24 @@
 	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 @@
  * \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 @@
  * \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 @@
  * \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 @@
  * \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 @@
  * \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,18 +602,40 @@
  */
 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_create(const struct ast_sorcery *sorcery, const void *object);
+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
+ *
+ * \param sorcery Pointer to a sorcery structure
+ * \param object Pointer to a sorcery object
+ *
+ * \retval non-NULL success
+ * \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.
+ */
+#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

Modified: branches/12/main/bucket.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/main/bucket.c?view=diff&rev=410006&r1=410005&r2=410006
==============================================================================
--- branches/12/main/bucket.c (original)
+++ branches/12/main/bucket.c Thu Mar  6 09:13:03 2014
@@ -944,8 +944,8 @@
 	}
 
 	ast_sorcery_object_field_register(bucket_sorcery, "bucket", "scheme", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_bucket, scheme));
-	ast_sorcery_object_field_register_custom(bucket_sorcery, "bucket", "created", "", timeval_str2struct, timeval_struct2str, 0, FLDSET(struct ast_bucket, created));
-	ast_sorcery_object_field_register_custom(bucket_sorcery, "bucket", "modified", "", timeval_str2struct, timeval_struct2str, 0, FLDSET(struct ast_bucket, modified));
+	ast_sorcery_object_field_register_custom(bucket_sorcery, "bucket", "created", "", timeval_str2struct, timeval_struct2str, NULL, 0, FLDSET(struct ast_bucket, created));
+	ast_sorcery_object_field_register_custom(bucket_sorcery, "bucket", "modified", "", timeval_str2struct, timeval_struct2str, NULL, 0, FLDSET(struct ast_bucket, modified));
 
 	if (ast_sorcery_apply_default(bucket_sorcery, "file", "bucket_file", NULL)) {
 		ast_log(LOG_ERROR, "Failed to apply intermediary for 'file' object type in Bucket sorcery\n");
@@ -958,8 +958,8 @@
 	}
 
 	ast_sorcery_object_field_register(bucket_sorcery, "file", "scheme", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_bucket_file, scheme));
-	ast_sorcery_object_field_register_custom(bucket_sorcery, "file", "created", "", timeval_str2struct, timeval_struct2str, 0, FLDSET(struct ast_bucket_file, created));
-	ast_sorcery_object_field_register_custom(bucket_sorcery, "file", "modified", "", timeval_str2struct, timeval_struct2str, 0, FLDSET(struct ast_bucket_file, modified));
+	ast_sorcery_object_field_register_custom(bucket_sorcery, "file", "created", "", timeval_str2struct, timeval_struct2str, NULL, 0, FLDSET(struct ast_bucket_file, created));
+	ast_sorcery_object_field_register_custom(bucket_sorcery, "file", "modified", "", timeval_str2struct, timeval_struct2str, NULL, 0, FLDSET(struct ast_bucket_file, modified));
 
 	return 0;
 }

Modified: branches/12/main/config.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/main/config.c?view=diff&rev=410006&r1=410005&r2=410006
==============================================================================
--- branches/12/main/config.c (original)
+++ branches/12/main/config.c Thu Mar  6 09:13:03 2014
@@ -618,6 +618,26 @@
 		}
 	}
 	return top.next;
+}
+
+struct ast_variable *ast_variable_list_append_hint(struct ast_variable **head, struct ast_variable *search_hint, struct ast_variable *newvar)
+{
+	struct ast_variable *curr;
+	ast_assert(head != NULL);
+
+	if (!*head) {
+		*head = newvar;
+	} else {
+		if (search_hint == NULL) {
+			search_hint = *head;
+		}
+		for (curr = search_hint; curr->next; curr = curr->next);
+		curr->next = newvar;
+	}
+
+	for (curr = newvar; curr->next; curr = curr->next);
+
+	return curr;
 }
 
 const char *ast_config_option(struct ast_config *cfg, const char *cat, const char *var)

Modified: branches/12/main/sorcery.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/main/sorcery.c?view=diff&rev=410006&r1=410005&r2=410006
==============================================================================
--- branches/12/main/sorcery.c (original)
+++ branches/12/main/sorcery.c Thu Mar  6 09:13:03 2014
@@ -811,7 +811,7 @@
 }
 
 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, ...)
+					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, ...)
 {
 	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
 	RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
@@ -832,6 +832,7 @@
 
 	ast_copy_string(object_field->name, name, sizeof(object_field->name));
 	object_field->handler = sorcery_handler;
+	object_field->multiple_handler = multiple_handler;
 
 	va_start(args, argc);
 	for (pos = 0; pos < argc; pos++) {
@@ -1015,13 +1016,47 @@
 	ao2_ref(sorcery, +1);
 }
 
-struct ast_variable *ast_sorcery_objectset_create(const struct ast_sorcery *sorcery, const void *object)
+static struct ast_variable *get_single_field_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
+{
+	struct ast_variable *tmp = NULL;
+	char *buf = NULL;
+
+	if (!object_field->handler) {
+		return NULL;
+	}
+
+	if (!(object_field->handler(object, object_field->args, &buf))) {
+		tmp = ast_variable_new(object_field->name, S_OR(buf, ""), "");
+	}
+	ast_free(buf);
+
+	return tmp;
+}
+
+static struct ast_variable *get_multiple_fields_as_var_list(const void *object, struct ast_sorcery_object_field *object_field)
+{
+	struct ast_variable *tmp = NULL;
+
+	if (!object_field->multiple_handler) {
+		return NULL;
+	}
+
+	if (object_field->multiple_handler(object, &tmp)) {
+		ast_variables_destroy(tmp);
+		tmp = NULL;
+	}
+
+	return tmp;
+}
+
+struct ast_variable *ast_sorcery_objectset_create2(const struct ast_sorcery *sorcery,
+	const void *object,	enum ast_sorcery_field_handler_flags flags)
 {
 	const struct ast_sorcery_object_details *details = object;
 	RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
 	struct ao2_iterator i;
 	struct ast_sorcery_object_field *object_field;
-	struct ast_variable *fields = NULL;
+	struct ast_variable *head = NULL, *tail = NULL;
 	int res = 0;
 
 	if (!object_type) {
@@ -1033,38 +1068,46 @@
 	for (; (object_field = ao2_iterator_next(&i)) && !res; ao2_ref(object_field, -1)) {
 		struct ast_variable *tmp = NULL;
 
-		if (object_field->multiple_handler) {
-			if ((res = object_field->multiple_handler(object, &tmp))) {
-				ast_variables_destroy(tmp);
+		switch (flags) {
+		case AST_HANDLER_PREFER_LIST:
+			if ((tmp = get_multiple_fields_as_var_list(object, object_field)) ||
+				(tmp = get_single_field_as_var_list(object, object_field))) {
+				break;
 			}
-		} else if (object_field->handler) {
-			char *buf = NULL;
-
-			if ((res = object_field->handler(object, object_field->args, &buf)) ||
-				!(tmp = ast_variable_new(object_field->name, S_OR(buf, ""), ""))) {
-				res = -1;
+			continue;
+		case AST_HANDLER_PREFER_STRING:
+			if ((tmp = get_single_field_as_var_list(object, object_field)) ||
+				(tmp = get_multiple_fields_as_var_list(object, object_field))) {
+				break;
 			}
-
-			ast_free(buf);
-		} else {
 			continue;
-		}
-
-		if (!res && tmp) {
-			tmp->next = fields;
-			fields = tmp;
-		}
+		case AST_HANDLER_ONLY_LIST:
+			if ((tmp = get_multiple_fields_as_var_list(object, object_field))) {
+				break;
+			}
+			continue;
+		case AST_HANDLER_ONLY_STRING:
+			if ((tmp = get_single_field_as_var_list(object, object_field))) {
+				break;
+			}
+			continue;
+		default:
+			continue;
+		}
+
+		tail = ast_variable_list_append_hint(&head, tail, tmp);
+
 	}
 
 	ao2_iterator_destroy(&i);
 
 	/* If any error occurs we destroy all fields handled before so a partial objectset is not returned */
 	if (res) {
-		ast_variables_destroy(fields);
-		fields = NULL;
-	}
-
-	return fields;
+		ast_variables_destroy(head);
+		head = NULL;
+	}
+
+	return head;
 }
 
 struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)

Modified: branches/12/res/res_pjsip/config_auth.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_pjsip/config_auth.c?view=diff&rev=410006&r1=410005&r2=410006
==============================================================================
--- branches/12/res/res_pjsip/config_auth.c (original)
+++ branches/12/res/res_pjsip/config_auth.c Thu Mar  6 09:13:03 2014
@@ -321,7 +321,7 @@
 	ast_sorcery_object_field_register(sorcery, SIP_SORCERY_AUTH_TYPE, "nonce_lifetime",
 			"32", OPT_UINT_T, 0, FLDSET(struct ast_sip_auth, nonce_lifetime));
 	ast_sorcery_object_field_register_custom(sorcery, SIP_SORCERY_AUTH_TYPE, "auth_type",
-			"userpass", auth_type_handler, auth_type_to_str, 0, 0);
+			"userpass", auth_type_handler, auth_type_to_str, NULL, 0, 0);
 
 	ast_sip_register_endpoint_formatter(&endpoint_auth_formatter);
 	ast_sip_register_cli_formatter(&cli_auth_formatter);

Modified: branches/12/res/res_pjsip/config_transport.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_pjsip/config_transport.c?view=diff&rev=410006&r1=410005&r2=410006
==============================================================================
--- branches/12/res/res_pjsip/config_transport.c (original)
+++ branches/12/res/res_pjsip/config_transport.c Thu Mar  6 09:13:03 2014
@@ -438,6 +438,29 @@
 	return error;
 }
 
+static int localnet_to_vl(const void *obj, struct ast_variable **fields)
+{
+	const struct ast_sip_transport *transport = obj;
+
+	char str[MAX_OBJECT_FIELD];
+	struct ast_variable *head = NULL;
+	struct ast_ha *ha = transport->localnet;
+
+	for (; ha; ha = ha->next) {
+		const char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr));
+		snprintf(str, MAX_OBJECT_FIELD, "%s%s/%s", ha->sense == AST_SENSE_ALLOW ? "!" : "",
+			addr, ast_sockaddr_stringify_addr(&ha->netmask));
+
+		ast_variable_list_append(&head, ast_variable_new("local_net", str, ""));
+	}
+
+	if (head) {
+		*fields = head;
+	}
+
+	return 0;
+}
+
 static int localnet_to_str(const void *obj, const intptr_t *args, char **buf)
 {
 	RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
@@ -564,8 +587,8 @@
 	}
 
 	ast_sorcery_object_field_register(sorcery, "transport", "type", "", OPT_NOOP_T, 0, 0);
-	ast_sorcery_object_field_register_custom(sorcery, "transport", "protocol", "udp", transport_protocol_handler, transport_protocol_to_str, 0, 0);
-	ast_sorcery_object_field_register_custom(sorcery, "transport", "bind", "", transport_bind_handler, transport_bind_to_str, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "protocol", "udp", transport_protocol_handler, transport_protocol_to_str, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "bind", "", transport_bind_handler, transport_bind_to_str, NULL, 0, 0);
 	ast_sorcery_object_field_register(sorcery, "transport", "async_operations", "1", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, async_operations));
 	ast_sorcery_object_field_register(sorcery, "transport", "ca_list_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, ca_list_file));
 	ast_sorcery_object_field_register(sorcery, "transport", "cert_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, cert_file));
@@ -575,12 +598,12 @@
 	ast_sorcery_object_field_register(sorcery, "transport", "external_signaling_port", "0", OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_sip_transport, external_signaling_port), 0, 65535);
 	ast_sorcery_object_field_register(sorcery, "transport", "external_media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, external_media_address));
 	ast_sorcery_object_field_register(sorcery, "transport", "domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, domain));
-	ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_server", "", transport_tls_bool_handler, verify_server_to_str, 0, 0);
-	ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_client", "", transport_tls_bool_handler, verify_client_to_str, 0, 0);
-	ast_sorcery_object_field_register_custom(sorcery, "transport", "require_client_cert", "", transport_tls_bool_handler, require_client_cert_to_str, 0, 0);
-	ast_sorcery_object_field_register_custom(sorcery, "transport", "method", "", transport_tls_method_handler, tls_method_to_str, 0, 0);
-	ast_sorcery_object_field_register_custom(sorcery, "transport", "cipher", "", transport_tls_cipher_handler, transport_tls_cipher_to_str, 0, 0);
-	ast_sorcery_object_field_register_custom(sorcery, "transport", "local_net", "", transport_localnet_handler, localnet_to_str, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_server", "", transport_tls_bool_handler, verify_server_to_str, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_client", "", transport_tls_bool_handler, verify_client_to_str, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "require_client_cert", "", transport_tls_bool_handler, require_client_cert_to_str, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "method", "", transport_tls_method_handler, tls_method_to_str, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "cipher", "", transport_tls_cipher_handler, transport_tls_cipher_to_str, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "transport", "local_net", "", transport_localnet_handler, localnet_to_str, localnet_to_vl, 0, 0);
 	ast_sorcery_object_field_register(sorcery, "transport", "tos", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, tos));
 	ast_sorcery_object_field_register(sorcery, "transport", "cos", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, cos));
 

Modified: branches/12/res/res_pjsip/location.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_pjsip/location.c?view=diff&rev=410006&r1=410005&r2=410006
==============================================================================
--- branches/12/res/res_pjsip/location.c (original)
+++ branches/12/res/res_pjsip/location.c Thu Mar  6 09:13:03 2014
@@ -279,6 +279,25 @@
 	return 0;
 }
 
+static int contact_to_vl(void *object, void *arg, int flags)
+{
+	struct ast_sip_contact *contact = object;
+	struct ast_variable **var = arg;
+
+	ast_variable_list_append(&*var, ast_variable_new("contact", contact->uri, ""));
+
+	return 0;
+}
+
+static int contacts_to_vl(const void *obj, struct ast_variable **fields)
+{
+	const struct ast_sip_aor *aor = obj;
+
+	ast_sip_for_each_contact(aor, contact_to_vl, fields);
+
+	return 0;
+}
+
 int ast_sip_for_each_aor(const char *aors, ao2_callback_fn on_aor, void *arg)
 {
 	char *copy, *name;
@@ -343,7 +362,46 @@
 
 static int sip_aor_to_ami(const struct ast_sip_aor *aor, struct ast_str **buf)
 {
-	return ast_sip_sorcery_object_to_ami(aor, buf);
+	RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create2(
+			 ast_sip_get_sorcery(), aor, AST_HANDLER_ONLY_STRING), ast_variables_destroy);
+	struct ast_variable *i;
+
+	if (!objset) {
+		return -1;
+	}
+
+	ast_str_append(buf, 0, "ObjectType: %s\r\n",
+		       ast_sorcery_object_get_type(aor));
+	ast_str_append(buf, 0, "ObjectName: %s\r\n",
+		       ast_sorcery_object_get_id(aor));
+
+	for (i = objset; i; i = i->next) {
+		char *camel = ast_to_camel_case(i->name);
+		if (strcmp(camel, "Contact") == 0) {
+			ast_free(camel);
+			camel = NULL;
+		}
+		ast_str_append(buf, 0, "%s: %s\r\n", S_OR(camel, "Contacts"), i->value);
+		ast_free(camel);
+	}
+
+	return 0;
+}
+
+static int contacts_to_str(const void *obj, const intptr_t *args, char **buf)
+{
+	const struct ast_sip_aor *aor = obj;
+	RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
+
+	ast_sip_for_each_contact(aor, ast_sip_contact_to_str, &str);
+	ast_str_truncate(str, -1);
+
+	*buf = ast_strdup(ast_str_buffer(str));
+	if (!*buf) {
+		return -1;
+	}
+
+	return 0;
 }
 
 static int format_ami_aor_handler(void *obj, void *arg, int flags)
@@ -364,11 +422,6 @@
 	}
 
 	sip_aor_to_ami(aor, &buf);
-	ast_str_append(&buf, 0, "Contacts: ");
-	ast_sip_for_each_contact(aor, ast_sip_contact_to_str, &buf);
-	ast_str_truncate(buf, -1);
-	ast_str_append(&buf, 0, "\r\n");
-
 	total_contacts = ao2_container_count(contacts);
 	num_permanent = aor->permanent_contacts ?
 		ao2_container_count(aor->permanent_contacts) : 0;
@@ -670,7 +723,7 @@
 	ast_sorcery_object_field_register(sorcery, "contact", "type", "", OPT_NOOP_T, 0, 0);
 	ast_sorcery_object_field_register(sorcery, "contact", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, uri));
 	ast_sorcery_object_field_register(sorcery, "contact", "path", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, path));
-	ast_sorcery_object_field_register_custom(sorcery, "contact", "expiration_time", "", expiration_str2struct, expiration_struct2str, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "contact", "expiration_time", "", expiration_str2struct, expiration_struct2str, NULL, 0, 0);
 	ast_sorcery_object_field_register(sorcery, "contact", "qualify_frequency", 0, OPT_UINT_T,
 					  PARSE_IN_RANGE, FLDSET(struct ast_sip_contact, qualify_frequency), 0, 86400);
 	ast_sorcery_object_field_register(sorcery, "contact", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, outbound_proxy));
@@ -684,7 +737,7 @@
 	ast_sorcery_object_field_register(sorcery, "aor", "authenticate_qualify", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, authenticate_qualify));
 	ast_sorcery_object_field_register(sorcery, "aor", "max_contacts", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, max_contacts));
 	ast_sorcery_object_field_register(sorcery, "aor", "remove_existing", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, remove_existing));
-	ast_sorcery_object_field_register_custom(sorcery, "aor", "contact", "", permanent_uri_handler, NULL, 0, 0);
+	ast_sorcery_object_field_register_custom(sorcery, "aor", "contact", "", permanent_uri_handler, contacts_to_str, contacts_to_vl, 0, 0);
 	ast_sorcery_object_field_register(sorcery, "aor", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_aor, mailboxes));
 	ast_sorcery_object_field_register(sorcery, "aor", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_aor, outbound_proxy));
 	ast_sorcery_object_field_register(sorcery, "aor", "support_path", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, support_path));

Modified: branches/12/res/res_pjsip/pjsip_configuration.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_pjsip/pjsip_configuration.c?view=diff&rev=410006&r1=410005&r2=410006
==============================================================================
--- branches/12/res/res_pjsip/pjsip_configuration.c (original)
+++ branches/12/res/res_pjsip/pjsip_configuration.c Thu Mar  6 09:13:03 2014
@@ -775,12 +775,12 @@
 	}
 
 	*val++ = '\0';
+
 	if (!(new_var = ast_variable_new(name, val, ""))) {
 		return -1;
 	}
 
-	new_var->next = endpoint->channel_vars;
-	endpoint->channel_vars = new_var;
+	ast_variable_list_append(&endpoint->channel_vars, new_var);
 
 	return 0;
 }
@@ -799,6 +799,16 @@
 	ast_free(str);
 	return 0;
 }
+
+static int set_var_to_vl(const void *obj, struct ast_variable **fields)
+{
+	const struct ast_sip_endpoint *endpoint = obj;
+	if (endpoint->channel_vars) {
+		*fields = ast_variables_dup(endpoint->channel_vars);
+	}
+	return 0;
+}
+
 
 static void *sip_nat_hook_alloc(const char *name)
 {
@@ -987,8 +997,8 @@
 
 int ast_sip_sorcery_object_to_ami(const void *obj, struct ast_str **buf)
 {
-	RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create(
-			 ast_sip_get_sorcery(), obj), ast_variables_destroy);
+	RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create2(
+			 ast_sip_get_sorcery(), obj, AST_HANDLER_ONLY_STRING), ast_variables_destroy);
 	struct ast_variable *i;
 
 	if (!objset) {
@@ -1534,7 +1544,7 @@
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context));
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, media.prefs, media.codecs));
 	ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, media.prefs, media.codecs));

[... 682 lines stripped ...]



More information about the asterisk-commits mailing list