Version 0.3.0 from FTP
[asterisk/asterisk.git] / cdr.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Call Detail Record API 
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
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 #include <asterisk/lock.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/cdr.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/callerid.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <pthread.h>
25
26 int ast_default_amaflags = AST_CDR_DOCUMENTATION;
27 char ast_default_accountcode[20] = "";
28
29 static pthread_mutex_t cdrlock = AST_MUTEX_INITIALIZER;
30
31 static struct ast_cdr_beitem {
32         char name[20];
33         char desc[80];
34         ast_cdrbe be;
35         struct ast_cdr_beitem *next;
36 } *bes = NULL;
37
38 /*
39  * We do a lot of checking here in the CDR code to try to be sure we don't ever let a CDR slip
40  * through our fingers somehow.  If someone allocates a CDR, it must be completely handled normally
41  * or a WARNING shall be logged, so that we can best keep track of any escape condition where the CDR
42  * isn't properly generated and posted.
43  */
44
45 int ast_cdr_register(char *name, char *desc, ast_cdrbe be)
46 {
47         struct ast_cdr_beitem *i;
48         if (!name)
49                 return -1;
50         if (!be) {
51                 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
52                 return -1;
53         }
54         ast_pthread_mutex_lock(&cdrlock);
55         i = bes;
56         while(i) {
57                 if (!strcasecmp(name, i->name))
58                         break;
59                 i = i->next;
60         }
61         ast_pthread_mutex_unlock(&cdrlock);
62         if (i) {
63                 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
64                 return -1;
65         }
66         i = malloc(sizeof(struct ast_cdr_beitem));
67         if (!i)         
68                 return -1;
69         memset(i, 0, sizeof(struct ast_cdr_beitem));
70         strncpy(i->name, name, sizeof(i->name) - 1);
71         strncpy(i->desc, desc, sizeof(i->desc) - 1);
72         i->be = be;
73         ast_pthread_mutex_lock(&cdrlock);
74         i->next = bes;
75         bes = i;
76         ast_pthread_mutex_unlock(&cdrlock);
77         return 0;
78 }
79
80 void ast_cdr_unregister(char *name)
81 {
82         struct ast_cdr_beitem *i, *prev = NULL;
83         ast_pthread_mutex_lock(&cdrlock);
84         i = bes;
85         while(i) {
86                 if (!strcasecmp(name, i->name)) {
87                         if (prev)
88                                 prev->next = i->next;
89                         else
90                                 bes = i->next;
91                         break;
92                 }
93                 i = i->next;
94         }
95         ast_pthread_mutex_unlock(&cdrlock);
96         if (i) 
97                 free(i);
98 }
99
100 void ast_cdr_free(struct ast_cdr *cdr)
101 {
102         char *chan; 
103         if (cdr) {
104                 chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
105                 if (!cdr->posted)
106                         ast_log(LOG_WARNING, "CDR on channel '%s' not posted\n", chan);
107                 if (!cdr->end.tv_sec && !cdr->end.tv_usec)
108                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
109                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
110                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
111                 free(cdr);
112         }
113 }
114
115 struct ast_cdr *ast_cdr_alloc(void)
116 {
117         struct ast_cdr *cdr;
118         cdr = malloc(sizeof(struct ast_cdr));
119         if (cdr) {
120                 memset(cdr, 0, sizeof(struct ast_cdr));
121         }
122         return cdr;
123 }
124
125 void ast_cdr_start(struct ast_cdr *cdr)
126 {
127         char *chan; 
128         if (cdr) {
129                 chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
130                 if (cdr->posted)
131                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
132                 if (cdr->start.tv_sec || cdr->start.tv_usec)
133                         ast_log(LOG_WARNING, "CDR on channel '%s' already started\n", chan);
134                 gettimeofday(&cdr->start, NULL);
135         }
136 }
137
138 void ast_cdr_answer(struct ast_cdr *cdr)
139 {
140         char *chan; 
141         if (cdr) {
142                 chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
143                 if (cdr->posted)
144                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
145                 if (cdr->disposition < AST_CDR_ANSWERED)
146                         cdr->disposition = AST_CDR_ANSWERED;
147                 if (!cdr->answer.tv_sec && !cdr->answer.tv_usec) {
148                         gettimeofday(&cdr->answer, NULL);
149                 }
150         }
151 }
152
153 void ast_cdr_busy(struct ast_cdr *cdr)
154 {
155         char *chan; 
156         if (cdr) {
157                 chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
158                 if (cdr->posted)
159                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
160                 if (cdr->disposition < AST_CDR_BUSY)
161                         cdr->disposition = AST_CDR_BUSY;
162         }
163 }
164
165 void ast_cdr_setdestchan(struct ast_cdr *cdr, char *chann)
166 {
167         char *chan; 
168         if (cdr) {
169                 chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
170                 if (cdr->posted)
171                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
172                 strncpy(cdr->dstchannel, chann, sizeof(cdr->dstchannel) - 1);
173         }
174 }
175
176 void ast_cdr_setapp(struct ast_cdr *cdr, char *app, char *data)
177 {
178         char *chan; 
179         if (cdr) {
180                 chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
181                 if (cdr->posted)
182                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
183                 if (!app)
184                         app = "";
185                 strncpy(cdr->lastapp, app, sizeof(cdr->lastapp) - 1);
186                 if (!data)
187                         data = "";
188                 strncpy(cdr->lastdata, data, sizeof(cdr->lastdata) - 1);
189         }
190 }
191
192 int ast_cdr_setcid(struct ast_cdr *cdr, struct ast_channel *c)
193 {
194         char tmp[AST_MAX_EXTENSION] = "";
195         char *num, *name;
196         if (cdr) {
197                 /* Grab source from ANI or normal Caller*ID */
198                 if (c->ani)
199                         strncpy(tmp, c->ani, sizeof(tmp) - 1);
200                 else if (c->callerid)
201                         strncpy(tmp, c->callerid, sizeof(tmp) - 1);
202                 if (c->callerid)
203                         strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
204                 name = NULL;
205                 num = NULL;
206                 ast_callerid_parse(tmp, &name, &num);
207                 if (num) {
208                         ast_shrink_phone_number(num);
209                         strncpy(cdr->src, num, sizeof(cdr->src) - 1);
210                 }
211         }
212         return 0;
213 }
214
215 int ast_cdr_init(struct ast_cdr *cdr, struct ast_channel *c)
216 {
217         char *chan;
218         char *num, *name;
219         char tmp[AST_MAX_EXTENSION] = "";
220         if (cdr) {
221                 chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
222                 if (strlen(cdr->channel)) 
223                         ast_log(LOG_WARNING, "CDR already initialized on '%s'\n", chan); 
224                 strncpy(cdr->channel, c->name, sizeof(cdr->channel) - 1);
225                 /* Grab source from ANI or normal Caller*ID */
226                 if (c->ani)
227                         strncpy(tmp, c->ani, sizeof(tmp) - 1);
228                 else if (c->callerid)
229                         strncpy(tmp, c->callerid, sizeof(tmp) - 1);
230                 if (c->callerid)
231                         strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
232                 name = NULL;
233                 num = NULL;
234                 ast_callerid_parse(tmp, &name, &num);
235                 if (num) {
236                         ast_shrink_phone_number(num);
237                         strncpy(cdr->src, num, sizeof(cdr->src) - 1);
238                 }
239                 
240                 if (c->_state == AST_STATE_UP)
241                         cdr->disposition = AST_CDR_ANSWERED;
242                 else
243                         cdr->disposition = AST_CDR_NOANSWER;
244                 if (c->amaflags)
245                         cdr->amaflags = c->amaflags;
246                 else
247                         cdr->amaflags = ast_default_amaflags;
248                 strncpy(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode) - 1);
249                 /* Destination information */
250                 strncpy(cdr->dst, c->exten, sizeof(cdr->dst) - 1);
251                 strncpy(cdr->dcontext, c->context, sizeof(cdr->dcontext) - 1);
252         }
253         return 0;
254 }
255
256 void ast_cdr_end(struct ast_cdr *cdr)
257 {
258         char *chan;
259         if (cdr) {
260                 chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
261                 if (cdr->posted)
262                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
263                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
264                         ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", chan);
265                 if (!cdr->end.tv_sec && !cdr->end.tv_usec) 
266                         gettimeofday(&cdr->end, NULL);
267         }
268 }
269
270 char *ast_cdr_disp2str(int disposition)
271 {
272         switch (disposition) {
273         case AST_CDR_NOANSWER:
274                 return "NO ANSWER";
275         case AST_CDR_BUSY:
276                 return "BUSY";          
277         case AST_CDR_ANSWERED:
278                 return "ANSWERED";
279         default:
280                 return "UNKNOWN";
281         }
282 }
283
284 char *ast_cdr_flags2str(int flag)
285 {
286         switch(flag) {
287         case AST_CDR_OMIT:
288                 return "OMIT";
289         case AST_CDR_BILLING:
290                 return "BILLING";
291         case AST_CDR_DOCUMENTATION:
292                 return "DOCUMENTATION";
293         }
294         return "Unknown";
295 }
296
297 int ast_cdr_setaccount(struct ast_channel *chan, char *account)
298 {
299         struct ast_cdr *cdr = chan->cdr;
300
301         strncpy(chan->accountcode, account, sizeof(chan->accountcode) - 1);
302         if (cdr)
303                 strncpy(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode) - 1);
304         return 0;
305 }
306
307 int ast_cdr_update(struct ast_channel *c)
308 {
309         struct ast_cdr *cdr = c->cdr;
310         char *name, *num;
311         char tmp[AST_MAX_EXTENSION] = "";
312         /* Grab source from ANI or normal Caller*ID */
313         if (c->ani)
314                 strncpy(tmp, c->ani, sizeof(tmp) - 1);
315         else if (c->callerid)
316                 strncpy(tmp, c->callerid, sizeof(tmp) - 1);
317         if (c->callerid)
318                 strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
319         else
320                 strcpy(cdr->clid, "");
321         name = NULL;
322         num = NULL;
323         ast_callerid_parse(tmp, &name, &num);
324         if (num) {
325                 ast_shrink_phone_number(num);
326                 strncpy(cdr->src, num, sizeof(cdr->src) - 1);
327         }
328         /* Copy account code et-al */   
329         strncpy(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode) - 1);
330         /* Destination information */
331         strncpy(cdr->dst, c->exten, sizeof(cdr->dst) - 1);
332         strncpy(cdr->dcontext, c->context, sizeof(cdr->dcontext) - 1);
333         return 0;
334 }
335
336 int ast_cdr_amaflags2int(char *flag)
337 {
338         if (!strcasecmp(flag, "default"))
339                 return 0;
340         if (!strcasecmp(flag, "omit"))
341                 return AST_CDR_OMIT;
342         if (!strcasecmp(flag, "billing"))
343                 return AST_CDR_BILLING;
344         if (!strcasecmp(flag, "documentation"))
345                 return AST_CDR_DOCUMENTATION;
346         return -1;
347 }
348
349 void ast_cdr_post(struct ast_cdr *cdr)
350 {
351         char *chan;
352         struct ast_cdr_beitem *i;
353         if (cdr) {
354                 chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
355                 if (cdr->posted)
356                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
357                 if (!cdr->end.tv_sec && !cdr->end.tv_usec)
358                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
359                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
360                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
361                 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec + (cdr->end.tv_usec - cdr->start.tv_usec) / 1000000;
362                 if (cdr->answer.tv_sec || cdr->answer.tv_usec) {
363                         cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec + (cdr->end.tv_usec - cdr->answer.tv_usec) / 1000000;
364                 } else
365                         cdr->billsec = 0;
366                 cdr->posted = 1;
367                 ast_pthread_mutex_lock(&cdrlock);
368                 i = bes;
369                 while(i) {
370                         i->be(cdr);
371                         i = i->next;
372                 }
373                 ast_pthread_mutex_unlock(&cdrlock);
374         }
375 }
376
377 void ast_cdr_reset(struct ast_cdr *cdr, int post)
378 {
379         /* Post if requested */
380         if (post) {
381                 ast_cdr_end(cdr);
382                 ast_cdr_post(cdr);
383         }
384         /* Reset to initial state */
385         cdr->posted = 0;
386         memset(&cdr->start, 0, sizeof(cdr->start));
387         memset(&cdr->end, 0, sizeof(cdr->end));
388         memset(&cdr->answer, 0, sizeof(cdr->answer));
389         cdr->billsec = 0;
390         cdr->duration = 0;
391         ast_cdr_start(cdr);
392         cdr->disposition = AST_CDR_NOANSWER;
393 }