|
@@ -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
|
|
@@ -473,14 +436,14 @@ bool flipper_http_send_data(const char *data)
|
|
|
|
|
|
|
|
// Create a buffer with data + '\n'
|
|
// Create a buffer with data + '\n'
|
|
|
size_t send_length = data_length + 1; // +1 for '\n'
|
|
size_t send_length = data_length + 1; // +1 for '\n'
|
|
|
- if (send_length > 512)
|
|
|
|
|
|
|
+ if (send_length > 256)
|
|
|
{ // Ensure buffer size is sufficient
|
|
{ // Ensure buffer size is sufficient
|
|
|
FURI_LOG_E("FlipperHTTP", "Data too long to send over FHTTP.");
|
|
FURI_LOG_E("FlipperHTTP", "Data too long to send over FHTTP.");
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- char send_buffer[513]; // 512 + 1 for safety
|
|
|
|
|
- strncpy(send_buffer, data, 512);
|
|
|
|
|
|
|
+ char send_buffer[257]; // 256 + 1 for safety
|
|
|
|
|
+ strncpy(send_buffer, data, 256);
|
|
|
send_buffer[data_length] = '\n'; // Append newline
|
|
send_buffer[data_length] = '\n'; // Append newline
|
|
|
send_buffer[data_length + 1] = '\0'; // Null-terminate
|
|
send_buffer[data_length + 1] = '\0'; // Null-terminate
|
|
|
|
|
|
|
@@ -497,7 +460,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;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -664,12 +627,19 @@ bool flipper_http_parse_json_array(const char *key, int index, const char *json_
|
|
|
*/
|
|
*/
|
|
|
bool flipper_http_scan_wifi()
|
|
bool flipper_http_scan_wifi()
|
|
|
{
|
|
{
|
|
|
- const char *command = "[WIFI/SCAN]";
|
|
|
|
|
- if (!flipper_http_send_data(command))
|
|
|
|
|
|
|
+ if (!flipper_http_send_data("[WIFI/SCAN]"))
|
|
|
{
|
|
{
|
|
|
FURI_LOG_E("FlipperHTTP", "Failed to send WiFi scan command.");
|
|
FURI_LOG_E("FlipperHTTP", "Failed to send WiFi scan command.");
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
+ // custom for FlipWiFi app
|
|
|
|
|
+ fhttp.just_started_get = true;
|
|
|
|
|
+ snprintf(
|
|
|
|
|
+ fhttp.file_path,
|
|
|
|
|
+ sizeof(fhttp.file_path),
|
|
|
|
|
+ STORAGE_EXT_PATH_PREFIX "/apps_data/flip_wifi/scan.txt");
|
|
|
|
|
+
|
|
|
|
|
+ fhttp.save_received_data = true;
|
|
|
|
|
|
|
|
// The response will be handled asynchronously via the callback
|
|
// The response will be handled asynchronously via the callback
|
|
|
return true;
|
|
return true;
|
|
@@ -715,8 +685,7 @@ bool flipper_http_save_wifi(const char *ssid, const char *password)
|
|
|
*/
|
|
*/
|
|
|
bool flipper_http_ip_address()
|
|
bool flipper_http_ip_address()
|
|
|
{
|
|
{
|
|
|
- const char *command = "[IP/ADDRESS]";
|
|
|
|
|
- if (!flipper_http_send_data(command))
|
|
|
|
|
|
|
+ if (!flipper_http_send_data("[IP/ADDRESS]"))
|
|
|
{
|
|
{
|
|
|
FURI_LOG_E("FlipperHTTP", "Failed to send IP address command.");
|
|
FURI_LOG_E("FlipperHTTP", "Failed to send IP address command.");
|
|
|
return false;
|
|
return false;
|
|
@@ -753,8 +722,7 @@ bool flipper_http_ip_wifi()
|
|
|
*/
|
|
*/
|
|
|
bool flipper_http_disconnect_wifi()
|
|
bool flipper_http_disconnect_wifi()
|
|
|
{
|
|
{
|
|
|
- const char *command = "[WIFI/DISCONNECT]";
|
|
|
|
|
- if (!flipper_http_send_data(command))
|
|
|
|
|
|
|
+ if (!flipper_http_send_data("[WIFI/DISCONNECT]"))
|
|
|
{
|
|
{
|
|
|
FURI_LOG_E("FlipperHTTP", "Failed to send WiFi disconnect command.");
|
|
FURI_LOG_E("FlipperHTTP", "Failed to send WiFi disconnect command.");
|
|
|
return false;
|
|
return false;
|
|
@@ -772,8 +740,7 @@ bool flipper_http_disconnect_wifi()
|
|
|
*/
|
|
*/
|
|
|
bool flipper_http_connect_wifi()
|
|
bool flipper_http_connect_wifi()
|
|
|
{
|
|
{
|
|
|
- const char *command = "[WIFI/CONNECT]";
|
|
|
|
|
- if (!flipper_http_send_data(command))
|
|
|
|
|
|
|
+ if (!flipper_http_send_data("[WIFI/CONNECT]"))
|
|
|
{
|
|
{
|
|
|
FURI_LOG_E("FlipperHTTP", "Failed to send WiFi connect command.");
|
|
FURI_LOG_E("FlipperHTTP", "Failed to send WiFi connect command.");
|
|
|
return false;
|
|
return false;
|
|
@@ -835,7 +802,7 @@ bool flipper_http_get_request_with_headers(const char *url, const char *headers)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Prepare GET request command with headers
|
|
// Prepare GET request command with headers
|
|
|
- char command[512];
|
|
|
|
|
|
|
+ char command[256];
|
|
|
int ret = snprintf(
|
|
int ret = snprintf(
|
|
|
command, sizeof(command), "[GET/HTTP]{\"url\":\"%s\",\"headers\":%s}", url, headers);
|
|
command, sizeof(command), "[GET/HTTP]{\"url\":\"%s\",\"headers\":%s}", url, headers);
|
|
|
if (ret < 0 || ret >= (int)sizeof(command))
|
|
if (ret < 0 || ret >= (int)sizeof(command))
|
|
@@ -1113,6 +1080,18 @@ void flipper_http_rx_callback(const char *line, void *context)
|
|
|
// Uncomment below line to log the data received over UART
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
+ // custom function to FlipWiFi
|
|
|
|
|
+ if (fhttp.save_received_data)
|
|
|
|
|
+ {
|
|
|
|
|
+ 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;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Check if we've started receiving data from a GET request
|
|
// Check if we've started receiving data from a GET request
|
|
|
if (fhttp.started_receiving_get)
|
|
if (fhttp.started_receiving_get)
|
|
|
{
|
|
{
|
|
@@ -1128,8 +1107,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 +1177,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 +1332,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 +1343,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)
|