• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_hf_stub.h"
17 #include "bluetooth_log.h"
18 
19 namespace OHOS {
20 namespace Bluetooth {
BluetoothHfpHfStub()21 BluetoothHfpHfStub::BluetoothHfpHfStub() {
22     HILOGD("%{public}s start.", __func__);
23     memberFuncMap_[static_cast<uint32_t>(
24         BluetoothHfpHfStub::Code::BT_HFP_HF_CONNECT_SCO)] =
25         &BluetoothHfpHfStub::ConnectScoInner;
26     memberFuncMap_[static_cast<uint32_t>(
27         BluetoothHfpHfStub::Code::BT_HFP_HF_DISCONNECT_SCO)] =
28         &BluetoothHfpHfStub::DisconnectScoInner;
29     memberFuncMap_[static_cast<uint32_t>(
30         BluetoothHfpHfStub::Code::BT_HFP_HF_GET_DEVICES_BY_STATES)] =
31         &BluetoothHfpHfStub::GetDevicesByStatesInner;
32     memberFuncMap_[static_cast<uint32_t>(
33         BluetoothHfpHfStub::Code::BT_HFP_HF_GET_DEVICE_STATE)] =
34         &BluetoothHfpHfStub::GetDeviceStateInner;
35     memberFuncMap_[static_cast<uint32_t>(
36         BluetoothHfpHfStub::Code::BT_HFP_HF_GET_SCO_STATE)] =
37         &BluetoothHfpHfStub::GetScoStateInner;
38     memberFuncMap_[static_cast<uint32_t>(
39         BluetoothHfpHfStub::Code::BT_HFP_HF_SEND_DTMF_TONE)] =
40         &BluetoothHfpHfStub::SendDTMFToneInner;
41     memberFuncMap_[static_cast<uint32_t>(
42         BluetoothHfpHfStub::Code::BT_HFP_HF_CONNECT)] =
43         &BluetoothHfpHfStub::ConnectInner;
44     memberFuncMap_[static_cast<uint32_t>(
45         BluetoothHfpHfStub::Code::BT_HFP_HF_DISCONNECT)] =
46         &BluetoothHfpHfStub::DisconnectInner;
47     memberFuncMap_[static_cast<uint32_t>(
48         BluetoothHfpHfStub::Code::BT_HFP_HF_OPEN_VOICE_RECOGNITION)] =
49         &BluetoothHfpHfStub::OpenVoiceRecognitionInner;
50     memberFuncMap_[static_cast<uint32_t>(
51         BluetoothHfpHfStub::Code::BT_HFP_HF_CLOSE_VOICE_RECOGNITION)] =
52         &BluetoothHfpHfStub::CloseVoiceRecognitionInner;
53     memberFuncMap_[static_cast<uint32_t>(
54         BluetoothHfpHfStub::Code::BT_HFP_HF_GET_CURRENT_CALL_LIST)] =
55         &BluetoothHfpHfStub::GetCurrentCallListInner;
56     memberFuncMap_[static_cast<uint32_t>(
57         BluetoothHfpHfStub::Code::BT_HFP_HF_ACCEPT_INCOMING_CALL)] =
58         &BluetoothHfpHfStub::AcceptIncomingCallInner;
59     memberFuncMap_[static_cast<uint32_t>(
60         BluetoothHfpHfStub::Code::BT_HFP_HF_HOLD_ACTIVE_CALL)] =
61         &BluetoothHfpHfStub::HoldActiveCallInner;
62     memberFuncMap_[static_cast<uint32_t>(
63         BluetoothHfpHfStub::Code::BT_HFP_HF_REJECT_INCOMING_CALL)] =
64         &BluetoothHfpHfStub::RejectIncomingCallInner;
65     memberFuncMap_[static_cast<uint32_t>(
66         BluetoothHfpHfStub::Code::BT_HFP_HF_FINISH_ATIVE_CALL)] =
67         &BluetoothHfpHfStub::FinishActiveCallInner;
68     memberFuncMap_[static_cast<uint32_t>(
69         BluetoothHfpHfStub::Code::BT_HFP_HF_START_DIAL)] =
70         &BluetoothHfpHfStub::StartDialInner;
71     memberFuncMap_[static_cast<uint32_t>(
72         BluetoothHfpHfStub::Code::BT_HFP_HF_REGISTER_OBSERVER)] =
73         &BluetoothHfpHfStub::RegisterObserverInner;
74     memberFuncMap_[static_cast<uint32_t>(
75         BluetoothHfpHfStub::Code::BT_HFP_HF_DEREGISTER_OBSERVER)] =
76         &BluetoothHfpHfStub::DeregisterObserverInner;
77 
78     HILOGD("%{public}s ends.", __func__);
79 }
80 
~BluetoothHfpHfStub()81 BluetoothHfpHfStub::~BluetoothHfpHfStub() {
82     HILOGD("%{public}s start.", __func__);
83     memberFuncMap_.clear();
84 }
85 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)86 int BluetoothHfpHfStub::OnRemoteRequest(
87     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
88     HILOGD("BluetoothHfpHfStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
89     std::u16string descriptor = BluetoothHfpHfStub::GetDescriptor();
90     std::u16string remoteDescriptor = data.ReadInterfaceToken();
91     if (descriptor != remoteDescriptor) {
92         HILOGI("local descriptor is not equal to remote");
93         return ERR_INVALID_STATE;
94     }
95 
96     auto itFunc = memberFuncMap_.find(code);
97     if (itFunc != memberFuncMap_.end()) {
98         auto memberFunc = itFunc->second;
99         if (memberFunc != nullptr) {
100             return (this->*memberFunc)(data, reply);
101         }
102     }
103     HILOGW("BluetoothHfpHfStub::OnRemoteRequest, default case, need check.");
104     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
105 }
106 
ConnectScoInner(MessageParcel & data,MessageParcel & reply)107 ErrCode BluetoothHfpHfStub::ConnectScoInner(MessageParcel &data, MessageParcel &reply) {
108     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
109     int result = ConnectSco(*device);
110     if (!reply.WriteBool(result)) {
111         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
112         return ERR_INVALID_VALUE;
113     }
114     return NO_ERROR;
115 }
116 
DisconnectScoInner(MessageParcel & data,MessageParcel & reply)117 ErrCode BluetoothHfpHfStub::DisconnectScoInner(MessageParcel &data, MessageParcel &reply) {
118     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
119     int result = DisconnectSco(*device);
120     if (!reply.WriteBool(result)) {
121         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
122         return ERR_INVALID_VALUE;
123     }
124     return NO_ERROR;
125 }
126 
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)127 ErrCode BluetoothHfpHfStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply) {
128     std::vector<BluetoothRawAddress> devices;
129     std::vector<int> states;
130     data.ReadInt32Vector(&states);
131     GetDevicesByStates(states, devices);
132     int dev_num = devices.size();
133     if (!reply.WriteInt32(dev_num)) {
134         HILOGE("BluetoothHfpHfStub:WriteInt32 failed in: %{public}s.", __func__);
135         return ERR_INVALID_VALUE;
136     }
137     for (int i = 0; i < dev_num; i++) {
138         if (!reply.WriteParcelable(&devices[i])) {
139             HILOGE("BluetoothHfpHfStub:WriteParcelable failed in: %{public}s.", __func__);
140             return ERR_INVALID_VALUE;
141         }
142     }
143     return NO_ERROR;
144 }
145 
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)146 ErrCode BluetoothHfpHfStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply) {
147     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
148     int result = GetDeviceState(*device);
149     if (!reply.WriteInt32(result)) {
150         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
151         return ERR_INVALID_VALUE;
152     }
153     return NO_ERROR;
154 }
155 
GetScoStateInner(MessageParcel & data,MessageParcel & reply)156 ErrCode BluetoothHfpHfStub::GetScoStateInner(MessageParcel &data, MessageParcel &reply) {
157     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
158     int result = GetScoState(*device);
159     if (!reply.WriteInt32(result)) {
160         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
161         return ERR_INVALID_VALUE;
162     }
163     return NO_ERROR;
164 }
165 
SendDTMFToneInner(MessageParcel & data,MessageParcel & reply)166 ErrCode BluetoothHfpHfStub::SendDTMFToneInner(MessageParcel &data, MessageParcel &reply) {
167     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
168     uint8_t code = data.ReadUint8();
169     int result = SendDTMFTone(*device, code);
170     if (!reply.WriteInt32(result)) {
171         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
172         return ERR_INVALID_VALUE;
173     }
174     return NO_ERROR;
175 }
176 
ConnectInner(MessageParcel & data,MessageParcel & reply)177 ErrCode BluetoothHfpHfStub::ConnectInner(MessageParcel &data, MessageParcel &reply) {
178     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
179     int result = Connect(*device);
180     if (!reply.WriteInt32(result)) {
181         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
182         return ERR_INVALID_VALUE;
183     }
184     return NO_ERROR;
185 }
186 
DisconnectInner(MessageParcel & data,MessageParcel & reply)187 ErrCode BluetoothHfpHfStub::DisconnectInner(MessageParcel &data, MessageParcel &reply) {
188     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
189     int result = Disconnect(*device);
190     if (!reply.WriteInt32(result)) {
191         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
192         return ERR_INVALID_VALUE;
193     }
194     return NO_ERROR;
195 }
196 
OpenVoiceRecognitionInner(MessageParcel & data,MessageParcel & reply)197 ErrCode BluetoothHfpHfStub::OpenVoiceRecognitionInner(MessageParcel &data, MessageParcel &reply) {
198     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
199     int result = OpenVoiceRecognition(*device);
200     if (!reply.WriteBool(result)) {
201         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
202         return ERR_INVALID_VALUE;
203     }
204     return NO_ERROR;
205 }
206 
CloseVoiceRecognitionInner(MessageParcel & data,MessageParcel & reply)207 ErrCode BluetoothHfpHfStub::CloseVoiceRecognitionInner(MessageParcel &data, MessageParcel &reply) {
208     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
209     int result = CloseVoiceRecognition(*device);
210     if (!reply.WriteBool(result)) {
211         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
212         return ERR_INVALID_VALUE;
213     }
214     return NO_ERROR;
215 }
216 
GetCurrentCallListInner(MessageParcel & data,MessageParcel & reply)217 ErrCode BluetoothHfpHfStub::GetCurrentCallListInner(MessageParcel &data, MessageParcel &reply) {
218     std::vector<BluetoothHfpHfCall> calls;
219     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
220     GetCurrentCallList(*device, calls);
221     int call_num = calls.size();
222     if (!reply.WriteInt32(call_num)) {
223         HILOGE("BluetoothHfpHfStub:WriteInt32 failed in: %{public}s.", __func__);
224         return ERR_INVALID_VALUE;
225     }
226     for (int i = 0; i < call_num; i++) {
227         if (!reply.WriteParcelable(&calls[i])) {
228             HILOGE("BluetoothHfpHfStub:WriteParcelable failed in: %{public}s.", __func__);
229             return ERR_INVALID_VALUE;
230         }
231     }
232     return NO_ERROR;
233 }
234 
AcceptIncomingCallInner(MessageParcel & data,MessageParcel & reply)235 ErrCode BluetoothHfpHfStub::AcceptIncomingCallInner(MessageParcel &data, MessageParcel &reply) {
236     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
237     int flag = data.ReadInt32();
238     int result = AcceptIncomingCall(*device, flag);
239     if (!reply.WriteBool(result)) {
240         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
241         return ERR_INVALID_VALUE;
242     }
243     return NO_ERROR;
244 }
245 
HoldActiveCallInner(MessageParcel & data,MessageParcel & reply)246 ErrCode BluetoothHfpHfStub::HoldActiveCallInner(MessageParcel &data, MessageParcel &reply) {
247     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
248     int result = HoldActiveCall(*device);
249     if (!reply.WriteBool(result)) {
250         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
251         return ERR_INVALID_VALUE;
252     }
253     return NO_ERROR;
254 }
255 
RejectIncomingCallInner(MessageParcel & data,MessageParcel & reply)256 ErrCode BluetoothHfpHfStub::RejectIncomingCallInner(MessageParcel &data, MessageParcel &reply) {
257     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
258     int result = RejectIncomingCall(*device);
259     if (!reply.WriteBool(result)) {
260         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
261         return ERR_INVALID_VALUE;
262     }
263     return NO_ERROR;
264 }
265 
FinishActiveCallInner(MessageParcel & data,MessageParcel & reply)266 ErrCode BluetoothHfpHfStub::FinishActiveCallInner(MessageParcel &data, MessageParcel &reply) {
267     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
268     BluetoothHfpHfCall *call = data.ReadParcelable<BluetoothHfpHfCall>();
269     int result = FinishActiveCall(*device, *call);
270     if (!reply.WriteBool(result)) {
271         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
272         return ERR_INVALID_VALUE;
273     }
274     return NO_ERROR;
275 }
276 
StartDialInner(MessageParcel & data,MessageParcel & reply)277 ErrCode BluetoothHfpHfStub::StartDialInner(MessageParcel &data, MessageParcel &reply) {
278     BluetoothRawAddress *device = data.ReadParcelable<BluetoothRawAddress>();
279     std::string number = data.ReadString();
280     BluetoothHfpHfCall *call = data.ReadParcelable<BluetoothHfpHfCall>();
281     int result = StartDial(*device, number, *call);
282     if (!reply.WriteInt32(result)) {
283         HILOGE("BluetoothHfpHfStub: reply writing failed in: %{public}s.", __func__);
284         return ERR_INVALID_VALUE;
285     }
286     return NO_ERROR;
287 }
288 
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)289 ErrCode BluetoothHfpHfStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply) {
290     sptr<IRemoteObject> tempObject = data.ReadRemoteObject();
291     sptr<IBluetoothHfpHfObserver> observer = iface_cast<IBluetoothHfpHfObserver>(tempObject);
292     RegisterObserver(observer);
293     return NO_ERROR;
294 }
295 
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)296 ErrCode BluetoothHfpHfStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply) {
297     sptr<IRemoteObject> tempObject = data.ReadRemoteObject();
298     sptr<IBluetoothHfpHfObserver> observer = iface_cast<IBluetoothHfpHfObserver>(tempObject);
299     DeregisterObserver(observer);
300     return NO_ERROR;
301 }
302 
303 }  // namespace Bluetooth
304 }  // namespace OHOS
305