flipper_http.h 16 KB

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