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