1 /*
2  * Copyright 2019 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 <memory>
20 #include <unordered_map>
21 #include <utility>
22 
23 #include "common/bidi_queue.h"
24 #include "l2cap/cid.h"
25 #include "l2cap/internal/channel_impl.h"
26 #include "l2cap/internal/data_controller.h"
27 #include "l2cap/internal/scheduler.h"
28 #include "l2cap/l2cap_packets.h"
29 #include "l2cap/mtu.h"
30 #include "os/handler.h"
31 #include "os/queue.h"
32 #include "packet/base_packet_builder.h"
33 #include "packet/packet_view.h"
34 #include "packet/raw_builder.h"
35 
36 namespace bluetooth {
37 namespace l2cap {
38 namespace internal {
39 class ILink;
40 
41 class ErtmController : public DataController {
42  public:
43   using UpperEnqueue = packet::PacketView<packet::kLittleEndian>;
44   using UpperDequeue = packet::BasePacketBuilder;
45   using UpperQueueDownEnd = common::BidiQueueEnd<UpperEnqueue, UpperDequeue>;
46   ErtmController(ILink* link, Cid cid, Cid remote_cid, UpperQueueDownEnd* channel_queue_end, os::Handler* handler,
47                  Scheduler* scheduler);
48   ~ErtmController();
49   // Segmentation is handled here
50   void OnSdu(std::unique_ptr<packet::BasePacketBuilder> sdu) override;
51   void OnPdu(packet::PacketView<true> pdu) override;
52   std::unique_ptr<packet::BasePacketBuilder> GetNextPacket() override;
53   void EnableFcs(bool enabled) override;
54   void SetRetransmissionAndFlowControlOptions(const RetransmissionAndFlowControlConfigurationOption& option) override;
55 
56  private:
57   ILink* link_;
58   Cid cid_;
59   Cid remote_cid_;
60   os::EnqueueBuffer<UpperEnqueue> enqueue_buffer_;
61   os::Handler* handler_;
62   std::queue<std::unique_ptr<packet::BasePacketBuilder>> pdu_queue_;
63   Scheduler* scheduler_;
64 
65   // Configuration options
66   bool fcs_enabled_ = false;
67   uint16_t local_tx_window_ = 10;
68   uint16_t local_max_transmit_ = 20;
69   uint16_t local_retransmit_timeout_ms_ = 2000;
70   uint16_t local_monitor_timeout_ms_ = 12000;
71 
72   uint16_t remote_tx_window_ = 10;
73   uint16_t remote_mps_ = 1010;
74 
75   class PacketViewForReassembly : public packet::PacketView<kLittleEndian> {
76    public:
PacketViewForReassembly(const PacketView & packetView)77     PacketViewForReassembly(const PacketView& packetView) : PacketView(packetView) {}
AppendPacketView(packet::PacketView<kLittleEndian> to_append)78     void AppendPacketView(packet::PacketView<kLittleEndian> to_append) {
79       Append(to_append);
80     }
81   };
82 
83   class CopyablePacketBuilder : public packet::BasePacketBuilder {
84    public:
CopyablePacketBuilder(std::shared_ptr<packet::RawBuilder> builder)85     CopyablePacketBuilder(std::shared_ptr<packet::RawBuilder> builder) : builder_(std::move(builder)) {}
86 
87     void Serialize(BitInserter& it) const override;
88 
89     size_t size() const override;
90 
91    private:
92     std::shared_ptr<packet::RawBuilder> builder_;
93   };
94 
95   PacketViewForReassembly reassembly_stage_{PacketView<kLittleEndian>(std::make_shared<std::vector<uint8_t>>())};
96   SegmentationAndReassembly sar_state_ = SegmentationAndReassembly::END;
97   uint16_t remaining_sdu_continuation_packet_size_ = 0;
98 
99   void stage_for_reassembly(SegmentationAndReassembly sar, uint16_t sdu_size,
100                             const packet::PacketView<kLittleEndian>& payload);
101   void send_pdu(std::unique_ptr<packet::BasePacketBuilder> pdu);
102 
103   void close_channel();
104 
105   void on_pdu_no_fcs(const packet::PacketView<true>& pdu);
106   void on_pdu_fcs(const packet::PacketView<true>& pdu);
107 
108   struct impl;
109   std::unique_ptr<impl> pimpl_;
110 };
111 
112 }  // namespace internal
113 }  // namespace l2cap
114 }  // namespace bluetooth
115