blob: e3cf4ac9c5616f1365834e7970e611bd7c7a3cee [file] [log] [blame]
Wyatt Heplerb8db5092020-09-17 10:36:41 -07001// 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
Alexei Frolovd3e5cb72021-01-08 13:08:45 -080015#include "pw_hdlc/rpc_packets.h"
Wyatt Heplerb8db5092020-09-17 10:36:41 -070016
17#include "pw_status/try.h"
18#include "pw_sys_io/sys_io.h"
19
Alexei Frolovd3e5cb72021-01-08 13:08:45 -080020namespace pw::hdlc {
Wyatt Heplerb8db5092020-09-17 10:36:41 -070021
22Status ReadAndProcessPackets(rpc::Server& server,
23 rpc::ChannelOutput& output,
24 std::span<std::byte> decode_buffer,
25 unsigned rpc_address) {
26 Decoder decoder(decode_buffer);
27
28 while (true) {
29 std::byte data;
30 PW_TRY(sys_io::ReadByte(&data));
31
32 if (auto result = decoder.Process(data); result.ok()) {
33 Frame& frame = result.value();
34 if (frame.address() == rpc_address) {
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +000035 server.ProcessPacket(frame.data(), output)
36 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
Wyatt Heplerb8db5092020-09-17 10:36:41 -070037 }
38 }
39 }
40}
41
Alexei Frolovd3e5cb72021-01-08 13:08:45 -080042} // namespace pw::hdlc