C/C++ API Reference
Loading...
Searching...
No Matches
router.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16// __ ___ ___ _ _ ___ _ _ ___
17// \ \ / /_\ | _ \ \| |_ _| \| |/ __|
18// \ \/\/ / _ \| / .` || || .` | (_ |
19// \_/\_/_/ \_\_|_\_|\_|___|_|\_|\___|
20// _____ _____ ___ ___ ___ __ __ ___ _ _ _____ _ _
21// | __\ \/ / _ \ __| _ \_ _| \/ | __| \| |_ _/_\ | |
22// | _| > <| _/ _|| /| || |\/| | _|| .` | | |/ _ \| |__
23// |___/_/\_\_| |___|_|_\___|_| |_|___|_|\_| |_/_/ \_\____|
24//
25// This module is in an early, experimental state. The APIs are in flux and may
26// change without notice. Please do not rely on it in production code, but feel
27// free to explore and share feedback with the Pigweed team!
28
29#include "pw_async2/dispatcher.h"
30#include "pw_async2/poll.h"
31#include "pw_channel/channel.h"
32#include "pw_containers/vector.h"
33#include "pw_hdlc/decoder.h"
34#include "pw_multibuf/allocator.h"
35#include "pw_multibuf/multibuf.h"
36#include "pw_status/status.h"
37
38namespace pw::hdlc {
39
41
44class Router final {
45 public:
46 class Registration;
47
58 Router(pw::channel::ByteReaderWriter& io_channel, ByteSpan decode_buffer)
59 : io_channel_(io_channel), decoder_(decode_buffer) {}
60
61 // Router is not copyable or movable.
62 Router(const Router&) = delete;
63 Router& operator=(const Router&) = delete;
64 Router(Router&&) = delete;
65 Router& operator=(Router&&) = delete;
66
94 uint64_t receive_address,
95 uint64_t send_address);
96
105 uint64_t receive_address,
106 uint64_t send_address);
107
115
118
119 private:
120 // TODO: https://pwbug.dev/329902904 - Use allocator-based collections and
121 // remove this arbitrary limit.
122 constexpr static size_t kSomeNumberOfChannels = 16;
123
125 struct ChannelData {
126 ChannelData(pw::channel::DatagramReaderWriter& channel_arg,
127 uint64_t receive_address_arg,
128 uint64_t send_address_arg)
129 : channel(&channel_arg),
130 receive_address(receive_address_arg),
131 send_address(send_address_arg) {}
132
133 ChannelData(const ChannelData&) = delete;
134 ChannelData& operator=(const ChannelData&) = delete;
135 ChannelData(ChannelData&&) = default;
136 ChannelData& operator=(ChannelData&&) = default;
137
140
143
146 uint64_t send_address;
147 };
148
151 ChannelData* FindChannelForReceiveAddress(uint64_t receive_address);
152
155 void DecodeAndWriteIncoming(pw::async2::Context& cx);
156
159 pw::async2::Poll<> PollDeliverIncomingFrame(pw::async2::Context& cx,
160 const Frame& frame);
161
163 void TryFillBufferToEncodeAndSend(pw::async2::Context& cx);
164
167 void WriteOutgoingMessages(pw::async2::Context& cx);
168
173 void RemoveClosedChannels();
174
178
182
185
189
191 pw::multibuf::MultiBuf incoming_data_;
192
194 pw::hdlc::Decoder decoder_;
195
197 std::optional<pw::hdlc::Frame> decoded_frame_;
198
202
203 struct OutgoingBuffer {
205 size_t hdlc_encoded_size;
206 uint64_t target_address;
207 };
210 std::optional<OutgoingBuffer> buffer_to_encode_and_send_;
211
215 size_t next_first_read_index_ = 0;
216};
217
218} // namespace pw::hdlc
Definition: status.h:120
Definition: vector.h:65
Definition: context.h:55
Definition: poll.h:60
Definition: channel.h:55
Definition: decoder.h:66
Definition: decoder.h:36
Definition: router.h:44
Definition: multibuf_v1.h:248
pw::async2::Poll PendClose(pw::async2::Context &cx)
Closes all underlying channels, attempting to flush any data.
pw::channel::DatagramReaderWriter * channel
A channel which reads and writes datagrams.
Definition: router.h:139
Status AddChannel(pw::channel::DatagramReaderWriter &channel, uint64_t receive_address, uint64_t send_address)
Router(pw::channel::ByteReaderWriter &io_channel, ByteSpan decode_buffer)
Definition: router.h:58
uint64_t receive_address
Data received over HDLC with this address will be sent to channel.
Definition: router.h:142
Status RemoveChannel(pw::channel::DatagramReaderWriter &channel, uint64_t receive_address, uint64_t send_address)
uint64_t send_address
Definition: router.h:146
pw::async2::Poll Pend(pw::async2::Context &cx)