[Asterisk-code-review] res_speech_aeap: Add basic config skeleton and CLI commands. (asterisk[master])

Benjamin Keith Ford asteriskteam at digium.com
Mon Mar 29 12:40:26 CDT 2021


Benjamin Keith Ford has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/15699 )


Change subject: res_speech_aeap: Add basic config skeleton and CLI commands.
......................................................................

res_speech_aeap: Add basic config skeleton and CLI commands.

Added support for a basic speech_aeap configuration read from
speech_aeap.conf. Also added 2 CLI commands for showing individual
configurations as well as all of them.

https://wiki.asterisk.org/wiki/pages/viewpage.action?pageId=45482453

Change-Id: I567ac5148c92b98d29d2ad83421b416b75ffdaa3
---
A res/res_speech_aeap.c
1 file changed, 290 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/99/15699/1

diff --git a/res/res_speech_aeap.c b/res/res_speech_aeap.c
new file mode 100644
index 0000000..36f8bd5
--- /dev/null
+++ b/res/res_speech_aeap.c
@@ -0,0 +1,290 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2021, Sangoma Technologies Corporation
+ *
+ * Ben Ford <bford at sangoma.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.
+ */
+
+/*** MODULEINFO
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/module.h"
+#include "asterisk/sorcery.h"
+#include "asterisk/cli.h"
+#include "asterisk/format.h"
+#include "asterisk/format_cap.h"
+
+/*** DOCUMENTATION
+	<configInfo name="res_speech_aeap" language="en_US">
+		<synopsis>Speech AEAP module for Asterisk</synopsis>
+		<configFile name="speech_aeap.conf">
+			<configObject name="server">
+				<synopsis>Speech AEAP server options</synopsis>
+				<configOption name="type">
+					<synopsis>Must be of type 'server'.</synopsis>
+				</configOption>
+				<configOption name="server">
+					<synopsis>The name of the server to connect to.</synopsis>
+				</configOption>
+				<configOption name="allow">
+				        <synopsis>Optional media codec(s)</synopsis>
+				</configOption>
+			</configObject>
+		</configFile>
+	</configInfo>
+ ***/
+
+#define CONFIG_TYPE "server"
+
+/* Asterisk External Application Protocol sorcery object */
+static struct ast_sorcery *speech_aeap_sorcery;
+
+struct speech_aeap_server
+{
+	SORCERY_OBJECT(details);
+	AST_DECLARE_STRING_FIELDS(
+		/*! The name of the speech server to connect to */
+		AST_STRING_FIELD(server);
+	);
+	/*! An optional list of codecs that will be used if provided */
+	struct ast_format_cap *codecs;
+};
+
+static void speech_aeap_server_destructor(void *obj)
+{
+	struct speech_aeap_server *cfg = obj;
+
+	ast_string_field_free_memory(cfg);
+	ao2_cleanup(cfg->codecs);
+}
+
+static void *speech_aeap_server_alloc(const char *name)
+{
+	struct speech_aeap_server *cfg;
+
+	cfg = ast_sorcery_generic_alloc(sizeof(*cfg), speech_aeap_server_destructor);
+	if (!cfg) {
+		return NULL;
+	}
+
+	if (ast_string_field_init(cfg, 512)) {
+		ao2_ref(cfg, -1);
+		return NULL;
+	}
+
+	if (!(cfg->codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
+		ao2_ref(cfg, -1);
+		return NULL;
+	}
+
+	return cfg;
+}
+
+static int speech_aeap_server_apply(const struct ast_sorcery *sorcery, void *obj)
+{
+	struct speech_aeap_server *cfg = obj;
+
+	if (ast_strlen_zero(cfg->server)) {
+		ast_log(LOG_ERROR, "Server must be present\n");
+		return -1;
+	}
+
+	return 0;
+}
+
+static struct speech_aeap_server *speech_aeap_server_get(const char *id)
+{
+	return ast_sorcery_retrieve_by_id(speech_aeap_sorcery, CONFIG_TYPE, id);
+}
+
+static struct ao2_container *speech_aeap_server_get_all(void)
+{
+	return ast_sorcery_retrieve_by_fields(speech_aeap_sorcery, CONFIG_TYPE,
+		AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
+}
+
+static char *speech_aeap_tab_complete_name(const char *word, struct ao2_container *container)
+{
+	void *obj;
+	struct ao2_iterator it;
+	int wordlen = strlen(word);
+	int ret;
+
+	it = ao2_iterator_init(container, 0);
+	while ((obj = ao2_iterator_next(&it))) {
+		if (!strncasecmp(word, ast_sorcery_object_get_id(obj), wordlen)) {
+			ret = ast_cli_completion_add(ast_strdup(ast_sorcery_object_get_id(obj)));
+			if (ret) {
+				ao2_ref(obj, -1);
+				break;
+			}
+		}
+		ao2_ref(obj, -1);
+	}
+	ao2_iterator_destroy(&it);
+
+	return NULL;
+}
+
+static int speech_aeap_cli_show(void *obj, void *arg, int flags)
+{
+	struct ast_cli_args *a = arg;
+	struct ast_variable *options;
+	struct ast_variable *i;
+
+	if (!obj) {
+		ast_cli(a->fd, "No speech_aeap configuration found\n");
+		return 0;
+	}
+
+	options = ast_variable_list_sort(ast_sorcery_objectset_create2(
+		speech_aeap_sorcery, obj, AST_HANDLER_ONLY_STRING));
+	if (!options) {
+		return 0;
+	}
+
+	ast_cli(a->fd, "%s: %s\n", ast_sorcery_object_get_type(obj),
+		ast_sorcery_object_get_id(obj));
+
+	for (i = options; i; i = i->next) {
+		ast_cli(a->fd, "\t%s: %s\n", i->name, i->value);
+	}
+
+	ast_cli(a->fd, "\n");
+
+	ast_variables_destroy(options);
+
+	return 0;
+}
+
+static char *speech_aeap_server_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	struct speech_aeap_server *cfg;
+
+	switch(cmd) {
+	case CLI_INIT:
+		e->command = "speech_aeap show server";
+		e->usage =
+			"Usage: speech_aeap show server <id>\n"
+			"       Show the speech_aeap settings for a given server\n";
+		return NULL;
+	case CLI_GENERATE: 
+		if (a->pos == 3) {
+			return speech_aeap_tab_complete_name(a->word, speech_aeap_server_get_all());
+		} else {
+			return NULL;
+		}
+	}
+
+	if (a->argc != 4) {
+		return CLI_SHOWUSAGE;
+	}
+
+	cfg = speech_aeap_server_get(a->argv[3]);
+	speech_aeap_cli_show(cfg, a, 0);
+	ao2_cleanup(cfg);
+
+	return CLI_SUCCESS;
+}
+
+static char *speech_aeap_server_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	struct ao2_container *container;
+
+	switch(cmd) {
+	case CLI_INIT:
+		e->command = "speech_aeap show servers";
+		e->usage =
+			"Usage: speech_aeap show servers\n"
+			"       Show all configured speech_aeap servers\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != 3) {
+		return CLI_SHOWUSAGE;
+	}
+
+	container = speech_aeap_server_get_all();
+	if (!container || ao2_container_count(container) == 0) {
+		ast_cli(a->fd, "No speech_aeap servers found\n");
+		ao2_cleanup(container);
+		return CLI_SUCCESS;
+	}
+
+	ao2_callback(container, OBJ_NODATA, speech_aeap_cli_show, a);
+	ao2_ref(container, -1);
+
+	return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry speech_aeap_cli[] = {
+	AST_CLI_DEFINE(speech_aeap_server_show, "Show speech_aeap server configuration by id"),
+	AST_CLI_DEFINE(speech_aeap_server_show_all, "Show all speech_aeap server configurations"),
+};
+
+static int reload_module(void)
+{
+	return 0;
+}
+
+static int unload_module(void)
+{
+	ast_sorcery_unref(speech_aeap_sorcery);
+	speech_aeap_sorcery = NULL;
+
+	ast_cli_unregister_multiple(speech_aeap_cli, ARRAY_LEN(speech_aeap_cli));
+
+	return 0;
+}
+
+static int load_module(void)
+{
+	if (!(speech_aeap_sorcery = ast_sorcery_open()))
+	{
+		ast_log(LOG_ERROR, "speech_aeap - failed to open sorcery\n");
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	ast_sorcery_apply_default(speech_aeap_sorcery, CONFIG_TYPE, "config", "speech_aeap.conf,criteria=type=server");
+
+	if (ast_sorcery_object_register(speech_aeap_sorcery, CONFIG_TYPE, speech_aeap_server_alloc,
+		NULL, speech_aeap_server_apply)) {
+		ast_log(LOG_ERROR, "speech_aeap - failed to register '%s' sorcery object\n", CONFIG_TYPE);
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	ast_sorcery_object_field_register(speech_aeap_sorcery, CONFIG_TYPE, "type", "", OPT_NOOP_T, 0, 0);
+	ast_sorcery_object_field_register(speech_aeap_sorcery, CONFIG_TYPE, "server", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct speech_aeap_server, server));
+	ast_sorcery_object_field_register(speech_aeap_sorcery, CONFIG_TYPE, "codecs", "", OPT_CODEC_T, 1, FLDSET(struct speech_aeap_server, codecs));
+
+	ast_sorcery_load(speech_aeap_sorcery);
+
+	ast_cli_register_multiple(speech_aeap_cli, ARRAY_LEN(speech_aeap_cli));
+
+	return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER,
+	"Asterisk External Application Protocol Speech Module for Asterisk",
+	.support_level = AST_MODULE_SUPPORT_CORE,
+	.load = load_module,
+	.unload = unload_module,
+	.reload = reload_module,
+	.load_pri = AST_MODPRI_CHANNEL_DEPEND,
+);

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/15699
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I567ac5148c92b98d29d2ad83421b416b75ffdaa3
Gerrit-Change-Number: 15699
Gerrit-PatchSet: 1
Gerrit-Owner: Benjamin Keith Ford <bford at digium.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210329/7f97f5ab/attachment-0001.html>


More information about the asterisk-code-review mailing list