[asterisk-commits] sgriepentrog: branch sgriepentrog/12-userevent r413025 - in /team/sgriepentro...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Apr 25 15:28:01 CDT 2014


Author: sgriepentrog
Date: Fri Apr 25 15:27:54 2014
New Revision: 413025

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=413025
Log:
Current working version - has json ref issue

Modified:
    team/sgriepentrog/12-userevent/apps/app_userevent.c
    team/sgriepentrog/12-userevent/include/asterisk/stasis_app.h
    team/sgriepentrog/12-userevent/include/asterisk/stasis_channels.h
    team/sgriepentrog/12-userevent/main/asterisk.c
    team/sgriepentrog/12-userevent/main/manager_channels.c
    team/sgriepentrog/12-userevent/main/stasis_channels.c
    team/sgriepentrog/12-userevent/res/ari/ari_model_validators.c
    team/sgriepentrog/12-userevent/res/ari/ari_model_validators.h
    team/sgriepentrog/12-userevent/res/ari/resource_applications.c
    team/sgriepentrog/12-userevent/res/ari/resource_applications.h
    team/sgriepentrog/12-userevent/res/res_ari_applications.c
    team/sgriepentrog/12-userevent/res/res_stasis.c
    team/sgriepentrog/12-userevent/res/stasis/app.c
    team/sgriepentrog/12-userevent/rest-api/api-docs/applications.json
    team/sgriepentrog/12-userevent/rest-api/api-docs/events.json

Modified: team/sgriepentrog/12-userevent/apps/app_userevent.c
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/apps/app_userevent.c?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/apps/app_userevent.c (original)
+++ team/sgriepentrog/12-userevent/apps/app_userevent.c Fri Apr 25 15:27:54 2014
@@ -35,6 +35,7 @@
 #include "asterisk/app.h"
 #include "asterisk/json.h"
 #include "asterisk/stasis_channels.h"
+#include "asterisk/stasis_user.h"
 
 /*** DOCUMENTATION
 	<application name="UserEvent" language="en_US">
@@ -115,7 +116,7 @@
 	}
 
 	ast_channel_lock(chan);
-	ast_channel_publish_blob(chan, ast_channel_user_event_type(), blob);
+	ast_multi_object_blob_single_channel_publish(chan, ast_multi_user_event_type(), blob);
 	ast_channel_unlock(chan);
 	return 0;
 }

Modified: team/sgriepentrog/12-userevent/include/asterisk/stasis_app.h
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/include/asterisk/stasis_app.h?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/include/asterisk/stasis_app.h (original)
+++ team/sgriepentrog/12-userevent/include/asterisk/stasis_app.h Fri Apr 25 15:27:54 2014
@@ -52,6 +52,7 @@
 
 #include "asterisk/channel.h"
 #include "asterisk/json.h"
+#include "asterisk/stasis_user.h"
 
 /*! @{ */
 
@@ -188,6 +189,14 @@
 	 */
 	void (*to_json)(const struct stasis_app *app, struct ast_json *json);
 
+	/*!
+	 * \brief Add event source snapshot to multi object blob user message
+	 *
+	 * \param obj an event source data object
+	 * \param blob a multi object blob
+	 */
+	void (*to_multi_object_blob)(void *obj, struct ast_multi_object_blob *blob);
+
 	/*! Next item in the list */
 	AST_LIST_ENTRY(stasis_app_event_source) next;
 };
@@ -227,6 +236,33 @@
  * \brief Unregister core event sources.
  */
 void stasis_app_unregister_event_sources(void);
+
+/*! \brief Return code for stasis_app_generate_event */
+enum stasis_app_generate_event_res {
+	STASIS_AGER_OK,
+	STASIS_AGER_APP_NOT_FOUND,
+	STASIS_AGER_EVENT_SOURCE_NOT_FOUND,
+	STASIS_AGER_EVENT_SOURCE_BAD_SCHEME,
+	STASIS_AGER_USEREVENT_INVALID,
+	STASIS_AGER_INTERNAL_ERROR,
+};
+
+/*!
+ * \brief Generate a Userevent for stasis app (echo to AMI)
+ *
+ * \param app_name Name of the application to generate event for/to.
+ * \param event_name Name of the Userevent.
+ * \param source_uris URIs for the source objects to attach to event.
+ * \param sources_count Array size of source_uris.
+ * \param userevent_data Custom parameters for the user event
+ * \param userevents_count Array size of userevent_data
+ *
+ * \return \ref stasis_app_generate_event_res return code.
+ */
+enum stasis_app_generate_event_res stasis_app_generate_event(const char *app_name,
+	const char *event_name,
+	const char **source_uris, int sources_count,
+	const char **userevent_data, int userevents_count);
 
 
 /*! \brief Return code for stasis_app_[un]subscribe */
