blob: 0f4cbffb94ca08c44df3da744ddc9aa60ecb30c3 [file] [log] [blame]
// Copyright 2022 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.
#define PW_LOG_MODULE_NAME "ClientReader"
#include "rpc_demo/client/client_reader.h"
#include <pw_hdlc/decoder.h>
#include <pw_hdlc/rpc_packets.h>
#include <pw_log/log.h>
#include <pw_status/status.h>
namespace rpc_demo {
ClientReader::ClientReader(pw::stream::Reader& rx, pw::rpc::Client* rpc_client)
: rx_(rx), rpc_client_(rpc_client), decoder_(decode_buffer_) {}
bool ClientReader::ParsePacket() {
PW_LOG_INFO("Reading bytes...");
while (true) {
std::byte data;
if (auto result = rx_.Read(&data, 1); !result.ok()) {
PW_LOG_ERROR("Failed to read data");
continue;
}
PW_LOG_DEBUG("0x%02x", static_cast<uint8_t>(data));
pw::Result<pw::hdlc::Frame> result = decoder_.Process(data);
if (!result.ok()) {
continue;
}
pw::hdlc::Frame& frame = result.value();
if (frame.address() != pw::hdlc::kDefaultRpcAddress) {
return false;
}
PW_LOG_INFO("Client got packet!");
auto client_result = rpc_client_->ProcessPacket(frame.data());
PW_LOG_DEBUG("Client ProcessPacket status (%s)",
pw_StatusString(client_result));
decoder_.Clear();
return true;
}
}
} // namespace rpc_demo