Browse Source

Update FlipperHTTP and EasyFipper

jblanked 1 năm trước cách đây
mục cha
commit
da9297b0e2
5 tập tin đã thay đổi với 72 bổ sung58 xóa
  1. 16 24
      app.c
  2. 20 0
      easy_flipper/easy_flipper.c
  3. 5 0
      easy_flipper/easy_flipper.h
  4. 27 30
      flipper_http/flipper_http.c
  5. 4 4
      flipper_http/flipper_http.h

+ 16 - 24
app.c

@@ -17,19 +17,20 @@ int32_t main_flip_social(void *p)
     if (!app_instance)
     {
         // Allocation failed
+        FURI_LOG_E(TAG, "Failed to allocate FlipSocialApp");
         return -1; // Indicate failure
     }
 
-    if (!flipper_http_ping())
+    // check if board is connected (Derek Jamison)
+    // initialize the http
+    if (flipper_http_init(flipper_http_rx_callback, app_instance))
     {
-        FURI_LOG_E(TAG, "Failed to ping the device");
-        return -1;
-    }
+        if (!flipper_http_ping())
+        {
+            FURI_LOG_E(TAG, "Failed to ping the device");
+            return -1;
+        }
 
-    // Thanks to Derek Jamison for the following edits
-    if (app_instance->wifi_ssid_logged_out != NULL &&
-        app_instance->wifi_password_logged_out != NULL)
-    {
         // Try to wait for pong response.
         uint8_t counter = 10;
         while (fhttp.state == INACTIVE && --counter > 0)
@@ -39,22 +40,13 @@ int32_t main_flip_social(void *p)
         }
 
         if (counter == 0)
-        {
-            DialogsApp *dialogs = furi_record_open(RECORD_DIALOGS);
-            DialogMessage *message = dialog_message_alloc();
-            dialog_message_set_header(
-                message, "[FlipperHTTP Error]", 64, 0, AlignCenter, AlignTop);
-            dialog_message_set_text(
-                message,
-                "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.",
-                0,
-                63,
-                AlignLeft,
-                AlignBottom);
-            dialog_message_show(dialogs, message);
-            dialog_message_free(message);
-            furi_record_close(RECORD_DIALOGS);
-        }
+            easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
+
+        flipper_http_deinit();
+    }
+    else
+    {
+        easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
     }
 
     // Run the view dispatcher

+ 20 - 0
easy_flipper/easy_flipper.c

@@ -1,5 +1,25 @@
 #include <easy_flipper/easy_flipper.h>
 