@@ -591,6 +627,13 @@
 	struct stasis_app_control *control, struct stasis_message *message);
 
 /*!
+ * \brief Returns the stasis topic for an app
+ *
+ * \param app Stasis app to get topic of
+ */
+struct stasis_topic *ast_app_get_topic(struct stasis_app *app);
+
+/*!
  * \brief Queue a control frame without payload.
  *
  * \param control Control to publish to.

Modified: team/sgriepentrog/12-userevent/include/asterisk/stasis_channels.h
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/include/asterisk/stasis_channels.h?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/include/asterisk/stasis_channels.h (original)
+++ team/sgriepentrog/12-userevent/include/asterisk/stasis_channels.h Fri Apr 25 15:27:54 2014
@@ -381,14 +381,6 @@
 
 /*!
  * \since 12
- * \brief Message type for when a custom user event is sent on a channel.
- *
- * \retval A stasis message type
- */
-struct stasis_message_type *ast_channel_user_event_type(void);
-
-/*!
- * \since 12
  * \brief Message type for when a hangup is requested on a channel.
  *
  * \retval A stasis message type

Modified: team/sgriepentrog/12-userevent/main/asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/main/asterisk.c?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/main/asterisk.c (original)
+++ team/sgriepentrog/12-userevent/main/asterisk.c Fri Apr 25 15:27:54 2014
@@ -248,6 +248,7 @@
 #include "asterisk/stasis_system.h"
 #include "asterisk/security_events.h"
 #include "asterisk/endpoints.h"
+#include "asterisk/stasis_user.h"
 
 #include "../defaults.h"
 
@@ -4456,6 +4457,11 @@
 		exit(1);
 	}
 
+	if (ast_user_stasis_init()) {		/* Initialize User Stasis Events */
+		printf("%s", term_quit());
+		exit(1);
+	}
+
 	if ((moduleresult = load_modules(1))) {		/* Load modules, pre-load only */
 		printf("%s", term_quit());
 		exit(moduleresult == -2 ? 2 : 1);

Modified: team/sgriepentrog/12-userevent/main/manager_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/main/manager_channels.c?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/main/manager_channels.c (original)
+++ team/sgriepentrog/12-userevent/main/manager_channels.c Fri Apr 25 15:27:54 2014
@@ -36,6 +36,7 @@
 #include "asterisk/stasis_message_router.h"
 #include "asterisk/pbx.h"
 #include "asterisk/stasis_channels.h"
+#include "asterisk/stasis_user.h"
 
 /*** DOCUMENTATION
 	<managerEvent language="en_US" name="Newchannel">
@@ -629,54 +630,6 @@
 	}
 }
 
-static int userevent_exclusion_cb(const char *key)
-{
-	if (!strcmp("type", key)) {
-		return 1;
-	}
-	if (!strcmp("eventname", key)) {
-		return 1;
-	}
-	return 0;
-}
-
-static void channel_user_event_cb(void *data, struct stasis_subscription *sub,
-	struct stasis_message *message)
-{
-	struct ast_channel_blob *obj = stasis_message_data(message);
-	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
-	RAII_VAR(struct ast_str *, body, NULL, ast_free);
-	const char *eventname;
-
-	eventname = ast_json_string_get(ast_json_object_get(obj->blob, "eventname"));
-	body = ast_manager_str_from_json_object(obj->blob, userevent_exclusion_cb);
-	channel_event_string = ast_manager_build_channel_state_string(obj->snapshot);
-
-	if (!channel_event_string || !body) {
-		return;
-	}
-
-	/*** DOCUMENTATION
-		<managerEventInstance>
-			<synopsis>A user defined event raised from the dialplan.</synopsis>
-			<syntax>
-				<channel_snapshot/>
-				<parameter name="UserEvent">
-					<para>The event name, as specified in the dialplan.</para>
-				</parameter>
-			</syntax>
-			<see-also>
-				<ref type="application">UserEvent</ref>
-			</see-also>
-		</managerEventInstance>
-	***/
-	manager_event(EVENT_FLAG_USER, "UserEvent",
-		      "%s"
-		      "UserEvent: %s\r\n"
-		      "%s",
-		      ast_str_buffer(channel_event_string), eventname, ast_str_buffer(body));
-}
-
 static void publish_basic_channel_event(const char *event, int class, struct ast_channel_snapshot *snapshot)
 {
 	RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
@@ -1160,9 +1113,6 @@
 		ast_channel_snapshot_type(), channel_snapshot_update, NULL);
 
 	ret |= stasis_message_router_add(message_router,
-		ast_channel_user_event_type(), channel_user_event_cb, NULL);
-
-	ret |= stasis_message_router_add(message_router,
 		ast_channel_dtmf_begin_type(), channel_dtmf_begin_cb, NULL);
 
 	ret |= stasis_message_router_add(message_router,

Modified: team/sgriepentrog/12-userevent/main/stasis_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/main/stasis_channels.c?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/main/stasis_channels.c (original)
+++ team/sgriepentrog/12-userevent/main/stasis_channels.c Fri Apr 25 15:27:54 2014
@@ -916,28 +916,6 @@
 		"channel", json_channel);
 }
 
