CVB++ 15.0
system_info.hpp
1#pragma once
2
3#include <chrono>
4#include <codecvt>
5#include <exception>
6#include <locale>
7#include <string>
8#include <sstream>
9#include <thread>
10#include <vector>
11
12
13#include "../_cexports/c_utilities.h"
14#include "../_cexports/c_core.h"
15
16#include "../exception.hpp"
17#include "../string.hpp"
18
19namespace Cvb
20{
21
22 CVB_BEGIN_INLINE_NS
23
24 namespace Utilities
25 {
26
28 namespace SystemInfo
29 {
30
31
32
33
35
37 class LicenseInfo final
38 {
40
41 public:
42
44
48 int SerialNumber() const noexcept
49 {
50 return serialNumber_;
51 }
52
54
58 bool IsFoundation() const noexcept
59 {
60 return isFoundation_;
61 }
62
63 private:
64
65 LicenseInfo(int serialNumber, bool isFoundation) noexcept
66 : serialNumber_(serialNumber)
67 , isFoundation_(isFoundation)
68 {
69 }
70
71
72 int serialNumber_ = 0;
73 bool isFoundation_ = false;
74 };
75
77
79 class MagicNumberEntry final
80 {
82
83 public:
84
86
90 String Provider() const noexcept
91 {
92 return provider_;
93 }
94
96
100 String ToolID() const noexcept
101 {
102 return toolID_;
103 }
104
106
110 String MagicNumber() const noexcept
111 {
112 return magicNumber_;
113 }
114
115
117
121 int SerialNumber() const noexcept
122 {
123 return serialNumber_;
124 }
125
126 private:
127
128 MagicNumberEntry(const String& provider, const String& toolID, const String& magicNumber, int serialNumber) noexcept
129 : provider_(provider)
130 , toolID_(toolID)
131 , magicNumber_(magicNumber)
132 , serialNumber_(serialNumber)
133 {
134 }
135
136 String provider_;
137 String toolID_;
138 String magicNumber_;
139 int serialNumber_ = 0;
140 };
141
142
144
149 inline std::string GetLastErrorMessage(int& errorCode)
150 {
151 CExports::cvbres_t code = 0;
152 std::size_t messageSize = 0;
153 auto res = CVB_CALL_CAPI(CVCGetLastErrorW(code, nullptr, messageSize));
154 if (res == 0)
155 {
156 std::wstring message;
157 message.resize(messageSize);
158 res = CVB_CALL_CAPI(CVCGetLastErrorW(code, &message[0], messageSize));
159 if (res == 0)
160 {
161 errorCode = static_cast<int>(code);
162 // currently the only standard way
163 return Internal::WideCharStrToUTF8(message);
164 }
165 }
166 errorCode = static_cast<int>(res);
167 return std::string();
168 }
169
170
172
177 {
178 int dummy = 0;
179 std::string message = GetLastErrorMessage(dummy);
180 return message;
181 }
182
183
185
192 {
193 int errorCode = 0;
194 auto message = GetLastErrorMessage(errorCode);
195 if (errorCode == 0)
196 return std::exception_ptr();
197
198 return CvbException::FromCvbResult(errorCode, message);
199 }
200
201
202 inline void ThrowLastError(int errorCode)
203 {
204 auto exception = GetLastError();
205 // Raise an exception for the code specified by errorCode:
206 if (!exception)
207 std::rethrow_exception(CvbException::FromCvbResult(errorCode, "no specific message"));
208 std::rethrow_exception(exception);
209 }
210
211 inline void ThrowLastError()
212 {
213 ThrowLastError(ErrorCodes::CVB_ERROR);
214 }
215
216 inline std::chrono::microseconds DefaultTimeout()
217 {
218 CExports::cvbval_t defaultTimeout = 0;
219 if (!CExports::GetDefaultTimeout(defaultTimeout))
220 return std::chrono::milliseconds(10000);
221
222 return std::chrono::milliseconds(defaultTimeout);
223 }
224
225
226
228
234 inline String ExpandPath(const String& path)
235 {
236 std::vector<Char> buffer(CExports::CVB_MAX_PATH);
237 if (!CExports::TranslateFileNameTyped(path.c_str(), &buffer[0], CExports::CVB_MAX_PATH))
238 ThrowLastError();
239 return String(&buffer[0]);
240 }
241
243
248 {
249 std::vector<Char> buffer(260);
250 if (!CExports::GetCVBDirectoryTyped(&buffer[0], buffer.size()))
251 ThrowLastError();
252
253 String path(&buffer[0]);
254
255#ifndef _WIN32
256 if (path.back() != CVB_LIT('/'))
257 path += CVB_LIT('/');
258#endif
259 return path;
260 }
261
263
268 {
269 std::vector<Char> buffer(260);
270 if (!CExports::GetCVBDataDirectoryTyped(&buffer[0], buffer.size()))
271 ThrowLastError();
272
273 return String(&buffer[0]);
274 }
275
276
278
291 {
292 auto resultUpdate = CVB_CALL_CAPI(UpdateLicenses());
293 if (resultUpdate < 0)
294 std::rethrow_exception(CvbException::FromCvbResult(resultUpdate, "failed to update licenses"));
295
296 auto licCount = static_cast<int>(CExports::GetLicenseCount());
297
298 std::vector<LicenseInfo> licenseList;
299
300 for (int i = 0; i < licCount; ++i)
301 {
302 CExports::cvblicres_t serialNumber = 0;
303 CExports::cvbbool_t isFoundation = false;
304
305 auto resultInfo = CVB_CALL_CAPI(GetLicenseInfoEx(static_cast<CExports::cvbval_t>(i), serialNumber, isFoundation));
306 if (resultInfo < 0)
307 ThrowLastError(resultInfo);
308
309 licenseList.push_back(LicenseInfo(static_cast<int>(serialNumber), (isFoundation) ? true : false));
310 }
311 return licenseList;
312
313 }
314
315
317
328 template<class Rep, class Period>
330 {
331 auto start = std::chrono::system_clock::now();
332
333 while (true)
334 {
335 auto result = CVB_CALL_CAPI(UpdateLicenses());
336 if (result >= 0 && CExports::GetLicenseCount() > 0)
337 return WaitStatus::Ok;
338 else
339 {
340 // wait until services are running
343 if (end - start > timeSpan)
344 return WaitStatus::Timeout;
345 }
346 }
347 }
348
350
364 {
365 auto resultUpdate = CVB_CALL_CAPI(UpdateLicensesDeep());
366 if (resultUpdate < 0)
367 ThrowLastError(resultUpdate);
368
369 auto magicNumberCount = static_cast<int>(CExports::GetMagicNumberCount());
370
372
373 for (int i = 0; i < magicNumberCount; ++i)
374 {
375 std::size_t providerSize = 0;
376 std::size_t toolIDSize = 0;
377 std::size_t magicNumberSize = 0;
378 CExports::cvblicres_t serialNumber = 0;
379 auto resultMnSize = CVB_CALL_CAPI(GetMagicNumber(static_cast<std::size_t>(i),
380 nullptr, providerSize,
381 nullptr, toolIDSize,
382 nullptr, magicNumberSize,
383 serialNumber));
384 if (resultMnSize >= 0)
385 {
386 std::vector<char> provider(providerSize);
387 std::vector<char> toolID(toolIDSize);
388 std::vector<char> magicNumber(magicNumberSize);
389 auto resultMn = CVB_CALL_CAPI(GetMagicNumber(static_cast<std::size_t>(i),
390 &provider[0], providerSize,
391 &toolID[0], toolIDSize,
392 &magicNumber[0], magicNumberSize,
393 serialNumber));
394 if (resultMn < 0)
395 continue;
396
397 entryList.push_back(MagicNumberEntry(String(provider.begin(), provider.end()),
398 String(toolID.begin(), toolID.end()),
399 String(magicNumber.begin(), magicNumber.end()),
400 static_cast<int>(serialNumber)));
401 }
402
403 }
404
405 return entryList;
406 }
407
408 }
409
418
419 }
420
423 using Utilities::ExpandPath;
424 using Utilities::InstallPath;
425 using Utilities::DataPath;
426 using Utilities::GetLicenseInfo;
427 using Utilities::WaitForLicense;
428 using Utilities::GetMagicNumberEntries;
429
430 CVB_END_INLINE_NS
431
432}
Information about CVB licenses.
Definition: system_info.hpp:38
int SerialNumber() const noexcept
The 32 bit CVB serial number of this license.
Definition: system_info.hpp:48
bool IsFoundation() const noexcept
Check if the license includes the Common Vision Blox Foundation package.
Definition: system_info.hpp:58
friend std::vector< LicenseInfo > GetLicenseInfo()
Get information about available licenses.
Definition: system_info.hpp:290
A single Magic Number entry.
Definition: system_info.hpp:80
String MagicNumber() const noexcept
The Magic Number which is tool-specific and at the same time specific to the Common Vision Blox Seria...
Definition: system_info.hpp:110
String ToolID() const noexcept
The tool ID of the Magic Number entry.
Definition: system_info.hpp:100
String Provider() const noexcept
The friendly (= human-readable) name of the license provider, that reported this Magic Number entry.
Definition: system_info.hpp:90
int SerialNumber() const noexcept
The serial number assigned to this Magic Number entry (0 if it has no inherent link to a serial numbe...
Definition: system_info.hpp:121
friend std::vector< MagicNumberEntry > GetMagicNumberEntries()
Query the set of currently available Magic Numbers.
Definition: system_info.hpp:363
const int CVB_ERROR
Generic unspecified error.
Definition: exception.hpp:24
String ExpandPath(const String &path)
Expands a path containing an environment variable.
Definition: system_info.hpp:234
String DataPath()
Directory where Common Vision Blox stores its settings.
Definition: system_info.hpp:267
WaitStatus WaitForLicense(const std::chrono::duration< Rep, Period > &timeSpan) noexcept
Wait for a given time for the license to become available.
Definition: system_info.hpp:329
std::vector< MagicNumberEntry > GetMagicNumberEntries()
Query the set of currently available Magic Numbers.
Definition: system_info.hpp:363
std::string GetLastErrorMessage(int &errorCode)
Returns the last error message and its code.
Definition: system_info.hpp:149
std::exception_ptr GetLastError() noexcept
Returns a pointer that points at the exception.
Definition: system_info.hpp:191
String InstallPath()
Directory Common Vision Blox has been installed to.
Definition: system_info.hpp:247
std::vector< LicenseInfo > GetLicenseInfo()
Get information about available licenses.
Definition: system_info.hpp:290
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
WaitStatus
Status after waiting for an image to be returned.
Definition: global.hpp:376
@ Ok
Everything is fine, a new image arrived.
@ Timeout
A timeout occurred, no image buffer has been returned.
T sleep_for(T... args)