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

Edit by WIlly-JL + Improved parsing

jblanked 1 год назад
Родитель
Сommit
a60c65d8c6
4 измененных файлов с 105 добавлено и 52 удалено
  1. 17 6
      callback/flip_weather_callback.c
  2. 72 44
      flipper_http/flipper_http.c
  3. 1 0
      flipper_http/flipper_http.h
  4. 15 2
      parse/flip_weather_parse.c

+ 17 - 6
callback/flip_weather_callback.c

@@ -71,11 +71,11 @@ void flip_weather_handle_gps_draw(Canvas *canvas, bool show_gps_data)
             }
             }
         }
         }
         // check status
         // check status
-        else if (fhttp.state == ISSUE || !get_request_success || fhttp.last_response == NULL)
+        else if (fhttp.state == ISSUE || !get_request_success)
         {
         {
             flip_weather_request_error(canvas);
             flip_weather_request_error(canvas);
         }
         }
-        else if (fhttp.state == IDLE && fhttp.last_response != NULL)
+        else if (fhttp.state == IDLE)
         {
         {
             // success, draw GPS
             // success, draw GPS
             process_geo_location();
             process_geo_location();
@@ -116,7 +116,7 @@ void flip_weather_view_draw_callback_weather(Canvas *canvas, void *model)
         return;
         return;
     }
     }
 
 
-    canvas_draw_str(canvas, 0, 10, "Loading Weather...");
+    canvas_draw_str(canvas, 0, 10, "Loading location data...");
     // handle geo location until it's processed and then handle weather
     // handle geo location until it's processed and then handle weather
 
 
     // start the process
     // start the process
@@ -127,16 +127,20 @@ void flip_weather_view_draw_callback_weather(Canvas *canvas, void *model)
     // wait until geo location is processed
     // wait until geo location is processed
     if (!sent_get_request || !get_request_success || fhttp.state == RECEIVING)
     if (!sent_get_request || !get_request_success || fhttp.state == RECEIVING)
     {
     {
+        canvas_draw_str(canvas, 0, 22, "Receiving data...");
         return;
         return;
     }
     }
     // get/set geo lcoation once
     // get/set geo lcoation once
     if (!geo_information_processed)
     if (!geo_information_processed)
     {
     {
         flip_weather_handle_gps_draw(canvas, false);
         flip_weather_handle_gps_draw(canvas, false);
+        canvas_draw_str(canvas, 0, 34, "Parsed location data.");
     }
     }
     // start the weather process
     // start the weather process
     if (!sent_weather_request && fhttp.state == IDLE)
     if (!sent_weather_request && fhttp.state == IDLE)
     {
     {
+        canvas_clear(canvas);
+        canvas_draw_str(canvas, 0, 10, "Getting Weather...");
         sent_weather_request = true;
         sent_weather_request = true;
         char url[512];
         char url[512];
         char *lattitude = lat_data + 10;
         char *lattitude = lat_data + 10;
@@ -163,9 +167,10 @@ void flip_weather_view_draw_callback_weather(Canvas *canvas, void *model)
         }
         }
 
 
         // check status
         // check status
-        if (fhttp.state == ISSUE || !weather_request_success || fhttp.last_response == NULL)
+        if (fhttp.state == ISSUE || !weather_request_success)
         {
         {
             flip_weather_request_error(canvas);
             flip_weather_request_error(canvas);
+            fhttp.state = ISSUE;
         }
         }
         else
         else
         {
         {
@@ -226,14 +231,20 @@ void callback_submenu_choices(void *context, uint32_t index)
         {
         {
             view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewPopupError);
             view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewPopupError);
         }
         }
-        view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewWeather);
+        else
+        {
+            view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewWeather);
+        }
         break;
         break;
     case FlipWeatherSubmenuIndexGPS:
     case FlipWeatherSubmenuIndexGPS:
         if (!flip_weather_handle_ip_address())
         if (!flip_weather_handle_ip_address())
         {
         {
             view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewPopupError);
             view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewPopupError);
         }
         }
-        view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewGPS);
+        else
+        {
+            view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewGPS);
+        }
         break;
         break;
     case FlipWeatherSubmenuIndexAbout:
     case FlipWeatherSubmenuIndexAbout:
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewAbout);
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipWeatherViewAbout);

+ 72 - 44
flipper_http/flipper_http.c

@@ -2,6 +2,7 @@
 FlipperHTTP fhttp;
 FlipperHTTP fhttp;
 char rx_line_buffer[RX_LINE_BUFFER_SIZE];
 char rx_line_buffer[RX_LINE_BUFFER_SIZE];
 uint8_t file_buffer[FILE_BUFFER_SIZE];
 uint8_t file_buffer[FILE_BUFFER_SIZE];
