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
15 #include "pw_channel/loopback_channel.h"
16
17 #include "pw_multibuf/multibuf.h"
18
19 namespace pw::channel {
20
21 using ::pw::async2::Context;
22 using ::pw::async2::Pending;
23 using ::pw::async2::Poll;
24 using ::pw::async2::Ready;
25 using ::pw::multibuf::MultiBuf;
26
DoPendRead(Context & cx)27 Poll<Result<MultiBuf>> LoopbackChannel<DataType::kDatagram>::DoPendRead(
28 Context& cx) {
29 if (!queue_.has_value()) {
30 PW_ASYNC_STORE_WAKER(
31 cx, waker_, "LoopbackChannel is waiting for incoming data");
32 return Pending();
33 }
34 MultiBuf data = std::move(*queue_);
35 queue_ = std::nullopt;
36 std::move(waker_).Wake();
37 return data;
38 }
39
DoPendReadyToWrite(Context & cx)40 Poll<Status> LoopbackChannel<DataType::kDatagram>::DoPendReadyToWrite(
41 Context& cx) {
42 if (queue_.has_value()) {
43 PW_ASYNC_STORE_WAKER(
44 cx,
45 waker_,
46 "LoopbackChannel is waiting for the incoming data to be consumed");
47 return Pending();
48 }
49 return Ready(OkStatus());
50 }
51
DoStageWrite(MultiBuf && data)52 Status LoopbackChannel<DataType::kDatagram>::DoStageWrite(MultiBuf&& data) {
53 PW_DASSERT(!queue_.has_value());
54 queue_ = std::move(data);
55 std::move(waker_).Wake();
56 return OkStatus();
57 }
58
DoPendWrite(async2::Context &)59 async2::Poll<Status> LoopbackChannel<DataType::kDatagram>::DoPendWrite(
60 async2::Context&) {
61 return OkStatus();
62 }
63
DoPendClose(async2::Context &)64 async2::Poll<Status> LoopbackChannel<DataType::kDatagram>::DoPendClose(
65 async2::Context&) {
66 queue_.reset();
67 return OkStatus();
68 }
69
DoPendRead(Context & cx)70 Poll<Result<MultiBuf>> LoopbackChannel<DataType::kByte>::DoPendRead(
71 Context& cx) {
72 if (queue_.empty()) {
73 PW_ASYNC_STORE_WAKER(
74 cx, read_waker_, "LoopbackChannel is waiting for incoming data");
75 return Pending();
76 }
77 return std::move(queue_);
78 }
79
DoStageWrite(MultiBuf && data)80 Status LoopbackChannel<DataType::kByte>::DoStageWrite(MultiBuf&& data) {
81 if (!data.empty()) {
82 bool was_empty = queue_.empty();
83 queue_.PushSuffix(std::move(data));
84 if (was_empty) {
85 std::move(read_waker_).Wake();
86 }
87 }
88 return OkStatus();
89 }
90
DoPendWrite(async2::Context &)91 async2::Poll<Status> LoopbackChannel<DataType::kByte>::DoPendWrite(
92 async2::Context&) {
93 return OkStatus();
94 }
95
DoPendClose(async2::Context &)96 async2::Poll<Status> LoopbackChannel<DataType::kByte>::DoPendClose(
97 async2::Context&) {
98 queue_.Release();
99 return OkStatus();
100 }
101
102 } // namespace pw::channel
103