-static struct ast_json *user_event_to_json(
-	struct stasis_message *message,
-	const struct stasis_message_sanitizer *sanitize)
-{
-	struct ast_channel_blob *channel_blob = stasis_message_data(message);
-	struct ast_json *blob = channel_blob->blob;
-	struct ast_channel_snapshot *snapshot = channel_blob->snapshot;
-	const struct timeval *tv = stasis_message_timestamp(message);
-	struct ast_json *json_channel = ast_channel_snapshot_to_json(snapshot, sanitize);
-
-	if (!json_channel) {
-		return NULL;
-	}
-
-	return ast_json_pack("{s: s, s: o, s: O, s: O, s: o}",
-		"type", "ChannelUserevent",
-		"timestamp", ast_json_timeval(*tv, NULL),
-		"eventname", ast_json_object_get(blob, "eventname"),
-		"userevent", blob,
-		"channel", json_channel);
-}
-
 static struct ast_json *varset_to_json(
 	struct stasis_message *message,
 	const struct stasis_message_sanitizer *sanitize)
@@ -1006,9 +984,6 @@
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_varset_type,
 	.to_ami = varset_to_ami,
 	.to_json = varset_to_json,
-	);
-STASIS_MESSAGE_TYPE_DEFN(ast_channel_user_event_type,
-	.to_json = user_event_to_json,
 	);
 STASIS_MESSAGE_TYPE_DEFN(ast_channel_hangup_request_type,
 	.to_json = hangup_request_to_json,
@@ -1048,7 +1023,6 @@
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_snapshot_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dial_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_varset_type);
-	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_user_event_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hangup_request_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_begin_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_end_type);
@@ -1097,7 +1071,6 @@
 
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_dial_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_varset_type);
-	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_user_event_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_hangup_request_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_begin_type);
 	res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_end_type);

Modified: team/sgriepentrog/12-userevent/res/ari/ari_model_validators.c
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/res/ari/ari_model_validators.c?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/res/ari/ari_model_validators.c (original)
+++ team/sgriepentrog/12-userevent/res/ari/ari_model_validators.c Fri Apr 25 15:27:54 2014
@@ -3076,7 +3076,6 @@
 	struct ast_json_iter *iter;
 	int has_type = 0;
 	int has_application = 0;
-	int has_channel = 0;
 	int has_eventname = 0;
 	int has_userevent = 0;
 
@@ -3112,7 +3111,6 @@
 		} else
 		if (strcmp("channel", ast_json_object_iter_key(iter)) == 0) {
 			int prop_is_valid;
-			has_channel = 1;
 			prop_is_valid = ast_ari_validate_channel(
 				ast_json_object_iter_value(iter));
 			if (!prop_is_valid) {
@@ -3155,11 +3153,6 @@
 
 	if (!has_application) {
 		ast_log(LOG_ERROR, "ARI ChannelUserevent missing required field application\n");
-		res = 0;
-	}
-
-	if (!has_channel) {
-		ast_log(LOG_ERROR, "ARI ChannelUserevent missing required field channel\n");
 		res = 0;
 	}
 

Modified: team/sgriepentrog/12-userevent/res/ari/ari_model_validators.h
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/res/ari/ari_model_validators.h?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/res/ari/ari_model_validators.h (original)
+++ team/sgriepentrog/12-userevent/res/ari/ari_model_validators.h Fri Apr 25 15:27:54 2014
@@ -1278,7 +1278,7 @@
  * - type: string (required)
  * - application: string (required)
  * - timestamp: Date
- * - channel: Channel (required)
+ * - channel: Channel
  * - eventname: string (required)
  * - userevent: object (required)
  * ChannelVarset

Modified: team/sgriepentrog/12-userevent/res/ari/resource_applications.c
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/res/ari/resource_applications.c?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/res/ari/resource_applications.c (original)
+++ team/sgriepentrog/12-userevent/res/ari/resource_applications.c Fri Apr 25 15:27:54 2014
@@ -170,3 +170,55 @@
 			"Error processing request");
 	}
 }
