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 #include "bluetooth_hfp_hf_proxy.h"
16 #include "bluetooth_errorcode.h"
17 #include "bluetooth_log.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
ConnectSco(const BluetoothRawAddress & device)21 bool BluetoothHfpHfProxy::ConnectSco(const BluetoothRawAddress &device) {
22 MessageParcel data;
23 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
24 HILOGE("BluetoothHfpHfProxy::ConnectSco WriteInterfaceToken error");
25 return false;
26 }
27 if (!data.WriteParcelable(&device)) {
28 HILOGE("BluetoothHfpHfProxy::ConnectSco WriteParcelable error");
29 return false;
30 }
31 MessageParcel reply;
32 MessageOption option {
33 MessageOption::TF_SYNC
34 };
35 int error = Remote()->SendRequest(
36 IBluetoothHfpHf::Code::BT_HFP_HF_CONNECT_SCO, data, reply, option);
37 if (error != NO_ERROR) {
38 HILOGE("BluetoothHfpHfProxy::ConnectSco done fail, error: %{public}d", error);
39 return false;
40 }
41 return reply.ReadBool();
42 }
43
DisconnectSco(const BluetoothRawAddress & device)44 bool BluetoothHfpHfProxy::DisconnectSco(const BluetoothRawAddress &device) {
45 MessageParcel data;
46 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
47 HILOGE("BluetoothHfpHfProxy::DisconnectSco WriteInterfaceToken error");
48 return false;
49 }
50 if (!data.WriteParcelable(&device)) {
51 HILOGE("BluetoothHfpHfProxy::DisconnectSco WriteParcelable error");
52 return false;
53 }
54 MessageParcel reply;
55 MessageOption option {
56 MessageOption::TF_SYNC
57 };
58 int error = Remote()->SendRequest(
59 IBluetoothHfpHf::Code::BT_HFP_HF_DISCONNECT_SCO, data, reply, option);
60 if (error != NO_ERROR) {
61 HILOGE("BluetoothHfpHfProxy::DisconnectSco done fail, error: %{public}d", error);
62 return false;
63 }
64 return reply.ReadBool();
65 }
66
GetDevicesByStates(const std::vector<int> & states,std::vector<BluetoothRawAddress> & devices)67 int BluetoothHfpHfProxy::GetDevicesByStates(const std::vector<int> &states, std::vector<BluetoothRawAddress> &devices) {
68 MessageParcel data;
69 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
70 HILOGE("BluetoothHfpHfProxy::GetDevicesByStates WriteInterfaceToken error");
71 return ERROR;
72 }
73 if (!data.WriteInt32Vector(states)) {
74 HILOGE("BluetoothHfpHfProxy::GetDevicesByStates WriteInt32Vector error");
75 return ERROR;
76 }
77 MessageParcel reply;
78 MessageOption option {
79 MessageOption::TF_SYNC
80 };
81 int error = Remote()->SendRequest(
82 IBluetoothHfpHf::Code::BT_HFP_HF_GET_DEVICES_BY_STATES, data, reply, option);
83 if (error != NO_ERROR) {
84 HILOGE("BluetoothHfpHfProxy::GetDevicesByStates done fail, error: %{public}d", error);
85 return ERROR;
86 }
87 int dev_num = reply.ReadInt32();
88 for (int i = dev_num; i > 0; i--) {
89 std::unique_ptr<BluetoothRawAddress> dev(reply.ReadParcelable<BluetoothRawAddress>());
90 devices.push_back(*dev);
91 }
92 return NO_ERROR;
93 }
94
GetDeviceState(const BluetoothRawAddress & device)95 int BluetoothHfpHfProxy::GetDeviceState(const BluetoothRawAddress &device) {
96 MessageParcel data;
97 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
98 HILOGE("BluetoothHfpHfProxy::GetDeviceState WriteInterfaceToken error");
99 return ERROR;
100 }
101 if (!data.WriteParcelable(&device)) {
102 HILOGE("BluetoothHfpHfProxy::GetDeviceState WriteParcelable error");
103 return ERROR;
104 }
105 MessageParcel reply;
106 MessageOption option {
107 MessageOption::TF_SYNC
108 };
109 int error = Remote()->SendRequest(
110 IBluetoothHfpHf::Code::BT_HFP_HF_GET_DEVICE_STATE, data, reply, option);
111 if (error != NO_ERROR) {
112 HILOGE("BluetoothHfpHfProxy::GetDeviceState done fail, error: %{public}d", error);
113 return ERROR;
114 }
115 return reply.ReadInt32();
116 }
117
GetScoState(const BluetoothRawAddress & device)118 int BluetoothHfpHfProxy::GetScoState(const BluetoothRawAddress &device) {
119 MessageParcel data;
120 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
121 HILOGE("BluetoothHfpHfProxy::GetScoState WriteInterfaceToken error");
122 return ERROR;
123 }
124 if (!data.WriteParcelable(&device)) {
125 HILOGE("BluetoothHfpHfProxy::GetScoState WriteParcelable error");
126 return ERROR;
127 }
128 MessageParcel reply;
129 MessageOption option {
130 MessageOption::TF_SYNC
131 };
132 int error = Remote()->SendRequest(
133 IBluetoothHfpHf::Code::BT_HFP_HF_GET_SCO_STATE, data, reply, option);
134 if (error != NO_ERROR) {
135 HILOGE("BluetoothHfpHfProxy::GetScoState done fail, error: %{public}d", error);
136 return ERROR;
137 }
138 return reply.ReadInt32();
139 }
140
SendDTMFTone(const BluetoothRawAddress & device,uint8_t code)141 bool BluetoothHfpHfProxy::SendDTMFTone(const BluetoothRawAddress &device, uint8_t code) {
142 MessageParcel data;
143 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
144 HILOGE("BluetoothHfpHfProxy::SendDTMFTone WriteInterfaceToken error");
145 return false;
146 }
147 if (!data.WriteParcelable(&device)) {
148 HILOGE("BluetoothHfpHfProxy::SendDTMFTone WriteParcelable error");
149 return false;
150 }
151 if (!data.WriteUint8(code)) {
152 HILOGE("BluetoothHfpHfProxy::SendDTMFTone WriteUint8 error");
153 return false;
154 }
155 MessageParcel reply;
156 MessageOption option {
157 MessageOption::TF_SYNC
158 };
159 int error = Remote()->SendRequest(
160 IBluetoothHfpHf::Code::BT_HFP_HF_SEND_DTMF_TONE, data, reply, option);
161 if (error != NO_ERROR) {
162 HILOGE("BluetoothHfpHfProxy::SendDTMFTone done fail, error: %{public}d", error);
163 return false;
164 }
165 return reply.ReadBool();
166 }
167
Connect(const BluetoothRawAddress & device)168 int BluetoothHfpHfProxy::Connect(const BluetoothRawAddress &device) {
169 MessageParcel data;
170 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
171 HILOGE("BluetoothHfpHfProxy::Connect WriteInterfaceToken error");
172 return ERROR;
173 }
174 if (!data.WriteParcelable(&device)) {
175 HILOGE("BluetoothHfpHfProxy::Connect WriteParcelable error");
176 return ERROR;
177 }
178 MessageParcel reply;
179 MessageOption option {
180 MessageOption::TF_SYNC
181 };
182 int error = Remote()->SendRequest(
183 IBluetoothHfpHf::Code::BT_HFP_HF_CONNECT, data, reply, option);
184 if (error != NO_ERROR) {
185 HILOGE("BluetoothHfpHfProxy::Connect done fail, error: %{public}d", error);
186 return ERROR;
187 }
188 return reply.ReadInt32();
189 }
190
Disconnect(const BluetoothRawAddress & device)191 int BluetoothHfpHfProxy::Disconnect(const BluetoothRawAddress &device) {
192 MessageParcel data;
193 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
194 HILOGE("BluetoothHfpHfProxy::Disconnect WriteInterfaceToken error");
195 return ERROR;
196 }
197 if (!data.WriteParcelable(&device)) {
198 HILOGE("BluetoothHfpHfProxy::Disconnect WriteParcelable error");
199 return ERROR;
200 }
201 MessageParcel reply;
202 MessageOption option {
203 MessageOption::TF_SYNC
204 };
205 int error = Remote()->SendRequest(
206 IBluetoothHfpHf::Code::BT_HFP_HF_DISCONNECT, data, reply, option);
207 if (error != NO_ERROR) {
208 HILOGE("BluetoothHfpHfProxy::Disconnect done fail, error: %{public}d", error);
209 return ERROR;
210 }
211 return reply.ReadInt32();
212 }
213
OpenVoiceRecognition(const BluetoothRawAddress & device)214 bool BluetoothHfpHfProxy::OpenVoiceRecognition(const BluetoothRawAddress &device) {
215 MessageParcel data;
216 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
217 HILOGE("BluetoothHfpHfProxy::OpenVoiceRecognition WriteInterfaceToken error");
218 return false;
219 }
220 if (!data.WriteParcelable(&device)) {
221 HILOGE("BluetoothHfpHfProxy::OpenVoiceRecognition WriteParcelable error");
222 return false;
223 }
224 MessageParcel reply;
225 MessageOption option {
226 MessageOption::TF_SYNC
227 };
228 int error = Remote()->SendRequest(
229 IBluetoothHfpHf::Code::BT_HFP_HF_OPEN_VOICE_RECOGNITION, data, reply, option);
230 if (error != NO_ERROR) {
231 HILOGE("BluetoothHfpHfProxy::OpenVoiceRecognition done fail, error: %{public}d", error);
232 return false;
233 }
234 return reply.ReadBool();
235 }
236
CloseVoiceRecognition(const BluetoothRawAddress & device)237 bool BluetoothHfpHfProxy::CloseVoiceRecognition(const BluetoothRawAddress &device) {
238 MessageParcel data;
239 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
240 HILOGE("BluetoothHfpHfProxy::CloseVoiceRecognition WriteInterfaceToken error");
241 return false;
242 }
243 if (!data.WriteParcelable(&device)) {
244 HILOGE("BluetoothHfpHfProxy::CloseVoiceRecognition WriteParcelable error");
245 return false;
246 }
247 MessageParcel reply;
248 MessageOption option {
249 MessageOption::TF_SYNC
250 };
251 int error = Remote()->SendRequest(
252 IBluetoothHfpHf::Code::BT_HFP_HF_CLOSE_VOICE_RECOGNITION, data, reply, option);
253 if (error != NO_ERROR) {
254 HILOGE("BluetoothHfpHfProxy::CloseVoiceRecognition done fail, error: %{public}d", error);
255 return false;
256 }
257 return reply.ReadBool();
258 }
259
GetCurrentCallList(const BluetoothRawAddress & device,std::vector<BluetoothHfpHfCall> & calls)260 int BluetoothHfpHfProxy::GetCurrentCallList(const BluetoothRawAddress &device, std::vector<BluetoothHfpHfCall> &calls) {
261 MessageParcel data;
262 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
263 HILOGE("BluetoothHfpHfProxy::GetCurrentCallList WriteInterfaceToken error");
264 return ERROR;
265 }
266 if (!data.WriteParcelable(&device)) {
267 HILOGE("BluetoothHfpHfProxy::GetCurrentCallList WriteParcelable error");
268 return ERROR;
269 }
270 MessageParcel reply;
271 MessageOption option {
272 MessageOption::TF_SYNC
273 };
274 int error = Remote()->SendRequest(
275 IBluetoothHfpHf::Code::BT_HFP_HF_GET_CURRENT_CALL_LIST, data, reply, option);
276 if (error != NO_ERROR) {
277 HILOGE("BluetoothHfpHfProxy::GetCurrentCallList done fail, error: %{public}d", error);
278 return ERROR;
279 }
280 int call_num = reply.ReadInt32();
281 for (int i = call_num; i > 0; i--) {
282 std::unique_ptr<BluetoothHfpHfCall> call(reply.ReadParcelable<BluetoothHfpHfCall>());
283 calls.push_back(*call);
284 }
285 return NO_ERROR;
286 }
287
AcceptIncomingCall(const BluetoothRawAddress & device,int flag)288 bool BluetoothHfpHfProxy::AcceptIncomingCall(const BluetoothRawAddress &device, int flag) {
289 MessageParcel data;
290 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
291 HILOGE("BluetoothHfpHfProxy::AcceptIncomingCall WriteInterfaceToken error");
292 return false;
293 }
294 if (!data.WriteParcelable(&device)) {
295 HILOGE("BluetoothHfpHfProxy::AcceptIncomingCall WriteParcelable error");
296 return false;
297 }
298 if (!data.WriteInt32(flag)) {
299 HILOGE("BluetoothHfpHfProxy::AcceptIncomingCall WriteInt32 error");
300 return false;
301 }
302 MessageParcel reply;
303 MessageOption option {
304 MessageOption::TF_SYNC
305 };
306 int error = Remote()->SendRequest(
307 IBluetoothHfpHf::Code::BT_HFP_HF_ACCEPT_INCOMING_CALL, data, reply, option);
308 if (error != NO_ERROR) {
309 HILOGE("BluetoothHfpHfProxy::AcceptIncomingCall done fail, error: %{public}d", error);
310 return false;
311 }
312 return reply.ReadBool();
313 }
314
HoldActiveCall(const BluetoothRawAddress & device)315 bool BluetoothHfpHfProxy::HoldActiveCall(const BluetoothRawAddress &device) {
316 MessageParcel data;
317 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
318 HILOGE("BluetoothHfpHfProxy::HoldActiveCall WriteInterfaceToken error");
319 return false;
320 }
321 if (!data.WriteParcelable(&device)) {
322 HILOGE("BluetoothHfpHfProxy::HoldActiveCall WriteParcelable error");
323 return false;
324 }
325 MessageParcel reply;
326 MessageOption option {
327 MessageOption::TF_SYNC
328 };
329 int error = Remote()->SendRequest(
330 IBluetoothHfpHf::Code::BT_HFP_HF_HOLD_ACTIVE_CALL, data, reply, option);
331 if (error != NO_ERROR) {
332 HILOGE("BluetoothHfpHfProxy::HoldActiveCall done fail, error: %{public}d", error);
333 return false;
334 }
335 return reply.ReadBool();
336 }
337
RejectIncomingCall(const BluetoothRawAddress & device)338 bool BluetoothHfpHfProxy::RejectIncomingCall(const BluetoothRawAddress &device) {
339 MessageParcel data;
340 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
341 HILOGE("BluetoothHfpHfProxy::RejectIncomingCall WriteInterfaceToken error");
342 return false;
343 }
344 if (!data.WriteParcelable(&device)) {
345 HILOGE("BluetoothHfpHfProxy::RejectIncomingCall WriteParcelable error");
346 return false;
347 }
348 MessageParcel reply;
349 MessageOption option {
350 MessageOption::TF_SYNC
351 };
352 int error = Remote()->SendRequest(
353 IBluetoothHfpHf::Code::BT_HFP_HF_REJECT_INCOMING_CALL, data, reply, option);
354 if (error != NO_ERROR) {
355 HILOGE("BluetoothHfpHfProxy::RejectIncomingCall done fail, error: %{public}d", error);
356 return false;
357 }
358 return reply.ReadBool();
359 }
360
FinishActiveCall(const BluetoothRawAddress & device,const BluetoothHfpHfCall & call)361 bool BluetoothHfpHfProxy::FinishActiveCall(const BluetoothRawAddress &device, const BluetoothHfpHfCall &call) {
362 MessageParcel data;
363 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
364 HILOGE("BluetoothHfpHfProxy::FinishActiveCall WriteInterfaceToken error");
365 return false;
366 }
367 if (!data.WriteParcelable(&device)) {
368 HILOGE("BluetoothHfpHfProxy::FinishActiveCall WriteParcelable error");
369 return false;
370 }
371 if (!data.WriteParcelable(&call)) {
372 HILOGE("BluetoothHfpHfProxy::FinishActiveCall WriteParcelable error");
373 return false;
374 }
375 MessageParcel reply;
376 MessageOption option {
377 MessageOption::TF_SYNC
378 };
379 int error = Remote()->SendRequest(
380 IBluetoothHfpHf::Code::BT_HFP_HF_FINISH_ATIVE_CALL, data, reply, option);
381 if (error != NO_ERROR) {
382 HILOGE("BluetoothHfpHfProxy::FinishActiveCall done fail, error: %{public}d", error);
383 return false;
384 }
385 return reply.ReadBool();
386 }
387
StartDial(const BluetoothRawAddress & device,const std::string & number,BluetoothHfpHfCall & call)388 int BluetoothHfpHfProxy::StartDial(const BluetoothRawAddress &device, const std::string &number,
389 BluetoothHfpHfCall &call) {
390 MessageParcel data;
391 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
392 HILOGE("BluetoothHfpHfProxy::StartDial WriteInterfaceToken error");
393 return ERROR;
394 }
395 if (!data.WriteParcelable(&device)) {
396 HILOGE("BluetoothHfpHfProxy::StartDial WriteParcelable error");
397 return ERROR;
398 }
399 if (!data.WriteString(number)) {
400 HILOGE("BluetoothHfpHfProxy::StartDial WriteString error");
401 return ERROR;
402 }
403 if (!data.WriteParcelable(&call)) {
404 HILOGE("BluetoothHfpHfProxy::StartDial WriteParcelable error");
405 return ERROR;
406 }
407 MessageParcel reply;
408 MessageOption option {
409 MessageOption::TF_SYNC
410 };
411 int error = Remote()->SendRequest(
412 IBluetoothHfpHf::Code::BT_HFP_HF_START_DIAL, data, reply, option);
413 if (error != NO_ERROR) {
414 HILOGE("BluetoothHfpHfProxy::StartDial done fail, error: %{public}d", error);
415 return ERROR;
416 }
417 return reply.ReadInt32();
418 }
419
RegisterObserver(const sptr<IBluetoothHfpHfObserver> & observer)420 void BluetoothHfpHfProxy::RegisterObserver(const sptr<IBluetoothHfpHfObserver> &observer) {
421 MessageParcel data;
422 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
423 HILOGE("BluetoothHfpHfProxy::RegisterObserver WriteInterfaceToken error");
424 return;
425 }
426 if (!data.WriteRemoteObject(observer->AsObject())) {
427 HILOGE("BluetoothHfpHfProxy::RegisterObserver WriteRemoteObject error");
428 return;
429 }
430 MessageParcel reply;
431 MessageOption option {
432 MessageOption::TF_ASYNC
433 };
434 int error = Remote()->SendRequest(
435 IBluetoothHfpHf::Code::BT_HFP_HF_REGISTER_OBSERVER, data, reply, option);
436 if (error != NO_ERROR) {
437 HILOGE("BluetoothHfpHfProxy::RegisterObserver done fail, error: %{public}d", error);
438 return;
439 }
440 }
441
DeregisterObserver(const sptr<IBluetoothHfpHfObserver> & observer)442 void BluetoothHfpHfProxy::DeregisterObserver(const sptr<IBluetoothHfpHfObserver> &observer) {
443 MessageParcel data;
444 if (!data.WriteInterfaceToken(BluetoothHfpHfProxy::GetDescriptor())) {
445 HILOGE("BluetoothHfpHfProxy::DeregisterObserver WriteInterfaceToken error");
446 return;
447 }
448 if (!data.WriteRemoteObject(observer->AsObject())) {
449 HILOGE("BluetoothHfpHfProxy::DeregisterObserver WriteRemoteObject error");
450 return;
451 }
452 MessageParcel reply;
453 MessageOption option {
454 MessageOption::TF_ASYNC
455 };
456 int error = Remote()->SendRequest(
457 IBluetoothHfpHf::Code::BT_HFP_HF_DEREGISTER_OBSERVER, data, reply, option);
458 if (error != NO_ERROR) {
459 HILOGE("BluetoothHfpHfProxy::DeregisterObserver done fail, error: %{public}d", error);
460 return;
461 }
462 }
463
464 } // namespace Bluetooth
465 } // namespace OHOS
466