blob: 87867fbe4b9ea5af9c042a250bb8acf940715c44 [file] [log] [blame]
// Copyright 2024 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.
// This function serves as a backend for pw_tokenizer / pw_log_tokenized that
// encodes tokenized logs as Base64 and writes them using HDLC.
#include "pw_bytes/endian.h"
#include "pw_bytes/span.h"
#include "pw_hdlc/encoder.h"
#include "pw_log/proto_utils.h"
#include "pw_log_tokenized/handler.h"
#include "pw_log_tokenized/metadata.h"
#include "pw_span/span.h"
#include "pw_stream/sys_io_stream.h"
#include "pw_string/string.h"
#include "pw_tokenizer/tokenize.h"
namespace pw::log_tokenized {
namespace {
inline constexpr int kLogProtoHdlcAddress = 1;
std::array<std::byte, 512> log_encode_buffer;
stream::SysIoWriter writer;
// Create a log encode failure message in place of a proper crash handler.
constexpr uint32_t kLogEncodeFailToken =
PW_TOKENIZE_STRING("pw::log::EncodeTokenizedLog failed");
constexpr std::array<std::byte, 4> kLogEncodeFailBytes =
pw::bytes::CopyInOrder(pw::endian::little, kLogEncodeFailToken);
} // namespace
// Proto encodes tokenized logs and writes them to pw::sys_io as HDLC frames.
extern "C" void pw_log_tokenized_HandleLog(uint32_t payload,
const uint8_t log_buffer[],
size_t size_bytes) {
pw::log_tokenized::Metadata metadata = payload;
Result<ConstByteSpan> encoded_log_result = pw::log::EncodeTokenizedLog(
metadata, log_buffer, size_bytes, /*timestamp=*/0, log_encode_buffer);
if (encoded_log_result.ok()) {
// HDLC-encode the encoded proto via a SysIoWriter.
hdlc::WriteUIFrame(kLogProtoHdlcAddress, encoded_log_result.value(),
writer);
} else {
hdlc::WriteUIFrame(kLogProtoHdlcAddress, kLogEncodeFailBytes, writer);
}
}
} // namespace pw::log_tokenized