18#include <peak/generic/peak_t_callback_manager.hpp>
29#include <unordered_map>
30#include <unordered_set>
99 using SystemFoundCallback = std::function<void(
const std::shared_ptr<const core::System>& foundSystem)>;
117 using DeviceFoundCallback = std::function<void(
const std::shared_ptr<core::DeviceDescriptor>& foundDevice)>;
130 std::function<void(
const std::shared_ptr<core::DeviceDescriptor>& disconnectedDevice)>;
137 std::function<void(
const std::shared_ptr<core::DeviceDescriptor>& reconnectedDevice,
299 std::vector<std::shared_ptr<const core::System>>
Systems()
const;
310 std::vector<std::shared_ptr<const core::Interface>>
Interfaces()
const;
321 std::vector<std::shared_ptr<core::DeviceDescriptor>>
Devices()
const;
574 void GetLostDevicesFromLostInterfaces(
575 const std::vector<std::string>& lostInterfaces, std::vector<std::string>& lostDevices);
577 void TriggerSystemFoundCallbacks(
const std::shared_ptr<const core::System>& foundSystem)
const;
578 void TriggerInterfaceFoundCallbacks(
const std::shared_ptr<const core::Interface>& foundInterface)
const;
579 void TriggerInterfaceLostCallbacks(
const std::string& lostInterfaceKey)
const;
580 void TriggerDeviceFoundCallbacks(
const std::shared_ptr<core::DeviceDescriptor>& foundDevice)
const;
581 void TriggerDeviceLostCallbacks(
const std::string& lostDeviceKey)
const;
583 void AddSystemCallbacks(
const std::shared_ptr<core::System>& addSystem);
584 void RemoveSystemCallbacks(
const std::shared_ptr<core::System>& removeSystem);
585 void AddInterfaceCallbacks(
const std::shared_ptr<core::Interface>& addInterface);
586 void RemoveInterfaceCallbacks(
const std::shared_ptr<core::Interface>& removeInterface);
588 void CheckDevicesOpened()
const;
590 std::mutex m_updateMutex;
591 std::atomic<core::Timeout> m_interfaceUpdateTimeout_ms{ 100 };
592 std::atomic<core::Timeout> m_deviceUpdateTimeout_ms{ 300 };
594 std::vector<std::string> m_producerLibrariesToAdd;
596 std::unordered_map<std::string, std::shared_ptr<core::ProducerLibrary>> m_producerLibraries;
597 std::vector<std::shared_ptr<const core::System>> m_systems;
598 std::vector<std::shared_ptr<const core::Interface>> m_interfaces;
599 std::vector<std::shared_ptr<core::DeviceDescriptor>> m_devices;
601 struct SystemCallbackContainer
603 SystemCallbackContainer(
605 : foundCallbackHandle(foundHandle)
606 , lostCallbackHandle(lostHandle)
608 ~SystemCallbackContainer() =
default;
609 SystemCallbackContainer(
const SystemCallbackContainer&) =
delete;
610 SystemCallbackContainer& operator=(
const SystemCallbackContainer&) =
delete;
612 SystemCallbackContainer(SystemCallbackContainer&& a)
noexcept
614 std::lock_guard<std::mutex> lock(a.mutex);
616 std::swap(foundCallbackHandle, a.foundCallbackHandle);
617 std::swap(lostCallbackHandle, a.lostCallbackHandle);
620 SystemCallbackContainer& operator=(SystemCallbackContainer&& a)
noexcept
622 std::lock(a.mutex, mutex);
624 std::lock_guard<std::mutex> lock1(a.mutex, std::adopt_lock);
625 std::lock_guard<std::mutex> lock2(mutex, std::adopt_lock);
627 std::swap(foundCallbackHandle, a.foundCallbackHandle);
628 std::swap(lostCallbackHandle, a.lostCallbackHandle);
633 mutable std::mutex mutex;
637 std::vector<std::shared_ptr<core::Interface>> foundInterfaces;
638 std::vector<std::string> lostInterfaces;
642 std::unordered_map<std::string, std::string> interfaceIdKeyMap;
645 struct InterfaceCallbackContainer
650 : foundCallbackHandle(foundHandle)
651 , lostCallbackHandle(lostHandle)
652 , disconnectedCallbackHandle(disconnectedHandle)
653 , reconnectedCallbackHandle(reconnectedHandle)
654 , deviceListChangedCallbackHandle(listChangedHandle)
657 ~InterfaceCallbackContainer() =
default;
658 InterfaceCallbackContainer(
const InterfaceCallbackContainer&) =
delete;
659 InterfaceCallbackContainer& operator=(
const InterfaceCallbackContainer&) =
delete;
661 InterfaceCallbackContainer(InterfaceCallbackContainer&& a)
noexcept
663 std::lock_guard<std::mutex> lock(a.mutex);
665 std::swap(foundCallbackHandle, a.foundCallbackHandle);
666 std::swap(lostCallbackHandle, a.lostCallbackHandle);
667 std::swap(disconnectedCallbackHandle, a.disconnectedCallbackHandle);
668 std::swap(reconnectedCallbackHandle, a.reconnectedCallbackHandle);
669 std::swap(deviceListChangedCallbackHandle, a.deviceListChangedCallbackHandle);
672 InterfaceCallbackContainer& operator=(InterfaceCallbackContainer&& a)
noexcept
674 std::lock(a.mutex, mutex);
676 std::lock_guard<std::mutex> lock1(a.mutex, std::adopt_lock);
677 std::lock_guard<std::mutex> lock2(mutex, std::adopt_lock);
679 std::swap(foundCallbackHandle, a.foundCallbackHandle);
680 std::swap(lostCallbackHandle, a.lostCallbackHandle);
681 std::swap(disconnectedCallbackHandle, a.disconnectedCallbackHandle);
682 std::swap(reconnectedCallbackHandle, a.reconnectedCallbackHandle);
683 std::swap(deviceListChangedCallbackHandle, a.deviceListChangedCallbackHandle);
688 mutable std::mutex mutex;
695 std::vector<std::shared_ptr<core::DeviceDescriptor>> foundDevices;
696 std::vector<std::string> lostDevices;
700 std::unordered_map<std::string, std::string> deviceIdKeyMap;
703 mutable std::mutex m_callbackContainerMutex;
704 std::unordered_map<std::string, SystemCallbackContainer> m_systemCallbackContainers;
705 std::unordered_map<std::string, InterfaceCallbackContainer> m_interfaceCallbackContainers;
709 std::weak_ptr<core::Device> device{};
710 std::string displayName;
712 std::vector<OpenedDevice> m_openedDevices;
714 core::TTriggerCallbackManager<SystemFoundCallbackHandle, SystemFoundCallback> m_systemFoundCallbackManager;
715 core::TTriggerCallbackManager<InterfaceFoundCallbackHandle, InterfaceFoundCallback> m_interfaceFoundCallbackManager;
716 core::TTriggerCallbackManager<InterfaceLostCallbackHandle, InterfaceLostCallback> m_interfaceLostCallbackManager;
717 core::TTriggerCallbackManager<DeviceFoundCallbackHandle, DeviceFoundCallback> m_deviceFoundCallbackManager;
718 core::TTriggerCallbackManager<DeviceLostCallbackHandle, DeviceLostCallback> m_deviceLostCallbackManager;
719 core::TTriggerCallbackManager<DeviceDisconnectedCallbackHandle, DeviceDisconnectedCallback>
720 m_deviceDisconnectedCallbackManager;
721 core::TTriggerCallbackManager<DeviceReconnectedCallbackHandle, DeviceReconnectedCallback>
722 m_deviceReconnectedCallbackManager;
723 core::TTriggerCallbackManager<DeviceListChangedCallbackHandle, DeviceListChangedCallback>
724 m_deviceListChangedCallbackManager;
726 std::mutex m_producerLibrariesToAddMutex;
727 mutable std::mutex m_systemsMutex;
728 mutable std::recursive_mutex m_interfacesMutex;
729 mutable std::recursive_mutex m_devicesMutex;
730 mutable std::mutex m_openedDevicesMutex;
733 template <
typename T,
734 typename std::enable_if<(noexcept(std::declval<T>()())
735 || (std::is_pointer<typename std::decay<T>::type>::value && __cplusplus < 201703L)
738 && std::is_same<void, decltype(std::declval<T>()())>::value && std::is_move_constructible<T>::value
739 && std::is_nothrow_move_constructible<T>::value,
742 template <
typename T>
749 explicit RAIIGuard(T&& fun) noexcept
750 : m_fun(std::forward<T>(fun))
752 RAIIGuard(RAIIGuard
const&) =
delete;
753 RAIIGuard(RAIIGuard&& other) noexcept
754 : m_fun(std::move(other.m_fun))
756 ~RAIIGuard() noexcept
761 template <
typename T>
762 static RAIIGuard<T> makeRAIIGuard(T&& fun)
noexcept
764 return { std::forward<T>(fun) };
767 template <
typename T = DeviceManager>
768 class LifetimeTracker
771 static void Create();
772 static void Destroy();
773 static T& Instance();
776 static T* m_instance;
777 static std::mutex m_mtx;
778 static size_t m_initializedCount;
789T* DeviceManager::LifetimeTracker<T>::m_instance = {};
792std::mutex DeviceManager::LifetimeTracker<T>::m_mtx = {};
795size_t DeviceManager::LifetimeTracker<T>::m_initializedCount = {};
798void DeviceManager::LifetimeTracker<T>::Create()
800 std::lock_guard<
decltype(m_mtx)> lock{ m_mtx };
804 m_instance =
new T{};
807 m_initializedCount++;
811void DeviceManager::LifetimeTracker<T>::Destroy()
813 std::lock_guard<
decltype(m_mtx)> lock{ m_mtx };
815 if (0 == m_initializedCount)
820 m_initializedCount--;
822 if (0 == m_initializedCount)
827 m_instance =
nullptr;
833T& DeviceManager::LifetimeTracker<T>::Instance()
835 if (m_instance ==
nullptr)
837 throw core::NotInitializedException(
838 "Library is not initialized. Call peak::Library::Initalize() first!");
845 return LifetimeTracker<>::Instance();
848inline DeviceManager::~DeviceManager()
855 std::lock_guard<std::mutex> lock(m_producerLibrariesToAddMutex);
857 m_producerLibrariesToAdd.emplace_back(ctiPath);
862 std::lock(m_updateMutex, m_systemsMutex);
864 std::lock_guard<std::mutex> lockUpdate(m_updateMutex, std::adopt_lock);
866 std::shared_ptr<core::System> newSystem{};
868 std::lock_guard<std::mutex> lockSystems(m_systemsMutex, std::adopt_lock);
870 const auto systemDescriptor = library->System();
871 const auto ctiPath = systemDescriptor->CTIFullPath();
874 if (m_producerLibraries.count(ctiPath) == 0)
876 m_producerLibraries.emplace(ctiPath, library);
878 newSystem = systemDescriptor->IsOpened() ? systemDescriptor->OpenedSystem() : systemDescriptor->OpenSystem();
879 AddSystemCallbacks(newSystem);
880 m_systems.emplace_back(newSystem);
886 TriggerSystemFoundCallbacks(newSystem);
893 std::lock_guard<std::mutex> lockUpdate(m_updateMutex);
895 std::vector<std::shared_ptr<core::System>> allFoundSystems;
896 std::vector<std::shared_ptr<core::Interface>> allFoundInterfaces;
897 std::vector<std::string> allLostInterfaces;
898 std::vector<std::shared_ptr<core::DeviceDescriptor>> allFoundDevices;
899 std::vector<std::string> allLostDevices;
901 auto interfaceUpdateTimeout_ms = m_interfaceUpdateTimeout_ms.load();
902 auto deviceUpdateTimeout_ms = m_deviceUpdateTimeout_ms.load();
904 std::vector<std::string> ctiPaths;
913 std::lock_guard<std::mutex> lock(m_producerLibrariesToAddMutex);
915 ctiPaths.reserve(ctiPaths.size() + m_producerLibrariesToAdd.size());
917 std::begin(m_producerLibrariesToAdd), std::end(m_producerLibrariesToAdd), std::back_inserter(ctiPaths));
919 m_producerLibrariesToAdd.clear();
922 for (
const auto& ctiPath : ctiPaths)
924 if (m_producerLibraries.count(ctiPath) == 0)
929 auto system = producerLibrary->System()->OpenSystem();
931 AddSystemCallbacks(system);
933 m_producerLibraries.emplace(ctiPath, producerLibrary);
934 allFoundSystems.emplace_back(system);
952 errorCallback(
"[CTI - " + ctiPath +
"]: " + ex.what());
961 auto systemUpdateChildren = [&](
const std::shared_ptr<core::System>& system) {
964 system->UpdateInterfaces(interfaceUpdateTimeout_ms);
972 errorCallback(
"[System - " + system->Key() +
"]: " + ex.what());
981 constexpr auto launchPolicy = std::launch::async;
983 std::vector<std::future<void>> systemUpdateChildrenTasks;
986 for (
const auto& system : m_systems)
988 systemUpdateChildrenTasks.emplace_back(
989 std::async(launchPolicy, systemUpdateChildren, std::const_pointer_cast<core::System>(system)));
993 for (
const auto& system : allFoundSystems)
995 systemUpdateChildrenTasks.emplace_back(std::async(launchPolicy, systemUpdateChildren, system));
999 systemUpdateChildrenTasks.clear();
1002 for (
auto& keyValue : m_systemCallbackContainers)
1004 auto& container = keyValue.second;
1006 std::lock_guard<std::mutex> lock(container.mutex);
1008 allFoundInterfaces.reserve(allFoundInterfaces.size() + container.foundInterfaces.size());
1009 for (
const auto& foundInterface : container.foundInterfaces)
1011 AddInterfaceCallbacks(foundInterface);
1012 allFoundInterfaces.emplace_back(foundInterface);
1014 container.foundInterfaces.clear();
1016 allLostInterfaces.reserve(allLostInterfaces.size() + container.lostInterfaces.size());
1017 for (
const auto& lostInterface : container.lostInterfaces)
1019 auto interfaceIt = std::find_if(std::begin(m_interfaces), std::end(m_interfaces),
1020 [&lostInterface](
const auto& intf) {
return intf->Key() == lostInterface; });
1022 if (interfaceIt != std::end(m_interfaces))
1024 RemoveInterfaceCallbacks(std::const_pointer_cast<core::Interface>(*interfaceIt));
1027 allLostInterfaces.emplace_back(lostInterface);
1029 container.lostInterfaces.clear();
1032 auto interfaceUpdateChildren = [&deviceUpdateTimeout_ms, &errorCallback](
1033 const std::shared_ptr<core::Interface>& iface) {
1036 iface->UpdateDevices(deviceUpdateTimeout_ms);
1044 errorCallback(
"[Interface - " + iface->Key() +
"]: " + ex.what());
1053 std::vector<std::future<void>> interfaceUpdateChildrenTasks;
1056 for (
const auto& iface : m_interfaces)
1058 interfaceUpdateChildrenTasks.emplace_back(
1059 std::async(launchPolicy, interfaceUpdateChildren, std::const_pointer_cast<core::Interface>(iface)));
1063 for (
const auto& iface : allFoundInterfaces)
1065 interfaceUpdateChildrenTasks.emplace_back(std::async(launchPolicy, interfaceUpdateChildren, iface));
1069 interfaceUpdateChildrenTasks.clear();
1072 for (
auto& keyValue : m_interfaceCallbackContainers)
1074 auto& container = keyValue.second;
1075 std::lock_guard<std::mutex> lock(container.mutex);
1077 decltype(container.foundDevices) foundDevices;
1078 std::swap(container.foundDevices, foundDevices);
1079 allFoundDevices.reserve(allFoundDevices.size() + foundDevices.size());
1080 std::move(std::begin(foundDevices), std::end(foundDevices), std::back_inserter(allFoundDevices));
1082 decltype(container.lostDevices) lostDevices;
1083 std::swap(container.lostDevices, lostDevices);
1084 allLostDevices.reserve(allLostDevices.size() + lostDevices.size());
1085 std::move(std::begin(lostDevices), std::end(lostDevices), std::back_inserter(allLostDevices));
1090 std::lock(m_systemsMutex, m_interfacesMutex, m_devicesMutex);
1092 std::lock_guard<std::mutex> lockSystems(m_systemsMutex, std::adopt_lock);
1093 std::lock_guard<std::recursive_mutex> lockInterfaces(m_interfacesMutex, std::adopt_lock);
1094 std::lock_guard<std::recursive_mutex> lockDevices(m_devicesMutex, std::adopt_lock);
1096 m_systems.reserve(m_systems.size() + allFoundSystems.size());
1097 std::copy(std::begin(allFoundSystems), std::end(allFoundSystems), std::back_inserter(m_systems));
1099 for (
const auto& lostInterfaceKey : allLostInterfaces)
1101 m_interfaces.erase(std::remove_if(std::begin(m_interfaces), std::end(m_interfaces),
1102 [&lostInterfaceKey](
const auto& interfaceToCheck) {
1103 return interfaceToCheck->Key() == lostInterfaceKey;
1105 std::end(m_interfaces));
1107 m_interfaces.reserve(m_interfaces.size() + allFoundInterfaces.size());
1108 std::copy(std::begin(allFoundInterfaces), std::end(allFoundInterfaces), std::back_inserter(m_interfaces));
1110 for (
const auto& lostDeviceKey : allLostDevices)
1113 std::remove_if(std::begin(m_devices), std::end(m_devices),
1114 [&lostDeviceKey](
const auto& deviceToCheck) {
return deviceToCheck->Key() == lostDeviceKey; }),
1115 std::end(m_devices));
1117 m_devices.reserve(m_devices.size() + allFoundDevices.size());
1118 std::copy(std::begin(allFoundDevices), std::end(allFoundDevices), std::back_inserter(m_devices));
1122 for (
const auto& foundSystem : allFoundSystems)
1124 TriggerSystemFoundCallbacks(foundSystem);
1127 for (
const auto& foundInterface : allFoundInterfaces)
1129 TriggerInterfaceFoundCallbacks(foundInterface);
1132 for (
const auto& lostInterfaceKey : allLostInterfaces)
1134 TriggerInterfaceLostCallbacks(lostInterfaceKey);
1137 for (
const auto& foundDevice : allFoundDevices)
1139 TriggerDeviceFoundCallbacks(foundDevice);
1142 GetLostDevicesFromLostInterfaces(allLostInterfaces, allLostDevices);
1143 for (
const auto& lostDeviceKey : allLostDevices)
1145 TriggerDeviceLostCallbacks(lostDeviceKey);
1151 std::lock_guard<std::mutex> lockUpdate(m_updateMutex);
1156 CheckDevicesOpened();
1160 std::lock(m_systemsMutex, m_interfacesMutex, m_devicesMutex);
1162 std::lock_guard<std::mutex> lockSystem(m_systemsMutex, std::adopt_lock);
1163 std::lock_guard<std::recursive_mutex> lockInterfaces(m_interfacesMutex, std::adopt_lock);
1164 std::lock_guard<std::recursive_mutex> lockDevices(m_devicesMutex, std::adopt_lock);
1168 decltype(m_devices) lostDevicesList{};
1169 std::swap(m_devices, lostDevicesList);
1170 for (
const auto& lostDevice : lostDevicesList)
1172 TriggerDeviceLostCallbacks(lostDevice->Key());
1175 decltype(m_interfaces) lostInterfacesList{};
1176 std::swap(m_interfaces, lostInterfacesList);
1177 for (
const auto& lostInterface : lostInterfacesList)
1179 TriggerInterfaceLostCallbacks(lostInterface->Key());
1180 RemoveInterfaceCallbacks(std::const_pointer_cast<core::Interface>(lostInterface));
1183 decltype(m_systems) lostSystemsList{};
1184 std::swap(m_systems, lostSystemsList);
1185 for (
const auto& lostSystem : lostSystemsList)
1187 RemoveSystemCallbacks(std::const_pointer_cast<core::System>(lostSystem));
1190 m_producerLibraries.clear();
1194 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1196 m_systemCallbackContainers.clear();
1197 m_interfaceCallbackContainers.clear();
1201 std::lock_guard<std::mutex> lock(m_producerLibrariesToAddMutex);
1202 m_producerLibrariesToAdd.clear();
1206inline void DeviceManager::AddSystemCallbacks(
const std::shared_ptr<core::System>& addSystem)
1208 auto SystemKey = addSystem->Key();
1210 const auto foundCallbackHandle = addSystem->RegisterInterfaceFoundCallback(
1211 [
this, SystemKey](
const std::shared_ptr<core::InterfaceDescriptor>& foundInterface) {
1212 const auto systemIt = m_systemCallbackContainers.find(SystemKey);
1213 if (systemIt != std::end(m_systemCallbackContainers))
1217 auto& container = systemIt->second;
1218 std::lock_guard<std::mutex> lock(container.mutex);
1220 const auto openedInterface = foundInterface->OpenInterface();
1222 container.foundInterfaces.emplace_back(openedInterface);
1223 container.interfaceIdKeyMap.emplace(openedInterface->ID(), openedInterface->Key());
1225 catch (
const std::exception&)
1230 const auto lostCallbackHandle = addSystem->RegisterInterfaceLostCallback(
1231 [
this, SystemKey](
const std::string& lostInterfaceId) {
1232 const auto systemIt = m_systemCallbackContainers.find(SystemKey);
1233 if (systemIt != std::end(m_systemCallbackContainers))
1235 auto& container = systemIt->second;
1236 std::lock_guard<std::mutex> lock(container.mutex);
1238 const auto interfaceKeyIt = container.interfaceIdKeyMap.find(lostInterfaceId);
1239 if (interfaceKeyIt != std::end(container.interfaceIdKeyMap))
1241 container.lostInterfaces.emplace_back(interfaceKeyIt->second);
1242 container.interfaceIdKeyMap.erase(interfaceKeyIt);
1247 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1249 m_systemCallbackContainers.emplace(
1250 addSystem->Key(), SystemCallbackContainer{ foundCallbackHandle, lostCallbackHandle });
1253inline void DeviceManager::RemoveSystemCallbacks(
const std::shared_ptr<core::System>& removeSystem)
1255 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1257 const auto SystemKey = removeSystem->Key();
1259 const auto systemIt = m_systemCallbackContainers.find(SystemKey);
1261 if (systemIt != std::end(m_systemCallbackContainers))
1263 const auto& container = systemIt->second;
1265 std::lock_guard<std::mutex> lockContainer(container.mutex);
1267 removeSystem->UnregisterInterfaceFoundCallback(container.foundCallbackHandle);
1268 removeSystem->UnregisterInterfaceLostCallback(container.lostCallbackHandle);
1271 m_systemCallbackContainers.erase(systemIt);
1275inline void DeviceManager::AddInterfaceCallbacks(
const std::shared_ptr<core::Interface>& addInterface)
1277 auto InterfaceKey = addInterface->Key();
1279 const auto foundCallbackHandle = addInterface->RegisterDeviceFoundCallback(
1280 [
this, InterfaceKey](
const std::shared_ptr<core::DeviceDescriptor>& foundDevice) {
1281 const auto interfaceIt = m_interfaceCallbackContainers.find(InterfaceKey);
1283 if (interfaceIt != std::end(m_interfaceCallbackContainers))
1285 auto& container = interfaceIt->second;
1286 std::lock_guard<std::mutex> lock(container.mutex);
1288 container.foundDevices.emplace_back(foundDevice);
1289 container.deviceIdKeyMap.emplace(foundDevice->ID(), foundDevice->Key());
1293 foundDevice->RegisterDeviceOpenedCallback([
this](
const std::shared_ptr<core::Device>& openedDevice) {
1294 std::lock_guard<std::mutex> lock(m_openedDevicesMutex);
1297 m_openedDevices.erase(std::remove_if(std::begin(m_openedDevices), std::end(m_openedDevices),
1298 [](
const OpenedDevice& dev) {
return dev.device.expired(); }),
1299 std::end(m_openedDevices));
1302 OpenedDevice device;
1303 device.device = openedDevice;
1304 device.displayName = openedDevice->DisplayName();
1305 m_openedDevices.push_back(device);
1309 const auto lostCallbackHandle = addInterface->RegisterDeviceLostCallback(
1310 [
this, InterfaceKey](
const std::string& lostDeviceId) {
1311 const auto interfaceIt = m_interfaceCallbackContainers.find(InterfaceKey);
1313 if (interfaceIt != std::end(m_interfaceCallbackContainers))
1315 auto& container = interfaceIt->second;
1316 std::lock_guard<std::mutex> lock(container.mutex);
1318 const auto lostDeviceKeyIt = container.deviceIdKeyMap.find(lostDeviceId);
1319 if (lostDeviceKeyIt != std::end(container.deviceIdKeyMap))
1321 container.lostDevices.emplace_back(lostDeviceKeyIt->second);
1322 container.deviceIdKeyMap.erase(lostDeviceKeyIt);
1327 const auto reconnectedHandle = addInterface->RegisterDeviceReconnectedCallback(
1328 [
this](
const std::shared_ptr<core::DeviceDescriptor>& reconnectedDevice,
1329 core::DeviceReconnectInformation reconnectInformation) {
1330 return m_deviceReconnectedCallbackManager.TriggerCallbacks(reconnectedDevice, reconnectInformation);
1333 const auto disconnectedHandle = addInterface->RegisterDeviceDisconnectedCallback(
1334 [
this](
const std::shared_ptr<core::DeviceDescriptor>& disconnectedDevice) {
1335 return m_deviceDisconnectedCallbackManager.TriggerCallbacks(disconnectedDevice);
1338 const auto deviceListChangedHandle = addInterface->RegisterDeviceListChangedCallback(
1339 [
this]() {
return m_deviceListChangedCallbackManager.TriggerCallbacks(); });
1341 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1343 m_interfaceCallbackContainers.emplace(addInterface->Key(),
1344 InterfaceCallbackContainer{
1345 foundCallbackHandle, lostCallbackHandle, disconnectedHandle, reconnectedHandle, deviceListChangedHandle });
1348inline void DeviceManager::RemoveInterfaceCallbacks(
const std::shared_ptr<core::Interface>& removeInterface)
1350 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1352 const auto interfaceIt = m_interfaceCallbackContainers.find(removeInterface->Key());
1354 if (interfaceIt != std::end(m_interfaceCallbackContainers))
1356 const auto& container = interfaceIt->second;
1358 std::lock_guard<std::mutex> lockContainer(container.mutex);
1360 removeInterface->UnregisterDeviceFoundCallback(container.foundCallbackHandle);
1361 removeInterface->UnregisterDeviceLostCallback(container.lostCallbackHandle);
1362 removeInterface->UnregisterDeviceDisconnectedCallback(container.disconnectedCallbackHandle);
1363 removeInterface->UnregisterDeviceReconnectedCallback(container.reconnectedCallbackHandle);
1364 removeInterface->UnregisterDeviceListChangedCallback(container.deviceListChangedCallbackHandle);
1366 m_interfaceCallbackContainers.erase(interfaceIt);
1370inline void DeviceManager::CheckDevicesOpened()
const
1372 size_t openDevicesFound = 0;
1373 std::string deviceDisplayNames;
1376 std::lock_guard<std::mutex> lock(m_openedDevicesMutex);
1378 for (
const auto& openedDevice : m_openedDevices)
1380 auto openDevice = openedDevice.device.lock();
1384 deviceDisplayNames += openedDevice.displayName;
1385 deviceDisplayNames +=
" ";
1390 if (openDevicesFound > 0)
1392 throw core::InternalErrorException(
1393 "Can't reset the device manager while there are devices open. The following devices ("
1394 + std::to_string(openDevicesFound) +
") are open: " + deviceDisplayNames);
1400 return m_interfaceUpdateTimeout_ms;
1405 m_interfaceUpdateTimeout_ms = timeout_ms;
1410 return m_deviceUpdateTimeout_ms;
1415 m_deviceUpdateTimeout_ms = timeout_ms;
1420 std::lock_guard<std::mutex> lock(m_systemsMutex);
1427 std::lock_guard<std::recursive_mutex> lock(m_interfacesMutex);
1429 return m_interfaces;
1434 std::lock_guard<std::recursive_mutex> lock(m_devicesMutex);
1442 return m_systemFoundCallbackManager.RegisterCallback(callback);
1447 m_systemFoundCallbackManager.UnregisterCallback(callbackHandle);
1453 return m_interfaceFoundCallbackManager.RegisterCallback(callback);
1458 m_interfaceFoundCallbackManager.UnregisterCallback(callbackHandle);
1464 return m_interfaceLostCallbackManager.RegisterCallback(callback);
1469 m_interfaceLostCallbackManager.UnregisterCallback(callbackHandle);
1475 return m_deviceFoundCallbackManager.RegisterCallback(callback);
1480 m_deviceFoundCallbackManager.UnregisterCallback(callbackHandle);
1486 return m_deviceLostCallbackManager.RegisterCallback(callback);
1491 m_deviceLostCallbackManager.UnregisterCallback(callbackHandle);
1497 return m_deviceDisconnectedCallbackManager.RegisterCallback(callback);
1503 m_deviceDisconnectedCallbackManager.UnregisterCallback(callbackHandle);
1509 return m_deviceReconnectedCallbackManager.RegisterCallback(callback);
1515 m_deviceReconnectedCallbackManager.UnregisterCallback(callbackHandle);
1521 return m_deviceListChangedCallbackManager.RegisterCallback(callback);
1527 m_deviceListChangedCallbackManager.UnregisterCallback(callbackHandle);
1530inline void DeviceManager::GetLostDevicesFromLostInterfaces(
1531 const std::vector<std::string>& lostInterfaces, std::vector<std::string>& lostDevices)
1533 std::lock_guard<std::recursive_mutex> lock(m_devicesMutex);
1535 for (
const auto& device : m_devices)
1537 auto resultIt = std::find(
1538 std::begin(lostInterfaces), std::end(lostInterfaces), device->ParentInterface()->Key());
1539 if (resultIt != std::end(lostInterfaces))
1541 lostDevices.emplace_back(device->Key());
1546inline void DeviceManager::TriggerSystemFoundCallbacks(
const std::shared_ptr<const core::System>& foundSystem)
const
1548 m_systemFoundCallbackManager.TriggerCallbacks(foundSystem);
1551inline void DeviceManager::TriggerInterfaceFoundCallbacks(
1552 const std::shared_ptr<const core::Interface>& foundInterface)
const
1554 m_interfaceFoundCallbackManager.TriggerCallbacks(foundInterface);
1557inline void DeviceManager::TriggerInterfaceLostCallbacks(
const std::string& lostInterfaceKey)
const
1559 m_interfaceLostCallbackManager.TriggerCallbacks(lostInterfaceKey);
1562inline void DeviceManager::TriggerDeviceFoundCallbacks(
const std::shared_ptr<core::DeviceDescriptor>& foundDevice)
const
1564 m_deviceFoundCallbackManager.TriggerCallbacks(foundDevice);
1567inline void DeviceManager::TriggerDeviceLostCallbacks(
const std::string& lostDeviceKey)
const
1569 m_deviceLostCallbackManager.TriggerCallbacks(lostDeviceKey);
The global DeviceManager (singleton) searches all installed producer libraries (*....
Definition peak_device_manager.hpp:76
core::Timeout InterfaceUpdateTimeout() const
Returns the interface update timeout.
Definition peak_device_manager.hpp:1398
std::function< void(const std::string &lostInterfaceKey)> InterfaceLostCallback
Definition peak_device_manager.hpp:111
InterfaceFoundCallbackHandle RegisterInterfaceFoundCallback(const InterfaceFoundCallback &callback)
Registers a callback for signaling a found interface.
Definition peak_device_manager.hpp:1450
void UnregisterDeviceFoundCallback(DeviceFoundCallbackHandle callbackHandle)
Unregisters a device found callback.
Definition peak_device_manager.hpp:1478
void UnregisterDeviceDisconnectedCallback(DeviceDisconnectedCallbackHandle callbackHandle)
Unregisters a device disconnected callback.
Definition peak_device_manager.hpp:1500
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &reconnectedDevice, core::DeviceReconnectInformation reconnectInformation)> DeviceReconnectedCallback
Definition peak_device_manager.hpp:138
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &foundDevice)> DeviceFoundCallback
Definition peak_device_manager.hpp:117
std::vector< std::shared_ptr< const core::System > > Systems() const
Returns the found and opened systems.
Definition peak_device_manager.hpp:1418
DeviceReconnectedCallback * DeviceReconnectedCallbackHandle
Definition peak_device_manager.hpp:141
void UnregisterInterfaceLostCallback(InterfaceLostCallbackHandle callbackHandle)
Unregisters an interface lost callback.
Definition peak_device_manager.hpp:1467
void Reset(ResetPolicy resetPolicy=ResetPolicy::ErrorOnOpenDevices)
Resets the DeviceManager.
Definition peak_device_manager.hpp:1149
UpdatePolicy
Enum holding the possible update policies.
Definition peak_device_manager.hpp:80
@ DontScanEnvironmentForProducerLibraries
@ ScanEnvironmentForProducerLibraries
ResetPolicy
Enum holding the possible reset policies.
Definition peak_device_manager.hpp:90
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition peak_device_manager.hpp:108
void UnregisterDeviceLostCallback(DeviceLostCallbackHandle callbackHandle)
Unregisters a device lost callback.
Definition peak_device_manager.hpp:1489
core::Timeout DeviceUpdateTimeout() const
Returns the device update timeout.
Definition peak_device_manager.hpp:1408
void Update(UpdatePolicy updatePolicy=UpdatePolicy::ScanEnvironmentForProducerLibraries, const UpdateErrorCallback &errorCallback=UpdateErrorCallback())
Starts an update.
Definition peak_device_manager.hpp:890
std::function< void()> DeviceListChangedCallback
Definition peak_device_manager.hpp:144
DeviceDisconnectedCallback * DeviceDisconnectedCallbackHandle
Definition peak_device_manager.hpp:133
std::function< void(const std::shared_ptr< const core::Interface > &foundInterface)> InterfaceFoundCallback
Definition peak_device_manager.hpp:105
DeviceManager & operator=(const DeviceManager &other)=delete
static DeviceManager & Instance()
Returns the global DeviceManager.
Definition peak_device_manager.hpp:843
std::function< void(const std::string &errorDescription)> UpdateErrorCallback
Definition peak_device_manager.hpp:150
std::function< void(const std::shared_ptr< const core::System > &foundSystem)> SystemFoundCallback
Definition peak_device_manager.hpp:99
DeviceReconnectedCallbackHandle RegisterDeviceReconnectedCallback(const DeviceReconnectedCallback &callback)
Registers a callback for signaling a reconnected device.
Definition peak_device_manager.hpp:1506
SystemFoundCallback * SystemFoundCallbackHandle
Definition peak_device_manager.hpp:102
DeviceManager(const DeviceManager &other)=delete
void UnregisterDeviceListChangedCallback(DeviceListChangedCallbackHandle callbackHandle)
Unregisters a device list change callback.
Definition peak_device_manager.hpp:1524
std::vector< std::shared_ptr< core::DeviceDescriptor > > Devices() const
Returns the found devices.
Definition peak_device_manager.hpp:1432
std::function< void(const std::string &lostDeviceKey)> DeviceLostCallback
Definition peak_device_manager.hpp:123
void AddProducerLibrary(const std::string &ctiPath)
Adds the given producer library (CTI).
Definition peak_device_manager.hpp:853
SystemFoundCallbackHandle RegisterSystemFoundCallback(const SystemFoundCallback &callback)
Registers a callback for signaling a found system.
Definition peak_device_manager.hpp:1439
InterfaceLostCallbackHandle RegisterInterfaceLostCallback(const InterfaceLostCallback &callback)
Registers a callback for signaling a lost interface.
Definition peak_device_manager.hpp:1461
DeviceDisconnectedCallbackHandle RegisterDeviceDisconnectedCallback(const DeviceDisconnectedCallback &callback)
Registers a callback for signaling a disconnected device.
Definition peak_device_manager.hpp:1494
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &disconnectedDevice)> DeviceDisconnectedCallback
Definition peak_device_manager.hpp:130
InterfaceLostCallback * InterfaceLostCallbackHandle
Definition peak_device_manager.hpp:114
DeviceFoundCallbackHandle RegisterDeviceFoundCallback(const DeviceFoundCallback &callback)
Registers a callback for signaling a found device.
Definition peak_device_manager.hpp:1472
DeviceFoundCallback * DeviceFoundCallbackHandle
Definition peak_device_manager.hpp:120
void UnregisterDeviceReconnectedCallback(DeviceReconnectedCallbackHandle callbackHandle)
Unregisters a device reconnected callback.
Definition peak_device_manager.hpp:1512
DeviceListChangedCallbackHandle RegisterDeviceListChangedCallback(const DeviceListChangedCallback &callback)
Registers a callback for signaling a change in the device list.
Definition peak_device_manager.hpp:1518
DeviceListChangedCallback * DeviceListChangedCallbackHandle
Definition peak_device_manager.hpp:147
void UnregisterSystemFoundCallback(SystemFoundCallbackHandle callbackHandle)
Unregisters a system found callback.
Definition peak_device_manager.hpp:1445
void SetInterfaceUpdateTimeout(core::Timeout timeout_ms)
Sets the interface update timeout.
Definition peak_device_manager.hpp:1403
void SetDeviceUpdateTimeout(core::Timeout timeout_ms)
Sets the device update timeout.
Definition peak_device_manager.hpp:1413
DeviceLostCallbackHandle RegisterDeviceLostCallback(const DeviceLostCallback &callback)
Registers a callback for signaling a lost device.
Definition peak_device_manager.hpp:1483
std::vector< std::shared_ptr< const core::Interface > > Interfaces() const
Returns the found and opened interfaces.
Definition peak_device_manager.hpp:1425
DeviceLostCallback * DeviceLostCallbackHandle
Definition peak_device_manager.hpp:126
void UnregisterInterfaceFoundCallback(InterfaceFoundCallbackHandle callbackHandle)
Unregisters a interface found callback.
Definition peak_device_manager.hpp:1456
A collection of global library functions.
Definition peak_library.hpp:31
static std::vector< std::string > CollectCTIPaths()
Collects producer library paths found in the current environment.
Definition peak_environment_inspector.hpp:71
The base class for all exceptions thrown by the library.
Definition peak_exception.hpp:26
The exception thrown for signaling a feature is not available in the device.
Definition peak_exception.hpp:98
The exception thrown for signaling that the library was not initialized.
Definition peak_exception.hpp:124
static std::shared_ptr< ProducerLibrary > Open(const std::string &ctiPath)
Opens the given producer library (CTI).
Definition peak_producer_library.hpp:109
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition peak_system.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:32