Skip to content

Commit

Permalink
SubGHz: Manual region bypass (#47)
Browse files Browse the repository at this point in the history
* Subghz: brought back manual region bypass

* MNTM: change frequency check
this allows to add "extended" frequencies to your custom added frequencies

* MNTM: reboot when changing extended frequencies

* Fix regio typo

* Show bypass before extend

* Consistency with OFW and UL api for region, version, otp

* Fix API symbols

* Restrict only on TX

* Oops

* Improve bypass/extend UI

---------

Co-authored-by: Willy-JL <[email protected]>
  • Loading branch information
Sil333033 and Willy-JL authored Mar 20, 2024
1 parent f5901cf commit 0111d85
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 35 deletions.
3 changes: 3 additions & 0 deletions applications/main/momentum_app/momentum_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ bool momentum_app_apply(MomentumApp* app) {
if(!flipper_format_write_bool(
file, "use_ext_range_at_own_risk", &app->subghz_extend, 1))
break;
if(!flipper_format_write_bool(file, "ignore_default_tx_region", &app->subghz_bypass, 1))
break;
} while(0);
flipper_format_free(file);
}
Expand Down Expand Up @@ -307,6 +309,7 @@ MomentumApp* momentum_app_alloc() {
file = flipper_format_file_alloc(storage);
if(flipper_format_file_open_existing(file, "/ext/subghz/assets/extend_range.txt")) {
flipper_format_read_bool(file, "use_ext_range_at_own_risk", &app->subghz_extend, 1);
flipper_format_read_bool(file, "ignore_default_tx_region", &app->subghz_bypass, 1);
}
flipper_format_free(file);
furi_record_close(RECORD_STORAGE);
Expand Down
1 change: 1 addition & 0 deletions applications/main/momentum_app/momentum_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ typedef struct {
uint8_t subghz_hopper_index;
char subghz_freq_buffer[7];
bool subghz_extend;
bool subghz_bypass;
RgbColor lcd_color;
Rgb565Color vgm_color;
char device_name[FURI_HAL_VERSION_ARRAY_NAME_LENGTH];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@

enum VarItemListIndex {
VarItemListIndexSubghzFreqs,
VarItemListIndexSubghzBypass,
VarItemListIndexSubghzExtend,
VarItemListIndexGpioPins,
VarItemListIndexFileNamingPrefix,
};

void momentum_app_scene_protocols_var_item_list_callback(void* context, uint32_t index) {
MomentumApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, index);
}

static void xtreme_app_scene_protocols_subghz_bypass_changed(VariableItem* item) {
MomentumApp* app = variable_item_get_context(item);
app->subghz_bypass = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, app->subghz_bypass ? "ON" : "OFF");
app->save_subghz = true;
app->require_reboot = true;
variable_item_set_locked(
variable_item_list_get(app->var_item_list, VarItemListIndexSubghzExtend),
!app->subghz_bypass,
NULL);
}

static void momentum_app_scene_protocols_subghz_extend_changed(VariableItem* item) {
MomentumApp* app = variable_item_get_context(item);
app->subghz_extend = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, app->subghz_extend ? "ON" : "OFF");
app->save_subghz = true;
app->require_reboot = true;
}

