flip_store_apps.c 16 KB

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