flip_store_apps.h 18 KB

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