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
16 #include "bluetooth_hfp_ag_stub.h"
17 #include "bluetooth_log.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
BluetoothHfpAgStub()21 BluetoothHfpAgStub::BluetoothHfpAgStub() {
22 HILOGD("%{public}s start.", __func__);
23 memberFuncMap_[static_cast<uint32_t>(
24 BluetoothHfpAgStub::Code::BT_HFP_AG_GET_CONNECT_DEVICES)] =
25 &BluetoothHfpAgStub::GetConnectDevicesInner;
26 memberFuncMap_[static_cast<uint32_t>(
27 BluetoothHfpAgStub::Code::BT_HFP_AG_GET_DEVICES_BY_STATES)] =
28 &BluetoothHfpAgStub::GetDevicesByStatesInner;
29 memberFuncMap_[static_cast<uint32_t>(
30 BluetoothHfpAgStub::Code::BT_HFP_AG_GET_DEVICE_STATE)] =
31 &BluetoothHfpAgStub::GetDeviceStateInner;
32 memberFuncMap_[static_cast<uint32_t>(
33 BluetoothHfpAgStub::Code::BT_HFP_AG_CONNECT)] =
34 &BluetoothHfpAgStub::ConnectInner;
35 memberFuncMap_[static_cast<uint32_t>(
36 BluetoothHfpAgStub::Code::BT_HFP_AG_DISCONNECT)] =
37 &BluetoothHfpAgStub::DisconnectInner;
38 memberFuncMap_[static_cast<uint32_t>(
39 BluetoothHfpAgStub::Code::BT_HFP_AG_GET_SCO_STATE)] =
40 &BluetoothHfpAgStub::GetScoStateInner;
41 memberFuncMap_[static_cast<uint32_t>(
42 BluetoothHfpAgStub::Code::BT_HFP_AG_CONNECT_SCO)] =
43 &BluetoothHfpAgStub::ConnectScoInner;
44 memberFuncMap_[static_cast<uint32_t>(
45 BluetoothHfpAgStub::Code::BT_HFP_AG_DISCONNECT_SCO)] =
46 &BluetoothHfpAgStub::DisconnectScoInner;
47 memberFuncMap_[static_cast<uint32_t>(
48 BluetoothHfpAgStub::Code::BT_HFP_AG_PHONE_STATE_CHANGED)] =
49 &BluetoothHfpAgStub::PhoneStateChangedInner;
50 memberFuncMap_[static_cast<uint32_t>(
51 BluetoothHfpAgStub::Code::BT_HFP_AG_CLCC_RESPONSE)] =
52 &BluetoothHfpAgStub::ClccResponseInner;
53 memberFuncMap_[static_cast<uint32_t>(
54 BluetoothHfpAgStub::Code::BT_HFP_AG_OPEN_VOICE_RECOGNITION)] =
55 &BluetoothHfpAgStub::OpenVoiceRecognitionInner;
56 memberFuncMap_[static_cast<uint32_t>(
57 BluetoothHfpAgStub::Code::BT_HFP_AG_CLOSE_VOICE_RECOGNITION)] =
58 &BluetoothHfpAgStub::CloseVoiceRecognitionInner;
59 memberFuncMap_[static_cast<uint32_t>(
60 BluetoothHfpAgStub::Code::BT_HFP_AG_SET_ACTIVE_DEVICE)] =
61 &BluetoothHfpAgStub::SetActiveDeviceInner;
62 memberFuncMap_[static_cast<uint32_t>(
63 BluetoothHfpAgStub::Code::BT_HFP_AG_GET_ACTIVE_DEVICE)] =
64 &BluetoothHfpAgStub::GetActiveDeviceInner;
65 memberFuncMap_[static_cast<uint32_t>(
66 BluetoothHfpAgStub::Code::BT_HFP_AG_REGISTER_OBSERVER)] =
67 &BluetoothHfpAgStub::RegisterObserverInner;
68 memberFuncMap_[static_cast<uint32_t>(
69 BluetoothHfpAgStub::Code::BT_HFP_AG_DEREGISTER_OBSERVER)] =
70 &BluetoothHfpAgStub::DeregisterObserverInner;
71
72 HILOGD("%{public}s ends.", __func__);
73 }
74
~BluetoothHfpAgStub()75 BluetoothHfpAgStub::~BluetoothHfpAgStub() {
76 HILOGD("%{public}s start.", __func__);
77 memberFuncMap_.clear();
78 }
79
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)80 int BluetoothHfpAgStub::OnRemoteRequest(
81 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
82 HILOGD("BluetoothHfpAgStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
83 std::u16string descriptor = BluetoothHfpAgStub::GetDescriptor();
84 std::u16string remoteDescriptor = data.ReadInterfaceToken();
85 if (descriptor != remoteDescriptor) {
86 HILOGI("local descriptor is not equal to remote");
87 return ERR_INVALID_STATE;
88 }
89
90 auto itFunc = memberFuncMap_.find(code);
91 if (itFunc != memberFuncMap_.end()) {
92 auto memberFunc = itFunc->second;
93 if (memberFunc != nullptr) {
94 return (this->*memberFunc)(data, reply);
95 }
96 }
97 HILOGW("BluetoothHfpAgStub::OnRemoteRequest, default case, need check.");
98 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
99 }
100
GetConnectDevicesInner(MessageParcel & data,MessageParcel & reply)101 ErrCode BluetoothHfpAgStub::GetConnectDevicesInner(MessageParcel &data, MessageParcel &reply) {
102 std::vector<BluetoothRawAddress> devices;
103 GetConnectDevices(devices);
104 int dev_num = devices.size();
105 if (!reply.WriteInt32(dev_num)) {
106 HILOGE("BluetoothHfpAgStub:WriteInt32 failed in: %{public}s.", __func__);
107 return ERR_INVALID_VALUE;
108 }
109 for (int i = 0; i < dev_num; i++) {
110 if (!reply.WriteParcelable(&devices[i])) {
111 HILOGE("BluetoothHfpAgStub:WriteParcelable failed in: %{public}s.", __func__);
112 return ERR_INVALID_VALUE;
113 }
114 }
115 return NO_ERROR;
116 }
117
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)118 ErrCode BluetoothHfpAgStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply) {
119 std::vector<BluetoothRawAddress> devices;
120 std::vector<int> states;
121 data.ReadInt32Vector(&states);
122 GetDevicesByStates(states, devices);
123 int dev_num = devices.size();
124 if (!reply.WriteInt32(dev_num)) {
125 HILOGE("BluetoothHfpAgStub:WriteInt32 failed in: %{public}s.", __func__);
126 return ERR_INVALID_VALUE;
127 }
128 for (int i = 0; i < dev_num; i++) {
129 if (!reply.WriteParcelable(&devices[i])) {
130 HILOGE("BluetoothHfpAgStub:WriteParcelable failed in: %{public}s.", __func__);
131 return ERR_INVALID_VALUE;
132 }
133 }
134 return NO_ERROR;
135 }
136
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)137 ErrCode BluetoothHfpAgStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply) {
138 BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
139 int result = GetDeviceState(*device);
140 if (!reply.WriteInt32(result)) {
141 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
142 return ERR_INVALID_VALUE;
143 }
144 return NO_ERROR;
145 }
146
ConnectInner(MessageParcel & data,MessageParcel & reply)147 ErrCode BluetoothHfpAgStub::ConnectInner(MessageParcel &data, MessageParcel &reply) {
148 BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
149 int result = Connect(*device);
150 if (!reply.WriteInt32(result)) {
151 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
152 return ERR_INVALID_VALUE;
153 }
154 return NO_ERROR;
155 }
156
DisconnectInner(MessageParcel & data,MessageParcel & reply)157 ErrCode BluetoothHfpAgStub::DisconnectInner(MessageParcel &data, MessageParcel &reply) {
158 BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
159 int result = Disconnect(*device);
160 if (!reply.WriteInt32(result)) {
161 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
162 return ERR_INVALID_VALUE;
163 }
164 return NO_ERROR;
165 }
166
GetScoStateInner(MessageParcel & data,MessageParcel & reply)167 ErrCode BluetoothHfpAgStub::GetScoStateInner(MessageParcel &data, MessageParcel &reply) {
168 BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
169 int result = GetScoState(*device);
170 if (!reply.WriteInt32(result)) {
171 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
172 return ERR_INVALID_VALUE;
173 }
174 return NO_ERROR;
175 }
176
ConnectScoInner(MessageParcel & data,MessageParcel & reply)177 ErrCode BluetoothHfpAgStub::ConnectScoInner(MessageParcel &data, MessageParcel &reply) {
178 bool result = ConnectSco();
179 if (!reply.WriteBool(result)) {
180 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
181 return ERR_INVALID_VALUE;
182 }
183 return NO_ERROR;
184 }
185
DisconnectScoInner(MessageParcel & data,MessageParcel & reply)186 ErrCode BluetoothHfpAgStub::DisconnectScoInner(MessageParcel &data, MessageParcel &reply) {
187 bool result = DisconnectSco();
188 if (!reply.WriteBool(result)) {
189 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
190 return ERR_INVALID_VALUE;
191 }
192 return NO_ERROR;
193 }
194
PhoneStateChangedInner(MessageParcel & data,MessageParcel & reply)195 ErrCode BluetoothHfpAgStub::PhoneStateChangedInner(MessageParcel &data, MessageParcel &reply) {
196 int numActive = data.ReadInt32();
197 int numHeld = data.ReadInt32();
198 int callState = data.ReadInt32();
199 std::string number = data.ReadString();
200 int type = data.ReadInt32();
201 std::string name = data.ReadString();
202 PhoneStateChanged(numActive, numHeld, callState, number, type, name);
203 return NO_ERROR;
204 }
205
ClccResponseInner(MessageParcel & data,MessageParcel & reply)206 ErrCode BluetoothHfpAgStub::ClccResponseInner(MessageParcel &data, MessageParcel &reply) {
207 int index = data.ReadInt32();
208 int direction = data.ReadInt32();
209 int status = data.ReadInt32();
210 int mode = data.ReadInt32();
211 bool mpty = data.ReadBool();
212 std::string number = data.ReadString();
213 int type = data.ReadInt32();
214 ClccResponse(index, direction, status, mode, mpty, number, type);
215 return NO_ERROR;
216 }
217
OpenVoiceRecognitionInner(MessageParcel & data,MessageParcel & reply)218 ErrCode BluetoothHfpAgStub::OpenVoiceRecognitionInner(MessageParcel &data, MessageParcel &reply) {
219 BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
220 int result = OpenVoiceRecognition(*device);
221 if (!reply.WriteInt32(result)) {
222 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
223 return ERR_INVALID_VALUE;
224 }
225 return NO_ERROR;
226 }
227
CloseVoiceRecognitionInner(MessageParcel & data,MessageParcel & reply)228 ErrCode BluetoothHfpAgStub::CloseVoiceRecognitionInner(MessageParcel &data, MessageParcel &reply) {
229 BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
230 int result = CloseVoiceRecognition(*device);
231 if (!reply.WriteInt32(result)) {
232 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
233 return ERR_INVALID_VALUE;
234 }
235 return NO_ERROR;
236 }
237
SetActiveDeviceInner(MessageParcel & data,MessageParcel & reply)238 ErrCode BluetoothHfpAgStub::SetActiveDeviceInner(MessageParcel &data, MessageParcel &reply) {
239 BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
240 int result = SetActiveDevice(*device);
241 if (!reply.WriteInt32(result)) {
242 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
243 return ERR_INVALID_VALUE;
244 }
245 return NO_ERROR;
246 }
247
GetActiveDeviceInner(MessageParcel & data,MessageParcel & reply)248 ErrCode BluetoothHfpAgStub::GetActiveDeviceInner(MessageParcel &data, MessageParcel &reply) {
249 std::string result = GetActiveDevice();
250 if (!reply.WriteString(result)) {
251 HILOGE("BluetoothHfpAgStub: reply writing failed in: %{public}s.", __func__);
252 return ERR_INVALID_VALUE;
253 }
254 return NO_ERROR;
255 }
256
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)257 ErrCode BluetoothHfpAgStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply) {
258 sptr<IRemoteObject> tempObject = data.ReadRemoteObject();
259 sptr<IBluetoothHfpAgObserver> observer = iface_cast<IBluetoothHfpAgObserver>(tempObject);
260 RegisterObserver(observer);
261 return NO_ERROR;
262 }
263
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)264 ErrCode BluetoothHfpAgStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply) {
265 sptr<IRemoteObject> tempObject = data.ReadRemoteObject();
266 sptr<IBluetoothHfpAgObserver> observer = iface_cast<IBluetoothHfpAgObserver>(tempObject);
267 DeregisterObserver(observer);
268 return NO_ERROR;
269 }
270
271 } // namespace Bluetooth
272 } // namespace OHOS
273