CVB++ 14.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#include "../_cexports/c_utilities.h"
13#include "../_cexports/c_core.h"
14
15#include "../exception.hpp"
16#include "../string.hpp"
17
18namespace Cvb
19{
20
21CVB_BEGIN_INLINE_NS
22
23namespace Utilities
24{
25
27namespace SystemInfo
28{
29
31
33class LicenseInfo final
34{
36
37 public:
38
40
44 int SerialNumber() const noexcept
45 {
46 return serialNumber_;
47 }
48
50
54 bool IsFoundation() const noexcept
55 {
56 return isFoundation_;
57 }
58
59 private:
60
61 LicenseInfo(int serialNumber, bool isFoundation) noexcept
62 : serialNumber_(serialNumber)
63 , isFoundation_(isFoundation)
64 {
65 }
66
67
68 int serialNumber_ = 0;
69 bool isFoundation_ = false;
70};
71
73
76{
78
79 public:
80
82
86 String Provider() const noexcept
87 {
88 return provider_;
89 }
90
92
96 String ToolID() const noexcept
97 {
98 return toolID_;
99 }
100
102
106 String MagicNumber() const noexcept
107 {
108 return magicNumber_;
109 }
110
111
113
117 int SerialNumber() const noexcept
118 {
119 return serialNumber_;
120 }
121
122 private:
123
124 MagicNumberEntry(const String & provider, const String & toolID, const String& magicNumber, int serialNumber) noexcept
125 : provider_(provider)
126 , toolID_(toolID)
127 , magicNumber_(magicNumber)
128 , serialNumber_(serialNumber)
129 {
130 }
131
132 String provider_;
133 String toolID_;
134 String magicNumber_;
135 int serialNumber_ = 0;
136};
137
138
140
145inline std::string GetLastErrorMessage(int& errorCode)
146{
147 CExports::cvbres_t code = 0;
148 std::size_t messageSize = 0;
149 auto res = CVB_CALL_CAPI(CVCGetLastErrorW(code, nullptr, messageSize));
150 if (res == 0)
151 {
152 std::vector<wchar_t> message(messageSize);
153 res = CVB_CALL_CAPI(CVCGetLastErrorW(code, message.data(), messageSize));
154 if (res == 0)
155 {
157 errorCode = static_cast<int>(code);
158 return converter.to_bytes(message.data());
159 }
160 }
161 errorCode = static_cast<int>(res);
162 return std::string();
163}
164
165
167
172{
173 int dummy = 0;
174 std::string message = GetLastErrorMessage(dummy);
175 return message;
176}
177
178
180
187{
188 int errorCode = 0;
189 auto message = GetLastErrorMessage(errorCode);
190 if (errorCode == 0)
191 return std::exception_ptr();
192
193 return CvbException::FromCvbResult(errorCode, message);
194}
195
196
197inline void ThrowLastError(int errorCode)
198{
199 auto exception = GetLastError();
200 // Raise an exception for the code specified by errorCode:
201 if (!exception)
202 std::rethrow_exception(CvbException::FromCvbResult(errorCode, "no specific message"));
203 std::rethrow_exception(exception);
204}
205
206inline void ThrowLastError()
207{
208 ThrowLastError(ErrorCodes::CVB_ERROR);
209}
210
211inline std::chrono::microseconds DefaultTimeout()
212{
213 CExports::cvbval_t defaultTimeout;
214 if (!CExports::GetDefaultTimeout(defaultTimeout))
215 return std::chrono::milliseconds(10000);
216
217 return std::chrono::milliseconds(defaultTimeout);
218}
219
220
221
223
229inline String ExpandPath(const String& path)
230{
231 std::vector<Char> buffer(CExports::CVB_MAX_PATH);
232 if (!CExports::TranslateFileNameTyped(path.c_str(), &buffer[0], CExports::CVB_MAX_PATH))
233 ThrowLastError();
234 return String(&buffer[0]);
235}
236
238
243{
244 std::vector<Char> buffer(260);
245 if (!CExports::GetCVBDirectoryTyped(&buffer[0], buffer.size()))
246 ThrowLastError();
247
248 String path(&buffer[0]);
249
250#ifndef _WIN32
251 if (path.back() != CVB_LIT('/'))
252 path += CVB_LIT('/');
253#endif
254 return path;
255}
256
258
263{
264 std::vector<Char> buffer(260);
265 if (!CExports::GetCVBDataDirectoryTyped(&buffer[0], buffer.size()))
266 ThrowLastError();
267
268 return String(&buffer[0]);
269}
270
271
273
286{
287 auto resultUpdate = CVB_CALL_CAPI(UpdateLicenses());
288 if (resultUpdate < 0)
289 std::rethrow_exception(CvbException::FromCvbResult(resultUpdate, "failed to update licenses"));
290
291 auto licCount = static_cast<int>(CExports::GetLicenseCount());
292
293 std::vector<LicenseInfo> licenseList;
294
295 for (int i = 0; i < licCount; ++i)
296 {
297 CExports::cvblicres_t serialNumber;
298 CExports::cvbbool_t isFoundation;
299
300 auto resultInfo = CVB_CALL_CAPI(GetLicenseInfoEx(static_cast<CExports::cvbval_t>(i), serialNumber, isFoundation));
301 if (resultInfo < 0)
302 ThrowLastError(resultInfo);
303
304 licenseList.push_back(LicenseInfo(static_cast<int>(serialNumber), (isFoundation) ? true : false));
305 }
306 return licenseList;
307
308}
309
310
312
323template<class Rep, class Period>
325{
326 auto start = std::chrono::system_clock::now();
327
328 while (true)
329 {
330 auto result = CVB_CALL_CAPI(UpdateLicenses());
331 if (result >= 0 && CExports::GetLicenseCount() > 0)
332 return WaitStatus::Ok;
333 else
334 {
335 // wait until services are running
338 if (end - start > timeSpan)
339 return WaitStatus::Timeout;
340 }
341 }
342}
343
345
359{
360 auto resultUpdate = CVB_CALL_CAPI(UpdateLicensesDeep());
361 if (resultUpdate < 0)
362 ThrowLastError(resultUpdate);
363
364 auto magicNumberCount = static_cast<int>(CExports::GetMagicNumberCount());
365
367
368 for (int i = 0; i < magicNumberCount; ++i)
369 {
370 std::size_t providerSize = 0;
371 std::size_t toolIDSize = 0;
372 std::size_t magicNumberSize = 0;
373 CExports::cvblicres_t serialNumber = 0;
374 auto resultMnSize = CVB_CALL_CAPI(GetMagicNumber(static_cast<std::size_t>(i),
375 nullptr, providerSize,
376 nullptr, toolIDSize,
377 nullptr, magicNumberSize,
378 serialNumber));
379 if (resultMnSize >= 0)
380 {
381 std::vector<char> provider(providerSize);
382 std::vector<char> toolID(toolIDSize);
383 std::vector<char> magicNumber(magicNumberSize);
384 auto resultMn = CVB_CALL_CAPI(GetMagicNumber(static_cast<std::size_t>(i),
385 &provider[0], providerSize,
386 &toolID[0], toolIDSize,
387 &magicNumber[0], magicNumberSize,
388 serialNumber));
389 if(resultMn < 0)
390 continue;
391
392 entryList.push_back(MagicNumberEntry(String(provider.begin(), provider.end()),
393 String(toolID.begin(), toolID.end()),
394 String(magicNumber.begin(), magicNumber.end()),
395 static_cast<int>(serialNumber)));
396 }
397
398 }
399
400 return entryList;
401}
402
403}
404
413
414}
415
418using Utilities::ExpandPath;
419using Utilities::InstallPath;
420using Utilities::DataPath;
421using Utilities::GetLicenseInfo;
422using Utilities::WaitForLicense;
423using Utilities::GetMagicNumberEntries;
424
425CVB_END_INLINE_NS
426
427}
Information about CVB licenses.
Definition: system_info.hpp:34
int SerialNumber() const noexcept
The 32 bit CVB serial number of this license.
Definition: system_info.hpp:44
bool IsFoundation() const noexcept
Check if the license includes the Common Vision Blox Foundation package.
Definition: system_info.hpp:54
friend std::vector< LicenseInfo > GetLicenseInfo()
Get information about available licenses.
Definition: system_info.hpp:285
A single Magic Number entry.
Definition: system_info.hpp:76
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:106
String ToolID() const noexcept
The tool ID of the Magic Number entry.
Definition: system_info.hpp:96
String Provider() const noexcept
The friendly (= human-readable) name of the license provider, that reported this Magic Number entry.
Definition: system_info.hpp:86
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:117
friend std::vector< MagicNumberEntry > GetMagicNumberEntries()
Query the set of currently available Magic Numbers.
Definition: system_info.hpp:358
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:229
String DataPath()
Directory where Common Vision Blox stores its settings.
Definition: system_info.hpp:262
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:324
std::vector< MagicNumberEntry > GetMagicNumberEntries()
Query the set of currently available Magic Numbers.
Definition: system_info.hpp:358
std::string GetLastErrorMessage(int &errorCode)
Returns the last error message and its code.
Definition: system_info.hpp:145
std::exception_ptr GetLastError() noexcept
Returns a pointer that points at the exception.
Definition: system_info.hpp:186
String InstallPath()
Directory Common Vision Blox has been installed to.
Definition: system_info.hpp:242
std::vector< LicenseInfo > GetLicenseInfo()
Get information about available licenses.
Definition: system_info.hpp:285
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:351
@ Ok
Everything is fine, a new image arrived.
@ Timeout
A timeout occurred, no image buffer has been returned.
T sleep_for(T... args)