15#include <peak/generic/peak_t_callback_manager.hpp>
26#include <unordered_map>
27#include <unordered_set>
97 using SystemFoundCallback = std::function<void(
const std::shared_ptr<const core::System>& foundSystem)>;
115 using DeviceFoundCallback = std::function<void(
const std::shared_ptr<core::DeviceDescriptor>& foundDevice)>;
128 std::function<void(
const std::shared_ptr<core::DeviceDescriptor>& disconnectedDevice)>;
135 std::function<void(
const std::shared_ptr<core::DeviceDescriptor>& reconnectedDevice,
391 template <
typename Pred>
661 void GetLostDevicesFromLostInterfaces(
662 const std::vector<std::string>& lostInterfaces, std::vector<std::string>& lostDevices);
664 void TriggerSystemFoundCallbacks(
const std::shared_ptr<const core::System>& foundSystem)
const;
665 void TriggerInterfaceFoundCallbacks(
const std::shared_ptr<const core::Interface>& foundInterface)
const;
666 void TriggerInterfaceLostCallbacks(
const std::string& lostInterfaceKey)
const;
667 void TriggerDeviceFoundCallbacks(
const std::shared_ptr<core::DeviceDescriptor>& foundDevice)
const;
668 void TriggerDeviceLostCallbacks(
const std::string& lostDeviceKey)
const;
670 void AddSystemCallbacks(
const std::shared_ptr<core::System>& addSystem);
671 void RemoveSystemCallbacks(
const std::shared_ptr<core::System>& removeSystem);
672 void AddInterfaceCallbacks(
const std::shared_ptr<core::Interface>& addInterface);
673 void RemoveInterfaceCallbacks(
const std::shared_ptr<core::Interface>& removeInterface);
675 void CheckDevicesOpened()
const;
677 std::mutex m_updateMutex;
678 std::atomic<core::Timeout> m_interfaceUpdateTimeout_ms{ 100 };
679 std::atomic<core::Timeout> m_deviceUpdateTimeout_ms{ 300 };
681 std::vector<std::string> m_producerLibrariesToAdd;
683 std::unordered_map<std::string, std::shared_ptr<core::ProducerLibrary>> m_producerLibraries;
684 std::vector<std::shared_ptr<const core::System>> m_systems;
685 std::vector<std::shared_ptr<const core::Interface>> m_interfaces;
686 std::vector<std::shared_ptr<core::DeviceDescriptor>> m_devices;
688 struct SystemCallbackContainer
690 SystemCallbackContainer(
692 : foundCallbackHandle(foundHandle)
693 , lostCallbackHandle(lostHandle)
695 ~SystemCallbackContainer() =
default;
696 SystemCallbackContainer(
const SystemCallbackContainer&) =
delete;
697 SystemCallbackContainer& operator=(
const SystemCallbackContainer&) =
delete;
699 SystemCallbackContainer(SystemCallbackContainer&& a)
noexcept
701 std::lock_guard<std::mutex> lock(a.mutex);
703 std::swap(foundCallbackHandle, a.foundCallbackHandle);
704 std::swap(lostCallbackHandle, a.lostCallbackHandle);
707 SystemCallbackContainer& operator=(SystemCallbackContainer&& a)
noexcept
709 std::lock(a.mutex, mutex);
711 std::lock_guard<std::mutex> lock1(a.mutex, std::adopt_lock);
712 std::lock_guard<std::mutex> lock2(mutex, std::adopt_lock);
714 std::swap(foundCallbackHandle, a.foundCallbackHandle);
715 std::swap(lostCallbackHandle, a.lostCallbackHandle);
720 mutable std::mutex mutex;
724 std::vector<std::shared_ptr<core::Interface>> foundInterfaces;
725 std::vector<std::string> lostInterfaces;
729 std::unordered_map<std::string, std::string> interfaceIdKeyMap;
732 struct InterfaceCallbackContainer
737 : foundCallbackHandle(foundHandle)
738 , lostCallbackHandle(lostHandle)
739 , disconnectedCallbackHandle(disconnectedHandle)
740 , reconnectedCallbackHandle(reconnectedHandle)
741 , deviceListChangedCallbackHandle(listChangedHandle)
744 ~InterfaceCallbackContainer() =
default;
745 InterfaceCallbackContainer(
const InterfaceCallbackContainer&) =
delete;
746 InterfaceCallbackContainer& operator=(
const InterfaceCallbackContainer&) =
delete;
748 InterfaceCallbackContainer(InterfaceCallbackContainer&& a)
noexcept
750 std::lock_guard<std::mutex> lock(a.mutex);
752 std::swap(foundCallbackHandle, a.foundCallbackHandle);
753 std::swap(lostCallbackHandle, a.lostCallbackHandle);
754 std::swap(disconnectedCallbackHandle, a.disconnectedCallbackHandle);
755 std::swap(reconnectedCallbackHandle, a.reconnectedCallbackHandle);
756 std::swap(deviceListChangedCallbackHandle, a.deviceListChangedCallbackHandle);
759 InterfaceCallbackContainer& operator=(InterfaceCallbackContainer&& a)
noexcept
761 std::lock(a.mutex, mutex);
763 std::lock_guard<std::mutex> lock1(a.mutex, std::adopt_lock);
764 std::lock_guard<std::mutex> lock2(mutex, std::adopt_lock);
766 std::swap(foundCallbackHandle, a.foundCallbackHandle);
767 std::swap(lostCallbackHandle, a.lostCallbackHandle);
768 std::swap(disconnectedCallbackHandle, a.disconnectedCallbackHandle);
769 std::swap(reconnectedCallbackHandle, a.reconnectedCallbackHandle);
770 std::swap(deviceListChangedCallbackHandle, a.deviceListChangedCallbackHandle);
775 mutable std::mutex mutex;
782 std::vector<std::shared_ptr<core::DeviceDescriptor>> foundDevices;
783 std::vector<std::string> lostDevices;
787 std::unordered_map<std::string, std::string> deviceIdKeyMap;
790 mutable std::mutex m_callbackContainerMutex;
791 std::unordered_map<std::string, SystemCallbackContainer> m_systemCallbackContainers;
792 std::unordered_map<std::string, InterfaceCallbackContainer> m_interfaceCallbackContainers;
796 std::weak_ptr<core::Device> device{};
797 std::string displayName;
799 std::vector<OpenedDevice> m_openedDevices;
801 core::TTriggerCallbackManager<SystemFoundCallbackHandle, SystemFoundCallback> m_systemFoundCallbackManager;
802 core::TTriggerCallbackManager<InterfaceFoundCallbackHandle, InterfaceFoundCallback> m_interfaceFoundCallbackManager;
803 core::TTriggerCallbackManager<InterfaceLostCallbackHandle, InterfaceLostCallback> m_interfaceLostCallbackManager;
804 core::TTriggerCallbackManager<DeviceFoundCallbackHandle, DeviceFoundCallback> m_deviceFoundCallbackManager;
805 core::TTriggerCallbackManager<DeviceLostCallbackHandle, DeviceLostCallback> m_deviceLostCallbackManager;
806 core::TTriggerCallbackManager<DeviceDisconnectedCallbackHandle, DeviceDisconnectedCallback>
807 m_deviceDisconnectedCallbackManager;
808 core::TTriggerCallbackManager<DeviceReconnectedCallbackHandle, DeviceReconnectedCallback>
809 m_deviceReconnectedCallbackManager;
810 core::TTriggerCallbackManager<DeviceListChangedCallbackHandle, DeviceListChangedCallback>
811 m_deviceListChangedCallbackManager;
813 std::mutex m_producerLibrariesToAddMutex;
814 mutable std::mutex m_systemsMutex;
815 mutable std::recursive_mutex m_interfacesMutex;
816 mutable std::recursive_mutex m_devicesMutex;
817 mutable std::mutex m_openedDevicesMutex;
820 template <
typename T,
821 typename std::enable_if<(noexcept(std::declval<T>()())
822 || (std::is_pointer<typename std::decay<T>::type>::value && __cplusplus < 201703L)
825 && std::is_same<void, decltype(std::declval<T>()())>::value && std::is_move_constructible<T>::value
826 && std::is_nothrow_move_constructible<T>::value,
829 template <
typename T>
836 explicit RAIIGuard(T&& fun) noexcept
837 : m_fun(std::forward<T>(fun))
839 RAIIGuard(RAIIGuard
const&) =
delete;
840 RAIIGuard(RAIIGuard&& other) noexcept
841 : m_fun(std::move(other.m_fun))
843 ~RAIIGuard() noexcept
848 template <
typename T>
849 static RAIIGuard<T> makeRAIIGuard(T&& fun)
noexcept
851 return { std::forward<T>(fun) };
854 template <
typename T = DeviceManager>
855 class LifetimeTracker
858 static void Create();
859 static void Destroy();
863 static T* m_instance;
864 static std::mutex m_mtx;
865 static size_t m_initializedCount;
876T* DeviceManager::LifetimeTracker<T>::m_instance = {};
879std::mutex DeviceManager::LifetimeTracker<T>::m_mtx = {};
882size_t DeviceManager::LifetimeTracker<T>::m_initializedCount = {};
884template <
typename Pred>
887 using ReturnType =
decltype(predicate(std::declval<
const std::shared_ptr<core::DeviceDescriptor>&>()));
888 static_assert(std::is_convertible<ReturnType, bool>::value,
"Predicate result must be convertible to bool!");
890 const auto devices =
Devices();
891 std::vector<std::shared_ptr<core::DeviceDescriptor>> result;
893 std::copy_if(devices.begin(), devices.end(), std::back_inserter(result), predicate);
898void DeviceManager::LifetimeTracker<T>::Create()
900 std::lock_guard<
decltype(m_mtx)> lock{ m_mtx };
904 m_instance =
new T{};
907 m_initializedCount++;
911void DeviceManager::LifetimeTracker<T>::Destroy()
913 std::lock_guard<
decltype(m_mtx)> lock{ m_mtx };
915 if (0 == m_initializedCount)
920 m_initializedCount--;
922 if (0 == m_initializedCount)
927 m_instance =
nullptr;
933T& DeviceManager::LifetimeTracker<T>::Instance()
935 if (m_instance ==
nullptr)
938 "Library is not initialized. Call peak::Library::Initalize() first!");
945 return LifetimeTracker<>::Instance();
948inline DeviceManager::~DeviceManager()
954 catch (
const std::exception&)
960 std::lock_guard<std::mutex> lock(m_producerLibrariesToAddMutex);
962 m_producerLibrariesToAdd.emplace_back(ctiPath);
969 auto it = std::find_if(systems.begin(), systems.end(),
970 [ctiPath](
const std::shared_ptr<const core::System>& system) { return system->CTIFullPath() == ctiPath; });
971 if (it != systems.end())
983 std::lock_guard<std::mutex> lockUpdate(m_updateMutex);
985 std::lock(m_systemsMutex, m_interfacesMutex, m_devicesMutex);
987 std::lock_guard<std::mutex> lockSystem(m_systemsMutex, std::adopt_lock);
988 std::lock_guard<std::recursive_mutex> lockInterfaces(m_interfacesMutex, std::adopt_lock);
989 std::lock_guard<std::recursive_mutex> lockDevices(m_devicesMutex, std::adopt_lock);
991 decltype(m_devices) lostDevicesList{};
992 decltype(m_interfaces) lostInterfacesList{};
993 decltype(m_systems) lostSystemsList{};
995 m_devices.erase(std::remove_if(m_devices.begin(), m_devices.end(),
996 [&library, &lostDevicesList](
const std::shared_ptr<core::DeviceDescriptor>& device) {
997 if (device == nullptr)
1001 auto deviceLibrary = device->ParentInterface()->ParentSystem()->ParentLibrary();
1002 if (library == deviceLibrary)
1004 lostDevicesList.emplace_back(device);
1011 m_interfaces.erase(std::remove_if(m_interfaces.begin(), m_interfaces.end(),
1012 [&library, &lostInterfacesList](
const std::shared_ptr<const core::Interface>& intf) {
1013 if (intf == nullptr)
1017 auto intfLibrary = intf->ParentSystem()->ParentLibrary();
1018 if (library == intfLibrary)
1020 lostInterfacesList.emplace_back(intf);
1025 m_interfaces.end());
1027 m_systems.erase(std::remove_if(m_systems.begin(), m_systems.end(),
1028 [&library, &lostSystemsList](
const std::shared_ptr<const core::System>& system) {
1029 if (system ==
nullptr)
1033 auto systemLibrary = system->ParentLibrary();
1034 if (library == systemLibrary)
1036 lostSystemsList.emplace_back(system);
1043 for (
const auto& lostDevice : lostDevicesList)
1045 TriggerDeviceLostCallbacks(lostDevice->Key());
1048 for (
const auto& lostInterface : lostInterfacesList)
1050 TriggerInterfaceLostCallbacks(lostInterface->Key());
1051 RemoveInterfaceCallbacks(std::const_pointer_cast<core::Interface>(lostInterface));
1054 for (
const auto& lostSystem : lostSystemsList)
1056 RemoveSystemCallbacks(std::const_pointer_cast<core::System>(lostSystem));
1059 for (
auto it = m_producerLibraries.begin(); it != m_producerLibraries.end();)
1061 if (it->second == library)
1063 it = m_producerLibraries.erase(it);
1071 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1073 for (
const auto& lostInterface : lostInterfacesList)
1075 m_interfaceCallbackContainers.erase(lostInterface->Key());
1078 for (
const auto& lostSystem : lostSystemsList)
1080 m_systemCallbackContainers.erase(lostSystem->Key());
1086 std::lock(m_updateMutex, m_systemsMutex);
1088 std::lock_guard<std::mutex> lockUpdate(m_updateMutex, std::adopt_lock);
1090 std::shared_ptr<core::System> newSystem{};
1092 std::lock_guard<std::mutex> lockSystems(m_systemsMutex, std::adopt_lock);
1094 const auto systemDescriptor = library->System();
1095 const auto ctiPath = systemDescriptor->CTIFullPath();
1098 if (m_producerLibraries.count(ctiPath) == 0)
1100 m_producerLibraries.emplace(ctiPath, library);
1102 newSystem = systemDescriptor->IsOpened() ? systemDescriptor->OpenedSystem() :
1103 systemDescriptor->OpenSystem();
1104 AddSystemCallbacks(newSystem);
1105 m_systems.emplace_back(newSystem);
1111 TriggerSystemFoundCallbacks(newSystem);
1118 std::lock_guard<std::mutex> lockUpdate(m_updateMutex);
1120 std::vector<std::shared_ptr<core::System>> allFoundSystems;
1121 std::vector<std::shared_ptr<core::Interface>> allFoundInterfaces;
1122 std::vector<std::string> allLostInterfaces;
1123 std::vector<std::shared_ptr<core::DeviceDescriptor>> allFoundDevices;
1124 std::vector<std::string> allLostDevices;
1126 auto interfaceUpdateTimeout_ms = m_interfaceUpdateTimeout_ms.load();
1127 auto deviceUpdateTimeout_ms = m_deviceUpdateTimeout_ms.load();
1129 std::vector<std::string> ctiPaths;
1138 std::lock_guard<std::mutex> lock(m_producerLibrariesToAddMutex);
1140 ctiPaths.reserve(ctiPaths.size() + m_producerLibrariesToAdd.size());
1142 std::begin(m_producerLibrariesToAdd), std::end(m_producerLibrariesToAdd), std::back_inserter(ctiPaths));
1144 m_producerLibrariesToAdd.clear();
1147 for (
const auto& ctiPath : ctiPaths)
1149 if (m_producerLibraries.count(ctiPath) == 0)
1154 auto system = producerLibrary->System()->OpenSystem();
1156 AddSystemCallbacks(system);
1158 m_producerLibraries.emplace(ctiPath, producerLibrary);
1159 allFoundSystems.emplace_back(system);
1176 errorCallback(
"[CTI - " + ctiPath +
"]: " + ex.what());
1185 auto systemUpdateChildren = [&](
const std::shared_ptr<core::System>& system) {
1188 system->UpdateInterfaces(interfaceUpdateTimeout_ms);
1196 errorCallback(
"[System - " + system->Key() +
"]: " + ex.what());
1205 constexpr auto launchPolicy = std::launch::async;
1207 std::vector<std::future<void>> systemUpdateChildrenTasks;
1210 for (
const auto& system : m_systems)
1212 systemUpdateChildrenTasks.emplace_back(
1213 std::async(launchPolicy, systemUpdateChildren, std::const_pointer_cast<core::System>(system)));
1217 for (
const auto& system : allFoundSystems)
1219 systemUpdateChildrenTasks.emplace_back(std::async(launchPolicy, systemUpdateChildren, system));
1223 systemUpdateChildrenTasks.clear();
1226 for (
auto& keyValue : m_systemCallbackContainers)
1228 auto& container = keyValue.second;
1230 std::lock_guard<std::mutex> lock(container.mutex);
1232 allFoundInterfaces.reserve(allFoundInterfaces.size() + container.foundInterfaces.size());
1233 for (
const auto& foundInterface : container.foundInterfaces)
1235 AddInterfaceCallbacks(foundInterface);
1236 allFoundInterfaces.emplace_back(foundInterface);
1238 container.foundInterfaces.clear();
1240 allLostInterfaces.reserve(allLostInterfaces.size() + container.lostInterfaces.size());
1241 for (
const auto& lostInterface : container.lostInterfaces)
1243 auto interfaceIt = std::find_if(std::begin(m_interfaces), std::end(m_interfaces),
1244 [&lostInterface](
const auto& intf) {
return intf->Key() == lostInterface; });
1246 if (interfaceIt != std::end(m_interfaces))
1248 RemoveInterfaceCallbacks(std::const_pointer_cast<core::Interface>(*interfaceIt));
1251 allLostInterfaces.emplace_back(lostInterface);
1253 container.lostInterfaces.clear();
1256 auto interfaceUpdateChildren = [&deviceUpdateTimeout_ms, &errorCallback](
1257 const std::shared_ptr<core::Interface>& iface) {
1260 iface->UpdateDevices(deviceUpdateTimeout_ms);
1268 errorCallback(
"[Interface - " + iface->Key() +
"]: " + ex.what());
1277 std::vector<std::future<void>> interfaceUpdateChildrenTasks;
1280 for (
const auto& iface : m_interfaces)
1282 interfaceUpdateChildrenTasks.emplace_back(
1283 std::async(launchPolicy, interfaceUpdateChildren, std::const_pointer_cast<core::Interface>(iface)));
1287 for (
const auto& iface : allFoundInterfaces)
1289 interfaceUpdateChildrenTasks.emplace_back(std::async(launchPolicy, interfaceUpdateChildren, iface));
1293 interfaceUpdateChildrenTasks.clear();
1296 for (
auto& keyValue : m_interfaceCallbackContainers)
1298 auto& container = keyValue.second;
1299 std::lock_guard<std::mutex> lock(container.mutex);
1301 decltype(container.foundDevices) foundDevices;
1302 std::swap(container.foundDevices, foundDevices);
1303 allFoundDevices.reserve(allFoundDevices.size() + foundDevices.size());
1304 std::move(std::begin(foundDevices), std::end(foundDevices), std::back_inserter(allFoundDevices));
1306 decltype(container.lostDevices) lostDevices;
1307 std::swap(container.lostDevices, lostDevices);
1308 allLostDevices.reserve(allLostDevices.size() + lostDevices.size());
1309 std::move(std::begin(lostDevices), std::end(lostDevices), std::back_inserter(allLostDevices));
1314 std::lock(m_systemsMutex, m_interfacesMutex, m_devicesMutex);
1316 std::lock_guard<std::mutex> lockSystems(m_systemsMutex, std::adopt_lock);
1317 std::lock_guard<std::recursive_mutex> lockInterfaces(m_interfacesMutex, std::adopt_lock);
1318 std::lock_guard<std::recursive_mutex> lockDevices(m_devicesMutex, std::adopt_lock);
1320 m_systems.reserve(m_systems.size() + allFoundSystems.size());
1321 std::copy(std::begin(allFoundSystems), std::end(allFoundSystems), std::back_inserter(m_systems));
1323 for (
const auto& lostInterfaceKey : allLostInterfaces)
1325 m_interfaces.erase(std::remove_if(std::begin(m_interfaces), std::end(m_interfaces),
1326 [&lostInterfaceKey](
const auto& interfaceToCheck) {
1327 return interfaceToCheck->Key() == lostInterfaceKey;
1329 std::end(m_interfaces));
1331 m_interfaces.reserve(m_interfaces.size() + allFoundInterfaces.size());
1332 std::copy(std::begin(allFoundInterfaces), std::end(allFoundInterfaces), std::back_inserter(m_interfaces));
1334 for (
const auto& lostDeviceKey : allLostDevices)
1337 std::remove_if(std::begin(m_devices), std::end(m_devices),
1338 [&lostDeviceKey](
const auto& deviceToCheck) {
return deviceToCheck->Key() == lostDeviceKey; }),
1339 std::end(m_devices));
1341 m_devices.reserve(m_devices.size() + allFoundDevices.size());
1342 std::copy(std::begin(allFoundDevices), std::end(allFoundDevices), std::back_inserter(m_devices));
1346 for (
const auto& foundSystem : allFoundSystems)
1348 TriggerSystemFoundCallbacks(foundSystem);
1351 for (
const auto& foundInterface : allFoundInterfaces)
1353 TriggerInterfaceFoundCallbacks(foundInterface);
1356 for (
const auto& lostInterfaceKey : allLostInterfaces)
1358 TriggerInterfaceLostCallbacks(lostInterfaceKey);
1361 for (
const auto& foundDevice : allFoundDevices)
1363 TriggerDeviceFoundCallbacks(foundDevice);
1366 GetLostDevicesFromLostInterfaces(allLostInterfaces, allLostDevices);
1367 for (
const auto& lostDeviceKey : allLostDevices)
1369 TriggerDeviceLostCallbacks(lostDeviceKey);
1375 std::lock_guard<std::mutex> lockUpdate(m_updateMutex);
1380 CheckDevicesOpened();
1384 std::lock(m_systemsMutex, m_interfacesMutex, m_devicesMutex);
1386 std::lock_guard<std::mutex> lockSystem(m_systemsMutex, std::adopt_lock);
1387 std::lock_guard<std::recursive_mutex> lockInterfaces(m_interfacesMutex, std::adopt_lock);
1388 std::lock_guard<std::recursive_mutex> lockDevices(m_devicesMutex, std::adopt_lock);
1392 decltype(m_devices) lostDevicesList{};
1393 std::swap(m_devices, lostDevicesList);
1394 for (
const auto& lostDevice : lostDevicesList)
1396 TriggerDeviceLostCallbacks(lostDevice->Key());
1399 decltype(m_interfaces) lostInterfacesList{};
1400 std::swap(m_interfaces, lostInterfacesList);
1401 for (
const auto& lostInterface : lostInterfacesList)
1403 TriggerInterfaceLostCallbacks(lostInterface->Key());
1404 RemoveInterfaceCallbacks(std::const_pointer_cast<core::Interface>(lostInterface));
1407 decltype(m_systems) lostSystemsList{};
1408 std::swap(m_systems, lostSystemsList);
1409 for (
const auto& lostSystem : lostSystemsList)
1411 RemoveSystemCallbacks(std::const_pointer_cast<core::System>(lostSystem));
1414 m_producerLibraries.clear();
1418 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1420 m_systemCallbackContainers.clear();
1421 m_interfaceCallbackContainers.clear();
1425 std::lock_guard<std::mutex> lock(m_producerLibrariesToAddMutex);
1426 m_producerLibrariesToAdd.clear();
1430inline void DeviceManager::AddSystemCallbacks(
const std::shared_ptr<core::System>& addSystem)
1432 auto SystemKey = addSystem->Key();
1434 const auto foundCallbackHandle = addSystem->RegisterInterfaceFoundCallback(
1435 [
this, SystemKey](
const std::shared_ptr<core::InterfaceDescriptor>& foundInterface) {
1436 const auto systemIt = m_systemCallbackContainers.find(SystemKey);
1437 if (systemIt != std::end(m_systemCallbackContainers))
1441 auto& container = systemIt->second;
1442 std::lock_guard<std::mutex> lock(container.mutex);
1444 const auto openedInterface = foundInterface->OpenInterface();
1446 container.foundInterfaces.emplace_back(openedInterface);
1447 container.interfaceIdKeyMap.emplace(openedInterface->ID(), openedInterface->Key());
1449 catch (
const std::exception&)
1454 const auto lostCallbackHandle = addSystem->RegisterInterfaceLostCallback(
1455 [
this, SystemKey](
const std::string& lostInterfaceId) {
1456 const auto systemIt = m_systemCallbackContainers.find(SystemKey);
1457 if (systemIt != std::end(m_systemCallbackContainers))
1459 auto& container = systemIt->second;
1460 std::lock_guard<std::mutex> lock(container.mutex);
1462 const auto interfaceKeyIt = container.interfaceIdKeyMap.find(lostInterfaceId);
1463 if (interfaceKeyIt != std::end(container.interfaceIdKeyMap))
1465 container.lostInterfaces.emplace_back(interfaceKeyIt->second);
1466 container.interfaceIdKeyMap.erase(interfaceKeyIt);
1471 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1473 m_systemCallbackContainers.emplace(
1474 addSystem->Key(), SystemCallbackContainer{ foundCallbackHandle, lostCallbackHandle });
1477inline void DeviceManager::RemoveSystemCallbacks(
const std::shared_ptr<core::System>& removeSystem)
1479 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1481 const auto SystemKey = removeSystem->Key();
1483 const auto systemIt = m_systemCallbackContainers.find(SystemKey);
1485 if (systemIt != std::end(m_systemCallbackContainers))
1487 const auto& container = systemIt->second;
1489 std::lock_guard<std::mutex> lockContainer(container.mutex);
1491 removeSystem->UnregisterInterfaceFoundCallback(container.foundCallbackHandle);
1492 removeSystem->UnregisterInterfaceLostCallback(container.lostCallbackHandle);
1495 m_systemCallbackContainers.erase(systemIt);
1499inline void DeviceManager::AddInterfaceCallbacks(
const std::shared_ptr<core::Interface>& addInterface)
1501 auto InterfaceKey = addInterface->Key();
1503 const auto foundCallbackHandle = addInterface->RegisterDeviceFoundCallback(
1504 [
this, InterfaceKey](
const std::shared_ptr<core::DeviceDescriptor>& foundDevice) {
1505 const auto interfaceIt = m_interfaceCallbackContainers.find(InterfaceKey);
1507 if (interfaceIt != std::end(m_interfaceCallbackContainers))
1509 auto& container = interfaceIt->second;
1510 std::lock_guard<std::mutex> lock(container.mutex);
1512 container.foundDevices.emplace_back(foundDevice);
1513 container.deviceIdKeyMap.emplace(foundDevice->ID(), foundDevice->Key());
1517 foundDevice->RegisterDeviceOpenedCallback([
this](
const std::shared_ptr<core::Device>& openedDevice) {
1518 std::lock_guard<std::mutex> lock(m_openedDevicesMutex);
1521 m_openedDevices.erase(std::remove_if(std::begin(m_openedDevices), std::end(m_openedDevices),
1522 [](
const OpenedDevice& dev) {
return dev.device.expired(); }),
1523 std::end(m_openedDevices));
1526 OpenedDevice device;
1527 device.device = openedDevice;
1528 device.displayName = openedDevice->DisplayName();
1529 m_openedDevices.push_back(device);
1533 const auto lostCallbackHandle = addInterface->RegisterDeviceLostCallback(
1534 [
this, InterfaceKey](
const std::string& lostDeviceId) {
1535 const auto interfaceIt = m_interfaceCallbackContainers.find(InterfaceKey);
1537 if (interfaceIt != std::end(m_interfaceCallbackContainers))
1539 auto& container = interfaceIt->second;
1540 std::lock_guard<std::mutex> lock(container.mutex);
1542 const auto lostDeviceKeyIt = container.deviceIdKeyMap.find(lostDeviceId);
1543 if (lostDeviceKeyIt != std::end(container.deviceIdKeyMap))
1545 container.lostDevices.emplace_back(lostDeviceKeyIt->second);
1546 container.deviceIdKeyMap.erase(lostDeviceKeyIt);
1551 const auto reconnectedHandle = addInterface->RegisterDeviceReconnectedCallback(
1552 [
this](
const std::shared_ptr<core::DeviceDescriptor>& reconnectedDevice,
1553 core::DeviceReconnectInformation reconnectInformation) {
1554 return m_deviceReconnectedCallbackManager.TriggerCallbacks(reconnectedDevice, reconnectInformation);
1557 const auto disconnectedHandle = addInterface->RegisterDeviceDisconnectedCallback(
1558 [
this](
const std::shared_ptr<core::DeviceDescriptor>& disconnectedDevice) {
1559 return m_deviceDisconnectedCallbackManager.TriggerCallbacks(disconnectedDevice);
1562 const auto deviceListChangedHandle = addInterface->RegisterDeviceListChangedCallback(
1563 [
this]() {
return m_deviceListChangedCallbackManager.TriggerCallbacks(); });
1565 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1567 m_interfaceCallbackContainers.emplace(addInterface->Key(),
1568 InterfaceCallbackContainer{
1569 foundCallbackHandle, lostCallbackHandle, disconnectedHandle, reconnectedHandle, deviceListChangedHandle });
1572inline void DeviceManager::RemoveInterfaceCallbacks(
const std::shared_ptr<core::Interface>& removeInterface)
1574 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1576 const auto interfaceIt = m_interfaceCallbackContainers.find(removeInterface->Key());
1578 if (interfaceIt != std::end(m_interfaceCallbackContainers))
1580 const auto& container = interfaceIt->second;
1582 std::lock_guard<std::mutex> lockContainer(container.mutex);
1584 removeInterface->UnregisterDeviceFoundCallback(container.foundCallbackHandle);
1585 removeInterface->UnregisterDeviceLostCallback(container.lostCallbackHandle);
1586 removeInterface->UnregisterDeviceDisconnectedCallback(container.disconnectedCallbackHandle);
1587 removeInterface->UnregisterDeviceReconnectedCallback(container.reconnectedCallbackHandle);
1588 removeInterface->UnregisterDeviceListChangedCallback(container.deviceListChangedCallbackHandle);
1590 m_interfaceCallbackContainers.erase(interfaceIt);
1594inline void DeviceManager::CheckDevicesOpened()
const
1596 size_t openDevicesFound = 0;
1597 std::string deviceDisplayNames;
1600 std::lock_guard<std::mutex> lock(m_openedDevicesMutex);
1602 for (
const auto& openedDevice : m_openedDevices)
1604 auto openDevice = openedDevice.device.lock();
1608 deviceDisplayNames += openedDevice.displayName;
1609 deviceDisplayNames +=
" ";
1614 if (openDevicesFound > 0)
1616 throw core::InternalErrorException(
1617 "Can't reset the device manager while there are devices open. The following devices ("
1618 + std::to_string(openDevicesFound) +
") are open: " + deviceDisplayNames);
1624 return m_interfaceUpdateTimeout_ms;
1629 m_interfaceUpdateTimeout_ms = timeout_ms;
1634 return m_deviceUpdateTimeout_ms;
1639 m_deviceUpdateTimeout_ms = timeout_ms;
1644 std::lock_guard<std::mutex> lock(m_systemsMutex);
1651 std::lock_guard<std::recursive_mutex> lock(m_interfacesMutex);
1653 return m_interfaces;
1658 std::lock_guard<std::recursive_mutex> lock(m_devicesMutex);
1666 if (devices.empty())
1670 return devices.at(0);
1676 std::lock_guard<std::recursive_mutex> lock(m_devicesMutex);
1678 std::vector<std::shared_ptr<core::DeviceDescriptor>> availableDevices;
1679 std::copy_if(m_devices.begin(), m_devices.end(), std::back_inserter(availableDevices),
1680 [access](
const std::shared_ptr<core::DeviceDescriptor>& device) { return device->IsOpenable(access); });
1682 return availableDevices;
1688 return m_systemFoundCallbackManager.RegisterCallback(callback);
1693 m_systemFoundCallbackManager.UnregisterCallback(callbackHandle);
1699 return m_interfaceFoundCallbackManager.RegisterCallback(callback);
1704 m_interfaceFoundCallbackManager.UnregisterCallback(callbackHandle);
1710 return m_interfaceLostCallbackManager.RegisterCallback(callback);
1715 m_interfaceLostCallbackManager.UnregisterCallback(callbackHandle);
1721 return m_deviceFoundCallbackManager.RegisterCallback(callback);
1726 m_deviceFoundCallbackManager.UnregisterCallback(callbackHandle);
1732 return m_deviceLostCallbackManager.RegisterCallback(callback);
1737 m_deviceLostCallbackManager.UnregisterCallback(callbackHandle);
1743 return m_deviceDisconnectedCallbackManager.RegisterCallback(callback);
1749 m_deviceDisconnectedCallbackManager.UnregisterCallback(callbackHandle);
1755 return m_deviceReconnectedCallbackManager.RegisterCallback(callback);
1761 m_deviceReconnectedCallbackManager.UnregisterCallback(callbackHandle);
1767 return m_deviceListChangedCallbackManager.RegisterCallback(callback);
1773 m_deviceListChangedCallbackManager.UnregisterCallback(callbackHandle);
1776inline void DeviceManager::GetLostDevicesFromLostInterfaces(
1777 const std::vector<std::string>& lostInterfaces, std::vector<std::string>& lostDevices)
1779 std::lock_guard<std::recursive_mutex> lock(m_devicesMutex);
1781 for (
const auto& device : m_devices)
1783 auto resultIt = std::find(
1784 std::begin(lostInterfaces), std::end(lostInterfaces), device->ParentInterface()->Key());
1785 if (resultIt != std::end(lostInterfaces))
1787 lostDevices.emplace_back(device->Key());
1792inline void DeviceManager::TriggerSystemFoundCallbacks(
const std::shared_ptr<const core::System>& foundSystem)
const
1794 m_systemFoundCallbackManager.TriggerCallbacks(foundSystem);
1797inline void DeviceManager::TriggerInterfaceFoundCallbacks(
1798 const std::shared_ptr<const core::Interface>& foundInterface)
const
1800 m_interfaceFoundCallbackManager.TriggerCallbacks(foundInterface);
1803inline void DeviceManager::TriggerInterfaceLostCallbacks(
const std::string& lostInterfaceKey)
const
1805 m_interfaceLostCallbackManager.TriggerCallbacks(lostInterfaceKey);
1808inline void DeviceManager::TriggerDeviceFoundCallbacks(
const std::shared_ptr<core::DeviceDescriptor>& foundDevice)
const
1810 m_deviceFoundCallbackManager.TriggerCallbacks(foundDevice);
1813inline void DeviceManager::TriggerDeviceLostCallbacks(
const std::string& lostDeviceKey)
const
1815 m_deviceLostCallbackManager.TriggerCallbacks(lostDeviceKey);
PEAK_COMMON_NO_DISCARD core::Timeout InterfaceUpdateTimeout() const
Returns the interface update timeout.
Definition peak_device_manager.hpp:1622
std::function< void(const std::string &lostInterfaceKey)> InterfaceLostCallback
Definition peak_device_manager.hpp:109
InterfaceFoundCallbackHandle RegisterInterfaceFoundCallback(const InterfaceFoundCallback &callback)
Registers a callback for signaling a found interface.
Definition peak_device_manager.hpp:1696
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &disconnectedDevice)> DeviceDisconnectedCallback
Definition peak_device_manager.hpp:127
void UnregisterDeviceFoundCallback(DeviceFoundCallbackHandle callbackHandle)
Unregisters a device found callback.
Definition peak_device_manager.hpp:1724
void UnregisterDeviceDisconnectedCallback(DeviceDisconnectedCallbackHandle callbackHandle)
Unregisters a device disconnected callback.
Definition peak_device_manager.hpp:1746
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &foundDevice)> DeviceFoundCallback
Definition peak_device_manager.hpp:115
PEAK_COMMON_NO_DISCARD std::vector< std::shared_ptr< const core::System > > Systems() const
Returns the found and opened systems.
Definition peak_device_manager.hpp:1642
DeviceReconnectedCallback * DeviceReconnectedCallbackHandle
Definition peak_device_manager.hpp:139
void UnregisterInterfaceLostCallback(InterfaceLostCallbackHandle callbackHandle)
Unregisters an interface lost callback.
Definition peak_device_manager.hpp:1713
void Reset(ResetPolicy resetPolicy=ResetPolicy::ErrorOnOpenDevices)
Resets the DeviceManager.
Definition peak_device_manager.hpp:1373
UpdatePolicy
Enum holding the possible update policies.
Definition peak_device_manager.hpp:78
@ DontScanEnvironmentForProducerLibraries
Definition peak_device_manager.hpp:83
@ ScanEnvironmentForProducerLibraries
Definition peak_device_manager.hpp:80
ResetPolicy
Enum holding the possible reset policies.
Definition peak_device_manager.hpp:88
@ ErrorOnOpenDevices
Definition peak_device_manager.hpp:90
@ IgnoreOpenDevices
Definition peak_device_manager.hpp:93
friend class Library
Definition peak_device_manager.hpp:656
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition peak_device_manager.hpp:106
void UnregisterDeviceLostCallback(DeviceLostCallbackHandle callbackHandle)
Unregisters a device lost callback.
Definition peak_device_manager.hpp:1735
PEAK_COMMON_NO_DISCARD core::Timeout DeviceUpdateTimeout() const
Returns the device update timeout.
Definition peak_device_manager.hpp:1632
PEAK_COMMON_NO_DISCARD std::shared_ptr< core::DeviceDescriptor > FirstAvailableDevice(core::DeviceAccessType access) const
Retrieves the first device that supports the given access type.
Definition peak_device_manager.hpp:1663
void Update(UpdatePolicy updatePolicy=UpdatePolicy::ScanEnvironmentForProducerLibraries, const UpdateErrorCallback &errorCallback=UpdateErrorCallback())
Starts an update.
Definition peak_device_manager.hpp:1115
std::function< void()> DeviceListChangedCallback
Definition peak_device_manager.hpp:142
DeviceDisconnectedCallback * DeviceDisconnectedCallbackHandle
Definition peak_device_manager.hpp:131
std::function< void(const std::shared_ptr< const core::Interface > &foundInterface)> InterfaceFoundCallback
Definition peak_device_manager.hpp:103
DeviceManager & operator=(const DeviceManager &other)=delete
static PEAK_COMMON_NO_DISCARD DeviceManager & Instance()
Returns the global DeviceManager.
Definition peak_device_manager.hpp:943
std::function< void(const std::string &errorDescription)> UpdateErrorCallback
Definition peak_device_manager.hpp:148
std::function< void(const std::shared_ptr< const core::System > &foundSystem)> SystemFoundCallback
Definition peak_device_manager.hpp:97
DeviceReconnectedCallbackHandle RegisterDeviceReconnectedCallback(const DeviceReconnectedCallback &callback)
Registers a callback for signaling a reconnected device.
Definition peak_device_manager.hpp:1752
SystemFoundCallback * SystemFoundCallbackHandle
Definition peak_device_manager.hpp:100
DeviceManager(const DeviceManager &other)=delete
void UnregisterDeviceListChangedCallback(DeviceListChangedCallbackHandle callbackHandle)
Unregisters a device list change callback.
Definition peak_device_manager.hpp:1770
PEAK_COMMON_NO_DISCARD std::vector< std::shared_ptr< core::DeviceDescriptor > > FindDevices(Pred predicate) const
Finds all devices that match a custom condition.
void RemoveProducerLibrary(const std::string &ctiPath)
Removes the producer library matching the given CTI path.
Definition peak_device_manager.hpp:965
PEAK_COMMON_NO_DISCARD std::vector< std::shared_ptr< core::DeviceDescriptor > > Devices() const
Returns the found devices.
Definition peak_device_manager.hpp:1656
std::function< void(const std::string &lostDeviceKey)> DeviceLostCallback
Definition peak_device_manager.hpp:121
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &reconnectedDevice, core::DeviceReconnectInformation reconnectInformation)> DeviceReconnectedCallback
Definition peak_device_manager.hpp:134
void AddProducerLibrary(const std::string &ctiPath)
Adds the given producer library (CTI).
Definition peak_device_manager.hpp:958
SystemFoundCallbackHandle RegisterSystemFoundCallback(const SystemFoundCallback &callback)
Registers a callback for signaling a found system.
Definition peak_device_manager.hpp:1685
InterfaceLostCallbackHandle RegisterInterfaceLostCallback(const InterfaceLostCallback &callback)
Registers a callback for signaling a lost interface.
Definition peak_device_manager.hpp:1707
PEAK_COMMON_NO_DISCARD std::vector< std::shared_ptr< core::DeviceDescriptor > > AvailableDevices(core::DeviceAccessType access) const
Retrieves a collection of all devices matching the specified access type.
Definition peak_device_manager.hpp:1673
DeviceDisconnectedCallbackHandle RegisterDeviceDisconnectedCallback(const DeviceDisconnectedCallback &callback)
Registers a callback for signaling a disconnected device.
Definition peak_device_manager.hpp:1740
InterfaceLostCallback * InterfaceLostCallbackHandle
Definition peak_device_manager.hpp:112
DeviceFoundCallbackHandle RegisterDeviceFoundCallback(const DeviceFoundCallback &callback)
Registers a callback for signaling a found device.
Definition peak_device_manager.hpp:1718
DeviceFoundCallback * DeviceFoundCallbackHandle
Definition peak_device_manager.hpp:118
void UnregisterDeviceReconnectedCallback(DeviceReconnectedCallbackHandle callbackHandle)
Unregisters a device reconnected callback.
Definition peak_device_manager.hpp:1758
DeviceListChangedCallbackHandle RegisterDeviceListChangedCallback(const DeviceListChangedCallback &callback)
Registers a callback for signaling a change in the device list.
Definition peak_device_manager.hpp:1764
DeviceListChangedCallback * DeviceListChangedCallbackHandle
Definition peak_device_manager.hpp:145
void UnregisterSystemFoundCallback(SystemFoundCallbackHandle callbackHandle)
Unregisters a system found callback.
Definition peak_device_manager.hpp:1691
void SetInterfaceUpdateTimeout(core::Timeout timeout_ms)
Sets the interface update timeout.
Definition peak_device_manager.hpp:1627
void SetDeviceUpdateTimeout(core::Timeout timeout_ms)
Sets the device update timeout.
Definition peak_device_manager.hpp:1637
DeviceLostCallbackHandle RegisterDeviceLostCallback(const DeviceLostCallback &callback)
Registers a callback for signaling a lost device.
Definition peak_device_manager.hpp:1729
PEAK_COMMON_NO_DISCARD std::vector< std::shared_ptr< const core::Interface > > Interfaces() const
Returns the found and opened interfaces.
Definition peak_device_manager.hpp:1649
DeviceLostCallback * DeviceLostCallbackHandle
Definition peak_device_manager.hpp:124
void UnregisterInterfaceFoundCallback(InterfaceFoundCallbackHandle callbackHandle)
Unregisters a interface found callback.
Definition peak_device_manager.hpp:1702
The base class for all exceptions thrown by the library.
Definition peak_exception.hpp:24
The exception thrown for signaling that a feature is not available in the device.
Definition peak_exception.hpp:181
The exception thrown for signaling a failed find operation.
Definition peak_exception.hpp:190
The exception thrown for signaling that the library was not initialized.
Definition peak_exception.hpp:212
static PEAK_COMMON_NO_DISCARD std::shared_ptr< ProducerLibrary > Open(const std::string &ctiPath)
Opens the given producer library (CTI).
Definition peak_producer_library.hpp:107
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition peak_system.hpp:54
Represents a timeout value in milliseconds.
Definition peak_timeout.hpp:28
static PEAK_COMMON_NO_DISCARD std::vector< std::string > CollectCTIPaths()
Collects producer library paths found in the current environment.
Definition peak_environment_inspector.hpp:70
DeviceAccessType
Different modes to open the device with.
Definition peak_device_descriptor.hpp:37
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_buffer.hpp:29
#define PEAK_COMMON_NO_DISCARD
Definition peak_common_defines.h:89