version 1.1, 2011/11/23 00:42:16
|
version 1.2, 2012/01/27 15:05:38
|
Line 0
|
Line 1
|
|
#include "global.h" |
|
#include "rtlm.h" |
|
|
|
|
|
static void *acc_rtlm, *pub_rtlm, *log_rtlm; |
|
|
|
struct tagCallbacks call; |
|
|
|
|
|
/* |
|
* mqttLog() Log message to syslog |
|
* |
|
* @fmt = format string |
|
* @... = argument list |
|
* return: none |
|
*/ |
|
void |
|
mqttLog(const char *fmt, ...) |
|
{ |
|
va_list lst; |
|
|
|
va_start(lst, fmt); |
|
vsyslog(LOG_ERR, fmt, lst); |
|
va_end(lst); |
|
} |
|
|
|
void * |
|
mqttLoadRTLM(sl_config *cfg, int modtype) |
|
{ |
|
const char *str, *attr; |
|
void *rtlm = NULL; |
|
void *(*mqttOpenRTLM)(sl_config *); |
|
void (*mqttCloseRTLM)(void *); |
|
|
|
if (!cfg) |
|
return NULL; |
|
|
|
switch (modtype) { |
|
case 0: |
|
attr = "acc_file"; |
|
break; |
|
case 1: |
|
attr = "pub_file"; |
|
break; |
|
default: |
|
attr = "log_file"; |
|
break; |
|
} |
|
|
|
str = (const char*) CFG(cfg_GetAttribute(cfg, CFG("mqttd"), CFG(attr))); |
|
if (!str) { |
|
mqttLog("Error:: RTL module not found\n"); |
|
return NULL; |
|
} |
|
|
|
rtlm = dlopen(str, RTLD_LAZY); |
|
if (!rtlm) { |
|
mqttLog("Error:: RTL module %s not found %s\n", str, dlerror()); |
|
return NULL; |
|
} |
|
|
|
mqttOpenRTLM = dlsym(rtlm, "mqtt_rtlm_open"); |
|
if (!mqttOpenRTLM) { |
|
mqttLog("Error:: Can't found mqtt_db_open call\n"); |
|
dlclose(rtlm); |
|
return NULL; |
|
} |
|
mqttCloseRTLM = dlsym(rtlm, "mqtt_rtlm_close"); |
|
if (!mqttCloseRTLM) { |
|
mqttLog("Error:: Can't found mqtt_db_close call\n"); |
|
dlclose(rtlm); |
|
return NULL; |
|
} |
|
|
|
switch (modtype) { |
|
case 0: |
|
acc_rtlm = rtlm; |
|
call.OpenACC = mqttOpenRTLM; |
|
call.CloseACC = mqttCloseRTLM; |
|
call.LoginACC = dlsym(rtlm, "mqtt_rtlm_login"); |
|
if (!call.LoginACC) { |
|
mqttLog("Error:: incorrect module ...\n"); |
|
dlclose(rtlm); |
|
return NULL; |
|
} |
|
break; |
|
case 1: |
|
pub_rtlm = rtlm; |
|
call.OpenPUB = mqttOpenRTLM; |
|
call.ClosePUB = mqttCloseRTLM; |
|
call.InitSessPUB = dlsym(rtlm, "mqtt_rtlm_init_session"); |
|
call.FiniSessPUB = dlsym(rtlm, "mqtt_rtlm_fini_session"); |
|
call.ChkSessPUB = dlsym(rtlm, "mqtt_rtlm_chk_session"); |
|
call.WritePUB = dlsym(rtlm, "mqtt_rtlm_write_topic"); |
|
call.ReadPUB = dlsym(rtlm, "mqtt_rtlm_read_topic"); |
|
call.DeletePUB = dlsym(rtlm, "mqtt_rtlm_delete_topic"); |
|
if (!call.InitSessPUB || !call.FiniSessPUB || !call.ChkSessPUB || |
|
!call.WritePUB || !call.ReadPUB || !call.DeletePUB) { |
|
mqttLog("Error:: incorrect module ...\n"); |
|
dlclose(rtlm); |
|
return NULL; |
|
} |
|
break; |
|
default: |
|
log_rtlm = rtlm; |
|
call.OpenLOG = mqttOpenRTLM; |
|
call.CloseLOG = mqttCloseRTLM; |
|
call.LOG = dlsym(rtlm, "mqtt_rtlm_logger"); |
|
if (!call.LOG) { |
|
mqttLog("Error:: incorrect module ...\n"); |
|
dlclose(rtlm); |
|
return NULL; |
|
} |
|
break; |
|
} |
|
|
|
return rtlm; |
|
} |
|
|
|
void |
|
mqttUnloadRTLM(int modtype) |
|
{ |
|
switch (modtype) { |
|
case 0: |
|
dlclose(acc_rtlm); |
|
break; |
|
case 1: |
|
dlclose(pub_rtlm); |
|
break; |
|
default: |
|
dlclose(log_rtlm); |
|
break; |
|
} |
|
} |
|
|
|
inline int |
|
mqttMkDir(sl_config *cfg) |
|
{ |
|
const char *str; |
|
|
|
if (!cfg) |
|
return -1; |
|
|
|
str = (const char*) cfg_GetAttribute(cfg, CFG("mqttd"), CFG("statedir")); |
|
if (!str) |
|
return -1; |
|
|
|
if (mkdir(str, 0600) == -1 && errno != EEXIST) |
|
return -1; |
|
|
|
return 0; |
|
} |