1 /*
2 * Copyright (C) 2021-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 "bluetooth_pbap_pce_stub.h"
17 #include "bluetooth_log.h"
18 #include "ipc_types.h"
19 #include "string_ex.h"
20 namespace OHOS {
21 namespace Bluetooth {
BluetoothPbapPceStub()22 BluetoothPbapPceStub::BluetoothPbapPceStub()
23 {
24 HILOGD("%{public}s start.", __func__);
25 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_GET_DEVICE_STATE)] =
26 &BluetoothPbapPceStub::GetDeviceStateInner;
27 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_CONNECT)] =
28 &BluetoothPbapPceStub::ConnectInner;
29 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_PULL_PHONEBOOK)] =
30 &BluetoothPbapPceStub::PullPhoneBookInner;
31 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_SET_PHONEBOOK)] =
32 &BluetoothPbapPceStub::SetPhoneBookInner;
33 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_PULL_VCARD_LISTING)] =
34 &BluetoothPbapPceStub::PullvCardListingInner;
35 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_PULL_VCARD_ENTRY)] =
36 &BluetoothPbapPceStub::PullvCardEntryInner;
37 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_PULL_ISDOWNLOAGING)] =
38 &BluetoothPbapPceStub::IsDownloadingInner;
39 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_ABORT_DOWNLOADING)] =
40 &BluetoothPbapPceStub::AbortDownloadingInner;
41 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_SET_DEVICE_PASSWORD)] =
42 &BluetoothPbapPceStub::SetDevicePasswordInner;
43 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_DISCONNECT)] =
44 &BluetoothPbapPceStub::DisconnectInner;
45 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_SET_CONNECT_STRATEGY)] =
46 &BluetoothPbapPceStub::SetConnectionStrategyInner;
47 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_GET_CONNECT_STRATEGY)] =
48 &BluetoothPbapPceStub::GetConnectionStrategyInner;
49 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_GET_DEVICES_BY_STATE)] =
50 &BluetoothPbapPceStub::GetDevicesByStatesInner;
51 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_REGISTER_OBSERVER)] =
52 &BluetoothPbapPceStub::RegisterObserverInner;
53 memberFuncMap_[static_cast<uint32_t>(BluetoothPbapPceStub::Code::PBAP_PCE_DEREGISTER_OBSERVER)] =
54 &BluetoothPbapPceStub::DeregisterObserverInner;
55 }
56
~BluetoothPbapPceStub()57 BluetoothPbapPceStub::~BluetoothPbapPceStub()
58 {
59 HILOGD("%{public}s start.", __func__);
60 memberFuncMap_.clear();
61 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)62 int BluetoothPbapPceStub::OnRemoteRequest(
63 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
64 {
65 HILOGD("BluetoothPbapPceStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
66 std::u16string descriptor = BluetoothPbapPceStub::GetDescriptor();
67 std::u16string remoteDescriptor = data.ReadInterfaceToken();
68 if (descriptor != remoteDescriptor) {
69 HILOGI("local descriptor is not equal to remote");
70 return ERR_INVALID_STATE;
71 }
72
73 auto itFunc = memberFuncMap_.find(code);
74 if (itFunc != memberFuncMap_.end()) {
75 auto memberFunc = itFunc->second;
76 if (memberFunc != nullptr) {
77 return (this->*memberFunc)(data, reply);
78 }
79 }
80 HILOGW("BluetoothPbapPceStub::OnRemoteRequest, default case, need check.");
81 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
82 };
83
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)84 ErrCode BluetoothPbapPceStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply)
85 {
86 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
87 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
88 if (!device) {
89 return TRANSACTION_ERR;
90 }
91 int result = GetDeviceState(*device);
92 bool ret = reply.WriteInt32(result);
93 if (!ret) {
94 HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
95 return ERR_INVALID_VALUE;
96 }
97 return NO_ERROR;
98 }
99
ConnectInner(MessageParcel & data,MessageParcel & reply)100 ErrCode BluetoothPbapPceStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
101 {
102 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
103 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
104 if (!device) {
105 return TRANSACTION_ERR;
106 }
107 int result = Connect(*device);
108 bool ret = reply.WriteInt32(result);
109 if (!ret) {
110 HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
111 return ERR_INVALID_VALUE;
112 }
113 return NO_ERROR;
114 }
115
PullPhoneBookInner(MessageParcel & data,MessageParcel & reply)116 ErrCode BluetoothPbapPceStub::PullPhoneBookInner(MessageParcel &data, MessageParcel &reply)
117 {
118 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
119 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
120 if (!device) {
121 return TRANSACTION_ERR;
122 }
123 std::shared_ptr<BluetoothIPbapPullPhoneBookParam> param(data.ReadParcelable<BluetoothIPbapPullPhoneBookParam>());
124 if (!param) {
125 return TRANSACTION_ERR;
126 }
127 int result = PullPhoneBook(*device, *param);
128 bool ret = reply.WriteInt32(result);
129 if (!ret) {
130 HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
131 return ERR_INVALID_VALUE;
132 }
133 return NO_ERROR;
134 }
135
SetPhoneBookInner(MessageParcel & data,MessageParcel & reply)136 ErrCode BluetoothPbapPceStub::SetPhoneBookInner(MessageParcel &data, MessageParcel &reply)
137 {
138 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
139 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
140 if (!device) {
141 return TRANSACTION_ERR;
142 }
143 const std::u16string name = data.ReadString16();
144 int32_t flag = data.ReadInt32();
145 int result = SetPhoneBook(*device, name, flag);
146 bool ret = reply.WriteInt32(result);
147 if (!ret) {
148 HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
149 return ERR_INVALID_VALUE;
150 }
151 return NO_ERROR;
152 }
153
PullvCardListingInner(MessageParcel & data,MessageParcel & reply)154 ErrCode BluetoothPbapPceStub::PullvCardListingInner(MessageParcel &data, MessageParcel &reply)
155 {
156 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
157 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
158 if (!device) {
159 return TRANSACTION_ERR;
160 }
161 std::shared_ptr<BluetoothIPbapPullvCardListingParam> param(
162 data.ReadParcelable<BluetoothIPbapPullvCardListingParam>());
163 int result = PullvCardListing(*device, *param);
164 bool ret = reply.WriteInt32(result);
165 if (!ret) {
166 HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
167 return ERR_INVALID_VALUE;
168 }
169 return NO_ERROR;
170 }
171
PullvCardEntryInner(MessageParcel & data,MessageParcel & reply)172 ErrCode BluetoothPbapPceStub::PullvCardEntryInner(MessageParcel &data, MessageParcel &reply)
173 {
174 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
175 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
176 if (!device) {
177 return TRANSACTION_ERR;
178 }
179 std::shared_ptr<BluetoothIPbapPullvCardEntryParam> param(data.ReadParcelable<BluetoothIPbapPullvCardEntryParam>());
180 if (!param) {
181 return TRANSACTION_ERR;
182 }
183 int result = PullvCardEntry(*device, *param);
184 bool ret = reply.WriteInt32(result);
185 if (!ret) {
186 HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
187 return ERR_INVALID_VALUE;
188 }
189 return NO_ERROR;
190 }
191
IsDownloadingInner(MessageParcel & data,MessageParcel & reply)192 ErrCode BluetoothPbapPceStub::IsDownloadingInner(MessageParcel &data, MessageParcel &reply)
193 {
194 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
195 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
196 if (!device) {
197 return TRANSACTION_ERR;
198 }
199 bool result = IsDownloading(*device);
200 bool ret = reply.WriteBool(result);
201 if (!ret) {
202 HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
203 return ERR_INVALID_VALUE;
204 }
205 return NO_ERROR;
206 }
207
AbortDownloadingInner(MessageParcel & data,MessageParcel & reply)208 ErrCode BluetoothPbapPceStub::AbortDownloadingInner(MessageParcel &data, MessageParcel &reply)
209 {
210 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
211 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
212 if (!device) {
213 return TRANSACTION_ERR;
214 }
215 int result = AbortDownloading(*device);
216 bool ret = reply.WriteInt32(result);
217 if (!ret) {
218 HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
219 return ERR_INVALID_VALUE;
220 }
221 return NO_ERROR;
222 }
223
SetDevicePasswordInner(MessageParcel & data,MessageParcel & reply)224 ErrCode BluetoothPbapPceStub::SetDevicePasswordInner(MessageParcel &data, MessageParcel &reply)
225 {
226 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
227 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
228 if (!device) {
229 return TRANSACTION_ERR;
230 }
231 const std::string password = data.ReadString();
232 const std::string userId = data.ReadString();
233 int result = SetDevicePassword(*device, password, userId);
234 bool ret = reply.WriteInt32(result);
235 if (!ret) {
236 HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
237 return ERR_INVALID_VALUE;
238 }
239 return NO_ERROR;
240 }
241
DisconnectInner(MessageParcel & data,MessageParcel & reply)242 ErrCode BluetoothPbapPceStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
243 {
244 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
245 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
246 if (!device) {
247 return TRANSACTION_ERR;
248 }
249 int result = Disconnect(*device);
250 bool ret = reply.WriteInt32(result);
251 if (!ret) {
252 HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
253 return ERR_INVALID_VALUE;
254 }
255 return NO_ERROR;
256 }
257
SetConnectionStrategyInner(MessageParcel & data,MessageParcel & reply)258 ErrCode BluetoothPbapPceStub::SetConnectionStrategyInner(MessageParcel &data, MessageParcel &reply)
259 {
260 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
261 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
262 if (!device) {
263 return TRANSACTION_ERR;
264 }
265 int32_t strategy = data.ReadInt32();
266 int result = SetConnectionStrategy(*device, strategy);
267 bool ret = reply.WriteInt32(result);
268 if (!ret) {
269 HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
270 return ERR_INVALID_VALUE;
271 }
272 return NO_ERROR;
273 }
274
GetConnectionStrategyInner(MessageParcel & data,MessageParcel & reply)275 ErrCode BluetoothPbapPceStub::GetConnectionStrategyInner(MessageParcel &data, MessageParcel &reply)
276 {
277 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
278 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
279 if (!device) {
280 return TRANSACTION_ERR;
281 }
282 int result = GetConnectionStrategy(*device);
283 bool ret = reply.WriteInt32(result);
284 if (!ret) {
285 HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
286 return ERR_INVALID_VALUE;
287 }
288 return NO_ERROR;
289 }
290
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)291 ErrCode BluetoothPbapPceStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
292 {
293 HILOGI("BluetoothPbapPceStub::GetDevicesByStatesInner starts");
294 std::vector<int32_t> state;
295 if (!data.ReadInt32Vector(&state)) {
296 HILOGW("BluetoothPbapPceStub::GetDevicesByStatesInner: get tmpState failed.");
297 return INVALID_DATA;
298 }
299 std::vector<BluetoothRawAddress> rawDevices;
300 GetDevicesByStates(state, rawDevices);
301 reply.WriteInt32(rawDevices.size());
302 int num = rawDevices.size();
303 for (int i = 0; i < num; i++) {
304 bool ret = reply.WriteParcelable(&rawDevices[i]);
305 if (!ret) {
306 HILOGE("WriteParcelable<GetDevicesByStatesInner> failed");
307 return ERR_INVALID_VALUE;
308 }
309 }
310 return NO_ERROR;
311 }
312
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)313 ErrCode BluetoothPbapPceStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
314 {
315 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
316 sptr<IRemoteObject> remote = data.ReadRemoteObject();
317 const sptr<IBluetoothPbapPceObserver> observer = OHOS::iface_cast<IBluetoothPbapPceObserver>(remote);
318 RegisterObserver(observer);
319
320 return NO_ERROR;
321 }
322
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)323 ErrCode BluetoothPbapPceStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply)
324 {
325 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
326 sptr<IRemoteObject> remote = data.ReadRemoteObject();
327 const sptr<IBluetoothPbapPceObserver> observer = OHOS::iface_cast<IBluetoothPbapPceObserver>(remote);
328 DeregisterObserver(observer);
329
330 return NO_ERROR;
331 }
332 } // namespace Bluetooth
333 } // namespace OHOS