+
+void ast_ari_applications_generate_event(struct ast_variable *headers,
+	struct ast_ari_applications_generate_event_args *args,
+	struct ast_ari_response *response)
+{
+	enum stasis_app_generate_event_res res;
+
+	if (ast_strlen_zero(args->application_name)) {
+		ast_ari_response_error(response, 400, "Bad Request",
+			"Missing parameter applicationName");
+		return;
+	}
+
+	res = stasis_app_generate_event(args->application_name,
+		args->event_name,
+		args->source, args->source_count,
+		args->user_event, args->user_event_count);
+
+	ast_log(LOG_ERROR,"have res code %d\n", res);
+	switch (res) {
+	case STASIS_AGER_OK:
+		ast_ari_response_no_content(response);
+		break;
+
+	case STASIS_AGER_APP_NOT_FOUND:
+		ast_ari_response_error(response, 404, "Not Found",
+			"Application not found");
+		break;
+
+	case STASIS_AGER_EVENT_SOURCE_NOT_FOUND:
+		ast_ari_response_error(response, 422, "Unprocessable Entity",
+			"Event source was not subscribed to");
+		break;
+
+	case STASIS_AGER_EVENT_SOURCE_BAD_SCHEME:
+		ast_ari_response_error(response, 400, "Bad Request",
+			"Invalid event source URI scheme");
+		break;
+
+	case STASIS_AGER_USEREVENT_INVALID:
+		ast_ari_response_error(response, 400, "Bad Request",
+			"Invalid userevent data");
+		ast_log(LOG_ERROR,"Setting 400 response\n");
+		break;
+
+	case STASIS_AGER_INTERNAL_ERROR:
+	default:
+		ast_ari_response_error(response, 500, "Internal Server Error",
+			"Error processing request");
+		ast_log(LOG_ERROR,"Setting 500 response\n");
+	}
+}

Modified: team/sgriepentrog/12-userevent/res/ari/resource_applications.h
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/res/ari/resource_applications.h?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/res/ari/resource_applications.h (original)
+++ team/sgriepentrog/12-userevent/res/ari/resource_applications.h Fri Apr 25 15:27:54 2014
@@ -63,6 +63,44 @@
  * \param[out] response HTTP response
  */
 void ast_ari_applications_get(struct ast_variable *headers, struct ast_ari_applications_get_args *args, struct ast_ari_response *response);
+/*! Argument struct for ast_ari_applications_generate_event() */
+struct ast_ari_applications_generate_event_args {
+	/*! Application's name */
+	const char *application_name;
+	/*! Event name */
+	const char *event_name;
+	/*! Array of URI for event source (channel:{channelId}, bridge:{bridgeId}, endpoint:{tech}/{resource}, deviceState:{deviceName} */
+	const char **source;
+	/*! Length of source array. */
+	size_t source_count;
+	/*! Parsing context for source. */
+	char *source_parse;
+	/*! Array of Custom Userevent data. */
+	const char **user_event;
+	/*! Length of user_event array. */
+	size_t user_event_count;
+	/*! Parsing context for user_event. */
+	char *user_event_parse;
+};
+/*!
+ * \brief Body parsing function for /applications/{applicationName}/user/{eventName}.
+ * \param body The JSON body from which to parse parameters.
+ * \param[out] args The args structure to parse into.
+ * \retval zero on success
+ * \retval non-zero on failure
+ */
+int ast_ari_applications_generate_event_parse_body(
+	struct ast_json *body,
+	struct ast_ari_applications_generate_event_args *args);
+
+/*!
+ * \brief Generate a user event.
+ *
+ * \param headers HTTP headers
+ * \param args Swagger parameters
+ * \param[out] response HTTP response
+ */
+void ast_ari_applications_generate_event(struct ast_variable *headers, struct ast_ari_applications_generate_event_args *args, struct ast_ari_response *response);
 /*! Argument struct for ast_ari_applications_subscribe() */
 struct ast_ari_applications_subscribe_args {
 	/*! Application's name */

Modified: team/sgriepentrog/12-userevent/res/res_ari_applications.c
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/res/res_ari_applications.c?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/res/res_ari_applications.c (original)
+++ team/sgriepentrog/12-userevent/res/res_ari_applications.c Fri Apr 25 15:27:54 2014
@@ -161,6 +161,249 @@
 fin: __attribute__((unused))
 	return;
 }
