flip_store_apps.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. #ifndef FLIP_STORE_APPS_H
  2. #define FLIP_STORE_APPS_H
  3. // Define maximum limits
  4. #define MAX_APP_NAME_LENGTH 32
  5. #define MAX_ID_LENGTH 32
  6. #define MAX_APP_COUNT 100
  7. typedef struct
  8. {
  9. char app_name[MAX_APP_NAME_LENGTH];
  10. char app_id[MAX_APP_NAME_LENGTH];
  11. char app_build_id[MAX_ID_LENGTH];
  12. } FlipStoreAppInfo;
  13. static FlipStoreAppInfo *flip_catalog = NULL;
  14. static uint32_t app_selected_index = 0;
  15. static bool flip_store_sent_request = false;
  16. static bool flip_store_success = false;
  17. static bool flip_store_saved_data = false;
  18. static bool flip_store_saved_success = false;
  19. static uint32_t flip_store_category_index = 0;
  20. enum ObjectState
  21. {
  22. OBJECT_EXPECT_KEY,
  23. OBJECT_EXPECT_COLON,
  24. OBJECT_EXPECT_VALUE,
  25. OBJECT_EXPECT_COMMA_OR_END
  26. };
  27. static void flip_catalog_free()
  28. {
  29. if (flip_catalog)
  30. {
  31. free(flip_catalog);
  32. flip_catalog = NULL;
  33. }
  34. }
  35. static bool flip_catalog_alloc()
  36. {
  37. if (!flip_catalog)
  38. {
  39. flip_catalog = (FlipStoreAppInfo *)malloc(MAX_APP_COUNT * sizeof(FlipStoreAppInfo));
  40. }
  41. if (!flip_catalog)
  42. {
  43. FURI_LOG_E(TAG, "Failed to allocate memory for flip_catalog.");
  44. return false;
  45. }
  46. return true;
  47. }
  48. // Utility function to parse JSON incrementally from a file
  49. static bool flip_store_process_app_list(const char *file_path)
  50. {
  51. if (file_path == NULL)
  52. {
  53. FURI_LOG_E(TAG, "JSON file path is NULL.");
  54. return false;
  55. }
  56. // initialize the flip_catalog
  57. if (!flip_catalog_alloc())
  58. {
  59. FURI_LOG_E(TAG, "Failed to allocate memory for flip_catalog.");
  60. return false;
  61. }
  62. Storage *_storage = NULL;
  63. File *_file = NULL;
  64. char buffer[BUFFER_SIZE];
  65. size_t bytes_read;
  66. bool in_string = false;
  67. bool is_escaped = false;
  68. bool reading_key = false;
  69. bool reading_value = false;
  70. bool inside_app_object = false;
  71. bool found_name = false;
  72. bool found_id = false;
  73. bool found_build_id = false;
  74. char current_key[MAX_KEY_LENGTH] = {0};
  75. size_t key_index = 0;
  76. char current_value[MAX_VALUE_LENGTH] = {0};
  77. size_t value_index = 0;
  78. int app_count = 0;
  79. enum ObjectState object_state = OBJECT_EXPECT_KEY; // Initialize object_state
  80. // Initialize parser state
  81. enum
  82. {
  83. STATE_SEARCH_APPS_KEY,
  84. STATE_SEARCH_ARRAY_START,
  85. STATE_READ_ARRAY_ELEMENTS,
  86. STATE_DONE
  87. } state = STATE_SEARCH_APPS_KEY;
  88. // Open storage and file
  89. _storage = furi_record_open(RECORD_STORAGE);
  90. if (!_storage)
  91. {
  92. FURI_LOG_E(TAG, "Failed to open storage.");
  93. return false;
  94. }
  95. _file = storage_file_alloc(_storage);
  96. if (!_file)
  97. {
  98. FURI_LOG_E(TAG, "Failed to allocate file.");
  99. furi_record_close(RECORD_STORAGE);
  100. return false;
  101. }
  102. if (!storage_file_open(_file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  103. {
  104. FURI_LOG_E(TAG, "Failed to open JSON file for reading.");
  105. storage_file_free(_file);
  106. furi_record_close(RECORD_STORAGE);
  107. return false;
  108. }
  109. while ((bytes_read = storage_file_read(_file, buffer, BUFFER_SIZE)) > 0 && state != STATE_DONE)
  110. {
  111. for (size_t i = 0; i < bytes_read; ++i)
  112. {
  113. char c = buffer[i];
  114. if (is_escaped)
  115. {
  116. is_escaped = false;
  117. if (reading_key)
  118. {
  119. if (key_index < MAX_KEY_LENGTH - 1)
  120. {
  121. current_key[key_index++] = c;
  122. }
  123. }
  124. else if (reading_value)
  125. {
  126. if (value_index < MAX_VALUE_LENGTH - 1)
  127. {
  128. current_value[value_index++] = c;
  129. }
  130. }
  131. continue;
  132. }
  133. if (c == '\\')
  134. {
  135. is_escaped = true;
  136. continue;
  137. }
  138. if (c == '\"')
  139. {
  140. in_string = !in_string;
  141. if (in_string)
  142. {
  143. // Start of a string
  144. if (!reading_key && !reading_value)
  145. {
  146. if (state == STATE_SEARCH_APPS_KEY)
  147. {
  148. reading_key = true;
  149. key_index = 0;
  150. current_key[0] = '\0';
  151. }
  152. else if (inside_app_object)
  153. {
  154. if (object_state == OBJECT_EXPECT_KEY)
  155. {
  156. reading_key = true;
  157. key_index = 0;
  158. current_key[0] = '\0';
  159. }
  160. else if (object_state == OBJECT_EXPECT_VALUE)
  161. {
  162. reading_value = true;
  163. value_index = 0;
  164. current_value[0] = '\0';
  165. }
  166. }
  167. }
  168. }
  169. else
  170. {
  171. // End of a string
  172. if (reading_key)
  173. {
  174. reading_key = false;
  175. current_key[key_index] = '\0';
  176. if (state == STATE_SEARCH_APPS_KEY && strcmp(current_key, "apps") == 0)
  177. {
  178. state = STATE_SEARCH_ARRAY_START;
  179. }
  180. else if (inside_app_object)
  181. {
  182. object_state = OBJECT_EXPECT_COLON;
  183. }
  184. }
  185. else if (reading_value)
  186. {
  187. reading_value = false;
  188. current_value[value_index] = '\0';
  189. if (inside_app_object)
  190. {
  191. if (strcmp(current_key, "name") == 0)
  192. {
  193. strncpy(flip_catalog[app_count].app_name, current_value, MAX_APP_NAME_LENGTH - 1);
  194. flip_catalog[app_count].app_name[MAX_APP_NAME_LENGTH - 1] = '\0';
  195. found_name = true;
  196. }
  197. else if (strcmp(current_key, "id") == 0)
  198. {
  199. strncpy(flip_catalog[app_count].app_id, current_value, MAX_APP_NAME_LENGTH - 1);
  200. flip_catalog[app_count].app_id[MAX_APP_NAME_LENGTH - 1] = '\0';
  201. found_id = true;
  202. }
  203. else if (strcmp(current_key, "build_id") == 0)
  204. {
  205. strncpy(flip_catalog[app_count].app_build_id, current_value, MAX_APP_NAME_LENGTH - 1);
  206. flip_catalog[app_count].app_build_id[MAX_ID_LENGTH - 1] = '\0';
  207. found_build_id = true;
  208. }
  209. // After processing value, expect comma or end
  210. object_state = OBJECT_EXPECT_COMMA_OR_END;
  211. // Check if both name and id are found
  212. if (found_name && found_id && found_build_id)
  213. {
  214. app_count++;
  215. if (app_count >= MAX_APP_COUNT)
  216. {
  217. FURI_LOG_I(TAG, "Reached maximum app count.");
  218. state = STATE_DONE;
  219. break;
  220. }
  221. // Reset for next app
  222. found_name = false;
  223. found_id = false;
  224. found_build_id = false;
  225. }
  226. }
  227. }
  228. }
  229. continue;
  230. }
  231. if (in_string)
  232. {
  233. if (reading_key)
  234. {
  235. if (key_index < MAX_KEY_LENGTH - 1)
  236. {
  237. current_key[key_index++] = c;
  238. }
  239. }
  240. else if (reading_value)
  241. {
  242. if (value_index < MAX_VALUE_LENGTH - 1)
  243. {
  244. current_value[value_index++] = c;
  245. }
  246. }
  247. continue;
  248. }
  249. // Not inside a string
  250. if (state == STATE_SEARCH_ARRAY_START)
  251. {
  252. if (c == '[')
  253. {
  254. state = STATE_READ_ARRAY_ELEMENTS;
  255. }
  256. continue;
  257. }
  258. if (state == STATE_READ_ARRAY_ELEMENTS)
  259. {
  260. if (c == '{')
  261. {
  262. inside_app_object = true;
  263. found_name = false;
  264. found_id = false;
  265. found_build_id = false;
  266. object_state = OBJECT_EXPECT_KEY;
  267. }
  268. else if (c == '}')
  269. {
  270. inside_app_object = false;
  271. object_state = OBJECT_EXPECT_KEY;
  272. }
  273. else if (c == ']')
  274. {
  275. state = STATE_DONE;
  276. break;
  277. }
  278. else if (c == ':')
  279. {
  280. if (inside_app_object && object_state == OBJECT_EXPECT_COLON)
  281. {
  282. object_state = OBJECT_EXPECT_VALUE;
  283. }
  284. }
  285. else if (c == ',')
  286. {
  287. if (inside_app_object && object_state == OBJECT_EXPECT_COMMA_OR_END)
  288. {
  289. object_state = OBJECT_EXPECT_KEY;
  290. }
  291. // Else, separator between objects or values
  292. }
  293. // Ignore other characters like whitespace, etc.
  294. continue;
  295. }
  296. }
  297. }
  298. // Clean up
  299. storage_file_close(_file);
  300. storage_file_free(_file);
  301. furi_record_close(RECORD_STORAGE);
  302. if (app_count == 0)
  303. {
  304. FURI_LOG_E(TAG, "No valid apps were parsed.");
  305. return false;
  306. }
  307. return true;
  308. }
  309. static bool flip_store_get_fap_file(char *build_id, uint8_t target, uint16_t api_major, uint16_t api_minor)
  310. {
  311. is_compile_app_request = true;
  312. char url[164];
  313. snprintf(url, sizeof(url), "https://catalog.flipperzero.one/api/v0/application/version/%s/build/compatible?target=f%d&api=%d.%d", build_id, target, api_major, api_minor);
  314. char *headers = jsmn("Content-Type", "application/octet-stream");
  315. bool sent_request = flipper_http_get_request_bytes(url, headers);
  316. free(headers);
  317. return sent_request;
  318. }
  319. static void flip_store_request_error(Canvas *canvas)
  320. {
  321. if (fhttp.received_data == NULL)
  322. {
  323. if (fhttp.last_response != NULL)
  324. {
  325. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  326. {
  327. canvas_clear(canvas);
  328. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  329. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  330. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  331. }
  332. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  333. {
  334. canvas_clear(canvas);
  335. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  336. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  337. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  338. }
  339. else
  340. {
  341. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  342. canvas_draw_str(canvas, 0, 42, "Unusual error...");
  343. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  344. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  345. }
  346. }
  347. else
  348. {
  349. canvas_clear(canvas);
  350. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  351. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  352. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  353. }
  354. }
  355. else
  356. {
  357. canvas_clear(canvas);
  358. canvas_draw_str(canvas, 0, 10, "Failed to receive data.");
  359. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  360. }
  361. }
  362. // function to handle the entire installation process "asynchronously"
  363. static bool flip_store_install_app(Canvas *canvas, char *category)
  364. {
  365. // create /apps/FlipStore directory if it doesn't exist
  366. char directory_path[128];
  367. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps/%s", category);
  368. // Create the directory
  369. Storage *storage = furi_record_open(RECORD_STORAGE);
  370. storage_common_mkdir(storage, directory_path);
  371. // Adjusted to access flip_catalog as an array of structures
  372. char *app_name = flip_catalog[app_selected_index].app_name;
  373. char installing_text[128];
  374. snprintf(installing_text, sizeof(installing_text), "Installing %s", app_name);
  375. char bin_path[256];
  376. snprintf(bin_path, sizeof(bin_path), STORAGE_EXT_PATH_PREFIX "/apps/%s/%s.fap", category, flip_catalog[app_selected_index].app_id);
  377. strncpy(fhttp.file_path, bin_path, sizeof(fhttp.file_path) - 1);
  378. canvas_draw_str(canvas, 0, 10, installing_text);
  379. canvas_draw_str(canvas, 0, 20, "Sending request..");
  380. uint8_t target = furi_hal_version_get_hw_target();
  381. uint16_t api_major, api_minor;
  382. furi_hal_info_get_api_version(&api_major, &api_minor);
  383. if (fhttp.state != INACTIVE && flip_store_get_fap_file(flip_catalog[app_selected_index].app_build_id, target, api_major, api_minor))
  384. {
  385. canvas_draw_str(canvas, 0, 30, "Request sent.");
  386. fhttp.state = RECEIVING;
  387. // furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  388. canvas_draw_str(canvas, 0, 40, "Receiving...");
  389. }
  390. else
  391. {
  392. FURI_LOG_E(TAG, "Failed to send the request");
  393. flip_store_success = false;
  394. return false;
  395. }
  396. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  397. {
  398. // Wait for the feed to be received
  399. furi_delay_ms(10);
  400. }
  401. // furi_timer_stop(fhttp.get_timeout_timer);
  402. if (fhttp.state == ISSUE || fhttp.received_data == NULL)
  403. {
  404. flip_store_request_error(canvas);
  405. flip_store_success = false;
  406. return false;
  407. }
  408. flip_store_success = true;
  409. return true;
  410. }
  411. // process the app list and return view
  412. static int32_t flip_store_handle_app_list(FlipStoreApp *app, int32_t success_view, char *category, Submenu **submenu)
  413. {
  414. // reset the flip_catalog
  415. if (flip_catalog)
  416. {
  417. flip_catalog_free();
  418. }
  419. if (!app)
  420. {
  421. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  422. return FlipStoreViewPopup;
  423. }
  424. char url[128];
  425. is_compile_app_request = false;
  426. // append the category to the end of the url
  427. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/flipper/apps/%s/extended/", category);
  428. // async call to the app list with timer
  429. if (fhttp.state != INACTIVE && flipper_http_get_request_with_headers(url, jsmn("Content-Type", "application/json")))
  430. {
  431. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  432. fhttp.state = RECEIVING;
  433. }
  434. else
  435. {
  436. FURI_LOG_E(TAG, "Failed to send the request");
  437. return FlipStoreViewPopup;
  438. }
  439. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  440. {
  441. // Wait for the feed to be received
  442. furi_delay_ms(100);
  443. }
  444. furi_timer_stop(fhttp.get_timeout_timer);
  445. if (fhttp.state == ISSUE || fhttp.received_data == NULL)
  446. {
  447. if (fhttp.received_data == NULL)
  448. {
  449. FURI_LOG_E(TAG, "Failed to receive data");
  450. if (fhttp.last_response != NULL)
  451. {
  452. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  453. {
  454. popup_set_text(app->popup, "[ERROR] WiFi Disconnected.\n\n\nUpdate your WiFi settings.\nPress BACK to return.", 0, 10, AlignLeft, AlignTop);
  455. }
  456. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  457. {
  458. popup_set_text(app->popup, "[ERROR] WiFi Disconnected.\n\n\nUpdate your WiFi settings.\nPress BACK to return.", 0, 10, AlignLeft, AlignTop);
  459. }
  460. else
  461. {
  462. popup_set_text(app->popup, fhttp.last_response, 0, 10, AlignLeft, AlignTop);
  463. }
  464. }
  465. else
  466. {
  467. popup_set_text(app->popup, "[ERROR] Unknown Error.\n\n\nUpdate your WiFi settings.\nPress BACK to return.", 0, 10, AlignLeft, AlignTop);
  468. }
  469. return FlipStoreViewPopup;
  470. }
  471. else
  472. {
  473. FURI_LOG_E(TAG, "Failed to receive data");
  474. popup_set_text(app->popup, "Failed to received data.", 0, 10, AlignLeft, AlignTop);
  475. return FlipStoreViewPopup;
  476. }
  477. }
  478. else
  479. {
  480. // process the app list
  481. const char *output_file_path = STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag "/received_data.txt";
  482. if (flip_store_process_app_list(output_file_path))
  483. {
  484. submenu_reset(*submenu);
  485. // add each app name to submenu
  486. for (int i = 0; i < MAX_APP_COUNT; i++)
  487. {
  488. if (strlen(flip_catalog[i].app_name) > 0)
  489. {
  490. submenu_add_item(*submenu, flip_catalog[i].app_name, FlipStoreSubmenuIndexStartAppList + i, callback_submenu_choices, app);
  491. }
  492. }
  493. return success_view;
  494. }
  495. else
  496. {
  497. FURI_LOG_E(TAG, "Failed to process the app list");
  498. popup_set_text(app->popup, "Failed to process the app list", 0, 10, AlignLeft, AlignTop);
  499. return FlipStoreViewPopup;
  500. }
  501. }
  502. }
  503. #endif // FLIP_STORE_APPS_H