another bunch of include removals (errno.h and asterisk/logger.h)
[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  * \ingroup functions
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "asterisk/module.h"
31 #include "asterisk/channel.h"
32 #include "asterisk/pbx.h"
33 #include "asterisk/utils.h"
34 #include "asterisk/app.h"
35 #include "asterisk/indications.h"
36 #include "asterisk/stringfields.h"
37
38 #define locked_copy_string(chan, dest, source, len) \
39         do { \
40                 ast_channel_lock(chan); \
41                 ast_copy_string(dest, source, len); \
42                 ast_channel_unlock(chan); \
43         } while (0)
44 #define locked_string_field_set(chan, field, source) \
45         do { \
46                 ast_channel_lock(chan); \
47                 ast_string_field_set(chan, field, source); \
48                 ast_channel_unlock(chan); \
49         } while (0)
50
51 char *transfercapability_table[0x20] = {
52         "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
53         "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
54         "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
55         "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
56
57 static int func_channel_read(struct ast_channel *chan, const char *function,
58                              char *data, char *buf, size_t len)
59 {
60         int ret = 0;
61
62         if (!strcasecmp(data, "audionativeformat"))
63                 /* use the _multiple version when chan->nativeformats holds multiple formats */
64                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_AUDIO_MASK); */
65                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
66         else if (!strcasecmp(data, "videonativeformat"))
67                 /* use the _multiple version when chan->nativeformats holds multiple formats */
68                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_VIDEO_MASK); */
69                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
70         else if (!strcasecmp(data, "audioreadformat"))
71                 ast_copy_string(buf, ast_getformatname(chan->readformat), len);
72         else if (!strcasecmp(data, "audiowriteformat"))
73                 ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
74         else if (!strcasecmp(data, "tonezone") && chan->zone)
75                 locked_copy_string(chan, buf, chan->zone->country, len);
76         else if (!strcasecmp(data, "language"))
77                 locked_copy_string(chan, buf, chan->language, len);
78         else if (!strcasecmp(data, "musicclass"))
79                 locked_copy_string(chan, buf, chan->musicclass, len);
80         else if (!strcasecmp(data, "state"))
81                 locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
82         else if (!strcasecmp(data, "channeltype"))
83                 locked_copy_string(chan, buf, chan->tech->type, len);
84         else if (!strcasecmp(data, "transfercapability"))
85                 locked_copy_string(chan, buf, transfercapability_table[chan->transfercapability & 0x1f], len);
86         else if (!strcasecmp(data, "callgroup")) {
87                 char groupbuf[256];
88                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
89         } else if (!chan->tech->func_channel_read
90                  || chan->tech->func_channel_read(chan, function, data, buf, len)) {
91                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
92                 ret = -1;
93         }
94
95         return ret;
96 }
97
98 static int func_channel_write(struct ast_channel *chan, const char *function,
99                               char *data, const char *value)
100 {
101         int ret = 0;
102         signed char gainset;
103
104         if (!strcasecmp(data, "language"))
105                 locked_string_field_set(chan, language, value);
106         else if (!strcasecmp(data, "musicclass"))
107                 locked_string_field_set(chan, musicclass, value);
108         else if (!strcasecmp(data, "tonezone")) {
109                 struct ind_tone_zone *new_zone;
110                 if (!(new_zone = ast_get_indication_zone(value))) {
111                         ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
112                         ret = -1;       
113                 } else 
114                         chan->zone = new_zone;
115         } else if (!strcasecmp(data, "callgroup"))
116                 chan->callgroup = ast_get_group(value);
117         else if (!strcasecmp(data, "txgain")) {
118                 sscanf(value, "%hhd", &gainset);
119                 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
120         } else if (!strcasecmp(data, "rxgain")) {
121                 sscanf(value, "%hhd", &gainset);
122                 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
123         } else if (!strcasecmp(data, "transfercapability")) {
124                 unsigned short i;
125                 for (i = 0; i < 0x20; i++) {
126                         if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
127                                 chan->transfercapability = i;
128                                 break;
129                         }
130                 }
131         } else if (!chan->tech->func_channel_write
132                  || chan->tech->func_channel_write(chan, function, data, value)) {
133                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
134                                 data);
135                 ret = -1;
136         }
137
138         return ret;
139 }
140
141 static struct ast_custom_function channel_function = {
142         .name = "CHANNEL",
143         .synopsis = "Gets/sets various pieces of information about the channel.",
144         .syntax = "CHANNEL(item)",
145         .desc = "Gets/set various pieces of information about the channel.\n"
146                 "Standard items (provided by all channel technologies) are:\n"
147                 "R/O    audioreadformat    format currently being read\n"
148                 "R/O    audionativeformat  format used natively for audio\n"
149                 "R/O    audiowriteformat   format currently being written\n"
150                 "R/W    callgroup          call groups for call pickup\n"
151                 "R/O    channeltype        technology used for channel\n"
152                 "R/W    language           language for sounds played\n"
153                 "R/W    musicclass         class (from musiconhold.conf) for hold music\n"
154                 "R/W    rxgain             set rxgain level on channel drivers that support it\n"
155                 "R/O    state              state for channel\n"
156                 "R/W    tonezone           zone for indications played\n"
157                 "R/W    txgain             set txgain level on channel drivers that support it\n"
158                 "R/O    videonativeformat  format used natively for video\n"
159                 "\n"
160                 "chan_sip provides the following additional options:\n"
161                 "R/O    rtpqos             Get QOS information about the RTP stream\n"
162                 "       This option takes two additional arguments:\n"
163                 "  Argument 1:\n"
164                 "    audio                 Get data about the audio stream\n"
165                 "    video                 Get data about the video stream\n"
166                 "    text                  Get data about the text stream\n"
167                 "  Argument 2:\n"
168                 "    local_ssrc            Local SSRC (stream ID)\n"
169                 "    local_lostpackets     Local lost packets\n"
170                 "    local_jitter          Local calculated jitter\n"
171                 "    local_count           Number of received packets\n"
172                 "    remote_ssrc           Remote SSRC (stream ID)\n"
173                 "    remote_lostpackets    Remote lost packets\n"
174                 "    remote_jitter         Remote reported jitter\n"
175                 "    remote_count          Number of transmitted packets\n"
176                 "    rtt                   Round trip time\n"
177                 "    all                   All statistics (in a form suited to logging, but not for parsing)\n"
178                 "R/O    rtpdest            Get remote RTP destination information\n"
179                 "       This option takes one additional argument:\n"
180                 "  Argument 1:\n"
181                 "    audio                 Get audio destination\n"
182                 "    video                 Get video destination\n"
183                 "\n"
184                 "chan_iax2 provides the following additional options:\n"
185                 "R/W    osptoken           Get or set the OSP token information for a call\n"
186                 "\n"
187                 "Additional items may be available from the channel driver providing\n"
188                 "the channel; see its documentation for details.\n"
189                 "\n"
190                 "Any item requested that is not available on the current channel will\n"
191                 "return an empty string.\n",
192         .read = func_channel_read,
193         .write = func_channel_write,
194 };
195
196 static int unload_module(void)
197 {
198         return ast_custom_function_unregister(&channel_function);
199 }
200
201 static int load_module(void)
202 {
203         return ast_custom_function_register(&channel_function);
204 }
205
206 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan function");