|
|
@@ -37,6 +37,18 @@ FlipStoreAppInfo *flip_catalog_alloc()
|
|
|
FURI_LOG_E(TAG, "Failed to allocate memory for app_build_id.");
|
|
|
return NULL;
|
|
|
}
|
|
|
+ app_catalog[i].app_version = (char *)malloc(MAX_APP_VERSION_LENGTH * sizeof(char));
|
|
|
+ if (!app_catalog[i].app_version)
|
|
|
+ {
|
|
|
+ FURI_LOG_E(TAG, "Failed to allocate memory for app_version.");
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+ app_catalog[i].app_description = (char *)malloc(MAX_APP_DESCRIPTION_LENGTH * sizeof(char));
|
|
|
+ if (!app_catalog[i].app_description)
|
|
|
+ {
|
|
|
+ FURI_LOG_E(TAG, "Failed to allocate memory for app_description.");
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
}
|
|
|
return app_catalog;
|
|
|
}
|
|
|
@@ -60,13 +72,20 @@ void flip_catalog_free()
|
|
|
{
|
|
|
free(flip_catalog[i].app_build_id);
|
|
|
}
|
|
|
+ if (flip_catalog[i].app_version)
|
|
|
+ {
|
|
|
+ free(flip_catalog[i].app_version);
|
|
|
+ }
|
|
|
+ if (flip_catalog[i].app_description)
|
|
|
+ {
|
|
|
+ free(flip_catalog[i].app_description);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// Utility function to parse JSON incrementally from a file
|
|
|
bool flip_store_process_app_list()
|
|
|
{
|
|
|
- // initialize the flip_catalog
|
|
|
+ // Initialize the flip_catalog
|
|
|
flip_catalog = flip_catalog_alloc();
|
|
|
if (!flip_catalog)
|
|
|
{
|
|
|
@@ -74,26 +93,34 @@ bool flip_store_process_app_list()
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- Storage *_storage = NULL;
|
|
|
- File *_file = NULL;
|
|
|
- char buffer[BUFFER_SIZE];
|
|
|
- size_t bytes_read;
|
|
|
+ FuriString *feed_data = flipper_http_load_from_file(fhttp.file_path);
|
|
|
+ if (feed_data == NULL)
|
|
|
+ {
|
|
|
+ FURI_LOG_E(TAG, "Failed to load received data from file.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ char *data_cstr = (char *)furi_string_get_cstr(feed_data);
|
|
|
+ if (data_cstr == NULL)
|
|
|
+ {
|
|
|
+ FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
|
|
|
+ furi_string_free(feed_data);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Parser state variables
|
|
|
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;
|
|
|
+ bool found_name = false, found_id = false, found_build_id = false, found_version = false, found_description = 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 ObjectState object_state = OBJECT_EXPECT_KEY;
|
|
|
enum
|
|
|
{
|
|
|
STATE_SEARCH_APPS_KEY,
|
|
|
@@ -102,235 +129,169 @@ bool flip_store_process_app_list()
|
|
|
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)
|
|
|
+ // Iterate through the data
|
|
|
+ for (size_t i = 0; data_cstr[i] != '\0' && state != STATE_DONE; ++i)
|
|
|
{
|
|
|
- FURI_LOG_E(TAG, "Failed to allocate file.");
|
|
|
- furi_record_close(RECORD_STORAGE);
|
|
|
- return false;
|
|
|
- }
|
|
|
+ char c = data_cstr[i];
|
|
|
|
|
|
- if (!storage_file_open(_file, fhttp.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;
|
|
|
- }
|
|
|
+ if (is_escaped)
|
|
|
+ {
|
|
|
+ is_escaped = false;
|
|
|
+ if (reading_key && key_index < MAX_KEY_LENGTH - 1)
|
|
|
+ {
|
|
|
+ current_key[key_index++] = c;
|
|
|
+ }
|
|
|
+ else if (reading_value && value_index < MAX_VALUE_LENGTH - 1)
|
|
|
+ {
|
|
|
+ current_value[value_index++] = c;
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- while ((bytes_read = storage_file_read(_file, buffer, BUFFER_SIZE)) > 0 && state != STATE_DONE)
|
|
|
- {
|
|
|
- for (size_t i = 0; i < bytes_read; ++i)
|
|
|
+ if (c == '\\')
|
|
|
{
|
|
|
- char c = buffer[i];
|
|
|
+ is_escaped = true;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- if (is_escaped)
|
|
|
+ if (c == '\"')
|
|
|
+ {
|
|
|
+ in_string = !in_string;
|
|
|
+ if (in_string)
|
|
|
{
|
|
|
- is_escaped = false;
|
|
|
- if (reading_key)
|
|
|
+ if (!reading_key && !reading_value)
|
|
|
{
|
|
|
- if (key_index < MAX_KEY_LENGTH - 1)
|
|
|
+ if (state == STATE_SEARCH_APPS_KEY || object_state == OBJECT_EXPECT_KEY)
|
|
|
{
|
|
|
- current_key[key_index++] = c;
|
|
|
+ reading_key = true;
|
|
|
+ key_index = 0;
|
|
|
+ current_key[0] = '\0';
|
|
|
}
|
|
|
- }
|
|
|
- else if (reading_value)
|
|
|
- {
|
|
|
- if (value_index < MAX_VALUE_LENGTH - 1)
|
|
|
+ else if (object_state == OBJECT_EXPECT_VALUE)
|
|
|
{
|
|
|
- current_value[value_index++] = c;
|
|
|
+ reading_value = true;
|
|
|
+ value_index = 0;
|
|
|
+ current_value[0] = '\0';
|
|
|
}
|
|
|
}
|
|
|
- continue;
|
|
|
}
|
|
|
-
|
|
|
- if (c == '\\')
|
|
|
- {
|
|
|
- is_escaped = true;
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (c == '\"')
|
|
|
+ else
|
|
|
{
|
|
|
- in_string = !in_string;
|
|
|
-
|
|
|
- if (in_string)
|
|
|
+ if (reading_key)
|
|
|
{
|
|
|
- // Start of a string
|
|
|
- if (!reading_key && !reading_value)
|
|
|
+ reading_key = false;
|
|
|
+ current_key[key_index] = '\0';
|
|
|
+ if (state == STATE_SEARCH_APPS_KEY && strcmp(current_key, "apps") == 0)
|
|
|
{
|
|
|
- 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';
|
|
|
- }
|
|
|
- }
|
|
|
+ state = STATE_SEARCH_ARRAY_START;
|
|
|
+ }
|
|
|
+ else if (inside_app_object)
|
|
|
+ {
|
|
|
+ object_state = OBJECT_EXPECT_COLON;
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
+ else if (reading_value)
|
|
|
{
|
|
|
- // End of a string
|
|
|
- if (reading_key)
|
|
|
- {
|
|
|
- reading_key = false;
|
|
|
- current_key[key_index] = '\0';
|
|
|
+ reading_value = false;
|
|
|
+ current_value[value_index] = '\0';
|
|
|
|
|
|
- if (state == STATE_SEARCH_APPS_KEY && strcmp(current_key, "apps") == 0)
|
|
|
+ if (inside_app_object)
|
|
|
+ {
|
|
|
+ if (strcmp(current_key, "name") == 0)
|
|
|
{
|
|
|
- state = STATE_SEARCH_ARRAY_START;
|
|
|
+ snprintf(flip_catalog[app_count].app_name, MAX_APP_NAME_LENGTH, "%.31s", current_value);
|
|
|
+ found_name = true;
|
|
|
}
|
|
|
- else if (inside_app_object)
|
|
|
+ else if (strcmp(current_key, "id") == 0)
|
|
|
{
|
|
|
- object_state = OBJECT_EXPECT_COLON;
|
|
|
+ snprintf(flip_catalog[app_count].app_id, MAX_ID_LENGTH, "%.31s", current_value);
|
|
|
+ found_id = true;
|
|
|
+ }
|
|
|
+ else if (strcmp(current_key, "build_id") == 0)
|
|
|
+ {
|
|
|
+ snprintf(flip_catalog[app_count].app_build_id, MAX_ID_LENGTH, "%.31s", current_value);
|
|
|
+ found_build_id = true;
|
|
|
+ }
|
|
|
+ else if (strcmp(current_key, "version") == 0)
|
|
|
+ {
|
|
|
+ snprintf(flip_catalog[app_count].app_version, MAX_APP_VERSION_LENGTH, "%.3s", current_value);
|
|
|
+ found_version = true;
|
|
|
+ }
|
|
|
+ else if (strcmp(current_key, "description") == 0)
|
|
|
+ {
|
|
|
+ snprintf(flip_catalog[app_count].app_description, MAX_APP_DESCRIPTION_LENGTH, "%.99s", current_value);
|
|
|
+ found_description = true;
|
|
|
}
|
|
|
- }
|
|
|
- else if (reading_value)
|
|
|
- {
|
|
|
- reading_value = false;
|
|
|
- current_value[value_index] = '\0';
|
|
|
|
|
|
- if (inside_app_object)
|
|
|
+ if (found_name && found_id && found_build_id && found_version && found_description)
|
|
|
{
|
|
|
- if (strcmp(current_key, "name") == 0)
|
|
|
- {
|
|
|
- snprintf(flip_catalog[app_count].app_name, MAX_APP_NAME_LENGTH, "%.31s", current_value);
|
|
|
- found_name = true;
|
|
|
- }
|
|
|
- else if (strcmp(current_key, "id") == 0)
|
|
|
- {
|
|
|
- snprintf(flip_catalog[app_count].app_id, MAX_ID_LENGTH, "%.31s", current_value);
|
|
|
- found_id = true;
|
|
|
- }
|
|
|
- else if (strcmp(current_key, "build_id") == 0)
|
|
|
+ app_count++;
|
|
|
+ if (app_count >= MAX_APP_COUNT)
|
|
|
{
|
|
|
- snprintf(flip_catalog[app_count].app_build_id, MAX_ID_LENGTH, "%.31s", current_value);
|
|
|
- found_build_id = true;
|
|
|
+ FURI_LOG_I(TAG, "Reached maximum app count.");
|
|
|
+ state = STATE_DONE;
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
- // 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;
|
|
|
- }
|
|
|
+ found_name = found_id = found_build_id = found_version = found_description = false;
|
|
|
}
|
|
|
+
|
|
|
+ object_state = OBJECT_EXPECT_COMMA_OR_END;
|
|
|
}
|
|
|
}
|
|
|
- continue;
|
|
|
}
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- if (in_string)
|
|
|
+ if (in_string)
|
|
|
+ {
|
|
|
+ if (reading_key && key_index < MAX_KEY_LENGTH - 1)
|
|
|
{
|
|
|
- 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;
|
|
|
+ current_key[key_index++] = c;
|
|
|
+ }
|
|
|
+ else if (reading_value && value_index < MAX_VALUE_LENGTH - 1)
|
|
|
+ {
|
|
|
+ current_value[value_index++] = c;
|
|
|
}
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- // Not inside a string
|
|
|
+ if (state == STATE_SEARCH_ARRAY_START && c == '[')
|
|
|
+ {
|
|
|
+ state = STATE_READ_ARRAY_ELEMENTS;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- if (state == STATE_SEARCH_ARRAY_START)
|
|
|
+ if (state == STATE_READ_ARRAY_ELEMENTS)
|
|
|
+ {
|
|
|
+ if (c == '{')
|
|
|
{
|
|
|
- if (c == '[')
|
|
|
- {
|
|
|
- state = STATE_READ_ARRAY_ELEMENTS;
|
|
|
- }
|
|
|
- continue;
|
|
|
+ inside_app_object = true;
|
|
|
+ object_state = OBJECT_EXPECT_KEY;
|
|
|
}
|
|
|
-
|
|
|
- if (state == STATE_READ_ARRAY_ELEMENTS)
|
|
|
+ else if (c == '}')
|
|
|
{
|
|
|
- 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;
|
|
|
+ inside_app_object = false;
|
|
|
+ }
|
|
|
+ else if (c == ':')
|
|
|
+ {
|
|
|
+ object_state = OBJECT_EXPECT_VALUE;
|
|
|
+ }
|
|
|
+ else if (c == ',')
|
|
|
+ {
|
|
|
+ object_state = OBJECT_EXPECT_KEY;
|
|
|
+ }
|
|
|
+ else if (c == ']')
|
|
|
+ {
|
|
|
+ state = STATE_DONE;
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Clean up
|
|
|
- storage_file_close(_file);
|
|
|
- storage_file_free(_file);
|
|
|
- furi_record_close(RECORD_STORAGE);
|
|
|
-
|
|
|
+ furi_string_free(feed_data);
|
|
|
+ free(data_cstr);
|
|
|
return app_count > 0;
|
|
|
}
|
|
|
|
|
|
@@ -392,10 +353,10 @@ bool flip_store_install_app(Canvas *canvas, char *category)
|
|
|
storage_common_mkdir(storage, directory_path);
|
|
|
|
|
|
// Adjusted to access flip_catalog as an array of structures
|
|
|
- char installing_text[64];
|
|
|
- snprintf(installing_text, sizeof(installing_text), "Installing %s", flip_catalog[app_selected_index].app_name);
|
|
|
+ char installation_text[64];
|
|
|
+ snprintf(installation_text, sizeof(installation_text), "Installing %s", flip_catalog[app_selected_index].app_name);
|
|
|
snprintf(fhttp.file_path, sizeof(fhttp.file_path), STORAGE_EXT_PATH_PREFIX "/apps/%s/%s.fap", category, flip_catalog[app_selected_index].app_id);
|
|
|
- canvas_draw_str(canvas, 0, 10, installing_text);
|
|
|
+ canvas_draw_str(canvas, 0, 10, installation_text);
|
|
|
canvas_draw_str(canvas, 0, 20, "Sending request..");
|
|
|
uint8_t target = furi_hal_version_get_hw_target();
|
|
|
uint16_t api_major, api_minor;
|
|
|
@@ -404,7 +365,6 @@ bool flip_store_install_app(Canvas *canvas, char *category)
|
|
|
{
|
|
|
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
|
|
|
@@ -440,34 +400,31 @@ int32_t flip_store_handle_app_list(FlipStoreApp *app, int32_t success_view, char
|
|
|
FURI_LOG_E(TAG, "FlipStoreApp is NULL");
|
|
|
return FlipStoreViewPopup;
|
|
|
}
|
|
|
- char url[128];
|
|
|
snprintf(
|
|
|
fhttp.file_path,
|
|
|
sizeof(fhttp.file_path),
|
|
|
- STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/apps.txt");
|
|
|
+ STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/%s.json", category);
|
|
|
|
|
|
fhttp.save_received_data = true;
|
|
|
fhttp.is_bytes_request = false;
|
|
|
- snprintf(url, sizeof(url), "https://www.flipsocial.net/api/flipper/apps/%s/extended/", category);
|
|
|
- char *headers = jsmn("Content-Type", "application/json");
|
|
|
+ char url[128];
|
|
|
+ snprintf(url, sizeof(url), "https://www.flipsocial.net/api/flipper/apps/%s/max/", category);
|
|
|
// async call to the app list with timer
|
|
|
- if (fhttp.state != INACTIVE && flipper_http_get_request_with_headers(url, headers))
|
|
|
+ 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);
|
|
|
- free(headers);
|
|
|
fhttp.state = RECEIVING;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
FURI_LOG_E(TAG, "Failed to send the request");
|
|
|
- free(headers);
|
|
|
fhttp.state = ISSUE;
|
|
|
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_delay_ms(10);
|
|
|
}
|
|
|
furi_timer_stop(fhttp.get_timeout_timer);
|
|
|
if (fhttp.state == ISSUE)
|
|
|
@@ -487,7 +444,7 @@ int32_t flip_store_handle_app_list(FlipStoreApp *app, int32_t success_view, char
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- popup_set_text(app->popup, fhttp.last_response, 0, 10, AlignLeft, AlignTop);
|
|
|
+ popup_set_text(app->popup, fhttp.last_response, 0, 50, AlignLeft, AlignTop);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
@@ -498,14 +455,14 @@ int32_t flip_store_handle_app_list(FlipStoreApp *app, int32_t success_view, char
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- popup_set_text(app->popup, "Failed to received data.", 0, 10, AlignLeft, AlignTop);
|
|
|
+ popup_set_text(app->popup, "Failed to received data.", 0, 50, AlignLeft, AlignTop);
|
|
|
return FlipStoreViewPopup;
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
// process the app list
|
|
|
- if (flip_store_process_app_list())
|
|
|
+ if (flip_store_process_app_list() && submenu && flip_catalog)
|
|
|
{
|
|
|
submenu_reset(*submenu);
|
|
|
// add each app name to submenu
|
|
|
@@ -515,6 +472,10 @@ int32_t flip_store_handle_app_list(FlipStoreApp *app, int32_t success_view, char
|
|
|
{
|
|
|
submenu_add_item(*submenu, flip_catalog[i].app_name, FlipStoreSubmenuIndexStartAppList + i, callback_submenu_choices, app);
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
return success_view;
|
|
|
}
|