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