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 #include <memory>
19 
20 #include "btm_iso_api.h"
21 #include "btm_iso_impl.h"
22 #include "stack/include/bt_hdr.h"
23 
24 using bluetooth::hci::iso_manager::BigCallbacks;
25 using bluetooth::hci::iso_manager::CigCallbacks;
26 using bluetooth::hci::iso_manager::iso_impl;
27 
28 namespace bluetooth {
29 namespace hci {
30 
31 struct IsoManager::impl {
implbluetooth::hci::IsoManager::impl32   impl(const IsoManager& iso_manager) : iso_manager_(iso_manager) {}
33 
Startbluetooth::hci::IsoManager::impl34   void Start() {
35     log::assert_that(iso_impl_ == nullptr,
36                      "assert failed: iso_impl_ == nullptr");
37     iso_impl_ = std::make_unique<iso_impl>();
38   }
39 
Stopbluetooth::hci::IsoManager::impl40   void Stop() {
41     log::assert_that(iso_impl_ != nullptr,
42                      "assert failed: iso_impl_ != nullptr");
43     iso_impl_.reset();
44   }
45 
Dumpbluetooth::hci::IsoManager::impl46   void Dump(int fd) {
47     if (iso_impl_) iso_impl_->dump(fd);
48   }
49 
IsRunningbluetooth::hci::IsoManager::impl50   bool IsRunning() { return iso_impl_ ? true : false; }
51 
52   const IsoManager& iso_manager_;
53   std::unique_ptr<iso_impl> iso_impl_;
54 };
55 
IsoManager()56 IsoManager::IsoManager() : pimpl_(std::make_unique<impl>(*this)) {}
57 
RegisterCigCallbacks(CigCallbacks * callbacks) const58 void IsoManager::RegisterCigCallbacks(CigCallbacks* callbacks) const {
59   pimpl_->iso_impl_->handle_register_cis_callbacks(callbacks);
60 }
61 
RegisterBigCallbacks(BigCallbacks * callbacks) const62 void IsoManager::RegisterBigCallbacks(BigCallbacks* callbacks) const {
63   pimpl_->iso_impl_->handle_register_big_callbacks(callbacks);
64 }
65 
RegisterOnIsoTrafficActiveCallback(void callback (bool)) const66 void IsoManager::RegisterOnIsoTrafficActiveCallback(void callback(bool)) const {
67   pimpl_->iso_impl_->handle_register_on_iso_traffic_active_callback(callback);
68 }
69 
CreateCig(uint8_t cig_id,struct iso_manager::cig_create_params cig_params)70 void IsoManager::CreateCig(uint8_t cig_id,
71                            struct iso_manager::cig_create_params cig_params) {
72   pimpl_->iso_impl_->create_cig(cig_id, std::move(cig_params));
73 }
74 
ReconfigureCig(uint8_t cig_id,struct iso_manager::cig_create_params cig_params)75 void IsoManager::ReconfigureCig(
76     uint8_t cig_id, struct iso_manager::cig_create_params cig_params) {
77   pimpl_->iso_impl_->reconfigure_cig(cig_id, std::move(cig_params));
78 }
79 
RemoveCig(uint8_t cig_id,bool force)80 void IsoManager::RemoveCig(uint8_t cig_id, bool force) {
81   pimpl_->iso_impl_->remove_cig(cig_id, force);
82 }
83 
EstablishCis(struct iso_manager::cis_establish_params conn_params)84 void IsoManager::EstablishCis(
85     struct iso_manager::cis_establish_params conn_params) {
86   pimpl_->iso_impl_->establish_cis(std::move(conn_params));
87 }
88 
DisconnectCis(uint16_t cis_handle,uint8_t reason)89 void IsoManager::DisconnectCis(uint16_t cis_handle, uint8_t reason) {
90   pimpl_->iso_impl_->disconnect_cis(cis_handle, reason);
91 }
92 
SetupIsoDataPath(uint16_t iso_handle,struct iso_manager::iso_data_path_params path_params)93 void IsoManager::SetupIsoDataPath(
94     uint16_t iso_handle, struct iso_manager::iso_data_path_params path_params) {
95   pimpl_->iso_impl_->setup_iso_data_path(iso_handle, std::move(path_params));
96 }
97 
RemoveIsoDataPath(uint16_t iso_handle,uint8_t data_path_dir)98 void IsoManager::RemoveIsoDataPath(uint16_t iso_handle, uint8_t data_path_dir) {
99   pimpl_->iso_impl_->remove_iso_data_path(iso_handle, data_path_dir);
100 }
101 
ReadIsoLinkQuality(uint16_t iso_handle)102 void IsoManager::ReadIsoLinkQuality(uint16_t iso_handle) {
103   pimpl_->iso_impl_->read_iso_link_quality(iso_handle);
104 }
105 
SendIsoData(uint16_t iso_handle,const uint8_t * data,uint16_t data_len)106 void IsoManager::SendIsoData(uint16_t iso_handle, const uint8_t* data,
107                              uint16_t data_len) {
108   pimpl_->iso_impl_->send_iso_data(iso_handle, data, data_len);
109 }
110 
CreateBig(uint8_t big_id,struct iso_manager::big_create_params big_params)111 void IsoManager::CreateBig(uint8_t big_id,
112                            struct iso_manager::big_create_params big_params) {
113   pimpl_->iso_impl_->create_big(big_id, std::move(big_params));
114 }
115 
TerminateBig(uint8_t big_id,uint8_t reason)116 void IsoManager::TerminateBig(uint8_t big_id, uint8_t reason) {
117   if (pimpl_->IsRunning())
118     pimpl_->iso_impl_->terminate_big(big_id, reason);
119 }
120 
HandleIsoData(void * p_msg)121 void IsoManager::HandleIsoData(void* p_msg) {
122   if (pimpl_->IsRunning())
123     pimpl_->iso_impl_->handle_iso_data(static_cast<BT_HDR*>(p_msg));
124 }
125 
HandleDisconnect(uint16_t handle,uint8_t reason)126 void IsoManager::HandleDisconnect(uint16_t handle, uint8_t reason) {
127   if (pimpl_->IsRunning())
128     pimpl_->iso_impl_->disconnection_complete(handle, reason);
129 }
130 
HandleNumComplDataPkts(uint16_t handle,uint16_t credits)131 void IsoManager::HandleNumComplDataPkts(uint16_t handle, uint16_t credits) {
132   if (pimpl_->IsRunning())
133     pimpl_->iso_impl_->handle_gd_num_completed_pkts(handle, credits);
134 }
135 
HandleHciEvent(uint8_t sub_code,uint8_t * params,uint16_t length)136 void IsoManager::HandleHciEvent(uint8_t sub_code, uint8_t* params,
137                                 uint16_t length) {
138   if (pimpl_->IsRunning())
139     pimpl_->iso_impl_->on_iso_event(sub_code, params, length);
140 }
141 
Start()142 void IsoManager::Start() {
143   if (!pimpl_->IsRunning())
144     pimpl_->Start();
145 }
146 
Stop()147 void IsoManager::Stop() {
148   if (pimpl_->IsRunning())
149     pimpl_->Stop();
150 }
151 
Dump(int fd)152 void IsoManager::Dump(int fd) {
153   if (pimpl_->IsRunning()) pimpl_->Dump(fd);
154 }
155 
156 IsoManager::~IsoManager() = default;
157 
GetInstance()158 IsoManager* IsoManager::GetInstance() {
159   static IsoManager* instance = new IsoManager();
160   return instance;
161 }
162 
163 }  // namespace hci
164 }  // namespace bluetooth
165