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, uint64_t* total_bytes_read,
60                                timespec* data_position);
61 
62   void SourceMetadataChanged(const source_metadata_v7_t& source_metadata);
63 
64   void SinkMetadataChanged(const sink_metadata_v7_t&);
65 
66   void ResetPresentationPosition();
67 
68   uint8_t GetPendingCmd() const;
69 
70   void ResetPendingCmd();
71 
72   void LogBytesProcessed(size_t bytes_read);
73 
74   static std::unordered_map<tBTA_AG_UUID_CODEC, ::hfp::sco_config> GetHfpScoConfig(
75           SessionType sessionType);
76 
77   bool IsStreamActive();
78 
79   void SetStreamActive(bool active);
80 
81 private:
82   tHFP_CTRL_CMD hfp_pending_cmd_;
83 
84   bool is_stream_active;
85 };
86 
87 // Source transport implementation
88 class HfpDecodingTransport : public ::bluetooth::audio::aidl::IBluetoothSourceTransportInstance {
89 public:
90   HfpDecodingTransport(SessionType sessionType);
91 
92   ~HfpDecodingTransport();
93 
94   BluetoothAudioCtrlAck StartRequest(bool is_low_latency);
95 
96   BluetoothAudioCtrlAck SuspendRequest();
97 
98   void StopRequest();
99 
100   void SetLatencyMode(LatencyMode latency_mode);
101 
102   bool GetPresentationPosition(uint64_t* remote_delay_report_ns, uint64_t* total_bytes_read,
103                                timespec* data_position);
104 
105   void SourceMetadataChanged(const source_metadata_v7_t& source_metadata);
106 
107   void SinkMetadataChanged(const sink_metadata_v7_t& sink_metadata);
108 
109   void ResetPresentationPosition();
110 
111   void LogBytesWritten(size_t bytes_written) override;
112 
113   uint8_t GetPendingCmd() const;
114 
115   void ResetPendingCmd();
116 
117   bool IsStreamActive();
118 
119   static inline HfpDecodingTransport* instance_ = nullptr;
120   static inline BluetoothAudioSourceClientInterface* software_hal_interface = nullptr;
121   static inline BluetoothAudioSourceClientInterface* offloading_hal_interface = nullptr;
122   static inline BluetoothAudioSourceClientInterface* active_hal_interface = nullptr;
123 
124 private:
125   HfpTransport* transport_;
126 };
127 
128 class HfpEncodingTransport : public ::bluetooth::audio::aidl::IBluetoothSinkTransportInstance {
129 public:
130   HfpEncodingTransport(SessionType sessionType);
131 
132   ~HfpEncodingTransport();
133 
134   BluetoothAudioCtrlAck StartRequest(bool is_low_latency);
135 
136   BluetoothAudioCtrlAck SuspendRequest();
137 
138   void StopRequest();
139 
140   void SetLatencyMode(LatencyMode latency_mode);
141 
142   bool GetPresentationPosition(uint64_t* remote_delay_report_ns, 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 LogBytesRead(size_t bytes_read) override;
152 
153   uint8_t GetPendingCmd() const;
154 
155   void ResetPendingCmd();
156 
157   bool IsStreamActive();
158 
159   static inline HfpEncodingTransport* instance_ = nullptr;
160   static inline BluetoothAudioSinkClientInterface* software_hal_interface = nullptr;
161   static inline BluetoothAudioSinkClientInterface* offloading_hal_interface = nullptr;
162   static inline BluetoothAudioSinkClientInterface* active_hal_interface = nullptr;
163 
164 private:
165   HfpTransport* transport_;
166 };
167 
168 }  // namespace hfp
169 }  // namespace aidl
170 }  // namespace audio
171 }  // namespace bluetooth
172 
173 namespace std {
174 template <>
175 struct formatter<bluetooth::audio::aidl::hfp::tHFP_CTRL_CMD>
176     : enum_formatter<bluetooth::audio::aidl::hfp::tHFP_CTRL_CMD> {};
177 }  // namespace std
178