1 /*
2  * Copyright 2020 HIMSA II K/S - www.himsa.com.
3  * Represented by EHIMA - www.ehima.com
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <cstdint>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "btm_iso_api_types.h"
26 
27 namespace bluetooth {
28 namespace hci {
29 namespace iso_manager {
30 struct CigCallbacks {
31   virtual ~CigCallbacks() = default;
32   virtual void OnSetupIsoDataPath(uint8_t status, uint16_t conn_handle,
33                                   uint8_t cig_id) = 0;
34   virtual void OnRemoveIsoDataPath(uint8_t status, uint16_t conn_handle,
35                                    uint8_t cig_id) = 0;
36   virtual void OnIsoLinkQualityRead(
37       uint8_t conn_handle, uint8_t cig_id, uint32_t txUnackedPackets,
38       uint32_t txFlushedPackets, uint32_t txLastSubeventPackets,
39       uint32_t retransmittedPackets, uint32_t crcErrorPackets,
40       uint32_t rxUnreceivedPackets, uint32_t duplicatePackets) = 0;
41 
42   virtual void OnCisEvent(uint8_t event, void* data) = 0;
43   virtual void OnCigEvent(uint8_t event, void* data) = 0;
44 };
45 
46 struct BigCallbacks {
47   virtual ~BigCallbacks() = default;
48   virtual void OnSetupIsoDataPath(uint8_t status, uint16_t conn_handle,
49                                   uint8_t big_id) = 0;
50   virtual void OnRemoveIsoDataPath(uint8_t status, uint16_t conn_handle,
51                                    uint8_t big_id) = 0;
52 
53   virtual void OnBigEvent(uint8_t event, void* data) = 0;
54 };
55 }  // namespace iso_manager
56 
57 class IsoManager {
58  public:
59   IsoManager();
60   IsoManager(const IsoManager&) = delete;
61   IsoManager& operator=(const IsoManager&) = delete;
62 
63   virtual ~IsoManager();
64 
65   static IsoManager* GetInstance();
66 
67   /**
68    * Set CIG and CIS related callbacks
69    *
70    * <p> Shall be set by the Le Audio Unicaster implementation
71    *
72    * @param callbacks CigCallbacks implementation
73    */
74   virtual void RegisterCigCallbacks(iso_manager::CigCallbacks* callbacks) const;
75 
76   /**
77    * Set BIG related callbacks
78    *
79    * <p> Shall be set by the Le Audio Broadcaster implementation
80    *
81    * @param callbacks BigCallbacks implementation
82    */
83   virtual void RegisterBigCallbacks(iso_manager::BigCallbacks* callbacks) const;
84 
85   /**
86    * Set true when CIG or BIG is active, false when CIG or BIG is closed
87    *
88    * @param callback function takes bool as parameter and return void
89    */
90   virtual void RegisterOnIsoTrafficActiveCallback(void callback(bool)) const;
91 
92   /**
93    * Creates connected isochronous group (CIG) according to given params.
94    *
95    * @param cig_id connected isochronous group id
96    * @param cig_params CIG parameters
97    */
98   virtual void CreateCig(uint8_t cig_id,
99                          struct iso_manager::cig_create_params cig_params);
100 
101   /**
102    * Reconfigures connected isochronous group (CIG) according to given params.
103    *
104    * @param cig_id connected isochronous group id
105    * @param cig_params CIG parameters
106    */
107   virtual void ReconfigureCig(uint8_t cig_id,
108                               struct iso_manager::cig_create_params cig_params);
109 
110   /**
111    * Initiates removing of connected isochronous group (CIG).
112    *
113    * @param cig_id connected isochronous group id
114    * @param force do not check if CIG exist
115    */
116   virtual void RemoveCig(uint8_t cig_id, bool force = false);
117 
118   /**
119    * Initiates creation of connected isochronous stream (CIS).
120    *
121    * @param conn_params A set of cis and acl connection handles
122    */
123   virtual void EstablishCis(
124       struct iso_manager::cis_establish_params conn_params);
125 
126   /**
127    * Initiates disconnection of connected isochronous stream (CIS).
128    *
129    * @param conn_handle CIS connection handle
130    * @param reason HCI reason for disconnection
131    */
132   virtual void DisconnectCis(uint16_t conn_handle, uint8_t reason);
133 
134   /**
135    * Initiates creation of isochronous data path for connected isochronous
136    * stream.
137    *
138    * @param conn_handle handle of BIS or CIS connection
139    * @param path_params iso data path parameters
140    */
141   virtual void SetupIsoDataPath(
142       uint16_t conn_handle,
143       struct iso_manager::iso_data_path_params path_params);
144 
145   /**
146    * Initiates removal of isochronous data path for connected isochronous
147    * stream.
148    *
149    * @param conn_handle handle of BIS or CIS connection
150    * @param data_path_dir iso data path direction
151    */
152   virtual void RemoveIsoDataPath(uint16_t conn_handle, uint8_t data_path_dir);
153 
154   /**
155    * Reads the ISO link quality. OnIsoLinkQualityRead callback is invoked only
156    * if read is successful.
157    *
158    * @param conn_handle handle of ISO connection
159    */
160   virtual void ReadIsoLinkQuality(uint16_t conn_handle);
161 
162   /**
163    * Sends iso data to the controller
164    *
165    * @param conn_handle handle of BIS or CIS connection
166    * @param data data buffer. The ownership of data is not being transferred.
167    * @param data_len data buffer length
168    */
169   virtual void SendIsoData(uint16_t conn_handle, const uint8_t* data,
170                            uint16_t data_len);
171 
172   /**
173    * Creates the Broadcast Isochronous Group
174    *
175    * @param big_id host assigned BIG identifier
176    * @param big_params BIG parameters
177    */
178   virtual void CreateBig(uint8_t big_id,
179                          struct iso_manager::big_create_params big_params);
180 
181   /**
182    * Terminates the Broadcast Isochronous Group
183    *
184    * @param big_id host assigned BIG identifier
185    * @param reason termination reason data
186    */
187   virtual void TerminateBig(uint8_t big_id, uint8_t reason);
188 
189   /* Below are defined handlers called by the legacy code in btu_hcif.cc */
190 
191   /**
192    * Handles Iso Data packets from the controller
193    *
194    * @param p_msg raw data packet. The ownership of p_msg is not being
195    * transferred.
196    */
197   virtual void HandleIsoData(void* p_msg);
198 
199   /**
200    * Handles disconnect HCI event
201    *
202    * <p> This callback can be called with handles other than ISO connection
203    * handles.
204    *
205    * @param conn_handle connection handle
206    * @param reason HCI reason for disconnection
207    */
208   virtual void HandleDisconnect(uint16_t conn_handle, uint8_t reason);
209 
210   /**
211    * Handles the number of completed packets
212    *
213    * @param handle - the handle for which there are completed packets
214    * @param credits - the number of packets completed
215    */
216   virtual void HandleNumComplDataPkts(uint16_t handle, uint16_t credits);
217 
218   /**
219    * Handle CIS and BIG related HCI events
220    *
221    * @param sub_code ble subcode for the HCI event
222    * @param params raw packet buffer for the event. The ownership of params is
223    * not being transferred
224    * @param length event packet buffer length
225    */
226   virtual void HandleHciEvent(uint8_t sub_code, uint8_t* params,
227                               uint16_t length);
228 
229   /**
230    * Starts the IsoManager module
231    */
232   void Start();
233 
234   /**
235    * Stops the IsoManager module
236    */
237   void Stop();
238 
239   /**
240    * Dumps the IsoManager module state
241    */
242   void Dump(int fd);
243 
244  private:
245   struct impl;
246   std::unique_ptr<impl> pimpl_;
247 };
248 
249 }  // namespace hci
250 }  // namespace bluetooth
251