1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "hci/controller.h"
20 #include "hci/hci_layer.h"
21 #include "hci/hci_packets.h"
22 #include "os/handler.h"
23 
24 #include <list>
25 
26 namespace bluetooth {
27 namespace iso {
28 using SetCigParametersCallback = common::ContextualOnceCallback<void(std::vector<uint16_t>)>;
29 using CisEstablishedCallback = common::ContextualCallback<void(uint16_t)>;
30 using IsoDataCallback = common::ContextualCallback<void(std::unique_ptr<hci::IsoView>)>;
31 
32 namespace internal {
33 
34 struct IsochronousConnection {
35   uint16_t connection_handle;
36   uint8_t cig_id;
37   uint8_t cis_id;
38 };
39 
40 class IsoManagerImpl {
41  public:
42   explicit IsoManagerImpl(os::Handler* iso_handler, hci::HciLayer* hci_layer, hci::Controller* controller);
43   ~IsoManagerImpl();
44 
RegisterIsoEstablishedCallback(CisEstablishedCallback cb)45   void RegisterIsoEstablishedCallback(CisEstablishedCallback cb) {
46     this->cis_established_callback = cb;
47   }
48 
RegisterIsoDataCallback(IsoDataCallback cb)49   void RegisterIsoDataCallback(IsoDataCallback cb) {
50     this->iso_data_callback = cb;
51   }
52 
53   void OnHciLeEvent(hci::LeMetaEventView event);
54 
55   void SetCigParameters(
56       uint8_t cig_id,
57       uint32_t sdu_interval_m_to_s,
58       uint32_t sdu_interval_s_to_m,
59       hci::ClockAccuracy peripherals_clock_accuracy,
60       hci::Packing packing,
61       hci::Enable framing,
62       uint16_t max_transport_latency_m_to_s,
63       uint16_t max_transport_latency_s_to_m,
64       const std::vector<hci::CisParametersConfig>& cis_config,
65       SetCigParametersCallback command_complete_callback);
66   void SetCigParametersComplete(
67       uint8_t cig_id,
68       const std::vector<hci::CisParametersConfig>& cis_configs,
69       SetCigParametersCallback command_complete_callback,
70       hci::CommandCompleteView command_complete);
71 
72   void SetCigParametersTest(
73       uint8_t cig_id,
74       uint32_t sdu_interval_m_to_s,
75       uint32_t sdu_interval_s_to_m,
76       uint8_t ft_m_to_s,
77       uint8_t ft_s_to_m,
78       uint16_t iso_interval,
79       hci::ClockAccuracy peripherals_clock_accuracy,
80       hci::Packing packing,
81       hci::Enable framing,
82       uint16_t max_transport_latency_m_to_s,
83       uint16_t max_transport_latency_s_to_m,
84       const std::vector<hci::LeCisParametersTestConfig>& cis_config,
85       SetCigParametersCallback command_complete_callback);
86   void SetCigParametersTestComplete(
87       uint8_t cig_id,
88       const std::vector<hci::LeCisParametersTestConfig>& cis_configs,
89       SetCigParametersCallback command_complete_callback,
90       hci::CommandCompleteView command_complete);
91 
92   void LeCreateCis(std::vector<std::pair<uint16_t, uint16_t>> cis_and_acl_handles);
93 
94   void RemoveCig(uint8_t cig_id);
95   void RemoveCigComplete(hci::CommandCompleteView command_complete);
96 
97   void SendIsoPacket(uint16_t cis_handle, std::vector<uint8_t> packet);
98   void OnIncomingPacket();
99 
IsKnownCig(uint8_t cig_id)100   bool IsKnownCig(uint8_t cig_id) {
101     return find_if(iso_connections_.begin(), iso_connections_.end(), [cig_id](const IsochronousConnection& c) {
102              return c.cig_id == cig_id;
103            }) != iso_connections_.end();
104   }
105 
106  private:
107   os::Handler* iso_handler_;
108   hci::HciLayer* hci_layer_;
109   hci::LeIsoInterface* hci_le_iso_interface_;
110   std::unique_ptr<os::EnqueueBuffer<bluetooth::hci::IsoBuilder>> iso_enqueue_buffer_;
111   hci::Controller* controller_ __attribute__((unused));
112   std::list<IsochronousConnection> iso_connections_;
113   CisEstablishedCallback cis_established_callback;
114   IsoDataCallback iso_data_callback;
115 };
116 }  // namespace internal
117 }  // namespace iso
118 }  // namespace bluetooth
119