-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.cpp
More file actions
574 lines (512 loc) · 15.5 KB
/
app.cpp
File metadata and controls
574 lines (512 loc) · 15.5 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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#include "app.hpp"
#include <update/update.h>
FlipSocialApp::FlipSocialApp()
{
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, FlipSocialViewSubmenu,
VERSION_TAG, callbackExitApp, &viewDispatcher))
{
FURI_LOG_E(TAG, "Failed to allocate submenu");
return;
}
submenu_add_item(submenu, "Run", FlipSocialSubmenuRun, submenuChoicesCallback, this);
submenu_add_item(submenu, "About", FlipSocialSubmenuAbout, submenuChoicesCallback, this);
submenu_add_item(submenu, "Settings", FlipSocialSubmenuSettings, submenuChoicesCallback, this);
flipperHttp = flipper_http_alloc();
if (!flipperHttp)
{
FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
return;
}
createAppDataPath(APP_ID);
createAppDataPath("flipper_http");
// Switch to the submenu view
view_dispatcher_switch_to_view(viewDispatcher, FlipSocialViewSubmenu);
}
FlipSocialApp::~FlipSocialApp()
{
// 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;
}
// 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, FlipSocialViewSubmenu);
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);
}
}
uint32_t FlipSocialApp::callbackExitApp(void *context)
{
UNUSED(context);
return VIEW_NONE;
}
void FlipSocialApp::callbackSubmenuChoices(uint32_t index)
{
switch (index)
{
case FlipSocialSubmenuRun:
// 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;
}
// if we don't have user credentials, we can't connect to the user account
if (!hasUserCredentials())
{
easy_flipper_dialog("No User Credentials", "Please set your Username\nand Password in Settings.");
return;
}
if (!run)
{
run = std::make_unique<FlipSocialRun>(this);
}
// setup view port
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);
// make sure timer is free
if (timer)
{
furi_timer_stop(timer);
furi_timer_free(timer);
timer = nullptr;
}
// 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
}
break;
case FlipSocialSubmenuAbout:
if (!about)
{
about = std::make_unique<FlipSocialAbout>(&viewDispatcher);
}
view_dispatcher_switch_to_view(viewDispatcher, FlipSocialViewAbout);
break;
case FlipSocialSubmenuSettings:
if (!settings)
{
settings = std::make_unique<FlipSocialSettings>(&viewDispatcher, this);
}
view_dispatcher_switch_to_view(viewDispatcher, FlipSocialViewSettings);
break;
default:
break;
}
}
void FlipSocialApp::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);
}
FuriString *FlipSocialApp::httpRequest(
const char *url,
HTTPMethod method,
const char *headers,
const char *payload)
{
if (!flipperHttp)
{
FURI_LOG_E(TAG, "FlipSocialApp::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, "FlipSocialApp::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 FlipSocialApp::httpRequestAsync(
const char *saveLocation,
const char *url,
HTTPMethod method,
const char *headers,
const char *payload)
{
if (!flipperHttp)
{
FURI_LOG_E(TAG, "FlipSocialApp::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, "FlipSocialApp::httpRequestAsync: Failed to send HTTP request");
return false;
}
flipperHttp->state = RECEIVING;
return true;
}
bool FlipSocialApp::hasWiFiCredentials()
{
char ssid[64] = {0};
char password[64] = {0};
return loadChar("wifi_ssid", ssid, sizeof(ssid), "flipper_http") &&
loadChar("wifi_pass", password, sizeof(password), "flipper_http") &&
strlen(ssid) > 0 &&
strlen(password) > 0;
}
bool FlipSocialApp::hasUserCredentials()
{
char username[64] = {0};
char password[64] = {0};
return loadChar("user_name", username, sizeof(username), "flipper_http") &&
loadChar("user_pass", password, sizeof(password), "flipper_http") &&
strlen(username) > 0 &&
strlen(password) > 0;
}
bool FlipSocialApp::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 FlipSocialApp::loadChar(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 FlipSocialApp::loadFileChunk(const char *filePath, char *buffer, size_t sizeOfChunk, uint8_t iteration)
{
if (!filePath || !buffer || sizeOfChunk == 0)
{
FURI_LOG_E(TAG, "Invalid parameters for loadFileChunk");
return false;
}
Storage *storage = static_cast<Storage *>(furi_record_open(RECORD_STORAGE));
if (!storage)
{
FURI_LOG_E(TAG, "Failed to open storage record");
return false;
}
File *file = storage_file_alloc(storage);
if (!file)
{
FURI_LOG_E(TAG, "Failed to allocate file");
furi_record_close(RECORD_STORAGE);
return false;
}
// Open the file for reading
if (!storage_file_open(file, filePath, FSAM_READ, FSOM_OPEN_EXISTING))
{
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
FURI_LOG_E(TAG, "Failed to open file for reading: %s", filePath);
return false;
}
// Change the current access position in a file.
if (!storage_file_seek(file, iteration * sizeOfChunk, true))
{
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
FURI_LOG_E(TAG, "Failed to seek file: %s", filePath);
return false;
}
// Check whether the current access position is at the end of the file.
if (storage_file_eof(file))
{
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return false;
}
// Read data into the buffer
size_t read_count = storage_file_read(file, buffer, sizeOfChunk);
if (storage_file_get_error(file) != FSE_OK)
{
FURI_LOG_E(TAG, "Error reading from file.");
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return false;
}
// ensure we don't go out of bounds
if (read_count > 0 && read_count < sizeOfChunk)
{
buffer[read_count] = '\0'; // Null-terminate after the last read character
}
else if (read_count >= sizeOfChunk && sizeOfChunk > 0)
{
buffer[sizeOfChunk - 1] = '\0'; // Use last byte for null terminator
}
else
{
buffer[0] = '\0'; // Empty buffer
}
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return read_count > 0;
}
void FlipSocialApp::runDispatcher()
{
view_dispatcher_run(viewDispatcher);
}
bool FlipSocialApp::saveChar(const char *path_name, const char *value, const char *appId, bool overwrite)
{
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_WRITE, overwrite ? FSOM_CREATE_ALWAYS : FSOM_OPEN_APPEND))
{
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return false;
}
size_t append = overwrite ? 1 : 0; // add null terminator if overwriting
size_t data_size = strlen(value) + append;
if (storage_file_write(file, value, data_size) != data_size)
{
FURI_LOG_E(TAG, "Failed to write complete data to file: %s", file_path);
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return false;
}
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return true;
}
bool FlipSocialApp::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);
}
bool FlipSocialApp::setHttpState(HTTPState state) noexcept
{
if (flipperHttp)
{
flipperHttp->state = state;
return true;
}
return false;
}
void FlipSocialApp::settingsItemSelected(uint32_t index)
{
if (settings)
{
settings->settingsItemSelected(index);
}
}
void FlipSocialApp::submenuChoicesCallback(void *context, uint32_t index)
{
FlipSocialApp *app = (FlipSocialApp *)context;
app->callbackSubmenuChoices(index);
}
void FlipSocialApp::timerCallback(void *context)
{
FlipSocialApp *app = static_cast<FlipSocialApp *>(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 and cleanup timer first
if (app->timer)
{
furi_timer_stop(app->timer);
}
// Clean up viewport if it exists
if (app->gui && app->viewPort)
{
gui_remove_view_port(app->gui, app->viewPort);
view_port_free(app->viewPort);
app->viewPort = nullptr;
}
view_dispatcher_switch_to_view(app->viewDispatcher, FlipSocialViewSubmenu);
app->run.reset();
}
}
}
void FlipSocialApp::updateApp()
{
if (flipperHttp && isBoardConnected())
{
if (update_is_ready(flipperHttp, true))
{
easy_flipper_dialog("Update Status", "Complete.\nRestart your Flipper Zero.");
}
}
}
void FlipSocialApp::viewPortDraw(Canvas *canvas, void *context)
{
FlipSocialApp *app = static_cast<FlipSocialApp *>(context);
furi_check(app);
auto run = app->run.get();
if (run && run->isActive())
{
run->updateDraw(canvas);
}
}
void FlipSocialApp::viewPortInput(InputEvent *event, void *context)
{
if (event->type != InputTypeShort && event->type != InputTypeLong)
{
return;
}
FlipSocialApp *app = static_cast<FlipSocialApp *>(context);
furi_check(app);
auto run = app->run.get();
if (run && run->isActive())
{
run->updateInput(event);
}
}
extern "C"
{
int32_t main_flip_social(void *p)
{
// Suppress unused parameter warning
UNUSED(p);
// Create the app
FlipSocialApp app;
app.saveChar("app_version", VERSION);
// check if update is available from lab.flipper.net
// app.updateApp();
// Run the app
app.runDispatcher();
// return success
return 0;
}
}