forked from asterisk/asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_dtmfstore.c
297 lines (260 loc) · 8.06 KB
/
app_dtmfstore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2021, Naveen Albert
*
* Naveen Albert <[email protected]>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Technology independent asynchronous DTMF collection
*
* \author Naveen Albert <[email protected]>
*
* \ingroup functions
*
*/
/*** MODULEINFO
<support_level>extended</support_level>
***/
#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/framehook.h"
#include "asterisk/app.h"
#include "asterisk/conversions.h"
/*** DOCUMENTATION
<application name="StoreDTMF" language="en_US">
<since>
<version>16.20.0</version>
<version>18.6.0</version>
<version>19.0.0</version>
</since>
<synopsis>
Stores DTMF digits transmitted or received on a channel.
</synopsis>
<syntax>
<parameter name="direction" required="true">
<para>Must be <literal>TX</literal> or <literal>RX</literal> to
store digits, or <literal>remove</literal> to disable.</para>
</parameter>
</syntax>
<description>
<para>The StoreDTMF function can be used to obtain digits sent in the
<literal>TX</literal> or <literal>RX</literal> direction of any channel.</para>
<para>The arguments are:</para>
<para><replaceable>var_name</replaceable>: Name of variable to which to append
digits.</para>
<para><replaceable>max_digits</replaceable>: The maximum number of digits to
store in the variable. Defaults to 0 (no maximum). After reading <literal>
maximum</literal> digits, no more digits will be stored.</para>
<example title="Store digits in CDR variable">
same => n,StoreDTMF(TX,CDR(digits))
</example>
<example title="Store up to 24 digits">
same => n,StoreDTMF(RX,testvar,24)
</example>
<example title="Disable digit collection">
same => n,StoreDTMF(remove)
</example>
</description>
</application>
***/
static char *app = "StoreDTMF";
/*! \brief Private data structure used with the function's datastore */
struct dtmf_store_data {
int framehook_id;
char *rx_var;
char *tx_var;
int maxdigits;
};
static void datastore_destroy_cb(void *data) {
struct dtmf_store_data *d;
d = data;
if (d) {
if (d->rx_var) {
ast_free(d->rx_var);
}
if (d->tx_var) {
ast_free(d->tx_var);
}
ast_free(data);
}
}
/*! \brief The channel datastore the function uses to store state */
static const struct ast_datastore_info dtmf_store_datastore = {
.type = "dtmf_store",
.destroy = datastore_destroy_cb
};
/*! \internal \brief Store digits tx/rx on the channel */
static int remove_dtmf_store(struct ast_channel *chan)
{
struct ast_datastore *datastore = NULL;
struct dtmf_store_data *data;
SCOPED_CHANNELLOCK(chan_lock, chan);
datastore = ast_channel_datastore_find(chan, &dtmf_store_datastore, NULL);
if (!datastore) {
ast_log(AST_LOG_WARNING, "Cannot remove StoreDTMF from %s: StoreDTMF not currently enabled\n",
ast_channel_name(chan));
return -1;
}
data = datastore->data;
if (ast_framehook_detach(chan, data->framehook_id)) {
ast_log(AST_LOG_WARNING, "Failed to remove StoreDTMF framehook from channel %s\n",
ast_channel_name(chan));
return -1;
}
if (ast_channel_datastore_remove(chan, datastore)) {
ast_log(AST_LOG_WARNING, "Failed to remove StoreDTMF datastore from channel %s\n",
ast_channel_name(chan));
return -1;
}
ast_datastore_free(datastore);
return 0;
}
/*! \brief Frame hook that is called to intercept digit/undigit */
static struct ast_frame *dtmf_store_framehook(struct ast_channel *chan,
struct ast_frame *f, enum ast_framehook_event event, void *data)
{
char currentdata[512];
char varnamesub[64];
char *varname = NULL;
struct dtmf_store_data *framedata = data;
int len;
if (!f || !framedata) {
return f;
}
if ((event != AST_FRAMEHOOK_EVENT_WRITE) && (event != AST_FRAMEHOOK_EVENT_READ)) {
return f;
}
if (f->frametype != AST_FRAME_DTMF_END) {
return f;
}
/* If this is DTMF then store the digits */
if (event == AST_FRAMEHOOK_EVENT_READ && framedata->rx_var) { /* coming from source */
varname = framedata->rx_var;
} else if (event == AST_FRAMEHOOK_EVENT_WRITE && framedata->tx_var) { /* going to source */
varname = framedata->tx_var;
}
if (!varname) {
return f;
}
sprintf(varnamesub, "${%s}", varname);
pbx_substitute_variables_helper(chan, varnamesub, currentdata, 511);
/* pbx_builtin_getvar_helper works for regular vars but not CDR vars */
if (ast_strlen_zero(currentdata)) { /* var doesn't exist yet */
ast_debug(3, "Creating new digit store: %s\n", varname);
}
len = strlen(currentdata);
if (framedata->maxdigits > 0 && len >= framedata->maxdigits) {
ast_debug(3, "Reached digit limit: %d\n", framedata->maxdigits);
remove_dtmf_store(chan); /* reached max digit count, stop now */
return f;
} else {
char newdata[len + 2]; /* one more char + terminator */
if (len > 0) {
ast_copy_string(newdata, currentdata, len + 2);
}
newdata[len] = (unsigned) f->subclass.integer;
newdata[len + 1] = '\0';
ast_debug(3, "Appending to digit store: now %s\n", newdata);
pbx_builtin_setvar_helper(chan, varname, newdata);
}
return f;
}
/*! \internal \brief Enable digit interception on the channel */
static int dtmfstore_exec(struct ast_channel *chan, const char *appdata)
{
struct ast_datastore *datastore;
struct dtmf_store_data *data;
static struct ast_framehook_interface digit_framehook_interface = {
.version = AST_FRAMEHOOK_INTERFACE_VERSION,
.event_cb = dtmf_store_framehook,
.disable_inheritance = 1,
};
char *parse = ast_strdupa(appdata);
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(direction);
AST_APP_ARG(varname);
AST_APP_ARG(maxdigits);
);
SCOPED_CHANNELLOCK(chan_lock, chan);
AST_STANDARD_APP_ARGS(args, parse);
if (ast_strlen_zero(appdata)) {
ast_log(AST_LOG_WARNING, "StoreDTMF requires an argument\n");
return -1;
}
if (!strcasecmp(args.direction, "remove")) {
return remove_dtmf_store(chan);
}
datastore = ast_channel_datastore_find(chan, &dtmf_store_datastore, NULL);
if (datastore) {
ast_log(AST_LOG_WARNING, "StoreDTMF already set on '%s'\n",
ast_channel_name(chan));
return 0;
}
datastore = ast_datastore_alloc(&dtmf_store_datastore, NULL);
if (!datastore) {
return -1;
}
data = ast_calloc(1, sizeof(*data));
if (!data) {
ast_datastore_free(datastore);
return -1;
}
digit_framehook_interface.data = data;
data->rx_var = NULL;
data->tx_var = NULL;
data->maxdigits = 0;
if (!strcasecmp(args.direction, "tx")) {
data->tx_var = ast_strdup(args.varname);
} else if (!strcasecmp(args.direction, "rx")) {
data->rx_var = ast_strdup(args.varname);
} else {
ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
return -1;
}
if (!ast_strlen_zero(args.maxdigits)) {
if (ast_str_to_int(args.maxdigits,&(data->maxdigits))) {
ast_log(LOG_ERROR, "Invalid integer: %s\n", args.maxdigits);
return -1;
}
if (data->maxdigits < 0) {
ast_log(LOG_ERROR, "Invalid natural number: %d\n", data->maxdigits);
return -1;
} else if (data->maxdigits == 0) {
ast_log(LOG_WARNING, "No maximum digit count set\n");
}
}
data->framehook_id = ast_framehook_attach(chan, &digit_framehook_interface);
if (data->framehook_id < 0) {
ast_log(AST_LOG_WARNING, "Failed to attach StoreDTMF framehook to '%s'\n",
ast_channel_name(chan));
ast_datastore_free(datastore);
ast_free(data);
return -1;
}
datastore->data = data;
ast_channel_datastore_add(chan, datastore);
return 0;
}
static int unload_module(void)
{
return ast_unregister_application(app);
}
static int load_module(void)
{
return ast_register_application_xml(app, dtmfstore_exec);
}
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Technology independent async DTMF storage");