+size_t file_buffer_len = 0;
 // Function to append received data to file
 // Function to append received data to file
 // make sure to initialize the file path before calling this function
 // make sure to initialize the file path before calling this function
 bool flipper_http_append_to_file(
 bool flipper_http_append_to_file(
@@ -147,14 +148,15 @@ int32_t flipper_http_worker(void *context)
 {
 {
     UNUSED(context);
     UNUSED(context);
     size_t rx_line_pos = 0;
     size_t rx_line_pos = 0;
-    static size_t file_buffer_len = 0;
 
 
     while (1)
     while (1)
     {
     {
         uint32_t events = furi_thread_flags_wait(
         uint32_t events = furi_thread_flags_wait(
             WorkerEvtStop | WorkerEvtRxDone, FuriFlagWaitAny, FuriWaitForever);
             WorkerEvtStop | WorkerEvtRxDone, FuriFlagWaitAny, FuriWaitForever);
         if (events & WorkerEvtStop)
         if (events & WorkerEvtStop)
+        {
             break;
             break;
+        }
         if (events & WorkerEvtRxDone)
         if (events & WorkerEvtRxDone)
         {
         {
             // Continuously read from the stream buffer until it's empty
             // Continuously read from the stream buffer until it's empty
@@ -210,45 +212,6 @@ int32_t flipper_http_worker(void *context)
         }
         }
     }
     }
 
 
-    if (fhttp.save_bytes)
-    {
-        // Write the remaining data to the file
-        if (file_buffer_len > 0)
-        {
-            if (!flipper_http_append_to_file(file_buffer, file_buffer_len, false, fhttp.file_path))
-            {
-                FURI_LOG_E(HTTP_TAG, "Failed to append remaining data to file");
-            }
-        }
-    }
-
-    // remove [POST/END] and/or [GET/END] from the file
-    if (fhttp.save_bytes)
-    {
-        char *end = NULL;
-        if ((end = strstr(fhttp.file_path, "[POST/END]")) != NULL)
-        {
-            *end = '\0';
-        }
-        else if ((end = strstr(fhttp.file_path, "[GET/END]")) != NULL)
-        {
-            *end = '\0';
-        }
-    }
-
-    // remove newline from the from the end of the file
-    if (fhttp.save_bytes)
-    {
-        char *end = NULL;
-        if ((end = strstr(fhttp.file_path, "\n")) != NULL)
-        {
-            *end = '\0';
-        }
-    }
-
-    // Reset the file buffer length
-    file_buffer_len = 0;
-
     return 0;
     return 0;
 }
 }
 // Timer callback function
 // Timer callback function
@@ -468,6 +431,7 @@ bool flipper_http_send_data(const char *data)
     if (data_length == 0)
     if (data_length == 0)
     {
     {
         FURI_LOG_E("FlipperHTTP", "Attempted to send empty data.");
         FURI_LOG_E("FlipperHTTP", "Attempted to send empty data.");
+        fhttp.state = ISSUE;
         return false;
         return false;
     }
     }
 
 
@@ -497,7 +461,7 @@ bool flipper_http_send_data(const char *data)
 
 
     // Uncomment below line to log the data sent over UART
     // Uncomment below line to log the data sent over UART
     // FURI_LOG_I("FlipperHTTP", "Sent data over UART: %s", send_buffer);
     // FURI_LOG_I("FlipperHTTP", "Sent data over UART: %s", send_buffer);
-    fhttp.state = IDLE;
+    // fhttp.state = IDLE;
     return true;
     return true;
 }
 }
 
 
@@ -799,7 +763,7 @@ bool flipper_http_get_request(const char *url)
     }
     }
 
 
     // Prepare GET request command
     // Prepare GET request command
-    char command[256];
+    char command[512];
     int ret = snprintf(command, sizeof(command), "[GET]%s", url);
     int ret = snprintf(command, sizeof(command), "[GET]%s", url);
     if (ret < 0 || ret >= (int)sizeof(command))
     if (ret < 0 || ret >= (int)sizeof(command))
     {
     {
@@ -1128,8 +1092,39 @@ void flipper_http_rx_callback(const char *line, void *context)
             fhttp.just_started_get = false;
             fhttp.just_started_get = false;
             fhttp.state = IDLE;
             fhttp.state = IDLE;
             fhttp.save_bytes = false;
             fhttp.save_bytes = false;
-            fhttp.is_bytes_request = false;
             fhttp.save_received_data = false;
             fhttp.save_received_data = false;
+
+            if (fhttp.is_bytes_request)
+            {
+                // Search for the binary marker `[GET/END]` in the file buffer
+                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++)
+                {
+                    // Check if the marker is found
+                    if (memcmp(&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;
+                        break;
+                    }
+                }
+
+                // If there is data left in the buffer, append it to the file
+                if (file_buffer_len > 0)
+                {
+                    if (!flipper_http_append_to_file(file_buffer, file_buffer_len, false, fhttp.file_path))
+                    {
+                        FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
+                    }
+                    file_buffer_len = 0;
+                }
+            }
+
+            fhttp.is_bytes_request = false;
             return;
             return;
         }
         }
 
 
