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 /// This class intercepts incoming connection requests and data packets, and
18 /// decides whether to intercept them or pass them to the legacy stack
19 ///
20 /// It allows us to easily gate changes to the datapath and roll back to legacy
21 /// behavior if needed.
22 
23 #pragma once
24 
25 #include "rust/cxx.h"
26 #include "stack/include/bt_hdr.h"
27 #include "types/raw_address.h"
28 
29 namespace bluetooth {
30 namespace shim {
31 namespace arbiter {
32 
33 enum class InterceptAction {
34   /// The packet should be forwarded to the legacy stack
35   FORWARD,
36   /// The packet should be dropped and not sent to legacy
37   DROP
38 };
39 
40 class AclArbiter {
41  public:
42   void OnLeConnect(uint8_t tcb_idx, uint16_t advertiser_id);
43   void OnLeDisconnect(uint8_t tcb_idx);
44   InterceptAction InterceptAttPacket(uint8_t tcb_idx, const BT_HDR* packet);
45 
46   void OnOutgoingMtuReq(uint8_t tcb_idx);
47   void OnIncomingMtuResp(uint8_t tcb_idx, size_t mtu);
48   void OnIncomingMtuReq(uint8_t tcb_idx, size_t mtu);
49 
50   void SendPacketToPeer(uint8_t tcb_idx, ::rust::Vec<uint8_t> buffer);
51 
52   AclArbiter() = default;
53   AclArbiter(AclArbiter&& other) = default;
54   AclArbiter& operator=(AclArbiter&& other) = default;
55   ~AclArbiter() = default;
56 };
57 
58 void StoreCallbacksFromRust(
59     ::rust::Fn<void(uint8_t tcb_idx, uint8_t advertiser)> on_le_connect,
60     ::rust::Fn<void(uint8_t tcb_idx)> on_le_disconnect,
61     ::rust::Fn<InterceptAction(uint8_t tcb_idx, ::rust::Vec<uint8_t> buffer)>
62         intercept_packet,
63     ::rust::Fn<void(uint8_t tcb_idx)> on_outgoing_mtu_req,
64     ::rust::Fn<void(uint8_t tcb_idx, size_t mtu)> on_incoming_mtu_resp,
65     ::rust::Fn<void(uint8_t tcb_idx, size_t mtu)> on_incoming_mtu_req);
66 
67 void SendPacketToPeer(uint8_t tcb_idx, ::rust::Vec<uint8_t> buffer);
68 
69 AclArbiter& GetArbiter();
70 
71 }  // namespace arbiter
72 }  // namespace shim
73 }  // namespace bluetooth
74