[asterisk-commits] branch kpfleming/chanfunc r9671 - /team/kpfleming/chanfunc/funcs/func_channel.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Sat Feb 11 21:05:34 MST 2006


Author: kpfleming
Date: Sat Feb 11 22:05:33 2006
New Revision: 9671

URL: http://svn.digium.com/view/asterisk?rev=9671&view=rev
Log:
add write mode
add locking around string copying
fix formatting

Modified:
    team/kpfleming/chanfunc/funcs/func_channel.c

Modified: team/kpfleming/chanfunc/funcs/func_channel.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/chanfunc/funcs/func_channel.c?rev=9671&r1=9670&r2=9671&view=diff
==============================================================================
--- team/kpfleming/chanfunc/funcs/func_channel.c (original)
+++ team/kpfleming/chanfunc/funcs/func_channel.c Sat Feb 11 22:05:33 2006
@@ -29,7 +29,6 @@
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision:  $")
-
 #include "asterisk/module.h"
 #include "asterisk/channel.h"
 #include "asterisk/pbx.h"
@@ -37,45 +36,69 @@
 #include "asterisk/utils.h"
 #include "asterisk/app.h"
 #include "asterisk/indications.h"
+#include "asterisk/stringfields.h"
+#define locked_copy_string(chan, dest, source, len) \
+	do { \
+		ast_mutex_lock(&chan->lock); \
+		ast_copy_string(dest, source, len); \
+		ast_mutex_unlock(&chan->lock); \
+	} while (0)
+#define locked_string_field_set(chan, field, source) \
+	do { \
+		ast_mutex_lock(&chan->lock); \
+		ast_string_field_set(chan, field, source); \
+		ast_mutex_unlock(&chan->lock); \
+	} while (0)
 
-static int func_channel_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len)
+static int func_channel_read(struct ast_channel *chan, char *function,
+			     char *data, char *buf, size_t len)
 {
-        int ret = 0;
+	int ret = 0;
 
-        if (!strcasecmp(data, "audionativeformat"))
-                /* use the _multiple version when chan->nativeformats holds multiple formats */
+	if (!strcasecmp(data, "audionativeformat"))
+		/* use the _multiple version when chan->nativeformats holds multiple formats */
 		/* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_AUDIO_MASK); */
-                ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
-        else if (!strcasecmp(data, "videonativeformat"))
-                /* use the _multiple version when chan->nativeformats holds multiple formats */
+		ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
+	else if (!strcasecmp(data, "videonativeformat"))
+		/* use the _multiple version when chan->nativeformats holds multiple formats */
 		/* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_VIDEO_MASK); */
-                ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
-        else if (!strcasecmp(data, "audioreadformat"))
-                ast_copy_string(buf, ast_getformatname(chan->readformat), len);
-        else if (!strcasecmp(data, "audiowriteformat"))
-                ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
-        else if (!strcasecmp(data, "tonezone") && chan->zone)
-                ast_copy_string(buf, chan->zone->country, len);
-        else if (!strcasecmp(data, "language"))
-                ast_copy_string(buf, chan->language, len);
-        else if (!strcasecmp(data, "musicclass"))
-                ast_copy_string(buf, chan->musicclass, len);
-        else if (!chan->tech->func_channel_read)
-                ret = -1;
-        else if ((ret = chan->tech->func_channel_read(chan, function, data, buf, len))) {
-                ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
-                ret = -1;
-        }
+		ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
+	else if (!strcasecmp(data, "audioreadformat"))
+		ast_copy_string(buf, ast_getformatname(chan->readformat), len);
+	else if (!strcasecmp(data, "audiowriteformat"))
+		ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
+	else if (!strcasecmp(data, "tonezone") && chan->zone)
+		locked_copy_string(chan, buf, chan->zone->country, len);
+	else if (!strcasecmp(data, "language"))
+		locked_copy_string(chan, buf, chan->language, len);
+	else if (!strcasecmp(data, "musicclass"))
+		locked_copy_string(chan, buf, chan->musicclass, len);
+	else if (!chan->tech->func_channel_read
+		 || chan->tech->func_channel_read(chan, function, data, buf, len)) {
+		ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
+		ret = -1;
+	}
 
-        return ret;
+	return ret;
 }
 
-static int func_channel_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
+static int func_channel_write(struct ast_channel *chan, char *function,
+			      char *data, const char *value)
 {
-	if (value)
-                ast_string_field_set(chan, language, value);
+	int ret = 0;
 
-        return 0;
+	if (!strcasecmp(data, "language"))
+		locked_string_field_set(chan, language, value);
+	else if (!strcasecmp(data, "musicclass"))
+		locked_string_field_set(chan, musicclass, value);
+	else if (!chan->tech->func_channel_write
+		 || chan->tech->func_channel_write(chan, function, data, value)) {
+		ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
+				data);
+		ret = -1;
+	}
+
+	return ret;
 }
 
 static struct ast_custom_function channel_function = {
@@ -83,34 +106,34 @@
 	.synopsis = "Gets/sets various pieces of information about the channel.",
 	.syntax = "CHANNEL(item)",
 	.desc = "Gets/set various pieces of information about the channel.\n"
-        "Standard items (provided by all channel technologies) are:\n"
-        "R/O	audioreadformat		format currently being read\n"
-        "R/O	audionativeformat 	format used natively for audio\n"
-        "R/O	audiowriteformat 	format currently being written\n"
-        "R/W	language 		language for sounds played\n"
-        "R/W	musicclass 		class (from musiconhold.conf) for hold music\n"
-        "R/O	tonezone 		zone for indications played\n"
-        "R/O	videonativeformat 	format used natively for video\n"
-        "\n"
-        "Additional items may be available from the channel driver providing\n"
-        "the channel; see its documentation for details.\n"
-        "\n"
-        "Any item requested that is not available on the current channel will\n"
-        "return an empty string.\n",
+		"Standard items (provided by all channel technologies) are:\n"
+		"R/O	audioreadformat		format currently being read\n"
+		"R/O	audionativeformat 	format used natively for audio\n"
+		"R/O	audiowriteformat 	format currently being written\n"
+		"R/W	language 		language for sounds played\n"
+		"R/W	musicclass 		class (from musiconhold.conf) for hold music\n"
+		"R/O	tonezone 		zone for indications played\n"
+		"R/O	videonativeformat 	format used natively for video\n"
+		"\n"
+		"Additional items may be available from the channel driver providing\n"
+		"the channel; see its documentation for details.\n"
+		"\n"
+		"Any item requested that is not available on the current channel will\n"
+		"return an empty string.\n",
 	.read = func_channel_read,
-        .write = func_channel_write,
+	.write = func_channel_write,
 };
 
 static char *tdesc = "Channel information dialplan function";
 
 int unload_module(void)
 {
-        return ast_custom_function_unregister(&channel_function);
+	return ast_custom_function_unregister(&channel_function);
 }
 
 int load_module(void)
 {
-        return ast_custom_function_register(&channel_function);
+	return ast_custom_function_register(&channel_function);
 }
 
 char *description(void)
@@ -127,11 +150,3 @@
 {
 	return ASTERISK_GPL_KEY;
 }
-
-/*
-Local Variables:
-mode: C
-c-file-style: "linux"
-indent-tabs-mode: nil
-End:
-*/



More information about the asterisk-commits mailing list