18 #include <peak/dll_interface/peak_dll_interface_util.hpp> 19 #include <peak/generic/peak_t_callback_manager.hpp> 21 #include <unordered_map> 37 struct DeviceFoundCallbackContainer;
47 ,
public std::enable_shared_from_this<Interface>
51 using DeviceFoundCallback = std::function<void(const std::shared_ptr<DeviceDescriptor>& foundDevice)>;
67 std::string
Key()
const;
72 std::string
ID()
const;
76 std::string
TLType()
const;
102 std::vector<std::shared_ptr<DeviceDescriptor>>
Devices()
const;
162 struct DeviceFoundCallbackContainer
164 std::weak_ptr<Interface> _Interface;
170 const char* lostDeviceId,
size_t lostDeviceIdSize,
void* context);
176 void InitializeUpdateMechanismIfNecessary();
178 friend ClassCreator<Interface>;
182 std::weak_ptr<System> m_parentSystem;
184 std::unique_ptr<TCallbackManager<PEAK_DEVICE_FOUND_CALLBACK_HANDLE, DeviceFoundCallbackContainer>>
185 m_deviceFoundCallbackManager;
186 std::unique_ptr<TCallbackManager<PEAK_DEVICE_LOST_CALLBACK_HANDLE, DeviceLostCallback>>
187 m_deviceLostCallbackManager;
189 std::vector<std::shared_ptr<DeviceDescriptor>> m_devices;
190 std::unordered_map<std::string, std::shared_ptr<DeviceDescriptor>> m_devicesByKey;
191 std::unordered_map<std::string, std::string> m_devicesKeyById;
192 mutable std::mutex m_devicesMutex;
193 std::once_flag m_updateMechanismInitializedFlag;
208 : m_backendHandle(interfaceHandle)
209 , m_parentSystem(parentSystem)
210 , m_deviceFoundCallbackManager()
211 , m_deviceLostCallbackManager()
212 , m_key(QueryStringFromCInterfaceFunction([&](char* key, size_t* keySize) {
216 m_deviceFoundCallbackManager =
217 std::make_unique<TCallbackManager<PEAK_DEVICE_FOUND_CALLBACK_HANDLE, DeviceFoundCallbackContainer>>(
218 [&](
void* callbackContext) {
219 return QueryNumericFromCInterfaceFunction<PEAK_DEVICE_FOUND_CALLBACK_HANDLE>(
222 m_backendHandle, DeviceFoundCallbackCWrapper, callbackContext, deviceFoundCallbackHandle);
226 CallAndCheckCInterfaceFunction([&] {
228 m_backendHandle, callbackHandle);
232 m_deviceLostCallbackManager =
233 std::make_unique<TCallbackManager<PEAK_DEVICE_LOST_CALLBACK_HANDLE, DeviceLostCallback>>(
234 [&](
void* callbackContext) {
235 return QueryNumericFromCInterfaceFunction<PEAK_DEVICE_LOST_CALLBACK_HANDLE>(
238 m_backendHandle, DeviceLostCallbackCWrapper, callbackContext, deviceLostCallbackHandle);
242 CallAndCheckCInterfaceFunction([&] {
244 m_backendHandle, callbackHandle);
253 m_deviceFoundCallbackManager->UnregisterAllCallbacks();
254 m_deviceLostCallbackManager->UnregisterAllCallbacks();
269 return QueryRawInformationFromCInterfaceFunction([&](int32_t* dataType, uint8_t* buffer,
size_t* bufferSize) {
271 m_backendHandle, infoCommand, dataType, buffer, bufferSize);
277 return QueryStringFromCInterfaceFunction([&](
char*
id,
size_t* idSize) {
284 return QueryStringFromCInterfaceFunction([&](
char* displayName,
size_t* displayNameSize) {
291 return QueryStringFromCInterfaceFunction([&](
char* tlType,
size_t* tlTypeSize) {
298 InitializeUpdateMechanismIfNecessary();
300 CallAndCheckCInterfaceFunction(
306 std::lock_guard<std::mutex> lock(m_devicesMutex);
312 return LockOrThrow(m_parentSystem);
319 m_deviceFoundCallbackManager->RegisterCallback(DeviceFoundCallbackContainer{ shared_from_this(), callback }));
324 m_deviceFoundCallbackManager->UnregisterCallback(
325 reinterpret_cast<PEAK_DEVICE_FOUND_CALLBACK_HANDLE>(callbackHandle));
336 m_deviceLostCallbackManager->UnregisterCallback(
337 reinterpret_cast<PEAK_DEVICE_LOST_CALLBACK_HANDLE>(callbackHandle));
340 inline void PEAK_CALL_CONV Interface::DeviceFoundCallbackCWrapper(
343 auto callbackContainer =
static_cast<DeviceFoundCallbackContainer*
>(context);
345 auto interfaceDescriptor = callbackContainer->_Interface.lock()->GetOrAddFoundDevice(foundDevice);
347 callbackContainer->Callback(interfaceDescriptor);
351 const char* lostDeviceId,
size_t lostDeviceIdSize,
void* context)
355 callback->operator()(std::string(lostDeviceId, lostDeviceIdSize - 1));
360 std::lock_guard<std::mutex> lock(m_devicesMutex);
362 const auto deviceKey = QueryStringFromCInterfaceFunction([&](
char* key,
size_t* keySize) {
365 auto keyDevicePairIterator = m_devicesByKey.find(deviceKey);
366 if (keyDevicePairIterator == m_devicesByKey.end())
369 std::make_shared<ClassCreator<DeviceDescriptor>>(
370 foundDevice, std::weak_ptr<Interface>{ shared_from_this() }));
371 m_devices.emplace_back(deviceDescriptor);
372 m_devicesByKey.emplace(deviceKey, deviceDescriptor);
373 m_devicesKeyById.emplace(deviceDescriptor->ID(), deviceKey);
375 return deviceDescriptor;
378 return keyDevicePairIterator->second;
383 auto moduleHandle = QueryNumericFromCInterfaceFunction<PEAK_MODULE_HANDLE>(
393 auto eventSupportingModuleHandle = QueryNumericFromCInterfaceFunction<PEAK_EVENT_SUPPORTING_MODULE_HANDLE>(
396 m_backendHandle, _eventSupportingModuleHandle);
399 return eventSupportingModuleHandle;
402 inline void Interface::InitializeUpdateMechanismIfNecessary()
404 std::call_once(m_updateMechanismInitializedFlag, [&] {
409 std::lock_guard<std::mutex> lock(m_devicesMutex);
410 const auto lostDeviceKey = m_devicesKeyById.at(lostDeviceId);
411 m_devices.erase(std::remove_if(std::begin(m_devices), std::end(m_devices),
412 [lostDeviceKey](
const std::shared_ptr<DeviceDescriptor>& device) {
413 return device->Key() == lostDeviceKey;
415 std::end(m_devices));
416 m_devicesByKey.erase(lostDeviceKey);
417 m_devicesKeyById.erase(lostDeviceId);
DeviceLostCallback * DeviceLostCallbackHandle
Definition: peak_interface.hpp:57
std::function< void(const std::shared_ptr< DeviceDescriptor > &foundDevice)> DeviceFoundCallback
Definition: peak_interface.hpp:51
std::vector< std::shared_ptr< DeviceDescriptor > > Devices() const
Returns the device list.
Definition: peak_interface.hpp:304
Encapsulates the GenTL functions associated with a GenTL Device module's ID.
Definition: peak_device_descriptor.hpp:102
std::function< void(const std::string &lostDeviceId)> DeviceLostCallback
Definition: peak_interface.hpp:55
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_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_C_API PEAK_Interface_GetTLType(PEAK_INTERFACE_HANDLE interfaceHandle, char *tlType, size_t *tlTypeSize)
Asks the given interface for its TL type.
PEAK_C_API PEAK_Interface_GetDisplayName(PEAK_INTERFACE_HANDLE interfaceHandle, char *displayName, size_t *displayNameSize)
Asks the given interface for its display name.
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition: peak_dynamic_loader.h:32
struct PEAK_EVENT_SUPPORTING_MODULE * PEAK_EVENT_SUPPORTING_MODULE_HANDLE
Definition: peak_backend.h:457
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_GetKey(PEAK_INTERFACE_HANDLE interfaceHandle, char *key, size_t *keySize)
Asks the given interface for its key.
PEAK_C_API PEAK_Interface_ToModule(PEAK_INTERFACE_HANDLE interfaceHandle, PEAK_MODULE_HANDLE *moduleHandle)
Casts the given interface to a module.
DeviceFoundCallbackHandle RegisterDeviceFoundCallback(const DeviceFoundCallback &callback)
Registers a callback for signaling a found device.
Definition: peak_interface.hpp:315
void UpdateDevices(Timeout timeout_ms)
Searches for devices.
Definition: peak_interface.hpp:296
Represents a GenTL Interface module.
Definition: peak_interface.hpp:45
~Interface() override
Definition: peak_interface.hpp:249
PEAK_DEVICE_FOUND_CALLBACK * PEAK_DEVICE_FOUND_CALLBACK_HANDLE
Definition: peak_backend.h:498
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.
void UnregisterDeviceLostCallback(DeviceLostCallbackHandle callbackHandle)
Unregisters a device lost callback.
Definition: peak_interface.hpp:334
std::string DisplayName() const
Returns the display name.
Definition: peak_interface.hpp:282
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.
std::string Key() const
Returns the unique key.
Definition: peak_interface.hpp:262
void UnregisterDeviceFoundCallback(DeviceFoundCallbackHandle callbackHandle)
Unregisters a device found callback.
Definition: peak_interface.hpp:322
RawInformation Info(int32_t infoCommand) const
Delivers information based on the given GenTL info command.
Definition: peak_interface.hpp:267
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.
Represents a timeout value in milliseconds.
Definition: peak_timeout.hpp:30
PEAK_C_API PEAK_Interface_Destruct(PEAK_INTERFACE_HANDLE interfaceHandle)
Destroys the given interface.
The base class for all exceptions thrown by the library.
Definition: peak_exception.hpp:25
struct PEAK_DEVICE_DESCRIPTOR * PEAK_DEVICE_DESCRIPTOR_HANDLE
Definition: peak_backend.h:365
DeviceFoundCallback * DeviceFoundCallbackHandle
Definition: peak_interface.hpp:53
#define PEAK_CALL_CONV
Definition: peak_dynamic_loader.h:29
#define PEAK_C_ABI_PREFIX
Definition: peak_dll_defines.h:16
std::shared_ptr< System > ParentSystem() const
Returns the parent system.
Definition: peak_interface.hpp:310
std::string TLType() const
Returns the TL (transport layer) type.
Definition: peak_interface.hpp:289
struct PEAK_INTERFACE * PEAK_INTERFACE_HANDLE
Definition: peak_backend.h:361
DeviceLostCallbackHandle RegisterDeviceLostCallback(const DeviceLostCallback &callback)
Registers a callback for signaling a lost device.
Definition: peak_interface.hpp:328
struct PEAK_MODULE * PEAK_MODULE_HANDLE
Definition: peak_backend.h:401
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_DEVICE_LOST_CALLBACK * PEAK_DEVICE_LOST_CALLBACK_HANDLE
Definition: peak_backend.h:500
The base class for all modules being able to raise events.
Definition: peak_event_supporting_module.hpp:33
std::string ID() const
Returns the ID.
Definition: peak_interface.hpp:275
PEAK_C_API PEAK_DeviceDescriptor_GetKey(PEAK_DEVICE_DESCRIPTOR_HANDLE deviceDescriptorHandle, char *key, size_t *keySize)
Asks the given device descriptor 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.
Interface & operator=(const Interface &other)=delete