CVB++ 15.1
Loading...
Searching...
No Matches
detail_fw_updater.hpp
1#pragma once
2
3#include <memory>
4#include <stdexcept>
5#include <vector>
6#include <thread>
7#include <chrono>
8
9#include "../../_cexports/c_gen_api.h"
10
11#include "../../global.hpp"
12
13#include "../_decl/decl_node.hpp"
14#include "../_decl/decl_node_map.hpp"
15#include "../_decl/decl_fw_updater.hpp"
16#include "../../device.hpp"
17#include "../../device_factory.hpp"
18#include "../node.hpp"
19#include "../string_node.hpp"
20
21namespace Cvb
22{
23
24 CVB_BEGIN_INLINE_NS
25
26 namespace GenApi
27 {
28 inline std::unique_ptr<FWUpdater> FWUpdater::Create(const String &filename, bool verify)
29 {
30 std::string cname(Internal::CastToAscii(filename));
31 auto firmwareUpdater = Internal::DoResCallObjectOut<FWUpdater>(
32 [&](void *&handle) { return CVB_CALL_CAPI(CreateFirmwareUpdater(cname.c_str(), verify, handle)); }, filename);
33 return firmwareUpdater;
34 }
35
36 inline FWUpdater::FWUpdater(const String &filename, bool verify)
37 : FWUpdater(std::move(*Create(filename, verify)))
38 {
39 }
40
41 inline FWUpdater::FWUpdater(HandleGuard<FWUpdater> &&guard, const String &filename, PrivateTag)
42 : handle_(std::move(guard))
43 , filename_(filename)
44 {
45 }
46
47 inline std::unique_ptr<FWUpdater> FWUpdater::FromHandle(HandleGuard<FWUpdater> &&guard, const String &filename)
48 {
49 if (!guard.Handle())
50 throw std::runtime_error("handle must not be null");
51
52 return std::make_unique<FWUpdater>(std::move(guard), filename, PrivateTag{});
53 }
54
56 {
57 return NativeCall<size_t>([&](size_t &count) { return CVB_CALL_CAPI(FWUpdateFileCount(Handle(), count)); });
58 }
59
60 inline size_t FWUpdater::UpdateFileInfoCount(const size_t &fileIndex)
61 {
62 return NativeCall<size_t>(
63 [&](size_t &count) { return CVB_CALL_CAPI(FWUpdateFileInfoCount(Handle(), fileIndex, count)); });
64 }
65
66 inline String FWUpdater::UpdateFileInfo(size_t fileIndex, size_t infoIndex, FirmwareUpdateInfo cmd)
67 {
68 return NativeCall<String>([&](String &value) {
69 std::size_t bufferSize = 0;
70 auto bufferSizeResult = CExports::FWUpdateFileInfoTyped(Handle(), fileIndex, infoIndex,
71 static_cast<CExports::TFirmwareUpdateInfo>(cmd),
72 reinterpret_cast<Char *>(0), bufferSize);
73 if (bufferSizeResult < 0)
74 return bufferSizeResult;
75
76 std::vector<Char> buffer(bufferSize);
77 auto bufferResult = CExports::FWUpdateFileInfoTyped(
78 Handle(), fileIndex, infoIndex, static_cast<CExports::TFirmwareUpdateInfo>(cmd), buffer.data(), bufferSize);
79 if (bufferResult < 0)
80 return bufferResult;
81
82 value = String(buffer.data());
83 return bufferResult;
84 });
85 }
86
88 {
89 std::vector<std::pair<String, String>> availableGufInformation;
90 auto updateFileCount = UpdateFileCount();
91 // Check all available update files within guf file
92 for (size_t updateFile = 0; updateFile < updateFileCount; updateFile++)
93 {
94 auto updateFileInfoCount = UpdateFileInfoCount(updateFile);
95 // Check all available file infos for name
96 for (size_t fileInfo = 0; fileInfo < updateFileInfoCount; fileInfo++)
97 {
98 auto key = UpdateFileInfo(updateFile, fileInfo, Cvb::GenApi::FirmwareUpdateInfo::Key);
99 auto value = UpdateFileInfo(updateFile, fileInfo, Cvb::GenApi::FirmwareUpdateInfo::Value);
100 availableGufInformation.push_back(std::make_pair(key, value));
101 }
102 }
103 return availableGufInformation;
104 }
105
107 {
108 std::vector<String> availableFileDescriptions;
109 auto updateFileCount = UpdateFileCount();
110 // Check all available update files within guf file
111 for (size_t updateFile = 0; updateFile < updateFileCount; updateFile++)
112 {
113 auto updateFileInfoCount = UpdateFileInfoCount(updateFile);
114 // Check all available file infos for name
115 String fileDescription;
116 String fileFirmwareVersion;
117 for (size_t fileInfo = 0; fileInfo < updateFileInfoCount; fileInfo++)
118 {
119 auto key = UpdateFileInfo(updateFile, fileInfo, Cvb::GenApi::FirmwareUpdateInfo::Key);
120 auto value = UpdateFileInfo(updateFile, fileInfo, Cvb::GenApi::FirmwareUpdateInfo::Value);
121 if (key == CVB_LIT("Description"))
122 fileDescription = value;
123 if (key == CVB_LIT("Version"))
124 fileFirmwareVersion = value;
125 }
126 availableFileDescriptions.push_back(fileDescription + CVB_LIT(" version ") + fileFirmwareVersion);
127 }
128 return availableFileDescriptions;
129 }
130
131 inline std::vector<String> FWUpdater::UpdateFileInfos(const size_t &updateFileIndex)
132 {
133 std::vector<String> availableFileInfos;
134 auto updateFileInfoCount = UpdateFileInfoCount(updateFileIndex);
135 for (size_t fileInfo = 0; fileInfo < updateFileInfoCount; fileInfo++)
136 {
137 auto key = UpdateFileInfo(updateFileIndex, fileInfo, Cvb::GenApi::FirmwareUpdateInfo::Key);
138 auto value = UpdateFileInfo(updateFileIndex, fileInfo, Cvb::GenApi::FirmwareUpdateInfo::Value);
139 availableFileInfos.push_back(key + CVB_LIT(": ") + value);
140 }
141 return availableFileInfos;
142 }
143
144 inline void FWUpdater::Update(DevicePtr &&device, const size_t updateFileSelection)
145 {
146 SetDevice(std::move(device));
147
148 NativeCall([&]() {
149 return CVB_CALL_CAPI(
150 FWUpdate(Handle(), deviceNM_->Handle(), updateFileSelection, &FWUpdater::EventOnDeviceReset, this));
151 });
152 device = std::move(device_);
153 }
154
155 inline CExports::cvbbool_t __stdcall FWUpdater::EventOnDeviceReset(unsigned int deviceDiscoveryTimeoutMs,
156 unsigned int deviceDiscoveryDelayMs,
157 CExports::NODEMAP &nodeMap, void *pPrivate)
158 {
159 try
160 {
161 auto fwu = reinterpret_cast<FWUpdater *>(pPrivate);
162
163 // Let device and nodemap go
164 fwu->ResetDevice();
165
166 // Wait for device to reset
168
169 // Discover device after reset
171 std::chrono::system_clock::rep runTimeMs = {0};
172 do // NOLINT(cppcoreguidelines-avoid-do-while)
173 {
174 try
175 {
177 for (const auto &device : discover)
178 {
179 // Open same device again an re-set new nodemap
180 Cvb::String discoverSn{CVB_LIT("")};
181 if (device.TryGetProperty(Cvb::Driver::DiscoveryProperties::DeviceSerialNumber, discoverSn))
182 {
183 if (fwu->DeviceSerialNumber() == discoverSn)
184 {
185 // Poll device since its availability depends on the environment (like DHCP server etc.)
186 do // NOLINT(cppcoreguidelines-avoid-do-while)
187 {
188 try
189 {
190 DevicePtr dev = {Cvb::DeviceFactory::Open(device.AccessToken(), AcquisitionStack::PreferGenTL)};
191 fwu->SetDevice(std::move(dev));
192 nodeMap = fwu->deviceNM_->Handle();
193 return true;
194 }
195 catch (...) // NOLINT(bugprone-empty-catch)
196 {
197 }
199 runTimeMs = (endTime - startTime) / std::chrono::milliseconds(1);
200 } while (runTimeMs < deviceDiscoveryTimeoutMs);
201 }
202 }
203 }
204 }
205 catch (...) // NOLINT(bugprone-empty-catch)
206 {
207 }
209 runTimeMs = (endTime - startTime) / std::chrono::milliseconds(1);
210
211 } while (runTimeMs < deviceDiscoveryTimeoutMs);
212 return false; // Timeout
213 }
214 catch (...)
215 {
216 return false;
217 }
218 }
219
220 inline String FWUpdater::DeviceSerialNumber() const
221 {
222 return deviceSn_;
223 }
224
225 inline void FWUpdater::SetDevice(DevicePtr &&device)
226 {
227 device_ = std::move(device);
228 deviceNM_ = device_->NodeMap(CVB_LIT("Device"));
229 deviceSn_ = deviceNM_->Node<StringNode>(CVB_LIT("DeviceSerialNumber"))->Value();
230 }
231
232 inline void FWUpdater::ResetDevice()
233 {
234 deviceNM_->Node<CommandNode>(CVB_LIT("DeviceReset"))->Execute();
235 deviceNM_.reset();
236 device_.reset();
237 }
238
239 } // namespace GenApi
240
241 CVB_END_INLINE_NS
242
243} // namespace Cvb
static std::vector< DiscoveryInformation > Discover()
Discovers available devices (not vins) with a default time span of 300ms.
Definition decl_device_factory.hpp:217
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
Opens a device with the given provider with its default board and port (if applicable).
Definition decl_device_factory.hpp:56
Firmware updater class.
Definition decl_fw_updater.hpp:40
std::vector< std::pair< String, String > > AvailableGufInfo()
Collects all key value pairs available in a guf file.
Definition detail_fw_updater.hpp:87
std::vector< String > UpdateFileInfos(const size_t &updateFileIndex)
Collects all information on a single update file available in a guf file.
Definition detail_fw_updater.hpp:131
std::vector< String > AvailableUpdateFiles()
Collects all mandatory update file infos available in a guf file.
Definition detail_fw_updater.hpp:106
String UpdateFileInfo(size_t fileIndex, size_t infoIndex, FirmwareUpdateInfo cmd)
Identifies a single firmware update and gets information about the firmware update as CVB::String.
Definition detail_fw_updater.hpp:66
size_t UpdateFileInfoCount(const size_t &fileIndex)
Gets the number of available update infos in a guf file.
Definition detail_fw_updater.hpp:60
static std::unique_ptr< FWUpdater > Create(const String &filename, bool verify=true)
Create a firmware updater, Validate a guf file and set the location of the guf file as CVB::String.
Definition detail_fw_updater.hpp:28
size_t UpdateFileCount()
Gets the number of available update files in a guf file.
Definition detail_fw_updater.hpp:55
static std::unique_ptr< FWUpdater > FromHandle(HandleGuard< FWUpdater > &&guard, const String &filename)
Creates a firmware updater from a classic API handle.
Definition detail_fw_updater.hpp:47
void * Handle() const noexcept
Classic API node map handle.
Definition decl_fw_updater.hpp:97
void Update(DevicePtr &&device, const size_t updateFileSelection)
Update the device.
Definition detail_fw_updater.hpp:144
T make_pair(T... args)
T move(T... args)
@ DeviceSerialNumber
Device only : Serial number.
Definition driver.hpp:247
@ FindAll
Default flags to find all devices and vin-drivers.
Definition driver.hpp:304
Namespace for GenApi based device configuration.
Definition decl_fw_updater.hpp:29
FirmwareUpdateInfo
Defines commands to retrieve firmware information from a guf file.
Definition genapi.hpp:259
@ Value
Gets the value for an info key name.
Definition genapi.hpp:263
@ Key
Gets the key name for a given index.
Definition genapi.hpp:261
Root namespace for the Image Manager interface.
Definition version.hpp:11
char Char
Character type for wide characters or unicode characters.
Definition string.hpp:63
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
std::shared_ptr< Device > DevicePtr
Convenience shared pointer for Device.
Definition global.hpp:98
T sleep_for(T... args)