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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
#include "subghz_i.h"
#include <math.h>
#include <furi.h>
#include <furi-hal.h>
#include <input/input.h>
#include <gui/elements.h>
#include <notification/notification-messages.h>
#include "file-worker.h"
#include "../notification/notification.h"
#include "views/subghz_receiver.h"
void subghz_begin(SubGhz* subghz, FuriHalSubGhzPreset preset) {
furi_assert(subghz);
furi_hal_subghz_reset();
furi_hal_subghz_idle();
furi_hal_subghz_load_preset(preset);
hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
subghz->txrx->txrx_state = SubGhzTxRxStateIdle;
}
uint32_t subghz_rx(SubGhz* subghz, uint32_t frequency) {
furi_assert(subghz);
if(!furi_hal_subghz_is_frequency_valid(frequency)) {
furi_crash(NULL);
}
furi_assert(
subghz->txrx->txrx_state != SubGhzTxRxStateRx &&
subghz->txrx->txrx_state != SubGhzTxRxStateSleep);
furi_hal_subghz_idle();
uint32_t value = furi_hal_subghz_set_frequency_and_path(frequency);
hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
furi_hal_subghz_flush_rx();
furi_hal_subghz_rx();
furi_hal_subghz_start_async_rx(subghz_worker_rx_callback, subghz->txrx->worker);
subghz_worker_start(subghz->txrx->worker);
subghz->txrx->txrx_state = SubGhzTxRxStateRx;
return value;
}
uint32_t subghz_tx(SubGhz* subghz, uint32_t frequency) {
furi_assert(subghz);
if(!furi_hal_subghz_is_frequency_valid(frequency)) {
furi_crash(NULL);
}
furi_assert(subghz->txrx->txrx_state != SubGhzTxRxStateSleep);
furi_hal_subghz_idle();
uint32_t value = furi_hal_subghz_set_frequency_and_path(frequency);
hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
hal_gpio_write(&gpio_cc1101_g0, true);
furi_hal_subghz_tx();
subghz->txrx->txrx_state = SubGhzTxRxStateTx;
return value;
}
void subghz_idle(SubGhz* subghz) {
furi_assert(subghz);
furi_assert(subghz->txrx->txrx_state != SubGhzTxRxStateSleep);
furi_hal_subghz_idle();
subghz->txrx->txrx_state = SubGhzTxRxStateIdle;
}
void subghz_rx_end(SubGhz* subghz) {
furi_assert(subghz);
furi_assert(subghz->txrx->txrx_state == SubGhzTxRxStateRx);
if(subghz_worker_is_running(subghz->txrx->worker)) {
subghz_worker_stop(subghz->txrx->worker);
furi_hal_subghz_stop_async_rx();
}
furi_hal_subghz_idle();
subghz->txrx->txrx_state = SubGhzTxRxStateIdle;
}
void subghz_sleep(SubGhz* subghz) {
furi_assert(subghz);
furi_hal_subghz_sleep();
subghz->txrx->txrx_state = SubGhzTxRxStateSleep;
}
static void subghz_frequency_preset_to_str(SubGhz* subghz, string_t output) {
furi_assert(subghz);
string_cat_printf(
output,
"Frequency: %d\n"
"Preset: %d\n",
(int)subghz->txrx->frequency,
(int)subghz->txrx->preset);
}
void subghz_tx_start(SubGhz* subghz) {
furi_assert(subghz);
subghz->txrx->encoder = subghz_protocol_encoder_common_alloc();
subghz->txrx->encoder->repeat = 200; //max repeat with the button held down
//get upload
if(subghz->txrx->protocol_result->get_upload_protocol) {
if(subghz->txrx->protocol_result->get_upload_protocol(
subghz->txrx->protocol_result, subghz->txrx->encoder)) {
if(subghz->txrx->preset) {
subghz_begin(subghz, subghz->txrx->preset);
} else {
subghz_begin(subghz, FuriHalSubGhzPresetOok270Async);
}
if(subghz->txrx->frequency) {
subghz_tx(subghz, subghz->txrx->frequency);
} else {
subghz_tx(subghz, 433920000);
}
//Start TX
furi_hal_subghz_start_async_tx(
subghz_protocol_encoder_common_yield, subghz->txrx->encoder);
}
}
}
void subghz_tx_stop(SubGhz* subghz) {
furi_assert(subghz);
furi_assert(subghz->txrx->txrx_state == SubGhzTxRxStateTx);
//Stop TX
furi_hal_subghz_stop_async_tx();
subghz_protocol_encoder_common_free(subghz->txrx->encoder);
subghz_idle(subghz);
//if protocol dynamic then we save the last upload
if(subghz->txrx->protocol_result->type_protocol == SubGhzProtocolCommonTypeDynamic) {
subghz_save_protocol_to_file(subghz, subghz->text_store);
}
notification_message(subghz->notifications, &sequence_reset_red);
}
bool subghz_key_load(SubGhz* subghz, const char* file_path) {
furi_assert(subghz);
furi_assert(file_path);
FileWorker* file_worker = file_worker_alloc(false);
// Load device data
bool loaded = false;
string_t path;
string_init_set_str(path, file_path);
string_t temp_str;
string_init(temp_str);
int res = 0;
int data = 0;
do {
if(!file_worker_open(file_worker, string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
break;
}
// Read and parse frequency from 1st line
if(!file_worker_read_until(file_worker, temp_str, '\n')) {
break;
}
res = sscanf(string_get_cstr(temp_str), "Frequency: %d\n", &data);
if(res != 1) {
break;
}
subghz->txrx->frequency = (uint32_t)data;
// Read and parse preset from 2st line
if(!file_worker_read_until(file_worker, temp_str, '\n')) {
break;
}
res = sscanf(string_get_cstr(temp_str), "Preset: %d\n", &data);
if(res != 1) {
break;
}
subghz->txrx->preset = (FuriHalSubGhzPreset)data;
// Read and parse name protocol from 2st line
if(!file_worker_read_until(file_worker, temp_str, '\n')) {
break;
}
// strlen("Protocol: ") = 10
string_right(temp_str, 10);
subghz->txrx->protocol_result =
subghz_parser_get_by_name(subghz->txrx->parser, string_get_cstr(temp_str));
if(subghz->txrx->protocol_result == NULL) {
break;
}
if(!subghz->txrx->protocol_result->to_load_protocol_from_file(
file_worker, subghz->txrx->protocol_result)) {
break;
}
loaded = true;
} while(0);
if(!loaded) {
file_worker_show_error(file_worker, "Cannot parse\nfile");
}
string_clear(temp_str);
string_clear(path);
file_worker_close(file_worker);
file_worker_free(file_worker);
return loaded;
}
bool subghz_save_protocol_to_file(SubGhz* subghz, const char* dev_name) {
furi_assert(subghz);
furi_assert(subghz->txrx->protocol_result);
FileWorker* file_worker = file_worker_alloc(false);
string_t dev_file_name;
string_init(dev_file_name);
string_t temp_str;
string_init(temp_str);
bool saved = false;
do {
// Create subghz folder directory if necessary
if(!file_worker_mkdir(file_worker, SUBGHZ_APP_FOLDER)) {
break;
}
// Create saved directory if necessary
if(!file_worker_mkdir(file_worker, SUBGHZ_APP_PATH_FOLDER)) {
break;
}
// First remove subghz device file if it was saved
string_printf(
dev_file_name, "%s/%s%s", SUBGHZ_APP_PATH_FOLDER, dev_name, SUBGHZ_APP_EXTENSION);
if(!file_worker_remove(file_worker, string_get_cstr(dev_file_name))) {
break;
}
// Open file
if(!file_worker_open(
file_worker, string_get_cstr(dev_file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
break;
}
//Get string frequency preset protocol
subghz_frequency_preset_to_str(subghz, temp_str);
if(!file_worker_write(file_worker, string_get_cstr(temp_str), string_size(temp_str))) {
break;
}
//Get string save
subghz->txrx->protocol_result->to_save_string(subghz->txrx->protocol_result, temp_str);
// Prepare and write data to file
if(!file_worker_write(file_worker, string_get_cstr(temp_str), string_size(temp_str))) {
break;
}
saved = true;
} while(0);
string_clear(temp_str);
string_clear(dev_file_name);
file_worker_close(file_worker);
file_worker_free(file_worker);
return saved;
}
bool subghz_load_protocol_from_file(SubGhz* subghz) {
furi_assert(subghz);
FileWorker* file_worker = file_worker_alloc(false);
string_t protocol_file_name;
string_init(protocol_file_name);
string_t temp_str;
string_init(temp_str);
int sscanf_res = 0;
int data = 0;
// Input events and views are managed by file_select
bool res = file_worker_file_select(
file_worker,
SUBGHZ_APP_PATH_FOLDER,
SUBGHZ_APP_EXTENSION,
subghz->text_store,
sizeof(subghz->text_store),
NULL);
if(res) {
// Get key file path
string_printf(
protocol_file_name,
"%s/%s%s",
SUBGHZ_APP_PATH_FOLDER,
subghz->text_store,
SUBGHZ_APP_EXTENSION);
} else {
string_clear(temp_str);
string_clear(protocol_file_name);
file_worker_close(file_worker);
file_worker_free(file_worker);
return res;
}
res = false;
do {
if(!file_worker_open(
file_worker, string_get_cstr(protocol_file_name), FSAM_READ, FSOM_OPEN_EXISTING)) {
break;
}
// Read and parse frequency from 1st line
if(!file_worker_read_until(file_worker, temp_str, '\n')) {
break;
}
sscanf_res = sscanf(string_get_cstr(temp_str), "Frequency: %d\n", &data);
if(sscanf_res != 1) {
break;
}
subghz->txrx->frequency = (uint32_t)data;
// Read and parse preset from 2st line
if(!file_worker_read_until(file_worker, temp_str, '\n')) {
break;
}
sscanf_res = sscanf(string_get_cstr(temp_str), "Preset: %d\n", &data);
if(sscanf_res != 1) {
break;
}
subghz->txrx->preset = (FuriHalSubGhzPreset)data;
// Read and parse name protocol from 3st line
if(!file_worker_read_until(file_worker, temp_str, '\n')) {
break;
}
// strlen("Protocol: ") = 10
string_right(temp_str, 10);
subghz->txrx->protocol_result =
subghz_parser_get_by_name(subghz->txrx->parser, string_get_cstr(temp_str));
if(subghz->txrx->protocol_result == NULL) {
break;
}
if(!subghz->txrx->protocol_result->to_load_protocol_from_file(
file_worker, subghz->txrx->protocol_result)) {
break;
}
res = true;
} while(0);
if(!res) {
file_worker_show_error(file_worker, "Cannot parse\nfile");
}
string_clear(temp_str);
string_clear(protocol_file_name);
file_worker_close(file_worker);
file_worker_free(file_worker);
return res;
}
uint32_t subghz_random_serial(void) {
static bool rand_generator_inited = false;
if(!rand_generator_inited) {
srand(DWT->CYCCNT);
rand_generator_inited = true;
}
return (uint32_t)rand();
}
void subghz_hopper_update(SubGhz* subghz) {
furi_assert(subghz);
switch(subghz->txrx->hopper_state) {
case SubGhzHopperStateOFF:
return;
break;
case SubGhzHopperStatePause:
return;
break;
case SubGhzHopperStateRSSITimeOut:
if(subghz->txrx->hopper_timeout != 0) {
subghz->txrx->hopper_timeout--;
return;
}
break;
default:
break;
}
float rssi = -127.0f;
if(subghz->txrx->hopper_state != SubGhzHopperStateRSSITimeOut) {
// See RSSI Calculation timings in CC1101 17.3 RSSI
rssi = furi_hal_subghz_get_rssi();
// Stay if RSSI is high enough
if(rssi > -90.0f) {
subghz->txrx->hopper_timeout = 10;
subghz->txrx->hopper_state = SubGhzHopperStateRSSITimeOut;
return;
}
} else {
subghz->txrx->hopper_state = SubGhzHopperStateRunnig;
}
// Select next frequency
if(subghz->txrx->hopper_idx_frequency < subghz_hopper_frequencies_count - 1) {
subghz->txrx->hopper_idx_frequency++;
} else {
subghz->txrx->hopper_idx_frequency = 0;
}
if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
subghz_rx_end(subghz);
};
if(subghz->txrx->txrx_state == SubGhzTxRxStateIdle) {
subghz_parser_reset(subghz->txrx->parser);
subghz->txrx->frequency = subghz_hopper_frequencies[subghz->txrx->hopper_idx_frequency];
subghz_rx(subghz, subghz->txrx->frequency);
}
}
|