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/ilink.h"
28 #include "l2cap/internal/scheduler.h"
29 #include "l2cap/l2cap_packets.h"
30 #include "l2cap/mtu.h"
31 #include "os/handler.h"
32 #include "os/queue.h"
33 #include "packet/base_packet_builder.h"
34 #include "packet/packet_view.h"
35 
36 namespace bluetooth {
37 namespace l2cap {
38 namespace internal {
39 
40 class LeCreditBasedDataController : public DataController {
41  public:
42   using UpperEnqueue = packet::PacketView<packet::kLittleEndian>;
43   using UpperDequeue = packet::BasePacketBuilder;
44   using UpperQueueDownEnd = common::BidiQueueEnd<UpperEnqueue, UpperDequeue>;
45   LeCreditBasedDataController(ILink* link, Cid cid, Cid remote_cid, UpperQueueDownEnd* channel_queue_end,
46                               os::Handler* handler, Scheduler* scheduler);
47 
48   void OnSdu(std::unique_ptr<packet::BasePacketBuilder> sdu) override;
49   void OnPdu(packet::PacketView<true> pdu) override;
50   std::unique_ptr<packet::BasePacketBuilder> GetNextPacket() override;
51 
EnableFcs(bool)52   void EnableFcs(bool /* enabled */) override {}
SetRetransmissionAndFlowControlOptions(const RetransmissionAndFlowControlConfigurationOption &)53   void SetRetransmissionAndFlowControlOptions(
54       const RetransmissionAndFlowControlConfigurationOption& /* option */) override {}
55 
56   // TODO: Set MTU and MPS from signalling channel
57   void SetMtu(Mtu mtu);
58   void SetMps(uint16_t mps);
59   // TODO: Handle credits
60   void OnCredit(uint16_t credits);
61 
62  private:
63   Cid cid_;
64   Cid remote_cid_;
65   os::EnqueueBuffer<UpperEnqueue> enqueue_buffer_;
66   os::Handler* handler_;
67   std::queue<std::unique_ptr<packet::BasePacketBuilder>> pdu_queue_;
68   Scheduler* scheduler_;
69   ILink* link_;
70   Mtu mtu_ = 512;
71   uint16_t mps_ = 251;
72   uint16_t credits_ = 0;
73   uint16_t pending_frames_count_ = 0;
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   PacketViewForReassembly reassembly_stage_{PacketView<kLittleEndian>(std::make_shared<std::vector<uint8_t>>())};
83   uint16_t remaining_sdu_continuation_packet_size_ = 0;
84 };
85 
86 }  // namespace internal
87 }  // namespace l2cap
88 }  // namespace bluetooth
89