Просмотр исходного кода

Merge flip_wifi from https://github.com/jblanked/FlipWiFi

# Conflicts:
#	flip_wifi/alloc/flip_wifi_alloc.c
#	flip_wifi/callback/flip_wifi_callback.c
#	flip_wifi/callback/flip_wifi_callback.h
#	flip_wifi/flipper_http/flipper_http.c
Willy-JL 1 год назад
Родитель
Сommit
af547dc7a2

+ 7 - 3
flip_wifi/CHANGELOG.md

@@ -1,6 +1,10 @@
+## v1.2
+- Updated scan loading and parsing.
+- Added connectivity check on startup. (thanks to Derek Jamison)
+
 ## v1.1
-- Fixed freeze when configs didn't exist (thanks WillyJL)
-- Improved memory allocation
+- Fixed a freeze issue when configurations did not exist (thanks to WillyJL).  
+- Improved memory allocation.  
 
 ## v1.0
-- Initial Release
+- Initial release.

+ 2 - 19
flip_wifi/alloc/flip_wifi_alloc.c

@@ -88,7 +88,7 @@ FlipWiFiApp* flip_wifi_app_alloc() {
     if(!easy_flipper_set_widget(
            &app->widget_info,
            FlipWiFiViewAbout,
-           "FlipWiFi v1.1\n-----\nFlipperHTTP companion app.\nScan and save WiFi networks.\n-----\nwww.github.com/jblanked",
+           "FlipWiFi v1.2\n-----\nFlipperHTTP companion app.\nScan and save WiFi networks.\n-----\nwww.github.com/jblanked",
            callback_to_submenu_main,
            &app->view_dispatcher)) {
         return NULL;
@@ -148,7 +148,7 @@ FlipWiFiApp* flip_wifi_app_alloc() {
     if(!easy_flipper_set_submenu(
            &app->submenu_main,
            FlipWiFiViewSubmenuMain,
-           "FlipWiFi v1.1",
+           "FlipWiFi v1.2",
            easy_flipper_callback_exit_app,
            &app->view_dispatcher)) {
         return NULL;
@@ -180,23 +180,6 @@ FlipWiFiApp* flip_wifi_app_alloc() {
     submenu_add_item(
         app->submenu_main, "Info", FlipWiFiSubmenuIndexAbout, callback_submenu_choices, app);
 
-    // Popup
-    if(!easy_flipper_set_popup(
-           &app->popup,
-           FlipWiFiViewPopup,
-           "Success",
-           0,
-           0,
-           "The WiFi setting has been set.",
-           0,
-           10,
-           popup_callback_saved,
-           callback_to_submenu_saved,
-           &app->view_dispatcher,
-           app)) {
-        return NULL;
-    }
-
     // Load the playlist from storage
     if(!load_playlist(&app->wifi_playlist)) {
         FURI_LOG_E(TAG, "Failed to load playlist");

+ 31 - 0
flip_wifi/app.c

@@ -18,6 +18,37 @@ int32_t flip_wifi_main(void* p) {
         return -1;
     }
 
+    // Thanks to Derek Jamison for the following code snippet:
+    if (app_instance->uart_text_input_buffer_add_ssid != NULL &&
+        app_instance->uart_text_input_buffer_add_password != NULL)
+    {
+        // Try to wait for pong response.
+        uint8_t counter = 10;
+        while (fhttp.state == INACTIVE && --counter > 0)
+        {
+            FURI_LOG_D(TAG, "Waiting for PONG");
+            furi_delay_ms(100);
+        }
+
+        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);
+        }
+    }
+
     // Run the view dispatcher
     view_dispatcher_run(app_instance->view_dispatcher);
 

+ 1 - 1
flip_wifi/application.fam

@@ -9,6 +9,6 @@ App(
     fap_icon_assets="assets",
     fap_author="JBlanked",
     fap_weburl="https://github.com/jblanked/FlipWiFi",
-    fap_version="1.1",
+    fap_version="1.2",
     fap_description="FlipperHTTP companion app.",
 )

BIN
flip_wifi/assets/01-home.png


+ 51 - 65
flip_wifi/callback/flip_wifi_callback.c

@@ -50,7 +50,7 @@ uint32_t callback_to_submenu_saved(void* context) {
     ssid_index = 0;
     return FlipWiFiViewSubmenuSaved;
 }
-void popup_callback_saved(void* context) {
+static void popup_callback_saved(void* context) {
     FlipWiFiApp* app = (FlipWiFiApp*)context;
     if(!app) {
         FURI_LOG_E(TAG, "HelloWorldApp is NULL");
@@ -58,7 +58,7 @@ void popup_callback_saved(void* context) {
     }
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipWiFiViewSubmenuSaved);
 }
-void popup_callback_main(void* context) {
+static void popup_callback_main(void* context) {
     FlipWiFiApp* app = (FlipWiFiApp*)context;
     if(!app) {
         FURI_LOG_E(TAG, "HelloWorldApp is NULL");
@@ -178,7 +178,7 @@ bool flip_wifi_view_input_callback_saved(InputEvent* event, void* context) {
 
 // Function to trim leading and trailing whitespace
 // Returns the trimmed start pointer and updates the length
-char* trim_whitespace(char* start, size_t* length) {
+static char* trim_whitespace(char* start, size_t* length) {
     // Trim leading whitespace
     while(*length > 0 && isspace((unsigned char)*start)) {
         start++;
@@ -193,7 +193,11 @@ char* trim_whitespace(char* start, size_t* length) {
     return start;
 }
 
-bool flip_wifi_handle_scan(FlipWiFiApp* app) {
+static bool flip_wifi_handle_scan() {
+    if(!app_instance) {
+        FURI_LOG_E(TAG, "FlipWiFiApp is NULL");
+        return false;
+    }
     // load the received data from the saved file
     FuriString* scan_data = flipper_http_load_from_file(fhttp.file_path);
     if(scan_data == NULL) {
@@ -272,7 +276,8 @@ bool flip_wifi_handle_scan(FlipWiFiApp* app) {
     }
 
     // Add each SSID as a submenu item
-    submenu_set_header(app->submenu_wifi_scan, "WiFi Nearby");
+    submenu_reset(app_instance->submenu_wifi_scan);
+    submenu_set_header(app_instance->submenu_wifi_scan, "WiFi Nearby");
     for(uint32_t i = 0; i < ssid_count; i++) {
         char* ssid_item = ssid_list[i];
         if(ssid_item == NULL) {
@@ -282,11 +287,11 @@ bool flip_wifi_handle_scan(FlipWiFiApp* app) {
         char ssid[64];
         snprintf(ssid, sizeof(ssid), "%s", ssid_item);
         submenu_add_item(
-            app->submenu_wifi_scan,
+            app_instance->submenu_wifi_scan,
             ssid,
             FlipWiFiSubmenuIndexWiFiScanStart + i,
             callback_submenu_choices,
-            app);
+            app_instance);
     }
     free(data_cstr);
     furi_string_free(scan_data);
@@ -300,8 +305,29 @@ void callback_submenu_choices(void* context, uint32_t index) {
     }
     switch(index) {
     case FlipWiFiSubmenuIndexWiFiScan:
+        // Popup
+        if(!app->popup) {
+            if(!easy_flipper_set_popup(
+                   &app->popup,
+                   FlipWiFiViewPopup,
+                   "Success",
+                   0,
+                   0,
+                   "The WiFi setting has been set.",
+                   0,
+                   10,
+                   popup_callback_saved,
+                   callback_to_submenu_saved,
+                   &app->view_dispatcher,
+                   app)) {
+                return;
+            }
+        }
+        popup_set_header(app->popup, "[ERROR]", 0, 0, AlignLeft, AlignTop);
+        view_set_previous_callback(popup_get_view(app->popup), callback_to_submenu_main);
+        popup_set_callback(app->popup, popup_callback_main);
+
         if(fhttp.state == INACTIVE) {
-            popup_set_header(app->popup, "[ERROR]", 0, 0, AlignLeft, AlignTop);
             popup_set_text(
                 app->popup,
                 "WiFi Devboard Disconnected.\nPlease reconnect the board.",
@@ -309,66 +335,26 @@ void callback_submenu_choices(void* context, uint32_t index) {
                 40,
                 AlignLeft,
                 AlignTop);
-            view_set_previous_callback(popup_get_view(app->popup), callback_to_submenu_main);
-            popup_set_callback(app->popup, popup_callback_main);
-            // switch to the popup view
             view_dispatcher_switch_to_view(app->view_dispatcher, FlipWiFiViewPopup);
-        }
-        // scan for wifi
-        if(!flipper_http_scan_wifi()) {
-            FURI_LOG_E(TAG, "Failed to scan for WiFi");
-            fhttp.state = ISSUE;
-            return;
-        } else // start the async feed request
-        {
-            furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
-            fhttp.state = RECEIVING;
-        }
-        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);
-        // set each SSID as a submenu item
-        if(fhttp.state != IDLE) {
-            fhttp.state = ISSUE;
-            FURI_LOG_E(TAG, "Failed to receive WiFi scan");
-            popup_set_header(app->popup, "[ERROR]", 0, 0, AlignLeft, AlignTop);
-            popup_set_text(
-                app->popup,
-                "Failed to scan...\nTry reconnecting the board!",
-                0,
-                40,
-                AlignLeft,
-                AlignTop);
-            view_set_previous_callback(popup_get_view(app->popup), callback_to_submenu_main);
-            popup_set_callback(app->popup, popup_callback_main);
-            // switch to the popup view
-            view_dispatcher_switch_to_view(app->view_dispatcher, FlipWiFiViewPopup);
-        } else {
-            submenu_reset(app->submenu_wifi_scan);
-            submenu_set_header(app->submenu_wifi_scan, "WiFi Nearby");
-            if(flip_wifi_handle_scan(app)) {
-                // switch to the submenu
-                view_dispatcher_switch_to_view(app->view_dispatcher, FlipWiFiViewSubmenuScan);
-            } else {
-                fhttp.state = ISSUE;
-                FURI_LOG_E(TAG, "Failed to handle WiFi scan");
-                popup_set_header(app->popup, "[ERROR]", 0, 0, AlignLeft, AlignTop);
-                popup_set_text(
-                    app->popup,
-                    "Failed to scan...\nTry reconnecting the board!",
-                    0,
-                    40,
-                    AlignLeft,
-                    AlignTop);
-                view_set_previous_callback(popup_get_view(app->popup), callback_to_submenu_main);
-                popup_set_callback(app->popup, popup_callback_main);
-                // switch to the popup view
-                view_dispatcher_switch_to_view(app->view_dispatcher, FlipWiFiViewPopup);
-            }
             return;
         }
+
+        // update the text in case the loading task fails
+        popup_set_text(
+            app->popup,
+            "Failed to scan...\nTry reconnecting the board!",
+            0,
+            40,
+            AlignLeft,
+            AlignTop);
+
+        // scan for wifi ad parse the results
+        flipper_http_loading_task(
+            flipper_http_scan_wifi,
+            flip_wifi_handle_scan,
+            FlipWiFiViewSubmenuScan,
+            FlipWiFiViewPopup,
+            &app->view_dispatcher);
         break;
     case FlipWiFiSubmenuIndexWiFiSaved:
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipWiFiViewSubmenuSaved);

+ 0 - 10
flip_wifi/callback/flip_wifi_callback.h

@@ -17,10 +17,6 @@ uint32_t callback_to_submenu_scan(void* context);
 
 uint32_t callback_to_submenu_saved(void* context);
 
-void popup_callback_saved(void* context);
-
-void popup_callback_main(void* context);
-
 // Callback for drawing the main screen
 void flip_wifi_view_draw_callback_scan(Canvas* canvas, void* model);
 
@@ -32,12 +28,6 @@ bool flip_wifi_view_input_callback_scan(InputEvent* event, void* context);
 // Input callback for the view (async input handling)
 bool flip_wifi_view_input_callback_saved(InputEvent* event, void* context);
 
-// Function to trim leading and trailing whitespace
-// Returns the trimmed start pointer and updates the length
-char* trim_whitespace(char* start, size_t* length);
-
-bool flip_wifi_handle_scan(FlipWiFiApp* app);
-
 void callback_submenu_choices(void* context, uint32_t index);
 
 void flip_wifi_text_updated_password_scan(void* context);

+ 2 - 0
flip_wifi/easy_flipper/easy_flipper.h

@@ -15,6 +15,8 @@
 #include <gui/modules/text_box.h>
 #include <gui/modules/variable_item_list.h>
 #include <gui/modules/dialog_ex.h>
+#include <notification/notification.h>
+#include <dialogs/dialogs.h>
 #include <gui/modules/popup.h>
 #include <gui/modules/loading.h>
 #include <stdio.h>

+ 51 - 1
flip_wifi/flipper_http/flipper_http.c

@@ -581,6 +581,7 @@ bool flipper_http_scan_wifi() {
     }
     // custom for FlipWiFi app
     fhttp.just_started_get = true;
+
     snprintf(
         fhttp.file_path,
         sizeof(fhttp.file_path),
@@ -987,10 +988,12 @@ void flipper_http_rx_callback(const char* line, void* context) {
         if(!flipper_http_append_to_file(
                line, strlen(line), fhttp.just_started_get, fhttp.file_path)) {
             FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
-            fhttp.just_started_get = false;
             fhttp.state = ISSUE;
             return;
         }
+        if(fhttp.just_started_get) {
+            fhttp.just_started_get = false;
+        }
     }
 
     // Check if we've started receiving data from a GET request
@@ -1314,3 +1317,50 @@ bool flipper_http_process_response_async(bool (*http_request)(void), bool (*pars
     }
     return true;
 }
+
+/**
+ * @brief Perform a task while displaying a loading screen
+ * @param http_request The function to send the request
+ * @param parse_response The function to parse the response
+ * @param success_view_id The view ID to switch to on success
+ * @param failure_view_id The view ID to switch to on failure
+ * @param view_dispatcher The view dispatcher to use
+ * @return
+ */
+void flipper_http_loading_task(
+    bool (*http_request)(void),
+    bool (*parse_response)(void),
+    uint32_t success_view_id,
+    uint32_t failure_view_id,
+    ViewDispatcher** view_dispatcher) {
+    Loading* loading;
+    int32_t loading_view_id = 987654321; // Random ID
+
+    loading = loading_alloc();
+    if(!loading) {
+        FURI_LOG_E(HTTP_TAG, "Failed to allocate loading");
+        view_dispatcher_switch_to_view(*view_dispatcher, failure_view_id);
+
+        return;
+    }
+
+    view_dispatcher_add_view(*view_dispatcher, loading_view_id, loading_get_view(loading));
+
+    // Switch to the loading view
+    view_dispatcher_switch_to_view(*view_dispatcher, loading_view_id);
+
+    // Make the request
+    if(!flipper_http_process_response_async(http_request, parse_response)) {
+        FURI_LOG_E(HTTP_TAG, "Failed to make request");
+        view_dispatcher_switch_to_view(*view_dispatcher, failure_view_id);
+        view_dispatcher_remove_view(*view_dispatcher, loading_view_id);
+        loading_free(loading);
+
+        return;
+    }
+
+    // Switch to the success view
+    view_dispatcher_switch_to_view(*view_dispatcher, success_view_id);
+    view_dispatcher_remove_view(*view_dispatcher, loading_view_id);
+    loading_free(loading);
+}

+ 19 - 0
flip_wifi/flipper_http/flipper_http.h

@@ -2,6 +2,10 @@
 #ifndef FLIPPER_HTTP_H
 #define FLIPPER_HTTP_H
 
+#include <gui/gui.h>
+#include <gui/view.h>
+#include <gui/view_dispatcher.h>
+#include <gui/modules/loading.h>
 #include <furi.h>
 #include <furi_hal.h>
 #include <furi_hal_gpio.h>
@@ -361,4 +365,19 @@ char* trim(const char* str);
  */
 bool flipper_http_process_response_async(bool (*http_request)(void), bool (*parse_json)(void));
 
+/**
+ * @brief Perform a task while displaying a loading screen
+ * @param http_request The function to send the request
+ * @param parse_response The function to parse the response
+ * @param success_view_id The view ID to switch to on success
+ * @param failure_view_id The view ID to switch to on failure
+ * @param view_dispatcher The view dispatcher to use
+ * @return
+ */
+void flipper_http_loading_task(bool (*http_request)(void),
+                               bool (*parse_response)(void),
+                               uint32_t success_view_id,
+                               uint32_t failure_view_id,
+                               ViewDispatcher **view_dispatcher);
+
 #endif // FLIPPER_HTTP_H