2025-01-08 06:26:36 -04:00
|
|
|
#include <iostream>
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <optional>
|
2025-01-08 13:43:44 -04:00
|
|
|
#include <filesystem>
|
2025-10-01 18:47:42 -04:00
|
|
|
#include <stimBuffApis/stimBuffApiManager.h>
|
|
|
|
|
#include <stimBuffApis/stimBuffApiLib.h>
|
2025-01-08 13:43:44 -04:00
|
|
|
#include <opts.h>
|
2025-09-10 11:37:35 -04:00
|
|
|
#include <asynchronousBridge.h>
|
2025-09-10 18:12:08 -04:00
|
|
|
#include <asynchronousContinuation.h>
|
2025-09-10 11:37:35 -04:00
|
|
|
#include <asynchronousLoop.h>
|
2025-09-27 18:30:09 -04:00
|
|
|
#include <callback.h>
|
2025-01-08 13:43:44 -04:00
|
|
|
#include <user/senseApiDesc.h>
|
2025-09-15 12:47:09 -04:00
|
|
|
#include <mind.h>
|
2025-01-12 14:31:33 -04:00
|
|
|
#include <deviceManager/deviceManager.h>
|
2025-09-16 18:38:06 -04:00
|
|
|
#include <marionette/marionette.h>
|
|
|
|
|
|
2025-01-08 13:43:44 -04:00
|
|
|
|
|
|
|
|
namespace fs = std::filesystem;
|
2025-01-08 06:26:36 -04:00
|
|
|
|
2025-07-22 06:48:04 -04:00
|
|
|
namespace smo {
|
2025-10-01 18:47:42 -04:00
|
|
|
namespace stim_buff {
|
2025-01-08 06:26:36 -04:00
|
|
|
|
2025-01-14 14:11:40 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Searches for a library in predefined locations
|
|
|
|
|
* @param libraryPath The name or path of the library to find
|
|
|
|
|
* @return Optional containing the full path if found in search paths, nullopt
|
|
|
|
|
* if not
|
|
|
|
|
*
|
|
|
|
|
* Searches for the library in the following locations in order:
|
|
|
|
|
* 1. Custom path specified by --sense-api-lib-path option (if provided)
|
|
|
|
|
* 2. Current working directory
|
|
|
|
|
* 3. Directory containing the executable
|
|
|
|
|
*
|
|
|
|
|
* If the library is not found in any of these locations, returns nullopt and
|
|
|
|
|
* falls back to system default library search paths (LD_LIBRARY_PATH, etc.)
|
|
|
|
|
*/
|
2025-07-24 02:12:31 -04:00
|
|
|
static std::optional<std::string> searchForLibInSmoSearchPaths(
|
2025-01-08 13:43:44 -04:00
|
|
|
const std::string& libraryPath)
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::string> searchPaths = {
|
|
|
|
|
fs::current_path().string(),
|
|
|
|
|
fs::path("/proc/self/exe").parent_path().string()
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-23 00:12:50 -04:00
|
|
|
const auto& options = OptionParser::getOptions();
|
|
|
|
|
if (!options.senseApiLibPath.empty())
|
2025-01-08 15:08:23 -04:00
|
|
|
{
|
2025-10-01 18:47:42 -04:00
|
|
|
// Insert all stim buff API library paths at the beginning of search paths
|
2025-01-08 15:08:23 -04:00
|
|
|
searchPaths.insert(
|
2025-07-23 00:12:50 -04:00
|
|
|
searchPaths.begin(),
|
|
|
|
|
options.senseApiLibPath.begin(),
|
|
|
|
|
options.senseApiLibPath.end());
|
2025-01-08 15:08:23 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-08 13:43:44 -04:00
|
|
|
for (const auto& path : searchPaths)
|
|
|
|
|
{
|
|
|
|
|
fs::path fullPath = fs::path(path) / libraryPath;
|
|
|
|
|
if (fs::exists(fullPath))
|
|
|
|
|
{
|
|
|
|
|
return fullPath.string();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cerr << std::string(__func__) + ": library '"
|
|
|
|
|
+ libraryPath + "' isn't in search bespoke search paths: ";
|
2025-01-13 21:57:11 -04:00
|
|
|
for (const auto& path : searchPaths) {
|
2025-01-08 13:43:44 -04:00
|
|
|
std::cerr << path << " ";
|
|
|
|
|
}
|
|
|
|
|
std::cerr << std::endl;
|
2025-01-13 21:57:11 -04:00
|
|
|
std::cerr << "Trying to load " + libraryPath + " from system default "
|
|
|
|
|
"search paths\n";
|
2025-01-08 13:43:44 -04:00
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 12:02:40 -04:00
|
|
|
/* Local static function to wrap ComponentThread::getSelf for SmoCallbacks */
|
|
|
|
|
static std::shared_ptr<ComponentThread> ComponentThread_getSelf()
|
|
|
|
|
{
|
|
|
|
|
return ComponentThread::getSelf();
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
/* Hooks to be provided to stimBuffApiLibs, enabling them to call into Salmanoff
|
2025-07-24 06:00:35 -04:00
|
|
|
* code.
|
|
|
|
|
*/
|
2025-09-04 17:45:11 -04:00
|
|
|
static SmoCallbacks smoCallbacks =
|
2025-07-24 06:00:35 -04:00
|
|
|
{
|
2025-09-09 12:02:40 -04:00
|
|
|
.searchForLibInSmoSearchPaths = searchForLibInSmoSearchPaths,
|
|
|
|
|
.ComponentThread_getSelf = ComponentThread_getSelf
|
2025-07-24 06:00:35 -04:00
|
|
|
};
|
|
|
|
|
|
2025-09-04 17:35:49 -04:00
|
|
|
/* Static file-scope threading model object for senseApi libraries */
|
|
|
|
|
static SmoThreadingModelDesc smoThreadingModelDesc = {
|
|
|
|
|
.componentThread = nullptr
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
std::optional<std::string> StimBuffApiManager::searchForLibInSmoSearchPaths(
|
2025-07-24 02:12:31 -04:00
|
|
|
const std::string& libraryPath)
|
|
|
|
|
{
|
2025-10-01 18:47:42 -04:00
|
|
|
return ::smo::stim_buff::searchForLibInSmoSearchPaths(libraryPath);
|
2025-07-24 02:12:31 -04:00
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
StimBuffApiLib& StimBuffApiManager::loadStimBuffApiLib(
|
2025-09-04 17:35:49 -04:00
|
|
|
const std::string& libraryPath,
|
2025-09-12 16:09:26 -04:00
|
|
|
const std::shared_ptr<ComponentThread>& componentThread
|
2025-09-04 17:35:49 -04:00
|
|
|
)
|
2025-01-08 06:26:36 -04:00
|
|
|
{
|
2025-07-24 02:12:31 -04:00
|
|
|
std::optional<std::string> fullPath = searchForLibInSmoSearchPaths(
|
|
|
|
|
libraryPath);
|
2025-01-09 06:03:43 -04:00
|
|
|
std::string resolvedPath = fullPath.value_or(libraryPath);
|
2025-01-08 06:26:36 -04:00
|
|
|
|
|
|
|
|
// Clear any existing error
|
|
|
|
|
dlerror();
|
2025-10-01 18:47:42 -04:00
|
|
|
auto dlopen_handle = std::unique_ptr<void, StimBuffApiLib::DlCloser>(
|
2025-01-09 06:03:43 -04:00
|
|
|
dlopen(resolvedPath.c_str(), RTLD_LAZY));
|
|
|
|
|
if (!dlopen_handle && fullPath.has_value())
|
2025-01-08 13:43:44 -04:00
|
|
|
{
|
|
|
|
|
// Fallback to using the supplied libraryPath
|
|
|
|
|
dlerror();
|
2025-01-09 06:03:43 -04:00
|
|
|
dlopen_handle.reset(dlopen(libraryPath.c_str(), RTLD_LAZY));
|
2025-01-08 13:43:44 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-09 06:03:43 -04:00
|
|
|
if (!dlopen_handle)
|
2025-01-08 06:26:36 -04:00
|
|
|
{
|
2025-01-08 15:08:51 -04:00
|
|
|
const char *dlerr = dlerror();
|
|
|
|
|
|
|
|
|
|
std::string error = (dlerr
|
|
|
|
|
? dlerr
|
|
|
|
|
: "Unknown error while opening shlib");
|
2025-01-08 06:26:36 -04:00
|
|
|
throw std::runtime_error(
|
2025-01-08 15:08:51 -04:00
|
|
|
std::string(__func__) + ": Cannot load library '"
|
2025-01-08 06:26:36 -04:00
|
|
|
+ libraryPath + "': "
|
|
|
|
|
+ error);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 13:43:44 -04:00
|
|
|
// Initialize getSenseApiDescriptor
|
2025-10-01 18:47:42 -04:00
|
|
|
auto func = reinterpret_cast<SMO_GET_STIM_BUFF_API_DESC_FN_TYPEDEF *>(
|
|
|
|
|
dlsym(dlopen_handle.get(), SMO_GET_STIM_BUFF_API_DESC_FN_NAME_STR));
|
2025-01-08 13:43:44 -04:00
|
|
|
if (!func)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error(
|
2025-01-08 15:09:18 -04:00
|
|
|
std::string(__func__) + ": dlsym('"
|
2025-10-01 18:47:42 -04:00
|
|
|
SMO_GET_STIM_BUFF_API_DESC_FN_NAME_STR "') failed for library '"
|
2025-01-09 06:03:43 -04:00
|
|
|
+ libraryPath + "'");
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 17:35:49 -04:00
|
|
|
// Check if the static threading model obj is null and initialize if needed
|
|
|
|
|
if (!smoThreadingModelDesc.componentThread) {
|
|
|
|
|
smoThreadingModelDesc.componentThread = componentThread;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
const StimBuffApiDesc &libApiDesc = func(
|
2025-09-04 17:45:11 -04:00
|
|
|
smoCallbacks, smoThreadingModelDesc);
|
2025-09-04 17:35:49 -04:00
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
auto lib = std::make_shared<StimBuffApiLib>(
|
2025-01-09 06:03:43 -04:00
|
|
|
libraryPath, dlopen_handle.release(), func);
|
2025-10-01 18:47:42 -04:00
|
|
|
lib->setStimBuffApiDesc(libApiDesc);
|
|
|
|
|
stimBuffApiLibs.push_back(lib);
|
|
|
|
|
return *stimBuffApiLibs.back();
|
2025-01-08 06:26:36 -04:00
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
std::optional<std::shared_ptr<StimBuffApiLib>>
|
|
|
|
|
StimBuffApiManager::getStimBuffApiLib(const std::string& libraryPath)
|
2025-01-08 06:26:36 -04:00
|
|
|
{
|
2025-10-01 18:47:42 -04:00
|
|
|
auto it = std::find_if(stimBuffApiLibs.begin(), stimBuffApiLibs.end(),
|
|
|
|
|
[&libPath = libraryPath](const std::shared_ptr<StimBuffApiLib>& lib) {
|
2025-01-08 06:26:36 -04:00
|
|
|
return lib->libraryPath == libPath;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
if (it != stimBuffApiLibs.end()) { return *it; }
|
2025-01-08 06:26:36 -04:00
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
std::optional<std::shared_ptr<StimBuffApiLib>>
|
|
|
|
|
StimBuffApiManager::getStimBuffApiLibByApiName(const std::string& apiName)
|
2025-01-12 14:31:33 -04:00
|
|
|
{
|
2025-10-01 18:47:42 -04:00
|
|
|
auto it = std::find_if(stimBuffApiLibs.begin(), stimBuffApiLibs.end(),
|
|
|
|
|
[&apiName](const std::shared_ptr<StimBuffApiLib>& lib) {
|
|
|
|
|
return lib->stimBuffApiDesc.name == apiName;
|
2025-01-12 14:31:33 -04:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
if (it != stimBuffApiLibs.end()) { return *it; }
|
2025-01-12 14:31:33 -04:00
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
void StimBuffApiManager::unloadStimBuffApiLib(const std::string& libraryPath)
|
2025-01-08 06:26:36 -04:00
|
|
|
{
|
2025-10-01 18:47:42 -04:00
|
|
|
auto it = std::find_if(stimBuffApiLibs.begin(), stimBuffApiLibs.end(),
|
|
|
|
|
[&lpath = libraryPath](const std::shared_ptr<StimBuffApiLib>& lib) {
|
2025-01-08 06:26:36 -04:00
|
|
|
return lib->libraryPath == lpath;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
if (it != stimBuffApiLibs.end())
|
2025-01-08 06:26:36 -04:00
|
|
|
{
|
2025-10-01 18:47:42 -04:00
|
|
|
stimBuffApiLibs.erase(it);
|
2025-01-08 06:26:36 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cerr << std::string(__func__) + ": Library not found: "
|
|
|
|
|
<< libraryPath << '\n';
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
void StimBuffApiManager::unloadAllStimBuffApiLibs(void)
|
2025-01-12 09:44:49 -04:00
|
|
|
{
|
2025-10-01 18:47:42 -04:00
|
|
|
stimBuffApiLibs.clear();
|
2025-01-12 09:44:49 -04:00
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
void StimBuffApiManager::loadAllStimBuffApiLibsFromOptions(
|
2025-09-12 16:09:26 -04:00
|
|
|
const std::shared_ptr<ComponentThread>& componentThread
|
2025-09-04 17:35:49 -04:00
|
|
|
)
|
2025-01-08 15:06:31 -04:00
|
|
|
{
|
|
|
|
|
const auto& options = OptionParser::getOptions();
|
|
|
|
|
for (const auto& libPath : options.senseApiLibs) {
|
2025-10-01 18:47:42 -04:00
|
|
|
loadStimBuffApiLib(libPath, componentThread);
|
2025-01-08 15:06:31 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
std::string StimBuffApiManager::stringifyLibs() const
|
2025-01-11 06:40:43 -04:00
|
|
|
{
|
|
|
|
|
std::string result;
|
2025-10-01 18:47:42 -04:00
|
|
|
for (const auto& lib : stimBuffApiLibs) {
|
2025-01-11 06:40:43 -04:00
|
|
|
result += lib->stringify() + "\n";
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
void StimBuffApiManager::initializeStimBuffApiLib(StimBuffApiLib& lib)
|
2025-01-12 09:44:49 -04:00
|
|
|
{
|
2025-09-30 20:48:45 -04:00
|
|
|
/** FIXME:
|
|
|
|
|
* When we eventually make this method async, this method should acquire
|
2025-10-01 18:47:42 -04:00
|
|
|
* the StimBuffApiManager's main CRUD qutex.
|
2025-09-30 20:48:45 -04:00
|
|
|
*/
|
2025-10-01 18:47:42 -04:00
|
|
|
if (!lib.stimBuffApiDesc.sal_mgmt_libOps.initializeInd)
|
2025-01-12 09:44:49 -04:00
|
|
|
{
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
std::string(__func__) + ": initializeInd() is NULL for library '"
|
|
|
|
|
+ lib.libraryPath + "'");
|
|
|
|
|
}
|
2025-10-01 18:47:42 -04:00
|
|
|
lib.stimBuffApiDesc.sal_mgmt_libOps.initializeInd();
|
2025-01-12 09:44:49 -04:00
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
void StimBuffApiManager::finalizeStimBuffApiLib(StimBuffApiLib& lib)
|
2025-01-12 09:44:49 -04:00
|
|
|
{
|
2025-09-30 20:48:45 -04:00
|
|
|
/** FIXME:
|
|
|
|
|
* When we eventually make this method async, this flag should only be set
|
2025-10-01 18:47:42 -04:00
|
|
|
* after acquiring the StimBuffApiManager's main CRUD qutex.
|
2025-09-30 20:48:45 -04:00
|
|
|
*/
|
|
|
|
|
lib.isBeingDestroyed.store(true);
|
2025-10-01 18:47:42 -04:00
|
|
|
if (!lib.stimBuffApiDesc.sal_mgmt_libOps.finalizeInd)
|
2025-01-12 09:44:49 -04:00
|
|
|
{
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
std::string(__func__) + ": finalizeInd() is NULL for library '"
|
|
|
|
|
+ lib.libraryPath + "'");
|
|
|
|
|
}
|
2025-10-01 18:47:42 -04:00
|
|
|
lib.stimBuffApiDesc.sal_mgmt_libOps.finalizeInd();
|
2025-01-12 09:44:49 -04:00
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
void StimBuffApiManager::initializeAllStimBuffApiLibs(void)
|
2025-01-12 09:44:49 -04:00
|
|
|
{
|
2025-10-01 18:47:42 -04:00
|
|
|
for (auto& lib : stimBuffApiLibs) {
|
|
|
|
|
initializeStimBuffApiLib(*lib);
|
2025-01-12 09:44:49 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
void StimBuffApiManager::finalizeAllStimBuffApiLibs(void)
|
2025-01-12 09:44:49 -04:00
|
|
|
{
|
2025-10-01 18:47:42 -04:00
|
|
|
for (auto& lib : stimBuffApiLibs) {
|
|
|
|
|
finalizeStimBuffApiLib(*lib);
|
2025-01-12 09:44:49 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-12 14:31:33 -04:00
|
|
|
|
2025-10-01 18:47:42 -04:00
|
|
|
} // namespace stim_buff
|
2025-07-22 06:48:04 -04:00
|
|
|
} // namespace smo
|