8
8
#include "../blocks/generic.h"
9
9
#include "../blocks/math.h"
10
10
11
+ #include <furi.h>
11
12
#include <flipper_format/flipper_format_i.h>
12
13
#include <lib/toolbox/stream/stream.h>
14
+ #include <stm32wbxx_ll_rtc.h>
13
15
14
16
#define TAG "SubGhzProtocolRAW"
15
17
#define SUBGHZ_DOWNLOAD_MAX_SIZE 512
18
+ #define SUBGHZ_AUTO_DETECT_RAW_THRESHOLD -72.0f
16
19
17
20
static const SubGhzBlockConst subghz_protocol_raw_const = {
18
21
.te_short = 50 ,
@@ -24,6 +27,8 @@ static const SubGhzBlockConst subghz_protocol_raw_const = {
24
27
struct SubGhzProtocolDecoderRAW {
25
28
SubGhzProtocolDecoderBase base ;
26
29
30
+ SubGhzBlockDecoder decoder ;
31
+
27
32
int32_t * upload_raw ;
28
33
uint16_t ind_write ;
29
34
Storage * storage ;
@@ -32,6 +37,8 @@ struct SubGhzProtocolDecoderRAW {
32
37
string_t file_name ;
33
38
size_t sample_write ;
34
39
bool last_level ;
40
+ bool auto_mode ;
41
+ bool has_rssi_above_threshold ;
35
42
};
36
43
37
44
struct SubGhzProtocolEncoderRAW {
@@ -55,8 +62,8 @@ const SubGhzProtocolDecoder subghz_protocol_raw_decoder = {
55
62
.feed = subghz_protocol_decoder_raw_feed ,
56
63
.reset = subghz_protocol_decoder_raw_reset ,
57
64
58
- .get_hash_data = NULL ,
59
- .serialize = NULL ,
65
+ .get_hash_data = subghz_protocol_decoder_raw_get_hash_data ,
66
+ .serialize = subghz_protocol_decoder_raw_serialize ,
60
67
.get_string = subghz_protocol_decoder_raw_get_string ,
61
68
};
62
69
@@ -187,6 +194,25 @@ void subghz_protocol_raw_save_to_file_stop(SubGhzProtocolDecoderRAW* instance) {
187
194
instance -> file_is_open = RAWFileIsOpenClose ;
188
195
}
189
196
197
+ void subghz_protocol_decoder_raw_set_auto_mode (void * context , bool auto_mode ) {
198
+ furi_assert (context );
199
+ SubGhzProtocolDecoderRAW * instance = context ;
200
+ instance -> auto_mode = auto_mode ;
201
+
202
+ if (auto_mode ) {
203
+ if (instance -> upload_raw == NULL ) {
204
+ instance -> upload_raw = malloc (SUBGHZ_DOWNLOAD_MAX_SIZE * sizeof (int32_t ));
205
+ }
206
+ } else {
207
+ if (instance -> upload_raw != NULL ) {
208
+ free (instance -> upload_raw );
209
+ instance -> upload_raw = NULL ;
210
+ }
211
+ }
212
+
213
+ subghz_protocol_decoder_raw_reset (context );
214
+ }
215
+
190
216
size_t subghz_protocol_raw_get_sample_write (SubGhzProtocolDecoderRAW * instance ) {
191
217
return instance -> sample_write + instance -> ind_write ;
192
218
}
@@ -208,39 +234,84 @@ void subghz_protocol_decoder_raw_free(void* context) {
208
234
furi_assert (context );
209
235
SubGhzProtocolDecoderRAW * instance = context ;
210
236
string_clear (instance -> file_name );
237
+ if (instance -> upload_raw != NULL ) {
238
+ free (instance -> upload_raw );
239
+ instance -> upload_raw = NULL ;
240
+ }
211
241
free (instance );
212
242
}
213
243
214
244
void subghz_protocol_decoder_raw_reset (void * context ) {
215
245
furi_assert (context );
216
246
SubGhzProtocolDecoderRAW * instance = context ;
217
247
instance -> ind_write = 0 ;
248
+ instance -> has_rssi_above_threshold = false;
218
249
instance -> last_level = false;
219
250
}
220
251
252
+ void subghz_protocol_decoder_raw_write_data (void * context , bool level , uint32_t duration ) {
253
+ furi_assert (context );
254
+ SubGhzProtocolDecoderRAW * instance = context ;
255
+
256
+ if (instance -> last_level != level ) {
257
+ instance -> last_level = (level ? true : false);
258
+ instance -> upload_raw [instance -> ind_write ++ ] = (level ? duration : - duration );
259
+ subghz_protocol_blocks_add_bit (& instance -> decoder , (level ) ? 1 : 0 );
260
+ }
261
+
262
+ if (instance -> ind_write == SUBGHZ_DOWNLOAD_MAX_SIZE ) {
263
+ if (instance -> base .callback )
264
+ instance -> base .callback (& instance -> base , instance -> base .context );
265
+ }
266
+ }
267
+
221
268
void subghz_protocol_decoder_raw_feed (void * context , bool level , uint32_t duration ) {
222
269
furi_assert (context );
223
270
SubGhzProtocolDecoderRAW * instance = context ;
224
271
225
272
if (instance -> upload_raw != NULL ) {
226
- if (duration > subghz_protocol_raw_const .te_short ) {
227
- if (instance -> last_level != level ) {
228
- instance -> last_level = (level ? true : false);
229
- instance -> upload_raw [instance -> ind_write ++ ] = (level ? duration : - duration );
273
+ if (instance -> auto_mode ) {
274
+ float rssi = furi_hal_subghz_get_rssi ();
275
+ if (rssi >= SUBGHZ_AUTO_DETECT_RAW_THRESHOLD && duration >= subghz_protocol_raw_const .te_short ) {
276
+ subghz_protocol_decoder_raw_write_data (context , level , duration );
277
+ instance -> has_rssi_above_threshold = true;
278
+ } else if (instance -> has_rssi_above_threshold ) {
279
+ subghz_protocol_decoder_raw_write_data (instance , level , duration );
280
+
281
+ if ((!level ) && (DURATION_DIFF (duration , subghz_protocol_raw_const .te_long )) >
282
+ subghz_protocol_raw_const .te_long * 7 ) {
283
+ if (instance -> base .callback )
284
+ instance -> base .callback (& instance -> base , instance -> base .context );
285
+ }
286
+ }
287
+ } else {
288
+ if (duration > subghz_protocol_raw_const .te_short ) {
289
+ if (instance -> last_level != level ) {
290
+ instance -> last_level = (level ? true : false);
291
+ instance -> upload_raw [instance -> ind_write ++ ] = (level ? duration : - duration );
292
+ subghz_protocol_blocks_add_bit (& instance -> decoder , (level ) ? 1 : 0 );
293
+ }
230
294
}
231
- }
232
295
233
- if (instance -> ind_write == SUBGHZ_DOWNLOAD_MAX_SIZE ) {
234
- subghz_protocol_raw_save_to_file_write (instance );
296
+ if (instance -> ind_write == SUBGHZ_DOWNLOAD_MAX_SIZE ) {
297
+ subghz_protocol_raw_save_to_file_stop (instance );
298
+ }
235
299
}
236
300
}
237
301
}
238
302
303
+ uint8_t subghz_protocol_decoder_raw_get_hash_data (void * context ) {
304
+ furi_assert (context );
305
+ SubGhzProtocolDecoderRAW * instance = context ;
306
+ return subghz_protocol_blocks_get_hash_data (
307
+ & instance -> decoder , (instance -> decoder .decode_count_bit / 8 ) + 1 );
308
+ }
309
+
239
310
void subghz_protocol_decoder_raw_get_string (void * context , string_t output ) {
240
311
furi_assert (context );
241
312
//SubGhzProtocolDecoderRAW* instance = context;
242
313
//ToDo no use
243
- string_cat_printf (output , "RAW Date " );
314
+ string_cat_printf (output , "RAW Data " );
244
315
}
245
316
246
317
void * subghz_protocol_encoder_raw_alloc (SubGhzEnvironment * environment ) {
@@ -310,6 +381,58 @@ void subghz_protocol_raw_gen_fff_data(FlipperFormat* flipper_format, const char*
310
381
} while (false);
311
382
}
312
383
384
+ bool subghz_protocol_decoder_raw_serialize (
385
+ void * context ,
386
+ FlipperFormat * flipper_format ,
387
+ uint32_t frequency ,
388
+ FuriHalSubGhzPreset preset ) {
389
+ furi_assert (context );
390
+ SubGhzProtocolDecoderRAW * instance = context ;
391
+ if (instance -> auto_mode ) {
392
+ furi_assert (instance );
393
+ bool res = false;
394
+ string_t temp_str ;
395
+ string_init (temp_str );
396
+
397
+ do {
398
+ stream_clean (flipper_format_get_raw_stream (flipper_format ));
399
+ if (!flipper_format_write_header_cstr (
400
+ flipper_format , SUBGHZ_KEY_FILE_TYPE , SUBGHZ_KEY_FILE_VERSION )) {
401
+ FURI_LOG_E (TAG , "Unable to add header" );
402
+ break ;
403
+ }
404
+
405
+ if (!flipper_format_write_uint32 (flipper_format , "Frequency" , & frequency , 1 )) {
406
+ FURI_LOG_E (TAG , "Unable to add Frequency" );
407
+ break ;
408
+ }
409
+ if (!subghz_block_generic_get_preset_name (preset , temp_str )) {
410
+ break ;
411
+ }
412
+ if (!flipper_format_write_string_cstr (flipper_format , "Preset" , string_get_cstr (temp_str ))) {
413
+ FURI_LOG_E (TAG , "Unable to add Preset" );
414
+ break ;
415
+ }
416
+ if (!flipper_format_write_string_cstr (flipper_format , "Protocol" , instance -> base .protocol -> name )) {
417
+ FURI_LOG_E (TAG , "Unable to add Protocol" );
418
+ break ;
419
+ }
420
+
421
+ if (!flipper_format_write_int32 (flipper_format , "RAW_Data" , instance -> upload_raw , instance -> ind_write )) {
422
+ FURI_LOG_E (TAG , "Unable to add Raw Data" );
423
+ break ;
424
+ } else {
425
+ instance -> ind_write = 0 ;
426
+ }
427
+ res = true;
428
+ } while (false);
429
+ string_clear (temp_str );
430
+ return res ;
431
+ } else {
432
+ return false;
433
+ }
434
+ }
435
+
313
436
bool subghz_protocol_encoder_raw_deserialize (void * context , FlipperFormat * flipper_format ) {
314
437
furi_assert (context );
315
438
SubGhzProtocolEncoderRAW * instance = context ;
0 commit comments