flip_store_apps.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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(char* build_id, char* target, char* api) {
  251. is_compile_app_request = true;
  252. char url[164];
  253. snprintf(
  254. url,
  255. sizeof(url),
  256. "https://catalog.flipperzero.one/api/v0/application/version/%s/build/compatible?target=%s&api=%s",
  257. build_id,
  258. target,
  259. api);
  260. return flipper_http_get_request_bytes(url, jsmn("Content-Type", "application/octet-stream"));
  261. }
  262. static void flip_store_request_error(Canvas* canvas) {
  263. if(fhttp.received_data == NULL) {
  264. if(fhttp.last_response != NULL) {
  265. if(strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") !=
  266. NULL) {
  267. canvas_clear(canvas);
  268. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  269. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  270. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  271. } else if(strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL) {
  272. canvas_clear(canvas);
  273. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  274. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  275. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  276. } else {
  277. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  278. canvas_draw_str(canvas, 0, 42, "Unusual error...");
  279. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  280. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  281. }
  282. } else {
  283. canvas_clear(canvas);
  284. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  285. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  286. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  287. }
  288. } else {
  289. canvas_clear(canvas);
  290. canvas_draw_str(canvas, 0, 10, "Failed to receive data.");
  291. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  292. }
  293. }
  294. // function to handle the entire installation process "asynchronously"
  295. static bool flip_store_install_app(Canvas* canvas, char* category) {
  296. // create /apps/FlipStore directory if it doesn't exist
  297. char directory_path[128];
  298. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps/%s", category);
  299. // Create the directory
  300. Storage* storage = furi_record_open(RECORD_STORAGE);
  301. storage_common_mkdir(storage, directory_path);
  302. // Adjusted to access flip_catalog as an array of structures
  303. char* app_name = flip_catalog[app_selected_index].app_name;
  304. char installing_text[128];
  305. snprintf(installing_text, sizeof(installing_text), "Installing %s", app_name);
  306. char bin_path[256];
  307. snprintf(
  308. bin_path,
  309. sizeof(bin_path),
  310. STORAGE_EXT_PATH_PREFIX "/apps/%s/%s.fap",
  311. category,
  312. flip_catalog[app_selected_index].app_id);
  313. strncpy(fhttp.file_path, bin_path, sizeof(fhttp.file_path) - 1);
  314. canvas_draw_str(canvas, 0, 10, installing_text);
  315. canvas_draw_str(canvas, 0, 20, "Sending request..");
  316. if(fhttp.state != INACTIVE &&
  317. flip_store_get_fap_file(flip_catalog[app_selected_index].app_build_id, "f7", "73.0")) {
  318. canvas_draw_str(canvas, 0, 30, "Request sent.");
  319. fhttp.state = RECEIVING;
  320. // furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  321. canvas_draw_str(canvas, 0, 40, "Receiving...");
  322. } else {
  323. FURI_LOG_E(TAG, "Failed to send the request");
  324. flip_store_success = false;
  325. return false;
  326. }
  327. while(fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0) {
  328. // Wait for the feed to be received
  329. furi_delay_ms(10);
  330. }
  331. // furi_timer_stop(fhttp.get_timeout_timer);
  332. if(fhttp.state == ISSUE || fhttp.received_data == NULL) {
  333. flip_store_request_error(canvas);
  334. flip_store_success = false;
  335. return false;
  336. }
  337. flip_store_success = true;
  338. return true;
  339. }
  340. // process the app list and return view
  341. static int32_t flip_store_handle_app_list(
  342. FlipStoreApp* app,
  343. int32_t success_view,
  344. char* category,
  345. Submenu** submenu) {
  346. // reset the flip_catalog
  347. if(flip_catalog) {
  348. flip_catalog_free();
  349. }
  350. if(!app) {
  351. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  352. return FlipStoreViewPopup;
  353. }
  354. char url[128];
  355. is_compile_app_request = false;
  356. // append the category to the end of the url
  357. snprintf(
  358. url, sizeof(url), "https://www.flipsocial.net/api/flipper/apps/%s/extended/", category);
  359. // async call to the app list with timer
  360. if(fhttp.state != INACTIVE &&
  361. flipper_http_get_request_with_headers(url, jsmn("Content-Type", "application/json"))) {
  362. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  363. fhttp.state = RECEIVING;
  364. } else {
  365. FURI_LOG_E(TAG, "Failed to send the request");
  366. return FlipStoreViewPopup;
  367. }
  368. while(fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0) {
  369. // Wait for the feed to be received
  370. furi_delay_ms(100);
  371. }
  372. furi_timer_stop(fhttp.get_timeout_timer);
  373. if(fhttp.state == ISSUE || fhttp.received_data == NULL) {
  374. if(fhttp.received_data == NULL) {
  375. FURI_LOG_E(TAG, "Failed to receive data");
  376. if(fhttp.last_response != NULL) {
  377. if(strstr(
  378. fhttp.last_response,
  379. "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL) {
  380. popup_set_text(
  381. app->popup,
  382. "[ERROR] WiFi Disconnected.\n\n\nUpdate your WiFi settings.\nPress BACK to return.",
  383. 0,
  384. 10,
  385. AlignLeft,
  386. AlignTop);
  387. } else if(strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL) {
  388. popup_set_text(
  389. app->popup,
  390. "[ERROR] WiFi Disconnected.\n\n\nUpdate your WiFi settings.\nPress BACK to return.",
  391. 0,
  392. 10,
  393. AlignLeft,
  394. AlignTop);
  395. } else {
  396. popup_set_text(app->popup, fhttp.last_response, 0, 10, AlignLeft, AlignTop);
  397. }
  398. } else {
  399. popup_set_text(
  400. app->popup,
  401. "[ERROR] Unknown Error.\n\n\nUpdate your WiFi settings.\nPress BACK to return.",
  402. 0,
  403. 10,
  404. AlignLeft,
  405. AlignTop);
  406. }
  407. return FlipStoreViewPopup;
  408. } else {
  409. FURI_LOG_E(TAG, "Failed to receive data");
  410. popup_set_text(app->popup, "Failed to received data.", 0, 10, AlignLeft, AlignTop);
  411. return FlipStoreViewPopup;
  412. }
  413. } else {
  414. // process the app list
  415. const char* output_file_path = STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag
  416. "/received_data.txt";
  417. if(flip_store_process_app_list(output_file_path)) {
  418. submenu_reset(*submenu);
  419. // add each app name to submenu
  420. for(int i = 0; i < MAX_APP_COUNT; i++) {
  421. if(strlen(flip_catalog[i].app_name) > 0) {
  422. submenu_add_item(
  423. *submenu,
  424. flip_catalog[i].app_name,
  425. FlipStoreSubmenuIndexStartAppList + i,
  426. callback_submenu_choices,
  427. app);
  428. }
  429. }
  430. return success_view;
  431. } else {
  432. FURI_LOG_E(TAG, "Failed to process the app list");
  433. popup_set_text(
  434. app->popup, "Failed to process the app list", 0, 10, AlignLeft, AlignTop);
  435. return FlipStoreViewPopup;
  436. }
  437. }
  438. }
  439. #endif // FLIP_STORE_APPS_H