Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_hdlc / public / pw_hdlc / rpc_channel.h
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/light.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 // wrap this in a pw::rpc::SynchronizedChannelOutput.
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.
36   constexpr RpcChannelOutput(stream::Writer& writer,
37                              std::span<std::byte> buffer,
38                              uint8_t address,
39                              const char* channel_name)
40       : ChannelOutput(channel_name),
41         writer_(writer),
42         buffer_(buffer),
43         address_(address) {}
44
45   std::span<std::byte> AcquireBuffer() override { return buffer_; }
46
47   Status SendAndReleaseBuffer(std::span<const std::byte> buffer) override {
48     PW_DASSERT(buffer.data() == buffer_.data());
49     if (buffer.empty()) {
50       return OkStatus();
51     }
52     return hdlc::WriteUIFrame(address_, buffer, writer_);
53   }
54
55  private:
56   stream::Writer& writer_;
57   const std::span<std::byte> buffer_;
58   const uint8_t address_;
59 };
60
61 // RpcChannelOutput with its own buffer.
62 //
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 {
67  public:
68   constexpr RpcChannelOutputBuffer(stream::Writer& writer,
69                                    uint8_t address,
70                                    const char* channel_name)
71       : ChannelOutput(channel_name), writer_(writer), address_(address) {}
72
73   std::span<std::byte> AcquireBuffer() override { return buffer_; }
74
75   Status SendAndReleaseBuffer(std::span<const std::byte> buffer) override {
76     PW_DASSERT(buffer.data() == buffer_.data());
77     if (buffer.empty()) {
78       return OkStatus();
79     }
80     return hdlc::WriteUIFrame(address_, buffer, writer_);
81   }
82
83  private:
84   stream::Writer& writer_;
85   std::array<std::byte, buffer_size> buffer_;
86   const uint8_t address_;
87 };
88
89 }  // namespace pw::hdlc