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