Wyatt Hepler | b8db509 | 2020-09-17 10:36:41 -0700 | [diff] [blame] | 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 | |
Alexei Frolov | d3e5cb7 | 2021-01-08 13:08:45 -0800 | [diff] [blame] | 15 | #include "pw_hdlc/rpc_packets.h" |
Wyatt Hepler | b8db509 | 2020-09-17 10:36:41 -0700 | [diff] [blame] | 16 | |
| 17 | #include "pw_status/try.h" |
| 18 | #include "pw_sys_io/sys_io.h" |
| 19 | |
Alexei Frolov | d3e5cb7 | 2021-01-08 13:08:45 -0800 | [diff] [blame] | 20 | namespace pw::hdlc { |
Wyatt Hepler | b8db509 | 2020-09-17 10:36:41 -0700 | [diff] [blame] | 21 | |
| 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 | |
Alexei Frolov | d3e5cb7 | 2021-01-08 13:08:45 -0800 | [diff] [blame] | 41 | } // namespace pw::hdlc |