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