[FL-2274] Inventing streams and moving FFF to them (#981)

* Streams: string stream
* String stream: updated insert/delete api
* Streams: generic stream interface and string stream implementation
* Streams: helpers for insert and delete_and_insert
* FFF: now compatible with streams
* MinUnit: introduced tests with arguments
* FFF: stream access violation
* Streams: copy data between streams
* Streams: file stream
* FFF: documentation
* FFStream: documentation
* FFF: alloc as file
* MinUnit: support for nested tests
* Streams: changed delete_and_insert, now it returns success flag. Added ability dump stream inner parameters and data to cout.
* FFF: simplified file open function
* Streams: unit tests
* FFF: tests
* Streams: declare cache_size constant as define, to allow variable modified arrays
* FFF: lib moved to a separate folder
* iButton: new FFF
* RFID: new FFF
* Animations: new FFF
* IR: new FFF
* NFC: new FFF
* Flipper file format: delete lib
* U2F: new FFF
* Subghz: new FFF and streams
* Streams: read line
* Streams: split
* FuriCore: implement memset with extra asserts
* FuriCore: implement extra heap asserts without inventing memset
* Scene manager: protected access to the scene id stack with a size check
* NFC worker: dirty fix for issue where hal_nfc was busy on app start
* Furi: update allocator to erase memory on allocation. Replace furi_alloc with malloc.
* FuriCore: cleanup memmgr code.
* Furi HAL: furi_hal_init is split into critical and non-critical parts. The critical part is currently clock and console.
* Memmgr: added ability to track allocations and deallocations through console.
* FFStream: some speedup
* Streams, FF: minor fixes
* Tests: restore
* File stream: a slightly more thread-safe version of file_stream_delete_and_insert

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
SG
2022-02-19 05:53:46 +10:00
committed by GitHub
parent 242241987e
commit 274c12fc56
257 changed files with 4480 additions and 2657 deletions

View File

@@ -0,0 +1,410 @@
#include <furi/check.h>
#include <toolbox/stream/stream.h>
#include <toolbox/stream/string_stream.h>
#include <toolbox/stream/file_stream.h>
#include "flipper_format.h"
#include "flipper_format_i.h"
#include "flipper_format_stream.h"
#include "flipper_format_stream_i.h"
/********************************** Private **********************************/
struct FlipperFormat {
Stream* stream;
bool strict_mode;
};
static const char* const flipper_format_filetype_key = "Filetype";
static const char* const flipper_format_version_key = "Version";
Stream* flipper_format_get_raw_stream(FlipperFormat* flipper_format) {
return flipper_format->stream;
}
/********************************** Public **********************************/
FlipperFormat* flipper_format_string_alloc() {
FlipperFormat* flipper_format = malloc(sizeof(FlipperFormat));
flipper_format->stream = string_stream_alloc();
flipper_format->strict_mode = false;
return flipper_format;
}
FlipperFormat* flipper_format_file_alloc(Storage* storage) {
FlipperFormat* flipper_format = malloc(sizeof(FlipperFormat));
flipper_format->stream = file_stream_alloc(storage);
flipper_format->strict_mode = false;
return flipper_format;
}
bool flipper_format_file_open_existing(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
return file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_OPEN_EXISTING);
}
bool flipper_format_file_open_append(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
bool result =
file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_OPEN_APPEND);
// Add EOL if it is not there
if(stream_size(flipper_format->stream) >= 1) {
do {
char last_char;
result = false;
if(!stream_seek(flipper_format->stream, -1, StreamOffsetFromEnd)) break;
uint16_t bytes_were_read =
stream_read(flipper_format->stream, (uint8_t*)&last_char, 1);
if(bytes_were_read != 1) break;
if(last_char != flipper_format_eoln) {
if(!flipper_format_stream_write_eol(flipper_format->stream)) break;
}
result = true;
} while(false);
} else {
stream_seek(flipper_format->stream, 0, StreamOffsetFromEnd);
}
return result;
}
bool flipper_format_file_open_always(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
return file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_CREATE_ALWAYS);
}
bool flipper_format_file_open_new(FlipperFormat* flipper_format, const char* path) {
furi_assert(flipper_format);
return file_stream_open(flipper_format->stream, path, FSAM_READ_WRITE, FSOM_CREATE_NEW);
}
bool flipper_format_file_close(FlipperFormat* flipper_format) {
furi_assert(flipper_format);
return file_stream_close(flipper_format->stream);
}
void flipper_format_free(FlipperFormat* flipper_format) {
furi_assert(flipper_format);
stream_free(flipper_format->stream);
free(flipper_format);
}
void flipper_format_set_strict_mode(FlipperFormat* flipper_format, bool strict_mode) {
flipper_format->strict_mode = strict_mode;
}
bool flipper_format_rewind(FlipperFormat* flipper_format) {
furi_assert(flipper_format);
return stream_rewind(flipper_format->stream);
}
bool flipper_format_read_header(
FlipperFormat* flipper_format,
string_t filetype,
uint32_t* version) {
furi_assert(flipper_format);
return flipper_format_read_string(flipper_format, flipper_format_filetype_key, filetype) &&
flipper_format_read_uint32(flipper_format, flipper_format_version_key, version, 1);
}
bool flipper_format_write_header(
FlipperFormat* flipper_format,
string_t filetype,
const uint32_t version) {
furi_assert(flipper_format);
return flipper_format_write_header_cstr(flipper_format, string_get_cstr(filetype), version);
}
bool flipper_format_write_header_cstr(
FlipperFormat* flipper_format,
const char* filetype,
const uint32_t version) {
furi_assert(flipper_format);
return flipper_format_write_string_cstr(
flipper_format, flipper_format_filetype_key, filetype) &&
flipper_format_write_uint32(flipper_format, flipper_format_version_key, &version, 1);
}
bool flipper_format_get_value_count(
FlipperFormat* flipper_format,
const char* key,
uint32_t* count) {
furi_assert(flipper_format);
return flipper_format_stream_get_value_count(
flipper_format->stream, key, count, flipper_format->strict_mode);
}
bool flipper_format_read_string(FlipperFormat* flipper_format, const char* key, string_t data) {
furi_assert(flipper_format);
return flipper_format_stream_read_value_line(
flipper_format->stream, key, FlipperStreamValueStr, data, 1, flipper_format->strict_mode);
}
bool flipper_format_write_string(FlipperFormat* flipper_format, const char* key, string_t data) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueStr,
.data = string_get_cstr(data),
.data_size = 1,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_write_string_cstr(
FlipperFormat* flipper_format,
const char* key,
const char* data) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueStr,
.data = data,
.data_size = 1,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_read_uint32(
FlipperFormat* flipper_format,
const char* key,
uint32_t* data,
const uint16_t data_size) {
furi_assert(flipper_format);
return flipper_format_stream_read_value_line(
flipper_format->stream,
key,
FlipperStreamValueUint32,
data,
data_size,
flipper_format->strict_mode);
}
bool flipper_format_write_uint32(
FlipperFormat* flipper_format,
const char* key,
const uint32_t* data,
const uint16_t data_size) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueUint32,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_read_int32(
FlipperFormat* flipper_format,
const char* key,
int32_t* data,
const uint16_t data_size) {
return flipper_format_stream_read_value_line(
flipper_format->stream,
key,
FlipperStreamValueInt32,
data,
data_size,
flipper_format->strict_mode);
}
bool flipper_format_write_int32(
FlipperFormat* flipper_format,
const char* key,
const int32_t* data,
const uint16_t data_size) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueInt32,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_read_float(
FlipperFormat* flipper_format,
const char* key,
float* data,
const uint16_t data_size) {
return flipper_format_stream_read_value_line(
flipper_format->stream,
key,
FlipperStreamValueFloat,
data,
data_size,
flipper_format->strict_mode);
}
bool flipper_format_write_float(
FlipperFormat* flipper_format,
const char* key,
const float* data,
const uint16_t data_size) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueFloat,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_read_hex(
FlipperFormat* flipper_format,
const char* key,
uint8_t* data,
const uint16_t data_size) {
return flipper_format_stream_read_value_line(
flipper_format->stream,
key,
FlipperStreamValueHex,
data,
data_size,
flipper_format->strict_mode);
}
bool flipper_format_write_hex(
FlipperFormat* flipper_format,
const char* key,
const uint8_t* data,
const uint16_t data_size) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueHex,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_write_value_line(flipper_format->stream, &write_data);
return result;
}
bool flipper_format_write_comment(FlipperFormat* flipper_format, string_t data) {
furi_assert(flipper_format);
return flipper_format_write_comment_cstr(flipper_format, string_get_cstr(data));
}
bool flipper_format_write_comment_cstr(FlipperFormat* flipper_format, const char* data) {
furi_assert(flipper_format);
return flipper_format_stream_write_comment_cstr(flipper_format->stream, data);
}
bool flipper_format_delete_key(FlipperFormat* flipper_format, const char* key) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueIgnore,
.data = NULL,
.data_size = 0,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key, string_t data) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueStr,
.data = data,
.data_size = 1,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_string_cstr(
FlipperFormat* flipper_format,
const char* key,
const char* data) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueStr,
.data = data,
.data_size = 1,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_uint32(
FlipperFormat* flipper_format,
const char* key,
const uint32_t* data,
const uint16_t data_size) {
furi_assert(flipper_format);
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueUint32,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_int32(
FlipperFormat* flipper_format,
const char* key,
const int32_t* data,
const uint16_t data_size) {
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueInt32,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_float(
FlipperFormat* flipper_format,
const char* key,
const float* data,
const uint16_t data_size) {
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueFloat,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}
bool flipper_format_update_hex(
FlipperFormat* flipper_format,
const char* key,
const uint8_t* data,
const uint16_t data_size) {
FlipperStreamWriteData write_data = {
.key = key,
.type = FlipperStreamValueHex,
.data = data,
.data_size = data_size,
};
bool result = flipper_format_stream_delete_key_and_write(
flipper_format->stream, &write_data, flipper_format->strict_mode);
return result;
}

View File

@@ -0,0 +1,471 @@
/**
* @file flipper_format.h
* Flipper File Format helper library.
*
* Flipper File Format is a fairly simple format for storing data in a file.
*
* Flipper file structure:
*
* ~~~~~~~~~~~~~~~~~~~~~
* # Commentary
* Field name: field value
* ~~~~~~~~~~~~~~~~~~~~~
*
* Lines starting with the # character are ignored (considered as comments). The separator between the name of the value and the value itself is the string ": ".
*
* Currently supported types:
*
* ~~~~~~~~~~~~~~~~~~~~~
* String: text
* Int32: 1 2 -3 4
* Uint32: 1 2 3 4
* Float: 1.0 1234.654
* Hex: A4 B3 C2 D1 12 FF
* ~~~~~~~~~~~~~~~~~~~~~
*
* End of line is LF when writing, but CR is supported when reading.
*
* The library is designed in such a way that comments and field values are completely ignored when searching for keys, that is, they do not consume memory.
*
* File example:
*
* ~~~~~~~~~~~~~~~~~~~~~
* Filetype: Flipper Test File
* Version: 1
* # Just test file
* String: String value
* UINT: 1234
* Hex: 00 01 FF A3
* ~~~~~~~~~~~~~~~~~~~~~
*
* Writing:
*
* ~~~~~~~~~~~~~~~~~~~~~
* FlipperFormat format = flipper_format_file_alloc(storage);
*
* do {
* const uint32_t version = 1;
* const char* string_value = "String value";
* const uint32_t uint32_value = 1234;
* const uint16_t array_size = 4;
* const uint8_t* array[array_size] = {0x00, 0x01, 0xFF, 0xA3};
*
* if(!flipper_format_file_open_new(format, "/ext/flipper_format_test")) break;
* if(!flipper_format_write_header_cstr(format, "Flipper Test File", version)) break;
* if(!flipper_format_write_comment_cstr(format, "Just test file")) break;
* if(!flipper_format_write_string_cstr(format, "String", string_value)) break;
* if(!flipper_format_write_uint32(format, "UINT", &uint32_value, 1)) break;
* if(!flipper_format_write_hex(format, "Hex Array", array, array_size)) break;
*
* // signal that the file was written successfully
* } while(0);
*
* flipper_format_free(file);
* ~~~~~~~~~~~~~~~~~~~~~
*
* Reading:
*
* ~~~~~~~~~~~~~~~~~~~~~
* FlipperFormat file = flipper_format_file_alloc(storage);
*
* do {
* uint32_t version = 1;
* string_t file_type;
* string_t string_value;
* uint32_t uint32_value = 1;
* uint16_t array_size = 4;
* uint8_t* array[array_size] = {0};
* string_init(file_type);
* string_init(string_value);
*
* if(!flipper_format_file_open_existing(file, "/ext/flipper_format_test")) break;
* if(!flipper_format_read_header(file, file_type, &version)) break;
* if(!flipper_format_read_string(file, "String", string_value)) break;
* if(!flipper_format_read_uint32(file, "UINT", &uint32_value, 1)) break;
* if(!flipper_format_read_hex(file, "Hex Array", array, array_size)) break;
*
* // signal that the file was read successfully
* } while(0);
*
* flipper_format_free(file);
* ~~~~~~~~~~~~~~~~~~~~~
*
*/
#pragma once
#include <stdint.h>
#include <mlib/m-string.h>
#include <storage/storage.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct FlipperFormat FlipperFormat;
/**
* Allocate FlipperFormat as string.
* @return FlipperFormat* pointer to a FlipperFormat instance
*/
FlipperFormat* flipper_format_string_alloc();
/**
* Allocate FlipperFormat as file.
* @return FlipperFormat* pointer to a FlipperFormat instance
*/
FlipperFormat* flipper_format_file_alloc(Storage* storage);
/**
* Open existing file.
* Use only if FlipperFormat allocated as a file.
* @param flipper_format Pointer to a FlipperFormat instance
* @param path File path
* @return True on success
*/
bool flipper_format_file_open_existing(FlipperFormat* flipper_format, const char* path);
/**
* Open existing file for writing and add values to the end of file.
* Use only if FlipperFormat allocated as a file.
* @param flipper_format Pointer to a FlipperFormat instance
* @param path File path
* @return True on success
*/
bool flipper_format_file_open_append(FlipperFormat* flipper_format, const char* path);
/**
* Open file. Creates a new file, or deletes the contents of the file if it already exists.
* Use only if FlipperFormat allocated as a file.
* @param flipper_format Pointer to a FlipperFormat instance
* @param path File path
* @return True on success
*/
bool flipper_format_file_open_always(FlipperFormat* flipper_format, const char* path);
/**
* Open file. Creates a new file, fails if file already exists.
* Use only if FlipperFormat allocated as a file.
* @param flipper_format Pointer to a FlipperFormat instance
* @param path File path
* @return True on success
*/
bool flipper_format_file_open_new(FlipperFormat* flipper_format, const char* path);
/**
* Closes the file, use only if FlipperFormat allocated as a file.
* @param flipper_format
* @return true
* @return false
*/
bool flipper_format_file_close(FlipperFormat* flipper_format);
/**
* Free FlipperFormat.
* @param flipper_format Pointer to a FlipperFormat instance
*/
void flipper_format_free(FlipperFormat* flipper_format);
/**
* Set FlipperFormat mode.
* @param flipper_format Pointer to a FlipperFormat instance
* @param strict_mode True obligates not to skip valid fields. False by default.
*/
void flipper_format_set_strict_mode(FlipperFormat* flipper_format, bool strict_mode);
/**
* Rewind the RW pointer.
* @param flipper_format Pointer to a FlipperFormat instance
* @return True on success
*/
bool flipper_format_rewind(FlipperFormat* flipper_format);
/**
* Read the header (file type and version).
* @param flipper_format Pointer to a FlipperFormat instance
* @param filetype File type string
* @param version Version Value
* @return True on success
*/
bool flipper_format_read_header(
FlipperFormat* flipper_format,
string_t filetype,
uint32_t* version);
/**
* Write the header (file type and version).
* @param flipper_format Pointer to a FlipperFormat instance
* @param filetype File type string
* @param version Version Value
* @return True on success
*/
bool flipper_format_write_header(
FlipperFormat* flipper_format,
string_t filetype,
const uint32_t version);
/**
* Write the header (file type and version). Plain C string version.
* @param flipper_format Pointer to a FlipperFormat instance
* @param filetype File type string
* @param version Version Value
* @return True on success
*/
bool flipper_format_write_header_cstr(
FlipperFormat* flipper_format,
const char* filetype,
const uint32_t version);
/**
* Get the count of values by key
* @param flipper_format Pointer to a FlipperFormat instance
* @param key
* @param count
* @return bool
*/
bool flipper_format_get_value_count(
FlipperFormat* flipper_format,
const char* key,
uint32_t* count);
/**
* Read a string by key
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_format_read_string(FlipperFormat* flipper_format, const char* key, string_t data);
/**
* Write key and string
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_format_write_string(FlipperFormat* flipper_format, const char* key, string_t data);
/**
* Write key and string. Plain C string version.
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_format_write_string_cstr(
FlipperFormat* flipper_format,
const char* key,
const char* data);
/**
* Read array of uint32 by key
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @param data_size Values count
* @return True on success
*/
bool flipper_format_read_uint32(
FlipperFormat* flipper_format,
const char* key,
uint32_t* data,
const uint16_t data_size);
/**
* Write key and array of uint32
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @param data_size Values count
* @return True on success
*/
bool flipper_format_write_uint32(
FlipperFormat* flipper_format,
const char* key,
const uint32_t* data,
const uint16_t data_size);
/**
* Read array of int32 by key
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @param data_size Values count
* @return True on success
*/
bool flipper_format_read_int32(
FlipperFormat* flipper_format,
const char* key,
int32_t* data,
const uint16_t data_size);
/**
* Write key and array of int32
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @param data_size Values count
* @return True on success
*/
bool flipper_format_write_int32(
FlipperFormat* flipper_format,
const char* key,
const int32_t* data,
const uint16_t data_size);
/**
* Read array of float by key
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @param data_size Values count
* @return True on success
*/
bool flipper_format_read_float(
FlipperFormat* flipper_format,
const char* key,
float* data,
const uint16_t data_size);
/**
* Write key and array of float
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @param data_size Values count
* @return True on success
*/
bool flipper_format_write_float(
FlipperFormat* flipper_format,
const char* key,
const float* data,
const uint16_t data_size);
/**
* Read array of hex-formatted bytes by key
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @param data_size Values count
* @return True on success
*/
bool flipper_format_read_hex(
FlipperFormat* flipper_format,
const char* key,
uint8_t* data,
const uint16_t data_size);
/**
* Write key and array of hex-formatted bytes
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @param data_size Values count
* @return True on success
*/
bool flipper_format_write_hex(
FlipperFormat* flipper_format,
const char* key,
const uint8_t* data,
const uint16_t data_size);
/**
* Write comment
* @param flipper_format Pointer to a FlipperFormat instance
* @param data Comment text
* @return True on success
*/
bool flipper_format_write_comment(FlipperFormat* flipper_format, string_t data);
/**
* Write comment. Plain C string version.
* @param flipper_format Pointer to a FlipperFormat instance
* @param data Comment text
* @return True on success
*/
bool flipper_format_write_comment_cstr(FlipperFormat* flipper_format, const char* data);
/**
* Removes the first matching key and its value. Sets the RW pointer to a position of deleted data.
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @return True on success
*/
bool flipper_format_delete_key(FlipperFormat* flipper_format, const char* key);
/**
* Updates the value of the first matching key to a string value. Sets the RW pointer to a position at the end of inserted data.
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_format_update_string(FlipperFormat* flipper_format, const char* key, string_t data);
/**
* Updates the value of the first matching key to a string value. Plain C version. Sets the RW pointer to a position at the end of inserted data.
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_format_update_string_cstr(
FlipperFormat* flipper_format,
const char* key,
const char* data);
/**
* Updates the value of the first matching key to a uint32 array value. Sets the RW pointer to a position at the end of inserted data.
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_format_update_uint32(
FlipperFormat* flipper_format,
const char* key,
const uint32_t* data,
const uint16_t data_size);
/**
* Updates the value of the first matching key to a int32 array value. Sets the RW pointer to a position at the end of inserted data.
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_format_update_int32(
FlipperFormat* flipper_format,
const char* key,
const int32_t* data,
const uint16_t data_size);
/**
* Updates the value of the first matching key to a float array value. Sets the RW pointer to a position at the end of inserted data.
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_format_update_float(
FlipperFormat* flipper_format,
const char* key,
const float* data,
const uint16_t data_size);
/**
* Updates the value of the first matching key to an array of hex-formatted bytes. Sets the RW pointer to a position at the end of inserted data.
* @param flipper_format Pointer to a FlipperFormat instance
* @param key Key
* @param data Value
* @return True on success
*/
bool flipper_format_update_hex(
FlipperFormat* flipper_format,
const char* key,
const uint8_t* data,
const uint16_t data_size);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,19 @@
#pragma once
#include <toolbox/stream/stream.h>
#include "flipper_format.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Returns the underlying stream instance.
* Use only if you know what you are doing.
* @param flipper_format
* @return Stream*
*/
Stream* flipper_format_get_raw_stream(FlipperFormat* flipper_format);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,497 @@
#include <inttypes.h>
#include <toolbox/hex.h>
#include <furi/check.h>
#include "flipper_format_stream.h"
#include "flipper_format_stream_i.h"
static bool flipper_format_stream_write(Stream* stream, const void* data, size_t data_size) {
size_t bytes_written = stream_write(stream, data, data_size);
return bytes_written == data_size;
}
static bool flipper_format_stream_write_key(Stream* stream, const char* key) {
bool result = false;
do {
if(!flipper_format_stream_write(stream, key, strlen(key))) break;
if(!flipper_format_stream_write(stream, &flipper_format_delimiter, 1)) break;
if(!flipper_format_stream_write(stream, " ", 1)) break;
result = true;
} while(false);
return result;
}
bool flipper_format_stream_write_eol(Stream* stream) {
return flipper_format_stream_write(stream, &flipper_format_eoln, 1);
}
static bool flipper_format_stream_read_valid_key(Stream* stream, string_t key) {
string_reset(key);
const size_t buffer_size = 32;
uint8_t buffer[buffer_size];
bool found = false;
bool error = false;
bool accumulate = true;
bool new_line = true;
while(true) {
size_t was_read = stream_read(stream, buffer, buffer_size);
if(was_read == 0) break;
for(size_t i = 0; i < was_read; i++) {
uint8_t data = buffer[i];
if(data == flipper_format_eoln) {
// EOL found, clean data, start accumulating data and set the new_line flag
string_reset(key);
accumulate = true;
new_line = true;
} else if(data == flipper_format_eolr) {
// ignore
} else if(data == flipper_format_comment && new_line) {
// if there is a comment character and we are at the beginning of a new line
// do not accumulate comment data and reset the new_line flag
accumulate = false;
new_line = false;
} else if(data == flipper_format_delimiter) {
if(new_line) {
// we are on a "new line" and found the delimiter
// this can only be if we have previously found some kind of key, so
// clear the data, set the flag that we no longer want to accumulate data
// and reset the new_line flag
string_reset(key);
accumulate = false;
new_line = false;
} else {
// parse the delimiter only if we are accumulating data
if(accumulate) {
// we found the delimiter, move the rw pointer to the delimiter location
// and signal that we have found something
if(!stream_seek(stream, i - was_read, StreamOffsetFromCurrent)) {
error = true;
break;
}
found = true;
break;
}
}
} else {
// just new symbol, reset the new_line flag
new_line = false;
if(accumulate) {
// and accumulate data if we want
string_push_back(key, data);
}
}
}
if(found || error) break;
}
return found;
}
static bool flipper_format_stream_seek_to_key(Stream* stream, const char* key, bool strict_mode) {
bool found = false;
string_t read_key;
string_init(read_key);
while(!stream_eof(stream)) {
if(flipper_format_stream_read_valid_key(stream, read_key)) {
if(string_cmp_str(read_key, key) == 0) {
if(!stream_seek(stream, 2, StreamOffsetFromCurrent)) break;
found = true;
break;
} else if(strict_mode) {
found = false;
break;
}
}
}
string_clear(read_key);
return found;
}
static bool flipper_format_stream_read_value(Stream* stream, string_t value, bool* last) {
string_reset(value);
const size_t buffer_size = 32;
uint8_t buffer[buffer_size];
bool result = false;
bool error = false;
while(true) {
size_t was_read = stream_read(stream, buffer, buffer_size);
if(was_read == 0) {
// check EOF
if(stream_eof(stream) && string_size(value) > 0) {
result = true;
*last = true;
break;
}
}
for(uint16_t i = 0; i < was_read; i++) {
uint8_t data = buffer[i];
if(data == flipper_format_eoln) {
if(string_size(value) > 0) {
if(!stream_seek(stream, i - was_read, StreamOffsetFromCurrent)) {
error = true;
break;
}
result = true;
*last = true;
break;
} else {
error = true;
}
} else if(data == ' ') {
if(string_size(value) > 0) {
if(!stream_seek(stream, i - was_read, StreamOffsetFromCurrent)) {
error = true;
break;
}
result = true;
*last = false;
break;
}
} else if(data == flipper_format_eolr) {
// Ignore
} else {
string_push_back(value, data);
}
}
if(error || result) break;
}
return result;
}
static bool flipper_format_stream_read_line(Stream* stream, string_t str_result) {
string_reset(str_result);
const size_t buffer_size = 32;
uint8_t buffer[buffer_size];
do {
size_t was_read = stream_read(stream, buffer, buffer_size);
if(was_read == 0) break;
bool result = false;
bool error = false;
for(size_t i = 0; i < was_read; i++) {
uint8_t data = buffer[i];
if(data == flipper_format_eoln) {
if(!stream_seek(stream, i - was_read, StreamOffsetFromCurrent)) {
error = true;
break;
}
result = true;
break;
} else if(data == flipper_format_eolr) {
// Ignore
} else {
string_push_back(str_result, data);
}
}
if(result || error) {
break;
}
} while(true);
return string_size(str_result) != 0;
}
static bool flipper_format_stream_seek_to_next_line(Stream* stream) {
const size_t buffer_size = 32;
uint8_t buffer[buffer_size];
bool result = false;
bool error = false;
do {
size_t was_read = stream_read(stream, buffer, buffer_size);
if(was_read == 0) {
if(stream_eof(stream)) {
result = true;
break;
}
}
for(size_t i = 0; i < was_read; i++) {
if(buffer[i] == flipper_format_eoln) {
if(!stream_seek(stream, i - was_read, StreamOffsetFromCurrent)) {
error = true;
break;
}
result = true;
break;
}
}
if(result || error) {
break;
}
} while(true);
return result;
}
bool flipper_format_stream_write_value_line(Stream* stream, FlipperStreamWriteData* write_data) {
bool result = false;
if(write_data->type == FlipperStreamValueIgnore) {
result = true;
} else {
string_t value;
string_init(value);
do {
if(!flipper_format_stream_write_key(stream, write_data->key)) break;
if(write_data->type == FlipperStreamValueStr) write_data->data_size = 1;
bool cycle_error = false;
for(uint16_t i = 0; i < write_data->data_size; i++) {
switch(write_data->type) {
case FlipperStreamValueStr: {
const char* data = write_data->data;
string_printf(value, "%s", data);
}; break;
case FlipperStreamValueHex: {
const uint8_t* data = write_data->data;
string_printf(value, "%02X", data[i]);
}; break;
case FlipperStreamValueFloat: {
const float* data = write_data->data;
string_printf(value, "%f", data[i]);
}; break;
case FlipperStreamValueInt32: {
const int32_t* data = write_data->data;
string_printf(value, "%" PRIi32, data[i]);
}; break;
case FlipperStreamValueUint32: {
const uint32_t* data = write_data->data;
string_printf(value, "%" PRId32, data[i]);
}; break;
default:
furi_crash("Unknown FF type");
}
if((i + 1) < write_data->data_size) {
string_cat(value, " ");
}
if(!flipper_format_stream_write(
stream, string_get_cstr(value), string_size(value))) {
cycle_error = true;
break;
}
}
if(cycle_error) break;
if(!flipper_format_stream_write_eol(stream)) break;
result = true;
} while(false);
string_clear(value);
}
return result;
}
bool flipper_format_stream_read_value_line(
Stream* stream,
const char* key,
FlipperStreamValue type,
void* _data,
size_t data_size,
bool strict_mode) {
bool result = false;
do {
if(!flipper_format_stream_seek_to_key(stream, key, strict_mode)) break;
if(type == FlipperStreamValueStr) {
string_ptr data = (string_ptr)_data;
if(flipper_format_stream_read_line(stream, data)) {
result = true;
break;
}
} else {
result = true;
string_t value;
string_init(value);
for(uint16_t i = 0; i < data_size; i++) {
bool last = false;
result = flipper_format_stream_read_value(stream, value, &last);
if(result) {
int scan_values = 0;
switch(type) {
case FlipperStreamValueHex: {
uint8_t* data = _data;
if(string_size(value) >= 2) {
// sscanf "%02X" does not work here
if(hex_chars_to_uint8(
string_get_char(value, 0),
string_get_char(value, 1),
&data[i])) {
scan_values = 1;
}
}
}; break;
case FlipperStreamValueFloat: {
float* data = _data;
// newlib-nano does not have sscanf for floats
// scan_values = sscanf(string_get_cstr(value), "%f", &data[i]);
char* end_char;
data[i] = strtof(string_get_cstr(value), &end_char);
if(*end_char == 0) {
// most likely ok
scan_values = 1;
}
}; break;
case FlipperStreamValueInt32: {
int32_t* data = _data;
scan_values = sscanf(string_get_cstr(value), "%" PRIi32, &data[i]);
}; break;
case FlipperStreamValueUint32: {
uint32_t* data = _data;
scan_values = sscanf(string_get_cstr(value), "%" PRId32, &data[i]);
}; break;
default:
furi_crash("Unknown FF type");
}
if(scan_values != 1) {
result = false;
break;
}
} else {
break;
}
if(last && ((i + 1) != data_size)) {
result = false;
break;
}
}
string_clear(value);
}
} while(false);
return result;
}
bool flipper_format_stream_get_value_count(
Stream* stream,
const char* key,
uint32_t* count,
bool strict_mode) {
bool result = false;
bool last = false;
string_t value;
string_init(value);
uint32_t position = stream_tell(stream);
do {
if(!flipper_format_stream_seek_to_key(stream, key, strict_mode)) break;
*count = 0;
result = true;
while(true) {
if(!flipper_format_stream_read_value(stream, value, &last)) {
result = false;
break;
}
*count = *count + 1;
if(last) break;
}
} while(false);
if(!stream_seek(stream, position, StreamOffsetFromStart)) {
result = false;
}
string_clear(value);
return result;
}
bool flipper_format_stream_delete_key_and_write(
Stream* stream,
FlipperStreamWriteData* write_data,
bool strict_mode) {
bool result = false;
do {
size_t size = stream_size(stream);
if(size == 0) break;
if(!stream_rewind(stream)) break;
// find key
if(!flipper_format_stream_seek_to_key(stream, write_data->key, strict_mode)) break;
// get key start position
size_t start_position = stream_tell(stream) - strlen(write_data->key);
if(start_position >= 2) {
start_position -= 2;
} else {
// something wrong
break;
}
// get value end position
if(!flipper_format_stream_seek_to_next_line(stream)) break;
size_t end_position = stream_tell(stream);
// newline symbol
if(end_position < size) {
end_position += 1;
}
if(!stream_seek(stream, start_position, StreamOffsetFromStart)) break;
if(!stream_delete_and_insert(
stream,
end_position - start_position,
(StreamWriteCB)flipper_format_stream_write_value_line,
write_data))
break;
result = true;
} while(false);
return result;
}
bool flipper_format_stream_write_comment_cstr(Stream* stream, const char* data) {
bool result = false;
do {
const char comment_buffer[2] = {flipper_format_comment, ' '};
result = flipper_format_stream_write(stream, comment_buffer, sizeof(comment_buffer));
if(!result) break;
result = flipper_format_stream_write(stream, data, strlen(data));
if(!result) break;
result = flipper_format_stream_write_eol(stream);
} while(false);
return result;
}

View File

@@ -0,0 +1,94 @@
#pragma once
#include <stdlib.h>
#include <stdbool.h>
#include <toolbox/stream/stream.h>
#include <mlib/m-string.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
FlipperStreamValueIgnore,
FlipperStreamValueStr,
FlipperStreamValueHex,
FlipperStreamValueFloat,
FlipperStreamValueInt32,
FlipperStreamValueUint32,
} FlipperStreamValue;
typedef struct {
const char* key;
FlipperStreamValue type;
const void* data;
size_t data_size;
} FlipperStreamWriteData;
/**
* Writes a key/value pair to the stream.
* @param stream
* @param write_data
* @return true
* @return false
*/
bool flipper_format_stream_write_value_line(Stream* stream, FlipperStreamWriteData* write_data);
/**
* Reads a value by key from a stream.
* @param stream
* @param key
* @param type
* @param _data
* @param data_size
* @param strict_mode
* @return true
* @return false
*/
bool flipper_format_stream_read_value_line(
Stream* stream,
const char* key,
FlipperStreamValue type,
void* _data,
size_t data_size,
bool strict_mode);
/**
* Get the count of values by key from a stream.
* @param stream
* @param key
* @param count
* @param strict_mode
* @return true
* @return false
*/
bool flipper_format_stream_get_value_count(
Stream* stream,
const char* key,
uint32_t* count,
bool strict_mode);
/**
* Removes a key and the corresponding value string from the stream and inserts a new key/value pair.
* @param stream
* @param write_data
* @param strict_mode
* @return true
* @return false
*/
bool flipper_format_stream_delete_key_and_write(
Stream* stream,
FlipperStreamWriteData* write_data,
bool strict_mode);
/**
* Writes a comment string to the stream.
* @param stream
* @param data
* @return true
* @return false
*/
bool flipper_format_stream_write_comment_cstr(Stream* stream, const char* data);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,23 @@
#pragma once
#include "flipper_format_stream.h"
static const char flipper_format_delimiter = ':';
static const char flipper_format_comment = '#';
static const char flipper_format_eoln = '\n';
static const char flipper_format_eolr = '\r';
#ifdef __cplusplus
extern "C" {
#endif
/**
* Write Flipper Format EOL to the stream
* @param stream
* @return true
* @return false
*/
bool flipper_format_stream_write_eol(Stream* stream);
#ifdef __cplusplus
}
#endif