flipper_http.h 10 KB

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