use the channel lock wrappers (issue #7120, Mithraen)
[asterisk/asterisk.git] / funcs / func_channel.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2006, Digium, Inc.
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16
17 /*! \file
18  *
19  * \brief Channel info dialplan function
20  *
21  * \author Kevin P. Fleming <kpfleming@digium.com>
22  * 
23  */
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/types.h>
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/module.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/app.h"
39 #include "asterisk/indications.h"
40 #include "asterisk/stringfields.h"
41 #define locked_copy_string(chan, dest, source, len) \
42         do { \
43                 ast_channel_lock(chan); \
44                 ast_copy_string(dest, source, len); \
45                 ast_channel_unlock(chan); \
46         } while (0)
47 #define locked_string_field_set(chan, field, source) \
48         do { \
49                 ast_channel_lock(chan); \
50                 ast_string_field_set(chan, field, source); \
51                 ast_channel_unlock(chan); \
52         } while (0)
53
54 static int func_channel_read(struct ast_channel *chan, char *function,
55                              char *data, char *buf, size_t len)
56 {
57         int ret = 0;
58
59         if (!strcasecmp(data, "audionativeformat"))
60                 /* use the _multiple version when chan->nativeformats holds multiple formats */
61                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_AUDIO_MASK); */
62                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
63         else if (!strcasecmp(data, "videonativeformat"))
64                 /* use the _multiple version when chan->nativeformats holds multiple formats */
65                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_VIDEO_MASK); */
66                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
67         else if (!strcasecmp(data, "audioreadformat"))
68                 ast_copy_string(buf, ast_getformatname(chan->readformat), len);
69         else if (!strcasecmp(data, "audiowriteformat"))
70                 ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
71         else if (!strcasecmp(data, "tonezone") && chan->zone)
72                 locked_copy_string(chan, buf, chan->zone->country, len);
73         else if (!strcasecmp(data, "language"))
74                 locked_copy_string(chan, buf, chan->language, len);
75         else if (!strcasecmp(data, "musicclass"))
76                 locked_copy_string(chan, buf, chan->musicclass, len);
77         else if (!strcasecmp(data, "state"))
78                 locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
79         else if (!strcasecmp(data, "channeltype"))
80                 locked_copy_string(chan, buf, chan->tech->type, len);
81         else if (!strcasecmp(data, "callgroup")) {
82                 char groupbuf[256];
83                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
84         } else if (!chan->tech->func_channel_read
85                  || chan->tech->func_channel_read(chan, function, data, buf, len)) {
86                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
87                 ret = -1;
88         }
89
90         return ret;
91 }
92
93 static int func_channel_write(struct ast_channel *chan, char *function,
94                               char *data, const char *value)
95 {
96         int ret = 0;
97         signed char gainset;
98
99         if (!strcasecmp(data, "language"))
100                 locked_string_field_set(chan, language, value);
101         else if (!strcasecmp(data, "musicclass"))
102                 locked_string_field_set(chan, musicclass, value);
103         else if (!strcasecmp(data, "callgroup"))
104                 chan->callgroup = ast_get_group(data);
105         else if (!strcasecmp(data, "txgain")) {
106                 sscanf(value, "%hhd", &gainset);
107                 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
108         } else if (!strcasecmp(data, "rxgain")) {
109                 sscanf(value, "%hhd", &gainset);
110                 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);   
111         } else if (!chan->tech->func_channel_write
112                  || chan->tech->func_channel_write(chan, function, data, value)) {
113                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
114                                 data);
115                 ret = -1;
116         }
117
118         return ret;
119 }
120
121 static struct ast_custom_function channel_function = {
122         .name = "CHANNEL",
123         .synopsis = "Gets/sets various pieces of information about the channel.",
124         .syntax = "CHANNEL(item)",
125         .desc = "Gets/set various pieces of information about the channel.\n"
126                 "Standard items (provided by all channel technologies) are:\n"
127                 "R/O    audioreadformat         format currently being read\n"
128                 "R/O    audionativeformat       format used natively for audio\n"
129                 "R/O    audiowriteformat        format currently being written\n"
130                 "R/W    callgroup               call groups for call pickup\n"
131                 "R/O    channeltype             technology used for channel\n"
132                 "R/W    language                language for sounds played\n"
133                 "R/W    musicclass              class (from musiconhold.conf) for hold music\n"
134                 "R/W    rxgain                  set rxgain level on channel drivers that support it\n"
135                 "R/O    state                   state for channel\n"
136                 "R/O    tonezone                zone for indications played\n"
137                 "R/W    txgain                  set txgain level on channel drivers that support it\n"
138                 "R/O    videonativeformat       format used natively for video\n"
139                 "\n"
140                 "Additional items may be available from the channel driver providing\n"
141                 "the channel; see its documentation for details.\n"
142                 "\n"
143                 "Any item requested that is not available on the current channel will\n"
144                 "return an empty string.\n",
145         .read = func_channel_read,
146         .write = func_channel_write,
147 };
148
149 static char *tdesc = "Channel information dialplan function";
150
151 static int unload_module(void *mod)
152 {
153         return ast_custom_function_unregister(&channel_function);
154 }
155
156 static int load_module(void *mod)
157 {
158         return ast_custom_function_register(&channel_function);
159 }
160
161 static const char *description(void)
162 {
163         return tdesc;
164 }
165
166 static const char *key(void)
167 {
168         return ASTERISK_GPL_KEY;
169 }
170
171 STD_MOD(MOD_1 | NO_USECOUNT, NULL, NULL, NULL);