Version 0.2.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_init(struct ast_cdr *cdr, struct ast_channel *c)
193 {
194         char *chan;
195         char *num, *name;
196         char tmp[AST_MAX_EXTENSION] = "";
197         if (cdr) {
198                 chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
199                 if (strlen(cdr->channel)) 
200                         ast_log(LOG_WARNING, "CDR already initialized on '%s'\n", chan); 
201                 strncpy(cdr->channel, c->name, sizeof(cdr->channel) - 1);
202                 /* Grab source from ANI or normal Caller*ID */
203                 if (c->ani)
204                         strncpy(tmp, c->ani, sizeof(tmp) - 1);
205                 else if (c->callerid)
206                         strncpy(tmp, c->callerid, sizeof(tmp) - 1);
207                 if (c->callerid)
208                         strncpy(cdr->clid, c->callerid, sizeof(cdr->clid) - 1);
209                 name = NULL;
210                 num = NULL;
211                 ast_callerid_parse(tmp, &name, &num);
212                 if (num) {
213                         ast_shrink_phone_number(num);
214                         strncpy(cdr->src, num, sizeof(cdr->src) - 1);
215                 }
216                 
217                 if (c->_state == AST_STATE_UP)
218                         cdr->disposition = AST_CDR_ANSWERED;
219                 else
220                         cdr->disposition = AST_CDR_NOANSWER;
221                 if (c->amaflags)
222                         cdr->amaflags = c->amaflags;
223                 else
224                         cdr->amaflags = ast_default_amaflags;
225                 strncpy(cdr->accountcode, c->accountcode, sizeof(cdr->accountcode) - 1);
226                 /* Destination information */
227                 strncpy(cdr->dst, c->exten, sizeof(cdr->dst) - 1);
228                 strncpy(cdr->dcontext, c->context, sizeof(cdr->dcontext) - 1);
229         }
230         return 0;
231 }
232
233 void ast_cdr_end(struct ast_cdr *cdr)
234 {
235         char *chan;
236         if (cdr) {
237                 chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
238                 if (cdr->posted)
239                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
240                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
241                         ast_log(LOG_WARNING, "CDR on channel '%s' has not started\n", chan);
242                 if (!cdr->end.tv_sec && !cdr->end.tv_usec) 
243                         gettimeofday(&cdr->end, NULL);
244         }
245 }
246
247 char *ast_cdr_disp2str(int disposition)
248 {
249         switch (disposition) {
250         case AST_CDR_NOANSWER:
251                 return "NO ANSWER";
252         case AST_CDR_BUSY:
253                 return "BUSY";          
254         case AST_CDR_ANSWERED:
255                 return "ANSWERED";
256         default:
257                 return "UNKNOWN";
258         }
259 }
260
261 char *ast_cdr_flags2str(int flag)
262 {
263         switch(flag) {
264         case AST_CDR_OMIT:
265                 return "OMIT";
266         case AST_CDR_BILLING:
267                 return "BILLING";
268         case AST_CDR_DOCUMENTATION:
269                 return "DOCUMENTATION";
270         }
271         return "Unknown";
272 }
273
274 int ast_cdr_setaccount(struct ast_channel *chan, char *account)
275 {
276         struct ast_cdr *cdr = chan->cdr;
277
278         strncpy(chan->accountcode, account, sizeof(chan->accountcode) - 1);
279         if (cdr)
280                 strncpy(cdr->accountcode, chan->accountcode, sizeof(cdr->accountcode) - 1);
281         return 0;
282 }
283
284 int ast_cdr_amaflags2int(char *flag)
285 {
286         if (!strcasecmp(flag, "default"))
287                 return 0;
288         if (!strcasecmp(flag, "omit"))
289                 return AST_CDR_OMIT;
290         if (!strcasecmp(flag, "billing"))
291                 return AST_CDR_BILLING;
292         if (!strcasecmp(flag, "documentation"))
293                 return AST_CDR_DOCUMENTATION;
294         return -1;
295 }
296
297 void ast_cdr_post(struct ast_cdr *cdr)
298 {
299         char *chan;
300         struct ast_cdr_beitem *i;
301         if (cdr) {
302                 chan = strlen(cdr->channel) ? cdr->channel : "<unknown>";
303                 if (cdr->posted)
304                         ast_log(LOG_WARNING, "CDR on channel '%s' already posted\n", chan);
305                 if (!cdr->end.tv_sec && !cdr->end.tv_usec)
306                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks end\n", chan);
307                 if (!cdr->start.tv_sec && !cdr->start.tv_usec)
308                         ast_log(LOG_WARNING, "CDR on channel '%s' lacks start\n", chan);
309                 cdr->duration = cdr->end.tv_sec - cdr->start.tv_sec + (cdr->end.tv_usec - cdr->start.tv_usec) / 1000000;
310                 if (cdr->answer.tv_sec || cdr->answer.tv_usec) {
311                         cdr->billsec = cdr->end.tv_sec - cdr->answer.tv_sec + (cdr->end.tv_usec - cdr->answer.tv_usec) / 1000000;
312                 } else
313                         cdr->billsec = 0;
314                 cdr->posted = 1;
315                 ast_pthread_mutex_lock(&cdrlock);
316                 i = bes;
317                 while(i) {
318                         i->be(cdr);
319                         i = i->next;
320                 }
321                 ast_pthread_mutex_unlock(&cdrlock);
322         }
323 }