+int ast_ari_applications_generate_event_parse_body(
+	struct ast_json *body,
+	struct ast_ari_applications_generate_event_args *args)
+{
+	struct ast_json *field;
+	/* Parse query parameters out of it */
+	field = ast_json_object_get(body, "source");
+	if (field) {
+		/* If they were silly enough to both pass in a query param and a
+		 * JSON body, free up the query value.
+		 */
+		ast_free(args->source);
+		if (ast_json_typeof(field) == AST_JSON_ARRAY) {
+			/* Multiple param passed as array */
+			size_t i;
+			args->source_count = ast_json_array_size(field);
+			args->source = ast_malloc(sizeof(*args->source) * args->source_count);
+
+			if (!args->source) {
+				return -1;
+			}
+
+			for (i = 0; i < args->source_count; ++i) {
+				args->source[i] = ast_json_string_get(ast_json_array_get(field, i));
+			}
+		} else {
+			/* Multiple param passed as single value */
+			args->source_count = 1;
+			args->source = ast_malloc(sizeof(*args->source) * args->source_count);
+			if (!args->source) {
+				return -1;
+			}
+			args->source[0] = ast_json_string_get(field);
+		}
+	}
+	field = ast_json_object_get(body, "userEvent");
+	if (field) {
+		/* If they were silly enough to both pass in a query param and a
+		 * JSON body, free up the query value.
+		 */
+		ast_free(args->user_event);
+		if (ast_json_typeof(field) == AST_JSON_ARRAY) {
+			/* Multiple param passed as array */
+			size_t i;
+			args->user_event_count = ast_json_array_size(field);
+			args->user_event = ast_malloc(sizeof(*args->user_event) * args->user_event_count);
+
+			if (!args->user_event) {
+				return -1;
+			}
+
+			for (i = 0; i < args->user_event_count; ++i) {
+				args->user_event[i] = ast_json_string_get(ast_json_array_get(field, i));
+			}
+		} else {
+			/* Multiple param passed as single value */
+			args->user_event_count = 1;
+			args->user_event = ast_malloc(sizeof(*args->user_event) * args->user_event_count);
+			if (!args->user_event) {
+				return -1;
+			}
+			args->user_event[0] = ast_json_string_get(field);
+		}
+	}
+	return 0;
+}
+
+/*!
+ * \brief Parameter parsing callback for /applications/{applicationName}/user/{eventName}.
+ * \param get_params GET parameters in the HTTP request.
+ * \param path_vars Path variables extracted from the request.
+ * \param headers HTTP headers.
+ * \param[out] response Response to the HTTP request.
+ */
+static void ast_ari_applications_generate_event_cb(
+	struct ast_tcptls_session_instance *ser,
+	struct ast_variable *get_params, struct ast_variable *path_vars,
+	struct ast_variable *headers, struct ast_ari_response *response)
+{
+	struct ast_ari_applications_generate_event_args args = {};
+	struct ast_variable *i;
+	RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
+#if defined(AST_DEVMODE)
+	int is_valid;
+	int code;
+#endif /* AST_DEVMODE */
+
+	for (i = get_params; i; i = i->next) {
+		if (strcmp(i->name, "source") == 0) {
+			/* Parse comma separated list */
+			char *vals[MAX_VALS];
+			size_t j;
+
+			args.source_parse = ast_strdup(i->value);
+			if (!args.source_parse) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+
+			if (strlen(args.source_parse) == 0) {
+				/* ast_app_separate_args can't handle "" */
+				args.source_count = 1;
+				vals[0] = args.source_parse;
+			} else {
+				args.source_count = ast_app_separate_args(
+					args.source_parse, ',', vals,
+					ARRAY_LEN(vals));
+			}
+
+			if (args.source_count == 0) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+
+			if (args.source_count >= MAX_VALS) {
+				ast_ari_response_error(response, 400,
+					"Bad Request",
+					"Too many values for source");
+				goto fin;
+			}
+
+			args.source = ast_malloc(sizeof(*args.source) * args.source_count);
+			if (!args.source) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+
+			for (j = 0; j < args.source_count; ++j) {
+				args.source[j] = (vals[j]);
+			}
+		} else
+		if (strcmp(i->name, "userEvent") == 0) {
+			/* Parse comma separated list */
+			char *vals[MAX_VALS];
+			size_t j;
+
+			args.user_event_parse = ast_strdup(i->value);
+			if (!args.user_event_parse) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+
+			if (strlen(args.user_event_parse) == 0) {
+				/* ast_app_separate_args can't handle "" */
+				args.user_event_count = 1;
+				vals[0] = args.user_event_parse;
+			} else {
+				args.user_event_count = ast_app_separate_args(
+					args.user_event_parse, ',', vals,
+					ARRAY_LEN(vals));
+			}
+
+			if (args.user_event_count == 0) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+
+			if (args.user_event_count >= MAX_VALS) {
+				ast_ari_response_error(response, 400,
+					"Bad Request",
+					"Too many values for user_event");
+				goto fin;
+			}
+
+			args.user_event = ast_malloc(sizeof(*args.user_event) * args.user_event_count);
+			if (!args.user_event) {
+				ast_ari_response_alloc_failed(response);
+				goto fin;
+			}
+
+			for (j = 0; j < args.user_event_count; ++j) {
+				args.user_event[j] = (vals[j]);
+			}
+		} else
+		{}
+	}
+	for (i = path_vars; i; i = i->next) {
+		if (strcmp(i->name, "applicationName") == 0) {
+			args.application_name = (i->value);
+		} else
+		if (strcmp(i->name, "eventName") == 0) {
+			args.event_name = (i->value);
+		} else
+		{}
+	}
+	/* Look for a JSON request entity */
+	body = ast_http_get_json(ser, headers);
+	if (!body) {
+		switch (errno) {
+		case EFBIG:
+			ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
+			goto fin;
+		case ENOMEM:
+			ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
+			goto fin;
+		case EIO:
+			ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
+			goto fin;
+		}
+	}
+	if (ast_ari_applications_generate_event_parse_body(body, &args)) {
+		ast_ari_response_alloc_failed(response);
+		goto fin;
+	}
+	ast_ari_applications_generate_event(headers, &args, response);
+#if defined(AST_DEVMODE)
+	code = response->response_code;
+
+	switch (code) {
+	case 0: /* Implementation is still a stub, or the code wasn't set */
+		is_valid = response->message == NULL;
+		break;
+	case 500: /* Internal Server Error */
+	case 501: /* Not Implemented */
+	case 404: /* Application does not exist. */
+	case 422: /* Event source not found. */
+	case 400: /* Invalid event source URI or userevent data. */
+		is_valid = 1;
+		break;
+	default:
+		if (200 <= code && code <= 299) {
+			is_valid = ast_ari_validate_void(
+				response->message);
+		} else {
+			ast_log(LOG_ERROR, "Invalid error response %d for /applications/{applicationName}/user/{eventName}\n", code);
+			is_valid = 0;
+		}
+	}
+
+	if (!is_valid) {
+		ast_log(LOG_ERROR, "Response validation failed for /applications/{applicationName}/user/{eventName}\n");
+		ast_ari_response_error(response, 500,
+			"Internal Server Error", "Response validation failed");
+	}
+#endif /* AST_DEVMODE */
+
+fin: __attribute__((unused))
+	ast_free(args.source_parse);
+	ast_free(args.source);
+	ast_free(args.user_event_parse);
+	ast_free(args.user_event);
+	return;
+}
 int ast_ari_applications_subscribe_parse_body(
 	struct ast_json *body,
 	struct ast_ari_applications_subscribe_args *args)
