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_hdlc/encoder.h"
19 #include "pw_log_tokenized/base64.h"
20 #include "pw_log_tokenized/handler.h"
21 #include "pw_span/span.h"
22 #include "pw_stream/sys_io_stream.h"
23 #include "pw_string/string.h"
24 #include "pw_tokenizer/base64.h"
25
26 namespace pw::log_tokenized {
27 namespace {
28
29 inline constexpr int kBase64LogHdlcAddress = 1;
30
31 stream::SysIoWriter writer;
32
33 } // namespace
34
35 // Base64-encodes tokenized logs and writes them to pw::sys_io as HDLC frames.
pw_log_tokenized_HandleLog(uint32_t,const uint8_t log_buffer[],size_t size_bytes)36 extern "C" void pw_log_tokenized_HandleLog(
37 uint32_t, // TODO(hepler): Use the metadata for filtering.
38 const uint8_t log_buffer[],
39 size_t size_bytes) {
40 // Encode the tokenized message as Base64.
41 const pw::InlineBasicString base64_string =
42 PrefixedBase64Encode(log_buffer, size_bytes);
43
44 // HDLC-encode the Base64 string via a SysIoWriter. Ignore errors since we
45 // cannot take any action anyway.
46 hdlc::WriteUIFrame(
47 kBase64LogHdlcAddress, as_bytes(span(base64_string)), writer)
48 .IgnoreError();
49 }
50
51 } // namespace pw::log_tokenized
52