[asterisk-commits] dlee: branch dlee/stasis-http r385787 - in /team/dlee/stasis-http: ./ apps/ i...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Apr 15 13:04:30 CDT 2013


Author: dlee
Date: Mon Apr 15 13:04:26 2013
New Revision: 385787

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=385787
Log:
Merged revisions 385734-385743 from http://svn.asterisk.org/svn/asterisk/trunk

Added:
    team/dlee/stasis-http/include/asterisk/stasis_app.h
      - copied, changed from r385743, trunk/include/asterisk/stasis_app.h
    team/dlee/stasis-http/res/res_stasis.c
      - copied, changed from r385743, trunk/res/res_stasis.c
    team/dlee/stasis-http/res/res_stasis.exports.in
      - copied unchanged from r385743, trunk/res/res_stasis.exports.in
    team/dlee/stasis-http/tests/test_res_stasis.c
      - copied unchanged from r385743, trunk/tests/test_res_stasis.c
Removed:
    team/dlee/stasis-http/apps/app_stasis.exports.in
    team/dlee/stasis-http/apps/stasis_json.c
    team/dlee/stasis-http/include/asterisk/app_stasis.h
    team/dlee/stasis-http/tests/test_app_stasis.c
Modified:
    team/dlee/stasis-http/   (props changed)
    team/dlee/stasis-http/apps/Makefile
    team/dlee/stasis-http/apps/app_stasis.c
    team/dlee/stasis-http/include/asterisk/cli.h
    team/dlee/stasis-http/include/asterisk/json.h
    team/dlee/stasis-http/include/asterisk/stasis_channels.h
    team/dlee/stasis-http/include/asterisk/strings.h
    team/dlee/stasis-http/main/channel.c
    team/dlee/stasis-http/main/json.c
    team/dlee/stasis-http/main/manager_channels.c
    team/dlee/stasis-http/main/stasis_channels.c
    team/dlee/stasis-http/res/res_stasis_websocket.c
    team/dlee/stasis-http/res/stasis_http/resource_channels.c
    team/dlee/stasis-http/tests/test_json.c
    team/dlee/stasis-http/tests/test_stasis_channels.c

Propchange: team/dlee/stasis-http/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Mon Apr 15 13:04:26 2013
@@ -1,1 +1,1 @@
-/trunk:1-385718
+/trunk:1-385746

Modified: team/dlee/stasis-http/apps/Makefile
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/apps/Makefile?view=diff&rev=385787&r1=385786&r2=385787
==============================================================================
--- team/dlee/stasis-http/apps/Makefile (original)
+++ team/dlee/stasis-http/apps/Makefile Mon Apr 15 13:04:26 2013
@@ -37,5 +37,3 @@
   LIBS+= -lres_features.so -lres_ael_share.so -lres_monitor.so -lres_speech.so
   LIBS+= -lres_smdi.so
 endif
-
-app_stasis.so: stasis_json.o

Modified: team/dlee/stasis-http/apps/app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/apps/app_stasis.c?view=diff&rev=385787&r1=385786&r2=385787
==============================================================================
--- team/dlee/stasis-http/apps/app_stasis.c (original)
+++ team/dlee/stasis-http/apps/app_stasis.c Mon Apr 15 13:04:26 2013
@@ -24,6 +24,7 @@
  */
 
 /*** MODULEINFO
+	<depend>res_stasis</depend>
 	<support_level>core</support_level>
  ***/
 
@@ -32,14 +33,9 @@
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/app.h"
-#include "asterisk/app_stasis.h"
-#include "asterisk/astobj2.h"
-#include "asterisk/channel.h"
-#include "asterisk/lock.h"
 #include "asterisk/module.h"
 #include "asterisk/stasis.h"
-#include "asterisk/strings.h"
-#include "asterisk/stasis_channels.h"
+#include "asterisk/stasis_app.h"
 
 /*** DOCUMENTATION
 	<application name="Stasis" language="en_US">
@@ -61,483 +57,16 @@
 	</application>
  ***/
 
-/*! Time to wait for a frame in the application */
-#define MAX_WAIT_MS 200
-
 /*! \brief Maximum number of arguments for the Stasis dialplan application */
 #define MAX_ARGS 128
 
 /*! \brief Dialplan application name */
 static const char *stasis = "Stasis";
 
