flipper_http.h 14 KB

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