[asterisk-commits] dlee: branch dlee/stasis-app r381746 - in /team/dlee/stasis-app: apps/ includ...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Feb 19 10:33:32 CST 2013


Author: dlee
Date: Tue Feb 19 10:33:29 2013
New Revision: 381746

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=381746
Log:
Initial app_stasis work

Added:
    team/dlee/stasis-app/apps/app_stasis.c   (with props)
    team/dlee/stasis-app/apps/app_stasis.c.exports.in   (with props)
    team/dlee/stasis-app/apps/stasis_json.c   (with props)
    team/dlee/stasis-app/include/asterisk/app_stasis.h   (with props)
    team/dlee/stasis-app/res/res_stasis_websocket.c   (with props)
    team/dlee/stasis-app/tests/test_app_stasis.c   (with props)
Modified:
    team/dlee/stasis-app/apps/Makefile
    team/dlee/stasis-app/include/asterisk/frame.h
    team/dlee/stasis-app/include/asterisk/json.h
    team/dlee/stasis-app/include/asterisk/localtime.h
    team/dlee/stasis-app/include/asterisk/strings.h
    team/dlee/stasis-app/main/frame.c
    team/dlee/stasis-app/res/res_json.c
    team/dlee/stasis-app/tests/test_abstract_jb.c
    team/dlee/stasis-app/tests/test_json.c
    team/dlee/stasis-app/tests/test_strings.c

Modified: team/dlee/stasis-app/apps/Makefile
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/apps/Makefile?view=diff&rev=381746&r1=381745&r2=381746
==============================================================================
--- team/dlee/stasis-app/apps/Makefile (original)
+++ team/dlee/stasis-app/apps/Makefile Tue Feb 19 10:33:29 2013
@@ -38,3 +38,4 @@
   LIBS+= -lres_smdi.so
 endif
 
+app_stasis.so: stasis_json.o

