flip_store_apps.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 = 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. // No need for a loop since all memory is allocated in one block
  32. return app_catalog;
  33. }
  34. void flip_catalog_free()
  35. {
  36. if (flip_catalog)
  37. {
  38. free(flip_catalog);
  39. }
  40. }
  41. bool flip_store_process_app_list()
  42. {
  43. // Initialize the flip_catalog
  44. flip_catalog = flip_catalog_alloc();
  45. if (!flip_catalog)
  46. {
  47. FURI_LOG_E(TAG, "Failed to allocate memory for flip_catalog.");
  48. return false;
  49. }
  50. FuriString *feed_data = flipper_http_load_from_file(fhttp.file_path);
  51. if (feed_data == NULL)
  52. {
  53. FURI_LOG_E(TAG, "Failed to load received data from file.");
  54. return false;
  55. }
  56. // free the resources
  57. flipper_http_deinit();
  58. char *data_cstr = (char *)furi_string_get_cstr(feed_data);
  59. if (data_cstr == NULL)
  60. {
  61. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  62. furi_string_free(feed_data);
  63. return false;
  64. }
  65. // Parser state variables
  66. bool in_string = false;
  67. bool is_escaped = false;
  68. bool reading_key = false;
  69. bool reading_value = false;
  70. bool inside_app_object = false;
  71. bool found_name = false, found_id = false, found_build_id = false, found_version = false, found_description = false;
  72. char current_key[MAX_KEY_LENGTH] = {0};
  73. size_t key_index = 0;
  74. char current_value[MAX_VALUE_LENGTH] = {0};
  75. size_t value_index = 0;
  76. int app_count = 0;
  77. enum ObjectState object_state = OBJECT_EXPECT_KEY;
  78. enum
  79. {
  80. STATE_SEARCH_APPS_KEY,
  81. STATE_SEARCH_ARRAY_START,
  82. STATE_READ_ARRAY_ELEMENTS,
  83. STATE_DONE
  84. } state = STATE_SEARCH_APPS_KEY;
  85. // Iterate through the data
  86. for (size_t i = 0; data_cstr[i] != '\0' && state != STATE_DONE; ++i)
  87. {
  88. char c = data_cstr[i];
  89. if (is_escaped)
  90. {
  91. is_escaped = false;
  92. if (reading_key && key_index < MAX_KEY_LENGTH - 1)
  93. {
  94. current_key[key_index++] = c;
  95. }
  96. else if (reading_value && value_index < MAX_VALUE_LENGTH - 1)
  97. {
  98. current_value[value_index++] = c;
  99. }
  100. continue;
  101. }
  102. if (c == '\\')
  103. {
  104. is_escaped = true;
  105. continue;
  106. }
  107. if (c == '\"')
  108. {
  109. in_string = !in_string;
  110. if (in_string)
  111. {
  112. if (!reading_key && !reading_value)
  113. {
  114. if (state == STATE_SEARCH_APPS_KEY || object_state == OBJECT_EXPECT_KEY)
  115. {
  116. reading_key = true;
  117. key_index = 0;
  118. current_key[0] = '\0';
  119. }
  120. else if (object_state == OBJECT_EXPECT_VALUE)
  121. {
  122. reading_value = true;
  123. value_index = 0;
  124. current_value[0] = '\0';
  125. }
  126. }
  127. }
  128. else
  129. {
  130. if (reading_key)
  131. {
  132. reading_key = false;
  133. current_key[key_index] = '\0';
  134. if (state == STATE_SEARCH_APPS_KEY && strcmp(current_key, "apps") == 0)
  135. {
  136. state = STATE_SEARCH_ARRAY_START;
  137. }
  138. else if (inside_app_object)
  139. {
  140. object_state = OBJECT_EXPECT_COLON;
  141. }
  142. }
  143. else if (reading_value)
  144. {
  145. reading_value = false;
  146. current_value[value_index] = '\0';
  147. if (inside_app_object)
  148. {
  149. if (strcmp(current_key, "name") == 0)
  150. {
  151. snprintf(flip_catalog[app_count].app_name, MAX_APP_NAME_LENGTH, "%.31s", current_value);
  152. found_name = true;
  153. }
  154. else if (strcmp(current_key, "id") == 0)
  155. {
  156. snprintf(flip_catalog[app_count].app_id, MAX_ID_LENGTH, "%.31s", current_value);
  157. found_id = true;
  158. }
  159. else if (strcmp(current_key, "build_id") == 0)
  160. {
  161. snprintf(flip_catalog[app_count].app_build_id, MAX_ID_LENGTH, "%.31s", current_value);
  162. found_build_id = true;
  163. }
  164. else if (strcmp(current_key, "version") == 0)
  165. {
  166. snprintf(flip_catalog[app_count].app_version, MAX_APP_VERSION_LENGTH, "%.3s", current_value);
  167. found_version = true;
  168. }
  169. else if (strcmp(current_key, "description") == 0)
  170. {
  171. snprintf(flip_catalog[app_count].app_description, MAX_APP_DESCRIPTION_LENGTH, "%.99s", current_value);
  172. found_description = true;
  173. }
  174. if (found_name && found_id && found_build_id && found_version && found_description)
  175. {
  176. app_count++;
  177. if (app_count >= MAX_APP_COUNT)
  178. {
  179. FURI_LOG_I(TAG, "Reached maximum app count.");
  180. state = STATE_DONE;
  181. break;
  182. }
  183. found_name = found_id = found_build_id = found_version = found_description = false;
  184. }
  185. object_state = OBJECT_EXPECT_COMMA_OR_END;
  186. }
  187. }
  188. }
  189. continue;
  190. }
  191. if (in_string)
  192. {
  193. if (reading_key && key_index < MAX_KEY_LENGTH - 1)
  194. {
  195. current_key[key_index++] = c;
  196. }
  197. else if (reading_value && value_index < MAX_VALUE_LENGTH - 1)
  198. {
  199. current_value[value_index++] = c;
  200. }
  201. continue;
  202. }
  203. if (state == STATE_SEARCH_ARRAY_START && c == '[')
  204. {
  205. state = STATE_READ_ARRAY_ELEMENTS;
  206. continue;
  207. }
  208. if (state == STATE_READ_ARRAY_ELEMENTS)
  209. {
  210. if (c == '{')
  211. {
  212. inside_app_object = true;
  213. object_state = OBJECT_EXPECT_KEY;
  214. }
  215. else if (c == '}')
  216. {
  217. inside_app_object = false;
  218. }
  219. else if (c == ':')
  220. {
  221. object_state = OBJECT_EXPECT_VALUE;
  222. }
  223. else if (c == ',')
  224. {
  225. object_state = OBJECT_EXPECT_KEY;
  226. }
  227. else if (c == ']')
  228. {
  229. state = STATE_DONE;
  230. break;
  231. }
  232. }
  233. }
  234. // Clean up
  235. furi_string_free(feed_data);
  236. free(data_cstr);
  237. return app_count > 0;
  238. }
  239. bool flip_store_get_fap_file(char *build_id, uint8_t target, uint16_t api_major, uint16_t api_minor)
  240. {
  241. if (!app_instance)
  242. {
  243. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  244. return false;
  245. }
  246. // initialize the http
  247. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  248. {
  249. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP.");
  250. return false;
  251. }
  252. fhttp.state = IDLE;
  253. char url[128];
  254. fhttp.save_received_data = false;
  255. fhttp.is_bytes_request = true;
  256. 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);
  257. return flipper_http_get_request_bytes(url, "{\"Content-Type\": \"application/octet-stream\"}");
  258. }
  259. bool flip_store_install_app(char *category)
  260. {
  261. // create /apps/FlipStore directory if it doesn't exist
  262. char directory_path[128];
  263. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps/%s", category);
  264. // Create the directory
  265. Storage *storage = furi_record_open(RECORD_STORAGE);
  266. storage_common_mkdir(storage, directory_path);
  267. snprintf(fhttp.file_path, sizeof(fhttp.file_path), STORAGE_EXT_PATH_PREFIX "/apps/%s/%s.fap", category, flip_catalog[app_selected_index].app_id);
  268. uint8_t target = furi_hal_version_get_hw_target();
  269. uint16_t api_major, api_minor;
  270. furi_hal_info_get_api_version(&api_major, &api_minor);
  271. if (fhttp.state != INACTIVE && flip_store_get_fap_file(flip_catalog[app_selected_index].app_build_id, target, api_major, api_minor))
  272. {
  273. fhttp.state = RECEIVING;
  274. return true;
  275. }
  276. else
  277. {
  278. FURI_LOG_E(TAG, "Failed to send the request");
  279. flip_store_success = false;
  280. return false;
  281. }
  282. }