1 /*
2 * Copyright (c) 2021 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 <memory>
17
18 #include "device_manager_service.h"
19 #include "dm_constants.h"
20 #include "dm_device_info.h"
21 #include "dm_log.h"
22 #include "dm_subscribe_info.h"
23 #include "ipc_cmd_register.h"
24 #include "ipc_def.h"
25 #include "ipc_notify_auth_result_req.h"
26 #include "ipc_notify_device_found_req.h"
27 #include "ipc_notify_device_state_req.h"
28 #include "ipc_notify_discover_result_req.h"
29 #include "ipc_notify_verify_auth_result_req.h"
30 #include "ipc_server_stub.h"
31
32 namespace OHOS {
33 namespace DistributedHardware {
ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)34 ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
35 {
36 std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::static_pointer_cast<IpcNotifyDeviceStateReq>(pBaseReq);
37 std::string pkgName = pReq->GetPkgName();
38 int32_t deviceState = pReq->GetDeviceState();
39 DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
40 if (!data.WriteString(pkgName)) {
41 LOGE("write pkgName failed");
42 return DM_FLATTEN_OBJECT;
43 }
44 if (!data.WriteInt32(deviceState)) {
45 LOGE("write state failed");
46 return DM_FLATTEN_OBJECT;
47 }
48 if (!data.WriteRawData(&deviceInfo, sizeof(DmDeviceInfo))) {
49 LOGE("write deviceInfo failed");
50 return DM_FLATTEN_OBJECT;
51 }
52 return DM_OK;
53 }
54
ON_IPC_READ_RESPONSE(SERVER_DEVICE_STATE_NOTIFY,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)55 ON_IPC_READ_RESPONSE(SERVER_DEVICE_STATE_NOTIFY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
56 {
57 pBaseRsp->SetErrCode(reply.ReadInt32());
58 return DM_OK;
59 }
60
ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)61 ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
62 {
63 std::shared_ptr<IpcNotifyDeviceFoundReq> pReq = std::static_pointer_cast<IpcNotifyDeviceFoundReq>(pBaseReq);
64 std::string pkgName = pReq->GetPkgName();
65 uint16_t subscribeId = pReq->GetSubscribeId();
66 DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
67 if (!data.WriteString(pkgName)) {
68 LOGE("write pkgName failed");
69 return DM_FLATTEN_OBJECT;
70 }
71 if (!data.WriteInt16((int16_t)subscribeId)) {
72 LOGE("write subscribeId failed");
73 return DM_FLATTEN_OBJECT;
74 }
75 if (!data.WriteRawData(&deviceInfo, sizeof(DmDeviceInfo))) {
76 LOGE("write deviceInfo failed");
77 return DM_FLATTEN_OBJECT;
78 }
79 return DM_OK;
80 }
81
ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)82 ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
83 {
84 pBaseRsp->SetErrCode(reply.ReadInt32());
85 return DM_OK;
86 }
87
ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)88 ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
89 {
90 std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::static_pointer_cast<IpcNotifyDiscoverResultReq>(pBaseReq);
91 std::string pkgName = pReq->GetPkgName();
92 uint16_t subscribeId = pReq->GetSubscribeId();
93 int32_t result = pReq->GetResult();
94 if (!data.WriteString(pkgName)) {
95 LOGE("write pkgName failed");
96 return DM_FLATTEN_OBJECT;
97 }
98 if (!data.WriteInt16((int16_t)subscribeId)) {
99 LOGE("write subscribeId failed");
100 return DM_FLATTEN_OBJECT;
101 }
102 if (!data.WriteInt32(result)) {
103 LOGE("write result failed");
104 return DM_FLATTEN_OBJECT;
105 }
106 return DM_OK;
107 }
108
ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)109 ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
110 {
111 if (pBaseRsp == nullptr) {
112 LOGE("pBaseRsp is null");
113 return DM_FAILED;
114 }
115 pBaseRsp->SetErrCode(reply.ReadInt32());
116 return DM_OK;
117 }
118
ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)119 ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
120 {
121 std::shared_ptr<IpcNotifyAuthResultReq> pReq = std::static_pointer_cast<IpcNotifyAuthResultReq>(pBaseReq);
122 std::string pkgName = pReq->GetPkgName();
123 std::string deviceId = pReq->GetDeviceId();
124 std::string token = pReq->GetPinToken();
125 int32_t status = pReq->GetStatus();
126 int32_t reason = pReq->GetReason();
127 if (!data.WriteString(pkgName)) {
128 LOGE("write pkgName failed");
129 return DM_FLATTEN_OBJECT;
130 }
131 if (!data.WriteString(deviceId)) {
132 LOGE("write deviceId failed");
133 return DM_FLATTEN_OBJECT;
134 }
135 if (!data.WriteString(token)) {
136 LOGE("write token failed");
137 return DM_FLATTEN_OBJECT;
138 }
139 if (!data.WriteInt32(status)) {
140 LOGE("write status failed");
141 return DM_FLATTEN_OBJECT;
142 }
143 if (!data.WriteInt32(reason)) {
144 LOGE("write reason failed");
145 return DM_FLATTEN_OBJECT;
146 }
147 return DM_OK;
148 }
149
ON_IPC_READ_RESPONSE(SERVER_AUTH_RESULT,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)150 ON_IPC_READ_RESPONSE(SERVER_AUTH_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
151 {
152 pBaseRsp->SetErrCode(reply.ReadInt32());
153 return DM_OK;
154 }
155
ON_IPC_SET_REQUEST(SERVER_VERIFY_AUTH_RESULT,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)156 ON_IPC_SET_REQUEST(SERVER_VERIFY_AUTH_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
157 {
158 std::shared_ptr<IpcNotifyVerifyAuthResultReq> pReq =
159 std::static_pointer_cast<IpcNotifyVerifyAuthResultReq>(pBaseReq);
160 std::string pkgName = pReq->GetPkgName();
161 std::string deviceId = pReq->GetDeviceId();
162 int32_t result = pReq->GetResult();
163 int32_t flag = pReq->GetFlag();
164 if (!data.WriteString(pkgName)) {
165 LOGE("write pkgName failed");
166 return DM_FLATTEN_OBJECT;
167 }
168 if (!data.WriteString(deviceId)) {
169 LOGE("write deviceId failed");
170 return DM_FLATTEN_OBJECT;
171 }
172 if (!data.WriteInt32(result)) {
173 LOGE("write result failed");
174 return DM_FLATTEN_OBJECT;
175 }
176 if (!data.WriteInt32(flag)) {
177 LOGE("write flag failed");
178 return DM_FLATTEN_OBJECT;
179 }
180 return DM_OK;
181 }
182
ON_IPC_READ_RESPONSE(SERVER_VERIFY_AUTH_RESULT,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)183 ON_IPC_READ_RESPONSE(SERVER_VERIFY_AUTH_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
184 {
185 pBaseRsp->SetErrCode(reply.ReadInt32());
186 return DM_OK;
187 }
188
ON_IPC_SET_REQUEST(SERVER_DEVICE_FA_NOTIFY,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)189 ON_IPC_SET_REQUEST(SERVER_DEVICE_FA_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
190 {
191 std::shared_ptr<IpcNotifyDMFAResultReq> pReq = std::static_pointer_cast<IpcNotifyDMFAResultReq>(pBaseReq);
192 std::string packagname = pReq->GetPkgName();
193 std::string paramJson = pReq->GetJsonParam();
194 if (!data.WriteString(packagname)) {
195 LOGE("write pkgName failed");
196 return DM_FLATTEN_OBJECT;
197 }
198 if (!data.WriteString(paramJson)) {
199 LOGE("write paramJson failed");
200 return DM_FLATTEN_OBJECT;
201 }
202 return DM_OK;
203 }
204
ON_IPC_READ_RESPONSE(SERVER_DEVICE_FA_NOTIFY,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)205 ON_IPC_READ_RESPONSE(SERVER_DEVICE_FA_NOTIFY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
206 {
207 pBaseRsp->SetErrCode(reply.ReadInt32());
208 return DM_OK;
209 }
210
ON_IPC_CMD(GET_TRUST_DEVICE_LIST,MessageParcel & data,MessageParcel & reply)211 ON_IPC_CMD(GET_TRUST_DEVICE_LIST, MessageParcel &data, MessageParcel &reply)
212 {
213 std::string pkgName = data.ReadString();
214 std::string extra = data.ReadString();
215 LOGI("pkgName:%s, extra:%s", pkgName.c_str(), extra.c_str());
216 std::vector<DmDeviceInfo> deviceList;
217 int32_t result = DeviceManagerService::GetInstance().GetTrustedDeviceList(pkgName, extra, deviceList);
218 int32_t infoNum =(int32_t)(deviceList.size());
219 DmDeviceInfo deviceInfo;
220 if (!reply.WriteInt32(infoNum)) {
221 LOGE("write infoNum failed");
222 return DM_WRITE_FAILED;
223 }
224 if (!deviceList.empty()) {
225 for (; !deviceList.empty();) {
226 deviceInfo = deviceList.back();
227 deviceList.pop_back();
228
229 if (!reply.WriteRawData(&deviceInfo, sizeof(DmDeviceInfo))) {
230 LOGE("write subscribeInfo failed");
231 return DM_WRITE_FAILED;
232 }
233 }
234 }
235 if (!reply.WriteInt32(result)) {
236 LOGE("write result failed");
237 return DM_WRITE_FAILED;
238 }
239 LOGI("GET_TRUST_DEVICE_LIST ok pkgName:%s, extra:%s", pkgName.c_str(), extra.c_str());
240 return DM_OK;
241 }
242
ON_IPC_CMD(REGISTER_DEVICE_MANAGER_LISTENER,MessageParcel & data,MessageParcel & reply)243 ON_IPC_CMD(REGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &data, MessageParcel &reply)
244 {
245 std::string pkgName = data.ReadString();
246 sptr<IRemoteObject> listener = data.ReadRemoteObject();
247 int32_t result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener);
248 if (!reply.WriteInt32(result)) {
249 LOGE("write result failed");
250 return DM_WRITE_FAILED;
251 }
252 return DM_OK;
253 }
254
ON_IPC_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER,MessageParcel & data,MessageParcel & reply)255 ON_IPC_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &data, MessageParcel &reply)
256 {
257 std::string pkgName = data.ReadString();
258 int32_t result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName);
259 if (!reply.WriteInt32(result)) {
260 LOGE("write result failed");
261 return DM_WRITE_FAILED;
262 }
263 return DM_OK;
264 }
265
ON_IPC_CMD(START_DEVICE_DISCOVER,MessageParcel & data,MessageParcel & reply)266 ON_IPC_CMD(START_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply)
267 {
268 std::string pkgName = data.ReadString();
269 std::string extra = data.ReadString();
270 DmSubscribeInfo *subscribeInfo = (DmSubscribeInfo *)data.ReadRawData(sizeof(DmSubscribeInfo));
271 int32_t result = DM_POINT_NULL;
272
273 if (subscribeInfo != nullptr) {
274 LOGI("pkgName:%s, subscribeId: %d", pkgName.c_str(), subscribeInfo->subscribeId);
275 result = DeviceManagerService::GetInstance().StartDeviceDiscovery(pkgName, *subscribeInfo, extra);
276 }
277 if (!reply.WriteInt32(result)) {
278 LOGE("write result failed");
279 return DM_WRITE_FAILED;
280 }
281 return DM_OK;
282 }
283
ON_IPC_CMD(STOP_DEVICE_DISCOVER,MessageParcel & data,MessageParcel & reply)284 ON_IPC_CMD(STOP_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply)
285 {
286 std::string pkgName = data.ReadString();
287 uint16_t subscribeId = (uint16_t)(data.ReadInt32());
288 LOGI("pkgName:%s, subscribeId: %d", pkgName.c_str(), subscribeId);
289 int32_t result = DeviceManagerService::GetInstance().StopDeviceDiscovery(pkgName, subscribeId);
290 if (!reply.WriteInt32(result)) {
291 LOGE("write result failed");
292 return DM_WRITE_FAILED;
293 }
294 return DM_OK;
295 }
296
ON_IPC_CMD(AUTHENTICATE_DEVICE,MessageParcel & data,MessageParcel & reply)297 ON_IPC_CMD(AUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply)
298 {
299 std::string pkgName = data.ReadString();
300 std::string extra = data.ReadString();
301 std::string deviceId = data.ReadString();
302 int32_t authType = data.ReadInt32();
303 int32_t result = DM_OK;
304 result = DeviceManagerService::GetInstance().AuthenticateDevice(pkgName, authType, deviceId, extra);
305 LOGE("AuthenticateDevice");
306 if (!reply.WriteInt32(result)) {
307 LOGE("write result failed");
308 return DM_WRITE_FAILED;
309 }
310 LOGE("AuthenticateDevice %d", result);
311 return DM_OK;
312 }
313
ON_IPC_CMD(UNAUTHENTICATE_DEVICE,MessageParcel & data,MessageParcel & reply)314 ON_IPC_CMD(UNAUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply)
315 {
316 std::string pkgName = data.ReadString();
317 std::string deviceId = data.ReadString();
318 int32_t result = DM_OK;
319 LOGI("pkgName:%s, trustedDeviceInfo: %d", pkgName.c_str(), deviceId.c_str());
320 result = DeviceManagerService::GetInstance().UnAuthenticateDevice(pkgName, deviceId);
321 if (!reply.WriteInt32(result)) {
322 LOGE("write result failed");
323 return DM_WRITE_FAILED;
324 }
325 return DM_OK;
326 }
327
ON_IPC_CMD(VERIFY_AUTHENTICATION,MessageParcel & data,MessageParcel & reply)328 ON_IPC_CMD(VERIFY_AUTHENTICATION, MessageParcel &data, MessageParcel &reply)
329 {
330 LOGI("ON_IPC_CMD VERIFY_AUTHENTICATION start");
331 std::string authPara = data.ReadString();
332 int32_t result = DeviceManagerService::GetInstance().VerifyAuthentication(authPara);
333 if (!reply.WriteInt32(result)) {
334 LOGE("write result failed");
335 return DM_WRITE_FAILED;
336 }
337 return DM_OK;
338 }
339
ON_IPC_CMD(GET_LOCAL_DEVICE_INFO,MessageParcel & data,MessageParcel & reply)340 ON_IPC_CMD(GET_LOCAL_DEVICE_INFO, MessageParcel &data, MessageParcel &reply)
341 {
342 DmDeviceInfo localDeviceInfo;
343 int32_t result = 0;
344 result = DeviceManagerService::GetInstance().GetLocalDeviceInfo(localDeviceInfo);
345
346 if (!reply.WriteRawData(&localDeviceInfo, sizeof(DmDeviceInfo))) {
347 LOGE("write subscribeInfo failed");
348 }
349
350 if (!reply.WriteInt32(result)) {
351 LOGE("write result failed");
352 return DM_WRITE_FAILED;
353 }
354 LOGI("localDeviceInfo: %s", localDeviceInfo.deviceId);
355 return DM_OK;
356 }
357
ON_IPC_CMD(GET_UDID_BY_NETWORK,MessageParcel & data,MessageParcel & reply)358 ON_IPC_CMD(GET_UDID_BY_NETWORK, MessageParcel &data, MessageParcel &reply)
359 {
360 std::string pkgName = data.ReadString();
361 std::string netWorkId = data.ReadString();
362 std::string udid;
363 int32_t result = DeviceManagerService::GetInstance().GetUdidByNetworkId(pkgName, netWorkId, udid);
364
365 if (!reply.WriteInt32(result)) {
366 LOGE("write result failed");
367 return DM_WRITE_FAILED;
368 }
369 if (!reply.WriteString(udid)) {
370 LOGE("write result failed");
371 return DM_WRITE_FAILED;
372 }
373 return DM_OK;
374 }
375
ON_IPC_CMD(GET_UUID_BY_NETWORK,MessageParcel & data,MessageParcel & reply)376 ON_IPC_CMD(GET_UUID_BY_NETWORK, MessageParcel &data, MessageParcel &reply)
377 {
378 std::string pkgName = data.ReadString();
379 std::string netWorkId = data.ReadString();
380 std::string uuid;
381 int32_t result = DeviceManagerService::GetInstance().GetUuidByNetworkId(pkgName, netWorkId, uuid);
382
383 if (!reply.WriteInt32(result)) {
384 LOGE("write result failed");
385 return DM_WRITE_FAILED;
386 }
387 if (!reply.WriteString(uuid)) {
388 LOGE("write result failed");
389 return DM_WRITE_FAILED;
390 }
391 return DM_OK;
392 }
393
ON_IPC_CMD(SERVER_GET_DMFA_INFO,MessageParcel & data,MessageParcel & reply)394 ON_IPC_CMD(SERVER_GET_DMFA_INFO, MessageParcel &data, MessageParcel &reply)
395 {
396 std::string packName = data.ReadString();
397 DmAuthParam authParam;
398 int32_t ret = DM_OK;
399 ret = DeviceManagerService::GetInstance().GetFaParam(packName, authParam);
400 int32_t appIconLen = authParam.imageinfo.GetAppIconLen();
401 int32_t appThumbnailLen = authParam.imageinfo.GetAppThumbnailLen();
402
403 if (!reply.WriteInt32(authParam.direction) || !reply.WriteInt32(authParam.authType) ||
404 !reply.WriteString(authParam.authToken) || !reply.WriteString(authParam.packageName) ||
405 !reply.WriteString(authParam.appName) || !reply.WriteString(authParam.appDescription) ||
406 !reply.WriteInt32(authParam.business) || !reply.WriteInt32(authParam.pincode) ||
407 !reply.WriteInt32(appIconLen) || !reply.WriteInt32(appThumbnailLen)) {
408 LOGE("write reply failed");
409 return DM_IPC_FLATTEN_OBJECT;
410 }
411
412 if (appIconLen > 0 && authParam.imageinfo.GetAppIcon() != nullptr) {
413 if (!reply.WriteRawData(authParam.imageinfo.GetAppIcon(), appIconLen)) {
414 LOGE("write appIcon failed");
415 return DM_IPC_FLATTEN_OBJECT;
416 }
417 }
418 if (appThumbnailLen > 0 && authParam.imageinfo.GetAppThumbnail() != nullptr) {
419 if (!reply.WriteRawData(authParam.imageinfo.GetAppThumbnail(), appThumbnailLen)) {
420 LOGE("write appThumbnail failed");
421 return DM_IPC_FLATTEN_OBJECT;
422 }
423 }
424 return DM_OK;
425 }
426
ON_IPC_CMD(SERVER_USER_AUTH_OPERATION,MessageParcel & data,MessageParcel & reply)427 ON_IPC_CMD(SERVER_USER_AUTH_OPERATION, MessageParcel &data, MessageParcel &reply)
428 {
429 std::string packageName = data.ReadString();
430 int32_t action = data.ReadInt32();
431 std::string params = data.ReadString();
432 int result = DeviceManagerService::GetInstance().SetUserOperation(packageName, action, params);
433 if (!reply.WriteInt32(result)) {
434 LOGE("write result failed");
435 return DM_WRITE_FAILED;
436 }
437 return result;
438 }
439
ON_IPC_CMD(REGISTER_DEV_STATE_CALLBACK,MessageParcel & data,MessageParcel & reply)440 ON_IPC_CMD(REGISTER_DEV_STATE_CALLBACK, MessageParcel &data, MessageParcel &reply)
441 {
442 std::string packageName = data.ReadString();
443 std::string extra = data.ReadString();
444 int result = DeviceManagerService::GetInstance().RegisterDevStateCallback(packageName, extra);
445 if (!reply.WriteInt32(result)) {
446 LOGE("write result failed");
447 return DM_WRITE_FAILED;
448 }
449 return result;
450 }
451
ON_IPC_CMD(UNREGISTER_DEV_STATE_CALLBACK,MessageParcel & data,MessageParcel & reply)452 ON_IPC_CMD(UNREGISTER_DEV_STATE_CALLBACK, MessageParcel &data, MessageParcel &reply)
453 {
454 std::string packageName = data.ReadString();
455 std::string extra = data.ReadString();
456 int result = DeviceManagerService::GetInstance().UnRegisterDevStateCallback(packageName, extra);
457 if (!reply.WriteInt32(result)) {
458 LOGE("write result failed");
459 return DM_WRITE_FAILED;
460 }
461 return result;
462 }
463 } // namespace DistributedHardware
464 } // namespace OHOS
465