CVB++ 15.0
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) noexcept
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 String device;
96 String firmware;
97 // Check all available file infos for name
98 for (size_t fileInfo = 0; fileInfo < updateFileInfoCount; fileInfo++)
99 {
100 auto key = UpdateFileInfo(updateFile, fileInfo, Cvb::GenApi::FirmwareUpdateInfo::Key);
101 auto value = UpdateFileInfo(updateFile, fileInfo, Cvb::GenApi::FirmwareUpdateInfo::Value);
102 availableGufInformation.push_back(std::make_pair(key, value));
103 }
104 }
105 return availableGufInformation;
106 }
107
109 {
110 std::vector<String> availableFileDescriptions;
111 auto updateFileCount = UpdateFileCount();
112 // Check all available update files within guf file
113 for (size_t updateFile = 0; updateFile < updateFileCount; updateFile++)
114 {
115 auto updateFileInfoCount = UpdateFileInfoCount(updateFile);
116 // Check all available file infos for name
117 String fileDescription;
118 String fileFirmwareVersion;
119 for (size_t fileInfo = 0; fileInfo < updateFileInfoCount; fileInfo++)
120 {
121 auto key = UpdateFileInfo(updateFile, fileInfo, Cvb::GenApi::FirmwareUpdateInfo::Key);
122 auto value = UpdateFileInfo(updateFile, fileInfo, Cvb::GenApi::FirmwareUpdateInfo::Value);
123 if (key == CVB_LIT("Description"))
124 fileDescription = value;
125 if (key == CVB_LIT("Version"))
126 fileFirmwareVersion = value;
127 }
128 availableFileDescriptions.push_back(fileDescription + CVB_LIT(" version ") + fileFirmwareVersion);
129 }
130 return availableFileDescriptions;
131 }
132
133 inline std::vector<String> FWUpdater::UpdateFileInfos(const size_t &updateFileIndex)
134 {
135 std::vector<String> availableFileInfos;
136 auto updateFileInfoCount = UpdateFileInfoCount(updateFileIndex);
137 for (size_t fileInfo = 0; fileInfo < updateFileInfoCount; fileInfo++)
138 {
139 auto key = UpdateFileInfo(updateFileIndex, fileInfo, Cvb::GenApi::FirmwareUpdateInfo::Key);
140 auto value = UpdateFileInfo(updateFileIndex, fileInfo, Cvb::GenApi::FirmwareUpdateInfo::Value);
141 availableFileInfos.push_back(key + CVB_LIT(": ") + value);
142 }
143 return availableFileInfos;
144 }
145
146 inline void FWUpdater::Update(DevicePtr &&device, const size_t updateFileSelection)
147 {
148 SetDevice(std::move(device));
149
150 NativeCall([&]() {
151 return CVB_CALL_CAPI(
152 FWUpdate(Handle(), deviceNM_->Handle(), updateFileSelection, &FWUpdater::EventOnDeviceReset, this));
153 });
154 device = std::move(device_);
155 }
156
157 inline CExports::cvbbool_t __stdcall FWUpdater::EventOnDeviceReset(unsigned int deviceDiscoveryTimeoutMs,
158 unsigned int deviceDiscoveryDelayMs,
159 CExports::NODEMAP &nodeMap, void *pPrivate)
160 {
161 try
162 {
163 auto fwu = reinterpret_cast<FWUpdater *>(pPrivate);
164
165 // Let device and nodemap go
166 fwu->ResetDevice();
167
168 // Wait for device to reset
170
171 // Discover device after reset
173 std::chrono::system_clock::rep runTimeMs = {0};
174 do
175 {
176 try
177 {
179 for (const auto &device : discover)
180 {
181 // Open same device again an re-set new nodemap
182 Cvb::String discoverSn{CVB_LIT("")};
183 if (device.TryGetProperty(Cvb::Driver::DiscoveryProperties::DeviceSerialNumber, discoverSn))
184 {
185 if (fwu->DeviceSerialNumber() == discoverSn)
186 {
187 // Poll device since its availability depends on the environment (like DHCP server etc.)
188 do
189 {
190 try
191 {
192 DevicePtr dev = {Cvb::DeviceFactory::Open(device.AccessToken(), AcquisitionStack::PreferGenTL)};
193 fwu->SetDevice(std::move(dev));
194 nodeMap = fwu->deviceNM_->Handle();
195 return true;
196 }
197 catch (...)
198 {
199 }
201 runTimeMs = (endTime - startTime) / std::chrono::milliseconds(1);
202 } while (runTimeMs < deviceDiscoveryTimeoutMs);
203 }
204 }
205 }
206 }
207 catch (...)
208 {
209 }
211 runTimeMs = (endTime - startTime) / std::chrono::milliseconds(1);
212
213 } while (runTimeMs < deviceDiscoveryTimeoutMs);
214 return false; // Timeout
215 }
216 catch (...)
217 {
218 return false;
219 }
220 }
221
222 inline String FWUpdater::DeviceSerialNumber() const noexcept
223 {
224 return deviceSn_;
225 }
226
227 inline void FWUpdater::SetDevice(DevicePtr &&device)
228 {
229 device_ = std::move(device);
230 deviceNM_ = device_->NodeMap(CVB_LIT("Device"));
231 deviceSn_ = deviceNM_->Node<StringNode>(CVB_LIT("DeviceSerialNumber"))->Value();
232 }
233
234 inline void FWUpdater::ResetDevice()
235 {
236 deviceNM_->Node<CommandNode>(CVB_LIT("DeviceReset"))->Execute();
237 deviceNM_.reset();
238 device_.reset();
239 }
240
241 } // namespace GenApi
242
243 CVB_END_INLINE_NS
244
245} // 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:133
std::vector< String > AvailableUpdateFiles()
Collects all mandatory update file infos available in a guf file.
Definition detail_fw_updater.hpp:108
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:146
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 c_bayer_to_rgb.h:17
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)