* 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>
		
			
				
	
	
		
			133 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			133 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include "stdglue.h"
 | 
						|
#include "check.h"
 | 
						|
#include "memmgr.h"
 | 
						|
 | 
						|
#include <FreeRTOS.h>
 | 
						|
#include <task.h>
 | 
						|
 | 
						|
#include <furi_hal.h>
 | 
						|
#include <m-dict.h>
 | 
						|
 | 
						|
DICT_DEF2(
 | 
						|
    FuriStdglueCallbackDict,
 | 
						|
    uint32_t,
 | 
						|
    M_DEFAULT_OPLIST,
 | 
						|
    FuriStdglueWriteCallback,
 | 
						|
    M_PTR_OPLIST)
 | 
						|
 | 
						|
typedef struct {
 | 
						|
    osMutexId_t mutex;
 | 
						|
    FuriStdglueCallbackDict_t global_outputs;
 | 
						|
    FuriStdglueCallbackDict_t thread_outputs;
 | 
						|
} FuriStdglue;
 | 
						|
 | 
						|
static FuriStdglue* furi_stdglue = NULL;
 | 
						|
 | 
						|
static ssize_t stdout_write(void* _cookie, const char* data, size_t size) {
 | 
						|
    furi_assert(furi_stdglue);
 | 
						|
    bool consumed = false;
 | 
						|
    osKernelState_t state = osKernelGetState();
 | 
						|
    osThreadId_t thread_id = osThreadGetId();
 | 
						|
    if(state == osKernelRunning && thread_id &&
 | 
						|
       osMutexAcquire(furi_stdglue->mutex, osWaitForever) == osOK) {
 | 
						|
        // We are in the thread context
 | 
						|
        // Handle global callbacks
 | 
						|
        FuriStdglueCallbackDict_it_t it;
 | 
						|
        for(FuriStdglueCallbackDict_it(it, furi_stdglue->global_outputs);
 | 
						|
            !FuriStdglueCallbackDict_end_p(it);
 | 
						|
            FuriStdglueCallbackDict_next(it)) {
 | 
						|
            osThreadId_t it_thread = (osThreadId_t)FuriStdglueCallbackDict_ref(it)->key;
 | 
						|
            FuriStdglueWriteCallback it_callback = FuriStdglueCallbackDict_ref(it)->value;
 | 
						|
            if(thread_id != it_thread) {
 | 
						|
                it_callback(_cookie, data, size);
 | 
						|
            }
 | 
						|
        }
 | 
						|
        // Handle thread callbacks
 | 
						|
        FuriStdglueWriteCallback* callback_ptr =
 | 
						|
            FuriStdglueCallbackDict_get(furi_stdglue->thread_outputs, (uint32_t)thread_id);
 | 
						|
        if(callback_ptr) {
 | 
						|
            (*callback_ptr)(_cookie, data, size);
 | 
						|
            consumed = true;
 | 
						|
        }
 | 
						|
        furi_check(osMutexRelease(furi_stdglue->mutex) == osOK);
 | 
						|
    }
 | 
						|
    // Flush
 | 
						|
    if(data == 0) {
 | 
						|
        /*
 | 
						|
         * This means that we should flush internal buffers.  Since we
 | 
						|
         * don't we just return.  (Remember, "handle" == -1 means that all
 | 
						|
         * handles should be flushed.)
 | 
						|
         */
 | 
						|
        return 0;
 | 
						|
    }
 | 
						|
    // Debug uart
 | 
						|
    if(!consumed) furi_hal_console_tx((const uint8_t*)data, size);
 | 
						|
    // All data consumed
 | 
						|
    return size;
 | 
						|
}
 | 
						|
 | 
						|
void furi_stdglue_init() {
 | 
						|
    furi_stdglue = malloc(sizeof(FuriStdglue));
 | 
						|
    // Init outputs structures
 | 
						|
    furi_stdglue->mutex = osMutexNew(NULL);
 | 
						|
    furi_check(furi_stdglue->mutex);
 | 
						|
    FuriStdglueCallbackDict_init(furi_stdglue->global_outputs);
 | 
						|
    FuriStdglueCallbackDict_init(furi_stdglue->thread_outputs);
 | 
						|
    // Prepare and set stdout descriptor
 | 
						|
    FILE* fp = fopencookie(
 | 
						|
        NULL,
 | 
						|
        "w",
 | 
						|
        (cookie_io_functions_t){
 | 
						|
            .read = NULL,
 | 
						|
            .write = stdout_write,
 | 
						|
            .seek = NULL,
 | 
						|
            .close = NULL,
 | 
						|
        });
 | 
						|
    setvbuf(fp, NULL, _IOLBF, 0);
 | 
						|
    stdout = fp;
 | 
						|
}
 | 
						|
 | 
						|
bool furi_stdglue_set_global_stdout_callback(FuriStdglueWriteCallback callback) {
 | 
						|
    furi_assert(furi_stdglue);
 | 
						|
    osThreadId_t thread_id = osThreadGetId();
 | 
						|
    if(thread_id) {
 | 
						|
        furi_check(osMutexAcquire(furi_stdglue->mutex, osWaitForever) == osOK);
 | 
						|
        if(callback) {
 | 
						|
            FuriStdglueCallbackDict_set_at(
 | 
						|
                furi_stdglue->global_outputs, (uint32_t)thread_id, callback);
 | 
						|
        } else {
 | 
						|
            FuriStdglueCallbackDict_erase(furi_stdglue->global_outputs, (uint32_t)thread_id);
 | 
						|
        }
 | 
						|
        furi_check(osMutexRelease(furi_stdglue->mutex) == osOK);
 | 
						|
        return true;
 | 
						|
    } else {
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
bool furi_stdglue_set_thread_stdout_callback(FuriStdglueWriteCallback callback) {
 | 
						|
    furi_assert(furi_stdglue);
 | 
						|
    osThreadId_t thread_id = osThreadGetId();
 | 
						|
    if(thread_id) {
 | 
						|
        furi_check(osMutexAcquire(furi_stdglue->mutex, osWaitForever) == osOK);
 | 
						|
        if(callback) {
 | 
						|
            FuriStdglueCallbackDict_set_at(
 | 
						|
                furi_stdglue->thread_outputs, (uint32_t)thread_id, callback);
 | 
						|
        } else {
 | 
						|
            FuriStdglueCallbackDict_erase(furi_stdglue->thread_outputs, (uint32_t)thread_id);
 | 
						|
        }
 | 
						|
        furi_check(osMutexRelease(furi_stdglue->mutex) == osOK);
 | 
						|
        return true;
 | 
						|
    } else {
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
void __malloc_lock(struct _reent* REENT) {
 | 
						|
    vTaskSuspendAll();
 | 
						|
}
 | 
						|
 | 
						|
void __malloc_unlock(struct _reent* REENT) {
 | 
						|
    xTaskResumeAll();
 | 
						|
}
 |