00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 1999 - 2005, Digium, Inc. 00005 * 00006 * Mark Spencer <markster@digium.com> 00007 * 00008 * See http://www.asterisk.org for more information about 00009 * the Asterisk project. Please do not directly contact 00010 * any of the maintainers of this project for assistance; 00011 * the project provides a web site, mailing lists and IRC 00012 * channels for your use. 00013 * 00014 * This program is free software, distributed under the terms of 00015 * the GNU General Public License Version 2. See the LICENSE file 00016 * at the top of the source tree. 00017 */ 00018 00019 /*! 00020 * \file 00021 * \brief Call Detail Record API 00022 * 00023 * \author Mark Spencer <markster@digium.com> 00024 */ 00025 00026 #ifndef _ASTERISK_CDR_H 00027 #define _ASTERISK_CDR_H 00028 00029 #include <sys/time.h> 00030 00031 #include "asterisk/data.h" 00032 00033 /*! 00034 * \brief CDR Flags 00035 */ 00036 enum { 00037 AST_CDR_FLAG_KEEP_VARS = (1 << 0), 00038 AST_CDR_FLAG_POSTED = (1 << 1), 00039 AST_CDR_FLAG_LOCKED = (1 << 2), 00040 AST_CDR_FLAG_CHILD = (1 << 3), 00041 AST_CDR_FLAG_POST_DISABLED = (1 << 4), 00042 AST_CDR_FLAG_BRIDGED = (1 << 5), 00043 AST_CDR_FLAG_MAIN = (1 << 6), 00044 AST_CDR_FLAG_ENABLE = (1 << 7), 00045 AST_CDR_FLAG_ANSLOCKED = (1 << 8), 00046 AST_CDR_FLAG_DONT_TOUCH = (1 << 9), 00047 AST_CDR_FLAG_POST_ENABLE = (1 << 10), 00048 AST_CDR_FLAG_DIALED = (1 << 11), 00049 AST_CDR_FLAG_ORIGINATED = (1 << 12), 00050 }; 00051 00052 /*! 00053 * \brief CDR Flags - Disposition 00054 */ 00055 enum { 00056 AST_CDR_NOANSWER = 0, 00057 AST_CDR_NULL = (1 << 0), 00058 AST_CDR_FAILED = (1 << 1), 00059 AST_CDR_BUSY = (1 << 2), 00060 AST_CDR_ANSWERED = (1 << 3), 00061 }; 00062 00063 /*! 00064 * \brief CDR AMA Flags 00065 */ 00066 enum { 00067 AST_CDR_OMIT = 1, 00068 AST_CDR_BILLING = 2, 00069 AST_CDR_DOCUMENTATION = 3, 00070 }; 00071 00072 #define AST_MAX_USER_FIELD 256 00073 #define AST_MAX_ACCOUNT_CODE 20 00074 00075 /* Include channel.h after relevant declarations it will need */ 00076 #include "asterisk/channel.h" 00077 #include "asterisk/utils.h" 00078 00079 /*! 00080 * \brief Responsible for call detail data 00081 */ 00082 struct ast_cdr { 00083 /*! Caller*ID with text */ 00084 char clid[AST_MAX_EXTENSION]; 00085 /*! Caller*ID number */ 00086 char src[AST_MAX_EXTENSION]; 00087 /*! Destination extension */ 00088 char dst[AST_MAX_EXTENSION]; 00089 /*! Destination context */ 00090 char dcontext[AST_MAX_EXTENSION]; 00091 00092 char channel[AST_MAX_EXTENSION]; 00093 /*! Destination channel if appropriate */ 00094 char dstchannel[AST_MAX_EXTENSION]; 00095 /*! Last application if appropriate */ 00096 char lastapp[AST_MAX_EXTENSION]; 00097 /*! Last application data */ 00098 char lastdata[AST_MAX_EXTENSION]; 00099 00100 struct timeval start; 00101 00102 struct timeval answer; 00103 00104 struct timeval end; 00105 /*! Total time in system, in seconds */ 00106 long int duration; 00107 /*! Total time call is up, in seconds */ 00108 long int billsec; 00109 /*! What happened to the call */ 00110 long int disposition; 00111 /*! What flags to use */ 00112 long int amaflags; 00113 /*! What account number to use */ 00114 char accountcode[AST_MAX_ACCOUNT_CODE]; 00115 /*! Account number of the last person we talked to */ 00116 char peeraccount[AST_MAX_ACCOUNT_CODE]; 00117 /*! flags */ 00118 unsigned int flags; 00119 /*! Unique Channel Identifier 00120 * 150 = 127 (max systemname) + "-" + 10 (epoch timestamp) + "." + 10 (monotonically incrementing integer) + NULL */ 00121 char uniqueid[150]; 00122 /* Linked group Identifier */ 00123 char linkedid[32]; 00124 /*! User field */ 00125 char userfield[AST_MAX_USER_FIELD]; 00126 /*! Sequence field */ 00127 int sequence; 00128 00129 /*! A linked list for variables */ 00130 struct varshead varshead; 00131 00132 struct ast_cdr *next; 00133 }; 00134 00135 int ast_cdr_isset_unanswered(void); 00136 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw); 00137 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur); 00138 int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char delim, char sep, int recur); 00139 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur); 00140 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr); 00141 00142 /*! 00143 * \brief CDR backend callback 00144 * \warning CDR backends should NOT attempt to access the channel associated 00145 * with a CDR record. This channel is not guaranteed to exist when the CDR 00146 * backend is invoked. 00147 */ 00148 typedef int (*ast_cdrbe)(struct ast_cdr *cdr); 00149 00150 /*! \brief Return TRUE if CDR subsystem is enabled */ 00151 int check_cdr_enabled(void); 00152 00153 /*! 00154 * \brief Allocate a CDR record 00155 * \retval a malloc'd ast_cdr structure 00156 * \retval NULL on error (malloc failure) 00157 */ 00158 struct ast_cdr *ast_cdr_alloc(void); 00159 00160 /*! 00161 * \brief Duplicate a record and increment the sequence number. 00162 * \param cdr the record to duplicate 00163 * \retval a malloc'd ast_cdr structure, 00164 * \retval NULL on error (malloc failure) 00165 * \see ast_cdr_dup() 00166 * \see ast_cdr_dup_unique_swap() 00167 */ 00168 struct ast_cdr *ast_cdr_dup_unique(struct ast_cdr *cdr); 00169 00170 /*! 00171 * \brief Duplicate a record and increment the sequence number of the old 00172 * record. 00173 * \param cdr the record to duplicate 00174 * \retval a malloc'd ast_cdr structure, 00175 * \retval NULL on error (malloc failure) 00176 * \note This version increments the original CDR's sequence number rather than 00177 * the duplicate's sequence number. The effect is as if the original CDR's 00178 * sequence number was swapped with the duplicate's sequence number. 00179 * 00180 * \see ast_cdr_dup() 00181 * \see ast_cdr_dup_unique() 00182 */ 00183 struct ast_cdr *ast_cdr_dup_unique_swap(struct ast_cdr *cdr); 00184 00185 /*! 00186 * \brief Duplicate a record 00187 * \param cdr the record to duplicate 00188 * \retval a malloc'd ast_cdr structure, 00189 * \retval NULL on error (malloc failure) 00190 * \see ast_cdr_dup_unique() 00191 * \see ast_cdr_dup_unique_swap() 00192 */ 00193 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr); 00194 00195 /*! 00196 * \brief Free a CDR record 00197 * \param cdr ast_cdr structure to free 00198 * Returns nothing 00199 */ 00200 void ast_cdr_free(struct ast_cdr *cdr); 00201 00202 /*! 00203 * \brief Discard and free a CDR record 00204 * \param cdr ast_cdr structure to free 00205 * Returns nothing -- same as free, but no checks or complaints 00206 */ 00207 void ast_cdr_discard(struct ast_cdr *cdr); 00208 00209 /*! 00210 * \brief Initialize based on a channel 00211 * \param cdr Call Detail Record to use for channel 00212 * \param chan Channel to bind CDR with 00213 * Initializes a CDR and associates it with a particular channel 00214 * \note The channel should be locked before calling. 00215 * \return 0 by default 00216 */ 00217 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *chan); 00218 00219 /*! 00220 * \brief Initialize based on a channel 00221 * \param cdr Call Detail Record to use for channel 00222 * \param chan Channel to bind CDR with 00223 * Initializes a CDR and associates it with a particular channel 00224 * \note The channel should be locked before calling. 00225 * \return 0 by default 00226 */ 00227 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *chan); 00228 00229 /*! 00230 * \brief Register a CDR handling engine 00231 * \param name name associated with the particular CDR handler 00232 * \param desc description of the CDR handler 00233 * \param be function pointer to a CDR handler 00234 * Used to register a Call Detail Record handler. 00235 * \retval 0 on success. 00236 * \retval -1 on error 00237 */ 00238 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be); 00239 00240 /*! 00241 * \brief Unregister a CDR handling engine 00242 * \param name name of CDR handler to unregister 00243 * Unregisters a CDR by it's name 00244 */ 00245 void ast_cdr_unregister(const char *name); 00246 00247 /*! 00248 * \brief Start a call 00249 * \param cdr the cdr you wish to associate with the call 00250 * Starts all CDR stuff necessary for monitoring a call 00251 * Returns nothing 00252 */ 00253 void ast_cdr_start(struct ast_cdr *cdr); 00254 00255 /*! \brief Answer a call 00256 * \param cdr the cdr you wish to associate with the call 00257 * Starts all CDR stuff necessary for doing CDR when answering a call 00258 * \note NULL argument is just fine. 00259 */ 00260 void ast_cdr_answer(struct ast_cdr *cdr); 00261 00262 /*! 00263 * \brief A call wasn't answered 00264 * \param cdr the cdr you wish to associate with the call 00265 * Marks the channel disposition as "NO ANSWER" 00266 * Will skip CDR's in chain with ANS_LOCK bit set. (see 00267 * forkCDR() application. 00268 */ 00269 extern void ast_cdr_noanswer(struct ast_cdr *cdr); 00270 00271 /*! 00272 * \brief Busy a call 00273 * \param cdr the cdr you wish to associate with the call 00274 * Marks the channel disposition as "BUSY" 00275 * Will skip CDR's in chain with ANS_LOCK bit set. (see 00276 * forkCDR() application. 00277 * Returns nothing 00278 */ 00279 void ast_cdr_busy(struct ast_cdr *cdr); 00280 00281 /*! 00282 * \brief Fail a call 00283 * \param cdr the cdr you wish to associate with the call 00284 * Marks the channel disposition as "FAILED" 00285 * Will skip CDR's in chain with ANS_LOCK bit set. (see 00286 * forkCDR() application. 00287 * Returns nothing 00288 */ 00289 void ast_cdr_failed(struct ast_cdr *cdr); 00290 00291 /*! 00292 * \brief Save the result of the call based on the AST_CAUSE_* 00293 * \param cdr the cdr you wish to associate with the call 00294 * \param cause the AST_CAUSE_* 00295 * Returns nothing 00296 */ 00297 int ast_cdr_disposition(struct ast_cdr *cdr, int cause); 00298 00299 /*! 00300 * \brief End a call 00301 * \param cdr the cdr you have associated the call with 00302 * Registers the end of call time in the cdr structure. 00303 * Returns nothing 00304 */ 00305 void ast_cdr_end(struct ast_cdr *cdr); 00306 00307 /*! 00308 * \brief Detaches the detail record for posting (and freeing) either now or at a 00309 * later time in bulk with other records during batch mode operation. 00310 * \param cdr Which CDR to detach from the channel thread 00311 * Prevents the channel thread from blocking on the CDR handling 00312 * Returns nothing 00313 */ 00314 void ast_cdr_detach(struct ast_cdr *cdr); 00315 00316 /*! 00317 * \brief Spawns (possibly) a new thread to submit a batch of CDRs to the backend engines 00318 * \param shutdown Whether or not we are shutting down 00319 * Blocks the asterisk shutdown procedures until the CDR data is submitted. 00320 * Returns nothing 00321 */ 00322 void ast_cdr_submit_batch(int shutdown); 00323 00324 /*! 00325 * \brief Set the destination channel, if there was one 00326 * \param cdr Which cdr it's applied to 00327 * \param chan Channel to which dest will be 00328 * Sets the destination channel the CDR is applied to 00329 * Returns nothing 00330 */ 00331 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chan); 00332 00333 /*! 00334 * \brief Set the last executed application 00335 * \param cdr which cdr to act upon 00336 * \param app the name of the app you wish to change it to 00337 * \param data the data you want in the data field of app you set it to 00338 * Changes the value of the last executed app 00339 * Returns nothing 00340 */ 00341 void ast_cdr_setapp(struct ast_cdr *cdr, const char *app, const char *data); 00342 00343 /*! 00344 * \brief Set the answer time for a call 00345 * \param cdr the cdr you wish to associate with the call 00346 * \param t the answer time 00347 * Starts all CDR stuff necessary for doing CDR when answering a call 00348 * NULL argument is just fine. 00349 */ 00350 void ast_cdr_setanswer(struct ast_cdr *cdr, struct timeval t); 00351 00352 /*! 00353 * \brief Set the disposition for a call 00354 * \param cdr the cdr you wish to associate with the call 00355 * \param disposition the new disposition 00356 * Set the disposition on a call. 00357 * NULL argument is just fine. 00358 */ 00359 void ast_cdr_setdisposition(struct ast_cdr *cdr, long int disposition); 00360 00361 /*! 00362 * \brief Convert a string to a detail record AMA flag 00363 * \param flag string form of flag 00364 * Converts the string form of the flag to the binary form. 00365 * \return the binary form of the flag 00366 */ 00367 int ast_cdr_amaflags2int(const char *flag); 00368 00369 /*! 00370 * \brief Disposition to a string 00371 * \param disposition input binary form 00372 * Converts the binary form of a disposition to string form. 00373 * \return a pointer to the string form 00374 */ 00375 char *ast_cdr_disp2str(int disposition); 00376 00377 /*! 00378 * \brief Reset the detail record, optionally posting it first 00379 * \param cdr which cdr to act upon 00380 * \param flags |AST_CDR_FLAG_POSTED whether or not to post the cdr first before resetting it 00381 * |AST_CDR_FLAG_LOCKED whether or not to reset locked CDR's 00382 */ 00383 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *flags); 00384 00385 /*! Reset the detail record times, flags */ 00386 /*! 00387 * \param cdr which cdr to act upon 00388 * \param flags |AST_CDR_FLAG_POSTED whether or not to post the cdr first before resetting it 00389 * |AST_CDR_FLAG_LOCKED whether or not to reset locked CDR's 00390 */ 00391 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *flags); 00392 00393 /*! Flags to a string */ 00394 /*! 00395 * \param flags binary flag 00396 * Converts binary flags to string flags 00397 * Returns string with flag name 00398 */ 00399 char *ast_cdr_flags2str(int flags); 00400 00401 /*! 00402 * \brief Move the non-null data from the "from" cdr to the "to" cdr 00403 * \param to the cdr to get the goodies 00404 * \param from the cdr to give the goodies 00405 */ 00406 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from); 00407 00408 /*! 00409 * \brief Set account code, will generate AMI event 00410 * \note The channel should be locked before calling. 00411 */ 00412 int ast_cdr_setaccount(struct ast_channel *chan, const char *account); 00413 00414 /*! 00415 * \brief Set the peer account 00416 * \note The channel should be locked before calling. 00417 */ 00418 int ast_cdr_setpeeraccount(struct ast_channel *chan, const char *account); 00419 00420 /*! 00421 * \brief Set AMA flags for channel 00422 * \note The channel should be locked before calling. 00423 */ 00424 int ast_cdr_setamaflags(struct ast_channel *chan, const char *amaflags); 00425 00426 /*! 00427 * \brief Set CDR user field for channel (stored in CDR) 00428 * \note The channel should be locked before calling. 00429 */ 00430 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield); 00431 /*! 00432 * \brief Append to CDR user field for channel (stored in CDR) 00433 * \note The channel should be locked before calling. 00434 */ 00435 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield); 00436 00437 00438 /*! 00439 * \brief Update CDR on a channel 00440 * \note The channel should be locked before calling. 00441 */ 00442 int ast_cdr_update(struct ast_channel *chan); 00443 00444 00445 extern int ast_default_amaflags; 00446 00447 extern char ast_default_accountcode[AST_MAX_ACCOUNT_CODE]; 00448 00449 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr); 00450 00451 /*! \brief Reload the configuration file cdr.conf and start/stop CDR scheduling thread */ 00452 int ast_cdr_engine_reload(void); 00453 00454 /*! \brief Load the configuration file cdr.conf and possibly start the CDR scheduling thread */ 00455 int ast_cdr_engine_init(void); 00456 00457 /*! Submit any remaining CDRs and prepare for shutdown */ 00458 void ast_cdr_engine_term(void); 00459 00460 /*! 00461 * \brief 00462 * \param[in] tree Where to insert the cdr. 00463 * \param[in] cdr The cdr structure to insert in 'tree'. 00464 * \param[in] recur Go throw all the cdr levels. 00465 * \retval <0 on error. 00466 * \retval 0 on success. 00467 */ 00468 int ast_cdr_data_add_structure(struct ast_data *tree, struct ast_cdr *cdr, int recur); 00469 00470 #endif /* _ASTERISK_CDR_H */