flipper_http.h 16 KB

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