flipper_http.h 11 KB

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