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