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