1 /*
2 * Copyright (c) 2022-2023 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_notify_auth_result_req.h"
24 #include "ipc_notify_credential_req.h"
25 #include "ipc_notify_device_found_req.h"
26 #include "ipc_notify_device_discovery_req.h"
27 #include "ipc_notify_device_state_req.h"
28 #include "ipc_notify_discover_result_req.h"
29 #include "ipc_notify_publish_result_req.h"
30 #include "ipc_notify_verify_auth_result_req.h"
31
32 namespace OHOS {
33 namespace DistributedHardware {
34 std::mutex DeviceManagerServiceListener::dmListenerMapLock_;
35 std::mutex DeviceManagerServiceListener::udidHashMapLock_;
36 std::map<std::string, std::string> DeviceManagerServiceListener::dmListenerMap_ = {};
37 std::map<std::string, std::string> DeviceManagerServiceListener::udidHashMap_ = {};
38
ConvertDeviceInfoToDeviceBasicInfo(const std::string & pkgName,const DmDeviceInfo & info,DmDeviceBasicInfo & deviceBasicInfo)39 void DeviceManagerServiceListener::ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName,
40 const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo)
41 {
42 (void)memset_s(&deviceBasicInfo, sizeof(DmDeviceBasicInfo), 0, sizeof(DmDeviceBasicInfo));
43 if (memcpy_s(deviceBasicInfo.deviceName, sizeof(deviceBasicInfo.deviceName), info.deviceName,
44 std::min(sizeof(deviceBasicInfo.deviceName), sizeof(info.deviceName))) != DM_OK) {
45 LOGE("ConvertDeviceInfoToDmDevice copy deviceName data failed.");
46 }
47
48 if (memcpy_s(deviceBasicInfo.networkId, sizeof(deviceBasicInfo.networkId), info.networkId,
49 std::min(sizeof(deviceBasicInfo.networkId), sizeof(info.networkId))) != DM_OK) {
50 LOGE("ConvertNodeBasicInfoToDmDevice copy networkId data failed.");
51 }
52 deviceBasicInfo.deviceTypeId = info.deviceTypeId;
53 }
54
OnDeviceStateChange(const std::string & pkgName,const DmDeviceState & state,const DmDeviceInfo & info)55 void DeviceManagerServiceListener::OnDeviceStateChange(const std::string &pkgName, const DmDeviceState &state,
56 const DmDeviceInfo &info)
57 {
58 LOGI("OnDeviceStateChange, state = %d", state);
59 std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
60 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
61 DmDeviceBasicInfo deviceBasicInfo;
62 ConvertDeviceInfoToDeviceBasicInfo(pkgName, info, deviceBasicInfo);
63 if (pkgName == std::string(DM_PKG_NAME)) {
64 std::vector<std::string> PkgNameVec = ipcServerListener_.GetAllPkgName();
65 for (const auto &it : PkgNameVec) {
66 std::string udIdHash = CalcDeviceId(it, info.deviceId);
67 if (memcpy_s(deviceBasicInfo.deviceId, DM_MAX_DEVICE_ID_LEN,
68 udIdHash.c_str(), udIdHash.length()) != DM_OK) {
69 LOGE("ConvertDeviceInfoToDmDevice copy deviceId data failed.");
70 }
71 pReq->SetPkgName(it);
72 pReq->SetDeviceState(state);
73 pReq->SetDeviceInfo(info);
74 pReq->SetDeviceBasicInfo(deviceBasicInfo);
75 ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
76 }
77 } else {
78 ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
79 }
80 }
81
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,const DmDeviceInfo & info)82 void DeviceManagerServiceListener::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
83 const DmDeviceInfo &info)
84 {
85 std::shared_ptr<IpcNotifyDeviceFoundReq> pReq = std::make_shared<IpcNotifyDeviceFoundReq>();
86 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
87
88 pReq->SetPkgName(pkgName);
89 pReq->SetSubscribeId(subscribeId);
90 pReq->SetDeviceInfo(info);
91 ipcServerListener_.SendRequest(SERVER_DEVICE_FOUND, pReq, pRsp);
92 }
93
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,DmDeviceBasicInfo & info)94 void DeviceManagerServiceListener::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
95 DmDeviceBasicInfo &info)
96 {
97 std::shared_ptr<IpcNotifyDeviceDiscoveryReq> pReq = std::make_shared<IpcNotifyDeviceDiscoveryReq>();
98 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
99 std::string udIdHash = CalcDeviceId(pkgName, info.deviceId);
100 if (memcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, udIdHash.c_str(), udIdHash.length()) != DM_OK) {
101 LOGE("ConvertDeviceInfoToDmDevice copy deviceId data failed.");
102 }
103 pReq->SetPkgName(pkgName);
104 pReq->SetSubscribeId(subscribeId);
105 pReq->SetDeviceBasicInfo(info);
106 ipcServerListener_.SendRequest(SERVER_DEVICE_DISCOVERY, pReq, pRsp);
107 }
108
OnDiscoveryFailed(const std::string & pkgName,uint16_t subscribeId,int32_t failedReason)109 void DeviceManagerServiceListener::OnDiscoveryFailed(const std::string &pkgName, uint16_t subscribeId,
110 int32_t failedReason)
111 {
112 LOGI("DeviceManagerServiceListener::OnDiscoveryFailed");
113 std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::make_shared<IpcNotifyDiscoverResultReq>();
114 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
115
116 pReq->SetPkgName(pkgName);
117 pReq->SetSubscribeId(subscribeId);
118 pReq->SetResult(failedReason);
119 ipcServerListener_.SendRequest(SERVER_DISCOVER_FINISH, pReq, pRsp);
120 }
121
OnDiscoverySuccess(const std::string & pkgName,int32_t subscribeId)122 void DeviceManagerServiceListener::OnDiscoverySuccess(const std::string &pkgName, int32_t subscribeId)
123 {
124 LOGI("DeviceManagerServiceListener::OnDiscoverySuccess");
125 std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::make_shared<IpcNotifyDiscoverResultReq>();
126 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
127
128 pReq->SetPkgName(pkgName);
129 pReq->SetSubscribeId((uint16_t)subscribeId);
130 pReq->SetResult(DM_OK);
131 ipcServerListener_.SendRequest(SERVER_DISCOVER_FINISH, pReq, pRsp);
132 }
133
OnPublishResult(const std::string & pkgName,int32_t publishId,int32_t publishResult)134 void DeviceManagerServiceListener::OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult)
135 {
136 LOGI("DeviceManagerServiceListener::OnPublishResult : %d", publishResult);
137 std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::make_shared<IpcNotifyPublishResultReq>();
138 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
139
140 pReq->SetPkgName(pkgName);
141 pReq->SetPublishId(publishId);
142 pReq->SetResult(publishResult);
143 ipcServerListener_.SendRequest(SERVER_PUBLISH_FINISH, pReq, pRsp);
144 }
145
OnAuthResult(const std::string & pkgName,const std::string & deviceId,const std::string & token,int32_t status,int32_t reason)146 void DeviceManagerServiceListener::OnAuthResult(const std::string &pkgName, const std::string &deviceId,
147 const std::string &token, int32_t status, int32_t reason)
148 {
149 std::shared_ptr<IpcNotifyAuthResultReq> pReq = std::make_shared<IpcNotifyAuthResultReq>();
150 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
151
152 pReq->SetPkgName(pkgName);
153 pReq->SetDeviceId(deviceId);
154 pReq->SetToken(token);
155 pReq->SetStatus(status);
156 pReq->SetReason(reason);
157 ipcServerListener_.SendRequest(SERVER_AUTH_RESULT, pReq, pRsp);
158 }
159
OnVerifyAuthResult(const std::string & pkgName,const std::string & deviceId,int32_t resultCode,const std::string & flag)160 void DeviceManagerServiceListener::OnVerifyAuthResult(const std::string &pkgName, const std::string &deviceId,
161 int32_t resultCode, const std::string &flag)
162 {
163 std::shared_ptr<IpcNotifyVerifyAuthResultReq> pReq = std::make_shared<IpcNotifyVerifyAuthResultReq>();
164 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
165
166 pReq->SetDeviceId(deviceId);
167 pReq->SetResult(resultCode);
168 ipcServerListener_.SendAll(SERVER_VERIFY_AUTH_RESULT, pReq, pRsp);
169 }
170
OnUiCall(std::string & pkgName,std::string & paramJson)171 void DeviceManagerServiceListener::OnUiCall(std::string &pkgName, std::string ¶mJson)
172 {
173 LOGI("OnUiCall in");
174 std::shared_ptr<IpcNotifyDMFAResultReq> pReq = std::make_shared<IpcNotifyDMFAResultReq>();
175 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
176
177 pReq->SetPkgName(pkgName);
178 pReq->SetJsonParam(paramJson);
179 ipcServerListener_.SendRequest(SERVER_DEVICE_FA_NOTIFY, pReq, pRsp);
180 }
181
OnCredentialResult(const std::string & pkgName,int32_t action,const std::string & resultInfo)182 void DeviceManagerServiceListener::OnCredentialResult(const std::string &pkgName, int32_t action,
183 const std::string &resultInfo)
184 {
185 LOGI("call OnCredentialResult for %s, action %d", pkgName.c_str(), action);
186 std::shared_ptr<IpcNotifyCredentialReq> pReq = std::make_shared<IpcNotifyCredentialReq>();
187 std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
188
189 pReq->SetPkgName(pkgName);
190 pReq->SetCredentialAction(action);
191 pReq->SetCredentialResult(resultInfo);
192 ipcServerListener_.SendRequest(SERVER_CREDENTIAL_RESULT, pReq, pRsp);
193 }
194
RegisterDmListener(const std::string & pkgName,const std::string & appId)195 void DeviceManagerServiceListener::RegisterDmListener(const std::string &pkgName, const std::string &appId)
196 {
197 std::lock_guard<std::mutex> autoLock(dmListenerMapLock_);
198 dmListenerMap_[pkgName] = appId;
199 }
200
UnRegisterDmListener(const std::string & pkgName)201 void DeviceManagerServiceListener::UnRegisterDmListener(const std::string &pkgName)
202 {
203 std::lock_guard<std::mutex> autoLock(dmListenerMapLock_);
204 dmListenerMap_.erase(pkgName);
205 }
206
DeleteDeviceIdFromMap(const std::string & deviceId)207 void DeviceManagerServiceListener::DeleteDeviceIdFromMap(const std::string &deviceId)
208 {
209 std::lock_guard<std::mutex> lock(udidHashMapLock_);
210 auto iter = udidHashMap_.find(deviceId);
211 if (iter == udidHashMap_.end()) {
212 return;
213 }
214 udidHashMap_.erase(deviceId);
215 }
SetUdidHashMap(const std::string & udidHash,const std::string & deviceId)216 void DeviceManagerServiceListener::SetUdidHashMap(const std::string &udidHash, const std::string &deviceId)
217 {
218 std::lock_guard<std::mutex> lock(udidHashMapLock_);
219 udidHashMap_[deviceId] = udidHash;
220 }
221
GetDeviceId(const std::string & udidHash)222 std::string DeviceManagerServiceListener::GetDeviceId(const std::string &udidHash)
223 {
224 std::lock_guard<std::mutex> lock(udidHashMapLock_);
225 for (auto iter = udidHashMap_.begin(); iter != udidHashMap_.end(); iter++) {
226 if (udidHash == iter->second) {
227 return iter->first;
228 }
229 }
230 return "";
231 }
232
GetUdidHash(const std::string & deviceId)233 std::string DeviceManagerServiceListener::GetUdidHash(const std::string &deviceId)
234 {
235 std::lock_guard<std::mutex> lock(udidHashMapLock_);
236 return udidHashMap_.count(deviceId) > 0 ? udidHashMap_[deviceId] : "";
237 }
238
GetAppId(const std::string & pkgName)239 std::string DeviceManagerServiceListener::GetAppId(const std::string &pkgName)
240 {
241 std::lock_guard<std::mutex> autoLock(dmListenerMapLock_);
242 return dmListenerMap_.count(pkgName) > 0 ? dmListenerMap_[pkgName] : "";
243 }
244
CalcDeviceId(const std::string & pkgName,const std::string & udidHash)245 std::string DeviceManagerServiceListener::CalcDeviceId(const std::string &pkgName, const std::string &udidHash)
246 {
247 std::string appId = GetAppId(pkgName);
248 LOGI("CalcDeviceId, appId : %s, udidHash : %s.", GetAnonyString(appId).c_str(), GetAnonyString(udidHash).c_str());
249 if (appId.empty()) {
250 return udidHash;
251 }
252 std::string deviceId = GetDeviceId(udidHash);
253 if (deviceId.empty()) {
254 deviceId = Crypto::Sha256(appId + udidHash);
255 SetUdidHashMap(udidHash, deviceId);
256 return deviceId;
257 }
258 return deviceId;
259 }
260 } // namespace DistributedHardware
261 } // namespace OHOS
262