summaryrefslogtreecommitdiff
path: root/system/gd/iso/facade.cc
blob: 42779f0e63723f0545a5cb82a873ebe2258e12a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "iso/facade.h"

#include "blueberry/facade/iso/facade.grpc.pb.h"
#include "common/contextual_callback.h"
#include "grpc/grpc_event_queue.h"
#include "hci/acl_manager.h"
#include "hci/address_with_type.h"
#include "hci/le_address_manager.h"
#include "iso/iso_module.h"
#include "os/handler.h"

using bluetooth::hci::AclManager;

namespace bluetooth {
namespace iso {

using namespace blueberry::facade::iso;

class IsoModuleFacadeService : public IsoModuleFacade::Service {
 public:
  IsoModuleFacadeService(IsoModule* iso_module, AclManager* acl_manager, ::bluetooth::os::Handler* iso_handler)
      : iso_module_(iso_module), acl_manager_(acl_manager), iso_handler_(iso_handler) {
    ASSERT(iso_module_);
    ASSERT(iso_handler_);

    iso_module_->GetIsoManager()->RegisterIsoEstablishedCallback(iso_handler_->Bind(
        [](::bluetooth::grpc::GrpcEventQueue<LeIsoEventsMsg>* le_iso_events_, uint16_t cis_connection_handle) {
          LeIsoEventsMsg msg;
          msg.set_message_type(IsoMsgType::ISO_CIS_ESTABLISHED);
          msg.add_cis_handle(cis_connection_handle);
          le_iso_events_->OnIncomingEvent(msg);
        },
        &le_iso_events_));

    iso_module_->GetIsoManager()->RegisterIsoDataCallback(
        iso_handler_->BindOn(this, &IsoModuleFacadeService::OnIsoPacketReceived));
  }

  ::grpc::Status LeSetCigParameters(
      ::grpc::ServerContext* context,
      const ::bluetooth::iso::LeSetCigParametersRequest* request,
      ::google::protobuf::Empty* response) override {
    std::vector<hci::CisParametersConfig> cis_config;

    hci::CisParametersConfig cfg;
    cfg.cis_id_ = request->cis_id();
    cfg.max_sdu_m_to_s_ = request->max_sdu_m_to_s();
    cfg.max_sdu_s_to_m_ = request->max_sdu_s_to_m();
    cfg.phy_m_to_s_ = request->phy_m_to_s();
    cfg.phy_s_to_m_ = request->phy_s_to_m();
    cfg.rtn_m_to_s_ = request->rtn_m_to_s();
    cfg.rtn_s_to_m_ = request->rtn_s_to_m();

    cis_config.push_back(cfg);

    iso_module_->GetIsoManager()->SetCigParameters(
        request->cig_id(),
        request->sdu_interval_m_to_s(),
        request->sdu_interval_s_to_m(),
        static_cast<hci::ClockAccuracy>(request->peripherals_clock_accuracy()),
        static_cast<hci::Packing>(request->packing()),
        static_cast<hci::Enable>(request->framing()),
        request->max_transport_latency_m_to_s(),
        request->max_transport_latency_s_to_m(),
        cis_config,
        iso_handler_->BindOnce(
            [](::bluetooth::grpc::GrpcEventQueue<LeIsoEventsMsg>* le_iso_events_, std::vector<uint16_t> conn_handles) {
              LeIsoEventsMsg msg;

              msg.set_message_type(IsoMsgType::ISO_PARAMETERS_SET_COMPLETE);
              for (const uint16_t conn_handle : conn_handles) {
                msg.add_cis_handle(conn_handle);
              }
              le_iso_events_->OnIncomingEvent(msg);
            },
            &le_iso_events_));
    return ::grpc::Status::OK;
  }

  ::grpc::Status LeSetCigParametersTest(
      ::grpc::ServerContext* context,
      const ::bluetooth::iso::LeSetCigParametersTestRequest* request,
      ::google::protobuf::Empty* response) override {
    std::vector<hci::LeCisParametersTestConfig> cis_config;

    for (const auto& cc : request->cis_configs()) {
      hci::LeCisParametersTestConfig cfg;
      cfg.cis_id_ = cc.cis_id();
      cfg.nse_ = cc.nse();
      cfg.max_sdu_m_to_s_ = cc.max_sdu_m_to_s();
      cfg.max_sdu_s_to_m_ = cc.max_sdu_s_to_m();
      cfg.max_pdu_m_to_s_ = cc.max_pdu_m_to_s();
      cfg.max_pdu_s_to_m_ = cc.max_pdu_s_to_m();
      cfg.phy_m_to_s_ = cc.phy_m_to_s();
      cfg.phy_s_to_m_ = cc.phy_s_to_m();
      cfg.bn_m_to_s_ = cc.bn_m_to_s();
      cfg.bn_s_to_m_ = cc.bn_s_to_m();
      cis_config.push_back(cfg);
    }
    iso_module_->GetIsoManager()->SetCigParametersTest(
        request->cig_id(),
        request->sdu_interval_m_to_s(),
        request->sdu_interval_s_to_m(),
        request->ft_m_to_s(),
        request->ft_s_to_m(),
        request->iso_interval(),
        static_cast<hci::ClockAccuracy>(request->peripherals_clock_accuracy()),
        static_cast<hci::Packing>(request->packing()),
        static_cast<hci::Enable>(request->framing()),
        request->max_transport_latency_m_to_s(),
        request->max_transport_latency_s_to_m(),
        cis_config,
        iso_handler_->BindOnce(
            [](::bluetooth::grpc::GrpcEventQueue<LeIsoEventsMsg>* le_iso_events_, std::vector<uint16_t> conn_handles) {
              LeIsoEventsMsg msg;

              msg.set_message_type(IsoMsgType::ISO_PARAMETERS_SET_COMPLETE);
              for (const uint16_t conn_handle : conn_handles) {
                msg.add_cis_handle(conn_handle);
              }
              le_iso_events_->OnIncomingEvent(msg);
            },
            &le_iso_events_));
    return ::grpc::Status::OK;
  }

