1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "scan_callback_proxy.h"
16
17 #include "message_parcel.h"
18 #include "scan_log.h"
19
20 namespace OHOS::Scan {
ScanCallbackProxy(const sptr<IRemoteObject> & impl)21 ScanCallbackProxy::ScanCallbackProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IScanCallback>(impl) {}
22
OnCallback(uint32_t state,const ScanDeviceInfo & info)23 bool ScanCallbackProxy::OnCallback(uint32_t state, const ScanDeviceInfo &info)
24 {
25 SCAN_HILOGD("ScanCallbackProxy::OnCallback Start");
26 MessageParcel data;
27 MessageParcel reply;
28 MessageOption option;
29
30 if (!data.WriteInterfaceToken(GetDescriptor())) {
31 SCAN_HILOGE("write descriptor failed");
32 return false;
33 }
34
35 data.WriteUint32(state);
36 info.Marshalling(data);
37
38 auto remote = Remote();
39 if (remote == nullptr) {
40 SCAN_HILOGE("ScanCallbackProxy::OnCallback remote is null");
41 return false;
42 }
43
44 int error = remote->SendRequest(SCAN_CALLBACK_DEVICE, data, reply, option);
45 if (error != 0) {
46 SCAN_HILOGE("SendRequest failed, error %{public}d", error);
47 return false;
48 }
49
50 SCAN_HILOGD("ScanCallbackProxy::OnCallback End");
51 return true;
52 }
53
OnCallbackSync(uint32_t state,const ScanDeviceInfoSync & info)54 bool ScanCallbackProxy::OnCallbackSync(uint32_t state, const ScanDeviceInfoSync &info)
55 {
56 SCAN_HILOGD("ScanCallbackProxy::OnCallback Start");
57 MessageParcel data;
58 MessageParcel reply;
59 MessageOption option;
60
61 if (!data.WriteInterfaceToken(GetDescriptor())) {
62 SCAN_HILOGE("write descriptor failed");
63 return false;
64 }
65 data.WriteUint32(state);
66 info.Marshalling(data);
67
68 auto remote = Remote();
69 if (remote == nullptr) {
70 SCAN_HILOGE("ScanCallbackProxy::OnCallbackSync remote is null");
71 return false;
72 }
73
74 int error = remote->SendRequest(SCAN_CALLBACK_DEVICE_SYNC, data, reply, option);
75 if (error != 0) {
76 SCAN_HILOGE("SendRequest failed, error %{public}d", error);
77 return false;
78 }
79
80 SCAN_HILOGD("ScanCallbackProxy::OnCallbackSync End");
81 return true;
82 }
83
OnGetDevicesList(std::vector<ScanDeviceInfo> & infos)84 bool ScanCallbackProxy::OnGetDevicesList(std::vector<ScanDeviceInfo> &infos)
85 {
86 SCAN_HILOGI("Enter OnGetDevicesList");
87 MessageParcel data;
88 MessageParcel reply;
89 MessageOption option;
90
91 if (!data.WriteInterfaceToken(GetDescriptor())) {
92 SCAN_HILOGE("write descriptor failed");
93 return false;
94 }
95 data.WriteInt32(infos.size());
96 for (size_t i = 0; i < infos.size(); i++) {
97 infos[i].Marshalling(data);
98 }
99 sptr<IRemoteObject> remote = Remote();
100 if (remote == nullptr) {
101 SCAN_HILOGE("ScanCallbackProxy::OnGetDevicesList remote is null");
102 return false;
103 }
104 int error = remote->SendRequest(SCAN_CALLBACK_DEVICE_LIST, data, reply, option);
105 if (error != 0) {
106 SCAN_HILOGE("SendRequest failed, error %{public}d", error);
107 return false;
108 }
109
110 SCAN_HILOGI("ScanCallbackProxy::OnCallback End");
111 return true;
112 }
113 } // namespace OHOS::Scan
114