[svn-commits] mmichelson: branch mmichelson/trunk-digiumphones r361326 - in /team/mmichelso...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Apr 5 16:19:34 CDT 2012


Author: mmichelson
Date: Thu Apr  5 16:19:30 2012
New Revision: 361326

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=361326
Log:
Change presence state API to take a state information when indicating changes.

Much like the improved ast_devstate_changed_* functions for device states,
it is much simpler if we provide a state, subtype, and message directly
when presence state changes. This way, the provider does not have to be
asked what changes have been made since in 99% of cases, the provider is
the one who is actually the one telling of the state change in the first
place.


Modified:
    team/mmichelson/trunk-digiumphones/funcs/func_presencestate.c
    team/mmichelson/trunk-digiumphones/include/asterisk/presencestate.h
    team/mmichelson/trunk-digiumphones/main/presencestate.c

Modified: team/mmichelson/trunk-digiumphones/funcs/func_presencestate.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/trunk-digiumphones/funcs/func_presencestate.c?view=diff&rev=361326&r1=361325&r2=361326
==============================================================================
--- team/mmichelson/trunk-digiumphones/funcs/func_presencestate.c (original)
+++ team/mmichelson/trunk-digiumphones/funcs/func_presencestate.c Thu Apr  5 16:19:30 2012
@@ -223,7 +223,7 @@
 
 	ast_db_put(astdb_family, data, value);
 
-	ast_presence_state_changed(tmp);
+	ast_presence_state_changed_literal(state, subtype, message, tmp);
 
 	return 0;
 }
@@ -288,8 +288,9 @@
 		return NULL;
 	}
 
