18#include <peak/generic/peak_t_callback_manager.hpp>
29#include <unordered_map>
30#include <unordered_set>
118 using DeviceFoundCallback = std::function<void(
const std::shared_ptr<core::DeviceDescriptor>& foundDevice)>;
131 std::function<void(
const std::shared_ptr<core::DeviceDescriptor>& disconnectedDevice)>;
138 std::function<void(
const std::shared_ptr<core::DeviceDescriptor>& reconnectedDevice,
300 std::vector<std::shared_ptr<const core::System>>
Systems()
const;
311 std::vector<std::shared_ptr<const core::Interface>>
Interfaces()
const;
322 std::vector<std::shared_ptr<core::DeviceDescriptor>>
Devices()
const;
575 void GetLostDevicesFromLostInterfaces(
576 const std::vector<std::string>& lostInterfaces, std::vector<std::string>& lostDevices);
578 void TriggerSystemFoundCallbacks(
const std::shared_ptr<const core::System>& foundSystem)
const;
579 void TriggerInterfaceFoundCallbacks(
const std::shared_ptr<const core::Interface>& foundInterface)
const;
580 void TriggerInterfaceLostCallbacks(
const std::string& lostInterfaceKey)
const;
581 void TriggerDeviceFoundCallbacks(
const std::shared_ptr<core::DeviceDescriptor>& foundDevice)
const;
582 void TriggerDeviceLostCallbacks(
const std::string& lostDeviceKey)
const;
584 void AddSystemCallbacks(
const std::shared_ptr<core::System>& addSystem);
585 void RemoveSystemCallbacks(
const std::shared_ptr<core::System>& removeSystem);
586 void AddInterfaceCallbacks(
const std::shared_ptr<core::Interface>& addInterface);
587 void RemoveInterfaceCallbacks(
const std::shared_ptr<core::Interface>& removeInterface);
589 void CheckDevicesOpened()
const;
591 std::mutex m_updateMutex;
592 std::atomic<core::Timeout> m_interfaceUpdateTimeout_ms{ 100 };
593 std::atomic<core::Timeout> m_deviceUpdateTimeout_ms{ 300 };
595 std::vector<std::string> m_producerLibrariesToAdd;
597 std::unordered_map<std::string, std::shared_ptr<core::ProducerLibrary>> m_producerLibraries;
598 std::vector<std::shared_ptr<const core::System>> m_systems;
599 std::vector<std::shared_ptr<const core::Interface>> m_interfaces;
600 std::vector<std::shared_ptr<core::DeviceDescriptor>> m_devices;
602 struct SystemCallbackContainer
604 SystemCallbackContainer(
606 : foundCallbackHandle(foundHandle)
607 , lostCallbackHandle(lostHandle)
609 ~SystemCallbackContainer() =
default;
610 SystemCallbackContainer(
const SystemCallbackContainer&) =
delete;
611 SystemCallbackContainer& operator=(
const SystemCallbackContainer&) =
delete;
613 SystemCallbackContainer(SystemCallbackContainer&& a)
noexcept
615 std::lock_guard<std::mutex> lock(a.mutex);
617 std::swap(foundCallbackHandle, a.foundCallbackHandle);
618 std::swap(lostCallbackHandle, a.lostCallbackHandle);
621 SystemCallbackContainer& operator=(SystemCallbackContainer&& a)
noexcept
623 std::lock(a.mutex, mutex);
625 std::lock_guard<std::mutex> lock1(a.mutex, std::adopt_lock);
626 std::lock_guard<std::mutex> lock2(mutex, std::adopt_lock);
628 std::swap(foundCallbackHandle, a.foundCallbackHandle);
629 std::swap(lostCallbackHandle, a.lostCallbackHandle);
634 mutable std::mutex mutex;
638 std::vector<std::shared_ptr<core::Interface>> foundInterfaces;
639 std::vector<std::string> lostInterfaces;
643 std::unordered_map<std::string, std::string> interfaceIdKeyMap;
646 struct InterfaceCallbackContainer
651 : foundCallbackHandle(foundHandle)
652 , lostCallbackHandle(lostHandle)
653 , disconnectedCallbackHandle(disconnectedHandle)
654 , reconnectedCallbackHandle(reconnectedHandle)
655 , deviceListChangedCallbackHandle(listChangedHandle)
658 ~InterfaceCallbackContainer() =
default;
659 InterfaceCallbackContainer(
const InterfaceCallbackContainer&) =
delete;
660 InterfaceCallbackContainer& operator=(
const InterfaceCallbackContainer&) =
delete;
662 InterfaceCallbackContainer(InterfaceCallbackContainer&& a)
noexcept
664 std::lock_guard<std::mutex> lock(a.mutex);
666 std::swap(foundCallbackHandle, a.foundCallbackHandle);
667 std::swap(lostCallbackHandle, a.lostCallbackHandle);
668 std::swap(disconnectedCallbackHandle, a.disconnectedCallbackHandle);
669 std::swap(reconnectedCallbackHandle, a.reconnectedCallbackHandle);
670 std::swap(deviceListChangedCallbackHandle, a.deviceListChangedCallbackHandle);
673 InterfaceCallbackContainer& operator=(InterfaceCallbackContainer&& a)
noexcept
675 std::lock(a.mutex, mutex);
677 std::lock_guard<std::mutex> lock1(a.mutex, std::adopt_lock);
678 std::lock_guard<std::mutex> lock2(mutex, std::adopt_lock);
680 std::swap(foundCallbackHandle, a.foundCallbackHandle);
681 std::swap(lostCallbackHandle, a.lostCallbackHandle);
682 std::swap(disconnectedCallbackHandle, a.disconnectedCallbackHandle);
683 std::swap(reconnectedCallbackHandle, a.reconnectedCallbackHandle);
684 std::swap(deviceListChangedCallbackHandle, a.deviceListChangedCallbackHandle);
689 mutable std::mutex mutex;
696 std::vector<std::shared_ptr<core::DeviceDescriptor>> foundDevices;
697 std::vector<std::string> lostDevices;
701 std::unordered_map<std::string, std::string> deviceIdKeyMap;
704 mutable std::mutex m_callbackContainerMutex;
705 std::unordered_map<std::string, SystemCallbackContainer> m_systemCallbackContainers;
706 std::unordered_map<std::string, InterfaceCallbackContainer> m_interfaceCallbackContainers;
710 std::weak_ptr<core::Device> device{};
711 std::string displayName;
713 std::vector<OpenedDevice> m_openedDevices;
715 core::TTriggerCallbackManager<SystemFoundCallbackHandle, SystemFoundCallback> m_systemFoundCallbackManager;
716 core::TTriggerCallbackManager<InterfaceFoundCallbackHandle, InterfaceFoundCallback> m_interfaceFoundCallbackManager;
717 core::TTriggerCallbackManager<InterfaceLostCallbackHandle, InterfaceLostCallback> m_interfaceLostCallbackManager;
718 core::TTriggerCallbackManager<DeviceFoundCallbackHandle, DeviceFoundCallback> m_deviceFoundCallbackManager;
719 core::TTriggerCallbackManager<DeviceLostCallbackHandle, DeviceLostCallback> m_deviceLostCallbackManager;
720 core::TTriggerCallbackManager<DeviceDisconnectedCallbackHandle, DeviceDisconnectedCallback>
721 m_deviceDisconnectedCallbackManager;
722 core::TTriggerCallbackManager<DeviceReconnectedCallbackHandle, DeviceReconnectedCallback>
723 m_deviceReconnectedCallbackManager;
724 core::TTriggerCallbackManager<DeviceListChangedCallbackHandle, DeviceListChangedCallback>
725 m_deviceListChangedCallbackManager;
727 std::mutex m_producerLibrariesToAddMutex;
728 mutable std::mutex m_systemsMutex;
729 mutable std::recursive_mutex m_interfacesMutex;
730 mutable std::recursive_mutex m_devicesMutex;
731 mutable std::mutex m_openedDevicesMutex;
734 template <
typename T,
735 typename std::enable_if<(noexcept(std::declval<T>()())
736 || (std::is_pointer<typename std::decay<T>::type>::value && __cplusplus < 201703L)
739 && std::is_same<void, decltype(std::declval<T>()())>::value && std::is_move_constructible<T>::value
740 && std::is_nothrow_move_constructible<T>::value,
743 template <
typename T>
750 explicit RAIIGuard(T&& fun) noexcept
751 : m_fun(std::forward<T>(fun))
753 RAIIGuard(RAIIGuard
const&) =
delete;
754 RAIIGuard(RAIIGuard&& other) noexcept
755 : m_fun(std::move(other.m_fun))
757 ~RAIIGuard() noexcept
762 template <
typename T>
763 static RAIIGuard<T> makeRAIIGuard(T&& fun)
noexcept
765 return { std::forward<T>(fun) };
768 template <
typename T = DeviceManager>
769 class LifetimeTracker
772 static void Create();
773 static void Destroy();
777 static T* m_instance;
778 static std::mutex m_mtx;
779 static size_t m_initializedCount;
790T* DeviceManager::LifetimeTracker<T>::m_instance = {};
793std::mutex DeviceManager::LifetimeTracker<T>::m_mtx = {};
796size_t DeviceManager::LifetimeTracker<T>::m_initializedCount = {};
799void DeviceManager::LifetimeTracker<T>::Create()
801 std::lock_guard<
decltype(m_mtx)> lock{ m_mtx };
805 m_instance =
new T{};
808 m_initializedCount++;
812void DeviceManager::LifetimeTracker<T>::Destroy()
814 std::lock_guard<
decltype(m_mtx)> lock{ m_mtx };
816 if (0 == m_initializedCount)
821 m_initializedCount--;
823 if (0 == m_initializedCount)
828 m_instance =
nullptr;
834T& DeviceManager::LifetimeTracker<T>::Instance()
836 if (m_instance ==
nullptr)
839 "Library is not initialized. Call peak::Library::Initalize() first!");
846 return LifetimeTracker<>::Instance();
849inline DeviceManager::~DeviceManager()
856 std::lock_guard<std::mutex> lock(m_producerLibrariesToAddMutex);
858 m_producerLibrariesToAdd.emplace_back(ctiPath);
863 std::lock(m_updateMutex, m_systemsMutex);
865 std::lock_guard<std::mutex> lockUpdate(m_updateMutex, std::adopt_lock);
867 std::shared_ptr<core::System> newSystem{};
869 std::lock_guard<std::mutex> lockSystems(m_systemsMutex, std::adopt_lock);
871 const auto systemDescriptor = library->System();
872 const auto ctiPath = systemDescriptor->CTIFullPath();
875 if (m_producerLibraries.count(ctiPath) == 0)
877 m_producerLibraries.emplace(ctiPath, library);
879 newSystem = systemDescriptor->IsOpened() ? systemDescriptor->OpenedSystem() : systemDescriptor->OpenSystem();
880 AddSystemCallbacks(newSystem);
881 m_systems.emplace_back(newSystem);
887 TriggerSystemFoundCallbacks(newSystem);
894 std::lock_guard<std::mutex> lockUpdate(m_updateMutex);
896 std::vector<std::shared_ptr<core::System>> allFoundSystems;
897 std::vector<std::shared_ptr<core::Interface>> allFoundInterfaces;
898 std::vector<std::string> allLostInterfaces;
899 std::vector<std::shared_ptr<core::DeviceDescriptor>> allFoundDevices;
900 std::vector<std::string> allLostDevices;
902 auto interfaceUpdateTimeout_ms = m_interfaceUpdateTimeout_ms.load();
903 auto deviceUpdateTimeout_ms = m_deviceUpdateTimeout_ms.load();
905 std::vector<std::string> ctiPaths;
914 std::lock_guard<std::mutex> lock(m_producerLibrariesToAddMutex);
916 ctiPaths.reserve(ctiPaths.size() + m_producerLibrariesToAdd.size());
918 std::begin(m_producerLibrariesToAdd), std::end(m_producerLibrariesToAdd), std::back_inserter(ctiPaths));
920 m_producerLibrariesToAdd.clear();
923 for (
const auto& ctiPath : ctiPaths)
925 if (m_producerLibraries.count(ctiPath) == 0)
930 auto system = producerLibrary->System()->OpenSystem();
932 AddSystemCallbacks(system);
934 m_producerLibraries.emplace(ctiPath, producerLibrary);
935 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);
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:112
InterfaceFoundCallbackHandle RegisterInterfaceFoundCallback(const InterfaceFoundCallback &callback)
Registers a callback for signaling a found interface.
Definition peak_device_manager.hpp:1450
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &disconnectedDevice)> DeviceDisconnectedCallback
Definition peak_device_manager.hpp:130
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 > &foundDevice)> DeviceFoundCallback
Definition peak_device_manager.hpp:118
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:142
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:81
@ DontScanEnvironmentForProducerLibraries
Definition peak_device_manager.hpp:86
@ ScanEnvironmentForProducerLibraries
Definition peak_device_manager.hpp:83
ResetPolicy
Enum holding the possible reset policies.
Definition peak_device_manager.hpp:91
@ ErrorOnOpenDevices
Definition peak_device_manager.hpp:93
@ IgnoreOpenDevices
Definition peak_device_manager.hpp:96
friend class Library
Definition peak_device_manager.hpp:570
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition peak_device_manager.hpp:109
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:891
std::function< void()> DeviceListChangedCallback
Definition peak_device_manager.hpp:145
DeviceDisconnectedCallback * DeviceDisconnectedCallbackHandle
Definition peak_device_manager.hpp:134
std::function< void(const std::shared_ptr< const core::Interface > &foundInterface)> InterfaceFoundCallback
Definition peak_device_manager.hpp:106
DeviceManager & operator=(const DeviceManager &other)=delete
static DeviceManager & Instance()
Returns the global DeviceManager.
Definition peak_device_manager.hpp:844
std::function< void(const std::string &errorDescription)> UpdateErrorCallback
Definition peak_device_manager.hpp:151
std::function< void(const std::shared_ptr< const core::System > &foundSystem)> SystemFoundCallback
Definition peak_device_manager.hpp:100
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:103
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:124
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &reconnectedDevice, core::DeviceReconnectInformation reconnectInformation)> DeviceReconnectedCallback
Definition peak_device_manager.hpp:137
void AddProducerLibrary(const std::string &ctiPath)
Adds the given producer library (CTI).
Definition peak_device_manager.hpp:854
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
InterfaceLostCallback * InterfaceLostCallbackHandle
Definition peak_device_manager.hpp:115
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:121
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:148
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:127
void UnregisterInterfaceFoundCallback(InterfaceFoundCallbackHandle callbackHandle)
Unregisters a interface found callback.
Definition peak_device_manager.hpp:1456
The base class for all exceptions thrown by the library.
Definition peak_exception.hpp:28
The exception thrown for signaling that a feature is not available in the device.
Definition peak_exception.hpp:177
The exception thrown for signaling that the library was not initialized.
Definition peak_exception.hpp:206
static std::shared_ptr< ProducerLibrary > Open(const std::string &ctiPath)
Opens the given producer library (CTI).
Definition peak_producer_library.hpp:110
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition peak_system.hpp:57
Represents a timeout value in milliseconds.
Definition peak_timeout.hpp:32
static std::vector< std::string > CollectCTIPaths()
Collects producer library paths found in the current environment.
Definition peak_environment_inspector.hpp:72
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_dynamic_loader.h:35