• 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_manager.h"
17 #include "device_discover.h"
18 
19 #include "common/log.h"
20 #include "common/utils.h"
21 
22 namespace OHOS {
23 namespace NeuralNetworkRuntime {
GetAllDeviceId()24 const std::vector<size_t>& DeviceManager::GetAllDeviceId()
25 {
26     m_tmpDeviceIds.clear();
27     std::shared_ptr<Device> device {nullptr};
28     for (auto iter = m_devices.begin(); iter != m_devices.end(); ++iter) {
29         device = iter->second;
30         if (!IsValidDevice(device)) {
31             continue;
32         }
33         m_tmpDeviceIds.emplace_back(iter->first);
34     }
35     return m_tmpDeviceIds;
36 }
37 
GetDevice(size_t deviceId) const38 std::shared_ptr<Device> DeviceManager::GetDevice(size_t deviceId) const
39 {
40     auto iter = m_devices.find(deviceId);
41     if (iter == m_devices.end()) {
42         LOGE("DeviceId is not found, deviceId=%zu", deviceId);
43         return nullptr;
44     }
45 
46     return iter->second;
47 }
48 
GetDeviceName(size_t deviceId)49 const std::string& DeviceManager::GetDeviceName(size_t deviceId)
50 {
51     m_tmpDeviceName.clear();
52     auto iter = m_devices.find(deviceId);
53     if (iter == m_devices.end()) {
54         LOGE("DeviceId is not found, deviceId=%zu", deviceId);
55         return m_tmpDeviceName;
56     }
57 
58     std::string deviceName;
59     auto ret = iter->second->GetDeviceName(deviceName);
60     if (ret != OH_NN_SUCCESS) {
61         LOGE("Get device name failed.");
62         return m_tmpDeviceName;
63     }
64 
65     std::string vendorName;
66     ret = iter->second->GetVendorName(vendorName);
67     if (ret != OH_NN_SUCCESS) {
68         LOGE("Get vendor name failed.");
69         return m_tmpDeviceName;
70     }
71 
72     std::string version;
73     ret = iter->second->GetVersion(version);
74     if (ret != OH_NN_SUCCESS) {
75         LOGE("Get version failed.");
76         return m_tmpDeviceName;
77     }
78 
79     m_tmpDeviceName = GenUniqueName(deviceName, vendorName, version);
80     return m_tmpDeviceName;
81 }
82 
GenUniqueName(const std::string & deviceName,const std::string & vendorName,const std::string & version) const83 std::string DeviceManager::GenUniqueName(
84     const std::string& deviceName, const std::string& vendorName, const std::string& version) const
85 {
86     return deviceName + "_" + vendorName + "_" + version;
87 }
88 
RegisterDevice(std::function<std::shared_ptr<Device> ()> creator)89 OH_NN_ReturnCode DeviceManager::RegisterDevice(std::function<std::shared_ptr<Device>()> creator)
90 {
91     auto regDevice = creator();
92     if (regDevice == nullptr) {
93         LOGE("Cannot create device, register device failed.");
94         return OH_NN_INVALID_PARAMETER;
95     }
96 
97     if (!IsValidDevice(regDevice)) {
98         LOGE("Device is not avaliable.");
99         return OH_NN_UNAVALIDABLE_DEVICE;
100     }
101 
102     std::string deviceName;
103     auto ret = regDevice->GetDeviceName(deviceName);
104     if (ret != OH_NN_SUCCESS) {
105         LOGE("Get device name failed.");
106         return ret;
107     }
108 
109     std::string vendorName;
110     ret = regDevice->GetVendorName(vendorName);
111     if (ret != OH_NN_SUCCESS) {
112         LOGE("Get vendor name failed.");
113         return ret;
114     }
115 
116     std::string version;
117     ret = regDevice->GetVersion(version);
118     if (ret != OH_NN_SUCCESS) {
119         LOGE("Get version failed.");
120         return ret;
121     }
122 
123     const std::lock_guard<std::mutex> lock(m_mtx);
124     std::string uniqueName = GenUniqueName(deviceName, vendorName, version);
125     auto setResult = m_uniqueName.emplace(uniqueName);
126     if (!setResult.second) {
127         LOGE("Device already exists, cannot register again. deviceName=%s, vendorName=%s",
128             deviceName.c_str(), vendorName.c_str());
129         return OH_NN_FAILED;
130     }
131 
132     m_devices.emplace(std::hash<std::string>{}(uniqueName), regDevice);
133     return OH_NN_SUCCESS;
134 }
135 
AddDevice(const std::string & deviceName,const std::string & vendorName,const std::string & version,std::shared_ptr<Device> device)136 void DeviceManager::AddDevice(const std::string& deviceName, const std::string& vendorName,
137     const std::string& version, std::shared_ptr<Device> device)
138 {
139     std::string uniqueName = GenUniqueName(deviceName, vendorName, version);
140     const std::lock_guard<std::mutex> lock(m_mtx);
141     auto setResult = m_uniqueName.emplace(uniqueName);
142     if (!setResult.second) {
143         LOGW("Device already exists, cannot register again. deviceName=%s, vendorName=%s",
144             deviceName.c_str(), vendorName.c_str());
145         return;
146     }
147 
148     m_devices.emplace(std::hash<std::string>{}(uniqueName), device);
149 }
150 
DiscoverHDIDevices()151 void DeviceManager::DiscoverHDIDevices()
152 {
153     std::string deviceName;
154     std::string vendorName;
155     std::string version;
156     std::shared_ptr<Device> deviceV1_0 = DiscoverHDIDevicesV1_0(deviceName, vendorName, version);
157     if (deviceV1_0 != nullptr) {
158         AddDevice(deviceName, vendorName, version, deviceV1_0);
159     }
160 
161     std::shared_ptr<Device> deviceV2_0 = DiscoverHDIDevicesV2_0(deviceName, vendorName, version);
162     if (deviceV2_0 != nullptr) {
163         AddDevice(deviceName, vendorName, version, deviceV2_0);
164     }
165 }
166 
IsValidDevice(std::shared_ptr<Device> device) const167 bool DeviceManager::IsValidDevice(std::shared_ptr<Device> device) const
168 {
169     DeviceStatus status {DeviceStatus::UNKNOWN};
170     auto ret = device->GetDeviceStatus(status);
171     if (ret != OH_NN_SUCCESS || status == DeviceStatus::UNKNOWN || status == DeviceStatus::OFFLINE) {
172         return false;
173     }
174     return true;
175 }
176 } // NeuralNetworkRuntime
177 } // OHOS