- mark some applications deprecated that already have replacements
[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 char *transfercapability_table[0x20] = {
55         "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
56         "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
57         "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
58         "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
59
60 static int func_channel_read(struct ast_channel *chan, char *function,
61                              char *data, char *buf, size_t len)
62 {
63         int ret = 0;
64
65         if (!strcasecmp(data, "audionativeformat"))
66                 /* use the _multiple version when chan->nativeformats holds multiple formats */
67                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_AUDIO_MASK); */
68                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
69         else if (!strcasecmp(data, "videonativeformat"))
70                 /* use the _multiple version when chan->nativeformats holds multiple formats */
71                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_VIDEO_MASK); */
72                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
73         else if (!strcasecmp(data, "audioreadformat"))
74                 ast_copy_string(buf, ast_getformatname(chan->readformat), len);
75         else if (!strcasecmp(data, "audiowriteformat"))
76                 ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
77         else if (!strcasecmp(data, "tonezone") && chan->zone)
78                 locked_copy_string(chan, buf, chan->zone->country, len);
79         else if (!strcasecmp(data, "language"))
80                 locked_copy_string(chan, buf, chan->language, len);
81         else if (!strcasecmp(data, "musicclass"))
82                 locked_copy_string(chan, buf, chan->musicclass, len);
83         else if (!strcasecmp(data, "state"))
84                 locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
85         else if (!strcasecmp(data, "channeltype"))
86                 locked_copy_string(chan, buf, chan->tech->type, len);
87         else if (!strcasecmp(data, "transfercapability"))
88                 locked_copy_string(chan, buf, transfercapability_table[chan->transfercapability & 0x1f], len);
89         else if (!strcasecmp(data, "callgroup")) {
90                 char groupbuf[256];
91                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
92         } else if (!chan->tech->func_channel_read
93                  || chan->tech->func_channel_read(chan, function, data, buf, len)) {
94                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
95                 ret = -1;
96         }
97
98         return ret;
99 }
100
101 static int func_channel_write(struct ast_channel *chan, char *function,
102                               char *data, const char *value)
103 {
104         int ret = 0;
105         signed char gainset;
106
107         if (!strcasecmp(data, "language"))
108                 locked_string_field_set(chan, language, value);
109         else if (!strcasecmp(data, "musicclass"))
110                 locked_string_field_set(chan, musicclass, value);
111         else if (!strcasecmp(data, "callgroup"))
112                 chan->callgroup = ast_get_group(data);
113         else if (!strcasecmp(data, "txgain")) {
114                 sscanf(value, "%hhd", &gainset);
115                 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
116         } else if (!strcasecmp(data, "rxgain")) {
117                 sscanf(value, "%hhd", &gainset);
118                 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
119         } else if (!strcasecmp(data, "transfercapability")) {
120                 unsigned short i;
121                 for (i = 0; i < 0x20; i++) {
122                         if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
123                                 chan->transfercapability = i;
124                                 break;
125                         }
126                 }
127         } else if (!chan->tech->func_channel_write
128                  || chan->tech->func_channel_write(chan, function, data, value)) {
129                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
130                                 data);
131                 ret = -1;
132         }
133
134         return ret;
135 }
136
137 static struct ast_custom_function channel_function = {
138         .name = "CHANNEL",
139         .synopsis = "Gets/sets various pieces of information about the channel.",
140         .syntax = "CHANNEL(item)",
141         .desc = "Gets/set various pieces of information about the channel.\n"
142                 "Standard items (provided by all channel technologies) are:\n"
143                 "R/O    audioreadformat         format currently being read\n"
144                 "R/O    audionativeformat       format used natively for audio\n"
145                 "R/O    audiowriteformat        format currently being written\n"
146                 "R/W    callgroup               call groups for call pickup\n"
147                 "R/O    channeltype             technology used for channel\n"
148                 "R/W    language                language for sounds played\n"
149                 "R/W    musicclass              class (from musiconhold.conf) for hold music\n"
150                 "R/W    rxgain                  set rxgain level on channel drivers that support it\n"
151                 "R/O    state                   state for channel\n"
152                 "R/O    tonezone                zone for indications played\n"
153                 "R/W    txgain                  set txgain level on channel drivers that support it\n"
154                 "R/O    videonativeformat       format used natively for video\n"
155                 "\n"
156                 "Additional items may be available from the channel driver providing\n"
157                 "the channel; see its documentation for details.\n"
158                 "\n"
159                 "Any item requested that is not available on the current channel will\n"
160                 "return an empty string.\n",
161         .read = func_channel_read,
162         .write = func_channel_write,
163 };
164
165 static char *tdesc = "Channel information dialplan function";
166
167 static int unload_module(void *mod)
168 {
169         return ast_custom_function_unregister(&channel_function);
170 }
171
172 static int load_module(void *mod)
173 {
174         return ast_custom_function_register(&channel_function);
175 }
176
177 static const char *description(void)
178 {
179         return tdesc;
180 }
181
182 static const char *key(void)
183 {
184         return ASTERISK_GPL_KEY;
185 }
186
187 STD_MOD(MOD_1 | NO_USECOUNT, NULL, NULL, NULL);