-/*!
- * \brief Number of buckets for the Stasis application hash table.  Remember to
- * keep it a prime number!
- */
-#define APPS_NUM_BUCKETS 127
-
-/*!
- * \brief Number of buckets for the Stasis application hash table.  Remember to
- * keep it a prime number!
- */
-#define CONTROLS_NUM_BUCKETS 127
-
-/*!
- * \brief Stasis application container. Please call apps_registry() instead of
- * directly accessing.
- */
-struct ao2_container *__apps_registry;
-
-struct ao2_container *__app_controls;
-
-/*! Ref-counting accessor for the stasis applications container */
-static struct ao2_container *apps_registry(void)
+/*! /brief Stasis dialplan application callback */
+static int app_exec(struct ast_channel *chan, const char *data)
 {
-	ao2_ref(__apps_registry, +1);
-	return __apps_registry;
-}
-
-static struct ao2_container *app_controls(void)
-{
-	ao2_ref(__app_controls, +1);
-	return __app_controls;
-}
-
-struct app {
-	/*! Callback function for this application. */
-	stasis_app_cb handler;
-	/*! Opaque data to hand to callback function. */
-	void *data;
-	/*! Name of the Stasis application */
-	char name[];
-};
-
-static void app_dtor(void *obj)
-{
-	struct app *app = obj;
-
-	ao2_cleanup(app->data);
-	app->data = NULL;
-}
-
-/*! Constructor for \ref app. */
-static struct app *app_create(const char *name, stasis_app_cb handler, void *data)
-{
-	struct app *app;
-	size_t size;
-
-	ast_assert(name != NULL);
-	ast_assert(handler != NULL);
-
-	size = sizeof(*app) + strlen(name) + 1;
-	app = ao2_alloc_options(size, app_dtor, AO2_ALLOC_OPT_LOCK_MUTEX);
-
-	if (!app) {
-		return NULL;
-	}
-
-	strncpy(app->name, name, size - sizeof(*app));
-	app->handler = handler;
-	ao2_ref(data, +1);
-	app->data = data;
-
-	return app;
-}
-
-/*! AO2 hash function for \ref app */
-static int app_hash(const void *obj, const int flags)
-{
-	const struct app *app = obj;
-	const char *name = flags & OBJ_KEY ? obj : app->name;
-
-	return ast_str_hash(name);
-}
-
-/*! AO2 comparison function for \ref app */
-static int app_compare(void *lhs, void *rhs, int flags)
-{
-	const struct app *lhs_app = lhs;
-	const struct 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 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 app *app, struct ast_json *message)
-{
-	app->handler(app->data, app->name, message);
-}
-
-typedef void* (*stasis_app_command_cb)(struct stasis_app_control *control,
-				       struct ast_channel *chan,
-				       void *data);
-
-struct stasis_app_command {
-	ast_mutex_t lock;
-	ast_cond_t condition;
-	stasis_app_command_cb callback;
-	void *data;
-	void *retval;
-	int is_done:1;
-};
-
-static void command_dtor(void *obj)
-{
-	struct stasis_app_command *command = obj;
-	ast_mutex_destroy(&command->lock);
-	ast_cond_destroy(&command->condition);
-}
-
-static struct stasis_app_command *command_create(stasis_app_command_cb callback,
-						 void *data)
-{
-	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
-
-	command = ao2_alloc(sizeof(*command), command_dtor);
-	if (!command) {
-		return NULL;
-	}
-
-	ast_mutex_init(&command->lock);
-	ast_cond_init(&command->condition, 0);
-	command->callback = callback;
-	command->data = data;
-
-	ao2_ref(command, +1);
-	return command;
-}
-
-static void command_complete(struct stasis_app_command *command, void *retval)
-{
-	SCOPED_MUTEX(lock, &command->lock);
-
-	command->is_done = 1;
-	command->retval = retval;
-	ast_cond_signal(&command->condition);
-}
-
-static void *wait_for_command(struct stasis_app_command *command)
-{
-	SCOPED_MUTEX(lock, &command->lock);
-	while (!command->is_done) {
-		ast_cond_wait(&command->condition, &command->lock);
-	}
-
-	return command->retval;
-}
-
-struct stasis_app_control {
-	/*! Queue of commands to dispatch on the channel */
-	struct ao2_container *command_queue;
-	/*!
-	 * When set, /c app_stasis should exit and continue in the dialplan.
-	 */
-	int continue_to_dialplan:1;
-	/*! Uniqueid of the associated channel */
-	char channel_id[];
-};
-
-static struct stasis_app_control *control_create(const char *uniqueid)
-{
-	struct stasis_app_control *control;
-	size_t size;
-
-	size = sizeof(*control) + strlen(uniqueid) + 1;
-	control = ao2_alloc(size, NULL);
-	if (!control) {
-		return NULL;
-	}
-
-	control->command_queue = ao2_container_alloc_list(0, 0, NULL, NULL);
-
-	strncpy(control->channel_id, uniqueid, size - sizeof(*control));
-
-	return control;
-}
-
-static void *exec_command(struct stasis_app_control *control,
-			  struct stasis_app_command *command)
-{
-        ao2_lock(control);
-	ao2_ref(command, +1);
-	ao2_link(control->command_queue, command);
-        ao2_unlock(control);
-
-	return wait_for_command(command);
-}
-
-/*! AO2 hash function for \ref stasis_app_control */
-static int control_hash(const void *obj, const int flags)
-{
-	const struct stasis_app_control *control = obj;
-	const char *id = flags & OBJ_KEY ? obj : control->channel_id;
-
-	return ast_str_hash(id);
-}
-
-/*! AO2 comparison function for \ref stasis_app_control */
-static int control_compare(void *lhs, void *rhs, int flags)
-{
-	const struct stasis_app_control *lhs_control = lhs;
-	const struct stasis_app_control *rhs_control = rhs;
-	const char *rhs_name =
-		flags & OBJ_KEY ? rhs : rhs_control->channel_id;
-
-	if (strcmp(lhs_control->channel_id, rhs_name) == 0) {
-		return CMP_MATCH | CMP_STOP;
-	} else {
-		return 0;
-	}
-}
-
-struct stasis_app_control *stasis_app_control_find_by_channel(
-	const struct ast_channel *chan)
-{
-	if (chan == NULL) {
-		return NULL;
-	}
-
-	return stasis_app_control_find_by_channel_id(
-		ast_channel_uniqueid(chan));
-}
-
-struct stasis_app_control *stasis_app_control_find_by_channel_id(
-	const char *channel_id)
-{
-	RAII_VAR(struct ao2_container *, controls, NULL, ao2_cleanup);
-	controls = app_controls();
-	return ao2_find(controls, channel_id, OBJ_KEY);
-}
-
-/*!
- * \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 control_continue_test_and_reset(struct stasis_app_control *control)
-{
-        int r;
-        SCOPED_AO2LOCK(lock, control);
-
-        r = control->continue_to_dialplan;
-        control->continue_to_dialplan = 0;
-        return r;
-}
-
-void stasis_app_control_continue(struct stasis_app_control *control)
-{
-	SCOPED_AO2LOCK(lock, control);
-	control->continue_to_dialplan = 1;
-}
-
-static int OK = 0;
-static int FAIL = -1;
-
-static void *__app_control_answer(struct stasis_app_control *control,
-				  struct ast_channel *chan, void *data)
-{
-	ast_debug(3, "%s: Answering", control->channel_id);
-	return __ast_answer(chan, 0, 1) == 0 ? &OK : &FAIL;
-}
-
-int stasis_app_control_answer(struct stasis_app_control *control)
-{
-	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
-	int *retval;
-
-	ast_debug(3, "%s: Sending answer command\n", control->channel_id);
-
-	command = command_create(__app_control_answer, NULL);
-	retval = exec_command(control, command);
-
-	if (*retval != 0) {
-		ast_log(LOG_WARNING, "Failed to answer channel");
-	}
-
-	return *retval;
-}
-
-static struct ast_json *app_event_create(
-	const char *event_name,
-	const struct ast_channel_snapshot *snapshot,
-	const struct ast_json *extra_info)
-{
-	RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
-	RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
-
-	if (extra_info) {
-		event = ast_json_deep_copy(extra_info);
-	} else {
-		event = ast_json_object_create();
-	}
-
-	if (snapshot) {
-		int ret;
-
-		/* Mustn't already have a channel field */
-		ast_assert(ast_json_object_get(event, "channel") == NULL);
-
-		ret = ast_json_object_set(
-			event,
-			"channel", ast_channel_snapshot_to_json(snapshot));
-		if (ret != 0) {
-			return NULL;
-		}
-	}
-
-	message = ast_json_pack("{s: o}", event_name, ast_json_ref(event));
-
-	return ast_json_ref(message);
-}
-
-static int send_start_msg(struct app *app, struct ast_channel *chan,
-			  int argc, char *argv[])
-{
-	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
-	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
-
-	struct ast_json *json_args;
-	int i;
-
-	ast_assert(chan != NULL);
-
-	/* Set channel info */
-	snapshot = ast_channel_snapshot_create(chan);
-	if (!snapshot) {
-		return -1;
-	}
-
-	msg = ast_json_pack("{s: {s: [], s: o}}",
-			    "stasis-start",
-			    "args",
-			    "channel", ast_channel_snapshot_to_json(snapshot));
-
-	if (!msg) {
-		return -1;
-	}
-
-	/* Append arguments to args array */
-	json_args = ast_json_object_get(
-		ast_json_object_get(msg, "stasis-start"),
-		"args");
-	ast_assert(json_args != NULL);
-	for (i = 0; i < argc; ++i) {
-		int r = ast_json_array_append(json_args,
-					      ast_json_string_create(argv[i]));
-		if (r != 0) {
-			ast_log(LOG_ERROR, "Error appending start message\n");
-			return -1;
-		}
-	}
-
-	app_send(app, msg);
-	return 0;
-}
-
-static int send_end_msg(struct 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);
-
-	/* Set channel info */
-	snapshot = ast_channel_snapshot_create(chan);
-	if (snapshot == NULL) {
-		return -1;
-	}
-	msg = app_event_create("stasis-end", snapshot, NULL);
-	if (!msg) {
-		return -1;
-	}
-
-	app_send(app, msg);
-	return 0;
-}
-
-static void sub_handler(void *data, struct stasis_subscription *sub,
-			struct stasis_topic *topic,
-			struct stasis_message *message)
-{
-	struct app *app = data;
-	if (ast_channel_snapshot_type() == stasis_message_type(message)) {
-		RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
-		struct ast_channel_snapshot *snapshot =
-			stasis_message_data(message);
-
-		msg = app_event_create("channel-state-change", snapshot, NULL);
-		if (!msg) {
-			return;
-		}
-		app_send(app, msg);
-	}
-	if (stasis_subscription_final_message(sub, message)) {
-		ao2_cleanup(data);
-	}
-}
-
-/*!
- * \brief In addition to running ao2_cleanup(), this function also removes the
- * object from the app_controls() container.
- */
-static void control_unlink(struct stasis_app_control *control)
-{
-	RAII_VAR(struct ao2_container *, controls, NULL, ao2_cleanup);
-
-	if (!control) {
-		return;
-	}
-
-	controls = app_controls();
-	ao2_unlink_flags(controls, control,
-			 OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
-	ao2_cleanup(control);
-}
-
-static void dispatch_commands(struct stasis_app_control *control,
-			      struct ast_channel *chan)
-{
-	struct ao2_iterator i;
-	void *obj;
-
-        SCOPED_AO2LOCK(lock, control);
-
-	i = ao2_iterator_init(control->command_queue, AO2_ITERATOR_UNLINK);
-
-	while ((obj = ao2_iterator_next(&i))) {
-		RAII_VAR(struct stasis_app_command *, command, obj, ao2_cleanup);
-		void *retval = command->callback(control, chan, command->data);
-		command_complete(command, retval);
-	}
-
-	ao2_iterator_destroy(&i);
-}
-
-
-/*! /brief Stasis dialplan application callback */
-static int app_stasis_exec(struct ast_channel *chan, const char *data)
-{
-	RAII_VAR(struct ao2_container *, apps, apps_registry(), ao2_cleanup);
-	RAII_VAR(struct app *, app, NULL, ao2_cleanup);
-	RAII_VAR(struct stasis_app_control *, control, NULL, control_unlink);
-	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);
@@ -556,192 +85,21 @@
 		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;
-	}
-
-	{
-		RAII_VAR(struct ao2_container *, controls, NULL, ao2_cleanup);
-
-		controls = app_controls();
-		control = control_create(ast_channel_uniqueid(chan));
-		if (!control) {
-			ast_log(LOG_ERROR, "Allocated failed\n");
-			return -1;
-		}
-		ao2_link(controls, control);
-	}
-
-	subscription =
-		stasis_subscribe(ast_channel_topic(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;
-	}
-	ao2_ref(app, +1); /* subscription now has a reference */
-
-	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 (1) {
-		RAII_VAR(struct ast_frame *, f, NULL, ast_frame_dtor);
-		int r;
-
-		if (hungup) {
-			ast_debug(3, "%s: Hangup\n",
-				  ast_channel_uniqueid(chan));
-			break;
-		}
-
-		if (control_continue_test_and_reset(control)) {
-			ast_debug(3, "%s: Continue\n",
-				  ast_channel_uniqueid(chan));
-			break;
-		}
-
-		r = ast_waitfor(chan, MAX_WAIT_MS);
-
-		if (r < 0) {
-			ast_debug(3, "%s: Poll error\n",
-				  ast_channel_uniqueid(chan));
-			break;
-		}
-
-		dispatch_commands(control, chan);
-
-		if (r == 0) {
-			/* Timeout */
-			continue;
-		}
-
-		f = ast_read(chan);
-		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) {
-				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, apps_registry(), ao2_cleanup);
-	RAII_VAR(struct 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_cb handler, void *data)
-{
-	RAII_VAR(struct ao2_container *, apps, apps_registry(), ao2_cleanup);
-	RAII_VAR(struct 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 = app_event_create("application-replaced", NULL, NULL);
-		app->handler(app->data, app_name, msg);
-
-		app->handler = handler;
-		ao2_cleanup(app->data);
-		ao2_ref(data, +1);
-		app->data = data;
-	} else {
-		app = app_create(app_name, handler, data);
-		if (app) {
-			ao2_link_flags(apps, app, OBJ_NOLOCK);
-		} else {
-			return -1;
-		}
-	}
-
-	return 0;
-}
-
-void stasis_app_unregister(const char *app_name)
-{
-	RAII_VAR(struct ao2_container *, apps, NULL, ao2_cleanup);
-
-	if (app_name) {
-		apps = apps_registry();
-		ao2_cleanup(ao2_find(apps, app_name, OBJ_KEY | OBJ_UNLINK));
-	}
+	return stasis_app_exec(
+		chan, args.app_name, args.argc - 1, args.app_argv);
 }
 
 static int load_module(void)
 {
 	int r = 0;
 
-	__apps_registry =
-		ao2_container_alloc(APPS_NUM_BUCKETS, app_hash, app_compare);
-	if (__apps_registry == NULL) {
-		return AST_MODULE_LOAD_FAILURE;
-	}
-
-	__app_controls = ao2_container_alloc(CONTROLS_NUM_BUCKETS,
-					     control_hash, control_compare);
-	if (__app_controls == NULL) {
-		return AST_MODULE_LOAD_FAILURE;
-	}
-
-	r |= ast_register_application_xml(stasis, app_stasis_exec);
+	r |= ast_register_application_xml(stasis, app_exec);
 	return r;
 }
 
 static int unload_module(void)
 {
 	int r = 0;
-
-	ao2_cleanup(__apps_registry);
-	__apps_registry = NULL;
-
-	ao2_cleanup(__app_controls);
-	__app_controls = NULL;
 
 	r |= ast_unregister_application(stasis);
 	return r;

Modified: team/dlee/stasis-http/include/asterisk/cli.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/cli.h?view=diff&rev=385787&r1=385786&r2=385787
==============================================================================
--- team/dlee/stasis-http/include/asterisk/cli.h (original)
+++ team/dlee/stasis-http/include/asterisk/cli.h Mon Apr 15 13:04:26 2013
@@ -28,6 +28,7 @@
 #endif
 
 #include "asterisk/linkedlists.h"
+#include "asterisk/strings.h"
 
 void ast_cli(int fd, const char *fmt, ...)
 	__attribute__((format(printf, 2, 3)));
@@ -57,12 +58,17 @@
  */
 #define ESS(x) ((x) == 1 ? "" : "s")
 
-/*! \brief return Yes or No depending on the argument.
- * This is used in many places in CLI command, having a function to generate
- * this helps maintaining a consistent output (and possibly emitting the
- * output in other languages, at some point).
- */
-#define AST_CLI_YESNO(x) (x) ? "Yes" : "No"
+/*!
+ * \brief Return Yes or No depending on the argument.
+ *
+ * Note that this should probably still be used for CLI commands instead of
+ * AST_YESNO(), in the off chance we someday want to translate the CLI.
+ *
+ * \param x Boolean value
+ * \return "Yes" if x is true (non-zero)
+ * \return "No" if x is false (zero)
+ */
+#define AST_CLI_YESNO(x) AST_YESNO(x)
 
 /*! \brief return On or Off depending on the argument.
  * This is used in many places in CLI command, having a function to generate

Modified: team/dlee/stasis-http/include/asterisk/json.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/json.h?view=diff&rev=385787&r1=385786&r2=385787
==============================================================================
--- team/dlee/stasis-http/include/asterisk/json.h (original)
+++ team/dlee/stasis-http/include/asterisk/json.h Mon Apr 15 13:04:26 2013
@@ -815,7 +815,7 @@
  * \return JSON string with ISO 8601 formatted date/time.
  * \return \c NULL on error.
  */
-struct ast_json *ast_json_timeval(const struct timeval *tv, const char *zone);
+struct ast_json *ast_json_timeval(const struct timeval tv, const char *zone);
 
 /*!
  * \brief Construct a context/exten/priority as JSON.

Copied: team/dlee/stasis-http/include/asterisk/stasis_app.h (from r385743, trunk/include/asterisk/stasis_app.h)
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/stasis_app.h?view=diff&rev=385787&p1=trunk/include/asterisk/stasis_app.h&r1=385743&p2=team/dlee/stasis-http/include/asterisk/stasis_app.h&r2=385787
==============================================================================
--- trunk/include/asterisk/stasis_app.h (original)
+++ team/dlee/stasis-http/include/asterisk/stasis_app.h Mon Apr 15 13:04:26 2013
@@ -66,7 +66,7 @@
  * \param argv Arguments for the application.
  */
 int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
-		    char *argv[]);
+	char *argv[]);
 
 /*! @} */
 
