Skip to content

Commit

Permalink
Merge pull request #76 from derskythe/feat/refactor
Browse files Browse the repository at this point in the history
feat: Some refactoring to clean-up the code
  • Loading branch information
derskythe authored Aug 6, 2024
2 parents 3227a82 + 37436c7 commit def1d0f
Show file tree
Hide file tree
Showing 16 changed files with 395 additions and 188 deletions.
126 changes: 126 additions & 0 deletions .github/workflows/pr-build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
name: "PR Build"
run-name: "PR Build ${{ inputs.DEPLOY_TARGET }} triggered by ${{ github.EVENT_NAME }} (@${{ github.ACTOR }})"

on:
pull_request:
types: [opened, reopened]

jobs:
build-for-pr:
runs-on: ubuntu-latest
env:
REPO_SELF: ${{ vars.REPO_SELF }}
APP_PATH: "applications_user/subbrute"
RELATIVE_PATH: "applications/external/subbrute"
PREV_TAG: ""
APP_NAME: ""
ZIP_NAME: ""
ZIP_TAG: ""
TGZ_NAME: ""
TGZ_TAG: ""
SHA: ""
FW_VERSION: ""
RELEASE_VERSION: ""
strategy:
fail-fast: false
matrix:
firmware: [unlshd, official]
include:
- firmware: unlshd
url: ${{ vars.REPO_UNLEASHED }}
version: ${{ vars.FIRMWARE_VERSION }}
src-included: 0
- firmware: official
url: ${{ vars.REPO_OFFICIAL }}
version: official
src-included: 0
steps:
- name: Copy Firmware Files
uses: actions/checkout@v4
with:
repository: ${{ matrix.url }}
clean: true
submodules: true
ref: dev

- name: Copy Repo Files
if: ${{ matrix.src-included == 0 }}
uses: actions/checkout@v4
with:
repository: ${{ vars.REPO_SELF }}
clean: true
submodules: true
path: ${{ env.APP_PATH }}

- name: Remove other apps
shell: pwsh
# rm to remove problem FAP which includes non-existent files
run: |
Remove-Item -Force -Recurse ./applications/debug -ErrorAction SilentlyContinue
Remove-Item -Force -Recurse ./applications/examples -ErrorAction SilentlyContinue
Remove-Item -Force -Recurse ${{ env.RELATIVE_PATH }} -ErrorAction SilentlyContinue
- name: Get SHA of our application and release version
shell: pwsh
env:
GITHUB_TOKEN: ${{ secrets.FLIPPER_TOKEN }}
INPUT_VERSION: ${{ inputs.version }}
run: |
Write-Output "::notice title=${{ matrix.firmware }} Input Version Number::v${{ inputs.version }}"
$fwInfo = ((gh release view --json tagName,url -R ${{ matrix.url }}) | ConvertFrom-Json)
cd '${{ env.APP_PATH }}'
$sha = (git rev-parse --verify HEAD)
$releaseVersion = ''
$latestTag = ((gh release view --json tagName,url -R ${{ vars.REPO_SELF }}) | ConvertFrom-Json ).tagName
if ( [string]::IsNullOrWhitespace($env:INPUT_VERSION) ) {
Write-Output "::notice title=${{ matrix.firmware }} Latest tag::$latestTag"
$lastIndex = $latestTag.LastIndexOf('.')
$minorValue = $latestTag.Substring($latestTag.LastIndexOf('.') + 1)
$minorValue = [Convert]::ToInt32($minorValue) + 1
$newTag = $latestTag.Substring(0, $lastIndex)
$releaseVersion = ('{0}.{1}' -f $newTag, $minorValue)
} else {
$releaseVersion = "$env:INPUT_VERSION"
}
if ( $releaseVersion.StartsWith('v') ) {
$releaseVersion = $releaseVersion.Substring(1)
}
Write-Output "::notice title=${{ matrix.firmware }} PREV_TAG::$latestTag"
Write-Output "::notice title=${{ matrix.firmware }} RELEASE_VERSION::$releaseVersion"
Write-Output "::notice title=${{ matrix.firmware }} SHA::$sha"
Write-Output ('::notice title=${{ matrix.firmware }} FW_VERSION::{0}' -f $fwInfo.tagName)
Write-Output ('RELEASE_VERSION={0}' -f $releaseVersion) >> $env:GITHUB_ENV
Write-Output ('SHA={0}' -f $sha) >> $env:GITHUB_ENV
Write-Output ('FW_VERSION={0}' -f $fwInfo.tagName) >> $env:GITHUB_ENV
Write-Output ('PREV_TAG={0}' -f $latestTag) >> $env:GITHUB_ENV
- name: Build Firmware
shell: bash
if: ${{ success() }}
env:
FBT_NO_SYNC: 0
DIST_SUFFIX: ${{ matrix.version }}
WORKFLOW_BRANCH_OR_TAG: release-cfw
run: |
./fbt COMPACT=1 DEBUG=0 FBT_NO_SYNC=0
- name: Build FAPs
shell: bash
if: ${{ success() }}
env:
FBT_NO_SYNC: 0
DIST_SUFFIX: ${{ matrix.version }}
WORKFLOW_BRANCH_OR_TAG: release-cfw
# rm to remove problem FAP which includes non-existent files
run: |
./fbt COMPACT=1 DEBUG=0 FBT_NO_SYNC=0 fap_dist
#EOF
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;
}
Loading

0 comments on commit def1d0f

Please sign in to comment.