aboutsummaryrefslogtreecommitdiff
path: root/targets/mimxrt595_evk/system_rpc_server.cc
blob: 52be514355a905ff7591fad4699583d7222c6ac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 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 "pw_hdlc/rpc_channel.h"
#include "pw_hdlc/rpc_packets.h"
#include "pw_log/log.h"
#include "pw_rpc_system_server/rpc_server.h"
#include "pw_stream/sys_io_stream.h"

namespace pw::rpc::system_server {
namespace {

constexpr size_t kMaxTransmissionUnit = 256;

// Used to write HDLC data to pw::sys_io.
stream::SysIoWriter writer;
stream::SysIoReader reader;

// Set up the output channel for the pw_rpc server to use.
hdlc::RpcChannelOutput hdlc_channel_output(writer,
                                           pw::hdlc::kDefaultRpcAddress,
                                           "HDLC channel");
Channel channels[] = {pw::rpc::Channel::Create<1>(&hdlc_channel_output)};
rpc::Server server(channels);

}  // namespace

void Init() {
  // Send log messages to HDLC address 1. This prevents logs from interfering
  // with pw_rpc communications.
  pw::log_basic::SetOutput([](std::string_view log) {
    pw::hdlc::WriteUIFrame(1, std::as_bytes(std::span(log)), writer);
  });
}

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::byte byte;
    Status ret_val = pw::sys_io::ReadByte(&byte);
    if (!ret_val.ok()) {
      return ret_val;
    }
    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