blob: 2038fd303f6c3c99f9da4743eacfb7c190e552ee [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;
Hansong Zhang7394ae72019-11-21 10:57:04 -080052 void EnableFcs(bool enabled) override;
53 void SetRetransmissionAndFlowControlOptions(const RetransmissionAndFlowControlConfigurationOption& option) override;
Hansong Zhangb0960762019-11-14 17:57:10 -080054
55 private:
Hansong Zhang7394ae72019-11-21 10:57:04 -080056 Cid cid_;
57 Cid remote_cid_;
58 os::EnqueueBuffer<UpperEnqueue> enqueue_buffer_;
59 os::Handler* handler_;
Hansong Zhangb0960762019-11-14 17:57:10 -080060 std::queue<std::unique_ptr<BasicFrameBuilder>> pdu_queue_;
Hansong Zhang7394ae72019-11-21 10:57:04 -080061 Scheduler* scheduler_;
62 bool fcs_enabled_ = false;
Hansong Zhang65941942019-11-16 23:48:30 -080063
64 class PacketViewForReassembly : public packet::PacketView<kLittleEndian> {
65 public:
66 PacketViewForReassembly(const PacketView& packetView) : PacketView(packetView) {}
Hansong Zhang65941942019-11-16 23:48:30 -080067 void AppendPacketView(packet::PacketView<kLittleEndian> to_append) {
68 Append(to_append);
69 }
70 };
71
72 class CopyablePacketBuilder : public packet::BasePacketBuilder {
73 public:
Hansong Zhanga6312532019-11-19 14:01:36 -080074 CopyablePacketBuilder(std::shared_ptr<packet::RawBuilder> builder) : builder_(std::move(builder)) {}
Hansong Zhang65941942019-11-16 23:48:30 -080075
76 void Serialize(BitInserter& it) const override;
77
78 size_t size() const override;
79
Hansong Zhang65941942019-11-16 23:48:30 -080080 private:
Hansong Zhanga6312532019-11-19 14:01:36 -080081 std::shared_ptr<packet::RawBuilder> builder_;
Hansong Zhang65941942019-11-16 23:48:30 -080082 };
83
Hansong Zhanga6312532019-11-19 14:01:36 -080084 PacketViewForReassembly reassembly_stage_{std::make_shared<std::vector<uint8_t>>()};
Hansong Zhang65941942019-11-16 23:48:30 -080085 SegmentationAndReassembly sar_state_ = SegmentationAndReassembly::END;
86
87 void stage_for_reassembly(SegmentationAndReassembly sar, const packet::PacketView<kLittleEndian>& payload);
88 void send_pdu(std::unique_ptr<BasicFrameBuilder> pdu);
89
90 void close_channel();
91
92 // Configuration options
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