flip_store_apps.c 9.7 KB

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