1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "bta/ag/bta_ag_int.h"
20 #include "common/message_loop_thread.h"
21 
22 namespace bluetooth {
23 namespace audio {
24 namespace hfp {
25 
26 /*
27  * Client interface for HFP.
28  * Only available if HFP is managed by AIDL sessions
29  */
30 class HfpClientInterface {
31  private:
32   class IClientInterfaceEndpoint {
33    public:
34     virtual ~IClientInterfaceEndpoint() = default;
35     virtual void Cleanup() = 0;
36     virtual void StartSession() = 0;
37     virtual void StopSession() = 0;
38     virtual void UpdateAudioConfigToHal(
39         const ::hfp::offload_config& config) = 0;
40     virtual void ConfirmStreamingRequest() = 0;
41     virtual void CancelStreamingRequest() = 0;
42   };
43 
44  public:
45   class Decode : public IClientInterfaceEndpoint {
46    public:
Decode()47     Decode(){};
48     virtual ~Decode() = default;
49 
50     void Cleanup() override;
51     void StartSession() override;
52     void StopSession() override;
53     void UpdateAudioConfigToHal(const ::hfp::offload_config& config) override;
54     void ConfirmStreamingRequest() override;
55     void CancelStreamingRequest() override;
56     size_t Read(uint8_t* p_buf, uint32_t len);
57   };
58 
59   class Encode : public IClientInterfaceEndpoint {
60    public:
61     virtual ~Encode() = default;
62 
63     void Cleanup() override;
64     void StartSession() override;
65     void StopSession() override;
66     void UpdateAudioConfigToHal(const ::hfp::offload_config& config) override;
67     void ConfirmStreamingRequest() override;
68     void CancelStreamingRequest() override;
69     size_t Write(const uint8_t* p_buf, uint32_t len);
70   };
71 
72   class Offload : public IClientInterfaceEndpoint {
73    public:
74     virtual ~Offload() = default;
75 
76     void Cleanup() override;
77     void StartSession() override;
78     void StopSession() override;
79     void UpdateAudioConfigToHal(const ::hfp::offload_config& config) override;
80     void ConfirmStreamingRequest() override;
81     void CancelStreamingRequest() override;
82     std::unordered_map<int, ::hfp::sco_config> GetHfpScoConfig();
83   };
84 
85   // Get HFP software decoding client interface if it's not previously acquired
86   // and not yet released.
87   Decode* GetDecode(bluetooth::common::MessageLoopThread* message_loop);
88   // Release interface if belongs to HFP client interface
89   bool ReleaseDecode(Decode* decode);
90 
91   // Get HFP software encoding client interface if it's not previously acquired
92   // and not yet released.
93   Encode* GetEncode(bluetooth::common::MessageLoopThread* message_loop);
94   // Release interface if belongs to HFP client interface
95   bool ReleaseEncode(Encode* encode);
96 
97   // Get HFP offload client interface
98   Offload* GetOffload(bluetooth::common::MessageLoopThread* message_loop);
99   // Release offload interface if belongs to HFP client interface
100   bool ReleaseOffload(Offload* offload);
101 
102   // Get interface, if previously not initialized - it'll initialize singleton.
103   static HfpClientInterface* Get();
104 
105  private:
106   static HfpClientInterface* interface;
107   Decode* decode_ = nullptr;
108   Encode* encode_ = nullptr;
109   Offload* offload_ = nullptr;
110 };
111 }  // namespace hfp
112 }  // namespace audio
113 }  // namespace bluetooth
114