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_ag.h
28 *
29 * @brief Declares HFP AG role framework functions, including basic and observer functions.
30 *
31 * @since 6
32 */
33
34#ifndef BLUETOOTH_HFP_AG_H
35#define BLUETOOTH_HFP_AG_H
36
37#include <string>
38#include <vector>
39#include <memory>
40#include <list>
41
42#include "bluetooth_def.h"
43#include "bluetooth_types.h"
44#include "bluetooth_remote_device.h"
45#include "bluetooth_no_destructor.h"
46
47namespace OHOS {
48namespace Bluetooth {
49/**
50 * @brief Class for HandsFree AudioGateway observer functions.
51 *
52 * @since 6
53 */
54class HandsFreeAudioGatewayObserver {
55public:
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     */
64    virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int32_t state, int32_t 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 Audio connection state.
72     * @param reason Reason for Audio status change.
73     * @since 6
74     */
75    virtual void OnScoStateChanged(const BluetoothRemoteDevice &device, int32_t state, int32_t reason)
76    {}
77
78    /**
79     * @brief The observer function to notify active device changed.
80     *
81     * @param device Remote active device object.
82     * @since 6
83     */
84    virtual void OnActiveDeviceChanged(const BluetoothRemoteDevice &device)
85    {}
86
87    /**
88     * @brief The observer function to notify enhanced driver safety changed.
89     *
90     * @param device Remote device object.
91     * @param indValue Enhanced driver safety value.
92     * @since 6
93     */
94    virtual void OnHfEnhancedDriverSafetyChanged(const BluetoothRemoteDevice &device, int32_t indValue)
95    {}
96
97    /**
98     * @brief The observer function to notify audio framework the hfp stack changed.
99     *
100     * @param device Remote device object.
101     * @param action Action on the device.
102     * @since 11
103     */
104    virtual void OnHfpStackChanged(const BluetoothRemoteDevice &device, int32_t action)
105    {}
106
107    /**
108     * @brief Destroy the HandsFreeAudioGateway Observer object.
109     *
110     * @since 6
111     */
112    virtual ~HandsFreeAudioGatewayObserver()
113    {}
114
115    /**
116     * @brief The observer function to notify virtual device changed.
117     *
118     * @param Action on the device.
119     * @param device bluetooth device address.
120     * @since 12
121     */
122    virtual void OnVirtualDeviceChanged(int32_t action, std::string address)
123    {}
124};
125
126/**
127 * @brief Class for HandsFree AudioGateway API.
128 *
129 * @since 6
130 */
131class BLUETOOTH_API HandsFreeAudioGateway {
132public:
133    /**
134     * @brief Get the instance of HandsFreeAudioGateway object.
135     *
136     * @return Returns the pointer to the HandsFreeAudioGateway instance.
137     * @since 6
138     */
139    static HandsFreeAudioGateway *GetProfile();
140
141    /**
142     * @brief Get remote HF device list which are in the connected state.
143     *
144     * @return Returns the list of devices.
145     * @since 6
146     */
147    int32_t GetConnectedDevices(std::vector<BluetoothRemoteDevice>& devices);
148
149    /**
150     * @brief Get remote HF device list which are in the specified states.
151     *
152     * @param states List of remote device states.
153     * @return Returns the list of devices.
154     * @since 6
155     */
156    std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states);
157
158    /**
159     * @brief Get the connection state of the specified remote HF device.
160     *
161     * @param device Remote device object.
162     * @return Returns the connection state of the remote device.
163     * @since 6
164     */
165    int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state);
166
167    /**
168     * @brief Initiate the establishment of a service level connection to remote HF device.
169     *
170     * @param device Remote device object.
171     * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
172     * @since 6
173     */
174    int32_t Connect(const BluetoothRemoteDevice &device);
175
176    /**
177     * @brief Release the audio connection from remote HF device.
178     *
179     * @param device Remote device object.
180     * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
181     * @since 6
182     */
183    int32_t Disconnect(const BluetoothRemoteDevice &device);
184
185    /**
186     * @brief Get the Audio connection state of the specified remote HF device.
187     *
188     * @param device Remote device object.
189     * @return Returns the Audio connection state.
190     * @since 6
191     */
192    int GetScoState(const BluetoothRemoteDevice &device) const;
193
194    /**
195     * @brief Initiate the establishment of an audio connection to remote active HF device.
196     *
197     * @param callType the type of the call, Refer to enum BtCallType
198     * @return Returns <b>0</b> if the operation is successful; returns <b>Other</b> if the operation fails.
199     * @since 10
200     */
201    int32_t ConnectSco(uint8_t callType);
202
203    /**
204     * @brief Release the audio connection from remote active HF device.
205     *
206     * @param callType the type of the call, Refer to enum BtCallType
207     * @return Returns <b>0</b> if the operation is successful; returns <b>Other</b> if the operation fails.
208     * @since 10
209     */
210    int32_t DisconnectSco(uint8_t callType);
211
212    /**
213     * @brief Initiate the establishment of an audio connection to remote active HF device.
214     *
215     * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
216     * @since 6
217     */
218    bool ConnectSco();
219
220    /**
221     * @brief Release the audio connection from remote active HF device.
222     *
223     * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
224     * @since 6
225     */
226    bool DisconnectSco();
227
228    /**
229     * @brief This function used to Update changed phone call informations.
230     *
231     * @param numActive Active call number.
232     * @param numHeld Held call number.
233     * @param callState Current call state.
234     * @param number Phone call number.
235     * @param type Type of phone call number.
236     * @param name Name of phone call number.
237     * @since 6
238     */
239    void PhoneStateChanged(
240        int numActive, int numHeld, int callState, const std::string &number, int type, const std::string &name);
241
242    /**
243     * @brief Send response for querying standard list current calls by remote Hf device.
244     *
245     * @param index Index of the current call.
246     * @param direction Direction of the current call.
247     * @param status Status of the current call.
248     * @param mode Source Mode of the current call.
249     * @param mpty Is this call a member of a conference call.
250     * @param number Phone call number.
251     * @param type Type of phone call number.
252     * @since 6
253     */
254    void ClccResponse(int index, int direction, int status, int mode, bool mpty, const std::string &number, int type);
255
256    /**
257     * @brief Open the voice recognition.
258     *
259     * @param device Remote device object.
260     * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
261     * @since 6
262     */
263    bool OpenVoiceRecognition(const BluetoothRemoteDevice &device);
264
265    /**
266     * @brief Close the voice recognition.
267     *
268     * @param device Remote device object.
269     * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
270     * @since 6
271     */
272    bool CloseVoiceRecognition(const BluetoothRemoteDevice &device);
273
274    /**
275     * @brief Set the active device for audio connection.
276     *
277     * @param device Remote device object.
278     * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
279     * @since 6
280     */
281    bool SetActiveDevice(const BluetoothRemoteDevice &device);
282
283    bool IntoMock(const BluetoothRemoteDevice &device, int state);
284    bool SendNoCarrier(const BluetoothRemoteDevice &device);
285
286    /**
287     * @brief Get the active device object.
288     *
289     * @param device Remote active device object.
290     * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
291     * @since 6
292     */
293    BluetoothRemoteDevice GetActiveDevice() const;
294
295    /**
296     * @brief Set connection strategy for peer bluetooth device.
297     *        If peer device is connected and the policy is set not allowed,then perform disconnect operation.
298     *        If peer device is disconnected and the policy is set allowed,then perform connect operation.
299     *
300     * @param device The address of the peer bluetooth device.
301     * @param strategy The device connect strategy.
302     * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
303     *         Returns <b>BT_ERR_PERMISSION_FAILED</b> Permission denied.
304     *         Returns <b>BT_ERR_INVALID_PARAM</b> Input error.
305     *         Returns <b>BT_ERR_INVALID_STATE</b> BT_ERR_INVALID_STATE.
306     *         Returns <b>BT_ERR_INTERNAL_ERROR</b> Operation failed.
307     * @since 10.0
308     */
309    int SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy);
310
311    /**
312     * @brief Get connection strategy of peer bluetooth device.
313     *
314     * @param device The address of the peer bluetooth device.
315     * @param strategy The device connect strategy.
316     * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
317     *         Returns <b>BT_ERR_PERMISSION_FAILED</b> Permission denied.
318     *         Returns <b>BT_ERR_INVALID_PARAM</b> Input error.
319     *         Returns <b>BT_ERR_INVALID_STATE</b> BT_ERR_INVALID_STATE.
320     *         Returns <b>BT_ERR_INTERNAL_ERROR</b> Operation failed.
321     * @since 10.0
322     */
323    int GetConnectStrategy(const BluetoothRemoteDevice &device, int &strategy) const;
324
325    /**
326     * @brief Register HandsFree AudioGateway observer instance.
327     *
328     * @param observer HandsFree AudioGateway observer instance.
329     * @since 6
330     */
331    void RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer);
332
333    /**
334     * @brief Deregister HandsFree AudioGateway observer instance.
335     *
336     * @param observer HandsFree AudioGateway observer instance.
337     * @since 6
338     */
339    void DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer);
340
341    /**
342     * @brief Get remote HF device list which are in the connected state.
343     *
344     * @return Returns the list of devices.
345     * @since 6
346     */
347    std::vector<BluetoothRemoteDevice> GetConnectedDevices() const;
348
349    /**
350     * @brief Check whether in-band-ringing is enabled
351     *
352     * @return Returns <b>true</b> if the in-band-ringing is enbale;
353     * returns <b>false</b> if the in-band-ringing is disable.
354     * @since 11
355     */
356    int IsInbandRingingEnabled(bool &isEnabled) const;
357
358    /**
359     * @brief This function used to update changed phone call state.
360     *
361     * @param callId Current call id.
362     * @param callState Current call state.
363     * @since 11
364     */
365    void CallDetailsChanged(int callId, int callState);
366
367    /**
368     * @brief Enable or disable the bluetooth log.
369     *
370     * @param state True: enable log, false: diaable log.
371     * @since 11
372     */
373    void EnableBtCallLog(bool state);
374
375    /**
376     * @brief Check whether vgs is supported.
377     *
378     * @param device The address of the peer bluetooth device.
379     * @param isSupported Whether vgs is supported.
380     * @return Returns <b>RET_NO_ERROR</b> if the operation is successful.
381     *         Returns <b>BT_ERR_PERMISSION_FAILED</b> Permission denied.
382     *         Returns <b>BT_ERR_INVALID_PARAM</b> Input error.
383     *         Returns <b>BT_ERR_INVALID_STATE</b> BT_ERR_INVALID_STATE.
384     *         Returns <b>BT_ERR_INTERNAL_ERROR</b> Operation failed.
385     * @since 12
386     */
387    int IsVgsSupported(const BluetoothRemoteDevice &device, bool &isSupported) const;
388
389    /**
390     * get virtual device list.
391     *
392     * @param devices virtual device list.
393     * @since 12
394     */
395    void GetVirtualDeviceList(std::vector<std::string> &devices) const;
396
397    /**
398     * @brief Static HandsFree AudioGateway observer instance.
399     *
400     * @since 6
401     */
402    static HandsFreeAudioGateway *instance_;
403
404    /**
405     * @brief The external process calls the HfpAg profile interface before the Bluetooth process starts. At this
406     * time, it needs to monitor the start of the Bluetooth process, and then call this interface to initialize the
407     * HfpAg proflie.
408     */
409    void Init();
410
411private:
412    HandsFreeAudioGateway();
413    ~HandsFreeAudioGateway();
414    BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HandsFreeAudioGateway);
415    BLUETOOTH_DECLARE_IMPL();
416
417#ifdef DTFUZZ_TEST
418    friend class BluetoothNoDestructor<HandsFreeAudioGateway>;
419#endif
420};
421}  // namespace Bluetooth
422}  // namespace OHOS
423#endif  // BLUETOOTH_HFP_AG_H
424