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 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines a bluetooth system that provides basic bluetooth connection and profile functions,
21  *        including A2DP, AVRCP, BLE, GATT, HFP, MAP, PBAP, and SPP, etc.
22  *
23  * @since 6
24  */
25 
26 /**
27  * @file bluetooth_hfp_hf.h
28  *
29  * @brief Declares HFP HF role framework functions, including basic and observer functions.
30  *
31  * @since 6
32  */
33 
34 #ifndef BLUETOOTH_HFP_HF_H
35 #define BLUETOOTH_HFP_HF_H
36 
37 #include <string>
38 #include <vector>
39 #include <memory>
40 
41 #include "bluetooth_def.h"
42 #include "bluetooth_types.h"
43 #include "bluetooth_remote_device.h"
44 #include "bluetooth_hf_call.h"
45 #include "bluetooth_no_destructor.h"
46 
47 namespace OHOS {
48 namespace Bluetooth {
49 /**
50  * @brief Class for HandsFree Unit observer functions.
51  *
52  * @since 6
53  */
54 class HandsFreeUnitObserver {
55 public:
56     /**
57      * @brief The observer function to notify connection state changed.
58      *
59      * @param device Remote device object.
60      * @param state Connection state.
61      * @param cause Connecton cause.
62      * @since 12
63      */
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state,int cause)64     virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause)
65     {}
66 
67     /**
68      * @brief The observer function to notify audio connection state changed.
69      *
70      * @param device Remote device object.
71      * @param state Connection state.
72      * @since 6
73      */
OnScoStateChanged(const BluetoothRemoteDevice & device,int state)74     virtual void OnScoStateChanged(const BluetoothRemoteDevice &device, int state)
75     {}
76 
77     /**
78      * @brief The observer function to notify call object changed.
79      *
80      * @param device Remote device object.
81      * @param call Call object.
82      * @since 6
83      */
OnCallChanged(const BluetoothRemoteDevice & device,const HandsFreeUnitCall & call)84     virtual void OnCallChanged(const BluetoothRemoteDevice &device, const HandsFreeUnitCall &call)
85     {}
86 
87     /**
88      * @brief The observer function to notify signal strength changed.
89      *
90      * @param device Remote device object.
91      * @param batteryLevel Signal strength.
92      * @since 6
93      */
OnSignalStrengthChanged(const BluetoothRemoteDevice & device,int signal)94     virtual void OnSignalStrengthChanged(const BluetoothRemoteDevice &device, int signal)
95     {}
96 
97     /**
98      * @brief The observer function to notify registration status changed.
99      *
100      * @param device Remote device object.
101      * @param status Registration status.
102      * @since 6
103      */
OnRegistrationStatusChanged(const BluetoothRemoteDevice & device,int status)104     virtual void OnRegistrationStatusChanged(const BluetoothRemoteDevice &device, int status)
105     {}
106 
107     /**
108      * @brief The observer function to notify roaming status changed.
109      *
110      * @param device Remote device object.
111      * @param status Roaming status.
112      * @since 6
113      */
OnRoamingStatusChanged(const BluetoothRemoteDevice & device,int status)114     virtual void OnRoamingStatusChanged(const BluetoothRemoteDevice &device, int status)
115     {}
116 
117     /**
118      * @brief The observer function to notify operator selection name changed.
119      *
120      * @param device Remote device object.
121      * @param name Operator selection name.
122      * @since 6
123      */
OnOperatorSelectionChanged(const BluetoothRemoteDevice & device,const std::string & name)124     virtual void OnOperatorSelectionChanged(const BluetoothRemoteDevice &device, const std::string &name)
125     {}
126 
127     /**
128      * @brief The observer function to notify subscriber number changed.
129      *
130      * @param device Remote device object.
131      * @param number Subscriber number.
132      * @since 6
133      */
OnSubscriberNumberChanged(const BluetoothRemoteDevice & device,const std::string & number)134     virtual void OnSubscriberNumberChanged(const BluetoothRemoteDevice &device, const std::string &number)
135     {}
136 
137     /**
138      * @brief The observer function to notify voice recognition status changed.
139      *
140      * @param device Remote device object.
141      * @param status Voice recognition status.
142      * @since 6
143      */
OnVoiceRecognitionStatusChanged(const BluetoothRemoteDevice & device,int status)144     virtual void OnVoiceRecognitionStatusChanged(const BluetoothRemoteDevice &device, int status)
145     {}
146 
147     /**
148      * @brief The observer function to notify inBand ring tone status changed.
149      *
150      * @param device Remote device object.
151      * @param status InBand ring tone status.
152      * @since 6
153      */
OnInBandRingToneChanged(const BluetoothRemoteDevice & device,int status)154     virtual void OnInBandRingToneChanged(const BluetoothRemoteDevice &device, int status)
155     {}
156 
157     /**
158      * @brief Destroy the HandsFreeUnitObserver object.
159      *
160      * @since 6
161      */
~HandsFreeUnitObserver()162     virtual ~HandsFreeUnitObserver()
163     {}
164 };
165 
166 /**
167  * @brief Class for HandsFree Unit API.
168  *
169  * @since 6
170  */
171 class BLUETOOTH_API HandsFreeUnit {
172 public:
173     /**
174      * @brief Get the instance of HandsFreeAudioGateway object.
175      *
176      * @return Returns the pointer to the HandsFreeAudioGateway instance.
177      * @since 6
178      */
179     static HandsFreeUnit *GetProfile();
180 
181     /**
182      * @brief Initiate the establishment of an audio connection to remote AG device.
183      *
184      * @param  device Remote device object.
185      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
186      * @since 6
187      */
188     bool ConnectSco(const BluetoothRemoteDevice &device);
189 
190     /**
191      * @brief Release the audio connection from remote HF device.
192      *
193      * @param  device Remote device object.
194      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
195      * @since 6
196      */
197     bool DisconnectSco(const BluetoothRemoteDevice &device);
198 
199     /**
200      * @brief Get remote AG device list which are in the specified states.
201      *
202      * @param states List of remote device states.
203      * @return Returns the list of devices.
204      * @since 6
205      */
206     std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states) const;
207 
208     /**
209      * @brief Get the connection state of the specified remote AG device.
210      *
211      * @param device Remote device object.
212      * @return Returns the connection state of the remote device.
213      * @since 6
214      */
215     int GetDeviceState(const BluetoothRemoteDevice &device) const;
216 
217     /**
218      * @brief Get the Audio connection state of the specified remote AG device.
219      *
220      * @param device Remote device object.
221      * @return Returns the Audio connection state.
222      * @since 6
223      */
224     int GetScoState(const BluetoothRemoteDevice &device) const;
225 
226     /**
227      * @brief Send DTMF tone code to remote AG device.
228      *
229      * @param device Remote device object.
230      * @param code DTMF tone code.
231      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
232      * @since 6
233      */
234     bool SendDTMFTone(const BluetoothRemoteDevice &device, uint8_t code);
235 
236     /**
237      * @brief Initiate the establishment of a service level connection to remote AG device.
238      *
239      * @param device Remote device object.
240      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
241      * @since 6
242      */
243     bool Connect(const BluetoothRemoteDevice &device);
244 
245     /**
246      * @brief Release the audio connection from remote AG device.
247      *
248      * @param device Remote device object.
249      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
250      * @since 6
251      */
252     bool Disconnect(const BluetoothRemoteDevice &device);
253 
254     /**
255      * @brief Open voice recognition.
256      *
257      * @param device Remote device object.
258      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
259      * @since 6
260      */
261     bool OpenVoiceRecognition(const BluetoothRemoteDevice &device);
262 
263     /**
264      * @brief Close voice recognition.
265      *
266      * @param device Remote device object.
267      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
268      * @since 6
269      */
270     bool CloseVoiceRecognition(const BluetoothRemoteDevice &device);
271 
272     /**
273      * @brief Get a list of all existing calls.
274      *
275      * @param device Remote device object.
276      * @return The list of all existing calls.
277      * @since 6
278      */
279     std::vector<HandsFreeUnitCall> GetExistingCalls(const BluetoothRemoteDevice &device);
280 
281     /**
282      * @brief Accept an incoming call.
283      *
284      * @param device Remote device object.
285      * @param flag Types of calls accepted.
286      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
287      * @since 6
288      */
289     bool AcceptIncomingCall(const BluetoothRemoteDevice &device, int flag);
290 
291     /**
292      * @brief Hold an active call.
293      *
294      * @param device Remote device object.
295      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
296      * @since 6
297      */
298     bool HoldActiveCall(const BluetoothRemoteDevice &device);
299 
300     /**
301      * @brief Reject an incoming call.
302      *
303      * @param device Remote device object.
304      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
305      * @since 6
306      */
307     bool RejectIncomingCall(const BluetoothRemoteDevice &device);
308 
309     bool SendKeyPressed(const BluetoothRemoteDevice &device);
310 
311     /**
312      * @brief Handle an incoming call.
313      *
314      * @param device Remote device object.
315      * @param flag handle action
316      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
317      * @since 9
318      */
319     bool HandleIncomingCall(const BluetoothRemoteDevice &device, int flag);
320 
321     /**
322      * @brief Hande multi calll.
323      *
324      * @param device Remote device object.
325      * @param flag handle action
326      * @param index call index
327      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
328      * @since 9
329      */
330     bool HandleMultiCall(const BluetoothRemoteDevice &device, int flag, int index);
331 
332     /**
333      * @brief dial last number.
334      *
335      * @param device Remote device object.
336      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
337      * @since 9
338      */
339     bool DialLastNumber(const BluetoothRemoteDevice &device);
340 
341     /**
342      * @brief dial memory number.
343      *
344      * @param device Remote device object.
345      * @param index memory number index
346      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
347      * @since 9
348      */
349     bool DialMemory(const BluetoothRemoteDevice &device, int index);
350 
351     bool SendVoiceTag(const BluetoothRemoteDevice &device, int index);
352 
353     /**
354      * @brief brief Finish a specified active call.
355      *
356      * @param device Remote device object.
357      * @param call Call object.
358      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
359      * @since 6
360      */
361     bool FinishActiveCall(const BluetoothRemoteDevice &device, const HandsFreeUnitCall &call);
362 
363     /**
364      * @brief Start dial with specified call number.
365      *
366      * @param device Remote device object.
367      * @param number Call's number to dial.
368      * @return The object of dial out call or nullopt.
369      * @since 6
370      */
371     std::optional<HandsFreeUnitCall> StartDial(const BluetoothRemoteDevice &device, const std::string &number);
372 
373     /**
374      * @brief Register HandsFree Unit observer instance.
375      *
376      * @param observer HandsFreeUnitObserver instance.
377      * @since 6
378      */
379     void RegisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer);
380 
381     /**
382      * @brief Deregister HandsFree Unit observer instance.
383      *
384      * @param observer HandsFreeUnitObserver instance.
385      * @since 6
386      */
387     void DeregisterObserver(std::shared_ptr<HandsFreeUnitObserver> observer);
388 
389     /**
390      * @brief The external process calls the HfpHf profile interface before the Bluetooth process starts. At this
391      * time, it needs to monitor the start of the Bluetooth process, and then call this interface to initialize the
392      * HfpHf proflie.
393      */
394     void Init();
395 
396     /**
397      * @brief Static HandsFree Unit observer instance.
398      *
399      * @since 6
400      */
401     static HandsFreeUnit *instance_;
402 
403 private:
404     HandsFreeUnit();
405     ~HandsFreeUnit();
406     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HandsFreeUnit);
407     BLUETOOTH_DECLARE_IMPL();
408 
409 #ifdef DTFUZZ_TEST
410     friend class BluetoothNoDestructor<HandsFreeUnit>;
411 #endif
412 };
413 }  // namespace Bluetooth
414 }  // namespace OHOS
415 #endif  // BLUETOOTH_HFP_HF_H
416