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)
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);
951 errorCallback(
"[CTI - " + ctiPath +
"]: " + ex.what());
960 auto systemUpdateChildren = [&](
const std::shared_ptr<core::System>& system) {
963 system->UpdateInterfaces(interfaceUpdateTimeout_ms);
971 errorCallback(
"[System - " + system->Key() +
"]: " + ex.what());
980 constexpr auto launchPolicy = std::launch::async;
982 std::vector<std::future<void>> systemUpdateChildrenTasks;
985 for (
const auto& system : m_systems)
987 systemUpdateChildrenTasks.emplace_back(
988 std::async(launchPolicy, systemUpdateChildren, std::const_pointer_cast<core::System>(system)));
992 for (
const auto& system : allFoundSystems)
994 systemUpdateChildrenTasks.emplace_back(std::async(launchPolicy, systemUpdateChildren, system));
998 systemUpdateChildrenTasks.clear();
1001 for (
auto& keyValue : m_systemCallbackContainers)
1003 auto& container = keyValue.second;
1005 std::lock_guard<std::mutex> lock(container.mutex);
1007 allFoundInterfaces.reserve(allFoundInterfaces.size() + container.foundInterfaces.size());
1008 for (
const auto& foundInterface : container.foundInterfaces)
1010 AddInterfaceCallbacks(foundInterface);
1011 allFoundInterfaces.emplace_back(foundInterface);
1013 container.foundInterfaces.clear();
1015 allLostInterfaces.reserve(allLostInterfaces.size() + container.lostInterfaces.size());
1016 for (
const auto& lostInterface : container.lostInterfaces)
1018 auto interfaceIt = std::find_if(std::begin(m_interfaces), std::end(m_interfaces),
1019 [&lostInterface](
const auto& intf) {
return intf->Key() == lostInterface; });
1021 if (interfaceIt != std::end(m_interfaces))
1023 RemoveInterfaceCallbacks(std::const_pointer_cast<core::Interface>(*interfaceIt));
1026 allLostInterfaces.emplace_back(lostInterface);
1028 container.lostInterfaces.clear();
1031 auto interfaceUpdateChildren = [&deviceUpdateTimeout_ms, &errorCallback](
1032 const std::shared_ptr<core::Interface>& iface) {
1035 iface->UpdateDevices(deviceUpdateTimeout_ms);
1043 errorCallback(
"[Interface - " + iface->Key() +
"]: " + ex.what());
1052 std::vector<std::future<void>> interfaceUpdateChildrenTasks;
1055 for (
const auto& iface : m_interfaces)
1057 interfaceUpdateChildrenTasks.emplace_back(
1058 std::async(launchPolicy, interfaceUpdateChildren, std::const_pointer_cast<core::Interface>(iface)));
1062 for (
const auto& iface : allFoundInterfaces)
1064 interfaceUpdateChildrenTasks.emplace_back(std::async(launchPolicy, interfaceUpdateChildren, iface));
1068 interfaceUpdateChildrenTasks.clear();
1071 for (
auto& keyValue : m_interfaceCallbackContainers)
1073 auto& container = keyValue.second;
1074 std::lock_guard<std::mutex> lock(container.mutex);
1076 decltype(container.foundDevices) foundDevices;
1077 std::swap(container.foundDevices, foundDevices);
1078 allFoundDevices.reserve(allFoundDevices.size() + foundDevices.size());
1079 std::move(std::begin(foundDevices), std::end(foundDevices), std::back_inserter(allFoundDevices));
1081 decltype(container.lostDevices) lostDevices;
1082 std::swap(container.lostDevices, lostDevices);
1083 allLostDevices.reserve(allLostDevices.size() + lostDevices.size());
1084 std::move(std::begin(lostDevices), std::end(lostDevices), std::back_inserter(allLostDevices));
1089 std::lock(m_systemsMutex, m_interfacesMutex, m_devicesMutex);
1091 std::lock_guard<std::mutex> lockSystems(m_systemsMutex, std::adopt_lock);
1092 std::lock_guard<std::recursive_mutex> lockInterfaces(m_interfacesMutex, std::adopt_lock);
1093 std::lock_guard<std::recursive_mutex> lockDevices(m_devicesMutex, std::adopt_lock);
1095 m_systems.reserve(m_systems.size() + allFoundSystems.size());
1096 std::copy(std::begin(allFoundSystems), std::end(allFoundSystems), std::back_inserter(m_systems));
1098 for (
const auto& lostInterfaceKey : allLostInterfaces)
1100 m_interfaces.erase(std::remove_if(std::begin(m_interfaces), std::end(m_interfaces),
1101 [&lostInterfaceKey](
const auto& interfaceToCheck) {
1102 return interfaceToCheck->Key() == lostInterfaceKey;
1104 std::end(m_interfaces));
1106 m_interfaces.reserve(m_interfaces.size() + allFoundInterfaces.size());
1107 std::copy(std::begin(allFoundInterfaces), std::end(allFoundInterfaces), std::back_inserter(m_interfaces));
1109 for (
const auto& lostDeviceKey : allLostDevices)
1112 std::remove_if(std::begin(m_devices), std::end(m_devices),
1113 [&lostDeviceKey](
const auto& deviceToCheck) {
return deviceToCheck->Key() == lostDeviceKey; }),
1114 std::end(m_devices));
1116 m_devices.reserve(m_devices.size() + allFoundDevices.size());
1117 std::copy(std::begin(allFoundDevices), std::end(allFoundDevices), std::back_inserter(m_devices));
1121 for (
const auto& foundSystem : allFoundSystems)
1123 TriggerSystemFoundCallbacks(foundSystem);
1126 for (
const auto& foundInterface : allFoundInterfaces)
1128 TriggerInterfaceFoundCallbacks(foundInterface);
1131 for (
const auto& lostInterfaceKey : allLostInterfaces)
1133 TriggerInterfaceLostCallbacks(lostInterfaceKey);
1136 for (
const auto& foundDevice : allFoundDevices)
1138 TriggerDeviceFoundCallbacks(foundDevice);
1141 GetLostDevicesFromLostInterfaces(allLostInterfaces, allLostDevices);
1142 for (
const auto& lostDeviceKey : allLostDevices)
1144 TriggerDeviceLostCallbacks(lostDeviceKey);
1150 std::lock_guard<std::mutex> lockUpdate(m_updateMutex);
1155 CheckDevicesOpened();
1159 std::lock(m_systemsMutex, m_interfacesMutex, m_devicesMutex);
1161 std::lock_guard<std::mutex> lockSystem(m_systemsMutex, std::adopt_lock);
1162 std::lock_guard<std::recursive_mutex> lockInterfaces(m_interfacesMutex, std::adopt_lock);
1163 std::lock_guard<std::recursive_mutex> lockDevices(m_devicesMutex, std::adopt_lock);
1167 decltype(m_devices) lostDevicesList{};
1168 std::swap(m_devices, lostDevicesList);
1169 for (
const auto& lostDevice : lostDevicesList)
1171 TriggerDeviceLostCallbacks(lostDevice->Key());
1174 decltype(m_interfaces) lostInterfacesList{};
1175 std::swap(m_interfaces, lostInterfacesList);
1176 for (
const auto& lostInterface : lostInterfacesList)
1178 TriggerInterfaceLostCallbacks(lostInterface->Key());
1179 RemoveInterfaceCallbacks(std::const_pointer_cast<core::Interface>(lostInterface));
1182 decltype(m_systems) lostSystemsList{};
1183 std::swap(m_systems, lostSystemsList);
1184 for (
const auto& lostSystem : lostSystemsList)
1186 RemoveSystemCallbacks(std::const_pointer_cast<core::System>(lostSystem));
1189 m_producerLibraries.clear();
1193 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1195 m_systemCallbackContainers.clear();
1196 m_interfaceCallbackContainers.clear();
1200 std::lock_guard<std::mutex> lock(m_producerLibrariesToAddMutex);
1201 m_producerLibrariesToAdd.clear();
1205inline void DeviceManager::AddSystemCallbacks(
const std::shared_ptr<core::System>& addSystem)
1207 auto SystemKey = addSystem->Key();
1209 const auto foundCallbackHandle = addSystem->RegisterInterfaceFoundCallback(
1210 [
this, SystemKey](
const std::shared_ptr<core::InterfaceDescriptor>& foundInterface) {
1211 const auto systemIt = m_systemCallbackContainers.find(SystemKey);
1212 if (systemIt != std::end(m_systemCallbackContainers))
1216 auto& container = systemIt->second;
1217 std::lock_guard<std::mutex> lock(container.mutex);
1219 const auto openedInterface = foundInterface->OpenInterface();
1221 container.foundInterfaces.emplace_back(openedInterface);
1222 container.interfaceIdKeyMap.emplace(openedInterface->ID(), openedInterface->Key());
1224 catch (
const std::exception&)
1229 const auto lostCallbackHandle = addSystem->RegisterInterfaceLostCallback(
1230 [
this, SystemKey](
const std::string& lostInterfaceId) {
1231 const auto systemIt = m_systemCallbackContainers.find(SystemKey);
1232 if (systemIt != std::end(m_systemCallbackContainers))
1234 auto& container = systemIt->second;
1235 std::lock_guard<std::mutex> lock(container.mutex);
1237 const auto interfaceKeyIt = container.interfaceIdKeyMap.find(lostInterfaceId);
1238 if (interfaceKeyIt != std::end(container.interfaceIdKeyMap))
1240 container.lostInterfaces.emplace_back(interfaceKeyIt->second);
1241 container.interfaceIdKeyMap.erase(interfaceKeyIt);
1246 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1248 m_systemCallbackContainers.emplace(
1249 addSystem->Key(), SystemCallbackContainer{ foundCallbackHandle, lostCallbackHandle });
1252inline void DeviceManager::RemoveSystemCallbacks(
const std::shared_ptr<core::System>& removeSystem)
1254 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1256 const auto SystemKey = removeSystem->Key();
1258 const auto systemIt = m_systemCallbackContainers.find(SystemKey);
1260 if (systemIt != std::end(m_systemCallbackContainers))
1262 const auto& container = systemIt->second;
1264 std::lock_guard<std::mutex> lockContainer(container.mutex);
1266 removeSystem->UnregisterInterfaceFoundCallback(container.foundCallbackHandle);
1267 removeSystem->UnregisterInterfaceLostCallback(container.lostCallbackHandle);
1270 m_systemCallbackContainers.erase(systemIt);
1274inline void DeviceManager::AddInterfaceCallbacks(
const std::shared_ptr<core::Interface>& addInterface)
1276 auto InterfaceKey = addInterface->Key();
1278 const auto foundCallbackHandle = addInterface->RegisterDeviceFoundCallback(
1279 [
this, InterfaceKey](
const std::shared_ptr<core::DeviceDescriptor>& foundDevice) {
1280 const auto interfaceIt = m_interfaceCallbackContainers.find(InterfaceKey);
1282 if (interfaceIt != std::end(m_interfaceCallbackContainers))
1284 auto& container = interfaceIt->second;
1285 std::lock_guard<std::mutex> lock(container.mutex);
1287 container.foundDevices.emplace_back(foundDevice);
1288 container.deviceIdKeyMap.emplace(foundDevice->ID(), foundDevice->Key());
1292 foundDevice->RegisterDeviceOpenedCallback([
this](
const std::shared_ptr<core::Device>& openedDevice) {
1293 std::lock_guard<std::mutex> lock(m_openedDevicesMutex);
1296 m_openedDevices.erase(std::remove_if(std::begin(m_openedDevices), std::end(m_openedDevices),
1297 [](
const OpenedDevice& dev) {
return dev.device.expired(); }),
1298 std::end(m_openedDevices));
1301 OpenedDevice device;
1302 device.device = openedDevice;
1303 device.displayName = openedDevice->DisplayName();
1304 m_openedDevices.push_back(device);
1308 const auto lostCallbackHandle = addInterface->RegisterDeviceLostCallback(
1309 [
this, InterfaceKey](
const std::string& lostDeviceId) {
1310 const auto interfaceIt = m_interfaceCallbackContainers.find(InterfaceKey);
1312 if (interfaceIt != std::end(m_interfaceCallbackContainers))
1314 auto& container = interfaceIt->second;
1315 std::lock_guard<std::mutex> lock(container.mutex);
1317 const auto lostDeviceKeyIt = container.deviceIdKeyMap.find(lostDeviceId);
1318 if (lostDeviceKeyIt != std::end(container.deviceIdKeyMap))
1320 container.lostDevices.emplace_back(lostDeviceKeyIt->second);
1321 container.deviceIdKeyMap.erase(lostDeviceKeyIt);
1326 const auto reconnectedHandle = addInterface->RegisterDeviceReconnectedCallback(
1327 [
this](
const std::shared_ptr<core::DeviceDescriptor>& reconnectedDevice,
1328 core::DeviceReconnectInformation reconnectInformation) {
1329 return m_deviceReconnectedCallbackManager.TriggerCallbacks(reconnectedDevice, reconnectInformation);
1332 const auto disconnectedHandle = addInterface->RegisterDeviceDisconnectedCallback(
1333 [
this](
const std::shared_ptr<core::DeviceDescriptor>& disconnectedDevice) {
1334 return m_deviceDisconnectedCallbackManager.TriggerCallbacks(disconnectedDevice);
1337 const auto deviceListChangedHandle = addInterface->RegisterDeviceListChangedCallback(
1338 [
this]() {
return m_deviceListChangedCallbackManager.TriggerCallbacks(); });
1340 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1342 m_interfaceCallbackContainers.emplace(addInterface->Key(),
1343 InterfaceCallbackContainer{
1344 foundCallbackHandle, lostCallbackHandle, disconnectedHandle, reconnectedHandle, deviceListChangedHandle });
1347inline void DeviceManager::RemoveInterfaceCallbacks(
const std::shared_ptr<core::Interface>& removeInterface)
1349 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1351 const auto interfaceIt = m_interfaceCallbackContainers.find(removeInterface->Key());
1353 if (interfaceIt != std::end(m_interfaceCallbackContainers))
1355 const auto& container = interfaceIt->second;
1357 std::lock_guard<std::mutex> lockContainer(container.mutex);
1359 removeInterface->UnregisterDeviceFoundCallback(container.foundCallbackHandle);
1360 removeInterface->UnregisterDeviceLostCallback(container.lostCallbackHandle);
1361 removeInterface->UnregisterDeviceDisconnectedCallback(container.disconnectedCallbackHandle);
1362 removeInterface->UnregisterDeviceReconnectedCallback(container.reconnectedCallbackHandle);
1363 removeInterface->UnregisterDeviceListChangedCallback(container.deviceListChangedCallbackHandle);
1365 m_interfaceCallbackContainers.erase(interfaceIt);
1369inline void DeviceManager::CheckDevicesOpened()
const
1371 size_t openDevicesFound = 0;
1372 std::string deviceDisplayNames;
1375 std::lock_guard<std::mutex> lock(m_openedDevicesMutex);
1377 for (
const auto& openedDevice : m_openedDevices)
1379 auto openDevice = openedDevice.device.lock();
1383 deviceDisplayNames += openedDevice.displayName;
1384 deviceDisplayNames +=
" ";
1389 if (openDevicesFound > 0)
1391 throw core::InternalErrorException(
1392 "Can't reset the device manager while there are devices open. The following devices ("
1393 + std::to_string(openDevicesFound) +
") are open: " + deviceDisplayNames);
1399 return m_interfaceUpdateTimeout_ms;
1404 m_interfaceUpdateTimeout_ms = timeout_ms;
1409 return m_deviceUpdateTimeout_ms;
1414 m_deviceUpdateTimeout_ms = timeout_ms;
1419 std::lock_guard<std::mutex> lock(m_systemsMutex);
1426 std::lock_guard<std::recursive_mutex> lock(m_interfacesMutex);
1428 return m_interfaces;
1433 std::lock_guard<std::recursive_mutex> lock(m_devicesMutex);
1441 return m_systemFoundCallbackManager.RegisterCallback(callback);
1446 m_systemFoundCallbackManager.UnregisterCallback(callbackHandle);
1452 return m_interfaceFoundCallbackManager.RegisterCallback(callback);
1457 m_interfaceFoundCallbackManager.UnregisterCallback(callbackHandle);
1463 return m_interfaceLostCallbackManager.RegisterCallback(callback);
1468 m_interfaceLostCallbackManager.UnregisterCallback(callbackHandle);
1474 return m_deviceFoundCallbackManager.RegisterCallback(callback);
1479 m_deviceFoundCallbackManager.UnregisterCallback(callbackHandle);
1485 return m_deviceLostCallbackManager.RegisterCallback(callback);
1490 m_deviceLostCallbackManager.UnregisterCallback(callbackHandle);
1496 return m_deviceDisconnectedCallbackManager.RegisterCallback(callback);
1502 m_deviceDisconnectedCallbackManager.UnregisterCallback(callbackHandle);
1508 return m_deviceReconnectedCallbackManager.RegisterCallback(callback);
1514 m_deviceReconnectedCallbackManager.UnregisterCallback(callbackHandle);
1520 return m_deviceListChangedCallbackManager.RegisterCallback(callback);
1526 m_deviceListChangedCallbackManager.UnregisterCallback(callbackHandle);
1529inline void DeviceManager::GetLostDevicesFromLostInterfaces(
1530 const std::vector<std::string>& lostInterfaces, std::vector<std::string>& lostDevices)
1532 std::lock_guard<std::recursive_mutex> lock(m_devicesMutex);
1534 for (
const auto& device : m_devices)
1536 auto resultIt = std::find(
1537 std::begin(lostInterfaces), std::end(lostInterfaces), device->ParentInterface()->Key());
1538 if (resultIt != std::end(lostInterfaces))
1540 lostDevices.emplace_back(device->Key());
1545inline void DeviceManager::TriggerSystemFoundCallbacks(
const std::shared_ptr<const core::System>& foundSystem)
const
1547 m_systemFoundCallbackManager.TriggerCallbacks(foundSystem);
1550inline void DeviceManager::TriggerInterfaceFoundCallbacks(
1551 const std::shared_ptr<const core::Interface>& foundInterface)
const
1553 m_interfaceFoundCallbackManager.TriggerCallbacks(foundInterface);
1556inline void DeviceManager::TriggerInterfaceLostCallbacks(
const std::string& lostInterfaceKey)
const
1558 m_interfaceLostCallbackManager.TriggerCallbacks(lostInterfaceKey);
1561inline void DeviceManager::TriggerDeviceFoundCallbacks(
const std::shared_ptr<core::DeviceDescriptor>& foundDevice)
const
1563 m_deviceFoundCallbackManager.TriggerCallbacks(foundDevice);
1566inline void DeviceManager::TriggerDeviceLostCallbacks(
const std::string& lostDeviceKey)
const
1568 m_deviceLostCallbackManager.TriggerCallbacks(lostDeviceKey);
core::Timeout InterfaceUpdateTimeout() const
Returns the interface update timeout.
Definition peak_device_manager.hpp:1397
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:1449
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &disconnectedDevice)> DeviceDisconnectedCallback
Definition peak_device_manager.hpp:129
void UnregisterDeviceFoundCallback(DeviceFoundCallbackHandle callbackHandle)
Unregisters a device found callback.
Definition peak_device_manager.hpp:1477
void UnregisterDeviceDisconnectedCallback(DeviceDisconnectedCallbackHandle callbackHandle)
Unregisters a device disconnected callback.
Definition peak_device_manager.hpp:1499
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:1417
DeviceReconnectedCallback * DeviceReconnectedCallbackHandle
Definition peak_device_manager.hpp:141
void UnregisterInterfaceLostCallback(InterfaceLostCallbackHandle callbackHandle)
Unregisters an interface lost callback.
Definition peak_device_manager.hpp:1466
void Reset(ResetPolicy resetPolicy=ResetPolicy::ErrorOnOpenDevices)
Resets the DeviceManager.
Definition peak_device_manager.hpp:1148
UpdatePolicy
Enum holding the possible update policies.
Definition peak_device_manager.hpp:80
@ DontScanEnvironmentForProducerLibraries
Definition peak_device_manager.hpp:85
@ ScanEnvironmentForProducerLibraries
Definition peak_device_manager.hpp:82
ResetPolicy
Enum holding the possible reset policies.
Definition peak_device_manager.hpp:90
@ ErrorOnOpenDevices
Definition peak_device_manager.hpp:92
@ IgnoreOpenDevices
Definition peak_device_manager.hpp:95
friend class Library
Definition peak_device_manager.hpp:569
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition peak_device_manager.hpp:108
void UnregisterDeviceLostCallback(DeviceLostCallbackHandle callbackHandle)
Unregisters a device lost callback.
Definition peak_device_manager.hpp:1488
core::Timeout DeviceUpdateTimeout() const
Returns the device update timeout.
Definition peak_device_manager.hpp:1407
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:1505
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:1523
std::vector< std::shared_ptr< core::DeviceDescriptor > > Devices() const
Returns the found devices.
Definition peak_device_manager.hpp:1431
std::function< void(const std::string &lostDeviceKey)> DeviceLostCallback
Definition peak_device_manager.hpp:123
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &reconnectedDevice, core::DeviceReconnectInformation reconnectInformation)> DeviceReconnectedCallback
Definition peak_device_manager.hpp:136
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:1438
InterfaceLostCallbackHandle RegisterInterfaceLostCallback(const InterfaceLostCallback &callback)
Registers a callback for signaling a lost interface.
Definition peak_device_manager.hpp:1460
DeviceDisconnectedCallbackHandle RegisterDeviceDisconnectedCallback(const DeviceDisconnectedCallback &callback)
Registers a callback for signaling a disconnected device.
Definition peak_device_manager.hpp:1493
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:1471
DeviceFoundCallback * DeviceFoundCallbackHandle
Definition peak_device_manager.hpp:120
void UnregisterDeviceReconnectedCallback(DeviceReconnectedCallbackHandle callbackHandle)
Unregisters a device reconnected callback.
Definition peak_device_manager.hpp:1511
DeviceListChangedCallbackHandle RegisterDeviceListChangedCallback(const DeviceListChangedCallback &callback)
Registers a callback for signaling a change in the device list.
Definition peak_device_manager.hpp:1517
DeviceListChangedCallback * DeviceListChangedCallbackHandle
Definition peak_device_manager.hpp:147
void UnregisterSystemFoundCallback(SystemFoundCallbackHandle callbackHandle)
Unregisters a system found callback.
Definition peak_device_manager.hpp:1444
void SetInterfaceUpdateTimeout(core::Timeout timeout_ms)
Sets the interface update timeout.
Definition peak_device_manager.hpp:1402
void SetDeviceUpdateTimeout(core::Timeout timeout_ms)
Sets the device update timeout.
Definition peak_device_manager.hpp:1412
DeviceLostCallbackHandle RegisterDeviceLostCallback(const DeviceLostCallback &callback)
Registers a callback for signaling a lost device.
Definition peak_device_manager.hpp:1482
std::vector< std::shared_ptr< const core::Interface > > Interfaces() const
Returns the found and opened interfaces.
Definition peak_device_manager.hpp:1424
DeviceLostCallback * DeviceLostCallbackHandle
Definition peak_device_manager.hpp:126
void UnregisterInterfaceFoundCallback(InterfaceFoundCallbackHandle callbackHandle)
Unregisters a interface found callback.
Definition peak_device_manager.hpp:1455
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:27
The exception thrown for signaling that a feature is not available in the device.
Definition peak_exception.hpp:167
The exception thrown for signaling that the library was not initialized.
Definition peak_exception.hpp:193
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:33