third_party.pigweed.src/targets/host/system_rpc_server.cc
Jason Graffius 322d594f36 pw_stream: Add client-side SocketStream support
Adds the SocketStream::Connect member function, allowing a SocketStream
to be established from the client side of a socket rather than only the
serving side as before.

Additionally, this change renames the existing Init member function to
Serve instead to make it clear that it is not required before calling
Connect and is instead an alternative way of establishing a SocketStream
connection.

Change-Id: Ib3fdb9f5405eee92ae1a166c64e990cb9c05fca7
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/31261
Pigweed-Auto-Submit: Jason Graffius <jgraff@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Alexei Frolov <frolv@google.com>
Reviewed-by: Keir Mierle <keir@google.com>
2021-02-24 19:16:52 +00:00

76 lines
2.3 KiB
C++

// Copyright 2020 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#include <cstddef>
#include <cstdint>
#include "pw_hdlc/rpc_channel.h"
#include "pw_hdlc/rpc_packets.h"
#include "pw_log/log.h"
#include "pw_rpc/synchronized_channel_output.h"
#include "pw_rpc_system_server/rpc_server.h"
#include "pw_stream/socket_stream.h"
namespace pw::rpc::system_server {
namespace {
constexpr size_t kMaxTransmissionUnit = 256;
constexpr uint16_t kSocketPort = 33000;
stream::SocketStream socket_stream;
sync::Mutex channel_output_mutex;
rpc::SynchronizedChannelOutput<
hdlc::RpcChannelOutputBuffer<kMaxTransmissionUnit>>
hdlc_channel_output(channel_output_mutex,
socket_stream,
hdlc::kDefaultRpcAddress,
"HDLC channel");
Channel channels[] = {rpc::Channel::Create<1>(&hdlc_channel_output)};
rpc::Server server(channels);
} // namespace
void Init() {
log_basic::SetOutput([](std::string_view log) {
hdlc::WriteUIFrame(1, std::as_bytes(std::span(log)), socket_stream);
});
socket_stream.Serve(kSocketPort);
}
rpc::Server& Server() { return server; }
Status Start() {
// Declare a buffer for decoding incoming HDLC frames.
std::array<std::byte, kMaxTransmissionUnit> input_buffer;
hdlc::Decoder decoder(input_buffer);
while (true) {
std::array<std::byte, kMaxTransmissionUnit> data;
auto ret_val = socket_stream.Read(data);
if (ret_val.ok()) {
for (std::byte byte : ret_val.value()) {
if (auto result = decoder.Process(byte); result.ok()) {
hdlc::Frame& frame = result.value();
if (frame.address() == hdlc::kDefaultRpcAddress) {
server.ProcessPacket(frame.data(), hdlc_channel_output);
}
}
}
}
}
}
} // namespace pw::rpc::system_server