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_stub.h"
17
18 #include "bluetooth_errorcode.h"
19 #include "bluetooth_log.h"
20
21 namespace OHOS {
22 namespace Bluetooth {
BluetoothHfpAgStub()23 BluetoothHfpAgStub::BluetoothHfpAgStub() {
24 HILOGI("%{public}s start.", __func__);
25 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_GET_CONNECT_DEVICES)] =
26 &BluetoothHfpAgStub::GetConnectDevicesInner;
27 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_GET_DEVICES_BY_STATES)] =
28 &BluetoothHfpAgStub::GetDevicesByStatesInner;
29 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_GET_DEVICE_STATE)] =
30 &BluetoothHfpAgStub::GetDeviceStateInner;
31 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_CONNECT)] =
32 &BluetoothHfpAgStub::ConnectInner;
33 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_DISCONNECT)] =
34 &BluetoothHfpAgStub::DisconnectInner;
35 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_GET_SCO_STATE)] =
36 &BluetoothHfpAgStub::GetScoStateInner;
37 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_CONNECT_SCO)] =
38 &BluetoothHfpAgStub::ConnectScoInner;
39 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_DISCONNECT_SCO)] =
40 &BluetoothHfpAgStub::DisconnectScoInner;
41 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_PHONE_STATE_CHANGED)] =
42 &BluetoothHfpAgStub::PhoneStateChangedInner;
43 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_CLCC_RESPONSE)] =
44 &BluetoothHfpAgStub::ClccResponseInner;
45 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_OPEN_VOICE_RECOGNITION)] =
46 &BluetoothHfpAgStub::OpenVoiceRecognitionInner;
47 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_CLOSE_VOICE_RECOGNITION)] =
48 &BluetoothHfpAgStub::CloseVoiceRecognitionInner;
49 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_SET_ACTIVE_DEVICE)] =
50 &BluetoothHfpAgStub::SetActiveDeviceInner;
51 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_GET_ACTIVE_DEVICE)] =
52 &BluetoothHfpAgStub::GetActiveDeviceInner;
53 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_INTO_MOCK)] =
54 &BluetoothHfpAgStub::IntoMockInner;
55 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_SEND_NO_CARRIER)] =
56 &BluetoothHfpAgStub::SendNoCarrierInner;
57 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_REGISTER_OBSERVER)] =
58 &BluetoothHfpAgStub::RegisterObserverInner;
59 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_DEREGISTER_OBSERVER)] =
60 &BluetoothHfpAgStub::DeregisterObserverInner;
61 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_SET_CONNECT_STRATEGY)] =
62 &BluetoothHfpAgStub::SetConnectStrategyInner;
63 memberFuncMap_[static_cast<uint32_t>(BluetoothHfpAgInterfaceCode::BT_HFP_AG_GET_CONNECT_STRATEGY)] =
64 &BluetoothHfpAgStub::GetConnectStrategyInner;
65
66 HILOGI("%{public}s ends.", __func__);
67 }
68
~BluetoothHfpAgStub()69 BluetoothHfpAgStub::~BluetoothHfpAgStub()
70 {
71 HILOGI("%{public}s start.", __func__);
72 memberFuncMap_.clear();
73 }
74
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)75 int BluetoothHfpAgStub::OnRemoteRequest(
76 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
77 {
78 HILOGI("BluetoothHfpAgStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
79 if (BluetoothHfpAgStub::GetDescriptor() != data.ReadInterfaceToken()) {
80 HILOGI("local descriptor is not equal to remote");
81 return ERR_INVALID_STATE;
82 }
83
84 auto itFunc = memberFuncMap_.find(code);
85 if (itFunc != memberFuncMap_.end()) {
86 auto memberFunc = itFunc->second;
87 if (memberFunc != nullptr) {
88 return (this->*memberFunc)(data, reply);
89 }
90 }
91 HILOGW("BluetoothHfpAgStub::OnRemoteRequest, default case, need check.");
92 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
93 }
94
GetConnectDevicesInner(MessageParcel & data,MessageParcel & reply)95 int32_t BluetoothHfpAgStub::GetConnectDevicesInner(MessageParcel &data, MessageParcel &reply)
96 {
97 std::vector<BluetoothRawAddress> devices;
98 GetConnectDevices(devices);
99 uint32_t DevNum = devices.size();
100 if (!reply.WriteUint32(DevNum)) {
101 HILOGE("BluetoothHfpAgStub:WriteInt32 failed in: %{public}s.", __func__);
102 return BT_ERR_IPC_TRANS_FAILED;
103 }
104 for (uint32_t i = 0; i < DevNum; i++) {
105 if (!reply.WriteParcelable(&devices[i])) {
106 HILOGE("BluetoothHfpAgStub:WriteParcelable failed in: %{public}s.", __func__);
107 return BT_ERR_IPC_TRANS_FAILED;
108 }
109 }
110 return NO_ERROR;
111 }
112
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)113 ErrCode BluetoothHfpAgStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
114 {
115 std::vector<BluetoothRawAddress> devices;
116 std::vector<int> states;
117 data.ReadInt32Vector(&states);
118 GetDevicesByStates(states, devices);
119 uint32_t DevNum = devices.size();
120 if (!reply.WriteUint32(DevNum)) {
121 HILOGE("BluetoothHfpAgStub:WriteInt32 failed in: %{public}s.", __func__);
122 return ERR_INVALID_VALUE;
123 }
124 for (uint32_t i = 0; i < DevNum; i++) {
125 if (!reply.WriteParcelable(&devices[i])) {
126 HILOGE("BluetoothHfpAgStub:WriteParcelable failed in: %{public}s.", __func__);
127 return ERR_INVALID_VALUE;
128 }
129 }
130 return NO_ERROR;
131 }
132
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)133 ErrCode BluetoothHfpAgStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply)
134 {
135 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
136 if (!device) {
137 return BT_ERR_IPC_TRANS_FAILED;
138 }
139 int32_t state;
140 int32_t errCode = GetDeviceState(*device, state);
141 if (!reply.WriteInt32(errCode)) {
142 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
143 return BT_ERR_IPC_TRANS_FAILED;
144 }
145 if (errCode != NO_ERROR) {
146 HILOGE("internal error.");
147 return BT_ERR_INTERNAL_ERROR;
148 }
149 // write state
150 if (!reply.WriteInt32(state)) {
151 HILOGE("reply write failed.");
152 return BT_ERR_IPC_TRANS_FAILED;
153 }
154 return NO_ERROR;
155 }
156
ConnectInner(MessageParcel & data,MessageParcel & reply)157 int32_t BluetoothHfpAgStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
158 {
159 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
160 if (!device) {
161 return BT_ERR_IPC_TRANS_FAILED;
162 }
163 int32_t errCode = Connect(*device);
164 if (!reply.WriteInt32(errCode)) {
165 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
166 return BT_ERR_IPC_TRANS_FAILED;
167 }
168 return NO_ERROR;
169 }
170
DisconnectInner(MessageParcel & data,MessageParcel & reply)171 int32_t BluetoothHfpAgStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
172 {
173 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
174 if (!device) {
175 return BT_ERR_IPC_TRANS_FAILED;
176 }
177 int32_t errCode = Disconnect(*device);
178 if (!reply.WriteInt32(errCode)) {
179 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
180 return BT_ERR_IPC_TRANS_FAILED;
181 }
182 return NO_ERROR;
183 }
184
GetScoStateInner(MessageParcel & data,MessageParcel & reply)185 ErrCode BluetoothHfpAgStub::GetScoStateInner(MessageParcel &data, MessageParcel &reply)
186 {
187 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
188 if (!device) {
189 return TRANSACTION_ERR;
190 }
191 int result = GetScoState(*device);
192 if (!reply.WriteInt32(result)) {
193 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
194 return ERR_INVALID_VALUE;
195 }
196 return NO_ERROR;
197 }
198
ConnectScoInner(MessageParcel & data,MessageParcel & reply)199 ErrCode BluetoothHfpAgStub::ConnectScoInner(MessageParcel &data, MessageParcel &reply)
200 {
201 bool result = ConnectSco();
202 if (!reply.WriteBool(result)) {
203 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
204 return ERR_INVALID_VALUE;
205 }
206 return NO_ERROR;
207 }
208
DisconnectScoInner(MessageParcel & data,MessageParcel & reply)209 ErrCode BluetoothHfpAgStub::DisconnectScoInner(MessageParcel &data, MessageParcel &reply)
210 {
211 bool result = DisconnectSco();
212 if (!reply.WriteBool(result)) {
213 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
214 return ERR_INVALID_VALUE;
215 }
216 return NO_ERROR;
217 }
218
PhoneStateChangedInner(MessageParcel & data,MessageParcel & reply)219 ErrCode BluetoothHfpAgStub::PhoneStateChangedInner(MessageParcel &data, MessageParcel &reply)
220 {
221 int numActive = data.ReadInt32();
222 int numHeld = data.ReadInt32();
223 int callState = data.ReadInt32();
224 std::string number = data.ReadString();
225 int type = data.ReadInt32();
226 std::string name = data.ReadString();
227 PhoneStateChanged(numActive, numHeld, callState, number, type, name);
228 return NO_ERROR;
229 }
230
ClccResponseInner(MessageParcel & data,MessageParcel & reply)231 ErrCode BluetoothHfpAgStub::ClccResponseInner(MessageParcel &data, MessageParcel &reply)
232 {
233 int index = data.ReadInt32();
234 int direction = data.ReadInt32();
235 int status = data.ReadInt32();
236 int mode = data.ReadInt32();
237 bool mpty = data.ReadBool();
238 std::string number = data.ReadString();
239 int type = data.ReadInt32();
240 ClccResponse(index, direction, status, mode, mpty, number, type);
241 return NO_ERROR;
242 }
243
OpenVoiceRecognitionInner(MessageParcel & data,MessageParcel & reply)244 ErrCode BluetoothHfpAgStub::OpenVoiceRecognitionInner(MessageParcel &data, MessageParcel &reply)
245 {
246 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
247 if (!device) {
248 return TRANSACTION_ERR;
249 }
250 int result = OpenVoiceRecognition(*device);
251 if (!reply.WriteInt32(result)) {
252 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
253 return ERR_INVALID_VALUE;
254 }
255 return NO_ERROR;
256 }
257
CloseVoiceRecognitionInner(MessageParcel & data,MessageParcel & reply)258 ErrCode BluetoothHfpAgStub::CloseVoiceRecognitionInner(MessageParcel &data, MessageParcel &reply)
259 {
260 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
261 if (!device) {
262 return TRANSACTION_ERR;
263 }
264 int result = CloseVoiceRecognition(*device);
265 if (!reply.WriteInt32(result)) {
266 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
267 return ERR_INVALID_VALUE;
268 }
269 return NO_ERROR;
270 }
271
SetActiveDeviceInner(MessageParcel & data,MessageParcel & reply)272 ErrCode BluetoothHfpAgStub::SetActiveDeviceInner(MessageParcel &data, MessageParcel &reply)
273 {
274 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
275 if (!device) {
276 return TRANSACTION_ERR;
277 }
278 int result = SetActiveDevice(*device);
279 if (!reply.WriteInt32(result)) {
280 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
281 return ERR_INVALID_VALUE;
282 }
283 return NO_ERROR;
284 }
285
IntoMockInner(MessageParcel & data,MessageParcel & reply)286 ErrCode BluetoothHfpAgStub::IntoMockInner(MessageParcel &data, MessageParcel &reply)
287 {
288 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
289 if (!device) {
290 return TRANSACTION_ERR;
291 }
292 int state = data.ReadInt32();
293 int result = IntoMock(*device, state);
294 if (!reply.WriteInt32(result)) {
295 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
296 return ERR_INVALID_VALUE;
297 }
298 return NO_ERROR;
299 }
300
SendNoCarrierInner(MessageParcel & data,MessageParcel & reply)301 ErrCode BluetoothHfpAgStub::SendNoCarrierInner(MessageParcel &data, MessageParcel &reply)
302 {
303 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
304 if (!device) {
305 return TRANSACTION_ERR;
306 }
307 int result = SendNoCarrier(*device);
308 if (!reply.WriteInt32(result)) {
309 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
310 return ERR_INVALID_VALUE;
311 }
312 return NO_ERROR;
313 }
314
GetActiveDeviceInner(MessageParcel & data,MessageParcel & reply)315 ErrCode BluetoothHfpAgStub::GetActiveDeviceInner(MessageParcel &data, MessageParcel &reply)
316 {
317 std::string result = GetActiveDevice();
318 if (!reply.WriteString(result)) {
319 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
320 return ERR_INVALID_VALUE;
321 }
322 return NO_ERROR;
323 }
324
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)325 ErrCode BluetoothHfpAgStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
326 {
327 sptr<IRemoteObject> tempObject = data.ReadRemoteObject();
328 sptr<IBluetoothHfpAgObserver> observer = iface_cast<IBluetoothHfpAgObserver>(tempObject);
329 RegisterObserver(observer);
330 return NO_ERROR;
331 }
332
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)333 ErrCode BluetoothHfpAgStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply)
334 {
335 sptr<IRemoteObject> tempObject = data.ReadRemoteObject();
336 sptr<IBluetoothHfpAgObserver> observer = iface_cast<IBluetoothHfpAgObserver>(tempObject);
337 DeregisterObserver(observer);
338 return NO_ERROR;
339 }
340
SetConnectStrategyInner(MessageParcel & data,MessageParcel & reply)341 ErrCode BluetoothHfpAgStub::SetConnectStrategyInner(MessageParcel &data, MessageParcel &reply)
342 {
343 return NO_ERROR;
344 }
345
GetConnectStrategyInner(MessageParcel & data,MessageParcel & reply)346 ErrCode BluetoothHfpAgStub::GetConnectStrategyInner(MessageParcel &data, MessageParcel &reply)
347 {
348 return NO_ERROR;
349 }
350
351 } // namespace Bluetooth
352 } // namespace OHOS
353