Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / targets / host / 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 #include <cstddef>
16 #include <cstdint>
17
18 #include "pw_hdlc/rpc_channel.h"
19 #include "pw_hdlc/rpc_packets.h"
20 #include "pw_log/log.h"
21 #include "pw_rpc/synchronized_channel_output.h"
22 #include "pw_rpc_system_server/rpc_server.h"
23 #include "pw_stream/socket_stream.h"
24
25 namespace pw::rpc::system_server {
26 namespace {
27
28 constexpr size_t kMaxTransmissionUnit = 256;
29 constexpr uint16_t kSocketPort = 33000;
30
31 stream::SocketStream socket_stream;
32 sync::Mutex channel_output_mutex;
33 rpc::SynchronizedChannelOutput<
34     hdlc::RpcChannelOutputBuffer<kMaxTransmissionUnit>>
35     hdlc_channel_output(channel_output_mutex,
36                         socket_stream,
37                         hdlc::kDefaultRpcAddress,
38                         "HDLC channel");
39 Channel channels[] = {rpc::Channel::Create<1>(&hdlc_channel_output)};
40 rpc::Server server(channels);
41
42 }  // namespace
43
44 void Init() {
45   log_basic::SetOutput([](std::string_view log) {
46     hdlc::WriteUIFrame(1, std::as_bytes(std::span(log)), socket_stream);
47   });
48
49   socket_stream.Init(kSocketPort);
50 }
51
52 rpc::Server& Server() { return server; }
53
54 Status Start() {
55   // Declare a buffer for decoding incoming HDLC frames.
56   std::array<std::byte, kMaxTransmissionUnit> input_buffer;
57   hdlc::Decoder decoder(input_buffer);
58
59   while (true) {
60     std::array<std::byte, kMaxTransmissionUnit> data;
61     auto ret_val = socket_stream.Read(data);
62     if (ret_val.ok()) {
63       for (std::byte byte : ret_val.value()) {
64         if (auto result = decoder.Process(byte); result.ok()) {
65           hdlc::Frame& frame = result.value();
66           if (frame.address() == hdlc::kDefaultRpcAddress) {
67             server.ProcessPacket(frame.data(), hdlc_channel_output);
68           }
69         }
70       }
71     }
72   }
73 }
74
75 }  // namespace pw::rpc::system_server