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 <memory>
17 #include "xcollie/xcollie.h"
18 #include "xcollie/xcollie_define.h"
19 
20 #include "device_manager_ipc_interface_code.h"
21 #include "device_manager_service.h"
22 #include "dm_anonymous.h"
23 #include "dm_constants.h"
24 #include "dm_device_info.h"
25 #include "dm_log.h"
26 #include "dm_subscribe_info.h"
27 #include "dm_publish_info.h"
28 #include "ipc_acl_profile_req.h"
29 #include "ipc_cmd_register.h"
30 #include "ipc_def.h"
31 #include "ipc_create_pin_holder_req.h"
32 #include "ipc_credential_auth_status_req.h"
33 #include "ipc_destroy_pin_holder_req.h"
34 #include "ipc_notify_devicetrustchange_req.h"
35 #include "ipc_notify_auth_result_req.h"
36 #include "ipc_notify_bind_result_req.h"
37 #include "ipc_notify_credential_req.h"
38 #include "ipc_notify_device_found_req.h"
39 #include "ipc_notify_device_discovery_req.h"
40 #include "ipc_notify_device_state_req.h"
41 #include "ipc_notify_discover_result_req.h"
42 #include "ipc_notify_publish_result_req.h"
43 #include "ipc_notify_pin_holder_event_req.h"
44 #include "ipc_server_client_proxy.h"
45 #include "ipc_server_stub.h"
46 
47 #include "nlohmann/json.hpp"
48 
49 namespace OHOS {
50 namespace DistributedHardware {
51 const unsigned int XCOLLIE_TIMEOUT_S = 5;
EncodeDmDeviceInfo(const DmDeviceInfo &devInfo, MessageParcel &parcel)52 bool EncodeDmDeviceInfo(const DmDeviceInfo &devInfo, MessageParcel &parcel)
53 {
54     bool bRet = true;
55     std::string deviceIdStr(devInfo.deviceId);
56     bRet = (bRet && parcel.WriteString(deviceIdStr));
57     std::string deviceNameStr(devInfo.deviceName);
58     bRet = (bRet && parcel.WriteString(deviceNameStr));
59     bRet = (bRet && parcel.WriteUint16(devInfo.deviceTypeId));
60     std::string networkIdStr(devInfo.networkId);
61     bRet = (bRet && parcel.WriteString(networkIdStr));
62     bRet = (bRet && parcel.WriteInt32(devInfo.range));
63     bRet = (bRet && parcel.WriteInt32(devInfo.networkType));
64     bRet = (bRet && parcel.WriteInt32(devInfo.authForm));
65     bRet = (bRet && parcel.WriteString(devInfo.extraData));
66     return bRet;
67 }
68 
EncodeDmDeviceBasicInfo(const DmDeviceBasicInfo &devInfo, MessageParcel &parcel)69 bool EncodeDmDeviceBasicInfo(const DmDeviceBasicInfo &devInfo, MessageParcel &parcel)
70 {
71     bool bRet = true;
72     std::string deviceIdStr(devInfo.deviceId);
73     bRet = (bRet && parcel.WriteString(deviceIdStr));
74     std::string deviceNameStr(devInfo.deviceName);
75     bRet = (bRet && parcel.WriteString(deviceNameStr));
76     bRet = (bRet && parcel.WriteUint16(devInfo.deviceTypeId));
77     std::string networkIdStr(devInfo.networkId);
78     bRet = (bRet && parcel.WriteString(networkIdStr));
79     return bRet;
80 }
81 
EncodePeerTargetId(const PeerTargetId &targetId, MessageParcel &parcel)82 bool EncodePeerTargetId(const PeerTargetId &targetId, MessageParcel &parcel)
83 {
84     bool bRet = true;
85     bRet = (bRet && parcel.WriteString(targetId.deviceId));
86     bRet = (bRet && parcel.WriteString(targetId.brMac));
87     bRet = (bRet && parcel.WriteString(targetId.bleMac));
88     bRet = (bRet && parcel.WriteString(targetId.wifiIp));
89     bRet = (bRet && parcel.WriteUint16(targetId.wifiPort));
90     return bRet;
91 }
92 
DecodePeerTargetId(MessageParcel &parcel, PeerTargetId &targetId)93 void DecodePeerTargetId(MessageParcel &parcel, PeerTargetId &targetId)
94 {
95     targetId.deviceId = parcel.ReadString();
96     targetId.brMac = parcel.ReadString();
97     targetId.bleMac = parcel.ReadString();
98     targetId.wifiIp = parcel.ReadString();
99     targetId.wifiPort = parcel.ReadUint16();
100 }
101 
DecodeDmAccessCaller(MessageParcel &parcel, DmAccessCaller &caller)102 void DecodeDmAccessCaller(MessageParcel &parcel, DmAccessCaller &caller)
103 {
104     caller.accountId = parcel.ReadString();
105     caller.pkgName = parcel.ReadString();
106     caller.networkId = parcel.ReadString();
107     caller.userId = parcel.ReadInt32();
108     caller.tokenId = parcel.ReadUint64();
109     caller.extra = parcel.ReadString();
110 }
111 
DecodeDmAccessCallee(MessageParcel &parcel, DmAccessCallee &callee)112 void DecodeDmAccessCallee(MessageParcel &parcel, DmAccessCallee &callee)
113 {
114     callee.accountId = parcel.ReadString();
115     callee.networkId = parcel.ReadString();
116     callee.peerId = parcel.ReadString();
117     callee.userId = parcel.ReadInt32();
118     callee.extra = parcel.ReadString();
119 }
120 
ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)121 ON_IPC_SET_REQUEST(SERVER_DEVICE_STATE_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
122 {
123     if (pBaseReq == nullptr) {
124         return ERR_DM_FAILED;
125     }
126 
127     std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::static_pointer_cast<IpcNotifyDeviceStateReq>(pBaseReq);
128     std::string pkgName = pReq->GetPkgName();
129     int32_t deviceState = pReq->GetDeviceState();
130     DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
131     DmDeviceBasicInfo deviceBasicInfo = pReq->GetDeviceBasicInfo();
132     if (!data.WriteString(pkgName)) {
133         LOGE("write pkgName failed");
134         return ERR_DM_IPC_WRITE_FAILED;
135     }
136     if (!data.WriteInt32(deviceState)) {
137         LOGE("write state failed");
138         return ERR_DM_IPC_WRITE_FAILED;
139     }
140     if (!EncodeDmDeviceInfo(deviceInfo, data)) {
141         LOGE("write dm device info failed");
142         return ERR_DM_IPC_WRITE_FAILED;
143     }
144     if (!data.WriteRawData(&deviceBasicInfo, sizeof(DmDeviceBasicInfo))) {
145         LOGE("write deviceBasicInfo failed");
146         return ERR_DM_IPC_WRITE_FAILED;
147     }
148     return DM_OK;
149 }
150 
ON_IPC_READ_RESPONSE(SERVER_DEVICE_STATE_NOTIFY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)151 ON_IPC_READ_RESPONSE(SERVER_DEVICE_STATE_NOTIFY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
152 {
153     if (pBaseRsp == nullptr) {
154         LOGE("pBaseRsp is null");
155         return ERR_DM_FAILED;
156     }
157     pBaseRsp->SetErrCode(reply.ReadInt32());
158     return DM_OK;
159 }
160 
ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)161 ON_IPC_SET_REQUEST(SERVER_DEVICE_FOUND, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
162 {
163     if (pBaseReq == nullptr) {
164         return ERR_DM_FAILED;
165     }
166 
167     std::shared_ptr<IpcNotifyDeviceFoundReq> pReq = std::static_pointer_cast<IpcNotifyDeviceFoundReq>(pBaseReq);
168     std::string pkgName = pReq->GetPkgName();
169     uint16_t subscribeId = pReq->GetSubscribeId();
170     DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
171     DmDeviceBasicInfo devBasicInfo = pReq->GetDeviceBasicInfo();
172     if (!data.WriteString(pkgName)) {
173         LOGE("write pkgName failed");
174         return ERR_DM_IPC_WRITE_FAILED;
175     }
176     if (!data.WriteInt16((int16_t)subscribeId)) {
177         LOGE("write subscribeId failed");
178         return ERR_DM_IPC_WRITE_FAILED;
179     }
180     if (!EncodeDmDeviceInfo(deviceInfo, data)) {
181         LOGE("write dm device info failed");
182         return ERR_DM_IPC_WRITE_FAILED;
183     }
184     if (!EncodeDmDeviceBasicInfo(devBasicInfo, data)) {
185         LOGE("write dm device basic info failed");
186         return ERR_DM_IPC_WRITE_FAILED;
187     }
188     return DM_OK;
189 }
190 
ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)191 ON_IPC_READ_RESPONSE(SERVER_DEVICE_FOUND, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
192 {
193     if (pBaseRsp == nullptr) {
194         LOGE("pBaseRsp is null");
195         return ERR_DM_FAILED;
196     }
197     pBaseRsp->SetErrCode(reply.ReadInt32());
198     return DM_OK;
199 }
200 
ON_IPC_SET_REQUEST(SERVER_DEVICE_DISCOVERY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)201 ON_IPC_SET_REQUEST(SERVER_DEVICE_DISCOVERY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
202 {
203     if (pBaseReq == nullptr) {
204         return ERR_DM_FAILED;
205     }
206 
207     std::shared_ptr<IpcNotifyDeviceDiscoveryReq> pReq = std::static_pointer_cast<IpcNotifyDeviceDiscoveryReq>(pBaseReq);
208     std::string pkgName = pReq->GetPkgName();
209     uint16_t subscribeId = pReq->GetSubscribeId();
210     DmDeviceBasicInfo deviceBasicInfo = pReq->GetDeviceBasicInfo();
211     if (!data.WriteString(pkgName)) {
212         LOGE("write pkgName failed");
213         return ERR_DM_IPC_WRITE_FAILED;
214     }
215     if (!data.WriteInt16((int16_t)subscribeId)) {
216         LOGE("write subscribeId failed");
217         return ERR_DM_IPC_WRITE_FAILED;
218     }
219     if (!data.WriteRawData(&deviceBasicInfo, sizeof(DmDeviceBasicInfo))) {
220         LOGE("write deviceBasicInfo failed");
221         return ERR_DM_IPC_WRITE_FAILED;
222     }
223     return DM_OK;
224 }
225 
ON_IPC_READ_RESPONSE(SERVER_DEVICE_DISCOVERY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)226 ON_IPC_READ_RESPONSE(SERVER_DEVICE_DISCOVERY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
227 {
228     if (pBaseRsp == nullptr) {
229         LOGE("pBaseRsp is null");
230         return ERR_DM_FAILED;
231     }
232     pBaseRsp->SetErrCode(reply.ReadInt32());
233     return DM_OK;
234 }
235 
ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)236 ON_IPC_SET_REQUEST(SERVER_DISCOVER_FINISH, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
237 {
238     if (pBaseReq == nullptr) {
239         return ERR_DM_FAILED;
240     }
241     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::static_pointer_cast<IpcNotifyDiscoverResultReq>(pBaseReq);
242     std::string pkgName = pReq->GetPkgName();
243     uint16_t subscribeId = pReq->GetSubscribeId();
244     int32_t result = pReq->GetResult();
245     if (!data.WriteString(pkgName)) {
246         LOGE("write pkgName failed");
247         return ERR_DM_IPC_WRITE_FAILED;
248     }
249     if (!data.WriteInt16((int16_t)subscribeId)) {
250         LOGE("write subscribeId failed");
251         return ERR_DM_IPC_WRITE_FAILED;
252     }
253     if (!data.WriteInt32(result)) {
254         LOGE("write result failed");
255         return ERR_DM_IPC_WRITE_FAILED;
256     }
257     return DM_OK;
258 }
259 
ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)260 ON_IPC_READ_RESPONSE(SERVER_DISCOVER_FINISH, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
261 {
262     if (pBaseRsp == nullptr) {
263         LOGE("pBaseRsp is null");
264         return ERR_DM_FAILED;
265     }
266     pBaseRsp->SetErrCode(reply.ReadInt32());
267     return DM_OK;
268 }
269 
ON_IPC_SET_REQUEST(SERVER_PUBLISH_FINISH, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)270 ON_IPC_SET_REQUEST(SERVER_PUBLISH_FINISH, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
271 {
272     if (pBaseReq == nullptr) {
273         return ERR_DM_FAILED;
274     }
275     std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::static_pointer_cast<IpcNotifyPublishResultReq>(pBaseReq);
276     std::string pkgName = pReq->GetPkgName();
277     int32_t publishId = pReq->GetPublishId();
278     int32_t result = pReq->GetResult();
279     if (!data.WriteString(pkgName)) {
280         LOGE("write pkgName failed");
281         return ERR_DM_IPC_WRITE_FAILED;
282     }
283     if (!data.WriteInt32(publishId)) {
284         LOGE("write publishId failed");
285         return ERR_DM_IPC_WRITE_FAILED;
286     }
287     if (!data.WriteInt32(result)) {
288         LOGE("write result failed");
289         return ERR_DM_IPC_WRITE_FAILED;
290     }
291     return DM_OK;
292 }
293 
ON_IPC_READ_RESPONSE(SERVER_PUBLISH_FINISH, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)294 ON_IPC_READ_RESPONSE(SERVER_PUBLISH_FINISH, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
295 {
296     if (pBaseRsp == nullptr) {
297         LOGE("pBaseRsp is null");
298         return ERR_DM_FAILED;
299     }
300     pBaseRsp->SetErrCode(reply.ReadInt32());
301     return DM_OK;
302 }
303 
ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)304 ON_IPC_SET_REQUEST(SERVER_AUTH_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
305 {
306     if (pBaseReq == nullptr) {
307         return ERR_DM_FAILED;
308     }
309     std::shared_ptr<IpcNotifyAuthResultReq> pReq = std::static_pointer_cast<IpcNotifyAuthResultReq>(pBaseReq);
310 
311     std::string pkgName = pReq->GetPkgName();
312     std::string deviceId = pReq->GetDeviceId();
313     std::string token = pReq->GetPinToken();
314     int32_t status = pReq->GetStatus();
315     int32_t reason = pReq->GetReason();
316     if (!data.WriteString(pkgName)) {
317         LOGE("write pkgName failed");
318         return ERR_DM_IPC_WRITE_FAILED;
319     }
320     if (!data.WriteString(deviceId)) {
321         LOGE("write deviceId failed");
322         return ERR_DM_IPC_WRITE_FAILED;
323     }
324     if (!data.WriteString(token)) {
325         LOGE("write token failed");
326         return ERR_DM_IPC_WRITE_FAILED;
327     }
328     if (!data.WriteInt32(status)) {
329         LOGE("write status failed");
330         return ERR_DM_IPC_WRITE_FAILED;
331     }
332     if (!data.WriteInt32(reason)) {
333         LOGE("write reason failed");
334         return ERR_DM_IPC_WRITE_FAILED;
335     }
336     return DM_OK;
337 }
338 
ON_IPC_READ_RESPONSE(SERVER_AUTH_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)339 ON_IPC_READ_RESPONSE(SERVER_AUTH_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
340 {
341     if (pBaseRsp == nullptr) {
342         LOGE("pBaseRsp is null");
343         return ERR_DM_FAILED;
344     }
345     pBaseRsp->SetErrCode(reply.ReadInt32());
346     return DM_OK;
347 }
348 
ON_IPC_SET_REQUEST(SERVER_DEVICE_FA_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)349 ON_IPC_SET_REQUEST(SERVER_DEVICE_FA_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
350 {
351     if (pBaseReq == nullptr) {
352         return ERR_DM_FAILED;
353     }
354 
355     std::shared_ptr<IpcNotifyDMFAResultReq> pReq = std::static_pointer_cast<IpcNotifyDMFAResultReq>(pBaseReq);
356 
357     std::string packagname = pReq->GetPkgName();
358     std::string paramJson = pReq->GetJsonParam();
359     if (!data.WriteString(packagname)) {
360         LOGE("write pkgName failed");
361         return ERR_DM_IPC_WRITE_FAILED;
362     }
363     if (!data.WriteString(paramJson)) {
364         LOGE("write paramJson failed");
365         return ERR_DM_IPC_WRITE_FAILED;
366     }
367     return DM_OK;
368 }
369 
ON_IPC_READ_RESPONSE(SERVER_DEVICE_FA_NOTIFY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)370 ON_IPC_READ_RESPONSE(SERVER_DEVICE_FA_NOTIFY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
371 {
372     if (pBaseRsp == nullptr) {
373         LOGE("pBaseRsp is null");
374         return ERR_DM_FAILED;
375     }
376     pBaseRsp->SetErrCode(reply.ReadInt32());
377     return DM_OK;
378 }
379 
ON_IPC_CMD(GET_TRUST_DEVICE_LIST, MessageParcel &data, MessageParcel &reply)380 ON_IPC_CMD(GET_TRUST_DEVICE_LIST, MessageParcel &data, MessageParcel &reply)
381 {
382     std::string pkgName = data.ReadString();
383     std::string extra = data.ReadString();
384     bool isRefresh = data.ReadBool();
385     DeviceManagerService::GetInstance().ShiftLNNGear(pkgName, pkgName, isRefresh, false);
386     std::vector<DmDeviceInfo> deviceList;
387     int32_t result = DeviceManagerService::GetInstance().GetTrustedDeviceList(pkgName, extra, deviceList);
388     if (!reply.WriteInt32((int32_t)deviceList.size())) {
389         LOGE("write device list size failed");
390         return ERR_DM_IPC_WRITE_FAILED;
391     }
392     for (const auto &devInfo : deviceList) {
393         if (!EncodeDmDeviceInfo(devInfo, reply)) {
394             LOGE("write dm device info failed");
395             return ERR_DM_IPC_WRITE_FAILED;
396         }
397     }
398     if (!reply.WriteInt32(result)) {
399         LOGE("write result failed");
400         return ERR_DM_IPC_WRITE_FAILED;
401     }
402     return DM_OK;
403 }
404 
ON_IPC_CMD(REGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &data, MessageParcel &reply)405 ON_IPC_CMD(REGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &data, MessageParcel &reply)
406 {
407     int32_t id = OHOS::HiviewDFX::XCollie::GetInstance().SetTimer("RegisterDeviceManagerListener", XCOLLIE_TIMEOUT_S,
408         nullptr, nullptr, OHOS::HiviewDFX::XCOLLIE_FLAG_LOG | OHOS::HiviewDFX::XCOLLIE_FLAG_RECOVERY);
409     std::string pkgName = data.ReadString();
410     sptr<IRemoteObject> listener = data.ReadRemoteObject();
411     if (listener == nullptr) {
412         LOGE("read remote object failed.");
413         OHOS::HiviewDFX::XCollie::GetInstance().CancelTimer(id);
414         return ERR_DM_POINT_NULL;
415     }
416     sptr<IpcServerClientProxy> callback(new IpcServerClientProxy(listener));
417     if (callback == nullptr) {
418         LOGE("create ipc server client proxy failed.");
419         OHOS::HiviewDFX::XCollie::GetInstance().CancelTimer(id);
420         return ERR_DM_POINT_NULL;
421     }
422     DeviceManagerService::GetInstance().RegisterCallerAppId(pkgName);
423     int32_t result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, callback);
424     if (!reply.WriteInt32(result)) {
425         LOGE("write result failed");
426         OHOS::HiviewDFX::XCollie::GetInstance().CancelTimer(id);
427         return ERR_DM_IPC_WRITE_FAILED;
428     }
429     OHOS::HiviewDFX::XCollie::GetInstance().CancelTimer(id);
430     return DM_OK;
431 }
432 
ON_IPC_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &data, MessageParcel &reply)433 ON_IPC_CMD(UNREGISTER_DEVICE_MANAGER_LISTENER, MessageParcel &data, MessageParcel &reply)
434 {
435     std::string pkgName = data.ReadString();
436     DeviceManagerService::GetInstance().UnRegisterCallerAppId(pkgName);
437     int32_t result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName);
438     if (!reply.WriteInt32(result)) {
439         LOGE("write result failed");
440         return ERR_DM_IPC_WRITE_FAILED;
441     }
442     return DM_OK;
443 }
444 
ON_IPC_CMD(START_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply)445 ON_IPC_CMD(START_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply)
446 {
447     std::string pkgName = data.ReadString();
448     std::string extra = data.ReadString();
449     DmSubscribeInfo *subscribeInfo =
450         static_cast<DmSubscribeInfo *>(const_cast<void *>(data.ReadRawData(sizeof(DmSubscribeInfo))));
451     int32_t result = ERR_DM_POINT_NULL;
452 
453     if (subscribeInfo != nullptr) {
454         result = DeviceManagerService::GetInstance().StartDeviceDiscovery(pkgName, *subscribeInfo, extra);
455     }
456     if (!reply.WriteInt32(result)) {
457         LOGE("write result failed");
458         return ERR_DM_IPC_WRITE_FAILED;
459     }
460     return DM_OK;
461 }
462 
ON_IPC_CMD(START_DEVICE_DISCOVERY, MessageParcel &data, MessageParcel &reply)463 ON_IPC_CMD(START_DEVICE_DISCOVERY, MessageParcel &data, MessageParcel &reply)
464 {
465     std::string pkgName = data.ReadString();
466     std::string filterOption = data.ReadString();
467     uint16_t subscribeId = data.ReadUint16();
468     int32_t result = DeviceManagerService::GetInstance().StartDeviceDiscovery(pkgName, subscribeId, filterOption);
469     if (!reply.WriteInt32(result)) {
470         LOGE("write result failed");
471         return ERR_DM_IPC_WRITE_FAILED;
472     }
473     return DM_OK;
474 }
475 
ON_IPC_CMD(STOP_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply)476 ON_IPC_CMD(STOP_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply)
477 {
478     std::string pkgName = data.ReadString();
479     uint16_t subscribeId = data.ReadUint16();
480     int32_t result = DeviceManagerService::GetInstance().StopDeviceDiscovery(pkgName, subscribeId);
481     if (!reply.WriteInt32(result)) {
482         LOGE("write result failed");
483         return ERR_DM_IPC_WRITE_FAILED;
484     }
485     return DM_OK;
486 }
487 
ON_IPC_CMD(PUBLISH_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply)488 ON_IPC_CMD(PUBLISH_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply)
489 {
490     std::string pkgName = data.ReadString();
491     DmPublishInfo *publishInfo =
492         static_cast<DmPublishInfo *>(const_cast<void *>(data.ReadRawData(sizeof(DmPublishInfo))));
493     int32_t result = ERR_DM_POINT_NULL;
494 
495     if (publishInfo != nullptr) {
496         result = DeviceManagerService::GetInstance().PublishDeviceDiscovery(pkgName, *publishInfo);
497     }
498     if (!reply.WriteInt32(result)) {
499         LOGE("write result failed");
500         return ERR_DM_IPC_WRITE_FAILED;
501     }
502     return DM_OK;
503 }
504 
ON_IPC_CMD(UNPUBLISH_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply)505 ON_IPC_CMD(UNPUBLISH_DEVICE_DISCOVER, MessageParcel &data, MessageParcel &reply)
506 {
507     std::string pkgName = data.ReadString();
508     int32_t publishId = data.ReadInt32();
509     int32_t result = DeviceManagerService::GetInstance().UnPublishDeviceDiscovery(pkgName, publishId);
510     if (!reply.WriteInt32(result)) {
511         LOGE("write result failed");
512         return ERR_DM_IPC_WRITE_FAILED;
513     }
514     return DM_OK;
515 }
516 
ON_IPC_CMD(AUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply)517 ON_IPC_CMD(AUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply)
518 {
519     std::string pkgName = data.ReadString();
520     std::string extra = data.ReadString();
521     std::string deviceId = data.ReadString();
522     int32_t authType = data.ReadInt32();
523 
524     int32_t result = DM_OK;
525     result = DeviceManagerService::GetInstance().AuthenticateDevice(pkgName, authType, deviceId, extra);
526     if (!reply.WriteInt32(result)) {
527         LOGE("write result failed");
528         return ERR_DM_IPC_WRITE_FAILED;
529     }
530     return DM_OK;
531 }
532 
ON_IPC_CMD(UNAUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply)533 ON_IPC_CMD(UNAUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply)
534 {
535     std::string pkgName = data.ReadString();
536     std::string deviceId = data.ReadString();
537     int32_t result = DeviceManagerService::GetInstance().UnAuthenticateDevice(pkgName, deviceId);
538     if (!reply.WriteInt32(result)) {
539         LOGE("write result failed");
540         return ERR_DM_IPC_WRITE_FAILED;
541     }
542     return DM_OK;
543 }
544 
ON_IPC_CMD(GET_DEVICE_INFO, MessageParcel &data, MessageParcel &reply)545 ON_IPC_CMD(GET_DEVICE_INFO, MessageParcel &data, MessageParcel &reply)
546 {
547     std::string networkId = data.ReadString();
548     DmDeviceInfo deviceInfo;
549     int32_t result = DeviceManagerService::GetInstance().GetDeviceInfo(networkId, deviceInfo);
550     if (!EncodeDmDeviceInfo(deviceInfo, reply)) {
551         LOGE("write dm device info failed");
552         return ERR_DM_IPC_WRITE_FAILED;
553     }
554     if (!reply.WriteInt32(result)) {
555         LOGE("write result failed");
556         return ERR_DM_IPC_WRITE_FAILED;
557     }
558     return DM_OK;
559 }
560 
ON_IPC_CMD(GET_LOCAL_DEVICE_INFO, MessageParcel &data, MessageParcel &reply)561 ON_IPC_CMD(GET_LOCAL_DEVICE_INFO, MessageParcel &data, MessageParcel &reply)
562 {
563     (void)data;
564     DmDeviceInfo localDeviceInfo;
565     int32_t result = DeviceManagerService::GetInstance().GetLocalDeviceInfo(localDeviceInfo);
566     if (!EncodeDmDeviceInfo(localDeviceInfo, reply)) {
567         LOGE("write dm device info failed");
568         return ERR_DM_IPC_WRITE_FAILED;
569     }
570     if (!reply.WriteInt32(result)) {
571         LOGE("write result failed");
572         return ERR_DM_IPC_WRITE_FAILED;
573     }
574     return DM_OK;
575 }
576 
ON_IPC_CMD(GET_UDID_BY_NETWORK, MessageParcel &data, MessageParcel &reply)577 ON_IPC_CMD(GET_UDID_BY_NETWORK, MessageParcel &data, MessageParcel &reply)
578 {
579     std::string pkgName = data.ReadString();
580     std::string netWorkId = data.ReadString();
581     std::string udid;
582     int32_t result = DeviceManagerService::GetInstance().GetUdidByNetworkId(pkgName, netWorkId, udid);
583 
584     if (!reply.WriteInt32(result)) {
585         LOGE("write result failed");
586         return ERR_DM_IPC_WRITE_FAILED;
587     }
588     if (!reply.WriteString(udid)) {
589         LOGE("write result failed");
590         return ERR_DM_IPC_WRITE_FAILED;
591     }
592     return DM_OK;
593 }
594 
ON_IPC_CMD(GET_UUID_BY_NETWORK, MessageParcel &data, MessageParcel &reply)595 ON_IPC_CMD(GET_UUID_BY_NETWORK, MessageParcel &data, MessageParcel &reply)
596 {
597     std::string pkgName = data.ReadString();
598     std::string netWorkId = data.ReadString();
599     std::string uuid;
600     int32_t result = DeviceManagerService::GetInstance().GetUuidByNetworkId(pkgName, netWorkId, uuid);
601 
602     if (!reply.WriteInt32(result)) {
603         LOGE("write result failed");
604         return ERR_DM_IPC_WRITE_FAILED;
605     }
606     if (!reply.WriteString(uuid)) {
607         LOGE("write result failed");
608         return ERR_DM_IPC_WRITE_FAILED;
609     }
610     return DM_OK;
611 }
612 
ON_IPC_CMD(SERVER_USER_AUTH_OPERATION, MessageParcel &data, MessageParcel &reply)613 ON_IPC_CMD(SERVER_USER_AUTH_OPERATION, MessageParcel &data, MessageParcel &reply)
614 {
615     std::string packageName = data.ReadString();
616     int32_t action = data.ReadInt32();
617     std::string params = data.ReadString();
618     int result = DeviceManagerService::GetInstance().SetUserOperation(packageName, action, params);
619     if (!reply.WriteInt32(result)) {
620         return ERR_DM_IPC_WRITE_FAILED;
621     }
622     return result;
623 }
624 
ON_IPC_CMD(REQUEST_CREDENTIAL, MessageParcel &data, MessageParcel &reply)625 ON_IPC_CMD(REQUEST_CREDENTIAL, MessageParcel &data, MessageParcel &reply)
626 {
627     std::string packageName = data.ReadString();
628     std::string reqParaStr = data.ReadString();
629     std::map<std::string, std::string> requestParam;
630     ParseMapFromJsonString(reqParaStr, requestParam);
631     std::string returnJsonStr;
632     int32_t ret = ERR_DM_FAILED;
633     if (requestParam[DM_CREDENTIAL_TYPE] == DM_TYPE_OH) {
634         ret = DeviceManagerService::GetInstance().RequestCredential(requestParam[DM_CREDENTIAL_REQJSONSTR],
635                                                                     returnJsonStr);
636     }
637     if (requestParam[DM_CREDENTIAL_TYPE] == DM_TYPE_MINE) {
638         ret = DeviceManagerService::GetInstance().MineRequestCredential(packageName, returnJsonStr);
639     }
640     if (!reply.WriteInt32(ret)) {
641         LOGE("write ret failed");
642         return ERR_DM_IPC_WRITE_FAILED;
643     }
644     if (ret == DM_OK && !returnJsonStr.empty()) {
645         if (!reply.WriteString(returnJsonStr)) {
646             LOGE("write returnJsonStr failed");
647             return ERR_DM_IPC_WRITE_FAILED;
648         }
649     }
650     return DM_OK;
651 }
652 
ON_IPC_CMD(IMPORT_CREDENTIAL, MessageParcel &data, MessageParcel &reply)653 ON_IPC_CMD(IMPORT_CREDENTIAL, MessageParcel &data, MessageParcel &reply)
654 {
655     std::string packageName = data.ReadString();
656     std::string reqParaStr = data.ReadString();
657     std::map<std::string, std::string> requestParam;
658     ParseMapFromJsonString(reqParaStr, requestParam);
659     std::string returnJsonStr;
660     std::map<std::string, std::string> outputResult;
661     int32_t ret = ERR_DM_FAILED;
662     if (requestParam[DM_CREDENTIAL_TYPE] == DM_TYPE_OH) {
663         ret = DeviceManagerService::GetInstance().ImportCredential(packageName, requestParam[DM_CREDENTIAL_REQJSONSTR]);
664         outputResult.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_OH);
665     }
666     if (requestParam[DM_CREDENTIAL_TYPE] == DM_TYPE_MINE) {
667         ret = DeviceManagerService::GetInstance().ImportCredential(packageName, requestParam[DM_CREDENTIAL_REQJSONSTR],
668                                                                    returnJsonStr);
669         outputResult.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_MINE);
670         outputResult.emplace(DM_CREDENTIAL_RETURNJSONSTR, returnJsonStr);
671     }
672     if (!reply.WriteInt32(ret)) {
673         LOGE("write ret failed");
674         return ERR_DM_IPC_WRITE_FAILED;
675     }
676     if (ret == DM_OK && !returnJsonStr.empty()) {
677         std::string outParaStr = ConvertMapToJsonString(outputResult);
678         if (!reply.WriteString(outParaStr)) {
679         LOGE("write returnJsonStr failed");
680         return ERR_DM_IPC_WRITE_FAILED;
681         }
682     }
683     return DM_OK;
684 }
685 
ON_IPC_CMD(DELETE_CREDENTIAL, MessageParcel &data, MessageParcel &reply)686 ON_IPC_CMD(DELETE_CREDENTIAL, MessageParcel &data, MessageParcel &reply)
687 {
688     std::string packageName = data.ReadString();
689     std::string reqParaStr = data.ReadString();
690     std::map<std::string, std::string> requestParam;
691     ParseMapFromJsonString(reqParaStr, requestParam);
692     std::map<std::string, std::string> outputResult;
693     std::string returnJsonStr;
694     int32_t ret = ERR_DM_FAILED;
695     if (requestParam[DM_CREDENTIAL_TYPE] == DM_TYPE_OH) {
696         ret = DeviceManagerService::GetInstance().DeleteCredential(packageName, requestParam[DM_CREDENTIAL_REQJSONSTR]);
697         outputResult.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_OH);
698     }
699     if (requestParam[DM_CREDENTIAL_TYPE] == DM_TYPE_MINE) {
700         ret = DeviceManagerService::GetInstance().DeleteCredential(packageName, requestParam[DM_CREDENTIAL_REQJSONSTR],
701                                                                    returnJsonStr);
702         outputResult.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_MINE);
703         outputResult.emplace(DM_CREDENTIAL_RETURNJSONSTR, returnJsonStr);
704     }
705     if (!reply.WriteInt32(ret)) {
706         LOGE("write ret failed");
707         return ERR_DM_IPC_WRITE_FAILED;
708     }
709     if (ret == DM_OK && !returnJsonStr.empty()) {
710         std::string outParaStr = ConvertMapToJsonString(outputResult);
711         if (!reply.WriteString(outParaStr)) {
712             LOGE("write returnJsonStr failed");
713             return ERR_DM_IPC_WRITE_FAILED;
714         }
715     }
716     return DM_OK;
717 }
718 
ON_IPC_CMD(SERVER_GET_DMFA_INFO, MessageParcel &data, MessageParcel &reply)719 ON_IPC_CMD(SERVER_GET_DMFA_INFO, MessageParcel &data, MessageParcel &reply)
720 {
721     std::string packageName = data.ReadString();
722     std::string reqJsonStr = data.ReadString();
723     std::string returnJsonStr;
724     int32_t ret = DeviceManagerService::GetInstance().CheckCredential(packageName, reqJsonStr, returnJsonStr);
725     if (!reply.WriteInt32(ret)) {
726         LOGE("write ret failed");
727         return ERR_DM_IPC_WRITE_FAILED;
728     }
729     if (ret == DM_OK && !returnJsonStr.empty()) {
730         if (!reply.WriteString(returnJsonStr)) {
731             LOGE("write returnJsonStr failed");
732             return ERR_DM_IPC_WRITE_FAILED;
733         }
734     }
735     return DM_OK;
736 }
737 
ON_IPC_CMD(REGISTER_CREDENTIAL_CALLBACK, MessageParcel &data, MessageParcel &reply)738 ON_IPC_CMD(REGISTER_CREDENTIAL_CALLBACK, MessageParcel &data, MessageParcel &reply)
739 {
740     std::string packageName = data.ReadString();
741     int result = DeviceManagerService::GetInstance().RegisterCredentialCallback(packageName);
742     if (!reply.WriteInt32(result)) {
743         LOGE("write result failed");
744         return ERR_DM_IPC_WRITE_FAILED;
745     }
746     return result;
747 }
748 
ON_IPC_CMD(UNREGISTER_CREDENTIAL_CALLBACK, MessageParcel &data, MessageParcel &reply)749 ON_IPC_CMD(UNREGISTER_CREDENTIAL_CALLBACK, MessageParcel &data, MessageParcel &reply)
750 {
751     std::string packageName = data.ReadString();
752     int result = DeviceManagerService::GetInstance().UnRegisterCredentialCallback(packageName);
753     if (!reply.WriteInt32(result)) {
754         LOGE("write result failed");
755         return ERR_DM_IPC_WRITE_FAILED;
756     }
757     return result;
758 }
759 
ON_IPC_CMD(NOTIFY_EVENT, MessageParcel &data, MessageParcel &reply)760 ON_IPC_CMD(NOTIFY_EVENT, MessageParcel &data, MessageParcel &reply)
761 {
762     std::string pkgName = data.ReadString();
763     int32_t eventId = data.ReadInt32();
764     std::string event = data.ReadString();
765     int32_t result = DeviceManagerService::GetInstance().NotifyEvent(pkgName, eventId, event);
766     if (!reply.WriteInt32(result)) {
767         LOGE("write result failed");
768         return ERR_DM_IPC_WRITE_FAILED;
769     }
770     return DM_OK;
771 }
772 
ON_IPC_SET_REQUEST(SERVER_CREDENTIAL_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)773 ON_IPC_SET_REQUEST(SERVER_CREDENTIAL_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
774 {
775     if (pBaseReq == nullptr) {
776         return ERR_DM_FAILED;
777     }
778     std::shared_ptr<IpcNotifyCredentialReq> pReq = std::static_pointer_cast<IpcNotifyCredentialReq>(pBaseReq);
779     std::string pkgName = pReq->GetPkgName();
780     int32_t action = pReq->GetCredentialAction();
781     std::string credentialResult = pReq->GetCredentialResult();
782     if (!data.WriteString(pkgName)) {
783         LOGE("write pkgName failed");
784         return ERR_DM_IPC_WRITE_FAILED;
785     }
786     if (!data.WriteInt32(action)) {
787         LOGE("write action failed");
788         return ERR_DM_IPC_WRITE_FAILED;
789     }
790     if (!data.WriteString(credentialResult)) {
791         LOGE("write credentialResult failed");
792         return ERR_DM_IPC_WRITE_FAILED;
793     }
794 
795     return DM_OK;
796 }
797 
ON_IPC_READ_RESPONSE(SERVER_CREDENTIAL_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)798 ON_IPC_READ_RESPONSE(SERVER_CREDENTIAL_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
799 {
800     if (pBaseRsp == nullptr) {
801         LOGE("pBaseRsp is null");
802         return ERR_DM_FAILED;
803     }
804     pBaseRsp->SetErrCode(reply.ReadInt32());
805     return DM_OK;
806 }
807 
ON_IPC_CMD(GET_ENCRYPTED_UUID_BY_NETWOEKID, MessageParcel &data, MessageParcel &reply)808 ON_IPC_CMD(GET_ENCRYPTED_UUID_BY_NETWOEKID, MessageParcel &data, MessageParcel &reply)
809 {
810     std::string pkgName = data.ReadString();
811     std::string networkId = data.ReadString();
812     std::string uuid;
813 
814     int32_t result = DeviceManagerService::GetInstance().GetEncryptedUuidByNetworkId(pkgName, networkId, uuid);
815     if (!reply.WriteInt32(result)) {
816         LOGE("write result failed");
817         return ERR_DM_IPC_WRITE_FAILED;
818     }
819     if (!reply.WriteString(uuid)) {
820         LOGE("write uuid failed");
821         return ERR_DM_IPC_WRITE_FAILED;
822     }
823     return DM_OK;
824 }
825 
ON_IPC_CMD(GENERATE_ENCRYPTED_UUID, MessageParcel &data, MessageParcel &reply)826 ON_IPC_CMD(GENERATE_ENCRYPTED_UUID, MessageParcel &data, MessageParcel &reply)
827 {
828     std::string pkgName = data.ReadString();
829     std::string uuid = data.ReadString();
830     std::string appId = data.ReadString();
831     std::string encryptedUuid;
832 
833     int32_t result = DeviceManagerService::GetInstance().GenerateEncryptedUuid(pkgName, uuid, appId, encryptedUuid);
834     if (!reply.WriteInt32(result)) {
835         LOGE("write result failed");
836         return ERR_DM_IPC_WRITE_FAILED;
837     }
838     if (!reply.WriteString(encryptedUuid)) {
839         LOGE("write encryptedUuid failed");
840         return ERR_DM_IPC_WRITE_FAILED;
841     }
842     return DM_OK;
843 }
844 
ON_IPC_CMD(BIND_DEVICE, MessageParcel &data, MessageParcel &reply)845 ON_IPC_CMD(BIND_DEVICE, MessageParcel &data, MessageParcel &reply)
846 {
847     std::string pkgName = data.ReadString();
848     std::string bindParam = data.ReadString();
849     std::string deviceId = data.ReadString();
850     int32_t bindType = data.ReadInt32();
851     int32_t result = DM_OK;
852     result = DeviceManagerService::GetInstance().BindDevice(pkgName, bindType, deviceId, bindParam);
853     if (!reply.WriteInt32(result)) {
854         LOGE("write result failed");
855         return ERR_DM_IPC_WRITE_FAILED;
856     }
857     return DM_OK;
858 }
859 
ON_IPC_CMD(UNBIND_DEVICE, MessageParcel &data, MessageParcel &reply)860 ON_IPC_CMD(UNBIND_DEVICE, MessageParcel &data, MessageParcel &reply)
861 {
862     std::string pkgName = data.ReadString();
863     std::string deviceId = data.ReadString();
864     int32_t result = DeviceManagerService::GetInstance().UnBindDevice(pkgName, deviceId);
865     if (!reply.WriteInt32(result)) {
866         LOGE("write result failed");
867         return ERR_DM_IPC_WRITE_FAILED;
868     }
869     return DM_OK;
870 }
871 
ON_IPC_CMD(GET_NETWORKTYPE_BY_NETWORK, MessageParcel &data, MessageParcel &reply)872 ON_IPC_CMD(GET_NETWORKTYPE_BY_NETWORK, MessageParcel &data, MessageParcel &reply)
873 {
874     std::string pkgName = data.ReadString();
875     std::string netWorkId = data.ReadString();
876     int32_t networkType = -1;
877     int32_t result = DeviceManagerService::GetInstance().GetNetworkTypeByNetworkId(pkgName, netWorkId, networkType);
878     if (!reply.WriteInt32(result)) {
879         LOGE("write result failed");
880         return ERR_DM_IPC_WRITE_FAILED;
881     }
882     if (!reply.WriteInt32(networkType)) {
883         LOGE("write result failed");
884         return ERR_DM_IPC_WRITE_FAILED;
885     }
886     return DM_OK;
887 }
888 
ON_IPC_CMD(REGISTER_UI_STATE_CALLBACK, MessageParcel &data, MessageParcel &reply)889 ON_IPC_CMD(REGISTER_UI_STATE_CALLBACK, MessageParcel &data, MessageParcel &reply)
890 {
891     std::string pkgName = data.ReadString();
892     int32_t result = DeviceManagerService::GetInstance().RegisterUiStateCallback(pkgName);
893     if (!reply.WriteInt32(result)) {
894         LOGE("write result failed");
895         return ERR_DM_IPC_WRITE_FAILED;
896     }
897     return DM_OK;
898 }
899 
ON_IPC_CMD(UNREGISTER_UI_STATE_CALLBACK, MessageParcel &data, MessageParcel &reply)900 ON_IPC_CMD(UNREGISTER_UI_STATE_CALLBACK, MessageParcel &data, MessageParcel &reply)
901 {
902     std::string pkgName = data.ReadString();
903     int32_t result = DeviceManagerService::GetInstance().UnRegisterUiStateCallback(pkgName);
904     if (!reply.WriteInt32(result)) {
905         LOGE("write result failed");
906         return ERR_DM_IPC_WRITE_FAILED;
907     }
908     return DM_OK;
909 }
910 
ON_IPC_CMD(IMPORT_AUTH_CODE, MessageParcel &data, MessageParcel &reply)911 ON_IPC_CMD(IMPORT_AUTH_CODE, MessageParcel &data, MessageParcel &reply)
912 {
913     std::string pkgName = data.ReadString();
914     std::string authCode = data.ReadString();
915     int32_t result = DeviceManagerService::GetInstance().ImportAuthCode(pkgName, authCode);
916     if (!reply.WriteInt32(result)) {
917         LOGE("write result failed");
918         return ERR_DM_IPC_WRITE_FAILED;
919     }
920     return DM_OK;
921 }
922 
ON_IPC_CMD(EXPORT_AUTH_CODE, MessageParcel &data, MessageParcel &reply)923 ON_IPC_CMD(EXPORT_AUTH_CODE, MessageParcel &data, MessageParcel &reply)
924 {
925     std::string authCode = "";
926     int32_t result = DeviceManagerService::GetInstance().ExportAuthCode(authCode);
927     if (!reply.WriteString(authCode)) {
928         LOGE("write result failed");
929         return ERR_DM_IPC_WRITE_FAILED;
930     }
931     if (!reply.WriteInt32(result)) {
932         LOGE("write result failed");
933         return ERR_DM_IPC_WRITE_FAILED;
934     }
935     return DM_OK;
936 }
937 
ON_IPC_CMD(REGISTER_DISCOVERY_CALLBACK, MessageParcel &data, MessageParcel &reply)938 ON_IPC_CMD(REGISTER_DISCOVERY_CALLBACK, MessageParcel &data, MessageParcel &reply)
939 {
940     std::string pkgName = data.ReadString();
941     std::string discParaStr = data.ReadString();
942     std::string filterOpStr = data.ReadString();
943     std::map<std::string, std::string> discoverParam;
944     ParseMapFromJsonString(discParaStr, discoverParam);
945     std::map<std::string, std::string> filterOptions;
946     ParseMapFromJsonString(filterOpStr, filterOptions);
947     int32_t result = DeviceManagerService::GetInstance().EnableDiscoveryListener(pkgName, discoverParam, filterOptions);
948     if (!reply.WriteInt32(result)) {
949         LOGE("write result failed");
950         return ERR_DM_IPC_WRITE_FAILED;
951     }
952     return DM_OK;
953 }
954 
ON_IPC_CMD(UNREGISTER_DISCOVERY_CALLBACK, MessageParcel &data, MessageParcel &reply)955 ON_IPC_CMD(UNREGISTER_DISCOVERY_CALLBACK, MessageParcel &data, MessageParcel &reply)
956 {
957     std::string pkgName = data.ReadString();
958     std::string extraParaStr = data.ReadString();
959     std::map<std::string, std::string> extraParam;
960     ParseMapFromJsonString(extraParaStr, extraParam);
961     int32_t result = DeviceManagerService::GetInstance().DisableDiscoveryListener(pkgName, extraParam);
962     if (!reply.WriteInt32(result)) {
963         LOGE("write result failed");
964         return ERR_DM_IPC_WRITE_FAILED;
965     }
966     return DM_OK;
967 }
968 
ON_IPC_CMD(START_DISCOVERING, MessageParcel &data, MessageParcel &reply)969 ON_IPC_CMD(START_DISCOVERING, MessageParcel &data, MessageParcel &reply)
970 {
971     std::string pkgName = data.ReadString();
972     std::string discParaStr = data.ReadString();
973     std::string filterOpStr = data.ReadString();
974     std::map<std::string, std::string> discoverParam;
975     ParseMapFromJsonString(discParaStr, discoverParam);
976     std::map<std::string, std::string> filterOptions;
977     ParseMapFromJsonString(filterOpStr, filterOptions);
978     int32_t result = DeviceManagerService::GetInstance().StartDiscovering(pkgName, discoverParam, filterOptions);
979     if (!reply.WriteInt32(result)) {
980         LOGE("write result failed");
981         return ERR_DM_IPC_WRITE_FAILED;
982     }
983     return DM_OK;
984 }
985 
ON_IPC_CMD(STOP_DISCOVERING, MessageParcel &data, MessageParcel &reply)986 ON_IPC_CMD(STOP_DISCOVERING, MessageParcel &data, MessageParcel &reply)
987 {
988     std::string pkgName = data.ReadString();
989     std::string discParaStr = data.ReadString();
990     std::map<std::string, std::string> discoverParam;
991     ParseMapFromJsonString(discParaStr, discoverParam);
992     int32_t result = DeviceManagerService::GetInstance().StopDiscovering(pkgName, discoverParam);
993     if (!reply.WriteInt32(result)) {
994         LOGE("write result failed");
995         return ERR_DM_IPC_WRITE_FAILED;
996     }
997     return DM_OK;
998 }
999 
ON_IPC_CMD(START_ADVERTISING, MessageParcel &data, MessageParcel &reply)1000 ON_IPC_CMD(START_ADVERTISING, MessageParcel &data, MessageParcel &reply)
1001 {
1002     std::string pkgName = data.ReadString();
1003     std::string adverParaStr = data.ReadString();
1004     std::map<std::string, std::string> advertiseParam;
1005     ParseMapFromJsonString(adverParaStr, advertiseParam);
1006     int32_t result = DeviceManagerService::GetInstance().StartAdvertising(pkgName, advertiseParam);
1007     if (!reply.WriteInt32(result)) {
1008         LOGE("write result failed");
1009         return ERR_DM_IPC_WRITE_FAILED;
1010     }
1011     return DM_OK;
1012 }
1013 
ON_IPC_CMD(STOP_ADVERTISING, MessageParcel &data, MessageParcel &reply)1014 ON_IPC_CMD(STOP_ADVERTISING, MessageParcel &data, MessageParcel &reply)
1015 {
1016     std::string pkgName = data.ReadString();
1017     std::string adverParaStr = data.ReadString();
1018     std::map<std::string, std::string> advertiseParam;
1019     ParseMapFromJsonString(adverParaStr, advertiseParam);
1020     int32_t result = DeviceManagerService::GetInstance().StopAdvertising(pkgName, advertiseParam);
1021     if (!reply.WriteInt32(result)) {
1022         LOGE("write result failed");
1023         return ERR_DM_IPC_WRITE_FAILED;
1024     }
1025     return DM_OK;
1026 }
1027 
ON_IPC_CMD(BIND_TARGET, MessageParcel &data, MessageParcel &reply)1028 ON_IPC_CMD(BIND_TARGET, MessageParcel &data, MessageParcel &reply)
1029 {
1030     std::string pkgName = data.ReadString();
1031     PeerTargetId targetId;
1032     DecodePeerTargetId(data, targetId);
1033     std::string bindParamStr = data.ReadString();
1034     std::map<std::string, std::string> bindParam;
1035     ParseMapFromJsonString(bindParamStr, bindParam);
1036     int32_t result = DeviceManagerService::GetInstance().BindTarget(pkgName, targetId, bindParam);
1037     if (!reply.WriteInt32(result)) {
1038         LOGE("write result failed");
1039         return ERR_DM_IPC_WRITE_FAILED;
1040     }
1041     return DM_OK;
1042 }
1043 
ON_IPC_CMD(UNBIND_TARGET, MessageParcel &data, MessageParcel &reply)1044 ON_IPC_CMD(UNBIND_TARGET, MessageParcel &data, MessageParcel &reply)
1045 {
1046     std::string pkgName = data.ReadString();
1047     PeerTargetId targetId;
1048     DecodePeerTargetId(data, targetId);
1049     std::string unbindParamStr = data.ReadString();
1050     std::map<std::string, std::string> unbindParam;
1051     ParseMapFromJsonString(unbindParamStr, unbindParam);
1052     int32_t result = DeviceManagerService::GetInstance().UnbindTarget(pkgName, targetId, unbindParam);
1053     if (!reply.WriteInt32(result)) {
1054         LOGE("write result failed");
1055         return ERR_DM_IPC_WRITE_FAILED;
1056     }
1057     return DM_OK;
1058 }
1059 
ON_IPC_SET_REQUEST(BIND_TARGET_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)1060 ON_IPC_SET_REQUEST(BIND_TARGET_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
1061 {
1062     if (pBaseReq == nullptr) {
1063         return ERR_DM_FAILED;
1064     }
1065     std::shared_ptr<IpcNotifyBindResultReq> pReq = std::static_pointer_cast<IpcNotifyBindResultReq>(pBaseReq);
1066     std::string pkgName = pReq->GetPkgName();
1067     PeerTargetId targetId = pReq->GetPeerTargetId();
1068     int32_t result = pReq->GetResult();
1069     int32_t status = pReq->GetStatus();
1070     std::string content = pReq->GetContent();
1071 
1072     if (!data.WriteString(pkgName)) {
1073         LOGE("write bind pkgName failed");
1074         return ERR_DM_IPC_WRITE_FAILED;
1075     }
1076     if (!EncodePeerTargetId(targetId, data)) {
1077         LOGE("write bind peer target id failed");
1078         return ERR_DM_IPC_WRITE_FAILED;
1079     }
1080     if (!data.WriteInt32(result)) {
1081         LOGE("write bind result code failed");
1082         return ERR_DM_IPC_WRITE_FAILED;
1083     }
1084     if (!data.WriteInt32(status)) {
1085         LOGE("write bind result status failed");
1086         return ERR_DM_IPC_WRITE_FAILED;
1087     }
1088     if (!data.WriteString(content)) {
1089         LOGE("write bind result content failed");
1090         return ERR_DM_IPC_WRITE_FAILED;
1091     }
1092     return DM_OK;
1093 }
1094 
ON_IPC_READ_RESPONSE(BIND_TARGET_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)1095 ON_IPC_READ_RESPONSE(BIND_TARGET_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
1096 {
1097     if (pBaseRsp == nullptr) {
1098         LOGE("pBaseRsp is null");
1099         return ERR_DM_FAILED;
1100     }
1101     pBaseRsp->SetErrCode(reply.ReadInt32());
1102     return DM_OK;
1103 }
1104 
ON_IPC_SET_REQUEST(UNBIND_TARGET_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)1105 ON_IPC_SET_REQUEST(UNBIND_TARGET_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
1106 {
1107     if (pBaseReq == nullptr) {
1108         return ERR_DM_FAILED;
1109     }
1110     std::shared_ptr<IpcNotifyBindResultReq> pReq = std::static_pointer_cast<IpcNotifyBindResultReq>(pBaseReq);
1111     std::string pkgName = pReq->GetPkgName();
1112     PeerTargetId targetId = pReq->GetPeerTargetId();
1113     int32_t result = pReq->GetResult();
1114     std::string content = pReq->GetContent();
1115 
1116     if (!data.WriteString(pkgName)) {
1117         LOGE("write unbind pkgName failed");
1118         return ERR_DM_IPC_WRITE_FAILED;
1119     }
1120     if (!EncodePeerTargetId(targetId, data)) {
1121         LOGE("write unbind peer target id failed");
1122         return ERR_DM_IPC_WRITE_FAILED;
1123     }
1124     if (!data.WriteInt32(result)) {
1125         LOGE("write unbind result code failed");
1126         return ERR_DM_IPC_WRITE_FAILED;
1127     }
1128     if (!data.WriteString(content)) {
1129         LOGE("write unbind result content failed");
1130         return ERR_DM_IPC_WRITE_FAILED;
1131     }
1132     return DM_OK;
1133 }
1134 
ON_IPC_READ_RESPONSE(UNBIND_TARGET_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)1135 ON_IPC_READ_RESPONSE(UNBIND_TARGET_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
1136 {
1137     if (pBaseRsp == nullptr) {
1138         LOGE("pBaseRsp is null");
1139         return ERR_DM_FAILED;
1140     }
1141     pBaseRsp->SetErrCode(reply.ReadInt32());
1142     return DM_OK;
1143 }
1144 
ON_IPC_CMD(REGISTER_PIN_HOLDER_CALLBACK, MessageParcel &data, MessageParcel &reply)1145 ON_IPC_CMD(REGISTER_PIN_HOLDER_CALLBACK, MessageParcel &data, MessageParcel &reply)
1146 {
1147     std::string pkgName = data.ReadString();
1148     int32_t result = DeviceManagerService::GetInstance().RegisterPinHolderCallback(pkgName);
1149     if (!reply.WriteInt32(result)) {
1150         LOGE("write result failed");
1151         return ERR_DM_IPC_WRITE_FAILED;
1152     }
1153     return DM_OK;
1154 }
1155 
ON_IPC_CMD(CREATE_PIN_HOLDER, MessageParcel &data, MessageParcel &reply)1156 ON_IPC_CMD(CREATE_PIN_HOLDER, MessageParcel &data, MessageParcel &reply)
1157 {
1158     std::string pkgName = data.ReadString();
1159     PeerTargetId targetId;
1160     DecodePeerTargetId(data, targetId);
1161     std::string payload = data.ReadString();
1162     DmPinType pinType = static_cast<DmPinType>(data.ReadInt32());
1163     int32_t result = DeviceManagerService::GetInstance().CreatePinHolder(pkgName, targetId, pinType, payload);
1164     if (!reply.WriteInt32(result)) {
1165         LOGE("write result failed");
1166         return ERR_DM_IPC_WRITE_FAILED;
1167     }
1168     return DM_OK;
1169 }
1170 
ON_IPC_CMD(DESTROY_PIN_HOLDER, MessageParcel &data, MessageParcel &reply)1171 ON_IPC_CMD(DESTROY_PIN_HOLDER, MessageParcel &data, MessageParcel &reply)
1172 {
1173     std::string pkgName = data.ReadString();
1174     PeerTargetId targetId;
1175     DecodePeerTargetId(data, targetId);
1176     DmPinType pinType = static_cast<DmPinType>(data.ReadInt32());
1177     std::string payload = data.ReadString();
1178     int32_t result = DeviceManagerService::GetInstance().DestroyPinHolder(pkgName, targetId, pinType, payload);
1179     if (!reply.WriteInt32(result)) {
1180         LOGE("write result failed");
1181         return ERR_DM_IPC_WRITE_FAILED;
1182     }
1183     return DM_OK;
1184 }
1185 
ON_IPC_SET_REQUEST(SERVER_CREATE_PIN_HOLDER, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)1186 ON_IPC_SET_REQUEST(SERVER_CREATE_PIN_HOLDER, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
1187 {
1188     if (pBaseReq == nullptr) {
1189         return ERR_DM_FAILED;
1190     }
1191     std::shared_ptr<IpcCreatePinHolderReq> pReq = std::static_pointer_cast<IpcCreatePinHolderReq>(pBaseReq);
1192     std::string pkgName = pReq->GetPkgName();
1193     std::string deviceId = pReq->GetDeviceId();
1194     int32_t pinType = pReq->GetPinType();
1195     std::string payload = pReq->GetPayload();
1196 
1197     if (!data.WriteString(pkgName)) {
1198         LOGE("write pkgName failed");
1199         return ERR_DM_IPC_WRITE_FAILED;
1200     }
1201     if (!data.WriteString(deviceId)) {
1202         LOGE("write deviceId failed");
1203         return ERR_DM_IPC_WRITE_FAILED;
1204     }
1205     if (!data.WriteInt32(pinType)) {
1206         LOGE("write pinType failed");
1207         return ERR_DM_IPC_WRITE_FAILED;
1208     }
1209     if (!data.WriteString(payload)) {
1210         LOGE("write payload failed");
1211         return ERR_DM_IPC_WRITE_FAILED;
1212     }
1213     return DM_OK;
1214 }
1215 
ON_IPC_READ_RESPONSE(SERVER_CREATE_PIN_HOLDER, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)1216 ON_IPC_READ_RESPONSE(SERVER_CREATE_PIN_HOLDER, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
1217 {
1218     if (pBaseRsp == nullptr) {
1219         LOGE("pBaseRsp is null");
1220         return ERR_DM_FAILED;
1221     }
1222     pBaseRsp->SetErrCode(reply.ReadInt32());
1223     return DM_OK;
1224 }
1225 
ON_IPC_SET_REQUEST(SERVER_DESTROY_PIN_HOLDER, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)1226 ON_IPC_SET_REQUEST(SERVER_DESTROY_PIN_HOLDER, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
1227 {
1228     if (pBaseReq == nullptr) {
1229         return ERR_DM_FAILED;
1230     }
1231     std::shared_ptr<IpcDestroyPinHolderReq> pReq = std::static_pointer_cast<IpcDestroyPinHolderReq>(pBaseReq);
1232     std::string pkgName = pReq->GetPkgName();
1233     int32_t pinType = pReq->GetPinType();
1234     std::string payload = pReq->GetPayload();
1235 
1236     if (!data.WriteString(pkgName)) {
1237         LOGE("write pkgName failed");
1238         return ERR_DM_IPC_WRITE_FAILED;
1239     }
1240     if (!data.WriteInt32(pinType)) {
1241         LOGE("write pinType failed");
1242         return ERR_DM_IPC_WRITE_FAILED;
1243     }
1244     if (!data.WriteString(payload)) {
1245         LOGE("write payload failed");
1246         return ERR_DM_IPC_WRITE_FAILED;
1247     }
1248     return DM_OK;
1249 }
1250 
ON_IPC_READ_RESPONSE(SERVER_DESTROY_PIN_HOLDER, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)1251 ON_IPC_READ_RESPONSE(SERVER_DESTROY_PIN_HOLDER, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
1252 {
1253     if (pBaseRsp == nullptr) {
1254         LOGE("pBaseRsp is null");
1255         return ERR_DM_FAILED;
1256     }
1257     pBaseRsp->SetErrCode(reply.ReadInt32());
1258     return DM_OK;
1259 }
1260 
ON_IPC_SET_REQUEST(SERVER_CREATE_PIN_HOLDER_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)1261 ON_IPC_SET_REQUEST(SERVER_CREATE_PIN_HOLDER_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
1262 {
1263     if (pBaseReq == nullptr) {
1264         return ERR_DM_FAILED;
1265     }
1266     std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::static_pointer_cast<IpcNotifyPublishResultReq>(pBaseReq);
1267     std::string pkgName = pReq->GetPkgName();
1268     int32_t result = pReq->GetResult();
1269 
1270     if (!data.WriteString(pkgName)) {
1271         LOGE("write pkgName failed");
1272         return ERR_DM_IPC_WRITE_FAILED;
1273     }
1274     if (!data.WriteInt32(result)) {
1275         LOGE("write result failed");
1276         return ERR_DM_IPC_WRITE_FAILED;
1277     }
1278     return DM_OK;
1279 }
1280 
ON_IPC_READ_RESPONSE(SERVER_CREATE_PIN_HOLDER_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)1281 ON_IPC_READ_RESPONSE(SERVER_CREATE_PIN_HOLDER_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
1282 {
1283     if (pBaseRsp == nullptr) {
1284         LOGE("pBaseRsp is null");
1285         return ERR_DM_FAILED;
1286     }
1287     pBaseRsp->SetErrCode(reply.ReadInt32());
1288     return DM_OK;
1289 }
1290 
ON_IPC_SET_REQUEST(SERVER_DESTROY_PIN_HOLDER_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)1291 ON_IPC_SET_REQUEST(SERVER_DESTROY_PIN_HOLDER_RESULT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
1292 {
1293     if (pBaseReq == nullptr) {
1294         return ERR_DM_FAILED;
1295     }
1296     std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::static_pointer_cast<IpcNotifyPublishResultReq>(pBaseReq);
1297     std::string pkgName = pReq->GetPkgName();
1298     int32_t result = pReq->GetResult();
1299 
1300     if (!data.WriteString(pkgName)) {
1301         LOGE("write pkgName failed");
1302         return ERR_DM_IPC_WRITE_FAILED;
1303     }
1304     if (!data.WriteInt32(result)) {
1305         LOGE("write result failed");
1306         return ERR_DM_IPC_WRITE_FAILED;
1307     }
1308     return DM_OK;
1309 }
1310 
ON_IPC_READ_RESPONSE(SERVER_DESTROY_PIN_HOLDER_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)1311 ON_IPC_READ_RESPONSE(SERVER_DESTROY_PIN_HOLDER_RESULT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
1312 {
1313     if (pBaseRsp == nullptr) {
1314         LOGE("pBaseRsp is null");
1315         return ERR_DM_FAILED;
1316     }
1317     pBaseRsp->SetErrCode(reply.ReadInt32());
1318     return DM_OK;
1319 }
1320 
ON_IPC_CMD(DP_ACL_ADD, MessageParcel &data, MessageParcel &reply)1321 ON_IPC_CMD(DP_ACL_ADD, MessageParcel &data, MessageParcel &reply)
1322 {
1323     std::string udid = data.ReadString();
1324     int32_t result = DeviceManagerService::GetInstance().DpAclAdd(udid);
1325     if (!reply.WriteInt32(result)) {
1326         LOGE("write result failed");
1327         return ERR_DM_IPC_WRITE_FAILED;
1328     }
1329     return DM_OK;
1330 }
1331 
ON_IPC_CMD(GET_SECURITY_LEVEL, MessageParcel &data, MessageParcel &reply)1332 ON_IPC_CMD(GET_SECURITY_LEVEL, MessageParcel &data, MessageParcel &reply)
1333 {
1334     std::string pkgName = data.ReadString();
1335     std::string networkId = data.ReadString();
1336     int32_t securityLevel = -1;
1337     int32_t result = DeviceManagerService::GetInstance().GetDeviceSecurityLevel(pkgName, networkId, securityLevel);
1338     if (!reply.WriteInt32(result)) {
1339         return ERR_DM_IPC_WRITE_FAILED;
1340     }
1341     if (!reply.WriteInt32(securityLevel)) {
1342         return ERR_DM_IPC_WRITE_FAILED;
1343     }
1344     return DM_OK;
1345 }
1346 
ON_IPC_SET_REQUEST(SERVER_ON_PIN_HOLDER_EVENT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)1347 ON_IPC_SET_REQUEST(SERVER_ON_PIN_HOLDER_EVENT, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
1348 {
1349     if (pBaseReq == nullptr) {
1350         return ERR_DM_FAILED;
1351     }
1352     std::shared_ptr<IpcNotifyPinHolderEventReq> pReq = std::static_pointer_cast<IpcNotifyPinHolderEventReq>(pBaseReq);
1353     std::string pkgName = pReq->GetPkgName();
1354     int32_t pinHolderEvent = pReq->GetPinHolderEvent();
1355     int32_t result = pReq->GetResult();
1356     std::string content = pReq->GetContent();
1357 
1358     if (!data.WriteString(pkgName)) {
1359         LOGE("write pkgName failed");
1360         return ERR_DM_IPC_WRITE_FAILED;
1361     }
1362     if (!data.WriteInt32(result)) {
1363         LOGE("write result failed");
1364         return ERR_DM_IPC_WRITE_FAILED;
1365     }
1366     if (!data.WriteInt32(pinHolderEvent)) {
1367         LOGE("write pinHolderEvent failed");
1368         return ERR_DM_IPC_WRITE_FAILED;
1369     }
1370     if (!data.WriteString(content)) {
1371         LOGE("write content failed");
1372         return ERR_DM_IPC_WRITE_FAILED;
1373     }
1374     return DM_OK;
1375 }
1376 
ON_IPC_READ_RESPONSE(SERVER_ON_PIN_HOLDER_EVENT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)1377 ON_IPC_READ_RESPONSE(SERVER_ON_PIN_HOLDER_EVENT, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
1378 {
1379     if (pBaseRsp == nullptr) {
1380         LOGE("pBaseRsp is null");
1381         return ERR_DM_FAILED;
1382     }
1383     pBaseRsp->SetErrCode(reply.ReadInt32());
1384     return DM_OK;
1385 }
1386 
ON_IPC_CMD(IS_SAME_ACCOUNT, MessageParcel &data, MessageParcel &reply)1387 ON_IPC_CMD(IS_SAME_ACCOUNT, MessageParcel &data, MessageParcel &reply)
1388 {
1389     std::string netWorkId = data.ReadString();
1390     int32_t result = DeviceManagerService::GetInstance().IsSameAccount(netWorkId);
1391     if (!reply.WriteInt32(result)) {
1392         LOGE("write result failed.");
1393         return ERR_DM_IPC_WRITE_FAILED;
1394     }
1395     return DM_OK;
1396 }
1397 
ON_IPC_CMD(CHECK_API_PERMISSION, MessageParcel &data, MessageParcel &reply)1398 ON_IPC_CMD(CHECK_API_PERMISSION, MessageParcel &data, MessageParcel &reply)
1399 {
1400     int32_t permissionLevel = data.ReadInt32();
1401     int32_t result = DeviceManagerService::GetInstance().CheckApiPermission(permissionLevel);
1402     if (!reply.WriteInt32(result)) {
1403         LOGE("write result failed");
1404         return ERR_DM_IPC_WRITE_FAILED;
1405     }
1406     return DM_OK;
1407 }
1408 
ON_IPC_CMD(CHECK_ACCESS_CONTROL, MessageParcel &data, MessageParcel &reply)1409 ON_IPC_CMD(CHECK_ACCESS_CONTROL, MessageParcel &data, MessageParcel &reply)
1410 {
1411     DmAccessCaller caller;
1412     DmAccessCallee callee;
1413     DecodeDmAccessCaller(data, caller);
1414     DecodeDmAccessCallee(data, callee);
1415     int32_t result = DeviceManagerService::GetInstance().CheckAccessControl(caller, callee);
1416     if (!reply.WriteInt32(result)) {
1417         LOGE("write result failed.");
1418         return ERR_DM_IPC_WRITE_FAILED;
1419     }
1420     return DM_OK;
1421 }
1422 
ON_IPC_CMD(CHECK_SAME_ACCOUNT, MessageParcel &data, MessageParcel &reply)1423 ON_IPC_CMD(CHECK_SAME_ACCOUNT, MessageParcel &data, MessageParcel &reply)
1424 {
1425     DmAccessCaller caller;
1426     DmAccessCallee callee;
1427     DecodeDmAccessCaller(data, caller);
1428     DecodeDmAccessCallee(data, callee);
1429     int32_t result = DeviceManagerService::GetInstance().CheckIsSameAccount(caller, callee);
1430     if (!reply.WriteInt32(result)) {
1431         LOGE("write result failed.");
1432         return ERR_DM_IPC_WRITE_FAILED;
1433     }
1434     return DM_OK;
1435 }
1436 
1437 
ON_IPC_CMD(SHIFT_LNN_GEAR, MessageParcel &data, MessageParcel &reply)1438 ON_IPC_CMD(SHIFT_LNN_GEAR, MessageParcel &data, MessageParcel &reply)
1439 {
1440     std::string pkgName = data.ReadString();
1441     int32_t result = DeviceManagerService::GetInstance().ShiftLNNGear(pkgName, pkgName, true, true);
1442     if (!reply.WriteInt32(result)) {
1443         LOGE("write result failed");
1444         return ERR_DM_IPC_WRITE_FAILED;
1445     }
1446     return DM_OK;
1447 }
1448 
ON_IPC_CMD(SET_DN_POLICY, MessageParcel &data, MessageParcel &reply)1449 ON_IPC_CMD(SET_DN_POLICY, MessageParcel &data, MessageParcel &reply)
1450 {
1451     std::string pkgName = data.ReadString();
1452     std::string policyStr = data.ReadString();
1453     std::map<std::string, std::string> policy;
1454     ParseMapFromJsonString(policyStr, policy);
1455     int32_t result = DeviceManagerService::GetInstance().SetDnPolicy(pkgName, policy);
1456     if (!reply.WriteInt32(result)) {
1457         LOGE("write result failed");
1458         return ERR_DM_IPC_WRITE_FAILED;
1459     }
1460     return DM_OK;
1461 }
1462 
ON_IPC_CMD(STOP_AUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply)1463 ON_IPC_CMD(STOP_AUTHENTICATE_DEVICE, MessageParcel &data, MessageParcel &reply)
1464 {
1465     std::string pkgName = data.ReadString();
1466     int32_t result = DeviceManagerService::GetInstance().StopAuthenticateDevice(pkgName);
1467     if (!reply.WriteInt32(result)) {
1468         LOGE("write result failed");
1469         return ERR_DM_IPC_WRITE_FAILED;
1470     }
1471     return DM_OK;
1472 }
1473 
ON_IPC_SET_REQUEST(REMOTE_DEVICE_TRUST_CHANGE, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)1474 ON_IPC_SET_REQUEST(REMOTE_DEVICE_TRUST_CHANGE, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
1475 {
1476     if (pBaseReq == nullptr) {
1477         return ERR_DM_FAILED;
1478     }
1479     std::shared_ptr<IpcNotifyDevTrustChangeReq> pReq = std::static_pointer_cast<IpcNotifyDevTrustChangeReq>(pBaseReq);
1480     std::string pkgName = pReq->GetPkgName();
1481     std::string udid = pReq->GetUdid();
1482     std::string uuid = pReq->GetUuid();
1483     int32_t authForm = pReq->GetAuthForm();
1484     if (!data.WriteString(pkgName)) {
1485         LOGE("write pkgName failed");
1486         return ERR_DM_IPC_WRITE_FAILED;
1487     }
1488     if (!data.WriteString(udid)) {
1489         LOGE("write udid failed");
1490         return ERR_DM_IPC_WRITE_FAILED;
1491     }
1492     if (!data.WriteString(uuid)) {
1493         LOGE("write uuid code failed");
1494         return ERR_DM_IPC_WRITE_FAILED;
1495     }
1496     if (!data.WriteInt32(authForm)) {
1497         LOGE("write authForm code failed");
1498         return ERR_DM_IPC_WRITE_FAILED;
1499     }
1500     return DM_OK;
1501 }
1502 
ON_IPC_READ_RESPONSE(REMOTE_DEVICE_TRUST_CHANGE, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)1503 ON_IPC_READ_RESPONSE(REMOTE_DEVICE_TRUST_CHANGE, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
1504 {
1505     if (pBaseRsp == nullptr) {
1506         LOGE("pBaseRsp is null");
1507         return ERR_DM_FAILED;
1508     }
1509     pBaseRsp->SetErrCode(reply.ReadInt32());
1510     return DM_OK;
1511 }
1512 
ON_IPC_SET_REQUEST(SERVER_DEVICE_SCREEN_STATE_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)1513 ON_IPC_SET_REQUEST(SERVER_DEVICE_SCREEN_STATE_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
1514 {
1515     if (pBaseReq == nullptr) {
1516         return ERR_DM_FAILED;
1517     }
1518     std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::static_pointer_cast<IpcNotifyDeviceStateReq>(pBaseReq);
1519     std::string pkgName = pReq->GetPkgName();
1520     DmDeviceInfo deviceInfo = pReq->GetDeviceInfo();
1521 
1522     if (!data.WriteString(pkgName)) {
1523         LOGE("write pkgName failed");
1524         return ERR_DM_IPC_WRITE_FAILED;
1525     }
1526     if (!EncodeDmDeviceInfo(deviceInfo, data)) {
1527         LOGE("write dm device info failed");
1528         return ERR_DM_IPC_WRITE_FAILED;
1529     }
1530     return DM_OK;
1531 }
1532 
ON_IPC_READ_RESPONSE(SERVER_DEVICE_SCREEN_STATE_NOTIFY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)1533 ON_IPC_READ_RESPONSE(SERVER_DEVICE_SCREEN_STATE_NOTIFY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
1534 {
1535     if (pBaseRsp == nullptr) {
1536         LOGE("pBaseRsp is null");
1537         return ERR_DM_FAILED;
1538     }
1539     pBaseRsp->SetErrCode(reply.ReadInt32());
1540     return DM_OK;
1541 }
1542 
ON_IPC_CMD(GET_DEVICE_SCREEN_STATUS, MessageParcel &data, MessageParcel &reply)1543 ON_IPC_CMD(GET_DEVICE_SCREEN_STATUS, MessageParcel &data, MessageParcel &reply)
1544 {
1545     std::string pkgName = data.ReadString();
1546     std::string networkId = data.ReadString();
1547     int32_t screenStatus = -1;
1548     int32_t result = DeviceManagerService::GetInstance().GetDeviceScreenStatus(pkgName, networkId, screenStatus);
1549     if (!reply.WriteInt32(result)) {
1550         return ERR_DM_IPC_WRITE_FAILED;
1551     }
1552     if (!reply.WriteInt32(screenStatus)) {
1553         return ERR_DM_IPC_WRITE_FAILED;
1554     }
1555     return DM_OK;
1556 }
1557 
ON_IPC_CMD(GET_NETWORKID_BY_UDID, MessageParcel &data, MessageParcel &reply)1558 ON_IPC_CMD(GET_NETWORKID_BY_UDID, MessageParcel &data, MessageParcel &reply)
1559 {
1560     std::string pkgName = data.ReadString();
1561     std::string udid = data.ReadString();
1562     std::string netWorkId;
1563     int32_t result = DeviceManagerService::GetInstance().GetNetworkIdByUdid(pkgName, udid, netWorkId);
1564 
1565     if (!reply.WriteInt32(result)) {
1566         LOGE("write result failed");
1567         return ERR_DM_IPC_WRITE_FAILED;
1568     }
1569     if (!reply.WriteString(netWorkId)) {
1570         LOGE("write result failed");
1571         return ERR_DM_IPC_WRITE_FAILED;
1572     }
1573     return DM_OK;
1574 }
1575 
ON_IPC_SET_REQUEST(SERVICE_CREDENTIAL_AUTH_STATUS_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)1576 ON_IPC_SET_REQUEST(SERVICE_CREDENTIAL_AUTH_STATUS_NOTIFY, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
1577 {
1578     if (pBaseReq == nullptr) {
1579         return ERR_DM_FAILED;
1580     }
1581     std::shared_ptr<IpcNotifyCredentialAuthStatusReq> pReq =
1582         std::static_pointer_cast<IpcNotifyCredentialAuthStatusReq>(pBaseReq);
1583     std::string pkgName = pReq->GetPkgName();
1584     std::string deviceList = pReq->GetDeviceList();
1585     uint16_t deviceTypeId = pReq->GetDeviceTypeId();
1586     int32_t errCode = pReq->GetErrCode();
1587 
1588     if (!data.WriteString(pkgName)) {
1589         LOGE("write pkgName failed");
1590         return ERR_DM_IPC_WRITE_FAILED;
1591     }
1592     if (!data.WriteString(deviceList)) {
1593         LOGE("write deviceList failed");
1594         return ERR_DM_IPC_WRITE_FAILED;
1595     }
1596     if (!data.WriteUint16(deviceTypeId)) {
1597         LOGE("write deviceTypeId failed");
1598         return ERR_DM_IPC_WRITE_FAILED;
1599     }
1600     if (!data.WriteInt32(errCode)) {
1601         LOGE("write errCode failed");
1602         return ERR_DM_IPC_WRITE_FAILED;
1603     }
1604     return DM_OK;
1605 }
1606 
ON_IPC_READ_RESPONSE(SERVICE_CREDENTIAL_AUTH_STATUS_NOTIFY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)1607 ON_IPC_READ_RESPONSE(SERVICE_CREDENTIAL_AUTH_STATUS_NOTIFY, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
1608 {
1609     if (pBaseRsp == nullptr) {
1610         LOGE("pBaseRsp is null");
1611         return ERR_DM_FAILED;
1612     }
1613     pBaseRsp->SetErrCode(reply.ReadInt32());
1614     return DM_OK;
1615 }
1616 } // namespace DistributedHardware
1617 } // namespace OHOS