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. uint32_t callback_to_submenu(void *context)
  404. {
  405. UNUSED(context);
  406. firmware_free();
  407. return FlipStoreViewSubmenu;
  408. }
  409. uint32_t callback_to_firmware_list(void *context)
  410. {
  411. UNUSED(context);
  412. sent_firmware_request = false;
  413. sent_firmware_request_2 = false;
  414. sent_firmware_request_3 = false;
  415. //
  416. firmware_request_success = false;
  417. firmware_request_success_2 = false;
  418. firmware_request_success_3 = false;
  419. //
  420. firmware_download_success = false;
  421. firmware_download_success_2 = false;
  422. firmware_download_success_3 = false;
  423. return FlipStoreViewFirmwares;
  424. }
  425. static uint32_t callback_to_app_category_list(void *context)
  426. {
  427. UNUSED(context);
  428. return FlipStoreViewAppListCategory;
  429. }
  430. uint32_t callback_to_app_list(void *context)
  431. {
  432. UNUSED(context);
  433. flip_store_sent_request = false;
  434. flip_store_success = false;
  435. flip_store_saved_data = false;
  436. flip_store_saved_success = false;
  437. flip_store_app_does_exist = false;
  438. sent_firmware_request = false;
  439. return FlipStoreViewAppList;
  440. }
  441. static uint32_t callback_to_wifi_settings(void *context)
  442. {
  443. UNUSED(context);
  444. return FlipStoreViewSettings;
  445. }
  446. static void dialog_firmware_callback(DialogExResult result, void *context)
  447. {
  448. FlipStoreApp *app = (FlipStoreApp *)context;
  449. if (!app)
  450. {
  451. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  452. return;
  453. }
  454. if (result == DialogExResultLeft) // No
  455. {
  456. // switch to the firmware list
  457. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  458. }
  459. else if (result == DialogExResultRight)
  460. {
  461. // download the firmware then return to the firmware list
  462. flip_store_switch_to_firmware_list(app);
  463. }
  464. }
  465. static bool alloc_about_view(FlipStoreApp *app)
  466. {
  467. if (!app)
  468. {
  469. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  470. return false;
  471. }
  472. if (!app->widget_about)
  473. {
  474. if (!easy_flipper_set_widget(
  475. &app->widget_about,
  476. FlipStoreViewAbout,
  477. "Welcome to the FlipStore!\n------\nDownload apps via WiFi and\nrun them on your Flipper!\n------\nwww.github.com/jblanked",
  478. callback_to_submenu,
  479. &app->view_dispatcher))
  480. {
  481. return false;
  482. }
  483. if (!app->widget_about)
  484. {
  485. return false;
  486. }
  487. }
  488. return true;
  489. }
  490. static void free_about_view(FlipStoreApp *app)
  491. {
  492. if (app && app->widget_about != NULL)
  493. {
  494. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAbout);
  495. widget_free(app->widget_about);
  496. app->widget_about = NULL;
  497. }
  498. }
  499. static bool alloc_text_input_view(void *context, char *title)
  500. {
  501. FlipStoreApp *app = (FlipStoreApp *)context;
  502. if (!app)
  503. {
  504. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  505. return false;
  506. }
  507. if (!title)
  508. {
  509. FURI_LOG_E(TAG, "Title is NULL");
  510. return false;
  511. }
  512. app->uart_text_input_buffer_size = 64;
  513. if (!app->uart_text_input_buffer)
  514. {
  515. if (!easy_flipper_set_buffer(&app->uart_text_input_buffer, app->uart_text_input_buffer_size))
  516. {
  517. return false;
  518. }
  519. }
  520. if (!app->uart_text_input_temp_buffer)
  521. {
  522. if (!easy_flipper_set_buffer(&app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size))
  523. {
  524. return false;
  525. }
  526. }
  527. if (!app->uart_text_input)
  528. {
  529. if (!easy_flipper_set_uart_text_input(
  530. &app->uart_text_input,
  531. FlipStoreViewTextInput,
  532. title,
  533. app->uart_text_input_temp_buffer,
  534. app->uart_text_input_buffer_size,
  535. strcmp(title, "SSID") == 0 ? flip_store_text_updated_ssid : flip_store_text_updated_pass,
  536. callback_to_wifi_settings,
  537. &app->view_dispatcher,
  538. app))
  539. {
  540. return false;
  541. }
  542. if (!app->uart_text_input)
  543. {
  544. return false;
  545. }
  546. char ssid[64];
  547. char pass[64];
  548. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  549. {
  550. if (strcmp(title, "SSID") == 0)
  551. {
  552. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size);
  553. }
  554. else
  555. {
  556. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size);
  557. }
  558. }
  559. }
  560. return true;
  561. }
  562. static void free_text_input_view(FlipStoreApp *app)
  563. {
  564. if (!app)
  565. {
  566. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  567. return;
  568. }
  569. if (app->uart_text_input)
  570. {
  571. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewTextInput);
  572. uart_text_input_free(app->uart_text_input);
  573. app->uart_text_input = NULL;
  574. }
  575. if (app->uart_text_input_buffer)
  576. {
  577. free(app->uart_text_input_buffer);
  578. app->uart_text_input_buffer = NULL;
  579. }
  580. if (app->uart_text_input_temp_buffer)
  581. {
  582. free(app->uart_text_input_temp_buffer);
  583. app->uart_text_input_temp_buffer = NULL;
  584. }
  585. }
  586. static void settings_item_selected(void *context, uint32_t index);
  587. static bool alloc_variable_item_list(void *context)
  588. {
  589. FlipStoreApp *app = (FlipStoreApp *)context;
  590. if (!app)
  591. {
  592. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  593. return false;
  594. }
  595. if (!app->variable_item_list)
  596. {
  597. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipStoreViewSettings, settings_item_selected, callback_to_submenu, &app->view_dispatcher, app))
  598. return false;
  599. if (!app->variable_item_list)
  600. return false;
  601. if (!app->variable_item_ssid)
  602. {
  603. app->variable_item_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  604. variable_item_set_current_value_text(app->variable_item_ssid, "");
  605. }
  606. if (!app->variable_item_pass)
  607. {
  608. app->variable_item_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  609. variable_item_set_current_value_text(app->variable_item_pass, "");
  610. }
  611. char ssid[64];
  612. char pass[64];
  613. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  614. {
  615. variable_item_set_current_value_text(app->variable_item_ssid, ssid);
  616. // variable_item_set_current_value_text(app->variable_item_pass, pass);
  617. save_char("WiFi-SSID", ssid);
  618. save_char("WiFi-Password", pass);
  619. }
  620. }
  621. return true;
  622. }
  623. static void free_variable_item_list(FlipStoreApp *app)
  624. {
  625. if (!app)
  626. {
  627. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  628. return;
  629. }
  630. if (app->variable_item_list)
  631. {
  632. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewSettings);
  633. variable_item_list_free(app->variable_item_list);
  634. app->variable_item_list = NULL;
  635. }
  636. if (app->variable_item_ssid)
  637. {
  638. free(app->variable_item_ssid);
  639. app->variable_item_ssid = NULL;
  640. }
  641. if (app->variable_item_pass)
  642. {
  643. free(app->variable_item_pass);
  644. app->variable_item_pass = NULL;
  645. }
  646. }
  647. static bool alloc_dialog_firmware(void *context)
  648. {
  649. FlipStoreApp *app = (FlipStoreApp *)context;
  650. if (!app)
  651. {
  652. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  653. return false;
  654. }
  655. FURI_LOG_I(TAG, "Allocating dialog_firmware");
  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. FURI_LOG_I(TAG, "Freeing dialog_firmware");
  690. // uncommenting below causes a furi-check failed
  691. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  692. dialog_ex_free(app->dialog_firmware);
  693. app->dialog_firmware = NULL;
  694. }
  695. }
  696. static bool alloc_app_info_view(FlipStoreApp *app)
  697. {
  698. if (!app)
  699. {
  700. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  701. return false;
  702. }
  703. if (!app->view_app_info)
  704. {
  705. if (!easy_flipper_set_view(
  706. &app->view_app_info,
  707. FlipStoreViewAppInfo,
  708. flip_store_view_draw_callback_app_list,
  709. flip_store_input_callback,
  710. callback_to_app_category_list,
  711. &app->view_dispatcher,
  712. app))
  713. {
  714. return false;
  715. }
  716. if (!app->view_app_info)
  717. {
  718. return false;
  719. }
  720. }
  721. return true;
  722. }
  723. static void free_app_info_view(FlipStoreApp *app)
  724. {
  725. if (!app)
  726. {
  727. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  728. return;
  729. }
  730. if (app->view_app_info)
  731. {
  732. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAppInfo);
  733. view_free(app->view_app_info);
  734. app->view_app_info = NULL;
  735. }
  736. }
  737. uint32_t callback_to_submenu_options(void *context)
  738. {
  739. // firmware_free();
  740. FlipStoreApp *app = (FlipStoreApp *)context;
  741. if (!app)
  742. {
  743. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  744. return FlipStoreViewSubmenuOptions;
  745. }
  746. free_category_submenu(app);
  747. // free_dialog_firmware(app);
  748. return FlipStoreViewSubmenuOptions;
  749. }
  750. static void free_all_views(FlipStoreApp *app, bool should_free_variable_item_list)
  751. {
  752. if (!app)
  753. {
  754. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  755. return;
  756. }
  757. free_about_view(app);
  758. flip_catalog_free();
  759. if (should_free_variable_item_list)
  760. {
  761. free_variable_item_list(app);
  762. }
  763. free_category_submenu(app);
  764. free_text_input_view(app);
  765. free_dialog_firmware(app);
  766. free_app_info_view(app);
  767. }
  768. uint32_t callback_exit_app(void *context)
  769. {
  770. UNUSED(context);
  771. return VIEW_NONE; // Return VIEW_NONE to exit the app
  772. }
  773. void callback_submenu_choices(void *context, uint32_t index)
  774. {
  775. FlipStoreApp *app = (FlipStoreApp *)context;
  776. if (!app)
  777. {
  778. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  779. return;
  780. }
  781. switch (index)
  782. {
  783. case FlipStoreSubmenuIndexAbout:
  784. free_all_views(app, true);
  785. if (!alloc_about_view(app))
  786. {
  787. FURI_LOG_E(TAG, "Failed to set about view");
  788. return;
  789. }
  790. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  791. break;
  792. case FlipStoreSubmenuIndexSettings:
  793. free_all_views(app, true);
  794. if (!alloc_variable_item_list(app))
  795. {
  796. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  797. return;
  798. }
  799. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  800. break;
  801. case FlipStoreSubmenuIndexOptions:
  802. free_all_views(app, true);
  803. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenuOptions);
  804. break;
  805. case FlipStoreSubmenuIndexAppList:
  806. flip_store_category_index = 0;
  807. flip_store_app_does_exist = false;
  808. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  809. break;
  810. case FlipStoreSubmenuIndexFirmwares:
  811. firmwares = firmware_alloc();
  812. if (firmwares == NULL)
  813. {
  814. FURI_LOG_E(TAG, "Failed to allocate memory for firmwares");
  815. return;
  816. }
  817. submenu_reset(app->submenu_firmwares);
  818. submenu_set_header(app->submenu_firmwares, "ESP32 Firmwares");
  819. for (int i = 0; i < FIRMWARE_COUNT; i++)
  820. {
  821. submenu_add_item(app->submenu_firmwares, firmwares[i].name, FlipStoreSubmenuIndexStartFirmwares + i, callback_submenu_choices, app);
  822. }
  823. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  824. break;
  825. case FlipStoreSubmenuIndexAppListBluetooth:
  826. free_all_views(app, true);
  827. flip_store_category_index = 0;
  828. flip_store_app_does_exist = false;
  829. flip_store_switch_to_app_list(app);
  830. break;
  831. case FlipStoreSubmenuIndexAppListGames:
  832. free_all_views(app, true);
  833. flip_store_category_index = 1;
  834. flip_store_app_does_exist = false;
  835. flip_store_switch_to_app_list(app);
  836. break;
  837. case FlipStoreSubmenuIndexAppListGPIO:
  838. free_all_views(app, true);
  839. flip_store_category_index = 2;
  840. flip_store_app_does_exist = false;
  841. flip_store_switch_to_app_list(app);
  842. break;
  843. case FlipStoreSubmenuIndexAppListInfrared:
  844. free_all_views(app, true);
  845. flip_store_category_index = 3;
  846. flip_store_app_does_exist = false;
  847. flip_store_switch_to_app_list(app);
  848. break;
  849. case FlipStoreSubmenuIndexAppListiButton:
  850. free_all_views(app, true);
  851. flip_store_category_index = 4;
  852. flip_store_app_does_exist = false;
  853. flip_store_switch_to_app_list(app);
  854. break;
  855. case FlipStoreSubmenuIndexAppListMedia:
  856. free_all_views(app, true);
  857. flip_store_category_index = 5;
  858. flip_store_app_does_exist = false;
  859. flip_store_switch_to_app_list(app);
  860. break;
  861. case FlipStoreSubmenuIndexAppListNFC:
  862. free_all_views(app, true);
  863. flip_store_category_index = 6;
  864. flip_store_app_does_exist = false;
  865. flip_store_switch_to_app_list(app);
  866. break;
  867. case FlipStoreSubmenuIndexAppListRFID:
  868. free_all_views(app, true);
  869. flip_store_category_index = 7;
  870. flip_store_app_does_exist = false;
  871. flip_store_switch_to_app_list(app);
  872. break;
  873. case FlipStoreSubmenuIndexAppListSubGHz:
  874. free_all_views(app, true);
  875. flip_store_category_index = 8;
  876. flip_store_app_does_exist = false;
  877. flip_store_switch_to_app_list(app);
  878. break;
  879. case FlipStoreSubmenuIndexAppListTools:
  880. free_all_views(app, true);
  881. flip_store_category_index = 9;
  882. flip_store_app_does_exist = false;
  883. flip_store_switch_to_app_list(app);
  884. break;
  885. case FlipStoreSubmenuIndexAppListUSB:
  886. free_all_views(app, true);
  887. flip_store_category_index = 10;
  888. flip_store_app_does_exist = false;
  889. flip_store_switch_to_app_list(app);
  890. break;
  891. default:
  892. // Check if the index is within the firmwares list range
  893. if (index >= FlipStoreSubmenuIndexStartFirmwares && index < FlipStoreSubmenuIndexStartFirmwares + 3)
  894. {
  895. // Get the firmware index
  896. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartFirmwares;
  897. // Check if the firmware index is valid
  898. if ((int)firmware_index >= 0 && firmware_index < FIRMWARE_COUNT)
  899. {
  900. // Get the firmware name
  901. selected_firmware_index = firmware_index;
  902. // Switch to the firmware download view
  903. free_dialog_firmware(app);
  904. if (!alloc_dialog_firmware(app))
  905. {
  906. FURI_LOG_E(TAG, "Failed to allocate dialog firmware");
  907. return;
  908. }
  909. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  910. }
  911. else
  912. {
  913. FURI_LOG_E(TAG, "Invalid firmware index");
  914. easy_flipper_dialog("Error", "Issue parsing firmware.");
  915. }
  916. }
  917. // Check if the index is within the app list range
  918. else if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + MAX_APP_COUNT)
  919. {
  920. // Get the app index
  921. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  922. // Check if the app index is valid
  923. if ((int)app_index >= 0 && app_index < MAX_APP_COUNT)
  924. {
  925. // Get the app name
  926. char *app_name = flip_catalog[app_index].app_name;
  927. // Check if the app name is valid
  928. if (app_name != NULL && strlen(app_name) > 0)
  929. {
  930. app_selected_index = app_index;
  931. flip_store_app_does_exist = app_exists(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]);
  932. free_app_info_view(app);
  933. if (!alloc_app_info_view(app))
  934. {
  935. FURI_LOG_E(TAG, "Failed to allocate app info view");
  936. return;
  937. }
  938. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  939. }
  940. else
  941. {
  942. FURI_LOG_E(TAG, "Invalid app name");
  943. }
  944. }
  945. else
  946. {
  947. FURI_LOG_E(TAG, "Invalid app index");
  948. }
  949. }
  950. else
  951. {
  952. FURI_LOG_E(TAG, "Unknown submenu index");
  953. }
  954. break;
  955. }
  956. }
  957. static void settings_item_selected(void *context, uint32_t index)
  958. {
  959. FlipStoreApp *app = (FlipStoreApp *)context;
  960. if (!app)
  961. {
  962. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  963. return;
  964. }
  965. char ssid[64];
  966. char pass[64];
  967. switch (index)
  968. {
  969. case 0: // Input SSID
  970. // Text Input
  971. free_all_views(app, false);
  972. if (!alloc_text_input_view(app, "SSID"))
  973. {
  974. FURI_LOG_E(TAG, "Failed to allocate text input view");
  975. return;
  976. }
  977. // load SSID
  978. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  979. {
  980. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size - 1);
  981. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  982. }
  983. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  984. break;
  985. case 1: // Input Password
  986. free_all_views(app, false);
  987. if (!alloc_text_input_view(app, "Password"))
  988. {
  989. FURI_LOG_E(TAG, "Failed to allocate text input view");
  990. return;
  991. }
  992. // load password
  993. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  994. {
  995. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size - 1);
  996. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  997. }
  998. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  999. break;
  1000. default:
  1001. FURI_LOG_E(TAG, "Unknown configuration item index");
  1002. break;
  1003. }
  1004. }
  1005. static void flip_store_widget_set_text(char *message, Widget **widget)
  1006. {
  1007. if (widget == NULL)
  1008. {
  1009. FURI_LOG_E(TAG, "flip_store_set_widget_text - widget is NULL");
  1010. DEV_CRASH();
  1011. return;
  1012. }
  1013. if (message == NULL)
  1014. {
  1015. FURI_LOG_E(TAG, "flip_store_set_widget_text - message is NULL");
  1016. DEV_CRASH();
  1017. return;
  1018. }
  1019. widget_reset(*widget);
  1020. uint32_t message_length = strlen(message); // Length of the message
  1021. uint32_t i = 0; // Index tracker
  1022. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1023. char *formatted_message; // Buffer to hold the final formatted message
  1024. // Allocate buffer with double the message length plus one for safety
  1025. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1026. {
  1027. return;
  1028. }
  1029. while (i < message_length)
  1030. {
  1031. uint32_t max_line_length = 31; // Maximum characters per line
  1032. uint32_t remaining_length = message_length - i; // Remaining characters
  1033. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1034. // Check for newline character within the current segment
  1035. uint32_t newline_pos = i;
  1036. bool found_newline = false;
  1037. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1038. {
  1039. if (message[newline_pos] == '\n')
  1040. {
  1041. found_newline = true;
  1042. break;
  1043. }
  1044. }
  1045. if (found_newline)
  1046. {
  1047. // If newline found, set line_length up to the newline
  1048. line_length = newline_pos - i;
  1049. }
  1050. // Temporary buffer to hold the current line
  1051. char line[32];
  1052. strncpy(line, message + i, line_length);
  1053. line[line_length] = '\0';
  1054. // If newline was found, skip it for the next iteration
  1055. if (found_newline)
  1056. {
  1057. i += line_length + 1; // +1 to skip the '\n' character
  1058. }
  1059. else
  1060. {
  1061. // Check if the line ends in the middle of a word and adjust accordingly
  1062. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1063. {
  1064. // Find the last space within the current line to avoid breaking a word
  1065. char *last_space = strrchr(line, ' ');
  1066. if (last_space != NULL)
  1067. {
  1068. // Adjust the line_length to avoid cutting the word
  1069. line_length = last_space - line;
  1070. line[line_length] = '\0'; // Null-terminate at the space
  1071. }
  1072. }
  1073. // Move the index forward by the determined line_length
  1074. i += line_length;
  1075. // Skip any spaces at the beginning of the next line
  1076. while (i < message_length && message[i] == ' ')
  1077. {
  1078. i++;
  1079. }
  1080. }
  1081. // Manually copy the fixed line into the formatted_message buffer
  1082. for (uint32_t j = 0; j < line_length; j++)
  1083. {
  1084. formatted_message[formatted_index++] = line[j];
  1085. }
  1086. // Add a newline character for line spacing
  1087. formatted_message[formatted_index++] = '\n';
  1088. }
  1089. // Null-terminate the formatted_message
  1090. formatted_message[formatted_index] = '\0';
  1091. // Add the formatted message to the widget
  1092. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1093. }
  1094. static void flip_store_request_error(Canvas *canvas, FlipperHTTP *fhttp)
  1095. {
  1096. if (!canvas)
  1097. {
  1098. FURI_LOG_E(TAG, "Canvas is NULL");
  1099. return;
  1100. }
  1101. if (!fhttp)
  1102. {
  1103. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  1104. return;
  1105. }
  1106. if (fhttp->last_response != NULL)
  1107. {
  1108. if (strstr(fhttp->last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  1109. {
  1110. canvas_clear(canvas);
  1111. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1112. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1113. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1114. }
  1115. else if (strstr(fhttp->last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  1116. {
  1117. canvas_clear(canvas);
  1118. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1119. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1120. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1121. }
  1122. else
  1123. {
  1124. FURI_LOG_E(TAG, "Received an error: %s", fhttp->last_response);
  1125. canvas_draw_str(canvas, 0, 42, "Unusual error...");
  1126. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1127. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1128. }
  1129. }
  1130. else
  1131. {
  1132. canvas_clear(canvas);
  1133. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  1134. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1135. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1136. }
  1137. }
  1138. void flip_store_loader_draw_callback(Canvas *canvas, void *model)
  1139. {
  1140. if (!canvas || !model)
  1141. {
  1142. FURI_LOG_E(TAG, "flip_store_loader_draw_callback - canvas or model is NULL");
  1143. return;
  1144. }
  1145. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1146. SerialState http_state = data_loader_model->fhttp->state;
  1147. DataState data_state = data_loader_model->data_state;
  1148. char *title = data_loader_model->title;
  1149. canvas_set_font(canvas, FontSecondary);
  1150. if (http_state == INACTIVE)
  1151. {
  1152. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1153. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1154. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1155. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1156. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1157. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1158. return;
  1159. }
  1160. if (data_state == DataStateError || data_state == DataStateParseError)
  1161. {
  1162. flip_store_request_error(canvas, data_loader_model->fhttp);
  1163. return;
  1164. }
  1165. canvas_draw_str(canvas, 0, 7, title);
  1166. canvas_draw_str(canvas, 0, 17, "Loading...");
  1167. if (data_state == DataStateInitial)
  1168. {
  1169. return;
  1170. }
  1171. if (http_state == SENDING)
  1172. {
  1173. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1174. return;
  1175. }
  1176. if (http_state == RECEIVING || data_state == DataStateRequested)
  1177. {
  1178. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1179. return;
  1180. }
  1181. if (http_state == IDLE && data_state == DataStateReceived)
  1182. {
  1183. canvas_draw_str(canvas, 0, 27, "Processing...");
  1184. return;
  1185. }
  1186. if (http_state == IDLE && data_state == DataStateParsed)
  1187. {
  1188. canvas_draw_str(canvas, 0, 27, "Processed...");
  1189. return;
  1190. }
  1191. }
  1192. static void flip_store_loader_process_callback(void *context)
  1193. {
  1194. if (context == NULL)
  1195. {
  1196. FURI_LOG_E(TAG, "flip_store_loader_process_callback - context is NULL");
  1197. DEV_CRASH();
  1198. return;
  1199. }
  1200. FlipStoreApp *app = (FlipStoreApp *)context;
  1201. View *view = app->view_loader;
  1202. DataState current_data_state;
  1203. DataLoaderModel *loader_model = NULL;
  1204. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; loader_model = model; }, false);
  1205. if (!loader_model || !loader_model->fhttp)
  1206. {
  1207. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1208. DEV_CRASH();
  1209. return;
  1210. }
  1211. if (current_data_state == DataStateInitial)
  1212. {
  1213. with_view_model(
  1214. view,
  1215. DataLoaderModel * model,
  1216. {
  1217. model->data_state = DataStateRequested;
  1218. DataLoaderFetch fetch = model->fetcher;
  1219. if (fetch == NULL)
  1220. {
  1221. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1222. model->data_state = DataStateError;
  1223. return;
  1224. }
  1225. // Clear any previous responses
  1226. strncpy(model->fhttp->last_response, "", 1);
  1227. bool request_status = fetch(model);
  1228. if (!request_status)
  1229. {
  1230. model->data_state = DataStateError;
  1231. }
  1232. },
  1233. true);
  1234. }
  1235. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1236. {
  1237. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1238. {
  1239. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1240. {
  1241. FURI_LOG_DEV(TAG, "PONG received.");
  1242. }
  1243. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9) == 0)
  1244. {
  1245. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1246. }
  1247. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9) == 0)
  1248. {
  1249. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1250. }
  1251. else if (strlen(loader_model->fhttp->last_response) == 0)
  1252. {
  1253. // Still waiting on response
  1254. }
  1255. else
  1256. {
  1257. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1258. }
  1259. }
  1260. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1261. {
  1262. // continue waiting
  1263. }
  1264. else if (loader_model->fhttp->state == INACTIVE)
  1265. {
  1266. // inactive. try again
  1267. }
  1268. else if (loader_model->fhttp->state == ISSUE)
  1269. {
  1270. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1271. }
  1272. else
  1273. {
  1274. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1275. DEV_CRASH();
  1276. }
  1277. }
  1278. else if (current_data_state == DataStateReceived)
  1279. {
  1280. with_view_model(
  1281. view,
  1282. DataLoaderModel * model,
  1283. {
  1284. char *data_text;
  1285. if (model->parser == NULL)
  1286. {
  1287. data_text = NULL;
  1288. FURI_LOG_DEV(TAG, "Parser is NULL");
  1289. DEV_CRASH();
  1290. }
  1291. else
  1292. {
  1293. data_text = model->parser(model);
  1294. }
  1295. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1296. model->data_text = data_text;
  1297. if (data_text == NULL)
  1298. {
  1299. model->data_state = DataStateParseError;
  1300. }
  1301. else
  1302. {
  1303. model->data_state = DataStateParsed;
  1304. }
  1305. },
  1306. true);
  1307. }
  1308. else if (current_data_state == DataStateParsed)
  1309. {
  1310. with_view_model(
  1311. view,
  1312. DataLoaderModel * model,
  1313. {
  1314. if (++model->request_index < model->request_count)
  1315. {
  1316. model->data_state = DataStateInitial;
  1317. }
  1318. else
  1319. {
  1320. flip_store_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1321. if (model->data_text != NULL)
  1322. {
  1323. free(model->data_text);
  1324. model->data_text = NULL;
  1325. }
  1326. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1327. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewWidgetResult);
  1328. }
  1329. },
  1330. true);
  1331. }
  1332. }
  1333. static void flip_store_loader_timer_callback(void *context)
  1334. {
  1335. if (context == NULL)
  1336. {
  1337. FURI_LOG_E(TAG, "flip_store_loader_timer_callback - context is NULL");
  1338. DEV_CRASH();
  1339. return;
  1340. }
  1341. FlipStoreApp *app = (FlipStoreApp *)context;
  1342. view_dispatcher_send_custom_event(app->view_dispatcher, FlipStoreCustomEventProcess);
  1343. }
  1344. static void flip_store_loader_on_enter(void *context)
  1345. {
  1346. if (context == NULL)
  1347. {
  1348. FURI_LOG_E(TAG, "flip_store_loader_on_enter - context is NULL");
  1349. DEV_CRASH();
  1350. return;
  1351. }
  1352. FlipStoreApp *app = (FlipStoreApp *)context;
  1353. View *view = app->view_loader;
  1354. with_view_model(
  1355. view,
  1356. DataLoaderModel * model,
  1357. {
  1358. view_set_previous_callback(view, model->back_callback);
  1359. if (model->timer == NULL)
  1360. {
  1361. model->timer = furi_timer_alloc(flip_store_loader_timer_callback, FuriTimerTypePeriodic, app);
  1362. }
  1363. furi_timer_start(model->timer, 250);
  1364. },
  1365. true);
  1366. }
  1367. static void flip_store_loader_on_exit(void *context)
  1368. {
  1369. if (context == NULL)
  1370. {
  1371. FURI_LOG_E(TAG, "flip_store_loader_on_exit - context is NULL");
  1372. DEV_CRASH();
  1373. return;
  1374. }
  1375. FlipStoreApp *app = (FlipStoreApp *)context;
  1376. View *view = app->view_loader;
  1377. with_view_model(
  1378. view,
  1379. DataLoaderModel * model,
  1380. {
  1381. if (model->timer)
  1382. {
  1383. furi_timer_stop(model->timer);
  1384. }
  1385. },
  1386. false);
  1387. }
  1388. void flip_store_loader_init(View *view)
  1389. {
  1390. if (view == NULL)
  1391. {
  1392. FURI_LOG_E(TAG, "flip_store_loader_init - view is NULL");
  1393. DEV_CRASH();
  1394. return;
  1395. }
  1396. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1397. view_set_enter_callback(view, flip_store_loader_on_enter);
  1398. view_set_exit_callback(view, flip_store_loader_on_exit);
  1399. }
  1400. void flip_store_loader_free_model(View *view)
  1401. {
  1402. if (view == NULL)
  1403. {
  1404. FURI_LOG_E(TAG, "flip_store_loader_free_model - view is NULL");
  1405. DEV_CRASH();
  1406. return;
  1407. }
  1408. with_view_model(
  1409. view,
  1410. DataLoaderModel * model,
  1411. {
  1412. if (model->timer)
  1413. {
  1414. furi_timer_free(model->timer);
  1415. model->timer = NULL;
  1416. }
  1417. if (model->parser_context)
  1418. {
  1419. // do not free the context here, it is the app context
  1420. // free(model->parser_context);
  1421. // model->parser_context = NULL;
  1422. }
  1423. if (model->fhttp)
  1424. {
  1425. flipper_http_free(model->fhttp);
  1426. model->fhttp = NULL;
  1427. }
  1428. },
  1429. false);
  1430. }
  1431. bool flip_store_custom_event_callback(void *context, uint32_t index)
  1432. {
  1433. if (context == NULL)
  1434. {
  1435. FURI_LOG_E(TAG, "flip_store_custom_event_callback - context is NULL");
  1436. DEV_CRASH();
  1437. return false;
  1438. }
  1439. switch (index)
  1440. {
  1441. case FlipStoreCustomEventProcess:
  1442. flip_store_loader_process_callback(context);
  1443. return true;
  1444. default:
  1445. FURI_LOG_DEV(TAG, "flip_store_custom_event_callback. Unknown index: %ld", index);
  1446. return false;
  1447. }
  1448. }
  1449. 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)
  1450. {
  1451. if (app == NULL)
  1452. {
  1453. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - app is NULL");
  1454. DEV_CRASH();
  1455. return;
  1456. }
  1457. View *view = app->view_loader;
  1458. if (view == NULL)
  1459. {
  1460. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - view is NULL");
  1461. DEV_CRASH();
  1462. return;
  1463. }
  1464. with_view_model(
  1465. view,
  1466. DataLoaderModel * model,
  1467. {
  1468. model->title = title;
  1469. model->fetcher = fetcher;
  1470. model->parser = parser;
  1471. model->request_index = 0;
  1472. model->request_count = request_count;
  1473. model->back_callback = back;
  1474. model->data_state = DataStateInitial;
  1475. model->data_text = NULL;
  1476. //
  1477. model->parser_context = app;
  1478. if (!model->fhttp)
  1479. {
  1480. model->fhttp = flipper_http_alloc();
  1481. }
  1482. },
  1483. true);
  1484. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1485. }