1 /*
2  * Copyright 2022 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 #include "model/controller/ffi.h"
18 
19 #include <android-base/logging.h>
20 
21 #include <iostream>
22 
23 #include "model/controller/dual_mode_controller.h"
24 
25 using namespace rootcanal;
26 using bluetooth::hci::Address;
27 
28 namespace hci {
29 
30 enum Idc {
31   CMD = 1,
32   ACL,
33   SCO,
34   EVT,
35   ISO,
36 };
37 
ConfigureLogging()38 __attribute__((constructor)) static void ConfigureLogging() {
39   android::base::InitLogging({}, android::base::StdioLogger);
40 }
41 
42 }  // namespace hci
43 
44 extern "C" {
45 
ffi_controller_new(uint8_t const address[6],void (* send_hci)(int idc,uint8_t const * data,size_t data_len),void (* send_ll)(uint8_t const * data,size_t data_len,int phy,int tx_power))46 __attribute__((visibility("default"))) void* ffi_controller_new(
47     uint8_t const address[6],
48     void (*send_hci)(int idc, uint8_t const* data, size_t data_len),
49     void (*send_ll)(uint8_t const* data, size_t data_len, int phy,
50                     int tx_power)) {
51   DualModeController* controller = new DualModeController();
52   controller->SetAddress(Address({address[0], address[1], address[2],
53                                   address[3], address[4], address[5]}));
54   controller->RegisterEventChannel(
55       [=](std::shared_ptr<std::vector<uint8_t>> data) {
56         send_hci(hci::Idc::EVT, data->data(), data->size());
57       });
58   controller->RegisterAclChannel(
59       [=](std::shared_ptr<std::vector<uint8_t>> data) {
60         send_hci(hci::Idc::ACL, data->data(), data->size());
61       });
62   controller->RegisterScoChannel(
63       [=](std::shared_ptr<std::vector<uint8_t>> data) {
64         send_hci(hci::Idc::SCO, data->data(), data->size());
65       });
66   controller->RegisterIsoChannel(
67       [=](std::shared_ptr<std::vector<uint8_t>> data) {
68         send_hci(hci::Idc::ISO, data->data(), data->size());
69       });
70   controller->RegisterLinkLayerChannel(
71       [=](std::vector<uint8_t> const& data, Phy::Type phy, int8_t tx_power) {
72         send_ll(data.data(), data.size(), static_cast<int>(phy), tx_power);
73       });
74 
75   return controller;
76 }
77 
ffi_controller_delete(void * controller_)78 __attribute__((visibility("default"))) void ffi_controller_delete(
79     void* controller_) {
80   DualModeController* controller =
81       reinterpret_cast<DualModeController*>(controller_);
82   delete controller;
83 }
84 
ffi_controller_receive_hci(void * controller_,int idc,uint8_t const * data,size_t data_len)85 __attribute__((visibility("default"))) void ffi_controller_receive_hci(
86     void* controller_, int idc, uint8_t const* data, size_t data_len) {
87   DualModeController* controller =
88       reinterpret_cast<DualModeController*>(controller_);
89   std::shared_ptr<std::vector<uint8_t>> bytes =
90       std::make_shared<std::vector<uint8_t>>(data, data + data_len);
91 
92   switch (idc) {
93     case hci::Idc::CMD:
94       controller->HandleCommand(bytes);
95       break;
96     case hci::Idc::ACL:
97       controller->HandleAcl(bytes);
98       break;
99     case hci::Idc::SCO:
100       controller->HandleSco(bytes);
101       break;
102     case hci::Idc::ISO:
103       controller->HandleIso(bytes);
104       break;
105     default:
106       std::cerr << "Dropping HCI packet with unknown type " << (int)idc
107                 << std::endl;
108       break;
109   }
110 }
111 
ffi_controller_receive_ll(void * controller_,uint8_t const * data,size_t data_len,int phy,int rssi)112 __attribute__((visibility("default"))) void ffi_controller_receive_ll(
113     void* controller_, uint8_t const* data, size_t data_len, int phy,
114     int rssi) {
115   DualModeController* controller =
116       reinterpret_cast<DualModeController*>(controller_);
117   std::shared_ptr<std::vector<uint8_t>> bytes =
118       std::make_shared<std::vector<uint8_t>>(data, data + data_len);
119   model::packets::LinkLayerPacketView packet =
120       model::packets::LinkLayerPacketView::Create(pdl::packet::slice(bytes));
121   if (!packet.IsValid()) {
122     std::cerr << "Dropping malformed LL packet" << std::endl;
123     return;
124   }
125   controller->ReceiveLinkLayerPacket(packet, Phy::Type(phy), rssi);
126 }
127 
ffi_controller_tick(void * controller_)128 __attribute__((visibility("default"))) void ffi_controller_tick(
129     void* controller_) {
130   DualModeController* controller =
131       reinterpret_cast<DualModeController*>(controller_);
132   controller->Tick();
133 }
134 
ffi_generate_rpa(uint8_t const irk_[16],uint8_t rpa[6])135 __attribute__((visibility("default"))) void ffi_generate_rpa(
136     uint8_t const irk_[16], uint8_t rpa[6]) {
137   std::array<uint8_t, LinkLayerController::kIrkSize> irk;
138   memcpy(irk.data(), irk_, LinkLayerController::kIrkSize);
139   Address address = LinkLayerController::generate_rpa(irk);
140   memcpy(rpa, address.data(), Address::kLength);
141 }
142 
143 };  // extern "C"
144