flip_store_apps.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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, char *target, char *api)
  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=%s&api=%s", build_id, target, api);
  314. return flipper_http_get_request_bytes(url, jsmn("Content-Type", "application/octet-stream"));
  315. }
  316. static void flip_store_request_error(Canvas *canvas)
  317. {
  318. if (fhttp.received_data == NULL)
  319. {
  320. if (fhttp.last_response != NULL)
  321. {
  322. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  323. {
  324. canvas_clear(canvas);
  325. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  326. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  327. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  328. }
  329. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  330. {
  331. canvas_clear(canvas);
  332. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  333. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  334. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  335. }
  336. else
  337. {
  338. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  339. canvas_draw_str(canvas, 0, 42, "Unusual error...");
  340. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  341. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  342. }
  343. }
  344. else
  345. {
  346. canvas_clear(canvas);
  347. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  348. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  349. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  350. }
  351. }
  352. else
  353. {
  354. canvas_clear(canvas);
  355. canvas_draw_str(canvas, 0, 10, "Failed to receive data.");
  356. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  357. }
  358. }
  359. // function to handle the entire installation process "asynchronously"
  360. static bool flip_store_install_app(Canvas *canvas, char *category)
  361. {
  362. // create /apps/FlipStore directory if it doesn't exist
  363. char directory_path[128];
  364. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps/%s", category);
  365. // Create the directory
  366. Storage *storage = furi_record_open(RECORD_STORAGE);
  367. storage_common_mkdir(storage, directory_path);
  368. // Adjusted to access flip_catalog as an array of structures
  369. char *app_name = flip_catalog[app_selected_index].app_name;
  370. char installing_text[128];
  371. snprintf(installing_text, sizeof(installing_text), "Installing %s", app_name);
  372. char bin_path[256];
  373. snprintf(bin_path, sizeof(bin_path), STORAGE_EXT_PATH_PREFIX "/apps/%s/%s.fap", category, flip_catalog[app_selected_index].app_id);
  374. strncpy(fhttp.file_path, bin_path, sizeof(fhttp.file_path) - 1);
  375. canvas_draw_str(canvas, 0, 10, installing_text);
  376. canvas_draw_str(canvas, 0, 20, "Sending request..");
  377. if (fhttp.state != INACTIVE && flip_store_get_fap_file(flip_catalog[app_selected_index].app_build_id, "f7", "73.0"))
  378. {
  379. canvas_draw_str(canvas, 0, 30, "Request sent.");
  380. fhttp.state = RECEIVING;
  381. // furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  382. canvas_draw_str(canvas, 0, 40, "Receiving...");
  383. }
  384. else
  385. {
  386. FURI_LOG_E(TAG, "Failed to send the request");
  387. flip_store_success = false;
  388. return false;
  389. }
  390. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  391. {
  392. // Wait for the feed to be received
  393. furi_delay_ms(10);
  394. }
  395. // furi_timer_stop(fhttp.get_timeout_timer);
  396. if (fhttp.state == ISSUE || fhttp.received_data == NULL)
  397. {
  398. flip_store_request_error(canvas);
  399. flip_store_success = false;
  400. return false;
  401. }
  402. flip_store_success = true;
  403. return true;
  404. }
  405. // process the app list and return view
  406. static int32_t flip_store_handle_app_list(FlipStoreApp *app, int32_t success_view, char *category, Submenu **submenu)
  407. {
  408. // reset the flip_catalog
  409. if (flip_catalog)
  410. {
  411. flip_catalog_free();
  412. }
  413. if (!app)
  414. {
  415. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  416. return FlipStoreViewPopup;
  417. }
  418. char url[128];
  419. is_compile_app_request = false;
  420. // append the category to the end of the url
  421. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/flipper/apps/%s/extended/", category);
  422. // async call to the app list with timer
  423. if (fhttp.state != INACTIVE && flipper_http_get_request_with_headers(url, jsmn("Content-Type", "application/json")))
  424. {
  425. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  426. fhttp.state = RECEIVING;
  427. }
  428. else
  429. {
  430. FURI_LOG_E(TAG, "Failed to send the request");
  431. return FlipStoreViewPopup;
  432. }
  433. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  434. {
  435. // Wait for the feed to be received
  436. furi_delay_ms(100);
  437. }
  438. furi_timer_stop(fhttp.get_timeout_timer);
  439. if (fhttp.state == ISSUE || fhttp.received_data == NULL)
  440. {
  441. if (fhttp.received_data == NULL)
  442. {
  443. FURI_LOG_E(TAG, "Failed to receive data");
  444. if (fhttp.last_response != NULL)
  445. {
  446. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  447. {
  448. popup_set_text(app->popup, "[ERROR] WiFi Disconnected.\n\n\nUpdate your WiFi settings.\nPress BACK to return.", 0, 10, AlignLeft, AlignTop);
  449. }
  450. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  451. {
  452. popup_set_text(app->popup, "[ERROR] WiFi Disconnected.\n\n\nUpdate your WiFi settings.\nPress BACK to return.", 0, 10, AlignLeft, AlignTop);
  453. }
  454. else
  455. {
  456. popup_set_text(app->popup, fhttp.last_response, 0, 10, AlignLeft, AlignTop);
  457. }
  458. }
  459. else
  460. {
  461. popup_set_text(app->popup, "[ERROR] Unknown Error.\n\n\nUpdate your WiFi settings.\nPress BACK to return.", 0, 10, AlignLeft, AlignTop);
  462. }
  463. return FlipStoreViewPopup;
  464. }
  465. else
  466. {
  467. FURI_LOG_E(TAG, "Failed to receive data");
  468. popup_set_text(app->popup, "Failed to received data.", 0, 10, AlignLeft, AlignTop);
  469. return FlipStoreViewPopup;
  470. }
  471. }
  472. else
  473. {
  474. // process the app list
  475. const char *output_file_path = STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag "/received_data.txt";
  476. if (flip_store_process_app_list(output_file_path))
  477. {
  478. submenu_reset(*submenu);
  479. // add each app name to submenu
  480. for (int i = 0; i < MAX_APP_COUNT; i++)
  481. {
  482. if (strlen(flip_catalog[i].app_name) > 0)
  483. {
  484. submenu_add_item(*submenu, flip_catalog[i].app_name, FlipStoreSubmenuIndexStartAppList + i, callback_submenu_choices, app);
  485. }
  486. }
  487. return success_view;
  488. }
  489. else
  490. {
  491. FURI_LOG_E(TAG, "Failed to process the app list");
  492. popup_set_text(app->popup, "Failed to process the app list", 0, 10, AlignLeft, AlignTop);
  493. return FlipStoreViewPopup;
  494. }
  495. }
  496. }
  497. #endif // FLIP_STORE_APPS_H