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(const ::hfp::offload_config& config) = 0;
39     virtual void UpdateAudioConfigToHal(const ::hfp::pcm_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 UpdateAudioConfigToHal(const ::hfp::pcm_config& config) override;
55     void ConfirmStreamingRequest() override;
56     void CancelStreamingRequest() override;
57     size_t Write(const uint8_t* p_buf, uint32_t len);
58   };
59 
60   class Encode : public IClientInterfaceEndpoint {
61   public:
62     virtual ~Encode() = default;
63 
64     void Cleanup() override;
65     void StartSession() override;
66     void StopSession() override;
67     void UpdateAudioConfigToHal(const ::hfp::offload_config& config) override;
68     void UpdateAudioConfigToHal(const ::hfp::pcm_config& config) override;
69     void ConfirmStreamingRequest() override;
70     void CancelStreamingRequest() override;
71     size_t Read(uint8_t* p_buf, uint32_t len);
72   };
73 
74   class Offload : public IClientInterfaceEndpoint {
75   public:
76     virtual ~Offload() = default;
77 
78     void Cleanup() override;
79     void StartSession() override;
80     void StopSession() override;
81     void UpdateAudioConfigToHal(const ::hfp::offload_config& config) override;
82     void UpdateAudioConfigToHal(const ::hfp::pcm_config& config) override;
83     void ConfirmStreamingRequest() override;
84     void CancelStreamingRequest() override;
85     std::unordered_map<tBTA_AG_UUID_CODEC, ::hfp::sco_config> GetHfpScoConfig();
86   };
87 
88   // Get HFP software decoding client interface if it's not previously acquired
89   // and not yet released.
90   Decode* GetDecode(bluetooth::common::MessageLoopThread* message_loop);
91   // Release interface if belongs to HFP client interface
92   bool ReleaseDecode(Decode* decode);
93 
94   // Get HFP software encoding client interface if it's not previously acquired
95   // and not yet released.
96   Encode* GetEncode(bluetooth::common::MessageLoopThread* message_loop);
97   // Release interface if belongs to HFP client interface
98   bool ReleaseEncode(Encode* encode);
99 
100   // Get HFP offload client interface
101   Offload* GetOffload(bluetooth::common::MessageLoopThread* message_loop);
102   // Release offload interface if belongs to HFP client interface
103   bool ReleaseOffload(Offload* offload);
104 
105   // Get interface, if previously not initialized - it'll initialize singleton.
106   static HfpClientInterface* Get();
107 
108 private:
109   static HfpClientInterface* interface;
110   Decode* decode_ = nullptr;
111   Encode* encode_ = nullptr;
112   Offload* offload_ = nullptr;
113 };
114 }  // namespace hfp
115 }  // namespace audio
116 }  // namespace bluetooth
117