flip_store_apps.h 18 KB

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