[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:
@@ -1,204 +0,0 @@
|
||||
#include "file_helper.h"
|
||||
|
||||
const char flipper_file_eoln = '\n';
|
||||
const char flipper_file_eolr = '\r';
|
||||
|
||||
bool file_helper_seek(File* file, int32_t offset) {
|
||||
uint64_t position = storage_file_tell(file);
|
||||
return storage_file_seek(file, position + offset, true);
|
||||
}
|
||||
|
||||
bool file_helper_write_hex(File* file, const uint8_t* data, const uint16_t data_size) {
|
||||
const uint8_t byte_text_size = 3;
|
||||
char byte_text[byte_text_size];
|
||||
|
||||
bool result = true;
|
||||
uint16_t bytes_written;
|
||||
for(uint8_t i = 0; i < data_size; i++) {
|
||||
snprintf(byte_text, byte_text_size, "%02X", data[i]);
|
||||
|
||||
if(i != 0) {
|
||||
// space
|
||||
const char space = ' ';
|
||||
bytes_written = storage_file_write(file, &space, sizeof(char));
|
||||
if(bytes_written != sizeof(char)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bytes_written = storage_file_write(file, &byte_text, strlen(byte_text));
|
||||
if(bytes_written != strlen(byte_text)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool file_helper_read_line(File* file, string_t str_result) {
|
||||
string_reset(str_result);
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
|
||||
do {
|
||||
uint16_t bytes_were_read = storage_file_read(file, buffer, buffer_size);
|
||||
// TODO process EOF
|
||||
if(bytes_were_read == 0) break;
|
||||
|
||||
bool result = false;
|
||||
bool error = false;
|
||||
for(uint16_t i = 0; i < bytes_were_read; i++) {
|
||||
if(buffer[i] == flipper_file_eoln) {
|
||||
if(!file_helper_seek(file, i - bytes_were_read)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
break;
|
||||
} else if(buffer[i] == flipper_file_eolr) {
|
||||
// Ignore
|
||||
} else {
|
||||
string_push_back(str_result, buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if(result || error) {
|
||||
break;
|
||||
}
|
||||
} while(true);
|
||||
|
||||
return string_size(str_result) != 0;
|
||||
}
|
||||
|
||||
bool file_helper_seek_to_next_line(File* file) {
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
bool result = false;
|
||||
bool error = false;
|
||||
|
||||
do {
|
||||
uint16_t bytes_were_read = storage_file_read(file, buffer, buffer_size);
|
||||
if(bytes_were_read == 0) {
|
||||
if(storage_file_eof(file)) {
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(uint16_t i = 0; i < bytes_were_read; i++) {
|
||||
if(buffer[i] == flipper_file_eoln) {
|
||||
if(!file_helper_seek(file, i - bytes_were_read)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(result || error) {
|
||||
break;
|
||||
}
|
||||
} while(true);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool file_helper_read_value(File* file, string_t value, bool* last) {
|
||||
string_reset(value);
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
bool result = false;
|
||||
bool error = false;
|
||||
|
||||
while(true) {
|
||||
uint16_t bytes_were_read = storage_file_read(file, buffer, buffer_size);
|
||||
|
||||
if(bytes_were_read == 0) {
|
||||
// check EOF
|
||||
if(storage_file_eof(file) && string_size(value) > 0) {
|
||||
result = true;
|
||||
*last = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(uint16_t i = 0; i < bytes_were_read; i++) {
|
||||
if(buffer[i] == flipper_file_eoln) {
|
||||
if(string_size(value) > 0) {
|
||||
if(!file_helper_seek(file, i - bytes_were_read)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
*last = true;
|
||||
break;
|
||||
} else {
|
||||
error = true;
|
||||
}
|
||||
} else if(buffer[i] == ' ') {
|
||||
if(string_size(value) > 0) {
|
||||
if(!file_helper_seek(file, i - bytes_were_read)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
*last = false;
|
||||
break;
|
||||
}
|
||||
|
||||
} else if(buffer[i] == flipper_file_eolr) {
|
||||
// Ignore
|
||||
} else {
|
||||
string_push_back(value, buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if(error || result) break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool file_helper_write(File* file, const void* data, uint16_t data_size) {
|
||||
uint16_t bytes_written = storage_file_write(file, data, data_size);
|
||||
return bytes_written == data_size;
|
||||
}
|
||||
|
||||
bool file_helper_write_eol(File* file) {
|
||||
return file_helper_write(file, &flipper_file_eoln, sizeof(char));
|
||||
}
|
||||
|
||||
bool file_helper_copy(File* file_from, File* file_to, uint64_t start_offset, uint64_t stop_offset) {
|
||||
bool result = false;
|
||||
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
uint64_t current_offset = start_offset;
|
||||
|
||||
if(storage_file_seek(file_from, start_offset, true)) {
|
||||
do {
|
||||
int32_t bytes_count = MIN(buffer_size, stop_offset - current_offset);
|
||||
if(bytes_count <= 0) {
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
|
||||
uint16_t bytes_were_read = storage_file_read(file_from, buffer, bytes_count);
|
||||
if(bytes_were_read != bytes_count) break;
|
||||
|
||||
uint16_t bytes_were_written = storage_file_write(file_to, buffer, bytes_count);
|
||||
if(bytes_were_written != bytes_count) break;
|
||||
|
||||
current_offset += bytes_count;
|
||||
} while(true);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern const char flipper_file_eoln;
|
||||
extern const char flipper_file_eolr;
|
||||
|
||||
/**
|
||||
* Negative seek helper
|
||||
* @param file
|
||||
* @param offset
|
||||
* @return bool
|
||||
*/
|
||||
bool file_helper_seek(File* file, int32_t offset);
|
||||
|
||||
/**
|
||||
* Writes data to a file as a hexadecimal array.
|
||||
* @param file
|
||||
* @param data
|
||||
* @param data_size
|
||||
* @return true on success write
|
||||
*/
|
||||
bool file_helper_write_hex(File* file, const uint8_t* data, const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Reads data as a string from the stored rw pointer to the \\n symbol position. Ignores \r.
|
||||
* @param file
|
||||
* @param str_result
|
||||
* @return true on success read
|
||||
*/
|
||||
bool file_helper_read_line(File* file, string_t str_result);
|
||||
|
||||
/**
|
||||
* Moves the RW pointer to the beginning of the next line
|
||||
* @param file
|
||||
* @return bool
|
||||
*/
|
||||
bool file_helper_seek_to_next_line(File* file);
|
||||
|
||||
/**
|
||||
* Read one value from array-like string (separated by ' ')
|
||||
* @param file
|
||||
* @param value
|
||||
* @return bool
|
||||
*/
|
||||
bool file_helper_read_value(File* file, string_t value, bool* last);
|
||||
|
||||
/**
|
||||
* Write helper
|
||||
* @param file
|
||||
* @param data
|
||||
* @param data_size
|
||||
* @return bool
|
||||
*/
|
||||
bool file_helper_write(File* file, const void* data, uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Write EOL
|
||||
* @param file
|
||||
* @return bool
|
||||
*/
|
||||
bool file_helper_write_eol(File* file);
|
||||
|
||||
/**
|
||||
* Appends part of one file to the end of another file
|
||||
* @param file_from
|
||||
* @param file_to
|
||||
* @param start_offset
|
||||
* @param stop_offset
|
||||
* @return bool
|
||||
*/
|
||||
bool file_helper_copy(File* file_from, File* file_to, uint64_t start_offset, uint64_t stop_offset);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,402 +0,0 @@
|
||||
#include <furi.h>
|
||||
#include "file_helper.h"
|
||||
#include "flipper_file_helper.h"
|
||||
#include "flipper_file.h"
|
||||
#include "flipper_file_i.h"
|
||||
#include <inttypes.h>
|
||||
#include <toolbox/hex.h>
|
||||
|
||||
FlipperFile* flipper_file_alloc(Storage* storage) {
|
||||
// furi_assert(storage);
|
||||
|
||||
FlipperFile* flipper_file = malloc(sizeof(FlipperFile));
|
||||
flipper_file->storage = storage;
|
||||
flipper_file->file = storage_file_alloc(flipper_file->storage);
|
||||
flipper_file->strict_mode = false;
|
||||
|
||||
return flipper_file;
|
||||
}
|
||||
|
||||
void flipper_file_free(FlipperFile* flipper_file) {
|
||||
furi_assert(flipper_file);
|
||||
if(storage_file_is_open(flipper_file->file)) {
|
||||
storage_file_close(flipper_file->file);
|
||||
}
|
||||
storage_file_free(flipper_file->file);
|
||||
free(flipper_file);
|
||||
}
|
||||
|
||||
void flipper_file_set_strict_mode(FlipperFile* flipper_file, bool strict_mode) {
|
||||
furi_assert(flipper_file);
|
||||
flipper_file->strict_mode = strict_mode;
|
||||
}
|
||||
|
||||
bool flipper_file_open_existing(FlipperFile* flipper_file, const char* filename) {
|
||||
furi_assert(flipper_file);
|
||||
bool result = storage_file_open(
|
||||
flipper_file->file, filename, FSAM_READ | FSAM_WRITE, FSOM_OPEN_EXISTING);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_open_append(FlipperFile* flipper_file, const char* filename) {
|
||||
furi_assert(flipper_file);
|
||||
|
||||
bool result =
|
||||
storage_file_open(flipper_file->file, filename, FSAM_READ | FSAM_WRITE, FSOM_OPEN_APPEND);
|
||||
|
||||
// Add EOL if it is not there
|
||||
if(storage_file_size(flipper_file->file) >= 1) {
|
||||
do {
|
||||
char last_char;
|
||||
result = false;
|
||||
|
||||
if(!file_helper_seek(flipper_file->file, -1)) break;
|
||||
|
||||
uint16_t bytes_were_read = storage_file_read(flipper_file->file, &last_char, 1);
|
||||
if(bytes_were_read != 1) break;
|
||||
|
||||
if(last_char != flipper_file_eoln) {
|
||||
if(!file_helper_write_eol(flipper_file->file)) break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
} while(false);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_open_always(FlipperFile* flipper_file, const char* filename) {
|
||||
furi_assert(flipper_file);
|
||||
bool result = storage_file_open(
|
||||
flipper_file->file, filename, FSAM_READ | FSAM_WRITE, FSOM_CREATE_ALWAYS);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_open_new(FlipperFile* flipper_file, const char* filename) {
|
||||
furi_assert(flipper_file);
|
||||
bool result =
|
||||
storage_file_open(flipper_file->file, filename, FSAM_READ | FSAM_WRITE, FSOM_CREATE_NEW);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_close(FlipperFile* flipper_file) {
|
||||
furi_assert(flipper_file);
|
||||
if(storage_file_is_open(flipper_file->file)) {
|
||||
return storage_file_close(flipper_file->file);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool flipper_file_rewind(FlipperFile* flipper_file) {
|
||||
furi_assert(flipper_file);
|
||||
return storage_file_seek(flipper_file->file, 0, true);
|
||||
}
|
||||
|
||||
bool flipper_file_read_header(FlipperFile* flipper_file, string_t filetype, uint32_t* version) {
|
||||
bool result = false;
|
||||
do {
|
||||
result = flipper_file_read_string(flipper_file, flipper_file_filetype_key, filetype);
|
||||
if(!result) break;
|
||||
result = flipper_file_read_uint32(flipper_file, flipper_file_version_key, version, 1);
|
||||
if(!result) break;
|
||||
} while(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_write_header(
|
||||
FlipperFile* flipper_file,
|
||||
string_t filetype,
|
||||
const uint32_t version) {
|
||||
bool result = false;
|
||||
do {
|
||||
result = flipper_file_write_string(flipper_file, flipper_file_filetype_key, filetype);
|
||||
if(!result) break;
|
||||
result = flipper_file_write_uint32(flipper_file, flipper_file_version_key, &version, 1);
|
||||
if(!result) break;
|
||||
} while(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_write_header_cstr(
|
||||
FlipperFile* flipper_file,
|
||||
const char* filetype,
|
||||
const uint32_t version) {
|
||||
bool result = false;
|
||||
string_t value;
|
||||
string_init_set(value, filetype);
|
||||
result = flipper_file_write_header(flipper_file, value, version);
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_get_value_count(FlipperFile* flipper_file, const char* key, uint32_t* count) {
|
||||
furi_assert(flipper_file);
|
||||
bool result = false;
|
||||
bool last = false;
|
||||
|
||||
string_t value;
|
||||
string_init(value);
|
||||
|
||||
uint32_t position = storage_file_tell(flipper_file->file);
|
||||
do {
|
||||
if(!flipper_file_seek_to_key(flipper_file->file, key, flipper_file->strict_mode)) break;
|
||||
|
||||
// Balance between speed and memory consumption
|
||||
// I prefer lower speed but less memory consumption
|
||||
*count = 0;
|
||||
|
||||
result = true;
|
||||
while(true) {
|
||||
if(!file_helper_read_value(flipper_file->file, value, &last)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
|
||||
*count = *count + 1;
|
||||
if(last) break;
|
||||
}
|
||||
|
||||
} while(false);
|
||||
|
||||
if(!storage_file_seek(flipper_file->file, position, true)) {
|
||||
result = false;
|
||||
}
|
||||
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_write_comment(FlipperFile* flipper_file, string_t data) {
|
||||
furi_assert(flipper_file);
|
||||
|
||||
bool result = false;
|
||||
do {
|
||||
const char comment_buffer[2] = {flipper_file_comment, ' '};
|
||||
result = file_helper_write(flipper_file->file, comment_buffer, sizeof(comment_buffer));
|
||||
if(!result) break;
|
||||
|
||||
result = file_helper_write(flipper_file->file, string_get_cstr(data), string_size(data));
|
||||
if(!result) break;
|
||||
|
||||
result = file_helper_write_eol(flipper_file->file);
|
||||
} while(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_write_comment_cstr(FlipperFile* flipper_file, const char* data) {
|
||||
bool result = false;
|
||||
string_t value;
|
||||
string_init_set(value, data);
|
||||
result = flipper_file_write_comment(flipper_file, value);
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_delete_key_and_call(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
flipper_file_cb call,
|
||||
const char* cb_key,
|
||||
const void* cb_data,
|
||||
const uint16_t cb_data_size) {
|
||||
bool result = false;
|
||||
File* scratch_file = storage_file_alloc(flipper_file->storage);
|
||||
|
||||
do {
|
||||
// get size
|
||||
uint64_t file_size = storage_file_size(flipper_file->file);
|
||||
if(file_size == 0) break;
|
||||
|
||||
if(!storage_file_seek(flipper_file->file, 0, true)) break;
|
||||
|
||||
// find key
|
||||
if(!flipper_file_seek_to_key(flipper_file->file, key, flipper_file->strict_mode)) break;
|
||||
// get key start position
|
||||
uint64_t start_position = storage_file_tell(flipper_file->file) - strlen(key);
|
||||
if(start_position >= 2) {
|
||||
start_position -= 2;
|
||||
} else {
|
||||
// something wrong
|
||||
break;
|
||||
}
|
||||
|
||||
// get value end position
|
||||
if(!file_helper_seek_to_next_line(flipper_file->file)) break;
|
||||
uint64_t end_position = storage_file_tell(flipper_file->file);
|
||||
// newline symbol
|
||||
if(end_position < file_size) {
|
||||
end_position += 1;
|
||||
}
|
||||
|
||||
// open scratchpad
|
||||
const char* scratch_name = "";
|
||||
if(!flipper_file_get_scratchpad_name(&scratch_name)) break;
|
||||
|
||||
if(!storage_file_open(
|
||||
scratch_file, scratch_name, FSAM_READ | FSAM_WRITE, FSOM_CREATE_ALWAYS))
|
||||
break;
|
||||
|
||||
// copy key file before key to scratchpad
|
||||
if(!file_helper_copy(flipper_file->file, scratch_file, 0, start_position)) break;
|
||||
|
||||
// do something in between if needed
|
||||
if(call != NULL) {
|
||||
if(!call(scratch_file, cb_key, cb_data, cb_data_size)) break;
|
||||
};
|
||||
|
||||
// copy key file after key value to scratchpad
|
||||
if(!file_helper_copy(flipper_file->file, scratch_file, end_position, file_size)) break;
|
||||
|
||||
file_size = storage_file_tell(scratch_file);
|
||||
if(file_size == 0) break;
|
||||
|
||||
if(!storage_file_seek(flipper_file->file, 0, true)) break;
|
||||
|
||||
// copy whole scratchpad file to the original file
|
||||
if(!file_helper_copy(scratch_file, flipper_file->file, 0, file_size)) break;
|
||||
|
||||
// and truncate original file
|
||||
if(!storage_file_truncate(flipper_file->file)) break;
|
||||
|
||||
// close and remove scratchpad file
|
||||
if(!storage_file_close(scratch_file)) break;
|
||||
if(storage_common_remove(flipper_file->storage, scratch_name) != FSE_OK) break;
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
storage_file_free(scratch_file);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_delete_key(FlipperFile* flipper_file, const char* key) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_delete_key_and_call(flipper_file, key, NULL, NULL, NULL, 0);
|
||||
}
|
||||
|
||||
bool flipper_file_write_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* _data,
|
||||
const uint16_t data_size,
|
||||
FlipperFileValueType type) {
|
||||
bool result = false;
|
||||
string_t value;
|
||||
string_init(value);
|
||||
|
||||
do {
|
||||
result = flipper_file_write_key(file, key);
|
||||
if(!result) break;
|
||||
|
||||
for(uint16_t i = 0; i < data_size; i++) {
|
||||
switch(type) {
|
||||
case FlipperFileValueHex: {
|
||||
const uint8_t* data = _data;
|
||||
string_printf(value, "%02X", data[i]);
|
||||
}; break;
|
||||
case FlipperFileValueFloat: {
|
||||
const float* data = _data;
|
||||
string_printf(value, "%f", data[i]);
|
||||
}; break;
|
||||
case FlipperFileValueInt32: {
|
||||
const int32_t* data = _data;
|
||||
string_printf(value, "%" PRIi32, data[i]);
|
||||
}; break;
|
||||
case FlipperFileValueUint32: {
|
||||
const uint32_t* data = _data;
|
||||
string_printf(value, "%" PRId32, data[i]);
|
||||
}; break;
|
||||
}
|
||||
|
||||
if((i + 1) < data_size) {
|
||||
string_cat(value, " ");
|
||||
}
|
||||
|
||||
result = file_helper_write(file, string_get_cstr(value), string_size(value));
|
||||
if(!result) break;
|
||||
}
|
||||
|
||||
result = file_helper_write_eol(file);
|
||||
} while(false);
|
||||
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_read_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
void* _data,
|
||||
const uint16_t data_size,
|
||||
bool strict_mode,
|
||||
FlipperFileValueType type) {
|
||||
bool result = false;
|
||||
string_t value;
|
||||
string_init(value);
|
||||
|
||||
if(flipper_file_seek_to_key(file, key, strict_mode)) {
|
||||
result = true;
|
||||
for(uint16_t i = 0; i < data_size; i++) {
|
||||
bool last = false;
|
||||
result = file_helper_read_value(file, value, &last);
|
||||
if(result) {
|
||||
int scan_values = 0;
|
||||
switch(type) {
|
||||
case FlipperFileValueHex: {
|
||||
uint8_t* data = _data;
|
||||
// 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 FlipperFileValueFloat: {
|
||||
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) {
|
||||
// very probably ok
|
||||
scan_values = 1;
|
||||
}
|
||||
}; break;
|
||||
case FlipperFileValueInt32: {
|
||||
int32_t* data = _data;
|
||||
scan_values = sscanf(string_get_cstr(value), "%" PRIi32, &data[i]);
|
||||
}; break;
|
||||
case FlipperFileValueUint32: {
|
||||
uint32_t* data = _data;
|
||||
scan_values = sscanf(string_get_cstr(value), "%" PRId32, &data[i]);
|
||||
}; break;
|
||||
}
|
||||
|
||||
if(scan_values != 1) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
if(last && ((i + 1) != data_size)) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
File* flipper_file_get_file(FlipperFile* flipper_file) {
|
||||
furi_assert(flipper_file);
|
||||
furi_assert(flipper_file->file);
|
||||
|
||||
return flipper_file->file;
|
||||
}
|
||||
@@ -1,465 +0,0 @@
|
||||
/**
|
||||
* @file flipper-file.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:
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
* FlipperFile file = flipper_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_file_open_new(file, "/ext/flipper_file_test")) break;
|
||||
* if(!flipper_file_write_header_cstr(file, "Flipper Test File", version)) break;
|
||||
* if(!flipper_file_write_comment_cstr(file, "Just test file")) break;
|
||||
* if(!flipper_file_write_string_cstr(file, "String", string_value)) break;
|
||||
* if(!flipper_file_flipper_file_write_uint32(file, "UINT", &uint32_value, 1)) break;
|
||||
* if(!flipper_file_write_hex(file, "Hex Array", array, array_size)) break;
|
||||
*
|
||||
* // signal that the file was written successfully
|
||||
* } while(0);
|
||||
*
|
||||
* flipper_file_close(file);
|
||||
* flipper_file_free(file);
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
*
|
||||
* Reading:
|
||||
*
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
* FlipperFile file = flipper_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_file_open_existing(file, "/ext/flipper_file_test")) break;
|
||||
* if(!flipper_file_read_header(file, file_type, &version)) break;
|
||||
* if(!flipper_file_read_string(file, "String", string_value)) break;
|
||||
* if(!flipper_file_read_uint32(file, "UINT", &uint32_value, 1)) break;
|
||||
* if(!flipper_file_read_hex(file, "Hex Array", array, array_size)) break;
|
||||
*
|
||||
* // signal that the file was read successfully
|
||||
* } while(0);
|
||||
*
|
||||
* flipper_file_close(file);
|
||||
* flipper_file_free(file);
|
||||
* ~~~~~~~~~~~~~~~~~~~~~
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** FlipperFile type anonymous structure. */
|
||||
typedef struct FlipperFile FlipperFile;
|
||||
|
||||
/**
|
||||
* Allocate FlipperFile.
|
||||
* @param storage storage api
|
||||
* @return FlipperFile* Pointer to a FlipperFile instance
|
||||
*/
|
||||
FlipperFile* flipper_file_alloc(Storage* storage);
|
||||
|
||||
/**
|
||||
* Free FlipperFile.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
*/
|
||||
void flipper_file_free(FlipperFile* flipper_file);
|
||||
|
||||
/**
|
||||
* Free FlipperFile.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param strict_mode True obligates not to skip valid fields. False by default.
|
||||
*/
|
||||
void flipper_file_set_strict_mode(FlipperFile* flipper_file, bool strict_mode);
|
||||
|
||||
/**
|
||||
* Open existing file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filename File name and path
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_open_existing(FlipperFile* flipper_file, const char* filename);
|
||||
|
||||
/**
|
||||
* Open existing file for writing and add values to the end of file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filename File name and path
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_open_append(FlipperFile* flipper_file, const char* filename);
|
||||
|
||||
/**
|
||||
* Open file. Creates a new file, or deletes the contents of the file if it already exists.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filename File name and path
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_open_always(FlipperFile* flipper_file, const char* filename);
|
||||
|
||||
/**
|
||||
* Open file. Creates a new file, fails if file already exists.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filename File name and path
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_open_new(FlipperFile* flipper_file, const char* filename);
|
||||
|
||||
/**
|
||||
* Close the file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_close(FlipperFile* flipper_file);
|
||||
|
||||
/**
|
||||
* Rewind the file RW pointer.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_rewind(FlipperFile* flipper_file);
|
||||
|
||||
/**
|
||||
* Read the header (file type and version) from the file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filetype File type string
|
||||
* @param version Version Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_read_header(FlipperFile* flipper_file, string_t filetype, uint32_t* version);
|
||||
|
||||
/**
|
||||
* Write the header (file type and version) to the file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filetype File type string
|
||||
* @param version Version Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_header(
|
||||
FlipperFile* flipper_file,
|
||||
string_t filetype,
|
||||
const uint32_t version);
|
||||
|
||||
/**
|
||||
* Write the header (file type and version) to the file. Plain C string version.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param filetype File type string
|
||||
* @param version Version Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_header_cstr(
|
||||
FlipperFile* flipper_file,
|
||||
const char* filetype,
|
||||
const uint32_t version);
|
||||
|
||||
/**
|
||||
* Get the count of values by key
|
||||
* @param flipper_file
|
||||
* @param key
|
||||
* @param count
|
||||
* @return bool
|
||||
*/
|
||||
bool flipper_file_get_value_count(FlipperFile* flipper_file, const char* key, uint32_t* count);
|
||||
|
||||
/**
|
||||
* Read a string from a file by Key
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_read_string(FlipperFile* flipper_file, const char* key, string_t data);
|
||||
|
||||
/**
|
||||
* Write key and string to file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_string(FlipperFile* flipper_file, const char* key, string_t data);
|
||||
|
||||
/**
|
||||
* Write key and string to file. Plain C string version.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_string_cstr(FlipperFile* flipper_file, const char* key, const char* data);
|
||||
|
||||
/**
|
||||
* Read array of uint32 from a file by Key
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_read_uint32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
uint32_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Write key and array of uint32 to file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_uint32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint32_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Read array of int32 from a file by Key
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_read_int32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
int32_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Write key and array of int32 to file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_int32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const int32_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Read array of float from a file by Key
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_read_float(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
float* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Write key and array of float to file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_float(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const float* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Read hex array from a file by Key
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Value size
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_read_hex(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
uint8_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Write key and hex array to file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_hex(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint8_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Write comment to file.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param data Comment text
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_comment(FlipperFile* flipper_file, string_t data);
|
||||
|
||||
/**
|
||||
* Write comment to file. Plain C string version.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param data Comment text
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_write_comment_cstr(FlipperFile* flipper_file, const char* data);
|
||||
|
||||
/**
|
||||
* Removes the first matching key and its value from the file. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_delete_key(FlipperFile* flipper_file, const char* key);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a string value. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_update_string(FlipperFile* flipper_file, const char* key, string_t data);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a string value. Plain C version. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_update_string_cstr(FlipperFile* flipper_file, const char* key, const char* data);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a uint32 array value. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_update_uint32(
|
||||
FlipperFile* flipper_file,
|
||||
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. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_update_int32(
|
||||
FlipperFile* flipper_file,
|
||||
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. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_update_float(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const float* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/**
|
||||
* Updates the value of the first matching key to a hex array value. Changes the RW pointer to an undefined position.
|
||||
* @param flipper_file Pointer to a FlipperFile instance
|
||||
* @param key Key
|
||||
* @param data Value
|
||||
* @param data_size Values count
|
||||
* @return True on success
|
||||
*/
|
||||
bool flipper_file_update_hex(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint8_t* data,
|
||||
const uint16_t data_size);
|
||||
|
||||
/** Get file descriptor.
|
||||
*
|
||||
* We higly don't recommend to use it.
|
||||
* This instance is owned by FlipperFile.
|
||||
* @param flipper_file
|
||||
* @return File*
|
||||
*/
|
||||
File* flipper_file_get_file(FlipperFile* flipper_file);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,42 +0,0 @@
|
||||
#include <furi.h>
|
||||
|
||||
#include "flipper_file.h"
|
||||
#include "flipper_file_i.h"
|
||||
#include "flipper_file_helper.h"
|
||||
|
||||
static bool flipper_file_write_float_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* _data,
|
||||
const uint16_t data_size) {
|
||||
return flipper_file_write_internal(file, key, _data, data_size, FlipperFileValueFloat);
|
||||
};
|
||||
|
||||
bool flipper_file_read_float(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
float* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_read_internal(
|
||||
flipper_file->file, key, data, data_size, flipper_file->strict_mode, FlipperFileValueFloat);
|
||||
}
|
||||
|
||||
bool flipper_file_write_float(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const float* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_write_float_internal(flipper_file->file, key, data, data_size);
|
||||
}
|
||||
|
||||
bool flipper_file_update_float(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const float* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_delete_key_and_call(
|
||||
flipper_file, key, flipper_file_write_float_internal, key, data, data_size);
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
#include "flipper_file_helper.h"
|
||||
|
||||
const char* const flipper_file_filetype_key = "Filetype";
|
||||
const char* const flipper_file_version_key = "Version";
|
||||
const char flipper_file_delimiter = ':';
|
||||
const char flipper_file_comment = '#';
|
||||
|
||||
#ifdef __linux__
|
||||
const char* flipper_file_scratchpad = ".scratch.pad";
|
||||
#else
|
||||
const char* flipper_file_scratchpad = "/any/.scratch.pad";
|
||||
#endif
|
||||
|
||||
bool flipper_file_read_valid_key(File* file, string_t key) {
|
||||
string_reset(key);
|
||||
bool found = false;
|
||||
bool error = false;
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
bool accumulate = true;
|
||||
bool new_line = true;
|
||||
|
||||
while(true) {
|
||||
uint16_t bytes_were_read = storage_file_read(file, buffer, buffer_size);
|
||||
if(bytes_were_read == 0) break;
|
||||
|
||||
for(uint16_t i = 0; i < bytes_were_read; i++) {
|
||||
if(buffer[i] == flipper_file_eoln) {
|
||||
// EOL found, clean data, start accumulating data and set the new_line flag
|
||||
string_reset(key);
|
||||
accumulate = true;
|
||||
new_line = true;
|
||||
} else if(buffer[i] == flipper_file_eolr) {
|
||||
// Ignore
|
||||
} else if(buffer[i] == flipper_file_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(buffer[i] == flipper_file_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 correct location
|
||||
// and signal that we have found something
|
||||
if(!file_helper_seek(file, i - bytes_were_read)) {
|
||||
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, buffer[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(found || error) break;
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
bool flipper_file_seek_to_key(File* file, const char* key, bool strict_mode) {
|
||||
bool found = false;
|
||||
string_t readed_key;
|
||||
|
||||
string_init(readed_key);
|
||||
|
||||
while(!storage_file_eof(file)) {
|
||||
if(flipper_file_read_valid_key(file, readed_key)) {
|
||||
if(string_cmp_str(readed_key, key) == 0) {
|
||||
if(!file_helper_seek(file, 2)) break;
|
||||
|
||||
found = true;
|
||||
break;
|
||||
} else if(strict_mode) {
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
string_clear(readed_key);
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
bool flipper_file_write_key(File* file, const char* key) {
|
||||
bool result = false;
|
||||
|
||||
do {
|
||||
result = file_helper_write(file, key, strlen(key));
|
||||
if(!result) break;
|
||||
|
||||
const char delimiter_buffer[2] = {flipper_file_delimiter, ' '};
|
||||
result = file_helper_write(file, delimiter_buffer, sizeof(delimiter_buffer));
|
||||
} while(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_get_scratchpad_name(const char** name) {
|
||||
// TODO do not rewrite existing file
|
||||
*name = flipper_file_scratchpad;
|
||||
return true;
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include <storage/storage.h>
|
||||
#include "file_helper.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern const char* const flipper_file_filetype_key;
|
||||
extern const char* const flipper_file_version_key;
|
||||
extern const char flipper_file_delimiter;
|
||||
extern const char flipper_file_comment;
|
||||
|
||||
/**
|
||||
* Reads a valid key from a file as a string.
|
||||
* After reading, the rw pointer will be on the flipper_file_delimiter symbol.
|
||||
* Optimized not to read comments and values into RAM.
|
||||
* @param file
|
||||
* @param key
|
||||
* @return true on success read
|
||||
*/
|
||||
bool flipper_file_read_valid_key(File* file, string_t key);
|
||||
|
||||
/**
|
||||
* Sets rw pointer to the data after the key
|
||||
* @param file
|
||||
* @param key
|
||||
* @param strict
|
||||
* @return true if key was found
|
||||
*/
|
||||
bool flipper_file_seek_to_key(File* file, const char* key, bool strict);
|
||||
|
||||
/**
|
||||
* Write key and key delimiter
|
||||
* @param file
|
||||
* @param key
|
||||
* @return bool
|
||||
*/
|
||||
bool flipper_file_write_key(File* file, const char* key);
|
||||
|
||||
/**
|
||||
* Get scratchpad name and path
|
||||
* @param name
|
||||
* @return bool
|
||||
*/
|
||||
bool flipper_file_get_scratchpad_name(const char** name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,42 +0,0 @@
|
||||
#include <furi.h>
|
||||
|
||||
#include "flipper_file.h"
|
||||
#include "flipper_file_i.h"
|
||||
#include "flipper_file_helper.h"
|
||||
|
||||
static bool flipper_file_write_hex_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* _data,
|
||||
const uint16_t data_size) {
|
||||
return flipper_file_write_internal(file, key, _data, data_size, FlipperFileValueHex);
|
||||
};
|
||||
|
||||
bool flipper_file_write_hex(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint8_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_write_hex_internal(flipper_file->file, key, data, data_size);
|
||||
}
|
||||
|
||||
bool flipper_file_read_hex(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
uint8_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_read_internal(
|
||||
flipper_file->file, key, data, data_size, flipper_file->strict_mode, FlipperFileValueHex);
|
||||
}
|
||||
|
||||
bool flipper_file_update_hex(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint8_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_delete_key_and_call(
|
||||
flipper_file, key, flipper_file_write_hex_internal, key, data, data_size);
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
struct FlipperFile {
|
||||
File* file;
|
||||
Storage* storage;
|
||||
bool strict_mode;
|
||||
};
|
||||
|
||||
/**
|
||||
* Value write type callback
|
||||
*/
|
||||
typedef bool (*flipper_file_cb)(File* file, const char* key, const void* data, uint16_t data_size);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param flipper_file
|
||||
* @param key
|
||||
* @param cb
|
||||
* @param cb_key
|
||||
* @param cb_data
|
||||
* @param cb_data_size
|
||||
* @return bool
|
||||
*/
|
||||
bool flipper_file_delete_key_and_call(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
flipper_file_cb cb,
|
||||
const char* cb_key,
|
||||
const void* cb_data,
|
||||
const uint16_t cb_data_size);
|
||||
|
||||
/**
|
||||
* Value types
|
||||
*/
|
||||
typedef enum {
|
||||
FlipperFileValueHex,
|
||||
FlipperFileValueFloat,
|
||||
FlipperFileValueInt32,
|
||||
FlipperFileValueUint32,
|
||||
} FlipperFileValueType;
|
||||
|
||||
/**
|
||||
* Internal write values function
|
||||
* @param file
|
||||
* @param key
|
||||
* @param _data
|
||||
* @param data_size
|
||||
* @param type
|
||||
* @return bool
|
||||
*/
|
||||
bool flipper_file_write_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* _data,
|
||||
const uint16_t data_size,
|
||||
FlipperFileValueType type);
|
||||
|
||||
/**
|
||||
* Internal read values function
|
||||
* @param file
|
||||
* @param key
|
||||
* @param _data
|
||||
* @param data_size
|
||||
* @param type
|
||||
* @return bool
|
||||
*/
|
||||
bool flipper_file_read_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
void* _data,
|
||||
const uint16_t data_size,
|
||||
bool strict_mode,
|
||||
FlipperFileValueType type);
|
||||
@@ -1,42 +0,0 @@
|
||||
#include <furi.h>
|
||||
|
||||
#include "flipper_file.h"
|
||||
#include "flipper_file_i.h"
|
||||
#include "flipper_file_helper.h"
|
||||
|
||||
static bool flipper_file_write_int32_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* _data,
|
||||
const uint16_t data_size) {
|
||||
return flipper_file_write_internal(file, key, _data, data_size, FlipperFileValueInt32);
|
||||
};
|
||||
|
||||
bool flipper_file_read_int32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
int32_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_read_internal(
|
||||
flipper_file->file, key, data, data_size, flipper_file->strict_mode, FlipperFileValueInt32);
|
||||
}
|
||||
|
||||
bool flipper_file_write_int32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const int32_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_write_int32_internal(flipper_file->file, key, data, data_size);
|
||||
}
|
||||
|
||||
bool flipper_file_update_int32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const int32_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_delete_key_and_call(
|
||||
flipper_file, key, flipper_file_write_int32_internal, key, data, data_size);
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
#include <furi.h>
|
||||
|
||||
#include "flipper_file.h"
|
||||
#include "flipper_file_i.h"
|
||||
#include "flipper_file_helper.h"
|
||||
|
||||
static bool flipper_file_write_string_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* data,
|
||||
const uint16_t data_size) {
|
||||
bool result = false;
|
||||
(void)data_size;
|
||||
|
||||
do {
|
||||
result = flipper_file_write_key(file, key);
|
||||
if(!result) break;
|
||||
|
||||
result = file_helper_write(file, string_get_cstr(data), string_size(data));
|
||||
if(!result) break;
|
||||
|
||||
result = file_helper_write_eol(file);
|
||||
} while(false);
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
bool flipper_file_read_string(FlipperFile* flipper_file, const char* key, string_t data) {
|
||||
furi_assert(flipper_file);
|
||||
|
||||
bool result = false;
|
||||
if(flipper_file_seek_to_key(flipper_file->file, key, flipper_file->strict_mode)) {
|
||||
if(file_helper_read_line(flipper_file->file, data)) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_write_string(FlipperFile* flipper_file, const char* key, string_t data) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_write_string_internal(flipper_file->file, key, data, 0);
|
||||
}
|
||||
|
||||
bool flipper_file_write_string_cstr(FlipperFile* flipper_file, const char* key, const char* data) {
|
||||
bool result = false;
|
||||
string_t value;
|
||||
string_init_set(value, data);
|
||||
result = flipper_file_write_string(flipper_file, key, value);
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool flipper_file_update_string(FlipperFile* flipper_file, const char* key, string_t data) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_delete_key_and_call(
|
||||
flipper_file, key, flipper_file_write_string_internal, key, data, 0);
|
||||
}
|
||||
|
||||
bool flipper_file_update_string_cstr(FlipperFile* flipper_file, const char* key, const char* data) {
|
||||
bool result = false;
|
||||
string_t value;
|
||||
string_init_set(value, data);
|
||||
result = flipper_file_update_string(flipper_file, key, value);
|
||||
string_clear(value);
|
||||
return result;
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
#include <furi.h>
|
||||
|
||||
#include "flipper_file.h"
|
||||
#include "flipper_file_i.h"
|
||||
#include "flipper_file_helper.h"
|
||||
|
||||
static bool flipper_file_write_uint32_internal(
|
||||
File* file,
|
||||
const char* key,
|
||||
const void* _data,
|
||||
const uint16_t data_size) {
|
||||
return flipper_file_write_internal(file, key, _data, data_size, FlipperFileValueUint32);
|
||||
};
|
||||
|
||||
bool flipper_file_read_uint32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
uint32_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_read_internal(
|
||||
flipper_file->file,
|
||||
key,
|
||||
data,
|
||||
data_size,
|
||||
flipper_file->strict_mode,
|
||||
FlipperFileValueUint32);
|
||||
}
|
||||
|
||||
bool flipper_file_write_uint32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint32_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_write_uint32_internal(flipper_file->file, key, data, data_size);
|
||||
}
|
||||
|
||||
bool flipper_file_update_uint32(
|
||||
FlipperFile* flipper_file,
|
||||
const char* key,
|
||||
const uint32_t* data,
|
||||
const uint16_t data_size) {
|
||||
furi_assert(flipper_file);
|
||||
return flipper_file_delete_key_and_call(
|
||||
flipper_file, key, flipper_file_write_uint32_internal, key, data, data_size);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
#if HEATSHRINK_DYNAMIC_ALLOC
|
||||
/* Optional replacement of malloc/free */
|
||||
#define HEATSHRINK_MALLOC(SZ) furi_alloc(SZ)
|
||||
#define HEATSHRINK_MALLOC(SZ) malloc(SZ)
|
||||
#define HEATSHRINK_FREE(P, SZ) free(P)
|
||||
#else
|
||||
/* Required parameters for static configuration */
|
||||
|
||||
@@ -283,7 +283,7 @@ void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
|
||||
|
||||
uint32_t alloc_size = sizeof(IrdaCommonDecoder) + protocol->databit_len[0] / 8 +
|
||||
!!(protocol->databit_len[0] % 8);
|
||||
IrdaCommonDecoder* decoder = furi_alloc(alloc_size);
|
||||
IrdaCommonDecoder* decoder = malloc(alloc_size);
|
||||
decoder->protocol = protocol;
|
||||
decoder->level = true;
|
||||
return decoder;
|
||||
|
||||
@@ -148,7 +148,7 @@ void* irda_common_encoder_alloc(const IrdaCommonProtocolSpec* protocol) {
|
||||
|
||||
uint32_t alloc_size = sizeof(IrdaCommonDecoder) + protocol->databit_len[0] / 8 +
|
||||
!!(protocol->databit_len[0] % 8);
|
||||
IrdaCommonEncoder* encoder = furi_alloc(alloc_size);
|
||||
IrdaCommonEncoder* encoder = malloc(alloc_size);
|
||||
memset(encoder, 0, alloc_size);
|
||||
encoder->protocol = protocol;
|
||||
|
||||
|
||||
@@ -134,8 +134,8 @@ const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t
|
||||
}
|
||||
|
||||
IrdaDecoderHandler* irda_alloc_decoder(void) {
|
||||
IrdaDecoderHandler* handler = furi_alloc(sizeof(IrdaDecoderHandler));
|
||||
handler->ctx = furi_alloc(sizeof(void*) * COUNT_OF(irda_encoder_decoder));
|
||||
IrdaDecoderHandler* handler = malloc(sizeof(IrdaDecoderHandler));
|
||||
handler->ctx = malloc(sizeof(void*) * COUNT_OF(irda_encoder_decoder));
|
||||
|
||||
for(int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
|
||||
handler->ctx[i] = 0;
|
||||
@@ -186,7 +186,7 @@ const IrdaMessage* irda_check_decoder_ready(IrdaDecoderHandler* handler) {
|
||||
}
|
||||
|
||||
IrdaEncoderHandler* irda_alloc_encoder(void) {
|
||||
IrdaEncoderHandler* handler = furi_alloc(sizeof(IrdaEncoderHandler));
|
||||
IrdaEncoderHandler* handler = malloc(sizeof(IrdaEncoderHandler));
|
||||
handler->handler = NULL;
|
||||
handler->encoder = NULL;
|
||||
return handler;
|
||||
|
||||
@@ -58,7 +58,7 @@ bool irda_decoder_rc5_interpret(IrdaCommonDecoder* decoder) {
|
||||
}
|
||||
|
||||
void* irda_decoder_rc5_alloc(void) {
|
||||
IrdaRc5Decoder* decoder = furi_alloc(sizeof(IrdaRc5Decoder));
|
||||
IrdaRc5Decoder* decoder = malloc(sizeof(IrdaRc5Decoder));
|
||||
decoder->toggle = false;
|
||||
decoder->common_decoder = irda_common_decoder_alloc(&protocol_rc5);
|
||||
decoder->common_decoder->context = decoder;
|
||||
|
||||
@@ -40,7 +40,7 @@ IrdaStatus irda_encoder_rc5_encode(void* encoder_ptr, uint32_t* duration, bool*
|
||||
}
|
||||
|
||||
void* irda_encoder_rc5_alloc(void) {
|
||||
IrdaEncoderRC5* encoder = furi_alloc(sizeof(IrdaEncoderRC5));
|
||||
IrdaEncoderRC5* encoder = malloc(sizeof(IrdaEncoderRC5));
|
||||
encoder->common_encoder = irda_common_encoder_alloc(&protocol_rc5);
|
||||
encoder->toggle_bit = false;
|
||||
return encoder;
|
||||
|
||||
@@ -89,7 +89,7 @@ IrdaStatus
|
||||
}
|
||||
|
||||
void* irda_decoder_rc6_alloc(void) {
|
||||
IrdaRc6Decoder* decoder = furi_alloc(sizeof(IrdaRc6Decoder));
|
||||
IrdaRc6Decoder* decoder = malloc(sizeof(IrdaRc6Decoder));
|
||||
decoder->toggle = false;
|
||||
decoder->common_decoder = irda_common_decoder_alloc(&protocol_rc6);
|
||||
decoder->common_decoder->context = decoder;
|
||||
|
||||
@@ -34,7 +34,7 @@ IrdaStatus irda_encoder_rc6_encode(void* encoder_ptr, uint32_t* duration, bool*
|
||||
}
|
||||
|
||||
void* irda_encoder_rc6_alloc(void) {
|
||||
IrdaEncoderRC6* encoder = furi_alloc(sizeof(IrdaEncoderRC6));
|
||||
IrdaEncoderRC6* encoder = malloc(sizeof(IrdaEncoderRC6));
|
||||
encoder->common_encoder = irda_common_encoder_alloc(&protocol_rc6);
|
||||
encoder->toggle_bit = false;
|
||||
return encoder;
|
||||
|
||||
@@ -218,7 +218,7 @@ void irda_worker_rx_set_received_signal_callback(
|
||||
}
|
||||
|
||||
IrdaWorker* irda_worker_alloc() {
|
||||
IrdaWorker* instance = furi_alloc(sizeof(IrdaWorker));
|
||||
IrdaWorker* instance = malloc(sizeof(IrdaWorker));
|
||||
|
||||
instance->thread = furi_thread_alloc();
|
||||
furi_thread_set_name(instance->thread, "IrdaWorker");
|
||||
|
||||
+6
-4
@@ -82,10 +82,6 @@ CFLAGS += -I$(LIB_DIR)/common-api
|
||||
CFLAGS += -I$(LIB_DIR)/drivers
|
||||
C_SOURCES += $(wildcard $(LIB_DIR)/drivers/*.c)
|
||||
|
||||
#file reader
|
||||
CFLAGS += -I$(LIB_DIR)/file_reader
|
||||
CPP_SOURCES += $(wildcard $(LIB_DIR)/file_reader/*.cpp)
|
||||
|
||||
# IR lib
|
||||
CFLAGS += -I$(LIB_DIR)/irda/encoder_decoder
|
||||
CFLAGS += -I$(LIB_DIR)/irda/worker
|
||||
@@ -105,7 +101,9 @@ CPP_SOURCES += $(wildcard $(LIB_DIR)/app-scened-template/*/*.cpp)
|
||||
|
||||
# Toolbox
|
||||
C_SOURCES += $(wildcard $(LIB_DIR)/toolbox/*.c)
|
||||
C_SOURCES += $(wildcard $(LIB_DIR)/toolbox/*/*.c)
|
||||
CPP_SOURCES += $(wildcard $(LIB_DIR)/toolbox/*.cpp)
|
||||
CPP_SOURCES += $(wildcard $(LIB_DIR)/toolbox/*/*.cpp)
|
||||
|
||||
# USB Stack
|
||||
CFLAGS += -I$(LIB_DIR)/libusb_stm32/inc
|
||||
@@ -123,6 +121,10 @@ C_SOURCES += $(wildcard $(LIB_DIR)/heatshrink/*.c)
|
||||
CFLAGS += -I$(LIB_DIR)/flipper_file
|
||||
C_SOURCES += $(wildcard $(LIB_DIR)/flipper_file/*.c)
|
||||
|
||||
# Flipper format
|
||||
CFLAGS += -I$(LIB_DIR)/flipper_format
|
||||
C_SOURCES += $(wildcard $(LIB_DIR)/flipper_format/*.c)
|
||||
|
||||
# Micro-ECC
|
||||
CFLAGS += -I$(LIB_DIR)/micro-ecc
|
||||
C_SOURCES += $(wildcard $(LIB_DIR)/micro-ecc/*.c)
|
||||
|
||||
@@ -19,7 +19,7 @@ typedef enum {
|
||||
} CameDecoderStep;
|
||||
|
||||
SubGhzProtocolCame* subghz_protocol_came_alloc() {
|
||||
SubGhzProtocolCame* instance = furi_alloc(sizeof(SubGhzProtocolCame));
|
||||
SubGhzProtocolCame* instance = malloc(sizeof(SubGhzProtocolCame));
|
||||
|
||||
instance->common.name = "CAME";
|
||||
instance->common.code_min_count_bit_for_found = 12;
|
||||
@@ -167,16 +167,16 @@ void subghz_protocol_came_to_str(SubGhzProtocolCame* instance, string_t output)
|
||||
code_found_reverse_lo);
|
||||
}
|
||||
|
||||
bool subghz_protocol_came_to_save_file(SubGhzProtocolCame* instance, FlipperFile* flipper_file) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_file);
|
||||
bool subghz_protocol_came_to_save_file(SubGhzProtocolCame* instance, FlipperFormat* flipper_format) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
bool subghz_protocol_came_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolCame* instance,
|
||||
const char* file_path) {
|
||||
return subghz_protocol_common_to_load_protocol_from_file(
|
||||
(SubGhzProtocolCommon*)instance, flipper_file);
|
||||
(SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
void subghz_decoder_came_to_load_protocol(SubGhzProtocolCame* instance, void* context) {
|
||||
|
||||
@@ -48,20 +48,20 @@ void subghz_protocol_came_to_str(SubGhzProtocolCame* instance, string_t output);
|
||||
/** Adding data to a file
|
||||
*
|
||||
* @param instance - SubGhzProtocolCame instance
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_came_to_save_file(SubGhzProtocolCame* instance, FlipperFile* flipper_file);
|
||||
bool subghz_protocol_came_to_save_file(SubGhzProtocolCame* instance, FlipperFormat* flipper_format);
|
||||
|
||||
/** Loading protocol from file
|
||||
*
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @param instance - SubGhzProtocolCame instance
|
||||
* @param file_path - file path
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_came_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolCame* instance,
|
||||
const char* file_path);
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ typedef enum {
|
||||
} CameAtomoDecoderStep;
|
||||
|
||||
SubGhzProtocolCameAtomo* subghz_protocol_came_atomo_alloc() {
|
||||
SubGhzProtocolCameAtomo* instance = furi_alloc(sizeof(SubGhzProtocolCameAtomo));
|
||||
SubGhzProtocolCameAtomo* instance = malloc(sizeof(SubGhzProtocolCameAtomo));
|
||||
|
||||
instance->common.name = "CAME Atomo";
|
||||
instance->common.code_min_count_bit_for_found = 62;
|
||||
|
||||
@@ -27,7 +27,7 @@ typedef enum {
|
||||
} CameTweeDecoderStep;
|
||||
|
||||
SubGhzProtocolCameTwee* subghz_protocol_came_twee_alloc() {
|
||||
SubGhzProtocolCameTwee* instance = furi_alloc(sizeof(SubGhzProtocolCameTwee));
|
||||
SubGhzProtocolCameTwee* instance = malloc(sizeof(SubGhzProtocolCameTwee));
|
||||
|
||||
instance->common.name = "CAME TWEE";
|
||||
instance->common.code_min_count_bit_for_found = 54;
|
||||
@@ -327,16 +327,16 @@ void subghz_protocol_came_twee_to_str(SubGhzProtocolCameTwee* instance, string_t
|
||||
|
||||
bool subghz_protocol_came_twee_to_save_file(
|
||||
SubGhzProtocolCameTwee* instance,
|
||||
FlipperFile* flipper_file) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_file);
|
||||
FlipperFormat* flipper_format) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
bool subghz_protocol_came_twee_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolCameTwee* instance,
|
||||
const char* file_path) {
|
||||
if(subghz_protocol_common_to_load_protocol_from_file(
|
||||
(SubGhzProtocolCommon*)instance, flipper_file)) {
|
||||
(SubGhzProtocolCommon*)instance, flipper_format)) {
|
||||
subghz_protocol_came_twee_remote_controller(instance);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -51,22 +51,22 @@ void subghz_protocol_came_twee_to_str(SubGhzProtocolCameTwee* instance, string_t
|
||||
/** Adding data to a file
|
||||
*
|
||||
* @param instance - SubGhzProtocolCameTwee instance
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_came_twee_to_save_file(
|
||||
SubGhzProtocolCameTwee* instance,
|
||||
FlipperFile* flipper_file);
|
||||
FlipperFormat* flipper_format);
|
||||
|
||||
/** Loading protocol from file
|
||||
*
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @param instance - SubGhzProtocolCameTwee instance
|
||||
* @param file_path - file path
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_came_twee_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolCameTwee* instance,
|
||||
const char* file_path);
|
||||
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
#include <lib/toolbox/hex.h>
|
||||
|
||||
SubGhzProtocolCommonEncoder* subghz_protocol_encoder_common_alloc() {
|
||||
SubGhzProtocolCommonEncoder* instance = furi_alloc(sizeof(SubGhzProtocolCommonEncoder));
|
||||
instance->upload = furi_alloc(SUBGHZ_ENCODER_UPLOAD_MAX_SIZE * sizeof(LevelDuration));
|
||||
SubGhzProtocolCommonEncoder* instance = malloc(sizeof(SubGhzProtocolCommonEncoder));
|
||||
instance->upload = malloc(SUBGHZ_ENCODER_UPLOAD_MAX_SIZE * sizeof(LevelDuration));
|
||||
instance->start = true;
|
||||
instance->repeat = 10; //default number of repeat
|
||||
return instance;
|
||||
@@ -169,17 +169,19 @@ bool subghz_protocol_common_read_hex(string_t str, uint8_t* buff, uint16_t len)
|
||||
return parsed;
|
||||
}
|
||||
|
||||
bool subghz_protocol_common_to_save_file(SubGhzProtocolCommon* instance, FlipperFile* flipper_file) {
|
||||
bool subghz_protocol_common_to_save_file(
|
||||
SubGhzProtocolCommon* instance,
|
||||
FlipperFormat* flipper_format) {
|
||||
furi_assert(instance);
|
||||
furi_assert(flipper_file);
|
||||
furi_assert(flipper_format);
|
||||
bool res = false;
|
||||
do {
|
||||
if(!flipper_file_write_string_cstr(flipper_file, "Protocol", instance->name)) {
|
||||
if(!flipper_format_write_string_cstr(flipper_format, "Protocol", instance->name)) {
|
||||
FURI_LOG_E(SUBGHZ_PARSER_TAG, "Unable to add Protocol");
|
||||
break;
|
||||
}
|
||||
uint32_t temp = instance->code_last_count_bit;
|
||||
if(!flipper_file_write_uint32(flipper_file, "Bit", &temp, 1)) {
|
||||
if(!flipper_format_write_uint32(flipper_format, "Bit", &temp, 1)) {
|
||||
FURI_LOG_E(SUBGHZ_PARSER_TAG, "Unable to add Bit");
|
||||
break;
|
||||
}
|
||||
@@ -189,7 +191,7 @@ bool subghz_protocol_common_to_save_file(SubGhzProtocolCommon* instance, Flipper
|
||||
key_data[sizeof(uint64_t) - i - 1] = (instance->code_last_found >> i * 8) & 0xFF;
|
||||
}
|
||||
|
||||
if(!flipper_file_write_hex(flipper_file, "Key", key_data, sizeof(uint64_t))) {
|
||||
if(!flipper_format_write_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
|
||||
FURI_LOG_E(SUBGHZ_PARSER_TAG, "Unable to add Key");
|
||||
break;
|
||||
}
|
||||
@@ -201,23 +203,23 @@ bool subghz_protocol_common_to_save_file(SubGhzProtocolCommon* instance, Flipper
|
||||
|
||||
bool subghz_protocol_common_to_load_protocol_from_file(
|
||||
SubGhzProtocolCommon* instance,
|
||||
FlipperFile* flipper_file) {
|
||||
FlipperFormat* flipper_format) {
|
||||
furi_assert(instance);
|
||||
furi_assert(flipper_file);
|
||||
furi_assert(flipper_format);
|
||||
bool loaded = false;
|
||||
string_t temp_str;
|
||||
string_init(temp_str);
|
||||
uint32_t temp_data = 0;
|
||||
|
||||
do {
|
||||
if(!flipper_file_read_uint32(flipper_file, "Bit", (uint32_t*)&temp_data, 1)) {
|
||||
if(!flipper_format_read_uint32(flipper_format, "Bit", (uint32_t*)&temp_data, 1)) {
|
||||
FURI_LOG_E(SUBGHZ_PARSER_TAG, "Missing Bit");
|
||||
break;
|
||||
}
|
||||
instance->code_last_count_bit = (uint8_t)temp_data;
|
||||
|
||||
uint8_t key_data[sizeof(uint64_t)] = {0};
|
||||
if(!flipper_file_read_hex(flipper_file, "Key", key_data, sizeof(uint64_t))) {
|
||||
if(!flipper_format_read_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
|
||||
FURI_LOG_E(SUBGHZ_PARSER_TAG, "Missing Key");
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <m-string.h>
|
||||
#include <furi_hal.h>
|
||||
#include <stdint.h>
|
||||
#include <lib/flipper_file/flipper_file.h>
|
||||
#include <flipper_format/flipper_format.h>
|
||||
|
||||
#define bit_read(value, bit) (((value) >> (bit)) & 0x01)
|
||||
#define bit_set(value, bit) ((value) |= (1UL << (bit)))
|
||||
@@ -43,11 +43,11 @@ typedef void (*SubGhzProtocolCommonToStr)(SubGhzProtocolCommon* instance, string
|
||||
|
||||
//Get string to save
|
||||
typedef bool (
|
||||
*SubGhzProtocolCommonSaveFile)(SubGhzProtocolCommon* instance, FlipperFile* flipper_file);
|
||||
*SubGhzProtocolCommonSaveFile)(SubGhzProtocolCommon* instance, FlipperFormat* flipper_format);
|
||||
|
||||
//Load protocol from file
|
||||
typedef bool (*SubGhzProtocolCommonLoadFromFile)(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolCommon* instance,
|
||||
const char* file_path);
|
||||
//Load protocol
|
||||
@@ -208,17 +208,19 @@ bool subghz_protocol_common_read_hex(string_t str, uint8_t* buff, uint16_t len);
|
||||
/** Adding data to a file
|
||||
*
|
||||
* @param instance - SubGhzProtocolCommon instance
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_common_to_save_file(SubGhzProtocolCommon* instance, FlipperFile* flipper_file);
|
||||
bool subghz_protocol_common_to_save_file(
|
||||
SubGhzProtocolCommon* instance,
|
||||
FlipperFormat* flipper_format);
|
||||
|
||||
/** Loading data to a file
|
||||
*
|
||||
* @param instance - SubGhzProtocolCommon instance
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_common_to_load_protocol_from_file(
|
||||
SubGhzProtocolCommon* instance,
|
||||
FlipperFile* flipper_file);
|
||||
FlipperFormat* flipper_format);
|
||||
|
||||
@@ -12,7 +12,7 @@ typedef enum {
|
||||
} FaacSLHDecoderStep;
|
||||
|
||||
SubGhzProtocolFaacSLH* subghz_protocol_faac_slh_alloc(void) {
|
||||
SubGhzProtocolFaacSLH* instance = furi_alloc(sizeof(SubGhzProtocolFaacSLH));
|
||||
SubGhzProtocolFaacSLH* instance = malloc(sizeof(SubGhzProtocolFaacSLH));
|
||||
|
||||
instance->common.name = "Faac SLH";
|
||||
instance->common.code_min_count_bit_for_found = 64;
|
||||
@@ -186,4 +186,4 @@ void subghz_decoder_faac_slh_to_load_protocol(SubGhzProtocolFaacSLH* instance, v
|
||||
instance->common.code_last_found = data->code_found;
|
||||
instance->common.code_last_count_bit = data->code_count_bit;
|
||||
subghz_protocol_faac_slh_check_remote_controller(instance);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ typedef enum {
|
||||
} GateTXDecoderStep;
|
||||
|
||||
SubGhzProtocolGateTX* subghz_protocol_gate_tx_alloc(void) {
|
||||
SubGhzProtocolGateTX* instance = furi_alloc(sizeof(SubGhzProtocolGateTX));
|
||||
SubGhzProtocolGateTX* instance = malloc(sizeof(SubGhzProtocolGateTX));
|
||||
|
||||
instance->common.name = "GateTX";
|
||||
instance->common.code_min_count_bit_for_found = 24;
|
||||
@@ -169,16 +169,16 @@ void subghz_protocol_gate_tx_to_str(SubGhzProtocolGateTX* instance, string_t out
|
||||
|
||||
bool subghz_protocol_gate_tx_to_save_file(
|
||||
SubGhzProtocolGateTX* instance,
|
||||
FlipperFile* flipper_file) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_file);
|
||||
FlipperFormat* flipper_format) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
bool subghz_protocol_gate_tx_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolGateTX* instance,
|
||||
const char* file_path) {
|
||||
if(subghz_protocol_common_to_load_protocol_from_file(
|
||||
(SubGhzProtocolCommon*)instance, flipper_file)) {
|
||||
(SubGhzProtocolCommon*)instance, flipper_format)) {
|
||||
subghz_protocol_gate_tx_check_remote_controller(instance);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -48,22 +48,22 @@ void subghz_protocol_gate_tx_to_str(SubGhzProtocolGateTX* instance, string_t out
|
||||
/** Adding data to a file
|
||||
*
|
||||
* @param instance - SubGhzProtocolGateTX instance
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_gate_tx_to_save_file(
|
||||
SubGhzProtocolGateTX* instance,
|
||||
FlipperFile* flipper_file);
|
||||
FlipperFormat* flipper_format);
|
||||
|
||||
/** Loading protocol from file
|
||||
*
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @param instance - SubGhzProtocolGateTX instance
|
||||
* @param file_path - file path
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_gate_tx_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolGateTX* instance,
|
||||
const char* file_path);
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ typedef enum {
|
||||
} HormannDecoderStep;
|
||||
|
||||
SubGhzProtocolHormann* subghz_protocol_hormann_alloc() {
|
||||
SubGhzProtocolHormann* instance = furi_alloc(sizeof(SubGhzProtocolHormann));
|
||||
SubGhzProtocolHormann* instance = malloc(sizeof(SubGhzProtocolHormann));
|
||||
|
||||
instance->common.name = "Hormann HSM";
|
||||
instance->common.code_min_count_bit_for_found = 44;
|
||||
@@ -188,16 +188,16 @@ void subghz_protocol_hormann_to_str(SubGhzProtocolHormann* instance, string_t ou
|
||||
|
||||
bool subghz_protocol_hormann_to_save_file(
|
||||
SubGhzProtocolHormann* instance,
|
||||
FlipperFile* flipper_file) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_file);
|
||||
FlipperFormat* flipper_format) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
bool subghz_protocol_hormann_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolHormann* instance,
|
||||
const char* file_path) {
|
||||
return subghz_protocol_common_to_load_protocol_from_file(
|
||||
(SubGhzProtocolCommon*)instance, flipper_file);
|
||||
(SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
void subghz_decoder_hormann_to_load_protocol(SubGhzProtocolHormann* instance, void* context) {
|
||||
|
||||
@@ -48,22 +48,22 @@ void subghz_protocol_hormann_to_str(SubGhzProtocolHormann* instance, string_t ou
|
||||
/** Adding data to a file
|
||||
*
|
||||
* @param instance - SubGhzProtocolHormann instance
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_hormann_to_save_file(
|
||||
SubGhzProtocolHormann* instance,
|
||||
FlipperFile* flipper_file);
|
||||
FlipperFormat* flipper_format);
|
||||
|
||||
/** Loading protocol from file
|
||||
*
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @param instance - SubGhzProtocolHormann instance
|
||||
* @param file_path - file path
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_hormann_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolHormann* instance,
|
||||
const char* file_path);
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ typedef enum {
|
||||
} IDoDecoderStep;
|
||||
|
||||
SubGhzProtocolIDo* subghz_protocol_ido_alloc(void) {
|
||||
SubGhzProtocolIDo* instance = furi_alloc(sizeof(SubGhzProtocolIDo));
|
||||
SubGhzProtocolIDo* instance = malloc(sizeof(SubGhzProtocolIDo));
|
||||
|
||||
instance->common.name = "iDo 117/111"; // PT4301-X";
|
||||
instance->common.code_min_count_bit_for_found = 48;
|
||||
@@ -186,4 +186,4 @@ void subghz_decoder_ido_to_load_protocol(SubGhzProtocolIDo* instance, void* cont
|
||||
instance->common.code_last_found = data->code_found;
|
||||
instance->common.code_last_count_bit = data->code_count_bit;
|
||||
subghz_protocol_ido_check_remote_controller(instance);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ typedef enum {
|
||||
} KeeloqDecoderStep;
|
||||
|
||||
SubGhzProtocolKeeloq* subghz_protocol_keeloq_alloc(SubGhzKeystore* keystore) {
|
||||
SubGhzProtocolKeeloq* instance = furi_alloc(sizeof(SubGhzProtocolKeeloq));
|
||||
SubGhzProtocolKeeloq* instance = malloc(sizeof(SubGhzProtocolKeeloq));
|
||||
|
||||
instance->keystore = keystore;
|
||||
|
||||
@@ -469,16 +469,18 @@ void subghz_protocol_keeloq_to_str(SubGhzProtocolKeeloq* instance, string_t outp
|
||||
instance->common.serial);
|
||||
}
|
||||
|
||||
bool subghz_protocol_keeloq_to_save_file(SubGhzProtocolKeeloq* instance, FlipperFile* flipper_file) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_file);
|
||||
bool subghz_protocol_keeloq_to_save_file(
|
||||
SubGhzProtocolKeeloq* instance,
|
||||
FlipperFormat* flipper_format) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
bool subghz_protocol_keeloq_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolKeeloq* instance,
|
||||
const char* file_path) {
|
||||
return subghz_protocol_common_to_load_protocol_from_file(
|
||||
(SubGhzProtocolCommon*)instance, flipper_file);
|
||||
(SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
void subghz_decoder_keeloq_to_load_protocol(SubGhzProtocolKeeloq* instance, void* context) {
|
||||
|
||||
@@ -79,20 +79,22 @@ void subghz_protocol_keeloq_to_str(SubGhzProtocolKeeloq* instance, string_t outp
|
||||
/** Adding data to a file
|
||||
*
|
||||
* @param instance - SubGhzProtocolKeeloq instance
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_keeloq_to_save_file(SubGhzProtocolKeeloq* instance, FlipperFile* flipper_file);
|
||||
bool subghz_protocol_keeloq_to_save_file(
|
||||
SubGhzProtocolKeeloq* instance,
|
||||
FlipperFormat* flipper_format);
|
||||
|
||||
/** Loading protocol from file
|
||||
*
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @param instance - SubGhzProtocolKeeloq instance
|
||||
* @param file_path - file path
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_keeloq_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolKeeloq* instance,
|
||||
const char* file_path);
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ typedef enum {
|
||||
} KIADecoderStep;
|
||||
|
||||
SubGhzProtocolKIA* subghz_protocol_kia_alloc(void) {
|
||||
SubGhzProtocolKIA* instance = furi_alloc(sizeof(SubGhzProtocolKIA));
|
||||
SubGhzProtocolKIA* instance = malloc(sizeof(SubGhzProtocolKIA));
|
||||
|
||||
instance->common.name = "KIA";
|
||||
instance->common.code_min_count_bit_for_found = 60;
|
||||
@@ -185,4 +185,4 @@ void subghz_decoder_kia_to_load_protocol(SubGhzProtocolKIA* instance, void* cont
|
||||
instance->common.code_last_found = data->code_found;
|
||||
instance->common.code_last_count_bit = data->code_count_bit;
|
||||
subghz_protocol_kia_check_remote_controller(instance);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ typedef enum {
|
||||
} NeroRadioDecoderStep;
|
||||
|
||||
SubGhzProtocolNeroRadio* subghz_protocol_nero_radio_alloc(void) {
|
||||
SubGhzProtocolNeroRadio* instance = furi_alloc(sizeof(SubGhzProtocolNeroRadio));
|
||||
SubGhzProtocolNeroRadio* instance = malloc(sizeof(SubGhzProtocolNeroRadio));
|
||||
|
||||
instance->common.name = "Nero Radio";
|
||||
instance->common.code_min_count_bit_for_found = 55;
|
||||
@@ -211,16 +211,16 @@ void subghz_protocol_nero_radio_to_str(SubGhzProtocolNeroRadio* instance, string
|
||||
|
||||
bool subghz_protocol_nero_radio_to_save_file(
|
||||
SubGhzProtocolNeroRadio* instance,
|
||||
FlipperFile* flipper_file) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_file);
|
||||
FlipperFormat* flipper_format) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
bool subghz_protocol_nero_radio_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolNeroRadio* instance,
|
||||
const char* file_path) {
|
||||
return subghz_protocol_common_to_load_protocol_from_file(
|
||||
(SubGhzProtocolCommon*)instance, flipper_file);
|
||||
(SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
void subghz_decoder_nero_radio_to_load_protocol(SubGhzProtocolNeroRadio* instance, void* context) {
|
||||
@@ -229,4 +229,4 @@ void subghz_decoder_nero_radio_to_load_protocol(SubGhzProtocolNeroRadio* instanc
|
||||
SubGhzProtocolCommonLoad* data = context;
|
||||
instance->common.code_last_found = data->code_found;
|
||||
instance->common.code_last_count_bit = data->code_count_bit;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,22 +57,22 @@ void subghz_protocol_nero_radio_to_str(SubGhzProtocolNeroRadio* instance, string
|
||||
/** Adding data to a file
|
||||
*
|
||||
* @param instance - SubGhzProtocolNeroRadio instance
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_nero_radio_to_save_file(
|
||||
SubGhzProtocolNeroRadio* instance,
|
||||
FlipperFile* flipper_file);
|
||||
FlipperFormat* flipper_format);
|
||||
|
||||
/** Loading protocol from file
|
||||
*
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @param instance - SubGhzProtocolNeroRadio instance
|
||||
* @param file_path - file path
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_nero_radio_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolNeroRadio* instance,
|
||||
const char* file_path);
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ typedef enum {
|
||||
} NeroSketchDecoderStep;
|
||||
|
||||
SubGhzProtocolNeroSketch* subghz_protocol_nero_sketch_alloc(void) {
|
||||
SubGhzProtocolNeroSketch* instance = furi_alloc(sizeof(SubGhzProtocolNeroSketch));
|
||||
SubGhzProtocolNeroSketch* instance = malloc(sizeof(SubGhzProtocolNeroSketch));
|
||||
|
||||
instance->common.name = "Nero Sketch";
|
||||
instance->common.code_min_count_bit_for_found = 40;
|
||||
@@ -204,16 +204,16 @@ void subghz_protocol_nero_sketch_to_str(SubGhzProtocolNeroSketch* instance, stri
|
||||
|
||||
bool subghz_protocol_nero_sketch_to_save_file(
|
||||
SubGhzProtocolNeroSketch* instance,
|
||||
FlipperFile* flipper_file) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_file);
|
||||
FlipperFormat* flipper_format) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
bool subghz_protocol_nero_sketch_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolNeroSketch* instance,
|
||||
const char* file_path) {
|
||||
return subghz_protocol_common_to_load_protocol_from_file(
|
||||
(SubGhzProtocolCommon*)instance, flipper_file);
|
||||
(SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
void subghz_decoder_nero_sketch_to_load_protocol(SubGhzProtocolNeroSketch* instance, void* context) {
|
||||
|
||||
@@ -57,22 +57,22 @@ void subghz_protocol_nero_sketch_to_str(SubGhzProtocolNeroSketch* instance, stri
|
||||
/** Adding data to a file
|
||||
*
|
||||
* @param instance - SubGhzProtocolNeroSketch instance
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_nero_sketch_to_save_file(
|
||||
SubGhzProtocolNeroSketch* instance,
|
||||
FlipperFile* flipper_file);
|
||||
FlipperFormat* flipper_format);
|
||||
|
||||
/** Loading protocol from file
|
||||
*
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @param instance - SubGhzProtocolNeroSketch instance
|
||||
* @param file_path - file path
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_nero_sketch_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolNeroSketch* instance,
|
||||
const char* file_path);
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ typedef enum {
|
||||
} NiceFloDecoderStep;
|
||||
|
||||
SubGhzProtocolNiceFlo* subghz_protocol_nice_flo_alloc() {
|
||||
SubGhzProtocolNiceFlo* instance = furi_alloc(sizeof(SubGhzProtocolNiceFlo));
|
||||
SubGhzProtocolNiceFlo* instance = malloc(sizeof(SubGhzProtocolNiceFlo));
|
||||
|
||||
instance->common.name = "Nice FLO";
|
||||
instance->common.code_min_count_bit_for_found = 12;
|
||||
@@ -166,16 +166,16 @@ void subghz_protocol_nice_flo_to_str(SubGhzProtocolNiceFlo* instance, string_t o
|
||||
|
||||
bool subghz_protocol_nice_flo_to_save_file(
|
||||
SubGhzProtocolNiceFlo* instance,
|
||||
FlipperFile* flipper_file) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_file);
|
||||
FlipperFormat* flipper_format) {
|
||||
return subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
bool subghz_protocol_nice_flo_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolNiceFlo* instance,
|
||||
const char* file_path) {
|
||||
return subghz_protocol_common_to_load_protocol_from_file(
|
||||
(SubGhzProtocolCommon*)instance, flipper_file);
|
||||
(SubGhzProtocolCommon*)instance, flipper_format);
|
||||
}
|
||||
|
||||
void subghz_decoder_nice_flo_to_load_protocol(SubGhzProtocolNiceFlo* instance, void* context) {
|
||||
|
||||
@@ -48,22 +48,22 @@ void subghz_protocol_nice_flo_to_str(SubGhzProtocolNiceFlo* instance, string_t o
|
||||
/** Adding data to a file
|
||||
*
|
||||
* @param instance - SubGhzProtocolNiceFlo instance
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_nice_flo_to_save_file(
|
||||
SubGhzProtocolNiceFlo* instance,
|
||||
FlipperFile* flipper_file);
|
||||
FlipperFormat* flipper_format);
|
||||
|
||||
/** Loading protocol from file
|
||||
*
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @param instance - SubGhzProtocolNiceFlo instance
|
||||
* @param file_path - file path
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_protocol_nice_flo_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolNiceFlo* instance,
|
||||
const char* file_path);
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ typedef enum {
|
||||
} NiceFlorSDecoderStep;
|
||||
|
||||
SubGhzProtocolNiceFlorS* subghz_protocol_nice_flor_s_alloc() {
|
||||
SubGhzProtocolNiceFlorS* instance = furi_alloc(sizeof(SubGhzProtocolNiceFlorS));
|
||||
SubGhzProtocolNiceFlorS* instance = malloc(sizeof(SubGhzProtocolNiceFlorS));
|
||||
|
||||
instance->common.name = "Nice FloR-S";
|
||||
instance->common.code_min_count_bit_for_found = 52;
|
||||
@@ -261,4 +261,4 @@ void subghz_decoder_nice_flor_s_to_load_protocol(SubGhzProtocolNiceFlorS* instan
|
||||
instance->common.code_last_found = data->code_found;
|
||||
instance->common.code_last_count_bit = data->code_count_bit;
|
||||
subghz_nice_flor_s_decoder_decrypt(instance);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ typedef enum {
|
||||
} PrincetonDecoderStep;
|
||||
|
||||
SubGhzEncoderPrinceton* subghz_encoder_princeton_alloc() {
|
||||
SubGhzEncoderPrinceton* instance = furi_alloc(sizeof(SubGhzEncoderPrinceton));
|
||||
SubGhzEncoderPrinceton* instance = malloc(sizeof(SubGhzEncoderPrinceton));
|
||||
return instance;
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ LevelDuration subghz_encoder_princeton_yield(void* context) {
|
||||
}
|
||||
|
||||
SubGhzDecoderPrinceton* subghz_decoder_princeton_alloc(void) {
|
||||
SubGhzDecoderPrinceton* instance = furi_alloc(sizeof(SubGhzDecoderPrinceton));
|
||||
SubGhzDecoderPrinceton* instance = malloc(sizeof(SubGhzDecoderPrinceton));
|
||||
|
||||
instance->te = SUBGHZ_PT_SHORT;
|
||||
instance->common.name = "Princeton";
|
||||
@@ -333,23 +333,24 @@ void subghz_decoder_princeton_to_str(SubGhzDecoderPrinceton* instance, string_t
|
||||
|
||||
bool subghz_decoder_princeton_to_save_file(
|
||||
SubGhzDecoderPrinceton* instance,
|
||||
FlipperFile* flipper_file) {
|
||||
bool res = subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_file);
|
||||
FlipperFormat* flipper_format) {
|
||||
bool res =
|
||||
subghz_protocol_common_to_save_file((SubGhzProtocolCommon*)instance, flipper_format);
|
||||
if(res) {
|
||||
res = flipper_file_write_uint32(flipper_file, "TE", &instance->te, 1);
|
||||
res = flipper_format_write_uint32(flipper_format, "TE", &instance->te, 1);
|
||||
if(!res) FURI_LOG_E(SUBGHZ_PARSER_TAG, "Unable to add Te");
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
bool subghz_decoder_princeton_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzDecoderPrinceton* instance,
|
||||
const char* file_path) {
|
||||
bool loaded = subghz_protocol_common_to_load_protocol_from_file(
|
||||
(SubGhzProtocolCommon*)instance, flipper_file);
|
||||
(SubGhzProtocolCommon*)instance, flipper_format);
|
||||
if(loaded) {
|
||||
loaded = flipper_file_read_uint32(flipper_file, "TE", (uint32_t*)&instance->te, 1);
|
||||
loaded = flipper_format_read_uint32(flipper_format, "TE", (uint32_t*)&instance->te, 1);
|
||||
if(!loaded) FURI_LOG_E(SUBGHZ_PARSER_TAG, "Missing TE");
|
||||
}
|
||||
return loaded;
|
||||
|
||||
@@ -110,22 +110,22 @@ void subghz_decoder_princeton_to_str(SubGhzDecoderPrinceton* instance, string_t
|
||||
/** Adding data to a file
|
||||
*
|
||||
* @param instance - SubGhzDecoderPrinceton instance
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_decoder_princeton_to_save_file(
|
||||
SubGhzDecoderPrinceton* instance,
|
||||
FlipperFile* flipper_file);
|
||||
FlipperFormat* flipper_format);
|
||||
|
||||
/** Loading protocol from file
|
||||
*
|
||||
* @param flipper_file - FlipperFile
|
||||
* @param flipper_format - FlipperFormat
|
||||
* @param instance - SubGhzDecoderPrinceton instance
|
||||
* @param file_path - file path
|
||||
* @return bool
|
||||
*/
|
||||
bool subghz_decoder_princeton_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzDecoderPrinceton* instance,
|
||||
const char* file_path);
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ struct SubGhzProtocolRAW {
|
||||
int32_t* upload_raw;
|
||||
uint16_t ind_write;
|
||||
Storage* storage;
|
||||
FlipperFile* flipper_file;
|
||||
FlipperFormat* flipper_format;
|
||||
SubGhzFileEncoderWorker* file_worker_encoder;
|
||||
uint32_t file_is_open;
|
||||
string_t file_name;
|
||||
@@ -28,7 +28,7 @@ typedef enum {
|
||||
} RAWFilIsOpen;
|
||||
|
||||
SubGhzProtocolRAW* subghz_protocol_raw_alloc(void) {
|
||||
SubGhzProtocolRAW* instance = furi_alloc(sizeof(SubGhzProtocolRAW));
|
||||
SubGhzProtocolRAW* instance = malloc(sizeof(SubGhzProtocolRAW));
|
||||
|
||||
instance->upload_raw = NULL;
|
||||
instance->ind_write = 0;
|
||||
@@ -36,7 +36,7 @@ SubGhzProtocolRAW* subghz_protocol_raw_alloc(void) {
|
||||
instance->last_level = false;
|
||||
|
||||
instance->storage = furi_record_open("storage");
|
||||
instance->flipper_file = flipper_file_alloc(instance->storage);
|
||||
instance->flipper_format = flipper_format_file_alloc(instance->storage);
|
||||
instance->file_is_open = RAWFileIsOpenClose;
|
||||
string_init(instance->file_name);
|
||||
|
||||
@@ -61,7 +61,7 @@ void subghz_protocol_raw_free(SubGhzProtocolRAW* instance) {
|
||||
furi_assert(instance);
|
||||
string_clear(instance->file_name);
|
||||
|
||||
flipper_file_free(instance->flipper_file);
|
||||
flipper_format_free(instance->flipper_format);
|
||||
furi_record_close("storage");
|
||||
|
||||
free(instance);
|
||||
@@ -166,7 +166,7 @@ bool subghz_protocol_raw_save_to_file_init(
|
||||
const char* preset) {
|
||||
furi_assert(instance);
|
||||
|
||||
//instance->flipper_file = flipper_file_alloc(instance->storage);
|
||||
//instance->flipper_format = flipper_format_file_alloc(instance->storage);
|
||||
string_t dev_file_name;
|
||||
string_init(dev_file_name);
|
||||
bool init = false;
|
||||
@@ -186,34 +186,35 @@ bool subghz_protocol_raw_save_to_file_init(
|
||||
}
|
||||
|
||||
// Open file
|
||||
if(!flipper_file_open_always(instance->flipper_file, string_get_cstr(dev_file_name))) {
|
||||
if(!flipper_format_file_open_always(
|
||||
instance->flipper_format, string_get_cstr(dev_file_name))) {
|
||||
FURI_LOG_E(TAG, "Unable to open file for write: %s", dev_file_name);
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_file_write_header_cstr(
|
||||
instance->flipper_file, SUBGHZ_RAW_FILE_TYPE, SUBGHZ_RAW_FILE_VERSION)) {
|
||||
if(!flipper_format_write_header_cstr(
|
||||
instance->flipper_format, SUBGHZ_RAW_FILE_TYPE, SUBGHZ_RAW_FILE_VERSION)) {
|
||||
FURI_LOG_E(TAG, "Unable to add header");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_file_write_uint32(instance->flipper_file, "Frequency", &frequency, 1)) {
|
||||
if(!flipper_format_write_uint32(instance->flipper_format, "Frequency", &frequency, 1)) {
|
||||
FURI_LOG_E(TAG, "Unable to add Frequency");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_file_write_string_cstr(instance->flipper_file, "Preset", preset)) {
|
||||
if(!flipper_format_write_string_cstr(instance->flipper_format, "Preset", preset)) {
|
||||
FURI_LOG_E(TAG, "Unable to add Preset");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_file_write_string_cstr(
|
||||
instance->flipper_file, "Protocol", instance->common.name)) {
|
||||
if(!flipper_format_write_string_cstr(
|
||||
instance->flipper_format, "Protocol", instance->common.name)) {
|
||||
FURI_LOG_E(TAG, "Unable to add Protocol");
|
||||
break;
|
||||
}
|
||||
|
||||
instance->upload_raw = furi_alloc(SUBGHZ_DOWNLOAD_MAX_SIZE * sizeof(int32_t));
|
||||
instance->upload_raw = malloc(SUBGHZ_DOWNLOAD_MAX_SIZE * sizeof(int32_t));
|
||||
instance->file_is_open = RAWFileIsOpenWrite;
|
||||
instance->sample_write = 0;
|
||||
init = true;
|
||||
@@ -234,7 +235,7 @@ void subghz_protocol_raw_save_to_file_stop(SubGhzProtocolRAW* instance) {
|
||||
instance->upload_raw = NULL;
|
||||
}
|
||||
|
||||
flipper_file_close(instance->flipper_file);
|
||||
flipper_format_file_close(instance->flipper_format);
|
||||
instance->file_is_open = RAWFileIsOpenClose;
|
||||
}
|
||||
|
||||
@@ -243,8 +244,8 @@ bool subghz_protocol_raw_save_to_file_write(SubGhzProtocolRAW* instance) {
|
||||
|
||||
bool is_write = false;
|
||||
if(instance->file_is_open == RAWFileIsOpenWrite) {
|
||||
if(!flipper_file_write_int32(
|
||||
instance->flipper_file, "RAW_Data", instance->upload_raw, instance->ind_write)) {
|
||||
if(!flipper_format_write_int32(
|
||||
instance->flipper_format, "RAW_Data", instance->upload_raw, instance->ind_write)) {
|
||||
FURI_LOG_E(TAG, "Unable to add RAW_Data");
|
||||
} else {
|
||||
instance->sample_write += instance->ind_write;
|
||||
@@ -260,10 +261,10 @@ size_t subghz_protocol_raw_get_sample_write(SubGhzProtocolRAW* instance) {
|
||||
}
|
||||
|
||||
bool subghz_protocol_raw_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolRAW* instance,
|
||||
const char* file_path) {
|
||||
furi_assert(file_path);
|
||||
subghz_protocol_raw_set_last_file_name(instance, file_path);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,6 +66,6 @@ bool subghz_protocol_raw_save_to_file_write(SubGhzProtocolRAW* instance);
|
||||
size_t subghz_protocol_raw_get_sample_write(SubGhzProtocolRAW* instance);
|
||||
|
||||
bool subghz_protocol_raw_to_load_protocol_from_file(
|
||||
FlipperFile* flipper_file,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzProtocolRAW* instance,
|
||||
const char* file_path);
|
||||
@@ -18,7 +18,7 @@ typedef enum {
|
||||
} ScherKhanDecoderStep;
|
||||
|
||||
SubGhzProtocolScherKhan* subghz_protocol_scher_khan_alloc(void) {
|
||||
SubGhzProtocolScherKhan* instance = furi_alloc(sizeof(SubGhzProtocolScherKhan));
|
||||
SubGhzProtocolScherKhan* instance = malloc(sizeof(SubGhzProtocolScherKhan));
|
||||
|
||||
instance->common.name = "Scher-Khan";
|
||||
instance->common.code_min_count_bit_for_found = 35;
|
||||
@@ -241,4 +241,4 @@ void subghz_decoder_scher_khan_to_load_protocol(SubGhzProtocolScherKhan* instanc
|
||||
instance->common.code_last_found = data->code_found;
|
||||
instance->common.code_last_count_bit = data->code_count_bit;
|
||||
subghz_protocol_scher_khan_check_remote_controller(instance);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ typedef enum {
|
||||
} SomfyKeytisDecoderStep;
|
||||
|
||||
SubGhzProtocolSomfyKeytis* subghz_protocol_somfy_keytis_alloc() {
|
||||
SubGhzProtocolSomfyKeytis* instance = furi_alloc(sizeof(SubGhzProtocolSomfyKeytis));
|
||||
SubGhzProtocolSomfyKeytis* instance = malloc(sizeof(SubGhzProtocolSomfyKeytis));
|
||||
|
||||
instance->common.name = "Somfy Keytis";
|
||||
instance->common.code_min_count_bit_for_found = 80;
|
||||
|
||||
@@ -18,7 +18,7 @@ typedef enum {
|
||||
} SomfyTelisDecoderStep;
|
||||
|
||||
SubGhzProtocolSomfyTelis* subghz_protocol_somfy_telis_alloc() {
|
||||
SubGhzProtocolSomfyTelis* instance = furi_alloc(sizeof(SubGhzProtocolSomfyTelis));
|
||||
SubGhzProtocolSomfyTelis* instance = malloc(sizeof(SubGhzProtocolSomfyTelis));
|
||||
|
||||
instance->common.name = "Somfy Telis";
|
||||
instance->common.code_min_count_bit_for_found = 56;
|
||||
|
||||
@@ -22,7 +22,7 @@ typedef enum {
|
||||
} StarLineDecoderStep;
|
||||
|
||||
SubGhzProtocolStarLine* subghz_protocol_star_line_alloc(SubGhzKeystore* keystore) {
|
||||
SubGhzProtocolStarLine* instance = furi_alloc(sizeof(SubGhzProtocolStarLine));
|
||||
SubGhzProtocolStarLine* instance = malloc(sizeof(SubGhzProtocolStarLine));
|
||||
|
||||
instance->keystore = keystore;
|
||||
|
||||
@@ -338,4 +338,4 @@ void subghz_decoder_star_line_to_load_protocol(SubGhzProtocolStarLine* instance,
|
||||
instance->common.code_last_found = data->code_found;
|
||||
instance->common.code_last_count_bit = data->code_count_bit;
|
||||
subghz_protocol_star_line_check_remote_controller(instance);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#include "subghz_file_encoder_worker.h"
|
||||
#include <stream_buffer.h>
|
||||
|
||||
#include <lib/flipper_file/flipper_file.h>
|
||||
#include <lib/flipper_file/file_helper.h>
|
||||
#include <toolbox/stream/stream.h>
|
||||
#include <flipper_format/flipper_format.h>
|
||||
#include <flipper_format/flipper_format_i.h>
|
||||
|
||||
#define TAG "SubGhzFileEncoderWorker"
|
||||
|
||||
@@ -13,7 +14,7 @@ struct SubGhzFileEncoderWorker {
|
||||
StreamBufferHandle_t stream;
|
||||
|
||||
Storage* storage;
|
||||
FlipperFile* flipper_file;
|
||||
FlipperFormat* flipper_format;
|
||||
|
||||
volatile bool worker_running;
|
||||
volatile bool worker_stoping;
|
||||
@@ -121,21 +122,21 @@ static int32_t subghz_file_encoder_worker_thread(void* context) {
|
||||
SubGhzFileEncoderWorker* instance = context;
|
||||
FURI_LOG_I(TAG, "Worker start");
|
||||
bool res = false;
|
||||
File* file = flipper_file_get_file(instance->flipper_file);
|
||||
Stream* stream = flipper_format_get_raw_stream(instance->flipper_format);
|
||||
do {
|
||||
if(!flipper_file_open_existing(
|
||||
instance->flipper_file, string_get_cstr(instance->file_path))) {
|
||||
if(!flipper_format_file_open_existing(
|
||||
instance->flipper_format, string_get_cstr(instance->file_path))) {
|
||||
FURI_LOG_E(
|
||||
TAG, "Unable to open file for read: %s", string_get_cstr(instance->file_path));
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_read_string(instance->flipper_file, "Protocol", instance->str_data)) {
|
||||
if(!flipper_format_read_string(instance->flipper_format, "Protocol", instance->str_data)) {
|
||||
FURI_LOG_E(TAG, "Missing Protocol");
|
||||
break;
|
||||
}
|
||||
|
||||
//skip the end of the previous line "\n"
|
||||
storage_file_seek(file, 1, false);
|
||||
stream_seek(stream, 1, StreamOffsetFromCurrent);
|
||||
res = true;
|
||||
instance->worker_stoping = false;
|
||||
FURI_LOG_I(TAG, "Start transmission");
|
||||
@@ -144,9 +145,9 @@ static int32_t subghz_file_encoder_worker_thread(void* context) {
|
||||
while(res && instance->worker_running) {
|
||||
size_t stream_free_byte = xStreamBufferSpacesAvailable(instance->stream);
|
||||
if((stream_free_byte / sizeof(int32_t)) >= SUBGHZ_FILE_ENCODER_LOAD) {
|
||||
if(file_helper_read_line(file, instance->str_data)) {
|
||||
if(stream_read_line(stream, instance->str_data)) {
|
||||
//skip the end of the previous line "\n"
|
||||
storage_file_seek(file, 1, false);
|
||||
stream_seek(stream, 1, StreamOffsetFromCurrent);
|
||||
if(!subghz_file_encoder_worker_data_parse(
|
||||
instance,
|
||||
string_get_cstr(instance->str_data),
|
||||
@@ -174,14 +175,14 @@ static int32_t subghz_file_encoder_worker_thread(void* context) {
|
||||
}
|
||||
osDelay(50);
|
||||
}
|
||||
flipper_file_close(instance->flipper_file);
|
||||
flipper_format_file_close(instance->flipper_format);
|
||||
|
||||
FURI_LOG_I(TAG, "Worker stop");
|
||||
return 0;
|
||||
}
|
||||
|
||||
SubGhzFileEncoderWorker* subghz_file_encoder_worker_alloc() {
|
||||
SubGhzFileEncoderWorker* instance = furi_alloc(sizeof(SubGhzFileEncoderWorker));
|
||||
SubGhzFileEncoderWorker* instance = malloc(sizeof(SubGhzFileEncoderWorker));
|
||||
|
||||
instance->thread = furi_thread_alloc();
|
||||
furi_thread_set_name(instance->thread, "SubghzFEWorker");
|
||||
@@ -191,7 +192,7 @@ SubGhzFileEncoderWorker* subghz_file_encoder_worker_alloc() {
|
||||
instance->stream = xStreamBufferCreate(sizeof(int32_t) * 2048, sizeof(int32_t));
|
||||
|
||||
instance->storage = furi_record_open("storage");
|
||||
instance->flipper_file = flipper_file_alloc(instance->storage);
|
||||
instance->flipper_format = flipper_format_file_alloc(instance->storage);
|
||||
|
||||
string_init(instance->str_data);
|
||||
string_init(instance->file_path);
|
||||
@@ -210,7 +211,7 @@ void subghz_file_encoder_worker_free(SubGhzFileEncoderWorker* instance) {
|
||||
string_clear(instance->str_data);
|
||||
string_clear(instance->file_path);
|
||||
|
||||
flipper_file_free(instance->flipper_file);
|
||||
flipper_format_free(instance->flipper_format);
|
||||
furi_record_close("storage");
|
||||
|
||||
free(instance);
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
|
||||
#include <storage/storage.h>
|
||||
#include <toolbox/hex.h>
|
||||
#include <flipper_file/flipper_file.h>
|
||||
#include <toolbox/stream/stream.h>
|
||||
#include <flipper_format/flipper_format.h>
|
||||
#include <flipper_format/flipper_format_i.h>
|
||||
|
||||
#define TAG "SubGhzKeystore"
|
||||
|
||||
@@ -29,7 +31,7 @@ struct SubGhzKeystore {
|
||||
};
|
||||
|
||||
SubGhzKeystore* subghz_keystore_alloc() {
|
||||
SubGhzKeystore* instance = furi_alloc(sizeof(SubGhzKeystore));
|
||||
SubGhzKeystore* instance = malloc(sizeof(SubGhzKeystore));
|
||||
|
||||
SubGhzKeyArray_init(instance->data);
|
||||
|
||||
@@ -104,19 +106,19 @@ static void subghz_keystore_mess_with_iv(uint8_t* iv) {
|
||||
: "r0", "r1", "r2", "r3", "memory");
|
||||
}
|
||||
|
||||
static bool subghz_keystore_read_file(SubGhzKeystore* instance, File* file, uint8_t* iv) {
|
||||
static bool subghz_keystore_read_file(SubGhzKeystore* instance, Stream* stream, uint8_t* iv) {
|
||||
bool result = true;
|
||||
char buffer[FILE_BUFFER_SIZE];
|
||||
uint8_t buffer[FILE_BUFFER_SIZE];
|
||||
|
||||
char* decrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
||||
char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
||||
char* decrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
||||
char* encrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
||||
size_t encrypted_line_cursor = 0;
|
||||
|
||||
if(iv) furi_hal_crypto_store_load_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT, iv);
|
||||
|
||||
size_t ret = 0;
|
||||
do {
|
||||
ret = storage_file_read(file, buffer, FILE_BUFFER_SIZE);
|
||||
ret = stream_read(stream, buffer, FILE_BUFFER_SIZE);
|
||||
for(uint16_t i = 0; i < ret; i++) {
|
||||
if(buffer[i] == '\n' && encrypted_line_cursor > 0) {
|
||||
// Process line
|
||||
@@ -187,17 +189,17 @@ bool subghz_keystore_load(SubGhzKeystore* instance, const char* file_name) {
|
||||
|
||||
Storage* storage = furi_record_open("storage");
|
||||
|
||||
FlipperFile* flipper_file = flipper_file_alloc(storage);
|
||||
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
||||
do {
|
||||
if(!flipper_file_open_existing(flipper_file, file_name)) {
|
||||
if(!flipper_format_file_open_existing(flipper_format, file_name)) {
|
||||
FURI_LOG_E(TAG, "Unable to open file for read: %s", file_name);
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_read_header(flipper_file, filetype, &version)) {
|
||||
if(!flipper_format_read_header(flipper_format, filetype, &version)) {
|
||||
FURI_LOG_E(TAG, "Missing or incorrect header");
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_read_uint32(flipper_file, "Encryption", (uint32_t*)&encryption, 1)) {
|
||||
if(!flipper_format_read_uint32(flipper_format, "Encryption", (uint32_t*)&encryption, 1)) {
|
||||
FURI_LOG_E(TAG, "Missing encryption type");
|
||||
break;
|
||||
}
|
||||
@@ -208,23 +210,22 @@ bool subghz_keystore_load(SubGhzKeystore* instance, const char* file_name) {
|
||||
break;
|
||||
}
|
||||
|
||||
File* file = flipper_file_get_file(flipper_file);
|
||||
Stream* stream = flipper_format_get_raw_stream(flipper_format);
|
||||
if(encryption == SubGhzKeystoreEncryptionNone) {
|
||||
result = subghz_keystore_read_file(instance, file, NULL);
|
||||
result = subghz_keystore_read_file(instance, stream, NULL);
|
||||
} else if(encryption == SubGhzKeystoreEncryptionAES256) {
|
||||
if(!flipper_file_read_hex(flipper_file, "IV", iv, 16)) {
|
||||
if(!flipper_format_read_hex(flipper_format, "IV", iv, 16)) {
|
||||
FURI_LOG_E(TAG, "Missing IV");
|
||||
break;
|
||||
}
|
||||
subghz_keystore_mess_with_iv(iv);
|
||||
result = subghz_keystore_read_file(instance, file, iv);
|
||||
result = subghz_keystore_read_file(instance, stream, iv);
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Unknown encryption");
|
||||
break;
|
||||
}
|
||||
} while(0);
|
||||
flipper_file_close(flipper_file);
|
||||
flipper_file_free(flipper_file);
|
||||
flipper_format_free(flipper_format);
|
||||
|
||||
furi_record_close("storage");
|
||||
|
||||
@@ -238,26 +239,26 @@ bool subghz_keystore_save(SubGhzKeystore* instance, const char* file_name, uint8
|
||||
bool result = false;
|
||||
|
||||
Storage* storage = furi_record_open("storage");
|
||||
char* decrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
||||
char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
||||
char* decrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
||||
char* encrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
||||
|
||||
FlipperFile* flipper_file = flipper_file_alloc(storage);
|
||||
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
||||
do {
|
||||
if(!flipper_file_open_always(flipper_file, file_name)) {
|
||||
if(!flipper_format_file_open_always(flipper_format, file_name)) {
|
||||
FURI_LOG_E(TAG, "Unable to open file for write: %s", file_name);
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_write_header_cstr(
|
||||
flipper_file, SUBGHZ_KEYSTORE_FILE_TYPE, SUBGHZ_KEYSTORE_FILE_VERSION)) {
|
||||
if(!flipper_format_write_header_cstr(
|
||||
flipper_format, SUBGHZ_KEYSTORE_FILE_TYPE, SUBGHZ_KEYSTORE_FILE_VERSION)) {
|
||||
FURI_LOG_E(TAG, "Unable to add header");
|
||||
break;
|
||||
}
|
||||
uint32_t encryption = SubGhzKeystoreEncryptionAES256;
|
||||
if(!flipper_file_write_uint32(flipper_file, "Encryption", &encryption, 1)) {
|
||||
if(!flipper_format_write_uint32(flipper_format, "Encryption", &encryption, 1)) {
|
||||
FURI_LOG_E(TAG, "Unable to add Encryption");
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_write_hex(flipper_file, "IV", iv, 16)) {
|
||||
if(!flipper_format_write_hex(flipper_format, "IV", iv, 16)) {
|
||||
FURI_LOG_E(TAG, "Unable to add IV");
|
||||
break;
|
||||
}
|
||||
@@ -269,7 +270,7 @@ bool subghz_keystore_save(SubGhzKeystore* instance, const char* file_name, uint8
|
||||
break;
|
||||
}
|
||||
|
||||
File* file = flipper_file_get_file(flipper_file);
|
||||
Stream* stream = flipper_format_get_raw_stream(flipper_format);
|
||||
size_t encrypted_line_count = 0;
|
||||
for
|
||||
M_EACH(key, instance->data, SubGhzKeyArray_t) {
|
||||
@@ -306,8 +307,8 @@ bool subghz_keystore_save(SubGhzKeystore* instance, const char* file_name, uint8
|
||||
encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
|
||||
encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
|
||||
}
|
||||
storage_file_write(file, encrypted_line, strlen(encrypted_line));
|
||||
storage_file_write(file, "\n", 1);
|
||||
stream_write_cstring(stream, encrypted_line);
|
||||
stream_write_char(stream, '\n');
|
||||
encrypted_line_count++;
|
||||
}
|
||||
furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
|
||||
@@ -319,8 +320,7 @@ bool subghz_keystore_save(SubGhzKeystore* instance, const char* file_name, uint8
|
||||
FURI_LOG_E(TAG, "Failure. Encrypted: %d of %d", encrypted_line_count, total_keys);
|
||||
}
|
||||
} while(0);
|
||||
flipper_file_close(flipper_file);
|
||||
flipper_file_free(flipper_file);
|
||||
flipper_format_free(flipper_format);
|
||||
|
||||
free(encrypted_line);
|
||||
free(decrypted_line);
|
||||
@@ -346,19 +346,20 @@ bool subghz_keystore_raw_encrypted_save(
|
||||
|
||||
Storage* storage = furi_record_open("storage");
|
||||
|
||||
char* encrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
||||
char* encrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_ENCRYPTED_LINE_SIZE);
|
||||
|
||||
FlipperFile* input_flipper_file = flipper_file_alloc(storage);
|
||||
FlipperFormat* input_flipper_format = flipper_format_file_alloc(storage);
|
||||
do {
|
||||
if(!flipper_file_open_existing(input_flipper_file, input_file_name)) {
|
||||
if(!flipper_format_file_open_existing(input_flipper_format, input_file_name)) {
|
||||
FURI_LOG_E(TAG, "Unable to open file for read: %s", input_file_name);
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_read_header(input_flipper_file, filetype, &version)) {
|
||||
if(!flipper_format_read_header(input_flipper_format, filetype, &version)) {
|
||||
FURI_LOG_E(TAG, "Missing or incorrect header");
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_read_uint32(input_flipper_file, "Encryption", (uint32_t*)&encryption, 1)) {
|
||||
if(!flipper_format_read_uint32(
|
||||
input_flipper_format, "Encryption", (uint32_t*)&encryption, 1)) {
|
||||
FURI_LOG_E(TAG, "Missing encryption type");
|
||||
break;
|
||||
}
|
||||
@@ -373,30 +374,30 @@ bool subghz_keystore_raw_encrypted_save(
|
||||
FURI_LOG_E(TAG, "Already encryption");
|
||||
break;
|
||||
}
|
||||
File* input_file = flipper_file_get_file(input_flipper_file);
|
||||
Stream* input_stream = flipper_format_get_raw_stream(input_flipper_format);
|
||||
|
||||
FlipperFile* output_flipper_file = flipper_file_alloc(storage);
|
||||
FlipperFormat* output_flipper_format = flipper_format_file_alloc(storage);
|
||||
|
||||
if(!flipper_file_open_always(output_flipper_file, output_file_name)) {
|
||||
if(!flipper_format_file_open_always(output_flipper_format, output_file_name)) {
|
||||
FURI_LOG_E(TAG, "Unable to open file for write: %s", output_file_name);
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_write_header_cstr(
|
||||
output_flipper_file, string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_VERSION)) {
|
||||
if(!flipper_format_write_header_cstr(
|
||||
output_flipper_format, string_get_cstr(filetype), SUBGHZ_KEYSTORE_FILE_VERSION)) {
|
||||
FURI_LOG_E(TAG, "Unable to add header");
|
||||
break;
|
||||
}
|
||||
uint32_t encryption = SubGhzKeystoreEncryptionAES256;
|
||||
if(!flipper_file_write_uint32(output_flipper_file, "Encryption", &encryption, 1)) {
|
||||
if(!flipper_format_write_uint32(output_flipper_format, "Encryption", &encryption, 1)) {
|
||||
FURI_LOG_E(TAG, "Unable to add Encryption");
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_write_hex(output_flipper_file, "IV", iv, 16)) {
|
||||
if(!flipper_format_write_hex(output_flipper_format, "IV", iv, 16)) {
|
||||
FURI_LOG_E(TAG, "Unable to add IV");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_file_write_string_cstr(output_flipper_file, "Encrypt_data", "RAW")) {
|
||||
if(!flipper_format_write_string_cstr(output_flipper_format, "Encrypt_data", "RAW")) {
|
||||
FURI_LOG_E(TAG, "Unable to add Encrypt_data");
|
||||
break;
|
||||
}
|
||||
@@ -408,19 +409,19 @@ bool subghz_keystore_raw_encrypted_save(
|
||||
break;
|
||||
}
|
||||
|
||||
File* output_file = flipper_file_get_file(output_flipper_file);
|
||||
char buffer[FILE_BUFFER_SIZE];
|
||||
Stream* output_stream = flipper_format_get_raw_stream(output_flipper_format);
|
||||
uint8_t buffer[FILE_BUFFER_SIZE];
|
||||
bool result = true;
|
||||
|
||||
size_t ret = 0;
|
||||
furi_assert(FILE_BUFFER_SIZE % 16 == 0);
|
||||
|
||||
//skip the end of the previous line "\n"
|
||||
storage_file_read(input_file, buffer, 1);
|
||||
stream_read(input_stream, buffer, 1);
|
||||
|
||||
do {
|
||||
memset(buffer, 0, FILE_BUFFER_SIZE);
|
||||
ret = storage_file_read(input_file, buffer, FILE_BUFFER_SIZE);
|
||||
ret = stream_read(input_stream, buffer, FILE_BUFFER_SIZE);
|
||||
if(ret == 0) {
|
||||
break;
|
||||
}
|
||||
@@ -450,12 +451,11 @@ bool subghz_keystore_raw_encrypted_save(
|
||||
encrypted_line[hex_cursor] = xx[encrypted_line[cursor] & 0xF];
|
||||
encrypted_line[hex_cursor - 1] = xx[(encrypted_line[cursor] >> 4) & 0xF];
|
||||
}
|
||||
storage_file_write(output_file, encrypted_line, strlen(encrypted_line));
|
||||
stream_write_cstring(output_stream, encrypted_line);
|
||||
|
||||
} while(ret > 0 && result);
|
||||
|
||||
flipper_file_close(output_flipper_file);
|
||||
flipper_file_free(output_flipper_file);
|
||||
flipper_format_free(output_flipper_format);
|
||||
|
||||
furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
|
||||
|
||||
@@ -464,8 +464,7 @@ bool subghz_keystore_raw_encrypted_save(
|
||||
encrypted = true;
|
||||
} while(0);
|
||||
|
||||
flipper_file_close(input_flipper_file);
|
||||
flipper_file_free(input_flipper_file);
|
||||
flipper_format_free(input_flipper_format);
|
||||
|
||||
free(encrypted_line);
|
||||
|
||||
@@ -484,19 +483,19 @@ bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t*
|
||||
string_init(str_temp);
|
||||
|
||||
Storage* storage = furi_record_open("storage");
|
||||
char* decrypted_line = furi_alloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
||||
char* decrypted_line = malloc(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE);
|
||||
|
||||
FlipperFile* flipper_file = flipper_file_alloc(storage);
|
||||
FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
|
||||
do {
|
||||
if(!flipper_file_open_existing(flipper_file, file_name)) {
|
||||
if(!flipper_format_file_open_existing(flipper_format, file_name)) {
|
||||
FURI_LOG_E(TAG, "Unable to open file for read: %s", file_name);
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_read_header(flipper_file, str_temp, &version)) {
|
||||
if(!flipper_format_read_header(flipper_format, str_temp, &version)) {
|
||||
FURI_LOG_E(TAG, "Missing or incorrect header");
|
||||
break;
|
||||
}
|
||||
if(!flipper_file_read_uint32(flipper_file, "Encryption", (uint32_t*)&encryption, 1)) {
|
||||
if(!flipper_format_read_uint32(flipper_format, "Encryption", (uint32_t*)&encryption, 1)) {
|
||||
FURI_LOG_E(TAG, "Missing encryption type");
|
||||
break;
|
||||
}
|
||||
@@ -507,21 +506,21 @@ bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t*
|
||||
break;
|
||||
}
|
||||
|
||||
File* file = flipper_file_get_file(flipper_file);
|
||||
Stream* stream = flipper_format_get_raw_stream(flipper_format);
|
||||
if(encryption != SubGhzKeystoreEncryptionAES256) {
|
||||
FURI_LOG_E(TAG, "Unknown encryption");
|
||||
break;
|
||||
}
|
||||
|
||||
if(offset < 16) {
|
||||
if(!flipper_file_read_hex(flipper_file, "IV", iv, 16)) {
|
||||
if(!flipper_format_read_hex(flipper_format, "IV", iv, 16)) {
|
||||
FURI_LOG_E(TAG, "Missing IV");
|
||||
break;
|
||||
}
|
||||
subghz_keystore_mess_with_iv(iv);
|
||||
}
|
||||
|
||||
if(!flipper_file_read_string(flipper_file, "Encrypt_data", str_temp)) {
|
||||
if(!flipper_format_read_string(flipper_format, "Encrypt_data", str_temp)) {
|
||||
FURI_LOG_E(TAG, "Missing Encrypt_data");
|
||||
break;
|
||||
}
|
||||
@@ -534,22 +533,22 @@ bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t*
|
||||
}
|
||||
furi_assert(SUBGHZ_KEYSTORE_FILE_DECRYPTED_LINE_SIZE >= bufer_size / 2);
|
||||
|
||||
char buffer[bufer_size];
|
||||
uint8_t buffer[bufer_size];
|
||||
size_t ret = 0;
|
||||
bool decrypted = true;
|
||||
//skip the end of the previous line "\n"
|
||||
storage_file_read(file, buffer, 1);
|
||||
stream_read(stream, buffer, 1);
|
||||
|
||||
size_t size = storage_file_size(file);
|
||||
size -= storage_file_tell(file);
|
||||
size_t size = stream_size(stream);
|
||||
size -= stream_tell(stream);
|
||||
if(size < (offset * 2 + len * 2)) {
|
||||
FURI_LOG_E(TAG, "Seek position exceeds file size");
|
||||
break;
|
||||
}
|
||||
|
||||
if(offset >= 16) {
|
||||
storage_file_seek(file, ((offset / 16) - 1) * 32, false);
|
||||
ret = storage_file_read(file, buffer, 32);
|
||||
stream_seek(stream, ((offset / 16) - 1) * 32, StreamOffsetFromCurrent);
|
||||
ret = stream_read(stream, buffer, 32);
|
||||
furi_assert(ret == 32);
|
||||
for(uint16_t i = 0; i < ret - 1; i += 2) {
|
||||
uint8_t hi_nibble = 0;
|
||||
@@ -567,7 +566,7 @@ bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t*
|
||||
|
||||
do {
|
||||
memset(buffer, 0, bufer_size);
|
||||
ret = storage_file_read(file, buffer, bufer_size);
|
||||
ret = stream_read(stream, buffer, bufer_size);
|
||||
furi_assert(ret == bufer_size);
|
||||
for(uint16_t i = 0; i < ret - 1; i += 2) {
|
||||
uint8_t hi_nibble = 0;
|
||||
@@ -591,8 +590,7 @@ bool subghz_keystore_raw_get_data(const char* file_name, size_t offset, uint8_t*
|
||||
furi_hal_crypto_store_unload_key(SUBGHZ_KEYSTORE_FILE_ENCRYPTION_KEY_SLOT);
|
||||
if(decrypted) result = true;
|
||||
} while(0);
|
||||
flipper_file_close(flipper_file);
|
||||
flipper_file_free(flipper_file);
|
||||
flipper_format_free(flipper_format);
|
||||
|
||||
furi_record_close("storage");
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ static void subghz_parser_parser_rx_callback(SubGhzProtocolCommon* parser, void*
|
||||
}
|
||||
|
||||
SubGhzParser* subghz_parser_alloc() {
|
||||
SubGhzParser* instance = furi_alloc(sizeof(SubGhzParser));
|
||||
SubGhzParser* instance = malloc(sizeof(SubGhzParser));
|
||||
|
||||
instance->keystore = subghz_keystore_alloc();
|
||||
|
||||
|
||||
@@ -197,7 +197,7 @@ static int32_t subghz_tx_rx_worker_thread(void* context) {
|
||||
}
|
||||
|
||||
SubGhzTxRxWorker* subghz_tx_rx_worker_alloc() {
|
||||
SubGhzTxRxWorker* instance = furi_alloc(sizeof(SubGhzTxRxWorker));
|
||||
SubGhzTxRxWorker* instance = malloc(sizeof(SubGhzTxRxWorker));
|
||||
|
||||
instance->thread = furi_thread_alloc();
|
||||
furi_thread_set_name(instance->thread, "SubghzTxRxWorker");
|
||||
|
||||
@@ -90,7 +90,7 @@ static int32_t subghz_worker_thread_callback(void* context) {
|
||||
}
|
||||
|
||||
SubGhzWorker* subghz_worker_alloc() {
|
||||
SubGhzWorker* instance = furi_alloc(sizeof(SubGhzWorker));
|
||||
SubGhzWorker* instance = malloc(sizeof(SubGhzWorker));
|
||||
|
||||
instance->thread = furi_thread_alloc();
|
||||
furi_thread_set_name(instance->thread, "SubghzWorker");
|
||||
|
||||
@@ -67,7 +67,7 @@ bool saved_struct_load(const char* path, void* data, size_t size, uint8_t magic,
|
||||
|
||||
SavedStructHeader header;
|
||||
|
||||
uint8_t* data_read = furi_alloc(size);
|
||||
uint8_t* data_read = malloc(size);
|
||||
Storage* storage = furi_record_open("storage");
|
||||
File* file = storage_file_alloc(storage);
|
||||
bool result = true;
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
#include "stream.h"
|
||||
#include "stream_i.h"
|
||||
#include "file_stream.h"
|
||||
|
||||
typedef struct {
|
||||
Stream stream_base;
|
||||
Storage* storage;
|
||||
File* file;
|
||||
} FileStream;
|
||||
|
||||
static void file_stream_free(FileStream* stream);
|
||||
static bool file_stream_eof(FileStream* stream);
|
||||
static void file_stream_clean(FileStream* stream);
|
||||
static bool file_stream_seek(FileStream* stream, int32_t offset, StreamOffset offset_type);
|
||||
static size_t file_stream_tell(FileStream* stream);
|
||||
static size_t file_stream_size(FileStream* stream);
|
||||
static size_t file_stream_write(FileStream* stream, const uint8_t* data, size_t size);
|
||||
static size_t file_stream_read(FileStream* stream, uint8_t* data, size_t size);
|
||||
static bool file_stream_delete_and_insert(
|
||||
FileStream* stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_callback,
|
||||
const void* ctx);
|
||||
|
||||
const StreamVTable file_stream_vtable = {
|
||||
.free = (StreamFreeFn)file_stream_free,
|
||||
.eof = (StreamEOFFn)file_stream_eof,
|
||||
.clean = (StreamCleanFn)file_stream_clean,
|
||||
.seek = (StreamSeekFn)file_stream_seek,
|
||||
.tell = (StreamTellFn)file_stream_tell,
|
||||
.size = (StreamSizeFn)file_stream_size,
|
||||
.write = (StreamWriteFn)file_stream_write,
|
||||
.read = (StreamReadFn)file_stream_read,
|
||||
.delete_and_insert = (StreamDeleteAndInsertFn)file_stream_delete_and_insert,
|
||||
};
|
||||
|
||||
Stream* file_stream_alloc(Storage* storage) {
|
||||
FileStream* stream = malloc(sizeof(FileStream));
|
||||
stream->file = storage_file_alloc(storage);
|
||||
stream->storage = storage;
|
||||
|
||||
stream->stream_base.vtable = &file_stream_vtable;
|
||||
return (Stream*)stream;
|
||||
}
|
||||
|
||||
bool file_stream_open(
|
||||
Stream* _stream,
|
||||
const char* path,
|
||||
FS_AccessMode access_mode,
|
||||
FS_OpenMode open_mode) {
|
||||
furi_assert(_stream);
|
||||
FileStream* stream = (FileStream*)_stream;
|
||||
furi_check(stream->stream_base.vtable == &file_stream_vtable);
|
||||
return storage_file_open(stream->file, path, access_mode, open_mode);
|
||||
}
|
||||
|
||||
bool file_stream_close(Stream* _stream) {
|
||||
furi_assert(_stream);
|
||||
FileStream* stream = (FileStream*)_stream;
|
||||
furi_check(stream->stream_base.vtable == &file_stream_vtable);
|
||||
return storage_file_close(stream->file);
|
||||
}
|
||||
|
||||
static void file_stream_free(FileStream* stream) {
|
||||
storage_file_free(stream->file);
|
||||
free(stream);
|
||||
}
|
||||
|
||||
static bool file_stream_eof(FileStream* stream) {
|
||||
return storage_file_eof(stream->file);
|
||||
}
|
||||
|
||||
static void file_stream_clean(FileStream* stream) {
|
||||
storage_file_seek(stream->file, 0, true);
|
||||
storage_file_truncate(stream->file);
|
||||
}
|
||||
|
||||
static bool file_stream_seek(FileStream* stream, int32_t offset, StreamOffset offset_type) {
|
||||
bool result = false;
|
||||
size_t seek_position = 0;
|
||||
size_t current_position = file_stream_tell(stream);
|
||||
size_t size = file_stream_size(stream);
|
||||
|
||||
// calc offset and limit to bottom
|
||||
switch(offset_type) {
|
||||
case StreamOffsetFromCurrent: {
|
||||
if((int32_t)(current_position + offset) >= 0) {
|
||||
seek_position = current_position + offset;
|
||||
result = true;
|
||||
}
|
||||
} break;
|
||||
case StreamOffsetFromStart: {
|
||||
if(offset >= 0) {
|
||||
seek_position = offset;
|
||||
result = true;
|
||||
}
|
||||
} break;
|
||||
case StreamOffsetFromEnd: {
|
||||
if((int32_t)(size + offset) >= 0) {
|
||||
seek_position = size + offset;
|
||||
result = true;
|
||||
}
|
||||
} break;
|
||||
}
|
||||
|
||||
if(result) {
|
||||
// limit to top
|
||||
if((int32_t)(seek_position - size) > 0) {
|
||||
storage_file_seek(stream->file, size, true);
|
||||
result = false;
|
||||
} else {
|
||||
result = storage_file_seek(stream->file, seek_position, true);
|
||||
}
|
||||
} else {
|
||||
storage_file_seek(stream->file, 0, true);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static size_t file_stream_tell(FileStream* stream) {
|
||||
return storage_file_tell(stream->file);
|
||||
}
|
||||
|
||||
static size_t file_stream_size(FileStream* stream) {
|
||||
return storage_file_size(stream->file);
|
||||
}
|
||||
|
||||
static size_t file_stream_write(FileStream* stream, const uint8_t* data, size_t size) {
|
||||
// TODO cache
|
||||
size_t need_to_write = size;
|
||||
while(need_to_write > 0) {
|
||||
uint16_t was_written =
|
||||
storage_file_write(stream->file, data + (size - need_to_write), need_to_write);
|
||||
need_to_write -= was_written;
|
||||
|
||||
if(was_written == 0) break;
|
||||
}
|
||||
|
||||
return size - need_to_write;
|
||||
}
|
||||
|
||||
static size_t file_stream_read(FileStream* stream, uint8_t* data, size_t size) {
|
||||
// TODO cache
|
||||
size_t need_to_read = size;
|
||||
while(need_to_read > 0) {
|
||||
uint16_t was_read =
|
||||
storage_file_read(stream->file, data + (size - need_to_read), need_to_read);
|
||||
need_to_read -= was_read;
|
||||
|
||||
if(was_read == 0) break;
|
||||
}
|
||||
|
||||
return size - need_to_read;
|
||||
}
|
||||
|
||||
static bool file_stream_delete_and_insert(
|
||||
FileStream* _stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_callback,
|
||||
const void* ctx) {
|
||||
bool result = false;
|
||||
Stream* stream = (Stream*)_stream;
|
||||
|
||||
// open scratchpad
|
||||
Stream* scratch_stream = file_stream_alloc(_stream->storage);
|
||||
|
||||
// TODO: we need something like "storage_open_tmpfile and storage_close_tmpfile"
|
||||
string_t scratch_name;
|
||||
string_t tmp_name;
|
||||
string_init(tmp_name);
|
||||
storage_get_next_filename(_stream->storage, "/any", ".scratch", ".pad", tmp_name);
|
||||
string_init_printf(scratch_name, "/any/%s.pad", string_get_cstr(tmp_name));
|
||||
string_clear(tmp_name);
|
||||
|
||||
do {
|
||||
if(!file_stream_open(
|
||||
scratch_stream, string_get_cstr(scratch_name), FSAM_READ_WRITE, FSOM_CREATE_NEW))
|
||||
break;
|
||||
|
||||
size_t current_position = stream_tell(stream);
|
||||
size_t file_size = stream_size(stream);
|
||||
|
||||
size_t size_to_delete = file_size - current_position;
|
||||
size_to_delete = MIN(delete_size, size_to_delete);
|
||||
|
||||
size_t size_to_copy_before = current_position;
|
||||
size_t size_to_copy_after = file_size - current_position - size_to_delete;
|
||||
|
||||
// copy file from 0 to insert position to scratchpad
|
||||
if(!stream_rewind(stream)) break;
|
||||
if(stream_copy(stream, scratch_stream, size_to_copy_before) != size_to_copy_before) break;
|
||||
|
||||
if(write_callback) {
|
||||
if(!write_callback(scratch_stream, ctx)) break;
|
||||
}
|
||||
size_t new_position = stream_tell(scratch_stream);
|
||||
|
||||
// copy key file after insert position + size_to_delete to scratchpad
|
||||
if(!stream_seek(stream, size_to_delete, StreamOffsetFromCurrent)) break;
|
||||
if(stream_copy(stream, scratch_stream, size_to_copy_after) != size_to_copy_after) break;
|
||||
|
||||
size_t new_file_size = stream_size(scratch_stream);
|
||||
|
||||
// copy whole scratchpad file to the original file
|
||||
if(!stream_rewind(stream)) break;
|
||||
if(!stream_rewind(scratch_stream)) break;
|
||||
if(stream_copy(scratch_stream, stream, new_file_size) != new_file_size) break;
|
||||
|
||||
// and truncate original file
|
||||
if(!storage_file_truncate(_stream->file)) break;
|
||||
|
||||
// move seek pointer at insert end
|
||||
if(!stream_seek(stream, new_position, StreamOffsetFromStart)) break;
|
||||
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
stream_free(scratch_stream);
|
||||
storage_common_remove(_stream->storage, string_get_cstr(scratch_name));
|
||||
string_clear(scratch_name);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
#include <stdlib.h>
|
||||
#include <storage/storage.h>
|
||||
#include "stream.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Allocate file stream
|
||||
* @return Stream*
|
||||
*/
|
||||
Stream* file_stream_alloc(Storage* storage);
|
||||
|
||||
/**
|
||||
* Opens an existing file or create a new one.
|
||||
* @param stream pointer to file stream object.
|
||||
* @param path path to file
|
||||
* @param access_mode access mode from FS_AccessMode
|
||||
* @param open_mode open mode from FS_OpenMode
|
||||
* @return success flag. You need to close the file even if the open operation failed.
|
||||
*/
|
||||
bool file_stream_open(
|
||||
Stream* stream,
|
||||
const char* path,
|
||||
FS_AccessMode access_mode,
|
||||
FS_OpenMode open_mode);
|
||||
|
||||
/**
|
||||
* Closes the file.
|
||||
* @param stream
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool file_stream_close(Stream* stream);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,337 @@
|
||||
#include "stream.h"
|
||||
#include "stream_i.h"
|
||||
#include "file_stream.h"
|
||||
#include <furi/check.h>
|
||||
#include <furi/common_defines.h>
|
||||
|
||||
void stream_free(Stream* stream) {
|
||||
furi_assert(stream);
|
||||
stream->vtable->free(stream);
|
||||
}
|
||||
|
||||
void stream_clean(Stream* stream) {
|
||||
furi_assert(stream);
|
||||
stream->vtable->clean(stream);
|
||||
}
|
||||
|
||||
bool stream_eof(Stream* stream) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->eof(stream);
|
||||
}
|
||||
|
||||
bool stream_seek(Stream* stream, int32_t offset, StreamOffset offset_type) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->seek(stream, offset, offset_type);
|
||||
}
|
||||
|
||||
size_t stream_tell(Stream* stream) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->tell(stream);
|
||||
}
|
||||
|
||||
size_t stream_size(Stream* stream) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->size(stream);
|
||||
}
|
||||
|
||||
size_t stream_write(Stream* stream, const uint8_t* data, size_t size) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->write(stream, data, size);
|
||||
}
|
||||
|
||||
size_t stream_read(Stream* stream, uint8_t* data, size_t size) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->read(stream, data, size);
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert(
|
||||
Stream* stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_callback,
|
||||
const void* ctx) {
|
||||
furi_assert(stream);
|
||||
return stream->vtable->delete_and_insert(stream, delete_size, write_callback, ctx);
|
||||
}
|
||||
|
||||
/********************************** Some random helpers starts here **********************************/
|
||||
|
||||
typedef struct {
|
||||
const uint8_t* data;
|
||||
size_t size;
|
||||
} StreamWriteData;
|
||||
|
||||
static bool stream_write_struct(Stream* stream, const void* context) {
|
||||
furi_assert(stream);
|
||||
furi_assert(context);
|
||||
const StreamWriteData* write_data = context;
|
||||
return (stream_write(stream, write_data->data, write_data->size) == write_data->size);
|
||||
}
|
||||
|
||||
bool stream_read_line(Stream* stream, string_t str_result) {
|
||||
string_reset(str_result);
|
||||
const uint8_t buffer_size = 32;
|
||||
uint8_t buffer[buffer_size];
|
||||
|
||||
do {
|
||||
uint16_t bytes_were_read = stream_read(stream, buffer, buffer_size);
|
||||
// TODO process EOF
|
||||
if(bytes_were_read == 0) break;
|
||||
|
||||
bool result = false;
|
||||
bool error = false;
|
||||
for(uint16_t i = 0; i < bytes_were_read; i++) {
|
||||
if(buffer[i] == '\n') {
|
||||
if(!stream_seek(stream, i - bytes_were_read, StreamOffsetFromCurrent)) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
result = true;
|
||||
break;
|
||||
} else if(buffer[i] == '\r') {
|
||||
// Ignore
|
||||
} else {
|
||||
string_push_back(str_result, buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if(result || error) {
|
||||
break;
|
||||
}
|
||||
} while(true);
|
||||
|
||||
return string_size(str_result) != 0;
|
||||
}
|
||||
|
||||
bool stream_rewind(Stream* stream) {
|
||||
furi_assert(stream);
|
||||
return stream_seek(stream, 0, StreamOffsetFromStart);
|
||||
}
|
||||
|
||||
size_t stream_write_char(Stream* stream, char c) {
|
||||
furi_assert(stream);
|
||||
return stream_write(stream, (const uint8_t*)&c, 1);
|
||||
}
|
||||
|
||||
size_t stream_write_string(Stream* stream, string_t string) {
|
||||
furi_assert(stream);
|
||||
return stream_write(stream, (const uint8_t*)string_get_cstr(string), string_size(string));
|
||||
}
|
||||
|
||||
size_t stream_write_cstring(Stream* stream, const char* string) {
|
||||
furi_assert(stream);
|
||||
return stream_write(stream, (const uint8_t*)string, strlen(string));
|
||||
}
|
||||
|
||||
size_t stream_write_format(Stream* stream, const char* format, ...) {
|
||||
furi_assert(stream);
|
||||
size_t size;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
size = stream_write_vaformat(stream, format, args);
|
||||
va_end(args);
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t stream_write_vaformat(Stream* stream, const char* format, va_list args) {
|
||||
furi_assert(stream);
|
||||
string_t data;
|
||||
string_init_vprintf(data, format, args);
|
||||
size_t size = stream_write_string(stream, data);
|
||||
string_clear(data);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
bool stream_insert(Stream* stream, const uint8_t* data, size_t size) {
|
||||
furi_assert(stream);
|
||||
StreamWriteData write_data = {.data = data, .size = size};
|
||||
return stream_delete_and_insert(stream, 0, stream_write_struct, &write_data);
|
||||
}
|
||||
|
||||
bool stream_insert_char(Stream* stream, char c) {
|
||||
furi_assert(stream);
|
||||
return stream_delete_and_insert_char(stream, 0, c);
|
||||
}
|
||||
|
||||
bool stream_insert_string(Stream* stream, string_t string) {
|
||||
furi_assert(stream);
|
||||
return stream_delete_and_insert_string(stream, 0, string);
|
||||
}
|
||||
|
||||
bool stream_insert_cstring(Stream* stream, const char* string) {
|
||||
furi_assert(stream);
|
||||
return stream_delete_and_insert_cstring(stream, 0, string);
|
||||
}
|
||||
|
||||
bool stream_insert_format(Stream* stream, const char* format, ...) {
|
||||
furi_assert(stream);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
bool result = stream_insert_vaformat(stream, format, args);
|
||||
va_end(args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool stream_insert_vaformat(Stream* stream, const char* format, va_list args) {
|
||||
furi_assert(stream);
|
||||
return stream_delete_and_insert_vaformat(stream, 0, format, args);
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert_char(Stream* stream, size_t delete_size, char c) {
|
||||
furi_assert(stream);
|
||||
StreamWriteData write_data = {.data = (uint8_t*)&c, .size = 1};
|
||||
return stream_delete_and_insert(stream, delete_size, stream_write_struct, &write_data);
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert_string(Stream* stream, size_t delete_size, string_t string) {
|
||||
furi_assert(stream);
|
||||
StreamWriteData write_data = {
|
||||
.data = (uint8_t*)string_get_cstr(string), .size = string_size(string)};
|
||||
return stream_delete_and_insert(stream, delete_size, stream_write_struct, &write_data);
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert_cstring(Stream* stream, size_t delete_size, const char* string) {
|
||||
furi_assert(stream);
|
||||
StreamWriteData write_data = {.data = (uint8_t*)string, .size = strlen(string)};
|
||||
return stream_delete_and_insert(stream, delete_size, stream_write_struct, &write_data);
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert_format(Stream* stream, size_t delete_size, const char* format, ...) {
|
||||
furi_assert(stream);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
bool result = stream_delete_and_insert_vaformat(stream, delete_size, format, args);
|
||||
va_end(args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool stream_delete_and_insert_vaformat(
|
||||
Stream* stream,
|
||||
size_t delete_size,
|
||||
const char* format,
|
||||
va_list args) {
|
||||
furi_assert(stream);
|
||||
string_t data;
|
||||
string_init_vprintf(data, format, args);
|
||||
StreamWriteData write_data = {
|
||||
.data = (uint8_t*)string_get_cstr(data), .size = string_size(data)};
|
||||
bool result = stream_delete_and_insert(stream, 0, stream_write_struct, &write_data);
|
||||
string_clear(data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool stream_delete(Stream* stream, size_t size) {
|
||||
furi_assert(stream);
|
||||
return stream_delete_and_insert(stream, size, NULL, NULL);
|
||||
}
|
||||
|
||||
size_t stream_copy(Stream* stream_from, Stream* stream_to, size_t size) {
|
||||
uint8_t* buffer = malloc(STREAM_CACHE_SIZE);
|
||||
size_t copied = 0;
|
||||
|
||||
do {
|
||||
size_t bytes_count = MIN(STREAM_CACHE_SIZE, size - copied);
|
||||
if(bytes_count <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
uint16_t bytes_were_read = stream_read(stream_from, buffer, bytes_count);
|
||||
if(bytes_were_read != bytes_count) break;
|
||||
|
||||
uint16_t bytes_were_written = stream_write(stream_to, buffer, bytes_count);
|
||||
if(bytes_were_written != bytes_count) break;
|
||||
|
||||
copied += bytes_count;
|
||||
} while(true);
|
||||
|
||||
free(buffer);
|
||||
return copied;
|
||||
}
|
||||
|
||||
size_t stream_copy_full(Stream* stream_from, Stream* stream_to) {
|
||||
size_t was_written = 0;
|
||||
|
||||
do {
|
||||
if(!stream_seek(stream_from, 0, StreamOffsetFromStart)) break;
|
||||
if(!stream_seek(stream_to, 0, StreamOffsetFromStart)) break;
|
||||
was_written = stream_copy(stream_from, stream_to, stream_size(stream_from));
|
||||
} while(false);
|
||||
|
||||
return was_written;
|
||||
}
|
||||
|
||||
bool stream_split(Stream* stream, Stream* stream_left, Stream* stream_right) {
|
||||
bool result = false;
|
||||
size_t size = stream_size(stream);
|
||||
size_t tell = stream_tell(stream);
|
||||
|
||||
do {
|
||||
// copy right
|
||||
if(stream_copy(stream, stream_right, size - tell) != (size - tell)) break;
|
||||
|
||||
// copy left
|
||||
if(!stream_rewind(stream)) break;
|
||||
if(stream_copy(stream, stream_left, tell) != tell) break;
|
||||
|
||||
// restore RW pointer
|
||||
if(!stream_seek(stream, tell, StreamOffsetFromStart)) break;
|
||||
result = true;
|
||||
} while(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t stream_load_from_file(Stream* stream, Storage* storage, const char* path) {
|
||||
size_t was_written = 0;
|
||||
Stream* file = file_stream_alloc(storage);
|
||||
|
||||
do {
|
||||
if(!file_stream_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
|
||||
was_written = stream_copy(file, stream, stream_size(file));
|
||||
} while(false);
|
||||
|
||||
stream_free(file);
|
||||
return was_written;
|
||||
}
|
||||
|
||||
size_t stream_save_to_file(Stream* stream, Storage* storage, const char* path, FS_OpenMode mode) {
|
||||
size_t was_written = 0;
|
||||
Stream* file = file_stream_alloc(storage);
|
||||
|
||||
do {
|
||||
if(!file_stream_open(file, path, FSAM_WRITE, mode)) break;
|
||||
was_written = stream_copy(stream, file, stream_size(stream));
|
||||
} while(false);
|
||||
|
||||
stream_free(file);
|
||||
return was_written;
|
||||
}
|
||||
|
||||
void stream_dump_data(Stream* stream) {
|
||||
size_t size = stream_size(stream);
|
||||
size_t tell = stream_tell(stream);
|
||||
printf("stream %p\r\n", stream);
|
||||
printf("size = %u\r\n", size);
|
||||
printf("tell = %u\r\n", tell);
|
||||
printf("DATA START\r\n");
|
||||
uint8_t* data = malloc(STREAM_CACHE_SIZE);
|
||||
stream_rewind(stream);
|
||||
|
||||
while(true) {
|
||||
size_t was_read = stream_read(stream, data, STREAM_CACHE_SIZE);
|
||||
if(was_read == 0) break;
|
||||
|
||||
for(size_t i = 0; i < was_read; i++) {
|
||||
printf("%c", data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
free(data);
|
||||
printf("\r\n");
|
||||
printf("DATA END\r\n");
|
||||
stream_seek(stream, tell, StreamOffsetFromStart);
|
||||
}
|
||||
@@ -0,0 +1,336 @@
|
||||
#pragma once
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct Stream Stream;
|
||||
|
||||
typedef enum {
|
||||
StreamOffsetFromCurrent,
|
||||
StreamOffsetFromStart,
|
||||
StreamOffsetFromEnd,
|
||||
} StreamOffset;
|
||||
|
||||
typedef bool (*StreamWriteCB)(Stream* stream, const void* context);
|
||||
|
||||
/**
|
||||
* Free Stream
|
||||
* @param stream Stream instance
|
||||
*/
|
||||
void stream_free(Stream* stream);
|
||||
|
||||
/**
|
||||
* Clean (empty) Stream
|
||||
* @param stream Stream instance
|
||||
*/
|
||||
void stream_clean(Stream* stream);
|
||||
|
||||
/**
|
||||
* Indicates that the rw pointer is at the end of the stream
|
||||
* @param stream Stream instance
|
||||
* @return true if rw pointer is at the end of the stream
|
||||
* @return false if rw pointer is not at the end of the stream
|
||||
*/
|
||||
bool stream_eof(Stream* stream);
|
||||
|
||||
/**
|
||||
* Moves the rw pointer.
|
||||
* @param stream Stream instance
|
||||
* @param offset how much to move the pointer
|
||||
* @param offset_type starting from what
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool stream_seek(Stream* stream, int32_t offset, StreamOffset offset_type);
|
||||
|
||||
/**
|
||||
* Gets the value of the rw pointer
|
||||
* @param stream Stream instance
|
||||
* @return size_t value of the rw pointer
|
||||
*/
|
||||
size_t stream_tell(Stream* stream);
|
||||
|
||||
/**
|
||||
* Gets the size of the stream
|
||||
* @param stream Stream instance
|
||||
* @return size_t size of the stream
|
||||
*/
|
||||
size_t stream_size(Stream* stream);
|
||||
|
||||
/**
|
||||
* Write N bytes to the stream
|
||||
* @param stream Stream instance
|
||||
* @param data data to write
|
||||
* @param size size of data to be written
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write(Stream* stream, const uint8_t* data, size_t size);
|
||||
|
||||
/**
|
||||
* Read N bytes from stream
|
||||
* @param stream Stream instance
|
||||
* @param data data to be read
|
||||
* @param count size of data to be read
|
||||
* @return size_t how many bytes was read
|
||||
*/
|
||||
size_t stream_read(Stream* stream, uint8_t* data, size_t count);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and write data by calling write_callback(context)
|
||||
* @param stream Stream instance
|
||||
* @param delete_size size of data to be deleted
|
||||
* @param write_callback write callback
|
||||
* @param context write callback context
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert(
|
||||
Stream* stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_callback,
|
||||
const void* context);
|
||||
|
||||
/********************************** Some random helpers starts here **********************************/
|
||||
|
||||
/**
|
||||
* Read line from a stream (supports LF and CRLF line endings)
|
||||
* @param stream
|
||||
* @param str_result
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool stream_read_line(Stream* stream, string_t str_result);
|
||||
|
||||
/**
|
||||
* Moves the rw pointer to the start
|
||||
* @param stream Stream instance
|
||||
*/
|
||||
bool stream_rewind(Stream* stream);
|
||||
|
||||
/**
|
||||
* Write char to the stream
|
||||
* @param stream Stream instance
|
||||
* @param c char value
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write_char(Stream* stream, char c);
|
||||
|
||||
/**
|
||||
* Write string to the stream
|
||||
* @param stream Stream instance
|
||||
* @param string string value
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write_string(Stream* stream, string_t string);
|
||||
|
||||
/**
|
||||
* Write const char* to the stream
|
||||
* @param stream Stream instance
|
||||
* @param string c-string value
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write_cstring(Stream* stream, const char* string);
|
||||
|
||||
/**
|
||||
* Write formatted string to the stream
|
||||
* @param stream Stream instance
|
||||
* @param format
|
||||
* @param ...
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write_format(Stream* stream, const char* format, ...);
|
||||
|
||||
/**
|
||||
* Write formatted string to the stream, va_list version
|
||||
* @param stream Stream instance
|
||||
* @param format
|
||||
* @param args
|
||||
* @return size_t how many bytes was written
|
||||
*/
|
||||
size_t stream_write_vaformat(Stream* stream, const char* format, va_list args);
|
||||
|
||||
/**
|
||||
* Insert N chars to the stream, starting at the current pointer.
|
||||
* Data will be inserted, not overwritteт, so the stream will be increased in size.
|
||||
* @param stream Stream instance
|
||||
* @param data data to be inserted
|
||||
* @param size size of data to be inserted
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert(Stream* stream, const uint8_t* data, size_t size);
|
||||
|
||||
/**
|
||||
* Insert char to the stream
|
||||
* @param stream Stream instance
|
||||
* @param c char value
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert_char(Stream* stream, char c);
|
||||
|
||||
/**
|
||||
* Insert string to the stream
|
||||
* @param stream Stream instance
|
||||
* @param string string value
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert_string(Stream* stream, string_t string);
|
||||
|
||||
/**
|
||||
* Insert const char* to the stream
|
||||
* @param stream Stream instance
|
||||
* @param string c-string value
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert_cstring(Stream* stream, const char* string);
|
||||
|
||||
/**
|
||||
* Insert formatted string to the stream
|
||||
* @param stream Stream instance
|
||||
* @param format
|
||||
* @param ...
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert_format(Stream* stream, const char* format, ...);
|
||||
|
||||
/**
|
||||
* Insert formatted string to the stream, va_list version
|
||||
* @param stream Stream instance
|
||||
* @param format
|
||||
* @param args
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_insert_vaformat(Stream* stream, const char* format, va_list args);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and insert char to the stream
|
||||
* @param stream Stream instance
|
||||
* @param delete_size size of data to be deleted
|
||||
* @param c char value
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert_char(Stream* stream, size_t delete_size, char c);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and insert string to the stream
|
||||
* @param stream Stream instance
|
||||
* @param delete_size size of data to be deleted
|
||||
* @param string string value
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert_string(Stream* stream, size_t delete_size, string_t string);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and insert const char* to the stream
|
||||
* @param stream Stream instance
|
||||
* @param delete_size size of data to be deleted
|
||||
* @param string c-string value
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert_cstring(Stream* stream, size_t delete_size, const char* string);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and insert formatted string to the stream
|
||||
* @param stream Stream instance
|
||||
* @param delete_size size of data to be deleted
|
||||
* @param format
|
||||
* @param ...
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert_format(Stream* stream, size_t delete_size, const char* format, ...);
|
||||
|
||||
/**
|
||||
* Delete N chars from the stream and insert formatted string to the stream, va_list version
|
||||
* @param stream Stream instance
|
||||
* @param delete_size size of data to be deleted
|
||||
* @param format
|
||||
* @param args
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete_and_insert_vaformat(
|
||||
Stream* stream,
|
||||
size_t delete_size,
|
||||
const char* format,
|
||||
va_list args);
|
||||
|
||||
/**
|
||||
* Remove N chars from the stream, starting at the current pointer.
|
||||
* The size may be larger than stream size, the stream will be cleared from current rw pointer to the end.
|
||||
* @param stream Stream instance
|
||||
* @param size how many chars need to be deleted
|
||||
* @return true if the operation was successful
|
||||
* @return false on error
|
||||
*/
|
||||
bool stream_delete(Stream* stream, size_t size);
|
||||
|
||||
/**
|
||||
* Copy data from one stream to another. Data will be copied from current rw pointer and to current rw pointer.
|
||||
* @param stream_from
|
||||
* @param stream_to
|
||||
* @param size
|
||||
* @return size_t
|
||||
*/
|
||||
size_t stream_copy(Stream* stream_from, Stream* stream_to, size_t size);
|
||||
|
||||
/**
|
||||
* Copy data from one stream to another. Data will be copied from start of one stream and to start of other stream.
|
||||
* @param stream_from
|
||||
* @param stream_to
|
||||
* @return size_t
|
||||
*/
|
||||
size_t stream_copy_full(Stream* stream_from, Stream* stream_to);
|
||||
|
||||
/**
|
||||
* Splits one stream into two others. The original stream will remain untouched.
|
||||
* @param stream
|
||||
* @param stream_left
|
||||
* @param stream_right
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool stream_split(Stream* stream, Stream* stream_left, Stream* stream_right);
|
||||
|
||||
/**
|
||||
* Loads data to the stream from a file. Data will be loaded to the current RW pointer. RW pointer will be moved to the end of the stream.
|
||||
* @param stream Stream instance
|
||||
* @param storage
|
||||
* @param path
|
||||
* @return size_t
|
||||
*/
|
||||
size_t stream_load_from_file(Stream* stream, Storage* storage, const char* path);
|
||||
|
||||
/**
|
||||
* Writes data from a stream to a file. Data will be saved starting from the current RW pointer. RW pointer will be moved to the end of the stream.
|
||||
* @param stream Stream instance
|
||||
* @param storage
|
||||
* @param path
|
||||
* @param mode
|
||||
* @return size_t
|
||||
*/
|
||||
size_t stream_save_to_file(Stream* stream, Storage* storage, const char* path, FS_OpenMode mode);
|
||||
|
||||
/**
|
||||
* Dump stream inner data (size, RW positiot, content)
|
||||
* @param stream Stream instance
|
||||
*/
|
||||
void stream_dump_data(Stream* stream);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "stream.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define STREAM_CACHE_SIZE 512
|
||||
|
||||
typedef struct StreamVTable StreamVTable;
|
||||
|
||||
typedef void (*StreamFreeFn)(Stream* stream);
|
||||
typedef bool (*StreamEOFFn)(Stream* stream);
|
||||
typedef void (*StreamCleanFn)(Stream* stream);
|
||||
typedef bool (*StreamSeekFn)(Stream* stream, int32_t offset, StreamOffset offset_type);
|
||||
typedef size_t (*StreamTellFn)(Stream* stream);
|
||||
typedef size_t (*StreamSizeFn)(Stream* stream);
|
||||
typedef size_t (*StreamWriteFn)(Stream* stream, const uint8_t* data, size_t size);
|
||||
typedef size_t (*StreamReadFn)(Stream* stream, uint8_t* data, size_t count);
|
||||
typedef bool (*StreamDeleteAndInsertFn)(
|
||||
Stream* stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_cb,
|
||||
const void* ctx);
|
||||
|
||||
struct StreamVTable {
|
||||
const StreamFreeFn free;
|
||||
const StreamEOFFn eof;
|
||||
const StreamCleanFn clean;
|
||||
const StreamSeekFn seek;
|
||||
const StreamTellFn tell;
|
||||
const StreamSizeFn size;
|
||||
const StreamWriteFn write;
|
||||
const StreamReadFn read;
|
||||
const StreamDeleteAndInsertFn delete_and_insert;
|
||||
};
|
||||
|
||||
struct Stream {
|
||||
const StreamVTable* vtable;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,179 @@
|
||||
#include "stream.h"
|
||||
#include "stream_i.h"
|
||||
#include "string_stream.h"
|
||||
#include <furi/common_defines.h>
|
||||
|
||||
typedef struct {
|
||||
Stream stream_base;
|
||||
string_t string;
|
||||
size_t index;
|
||||
} StringStream;
|
||||
|
||||
static size_t string_stream_write_char(StringStream* stream, char c);
|
||||
|
||||
static void string_stream_free(StringStream* stream);
|
||||
static bool string_stream_eof(StringStream* stream);
|
||||
static void string_stream_clean(StringStream* stream);
|
||||
static bool string_stream_seek(StringStream* stream, int32_t offset, StreamOffset offset_type);
|
||||
static size_t string_stream_tell(StringStream* stream);
|
||||
static size_t string_stream_size(StringStream* stream);
|
||||
static size_t string_stream_write(StringStream* stream, const char* data, size_t size);
|
||||
static size_t string_stream_read(StringStream* stream, char* data, size_t size);
|
||||
static bool string_stream_delete_and_insert(
|
||||
StringStream* stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_callback,
|
||||
const void* ctx);
|
||||
|
||||
const StreamVTable string_stream_vtable = {
|
||||
.free = (StreamFreeFn)string_stream_free,
|
||||
.eof = (StreamEOFFn)string_stream_eof,
|
||||
.clean = (StreamCleanFn)string_stream_clean,
|
||||
.seek = (StreamSeekFn)string_stream_seek,
|
||||
.tell = (StreamTellFn)string_stream_tell,
|
||||
.size = (StreamSizeFn)string_stream_size,
|
||||
.write = (StreamWriteFn)string_stream_write,
|
||||
.read = (StreamReadFn)string_stream_read,
|
||||
.delete_and_insert = (StreamDeleteAndInsertFn)string_stream_delete_and_insert,
|
||||
};
|
||||
|
||||
Stream* string_stream_alloc() {
|
||||
StringStream* stream = malloc(sizeof(StringStream));
|
||||
string_init(stream->string);
|
||||
stream->index = 0;
|
||||
stream->stream_base.vtable = &string_stream_vtable;
|
||||
return (Stream*)stream;
|
||||
}
|
||||
|
||||
static void string_stream_free(StringStream* stream) {
|
||||
string_clear(stream->string);
|
||||
free(stream);
|
||||
}
|
||||
|
||||
static bool string_stream_eof(StringStream* stream) {
|
||||
return (string_stream_tell(stream) >= string_stream_size(stream));
|
||||
}
|
||||
|
||||
static void string_stream_clean(StringStream* stream) {
|
||||
stream->index = 0;
|
||||
string_reset(stream->string);
|
||||
}
|
||||
|
||||
static bool string_stream_seek(StringStream* stream, int32_t offset, StreamOffset offset_type) {
|
||||
bool result = true;
|
||||
switch(offset_type) {
|
||||
case StreamOffsetFromStart:
|
||||
if(offset >= 0) {
|
||||
stream->index = offset;
|
||||
} else {
|
||||
result = false;
|
||||
stream->index = 0;
|
||||
}
|
||||
break;
|
||||
case StreamOffsetFromCurrent:
|
||||
if(((int32_t)stream->index + offset) >= 0) {
|
||||
stream->index += offset;
|
||||
} else {
|
||||
result = false;
|
||||
stream->index = 0;
|
||||
}
|
||||
break;
|
||||
case StreamOffsetFromEnd:
|
||||
if(((int32_t)string_size(stream->string) + offset) >= 0) {
|
||||
stream->index = string_size(stream->string) + offset;
|
||||
} else {
|
||||
result = false;
|
||||
stream->index = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
int32_t diff = (stream->index - string_size(stream->string));
|
||||
if(diff > 0) {
|
||||
stream->index -= diff;
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static size_t string_stream_tell(StringStream* stream) {
|
||||
return stream->index;
|
||||
}
|
||||
|
||||
static size_t string_stream_size(StringStream* stream) {
|
||||
return string_size(stream->string);
|
||||
}
|
||||
|
||||
static size_t string_stream_write(StringStream* stream, const char* data, size_t size) {
|
||||
// TODO: can be optimized for edge cases
|
||||
size_t i;
|
||||
for(i = 0; i < size; i++) {
|
||||
string_stream_write_char(stream, data[i]);
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
static size_t string_stream_read(StringStream* stream, char* data, size_t size) {
|
||||
size_t write_index = 0;
|
||||
const char* cstr = string_get_cstr(stream->string);
|
||||
|
||||
if(!string_stream_eof(stream)) {
|
||||
while(true) {
|
||||
if(write_index >= size) break;
|
||||
|
||||
data[write_index] = cstr[stream->index];
|
||||
write_index++;
|
||||
string_stream_seek(stream, 1, StreamOffsetFromCurrent);
|
||||
if(string_stream_eof(stream)) break;
|
||||
}
|
||||
}
|
||||
|
||||
return write_index;
|
||||
}
|
||||
|
||||
static bool string_stream_delete_and_insert(
|
||||
StringStream* stream,
|
||||
size_t delete_size,
|
||||
StreamWriteCB write_callback,
|
||||
const void* ctx) {
|
||||
bool result = true;
|
||||
|
||||
if(delete_size) {
|
||||
size_t remain_size = string_stream_size(stream) - string_stream_tell(stream);
|
||||
remain_size = MIN(delete_size, remain_size);
|
||||
|
||||
if(remain_size != 0) {
|
||||
string_replace_at(stream->string, stream->index, remain_size, "");
|
||||
}
|
||||
}
|
||||
|
||||
if(write_callback) {
|
||||
string_t right;
|
||||
string_init_set(right, &string_get_cstr(stream->string)[stream->index]);
|
||||
string_left(stream->string, string_stream_tell(stream));
|
||||
result &= write_callback((Stream*)stream, ctx);
|
||||
string_cat(stream->string, right);
|
||||
string_clear(right);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to string stream helper
|
||||
* @param stream
|
||||
* @param c
|
||||
* @return size_t
|
||||
*/
|
||||
static size_t string_stream_write_char(StringStream* stream, char c) {
|
||||
if(string_stream_eof(stream)) {
|
||||
string_push_back(stream->string, c);
|
||||
} else {
|
||||
string_set_char(stream->string, stream->index, c);
|
||||
}
|
||||
stream->index++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include <stdlib.h>
|
||||
#include <mlib/m-string.h>
|
||||
#include "stream.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Allocate string stream
|
||||
* @return Stream*
|
||||
*/
|
||||
Stream* string_stream_alloc();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user