1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
19 #include "pw_assert/light.h"
20 #include "pw_hdlc/encoder.h"
21 #include "pw_rpc/channel.h"
22 #include "pw_stream/stream.h"
26 // Custom HDLC ChannelOutput class to write and read data through serial using
29 // WARNING: This ChannelOutput is not thread-safe. If thread-safety is required,
30 // wrap this in a pw::rpc::SynchronizedChannelOutput.
31 class RpcChannelOutput : public rpc::ChannelOutput {
33 // The RpcChannelOutput class does not own the buffer it uses to store the
34 // protobuf bytes. This buffer is specified at the time of creation along with
35 // a writer object to which will be used to write and send the bytes.
36 constexpr RpcChannelOutput(stream::Writer& writer,
37 std::span<std::byte> buffer,
39 const char* channel_name)
40 : ChannelOutput(channel_name),
45 std::span<std::byte> AcquireBuffer() override { return buffer_; }
47 Status SendAndReleaseBuffer(std::span<const std::byte> buffer) override {
48 PW_DASSERT(buffer.data() == buffer_.data());
52 return hdlc::WriteUIFrame(address_, buffer, writer_);
56 stream::Writer& writer_;
57 const std::span<std::byte> buffer_;
58 const uint8_t address_;
61 // RpcChannelOutput with its own buffer.
63 // WARNING: This ChannelOutput is not thread-safe. If thread-safety is required,
64 // wrap this in a pw::rpc::SynchronizedChannelOutput.
65 template <size_t buffer_size>
66 class RpcChannelOutputBuffer : public rpc::ChannelOutput {
68 constexpr RpcChannelOutputBuffer(stream::Writer& writer,
70 const char* channel_name)
71 : ChannelOutput(channel_name), writer_(writer), address_(address) {}
73 std::span<std::byte> AcquireBuffer() override { return buffer_; }
75 Status SendAndReleaseBuffer(std::span<const std::byte> buffer) override {
76 PW_DASSERT(buffer.data() == buffer_.data());
80 return hdlc::WriteUIFrame(address_, buffer, writer_);
84 stream::Writer& writer_;
85 std::array<std::byte, buffer_size> buffer_;
86 const uint8_t address_;
89 } // namespace pw::hdlc