@@ -126,22 +126,50 @@
 struct stasis_app_control;
 
 /*!
- * \brief Returns the handler for the given channel
+ * \brief Returns the handler for the given channel.
  * \param chan Channel to handle.
- * \return NULL channel not in Stasis application
- * \return Pointer to stasis handler.
+ * \return NULL channel not in Stasis application.
+ * \return Pointer to \c res_stasis handler.
  */
 struct stasis_app_control *stasis_app_control_find_by_channel(
 	const struct ast_channel *chan);
 
 /*!
- * \brief Exit \c app_stasis and continue execution in the dialplan.
+ * \brief Returns the handler for the channel with the given id.
+ * \param channel_id Uniqueid of the channel.
+ * \return NULL channel not in Stasis application, or channel does not exist.
+ * \return Pointer to \c res_stasis handler.
+ */
+struct stasis_app_control *stasis_app_control_find_by_channel_id(
+	const char *channel_id);
+
+/*!
+ * \brief Exit \c res_stasis and continue execution in the dialplan.
  *
- * If the channel is no longer in \c app_stasis, this function does nothing.
+ * If the channel is no longer in \c res_stasis, this function does nothing.
  *
- * \param handler Handler for \c app_stasis
+ * \param control Control for \c res_stasis
  */
-void stasis_app_control_continue(struct stasis_app_control *handler);
+void stasis_app_control_continue(struct stasis_app_control *control);
+
+/*!
+ * \brief Answer the channel associated with this control.
+ * \param control Control for \c res_stasis.
+ * \return 0 for success.
+ * \return -1 for error.
+ */
+int stasis_app_control_answer(struct stasis_app_control *control);
+
+/*! @} */
+
+/*! @{ */
+
+/*!
+ * \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(const struct ast_channel_snapshot *snapshot);
 
 /*! @} */
 

