Make tonezone writeable in CHANNEL() (from my old func_tonezone.c)
[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, "tonezone")) {
112                 struct tone_zone *new_zone;
113                 if (!(new_zone = ast_get_indication_zone(data))) {
114                         ast_log(LOG_ERROR, "Unknown country code for tonezone. Check indications.conf for available country codes.\n");
115                         ret = -1;       
116                 } else 
117                         chan->zone = new_zone;
118         } else if (!strcasecmp(data, "callgroup"))
119                 chan->callgroup = ast_get_group(data);
120         else if (!strcasecmp(data, "txgain")) {
121                 sscanf(value, "%hhd", &gainset);
122                 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
123         } else if (!strcasecmp(data, "rxgain")) {
124                 sscanf(value, "%hhd", &gainset);
125                 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
126         } else if (!strcasecmp(data, "transfercapability")) {
127                 unsigned short i;
128                 for (i = 0; i < 0x20; i++) {
129                         if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
130                                 chan->transfercapability = i;
131                                 break;
132                         }
133                 }
134         } else if (!chan->tech->func_channel_write
135                  || chan->tech->func_channel_write(chan, function, data, value)) {
136                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
137                                 data);
138                 ret = -1;
139         }
140
141         return ret;
142 }
143
144 static struct ast_custom_function channel_function = {
145         .name = "CHANNEL",
146         .synopsis = "Gets/sets various pieces of information about the channel.",
147         .syntax = "CHANNEL(item)",
148         .desc = "Gets/set various pieces of information about the channel.\n"
149                 "Standard items (provided by all channel technologies) are:\n"
150                 "R/O    audioreadformat         format currently being read\n"
151                 "R/O    audionativeformat       format used natively for audio\n"
152                 "R/O    audiowriteformat        format currently being written\n"
153                 "R/W    callgroup               call groups for call pickup\n"
154                 "R/O    channeltype             technology used for channel\n"
155                 "R/W    language                language for sounds played\n"
156                 "R/W    musicclass              class (from musiconhold.conf) for hold music\n"
157                 "R/W    rxgain                  set rxgain level on channel drivers that support it\n"
158                 "R/O    state                   state for channel\n"
159                 "R/W    tonezone                zone for indications played\n"
160                 "R/W    txgain                  set txgain level on channel drivers that support it\n"
161                 "R/O    videonativeformat       format used natively for video\n"
162                 "\n"
163                 "Additional items may be available from the channel driver providing\n"
164                 "the channel; see its documentation for details.\n"
165                 "\n"
166                 "Any item requested that is not available on the current channel will\n"
167                 "return an empty string.\n",
168         .read = func_channel_read,
169         .write = func_channel_write,
170 };
171
172 static char *tdesc = "Channel information dialplan function";
173
174 static int unload_module(void *mod)
175 {
176         return ast_custom_function_unregister(&channel_function);
177 }
178
179 static int load_module(void *mod)
180 {
181         return ast_custom_function_register(&channel_function);
182 }
183
184 static const char *description(void)
185 {
186         return tdesc;
187 }
188
189 static const char *key(void)
190 {
191         return ASTERISK_GPL_KEY;
192 }
193
194 STD_MOD(MOD_1 | NO_USECOUNT, NULL, NULL, NULL);