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 #include <cstdio>
18
19 #include "pw_assert/check.h"
20 #include "pw_hdlc/decoder.h"
21 #include "pw_hdlc/default_addresses.h"
22 #include "pw_hdlc/encoded_size.h"
23 #include "pw_hdlc/rpc_channel.h"
24 #include "pw_log/log.h"
25 #include "pw_rpc_system_server/rpc_server.h"
26 #include "pw_stream/socket_stream.h"
27
28 namespace pw::rpc::system_server {
29 namespace {
30
31 // Hard-coded to 1055 bytes, which is enough to fit 512-byte payloads when using
32 // HDLC framing.
33 constexpr size_t kMaxTransmissionUnit = 1055;
34 uint16_t socket_port = 33000;
35
36 static_assert(kMaxTransmissionUnit ==
37 hdlc::MaxEncodedFrameSize(rpc::cfg::kEncodingBufferSizeBytes));
38
39 stream::ServerSocket server_socket;
40 stream::SocketStream socket_stream;
41
42 hdlc::FixedMtuChannelOutput<kMaxTransmissionUnit> hdlc_channel_output(
43 socket_stream, hdlc::kDefaultRpcAddress, "HDLC channel");
44 Channel channels[] = {rpc::Channel::Create<1>(&hdlc_channel_output)};
45 rpc::Server server(channels);
46
47 } // namespace
48
set_socket_port(uint16_t new_socket_port)49 void set_socket_port(uint16_t new_socket_port) {
50 socket_port = new_socket_port;
51 }
52
SetServerSockOpt(int level,int optname,const void * optval,unsigned int optlen)53 int SetServerSockOpt(int level,
54 int optname,
55 const void* optval,
56 unsigned int optlen) {
57 return socket_stream.SetSockOpt(level, optname, optval, optlen);
58 }
59
Init()60 void Init() {
61 log_basic::SetOutput([](std::string_view log) {
62 std::fprintf(stderr, "%.*s\n", static_cast<int>(log.size()), log.data());
63 hdlc::WriteUIFrame(1, as_bytes(span<const char>(log)), socket_stream)
64 .IgnoreError(); // TODO: b/242598609 - Handle Status properly
65 });
66
67 PW_LOG_INFO("Starting pw_rpc server on port %d", socket_port);
68 PW_CHECK_OK(server_socket.Listen(socket_port));
69 auto accept_result = server_socket.Accept();
70 PW_CHECK_OK(accept_result.status());
71 socket_stream = *std::move(accept_result);
72 }
73
Server()74 rpc::Server& Server() { return server; }
75
Start()76 Status Start() {
77 constexpr size_t kDecoderBufferSize =
78 hdlc::Decoder::RequiredBufferSizeForFrameSize(kMaxTransmissionUnit);
79 // Declare a buffer for decoding incoming HDLC frames.
80 std::array<std::byte, kDecoderBufferSize> input_buffer;
81 hdlc::Decoder decoder(input_buffer);
82
83 while (true) {
84 std::array<std::byte, kMaxTransmissionUnit> data;
85 auto ret_val = socket_stream.Read(data);
86 if (!ret_val.ok()) {
87 if (ret_val.status() == Status::OutOfRange()) {
88 // An out of range status indicates the remote end has disconnected.
89 return OkStatus();
90 }
91 continue;
92 }
93
94 for (std::byte byte : ret_val.value()) {
95 auto result = decoder.Process(byte);
96 if (!result.ok()) {
97 // Non-OK means there isn't a complete packet yet, or there was some
98 // other issue. Wait for more bytes that form a complete packet.
99 continue;
100 }
101 hdlc::Frame& frame = result.value();
102 if (frame.address() != hdlc::kDefaultRpcAddress) {
103 // Wrong address; ignore the packet for now. In the future, this branch
104 // could expand to add packet routing or metrics.
105 continue;
106 }
107
108 server.ProcessPacket(frame.data()).IgnoreError();
109 }
110 }
111 }
112
113 } // namespace pw::rpc::system_server
114