Modified: team/dlee/stasis-http/include/asterisk/stasis_channels.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/stasis_channels.h?view=diff&rev=385787&r1=385786&r2=385787
==============================================================================
--- team/dlee/stasis-http/include/asterisk/stasis_channels.h (original)
+++ team/dlee/stasis-http/include/asterisk/stasis_channels.h Mon Apr 15 13:04:26 2013
@@ -292,6 +292,13 @@
 /*! @} */
 
 /*!
+ * \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(const struct ast_channel_snapshot *snapshot);
+
+/*!
  * \brief Dispose of the stasis channel topics and message types
  */
 void ast_stasis_channels_shutdown(void);

Modified: team/dlee/stasis-http/include/asterisk/strings.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/include/asterisk/strings.h?view=diff&rev=385787&r1=385786&r2=385787
==============================================================================
--- team/dlee/stasis-http/include/asterisk/strings.h (original)
+++ team/dlee/stasis-http/include/asterisk/strings.h Mon Apr 15 13:04:26 2013
@@ -125,6 +125,18 @@
 }
 
 /*!
+ * \brief return Yes or No depending on the argument.
+ *
+ * Note that this macro is used my AMI, where a literal "Yes" and "No" are
+ * expected, and translations would cause problems.
+ *
+ * \param x Boolean value
+ * \return "Yes" if x is true (non-zero)
+ * \return "No" if x is false (zero)
+ */
+#define AST_YESNO(x) ((x) ? "Yes" : "No")
+
+/*!
   \brief Gets a pointer to the first non-whitespace character in a string.
   \param str the input string
   \return a pointer to the first non-whitespace character

Modified: team/dlee/stasis-http/main/channel.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/main/channel.c?view=diff&rev=385787&r1=385786&r2=385787
==============================================================================
--- team/dlee/stasis-http/main/channel.c (original)
+++ team/dlee/stasis-http/main/channel.c Mon Apr 15 13:04:26 2013
@@ -3714,42 +3714,56 @@
 	return 0; /* Time is up */
 }
 
