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