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_hfp_ag_proxy.h"
17 #include "bluetooth_errorcode.h"
18 #include "bluetooth_log.h"
19
20 namespace OHOS {
21 namespace Bluetooth {
GetConnectDevices(std::vector<BluetoothRawAddress> & devices)22 int32_t BluetoothHfpAgProxy::GetConnectDevices(std::vector<BluetoothRawAddress> &devices)
23 {
24 MessageParcel data;
25 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
26 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
27
28 MessageParcel reply;
29 MessageOption option(MessageOption::TF_SYNC);
30
31 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_GET_CONNECT_DEVICES,
32 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
33
34 uint32_t DevNum = reply.ReadUint32();
35 for (uint32_t i = 0; i < DevNum; i++) {
36 std::shared_ptr<BluetoothRawAddress> dev(reply.ReadParcelable<BluetoothRawAddress>());
37 if (!dev) {
38 return BT_ERR_IPC_TRANS_FAILED;
39 }
40 devices.push_back(*dev);
41 }
42 return BT_NO_ERROR;
43 }
44
GetDevicesByStates(const std::vector<int> & states,std::vector<BluetoothRawAddress> & devices)45 int BluetoothHfpAgProxy::GetDevicesByStates(const std::vector<int> &states, std::vector<BluetoothRawAddress> &devices)
46 {
47 MessageParcel data;
48 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
49 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
50 CHECK_AND_RETURN_LOG_RET(data.WriteInt32Vector(states), BT_ERR_IPC_TRANS_FAILED, "write states error");
51
52 MessageParcel reply;
53 MessageOption option(MessageOption::TF_SYNC);
54
55 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_GET_DEVICES_BY_STATES,
56 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
57
58 uint32_t DevNum = reply.ReadUint32();
59 for (uint32_t i = 0; i < DevNum; i++) {
60 std::shared_ptr<BluetoothRawAddress> dev(reply.ReadParcelable<BluetoothRawAddress>());
61 if (!dev) {
62 return BT_ERR_IPC_TRANS_FAILED;
63 }
64 devices.push_back(*dev);
65 }
66 return BT_NO_ERROR;
67 }
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)68 int32_t BluetoothHfpAgProxy::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
69 {
70 MessageParcel data;
71 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
72 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
73 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
74
75 MessageParcel reply;
76 MessageOption option(MessageOption::TF_SYNC);
77
78 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_GET_DEVICE_STATE,
79 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
80
81 // read error code
82 int32_t errCode = reply.ReadInt32();
83 if (errCode != BT_NO_ERROR) {
84 HILOGE("reply errCode: %{public}d", errCode);
85 return errCode;
86 }
87 // read state
88 state = reply.ReadInt32();
89 return BT_NO_ERROR;
90 }
91
Connect(const BluetoothRawAddress & device)92 int32_t BluetoothHfpAgProxy::Connect(const BluetoothRawAddress &device)
93 {
94 MessageParcel data;
95 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
96 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
97 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
98
99 MessageParcel reply;
100 MessageOption option(MessageOption::TF_SYNC);
101
102 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_CONNECT,
103 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
104
105 return reply.ReadInt32();
106 }
107
Disconnect(const BluetoothRawAddress & device)108 int32_t BluetoothHfpAgProxy::Disconnect(const BluetoothRawAddress &device)
109 {
110 MessageParcel data;
111 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
112 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
113 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
114
115 MessageParcel reply;
116 MessageOption option(MessageOption::TF_SYNC);
117
118 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_DISCONNECT,
119 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
120
121 return reply.ReadInt32();
122 }
123
GetScoState(const BluetoothRawAddress & device)124 int BluetoothHfpAgProxy::GetScoState(const BluetoothRawAddress &device)
125 {
126 MessageParcel data;
127 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
128 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
129 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
130
131 MessageParcel reply;
132 MessageOption option(MessageOption::TF_SYNC);
133
134 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_GET_SCO_STATE,
135 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
136
137 return reply.ReadInt32();
138 }
139
ConnectSco(uint8_t callType)140 int32_t BluetoothHfpAgProxy::ConnectSco(uint8_t callType)
141 {
142 MessageParcel data;
143 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
144 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
145 CHECK_AND_RETURN_LOG_RET(data.WriteUint8(callType), BT_ERR_IPC_TRANS_FAILED, "write callType error");
146
147 MessageParcel reply;
148 MessageOption option(MessageOption::TF_SYNC);
149
150 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_CONNECT_SCO_EX,
151 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
152
153 return reply.ReadInt32();
154 }
155
DisconnectSco(uint8_t callType)156 int32_t BluetoothHfpAgProxy::DisconnectSco(uint8_t callType)
157 {
158 MessageParcel data;
159 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
160 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
161 CHECK_AND_RETURN_LOG_RET(data.WriteUint8(callType), BT_ERR_IPC_TRANS_FAILED, "write callType error");
162
163 MessageParcel reply;
164 MessageOption option(MessageOption::TF_SYNC);
165
166 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_DISCONNECT_SCO_EX,
167 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
168
169 return reply.ReadInt32();
170 }
171
ConnectSco()172 bool BluetoothHfpAgProxy::ConnectSco()
173 {
174 MessageParcel data;
175 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
176 false, "WriteInterfaceToken error");
177
178 MessageParcel reply;
179 MessageOption option(MessageOption::TF_SYNC);
180 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_CONNECT_SCO, data, reply, option, false);
181
182 return reply.ReadBool();
183 }
184
DisconnectSco()185 bool BluetoothHfpAgProxy::DisconnectSco()
186 {
187 MessageParcel data;
188 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
189 false, "WriteInterfaceToken error");
190
191 MessageParcel reply;
192 MessageOption option(MessageOption::TF_SYNC);
193
194 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_DISCONNECT_SCO, data, reply, option, false);
195
196 return reply.ReadBool();
197 }
198
PhoneStateChanged(BluetoothPhoneState & phoneState)199 void BluetoothHfpAgProxy::PhoneStateChanged(BluetoothPhoneState &phoneState)
200 {
201 MessageParcel data;
202 CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()), "WriteInterfaceToken error");
203 CHECK_AND_RETURN_LOG(data.WriteParcelable(&phoneState), "write phoneState error");
204 MessageParcel reply;
205 MessageOption option(MessageOption::TF_ASYNC);
206
207 SEND_IPC_REQUEST_RETURN(BluetoothHfpAgInterfaceCode::BT_HFP_AG_PHONE_STATE_CHANGED, data, reply, option);
208 }
209
ClccResponse(int index,int direction,int status,int mode,bool mpty,const std::string & number,int type)210 void BluetoothHfpAgProxy::ClccResponse(
211 int index, int direction, int status, int mode, bool mpty, const std::string &number, int type)
212 {
213 MessageParcel data;
214 CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()), "WriteInterfaceToken error");
215 CHECK_AND_RETURN_LOG(data.WriteInt32(index), "write index error");
216 CHECK_AND_RETURN_LOG(data.WriteInt32(direction), "write direction error");
217 CHECK_AND_RETURN_LOG(data.WriteInt32(status), "write status error");
218 CHECK_AND_RETURN_LOG(data.WriteInt32(mode), "write mode error");
219 CHECK_AND_RETURN_LOG(data.WriteBool(mpty), "write mpty error");
220 CHECK_AND_RETURN_LOG(data.WriteString(number), "write number error");
221 CHECK_AND_RETURN_LOG(data.WriteInt32(type), "write type error");
222
223 MessageParcel reply;
224 MessageOption option(MessageOption::TF_ASYNC);
225
226 SEND_IPC_REQUEST_RETURN(BluetoothHfpAgInterfaceCode::BT_HFP_AG_CLCC_RESPONSE, data, reply, option);
227 }
228
OpenVoiceRecognition(const BluetoothRawAddress & device)229 bool BluetoothHfpAgProxy::OpenVoiceRecognition(const BluetoothRawAddress &device)
230 {
231 MessageParcel data;
232 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::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(BluetoothHfpAgInterfaceCode::BT_HFP_AG_OPEN_VOICE_RECOGNITION,
240 data, reply, option, false);
241
242 return reply.ReadBool();
243 }
244
CloseVoiceRecognition(const BluetoothRawAddress & device)245 bool BluetoothHfpAgProxy::CloseVoiceRecognition(const BluetoothRawAddress &device)
246 {
247 MessageParcel data;
248 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
249 false, "WriteInterfaceToken error");
250 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), false, "write device error");
251
252 MessageParcel reply;
253 MessageOption option(MessageOption::TF_SYNC);
254
255 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_CLOSE_VOICE_RECOGNITION,
256 data, reply, option, false);
257
258 return reply.ReadBool();
259 }
260
SetActiveDevice(const BluetoothRawAddress & device)261 bool BluetoothHfpAgProxy::SetActiveDevice(const BluetoothRawAddress &device)
262 {
263 MessageParcel data;
264 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
265 false, "WriteInterfaceToken error");
266 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), false, "write device error");
267
268 MessageParcel reply;
269 MessageOption option(MessageOption::TF_SYNC);
270
271 SEND_IPC_REQUEST_RETURN_RESULT(
272 BluetoothHfpAgInterfaceCode::BT_HFP_AG_SET_ACTIVE_DEVICE, data, reply, option, false);
273
274 return reply.ReadBool();
275 }
276
IntoMock(const BluetoothRawAddress & device,int state)277 bool BluetoothHfpAgProxy::IntoMock(const BluetoothRawAddress &device, int state)
278 {
279 MessageParcel data;
280 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::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(state), false, "write state error");
284
285 MessageParcel reply;
286 MessageOption option(MessageOption::TF_SYNC);
287
288 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_INTO_MOCK, data, reply, option, false);
289
290 return reply.ReadBool();
291 }
292
SendNoCarrier(const BluetoothRawAddress & device)293 bool BluetoothHfpAgProxy::SendNoCarrier(const BluetoothRawAddress &device)
294 {
295 MessageParcel data;
296 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
297 false, "WriteInterfaceToken error");
298 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), false, "write device error");
299
300 MessageParcel reply;
301 MessageOption option(MessageOption::TF_SYNC);
302
303 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_SEND_NO_CARRIER, data, reply, option, false);
304
305 return reply.ReadBool();
306 }
307
GetActiveDevice()308 std::string BluetoothHfpAgProxy::GetActiveDevice()
309 {
310 MessageParcel data;
311 CHECK_AND_RETURN_LOG_RET(
312 data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()), "", "WriteInterfaceToken error");
313
314 MessageParcel reply;
315 MessageOption option(MessageOption::TF_SYNC);
316
317 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_GET_ACTIVE_DEVICE, data, reply, option, "");
318
319 return reply.ReadString();
320 }
321
SetConnectStrategy(const BluetoothRawAddress & device,int strategy)322 int BluetoothHfpAgProxy::SetConnectStrategy(const BluetoothRawAddress &device, int strategy)
323 {
324 MessageParcel data;
325 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
326 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
327 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
328 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(strategy), BT_ERR_IPC_TRANS_FAILED, "write strategy error");
329
330 MessageParcel reply;
331 MessageOption option(MessageOption::TF_SYNC);
332
333 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_SET_CONNECT_STRATEGY,
334 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
335
336 return reply.ReadInt32();
337 }
338
GetConnectStrategy(const BluetoothRawAddress & device,int & strategy)339 int BluetoothHfpAgProxy::GetConnectStrategy(const BluetoothRawAddress &device, int &strategy)
340 {
341 MessageParcel data;
342 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
343 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
344 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
345
346 MessageParcel reply;
347 MessageOption option(MessageOption::TF_SYNC);
348
349 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_GET_CONNECT_STRATEGY,
350 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
351
352 int32_t res = reply.ReadInt32();
353 if (res == NO_ERROR) {
354 strategy = reply.ReadInt32();
355 }
356
357 return res;
358 }
359
IsInbandRingingEnabled(bool & isEnabled)360 int BluetoothHfpAgProxy::IsInbandRingingEnabled(bool &isEnabled)
361 {
362 MessageParcel data;
363 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()),
364 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
365
366 MessageParcel reply;
367 MessageOption option(MessageOption::TF_SYNC);
368
369 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothHfpAgInterfaceCode::BT_HFP_AG_IS_IN_BAND_RINGING_ENABLE,
370 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
371
372 int ret = reply.ReadInt32();
373 isEnabled = reply.ReadBool();
374 return ret;
375 }
376
RegisterObserver(const sptr<IBluetoothHfpAgObserver> & observer)377 void BluetoothHfpAgProxy::RegisterObserver(const sptr<IBluetoothHfpAgObserver> &observer)
378 {
379 MessageParcel data;
380 CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()), "WriteInterfaceToken error");
381 CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "write object error");
382
383 MessageParcel reply;
384 MessageOption option(MessageOption::TF_ASYNC);
385
386 SEND_IPC_REQUEST_RETURN(BluetoothHfpAgInterfaceCode::BT_HFP_AG_REGISTER_OBSERVER, data, reply, option);
387 }
388
DeregisterObserver(const sptr<IBluetoothHfpAgObserver> & observer)389 void BluetoothHfpAgProxy::DeregisterObserver(const sptr<IBluetoothHfpAgObserver> &observer)
390 {
391 MessageParcel data;
392 CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothHfpAgProxy::GetDescriptor()), "WriteInterfaceToken error");
393 CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "write object error");
394
395 MessageParcel reply;
396 MessageOption option(MessageOption::TF_ASYNC);
397
398 SEND_IPC_REQUEST_RETURN(BluetoothHfpAgInterfaceCode::BT_HFP_AG_DEREGISTER_OBSERVER, data, reply, option);
399 }
400 } // namespace Bluetooth
401 } // namespace OHOS
402