static void momentum_app_scene_protocols_file_naming_prefix_changed(VariableItem* item) {
Expand All @@ -35,9 +50,23 @@ void momentum_app_scene_protocols_on_enter(void* context) {
variable_item_set_current_value_text(item, ">");

item = variable_item_list_add(
var_item_list, "SubGHz Extend", 2, momentum_app_scene_protocols_subghz_extend_changed, app);
var_item_list,
"SubGHz Bypass Region Lock",
2,
xtreme_app_scene_protocols_subghz_bypass_changed,
app);
variable_item_set_current_value_index(item, app->subghz_bypass);
variable_item_set_current_value_text(item, app->subghz_bypass ? "ON" : "OFF");

item = variable_item_list_add(
var_item_list,
"SubGHz Extend Freq Bands",
2,
momentum_app_scene_protocols_subghz_extend_changed,
app);
variable_item_set_current_value_index(item, app->subghz_extend);
variable_item_set_current_value_text(item, app->subghz_extend ? "ON" : "OFF");
variable_item_set_locked(item, !app->subghz_bypass, "Must bypass\nregion lock\nfirst!");

item = variable_item_list_add(var_item_list, "GPIO Pins", 0, NULL, app);
variable_item_set_current_value_text(item, ">");
Expand Down
3 changes: 3 additions & 0 deletions applications/main/subghz/subghz_extended_freq.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

void subghz_extended_freq() {
bool is_extended_i = false;
bool is_bypassed = false;
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = flipper_format_file_alloc(storage);

if(flipper_format_file_open_existing(file, "/ext/subghz/assets/extend_range.txt")) {
flipper_format_read_bool(file, "use_ext_range_at_own_risk", &is_extended_i, 1);
flipper_format_read_bool(file, "ignore_default_tx_region", &is_bypassed, 1);
}

furi_hal_subghz_set_extended_frequency(is_extended_i);
furi_hal_subghz_set_bypass_region(is_bypassed);

flipper_format_free(file);
furi_record_close(RECORD_STORAGE);
Expand Down
2 changes: 1 addition & 1 deletion applications/settings/about/about.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static DialogMessageButton about_screen_hw_version(DialogsApp* dialogs, DialogMe
furi_hal_version_get_hw_target(),
furi_hal_version_get_hw_body(),
furi_hal_version_get_hw_connect(),
furi_hal_version_get_hw_region_name_otp(),
furi_hal_version_get_hw_region_name(),
furi_hal_region_get_name(),
my_name ? my_name : "Unknown");

Expand Down
1 change: 1 addition & 0 deletions targets/f7/api_symbols.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,7 @@ Function,+,furi_hal_random_init,void,
Function,+,furi_hal_region_get,const FuriHalRegion*,
Function,+,furi_hal_region_get_band,const FuriHalRegionBand*,uint32_t
Function,+,furi_hal_region_get_name,const char*,
Function,-,furi_hal_region_init,void,
Function,+,furi_hal_region_is_frequency_allowed,_Bool,uint32_t
Function,+,furi_hal_region_is_provisioned,_Bool,
Function,+,furi_hal_region_set,void,FuriHalRegion*
Expand Down
1 change: 1 addition & 0 deletions targets/f7/furi_hal/furi_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void furi_hal_init() {
furi_hal_flash_init();
furi_hal_resources_init();
furi_hal_version_init();
furi_hal_region_init();
furi_hal_spi_config_init();
furi_hal_spi_dma_init();
furi_hal_ibutton_init();
Expand Down
122 changes: 105 additions & 17 deletions targets/f7/furi_hal/furi_hal_region.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <furi_hal_region.h>
#include <furi.h>
#include <furi_hal_version.h>
#include <furi_hal_subghz.h>

const FuriHalRegion furi_hal_region_zero = {
.country_code = "00",
Expand All @@ -13,18 +14,118 @@ const FuriHalRegion furi_hal_region_zero = {
},
}};

const FuriHalRegion furi_hal_region_eu_ru = {
.country_code = "EU",
.bands_count = 2,
.bands = {
{
.start = 433050000,
.end = 434790000,
.power_limit = 12,
.duty_cycle = 50,
},
{
.start = 868150000,
.end = 868550000,
.power_limit = 12,
.duty_cycle = 50,
}}};

const FuriHalRegion furi_hal_region_us_ca_au = {
.country_code = "US",
.bands_count = 3,
.bands = {
{
.start = 304100000,
.end = 321950000,
.power_limit = 12,
.duty_cycle = 50,
},
{
.start = 433050000,
.end = 434790000,
.power_limit = 12,
.duty_cycle = 50,
},
{
.start = 915000000,
.end = 928000000,
.power_limit = 12,
.duty_cycle = 50,
}}};

const FuriHalRegion furi_hal_region_jp = {
.country_code = "JP",
.bands_count = 2,
.bands = {
{
.start = 312000000,
.end = 315250000,
.power_limit = 12,
.duty_cycle = 50,
},
{
.start = 920500000,
.end = 923500000,
.power_limit = 12,
.duty_cycle = 50,
}}};

static const FuriHalRegion* furi_hal_region = NULL;

void furi_hal_region_init() {
FuriHalVersionRegion region = furi_hal_version_get_hw_region();

if(region == FuriHalVersionRegionUnknown) {
furi_hal_region = &furi_hal_region_zero;
} else if(region == FuriHalVersionRegionEuRu) {
furi_hal_region = &furi_hal_region_eu_ru;
} else if(region == FuriHalVersionRegionUsCaAu) {
furi_hal_region = &furi_hal_region_us_ca_au;
} else if(region == FuriHalVersionRegionJp) {
furi_hal_region = &furi_hal_region_jp;
}
}

const FuriHalRegion* furi_hal_region_get() {
return &furi_hal_region_zero;
return furi_hal_region;
}

void furi_hal_region_set(FuriHalRegion* region) {
UNUSED(region);
furi_hal_region = region;
}

bool furi_hal_region_is_provisioned() {
return furi_hal_region != NULL;
}

const char* furi_hal_region_get_name() {
if(furi_hal_region) {
return furi_hal_region->country_code;
} else {
return "--";
}
}

bool _furi_hal_region_is_frequency_allowed(uint32_t frequency) {
if(!furi_hal_region) {
return false;
}

const FuriHalRegionBand* band = furi_hal_region_get_band(frequency);
if(!band) {
return false;
}

return true;
}

// Check furi_hal_subghz settings for region bypass, if not it uses function above
bool furi_hal_region_is_frequency_allowed(uint32_t frequency) {
return furi_hal_subghz_is_tx_allowed(frequency);
}

const FuriHalRegionBand* furi_hal_region_get_band(uint32_t frequency) {
furi_hal_region = &furi_hal_region_zero;
if(!furi_hal_region) {
return NULL;
}
Expand All @@ -38,16 +139,3 @@ const FuriHalRegionBand* furi_hal_region_get_band(uint32_t frequency) {

return NULL;
}

bool furi_hal_region_is_frequency_allowed(uint32_t frequency) {
UNUSED(frequency);
return true;
}

bool furi_hal_region_is_provisioned() {
return true;
}

const char* furi_hal_region_get_name() {
return "00";
}
5 changes: 5 additions & 0 deletions targets/f7/furi_hal/furi_hal_region_i.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <stdint.h>

bool _furi_hal_region_is_frequency_allowed(uint32_t frequency);
17 changes: 16 additions & 1 deletion targets/f7/furi_hal/furi_hal_subghz.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <furi_hal_subghz.h>
#include <lib/subghz/devices/cc1101_configs.h>

#include <furi_hal_region_i.h>
#include <furi_hal_version.h>
#include <furi_hal_rtc.h>
#include <furi_hal_spi.h>
Expand Down Expand Up @@ -54,6 +54,7 @@ typedef struct {
int8_t rolling_counter_mult;
bool ext_power_amp : 1;
bool extended_frequency_i : 1;
bool bypass_region : 1;
} FuriHalSubGhz;

volatile FuriHalSubGhz furi_hal_subghz = {
Expand All @@ -63,6 +64,7 @@ volatile FuriHalSubGhz furi_hal_subghz = {
.rolling_counter_mult = 1,
.ext_power_amp = false,
.extended_frequency_i = false,
.bypass_region = false,
};

int8_t furi_hal_subghz_get_rolling_counter_mult(void) {
Expand All @@ -77,6 +79,10 @@ void furi_hal_subghz_set_extended_frequency(bool state_i) {
furi_hal_subghz.extended_frequency_i = state_i;
}

void furi_hal_subghz_set_bypass_region(bool enabled) {
furi_hal_subghz.bypass_region = enabled;
}

void furi_hal_subghz_set_ext_power_amp(bool enabled) {
furi_hal_subghz.ext_power_amp = enabled;
}
Expand Down Expand Up @@ -381,6 +387,15 @@ uint32_t furi_hal_subghz_set_frequency_and_path(uint32_t value) {
}

bool furi_hal_subghz_is_tx_allowed(uint32_t value) {
if(!furi_hal_subghz.bypass_region) {
if(!_furi_hal_region_is_frequency_allowed(value)) {
FURI_LOG_I(TAG, "Frequency blocked - outside region range");
return false;
}

return true;
}

bool allow_extended_for_int = furi_hal_subghz.extended_frequency_i;

if(!(allow_extended_for_int) &&
Expand Down
4 changes: 3 additions & 1 deletion targets/f7/furi_hal/furi_hal_subghz_i.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

void furi_hal_subghz_set_extended_frequency(bool state_i);
void furi_hal_subghz_set_extended_frequency(bool state_i);

void furi_hal_subghz_set_bypass_region(bool enabled);
16 changes: 8 additions & 8 deletions targets/f7/furi_hal/furi_hal_version.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,15 @@ uint8_t furi_hal_version_get_hw_connect() {
}

FuriHalVersionRegion furi_hal_version_get_hw_region() {
return FuriHalVersionRegionUnknown;
return furi_hal_version.board_region;
}

FuriHalVersionRegion furi_hal_version_get_hw_region_otp() {
return furi_hal_version.board_region;
return furi_hal_version_get_hw_region();
}

const char* furi_hal_version_get_hw_region_name() {
return "R00";
}

const char* furi_hal_version_get_hw_region_name_otp() {
switch(furi_hal_version_get_hw_region_otp()) {
switch(furi_hal_version_get_hw_region()) {
case FuriHalVersionRegionUnknown:
return "R00";
case FuriHalVersionRegionEuRu:
Expand All @@ -276,6 +272,10 @@ const char* furi_hal_version_get_hw_region_name_otp() {
return "R??";
}

const char* furi_hal_version_get_hw_region_name_otp() {
return furi_hal_version_get_hw_region_name();
}

FuriHalVersionDisplay furi_hal_version_get_hw_display() {
return furi_hal_version.board_display;
}
Expand Down Expand Up @@ -316,5 +316,5 @@ const uint8_t* furi_hal_version_uid() {
if(version_get_custom_name(NULL) != NULL) {
return (const uint8_t*)&(*((uint32_t*)version_get_custom_name(NULL)));
}
return (const uint8_t*)UID64_BASE;
return furi_hal_version_uid_default();
}
3 changes: 3 additions & 0 deletions targets/furi_hal_include/furi_hal_region.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ typedef struct {
FuriHalRegionBand bands[];
} FuriHalRegion;

/** Initialize region */
void furi_hal_region_init();

/** Get Region Data.
*
* Region data may be allocated in Flash or in RAM.
Expand Down
Loading

0 comments on commit 0111d85

Please sign in to comment.