Thu Apr 28 2011 17:13:29

Asterisk developer's documentation


cdr.h

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