summaryrefslogtreecommitdiff
path: root/system/stack/arbiter/acl_arbiter.cc
blob: 5f37965e98839661799d725d2cb2eccee19a83e9 (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
/*
 * Copyright 2022 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 "stack/arbiter/acl_arbiter.h"

#include <base/functional/bind.h>
#include <bluetooth/log.h>

#include <iterator>

#include "osi/include/allocator.h"
#include "stack/gatt/gatt_int.h"
#include "stack/include/l2cap_interface.h"
#include "stack/include/l2cdefs.h"
#include "stack/include/main_thread.h"

namespace bluetooth {
namespace shim {
namespace arbiter {

namespace {
struct RustArbiterCallbacks {
  ::rust::Fn<void(uint8_t tcb_idx, uint8_t advertiser)> on_le_connect;
  ::rust::Fn<void(uint8_t tcb_idx)> on_le_disconnect;
  ::rust::Fn<InterceptAction(uint8_t tcb_idx, ::rust::Vec<uint8_t> buffer)> intercept_packet;
  ::rust::Fn<void(uint8_t tcb_idx)> on_outgoing_mtu_req;
  ::rust::Fn<void(uint8_t tcb_idx, size_t mtu)> on_incoming_mtu_resp;
  ::rust::Fn<void(uint8_t tcb_idx, size_t mtu)> on_incoming_mtu_req;
};

RustArbiterCallbacks callbacks_{};
}  // namespace

void AclArbiter::OnLeConnect(uint8_t tcb_idx, uint16_t advertiser_id) {
#ifdef TARGET_FLOSS
  return;
#endif
  log::info("Notifying Rust of LE connection");
  callbacks_.on_le_connect(tcb_idx, advertiser_id);
}

void AclArbiter::OnLeDisconnect(uint8_t tcb_idx) {
#ifdef TARGET_FLOSS
  return;
#endif
  log::info("Notifying Rust of LE disconnection");
  callbacks_.on_le_disconnect(tcb_idx);
}

InterceptAction AclArbiter::InterceptAttPacket(uint8_t tcb_idx, const BT_HDR* packet) {
#ifdef TARGET_FLOSS
  return InterceptAction::FORWARD;
#endif
  log::debug("Intercepting ATT packet and forwarding to Rust");

  uint8_t* packet_start = (uint8_t*)(packet + 1) + packet->offset;
  uint8_t* packet_end = packet_start + packet->len;

  auto vec = ::rust::Vec<uint8_t>();
  std::copy(packet_start, packet_end, std::back_inserter(vec));
  return callbacks_.intercept_packet(tcb_idx, std::move(vec));
}

void AclArbiter::OnOutgoingMtuReq(uint8_t tcb_idx) {
#ifdef TARGET_FLOSS
  return;
#endif
  log::debug("Notifying Rust of outgoing MTU request");
  callbacks_.on_outgoing_mtu_req(tcb_idx);
}

void AclArbiter::OnIncomingMtuResp(uint8_t tcb_idx, size_t mtu) {
#ifdef TARGET_FLOSS
  return;
#endif
  log::debug("Notifying Rust of incoming MTU response {}", mtu);
  callbacks_.on_incoming_mtu_resp(tcb_idx, mtu);
}

void AclArbiter::OnIncomingMtuReq(uint8_t tcb_idx, size_t mtu) {
#ifdef TARGET_FLOSS
  return;
#endif
  log::debug("Notifying Rust of incoming MTU request {}", mtu);
  callbacks_.on_incoming_mtu_req(tcb_idx, mtu);
}

void AclArbiter::SendPacketToPeer(uint8_t tcb_idx, ::rust::Vec<uint8_t> buffer) {
#ifdef TARGET_FLOSS
  return;
#endif
  tGATT_TCB* p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  if (p_tcb != nullptr) {
    BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR) + buffer.size() + L2CAP_MIN_OFFSET);
    if (p_buf == nullptr) {
      log::fatal("OOM when sending packet");
    }
    auto p = (uint8_t*)(p_buf + 1) + L2CAP_MIN_OFFSET;
    std::copy(buffer.begin(), buffer.end(), p);
    p_buf->offset = L2CAP_MIN_OFFSET;
    p_buf->len = buffer.size();
    if (stack::l2cap::get_interface().L2CA_SendFixedChnlData(L2CAP_ATT_CID, p_tcb->peer_bda,
                                                             p_buf) != tL2CAP_DW_RESULT::SUCCESS) {
      log::warn("Unable to send L2CAP data peer:{} fixed_cid:{} len:{}", p_tcb->peer_bda,
                L2CAP_ATT_CID, buffer.size());
    }
  } else {
    log::error("Dropping packet since connection no longer exists");
  }
}

void StoreCallbacksFromRust(
        ::rust::Fn<void(uint8_t tcb_idx, uint8_t advertiser)> on_le_connect,
        ::rust::Fn<void(uint8_t tcb_idx)> on_le_disconnect,
        ::rust::Fn<InterceptAction(uint8_t tcb_idx, ::rust::Vec<uint8_t> buffer)> intercept_packet,
        ::rust::Fn<void(uint8_t tcb_idx)> on_outgoing_mtu_req,
        ::rust::Fn<void(uint8_t tcb_idx, size_t mtu)> on_incoming_mtu_resp,
        ::rust::Fn<void(uint8_t tcb_idx, size_t mtu)> on_incoming_mtu_req) {
  log::info("Received callbacks from Rust, registering in Arbiter");
  callbacks_ = {on_le_connect,       on_le_disconnect,     intercept_packet,
                on_outgoing_mtu_req, on_incoming_mtu_resp, on_incoming_mtu_req};
}

void SendPacketToPeer(uint8_t tcb_idx, ::rust::Vec<uint8_t> buffer) {
  do_in_main_thread(base::BindOnce(&AclArbiter::SendPacketToPeer, base::Unretained(&GetArbiter()),
                                   tcb_idx, std::move(buffer)));
}

AclArbiter& GetArbiter() {
  static auto singleton = AclArbiter();
  return singleton;
}

}  // namespace arbiter
}  // namespace shim
}  // namespace bluetooth