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 HILOGD("BluetoothHidHostStub::ConnectInner");
82 int32_t errCode = Connect(*device);
83 // write error code
84 if (!reply.WriteInt32(errCode)) {
85 HILOGE("reply write failed.");
86 return BT_ERR_IPC_TRANS_FAILED;
87 }
88 return NO_ERROR;
89 }
90
DisconnectInner(MessageParcel & data,MessageParcel & reply)91 int32_t BluetoothHidHostStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
92 {
93 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
94 HILOGD("BluetoothHidHostStub::DisconnectInner");
95 int32_t errCode = Disconnect(*device);
96 // write error code
97 if (!reply.WriteInt32(errCode)) {
98 HILOGE("reply write failed.");
99 return BT_ERR_IPC_TRANS_FAILED;
100 }
101 return NO_ERROR;
102 }
103
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)104 int32_t BluetoothHidHostStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply)
105 {
106 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
107 HILOGD("BluetoothHidHostStub::GetDeviceStateInner");
108 int32_t state;
109 int32_t errCode = GetDeviceState(*device, state);
110 // write error code
111 if (!reply.WriteInt32(errCode)) {
112 HILOGE("reply write failed.");
113 return BT_ERR_IPC_TRANS_FAILED;
114 }
115 if (errCode != NO_ERROR) {
116 HILOGE("internal error.");
117 return BT_ERR_INTERNAL_ERROR;
118 }
119 // write state
120 if (!reply.WriteInt32(state)) {
121 HILOGE("reply write failed.");
122 return BT_ERR_IPC_TRANS_FAILED;
123 }
124 return NO_ERROR;
125 }
126
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)127 int32_t BluetoothHidHostStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
128 {
129 std::vector<int32_t> states = {};
130 int32_t stateSize = data.ReadInt32();
131 if (static_cast<uint32_t>(stateSize) > HID_DEVICE_BY_STATES_NUM_MAX) {
132 return ERR_INVALID_STATE;
133 }
134 HILOGD("BluetoothHidHostStub::GetDevicesByStatesInner");
135 for (int i = 0; i < stateSize; i++) {
136 int32_t state = data.ReadInt32();
137 states.push_back(state);
138 }
139 std::vector<BluetoothRawAddress> rawAdds;
140 int32_t errCode = GetDevicesByStates(states, rawAdds);
141 // write error code
142 if (!reply.WriteInt32(errCode)) {
143 HILOGE("reply write failed.");
144 return BT_ERR_IPC_TRANS_FAILED;
145 }
146 if (errCode != NO_ERROR) {
147 HILOGE("internal error.");
148 return BT_ERR_INTERNAL_ERROR;
149 }
150 // write size
151 if (!reply.WriteInt32(rawAdds.size())) {
152 HILOGE("reply write failed.");
153 return BT_ERR_IPC_TRANS_FAILED;
154 }
155 // write devices
156 for (auto rawAdd : rawAdds) {
157 if (!reply.WriteParcelable(&rawAdd)) {
158 return BT_ERR_IPC_TRANS_FAILED;
159 }
160 }
161 return NO_ERROR;
162 }
163
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)164 ErrCode BluetoothHidHostStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
165 {
166 HILOGD("BluetoothHidHostStub::RegisterObserverInner");
167 sptr<IRemoteObject> remote = data.ReadRemoteObject();
168 const sptr<IBluetoothHidHostObserver> observer = OHOS::iface_cast<IBluetoothHidHostObserver>(remote);
169 RegisterObserver(observer);
170 return NO_ERROR;
171 }
172
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)173 ErrCode BluetoothHidHostStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply)
174 {
175 HILOGD("BluetoothHidHostStub::DeregisterObserverInner");
176 sptr<IRemoteObject> remote = data.ReadRemoteObject();
177 const sptr<IBluetoothHidHostObserver> observer = OHOS::iface_cast<IBluetoothHidHostObserver>(remote);
178 DeregisterObserver(observer);
179 return NO_ERROR;
180 }
181
HidHostVCUnplugInner(MessageParcel & data,MessageParcel & reply)182 ErrCode BluetoothHidHostStub::HidHostVCUnplugInner(MessageParcel &data, MessageParcel &reply)
183 {
184 HILOGD("BluetoothHidHostStub::HidHostVCUnplugInner");
185 std::string device = data.ReadString();
186 uint8_t id = data.ReadUint8();
187 uint16_t size = data.ReadUint16();
188 uint8_t type = data.ReadUint8();
189 int result;
190 ErrCode ec = HidHostVCUnplug(device, id, size, type, result);
191 if (SUCCEEDED(ec)) {
192 reply.WriteInt32(result);
193 }
194 return NO_ERROR;
195 }
196
HidHostSendDataInner(MessageParcel & data,MessageParcel & reply)197 ErrCode BluetoothHidHostStub::HidHostSendDataInner(MessageParcel &data, MessageParcel &reply)
198 {
199 HILOGD("BluetoothHidHostStub::HidHostSendDataInner");
200 std::string device = data.ReadString();
201 uint8_t id = data.ReadUint8();
202 uint16_t size = data.ReadUint16();
203 uint8_t type = data.ReadUint8();
204 int result;
205 ErrCode ec = HidHostSendData(device, id, size, type, result);
206 if (SUCCEEDED(ec)) {
207 reply.WriteInt32(result);
208 }
209 return NO_ERROR;
210 }
211
HidHostSetReportInner(MessageParcel & data,MessageParcel & reply)212 ErrCode BluetoothHidHostStub::HidHostSetReportInner(MessageParcel &data, MessageParcel &reply)
213 {
214 HILOGD("BluetoothHidHostStub::HidHostSetReportInner");
215 std::string device = data.ReadString();
216 uint8_t type = data.ReadUint8();
217 uint16_t size = data.ReadUint16();
218 uint8_t report = data.ReadUint8();
219 int result;
220 ErrCode ec = HidHostSetReport(device, type, size, report, result);
221 if (SUCCEEDED(ec)) {
222 reply.WriteInt32(result);
223 }
224 return NO_ERROR;
225 }
226
HidHostGetReportInner(MessageParcel & data,MessageParcel & reply)227 ErrCode BluetoothHidHostStub::HidHostGetReportInner(MessageParcel &data, MessageParcel &reply)
228 {
229 HILOGD("BluetoothHidHostStub::HidHostGetReportInner");
230 std::string device = data.ReadString();
231 uint8_t id = data.ReadUint8();
232 uint16_t size = data.ReadUint16();
233 uint8_t type = data.ReadUint8();
234 int result;
235 ErrCode ec = HidHostGetReport(device, id, size, type, result);
236 if (SUCCEEDED(ec)) {
237 reply.WriteInt32(result);
238 }
239 return NO_ERROR;
240 }
241
HidHostSetConnectStrategyInner(MessageParcel & data,MessageParcel & reply)242 ErrCode BluetoothHidHostStub::HidHostSetConnectStrategyInner(MessageParcel &data, MessageParcel &reply)
243 {
244 std::string addr = data.ReadString();
245 int strategy = data.ReadInt32();
246
247 int result = SetConnectStrategy(RawAddress(addr), strategy);
248 if (!reply.WriteInt32(result)) {
249 HILOGE("BluetoothHidHostStub: reply writing failed in: %{public}s.", __func__);
250 return ERR_INVALID_VALUE;
251 }
252 return NO_ERROR;
253 }
254
HidHostGetConnectStrategyInner(MessageParcel & data,MessageParcel & reply)255 ErrCode BluetoothHidHostStub::HidHostGetConnectStrategyInner(MessageParcel &data, MessageParcel &reply)
256 {
257 return NO_ERROR;
258 }
259 } // Bluetooth
260 } // OHOS
261