2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
20 * \brief Call Detail Record API
23 #ifndef _ASTERISK_CDR_H
24 #define _ASTERISK_CDR_H
27 #define AST_CDR_FLAG_KEEP_VARS (1 << 0)
28 #define AST_CDR_FLAG_POSTED (1 << 1)
29 #define AST_CDR_FLAG_LOCKED (1 << 2)
30 #define AST_CDR_FLAG_CHILD (1 << 3)
31 #define AST_CDR_FLAG_POST_DISABLED (1 << 4)
32 #define AST_CDR_FLAG_POST_ENABLE (1 << 5)
34 /*! \name CDR Flags */
36 #define AST_CDR_NULL 0
37 #define AST_CDR_FAILED (1 << 0)
38 #define AST_CDR_BUSY (1 << 1)
39 #define AST_CDR_NOANSWER (1 << 2)
40 #define AST_CDR_ANSWERED (1 << 3)
43 /*! \name CDR AMA Flags */
45 #define AST_CDR_OMIT (1)
46 #define AST_CDR_BILLING (2)
47 #define AST_CDR_DOCUMENTATION (3)
50 #define AST_MAX_USER_FIELD 256
51 #define AST_MAX_ACCOUNT_CODE 20
53 /* Include channel.h after relevant declarations it will need */
54 #include "asterisk/channel.h"
55 #include "asterisk/utils.h"
57 /*! \brief Responsible for call detail data */
59 /*! Caller*ID with text */
60 char clid[AST_MAX_EXTENSION];
61 /*! Caller*ID number */
62 char src[AST_MAX_EXTENSION];
63 /*! Destination extension */
64 char dst[AST_MAX_EXTENSION];
65 /*! Destination context */
66 char dcontext[AST_MAX_EXTENSION];
68 char channel[AST_MAX_EXTENSION];
69 /*! Destination channel if appropriate */
70 char dstchannel[AST_MAX_EXTENSION];
71 /*! Last application if appropriate */
72 char lastapp[AST_MAX_EXTENSION];
73 /*! Last application data */
74 char lastdata[AST_MAX_EXTENSION];
78 struct timeval answer;
81 /*! Total time in system, in seconds */
83 /*! Total time call is up, in seconds */
85 /*! What happened to the call */
87 /*! What flags to use */
89 /*! What account number to use */
90 char accountcode[AST_MAX_ACCOUNT_CODE];
93 /*! Unique Channel Identifier */
96 char userfield[AST_MAX_USER_FIELD];
98 /*! A linked list for variables */
99 struct varshead varshead;
101 struct ast_cdr *next;
104 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw);
105 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur);
106 int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char delim, char sep, int recur);
107 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur);
108 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr);
109 int ast_cdr_log_unanswered(void);
111 typedef int (*ast_cdrbe)(struct ast_cdr *cdr);
113 /*! \brief Return TRUE if CDR subsystem is enabled */
114 int check_cdr_enabled(void);
117 * \brief Allocate a CDR record
118 * \retval a malloc'd ast_cdr structure
119 * \retval NULL on error (malloc failure)
121 struct ast_cdr *ast_cdr_alloc(void);
124 * \brief Duplicate a record
125 * \retval a malloc'd ast_cdr structure,
126 * \retval NULL on error (malloc failure)
128 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr);
131 * \brief Free a CDR record
132 * \param cdr ast_cdr structure to free
135 void ast_cdr_free(struct ast_cdr *cdr);
138 * \brief Discard and free a CDR record
139 * \param cdr ast_cdr structure to free
140 * Returns nothing -- same as free, but no checks or complaints
142 void ast_cdr_discard(struct ast_cdr *cdr);
145 * \brief Initialize based on a channel
146 * \param cdr Call Detail Record to use for channel
147 * \param chan Channel to bind CDR with
148 * Initializes a CDR and associates it with a particular channel
149 * \return 0 by default
151 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *chan);
154 * \brief Initialize based on a channel
155 * \param cdr Call Detail Record to use for channel
156 * \param chan Channel to bind CDR with
157 * Initializes a CDR and associates it with a particular channel
158 * \return 0 by default
160 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *chan);
163 * \brief Register a CDR handling engine
164 * \param name name associated with the particular CDR handler
165 * \param desc description of the CDR handler
166 * \param be function pointer to a CDR handler
167 * Used to register a Call Detail Record handler.
168 * \retval 0 on success.
169 * \retval -1 on error
171 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be);
174 * \brief Unregister a CDR handling engine
175 * \param name name of CDR handler to unregister
176 * Unregisters a CDR by it's name
178 void ast_cdr_unregister(const char *name);
181 * \brief Start a call
182 * \param cdr the cdr you wish to associate with the call
183 * Starts all CDR stuff necessary for monitoring a call
186 void ast_cdr_start(struct ast_cdr *cdr);
188 /*! \brief Answer a call
189 * \param cdr the cdr you wish to associate with the call
190 * Starts all CDR stuff necessary for doing CDR when answering a call
191 * \note NULL argument is just fine.
193 void ast_cdr_answer(struct ast_cdr *cdr);
196 * \brief A call wasn't answered
197 * \param cdr the cdr you wish to associate with the call
198 * Marks the channel disposition as "NO ANSWER"
200 extern void ast_cdr_noanswer(struct ast_cdr *cdr);
204 * \param cdr the cdr you wish to associate with the call
207 void ast_cdr_busy(struct ast_cdr *cdr);
211 * \param cdr the cdr you wish to associate with the call
214 void ast_cdr_failed(struct ast_cdr *cdr);
217 * \brief Save the result of the call based on the AST_CAUSE_*
218 * \param cdr the cdr you wish to associate with the call
219 * \param cause the AST_CAUSE_*
222 int ast_cdr_disposition(struct ast_cdr *cdr, int cause);
226 * \param cdr the cdr you have associated the call with
227 * Registers the end of call time in the cdr structure.
230 void ast_cdr_end(struct ast_cdr *cdr);
233 * \brief Detaches the detail record for posting (and freeing) either now or at a
234 * later time in bulk with other records during batch mode operation.
235 * \param cdr Which CDR to detach from the channel thread
236 * Prevents the channel thread from blocking on the CDR handling
239 void ast_cdr_detach(struct ast_cdr *cdr);
242 * \brief Spawns (possibly) a new thread to submit a batch of CDRs to the backend engines
243 * \param shutdown Whether or not we are shutting down
244 * Blocks the asterisk shutdown procedures until the CDR data is submitted.
247 void ast_cdr_submit_batch(int shutdown);
250 * \brief Set the destination channel, if there was one
251 * \param cdr Which cdr it's applied to
252 * \param chan Channel to which dest will be
253 * Sets the destination channel the CDR is applied to
256 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chan);
259 * \brief Set the last executed application
260 * \param cdr which cdr to act upon
261 * \param app the name of the app you wish to change it to
262 * \param data the data you want in the data field of app you set it to
263 * Changes the value of the last executed app
266 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data);
269 * \brief Convert a string to a detail record AMA flag
270 * \param flag string form of flag
271 * Converts the string form of the flag to the binary form.
272 * \return the binary form of the flag
274 int ast_cdr_amaflags2int(const char *flag);
277 * \brief Disposition to a string
278 * \param disposition input binary form
279 * Converts the binary form of a disposition to string form.
280 * \return a pointer to the string form
282 char *ast_cdr_disp2str(int disposition);
285 * \brief Reset the detail record, optionally posting it first
286 * \param cdr which cdr to act upon
287 * \param flags |AST_CDR_FLAG_POSTED whether or not to post the cdr first before resetting it
288 * |AST_CDR_FLAG_LOCKED whether or not to reset locked CDR's
290 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *flags);
293 * \brief Flags to a string
294 * \param flags binary flag
295 * Converts binary flags to string flags
296 * Returns string with flag name
298 char *ast_cdr_flags2str(int flags);
301 * \brief Move the non-null data from the "from" cdr to the "to" cdr
302 * \param to the cdr to get the goodies
303 * \param from the cdr to give the goodies
305 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from);
307 /*! \brief Set account code, will generate AMI event */
308 int ast_cdr_setaccount(struct ast_channel *chan, const char *account);
310 /*! \brief Set AMA flags for channel */
311 int ast_cdr_setamaflags(struct ast_channel *chan, const char *amaflags);
313 /*! \brief Set CDR user field for channel (stored in CDR) */
314 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield);
315 /*! \brief Append to CDR user field for channel (stored in CDR) */
316 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield);
319 /*! Update CDR on a channel */
320 int ast_cdr_update(struct ast_channel *chan);
323 extern int ast_default_amaflags;
325 extern char ast_default_accountcode[AST_MAX_ACCOUNT_CODE];
327 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr);
329 /*! \brief Reload the configuration file cdr.conf and start/stop CDR scheduling thread */
330 int ast_cdr_engine_reload(void);
332 /*! \brief Load the configuration file cdr.conf and possibly start the CDR scheduling thread */
333 int ast_cdr_engine_init(void);
335 /*! Submit any remaining CDRs and prepare for shutdown */
336 void ast_cdr_engine_term(void);
338 #endif /* _ASTERISK_CDR_H */