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