Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSG <who.just.the.doctor@gmail.com>2022-08-03 19:00:17 +0300
committerGitHub <noreply@github.com>2022-08-03 19:00:17 +0300
commitbc34689ed6e6a8c2c757be2da01984814468537a (patch)
tree887e00334926bd9b9168fb304adbf758b19a9fde /applications
parenteed4296890f7bb24c0b7f7627e9fcbcc695dd10a (diff)
Make printf great again (#1438)
* Printf lib: wrap *printf* functions * Printf lib, FW: drop sprintf. Dolphin: dump timestamp as is, wo asctime. * FW: remove sniprintf, wrap assert functions * Printf lib: wrap putc, puts, putchar * Printf: a working but not thread-safe concept. * Poorly wrap fflush * stdglue: buffers * Core: thread local buffers * Core: move stdglue to thread api, add ability to get FuriThread instance of current thread. * RPC tests: replace sprintf with snprintf * Applications: use new stdout api * Printf lib: wrap more printf-like and stdout functions * Documentation * Apps: snprintf size fixes Co-authored-by: あく <alleteam@gmail.com>
Diffstat (limited to 'applications')
-rw-r--r--applications/cli/cli.c10
-rwxr-xr-xapplications/cli/cli_i.h2
-rw-r--r--applications/cli/cli_vcp.c3
-rw-r--r--applications/debug_tools/keypad_test.c10
-rw-r--r--applications/desktop/views/desktop_view_debug.c9
-rw-r--r--applications/infrared/infrared_cli.c8
-rw-r--r--applications/rpc/rpc_storage.c2
-rw-r--r--applications/subghz/scenes/subghz_scene_receiver_config.c9
-rw-r--r--applications/unit_tests/rpc/rpc_test.c15
9 files changed, 36 insertions, 32 deletions
diff --git a/applications/cli/cli.c b/applications/cli/cli.c
index 4d9b8a5f..e554ac89 100644
--- a/applications/cli/cli.c
+++ b/applications/cli/cli.c
@@ -439,9 +439,9 @@ void cli_session_open(Cli* cli, void* session) {
cli->session = session;
if(cli->session != NULL) {
cli->session->init();
- furi_stdglue_set_thread_stdout_callback(cli->session->tx_stdout);
+ furi_thread_set_stdout_callback(cli->session->tx_stdout);
} else {
- furi_stdglue_set_thread_stdout_callback(NULL);
+ furi_thread_set_stdout_callback(NULL);
}
furi_semaphore_release(cli->idle_sem);
furi_check(furi_mutex_release(cli->mutex) == FuriStatusOk);
@@ -455,7 +455,7 @@ void cli_session_close(Cli* cli) {
cli->session->deinit();
}
cli->session = NULL;
- furi_stdglue_set_thread_stdout_callback(NULL);
+ furi_thread_set_stdout_callback(NULL);
furi_check(furi_mutex_release(cli->mutex) == FuriStatusOk);
}
@@ -469,9 +469,9 @@ int32_t cli_srv(void* p) {
furi_record_create(RECORD_CLI, cli);
if(cli->session != NULL) {
- furi_stdglue_set_thread_stdout_callback(cli->session->tx_stdout);
+ furi_thread_set_stdout_callback(cli->session->tx_stdout);
} else {
- furi_stdglue_set_thread_stdout_callback(NULL);
+ furi_thread_set_stdout_callback(NULL);
}
if(furi_hal_rtc_get_boot_mode() == FuriHalRtcBootModeNormal) {
diff --git a/applications/cli/cli_i.h b/applications/cli/cli_i.h
index 076dd75e..8f0bd85d 100755
--- a/applications/cli/cli_i.h
+++ b/applications/cli/cli_i.h
@@ -25,7 +25,7 @@ struct CliSession {
void (*deinit)(void);
size_t (*rx)(uint8_t* buffer, size_t size, uint32_t timeout);
void (*tx)(const uint8_t* buffer, size_t size);
- void (*tx_stdout)(void* _cookie, const char* data, size_t size);
+ void (*tx_stdout)(const char* data, size_t size);
bool (*is_connected)(void);
};
diff --git a/applications/cli/cli_vcp.c b/applications/cli/cli_vcp.c
index 5d66b8f8..5a8b44dc 100644
--- a/applications/cli/cli_vcp.c
+++ b/applications/cli/cli_vcp.c
@@ -277,8 +277,7 @@ static void cli_vcp_tx(const uint8_t* buffer, size_t size) {
#endif
}
-static void cli_vcp_tx_stdout(void* _cookie, const char* data, size_t size) {
- UNUSED(_cookie);
+static void cli_vcp_tx_stdout(const char* data, size_t size) {
cli_vcp_tx((const uint8_t*)data, size);
}
diff --git a/applications/debug_tools/keypad_test.c b/applications/debug_tools/keypad_test.c
index 6708c82b..2470baf8 100644
--- a/applications/debug_tools/keypad_test.c
+++ b/applications/debug_tools/keypad_test.c
@@ -26,11 +26,11 @@ static void keypad_test_render_callback(Canvas* canvas, void* ctx) {
canvas_clear(canvas);
char strings[5][20];
- sprintf(strings[0], "Ok: %d", state->ok);
- sprintf(strings[1], "L: %d", state->left);
- sprintf(strings[2], "R: %d", state->right);
- sprintf(strings[3], "U: %d", state->up);
- sprintf(strings[4], "D: %d", state->down);
+ snprintf(strings[0], 20, "Ok: %d", state->ok);
+ snprintf(strings[1], 20, "L: %d", state->left);
+ snprintf(strings[2], 20, "R: %d", state->right);
+ snprintf(strings[3], 20, "U: %d", state->up);
+ snprintf(strings[4], 20, "D: %d", state->down);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 0, 10, "Keypad test");
diff --git a/applications/desktop/views/desktop_view_debug.c b/applications/desktop/views/desktop_view_debug.c
index 68c054c2..b69a6a2d 100644
--- a/applications/desktop/views/desktop_view_debug.c
+++ b/applications/desktop/views/desktop_view_debug.c
@@ -78,7 +78,6 @@ void desktop_debug_render(Canvas* canvas, void* model) {
canvas_draw_str(canvas, 5, 50 + STATUS_BAR_Y_SHIFT, buffer);
} else {
- char buffer[64];
Dolphin* dolphin = furi_record_open(RECORD_DOLPHIN);
DolphinStats stats = dolphin_stats(dolphin);
furi_record_close(RECORD_DOLPHIN);
@@ -87,18 +86,20 @@ void desktop_debug_render(Canvas* canvas, void* model) {
uint32_t remaining = dolphin_state_xp_to_levelup(m->icounter);
canvas_set_font(canvas, FontSecondary);
- snprintf(buffer, 64, "Icounter: %ld Butthurt %ld", m->icounter, m->butthurt);
+ snprintf(buffer, sizeof(buffer), "Icounter: %ld Butthurt %ld", m->icounter, m->butthurt);
canvas_draw_str(canvas, 5, 19 + STATUS_BAR_Y_SHIFT, buffer);
snprintf(
buffer,
- 64,
+ sizeof(buffer),
"Level: %ld To level up: %ld",
current_lvl,
(remaining == (uint32_t)(-1) ? remaining : 0));
canvas_draw_str(canvas, 5, 29 + STATUS_BAR_Y_SHIFT, buffer);
- snprintf(buffer, 64, "%s", asctime(localtime((const time_t*)&m->timestamp)));
+ // even if timestamp is uint64_t, it's safe to cast it to uint32_t, because furi_hal_rtc_datetime_to_timestamp only returns uint32_t
+ snprintf(buffer, sizeof(buffer), "%ld", (uint32_t)m->timestamp);
+
canvas_draw_str(canvas, 5, 39 + STATUS_BAR_Y_SHIFT, buffer);
canvas_draw_str(canvas, 0, 49 + STATUS_BAR_Y_SHIFT, "[< >] icounter value [ok] save");
}
diff --git a/applications/infrared/infrared_cli.c b/applications/infrared/infrared_cli.c
index c190aad3..aae02e8f 100644
--- a/applications/infrared/infrared_cli.c
+++ b/applications/infrared/infrared_cli.c
@@ -27,7 +27,7 @@ static void signal_received_callback(void* context, InfraredWorkerSignal* receiv
if(infrared_worker_signal_is_decoded(received_signal)) {
const InfraredMessage* message = infrared_worker_get_decoded_signal(received_signal);
- buf_cnt = sniprintf(
+ buf_cnt = snprintf(
buf,
sizeof(buf),
"%s, A:0x%0*lX, C:0x%0*lX%s\r\n",
@@ -43,13 +43,13 @@ static void signal_received_callback(void* context, InfraredWorkerSignal* receiv
size_t timings_cnt;
infrared_worker_get_raw_signal(received_signal, &timings, &timings_cnt);
- buf_cnt = sniprintf(buf, sizeof(buf), "RAW, %d samples:\r\n", timings_cnt);
+ buf_cnt = snprintf(buf, sizeof(buf), "RAW, %d samples:\r\n", timings_cnt);
cli_write(cli, (uint8_t*)buf, buf_cnt);
for(size_t i = 0; i < timings_cnt; ++i) {
- buf_cnt = sniprintf(buf, sizeof(buf), "%lu ", timings[i]);
+ buf_cnt = snprintf(buf, sizeof(buf), "%lu ", timings[i]);
cli_write(cli, (uint8_t*)buf, buf_cnt);
}
- buf_cnt = sniprintf(buf, sizeof(buf), "\r\n");
+ buf_cnt = snprintf(buf, sizeof(buf), "\r\n");
cli_write(cli, (uint8_t*)buf, buf_cnt);
}
}
diff --git a/applications/rpc/rpc_storage.c b/applications/rpc/rpc_storage.c
index d2b43ff6..1e2920f5 100644
--- a/applications/rpc/rpc_storage.c
+++ b/applications/rpc/rpc_storage.c
@@ -541,7 +541,7 @@ static void rpc_system_storage_md5sum_process(const PB_Main* request, void* cont
(void)md5sum_size;
furi_assert(hash_size <= ((md5sum_size - 1) / 2));
for(uint8_t i = 0; i < hash_size; i++) {
- md5sum += sprintf(md5sum, "%02x", hash[i]);
+ md5sum += snprintf(md5sum, md5sum_size, "%02x", hash[i]);
}
free(hash);
diff --git a/applications/subghz/scenes/subghz_scene_receiver_config.c b/applications/subghz/scenes/subghz_scene_receiver_config.c
index 590b51d1..bf2f0cdb 100644
--- a/applications/subghz/scenes/subghz_scene_receiver_config.c
+++ b/applications/subghz/scenes/subghz_scene_receiver_config.c
@@ -73,8 +73,9 @@ static void subghz_scene_receiver_config_set_frequency(VariableItem* item) {
if(subghz->txrx->hopper_state == SubGhzHopperStateOFF) {
char text_buf[10] = {0};
- sprintf(
+ snprintf(
text_buf,
+ sizeof(text_buf),
"%lu.%02lu",
subghz_setting_get_frequency(subghz->setting, index) / 1000000,
(subghz_setting_get_frequency(subghz->setting, index) % 1000000) / 10000);
@@ -106,8 +107,9 @@ static void subghz_scene_receiver_config_set_hopping_runing(VariableItem* item)
variable_item_set_current_value_text(item, hopping_text[index]);
if(hopping_value[index] == SubGhzHopperStateOFF) {
char text_buf[10] = {0};
- sprintf(
+ snprintf(
text_buf,
+ sizeof(text_buf),
"%lu.%02lu",
subghz_setting_get_default_frequency(subghz->setting) / 1000000,
(subghz_setting_get_default_frequency(subghz->setting) % 1000000) / 10000);
@@ -160,8 +162,9 @@ void subghz_scene_receiver_config_on_enter(void* context) {
subghz->scene_manager, SubGhzSceneReceiverConfig, (uint32_t)item);
variable_item_set_current_value_index(item, value_index);
char text_buf[10] = {0};
- sprintf(
+ snprintf(
text_buf,
+ sizeof(text_buf),
"%lu.%02lu",
subghz_setting_get_frequency(subghz->setting, value_index) / 1000000,
(subghz_setting_get_frequency(subghz->setting, value_index) % 1000000) / 10000);
diff --git a/applications/unit_tests/rpc/rpc_test.c b/applications/unit_tests/rpc/rpc_test.c
index 69a0c434..d31311af 100644
--- a/applications/unit_tests/rpc/rpc_test.c
+++ b/applications/unit_tests/rpc/rpc_test.c
@@ -189,8 +189,9 @@ static void clean_directory(Storage* fs_api, const char* clean_dir) {
FileInfo fileinfo;
char* name = malloc(MAX_NAME_LENGTH + 1);
while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
- char* fullname = malloc(strlen(clean_dir) + strlen(name) + 1 + 1);
- sprintf(fullname, "%s/%s", clean_dir, name);
+ size_t size = strlen(clean_dir) + strlen(name) + 1 + 1;
+ char* fullname = malloc(size);
+ snprintf(fullname, size, "%s/%s", clean_dir, name);
if(fileinfo.flags & FSF_DIRECTORY) {
clean_directory(fs_api, fullname);
}
@@ -1226,7 +1227,7 @@ MU_TEST(test_storage_mkdir) {
mu_check(test_is_exists(TEST_DIR "dir2"));
}
-static void test_storage_calculate_md5sum(const char* path, char* md5sum) {
+static void test_storage_calculate_md5sum(const char* path, char* md5sum, size_t md5sum_size) {
Storage* api = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(api);
@@ -1247,7 +1248,7 @@ static void test_storage_calculate_md5sum(const char* path, char* md5sum) {
free(md5_ctx);
for(uint8_t i = 0; i < hash_size; i++) {
- md5sum += sprintf(md5sum, "%02x", hash[i]);
+ md5sum += snprintf(md5sum, md5sum_size, "%02x", hash[i]);
}
free(hash);
@@ -1299,9 +1300,9 @@ MU_TEST(test_storage_md5sum) {
test_create_file(TEST_DIR "file1.txt", 0);
test_create_file(TEST_DIR "file2.txt", 1);
test_create_file(TEST_DIR "file3.txt", 512);
- test_storage_calculate_md5sum(TEST_DIR "file1.txt", md5sum1);
- test_storage_calculate_md5sum(TEST_DIR "file2.txt", md5sum2);
- test_storage_calculate_md5sum(TEST_DIR "file3.txt", md5sum3);
+ test_storage_calculate_md5sum(TEST_DIR "file1.txt", md5sum1, MD5SUM_SIZE * 2 + 1);
+ test_storage_calculate_md5sum(TEST_DIR "file2.txt", md5sum2, MD5SUM_SIZE * 2 + 1);
+ test_storage_calculate_md5sum(TEST_DIR "file3.txt", md5sum3, MD5SUM_SIZE * 2 + 1);
test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);