Skip to content

Commit d44c43b

Browse files
authored
Merge pull request #939 from Dmitry422/dev
Subghz counter editor refactor
2 parents ef9cd22 + 02796c3 commit d44c43b

22 files changed

Lines changed: 297 additions & 292 deletions

applications/examples/example_event_loop/example_event_loop_mutex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ typedef struct {
2626
uint8_t worker_result;
2727
} EventLoopMutexApp;
2828

29-
// This funciton is being run in a separate thread to simulate lenghty blocking operations
29+
// This funciton is being run in a separate thread to simulate lengthy blocking operations
3030
static int32_t event_loop_mutex_app_worker_thread(void* context) {
3131
furi_assert(context);
3232
EventLoopMutexApp* app = context;

applications/main/subghz/scenes/subghz_scene_signal_settings.c

Lines changed: 25 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
#include <lib/toolbox/value_index.h>
55
#include <machine/endian.h>
66
#include <toolbox/strint.h>
7+
#include <lib/subghz/blocks/generic.h>
78

89
#define TAG "SubGhzSceneSignalSettings"
910

1011
static uint32_t counter_mode = 0xff;
11-
static uint32_t loaded_counter32 = 0x0;
1212
static uint32_t counter32 = 0x0;
1313
static uint16_t counter16 = 0x0;
1414
static uint8_t byte_count = 0;
1515
static uint8_t* byte_ptr = NULL;
16-
static uint8_t hex_char_lenght = 0;
1716
static FuriString* byte_input_text;
1817

1918
#define COUNTER_MODE_COUNT 7
@@ -52,55 +51,6 @@ static Protocols protocols[] = {
5251

5352
#define PROTOCOLS_COUNT (sizeof(protocols) / sizeof(Protocols));
5453

55-
// our special case function based on strint_to_uint32 from strint.c
56-
StrintParseError strint_to_uint32_base16(const char* str, uint32_t* out, uint8_t* lenght) {
57-
// skip whitespace
58-
while(((*str >= '\t') && (*str <= '\r')) || *str == ' ') {
59-
str++;
60-
}
61-
62-
// read digits
63-
uint32_t limit = UINT32_MAX;
64-
uint32_t mul_limit = limit / 16;
65-
uint32_t result = 0;
66-
int read_total = 0;
67-
68-
while(*str != 0) {
69-
int digit_value;
70-
if(*str >= '0' && *str <= '9') {
71-
digit_value = *str - '0';
72-
} else if(*str >= 'A' && *str <= 'Z') {
73-
digit_value = *str - 'A' + 10;
74-
} else if(*str >= 'a' && *str <= 'z') {
75-
digit_value = *str - 'a' + 10;
76-
} else {
77-
break;
78-
}
79-
80-
if(digit_value >= 16) {
81-
break;
82-
}
83-
84-
if(result > mul_limit) return StrintParseOverflowError;
85-
result *= 16;
86-
if(result > limit - digit_value) return StrintParseOverflowError; //-V658
87-
result += digit_value;
88-
89-
read_total++;
90-
str++;
91-
}
92-
93-
if(read_total == 0) {
94-
result = 0;
95-
*lenght = 0;
96-
return StrintParseAbsentError;
97-
}
98-
99-
if(out) *out = result;
100-
if(lenght) *lenght = read_total;
101-
return StrintParseNoError;
102-
}
103-
10454
void subghz_scene_signal_settings_counter_mode_changed(VariableItem* item) {
10555
uint8_t index = variable_item_get_current_value_index(item);
10656
variable_item_set_current_value_text(item, counter_mode_text[index]);
@@ -117,8 +67,8 @@ void subghz_scene_signal_settings_variable_item_list_enter_callback(void* contex
11767

11868
// when we click OK on "Edit counter" item
11969
if(index == 1) {
120-
furi_string_cat_printf(byte_input_text, "%i", hex_char_lenght * 4);
121-
furi_string_cat_str(byte_input_text, "-bit counter in HEX");
70+
furi_string_cat_printf(byte_input_text, "%i", subghz_block_generic_global.cnt_length_bit);
71+
furi_string_cat_str(byte_input_text, "-bits counter in HEX");
12272

12373
// Setup byte_input view
12474
ByteInput* byte_input = subghz->byte_input;
@@ -174,93 +124,45 @@ void subghz_scene_signal_settings_on_enter(void* context) {
174124
}
175125
}
176126
}
177-
FURI_LOG_D(TAG, "Current CounterMode value %li", counter_mode);
127+
FURI_LOG_D(TAG, "Loaded CounterMode value %li", counter_mode);
178128

179129
flipper_format_file_close(fff_data_file);
180130
flipper_format_free(fff_data_file);
181131
furi_record_close(RECORD_STORAGE);
182132

183133
// ### Counter edit section ###
184-
FuriString* textCnt = furi_string_alloc_set_str("");
185134
byte_input_text = furi_string_alloc_set_str("Enter ");
186-
furi_string_reset(tmp_text);
187-
188135
bool counter_not_available = true;
189136
SubGhzProtocolDecoderBase* decoder = subghz_txrx_get_decoder(subghz->txrx);
190137

191-
// deserialaze and decode loaded sugbhz file and take information string from decoder
138+
// deserialaze and decode loaded sugbhz file and push data to subghz_block_generic_global variable
192139
if(subghz_protocol_decoder_base_deserialize(decoder, subghz_txrx_get_fff_data(subghz->txrx)) ==
193140
SubGhzProtocolStatusOk) {
194141
subghz_protocol_decoder_base_get_string(decoder, tmp_text);
195142
} else {
196143
FURI_LOG_E(TAG, "Cant deserialize this subghz file");
197144
}
198145

199-
// In protocols output we allways have HEX format for "Cnt:" output (text formating like ...Cnt:%05lX\r\n")
200-
// we take 8 symbols starting from "Cnt:........"
201-
// at first we search "Cnt:????" that means for this protocol counter cannot be decoded
202-
203-
int8_t place = furi_string_search_str(tmp_text, "Cnt:??", 0);
204-
if(place > 0) {
146+
if(!subghz_block_generic_global.cnt_is_available) {
205147
counter_mode = 0xff;
206-
FURI_LOG_D(TAG, "Found Cnt:???? - Counter mode and edit not available for this protocol");
148+
FURI_LOG_D(TAG, "Counter mode and edit not available for this protocol");
207149
} else {
208-
place = furi_string_search_str(tmp_text, "Cnt:", 0);
209-
if(place > 0) {
210-
// defence from memory leaks. Check can we take 8 symbols after 'Cnt:' ?
211-
// if from current place to end of stirngs more than 8 symbols - ok, if not - just take symbols from current place to end of string.
212-
// +4 - its 'Cnt:' lenght
213-
uint8_t n_symbols_taken = 8;
214-
if(sizeof(tmp_text) - (place + 4) < 8) {
215-
n_symbols_taken = sizeof(tmp_text) - (place + 4);
216-
}
217-
furi_string_set_n(textCnt, tmp_text, place + 4, n_symbols_taken);
218-
furi_string_trim(textCnt);
219-
FURI_LOG_D(
220-
TAG,
221-
"Took 8 bytes hex value starting after 'Cnt:' - %s",
222-
furi_string_get_cstr(textCnt));
223-
224-
// trim and convert 8 simbols string to uint32 by base 16 (hex);
225-
// later we use loaded_counter in subghz_scene_signal_settings_on_event to check is there 0 or not - special case
226-
227-
if(strint_to_uint32_base16(
228-
furi_string_get_cstr(textCnt), &loaded_counter32, &hex_char_lenght) ==
229-
StrintParseNoError) {
230-
counter_not_available = false;
231-
232-
// calculate and roundup number of hex bytes to display counter in byte_input (every 2 hex symbols = 1 byte for view)
233-
// later must be used in byte_input to restrict number of available byte to edit
234-
// cnt_byte_count = (hex_char_lenght + 1) / 2;
235-
236-
FURI_LOG_D(
237-
TAG,
238-
"Result of conversion from String to uint_32 DEC %li, HEX %lX, HEX length %i symbols",
239-
loaded_counter32,
240-
loaded_counter32,
241-
hex_char_lenght);
242-
243-
// Check is there byte_count more than 2 hex bytes long (16 bit) or not (32bit)
244-
// To show hex value we must correct revert bytes for ByteInput view
245-
if(hex_char_lenght > 4) {
246-
counter32 = loaded_counter32;
247-
furi_string_printf(tmp_text, "%lX", counter32);
248-
counter32 = __bswap32(counter32);
249-
byte_ptr = (uint8_t*)&counter32;
250-
byte_count = 4;
251-
} else {
252-
counter16 = loaded_counter32;
253-
furi_string_printf(tmp_text, "%X", counter16);
254-
counter16 = __bswap16(counter16);
255-
byte_ptr = (uint8_t*)&counter16;
256-
byte_count = 2;
257-
}
258-
} else {
259-
FURI_LOG_E(TAG, "Cant convert text counter value");
260-
};
261-
150+
counter_not_available = false;
151+
152+
// Check is there byte_count more than 2 hex bytes long or not
153+
// To show hex value we must correct revert bytes for ByteInput view with __bswapХХ
154+
if(subghz_block_generic_global.cnt_length_bit > 16) {
155+
counter32 = subghz_block_generic_global.current_cnt;
156+
furi_string_printf(tmp_text, "%lX", counter32);
157+
counter32 = __bswap32(counter32);
158+
byte_ptr = (uint8_t*)&counter32;
159+
byte_count = 4;
262160
} else {
263-
FURI_LOG_D(TAG, "Counter editor not available for this protocol");
161+
counter16 = subghz_block_generic_global.current_cnt;
162+
furi_string_printf(tmp_text, "%X", counter16);
163+
counter16 = __bswap16(counter16);
164+
byte_ptr = (uint8_t*)&counter16;
165+
byte_count = 2;
264166
}
265167
}
266168

@@ -272,9 +174,6 @@ void subghz_scene_signal_settings_on_enter(void* context) {
272174
int32_t value_index;
273175
VariableItem* item;
274176

275-
// variable_item_list_set_selected_item(subghz->variable_item_list, 0);
276-
// variable_item_list_reset(subghz->variable_item_list);
277-
278177
variable_item_list_set_enter_callback(
279178
variable_item_list,
280179
subghz_scene_signal_settings_variable_item_list_enter_callback,
@@ -298,71 +197,29 @@ void subghz_scene_signal_settings_on_enter(void* context) {
298197
variable_item_set_locked(item, (counter_not_available), "Not available\nfor this\nprotocol !");
299198

300199
furi_string_free(tmp_text);
301-
furi_string_free(textCnt);
302200

303201
view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdVariableItemList);
304202
}
305203

306204
bool subghz_scene_signal_settings_on_event(void* context, SceneManagerEvent event) {
307205
SubGhz* subghz = context;
308-
int32_t tmp_counter = 0;
309206

310207
if(event.type == SceneManagerEventTypeCustom) {
311208
if(event.event == SubGhzCustomEventByteInputDone) {
312209
switch(byte_count) {
313210
case 2:
314-
// when signal has Cnt:00 we can step only to 0000+FFFF = FFFF, but we need 0000 for next step
315-
// for this case we must use +1 additional step to increace Cnt from FFFF to 0000.
316-
317-
// save current user defined counter increase value (mult)
318-
tmp_counter = furi_hal_subghz_get_rolling_counter_mult();
319-
320-
// increase signal counter to max value - at result it must be 0000 in most cases
321-
// but can be FFFF in case Cnt:0000 (for this we have +1 additional step below)
322-
furi_hal_subghz_set_rolling_counter_mult(0xFFFF);
323-
subghz_tx_start(subghz, subghz_txrx_get_fff_data(subghz->txrx));
324-
subghz_txrx_stop(subghz->txrx);
325-
326-
// if file Cnt:00 then do +1 additional step
327-
if(loaded_counter32 == 0) {
328-
furi_hal_subghz_set_rolling_counter_mult(1);
329-
subghz_tx_start(subghz, subghz_txrx_get_fff_data(subghz->txrx));
330-
subghz_txrx_stop(subghz->txrx);
331-
}
332-
333-
// at this point we must have signal Cnt:00
334-
// convert back after byte_input and do one send with our new mult (counter16) - at end we must have signal Cnt = counter16
211+
// set new cnt value and override_flag to global variable and call transmit to generate and save subghz signal
335212
counter16 = __bswap16(counter16);
336-
337-
furi_hal_subghz_set_rolling_counter_mult(counter16);
213+
subghz_block_generic_global_counter_override_set(counter16);
338214
subghz_tx_start(subghz, subghz_txrx_get_fff_data(subghz->txrx));
339215
subghz_txrx_stop(subghz->txrx);
340-
341-
// restore user definded counter increase value (mult)
342-
furi_hal_subghz_set_rolling_counter_mult(tmp_counter);
343-
344216
break;
345217
case 4:
346218
// the same for 32 bit Counter
347-
tmp_counter = furi_hal_subghz_get_rolling_counter_mult();
348-
349-
furi_hal_subghz_set_rolling_counter_mult(0xFFFFFFF);
350-
subghz_tx_start(subghz, subghz_txrx_get_fff_data(subghz->txrx));
351-
subghz_txrx_stop(subghz->txrx);
352-
353-
if(loaded_counter32 == 0) {
354-
furi_hal_subghz_set_rolling_counter_mult(1);
355-
subghz_tx_start(subghz, subghz_txrx_get_fff_data(subghz->txrx));
356-
subghz_txrx_stop(subghz->txrx);
357-
}
358-
359219
counter32 = __bswap32(counter32);
360-
361-
furi_hal_subghz_set_rolling_counter_mult((counter32 & 0xFFFFFFF));
220+
subghz_block_generic_global_counter_override_set(counter32);
362221
subghz_tx_start(subghz, subghz_txrx_get_fff_data(subghz->txrx));
363222
subghz_txrx_stop(subghz->txrx);
364-
365-
furi_hal_subghz_set_rolling_counter_mult(tmp_counter);
366223
break;
367224
default:
368225
break;

lib/mjs/mjs_exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ MJS_PRIVATE mjs_err_t mjs_execute(struct mjs* mjs, size_t off, mjs_val_t* res) {
10631063

10641064
mjs_gen_stack_trace(mjs, bp.start_idx + i - 1 /* undo the i++ */);
10651065

1066-
/* restore stack lenghts */
1066+
/* restore stack lengths */
10671067
mjs->stack.len = stack_len;
10681068
mjs->call_stack.len = call_stack_len;
10691069
mjs->arg_stack.len = arg_stack_len;

lib/subghz/blocks/generic.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,35 @@
44

55
#define TAG "SubGhzBlockGeneric"
66

7+
// Main things: subghz protocols working (serialize, deserialize, decode and encode)
8+
// with flipper_format data isolated from upper level subghz functions and structures.
9+
// So if we need change something inside of protocol data - we need use this API from protocols to get and set data
10+
11+
SubGhzBlockGenericGlobal subghz_block_generic_global; //global structure for subghz
12+
13+
void subghz_block_generic_global_counter_override_set(uint32_t counter) {
14+
subghz_block_generic_global.new_cnt = counter; // set global variable
15+
subghz_block_generic_global.cnt_need_override = true; // set flag for protocols
16+
}
17+
18+
bool subghz_block_generic_global_counter_override_get(uint32_t* counter) {
19+
// if override flag was enabled then return succes TRUE and return overrided counter, else return success = FALSE
20+
// we cut counter bit length to available protocol bits length by the logical AND function
21+
if(subghz_block_generic_global.cnt_need_override) {
22+
*counter = subghz_block_generic_global.new_cnt &
23+
((0xFFFFFFFF >> (32 - subghz_block_generic_global.cnt_length_bit)));
24+
subghz_block_generic_global.cnt_need_override = false;
25+
return true;
26+
} else {
27+
return false;
28+
}
29+
}
30+
31+
void subghz_block_generic_global_reset(void* p) {
32+
UNUSED(p);
33+
memset(&subghz_block_generic_global, 0, sizeof(subghz_block_generic_global));
34+
}
35+
736
void subghz_block_generic_get_preset_name(const char* preset_name, FuriString* preset_str) {
837
const char* preset_name_temp;
938
if(!strcmp(preset_name, "AM270")) {

lib/subghz/blocks/generic.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,36 @@ struct SubGhzBlockGeneric {
2727
uint32_t seed;
2828
};
2929

30+
typedef struct SubGhzBlockGenericGlobal SubGhzBlockGenericGlobal;
31+
32+
struct SubGhzBlockGenericGlobal {
33+
uint32_t current_cnt; // global counter value;
34+
uint32_t new_cnt; // global counter value;
35+
bool cnt_need_override; // flag for protocols to override signals counter inside of protocols
36+
uint8_t cnt_length_bit; // counter length in bytes (used in counter editor giu)
37+
bool cnt_is_available; // is there counter available for protocol (used in counter editor giu)
38+
};
39+
40+
extern SubGhzBlockGenericGlobal subghz_block_generic_global; //global structure for subghz
41+
42+
/**
43+
* Setup SubGhzBlockGenericGlobal.cnt and cnt_need_override flag to be used in protocols;
44+
* @param counter new counter value;
45+
*/
46+
void subghz_block_generic_global_counter_override_set(uint32_t counter);
47+
48+
/**
49+
* Return true if incomming variable was overrided by SubGhzBlockGenericGlobal.cnt
50+
* else return false and not change incomming variable
51+
* @param counter pointer to counter variable that must be changed
52+
*/
53+
bool subghz_block_generic_global_counter_override_get(uint32_t* counter);
54+
55+
/**
56+
* Reset subghz_block_generic global structure;
57+
*/
58+
void subghz_block_generic_global_reset(void* p);
59+
3060
/**
3161
* Get name preset.
3262
* @param preset_name name preset

0 commit comments

Comments
 (0)