2021-10-25 14:37:14 +00:00
|
|
|
#include "subghz_file_encoder_worker.h"
|
|
|
|
#include <stream_buffer.h>
|
|
|
|
|
2021-11-11 12:49:19 +00:00
|
|
|
#include <lib/flipper_file/flipper_file.h>
|
|
|
|
#include <lib/flipper_file/file_helper.h>
|
2021-10-25 14:37:14 +00:00
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "SubGhzFileEncoderWorker"
|
|
|
|
|
2021-10-25 14:37:14 +00:00
|
|
|
#define SUBGHZ_FILE_ENCODER_LOAD 512
|
|
|
|
|
|
|
|
struct SubGhzFileEncoderWorker {
|
|
|
|
FuriThread* thread;
|
|
|
|
StreamBufferHandle_t stream;
|
2021-11-11 12:49:19 +00:00
|
|
|
|
|
|
|
Storage* storage;
|
|
|
|
FlipperFile* flipper_file;
|
2021-10-25 14:37:14 +00:00
|
|
|
|
|
|
|
volatile bool worker_running;
|
2021-11-24 13:59:45 +00:00
|
|
|
volatile bool worker_stoping;
|
2021-10-25 14:37:14 +00:00
|
|
|
bool level;
|
2021-11-11 12:49:19 +00:00
|
|
|
int32_t duration;
|
2021-10-25 14:37:14 +00:00
|
|
|
string_t str_data;
|
|
|
|
string_t file_path;
|
2021-11-24 13:59:45 +00:00
|
|
|
|
|
|
|
SubGhzFileEncoderWorkerCallbackEnd callback_end;
|
|
|
|
void* context_end;
|
2021-10-25 14:37:14 +00:00
|
|
|
};
|
|
|
|
|
2021-11-24 13:59:45 +00:00
|
|
|
void subghz_file_encoder_worker_callback_end(
|
|
|
|
SubGhzFileEncoderWorker* instance,
|
|
|
|
SubGhzFileEncoderWorkerCallbackEnd callback_end,
|
|
|
|
void* context_end) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(callback_end);
|
|
|
|
instance->callback_end = callback_end;
|
|
|
|
instance->context_end = context_end;
|
|
|
|
}
|
|
|
|
|
2021-10-25 14:37:14 +00:00
|
|
|
void subghz_file_encoder_worker_add_livel_duration(
|
|
|
|
SubGhzFileEncoderWorker* instance,
|
2021-11-11 12:49:19 +00:00
|
|
|
int32_t duration) {
|
2021-10-25 14:37:14 +00:00
|
|
|
bool res = true;
|
|
|
|
if(duration < 0 && !instance->level) {
|
|
|
|
instance->duration += duration;
|
|
|
|
res = false;
|
|
|
|
} else if(duration > 0 && instance->level) {
|
|
|
|
instance->duration += duration;
|
|
|
|
res = false;
|
|
|
|
} else if(duration == 0) {
|
|
|
|
instance->duration = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(res) {
|
|
|
|
instance->level = !instance->level;
|
|
|
|
instance->duration += duration;
|
2021-11-11 12:49:19 +00:00
|
|
|
xStreamBufferSend(instance->stream, &instance->duration, sizeof(int32_t), 10);
|
2021-10-25 14:37:14 +00:00
|
|
|
instance->duration = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_file_encoder_worker_data_parse(
|
|
|
|
SubGhzFileEncoderWorker* instance,
|
|
|
|
const char* strStart,
|
|
|
|
size_t len) {
|
|
|
|
char* str1;
|
|
|
|
size_t ind_start = (size_t)strStart; //store the start address of the beginning of the line
|
|
|
|
bool res = false;
|
|
|
|
|
|
|
|
str1 = strstr(
|
|
|
|
strStart, "RAW_Data: "); //looking for the beginning of the desired title in the line
|
|
|
|
if(str1 != NULL) {
|
|
|
|
str1 = strchr(
|
|
|
|
str1,
|
|
|
|
' '); //if found, shift the pointer by 1 element per line "RAW_Data: -1, 2, -2..."
|
|
|
|
while(
|
2021-11-11 12:49:19 +00:00
|
|
|
strchr(str1, ' ') != NULL &&
|
2021-10-25 14:37:14 +00:00
|
|
|
((size_t)str1 <
|
|
|
|
(len +
|
|
|
|
ind_start))) { //check that there is still an element in the line and that it has not gone beyond the line
|
2021-11-11 12:49:19 +00:00
|
|
|
str1 = strchr(str1, ' ');
|
|
|
|
str1 += 1; //if found, shift the pointer by next element per line
|
2021-10-25 14:37:14 +00:00
|
|
|
subghz_file_encoder_worker_add_livel_duration(instance, atoi(str1));
|
|
|
|
}
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
LevelDuration subghz_file_encoder_worker_get_level_duration(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
SubGhzFileEncoderWorker* instance = context;
|
2021-11-11 12:49:19 +00:00
|
|
|
int32_t duration;
|
2021-10-25 14:37:14 +00:00
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
int ret = xStreamBufferReceiveFromISR(
|
2021-11-11 12:49:19 +00:00
|
|
|
instance->stream, &duration, sizeof(int32_t), &xHigherPriorityTaskWoken);
|
2021-10-25 14:37:14 +00:00
|
|
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
2021-11-11 12:49:19 +00:00
|
|
|
if(ret == sizeof(int32_t)) {
|
2021-10-25 14:37:14 +00:00
|
|
|
LevelDuration level_duration = {.level = LEVEL_DURATION_RESET};
|
|
|
|
if(duration < 0) {
|
|
|
|
level_duration = level_duration_make(false, duration * -1);
|
|
|
|
} else if(duration > 0) {
|
|
|
|
level_duration = level_duration_make(true, duration);
|
|
|
|
} else if(duration == 0) {
|
|
|
|
level_duration = level_duration_reset();
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Stop transmission");
|
2021-11-24 13:59:45 +00:00
|
|
|
instance->worker_stoping = true;
|
2021-10-25 14:37:14 +00:00
|
|
|
}
|
|
|
|
return level_duration;
|
|
|
|
} else {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Slow flash read");
|
2021-10-25 14:37:14 +00:00
|
|
|
return level_duration_wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Worker thread
|
|
|
|
*
|
|
|
|
* @param context
|
|
|
|
* @return exit code
|
|
|
|
*/
|
|
|
|
static int32_t subghz_file_encoder_worker_thread(void* context) {
|
|
|
|
SubGhzFileEncoderWorker* instance = context;
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Worker start");
|
2021-10-25 14:37:14 +00:00
|
|
|
bool res = false;
|
2021-11-11 12:49:19 +00:00
|
|
|
File* file = flipper_file_get_file(instance->flipper_file);
|
2021-10-25 14:37:14 +00:00
|
|
|
do {
|
2021-11-11 12:49:19 +00:00
|
|
|
if(!flipper_file_open_existing(
|
|
|
|
instance->flipper_file, string_get_cstr(instance->file_path))) {
|
|
|
|
FURI_LOG_E(
|
2021-11-24 13:59:45 +00:00
|
|
|
TAG, "Unable to open file for read: %s", string_get_cstr(instance->file_path));
|
2021-10-25 14:37:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-11-11 12:49:19 +00:00
|
|
|
if(!flipper_file_read_string(instance->flipper_file, "Protocol", instance->str_data)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Missing Protocol");
|
2021-10-25 14:37:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-11-11 12:49:19 +00:00
|
|
|
|
|
|
|
//skip the end of the previous line "\n"
|
|
|
|
storage_file_seek(file, 1, false);
|
2021-10-25 14:37:14 +00:00
|
|
|
res = true;
|
2021-11-24 13:59:45 +00:00
|
|
|
instance->worker_stoping = false;
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Start transmission");
|
2021-10-25 14:37:14 +00:00
|
|
|
} while(0);
|
|
|
|
|
|
|
|
while(res && instance->worker_running) {
|
|
|
|
size_t stream_free_byte = xStreamBufferSpacesAvailable(instance->stream);
|
2021-11-11 12:49:19 +00:00
|
|
|
if((stream_free_byte / sizeof(int32_t)) >= SUBGHZ_FILE_ENCODER_LOAD) {
|
|
|
|
if(file_helper_read_line(file, instance->str_data)) {
|
|
|
|
//skip the end of the previous line "\n"
|
|
|
|
storage_file_seek(file, 1, false);
|
2021-10-25 14:37:14 +00:00
|
|
|
if(!subghz_file_encoder_worker_data_parse(
|
|
|
|
instance,
|
|
|
|
string_get_cstr(instance->str_data),
|
|
|
|
strlen(string_get_cstr(instance->str_data)))) {
|
|
|
|
//to stop DMA correctly
|
|
|
|
subghz_file_encoder_worker_add_livel_duration(instance, LEVEL_DURATION_RESET);
|
|
|
|
subghz_file_encoder_worker_add_livel_duration(instance, LEVEL_DURATION_RESET);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
subghz_file_encoder_worker_add_livel_duration(instance, LEVEL_DURATION_RESET);
|
|
|
|
subghz_file_encoder_worker_add_livel_duration(instance, LEVEL_DURATION_RESET);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 14:01:03 +00:00
|
|
|
osDelay(5);
|
2021-10-25 14:37:14 +00:00
|
|
|
}
|
|
|
|
//waiting for the end of the transfer
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "End read file");
|
2021-11-24 13:59:45 +00:00
|
|
|
|
2021-10-25 14:37:14 +00:00
|
|
|
while(instance->worker_running) {
|
2021-11-24 13:59:45 +00:00
|
|
|
if(instance->worker_stoping) {
|
|
|
|
if(instance->callback_end) instance->callback_end(instance->context_end);
|
|
|
|
}
|
2021-10-25 14:37:14 +00:00
|
|
|
osDelay(50);
|
|
|
|
}
|
2021-11-11 12:49:19 +00:00
|
|
|
flipper_file_close(instance->flipper_file);
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Worker stop");
|
2021-10-25 14:37:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubGhzFileEncoderWorker* subghz_file_encoder_worker_alloc() {
|
|
|
|
SubGhzFileEncoderWorker* instance = furi_alloc(sizeof(SubGhzFileEncoderWorker));
|
|
|
|
|
|
|
|
instance->thread = furi_thread_alloc();
|
2021-11-11 16:17:50 +00:00
|
|
|
furi_thread_set_name(instance->thread, "SubghzFEWorker");
|
2021-10-25 14:37:14 +00:00
|
|
|
furi_thread_set_stack_size(instance->thread, 2048);
|
|
|
|
furi_thread_set_context(instance->thread, instance);
|
|
|
|
furi_thread_set_callback(instance->thread, subghz_file_encoder_worker_thread);
|
2021-11-11 12:49:19 +00:00
|
|
|
instance->stream = xStreamBufferCreate(sizeof(int32_t) * 2048, sizeof(int32_t));
|
|
|
|
|
|
|
|
instance->storage = furi_record_open("storage");
|
|
|
|
instance->flipper_file = flipper_file_alloc(instance->storage);
|
2021-10-25 14:37:14 +00:00
|
|
|
|
|
|
|
string_init(instance->str_data);
|
|
|
|
string_init(instance->file_path);
|
|
|
|
instance->level = false;
|
2021-11-24 13:59:45 +00:00
|
|
|
instance->worker_stoping = true;
|
2021-10-25 14:37:14 +00:00
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_file_encoder_worker_free(SubGhzFileEncoderWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
vStreamBufferDelete(instance->stream);
|
|
|
|
furi_thread_free(instance->thread);
|
|
|
|
|
|
|
|
string_clear(instance->str_data);
|
|
|
|
string_clear(instance->file_path);
|
2021-11-11 12:49:19 +00:00
|
|
|
|
|
|
|
flipper_file_free(instance->flipper_file);
|
|
|
|
furi_record_close("storage");
|
2021-10-25 14:37:14 +00:00
|
|
|
|
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_file_encoder_worker_start(SubGhzFileEncoderWorker* instance, const char* file_path) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(!instance->worker_running);
|
|
|
|
|
|
|
|
xStreamBufferReset(instance->stream);
|
|
|
|
string_set(instance->file_path, file_path);
|
|
|
|
instance->worker_running = true;
|
2021-10-27 17:37:11 +00:00
|
|
|
bool res = furi_thread_start(instance->thread);
|
|
|
|
return res;
|
2021-10-25 14:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_file_encoder_worker_stop(SubGhzFileEncoderWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(instance->worker_running);
|
|
|
|
|
|
|
|
instance->worker_running = false;
|
|
|
|
furi_thread_join(instance->thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_file_encoder_worker_is_running(SubGhzFileEncoderWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
return instance->worker_running;
|
|
|
|
}
|