a quick fix to app_sms.c to get rid of cursed compiler warnings so I can compile...
[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 /*** MODULEINFO
35         <depend>sqlite</depend>
36  ***/
37
38 #include "asterisk.h"
39
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <sqlite.h>
48
49 #include "asterisk/channel.h"
50 #include "asterisk/module.h"
51 #include "asterisk/logger.h"
52 #include "asterisk/utils.h"
53
54 #define LOG_UNIQUEID    0
55 #define LOG_USERFIELD   0
56
57 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
58 #define DATE_FORMAT "%Y-%m-%d %T"
59
60 static char *name = "sqlite";
61 static sqlite* db = NULL;
62
63 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
64
65 /*! \brief SQL table format */
66 static char sql_create_table[] = "CREATE TABLE cdr ("
67 "       AcctId          INTEGER PRIMARY KEY,"
68 "       clid            VARCHAR(80),"
69 "       src             VARCHAR(80),"
70 "       dst             VARCHAR(80),"
71 "       dcontext        VARCHAR(80),"
72 "       channel         VARCHAR(80),"
73 "       dstchannel      VARCHAR(80),"
74 "       lastapp         VARCHAR(80),"
75 "       lastdata        VARCHAR(80),"
76 "       start           CHAR(19),"
77 "       answer          CHAR(19),"
78 "       end             CHAR(19),"
79 "       duration        INTEGER,"
80 "       billsec         INTEGER,"
81 "       disposition     INTEGER,"
82 "       amaflags        INTEGER,"
83 "       accountcode     VARCHAR(20)"
84 #if LOG_UNIQUEID
85 "       ,uniqueid       VARCHAR(32)"
86 #endif
87 #if LOG_USERFIELD
88 "       ,userfield      VARCHAR(255)"
89 #endif
90 ");";
91
92 static int sqlite_log(struct ast_cdr *cdr)
93 {
94         int res = 0;
95         char *zErr = 0;
96         struct tm tm;
97         time_t t;
98         char startstr[80], answerstr[80], endstr[80];
99         int count;
100
101         ast_mutex_lock(&sqlite_lock);
102
103         t = cdr->start.tv_sec;
104         localtime_r(&t, &tm);
105         strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
106
107         t = cdr->answer.tv_sec;
108         localtime_r(&t, &tm);
109         strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
110
111         t = cdr->end.tv_sec;
112         localtime_r(&t, &tm);
113         strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
114
115         for(count=0; count<5; count++) {
116                 res = sqlite_exec_printf(db,
117                         "INSERT INTO cdr ("
118                                 "clid,src,dst,dcontext,"
119                                 "channel,dstchannel,lastapp,lastdata, "
120                                 "start,answer,end,"
121                                 "duration,billsec,disposition,amaflags, "
122                                 "accountcode"
123 #                               if LOG_UNIQUEID
124                                 ",uniqueid"
125 #                               endif
126 #                               if LOG_USERFIELD
127                                 ",userfield"
128 #                               endif
129                         ") VALUES ("
130                                 "'%q', '%q', '%q', '%q', "
131                                 "'%q', '%q', '%q', '%q', "
132                                 "'%q', '%q', '%q', "
133                                 "%d, %d, %d, %d, "
134                                 "'%q'"
135 #                               if LOG_UNIQUEID
136                                 ",'%q'"
137 #                               endif
138 #                               if LOG_USERFIELD
139                                 ",'%q'"
140 #                               endif
141                         ")", NULL, NULL, &zErr,
142                                 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
143                                 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
144                                 startstr, answerstr, endstr,
145                                 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
146                                 cdr->accountcode
147 #                               if LOG_UNIQUEID
148                                 ,cdr->uniqueid
149 #                               endif
150 #                               if LOG_USERFIELD
151                                 ,cdr->userfield
152 #                               endif
153                         );
154                 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
155                         break;
156                 usleep(200);
157         }
158         
159         if (zErr) {
160                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
161                 free(zErr);
162         }
163
164         ast_mutex_unlock(&sqlite_lock);
165         return res;
166 }
167
168 static int unload_module(void)
169 {
170         if (db)
171                 sqlite_close(db);
172         ast_cdr_unregister(name);
173         return 0;
174 }
175
176 static int load_module(void)
177 {
178         char *zErr;
179         char fn[PATH_MAX];
180         int res;
181
182         /* is the database there? */
183         snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
184         db = sqlite_open(fn, AST_FILE_MODE, &zErr);
185         if (!db) {
186                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
187                 free(zErr);
188                 return -1;
189         }
190
191         /* is the table there? */
192         res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
193         if (res) {
194                 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
195                 if (res) {
196                         ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
197                         free(zErr);
198                         goto err;
199                 }
200
201                 /* TODO: here we should probably create an index */
202         }
203         
204         res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
205         if (res) {
206                 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
207                 return -1;
208         }
209         return 0;
210
211 err:
212         if (db)
213                 sqlite_close(db);
214         return -1;
215 }
216
217 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SQLite CDR Backend");