flip_store_apps.c 9.7 KB

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