1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
7 #if (defined(_WIN32) || defined(_WIN64))
12 #include <gtest/gtest.h>
18 #include "mvnc_data.h"
21 #include "ncPrivateTypes.h"
22 #include "ncCommPrivate.h"
26 #define ASSERT_NO_ERROR(call) ASSERT_EQ(call, 0)
27 #define ASSERT_ERROR(call) ASSERT_TRUE(call)
31 #define MYRIAD_X_NAME_STR "ma2480"
32 #define MYRIAD_2_NAME_STR "ma2450"
34 #if (defined(_WIN32) || defined(_WIN64))
35 #define PCIE_NAME_STR "mxlink"
37 #define PCIE_NAME_STR "mxlk"
40 const int MAX_DEVICES = 32;
41 const int MAX_DEV_NAME = 20;
44 const int MAX_PATH = 255;
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();
52 #define initialize_usb_boot()
55 class MvncTestsCommon : public ::testing::Test {
57 char firmwarePath[MAX_PATH];
61 ~MvncTestsCommon() override = default;
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");
73 void SetUp() override {
74 initialize_usb_boot();
75 ASSERT_NO_ERROR(setLogLevel(ncLogLevel));
78 void TearDown() override {
83 int setLogLevel(const mvLog_t logLevel) {
84 ncStatus_t status = ncGlobalSetOption(NC_RW_LOG_LEVEL, &logLevel,
86 if (status != NC_OK) {
88 "WARNING: failed to set log level: %d with error: %d\n",
92 ncLogLevel = logLevel;
97 * @brief Get amount of all currently connected Myriad devices
98 * @param[in] deviceProtocol Count only platform specific devices
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);
107 deviceDesc_t deviceDescArray[NC_MAX_DEVICES] = {};
108 unsigned int foundDevices = 0;
109 XLinkFindAllSuitableDevices(
110 state, req_deviceDesc, deviceDescArray, NC_MAX_DEVICES, &foundDevices);
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
120 void openDevices(const int devicesToBoot, ncDeviceHandle_t** deviceHandlers,
122 const ncDeviceProtocol_t protocol = NC_ANY_PROTOCOL)
124 ASSERT_TRUE(deviceHandlers != nullptr);
125 const int availableDevices = getAmountOfDevices(NC_USB);
126 if (availableDevices < devicesToBoot) {
127 GTEST_SKIP_("Not enough devices");
131 ncDeviceDescr_t ncDeviceDesc = {};
132 ncDeviceDesc.protocol = NC_USB;
133 ncDeviceDesc.platform = NC_ANY_PLATFORM;
135 for (int index = 0; index < devicesToBoot; ++index) {
136 ASSERT_NO_ERROR(ncDeviceOpen(&deviceHandlers[index], ncDeviceDesc, watchdogInterval, firmwarePath));
137 ASSERT_TRUE(deviceHandlers[index] != nullptr);
140 ASSERT_EQ(amountOfBooted, devicesToBoot) << "Not all devices was loaded";
144 * @brief Load firmware to device
145 * @warning Only USB devices is supported
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");
151 ASSERT_NO_ERROR(ncDeviceLoadFirmware(NC_ANY_PLATFORM, firmwarePath));
155 * @brief Get list of all currently connected Myriad devices
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) {
162 deviceDesc_t req_deviceDesc = {};
163 req_deviceDesc.protocol = convertProtocolToXlink(deviceProtocol);
164 req_deviceDesc.platform = convertPlatformToXlink(devicePlatform);
166 deviceDesc_t deviceDescArray[NC_MAX_DEVICES] = {};
167 unsigned int foundDevices = 0;
168 XLinkFindAllSuitableDevices(
169 state, req_deviceDesc, deviceDescArray, NC_MAX_DEVICES, &foundDevices);
171 std::vector < std::string > devNames;
172 for (int i = 0; i < foundDevices; ++i) {
173 devNames.emplace_back(deviceDescArray[i].name);
179 static bool isMyriadXUSBDevice(const std::string &deviceName) {
180 return (deviceName.find(MYRIAD_X_NAME_STR) != std::string::npos);
183 static bool isMyriad2USBDevice(const std::string &deviceName) {
184 return (deviceName.find(MYRIAD_2_NAME_STR) != std::string::npos);
187 static bool isMyriadPCIeDevice(const std::string& deviceName) {
188 return deviceName.find(std::string(PCIE_NAME_STR)) != std::string::npos;
192 * @warning The booted USB device will also be counted here.
194 static bool isMyriadUSBDevice(const std::string& deviceName) {
195 return (isMyriad2USBDevice(deviceName)
196 || isMyriadXUSBDevice(deviceName)
197 || isMyriadBootedUSBDevice(deviceName));
200 static bool isMyriadBootedUSBDevice(const std::string &deviceName) {
201 return (!isMyriad2USBDevice(deviceName) &&
202 !isMyriadXUSBDevice(deviceName) &&
203 !isMyriadPCIeDevice(deviceName));
207 * @brief Check that device matches the specified protocol
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);
217 std::cout << "Unknown device protocol" << std::endl;
223 * @brief Check that device matches the specified protocol
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);
233 std::cout << "Unknown device platform" << std::endl;
238 static long getAmountOfMyriadXDevices() {
239 return getAmountOfDevices(NC_ANY_PROTOCOL, NC_MYRIAD_X);
242 static long getAmountOfMyriad2Devices() {
243 return getAmountOfDevices(NC_ANY_PROTOCOL, NC_MYRIAD_2);
246 static long getAmountOfBootedDevices(ncDeviceProtocol_t deviceProtocol = NC_ANY_PROTOCOL) {
247 return getAmountOfDevices(deviceProtocol, NC_ANY_PLATFORM, X_LINK_BOOTED);
250 static long getAmountOfNotBootedDevices(ncDeviceProtocol_t deviceProtocol = NC_ANY_PROTOCOL) {
251 return getAmountOfDevices(deviceProtocol, NC_ANY_PLATFORM, X_LINK_UNBOOTED);
254 static long getAmountOfPCIeDevices() {
255 return getAmountOfDevices(NC_PCIE);
258 static long getAmountOfUSBDevices() {
259 return getAmountOfDevices(NC_USB);
265 * @param fileName Path to blob from bin directory
266 * @return True if blob is readed without problem
268 bool readBINFile(const std::string& fileName, std::vector<char>& buf) {
269 std::ifstream file(fileName, std::ios_base::binary | std::ios_base::ate);
271 std::cout << "Can't open file!" << std::endl;
274 buf.resize(static_cast<unsigned int>(file.tellg()));
276 file.read(buf.data(), buf.size());
281 /// Parametric tests initialization
283 static const std::vector<ncDeviceProtocol_t> myriadProtocols = {
288 static const std::vector<ncDevicePlatform_t> myriadPlatforms = {
296 * @brief Converter from enum to string
298 struct PrintToStringParamName {
299 std::string operator()(
300 const ::testing::TestParamInfo<ncDeviceProtocol_t> &info) const {
301 return ncProtocolToStr(info.param);
304 std::string operator()(
305 const ::testing::TestParamInfo<ncDevicePlatform_t> &info) const {
306 return std::string("USB_") + ncPlatformToStr(info.param);