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 /*! \file 00020 * \brief Call Detail Record API 00021 */ 00022 00023 #ifndef _ASTERISK_CDR_H 00024 #define _ASTERISK_CDR_H 00025 00026 #include <sys/time.h> 00027 00028 /*! \name CDR Flags */ 00029 /*@{ */ 00030 #define AST_CDR_FLAG_KEEP_VARS (1 << 0) 00031 #define AST_CDR_FLAG_POSTED (1 << 1) 00032 #define AST_CDR_FLAG_LOCKED (1 << 2) 00033 #define AST_CDR_FLAG_CHILD (1 << 3) 00034 #define AST_CDR_FLAG_POST_DISABLED (1 << 4) 00035 #define AST_CDR_FLAG_BRIDGED (1 << 5) 00036 #define AST_CDR_FLAG_MAIN (1 << 6) 00037 #define AST_CDR_FLAG_ENABLE (1 << 7) 00038 #define AST_CDR_FLAG_ANSLOCKED (1 << 8) 00039 #define AST_CDR_FLAG_DONT_TOUCH (1 << 9) 00040 #define AST_CDR_FLAG_POST_ENABLE (1 << 10) 00041 #define AST_CDR_FLAG_DIALED (1 << 11) 00042 /*@} */ 00043 00044 /*! \name CDR Flags - Disposition */ 00045 /*@{ */ 00046 #define AST_CDR_NULL 0 00047 #define AST_CDR_FAILED (1 << 0) 00048 #define AST_CDR_BUSY (1 << 1) 00049 #define AST_CDR_NOANSWER (1 << 2) 00050 #define AST_CDR_ANSWERED (1 << 3) 00051 /*@} */ 00052 00053 /*! \name CDR AMA Flags */ 00054 /*@{ */ 00055 #define AST_CDR_OMIT (1) 00056 #define AST_CDR_BILLING (2) 00057 #define AST_CDR_DOCUMENTATION (3) 00058 /*@} */ 00059 00060 #define AST_MAX_USER_FIELD 256 00061 #define AST_MAX_ACCOUNT_CODE 20 00062 00063 /* Include channel.h after relevant declarations it will need */ 00064 #include "asterisk/channel.h" 00065 #include "asterisk/utils.h" 00066 00067 /*! \brief Responsible for call detail data */ 00068 struct ast_cdr { 00069 /*! Caller*ID with text */ 00070 char clid[AST_MAX_EXTENSION]; 00071 /*! Caller*ID number */ 00072 char src[AST_MAX_EXTENSION]; 00073 /*! Destination extension */ 00074 char dst[AST_MAX_EXTENSION]; 00075 /*! Destination context */ 00076 char dcontext[AST_MAX_EXTENSION]; 00077 00078 char channel[AST_MAX_EXTENSION]; 00079 /*! Destination channel if appropriate */ 00080 char dstchannel[AST_MAX_EXTENSION]; 00081 /*! Last application if appropriate */ 00082 char lastapp[AST_MAX_EXTENSION]; 00083 /*! Last application data */ 00084 char lastdata[AST_MAX_EXTENSION]; 00085 00086 struct timeval start; 00087 00088 struct timeval answer; 00089 00090 struct timeval end; 00091 /*! Total time in system, in seconds */ 00092 long int duration; 00093 /*! Total time call is up, in seconds */ 00094 long int billsec; 00095 /*! What happened to the call */ 00096 long int disposition; 00097 /*! What flags to use */ 00098 long int amaflags; 00099 /*! What account number to use */ 00100 char accountcode[AST_MAX_ACCOUNT_CODE]; 00101 /*! flags */ 00102 unsigned int flags; 00103 /*! Unique Channel Identifier 00104 * 150 = 127 (max systemname) + "-" + 10 (epoch timestamp) + "." + 10 (monotonically incrementing integer) + NULL */ 00105 char uniqueid[150]; 00106 /*! User field */ 00107 char userfield[AST_MAX_USER_FIELD]; 00108 00109 /*! A linked list for variables */ 00110 struct varshead varshead; 00111 00112 struct ast_cdr *next; 00113 }; 00114 00115 int ast_cdr_isset_unanswered(void); 00116 void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw); 00117 int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, const char *value, int recur); 00118 int ast_cdr_serialize_variables(struct ast_cdr *cdr, struct ast_str **buf, char delim, char sep, int recur); 00119 void ast_cdr_free_vars(struct ast_cdr *cdr, int recur); 00120 int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr); 00121 00122 typedef int (*ast_cdrbe)(struct ast_cdr *cdr); 00123 00124 /*! \brief Return TRUE if CDR subsystem is enabled */ 00125 int check_cdr_enabled(void); 00126 00127 /*! 00128 * \brief Allocate a CDR record 00129 * \retval a malloc'd ast_cdr structure 00130 * \retval NULL on error (malloc failure) 00131 */ 00132 struct ast_cdr *ast_cdr_alloc(void); 00133 00134 /*! 00135 * \brief Duplicate a record 00136 * \retval a malloc'd ast_cdr structure, 00137 * \retval NULL on error (malloc failure) 00138 */ 00139 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr); 00140 00141 /*! 00142 * \brief Free a CDR record 00143 * \param cdr ast_cdr structure to free 00144 * Returns nothing 00145 */ 00146 void ast_cdr_free(struct ast_cdr *cdr); 00147 00148 /*! 00149 * \brief Discard and free a CDR record 00150 * \param cdr ast_cdr structure to free 00151 * Returns nothing -- same as free, but no checks or complaints 00152 */ 00153 void ast_cdr_discard(struct ast_cdr *cdr); 00154 00155 /*! 00156 * \brief Initialize based on a channel 00157 * \param cdr Call Detail Record to use for channel 00158 * \param chan Channel to bind CDR with 00159 * Initializes a CDR and associates it with a particular channel 00160 * \return 0 by default 00161 */ 00162 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *chan); 00163 00164 /*! 00165 * \brief Initialize based on a channel 00166 * \param cdr Call Detail Record to use for channel 00167 * \param chan Channel to bind CDR with 00168 * Initializes a CDR and associates it with a particular channel 00169 * \return 0 by default 00170 */ 00171 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *chan); 00172 00173 /*! 00174 * \brief Register a CDR handling engine 00175 * \param name name associated with the particular CDR handler 00176 * \param desc description of the CDR handler 00177 * \param be function pointer to a CDR handler 00178 * Used to register a Call Detail Record handler. 00179 * \retval 0 on success. 00180 * \retval -1 on error 00181 */ 00182 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be); 00183 00184 /*! 00185 * \brief Unregister a CDR handling engine 00186 * \param name name of CDR handler to unregister 00187 * Unregisters a CDR by it's name 00188 */ 00189 void ast_cdr_unregister(const char *name); 00190 00191 /*! 00192 * \brief Start a call 00193 * \param cdr the cdr you wish to associate with the call 00194 * Starts all CDR stuff necessary for monitoring a call 00195 * Returns nothing 00196 */ 00197 void ast_cdr_start(struct ast_cdr *cdr); 00198 00199 /*! \brief Answer a call 00200 * \param cdr the cdr you wish to associate with the call 00201 * Starts all CDR stuff necessary for doing CDR when answering a call 00202 * \note NULL argument is just fine. 00203 */ 00204 void ast_cdr_answer(struct ast_cdr *cdr); 00205 00206 /*! 00207 * \brief A call wasn't answered 00208 * \param cdr the cdr you wish to associate with the call 00209 * Marks the channel disposition as "NO ANSWER" 00210 * Will skip CDR's in chain with ANS_LOCK bit set. (see 00211 * forkCDR() application. 00212 */ 00213 extern void ast_cdr_noanswer(struct ast_cdr *cdr); 00214 00215 /*! 00216 * \brief Busy a call 00217 * \param cdr the cdr you wish to associate with the call 00218 * Marks the channel disposition as "BUSY" 00219 * Will skip CDR's in chain with ANS_LOCK bit set. (see 00220 * forkCDR() application. 00221 * Returns nothing 00222 */ 00223 void ast_cdr_busy(struct ast_cdr *cdr); 00224 00225 /*! 00226 * \brief Fail a call 00227 * \param cdr the cdr you wish to associate with the call 00228 * Marks the channel disposition as "FAILED" 00229 * Will skip CDR's in chain with ANS_LOCK bit set. (see 00230 * forkCDR() application. 00231 * Returns nothing 00232 */ 00233 void ast_cdr_failed(struct ast_cdr *cdr); 00234 00235 /*! 00236 * \brief Save the result of the call based on the AST_CAUSE_* 00237 * \param cdr the cdr you wish to associate with the call 00238 * \param cause the AST_CAUSE_* 00239 * Returns nothing 00240 */ 00241 int ast_cdr_disposition(struct ast_cdr *cdr, int cause); 00242 00243 /*! 00244 * \brief End a call 00245 * \param cdr the cdr you have associated the call with 00246 * Registers the end of call time in the cdr structure. 00247 * Returns nothing 00248 */ 00249 void ast_cdr_end(struct ast_cdr *cdr); 00250 00251 /*! 00252 * \brief Detaches the detail record for posting (and freeing) either now or at a 00253 * later time in bulk with other records during batch mode operation. 00254 * \param cdr Which CDR to detach from the channel thread 00255 * Prevents the channel thread from blocking on the CDR handling 00256 * Returns nothing 00257 */ 00258 void ast_cdr_detach(struct ast_cdr *cdr); 00259 00260 /*! 00261 * \brief Spawns (possibly) a new thread to submit a batch of CDRs to the backend engines 00262 * \param shutdown Whether or not we are shutting down 00263 * Blocks the asterisk shutdown procedures until the CDR data is submitted. 00264 * Returns nothing 00265 */ 00266 void ast_cdr_submit_batch(int shutdown); 00267 00268 /*! 00269 * \brief Set the destination channel, if there was one 00270 * \param cdr Which cdr it's applied to 00271 * \param chan Channel to which dest will be 00272 * Sets the destination channel the CDR is applied to 00273 * Returns nothing 00274 */ 00275 void ast_cdr_setdestchan(struct ast_cdr *cdr, const char *chan); 00276 00277 /*! 00278 * \brief Set the last executed application 00279 * \param cdr which cdr to act upon 00280 * \param app the name of the app you wish to change it to 00281 * \param data the data you want in the data field of app you set it to 00282 * Changes the value of the last executed app 00283 * Returns nothing 00284 */ 00285 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data); 00286 00287 /*! 00288 * \brief Convert a string to a detail record AMA flag 00289 * \param flag string form of flag 00290 * Converts the string form of the flag to the binary form. 00291 * \return the binary form of the flag 00292 */ 00293 int ast_cdr_amaflags2int(const char *flag); 00294 00295 /*! 00296 * \brief Disposition to a string 00297 * \param disposition input binary form 00298 * Converts the binary form of a disposition to string form. 00299 * \return a pointer to the string form 00300 */ 00301 char *ast_cdr_disp2str(int disposition); 00302 00303 /*! 00304 * \brief Reset the detail record, optionally posting it first 00305 * \param cdr which cdr to act upon 00306 * \param flags |AST_CDR_FLAG_POSTED whether or not to post the cdr first before resetting it 00307 * |AST_CDR_FLAG_LOCKED whether or not to reset locked CDR's 00308 */ 00309 void ast_cdr_reset(struct ast_cdr *cdr, struct ast_flags *flags); 00310 00311 /*! Reset the detail record times, flags */ 00312 /*! 00313 * \param cdr which cdr to act upon 00314 * \param flags |AST_CDR_FLAG_POSTED whether or not to post the cdr first before resetting it 00315 * |AST_CDR_FLAG_LOCKED whether or not to reset locked CDR's 00316 */ 00317 void ast_cdr_specialized_reset(struct ast_cdr *cdr, struct ast_flags *flags); 00318 00319 /*! Flags to a string */ 00320 /*! 00321 * \param flags binary flag 00322 * Converts binary flags to string flags 00323 * Returns string with flag name 00324 */ 00325 char *ast_cdr_flags2str(int flags); 00326 00327 /*! 00328 * \brief Move the non-null data from the "from" cdr to the "to" cdr 00329 * \param to the cdr to get the goodies 00330 * \param from the cdr to give the goodies 00331 */ 00332 void ast_cdr_merge(struct ast_cdr *to, struct ast_cdr *from); 00333 00334 /*! \brief Set account code, will generate AMI event */ 00335 int ast_cdr_setaccount(struct ast_channel *chan, const char *account); 00336 00337 /*! \brief Set AMA flags for channel */ 00338 int ast_cdr_setamaflags(struct ast_channel *chan, const char *amaflags); 00339 00340 /*! \brief Set CDR user field for channel (stored in CDR) */ 00341 int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield); 00342 /*! \brief Append to CDR user field for channel (stored in CDR) */ 00343 int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield); 00344 00345 00346 /*! Update CDR on a channel */ 00347 int ast_cdr_update(struct ast_channel *chan); 00348 00349 00350 extern int ast_default_amaflags; 00351 00352 extern char ast_default_accountcode[AST_MAX_ACCOUNT_CODE]; 00353 00354 struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr); 00355 00356 /*! \brief Reload the configuration file cdr.conf and start/stop CDR scheduling thread */ 00357 int ast_cdr_engine_reload(void); 00358 00359 /*! \brief Load the configuration file cdr.conf and possibly start the CDR scheduling thread */ 00360 int ast_cdr_engine_init(void); 00361 00362 /*! Submit any remaining CDRs and prepare for shutdown */ 00363 void ast_cdr_engine_term(void); 00364 00365 #endif /* _ASTERISK_CDR_H */