| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- #ifndef FLIP_STORE_APPS_H
- #define FLIP_STORE_APPS_H
- // Define maximum limits
- #define MAX_APP_NAME_LENGTH 32
- #define MAX_ID_LENGTH 32
- #define MAX_APP_COUNT 100
- typedef struct
- {
- char app_name[MAX_APP_NAME_LENGTH];
- char app_id[MAX_APP_NAME_LENGTH];
- char app_build_id[MAX_ID_LENGTH];
- } FlipStoreAppInfo;
- static FlipStoreAppInfo *flip_catalog = NULL;
- static uint32_t app_selected_index = 0;
- static bool flip_store_sent_request = false;
- static bool flip_store_success = false;
- static bool flip_store_saved_data = false;
- static bool flip_store_saved_success = false;
- static uint32_t flip_store_category_index = 0;
- enum ObjectState
- {
- OBJECT_EXPECT_KEY,
- OBJECT_EXPECT_COLON,
- OBJECT_EXPECT_VALUE,
- OBJECT_EXPECT_COMMA_OR_END
- };
- void flip_catalog_free()
- {
- if (flip_catalog)
- {
- free(flip_catalog);
- flip_catalog = NULL;
- }
- }
- // Utility function to parse JSON incrementally from a file
- bool flip_store_process_app_list(const char *file_path)
- {
- if (file_path == NULL)
- {
- FURI_LOG_E(TAG, "JSON file path is NULL.");
- return false;
- }
- // initialize the flip_catalog
- flip_catalog = (FlipStoreAppInfo *)malloc(MAX_APP_COUNT * sizeof(FlipStoreAppInfo));
- if (!flip_catalog)
- {
- FURI_LOG_E(TAG, "Failed to allocate memory for flip_catalog.");
- return false;
- }
- Storage *_storage = NULL;
- File *_file = NULL;
- char buffer[BUFFER_SIZE];
- size_t bytes_read;
- bool in_string = false;
- bool is_escaped = false;
- bool reading_key = false;
- bool reading_value = false;
- bool inside_app_object = false;
- bool found_name = false;
- bool found_id = false;
- bool found_build_id = false;
- char current_key[MAX_KEY_LENGTH] = {0};
- size_t key_index = 0;
- char current_value[MAX_VALUE_LENGTH] = {0};
- size_t value_index = 0;
- int app_count = 0;
- enum ObjectState object_state = OBJECT_EXPECT_KEY; // Initialize object_state
- // Initialize parser state
- enum
- {
- STATE_SEARCH_APPS_KEY,
- STATE_SEARCH_ARRAY_START,
- STATE_READ_ARRAY_ELEMENTS,
- STATE_DONE
- } state = STATE_SEARCH_APPS_KEY;
- // Open storage and file
- _storage = furi_record_open(RECORD_STORAGE);
- if (!_storage)
- {
- FURI_LOG_E(TAG, "Failed to open storage.");
- return false;
- }
- _file = storage_file_alloc(_storage);
- if (!_file)
- {
- FURI_LOG_E(TAG, "Failed to allocate file.");
- furi_record_close(RECORD_STORAGE);
- return false;
- }
- if (!storage_file_open(_file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
- {
- FURI_LOG_E(TAG, "Failed to open JSON file for reading.");
- storage_file_free(_file);
- furi_record_close(RECORD_STORAGE);
- return false;
- }
- while ((bytes_read = storage_file_read(_file, buffer, BUFFER_SIZE)) > 0 && state != STATE_DONE)
- {
- for (size_t i = 0; i < bytes_read; ++i)
- {
- char c = buffer[i];
- if (is_escaped)
- {
- is_escaped = false;
- if (reading_key)
- {
- if (key_index < MAX_KEY_LENGTH - 1)
- {
- current_key[key_index++] = c;
- }
- }
- else if (reading_value)
- {
- if (value_index < MAX_VALUE_LENGTH - 1)
- {
- current_value[value_index++] = c;
- }
- }
- continue;
- }
- if (c == '\\')
- {
- is_escaped = true;
- continue;
- }
- if (c == '\"')
- {
- in_string = !in_string;
- if (in_string)
- {
- // Start of a string
- if (!reading_key && !reading_value)
- {
- if (state == STATE_SEARCH_APPS_KEY)
- {
- reading_key = true;
- key_index = 0;
- current_key[0] = '\0';
- }
- else if (inside_app_object)
- {
- if (object_state == OBJECT_EXPECT_KEY)
- {
- reading_key = true;
- key_index = 0;
- current_key[0] = '\0';
- }
- else if (object_state == OBJECT_EXPECT_VALUE)
- {
- reading_value = true;
- value_index = 0;
- current_value[0] = '\0';
- }
- }
- }
- }
- else
- {
- // End of a string
- if (reading_key)
- {
- reading_key = false;
- current_key[key_index] = '\0';
- if (state == STATE_SEARCH_APPS_KEY && strcmp(current_key, "apps") == 0)
- {
- state = STATE_SEARCH_ARRAY_START;
- }
- else if (inside_app_object)
- {
- object_state = OBJECT_EXPECT_COLON;
- }
- }
- else if (reading_value)
- {
- reading_value = false;
- current_value[value_index] = '\0';
- if (inside_app_object)
- {
- if (strcmp(current_key, "name") == 0)
- {
- strncpy(flip_catalog[app_count].app_name, current_value, MAX_APP_NAME_LENGTH - 1);
- flip_catalog[app_count].app_name[MAX_APP_NAME_LENGTH - 1] = '\0';
- found_name = true;
- }
- else if (strcmp(current_key, "id") == 0)
- {
- strncpy(flip_catalog[app_count].app_id, current_value, MAX_APP_NAME_LENGTH - 1);
- flip_catalog[app_count].app_id[MAX_APP_NAME_LENGTH - 1] = '\0';
- found_id = true;
- }
- else if (strcmp(current_key, "build_id") == 0)
- {
- strncpy(flip_catalog[app_count].app_build_id, current_value, MAX_APP_NAME_LENGTH - 1);
- flip_catalog[app_count].app_build_id[MAX_ID_LENGTH - 1] = '\0';
- found_build_id = true;
- }
- // After processing value, expect comma or end
- object_state = OBJECT_EXPECT_COMMA_OR_END;
- // Check if both name and id are found
- if (found_name && found_id && found_build_id)
- {
- app_count++;
- if (app_count >= MAX_APP_COUNT)
- {
- FURI_LOG_I(TAG, "Reached maximum app count.");
- state = STATE_DONE;
- break;
- }
- // Reset for next app
- found_name = false;
- found_id = false;
- found_build_id = false;
- }
- }
- }
- }
- continue;
- }
- if (in_string)
- {
- if (reading_key)
- {
- if (key_index < MAX_KEY_LENGTH - 1)
- {
- current_key[key_index++] = c;
- }
- }
- else if (reading_value)
- {
- if (value_index < MAX_VALUE_LENGTH - 1)
- {
- current_value[value_index++] = c;
- }
- }
- continue;
- }
- // Not inside a string
- if (state == STATE_SEARCH_ARRAY_START)
- {
- if (c == '[')
- {
- state = STATE_READ_ARRAY_ELEMENTS;
- }
- continue;
- }
- if (state == STATE_READ_ARRAY_ELEMENTS)
- {
- if (c == '{')
- {
- inside_app_object = true;
- found_name = false;
- found_id = false;
- found_build_id = false;
- object_state = OBJECT_EXPECT_KEY;
- }
- else if (c == '}')
- {
- inside_app_object = false;
- object_state = OBJECT_EXPECT_KEY;
- }
- else if (c == ']')
- {
- state = STATE_DONE;
- break;
- }
- else if (c == ':')
- {
- if (inside_app_object && object_state == OBJECT_EXPECT_COLON)
- {
- object_state = OBJECT_EXPECT_VALUE;
- }
- }
- else if (c == ',')
- {
- if (inside_app_object && object_state == OBJECT_EXPECT_COMMA_OR_END)
- {
- object_state = OBJECT_EXPECT_KEY;
- }
- // Else, separator between objects or values
- }
- // Ignore other characters like whitespace, etc.
- continue;
- }
- }
- }
- // free remaining app catalog memory
- for (int i = app_count; i < MAX_APP_COUNT; i++)
- {
- flip_catalog[i].app_name[0] = '\0';
- flip_catalog[i].app_id[0] = '\0';
- flip_catalog[i].app_build_id[0] = '\0';
- }
- // Clean up
- storage_file_close(_file);
- storage_file_free(_file);
- furi_record_close(RECORD_STORAGE);
- if (app_count == 0)
- {
- FURI_LOG_E(TAG, "No valid apps were parsed.");
- return false;
- }
- return true;
- }
- bool flip_store_get_fap_file(char *build_id, char *target, char *api)
- {
- is_compile_app_request = true;
- char url[164];
- snprintf(url, sizeof(url), "https://catalog.flipperzero.one/api/v0/application/version/%s/build/compatible?target=%s&api=%s", build_id, target, api);
- return flipper_http_get_request_bytes(url, "{\"Content-Type\":\"application/json\"}");
- }
- void flip_store_request_error(Canvas *canvas)
- {
- if (fhttp.received_data == NULL)
- {
- if (fhttp.last_response != NULL)
- {
- if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
- {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
- canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- }
- else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
- {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
- canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- }
- else
- {
- FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
- canvas_draw_str(canvas, 0, 42, "Unusual error...");
- canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- }
- }
- else
- {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
- canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- }
- }
- else
- {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "Failed to receive data.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- }
- }
- // function to handle the entire installation process "asynchronously"
- bool flip_store_install_app(Canvas *canvas, char *category)
- {
- // create /apps/FlipStore directory if it doesn't exist
- char directory_path[128];
- snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps/%s", category);
- // Create the directory
- Storage *storage = furi_record_open(RECORD_STORAGE);
- storage_common_mkdir(storage, directory_path);
- // Adjusted to access flip_catalog as an array of structures
- char *app_name = flip_catalog[app_selected_index].app_name;
- char installing_text[128];
- snprintf(installing_text, sizeof(installing_text), "Installing %s", app_name);
- char bin_path[256];
- snprintf(bin_path, sizeof(bin_path), STORAGE_EXT_PATH_PREFIX "/apps/%s/%s.fap", category, flip_catalog[app_selected_index].app_id);
- strncpy(fhttp.file_path, bin_path, sizeof(fhttp.file_path) - 1);
- canvas_draw_str(canvas, 0, 10, installing_text);
- canvas_draw_str(canvas, 0, 20, "Sending request..");
- if (fhttp.state != INACTIVE && flip_store_get_fap_file(flip_catalog[app_selected_index].app_build_id, "f7", "73.0"))
- {
- canvas_draw_str(canvas, 0, 30, "Request sent.");
- fhttp.state = RECEIVING;
- // furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
- canvas_draw_str(canvas, 0, 40, "Receiving...");
- }
- else
- {
- FURI_LOG_E(TAG, "Failed to send the request");
- flip_store_success = false;
- return false;
- }
- while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
- {
- // Wait for the feed to be received
- furi_delay_ms(10);
- }
- // furi_timer_stop(fhttp.get_timeout_timer);
- if (fhttp.state == ISSUE || fhttp.received_data == NULL)
- {
- flip_store_request_error(canvas);
- flip_store_success = false;
- return false;
- }
- flip_store_success = true;
- return true;
- }
- // process the app list and return view
- int32_t flip_store_handle_app_list(FlipStoreApp *app, int32_t success_view, char *category, Submenu **submenu)
- {
- // reset the flip_catalog
- if (flip_catalog)
- {
- flip_catalog_free();
- }
- if (!app)
- {
- FURI_LOG_E(TAG, "FlipStoreApp is NULL");
- return FlipStoreViewPopup;
- }
- char url[128];
- is_compile_app_request = false;
- // append the category to the end of the url
- snprintf(url, sizeof(url), "https://www.flipsocial.net/api/flipper/apps/%s/extended/", category);
- // async call to the app list with timer
- if (fhttp.state != INACTIVE && flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}"))
- {
- furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
- fhttp.state = RECEIVING;
- }
- else
- {
- FURI_LOG_E(TAG, "Failed to send the request");
- return FlipStoreViewPopup;
- }
- while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
- {
- // Wait for the feed to be received
- furi_delay_ms(100);
- }
- furi_timer_stop(fhttp.get_timeout_timer);
- if (fhttp.state == ISSUE || fhttp.received_data == NULL)
- {
- if (fhttp.received_data == NULL)
- {
- FURI_LOG_E(TAG, "Failed to receive data");
- if (fhttp.last_response != NULL)
- {
- if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
- {
- popup_set_text(app->popup, "[ERROR] WiFi Disconnected.\n\n\nUpdate your WiFi settings.\nPress BACK to return.", 0, 10, AlignLeft, AlignTop);
- }
- else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
- {
- popup_set_text(app->popup, "[ERROR] WiFi Disconnected.\n\n\nUpdate your WiFi settings.\nPress BACK to return.", 0, 10, AlignLeft, AlignTop);
- }
- else
- {
- popup_set_text(app->popup, fhttp.last_response, 0, 10, AlignLeft, AlignTop);
- }
- }
- else
- {
- popup_set_text(app->popup, "[ERROR] Unknown Error.\n\n\nUpdate your WiFi settings.\nPress BACK to return.", 0, 10, AlignLeft, AlignTop);
- }
- return FlipStoreViewPopup;
- }
- else
- {
- FURI_LOG_E(TAG, "Failed to receive data");
- popup_set_text(app->popup, "Failed to received data.", 0, 10, AlignLeft, AlignTop);
- return FlipStoreViewPopup;
- }
- }
- else
- {
- // process the app list
- const char *output_file_path = STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag "/received_data.txt";
- if (flip_store_process_app_list(output_file_path))
- {
- submenu_reset(*submenu);
- // add each app name to submenu
- for (int i = 0; i < MAX_APP_COUNT; i++)
- {
- if (strlen(flip_catalog[i].app_name) > 0)
- {
- submenu_add_item(*submenu, flip_catalog[i].app_name, FlipStoreSubmenuIndexStartAppList + i, callback_submenu_choices, app);
- }
- }
- return success_view;
- }
- else
- {
- FURI_LOG_E(TAG, "Failed to process the app list");
- popup_set_text(app->popup, "Failed to process the app list", 0, 10, AlignLeft, AlignTop);
- return FlipStoreViewPopup;
- }
- }
- }
- #endif // FLIP_STORE_APPS_H
|