Publishing 2019 R3 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / movidius / mvnc / tests / mvnc_tests_common.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #if (defined(_WIN32) || defined(_WIN64))
8 #include "windows.h"
9 #endif
10
11 #include <thread>
12 #include <gtest/gtest.h>
13 #include <fstream>
14
15 #include "XLink.h"
16 #include "mvnc.h"
17 #include "mvnc_ext.h"
18 #include "mvnc_data.h"
19 #include "mvLog.h"
20 #include "usb_boot.h"
21 #include "ncPrivateTypes.h"
22 #include "ncCommPrivate.h"
23
24
25 ///// Macroses
26 #define ASSERT_NO_ERROR(call)   ASSERT_EQ(call, 0)
27 #define ASSERT_ERROR(call)      ASSERT_TRUE(call)
28
29
30 //// Defines
31 #define MYRIAD_X_NAME_STR "ma2480"
32 #define MYRIAD_2_NAME_STR "ma2450"
33
34 #if (defined(_WIN32) || defined(_WIN64))
35 #define PCIE_NAME_STR     "mxlink"
36 #else
37 #define PCIE_NAME_STR     "mxlk"
38 #endif
39
40 const int MAX_DEVICES = 32;
41 const int MAX_DEV_NAME = 20;
42
43 #ifndef MAX_PATH
44 const int MAX_PATH = 255;
45 #endif
46
47 //// Usb initialization
48 // Without this initialization find device on windows could not work
49 #if (defined(_WIN32) || defined(_WIN64) )
50 extern "C" void initialize_usb_boot();
51 #else
52 #define initialize_usb_boot()
53 #endif
54
55 class MvncTestsCommon : public ::testing::Test {
56 public:
57     char        firmwarePath[MAX_PATH];
58     mvLog_t     ncLogLevel;
59     int         watchdogInterval;
60
61     ~MvncTestsCommon() override = default;
62
63     MvncTestsCommon() : ncLogLevel(MVLOG_INFO), watchdogInterval(1000) {
64 #if !(defined(_WIN32) || defined(_WIN64))
65         // On linux we should use custom path to firmware due to another searching mechanism for library
66         strcpy(firmwarePath, "./lib");
67 #else
68         firmwarePath[0] = 0;
69 #endif
70     }
71 protected:
72
73     void SetUp() override {
74         initialize_usb_boot();
75         ASSERT_NO_ERROR(setLogLevel(ncLogLevel));
76     }
77
78     void TearDown() override {
79         ncDeviceResetAll();
80     }
81
82 public:
83     int setLogLevel(const mvLog_t logLevel) {
84         ncStatus_t status = ncGlobalSetOption(NC_RW_LOG_LEVEL, &logLevel,
85                                               sizeof(logLevel));
86         if (status != NC_OK) {
87             fprintf(stderr,
88                     "WARNING: failed to set log level: %d with error: %d\n",
89                     ncLogLevel, status);
90             return -1;
91         }
92         ncLogLevel = logLevel;
93         return 0;
94     }
95
96     /**
97      * @brief Get amount of all currently connected Myriad devices
98      * @param[in] deviceProtocol Count only platform specific devices
99      */
100     static int getAmountOfDevices(const ncDeviceProtocol_t deviceProtocol = NC_ANY_PROTOCOL,
101                                   const ncDevicePlatform_t devicePlatform = NC_ANY_PLATFORM,
102                                   const XLinkDeviceState_t state = X_LINK_ANY_STATE) {
103         deviceDesc_t req_deviceDesc = {};
104         req_deviceDesc.protocol = convertProtocolToXlink(deviceProtocol);
105         req_deviceDesc.platform = convertPlatformToXlink(devicePlatform);
106
107         deviceDesc_t deviceDescArray[NC_MAX_DEVICES] = {};
108         unsigned int foundDevices = 0;
109         XLinkFindAllSuitableDevices(
110                 state, req_deviceDesc, deviceDescArray, NC_MAX_DEVICES, &foundDevices);
111
112         return foundDevices;
113     }
114
115     /**
116      * @brief Boot and open selected amount of device
117      * @param[out] amountOfBooted Amount of device which was booted
118      * @param[out] deviceHandlers Pre-allocated array for handlers
119      */
120     void openDevices(const int devicesToBoot, ncDeviceHandle_t** deviceHandlers,
121             int& amountOfBooted,
122             const ncDeviceProtocol_t protocol = NC_ANY_PROTOCOL)
123     {
124         ASSERT_TRUE(deviceHandlers != nullptr);
125         const int availableDevices = getAmountOfDevices(NC_USB);
126         if (availableDevices < devicesToBoot) {
127             GTEST_SKIP_("Not enough devices");
128         }
129
130         amountOfBooted = 0;
131         ncDeviceDescr_t ncDeviceDesc = {};
132         ncDeviceDesc.protocol = NC_USB;
133         ncDeviceDesc.platform = NC_ANY_PLATFORM;
134
135         for (int index = 0; index < devicesToBoot; ++index) {
136             ASSERT_NO_ERROR(ncDeviceOpen(&deviceHandlers[index], ncDeviceDesc, watchdogInterval, firmwarePath));
137             ASSERT_TRUE(deviceHandlers[index] != nullptr);
138             ++amountOfBooted;
139         }
140         ASSERT_EQ(amountOfBooted, devicesToBoot) << "Not all devices was loaded";
141     }
142
143     /**
144      * @brief Load firmware to device
145      * @warning Only USB devices is supported
146      */
147     virtual void bootOneDevice(ncDeviceProtocol_t deviceProtocol= NC_USB) {
148         if (deviceProtocol == NC_PCIE) {
149             GTEST_FATAL_FAILURE_("Boot doesn't supported for PCIe protocol\n");
150         }
151         ASSERT_NO_ERROR(ncDeviceLoadFirmware(NC_ANY_PLATFORM, firmwarePath));
152     }
153
154     /**
155      * @brief Get list of all currently connected Myriad devices
156      */
157     static std::vector<std::string> getDevicesList(
158             const ncDeviceProtocol_t deviceProtocol = NC_ANY_PROTOCOL,
159             const ncDevicePlatform_t devicePlatform = NC_ANY_PLATFORM,
160             const XLinkDeviceState_t state = X_LINK_ANY_STATE) {
161
162         deviceDesc_t req_deviceDesc = {};
163         req_deviceDesc.protocol = convertProtocolToXlink(deviceProtocol);
164         req_deviceDesc.platform = convertPlatformToXlink(devicePlatform);
165
166         deviceDesc_t deviceDescArray[NC_MAX_DEVICES] = {};
167         unsigned int foundDevices = 0;
168         XLinkFindAllSuitableDevices(
169                 state, req_deviceDesc, deviceDescArray, NC_MAX_DEVICES, &foundDevices);
170
171         std::vector < std::string > devNames;
172         for (int i = 0; i < foundDevices; ++i) {
173             devNames.emplace_back(deviceDescArray[i].name);
174         }
175
176         return devNames;
177     }
178
179     static bool isMyriadXUSBDevice(const std::string &deviceName) {
180         return (deviceName.find(MYRIAD_X_NAME_STR) != std::string::npos);
181     }
182
183     static bool isMyriad2USBDevice(const std::string &deviceName) {
184         return (deviceName.find(MYRIAD_2_NAME_STR) != std::string::npos);
185     }
186
187     static bool isMyriadPCIeDevice(const std::string& deviceName) {
188         return deviceName.find(std::string(PCIE_NAME_STR)) != std::string::npos;
189     }
190
191     /**
192      * @warning The booted USB device will also be counted here.
193      */
194     static bool isMyriadUSBDevice(const std::string& deviceName) {
195         return (isMyriad2USBDevice(deviceName)
196                     || isMyriadXUSBDevice(deviceName)
197                     || isMyriadBootedUSBDevice(deviceName));
198     }
199
200     static bool isMyriadBootedUSBDevice(const std::string &deviceName) {
201         return (!isMyriad2USBDevice(deviceName) &&
202                     !isMyriadXUSBDevice(deviceName) &&
203                     !isMyriadPCIeDevice(deviceName));
204     }
205
206     /**
207      * @brief Check that device matches the specified protocol
208      */
209     static bool isSameProtocolDevice(const std::string &deviceName,
210             const ncDeviceProtocol_t expectedProtocol) {
211         switch (expectedProtocol) {
212             case NC_USB:            return isMyriadUSBDevice(deviceName);
213             case NC_PCIE:           return isMyriadPCIeDevice(deviceName);
214             case NC_ANY_PROTOCOL:
215                 return isMyriadPCIeDevice(deviceName) || isMyriadUSBDevice(deviceName);
216             default:
217                 std::cout << "Unknown device protocol" << std::endl;
218                 return false;
219         }
220     }
221
222     /**
223     * @brief Check that device matches the specified protocol
224     */
225     static bool isSamePlatformUSBDevice(const std::string &deviceName,
226                                         const ncDevicePlatform_t expectedPlatform) {
227         switch (expectedPlatform) {
228             case NC_MYRIAD_2:  return isMyriad2USBDevice(deviceName);
229             case NC_MYRIAD_X:  return isMyriadXUSBDevice(deviceName);
230             case NC_ANY_PLATFORM:
231                 return isMyriad2USBDevice(deviceName) || isMyriadXUSBDevice(deviceName);
232             default:
233                 std::cout << "Unknown device platform" << std::endl;
234                 return false;
235         }
236     }
237
238     static long getAmountOfMyriadXDevices() {
239         return getAmountOfDevices(NC_ANY_PROTOCOL, NC_MYRIAD_X);
240     }
241
242     static long getAmountOfMyriad2Devices() {
243         return getAmountOfDevices(NC_ANY_PROTOCOL, NC_MYRIAD_2);
244     }
245
246     static long getAmountOfBootedDevices(ncDeviceProtocol_t deviceProtocol = NC_ANY_PROTOCOL) {
247         return getAmountOfDevices(deviceProtocol, NC_ANY_PLATFORM, X_LINK_BOOTED);
248     }
249
250     static long getAmountOfNotBootedDevices(ncDeviceProtocol_t deviceProtocol = NC_ANY_PROTOCOL) {
251         return getAmountOfDevices(deviceProtocol, NC_ANY_PLATFORM, X_LINK_UNBOOTED);
252     }
253
254     static long getAmountOfPCIeDevices() {
255         return getAmountOfDevices(NC_PCIE);
256     }
257
258     static long getAmountOfUSBDevices() {
259         return getAmountOfDevices(NC_USB);
260     }
261
262
263     /**
264      * @brief   Read blob
265      * @param   fileName Path to blob from bin directory
266      * @return  True if blob is readed without problem
267      */
268     bool readBINFile(const std::string& fileName, std::vector<char>& buf) {
269         std::ifstream file(fileName, std::ios_base::binary | std::ios_base::ate);
270         if (file.fail()) {
271             std::cout << "Can't open file!" << std::endl;
272             return false;
273         }
274         buf.resize(static_cast<unsigned int>(file.tellg()));
275         file.seekg(0);
276         file.read(buf.data(), buf.size());
277         return true;
278     }
279 };
280
281 /// Parametric tests initialization
282
283 static const std::vector<ncDeviceProtocol_t> myriadProtocols = {
284         NC_USB,
285         NC_PCIE
286 };
287
288 static const std::vector<ncDevicePlatform_t> myriadPlatforms = {
289         NC_MYRIAD_2,
290         NC_MYRIAD_X
291 };
292
293
294 namespace {
295     /**
296      * @brief   Converter from enum to string
297      */
298     struct PrintToStringParamName {
299         std::string operator()(
300                 const ::testing::TestParamInfo<ncDeviceProtocol_t> &info) const {
301             return ncProtocolToStr(info.param);
302         }
303
304         std::string operator()(
305                 const ::testing::TestParamInfo<ncDevicePlatform_t> &info) const {
306             return std::string("USB_") + ncPlatformToStr(info.param);
307         }
308     };
309 }