flip_store_callback.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. #include <callback/flip_store_callback.h>
  2. // Below added by Derek Jamison
  3. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  4. #ifdef DEVELOPMENT
  5. #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  6. #define DEV_CRASH() furi_crash()
  7. #else
  8. #define FURI_LOG_DEV(tag, format, ...)
  9. #define DEV_CRASH()
  10. #endif
  11. bool flip_store_app_does_exist = false;
  12. uint32_t selected_firmware_index = 0;
  13. static uint32_t callback_to_app_category_list(void *context);
  14. static bool flip_store_dl_app_fetch(DataLoaderModel *model)
  15. {
  16. if (!model->fhttp)
  17. {
  18. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  19. return false;
  20. }
  21. return flip_store_install_app(model->fhttp, categories[flip_store_category_index]);
  22. }
  23. static char *flip_store_dl_app_parse(DataLoaderModel *model)
  24. {
  25. if (!model->fhttp || model->fhttp->state != IDLE)
  26. {
  27. FURI_LOG_E(TAG, "FlipperHTTP is NULL or not IDLE");
  28. return "Failed to install app.";
  29. }
  30. return "App installed successfully.";
  31. }
  32. static void flip_store_dl_app_switch_to_view(FlipStoreApp *app)
  33. {
  34. flip_store_generic_switch_to_view(app, flip_catalog[app_selected_index].app_name, flip_store_dl_app_fetch, flip_store_dl_app_parse, 1, callback_to_app_category_list, FlipStoreViewLoader);
  35. }
  36. //
  37. static bool flip_store_fetch_app_list(DataLoaderModel *model)
  38. {
  39. if (!model->fhttp)
  40. {
  41. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  42. return false;
  43. }
  44. model->fhttp->state = IDLE;
  45. flip_catalog_free();
  46. snprintf(
  47. model->fhttp->file_path,
  48. sizeof(model->fhttp->file_path),
  49. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/%s.json", categories[flip_store_category_index]);
  50. model->fhttp->save_received_data = true;
  51. model->fhttp->is_bytes_request = false;
  52. char url[128];
  53. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/flipper/apps/%s/max/", categories[flip_store_category_index]);
  54. return flipper_http_get_request_with_headers(model->fhttp, url, "{\"Content-Type\":\"application/json\"}");
  55. }
  56. static bool set_appropriate_list(FlipperHTTP *fhttp, Submenu **submenu, FlipStoreApp *app)
  57. {
  58. if (!submenu || !fhttp || !app)
  59. {
  60. FURI_LOG_E(TAG, "Submenu, FlipperHTTP, or app is NULL");
  61. return false;
  62. }
  63. if (!easy_flipper_set_submenu(submenu, FlipStoreViewAppListCategory, categories[flip_store_category_index], callback_to_app_list, &app->view_dispatcher))
  64. {
  65. return false;
  66. }
  67. if (flip_store_process_app_list(fhttp) && submenu && flip_catalog)
  68. {
  69. submenu_reset(*submenu);
  70. // add each app name to submenu
  71. for (int i = 0; i < MAX_APP_COUNT; i++)
  72. {
  73. if (strlen(flip_catalog[i].app_name) > 0)
  74. {
  75. submenu_add_item(*submenu, flip_catalog[i].app_name, FlipStoreSubmenuIndexStartAppList + i, callback_submenu_choices, app);
  76. }
  77. else
  78. {
  79. break;
  80. }
  81. }
  82. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppListCategory);
  83. return true;
  84. }
  85. else
  86. {
  87. FURI_LOG_E(TAG, "Failed to process the app list");
  88. return false;
  89. }
  90. return false;
  91. }
  92. static char *flip_store_parse_app_list(DataLoaderModel *model)
  93. {
  94. if (!model->fhttp)
  95. {
  96. return "Failed to fetch app list.";
  97. }
  98. FlipStoreApp *app = (FlipStoreApp *)model->parser_context;
  99. if (!app)
  100. {
  101. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  102. return "Failed to fetch app list.";
  103. }
  104. return set_appropriate_list(model->fhttp, &app->submenu_app_list_category, app) ? "App list fetched successfully." : "Failed to fetch app list.";
  105. }
  106. static void flip_store_switch_to_app_list(FlipStoreApp *app)
  107. {
  108. flip_store_generic_switch_to_view(app, categories[flip_store_category_index], flip_store_fetch_app_list, flip_store_parse_app_list, 1, callback_to_submenu, FlipStoreViewLoader);
  109. }
  110. //
  111. static bool flip_store_fetch_firmware(DataLoaderModel *model)
  112. {
  113. if (!model->fhttp)
  114. {
  115. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  116. return false;
  117. }
  118. model->fhttp->state = IDLE;
  119. if (model->request_index == 0)
  120. {
  121. firmware_free();
  122. firmwares = firmware_alloc();
  123. if (!firmwares)
  124. {
  125. return false;
  126. }
  127. firmware_request_success = flip_store_get_firmware_file(
  128. model->fhttp,
  129. firmwares[selected_firmware_index].links[0],
  130. firmwares[selected_firmware_index].name,
  131. strrchr(firmwares[selected_firmware_index].links[0], '/') + 1);
  132. return firmware_request_success;
  133. }
  134. else if (model->request_index == 1)
  135. {
  136. firmware_request_success_2 = flip_store_get_firmware_file(
  137. model->fhttp,
  138. firmwares[selected_firmware_index].links[1],
  139. firmwares[selected_firmware_index].name,
  140. strrchr(firmwares[selected_firmware_index].links[1], '/') + 1);
  141. return firmware_request_success_2;
  142. }
  143. else if (model->request_index == 2)
  144. {
  145. firmware_request_success_3 = flip_store_get_firmware_file(
  146. model->fhttp,
  147. firmwares[selected_firmware_index].links[2],
  148. firmwares[selected_firmware_index].name,
  149. strrchr(firmwares[selected_firmware_index].links[2], '/') + 1);
  150. return firmware_request_success_3;
  151. }
  152. return false;
  153. }
  154. static char *flip_store_parse_firmware(DataLoaderModel *model)
  155. {
  156. if (model->request_index == 0)
  157. {
  158. if (firmware_request_success)
  159. {
  160. return "File 1 installed.";
  161. }
  162. }
  163. else if (model->request_index == 1)
  164. {
  165. if (firmware_request_success_2)
  166. {
  167. return "File 2 installed.";
  168. }
  169. }
  170. else if (model->request_index == 2)
  171. {
  172. if (firmware_request_success_3)
  173. {
  174. return "Firmware downloaded successfully";
  175. }
  176. }
  177. return "Failed to download firmware.";
  178. }
  179. static void flip_store_switch_to_firmware_list(FlipStoreApp *app)
  180. {
  181. flip_store_generic_switch_to_view(app, firmwares[selected_firmware_index].name, flip_store_fetch_firmware, flip_store_parse_firmware, FIRMWARE_LINKS, callback_to_firmware_list, FlipStoreViewLoader);
  182. }
  183. // Function to draw the message on the canvas with word wrapping
  184. static void draw_description(Canvas *canvas, const char *description, int x, int y)
  185. {
  186. if (description == NULL || strlen(description) == 0)
  187. {
  188. FURI_LOG_E(TAG, "User message is NULL.");
  189. return;
  190. }
  191. if (!canvas)
  192. {
  193. FURI_LOG_E(TAG, "Canvas is NULL.");
  194. return;
  195. }
  196. size_t msg_length = strlen(description);
  197. size_t start = 0;
  198. int line_num = 0;
  199. char line[MAX_LINE_LENGTH + 1]; // Buffer for the current line (+1 for null terminator)
  200. while (start < msg_length && line_num < 4)
  201. {
  202. size_t remaining = msg_length - start;
  203. size_t len = (remaining > MAX_LINE_LENGTH) ? MAX_LINE_LENGTH : remaining;
  204. if (remaining > MAX_LINE_LENGTH)
  205. {
  206. // Find the last space within the first 'len' characters
  207. size_t last_space = len;
  208. while (last_space > 0 && description[start + last_space - 1] != ' ')
  209. {
  210. last_space--;
  211. }
  212. if (last_space > 0)
  213. {
  214. len = last_space; // Adjust len to the position of the last space
  215. }
  216. }
  217. // Copy the substring to 'line' and null-terminate it
  218. memcpy(line, description + start, len);
  219. line[len] = '\0'; // Ensure the string is null-terminated
  220. // Draw the string on the canvas
  221. // Adjust the y-coordinate based on the line number
  222. canvas_draw_str_aligned(canvas, x, y + line_num * 10, AlignLeft, AlignTop, line);
  223. // Update the start position for the next line
  224. start += len;
  225. // Skip any spaces to avoid leading spaces on the next line
  226. while (start < msg_length && description[start] == ' ')
  227. {
  228. start++;
  229. }
  230. // Increment the line number
  231. line_num++;
  232. }
  233. }
  234. static void flip_store_view_draw_callback_app_list(Canvas *canvas, void *model)
  235. {
  236. UNUSED(model);
  237. canvas_clear(canvas);
  238. canvas_set_font(canvas, FontPrimary);
  239. char title[64];
  240. snprintf(title, 64, "%s (v.%s)", flip_catalog[app_selected_index].app_name, flip_catalog[app_selected_index].app_version);
  241. canvas_draw_str(canvas, 0, 10, title);
  242. canvas_set_font(canvas, FontSecondary);
  243. draw_description(canvas, flip_catalog[app_selected_index].app_description, 0, 13);
  244. if (flip_store_app_does_exist)
  245. {
  246. canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7);
  247. canvas_draw_str_aligned(canvas, 7, 54, AlignLeft, AlignTop, "Delete");
  248. canvas_draw_icon(canvas, 45, 53, &I_ButtonBACK_10x8);
  249. canvas_draw_str_aligned(canvas, 57, 54, AlignLeft, AlignTop, "Back");
  250. }
  251. else
  252. {
  253. canvas_draw_icon(canvas, 0, 53, &I_ButtonBACK_10x8);
  254. canvas_draw_str_aligned(canvas, 12, 54, AlignLeft, AlignTop, "Back");
  255. }
  256. canvas_draw_icon(canvas, 90, 53, &I_ButtonRight_4x7);
  257. canvas_draw_str_aligned(canvas, 97, 54, AlignLeft, AlignTop, "Install");
  258. }
  259. static bool flip_store_input_callback(InputEvent *event, void *context)
  260. {
  261. FlipStoreApp *app = (FlipStoreApp *)context;
  262. if (!app)
  263. {
  264. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  265. return false;
  266. }
  267. if (event->type == InputTypeShort)
  268. {
  269. if (event->key == InputKeyLeft && flip_store_app_does_exist)
  270. {
  271. // Left button clicked, delete the app
  272. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppDelete);
  273. return true;
  274. }
  275. if (event->key == InputKeyRight)
  276. {
  277. // Right button clicked, download the app
  278. flip_store_dl_app_switch_to_view(app);
  279. return true;
  280. }
  281. }
  282. else if (event->type == InputTypePress)
  283. {
  284. if (event->key == InputKeyBack)
  285. {
  286. // Back button clicked, switch to the previous view.
  287. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppListCategory);
  288. return true;
  289. }
  290. }
  291. return false;
  292. }
  293. static void flip_store_text_updated_ssid(void *context)
  294. {
  295. FlipStoreApp *app = (FlipStoreApp *)context;
  296. if (!app)
  297. {
  298. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  299. return;
  300. }
  301. // store the entered text
  302. strncpy(app->uart_text_input_buffer, app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size);
  303. // Ensure null-termination
  304. app->uart_text_input_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  305. // save the setting
  306. save_char("WiFi-SSID", app->uart_text_input_buffer);
  307. // update the variable item text
  308. if (app->variable_item_ssid)
  309. {
  310. variable_item_set_current_value_text(app->variable_item_ssid, app->uart_text_input_buffer);
  311. // get value of password
  312. char pass[64];
  313. if (load_char("WiFi-Password", pass, sizeof(pass)))
  314. {
  315. if (strlen(pass) > 0 && strlen(app->uart_text_input_buffer) > 0)
  316. {
  317. // save the settings
  318. save_settings(app->uart_text_input_buffer, pass);
  319. // initialize the http
  320. FlipperHTTP *fhttp = flipper_http_alloc();
  321. if (fhttp)
  322. {
  323. // save the wifi if the device is connected
  324. if (!flipper_http_save_wifi(fhttp, app->uart_text_input_buffer, pass))
  325. {
  326. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  327. }
  328. // free the resources
  329. flipper_http_free(fhttp);
  330. }
  331. else
  332. {
  333. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  334. }
  335. }
  336. }
  337. }
  338. // switch to the settings view
  339. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  340. }
  341. static void flip_store_text_updated_pass(void *context)
  342. {
  343. FlipStoreApp *app = (FlipStoreApp *)context;
  344. if (!app)
  345. {
  346. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  347. return;
  348. }
  349. // store the entered text
  350. strncpy(app->uart_text_input_buffer, app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size);
  351. // Ensure null-termination
  352. app->uart_text_input_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  353. // save the setting
  354. save_char("WiFi-Password", app->uart_text_input_buffer);
  355. // update the variable item text
  356. if (app->variable_item_pass)
  357. {
  358. // variable_item_set_current_value_text(app->variable_item_pass, app->uart_text_input_buffer);
  359. }
  360. // get value of ssid
  361. char ssid[64];
  362. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  363. {
  364. if (strlen(ssid) > 0 && strlen(app->uart_text_input_buffer) > 0)
  365. {
  366. // save the settings
  367. save_settings(ssid, app->uart_text_input_buffer);
  368. // initialize the http
  369. FlipperHTTP *fhttp = flipper_http_alloc();
  370. if (fhttp)
  371. {
  372. // save the wifi if the device is connected
  373. if (!flipper_http_save_wifi(fhttp, ssid, app->uart_text_input_buffer))
  374. {
  375. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  376. }
  377. // free the resources
  378. flipper_http_free(fhttp);
  379. }
  380. else
  381. {
  382. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  383. }
  384. }
  385. }
  386. // switch to the settings view
  387. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  388. }
  389. static void free_category_submenu(FlipStoreApp *app)
  390. {
  391. if (!app)
  392. {
  393. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  394. return;
  395. }
  396. if (app->submenu_app_list_category)
  397. {
  398. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAppListCategory);
  399. submenu_free(app->submenu_app_list_category);
  400. app->submenu_app_list_category = NULL;
  401. }
  402. }
  403. static void free_variable_item_list(FlipStoreApp *app);
  404. uint32_t callback_to_submenu(void *context)
  405. {
  406. UNUSED(context);
  407. firmware_free();
  408. return FlipStoreViewSubmenu;
  409. }
  410. uint32_t callback_to_firmware_list(void *context)
  411. {
  412. UNUSED(context);
  413. sent_firmware_request = false;
  414. sent_firmware_request_2 = false;
  415. sent_firmware_request_3 = false;
  416. //
  417. firmware_request_success = false;
  418. firmware_request_success_2 = false;
  419. firmware_request_success_3 = false;
  420. //
  421. firmware_download_success = false;
  422. firmware_download_success_2 = false;
  423. firmware_download_success_3 = false;
  424. return FlipStoreViewFirmwares;
  425. }
  426. static uint32_t callback_to_app_category_list(void *context)
  427. {
  428. UNUSED(context);
  429. return FlipStoreViewAppListCategory;
  430. }
  431. uint32_t callback_to_app_list(void *context)
  432. {
  433. UNUSED(context);
  434. flip_store_sent_request = false;
  435. flip_store_success = false;
  436. flip_store_saved_data = false;
  437. flip_store_saved_success = false;
  438. flip_store_app_does_exist = false;
  439. sent_firmware_request = false;
  440. return FlipStoreViewAppList;
  441. }
  442. static uint32_t callback_to_wifi_settings(void *context)
  443. {
  444. UNUSED(context);
  445. return FlipStoreViewSettings;
  446. }
  447. static void dialog_firmware_callback(DialogExResult result, void *context)
  448. {
  449. FlipStoreApp *app = (FlipStoreApp *)context;
  450. if (!app)
  451. {
  452. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  453. return;
  454. }
  455. if (result == DialogExResultLeft) // No
  456. {
  457. // switch to the firmware list
  458. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  459. }
  460. else if (result == DialogExResultRight)
  461. {
  462. // download the firmware then return to the firmware list
  463. flip_store_switch_to_firmware_list(app);
  464. }
  465. }
  466. static bool alloc_about_view(FlipStoreApp *app)
  467. {
  468. if (!app)
  469. {
  470. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  471. return false;
  472. }
  473. if (!app->widget_about)
  474. {
  475. if (!easy_flipper_set_widget(
  476. &app->widget_about,
  477. FlipStoreViewAbout,
  478. "Welcome to the FlipStore!\n------\nDownload apps via WiFi and\nrun them on your Flipper!\n------\nwww.github.com/jblanked",
  479. callback_to_submenu,
  480. &app->view_dispatcher))
  481. {
  482. return false;
  483. }
  484. if (!app->widget_about)
  485. {
  486. return false;
  487. }
  488. }
  489. return true;
  490. }
  491. static void free_about_view(FlipStoreApp *app)
  492. {
  493. if (app && app->widget_about != NULL)
  494. {
  495. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAbout);
  496. widget_free(app->widget_about);
  497. app->widget_about = NULL;
  498. }
  499. }
  500. static bool alloc_text_input_view(void *context, char *title)
  501. {
  502. FlipStoreApp *app = (FlipStoreApp *)context;
  503. if (!app)
  504. {
  505. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  506. return false;
  507. }
  508. if (!title)
  509. {
  510. FURI_LOG_E(TAG, "Title is NULL");
  511. return false;
  512. }
  513. app->uart_text_input_buffer_size = 64;
  514. if (!app->uart_text_input_buffer)
  515. {
  516. if (!easy_flipper_set_buffer(&app->uart_text_input_buffer, app->uart_text_input_buffer_size))
  517. {
  518. return false;
  519. }
  520. }
  521. if (!app->uart_text_input_temp_buffer)
  522. {
  523. if (!easy_flipper_set_buffer(&app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size))
  524. {
  525. return false;
  526. }
  527. }
  528. if (!app->uart_text_input)
  529. {
  530. if (!easy_flipper_set_uart_text_input(
  531. &app->uart_text_input,
  532. FlipStoreViewTextInput,
  533. title,
  534. app->uart_text_input_temp_buffer,
  535. app->uart_text_input_buffer_size,
  536. strcmp(title, "SSID") == 0 ? flip_store_text_updated_ssid : flip_store_text_updated_pass,
  537. callback_to_wifi_settings,
  538. &app->view_dispatcher,
  539. app))
  540. {
  541. return false;
  542. }
  543. if (!app->uart_text_input)
  544. {
  545. return false;
  546. }
  547. char ssid[64];
  548. char pass[64];
  549. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  550. {
  551. if (strcmp(title, "SSID") == 0)
  552. {
  553. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size);
  554. }
  555. else
  556. {
  557. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size);
  558. }
  559. }
  560. }
  561. return true;
  562. }
  563. static void free_text_input_view(FlipStoreApp *app)
  564. {
  565. if (!app)
  566. {
  567. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  568. return;
  569. }
  570. if (app->uart_text_input)
  571. {
  572. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewTextInput);
  573. uart_text_input_free(app->uart_text_input);
  574. app->uart_text_input = NULL;
  575. }
  576. if (app->uart_text_input_buffer)
  577. {
  578. free(app->uart_text_input_buffer);
  579. app->uart_text_input_buffer = NULL;
  580. }
  581. if (app->uart_text_input_temp_buffer)
  582. {
  583. free(app->uart_text_input_temp_buffer);
  584. app->uart_text_input_temp_buffer = NULL;
  585. }
  586. }
  587. static void settings_item_selected(void *context, uint32_t index);
  588. static bool alloc_variable_item_list(void *context)
  589. {
  590. FlipStoreApp *app = (FlipStoreApp *)context;
  591. if (!app)
  592. {
  593. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  594. return false;
  595. }
  596. if (!app->variable_item_list)
  597. {
  598. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipStoreViewSettings, settings_item_selected, callback_to_submenu, &app->view_dispatcher, app))
  599. return false;
  600. if (!app->variable_item_list)
  601. return false;
  602. if (!app->variable_item_ssid)
  603. {
  604. app->variable_item_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  605. variable_item_set_current_value_text(app->variable_item_ssid, "");
  606. }
  607. if (!app->variable_item_pass)
  608. {
  609. app->variable_item_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  610. variable_item_set_current_value_text(app->variable_item_pass, "");
  611. }
  612. char ssid[64];
  613. char pass[64];
  614. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  615. {
  616. variable_item_set_current_value_text(app->variable_item_ssid, ssid);
  617. // variable_item_set_current_value_text(app->variable_item_pass, pass);
  618. save_char("WiFi-SSID", ssid);
  619. save_char("WiFi-Password", pass);
  620. }
  621. }
  622. return true;
  623. }
  624. static void free_variable_item_list(FlipStoreApp *app)
  625. {
  626. if (!app)
  627. {
  628. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  629. return;
  630. }
  631. if (app->variable_item_list)
  632. {
  633. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewSettings);
  634. variable_item_list_free(app->variable_item_list);
  635. app->variable_item_list = NULL;
  636. }
  637. if (app->variable_item_ssid)
  638. {
  639. free(app->variable_item_ssid);
  640. app->variable_item_ssid = NULL;
  641. }
  642. if (app->variable_item_pass)
  643. {
  644. free(app->variable_item_pass);
  645. app->variable_item_pass = NULL;
  646. }
  647. }
  648. static bool alloc_dialog_firmware(void *context)
  649. {
  650. FlipStoreApp *app = (FlipStoreApp *)context;
  651. if (!app)
  652. {
  653. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  654. return false;
  655. }
  656. if (!app->dialog_firmware)
  657. {
  658. if (!easy_flipper_set_dialog_ex(
  659. &app->dialog_firmware,
  660. FlipStoreViewFirmwareDialog,
  661. "Download Firmware",
  662. 0,
  663. 0,
  664. "Are you sure you want to\ndownload this firmware?",
  665. 0,
  666. 10,
  667. "No",
  668. "Yes",
  669. NULL,
  670. dialog_firmware_callback,
  671. callback_to_firmware_list,
  672. &app->view_dispatcher,
  673. app))
  674. {
  675. return false;
  676. }
  677. if (!app->dialog_firmware)
  678. {
  679. return false;
  680. }
  681. dialog_ex_set_header(app->dialog_firmware, firmwares[selected_firmware_index].name, 0, 0, AlignLeft, AlignTop);
  682. }
  683. return true;
  684. }
  685. static void free_dialog_firmware(FlipStoreApp *app)
  686. {
  687. if (app && app->dialog_firmware)
  688. {
  689. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  690. dialog_ex_free(app->dialog_firmware);
  691. app->dialog_firmware = NULL;
  692. }
  693. }
  694. static bool alloc_app_info_view(FlipStoreApp *app)
  695. {
  696. if (!app)
  697. {
  698. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  699. return false;
  700. }
  701. if (!app->view_app_info)
  702. {
  703. if (!easy_flipper_set_view(
  704. &app->view_app_info,
  705. FlipStoreViewAppInfo,
  706. flip_store_view_draw_callback_app_list,
  707. flip_store_input_callback,
  708. callback_to_app_category_list,
  709. &app->view_dispatcher,
  710. app))
  711. {
  712. return false;
  713. }
  714. if (!app->view_app_info)
  715. {
  716. return false;
  717. }
  718. }
  719. return true;
  720. }
  721. static void free_app_info_view(FlipStoreApp *app)
  722. {
  723. if (!app)
  724. {
  725. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  726. return;
  727. }
  728. if (app->view_app_info)
  729. {
  730. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAppInfo);
  731. view_free(app->view_app_info);
  732. app->view_app_info = NULL;
  733. }
  734. }
  735. uint32_t callback_to_submenu_options(void *context)
  736. {
  737. FlipStoreApp *app = (FlipStoreApp *)context;
  738. if (!app)
  739. {
  740. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  741. return FlipStoreViewSubmenuOptions;
  742. }
  743. firmware_free();
  744. flip_catalog_free();
  745. free_category_submenu(app);
  746. return FlipStoreViewSubmenuOptions;
  747. }
  748. void free_all_views(FlipStoreApp *app, bool should_free_variable_item_list)
  749. {
  750. if (!app)
  751. {
  752. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  753. return;
  754. }
  755. free_about_view(app);
  756. flip_catalog_free();
  757. if (should_free_variable_item_list)
  758. {
  759. free_variable_item_list(app);
  760. }
  761. free_category_submenu(app);
  762. free_text_input_view(app);
  763. free_dialog_firmware(app);
  764. free_app_info_view(app);
  765. firmware_free();
  766. }
  767. uint32_t callback_exit_app(void *context)
  768. {
  769. UNUSED(context);
  770. return VIEW_NONE; // Return VIEW_NONE to exit the app
  771. }
  772. void callback_submenu_choices(void *context, uint32_t index)
  773. {
  774. FlipStoreApp *app = (FlipStoreApp *)context;
  775. if (!app)
  776. {
  777. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  778. return;
  779. }
  780. switch (index)
  781. {
  782. case FlipStoreSubmenuIndexAbout:
  783. free_all_views(app, true);
  784. if (!alloc_about_view(app))
  785. {
  786. FURI_LOG_E(TAG, "Failed to set about view");
  787. return;
  788. }
  789. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  790. break;
  791. case FlipStoreSubmenuIndexSettings:
  792. free_all_views(app, true);
  793. if (!alloc_variable_item_list(app))
  794. {
  795. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  796. return;
  797. }
  798. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  799. break;
  800. case FlipStoreSubmenuIndexOptions:
  801. free_all_views(app, true);
  802. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenuOptions);
  803. break;
  804. case FlipStoreSubmenuIndexAppList:
  805. flip_store_category_index = 0;
  806. flip_store_app_does_exist = false;
  807. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  808. break;
  809. case FlipStoreSubmenuIndexFirmwares:
  810. firmwares = firmware_alloc();
  811. if (firmwares == NULL)
  812. {
  813. FURI_LOG_E(TAG, "Failed to allocate memory for firmwares");
  814. return;
  815. }
  816. submenu_reset(app->submenu_firmwares);
  817. submenu_set_header(app->submenu_firmwares, "ESP32 Firmwares");
  818. for (int i = 0; i < FIRMWARE_COUNT; i++)
  819. {
  820. submenu_add_item(app->submenu_firmwares, firmwares[i].name, FlipStoreSubmenuIndexStartFirmwares + i, callback_submenu_choices, app);
  821. }
  822. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  823. break;
  824. case FlipStoreSubmenuIndexAppListBluetooth:
  825. free_all_views(app, true);
  826. flip_store_category_index = 0;
  827. flip_store_app_does_exist = false;
  828. flip_store_switch_to_app_list(app);
  829. break;
  830. case FlipStoreSubmenuIndexAppListGames:
  831. free_all_views(app, true);
  832. flip_store_category_index = 1;
  833. flip_store_app_does_exist = false;
  834. flip_store_switch_to_app_list(app);
  835. break;
  836. case FlipStoreSubmenuIndexAppListGPIO:
  837. free_all_views(app, true);
  838. flip_store_category_index = 2;
  839. flip_store_app_does_exist = false;
  840. flip_store_switch_to_app_list(app);
  841. break;
  842. case FlipStoreSubmenuIndexAppListInfrared:
  843. free_all_views(app, true);
  844. flip_store_category_index = 3;
  845. flip_store_app_does_exist = false;
  846. flip_store_switch_to_app_list(app);
  847. break;
  848. case FlipStoreSubmenuIndexAppListiButton:
  849. free_all_views(app, true);
  850. flip_store_category_index = 4;
  851. flip_store_app_does_exist = false;
  852. flip_store_switch_to_app_list(app);
  853. break;
  854. case FlipStoreSubmenuIndexAppListMedia:
  855. free_all_views(app, true);
  856. flip_store_category_index = 5;
  857. flip_store_app_does_exist = false;
  858. flip_store_switch_to_app_list(app);
  859. break;
  860. case FlipStoreSubmenuIndexAppListNFC:
  861. free_all_views(app, true);
  862. flip_store_category_index = 6;
  863. flip_store_app_does_exist = false;
  864. flip_store_switch_to_app_list(app);
  865. break;
  866. case FlipStoreSubmenuIndexAppListRFID:
  867. free_all_views(app, true);
  868. flip_store_category_index = 7;
  869. flip_store_app_does_exist = false;
  870. flip_store_switch_to_app_list(app);
  871. break;
  872. case FlipStoreSubmenuIndexAppListSubGHz:
  873. free_all_views(app, true);
  874. flip_store_category_index = 8;
  875. flip_store_app_does_exist = false;
  876. flip_store_switch_to_app_list(app);
  877. break;
  878. case FlipStoreSubmenuIndexAppListTools:
  879. free_all_views(app, true);
  880. flip_store_category_index = 9;
  881. flip_store_app_does_exist = false;
  882. flip_store_switch_to_app_list(app);
  883. break;
  884. case FlipStoreSubmenuIndexAppListUSB:
  885. free_all_views(app, true);
  886. flip_store_category_index = 10;
  887. flip_store_app_does_exist = false;
  888. flip_store_switch_to_app_list(app);
  889. break;
  890. default:
  891. // Check if the index is within the firmwares list range
  892. if (index >= FlipStoreSubmenuIndexStartFirmwares && index < FlipStoreSubmenuIndexStartFirmwares + 3)
  893. {
  894. // Get the firmware index
  895. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartFirmwares;
  896. // Check if the firmware index is valid
  897. if ((int)firmware_index >= 0 && firmware_index < FIRMWARE_COUNT)
  898. {
  899. // Get the firmware name
  900. selected_firmware_index = firmware_index;
  901. // Switch to the firmware download view
  902. free_dialog_firmware(app);
  903. if (!alloc_dialog_firmware(app))
  904. {
  905. FURI_LOG_E(TAG, "Failed to allocate dialog firmware");
  906. return;
  907. }
  908. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  909. }
  910. else
  911. {
  912. FURI_LOG_E(TAG, "Invalid firmware index");
  913. easy_flipper_dialog("Error", "Issue parsing firmware.");
  914. }
  915. }
  916. // Check if the index is within the app list range
  917. else if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + MAX_APP_COUNT)
  918. {
  919. // Get the app index
  920. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  921. // Check if the app index is valid
  922. if ((int)app_index >= 0 && app_index < MAX_APP_COUNT)
  923. {
  924. // Get the app name
  925. char *app_name = flip_catalog[app_index].app_name;
  926. // Check if the app name is valid
  927. if (app_name != NULL && strlen(app_name) > 0)
  928. {
  929. app_selected_index = app_index;
  930. flip_store_app_does_exist = app_exists(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]);
  931. free_app_info_view(app);
  932. if (!alloc_app_info_view(app))
  933. {
  934. FURI_LOG_E(TAG, "Failed to allocate app info view");
  935. return;
  936. }
  937. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  938. }
  939. else
  940. {
  941. FURI_LOG_E(TAG, "Invalid app name");
  942. }
  943. }
  944. else
  945. {
  946. FURI_LOG_E(TAG, "Invalid app index");
  947. }
  948. }
  949. else
  950. {
  951. FURI_LOG_E(TAG, "Unknown submenu index");
  952. }
  953. break;
  954. }
  955. }
  956. static void settings_item_selected(void *context, uint32_t index)
  957. {
  958. FlipStoreApp *app = (FlipStoreApp *)context;
  959. if (!app)
  960. {
  961. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  962. return;
  963. }
  964. char ssid[64];
  965. char pass[64];
  966. switch (index)
  967. {
  968. case 0: // Input SSID
  969. // Text Input
  970. free_all_views(app, false);
  971. if (!alloc_text_input_view(app, "SSID"))
  972. {
  973. FURI_LOG_E(TAG, "Failed to allocate text input view");
  974. return;
  975. }
  976. // load SSID
  977. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  978. {
  979. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size - 1);
  980. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  981. }
  982. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  983. break;
  984. case 1: // Input Password
  985. free_all_views(app, false);
  986. if (!alloc_text_input_view(app, "Password"))
  987. {
  988. FURI_LOG_E(TAG, "Failed to allocate text input view");
  989. return;
  990. }
  991. // load password
  992. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  993. {
  994. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size - 1);
  995. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  996. }
  997. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  998. break;
  999. default:
  1000. FURI_LOG_E(TAG, "Unknown configuration item index");
  1001. break;
  1002. }
  1003. }
  1004. static void flip_store_widget_set_text(char *message, Widget **widget)
  1005. {
  1006. if (widget == NULL)
  1007. {
  1008. FURI_LOG_E(TAG, "flip_store_set_widget_text - widget is NULL");
  1009. DEV_CRASH();
  1010. return;
  1011. }
  1012. if (message == NULL)
  1013. {
  1014. FURI_LOG_E(TAG, "flip_store_set_widget_text - message is NULL");
  1015. DEV_CRASH();
  1016. return;
  1017. }
  1018. widget_reset(*widget);
  1019. uint32_t message_length = strlen(message); // Length of the message
  1020. uint32_t i = 0; // Index tracker
  1021. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1022. char *formatted_message; // Buffer to hold the final formatted message
  1023. // Allocate buffer with double the message length plus one for safety
  1024. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1025. {
  1026. return;
  1027. }
  1028. while (i < message_length)
  1029. {
  1030. uint32_t max_line_length = 31; // Maximum characters per line
  1031. uint32_t remaining_length = message_length - i; // Remaining characters
  1032. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1033. // Check for newline character within the current segment
  1034. uint32_t newline_pos = i;
  1035. bool found_newline = false;
  1036. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1037. {
  1038. if (message[newline_pos] == '\n')
  1039. {
  1040. found_newline = true;
  1041. break;
  1042. }
  1043. }
  1044. if (found_newline)
  1045. {
  1046. // If newline found, set line_length up to the newline
  1047. line_length = newline_pos - i;
  1048. }
  1049. // Temporary buffer to hold the current line
  1050. char line[32];
  1051. strncpy(line, message + i, line_length);
  1052. line[line_length] = '\0';
  1053. // If newline was found, skip it for the next iteration
  1054. if (found_newline)
  1055. {
  1056. i += line_length + 1; // +1 to skip the '\n' character
  1057. }
  1058. else
  1059. {
  1060. // Check if the line ends in the middle of a word and adjust accordingly
  1061. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1062. {
  1063. // Find the last space within the current line to avoid breaking a word
  1064. char *last_space = strrchr(line, ' ');
  1065. if (last_space != NULL)
  1066. {
  1067. // Adjust the line_length to avoid cutting the word
  1068. line_length = last_space - line;
  1069. line[line_length] = '\0'; // Null-terminate at the space
  1070. }
  1071. }
  1072. // Move the index forward by the determined line_length
  1073. i += line_length;
  1074. // Skip any spaces at the beginning of the next line
  1075. while (i < message_length && message[i] == ' ')
  1076. {
  1077. i++;
  1078. }
  1079. }
  1080. // Manually copy the fixed line into the formatted_message buffer
  1081. for (uint32_t j = 0; j < line_length; j++)
  1082. {
  1083. formatted_message[formatted_index++] = line[j];
  1084. }
  1085. // Add a newline character for line spacing
  1086. formatted_message[formatted_index++] = '\n';
  1087. }
  1088. // Null-terminate the formatted_message
  1089. formatted_message[formatted_index] = '\0';
  1090. // Add the formatted message to the widget
  1091. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1092. }
  1093. static void flip_store_request_error(Canvas *canvas, FlipperHTTP *fhttp)
  1094. {
  1095. if (!canvas)
  1096. {
  1097. FURI_LOG_E(TAG, "Canvas is NULL");
  1098. return;
  1099. }
  1100. if (!fhttp)
  1101. {
  1102. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  1103. return;
  1104. }
  1105. if (fhttp->last_response != NULL)
  1106. {
  1107. if (strstr(fhttp->last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  1108. {
  1109. canvas_clear(canvas);
  1110. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1111. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1112. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1113. }
  1114. else if (strstr(fhttp->last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  1115. {
  1116. canvas_clear(canvas);
  1117. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1118. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1119. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1120. }
  1121. else
  1122. {
  1123. FURI_LOG_E(TAG, "Received an error: %s", fhttp->last_response);
  1124. canvas_draw_str(canvas, 0, 42, "Unusual error...");
  1125. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1126. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1127. }
  1128. }
  1129. else
  1130. {
  1131. canvas_clear(canvas);
  1132. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  1133. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1134. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1135. }
  1136. }
  1137. void flip_store_loader_draw_callback(Canvas *canvas, void *model)
  1138. {
  1139. if (!canvas || !model)
  1140. {
  1141. FURI_LOG_E(TAG, "flip_store_loader_draw_callback - canvas or model is NULL");
  1142. return;
  1143. }
  1144. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1145. SerialState http_state = data_loader_model->fhttp->state;
  1146. DataState data_state = data_loader_model->data_state;
  1147. char *title = data_loader_model->title;
  1148. canvas_set_font(canvas, FontSecondary);
  1149. if (http_state == INACTIVE)
  1150. {
  1151. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1152. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1153. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1154. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1155. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1156. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1157. return;
  1158. }
  1159. if (data_state == DataStateError || data_state == DataStateParseError)
  1160. {
  1161. flip_store_request_error(canvas, data_loader_model->fhttp);
  1162. return;
  1163. }
  1164. canvas_draw_str(canvas, 0, 7, title);
  1165. canvas_draw_str(canvas, 0, 17, "Loading...");
  1166. if (data_state == DataStateInitial)
  1167. {
  1168. return;
  1169. }
  1170. if (http_state == SENDING)
  1171. {
  1172. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1173. return;
  1174. }
  1175. if (http_state == RECEIVING || data_state == DataStateRequested)
  1176. {
  1177. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1178. return;
  1179. }
  1180. if (http_state == IDLE && data_state == DataStateReceived)
  1181. {
  1182. canvas_draw_str(canvas, 0, 27, "Processing...");
  1183. return;
  1184. }
  1185. if (http_state == IDLE && data_state == DataStateParsed)
  1186. {
  1187. canvas_draw_str(canvas, 0, 27, "Processed...");
  1188. return;
  1189. }
  1190. }
  1191. static void flip_store_loader_process_callback(void *context)
  1192. {
  1193. if (context == NULL)
  1194. {
  1195. FURI_LOG_E(TAG, "flip_store_loader_process_callback - context is NULL");
  1196. DEV_CRASH();
  1197. return;
  1198. }
  1199. FlipStoreApp *app = (FlipStoreApp *)context;
  1200. View *view = app->view_loader;
  1201. DataState current_data_state;
  1202. DataLoaderModel *loader_model = NULL;
  1203. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; loader_model = model; }, false);
  1204. if (!loader_model || !loader_model->fhttp)
  1205. {
  1206. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1207. DEV_CRASH();
  1208. return;
  1209. }
  1210. if (current_data_state == DataStateInitial)
  1211. {
  1212. with_view_model(
  1213. view,
  1214. DataLoaderModel * model,
  1215. {
  1216. model->data_state = DataStateRequested;
  1217. DataLoaderFetch fetch = model->fetcher;
  1218. if (fetch == NULL)
  1219. {
  1220. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1221. model->data_state = DataStateError;
  1222. return;
  1223. }
  1224. // Clear any previous responses
  1225. strncpy(model->fhttp->last_response, "", 1);
  1226. bool request_status = fetch(model);
  1227. if (!request_status)
  1228. {
  1229. model->data_state = DataStateError;
  1230. }
  1231. },
  1232. true);
  1233. }
  1234. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1235. {
  1236. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1237. {
  1238. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1239. {
  1240. FURI_LOG_DEV(TAG, "PONG received.");
  1241. }
  1242. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9) == 0)
  1243. {
  1244. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1245. }
  1246. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9) == 0)
  1247. {
  1248. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1249. }
  1250. else if (strlen(loader_model->fhttp->last_response) == 0)
  1251. {
  1252. // Still waiting on response
  1253. }
  1254. else
  1255. {
  1256. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1257. }
  1258. }
  1259. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1260. {
  1261. // continue waiting
  1262. }
  1263. else if (loader_model->fhttp->state == INACTIVE)
  1264. {
  1265. // inactive. try again
  1266. }
  1267. else if (loader_model->fhttp->state == ISSUE)
  1268. {
  1269. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1270. }
  1271. else
  1272. {
  1273. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1274. DEV_CRASH();
  1275. }
  1276. }
  1277. else if (current_data_state == DataStateReceived)
  1278. {
  1279. with_view_model(
  1280. view,
  1281. DataLoaderModel * model,
  1282. {
  1283. char *data_text;
  1284. if (model->parser == NULL)
  1285. {
  1286. data_text = NULL;
  1287. FURI_LOG_DEV(TAG, "Parser is NULL");
  1288. DEV_CRASH();
  1289. }
  1290. else
  1291. {
  1292. data_text = model->parser(model);
  1293. }
  1294. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1295. model->data_text = data_text;
  1296. if (data_text == NULL)
  1297. {
  1298. model->data_state = DataStateParseError;
  1299. }
  1300. else
  1301. {
  1302. model->data_state = DataStateParsed;
  1303. }
  1304. },
  1305. true);
  1306. }
  1307. else if (current_data_state == DataStateParsed)
  1308. {
  1309. with_view_model(
  1310. view,
  1311. DataLoaderModel * model,
  1312. {
  1313. if (++model->request_index < model->request_count)
  1314. {
  1315. model->data_state = DataStateInitial;
  1316. }
  1317. else
  1318. {
  1319. flip_store_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1320. if (model->data_text != NULL)
  1321. {
  1322. free(model->data_text);
  1323. model->data_text = NULL;
  1324. }
  1325. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1326. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewWidgetResult);
  1327. }
  1328. },
  1329. true);
  1330. }
  1331. }
  1332. static void flip_store_loader_timer_callback(void *context)
  1333. {
  1334. if (context == NULL)
  1335. {
  1336. FURI_LOG_E(TAG, "flip_store_loader_timer_callback - context is NULL");
  1337. DEV_CRASH();
  1338. return;
  1339. }
  1340. FlipStoreApp *app = (FlipStoreApp *)context;
  1341. view_dispatcher_send_custom_event(app->view_dispatcher, FlipStoreCustomEventProcess);
  1342. }
  1343. static void flip_store_loader_on_enter(void *context)
  1344. {
  1345. if (context == NULL)
  1346. {
  1347. FURI_LOG_E(TAG, "flip_store_loader_on_enter - context is NULL");
  1348. DEV_CRASH();
  1349. return;
  1350. }
  1351. FlipStoreApp *app = (FlipStoreApp *)context;
  1352. View *view = app->view_loader;
  1353. with_view_model(
  1354. view,
  1355. DataLoaderModel * model,
  1356. {
  1357. view_set_previous_callback(view, model->back_callback);
  1358. if (model->timer == NULL)
  1359. {
  1360. model->timer = furi_timer_alloc(flip_store_loader_timer_callback, FuriTimerTypePeriodic, app);
  1361. }
  1362. furi_timer_start(model->timer, 250);
  1363. },
  1364. true);
  1365. }
  1366. static void flip_store_loader_on_exit(void *context)
  1367. {
  1368. if (context == NULL)
  1369. {
  1370. FURI_LOG_E(TAG, "flip_store_loader_on_exit - context is NULL");
  1371. DEV_CRASH();
  1372. return;
  1373. }
  1374. FlipStoreApp *app = (FlipStoreApp *)context;
  1375. View *view = app->view_loader;
  1376. with_view_model(
  1377. view,
  1378. DataLoaderModel * model,
  1379. {
  1380. if (model->timer)
  1381. {
  1382. furi_timer_stop(model->timer);
  1383. }
  1384. },
  1385. false);
  1386. }
  1387. void flip_store_loader_init(View *view)
  1388. {
  1389. if (view == NULL)
  1390. {
  1391. FURI_LOG_E(TAG, "flip_store_loader_init - view is NULL");
  1392. DEV_CRASH();
  1393. return;
  1394. }
  1395. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1396. view_set_enter_callback(view, flip_store_loader_on_enter);
  1397. view_set_exit_callback(view, flip_store_loader_on_exit);
  1398. }
  1399. void flip_store_loader_free_model(View *view)
  1400. {
  1401. if (view == NULL)
  1402. {
  1403. FURI_LOG_E(TAG, "flip_store_loader_free_model - view is NULL");
  1404. DEV_CRASH();
  1405. return;
  1406. }
  1407. with_view_model(
  1408. view,
  1409. DataLoaderModel * model,
  1410. {
  1411. if (model->timer)
  1412. {
  1413. furi_timer_free(model->timer);
  1414. model->timer = NULL;
  1415. }
  1416. if (model->parser_context)
  1417. {
  1418. // do not free the context here, it is the app context
  1419. // free(model->parser_context);
  1420. // model->parser_context = NULL;
  1421. }
  1422. if (model->fhttp)
  1423. {
  1424. flipper_http_free(model->fhttp);
  1425. model->fhttp = NULL;
  1426. }
  1427. },
  1428. false);
  1429. }
  1430. bool flip_store_custom_event_callback(void *context, uint32_t index)
  1431. {
  1432. if (context == NULL)
  1433. {
  1434. FURI_LOG_E(TAG, "flip_store_custom_event_callback - context is NULL");
  1435. DEV_CRASH();
  1436. return false;
  1437. }
  1438. switch (index)
  1439. {
  1440. case FlipStoreCustomEventProcess:
  1441. flip_store_loader_process_callback(context);
  1442. return true;
  1443. default:
  1444. FURI_LOG_DEV(TAG, "flip_store_custom_event_callback. Unknown index: %ld", index);
  1445. return false;
  1446. }
  1447. }
  1448. void flip_store_generic_switch_to_view(FlipStoreApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  1449. {
  1450. if (app == NULL)
  1451. {
  1452. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - app is NULL");
  1453. DEV_CRASH();
  1454. return;
  1455. }
  1456. View *view = app->view_loader;
  1457. if (view == NULL)
  1458. {
  1459. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - view is NULL");
  1460. DEV_CRASH();
  1461. return;
  1462. }
  1463. with_view_model(
  1464. view,
  1465. DataLoaderModel * model,
  1466. {
  1467. model->title = title;
  1468. model->fetcher = fetcher;
  1469. model->parser = parser;
  1470. model->request_index = 0;
  1471. model->request_count = request_count;
  1472. model->back_callback = back;
  1473. model->data_state = DataStateInitial;
  1474. model->data_text = NULL;
  1475. //
  1476. model->parser_context = app;
  1477. if (!model->fhttp)
  1478. {
  1479. model->fhttp = flipper_http_alloc();
  1480. }
  1481. },
  1482. true);
  1483. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1484. }