flip_store_callback.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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 bool flip_store_dl_app_fetch(DataLoaderModel *model)
  14. {
  15. UNUSED(model);
  16. return flip_store_install_app(categories[flip_store_category_index]);
  17. }
  18. static char *flip_store_dl_app_parse(DataLoaderModel *model)
  19. {
  20. UNUSED(model);
  21. if (fhttp.state != IDLE)
  22. {
  23. return NULL;
  24. }
  25. return "App installed successfully.";
  26. }
  27. static void flip_store_dl_app_switch_to_view(FlipStoreApp *app)
  28. {
  29. 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_list, FlipStoreViewLoader);
  30. }
  31. //
  32. static bool flip_store_fetch_app_list(DataLoaderModel *model)
  33. {
  34. UNUSED(model);
  35. flip_catalog_free();
  36. snprintf(
  37. fhttp.file_path,
  38. sizeof(fhttp.file_path),
  39. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/%s.json", categories[flip_store_category_index]);
  40. fhttp.save_received_data = true;
  41. fhttp.is_bytes_request = false;
  42. char url[128];
  43. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/flipper/apps/%s/max/", categories[flip_store_category_index]);
  44. return fhttp.state != INACTIVE && flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  45. }
  46. static char *flip_store_parse_app_list(DataLoaderModel *model)
  47. {
  48. UNUSED(model);
  49. if (!app_instance)
  50. {
  51. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  52. return "Failed to fetch app list.";
  53. }
  54. Submenu **submenu = NULL;
  55. uint32_t view_id = 0;
  56. switch (flip_store_category_index)
  57. {
  58. case 0:
  59. submenu = &app_instance->submenu_app_list_bluetooth;
  60. view_id = FlipStoreViewAppListBluetooth;
  61. break;
  62. case 1:
  63. submenu = &app_instance->submenu_app_list_games;
  64. view_id = FlipStoreViewAppListGames;
  65. break;
  66. case 2:
  67. submenu = &app_instance->submenu_app_list_gpio;
  68. view_id = FlipStoreViewAppListGPIO;
  69. break;
  70. case 3:
  71. submenu = &app_instance->submenu_app_list_infrared;
  72. view_id = FlipStoreViewAppListInfrared;
  73. break;
  74. case 4:
  75. submenu = &app_instance->submenu_app_list_ibutton;
  76. view_id = FlipStoreViewAppListiButton;
  77. break;
  78. case 5:
  79. submenu = &app_instance->submenu_app_list_media;
  80. view_id = FlipStoreViewAppListMedia;
  81. break;
  82. case 6:
  83. submenu = &app_instance->submenu_app_list_nfc;
  84. view_id = FlipStoreViewAppListNFC;
  85. break;
  86. case 7:
  87. submenu = &app_instance->submenu_app_list_rfid;
  88. view_id = FlipStoreViewAppListRFID;
  89. break;
  90. case 8:
  91. submenu = &app_instance->submenu_app_list_subghz;
  92. view_id = FlipStoreViewAppListSubGHz;
  93. break;
  94. case 9:
  95. submenu = &app_instance->submenu_app_list_tools;
  96. view_id = FlipStoreViewAppListTools;
  97. break;
  98. case 10:
  99. submenu = &app_instance->submenu_app_list_usb;
  100. view_id = FlipStoreViewAppListUSB;
  101. break;
  102. }
  103. if (!submenu)
  104. {
  105. FURI_LOG_E(TAG, "Submenu is NULL");
  106. return "Failed to fetch app list.";
  107. }
  108. if (!easy_flipper_set_submenu(submenu, view_id, categories[flip_store_category_index], callback_to_app_list, &app_instance->view_dispatcher))
  109. {
  110. return NULL;
  111. }
  112. if (flip_store_process_app_list() && submenu && flip_catalog)
  113. {
  114. submenu_reset(*submenu);
  115. // add each app name to submenu
  116. for (int i = 0; i < MAX_APP_COUNT; i++)
  117. {
  118. if (strlen(flip_catalog[i].app_name) > 0)
  119. {
  120. submenu_add_item(*submenu, flip_catalog[i].app_name, FlipStoreSubmenuIndexStartAppList + i, callback_submenu_choices, app_instance);
  121. }
  122. else
  123. {
  124. break;
  125. }
  126. }
  127. view_dispatcher_switch_to_view(app_instance->view_dispatcher, view_id);
  128. return "Fetched app list successfully.";
  129. }
  130. else
  131. {
  132. FURI_LOG_E(TAG, "Failed to process the app list");
  133. return "Failed to fetch app list.";
  134. }
  135. }
  136. static void flip_store_switch_to_app_list(FlipStoreApp *app)
  137. {
  138. 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);
  139. }
  140. //
  141. static bool flip_store_fetch_firmware(DataLoaderModel *model)
  142. {
  143. if (model->request_index == 0)
  144. {
  145. firmware_free();
  146. firmwares = firmware_alloc();
  147. if (!firmwares)
  148. {
  149. return false;
  150. }
  151. firmware_request_success = flip_store_get_firmware_file(
  152. firmwares[selected_firmware_index].links[0],
  153. firmwares[selected_firmware_index].name,
  154. strrchr(firmwares[selected_firmware_index].links[0], '/') + 1);
  155. return firmware_request_success;
  156. }
  157. else if (model->request_index == 1)
  158. {
  159. firmware_request_success_2 = flip_store_get_firmware_file(
  160. firmwares[selected_firmware_index].links[1],
  161. firmwares[selected_firmware_index].name,
  162. strrchr(firmwares[selected_firmware_index].links[1], '/') + 1);
  163. return firmware_request_success_2;
  164. }
  165. else if (model->request_index == 2)
  166. {
  167. firmware_request_success_3 = flip_store_get_firmware_file(
  168. firmwares[selected_firmware_index].links[2],
  169. firmwares[selected_firmware_index].name,
  170. strrchr(firmwares[selected_firmware_index].links[2], '/') + 1);
  171. return firmware_request_success_3;
  172. }
  173. return false;
  174. }
  175. static char *flip_store_parse_firmware(DataLoaderModel *model)
  176. {
  177. if (model->request_index == 0)
  178. {
  179. if (firmware_request_success)
  180. {
  181. return "File 1 installed.";
  182. }
  183. }
  184. else if (model->request_index == 1)
  185. {
  186. if (firmware_request_success_2)
  187. {
  188. return "File 2 installed.";
  189. }
  190. }
  191. else if (model->request_index == 2)
  192. {
  193. if (firmware_request_success_3)
  194. {
  195. return "Firmware downloaded successfully";
  196. }
  197. }
  198. return NULL;
  199. }
  200. static void flip_store_switch_to_firmware_list(FlipStoreApp *app)
  201. {
  202. flip_store_generic_switch_to_view(app, firmwares[selected_firmware_index].name, flip_store_fetch_firmware, flip_store_parse_firmware, FIRMWARE_LINKS, callback_to_submenu, FlipStoreViewLoader);
  203. }
  204. // Function to draw the message on the canvas with word wrapping
  205. void draw_description(Canvas *canvas, const char *description, int x, int y)
  206. {
  207. if (description == NULL || strlen(description) == 0)
  208. {
  209. FURI_LOG_E(TAG, "User message is NULL.");
  210. return;
  211. }
  212. if (!canvas)
  213. {
  214. FURI_LOG_E(TAG, "Canvas is NULL.");
  215. return;
  216. }
  217. size_t msg_length = strlen(description);
  218. size_t start = 0;
  219. int line_num = 0;
  220. char line[MAX_LINE_LENGTH + 1]; // Buffer for the current line (+1 for null terminator)
  221. while (start < msg_length && line_num < 4)
  222. {
  223. size_t remaining = msg_length - start;
  224. size_t len = (remaining > MAX_LINE_LENGTH) ? MAX_LINE_LENGTH : remaining;
  225. if (remaining > MAX_LINE_LENGTH)
  226. {
  227. // Find the last space within the first 'len' characters
  228. size_t last_space = len;
  229. while (last_space > 0 && description[start + last_space - 1] != ' ')
  230. {
  231. last_space--;
  232. }
  233. if (last_space > 0)
  234. {
  235. len = last_space; // Adjust len to the position of the last space
  236. }
  237. }
  238. // Copy the substring to 'line' and null-terminate it
  239. memcpy(line, description + start, len);
  240. line[len] = '\0'; // Ensure the string is null-terminated
  241. // Draw the string on the canvas
  242. // Adjust the y-coordinate based on the line number
  243. canvas_draw_str_aligned(canvas, x, y + line_num * 10, AlignLeft, AlignTop, line);
  244. // Update the start position for the next line
  245. start += len;
  246. // Skip any spaces to avoid leading spaces on the next line
  247. while (start < msg_length && description[start] == ' ')
  248. {
  249. start++;
  250. }
  251. // Increment the line number
  252. line_num++;
  253. }
  254. }
  255. void flip_store_view_draw_callback_app_list(Canvas *canvas, void *model)
  256. {
  257. UNUSED(model);
  258. canvas_clear(canvas);
  259. canvas_set_font(canvas, FontPrimary);
  260. char title[30];
  261. snprintf(title, 30, "%s (v.%s)", flip_catalog[app_selected_index].app_name, flip_catalog[app_selected_index].app_version);
  262. canvas_draw_str(canvas, 0, 10, title);
  263. canvas_set_font(canvas, FontSecondary);
  264. draw_description(canvas, flip_catalog[app_selected_index].app_description, 0, 13);
  265. if (flip_store_app_does_exist)
  266. {
  267. canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7);
  268. canvas_draw_str_aligned(canvas, 7, 54, AlignLeft, AlignTop, "Delete");
  269. canvas_draw_icon(canvas, 45, 53, &I_ButtonBACK_10x8);
  270. canvas_draw_str_aligned(canvas, 57, 54, AlignLeft, AlignTop, "Back");
  271. }
  272. else
  273. {
  274. canvas_draw_icon(canvas, 0, 53, &I_ButtonBACK_10x8);
  275. canvas_draw_str_aligned(canvas, 12, 54, AlignLeft, AlignTop, "Back");
  276. }
  277. canvas_draw_icon(canvas, 90, 53, &I_ButtonRight_4x7);
  278. canvas_draw_str_aligned(canvas, 97, 54, AlignLeft, AlignTop, "Install");
  279. }
  280. bool flip_store_input_callback(InputEvent *event, void *context)
  281. {
  282. FlipStoreApp *app = (FlipStoreApp *)context;
  283. if (!app)
  284. {
  285. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  286. return false;
  287. }
  288. if (event->type == InputTypeShort)
  289. {
  290. if (event->key == InputKeyLeft && flip_store_app_does_exist)
  291. {
  292. // Left button clicked, delete the app
  293. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppDelete);
  294. return true;
  295. }
  296. if (event->key == InputKeyRight)
  297. {
  298. // Right button clicked, download the app
  299. flip_store_dl_app_switch_to_view(app);
  300. return true;
  301. }
  302. }
  303. else if (event->type == InputTypePress)
  304. {
  305. if (event->key == InputKeyBack)
  306. {
  307. // Back button clicked, switch to the previous view.
  308. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  309. return true;
  310. }
  311. }
  312. return false;
  313. }
  314. void flip_store_text_updated_ssid(void *context)
  315. {
  316. FlipStoreApp *app = (FlipStoreApp *)context;
  317. if (!app)
  318. {
  319. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  320. return;
  321. }
  322. // store the entered text
  323. strncpy(app->uart_text_input_buffer_ssid, app->uart_text_input_temp_buffer_ssid, app->uart_text_input_buffer_size_ssid);
  324. // Ensure null-termination
  325. app->uart_text_input_buffer_ssid[app->uart_text_input_buffer_size_ssid - 1] = '\0';
  326. // update the variable item text
  327. if (app->variable_item_ssid)
  328. {
  329. variable_item_set_current_value_text(app->variable_item_ssid, app->uart_text_input_buffer_ssid);
  330. }
  331. // save the settings
  332. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass);
  333. // if SSID and PASS are not empty, connect to the WiFi
  334. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_pass) > 0)
  335. {
  336. // save wifi settings
  337. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass))
  338. {
  339. FURI_LOG_E(TAG, "Failed to save WiFi settings");
  340. }
  341. }
  342. // switch to the settings view
  343. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  344. }
  345. void flip_store_text_updated_pass(void *context)
  346. {
  347. FlipStoreApp *app = (FlipStoreApp *)context;
  348. if (!app)
  349. {
  350. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  351. return;
  352. }
  353. // store the entered text
  354. strncpy(app->uart_text_input_buffer_pass, app->uart_text_input_temp_buffer_pass, app->uart_text_input_buffer_size_pass);
  355. // Ensure null-termination
  356. app->uart_text_input_buffer_pass[app->uart_text_input_buffer_size_pass - 1] = '\0';
  357. // update the variable item text
  358. if (app->variable_item_pass)
  359. {
  360. variable_item_set_current_value_text(app->variable_item_pass, app->uart_text_input_buffer_pass);
  361. }
  362. // save the settings
  363. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass);
  364. // if SSID and PASS are not empty, connect to the WiFi
  365. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_pass) > 0)
  366. {
  367. // save wifi settings
  368. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass))
  369. {
  370. FURI_LOG_E(TAG, "Failed to save WiFi settings");
  371. }
  372. }
  373. // switch to the settings view
  374. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  375. }
  376. uint32_t callback_to_submenu(void *context)
  377. {
  378. if (!context)
  379. {
  380. FURI_LOG_E(TAG, "Context is NULL");
  381. return VIEW_NONE;
  382. }
  383. UNUSED(context);
  384. firmware_free();
  385. return FlipStoreViewSubmenu;
  386. }
  387. uint32_t callback_to_submenu_options(void *context)
  388. {
  389. if (!context)
  390. {
  391. FURI_LOG_E(TAG, "Context is NULL");
  392. return VIEW_NONE;
  393. }
  394. UNUSED(context);
  395. firmware_free();
  396. return FlipStoreViewSubmenuOptions;
  397. }
  398. uint32_t callback_to_firmware_list(void *context)
  399. {
  400. if (!context)
  401. {
  402. FURI_LOG_E(TAG, "Context is NULL");
  403. return VIEW_NONE;
  404. }
  405. UNUSED(context);
  406. sent_firmware_request = false;
  407. sent_firmware_request_2 = false;
  408. sent_firmware_request_3 = false;
  409. //
  410. firmware_request_success = false;
  411. firmware_request_success_2 = false;
  412. firmware_request_success_3 = false;
  413. //
  414. firmware_download_success = false;
  415. firmware_download_success_2 = false;
  416. firmware_download_success_3 = false;
  417. return FlipStoreViewFirmwares;
  418. }
  419. uint32_t callback_to_app_list(void *context)
  420. {
  421. if (!context)
  422. {
  423. FURI_LOG_E(TAG, "Context is NULL");
  424. return VIEW_NONE;
  425. }
  426. UNUSED(context);
  427. flip_store_sent_request = false;
  428. flip_store_success = false;
  429. flip_store_saved_data = false;
  430. flip_store_saved_success = false;
  431. flip_store_app_does_exist = false;
  432. sent_firmware_request = false;
  433. return FlipStoreViewAppList;
  434. }
  435. void settings_item_selected(void *context, uint32_t index)
  436. {
  437. FlipStoreApp *app = (FlipStoreApp *)context;
  438. if (!app)
  439. {
  440. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  441. return;
  442. }
  443. switch (index)
  444. {
  445. case 0: // Input SSID
  446. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInputSSID);
  447. break;
  448. case 1: // Input Password
  449. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInputPass);
  450. break;
  451. default:
  452. FURI_LOG_E(TAG, "Unknown configuration item index");
  453. break;
  454. }
  455. }
  456. void dialog_delete_callback(DialogExResult result, void *context)
  457. {
  458. furi_assert(context);
  459. FlipStoreApp *app = (FlipStoreApp *)context;
  460. if (result == DialogExResultLeft) // No
  461. {
  462. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  463. }
  464. else if (result == DialogExResultRight)
  465. {
  466. // delete the app then return to the app list
  467. if (!delete_app(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]))
  468. {
  469. // pop up a message
  470. popup_set_header(app->popup, "[ERROR]", 0, 0, AlignLeft, AlignTop);
  471. popup_set_text(app->popup, "Issue deleting app.", 0, 50, AlignLeft, AlignTop);
  472. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  473. furi_delay_ms(2000); // delay for 2 seconds
  474. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  475. }
  476. else
  477. {
  478. // pop up a message
  479. popup_set_header(app->popup, "[SUCCESS]", 0, 0, AlignLeft, AlignTop);
  480. popup_set_text(app->popup, "App deleted successfully.", 0, 50, AlignLeft, AlignTop);
  481. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  482. furi_delay_ms(2000); // delay for 2 seconds
  483. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  484. }
  485. }
  486. }
  487. void dialog_firmware_callback(DialogExResult result, void *context)
  488. {
  489. furi_assert(context);
  490. FlipStoreApp *app = (FlipStoreApp *)context;
  491. if (result == DialogExResultLeft) // No
  492. {
  493. // switch to the firmware list
  494. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  495. }
  496. else if (result == DialogExResultRight)
  497. {
  498. // download the firmware then return to the firmware list
  499. flip_store_switch_to_firmware_list(app);
  500. }
  501. }
  502. void popup_callback(void *context)
  503. {
  504. FlipStoreApp *app = (FlipStoreApp *)context;
  505. if (!app)
  506. {
  507. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  508. return;
  509. }
  510. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenu);
  511. }
  512. uint32_t callback_exit_app(void *context)
  513. {
  514. // Exit the application
  515. if (!context)
  516. {
  517. FURI_LOG_E(TAG, "Context is NULL");
  518. return VIEW_NONE;
  519. }
  520. UNUSED(context);
  521. return VIEW_NONE; // Return VIEW_NONE to exit the app
  522. }
  523. void callback_submenu_choices(void *context, uint32_t index)
  524. {
  525. FlipStoreApp *app = (FlipStoreApp *)context;
  526. if (!app)
  527. {
  528. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  529. return;
  530. }
  531. switch (index)
  532. {
  533. case FlipStoreSubmenuIndexAbout:
  534. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  535. break;
  536. case FlipStoreSubmenuIndexSettings:
  537. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  538. break;
  539. case FlipStoreSubmenuIndexOptions:
  540. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenuOptions);
  541. break;
  542. case FlipStoreSubmenuIndexAppList:
  543. flip_store_category_index = 0;
  544. flip_store_app_does_exist = false;
  545. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  546. break;
  547. case FlipStoreSubmenuIndexFirmwares:
  548. if (!app->submenu_firmwares)
  549. {
  550. FURI_LOG_E(TAG, "Submenu firmwares is NULL");
  551. return;
  552. }
  553. firmwares = firmware_alloc();
  554. if (firmwares == NULL)
  555. {
  556. FURI_LOG_E(TAG, "Failed to allocate memory for firmwares");
  557. return;
  558. }
  559. submenu_reset(app->submenu_firmwares);
  560. submenu_set_header(app->submenu_firmwares, "ESP32 Firmwares");
  561. for (int i = 0; i < FIRMWARE_COUNT; i++)
  562. {
  563. submenu_add_item(app->submenu_firmwares, firmwares[i].name, FlipStoreSubmenuIndexStartFirmwares + i, callback_submenu_choices, app);
  564. }
  565. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  566. break;
  567. case FlipStoreSubmenuIndexAppListBluetooth:
  568. flip_store_category_index = 0;
  569. flip_store_app_does_exist = false;
  570. flip_store_switch_to_app_list(app);
  571. break;
  572. case FlipStoreSubmenuIndexAppListGames:
  573. flip_store_category_index = 1;
  574. flip_store_app_does_exist = false;
  575. flip_store_switch_to_app_list(app);
  576. break;
  577. case FlipStoreSubmenuIndexAppListGPIO:
  578. flip_store_category_index = 2;
  579. flip_store_app_does_exist = false;
  580. flip_store_switch_to_app_list(app);
  581. break;
  582. case FlipStoreSubmenuIndexAppListInfrared:
  583. flip_store_category_index = 3;
  584. flip_store_app_does_exist = false;
  585. flip_store_switch_to_app_list(app);
  586. break;
  587. case FlipStoreSubmenuIndexAppListiButton:
  588. flip_store_category_index = 4;
  589. flip_store_app_does_exist = false;
  590. flip_store_switch_to_app_list(app);
  591. break;
  592. case FlipStoreSubmenuIndexAppListMedia:
  593. flip_store_category_index = 5;
  594. flip_store_app_does_exist = false;
  595. flip_store_switch_to_app_list(app);
  596. break;
  597. case FlipStoreSubmenuIndexAppListNFC:
  598. flip_store_category_index = 6;
  599. flip_store_app_does_exist = false;
  600. flip_store_switch_to_app_list(app);
  601. break;
  602. case FlipStoreSubmenuIndexAppListRFID:
  603. flip_store_category_index = 7;
  604. flip_store_app_does_exist = false;
  605. flip_store_switch_to_app_list(app);
  606. break;
  607. case FlipStoreSubmenuIndexAppListSubGHz:
  608. flip_store_category_index = 8;
  609. flip_store_app_does_exist = false;
  610. flip_store_switch_to_app_list(app);
  611. break;
  612. case FlipStoreSubmenuIndexAppListTools:
  613. flip_store_category_index = 9;
  614. flip_store_app_does_exist = false;
  615. flip_store_switch_to_app_list(app);
  616. break;
  617. case FlipStoreSubmenuIndexAppListUSB:
  618. flip_store_category_index = 10;
  619. flip_store_app_does_exist = false;
  620. flip_store_switch_to_app_list(app);
  621. break;
  622. default:
  623. // Check if the index is within the firmwares list range
  624. if (index >= FlipStoreSubmenuIndexStartFirmwares && index < FlipStoreSubmenuIndexStartFirmwares + 3)
  625. {
  626. // Get the firmware index
  627. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartFirmwares;
  628. // Check if the firmware index is valid
  629. if ((int)firmware_index >= 0 && firmware_index < FIRMWARE_COUNT)
  630. {
  631. // Get the firmware name
  632. selected_firmware_index = firmware_index;
  633. // Switch to the firmware download view
  634. dialog_ex_set_header(app->dialog_firmware, firmwares[firmware_index].name, 0, 0, AlignLeft, AlignTop);
  635. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  636. }
  637. else
  638. {
  639. FURI_LOG_E(TAG, "Invalid firmware index");
  640. popup_set_header(app->popup, "[ERROR]", 0, 0, AlignLeft, AlignTop);
  641. popup_set_text(app->popup, "Issue parsing firmwarex", 0, 50, AlignLeft, AlignTop);
  642. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  643. }
  644. }
  645. // Check if the index is within the app list range
  646. else if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + MAX_APP_COUNT)
  647. {
  648. // Get the app index
  649. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  650. // Check if the app index is valid
  651. if ((int)app_index >= 0 && app_index < MAX_APP_COUNT)
  652. {
  653. // Get the app name
  654. char *app_name = flip_catalog[app_index].app_name;
  655. // Check if the app name is valid
  656. if (app_name != NULL && strlen(app_name) > 0)
  657. {
  658. app_selected_index = app_index;
  659. flip_store_app_does_exist = app_exists(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]);
  660. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  661. }
  662. else
  663. {
  664. FURI_LOG_E(TAG, "Invalid app name");
  665. }
  666. }
  667. else
  668. {
  669. FURI_LOG_E(TAG, "Invalid app index");
  670. }
  671. }
  672. else
  673. {
  674. FURI_LOG_E(TAG, "Unknown submenu index");
  675. }
  676. break;
  677. }
  678. }
  679. static void flip_store_widget_set_text(char *message, Widget **widget)
  680. {
  681. if (widget == NULL)
  682. {
  683. FURI_LOG_E(TAG, "flip_store_set_widget_text - widget is NULL");
  684. DEV_CRASH();
  685. return;
  686. }
  687. if (message == NULL)
  688. {
  689. FURI_LOG_E(TAG, "flip_store_set_widget_text - message is NULL");
  690. DEV_CRASH();
  691. return;
  692. }
  693. widget_reset(*widget);
  694. uint32_t message_length = strlen(message); // Length of the message
  695. uint32_t i = 0; // Index tracker
  696. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  697. char *formatted_message; // Buffer to hold the final formatted message
  698. // Allocate buffer with double the message length plus one for safety
  699. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  700. {
  701. return;
  702. }
  703. while (i < message_length)
  704. {
  705. uint32_t max_line_length = 31; // Maximum characters per line
  706. uint32_t remaining_length = message_length - i; // Remaining characters
  707. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  708. // Check for newline character within the current segment
  709. uint32_t newline_pos = i;
  710. bool found_newline = false;
  711. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  712. {
  713. if (message[newline_pos] == '\n')
  714. {
  715. found_newline = true;
  716. break;
  717. }
  718. }
  719. if (found_newline)
  720. {
  721. // If newline found, set line_length up to the newline
  722. line_length = newline_pos - i;
  723. }
  724. // Temporary buffer to hold the current line
  725. char line[32];
  726. strncpy(line, message + i, line_length);
  727. line[line_length] = '\0';
  728. // If newline was found, skip it for the next iteration
  729. if (found_newline)
  730. {
  731. i += line_length + 1; // +1 to skip the '\n' character
  732. }
  733. else
  734. {
  735. // Check if the line ends in the middle of a word and adjust accordingly
  736. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  737. {
  738. // Find the last space within the current line to avoid breaking a word
  739. char *last_space = strrchr(line, ' ');
  740. if (last_space != NULL)
  741. {
  742. // Adjust the line_length to avoid cutting the word
  743. line_length = last_space - line;
  744. line[line_length] = '\0'; // Null-terminate at the space
  745. }
  746. }
  747. // Move the index forward by the determined line_length
  748. i += line_length;
  749. // Skip any spaces at the beginning of the next line
  750. while (i < message_length && message[i] == ' ')
  751. {
  752. i++;
  753. }
  754. }
  755. // Manually copy the fixed line into the formatted_message buffer
  756. for (uint32_t j = 0; j < line_length; j++)
  757. {
  758. formatted_message[formatted_index++] = line[j];
  759. }
  760. // Add a newline character for line spacing
  761. formatted_message[formatted_index++] = '\n';
  762. }
  763. // Null-terminate the formatted_message
  764. formatted_message[formatted_index] = '\0';
  765. // Add the formatted message to the widget
  766. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  767. }
  768. void flip_store_loader_draw_callback(Canvas *canvas, void *model)
  769. {
  770. if (!canvas || !model)
  771. {
  772. FURI_LOG_E(TAG, "flip_store_loader_draw_callback - canvas or model is NULL");
  773. return;
  774. }
  775. SerialState http_state = fhttp.state;
  776. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  777. DataState data_state = data_loader_model->data_state;
  778. char *title = data_loader_model->title;
  779. canvas_set_font(canvas, FontSecondary);
  780. if (http_state == INACTIVE)
  781. {
  782. canvas_draw_str(canvas, 0, 7, "WiFi Dev Board disconnected.");
  783. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  784. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  785. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  786. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  787. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  788. return;
  789. }
  790. if (data_state == DataStateError || data_state == DataStateParseError)
  791. {
  792. flip_store_request_error(canvas);
  793. return;
  794. }
  795. canvas_draw_str(canvas, 0, 7, title);
  796. canvas_draw_str(canvas, 0, 17, "Loading...");
  797. if (data_state == DataStateInitial)
  798. {
  799. return;
  800. }
  801. if (http_state == SENDING)
  802. {
  803. canvas_draw_str(canvas, 0, 27, "Fetching...");
  804. return;
  805. }
  806. if (http_state == RECEIVING || data_state == DataStateRequested)
  807. {
  808. canvas_draw_str(canvas, 0, 27, "Receiving...");
  809. return;
  810. }
  811. if (http_state == IDLE && data_state == DataStateReceived)
  812. {
  813. canvas_draw_str(canvas, 0, 27, "Processing...");
  814. return;
  815. }
  816. if (http_state == IDLE && data_state == DataStateParsed)
  817. {
  818. canvas_draw_str(canvas, 0, 27, "Processed...");
  819. return;
  820. }
  821. }
  822. static void flip_store_loader_process_callback(void *context)
  823. {
  824. if (context == NULL)
  825. {
  826. FURI_LOG_E(TAG, "flip_store_loader_process_callback - context is NULL");
  827. DEV_CRASH();
  828. return;
  829. }
  830. FlipStoreApp *app = (FlipStoreApp *)context;
  831. View *view = app->view_loader;
  832. DataState current_data_state;
  833. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  834. if (current_data_state == DataStateInitial)
  835. {
  836. with_view_model(
  837. view,
  838. DataLoaderModel * model,
  839. {
  840. model->data_state = DataStateRequested;
  841. DataLoaderFetch fetch = model->fetcher;
  842. if (fetch == NULL)
  843. {
  844. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  845. model->data_state = DataStateError;
  846. return;
  847. }
  848. // Clear any previous responses
  849. strncpy(fhttp.last_response, "", 1);
  850. bool request_status = fetch(model);
  851. if (!request_status)
  852. {
  853. model->data_state = DataStateError;
  854. }
  855. },
  856. true);
  857. }
  858. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  859. {
  860. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  861. {
  862. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  863. {
  864. FURI_LOG_DEV(TAG, "PONG received.");
  865. }
  866. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  867. {
  868. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  869. }
  870. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  871. {
  872. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  873. }
  874. else if (strlen(fhttp.last_response) == 0)
  875. {
  876. // Still waiting on response
  877. }
  878. else
  879. {
  880. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  881. }
  882. }
  883. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  884. {
  885. // continue waiting
  886. }
  887. else if (fhttp.state == INACTIVE)
  888. {
  889. // inactive. try again
  890. }
  891. else if (fhttp.state == ISSUE)
  892. {
  893. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  894. }
  895. else
  896. {
  897. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  898. DEV_CRASH();
  899. }
  900. }
  901. else if (current_data_state == DataStateReceived)
  902. {
  903. with_view_model(
  904. view,
  905. DataLoaderModel * model,
  906. {
  907. char *data_text;
  908. if (model->parser == NULL)
  909. {
  910. data_text = NULL;
  911. FURI_LOG_DEV(TAG, "Parser is NULL");
  912. DEV_CRASH();
  913. }
  914. else
  915. {
  916. data_text = model->parser(model);
  917. }
  918. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  919. model->data_text = data_text;
  920. if (data_text == NULL)
  921. {
  922. model->data_state = DataStateParseError;
  923. }
  924. else
  925. {
  926. model->data_state = DataStateParsed;
  927. }
  928. },
  929. true);
  930. }
  931. else if (current_data_state == DataStateParsed)
  932. {
  933. with_view_model(
  934. view,
  935. DataLoaderModel * model,
  936. {
  937. if (++model->request_index < model->request_count)
  938. {
  939. model->data_state = DataStateInitial;
  940. }
  941. else
  942. {
  943. flip_store_widget_set_text(model->data_text != NULL ? model->data_text : "Empty result", &app->widget_result);
  944. if (model->data_text != NULL)
  945. {
  946. free(model->data_text);
  947. model->data_text = NULL;
  948. }
  949. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  950. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewWidgetResult);
  951. }
  952. },
  953. true);
  954. }
  955. }
  956. static void flip_store_loader_timer_callback(void *context)
  957. {
  958. if (context == NULL)
  959. {
  960. FURI_LOG_E(TAG, "flip_store_loader_timer_callback - context is NULL");
  961. DEV_CRASH();
  962. return;
  963. }
  964. FlipStoreApp *app = (FlipStoreApp *)context;
  965. view_dispatcher_send_custom_event(app->view_dispatcher, FlipStoreCustomEventProcess);
  966. }
  967. static void flip_store_loader_on_enter(void *context)
  968. {
  969. if (context == NULL)
  970. {
  971. FURI_LOG_E(TAG, "flip_store_loader_on_enter - context is NULL");
  972. DEV_CRASH();
  973. return;
  974. }
  975. FlipStoreApp *app = (FlipStoreApp *)context;
  976. View *view = app->view_loader;
  977. with_view_model(
  978. view,
  979. DataLoaderModel * model,
  980. {
  981. view_set_previous_callback(view, model->back_callback);
  982. if (model->timer == NULL)
  983. {
  984. model->timer = furi_timer_alloc(flip_store_loader_timer_callback, FuriTimerTypePeriodic, app);
  985. }
  986. furi_timer_start(model->timer, 250);
  987. },
  988. true);
  989. }
  990. static void flip_store_loader_on_exit(void *context)
  991. {
  992. if (context == NULL)
  993. {
  994. FURI_LOG_E(TAG, "flip_store_loader_on_exit - context is NULL");
  995. DEV_CRASH();
  996. return;
  997. }
  998. FlipStoreApp *app = (FlipStoreApp *)context;
  999. View *view = app->view_loader;
  1000. with_view_model(
  1001. view,
  1002. DataLoaderModel * model,
  1003. {
  1004. if (model->timer)
  1005. {
  1006. furi_timer_stop(model->timer);
  1007. }
  1008. },
  1009. false);
  1010. }
  1011. void flip_store_loader_init(View *view)
  1012. {
  1013. if (view == NULL)
  1014. {
  1015. FURI_LOG_E(TAG, "flip_store_loader_init - view is NULL");
  1016. DEV_CRASH();
  1017. return;
  1018. }
  1019. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1020. view_set_enter_callback(view, flip_store_loader_on_enter);
  1021. view_set_exit_callback(view, flip_store_loader_on_exit);
  1022. }
  1023. void flip_store_loader_free_model(View *view)
  1024. {
  1025. if (view == NULL)
  1026. {
  1027. FURI_LOG_E(TAG, "flip_store_loader_free_model - view is NULL");
  1028. DEV_CRASH();
  1029. return;
  1030. }
  1031. with_view_model(
  1032. view,
  1033. DataLoaderModel * model,
  1034. {
  1035. if (model->timer)
  1036. {
  1037. furi_timer_free(model->timer);
  1038. model->timer = NULL;
  1039. }
  1040. if (model->parser_context)
  1041. {
  1042. free(model->parser_context);
  1043. model->parser_context = NULL;
  1044. }
  1045. },
  1046. false);
  1047. }
  1048. bool flip_store_custom_event_callback(void *context, uint32_t index)
  1049. {
  1050. if (context == NULL)
  1051. {
  1052. FURI_LOG_E(TAG, "flip_store_custom_event_callback - context is NULL");
  1053. DEV_CRASH();
  1054. return false;
  1055. }
  1056. switch (index)
  1057. {
  1058. case FlipStoreCustomEventProcess:
  1059. flip_store_loader_process_callback(context);
  1060. return true;
  1061. default:
  1062. FURI_LOG_DEV(TAG, "flip_store_custom_event_callback. Unknown index: %ld", index);
  1063. return false;
  1064. }
  1065. }
  1066. 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)
  1067. {
  1068. if (app == NULL)
  1069. {
  1070. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - app is NULL");
  1071. DEV_CRASH();
  1072. return;
  1073. }
  1074. View *view = app->view_loader;
  1075. if (view == NULL)
  1076. {
  1077. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - view is NULL");
  1078. DEV_CRASH();
  1079. return;
  1080. }
  1081. with_view_model(
  1082. view,
  1083. DataLoaderModel * model,
  1084. {
  1085. model->title = title;
  1086. model->fetcher = fetcher;
  1087. model->parser = parser;
  1088. model->request_index = 0;
  1089. model->request_count = request_count;
  1090. model->back_callback = back;
  1091. model->data_state = DataStateInitial;
  1092. model->data_text = NULL;
  1093. },
  1094. true);
  1095. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1096. }