|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include "log_backend_rpc_history.h" |
| 8 | + |
| 9 | +#include <zephyr/fs/fcb.h> |
| 10 | +#include <zephyr/sys/util.h> |
| 11 | + |
| 12 | +#define LOG_HISTORY_MAGIC 0x7d2ac863 |
| 13 | +#define LOG_HISTORY_AREA FIXED_PARTITION_ID(log_history) |
| 14 | + |
| 15 | +static struct fcb fcb; |
| 16 | +static struct flash_sector fcb_sectors[CONFIG_LOG_BACKEND_RPC_HISTORY_STORAGE_FCB_NUM_SECTORS]; |
| 17 | +static struct fcb_entry last_popped; |
| 18 | +static bool erase_oldest; |
| 19 | +static K_MUTEX_DEFINE(fcb_lock); |
| 20 | + |
| 21 | +void log_rpc_history_init(void) |
| 22 | +{ |
| 23 | + int rc; |
| 24 | + uint32_t sector_cnt = ARRAY_SIZE(fcb_sectors); |
| 25 | + |
| 26 | + rc = flash_area_get_sectors(LOG_HISTORY_AREA, §or_cnt, fcb_sectors); |
| 27 | + |
| 28 | + if (rc) { |
| 29 | + goto out; |
| 30 | + } |
| 31 | + |
| 32 | + fcb.f_magic = LOG_HISTORY_MAGIC; |
| 33 | + fcb.f_sectors = fcb_sectors; |
| 34 | + fcb.f_sector_cnt = (uint8_t)sector_cnt; |
| 35 | + erase_oldest = true; |
| 36 | + |
| 37 | + rc = fcb_init(LOG_HISTORY_AREA, &fcb); |
| 38 | + |
| 39 | + if (rc) { |
| 40 | + goto out; |
| 41 | + } |
| 42 | + |
| 43 | + rc = fcb_clear(&fcb); |
| 44 | + |
| 45 | +out: |
| 46 | + __ASSERT_NO_MSG(rc == 0); |
| 47 | +} |
| 48 | + |
| 49 | +void log_rpc_history_push(const union log_msg_generic *msg) |
| 50 | +{ |
| 51 | + int rc; |
| 52 | + size_t len; |
| 53 | + struct fcb_entry entry; |
| 54 | + |
| 55 | + len = log_msg_generic_get_wlen(&msg->buf) * sizeof(uint32_t); |
| 56 | + |
| 57 | + k_mutex_lock(&fcb_lock, K_FOREVER); |
| 58 | + rc = fcb_append(&fcb, len, &entry); |
| 59 | + |
| 60 | + if (rc == -ENOSPC && erase_oldest) { |
| 61 | + /* |
| 62 | + * The log history FCB is full but overwriting is enabled. |
| 63 | + * Erase the oldest FCB page and try again. |
| 64 | + * Note that the "last popped" location is cleared so that the next log transfer |
| 65 | + * starts from the updated oldest log message. |
| 66 | + */ |
| 67 | + rc = fcb_rotate(&fcb); |
| 68 | + |
| 69 | + if (rc) { |
| 70 | + goto out; |
| 71 | + } |
| 72 | + |
| 73 | + memset(&last_popped, 0, sizeof(last_popped)); |
| 74 | + rc = fcb_append(&fcb, len, &entry); |
| 75 | + } |
| 76 | + |
| 77 | + if (rc) { |
| 78 | + goto out; |
| 79 | + } |
| 80 | + |
| 81 | + rc = flash_area_write(fcb.fap, FCB_ENTRY_FA_DATA_OFF(entry), msg, len); |
| 82 | + |
| 83 | + if (rc) { |
| 84 | + goto out; |
| 85 | + } |
| 86 | + |
| 87 | + rc = fcb_append_finish(&fcb, &entry); |
| 88 | + |
| 89 | +out: |
| 90 | + k_mutex_unlock(&fcb_lock); |
| 91 | + |
| 92 | +#ifdef LOG_HISTORY_DEBUG |
| 93 | + __ASSERT_NO_MSG(rc == 0); |
| 94 | +#endif |
| 95 | +} |
| 96 | + |
| 97 | +void log_rpc_history_set_overwriting(bool overwriting) |
| 98 | +{ |
| 99 | + k_mutex_lock(&fcb_lock, K_FOREVER); |
| 100 | + |
| 101 | + erase_oldest = overwriting; |
| 102 | + |
| 103 | + k_mutex_unlock(&fcb_lock); |
| 104 | +} |
| 105 | + |
| 106 | +union log_msg_generic *log_rpc_history_pop(void) |
| 107 | +{ |
| 108 | + int rc; |
| 109 | + struct fcb_entry entry = last_popped; |
| 110 | + union log_msg_generic *msg = NULL; |
| 111 | + |
| 112 | + k_mutex_lock(&fcb_lock, K_FOREVER); |
| 113 | + rc = fcb_getnext(&fcb, &entry); |
| 114 | + |
| 115 | + if (rc) { |
| 116 | + rc = 0; |
| 117 | + goto out; |
| 118 | + } |
| 119 | + |
| 120 | + msg = (union log_msg_generic *)k_malloc(entry.fe_data_len); |
| 121 | + |
| 122 | + if (!msg) { |
| 123 | + goto out; |
| 124 | + } |
| 125 | + |
| 126 | + rc = flash_area_read(fcb.fap, FCB_ENTRY_FA_DATA_OFF(entry), msg, entry.fe_data_len); |
| 127 | + |
| 128 | + if (rc) { |
| 129 | + goto out; |
| 130 | + } |
| 131 | + |
| 132 | + last_popped = entry; |
| 133 | + |
| 134 | + while (fcb.f_oldest != last_popped.fe_sector) { |
| 135 | + rc = fcb_rotate(&fcb); |
| 136 | + |
| 137 | + if (rc) { |
| 138 | + goto out; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | +out: |
| 143 | + k_mutex_unlock(&fcb_lock); |
| 144 | + |
| 145 | +#ifdef LOG_HISTORY_DEBUG |
| 146 | + __ASSERT_NO_MSG(rc == 0); |
| 147 | +#endif |
| 148 | + |
| 149 | + return msg; |
| 150 | +} |
| 151 | + |
| 152 | +void log_rpc_history_free(const union log_msg_generic *msg) |
| 153 | +{ |
| 154 | + k_free((void *)msg); |
| 155 | +} |
0 commit comments