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>
This commit is contained in:
SG
2022-08-04 02:00:17 +10:00
committed by GitHub
parent eed4296890
commit bc34689ed6
22 changed files with 1484 additions and 175 deletions

View File

@@ -1,102 +0,0 @@
#include "stdglue.h"
#include "check.h"
#include "memmgr.h"
#include <FreeRTOS.h>
#include <task.h>
#include <furi_hal.h>
#include <m-dict.h>
DICT_DEF2(
FuriStdglueCallbackDict,
uint32_t,
M_DEFAULT_OPLIST,
FuriStdglueWriteCallback,
M_PTR_OPLIST)
typedef struct {
FuriMutex* mutex;
FuriStdglueCallbackDict_t thread_outputs;
} FuriStdglue;
static FuriStdglue* furi_stdglue = NULL;
static ssize_t stdout_write(void* _cookie, const char* data, size_t size) {
furi_assert(furi_stdglue);
bool consumed = false;
FuriThreadId task_id = furi_thread_get_current_id();
if(xTaskGetSchedulerState() == taskSCHEDULER_RUNNING && task_id &&
furi_mutex_acquire(furi_stdglue->mutex, FuriWaitForever) == FuriStatusOk) {
// We are in the thread context
// Handle thread callbacks
FuriStdglueWriteCallback* callback_ptr =
FuriStdglueCallbackDict_get(furi_stdglue->thread_outputs, (uint32_t)task_id);
if(callback_ptr) {
(*callback_ptr)(_cookie, data, size);
consumed = true;
}
furi_check(furi_mutex_release(furi_stdglue->mutex) == FuriStatusOk);
}
// Flush
if(data == 0) {
/*
* This means that we should flush internal buffers. Since we
* don't we just return. (Remember, "handle" == -1 means that all
* handles should be flushed.)
*/
return 0;
}
// Debug uart
if(!consumed) furi_hal_console_tx((const uint8_t*)data, size);
// All data consumed
return size;
}
void furi_stdglue_init() {
furi_stdglue = malloc(sizeof(FuriStdglue));
// Init outputs structures
furi_stdglue->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
furi_check(furi_stdglue->mutex);
FuriStdglueCallbackDict_init(furi_stdglue->thread_outputs);
// Prepare and set stdout descriptor
FILE* fp = fopencookie(
NULL,
"w",
(cookie_io_functions_t){
.read = NULL,
.write = stdout_write,
.seek = NULL,
.close = NULL,
});
setvbuf(fp, NULL, _IOLBF, 0);
stdout = fp;
}
bool furi_stdglue_set_thread_stdout_callback(FuriStdglueWriteCallback callback) {
furi_assert(furi_stdglue);
FuriThreadId task_id = furi_thread_get_current_id();
if(task_id) {
furi_check(furi_mutex_acquire(furi_stdglue->mutex, FuriWaitForever) == FuriStatusOk);
if(callback) {
FuriStdglueCallbackDict_set_at(
furi_stdglue->thread_outputs, (uint32_t)task_id, callback);
} else {
FuriStdglueCallbackDict_erase(furi_stdglue->thread_outputs, (uint32_t)task_id);
}
furi_check(furi_mutex_release(furi_stdglue->mutex) == FuriStatusOk);
return true;
} else {
return false;
}
}
void __malloc_lock(struct _reent* REENT) {
UNUSED(REENT);
vTaskSuspendAll();
}
void __malloc_unlock(struct _reent* REENT) {
UNUSED(REENT);
xTaskResumeAll();
}

View File

@@ -1,36 +0,0 @@
/**
* @file stdglue.h
* Furi: stdlibc glue
*/
#pragma once
#include <stdbool.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
/** Write callback
* @param _cookie pointer to cookie (see stdio gnu extension)
* @param data pointer to data
* @param size data size @warnign your handler must consume everything
*/
typedef void (*FuriStdglueWriteCallback)(void* _cookie, const char* data, size_t size);
/** Initialized std library glue code */
void furi_stdglue_init();
/** Set STDOUT callback for your thread
*
* @param callback callback or NULL to clear
*
* @return true on success, otherwise fail
* @warning function is thread aware, use this API from the same thread
*/
bool furi_stdglue_set_thread_stdout_callback(FuriStdglueWriteCallback callback);
#ifdef __cplusplus
}
#endif

View File

