flipper_http.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // flipper_http.h
  2. #ifndef FLIPPER_HTTP_H
  3. #define FLIPPER_HTTP_H
  4. #include <furi.h>
  5. #include <furi_hal.h>
  6. #include <furi_hal_gpio.h>
  7. #include <furi_hal_serial.h>
  8. #include <storage/storage.h>
  9. // STORAGE_EXT_PATH_PREFIX is defined in the Furi SDK as /ext
  10. #define HTTP_TAG "Web Crawler" // change this to your app name
  11. #define http_tag "web_crawler" // change this to your app id
  12. #define UART_CH (FuriHalSerialIdUsart) // UART channel
  13. #define TIMEOUT_DURATION_TICKS (5 * 1000) // 5 seconds
  14. #define BAUDRATE (115200) // UART baudrate
  15. #define RX_BUF_SIZE 2048 // UART RX buffer size
  16. #define RX_LINE_BUFFER_SIZE 2048 // UART RX line buffer size (increase for large responses)
  17. #define MAX_FILE_SHOW 4096 // Maximum data from file to show
  18. #define FILE_BUFFER_SIZE 512 // File buffer size
  19. // Forward declaration for callback
  20. typedef void (*FlipperHTTP_Callback)(const char* line, void* context);
  21. // State variable to track the UART state
  22. typedef enum {
  23. INACTIVE, // Inactive state
  24. IDLE, // Default state
  25. RECEIVING, // Receiving data
  26. SENDING, // Sending data
  27. ISSUE, // Issue with connection
  28. } SerialState;
  29. // Event Flags for UART Worker Thread
  30. typedef enum {
  31. WorkerEvtStop = (1 << 0),
  32. WorkerEvtRxDone = (1 << 1),
  33. } WorkerEvtFlags;
  34. // FlipperHTTP Structure
  35. typedef struct {
  36. FuriStreamBuffer* flipper_http_stream; // Stream buffer for UART communication
  37. FuriHalSerialHandle* serial_handle; // Serial handle for UART communication
  38. FuriThread* rx_thread; // Worker thread for UART
  39. FuriThreadId rx_thread_id; // Worker thread ID
  40. FlipperHTTP_Callback handle_rx_line_cb; // Callback for received lines
  41. void* callback_context; // Context for the callback
  42. SerialState state; // State of the UART
  43. // variable to store the last received data from the UART
  44. char* last_response;
  45. char file_path[256]; // Path to save the received data
  46. // Timer-related members
  47. FuriTimer* get_timeout_timer; // Timer for HTTP request timeout
  48. bool started_receiving_get; // Indicates if a GET request has started
  49. bool just_started_get; // Indicates if GET data reception has just started
  50. bool started_receiving_post; // Indicates if a POST request has started
  51. bool just_started_post; // Indicates if POST data reception has just started
  52. bool started_receiving_put; // Indicates if a PUT request has started
  53. bool just_started_put; // Indicates if PUT data reception has just started
  54. bool started_receiving_delete; // Indicates if a DELETE request has started
  55. bool just_started_delete; // Indicates if DELETE data reception has just started
  56. // Buffer to hold the raw bytes received from the UART
  57. uint8_t* received_bytes;
  58. size_t received_bytes_len; // Length of the received bytes
  59. bool is_bytes_request; // Flag to indicate if the request is for bytes
  60. bool save_bytes; // Flag to save the received data to a file
  61. bool save_received_data; // Flag to save the received data to a file
  62. } FlipperHTTP;
  63. extern FlipperHTTP fhttp;
  64. // Global static array for the line buffer
  65. extern char rx_line_buffer[RX_LINE_BUFFER_SIZE];
  66. extern uint8_t file_buffer[FILE_BUFFER_SIZE];
  67. // fhttp.last_response holds the last received data from the UART
  68. // Function to append received data to file
  69. // make sure to initialize the file path before calling this function
  70. bool flipper_http_append_to_file(
  71. const void* data,
  72. size_t data_size,
  73. bool start_new_file,
  74. char* file_path);
  75. FuriString* flipper_http_load_from_file(char* file_path);
  76. // UART worker thread
  77. /**
  78. * @brief Worker thread to handle UART data asynchronously.
  79. * @return 0
  80. * @param context The context to pass to the callback.
  81. * @note This function will handle received data asynchronously via the callback.
  82. */
  83. // UART worker thread
  84. int32_t flipper_http_worker(void* context);
  85. // Timer callback function
  86. /**
  87. * @brief Callback function for the GET timeout timer.
  88. * @return 0
  89. * @param context The context to pass to the callback.
  90. * @note This function will be called when the GET request times out.
  91. */
  92. void get_timeout_timer_callback(void* context);
  93. // UART RX Handler Callback (Interrupt Context)
  94. /**
  95. * @brief A private callback function to handle received data asynchronously.
  96. * @return void
  97. * @param handle The UART handle.
  98. * @param event The event type.
  99. * @param context The context to pass to the callback.
  100. * @note This function will handle received data asynchronously via the callback.
  101. */
  102. void _flipper_http_rx_callback(
  103. FuriHalSerialHandle* handle,
  104. FuriHalSerialRxEvent event,
  105. void* context);
  106. // UART initialization function
  107. /**
  108. * @brief Initialize UART.
  109. * @return true if the UART was initialized successfully, false otherwise.
  110. * @param callback The callback function to handle received data (ex. flipper_http_rx_callback).
  111. * @param context The context to pass to the callback.
  112. * @note The received data will be handled asynchronously via the callback.
  113. */
  114. bool flipper_http_init(FlipperHTTP_Callback callback, void* context);
  115. // Deinitialize UART
  116. /**
  117. * @brief Deinitialize UART.
  118. * @return void
  119. * @note This function will stop the asynchronous RX, release the serial handle, and free the resources.
  120. */
  121. void flipper_http_deinit();
  122. // Function to send data over UART with newline termination
  123. /**
  124. * @brief Send data over UART with newline termination.
  125. * @return true if the data was sent successfully, false otherwise.
  126. * @param data The data to send over UART.
  127. * @note The data will be sent over UART with a newline character appended.
  128. */
  129. bool flipper_http_send_data(const char* data);
  130. // Function to send a PING request
  131. /**
  132. * @brief Send a PING request to check if the Wifi Dev Board is connected.
  133. * @return true if the request was successful, false otherwise.
  134. * @note The received data will be handled asynchronously via the callback.
  135. * @note This is best used to check if the Wifi Dev Board is connected.
  136. * @note The state will remain INACTIVE until a PONG is received.
  137. */
  138. bool flipper_http_ping();
  139. // Function to list available commands
  140. /**
  141. * @brief Send a command to list available commands.
  142. * @return true if the request was successful, false otherwise.
  143. * @note The received data will be handled asynchronously via the callback.
  144. */
  145. bool flipper_http_list_commands();
  146. // Function to turn on the LED
  147. /**
  148. * @brief Allow the LED to display while processing.
  149. * @return true if the request was successful, false otherwise.
  150. * @note The received data will be handled asynchronously via the callback.
  151. */
  152. bool flipper_http_led_on();
  153. // Function to turn off the LED
  154. /**
  155. * @brief Disable the LED from displaying while processing.
  156. * @return true if the request was successful, false otherwise.
  157. * @note The received data will be handled asynchronously via the callback.
  158. */
  159. bool flipper_http_led_off();
  160. // Function to parse JSON data
  161. /**
  162. * @brief Parse JSON data.
  163. * @return true if the JSON data was parsed successfully, false otherwise.
  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(const char* key, const char* json_data);
  169. // Function to parse JSON array data
  170. /**
  171. * @brief Parse JSON array data.
  172. * @return true if the JSON array data was parsed successfully, false otherwise.
  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(const char* key, int index, const char* json_data);
  179. // Function to scan for WiFi networks
  180. /**
  181. * @brief Send a command to scan for WiFi networks.
  182. * @return true if the request was successful, false otherwise.
  183. * @note The received data will be handled asynchronously via the callback.
  184. */
  185. bool flipper_http_scan_wifi();
  186. // Function to save WiFi settings (returns true if successful)
  187. /**
  188. * @brief Send a command to save WiFi settings.
  189. * @return true if the request was successful, false otherwise.
  190. * @note The received data will be handled asynchronously via the callback.
  191. */
  192. bool flipper_http_save_wifi(const char* ssid, const char* password);
  193. // Function to get IP address of WiFi Devboard
  194. /**
  195. * @brief Send a command to get the IP address of the WiFi Devboard
  196. * @return true if the request was successful, false otherwise.
  197. * @note The received data will be handled asynchronously via the callback.
  198. */
  199. bool flipper_http_ip_address();
  200. // Function to get IP address of the connected WiFi network
  201. /**
  202. * @brief Send a command to get the IP address of the connected WiFi network.
  203. * @return true if the request was successful, false otherwise.
  204. * @note The received data will be handled asynchronously via the callback.
  205. */
  206. bool flipper_http_ip_wifi();
  207. // Function to disconnect from WiFi (returns true if successful)
  208. /**
  209. * @brief Send a command to disconnect from WiFi.
  210. * @return true if the request was successful, false otherwise.
  211. * @note The received data will be handled asynchronously via the callback.
  212. */
  213. bool flipper_http_disconnect_wifi();
  214. // Function to connect to WiFi (returns true if successful)
  215. /**
  216. * @brief Send a command to connect to WiFi.
  217. * @return true if the request was successful, false otherwise.
  218. * @note The received data will be handled asynchronously via the callback.
  219. */
  220. bool flipper_http_connect_wifi();
  221. // Function to send a GET request
  222. /**
  223. * @brief Send a GET request to the specified URL.
  224. * @return true if the request was successful, false otherwise.
  225. * @param url The URL to send the GET request to.
  226. * @note The received data will be handled asynchronously via the callback.
  227. */
  228. bool flipper_http_get_request(const char* url);
  229. // Function to send a GET request with headers
  230. /**
  231. * @brief Send a GET request to the specified URL.
  232. * @return true if the request was successful, false otherwise.
  233. * @param url The URL to send the GET request to.
  234. * @param headers The headers to send with the GET request.
  235. * @note The received data will be handled asynchronously via the callback.
  236. */
  237. bool flipper_http_get_request_with_headers(const char* url, const char* headers);
  238. // Function to send a GET request with headers and return bytes
  239. /**
  240. * @brief Send a GET request to the specified URL.
  241. * @return true if the request was successful, false otherwise.
  242. * @param url The URL to send the GET request to.
  243. * @param headers The headers to send with the GET request.
  244. * @note The received data will be handled asynchronously via the callback.
  245. */
  246. bool flipper_http_get_request_bytes(const char* url, const char* headers);
  247. // Function to send a POST request with headers
  248. /**
  249. * @brief Send a POST request to the specified URL.
  250. * @return true if the request was successful, false otherwise.
  251. * @param url The URL to send the POST request to.
  252. * @param headers The headers to send with the POST request.
  253. * @param data The data to send with the POST request.
  254. * @note The received data will be handled asynchronously via the callback.
  255. */
  256. bool flipper_http_post_request_with_headers(
  257. const char* url,
  258. const char* headers,
  259. const char* payload);
  260. // Function to send a POST request with headers and return bytes
  261. /**
  262. * @brief Send a POST request to the specified URL.
  263. * @return true if the request was successful, false otherwise.
  264. * @param url The URL to send the POST request to.
  265. * @param headers The headers to send with the POST request.
  266. * @param payload The data to send with the POST request.
  267. * @note The received data will be handled asynchronously via the callback.
  268. */
  269. bool flipper_http_post_request_bytes(const char* url, const char* headers, const char* payload);
  270. // Function to send a PUT request with headers
  271. /**
  272. * @brief Send a PUT request to the specified URL.
  273. * @return true if the request was successful, false otherwise.
  274. * @param url The URL to send the PUT request to.
  275. * @param headers The headers to send with the PUT request.
  276. * @param data The data to send with the PUT request.
  277. * @note The received data will be handled asynchronously via the callback.
  278. */
  279. bool flipper_http_put_request_with_headers(
  280. const char* url,
  281. const char* headers,
  282. const char* payload);
  283. // Function to send a DELETE request with headers
  284. /**
  285. * @brief Send a DELETE request to the specified URL.
  286. * @return true if the request was successful, false otherwise.
  287. * @param url The URL to send the DELETE request to.
  288. * @param headers The headers to send with the DELETE request.
  289. * @param data The data to send with the DELETE request.
  290. * @note The received data will be handled asynchronously via the callback.
  291. */
  292. bool flipper_http_delete_request_with_headers(
  293. const char* url,
  294. const char* headers,
  295. const char* payload);
  296. // Function to handle received data asynchronously
  297. /**
  298. * @brief Callback function to handle received data asynchronously.
  299. * @return void
  300. * @param line The received line.
  301. * @param context The context passed to the callback.
  302. * @note The received data will be handled asynchronously via the callback and handles the state of the UART.
  303. */
  304. void flipper_http_rx_callback(const char* line, void* context);
  305. // Function to trim leading and trailing spaces and newlines from a constant string
  306. char* trim(const char* str);
  307. /**
  308. * @brief Process requests and parse JSON data asynchronously
  309. * @param http_request The function to send the request
  310. * @param parse_json The function to parse the JSON
  311. * @return true if successful, false otherwise
  312. */
  313. bool flipper_http_process_response_async(bool (*http_request)(void), bool (*parse_json)(void));
  314. #endif // FLIPPER_HTTP_H