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 <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "btm_iso_api_types.h"
25 
26 namespace bluetooth {
27 namespace hci {
28 namespace iso_manager {
29 struct CigCallbacks {
30   virtual ~CigCallbacks() = default;
31   virtual void OnSetupIsoDataPath(uint8_t status, uint16_t conn_handle,
32                                   uint8_t cig_id) = 0;
33   virtual void OnRemoveIsoDataPath(uint8_t status, uint16_t conn_handle,
34                                    uint8_t cig_id) = 0;
35   virtual void OnIsoLinkQualityRead(
36       uint8_t conn_handle, uint8_t cig_id, uint32_t txUnackedPackets,
37       uint32_t txFlushedPackets, uint32_t txLastSubeventPackets,
38       uint32_t retransmittedPackets, uint32_t crcErrorPackets,
39       uint32_t rxUnreceivedPackets, uint32_t duplicatePackets) = 0;
40 
41   virtual void OnCisEvent(uint8_t event, void* data) = 0;
42   virtual void OnCigEvent(uint8_t event, void* data) = 0;
43 };
44 
45 struct BigCallbacks {
46   virtual ~BigCallbacks() = default;
47   virtual void OnSetupIsoDataPath(uint8_t status, uint16_t conn_handle,
48                                   uint8_t big_id) = 0;
49   virtual void OnRemoveIsoDataPath(uint8_t status, uint16_t conn_handle,
50                                    uint8_t big_id) = 0;
51 
52   virtual void OnBigEvent(uint8_t event, void* data) = 0;
53 };
54 }  // namespace iso_manager
55 
56 class IsoManager {
57  public:
58   IsoManager();
59   virtual ~IsoManager();
60 
GetInstance()61   static IsoManager* GetInstance() {
62     static IsoManager* instance = new IsoManager();
63     return instance;
64   }
65 
66   /**
67    * Set CIG and CIS related callbacks
68    *
69    * <p> Shall be set by the Le Audio Unicaster implementation
70    *
71    * @param callbacks CigCallbacks implementation
72    */
73   virtual void RegisterCigCallbacks(iso_manager::CigCallbacks* callbacks) const;
74 
75   /**
76    * Set BIG related callbacks
77    *
78    * <p> Shall be set by the Le Audio Broadcaster implementation
79    *
80    * @param callbacks BigCallbacks implementation
81    */
82   virtual void RegisterBigCallbacks(iso_manager::BigCallbacks* callbacks) const;
83 
84   /**
85    * Creates connected isochronous group (CIG) according to given params.
86    *
87    * @param cig_id connected isochronous group id
88    * @param cig_params CIG parameters
89    */
90   virtual void CreateCig(uint8_t cig_id,
91                          struct iso_manager::cig_create_params cig_params);
92 
93   /**
94    * Reconfigures connected isochronous group (CIG) according to given params.
95    *
96    * @param cig_id connected isochronous group id
97    * @param cig_params CIG parameters
98    */
99   virtual void ReconfigureCig(uint8_t cig_id,
100                               struct iso_manager::cig_create_params cig_params);
101 
102   /**
103    * Initiates removing of connected isochronous group (CIG).
104    *
105    * @param cig_id connected isochronous group id
106    */
107   virtual void RemoveCig(uint8_t cig_id);
108 
109   /**
110    * Initiates creation of connected isochronous stream (CIS).
111    *
112    * @param conn_params A set of cis and acl connection handles
113    */
114   virtual void EstablishCis(
115       struct iso_manager::cis_establish_params conn_params);
116 
117   /**
118    * Initiates disconnection of connected isochronous stream (CIS).
119    *
120    * @param conn_handle CIS connection handle
121    * @param reason HCI reason for disconnection
122    */
123   virtual void DisconnectCis(uint16_t conn_handle, uint8_t reason);
124 
125   /**
126    * Initiates creation of isochronous data path for connected isochronous
127    * stream.
128    *
129    * @param conn_handle handle of BIS or CIS connection
130    * @param path_params iso data path parameters
131    */
132   virtual void SetupIsoDataPath(
133       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,
161                            uint16_t data_len);
162 
163   /**
164    * Creates the Broadcast Isochronous Group
165    *
166    * @param big_id host assigned BIG identifier
167    * @param big_params BIG parameters
168    */
169   virtual void CreateBig(uint8_t big_id,
170                          struct iso_manager::big_create_params big_params);
171 
172   /**
173    * Terminates the Broadcast Isochronous Group
174    *
175    * @param big_id host assigned BIG identifier
176    * @param reason termination reason data
177    */
178   virtual void TerminateBig(uint8_t big_id, uint8_t reason);
179 
180   /* Below are defined handlers called by the legacy code in btu_hcif.cc */
181 
182   /**
183    * Handles Iso Data packets from the controller
184    *
185    * @param p_msg raw data packet. The ownership of p_msg is not being
186    * transferred.
187    */
188   virtual void HandleIsoData(void* p_msg);
189 
190   /**
191    * Handles disconnect HCI event
192    *
193    * <p> This callback can be called with handles other than ISO connection
194    * handles.
195    *
196    * @param conn_handle connection handle
197    * @param reason HCI reason for disconnection
198    */
199   virtual void HandleDisconnect(uint16_t conn_handle, uint8_t reason);
200 
201   /**
202    * Handles HCI event for the number of completed packets
203    *
204    * @param p raw packet buffer for the event. The ownership of p is not being
205    * transferred.
206    * @param evt_len event packet buffer length
207    */
208   virtual void HandleNumComplDataPkts(uint8_t* p, uint8_t evt_len);
209 
210   /**
211    * Handle CIS and BIG related HCI events
212    *
213    * @param sub_code ble subcode for the HCI event
214    * @param params raw packet buffer for the event. The ownership of params is
215    * not being transferred
216    * @param length event packet buffer length
217    */
218   virtual void HandleHciEvent(uint8_t sub_code, uint8_t* params,
219                               uint16_t length);
220 
221   /**
222    * Starts the IsoManager module
223    */
224   void Start();
225 
226   /**
227    * Stops the IsoManager module
228    */
229   void Stop();
230 
231  private:
232   struct impl;
233   std::unique_ptr<impl> pimpl_;
234 
235   DISALLOW_COPY_AND_ASSIGN(IsoManager);
236 };
237 
238 }  // namespace hci
239 }  // namespace bluetooth
240