2022-10-07 12:27:11 +00:00
|
|
|
#include "stream_buffer.h"
|
2024-06-10 17:53:08 +00:00
|
|
|
|
|
|
|
#include "check.h"
|
2022-10-07 12:27:11 +00:00
|
|
|
#include "common_defines.h"
|
2023-11-01 07:24:11 +00:00
|
|
|
|
2022-10-07 12:27:11 +00:00
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <FreeRTOS-Kernel/include/stream_buffer.h>
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
struct FuriStreamBuffer {
|
|
|
|
StaticStreamBuffer_t container;
|
|
|
|
uint8_t buffer[];
|
|
|
|
};
|
|
|
|
|
|
|
|
// IMPORTANT: container MUST be the FIRST struct member
|
|
|
|
static_assert(offsetof(FuriStreamBuffer, container) == 0);
|
|
|
|
// IMPORTANT: buffer MUST be the LAST struct member
|
|
|
|
static_assert(offsetof(FuriStreamBuffer, buffer) == sizeof(FuriStreamBuffer));
|
|
|
|
|
2022-10-07 12:27:11 +00:00
|
|
|
FuriStreamBuffer* furi_stream_buffer_alloc(size_t size, size_t trigger_level) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(size != 0);
|
2022-12-26 12:13:30 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
// Actual FreeRTOS usable buffer size seems to be one less
|
|
|
|
const size_t buffer_size = size + 1;
|
2022-12-26 12:13:30 +00:00
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
FuriStreamBuffer* stream_buffer = malloc(sizeof(FuriStreamBuffer) + buffer_size);
|
|
|
|
StreamBufferHandle_t hStreamBuffer = xStreamBufferCreateStatic(
|
|
|
|
buffer_size, trigger_level, stream_buffer->buffer, &stream_buffer->container);
|
|
|
|
|
|
|
|
furi_check(hStreamBuffer == (StreamBufferHandle_t)stream_buffer);
|
|
|
|
|
|
|
|
return stream_buffer;
|
2022-10-07 12:27:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void furi_stream_buffer_free(FuriStreamBuffer* stream_buffer) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(stream_buffer);
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
vStreamBufferDelete((StreamBufferHandle_t)stream_buffer);
|
|
|
|
free(stream_buffer);
|
2022-10-07 12:27:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool furi_stream_set_trigger_level(FuriStreamBuffer* stream_buffer, size_t trigger_level) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(stream_buffer);
|
2024-06-05 17:04:03 +00:00
|
|
|
return xStreamBufferSetTriggerLevel((StreamBufferHandle_t)stream_buffer, trigger_level) ==
|
|
|
|
pdTRUE;
|
2022-10-07 12:27:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t furi_stream_buffer_send(
|
|
|
|
FuriStreamBuffer* stream_buffer,
|
|
|
|
const void* data,
|
|
|
|
size_t length,
|
|
|
|
uint32_t timeout) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(stream_buffer);
|
|
|
|
|
2022-10-07 12:27:11 +00:00
|
|
|
size_t ret;
|
|
|
|
|
2022-11-05 15:07:24 +00:00
|
|
|
if(FURI_IS_IRQ_MODE()) {
|
2022-10-07 12:27:11 +00:00
|
|
|
BaseType_t yield;
|
2024-06-05 17:04:03 +00:00
|
|
|
ret = xStreamBufferSendFromISR((StreamBufferHandle_t)stream_buffer, data, length, &yield);
|
2022-10-07 12:27:11 +00:00
|
|
|
portYIELD_FROM_ISR(yield);
|
|
|
|
} else {
|
2024-06-05 17:04:03 +00:00
|
|
|
ret = xStreamBufferSend((StreamBufferHandle_t)stream_buffer, data, length, timeout);
|
2022-10-07 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t furi_stream_buffer_receive(
|
|
|
|
FuriStreamBuffer* stream_buffer,
|
|
|
|
void* data,
|
|
|
|
size_t length,
|
|
|
|
uint32_t timeout) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(stream_buffer);
|
|
|
|
|
2022-10-07 12:27:11 +00:00
|
|
|
size_t ret;
|
|
|
|
|
2022-11-05 15:07:24 +00:00
|
|
|
if(FURI_IS_IRQ_MODE()) {
|
2022-10-07 12:27:11 +00:00
|
|
|
BaseType_t yield;
|
2024-06-05 17:04:03 +00:00
|
|
|
ret =
|
|
|
|
xStreamBufferReceiveFromISR((StreamBufferHandle_t)stream_buffer, data, length, &yield);
|
2022-10-07 12:27:11 +00:00
|
|
|
portYIELD_FROM_ISR(yield);
|
|
|
|
} else {
|
2024-06-05 17:04:03 +00:00
|
|
|
ret = xStreamBufferReceive((StreamBufferHandle_t)stream_buffer, data, length, timeout);
|
2022-10-07 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t furi_stream_buffer_bytes_available(FuriStreamBuffer* stream_buffer) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(stream_buffer);
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
return xStreamBufferBytesAvailable((StreamBufferHandle_t)stream_buffer);
|
2022-10-07 12:27:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t furi_stream_buffer_spaces_available(FuriStreamBuffer* stream_buffer) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(stream_buffer);
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
return xStreamBufferSpacesAvailable((StreamBufferHandle_t)stream_buffer);
|
2022-10-07 12:27:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool furi_stream_buffer_is_full(FuriStreamBuffer* stream_buffer) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(stream_buffer);
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
return xStreamBufferIsFull((StreamBufferHandle_t)stream_buffer) == pdTRUE;
|
2022-10-07 12:27:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool furi_stream_buffer_is_empty(FuriStreamBuffer* stream_buffer) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(stream_buffer);
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
return (xStreamBufferIsEmpty((StreamBufferHandle_t)stream_buffer) == pdTRUE);
|
2022-10-07 12:27:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FuriStatus furi_stream_buffer_reset(FuriStreamBuffer* stream_buffer) {
|
2024-03-19 14:43:52 +00:00
|
|
|
furi_check(stream_buffer);
|
|
|
|
|
2024-06-05 17:04:03 +00:00
|
|
|
if(xStreamBufferReset((StreamBufferHandle_t)stream_buffer) == pdPASS) {
|
2022-10-07 12:27:11 +00:00
|
|
|
return FuriStatusOk;
|
|
|
|
} else {
|
|
|
|
return FuriStatusError;
|
|
|
|
}
|
2024-06-05 17:04:03 +00:00
|
|
|
}
|