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
18 #include "scan_constant.h"
19 #include "scan_log.h"
20
21 namespace OHOS::Scan {
ScanCallbackStub()22 ScanCallbackStub::ScanCallbackStub()
23 {
24 cmdMap_[SCAN_CALLBACK_DEVICE_TCP] = &ScanCallbackStub::HandleDeviceInfoTcpEvent;
25 cmdMap_[SCAN_CALLBACK_DEVICE] = &ScanCallbackStub::HandleDeviceInfoEvent;
26 cmdMap_[SCAN_CALLBACK_DEVICE_SYNC] = &ScanCallbackStub::HandleDeviceInfoSyncEvent;
27 cmdMap_[SCAN_CALLBACK_GET_FRAME_RES] = &ScanCallbackStub::HandleGetFrameResEvent;
28 cmdMap_[SCAN_CALLBACK_SCAN_INIT] = &ScanCallbackStub::HandleGetFrameResEvent;
29 cmdMap_[SCAN_CALLBACK_SEND_MESSAGE] = &ScanCallbackStub::HandleSendSearchMessage;
30 cmdMap_[SCAN_CALLBACK_DEVICE_LIST] = &ScanCallbackStub::HandleSendDeviceList;
31 }
32
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t ScanCallbackStub::OnRemoteRequest(
34 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
35 {
36 SCAN_HILOGD("OnRemoteRequest started, code = %{public}d", code);
37 auto descriptorToken = data.ReadInterfaceToken();
38 if (descriptorToken != GetDescriptor()) {
39 SCAN_HILOGE("Remote descriptor not the same as local descriptor.");
40 return E_SCAN_RPC_FAILURE;
41 }
42
43 auto itFunc = cmdMap_.find(code);
44 if (itFunc != cmdMap_.end()) {
45 auto requestFunc = itFunc->second;
46 if (requestFunc != nullptr) {
47 bool result = (this->*requestFunc)(data, reply);
48 return result ? E_SCAN_NONE : E_SCAN_SERVER_FAILURE;
49 }
50 }
51 SCAN_HILOGW("default case, need check.");
52 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
53 }
54
HandleDeviceInfoTcpEvent(MessageParcel & data,MessageParcel & reply)55 bool ScanCallbackStub::HandleDeviceInfoTcpEvent(MessageParcel &data, MessageParcel &reply)
56 {
57 uint32_t state = data.ReadUint32();
58 auto info = ScanDeviceInfoTCP::Unmarshalling(data);
59 if (info == nullptr) {
60 SCAN_HILOGE("invalid scaner info object");
61 return false;
62 }
63 bool result = OnCallback(state, *info);
64 reply.WriteBool(result);
65 return true;
66 }
67
HandleDeviceInfoEvent(MessageParcel & data,MessageParcel & reply)68 bool ScanCallbackStub::HandleDeviceInfoEvent(MessageParcel &data, MessageParcel &reply)
69 {
70 uint32_t state = data.ReadUint32();
71 auto info = ScanDeviceInfo::Unmarshalling(data);
72 if (info == nullptr) {
73 SCAN_HILOGE("invalid scaner info object");
74 return false;
75 }
76 bool result = OnCallback(state, *info);
77 reply.WriteBool(result);
78 return true;
79 }
80
HandleDeviceInfoSyncEvent(MessageParcel & data,MessageParcel & reply)81 bool ScanCallbackStub::HandleDeviceInfoSyncEvent(MessageParcel &data, MessageParcel &reply)
82 {
83 uint32_t state = data.ReadUint32();
84 auto info = ScanDeviceInfoSync::Unmarshalling(data);
85 if (info == nullptr) {
86 SCAN_HILOGE("invalid scaner info object");
87 return false;
88 }
89 bool result = OnCallbackSync(state, *info);
90 reply.WriteBool(result);
91 return true;
92 }
93
HandleGetFrameResEvent(MessageParcel & data,MessageParcel & reply)94 bool ScanCallbackStub::HandleGetFrameResEvent(MessageParcel &data, MessageParcel &reply)
95 {
96 bool isGetSucc = data.ReadBool();
97 int32_t sizeRead = data.ReadInt32();
98 bool result = OnGetFrameResCallback(isGetSucc, sizeRead);
99 reply.WriteBool(result);
100 return true;
101 }
102
HandleScanInitEvent(MessageParcel & data,MessageParcel & reply)103 bool ScanCallbackStub::HandleScanInitEvent(MessageParcel &data, MessageParcel &reply)
104 {
105 int32_t scanVersion = data.ReadInt32();
106 bool result = OnScanInitCallback(scanVersion);
107 reply.WriteBool(result);
108 return true;
109 }
110
HandleSendSearchMessage(MessageParcel & data,MessageParcel & reply)111 bool ScanCallbackStub::HandleSendSearchMessage(MessageParcel &data, MessageParcel &reply)
112 {
113 std::string message = data.ReadString();
114 bool result = OnSendSearchMessage(message);
115 reply.WriteBool(result);
116 return true;
117 }
118
HandleSendDeviceList(MessageParcel & data,MessageParcel & reply)119 bool ScanCallbackStub::HandleSendDeviceList(MessageParcel &data, MessageParcel &reply)
120 {
121 std::vector<ScanDeviceInfo> infos;
122 int infosSize = data.ReadInt32();
123 CHECK_IS_EXCEED_SCAN_RANGE_BOOL(infosSize);
124 SCAN_HILOGI("get infosSize : %{public}d", infosSize);
125 for (auto i = 0; i < infosSize; i++) {
126 auto info = ScanDeviceInfo::Unmarshalling(data);
127 if (info == nullptr) {
128 SCAN_HILOGE("invalid scaner info object");
129 return false;
130 }
131 infos.emplace_back(*info);
132 }
133 bool result = OnGetDevicesList(infos);
134 reply.WriteBool(result);
135 return true;
136 }
137
138 } // namespace OHOS::Scan
139
140 // namespace OHOS::Scan
141