-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.cpp
More file actions
492 lines (444 loc) · 13.4 KB
/
app.cpp
File metadata and controls
492 lines (444 loc) · 13.4 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include "app.hpp"
#include <update/update.h>
void FlipDownloaderApp::callbackSubmenuChoices(uint32_t index)
{
switch (index)
{
case FlipDownloaderSubmenuRun:
// if the board is not connected, we can't use WiFi
if (!isBoardConnected())
{
easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
return;
}
// if we don't have WiFi credentials, we can't connect to WiFi in case
// we are not connected to WiFi yet
if (!hasWiFiCredentials())
{
easy_flipper_dialog("No WiFi Credentials", "Please set your WiFi SSID\nand Password in Settings.");
return;
}
run = std::make_unique<FlipDownloaderRun>();
if (!run->init(this))
{
FURI_LOG_E(TAG, "Failed to initialize run");
run.reset();
return;
}
freeRunView(); // Free any previous run view resources
if (!initRunView())
{
FURI_LOG_E(TAG, "Failed to initialize run view");
run.reset();
return;
}
break;
case FlipDownloaderSubmenuAbout:
about = std::make_unique<FlipDownloaderAbout>();
if (!about->init(&viewDispatcher, this))
{
FURI_LOG_E(TAG, "Failed to initialize about");
about.reset();
return;
}
view_dispatcher_switch_to_view(viewDispatcher, FlipDownloaderViewAbout);
break;
case FlipDownloaderSubmenuSettings:
settings = std::make_unique<FlipDownloaderSettings>();
if (!settings->init(&viewDispatcher, this))
{
FURI_LOG_E(TAG, "Failed to initialize settings");
settings.reset();
return;
}
view_dispatcher_switch_to_view(viewDispatcher, FlipDownloaderViewSettings);
break;
default:
break;
}
}
uint32_t FlipDownloaderApp::callbackExitApp(void *context)
{
UNUSED(context);
return VIEW_NONE;
}
void FlipDownloaderApp::createAppDataPath(const char *appId)
{
Storage *storage = static_cast<Storage *>(furi_record_open(RECORD_STORAGE));
char directory_path[256];
snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s", appId);
storage_common_mkdir(storage, directory_path);
snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/data", appId);
storage_common_mkdir(storage, directory_path);
furi_record_close(RECORD_STORAGE);
}
void FlipDownloaderApp::freeRunView()
{
// Stop and free timer first
if (timer)
{
furi_timer_stop(timer);
furi_timer_free(timer);
timer = nullptr;
}
// Clean up viewport if it exists
if (gui && viewPort)
{
gui_remove_view_port(gui, viewPort);
view_port_free(viewPort);
viewPort = nullptr;
}
}
bool FlipDownloaderApp::httpDownloadFile(
const char *saveLocation, // full path where the file will be saved
const char *url // URL to download the file from
)
{
if (!flipperHttp)
{
FURI_LOG_E(TAG, "FlipDownloaderApp::httpDownloadFile: FlipperHTTP is NULL");
return false;
}
snprintf(flipperHttp->file_path, sizeof(flipperHttp->file_path), saveLocation);
flipperHttp->save_received_data = false;
flipperHttp->is_bytes_request = true;
flipperHttp->state = IDLE;
return flipper_http_request(flipperHttp, BYTES, url, "{\"Content-Type\": \"application/octet-stream\"}", NULL);
}
FuriString *FlipDownloaderApp::httpRequest(
const char *url,
HTTPMethod method,
const char *headers,
const char *payload)
{
if (!flipperHttp)
{
FURI_LOG_E(TAG, "FlipDownloaderApp::httpRequest: FlipperHTTP is NULL");
return NULL;
}
snprintf(flipperHttp->file_path, sizeof(flipperHttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/data/temp.json", APP_ID);
flipperHttp->save_received_data = true;
flipperHttp->state = IDLE;
if (!flipper_http_request(flipperHttp, method, url, headers, payload))
{
FURI_LOG_E(TAG, "FlipDownloaderApp::httpRequest: Failed to send HTTP request");
return NULL;
}
flipperHttp->state = RECEIVING;
while (flipperHttp->state != IDLE)
{
furi_delay_ms(100);
}
return flipper_http_load_from_file(flipperHttp->file_path);
}
bool FlipDownloaderApp::httpRequestAsync(
const char *saveLocation,
const char *url,
HTTPMethod method,
const char *headers,
const char *payload)
{
if (!flipperHttp)
{
FURI_LOG_E(TAG, "FlipDownloaderApp::httpRequestAsync: FlipperHTTP is NULL");
return false;
}
snprintf(flipperHttp->file_path, sizeof(flipperHttp->file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/data/%s", APP_ID, saveLocation);
flipperHttp->save_received_data = true;
flipperHttp->state = IDLE;
if (!flipper_http_request(flipperHttp, method, url, headers, payload))
{
FURI_LOG_E(TAG, "FlipDownloaderApp::httpRequestAsync: Failed to send HTTP request");
return false;
}
flipperHttp->state = RECEIVING;
return true;
}
bool FlipDownloaderApp::hasWiFiCredentials()
{
char ssid[64] = {0};
char password[64] = {0};
return load_char("wifi_ssid", ssid, sizeof(ssid), "flipper_http") &&
load_char("wifi_pass", password, sizeof(password), "flipper_http") &&
strlen(ssid) > 0 &&
strlen(password) > 0;
}
bool FlipDownloaderApp::initRunView()
{
if (!gui)
{
FURI_LOG_E(TAG, "GUI is not initialized");
return false;
}
if (viewPort)
{
FURI_LOG_E(TAG, "ViewPort is already initialized");
return false;
}
viewPort = view_port_alloc();
view_port_draw_callback_set(viewPort, viewPortDraw, this);
view_port_input_callback_set(viewPort, viewPortInput, this);
gui_add_view_port(gui, viewPort, GuiLayerFullscreen);
// Start the timer for game updates
if (!timer)
{
timer = furi_timer_alloc(timerCallback, FuriTimerTypePeriodic, this);
}
if (timer)
{
furi_timer_start(timer, 100); // Update every 100ms
}
return true;
}
bool FlipDownloaderApp::isBoardConnected()
{
if (!flipperHttp)
{
FURI_LOG_E(TAG, "FlipperHTTP is not initialized");
return false;
}
if (!flipper_http_send_command(flipperHttp, HTTP_CMD_PING))
{
FURI_LOG_E(TAG, "Failed to ping the device");
return false;
}
furi_delay_ms(100);
// Try to wait for pong response.
uint32_t counter = 100;
while (flipperHttp->state == INACTIVE && --counter > 0)
{
furi_delay_ms(100);
}
// last response should be PONG
return flipperHttp->last_response && strcmp(flipperHttp->last_response, "[PONG]") == 0;
}
bool FlipDownloaderApp::load_char(const char *path_name, char *value, size_t value_size, const char *appId)
{
Storage *storage = static_cast<Storage *>(furi_record_open(RECORD_STORAGE));
File *file = storage_file_alloc(storage);
char file_path[256];
snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/data/%s.txt", appId, path_name);
if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
{
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return false;
}
size_t read_count = storage_file_read(file, value, value_size);
// ensure we don't go out of bounds
if (read_count > 0 && read_count < value_size)
{
value[read_count - 1] = '\0';
}
else if (read_count >= value_size && value_size > 0)
{
value[value_size - 1] = '\0';
}
else
{
value[0] = '\0';
}
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return strlen(value) > 0;
}
bool FlipDownloaderApp::save_char(const char *path_name, const char *value, const char *appId)
{
Storage *storage = static_cast<Storage *>(furi_record_open(RECORD_STORAGE));
File *file = storage_file_alloc(storage);
char file_path[256];
snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/%s/data/%s.txt", appId, path_name);
storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
size_t data_size = strlen(value) + 1; // Include null terminator
storage_file_write(file, value, data_size);
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return true;
}
bool FlipDownloaderApp::sendWiFiCredentials(const char *ssid, const char *password)
{
if (!flipperHttp)
{
FURI_LOG_E(TAG, "FlipperHTTP is not initialized");
return false;
}
if (!ssid || !password)
{
FURI_LOG_E(TAG, "SSID or Password is NULL");
return false;
}
return flipper_http_save_wifi(flipperHttp, ssid, password);
}
void FlipDownloaderApp::settings_item_selected_callback(void *context, uint32_t index)
{
FlipDownloaderApp *app = (FlipDownloaderApp *)context;
app->settingsItemSelected(index);
}
void FlipDownloaderApp::submenu_choices_callback(void *context, uint32_t index)
{
FlipDownloaderApp *app = (FlipDownloaderApp *)context;
app->callbackSubmenuChoices(index);
}
void FlipDownloaderApp::timerCallback(void *context)
{
FlipDownloaderApp *app = static_cast<FlipDownloaderApp *>(context);
furi_check(app);
auto run = app->run.get();
if (run)
{
if (run->isActive())
{
// Run is active, update the viewport
if (app->viewPort)
{
view_port_update(app->viewPort);
}
}
else
{
// Stop the timer first
if (app->timer)
{
furi_timer_stop(app->timer);
}
// Remove viewport
if (app->gui && app->viewPort)
{
gui_remove_view_port(app->gui, app->viewPort);
view_port_free(app->viewPort);
app->viewPort = nullptr;
}
}
}
}
void FlipDownloaderApp::settingsItemSelected(uint32_t index)
{
if (settings)
{
settings->settingsItemSelected(index);
}
}
void FlipDownloaderApp::viewPortDraw(Canvas *canvas, void *context)
{
FlipDownloaderApp *app = static_cast<FlipDownloaderApp *>(context);
furi_check(app);
auto run = app->run.get();
if (run)
{
if (run->isActive())
{
run->updateDraw(canvas);
}
}
}
void FlipDownloaderApp::viewPortInput(InputEvent *event, void *context)
{
FlipDownloaderApp *app = static_cast<FlipDownloaderApp *>(context);
furi_check(app);
auto run = app->run.get();
if (run && run->isActive())
{
run->updateInput(event);
}
}
FlipDownloaderApp::FlipDownloaderApp()
{
gui = static_cast<Gui *>(furi_record_open(RECORD_GUI));
// Allocate ViewDispatcher
if (!easy_flipper_set_view_dispatcher(&viewDispatcher, gui, this))
{
FURI_LOG_E(TAG, "Failed to allocate view dispatcher");
return;
}
// Submenu
if (!easy_flipper_set_submenu(&submenu, FlipDownloaderViewSubmenu, VERSION_TAG, callbackExitApp, &viewDispatcher))
{
FURI_LOG_E(TAG, "Failed to allocate submenu");
return;
}
submenu_add_item(submenu, "Browse", FlipDownloaderSubmenuRun, submenu_choices_callback, this);
submenu_add_item(submenu, "About", FlipDownloaderSubmenuAbout, submenu_choices_callback, this);
submenu_add_item(submenu, "Settings", FlipDownloaderSubmenuSettings, submenu_choices_callback, this);
flipperHttp = flipper_http_alloc();
if (!flipperHttp)
{
FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
return;
}
createAppDataPath();
// Switch to the submenu view
view_dispatcher_switch_to_view(viewDispatcher, FlipDownloaderViewSubmenu);
}
FlipDownloaderApp::~FlipDownloaderApp()
{
freeRunView();
// Clean up run
if (run)
{
run.reset();
}
// Clean up settings
if (settings)
{
settings.reset();
}
// Clean up about
if (about)
{
about.reset();
}
// Free submenu
if (submenu)
{
view_dispatcher_remove_view(viewDispatcher, FlipDownloaderViewSubmenu);
submenu_free(submenu);
}
// Free view dispatcher
if (viewDispatcher)
{
view_dispatcher_free(viewDispatcher);
}
// Close GUI
if (gui)
{
furi_record_close(RECORD_GUI);
}
// Free FlipperHTTP
if (flipperHttp)
{
flipper_http_free(flipperHttp);
}
}
void FlipDownloaderApp::runDispatcher()
{
view_dispatcher_run(viewDispatcher);
}
void FlipDownloaderApp::updateApp()
{
if (flipperHttp && isBoardConnected() && hasWiFiCredentials())
{
if (update_is_ready(flipperHttp, true))
{
easy_flipper_dialog("Update Status", "Complete.\nRestart your Flipper Zero.");
}
}
}
extern "C"
{
int32_t main_flip_downloader(void *p)
{
// Suppress unused parameter warning
UNUSED(p);
// Create the app
FlipDownloaderApp app;
app.save_char("app_version", VERSION);
// check if update is available from lab.flipper.net
app.updateApp();
// Run the app
app.runDispatcher();
// return success
return 0;
}
}