2021-04-22 04:15:23 +02:00
|
|
|
#include <module_com.h>
|
2023-02-25 18:14:35 +01:00
|
|
|
#include <utils/flog.h>
|
2021-04-22 04:15:23 +02:00
|
|
|
|
|
|
|
bool ModuleComManager::registerInterface(std::string moduleName, std::string name, void (*handler)(int code, void* in, void* out, void* ctx), void* ctx) {
|
2023-09-29 14:42:45 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lck(mtx);
|
2021-04-22 04:15:23 +02:00
|
|
|
if (interfaces.find(name) != interfaces.end()) {
|
2023-02-25 18:12:34 +01:00
|
|
|
flog::error("Tried creating module interface with an existing name: {0}", name);
|
2021-04-22 04:15:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ModuleComInterface iface;
|
|
|
|
iface.moduleName = moduleName;
|
|
|
|
iface.handler = handler;
|
|
|
|
iface.ctx = ctx;
|
|
|
|
interfaces[name] = iface;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleComManager::unregisterInterface(std::string name) {
|
2023-09-29 14:42:45 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lck(mtx);
|
2021-04-22 04:15:23 +02:00
|
|
|
if (interfaces.find(name) == interfaces.end()) {
|
2023-02-25 18:12:34 +01:00
|
|
|
flog::error("Tried to erase module interface with unknown name: {0}", name);
|
2021-04-22 04:15:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
interfaces.erase(name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleComManager::interfaceExists(std::string name) {
|
2023-09-29 14:42:45 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lck(mtx);
|
2021-04-22 04:15:23 +02:00
|
|
|
if (interfaces.find(name) == interfaces.end()) { return false; }
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ModuleComManager::getModuleName(std::string name) {
|
2023-09-29 14:42:45 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lck(mtx);
|
2021-04-22 04:15:23 +02:00
|
|
|
if (interfaces.find(name) == interfaces.end()) {
|
2023-02-25 18:12:34 +01:00
|
|
|
flog::error("Tried to call unknown module interface: {0}", name);
|
2021-04-22 04:15:23 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return interfaces[name].moduleName;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleComManager::callInterface(std::string name, int code, void* in, void* out) {
|
2023-09-29 14:42:45 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lck(mtx);
|
2021-04-22 04:15:23 +02:00
|
|
|
if (interfaces.find(name) == interfaces.end()) {
|
2023-02-25 18:12:34 +01:00
|
|
|
flog::error("Tried to call unknown module interface: {0}", name);
|
2021-04-22 04:15:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ModuleComInterface iface = interfaces[name];
|
|
|
|
iface.handler(code, in, out, iface.ctx);
|
|
|
|
return true;
|
|
|
|
}
|