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 "pw_hdlc/rpc_packets.h"
16 
17 #include "pw_status/try.h"
18 #include "pw_sys_io/sys_io.h"
19 
20 namespace pw::hdlc {
21 
ReadAndProcessPackets(rpc::Server & server,rpc::ChannelOutput & output,std::span<std::byte> decode_buffer,unsigned rpc_address)22 Status 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) {
35         server.ProcessPacket(frame.data(), output);
36       }
37     }
38   }
39 }
40 
41 }  // namespace pw::hdlc
42