Skip to content

Commit

Permalink
feat: Some refactoring to clean-up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
derskythe committed Jul 22, 2024
1 parent 3227a82 commit 6fd7fac
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 188 deletions.
28 changes: 21 additions & 7 deletions helpers/gui_top_buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,29 @@
#include <gui/icon_animation.h>

/**
* Thanks to the author of metronome
* @param canvas
* @param str
* @brief This function draws a button in the top left corner of the canvas with icon and string.
*
* The design and layout of the button is defined within this function.
*
*
* @param[in] canvas This is a pointer to the @c Canvas structure where the button will be drawn.
* @param[in] str This is a pointer to the character string that will be drawn within the button.
*
* @note Thanks to the author of metronome @see https://github.com/panki27/Metronome
*
*/
void elements_button_top_left(Canvas* canvas, const char* str);

/**
* Thanks to the author of metronome
* @param canvas
* @param str
* @brief This function draws a button in the top right corner of the canvas with icon and string.
*
* The design and layout of the button is defined within this function.
*
*
* @param[in] canvas This is a pointer to the @c Canvas structure where the button will be drawn.
* @param[in] str This is a pointer to the character string that will be drawn within the button.
*
* @note Thanks to the author of metronome @see https://github.com/panki27/Metronome
*
*/
void elements_button_top_right(Canvas* canvas, const char* str);
void elements_button_top_right(Canvas* canvas, const char* str);
45 changes: 41 additions & 4 deletions helpers/subbrute_radio_device_loader.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
#include <furi/core/check.h>
#include "subbrute_radio_device_loader.h"

#include <applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h>
#include <furi/core/check.h>
#include <lib/subghz/devices/cc1101_int/cc1101_int_interconnect.h>

#include "subbrute_radio_device_loader.h"
#include "applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h"

#define TAG "SubBruteRadioDeviceLoader"

