18#include <peak/dll_interface/peak_dll_interface_util.hpp>
19#include <peak/generic/peak_t_callback_manager.hpp>
22#include <unordered_map>
38struct DeviceFoundCallbackContainer;
48 ,
public std::enable_shared_from_this<Interface>
52 using DeviceFoundCallback = std::function<void(
const std::shared_ptr<DeviceDescriptor>& foundDevice)>;
81 std::string
Key()
const;
86 std::string
ID()
const;
90 std::string
TLType()
const;
117 std::vector<std::shared_ptr<DeviceDescriptor>>
Devices()
const;
272 struct DeviceFoundCallbackContainer
274 std::weak_ptr<Interface> _Interface;
277 struct DeviceDisconnectedCallbackContainer
279 std::weak_ptr<Interface> _Interface;
282 struct DeviceReconnectedCallbackContainer
284 std::weak_ptr<Interface> _Interface;
290 const char* lostDeviceId,
size_t lostDeviceIdSize,
void* context);
295 static void PEAK_CALL_CONV DeviceListChangedCallbackCWrapper(
void* context);
302 void InitializeUpdateMechanismIfNecessary();
304 friend ClassCreator<Interface>;
308 std::weak_ptr<System> m_parentSystem;
310 std::unique_ptr<TCallbackManager<PEAK_DEVICE_FOUND_CALLBACK_HANDLE, DeviceFoundCallbackContainer>>
311 m_deviceFoundCallbackManager;
312 std::unique_ptr<TCallbackManager<PEAK_DEVICE_LOST_CALLBACK_HANDLE, DeviceLostCallback>>
313 m_deviceLostCallbackManager;
315 std::unique_ptr<TCallbackManager<PEAK_DEVICE_DISCONNECTED_CALLBACK_HANDLE, DeviceDisconnectedCallbackContainer>>
316 m_deviceDisconnectedCallbackManager;
317 std::unique_ptr<TCallbackManager<PEAK_DEVICE_RECONNECTED_CALLBACK_HANDLE, DeviceReconnectedCallbackContainer>>
318 m_deviceReconnectedCallbackManager;
320 std::unique_ptr<TCallbackManager<PEAK_DEVICE_LIST_CHANGED_CALLBACK_HANDLE, DeviceListChangedCallback>>
321 m_deviceListChangedCallbackManager;
323 std::vector<std::shared_ptr<DeviceDescriptor>> m_devices;
324 std::unordered_map<std::string, std::shared_ptr<DeviceDescriptor>> m_devicesByKey;
325 std::unordered_map<std::string, std::string> m_devicesKeyById;
326 mutable std::mutex m_devicesMutex;
327 std::once_flag m_updateMechanismInitializedFlag;
342 : m_backendHandle(interfaceHandle)
343 , m_parentSystem(parentSystem)
344 , m_key(QueryStringFromCInterfaceFunction([&](char* key, size_t* keySize) {
348 m_deviceFoundCallbackManager =
349 std::make_unique<TCallbackManager<PEAK_DEVICE_FOUND_CALLBACK_HANDLE, DeviceFoundCallbackContainer>>(
350 [&](
void* callbackContext) {
351 return QueryNumericFromCInterfaceFunction<PEAK_DEVICE_FOUND_CALLBACK_HANDLE>(
354 m_backendHandle, DeviceFoundCallbackCWrapper, callbackContext, deviceFoundCallbackHandle);
358 CallAndCheckCInterfaceFunction([&] {
360 m_backendHandle, callbackHandle);
364 m_deviceLostCallbackManager =
365 std::make_unique<TCallbackManager<PEAK_DEVICE_LOST_CALLBACK_HANDLE, DeviceLostCallback>>(
366 [&](
void* callbackContext) {
367 return QueryNumericFromCInterfaceFunction<PEAK_DEVICE_LOST_CALLBACK_HANDLE>(
370 m_backendHandle, DeviceLostCallbackCWrapper, callbackContext, deviceLostCallbackHandle);
374 CallAndCheckCInterfaceFunction([&] {
376 m_backendHandle, callbackHandle);
380 m_deviceDisconnectedCallbackManager = std::make_unique<
381 TCallbackManager<PEAK_DEVICE_DISCONNECTED_CALLBACK_HANDLE, DeviceDisconnectedCallbackContainer>>(
382 [&](
void* callbackContext) {
383 return QueryNumericFromCInterfaceFunction<PEAK_DEVICE_DISCONNECTED_CALLBACK_HANDLE>(
386 DeviceDisconnectedCallbackCWrapper, callbackContext, deviceDisconnectedCallbackHandle);
390 CallAndCheckCInterfaceFunction([&] {
392 m_backendHandle, callbackHandle);
396 m_deviceReconnectedCallbackManager = std::make_unique<
397 TCallbackManager<PEAK_DEVICE_RECONNECTED_CALLBACK_HANDLE, DeviceReconnectedCallbackContainer>>(
398 [&](
void* callbackContext) {
399 return QueryNumericFromCInterfaceFunction<PEAK_DEVICE_RECONNECTED_CALLBACK_HANDLE>(
402 DeviceReconnectedCallbackCWrapper, callbackContext, deviceReconnectedCallbackHandle);
406 CallAndCheckCInterfaceFunction([&] {
408 m_backendHandle, callbackHandle);
412 m_deviceListChangedCallbackManager =
413 std::make_unique<TCallbackManager<PEAK_DEVICE_LIST_CHANGED_CALLBACK_HANDLE, DeviceListChangedCallback>>(
414 [&](
void* callbackContext) {
415 return QueryNumericFromCInterfaceFunction<PEAK_DEVICE_LIST_CHANGED_CALLBACK_HANDLE>(
418 DeviceListChangedCallbackCWrapper, callbackContext, deviceListChangedCallbackHandle);
422 CallAndCheckCInterfaceFunction([&] {
424 m_backendHandle, callbackHandle);
433 m_deviceFoundCallbackManager->UnregisterAllCallbacks();
434 m_deviceLostCallbackManager->UnregisterAllCallbacks();
436 m_deviceDisconnectedCallbackManager->UnregisterAllCallbacks();
437 m_deviceReconnectedCallbackManager->UnregisterAllCallbacks();
439 m_deviceListChangedCallbackManager->UnregisterAllCallbacks();
454 return QueryRawInformationFromCInterfaceFunction([&](int32_t* dataType, uint8_t* buffer,
size_t* bufferSize) {
456 m_backendHandle, infoCommand, dataType, buffer, bufferSize);
462 return QueryStringFromCInterfaceFunction([&](
char*
id,
size_t* idSize) {
469 return QueryStringFromCInterfaceFunction([&](
char* displayName,
size_t* displayNameSize) {
476 return QueryStringFromCInterfaceFunction([&](
char* tlType,
size_t* tlTypeSize) {
483 InitializeUpdateMechanismIfNecessary();
485 CallAndCheckCInterfaceFunction(
491 std::lock_guard<std::mutex> lock(m_devicesMutex);
497 return LockOrThrow(m_parentSystem);
504 m_deviceFoundCallbackManager->RegisterCallback(DeviceFoundCallbackContainer{ shared_from_this(), callback }));
509 m_deviceFoundCallbackManager->UnregisterCallback(
517 DeviceReconnectedCallbackContainer{ shared_from_this(), callback }));
522 m_deviceReconnectedCallbackManager->UnregisterCallback(
530 DeviceDisconnectedCallbackContainer{ shared_from_this(), callback }));
535 m_deviceDisconnectedCallbackManager->UnregisterCallback(
547 m_deviceLostCallbackManager->UnregisterCallback(
559 m_deviceListChangedCallbackManager->UnregisterCallback(
566 auto callbackContainer =
static_cast<DeviceFoundCallbackContainer*
>(context);
568 auto interfaceDescriptor = callbackContainer->_Interface.lock()->GetOrAddFoundDevice(foundDevice);
570 callbackContainer->Callback(interfaceDescriptor);
574 const char* lostDeviceId,
size_t lostDeviceIdSize,
void* context)
578 callback->operator()(std::string(lostDeviceId, lostDeviceIdSize - 1));
581inline void PEAK_CALL_CONV Interface::DeviceDisconnectedCallbackCWrapper(
584 auto callbackContainer =
static_cast<DeviceDisconnectedCallbackContainer*
>(context);
586 auto deviceDescriptor = callbackContainer->_Interface.lock()->GetOrAddFoundDevice(foundDevice);
588 callbackContainer->Callback(deviceDescriptor);
594 auto callbackContainer =
static_cast<DeviceReconnectedCallbackContainer*
>(context);
596 auto deviceDescriptor = callbackContainer->_Interface.lock()->GetOrAddFoundDevice(foundDevice);
598 callbackContainer->Callback(deviceDescriptor, DeviceReconnectInformation(std::move(reconnectInformation)));
601inline void PEAK_CALL_CONV Interface::DeviceListChangedCallbackCWrapper(
void* context)
604 callback->operator()();
609 std::lock_guard<std::mutex> lock(m_devicesMutex);
611 const auto deviceKey = QueryStringFromCInterfaceFunction([&](
char* key,
size_t* keySize) {
614 auto keyDevicePairIterator = m_devicesByKey.find(deviceKey);
615 if (keyDevicePairIterator == m_devicesByKey.end())
617 auto deviceDescriptor = std::static_pointer_cast<DeviceDescriptor>(
618 std::make_shared<ClassCreator<DeviceDescriptor>>(
619 foundDevice, std::weak_ptr<Interface>{ shared_from_this() }));
620 m_devices.emplace_back(deviceDescriptor);
621 m_devicesByKey.emplace(deviceKey, deviceDescriptor);
622 m_devicesKeyById.emplace(deviceDescriptor->ID(), deviceKey);
624 return deviceDescriptor;
627 return keyDevicePairIterator->second;
632 auto moduleHandle = QueryNumericFromCInterfaceFunction<PEAK_MODULE_HANDLE>(
642 auto eventSupportingModuleHandle = QueryNumericFromCInterfaceFunction<PEAK_EVENT_SUPPORTING_MODULE_HANDLE>(
645 m_backendHandle, _eventSupportingModuleHandle);
648 return eventSupportingModuleHandle;
651inline void Interface::InitializeUpdateMechanismIfNecessary()
653 std::call_once(m_updateMechanismInitializedFlag, [&] {
660 std::lock_guard<std::mutex> lock(m_devicesMutex);
661 const auto lostDeviceKey = m_devicesKeyById.at(lostDeviceId);
662 m_devices.erase(std::remove_if(std::begin(m_devices), std::end(m_devices),
663 [lostDeviceKey](
const std::shared_ptr<DeviceDescriptor>& device) {
664 return device->Key() == lostDeviceKey;
666 std::end(m_devices));
667 m_devicesByKey.erase(lostDeviceKey);
668 m_devicesKeyById.erase(lostDeviceId);
The base class for all modules being able to raise events.
Definition peak_event_supporting_module.hpp:34
The base class for all exceptions thrown by the library.
Definition peak_exception.hpp:26
Represents a GenTL Interface module.
Definition peak_interface.hpp:49
std::function< void()> DeviceListChangedCallback
Definition peak_interface.hpp:69
~Interface() override
Definition peak_interface.hpp:429
std::shared_ptr< System > ParentSystem() const
Returns the parent system.
Definition peak_interface.hpp:495
std::string TLType() const
Returns the TL (transport layer) type.
Definition peak_interface.hpp:474
RawInformation Info(int32_t infoCommand) const
Delivers information based on the given GenTL info command.
Definition peak_interface.hpp:452
Interface(Interface &&other)=delete
Interface(const Interface &other)=delete
Interface & operator=(const Interface &other)=delete
DeviceReconnectedCallback * DeviceReconnectedCallbackHandle
Definition peak_interface.hpp:63
void UpdateDevices(Timeout timeout_ms)
Searches for devices.
Definition peak_interface.hpp:481
void UnregisterDeviceReconnectedCallback(DeviceReconnectedCallbackHandle callbackHandle)
Unregisters a device reconnected callback.
Definition peak_interface.hpp:520
void UnregisterDeviceListChangedCallback(DeviceListChangedCallbackHandle callbackHandle)
Unregisters a device list changed callback.
Definition peak_interface.hpp:557
DeviceDisconnectedCallbackHandle RegisterDeviceDisconnectedCallback(const DeviceDisconnectedCallback &callback)
Registers a callback for signaling a disconnected device.
Definition peak_interface.hpp:526
DeviceReconnectedCallbackHandle RegisterDeviceReconnectedCallback(const DeviceReconnectedCallback &callback)
Registers a callback for signaling a reconnected device.
Definition peak_interface.hpp:513
std::string ID() const
Returns the ID.
Definition peak_interface.hpp:460
DeviceListChangedCallbackHandle RegisterDeviceListChangedCallback(const DeviceListChangedCallback &callback)
Registers a callback for signaling a device list changed.
Definition peak_interface.hpp:551
void UnregisterDeviceDisconnectedCallback(DeviceDisconnectedCallbackHandle callbackHandle)
Unregisters a device disconnected callback.
Definition peak_interface.hpp:533
std::function< void(const std::shared_ptr< DeviceDescriptor > &foundDevice)> DeviceFoundCallback
Definition peak_interface.hpp:52
DeviceListChangedCallback * DeviceListChangedCallbackHandle
Definition peak_interface.hpp:71
std::vector< std::shared_ptr< DeviceDescriptor > > Devices() const
Returns the device list.
Definition peak_interface.hpp:489
std::string DisplayName() const
Returns the display name.
Definition peak_interface.hpp:467
DeviceLostCallbackHandle RegisterDeviceLostCallback(const DeviceLostCallback &callback)
Registers a callback for signaling a lost device.
Definition peak_interface.hpp:539
DeviceLostCallback * DeviceLostCallbackHandle
Definition peak_interface.hpp:67
Interface & operator=(Interface &&other)=delete
DeviceFoundCallback * DeviceFoundCallbackHandle
Definition peak_interface.hpp:54
void UnregisterDeviceFoundCallback(DeviceFoundCallbackHandle callbackHandle)
Unregisters a device found callback.
Definition peak_interface.hpp:507
std::function< void(const std::shared_ptr< DeviceDescriptor > &reconnectedDevice, DeviceReconnectInformation reconnectInformation)> DeviceReconnectedCallback
Definition peak_interface.hpp:61
void UnregisterDeviceLostCallback(DeviceLostCallbackHandle callbackHandle)
Unregisters a device lost callback.
Definition peak_interface.hpp:545
DeviceFoundCallbackHandle RegisterDeviceFoundCallback(const DeviceFoundCallback &callback)
Registers a callback for signaling a found device.
Definition peak_interface.hpp:500
std::string Key() const
Returns the unique key.
Definition peak_interface.hpp:447
DeviceDisconnectedCallback * DeviceDisconnectedCallbackHandle
Definition peak_interface.hpp:58
std::function< void(const std::string &lostDeviceId)> DeviceLostCallback
Definition peak_interface.hpp:65
std::function< void(const std::shared_ptr< DeviceDescriptor > &disconnectedDevice)> DeviceDisconnectedCallback
Definition peak_interface.hpp:56
Represents a timeout value in milliseconds.
Definition peak_timeout.hpp:31
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_dynamic_loader.h:33
PEAK_C_API PEAK_DeviceDescriptor_GetKey(PEAK_DEVICE_DESCRIPTOR_HANDLE deviceDescriptorHandle, char *key, size_t *keySize)
Asks the given device descriptor for its key.
struct PEAK_MODULE * PEAK_MODULE_HANDLE
Definition peak_backend.h:404
PEAK_C_API PEAK_Interface_ToEventSupportingModule(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_EVENT_SUPPORTING_MODULE_HANDLE *eventSupportingModuleHandle)
Casts the given interface to an event-supporting module.
PEAK_C_API PEAK_Interface_GetInfo(PEAK_INTERFACE_HANDLE interfaceHandle, int32_t infoCommand, int32_t *infoDataType, uint8_t *info, size_t *infoSize)
Asks the given interface for an information based on a specific command.
PEAK_C_API PEAK_Interface_UnregisterDeviceFoundCallback(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_DEVICE_FOUND_CALLBACK_HANDLE callbackHandle)
Unregisters the device-found callback with the given handle from the given interface.
PEAK_C_API PEAK_Interface_ToModule(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_MODULE_HANDLE *moduleHandle)
Casts the given interface to a module.
struct PEAK_INTERFACE * PEAK_INTERFACE_HANDLE
Definition peak_backend.h:364
PEAK_C_API PEAK_Interface_GetTLType(PEAK_INTERFACE_HANDLE interfaceHandle, char *tlType, size_t *tlTypeSize)
Asks the given interface for its TL type.
PEAK_DEVICE_DISCONNETED_CALLBACK * PEAK_DEVICE_DISCONNECTED_CALLBACK_HANDLE
Definition peak_backend.h:549
PEAK_C_API PEAK_Interface_UnregisterDeviceListChangedCallback(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_DEVICE_LIST_CHANGED_CALLBACK_HANDLE callbackHandle)
Unregisters the device-list-changed callback with the given handle from the given interface.
struct PEAK_EVENT_SUPPORTING_MODULE * PEAK_EVENT_SUPPORTING_MODULE_HANDLE
Definition peak_backend.h:460
PEAK_C_API PEAK_Interface_RegisterDeviceLostCallback(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_DEVICE_LOST_CALLBACK callback, void *callbackContext, PEAK_DEVICE_LOST_CALLBACK_HANDLE *callbackHandle)
Registers a callback signaling a lost device at the given interface.
PEAK_DEVICE_LOST_CALLBACK * PEAK_DEVICE_LOST_CALLBACK_HANDLE
Definition peak_backend.h:551
PEAK_DEVICE_RECONNECTED_CALLBACK * PEAK_DEVICE_RECONNECTED_CALLBACK_HANDLE
Definition peak_backend.h:547
PEAK_C_API PEAK_Interface_UnregisterDeviceDisconnectedCallback(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_DEVICE_DISCONNECTED_CALLBACK_HANDLE callbackHandle)
Unregisters the device disconnected callback with the given handle from the given interface.
PEAK_DEVICE_FOUND_CALLBACK * PEAK_DEVICE_FOUND_CALLBACK_HANDLE
Definition peak_backend.h:545
PEAK_DEVICE_LIST_CHANGED_CALLBACK * PEAK_DEVICE_LIST_CHANGED_CALLBACK_HANDLE
Definition peak_backend.h:553
PEAK_C_API PEAK_Interface_RegisterDeviceFoundCallback(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_DEVICE_FOUND_CALLBACK callback, void *callbackContext, PEAK_DEVICE_FOUND_CALLBACK_HANDLE *callbackHandle)
Registers a callback signaling a found device at the given interface.
PEAK_C_API PEAK_Interface_RegisterDeviceDisconnectedCallback(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_DEVICE_DISCONNETED_CALLBACK callback, void *callbackContext, PEAK_DEVICE_DISCONNECTED_CALLBACK_HANDLE *callbackHandle)
Registers a callback signaling a disconnected device that is now waiting to be reconnected at the giv...
PEAK_C_API PEAK_Interface_RegisterDeviceReconnectedCallback(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_DEVICE_RECONNECTED_CALLBACK callback, void *callbackContext, PEAK_DEVICE_RECONNECTED_CALLBACK_HANDLE *callbackHandle)
Registers a callback signaling a reconnected device at the given interface.
PEAK_C_API PEAK_Interface_UnregisterDeviceLostCallback(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_DEVICE_LOST_CALLBACK_HANDLE callbackHandle)
Unregisters the device-lost callback with the given handle from the given interface.
PEAK_C_API PEAK_Interface_RegisterDeviceListChangedCallback(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_DEVICE_LIST_CHANGED_CALLBACK callback, void *callbackContext, PEAK_DEVICE_LIST_CHANGED_CALLBACK_HANDLE *callbackHandle)
Registers a callback signaling a change in the device list at the given interface.
PEAK_C_API PEAK_Interface_Destruct(PEAK_INTERFACE_HANDLE interfaceHandle)
Destroys the given interface.
PEAK_C_API PEAK_Interface_UnregisterDeviceReconnectedCallback(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_DEVICE_RECONNECTED_CALLBACK_HANDLE callbackHandle)
Unregisters the device-reconnected callback with the given handle from the given interface.
PEAK_C_API PEAK_Interface_GetDisplayName(PEAK_INTERFACE_HANDLE interfaceHandle, char *displayName, size_t *displayNameSize)
Asks the given interface for its display name.
struct PEAK_DEVICE_DESCRIPTOR * PEAK_DEVICE_DESCRIPTOR_HANDLE
Definition peak_backend.h:368
PEAK_C_API PEAK_Interface_GetKey(PEAK_INTERFACE_HANDLE interfaceHandle, char *key, size_t *keySize)
Asks the given interface for its key.
PEAK_C_API PEAK_Interface_GetID(PEAK_INTERFACE_HANDLE interfaceHandle, char *id, size_t *idSize)
Asks the given interface for its ID.
PEAK_C_API PEAK_Interface_UpdateDevices(PEAK_INTERFACE_HANDLE interfaceHandle, uint64_t timeout_ms)
Tells the given interface to update its devices with the given timeout.
#define PEAK_C_ABI_PREFIX
Definition peak_dll_defines.h:16
#define PEAK_CALL_CONV
Definition peak_dynamic_loader.h:30
Definition peak_backend.h:515