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_pse_proxy.h"
17 #include "bluetooth_log.h"
18 #include "i_bluetooth_pbap_pse.h"
19 #include "bluetooth_errorcode.h"
20
21 namespace OHOS {
22 namespace Bluetooth {
GetDeviceState(const BluetoothRawAddress & device)23 int BluetoothPbapPseProxy::GetDeviceState(const BluetoothRawAddress& device)
24 {
25 HILOGI("Enter!");
26 MessageParcel data;
27 if (!data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor())) {
28 HILOGE("GetDeviceState WriteInterfaceToken error");
29 return ERROR;
30 }
31
32 if (!data.WriteParcelable(&device)) {
33 HILOGE("GetDeviceState error");
34 return ERROR;
35 }
36
37 MessageParcel reply;
38 MessageOption option {
39 MessageOption::TF_SYNC
40 };
41
42 int error = Remote()->SendRequest(BluetoothPbapPseInterfaceCode::PBAP_PSE_GET_DEVICE_STATE, data, reply, option);
43 if (error != NO_ERROR) {
44 HILOGE("GetDeviceState done fail, error: %{public}d", error);
45 return ERROR;
46 }
47 return reply.ReadInt32();
48 }
49
GetDevicesByStates(const std::vector<int32_t> tmpStates,std::vector<BluetoothRawAddress> & rawDevices)50 void BluetoothPbapPseProxy::GetDevicesByStates(
51 const std::vector<int32_t> tmpStates, std::vector<BluetoothRawAddress> &rawDevices)
52 {
53 HILOGI("start");
54 MessageParcel data;
55 if (!data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor())) {
56 HILOGE("GetServices WriteInterfaceToken error");
57 return;
58 }
59 if (!data.WriteInt32Vector(tmpStates)) {
60 HILOGE("GetServices transport error");
61 return;
62 }
63 MessageParcel reply;
64 MessageOption option {
65 MessageOption::TF_SYNC
66 };
67 int error = Remote()->SendRequest(
68 BluetoothPbapPseInterfaceCode::PBAP_PSE_GET_DEVICES_BY_STATES, data, reply, option);
69 if (error != NO_ERROR) {
70 HILOGE("GetServices done fail, error: %{public}d", error);
71 }
72 int DevNum = reply.ReadInt32();
73 for (int i = DevNum; i > 0; i--) {
74 std::shared_ptr<BluetoothRawAddress> dev(reply.ReadParcelable<BluetoothRawAddress>());
75 if (!dev) {
76 return;
77 }
78 rawDevices.push_back(*dev);
79 }
80 }
81
Disconnect(const BluetoothRawAddress & device)82 int BluetoothPbapPseProxy::Disconnect(const BluetoothRawAddress& device)
83 {
84 HILOGI("Enter!");
85 MessageParcel data;
86 if (!data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor())) {
87 HILOGE("Disconnect WriteInterfaceToken error");
88 return ERROR;
89 }
90
91 if (!data.WriteParcelable(&device)) {
92 HILOGE("Disconnect error");
93 return ERROR;
94 }
95
96 MessageParcel reply;
97 MessageOption option {
98 MessageOption::TF_SYNC
99 };
100
101 int error = Remote()->SendRequest(BluetoothPbapPseInterfaceCode::PBAP_PSE_DISCONNECT, data, reply, option);
102 if (error != NO_ERROR) {
103 HILOGE("Disconnect done fail, error: %{public}d", error);
104 return ERROR;
105 }
106 return reply.ReadInt32();
107 }
108
SetConnectionStrategy(const BluetoothRawAddress & device,int32_t strategy)109 int BluetoothPbapPseProxy::SetConnectionStrategy(const BluetoothRawAddress& device, int32_t strategy)
110 {
111 HILOGI("Enter!");
112 MessageParcel data;
113 if (!data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor())) {
114 HILOGE("SetConnectionStrategy WriteInterfaceToken error");
115 return ERROR;
116 }
117
118 if (!data.WriteParcelable(&device)) {
119 HILOGE("SetConnectionStrategy error");
120 return ERROR;
121 }
122
123 if (!data.WriteInt32(strategy)) {
124 HILOGE("SetConnectionStrategy error");
125 return ERROR;
126 }
127
128 MessageParcel reply;
129 MessageOption option {
130 MessageOption::TF_SYNC
131 };
132
133 int error = Remote()->SendRequest(
134 BluetoothPbapPseInterfaceCode::PBAP_PSE_SET_CONNECTION_STRATEGY, data, reply, option);
135 if (error != NO_ERROR) {
136 HILOGE("SetConnectionStrategy done fail, error: %{public}d", error);
137 return ERROR;
138 }
139 return reply.ReadInt32();
140 }
141
GetConnectionStrategy(const BluetoothRawAddress & device)142 int BluetoothPbapPseProxy::GetConnectionStrategy(const BluetoothRawAddress& device)
143 {
144 HILOGI("Enter!");
145 MessageParcel data;
146 if (!data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor())) {
147 HILOGE("GetConnectionStrategy WriteInterfaceToken error");
148 return ERROR;
149 }
150
151 if (!data.WriteParcelable(&device)) {
152 HILOGE("GetConnectionStrategy error");
153 return ERROR;
154 }
155
156 MessageParcel reply;
157 MessageOption option {
158 MessageOption::TF_SYNC
159 };
160
161 int error = Remote()->SendRequest(
162 BluetoothPbapPseInterfaceCode::PBAP_PSE_GET_CONNECTION_STRATEGY, data, reply, option);
163 if (error != NO_ERROR) {
164 HILOGE("GetConnectionStrategy done fail, error: %{public}d", error);
165 return ERROR;
166 }
167 return reply.ReadInt32();
168 }
169
GrantPermission(const BluetoothRawAddress & device,bool allow,bool save)170 void BluetoothPbapPseProxy::GrantPermission(const BluetoothRawAddress& device, bool allow, bool save)
171 {
172 HILOGI("Enter!");
173 MessageParcel data;
174 if (!data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor())) {
175 HILOGE("GrantPermission WriteInterfaceToken error");
176 return;
177 }
178
179 if (!data.WriteParcelable(&device)) {
180 HILOGE("GrantPermission error");
181 return;
182 }
183
184 if (!data.WriteBool(allow)) {
185 HILOGE("GrantPermission error");
186 return;
187 }
188
189 if (!data.WriteBool(save)) {
190 HILOGE("GrantPermission error");
191 return;
192 }
193
194 MessageParcel reply;
195 MessageOption option {
196 MessageOption::TF_SYNC
197 };
198
199 int error = Remote()->SendRequest(BluetoothPbapPseInterfaceCode::PBAP_PSE_GRANT_PERMISSION, data, reply, option);
200 if (error != NO_ERROR) {
201 HILOGE("GrantPermission done fail, error: %{public}d", error);
202 return;
203 }
204 }
205
SetDevicePassword(const BluetoothRawAddress & device,const std::string & password,const std::string & userId)206 int BluetoothPbapPseProxy::SetDevicePassword(
207 const BluetoothRawAddress &device, const std::string &password, const std::string &userId)
208 {
209 HILOGI("Enter!");
210 MessageParcel data;
211 if (!data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor())) {
212 HILOGE("SetDevicePassword WriteInterfaceToken error");
213 return ERROR;
214 }
215
216 if (!data.WriteParcelable(&device)) {
217 HILOGE("SetDevicePassword error");
218 return ERROR;
219 }
220
221 if (!data.WriteString(password)) {
222 HILOGE("SetDevicePassword error");
223 return ERROR;
224 }
225
226 if (!data.WriteString(userId)) {
227 HILOGE("SetDevicePassword error");
228 return ERROR;
229 }
230
231 MessageParcel reply;
232 MessageOption option {
233 MessageOption::TF_SYNC
234 };
235
236 int error = Remote()->SendRequest(BluetoothPbapPseInterfaceCode::PBAP_PSE_SET_DEVICE_PASSWORD, data, reply, option);
237 if (error != NO_ERROR) {
238 HILOGE("SetDevicePassword done fail, error: %{public}d", error);
239 return ERROR;
240 }
241 return reply.ReadInt32();
242 }
243
RegisterObserver(const sptr<IBluetoothPbapPseObserver> & observer)244 void BluetoothPbapPseProxy::RegisterObserver(const sptr<IBluetoothPbapPseObserver> &observer)
245 {
246 HILOGI("Enter!");
247 MessageParcel data;
248 if (!data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor())) {
249 HILOGE("RegisterObserver WriteInterfaceToken error");
250 return;
251 }
252 if (!data.WriteRemoteObject(observer->AsObject())) {
253 HILOGE("RegisterObserver error");
254 return;
255 }
256
257 MessageParcel reply;
258 MessageOption option {
259 MessageOption::TF_SYNC
260 };
261 int error = Remote()->SendRequest(BluetoothPbapPseInterfaceCode::PBAP_PSE_REGISTER_OBSERVER, data, reply, option);
262 if (error != NO_ERROR) {
263 HILOGE("RegisterObserver done fail, error: %{public}d", error);
264 return;
265 }
266 return;
267 }
268
DeregisterObserver(const sptr<IBluetoothPbapPseObserver> & observer)269 void BluetoothPbapPseProxy::DeregisterObserver(const sptr<IBluetoothPbapPseObserver> &observer)
270 {
271 HILOGI("Enter!");
272 MessageParcel data;
273 if (!data.WriteInterfaceToken(BluetoothPbapPseProxy::GetDescriptor())) {
274 HILOGE("RegisterObserver WriteInterfaceToken error");
275 return;
276 }
277 if (!data.WriteRemoteObject(observer->AsObject())) {
278 HILOGE("RegisterObserver error");
279 return;
280 }
281
282 MessageParcel reply;
283 MessageOption option {
284 MessageOption::TF_SYNC
285 };
286 int error = Remote()->SendRequest(BluetoothPbapPseInterfaceCode::PBAP_PSE_DEREGISTER_OBSERVER, data, reply, option);
287 if (error != NO_ERROR) {
288 HILOGE("RegisterObserver done fail, error: %{public}d", error);
289 return;
290 }
291 return;
292 }
293 } // namespace Bluetooth
294 } // namespace OHOS