flipper_http.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Description: Flipper HTTP API (For use with Flipper Zero and the FlipperHTTP flash: https://github.com/jblanked/FlipperHTTP)
  2. // License: MIT
  3. // Author: JBlanked
  4. // File: flipper_http.h
  5. #pragma once
  6. #include <gui/gui.h>
  7. #include <gui/view.h>
  8. #include <gui/view_dispatcher.h>
  9. #include <gui/modules/loading.h>
  10. #include <furi.h>
  11. #include <furi_hal.h>
  12. #include <furi_hal_gpio.h>
  13. #include <furi_hal_serial.h>
  14. #include <storage/storage.h>
  15. #include <momentum/settings.h>
  16. // added by Derek Jamison to lower memory usage
  17. #undef FURI_LOG_E
  18. #define FURI_LOG_E(tag, msg, ...)
  19. #undef FURI_LOG_I
  20. #define FURI_LOG_I(tag, msg, ...)
  21. #define HTTP_TAG "FlipWorld" // change this to your app name
  22. #define http_tag "flip_world" // change this to your app id
  23. #define UART_CH (momentum_settings.uart_esp_channel) // UART channel
  24. #define TIMEOUT_DURATION_TICKS (5 * 1000) // 5 seconds
  25. #define BAUDRATE (115200) // UART baudrate
  26. #define RX_BUF_SIZE 2048 // UART RX buffer size
  27. #define RX_LINE_BUFFER_SIZE 1024 // UART RX line buffer size (increase for large responses)
  28. #define MAX_FILE_SHOW 3000 // Maximum data from file to show
  29. #define FILE_BUFFER_SIZE 512 // File buffer size
  30. // Forward declaration for callback
  31. typedef void (*FlipperHTTP_Callback)(const char *line, void *context);
  32. // State variable to track the UART state
  33. typedef enum
  34. {
  35. INACTIVE, // Inactive state
  36. IDLE, // Default state
  37. RECEIVING, // Receiving data
  38. SENDING, // Sending data
  39. ISSUE, // Issue with connection
  40. } HTTPState;
  41. // Event Flags for UART Worker Thread
  42. typedef enum
  43. {
  44. WorkerEvtStop = (1 << 0),
  45. WorkerEvtRxDone = (1 << 1),
  46. } WorkerEvtFlags;
  47. typedef enum
  48. {
  49. GET, // GET request
  50. POST, // POST request
  51. PUT, // PUT request
  52. DELETE, // DELETE request
  53. //
  54. BYTES, // Stream bytes to file
  55. BYTES_POST, // Stream bytes to file after a POST request
  56. } HTTPMethod;
  57. typedef enum
  58. {
  59. HTTP_CMD_WIFI_CONNECT,
  60. HTTP_CMD_WIFI_DISCONNECT,
  61. HTTP_CMD_IP_ADDRESS,
  62. HTTP_CMD_IP_WIFI,
  63. HTTP_CMD_SCAN,
  64. HTTP_CMD_LIST_COMMANDS,
  65. HTTP_CMD_LED_ON,
  66. HTTP_CMD_LED_OFF,
  67. HTTP_CMD_PING,
  68. HTTP_CMD_REBOOT
  69. } HTTPCommand; // list of non-input commands
  70. // FlipperHTTP Structure
  71. typedef struct
  72. {
  73. FuriStreamBuffer *flipper_http_stream; // Stream buffer for UART communication
  74. FuriHalSerialHandle *serial_handle; // Serial handle for UART communication
  75. FuriThread *rx_thread; // Worker thread for UART
  76. FuriThreadId rx_thread_id; // Worker thread ID
  77. FlipperHTTP_Callback handle_rx_line_cb; // Callback for received lines
  78. void *callback_context; // Context for the callback
  79. HTTPState state; // State of the UART
  80. HTTPMethod method; // HTTP method
  81. char *last_response; // variable to store the last received data from the UART
  82. char file_path[256]; // Path to save the received data
  83. FuriTimer *get_timeout_timer; // Timer for HTTP request timeout
  84. bool started_receiving; // Indicates if a request has started
  85. bool just_started; // Indicates if data reception has just started
  86. bool is_bytes_request; // Flag to indicate if the request is for bytes
  87. bool save_bytes; // Flag to save the received data to a file
  88. bool save_received_data; // Flag to save the received data to a file
  89. bool just_started_bytes; // Indicates if bytes data reception has just started
  90. size_t bytes_received; // Number of bytes received
  91. char rx_line_buffer[RX_LINE_BUFFER_SIZE]; // Buffer for received lines
  92. uint8_t file_buffer[FILE_BUFFER_SIZE]; // Buffer for file data
  93. size_t file_buffer_len; // Length of the file buffer
  94. size_t content_length; // Length of the content received
  95. int status_code; // HTTP status code
  96. } FlipperHTTP;
  97. /**
  98. * @brief Initialize UART.
  99. * @return FlipperHTTP context if the UART was initialized successfully, NULL otherwise.
  100. * @note The received data will be handled asynchronously via the callback.
  101. */
  102. FlipperHTTP *flipper_http_alloc();
  103. /**
  104. * @brief Deinitialize UART.
  105. * @return void
  106. * @param fhttp The FlipperHTTP context
  107. * @note This function will stop the asynchronous RX, release the serial handle, and free the resources.
  108. */
  109. void flipper_http_free(FlipperHTTP *fhttp);
  110. /**
  111. * @brief Append received data to a file.
  112. * @return true if the data was appended successfully, false otherwise.
  113. * @param data The data to append to the file.
  114. * @param data_size The size of the data to append.
  115. * @param start_new_file Flag to indicate if a new file should be created.
  116. * @param file_path The path to the file.
  117. * @note Make sure to initialize the file path before calling this function.
  118. */
  119. bool flipper_http_append_to_file(const void *data, size_t data_size, bool start_new_file, char *file_path);
  120. /**
  121. * @brief Load data from a file.
  122. * @return The loaded data as a FuriString.
  123. * @param file_path The path to the file to load.
  124. */
  125. FuriString *flipper_http_load_from_file(char *file_path);
  126. /**
  127. * @brief Load data from a file with a size limit.
  128. * @return The loaded data as a FuriString.
  129. * @param file_path The path to the file to load.
  130. * @param limit The size limit for loading data.
  131. */
  132. FuriString *flipper_http_load_from_file_with_limit(char *file_path, size_t limit);
  133. /**
  134. * @brief Perform a task while displaying a loading screen
  135. * @param fhttp The FlipperHTTP context
  136. * @param http_request The function to send the request
  137. * @param parse_response The function to parse the response
  138. * @param success_view_id The view ID to switch to on success
  139. * @param failure_view_id The view ID to switch to on failure
  140. * @param view_dispatcher The view dispatcher to use
  141. * @return
  142. */
  143. void flipper_http_loading_task(FlipperHTTP *fhttp,
  144. bool (*http_request)(void),
  145. bool (*parse_response)(void),
  146. uint32_t success_view_id,
  147. uint32_t failure_view_id,
  148. ViewDispatcher **view_dispatcher);
  149. /**
  150. * @brief Parse JSON data.
  151. * @return true if the JSON data was parsed successfully, false otherwise.
  152. * @param fhttp The FlipperHTTP context
  153. * @param key The key to parse from the JSON data.
  154. * @param json_data The JSON data to parse.
  155. * @note The received data will be handled asynchronously via the callback.
  156. */
  157. bool flipper_http_parse_json(FlipperHTTP *fhttp, const char *key, const char *json_data);
  158. /**
  159. * @brief Parse JSON array data.
  160. * @return true if the JSON array data was parsed successfully, false otherwise.
  161. * @param fhttp The FlipperHTTP context
  162. * @param key The key to parse from the JSON array data.
  163. * @param index The index to parse from the JSON array data.
  164. * @param json_data The JSON array data to parse.
  165. * @note The received data will be handled asynchronously via the callback.
  166. */
  167. bool flipper_http_parse_json_array(FlipperHTTP *fhttp, const char *key, int index, const char *json_data);
  168. /**
  169. * @brief Process requests and parse JSON data asynchronously
  170. * @param fhttp The FlipperHTTP context
  171. * @param http_request The function to send the request
  172. * @param parse_json The function to parse the JSON
  173. * @return true if successful, false otherwise
  174. */
  175. bool flipper_http_process_response_async(FlipperHTTP *fhttp, bool (*http_request)(void), bool (*parse_json)(void));
  176. /**
  177. * @brief Send a request to the specified URL.
  178. * @return true if the request was successful, false otherwise.
  179. * @param fhttp The FlipperHTTP context
  180. * @param method The HTTP method to use.
  181. * @param url The URL to send the request to.
  182. * @param headers The headers to send with the request.
  183. * @param payload The data to send with the request.
  184. * @note The received data will be handled asynchronously via the callback.
  185. */
  186. bool flipper_http_request(FlipperHTTP *fhttp, HTTPMethod method, const char *url, const char *headers, const char *payload);
  187. /**
  188. * @brief Send a command to save WiFi settings.
  189. * @return true if the request was successful, false otherwise.
  190. * @param fhttp The FlipperHTTP context
  191. * @note The received data will be handled asynchronously via the callback.
  192. */
  193. bool flipper_http_save_wifi(FlipperHTTP *fhttp, const char *ssid, const char *password);
  194. /**
  195. * @brief Send a command.
  196. * @return true if the request was successful, false otherwise.
  197. * @param fhttp The FlipperHTTP context
  198. * @param command The command to send.
  199. * @note The received data will be handled asynchronously via the callback.
  200. */
  201. bool flipper_http_send_command(FlipperHTTP *fhttp, HTTPCommand command);
  202. /**
  203. * @brief Send data over UART with newline termination.
  204. * @return true if the data was sent successfully, false otherwise.
  205. * @param fhttp The FlipperHTTP context
  206. * @param data The data to send over UART.
  207. * @note The data will be sent over UART with a newline character appended.
  208. */
  209. bool flipper_http_send_data(FlipperHTTP *fhttp, const char *data);
  210. /**
  211. * @brief Send a request to the specified URL to start a WebSocket connection.
  212. * @return true if the request was successful, false otherwise.
  213. * @param fhttp The FlipperHTTP context
  214. * @param url The URL to send the WebSocket request to.
  215. * @param port The port to connect to
  216. * @param headers The headers to send with the WebSocket request
  217. * @note The received data will be handled asynchronously via the callback.
  218. */
  219. bool flipper_http_websocket_start(FlipperHTTP *fhttp, const char *url, uint16_t port, const char *headers);
  220. /**
  221. * @brief Send a request to stop the WebSocket connection.
  222. * @return true if the request was successful, false otherwise.
  223. * @param fhttp The FlipperHTTP context
  224. * @note The received data will be handled asynchronously via the callback.
  225. */
  226. bool flipper_http_websocket_stop(FlipperHTTP *fhttp);