flipper_http.h 14 KB

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