Merge anthm's CDR updates (bug #3595)
[asterisk/asterisk.git] / include / asterisk / cdr.h
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Call Detail Record API 
5  * 
6  * Copyright (C) 1999 - 2005, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License.
12  *
13  * Includes code and algorithms from the Zapata library.
14  *
15  */
16
17 #ifndef _CDR_H
18 #define _CDR_H
19
20 #include <asterisk/channel.h>
21 #include <sys/time.h>
22 #define AST_CDR_FLAG_KEEP_VARS          (1 << 0)
23 #define AST_CDR_FLAG_POSTED                     (1 << 1)
24 #define AST_CDR_FLAG_LOCKED                     (1 << 2)
25 #define AST_CDR_FLAG_CHILD                      (1 << 3)
26 #define AST_CDR_FLAG_SETVAR                     (1 << 4)
27 #define AST_CDR_FLAG_RECUR                      (1 << 5)
28
29 #define AST_CDR_NOANSWER                        (1 << 0)
30 #define AST_CDR_BUSY                            (1 << 1)
31 #define AST_CDR_ANSWERED                        (1 << 2)
32 #define AST_CDR_FAILED                          (1 << 3)
33
34 /*! AMA Flags */
35 #define AST_CDR_OMIT                            (1)
36 #define AST_CDR_BILLING                         (2)
37 #define AST_CDR_DOCUMENTATION                   (3)
38
39 #define AST_MAX_USER_FIELD                      256
40
41 struct ast_channel;
42 AST_LIST_HEAD(varshead,ast_var_t);
43
44 /*! Responsible for call detail data */
45 struct ast_cdr {
46         /*! Caller*ID with text */
47         char clid[AST_MAX_EXTENSION];
48         /*! Caller*ID number */
49         char src[AST_MAX_EXTENSION];            
50         /*! Destination extension */
51         char dst[AST_MAX_EXTENSION];            
52         /*! Destination context */
53         char dcontext[AST_MAX_EXTENSION];       
54         
55         char channel[AST_MAX_EXTENSION];
56         /*! Destination channel if appropriate */
57         char dstchannel[AST_MAX_EXTENSION];     
58         /*! Last application if appropriate */
59         char lastapp[AST_MAX_EXTENSION];        
60         /*! Last application data */
61         char lastdata[AST_MAX_EXTENSION];       
62         
63         struct timeval start;
64         
65         struct timeval answer;
66         
67         struct timeval end;
68         /*! Total time in system, in seconds */
69         int duration;                           
70         /*! Total time call is up, in seconds */
71         int billsec;                            
72         /*! What happened to the call */
73         int disposition;                        
74         /*! What flags to use */
75         int amaflags;                           
76         /*! What account number to use */
77         char accountcode[20];                   
78         /*! flags */
79         unsigned int flags;                             
80         /* Unique Channel Identifier */
81         char uniqueid[32];
82         /* User field */
83         char userfield[AST_MAX_USER_FIELD];
84
85         /* A linked list for variables */
86         struct varshead varshead;
87
88         struct ast_cdr *next;
89 };
90
91 extern void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur);
92 extern int ast_cdr_setvar(struct ast_cdr *cdr, const char *name, char *value, int recur);
93 extern int ast_cdr_serialize_variables(struct ast_cdr *cdr, char *buf, size_t size, char delim, char sep, int recur);
94 extern void ast_cdr_free_vars(struct ast_cdr *cdr, int recur);
95 extern int ast_cdr_copy_vars(struct ast_cdr *to_cdr, struct ast_cdr *from_cdr);
96
97 typedef int (*ast_cdrbe)(struct ast_cdr *cdr);
98
99 /*! Allocate a record */
100 /*! 
101  * Returns a malloc'd ast_cdr structure, returns NULL on error (malloc failure)
102  */
103 extern struct ast_cdr *ast_cdr_alloc(void);
104
105 /*! Free a record */
106 /* \param cdr ast_cdr structure to free
107  * Returns nothing important
108  */
109 extern void ast_cdr_free(struct ast_cdr *cdr);
110
111 /*! Initialize based on a channel */
112 /*! 
113  * \param cdr Call Detail Record to use for channel
114  * \param chan Channel to bind CDR with
115  * Initializes a CDR and associates it with a particular channel
116  * Return is negligible.  (returns 0 by default)
117  */
118 extern int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *chan);
119
120 /*! Initialize based on a channel */
121 /*! 
122  * \param cdr Call Detail Record to use for channel
123  * \param chan Channel to bind CDR with
124  * Initializes a CDR and associates it with a particular channel
125  * Return is negligible.  (returns 0 by default)
126  */
127 extern int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *chan);
128
129 /*! Register a CDR handling engine */
130 /*!
131  * \param name name associated with the particular CDR handler
132  * \param desc description of the CDR handler
133  * \param be function pointer to a CDR handler
134  * Used to register a Call Detail Record handler.
135  * Returns -1 on error, 0 on success.
136  */
137 extern int ast_cdr_register(char *name, char *desc, ast_cdrbe be);
138
139 /*! Unregister a CDR handling engine */
140 /*!
141  * \param name name of CDR handler to unregister
142  * Unregisters a CDR by it's name
143  */
144 extern void ast_cdr_unregister(char *name);
145
146 /*! Start a call */
147 /*!
148  * \param cdr the cdr you wish to associate with the call
149  * Starts all CDR stuff necessary for monitoring a call
150  * Returns nothing important
151  */
152 extern void ast_cdr_start(struct ast_cdr *cdr);
153
154 /*! Answer a call */
155 /*!
156  * \param cdr the cdr you wish to associate with the call
157  * Starts all CDR stuff necessary for doing CDR when answering a call
158  */
159 extern void ast_cdr_answer(struct ast_cdr *cdr);
160
161 /*! Busy a call */
162 /*!
163  * \param cdr the cdr you wish to associate with the call
164  * Returns nothing important
165  */
166 extern void ast_cdr_busy(struct ast_cdr *cdr);
167
168 /*! Fail a call */
169 /*!
170  * \param cdr the cdr you wish to associate with the call
171  * Returns nothing important
172  */
173 extern void ast_cdr_failed(struct ast_cdr *cdr);
174
175 /*! Save the result of the call based on the AST_CAUSE_* */
176 /*!
177  * \param cdr the cdr you wish to associate with the call
178  * Returns nothing important
179  * \param cause the AST_CAUSE_*
180  */
181 extern int ast_cdr_disposition(struct ast_cdr *cdr, int cause);
182         
183 /*! End a call */
184 /*!
185  * \param cdr the cdr you have associated the call with
186  * Registers the end of call time in the cdr structure.
187  * Returns nothing important
188  */
189 extern void ast_cdr_end(struct ast_cdr *cdr);
190
191 /*! Post the detail record */
192 /*! 
193  * \param cdr Which cdr to post
194  * Actually outputs the CDR record to the CDR plugins installed
195  * Returns nothing
196  */
197 extern void ast_cdr_post(struct ast_cdr *cdr);
198
199 /*! Set the destination channel, if there was one */
200 /*!
201  * \param cdr Which cdr it's applied to
202  * Sets the destination channel the CDR is applied to
203  * Returns nothing
204  */
205 extern void ast_cdr_setdestchan(struct ast_cdr *cdr, char *chan);
206
207 /*! Set the last executed application */
208 /*!
209  * \param cdr which cdr to act upon
210  * \param app the name of the app you wish to change it to
211  * \param data the data you want in the data field of app you set it to
212  * Changes the value of the last executed app
213  * Returns nothing
214  */
215 extern void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data);
216
217 /*! Convert a string to a detail record AMA flag */
218 /*!
219  * \param flag string form of flag
220  * Converts the string form of the flag to the binary form.
221  * Returns the binary form of the flag
222  */
223 extern int ast_cdr_amaflags2int(const char *flag);
224
225 /*! Disposition to a string */
226 /*!
227  * \param flag input binary form
228  * Converts the binary form of a disposition to string form.
229  * Returns a pointer to the string form
230  */
231 extern char *ast_cdr_disp2str(int disposition);
232
233 /*! Reset the detail record, optionally posting it first */
234 /*!
235  * \param cdr which cdr to act upon
236  * \param flags |AST_CDR_FLAG_POSTED whether or not to post the cdr first before resetting it
237  *              |AST_CDR_FLAG_LOCKED whether or not to reset locked CDR's
238  */
239 extern void ast_cdr_reset(struct ast_cdr *cdr, int flags);
240
241 /*! Flags to a string */
242 /*!
243  * \param flags binary flag
244  * Converts binary flags to string flags
245  * Returns string with flag name
246  */
247 extern char *ast_cdr_flags2str(int flags);
248
249 extern int ast_cdr_setaccount(struct ast_channel *chan, const char *account);
250 extern int ast_cdr_setamaflags(struct ast_channel *chan, const char *amaflags);
251
252
253 extern int ast_cdr_setuserfield(struct ast_channel *chan, const char *userfield);
254 extern int ast_cdr_appenduserfield(struct ast_channel *chan, const char *userfield);
255
256
257 /* Update CDR on a channel */
258 extern int ast_cdr_update(struct ast_channel *chan);
259
260
261 extern int ast_default_amaflags;
262
263 extern char ast_default_accountcode[20];
264
265 extern struct ast_cdr *ast_cdr_append(struct ast_cdr *cdr, struct ast_cdr *newcdr);
266
267 #endif /* _CDR_H */