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
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"
25 namespace pw::rpc::system_server {
28 constexpr size_t kMaxTransmissionUnit = 256;
29 constexpr uint16_t kSocketPort = 33000;
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,
37 hdlc::kDefaultRpcAddress,
39 Channel channels[] = {rpc::Channel::Create<1>(&hdlc_channel_output)};
40 rpc::Server server(channels);
45 log_basic::SetOutput([](std::string_view log) {
46 hdlc::WriteUIFrame(1, std::as_bytes(std::span(log)), socket_stream);
49 socket_stream.Init(kSocketPort);
52 rpc::Server& Server() { return server; }
55 // Declare a buffer for decoding incoming HDLC frames.
56 std::array<std::byte, kMaxTransmissionUnit> input_buffer;
57 hdlc::Decoder decoder(input_buffer);
60 std::array<std::byte, kMaxTransmissionUnit> data;
61 auto ret_val = socket_stream.Read(data);
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);
75 } // namespace pw::rpc::system_server