[asterisk-commits] mmichelson: branch mmichelson/digit_manipulation r196231 - in /team/mmichelso...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri May 22 11:13:44 CDT 2009


Author: mmichelson
Date: Fri May 22 11:13:41 2009
New Revision: 196231

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=196231
Log:
Add function ast_channel_connected_line_macro to decrease code duplication.

With this commit, I now can state that I officially am a fan of unions.


Modified:
    team/mmichelson/digit_manipulation/apps/app_dial.c
    team/mmichelson/digit_manipulation/apps/app_directed_pickup.c
    team/mmichelson/digit_manipulation/apps/app_queue.c
    team/mmichelson/digit_manipulation/include/asterisk/channel.h
    team/mmichelson/digit_manipulation/main/channel.c
    team/mmichelson/digit_manipulation/main/dial.c
    team/mmichelson/digit_manipulation/main/features.c

Modified: team/mmichelson/digit_manipulation/apps/app_dial.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/mmichelson/digit_manipulation/apps/app_dial.c?view=diff&rev=196231&r1=196230&r2=196231
==============================================================================
--- team/mmichelson/digit_manipulation/apps/app_dial.c (original)
+++ team/mmichelson/digit_manipulation/apps/app_dial.c Fri May 22 11:13:41 2009
@@ -868,8 +868,6 @@
 #endif
 	struct ast_party_connected_line connected_caller;
 	struct ast_str *featurecode = ast_str_alloca(FEATURE_MAX_LEN + 1);
