2021-10-25 14:37:14 +00:00
|
|
|
#include "subghz_file_encoder_worker.h"
|
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
#include <toolbox/stream/stream.h>
|
|
|
|
#include <flipper_format/flipper_format.h>
|
|
|
|
#include <flipper_format/flipper_format_i.h>
|
2023-06-18 17:25:40 +00:00
|
|
|
#include <lib/subghz/devices/devices.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;
|
2022-10-07 12:27:11 +00:00
|
|
|
FuriStreamBuffer* stream;
|
2021-11-11 12:49:19 +00:00
|
|
|
|
|
|
|
Storage* storage;
|
2022-02-18 19:53:46 +00:00
|
|
|
FlipperFormat* flipper_format;
|
2021-10-25 14:37:14 +00:00
|
|
|
|
|
|
|
volatile bool worker_running;
|
2023-02-20 15:55:53 +00:00
|
|
|
volatile bool worker_stopping;
|
2022-11-30 11:41:23 +00:00
|
|
|
bool is_storage_slow;
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* str_data;
|
|
|
|
FuriString* file_path;
|
2023-06-18 17:25:40 +00:00
|
|
|
const SubGhzDevice* device;
|
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;
|
|
|
|
}
|
|
|
|
|
2022-05-08 17:50:20 +00:00
|
|
|
void subghz_file_encoder_worker_add_level_duration(
|
2021-10-25 14:37:14 +00:00
|
|
|
SubGhzFileEncoderWorker* instance,
|
2021-11-11 12:49:19 +00:00
|
|
|
int32_t duration) {
|
2024-01-11 14:33:29 +00:00
|
|
|
size_t ret = furi_stream_buffer_send(instance->stream, &duration, sizeof(int32_t), 100);
|
|
|
|
if(sizeof(int32_t) != ret) FURI_LOG_E(TAG, "Invalid add duration in the stream");
|
2021-10-25 14:37:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 14:05:36 +00:00
|
|
|
bool subghz_file_encoder_worker_data_parse(SubGhzFileEncoderWorker* instance, const char* strStart) {
|
2021-10-25 14:37:14 +00:00
|
|
|
char* str1;
|
2023-04-14 20:37:20 +00:00
|
|
|
int32_t temp_ds = 0;
|
2021-10-25 14:37:14 +00:00
|
|
|
bool res = false;
|
2022-05-10 14:05:36 +00:00
|
|
|
// Line sample: "RAW_Data: -1, 2, -2..."
|
|
|
|
|
|
|
|
// Look for a key in the line
|
|
|
|
str1 = strstr(strStart, "RAW_Data: ");
|
2021-10-25 14:37:14 +00:00
|
|
|
|
|
|
|
if(str1 != NULL) {
|
2022-05-10 14:05:36 +00:00
|
|
|
// Skip key
|
|
|
|
str1 = strchr(str1, ' ');
|
|
|
|
|
|
|
|
// Check that there is still an element in the line
|
|
|
|
while(strchr(str1, ' ') != NULL) {
|
2021-11-11 12:49:19 +00:00
|
|
|
str1 = strchr(str1, ' ');
|
2022-05-10 14:05:36 +00:00
|
|
|
|
|
|
|
// Skip space
|
|
|
|
str1 += 1;
|
2023-04-14 20:37:20 +00:00
|
|
|
//
|
|
|
|
temp_ds = atoi(str1);
|
|
|
|
if((temp_ds < -1000000) || (temp_ds > 1000000)) {
|
2023-04-19 22:24:04 +00:00
|
|
|
if(temp_ds > 0) {
|
|
|
|
subghz_file_encoder_worker_add_level_duration(instance, (int32_t)100);
|
|
|
|
} else {
|
|
|
|
subghz_file_encoder_worker_add_level_duration(instance, (int32_t)-100);
|
|
|
|
}
|
2023-04-14 20:37:20 +00:00
|
|
|
//FURI_LOG_I("PARSE", "Number overflow - %d", atoi(str1));
|
|
|
|
} else {
|
|
|
|
subghz_file_encoder_worker_add_level_duration(instance, temp_ds);
|
|
|
|
}
|
2021-10-25 14:37:14 +00:00
|
|
|
}
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-08-28 01:46:59 +00:00
|
|
|
void subghz_file_encoder_worker_get_text_progress(
|
|
|
|
SubGhzFileEncoderWorker* instance,
|
2022-10-05 18:27:13 +00:00
|
|
|
FuriString* output) {
|
2022-08-28 01:46:59 +00:00
|
|
|
UNUSED(output);
|
|
|
|
Stream* stream = flipper_format_get_raw_stream(instance->flipper_format);
|
|
|
|
size_t total_size = stream_size(stream);
|
|
|
|
size_t current_offset = stream_tell(stream);
|
2022-10-07 15:32:58 +00:00
|
|
|
size_t buffer_avail = furi_stream_buffer_bytes_available(instance->stream);
|
2022-08-28 01:46:59 +00:00
|
|
|
|
2022-10-05 18:27:13 +00:00
|
|
|
furi_string_printf(output, "%03u%%", 100 * (current_offset - buffer_avail) / total_size);
|
2022-08-28 01:46:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 14:37:14 +00:00
|
|
|
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;
|
2022-10-07 12:27:11 +00:00
|
|
|
int ret = furi_stream_buffer_receive(instance->stream, &duration, sizeof(int32_t), 0);
|
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) {
|
2022-11-30 11:41:23 +00:00
|
|
|
level_duration = level_duration_make(false, -duration);
|
2021-10-25 14:37:14 +00:00
|
|
|
} else if(duration > 0) {
|
|
|
|
level_duration = level_duration_make(true, duration);
|
2022-12-26 12:13:30 +00:00
|
|
|
} else if(duration == 0) { //-V547
|
2021-10-25 14:37:14 +00:00
|
|
|
level_duration = level_duration_reset();
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Stop transmission");
|
2023-02-20 15:55:53 +00:00
|
|
|
instance->worker_stopping = true;
|
2021-10-25 14:37:14 +00:00
|
|
|
}
|
|
|
|
return level_duration;
|
|
|
|
} else {
|
2022-11-30 11:41:23 +00:00
|
|
|
instance->is_storage_slow = true;
|
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;
|
2022-11-30 11:41:23 +00:00
|
|
|
instance->is_storage_slow = false;
|
2022-02-18 19:53:46 +00:00
|
|
|
Stream* stream = flipper_format_get_raw_stream(instance->flipper_format);
|
2021-10-25 14:37:14 +00:00
|
|
|
do {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_file_open_existing(
|
2022-10-05 15:15:23 +00:00
|
|
|
instance->flipper_format, furi_string_get_cstr(instance->file_path))) {
|
2021-11-11 12:49:19 +00:00
|
|
|
FURI_LOG_E(
|
2022-10-05 15:15:23 +00:00
|
|
|
TAG,
|
|
|
|
"Unable to open file for read: %s",
|
|
|
|
furi_string_get_cstr(instance->file_path));
|
2021-10-25 14:37:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
if(!flipper_format_read_string(instance->flipper_format, "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"
|
2022-02-18 19:53:46 +00:00
|
|
|
stream_seek(stream, 1, StreamOffsetFromCurrent);
|
2021-10-25 14:37:14 +00:00
|
|
|
res = true;
|
2023-02-20 15:55:53 +00:00
|
|
|
instance->worker_stopping = 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) {
|
2022-10-07 12:27:11 +00:00
|
|
|
size_t stream_free_byte = furi_stream_buffer_spaces_available(instance->stream);
|
2021-11-11 12:49:19 +00:00
|
|
|
if((stream_free_byte / sizeof(int32_t)) >= SUBGHZ_FILE_ENCODER_LOAD) {
|
2022-02-18 19:53:46 +00:00
|
|
|
if(stream_read_line(stream, instance->str_data)) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_trim(instance->str_data);
|
2021-10-25 14:37:14 +00:00
|
|
|
if(!subghz_file_encoder_worker_data_parse(
|
2022-10-05 15:15:23 +00:00
|
|
|
instance, furi_string_get_cstr(instance->str_data))) {
|
2022-05-08 17:50:20 +00:00
|
|
|
subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
|
2021-10-25 14:37:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2022-05-08 17:50:20 +00:00
|
|
|
subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
|
2021-10-25 14:37:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-11-30 11:41:23 +00:00
|
|
|
} else {
|
|
|
|
furi_delay_ms(1);
|
2021-10-25 14:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//waiting for the end of the transfer
|
2022-11-30 11:41:23 +00:00
|
|
|
if(instance->is_storage_slow) {
|
|
|
|
FURI_LOG_E(TAG, "Storage is slow");
|
|
|
|
}
|
2023-06-18 17:25:40 +00:00
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "End read file");
|
2023-06-18 17:25:40 +00:00
|
|
|
while(instance->device && !subghz_devices_is_async_complete_tx(instance->device) &&
|
|
|
|
instance->worker_running) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_ms(5);
|
2022-04-22 11:33:43 +00:00
|
|
|
}
|
2023-06-18 17:25:40 +00:00
|
|
|
|
2022-04-22 11:33:43 +00:00
|
|
|
FURI_LOG_I(TAG, "End transmission");
|
2021-10-25 14:37:14 +00:00
|
|
|
while(instance->worker_running) {
|
2023-02-20 15:55:53 +00:00
|
|
|
if(instance->worker_stopping) {
|
2021-11-24 13:59:45 +00:00
|
|
|
if(instance->callback_end) instance->callback_end(instance->context_end);
|
|
|
|
}
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_ms(50);
|
2021-10-25 14:37:14 +00:00
|
|
|
}
|
2022-02-18 19:53:46 +00:00
|
|
|
flipper_format_file_close(instance->flipper_format);
|
2021-11-11 12:49:19 +00:00
|
|
|
|
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() {
|
2022-02-18 19:53:46 +00:00
|
|
|
SubGhzFileEncoderWorker* instance = malloc(sizeof(SubGhzFileEncoderWorker));
|
2021-10-25 14:37:14 +00:00
|
|
|
|
2022-11-23 12:49:17 +00:00
|
|
|
instance->thread =
|
|
|
|
furi_thread_alloc_ex("SubGhzFEWorker", 2048, subghz_file_encoder_worker_thread, instance);
|
2022-10-07 12:27:11 +00:00
|
|
|
instance->stream = furi_stream_buffer_alloc(sizeof(int32_t) * 2048, sizeof(int32_t));
|
2021-11-11 12:49:19 +00:00
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
instance->storage = furi_record_open(RECORD_STORAGE);
|
2022-02-18 19:53:46 +00:00
|
|
|
instance->flipper_format = flipper_format_file_alloc(instance->storage);
|
2021-10-25 14:37:14 +00:00
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
instance->str_data = furi_string_alloc();
|
|
|
|
instance->file_path = furi_string_alloc();
|
2023-02-20 15:55:53 +00:00
|
|
|
instance->worker_stopping = true;
|
2021-10-25 14:37:14 +00:00
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subghz_file_encoder_worker_free(SubGhzFileEncoderWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
2022-10-07 12:27:11 +00:00
|
|
|
furi_stream_buffer_free(instance->stream);
|
2021-10-25 14:37:14 +00:00
|
|
|
furi_thread_free(instance->thread);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(instance->str_data);
|
|
|
|
furi_string_free(instance->file_path);
|
2021-11-11 12:49:19 +00:00
|
|
|
|
2022-02-18 19:53:46 +00:00
|
|
|
flipper_format_free(instance->flipper_format);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2021-10-25 14:37:14 +00:00
|
|
|
|
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
2023-06-18 17:25:40 +00:00
|
|
|
bool subghz_file_encoder_worker_start(
|
|
|
|
SubGhzFileEncoderWorker* instance,
|
|
|
|
const char* file_path,
|
|
|
|
const char* radio_device_name) {
|
2021-10-25 14:37:14 +00:00
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(!instance->worker_running);
|
|
|
|
|
2022-10-07 12:27:11 +00:00
|
|
|
furi_stream_buffer_reset(instance->stream);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(instance->file_path, file_path);
|
2023-08-08 08:03:39 +00:00
|
|
|
if(radio_device_name) {
|
|
|
|
instance->device = subghz_devices_get_by_name(radio_device_name);
|
|
|
|
}
|
2021-10-25 14:37:14 +00:00
|
|
|
instance->worker_running = true;
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_start(instance->thread);
|
|
|
|
|
|
|
|
return true;
|
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;
|
|
|
|
}
|