[FL-1542], [FL-1603] Generate random name (#602)

* Lib: add random name library
* Text_input: add default text clearing
* All: rework all applications with new text_input API
* Nfc: fix removing dev file on save_name scene enter
* Lib: move all free radicals to toolbox and update API usage.

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
gornekich
2021-07-25 14:34:54 +03:00
committed by GitHub
parent 0c7a8edf51
commit ebd158a823
39 changed files with 142 additions and 132 deletions

79
lib/toolbox/args.c Normal file
View File

@@ -0,0 +1,79 @@
#include "args.h"
#include "hex.h"
size_t args_get_first_word_length(string_t args) {
size_t ws = string_search_char(args, ' ');
if(ws == STRING_FAILURE) {
ws = string_size(args);
}
return ws;
}
size_t args_length(string_t args) {
return string_size(args);
}
bool args_read_string_and_trim(string_t args, string_t word) {
size_t cmd_length = args_get_first_word_length(args);
if(cmd_length == 0) {
return false;
}
string_set_n(word, args, 0, cmd_length);
string_right(args, cmd_length);
string_strim(args);
return true;
}
bool args_read_probably_quoted_string_and_trim(string_t args, string_t word) {
if(string_size(args) > 1 && string_get_char(args, 0) == '\"') {
size_t second_quote_pos = string_search_char(args, '\"', 1);
if(second_quote_pos == 0) {
return false;
}
string_set_n(word, args, 1, second_quote_pos - 1);
string_right(args, second_quote_pos + 1);
string_strim(args);
return true;
} else {
return args_read_string_and_trim(args, word);
}
}
bool args_char_to_hex(char hi_nibble, char low_nibble, uint8_t* byte) {
uint8_t hi_nibble_value = 0;
uint8_t low_nibble_value = 0;
bool result = false;
if(hex_char_to_hex_nibble(hi_nibble, &hi_nibble_value)) {
if(hex_char_to_hex_nibble(low_nibble, &low_nibble_value)) {
result = true;
*byte = (hi_nibble_value << 4) | low_nibble_value;
}
}
return result;
}
bool args_read_hex_bytes(string_t args, uint8_t* bytes, uint8_t bytes_count) {
bool result = true;
const char* str_pointer = string_get_cstr(args);
if(args_get_first_word_length(args) == (bytes_count * 2)) {
for(uint8_t i = 0; i < bytes_count; i++) {
if(!args_char_to_hex(str_pointer[i * 2], str_pointer[i * 2 + 1], &(bytes[i]))) {
result = false;
break;
}
}
} else {
result = false;
}
return result;
}

71
lib/toolbox/args.h Normal file
View File

@@ -0,0 +1,71 @@
#pragma once
#include "m-string.h"
#include "stdint.h"
#include "stdbool.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Extract first argument from arguments string and trim arguments string
*
* @param args arguments string
* @param word first argument, output
* @return true - success
* @return false - arguments string does not contain anything
*/
bool args_read_string_and_trim(string_t args, string_t 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
* @return false - arguments string does not contain anything
*/
bool args_read_probably_quoted_string_and_trim(string_t args, string_t word);
/**
* @brief Convert hex ASCII values to byte array
*
* @param args arguments string
* @param bytes byte array pointer, output
* @param bytes_count needed bytes count
* @return true - success
* @return false - arguments string does not contain enough values, or contain non-hex ASCII values
*/
bool args_read_hex_bytes(string_t args, uint8_t* bytes, uint8_t bytes_count);
/************************************ HELPERS ***************************************/
/**
* @brief Get length of first word from arguments string
*
* @param args arguments string
* @return size_t length of first word
*/
size_t args_get_first_word_length(string_t args);
/**
* @brief Get length of arguments string
*
* @param args arguments string
* @return size_t length of arguments string
*/
size_t args_length(string_t 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
* @return bool conversion status
*/
bool args_char_to_hex(char hi_nibble, char low_nibble, uint8_t* byte);
#ifdef __cplusplus
}
#endif

16
lib/toolbox/hex.c Normal file
View File

@@ -0,0 +1,16 @@
#include "hex.h"
bool hex_char_to_hex_nibble(char c, uint8_t* nibble) {
if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) {
if((c >= '0' && c <= '9')) {
*nibble = c - '0';
} else if((c >= 'A' && c <= 'F')) {
*nibble = c - 'A' + 10;
} else {
*nibble = c - 'a' + 10;
}
return true;
} else {
return false;
}
}

20
lib/toolbox/hex.h Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include "stdint.h"
#include "stdbool.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Convert ASCII hex value to nibble
*
* @param c ASCII character
* @param nibble nibble pointer, output
* @return bool conversion status
*/
bool hex_char_to_hex_nibble(char c, uint8_t* nibble);
#ifdef __cplusplus
}
#endif