  ::grpc::Status LeCreateCis(
      ::grpc::ServerContext* context,
      const ::bluetooth::iso::LeCreateCisRequest* request,
      ::google::protobuf::Empty* response) override {
    std::vector<std::pair<uint16_t, uint16_t>> create_cis_params;
    for (const auto& handle_pair : request->handle_pair()) {
      create_cis_params.push_back(
          std::make_pair<uint16_t, uint16_t>(handle_pair.cis_handle(), handle_pair.acl_handle()));
    }
    iso_module_->GetIsoManager()->LeCreateCis(create_cis_params);

    return ::grpc::Status::OK;
  }

  ::grpc::Status FetchIsoData(
      ::grpc::ServerContext* context, const LeCisHandleMsg* request, ::grpc::ServerWriter<IsoPacket>* writer) override {
    return le_iso_data_.RunLoop(context, writer);
  }

  ::grpc::Status FetchIsoEvents(
      ::grpc::ServerContext* context,
      const google::protobuf::Empty* request,
      ::grpc::ServerWriter<LeIsoEventsMsg>* writer) override {
    return le_iso_events_.RunLoop(context, writer);
  }

  ::grpc::Status SendIsoPacket(
      ::grpc::ServerContext* context,
      const ::bluetooth::iso::IsoPacket* request,
      ::google::protobuf::Empty* response) override {
    std::vector<uint8_t> packet(request->payload().begin(), request->payload().end());
    iso_module_->GetIsoManager()->SendIsoPacket(request->handle(), packet);
    return ::grpc::Status::OK;
  }

  void OnIsoPacketReceived(std::unique_ptr<hci::IsoView> iso_view) {
    ASSERT(iso_view->IsValid());

    IsoPacket packet;
    packet.set_handle(iso_view->GetConnectionHandle());

    if (iso_view->GetTsFlag() == hci::TimeStampFlag::NOT_PRESENT) {
      hci::IsoWithoutTimestampView nts = hci::IsoWithoutTimestampView::Create(*iso_view);
      ASSERT(nts.IsValid());

      auto data_vec = nts.GetPayload();
      std::string data = std::string(data_vec.begin(), data_vec.end());
      packet.set_payload(data);
      le_iso_data_.OnIncomingEvent(packet);
    } else {
      hci::IsoWithTimestampView tsv = hci::IsoWithTimestampView::Create(*iso_view);
      ASSERT(tsv.IsValid());

      auto data_vec = tsv.GetPayload();
      std::string data = std::string(data_vec.begin(), data_vec.end());
      packet.set_payload(data);
      le_iso_data_.OnIncomingEvent(packet);
    }
  }

 private:
  IsoModule* iso_module_;
  ::bluetooth::grpc::GrpcEventQueue<LeIsoEventsMsg> le_iso_events_{"LE ISO events"};
  ::bluetooth::grpc::GrpcEventQueue<IsoPacket> le_iso_data_{"LE ISO data"};
  AclManager* acl_manager_ __attribute__((unused));
  ::bluetooth::os::Handler* iso_handler_;
};

void IsoModuleFacadeModule::ListDependencies(ModuleList* list) const {
  ::bluetooth::grpc::GrpcFacadeModule::ListDependencies(list);
  list->add<IsoModule>();
  list->add<AclManager>();
}

void IsoModuleFacadeModule::Start() {
  ::bluetooth::grpc::GrpcFacadeModule::Start();
  service_ = new IsoModuleFacadeService(GetDependency<IsoModule>(), GetDependency<AclManager>(), GetHandler());
}

void IsoModuleFacadeModule::Stop() {
  delete service_;
  ::bluetooth::grpc::GrpcFacadeModule::Stop();
}

::grpc::Service* IsoModuleFacadeModule::GetService() const {
  return service_;
}

const ModuleFactory IsoModuleFacadeModule::Factory =
    ::bluetooth::ModuleFactory([]() { return new IsoModuleFacadeModule(); });

}  // namespace iso
}  // namespace bluetooth