Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / common / pigweed / system_rpc_server.cc
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
15 // THIS FILE IS DEPRECATED, keep only until examples/pigweed-app depends on
16 // third_party/pigweed/repo/pw_hdlc/rpc_example/hdlc_rpc_server.cc
17
18 #include <cstddef>
19
20 #include "pw_hdlc/rpc_channel.h"
21 #include "pw_hdlc/rpc_packets.h"
22 #include "pw_log/log.h"
23 #include "pw_rpc_system_server/rpc_server.h"
24 #include "pw_stream/sys_io_stream.h"
25
26 namespace pw::rpc::system_server {
27 namespace {
28
29 constexpr size_t kMaxTransmissionUnit = 256;
30
31 // Used to write HDLC data to pw::sys_io.
32 stream::SysIoWriter writer;
33 stream::SysIoReader reader;
34
35 // Set up the output channel for the pw_rpc server to use.
36 hdlc::RpcChannelOutputBuffer<kMaxTransmissionUnit> hdlc_channel_output(writer, pw::hdlc::kDefaultRpcAddress, "HDLC channel");
37 Channel channels[] = { pw::rpc::Channel::Create<1>(&hdlc_channel_output) };
38 rpc::Server server(channels);
39
40 } // namespace
41
42 void Init()
43 {
44     // Send log messages to HDLC address 1. This prevents logs from interfering
45     // with pw_rpc communications.
46     pw::log_basic::SetOutput([](std::string_view log) { pw::hdlc::WriteUIFrame(1, std::as_bytes(std::span(log)), writer); });
47 }
48
49 rpc::Server & Server()
50 {
51     return server;
52 }
53
54 Status Start()
55 {
56     // Declare a buffer for decoding incoming HDLC frames.
57     std::array<std::byte, kMaxTransmissionUnit> input_buffer;
58     hdlc::Decoder decoder(input_buffer);
59
60     while (true)
61     {
62         std::byte byte;
63         Status ret_val = pw::sys_io::ReadByte(&byte);
64         if (!ret_val.ok())
65         {
66             return ret_val;
67         }
68         if (auto result = decoder.Process(byte); result.ok())
69         {
70             hdlc::Frame & frame = result.value();
71             if (frame.address() == hdlc::kDefaultRpcAddress)
72             {
73                 server.ProcessPacket(frame.data(), hdlc_channel_output);
74             }
75         }
76     }
77 }
78
79 } // namespace pw::rpc::system_server