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:
107
lib/print/SConscript
Normal file
107
lib/print/SConscript
Normal file
@@ -0,0 +1,107 @@
|
||||
Import("env")
|
||||
|
||||
wrapped_fn_list = [
|
||||
#
|
||||
# used by our firmware, so we provide their realizations
|
||||
#
|
||||
"fflush",
|
||||
"printf",
|
||||
"putc", # fallback from printf, thanks gcc
|
||||
"putchar", # storage cli
|
||||
"puts", # fallback from printf, thanks gcc
|
||||
"snprintf",
|
||||
"vsnprintf", # m-string
|
||||
"__assert", # ???
|
||||
"__assert_func", # ???
|
||||
#
|
||||
# wrap other functions to make sure they are not called
|
||||
# realization is not provided
|
||||
#
|
||||
"setbuf",
|
||||
"setvbuf",
|
||||
"fprintf",
|
||||
"vfprintf",
|
||||
"vprintf",
|
||||
"fputc",
|
||||
"fputs",
|
||||
"sprintf", # specially, because this function is dangerous
|
||||
"asprintf",
|
||||
"vasprintf",
|
||||
"asiprintf",
|
||||
"asniprintf",
|
||||
"asnprintf",
|
||||
"diprintf",
|
||||
"fiprintf",
|
||||
"iprintf",
|
||||
"siprintf",
|
||||
"sniprintf",
|
||||
"vasiprintf",
|
||||
"vasniprintf",
|
||||
"vasnprintf",
|
||||
"vdiprintf",
|
||||
"vfiprintf",
|
||||
"viprintf",
|
||||
"vsiprintf",
|
||||
"vsniprintf",
|
||||
#
|
||||
# Scanf is not implemented 4 now
|
||||
#
|
||||
# "fscanf",
|
||||
# "scanf",
|
||||
# "sscanf",
|
||||
# "vsprintf",
|
||||
# "fgetc",
|
||||
# "fgets",
|
||||
# "getc",
|
||||
# "getchar",
|
||||
# "gets",
|
||||
# "ungetc",
|
||||
# "vfscanf",
|
||||
# "vscanf",
|
||||
# "vsscanf",
|
||||
# "fiscanf",
|
||||
# "iscanf",
|
||||
# "siscanf",
|
||||
# "vfiscanf",
|
||||
# "viscanf",
|
||||
# "vsiscanf",
|
||||
#
|
||||
# File management
|
||||
#
|
||||
# "fclose",
|
||||
# "freopen",
|
||||
# "fread",
|
||||
# "fwrite",
|
||||
# "fgetpos",
|
||||
# "fseek",
|
||||
# "fsetpos",
|
||||
# "ftell",
|
||||
# "rewind",
|
||||
# "feof",
|
||||
# "ferror",
|
||||
# "fopen",
|
||||
# "remove",
|
||||
# "rename",
|
||||
# "fseeko",
|
||||
# "ftello",
|
||||
]
|
||||
|
||||
for wrapped_fn in wrapped_fn_list:
|
||||
env.Append(
|
||||
LINKFLAGS=[
|
||||
"-Wl,--wrap," + wrapped_fn,
|
||||
"-Wl,--wrap," + wrapped_fn + "_unlocked",
|
||||
"-Wl,--wrap,_" + wrapped_fn + "_r",
|
||||
"-Wl,--wrap,_" + wrapped_fn + "_unlocked_r",
|
||||
]
|
||||
)
|
||||
|
||||
libenv = env.Clone(FW_LIB_NAME="print")
|
||||
libenv.ApplyLibFlags()
|
||||
libenv.Append(CCFLAGS=["-Wno-double-promotion"])
|
||||
|
||||
sources = libenv.GlobRecursive("*.c*", ".")
|
||||
|
||||
lib = libenv.StaticLibrary("${FW_LIB_NAME}", sources)
|
||||
libenv.Install("${LIB_DIST_DIR}", lib)
|
||||
Return("lib")
|
1037
lib/print/printf_tiny.c
Normal file
1037
lib/print/printf_tiny.c
Normal file
File diff suppressed because it is too large
Load Diff
103
lib/print/printf_tiny.h
Normal file
103
lib/print/printf_tiny.h
Normal file
@@ -0,0 +1,103 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// \author (c) Marco Paland (info@paland.com)
|
||||
// 2014-2019, PALANDesign Hannover, Germany
|
||||
//
|
||||
// \license The MIT License (MIT)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on
|
||||
// embedded systems with a very limited resources.
|
||||
// Use this instead of bloated standard/newlib printf.
|
||||
// These routines are thread safe and reentrant.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _PRINTF_H_
|
||||
#define _PRINTF_H_
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Output a character to a custom device like UART, used by the printf() function
|
||||
* This function is declared here only. You have to write your custom implementation somewhere
|
||||
* \param character Character to output
|
||||
*/
|
||||
void _putchar(char character);
|
||||
|
||||
/**
|
||||
* Tiny printf implementation
|
||||
* You have to implement _putchar if you use printf()
|
||||
* To avoid conflicts with the regular printf() API it is overridden by macro defines
|
||||
* and internal underscore-appended functions like printf_() are used
|
||||
* \param format A string that specifies the format of the output
|
||||
* \return The number of characters that are written into the array, not counting the terminating null character
|
||||
*/
|
||||
int printf_(const char* format, ...);
|
||||
|
||||
/**
|
||||
* Tiny sprintf implementation
|
||||
* Due to security reasons (buffer overflow) YOU SHOULD CONSIDER USING (V)SNPRINTF INSTEAD!
|
||||
* \param buffer A pointer to the buffer where to store the formatted string. MUST be big enough to store the output!
|
||||
* \param format A string that specifies the format of the output
|
||||
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
|
||||
*/
|
||||
int sprintf_(char* buffer, const char* format, ...);
|
||||
|
||||
/**
|
||||
* Tiny snprintf/vsnprintf implementation
|
||||
* \param buffer A pointer to the buffer where to store the formatted string
|
||||
* \param count The maximum number of characters to store in the buffer, including a terminating null character
|
||||
* \param format A string that specifies the format of the output
|
||||
* \param va A value identifying a variable arguments list
|
||||
* \return The number of characters that COULD have been written into the buffer, not counting the terminating
|
||||
* null character. A value equal or larger than count indicates truncation. Only when the returned value
|
||||
* is non-negative and less than count, the string has been completely written.
|
||||
*/
|
||||
int snprintf_(char* buffer, size_t count, const char* format, ...);
|
||||
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);
|
||||
|
||||
/**
|
||||
* Tiny vprintf implementation
|
||||
* \param format A string that specifies the format of the output
|
||||
* \param va A value identifying a variable arguments list
|
||||
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
|
||||
*/
|
||||
int vprintf_(const char* format, va_list va);
|
||||
|
||||
/**
|
||||
* printf with output function
|
||||
* You may use this as dynamic alternative to printf() with its fixed _putchar() output
|
||||
* \param out An output function which takes one character and an argument pointer
|
||||
* \param arg An argument pointer for user data passed to output function
|
||||
* \param format A string that specifies the format of the output
|
||||
* \return The number of characters that are sent to the output function, not counting the terminating null character
|
||||
*/
|
||||
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // _PRINTF_H_
|
74
lib/print/wrappers.c
Normal file
74
lib/print/wrappers.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
#include <furi/core/check.h>
|
||||
#include <furi/core/thread.h>
|
||||
#include <furi/core/common_defines.h>
|
||||
#include <string.h>
|
||||
#include "printf_tiny.h"
|
||||
|
||||
void _putchar(char character) {
|
||||
furi_thread_stdout_write(&character, 1);
|
||||
}
|
||||
|
||||
int __wrap_printf(const char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
int ret = vprintf_(format, args);
|
||||
va_end(args);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int __wrap_vsnprintf(char* str, size_t size, const char* format, va_list args) {
|
||||
return vsnprintf_(str, size, format, args);
|
||||
}
|
||||
|
||||
int __wrap_puts(const char* str) {
|
||||
size_t size = furi_thread_stdout_write(str, strlen(str));
|
||||
size += furi_thread_stdout_write("\n", 1);
|
||||
return size;
|
||||
}
|
||||
|
||||
int __wrap_putchar(int ch) {
|
||||
size_t size = furi_thread_stdout_write((char*)&ch, 1);
|
||||
return size;
|
||||
}
|
||||
|
||||
int __wrap_putc(int ch, FILE* stream) {
|
||||
UNUSED(stream);
|
||||
size_t size = furi_thread_stdout_write((char*)&ch, 1);
|
||||
return size;
|
||||
}
|
||||
|
||||
int __wrap_snprintf(char* str, size_t size, const char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
int ret = __wrap_vsnprintf(str, size, format, args);
|
||||
va_end(args);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int __wrap_fflush(FILE* stream) {
|
||||
UNUSED(stream);
|
||||
furi_thread_stdout_flush();
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((__noreturn__)) void __wrap___assert(const char* file, int line, const char* e) {
|
||||
UNUSED(file);
|
||||
UNUSED(line);
|
||||
// TODO: message file and line number
|
||||
furi_crash(e);
|
||||
}
|
||||
|
||||
__attribute__((__noreturn__)) void
|
||||
__wrap___assert_func(const char* file, int line, const char* func, const char* e) {
|
||||
UNUSED(file);
|
||||
UNUSED(line);
|
||||
UNUSED(func);
|
||||
// TODO: message file and line number
|
||||
furi_crash(e);
|
||||
}
|
Reference in New Issue
Block a user