2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2003-2005, Digium, Inc.
6 * Brian K. West <brian@bkw.org>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief ODBC CDR Backend
23 * \author Brian K. West <brian@bkw.org>
26 * \arg http://www.unixodbc.org
27 * \arg \ref Config_cdr
28 * \ingroup cdr_drivers
32 <depend>unixodbc</depend>
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42 #include "asterisk/config.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/cdr.h"
45 #include "asterisk/module.h"
46 #include "asterisk/res_odbc.h"
48 #define DATE_FORMAT "%Y-%m-%d %T"
50 static char *name = "ODBC";
51 static char *config_file = "cdr_odbc.conf";
52 static char *dsn = NULL, *table = NULL;
55 CONFIG_LOGUNIQUEID = 1 << 0,
56 CONFIG_USEGMTIME = 1 << 1,
57 CONFIG_DISPOSITIONSTRING = 1 << 2,
60 static struct ast_flags config = { 0 };
62 static SQLHSTMT prepare_cb(struct odbc_obj *obj, void *data)
64 struct ast_cdr *cdr = data;
66 char sqlcmd[2048] = "", timestr[128];
70 ast_localtime(&cdr->start, &tm, ast_test_flag(&config, CONFIG_USEGMTIME) ? "GMT" : NULL);
71 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
73 if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) {
74 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
75 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,"
76 "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) "
77 "VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table, timestr);
79 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
80 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,"
81 "duration,billsec,disposition,amaflags,accountcode) "
82 "VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?)", table, timestr);
85 ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
87 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
88 ast_verb(11, "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res);
89 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
93 ODBC_res = SQLPrepare(stmt, (unsigned char *)sqlcmd, SQL_NTS);
95 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
96 ast_verb(11, "cdr_odbc: Error in PREPARE %d\n", ODBC_res);
97 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
101 SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->clid), 0, cdr->clid, 0, NULL);
102 SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->src), 0, cdr->src, 0, NULL);
103 SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dst), 0, cdr->dst, 0, NULL);
104 SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dcontext), 0, cdr->dcontext, 0, NULL);
105 SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->channel), 0, cdr->channel, 0, NULL);
106 SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dstchannel), 0, cdr->dstchannel, 0, NULL);
107 SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastapp), 0, cdr->lastapp, 0, NULL);
108 SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastdata), 0, cdr->lastdata, 0, NULL);
109 SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL);
110 SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL);
111 if (ast_test_flag(&config, CONFIG_DISPOSITIONSTRING))
112 SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(ast_cdr_disp2str(cdr->disposition)) + 1, 0, ast_cdr_disp2str(cdr->disposition), 0, NULL);
114 SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL);
115 SQLBindParameter(stmt, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL);
116 SQLBindParameter(stmt, 13, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->accountcode), 0, cdr->accountcode, 0, NULL);
118 if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) {
119 SQLBindParameter(stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->uniqueid), 0, cdr->uniqueid, 0, NULL);
120 SQLBindParameter(stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->userfield), 0, cdr->userfield, 0, NULL);
127 static int odbc_log(struct ast_cdr *cdr)
129 struct odbc_obj *obj = ast_odbc_request_obj(dsn, 0);
133 ast_log(LOG_ERROR, "Unable to retrieve database handle. CDR failed.\n");
137 stmt = ast_odbc_prepare_and_execute(obj, prepare_cb, cdr);
141 SQLRowCount(stmt, &rows);
142 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
145 ast_log(LOG_WARNING, "CDR successfully ran, but inserted 0 rows?\n");
147 ast_log(LOG_ERROR, "CDR prepare or execute failed\n");
148 ast_odbc_release_obj(obj);
152 static int odbc_load_module(int reload)
155 struct ast_config *cfg;
156 struct ast_variable *var;
158 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
161 cfg = ast_config_load(config_file, config_flags);
163 ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config_file);
164 res = AST_MODULE_LOAD_DECLINE;
166 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
169 var = ast_variable_browse(cfg, "global");
171 /* nothing configured */
175 if ((tmp = ast_variable_retrieve(cfg, "global", "dsn")) == NULL) {
176 ast_log(LOG_WARNING, "cdr_odbc: dsn not specified. Assuming asteriskdb\n");
181 dsn = ast_strdup(tmp);
187 if (((tmp = ast_variable_retrieve(cfg, "global", "dispositionstring"))) && ast_true(tmp))
188 ast_set_flag(&config, CONFIG_DISPOSITIONSTRING);
190 ast_clear_flag(&config, CONFIG_DISPOSITIONSTRING);
192 if (((tmp = ast_variable_retrieve(cfg, "global", "loguniqueid"))) && ast_true(tmp)) {
193 ast_set_flag(&config, CONFIG_LOGUNIQUEID);
194 ast_debug(1, "cdr_odbc: Logging uniqueid\n");
196 ast_clear_flag(&config, CONFIG_LOGUNIQUEID);
197 ast_debug(1, "cdr_odbc: Not logging uniqueid\n");
200 if (((tmp = ast_variable_retrieve(cfg, "global", "usegmtime"))) && ast_true(tmp)) {
201 ast_set_flag(&config, CONFIG_USEGMTIME);
202 ast_debug(1, "cdr_odbc: Logging in GMT\n");
204 ast_clear_flag(&config, CONFIG_USEGMTIME);
205 ast_debug(1, "cdr_odbc: Logging in local time\n");
208 if ((tmp = ast_variable_retrieve(cfg, "global", "table")) == NULL) {
209 ast_log(LOG_WARNING, "cdr_odbc: table not specified. Assuming cdr\n");
214 table = ast_strdup(tmp);
220 ast_verb(3, "cdr_odbc: dsn is %s\n", dsn);
221 ast_verb(3, "cdr_odbc: table is %s\n", table);
223 res = ast_cdr_register(name, ast_module_info->description, odbc_log);
225 ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
229 if (cfg && cfg != CONFIG_STATUS_FILEUNCHANGED)
230 ast_config_destroy(cfg);
234 static int load_module(void)
236 return odbc_load_module(0);
239 static int unload_module(void)
241 ast_cdr_unregister(name);
244 ast_verb(11, "cdr_odbc: free dsn\n");
248 ast_verb(11, "cdr_odbc: free table\n");
255 static int reload(void)
257 return odbc_load_module(1);
260 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ODBC CDR Backend",
262 .unload = unload_module,