-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathradio_scanner_app.c
425 lines (381 loc) · 13.1 KB
/
radio_scanner_app.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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include "radio_scanner_app.h"
#include <furi.h>
#include <furi_hal.h>
#include <furi_hal_gpio.h>
#include <gui/elements.h>
#include <furi_hal_speaker.h>
#include <subghz/devices/devices.h>
#define TAG "RadioScannerApp"
#define RADIO_SCANNER_DEFAULT_FREQ 433920000
#define RADIO_SCANNER_DEFAULT_RSSI (-100.0f)
#define RADIO_SCANNER_DEFAULT_SENSITIVITY (-85.0f)
#define RADIO_SCANNER_BUFFER_SZ 32
#define SUBGHZ_FREQUENCY_MIN 300000000
#define SUBGHZ_FREQUENCY_MAX 928000000
#define SUBGHZ_FREQUENCY_STEP 10000
#define SUBGHZ_DEVICE_NAME "cc1101_int"
static void radio_scanner_draw_callback(Canvas* canvas, void* context) {
furi_assert(canvas);
furi_assert(context);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Enter radio_scanner_draw_callback");
#endif
RadioScannerApp* app = (RadioScannerApp*)context;
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Radio Scanner");
canvas_set_font(canvas, FontSecondary);
char freq_str[RADIO_SCANNER_BUFFER_SZ + 1] = {0};
snprintf(freq_str, RADIO_SCANNER_BUFFER_SZ, "Freq: %.2f MHz", (double)app->frequency / 1000000);
canvas_draw_str_aligned(canvas, 64, 18, AlignCenter, AlignTop, freq_str);
char rssi_str[RADIO_SCANNER_BUFFER_SZ + 1] = {0};
snprintf(rssi_str, RADIO_SCANNER_BUFFER_SZ, "RSSI: %.2f", (double)app->rssi);
canvas_draw_str_aligned(canvas, 64, 30, AlignCenter, AlignTop, rssi_str);
char sensitivity_str[RADIO_SCANNER_BUFFER_SZ + 1] = {0};
snprintf(sensitivity_str, RADIO_SCANNER_BUFFER_SZ, "Sens: %.2f", (double)app->sensitivity);
canvas_draw_str_aligned(canvas, 64, 42, AlignCenter, AlignTop, sensitivity_str);
canvas_draw_str_aligned(
canvas, 64, 54, AlignCenter, AlignTop, app->scanning ? "Scanning..." : "Locked");
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Exit radio_scanner_draw_callback");
#endif
}
static void radio_scanner_input_callback(InputEvent* input_event, void* context) {
furi_assert(context);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Enter radio_scanner_input_callback");
FURI_LOG_D(TAG, "Input event: type=%d, key=%d", input_event->type, input_event->key);
#endif
FuriMessageQueue* event_queue = context;
furi_message_queue_put(event_queue, input_event, FuriWaitForever);
FURI_LOG_D(TAG, "Exit radio_scanner_input_callback");
}
static void radio_scanner_rx_callback(const void* data, size_t size, void* context) {
UNUSED(data);
UNUSED(context);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "radio_scanner_rx_callback called with size: %zu", size);
#else
UNUSED(size);
#endif
}
static void radio_scanner_update_rssi(RadioScannerApp* app) {
furi_assert(app);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Enter radio_scanner_update_rssi");
#endif
if(app->radio_device) {
app->rssi = subghz_devices_get_rssi(app->radio_device);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Updated RSSI: %f", (double)app->rssi);
#endif
} else {
FURI_LOG_E(TAG, "Radio device is NULL");
app->rssi = RADIO_SCANNER_DEFAULT_RSSI;
}
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Exit radio_scanner_update_rssi");
#endif
}
static bool radio_scanner_init_subghz(RadioScannerApp* app) {
furi_assert(app);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Enter radio_scanner_init_subghz");
#endif
subghz_devices_init();
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "SubGHz devices initialized");
#endif
const SubGhzDevice* device = subghz_devices_get_by_name(SUBGHZ_DEVICE_NAME);
if(!device) {
FURI_LOG_E(TAG, "Failed to get SubGhzDevice");
return false;
}
FURI_LOG_I(TAG, "SubGhzDevice obtained: %s", subghz_devices_get_name(device));
app->radio_device = device;
subghz_devices_begin(device);
subghz_devices_reset(device);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "SubGhzDevice begun");
#endif
if(!subghz_devices_is_frequency_valid(device, app->frequency)) {
FURI_LOG_E(TAG, "Invalid frequency: %lu", app->frequency);
return false;
}
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Frequency is valid: %lu", app->frequency);
#endif
subghz_devices_load_preset(device, FuriHalSubGhzPreset2FSKDev238Async, NULL);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Preset loaded");
#endif
subghz_devices_set_frequency(device, app->frequency);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Frequency set to %lu", app->frequency);
#endif
subghz_devices_start_async_rx(device, radio_scanner_rx_callback, app);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Asynchronous RX started");
#endif
if(furi_hal_speaker_acquire(30)) {
app->speaker_acquired = true;
subghz_devices_set_async_mirror_pin(device, &gpio_speaker);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Speaker acquired and async mirror pin set");
#endif
} else {
app->speaker_acquired = false;
FURI_LOG_E(TAG, "Failed to acquire speaker");
}
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Exit radio_scanner_init_subghz");
#endif
return true;
}
static void radio_scanner_process_scanning(RadioScannerApp* app) {
furi_assert(app);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Enter radio_scanner_process_scanning");
#endif
radio_scanner_update_rssi(app);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "RSSI after update: %f", (double)app->rssi);
#endif
bool signal_detected = (app->rssi > app->sensitivity);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Signal detected: %d", signal_detected);
#endif
if(signal_detected) {
if(app->scanning) {
app->scanning = false;
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Scanning stopped");
#endif
}
} else {
if(!app->scanning) {
app->scanning = true;
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Scanning started");
#endif
}
}
if(!app->scanning) {
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Exit radio_scanner_process_scanning");
return;
#endif
}
uint32_t new_frequency = (app->scan_direction == ScanDirectionUp) ?
app->frequency + SUBGHZ_FREQUENCY_STEP :
app->frequency - SUBGHZ_FREQUENCY_STEP;
if(!subghz_devices_is_frequency_valid(app->radio_device, new_frequency)) {
if(app->scan_direction == ScanDirectionUp) {
if(new_frequency < 387000000) {
new_frequency = 387000000;
} else if(new_frequency < 779000000) {
new_frequency = 779000000;
} else if(new_frequency > SUBGHZ_FREQUENCY_MAX) {
new_frequency = SUBGHZ_FREQUENCY_MIN;
}
} else {
if(new_frequency > 464000000) {
new_frequency = 464000000;
} else if(new_frequency > 348000000) {
new_frequency = 348000000;
} else if(new_frequency < SUBGHZ_FREQUENCY_MIN) {
new_frequency = SUBGHZ_FREQUENCY_MAX;
}
}
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Adjusted frequency to next valid range: %lu", new_frequency);
#endif
}
subghz_devices_flush_rx(app->radio_device);
subghz_devices_stop_async_rx(app->radio_device);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Asynchronous RX stopped");
#endif
subghz_devices_idle(app->radio_device);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Device set to idle");
#endif
app->frequency = new_frequency;
subghz_devices_set_frequency(app->radio_device, app->frequency);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Frequency set to %lu", app->frequency);
#endif
subghz_devices_start_async_rx(app->radio_device, radio_scanner_rx_callback, app);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Asynchronous RX restarted");
FURI_LOG_D(TAG, "Exit radio_scanner_process_scanning");
#endif
}
RadioScannerApp* radio_scanner_app_alloc() {
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Enter radio_scanner_app_alloc");
#endif
RadioScannerApp* app = malloc(sizeof(RadioScannerApp));
if(!app) {
FURI_LOG_E(TAG, "Failed to allocate RadioScannerApp");
return NULL;
}
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "RadioScannerApp allocated");
#endif
app->view_port = view_port_alloc();
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "ViewPort allocated");
#endif
app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Event queue allocated");
#endif
app->running = true;
app->frequency = RADIO_SCANNER_DEFAULT_FREQ;
app->rssi = RADIO_SCANNER_DEFAULT_RSSI;
app->sensitivity = RADIO_SCANNER_DEFAULT_SENSITIVITY;
app->scanning = true;
app->scan_direction = ScanDirectionUp;
app->speaker_acquired = false;
app->radio_device = NULL;
view_port_draw_callback_set(app->view_port, radio_scanner_draw_callback, app);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Draw callback set");
#endif
view_port_input_callback_set(app->view_port, radio_scanner_input_callback, app->event_queue);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Input callback set");
FURI_LOG_D(TAG, "Exit radio_scanner_app_alloc");
#endif
app->gui = furi_record_open(RECORD_GUI);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "GUI record opened");
#endif
gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "ViewPort added to GUI");
#endif
return app;
}
void radio_scanner_app_free(RadioScannerApp* app) {
furi_assert(app);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Enter radio_scanner_app_free");
#endif
if(app->speaker_acquired && furi_hal_speaker_is_mine()) {
subghz_devices_set_async_mirror_pin(app->radio_device, NULL);
furi_hal_speaker_release();
app->speaker_acquired = false;
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Speaker released");
#endif
}
if(app->radio_device) {
subghz_devices_flush_rx(app->radio_device);
subghz_devices_stop_async_rx(app->radio_device);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Asynchronous RX stopped");
#endif
subghz_devices_idle(app->radio_device);
subghz_devices_sleep(app->radio_device);
subghz_devices_end(app->radio_device);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "SubGhzDevice stopped and ended");
#endif
}
subghz_devices_deinit();
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "SubGHz devices de-initialized");
#endif
gui_remove_view_port(app->gui, app->view_port);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "ViewPort removed from GUI");
#endif
view_port_free(app->view_port);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "ViewPort freed");
#endif
furi_message_queue_free(app->event_queue);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Event queue freed");
#endif
furi_record_close(RECORD_GUI);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "GUI record closed");
#endif
free(app);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "RadioScannerApp memory freed");
#endif
}
int32_t radio_scanner_app(void* p) {
UNUSED(p);
FURI_LOG_I(TAG, "Enter radio_scanner_app");
RadioScannerApp* app = radio_scanner_app_alloc();
if(!app) {
FURI_LOG_E(TAG, "Failed to allocate app");
return 1;
}
if(!radio_scanner_init_subghz(app)) {
FURI_LOG_E(TAG, "Failed to initialize SubGHz");
radio_scanner_app_free(app);
return 255;
}
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "SubGHz initialized successfully");
#endif
InputEvent event;
while(app->running) {
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Main loop iteration");
#endif
if(app->scanning) {
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Scanning is active");
#endif
radio_scanner_process_scanning(app);
} else {
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Scanning is inactive, updating RSSI");
#endif
radio_scanner_update_rssi(app);
}
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Checking for input events");
#endif
if(furi_message_queue_get(app->event_queue, &event, 10) == FuriStatusOk) {
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Input event received: type=%d, key=%d", event.type, event.key);
#endif
if(event.type == InputTypeShort) {
if(event.key == InputKeyOk) {
app->scanning = !app->scanning;
FURI_LOG_I(TAG, "Toggled scanning: %d", app->scanning);
} else if(event.key == InputKeyUp) {
app->sensitivity += 1.0f;
FURI_LOG_I(TAG, "Increased sensitivity: %f", (double)app->sensitivity);
} else if(event.key == InputKeyDown) {
app->sensitivity -= 1.0f;
FURI_LOG_I(TAG, "Decreased sensitivity: %f", (double)app->sensitivity);
} else if(event.key == InputKeyLeft) {
app->scan_direction = ScanDirectionDown;
FURI_LOG_I(TAG, "Scan direction set to down");
} else if(event.key == InputKeyRight) {
app->scan_direction = ScanDirectionUp;
FURI_LOG_I(TAG, "Scan direction set to up");
} else if(event.key == InputKeyBack) {
app->running = false;
FURI_LOG_I(TAG, "Exiting app");
}
}
}
view_port_update(app->view_port);
furi_delay_ms(10);
}
radio_scanner_app_free(app);
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Exit radio_scanner_app");
#endif
return 0;
}