| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-01-15 09:01:52 +00:00
										 |  |  |  * Copyright (C) 2003-2005, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  |  * Brian K. West <brian@bkw.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 08:45:11 +00:00
										 |  |  | /*!
 | 
					
						
							|  |  |  |  * \file | 
					
						
							| 
									
										
										
										
											2005-10-26 13:03:17 +00:00
										 |  |  |  * \brief ODBC CDR Backend | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-26 13:03:17 +00:00
										 |  |  |  * \author Brian K. West <brian@bkw.org> | 
					
						
							| 
									
										
										
										
											2021-11-23 14:45:58 +01:00
										 |  |  |  * http://www.unixodbc.org
 | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \ingroup cdr_drivers | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-14 21:46:45 +00:00
										 |  |  | /*! \li \ref cdr_odbc.c uses the configuration file \ref cdr_odbc.conf
 | 
					
						
							|  |  |  |  * \addtogroup configuration_file Configuration Files | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*!
 | 
					
						
							|  |  |  |  * \page cdr_odbc.conf cdr_odbc.conf | 
					
						
							|  |  |  |  * \verbinclude cdr_odbc.conf.sample | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-24 17:11:45 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							| 
									
										
										
										
											2010-08-23 21:35:09 +00:00
										 |  |  | 	<depend>res_odbc</depend> | 
					
						
							| 
									
										
										
										
											2018-03-06 14:33:14 +01:00
										 |  |  | 	<depend>generic_odbc</depend> | 
					
						
							| 
									
										
										
										
											2011-07-14 20:28:54 +00:00
										 |  |  | 	<support_level>extended</support_level> | 
					
						
							| 
									
										
										
										
											2006-04-24 17:11:45 +00:00
										 |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-06 21:09:59 +00:00
										 |  |  | #include "asterisk/config.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/cdr.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | #include "asterisk/res_odbc.h"
 | 
					
						
							| 
									
										
										
										
											2005-06-06 21:09:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | #define DATE_FORMAT "%Y-%m-%d %T"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 08:45:11 +00:00
										 |  |  | static const char name[] = "ODBC"; | 
					
						
							|  |  |  | static const char config_file[] = "cdr_odbc.conf"; | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | static char *dsn = NULL, *table = NULL; | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | enum { | 
					
						
							|  |  |  | 	CONFIG_LOGUNIQUEID =       1 << 0, | 
					
						
							|  |  |  | 	CONFIG_USEGMTIME =         1 << 1, | 
					
						
							|  |  |  | 	CONFIG_DISPOSITIONSTRING = 1 << 2, | 
					
						
							| 
									
										
										
										
											2010-06-08 23:48:17 +00:00
										 |  |  | 	CONFIG_HRTIME =            1 << 3, | 
					
						
							| 
									
										
										
										
											2010-07-25 18:22:13 +00:00
										 |  |  | 	CONFIG_REGISTERED =        1 << 4, | 
					
						
							| 
									
										
										
										
											2015-04-15 20:55:33 -03:00
										 |  |  | 	CONFIG_NEWCDRCOLUMNS =     1 << 5, | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | static struct ast_flags config = { 0 }; | 
					
						
							| 
									
										
										
										
											2006-04-19 18:56:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-11 21:10:45 +00:00
										 |  |  | static SQLHSTMT execute_cb(struct odbc_obj *obj, void *data) | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	struct ast_cdr *cdr = data; | 
					
						
							|  |  |  | 	SQLRETURN ODBC_res; | 
					
						
							| 
									
										
										
										
											2015-04-15 20:55:33 -03:00
										 |  |  | 	char sqlcmd[2048] = "", timestr[128], new_columns[120] = "", new_values[7] = ""; | 
					
						
							| 
									
										
										
										
											2007-07-18 19:47:20 +00:00
										 |  |  | 	struct ast_tm tm; | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	SQLHSTMT stmt; | 
					
						
							| 
									
										
										
										
											2015-04-15 20:55:33 -03:00
										 |  |  | 	int i = 0; | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	ast_localtime(&cdr->start, &tm, ast_test_flag(&config, CONFIG_USEGMTIME) ? "GMT" : NULL); | 
					
						
							| 
									
										
										
										
											2007-07-18 19:47:20 +00:00
										 |  |  | 	ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm); | 
					
						
							| 
									
										
										
										
											2008-01-10 17:38:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-15 20:55:33 -03:00
										 |  |  | 	if (ast_test_flag(&config, CONFIG_NEWCDRCOLUMNS)) { | 
					
						
							|  |  |  | 		snprintf(new_columns, sizeof(new_columns), "%s", ",peeraccount,linkedid,sequence"); | 
					
						
							|  |  |  | 		snprintf(new_values, sizeof(new_values), "%s", ",?,?,?"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) { | 
					
						
							| 
									
										
										
										
											2004-12-10 11:59:43 +00:00
										 |  |  | 		snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s " | 
					
						
							| 
									
										
										
										
											2003-12-24 00:04:33 +00:00
										 |  |  | 		"(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp," | 
					
						
							| 
									
										
										
										
											2015-04-15 20:55:33 -03:00
										 |  |  | 		"lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield%s) " | 
					
						
							|  |  |  | 		"VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?,?,?%s)", table, new_columns, timestr, new_values); | 
					
						
							| 
									
										
										
										
											2004-10-05 01:05:30 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2004-12-10 11:59:43 +00:00
										 |  |  | 		snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s " | 
					
						
							| 
									
										
										
										
											2003-12-24 00:04:33 +00:00
										 |  |  | 		"(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata," | 
					
						
							| 
									
										
										
										
											2015-04-15 20:55:33 -03:00
										 |  |  | 		"duration,billsec,disposition,amaflags,accountcode%s) " | 
					
						
							|  |  |  | 		"VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?%s)", table, new_columns, timestr, new_values); | 
					
						
							| 
									
										
										
										
											2003-12-24 00:04:33 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt); | 
					
						
							| 
									
										
										
										
											2003-12-24 00:04:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-05 01:05:30 +00:00
										 |  |  | 	if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							| 
									
										
										
										
											2012-10-17 14:24:52 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res); | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 		SQLFreeHandle(SQL_HANDLE_STMT, stmt); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2003-12-07 17:12:39 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-10 17:38:36 +00:00
										 |  |  | 	SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->clid), 0, cdr->clid, 0, NULL); | 
					
						
							|  |  |  | 	SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->src), 0, cdr->src, 0, NULL); | 
					
						
							|  |  |  | 	SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dst), 0, cdr->dst, 0, NULL); | 
					
						
							|  |  |  | 	SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dcontext), 0, cdr->dcontext, 0, NULL); | 
					
						
							|  |  |  | 	SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->channel), 0, cdr->channel, 0, NULL); | 
					
						
							|  |  |  | 	SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dstchannel), 0, cdr->dstchannel, 0, NULL); | 
					
						
							|  |  |  | 	SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastapp), 0, cdr->lastapp, 0, NULL); | 
					
						
							|  |  |  | 	SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastdata), 0, cdr->lastdata, 0, NULL); | 
					
						
							| 
									
										
										
										
											2010-06-08 23:48:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_test_flag(&config, CONFIG_HRTIME)) { | 
					
						
							|  |  |  | 		double hrbillsec = 0.0; | 
					
						
							|  |  |  | 		double hrduration; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!ast_tvzero(cdr->answer)) { | 
					
						
							|  |  |  | 			hrbillsec = (double) ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		hrduration = (double) ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-27 04:27:19 +00:00
										 |  |  | 		SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_FLOAT, 0, 0, &hrduration, 0, NULL); | 
					
						
							|  |  |  | 		SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_FLOAT, 0, 0, &hrbillsec, 0, NULL); | 
					
						
							| 
									
										
										
										
											2010-06-08 23:48:17 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL); | 
					
						
							|  |  |  | 		SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 03:00:38 +00:00
										 |  |  | 	if (ast_test_flag(&config, CONFIG_DISPOSITIONSTRING)) { | 
					
						
							|  |  |  | 		char *disposition; | 
					
						
							|  |  |  | 		disposition = ast_strdupa(ast_cdr_disp2str(cdr->disposition)); | 
					
						
							|  |  |  | 		SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(disposition) + 1, 0, disposition, 0, NULL); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2008-01-10 17:38:36 +00:00
										 |  |  | 		SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL); | 
					
						
							| 
									
										
										
										
											2013-06-17 03:00:38 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-01-10 17:38:36 +00:00
										 |  |  | 	SQLBindParameter(stmt, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL); | 
					
						
							|  |  |  | 	SQLBindParameter(stmt, 13, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->accountcode), 0, cdr->accountcode, 0, NULL); | 
					
						
							| 
									
										
										
										
											2003-12-24 00:04:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-15 20:55:33 -03:00
										 |  |  | 	i = 14; | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) { | 
					
						
							| 
									
										
										
										
											2008-01-10 17:38:36 +00:00
										 |  |  | 		SQLBindParameter(stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->uniqueid), 0, cdr->uniqueid, 0, NULL); | 
					
						
							|  |  |  | 		SQLBindParameter(stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->userfield), 0, cdr->userfield, 0, NULL); | 
					
						
							| 
									
										
										
										
											2015-04-15 20:55:33 -03:00
										 |  |  | 		i = 16; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_test_flag(&config, CONFIG_NEWCDRCOLUMNS)) { | 
					
						
							|  |  |  | 		SQLBindParameter(stmt, i, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->peeraccount), 0, cdr->peeraccount, 0, NULL); | 
					
						
							|  |  |  | 		SQLBindParameter(stmt, i + 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->linkedid), 0, cdr->linkedid, 0, NULL); | 
					
						
							|  |  |  | 		SQLBindParameter(stmt, i + 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->sequence, 0, NULL); | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-06 12:16:01 +00:00
										 |  |  | 	ODBC_res = ast_odbc_execute_sql(obj, stmt, sqlcmd); | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-11 21:10:45 +00:00
										 |  |  | 	if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							| 
									
										
										
										
											2012-10-17 14:24:52 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "cdr_odbc: Error in ExecDirect: %d, query is: %s\n", ODBC_res, sqlcmd); | 
					
						
							| 
									
										
										
										
											2008-03-11 21:10:45 +00:00
										 |  |  | 		SQLFreeHandle(SQL_HANDLE_STMT, stmt); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	return stmt; | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int odbc_log(struct ast_cdr *cdr) | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	struct odbc_obj *obj = ast_odbc_request_obj(dsn, 0); | 
					
						
							|  |  |  | 	SQLHSTMT stmt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!obj) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to retrieve database handle.  CDR failed.\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2004-12-10 11:59:43 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-01-15 09:01:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-11 21:10:45 +00:00
										 |  |  | 	stmt = ast_odbc_direct_execute(obj, execute_cb, cdr); | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	if (stmt) { | 
					
						
							| 
									
										
										
										
											2007-11-02 03:09:02 +00:00
										 |  |  | 		SQLLEN rows = 0; | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		SQLRowCount(stmt, &rows); | 
					
						
							|  |  |  | 		SQLFreeHandle(SQL_HANDLE_STMT, stmt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (rows == 0) | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "CDR successfully ran, but inserted 0 rows?\n"); | 
					
						
							|  |  |  | 	} else | 
					
						
							| 
									
										
										
										
											2008-03-11 21:10:45 +00:00
										 |  |  | 		ast_log(LOG_ERROR, "CDR direct execute failed\n"); | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	ast_odbc_release_obj(obj); | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | static int odbc_load_module(int reload) | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-01-04 05:11:53 +00:00
										 |  |  | 	int res = 0; | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 	struct ast_config *cfg; | 
					
						
							|  |  |  | 	struct ast_variable *var; | 
					
						
							| 
									
										
										
										
											2006-09-20 20:40:39 +00:00
										 |  |  | 	const char *tmp; | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 	struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 	do { | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 		cfg = ast_config_load(config_file, config_flags); | 
					
						
							| 
									
										
										
										
											2008-11-19 19:25:14 +00:00
										 |  |  | 		if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 			ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config_file); | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 			res = AST_MODULE_LOAD_DECLINE; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2008-11-20 17:48:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 		var = ast_variable_browse(cfg, "global"); | 
					
						
							|  |  |  | 		if (!var) { | 
					
						
							|  |  |  | 			/* nothing configured */ | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 		if ((tmp = ast_variable_retrieve(cfg, "global", "dsn")) == NULL) { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "cdr_odbc: dsn not specified.  Assuming asteriskdb\n"); | 
					
						
							|  |  |  | 			tmp = "asteriskdb"; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (dsn) | 
					
						
							|  |  |  | 			ast_free(dsn); | 
					
						
							|  |  |  | 		dsn = ast_strdup(tmp); | 
					
						
							|  |  |  | 		if (dsn == NULL) { | 
					
						
							| 
									
										
										
										
											2005-01-15 09:01:52 +00:00
										 |  |  | 			res = -1; | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 		if (((tmp = ast_variable_retrieve(cfg, "global", "dispositionstring"))) && ast_true(tmp)) | 
					
						
							|  |  |  | 			ast_set_flag(&config, CONFIG_DISPOSITIONSTRING); | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 			ast_clear_flag(&config, CONFIG_DISPOSITIONSTRING); | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 		if (((tmp = ast_variable_retrieve(cfg, "global", "loguniqueid"))) && ast_true(tmp)) { | 
					
						
							|  |  |  | 			ast_set_flag(&config, CONFIG_LOGUNIQUEID); | 
					
						
							|  |  |  | 			ast_debug(1, "cdr_odbc: Logging uniqueid\n"); | 
					
						
							| 
									
										
										
										
											2004-10-05 01:05:30 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 			ast_clear_flag(&config, CONFIG_LOGUNIQUEID); | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 			ast_debug(1, "cdr_odbc: Not logging uniqueid\n"); | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 		if (((tmp = ast_variable_retrieve(cfg, "global", "usegmtime"))) && ast_true(tmp)) { | 
					
						
							|  |  |  | 			ast_set_flag(&config, CONFIG_USEGMTIME); | 
					
						
							|  |  |  | 			ast_debug(1, "cdr_odbc: Logging in GMT\n"); | 
					
						
							| 
									
										
										
										
											2005-02-04 05:52:45 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 			ast_clear_flag(&config, CONFIG_USEGMTIME); | 
					
						
							|  |  |  | 			ast_debug(1, "cdr_odbc: Logging in local time\n"); | 
					
						
							| 
									
										
										
										
											2005-02-04 05:52:45 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-08 23:48:17 +00:00
										 |  |  | 		if (((tmp = ast_variable_retrieve(cfg, "global", "hrtime"))) && ast_true(tmp)) { | 
					
						
							|  |  |  | 			ast_set_flag(&config, CONFIG_HRTIME); | 
					
						
							|  |  |  | 			ast_debug(1, "cdr_odbc: Logging billsec and duration fields as floats\n"); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_clear_flag(&config, CONFIG_HRTIME); | 
					
						
							|  |  |  | 			ast_debug(1, "cdr_odbc: Logging billsec and duration fields as integers\n"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 		if ((tmp = ast_variable_retrieve(cfg, "global", "table")) == NULL) { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "cdr_odbc: table not specified.  Assuming cdr\n"); | 
					
						
							|  |  |  | 			tmp = "cdr"; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (table) | 
					
						
							|  |  |  | 			ast_free(table); | 
					
						
							|  |  |  | 		table = ast_strdup(tmp); | 
					
						
							|  |  |  | 		if (table == NULL) { | 
					
						
							|  |  |  | 			res = -1; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-12-10 11:59:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-25 18:22:13 +00:00
										 |  |  | 		if (!ast_test_flag(&config, CONFIG_REGISTERED)) { | 
					
						
							|  |  |  | 			res = ast_cdr_register(name, ast_module_info->description, odbc_log); | 
					
						
							|  |  |  | 			if (res) { | 
					
						
							|  |  |  | 				ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n"); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				ast_set_flag(&config, CONFIG_REGISTERED); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-01-15 09:01:52 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-04-15 20:55:33 -03:00
										 |  |  | 		if (((tmp = ast_variable_retrieve(cfg, "global", "newcdrcolumns"))) && ast_true(tmp)) { | 
					
						
							|  |  |  | 			ast_set_flag(&config, CONFIG_NEWCDRCOLUMNS); | 
					
						
							|  |  |  | 			ast_debug(1, "cdr_odbc: Add new cdr columns\n"); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_clear_flag(&config, CONFIG_NEWCDRCOLUMNS); | 
					
						
							|  |  |  | 			ast_debug(1, "cdr_odbc: Not add new cdr columns\n"); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 	} while (0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-25 18:22:13 +00:00
										 |  |  | 	if (ast_test_flag(&config, CONFIG_REGISTERED) && (!cfg || dsn == NULL || table == NULL)) { | 
					
						
							| 
									
										
										
										
											2013-10-27 20:04:17 +00:00
										 |  |  | 		ast_cdr_backend_suspend(name); | 
					
						
							| 
									
										
										
										
											2010-07-25 18:22:13 +00:00
										 |  |  | 		ast_clear_flag(&config, CONFIG_REGISTERED); | 
					
						
							| 
									
										
										
										
											2013-10-27 20:04:17 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_cdr_backend_unsuspend(name); | 
					
						
							| 
									
										
										
										
											2010-07-25 18:22:13 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (cfg && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) { | 
					
						
							| 
									
										
										
										
											2006-10-23 00:33:20 +00:00
										 |  |  | 		ast_config_destroy(cfg); | 
					
						
							| 
									
										
										
										
											2010-07-25 18:22:13 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	return odbc_load_module(0); | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-27 20:04:17 +00:00
										 |  |  | 	if (ast_cdr_unregister(name)) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	if (dsn) { | 
					
						
							|  |  |  | 		ast_free(dsn); | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	if (table) { | 
					
						
							|  |  |  | 		ast_free(table); | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-01 22:43:46 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int reload(void) | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 	return odbc_load_module(1); | 
					
						
							| 
									
										
										
										
											2003-12-02 00:27:28 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-20 19:35:02 +00:00
										 |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ODBC CDR Backend", | 
					
						
							| 
									
										
										
										
											2015-05-04 23:57:57 -04:00
										 |  |  | 	.support_level = AST_MODULE_SUPPORT_EXTENDED, | 
					
						
							|  |  |  | 	.load = load_module, | 
					
						
							|  |  |  | 	.unload = unload_module, | 
					
						
							|  |  |  | 	.reload = reload, | 
					
						
							|  |  |  | 	.load_pri = AST_MODPRI_CDR_DRIVER, | 
					
						
							| 
									
										
										
										
											2020-04-17 12:41:02 +02:00
										 |  |  | 	.requires = "cdr,res_odbc", | 
					
						
							| 
									
										
										
										
											2015-05-04 23:57:57 -04:00
										 |  |  | ); |