Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
app_dtmfstore.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2021, Naveen Albert
5 *
6 * Naveen Albert <asterisk@phreaknet.org>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*! \file
20 *
21 * \brief Technology independent asynchronous DTMF collection
22 *
23 * \author Naveen Albert <asterisk@phreaknet.org>
24 *
25 * \ingroup functions
26 *
27 */
28
29/*** MODULEINFO
30 <support_level>extended</support_level>
31 ***/
32
33#include "asterisk.h"
34
35#include "asterisk/module.h"
36#include "asterisk/channel.h"
37#include "asterisk/pbx.h"
38#include "asterisk/framehook.h"
39#include "asterisk/app.h"
41
42/*** DOCUMENTATION
43 <application name="StoreDTMF" language="en_US">
44 <since>
45 <version>16.20.0</version>
46 <version>18.6.0</version>
47 </since>
48 <synopsis>
49 Stores DTMF digits transmitted or received on a channel.
50 </synopsis>
51 <syntax>
52 <parameter name="direction" required="true">
53 <para>Must be <literal>TX</literal> or <literal>RX</literal> to
54 store digits, or <literal>remove</literal> to disable.</para>
55 </parameter>
56 </syntax>
57 <description>
58 <para>The StoreDTMF function can be used to obtain digits sent in the
59 <literal>TX</literal> or <literal>RX</literal> direction of any channel.</para>
60 <para>The arguments are:</para>
61 <para><replaceable>var_name</replaceable>: Name of variable to which to append
62 digits.</para>
63 <para><replaceable>max_digits</replaceable>: The maximum number of digits to
64 store in the variable. Defaults to 0 (no maximum). After reading <literal>
65 maximum</literal> digits, no more digits will be stored.</para>
66 <example title="Store digits in CDR variable">
67 same => n,StoreDTMF(TX,CDR(digits))
68 </example>
69 <example title="Store up to 24 digits">
70 same => n,StoreDTMF(RX,testvar,24)
71 </example>
72 <example title="Disable digit collection">
73 same => n,StoreDTMF(remove)
74 </example>
75 </description>
76 </application>
77 ***/
78
79static char *app = "StoreDTMF";
80
81/*! \brief Private data structure used with the function's datastore */
84 char *rx_var;
85 char *tx_var;
87};
88
89static void datastore_destroy_cb(void *data) {
90 struct dtmf_store_data *d;
91 d = data;
92 if (d) {
93 if (d->rx_var) {
94 ast_free(d->rx_var);
95 }
96 if (d->tx_var) {
97 ast_free(d->tx_var);
98 }
99 ast_free(data);
100 }
101}
102
103/*! \brief The channel datastore the function uses to store state */
105 .type = "dtmf_store",
106 .destroy = datastore_destroy_cb
107};
108
109/*! \internal \brief Store digits tx/rx on the channel */
110static int remove_dtmf_store(struct ast_channel *chan)
111{
112 struct ast_datastore *datastore = NULL;
113 struct dtmf_store_data *data;
114 SCOPED_CHANNELLOCK(chan_lock, chan);
115
117 if (!datastore) {
118 ast_log(AST_LOG_WARNING, "Cannot remove StoreDTMF from %s: StoreDTMF not currently enabled\n",
119 ast_channel_name(chan));
120 return -1;
121 }
122 data = datastore->data;
123
124 if (ast_framehook_detach(chan, data->framehook_id)) {
125 ast_log(AST_LOG_WARNING, "Failed to remove StoreDTMF framehook from channel %s\n",
126 ast_channel_name(chan));
127 return -1;
128 }
129
130 if (ast_channel_datastore_remove(chan, datastore)) {
131 ast_log(AST_LOG_WARNING, "Failed to remove StoreDTMF datastore from channel %s\n",
132 ast_channel_name(chan));
133 return -1;
134 }
135 ast_datastore_free(datastore);
136
137 return 0;
138}
139
140/*! \brief Frame hook that is called to intercept digit/undigit */
141static struct ast_frame *dtmf_store_framehook(struct ast_channel *chan,
142 struct ast_frame *f, enum ast_framehook_event event, void *data)
143{
144 char currentdata[512];
145 char varnamesub[64];
146 char *varname = NULL;
147 struct dtmf_store_data *framedata = data;
148 int len;
149
150 if (!f || !framedata) {
151 return f;
152 }
153
155 return f;
156 }
157
158 if (f->frametype != AST_FRAME_DTMF_END) {
159 return f;
160 }
161
162 /* If this is DTMF then store the digits */
163 if (event == AST_FRAMEHOOK_EVENT_READ && framedata->rx_var) { /* coming from source */
164 varname = framedata->rx_var;
165 } else if (event == AST_FRAMEHOOK_EVENT_WRITE && framedata->tx_var) { /* going to source */
166 varname = framedata->tx_var;
167 }
168
169 if (!varname) {
170 return f;
171 }
172
173 sprintf(varnamesub, "${%s}", varname);
174 pbx_substitute_variables_helper(chan, varnamesub, currentdata, 511);
175 /* pbx_builtin_getvar_helper works for regular vars but not CDR vars */
176 if (ast_strlen_zero(currentdata)) { /* var doesn't exist yet */
177 ast_debug(3, "Creating new digit store: %s\n", varname);
178 }
179 len = strlen(currentdata);
180 if (framedata->maxdigits > 0 && len >= framedata->maxdigits) {
181 ast_debug(3, "Reached digit limit: %d\n", framedata->maxdigits);
182 remove_dtmf_store(chan); /* reached max digit count, stop now */
183 return f;
184 } else {
185 char newdata[len + 2]; /* one more char + terminator */
186 if (len > 0) {
187 ast_copy_string(newdata, currentdata, len + 2);
188 }
189 newdata[len] = (unsigned) f->subclass.integer;
190 newdata[len + 1] = '\0';
191 ast_debug(3, "Appending to digit store: now %s\n", newdata);
192 pbx_builtin_setvar_helper(chan, varname, newdata);
193 }
194 return f;
195}
196
197/*! \internal \brief Enable digit interception on the channel */
198static int dtmfstore_exec(struct ast_channel *chan, const char *appdata)
199{
200 struct ast_datastore *datastore;
201 struct dtmf_store_data *data;
202 static struct ast_framehook_interface digit_framehook_interface = {
204 .event_cb = dtmf_store_framehook,
205 .disable_inheritance = 1,
206 };
207 char *parse = ast_strdupa(appdata);
210 AST_APP_ARG(varname);
211 AST_APP_ARG(maxdigits);
212 );
213 SCOPED_CHANNELLOCK(chan_lock, chan);
215
216 if (ast_strlen_zero(appdata)) {
217 ast_log(AST_LOG_WARNING, "StoreDTMF requires an argument\n");
218 return -1;
219 }
220
221 if (!strcasecmp(args.direction, "remove")) {
222 return remove_dtmf_store(chan);
223 }
224
226 if (datastore) {
227 ast_log(AST_LOG_WARNING, "StoreDTMF already set on '%s'\n",
228 ast_channel_name(chan));
229 return 0;
230 }
231
233 if (!datastore) {
234 return -1;
235 }
236
237 data = ast_calloc(1, sizeof(*data));
238 if (!data) {
239 ast_datastore_free(datastore);
240 return -1;
241 }
242
243 digit_framehook_interface.data = data;
244
245 data->rx_var = NULL;
246 data->tx_var = NULL;
247 data->maxdigits = 0;
248
249 if (!strcasecmp(args.direction, "tx")) {
250 data->tx_var = ast_strdup(args.varname);
251 } else if (!strcasecmp(args.direction, "rx")) {
252 data->rx_var = ast_strdup(args.varname);
253 } else {
254 ast_log(LOG_ERROR, "Direction must be either RX or TX\n");
255 return -1;
256 }
257
258 if (!ast_strlen_zero(args.maxdigits)) {
259 if (ast_str_to_int(args.maxdigits,&(data->maxdigits))) {
260 ast_log(LOG_ERROR, "Invalid integer: %s\n", args.maxdigits);
261 return -1;
262 }
263 if (data->maxdigits < 0) {
264 ast_log(LOG_ERROR, "Invalid natural number: %d\n", data->maxdigits);
265 return -1;
266 } else if (data->maxdigits == 0) {
267 ast_log(LOG_WARNING, "No maximum digit count set\n");
268 }
269 }
270
271 data->framehook_id = ast_framehook_attach(chan, &digit_framehook_interface);
272 if (data->framehook_id < 0) {
273 ast_log(AST_LOG_WARNING, "Failed to attach StoreDTMF framehook to '%s'\n",
274 ast_channel_name(chan));
275 ast_datastore_free(datastore);
276 ast_free(data);
277 return -1;
278 }
279 datastore->data = data;
280
281 ast_channel_datastore_add(chan, datastore);
282
283 return 0;
284}
285
286static int unload_module(void)
287{
289}
290
291static int load_module(void)
292{
294}
295
296AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Technology independent async DTMF storage");
static const struct ast_datastore_info dtmf_store_datastore
The channel datastore the function uses to store state.
static struct ast_frame * dtmf_store_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data)
Frame hook that is called to intercept digit/undigit.
static char * app
Definition: app_dtmfstore.c:79
static int dtmfstore_exec(struct ast_channel *chan, const char *appdata)
static void datastore_destroy_cb(void *data)
Definition: app_dtmfstore.c:89
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Technology independent async DTMF storage")
static int load_module(void)
static int unload_module(void)
static int remove_dtmf_store(struct ast_channel *chan)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2414
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
Definition: channel.c:2423
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2428
Conversion utility functions.
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition: conversions.c:44
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:85
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
FrameHook Architecture.
int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
Attach an framehook onto a channel for frame interception.
Definition: framehook.c:132
ast_framehook_event
These are the types of events that the framehook's event callback can receive.
Definition: framehook.h:151
@ AST_FRAMEHOOK_EVENT_WRITE
Definition: framehook.h:153
@ AST_FRAMEHOOK_EVENT_READ
Definition: framehook.h:152
int ast_framehook_detach(struct ast_channel *chan, int framehook_id)
Detach an framehook from a channel.
Definition: framehook.c:177
#define AST_FRAMEHOOK_INTERFACE_VERSION
Definition: framehook.h:227
direction
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
@ AST_FRAME_DTMF_END
#define AST_LOG_WARNING
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_WARNING
#define SCOPED_CHANNELLOCK(varname, chan)
scoped lock specialization for channels.
Definition: lock.h:623
Asterisk module definitions.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
Core PBX routines and definitions.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: ael_main.c:211
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Main Channel structure associated with a channel.
Structure for a data store type.
Definition: datastore.h:31
const char * type
Definition: datastore.h:32
Structure for a data store object.
Definition: datastore.h:64
void * data
Definition: datastore.h:66
Data structure associated with a single frame of data.
union ast_frame::@228 data
struct ast_frame_subclass subclass
enum ast_frame_type frametype
Private data structure used with the function's datastore.
Definition: app_dtmfstore.c:82
Definition: astman.c:222
const char * args
static struct test_val d