Pigweed
 
Loading...
Searching...
No Matches
loopback_channel.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#include <cstdint>
17#include <optional>
18
19#include "pw_async2/dispatcher.h"
20#include "pw_async2/poll.h"
21#include "pw_channel/channel.h"
22#include "pw_multibuf/allocator.h"
23#include "pw_multibuf/allocator_async.h"
24#include "pw_multibuf/multibuf.h"
25
26namespace pw::channel {
27
30
31// Channel implementation which will read its own writes.
32template <DataType kType>
34
37
40
42
43template <>
44class LoopbackChannel<DataType::kDatagram>
45 : public Implement<ReliableDatagramReaderWriter> {
46 public:
48 : write_alloc_future_(write_allocator) {}
49 LoopbackChannel(const LoopbackChannel&) = delete;
50 LoopbackChannel& operator=(const LoopbackChannel&) = delete;
51
53 LoopbackChannel& operator=(LoopbackChannel&&) = default;
54
55 private:
57 async2::Context& cx) override;
58
59 async2::Poll<Status> DoPendReadyToWrite(async2::Context& cx) final;
60
61 async2::Poll<std::optional<multibuf::MultiBuf>> DoPendAllocateWriteBuffer(
62 async2::Context& cx, size_t min_bytes) final {
63 write_alloc_future_.SetDesiredSize(min_bytes);
64 return write_alloc_future_.Pend(cx);
65 }
66
67 Status DoStageWrite(multibuf::MultiBuf&& data) final;
68
69 async2::Poll<Status> DoPendWrite(async2::Context&) final;
70
71 async2::Poll<Status> DoPendClose(async2::Context&) final;
72
73 multibuf::MultiBufAllocationFuture write_alloc_future_;
74 std::optional<multibuf::MultiBuf> queue_;
75
76 async2::Waker waker_;
77};
78
79template <>
80class LoopbackChannel<DataType::kByte>
81 : public Implement<ReliableByteReaderWriter> {
82 public:
84 : write_alloc_future_(write_allocator) {}
85 LoopbackChannel(const LoopbackChannel&) = delete;
86 LoopbackChannel& operator=(const LoopbackChannel&) = delete;
87
89 LoopbackChannel& operator=(LoopbackChannel&&) = default;
90
91 private:
93 async2::Context& cx) override;
94
95 async2::Poll<Status> DoPendReadyToWrite(async2::Context&) final {
96 return async2::Ready(OkStatus());
97 }
98
99 async2::Poll<std::optional<multibuf::MultiBuf>> DoPendAllocateWriteBuffer(
100 async2::Context& cx, size_t min_bytes) final {
101 write_alloc_future_.SetDesiredSize(min_bytes);
102 return write_alloc_future_.Pend(cx);
103 }
104
105 Status DoStageWrite(multibuf::MultiBuf&& data) final;
106
107 async2::Poll<Status> DoPendWrite(async2::Context&) final;
108
109 async2::Poll<Status> DoPendClose(async2::Context&) final;
110
111 multibuf::MultiBufAllocationFuture write_alloc_future_;
112 multibuf::MultiBuf queue_;
113
114 async2::Waker read_waker_;
115};
116
117} // namespace pw::channel
Definition: status.h:85
Definition: dispatcher_base.h:52
Definition: poll.h:54
Definition: dispatcher_base.h:318
Definition: channel.h:583
Definition: loopback_channel.h:81
Definition: loopback_channel.h:33
Definition: allocator_async.h:90
Definition: allocator.h:54
Definition: multibuf.h:245
constexpr Status OkStatus()
Definition: status.h:234