-static void send_dtmf_event(struct ast_channel *chan, const char *direction, const char digit, const char *begin, const char *end)
-{
-	/*** DOCUMENTATION
-		<managerEventInstance>
-			<synopsis>Raised when a DTMF digit has started or ended on a channel.</synopsis>
-				<syntax>
-					<parameter name="Direction">
-						<enumlist>
-							<enum name="Received"/>
-							<enum name="Sent"/>
-						</enumlist>
-					</parameter>
-					<parameter name="Begin">
-						<enumlist>
-							<enum name="Yes"/>
-							<enum name="No"/>
-						</enumlist>
-					</parameter>
-					<parameter name="End">
-						<enumlist>
-							<enum name="Yes"/>
-							<enum name="No"/>
-						</enumlist>
-					</parameter>
-				</syntax>
-		</managerEventInstance>
-	***/
-	ast_manager_event(chan, EVENT_FLAG_DTMF,
-			"DTMF",
-			"Channel: %s\r\n"
-			"Uniqueid: %s\r\n"
-			"Digit: %c\r\n"
-			"Direction: %s\r\n"
-			"Begin: %s\r\n"
-			"End: %s\r\n",
-			ast_channel_name(chan), ast_channel_uniqueid(chan), digit, direction, begin, end);
+enum DtmfDirection {
+	DTMF_RECEIVED,
+	DTMF_SENT
+};
+
+static const char *dtmf_direction_to_string(enum DtmfDirection direction)
+{
+	switch (direction) {
+	case DTMF_RECEIVED:
+		return "Received";
+	case DTMF_SENT:
+		return "Sent";
+	}
+
+	return "?";
+}
+
+static void send_dtmf_begin_event(struct ast_channel *chan,
+	enum DtmfDirection direction, const char digit)
+{
+	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+	char digit_str[] = { digit, '\0' };
+
+	blob = ast_json_pack("{ s: s, s: s, s: s }",
+		"type", "dtmf_begin",
+		"digit", digit_str,
+		"direction", dtmf_direction_to_string(direction));
+	if (!blob) {
+		return;
+	}
+
+	publish_channel_blob(chan, blob);
+}
+
+static void send_dtmf_end_event(struct ast_channel *chan,
+	enum DtmfDirection direction, const char digit, long duration_ms)
+{
+	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+	char digit_str[] = { digit, '\0' };
+
+	blob = ast_json_pack("{ s: s, s: s, s: s, s: i }",
+		"type", "dtmf_end",
+		"digit", digit_str,
+		"direction", dtmf_direction_to_string(direction),
+		"duration_ms", duration_ms);
+	if (!blob) {
+		return;
+	}
+
+	publish_channel_blob(chan, blob);
 }
 
 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
@@ -4104,7 +4118,7 @@
 			}
 			break;
 		case AST_FRAME_DTMF_END:
