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 enabled)52   void EnableFcs(bool enabled) override {}
SetRetransmissionAndFlowControlOptions(const RetransmissionAndFlowControlConfigurationOption & option)53   void SetRetransmissionAndFlowControlOptions(const RetransmissionAndFlowControlConfigurationOption& option) override {}
54 
55   // TODO: Set MTU and MPS from signalling channel
56   void SetMtu(Mtu mtu);
57   void SetMps(uint16_t mps);
58   // TODO: Handle credits
59   void OnCredit(uint16_t credits);
60 
61  private:
62   Cid cid_;
63   Cid remote_cid_;
64   os::EnqueueBuffer<UpperEnqueue> enqueue_buffer_;
65   os::Handler* handler_;
66   std::queue<std::unique_ptr<packet::BasePacketBuilder>> pdu_queue_;
67   Scheduler* scheduler_;
68   ILink* link_;
69   Mtu mtu_ = 512;
70   uint16_t mps_ = 251;
71   uint16_t credits_ = 0;
72   uint16_t pending_frames_count_ = 0;
73 
74   class PacketViewForReassembly : public packet::PacketView<kLittleEndian> {
75    public:
PacketViewForReassembly(const PacketView & packetView)76     PacketViewForReassembly(const PacketView& packetView) : PacketView(packetView) {}
AppendPacketView(packet::PacketView<kLittleEndian> to_append)77     void AppendPacketView(packet::PacketView<kLittleEndian> to_append) {
78       Append(to_append);
79     }
80   };
81   PacketViewForReassembly reassembly_stage_{PacketView<kLittleEndian>(std::make_shared<std::vector<uint8_t>>())};
82   uint16_t remaining_sdu_continuation_packet_size_ = 0;
83 };
84 
85 }  // namespace internal
86 }  // namespace l2cap
87 }  // namespace bluetooth
88