#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sqlite.h>
#include "asterisk.h"
#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
Go to the source code of this file.
Defines | |
| #define | DATE_FORMAT "%Y-%m-%d %T" |
| #define | LOG_UNIQUEID 0 |
| #define | LOG_USERFIELD 0 |
Functions | |
| AST_MUTEX_DEFINE_STATIC (sqlite_lock) | |
| char * | description (void) |
| Provides a description of the module. | |
| char * | key () |
| Returns the ASTERISK_GPL_KEY. | |
| int | load_module (void) |
| Initialize the module. | |
| int | reload (void) |
| Reload stuff. | |
| int | sqlite_log (struct ast_cdr *cdr) |
| int | unload_module (void) |
| Cleanup all module structures, sockets, etc. | |
| int | usecount (void) |
| Provides a usecount. | |
Variables | |
| sqlite * | db = NULL |
| char * | desc = "SQLite CDR Backend" |
| char * | name = "sqlite" |
| char | sql_create_table [] |
| SQL table format. | |
Definition in file cdr_sqlite.c.
|
|
Definition at line 54 of file cdr_sqlite.c. |
|
|
Definition at line 50 of file cdr_sqlite.c. Referenced by sqlite_log(). |
|
|
Definition at line 51 of file cdr_sqlite.c. Referenced by sqlite_log(). |
|
|
|
|
|
Provides a description of the module.
Definition at line 166 of file cdr_sqlite.c. 00167 {
00168 return desc;
00169 }
|
|
|
Returns the ASTERISK_GPL_KEY. This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:
char *key(void) { return ASTERISK_GPL_KEY; }
Definition at line 230 of file cdr_sqlite.c. 00231 {
00232 return ASTERISK_GPL_KEY;
00233 }
|
|
|
Initialize the module. Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.
Definition at line 179 of file cdr_sqlite.c. References ast_cdr_register(), ast_config_AST_LOG_DIR, ast_log(), db, desc, free, LOG_ERROR, name, sql_create_table, and sqlite_log(). 00180 {
00181 char *zErr;
00182 char fn[PATH_MAX];
00183 int res;
00184
00185 /* is the database there? */
00186 snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
00187 db = sqlite_open(fn, 0660, &zErr);
00188 if (!db) {
00189 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00190 free(zErr);
00191 return -1;
00192 }
00193
00194 /* is the table there? */
00195 res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
00196 if (res) {
00197 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
00198 if (res) {
00199 ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
00200 free(zErr);
00201 goto err;
00202 }
00203
00204 /* TODO: here we should probably create an index */
00205 }
00206
00207 res = ast_cdr_register(name, desc, sqlite_log);
00208 if (res) {
00209 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
00210 return -1;
00211 }
00212 return 0;
00213
00214 err:
00215 if (db)
00216 sqlite_close(db);
00217 return -1;
00218 }
|
|
|
Reload stuff. This function is where any reload routines take place. Re-read config files, change signalling, whatever is appropriate on a reload.
Definition at line 220 of file cdr_sqlite.c. 00221 {
00222 return 0;
00223 }
|
|
|
Definition at line 89 of file cdr_sqlite.c. References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr::answer, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_cdr::billsec, ast_cdr::channel, ast_cdr::clid, DATE_FORMAT, db, ast_cdr::dcontext, ast_cdr::disposition, ast_cdr::dst, ast_cdr::dstchannel, ast_cdr::duration, ast_cdr::end, free, ast_cdr::lastapp, ast_cdr::lastdata, LOG_ERROR, LOG_UNIQUEID, LOG_USERFIELD, ast_cdr::src, ast_cdr::start, ast_cdr::uniqueid, and ast_cdr::userfield. Referenced by load_module(). 00090 {
00091 int res = 0;
00092 char *zErr = 0;
00093 struct tm tm;
00094 time_t t;
00095 char startstr[80], answerstr[80], endstr[80];
00096 int count;
00097
00098 ast_mutex_lock(&sqlite_lock);
00099
00100 t = cdr->start.tv_sec;
00101 localtime_r(&t, &tm);
00102 strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
00103
00104 t = cdr->answer.tv_sec;
00105 localtime_r(&t, &tm);
00106 strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
00107
00108 t = cdr->end.tv_sec;
00109 localtime_r(&t, &tm);
00110 strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
00111
00112 for(count=0; count<5; count++) {
00113 res = sqlite_exec_printf(db,
00114 "INSERT INTO cdr ("
00115 "clid,src,dst,dcontext,"
00116 "channel,dstchannel,lastapp,lastdata, "
00117 "start,answer,end,"
00118 "duration,billsec,disposition,amaflags, "
00119 "accountcode"
00120 # if LOG_UNIQUEID
00121 ",uniqueid"
00122 # endif
00123 # if LOG_USERFIELD
00124 ",userfield"
00125 # endif
00126 ") VALUES ("
00127 "'%q', '%q', '%q', '%q', "
00128 "'%q', '%q', '%q', '%q', "
00129 "'%q', '%q', '%q', "
00130 "%d, %d, %d, %d, "
00131 "'%q'"
00132 # if LOG_UNIQUEID
00133 ",'%q'"
00134 # endif
00135 # if LOG_USERFIELD
00136 ",'%q'"
00137 # endif
00138 ")", NULL, NULL, &zErr,
00139 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
00140 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
00141 startstr, answerstr, endstr,
00142 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
00143 cdr->accountcode
00144 # if LOG_UNIQUEID
00145 ,cdr->uniqueid
00146 # endif
00147 # if LOG_USERFIELD
00148 ,cdr->userfield
00149 # endif
00150 );
00151 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
00152 break;
00153 usleep(200);
00154 }
00155
00156 if (zErr) {
00157 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00158 free(zErr);
00159 }
00160
00161 ast_mutex_unlock(&sqlite_lock);
00162 return res;
00163 }
|
|
|
Cleanup all module structures, sockets, etc. This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).
Definition at line 171 of file cdr_sqlite.c. References ast_cdr_unregister(), db, and name. 00172 {
00173 if (db)
00174 sqlite_close(db);
00175 ast_cdr_unregister(name);
00176 return 0;
00177 }
|
|
|
Provides a usecount. This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.
Definition at line 225 of file cdr_sqlite.c. 00226 {
00227 return 0;
00228 }
|
|
|
Definition at line 58 of file cdr_sqlite.c. Referenced by load_module(), sqlite_log(), and unload_module(). |
|
|
Definition at line 56 of file cdr_sqlite.c. Referenced by load_module(). |
|
|
Definition at line 57 of file cdr_sqlite.c. |
|
|
SQL table format.
Definition at line 63 of file cdr_sqlite.c. Referenced by load_module(). |
1.3.9.1