2022-09-24 17:47:21 +00:00
|
|
|
#include "subbrute_worker.h"
|
|
|
|
|
2022-09-25 17:31:33 +00:00
|
|
|
#include <subghz/environment.h>
|
|
|
|
#include <subghz/transmitter.h>
|
|
|
|
#include <flipper_format_i.h>
|
2022-09-26 16:14:33 +00:00
|
|
|
#include <lib/subghz/subghz_tx_rx_worker.h>
|
2022-09-25 17:31:33 +00:00
|
|
|
|
2022-09-24 17:47:21 +00:00
|
|
|
#define TAG "SubBruteWorker"
|
|
|
|
|
|
|
|
struct SubBruteWorker {
|
2022-09-26 16:14:33 +00:00
|
|
|
SubGhzTxRxWorker* subghz_txrx;
|
2022-09-24 17:47:21 +00:00
|
|
|
volatile bool worker_running;
|
2022-09-25 20:07:14 +00:00
|
|
|
volatile bool worker_manual_mode;
|
2022-09-25 21:07:16 +00:00
|
|
|
bool is_manual_init;
|
2022-09-26 16:14:33 +00:00
|
|
|
bool is_continuous_worker;
|
2022-09-24 17:47:21 +00:00
|
|
|
|
|
|
|
SubGhzEnvironment* environment;
|
|
|
|
SubGhzTransmitter* transmitter;
|
|
|
|
FlipperFormat* flipper_format;
|
|
|
|
|
|
|
|
uint32_t last_time_tx_data;
|
|
|
|
|
|
|
|
// Preset and frequency needed
|
|
|
|
FuriHalSubGhzPreset preset;
|
|
|
|
uint32_t frequency;
|
|
|
|
string_t protocol_name;
|
|
|
|
|
|
|
|
//SubBruteWorkerCallback callback;
|
|
|
|
//void* context;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Taken from subghz_tx_rx_worker.c */
|
|
|
|
#define SUBBRUTE_TXRX_WORKER_BUF_SIZE 2048
|
|
|
|
#define SUBBRUTE_TXRX_WORKER_MAX_TXRX_SIZE 60
|
|
|
|
#define SUBBRUTE_TXRX_WORKER_TIMEOUT_READ_WRITE_BUF 40
|
2022-09-26 16:14:33 +00:00
|
|
|
#define SUBBRUTE_TX_TIMEOUT 5
|
|
|
|
#define SUBBRUTE_SEND_DELAY 20
|
2022-09-24 17:47:21 +00:00
|
|
|
|
|
|
|
SubBruteWorker* subbrute_worker_alloc() {
|
|
|
|
SubBruteWorker* instance = malloc(sizeof(SubBruteWorker));
|
|
|
|
|
|
|
|
//instance->status = SubBruteWorkerStatusIDLE;
|
|
|
|
instance->worker_running = false;
|
2022-09-25 20:07:14 +00:00
|
|
|
instance->worker_manual_mode = false;
|
2022-09-24 17:47:21 +00:00
|
|
|
|
2022-09-26 16:14:33 +00:00
|
|
|
instance->environment = NULL;
|
|
|
|
instance->transmitter = NULL;
|
|
|
|
|
2022-09-24 17:47:21 +00:00
|
|
|
instance->flipper_format = flipper_format_string_alloc();
|
|
|
|
string_init(instance->protocol_name);
|
|
|
|
|
2022-09-26 16:14:33 +00:00
|
|
|
// SubGhzTxRxWorker
|
|
|
|
instance->subghz_txrx = subghz_tx_rx_worker_alloc();
|
|
|
|
|
2022-09-24 17:47:21 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subbrute_worker_free(SubBruteWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(!instance->worker_running);
|
|
|
|
|
2022-09-25 21:07:16 +00:00
|
|
|
if(instance->transmitter != NULL) {
|
|
|
|
subghz_transmitter_free(instance->transmitter);
|
|
|
|
instance->transmitter = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(instance->environment != NULL) {
|
|
|
|
subghz_environment_free(instance->environment);
|
|
|
|
instance->environment = NULL;
|
|
|
|
}
|
|
|
|
|
2022-09-24 17:47:21 +00:00
|
|
|
flipper_format_free(instance->flipper_format);
|
2022-09-25 13:05:52 +00:00
|
|
|
|
2022-09-24 17:47:21 +00:00
|
|
|
string_clear(instance->protocol_name);
|
|
|
|
|
2022-09-26 16:14:33 +00:00
|
|
|
// SubGhzTxRxWorker
|
|
|
|
subghz_tx_rx_worker_free(instance->subghz_txrx);
|
|
|
|
|
2022-09-24 17:47:21 +00:00
|
|
|
free(instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subbrute_worker_start(
|
|
|
|
SubBruteWorker* instance,
|
|
|
|
uint32_t frequency,
|
|
|
|
FuriHalSubGhzPreset preset,
|
2022-09-25 13:05:52 +00:00
|
|
|
const char* protocol_name) {
|
2022-09-24 17:47:21 +00:00
|
|
|
furi_assert(instance);
|
2022-09-25 20:07:14 +00:00
|
|
|
|
2022-09-25 21:07:16 +00:00
|
|
|
if(instance->worker_manual_mode) {
|
2022-09-26 16:14:33 +00:00
|
|
|
FURI_LOG_W(TAG, "Invalid mode for starting worker!");
|
2022-09-25 20:07:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-09-24 17:47:21 +00:00
|
|
|
|
|
|
|
instance->frequency = frequency;
|
|
|
|
instance->preset = preset;
|
2022-09-25 13:05:52 +00:00
|
|
|
|
|
|
|
string_clear(instance->protocol_name);
|
2022-09-25 17:31:33 +00:00
|
|
|
string_init_printf(instance->protocol_name, "%s", protocol_name);
|
2022-09-24 17:47:21 +00:00
|
|
|
|
|
|
|
bool res = false;
|
|
|
|
|
|
|
|
furi_hal_subghz_reset();
|
|
|
|
furi_hal_subghz_idle();
|
|
|
|
furi_hal_subghz_load_preset(instance->preset);
|
|
|
|
|
|
|
|
furi_hal_subghz_set_frequency_and_path(instance->frequency);
|
|
|
|
furi_hal_subghz_flush_rx();
|
|
|
|
|
|
|
|
if(furi_hal_subghz_is_tx_allowed(frequency)) {
|
|
|
|
instance->frequency = frequency;
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
instance->worker_running = res;
|
|
|
|
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
FURI_LOG_I(TAG, "Frequency: %d", frequency);
|
|
|
|
#endif
|
|
|
|
instance->preset = preset;
|
2022-09-26 16:14:33 +00:00
|
|
|
if(res) {
|
|
|
|
instance->worker_running = res =
|
|
|
|
subghz_tx_rx_worker_start(instance->subghz_txrx, frequency);
|
|
|
|
}
|
2022-09-24 17:47:21 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subbrute_worker_stop(SubBruteWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
instance->worker_running = false;
|
|
|
|
|
2022-09-26 16:14:33 +00:00
|
|
|
if(subghz_tx_rx_worker_is_running(instance->subghz_txrx)) {
|
|
|
|
subghz_tx_rx_worker_stop(instance->subghz_txrx);
|
|
|
|
}
|
|
|
|
}
|
2022-09-24 17:47:21 +00:00
|
|
|
|
2022-09-26 16:14:33 +00:00
|
|
|
void subbrute_worker_set_continuous_worker(SubBruteWorker* instance, bool is_continuous_worker) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
instance->is_continuous_worker = is_continuous_worker;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subbrute_worker_get_continuous_worker(SubBruteWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
return instance->is_continuous_worker;
|
2022-09-24 17:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool subbrute_worker_is_running(SubBruteWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
return instance->worker_running;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subbrute_worker_can_transmit(SubBruteWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
return (furi_get_tick() - instance->last_time_tx_data) > SUBBRUTE_SEND_DELAY;
|
|
|
|
}
|
|
|
|
|
2022-09-26 14:27:58 +00:00
|
|
|
bool subbrute_worker_can_manual_transmit(SubBruteWorker* instance) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
return !instance->worker_manual_mode;
|
|
|
|
}
|
|
|
|
|
2022-09-25 13:05:52 +00:00
|
|
|
bool subbrute_worker_transmit(SubBruteWorker* instance, const char* payload) {
|
2022-09-24 17:47:21 +00:00
|
|
|
furi_assert(instance);
|
|
|
|
furi_assert(instance->worker_running);
|
|
|
|
|
|
|
|
if(!subbrute_worker_can_transmit(instance)) {
|
|
|
|
FURI_LOG_E(TAG, "Too early to transmit");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
instance->last_time_tx_data = furi_get_tick();
|
|
|
|
|
2022-09-25 13:05:52 +00:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
//FURI_LOG_D(TAG, "payload: %s", payload);
|
|
|
|
#endif
|
|
|
|
|
2022-09-26 16:14:33 +00:00
|
|
|
while(!subghz_tx_rx_worker_write(instance->subghz_txrx, (uint8_t*)payload, strlen(payload))) {
|
|
|
|
furi_delay_ms(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_hal_subghz_flush_tx();
|
|
|
|
// Stream* stream = flipper_format_get_raw_stream(instance->flipper_format);
|
|
|
|
// stream_clean(stream);
|
|
|
|
// stream_write_cstring(stream, payload);
|
|
|
|
// subghz_transmitter_deserialize(instance->transmitter, instance->flipper_format);
|
2022-09-24 17:47:21 +00:00
|
|
|
|
2022-09-25 20:07:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-26 16:14:33 +00:00
|
|
|
// Init MANUAL
|
2022-09-25 21:07:16 +00:00
|
|
|
bool subbrute_worker_init_manual_transmit(
|
|
|
|
SubBruteWorker* instance,
|
|
|
|
uint32_t frequency,
|
|
|
|
FuriHalSubGhzPreset preset,
|
|
|
|
const char* protocol_name) {
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
FURI_LOG_D(
|
|
|
|
TAG,
|
|
|
|
"subbrute_worker_init_manual_transmit. frequency: %d, protocol: %s",
|
|
|
|
frequency,
|
|
|
|
protocol_name);
|
|
|
|
#endif
|
2022-09-26 16:14:33 +00:00
|
|
|
if(instance->worker_manual_mode || !subbrute_worker_can_manual_transmit(instance) ||
|
|
|
|
instance->worker_running) {
|
2022-09-25 20:07:14 +00:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
FURI_LOG_D(TAG, "cannot transmit");
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-25 21:07:16 +00:00
|
|
|
if(instance->worker_running) {
|
2022-09-25 20:07:14 +00:00
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
FURI_LOG_D(TAG, "subbrute_worker_stop");
|
|
|
|
#endif
|
|
|
|
subbrute_worker_stop(instance);
|
|
|
|
}
|
|
|
|
|
2022-09-25 21:07:16 +00:00
|
|
|
// Not transmit at this period
|
2022-09-25 20:07:14 +00:00
|
|
|
instance->worker_manual_mode = true;
|
|
|
|
|
2022-09-25 21:07:16 +00:00
|
|
|
if(instance->is_manual_init) {
|
|
|
|
FURI_LOG_E(TAG, "Trying to setup without normally shutdown prev transmit session!");
|
|
|
|
subbrute_worker_manual_transmit_stop(instance);
|
|
|
|
}
|
|
|
|
|
2022-09-25 20:07:14 +00:00
|
|
|
instance->preset = preset;
|
|
|
|
instance->frequency = frequency;
|
|
|
|
|
|
|
|
string_clear(instance->protocol_name);
|
|
|
|
string_init_printf(instance->protocol_name, "%s", protocol_name);
|
|
|
|
|
|
|
|
furi_hal_subghz_reset();
|
|
|
|
furi_hal_subghz_idle();
|
|
|
|
furi_hal_subghz_load_preset(instance->preset);
|
|
|
|
|
|
|
|
furi_hal_subghz_set_frequency_and_path(instance->frequency);
|
|
|
|
furi_hal_subghz_flush_rx();
|
|
|
|
|
|
|
|
if(!furi_hal_subghz_is_tx_allowed(frequency)) {
|
2022-09-25 21:07:16 +00:00
|
|
|
FURI_LOG_E(TAG, "Frequency: %d invalid!", frequency);
|
|
|
|
|
2022-09-25 20:07:14 +00:00
|
|
|
instance->frequency = frequency;
|
|
|
|
instance->worker_manual_mode = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
FURI_LOG_I(TAG, "Frequency: %d", frequency);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
instance->environment = subghz_environment_alloc();
|
|
|
|
instance->transmitter = subghz_transmitter_alloc_init(
|
|
|
|
instance->environment, string_get_cstr(instance->protocol_name));
|
|
|
|
|
2022-09-25 21:07:16 +00:00
|
|
|
furi_hal_subghz_reset();
|
|
|
|
furi_hal_subghz_load_preset(instance->preset);
|
|
|
|
instance->frequency = furi_hal_subghz_set_frequency_and_path(frequency);
|
|
|
|
|
2022-09-25 22:45:09 +00:00
|
|
|
furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
|
|
|
|
furi_hal_subghz_sleep();
|
|
|
|
subghz_transmitter_free(instance->transmitter);
|
|
|
|
instance->transmitter = NULL;
|
|
|
|
|
2022-09-25 21:07:16 +00:00
|
|
|
instance->worker_manual_mode = false;
|
|
|
|
instance->is_manual_init = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void subbrute_worker_manual_transmit_stop(SubBruteWorker* instance) {
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
FURI_LOG_D(TAG, "subbrute_worker_manual_transmit_stop");
|
|
|
|
#endif
|
|
|
|
if(!instance->is_manual_init) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_hal_subghz_idle();
|
|
|
|
furi_hal_subghz_sleep();
|
|
|
|
|
|
|
|
if(instance->transmitter != NULL) {
|
|
|
|
subghz_transmitter_free(instance->transmitter);
|
|
|
|
instance->transmitter = NULL;
|
|
|
|
}
|
|
|
|
subghz_environment_free(instance->environment);
|
|
|
|
instance->environment = NULL;
|
|
|
|
|
|
|
|
instance->is_manual_init = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subbrute_worker_manual_transmit(SubBruteWorker* instance, const char* payload) {
|
|
|
|
furi_assert(instance);
|
|
|
|
|
|
|
|
if(instance->worker_manual_mode || !subbrute_worker_can_transmit(instance)) {
|
|
|
|
#ifdef FURI_DEBUG
|
|
|
|
FURI_LOG_D(TAG, "cannot transmit");
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(instance->worker_running) {
|
2022-09-26 16:14:33 +00:00
|
|
|
FURI_LOG_W(TAG, "Worker was working for manual mode. Shutdown thread");
|
2022-09-25 21:07:16 +00:00
|
|
|
subbrute_worker_stop(instance);
|
|
|
|
}
|
|
|
|
if(!instance->is_manual_init) {
|
|
|
|
FURI_LOG_E(TAG, "Manually transmit doesn't set!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
instance->last_time_tx_data = furi_get_tick();
|
|
|
|
instance->worker_manual_mode = true;
|
|
|
|
|
2022-09-25 20:07:14 +00:00
|
|
|
Stream* stream = flipper_format_get_raw_stream(instance->flipper_format);
|
|
|
|
stream_clean(stream);
|
|
|
|
stream_write_cstring(stream, payload);
|
|
|
|
|
2022-09-25 21:07:16 +00:00
|
|
|
instance->transmitter = subghz_transmitter_alloc_init(
|
|
|
|
instance->environment, string_get_cstr(instance->protocol_name));
|
|
|
|
subghz_transmitter_deserialize(instance->transmitter, instance->flipper_format);
|
2022-09-25 20:07:14 +00:00
|
|
|
furi_hal_subghz_reset();
|
|
|
|
furi_hal_subghz_load_preset(instance->preset);
|
2022-09-25 21:07:16 +00:00
|
|
|
instance->frequency = furi_hal_subghz_set_frequency_and_path(instance->frequency);
|
2022-09-25 20:07:14 +00:00
|
|
|
|
|
|
|
furi_hal_subghz_start_async_tx(subghz_transmitter_yield, instance->transmitter);
|
|
|
|
|
|
|
|
while(!furi_hal_subghz_is_async_tx_complete()) {
|
2022-09-25 21:07:16 +00:00
|
|
|
furi_delay_ms(SUBBRUTE_TX_TIMEOUT);
|
2022-09-25 20:07:14 +00:00
|
|
|
}
|
|
|
|
furi_hal_subghz_stop_async_tx();
|
|
|
|
|
2022-09-25 22:42:39 +00:00
|
|
|
furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
|
2022-09-25 21:07:16 +00:00
|
|
|
furi_hal_subghz_sleep();
|
2022-09-25 20:07:14 +00:00
|
|
|
subghz_transmitter_free(instance->transmitter);
|
|
|
|
instance->transmitter = NULL;
|
|
|
|
|
2022-09-25 22:36:38 +00:00
|
|
|
stream_clean(stream);
|
|
|
|
|
2022-09-25 20:07:14 +00:00
|
|
|
instance->worker_manual_mode = false;
|
|
|
|
|
2022-09-24 17:47:21 +00:00
|
|
|
return true;
|
|
|
|
}
|