flipper_http.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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 "WebCrawler" // change this to your app name
  11. #define http_tag "web_crawler_app" // change this to your app id
  12. #define UART_CH (FuriHalSerialIdUsart) // UART channel
  13. #define TIMEOUT_DURATION_TICKS (2 * 1000) // 2 seconds
  14. #define BAUDRATE (115200) // UART baudrate
  15. #define RX_BUF_SIZE 1024 // UART RX buffer size
  16. // Forward declaration for callback
  17. typedef void (*FlipperHTTP_Callback)(const char *line, void *context);
  18. // Functions
  19. bool flipper_http_init(FlipperHTTP_Callback callback, void *context);
  20. void flipper_http_deinit();
  21. //---
  22. void flipper_http_rx_callback(const char *line, void *context);
  23. bool flipper_http_send_data(const char *data);
  24. //---
  25. bool flipper_http_connect_wifi();
  26. bool flipper_http_disconnect_wifi();
  27. bool flipper_http_ping();
  28. bool flipper_http_save_wifi(const char *ssid, const char *password);
  29. //---
  30. bool flipper_http_get_request(const char *url);
  31. //---
  32. bool flipper_http_save_received_data(size_t bytes_received, const char line_buffer[]);
  33. // Define GPIO pins for UART
  34. GpioPin test_pins[2] = {
  35. {.port = GPIOA, .pin = LL_GPIO_PIN_7}, // USART1_RX
  36. {.port = GPIOA, .pin = LL_GPIO_PIN_6} // USART1_TX
  37. };
  38. // State variable to track the UART state
  39. typedef enum
  40. {
  41. INACTIVE, // Inactive state
  42. IDLE, // Default state
  43. RECEIVING, // Receiving data
  44. SENDING, // Sending data
  45. ISSUE, // Issue with connection
  46. } SerialState;
  47. // Event Flags for UART Worker Thread
  48. typedef enum
  49. {
  50. WorkerEvtStop = (1 << 0),
  51. WorkerEvtRxDone = (1 << 1),
  52. } WorkerEvtFlags;
  53. // FlipperHTTP Structure
  54. typedef struct
  55. {
  56. FuriStreamBuffer *flipper_http_stream; // Stream buffer for UART communication
  57. FuriHalSerialHandle *serial_handle; // Serial handle for UART communication
  58. FuriThread *rx_thread; // Worker thread for UART
  59. uint8_t rx_buf[RX_BUF_SIZE]; // Buffer for received data
  60. FuriThreadId rx_thread_id; // Worker thread ID
  61. FlipperHTTP_Callback handle_rx_line_cb; // Callback for received lines
  62. void *callback_context; // Context for the callback
  63. SerialState state; // State of the UART
  64. // variable to store the last received data from the UART
  65. char *last_response;
  66. // Timer-related members
  67. FuriTimer *get_timeout_timer; // Timer for GET request timeout
  68. bool started_receiving; // Indicates if a GET request has started
  69. bool just_started; // Indicates if data reception has just started
  70. char *received_data; // Buffer to store received data
  71. } FlipperHTTP;
  72. // Declare uart as extern to prevent multiple definitions
  73. FlipperHTTP fhttp;
  74. typedef struct
  75. {
  76. char *key;
  77. char *value;
  78. } FlipperHTTPHeader;
  79. // Timer callback function
  80. /**
  81. * @brief Callback function for the GET timeout timer.
  82. * @return 0
  83. * @param context The context to pass to the callback.
  84. * @note This function will be called when the GET request times out.
  85. */
  86. void get_timeout_timer_callback(void *context)
  87. {
  88. UNUSED(context);
  89. FURI_LOG_E(HTTP_TAG, "Timeout reached: 2 seconds without receiving [GET/END]...");
  90. // Reset the state
  91. fhttp.started_receiving = false;
  92. // Free received data if any
  93. if (fhttp.received_data)
  94. {
  95. free(fhttp.received_data);
  96. fhttp.received_data = NULL;
  97. }
  98. // Update UART state
  99. fhttp.state = ISSUE;
  100. }
  101. // UART RX Handler Callback (Interrupt Context)
  102. /**
  103. * @brief A private callback function to handle received data asynchronously.
  104. * @return void
  105. * @param handle The UART handle.
  106. * @param event The event type.
  107. * @param context The context to pass to the callback.
  108. * @note This function will handle received data asynchronously via the callback.
  109. */
  110. static void _flipper_http_rx_callback(FuriHalSerialHandle *handle, FuriHalSerialRxEvent event, void *context)
  111. {
  112. UNUSED(context);
  113. if (event == FuriHalSerialRxEventData)
  114. {
  115. uint8_t data = furi_hal_serial_async_rx(handle);
  116. furi_stream_buffer_send(fhttp.flipper_http_stream, &data, 1, 0);
  117. furi_thread_flags_set(fhttp.rx_thread_id, WorkerEvtRxDone);
  118. }
  119. }
  120. // UART worker thread
  121. /**
  122. * @brief Worker thread to handle UART data asynchronously.
  123. * @return 0
  124. * @param context The context to pass to the callback.
  125. * @note This function will handle received data asynchronously via the callback.
  126. */
  127. static int32_t flipper_http_worker(void *context)
  128. {
  129. UNUSED(context);
  130. size_t rx_line_pos = 0;
  131. char rx_line_buffer[256]; // Buffer to collect a line
  132. while (1)
  133. {
  134. uint32_t events = furi_thread_flags_wait(WorkerEvtStop | WorkerEvtRxDone, FuriFlagWaitAny, FuriWaitForever);
  135. if (events & WorkerEvtStop)
  136. break;
  137. if (events & WorkerEvtRxDone)
  138. {
  139. size_t len = furi_stream_buffer_receive(fhttp.flipper_http_stream, fhttp.rx_buf, RX_BUF_SIZE * 10, 0);
  140. for (size_t i = 0; i < len; i++)
  141. {
  142. char c = fhttp.rx_buf[i];
  143. if (c == '\n' || rx_line_pos >= sizeof(rx_line_buffer) - 1)
  144. {
  145. rx_line_buffer[rx_line_pos] = '\0';
  146. // Invoke the callback with the complete line
  147. if (fhttp.handle_rx_line_cb)
  148. {
  149. fhttp.handle_rx_line_cb(rx_line_buffer, fhttp.callback_context);
  150. }
  151. // Reset the line buffer
  152. rx_line_pos = 0;
  153. }
  154. else
  155. {
  156. rx_line_buffer[rx_line_pos++] = c;
  157. }
  158. }
  159. }
  160. }
  161. return 0;
  162. }
  163. // UART initialization function
  164. /**
  165. * @brief Initialize UART.
  166. * @return true if the UART was initialized successfully, false otherwise.
  167. * @param callback The callback function to handle received data (ex. flipper_http_rx_callback).
  168. * @param context The context to pass to the callback.
  169. * @note The received data will be handled asynchronously via the callback.
  170. */
  171. bool flipper_http_init(FlipperHTTP_Callback callback, void *context)
  172. {
  173. fhttp.flipper_http_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
  174. if (!fhttp.flipper_http_stream)
  175. {
  176. FURI_LOG_E(HTTP_TAG, "Failed to allocate UART stream buffer.");
  177. return false;
  178. }
  179. fhttp.rx_thread = furi_thread_alloc();
  180. if (!fhttp.rx_thread)
  181. {
  182. FURI_LOG_E(HTTP_TAG, "Failed to allocate UART thread.");
  183. furi_stream_buffer_free(fhttp.flipper_http_stream);
  184. return false;
  185. }
  186. furi_thread_set_name(fhttp.rx_thread, "FlipperHTTP_RxThread");
  187. furi_thread_set_stack_size(fhttp.rx_thread, 1024);
  188. furi_thread_set_context(fhttp.rx_thread, &fhttp);
  189. furi_thread_set_callback(fhttp.rx_thread, flipper_http_worker);
  190. fhttp.handle_rx_line_cb = callback;
  191. fhttp.callback_context = context;
  192. furi_thread_start(fhttp.rx_thread);
  193. fhttp.rx_thread_id = furi_thread_get_id(fhttp.rx_thread);
  194. // Initialize GPIO pins for UART
  195. furi_hal_gpio_init_simple(&test_pins[0], GpioModeInput);
  196. furi_hal_gpio_init_simple(&test_pins[1], GpioModeOutputPushPull);
  197. fhttp.serial_handle = NULL;
  198. fhttp.serial_handle = furi_hal_serial_control_acquire(UART_CH);
  199. if (fhttp.serial_handle == NULL)
  200. {
  201. FURI_LOG_E(HTTP_TAG, "Failed to acquire UART control - handle is NULL");
  202. // Cleanup resources
  203. furi_thread_free(fhttp.rx_thread);
  204. furi_stream_buffer_free(fhttp.flipper_http_stream);
  205. return false;
  206. }
  207. // Initialize UART with acquired handle
  208. furi_hal_serial_init(fhttp.serial_handle, BAUDRATE);
  209. // Enable RX direction
  210. furi_hal_serial_enable_direction(fhttp.serial_handle, FuriHalSerialDirectionRx);
  211. // Start asynchronous RX with the callback
  212. furi_hal_serial_async_rx_start(fhttp.serial_handle, _flipper_http_rx_callback, &fhttp, false);
  213. // Wait for the TX to complete to ensure UART is ready
  214. furi_hal_serial_tx_wait_complete(fhttp.serial_handle);
  215. // Allocate the timer for handling timeouts
  216. fhttp.get_timeout_timer = furi_timer_alloc(
  217. get_timeout_timer_callback, // Callback function
  218. FuriTimerTypeOnce, // One-shot timer
  219. &fhttp // Context passed to callback
  220. );
  221. if (!fhttp.get_timeout_timer)
  222. {
  223. FURI_LOG_E(HTTP_TAG, "Failed to allocate GET timeout timer.");
  224. // Cleanup resources
  225. furi_hal_serial_async_rx_stop(fhttp.serial_handle);
  226. furi_hal_serial_disable_direction(fhttp.serial_handle, FuriHalSerialDirectionRx);
  227. furi_hal_serial_control_release(fhttp.serial_handle);
  228. furi_hal_serial_deinit(fhttp.serial_handle);
  229. furi_thread_flags_set(fhttp.rx_thread_id, WorkerEvtStop);
  230. furi_thread_join(fhttp.rx_thread);
  231. furi_thread_free(fhttp.rx_thread);
  232. furi_stream_buffer_free(fhttp.flipper_http_stream);
  233. return false;
  234. }
  235. // Set the timer thread priority if needed
  236. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  237. FURI_LOG_I(HTTP_TAG, "UART initialized successfully.");
  238. return true;
  239. }
  240. // Deinitialize UART
  241. /**
  242. * @brief Deinitialize UART.
  243. * @return void
  244. * @note This function will stop the asynchronous RX, release the serial handle, and free the resources.
  245. */
  246. void flipper_http_deinit()
  247. {
  248. if (fhttp.serial_handle == NULL)
  249. {
  250. FURI_LOG_E(HTTP_TAG, "UART handle is NULL. Already deinitialized?");
  251. return;
  252. }
  253. // Stop asynchronous RX
  254. furi_hal_serial_async_rx_stop(fhttp.serial_handle);
  255. // Release and deinitialize the serial handle
  256. furi_hal_serial_disable_direction(fhttp.serial_handle, FuriHalSerialDirectionRx);
  257. furi_hal_serial_control_release(fhttp.serial_handle);
  258. furi_hal_serial_deinit(fhttp.serial_handle);
  259. // Signal the worker thread to stop
  260. furi_thread_flags_set(fhttp.rx_thread_id, WorkerEvtStop);
  261. // Wait for the thread to finish
  262. furi_thread_join(fhttp.rx_thread);
  263. // Free the thread resources
  264. furi_thread_free(fhttp.rx_thread);
  265. // Free the stream buffer
  266. furi_stream_buffer_free(fhttp.flipper_http_stream);
  267. // Free the timer
  268. if (fhttp.get_timeout_timer)
  269. {
  270. furi_timer_free(fhttp.get_timeout_timer);
  271. fhttp.get_timeout_timer = NULL;
  272. }
  273. // Free received data if any
  274. if (fhttp.received_data)
  275. {
  276. free(fhttp.received_data);
  277. fhttp.received_data = NULL;
  278. }
  279. FURI_LOG_I("FlipperHTTP", "UART deinitialized successfully.");
  280. }
  281. // Function to send data over UART with newline termination
  282. /**
  283. * @brief Send data over UART with newline termination.
  284. * @return true if the data was sent successfully, false otherwise.
  285. * @param data The data to send over UART.
  286. * @note The data will be sent over UART with a newline character appended.
  287. */
  288. bool flipper_http_send_data(const char *data)
  289. {
  290. size_t data_length = strlen(data);
  291. if (data_length == 0)
  292. {
  293. FURI_LOG_E("FlipperHTTP", "Attempted to send empty data.");
  294. return false;
  295. }
  296. // Create a buffer with data + '\n'
  297. size_t send_length = data_length + 1; // +1 for '\n'
  298. if (send_length > 256)
  299. { // Ensure buffer size is sufficient
  300. FURI_LOG_E("FlipperHTTP", "Data too long to send over FHTTP.");
  301. return false;
  302. }
  303. char send_buffer[257]; // 256 + 1 for safety
  304. strncpy(send_buffer, data, 256);
  305. send_buffer[data_length] = '\n'; // Append newline
  306. send_buffer[data_length + 1] = '\0'; // Null-terminate
  307. if (fhttp.state == INACTIVE && ((strstr(send_buffer, "[PING]") == NULL) && (strstr(send_buffer, "[WIFI/CONNECT]") == NULL)))
  308. {
  309. FURI_LOG_E("FlipperHTTP", "Cannot send data while INACTIVE.");
  310. fhttp.last_response = "Cannot send data while INACTIVE.";
  311. return false;
  312. }
  313. fhttp.state = SENDING;
  314. furi_hal_serial_tx(fhttp.serial_handle, (const uint8_t *)send_buffer, send_length);
  315. // Uncomment below line to log the data sent over UART
  316. // FURI_LOG_I("FlipperHTTP", "Sent data over UART: %s", send_buffer);
  317. fhttp.state = IDLE;
  318. return true;
  319. }
  320. // Function to send a PING request
  321. /**
  322. * @brief Send a GET request to the specified URL.
  323. * @return true if the request was successful, false otherwise.
  324. * @param url The URL to send the GET request to.
  325. * @note The received data will be handled asynchronously via the callback.
  326. * @note This is best used to check if the Wifi Dev Board is connected.
  327. * @note The state will remain INACTIVE until a PONG is received.
  328. */
  329. bool flipper_http_ping()
  330. {
  331. const char *command = "[PING]";
  332. if (!flipper_http_send_data(command))
  333. {
  334. FURI_LOG_E("FlipperHTTP", "Failed to send PING command.");
  335. return false;
  336. }
  337. // set state as INACTIVE to be made IDLE if PONG is received
  338. fhttp.state = INACTIVE;
  339. // The response will be handled asynchronously via the callback
  340. return true;
  341. }
  342. // Function to save WiFi settings (returns true if successful)
  343. /**
  344. * @brief Send a command to save WiFi settings.
  345. * @return true if the request was successful, false otherwise.
  346. * @note The received data will be handled asynchronously via the callback.
  347. */
  348. bool flipper_http_save_wifi(const char *ssid, const char *password)
  349. {
  350. if (!ssid || !password)
  351. {
  352. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_save_wifi.");
  353. return false;
  354. }
  355. char buffer[256];
  356. int ret = snprintf(buffer, sizeof(buffer), "[WIFI/SAVE]{\"ssid\":\"%s\",\"password\":\"%s\"}", ssid, password);
  357. if (ret < 0 || ret >= (int)sizeof(buffer))
  358. {
  359. FURI_LOG_E("FlipperHTTP", "Failed to format WiFi save command.");
  360. return false;
  361. }
  362. if (!flipper_http_send_data(buffer))
  363. {
  364. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi save command.");
  365. return false;
  366. }
  367. // The response will be handled asynchronously via the callback
  368. return true;
  369. }
  370. // Function to disconnect from WiFi (returns true if successful)
  371. /**
  372. * @brief Send a command to disconnect from WiFi.
  373. * @return true if the request was successful, false otherwise.
  374. * @note The received data will be handled asynchronously via the callback.
  375. */
  376. bool flipper_http_disconnect_wifi()
  377. {
  378. const char *command = "[WIFI/DISCONNECT]";
  379. if (!flipper_http_send_data(command))
  380. {
  381. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi disconnect command.");
  382. return false;
  383. }
  384. // The response will be handled asynchronously via the callback
  385. return true;
  386. }
  387. // Function to connect to WiFi (returns true if successful)
  388. /**
  389. * @brief Send a command to connect to WiFi.
  390. * @return true if the request was successful, false otherwise.
  391. * @note The received data will be handled asynchronously via the callback.
  392. */
  393. bool flipper_http_connect_wifi()
  394. {
  395. const char *command = "[WIFI/CONNECT]";
  396. if (!flipper_http_send_data(command))
  397. {
  398. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi connect command.");
  399. return false;
  400. }
  401. // The response will be handled asynchronously via the callback
  402. return true;
  403. }
  404. // Function to send a GET request
  405. /**
  406. * @brief Send a GET request to the specified URL.
  407. * @return true if the request was successful, false otherwise.
  408. * @param url The URL to send the GET request to.
  409. * @note The received data will be handled asynchronously via the callback.
  410. */
  411. bool flipper_http_get_request(const char *url)
  412. {
  413. if (!url)
  414. {
  415. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_get_request.");
  416. return false;
  417. }
  418. // Prepare GET request command
  419. char command[256];
  420. int ret = snprintf(command, sizeof(command), "[GET]%s", url);
  421. if (ret < 0 || ret >= (int)sizeof(command))
  422. {
  423. FURI_LOG_E("FlipperHTTP", "Failed to format GET request command.");
  424. return false;
  425. }
  426. // Send GET request via UART
  427. if (!flipper_http_send_data(command))
  428. {
  429. FURI_LOG_E("FlipperHTTP", "Failed to send GET request command.");
  430. return false;
  431. }
  432. // The response will be handled asynchronously via the callback
  433. return true;
  434. }
  435. // Function to handle received data asynchronously
  436. /**
  437. * @brief Callback function to handle received data asynchronously.
  438. * @return void
  439. * @param line The received line.
  440. * @param context The context passed to the callback.
  441. * @note The received data will be handled asynchronously via the callback and handles the state of the UART.
  442. */
  443. void flipper_http_rx_callback(const char *line, void *context)
  444. {
  445. if (!line || !context)
  446. {
  447. FURI_LOG_E(HTTP_TAG, "Invalid arguments provided to flipper_http_rx_callback.");
  448. return;
  449. }
  450. fhttp.last_response = (char *)line;
  451. // the only way for the state to change from INACTIVE to RECEIVING is if a PONG is received
  452. if (fhttp.state != INACTIVE)
  453. {
  454. fhttp.state = RECEIVING;
  455. }
  456. // Uncomment below line to log the data received over UART
  457. // FURI_LOG_I(HTTP_TAG, "Received UART line: %s", line);
  458. // Check if we've started receiving data from a GET request
  459. if (fhttp.started_receiving)
  460. {
  461. // Restart the timeout timer each time new data is received
  462. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  463. if (strstr(line, "[GET/END]") != NULL)
  464. {
  465. FURI_LOG_I(HTTP_TAG, "GET request completed.");
  466. // Stop the timer since we've completed the GET request
  467. furi_timer_stop(fhttp.get_timeout_timer);
  468. if (fhttp.received_data)
  469. {
  470. flipper_http_save_received_data(strlen(fhttp.received_data), fhttp.received_data);
  471. free(fhttp.received_data);
  472. fhttp.received_data = NULL;
  473. fhttp.started_receiving = false;
  474. fhttp.just_started = false;
  475. fhttp.state = IDLE;
  476. return;
  477. }
  478. else
  479. {
  480. FURI_LOG_E(HTTP_TAG, "No data received.");
  481. fhttp.started_receiving = false;
  482. fhttp.just_started = false;
  483. fhttp.state = IDLE;
  484. return;
  485. }
  486. }
  487. // Append the new line to the existing data
  488. if (fhttp.received_data == NULL)
  489. {
  490. fhttp.received_data = (char *)malloc(strlen(line) + 2); // +2 for newline and null terminator
  491. if (fhttp.received_data)
  492. {
  493. strcpy(fhttp.received_data, line);
  494. fhttp.received_data[strlen(line)] = '\n'; // Add newline
  495. fhttp.received_data[strlen(line) + 1] = '\0'; // Null terminator
  496. }
  497. }
  498. else
  499. {
  500. size_t current_len = strlen(fhttp.received_data);
  501. size_t new_size = current_len + strlen(line) + 2; // +2 for newline and null terminator
  502. fhttp.received_data = (char *)realloc(fhttp.received_data, new_size);
  503. if (fhttp.received_data)
  504. {
  505. memcpy(fhttp.received_data + current_len, line, strlen(line)); // Copy line at the end of the current data
  506. fhttp.received_data[current_len + strlen(line)] = '\n'; // Add newline
  507. fhttp.received_data[current_len + strlen(line) + 1] = '\0'; // Null terminator
  508. }
  509. }
  510. if (!fhttp.just_started)
  511. {
  512. fhttp.just_started = true;
  513. }
  514. return;
  515. }
  516. // Handle different types of responses
  517. if (strstr(line, "[SUCCESS]") != NULL || strstr(line, "[CONNECTED]") != NULL)
  518. {
  519. FURI_LOG_I(HTTP_TAG, "Operation succeeded.");
  520. }
  521. else if (strstr(line, "[INFO]") != NULL)
  522. {
  523. FURI_LOG_I(HTTP_TAG, "Received info: %s", line);
  524. if (fhttp.state == INACTIVE && strstr(line, "[INFO] Already connected to Wifi.") != NULL)
  525. {
  526. fhttp.state = IDLE;
  527. }
  528. }
  529. else if (strstr(line, "[GET/SUCCESS]") != NULL)
  530. {
  531. FURI_LOG_I(HTTP_TAG, "GET request succeeded.");
  532. fhttp.started_receiving = true;
  533. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  534. fhttp.state = RECEIVING;
  535. return;
  536. }
  537. else if (strstr(line, "[DISCONNECTED]") != NULL)
  538. {
  539. FURI_LOG_I(HTTP_TAG, "WiFi disconnected successfully.");
  540. }
  541. else if (strstr(line, "[ERROR]") != NULL)
  542. {
  543. FURI_LOG_E(HTTP_TAG, "Received error: %s", line);
  544. fhttp.state = ISSUE;
  545. return;
  546. }
  547. else if (strstr(line, "[PONG]") != NULL)
  548. {
  549. FURI_LOG_I(HTTP_TAG, "Received PONG response: Wifi Dev Board is still alive.");
  550. // send command to connect to WiFi
  551. if (fhttp.state == INACTIVE)
  552. {
  553. fhttp.state = IDLE;
  554. return;
  555. }
  556. }
  557. if (fhttp.state == INACTIVE && strstr(line, "[PONG]") != NULL)
  558. {
  559. fhttp.state = IDLE;
  560. }
  561. else if (fhttp.state == INACTIVE && strstr(line, "[PONG]") == NULL)
  562. {
  563. fhttp.state = INACTIVE;
  564. }
  565. else
  566. {
  567. fhttp.state = IDLE;
  568. }
  569. }
  570. // Function to save received data to a file
  571. /**
  572. * @brief Save the received data to a file.
  573. * @return true if the data was saved successfully, false otherwise.
  574. * @param bytes_received The number of bytes received.
  575. * @param line_buffer The buffer containing the received data.
  576. * @note The data will be saved to a file in the STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag "/received_data.txt" directory.
  577. */
  578. bool flipper_http_save_received_data(size_t bytes_received, const char line_buffer[])
  579. {
  580. const char *output_file_path = STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag "/received_data.txt";
  581. // Ensure the directory exists
  582. char directory_path[128];
  583. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag);
  584. Storage *_storage = NULL;
  585. File *_file = NULL;
  586. // Open the storage if not opened already
  587. // Initialize storage and create the directory if it doesn't exist
  588. _storage = furi_record_open(RECORD_STORAGE);
  589. storage_common_mkdir(_storage, directory_path); // Create directory if it doesn't exist
  590. _file = storage_file_alloc(_storage);
  591. // Open file for writing and append data line by line
  592. if (!storage_file_open(_file, output_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  593. {
  594. FURI_LOG_E(HTTP_TAG, "Failed to open output file for writing.");
  595. storage_file_free(_file);
  596. furi_record_close(RECORD_STORAGE);
  597. return false;
  598. }
  599. // Write each line received from the UART to the file
  600. if (bytes_received > 0 && _file)
  601. {
  602. storage_file_write(_file, line_buffer, bytes_received);
  603. storage_file_write(_file, "\n", 1); // Add a newline after each line
  604. }
  605. else
  606. {
  607. FURI_LOG_E(HTTP_TAG, "No data received.");
  608. return false;
  609. }
  610. if (_file)
  611. {
  612. storage_file_close(_file);
  613. storage_file_free(_file);
  614. _file = NULL;
  615. }
  616. if (_storage)
  617. {
  618. furi_record_close(RECORD_STORAGE);
  619. _storage = NULL;
  620. }
  621. return true;
  622. }
  623. #endif // FLIPPER_HTTP_H