-	if (a->argc != e->args)
+	if (a->argc != e->args) {
 		return CLI_SHOWUSAGE;
+	}
 
 	ast_cli(a->fd, "\n"
 	        "---------------------------------------------------------------------\n"
@@ -300,7 +301,7 @@
 	db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
 	for (; db_entry; db_entry = db_entry->next) {
 		const char *object_name = strrchr(db_entry->key, '/') + 1;
-		char state_info[1024];
+		char state_info[1301];
 		int state;
 		char *subtype;
 		char *message;
@@ -309,8 +310,9 @@
 		ast_copy_string(state_info, db_entry->data, sizeof(state_info));
 		parse_data(state_info, &state, &subtype, &message, &options);
 
-		if (object_name <= (const char *) 1)
+		if (object_name <= (const char *) 1) {
 			continue;
+		}
 		ast_cli(a->fd, "--- Name: 'CustomPresence:%s'\n"
 				       "    --- State: '%s'\n"
 					   "    --- Subtype: '%s'\n"
@@ -363,8 +365,9 @@
 		static const char * const cmds[] = { "NOT_SET", "UNAVAILABLE", "AVAILABLE", "AWAY",
 						     "XA", "CHAT", "DND", NULL };
 
-		if (a->pos == e->args + 1)
+		if (a->pos == e->args + 1) {
 			return ast_cli_complete(a->word, cmds, a->n);
+		}
 
 		return NULL;
 	}
@@ -401,7 +404,7 @@
 
 	ast_db_put(astdb_family, dev, state);
 
-	ast_presence_state_changed(full_dev);
+	ast_presence_state_changed_literal(state_val, subtype, message, full_dev);
 
 	return CLI_SUCCESS;
 }
@@ -716,6 +719,27 @@
 static int load_module(void)
 {
 	int res = 0;
+	struct ast_db_entry *db_entry, *db_tree;
+
+	/* Populate the presence state cache on the system with all of the currently
+	 * known custom presence states. */
+	db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
+	for (; db_entry; db_entry = db_entry->next) {
+		const char *dev_name = strrchr(db_entry->key, '/') + 1;
+		char state_info[1301];
+		int state;
+		char *message;
+		char *subtype;
+		char *options;
+		if (dev_name <= (const char *) 1) {
+			continue;
+		}
+		ast_copy_string(state_info, db_entry->data, sizeof(state_info));
+		parse_data(state_info, &state, &subtype, &message, &options);
+		ast_presence_state_changed(state, subtype, message, "CustomPresence:%s", dev_name);
+	}
+	ast_db_freetree(db_tree);
+	db_tree = NULL;
 
 	res |= ast_custom_function_register(&presence_function);
 	res |= ast_presence_state_prov_add("CustomPresence", custom_presence_callback);

Modified: team/mmichelson/trunk-digiumphones/include/asterisk/presencestate.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/trunk-digiumphones/include/asterisk/presencestate.h?view=diff&rev=361326&r1=361325&r2=361326
==============================================================================
--- team/mmichelson/trunk-digiumphones/include/asterisk/presencestate.h (original)
+++ team/mmichelson/trunk-digiumphones/include/asterisk/presencestate.h Thu Apr  5 16:19:30 2012
@@ -67,8 +67,40 @@
 
 /*!
  * \brief Notify the world that a presence provider state changed.
+ *
+ * \param state the new presence state
+ * \param fmt Presence entity whose state has changed
+ *
+ * The new state of the entity will be sent off to any subscribers
+ * of the presence state. It will also be stored in the internal event
+ * cache.
+ *
+ * \retval 0 Success
+ * \retval -1 Failure
  */
-int ast_presence_state_changed(const char *presence_provider);
+int ast_presence_state_changed(enum ast_presence_state state,
+		const char *subtype,
+		const char *message,
+		const char *fmt, ...)
+	__attribute__((format(printf, 4, 5)));
+
+/*!
+ * \brief Notify the world that a presence provider state changed.
+ *
+ * \param state the new presence state
+ * \param presence_provider Presence entity whose state has changed
+ *
+ * The new state of the entity will be sent off to any subscribers
+ * of the presence state. It will also be stored in the internal event
+ * cache.
+ *
+ * \retval 0 Success
+ * \retval -1 Failure
+ */
+int ast_presence_state_changed_literal(enum ast_presence_state state,
+		const char *subtype,
+		const char *message,
+		const char *presence_provider);
 
 /*!
  * \brief Add presence state provider

Modified: team/mmichelson/trunk-digiumphones/main/presencestate.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/trunk-digiumphones/main/presencestate.c?view=diff&rev=361326&r1=361325&r2=361326
==============================================================================
--- team/mmichelson/trunk-digiumphones/main/presencestate.c (original)
+++ team/mmichelson/trunk-digiumphones/main/presencestate.c Thu Apr  5 16:19:30 2012
@@ -200,23 +200,14 @@
 	return res;
 }
 
-static void do_presence_state_change(const char *provider)
+static void presence_state_event(const char *provider,
+		enum ast_presence_state state,
+		const char *subtype,
+		const char *message)
 {
 	struct ast_event *event;
-	enum ast_event_type event_type;
-	char *subtype = NULL;
-	char *message = NULL;
-	int state;
-
-	state = ast_presence_state_helper(provider, &subtype, &message, 0);
-
-	if (state < 0) {
-		return;
-	}
-
-	event_type = AST_EVENT_PRESENCE_STATE;
-
-	if (!(event = ast_event_new(event_type,
+
+	if (!(event = ast_event_new(AST_EVENT_PRESENCE_STATE,
 			AST_EVENT_IE_PRESENCE_PROVIDER, AST_EVENT_IE_PLTYPE_STR, provider,
 			AST_EVENT_IE_PRESENCE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
 			AST_EVENT_IE_PRESENCE_SUBTYPE, AST_EVENT_IE_PLTYPE_STR, S_OR(subtype, ""),
@@ -226,15 +217,35 @@
 	}
 
 	ast_event_queue_and_cache(event);
+}
+
+static void do_presence_state_change(const char *provider)
+{
+	char *subtype = NULL;
+	char *message = NULL;
+	int state;
+
+	state = ast_presence_state_helper(provider, &subtype, &message, 0);
+
+	if (state < 0) {
+		return;
+	}
+
+	presence_state_event(provider, state, subtype, message);
 	ast_free(subtype);
 	ast_free(message);
 }
 
-int ast_presence_state_changed(const char *presence_provider)
+int ast_presence_state_changed_literal(enum ast_presence_state state,
+		const char *subtype,
+		const char *message,
+		const char *presence_provider)
 {
 	struct state_change *change;
 
-	if ((change_thread == AST_PTHREADT_NULL) ||
+	if (state != AST_PRESENCE_NOT_SET) {
+		presence_state_event(presence_provider, state, subtype, message);
+	} else if ((change_thread == AST_PTHREADT_NULL) ||
 		!(change = ast_calloc(1, sizeof(*change) + strlen(presence_provider)))) {
 		do_presence_state_change(presence_provider);
 	} else {
@@ -244,7 +255,23 @@
 		ast_cond_signal(&change_pending);
 		AST_LIST_UNLOCK(&state_changes);
 	}
+
 	return 0;
+}
+
+int ast_presence_state_changed(enum ast_presence_state state,
+		const char *subtype,
+		const char *message,
+		const char *fmt, ...)
+{
+	char buf[AST_MAX_EXTENSION];
+	va_list ap;
+
+	va_start(ap, fmt);
+	vsnprintf(buf, sizeof(buf), fmt, ap);
+	va_end(ap);
+
+	return ast_presence_state_changed_literal(state, subtype, message, buf);
 }
 
 /*! \brief Go through the presence state change queue and update changes in the presence state thread */




More information about the svn-commits mailing list