blob: 04a70adbab016adaeacbd18b0d09ec4b2beb0522 [file] [log] [blame]
Alexei Frolov80246792020-11-05 21:12:45 -08001// 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 "pw_hdlc_lite/encoder.h"
16#include "pw_hdlc_lite/rpc_channel.h"
17#include "pw_hdlc_lite/rpc_packets.h"
18#include "pw_hdlc_lite/sys_io_stream.h"
19#include "pw_log/log.h"
20#include "pw_unit_test/framework.h"
21#include "pw_unit_test/unit_test_service.h"
22
23// TODO(frolv): This file is largely copied from the HDLC RPC example. It should
24// be updated to use the system RPC server facade when that is ready.
25
26namespace pw {
27namespace {
28
29constexpr size_t kMaxTransmissionUnit = 256;
30
31// Used to write HDLC data to pw::sys_io.
32stream::SysIoWriter writer;
33
34// Set up the output channel for the pw_rpc server to use to use.
35hdlc_lite::RpcChannelOutputBuffer<kMaxTransmissionUnit> hdlc_channel_output(
36 writer, hdlc_lite::kDefaultRpcAddress, "HDLC channel");
37
38rpc::Channel channels[] = {rpc::Channel::Create<1>(&hdlc_channel_output)};
39
40// Declare the pw_rpc server with the HDLC channel.
41rpc::Server server(channels);
42
43unit_test::UnitTestService unit_test_service;
44
45void RegisterServices() { server.RegisterService(unit_test_service); }
46
47} // namespace
48
49extern "C" int main() {
50 // Send log messages to HDLC address 1. This prevents logs from interfering
51 // with pw_rpc communications.
52 log_basic::SetOutput([](std::string_view log) {
53 hdlc_lite::WriteInformationFrame(1, std::as_bytes(std::span(log)), writer);
54 });
55
56 RegisterServices();
57
58 // Declare a buffer for decoding incoming HDLC frames.
59 std::array<std::byte, kMaxTransmissionUnit> input_buffer;
60
61 PW_LOG_INFO("Starting pw_rpc server");
62 hdlc_lite::ReadAndProcessPackets(server, hdlc_channel_output, input_buffer);
63
64 return 0;
65}
66
67} // namespace pw