Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions subsys/data_logger/data_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ static int do_block_write(const struct device *dev, enum infuse_type type, void
if ((data->current_block >= data->physical_blocks) &&
((data->current_block % erase_blocks) == 0)) {
LOG_DBG("%s preparing block for write", dev->name);
/* Old data is no longer present as soon as we start erasing */
data->earliest_block += erase_blocks;
/* Do the erase */
rc = api->erase(dev, phy_block, erase_blocks);
if (rc < 0) {
/* Restore previous value of `earliest_block` */
data->earliest_block -= erase_blocks;
LOG_ERR("%s failed to prepare block (%d)", dev->name, rc);
goto release;
}
/* Old data is no longer present */
data->earliest_block += erase_blocks;
}

/* Add persistent block header if required */
Expand Down Expand Up @@ -409,10 +412,12 @@ int data_logger_block_write(const struct device *dev, enum infuse_type type, voi
return 0;
}

#ifndef CONFIG_ZTEST
/* Logging on the system workqueue can cause deadlocks and should be avoided */
if (k_current_get() == k_work_queue_thread_get(&k_sys_work_q)) {
LOG_WRN("%s logging on system workqueue", dev->name);
}
#endif /* CONFIG_ZTEST */

#ifdef CONFIG_DATA_LOGGER_OFFLOAD_WRITES
const struct data_logger_common_config *config = dev->config;
Expand Down
2 changes: 2 additions & 0 deletions subsys/data_logger/high_level/tdf_data_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,12 @@ int tdf_data_logger_log_core_dev(const struct device *dev, uint16_t tdf_id, uint
return -ENOTCONN;
}

#ifndef CONFIG_ZTEST
/* Logging on the system workqueue can cause deadlocks and should be avoided */
if (k_current_get() == k_work_queue_thread_get(&k_sys_work_q)) {
LOG_WRN("%s logging on system workqueue", dev->name);
}
#endif /* CONFIG_ZTEST */

k_sem_take(&data->lock, K_FOREVER);
rc = log_locked(dev, tdf_id, tdf_len, tdf_num, format, time, idx_period, mem);
Expand Down
2 changes: 1 addition & 1 deletion subsys/rpc/commands/data_logger_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static int do_read(struct common_state *state)
block_offset += tail;

if (net_buf_tailroom(data_buf) < sizeof(uint32_t)) {
/* Update sent data CRC (payload only, not the header )*/
/* Update sent data CRC (payload only, not the header) */
state->sent_crc = crc32_ieee_update(state->sent_crc,
data_buf->data + sizeof(*data),
data_buf->len - sizeof(*data));
Expand Down
45 changes: 45 additions & 0 deletions tests/subsys/data_logger/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,48 @@ ZTEST(data_logger_api, test_while_erase)
data_logger_block_read(logger, 0, 0, output_buffer, state.block_size));
zassert_equal(0, data->read.num_calls);

/* Unblock the erase worker */
k_sem_give(&erase_sem);
data->reset.block_until = NULL;
}

static void do_writes(struct k_work *work)
{
const struct device *logger = DEVICE_DT_GET(DT_NODELABEL(data_logger_shim));
struct data_logger_state state;
int rc = 0;

data_logger_get_state(logger, &state);

for (int i = 0; i < state.physical_blocks + 1; i++) {
rc = data_logger_block_write(logger, 0x10, input_buffer, state.block_size);
zassert_equal(0, rc);
}
}

ZTEST(data_logger_api, test_while_prepare)
{
const struct device *logger = DEVICE_DT_GET(DT_NODELABEL(data_logger_shim));
struct data_logger_shim_function_data *data = data_logger_backend_shim_data_pointer(logger);
struct data_logger_state state;
struct k_work erase_work;
struct k_sem erase_sem;

k_sem_init(&erase_sem, 0, 1);
k_work_init(&erase_work, do_writes);
data_logger_get_state(logger, &state);

/* Submit block write work */
data->erase.block_until = &erase_sem;
k_work_submit(&erase_work);
k_sleep(K_TICKS(100));

/* Writing should currently be blocked in the erase step */
data_logger_get_state(logger, &state);
zassert_equal(state.physical_blocks, state.current_block);
/* Earliest block should no longer be available, since we are actively erasing it */
zassert_not_equal(0, state.earliest_block);

/* Unblock the erase worker */
k_sem_give(&erase_sem);
}
Expand All @@ -325,12 +367,15 @@ ZTEST(data_logger_api, test_flush)
static void test_before(void *ignored)
{
const struct device *logger = DEVICE_DT_GET(DT_NODELABEL(data_logger_shim));
struct data_logger_shim_function_data *data = data_logger_backend_shim_data_pointer(logger);

k_sleep(K_TICKS(1));
logger_shim_init(logger);
logger_shim_change_size(logger, 512);
(void)k_sem_take(&write_fail, K_NO_WAIT);
write_fail_count = 0;
data->erase.block_until = NULL;
data->reset.block_until = NULL;
}

ZTEST_SUITE(data_logger_api, NULL, NULL, test_before, NULL, NULL);