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#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
21 CVB_BEGIN_INLINE_NS
22
23 namespace Utilities
24 {
25
27 namespace SystemInfo
28 {
29
31
33 class LicenseInfo final
34 {
36
37 public:
39
43 int SerialNumber() const noexcept
44 {
45 return serialNumber_;
46 }
47
49
53 bool IsFoundation() const noexcept
54 {
55 return isFoundation_;
56 }
57
58 private:
59 LicenseInfo(int serialNumber, bool isFoundation) noexcept
60 : serialNumber_(serialNumber)
61 , isFoundation_(isFoundation)
62 {
63 }
64
65 int serialNumber_ = 0;
66 bool isFoundation_ = false;
67 };
68
70
72 class MagicNumberEntry final
73 {
75
76 public:
78
82 String Provider() const noexcept
83 {
84 return provider_;
85 }
86
88
92 String ToolID() const noexcept
93 {
94 return toolID_;
95 }
96
99
103 String MagicNumber() const noexcept
104 {
105 return magicNumber_;
106 }
107
110
114 int SerialNumber() const noexcept
115 {
116 return serialNumber_;
117 }
118
119 private:
120 MagicNumberEntry(const String &provider, const String &toolID, const String &magicNumber,
121 int serialNumber) noexcept
122 : provider_(provider)
123 , toolID_(toolID)
124 , magicNumber_(magicNumber)
125 , serialNumber_(serialNumber)
126 {
127 }
128
129 String provider_;
130 String toolID_;
131 String magicNumber_;
132 int serialNumber_ = 0;
133 };
134
136
141 inline std::string GetLastErrorMessage(int &errorCode)
142 {
143 CExports::cvbres_t code = 0;
144 std::size_t messageSize = 0;
145 auto res = CVB_CALL_CAPI(CVCGetLastErrorW(code, nullptr, messageSize));
146 if (res == 0)
147 {
148 std::wstring message;
149 message.resize(messageSize);
150 res = CVB_CALL_CAPI(CVCGetLastErrorW(code, &message[0], messageSize));
151 if (res == 0)
152 {
153 errorCode = static_cast<int>(code);
154 // currently the only standard way
155 return Internal::WideCharStrToUTF8(message);
156 }
157 }
158 errorCode = static_cast<int>(res);
159 return std::string();
160 }
161
163
168 {
169 int dummy = 0;
170 std::string message = GetLastErrorMessage(dummy);
171 return message;
172 }
173
175
182 {
183 int errorCode = 0;
184 auto message = GetLastErrorMessage(errorCode);
185 if (errorCode == 0)
186 return std::exception_ptr();
187
188 return CvbException::FromCvbResult(errorCode, message);
189 }
190
191 inline void ThrowLastError(int errorCode)
192 {
193 auto exception = GetLastError();
194 // Raise an exception for the code specified by errorCode:
195 if (!exception)
196 std::rethrow_exception(CvbException::FromCvbResult(errorCode, "no specific message"));
197 std::rethrow_exception(exception);
198 }
199
200 inline void ThrowLastError()
201 {
202 ThrowLastError(ErrorCodes::CVB_ERROR);
203 }
204
205 inline std::chrono::microseconds DefaultTimeout()
206 {
207 CExports::cvbval_t defaultTimeout = 0;
208 if (!CExports::GetDefaultTimeout(defaultTimeout))
209 return std::chrono::milliseconds(10000);
210
211 return std::chrono::milliseconds(defaultTimeout);
212 }
213
215
221 inline String ExpandPath(const String &path)
222 {
223 std::vector<Char> buffer(CExports::CVB_MAX_PATH);
224 if (!CExports::TranslateFileNameTyped(path.c_str(), &buffer[0], CExports::CVB_MAX_PATH))
225 ThrowLastError();
226 return String(&buffer[0]);
227 }
228
230
235 {
236 std::vector<Char> buffer(260);
237 if (!CExports::GetCVBDirectoryTyped(&buffer[0], buffer.size()))
238 ThrowLastError();
239
240 String path(&buffer[0]);
241
242#ifndef _WIN32
243 if (path.back() != CVB_LIT('/'))
244 path += CVB_LIT('/');
245#endif
246 return path;
247 }
248
250
255 {
256 std::vector<Char> buffer(260);
257 if (!CExports::GetCVBDataDirectoryTyped(&buffer[0], buffer.size()))
258 ThrowLastError();
259
260 return String(&buffer[0]);
261 }
262
264
277 {
278 auto resultUpdate = CVB_CALL_CAPI(UpdateLicenses());
279 if (resultUpdate < 0)
280 std::rethrow_exception(CvbException::FromCvbResult(resultUpdate, "failed to update licenses"));
281
282 auto licCount = static_cast<int>(CExports::GetLicenseCount());
283
284 std::vector<LicenseInfo> licenseList;
285
286 for (int i = 0; i < licCount; ++i)
287 {
288 CExports::cvblicres_t serialNumber = 0;
289 CExports::cvbbool_t isFoundation = false;
290
291 auto resultInfo =
292 CVB_CALL_CAPI(GetLicenseInfoEx(static_cast<CExports::cvbval_t>(i), serialNumber, isFoundation));
293 if (resultInfo < 0)
294 ThrowLastError(resultInfo);
295
296 licenseList.push_back(LicenseInfo(static_cast<int>(serialNumber), (isFoundation) ? true : false));
297 }
298 return licenseList;
299 }
300
302
313 template <class Rep, class Period>
315 {
316 auto start = std::chrono::system_clock::now();
317
318 while (true)
319 {
320 auto result = CVB_CALL_CAPI(UpdateLicenses());
321 if (result >= 0 && CExports::GetLicenseCount() > 0)
322 return WaitStatus::Ok;
323 else
324 {
325 // wait until services are running
328 if (end - start > timeSpan)
329 return WaitStatus::Timeout;
330 }
331 }
332 }
333
335
349 {
350 auto resultUpdate = CVB_CALL_CAPI(UpdateLicensesDeep());
351 if (resultUpdate < 0)
352 ThrowLastError(resultUpdate);
353
354 auto magicNumberCount = static_cast<int>(CExports::GetMagicNumberCount());
355
357
358 for (int i = 0; i < magicNumberCount; ++i)
359 {
360 std::size_t providerSize = 0;
361 std::size_t toolIDSize = 0;
362 std::size_t magicNumberSize = 0;
363 CExports::cvblicres_t serialNumber = 0;
364 auto resultMnSize = CVB_CALL_CAPI(GetMagicNumber(static_cast<std::size_t>(i), nullptr, providerSize, nullptr,
365 toolIDSize, nullptr, magicNumberSize, serialNumber));
366 if (resultMnSize >= 0)
367 {
368 std::vector<char> provider(providerSize);
369 std::vector<char> toolID(toolIDSize);
370 std::vector<char> magicNumber(magicNumberSize);
371 auto resultMn =
372 CVB_CALL_CAPI(GetMagicNumber(static_cast<std::size_t>(i), &provider[0], providerSize, &toolID[0],
373 toolIDSize, &magicNumber[0], magicNumberSize, serialNumber));
374 if (resultMn < 0)
375 continue;
376
377 entryList.push_back(
378 MagicNumberEntry(String(provider.begin(), provider.end()), String(toolID.begin(), toolID.end()),
379 String(magicNumber.begin(), magicNumber.end()), static_cast<int>(serialNumber)));
380 }
381 }
382
383 return entryList;
384 }
385
386 } // namespace SystemInfo
387
393 using SystemInfo::LicenseInfo;
394 using SystemInfo::MagicNumberEntry;
396
397 } // namespace Utilities
398
404 using Utilities::LicenseInfo;
405 using Utilities::MagicNumberEntry;
407
408 CVB_END_INLINE_NS
409
410} // namespace Cvb
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:43
bool IsFoundation() const noexcept
Check if the license includes the Common Vision Blox Foundation package.
Definition system_info.hpp:53
friend std::vector< LicenseInfo > GetLicenseInfo()
Get information about available licenses.
Definition system_info.hpp:276
A single Magic Number entry.
Definition system_info.hpp:73
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:103
String ToolID() const noexcept
The tool ID of the Magic Number entry.
Definition system_info.hpp:92
String Provider() const noexcept
The friendly (= human-readable) name of the license provider, that reported this Magic Number entry.
Definition system_info.hpp:82
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:114
friend std::vector< MagicNumberEntry > GetMagicNumberEntries()
Query the set of currently available Magic Numbers.
Definition system_info.hpp:348
cvbres_t GetLicenseInfoEx(cvbval_t Index, cvblicres_t &SerialNumber, cvbbool_t &IsFoundation)
cvbres_t GetMagicNumber(size_t Index, char *ProviderName, size_t &ProviderNameBufferSize, char *ToolID, size_t &ToolIDBufferSize, char *MagicNumber, size_t &MagicNumberBufferSize, cvblicres_t &SerialNumber)
cvbres_t UpdateLicenses()
cvbres_t UpdateLicensesDeep()
const int CVB_ERROR
Generic unspecified error.
Definition exception.hpp:23
Namespace for helper functions related to system or CVB installation information.
Definition version.hpp:19
String ExpandPath(const String &path)
Expands a path containing an environment variable.
Definition system_info.hpp:221
String DataPath()
Directory where Common Vision Blox stores its settings.
Definition system_info.hpp:254
std::string GetLastErrorMessage()
Returns the last error message.
Definition system_info.hpp:167
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:314
std::vector< MagicNumberEntry > GetMagicNumberEntries()
Query the set of currently available Magic Numbers.
Definition system_info.hpp:348
std::exception_ptr GetLastError() noexcept
Returns a pointer that points at the exception.
Definition system_info.hpp:181
String InstallPath()
Directory Common Vision Blox has been installed to.
Definition system_info.hpp:234
std::vector< LicenseInfo > GetLicenseInfo()
Get information about available licenses.
Definition system_info.hpp:276
Namespace for helpers and utilities, which are not directly related to image processing.
Definition version.hpp:16
String ExpandPath(const String &path)
Expands a path containing an environment variable.
Definition system_info.hpp:221
String DataPath()
Directory where Common Vision Blox stores its settings.
Definition system_info.hpp:254
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:314
std::vector< MagicNumberEntry > GetMagicNumberEntries()
Query the set of currently available Magic Numbers.
Definition system_info.hpp:348
String InstallPath()
Directory Common Vision Blox has been installed to.
Definition system_info.hpp:234
std::vector< LicenseInfo > GetLicenseInfo()
Get information about available licenses.
Definition system_info.hpp:276
Root namespace for the Image Manager interface.
Definition c_barcode.h:15
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
WaitStatus
Status after waiting for an image to be returned.
Definition global.hpp:375
@ Ok
Everything is fine, a new image arrived.
Definition global.hpp:377
@ Timeout
A timeout occurred, no image buffer has been returned.
Definition global.hpp:379
T rethrow_exception(T... args)
T sleep_for(T... args)