• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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 #include <cstdint>
16 #include <cstring>
17 #include <memory>
18 #include <span>
19 
20 #include "pw_assert/config.h"
21 #include "pw_assert_tokenized/handler.h"
22 #include "pw_base64/base64.h"
23 #include "pw_log/log.h"
24 #include "pw_log_tokenized/log_tokenized.h"
25 
pw_assert_tokenized_HandleAssertFailure(uint32_t tokenized_file_name,int line_number)26 extern "C" void pw_assert_tokenized_HandleAssertFailure(
27     uint32_t tokenized_file_name, int line_number) {
28   // Buffer size for binary->base64 conversion with a null terminator.
29   constexpr size_t kBufferSize =
30       pw::base64::EncodedSize(sizeof(tokenized_file_name)) + 1;
31   std::byte* hash_buffer = reinterpret_cast<std::byte*>(&tokenized_file_name);
32   char base64_buffer[kBufferSize];
33 
34   size_t len =
35       pw::base64::Encode(std::span(hash_buffer, sizeof(tokenized_file_name)),
36                          std::span(base64_buffer));
37   base64_buffer[len] = '\0';
38 #if PW_ASSERT_ENABLE_DEBUG
39   PW_LOG(PW_LOG_LEVEL_FATAL,
40          PW_LOG_FLAGS,
41          "PW_ASSERT() or PW_DASSERT() failure at $%s:%d",
42          base64_buffer,
43          line_number);
44 #else
45   PW_LOG(PW_LOG_LEVEL_FATAL,
46          PW_LOG_FLAGS,
47          "PW_ASSERT() failure. Note: PW_DASSERT disabled $%s:%d",
48          base64_buffer,
49          line_number);
50 #endif  // PW_ASSERT_ENABLE_DEBUG
51   PW_UNREACHABLE;
52 }
53 
pw_assert_tokenized_HandleCheckFailure(uint32_t tokenized_message,int line_number)54 extern "C" void pw_assert_tokenized_HandleCheckFailure(
55     uint32_t tokenized_message, int line_number) {
56   // TODO(amontanez): There should be a less-hacky way to assemble this.
57   const uint32_t payload = _PW_LOG_TOKENIZED_LEVEL(PW_LOG_LEVEL_FATAL) |
58                            _PW_LOG_TOKENIZED_FLAGS(PW_LOG_FLAGS) |
59                            _PW_LOG_TOKENIZED_LINE(line_number);
60   uint8_t token_buffer[sizeof(tokenized_message)];
61   memcpy(token_buffer, &tokenized_message, sizeof(tokenized_message));
62 
63   pw_tokenizer_HandleEncodedMessageWithPayload(
64       payload, token_buffer, sizeof(token_buffer));
65   PW_UNREACHABLE;
66 }
67