@@ -4,12 +4,21 @@
#include "memmgr_heap.h"
#include "check.h"
#include "common_defines.h"
#include "mutex.h"
#include <task.h>
#include <m-string.h>
#include <furi_hal_console.h>
#define THREAD_NOTIFY_INDEX 1 // Index 0 is used for stream buffers
typedef struct FuriThreadStdout FuriThreadStdout;
struct FuriThreadStdout {
FuriThreadStdoutWriteCallback write_callback;
string_t buffer;
};
struct FuriThread {
FuriThreadState state;
int32_t ret;
@@ -27,8 +36,13 @@ struct FuriThread {
TaskHandle_t task_handle;
bool heap_trace_enabled;
size_t heap_size;
FuriThreadStdout output;
};
static size_t __furi_thread_stdout_write(FuriThread* thread, const char* data, size_t size);
static int32_t __furi_thread_stdout_flush(FuriThread* thread);
/** Catch threads that are trying to exit wrong way */
__attribute__((__noreturn__)) void furi_thread_catch() {
asm volatile("nop"); // extra magic
@@ -47,6 +61,10 @@ static void furi_thread_body(void* context) {
furi_assert(context);
FuriThread* thread = context;
// store thread instance to thread local storage
furi_assert(pvTaskGetThreadLocalStoragePointer(NULL, 0) == NULL);
vTaskSetThreadLocalStoragePointer(NULL, 0, thread);
furi_assert(thread->state == FuriThreadStateStarting);
furi_thread_set_state(thread, FuriThreadStateRunning);
@@ -66,12 +84,18 @@ static void furi_thread_body(void* context) {
furi_assert(thread->state == FuriThreadStateRunning);
furi_thread_set_state(thread, FuriThreadStateStopped);
// clear thread local storage
__furi_thread_stdout_flush(thread);
furi_assert(pvTaskGetThreadLocalStoragePointer(NULL, 0) != NULL);
vTaskSetThreadLocalStoragePointer(NULL, 0, NULL);
vTaskDelete(thread->task_handle);
furi_thread_catch();
}
FuriThread* furi_thread_alloc() {
FuriThread* thread = malloc(sizeof(FuriThread));
string_init(thread->output.buffer);
return thread;
}
@@ -81,6 +105,8 @@ void furi_thread_free(FuriThread* thread) {
furi_assert(thread->state == FuriThreadStateStopped);
if(thread->name) free((void*)thread->name);
string_clear(thread->output.buffer);
free(thread);
}
@@ -199,6 +225,12 @@ FuriThreadId furi_thread_get_current_id() {
return xTaskGetCurrentTaskHandle();
}
FuriThread* furi_thread_get_current() {
FuriThread* thread = pvTaskGetThreadLocalStoragePointer(NULL, 0);
furi_assert(thread != NULL);
return thread;
}
void furi_thread_yield() {
furi_assert(!FURI_IS_IRQ_MODE());
taskYIELD();
@@ -408,3 +440,59 @@ uint32_t furi_thread_get_stack_space(FuriThreadId thread_id) {
return (sz);
}
static size_t __furi_thread_stdout_write(FuriThread* thread, const char* data, size_t size) {
if(thread->output.write_callback != NULL) {
thread->output.write_callback(data, size);
} else {
furi_hal_console_tx((const uint8_t*)data, size);
}
return size;
}
static int32_t __furi_thread_stdout_flush(FuriThread* thread) {
string_ptr buffer = thread->output.buffer;
size_t size = string_size(buffer);
if(size > 0) {
__furi_thread_stdout_write(thread, string_get_cstr(buffer), size);
string_reset(buffer);
}
return 0;
}
bool furi_thread_set_stdout_callback(FuriThreadStdoutWriteCallback callback) {
FuriThread* thread = furi_thread_get_current();
__furi_thread_stdout_flush(thread);
thread->output.write_callback = callback;
return true;
}
size_t furi_thread_stdout_write(const char* data, size_t size) {
FuriThread* thread = furi_thread_get_current();
if(size == 0 || data == NULL) {
return __furi_thread_stdout_flush(thread);
} else {
if(data[size - 1] == '\n') {
// if the last character is a newline, we can flush buffer and write data as is, wo buffers
__furi_thread_stdout_flush(thread);
__furi_thread_stdout_write(thread, data, size);
} else {
// string_cat doesn't work here because we need to write the exact size data
for(size_t i = 0; i < size; i++) {
string_push_back(thread->output.buffer, data[i]);
if(data[i] == '\n') {
__furi_thread_stdout_flush(thread);
}
}
}
}
return size;
}
int32_t furi_thread_stdout_flush() {
return __furi_thread_stdout_flush(furi_thread_get_current());
}

View File

@@ -42,6 +42,12 @@ typedef void* FuriThreadId;
*/
typedef int32_t (*FuriThreadCallback)(void* context);
/** Write to stdout callback
* @param data pointer to data
* @param size data size @warning your handler must consume everything
*/
typedef void (*FuriThreadStdoutWriteCallback)(const char* data, size_t size);
/** FuriThread state change calback called upon thread state change
* @param state new thread state
* @param context callback context
@@ -177,6 +183,12 @@ int32_t furi_thread_get_return_code(FuriThread* thread);
*/
FuriThreadId furi_thread_get_current_id();
/** Get FuriThread instance for current thread
*
* @return FuriThread*
*/
FuriThread* furi_thread_get_current();
/** Return control to scheduler */
void furi_thread_yield();
@@ -194,6 +206,29 @@ const char* furi_thread_get_name(FuriThreadId thread_id);
uint32_t furi_thread_get_stack_space(FuriThreadId thread_id);
/** Set STDOUT callback for thread
*
* @param callback callback or NULL to clear
*
* @return true on success, otherwise fail
*/
bool furi_thread_set_stdout_callback(FuriThreadStdoutWriteCallback callback);
/** Write data to buffered STDOUT
*
* @param data input data
* @param size input data size
*
* @return size_t written data size
*/
size_t furi_thread_stdout_write(const char* data, size_t size);
/** Flush data to STDOUT
*
* @return int32_t error code
*/
int32_t furi_thread_stdout_flush();
#ifdef __cplusplus
}
#endif