| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350 |
- // 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;
- // Function to append received data to file
- static bool append_to_file(const char *file_path, const void *data, size_t data_size)
- {
- 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;
- }
- // Write the data to the file
- if (storage_file_write(file, data, data_size) != data_size)
- {
- FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
- storage_file_close(file);
- storage_file_free(file);
- furi_record_close(RECORD_STORAGE);
- return false;
- }
- 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];
- // UART worker thread
- /**
- * @brief Worker thread to handle UART data asynchronously.
- * @return 0
- * @param context The context to pass to the callback.
- * @note This function will handle received data asynchronously via the callback.
- */
- // UART worker thread
- static int32_t flipper_http_worker(void *context)
- {
- UNUSED(context);
- size_t rx_line_pos = 0;
- static size_t file_buffer_len = 0;
- while (1)
- {
- uint32_t events = furi_thread_flags_wait(WorkerEvtStop | WorkerEvtRxDone, FuriFlagWaitAny, FuriWaitForever);
- if (events & WorkerEvtStop)
- break;
- if (events & WorkerEvtRxDone)
- {
- // Continuously read from the stream buffer until it's empty
- while (!furi_stream_buffer_is_empty(fhttp.flipper_http_stream))
- {
- // Read one byte at a time
- char c = 0;
- size_t received = furi_stream_buffer_receive(fhttp.flipper_http_stream, &c, 1, 0);
- if (received == 0)
- {
- // No more data to read
- break;
- }
- // Append the received byte to the file if saving is enabled
- if (fhttp.save_data)
- {
- // 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))
- {
- FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
- }
- file_buffer_len = 0;
- }
- }
- // Handle line buffering only if callback is set (text data)
- if (fhttp.handle_rx_line_cb)
- {
- // Handle line buffering
- if (c == '\n' || rx_line_pos >= RX_LINE_BUFFER_SIZE - 1)
- {
- rx_line_buffer[rx_line_pos] = '\0'; // Null-terminate the line
- // Invoke the callback with the complete line
- fhttp.handle_rx_line_cb(rx_line_buffer, fhttp.callback_context);
- // Reset the line buffer position
- rx_line_pos = 0;
- }
- else
- {
- rx_line_buffer[rx_line_pos++] = c; // Add character to the line buffer
- }
- }
- }
- }
- }
- if (fhttp.save_data)
- {
- // Write the remaining data to the file
- if (file_buffer_len > 0)
- {
- if (!append_to_file(fhttp.file_path, file_buffer, file_buffer_len))
- {
- 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)
- {
- 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_data)
- {
- char *end = NULL;
- if ((end = strstr(fhttp.file_path, "\n")) != NULL)
- {
- *end = '\0';
- }
- }
- // Reset the file buffer length
- file_buffer_len = 0;
- return 0;
- }
- // Timer callback function
- /**
- * @brief Callback function for the GET timeout timer.
- * @return 0
- * @param context The context to pass to the callback.
- * @note This function will be called when the GET request times out.
- */
- void get_timeout_timer_callback(void *context)
- {
- UNUSED(context);
- FURI_LOG_E(HTTP_TAG, "Timeout reached: 2 seconds without receiving the end.");
- // Reset the state
- fhttp.started_receiving_get = false;
- fhttp.started_receiving_post = false;
- 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;
- }
- // UART RX Handler Callback (Interrupt Context)
- /**
- * @brief A private callback function to handle received data asynchronously.
- * @return void
- * @param handle The UART handle.
- * @param event The event type.
- * @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(FuriHalSerialHandle *handle, FuriHalSerialRxEvent event, void *context)
- {
- UNUSED(context);
- if (event == FuriHalSerialRxEventData)
- {
- uint8_t data = furi_hal_serial_async_rx(handle);
- furi_stream_buffer_send(fhttp.flipper_http_stream, &data, 1, 0);
- furi_thread_flags_set(fhttp.rx_thread_id, WorkerEvtRxDone);
- }
- }
- // UART initialization function
- /**
- * @brief Initialize UART.
- * @return true if the UART was initialized successfully, false otherwise.
- * @param callback The callback function to handle received data (ex. flipper_http_rx_callback).
- * @param context The context to pass to the callback.
- * @note The received data will be handled asynchronously via the callback.
- */
- bool flipper_http_init(FlipperHTTP_Callback callback, void *context)
- {
- if (!context)
- {
- FURI_LOG_E(HTTP_TAG, "Invalid context provided to flipper_http_init.");
- return false;
- }
- if (!callback)
- {
- FURI_LOG_E(HTTP_TAG, "Invalid callback provided to flipper_http_init.");
- return false;
- }
- fhttp.flipper_http_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
- if (!fhttp.flipper_http_stream)
- {
- FURI_LOG_E(HTTP_TAG, "Failed to allocate UART stream buffer.");
- return false;
- }
- fhttp.rx_thread = furi_thread_alloc();
- if (!fhttp.rx_thread)
- {
- FURI_LOG_E(HTTP_TAG, "Failed to allocate UART thread.");
- furi_stream_buffer_free(fhttp.flipper_http_stream);
- return false;
- }
- furi_thread_set_name(fhttp.rx_thread, "FlipperHTTP_RxThread");
- furi_thread_set_stack_size(fhttp.rx_thread, 1024);
- furi_thread_set_context(fhttp.rx_thread, &fhttp);
- furi_thread_set_callback(fhttp.rx_thread, flipper_http_worker);
- fhttp.handle_rx_line_cb = callback;
- fhttp.callback_context = context;
- furi_thread_start(fhttp.rx_thread);
- fhttp.rx_thread_id = furi_thread_get_id(fhttp.rx_thread);
- // handle when the UART control is busy to avoid furi_check failed
- if (furi_hal_serial_control_is_busy(UART_CH))
- {
- FURI_LOG_E(HTTP_TAG, "UART control is busy.");
- return false;
- }
- fhttp.serial_handle = furi_hal_serial_control_acquire(UART_CH);
- if (!fhttp.serial_handle)
- {
- FURI_LOG_E(HTTP_TAG, "Failed to acquire UART control - handle is NULL");
- // Cleanup resources
- furi_thread_free(fhttp.rx_thread);
- furi_stream_buffer_free(fhttp.flipper_http_stream);
- return false;
- }
- // Initialize UART with acquired handle
- furi_hal_serial_init(fhttp.serial_handle, BAUDRATE);
- // Enable RX direction
- furi_hal_serial_enable_direction(fhttp.serial_handle, FuriHalSerialDirectionRx);
- // Start asynchronous RX with the callback
- furi_hal_serial_async_rx_start(fhttp.serial_handle, _flipper_http_rx_callback, &fhttp, false);
- // Wait for the TX to complete to ensure UART is ready
- furi_hal_serial_tx_wait_complete(fhttp.serial_handle);
- // Allocate the timer for handling timeouts
- fhttp.get_timeout_timer = furi_timer_alloc(
- get_timeout_timer_callback, // Callback function
- FuriTimerTypeOnce, // One-shot timer
- &fhttp // Context passed to callback
- );
- if (!fhttp.get_timeout_timer)
- {
- FURI_LOG_E(HTTP_TAG, "Failed to allocate HTTP request timeout timer.");
- // Cleanup resources
- furi_hal_serial_async_rx_stop(fhttp.serial_handle);
- furi_hal_serial_disable_direction(fhttp.serial_handle, FuriHalSerialDirectionRx);
- furi_hal_serial_control_release(fhttp.serial_handle);
- furi_hal_serial_deinit(fhttp.serial_handle);
- furi_thread_flags_set(fhttp.rx_thread_id, WorkerEvtStop);
- furi_thread_join(fhttp.rx_thread);
- furi_thread_free(fhttp.rx_thread);
- furi_stream_buffer_free(fhttp.flipper_http_stream);
- return false;
- }
- // 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;
- return true;
- }
- // Deinitialize UART
- /**
- * @brief Deinitialize UART.
- * @return void
- * @note This function will stop the asynchronous RX, release the serial handle, and free the resources.
- */
- void flipper_http_deinit()
- {
- if (fhttp.serial_handle == NULL)
- {
- FURI_LOG_E(HTTP_TAG, "UART handle is NULL. Already deinitialized?");
- return;
- }
- // Stop asynchronous RX
- furi_hal_serial_async_rx_stop(fhttp.serial_handle);
- // Release and deinitialize the serial handle
- furi_hal_serial_disable_direction(fhttp.serial_handle, FuriHalSerialDirectionRx);
- furi_hal_serial_control_release(fhttp.serial_handle);
- furi_hal_serial_deinit(fhttp.serial_handle);
- // Signal the worker thread to stop
- furi_thread_flags_set(fhttp.rx_thread_id, WorkerEvtStop);
- // Wait for the thread to finish
- furi_thread_join(fhttp.rx_thread);
- // Free the thread resources
- furi_thread_free(fhttp.rx_thread);
- // Free the stream buffer
- furi_stream_buffer_free(fhttp.flipper_http_stream);
- // Free the timer
- if (fhttp.get_timeout_timer)
- {
- furi_timer_free(fhttp.get_timeout_timer);
- 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);
- fhttp.last_response = NULL;
- }
- // FURI_LOG_I("FlipperHTTP", "UART deinitialized successfully.");
- }
- // Function to send data over UART with newline termination
- /**
- * @brief Send data over UART with newline termination.
- * @return true if the data was sent successfully, false otherwise.
- * @param data The data to send over UART.
- * @note The data will be sent over UART with a newline character appended.
- */
- bool flipper_http_send_data(const char *data)
- {
- size_t data_length = strlen(data);
- if (data_length == 0)
- {
- FURI_LOG_E("FlipperHTTP", "Attempted to send empty data.");
- return false;
- }
- // Create a buffer with data + '\n'
- size_t send_length = data_length + 1; // +1 for '\n'
- if (send_length > 256)
- { // Ensure buffer size is sufficient
- FURI_LOG_E("FlipperHTTP", "Data too long to send over FHTTP.");
- return false;
- }
- char send_buffer[257]; // 256 + 1 for safety
- strncpy(send_buffer, data, 256);
- send_buffer[data_length] = '\n'; // Append newline
- send_buffer[data_length + 1] = '\0'; // Null-terminate
- if (fhttp.state == INACTIVE && ((strstr(send_buffer, "[PING]") == NULL) && (strstr(send_buffer, "[WIFI/CONNECT]") == NULL)))
- {
- FURI_LOG_E("FlipperHTTP", "Cannot send data while INACTIVE.");
- fhttp.last_response = "Cannot send data while INACTIVE.";
- return false;
- }
- fhttp.state = SENDING;
- furi_hal_serial_tx(fhttp.serial_handle, (const uint8_t *)send_buffer, send_length);
- // Uncomment below line to log the data sent over UART
- // FURI_LOG_I("FlipperHTTP", "Sent data over UART: %s", send_buffer);
- fhttp.state = IDLE;
- return true;
- }
- // Function to send a PING request
- /**
- * @brief Send a PING request to check the connection status.
- * @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.
- * @note The state will remain INACTIVE until a PONG is received.
- */
- bool flipper_http_ping()
- {
- const char *command = "[PING]";
- if (!flipper_http_send_data(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to send PING command.");
- return false;
- }
- // set state as INACTIVE to be made IDLE if PONG is received
- fhttp.state = INACTIVE;
- // 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.
- * @return true if the request was successful, false otherwise.
- * @note The received data will be handled asynchronously via the callback.
- */
- bool flipper_http_scan_wifi()
- {
- const char *command = "[WIFI/SCAN]";
- if (!flipper_http_send_data(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to send WiFi scan command.");
- return false;
- }
- // The response will be handled asynchronously via the callback
- return true;
- }
- // Function to save WiFi settings (returns true if successful)
- /**
- * @brief Send a command to save WiFi settings.
- * @return true if the request was successful, false otherwise.
- * @note The received data will be handled asynchronously via the callback.
- */
- bool flipper_http_save_wifi(const char *ssid, const char *password)
- {
- if (!ssid || !password)
- {
- FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_save_wifi.");
- return false;
- }
- char buffer[256];
- int ret = snprintf(buffer, sizeof(buffer), "[WIFI/SAVE]{\"ssid\":\"%s\",\"password\":\"%s\"}", ssid, password);
- if (ret < 0 || ret >= (int)sizeof(buffer))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to format WiFi save command.");
- return false;
- }
- if (!flipper_http_send_data(buffer))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to send WiFi save 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.
- * @return true if the request was successful, false otherwise.
- * @note The received data will be handled asynchronously via the callback.
- */
- bool flipper_http_disconnect_wifi()
- {
- const char *command = "[WIFI/DISCONNECT]";
- if (!flipper_http_send_data(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to send WiFi disconnect command.");
- return false;
- }
- // The response will be handled asynchronously via the callback
- return true;
- }
- // Function to connect to WiFi (returns true if successful)
- /**
- * @brief Send a command to connect to WiFi.
- * @return true if the request was successful, false otherwise.
- * @note The received data will be handled asynchronously via the callback.
- */
- bool flipper_http_connect_wifi()
- {
- const char *command = "[WIFI/CONNECT]";
- if (!flipper_http_send_data(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to send WiFi connect command.");
- return false;
- }
- // The response will be handled asynchronously via the callback
- return true;
- }
- // Function to send a GET request
- /**
- * @brief Send a GET request to the specified URL.
- * @return true if the request was successful, false otherwise.
- * @param url The URL to send the GET request to.
- * @note The received data will be handled asynchronously via the callback.
- */
- bool flipper_http_get_request(const char *url)
- {
- if (!url)
- {
- FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_get_request.");
- return false;
- }
- // Prepare GET request command
- char command[256];
- int ret = snprintf(command, sizeof(command), "[GET]%s", url);
- if (ret < 0 || ret >= (int)sizeof(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to format GET request command.");
- return false;
- }
- // Send GET request via UART
- if (!flipper_http_send_data(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to send GET request command.");
- return false;
- }
- // The response will be handled asynchronously via the callback
- return true;
- }
- // Function to send a GET request with headers
- /**
- * @brief Send a GET request to the specified URL.
- * @return true if the request was successful, false otherwise.
- * @param url The URL to send the GET request to.
- * @param headers The headers to send with the GET request.
- * @note The received data will be handled asynchronously via the callback.
- */
- bool flipper_http_get_request_with_headers(const char *url, const char *headers)
- {
- if (!url || !headers)
- {
- FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_get_request_with_headers.");
- return false;
- }
- // Prepare GET request command with headers
- char command[256];
- int ret = snprintf(command, sizeof(command), "[GET/HTTP]{\"url\":\"%s\",\"headers\":%s}", url, headers);
- if (ret < 0 || ret >= (int)sizeof(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to format GET request command with headers.");
- return false;
- }
- // Send GET request via UART
- if (!flipper_http_send_data(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to send GET request command with headers.");
- return false;
- }
- // The response will be handled asynchronously via the callback
- return true;
- }
- // Function to send a GET request with headers and return bytes
- /**
- * @brief Send a GET request to the specified URL.
- * @return true if the request was successful, false otherwise.
- * @param url The URL to send the GET request to.
- * @param headers The headers to send with the GET request.
- * @note The received data will be handled asynchronously via the callback.
- */
- bool flipper_http_get_request_bytes(const char *url, const char *headers)
- {
- if (!url || !headers)
- {
- FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_get_request_bytes.");
- return false;
- }
- // Prepare GET request command with headers
- char command[256];
- int ret = snprintf(command, sizeof(command), "[GET/BYTES]{\"url\":\"%s\",\"headers\":%s}", url, headers);
- if (ret < 0 || ret >= (int)sizeof(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to format GET request command with headers.");
- return false;
- }
- // Send GET request via UART
- if (!flipper_http_send_data(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to send GET request command with headers.");
- return false;
- }
- // The response will be handled asynchronously via the callback
- return true;
- }
- // Function to send a POST request with headers
- /**
- * @brief Send a POST request to the specified URL.
- * @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.
- * @note The received data will be handled asynchronously via the callback.
- */
- bool flipper_http_post_request_with_headers(const char *url, const char *headers, const char *payload)
- {
- if (!url || !headers || !payload)
- {
- FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_post_request_with_headers.");
- return false;
- }
- // Prepare POST request command with headers and data
- char command[256];
- int ret = snprintf(command, sizeof(command), "[POST/HTTP]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}", url, headers, payload);
- if (ret < 0 || ret >= (int)sizeof(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to format POST request command with headers and data.");
- return false;
- }
- // Send POST request via UART
- if (!flipper_http_send_data(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to send POST request command with headers and data.");
- return false;
- }
- // The response will be handled asynchronously via the callback
- return true;
- }
- // Function to send a POST request with headers and return bytes
- /**
- * @brief Send a POST request to the specified URL.
- * @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.
- * @note The received data will be handled asynchronously via the callback.
- */
- bool flipper_http_post_request_bytes(const char *url, const char *headers, const char *payload)
- {
- if (!url || !headers || !payload)
- {
- FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_post_request_bytes.");
- return false;
- }
- // Prepare POST request command with headers and data
- char command[256];
- int ret = snprintf(command, sizeof(command), "[POST/BYTES]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}", url, headers, payload);
- if (ret < 0 || ret >= (int)sizeof(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to format POST request command with headers and data.");
- return false;
- }
- // Send POST request via UART
- if (!flipper_http_send_data(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to send POST request command with headers and data.");
- return false;
- }
- // The response will be handled asynchronously via the callback
- return true;
- }
- // Function to send a PUT request with headers
- /**
- * @brief Send a PUT request to the specified URL.
- * @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.
- * @note The received data will be handled asynchronously via the callback.
- */
- bool flipper_http_put_request_with_headers(const char *url, const char *headers, const char *payload)
- {
- if (!url || !headers || !payload)
- {
- FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_put_request_with_headers.");
- return false;
- }
- // Prepare PUT request command with headers and data
- char command[256];
- int ret = snprintf(command, sizeof(command), "[PUT/HTTP]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}", url, headers, payload);
- if (ret < 0 || ret >= (int)sizeof(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to format PUT request command with headers and data.");
- return false;
- }
- // Send PUT request via UART
- if (!flipper_http_send_data(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to send PUT request command with headers and data.");
- return false;
- }
- // The response will be handled asynchronously via the callback
- return true;
- }
- // Function to send a DELETE request with headers
- /**
- * @brief Send a DELETE request to the specified URL.
- * @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.
- * @note The received data will be handled asynchronously via the callback.
- */
- bool flipper_http_delete_request_with_headers(const char *url, const char *headers, const char *payload)
- {
- if (!url || !headers || !payload)
- {
- FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_delete_request_with_headers.");
- return false;
- }
- // Prepare DELETE request command with headers and data
- char command[256];
- int ret = snprintf(command, sizeof(command), "[DELETE/HTTP]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}", url, headers, payload);
- if (ret < 0 || ret >= (int)sizeof(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to format DELETE request command with headers and data.");
- return false;
- }
- // Send DELETE request via UART
- if (!flipper_http_send_data(command))
- {
- FURI_LOG_E("FlipperHTTP", "Failed to send DELETE request command with headers and data.");
- return false;
- }
- // The response will be handled asynchronously via the callback
- return true;
- }
- // Function to handle received data asynchronously
- /**
- * @brief Callback function to handle received data asynchronously.
- * @return void
- * @param line The received line.
- * @param context The context passed to the callback.
- * @note The received data will be handled asynchronously via the callback and handles the state of the UART.
- */
- void flipper_http_rx_callback(const char *line, void *context)
- {
- if (!line || !context)
- {
- FURI_LOG_E(HTTP_TAG, "Invalid arguments provided to flipper_http_rx_callback.");
- return;
- }
- // 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;
- }
- free(trimmed_line); // Free the allocated memory for trimmed_line
- if (fhttp.state != INACTIVE && fhttp.state != ISSUE)
- {
- fhttp.state = RECEIVING;
- }
- // Uncomment below line to log the data received over UART
- 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)
- {
- // Restart the timeout timer each time new data is received
- furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
- if (strstr(line, "[GET/END]") != NULL)
- {
- 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;
- }
- }
- // 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.just_started_get)
- {
- fhttp.just_started_get = true;
- }
- return;
- }
- // Check if we've started receiving data from a POST request
- else if (fhttp.started_receiving_post)
- {
- // Restart the timeout timer each time new data is received
- furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
- 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;
- }
- }
- // 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.just_started_post)
- {
- fhttp.just_started_post = true;
- }
- return;
- }
- // Check if we've started receiving data from a PUT request
- else if (fhttp.started_receiving_put)
- {
- // Restart the timeout timer each time new data is received
- furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
- if (strstr(line, "[PUT/END]") != NULL)
- {
- 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;
- }
- }
- // 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.just_started_put)
- {
- fhttp.just_started_put = true;
- }
- return;
- }
- // Check if we've started receiving data from a DELETE request
- else if (fhttp.started_receiving_delete)
- {
- // Restart the timeout timer each time new data is received
- furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
- if (strstr(line, "[DELETE/END]") != NULL)
- {
- 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;
- }
- }
- // 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.just_started_delete)
- {
- fhttp.just_started_delete = true;
- }
- return;
- }
- // Handle different types of responses
- if (strstr(line, "[SUCCESS]") != NULL || strstr(line, "[CONNECTED]") != NULL)
- {
- FURI_LOG_I(HTTP_TAG, "Operation succeeded.");
- }
- else if (strstr(line, "[INFO]") != NULL)
- {
- FURI_LOG_I(HTTP_TAG, "Received info: %s", line);
- if (fhttp.state == INACTIVE && strstr(line, "[INFO] Already connected to Wifi.") != NULL)
- {
- fhttp.state = IDLE;
- }
- }
- else if (strstr(line, "[GET/SUCCESS]") != NULL)
- {
- FURI_LOG_I(HTTP_TAG, "GET request succeeded.");
- 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;
- }
- 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;
- }
- 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.");
- }
- else if (strstr(line, "[ERROR]") != NULL)
- {
- FURI_LOG_E(HTTP_TAG, "Received error: %s", line);
- fhttp.state = ISSUE;
- return;
- }
- else if (strstr(line, "[PONG]") != NULL)
- {
- FURI_LOG_I(HTTP_TAG, "Received PONG response: Wifi Dev Board is still alive.");
- // send command to connect to WiFi
- if (fhttp.state == INACTIVE)
- {
- fhttp.state = IDLE;
- return;
- }
- }
- if (fhttp.state == INACTIVE && strstr(line, "[PONG]") != NULL)
- {
- fhttp.state = IDLE;
- }
- else if (fhttp.state == INACTIVE && strstr(line, "[PONG]") == NULL)
- {
- fhttp.state = INACTIVE;
- }
- else
- {
- 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;
- char *trimmed_str;
- size_t len;
- // Trim leading space
- while (isspace((unsigned char)*str))
- str++;
- // All spaces?
- if (*str == 0)
- return strdup(""); // Return an empty string if all spaces
- // Trim trailing space
- end = str + strlen(str) - 1;
- while (end > str && isspace((unsigned char)*end))
- end--;
- // Set length for the trimmed string
- len = end - str + 1;
- // Allocate space for the trimmed string and null terminator
- trimmed_str = (char *)malloc(len + 1);
- if (trimmed_str == NULL)
- {
- return NULL; // Handle memory allocation failure
- }
- // Copy the trimmed part of the string into trimmed_str
- strncpy(trimmed_str, str, len);
- trimmed_str[len] = '\0'; // Null terminate the string
- return trimmed_str;
- }
- #endif // FLIPPER_HTTP_H
|