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 "pw_hdlc/encoder.h"
21 #include "pw_log_tokenized/handler.h"
22 #include "pw_span/span.h"
23 #include "pw_stream/sys_io_stream.h"
24 #include "pw_tokenizer/base64.h"
25
26 namespace pw::log_tokenized {
27 namespace {
28
29 stream::SysIoWriter writer;
30
31 } // namespace
32
33 // 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)34 extern "C" void pw_log_tokenized_HandleLog(
35 uint32_t, // TODO(hepler): Use the metadata for filtering.
36 const uint8_t log_buffer[],
37 size_t size_bytes) {
38 // Encode the tokenized message as Base64.
39 char base64_buffer[tokenizer::kDefaultBase64EncodedBufferSize];
40 const size_t base64_bytes = tokenizer::PrefixedBase64Encode(
41 span(log_buffer, size_bytes), base64_buffer);
42 base64_buffer[base64_bytes] = '\0';
43
44 // HDLC-encode the Base64 string via a SysIoWriter.
45 hdlc::WriteUIFrame(PW_LOG_TOKENIZED_BASE64_LOG_HDLC_ADDRESS,
46 as_bytes(span(base64_buffer, base64_bytes)),
47 writer);
48 }
49
50 } // namespace pw::log_tokenized
51