#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_topic = dlsym(rtlm, "mqtt_rtlm_write_topic");
call.ReadPUB_topic = dlsym(rtlm, "mqtt_rtlm_read_topic");
call.DeletePUB_topic = dlsym(rtlm, "mqtt_rtlm_delete_topic");
call.WritePUB_subscribe = dlsym(rtlm, "mqtt_rtlm_write_subscribe");
call.ReadPUB_subscribe = dlsym(rtlm, "mqtt_rtlm_read_subscribe");
call.DeletePUB_subscribe = dlsym(rtlm, "mqtt_rtlm_delete_subscribe");
if (!call.InitSessPUB || !call.FiniSessPUB || !call.ChkSessPUB ||
!call.WritePUB_topic || !call.ReadPUB_topic ||
!call.DeletePUB_topic || !call.WritePUB_subscribe ||
!call.ReadPUB_subscribe || !call.DeletePUB_subscribe) {
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;
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>