-			send_dtmf_event(chan, "Received", f->subclass.integer, "No", "Yes");
+			send_dtmf_end_event(chan, DTMF_RECEIVED, f->subclass.integer, f->len);
 			ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, ast_channel_name(chan), f->len);
 			/* Queue it up if DTMF is deferred, or if DTMF emulation is forced. */
 			if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF) || ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
@@ -4190,7 +4204,7 @@
 			}
 			break;
 		case AST_FRAME_DTMF_BEGIN:
-			send_dtmf_event(chan, "Received", f->subclass.integer, "Yes", "No");
+			send_dtmf_begin_event(chan, DTMF_RECEIVED, f->subclass.integer);
 			ast_log(LOG_DTMF, "DTMF begin '%c' received on %s\n", f->subclass.integer, ast_channel_name(chan));
 			if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_END_DTMF_ONLY | AST_FLAG_EMULATE_DTMF) ||
 			    (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
@@ -5042,7 +5056,7 @@
 			if (old_frame != fr)
 				f = fr;
 		}
-		send_dtmf_event(chan, "Sent", fr->subclass.integer, "Yes", "No");
+		send_dtmf_begin_event(chan, DTMF_SENT, fr->subclass.integer);
 		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
 		ast_channel_unlock(chan);
 		res = ast_senddigit_begin(chan, fr->subclass.integer);
