• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 <array>
17 #include <span>
18 
19 #include "pw_assert/assert.h"
20 #include "pw_hdlc/encoder.h"
21 #include "pw_rpc/channel.h"
22 #include "pw_stream/stream.h"
23 
24 namespace pw::hdlc {
25 
26 // Custom HDLC ChannelOutput class to write and read data through serial using
27 // the HDLC protocol.
28 //
29 // WARNING: This ChannelOutput is not thread-safe. If thread-safety is required,
30 // create a similar class that adds a mtuex to Send.
31 class RpcChannelOutput : public rpc::ChannelOutput {
32  public:
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.
RpcChannelOutput(stream::Writer & writer,uint64_t address,const char * channel_name)36   constexpr RpcChannelOutput(stream::Writer& writer,
37                              uint64_t address,
38                              const char* channel_name)
39       : ChannelOutput(channel_name), writer_(writer), address_(address) {}
40 
Send(std::span<const std::byte> buffer)41   Status Send(std::span<const std::byte> buffer) override {
42     return hdlc::WriteUIFrame(address_, buffer, writer_);
43   }
44 
45  private:
46   stream::Writer& writer_;
47   const uint64_t address_;
48 };
49 
50 }  // namespace pw::hdlc
51