/**
* @brief This function is responsible for powering on the radio device loader.
*
* This function is responsible for powering on the radio device loader.
* It attempts to enable the OTG (On-The-Go) power.
* The function uses a @c `while` loop with a maximum of 5 attempts to enable the OTG power.
* If it succeeds, it will break out of the loop.
* If the maximum number of attempts is reached and the OTG power is not enabled,
* the function checks the USB voltage using @c `furi_hal_power_get_usb_voltage()`.
*
* If the voltage is below 4.5 volts, an error message is logged using @c `FURI_LOG_E()`
* with a message includes the value of @c `furi_hal_power_check_otg_fault()`
* (converted to 1 if true, 0 if false),
* which checks the OTG fault status using the BQ2589 chip.
*
*/
static void subbrute_radio_device_loader_power_on() {
uint8_t attempts = 5;
while(--attempts > 0) {
Expand All @@ -24,13 +40,34 @@ static void subbrute_radio_device_loader_power_on() {
}
}

/**
* @brief Turns off the power of the radio device loader.
*
* This function checks if OTG (On-The-Go) power is enabled, and if so, it disables it.
*
* @note This function is static and used internally by the radio device loader module.
*
*/
static void subbrute_radio_device_loader_power_off() {
if(furi_hal_power_is_otg_enabled()) {
furi_hal_power_disable_otg();
}
}

bool subbrute_radio_device_loader_is_connect_external(const char* name) {
/**
* @brief Checks if a radio device of the given name is connected externally.
*
* This function checks if a radio device with the specified name is connected externally.
* If the OTG power is not enabled, it attempts to power on the radio device loader.
* It then retrieves the device information using the provided name and checks if it is connected.
* Finally, if the OTG power was initially disabled, it powers off the radio device loader.
*
* @param[in] name The name of the radio device to check.
*
* @return true if the device is connected externally, false otherwise.
*
*/
static bool subbrute_radio_device_loader_is_connect_external(const char* name) {
bool is_connect = false;
bool is_otg_enabled = furi_hal_power_is_otg_enabled();

Expand Down
20 changes: 14 additions & 6 deletions helpers/subbrute_radio_device_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,33 @@ typedef enum {
*
* This function is used to set the SubGhz radio device type for the SubBrute radio device loader.
*
* @param current_radio_device Pointer to the current SubGhz radio device.
* @param radio_device_type The desired SubGhz radio device type.
* @return const SubGhzDevice* Pointer to the new SubGhz radio device.
* @param [in] current_radio_device Pointer to the current SubGhz radio device.
* @param [in] radio_device_type The desired SubGhz radio device type.
*
* @return @c const SubGhzDevice* Pointer to the new SubGhz radio device.
*
* @remark This function sets the SubGhz radio device type for the SubBrute radio device loader.
* The current radio device will be replaced with a new instance of the specified radio device type.
* If @p current_radio_device is NULL, a new instance of the specified radio device type will be created.
*
* @note The caller is responsible for handling memory deallocation of the returned pointer.
*
*/
const SubGhzDevice* subbrute_radio_device_loader_set(
const SubGhzDevice* current_radio_device,
SubGhzRadioDeviceType radio_device_type);

/**
* @brief Unloads a SubGhz radio device.
* @brief Unloads the radio device in the subbrute radio device loader.
*
* This function is called to unload the specified radio device in the
* subbrute radio device loader.
* It first checks if the radio device is valid and then performs the necessary steps to
* power off the device.
* If the radio device is not the CC1101 internal device, it calls @c subghz_devices_end()
* function to terminate the radio device.
*
* This function unloads a SubGhz radio device and performs any necessary cleanup.
* @param[in] radio_device A pointer to the radio device to unload.
*
* @param radio_device Pointer to the SubGhzDevice structure representing the radio device to be unloaded.
*/
void subbrute_radio_device_loader_end(const SubGhzDevice* radio_device);
5 changes: 3 additions & 2 deletions helpers/subbrute_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ typedef struct SubBruteWorker SubBruteWorker;
* This function creates a new SubBruteWorker object by allocating memory for it on the heap and
* initializes it with the provided radio_device. The radio_device parameter must not be NULL.
*
* @param radio_device A pointer to a valid SubGhzDevice object.
* @return A pointer to the newly allocated SubBruteWorker object, or NULL if memory allocation failed.
* @param[in] radio_device A pointer to a valid SubGhzDevice object.
* @return A pointer to the newly allocated SubBruteWorker object, or @c NULL if memory
* allocation was failed.
*/
SubBruteWorker* subbrute_worker_alloc(const SubGhzDevice* radio_device);

Expand Down
55 changes: 28 additions & 27 deletions helpers/subbrute_worker_private.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "subbrute_worker.h"

#include <lib/subghz/protocols/base.h>
#include <lib/subghz/transmitter.h>
#include <lib/subghz/receiver.h>
Expand All @@ -14,41 +15,41 @@
* It manages the state, configuration and execution of the sub-brute forcing algorithm.
*/
struct SubBruteWorker {
SubBruteWorkerState state;
volatile bool worker_running;
volatile bool initiated;
volatile bool transmit_mode;
SubBruteWorkerState state; /**< State of the worker */
volatile bool worker_running; /**< Worker running state */
volatile bool initiated; /**< Initiated state */
volatile bool transmit_mode; /**< Transmit mode */

uint64_t step; /**< Current step */

// Current step
uint64_t step;
FuriThread* thread; /**< Thread */

// SubGhz
FuriThread* thread;
/** @see @c SubGhz service for more info */
SubGhzEnvironment* environment; /**< Environment */
SubGhzProtocolDecoderBase* decoder_result;
SubGhzEnvironment* environment;
SubGhzTransmitter* transmitter;
const char* protocol_name;
uint8_t tx_timeout_ms;
const SubGhzDevice* radio_device;
SubGhzTransmitter* transmitter; /**< Transmitter */
const char* protocol_name; /**< Name of the protocol */
uint8_t tx_timeout_ms; /**< Timeout for transmit */
const SubGhzDevice* radio_device; /**< Radio device */

// Initiated values
SubBruteAttacks attack; // Attack state
uint32_t frequency;
FuriHalSubGhzPreset preset;
SubBruteFileProtocol file;
uint8_t bits;
uint32_t te;
uint8_t repeat;
uint8_t load_index; // Index of group to bruteforce in loaded file
uint64_t file_key;
uint64_t max_value; // Max step
bool two_bytes;
SubBruteAttacks attack; /**< Attack state */
uint32_t frequency; /**< Frequency */
FuriHalSubGhzPreset preset; /**< Preset */
SubBruteFileProtocol file; /**< File protocol */
uint8_t bits; /**< Number of bits in key */
uint32_t te; /**< Time to wait for response */
uint8_t repeat; /**< Number of extra repeats */
uint8_t load_index; /**< Index of group to bruteforce in loaded file */
uint64_t file_key; /**< Key from file */
uint64_t max_value; /**< Max step */
bool two_bytes; /**< Two bytes key */

// Manual transmit
uint32_t last_time_tx_data;
uint32_t last_time_tx_data; /**< Last time data was transmitted */

// Callback for changed states
SubBruteWorkerCallback callback;
SubBruteWorkerCallback callback; /**< Callback for changed states */
void* context;
};

Expand Down Expand Up @@ -85,4 +86,4 @@ void subbrute_worker_subghz_transmit(SubBruteWorker* instance, FlipperFormat* fl
*
* @note This function does not return any values.
*/
void subbrute_worker_send_callback(SubBruteWorker* instance);
void subbrute_worker_send_callback(SubBruteWorker* instance);
5 changes: 2 additions & 3 deletions subbrute.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ SubBruteState* subbrute_alloc() {

instance->settings = subbrute_settings_alloc();
subbrute_settings_load(instance->settings);
//instance->flipper_format = flipper_format_string_alloc();
//instance->environment = subghz_environment_alloc();

// Uncomment to enable Debug pin output on PIN 17(1W)
// subghz_devices_set_async_mirror_pin(instance->radio_device, &gpio_ibutton);
Expand Down Expand Up @@ -166,7 +164,7 @@ void subbrute_free(SubBruteState* instance) {
view_dispatcher_remove_view(instance->view_dispatcher, SubBruteViewStack);
view_stack_free(instance->view_stack);

//Dialog
// Dialog
furi_record_close(RECORD_DIALOGS);
instance->dialogs = NULL;

Expand Down Expand Up @@ -216,5 +214,6 @@ int32_t subbrute_app(void* p) {
subbrute_free(instance);

furi_hal_power_suppress_charge_exit();

return 0;
}
4 changes: 2 additions & 2 deletions subbrute_custom_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
* Event type for the load file event.
*/
typedef enum {
// Reserve first 100 events for button types and indexes, starting from 0
/** Reserve first 100 events for button types and indexes, starting from 0 */
SubBruteCustomEventTypeReserved = 100,

SubBruteCustomEventTypeBackPressed,
Expand All @@ -94,4 +94,4 @@ typedef enum {
SubBruteCustomEventTypePopupClosed,

SubBruteCustomEventTypeLoadFile,
} SubBruteCustomEvent;
} SubBruteCustomEvent;
Loading

0 comments on commit 6fd7fac

Please sign in to comment.