Merge branch 'feat/nfc-type-4-final' into mntm-dev

This commit is contained in:
WillyJL
2025-10-05 23:24:30 +02:00
242 changed files with 10348 additions and 697 deletions

View File

@@ -29,6 +29,7 @@ env.Append(
File("float_tools.h"),
File("value_index.h"),
File("tar/tar_archive.h"),
File("str_buffer.h"),
File("stream/stream.h"),
File("stream/file_stream.h"),
File("stream/string_stream.h"),

View File

@@ -2,6 +2,7 @@
#include "hex.h"
#include "strint.h"
#include "m-core.h"
#include <errno.h>
size_t args_get_first_word_length(FuriString* args) {
size_t ws = furi_string_search_char(args, ' ');
@@ -34,6 +35,24 @@ bool args_read_int_and_trim(FuriString* args, int* value) {
return false;
}
bool args_read_float_and_trim(FuriString* args, float* value) {
size_t cmd_length = args_get_first_word_length(args);
if(cmd_length == 0) {
return false;
}
char* end_ptr;
float temp = strtof(furi_string_get_cstr(args), &end_ptr);
if(end_ptr == furi_string_get_cstr(args)) {
return false;
}
*value = temp;
furi_string_right(args, cmd_length);
furi_string_trim(args);
return true;
}
bool args_read_string_and_trim(FuriString* args, FuriString* word) {
size_t cmd_length = args_get_first_word_length(args);
@@ -97,3 +116,38 @@ bool args_read_hex_bytes(FuriString* args, uint8_t* bytes, size_t bytes_count) {
return result;
}
bool args_read_duration(FuriString* args, uint32_t* value, const char* default_unit) {
const char* args_cstr = furi_string_get_cstr(args);
const char* unit;
errno = 0;
double duration_ms = strtod(args_cstr, (char**)&unit);
if(errno) return false;
if(duration_ms < 0) return false;
if(unit == args_cstr) return false;
if(strcmp(unit, "") == 0) {
unit = default_unit;
if(!unit) unit = "ms";
}
uint32_t multiplier;
if(strcasecmp(unit, "ms") == 0) {
multiplier = 1;
} else if(strcasecmp(unit, "s") == 0) {
multiplier = 1000;
} else if(strcasecmp(unit, "m") == 0) {
multiplier = 60 * 1000;
} else if(strcasecmp(unit, "h") == 0) {
multiplier = 60 * 60 * 1000;
} else {
return false;
}
const uint32_t max_pre_multiplication = UINT32_MAX / multiplier;
if(duration_ms > max_pre_multiplication) return false;
*value = round(duration_ms * multiplier);
return true;
}

View File

@@ -9,17 +9,26 @@ extern "C" {
#endif
/** Extract int value and trim arguments string
*
* @param args - arguments string
* @param word first argument, output
*
* @param args - arguments string
* @param value first argument, output
* @return true - success
* @return false - arguments string does not contain int
*/
bool args_read_int_and_trim(FuriString* args, int* value);
/** Extract float value and trim arguments string
*
* @param [in, out] args arguments string
* @param [out] value first argument
* @return true - success
* @return false - arguments string does not contain float
*/
bool args_read_float_and_trim(FuriString* args, float* value);
/**
* @brief Extract first argument from arguments string and trim arguments string
*
*
* @param args arguments string
* @param word first argument, output
* @return true - success
@@ -29,7 +38,7 @@ bool args_read_string_and_trim(FuriString* args, FuriString* word);
/**
* @brief Extract the first quoted argument from the argument string and trim the argument string. If the argument is not quoted, calls args_read_string_and_trim.
*
*
* @param args arguments string
* @param word first argument, output, without quotes
* @return true - success
@@ -39,7 +48,7 @@ bool args_read_probably_quoted_string_and_trim(FuriString* args, FuriString* wor
/**
* @brief Convert hex ASCII values to byte array
*
*
* @param args arguments string
* @param bytes byte array pointer, output
* @param bytes_count needed bytes count
@@ -48,11 +57,23 @@ bool args_read_probably_quoted_string_and_trim(FuriString* args, FuriString* wor
*/
bool args_read_hex_bytes(FuriString* args, uint8_t* bytes, size_t bytes_count);
/**
* @brief Parses a duration value from a given string and converts it to milliseconds
*
* @param [in] args the input string containing the duration value. The string may include units (e.g., "10s", "0.5m").
* @param [out] value pointer to store the parsed value in milliseconds
* @param [in] default_unit A default unit to be used if the input string does not contain a valid suffix.
* Supported units: `"ms"`, `"s"`, `"m"`, `"h"`
* If NULL, the function will assume milliseconds by default.
* @return `true` if the parsing and conversion succeeded, `false` otherwise.
*/
bool args_read_duration(FuriString* args, uint32_t* value, const char* default_unit);
/************************************ HELPERS ***************************************/
/**
* @brief Get length of first word from arguments string
*
*
* @param args arguments string
* @return size_t length of first word
*/
@@ -60,7 +81,7 @@ size_t args_get_first_word_length(FuriString* args);
/**
* @brief Get length of arguments string
*
*
* @param args arguments string
* @return size_t length of arguments string
*/
@@ -68,7 +89,7 @@ size_t args_length(FuriString* args);
/**
* @brief Convert ASCII hex values to byte
*
*
* @param hi_nibble ASCII hi nibble character
* @param low_nibble ASCII low nibble character
* @param byte byte pointer, output

View File

@@ -15,3 +15,18 @@ void cli_print_usage(const char* cmd, const char* usage, const char* arg) {
printf("%s: illegal option -- %s\r\nusage: %s %s", cmd, arg, cmd, usage);
}
bool cli_sleep(PipeSide* side, uint32_t duration_in_ms) {
uint32_t passed_time = 0;
bool is_interrupted = false;
do {
uint32_t left_time = duration_in_ms - passed_time;
uint32_t check_interval = left_time >= 100 ? 100 : left_time;
furi_delay_ms(check_interval);
passed_time += check_interval;
is_interrupted = cli_is_pipe_broken_or_is_etx_next_char(side);
} while(!is_interrupted && passed_time < duration_in_ms);
return !is_interrupted;
}

View File

@@ -29,14 +29,14 @@ typedef enum {
CliCommandFlagExternal = (1 << 4), /**< The command comes from a .fal file */
} CliCommandFlag;
/**
/**
* @brief CLI command execution callback pointer
*
*
* This callback will be called from a separate thread spawned just for your
* command. The pipe will be installed as the thread's stdio, so you can use
* `printf`, `getchar` and other standard functions to communicate with the
* user.
*
*
* @param [in] pipe Pipe that can be used to send and receive data. If
* `CliCommandFlagDontAttachStdio` was not set, you can
* also use standard C functions (printf, getc, etc.) to
@@ -64,7 +64,7 @@ typedef struct {
/**
* @brief Detects if Ctrl+C has been pressed or session has been terminated
*
*
* @param [in] side Pointer to pipe side given to the command thread
* @warning This function also assumes that the pipe is installed as the
* thread's stdio
@@ -80,6 +80,21 @@ bool cli_is_pipe_broken_or_is_etx_next_char(PipeSide* side);
*/
void cli_print_usage(const char* cmd, const char* usage, const char* arg);
/**
* @brief Pause for a specified duration or until Ctrl+C is pressed or the
* session is terminated.
*
* @param [in] side Pointer to pipe side given to the command thread.
* @param [in] duration_in_ms Duration of sleep in milliseconds.
* @return `true` if the sleep completed without interruption.
* @return `false` if interrupted.
*
* @warning This function also assumes that the pipe is installed as the
* thread's stdio.
* @warning This function will consume 0 or 1 bytes from the pipe.
*/
bool cli_sleep(PipeSide* side, uint32_t duration_in_ms);
#define CLI_COMMAND_INTERFACE(name, execute_callback, flags, stack_depth, app_id) \
static const CliCommandDescriptor cli_##name##_desc = { \
#name, \

View File

@@ -488,7 +488,6 @@ static int32_t cli_shell_thread(void* context) {
// ==========
// Public API
// ==========
CliShell* cli_shell_alloc(
CliShellMotd motd,
void* context,

View File

@@ -1,6 +1,6 @@
#include "cli_shell_completions.h"
ARRAY_DEF(CommandCompletions, FuriString*, FURI_STRING_OPLIST); // -V524
ARRAY_DEF(CommandCompletions, FuriString*, FURI_STRING_OPLIST); // -V524 //-V658
#define M_OPL_CommandCompletions_t() ARRAY_OPLIST(CommandCompletions)
struct CliShellCompletions {

18
lib/toolbox/str_buffer.c Normal file
View File

@@ -0,0 +1,18 @@
#include "str_buffer.h"
const char* str_buffer_make_owned_clone(StrBuffer* buffer, const char* str) {
char* owned = strdup(str);
buffer->n_owned_strings++;
buffer->owned_strings =
realloc(buffer->owned_strings, buffer->n_owned_strings * sizeof(const char*)); // -V701
buffer->owned_strings[buffer->n_owned_strings - 1] = owned;
return owned;
}
void str_buffer_clear_all_clones(StrBuffer* buffer) {
for(size_t i = 0; i < buffer->n_owned_strings; i++) {
free(buffer->owned_strings[i]);
}
free(buffer->owned_strings);
buffer->owned_strings = NULL;
}

47
lib/toolbox/str_buffer.h Normal file
View File

@@ -0,0 +1,47 @@
/**
* @file str_buffer.h
*
* Allows you to create an owned clone of however many strings that you need,
* then free all of them at once. Essentially the simplest possible append-only
* unindexable array of owned C-style strings.
*/
#pragma once
#include <furi.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief StrBuffer instance
*
* Place this struct directly wherever you want, it doesn't have to be `alloc`ed
* and `free`d.
*/
typedef struct {
char** owned_strings;
size_t n_owned_strings;
} StrBuffer;
/**
* @brief Makes a owned duplicate of the provided string
*
* @param[in] buffer StrBuffer instance
* @param[in] str Input C-style string
*
* @returns C-style string that contains to be valid event after `str` becomes
* invalid
*/
const char* str_buffer_make_owned_clone(StrBuffer* buffer, const char* str);
/**
* @brief Clears all owned duplicates
*
* @param[in] buffer StrBuffer instance
*/
void str_buffer_clear_all_clones(StrBuffer* buffer);
#ifdef __cplusplus
}
#endif

View File

@@ -80,7 +80,7 @@ StrintParseError strint_to_uint64_internal(
if(result > mul_limit) return StrintParseOverflowError;
result *= base;
if(result > limit - digit_value) return StrintParseOverflowError;
if(result > limit - digit_value) return StrintParseOverflowError; //-V658
result += digit_value;
read_total++;