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, uint8_t cig_id) = 0;
33   virtual void OnRemoveIsoDataPath(uint8_t status, uint16_t conn_handle, uint8_t cig_id) = 0;
34   virtual void OnIsoLinkQualityRead(uint8_t conn_handle, uint8_t cig_id, uint32_t txUnackedPackets,
35                                     uint32_t txFlushedPackets, uint32_t txLastSubeventPackets,
36                                     uint32_t retransmittedPackets, uint32_t crcErrorPackets,
37                                     uint32_t rxUnreceivedPackets, uint32_t duplicatePackets) = 0;
38 
39   virtual void OnCisEvent(uint8_t event, void* data) = 0;
40   virtual void OnCigEvent(uint8_t event, void* data) = 0;
41 };
42 
43 struct BigCallbacks {
44   virtual ~BigCallbacks() = default;
45   virtual void OnSetupIsoDataPath(uint8_t status, uint16_t conn_handle, uint8_t big_id) = 0;
46   virtual void OnRemoveIsoDataPath(uint8_t status, uint16_t conn_handle, uint8_t big_id) = 0;
47 
48   virtual void OnBigEvent(uint8_t event, void* data) = 0;
49 };
50 }  // namespace iso_manager
51 
52 class IsoManager {
53 public:
54   IsoManager();
55   IsoManager(const IsoManager&) = delete;
56   IsoManager& operator=(const IsoManager&) = delete;
57 
58   virtual ~IsoManager();
59 
60   static IsoManager* GetInstance();
61 
62   /**
63    * Set CIG and CIS related callbacks
64    *
65    * <p> Shall be set by the Le Audio Unicaster implementation
66    *
67    * @param callbacks CigCallbacks implementation
68    */
69   virtual void RegisterCigCallbacks(iso_manager::CigCallbacks* callbacks) const;
70 
71   /**
72    * Set BIG related callbacks
73    *
74    * <p> Shall be set by the Le Audio Broadcaster implementation
75    *
76    * @param callbacks BigCallbacks implementation
77    */
78   virtual void RegisterBigCallbacks(iso_manager::BigCallbacks* callbacks) const;
79 
80   /**
81    * Set true when CIG or BIG is active, false when CIG or BIG is closed
82    *
83    * @param callback function takes bool as parameter and return void
84    */
85   virtual void RegisterOnIsoTrafficActiveCallback(void callback(bool)) const;
86 
87   /**
88    * Creates connected isochronous group (CIG) according to given params.
89    *
90    * @param cig_id connected isochronous group id
91    * @param cig_params CIG parameters
92    */
93   virtual void CreateCig(uint8_t cig_id, struct iso_manager::cig_create_params cig_params);
94 
95   /**
96    * Reconfigures connected isochronous group (CIG) according to given params.
97    *
98    * @param cig_id connected isochronous group id
99    * @param cig_params CIG parameters
100    */
101   virtual void ReconfigureCig(uint8_t cig_id, struct iso_manager::cig_create_params cig_params);
102 
103   /**
104    * Initiates removing of connected isochronous group (CIG).
105    *
106    * @param cig_id connected isochronous group id
107    * @param force do not check if CIG exist
108    */
109   virtual void RemoveCig(uint8_t cig_id, bool force = false);
110 
111   /**
112    * Initiates creation of connected isochronous stream (CIS).
113    *
114    * @param conn_params A set of cis and acl connection handles
115    */
116   virtual void EstablishCis(struct iso_manager::cis_establish_params conn_params);
117 
118   /**
119    * Initiates disconnection of connected isochronous stream (CIS).
120    *
121    * @param conn_handle CIS connection handle
122    * @param reason HCI reason for disconnection
123    */
124   virtual void DisconnectCis(uint16_t conn_handle, uint8_t reason);
125 
126   /**
127    * Initiates creation of isochronous data path for connected isochronous
128    * stream.
129    *
130    * @param conn_handle handle of BIS or CIS connection
131    * @param path_params iso data path parameters
132    */
133   virtual void SetupIsoDataPath(uint16_t conn_handle,
134                                 struct iso_manager::iso_data_path_params path_params);
135 
136   /**
137    * Initiates removal of isochronous data path for connected isochronous
138    * stream.
139    *
140    * @param conn_handle handle of BIS or CIS connection
141    * @param data_path_dir iso data path direction
142    */
143   virtual void RemoveIsoDataPath(uint16_t conn_handle, uint8_t data_path_dir);
144 
145   /**
146    * Reads the ISO link quality. OnIsoLinkQualityRead callback is invoked only
147    * if read is successful.
148    *
149    * @param conn_handle handle of ISO connection
150    */
151   virtual void ReadIsoLinkQuality(uint16_t conn_handle);
152 
153   /**
154    * Sends iso data to the controller
155    *
156    * @param conn_handle handle of BIS or CIS connection
157    * @param data data buffer. The ownership of data is not being transferred.
158    * @param data_len data buffer length
159    */
160   virtual void SendIsoData(uint16_t conn_handle, const uint8_t* data, uint16_t data_len);
161 
162   /**
163    * Creates the Broadcast Isochronous Group
164    *
165    * @param big_id host assigned BIG identifier
166    * @param big_params BIG parameters
167    */
168   virtual void CreateBig(uint8_t big_id, struct iso_manager::big_create_params big_params);
169 
170   /**
171    * Terminates the Broadcast Isochronous Group
172    *
173    * @param big_id host assigned BIG identifier
174    * @param reason termination reason data
175    */
176   virtual void TerminateBig(uint8_t big_id, uint8_t reason);
177 
178   /* Below are defined handlers called by the legacy code in btu_hcif.cc */
179 
180   /**
181    * Handles Iso Data packets from the controller
182    *
183    * @param p_msg raw data packet. The ownership of p_msg is not being
184    * transferred.
185    */
186   virtual void HandleIsoData(void* p_msg);
187 
188   /**
189    * Handles disconnect HCI event
190    *
191    * <p> This callback can be called with handles other than ISO connection
192    * handles.
193    *
194    * @param conn_handle connection handle
195    * @param reason HCI reason for disconnection
196    */
197   virtual void HandleDisconnect(uint16_t conn_handle, uint8_t reason);
198 
199   /**
200    * Handles the number of completed packets
201    *
202    * @param handle - the handle for which there are completed packets
203    * @param credits - the number of packets completed
204    */
205   virtual void HandleNumComplDataPkts(uint16_t handle, uint16_t credits);
206 
207   /**
208    * Handle CIS and BIG related HCI events
209    *
210    * @param sub_code ble subcode for the HCI event
211    * @param params raw packet buffer for the event. The ownership of params is
212    * not being transferred
213    * @param length event packet buffer length
214    */
215   virtual void HandleHciEvent(uint8_t sub_code, uint8_t* params, uint16_t length);
216 
217   /**
218    * Return the current number of ISO channels
219    */
220   virtual int GetNumberOfActiveIso();
221 
222   /**
223    * Starts the IsoManager module
224    */
225   void Start();
226 
227   /**
228    * Stops the IsoManager module
229    */
230   void Stop();
231 
232   /**
233    * Dumps the IsoManager module state
234    */
235   void Dump(int fd);
236 
237 private:
238   struct impl;
239   std::unique_ptr<impl> pimpl_;
240 };
241 
242 }  // namespace hci
243 }  // namespace bluetooth
244