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_hdlc_lite/encoder.h"
20 #include "pw_rpc/channel.h"
21 #include "pw_stream/stream.h"
23 namespace pw::hdlc_lite {
25 // Custom HDLC ChannelOutput class to write and read data through serial using
26 // the HDLC-Lite protocol.
27 class RpcChannelOutput : public rpc::ChannelOutput {
29 // The RpcChannelOutput class does not own the buffer it uses to store the
30 // protobuf bytes. This buffer is specified at the time of creation along with
31 // a writer object to which will be used to write and send the bytes.
32 constexpr RpcChannelOutput(stream::Writer& writer,
33 std::span<std::byte> buffer,
35 const char* channel_name)
36 : ChannelOutput(channel_name),
41 std::span<std::byte> AcquireBuffer() override { return buffer_; }
43 Status SendAndReleaseBuffer(size_t size) override {
44 return hdlc_lite::WriteInformationFrame(
45 address_, buffer_.first(size), writer_);
49 stream::Writer& writer_;
50 const std::span<std::byte> buffer_;
51 const uint8_t address_;
54 // RpcChannelOutput with its own buffer.
55 template <size_t buffer_size>
56 class RpcChannelOutputBuffer : public rpc::ChannelOutput {
58 constexpr RpcChannelOutputBuffer(stream::Writer& writer,
60 const char* channel_name)
61 : ChannelOutput(channel_name), writer_(writer), address_(address) {}
63 std::span<std::byte> AcquireBuffer() override { return buffer_; }
65 Status SendAndReleaseBuffer(size_t size) override {
66 return hdlc_lite::WriteInformationFrame(
67 address_, std::span(buffer_.data(), size), writer_);
71 stream::Writer& writer_;
72 std::array<std::byte, buffer_size> buffer_;
73 const uint8_t address_;
76 } // namespace pw::hdlc_lite