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 <bluetooth/log.h>
20 
21 #include <cstdint>
22 #include <unordered_map>
23 
24 #include "bta/ag/bta_ag_int.h"
25 #include "client_interface_aidl.h"
26 
27 namespace bluetooth {
28 namespace audio {
29 namespace aidl {
30 namespace hfp {
31 
32 using ::aidl::android::hardware::bluetooth::audio::LatencyMode;
33 
34 typedef enum {
35   HFP_CTRL_CMD_NONE = 0,
36   HFP_CTRL_CMD_CHECK_READY,
37   HFP_CTRL_CMD_START,
38   HFP_CTRL_CMD_STOP,
39   HFP_CTRL_CMD_SUSPEND,
40   HFP_CTRL_GET_INPUT_AUDIO_CONFIG,
41   HFP_CTRL_GET_OUTPUT_AUDIO_CONFIG,
42   HFP_CTRL_SET_OUTPUT_AUDIO_CONFIG,
43   HFP_CTRL_GET_PRESENTATION_POSITION,
44 } tHFP_CTRL_CMD;
45 
46 // Provide call-in APIs for the Bluetooth Audio HAL
47 class HfpTransport {
48  public:
49   HfpTransport();
50 
51   BluetoothAudioCtrlAck StartRequest();
52 
53   BluetoothAudioCtrlAck SuspendRequest();
54 
55   void StopRequest();
56 
57   void SetLatencyMode(LatencyMode latency_mode);
58 
59   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
60                                uint64_t* total_bytes_read,
61                                timespec* data_position);
62 
63   void SourceMetadataChanged(const source_metadata_v7_t& source_metadata);
64 
65   void SinkMetadataChanged(const sink_metadata_v7_t&);
66 
67   void ResetPresentationPosition();
68 
69   uint8_t GetPendingCmd() const;
70 
71   void ResetPendingCmd();
72 
73   void LogBytesProcessed(size_t bytes_read);
74 
75   static std::unordered_map<int, ::hfp::sco_config> GetHfpScoConfig(
76       SessionType sessionType);
77 
78  private:
79   tHFP_CTRL_CMD hfp_pending_cmd_;
80 };
81 
82 // Sink transport implementation
83 class HfpDecodingTransport
84     : public ::bluetooth::audio::aidl::IBluetoothSinkTransportInstance {
85  public:
86   HfpDecodingTransport(SessionType sessionType);
87 
88   ~HfpDecodingTransport();
89 
90   BluetoothAudioCtrlAck StartRequest(bool is_low_latency);
91 
92   BluetoothAudioCtrlAck SuspendRequest();
93 
94   void StopRequest();
95 
96   void SetLatencyMode(LatencyMode latency_mode);
97 
98   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
99                                uint64_t* total_bytes_read,
100                                timespec* data_position);
101 
102   void SourceMetadataChanged(const source_metadata_v7_t& source_metadata);
103 
104   void SinkMetadataChanged(const sink_metadata_v7_t& sink_metadata);
105 
106   void ResetPresentationPosition();
107 
108   void LogBytesRead(size_t bytes_read) override;
109 
110   uint8_t GetPendingCmd() const;
111 
112   void ResetPendingCmd();
113 
114   static inline HfpDecodingTransport* instance_ = nullptr;
115   static inline BluetoothAudioSinkClientInterface* software_hal_interface =
116       nullptr;
117   static inline BluetoothAudioSinkClientInterface* offloading_hal_interface =
118       nullptr;
119   static inline BluetoothAudioSinkClientInterface* active_hal_interface =
120       nullptr;
121 
122  private:
123   HfpTransport* transport_;
124 };
125 
126 class HfpEncodingTransport
127     : public ::bluetooth::audio::aidl::IBluetoothSourceTransportInstance {
128  public:
129   HfpEncodingTransport(SessionType sessionType);
130 
131   ~HfpEncodingTransport();
132 
133   BluetoothAudioCtrlAck StartRequest(bool is_low_latency);
134 
135   BluetoothAudioCtrlAck SuspendRequest();
136 
137   void StopRequest();
138 
139   void SetLatencyMode(LatencyMode latency_mode);
140 
141   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
142                                uint64_t* total_bytes_read,
143                                timespec* data_position);
144 
145   void SourceMetadataChanged(const source_metadata_v7_t& source_metadata);
146 
147   void SinkMetadataChanged(const sink_metadata_v7_t& sink_metadata);
148 
149   void ResetPresentationPosition();
150 
151   void LogBytesWritten(size_t bytes_written) override;
152 
153   uint8_t GetPendingCmd() const;
154 
155   void ResetPendingCmd();
156 
157   static inline HfpEncodingTransport* instance_ = nullptr;
158   static inline BluetoothAudioSourceClientInterface* software_hal_interface =
159       nullptr;
160   static inline BluetoothAudioSourceClientInterface* offloading_hal_interface =
161       nullptr;
162   static inline BluetoothAudioSourceClientInterface* active_hal_interface =
163       nullptr;
164 
165  private:
166   HfpTransport* transport_;
167 };
168 
169 }  // namespace hfp
170 }  // namespace aidl
171 }  // namespace audio
172 }  // namespace bluetooth
173 
174 namespace fmt {
175 template <>
176 struct formatter<bluetooth::audio::aidl::hfp::tHFP_CTRL_CMD>
177     : enum_formatter<bluetooth::audio::aidl::hfp::tHFP_CTRL_CMD> {};
178 }  // namespace fmt
179