Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 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" |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame^] | 34 | #include "packet/raw_builder.h" |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 35 | |
| 36 | namespace bluetooth { |
| 37 | namespace l2cap { |
| 38 | namespace internal { |
| 39 | |
| 40 | class 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 Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 47 | ~ErtmController(); |
| 48 | // Segmentation is handled here |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 49 | 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 Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 55 | [[maybe_unused]] Cid remote_cid_; |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 56 | [[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 Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 60 | // 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 Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 66 | void AppendPacketView(packet::PacketView<kLittleEndian> to_append) { |
| 67 | Append(to_append); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | class CopyablePacketBuilder : public packet::BasePacketBuilder { |
| 72 | public: |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame^] | 73 | CopyablePacketBuilder(std::shared_ptr<packet::RawBuilder> builder) : builder_(std::move(builder)) {} |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 74 | |
| 75 | void Serialize(BitInserter& it) const override; |
| 76 | |
| 77 | size_t size() const override; |
| 78 | |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 79 | private: |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame^] | 80 | std::shared_ptr<packet::RawBuilder> builder_; |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame^] | 83 | PacketViewForReassembly reassembly_stage_{std::make_shared<std::vector<uint8_t>>()}; |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 84 | 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 Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame^] | 93 | 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 Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 97 | |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame^] | 98 | uint16_t remote_tx_window_ = 10; |
| 99 | uint16_t remote_mps_ = 1010; |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 100 | |
| 101 | struct impl; |
| 102 | std::unique_ptr<impl> pimpl_; |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace internal |
| 106 | } // namespace l2cap |
| 107 | } // namespace bluetooth |