@@ -495,6 +738,24 @@
 	return;
 }
 
+/*! \brief REST handler for /api-docs/applications.{format} */
+static struct stasis_rest_handlers applications_applicationName_user_eventName = {
+	.path_segment = "eventName",
+	.is_wildcard = 1,
+	.callbacks = {
+		[AST_HTTP_POST] = ast_ari_applications_generate_event_cb,
+	},
+	.num_children = 0,
+	.children = {  }
+};
+/*! \brief REST handler for /api-docs/applications.{format} */
+static struct stasis_rest_handlers applications_applicationName_user = {
+	.path_segment = "user",
+	.callbacks = {
+	},
+	.num_children = 1,
+	.children = { &applications_applicationName_user_eventName, }
+};
 /*! \brief REST handler for /api-docs/applications.{format} */
 static struct stasis_rest_handlers applications_applicationName_subscription = {
 	.path_segment = "subscription",
@@ -512,8 +773,8 @@
 	.callbacks = {
 		[AST_HTTP_GET] = ast_ari_applications_get_cb,
 	},
-	.num_children = 1,
-	.children = { &applications_applicationName_subscription, }
+	.num_children = 2,
+	.children = { &applications_applicationName_user,&applications_applicationName_subscription, }
 };
 /*! \brief REST handler for /api-docs/applications.{format} */
 static struct stasis_rest_handlers applications = {

Modified: team/sgriepentrog/12-userevent/res/res_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/res/res_stasis.c?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/res/res_stasis.c (original)
+++ team/sgriepentrog/12-userevent/res/res_stasis.c Fri Apr 25 15:27:54 2014
@@ -62,6 +62,7 @@
 #include "asterisk/stasis_channels.h"
 #include "asterisk/stasis_bridges.h"
 #include "asterisk/stasis_message_router.h"
+#include "asterisk/stasis_user.h"
 #include "asterisk/strings.h"
 #include "stasis/app.h"
 #include "stasis/control.h"
@@ -1308,6 +1309,100 @@
 	return app_handle_subscriptions(
 		app_name, event_source_uris, event_sources_count,
 		json, app_unsubscribe);
+}
+
+enum stasis_app_generate_event_res stasis_app_generate_event(const char *app_name,
+	const char *event_name,
+	const char **source_uris, int sources_count,
+	const char **userevent_data, int userevents_count)
+{
+	RAII_VAR(struct stasis_app *, app, find_app_by_name(app_name), ao2_cleanup);
+	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+	RAII_VAR(struct ast_multi_object_blob *, multi, NULL, ao2_cleanup);
+	RAII_VAR(void *, obj, NULL, ao2_cleanup);
+	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
+	enum stasis_app_subscribe_res res = STASIS_AGER_INTERNAL_ERROR;
+	int have_channel = 0;
+	int i;
+
+	if (!app) {
+		ast_log(LOG_WARNING, "App %s not found\n", app_name);
+		return STASIS_AGER_APP_NOT_FOUND;
+	}
+
+	blob = ast_json_pack("{s: s}", "eventname", event_name);
+	if (!blob) {
+		ast_log(LOG_ERROR, "unable to create blob\n");
+		return res;
+	}
+
+	for (i = 0; i < userevents_count; ++i) {
+		const char *key;
+		char *value;
+		struct ast_json *json_value;
+
+		value = (char *)userevent_data[i];
+		key = strsep(&value, ":");
+		if (!value) {
+			ast_log(LOG_WARNING, "Invalid userevent data: %s\n", key);
+			return STASIS_AGER_USEREVENT_INVALID;
+		}
+
+		value = ast_strip(value);
+		json_value = ast_json_string_create(value);
+		if (!json_value) {
+			ast_log(LOG_ERROR, "unable to create json string\n");
+			return res;
+		}
+
+		if (ast_json_object_set(blob, key, json_value)) {
+			ast_log(LOG_ERROR, "unable to set pair to blob\n");
+			return res;
+		}
+	}
+
+	multi = ast_multi_object_blob_create(blob);
+
+	for (i = 0; i < sources_count; ++i) {
+		const char *uri = source_uris[i];
+		struct stasis_app_event_source *source;
+
+		if (!(source = app_event_source_find(uri))) {
+			ast_log(LOG_WARNING, "Invalid scheme: %s\n", uri);
+			return STASIS_AGER_EVENT_SOURCE_BAD_SCHEME;
+		}
+		if (!source->find ||!source->to_multi_object_blob) {
+			continue;
+		}
+		obj = source->find(app, uri + strlen(source->scheme));
+		if (!obj) {
+			ast_log(LOG_WARNING, "Event source not found: %s\n", uri);
+			return STASIS_ASR_EVENT_SOURCE_NOT_FOUND;
+		}
+		source->to_multi_object_blob(obj, multi);
+
+		if (!strcmp(source->scheme, "channel:")) {
+			have_channel = 1;
+		}
+	}
+
+	message = stasis_message_create(ast_multi_user_event_type(), multi);
+	if (!message) {
+		ast_log(LOG_ERROR, "Unable to create stasis user event message\n");
+		return res;
+	}
+
+	//ao2_ref(message, +1);
+	stasis_publish(ast_app_get_topic(app), message);
+
+	/*
+	if (have_channel) {
+		//ao2_ref(message, +1);
+		stasis_publish(ast_manager_get_topic(), message);
+	}
+	*/
+
+	return STASIS_AGER_OK;
 }
 
 void stasis_app_ref(void)