20
lib/toolbox/path.c Normal file
View File

@@ -0,0 +1,20 @@
#include "path.h"
void path_extract_filename_no_ext(const char* path, string_t filename) {
string_set(filename, path);
size_t start_position = string_search_rchar(filename, '/');
size_t end_position = string_search_rchar(filename, '.');
if(start_position == STRING_FAILURE) {
start_position = 0;
} else {
start_position += 1;
}
if(end_position == STRING_FAILURE) {
end_position = string_size(filename);
}
string_mid(filename, start_position, end_position - start_position);
}

18
lib/toolbox/path.h Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#include "m-string.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Extract filename without extension from path.
*
* @param path path string
* @param filename output filename string. Must be initialized before.
*/
void path_extract_filename_no_ext(const char* path, string_t filename);
#ifdef __cplusplus
}
#endif

43
lib/toolbox/random_name.c Normal file
View File

@@ -0,0 +1,43 @@
#include "random_name.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <furi.h>
void set_random_name(char* name, uint8_t max_name_size) {
static bool rand_generator_inited = false;
if(!rand_generator_inited) {
srand(DWT->CYCCNT);
rand_generator_inited = true;
}
const char* prefix[] = {
"ancient",
"hollow",
"strange",
"disappeared",
"unknown",
"unthinkable",
"unnamable",
"nameless",
"my",
};
const char* suffix[] = {
"door",
"entrance",
"doorway",
"entry",
"portal",
"entree",
"opening",
"crack",
};
uint8_t prefix_i = rand() % SIZEOF_ARRAY(prefix);
uint8_t suffix_i = rand() % SIZEOF_ARRAY(suffix);
sniprintf(
name, max_name_size, "%s_%s", prefix[prefix_i], suffix[suffix_i]);
// Set first symbol to upper case
name[0] = name[0] - 0x20;
}

17
lib/toolbox/random_name.h Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/** Generates random name
* @param name buffer to write random name
* @param max_name_size length of given buffer
*/
void set_random_name(char* name, uint8_t max_name_size);
#ifdef __cplusplus
}
#endif

50
lib/toolbox/version.c Normal file
View File

@@ -0,0 +1,50 @@
#include "version.h"
struct Version {
const char* git_hash;
const char* git_branch;
const char* git_branch_num;
const char* build_date;
const char* version;
const char* target;
};
/* version of current running firmware (bootloader/flipper) */
static const Version version = {
.git_hash = GIT_COMMIT,
.git_branch = GIT_BRANCH,
.git_branch_num = GIT_BRANCH_NUM,
.build_date = BUILD_DATE,
.version = VERSION,
.target = TARGET,
};
const Version* version_get(void) {
return &version;
}
const char* version_get_githash(const Version* v) {
return v ? v->git_hash : version.git_hash;
}
const char* version_get_gitbranch(const Version* v) {
return v ? v->git_branch : version.git_branch;
}
const char* version_get_gitbranchnum(const Version* v) {
return v ? v->git_branch_num : version.git_branch_num;
}
const char* version_get_builddate(const Version* v) {
return v ? v->build_date : version.build_date;
}
const char* version_get_version(const Version* v) {
return v ? v->version : version.version;
}
const char* version_get_target(const Version* v) {
return v ? v->target : version.target;
}

73
lib/toolbox/version.h Normal file
View File

@@ -0,0 +1,73 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
typedef struct Version Version;
/**
* Gets current running firmware version handle.
* You can store it somewhere. But if you want to retrieve data,
* you have to use 'version_*_get()' set of functions.
* Also, 'version_*_get()' imply to use this
* handle if no handle (NULL_PTR) provided.
*
* @return Handle to version data.
*/
const Version* version_get(void);
/**
* Gets git hash of build commit.
*
* @param v - ptr to version handle. If zero - gets current running fw info.
* @return git hash
*/
const char* version_get_githash(const Version* v);
/**
* Gets git branch of build commit.
*
* @param v - ptr to version handle. If zero - gets current running fw info.
* @return git branch
*/
const char* version_get_gitbranch(const Version* v);
/**
* Gets git number of build commit.
*
* @param v - ptr to version handle. If zero - gets current running fw info.
* @return number of commit
*/
const char* version_get_gitbranchnum(const Version* v);
/**
* Gets build date.
*
* @param v - ptr to version handle. If zero - gets current running fw info.
* @return build date
*/
const char* version_get_builddate(const Version* v);
/**
* Gets build version.
* Build version is last tag in git history.
*
* @param v - ptr to version handle. If zero - gets current running fw info.
* @return build date
*/
const char* version_get_version(const Version* v);
/**
* Gets firmware target.
* Build version is last tag for build commit.
*
* @param v - ptr to version handle. If zero - gets current running fw info.
* @return build date
*/
const char* version_get_target(const Version* v);
#ifdef __cplusplus
}
#endif