[asterisk-commits] seanbright: trunk r155590 - in /trunk: apps/ channels/ include/asterisk/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Nov 8 20:00:00 CST 2008


Author: seanbright
Date: Sat Nov  8 19:59:59 2008
New Revision: 155590

URL: http://svn.digium.com/view/asterisk?view=rev&rev=155590
Log:
In order to move away from nested function use, some changes to the recently introduced
ast_channel_search_locked need to be made.  Specifically, the caller needs to be able to
pass arbitrary data which in turn is passed to the callback.  This patch addresses all
of the nested functions currently in asterisk trunk.

Modified:
    trunk/apps/app_directed_pickup.c
    trunk/channels/chan_sip.c
    trunk/include/asterisk/channel.h
    trunk/main/channel.c
    trunk/main/features.c

Modified: trunk/apps/app_directed_pickup.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_directed_pickup.c?view=diff&rev=155590&r1=155589&r2=155590
==============================================================================
--- trunk/apps/app_directed_pickup.c (original)
+++ trunk/apps/app_directed_pickup.c Sat Nov  8 19:59:59 2008
@@ -169,17 +169,30 @@
 	return res;
 }
 
+struct pickup_criteria {
+	const char *exten;
+	const char *context;
+};
+
+static int find_by_exten(struct ast_channel *c, void *data)
+{
+	struct pickup_criteria *info = data;
+
+	return (!strcasecmp(c->macroexten, info->exten) || !strcasecmp(c->exten, info->exten)) &&
+		!strcasecmp(c->dialcontext, info->context) &&
+		can_pickup(c);
+}
+
 /* Attempt to pick up specified extension with context */
 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
 {
-	auto int find_by_exten(struct ast_channel *c);
-	int find_by_exten(struct ast_channel *c) {
-		return (!strcasecmp(c->macroexten, exten) || !strcasecmp(c->exten, exten)) &&
-			!strcasecmp(c->dialcontext, context) &&
-			can_pickup(c);
-	}
-
-	struct ast_channel *target = ast_channel_search_locked(find_by_exten);
+	struct ast_channel *target = NULL;
+	struct pickup_criteria search = {
+		.exten = exten,
+		.context = context,
+	};
+
+	target = ast_channel_search_locked(find_by_exten, &search);
 
 	if (target) {
 		int res = pickup_do(chan, target);
@@ -191,18 +204,20 @@
 	return -1;
 }
 
+static int find_by_mark(struct ast_channel *c, void *data)
+{
+	const char *mark = data;
+	const char *tmp;
+
+	return (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
+		!strcasecmp(tmp, mark) &&
+		can_pickup(c);
+}
+
 /* Attempt to pick up specified mark */
 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
 {
-	auto int find_by_mark(struct ast_channel *);
-	int find_by_mark(struct ast_channel *c) {
-		const char *tmp;
-		return (tmp = pbx_builtin_getvar_helper(c, PICKUPMARK)) &&
-			!strcasecmp(tmp, mark) &&
-			can_pickup(c);
-	}	
-
-	struct ast_channel *target = ast_channel_search_locked(find_by_mark);
+	struct ast_channel *target = ast_channel_search_locked(find_by_mark, (char *) mark);
 
 	if (target) {
 		int res = pickup_do(chan, target);

Modified: trunk/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/trunk/channels/chan_sip.c?view=diff&rev=155590&r1=155589&r2=155590
==============================================================================
--- trunk/channels/chan_sip.c (original)
+++ trunk/channels/chan_sip.c Sat Nov  8 19:59:59 2008
@@ -4421,6 +4421,26 @@
 	return peer;
 }
 
+/* Function to assist finding peers by name only */
+static int find_by_name(void *obj, void *arg, void *data, int flags)
+{
+	struct sip_peer *search = obj, *match = arg;
+	int *forcenamematch = data;
+
+	/* Usernames in SIP uri's are case sensitive. Domains are not */
+	if (strcmp(search->name, match->name)) {
+		return 0;
+	}
+
+	/* If we're only looking for name matches, we should avoid type=peer devices,
+	   since these should not match on any name-based search */
+	if (*forcenamematch && search->onlymatchonip) {
+		return 0;
+	}
+
+	return CMP_MATCH | CMP_STOP;
+}
+
 /*! \brief Locate device by name or ip address 
  *	This is used on find matching device on name or ip/port.
 	If the device was declared as type=peer, we don't match on peer name on incoming INVITEs.
@@ -4433,31 +4453,10 @@
 {
 	struct sip_peer *p = NULL;
 	struct sip_peer tmp_peer;
-	
-	/* Inline function to assist finding peers by name only */
-	auto int find_by_name(void *obj, void *arg, void *data, int flags);
-
-	int find_by_name(void *obj, void *arg, void *data, int flags)
-	{
-		struct sip_peer *search = obj, *match = arg;
-
-		/* Usernames in SIP uri's are case sensitive. Domains are not */
-		if (strcmp(search->name, match->name)) {
-			return 0;
-		}
-
-		/* If we're only looking for name matches, we should avoid type=peer devices,
-		   since these should not match on any name-based search */
-		if (forcenamematch && search->onlymatchonip) {
-			return 0;
-		}
-
-		return CMP_MATCH | CMP_STOP;
-	}
 
 	if (peer) {
 		ast_copy_string(tmp_peer.name, peer, sizeof(tmp_peer.name));
-		p = ao2_t_callback(peers, OBJ_POINTER, find_by_name, &tmp_peer, NULL, "ao2_find in peers table");
+		p = ao2_t_callback(peers, OBJ_POINTER, find_by_name, &tmp_peer, &forcenamematch, "ao2_find in peers table");
 	} else if (sin) { /* search by addr? */
 		tmp_peer.addr.sin_addr.s_addr = sin->sin_addr.s_addr;
 		tmp_peer.addr.sin_port = sin->sin_port;
@@ -9870,8 +9869,21 @@
 	
 	/* Actually send the packet */
 	transmit_invite(mwi->call, SIP_SUBSCRIBE, 0, 2);
-	
+
 	return 0;
+}
+
+struct caller_criteria {
+	const char *exten;
+	const char *context;
+};
+
+static int find_calling_channel(struct ast_channel *c, void *data) {
+	struct caller_criteria *info = data;
+
+	return (c->pbx &&
+			(!strcasecmp(c->macroexten, info->exten) || !strcasecmp(c->exten, info->exten)) &&
+			!strcasecmp(c->context, info->context));
 }
 
 /*! \brief Used in the SUBSCRIBE notification subsystem (RFC3265) */
@@ -10039,14 +10051,13 @@
 			   callee must be dialing the same extension that is being monitored.  Simply dialing
 			   the hint'd device is not sufficient. */
 			if (global_notifycid) {
-				auto int find_calling_channel(struct ast_channel *c);
-				int find_calling_channel(struct ast_channel *c) {
-					return (c->pbx &&
-							(!strcasecmp(c->macroexten, p->exten) || !strcasecmp(c->exten, p->exten)) &&
-							!strcasecmp(c->context, p->context));
-				}
-
-				struct ast_channel *caller = ast_channel_search_locked(find_calling_channel);
+				struct ast_channel *caller = NULL;
+				struct caller_criteria data = {
+					.exten = p->exten,
+					.context = p->context,
+				};
+
+				caller = ast_channel_search_locked(find_calling_channel, &data);
 
 				if (caller) {
 					local_display = ast_strdupa(caller->cid.cid_name);

Modified: trunk/include/asterisk/channel.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/channel.h?view=diff&rev=155590&r1=155589&r2=155590
==============================================================================
--- trunk/include/asterisk/channel.h (original)
+++ trunk/include/asterisk/channel.h Sat Nov  8 19:59:59 2008
@@ -1185,9 +1185,10 @@
  * return 0 if there is no match, and non-zero if there is.
  * \param is_match callback executed on each channel until non-zero is returned, or we
  *        run out of channels to search.
+ * \param data data passed to the is_match callback during each invocation.
  * \return Returns the matched channel, or NULL if no channel was matched.
  */
-struct ast_channel *ast_channel_search_locked(int (*is_match)(struct ast_channel *));
+struct ast_channel *ast_channel_search_locked(int (*is_match)(struct ast_channel *, void *), void *data);
 
 /*! ! \brief Waits for a digit
  * \param c channel to wait for a digit on

Modified: trunk/main/channel.c
URL: http://svn.digium.com/view/asterisk/trunk/main/channel.c?view=diff&rev=155590&r1=155589&r2=155590
==============================================================================
--- trunk/main/channel.c (original)
+++ trunk/main/channel.c Sat Nov  8 19:59:59 2008
@@ -1227,14 +1227,14 @@
 }
 
 /*! \brief Search for a channel based on the passed channel matching callback (first match) and return it, locked */
-struct ast_channel *ast_channel_search_locked(int (*is_match)(struct ast_channel *))
+struct ast_channel *ast_channel_search_locked(int (*is_match)(struct ast_channel *, void *), void *data)
 {
 	struct ast_channel *c = NULL;
 
 	AST_RWLIST_RDLOCK(&channels);
 	AST_RWLIST_TRAVERSE(&channels, c, chan_list) {
 		ast_channel_lock(c);
-		if (is_match(c)) {
+		if (is_match(c, data)) {
 			break;
 		}
 		ast_channel_unlock(c);

Modified: trunk/main/features.c
URL: http://svn.digium.com/view/asterisk/trunk/main/features.c?view=diff&rev=155590&r1=155589&r2=155590
==============================================================================
--- trunk/main/features.c (original)
+++ trunk/main/features.c Sat Nov  8 19:59:59 2008
@@ -3987,6 +3987,18 @@
 	return 0;
 }
 
+static int find_channel_by_group(struct ast_channel *c, void *data) {
+	struct ast_channel *chan = data;
+
+	return !c->pbx &&
+		/* Accessing 'chan' here is safe without locking, because there is no way for
+		   the channel do disappear from under us at this point.  pickupgroup *could*
+		   change while we're here, but that isn't a problem. */
+		(c != chan) &&
+		(chan->pickupgroup & c->callgroup) &&
+		((c->_state == AST_STATE_RINGING) || (c->_state == AST_STATE_RING));
+}
+
 /*!
  * \brief Pickup a call
  * \param chan channel that initiated pickup.
@@ -3997,15 +4009,7 @@
 */
 int ast_pickup_call(struct ast_channel *chan)
 {
-	auto int find_channel_by_group(struct ast_channel *);
-	int find_channel_by_group(struct ast_channel *c) {
-		return !c->pbx &&
-			(c != chan) &&
-			(chan->pickupgroup & c->callgroup) &&
-			((c->_state == AST_STATE_RINGING) ||
-			 (c->_state == AST_STATE_RING));
-	}
-	struct ast_channel *cur = ast_channel_search_locked(find_channel_by_group);
+	struct ast_channel *cur = ast_channel_search_locked(find_channel_by_group, chan);
 
 	if (cur) {
 		int res = -1;




More information about the asterisk-commits mailing list