PigweedLogger.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. *
  3. * Copyright (c) 2020 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /*
  19. * @file PigweedLogger.cpp
  20. *
  21. * This file contains a backend of Zephyr logging system, based on Pigweed HDLC
  22. * over UART transport. It allows to send log messages even if the application
  23. * needs to use HDLC/UART for another purpose like the RPC server.
  24. */
  25. #include <zephyr/kernel.h>
  26. #include <zephyr/logging/log.h>
  27. #include <zephyr/logging/log_backend.h>
  28. #include <zephyr/logging/log_backend_std.h>
  29. #include <zephyr/logging/log_output.h>
  30. #include <pw_hdlc/encoder.h>
  31. #include <pw_stream/sys_io_stream.h>
  32. #include <pw_sys_io_nrfconnect/init.h>
  33. #include "pw_span/span.h"
  34. #include <cassert>
  35. #include <cstdint>
  36. #include <string_view>
  37. namespace PigweedLogger {
  38. namespace {
  39. #if CONFIG_LOG
  40. #if !CONFIG_LOG_MODE_IMMEDIATE
  41. #error "Backend of Zephyr logger based on Pigweed HDLC requires LOG_MODE_IMMEDIATE=y"
  42. #endif
  43. constexpr uint8_t kLogHdlcAddress = 1; // Send log messages to HDLC address 1 (other than RPC communication)
  44. constexpr size_t kWriteBufferSize = 128; // Buffer for constructing HDLC frames
  45. // Exclusive access to the backend is needed to make sure that log messages coming
  46. // from different threads are not interwoven.
  47. K_SEM_DEFINE(sLoggerLock, 1, 1);
  48. pw::stream::SysIoWriter sWriter;
  49. size_t sWriteBufferPos;
  50. uint8_t sWriteBuffer[kWriteBufferSize];
  51. bool sIsPanicMode;
  52. void flush()
  53. {
  54. pw::hdlc::WriteUIFrame(kLogHdlcAddress, pw::as_bytes(pw::span(sWriteBuffer, sWriteBufferPos)), sWriter);
  55. sWriteBufferPos = 0;
  56. }
  57. int putString(uint8_t * buffer, size_t size, void * /* ctx */)
  58. {
  59. assert(sWriteBufferPos < kWriteBufferSize);
  60. for (size_t i = 0; i < size; ++i)
  61. {
  62. // Send each line excluding "\r\n" in a separate frame
  63. if (buffer[i] == '\r')
  64. continue;
  65. if (buffer[i] == '\n')
  66. {
  67. flush();
  68. continue;
  69. }
  70. sWriteBuffer[sWriteBufferPos++] = buffer[i];
  71. if (sWriteBufferPos == kWriteBufferSize)
  72. flush();
  73. }
  74. return size;
  75. }
  76. LOG_OUTPUT_DEFINE(pigweedLogOutput, putString, nullptr, 0);
  77. void init(const log_backend *)
  78. {
  79. pw_sys_io_Init();
  80. }
  81. void processMessage(const struct log_backend * const backend, union log_msg_generic * msg)
  82. {
  83. int ret = k_sem_take(&sLoggerLock, K_FOREVER);
  84. assert(ret == 0);
  85. if (!sIsPanicMode)
  86. {
  87. log_format_func_t outputFunc = log_format_func_t_get(LOG_OUTPUT_TEXT);
  88. outputFunc(&pigweedLogOutput, &msg->log, log_backend_std_get_flags());
  89. }
  90. k_sem_give(&sLoggerLock);
  91. }
  92. void panic(const log_backend *)
  93. {
  94. int ret = k_sem_take(&sLoggerLock, K_FOREVER);
  95. assert(ret == 0);
  96. log_backend_std_panic(&pigweedLogOutput);
  97. flush();
  98. sIsPanicMode = true;
  99. k_sem_give(&sLoggerLock);
  100. }
  101. const log_backend_api pigweedLogApi = {
  102. .process = processMessage,
  103. .panic = panic,
  104. .init = init,
  105. };
  106. LOG_BACKEND_DEFINE(pigweedLogBackend, pigweedLogApi, /* autostart */ true);
  107. #endif // CONFIG_LOG
  108. } // namespace
  109. k_sem * GetSemaphore()
  110. {
  111. #if CONFIG_LOG
  112. return &sLoggerLock;
  113. #else
  114. return nullptr;
  115. #endif
  116. }
  117. } // namespace PigweedLogger