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
16 #include "scan_callback_stub.h"
17 #include "scan_constant.h"
18 #include "scan_log.h"
19
20 namespace OHOS::Scan {
ScanCallbackStub()21 ScanCallbackStub::ScanCallbackStub()
22 {
23 cmdMap_[SCAN_CALLBACK_DEVICE] = &ScanCallbackStub::HandleDeviceInfoEvent;
24 cmdMap_[SCAN_CALLBACK_DEVICE_SYNC] = &ScanCallbackStub::HandleDeviceInfoSyncEvent;
25 cmdMap_[SCAN_CALLBACK_DEVICE_LIST] = &ScanCallbackStub::HandleSendDeviceList;
26 }
27
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)28 int32_t ScanCallbackStub::OnRemoteRequest(
29 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
30 {
31 SCAN_HILOGD("OnRemoteRequest started, code = %{public}d", code);
32 auto descriptorToken = data.ReadInterfaceToken();
33 if (descriptorToken != GetDescriptor()) {
34 SCAN_HILOGE("Remote descriptor not the same as local descriptor.");
35 return E_SCAN_RPC_FAILURE;
36 }
37
38 auto itFunc = cmdMap_.find(code);
39 if (itFunc != cmdMap_.end()) {
40 auto requestFunc = itFunc->second;
41 if (requestFunc != nullptr) {
42 bool result = (this->*requestFunc)(data, reply);
43 return result ? E_SCAN_NONE : E_SCAN_SERVER_FAILURE;
44 }
45 }
46 SCAN_HILOGW("default case, need check.");
47 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
48 }
49
HandleDeviceInfoEvent(MessageParcel & data,MessageParcel & reply)50 bool ScanCallbackStub::HandleDeviceInfoEvent(MessageParcel &data, MessageParcel &reply)
51 {
52 uint32_t state = data.ReadUint32();
53 auto info = ScanDeviceInfo::Unmarshalling(data);
54 if (info == nullptr) {
55 SCAN_HILOGE("invalid scaner info object");
56 return false;
57 }
58 bool result = OnCallback(state, *info);
59 reply.WriteBool(result);
60 return true;
61 }
62
HandleDeviceInfoSyncEvent(MessageParcel & data,MessageParcel & reply)63 bool ScanCallbackStub::HandleDeviceInfoSyncEvent(MessageParcel &data, MessageParcel &reply)
64 {
65 uint32_t state = data.ReadUint32();
66 auto info = ScanDeviceInfoSync::Unmarshalling(data);
67 if (info == nullptr) {
68 SCAN_HILOGE("invalid scaner info object");
69 return false;
70 }
71 bool result = OnCallbackSync(state, *info);
72 reply.WriteBool(result);
73 return true;
74 }
75
HandleSendDeviceList(MessageParcel & data,MessageParcel & reply)76 bool ScanCallbackStub::HandleSendDeviceList(MessageParcel &data, MessageParcel &reply)
77 {
78 std::vector<ScanDeviceInfo> infos;
79 int infosSize = data.ReadInt32();
80 CHECK_IS_EXCEED_SCAN_RANGE_BOOL(infosSize);
81 SCAN_HILOGI("get infosSize : %{public}d", infosSize);
82 for (auto i = 0; i < infosSize; i++) {
83 auto info = ScanDeviceInfo::Unmarshalling(data);
84 if (info == nullptr) {
85 SCAN_HILOGE("invalid scaner info object");
86 return false;
87 }
88 infos.emplace_back(*info);
89 }
90 bool result = OnGetDevicesList(infos);
91 reply.WriteBool(result);
92 return true;
93 }
94
95 } // namespace OHOS::Scan
96
97 // namespace OHOS::Scan
98