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

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

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
SG
2022-02-19 05:53:46 +10:00
committed by GitHub
parent 242241987e
commit 274c12fc56
257 changed files with 4480 additions and 2657 deletions
+3
View File
@@ -7,6 +7,9 @@
#define TAG "Main"
int main(void) {
// Flipper critical FURI HAL
furi_hal_init_critical();
// Initialize FURI layer
furi_init();
@@ -17,7 +17,7 @@ static const uint16_t service_uuid = BATTERY_SERVICE_UUID;
static const uint16_t char_battery_level_uuid = BATTERY_LEVEL_CHAR_UUID;
void battery_svc_start() {
battery_svc = furi_alloc(sizeof(BatterySvc));
battery_svc = malloc(sizeof(BatterySvc));
tBleStatus status;
// Add Battery service
+1 -1
View File
@@ -35,7 +35,7 @@ static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status);
bool ble_app_init() {
SHCI_CmdStatus_t status;
ble_app = furi_alloc(sizeof(BleApp));
ble_app = malloc(sizeof(BleApp));
// Allocate semafore and mutex for ble command buffer access
ble_app->hci_mtx = osMutexNew(NULL);
ble_app->hci_sem = osSemaphoreNew(1, 0, NULL);
+1 -1
View File
@@ -64,7 +64,7 @@ void ble_glue_set_key_storage_changed_callback(
}
void ble_glue_init() {
ble_glue = furi_alloc(sizeof(BleGlue));
ble_glue = malloc(sizeof(BleGlue));
ble_glue->status = BleGlueStatusStartup;
// Configure the system Power Mode
@@ -23,7 +23,7 @@ static const char dev_info_software_rev_num[] = GIT_COMMIT " " GIT_BRANCH " " GI
" " BUILD_DATE;
void dev_info_svc_start() {
dev_info_svc = furi_alloc(sizeof(DevInfoSvc));
dev_info_svc = malloc(sizeof(DevInfoSvc));
tBleStatus status;
// Add Device Information Service
+2 -2
View File
@@ -463,7 +463,7 @@ bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
return false;
}
gap = furi_alloc(sizeof(Gap));
gap = malloc(sizeof(Gap));
gap->config = config;
srand(DWT->CYCCNT);
// Create advertising timer
@@ -516,7 +516,7 @@ GapState gap_get_state() {
void gap_start_scan(GapScanCallback callback, void* context) {
furi_assert(callback);
gap_scan = furi_alloc(sizeof(GapScan));
gap_scan = malloc(sizeof(GapScan));
gap_scan->callback = callback;
gap_scan->context = context;
// Scan interval 250 ms
+1 -1
View File
@@ -38,7 +38,7 @@ static SVCCTL_EvtAckStatus_t hid_svc_event_handler(void* event) {
void hid_svc_start() {
tBleStatus status;
hid_svc = furi_alloc(sizeof(HIDSvc));
hid_svc = malloc(sizeof(HIDSvc));
Service_UUID_t svc_uuid = {};
Char_Desc_Uuid_t desc_uuid = {};
Char_UUID_t char_uuid = {};
@@ -82,7 +82,7 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
void serial_svc_start() {
tBleStatus status;
serial_svc = furi_alloc(sizeof(SerialSvc));
serial_svc = malloc(sizeof(SerialSvc));
// Register event handler
SVCCTL_RegisterSvcHandler(serial_svc_event_handler);
@@ -435,7 +435,7 @@ uint8_t
goto error;
}
ptr = furi_alloc(sizeof(uint8_t) * BlockSize);
ptr = malloc(sizeof(uint8_t) * BlockSize);
if(ptr == NULL) {
goto error;
}
@@ -516,7 +516,7 @@ uint8_t BSP_SD_WriteBlocks(
goto error;
}
ptr = furi_alloc(sizeof(uint8_t) * BlockSize);
ptr = malloc(sizeof(uint8_t) * BlockSize);
if(ptr == NULL) {
goto error;
}
+5 -2
View File
@@ -11,9 +11,7 @@
#define TAG "FuriHal"
void furi_hal_init() {
furi_hal_clock_init();
furi_hal_rtc_init();
furi_hal_console_init();
furi_hal_interrupt_init();
furi_hal_delay_init();
@@ -71,3 +69,8 @@ void furi_hal_init() {
LL_MPU_INSTRUCTION_ACCESS_ENABLE);
LL_MPU_Enable(LL_MPU_CTRL_PRIVILEGED_DEFAULT);
}
void furi_hal_init_critical() {
furi_hal_clock_init();
furi_hal_console_init();
}
@@ -147,8 +147,8 @@ void furi_hal_bt_hid_start() {
hid_svc_start();
}
// Configure HID Keyboard
kb_report = furi_alloc(sizeof(FuriHalBtHidKbReport));
media_report = furi_alloc(sizeof(FuriHalBtHidMediaReport));
kb_report = malloc(sizeof(FuriHalBtHidKbReport));
media_report = malloc(sizeof(FuriHalBtHidMediaReport));
// Configure Report Map characteristic
hid_svc_update_report_map(
furi_hal_bt_hid_report_map_data, sizeof(furi_hal_bt_hid_report_map_data));
@@ -41,7 +41,7 @@ static void furi_hal_compress_reset(FuriHalCompress* compress) {
}
void furi_hal_compress_icon_init() {
icon_decoder = furi_alloc(sizeof(FuriHalCompressIcon));
icon_decoder = malloc(sizeof(FuriHalCompressIcon));
icon_decoder->decoder = heatshrink_decoder_alloc(
icon_decoder->compress_buff,
FURI_HAL_COMPRESS_ICON_ENCODED_BUFF_SIZE,
@@ -84,8 +84,8 @@ void furi_hal_compress_icon_decode(const uint8_t* icon_data, uint8_t** decoded_b
}
FuriHalCompress* furi_hal_compress_alloc(uint16_t compress_buff_size) {
FuriHalCompress* compress = furi_alloc(sizeof(FuriHalCompress));
compress->compress_buff = furi_alloc(compress_buff_size + FURI_HAL_COMPRESS_EXP_BUFF_SIZE);
FuriHalCompress* compress = malloc(sizeof(FuriHalCompress));
compress->compress_buff = malloc(compress_buff_size + FURI_HAL_COMPRESS_EXP_BUFF_SIZE);
compress->encoder = heatshrink_encoder_alloc(
compress->compress_buff,
FURI_HAL_COMPRESS_EXP_BUFF_SIZE_LOG,
@@ -12,15 +12,17 @@
#define TAG "FuriHalConsole"
#ifdef HEAP_PRINT_DEBUG
#define CONSOLE_BAUDRATE 1843200
#else
#define CONSOLE_BAUDRATE 230400
#endif
volatile bool furi_hal_console_alive = false;
void furi_hal_console_init() {
furi_hal_uart_init(FuriHalUartIdUSART1, CONSOLE_BAUDRATE);
furi_hal_console_alive = true;
FURI_LOG_I(TAG, "Init OK");
}
void furi_hal_console_enable() {
+4 -4
View File
@@ -588,13 +588,13 @@ void furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle) {
furi_assert(irda_tim_tx.buffer[1].polarity == NULL);
size_t alloc_size_data = IRDA_TIM_TX_DMA_BUFFER_SIZE * sizeof(uint16_t);
irda_tim_tx.buffer[0].data = furi_alloc(alloc_size_data);
irda_tim_tx.buffer[1].data = furi_alloc(alloc_size_data);
irda_tim_tx.buffer[0].data = malloc(alloc_size_data);
irda_tim_tx.buffer[1].data = malloc(alloc_size_data);
size_t alloc_size_polarity =
(IRDA_TIM_TX_DMA_BUFFER_SIZE + IRDA_POLARITY_SHIFT) * sizeof(uint8_t);
irda_tim_tx.buffer[0].polarity = furi_alloc(alloc_size_polarity);
irda_tim_tx.buffer[1].polarity = furi_alloc(alloc_size_polarity);
irda_tim_tx.buffer[0].polarity = malloc(alloc_size_polarity);
irda_tim_tx.buffer[1].polarity = malloc(alloc_size_polarity);
irda_tim_tx.stop_semaphore = osSemaphoreNew(1, 0, NULL);
irda_tim_tx.cycle_duration = 1000000.0 / freq;
@@ -874,7 +874,7 @@ bool furi_hal_subghz_start_async_tx(FuriHalSubGhzAsyncTxCallback callback, void*
furi_hal_subghz_async_tx.duty_high = 0;
furi_hal_subghz_async_tx.buffer =
furi_alloc(API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL * sizeof(uint32_t));
malloc(API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL * sizeof(uint32_t));
furi_hal_subghz_async_tx_refill(
furi_hal_subghz_async_tx.buffer, API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL);
@@ -434,14 +434,14 @@ static void cdc_init(usbd_device* dev, FuriHalUsbInterface* intf) {
char* name = (char*)furi_hal_version_get_device_name_ptr();
uint8_t len = (name == NULL) ? (0) : (strlen(name));
struct usb_string_descriptor* dev_prod_desc = furi_alloc(len * 2 + 2);
struct usb_string_descriptor* dev_prod_desc = malloc(len * 2 + 2);
dev_prod_desc->bLength = len * 2 + 2;
dev_prod_desc->bDescriptorType = USB_DTYPE_STRING;
for(uint8_t i = 0; i < len; i++) dev_prod_desc->wString[i] = name[i];
name = (char*)furi_hal_version_get_name_ptr();
len = (name == NULL) ? (0) : (strlen(name));
struct usb_string_descriptor* dev_serial_desc = furi_alloc((len + 5) * 2 + 2);
struct usb_string_descriptor* dev_serial_desc = malloc((len + 5) * 2 + 2);
dev_serial_desc->bLength = (len + 5) * 2 + 2;
dev_serial_desc->bDescriptorType = USB_DTYPE_STRING;
memcpy(dev_serial_desc->wString, "f\0l\0i\0p\0_\0", 5 * 2);
+1 -1
View File
@@ -58,7 +58,7 @@ static const uint8_t ascii_soh = 0x01;
static const uint8_t ascii_eot = 0x04;
void furi_hal_vcp_init() {
vcp = furi_alloc(sizeof(FuriHalVcp));
vcp = malloc(sizeof(FuriHalVcp));
vcp->connected = false;
vcp->tx_stream = xStreamBufferCreate(VCP_TX_BUF_SIZE, 1);
+3
View File
@@ -7,6 +7,9 @@
#define TAG "Main"
int main(void) {
// Flipper critical FURI HAL
furi_hal_init_critical();
// Initialize FURI layer
furi_init();
@@ -17,7 +17,7 @@ static const uint16_t service_uuid = BATTERY_SERVICE_UUID;
static const uint16_t char_battery_level_uuid = BATTERY_LEVEL_CHAR_UUID;
void battery_svc_start() {
battery_svc = furi_alloc(sizeof(BatterySvc));
battery_svc = malloc(sizeof(BatterySvc));
tBleStatus status;
// Add Battery service
+1 -1
View File
@@ -35,7 +35,7 @@ static void ble_app_hci_status_not_handler(HCI_TL_CmdStatus_t status);
bool ble_app_init() {
SHCI_CmdStatus_t status;
ble_app = furi_alloc(sizeof(BleApp));
ble_app = malloc(sizeof(BleApp));
// Allocate semafore and mutex for ble command buffer access
ble_app->hci_mtx = osMutexNew(NULL);
ble_app->hci_sem = osSemaphoreNew(1, 0, NULL);
+1 -1
View File
@@ -64,7 +64,7 @@ void ble_glue_set_key_storage_changed_callback(
}
void ble_glue_init() {
ble_glue = furi_alloc(sizeof(BleGlue));
ble_glue = malloc(sizeof(BleGlue));
ble_glue->status = BleGlueStatusStartup;
// Configure the system Power Mode
@@ -23,7 +23,7 @@ static const char dev_info_software_rev_num[] = GIT_COMMIT " " GIT_BRANCH " " GI
" " BUILD_DATE;
void dev_info_svc_start() {
dev_info_svc = furi_alloc(sizeof(DevInfoSvc));
dev_info_svc = malloc(sizeof(DevInfoSvc));
tBleStatus status;
// Add Device Information Service
+2 -2
View File
@@ -463,7 +463,7 @@ bool gap_init(GapConfig* config, GapEventCallback on_event_cb, void* context) {
return false;
}
gap = furi_alloc(sizeof(Gap));
gap = malloc(sizeof(Gap));
gap->config = config;
srand(DWT->CYCCNT);
// Create advertising timer
@@ -516,7 +516,7 @@ GapState gap_get_state() {
void gap_start_scan(GapScanCallback callback, void* context) {
furi_assert(callback);
gap_scan = furi_alloc(sizeof(GapScan));
gap_scan = malloc(sizeof(GapScan));
gap_scan->callback = callback;
gap_scan->context = context;
// Scan interval 250 ms
+1 -1
View File
@@ -38,7 +38,7 @@ static SVCCTL_EvtAckStatus_t hid_svc_event_handler(void* event) {
void hid_svc_start() {
tBleStatus status;
hid_svc = furi_alloc(sizeof(HIDSvc));
hid_svc = malloc(sizeof(HIDSvc));
Service_UUID_t svc_uuid = {};
Char_Desc_Uuid_t desc_uuid = {};
Char_UUID_t char_uuid = {};
@@ -82,7 +82,7 @@ static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void* event) {
void serial_svc_start() {
tBleStatus status;
serial_svc = furi_alloc(sizeof(SerialSvc));
serial_svc = malloc(sizeof(SerialSvc));
// Register event handler
SVCCTL_RegisterSvcHandler(serial_svc_event_handler);
@@ -435,7 +435,7 @@ uint8_t
goto error;
}
ptr = furi_alloc(sizeof(uint8_t) * BlockSize);
ptr = malloc(sizeof(uint8_t) * BlockSize);
if(ptr == NULL) {
goto error;
}
@@ -516,7 +516,7 @@ uint8_t BSP_SD_WriteBlocks(
goto error;
}
ptr = furi_alloc(sizeof(uint8_t) * BlockSize);
ptr = malloc(sizeof(uint8_t) * BlockSize);
if(ptr == NULL) {
goto error;
}
+5 -2
View File
@@ -11,9 +11,7 @@
#define TAG "FuriHal"
void furi_hal_init() {
furi_hal_clock_init();
furi_hal_rtc_init();
furi_hal_console_init();
furi_hal_interrupt_init();
furi_hal_delay_init();
@@ -71,3 +69,8 @@ void furi_hal_init() {
LL_MPU_INSTRUCTION_ACCESS_ENABLE);
LL_MPU_Enable(LL_MPU_CTRL_PRIVILEGED_DEFAULT);
}
void furi_hal_init_critical() {
furi_hal_clock_init();
furi_hal_console_init();
}
@@ -147,8 +147,8 @@ void furi_hal_bt_hid_start() {
hid_svc_start();
}
// Configure HID Keyboard
kb_report = furi_alloc(sizeof(FuriHalBtHidKbReport));
media_report = furi_alloc(sizeof(FuriHalBtHidMediaReport));
kb_report = malloc(sizeof(FuriHalBtHidKbReport));
media_report = malloc(sizeof(FuriHalBtHidMediaReport));
// Configure Report Map characteristic
hid_svc_update_report_map(
furi_hal_bt_hid_report_map_data, sizeof(furi_hal_bt_hid_report_map_data));
@@ -41,7 +41,7 @@ static void furi_hal_compress_reset(FuriHalCompress* compress) {
}
void furi_hal_compress_icon_init() {
icon_decoder = furi_alloc(sizeof(FuriHalCompressIcon));
icon_decoder = malloc(sizeof(FuriHalCompressIcon));
icon_decoder->decoder = heatshrink_decoder_alloc(
icon_decoder->compress_buff,
FURI_HAL_COMPRESS_ICON_ENCODED_BUFF_SIZE,
@@ -84,8 +84,8 @@ void furi_hal_compress_icon_decode(const uint8_t* icon_data, uint8_t** decoded_b
}
FuriHalCompress* furi_hal_compress_alloc(uint16_t compress_buff_size) {
FuriHalCompress* compress = furi_alloc(sizeof(FuriHalCompress));
compress->compress_buff = furi_alloc(compress_buff_size + FURI_HAL_COMPRESS_EXP_BUFF_SIZE);
FuriHalCompress* compress = malloc(sizeof(FuriHalCompress));
compress->compress_buff = malloc(compress_buff_size + FURI_HAL_COMPRESS_EXP_BUFF_SIZE);
compress->encoder = heatshrink_encoder_alloc(
compress->compress_buff,
FURI_HAL_COMPRESS_EXP_BUFF_SIZE_LOG,
@@ -12,15 +12,17 @@
#define TAG "FuriHalConsole"
#ifdef HEAP_PRINT_DEBUG
#define CONSOLE_BAUDRATE 1843200
#else
#define CONSOLE_BAUDRATE 230400
#endif
volatile bool furi_hal_console_alive = false;
void furi_hal_console_init() {
furi_hal_uart_init(FuriHalUartIdUSART1, CONSOLE_BAUDRATE);
furi_hal_console_alive = true;
FURI_LOG_I(TAG, "Init OK");
}
void furi_hal_console_enable() {
+4 -4
View File
@@ -588,13 +588,13 @@ void furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle) {
furi_assert(irda_tim_tx.buffer[1].polarity == NULL);
size_t alloc_size_data = IRDA_TIM_TX_DMA_BUFFER_SIZE * sizeof(uint16_t);
irda_tim_tx.buffer[0].data = furi_alloc(alloc_size_data);
irda_tim_tx.buffer[1].data = furi_alloc(alloc_size_data);
irda_tim_tx.buffer[0].data = malloc(alloc_size_data);
irda_tim_tx.buffer[1].data = malloc(alloc_size_data);
size_t alloc_size_polarity =
(IRDA_TIM_TX_DMA_BUFFER_SIZE + IRDA_POLARITY_SHIFT) * sizeof(uint8_t);
irda_tim_tx.buffer[0].polarity = furi_alloc(alloc_size_polarity);
irda_tim_tx.buffer[1].polarity = furi_alloc(alloc_size_polarity);
irda_tim_tx.buffer[0].polarity = malloc(alloc_size_polarity);
irda_tim_tx.buffer[1].polarity = malloc(alloc_size_polarity);
irda_tim_tx.stop_semaphore = osSemaphoreNew(1, 0, NULL);
irda_tim_tx.cycle_duration = 1000000.0 / freq;
@@ -874,7 +874,7 @@ bool furi_hal_subghz_start_async_tx(FuriHalSubGhzAsyncTxCallback callback, void*
furi_hal_subghz_async_tx.duty_high = 0;
furi_hal_subghz_async_tx.buffer =
furi_alloc(API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL * sizeof(uint32_t));
malloc(API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL * sizeof(uint32_t));
furi_hal_subghz_async_tx_refill(
furi_hal_subghz_async_tx.buffer, API_HAL_SUBGHZ_ASYNC_TX_BUFFER_FULL);
@@ -434,14 +434,14 @@ static void cdc_init(usbd_device* dev, FuriHalUsbInterface* intf) {
char* name = (char*)furi_hal_version_get_device_name_ptr();
uint8_t len = (name == NULL) ? (0) : (strlen(name));
struct usb_string_descriptor* dev_prod_desc = furi_alloc(len * 2 + 2);
struct usb_string_descriptor* dev_prod_desc = malloc(len * 2 + 2);
dev_prod_desc->bLength = len * 2 + 2;
dev_prod_desc->bDescriptorType = USB_DTYPE_STRING;
for(uint8_t i = 0; i < len; i++) dev_prod_desc->wString[i] = name[i];
name = (char*)furi_hal_version_get_name_ptr();
len = (name == NULL) ? (0) : (strlen(name));
struct usb_string_descriptor* dev_serial_desc = furi_alloc((len + 5) * 2 + 2);
struct usb_string_descriptor* dev_serial_desc = malloc((len + 5) * 2 + 2);
dev_serial_desc->bLength = (len + 5) * 2 + 2;
dev_serial_desc->bDescriptorType = USB_DTYPE_STRING;
memcpy(dev_serial_desc->wString, "f\0l\0i\0p\0_\0", 5 * 2);
+1 -1
View File
@@ -58,7 +58,7 @@ static const uint8_t ascii_soh = 0x01;
static const uint8_t ascii_eot = 0x04;
void furi_hal_vcp_init() {
vcp = furi_alloc(sizeof(FuriHalVcp));
vcp = malloc(sizeof(FuriHalVcp));
vcp->connected = false;
vcp->tx_stream = xStreamBufferCreate(VCP_TX_BUF_SIZE, 1);
@@ -44,3 +44,9 @@ template <unsigned int N> struct STOP_EXTERNING_ME {};
/** Init furi_hal */
void furi_hal_init();
/**
* Init critical parts of furi_hal
* That code should not use memory allocations
*/
void furi_hal_init_critical();