• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "device_discover.h"
17 #include "hdi_device_v2_0.h"
18 #include "hdi_returncode_utils.h"
19 #include "common/log.h"
20 #include "common/utils.h"
21 
22 namespace OHOS {
23 namespace NeuralNetworkRuntime {
DiscoverHDIDevicesV2_0(std::string & deviceName,std::string & vendorName,std::string & version)24 std::shared_ptr<Device> DiscoverHDIDevicesV2_0(std::string& deviceName, std::string& vendorName, std::string& version)
25 {
26     // only one device from HDI now.
27     OHOS::sptr<V2_0::INnrtDevice> iDevice = V2_0::INnrtDevice::Get();
28     if (iDevice == nullptr) {
29         LOGW("Get HDI device failed.");
30         return nullptr;
31     }
32 
33     auto ret = iDevice->GetDeviceName(deviceName);
34     if (ret != V2_0::NNRT_ReturnCode::NNRT_SUCCESS) {
35         if (ret < V2_0::NNRT_ReturnCode::NNRT_SUCCESS) {
36             LOGW("Get device name failed. An error occurred in HDI, errorcode is %{public}d.", ret);
37         } else {
38             OHOS::HDI::Nnrt::V2_0::NNRT_ReturnCode nnrtRet = static_cast<OHOS::HDI::Nnrt::V2_0::NNRT_ReturnCode>(ret);
39             LOGW("Get device name failed. Errorcode is %{public}s.", ConverterRetToString(nnrtRet).c_str());
40         }
41         return nullptr;
42     }
43 
44     ret = iDevice->GetVendorName(vendorName);
45     if (ret != V2_0::NNRT_ReturnCode::NNRT_SUCCESS) {
46         if (ret < V2_0::NNRT_ReturnCode::NNRT_SUCCESS) {
47             LOGW("Get vendor name failed. An error occurred in HDI, errorcode is %{public}d.", ret);
48         } else {
49             OHOS::HDI::Nnrt::V2_0::NNRT_ReturnCode nnrtRet = static_cast<OHOS::HDI::Nnrt::V2_0::NNRT_ReturnCode>(ret);
50             LOGW("Get vendor name failed. Errorcode is %{public}s.", ConverterRetToString(nnrtRet).c_str());
51         }
52         return nullptr;
53     }
54 
55     std::pair<uint32_t, uint32_t> hdiVersion;
56     ret = iDevice->GetVersion(hdiVersion.first, hdiVersion.second);
57     if (ret != V2_0::NNRT_ReturnCode::NNRT_SUCCESS) {
58         if (ret < V2_0::NNRT_ReturnCode::NNRT_SUCCESS) {
59             LOGW("Get version failed. An error occurred in HDI, errorcode is %{public}d.", ret);
60         } else {
61             OHOS::HDI::Nnrt::V2_0::NNRT_ReturnCode nnrtRet = static_cast<OHOS::HDI::Nnrt::V2_0::NNRT_ReturnCode>(ret);
62             LOGW("Get version failed. Errorcode is %{public}s.", ConverterRetToString(nnrtRet).c_str());
63         }
64         return nullptr;
65     }
66     version = 'v' + std::to_string(hdiVersion.first) + '_' + std::to_string(hdiVersion.second);
67 
68     std::shared_ptr<Device> device = CreateSharedPtr<HDIDeviceV2_0>(iDevice);
69     if (device == nullptr) {
70         LOGW("Failed to register device, because fail to create device instance.");
71     }
72     return device;
73 }
74 } // namespace NeuralNetworkRuntime
75 } // namespace OHOS
76