flip_store_apps.c 10 KB

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