This rather large commit changes the way modules are loaded.
[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 <string.h>
27 #include <sys/types.h>
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/module.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/logger.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/app.h"
38 #include "asterisk/indications.h"
39 #include "asterisk/stringfields.h"
40 #define locked_copy_string(chan, dest, source, len) \
41         do { \
42                 ast_mutex_lock(&chan->lock); \
43                 ast_copy_string(dest, source, len); \
44                 ast_mutex_unlock(&chan->lock); \
45         } while (0)
46 #define locked_string_field_set(chan, field, source) \
47         do { \
48                 ast_mutex_lock(&chan->lock); \
49                 ast_string_field_set(chan, field, source); \
50                 ast_mutex_unlock(&chan->lock); \
51         } while (0)
52
53 static int func_channel_read(struct ast_channel *chan, char *function,
54                              char *data, char *buf, size_t len)
55 {
56         int ret = 0;
57
58         if (!strcasecmp(data, "audionativeformat"))
59                 /* use the _multiple version when chan->nativeformats holds multiple formats */
60                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_AUDIO_MASK); */
61                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
62         else if (!strcasecmp(data, "videonativeformat"))
63                 /* use the _multiple version when chan->nativeformats holds multiple formats */
64                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_VIDEO_MASK); */
65                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
66         else if (!strcasecmp(data, "audioreadformat"))
67                 ast_copy_string(buf, ast_getformatname(chan->readformat), len);
68         else if (!strcasecmp(data, "audiowriteformat"))
69                 ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
70         else if (!strcasecmp(data, "tonezone") && chan->zone)
71                 locked_copy_string(chan, buf, chan->zone->country, len);
72         else if (!strcasecmp(data, "language"))
73                 locked_copy_string(chan, buf, chan->language, len);
74         else if (!strcasecmp(data, "musicclass"))
75                 locked_copy_string(chan, buf, chan->musicclass, len);
76         else if (!strcasecmp(data, "state"))
77                 locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
78         else if (!strcasecmp(data, "channeltype"))
79                 locked_copy_string(chan, buf, chan->tech->type, len);
80         else if (!strcasecmp(data, "callgroup")) {
81                 char groupbuf[256];
82                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
83         } else if (!chan->tech->func_channel_read
84                  || chan->tech->func_channel_read(chan, function, data, buf, len)) {
85                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
86                 ret = -1;
87         }
88
89         return ret;
90 }
91
92 static int func_channel_write(struct ast_channel *chan, char *function,
93                               char *data, const char *value)
94 {
95         int ret = 0;
96
97         if (!strcasecmp(data, "language"))
98                 locked_string_field_set(chan, language, value);
99         else if (!strcasecmp(data, "musicclass"))
100                 locked_string_field_set(chan, musicclass, value);
101         else if (!strcasecmp(data, "callgroup"))
102                 chan->callgroup = ast_get_group(data);
103         else if (!chan->tech->func_channel_write
104                  || chan->tech->func_channel_write(chan, function, data, value)) {
105                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
106                                 data);
107                 ret = -1;
108         }
109
110         return ret;
111 }
112
113 static struct ast_custom_function channel_function = {
114         .name = "CHANNEL",
115         .synopsis = "Gets/sets various pieces of information about the channel.",
116         .syntax = "CHANNEL(item)",
117         .desc = "Gets/set various pieces of information about the channel.\n"
118                 "Standard items (provided by all channel technologies) are:\n"
119                 "R/O    audioreadformat         format currently being read\n"
120                 "R/O    audionativeformat       format used natively for audio\n"
121                 "R/O    audiowriteformat        format currently being written\n"
122                 "R/W    callgroup               call groups for call pickup\n"
123                 "R/O    channeltype             technology used for channel\n"
124                 "R/W    language                language for sounds played\n"
125                 "R/W    musicclass              class (from musiconhold.conf) for hold music\n"
126                 "R/O    state                   state for channel\n"
127                 "R/O    tonezone                zone for indications played\n"
128                 "R/O    videonativeformat       format used natively for video\n"
129                 "\n"
130                 "Additional items may be available from the channel driver providing\n"
131                 "the channel; see its documentation for details.\n"
132                 "\n"
133                 "Any item requested that is not available on the current channel will\n"
134                 "return an empty string.\n",
135         .read = func_channel_read,
136         .write = func_channel_write,
137 };
138
139 static char *tdesc = "Channel information dialplan function";
140
141 static int unload_module(void *mod)
142 {
143         return ast_custom_function_unregister(&channel_function);
144 }
145
146 static int load_module(void *mod)
147 {
148         return ast_custom_function_register(&channel_function);
149 }
150
151 static const char *description(void)
152 {
153         return tdesc;
154 }
155
156 static const char *key(void)
157 {
158         return ASTERISK_GPL_KEY;
159 }
160
161 STD_MOD(MOD_1 | NO_USECOUNT, NULL, NULL, NULL);