log_backend_spinel.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2020 Nordic Semiconductor ASA
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <logging/log_backend.h>
  7. #include <logging/log_backend_std.h>
  8. #include <logging/log_output.h>
  9. #include <openthread/platform/logging.h>
  10. #include <utils/uart.h>
  11. #include <platform-zephyr.h>
  12. #ifndef CONFIG_LOG_BACKEND_SPINEL_BUFFER_SIZE
  13. #define CONFIG_LOG_BACKEND_SPINEL_BUFFER_SIZE 0
  14. #endif
  15. static uint8_t char_buf[CONFIG_LOG_BACKEND_SPINEL_BUFFER_SIZE];
  16. static bool panic_mode;
  17. static uint16_t last_log_level;
  18. static int write(uint8_t *data, size_t length, void *ctx);
  19. LOG_OUTPUT_DEFINE(log_output_spinel, write, char_buf, sizeof(char_buf));
  20. static inline bool is_panic_mode(void)
  21. {
  22. return panic_mode;
  23. }
  24. static void put(const struct log_backend *const backend,
  25. struct log_msg *msg)
  26. {
  27. /* prevent adding CRLF, which may crash spinel decoding */
  28. uint32_t flag = LOG_OUTPUT_FLAG_CRLF_NONE;
  29. last_log_level = msg->hdr.ids.level;
  30. log_backend_std_put(&log_output_spinel, flag, msg);
  31. }
  32. static void process(const struct log_backend *const backend,
  33. union log_msg2_generic *msg)
  34. {
  35. /* prevent adding CRLF, which may crash spinel decoding */
  36. uint32_t flags = LOG_OUTPUT_FLAG_CRLF_NONE | log_backend_std_get_flags();
  37. log_output_msg2_process(&log_output_spinel, &msg->log, flags);
  38. }
  39. static void sync_string(const struct log_backend *const backend,
  40. struct log_msg_ids src_level, uint32_t timestamp,
  41. const char *fmt, va_list ap)
  42. {
  43. /* prevent adding CRLF, which may crash spinel decoding */
  44. uint32_t flag = LOG_OUTPUT_FLAG_CRLF_NONE;
  45. log_backend_std_sync_string(&log_output_spinel, flag, src_level,
  46. timestamp, fmt, ap);
  47. }
  48. static void sync_hexdump(const struct log_backend *const backend,
  49. struct log_msg_ids src_level, uint32_t timestamp,
  50. const char *metadata, const uint8_t *data,
  51. uint32_t length)
  52. {
  53. /* prevent adding CRLF, which may crash spinel decoding */
  54. uint32_t flag = LOG_OUTPUT_FLAG_CRLF_NONE;
  55. log_backend_std_sync_hexdump(&log_output_spinel, flag, src_level,
  56. timestamp, metadata, data, length);
  57. }
  58. static void log_backend_spinel_init(struct log_backend const *const backend)
  59. {
  60. memset(char_buf, '\0', sizeof(char_buf));
  61. }
  62. static void panic(struct log_backend const *const backend)
  63. {
  64. ARG_UNUSED(backend);
  65. panic_mode = true;
  66. }
  67. static void dropped(const struct log_backend *const backend, uint32_t cnt)
  68. {
  69. ARG_UNUSED(backend);
  70. log_backend_std_dropped(&log_output_spinel, cnt);
  71. }
  72. static int write(uint8_t *data, size_t length, void *ctx)
  73. {
  74. otLogLevel log_level;
  75. if (is_panic_mode()) {
  76. /* In panic mode otPlatLog implemented for Spinel protocol
  77. * cannot be used, because it cannot be called from interrupt.
  78. * In such situation raw data bytes without encoding are send.
  79. */
  80. platformUartPanic();
  81. otPlatUartSend(data, length);
  82. } else {
  83. switch (last_log_level) {
  84. case LOG_LEVEL_ERR:
  85. log_level = OT_LOG_LEVEL_CRIT;
  86. break;
  87. case LOG_LEVEL_WRN:
  88. log_level = OT_LOG_LEVEL_WARN;
  89. break;
  90. case LOG_LEVEL_INF:
  91. log_level = OT_LOG_LEVEL_INFO;
  92. break;
  93. case LOG_LEVEL_DBG:
  94. log_level = OT_LOG_LEVEL_DEBG;
  95. break;
  96. default:
  97. log_level = OT_LOG_LEVEL_NONE;
  98. break;
  99. }
  100. otPlatLog(log_level, OT_LOG_REGION_PLATFORM, "%s", data);
  101. }
  102. /* make sure that buffer will be clean in next attempt */
  103. memset(char_buf, '\0', length);
  104. return length;
  105. }
  106. const struct log_backend_api log_backend_spinel_api = {
  107. .process = IS_ENABLED(CONFIG_LOG2) ? process : NULL,
  108. .put = IS_ENABLED(CONFIG_LOG_MODE_DEFERRED) ? put : NULL,
  109. .put_sync_string = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ?
  110. sync_string : NULL,
  111. .put_sync_hexdump = IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE) ?
  112. sync_hexdump : NULL,
  113. .panic = panic,
  114. .init = log_backend_spinel_init,
  115. .dropped = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ? NULL : dropped,
  116. };
  117. LOG_BACKEND_DEFINE(log_backend_spinel, log_backend_spinel_api, true);