flipper_http.h 16 KB

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