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 "bluetooth_hid_host_stub.h"
16
17 #include "bluetooth_errorcode.h"
18 #include "bluetooth_log.h"
19
20 namespace OHOS {
21 namespace Bluetooth {
22 const uint32_t HID_DEVICE_BY_STATES_NUM_MAX = 0XFF;
BluetoothHidHostStub()23 BluetoothHidHostStub::BluetoothHidHostStub()
24 {
25 HILOGD("%{public}s start.", __func__);
26 memberFuncMap_[static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_CONNECT)] =
27 &BluetoothHidHostStub::ConnectInner;
28 memberFuncMap_[static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_DISCONNECT)] =
29 &BluetoothHidHostStub::DisconnectInner;
30 memberFuncMap_[static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_DEVICE_STATE)] =
31 &BluetoothHidHostStub::GetDeviceStateInner;
32 memberFuncMap_[static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_DEVICES_BY_STATES)] =
33 &BluetoothHidHostStub::GetDevicesByStatesInner;
34 memberFuncMap_[static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_REGISTER_OBSERVER)] =
35 &BluetoothHidHostStub::RegisterObserverInner;
36 memberFuncMap_[static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_DEREGISTER_OBSERVER)] =
37 &BluetoothHidHostStub::DeregisterObserverInner;
38 memberFuncMap_[static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_VCUN_PLUG)] =
39 &BluetoothHidHostStub::HidHostVCUnplugInner;
40 memberFuncMap_[static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_SEND_DATA)] =
41 &BluetoothHidHostStub::HidHostSendDataInner;
42 memberFuncMap_[static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_SET_REPORT)] =
43 &BluetoothHidHostStub::HidHostSetReportInner;
44 memberFuncMap_[static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_REPORT)] =
45 &BluetoothHidHostStub::HidHostGetReportInner;
46 memberFuncMap_[static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_SET_CONNECT_STRATEGY)] =
47 &BluetoothHidHostStub::HidHostSetConnectStrategyInner;
48 memberFuncMap_[static_cast<uint32_t>(BluetoothHidHostInterfaceCode::COMMAND_GET_CONNECT_STRATEGY)] =
49 &BluetoothHidHostStub::HidHostGetConnectStrategyInner;
50 HILOGD("%{public}s ends.", __func__);
51 }
52
~BluetoothHidHostStub()53 BluetoothHidHostStub::~BluetoothHidHostStub()
54 {
55 HILOGD("%{public}s start.", __func__);
56 memberFuncMap_.clear();
57 }
58
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)59 int BluetoothHidHostStub::OnRemoteRequest(
60 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
61 {
62 HILOGI("BluetoothHidHostStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
63 if (BluetoothHidHostStub::GetDescriptor() != data.ReadInterfaceToken()) {
64 HILOGE("local descriptor is not equal to remote");
65 return IPC_INVOKER_TRANSLATE_ERR;
66 }
67 auto itFunc = memberFuncMap_.find(code);
68 if (itFunc != memberFuncMap_.end()) {
69 auto memberFunc = itFunc->second;
70 if (memberFunc != nullptr) {
71 return (this->*memberFunc)(data, reply);
72 }
73 }
74 HILOGW("BluetoothHfpHfStub::OnRemoteRequest, default case, need check.");
75 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
76 }
77
ConnectInner(MessageParcel & data,MessageParcel & reply)78 int32_t BluetoothHidHostStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
79 {
80 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
81 if (!device) {
82 return BT_ERR_IPC_TRANS_FAILED;
83 }
84 HILOGD("BluetoothHidHostStub::ConnectInner");
85 int32_t errCode = Connect(*device);
86 // write error code
87 if (!reply.WriteInt32(errCode)) {
88 HILOGE("reply write failed.");
89 return BT_ERR_IPC_TRANS_FAILED;
90 }
91 return NO_ERROR;
92 }
93
DisconnectInner(MessageParcel & data,MessageParcel & reply)94 int32_t BluetoothHidHostStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
95 {
96 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
97 if (!device) {
98 return BT_ERR_IPC_TRANS_FAILED;
99 }
100 HILOGD("BluetoothHidHostStub::DisconnectInner");
101 int32_t errCode = Disconnect(*device);
102 // write error code
103 if (!reply.WriteInt32(errCode)) {
104 HILOGE("reply write failed.");
105 return BT_ERR_IPC_TRANS_FAILED;
106 }
107 return NO_ERROR;
108 }
109
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)110 int32_t BluetoothHidHostStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply)
111 {
112 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
113 if (!device) {
114 return BT_ERR_IPC_TRANS_FAILED;
115 }
116 HILOGD("BluetoothHidHostStub::GetDeviceStateInner");
117 int32_t state;
118 int32_t errCode = GetDeviceState(*device, state);
119 // write error code
120 if (!reply.WriteInt32(errCode)) {
121 HILOGE("reply write failed.");
122 return BT_ERR_IPC_TRANS_FAILED;
123 }
124 if (errCode != NO_ERROR) {
125 HILOGE("internal error.");
126 return BT_ERR_INTERNAL_ERROR;
127 }
128 // write state
129 if (!reply.WriteInt32(state)) {
130 HILOGE("reply write failed.");
131 return BT_ERR_IPC_TRANS_FAILED;
132 }
133 return NO_ERROR;
134 }
135
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)136 int32_t BluetoothHidHostStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
137 {
138 std::vector<int32_t> states = {};
139 int32_t stateSize = data.ReadInt32();
140 if (static_cast<uint32_t>(stateSize) > HID_DEVICE_BY_STATES_NUM_MAX) {
141 return ERR_INVALID_STATE;
142 }
143 HILOGD("BluetoothHidHostStub::GetDevicesByStatesInner");
144 for (int i = 0; i < stateSize; i++) {
145 int32_t state = data.ReadInt32();
146 states.push_back(state);
147 }
148 std::vector<BluetoothRawAddress> rawAdds;
149 int32_t errCode = GetDevicesByStates(states, rawAdds);
150 // write error code
151 if (!reply.WriteInt32(errCode)) {
152 HILOGE("reply write failed.");
153 return BT_ERR_IPC_TRANS_FAILED;
154 }
155 if (errCode != NO_ERROR) {
156 HILOGE("internal error.");
157 return BT_ERR_INTERNAL_ERROR;
158 }
159 // write size
160 if (!reply.WriteInt32(rawAdds.size())) {
161 HILOGE("reply write failed.");
162 return BT_ERR_IPC_TRANS_FAILED;
163 }
164 // write devices
165 for (auto rawAdd : rawAdds) {
166 if (!reply.WriteParcelable(&rawAdd)) {
167 return BT_ERR_IPC_TRANS_FAILED;
168 }
169 }
170 return NO_ERROR;
171 }
172
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)173 ErrCode BluetoothHidHostStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
174 {
175 HILOGD("BluetoothHidHostStub::RegisterObserverInner");
176 sptr<IRemoteObject> remote = data.ReadRemoteObject();
177 const sptr<IBluetoothHidHostObserver> observer = OHOS::iface_cast<IBluetoothHidHostObserver>(remote);
178 RegisterObserver(observer);
179 return NO_ERROR;
180 }
181
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)182 ErrCode BluetoothHidHostStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply)
183 {
184 HILOGD("BluetoothHidHostStub::DeregisterObserverInner");
185 sptr<IRemoteObject> remote = data.ReadRemoteObject();
186 const sptr<IBluetoothHidHostObserver> observer = OHOS::iface_cast<IBluetoothHidHostObserver>(remote);
187 DeregisterObserver(observer);
188 return NO_ERROR;
189 }
190
HidHostVCUnplugInner(MessageParcel & data,MessageParcel & reply)191 ErrCode BluetoothHidHostStub::HidHostVCUnplugInner(MessageParcel &data, MessageParcel &reply)
192 {
193 HILOGD("BluetoothHidHostStub::HidHostVCUnplugInner");
194 std::string device = data.ReadString();
195 uint8_t id = data.ReadUint8();
196 uint16_t size = data.ReadUint16();
197 uint8_t type = data.ReadUint8();
198 int result;
199 ErrCode ec = HidHostVCUnplug(device, id, size, type, result);
200 if (SUCCEEDED(ec)) {
201 reply.WriteInt32(result);
202 }
203 return NO_ERROR;
204 }
205
HidHostSendDataInner(MessageParcel & data,MessageParcel & reply)206 ErrCode BluetoothHidHostStub::HidHostSendDataInner(MessageParcel &data, MessageParcel &reply)
207 {
208 HILOGD("BluetoothHidHostStub::HidHostSendDataInner");
209 std::string device = data.ReadString();
210 uint8_t id = data.ReadUint8();
211 uint16_t size = data.ReadUint16();
212 uint8_t type = data.ReadUint8();
213 int result;
214 ErrCode ec = HidHostSendData(device, id, size, type, result);
215 if (SUCCEEDED(ec)) {
216 reply.WriteInt32(result);
217 }
218 return NO_ERROR;
219 }
220
HidHostSetReportInner(MessageParcel & data,MessageParcel & reply)221 ErrCode BluetoothHidHostStub::HidHostSetReportInner(MessageParcel &data, MessageParcel &reply)
222 {
223 HILOGD("BluetoothHidHostStub::HidHostSetReportInner");
224 std::string device = data.ReadString();
225 uint8_t type = data.ReadUint8();
226 std::string report = data.ReadString();
227 int result;
228 ErrCode ec = HidHostSetReport(device, type, report, result);
229 if (SUCCEEDED(ec)) {
230 reply.WriteInt32(result);
231 }
232 return NO_ERROR;
233 }
234
HidHostGetReportInner(MessageParcel & data,MessageParcel & reply)235 ErrCode BluetoothHidHostStub::HidHostGetReportInner(MessageParcel &data, MessageParcel &reply)
236 {
237 HILOGD("BluetoothHidHostStub::HidHostGetReportInner");
238 std::string device = data.ReadString();
239 uint8_t id = data.ReadUint8();
240 uint16_t size = data.ReadUint16();
241 uint8_t type = data.ReadUint8();
242 int result;
243 ErrCode ec = HidHostGetReport(device, id, size, type, result);
244 if (SUCCEEDED(ec)) {
245 reply.WriteInt32(result);
246 }
247 return NO_ERROR;
248 }
249
HidHostSetConnectStrategyInner(MessageParcel & data,MessageParcel & reply)250 ErrCode BluetoothHidHostStub::HidHostSetConnectStrategyInner(MessageParcel &data, MessageParcel &reply)
251 {
252 std::string addr = data.ReadString();
253 int strategy = data.ReadInt32();
254
255 int result = SetConnectStrategy(RawAddress(addr), strategy);
256 if (!reply.WriteInt32(result)) {
257 HILOGE("BluetoothHidHostStub: reply writing failed in: %{public}s.", __func__);
258 return ERR_INVALID_VALUE;
259 }
260 return NO_ERROR;
261 }
262
HidHostGetConnectStrategyInner(MessageParcel & data,MessageParcel & reply)263 ErrCode BluetoothHidHostStub::HidHostGetConnectStrategyInner(MessageParcel &data, MessageParcel &reply)
264 {
265 return NO_ERROR;
266 }
267 } // Bluetooth
268 } // OHOS
269