|
@@ -1,8 +1,5 @@
|
|
|
#include <flipper_http/flipper_http.h> // change this to where flipper_http.h is located
|
|
#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
|
|
// 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(
|
|
@@ -187,19 +184,19 @@ int32_t flipper_http_worker(void *context)
|
|
|
if (fhttp.save_bytes)
|
|
if (fhttp.save_bytes)
|
|
|
{
|
|
{
|
|
|
// Add byte to the buffer
|
|
// 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
|
|
// 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(
|
|
if (!flipper_http_append_to_file(
|
|
|
- file_buffer,
|
|
|
|
|
- file_buffer_len,
|
|
|
|
|
|
|
+ fhttp.file_buffer,
|
|
|
|
|
+ fhttp.file_buffer_len,
|
|
|
fhttp.just_started_bytes,
|
|
fhttp.just_started_bytes,
|
|
|
fhttp.file_path))
|
|
fhttp.file_path))
|
|
|
{
|
|
{
|
|
|
FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
|
|
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;
|
|
fhttp.just_started_bytes = false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -210,17 +207,17 @@ int32_t flipper_http_worker(void *context)
|
|
|
// Handle line buffering
|
|
// Handle line buffering
|
|
|
if (c == '\n' || rx_line_pos >= RX_LINE_BUFFER_SIZE - 1)
|
|
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
|
|
// 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
|
|
// Reset the line buffer position
|
|
|
rx_line_pos = 0;
|
|
rx_line_pos = 0;
|
|
|
}
|
|
}
|
|
|
else
|
|
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 char marker[] = "[GET/END]";
|
|
|
const size_t marker_len = sizeof(marker) - 1; // Exclude null terminator
|
|
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
|
|
// 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
|
|
// 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;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// If there is data left in the buffer, append it to the file
|
|
// 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.");
|
|
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 char marker[] = "[POST/END]";
|
|
|
const size_t marker_len = sizeof(marker) - 1; // Exclude null terminator
|
|
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
|
|
// 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
|
|
// 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;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// If there is data left in the buffer, append it to the file
|
|
// 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.");
|
|
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
|
|
// 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;
|
|
|
fhttp.just_started_bytes = true;
|
|
fhttp.just_started_bytes = true;
|
|
|
- file_buffer_len = 0;
|
|
|
|
|
|
|
+ fhttp.file_buffer_len = 0;
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
else if (strstr(line, "[POST/SUCCESS]") != NULL)
|
|
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
|
|
// 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;
|
|
|
fhttp.just_started_bytes = true;
|
|
fhttp.just_started_bytes = true;
|
|
|
- file_buffer_len = 0;
|
|
|
|
|
|
|
+ fhttp.file_buffer_len = 0;
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
else if (strstr(line, "[PUT/SUCCESS]") != NULL)
|
|
else if (strstr(line, "[PUT/SUCCESS]") != NULL)
|