flip_store_callback.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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. 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. free_dialog_firmware(app);
  747. return FlipStoreViewSubmenuOptions;
  748. }
  749. static void free_all_views(FlipStoreApp *app, bool should_free_variable_item_list)
  750. {
  751. if (!app)
  752. {
  753. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  754. return;
  755. }
  756. free_about_view(app);
  757. flip_catalog_free();
  758. if (should_free_variable_item_list)
  759. {
  760. free_variable_item_list(app);
  761. }
  762. free_category_submenu(app);
  763. free_text_input_view(app);
  764. free_dialog_firmware(app);
  765. free_app_info_view(app);
  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. }