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