Explorar o código

Raspberry Pi Pico W Support

jblanked hai 1 ano
pai
achega
1f23df8e7e
Modificáronse 7 ficheiros con 129 adicións e 19 borrados
  1. BIN=BIN
      .DS_Store
  2. 2 0
      .gitignore
  3. 5 5
      app.c
  4. 2 2
      flip_store_apps.h
  5. 0 5
      flip_store_free.h
  6. 2 7
      flipper_http.h
  7. 118 0
      jsmn.h

BIN=BIN
.DS_Store


+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+
+.DS_Store

+ 5 - 5
app.c

@@ -13,16 +13,16 @@ int32_t main_flip_store(void *p)
     // Initialize the Hello World application
     FlipStoreApp *app = flip_store_app_alloc();
 
-    // send settings and connect wifi
-    if (!flipper_http_connect_wifi())
+    if (!flipper_http_ping())
     {
-        FURI_LOG_E(TAG, "Failed to connect to WiFi");
+        FURI_LOG_E(TAG, "Failed to ping the device");
         return -1;
     }
 
-    if (!flipper_http_ping())
+    // send settings and connect wifi
+    if (!flipper_http_connect_wifi())
     {
-        FURI_LOG_E(TAG, "Failed to ping the device");
+        FURI_LOG_E(TAG, "Failed to connect to WiFi");
         return -1;
     }
 

+ 2 - 2
flip_store_apps.h

@@ -337,7 +337,7 @@ 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/octet-stream\"}}");
+    return flipper_http_get_request_bytes(url, jsmn("Content-Type", "application/octet-stream"));
 }
 
 void flip_store_request_error(Canvas *canvas)
@@ -451,7 +451,7 @@ int32_t flip_store_handle_app_list(FlipStoreApp *app, int32_t success_view, char
     // 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\"}"))
+    if (fhttp.state != INACTIVE && flipper_http_get_request_with_headers(url, jsmn("Content-Type", "application/json")))
     {
         furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
         fhttp.state = RECEIVING;

+ 0 - 5
flip_store_free.h

@@ -10,11 +10,6 @@ static void flip_store_app_free(FlipStoreApp *app)
         return;
     }
 
-    if (!flipper_http_disconnect_wifi())
-    {
-        FURI_LOG_E(TAG, "Failed to disconnect from wifi");
-    }
-
     // Free View(s)
     if (app->view_main)
     {

+ 2 - 7
flipper_http.h

@@ -15,7 +15,7 @@
 #define HTTP_TAG "FlipStore"              // change this to your app name
 #define http_tag "flip_store"             // change this to your app id
 #define UART_CH (FuriHalSerialIdUsart)    // UART channel
-#define TIMEOUT_DURATION_TICKS (5 * 1000) // 5 seconds
+#define TIMEOUT_DURATION_TICKS (6 * 1000) // 6 seconds
 #define BAUDRATE (115200)                 // UART baudrate
 #define RX_BUF_SIZE 1024                  // UART RX buffer size
 #define RX_LINE_BUFFER_SIZE 5000          // UART RX line buffer size (increase for large responses)
@@ -259,11 +259,6 @@ static int32_t flipper_http_worker(void *context)
     // Reset the file buffer length
     file_buffer_len = 0;
 
-    if (fhttp.save_data)
-    {
-        FURI_LOG_I(HTTP_TAG, "Data saved to file: %s", fhttp.file_path);
-    }
-
     return 0;
 }
 
@@ -912,7 +907,7 @@ void flipper_http_rx_callback(const char *line, void *context)
     }
 
     // Uncomment below line to log the data received over UART
-    // FURI_LOG_I(HTTP_TAG, "Received UART line: %s", line);
+    FURI_LOG_I(HTTP_TAG, "Received UART line: %s", line);
 
     // Check if we've started receiving data from a GET request
     if (fhttp.started_receiving_get)

+ 118 - 0
jsmn.h

@@ -537,6 +537,19 @@ extern "C"
 #include <stdio.h>
 #include <furi.h>
 
