blob: 0566e0bcc39ece9bd35e28b6992fc3937c320a66 [file] [log] [blame]
Hansong Zhangb0960762019-11-14 17:57:10 -08001/*
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"
Hansong Zhanga6312532019-11-19 14:01:36 -080034#include "packet/raw_builder.h"
Hansong Zhangb0960762019-11-14 17:57:10 -080035
36namespace bluetooth {
37namespace l2cap {
38namespace internal {
39
40class ErtmController : public DataController {
41 public:
42 using UpperEnqueue = packet::PacketView<packet::kLittleEndian>;
43 using UpperDequeue = packet::BasePacketBuilder;
44 using UpperQueueDownEnd = common::BidiQueueEnd<UpperEnqueue, UpperDequeue>;
45 ErtmController(Cid cid, Cid remote_cid, UpperQueueDownEnd* channel_queue_end, os::Handler* handler,
46 Scheduler* scheduler);
Hansong Zhang65941942019-11-16 23:48:30 -080047 ~ErtmController();
48 // Segmentation is handled here
Hansong Zhangb0960762019-11-14 17:57:10 -080049 void OnSdu(std::unique_ptr<packet::BasePacketBuilder> sdu) override;
50 void OnPdu(BasicFrameView pdu) override;
51 std::unique_ptr<BasicFrameBuilder> GetNextPacket() override;
52
53 private:
54 [[maybe_unused]] Cid cid_;
Hansong Zhang65941942019-11-16 23:48:30 -080055 [[maybe_unused]] Cid remote_cid_;
Hansong Zhangb0960762019-11-14 17:57:10 -080056 [[maybe_unused]] os::EnqueueBuffer<UpperEnqueue> enqueue_buffer_;
57 [[maybe_unused]] os::Handler* handler_;
58 std::queue<std::unique_ptr<BasicFrameBuilder>> pdu_queue_;
59 [[maybe_unused]] Scheduler* scheduler_;
Hansong Zhang65941942019-11-16 23:48:30 -080060 // TODO: Support FCS
61 [[maybe_unused]] FcsType fcs_type_ = FcsType::NO_FCS;
62
63 class PacketViewForReassembly : public packet::PacketView<kLittleEndian> {
64 public:
65 PacketViewForReassembly(const PacketView& packetView) : PacketView(packetView) {}
Hansong Zhang65941942019-11-16 23:48:30 -080066 void AppendPacketView(packet::PacketView<kLittleEndian> to_append) {
67 Append(to_append);
68 }
69 };
70
71 class CopyablePacketBuilder : public packet::BasePacketBuilder {
72 public:
Hansong Zhanga6312532019-11-19 14:01:36 -080073 CopyablePacketBuilder(std::shared_ptr<packet::RawBuilder> builder) : builder_(std::move(builder)) {}
Hansong Zhang65941942019-11-16 23:48:30 -080074
75 void Serialize(BitInserter& it) const override;
76
77 size_t size() const override;
78
Hansong Zhang65941942019-11-16 23:48:30 -080079 private:
Hansong Zhanga6312532019-11-19 14:01:36 -080080 std::shared_ptr<packet::RawBuilder> builder_;
Hansong Zhang65941942019-11-16 23:48:30 -080081 };
82
Hansong Zhanga6312532019-11-19 14:01:36 -080083 PacketViewForReassembly reassembly_stage_{std::make_shared<std::vector<uint8_t>>()};
Hansong Zhang65941942019-11-16 23:48:30 -080084 SegmentationAndReassembly sar_state_ = SegmentationAndReassembly::END;
85
86 void stage_for_reassembly(SegmentationAndReassembly sar, const packet::PacketView<kLittleEndian>& payload);
87 void send_pdu(std::unique_ptr<BasicFrameBuilder> pdu);
88
89 void close_channel();
90
91 // Configuration options
92 // TODO: Configure these number
Hansong Zhanga6312532019-11-19 14:01:36 -080093 uint16_t local_tx_window_ = 10;
94 uint16_t local_max_transmit_ = 20;
95 uint16_t local_retransmit_timeout_ms_ = 2000;
96 uint16_t local_monitor_timeout_ms_ = 12000;
Hansong Zhang65941942019-11-16 23:48:30 -080097
Hansong Zhanga6312532019-11-19 14:01:36 -080098 uint16_t remote_tx_window_ = 10;
99 uint16_t remote_mps_ = 1010;
Hansong Zhang65941942019-11-16 23:48:30 -0800100
101 struct impl;
102 std::unique_ptr<impl> pimpl_;
Hansong Zhangb0960762019-11-14 17:57:10 -0800103};
104
105} // namespace internal
106} // namespace l2cap
107} // namespace bluetooth