Added: team/dlee/stasis-app/apps/app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/apps/app_stasis.c?view=auto&rev=381746
==============================================================================
--- team/dlee/stasis-app/apps/app_stasis.c (added)
+++ team/dlee/stasis-app/apps/app_stasis.c Tue Feb 19 10:33:29 2013
@@ -1,0 +1,469 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2012 - 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee at digium.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 Stasis dialplan application.
+ *
+ * \author David M. Lee, II <dlee at digium.com>
+ */
+
+/*** MODULEINFO
+	<depend>res_json</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/app.h"
+#include "asterisk/app_stasis.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/channel.h"
+#include "asterisk/hashtab.h"
+#include "asterisk/module.h"
+#include "asterisk/stasis.h"
+
+/*** DOCUMENTATION
+	<application name="Stasis" language="en_US">
+		<synopsis>Invoke an extenal Stasis application.</synopsis>
+		<syntax>
+			<parameter name="app_name" required="true">
+				<para>Name of the application to invoke.</para>
+			</parameter>
+			<parameter name="args">
+				<para>Optional arguments for the application invocation.</para>
+			</parameter>
+		</syntax>
+		<description>
+			<para>Invoke an extenal Stasis application.</para>
+		</description>
+	</application>
+ ***/
+
+static struct stasis_message_type *__ast_app_event;
+
+struct stasis_message_type *ast_app_event(void) {
+	ast_assert(__ast_app_event != NULL);
+	return __ast_app_event;
+}
+
+/*! Maximum number of arguments for the Stasis dialplan application */
+#define MAX_ARGS 128
+
+/*! Dialplan application name */
+static const char *stasis = "Stasis";
+
+/*! Number of buckets for the Stasis application hash table. Remember to keep it a prime number! */
+#define APPS_NUM_BUCKETS 127
+
+/*! Stasis application container. Please call stasis_apps() instead. */
+struct ao2_container *__stasis_apps;
+
+/*! Ref-counting accessor for the stasis applications container */
+static struct ao2_container *stasis_apps(void)
+{
+	ao2_ref(__stasis_apps, +1);
+	return __stasis_apps;
+}
+
+/*! Stasis application ref */
+struct stasis_app {
+	/*! Name of the Stasis application */
+	char *name;
+	/*! Callback function for this application. */
+	stasis_app_handler handler;
+	/*! Opaque data to hand to callback function. */
+	void *data;
+	/*! When set, /c app_stasis should exit and continue in the dialplan */
+	int continue_to_dialplan:1;
+};
+
+/*! Destructor for \ref stasis_app. */
+static void app_dtor(void *obj)
+{
+	struct stasis_app *app = obj;
+	ast_free(app->name);
+}
+
+static void stasis_app_event_dtor(void *obj) {
+	struct stasis_app_event *app_event = obj;
+
+	ast_free(app_event->event_name);
+	app_event->event_name = NULL;
+	ast_json_unref(app_event->event_details);
+	app_event->event_details = NULL;
+}
+
+/*! Constructor for \ref stasis_app. */
+static struct stasis_app *app_create(const char *name, stasis_app_handler handler, void *data)
+{
+	RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
+
+	ast_assert(name != NULL);
+	ast_assert(handler != NULL);
+
+	app = ao2_alloc_options(sizeof(*app), app_dtor, AO2_ALLOC_OPT_LOCK_MUTEX);
+
+	if (!app) {
+		return NULL;
+	}
+
+	if (!(app->name = ast_strdup(name))) {
+		return NULL;
+	}
+
+	app->handler = handler;
+	app->data = data;
+
+	ao2_ref(app, +1);
+	return app;
+}
+
+/*! AO2 hash function for \ref stasis_app */
+static int app_hash(const void *obj, const int flags)
+{
+	const struct stasis_app *app = obj;
+	const char *name = flags & OBJ_KEY ? obj : app->name;
+
+	return ast_hashtab_hash_string(name);
+}
+
+/*! AO2 comparison function for \ref stasis_app */
+static int app_compare(void *lhs, void *rhs, int flags)
+{
+	const struct stasis_app *lhs_app = lhs;
+	const struct stasis_app *rhs_app = rhs;
+	const char *rhs_name = flags & OBJ_KEY ? rhs : rhs_app->name;
+
+	if (strcmp(lhs_app->name, rhs_name) == 0) {
+		return CMP_MATCH | CMP_STOP;
+	} else {
+		return 0;
+	}
+}
+
+/*!
+ * \brief Test the \c continue_to_dialplan bit for the given \a app.
+ *
+ * The bit is also reset for the next call.
+ *
+ * \param app Application to check the \c continue_to_dialplan bit.
+ * \return Zero to remain in \c Stasis
+ * \return Non-zero to continue in the dialplan
+ */
+static int app_continue_test_and_reset(struct stasis_app *app)
+{
+	int r;
+	SCOPED_AO2LOCK(lock, app);
+
+	r = app->continue_to_dialplan;
+	app->continue_to_dialplan = 0;
+	return r;
+}
+
+/*!
+ * \brief Send a message to the given application.
+ * \param app App to send the message to.
+ * \param message Message to send.
+ */
+static void app_send(struct stasis_app *app, struct stasis_message *message)
+{
+	app->handler(app->data, app->name, message);
+}
+
+static struct stasis_message *stasis_app_event_create(const char *event_name, const struct ast_channel_snapshot *channel_info, const struct ast_json *extra_info) {
+	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
+	RAII_VAR(struct stasis_app_event *, app_event, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_json *, event_details, NULL, ast_json_unref);
+
+	if (event_name == NULL) {
+		return NULL;
+	}
+
+	app_event = ao2_alloc(sizeof(*app_event), stasis_app_event_dtor);
+
+	app_event->event_name = ast_strdup(event_name);
+	if (!app_event->event_name) {
+		ast_log(LOG_ERROR, "Allocation failed\n");
+		return NULL;
+	}
+
+	if (extra_info) {
+		event_details = ast_json_deep_copy(extra_info);
+	} else {
+		event_details = ast_json_object_create();
+	}
+	if (channel_info) {
+		ast_json_object_add(event_details, "channel_info", channel_info);
+	}
+	app_event->event_details = ast_json_ref(event_details);
+
+	message = stasis_message_create(stasis_app_event(), app_event);
+	if (!message) {
+		return NULL;
+	}
+
+	ao2_ref(message, +1);
+	return message;
+}
+
+static int send_start_msg(struct stasis_app *app, struct ast_channel *chan, int argc, char *argv[])
+{
+	RAII_VAR(struct ast_json *, msg_info, NULL, ast_json_unref);
+	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+
+	struct ast_json *json_args;
+	int i;
+
+	ast_assert(chan != NULL);
+
+	msg_info = ast_json_pack("{s: []}", "args");
+	if (!msg) {
+		ast_log(LOG_ERROR, "Couldn't create message for %s\n", app->name);
+		return -1;
+	}
+
+	/* Append arguments to args array */
+	json_args = ast_json_object_get(msg, "args");
+	ast_assert(json_args != NULL);
+	for (i = 0; i < argc; ++i) {
+		if (ast_json_array_append(json_args, ast_json_string_create(argv[i])) != 0) {
+			ast_log(LOG_ERROR, "Error appending arg to start message\n");
+			return -1;
+		}
+	}
+
+	/* Set channel info */
+	snapshot = ast_channel_snapshot_create(chan);
+	if (ast_json_object_set(msg, "channel-info", ast_channel_snapshot_to_json(snapshot)) != 0) {
+		ast_log(LOG_ERROR, "Couldn't attach channel-info info to message\n");
+		return -1;
+	}
+
+	app_send(app, msg);
+	return 0;
+}
+
+static int send_end_msg(struct stasis_app *app, struct ast_channel *chan)
+{
+	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+
+	ast_assert(chan != NULL);
+
+	msg = ast_json_pack("{s: s, s: s}",
+			    "event", "app-end",
+			    "app_name", app->name,
+			    "channel_id", ast_channel_uniqueid(chan));
+	if (!msg) {
+		ast_log(LOG_ERROR, "Couldn't create message for %s\n", app->name);
+		return -1;
+	}
+
+	/* Set channel info */
+	snapshot = ast_channel_snapshot_create(chan);
+	if (ast_json_object_set(msg, "channel-info", ast_channel_snapshot_to_json(snapshot)) != 0) {
+		ast_log(LOG_ERROR, "Couldn't attach channel-info info to message\n");
+		return -1;
+	}
+
+	app_send(app, msg);
+	return 0;
+}
+
+static void sub_handler(void *data, struct stasis_topic *topic, struct stasis_message *message)
+{
+	/* TODO */
+	//struct stasis_app *app = data;
+	ast_assert(0);
+}
+
+
+/*! /brief Stasis dialplan application callback */
+static int stasis_exec(struct ast_channel *chan, const char *data)
+{
+	RAII_VAR(struct ao2_container *, apps, stasis_apps(), ao2_cleanup);
+	RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
+	RAII_VAR(struct stasis_subscription *, subscription, NULL, stasis_unsubscribe);
+	int res = 0;
+	char *parse = NULL;
+	int hungup = 0;
+
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(app_name);
+		AST_APP_ARG(app_argv)[MAX_ARGS];
+	);
+
+	ast_assert(chan != NULL);
+	ast_assert(data != NULL);
+
+	/* parse the arguments */
+	parse = ast_strdupa(data);
+	AST_STANDARD_APP_ARGS(args, parse);
+
+	if (args.argc < 1) {
+		ast_log(LOG_WARNING, "Stasis app_name argument missing\n");
+		return -1;
+	}
+
+	app = ao2_find(apps, args.app_name, OBJ_KEY);
+
+	if (!app) {
+		ast_log(LOG_ERROR, "Stasis app '%s' not registered\n", args.app_name);
+		return -1;
+	}
+
+	subscription = stasis_subscribe(ast_channel_events(chan), sub_handler, app);
+
+	if (subscription == NULL) {
+		ast_log(LOG_ERROR, "Error subscribing app %s to channel %s\n", args.app_name, ast_channel_name(chan));
+		return -1;
+	}
+
+	res = send_start_msg(app, chan, args.argc - 1, args.app_argv);
+	if (res != 0) {
+		ast_log(LOG_ERROR, "Error sending start message to %s\n", args.app_name);
+		return res;
+	}
+
+	while (!hungup && !app_continue_test_and_reset(app) && ast_waitfor(chan, -1) > -1) {
+		RAII_VAR(struct ast_frame *, f, ast_read(chan), ast_frame_dtor);
+		if (!f) {
+			ast_debug(3, "%s: No more frames. Must be done, I guess.\n", ast_channel_uniqueid(chan));
+			break;
+		}
+
+		switch (f->frametype) {
+		case AST_FRAME_CONTROL:
+			if (f->subclass.integer == AST_CONTROL_HANGUP) {
+				ast_debug(3, "%s: Received hangup\n", ast_channel_uniqueid(chan));
+				hungup = 1;
+			}
+			break;
+		default:
+			/* Not handled; discard */
+			break;
+		}
+	}
+
+	res = send_end_msg(app, chan);
+	if (res != 0) {
+		ast_log(LOG_ERROR, "Error sending end message to %s\n", args.app_name);
+		return res;
+	}
+
+	return res;
+}
+
+int stasis_app_send(const char *app_name, struct ast_json *message)
+{
+	RAII_VAR(struct ao2_container *, apps, stasis_apps(), ao2_cleanup);
+	RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
+
+	app = ao2_find(apps, app_name, OBJ_KEY);
+
+	if (!app) {
+		/* XXX We can do a better job handling late binding, queueing up the call for a few seconds
+		 * to wait for the app to register.
+		 */
+		ast_log(LOG_WARNING, "Stasis app '%s' not registered\n", app_name);
+		return -1;
+	}
+
+	app_send(app, message);
+	return 0;
+}
+
+int stasis_app_register(const char *app_name, stasis_app_handler handler, void *data)
+{
+	RAII_VAR(struct ao2_container *, apps, stasis_apps(), ao2_cleanup);
+	RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
+
+	SCOPED_LOCK(apps_lock, apps, ao2_lock, ao2_unlock);
+
+	app = ao2_find(apps, app_name, OBJ_KEY | OBJ_NOLOCK);
+
+	if (app) {
+		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
+		SCOPED_LOCK(app_lock, app, ao2_lock, ao2_unlock);
+
+		msg = ast_json_pack("{s: s}", "event", "application-replaced");
+		app->handler(app->data, app_name, msg);
+
+		app->handler = handler;
+		app->data = data;
+	} else {
+		app = app_create(app_name, handler, data);
+		if (app) {
+			ao2_link(apps, app);
+		} else {
+			ast_log(LOG_ERROR, "Failed to allocate stasis_app\n");
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
+void stasis_app_unregister(const char *app_name)
+{
+	RAII_VAR(struct ao2_container *, apps, stasis_apps(), ao2_cleanup);
+
+	ao2_cleanup(ao2_find(apps, app_name, OBJ_KEY | OBJ_UNLINK));
+}
+
+static int load_module(void)
+{
+	int r = 0;
+
+	__ast_app_event = stasis_message_type_create("ast_app_event");
+	if (__ast_app_event == NULL) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
+	__stasis_apps = ao2_container_alloc(APPS_NUM_BUCKETS, app_hash, app_compare);
+	if (__stasis_apps == NULL) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
+	r |= ast_register_application_xml(stasis, stasis_exec);
+	return r;
+}
+
+static int unload_module(void)
+{
+	int r = 0;
+
+	ao2_cleanup(__ast_app_event);
+	__ast_app_event = NULL;
+
+	ao2_cleanup(__stasis_apps);
+	__stasis_apps = NULL;
+
+	r |= ast_unregister_application(stasis);
+	return r;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Stasis dialplan application",
+		.load = load_module,
+		.unload = unload_module,
+		.load_pri = AST_MODPRI_DEFAULT,
+		.nonoptreq = "res_json");

Propchange: team/dlee/stasis-app/apps/app_stasis.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/dlee/stasis-app/apps/app_stasis.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Propchange: team/dlee/stasis-app/apps/app_stasis.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/dlee/stasis-app/apps/app_stasis.c.exports.in
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/apps/app_stasis.c.exports.in?view=auto&rev=381746
==============================================================================
--- team/dlee/stasis-app/apps/app_stasis.c.exports.in (added)
+++ team/dlee/stasis-app/apps/app_stasis.c.exports.in Tue Feb 19 10:33:29 2013
@@ -1,0 +1,7 @@
+{
+	global:
+		LINKER_SYMBOL_PREFIXast_app_stasis_find_by_channel;
+		LINKER_SYMBOL_PREFIXast_channel_snapshot_to_json;
+	local:
+		*;
+};

Propchange: team/dlee/stasis-app/apps/app_stasis.c.exports.in
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/dlee/stasis-app/apps/app_stasis.c.exports.in
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Propchange: team/dlee/stasis-app/apps/app_stasis.c.exports.in
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/dlee/stasis-app/apps/stasis_json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/apps/stasis_json.c?view=auto&rev=381746
==============================================================================
--- team/dlee/stasis-app/apps/stasis_json.c (added)
+++ team/dlee/stasis-app/apps/stasis_json.c Tue Feb 19 10:33:29 2013
@@ -1,0 +1,71 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee at digium.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 Stasis application JSON converters.
+ *
+ * \author David M. Lee, II <dlee at digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/app_stasis.h"
+
+struct ast_json *ast_channel_snapshot_to_json(struct ast_channel_snapshot *snapshot)
+{
+	RAII_VAR(struct ast_json *, json_chan, ast_json_object_create(), ast_json_unref);
+	int r = 0;
+
+	if (!json_chan) { ast_log(LOG_ERROR, "Error creating channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "name", ast_json_string_create(snapshot->name));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "state", ast_json_string_create(ast_state2str(snapshot->state)));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "accountcode", ast_json_string_create(snapshot->accountcode));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "peeraccount", ast_json_string_create(snapshot->peeraccount));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "userfield", ast_json_string_create(snapshot->userfield));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "uniqueid", ast_json_string_create(snapshot->uniqueid));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "linkedid", ast_json_string_create(snapshot->linkedid));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "parkinglot", ast_json_string_create(snapshot->parkinglot));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "hangupsource", ast_json_string_create(snapshot->hangupsource));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "appl", ast_json_string_create(snapshot->appl));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "data", ast_json_string_create(snapshot->data));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "dialplan", ast_json_dialplan_cep(snapshot->context, snapshot->exten, snapshot->priority));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "caller", ast_json_name_number(snapshot->caller_name, snapshot->caller_number));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "connected", ast_json_name_number(snapshot->connected_name, snapshot->connected_number));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+	r = ast_json_object_set(json_chan, "creationtime", ast_json_timeval(&snapshot->creationtime, NULL));
+	if (r) { ast_log(LOG_ERROR, "Error adding attrib to channel json object\n"); return NULL; }
+
+	return ast_json_ref(json_chan);
+}
+

Propchange: team/dlee/stasis-app/apps/stasis_json.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/dlee/stasis-app/apps/stasis_json.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Propchange: team/dlee/stasis-app/apps/stasis_json.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/dlee/stasis-app/include/asterisk/app_stasis.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/include/asterisk/app_stasis.h?view=auto&rev=381746
==============================================================================
--- team/dlee/stasis-app/include/asterisk/app_stasis.h (added)
+++ team/dlee/stasis-app/include/asterisk/app_stasis.h Tue Feb 19 10:33:29 2013
@@ -1,0 +1,115 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2012 - 2013, Digium, Inc.
+ *
+ * David M. Lee, II <dlee at digium.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.
+ */
+
+#ifndef _ASTERISK_APP_STASIS_H
+#define _ASTERISK_APP_STASIS_H
+
+/*! \file
+ *
+ * \brief Stasis Application API.
+ *
+ * This is the API that binds the Stasis dialplan application to external
+ * Stasis applications.
+ *
+ * \author David M. Lee, II <dlee at digium.com>
+ */
+
+#include "asterisk/channel.h"
+#include "asterisk/json.h"
+
+/*! \brief Handler for controlling app_stasis */
+struct stasis_app_handler;
+
+/*!
+ * \brief Returns the handler for the given channel
+ * \param chan Channel to handle.
+ * \return NULL channel not in Stasis application
+ * \return Pointer to app_stasis handler.
+ */
+struct stasis_app_handler *stasis_app_find_by_channel(struct ast_channel *chan);
+
+/*!
+ * \brief Exit app_stasis and continue execution in the dialplan
+ * \param handler Handler for app_stasis
+ * \return 0 on Success
+ * \return Non-zero on error
+ */
+void stasis_app_continue(struct stasis_app_handler *handler);
+
+void stasis_app_handler_unref(struct stasis_app_handler *handler);
+
+/*!
+ * \brief Send a message to the given Stasis application
+ * \param app_name Name of the application to invoke
+ * \param message Message to send (borrowed reference)
+ * \return 0 for success.
+ * \return -1 for error.
+ */
+int stasis_app_send(const char *app_name, struct ast_json *message);
+
+/*!
+ * \brief Callback for Stasis application handler.
+ *
+ * The message given to the handler is a borrowed copy. If you want to keep a
+ * reference to it, you should use \c ast_ref() to keep it around.
+ *
+ * \param data Data ptr given when registered.
+ * \param app_name Name of the application being dispatched to.
+ * \param message Message to handle. (borrowed copy)
+ */
+typedef void (*stasis_app_handler)(void *data, const char *app_name, struct stasis_message *message);
+
+/*!
+ * \brief Register a new Stasis application.
+ * If an application is already registered with the given name, the old
+ * application is sent a 'replaced' message and unregistered.
+ * \param app_name Name of this application.
+ * \param handler Callback for application messages.
+ * \param data Data blob to pass to the callback.
+ * \return 0 for success
+ * \return -1 for error.
+ */
+int stasis_app_register(const char *app_name, stasis_app_handler handler, void *data);
+
+/*!
+ * \brief Unregister a Stasis application.
+ * \param app_name Name of the application to unregister.
+ */
+void stasis_app_unregister(const char *app_name);
+
+/*!
+ * \brief Build a JSON object from a \ref ast_channel_snapshot.
+ * \return JSON object representing channel snapshot.
+ * \return \c NULL on error
+ */
+struct ast_json *ast_channel_snapshot_to_json(struct ast_channel_snapshot *snapshot);
+
+/*!
+ * \brief This wrapper around \ref ast_json to make it an AO2 object.
+ */
+struct stasis_app_event {
+	char *event_name;
+	struct ast_json *event_details;
+};
+
+/*!
+ * \brief Message type for \ref stasis_app_event.
+ */
+struct stasis_message_type *stasis_app_event(void);
+
+#endif /* _ASTERISK_APP_STASIS_H */

Propchange: team/dlee/stasis-app/include/asterisk/app_stasis.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/dlee/stasis-app/include/asterisk/app_stasis.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Propchange: team/dlee/stasis-app/include/asterisk/app_stasis.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/dlee/stasis-app/include/asterisk/frame.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/include/asterisk/frame.h?view=diff&rev=381746&r1=381745&r2=381746
==============================================================================
--- team/dlee/stasis-app/include/asterisk/frame.h (original)
+++ team/dlee/stasis-app/include/asterisk/frame.h Tue Feb 19 10:33:29 2013
@@ -39,11 +39,11 @@
 /*!
  * \page Def_Frame AST Multimedia and signalling frames
  * \section Def_AstFrame What is an ast_frame ?
- * A frame of data read used to communicate between 
+ * A frame of data read used to communicate between
  * between channels and applications.
  * Frames are divided into frame types and subclasses.
  *
- * \par Frame types 
+ * \par Frame types
  * \arg \b VOICE:  Voice data, subclass is codec (AST_FORMAT_*)
  * \arg \b VIDEO:  Video data, subclass is codec (AST_FORMAT_*)
  * \arg \b DTMF:   A DTMF digit, subclass is the digit
@@ -88,7 +88,7 @@
  */
 
 /*!
- * \brief Frame types 
+ * \brief Frame types
  *
  * \note It is important that the values of each frame type are never changed,
  *       because it will break backwards compatability with older versions.
@@ -113,11 +113,11 @@
 	AST_FRAME_IMAGE,
 	/*! HTML Frame */
 	AST_FRAME_HTML,
-	/*! Comfort Noise frame (subclass is level of CNG in -dBov), 
+	/*! Comfort Noise frame (subclass is level of CNG in -dBov),
 	    body may include zero or more 8-bit quantization coefficients */
 	AST_FRAME_CNG,
 	/*! Modem-over-IP data streams */
-	AST_FRAME_MODEM,	
+	AST_FRAME_MODEM,
 	/*! DTMF begin event, subclass is the digit */
 	AST_FRAME_DTMF_BEGIN,
 };
@@ -137,24 +137,24 @@
  */
 struct ast_frame {
 	/*! Kind of frame */
-	enum ast_frame_type frametype;				
+	enum ast_frame_type frametype;
 	/*! Subclass, frame dependent */
 	union ast_frame_subclass subclass;
 	/*! Length of data */
-	int datalen;				
+	int datalen;
 	/*! Number of samples in this frame */
-	int samples;				
+	int samples;
 	/*! Was the data malloc'd?  i.e. should we free it when we discard the frame? */
-	int mallocd;				
+	int mallocd;
 	/*! The number of bytes allocated for a malloc'd frame header */
 	size_t mallocd_hdr_len;
 	/*! How many bytes exist _before_ "data" that can be used if needed */
-	int offset;				
+	int offset;
 	/*! Optional source of frame for debugging */
-	const char *src;				
+	const char *src;
 	/*! Pointer to actual data */
 	union { void *ptr; uint32_t uint32; char pad[8]; } data;
-	/*! Global delivery time */		
+	/*! Global delivery time */
 	struct timeval delivery;
 	/*! For placing in a linked list */
 	AST_LIST_ENTRY(ast_frame) frame_list;
@@ -197,7 +197,7 @@
  * RTP header information into the space provided by AST_FRIENDLY_OFFSET instead
  * of having to create a new buffer with the necessary space allocated.
  */
-#define AST_FRIENDLY_OFFSET 	64	
+#define AST_FRIENDLY_OFFSET 	64
 #define AST_MIN_OFFSET 		32	/*! Make sure we keep at least this much handy */
 
 /*! Need the header be free'd? */
@@ -353,10 +353,10 @@
 #define AST_OPTION_FLAG_ANSWER		5
 #define AST_OPTION_FLAG_WTF		6
 
-/*! Verify touchtones by muting audio transmission 
+/*! Verify touchtones by muting audio transmission
  * (and reception) and verify the tone is still present
  * Option data is a single signed char value 0 or 1 */
-#define AST_OPTION_TONE_VERIFY		1		
+#define AST_OPTION_TONE_VERIFY		1
 
 /*! Put a compatible channel into TDD (TTY for the hearing-impared) mode
  * Option data is a single signed char value 0 or 1 */
@@ -370,7 +370,7 @@
  * Option data is a single signed char value 0 or 1 */
 #define	AST_OPTION_AUDIO_MODE		4
 
-/*! Set channel transmit gain 
+/*! Set channel transmit gain
  * Option data is a single signed char representing number of decibels (dB)
  * to set gain to (on top of any gain specified in channel driver) */
 #define AST_OPTION_TXGAIN		5
@@ -380,7 +380,7 @@
  * to set gain to (on top of any gain specified in channel driver) */
 #define AST_OPTION_RXGAIN		6
 
-/* set channel into "Operator Services" mode 
+/* set channel into "Operator Services" mode
  * Option data is a struct oprmode
  *
  * \note This option should never be sent over the network */
@@ -433,7 +433,7 @@
  * Option data is a character buffer of suitable length */
 #define AST_OPTION_DEVICE_NAME		16
 
-/*! Get the CC agent type from the channel (Read only) 
+/*! Get the CC agent type from the channel (Read only)
  * Option data is a character buffer of suitable length */
 #define AST_OPTION_CC_AGENT_TYPE    17
 
@@ -450,12 +450,12 @@
 struct ast_option_header {
 	/* Always keep in network byte order */
 #if __BYTE_ORDER == __BIG_ENDIAN
-        uint16_t flag:3;
-        uint16_t option:13;
+	uint16_t flag:3;
+	uint16_t option:13;
 #else
 #if __BYTE_ORDER == __LITTLE_ENDIAN
-        uint16_t option:13;
-        uint16_t flag:3;
+	uint16_t option:13;
+	uint16_t flag:3;
 #else
 #error Byte order not defined
 #endif
@@ -463,25 +463,31 @@
 		uint8_t data[0];
 };
 
-/*! \brief  Requests a frame to be allocated 
- * 
- * \param source 
- * Request a frame be allocated.  source is an optional source of the frame, 
- * len is the requested length, or "0" if the caller will supply the buffer 
+/*! \brief  Requests a frame to be allocated
+ *
+ * \param source
+ * Request a frame be allocated.  source is an optional source of the frame,
+ * len is the requested length, or "0" if the caller will supply the buffer
  */
 #if 0 /* Unimplemented */
 struct ast_frame *ast_fralloc(char *source, int len);
 #endif
 
-/*!  
+/*!
  * \brief Frees a frame or list of frames
- * 
+ *
  * \param fr Frame to free, or head of list to free
  * \param cache Whether to consider this frame for frame caching
  */
 void ast_frame_free(struct ast_frame *fr, int cache);
 
 #define ast_frfree(fr) ast_frame_free(fr, 1)
+
+/*!
+ * \brief NULL-safe wrapper for \ref ast_frfree, good for \ref RAII_VAR.
+ * \param frame Frame to free, or head of list to free.
+ */
+void ast_frame_dtor(struct ast_frame *frame);
 
 /*! \brief Makes a frame independent of any static storage
  * \param fr frame to act upon
@@ -498,7 +504,7 @@
  */
 struct ast_frame *ast_frisolate(struct ast_frame *fr);
 
-/*! \brief Copies a frame 
+/*! \brief Copies a frame
  * \param fr frame to copy
  * Duplicates a frame -- should only rarely be used, typically frisolate is good enough
  * \return Returns a frame on success, NULL on error
@@ -507,7 +513,7 @@
 
 void ast_swapcopy_samples(void *dst, const void *src, int samples);
 
-/* Helpers for byteswapping native samples to/from 
+/* Helpers for byteswapping native samples to/from
    little-endian and big-endian. */
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #define ast_frame_byteswap_le(fr) do { ; } while(0)
@@ -518,13 +524,13 @@
 #endif
 
 /*! \brief Parse an "allow" or "deny" line in a channel or device configuration
-        and update the capabilities and pref if provided.
+	and update the capabilities and pref if provided.
 	Video codecs are not added to codec preference lists, since we can not transcode
 	\return Returns number of errors encountered during parsing
  */
 int ast_parse_allow_disallow(struct ast_codec_pref *pref, struct ast_format_cap *cap, const char *list, int allowing);
 
-/*! \name AST_Smoother 
+/*! \name AST_Smoother
 */
 /*@{ */
 /*! \page ast_smooth The AST Frame Smoother
@@ -584,7 +590,7 @@
 
 /*! \brief Gets duration in ms of interpolation frame for a format */
 static inline int ast_codec_interp_len(struct ast_format *format)
-{ 
+{
 	return (format->id == AST_FORMAT_ILBC) ? 30 : 20;
 }
 

Modified: team/dlee/stasis-app/include/asterisk/json.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/include/asterisk/json.h?view=diff&rev=381746&r1=381745&r2=381746
==============================================================================
--- team/dlee/stasis-app/include/asterisk/json.h (original)
+++ team/dlee/stasis-app/include/asterisk/json.h Tue Feb 19 10:33:29 2013
@@ -759,4 +759,48 @@
 
 /*!@}*/
 
+/*!@{*/
+
+/*!
+ * \brief Common JSON rendering functions for common 'objects'.
+ */
+
+/*!
+ * \brief Simple name/number pair.
+ * \param name Name
+ * \param number Number
+ * \return NULL if error (non-UTF8 characters, NULL inputs, etc.)
+ * \return JSON object with name and number fields
+ */
+struct ast_json *ast_json_name_number(const char *name, const char *number);
+
+/*!
+ * \brief Construct a timeval as JSON.
+ *
+ * JSON does not define a standard date format (boo), but the de facto standard
+ * is to use ISO 8601 formatted string. We build a millisecond resolution string
+ * from the \c timeval
+ *
+ * \param tv \c timeval to encode.
+ * \param zone Text string of a standard system zoneinfo file.  If NULL, the system localtime will be used.
+ * \return JSON string with ISO 8601 formatted date/time.
+ * \return \c NULL on error.
+ */
+struct ast_json *ast_json_timeval(struct timeval *tv, const char *zone);
+
+/*!
+ * \brief Construct a context/exten/priority as JSON.
+ *
+ * If a \c NULL is passed for \c context or \c exten, or -1 for \c priority,
+ * the fields is set to ast_json_null().
+ *
+ * \param context Context name.
+ * \param exten Extension.
+ * \param priority Dialplan priority.
+ * \return JSON object with \c context, \c exten and \c priority fields
+ */
+struct ast_json *ast_json_dialplan_cep(const char *context, const char *exten, int priority);
+
+/*!@}*/
+
 #endif /* _ASTERISK_JSON_H */

Modified: team/dlee/stasis-app/include/asterisk/localtime.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/include/asterisk/localtime.h?view=diff&rev=381746&r1=381745&r2=381746
==============================================================================
--- team/dlee/stasis-app/include/asterisk/localtime.h (original)
+++ team/dlee/stasis-app/include/asterisk/localtime.h Tue Feb 19 10:33:29 2013
@@ -99,4 +99,9 @@
 struct ast_test;
 void ast_localtime_wakeup_monitor(struct ast_test *info);
 
+/*! \brief ast_strftime for ISO8601 formatting timestamps. */
+#define AST_ISO8601_FORMAT "%FT%T.%q%z"
+/*! \brief Max length of an null terminated, millisecond resolution, ISO8601 timestamp string. */
+#define AST_ISO8601_LEN 29
+
 #endif /* _ASTERISK_LOCALTIME_H */

Modified: team/dlee/stasis-app/include/asterisk/strings.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/include/asterisk/strings.h?view=diff&rev=381746&r1=381745&r2=381746
==============================================================================
--- team/dlee/stasis-app/include/asterisk/strings.h (original)
+++ team/dlee/stasis-app/include/asterisk/strings.h Tue Feb 19 10:33:29 2013
@@ -82,6 +82,48 @@
  */
 #define S_COR(a, b, c) ({typeof(&((b)[0])) __x = (b); (a) && !ast_strlen_zero(__x) ? (__x) : (c);})
 
+/*
+  \brief Checks whether a string begins with another.
+  \since 12.0.0
+  \param str String to check.
+  \param prefix Prefix to look for.
+  \param 1 if \a str begins with \a prefix, 0 otherwise.
+ */
+static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
+{
+	ast_assert(str != NULL);
+	ast_assert(prefix != NULL);
+	while (*str == *prefix && *prefix != '\0') {
+		++str;
+		++prefix;
+	}
+	return *prefix == '\0';
+}
+
+/*
+  \brief Checks whether a string ends with another.
+  \since 12.0.0
+  \param str String to check.
+  \param suffix Suffix to look for.
+  \param 1 if \a str ends with \a suffix, 0 otherwise.
+ */
+static int force_inline attribute_pure ast_ends_with(const char *str, const char *suffix)
+{
+	size_t str_len;
+	size_t suffix_len;
+
+	ast_assert(str != NULL);
+	ast_assert(suffix != NULL);
+	str_len = strlen(str);
+	suffix_len = strlen(suffix);
+
+	if (suffix_len > str_len) {
+		return 0;
+	}
+
+	return strcmp(str + str_len - suffix_len, suffix) == 0;
+}
+
 /*!
   \brief Gets a pointer to the first non-whitespace character in a string.
   \param str the input string

Modified: team/dlee/stasis-app/main/frame.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/main/frame.c?view=diff&rev=381746&r1=381745&r2=381746
==============================================================================
--- team/dlee/stasis-app/main/frame.c (original)
+++ team/dlee/stasis-app/main/frame.c Tue Feb 19 10:33:29 2013
@@ -351,6 +351,13 @@
 	}
 }
 
+void ast_frame_dtor(struct ast_frame *f)
+{
+	if (f) {
+		ast_frfree(f);
+	}
+}
+
 /*!
  * \brief 'isolates' a frame by duplicating non-malloc'ed components
  * (header, src, data).

Modified: team/dlee/stasis-app/res/res_json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/res/res_json.c?view=diff&rev=381746&r1=381745&r2=381746
==============================================================================
--- team/dlee/stasis-app/res/res_json.c (original)
+++ team/dlee/stasis-app/res/res_json.c Tue Feb 19 10:33:29 2013
@@ -36,10 +36,12 @@
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/json.h"
+#include "asterisk/localtime.h"
 #include "asterisk/module.h"
 #include "asterisk/utils.h"
 
 #include <jansson.h>
+#include <time.h>
 
 /*!
  * \brief Function wrapper around ast_malloc macro.
@@ -502,6 +504,38 @@
 	return (struct ast_json *)json_deep_copy((json_t *)value);
 }
 
+struct ast_json *ast_json_name_number(const char *name, const char *number)
+{
+	return ast_json_pack("{s: s, s: s}",
+			     "name", name,
+			     "number", number);
+}
+
+struct ast_json *ast_json_dialplan_cep(const char *context, const char *exten, int priority)
+{
+	return ast_json_pack("{s: o, s: o, s: o}",
+			     "context", context ? ast_json_string_create(context) : ast_json_null(),
+			     "exten", exten ? ast_json_string_create(exten) : ast_json_null(),
+			     "priority", priority != -1 ? ast_json_integer_create(priority) : ast_json_null());
+}
+
+struct ast_json *ast_json_timeval(struct timeval *tv, const char *zone)
+{
+	char buf[AST_ISO8601_LEN];
+	struct ast_tm tm = {};
+
+	if (tv == NULL) {
+		return NULL;
+	}
+
+	ast_localtime(tv, &tm, zone);
+
+	ast_strftime(buf, sizeof(buf),AST_ISO8601_FORMAT, &tm);
+
+	return ast_json_string_create(buf);
+}
+
+
 static int unload_module(void)
 {
 	/* Nothing to do */

Added: team/dlee/stasis-app/res/res_stasis_websocket.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/res/res_stasis_websocket.c?view=auto&rev=381746
==============================================================================

[... 804 lines stripped ...]



More information about the asterisk-commits mailing list