IDS peak genericC++ v1.8.0
Loading...
Searching...
No Matches
peak_device_manager.hpp
Go to the documentation of this file.
1
11#pragma once
12
18#include <peak/generic/peak_t_callback_manager.hpp>
25#include <utility>
26
27
28#include <type_traits>
29#include <unordered_map>
30#include <unordered_set>
31#include <algorithm>
32#include <atomic>
33#include <cstdlib>
34#include <exception>
35#include <functional>
36#include <future>
37#include <iterator>
38#include <memory>
39#include <mutex>
40#include <new>
41#include <utility>
42#include <vector>
43
44
45namespace peak
46{
47
75class DeviceManager final
76{
77public:
87
89 enum class ResetPolicy
90 {
93
96 };
97
99 using SystemFoundCallback = std::function<void(const std::shared_ptr<const core::System>& foundSystem)>;
100
103
105 using InterfaceFoundCallback = std::function<void(const std::shared_ptr<const core::Interface>& foundInterface)>;
106
109
111 using InterfaceLostCallback = std::function<void(const std::string& lostInterfaceKey)>;
112
115
117 using DeviceFoundCallback = std::function<void(const std::shared_ptr<core::DeviceDescriptor>& foundDevice)>;
118
121
123 using DeviceLostCallback = std::function<void(const std::string& lostDeviceKey)>;
124
127
130 std::function<void(const std::shared_ptr<core::DeviceDescriptor>& disconnectedDevice)>;
131
134
137 std::function<void(const std::shared_ptr<core::DeviceDescriptor>& reconnectedDevice,
138 core::DeviceReconnectInformation reconnectInformation)>;
139
142
144 using DeviceListChangedCallback = std::function<void()>;
145
148
150 using UpdateErrorCallback = std::function<void(const std::string& errorDescription)>;
151
157 static DeviceManager& Instance();
158
174 void AddProducerLibrary(const std::string& ctiPath);
175
195 void AddProducerLibrary(const std::shared_ptr<core::ProducerLibrary>& library);
196
216 const UpdateErrorCallback& errorCallback = UpdateErrorCallback());
217
237
248
263
274
288 void SetDeviceUpdateTimeout(core::Timeout timeout_ms);
289
299 std::vector<std::shared_ptr<const core::System>> Systems() const;
300
310 std::vector<std::shared_ptr<const core::Interface>> Interfaces() const;
311
321 std::vector<std::shared_ptr<core::DeviceDescriptor>> Devices() const;
322
340
353
371
384
402
415
433
446
464
477
493
506
522
535
551
564
565 DeviceManager(const DeviceManager& other) = delete;
566 DeviceManager& operator=(const DeviceManager& other) = delete;
567
568private:
569 friend class Library;
570
571 DeviceManager() = default;
573
574 void GetLostDevicesFromLostInterfaces(
575 const std::vector<std::string>& lostInterfaces, std::vector<std::string>& lostDevices);
576
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;
582
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);
587
588 void CheckDevicesOpened() const;
589
590 std::mutex m_updateMutex;
591 std::atomic<core::Timeout> m_interfaceUpdateTimeout_ms{ 100 };
592 std::atomic<core::Timeout> m_deviceUpdateTimeout_ms{ 300 };
593
594 std::vector<std::string> m_producerLibrariesToAdd;
595
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;
600
601 struct SystemCallbackContainer
602 {
603 SystemCallbackContainer(
605 : foundCallbackHandle(foundHandle)
606 , lostCallbackHandle(lostHandle)
607 {}
608 ~SystemCallbackContainer() = default;
609 SystemCallbackContainer(const SystemCallbackContainer&) = delete;
610 SystemCallbackContainer& operator=(const SystemCallbackContainer&) = delete;
611
612 SystemCallbackContainer(SystemCallbackContainer&& a) noexcept
613 {
614 std::lock_guard<std::mutex> lock(a.mutex);
615
616 std::swap(foundCallbackHandle, a.foundCallbackHandle);
617 std::swap(lostCallbackHandle, a.lostCallbackHandle);
618 }
619
620 SystemCallbackContainer& operator=(SystemCallbackContainer&& a) noexcept
621 {
622 std::lock(a.mutex, mutex);
623
624 std::lock_guard<std::mutex> lock1(a.mutex, std::adopt_lock);
625 std::lock_guard<std::mutex> lock2(mutex, std::adopt_lock);
626
627 std::swap(foundCallbackHandle, a.foundCallbackHandle);
628 std::swap(lostCallbackHandle, a.lostCallbackHandle);
629
630 return *this;
631 }
632
633 mutable std::mutex mutex;
634 core::System::InterfaceFoundCallbackHandle foundCallbackHandle{};
635 InterfaceLostCallbackHandle lostCallbackHandle{};
636
637 std::vector<std::shared_ptr<core::Interface>> foundInterfaces;
638 std::vector<std::string> lostInterfaces;
639
640 // TL reports interface lost only with interface id part
641 // in order to get the full interface key we need to map it
642 std::unordered_map<std::string, std::string> interfaceIdKeyMap;
643 };
644
645 struct InterfaceCallbackContainer
646 {
647 InterfaceCallbackContainer(DeviceFoundCallbackHandle foundHandle, DeviceLostCallbackHandle lostHandle,
648 DeviceDisconnectedCallbackHandle disconnectedHandle, DeviceReconnectedCallbackHandle reconnectedHandle,
649 DeviceListChangedCallbackHandle listChangedHandle)
650 : foundCallbackHandle(foundHandle)
651 , lostCallbackHandle(lostHandle)
652 , disconnectedCallbackHandle(disconnectedHandle)
653 , reconnectedCallbackHandle(reconnectedHandle)
654 , deviceListChangedCallbackHandle(listChangedHandle)
655 {}
656
657 ~InterfaceCallbackContainer() = default;
658 InterfaceCallbackContainer(const InterfaceCallbackContainer&) = delete;
659 InterfaceCallbackContainer& operator=(const InterfaceCallbackContainer&) = delete;
660
661 InterfaceCallbackContainer(InterfaceCallbackContainer&& a) noexcept
662 {
663 std::lock_guard<std::mutex> lock(a.mutex);
664
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);
670 }
671
672 InterfaceCallbackContainer& operator=(InterfaceCallbackContainer&& a) noexcept
673 {
674 std::lock(a.mutex, mutex);
675
676 std::lock_guard<std::mutex> lock1(a.mutex, std::adopt_lock);
677 std::lock_guard<std::mutex> lock2(mutex, std::adopt_lock);
678
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);
684
685 return *this;
686 }
687
688 mutable std::mutex mutex;
689 DeviceFoundCallbackHandle foundCallbackHandle{};
690 DeviceLostCallbackHandle lostCallbackHandle{};
691 DeviceDisconnectedCallbackHandle disconnectedCallbackHandle{};
692 DeviceReconnectedCallbackHandle reconnectedCallbackHandle{};
693 DeviceListChangedCallbackHandle deviceListChangedCallbackHandle{};
694
695 std::vector<std::shared_ptr<core::DeviceDescriptor>> foundDevices;
696 std::vector<std::string> lostDevices;
697
698 // TL reports device lost only with device id part
699 // in order to get the full device key we need to map it
700 std::unordered_map<std::string, std::string> deviceIdKeyMap;
701 };
702
703 mutable std::mutex m_callbackContainerMutex;
704 std::unordered_map<std::string, SystemCallbackContainer> m_systemCallbackContainers;
705 std::unordered_map<std::string, InterfaceCallbackContainer> m_interfaceCallbackContainers;
706
707 struct OpenedDevice
708 {
709 std::weak_ptr<core::Device> device{};
710 std::string displayName;
711 };
712 std::vector<OpenedDevice> m_openedDevices;
713
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;
725
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;
731
732#ifndef _MSC_VER
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)
736 // NOTE pre c++17 there is no way to check whether a function pointer is noexcept
737 )
738 && std::is_same<void, decltype(std::declval<T>()())>::value && std::is_move_constructible<T>::value
739 && std::is_nothrow_move_constructible<T>::value,
740 int>::type = 0>
741#else
742 template <typename T>
743#endif
744 class RAIIGuard
745 {
746 T m_fun;
747
748 public:
749 explicit RAIIGuard(T&& fun) noexcept
750 : m_fun(std::forward<T>(fun))
751 {}
752 RAIIGuard(RAIIGuard const&) = delete;
753 RAIIGuard(RAIIGuard&& other) noexcept
754 : m_fun(std::move(other.m_fun))
755 {}
756 ~RAIIGuard() noexcept
757 {
758 m_fun();
759 }
760 };
761 template <typename T>
762 static RAIIGuard<T> makeRAIIGuard(T&& fun) noexcept
763 {
764 return { std::forward<T>(fun) };
765 }
766
767 template <typename T = DeviceManager>
768 class LifetimeTracker
769 {
770 public:
771 static void Create();
772 static void Destroy();
773 static T& Instance();
774
775 private:
776 static T* m_instance;
777 static std::mutex m_mtx;
778 static size_t m_initializedCount;
779 };
780};
781
782} /* namespace peak */
783
784/* Implementation */
785namespace peak
786{
787
788template <typename T>
789T* DeviceManager::LifetimeTracker<T>::m_instance = {};
790
791template <typename T>
792std::mutex DeviceManager::LifetimeTracker<T>::m_mtx = {};
793
794template <typename T>
795size_t DeviceManager::LifetimeTracker<T>::m_initializedCount = {};
796
797template <typename T>
798void DeviceManager::LifetimeTracker<T>::Create()
799{
800 std::lock_guard<decltype(m_mtx)> lock{ m_mtx };
801
802 if (!m_instance)
803 {
804 m_instance = new T{};
805 }
806
807 m_initializedCount++;
808}
809
810template <typename T>
811void DeviceManager::LifetimeTracker<T>::Destroy()
812{
813 std::lock_guard<decltype(m_mtx)> lock{ m_mtx };
814
815 if (0 == m_initializedCount)
816 {
817 return;
818 }
819
820 m_initializedCount--;
821
822 if (0 == m_initializedCount)
823 {
824 if (m_instance)
825 {
826 delete m_instance;
827 m_instance = nullptr;
828 }
829 }
830}
831
832template <typename T>
833T& DeviceManager::LifetimeTracker<T>::Instance()
834{
835 if (m_instance == nullptr)
836 {
837 throw core::NotInitializedException(
838 "Library is not initialized. Call peak::Library::Initalize() first!");
839 }
840 return *m_instance;
841}
842
844{
845 return LifetimeTracker<>::Instance();
846}
847
848inline DeviceManager::~DeviceManager()
849{
851}
852
853inline void DeviceManager::AddProducerLibrary(const std::string& ctiPath)
854{
855 std::lock_guard<std::mutex> lock(m_producerLibrariesToAddMutex);
856
857 m_producerLibrariesToAdd.emplace_back(ctiPath);
858}
859
860inline void DeviceManager::AddProducerLibrary(const std::shared_ptr<core::ProducerLibrary>& library)
861{
862 std::lock(m_updateMutex, m_systemsMutex);
863
864 std::lock_guard<std::mutex> lockUpdate(m_updateMutex, std::adopt_lock);
865
866 std::shared_ptr<core::System> newSystem{};
867 {
868 std::lock_guard<std::mutex> lockSystems(m_systemsMutex, std::adopt_lock);
869
870 const auto systemDescriptor = library->System();
871 const auto ctiPath = systemDescriptor->CTIFullPath();
872
873 // Check if system is already added
874 if (m_producerLibraries.count(ctiPath) == 0)
875 {
876 m_producerLibraries.emplace(ctiPath, library);
877
878 newSystem = systemDescriptor->IsOpened() ? systemDescriptor->OpenedSystem() : systemDescriptor->OpenSystem();
879 AddSystemCallbacks(newSystem);
880 m_systems.emplace_back(newSystem);
881 }
882 }
883
884 if (newSystem)
885 {
886 TriggerSystemFoundCallbacks(newSystem);
887 }
888}
889
890inline void DeviceManager::Update(UpdatePolicy updatePolicy /*= UpdatePolicy::ScanEnvironmentForProducerLibraries*/,
891 const UpdateErrorCallback& errorCallback /*= UpdateErrorCallback()*/)
892{
893 std::lock_guard<std::mutex> lockUpdate(m_updateMutex);
894
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;
900
901 auto interfaceUpdateTimeout_ms = m_interfaceUpdateTimeout_ms.load();
902 auto deviceUpdateTimeout_ms = m_deviceUpdateTimeout_ms.load();
903
904 std::vector<std::string> ctiPaths;
905 // We look for all producer libraries in the current environment if we have to
907 {
909 }
910
911 // We look for the manually added producer libraries
912 {
913 std::lock_guard<std::mutex> lock(m_producerLibrariesToAddMutex);
914
915 ctiPaths.reserve(ctiPaths.size() + m_producerLibrariesToAdd.size());
916 std::move(
917 std::begin(m_producerLibrariesToAdd), std::end(m_producerLibrariesToAdd), std::back_inserter(ctiPaths));
918
919 m_producerLibrariesToAdd.clear();
920 }
921
922 for (const auto& ctiPath : ctiPaths)
923 {
924 if (m_producerLibraries.count(ctiPath) == 0)
925 {
926 try
927 {
928 auto producerLibrary = core::ProducerLibrary::Open(ctiPath);
929 auto system = producerLibrary->System()->OpenSystem();
930
931 AddSystemCallbacks(system);
932
933 m_producerLibraries.emplace(ctiPath, producerLibrary);
934 allFoundSystems.emplace_back(system);
935 }
936 catch (const core::NotAvailableException&)
937 {
938 // TL not available or not supported.
939 continue;
940 }
941 catch (const core::NotInitializedException&)
942 {
943 // TODO: check this on creation of DeviceManager?
944 throw; // if this exception occurs, nothing else will work. so we stop here
945 }
946 catch (const core::Exception& ex)
947 {
948 if (errorCallback)
949 {
950 try
951 {
952 errorCallback("[CTI - " + ctiPath + "]: " + ex.what());
953 }
954 catch (...)
955 {}
956 }
957 }
958 }
959 }
960
961 auto systemUpdateChildren = [&](const std::shared_ptr<core::System>& system) {
962 try
963 {
964 system->UpdateInterfaces(interfaceUpdateTimeout_ms);
965 }
966 catch (const core::Exception& ex)
967 {
968 if (errorCallback)
969 {
970 try
971 {
972 errorCallback("[System - " + system->Key() + "]: " + ex.what());
973 }
974 catch (...)
975 {}
976 }
977 }
978 };
979
980 // We do an update on all available systems
981 constexpr auto launchPolicy = std::launch::async; // switching this to std::launch::deferred makes debugging easier
982
983 std::vector<std::future<void>> systemUpdateChildrenTasks;
984
985 // add already found systems
986 for (const auto& system : m_systems)
987 {
988 systemUpdateChildrenTasks.emplace_back(
989 std::async(launchPolicy, systemUpdateChildren, std::const_pointer_cast<core::System>(system)));
990 }
991
992 // now add newly found systems
993 for (const auto& system : allFoundSystems)
994 {
995 systemUpdateChildrenTasks.emplace_back(std::async(launchPolicy, systemUpdateChildren, system));
996 }
997
998 // wait for tasks to be done
999 systemUpdateChildrenTasks.clear();
1000
1001 // copy results to our local lists
1002 for (auto& keyValue : m_systemCallbackContainers)
1003 {
1004 auto& container = keyValue.second;
1005
1006 std::lock_guard<std::mutex> lock(container.mutex);
1007
1008 allFoundInterfaces.reserve(allFoundInterfaces.size() + container.foundInterfaces.size());
1009 for (const auto& foundInterface : container.foundInterfaces)
1010 {
1011 AddInterfaceCallbacks(foundInterface);
1012 allFoundInterfaces.emplace_back(foundInterface);
1013 }
1014 container.foundInterfaces.clear();
1015
1016 allLostInterfaces.reserve(allLostInterfaces.size() + container.lostInterfaces.size());
1017 for (const auto& lostInterface : container.lostInterfaces)
1018 {
1019 auto interfaceIt = std::find_if(std::begin(m_interfaces), std::end(m_interfaces),
1020 [&lostInterface](const auto& intf) { return intf->Key() == lostInterface; });
1021
1022 if (interfaceIt != std::end(m_interfaces))
1023 {
1024 RemoveInterfaceCallbacks(std::const_pointer_cast<core::Interface>(*interfaceIt));
1025 }
1026
1027 allLostInterfaces.emplace_back(lostInterface);
1028 }
1029 container.lostInterfaces.clear();
1030 }
1031
1032 auto interfaceUpdateChildren = [&deviceUpdateTimeout_ms, &errorCallback](
1033 const std::shared_ptr<core::Interface>& iface) {
1034 try
1035 {
1036 iface->UpdateDevices(deviceUpdateTimeout_ms);
1037 }
1038 catch (const core::Exception& ex)
1039 {
1040 if (errorCallback)
1041 {
1042 try
1043 {
1044 errorCallback("[Interface - " + iface->Key() + "]: " + ex.what());
1045 }
1046 catch (...)
1047 {}
1048 }
1049 }
1050 };
1051
1052 // We do an update on all available interfaces
1053 std::vector<std::future<void>> interfaceUpdateChildrenTasks;
1054
1055 // add already found interfaces
1056 for (const auto& iface : m_interfaces)
1057 {
1058 interfaceUpdateChildrenTasks.emplace_back(
1059 std::async(launchPolicy, interfaceUpdateChildren, std::const_pointer_cast<core::Interface>(iface)));
1060 }
1061
1062 // add newly found interfaces
1063 for (const auto& iface : allFoundInterfaces)
1064 {
1065 interfaceUpdateChildrenTasks.emplace_back(std::async(launchPolicy, interfaceUpdateChildren, iface));
1066 }
1067
1068 // wait for tasks to be done
1069 interfaceUpdateChildrenTasks.clear();
1070
1071 // copy results to our local lists
1072 for (auto& keyValue : m_interfaceCallbackContainers)
1073 {
1074 auto& container = keyValue.second;
1075 std::lock_guard<std::mutex> lock(container.mutex);
1076
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));
1081
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));
1086 }
1087
1088 // We apply the update changes to our internal lists
1089 {
1090 std::lock(m_systemsMutex, m_interfacesMutex, m_devicesMutex);
1091
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);
1095
1096 m_systems.reserve(m_systems.size() + allFoundSystems.size());
1097 std::copy(std::begin(allFoundSystems), std::end(allFoundSystems), std::back_inserter(m_systems));
1098
1099 for (const auto& lostInterfaceKey : allLostInterfaces)
1100 {
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;
1104 }),
1105 std::end(m_interfaces));
1106 }
1107 m_interfaces.reserve(m_interfaces.size() + allFoundInterfaces.size());
1108 std::copy(std::begin(allFoundInterfaces), std::end(allFoundInterfaces), std::back_inserter(m_interfaces));
1109
1110 for (const auto& lostDeviceKey : allLostDevices)
1111 {
1112 m_devices.erase(
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));
1116 }
1117 m_devices.reserve(m_devices.size() + allFoundDevices.size());
1118 std::copy(std::begin(allFoundDevices), std::end(allFoundDevices), std::back_inserter(m_devices));
1119 }
1120
1121 // We trigger all callbacks
1122 for (const auto& foundSystem : allFoundSystems)
1123 {
1124 TriggerSystemFoundCallbacks(foundSystem);
1125 }
1126
1127 for (const auto& foundInterface : allFoundInterfaces)
1128 {
1129 TriggerInterfaceFoundCallbacks(foundInterface);
1130 }
1131
1132 for (const auto& lostInterfaceKey : allLostInterfaces)
1133 {
1134 TriggerInterfaceLostCallbacks(lostInterfaceKey);
1135 }
1136
1137 for (const auto& foundDevice : allFoundDevices)
1138 {
1139 TriggerDeviceFoundCallbacks(foundDevice);
1140 }
1141
1142 GetLostDevicesFromLostInterfaces(allLostInterfaces, allLostDevices);
1143 for (const auto& lostDeviceKey : allLostDevices)
1144 {
1145 TriggerDeviceLostCallbacks(lostDeviceKey);
1146 }
1147}
1148
1149inline void DeviceManager::Reset(ResetPolicy resetPolicy)
1150{
1151 std::lock_guard<std::mutex> lockUpdate(m_updateMutex);
1152
1153 if (resetPolicy == ResetPolicy::ErrorOnOpenDevices)
1154 {
1155 // check if any devices are running
1156 CheckDevicesOpened();
1157 }
1158
1159 {
1160 std::lock(m_systemsMutex, m_interfacesMutex, m_devicesMutex);
1161
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);
1165
1166 // Note: the list must be swapped because the devices must be removed from the list before the callback is
1167 // called
1168 decltype(m_devices) lostDevicesList{};
1169 std::swap(m_devices, lostDevicesList);
1170 for (const auto& lostDevice : lostDevicesList)
1171 {
1172 TriggerDeviceLostCallbacks(lostDevice->Key());
1173 }
1174
1175 decltype(m_interfaces) lostInterfacesList{};
1176 std::swap(m_interfaces, lostInterfacesList);
1177 for (const auto& lostInterface : lostInterfacesList)
1178 {
1179 TriggerInterfaceLostCallbacks(lostInterface->Key());
1180 RemoveInterfaceCallbacks(std::const_pointer_cast<core::Interface>(lostInterface));
1181 }
1182
1183 decltype(m_systems) lostSystemsList{};
1184 std::swap(m_systems, lostSystemsList);
1185 for (const auto& lostSystem : lostSystemsList)
1186 {
1187 RemoveSystemCallbacks(std::const_pointer_cast<core::System>(lostSystem));
1188 }
1189
1190 m_producerLibraries.clear();
1191 }
1192
1193 {
1194 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1195
1196 m_systemCallbackContainers.clear();
1197 m_interfaceCallbackContainers.clear();
1198 }
1199
1200 {
1201 std::lock_guard<std::mutex> lock(m_producerLibrariesToAddMutex);
1202 m_producerLibrariesToAdd.clear();
1203 }
1204}
1205
1206inline void DeviceManager::AddSystemCallbacks(const std::shared_ptr<core::System>& addSystem)
1207{
1208 auto SystemKey = addSystem->Key();
1209
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))
1214 {
1215 try
1216 {
1217 auto& container = systemIt->second;
1218 std::lock_guard<std::mutex> lock(container.mutex);
1219
1220 const auto openedInterface = foundInterface->OpenInterface();
1221
1222 container.foundInterfaces.emplace_back(openedInterface);
1223 container.interfaceIdKeyMap.emplace(openedInterface->ID(), openedInterface->Key());
1224 }
1225 catch (const std::exception&)
1226 {}
1227 }
1228 });
1229
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))
1234 {
1235 auto& container = systemIt->second;
1236 std::lock_guard<std::mutex> lock(container.mutex);
1237
1238 const auto interfaceKeyIt = container.interfaceIdKeyMap.find(lostInterfaceId);
1239 if (interfaceKeyIt != std::end(container.interfaceIdKeyMap))
1240 {
1241 container.lostInterfaces.emplace_back(interfaceKeyIt->second);
1242 container.interfaceIdKeyMap.erase(interfaceKeyIt);
1243 }
1244 }
1245 });
1246
1247 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1248
1249 m_systemCallbackContainers.emplace(
1250 addSystem->Key(), SystemCallbackContainer{ foundCallbackHandle, lostCallbackHandle });
1251}
1252
1253inline void DeviceManager::RemoveSystemCallbacks(const std::shared_ptr<core::System>& removeSystem)
1254{
1255 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1256
1257 const auto SystemKey = removeSystem->Key();
1258
1259 const auto systemIt = m_systemCallbackContainers.find(SystemKey);
1260
1261 if (systemIt != std::end(m_systemCallbackContainers))
1262 {
1263 const auto& container = systemIt->second;
1264 {
1265 std::lock_guard<std::mutex> lockContainer(container.mutex);
1266
1267 removeSystem->UnregisterInterfaceFoundCallback(container.foundCallbackHandle);
1268 removeSystem->UnregisterInterfaceLostCallback(container.lostCallbackHandle);
1269 }
1270
1271 m_systemCallbackContainers.erase(systemIt);
1272 }
1273}
1274
1275inline void DeviceManager::AddInterfaceCallbacks(const std::shared_ptr<core::Interface>& addInterface)
1276{
1277 auto InterfaceKey = addInterface->Key();
1278
1279 const auto foundCallbackHandle = addInterface->RegisterDeviceFoundCallback(
1280 [this, InterfaceKey](const std::shared_ptr<core::DeviceDescriptor>& foundDevice) {
1281 const auto interfaceIt = m_interfaceCallbackContainers.find(InterfaceKey);
1282
1283 if (interfaceIt != std::end(m_interfaceCallbackContainers))
1284 {
1285 auto& container = interfaceIt->second;
1286 std::lock_guard<std::mutex> lock(container.mutex);
1287
1288 container.foundDevices.emplace_back(foundDevice);
1289 container.deviceIdKeyMap.emplace(foundDevice->ID(), foundDevice->Key());
1290 }
1291
1292 // also keep track about opened devices
1293 foundDevice->RegisterDeviceOpenedCallback([this](const std::shared_ptr<core::Device>& openedDevice) {
1294 std::lock_guard<std::mutex> lock(m_openedDevicesMutex);
1295
1296 // clear out closed devices
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));
1300
1301 // add the new one
1302 OpenedDevice device;
1303 device.device = openedDevice;
1304 device.displayName = openedDevice->DisplayName();
1305 m_openedDevices.push_back(device);
1306 });
1307 });
1308
1309 const auto lostCallbackHandle = addInterface->RegisterDeviceLostCallback(
1310 [this, InterfaceKey](const std::string& lostDeviceId) {
1311 const auto interfaceIt = m_interfaceCallbackContainers.find(InterfaceKey);
1312
1313 if (interfaceIt != std::end(m_interfaceCallbackContainers))
1314 {
1315 auto& container = interfaceIt->second;
1316 std::lock_guard<std::mutex> lock(container.mutex);
1317
1318 const auto lostDeviceKeyIt = container.deviceIdKeyMap.find(lostDeviceId);
1319 if (lostDeviceKeyIt != std::end(container.deviceIdKeyMap))
1320 {
1321 container.lostDevices.emplace_back(lostDeviceKeyIt->second);
1322 container.deviceIdKeyMap.erase(lostDeviceKeyIt);
1323 }
1324 }
1325 });
1326
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);
1331 });
1332
1333 const auto disconnectedHandle = addInterface->RegisterDeviceDisconnectedCallback(
1334 [this](const std::shared_ptr<core::DeviceDescriptor>& disconnectedDevice) {
1335 return m_deviceDisconnectedCallbackManager.TriggerCallbacks(disconnectedDevice);
1336 });
1337
1338 const auto deviceListChangedHandle = addInterface->RegisterDeviceListChangedCallback(
1339 [this]() { return m_deviceListChangedCallbackManager.TriggerCallbacks(); });
1340
1341 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1342
1343 m_interfaceCallbackContainers.emplace(addInterface->Key(),
1344 InterfaceCallbackContainer{
1345 foundCallbackHandle, lostCallbackHandle, disconnectedHandle, reconnectedHandle, deviceListChangedHandle });
1346}
1347
1348inline void DeviceManager::RemoveInterfaceCallbacks(const std::shared_ptr<core::Interface>& removeInterface)
1349{
1350 std::lock_guard<std::mutex> lock(m_callbackContainerMutex);
1351
1352 const auto interfaceIt = m_interfaceCallbackContainers.find(removeInterface->Key());
1353
1354 if (interfaceIt != std::end(m_interfaceCallbackContainers))
1355 {
1356 const auto& container = interfaceIt->second;
1357 {
1358 std::lock_guard<std::mutex> lockContainer(container.mutex);
1359
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);
1365 }
1366 m_interfaceCallbackContainers.erase(interfaceIt);
1367 }
1368}
1369
1370inline void DeviceManager::CheckDevicesOpened() const
1371{
1372 size_t openDevicesFound = 0;
1373 std::string deviceDisplayNames;
1374
1375 {
1376 std::lock_guard<std::mutex> lock(m_openedDevicesMutex);
1377
1378 for (const auto& openedDevice : m_openedDevices)
1379 {
1380 auto openDevice = openedDevice.device.lock();
1381 if (openDevice)
1382 {
1383 ++openDevicesFound;
1384 deviceDisplayNames += openedDevice.displayName;
1385 deviceDisplayNames += " ";
1386 }
1387 }
1388 }
1389
1390 if (openDevicesFound > 0)
1391 {
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);
1395 }
1396}
1397
1399{
1400 return m_interfaceUpdateTimeout_ms;
1401}
1402
1404{
1405 m_interfaceUpdateTimeout_ms = timeout_ms;
1406}
1407
1409{
1410 return m_deviceUpdateTimeout_ms;
1411}
1412
1414{
1415 m_deviceUpdateTimeout_ms = timeout_ms;
1416}
1417
1418inline std::vector<std::shared_ptr<const core::System>> DeviceManager::Systems() const
1419{
1420 std::lock_guard<std::mutex> lock(m_systemsMutex);
1421
1422 return m_systems;
1423}
1424
1425inline std::vector<std::shared_ptr<const core::Interface>> DeviceManager::Interfaces() const
1426{
1427 std::lock_guard<std::recursive_mutex> lock(m_interfacesMutex);
1428
1429 return m_interfaces;
1430}
1431
1432inline std::vector<std::shared_ptr<core::DeviceDescriptor>> DeviceManager::Devices() const
1433{
1434 std::lock_guard<std::recursive_mutex> lock(m_devicesMutex);
1435
1436 return m_devices;
1437}
1438
1440 const DeviceManager::SystemFoundCallback& callback)
1441{
1442 return m_systemFoundCallbackManager.RegisterCallback(callback);
1443}
1444
1446{
1447 m_systemFoundCallbackManager.UnregisterCallback(callbackHandle);
1448}
1449
1452{
1453 return m_interfaceFoundCallbackManager.RegisterCallback(callback);
1454}
1455
1457{
1458 m_interfaceFoundCallbackManager.UnregisterCallback(callbackHandle);
1459}
1460
1463{
1464 return m_interfaceLostCallbackManager.RegisterCallback(callback);
1465}
1466
1468{
1469 m_interfaceLostCallbackManager.UnregisterCallback(callbackHandle);
1470}
1471
1473 const DeviceManager::DeviceFoundCallback& callback)
1474{
1475 return m_deviceFoundCallbackManager.RegisterCallback(callback);
1476}
1477
1479{
1480 m_deviceFoundCallbackManager.UnregisterCallback(callbackHandle);
1481}
1482
1484 const DeviceManager::DeviceLostCallback& callback)
1485{
1486 return m_deviceLostCallbackManager.RegisterCallback(callback);
1487}
1488
1490{
1491 m_deviceLostCallbackManager.UnregisterCallback(callbackHandle);
1492}
1493
1496{
1497 return m_deviceDisconnectedCallbackManager.RegisterCallback(callback);
1498}
1499
1502{
1503 m_deviceDisconnectedCallbackManager.UnregisterCallback(callbackHandle);
1504}
1505
1508{
1509 return m_deviceReconnectedCallbackManager.RegisterCallback(callback);
1510}
1511
1514{
1515 m_deviceReconnectedCallbackManager.UnregisterCallback(callbackHandle);
1516}
1517
1520{
1521 return m_deviceListChangedCallbackManager.RegisterCallback(callback);
1522}
1523
1526{
1527 m_deviceListChangedCallbackManager.UnregisterCallback(callbackHandle);
1528}
1529
1530inline void DeviceManager::GetLostDevicesFromLostInterfaces(
1531 const std::vector<std::string>& lostInterfaces, std::vector<std::string>& lostDevices)
1532{
1533 std::lock_guard<std::recursive_mutex> lock(m_devicesMutex);
1534
1535 for (const auto& device : m_devices)
1536 {
1537 auto resultIt = std::find(
1538 std::begin(lostInterfaces), std::end(lostInterfaces), device->ParentInterface()->Key());
1539 if (resultIt != std::end(lostInterfaces))
1540 {
1541 lostDevices.emplace_back(device->Key());
1542 }
1543 }
1544}
1545
1546inline void DeviceManager::TriggerSystemFoundCallbacks(const std::shared_ptr<const core::System>& foundSystem) const
1547{
1548 m_systemFoundCallbackManager.TriggerCallbacks(foundSystem);
1549}
1550
1551inline void DeviceManager::TriggerInterfaceFoundCallbacks(
1552 const std::shared_ptr<const core::Interface>& foundInterface) const
1553{
1554 m_interfaceFoundCallbackManager.TriggerCallbacks(foundInterface);
1555}
1556
1557inline void DeviceManager::TriggerInterfaceLostCallbacks(const std::string& lostInterfaceKey) const
1558{
1559 m_interfaceLostCallbackManager.TriggerCallbacks(lostInterfaceKey);
1560}
1561
1562inline void DeviceManager::TriggerDeviceFoundCallbacks(const std::shared_ptr<core::DeviceDescriptor>& foundDevice) const
1563{
1564 m_deviceFoundCallbackManager.TriggerCallbacks(foundDevice);
1565}
1566
1567inline void DeviceManager::TriggerDeviceLostCallbacks(const std::string& lostDeviceKey) const
1568{
1569 m_deviceLostCallbackManager.TriggerCallbacks(lostDeviceKey);
1570}
1571
1572} /* namespace peak */
The global DeviceManager (singleton) searches all installed producer libraries (*....
Definition peak_device_manager.hpp:76
core::Timeout InterfaceUpdateTimeout() const
Returns the interface update timeout.
Definition peak_device_manager.hpp:1398
std::function< void(const std::string &lostInterfaceKey)> InterfaceLostCallback
Definition peak_device_manager.hpp:111
InterfaceFoundCallbackHandle RegisterInterfaceFoundCallback(const InterfaceFoundCallback &callback)
Registers a callback for signaling a found interface.
Definition peak_device_manager.hpp:1450
void UnregisterDeviceFoundCallback(DeviceFoundCallbackHandle callbackHandle)
Unregisters a device found callback.
Definition peak_device_manager.hpp:1478
void UnregisterDeviceDisconnectedCallback(DeviceDisconnectedCallbackHandle callbackHandle)
Unregisters a device disconnected callback.
Definition peak_device_manager.hpp:1500
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &reconnectedDevice, core::DeviceReconnectInformation reconnectInformation)> DeviceReconnectedCallback
Definition peak_device_manager.hpp:138
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &foundDevice)> DeviceFoundCallback
Definition peak_device_manager.hpp:117
std::vector< std::shared_ptr< const core::System > > Systems() const
Returns the found and opened systems.
Definition peak_device_manager.hpp:1418
DeviceReconnectedCallback * DeviceReconnectedCallbackHandle
Definition peak_device_manager.hpp:141
void UnregisterInterfaceLostCallback(InterfaceLostCallbackHandle callbackHandle)
Unregisters an interface lost callback.
Definition peak_device_manager.hpp:1467
void Reset(ResetPolicy resetPolicy=ResetPolicy::ErrorOnOpenDevices)
Resets the DeviceManager.
Definition peak_device_manager.hpp:1149
UpdatePolicy
Enum holding the possible update policies.
Definition peak_device_manager.hpp:80
ResetPolicy
Enum holding the possible reset policies.
Definition peak_device_manager.hpp:90
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition peak_device_manager.hpp:108
void UnregisterDeviceLostCallback(DeviceLostCallbackHandle callbackHandle)
Unregisters a device lost callback.
Definition peak_device_manager.hpp:1489
core::Timeout DeviceUpdateTimeout() const
Returns the device update timeout.
Definition peak_device_manager.hpp:1408
void Update(UpdatePolicy updatePolicy=UpdatePolicy::ScanEnvironmentForProducerLibraries, const UpdateErrorCallback &errorCallback=UpdateErrorCallback())
Starts an update.
Definition peak_device_manager.hpp:890
std::function< void()> DeviceListChangedCallback
Definition peak_device_manager.hpp:144
DeviceDisconnectedCallback * DeviceDisconnectedCallbackHandle
Definition peak_device_manager.hpp:133
std::function< void(const std::shared_ptr< const core::Interface > &foundInterface)> InterfaceFoundCallback
Definition peak_device_manager.hpp:105
DeviceManager & operator=(const DeviceManager &other)=delete
static DeviceManager & Instance()
Returns the global DeviceManager.
Definition peak_device_manager.hpp:843
std::function< void(const std::string &errorDescription)> UpdateErrorCallback
Definition peak_device_manager.hpp:150
std::function< void(const std::shared_ptr< const core::System > &foundSystem)> SystemFoundCallback
Definition peak_device_manager.hpp:99
DeviceReconnectedCallbackHandle RegisterDeviceReconnectedCallback(const DeviceReconnectedCallback &callback)
Registers a callback for signaling a reconnected device.
Definition peak_device_manager.hpp:1506
SystemFoundCallback * SystemFoundCallbackHandle
Definition peak_device_manager.hpp:102
DeviceManager(const DeviceManager &other)=delete
void UnregisterDeviceListChangedCallback(DeviceListChangedCallbackHandle callbackHandle)
Unregisters a device list change callback.
Definition peak_device_manager.hpp:1524
std::vector< std::shared_ptr< core::DeviceDescriptor > > Devices() const
Returns the found devices.
Definition peak_device_manager.hpp:1432
std::function< void(const std::string &lostDeviceKey)> DeviceLostCallback
Definition peak_device_manager.hpp:123
void AddProducerLibrary(const std::string &ctiPath)
Adds the given producer library (CTI).
Definition peak_device_manager.hpp:853
SystemFoundCallbackHandle RegisterSystemFoundCallback(const SystemFoundCallback &callback)
Registers a callback for signaling a found system.
Definition peak_device_manager.hpp:1439
InterfaceLostCallbackHandle RegisterInterfaceLostCallback(const InterfaceLostCallback &callback)
Registers a callback for signaling a lost interface.
Definition peak_device_manager.hpp:1461
DeviceDisconnectedCallbackHandle RegisterDeviceDisconnectedCallback(const DeviceDisconnectedCallback &callback)
Registers a callback for signaling a disconnected device.
Definition peak_device_manager.hpp:1494
std::function< void(const std::shared_ptr< core::DeviceDescriptor > &disconnectedDevice)> DeviceDisconnectedCallback
Definition peak_device_manager.hpp:130
InterfaceLostCallback * InterfaceLostCallbackHandle
Definition peak_device_manager.hpp:114
DeviceFoundCallbackHandle RegisterDeviceFoundCallback(const DeviceFoundCallback &callback)
Registers a callback for signaling a found device.
Definition peak_device_manager.hpp:1472
DeviceFoundCallback * DeviceFoundCallbackHandle
Definition peak_device_manager.hpp:120
void UnregisterDeviceReconnectedCallback(DeviceReconnectedCallbackHandle callbackHandle)
Unregisters a device reconnected callback.
Definition peak_device_manager.hpp:1512
DeviceListChangedCallbackHandle RegisterDeviceListChangedCallback(const DeviceListChangedCallback &callback)
Registers a callback for signaling a change in the device list.
Definition peak_device_manager.hpp:1518
DeviceListChangedCallback * DeviceListChangedCallbackHandle
Definition peak_device_manager.hpp:147
void UnregisterSystemFoundCallback(SystemFoundCallbackHandle callbackHandle)
Unregisters a system found callback.
Definition peak_device_manager.hpp:1445
void SetInterfaceUpdateTimeout(core::Timeout timeout_ms)
Sets the interface update timeout.
Definition peak_device_manager.hpp:1403
void SetDeviceUpdateTimeout(core::Timeout timeout_ms)
Sets the device update timeout.
Definition peak_device_manager.hpp:1413
DeviceLostCallbackHandle RegisterDeviceLostCallback(const DeviceLostCallback &callback)
Registers a callback for signaling a lost device.
Definition peak_device_manager.hpp:1483
std::vector< std::shared_ptr< const core::Interface > > Interfaces() const
Returns the found and opened interfaces.
Definition peak_device_manager.hpp:1425
DeviceLostCallback * DeviceLostCallbackHandle
Definition peak_device_manager.hpp:126
void UnregisterInterfaceFoundCallback(InterfaceFoundCallbackHandle callbackHandle)
Unregisters a interface found callback.
Definition peak_device_manager.hpp:1456
A collection of global library functions.
Definition peak_library.hpp:31
Definition reconnect_information.hpp:15
static std::vector< std::string > CollectCTIPaths()
Collects producer library paths found in the current environment.
Definition peak_environment_inspector.hpp:71
The base class for all exceptions thrown by the library.
Definition peak_exception.hpp:26
The exception thrown for signaling that a feature is not available in the device.
Definition peak_exception.hpp:98
The exception thrown for signaling that the library was not initialized.
Definition peak_exception.hpp:124
static std::shared_ptr< ProducerLibrary > Open(const std::string &ctiPath)
Opens the given producer library (CTI).
Definition peak_producer_library.hpp:109
InterfaceFoundCallback * InterfaceFoundCallbackHandle
Definition peak_system.hpp:56
Represents a timeout value in milliseconds.
Definition peak_timeout.hpp:31
The "peak" namespace directly contains convenience functionality like the DeviceManager as well as th...
Definition peak_dynamic_loader.h:32