flip_store_apps.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. #include <apps/flip_store_apps.h>
  2. FlipStoreAppInfo *flip_catalog = NULL;
  3. uint32_t app_selected_index = 0;
  4. bool flip_store_sent_request = false;
  5. bool flip_store_success = false;
  6. bool flip_store_saved_data = false;
  7. bool flip_store_saved_success = false;
  8. uint32_t flip_store_category_index = 0;
  9. // define the list of categories
  10. char *categories[] = {
  11. "Bluetooth",
  12. "Games",
  13. "GPIO",
  14. "Infrared",
  15. "iButton",
  16. "Media",
  17. "NFC",
  18. "RFID",
  19. "Sub-GHz",
  20. "Tools",
  21. "USB",
  22. };
  23. FlipStoreAppInfo *flip_catalog_alloc()
  24. {
  25. FlipStoreAppInfo *app_catalog = (FlipStoreAppInfo *)malloc(MAX_APP_COUNT * sizeof(FlipStoreAppInfo));
  26. if (!app_catalog)
  27. {
  28. FURI_LOG_E(TAG, "Failed to allocate memory for flip_catalog.");
  29. return NULL;
  30. }
  31. for (int i = 0; i < MAX_APP_COUNT; i++)
  32. {
  33. app_catalog[i].app_name = (char *)malloc(MAX_APP_NAME_LENGTH * sizeof(char));
  34. if (!app_catalog[i].app_name)
  35. {
  36. FURI_LOG_E(TAG, "Failed to allocate memory for app_name.");
  37. return NULL;
  38. }
  39. app_catalog[i].app_id = (char *)malloc(MAX_APP_NAME_LENGTH * sizeof(char));
  40. if (!app_catalog[i].app_id)
  41. {
  42. FURI_LOG_E(TAG, "Failed to allocate memory for app_id.");
  43. return NULL;
  44. }
  45. app_catalog[i].app_build_id = (char *)malloc(MAX_ID_LENGTH * sizeof(char));
  46. if (!app_catalog[i].app_build_id)
  47. {
  48. FURI_LOG_E(TAG, "Failed to allocate memory for app_build_id.");
  49. return NULL;
  50. }
  51. app_catalog[i].app_version = (char *)malloc(MAX_APP_VERSION_LENGTH * sizeof(char));
  52. if (!app_catalog[i].app_version)
  53. {
  54. FURI_LOG_E(TAG, "Failed to allocate memory for app_version.");
  55. return NULL;
  56. }
  57. app_catalog[i].app_description = (char *)malloc(MAX_APP_DESCRIPTION_LENGTH * sizeof(char));
  58. if (!app_catalog[i].app_description)
  59. {
  60. FURI_LOG_E(TAG, "Failed to allocate memory for app_description.");
  61. return NULL;
  62. }
  63. }
  64. return app_catalog;
  65. }
  66. void flip_catalog_free()
  67. {
  68. if (!flip_catalog)
  69. {
  70. return;
  71. }
  72. for (int i = 0; i < MAX_APP_COUNT; i++)
  73. {
  74. if (flip_catalog[i].app_name)
  75. {
  76. free(flip_catalog[i].app_name);
  77. }
  78. if (flip_catalog[i].app_id)
  79. {
  80. free(flip_catalog[i].app_id);
  81. }
  82. if (flip_catalog[i].app_build_id)
  83. {
  84. free(flip_catalog[i].app_build_id);
  85. }
  86. if (flip_catalog[i].app_version)
  87. {
  88. free(flip_catalog[i].app_version);
  89. }
  90. if (flip_catalog[i].app_description)
  91. {
  92. free(flip_catalog[i].app_description);
  93. }
  94. }
  95. }
  96. bool flip_store_process_app_list()
  97. {
  98. // Initialize the flip_catalog
  99. flip_catalog = flip_catalog_alloc();
  100. if (!flip_catalog)
  101. {
  102. FURI_LOG_E(TAG, "Failed to allocate memory for flip_catalog.");
  103. return false;
  104. }
  105. FuriString *feed_data = flipper_http_load_from_file(fhttp.file_path);
  106. if (feed_data == NULL)
  107. {
  108. FURI_LOG_E(TAG, "Failed to load received data from file.");
  109. return false;
  110. }
  111. char *data_cstr = (char *)furi_string_get_cstr(feed_data);
  112. if (data_cstr == NULL)
  113. {
  114. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  115. furi_string_free(feed_data);
  116. return false;
  117. }
  118. // Parser state variables
  119. bool in_string = false;
  120. bool is_escaped = false;
  121. bool reading_key = false;
  122. bool reading_value = false;
  123. bool inside_app_object = false;
  124. bool found_name = false, found_id = false, found_build_id = false, found_version = false, found_description = false;
  125. char current_key[MAX_KEY_LENGTH] = {0};
  126. size_t key_index = 0;
  127. char current_value[MAX_VALUE_LENGTH] = {0};
  128. size_t value_index = 0;
  129. int app_count = 0;
  130. enum ObjectState object_state = OBJECT_EXPECT_KEY;
  131. enum
  132. {
  133. STATE_SEARCH_APPS_KEY,
  134. STATE_SEARCH_ARRAY_START,
  135. STATE_READ_ARRAY_ELEMENTS,
  136. STATE_DONE
  137. } state = STATE_SEARCH_APPS_KEY;
  138. // Iterate through the data
  139. for (size_t i = 0; data_cstr[i] != '\0' && state != STATE_DONE; ++i)
  140. {
  141. char c = data_cstr[i];
  142. if (is_escaped)
  143. {
  144. is_escaped = false;
  145. if (reading_key && key_index < MAX_KEY_LENGTH - 1)
  146. {
  147. current_key[key_index++] = c;
  148. }
  149. else if (reading_value && value_index < MAX_VALUE_LENGTH - 1)
  150. {
  151. current_value[value_index++] = c;
  152. }
  153. continue;
  154. }
  155. if (c == '\\')
  156. {
  157. is_escaped = true;
  158. continue;
  159. }
  160. if (c == '\"')
  161. {
  162. in_string = !in_string;
  163. if (in_string)
  164. {
  165. if (!reading_key && !reading_value)
  166. {
  167. if (state == STATE_SEARCH_APPS_KEY || object_state == OBJECT_EXPECT_KEY)
  168. {
  169. reading_key = true;
  170. key_index = 0;
  171. current_key[0] = '\0';
  172. }
  173. else if (object_state == OBJECT_EXPECT_VALUE)
  174. {
  175. reading_value = true;
  176. value_index = 0;
  177. current_value[0] = '\0';
  178. }
  179. }
  180. }
  181. else
  182. {
  183. if (reading_key)
  184. {
  185. reading_key = false;
  186. current_key[key_index] = '\0';
  187. if (state == STATE_SEARCH_APPS_KEY && strcmp(current_key, "apps") == 0)
  188. {
  189. state = STATE_SEARCH_ARRAY_START;
  190. }
  191. else if (inside_app_object)
  192. {
  193. object_state = OBJECT_EXPECT_COLON;
  194. }
  195. }
  196. else if (reading_value)
  197. {
  198. reading_value = false;
  199. current_value[value_index] = '\0';
  200. if (inside_app_object)
  201. {
  202. if (strcmp(current_key, "name") == 0)
  203. {
  204. snprintf(flip_catalog[app_count].app_name, MAX_APP_NAME_LENGTH, "%.31s", current_value);
  205. found_name = true;
  206. }
  207. else if (strcmp(current_key, "id") == 0)
  208. {
  209. snprintf(flip_catalog[app_count].app_id, MAX_ID_LENGTH, "%.31s", current_value);
  210. found_id = true;
  211. }
  212. else if (strcmp(current_key, "build_id") == 0)
  213. {
  214. snprintf(flip_catalog[app_count].app_build_id, MAX_ID_LENGTH, "%.31s", current_value);
  215. found_build_id = true;
  216. }
  217. else if (strcmp(current_key, "version") == 0)
  218. {
  219. snprintf(flip_catalog[app_count].app_version, MAX_APP_VERSION_LENGTH, "%.3s", current_value);
  220. found_version = true;
  221. }
  222. else if (strcmp(current_key, "description") == 0)
  223. {
  224. snprintf(flip_catalog[app_count].app_description, MAX_APP_DESCRIPTION_LENGTH, "%.99s", current_value);
  225. found_description = true;
  226. }
  227. if (found_name && found_id && found_build_id && found_version && found_description)
  228. {
  229. app_count++;
  230. if (app_count >= MAX_APP_COUNT)
  231. {
  232. FURI_LOG_I(TAG, "Reached maximum app count.");
  233. state = STATE_DONE;
  234. break;
  235. }
  236. found_name = found_id = found_build_id = found_version = found_description = false;
  237. }
  238. object_state = OBJECT_EXPECT_COMMA_OR_END;
  239. }
  240. }
  241. }
  242. continue;
  243. }
  244. if (in_string)
  245. {
  246. if (reading_key && key_index < MAX_KEY_LENGTH - 1)
  247. {
  248. current_key[key_index++] = c;
  249. }
  250. else if (reading_value && value_index < MAX_VALUE_LENGTH - 1)
  251. {
  252. current_value[value_index++] = c;
  253. }
  254. continue;
  255. }
  256. if (state == STATE_SEARCH_ARRAY_START && c == '[')
  257. {
  258. state = STATE_READ_ARRAY_ELEMENTS;
  259. continue;
  260. }
  261. if (state == STATE_READ_ARRAY_ELEMENTS)
  262. {
  263. if (c == '{')
  264. {
  265. inside_app_object = true;
  266. object_state = OBJECT_EXPECT_KEY;
  267. }
  268. else if (c == '}')
  269. {
  270. inside_app_object = false;
  271. }
  272. else if (c == ':')
  273. {
  274. object_state = OBJECT_EXPECT_VALUE;
  275. }
  276. else if (c == ',')
  277. {
  278. object_state = OBJECT_EXPECT_KEY;
  279. }
  280. else if (c == ']')
  281. {
  282. state = STATE_DONE;
  283. break;
  284. }
  285. }
  286. }
  287. // Clean up
  288. furi_string_free(feed_data);
  289. free(data_cstr);
  290. return app_count > 0;
  291. }
  292. bool flip_store_get_fap_file(char *build_id, uint8_t target, uint16_t api_major, uint16_t api_minor)
  293. {
  294. char url[128];
  295. fhttp.save_received_data = false;
  296. fhttp.is_bytes_request = true;
  297. 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);
  298. char *headers = jsmn("Content-Type", "application/octet-stream");
  299. bool sent_request = flipper_http_get_request_bytes(url, headers);
  300. free(headers);
  301. return sent_request;
  302. }
  303. // function to handle the entire installation process "asynchronously"
  304. bool flip_store_install_app(Canvas *canvas, char *category)
  305. {
  306. // create /apps/FlipStore directory if it doesn't exist
  307. char directory_path[128];
  308. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps/%s", category);
  309. // Create the directory
  310. Storage *storage = furi_record_open(RECORD_STORAGE);
  311. storage_common_mkdir(storage, directory_path);
  312. // Adjusted to access flip_catalog as an array of structures
  313. char installation_text[64];
  314. snprintf(installation_text, sizeof(installation_text), "Installing %s", flip_catalog[app_selected_index].app_name);
  315. snprintf(fhttp.file_path, sizeof(fhttp.file_path), STORAGE_EXT_PATH_PREFIX "/apps/%s/%s.fap", category, flip_catalog[app_selected_index].app_id);
  316. canvas_draw_str(canvas, 0, 10, installation_text);
  317. canvas_draw_str(canvas, 0, 20, "Sending request..");
  318. uint8_t target = furi_hal_version_get_hw_target();
  319. uint16_t api_major, api_minor;
  320. furi_hal_info_get_api_version(&api_major, &api_minor);
  321. if (fhttp.state != INACTIVE && flip_store_get_fap_file(flip_catalog[app_selected_index].app_build_id, target, api_major, api_minor))
  322. {
  323. canvas_draw_str(canvas, 0, 30, "Request sent.");
  324. fhttp.state = RECEIVING;
  325. canvas_draw_str(canvas, 0, 40, "Receiving...");
  326. }
  327. else
  328. {
  329. FURI_LOG_E(TAG, "Failed to send the request");
  330. flip_store_success = false;
  331. return false;
  332. }
  333. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  334. {
  335. // Wait for the feed to be received
  336. furi_delay_ms(10);
  337. }
  338. // furi_timer_stop(fhttp.get_timeout_timer);
  339. if (fhttp.state == ISSUE)
  340. {
  341. flip_store_request_error(canvas);
  342. flip_store_success = false;
  343. return false;
  344. }
  345. flip_store_success = true;
  346. return true;
  347. }
  348. // process the app list and return view
  349. int32_t flip_store_handle_app_list(FlipStoreApp *app, int32_t success_view, char *category, Submenu **submenu)
  350. {
  351. // reset the flip_catalog
  352. flip_catalog_free();
  353. if (!app)
  354. {
  355. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  356. return FlipStoreViewPopup;
  357. }
  358. snprintf(
  359. fhttp.file_path,
  360. sizeof(fhttp.file_path),
  361. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/%s.json", category);
  362. fhttp.save_received_data = true;
  363. fhttp.is_bytes_request = false;
  364. char url[128];
  365. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/flipper/apps/%s/max/", category);
  366. // async call to the app list with timer
  367. if (fhttp.state != INACTIVE && flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}"))
  368. {
  369. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  370. fhttp.state = RECEIVING;
  371. }
  372. else
  373. {
  374. FURI_LOG_E(TAG, "Failed to send the request");
  375. fhttp.state = ISSUE;
  376. return FlipStoreViewPopup;
  377. }
  378. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  379. {
  380. // Wait for the feed to be received
  381. furi_delay_ms(10);
  382. }
  383. furi_timer_stop(fhttp.get_timeout_timer);
  384. if (fhttp.state == ISSUE)
  385. {
  386. FURI_LOG_E(TAG, "Failed to receive data");
  387. if (fhttp.last_response == NULL)
  388. {
  389. if (fhttp.last_response != NULL)
  390. {
  391. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  392. {
  393. popup_set_text(app->popup, "[ERROR] WiFi Disconnected.\n\n\nUpdate your WiFi settings.\nPress BACK to return.", 0, 10, AlignLeft, AlignTop);
  394. }
  395. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  396. {
  397. popup_set_text(app->popup, "[ERROR] WiFi Disconnected.\n\n\nUpdate your WiFi settings.\nPress BACK to return.", 0, 10, AlignLeft, AlignTop);
  398. }
  399. else
  400. {
  401. popup_set_text(app->popup, fhttp.last_response, 0, 50, AlignLeft, AlignTop);
  402. }
  403. }
  404. else
  405. {
  406. popup_set_text(app->popup, "[ERROR] Unknown Error.\n\n\nUpdate your WiFi settings.\nPress BACK to return.", 0, 10, AlignLeft, AlignTop);
  407. }
  408. return FlipStoreViewPopup;
  409. }
  410. else
  411. {
  412. popup_set_text(app->popup, "Failed to received data.", 0, 50, AlignLeft, AlignTop);
  413. return FlipStoreViewPopup;
  414. }
  415. }
  416. else
  417. {
  418. // process the app list
  419. if (flip_store_process_app_list() && submenu && flip_catalog)
  420. {
  421. submenu_reset(*submenu);
  422. // add each app name to submenu
  423. for (int i = 0; i < MAX_APP_COUNT; i++)
  424. {
  425. if (strlen(flip_catalog[i].app_name) > 0)
  426. {
  427. submenu_add_item(*submenu, flip_catalog[i].app_name, FlipStoreSubmenuIndexStartAppList + i, callback_submenu_choices, app);
  428. }
  429. else
  430. {
  431. break;
  432. }
  433. }
  434. return success_view;
  435. }
  436. else
  437. {
  438. FURI_LOG_E(TAG, "Failed to process the app list");
  439. popup_set_text(app->popup, "Failed to process the app list", 0, 10, AlignLeft, AlignTop);
  440. return FlipStoreViewPopup;
  441. }
  442. }
  443. }