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 // This function serves as a backend for pw_tokenizer / pw_log_tokenized that
16 // encodes tokenized logs as Base64 and writes them using HDLC.
17
18 #include "pw_log_tokenized/base64_over_hdlc.h"
19
20 #include <span>
21
22 #include "pw_hdlc/encoder.h"
23 #include "pw_stream/sys_io_stream.h"
24 #include "pw_tokenizer/base64.h"
25 #include "pw_tokenizer/tokenize_to_global_handler_with_payload.h"
26
27 namespace pw::log_tokenized {
28 namespace {
29
30 stream::SysIoWriter writer;
31
32 } // namespace
33
34 // Base64-encodes tokenized logs and writes them to pw::sys_io as HDLC frames.
pw_tokenizer_HandleEncodedMessageWithPayload(pw_tokenizer_Payload,const uint8_t log_buffer[],size_t size_bytes)35 extern "C" void pw_tokenizer_HandleEncodedMessageWithPayload(
36 pw_tokenizer_Payload, // TODO(hepler): Use the metadata for filtering.
37 const uint8_t log_buffer[],
38 size_t size_bytes) {
39 // Encode the tokenized message as Base64.
40 char base64_buffer[tokenizer::kDefaultBase64EncodedBufferSize];
41 const size_t base64_bytes = tokenizer::PrefixedBase64Encode(
42 std::span(log_buffer, size_bytes), base64_buffer);
43 base64_buffer[base64_bytes] = '\0';
44
45 // HDLC-encode the Base64 string via a SysIoWriter.
46 hdlc::WriteUIFrame(PW_LOG_TOKENIZED_BASE64_LOG_HDLC_ADDRESS,
47 std::as_bytes(std::span(base64_buffer, base64_bytes)),
48 writer);
49 }
50
51 } // namespace pw::log_tokenized
52