1 /*
2 * Copyright (c) 2022-2024 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_service_listener.h"
17
18 #include "device_manager_ipc_interface_code.h"
19 #include "dm_anonymous.h"
20 #include "dm_constants.h"
21 #include "dm_log.h"
22 #include "dm_crypto.h"
23 #include "ipc_create_pin_holder_req.h"
24 #include "ipc_destroy_pin_holder_req.h"
25 #include "ipc_notify_auth_result_req.h"
26 #include "ipc_notify_bind_result_req.h"
27 #include "ipc_notify_credential_req.h"
28 #include "ipc_notify_device_found_req.h"
29 #include "ipc_notify_device_discovery_req.h"
30 #include "ipc_notify_device_state_req.h"
31 #include "ipc_notify_discover_result_req.h"
32 #include "ipc_notify_publish_result_req.h"
33
34 namespace OHOS {
35 namespace DistributedHardware {
36 std::mutex DeviceManagerServiceListener::dmListenerMapLock_;
37 std::mutex DeviceManagerServiceListener::udidHashMapLock_;
38 std::map<std::string, std::string> DeviceManagerServiceListener::dmListenerMap_ = {};
39 std::map<std::string, std::map<std::string, std::string>> DeviceManagerServiceListener::udidHashMap_ = {};
40 std::mutex DeviceManagerServiceListener::alreadyOnlineSetLock_;
41 std::unordered_set<std::string> DeviceManagerServiceListener::alreadyOnlineSet_ = {};
42
ConvertDeviceInfoToDeviceBasicInfo(const std::string & pkgName,const DmDeviceInfo & info,DmDeviceBasicInfo & deviceBasicInfo)43 void DeviceManagerServiceListener::ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName,
44 const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo)
45 {
46 (void)memset_s(&deviceBasicInfo, sizeof(DmDeviceBasicInfo), 0, sizeof(DmDeviceBasicInfo));
47 if (memcpy_s(deviceBasicInfo.deviceName, sizeof(deviceBasicInfo.deviceName), info.deviceName,
48 std::min(sizeof(deviceBasicInfo.deviceName), sizeof(info.deviceName))) != DM_OK) {
49 LOGE("ConvertDeviceInfoToDmDevice copy deviceName data failed.");
50 }
51
52 if (memcpy_s(deviceBasicInfo.networkId, sizeof(deviceBasicInfo.networkId), info.networkId,
53 std::min(sizeof(deviceBasicInfo.networkId), sizeof(info.networkId))) != DM_OK) {
54 LOGE("ConvertNodeBasicInfoToDmDevice copy networkId data failed.");
55 }
56 if (memcpy_s(deviceBasicInfo.deviceId, sizeof(deviceBasicInfo.deviceId), info.deviceId,
57 std::min(sizeof(deviceBasicInfo.deviceId), sizeof(info.deviceId))) != DM_OK) {
58 LOGE("ConvertNodeBasicInfoToDmDevice copy deviceId data failed.");
59 }
60 deviceBasicInfo.deviceTypeId = info.deviceTypeId;
61 }
62
OnDeviceStateChange(const std::string & pkgName,const DmDeviceState & state,const DmDeviceInfo & info)63 void DeviceManagerServiceListener::OnDeviceStateChange(const std::string &pkgName, const DmDeviceState &state,
64 const DmDeviceInfo &info)
65 {
66 LOGI("OnDeviceStateChange, state = %d", state);
67 std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
68 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
69 DmDeviceBasicInfo deviceBasicInfo;
70 ConvertDeviceInfoToDeviceBasicInfo(pkgName, info, deviceBasicInfo);
71 if (pkgName == std::string(DM_PKG_NAME)) {
72 std::vector<std::string> PkgNameVec = ipcServerListener_.GetAllPkgName();
73 if (state == DEVICE_STATE_OFFLINE) {
74 {
75 std::lock_guard<std::mutex> autoLock(alreadyOnlineSetLock_);
76 alreadyOnlineSet_.clear();
77 }
78 }
79 for (const auto &it : PkgNameVec) {
80 std::string notifyKey = it + "_" + info.deviceId;
81 {
82 std::lock_guard<std::mutex> autoLock(alreadyOnlineSetLock_);
83 if (state == DEVICE_STATE_ONLINE && alreadyOnlineSet_.find(notifyKey) != alreadyOnlineSet_.end()) {
84 continue;
85 } else if (state == DEVICE_STATE_ONLINE) {
86 alreadyOnlineSet_.insert(notifyKey);
87 }
88 }
89 pReq->SetPkgName(it);
90 pReq->SetDeviceState(state);
91 pReq->SetDeviceInfo(info);
92 pReq->SetDeviceBasicInfo(deviceBasicInfo);
93 ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
94 }
95 } else {
96 std::string notifyKey = pkgName + "_" + info.deviceId;
97 {
98 std::lock_guard<std::mutex> autoLock(alreadyOnlineSetLock_);
99 if (state == DEVICE_STATE_ONLINE) {
100 alreadyOnlineSet_.insert(notifyKey);
101 } else if (state == DEVICE_STATE_OFFLINE) {
102 alreadyOnlineSet_.erase(notifyKey);
103 }
104 }
105 pReq->SetPkgName(pkgName);
106 pReq->SetDeviceState(state);
107 pReq->SetDeviceInfo(info);
108 pReq->SetDeviceBasicInfo(deviceBasicInfo);
109 ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
110 }
111 }
112
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,const DmDeviceInfo & info)113 void DeviceManagerServiceListener::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
114 const DmDeviceInfo &info)
115 {
116 std::shared_ptr<IpcNotifyDeviceFoundReq> pReq = std::make_shared<IpcNotifyDeviceFoundReq>();
117 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
118
119 DmDeviceBasicInfo devBasicInfo;
120 ConvertDeviceInfoToDeviceBasicInfo(pkgName, info, devBasicInfo);
121 pReq->SetDeviceBasicInfo(devBasicInfo);
122 pReq->SetPkgName(pkgName);
123 pReq->SetSubscribeId(subscribeId);
124 pReq->SetDeviceInfo(info);
125 ipcServerListener_.SendRequest(SERVER_DEVICE_FOUND, pReq, pRsp);
126 }
127
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,DmDeviceBasicInfo & info)128 void DeviceManagerServiceListener::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
129 DmDeviceBasicInfo &info)
130 {
131 std::shared_ptr<IpcNotifyDeviceDiscoveryReq> pReq = std::make_shared<IpcNotifyDeviceDiscoveryReq>();
132 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
133 std::string udIdHash = CalcDeviceId(pkgName, info.deviceId);
134 if (memcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, udIdHash.c_str(), udIdHash.length()) != DM_OK) {
135 LOGE("ConvertDeviceInfoToDmDevice copy deviceId data failed.");
136 }
137 pReq->SetPkgName(pkgName);
138 pReq->SetSubscribeId(subscribeId);
139 pReq->SetDeviceBasicInfo(info);
140 ipcServerListener_.SendRequest(SERVER_DEVICE_DISCOVERY, pReq, pRsp);
141 }
142
OnDiscoveryFailed(const std::string & pkgName,uint16_t subscribeId,int32_t failedReason)143 void DeviceManagerServiceListener::OnDiscoveryFailed(const std::string &pkgName, uint16_t subscribeId,
144 int32_t failedReason)
145 {
146 LOGI("DeviceManagerServiceListener::OnDiscoveryFailed");
147 std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::make_shared<IpcNotifyDiscoverResultReq>();
148 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
149
150 pReq->SetPkgName(pkgName);
151 pReq->SetSubscribeId(subscribeId);
152 pReq->SetResult(failedReason);
153 ipcServerListener_.SendRequest(SERVER_DISCOVER_FINISH, pReq, pRsp);
154 }
155
OnDiscoverySuccess(const std::string & pkgName,int32_t subscribeId)156 void DeviceManagerServiceListener::OnDiscoverySuccess(const std::string &pkgName, int32_t subscribeId)
157 {
158 LOGI("DeviceManagerServiceListener::OnDiscoverySuccess");
159 std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::make_shared<IpcNotifyDiscoverResultReq>();
160 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
161
162 pReq->SetPkgName(pkgName);
163 pReq->SetSubscribeId((uint16_t)subscribeId);
164 pReq->SetResult(DM_OK);
165 ipcServerListener_.SendRequest(SERVER_DISCOVER_FINISH, pReq, pRsp);
166 }
167
OnPublishResult(const std::string & pkgName,int32_t publishId,int32_t publishResult)168 void DeviceManagerServiceListener::OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult)
169 {
170 LOGI("DeviceManagerServiceListener::OnPublishResult : %d", publishResult);
171 std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::make_shared<IpcNotifyPublishResultReq>();
172 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
173
174 pReq->SetPkgName(pkgName);
175 pReq->SetPublishId(publishId);
176 pReq->SetResult(publishResult);
177 ipcServerListener_.SendRequest(SERVER_PUBLISH_FINISH, pReq, pRsp);
178 }
179
OnAuthResult(const std::string & pkgName,const std::string & deviceId,const std::string & token,int32_t status,int32_t reason)180 void DeviceManagerServiceListener::OnAuthResult(const std::string &pkgName, const std::string &deviceId,
181 const std::string &token, int32_t status, int32_t reason)
182 {
183 std::shared_ptr<IpcNotifyAuthResultReq> pReq = std::make_shared<IpcNotifyAuthResultReq>();
184 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
185 if (status < STATUS_DM_AUTH_FINISH && status > STATUS_DM_AUTH_DEFAULT) {
186 status = STATUS_DM_AUTH_DEFAULT;
187 }
188
189 pReq->SetPkgName(pkgName);
190 pReq->SetDeviceId(deviceId);
191 pReq->SetToken(token);
192 pReq->SetStatus(status);
193 pReq->SetReason(reason);
194 ipcServerListener_.SendRequest(SERVER_AUTH_RESULT, pReq, pRsp);
195 }
196
OnUiCall(std::string & pkgName,std::string & paramJson)197 void DeviceManagerServiceListener::OnUiCall(std::string &pkgName, std::string ¶mJson)
198 {
199 LOGI("OnUiCall in");
200 std::shared_ptr<IpcNotifyDMFAResultReq> pReq = std::make_shared<IpcNotifyDMFAResultReq>();
201 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
202
203 pReq->SetPkgName(pkgName);
204 pReq->SetJsonParam(paramJson);
205 ipcServerListener_.SendRequest(SERVER_DEVICE_FA_NOTIFY, pReq, pRsp);
206 }
207
OnCredentialResult(const std::string & pkgName,int32_t action,const std::string & resultInfo)208 void DeviceManagerServiceListener::OnCredentialResult(const std::string &pkgName, int32_t action,
209 const std::string &resultInfo)
210 {
211 LOGI("call OnCredentialResult for %s, action %d", pkgName.c_str(), action);
212 std::shared_ptr<IpcNotifyCredentialReq> pReq = std::make_shared<IpcNotifyCredentialReq>();
213 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
214
215 pReq->SetPkgName(pkgName);
216 pReq->SetCredentialAction(action);
217 pReq->SetCredentialResult(resultInfo);
218 ipcServerListener_.SendRequest(SERVER_CREDENTIAL_RESULT, pReq, pRsp);
219 }
220
OnBindResult(const std::string & pkgName,const PeerTargetId & targetId,int32_t result,int32_t status,std::string content)221 void DeviceManagerServiceListener::OnBindResult(const std::string &pkgName, const PeerTargetId &targetId,
222 int32_t result, int32_t status, std::string content)
223 {
224 std::shared_ptr<IpcNotifyBindResultReq> pReq = std::make_shared<IpcNotifyBindResultReq>();
225 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
226 if (status < STATUS_DM_AUTH_FINISH && status > STATUS_DM_AUTH_DEFAULT) {
227 status = STATUS_DM_AUTH_DEFAULT;
228 }
229
230 pReq->SetPkgName(pkgName);
231 pReq->SetPeerTargetId(targetId);
232 pReq->SetResult(result);
233 pReq->SetStatus(status);
234 pReq->SetContent(content);
235 ipcServerListener_.SendRequest(BIND_TARGET_RESULT, pReq, pRsp);
236 }
237
OnUnbindResult(const std::string & pkgName,const PeerTargetId & targetId,int32_t result,std::string content)238 void DeviceManagerServiceListener::OnUnbindResult(const std::string &pkgName, const PeerTargetId &targetId,
239 int32_t result, std::string content)
240 {
241 std::shared_ptr<IpcNotifyBindResultReq> pReq = std::make_shared<IpcNotifyBindResultReq>();
242 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
243
244 pReq->SetPkgName(pkgName);
245 pReq->SetPeerTargetId(targetId);
246 pReq->SetResult(result);
247 pReq->SetContent(content);
248 ipcServerListener_.SendRequest(UNBIND_TARGET_RESULT, pReq, pRsp);
249 }
250
RegisterDmListener(const std::string & pkgName,const std::string & appId)251 void DeviceManagerServiceListener::RegisterDmListener(const std::string &pkgName, const std::string &appId)
252 {
253 std::lock_guard<std::mutex> autoLock(dmListenerMapLock_);
254 dmListenerMap_[pkgName] = appId;
255 }
256
UnRegisterDmListener(const std::string & pkgName)257 void DeviceManagerServiceListener::UnRegisterDmListener(const std::string &pkgName)
258 {
259 std::lock_guard<std::mutex> autoLock(dmListenerMapLock_);
260 dmListenerMap_.erase(pkgName);
261 }
262
DeleteDeviceIdFromMap(const std::string & deviceId,const std::string & pkgName)263 void DeviceManagerServiceListener::DeleteDeviceIdFromMap(const std::string &deviceId, const std::string &pkgName)
264 {
265 std::lock_guard<std::mutex> lock(udidHashMapLock_);
266 std::map<std::string, std::string> &udidMap = udidHashMap_[pkgName];
267 auto iter = udidMap.find(deviceId);
268 if (iter == udidMap.end()) {
269 return;
270 }
271 udidMap.erase(deviceId);
272 }
SetUdidHashMap(const std::string & udidHash,const std::string & deviceId,const std::string & pkgName)273 void DeviceManagerServiceListener::SetUdidHashMap(const std::string &udidHash, const std::string &deviceId,
274 const std::string &pkgName)
275 {
276 std::lock_guard<std::mutex> lock(udidHashMapLock_);
277 udidHashMap_[pkgName][deviceId] = udidHash;
278 }
279
GetDeviceId(const std::string & udidHash,const std::string & pkgName)280 std::string DeviceManagerServiceListener::GetDeviceId(const std::string &udidHash, const std::string &pkgName)
281 {
282 std::lock_guard<std::mutex> lock(udidHashMapLock_);
283 std::map<std::string, std::string> &udidMap = udidHashMap_[pkgName];
284 for (auto iter = udidMap.begin(); iter != udidMap.end(); iter++) {
285 if (udidHash == iter->second) {
286 return iter->first;
287 }
288 }
289 return "";
290 }
291
GetUdidHash(const std::string & deviceId,const std::string & pkgName)292 std::string DeviceManagerServiceListener::GetUdidHash(const std::string &deviceId, const std::string &pkgName)
293 {
294 std::lock_guard<std::mutex> lock(udidHashMapLock_);
295 return udidHashMap_[pkgName].count(deviceId) > 0 ? udidHashMap_[pkgName][deviceId] : "";
296 }
297
GetAppId(const std::string & pkgName)298 std::string DeviceManagerServiceListener::GetAppId(const std::string &pkgName)
299 {
300 std::lock_guard<std::mutex> autoLock(dmListenerMapLock_);
301 return dmListenerMap_.count(pkgName) > 0 ? dmListenerMap_[pkgName] : "";
302 }
303
CalcDeviceId(const std::string & pkgName,const std::string & udidHash)304 std::string DeviceManagerServiceListener::CalcDeviceId(const std::string &pkgName, const std::string &udidHash)
305 {
306 std::string appId = GetAppId(pkgName);
307 LOGI("CalcDeviceId, appId : %s, udidHash : %s.", GetAnonyString(appId).c_str(), GetAnonyString(udidHash).c_str());
308 if (appId.empty()) {
309 return udidHash;
310 }
311 std::string deviceId = GetDeviceId(udidHash, pkgName);
312 if (deviceId.empty()) {
313 deviceId = Crypto::Sha256(appId + udidHash);
314 SetUdidHashMap(udidHash, deviceId, pkgName);
315 return deviceId;
316 }
317 return deviceId;
318 }
319
OnPinHolderCreate(const std::string & pkgName,const std::string & deviceId,DmPinType pinType,const std::string & payload)320 void DeviceManagerServiceListener::OnPinHolderCreate(const std::string &pkgName, const std::string &deviceId,
321 DmPinType pinType, const std::string &payload)
322 {
323 LOGI("DeviceManagerServiceListener::OnPinHolderCreate : %s", pkgName.c_str());
324 std::shared_ptr<IpcCreatePinHolderReq> pReq = std::make_shared<IpcCreatePinHolderReq>();
325 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
326
327 pReq->SetPkgName(pkgName);
328 pReq->SetDeviceId(deviceId);
329 pReq->SetPinType(pinType);
330 pReq->SetPayload(payload);
331 ipcServerListener_.SendRequest(SERVER_CREATE_PIN_HOLDER, pReq, pRsp);
332 }
333
OnPinHolderDestroy(const std::string & pkgName,DmPinType pinType,const std::string & payload)334 void DeviceManagerServiceListener::OnPinHolderDestroy(const std::string &pkgName, DmPinType pinType,
335 const std::string &payload)
336 {
337 LOGI("DeviceManagerServiceListener::OnPinHolderDestroy : %s", pkgName.c_str());
338 std::shared_ptr<IpcDestroyPinHolderReq> pReq = std::make_shared<IpcDestroyPinHolderReq>();
339 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
340
341 pReq->SetPkgName(pkgName);
342 pReq->SetPinType(pinType);
343 pReq->SetPayload(payload);
344 ipcServerListener_.SendRequest(SERVER_DESTROY_PIN_HOLDER, pReq, pRsp);
345 }
346
OnCreateResult(const std::string & pkgName,int32_t result)347 void DeviceManagerServiceListener::OnCreateResult(const std::string &pkgName, int32_t result)
348 {
349 LOGI("DeviceManagerServiceListener::OnCreateResult : %d", result);
350 std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::make_shared<IpcNotifyPublishResultReq>();
351 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
352
353 pReq->SetPkgName(pkgName);
354 pReq->SetResult(result);
355 ipcServerListener_.SendRequest(SERVER_CREATE_PIN_HOLDER_RESULT, pReq, pRsp);
356 }
357
OnDestroyResult(const std::string & pkgName,int32_t result)358 void DeviceManagerServiceListener::OnDestroyResult(const std::string &pkgName, int32_t result)
359 {
360 LOGI("DeviceManagerServiceListener::OnDestroyResult : %d", result);
361 std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::make_shared<IpcNotifyPublishResultReq>();
362 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
363
364 pReq->SetPkgName(pkgName);
365 pReq->SetResult(result);
366 ipcServerListener_.SendRequest(SERVER_DESTROY_PIN_HOLDER_RESULT, pReq, pRsp);
367 }
368 } // namespace DistributedHardware
369 } // namespace OHOS
370