@@ -5058,7 +5072,7 @@
 				ast_frfree(new_frame);
 			}
 		}
-		send_dtmf_event(chan, "Sent", fr->subclass.integer, "No", "Yes");
+		send_dtmf_end_event(chan, DTMF_SENT, fr->subclass.integer, fr->len);
 		ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
 		ast_channel_unlock(chan);
 		res = ast_senddigit_end(chan, fr->subclass.integer, fr->len);

Modified: team/dlee/stasis-http/main/json.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/main/json.c?view=diff&rev=385787&r1=385786&r2=385787
==============================================================================
--- team/dlee/stasis-http/main/json.c (original)
+++ team/dlee/stasis-http/main/json.c Mon Apr 15 13:04:26 2013
@@ -514,16 +514,12 @@
 			     "priority", priority != -1 ? ast_json_integer_create(priority) : ast_json_null());
 }
 
-struct ast_json *ast_json_timeval(const struct timeval *tv, const char *zone)
+struct ast_json *ast_json_timeval(const 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_localtime(&tv, &tm, zone);
 
 	ast_strftime(buf, sizeof(buf),AST_ISO8601_FORMAT, &tm);
 

Modified: team/dlee/stasis-http/main/manager_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/main/manager_channels.c?view=diff&rev=385787&r1=385786&r2=385787
==============================================================================
--- team/dlee/stasis-http/main/manager_channels.c (original)
+++ team/dlee/stasis-http/main/manager_channels.c Mon Apr 15 13:04:26 2013
@@ -275,8 +275,8 @@
 		struct ast_var_t *var;
 		AST_LIST_TRAVERSE(snapshot->manager_vars, var, entries) {
 			ast_str_append(&out, 0, "ChanVariable%s: %s=%s\r\n",
-						   suffix,
-						   var->name, var->value);
+				       suffix,
+				       var->name, var->value);
 		}
 	}
 
@@ -657,6 +657,90 @@
 		      ast_str_buffer(extra));
 }
 
+static void channel_dtmf_begin(struct ast_channel_blob *obj)
+{
+	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
+	const char *digit =
+		ast_json_string_get(ast_json_object_get(obj->blob, "digit"));
+	const char *direction =
+		ast_json_string_get(ast_json_object_get(obj->blob, "direction"));
+
+	channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
+
+	if (!channel_event_string) {
+		return;
+	}
+
+	/*** DOCUMENTATION
+		<managerEventInstance>
+			<synopsis>Raised when a DTMF digit has started on a channel.</synopsis>
+				<syntax>
+					<xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
+					<parameter name="Digit">
+						<para>DTMF digit received or transmitted (0-9, A-E, # or *</para>
+					</parameter>
+					<parameter name="Direction">
+						<enumlist>
+							<enum name="Received"/>
+							<enum name="Sent"/>
+						</enumlist>
+					</parameter>
+				</syntax>
+		</managerEventInstance>
+	***/
+	manager_event(EVENT_FLAG_DTMF, "DTMFBegin",
+		"%s"
+		"Digit: %s\r\n"
+		"Direction: %s\r\n",
+		ast_str_buffer(channel_event_string),
+		digit, direction);
+}
+
+static void channel_dtmf_end(struct ast_channel_blob *obj)
+{
+	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
+	const char *digit =
+		ast_json_string_get(ast_json_object_get(obj->blob, "digit"));
+	const char *direction =
+		ast_json_string_get(ast_json_object_get(obj->blob, "direction"));
+	long duration_ms =
+		ast_json_integer_get(ast_json_object_get(obj->blob, "duration_ms"));
+
+	channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
+
+	if (!channel_event_string) {
+		return;
+	}
+
+	/*** DOCUMENTATION
+		<managerEventInstance>
+			<synopsis>Raised when a DTMF digit has ended on a channel.</synopsis>
+				<syntax>
+					<xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
+					<parameter name="Digit">
+						<para>DTMF digit received or transmitted (0-9, A-E, # or *</para>
+					</parameter>
+					<parameter name="DurationMs">
+						<para>Duration (in milliseconds) DTMF was sent/received</para>
+					</parameter>
+					<parameter name="Direction">
+						<enumlist>
+							<enum name="Received"/>
+							<enum name="Sent"/>
+						</enumlist>
+					</parameter>
+				</syntax>
+		</managerEventInstance>
+	***/
+	manager_event(EVENT_FLAG_DTMF, "DTMFEnd",
+		"%s"
+		"Digit: %s\r\n"
+		"DurationMs: %ld\r\n"
+		"Direction: %s\r\n",
+		ast_str_buffer(channel_event_string),
+		digit, duration_ms, direction);
+}
+
 /*!
  * \brief Callback processing messages on the channel topic.
  */