-	char macro[AST_MAX_CONTEXT];
-	char macro_args[512]; /*XXX Big enough? */
 
 	ast_party_connected_line_init(&connected_caller);
 	if (single) {
@@ -936,16 +934,7 @@
 					ast_verb(3, "%s answered %s\n", c->name, in->name);
 					if (!single && !ast_test_flag64(peerflags, OPT_IGNORE_CONNECTEDLINE)) {
 						if (o->connected.id.number) {
-							ast_channel_lock(in);
-							ast_copy_string(macro, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLER_SEND_MACRO"), ""), sizeof(macro));
-							ast_copy_string(macro_args, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS"), ""), sizeof(macro_args));
-							ast_channel_unlock(in);
-							ast_log(LOG_NOTICE, "macro is %s and args is %s\n", macro, macro_args);
-							if (!ast_strlen_zero(macro)) {
-								ast_party_connected_line_copy(&in->connected, &o->connected);
-								ast_app_run_macro(c, in, macro, macro_args);
-								ast_channel_update_connected_line(in, &in->connected);
-							} else {
+							if (ast_channel_connected_line_macro(c, in, &o->connected, 1, 0)) {
 								ast_channel_update_connected_line(in, &o->connected);
 							}
 						} else if (!ast_test_flag64(o, DIAL_NOCONNECTEDLINE)) {
@@ -997,16 +986,7 @@
 						ast_verb(3, "%s answered %s\n", c->name, in->name);
 						if (!single && !ast_test_flag64(peerflags, OPT_IGNORE_CONNECTEDLINE)) {
 							if (o->connected.id.number) {
-								ast_channel_lock(in);
-								ast_copy_string(macro, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLER_SEND_MACRO"), ""), sizeof(macro));
-								ast_copy_string(macro_args, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS"), ""), sizeof(macro_args));
-								ast_channel_unlock(in);
-								ast_log(LOG_NOTICE, "macro is %s and args is %s\n", macro, macro_args);
-								if (!ast_strlen_zero(macro)) {
-									ast_party_connected_line_copy(&in->connected, &o->connected);
-									ast_app_run_macro(c, in, macro, macro_args);
-									ast_channel_update_connected_line(in, &in->connected);
-								} else {
+								if (ast_channel_connected_line_macro(c, in, &o->connected, 1, 0)) {
 									ast_channel_update_connected_line(in, &o->connected);
 								}
 							} else if (!ast_test_flag64(o, DIAL_NOCONNECTEDLINE)) {
@@ -1097,17 +1077,7 @@
 						ast_party_connected_line_set(&o->connected, &connected);
 						ast_party_connected_line_free(&connected);
 					} else {
-						ast_channel_lock(in);
-						ast_copy_string(macro, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLER_SEND_MACRO"), ""), sizeof(macro));
-						ast_copy_string(macro_args, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS"), ""), sizeof(macro_args));
-						ast_channel_unlock(in);
-						ast_log(LOG_NOTICE, "macro is %s and args is %s\n", macro, macro_args);
-						ast_verb(3, "%s connected line has changed, passing it to %s\n", c->name, in->name);
-						if (!ast_strlen_zero(macro)) {
-							ast_connected_line_parse_data(f->data.ptr, f->datalen, &in->connected);
-							ast_app_run_macro(c, in, macro, macro_args);
-							ast_channel_update_connected_line(in, &in->connected);
-						} else {
+						if (ast_channel_connected_line_macro(c, in, f, 1, 1)) {
 							ast_indicate_data(in, AST_CONTROL_CONNECTED_LINE, f->data.ptr, f->datalen);
 						}
 					}
@@ -1239,16 +1209,7 @@
 					ast_verb(3, "%s requested special control %d, passing it to %s\n", in->name, f->subclass, outgoing->chan->name);
 					ast_indicate_data(outgoing->chan, f->subclass, f->data.ptr, f->datalen);
 				} else if (f->subclass == AST_CONTROL_CONNECTED_LINE) {
-					ast_channel_lock(in);
-					ast_copy_string(macro, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLEE_SEND_MACRO"), ""), sizeof(macro));
-					ast_copy_string(macro_args, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLEE_SEND_MACRO_ARGS"), ""), sizeof(macro_args));
-					ast_channel_unlock(in);
-					ast_log(LOG_NOTICE, "macro is %s and args is %s\n", macro, macro_args);
-					if (!ast_strlen_zero(macro)) {
-						ast_connected_line_parse_data(f->data.ptr, f->datalen, &outgoing->chan->connected);
-						ast_app_run_macro(in, outgoing->chan, macro, macro_args);
-						ast_channel_update_connected_line(outgoing->chan, &outgoing->chan->connected);
-					} else {
+					if (ast_channel_connected_line_macro(in, outgoing->chan, f, 0, 1)) {
 						ast_indicate_data(outgoing->chan, f->subclass, f->data.ptr, f->datalen);
 					}
 				}

Modified: team/mmichelson/digit_manipulation/apps/app_directed_pickup.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/mmichelson/digit_manipulation/apps/app_directed_pickup.c?view=diff&rev=196231&r1=196230&r2=196231
==============================================================================
--- team/mmichelson/digit_manipulation/apps/app_directed_pickup.c (original)
+++ team/mmichelson/digit_manipulation/apps/app_directed_pickup.c Fri May 22 11:13:41 2009
@@ -93,24 +93,13 @@
 {
 	int res = 0;
 	struct ast_party_connected_line connected_caller;
-	const char * macro;
-	const char * macro_args;
 
 	ast_debug(1, "Call pickup on '%s' by '%s'\n", target->name, chan->name);
-
-	ast_channel_lock(target);
-	macro = ast_strdupa(S_OR(pbx_builtin_getvar_helper(target, "CONNECTED_LINE_CALLEE_SEND_MACRO"), ""));
-	macro_args = ast_strdupa(S_OR(pbx_builtin_getvar_helper(target, "CONNECTED_LINE_CALLEE_SEND_MACRO_ARGS"), ""));
-	ast_channel_unlock(target);
 
 	connected_caller = target->connected;
 	ast_party_connected_line_init(&target->connected);
 	connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
-	if (!ast_strlen_zero(macro)) {
-		ast_party_connected_line_copy(&chan->connected, &connected_caller);
-		ast_app_run_macro(NULL, chan, macro, macro_args);
-		ast_channel_update_connected_line(chan, &chan->connected);
-	} else {
+	if (ast_channel_connected_line_macro(NULL, chan, &connected_caller, 0, 0)) {
 		ast_channel_update_connected_line(chan, &connected_caller);
 	}
 

Modified: team/mmichelson/digit_manipulation/apps/app_queue.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/mmichelson/digit_manipulation/apps/app_queue.c?view=diff&rev=196231&r1=196230&r2=196231
==============================================================================
--- team/mmichelson/digit_manipulation/apps/app_queue.c (original)
+++ team/mmichelson/digit_manipulation/apps/app_queue.c Fri May 22 11:13:41 2009
@@ -2871,8 +2871,6 @@
 #endif
 	struct ast_party_connected_line connected_caller;
 	char *inchan_name;
-	char macro[AST_MAX_CONTEXT];
-	char macro_args[512]; /*XXX Big enough? */
 
 	ast_party_connected_line_init(&connected_caller);
 
@@ -2941,14 +2939,7 @@
 					ast_verb(3, "%s answered %s\n", ochan_name, inchan_name);
 					if (update_connectedline) {
 						if (o->connected.id.number) {
-							ast_copy_string(macro, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLER_SEND_MACRO"), ""), sizeof(macro));
-							ast_copy_string(macro_args, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS"), ""), sizeof(macro_args));
-							ast_log(LOG_NOTICE, "macro is %s and args is %s\n", macro, macro_args);
-							if (!ast_strlen_zero(macro)) {
-								ast_party_connected_line_copy(&in->connected, &o->connected);
-								ast_app_run_macro(o->chan, in, macro, macro_args);
-								ast_channel_update_connected_line(in, &in->connected);
-							} else {
+							if (ast_channel_connected_line_macro(o->chan, in, &o->connected, 1, 0)) {
 								ast_channel_update_connected_line(in, &o->connected);
 							}
 						} else if (o->update_connectedline) {
@@ -3050,14 +3041,7 @@
 								ast_verb(3, "%s answered %s\n", ochan_name, inchan_name);
 								if (update_connectedline) {
 									if (o->connected.id.number) {
-										ast_copy_string(macro, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLER_SEND_MACRO"), ""), sizeof(macro));
-										ast_copy_string(macro_args, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS"), ""), sizeof(macro_args));
-										ast_log(LOG_NOTICE, "macro is %s and args is %s\n", macro, macro_args);
-										if (!ast_strlen_zero(macro)) {
-											ast_party_connected_line_copy(&in->connected, &o->connected);
-											ast_app_run_macro(o->chan, in, macro, macro_args);
-											ast_channel_update_connected_line(in, &in->connected);
-										} else {
+										if (ast_channel_connected_line_macro(o->chan, in, &o->connected, 1, 0)) {
 											ast_channel_update_connected_line(in, &o->connected);
 										}
 									} else if (o->update_connectedline) {
@@ -3119,15 +3103,7 @@
 								ast_party_connected_line_set(&o->connected, &connected);
 								ast_party_connected_line_free(&connected);
 							} else {
-								ast_copy_string(macro, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLER_SEND_MACRO"), ""), sizeof(macro));
-								ast_copy_string(macro_args, S_OR(pbx_builtin_getvar_helper(in, "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS"), ""), sizeof(macro_args));
-								ast_log(LOG_NOTICE, "macro is %s and args is %s\n", macro, macro_args);
-								ast_verb(3, "%s connected line has changed, passing it to %s\n", o->chan->name, in->name);
-								if (!ast_strlen_zero(macro)) {
-									ast_connected_line_parse_data(f->data.ptr, f->datalen, &in->connected);
-									ast_app_run_macro(o->chan, in, macro, macro_args);
-									ast_channel_update_connected_line(in, &in->connected);
-								} else {
+								if (ast_channel_connected_line_macro(o->chan, in, f, 1, 1)) {
 									ast_indicate_data(in, AST_CONTROL_CONNECTED_LINE, f->data.ptr, f->datalen);
 								}
 							}

Modified: team/mmichelson/digit_manipulation/include/asterisk/channel.h
URL: http://svn.asterisk.org/svn-view/asterisk/team/mmichelson/digit_manipulation/include/asterisk/channel.h?view=diff&rev=196231&r1=196230&r2=196231
==============================================================================
--- team/mmichelson/digit_manipulation/include/asterisk/channel.h (original)
+++ team/mmichelson/digit_manipulation/include/asterisk/channel.h Fri May 22 11:13:41 2009
@@ -2550,6 +2550,26 @@
  */
 void ast_channel_queue_redirecting_update(struct ast_channel *chan, const struct ast_party_redirecting *redirecting);
 
+/*!
+ * \since 1.6.3
+ * \brief Run a connected line interception macro and update a channel's connected line information
+ *
+ * Whenever we want to update a channel's connected line information, we may need to run a macro so that
+ * an administrator can manipulate the information before sending it out. This function both runs the
+ * macro and sends the update to the channel.
+ *
+ * \param autoservice_chan Channel to place into autoservice while the macro is running. It is perfectly safe for this to be NULL
+ * \param macro_chan The channel to run the macro on. Also the channel from which we determine which macro we need to run.
+ * \param connected_info Either an ast_party_connected_line or ast_frame pointer of type AST_CONTROL_CONNECTED_LINE
+ * \param caller If true, then run CONNECTED_LINE_CALLER_SEND_MACRO, otherwise run CONNECTED_LINE_CALLEE_SEND_MACRO
+ * \param frame If true, then connected_info is an ast_frame pointer, otherwise it is an ast_party_connected_line pointer.
+ * \retval 0 Success
+ * \retval -1 Either the macro does not exist, or there was an error while attempting to run the macro
+ *
+ * \todo Have multiple return codes based on the MACRO_RESULT
+ * \todo Make constants so that caller and frame can be more expressive than just '1' and '0'
+ */
+int ast_channel_connected_line_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, void *connected_info, int caller, int frame);
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

Modified: team/mmichelson/digit_manipulation/main/channel.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/mmichelson/digit_manipulation/main/channel.c?view=diff&rev=196231&r1=196230&r2=196231
==============================================================================
--- team/mmichelson/digit_manipulation/main/channel.c (original)
+++ team/mmichelson/digit_manipulation/main/channel.c Fri May 22 11:13:41 2009
@@ -6502,6 +6502,42 @@
 	ast_queue_control_data(chan, AST_CONTROL_REDIRECTING, data, datalen);
 }
 
+int ast_channel_connected_line_macro(struct ast_channel *autoservice_chan, struct ast_channel *macro_chan, void *connected_info, int caller, int frame)
+{
+	const char *macro;
+	const char *macro_args;
+	union {
+		struct ast_frame *frame;
+		struct ast_party_connected_line *connected;
+	} pointer;
+
+	if (frame) {
+		pointer.frame = connected_info;
+	} else {
+		pointer.connected = connected_info;
+	}
+
+	ast_channel_lock(macro_chan);
+	macro = ast_strdupa(S_OR(pbx_builtin_getvar_helper(macro_chan, caller ? "CONNECTED_LINE_CALLER_SEND_MACRO" : "CONNECTED_LINE_CALLEE_SEND_MACRO"), ""));
+	macro_args = ast_strdupa(S_OR(pbx_builtin_getvar_helper(macro_chan, caller ? "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS" : "CONNECTED_LINE_CALLEE_SEND_MACRO_ARGS"), ""));
+	ast_channel_unlock(macro_chan);
+
+	if (!ast_strlen_zero(macro)) {
+		int retval;
+		if (frame) {
+			ast_connected_line_parse_data(pointer.frame->data.ptr, pointer.frame->datalen, &macro_chan->connected);
+		} else {
+			ast_party_connected_line_copy(&macro_chan->connected, pointer.connected);
+		}
+		if (!(retval = ast_app_run_macro(autoservice_chan, macro_chan, macro, macro_args))) {
+			ast_channel_update_connected_line(macro_chan, &macro_chan->connected);
+		}
+		return retval;
+	} else {
+		return -1;
+	}
+}
+
 /* DO NOT PUT ADDITIONAL FUNCTIONS BELOW THIS BOUNDARY
  *
  * ONLY FUNCTIONS FOR PROVIDING BACKWARDS ABI COMPATIBILITY BELONG HERE

Modified: team/mmichelson/digit_manipulation/main/dial.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/mmichelson/digit_manipulation/main/dial.c?view=diff&rev=196231&r1=196230&r2=196231
==============================================================================
--- team/mmichelson/digit_manipulation/main/dial.c (original)
+++ team/mmichelson/digit_manipulation/main/dial.c Fri May 22 11:13:41 2009
@@ -389,8 +389,6 @@
 /*! \brief Helper function that handles control frames WITH owner */
 static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
 {
-	const char *macro;
-	const char *macro_args;
 	if (fr->frametype == AST_FRAME_CONTROL) {
 		switch (fr->subclass) {
 		case AST_CONTROL_ANSWER:
@@ -433,15 +431,7 @@
 			break;
 		case AST_CONTROL_CONNECTED_LINE:
 			ast_verb(3, "%s connected line has changed, passing it to %s\n", channel->owner->name, chan->name);
-			ast_channel_lock(chan);
-			macro = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "CONNECTED_LINE_CALLER_SEND_MACRO"), ""));
-			macro_args = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS"), ""));
-			ast_channel_unlock(chan);
-			if (!ast_strlen_zero(macro)) {
-				ast_connected_line_parse_data(fr->data.ptr, fr->datalen, &chan->connected);
-				ast_app_run_macro(channel->owner, chan, macro, macro_args);
-				ast_channel_update_connected_line(chan, &chan->connected);
-			} else {
+			if (ast_channel_connected_line_macro(channel->owner, chan, fr, 1, 1)) {
 				ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, fr->data.ptr, fr->datalen);
 			}
 			break;

Modified: team/mmichelson/digit_manipulation/main/features.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/mmichelson/digit_manipulation/main/features.c?view=diff&rev=196231&r1=196230&r2=196231
==============================================================================
--- team/mmichelson/digit_manipulation/main/features.c (original)
+++ team/mmichelson/digit_manipulation/main/features.c Fri May 22 11:13:41 2009
@@ -2626,8 +2626,6 @@
 	}
 	for (;;) {
 		struct ast_channel *other;	/* used later */
-		char macro[AST_MAX_CONTEXT];
-		char macro_args[512]; /*XXX Big enough? */
 	
 		res = ast_channel_bridge(chan, peer, config, &f, &who);
 		
@@ -2714,14 +2712,7 @@
 				ast_indicate(other, f->subclass);
 				break;
 			case AST_CONTROL_CONNECTED_LINE:
-				ast_channel_lock(who);
-				ast_copy_string(macro, S_OR(pbx_builtin_getvar_helper(who, who == chan ? "CONNECTED_LINE_CALLEE_SEND_MACRO" : "CONNECTED_LINE_CALLER_SEND_MACRO"), ""), sizeof(macro));
-				ast_copy_string(macro_args, S_OR(pbx_builtin_getvar_helper(who, who == chan ? "CONNECTED_LINE_CALLEE_SEND_MACRO_ARGS " : "CONNECTED_LINE_CALLER_SEND_MACRO_ARGS"), ""), sizeof(macro_args));
-				ast_channel_unlock(who);
-				if (!ast_strlen_zero(macro)) {
-					ast_connected_line_parse_data(f->data.ptr, f->datalen, &other->connected);
-					ast_app_run_macro(who, other, macro, macro_args);
-					ast_channel_update_connected_line(other, &other->connected);
+				if (!ast_channel_connected_line_macro(who, other, f, who != chan, 1)) {
 					break;
 				}
 				/* The implied "else" falls through purposely */
@@ -4501,8 +4492,6 @@
 	int res;
 	const char *chan_name;
 	const char *cur_name;
-	const char *macro;
-	const char *macro_args;
 
 	if (!(cur = ast_channel_callback(find_channel_by_group, NULL, chan, 0))) {
 		ast_debug(1, "No call pickup possible...\n");
@@ -4514,9 +4503,6 @@
 
 	ast_channel_lock_both(cur, chan);
 
-	macro = ast_strdupa(S_OR(pbx_builtin_getvar_helper(cur, "CONNECTED_LINE_CALLEE_SEND_MACRO"), ""));
-	macro_args = ast_strdupa(S_OR(pbx_builtin_getvar_helper(cur, "CONNECTED_LINE_CALLEE_SEND_MACRO_ARGS"), ""));
-
 	cur_name = ast_strdupa(cur->name);
 	chan_name = ast_strdupa(chan->name);
 
@@ -4524,11 +4510,7 @@
 
 	connected_caller = cur->connected;
 	connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
-	if (!ast_strlen_zero(macro)) {
-		ast_party_connected_line_copy(&chan->connected, &connected_caller);
-		ast_app_run_macro(NULL, chan, macro, macro_args);
-		ast_channel_update_connected_line(chan, &chan->connected);
-	} else {
+	if (ast_channel_connected_line_macro(NULL, chan, &connected_caller, 0, 0)) {
 		ast_channel_update_connected_line(chan, &connected_caller);
 	}
 




More information about the asterisk-commits mailing list