17#include <peak/dll_interface/peak_dll_interface_util.hpp>
18#include <peak/generic/peak_t_callback_manager.hpp>
22#include <unordered_map>
39struct InterfaceFoundCallbackContainer;
51 ,
public std::enable_shared_from_this<System>
71 std::string
Key()
const;
84 std::string
ID()
const;
99 std::string
TLType()
const;
141 std::vector<std::shared_ptr<InterfaceDescriptor>>
Interfaces()
const;
205 struct InterfaceFoundCallbackContainer
207 std::weak_ptr<System> System_;
213 const char* lostInterfaceId,
size_t lostInterfaceIdSize,
void* context);
219 void InitializeUpdateMechanismIfNecessary();
221 friend ClassCreator<System>;
225 std::weak_ptr<ProducerLibrary> m_parentLibrary;
227 std::unique_ptr<TCallbackManager<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE, InterfaceFoundCallbackContainer>>
228 m_interfaceFoundCallbackManager;
229 std::unique_ptr<TCallbackManager<PEAK_INTERFACE_LOST_CALLBACK_HANDLE, InterfaceLostCallback>>
230 m_interfaceLostCallbackManager;
232 std::vector<std::shared_ptr<InterfaceDescriptor>> m_interfaces;
233 std::unordered_map<std::string, std::shared_ptr<InterfaceDescriptor>> m_interfacesByKey;
234 std::unordered_map<std::string, std::string> m_interfacesKeyById;
235 mutable std::mutex m_interfacesMutex;
236 std::once_flag m_updateMechanismInitializedFlag;
251 : m_backendHandle(systemHandle)
252 , m_parentLibrary(parentLibrary)
253 , m_interfaceFoundCallbackManager()
254 , m_interfaceLostCallbackManager()
255 , m_key(QueryStringFromCInterfaceFunction([&](char* key, size_t* keySize) {
259 m_interfaceFoundCallbackManager =
260 std::make_unique<TCallbackManager<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE, InterfaceFoundCallbackContainer>>(
261 [&](
void* callbackContext) {
262 return QueryNumericFromCInterfaceFunction<PEAK_INTERFACE_FOUND_CALLBACK_HANDLE>(
265 InterfaceFoundCallbackCWrapper, callbackContext, interfaceFoundCallbackHandle);
269 CallAndCheckCInterfaceFunction([&] {
271 m_backendHandle, callbackHandle);
275 m_interfaceLostCallbackManager =
276 std::make_unique<TCallbackManager<PEAK_INTERFACE_LOST_CALLBACK_HANDLE, InterfaceLostCallback>>(
277 [&](
void* callbackContext) {
278 return QueryNumericFromCInterfaceFunction<PEAK_INTERFACE_LOST_CALLBACK_HANDLE>(
281 InterfaceLostCallbackCWrapper, callbackContext, interfaceLostCallbackHandle);
285 CallAndCheckCInterfaceFunction([&] {
287 m_backendHandle, callbackHandle);
296 m_interfaceFoundCallbackManager->UnregisterAllCallbacks();
297 m_interfaceLostCallbackManager->UnregisterAllCallbacks();
312 return QueryRawInformationFromCInterfaceFunction([&](int32_t* dataType, uint8_t* buffer,
size_t* bufferSize) {
319 return QueryStringFromCInterfaceFunction([&](
char*
id,
size_t* idSize) {
326 return QueryStringFromCInterfaceFunction([&](
char* displayName,
size_t* displayNameSize) {
333 return QueryStringFromCInterfaceFunction([&](
char* vendorName,
size_t* vendorNameSize) {
340 return QueryStringFromCInterfaceFunction([&](
char* modelName,
size_t* modelNameSize) {
347 return QueryStringFromCInterfaceFunction([&](
char* version,
size_t* versionSize) {
354 return QueryStringFromCInterfaceFunction([&](
char* tlType,
size_t* tlTypeSize) {
361 return QueryStringFromCInterfaceFunction([&](
char* ctiFileName,
size_t* ctiFileNameSize) {
368 return QueryStringFromCInterfaceFunction([&](
char* ctiFullPath,
size_t* ctiFullPathSize) {
375 return QueryNumericFromCInterfaceFunction<uint32_t>([&](uint32_t* gentlVersionMajor) {
382 return QueryNumericFromCInterfaceFunction<uint32_t>([&](uint32_t* gentlVersionMinor) {
390 QueryNumericFromCInterfaceFunction<PEAK_CHARACTER_ENCODING>(
398 InitializeUpdateMechanismIfNecessary();
400 CallAndCheckCInterfaceFunction(
406 std::lock_guard<std::mutex> lock(m_interfacesMutex);
412 return LockOrThrow(m_parentLibrary);
419 m_interfaceFoundCallbackManager->RegisterCallback(InterfaceFoundCallbackContainer{ shared_from_this(),
420 [callback](
const std::shared_ptr<InterfaceDescriptor>& foundInterface) { callback(foundInterface); } }));
425 m_interfaceFoundCallbackManager->UnregisterCallback(
437 m_interfaceLostCallbackManager->UnregisterCallback(
444 auto callbackContainer =
static_cast<InterfaceFoundCallbackContainer*
>(context);
446 auto interfaceDescriptor = callbackContainer->System_.lock()->GetOrAddFoundInterface(foundInterface);
448 callbackContainer->Callback(interfaceDescriptor);
452 const char* lostInterfaceId,
size_t lostInterfaceIdSize,
void* context)
456 callback->operator()(std::string(lostInterfaceId, lostInterfaceIdSize - 1));
459inline std::shared_ptr<InterfaceDescriptor> System::GetOrAddFoundInterface(
462 std::lock_guard<std::mutex> lock(m_interfacesMutex);
464 const auto interfaceKey = QueryStringFromCInterfaceFunction([&](
char* key,
size_t* keySize) {
467 auto keyInterfacePairIterator = m_interfacesByKey.find(interfaceKey);
468 if (keyInterfacePairIterator == m_interfacesByKey.end())
470 auto interfaceDescriptor = std::static_pointer_cast<InterfaceDescriptor>(
471 std::make_shared<ClassCreator<InterfaceDescriptor>>(
472 foundInterface, std::weak_ptr<System>{ shared_from_this() }));
473 m_interfaces.emplace_back(interfaceDescriptor);
474 m_interfacesByKey.emplace(interfaceKey, interfaceDescriptor);
475 m_interfacesKeyById.emplace(interfaceDescriptor->ID(), interfaceKey);
477 return interfaceDescriptor;
480 return keyInterfacePairIterator->second;
485 auto moduleHandle = QueryNumericFromCInterfaceFunction<PEAK_MODULE_HANDLE>(
495 auto eventSupportingModuleHandle = QueryNumericFromCInterfaceFunction<PEAK_EVENT_SUPPORTING_MODULE_HANDLE>(
498 m_backendHandle, _eventSupportingModuleHandle);
501 return eventSupportingModuleHandle;
504inline void System::InitializeUpdateMechanismIfNecessary()
506 std::call_once(m_updateMechanismInitializedFlag, [&] {
511 std::lock_guard<std::mutex> lock(m_interfacesMutex);
512 const auto lostInterfaceKey = m_interfacesKeyById.at(lostInterfaceId);
513 m_interfaces.erase(std::remove_if(std::begin(m_interfaces), std::end(m_interfaces),
514 [lostInterfaceKey](
const std::shared_ptr<InterfaceDescriptor>& interface_) {
515 return interface_->Key() == lostInterfaceKey;
517 std::end(m_interfaces));
518 m_interfacesByKey.erase(lostInterfaceKey);
519 m_interfacesKeyById.erase(lostInterfaceId);
EventSupportingModule()=default
virtual PEAK_EVENT_SUPPORTING_MODULE_HANDLE EventSupportingModuleHandle() const =0
The base class for all exceptions thrown by the library.
Definition peak_exception.hpp:28
virtual PEAK_MODULE_HANDLE ModuleHandle() const =0
Represents a GenTL producer library (CTI).
Definition peak_producer_library.hpp:40
uint32_t GenTLVersionMajor() const
Returns the GenTL major version of the GenTL producer library this system belongs to.
Definition peak_system.hpp:373
void UnregisterInterfaceLostCallback(InterfaceLostCallbackHandle callbackHandle)
Unregisters a interface lost callback.
Definition peak_system.hpp:435
std::string TLType() const
Returns the TL (transport layer) type.
Definition peak_system.hpp:352
void UpdateInterfaces(Timeout timeout_ms)
Searches for interfaces.
Definition peak_system.hpp:396
std::function< void(const std::shared_ptr< InterfaceDescriptor > &foundInterface)> InterfaceFoundCallback
Definition peak_system.hpp:55
std::string CTIFileName() const
Returns the file name of the GenTL producer library this system belongs to.
Definition peak_system.hpp:359
void UnregisterInterfaceFoundCallback(InterfaceFoundCallbackHandle callbackHandle)
Unregisters a interface found callback.
Definition peak_system.hpp:423
std::string CTIFullPath() const
Returns the full path of the GenTL producer library this system belongs to.
Definition peak_system.hpp:366
System(const System &other)=delete
InterfaceLostCallback * InterfaceLostCallbackHandle
Definition peak_system.hpp:61
std::string DisplayName() const
Returns the display name.
Definition peak_system.hpp:324
InterfaceFoundCallbackHandle RegisterInterfaceFoundCallback(const InterfaceFoundCallback &callback)
Registers a callback for signaling a found interface.
Definition peak_system.hpp:415
uint32_t GenTLVersionMinor() const
Returns the GenTL minor version of the GenTL producer library this system belongs to.
Definition peak_system.hpp:380
std::string Version() const
Returns the version.
Definition peak_system.hpp:345
System(System &&other)=delete
InterfaceLostCallbackHandle RegisterInterfaceLostCallback(const InterfaceLostCallback &callback)
Registers a callback for signaling a lost interface.
Definition peak_system.hpp:429
~System() override
Definition peak_system.hpp:292
std::string ID() const
Returns the ID.
Definition peak_system.hpp:317
peak::core::CharacterEncoding CharacterEncoding() const
Returns the character encoding of the GenTL producer library this system belongs to.
Definition peak_system.hpp:387
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition peak_system.hpp:57
std::shared_ptr< ProducerLibrary > ParentLibrary() const
Returns the parent library.
Definition peak_system.hpp:410
System & operator=(const System &other)=delete
std::vector< std::shared_ptr< InterfaceDescriptor > > Interfaces() const
Returns the interface list.
Definition peak_system.hpp:404
std::string ModelName() const
Returns the model name.
Definition peak_system.hpp:338
std::function< void(const std::string &lostInterfaceId)> InterfaceLostCallback
Definition peak_system.hpp:59
System & operator=(System &&other)=delete
std::string VendorName() const
Returns the vendor name.
Definition peak_system.hpp:331
std::string Key() const
Returns the unique key.
Definition peak_system.hpp:305
RawInformation Info(int32_t infoCommand) const
Delivers information based on the given GenTL info command.
Definition peak_system.hpp:310
Represents a timeout value in milliseconds.
Definition peak_timeout.hpp:32
The "core" namespace contains the direct GenAPI and GenTL wrapper.
Definition peak_buffer.hpp:34
CharacterEncoding
Possible character encodings.
Definition peak_common_system_enums.hpp:25
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_dynamic_loader.h:33
PEAK_C_API PEAK_System_GetGenTLVersionMinor(PEAK_SYSTEM_HANDLE systemHandle, uint32_t *gentlVersionMinor)
Asks the given system for the minor component of its GenTL version.
PEAK_INTERFACE_LOST_CALLBACK * PEAK_INTERFACE_LOST_CALLBACK_HANDLE
Definition peak_backend.h:515
PEAK_C_API PEAK_System_GetGenTLVersionMajor(PEAK_SYSTEM_HANDLE systemHandle, uint32_t *gentlVersionMajor)
Asks the given system for the major component of its GenTL version.
PEAK_C_API PEAK_System_ToEventSupportingModule(PEAK_SYSTEM_HANDLE systemHandle, PEAK_EVENT_SUPPORTING_MODULE_HANDLE *eventSupportingModuleHandle)
Casts the given system to an event-supporting module.
PEAK_C_API PEAK_System_GetInfo(PEAK_SYSTEM_HANDLE systemHandle, int32_t infoCommand, int32_t *infoDataType, uint8_t *info, size_t *infoSize)
Asks the given system for an information based on a specific command.
struct PEAK_MODULE * PEAK_MODULE_HANDLE
Definition peak_backend.h:421
PEAK_C_API PEAK_System_GetKey(PEAK_SYSTEM_HANDLE systemHandle, char *key, size_t *keySize)
Asks the given system for its key.
PEAK_C_API PEAK_System_Destruct(PEAK_SYSTEM_HANDLE systemHandle)
Destroys the given system.
struct PEAK_INTERFACE_DESCRIPTOR * PEAK_INTERFACE_DESCRIPTOR_HANDLE
Definition peak_backend.h:377
PEAK_C_API PEAK_System_GetDisplayName(PEAK_SYSTEM_HANDLE systemHandle, char *displayName, size_t *displayNameSize)
Asks the given system for its display name.
PEAK_C_API PEAK_InterfaceDescriptor_GetKey(PEAK_INTERFACE_DESCRIPTOR_HANDLE interfaceDescriptorHandle, char *key, size_t *keySize)
Asks the given interface descriptor for its key.
PEAK_C_API PEAK_System_UpdateInterfaces(PEAK_SYSTEM_HANDLE systemHandle, uint64_t timeout_ms)
Tells the given system to update its interfaces with the given timeout.
PEAK_C_API PEAK_System_GetID(PEAK_SYSTEM_HANDLE systemHandle, char *id, size_t *idSize)
Asks the given system for its ID.
struct PEAK_EVENT_SUPPORTING_MODULE * PEAK_EVENT_SUPPORTING_MODULE_HANDLE
Definition peak_backend.h:477
PEAK_C_API PEAK_System_UnregisterInterfaceLostCallback(PEAK_SYSTEM_HANDLE systemHandle, PEAK_INTERFACE_LOST_CALLBACK_HANDLE callbackHandle)
Unregisters the interface-lost callback with the given handle from the given system.
struct PEAK_SYSTEM * PEAK_SYSTEM_HANDLE
Definition peak_backend.h:373
PEAK_C_API PEAK_System_GetCTIFileName(PEAK_SYSTEM_HANDLE systemHandle, char *ctiFileName, size_t *ctiFileNameSize)
Asks the given system for its parent CTI's file name.
PEAK_C_API PEAK_System_GetModelName(PEAK_SYSTEM_HANDLE systemHandle, char *modelName, size_t *modelNameSize)
Asks the given system for its model name.
PEAK_C_API PEAK_System_GetCharacterEncoding(PEAK_SYSTEM_HANDLE systemHandle, PEAK_CHARACTER_ENCODING *characterEncoding)
Asks the given system for its character encoding.
PEAK_C_API PEAK_System_GetVendorName(PEAK_SYSTEM_HANDLE systemHandle, char *vendorName, size_t *vendorNameSize)
Asks the given system for its vendor name.
PEAK_C_API PEAK_System_GetCTIFullPath(PEAK_SYSTEM_HANDLE systemHandle, char *ctiFullPath, size_t *ctiFullPathSize)
Asks the given system for its parent CTI's full path.
PEAK_C_API PEAK_System_GetTLType(PEAK_SYSTEM_HANDLE systemHandle, char *tlType, size_t *tlTypeSize)
Asks the given system for its TL type.
PEAK_C_API PEAK_System_GetVersion(PEAK_SYSTEM_HANDLE systemHandle, char *version, size_t *versionSize)
Asks the given system for its version.
PEAK_C_API PEAK_System_RegisterInterfaceLostCallback(PEAK_SYSTEM_HANDLE systemHandle, PEAK_INTERFACE_LOST_CALLBACK callback, void *callbackContext, PEAK_INTERFACE_LOST_CALLBACK_HANDLE *callbackHandle)
Registers a callback signaling a lost interface at the given system.
int32_t PEAK_CHARACTER_ENCODING
Definition peak_backend.h:107
PEAK_C_API PEAK_System_RegisterInterfaceFoundCallback(PEAK_SYSTEM_HANDLE systemHandle, PEAK_INTERFACE_FOUND_CALLBACK callback, void *callbackContext, PEAK_INTERFACE_FOUND_CALLBACK_HANDLE *callbackHandle)
Registers a callback signaling a found interface at the given system.
PEAK_INTERFACE_FOUND_CALLBACK * PEAK_INTERFACE_FOUND_CALLBACK_HANDLE
Definition peak_backend.h:513
PEAK_C_API PEAK_System_UnregisterInterfaceFoundCallback(PEAK_SYSTEM_HANDLE systemHandle, PEAK_INTERFACE_FOUND_CALLBACK_HANDLE callbackHandle)
Unregisters the interface-found callback with the given handle from the given system.
PEAK_C_API PEAK_System_ToModule(PEAK_SYSTEM_HANDLE systemHandle, PEAK_MODULE_HANDLE *moduleHandle)
Casts the given system to a module.
#define PEAK_C_ABI_PREFIX
Definition peak_dll_defines.h:16
#define PEAK_CALL_CONV
Definition peak_dynamic_loader.h:30