flip_store_apps.c 18 KB

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