Modified: team/sgriepentrog/12-userevent/res/stasis/app.c
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/res/stasis/app.c?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/res/stasis/app.c (original)
+++ team/sgriepentrog/12-userevent/res/stasis/app.c Fri Apr 25 15:27:54 2014
@@ -35,6 +35,7 @@
 #include "asterisk/stasis_channels.h"
 #include "asterisk/stasis_endpoints.h"
 #include "asterisk/stasis_message_router.h"
+#include "asterisk/stasis_user.h"
 
 struct stasis_app {
 	/*! Aggregation topic for this application. */
@@ -795,6 +796,10 @@
 	return app;
 }
 
+struct stasis_topic *ast_app_get_topic(struct stasis_app *app) {
+	return app->topic;
+}
+
 /*!
  * \brief Send a message to the given application.
  * \param app App to send the message to.
@@ -1035,12 +1040,20 @@
 	return ast_channel_get_by_name(id);
 }
 
+static void channel_add_to_multi_object_blob(void *obj,
+	struct ast_multi_object_blob *blob)
+{
+	struct ast_channel *chan = obj;
+	ast_multi_object_blob_add_channel(blob, NULL, ast_channel_snapshot_create(chan));
+}
+
 struct stasis_app_event_source channel_event_source = {
 	.scheme = "channel:",
 	.find = channel_find,
 	.subscribe = subscribe_channel,
 	.unsubscribe = app_unsubscribe_channel_id,
-	.is_subscribed = app_is_subscribed_channel_id
+	.is_subscribed = app_is_subscribed_channel_id,
+	.to_multi_object_blob = channel_add_to_multi_object_blob
 };
 
 int app_subscribe_bridge(struct stasis_app *app, struct ast_bridge *bridge)
@@ -1104,12 +1117,20 @@
 	return stasis_app_bridge_find_by_id(id);
 }
 
+static void bridge_add_to_multi_object_blob(void *obj,
+	struct ast_multi_object_blob *blob)
+{
+	struct ast_bridge *bridge = obj;
+	ast_multi_object_blob_add_bridge(blob, NULL, ast_bridge_snapshot_create(bridge));
+}
+
 struct stasis_app_event_source bridge_event_source = {
 	.scheme = "bridge:",
 	.find = bridge_find,
 	.subscribe = subscribe_bridge,
 	.unsubscribe = app_unsubscribe_bridge_id,
-	.is_subscribed = app_is_subscribed_bridge_id
+	.is_subscribed = app_is_subscribed_bridge_id,
+	.to_multi_object_blob = bridge_add_to_multi_object_blob
 };
 
 int app_subscribe_endpoint(struct stasis_app *app, struct ast_endpoint *endpoint)
@@ -1164,12 +1185,20 @@
 	return ast_endpoint_find_by_id(id);
 }
 
+static void endpoint_add_to_multi_object_blob(void *obj,
+	struct ast_multi_object_blob *blob)
+{
+	struct ast_endpoint *endpoint = obj;
+	ast_multi_object_blob_add_endpoint(blob, NULL, ast_endpoint_snapshot_create(endpoint));
+}
+
 struct stasis_app_event_source endpoint_event_source = {
 	.scheme = "endpoint:",
 	.find = endpoint_find,
 	.subscribe = subscribe_endpoint,
 	.unsubscribe = app_unsubscribe_endpoint_id,
-	.is_subscribed = app_is_subscribed_endpoint_id
+	.is_subscribed = app_is_subscribed_endpoint_id,
+	.to_multi_object_blob = endpoint_add_to_multi_object_blob
 };
 
 void stasis_app_register_event_sources(void)

Modified: team/sgriepentrog/12-userevent/rest-api/api-docs/applications.json
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/rest-api/api-docs/applications.json?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/rest-api/api-docs/applications.json (original)
+++ team/sgriepentrog/12-userevent/rest-api/api-docs/applications.json Fri Apr 25 15:27:54 2014
@@ -42,6 +42,65 @@
 						{
 							"code": 404,
 							"reason": "Application does not exist."
+						}
+					]
+				}
+			]
+		},
+		{
+			"path": "/applications/{applicationName}/user/{eventName}",
+			"scription": "Stasis application user events",
+			"operations": [
+				{
+					"httpMethod": "POST",
+					"summary": "Generate a user event.",
+					"nickname": "generateEvent",
+					"responseClass": "void",
+					"parameters": [
+						{
+							"name": "applicationName",
+							"description": "Application's name",
+							"paramType": "path",
+							"required": true,
+							"allowMultiple": false,
+							"dataType": "string"
+						},
+						{
+							"name": "eventName",
+							"description": "Event name",
+							"paramType": "path",
+							"required": true,
+							"allowMultiple": false,
+							"dataType": "string"
+						},
+						{
+							"name": "source",
+							"description": "URI for event source (channel:{channelId}, bridge:{bridgeId}, endpoint:{tech}/{resource}, deviceState:{deviceName}",
+							"paramType": "query",
+							"required": false,
+							"allowMultiple": true,
+							"dataType": "string"
+						},
+						{
+							"name": "userEvent",
+							"description": "Custom Userevent data.",
+							"paramType": "query",
+							"allowMultiple": true,
+							"dataType": "string"
+						}
+					],
+					"errorResponses": [
+						{
+							"code": 404,
+							"reason": "Application does not exist."
+						},
+						{
+							"code": 422,
+							"reason": "Event source not found."
+						},
+						{
+							"code": 400,
+							"reason": "Invalid event source URI or userevent data."
 						}
 					]
 				}

Modified: team/sgriepentrog/12-userevent/rest-api/api-docs/events.json
URL: http://svnview.digium.com/svn/asterisk/team/sgriepentrog/12-userevent/rest-api/api-docs/events.json?view=diff&rev=413025&r1=413024&r2=413025
==============================================================================
--- team/sgriepentrog/12-userevent/rest-api/api-docs/events.json (original)
+++ team/sgriepentrog/12-userevent/rest-api/api-docs/events.json Fri Apr 25 15:27:54 2014
@@ -451,7 +451,7 @@
 					"description": "The name of the user event."
 				},
 				"channel": {
-					"required": true,
+					"required": false,
 					"type": "Channel",
 					"description": "The channel that signaled the user event."
 				},




More information about the asterisk-commits mailing list