+void easy_flipper_dialog(
+    char *header,
+    char *text)
+{
+    DialogsApp *dialogs = furi_record_open(RECORD_DIALOGS);
+    DialogMessage *message = dialog_message_alloc();
+    dialog_message_set_header(
+        message, header, 64, 0, AlignCenter, AlignTop);
+    dialog_message_set_text(
+        message,
+        text,
+        0,
+        63,
+        AlignLeft,
+        AlignBottom);
+    dialog_message_show(dialogs, message);
+    dialog_message_free(message);
+    furi_record_close(RECORD_DIALOGS);
+}
+
 /**
  * @brief Navigation callback for exiting the application
  * @param context The context - unused

+ 5 - 0
easy_flipper/easy_flipper.h

@@ -8,6 +8,7 @@
 #include <gui/view.h>
 #include <gui/modules/submenu.h>
 #include <gui/view_dispatcher.h>
+#include <gui/elements.h>
 #include <gui/modules/menu.h>
 #include <gui/modules/submenu.h>
 #include <gui/modules/widget.h>
@@ -26,6 +27,10 @@
 
 #define EASY_TAG "EasyFlipper"
 
+void easy_flipper_dialog(
+    char *header,
+    char *text);
+
 /**
  * @brief Navigation callback for exiting the application
  * @param context The context - unused

+ 27 - 30
flipper_http/flipper_http.c

@@ -1,8 +1,5 @@
 #include <flipper_http/flipper_http.h> // change this to where flipper_http.h is located
-FlipperHTTP fhttp;
-char rx_line_buffer[RX_LINE_BUFFER_SIZE];
-uint8_t file_buffer[FILE_BUFFER_SIZE];
-size_t file_buffer_len = 0;
+FlipperHTTP fhttp = {0};
 // Function to append received data to file
 // make sure to initialize the file path before calling this function
 bool flipper_http_append_to_file(
@@ -187,19 +184,19 @@ int32_t flipper_http_worker(void *context)
                 if (fhttp.save_bytes)
                 {
                     // Add byte to the buffer
-                    file_buffer[file_buffer_len++] = c;
+                    fhttp.file_buffer[fhttp.file_buffer_len++] = c;
                     // Write to file if buffer is full
-                    if (file_buffer_len >= FILE_BUFFER_SIZE)
+                    if (fhttp.file_buffer_len >= FILE_BUFFER_SIZE)
                     {
                         if (!flipper_http_append_to_file(
-                                file_buffer,
-                                file_buffer_len,
+                                fhttp.file_buffer,
+                                fhttp.file_buffer_len,
                                 fhttp.just_started_bytes,
                                 fhttp.file_path))
                         {
                             FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
                         }
-                        file_buffer_len = 0;
+                        fhttp.file_buffer_len = 0;
                         fhttp.just_started_bytes = false;
                     }
                 }
@@ -210,17 +207,17 @@ int32_t flipper_http_worker(void *context)
                     // Handle line buffering
                     if (c == '\n' || rx_line_pos >= RX_LINE_BUFFER_SIZE - 1)
                     {
-                        rx_line_buffer[rx_line_pos] = '\0'; // Null-terminate the line
+                        fhttp.rx_line_buffer[rx_line_pos] = '\0'; // Null-terminate the line
 
                         // Invoke the callback with the complete line
-                        fhttp.handle_rx_line_cb(rx_line_buffer, fhttp.callback_context);
+                        fhttp.handle_rx_line_cb(fhttp.rx_line_buffer, fhttp.callback_context);
 
                         // Reset the line buffer position
                         rx_line_pos = 0;
                     }
                     else
                     {
-                        rx_line_buffer[rx_line_pos++] = c; // Add character to the line buffer
+                        fhttp.rx_line_buffer[rx_line_pos++] = c; // Add character to the line buffer
                     }
                 }
             }
@@ -1114,27 +1111,27 @@ void flipper_http_rx_callback(const char *line, void *context)
                 const char marker[] = "[GET/END]";
                 const size_t marker_len = sizeof(marker) - 1; // Exclude null terminator
 
-                for (size_t i = 0; i <= file_buffer_len - marker_len; i++)
+                for (size_t i = 0; i <= fhttp.file_buffer_len - marker_len; i++)
                 {
                     // Check if the marker is found
-                    if (memcmp(&file_buffer[i], marker, marker_len) == 0)
+                    if (memcmp(&fhttp.file_buffer[i], marker, marker_len) == 0)
                     {
                         // Remove the marker by shifting the remaining data left
-                        size_t remaining_len = file_buffer_len - (i + marker_len);
-                        memmove(&file_buffer[i], &file_buffer[i + marker_len], remaining_len);
-                        file_buffer_len -= marker_len;
+                        size_t remaining_len = fhttp.file_buffer_len - (i + marker_len);
+                        memmove(&fhttp.file_buffer[i], &fhttp.file_buffer[i + marker_len], remaining_len);
+                        fhttp.file_buffer_len -= marker_len;
                         break;
                     }
                 }
 
                 // If there is data left in the buffer, append it to the file
-                if (file_buffer_len > 0)
+                if (fhttp.file_buffer_len > 0)
                 {
-                    if (!flipper_http_append_to_file(file_buffer, file_buffer_len, false, fhttp.file_path))
+                    if (!flipper_http_append_to_file(fhttp.file_buffer, fhttp.file_buffer_len, false, fhttp.file_path))
                     {
                         FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
                     }
-                    file_buffer_len = 0;
+                    fhttp.file_buffer_len = 0;
                 }
             }
 
@@ -1184,27 +1181,27 @@ void flipper_http_rx_callback(const char *line, void *context)
                 const char marker[] = "[POST/END]";
                 const size_t marker_len = sizeof(marker) - 1; // Exclude null terminator
 
-                for (size_t i = 0; i <= file_buffer_len - marker_len; i++)
+                for (size_t i = 0; i <= fhttp.file_buffer_len - marker_len; i++)
                 {
                     // Check if the marker is found
-                    if (memcmp(&file_buffer[i], marker, marker_len) == 0)
+                    if (memcmp(&fhttp.file_buffer[i], marker, marker_len) == 0)
                     {
                         // Remove the marker by shifting the remaining data left
-                        size_t remaining_len = file_buffer_len - (i + marker_len);
-                        memmove(&file_buffer[i], &file_buffer[i + marker_len], remaining_len);
-                        file_buffer_len -= marker_len;
+                        size_t remaining_len = fhttp.file_buffer_len - (i + marker_len);
+                        memmove(&fhttp.file_buffer[i], &fhttp.file_buffer[i + marker_len], remaining_len);
+                        fhttp.file_buffer_len -= marker_len;
                         break;
                     }
                 }
 
                 // If there is data left in the buffer, append it to the file
-                if (file_buffer_len > 0)
+                if (fhttp.file_buffer_len > 0)
                 {
-                    if (!flipper_http_append_to_file(file_buffer, file_buffer_len, false, fhttp.file_path))
+                    if (!flipper_http_append_to_file(fhttp.file_buffer, fhttp.file_buffer_len, false, fhttp.file_path))
                     {
                         FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
                     }
-                    file_buffer_len = 0;
+                    fhttp.file_buffer_len = 0;
                 }
             }
 
@@ -1332,7 +1329,7 @@ void flipper_http_rx_callback(const char *line, void *context)
         // for GET request, save data only if it's a bytes request
         fhttp.save_bytes = fhttp.is_bytes_request;
         fhttp.just_started_bytes = true;
-        file_buffer_len = 0;
+        fhttp.file_buffer_len = 0;
         return;
     }
     else if (strstr(line, "[POST/SUCCESS]") != NULL)
@@ -1344,7 +1341,7 @@ void flipper_http_rx_callback(const char *line, void *context)
         // for POST request, save data only if it's a bytes request
         fhttp.save_bytes = fhttp.is_bytes_request;
         fhttp.just_started_bytes = true;
-        file_buffer_len = 0;
+        fhttp.file_buffer_len = 0;
         return;
     }
     else if (strstr(line, "[PUT/SUCCESS]") != NULL)

+ 4 - 4
flipper_http/flipper_http.h

@@ -82,13 +82,13 @@ typedef struct
     bool save_received_data;   // Flag to save the received data to a file
 
     bool just_started_bytes; // Indicates if bytes data reception has just started
+
+    char rx_line_buffer[RX_LINE_BUFFER_SIZE];
+    uint8_t file_buffer[FILE_BUFFER_SIZE];
+    size_t file_buffer_len;
 } FlipperHTTP;
 
 extern FlipperHTTP fhttp;
-// Global static array for the line buffer
-extern char rx_line_buffer[RX_LINE_BUFFER_SIZE];
-extern uint8_t file_buffer[FILE_BUFFER_SIZE];
-extern size_t file_buffer_len;
 
 // fhttp.last_response holds the last received data from the UART