refactor worker moved it to SubBruteState

This commit is contained in:
DerSkythe 2022-09-26 01:07:16 +04:00
parent ba5f590dab
commit 23f6ea2e05
9 changed files with 132 additions and 152 deletions

View file

@ -10,6 +10,7 @@ struct SubBruteWorker {
FuriThread* thread;
volatile bool worker_running;
volatile bool worker_manual_mode;
bool is_manual_init;
SubGhzEnvironment* environment;
SubGhzTransmitter* transmitter;
@ -30,7 +31,7 @@ struct SubBruteWorker {
#define SUBBRUTE_TXRX_WORKER_BUF_SIZE 2048
#define SUBBRUTE_TXRX_WORKER_MAX_TXRX_SIZE 60
#define SUBBRUTE_TXRX_WORKER_TIMEOUT_READ_WRITE_BUF 40
#define SUBBRUTE_TX_TIMEOUT 200
#define SUBBRUTE_TX_TIMEOUT 50
#define SUBBRUTE_SEND_DELAY 260
/**
@ -115,6 +116,16 @@ void subbrute_worker_free(SubBruteWorker* instance) {
furi_assert(instance);
furi_assert(!instance->worker_running);
if(instance->transmitter != NULL) {
subghz_transmitter_free(instance->transmitter);
instance->transmitter = NULL;
}
if(instance->environment != NULL) {
subghz_environment_free(instance->environment);
instance->environment = NULL;
}
furi_thread_free(instance->thread);
flipper_format_free(instance->flipper_format);
@ -130,7 +141,7 @@ bool subbrute_worker_start(
const char* protocol_name) {
furi_assert(instance);
if (instance->worker_manual_mode) {
if(instance->worker_manual_mode) {
return false;
}
@ -211,29 +222,39 @@ bool subbrute_worker_transmit(SubBruteWorker* instance, const char* payload) {
return true;
}
bool subbrute_worker_single_transmit(SubBruteWorker* instance,
uint32_t frequency,
FuriHalSubGhzPreset preset,
const char* protocol_name,
const char* payload) {
furi_assert(instance);
if (instance->worker_manual_mode || !subbrute_worker_can_transmit(instance)) {
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
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) {
if(instance->worker_running) {
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "subbrute_worker_stop");
#endif
subbrute_worker_stop(instance);
}
instance->last_time_tx_data = furi_get_tick();
// Not transmit at this period
instance->worker_manual_mode = true;
if(instance->is_manual_init) {
FURI_LOG_E(TAG, "Trying to setup without normally shutdown prev transmit session!");
subbrute_worker_manual_transmit_stop(instance);
}
instance->preset = preset;
instance->frequency = frequency;
@ -248,6 +269,8 @@ bool subbrute_worker_single_transmit(SubBruteWorker* instance,
furi_hal_subghz_flush_rx();
if(!furi_hal_subghz_is_tx_allowed(frequency)) {
FURI_LOG_E(TAG, "Frequency: %d invalid!", frequency);
instance->frequency = frequency;
instance->worker_manual_mode = false;
return false;
@ -261,30 +284,81 @@ bool subbrute_worker_single_transmit(SubBruteWorker* instance,
instance->transmitter = subghz_transmitter_alloc_init(
instance->environment, string_get_cstr(instance->protocol_name));
furi_hal_subghz_reset();
furi_hal_subghz_load_preset(instance->preset);
instance->frequency = furi_hal_subghz_set_frequency_and_path(frequency);
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) {
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "subbrute_worker_stop");
#endif
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;
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);
instance->transmitter = subghz_transmitter_alloc_init(
instance->environment, string_get_cstr(instance->protocol_name));
subghz_transmitter_deserialize(instance->transmitter, instance->flipper_format);
furi_hal_subghz_reset();
furi_hal_subghz_load_preset(instance->preset);
frequency = furi_hal_subghz_set_frequency_and_path(frequency);
instance->frequency = furi_hal_subghz_set_frequency_and_path(instance->frequency);
furi_hal_power_suppress_charge_enter();
furi_hal_subghz_start_async_tx(subghz_transmitter_yield, instance->transmitter);
while(!furi_hal_subghz_is_async_tx_complete()) {
furi_delay_ms(SUBBRUTE_SEND_DELAY);
furi_delay_ms(SUBBRUTE_TX_TIMEOUT);
}
furi_hal_subghz_stop_async_tx();
furi_hal_subghz_sleep();
furi_hal_power_suppress_charge_exit();
subghz_transmitter_free(instance->transmitter);
instance->transmitter = NULL;
subghz_environment_free(instance->environment);
instance->environment = NULL;
instance->worker_manual_mode = false;

View file

@ -27,8 +27,9 @@ void subbrute_worker_stop(SubBruteWorker* instance);
bool subbrute_worker_is_running(SubBruteWorker* instance);
bool subbrute_worker_can_transmit(SubBruteWorker* instance);
bool subbrute_worker_transmit(SubBruteWorker* instance, const char* payload);
bool subbrute_worker_single_transmit(SubBruteWorker* instance,
uint32_t frequency,
FuriHalSubGhzPreset preset,
const char* protocol_name,
const char* payload);
bool subbrute_worker_init_manual_transmit(SubBruteWorker* instance,
uint32_t frequency,
FuriHalSubGhzPreset preset,
const char* protocol_name);
bool subbrute_worker_manual_transmit(SubBruteWorker* instance, const char* payload);
void subbrute_worker_manual_transmit_stop(SubBruteWorker* instance);

View file

@ -1,6 +1,7 @@
#include "../subbrute_i.h"
#include "../subbrute_custom_event.h"
#include "../views/subbrute_attack_view.h"
#include "../helpers/subbrute_worker.h"
static void subbrute_scene_run_attack_callback(SubBruteCustomEvent event, void* context) {
furi_assert(context);
@ -33,11 +34,11 @@ void subbrute_scene_run_attack_on_enter(void* context) {
true);
// Start worker if not started
/*subbrute_attack_view_start_worker(
view,
subbrute_worker_init_manual_transmit(
instance->worker,
instance->device->frequency,
instance->device->preset,
string_get_cstr(instance->device->protocol_name));*/
string_get_cstr(instance->device->protocol_name));
}
bool subbrute_scene_run_attack_on_event(void* context, SceneManagerEvent event) {
@ -54,16 +55,11 @@ bool subbrute_scene_run_attack_on_event(void* context, SceneManagerEvent event)
consumed = true;
}
} else if(event.type == SceneManagerEventTypeTick) {
if(subbrute_attack_view_can_send(view)) {
if(subbrute_worker_can_transmit(instance->worker)) {
// Blink
notification_message(instance->notifications, &sequence_blink_yellow_100);
if(subbrute_attack_view_single_transmit(
view,
instance->device->frequency,
instance->device->preset,
string_get_cstr(instance->device->protocol_name),
instance->device->payload)) {
if(subbrute_worker_manual_transmit(instance->worker, instance->device->payload)) {
// Make payload for new iteration or exit
if(instance->device->key_index + 1 > instance->device->max_value) {
// End of list

View file

@ -27,7 +27,11 @@ void subbrute_scene_setup_attack_on_enter(void* context) {
instance->device->key_index,
false);
subbrute_attack_view_stop_worker(view);
subbrute_worker_init_manual_transmit(
instance->worker,
instance->device->frequency,
instance->device->preset,
string_get_cstr(instance->device->protocol_name));
instance->current_view = SubBruteViewAttack;
subbrute_attack_view_set_callback(view, subbrute_scene_setup_attack_callback, instance);
@ -40,6 +44,7 @@ void subbrute_scene_setup_attack_on_exit(void* context) {
FURI_LOG_D(TAG, "subbrute_scene_setup_attack_on_exit");
#endif
SubBruteState* instance = (SubBruteState*)context;
subbrute_worker_manual_transmit_stop(instance->worker);
notification_message(instance->notifications, &sequence_blink_stop);
}
@ -52,7 +57,7 @@ bool subbrute_scene_setup_attack_on_event(void* context, SceneManagerEvent event
if(event.event == SubBruteCustomEventTypeTransmitStarted) {
scene_manager_next_scene(instance->scene_manager, SubBruteSceneRunAttack);
} else if(event.event == SubBruteCustomEventTypeSaveFile) {
//subbrute_attack_view_stop_worker(view);
subbrute_worker_manual_transmit_stop(instance->worker);
subbrute_attack_view_init_values(
view,
@ -97,7 +102,7 @@ bool subbrute_scene_setup_attack_on_event(void* context, SceneManagerEvent event
instance->device->max_value;
subbrute_attack_view_set_current_step(view, instance->device->key_index);
} else if(event.event == SubBruteCustomEventTypeTransmitCustom) {
if(subbrute_attack_view_can_send(view)) {
if(subbrute_worker_can_transmit(instance->worker)) {
// Blink
notification_message(instance->notifications, &sequence_blink_green_100);
@ -110,12 +115,7 @@ bool subbrute_scene_setup_attack_on_event(void* context, SceneManagerEvent event
// }
subbrute_device_create_packet_parsed(
instance->device, instance->device->key_index);
subbrute_attack_view_single_transmit(
view,
instance->device->frequency,
instance->device->preset,
string_get_cstr(instance->device->protocol_name),
instance->device->payload);
subbrute_worker_manual_transmit(instance->worker, instance->device->payload);
// Stop
notification_message(instance->notifications, &sequence_blink_stop);

View file

@ -92,6 +92,9 @@ SubBruteState* subbrute_alloc() {
// Devices
instance->device = subbrute_device_alloc();
// Worker
instance->worker = subbrute_worker_alloc();
// TextInput
instance->text_input = text_input_alloc();
view_dispatcher_add_view(
@ -145,6 +148,13 @@ void subbrute_free(SubBruteState* instance) {
#endif
subbrute_device_free(instance->device);
// SubBruteWorker
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "free SubBruteDevice");
#endif
subbrute_worker_stop(instance->worker);
subbrute_worker_free(instance->worker);
// Notifications
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "free Notifications");
@ -280,31 +290,16 @@ const char* subbrute_get_small_menu_name(SubBruteAttacks index) {
// ENTRYPOINT
int32_t subbrute_app(void* p) {
UNUSED(p);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "subbrute_app");
#endif
SubBruteState* instance = subbrute_alloc();
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Starting subbrute_alloc done");
#endif
view_dispatcher_attach_to_gui(
instance->view_dispatcher, instance->gui, ViewDispatcherTypeFullscreen);
scene_manager_next_scene(instance->scene_manager, SubBruteSceneStart);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "scene_manager_next_scene set");
#endif
furi_hal_power_suppress_charge_enter();
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "view_dispatcher_run");
#endif
view_dispatcher_run(instance->view_dispatcher);
furi_hal_power_suppress_charge_exit();
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "before subbrute_free");
#endif
subbrute_free(instance);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "return 0");
#endif
return 0;
}

View file

@ -31,6 +31,7 @@
#include <notification/notification_messages.h>
#include "subbrute_device.h"
#include "helpers/subbrute_worker.h"
#include "subbrute.h"
#include "scenes/subbrute_scene.h"
#include "views/subbrute_attack_view.h"
@ -68,6 +69,7 @@ struct SubBruteState {
SceneManager* scene_manager;
SubBruteDevice* device;
SubBruteWorker* worker;
//Menu stuff
// TODO: Do we need it?

View file

@ -1,6 +1,5 @@
#include "subbrute_attack_view.h"
#include "../subbrute_i.h"
#include "../helpers/subbrute_worker.h"
#include "assets_icons.h"
#include <input/input.h>
@ -14,7 +13,6 @@ struct SubBruteAttackView {
View* view;
SubBruteAttackViewCallback callback;
void* context;
SubBruteWorker* worker;
};
typedef struct {
@ -89,14 +87,8 @@ bool subbrute_attack_view_input(InputEvent* event, void* context) {
// instance->callback(SubBruteCustomEventTypeBackPressed, instance->context);
// }
} else if(event->key == InputKeyUp) {
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "InputKey: %d UP", event->key);
#endif
instance->callback(SubBruteCustomEventTypeSaveFile, instance->context);
} else if(event->key == InputKeyDown) {
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "InputKey: %d DOWN", event->key);
#endif
instance->callback(SubBruteCustomEventTypeTransmitCustom, instance->context);
} else if(event->type == InputTypeShort) {
if(event->key == InputKeyLeft) {
@ -169,7 +161,6 @@ SubBruteAttackView* subbrute_attack_view_alloc() {
view_set_enter_callback(instance->view, subbrute_attack_view_enter);
view_set_exit_callback(instance->view, subbrute_attack_view_exit);
instance->worker = subbrute_worker_alloc();
return instance;
}
@ -188,7 +179,6 @@ void subbrute_attack_view_free(SubBruteAttackView* instance) {
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "subbrute_attack_view_free");
#endif
subbrute_worker_free(instance->worker);
with_view_model(
instance->view, (SubBruteAttackViewModel * model) {
@ -257,60 +247,6 @@ void subbrute_attack_view_init_values(
});
}
void subbrute_attack_view_stop_worker(SubBruteAttackView* instance) {
furi_assert(instance);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "subbrute_attack_view_stop_worker");
#endif
subbrute_worker_stop(instance->worker);
}
bool subbrute_attack_view_can_send(SubBruteAttackView* instance) {
furi_assert(instance);
return subbrute_worker_can_transmit(instance->worker);
}
void subbrute_attack_view_start_worker(
SubBruteAttackView* instance,
uint32_t frequency,
FuriHalSubGhzPreset preset,
const char* protocol_name) {
furi_assert(instance);
#ifdef FURI_DEBUG
FURI_LOG_D(
TAG,
"start_worker. frequency: %d, preset: %d, protocol_name: %s",
frequency,
preset,
protocol_name);
#endif
if(!subbrute_worker_is_running(instance->worker)) {
subbrute_worker_start(instance->worker, frequency, preset, protocol_name);
}
}
bool subbrute_attack_view_transmit(SubBruteAttackView* instance, const char* payload) {
furi_assert(instance);
return subbrute_worker_transmit(instance->worker, payload);
}
bool subbrute_attack_view_single_transmit(
SubBruteAttackView* instance,
uint32_t frequency,
FuriHalSubGhzPreset preset,
const char* protocol_name,
const char* payload) {
return subbrute_worker_single_transmit(
instance->worker, frequency, preset, protocol_name, payload);
}
bool subbrute_attack_view_is_worker_running(SubBruteAttackView* instance) {
furi_assert(instance);
return subbrute_worker_is_running(instance->worker);
}
void subbrute_attack_view_exit(void* context) {
furi_assert(context);
SubBruteAttackView* instance = context;
@ -322,13 +258,6 @@ void subbrute_attack_view_exit(void* context) {
icon_animation_stop(model->icon);
return false;
});
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "subbrute_worker_stop");
furi_delay_ms(150);
#endif
// Just stop, make free in free method
subbrute_worker_stop(instance->worker);
}
void elements_button_top_left(Canvas* canvas, const char* str) {

View file

@ -25,19 +25,4 @@ void subbrute_attack_view_init_values(
uint8_t index,
uint64_t max_value,
uint64_t current_step,
bool is_attacking);
void subbrute_attack_view_stop_worker(SubBruteAttackView* instance);
bool subbrute_attack_view_can_send(SubBruteAttackView* instance);
void subbrute_attack_view_start_worker(
SubBruteAttackView* instance,
uint32_t frequency,
FuriHalSubGhzPreset preset,
const char* protocol_name);
bool subbrute_attack_view_transmit(SubBruteAttackView* instance, const char* payload);
bool subbrute_attack_view_single_transmit(
SubBruteAttackView* instance,
uint32_t frequency,
FuriHalSubGhzPreset preset,
const char* protocol_name,
const char* payload);
bool subbrute_attack_view_is_worker_running(SubBruteAttackView* instance);
bool is_attacking);

View file

@ -18,14 +18,12 @@ void subbrute_main_view_set_callback(
SubBruteMainView* subbrute_main_view_alloc();
void subbrute_main_view_free(SubBruteMainView* instance);
View* subbrute_main_view_get_view(SubBruteMainView* instance);
void subbrute_main_view_set_index(
SubBruteMainView* instance,
uint8_t idx,
bool is_select_byte,
const char* key_field);
uint8_t subbrute_main_view_get_index(SubBruteMainView* instance);
void subbrute_attack_view_enter(void* context);
void subbrute_attack_view_exit(void* context);
bool subbrute_attack_view_input(InputEvent* event, void* context);