2025-01-08 11:49:28 -04:00
|
|
|
#ifndef __USER_SENSE_API_LIB_H__
|
|
|
|
|
#define __USER_SENSE_API_LIB_H__
|
|
|
|
|
|
2025-01-09 06:03:43 -04:00
|
|
|
#include <stdbool.h>
|
2025-07-24 02:12:31 -04:00
|
|
|
#include <optional>
|
|
|
|
|
#include <string>
|
2025-08-29 09:50:26 -04:00
|
|
|
#include <memory>
|
2025-08-29 15:16:11 -04:00
|
|
|
#include <preprocessor.h>
|
2025-09-04 17:35:49 -04:00
|
|
|
#include <componentThread.h>
|
2025-08-29 15:16:11 -04:00
|
|
|
#include <user/deviceAttachmentSpec.h>
|
2025-01-08 17:16:49 -04:00
|
|
|
|
2025-07-22 06:48:04 -04:00
|
|
|
namespace smo {
|
2025-01-13 21:57:11 -04:00
|
|
|
namespace sense_api {
|
2025-01-08 11:49:28 -04:00
|
|
|
|
2025-09-04 17:35:49 -04:00
|
|
|
/**
|
|
|
|
|
* @brief Threading model descriptor for senseApi libraries.
|
|
|
|
|
*
|
|
|
|
|
* This structure provides senseApi libraries with access to the information and
|
|
|
|
|
* resources they need to operate with SMO's threading model.
|
|
|
|
|
*/
|
|
|
|
|
struct SmoThreadingModelDesc
|
|
|
|
|
{
|
|
|
|
|
/**
|
2025-09-04 17:45:11 -04:00
|
|
|
* @brief sh_ptr to ComponentThread for device-independent state mgt.
|
2025-09-04 17:35:49 -04:00
|
|
|
*
|
|
|
|
|
* This ComponentThread should be used by senseApis for state management
|
|
|
|
|
* that's independent of any particular device or attachment spec.
|
|
|
|
|
* SMO will usually pass in the Marionette thread here.
|
|
|
|
|
*
|
|
|
|
|
* State management that's tied to a particular attachment spec should be
|
|
|
|
|
* done on the ComponentThread for the thread that SMO provided in the
|
|
|
|
|
* attachDeviceReq call.
|
|
|
|
|
*/
|
|
|
|
|
std::shared_ptr<ComponentThread> componentThread;
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-10 06:51:55 -04:00
|
|
|
typedef std::function<void(bool)> sal_mlo_attachDeviceReqCbFn;
|
|
|
|
|
typedef std::function<void(bool)> sal_mlo_detachDeviceReqCbFn;
|
|
|
|
|
|
2025-01-12 09:44:49 -04:00
|
|
|
typedef int (sal_mlo_initializeIndFn)(void);
|
2025-01-09 17:18:24 -04:00
|
|
|
typedef int (sal_mlo_finalizeIndFn)(void);
|
2025-09-10 06:51:55 -04:00
|
|
|
typedef void (sal_mlo_attachDeviceReqFn)(
|
|
|
|
|
const std::shared_ptr<device::DeviceAttachmentSpec>& desc,
|
|
|
|
|
sal_mlo_attachDeviceReqCbFn cb);
|
|
|
|
|
typedef void (sal_mlo_detachDeviceReqFn)(
|
|
|
|
|
const std::shared_ptr<device::DeviceAttachmentSpec>& desc,
|
|
|
|
|
sal_mlo_detachDeviceReqCbFn cb);
|
2025-01-09 17:18:24 -04:00
|
|
|
|
2025-07-24 02:12:31 -04:00
|
|
|
/**
|
|
|
|
|
* @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.
|
|
|
|
|
*/
|
2025-09-04 17:45:11 -04:00
|
|
|
struct SmoCallbacks
|
2025-07-24 02:12:31 -04:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @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);
|
2025-09-09 12:02:40 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Get the current ComponentThread instance
|
|
|
|
|
* @return Shared pointer to the current ComponentThread
|
|
|
|
|
*
|
|
|
|
|
* This function provides access to the current ComponentThread instance,
|
|
|
|
|
* equivalent to calling ComponentThread::getSelf().
|
|
|
|
|
*/
|
|
|
|
|
std::shared_ptr<ComponentThread> (*ComponentThread_getSelf)(void);
|
2025-07-24 02:12:31 -04:00
|
|
|
};
|
|
|
|
|
|
2025-01-13 21:57:11 -04:00
|
|
|
struct Sal_Mgmt_LibOps
|
2025-01-09 06:03:43 -04:00
|
|
|
{
|
2025-07-22 06:48:04 -04:00
|
|
|
/* When Salmanoff loads a sense API lib, it calls this function to initialize
|
2025-01-09 06:03:43 -04:00
|
|
|
* the lib. When this returns, the lib should be ready to attach devices.
|
|
|
|
|
*/
|
2025-01-09 17:18:24 -04:00
|
|
|
sal_mlo_initializeIndFn *initializeInd;
|
2025-07-22 06:48:04 -04:00
|
|
|
/* Salmanoff calls this to finalize the lib and free its internal
|
2025-01-09 06:03:43 -04:00
|
|
|
* resources. When this returns, the lib should be ready to be unloaded.
|
|
|
|
|
*/
|
2025-01-09 17:18:24 -04:00
|
|
|
sal_mlo_finalizeIndFn *finalizeInd;
|
2025-07-22 06:48:04 -04:00
|
|
|
/* Salmanoff calls this to attach a device to the lib. When it returns, the
|
2025-01-09 06:03:43 -04:00
|
|
|
* device should be attached and ready to be implexed.
|
|
|
|
|
*/
|
2025-01-09 17:18:24 -04:00
|
|
|
sal_mlo_attachDeviceReqFn *attachDeviceReq;
|
2025-01-09 06:03:43 -04:00
|
|
|
// When this returns, the device should be detached.
|
2025-01-09 17:18:24 -04:00
|
|
|
sal_mlo_detachDeviceReqFn *detachDeviceReq;
|
2025-01-09 06:03:43 -04:00
|
|
|
|
2025-01-13 21:57:11 -04:00
|
|
|
static bool sanityCheck(const Sal_Mgmt_LibOps &ops)
|
|
|
|
|
{
|
|
|
|
|
if (!ops.initializeInd || !ops.finalizeInd
|
|
|
|
|
|| !ops.attachDeviceReq || !ops.detachDeviceReq)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-01-08 11:49:28 -04:00
|
|
|
};
|
|
|
|
|
|
2025-07-22 06:48:04 -04:00
|
|
|
/* Exported by all sense API Libraries to tell Salmanoff what API the lib uses
|
2025-01-18 10:16:02 -04:00
|
|
|
* to connect to providers; and also to state which implexor APIs it exports.
|
2025-01-13 21:57:11 -04:00
|
|
|
*/
|
|
|
|
|
class SenseApiDesc
|
2025-01-09 06:03:43 -04:00
|
|
|
{
|
2025-01-13 21:57:11 -04:00
|
|
|
public:
|
|
|
|
|
class ExportedImplexorApiDesc
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static bool sanityCheck(const ExportedImplexorApiDesc &desc)
|
|
|
|
|
{
|
|
|
|
|
if (desc.name.empty()) { return false; }
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
std::string name;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
std::string stringify() const
|
2025-01-09 06:03:43 -04:00
|
|
|
{
|
2025-01-13 21:57:11 -04:00
|
|
|
std::string result = "Name: " + name + "\n";
|
|
|
|
|
result += "Exported Implexor APIs:\n";
|
|
|
|
|
for (const auto& api : exportedImplexorApis) {
|
|
|
|
|
result += " - " + api.name + "\n";
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2025-01-09 06:03:43 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-13 21:57:11 -04:00
|
|
|
static bool sanityCheck(const SenseApiDesc &desc)
|
2025-01-09 06:03:43 -04:00
|
|
|
{
|
2025-01-13 21:57:11 -04:00
|
|
|
if (desc.name.empty() || desc.exportedImplexorApis.empty()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-01-09 06:03:43 -04:00
|
|
|
|
2025-01-13 21:57:11 -04:00
|
|
|
for (const auto& api : desc.exportedImplexorApis) {
|
|
|
|
|
if (!ExportedImplexorApiDesc::sanityCheck(api)) { return false; }
|
|
|
|
|
}
|
2025-01-09 06:03:43 -04:00
|
|
|
|
2025-01-13 21:57:11 -04:00
|
|
|
return Sal_Mgmt_LibOps::sanityCheck(desc.sal_mgmt_libOps);
|
2025-01-09 06:03:43 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-13 21:57:11 -04:00
|
|
|
std::string name;
|
|
|
|
|
// These are the implexors whose APIs this lib exports.
|
|
|
|
|
std::vector<ExportedImplexorApiDesc> exportedImplexorApis;
|
|
|
|
|
Sal_Mgmt_LibOps sal_mgmt_libOps;
|
|
|
|
|
};
|
2025-01-09 06:03:43 -04:00
|
|
|
|
2025-01-18 10:16:02 -04:00
|
|
|
|
2025-07-22 06:48:04 -04:00
|
|
|
#define SMO_GET_SENSE_API_DESC_FN_NAME getSenseApiDesc
|
|
|
|
|
#define SMO_GET_SENSE_API_DESC_FN_NAME_STR \
|
|
|
|
|
SMO_QUOTE(SMO_GET_SENSE_API_DESC_FN_NAME)
|
|
|
|
|
#define SMO_GET_SENSE_API_DESC_FN_TYPEDEF \
|
|
|
|
|
SMO_CONCAT(SMO_GET_SENSE_API_DESC_FN_NAME, Fn)
|
2025-01-08 17:16:49 -04:00
|
|
|
|
2025-01-18 10:16:02 -04:00
|
|
|
/* Every Sense API library must define a global instance of this
|
2025-07-22 06:48:04 -04:00
|
|
|
* function. Salmanoff will search for it and invoke it via dlsym().
|
2025-01-18 10:16:02 -04:00
|
|
|
*
|
2025-07-22 06:48:04 -04:00
|
|
|
* The function must return a SenseApiDesc struct that Smo will tell
|
|
|
|
|
* 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.
|
2025-07-24 02:12:31 -04:00
|
|
|
*
|
2025-09-04 17:45:11 -04:00
|
|
|
* The SmoCallbacks parameter provides the library with access to
|
2025-07-24 02:12:31 -04:00
|
|
|
* Salmanoff's hooks.
|
2025-09-04 17:35:49 -04:00
|
|
|
* The SmoThreadingModelDesc parameter provides the library with access to
|
|
|
|
|
* the io_service for network operations and event handling.
|
2025-01-18 10:16:02 -04:00
|
|
|
*/
|
2025-07-24 02:12:31 -04:00
|
|
|
typedef const SenseApiDesc &(SMO_GET_SENSE_API_DESC_FN_TYPEDEF)(
|
2025-09-04 17:45:11 -04:00
|
|
|
const SmoCallbacks& callbacks,
|
2025-09-04 17:35:49 -04:00
|
|
|
const SmoThreadingModelDesc& threadingModel);
|
2025-01-08 17:16:49 -04:00
|
|
|
|
2025-01-13 21:57:11 -04:00
|
|
|
} // namespace sense_api
|
2025-07-22 06:48:04 -04:00
|
|
|
} // namespace smo
|
2025-01-08 17:16:49 -04:00
|
|
|
|
2025-01-08 11:49:28 -04:00
|
|
|
#endif // __USER_SENSE_API_LIB_H__
|