update MANY more files with proper copyright/license info (thanks Ian!)
[asterisk/asterisk.git] / cdr / cdr_sqlite.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2004 - 2005, Holger Schurig
5  *
6  * Holger Schurig <hs4233@mail.mn-solutions.de>
7  *
8  * Ideas taken from other cdr_*.c files
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*
22  *
23  * Store CDR records in a SQLite database.
24  * 
25  */
26
27 #include <sys/types.h>
28
29 #include <unistd.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <sqlite.h>
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/channel.h"
39 #include "asterisk/module.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/utils.h"
42
43 #define LOG_UNIQUEID    0
44 #define LOG_USERFIELD   0
45
46 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
47 #define DATE_FORMAT "%Y-%m-%d %T"
48
49 static char *desc = "SQLite CDR Backend";
50 static char *name = "sqlite";
51 static sqlite* db = NULL;
52
53 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
54
55 static char sql_create_table[] = "CREATE TABLE cdr ("
56 "       AcctId          INTEGER PRIMARY KEY,"
57 "       clid            VARCHAR(80),"
58 "       src             VARCHAR(80),"
59 "       dst             VARCHAR(80),"
60 "       dcontext        VARCHAR(80),"
61 "       channel         VARCHAR(80),"
62 "       dstchannel      VARCHAR(80),"
63 "       lastapp         VARCHAR(80),"
64 "       lastdata        VARCHAR(80),"
65 "       start           CHAR(19),"
66 "       answer          CHAR(19),"
67 "       end             CHAR(19),"
68 "       duration        INTEGER,"
69 "       billsec         INTEGER,"
70 "       disposition     INTEGER,"
71 "       amaflags        INTEGER,"
72 "       accountcode     VARCHAR(20)"
73 #if LOG_UNIQUEID
74 "       ,uniqueid       VARCHAR(32)"
75 #endif
76 #if LOG_USERFIELD
77 "       ,userfield      VARCHAR(255)"
78 #endif
79 ");";
80
81 static int sqlite_log(struct ast_cdr *cdr)
82 {
83         int res = 0;
84         char *zErr = 0;
85         struct tm tm;
86         time_t t;
87         char startstr[80], answerstr[80], endstr[80];
88         int count;
89
90         ast_mutex_lock(&sqlite_lock);
91
92         t = cdr->start.tv_sec;
93         localtime_r(&t, &tm);
94         strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
95
96         t = cdr->answer.tv_sec;
97         localtime_r(&t, &tm);
98         strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
99
100         t = cdr->end.tv_sec;
101         localtime_r(&t, &tm);
102         strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
103
104         for(count=0; count<5; count++) {
105                 res = sqlite_exec_printf(db,
106                         "INSERT INTO cdr ("
107                                 "clid,src,dst,dcontext,"
108                                 "channel,dstchannel,lastapp,lastdata, "
109                                 "start,answer,end,"
110                                 "duration,billsec,disposition,amaflags, "
111                                 "accountcode"
112 #                               if LOG_UNIQUEID
113                                 ",uniqueid"
114 #                               endif
115 #                               if LOG_USERFIELD
116                                 ",userfield"
117 #                               endif
118                         ") VALUES ("
119                                 "'%q', '%q', '%q', '%q', "
120                                 "'%q', '%q', '%q', '%q', "
121                                 "'%q', '%q', '%q', "
122                                 "%d, %d, %d, %d, "
123                                 "'%q'"
124 #                               if LOG_UNIQUEID
125                                 ",'%q'"
126 #                               endif
127 #                               if LOG_USERFIELD
128                                 ",'%q'"
129 #                               endif
130                         ")", NULL, NULL, &zErr,
131                                 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
132                                 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
133                                 startstr, answerstr, endstr,
134                                 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
135                                 cdr->accountcode
136 #                               if LOG_UNIQUEID
137                                 ,cdr->uniqueid
138 #                               endif
139 #                               if LOG_USERFIELD
140                                 ,cdr->userfield
141 #                               endif
142                         );
143                 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
144                         break;
145                 usleep(200);
146         }
147         
148         if (zErr) {
149                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
150                 free(zErr);
151         }
152
153         ast_mutex_unlock(&sqlite_lock);
154         return res;
155 }
156
157
158 char *description(void)
159 {
160         return desc;
161 }
162
163 int unload_module(void)
164 {
165         if (db)
166                 sqlite_close(db);
167         ast_cdr_unregister(name);
168         return 0;
169 }
170
171 int load_module(void)
172 {
173         char *zErr;
174         char fn[PATH_MAX];
175         int res;
176
177         /* is the database there? */
178         snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
179         db = sqlite_open(fn, 0660, &zErr);
180         if (!db) {
181                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
182                 free(zErr);
183                 return -1;
184         }
185
186         /* is the table there? */
187         res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
188         if (res) {
189                 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
190                 if (res) {
191                         ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
192                         free(zErr);
193                         goto err;
194                 }
195
196                 /* TODO: here we should probably create an index */
197         }
198         
199         res = ast_cdr_register(name, desc, sqlite_log);
200         if (res) {
201                 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
202                 return -1;
203         }
204         return 0;
205
206 err:
207         if (db)
208                 sqlite_close(db);
209         return -1;
210 }
211
212 int reload(void)
213 {
214         return 0;
215 }
216
217 int usecount(void)
218 {
219         return 0;
220 }
221
222 char *key()
223 {
224         return ASTERISK_GPL_KEY;
225 }