@@ -1167,8 +1162,39 @@ void flipper_http_rx_callback(const char *line, void *context)
             fhttp.just_started_post = false;
             fhttp.just_started_post = false;
             fhttp.state = IDLE;
             fhttp.state = IDLE;
             fhttp.save_bytes = false;
             fhttp.save_bytes = false;
-            fhttp.is_bytes_request = false;
             fhttp.save_received_data = false;
             fhttp.save_received_data = false;
+
+            if (fhttp.is_bytes_request)
+            {
+                // Search for the binary marker `[POST/END]` in the file buffer
+                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++)
+                {
+                    // Check if the marker is found
+                    if (memcmp(&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;
+                        break;
+                    }
+                }
+
+                // If there is data left in the buffer, append it to the file
+                if (file_buffer_len > 0)
+                {
+                    if (!flipper_http_append_to_file(file_buffer, file_buffer_len, false, fhttp.file_path))
+                    {
+                        FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
+                    }
+                    file_buffer_len = 0;
+                }
+            }
+
+            fhttp.is_bytes_request = false;
             return;
             return;
         }
         }
 
 
@@ -1291,6 +1317,7 @@ void flipper_http_rx_callback(const char *line, void *context)
         fhttp.state = RECEIVING;
         fhttp.state = RECEIVING;
         // for GET request, save data only if it's a bytes request
         // for GET request, save data only if it's a bytes request
         fhttp.save_bytes = fhttp.is_bytes_request;
         fhttp.save_bytes = fhttp.is_bytes_request;
+        file_buffer_len = 0;
         return;
         return;
     }
     }
     else if (strstr(line, "[POST/SUCCESS]") != NULL)
     else if (strstr(line, "[POST/SUCCESS]") != NULL)
@@ -1301,6 +1328,7 @@ void flipper_http_rx_callback(const char *line, void *context)
         fhttp.state = RECEIVING;
         fhttp.state = RECEIVING;
         // for POST request, save data only if it's a bytes request
         // for POST request, save data only if it's a bytes request
         fhttp.save_bytes = fhttp.is_bytes_request;
         fhttp.save_bytes = fhttp.is_bytes_request;
+        file_buffer_len = 0;
         return;
         return;
     }
     }
     else if (strstr(line, "[PUT/SUCCESS]") != NULL)
     else if (strstr(line, "[PUT/SUCCESS]") != NULL)

+ 1 - 0
flipper_http/flipper_http.h

@@ -82,6 +82,7 @@ extern FlipperHTTP fhttp;
 // Global static array for the line buffer
 // Global static array for the line buffer
 extern char rx_line_buffer[RX_LINE_BUFFER_SIZE];
 extern char rx_line_buffer[RX_LINE_BUFFER_SIZE];
 extern uint8_t file_buffer[FILE_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
 // fhttp.last_response holds the last received data from the UART
 
 

+ 15 - 2
parse/flip_weather_parse.c

@@ -15,7 +15,7 @@ bool flip_weather_parse_ip_address()
         FURI_LOG_E(TAG, "Failed to load received data from file.");
         FURI_LOG_E(TAG, "Failed to load received data from file.");
         return false;
         return false;
     }
     }
-    const char *data_cstr = furi_string_get_cstr(returned_data);
+    char *data_cstr = (char *)furi_string_get_cstr(returned_data);
     if (data_cstr == NULL)
     if (data_cstr == NULL)
     {
     {
         FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
         FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
@@ -29,13 +29,14 @@ bool flip_weather_parse_ip_address()
         sent_get_request = true;
         sent_get_request = true;
         get_request_success = false;
         get_request_success = false;
         fhttp.state = ISSUE;
         fhttp.state = ISSUE;
-        free(ip);
         furi_string_free(returned_data);
         furi_string_free(returned_data);
+        free(data_cstr);
         return false;
         return false;
     }
     }
     snprintf(ip_address, 16, "%s", ip);
     snprintf(ip_address, 16, "%s", ip);
     free(ip);
     free(ip);
     furi_string_free(returned_data);
     furi_string_free(returned_data);
+    free(data_cstr);
     return true;
     return true;
 }
 }
 
 
@@ -130,6 +131,11 @@ void process_geo_location()
         snprintf(ip_data, 64, "IP Address: %s", ip_address);
         snprintf(ip_data, 64, "IP Address: %s", ip_address);
 
 
         fhttp.state = IDLE;
         fhttp.state = IDLE;
+        free(city);
+        free(region);
+        free(country);
+        free(latitude);
+        free(longitude);
     }
     }
 }
 }
 
 
@@ -161,6 +167,13 @@ void process_weather()
         snprintf(time_data, 64, "Time: %s", time);
         snprintf(time_data, 64, "Time: %s", time);
 
 
         fhttp.state = IDLE;
         fhttp.state = IDLE;
+        free(current_data);
+        free(temperature);
+        free(precipitation);
+        free(rain);
+        free(showers);
+        free(snowfall);
+        free(time);
     }
     }
     else if (!weather_information_processed && fhttp.last_response == NULL)
     else if (!weather_information_processed && fhttp.last_response == NULL)
     {
     {