unleashed-firmware/lib/fl_subghz/subghz_worker.c
あく e8211226f3
[FL-1496] SubGhz: Library, Cli, Application (#543)
* ApiHal: set frequency and path in one go. Drivers: proper frequency registers calculation for CC1101. Update subghz cli to match new api.
* SubGhz: preparation for parsers porting, tim2 sharing
* ApiHal: add interrupts API, move all TIM2 related things there.
* SubGhz: refactor protocol lib and add keeloq.
* SubGhz: proper init_set for keeloq manafacture key
* SubGhz: port more protocols to lib
* SubGhz: load keeloq manufacture keys from sd card (if any).
* SubGhz: format output from protocols.
* SubGhz: use default frequency in subghz_rx cli command.
* SubGhz: keeloq key types
* Fix compillation error when internal storage disabled
* SubGhz: minor cleanup
* SubGhz: properly handle timeout and reset signal in subghz_rx
* SubGhz: Worker, Capture View. Furi: emulate thread join.
* SubGhz: free strings on keeloq key load end
* SubGhz: update protocols reporting API, app refactoring and capture view, update API HAL usage.
* SubGhz: update dump formatting
* ApiHal: backport subghz preset to F5
* ApiHal: backport subghz frequency range to F5
2021-06-30 00:19:20 +03:00

111 lines
3.1 KiB
C

#include "subghz_worker.h"
#include <stream_buffer.h>
#include <furi.h>
struct SubGhzWorker {
FuriThread* thread;
StreamBufferHandle_t stream;
volatile bool running;
volatile bool overrun;
SubGhzWorkerOverrunCallback overrun_callback;
SubGhzWorkerPairCallback pair_callback;
void* context;
};
void subghz_worker_rx_callback(
ApiHalSubGhzCaptureLevel level,
uint32_t duration,
void* context) {
SubGhzWorker* instance = context;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
LevelPair pair = {.level = level, .duration = duration};
if(instance->overrun) {
instance->overrun = false;
pair.level = ApiHalSubGhzCaptureLevelOverrun;
}
size_t ret =
xStreamBufferSendFromISR(instance->stream, &pair, sizeof(LevelPair), &xHigherPriorityTaskWoken);
if(sizeof(LevelPair) != ret) instance->overrun = true;
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
static int32_t subghz_worker_thread_callback(void* context) {
SubGhzWorker* instance = context;
LevelPair pair;
while(instance->running) {
int ret = xStreamBufferReceive(instance->stream, &pair, sizeof(LevelPair), 10);
if(ret == sizeof(LevelPair)) {
if(pair.level == ApiHalSubGhzCaptureLevelOverrun) {
printf(".");
if (instance->overrun_callback) instance->overrun_callback(instance->context);
} else {
if (instance->pair_callback) instance->pair_callback(instance->context, pair);
}
}
}
return 0;
}
SubGhzWorker* subghz_worker_alloc() {
SubGhzWorker* instance = furi_alloc(sizeof(SubGhzWorker));
instance->thread = furi_thread_alloc();
furi_thread_set_name(instance->thread, "subghz_worker");
furi_thread_set_stack_size(instance->thread, 2048);
furi_thread_set_context(instance->thread, instance);
furi_thread_set_callback(instance->thread, subghz_worker_thread_callback);
instance->stream = xStreamBufferCreate(sizeof(LevelPair) * 1024, sizeof(LevelPair));
return instance;
}
void subghz_worker_free(SubGhzWorker* instance) {
furi_assert(instance);
vStreamBufferDelete(instance->stream);
furi_thread_free(instance->thread);
free(instance);
}
void subghz_worker_set_overrun_callback(SubGhzWorker* instance, SubGhzWorkerOverrunCallback callback) {
furi_assert(instance);
instance->overrun_callback = callback;
}
void subghz_worker_set_pair_callback(SubGhzWorker* instance, SubGhzWorkerPairCallback callback) {
furi_assert(instance);
instance->pair_callback = callback;
}
void subghz_worker_set_context(SubGhzWorker* instance, void* context) {
furi_assert(instance);
instance->context = context;
}
void subghz_worker_start(SubGhzWorker* instance) {
furi_assert(instance);
furi_assert(!instance->running);
instance->running = true;
furi_thread_start(instance->thread);
}
void subghz_worker_stop(SubGhzWorker* instance) {
furi_assert(instance);
furi_assert(instance->running);
instance->running = false;
furi_thread_join(instance->thread);
}