[Asterisk-code-review] pbx.c: Minor code rearangements. (asterisk[13])

Richard Mudgett asteriskteam at digium.com
Thu Apr 7 17:13:44 CDT 2016


Richard Mudgett has uploaded a new change for review.

  https://gerrit.asterisk.org/2558

Change subject: pbx.c: Minor code rearangements.
......................................................................

pbx.c: Minor code rearangements.

* Pull out a loop invariant.

* Convert an else-if ladder to a switch statement.

Change-Id: I0a95cfa9474a4600b9865f7b444534d275b37e95
---
M main/pbx.c
1 file changed, 63 insertions(+), 54 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/58/2558/1

diff --git a/main/pbx.c b/main/pbx.c
index b166978..c2ce7b0 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -3252,7 +3252,8 @@
 {
 	struct ao2_iterator cb_iter;
 	struct ast_state_cb *state_cb;
-	int state, same_state;
+	int state;
+	int same_state;
 	struct ao2_container *device_state_info;
 	int first_extended_cb_call = 1;
 	char context_name[AST_MAX_CONTEXT];
@@ -3291,7 +3292,8 @@
 	device_state_info = alloc_device_state_info();
 
 	state = ast_extension_state3(*hint_app, device_state_info);
-	if ((same_state = state == hint->laststate) && (~state & AST_EXTENSION_RINGING)) {
+	same_state = state == hint->laststate;
+	if (same_state && (~state & AST_EXTENSION_RINGING)) {
 		ao2_cleanup(device_state_info);
 		return;
 	}
@@ -3300,17 +3302,19 @@
 	hint->laststate = state;	/* record we saw the change */
 
 	/* For general callbacks */
-	cb_iter = ao2_iterator_init(statecbs, 0);
-	for (; !same_state && (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
-		execute_state_callback(state_cb->change_cb,
-				       context_name,
-				       exten_name,
-				       state_cb->data,
-				       AST_HINT_UPDATE_DEVICE,
-				       hint,
-				       NULL);
+	if (!same_state) {
+		cb_iter = ao2_iterator_init(statecbs, 0);
+		for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
+			execute_state_callback(state_cb->change_cb,
+				context_name,
+				exten_name,
+				state_cb->data,
+				AST_HINT_UPDATE_DEVICE,
+				hint,
+				NULL);
+		}
+		ao2_iterator_destroy(&cb_iter);
 	}
-	ao2_iterator_destroy(&cb_iter);
 
 	/* For extension callbacks */
 	/* extended callbacks are called when the state changed or when AST_STATE_RINGING is
@@ -3325,12 +3329,12 @@
 		}
 		if (state_cb->extended || !same_state) {
 			execute_state_callback(state_cb->change_cb,
-					       context_name,
-					       exten_name,
-					       state_cb->data,
-					       AST_HINT_UPDATE_DEVICE,
-					       hint,
-					       state_cb->extended ? device_state_info : NULL);
+				context_name,
+				exten_name,
+				state_cb->data,
+				AST_HINT_UPDATE_DEVICE,
+				hint,
+				state_cb->extended ? device_state_info : NULL);
 		}
 	}
 	ao2_iterator_destroy(&cb_iter);
@@ -3422,12 +3426,12 @@
 	cb_iter = ao2_iterator_init(statecbs, 0);
 	for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
 		execute_state_callback(state_cb->change_cb,
-				       context_name,
-				       exten_name,
-				       state_cb->data,
-				       AST_HINT_UPDATE_PRESENCE,
-				       hint,
-				       NULL);
+			context_name,
+			exten_name,
+			state_cb->data,
+			AST_HINT_UPDATE_PRESENCE,
+			hint,
+			NULL);
 	}
 	ao2_iterator_destroy(&cb_iter);
 
@@ -3435,12 +3439,12 @@
 	cb_iter = ao2_iterator_init(hint->callbacks, 0);
 	for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_cleanup(state_cb)) {
 		execute_state_callback(state_cb->change_cb,
-				       context_name,
-				       exten_name,
-				       state_cb->data,
-				       AST_HINT_UPDATE_PRESENCE,
-				       hint,
-				       NULL);
+			context_name,
+			exten_name,
+			state_cb->data,
+			AST_HINT_UPDATE_PRESENCE,
+			hint,
+			NULL);
 	}
 	ao2_iterator_destroy(&cb_iter);
 }
@@ -3460,27 +3464,32 @@
 
 	hint = stasis_message_data(msg);
 
-	if (reason == AST_HINT_UPDATE_DEVICE) {
+	switch (reason) {
+	case AST_HINT_UPDATE_DEVICE:
 		device_state_notify_callbacks(hint, &hint_app);
-	} else if (reason == AST_HINT_UPDATE_PRESENCE) {
-		char *presence_subtype = NULL;
-		char *presence_message = NULL;
-		int state;
-
-		state = extension_presence_state_helper(
-			hint->exten, &presence_subtype, &presence_message);
-
+		break;
+	case AST_HINT_UPDATE_PRESENCE:
 		{
-			struct ast_presence_state_message presence_state = {
-				.state = state > 0 ? state : AST_PRESENCE_INVALID,
-				.subtype = presence_subtype,
-				.message = presence_message
-			};
-			presence_state_notify_callbacks(msg, hint, &hint_app, &presence_state);
-		}
+			char *presence_subtype = NULL;
+			char *presence_message = NULL;
+			int state;
 
-		ast_free(presence_subtype);
-		ast_free(presence_message);
+			state = extension_presence_state_helper(
+				hint->exten, &presence_subtype, &presence_message);
+			{
+				struct ast_presence_state_message presence_state = {
+					.state = state > 0 ? state : AST_PRESENCE_INVALID,
+					.subtype = presence_subtype,
+					.message = presence_message
+				};
+
+				presence_state_notify_callbacks(msg, hint, &hint_app, &presence_state);
+			}
+
+			ast_free(presence_subtype);
+			ast_free(presence_message);
+		}
+		break;
 	}
 
 	ast_free(hint_app);
@@ -3904,12 +3913,12 @@
 		cb_iter = ao2_iterator_init(statecbs, 0);
 		for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
 			execute_state_callback(state_cb->change_cb,
-					ast_get_context_name(ast_get_extension_context(e)),
-					ast_get_extension_name(e),
-					state_cb->data,
-					AST_HINT_UPDATE_DEVICE,
-					hint_new,
-					NULL);
+				ast_get_context_name(ast_get_extension_context(e)),
+				ast_get_extension_name(e),
+				state_cb->data,
+				AST_HINT_UPDATE_DEVICE,
+				hint_new,
+				NULL);
 		}
 		ao2_iterator_destroy(&cb_iter);
 	}

-- 
To view, visit https://gerrit.asterisk.org/2558
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0a95cfa9474a4600b9865f7b444534d275b37e95
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>



More information about the asterisk-code-review mailing list