|
|
@@ -1,133 +1,33 @@
|
|
|
-// flipper_http.h - Flipper HTTP Library (www.github.com/jblanked)
|
|
|
-// Author: JBlanked
|
|
|
-#ifndef FLIPPER_HTTP_H
|
|
|
-#define FLIPPER_HTTP_H
|
|
|
-
|
|
|
-#include <furi.h>
|
|
|
-#include <furi_hal.h>
|
|
|
-#include <furi_hal_gpio.h>
|
|
|
-#include <furi_hal_serial.h>
|
|
|
-#include <storage/storage.h>
|
|
|
-#include <stdlib.h>
|
|
|
-
|
|
|
-// STORAGE_EXT_PATH_PREFIX is defined in the Furi SDK as /ext
|
|
|
-
|
|
|
-#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 (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)
|
|
|
-
|
|
|
-// Forward declaration for callback
|
|
|
-typedef void (*FlipperHTTP_Callback)(const char* line, void* context);
|
|
|
-
|
|
|
-// Functions
|
|
|
-bool flipper_http_init(FlipperHTTP_Callback callback, void* context);
|
|
|
-void flipper_http_deinit();
|
|
|
-//---
|
|
|
-void flipper_http_rx_callback(const char* line, void* context);
|
|
|
-bool flipper_http_send_data(const char* data);
|
|
|
-//---
|
|
|
-bool flipper_http_connect_wifi();
|
|
|
-bool flipper_http_disconnect_wifi();
|
|
|
-bool flipper_http_ping();
|
|
|
-bool flipper_http_scan_wifi();
|
|
|
-bool flipper_http_save_wifi(const char* ssid, const char* password);
|
|
|
-//---
|
|
|
-bool flipper_http_get_request(const char* url);
|
|
|
-bool flipper_http_get_request_with_headers(const char* url, const char* headers);
|
|
|
-bool flipper_http_post_request_with_headers(
|
|
|
- const char* url,
|
|
|
- const char* headers,
|
|
|
- const char* payload);
|
|
|
-bool flipper_http_put_request_with_headers(
|
|
|
- const char* url,
|
|
|
- const char* headers,
|
|
|
- const char* payload);
|
|
|
-bool flipper_http_delete_request_with_headers(
|
|
|
- const char* url,
|
|
|
- const char* headers,
|
|
|
- const char* payload);
|
|
|
-//---
|
|
|
-bool flipper_http_get_request_bytes(const char* url, const char* headers);
|
|
|
-bool flipper_http_post_request_bytes(const char* url, const char* headers, const char* payload);
|
|
|
-//---
|
|
|
-bool flipper_http_save_received_data(size_t bytes_received, const char line_buffer[]);
|
|
|
-char* trim(const char* str);
|
|
|
-
|
|
|
-// State variable to track the UART state
|
|
|
-typedef enum {
|
|
|
- INACTIVE, // Inactive state
|
|
|
- IDLE, // Default state
|
|
|
- RECEIVING, // Receiving data
|
|
|
- SENDING, // Sending data
|
|
|
- ISSUE, // Issue with connection
|
|
|
-} SerialState;
|
|
|
-
|
|
|
-// Event Flags for UART Worker Thread
|
|
|
-typedef enum {
|
|
|
- WorkerEvtStop = (1 << 0),
|
|
|
- WorkerEvtRxDone = (1 << 1),
|
|
|
-} WorkerEvtFlags;
|
|
|
-
|
|
|
-static bool is_compile_app_request = false; // personal use in flip_store_apps.h
|
|
|
-
|
|
|
-// FlipperHTTP Structure
|
|
|
-typedef struct {
|
|
|
- FuriStreamBuffer* flipper_http_stream; // Stream buffer for UART communication
|
|
|
- FuriHalSerialHandle* serial_handle; // Serial handle for UART communication
|
|
|
- FuriThread* rx_thread; // Worker thread for UART
|
|
|
- // uint8_t rx_buf[RX_BUF_SIZE]; // Buffer for received data
|
|
|
- FuriThreadId rx_thread_id; // Worker thread ID
|
|
|
- FlipperHTTP_Callback handle_rx_line_cb; // Callback for received lines
|
|
|
- void* callback_context; // Context for the callback
|
|
|
- SerialState state; // State of the UART
|
|
|
-
|
|
|
- // variable to store the last received data from the UART
|
|
|
- char* last_response;
|
|
|
-
|
|
|
- // Timer-related members
|
|
|
- FuriTimer* get_timeout_timer; // Timer for HTTP request timeout
|
|
|
- char* received_data; // Buffer to store received data
|
|
|
-
|
|
|
- bool started_receiving_get; // Indicates if a GET request has started
|
|
|
- bool just_started_get; // Indicates if GET data reception has just started
|
|
|
-
|
|
|
- bool started_receiving_post; // Indicates if a POST request has started
|
|
|
- bool just_started_post; // Indicates if POST data reception has just started
|
|
|
-
|
|
|
- bool started_receiving_put; // Indicates if a PUT request has started
|
|
|
- bool just_started_put; // Indicates if PUT data reception has just started
|
|
|
-
|
|
|
- bool started_receiving_delete; // Indicates if a DELETE request has started
|
|
|
- bool just_started_delete; // Indicates if DELETE data reception has just started
|
|
|
-
|
|
|
- // Buffer to hold the raw bytes received from the UART
|
|
|
- uint8_t* received_bytes;
|
|
|
- size_t received_bytes_len; // Length of the received bytes
|
|
|
-
|
|
|
- // File path to save the bytes received
|
|
|
- char file_path[256];
|
|
|
-
|
|
|
- bool save_data; // Flag to save the received data
|
|
|
-
|
|
|
-} FlipperHTTP;
|
|
|
-
|
|
|
-static FlipperHTTP fhttp;
|
|
|
-
|
|
|
+#include <flipper_http/flipper_http.h>
|
|
|
+FlipperHTTP fhttp;
|
|
|
+char rx_line_buffer[RX_LINE_BUFFER_SIZE];
|
|
|
+uint8_t file_buffer[FILE_BUFFER_SIZE];
|
|
|
// Function to append received data to file
|
|
|
-static bool append_to_file(const char* file_path, const void* data, size_t data_size) {
|
|
|
+// make sure to initialize the file path before calling this function
|
|
|
+bool flipper_http_append_to_file(
|
|
|
+ const void* data,
|
|
|
+ size_t data_size,
|
|
|
+ bool start_new_file,
|
|
|
+ char* file_path) {
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
|
|
File* file = storage_file_alloc(storage);
|
|
|
|
|
|
- // Open the file in append mode
|
|
|
- if(!storage_file_open(file, file_path, FSAM_WRITE, FSOM_OPEN_APPEND)) {
|
|
|
- FURI_LOG_E(HTTP_TAG, "Failed to open file for appending: %s", file_path);
|
|
|
- storage_file_free(file);
|
|
|
- furi_record_close(RECORD_STORAGE);
|
|
|
- return false;
|
|
|
+ if(start_new_file) {
|
|
|
+ // Open the file in write mode
|
|
|
+ if(!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
|
|
|
+ storage_file_free(file);
|
|
|
+ furi_record_close(RECORD_STORAGE);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // Open the file in append mode
|
|
|
+ if(!storage_file_open(file, file_path, FSAM_WRITE, FSOM_OPEN_APPEND)) {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to open file for appending: %s", file_path);
|
|
|
+ storage_file_free(file);
|
|
|
+ furi_record_close(RECORD_STORAGE);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// Write the data to the file
|
|
|
@@ -142,13 +42,83 @@ static bool append_to_file(const char* file_path, const void* data, size_t data_
|
|
|
storage_file_close(file);
|
|
|
storage_file_free(file);
|
|
|
furi_record_close(RECORD_STORAGE);
|
|
|
-
|
|
|
return true;
|
|
|
}
|
|
|
-// Global static array for the line buffer
|
|
|
-static char rx_line_buffer[RX_LINE_BUFFER_SIZE];
|
|
|
-#define FILE_BUFFER_SIZE 512
|
|
|
-static uint8_t file_buffer[FILE_BUFFER_SIZE];
|
|
|
+
|
|
|
+FuriString* flipper_http_load_from_file(char* file_path) {
|
|
|
+ // Open the storage record
|
|
|
+ Storage* storage = furi_record_open(RECORD_STORAGE);
|
|
|
+ if(!storage) {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to open storage record");
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Allocate a file handle
|
|
|
+ File* file = storage_file_alloc(storage);
|
|
|
+ if(!file) {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to allocate storage file");
|
|
|
+ furi_record_close(RECORD_STORAGE);
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Open the file for reading
|
|
|
+ if(!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
|
|
+ storage_file_free(file);
|
|
|
+ furi_record_close(RECORD_STORAGE);
|
|
|
+ return NULL; // Return false if the file does not exist
|
|
|
+ }
|
|
|
+
|
|
|
+ // Allocate a FuriString to hold the received data
|
|
|
+ FuriString* str_result = furi_string_alloc();
|
|
|
+ if(!str_result) {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to allocate FuriString");
|
|
|
+ storage_file_close(file);
|
|
|
+ storage_file_free(file);
|
|
|
+ furi_record_close(RECORD_STORAGE);
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Reset the FuriString to ensure it's empty before reading
|
|
|
+ furi_string_reset(str_result);
|
|
|
+
|
|
|
+ // Define a buffer to hold the read data
|
|
|
+ uint8_t* buffer = (uint8_t*)malloc(MAX_FILE_SHOW);
|
|
|
+ if(!buffer) {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to allocate buffer");
|
|
|
+ furi_string_free(str_result);
|
|
|
+ storage_file_close(file);
|
|
|
+ storage_file_free(file);
|
|
|
+ furi_record_close(RECORD_STORAGE);
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Read data into the buffer
|
|
|
+ size_t read_count = storage_file_read(file, buffer, MAX_FILE_SHOW);
|
|
|
+ if(storage_file_get_error(file) != FSE_OK) {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Error reading from file.");
|
|
|
+ furi_string_free(str_result);
|
|
|
+ storage_file_close(file);
|
|
|
+ storage_file_free(file);
|
|
|
+ furi_record_close(RECORD_STORAGE);
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Append each byte to the FuriString
|
|
|
+ for(size_t i = 0; i < read_count; i++) {
|
|
|
+ furi_string_push_back(str_result, buffer[i]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check if there is more data beyond the maximum size
|
|
|
+ char extra_byte;
|
|
|
+ storage_file_read(file, &extra_byte, 1);
|
|
|
+
|
|
|
+ // Clean up
|
|
|
+ storage_file_close(file);
|
|
|
+ storage_file_free(file);
|
|
|
+ furi_record_close(RECORD_STORAGE);
|
|
|
+ free(buffer);
|
|
|
+ return str_result;
|
|
|
+}
|
|
|
|
|
|
// UART worker thread
|
|
|
/**
|
|
|
@@ -158,7 +128,7 @@ static uint8_t file_buffer[FILE_BUFFER_SIZE];
|
|
|
* @note This function will handle received data asynchronously via the callback.
|
|
|
*/
|
|
|
// UART worker thread
|
|
|
-static int32_t flipper_http_worker(void* context) {
|
|
|
+int32_t flipper_http_worker(void* context) {
|
|
|
UNUSED(context);
|
|
|
size_t rx_line_pos = 0;
|
|
|
static size_t file_buffer_len = 0;
|
|
|
@@ -180,12 +150,13 @@ static int32_t flipper_http_worker(void* context) {
|
|
|
}
|
|
|
|
|
|
// Append the received byte to the file if saving is enabled
|
|
|
- if(fhttp.save_data) {
|
|
|
+ if(fhttp.save_bytes) {
|
|
|
// Add byte to the buffer
|
|
|
file_buffer[file_buffer_len++] = c;
|
|
|
// Write to file if buffer is full
|
|
|
if(file_buffer_len >= FILE_BUFFER_SIZE) {
|
|
|
- if(!append_to_file(fhttp.file_path, file_buffer, file_buffer_len)) {
|
|
|
+ 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;
|
|
|
@@ -211,17 +182,17 @@ static int32_t flipper_http_worker(void* context) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if(fhttp.save_data) {
|
|
|
+ if(fhttp.save_bytes) {
|
|
|
// Write the remaining data to the file
|
|
|
if(file_buffer_len > 0) {
|
|
|
- if(!append_to_file(fhttp.file_path, file_buffer, file_buffer_len)) {
|
|
|
+ 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_data) {
|
|
|
+ if(fhttp.save_bytes) {
|
|
|
char* end = NULL;
|
|
|
if((end = strstr(fhttp.file_path, "[POST/END]")) != NULL) {
|
|
|
*end = '\0';
|
|
|
@@ -231,7 +202,7 @@ static int32_t flipper_http_worker(void* context) {
|
|
|
}
|
|
|
|
|
|
// remove newline from the from the end of the file
|
|
|
- if(fhttp.save_data) {
|
|
|
+ if(fhttp.save_bytes) {
|
|
|
char* end = NULL;
|
|
|
if((end = strstr(fhttp.file_path, "\n")) != NULL) {
|
|
|
*end = '\0';
|
|
|
@@ -243,7 +214,6 @@ static int32_t flipper_http_worker(void* context) {
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
-
|
|
|
// Timer callback function
|
|
|
/**
|
|
|
* @brief Callback function for the GET timeout timer.
|
|
|
@@ -261,12 +231,6 @@ void get_timeout_timer_callback(void* context) {
|
|
|
fhttp.started_receiving_put = false;
|
|
|
fhttp.started_receiving_delete = false;
|
|
|
|
|
|
- // Free received data if any
|
|
|
- if(fhttp.received_data) {
|
|
|
- free(fhttp.received_data);
|
|
|
- fhttp.received_data = NULL;
|
|
|
- }
|
|
|
-
|
|
|
// Update UART state
|
|
|
fhttp.state = ISSUE;
|
|
|
}
|
|
|
@@ -280,7 +244,7 @@ void get_timeout_timer_callback(void* context) {
|
|
|
* @param context The context to pass to the callback.
|
|
|
* @note This function will handle received data asynchronously via the callback.
|
|
|
*/
|
|
|
-static void _flipper_http_rx_callback(
|
|
|
+void _flipper_http_rx_callback(
|
|
|
FuriHalSerialHandle* handle,
|
|
|
FuriHalSerialRxEvent event,
|
|
|
void* context) {
|
|
|
@@ -384,10 +348,13 @@ bool flipper_http_init(FlipperHTTP_Callback callback, void* context) {
|
|
|
// Set the timer thread priority if needed
|
|
|
furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
|
|
|
|
|
|
- fhttp.file_path[0] = '\0'; // Null-terminate the file path
|
|
|
- fhttp.received_data = NULL;
|
|
|
- fhttp.received_bytes = NULL;
|
|
|
- fhttp.received_bytes_len = 0;
|
|
|
+ fhttp.last_response = (char*)malloc(RX_BUF_SIZE);
|
|
|
+ if(!fhttp.last_response) {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to allocate memory for last_response.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // FURI_LOG_I(HTTP_TAG, "UART initialized successfully.");
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -426,12 +393,6 @@ void flipper_http_deinit() {
|
|
|
fhttp.get_timeout_timer = NULL;
|
|
|
}
|
|
|
|
|
|
- // Free received data if any
|
|
|
- if(fhttp.received_data) {
|
|
|
- free(fhttp.received_data);
|
|
|
- fhttp.received_data = NULL;
|
|
|
- }
|
|
|
-
|
|
|
// Free the last response
|
|
|
if(fhttp.last_response) {
|
|
|
free(fhttp.last_response);
|
|
|
@@ -485,7 +446,7 @@ bool flipper_http_send_data(const char* data) {
|
|
|
|
|
|
// Function to send a PING request
|
|
|
/**
|
|
|
- * @brief Send a PING request to check the connection status.
|
|
|
+ * @brief Send a PING request to check if the Wifi Dev Board is connected.
|
|
|
* @return true if the request was successful, false otherwise.
|
|
|
* @note The received data will be handled asynchronously via the callback.
|
|
|
* @note This is best used to check if the Wifi Dev Board is connected.
|
|
|
@@ -503,6 +464,125 @@ bool flipper_http_ping() {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+// Function to list available commands
|
|
|
+/**
|
|
|
+ * @brief Send a command to list available commands.
|
|
|
+ * @return true if the request was successful, false otherwise.
|
|
|
+ * @note The received data will be handled asynchronously via the callback.
|
|
|
+ */
|
|
|
+bool flipper_http_list_commands() {
|
|
|
+ const char* command = "[LIST]";
|
|
|
+ if(!flipper_http_send_data(command)) {
|
|
|
+ FURI_LOG_E("FlipperHTTP", "Failed to send LIST command.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // The response will be handled asynchronously via the callback
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+// Function to turn on the LED
|
|
|
+/**
|
|
|
+ * @brief Allow the LED to display while processing.
|
|
|
+ * @return true if the request was successful, false otherwise.
|
|
|
+ * @note The received data will be handled asynchronously via the callback.
|
|
|
+ */
|
|
|
+bool flipper_http_led_on() {
|
|
|
+ const char* command = "[LED/ON]";
|
|
|
+ if(!flipper_http_send_data(command)) {
|
|
|
+ FURI_LOG_E("FlipperHTTP", "Failed to send LED ON command.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // The response will be handled asynchronously via the callback
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+// Function to turn off the LED
|
|
|
+/**
|
|
|
+ * @brief Disable the LED from displaying while processing.
|
|
|
+ * @return true if the request was successful, false otherwise.
|
|
|
+ * @note The received data will be handled asynchronously via the callback.
|
|
|
+ */
|
|
|
+bool flipper_http_led_off() {
|
|
|
+ const char* command = "[LED/OFF]";
|
|
|
+ if(!flipper_http_send_data(command)) {
|
|
|
+ FURI_LOG_E("FlipperHTTP", "Failed to send LED OFF command.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // The response will be handled asynchronously via the callback
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+// Function to parse JSON data
|
|
|
+/**
|
|
|
+ * @brief Parse JSON data.
|
|
|
+ * @return true if the JSON data was parsed successfully, false otherwise.
|
|
|
+ * @param key The key to parse from the JSON data.
|
|
|
+ * @param json_data The JSON data to parse.
|
|
|
+ * @note The received data will be handled asynchronously via the callback.
|
|
|
+ */
|
|
|
+bool flipper_http_parse_json(const char* key, const char* json_data) {
|
|
|
+ if(!key || !json_data) {
|
|
|
+ FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_parse_json.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ char buffer[256];
|
|
|
+ int ret =
|
|
|
+ snprintf(buffer, sizeof(buffer), "[PARSE]{\"key\":\"%s\",\"json\":%s}", key, json_data);
|
|
|
+ if(ret < 0 || ret >= (int)sizeof(buffer)) {
|
|
|
+ FURI_LOG_E("FlipperHTTP", "Failed to format JSON parse command.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!flipper_http_send_data(buffer)) {
|
|
|
+ FURI_LOG_E("FlipperHTTP", "Failed to send JSON parse command.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // The response will be handled asynchronously via the callback
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+// Function to parse JSON array data
|
|
|
+/**
|
|
|
+ * @brief Parse JSON array data.
|
|
|
+ * @return true if the JSON array data was parsed successfully, false otherwise.
|
|
|
+ * @param key The key to parse from the JSON array data.
|
|
|
+ * @param index The index to parse from the JSON array data.
|
|
|
+ * @param json_data The JSON array data to parse.
|
|
|
+ * @note The received data will be handled asynchronously via the callback.
|
|
|
+ */
|
|
|
+bool flipper_http_parse_json_array(const char* key, int index, const char* json_data) {
|
|
|
+ if(!key || !json_data) {
|
|
|
+ FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_parse_json_array.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ char buffer[256];
|
|
|
+ int ret = snprintf(
|
|
|
+ buffer,
|
|
|
+ sizeof(buffer),
|
|
|
+ "[PARSE/ARRAY]{\"key\":\"%s\",\"index\":%d,\"json\":%s}",
|
|
|
+ key,
|
|
|
+ index,
|
|
|
+ json_data);
|
|
|
+ if(ret < 0 || ret >= (int)sizeof(buffer)) {
|
|
|
+ FURI_LOG_E("FlipperHTTP", "Failed to format JSON parse array command.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!flipper_http_send_data(buffer)) {
|
|
|
+ FURI_LOG_E("FlipperHTTP", "Failed to send JSON parse array command.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // The response will be handled asynchronously via the callback
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
// Function to scan for WiFi networks
|
|
|
/**
|
|
|
* @brief Send a command to scan for WiFi networks.
|
|
|
@@ -548,6 +628,40 @@ bool flipper_http_save_wifi(const char* ssid, const char* password) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+// Function to get IP address of WiFi Devboard
|
|
|
+/**
|
|
|
+ * @brief Send a command to get the IP address of the WiFi Devboard
|
|
|
+ * @return true if the request was successful, false otherwise.
|
|
|
+ * @note The received data will be handled asynchronously via the callback.
|
|
|
+ */
|
|
|
+bool flipper_http_ip_address() {
|
|
|
+ const char* command = "[IP/ADDRESS]";
|
|
|
+ if(!flipper_http_send_data(command)) {
|
|
|
+ FURI_LOG_E("FlipperHTTP", "Failed to send IP address command.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // The response will be handled asynchronously via the callback
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+// Function to get IP address of the connected WiFi network
|
|
|
+/**
|
|
|
+ * @brief Send a command to get the IP address of the connected WiFi network.
|
|
|
+ * @return true if the request was successful, false otherwise.
|
|
|
+ * @note The received data will be handled asynchronously via the callback.
|
|
|
+ */
|
|
|
+bool flipper_http_ip_wifi() {
|
|
|
+ const char* command = "[WIFI/IP]";
|
|
|
+ if(!flipper_http_send_data(command)) {
|
|
|
+ FURI_LOG_E("FlipperHTTP", "Failed to send WiFi IP command.");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // The response will be handled asynchronously via the callback
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
// Function to disconnect from WiFi (returns true if successful)
|
|
|
/**
|
|
|
* @brief Send a command to disconnect from WiFi.
|
|
|
@@ -683,7 +797,7 @@ bool flipper_http_get_request_bytes(const char* url, const char* headers) {
|
|
|
* @return true if the request was successful, false otherwise.
|
|
|
* @param url The URL to send the POST request to.
|
|
|
* @param headers The headers to send with the POST request.
|
|
|
- * @param payload The data to send with the POST request.
|
|
|
+ * @param data The data to send with the POST request.
|
|
|
* @note The received data will be handled asynchronously via the callback.
|
|
|
*/
|
|
|
bool flipper_http_post_request_with_headers(
|
|
|
@@ -765,7 +879,7 @@ bool flipper_http_post_request_bytes(const char* url, const char* headers, const
|
|
|
* @return true if the request was successful, false otherwise.
|
|
|
* @param url The URL to send the PUT request to.
|
|
|
* @param headers The headers to send with the PUT request.
|
|
|
- * @param payload The data to send with the PUT request.
|
|
|
+ * @param data The data to send with the PUT request.
|
|
|
* @note The received data will be handled asynchronously via the callback.
|
|
|
*/
|
|
|
bool flipper_http_put_request_with_headers(
|
|
|
@@ -807,7 +921,7 @@ bool flipper_http_put_request_with_headers(
|
|
|
* @return true if the request was successful, false otherwise.
|
|
|
* @param url The URL to send the DELETE request to.
|
|
|
* @param headers The headers to send with the DELETE request.
|
|
|
- * @param payload The data to send with the DELETE request.
|
|
|
+ * @param data The data to send with the DELETE request.
|
|
|
* @note The received data will be handled asynchronously via the callback.
|
|
|
*/
|
|
|
bool flipper_http_delete_request_with_headers(
|
|
|
@@ -862,7 +976,13 @@ void flipper_http_rx_callback(const char* line, void* context) {
|
|
|
// Trim the received line to check if it's empty
|
|
|
char* trimmed_line = trim(line);
|
|
|
if(trimmed_line != NULL && trimmed_line[0] != '\0') {
|
|
|
- fhttp.last_response = (char*)line;
|
|
|
+ // if the line is not [GET/END] or [POST/END] or [PUT/END] or [DELETE/END]
|
|
|
+ if(strstr(trimmed_line, "[GET/END]") == NULL &&
|
|
|
+ strstr(trimmed_line, "[POST/END]") == NULL &&
|
|
|
+ strstr(trimmed_line, "[PUT/END]") == NULL &&
|
|
|
+ strstr(trimmed_line, "[DELETE/END]") == NULL) {
|
|
|
+ strncpy(fhttp.last_response, trimmed_line, RX_BUF_SIZE);
|
|
|
+ }
|
|
|
}
|
|
|
free(trimmed_line); // Free the allocated memory for trimmed_line
|
|
|
|
|
|
@@ -871,7 +991,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) {
|
|
|
@@ -882,44 +1002,24 @@ void flipper_http_rx_callback(const char* line, void* context) {
|
|
|
FURI_LOG_I(HTTP_TAG, "GET request completed.");
|
|
|
// Stop the timer since we've completed the GET request
|
|
|
furi_timer_stop(fhttp.get_timeout_timer);
|
|
|
-
|
|
|
- if(fhttp.received_data) {
|
|
|
- // uncomment if you want to save the received data to the external storage
|
|
|
- flipper_http_save_received_data(strlen(fhttp.received_data), fhttp.received_data);
|
|
|
- fhttp.started_receiving_get = false;
|
|
|
- fhttp.just_started_get = false;
|
|
|
- fhttp.state = IDLE;
|
|
|
- return;
|
|
|
- } else {
|
|
|
- FURI_LOG_E(HTTP_TAG, "No data received.");
|
|
|
- fhttp.started_receiving_get = false;
|
|
|
- fhttp.just_started_get = false;
|
|
|
- fhttp.state = IDLE;
|
|
|
- return;
|
|
|
- }
|
|
|
+ fhttp.started_receiving_get = false;
|
|
|
+ fhttp.just_started_get = false;
|
|
|
+ fhttp.state = IDLE;
|
|
|
+ fhttp.save_bytes = false;
|
|
|
+ fhttp.is_bytes_request = false;
|
|
|
+ fhttp.save_received_data = false;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
// Append the new line to the existing data
|
|
|
- if(fhttp.received_data == NULL) {
|
|
|
- fhttp.received_data =
|
|
|
- (char*)malloc(strlen(line) + 2); // +2 for newline and null terminator
|
|
|
- if(fhttp.received_data) {
|
|
|
- strcpy(fhttp.received_data, line);
|
|
|
- fhttp.received_data[strlen(line)] = '\n'; // Add newline
|
|
|
- fhttp.received_data[strlen(line) + 1] = '\0'; // Null terminator
|
|
|
- }
|
|
|
- } else {
|
|
|
- size_t current_len = strlen(fhttp.received_data);
|
|
|
- size_t new_size = current_len + strlen(line) + 2; // +2 for newline and null terminator
|
|
|
- fhttp.received_data = (char*)realloc(fhttp.received_data, new_size);
|
|
|
- if(fhttp.received_data) {
|
|
|
- memcpy(
|
|
|
- fhttp.received_data + current_len,
|
|
|
- line,
|
|
|
- strlen(line)); // Copy line at the end of the current data
|
|
|
- fhttp.received_data[current_len + strlen(line)] = '\n'; // Add newline
|
|
|
- fhttp.received_data[current_len + strlen(line) + 1] = '\0'; // Null terminator
|
|
|
- }
|
|
|
+ if(fhttp.save_received_data &&
|
|
|
+ !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.started_receiving_get = false;
|
|
|
+ fhttp.just_started_get = false;
|
|
|
+ fhttp.state = IDLE;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
if(!fhttp.just_started_get) {
|
|
|
@@ -935,47 +1035,26 @@ void flipper_http_rx_callback(const char* line, void* context) {
|
|
|
|
|
|
if(strstr(line, "[POST/END]") != NULL) {
|
|
|
FURI_LOG_I(HTTP_TAG, "POST request completed.");
|
|
|
- fhttp.save_data = false;
|
|
|
// Stop the timer since we've completed the POST request
|
|
|
furi_timer_stop(fhttp.get_timeout_timer);
|
|
|
-
|
|
|
- if(fhttp.received_data) {
|
|
|
- // uncomment if you want to save the received data to the external storage
|
|
|
- // flipper_http_save_received_data(strlen(fhttp.received_data), fhttp.received_data);
|
|
|
- fhttp.started_receiving_post = false;
|
|
|
- fhttp.just_started_post = false;
|
|
|
- fhttp.state = IDLE;
|
|
|
- return;
|
|
|
- } else {
|
|
|
- FURI_LOG_E(HTTP_TAG, "No data received.");
|
|
|
- fhttp.started_receiving_post = false;
|
|
|
- fhttp.just_started_post = false;
|
|
|
- fhttp.state = IDLE;
|
|
|
- return;
|
|
|
- }
|
|
|
+ fhttp.started_receiving_post = false;
|
|
|
+ fhttp.just_started_post = false;
|
|
|
+ fhttp.state = IDLE;
|
|
|
+ fhttp.save_bytes = false;
|
|
|
+ fhttp.is_bytes_request = false;
|
|
|
+ fhttp.save_received_data = false;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
// Append the new line to the existing data
|
|
|
- if(fhttp.received_data == NULL) {
|
|
|
- fhttp.received_data =
|
|
|
- (char*)malloc(strlen(line) + 2); // +2 for newline and null terminator
|
|
|
- if(fhttp.received_data) {
|
|
|
- strcpy(fhttp.received_data, line);
|
|
|
- fhttp.received_data[strlen(line)] = '\n'; // Add newline
|
|
|
- fhttp.received_data[strlen(line) + 1] = '\0'; // Null terminator
|
|
|
- }
|
|
|
- } else {
|
|
|
- size_t current_len = strlen(fhttp.received_data);
|
|
|
- size_t new_size = current_len + strlen(line) + 2; // +2 for newline and null terminator
|
|
|
- fhttp.received_data = (char*)realloc(fhttp.received_data, new_size);
|
|
|
- if(fhttp.received_data) {
|
|
|
- memcpy(
|
|
|
- fhttp.received_data + current_len,
|
|
|
- line,
|
|
|
- strlen(line)); // Copy line at the end of the current data
|
|
|
- fhttp.received_data[current_len + strlen(line)] = '\n'; // Add newline
|
|
|
- fhttp.received_data[current_len + strlen(line) + 1] = '\0'; // Null terminator
|
|
|
- }
|
|
|
+ if(fhttp.save_received_data &&
|
|
|
+ !flipper_http_append_to_file(
|
|
|
+ line, strlen(line), !fhttp.just_started_post, fhttp.file_path)) {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
|
|
|
+ fhttp.started_receiving_post = false;
|
|
|
+ fhttp.just_started_post = false;
|
|
|
+ fhttp.state = IDLE;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
if(!fhttp.just_started_post) {
|
|
|
@@ -993,44 +1072,24 @@ void flipper_http_rx_callback(const char* line, void* context) {
|
|
|
FURI_LOG_I(HTTP_TAG, "PUT request completed.");
|
|
|
// Stop the timer since we've completed the PUT request
|
|
|
furi_timer_stop(fhttp.get_timeout_timer);
|
|
|
-
|
|
|
- if(fhttp.received_data) {
|
|
|
- // uncomment if you want to save the received data to the external storage
|
|
|
- // flipper_http_save_received_data(strlen(fhttp.received_data), fhttp.received_data);
|
|
|
- fhttp.started_receiving_put = false;
|
|
|
- fhttp.just_started_put = false;
|
|
|
- fhttp.state = IDLE;
|
|
|
- return;
|
|
|
- } else {
|
|
|
- FURI_LOG_E(HTTP_TAG, "No data received.");
|
|
|
- fhttp.started_receiving_put = false;
|
|
|
- fhttp.just_started_put = false;
|
|
|
- fhttp.state = IDLE;
|
|
|
- return;
|
|
|
- }
|
|
|
+ fhttp.started_receiving_put = false;
|
|
|
+ fhttp.just_started_put = false;
|
|
|
+ fhttp.state = IDLE;
|
|
|
+ fhttp.save_bytes = false;
|
|
|
+ fhttp.is_bytes_request = false;
|
|
|
+ fhttp.save_received_data = false;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
// Append the new line to the existing data
|
|
|
- if(fhttp.received_data == NULL) {
|
|
|
- fhttp.received_data =
|
|
|
- (char*)malloc(strlen(line) + 2); // +2 for newline and null terminator
|
|
|
- if(fhttp.received_data) {
|
|
|
- strcpy(fhttp.received_data, line);
|
|
|
- fhttp.received_data[strlen(line)] = '\n'; // Add newline
|
|
|
- fhttp.received_data[strlen(line) + 1] = '\0'; // Null terminator
|
|
|
- }
|
|
|
- } else {
|
|
|
- size_t current_len = strlen(fhttp.received_data);
|
|
|
- size_t new_size = current_len + strlen(line) + 2; // +2 for newline and null terminator
|
|
|
- fhttp.received_data = (char*)realloc(fhttp.received_data, new_size);
|
|
|
- if(fhttp.received_data) {
|
|
|
- memcpy(
|
|
|
- fhttp.received_data + current_len,
|
|
|
- line,
|
|
|
- strlen(line)); // Copy line at the end of the current data
|
|
|
- fhttp.received_data[current_len + strlen(line)] = '\n'; // Add newline
|
|
|
- fhttp.received_data[current_len + strlen(line) + 1] = '\0'; // Null terminator
|
|
|
- }
|
|
|
+ if(fhttp.save_received_data &&
|
|
|
+ !flipper_http_append_to_file(
|
|
|
+ line, strlen(line), !fhttp.just_started_put, fhttp.file_path)) {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
|
|
|
+ fhttp.started_receiving_put = false;
|
|
|
+ fhttp.just_started_put = false;
|
|
|
+ fhttp.state = IDLE;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
if(!fhttp.just_started_put) {
|
|
|
@@ -1048,44 +1107,24 @@ void flipper_http_rx_callback(const char* line, void* context) {
|
|
|
FURI_LOG_I(HTTP_TAG, "DELETE request completed.");
|
|
|
// Stop the timer since we've completed the DELETE request
|
|
|
furi_timer_stop(fhttp.get_timeout_timer);
|
|
|
-
|
|
|
- if(fhttp.received_data) {
|
|
|
- // uncomment if you want to save the received data to the external storage
|
|
|
- // flipper_http_save_received_data(strlen(fhttp.received_data), fhttp.received_data);
|
|
|
- fhttp.started_receiving_delete = false;
|
|
|
- fhttp.just_started_delete = false;
|
|
|
- fhttp.state = IDLE;
|
|
|
- return;
|
|
|
- } else {
|
|
|
- FURI_LOG_E(HTTP_TAG, "No data received.");
|
|
|
- fhttp.started_receiving_delete = false;
|
|
|
- fhttp.just_started_delete = false;
|
|
|
- fhttp.state = IDLE;
|
|
|
- return;
|
|
|
- }
|
|
|
+ fhttp.started_receiving_delete = false;
|
|
|
+ fhttp.just_started_delete = false;
|
|
|
+ fhttp.state = IDLE;
|
|
|
+ fhttp.save_bytes = false;
|
|
|
+ fhttp.is_bytes_request = false;
|
|
|
+ fhttp.save_received_data = false;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
// Append the new line to the existing data
|
|
|
- if(fhttp.received_data == NULL) {
|
|
|
- fhttp.received_data =
|
|
|
- (char*)malloc(strlen(line) + 2); // +2 for newline and null terminator
|
|
|
- if(fhttp.received_data) {
|
|
|
- strcpy(fhttp.received_data, line);
|
|
|
- fhttp.received_data[strlen(line)] = '\n'; // Add newline
|
|
|
- fhttp.received_data[strlen(line) + 1] = '\0'; // Null terminator
|
|
|
- }
|
|
|
- } else {
|
|
|
- size_t current_len = strlen(fhttp.received_data);
|
|
|
- size_t new_size = current_len + strlen(line) + 2; // +2 for newline and null terminator
|
|
|
- fhttp.received_data = (char*)realloc(fhttp.received_data, new_size);
|
|
|
- if(fhttp.received_data) {
|
|
|
- memcpy(
|
|
|
- fhttp.received_data + current_len,
|
|
|
- line,
|
|
|
- strlen(line)); // Copy line at the end of the current data
|
|
|
- fhttp.received_data[current_len + strlen(line)] = '\n'; // Add newline
|
|
|
- fhttp.received_data[current_len + strlen(line) + 1] = '\0'; // Null terminator
|
|
|
- }
|
|
|
+ if(fhttp.save_received_data &&
|
|
|
+ !flipper_http_append_to_file(
|
|
|
+ line, strlen(line), !fhttp.just_started_delete, fhttp.file_path)) {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
|
|
|
+ fhttp.started_receiving_delete = false;
|
|
|
+ fhttp.just_started_delete = false;
|
|
|
+ fhttp.state = IDLE;
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
if(!fhttp.just_started_delete) {
|
|
|
@@ -1108,34 +1147,28 @@ void flipper_http_rx_callback(const char* line, void* context) {
|
|
|
fhttp.started_receiving_get = true;
|
|
|
furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
|
|
|
fhttp.state = RECEIVING;
|
|
|
- fhttp.received_data = NULL;
|
|
|
- if(is_compile_app_request) {
|
|
|
- fhttp.save_data = true;
|
|
|
- }
|
|
|
+ // for GET request, save data only if it's a bytes request
|
|
|
+ fhttp.save_bytes = fhttp.is_bytes_request;
|
|
|
return;
|
|
|
} else if(strstr(line, "[POST/SUCCESS]") != NULL) {
|
|
|
FURI_LOG_I(HTTP_TAG, "POST request succeeded.");
|
|
|
fhttp.started_receiving_post = true;
|
|
|
furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
|
|
|
fhttp.state = RECEIVING;
|
|
|
- fhttp.received_data = NULL;
|
|
|
- if(is_compile_app_request) {
|
|
|
- fhttp.save_data = true;
|
|
|
- }
|
|
|
+ // for POST request, save data only if it's a bytes request
|
|
|
+ fhttp.save_bytes = fhttp.is_bytes_request;
|
|
|
return;
|
|
|
} else if(strstr(line, "[PUT/SUCCESS]") != NULL) {
|
|
|
FURI_LOG_I(HTTP_TAG, "PUT request succeeded.");
|
|
|
fhttp.started_receiving_put = true;
|
|
|
furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
|
|
|
fhttp.state = RECEIVING;
|
|
|
- fhttp.received_data = NULL;
|
|
|
return;
|
|
|
} else if(strstr(line, "[DELETE/SUCCESS]") != NULL) {
|
|
|
FURI_LOG_I(HTTP_TAG, "DELETE request succeeded.");
|
|
|
fhttp.started_receiving_delete = true;
|
|
|
furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
|
|
|
fhttp.state = RECEIVING;
|
|
|
- fhttp.received_data = NULL;
|
|
|
return;
|
|
|
} else if(strstr(line, "[DISCONNECTED]") != NULL) {
|
|
|
FURI_LOG_I(HTTP_TAG, "WiFi disconnected successfully.");
|
|
|
@@ -1161,60 +1194,7 @@ void flipper_http_rx_callback(const char* line, void* context) {
|
|
|
fhttp.state = IDLE;
|
|
|
}
|
|
|
}
|
|
|
-// Function to save received data to a file
|
|
|
-/**
|
|
|
- * @brief Save the received data to a file.
|
|
|
- * @return true if the data was saved successfully, false otherwise.
|
|
|
- * @param bytes_received The number of bytes received.
|
|
|
- * @param line_buffer The buffer containing the received data.
|
|
|
- * @note The data will be saved to a file in the STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag "/received_data.txt" directory.
|
|
|
- */
|
|
|
-bool flipper_http_save_received_data(size_t bytes_received, const char line_buffer[]) {
|
|
|
- const char* output_file_path = STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag
|
|
|
- "/received_data.txt";
|
|
|
-
|
|
|
- // Ensure the directory exists
|
|
|
- char directory_path[128];
|
|
|
- snprintf(
|
|
|
- directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag);
|
|
|
-
|
|
|
- Storage* _storage = NULL;
|
|
|
- File* _file = NULL;
|
|
|
- // Open the storage if not opened already
|
|
|
- // Initialize storage and create the directory if it doesn't exist
|
|
|
- _storage = furi_record_open(RECORD_STORAGE);
|
|
|
- storage_common_mkdir(_storage, directory_path); // Create directory if it doesn't exist
|
|
|
- _file = storage_file_alloc(_storage);
|
|
|
-
|
|
|
- // Open file for writing and append data line by line
|
|
|
- if(!storage_file_open(_file, output_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
|
|
|
- FURI_LOG_E(HTTP_TAG, "Failed to open output file for writing.");
|
|
|
- storage_file_free(_file);
|
|
|
- furi_record_close(RECORD_STORAGE);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // Write each line received from the UART to the file
|
|
|
- if(bytes_received > 0 && _file) {
|
|
|
- storage_file_write(_file, line_buffer, bytes_received);
|
|
|
- storage_file_write(_file, "\n", 1); // Add a newline after each line
|
|
|
- } else {
|
|
|
- FURI_LOG_E(HTTP_TAG, "No data received.");
|
|
|
- return false;
|
|
|
- }
|
|
|
|
|
|
- if(_file) {
|
|
|
- storage_file_close(_file);
|
|
|
- storage_file_free(_file);
|
|
|
- _file = NULL;
|
|
|
- }
|
|
|
- if(_storage) {
|
|
|
- furi_record_close(RECORD_STORAGE);
|
|
|
- _storage = NULL;
|
|
|
- }
|
|
|
-
|
|
|
- return true;
|
|
|
-}
|
|
|
// Function to trim leading and trailing spaces and newlines from a constant string
|
|
|
char* trim(const char* str) {
|
|
|
const char* end;
|
|
|
@@ -1249,4 +1229,30 @@ char* trim(const char* str) {
|
|
|
return trimmed_str;
|
|
|
}
|
|
|
|
|
|
-#endif // FLIPPER_HTTP_H
|
|
|
+/**
|
|
|
+ * @brief Process requests and parse JSON data asynchronously
|
|
|
+ * @param http_request The function to send the request
|
|
|
+ * @param parse_json The function to parse the JSON
|
|
|
+ * @return true if successful, false otherwise
|
|
|
+ */
|
|
|
+bool flipper_http_process_response_async(bool (*http_request)(void), bool (*parse_json)(void)) {
|
|
|
+ if(http_request()) // start the async request
|
|
|
+ {
|
|
|
+ furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
|
|
|
+ fhttp.state = RECEIVING;
|
|
|
+ } else {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to send request");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ while(fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0) {
|
|
|
+ // Wait for the request to be received
|
|
|
+ furi_delay_ms(100);
|
|
|
+ }
|
|
|
+ furi_timer_stop(fhttp.get_timeout_timer);
|
|
|
+ if(!parse_json()) // parse the JSON before switching to the view (synchonous)
|
|
|
+ {
|
|
|
+ FURI_LOG_E(HTTP_TAG, "Failed to parse the JSON...");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|