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)
33 /*! \name CDR Flags */
35 #define AST_CDR_NULL 0
36 #define AST_CDR_FAILED (1 << 0)
37 #define AST_CDR_BUSY (1 << 1)
38 #define AST_CDR_NOANSWER (1 << 2)
39 #define AST_CDR_ANSWERED (1 << 3)
42 /*! \name CDR AMA Flags */
44 #define AST_CDR_OMIT (1)
45 #define AST_CDR_BILLING (2)
46 #define AST_CDR_DOCUMENTATION (3)
49 #define AST_MAX_USER_FIELD 256
50 #define AST_MAX_ACCOUNT_CODE 20
52 /* Include channel.h after relevant declarations it will need */
53 #include "asterisk/channel.h"
54 #include "asterisk/utils.h"
56 /*! \brief Responsible for call detail data */
58 /*! Caller*ID with text */
59 char clid[AST_MAX_EXTENSION];
60 /*! Caller*ID number */
61 char src[AST_MAX_EXTENSION];
62 /*! Destination extension */
63 char dst[AST_MAX_EXTENSION];
64 /*! Destination context */
65 char dcontext[AST_MAX_EXTENSION];
67 char channel[AST_MAX_EXTENSION];
68 /*! Destination channel if appropriate */
69 char dstchannel[AST_MAX_EXTENSION];
70 /*! Last application if appropriate */
71 char lastapp[AST_MAX_EXTENSION];
72 /*! Last application data */
73 char lastdata[AST_MAX_EXTENSION];
77 struct timeval answer;
80 /*! Total time in system, in seconds */
82 /*! Total time call is up, in seconds */
84 /*! What happened to the call */
86 /*! What flags to use */
88 /*! What account number to use */
89 char accountcode[AST_MAX_ACCOUNT_CODE];
92 /*! Unique Channel Identifier */
95 char userfield[AST_MAX_USER_FIELD];
97 /*! A linked list for variables */
98 struct varshead varshead;
100 struct ast_cdr *next;
103 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw);
104 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur);
105 int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char delim, char sep, int recur);
106 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur);
107 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr);
108 int ast_cdr_log_unanswered(void);
110 typedef int (*ast_cdrbe)(struct ast_cdr *cdr);
112 /*! \brief Return TRUE if CDR subsystem is enabled */
113 int check_cdr_enabled(void);
116 * \brief Allocate a CDR record
117 * \retval a malloc'd ast_cdr structure
118 * \retval NULL on error (malloc failure)
120 struct ast_cdr *ast_cdr_alloc(void);
123 * \brief Duplicate a record
124 * \retval a malloc'd ast_cdr structure,
125 * \retval NULL on error (malloc failure)
127 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr);
130 * \brief Free a CDR record
131 * \param cdr ast_cdr structure to free
134 void ast_cdr_free(struct ast_cdr *cdr);
137 * \brief Discard and free a CDR record
138 * \param cdr ast_cdr structure to free
139 * Returns nothing -- same as free, but no checks or complaints
141 void ast_cdr_discard(struct ast_cdr *cdr);
144 * \brief Initialize based on a channel
145 * \param cdr Call Detail Record to use for channel
146 * \param chan Channel to bind CDR with
147 * Initializes a CDR and associates it with a particular channel
148 * \return 0 by default
150 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *chan);
153 * \brief Initialize based on a channel
154 * \param cdr Call Detail Record to use for channel
155 * \param chan Channel to bind CDR with
156 * Initializes a CDR and associates it with a particular channel
157 * \return 0 by default
159 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *chan);
162 * \brief Register a CDR handling engine
163 * \param name name associated with the particular CDR handler
164 * \param desc description of the CDR handler
165 * \param be function pointer to a CDR handler
166 * Used to register a Call Detail Record handler.
167 * \retval 0 on success.
168 * \retval -1 on error
170 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be);
173 * \brief Unregister a CDR handling engine
174 * \param name name of CDR handler to unregister
175 * Unregisters a CDR by it's name
177 void ast_cdr_unregister(const char *name);
180 * \brief Start a call
181 * \param cdr the cdr you wish to associate with the call
182 * Starts all CDR stuff necessary for monitoring a call
185 void ast_cdr_start(struct ast_cdr *cdr);
187 /*! \brief Answer a call
188 * \param cdr the cdr you wish to associate with the call
189 * Starts all CDR stuff necessary for doing CDR when answering a call
190 * \note NULL argument is just fine.
192 void ast_cdr_answer(struct ast_cdr *cdr);
195 * \brief A call wasn't answered
196 * \param cdr the cdr you wish to associate with the call
197 * Marks the channel disposition as "NO ANSWER"
199 extern void ast_cdr_noanswer(struct ast_cdr *cdr);
203 * \param cdr the cdr you wish to associate with the call
206 void ast_cdr_busy(struct ast_cdr *cdr);
210 * \param cdr the cdr you wish to associate with the call
213 void ast_cdr_failed(struct ast_cdr *cdr);
216 * \brief Save the result of the call based on the AST_CAUSE_*
217 * \param cdr the cdr you wish to associate with the call
218 * \param cause the AST_CAUSE_*
221 int ast_cdr_disposition(struct ast_cdr *cdr, int cause);
225 * \param cdr the cdr you have associated the call with
226 * Registers the end of call time in the cdr structure.
229 void ast_cdr_end(struct ast_cdr *cdr);
232 * \brief Detaches the detail record for posting (and freeing) either now or at a
233 * later time in bulk with other records during batch mode operation.
234 * \param cdr Which CDR to detach from the channel thread
235 * Prevents the channel thread from blocking on the CDR handling
238 void ast_cdr_detach(struct ast_cdr *cdr);
241 * \brief Spawns (possibly) a new thread to submit a batch of CDRs to the backend engines
242 * \param shutdown Whether or not we are shutting down
243 * Blocks the asterisk shutdown procedures until the CDR data is submitted.
246 void ast_cdr_submit_batch(int shutdown);
249 * \brief Set the destination channel, if there was one
250 * \param cdr Which cdr it's applied to
251 * \param chan Channel to which dest will be
252 * Sets the destination channel the CDR is applied to
255 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chan);
258 * \brief Set the last executed application
259 * \param cdr which cdr to act upon
260 * \param app the name of the app you wish to change it to
261 * \param data the data you want in the data field of app you set it to
262 * Changes the value of the last executed app
265 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data);
268 * \brief Convert a string to a detail record AMA flag
269 * \param flag string form of flag
270 * Converts the string form of the flag to the binary form.
271 * \return the binary form of the flag
273 int ast_cdr_amaflags2int(const char *flag);
276 * \brief Disposition to a string
277 * \param disposition input binary form
278 * Converts the binary form of a disposition to string form.
279 * \return a pointer to the string form
281 char *ast_cdr_disp2str(int disposition);
284 * \brief Reset the detail record, optionally posting it first
285 * \param cdr which cdr to act upon
286 * \param flags |AST_CDR_FLAG_POSTED whether or not to post the cdr first before resetting it
287 * |AST_CDR_FLAG_LOCKED whether or not to reset locked CDR's
289 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *flags);
292 * \brief Flags to a string
293 * \param flags binary flag
294 * Converts binary flags to string flags
295 * Returns string with flag name
297 char *ast_cdr_flags2str(int flags);
300 * \brief Move the non-null data from the "from" cdr to the "to" cdr
301 * \param to the cdr to get the goodies
302 * \param from the cdr to give the goodies
304 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from);
306 /*! \brief Set account code, will generate AMI event */
307 int ast_cdr_setaccount(struct ast_channel *chan, const char *account);
309 /*! \brief Set AMA flags for channel */
310 int ast_cdr_setamaflags(struct ast_channel *chan, const char *amaflags);
312 /*! \brief Set CDR user field for channel (stored in CDR) */
313 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield);
314 /*! \brief Append to CDR user field for channel (stored in CDR) */
315 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield);
318 /*! Update CDR on a channel */
319 int ast_cdr_update(struct ast_channel *chan);
322 extern int ast_default_amaflags;
324 extern char ast_default_accountcode[AST_MAX_ACCOUNT_CODE];
326 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr);
328 /*! \brief Reload the configuration file cdr.conf and start/stop CDR scheduling thread */
329 int ast_cdr_engine_reload(void);
331 /*! \brief Load the configuration file cdr.conf and possibly start the CDR scheduling thread */
332 int ast_cdr_engine_init(void);
334 /*! Submit any remaining CDRs and prepare for shutdown */
335 void ast_cdr_engine_term(void);
337 #endif /* _ASTERISK_CDR_H */