Provide dlopen() path searching hook to senseApi libs
This commit is contained in:
@@ -457,13 +457,18 @@ static smo::sense_api::SenseApiDesc xcbXorgApiDesc =
|
||||
* its name, the implexors it exports, and the management operations it
|
||||
* supports.
|
||||
*
|
||||
* @param callbacks Hooks provided by Salmanoff for library operations
|
||||
* @return A reference to the SenseApiDesc structure describing the API.
|
||||
*/
|
||||
extern SMO_UNMANGLED smo::sense_api::SMO_GET_SENSE_API_DESC_FN_TYPEDEF
|
||||
SMO_GET_SENSE_API_DESC_FN_NAME;
|
||||
SMO_GET_SENSE_API_DESC_FN_NAME;
|
||||
|
||||
const smo::sense_api::SenseApiDesc &SMO_GET_SENSE_API_DESC_FN_NAME(void)
|
||||
static const smo::sense_api::SalmanoffCallbacks* xcbXorg_callbacks = nullptr;
|
||||
|
||||
const smo::sense_api::SenseApiDesc &SMO_GET_SENSE_API_DESC_FN_NAME(
|
||||
const smo::sense_api::SalmanoffCallbacks& callbacks)
|
||||
{
|
||||
xcbXorg_callbacks = &callbacks;
|
||||
return xcbXorgApiDesc;
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <preprocessor.h>
|
||||
#include <stdbool.h>
|
||||
#include <user/senseDeviceSpec.h>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace smo {
|
||||
namespace sense_api {
|
||||
@@ -13,6 +15,27 @@ typedef int (sal_mlo_finalizeIndFn)(void);
|
||||
typedef int (sal_mlo_attachDeviceReqFn)(const device::SenseDeviceSpec &desc);
|
||||
typedef int (sal_mlo_detachDeviceReqFn)(const device::SenseDeviceSpec &desc);
|
||||
|
||||
/**
|
||||
* @brief Hooks provided by Salmanoff to senseApi libraries.
|
||||
|
||||
* This structure contains function pointers that senseApi libraries can use
|
||||
* to interact with Salmanoff's functionality, such as searching for commonLibs.
|
||||
*/
|
||||
struct SalmanoffCallbacks
|
||||
{
|
||||
/**
|
||||
* @brief Search for a library in Salmanoff's search paths
|
||||
* @param libraryPath The relative filename of the library to search for
|
||||
* @return Optional containing the full path if found, nullopt if not found
|
||||
*
|
||||
* This function searches for the given library in the same search paths
|
||||
* that Salmanoff uses when loading senseApi libraries (user-specified
|
||||
* paths via -p option, current directory, and executable directory).
|
||||
*/
|
||||
std::optional<std::string> (*searchForLibInSmoSearchPaths)(
|
||||
const std::string& libraryPath);
|
||||
};
|
||||
|
||||
struct Sal_Mgmt_LibOps
|
||||
{
|
||||
/* When Salmanoff loads a sense API lib, it calls this function to initialize
|
||||
@@ -105,8 +128,12 @@ public:
|
||||
* Smo what implexors can be used with it & what APIs it exports.
|
||||
* The SenseApiDesc struct also gives Smo pointers to API functions
|
||||
* to invoke for communication between Smo and the library.
|
||||
*
|
||||
* The SalmanoffCallbacks parameter provides the library with access to
|
||||
* Salmanoff's hooks.
|
||||
*/
|
||||
typedef const SenseApiDesc &(SMO_GET_SENSE_API_DESC_FN_TYPEDEF)(void);
|
||||
typedef const SenseApiDesc &(SMO_GET_SENSE_API_DESC_FN_TYPEDEF)(
|
||||
const SalmanoffCallbacks& callbacks);
|
||||
|
||||
} // namespace sense_api
|
||||
} // namespace smo
|
||||
|
||||
@@ -52,6 +52,10 @@ private:
|
||||
SenseApiManager& operator=(const SenseApiManager&) = delete;
|
||||
|
||||
std::vector<std::unique_ptr<SenseApiLib>> senseApiLibs;
|
||||
|
||||
public:
|
||||
static std::optional<std::string> searchForLibInSmoSearchPaths(
|
||||
const std::string& libraryPath);
|
||||
};
|
||||
|
||||
} // namespace sense_api
|
||||
|
||||
@@ -13,6 +13,15 @@ namespace fs = std::filesystem;
|
||||
namespace smo {
|
||||
namespace sense_api {
|
||||
|
||||
/* Hooks to be provided to senseApiLibs, enabling them to call into Salmanoff
|
||||
* code.
|
||||
*/
|
||||
static SalmanoffCallbacks salmanoffCallbacks =
|
||||
{
|
||||
.searchForLibInSmoSearchPaths = &SenseApiManager
|
||||
::searchForLibInSmoSearchPaths
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Searches for a library in predefined locations
|
||||
* @param libraryPath The name or path of the library to find
|
||||
@@ -27,7 +36,7 @@ namespace sense_api {
|
||||
* 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.)
|
||||
*/
|
||||
static std::optional<std::string> findLibraryPath(
|
||||
static std::optional<std::string> searchForLibInSmoSearchPaths(
|
||||
const std::string& libraryPath)
|
||||
{
|
||||
std::vector<std::string> searchPaths = {
|
||||
@@ -66,9 +75,16 @@ static std::optional<std::string> findLibraryPath(
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<std::string> SenseApiManager::searchForLibInSmoSearchPaths(
|
||||
const std::string& libraryPath)
|
||||
{
|
||||
return ::smo::sense_api::searchForLibInSmoSearchPaths(libraryPath);
|
||||
}
|
||||
|
||||
SenseApiLib& SenseApiManager::loadSenseApiLib(const std::string& libraryPath)
|
||||
{
|
||||
std::optional<std::string> fullPath = findLibraryPath(libraryPath);
|
||||
std::optional<std::string> fullPath = searchForLibInSmoSearchPaths(
|
||||
libraryPath);
|
||||
std::string resolvedPath = fullPath.value_or(libraryPath);
|
||||
|
||||
// Clear any existing error
|
||||
@@ -106,7 +122,7 @@ SenseApiLib& SenseApiManager::loadSenseApiLib(const std::string& libraryPath)
|
||||
+ libraryPath + "'");
|
||||
}
|
||||
|
||||
const SenseApiDesc &libApiDesc = func();
|
||||
const SenseApiDesc &libApiDesc = func(salmanoffCallbacks);
|
||||
auto lib = std::make_unique<SenseApiLib>(
|
||||
libraryPath, dlopen_handle.release(), func);
|
||||
lib->setSenseApiDesc(libApiDesc);
|
||||
|
||||
Reference in New Issue
Block a user