Files
salmanoff/smocore/include/senseApis/senseApiLib.h
T

90 lines
2.1 KiB
C++
Raw Normal View History

#ifndef SENSE_API_PROVIDER_DESC_H
#define SENSE_API_PROVIDER_DESC_H
#include <string>
#include <memory>
#include <atomic>
#include <vector>
#include <dlfcn.h>
#include <functional>
#include <user/senseApiDesc.h>
#include <qutex.h>
2025-07-22 06:48:04 -04:00
namespace smo {
namespace sense_api {
class SenseApiLib
{
2025-01-12 09:44:08 -04:00
private:
2025-09-30 01:15:02 -04:00
friend class SenseApiManager;
struct DlCloser
{
void operator()(void* handle) const
{
if (handle) {
dlclose(handle);
}
}
};
2025-01-12 09:44:08 -04:00
public:
2025-09-30 01:15:02 -04:00
SenseApiLib(
const std::string& path, void *_dlopen_handle,
SMO_GET_SENSE_API_DESC_FN_TYPEDEF *descFn)
: libraryPath(path), qutex("SenseApiLib-" + path), isBeingDestroyed(false),
dlopen_handle(_dlopen_handle, DlCloser()),
SMO_GET_SENSE_API_DESC_FN_NAME(descFn)
2025-09-30 01:15:02 -04:00
{}
2025-09-30 01:15:02 -04:00
void setSenseApiDesc(const SenseApiDesc &desc)
{
if (!SenseApiDesc::sanityCheck(desc))
{
throw std::runtime_error(
std::string(__func__) + ": Sanity check failed for sense API "
"descriptor in library '" + libraryPath + "'");
}
2025-09-30 01:15:02 -04:00
senseApiDesc = desc;
}
public:
2025-09-30 01:15:02 -04:00
std::string libraryPath;
Qutex qutex;
std::atomic<bool> isBeingDestroyed;
2025-09-30 01:15:02 -04:00
std::unique_ptr<void, DlCloser> dlopen_handle;
/* UNIMPLEMENTED: API-specific cmdline options. These affect this specific
* sense api lib's behaviour globally.
*/
std::vector<std::string> options;
2025-09-30 01:15:02 -04:00
/**
* @brief Every sense API lib is required to provide a function that returns
* a SenseApiDesc struct. This struct states which API the lib uses to
* connect Salmanoff to the sense provider it supports.
*
* This getter function should be visible to dlsym() so that Salmanoff can
* find it in the lib after loading it, and call it.
*/
std::function<SMO_GET_SENSE_API_DESC_FN_TYPEDEF>
SMO_GET_SENSE_API_DESC_FN_NAME;
2025-09-30 01:15:02 -04:00
/**
* @brief Salmanoff will call the `SMO_GET_SENSE_API_DESC_FN_NAME` getter
* function and use the data it provides in order to fill out this
* descriptor.
*/
SenseApiDesc senseApiDesc;
2025-09-30 01:15:02 -04:00
std::string stringify() const {
std::string result = "Library Path: " + libraryPath + "\n";
result += "Sense API Descriptor: " + senseApiDesc.stringify() + "\n";
return result;
}
};
} // namespace sense_api
2025-07-22 06:48:04 -04:00
} // namespace smo
#endif // SENSE_API_PROVIDER_DESC_H