-
Notifications
You must be signed in to change notification settings - Fork 0
/
extend_range.c
237 lines (200 loc) · 6.78 KB
/
extend_range.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
This application let's you unlock the extended range of the Sub-GHz module.
Use at your own risk!
@author: maede97
*/
#include <furi.h>
#include <gui/gui.h>
#include <input/input.h>
#include <flipper_format/flipper_format.h>
#define PATH_SUBGHZ EXT_PATH("subghz") "/assets/extend_range.txt"
#define KEY_EXTEND_RANGE "use_ext_range_at_own_risk"
#define KEY_IGNORE_DEFAULT "ignore_default_tx_region"
struct UserSelection {
bool extend_range;
bool ignore_default;
unsigned int current_selection; // 0 - extend_range, 1 - ignore_default
};
struct UserSelection global_user_selection = {
.extend_range = false,
.ignore_default = false,
.current_selection = 0,
};
int global_ret_val = -1;
static void extend_range_close_file(FlipperFormat* file) {
if(file == NULL) {
furi_record_close(RECORD_STORAGE);
return;
}
flipper_format_file_close(file);
flipper_format_free(file);
furi_record_close(RECORD_STORAGE);
}
static FlipperFormat* extend_range_open_file() {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = flipper_format_file_alloc(storage);
if(storage_common_stat(storage, PATH_SUBGHZ, NULL) == FSE_OK) {
if(!flipper_format_file_open_existing(file, PATH_SUBGHZ)) {
extend_range_close_file(file);
return NULL;
}
} else {
return NULL;
}
return file;
}
void extend_range_draw_callback(Canvas* canvas, void* ctx) {
UNUSED(ctx);
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 2, 10, "Extend Range Sub-GHz");
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 2, 20, "Back -> Exit, OK -> Apply");
// Extend Range
if(global_user_selection.extend_range)
canvas_draw_str(canvas, 2, 30, "< Risky Range: Yes >");
else
canvas_draw_str(canvas, 2, 30, "< Risky Range: No >");
if(global_user_selection.current_selection == 0) {
canvas_draw_line(canvas, 2, 31, 125, 31);
}
// Ignore Default
if(global_user_selection.ignore_default)
canvas_draw_str(canvas, 2, 40, "< Ignore Region: Yes >");
else
canvas_draw_str(canvas, 2, 40, "< Ignore Region: No >");
if(global_user_selection.current_selection == 1) {
canvas_draw_line(canvas, 2, 41, 125, 41);
}
// Current status
canvas_draw_str(canvas, 2, 50, "Current Status: ");
switch(global_ret_val) {
case -1:
canvas_draw_str(canvas, 2, 60, "Make your selection!");
break;
case 0:
canvas_draw_str(canvas, 2, 60, "Success. Reboot now.");
break;
case 1:
canvas_draw_str(canvas, 2, 60, "File not found");
break;
case 2:
canvas_draw_str(canvas, 2, 60, "Failed to update 'risky ranges'");
break;
case 3:
canvas_draw_str(canvas, 2, 60, "Failed to update 'ignore region'");
break;
default:
canvas_draw_str(canvas, 2, 60, "Unknown error");
break;
}
}
void extend_range_input_callback(InputEvent* input_event, void* ctx) {
furi_assert(ctx);
FuriMessageQueue* event_queue = ctx;
furi_message_queue_put(event_queue, input_event, FuriWaitForever);
}
void extend_range_read_defaults() {
FlipperFormat* file = extend_range_open_file();
if(file == NULL) {
return;
}
FuriString* extend_range_;
FuriString* ignore_default_;
extend_range_ = furi_string_alloc();
ignore_default_ = furi_string_alloc();
if(!flipper_format_read_string(file, KEY_EXTEND_RANGE, extend_range_)) {
extend_range_close_file(file);
return;
}
if(!flipper_format_read_string(file, KEY_IGNORE_DEFAULT, ignore_default_)) {
extend_range_close_file(file);
return;
}
if(furi_string_cmp_str(extend_range_, "true") == 0) {
global_user_selection.extend_range = true;
} else {
global_user_selection.extend_range = false;
}
if(furi_string_cmp_str(ignore_default_, "true") == 0) {
global_user_selection.ignore_default = true;
} else {
global_user_selection.ignore_default = false;
}
extend_range_close_file(file);
}
int apply_user_selection() {
FlipperFormat* file = extend_range_open_file();
if(file == NULL) {
return 1;
}
if(!flipper_format_update_string_cstr(
file, KEY_EXTEND_RANGE, global_user_selection.extend_range ? "true" : "false")) {
extend_range_close_file(file);
return 2;
}
if(!flipper_format_update_string_cstr(
file, KEY_IGNORE_DEFAULT, global_user_selection.ignore_default ? "true" : "false")) {
extend_range_close_file(file);
return 3;
}
extend_range_close_file(file);
return 0;
}
void handle_key(InputEvent* input_event) {
if(input_event->type != InputTypePress) {
return;
}
switch(input_event->key) {
case InputKeyBack:
return;
case InputKeyOk:
global_ret_val = apply_user_selection();
return;
case InputKeyUp:
global_user_selection.current_selection =
(global_user_selection.current_selection + 1) % 2;
return;
case InputKeyDown:
global_user_selection.current_selection =
(global_user_selection.current_selection + 1) % 2;
return;
case InputKeyLeft:
if(global_user_selection.current_selection == 0) {
global_user_selection.extend_range = !global_user_selection.extend_range;
} else {
global_user_selection.ignore_default = !global_user_selection.ignore_default;
}
return;
case InputKeyRight:
if(global_user_selection.current_selection == 0) {
global_user_selection.extend_range = !global_user_selection.extend_range;
} else {
global_user_selection.ignore_default = !global_user_selection.ignore_default;
}
return;
default:
return;
}
}
int32_t extend_range_app(void* p) {
UNUSED(p);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
extend_range_read_defaults();
// Configure view port & callbacks
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, extend_range_draw_callback, NULL);
view_port_input_callback_set(view_port, extend_range_input_callback, event_queue);
Gui* gui = furi_record_open(RECORD_GUI);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
InputEvent event;
while(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk) {
if(event.type == InputTypeShort && event.key == InputKeyBack) break;
handle_key(&event);
}
gui_remove_view_port(gui, view_port);
view_port_free(view_port);
furi_message_queue_free(event_queue);
furi_record_close(RECORD_GUI);
return 0;
}