@@ -672,6 +756,10 @@
 		channel_userevent(obj);
 	} else if (strcmp("hangup_request", ast_channel_blob_json_type(obj)) == 0) {
 		channel_hangup_request(obj);
+	} else if (strcmp("dtmf_begin", ast_channel_blob_json_type(obj)) == 0) {
+		channel_dtmf_begin(obj);
+	} else if (strcmp("dtmf_end", ast_channel_blob_json_type(obj)) == 0) {
+		channel_dtmf_end(obj);
 	}
 }
 

Modified: team/dlee/stasis-http/main/stasis_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/main/stasis_channels.c?view=diff&rev=385787&r1=385786&r2=385787
==============================================================================
--- team/dlee/stasis-http/main/stasis_channels.c (original)
+++ team/dlee/stasis-http/main/stasis_channels.c Mon Apr 15 13:04:26 2013
@@ -458,6 +458,37 @@
 	publish_message_for_channel_topics(msg, chan);
 }
 
+struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot)
+{
+	RAII_VAR(struct ast_json *, json_chan, NULL, ast_json_unref);
+
+	if (snapshot == NULL) {
+		return NULL;
+	}
+
+	json_chan = ast_json_pack("{ s: s, s: s, s: s, s: s, s: s, s: s, s: s,"
+				  "  s: s, s: s, s: s, s: s, s: o, s: o, s: o,"
+				  "  s: o"
+				  "}",
+				  "name", snapshot->name,
+				  "state", ast_state2str(snapshot->state),
+				  "accountcode", snapshot->accountcode,
+				  "peeraccount", snapshot->peeraccount,
+				  "userfield", snapshot->userfield,
+				  "uniqueid", snapshot->uniqueid,
+				  "linkedid", snapshot->linkedid,
+				  "parkinglot", snapshot->parkinglot,
+				  "hangupsource", snapshot->hangupsource,
+				  "appl", snapshot->appl,
+				  "data", snapshot->data,
+				  "dialplan", ast_json_dialplan_cep(snapshot->context, snapshot->exten, snapshot->priority),
+				  "caller", ast_json_name_number(snapshot->caller_name, snapshot->caller_number),
+				  "connected", ast_json_name_number(snapshot->connected_name, snapshot->connected_number),
+				  "creationtime", ast_json_timeval(snapshot->creationtime, NULL));
+
+	return ast_json_ref(json_chan);
+}
+
 void ast_stasis_channels_shutdown(void)
 {
 	ao2_cleanup(channel_snapshot_type);

Copied: team/dlee/stasis-http/res/res_stasis.c (from r385743, trunk/res/res_stasis.c)
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-http/res/res_stasis.c?view=diff&rev=385787&p1=trunk/res/res_stasis.c&r1=385743&p2=team/dlee/stasis-http/res/res_stasis.c&r2=385787
==============================================================================
--- trunk/res/res_stasis.c (original)
+++ team/dlee/stasis-http/res/res_stasis.c Mon Apr 15 13:04:26 2013
@@ -40,6 +40,9 @@
 #include "asterisk/stasis_channels.h"
 #include "asterisk/strings.h"
 
+/*! Time to wait for a frame in the application */
+#define MAX_WAIT_MS 200
+
 /*!
  * \brief Number of buckets for the Stasis application hash table.  Remember to
  * keep it a prime number!
@@ -147,7 +150,67 @@
 	app->handler(app->data, app->name, message);
 }
 
+typedef void* (*stasis_app_command_cb)(struct stasis_app_control *control,
+				       struct ast_channel *chan,
+				       void *data);
+
+struct stasis_app_command {
+	ast_mutex_t lock;
+	ast_cond_t condition;
+	stasis_app_command_cb callback;
+	void *data;
+	void *retval;
+	int is_done:1;
+};
+
+static void command_dtor(void *obj)
+{
+	struct stasis_app_command *command = obj;
+	ast_mutex_destroy(&command->lock);
+	ast_cond_destroy(&command->condition);
+}
+
+static struct stasis_app_command *command_create(stasis_app_command_cb callback,
+						 void *data)
+{
+	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
+
+	command = ao2_alloc(sizeof(*command), command_dtor);
+	if (!command) {
+		return NULL;
+	}
+
+	ast_mutex_init(&command->lock);
+	ast_cond_init(&command->condition, 0);
+	command->callback = callback;
+	command->data = data;
+
+	ao2_ref(command, +1);
+	return command;
+}
+
+static void command_complete(struct stasis_app_command *command, void *retval)
+{
+	SCOPED_MUTEX(lock, &command->lock);
+
+	command->is_done = 1;
+	command->retval = retval;
+	ast_cond_signal(&command->condition);
+}
+
+static void *wait_for_command(struct stasis_app_command *command)
+{
+	SCOPED_MUTEX(lock, &command->lock);
+	while (!command->is_done) {
+		ast_cond_wait(&command->condition, &command->lock);
+	}
+
+	return command->retval;
+}
+
 struct stasis_app_control {
+	/*! Queue of commands to dispatch on the channel */
+	struct ao2_container *command_queue;
 	/*!

[... 332 lines stripped ...]



More information about the asterisk-commits mailing list