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; |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 50 | void OnPdu(packet::PacketView<true> pdu) override; |
| 51 | std::unique_ptr<packet::BasePacketBuilder> GetNextPacket() override; |
Hansong Zhang | 7394ae7 | 2019-11-21 10:57:04 -0800 | [diff] [blame] | 52 | void EnableFcs(bool enabled) override; |
| 53 | void SetRetransmissionAndFlowControlOptions(const RetransmissionAndFlowControlConfigurationOption& option) override; |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 54 | |
| 55 | private: |
Hansong Zhang | 7394ae7 | 2019-11-21 10:57:04 -0800 | [diff] [blame] | 56 | Cid cid_; |
| 57 | Cid remote_cid_; |
| 58 | os::EnqueueBuffer<UpperEnqueue> enqueue_buffer_; |
| 59 | os::Handler* handler_; |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 60 | std::queue<std::unique_ptr<packet::BasePacketBuilder>> pdu_queue_; |
Hansong Zhang | 7394ae7 | 2019-11-21 10:57:04 -0800 | [diff] [blame] | 61 | Scheduler* scheduler_; |
| 62 | bool fcs_enabled_ = false; |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 63 | |
| 64 | class PacketViewForReassembly : public packet::PacketView<kLittleEndian> { |
| 65 | public: |
| 66 | PacketViewForReassembly(const PacketView& packetView) : PacketView(packetView) {} |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 67 | void AppendPacketView(packet::PacketView<kLittleEndian> to_append) { |
| 68 | Append(to_append); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | class CopyablePacketBuilder : public packet::BasePacketBuilder { |
| 73 | public: |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 74 | CopyablePacketBuilder(std::shared_ptr<packet::RawBuilder> builder) : builder_(std::move(builder)) {} |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 75 | |
| 76 | void Serialize(BitInserter& it) const override; |
| 77 | |
| 78 | size_t size() const override; |
| 79 | |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 80 | private: |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 81 | std::shared_ptr<packet::RawBuilder> builder_; |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 84 | PacketViewForReassembly reassembly_stage_{std::make_shared<std::vector<uint8_t>>()}; |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 85 | SegmentationAndReassembly sar_state_ = SegmentationAndReassembly::END; |
| 86 | |
| 87 | void stage_for_reassembly(SegmentationAndReassembly sar, const packet::PacketView<kLittleEndian>& payload); |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 88 | void send_pdu(std::unique_ptr<packet::BasePacketBuilder> pdu); |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 89 | |
| 90 | void close_channel(); |
| 91 | |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 92 | void on_pdu_no_fcs(const packet::PacketView<true>& pdu); |
| 93 | void on_pdu_fcs(const packet::PacketView<true>& pdu); |
| 94 | |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 95 | // Configuration options |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 96 | uint16_t local_tx_window_ = 10; |
| 97 | uint16_t local_max_transmit_ = 20; |
| 98 | uint16_t local_retransmit_timeout_ms_ = 2000; |
| 99 | uint16_t local_monitor_timeout_ms_ = 12000; |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 100 | |
Hansong Zhang | a631253 | 2019-11-19 14:01:36 -0800 | [diff] [blame] | 101 | uint16_t remote_tx_window_ = 10; |
| 102 | uint16_t remote_mps_ = 1010; |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 103 | |
Hansong Zhang | d164e1b | 2019-11-21 15:33:22 -0800 | [diff] [blame^] | 104 | uint16_t size_each_packet_ = |
| 105 | (remote_mps_ - 4 /* basic L2CAP header */ - 2 /* SDU length */ - 2 /* Extended control */ - 2 /* FCS */); |
| 106 | |
Hansong Zhang | 6594194 | 2019-11-16 23:48:30 -0800 | [diff] [blame] | 107 | struct impl; |
| 108 | std::unique_ptr<impl> pimpl_; |
Hansong Zhang | b096076 | 2019-11-14 17:57:10 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace internal |
| 112 | } // namespace l2cap |
| 113 | } // namespace bluetooth |