+// Helper function to create a JSON object
+char *jsmn(const char *key, const char *value)
+{
+  int length = strlen(key) + strlen(value) + 8;         // Calculate required length
+  char *result = (char *)malloc(length * sizeof(char)); // Allocate memory
+  if (result == NULL)
+  {
+    return NULL; // Handle memory allocation failure
+  }
+  snprintf(result, length, "{\"%s\":\"%s\"}", key, value);
+  return result; // Caller is responsible for freeing this memory
+}
+
 // Helper function to compare JSON keys
 int jsoneq(const char *json, jsmntok_t *tok, const char *s)
 {
@@ -615,6 +628,111 @@ char *get_json_value(char *key, char *json_data, uint32_t max_tokens)
   return NULL; // Return NULL if something goes wrong
 }
 
+// Revised get_json_array_value function
+char *get_json_array_value(char *key, uint32_t index, char *json_data, uint32_t max_tokens)
+{
+  // Retrieve the array string for the given key
+  char *array_str = get_json_value(key, json_data, max_tokens);
+  if (array_str == NULL)
+  {
+    FURI_LOG_E("JSMM.H", "Failed to get array for key: %s", key);
+    return NULL;
+  }
+
+  // Initialize the JSON parser
+  jsmn_parser parser;
+  jsmn_init(&parser);
+
+  // Allocate memory for JSON tokens
+  jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens);
+  if (tokens == NULL)
+  {
+    FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
+    free(array_str);
+    return NULL;
+  }
+
+  // Parse the JSON array
+  int ret = jsmn_parse(&parser, array_str, strlen(array_str), tokens, max_tokens);
+  if (ret < 0)
+  {
+    FURI_LOG_E("JSMM.H", "Failed to parse JSON array: %d", ret);
+    free(tokens);
+    free(array_str);
+    return NULL;
+  }
+
+  // Ensure the root element is an array
+  if (ret < 1 || tokens[0].type != JSMN_ARRAY)
+  {
+    FURI_LOG_E("JSMM.H", "Value for key '%s' is not an array.", key);
+    free(tokens);
+    free(array_str);
+    return NULL;
+  }
+
+  // Check if the index is within bounds
+  if (index >= (uint32_t)tokens[0].size)
+  {
+    FURI_LOG_E("JSMM.H", "Index %lu out of bounds for array with size %d.", (unsigned long)index, tokens[0].size);
+    free(tokens);
+    free(array_str);
+    return NULL;
+  }
+
+  // Locate the token corresponding to the desired array element
+  int current_token = 1; // Start after the array token
+  for (uint32_t i = 0; i < index; i++)
+  {
+    if (tokens[current_token].type == JSMN_OBJECT)
+    {
+      // For objects, skip all key-value pairs
+      current_token += 1 + 2 * tokens[current_token].size;
+    }
+    else if (tokens[current_token].type == JSMN_ARRAY)
+    {
+      // For nested arrays, skip all elements
+      current_token += 1 + tokens[current_token].size;
+    }
+    else
+    {
+      // For primitive types, simply move to the next token
+      current_token += 1;
+    }
+
+    // Safety check to prevent out-of-bounds
+    if (current_token >= ret)
+    {
+      FURI_LOG_E("JSMM.H", "Unexpected end of tokens while traversing array.");
+      free(tokens);
+      free(array_str);
+      return NULL;
+    }
+  }
+
+  // Extract the array element
+  jsmntok_t element = tokens[current_token];
+  int length = element.end - element.start;
+  char *value = malloc(length + 1);
+  if (value == NULL)
+  {
+    FURI_LOG_E("JSMM.H", "Failed to allocate memory for array element.");
+    free(tokens);
+    free(array_str);
+    return NULL;
+  }
+
+  // Copy the element value to a new string
+  strncpy(value, array_str + element.start, length);
+  value[length] = '\0'; // Null-terminate the string
+
+  // Clean up
+  free(tokens);
+  free(array_str);
+
+  return value;
+}
+
 // Revised get_json_array_values function with correct token skipping
 char **get_json_array_values(char *key, char *json_data, uint32_t max_tokens, int *num_values)
 {