Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
app_cdr.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Martin Pycko <martinp@digium.com>
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 Applications connected with CDR engine
22 *
23 * \author Martin Pycko <martinp@digium.com>
24 *
25 * \ingroup applications
26 */
27
28/*** MODULEINFO
29 <support_level>core</support_level>
30 ***/
31
32#include "asterisk.h"
33
34#include "asterisk/channel.h"
35#include "asterisk/module.h"
36#include "asterisk/app.h"
37#include "asterisk/stasis.h"
39
40/*** DOCUMENTATION
41 <application name="ResetCDR" language="en_US">
42 <since>
43 <version>12.0.0</version>
44 </since>
45 <synopsis>
46 Resets the Call Data Record.
47 </synopsis>
48 <syntax>
49 <parameter name="options">
50 <optionlist>
51 <option name="v">
52 <para>Save the CDR variables during the reset.</para>
53 </option>
54 </optionlist>
55 </parameter>
56 </syntax>
57 <description>
58 <para>This application causes the Call Data Record to be reset.
59 Depending on the flags passed in, this can have several effects.
60 With no options, a reset does the following:</para>
61 <para>1. The <literal>start</literal> time is set to the current time.</para>
62 <para>2. If the channel is answered, the <literal>answer</literal> time is set to the
63 current time.</para>
64 <para>3. All variables are wiped from the CDR. Note that this step
65 can be prevented with the <literal>v</literal> option.</para>
66 </description>
67 <see-also>
68 <ref type="application">ForkCDR</ref>
69 <ref type="function">CDR_PROP</ref>
70 </see-also>
71 </application>
72 ***/
73
74static const char resetcdr_app[] = "ResetCDR";
75
78 OPT_KEEP_VARS = (1 << 1),
79 OPT_ENABLE = (1 << 2),
80};
81
84});
85
86STASIS_MESSAGE_TYPE_DEFN_LOCAL(appcdr_message_type);
87
88/*! \internal \brief Payload for the Stasis message sent to manipulate a CDR */
90 /*! The name of the channel to be manipulated */
91 const char *channel_name;
92 /*! Reset the CDR */
93 unsigned int reset:1;
94 /*! If reseting the CDR, keep the variables */
95 unsigned int keep_variables:1;
96};
97
98static void appcdr_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
99{
100 struct app_cdr_message_payload *payload;
101
102 if (stasis_message_type(message) != appcdr_message_type()) {
103 return;
104 }
105
106 payload = stasis_message_data(message);
107 if (!payload) {
108 return;
109 }
110
111 if (payload->reset) {
112 if (ast_cdr_reset(payload->channel_name, payload->keep_variables)) {
113 ast_log(AST_LOG_WARNING, "Failed to reset CDRs on channel %s\n", payload->channel_name);
114 }
115 }
116}
117
118static int publish_app_cdr_message(struct ast_channel *chan, struct app_cdr_message_payload *payload)
119{
122
123 if (!router) {
124 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: no message router\n",
125 ast_channel_name(chan));
126 return -1;
127 }
128
129 message = stasis_message_create(appcdr_message_type(), payload);
130 if (!message) {
131 ast_log(AST_LOG_WARNING, "Failed to manipulate CDR for channel %s: unable to create message\n",
132 payload->channel_name);
133 return -1;
134 }
136
137 return 0;
138}
139
140static int resetcdr_exec(struct ast_channel *chan, const char *data)
141{
142 RAII_VAR(struct app_cdr_message_payload *, payload,
143 ao2_alloc(sizeof(*payload), NULL), ao2_cleanup);
144 char *args;
145 struct ast_flags flags = { 0 };
146
147 if (!payload) {
148 return -1;
149 }
150
151 if (!ast_strlen_zero(data)) {
152 args = ast_strdupa(data);
154 }
155
156 payload->channel_name = ast_channel_name(chan);
157 payload->reset = 1;
158
160 payload->keep_variables = 1;
161 }
162
163 return publish_app_cdr_message(chan, payload);
164}
165
166static int unload_module(void)
167{
169
170 if (router) {
171 stasis_message_router_remove(router, appcdr_message_type());
172 }
173 STASIS_MESSAGE_TYPE_CLEANUP(appcdr_message_type);
175 return 0;
176}
177
178static int load_module(void)
179{
181 int res = 0;
182
183 if (!router) {
185 }
186
187 res |= STASIS_MESSAGE_TYPE_INIT(appcdr_message_type);
189 res |= stasis_message_router_add(router, appcdr_message_type(), appcdr_callback, NULL);
190
191 if (res) {
194 }
196}
197
198AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Tell Asterisk to not maintain a CDR for the current call");
static const struct ast_app_option resetcdr_opts[128]
Definition: app_cdr.c:84
reset_cdr_options
Definition: app_cdr.c:76
@ OPT_DISABLE_DISPATCH
Definition: app_cdr.c:77
@ OPT_KEEP_VARS
Definition: app_cdr.c:78
@ OPT_ENABLE
Definition: app_cdr.c:79
STASIS_MESSAGE_TYPE_DEFN_LOCAL(appcdr_message_type)
static int publish_app_cdr_message(struct ast_channel *chan, struct app_cdr_message_payload *payload)
Definition: app_cdr.c:118
static void appcdr_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: app_cdr.c:98
static int resetcdr_exec(struct ast_channel *chan, const char *data)
Definition: app_cdr.c:140
static int load_module(void)
Definition: app_cdr.c:178
static int unload_module(void)
Definition: app_cdr.c:166
static const char resetcdr_app[]
Definition: app_cdr.c:74
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
struct stasis_message_router * ast_cdr_message_router(void)
Return the message router for the CDR engine.
Definition: cdr.c:4427
@ AST_CDR_FLAG_KEEP_VARS
Definition: cdr.h:243
int ast_cdr_reset(const char *channel_name, int keep_variables)
Reset the detail record.
Definition: cdr.c:3731
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_OPTIONS(holder, options...)
Declares an array of options for an application.
#define AST_APP_OPTION(option, flagno)
Declares an application option that does not accept an argument.
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:3066
#define AST_LOG_WARNING
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#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
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:640
static struct stasis_message_router * router
struct stasis_forward * sub
Definition: res_corosync.c:240
#define NULL
Definition: resample.c:96
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
struct stasis_message_type * stasis_message_type(const struct stasis_message *msg)
Get the message type for a stasis_message.
#define STASIS_MESSAGE_TYPE_CLEANUP(name)
Boiler-plate messaging macro for cleaning up message types.
Definition: stasis.h:1515
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1493
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
struct stasis_message * stasis_message_create(struct stasis_message_type *type, void *data)
Create a new message.
void stasis_message_router_remove(struct stasis_message_router *router, struct stasis_message_type *message_type)
Remove a route from a message router.
int stasis_message_router_add(struct stasis_message_router *router, struct stasis_message_type *message_type, stasis_subscription_cb callback, void *data)
Add a route to a message router.
void stasis_message_router_publish_sync(struct stasis_message_router *router, struct stasis_message *message)
Publish a message to a message router's subscription synchronously.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
unsigned int reset
Definition: app_cdr.c:93
const char * channel_name
Definition: app_cdr.c:91
unsigned int keep_variables
Definition: app_cdr.c:95
Main Channel structure associated with a channel.
Structure used to handle boolean flags.
Definition: utils.h:199
unsigned int flags
Definition: utils.h:200
const char * args
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941