• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
88     int result = GetDeviceState(*device);
89     bool ret = reply.WriteInt32(result);
90     if (!ret) {
91         HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
92         return ERR_INVALID_VALUE;
93     }
94     return NO_ERROR;
95 }
96 
ConnectInner(MessageParcel & data,MessageParcel & reply)97 ErrCode BluetoothPbapPceStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
98 {
99     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
100     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
101     int result = Connect(*device);
102     bool ret = reply.WriteInt32(result);
103     if (!ret) {
104         HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
105         return ERR_INVALID_VALUE;
106     }
107     return NO_ERROR;
108 }
109 
PullPhoneBookInner(MessageParcel & data,MessageParcel & reply)110 ErrCode BluetoothPbapPceStub::PullPhoneBookInner(MessageParcel &data, MessageParcel &reply)
111 {
112     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
113     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
114     const BluetoothIPbapPullPhoneBookParam *param = data.ReadParcelable<BluetoothIPbapPullPhoneBookParam>();
115     int result = PullPhoneBook(*device, *param);
116     bool ret = reply.WriteInt32(result);
117     if (!ret) {
118         HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
119         return ERR_INVALID_VALUE;
120     }
121     return NO_ERROR;
122 }
123 
SetPhoneBookInner(MessageParcel & data,MessageParcel & reply)124 ErrCode BluetoothPbapPceStub::SetPhoneBookInner(MessageParcel &data, MessageParcel &reply)
125 {
126     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
127     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
128     const std::u16string name = data.ReadString16();
129     int32_t flag = data.ReadInt32();
130     int result = SetPhoneBook(*device, name, flag);
131     bool ret = reply.WriteInt32(result);
132     if (!ret) {
133         HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
134         return ERR_INVALID_VALUE;
135     }
136     return NO_ERROR;
137 }
138 
PullvCardListingInner(MessageParcel & data,MessageParcel & reply)139 ErrCode BluetoothPbapPceStub::PullvCardListingInner(MessageParcel &data, MessageParcel &reply)
140 {
141     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
142     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
143     const BluetoothIPbapPullvCardListingParam *param = data.ReadParcelable<BluetoothIPbapPullvCardListingParam>();
144     int result = PullvCardListing(*device, *param);
145     bool ret = reply.WriteInt32(result);
146     if (!ret) {
147         HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
148         return ERR_INVALID_VALUE;
149     }
150     return NO_ERROR;
151 }
152 
PullvCardEntryInner(MessageParcel & data,MessageParcel & reply)153 ErrCode BluetoothPbapPceStub::PullvCardEntryInner(MessageParcel &data, MessageParcel &reply)
154 {
155     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
156     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
157     const BluetoothIPbapPullvCardEntryParam *param = data.ReadParcelable<BluetoothIPbapPullvCardEntryParam>();
158     int result = PullvCardEntry(*device, *param);
159     bool ret = reply.WriteInt32(result);
160     if (!ret) {
161         HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
162         return ERR_INVALID_VALUE;
163     }
164     return NO_ERROR;
165 }
166 
IsDownloadingInner(MessageParcel & data,MessageParcel & reply)167 ErrCode BluetoothPbapPceStub::IsDownloadingInner(MessageParcel &data, MessageParcel &reply)
168 {
169     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
170     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
171     bool result = IsDownloading(*device);
172     bool ret = reply.WriteBool(result);
173     if (!ret) {
174         HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
175         return ERR_INVALID_VALUE;
176     }
177     return NO_ERROR;
178 }
179 
AbortDownloadingInner(MessageParcel & data,MessageParcel & reply)180 ErrCode BluetoothPbapPceStub::AbortDownloadingInner(MessageParcel &data, MessageParcel &reply)
181 {
182     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
183     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
184     int result = AbortDownloading(*device);
185     bool ret = reply.WriteInt32(result);
186     if (!ret) {
187         HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
188         return ERR_INVALID_VALUE;
189     }
190     return NO_ERROR;
191 }
192 
SetDevicePasswordInner(MessageParcel & data,MessageParcel & reply)193 ErrCode BluetoothPbapPceStub::SetDevicePasswordInner(MessageParcel &data, MessageParcel &reply)
194 {
195     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
196     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
197     const std::string password = data.ReadString();
198     const std::string userId = data.ReadString();
199     int result = SetDevicePassword(*device, password, userId);
200     bool ret = reply.WriteInt32(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 
DisconnectInner(MessageParcel & data,MessageParcel & reply)208 ErrCode BluetoothPbapPceStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
209 {
210     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
211     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
212     int result = Disconnect(*device);
213     bool ret = reply.WriteInt32(result);
214     if (!ret) {
215         HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
216         return ERR_INVALID_VALUE;
217     }
218     return NO_ERROR;
219 }
220 
SetConnectionStrategyInner(MessageParcel & data,MessageParcel & reply)221 ErrCode BluetoothPbapPceStub::SetConnectionStrategyInner(MessageParcel &data, MessageParcel &reply)
222 {
223     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
224     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
225     int32_t strategy = data.ReadInt32();
226     int result = SetConnectionStrategy(*device, strategy);
227     bool ret = reply.WriteInt32(result);
228     if (!ret) {
229         HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
230         return ERR_INVALID_VALUE;
231     }
232     return NO_ERROR;
233 }
234 
GetConnectionStrategyInner(MessageParcel & data,MessageParcel & reply)235 ErrCode BluetoothPbapPceStub::GetConnectionStrategyInner(MessageParcel &data, MessageParcel &reply)
236 {
237     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
238     const BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
239     int result = GetConnectionStrategy(*device);
240     bool ret = reply.WriteInt32(result);
241     if (!ret) {
242         HILOGE("BluetoothPbapPceStub: reply writing failed in: %{public}s.", __func__);
243         return ERR_INVALID_VALUE;
244     }
245     return NO_ERROR;
246 }
247 
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)248 ErrCode BluetoothPbapPceStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
249 {
250     HILOGI("BluetoothPbapPceStub::GetDevicesByStatesInner starts");
251     std::vector<int32_t> state;
252     if (!data.ReadInt32Vector(&state)) {
253         HILOGW("BluetoothPbapPceStub::GetDevicesByStatesInner: get tmpState failed.");
254         return INVALID_DATA;
255     }
256     std::vector<BluetoothRawAddress> rawDevices;
257     GetDevicesByStates(state, rawDevices);
258     reply.WriteInt32(rawDevices.size());
259     int num = rawDevices.size();
260     for (int i = 0; i < num; i++) {
261         bool ret = reply.WriteParcelable(&rawDevices[i]);
262         if (!ret) {
263             HILOGE("WriteParcelable<GetDevicesByStatesInner> failed");
264             return ERR_INVALID_VALUE;
265         }
266     }
267     return NO_ERROR;
268 }
269 
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)270 ErrCode BluetoothPbapPceStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
271 {
272     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
273     sptr<IRemoteObject> remote = data.ReadRemoteObject();
274     const sptr<IBluetoothPbapPceObserver> observer = OHOS::iface_cast<IBluetoothPbapPceObserver>(remote);
275     RegisterObserver(observer);
276 
277     return NO_ERROR;
278 }
279 
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)280 ErrCode BluetoothPbapPceStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply)
281 {
282     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
283     sptr<IRemoteObject> remote = data.ReadRemoteObject();
284     const sptr<IBluetoothPbapPceObserver> observer = OHOS::iface_cast<IBluetoothPbapPceObserver>(remote);
285     DeregisterObserver(observer);
286 
287     return NO_ERROR;
288 }
289 }  // namespace Bluetooth
290 }  // namespace OHOS