flipper_http.h 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. // flipper_http.h - Flipper HTTP Library (www.github.com/jblanked)
  2. // Author: JBlanked
  3. #ifndef FLIPPER_HTTP_H
  4. #define FLIPPER_HTTP_H
  5. #include <furi.h>
  6. #include <furi_hal.h>
  7. #include <furi_hal_gpio.h>
  8. #include <furi_hal_serial.h>
  9. #include <storage/storage.h>
  10. #include <stdlib.h>
  11. // STORAGE_EXT_PATH_PREFIX is defined in the Furi SDK as /ext
  12. #define HTTP_TAG "FlipStore" // change this to your app name
  13. #define http_tag "flip_store" // change this to your app id
  14. #define UART_CH (FuriHalSerialIdUsart) // UART channel
  15. #define TIMEOUT_DURATION_TICKS (2 * 1000) // 2 seconds
  16. #define BAUDRATE (115200) // UART baudrate
  17. #define RX_BUF_SIZE 1024 // UART RX buffer size
  18. #define RX_LINE_BUFFER_SIZE 10000 // UART RX line buffer size (increase for large responses)
  19. // Forward declaration for callback
  20. typedef void (*FlipperHTTP_Callback)(const char *line, void *context);
  21. // Functions
  22. bool flipper_http_init(FlipperHTTP_Callback callback, void *context);
  23. void flipper_http_deinit();
  24. //---
  25. void flipper_http_rx_callback(const char *line, void *context);
  26. bool flipper_http_send_data(const char *data);
  27. //---
  28. bool flipper_http_connect_wifi();
  29. bool flipper_http_disconnect_wifi();
  30. bool flipper_http_ping();
  31. bool flipper_http_scan_wifi();
  32. bool flipper_http_save_wifi(const char *ssid, const char *password);
  33. //---
  34. bool flipper_http_get_request(const char *url);
  35. bool flipper_http_get_request_with_headers(const char *url, const char *headers);
  36. bool flipper_http_post_request_with_headers(const char *url, const char *headers, const char *payload);
  37. bool flipper_http_put_request_with_headers(const char *url, const char *headers, const char *payload);
  38. bool flipper_http_delete_request_with_headers(const char *url, const char *headers, const char *payload);
  39. //---
  40. bool flipper_http_get_request_bytes(const char *url, const char *headers);
  41. bool flipper_http_post_request_bytes(const char *url, const char *headers, const char *payload);
  42. //---
  43. bool flipper_http_save_received_data(size_t bytes_received, const char line_buffer[]);
  44. char *trim(const char *str);
  45. // State variable to track the UART state
  46. typedef enum
  47. {
  48. INACTIVE, // Inactive state
  49. IDLE, // Default state
  50. RECEIVING, // Receiving data
  51. SENDING, // Sending data
  52. ISSUE, // Issue with connection
  53. } SerialState;
  54. // Event Flags for UART Worker Thread
  55. typedef enum
  56. {
  57. WorkerEvtStop = (1 << 0),
  58. WorkerEvtRxDone = (1 << 1),
  59. } WorkerEvtFlags;
  60. // FlipperHTTP Structure
  61. typedef struct
  62. {
  63. FuriStreamBuffer *flipper_http_stream; // Stream buffer for UART communication
  64. FuriHalSerialHandle *serial_handle; // Serial handle for UART communication
  65. FuriThread *rx_thread; // Worker thread for UART
  66. uint8_t rx_buf[RX_BUF_SIZE]; // Buffer for received data
  67. FuriThreadId rx_thread_id; // Worker thread ID
  68. FlipperHTTP_Callback handle_rx_line_cb; // Callback for received lines
  69. void *callback_context; // Context for the callback
  70. SerialState state; // State of the UART
  71. // variable to store the last received data from the UART
  72. char *last_response;
  73. // Timer-related members
  74. FuriTimer *get_timeout_timer; // Timer for HTTP request timeout
  75. char *received_data; // Buffer to store received data
  76. bool started_receiving_get; // Indicates if a GET request has started
  77. bool just_started_get; // Indicates if GET data reception has just started
  78. bool started_receiving_post; // Indicates if a POST request has started
  79. bool just_started_post; // Indicates if POST data reception has just started
  80. bool started_receiving_put; // Indicates if a PUT request has started
  81. bool just_started_put; // Indicates if PUT data reception has just started
  82. bool started_receiving_delete; // Indicates if a DELETE request has started
  83. bool just_started_delete; // Indicates if DELETE data reception has just started
  84. // Buffer to hold the raw bytes received from the UART
  85. uint8_t *received_bytes;
  86. size_t received_bytes_len; // Length of the received bytes
  87. // File path to save the bytes received
  88. char file_path[256];
  89. bool save_data; // Flag to save the received data
  90. } FlipperHTTP;
  91. FlipperHTTP fhttp;
  92. // Function to append received data to file
  93. bool append_to_file(const char *file_path, const void *data, size_t data_size)
  94. {
  95. Storage *storage = furi_record_open(RECORD_STORAGE);
  96. File *file = storage_file_alloc(storage);
  97. // Open the file in append mode
  98. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_OPEN_APPEND))
  99. {
  100. FURI_LOG_E(HTTP_TAG, "Failed to open file for appending: %s", file_path);
  101. storage_file_free(file);
  102. furi_record_close(RECORD_STORAGE);
  103. return false;
  104. }
  105. // Write the data to the file
  106. if (storage_file_write(file, data, data_size) != data_size)
  107. {
  108. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  109. storage_file_close(file);
  110. storage_file_free(file);
  111. furi_record_close(RECORD_STORAGE);
  112. return false;
  113. }
  114. storage_file_close(file);
  115. storage_file_free(file);
  116. furi_record_close(RECORD_STORAGE);
  117. return true;
  118. }
  119. // UART worker thread
  120. /**
  121. * @brief Worker thread to handle UART data asynchronously.
  122. * @return 0
  123. * @param context The context to pass to the callback.
  124. * @note This function will handle received data asynchronously via the callback.
  125. */
  126. static int32_t flipper_http_worker(void *context)
  127. {
  128. UNUSED(context);
  129. size_t rx_line_pos = 0;
  130. char *rx_line_buffer = (char *)malloc(RX_LINE_BUFFER_SIZE);
  131. if (!rx_line_buffer)
  132. {
  133. // Handle malloc failure
  134. FURI_LOG_E(HTTP_TAG, "Failed to allocate memory for rx_line_buffer");
  135. return -1;
  136. }
  137. // Create the file path if not already set
  138. // snprintf(fhttp.file_path, sizeof(fhttp.file_path), STORAGE_EXT_PATH_PREFIX "/apps/http_received_data.fap");
  139. while (1)
  140. {
  141. uint32_t events = furi_thread_flags_wait(WorkerEvtStop | WorkerEvtRxDone, FuriFlagWaitAny, FuriWaitForever);
  142. if (events & WorkerEvtStop)
  143. break;
  144. if (events & WorkerEvtRxDone)
  145. {
  146. size_t len = furi_stream_buffer_receive(fhttp.flipper_http_stream, fhttp.rx_buf, RX_BUF_SIZE, 0);
  147. // Append each received byte chunk to the file
  148. if (fhttp.save_data && !append_to_file(fhttp.file_path, fhttp.rx_buf, len))
  149. {
  150. FURI_LOG_E(HTTP_TAG, "Failed to append received data to file");
  151. }
  152. for (size_t i = 0; i < len; i++)
  153. {
  154. char c = fhttp.rx_buf[i]; // Raw byte received
  155. if (c == '\n' || rx_line_pos >= RX_LINE_BUFFER_SIZE - 1)
  156. {
  157. rx_line_buffer[rx_line_pos] = '\0'; // Null-terminate the line
  158. // Invoke the callback with the complete line
  159. if (fhttp.handle_rx_line_cb)
  160. {
  161. fhttp.handle_rx_line_cb(rx_line_buffer, fhttp.callback_context);
  162. }
  163. // Reset the line buffer position
  164. rx_line_pos = 0;
  165. }
  166. else
  167. {
  168. rx_line_buffer[rx_line_pos++] = c; // Add character to the line buffer
  169. }
  170. }
  171. }
  172. }
  173. // Free the allocated memory before exiting the thread
  174. free(rx_line_buffer);
  175. return 0;
  176. }
  177. // Timer callback function
  178. /**
  179. * @brief Callback function for the GET timeout timer.
  180. * @return 0
  181. * @param context The context to pass to the callback.
  182. * @note This function will be called when the GET request times out.
  183. */
  184. void get_timeout_timer_callback(void *context)
  185. {
  186. UNUSED(context);
  187. FURI_LOG_E(HTTP_TAG, "Timeout reached: 2 seconds without receiving the end.");
  188. // Reset the state
  189. fhttp.started_receiving_get = false;
  190. fhttp.started_receiving_post = false;
  191. fhttp.started_receiving_put = false;
  192. fhttp.started_receiving_delete = false;
  193. // Free received data if any
  194. if (fhttp.received_data)
  195. {
  196. free(fhttp.received_data);
  197. fhttp.received_data = NULL;
  198. }
  199. // Update UART state
  200. fhttp.state = ISSUE;
  201. }
  202. // UART RX Handler Callback (Interrupt Context)
  203. /**
  204. * @brief A private callback function to handle received data asynchronously.
  205. * @return void
  206. * @param handle The UART handle.
  207. * @param event The event type.
  208. * @param context The context to pass to the callback.
  209. * @note This function will handle received data asynchronously via the callback.
  210. */
  211. static void _flipper_http_rx_callback(FuriHalSerialHandle *handle, FuriHalSerialRxEvent event, void *context)
  212. {
  213. UNUSED(context);
  214. if (event == FuriHalSerialRxEventData)
  215. {
  216. uint8_t data = furi_hal_serial_async_rx(handle);
  217. furi_stream_buffer_send(fhttp.flipper_http_stream, &data, 1, 0);
  218. furi_thread_flags_set(fhttp.rx_thread_id, WorkerEvtRxDone);
  219. }
  220. }
  221. // UART initialization function
  222. /**
  223. * @brief Initialize UART.
  224. * @return true if the UART was initialized successfully, false otherwise.
  225. * @param callback The callback function to handle received data (ex. flipper_http_rx_callback).
  226. * @param context The context to pass to the callback.
  227. * @note The received data will be handled asynchronously via the callback.
  228. */
  229. bool flipper_http_init(FlipperHTTP_Callback callback, void *context)
  230. {
  231. if (!context)
  232. {
  233. FURI_LOG_E(HTTP_TAG, "Invalid context provided to flipper_http_init.");
  234. return false;
  235. }
  236. if (!callback)
  237. {
  238. FURI_LOG_E(HTTP_TAG, "Invalid callback provided to flipper_http_init.");
  239. return false;
  240. }
  241. fhttp.flipper_http_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
  242. if (!fhttp.flipper_http_stream)
  243. {
  244. FURI_LOG_E(HTTP_TAG, "Failed to allocate UART stream buffer.");
  245. return false;
  246. }
  247. fhttp.rx_thread = furi_thread_alloc();
  248. if (!fhttp.rx_thread)
  249. {
  250. FURI_LOG_E(HTTP_TAG, "Failed to allocate UART thread.");
  251. furi_stream_buffer_free(fhttp.flipper_http_stream);
  252. return false;
  253. }
  254. furi_thread_set_name(fhttp.rx_thread, "FlipperHTTP_RxThread");
  255. furi_thread_set_stack_size(fhttp.rx_thread, 1024);
  256. furi_thread_set_context(fhttp.rx_thread, &fhttp);
  257. furi_thread_set_callback(fhttp.rx_thread, flipper_http_worker);
  258. fhttp.handle_rx_line_cb = callback;
  259. fhttp.callback_context = context;
  260. furi_thread_start(fhttp.rx_thread);
  261. fhttp.rx_thread_id = furi_thread_get_id(fhttp.rx_thread);
  262. // handle when the UART control is busy to avoid furi_check failed
  263. if (furi_hal_serial_control_is_busy(UART_CH))
  264. {
  265. FURI_LOG_E(HTTP_TAG, "UART control is busy.");
  266. return false;
  267. }
  268. fhttp.serial_handle = furi_hal_serial_control_acquire(UART_CH);
  269. if (!fhttp.serial_handle)
  270. {
  271. FURI_LOG_E(HTTP_TAG, "Failed to acquire UART control - handle is NULL");
  272. // Cleanup resources
  273. furi_thread_free(fhttp.rx_thread);
  274. furi_stream_buffer_free(fhttp.flipper_http_stream);
  275. return false;
  276. }
  277. // Initialize UART with acquired handle
  278. furi_hal_serial_init(fhttp.serial_handle, BAUDRATE);
  279. // Enable RX direction
  280. furi_hal_serial_enable_direction(fhttp.serial_handle, FuriHalSerialDirectionRx);
  281. // Start asynchronous RX with the callback
  282. furi_hal_serial_async_rx_start(fhttp.serial_handle, _flipper_http_rx_callback, &fhttp, false);
  283. // Wait for the TX to complete to ensure UART is ready
  284. furi_hal_serial_tx_wait_complete(fhttp.serial_handle);
  285. // Allocate the timer for handling timeouts
  286. fhttp.get_timeout_timer = furi_timer_alloc(
  287. get_timeout_timer_callback, // Callback function
  288. FuriTimerTypeOnce, // One-shot timer
  289. &fhttp // Context passed to callback
  290. );
  291. if (!fhttp.get_timeout_timer)
  292. {
  293. FURI_LOG_E(HTTP_TAG, "Failed to allocate HTTP request timeout timer.");
  294. // Cleanup resources
  295. furi_hal_serial_async_rx_stop(fhttp.serial_handle);
  296. furi_hal_serial_disable_direction(fhttp.serial_handle, FuriHalSerialDirectionRx);
  297. furi_hal_serial_control_release(fhttp.serial_handle);
  298. furi_hal_serial_deinit(fhttp.serial_handle);
  299. furi_thread_flags_set(fhttp.rx_thread_id, WorkerEvtStop);
  300. furi_thread_join(fhttp.rx_thread);
  301. furi_thread_free(fhttp.rx_thread);
  302. furi_stream_buffer_free(fhttp.flipper_http_stream);
  303. return false;
  304. }
  305. // Set the timer thread priority if needed
  306. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  307. fhttp.file_path[0] = '\0'; // Null-terminate the file path
  308. fhttp.received_data = NULL;
  309. fhttp.received_bytes = NULL;
  310. fhttp.received_bytes_len = 0;
  311. return true;
  312. }
  313. // Deinitialize UART
  314. /**
  315. * @brief Deinitialize UART.
  316. * @return void
  317. * @note This function will stop the asynchronous RX, release the serial handle, and free the resources.
  318. */
  319. void flipper_http_deinit()
  320. {
  321. if (fhttp.serial_handle == NULL)
  322. {
  323. FURI_LOG_E(HTTP_TAG, "UART handle is NULL. Already deinitialized?");
  324. return;
  325. }
  326. // Stop asynchronous RX
  327. furi_hal_serial_async_rx_stop(fhttp.serial_handle);
  328. // Release and deinitialize the serial handle
  329. furi_hal_serial_disable_direction(fhttp.serial_handle, FuriHalSerialDirectionRx);
  330. furi_hal_serial_control_release(fhttp.serial_handle);
  331. furi_hal_serial_deinit(fhttp.serial_handle);
  332. // Signal the worker thread to stop
  333. furi_thread_flags_set(fhttp.rx_thread_id, WorkerEvtStop);
  334. // Wait for the thread to finish
  335. furi_thread_join(fhttp.rx_thread);
  336. // Free the thread resources
  337. furi_thread_free(fhttp.rx_thread);
  338. // Free the stream buffer
  339. furi_stream_buffer_free(fhttp.flipper_http_stream);
  340. // Free the timer
  341. if (fhttp.get_timeout_timer)
  342. {
  343. furi_timer_free(fhttp.get_timeout_timer);
  344. fhttp.get_timeout_timer = NULL;
  345. }
  346. // Free received data if any
  347. if (fhttp.received_data)
  348. {
  349. free(fhttp.received_data);
  350. fhttp.received_data = NULL;
  351. }
  352. // Free the last response
  353. if (fhttp.last_response)
  354. {
  355. free(fhttp.last_response);
  356. fhttp.last_response = NULL;
  357. }
  358. // FURI_LOG_I("FlipperHTTP", "UART deinitialized successfully.");
  359. }
  360. // Function to send data over UART with newline termination
  361. /**
  362. * @brief Send data over UART with newline termination.
  363. * @return true if the data was sent successfully, false otherwise.
  364. * @param data The data to send over UART.
  365. * @note The data will be sent over UART with a newline character appended.
  366. */
  367. bool flipper_http_send_data(const char *data)
  368. {
  369. size_t data_length = strlen(data);
  370. if (data_length == 0)
  371. {
  372. FURI_LOG_E("FlipperHTTP", "Attempted to send empty data.");
  373. return false;
  374. }
  375. // Create a buffer with data + '\n'
  376. size_t send_length = data_length + 1; // +1 for '\n'
  377. if (send_length > 256)
  378. { // Ensure buffer size is sufficient
  379. FURI_LOG_E("FlipperHTTP", "Data too long to send over FHTTP.");
  380. return false;
  381. }
  382. char send_buffer[257]; // 256 + 1 for safety
  383. strncpy(send_buffer, data, 256);
  384. send_buffer[data_length] = '\n'; // Append newline
  385. send_buffer[data_length + 1] = '\0'; // Null-terminate
  386. if (fhttp.state == INACTIVE && ((strstr(send_buffer, "[PING]") == NULL) && (strstr(send_buffer, "[WIFI/CONNECT]") == NULL)))
  387. {
  388. FURI_LOG_E("FlipperHTTP", "Cannot send data while INACTIVE.");
  389. fhttp.last_response = "Cannot send data while INACTIVE.";
  390. return false;
  391. }
  392. fhttp.state = SENDING;
  393. furi_hal_serial_tx(fhttp.serial_handle, (const uint8_t *)send_buffer, send_length);
  394. // Uncomment below line to log the data sent over UART
  395. // FURI_LOG_I("FlipperHTTP", "Sent data over UART: %s", send_buffer);
  396. fhttp.state = IDLE;
  397. return true;
  398. }
  399. // Function to send a PING request
  400. /**
  401. * @brief Send a PING request to check the connection status.
  402. * @return true if the request was successful, false otherwise.
  403. * @note The received data will be handled asynchronously via the callback.
  404. * @note This is best used to check if the Wifi Dev Board is connected.
  405. * @note The state will remain INACTIVE until a PONG is received.
  406. */
  407. bool flipper_http_ping()
  408. {
  409. const char *command = "[PING]";
  410. if (!flipper_http_send_data(command))
  411. {
  412. FURI_LOG_E("FlipperHTTP", "Failed to send PING command.");
  413. return false;
  414. }
  415. // set state as INACTIVE to be made IDLE if PONG is received
  416. fhttp.state = INACTIVE;
  417. // The response will be handled asynchronously via the callback
  418. return true;
  419. }
  420. // Function to scan for WiFi networks
  421. /**
  422. * @brief Send a command to scan for WiFi networks.
  423. * @return true if the request was successful, false otherwise.
  424. * @note The received data will be handled asynchronously via the callback.
  425. */
  426. bool flipper_http_scan_wifi()
  427. {
  428. const char *command = "[WIFI/SCAN]";
  429. if (!flipper_http_send_data(command))
  430. {
  431. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi scan command.");
  432. return false;
  433. }
  434. // The response will be handled asynchronously via the callback
  435. return true;
  436. }
  437. // Function to save WiFi settings (returns true if successful)
  438. /**
  439. * @brief Send a command to save WiFi settings.
  440. * @return true if the request was successful, false otherwise.
  441. * @note The received data will be handled asynchronously via the callback.
  442. */
  443. bool flipper_http_save_wifi(const char *ssid, const char *password)
  444. {
  445. if (!ssid || !password)
  446. {
  447. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_save_wifi.");
  448. return false;
  449. }
  450. char buffer[256];
  451. int ret = snprintf(buffer, sizeof(buffer), "[WIFI/SAVE]{\"ssid\":\"%s\",\"password\":\"%s\"}", ssid, password);
  452. if (ret < 0 || ret >= (int)sizeof(buffer))
  453. {
  454. FURI_LOG_E("FlipperHTTP", "Failed to format WiFi save command.");
  455. return false;
  456. }
  457. if (!flipper_http_send_data(buffer))
  458. {
  459. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi save command.");
  460. return false;
  461. }
  462. // The response will be handled asynchronously via the callback
  463. return true;
  464. }
  465. // Function to disconnect from WiFi (returns true if successful)
  466. /**
  467. * @brief Send a command to disconnect from WiFi.
  468. * @return true if the request was successful, false otherwise.
  469. * @note The received data will be handled asynchronously via the callback.
  470. */
  471. bool flipper_http_disconnect_wifi()
  472. {
  473. const char *command = "[WIFI/DISCONNECT]";
  474. if (!flipper_http_send_data(command))
  475. {
  476. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi disconnect command.");
  477. return false;
  478. }
  479. // The response will be handled asynchronously via the callback
  480. return true;
  481. }
  482. // Function to connect to WiFi (returns true if successful)
  483. /**
  484. * @brief Send a command to connect to WiFi.
  485. * @return true if the request was successful, false otherwise.
  486. * @note The received data will be handled asynchronously via the callback.
  487. */
  488. bool flipper_http_connect_wifi()
  489. {
  490. const char *command = "[WIFI/CONNECT]";
  491. if (!flipper_http_send_data(command))
  492. {
  493. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi connect command.");
  494. return false;
  495. }
  496. // The response will be handled asynchronously via the callback
  497. return true;
  498. }
  499. // Function to send a GET request
  500. /**
  501. * @brief Send a GET request to the specified URL.
  502. * @return true if the request was successful, false otherwise.
  503. * @param url The URL to send the GET request to.
  504. * @note The received data will be handled asynchronously via the callback.
  505. */
  506. bool flipper_http_get_request(const char *url)
  507. {
  508. if (!url)
  509. {
  510. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_get_request.");
  511. return false;
  512. }
  513. // Prepare GET request command
  514. char command[256];
  515. int ret = snprintf(command, sizeof(command), "[GET]%s", url);
  516. if (ret < 0 || ret >= (int)sizeof(command))
  517. {
  518. FURI_LOG_E("FlipperHTTP", "Failed to format GET request command.");
  519. return false;
  520. }
  521. // Send GET request via UART
  522. if (!flipper_http_send_data(command))
  523. {
  524. FURI_LOG_E("FlipperHTTP", "Failed to send GET request command.");
  525. return false;
  526. }
  527. // The response will be handled asynchronously via the callback
  528. return true;
  529. }
  530. // Function to send a GET request with headers
  531. /**
  532. * @brief Send a GET request to the specified URL.
  533. * @return true if the request was successful, false otherwise.
  534. * @param url The URL to send the GET request to.
  535. * @param headers The headers to send with the GET request.
  536. * @note The received data will be handled asynchronously via the callback.
  537. */
  538. bool flipper_http_get_request_with_headers(const char *url, const char *headers)
  539. {
  540. if (!url || !headers)
  541. {
  542. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_get_request_with_headers.");
  543. return false;
  544. }
  545. // Prepare GET request command with headers
  546. char command[256];
  547. int ret = snprintf(command, sizeof(command), "[GET/HTTP]{\"url\":\"%s\",\"headers\":%s}", url, headers);
  548. if (ret < 0 || ret >= (int)sizeof(command))
  549. {
  550. FURI_LOG_E("FlipperHTTP", "Failed to format GET request command with headers.");
  551. return false;
  552. }
  553. // Send GET request via UART
  554. if (!flipper_http_send_data(command))
  555. {
  556. FURI_LOG_E("FlipperHTTP", "Failed to send GET request command with headers.");
  557. return false;
  558. }
  559. // The response will be handled asynchronously via the callback
  560. return true;
  561. }
  562. // Function to send a GET request with headers and return bytes
  563. /**
  564. * @brief Send a GET request to the specified URL.
  565. * @return true if the request was successful, false otherwise.
  566. * @param url The URL to send the GET request to.
  567. * @param headers The headers to send with the GET request.
  568. * @note The received data will be handled asynchronously via the callback.
  569. */
  570. bool flipper_http_get_request_bytes(const char *url, const char *headers)
  571. {
  572. if (!url || !headers)
  573. {
  574. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_get_request_bytes.");
  575. return false;
  576. }
  577. // Prepare GET request command with headers
  578. char command[256];
  579. int ret = snprintf(command, sizeof(command), "[GET/BYTES]{\"url\":\"%s\",\"headers\":%s}", url, headers);
  580. if (ret < 0 || ret >= (int)sizeof(command))
  581. {
  582. FURI_LOG_E("FlipperHTTP", "Failed to format GET request command with headers.");
  583. return false;
  584. }
  585. // Send GET request via UART
  586. if (!flipper_http_send_data(command))
  587. {
  588. FURI_LOG_E("FlipperHTTP", "Failed to send GET request command with headers.");
  589. return false;
  590. }
  591. // The response will be handled asynchronously via the callback
  592. return true;
  593. }
  594. // Function to send a POST request with headers
  595. /**
  596. * @brief Send a POST request to the specified URL.
  597. * @return true if the request was successful, false otherwise.
  598. * @param url The URL to send the POST request to.
  599. * @param headers The headers to send with the POST request.
  600. * @param payload The data to send with the POST request.
  601. * @note The received data will be handled asynchronously via the callback.
  602. */
  603. bool flipper_http_post_request_with_headers(const char *url, const char *headers, const char *payload)
  604. {
  605. if (!url || !headers || !payload)
  606. {
  607. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_post_request_with_headers.");
  608. return false;
  609. }
  610. // Prepare POST request command with headers and data
  611. char command[256];
  612. int ret = snprintf(command, sizeof(command), "[POST/HTTP]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}", url, headers, payload);
  613. if (ret < 0 || ret >= (int)sizeof(command))
  614. {
  615. FURI_LOG_E("FlipperHTTP", "Failed to format POST request command with headers and data.");
  616. return false;
  617. }
  618. // Send POST request via UART
  619. if (!flipper_http_send_data(command))
  620. {
  621. FURI_LOG_E("FlipperHTTP", "Failed to send POST request command with headers and data.");
  622. return false;
  623. }
  624. // The response will be handled asynchronously via the callback
  625. return true;
  626. }
  627. // Function to send a POST request with headers and return bytes
  628. /**
  629. * @brief Send a POST request to the specified URL.
  630. * @return true if the request was successful, false otherwise.
  631. * @param url The URL to send the POST request to.
  632. * @param headers The headers to send with the POST request.
  633. * @param payload The data to send with the POST request.
  634. * @note The received data will be handled asynchronously via the callback.
  635. */
  636. bool flipper_http_post_request_bytes(const char *url, const char *headers, const char *payload)
  637. {
  638. if (!url || !headers || !payload)
  639. {
  640. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_post_request_bytes.");
  641. return false;
  642. }
  643. // Prepare POST request command with headers and data
  644. char command[256];
  645. int ret = snprintf(command, sizeof(command), "[POST/BYTES]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}", url, headers, payload);
  646. if (ret < 0 || ret >= (int)sizeof(command))
  647. {
  648. FURI_LOG_E("FlipperHTTP", "Failed to format POST request command with headers and data.");
  649. return false;
  650. }
  651. // Send POST request via UART
  652. if (!flipper_http_send_data(command))
  653. {
  654. FURI_LOG_E("FlipperHTTP", "Failed to send POST request command with headers and data.");
  655. return false;
  656. }
  657. // The response will be handled asynchronously via the callback
  658. return true;
  659. }
  660. // Function to send a PUT request with headers
  661. /**
  662. * @brief Send a PUT request to the specified URL.
  663. * @return true if the request was successful, false otherwise.
  664. * @param url The URL to send the PUT request to.
  665. * @param headers The headers to send with the PUT request.
  666. * @param payload The data to send with the PUT request.
  667. * @note The received data will be handled asynchronously via the callback.
  668. */
  669. bool flipper_http_put_request_with_headers(const char *url, const char *headers, const char *payload)
  670. {
  671. if (!url || !headers || !payload)
  672. {
  673. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_put_request_with_headers.");
  674. return false;
  675. }
  676. // Prepare PUT request command with headers and data
  677. char command[256];
  678. int ret = snprintf(command, sizeof(command), "[PUT/HTTP]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}", url, headers, payload);
  679. if (ret < 0 || ret >= (int)sizeof(command))
  680. {
  681. FURI_LOG_E("FlipperHTTP", "Failed to format PUT request command with headers and data.");
  682. return false;
  683. }
  684. // Send PUT request via UART
  685. if (!flipper_http_send_data(command))
  686. {
  687. FURI_LOG_E("FlipperHTTP", "Failed to send PUT request command with headers and data.");
  688. return false;
  689. }
  690. // The response will be handled asynchronously via the callback
  691. return true;
  692. }
  693. // Function to send a DELETE request with headers
  694. /**
  695. * @brief Send a DELETE request to the specified URL.
  696. * @return true if the request was successful, false otherwise.
  697. * @param url The URL to send the DELETE request to.
  698. * @param headers The headers to send with the DELETE request.
  699. * @param payload The data to send with the DELETE request.
  700. * @note The received data will be handled asynchronously via the callback.
  701. */
  702. bool flipper_http_delete_request_with_headers(const char *url, const char *headers, const char *payload)
  703. {
  704. if (!url || !headers || !payload)
  705. {
  706. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_delete_request_with_headers.");
  707. return false;
  708. }
  709. // Prepare DELETE request command with headers and data
  710. char command[256];
  711. int ret = snprintf(command, sizeof(command), "[DELETE/HTTP]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}", url, headers, payload);
  712. if (ret < 0 || ret >= (int)sizeof(command))
  713. {
  714. FURI_LOG_E("FlipperHTTP", "Failed to format DELETE request command with headers and data.");
  715. return false;
  716. }
  717. // Send DELETE request via UART
  718. if (!flipper_http_send_data(command))
  719. {
  720. FURI_LOG_E("FlipperHTTP", "Failed to send DELETE request command with headers and data.");
  721. return false;
  722. }
  723. // The response will be handled asynchronously via the callback
  724. return true;
  725. }
  726. // Function to handle received data asynchronously
  727. /**
  728. * @brief Callback function to handle received data asynchronously.
  729. * @return void
  730. * @param line The received line.
  731. * @param context The context passed to the callback.
  732. * @note The received data will be handled asynchronously via the callback and handles the state of the UART.
  733. */
  734. void flipper_http_rx_callback(const char *line, void *context)
  735. {
  736. if (!line || !context)
  737. {
  738. FURI_LOG_E(HTTP_TAG, "Invalid arguments provided to flipper_http_rx_callback.");
  739. return;
  740. }
  741. // Trim the received line to check if it's empty
  742. char *trimmed_line = trim(line);
  743. if (trimmed_line != NULL && trimmed_line[0] != '\0')
  744. {
  745. fhttp.last_response = (char *)line;
  746. }
  747. free(trimmed_line); // Free the allocated memory for trimmed_line
  748. if (fhttp.state != INACTIVE && fhttp.state != ISSUE)
  749. {
  750. fhttp.state = RECEIVING;
  751. }
  752. // Uncomment below line to log the data received over UART
  753. // FURI_LOG_I(HTTP_TAG, "Received UART line: %s", line);
  754. // Check if we've started receiving data from a GET request
  755. if (fhttp.started_receiving_get)
  756. {
  757. // Restart the timeout timer each time new data is received
  758. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  759. if (strstr(line, "[GET/END]") != NULL)
  760. {
  761. FURI_LOG_I(HTTP_TAG, "GET request completed.");
  762. // Stop the timer since we've completed the GET request
  763. furi_timer_stop(fhttp.get_timeout_timer);
  764. if (fhttp.received_data)
  765. {
  766. // uncomment if you want to save the received data to the external storage
  767. // flipper_http_save_received_data(strlen(fhttp.received_data), fhttp.received_data);
  768. fhttp.started_receiving_get = false;
  769. fhttp.just_started_get = false;
  770. fhttp.state = IDLE;
  771. return;
  772. }
  773. else
  774. {
  775. FURI_LOG_E(HTTP_TAG, "No data received.");
  776. fhttp.started_receiving_get = false;
  777. fhttp.just_started_get = false;
  778. fhttp.state = IDLE;
  779. return;
  780. }
  781. }
  782. // Append the new line to the existing data
  783. if (fhttp.received_data == NULL)
  784. {
  785. fhttp.received_data = (char *)malloc(strlen(line) + 2); // +2 for newline and null terminator
  786. if (fhttp.received_data)
  787. {
  788. strcpy(fhttp.received_data, line);
  789. fhttp.received_data[strlen(line)] = '\n'; // Add newline
  790. fhttp.received_data[strlen(line) + 1] = '\0'; // Null terminator
  791. }
  792. }
  793. else
  794. {
  795. size_t current_len = strlen(fhttp.received_data);
  796. size_t new_size = current_len + strlen(line) + 2; // +2 for newline and null terminator
  797. fhttp.received_data = (char *)realloc(fhttp.received_data, new_size);
  798. if (fhttp.received_data)
  799. {
  800. memcpy(fhttp.received_data + current_len, line, strlen(line)); // Copy line at the end of the current data
  801. fhttp.received_data[current_len + strlen(line)] = '\n'; // Add newline
  802. fhttp.received_data[current_len + strlen(line) + 1] = '\0'; // Null terminator
  803. }
  804. }
  805. if (!fhttp.just_started_get)
  806. {
  807. fhttp.just_started_get = true;
  808. }
  809. return;
  810. }
  811. // Check if we've started receiving data from a POST request
  812. else if (fhttp.started_receiving_post)
  813. {
  814. // Restart the timeout timer each time new data is received
  815. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  816. if (strstr(line, "[POST/END]") != NULL)
  817. {
  818. FURI_LOG_I(HTTP_TAG, "POST request completed.");
  819. fhttp.save_data = false;
  820. // Stop the timer since we've completed the POST request
  821. furi_timer_stop(fhttp.get_timeout_timer);
  822. if (fhttp.received_data)
  823. {
  824. // uncomment if you want to save the received data to the external storage
  825. // flipper_http_save_received_data(strlen(fhttp.received_data), fhttp.received_data);
  826. fhttp.started_receiving_post = false;
  827. fhttp.just_started_post = false;
  828. fhttp.state = IDLE;
  829. return;
  830. }
  831. else
  832. {
  833. FURI_LOG_E(HTTP_TAG, "No data received.");
  834. fhttp.started_receiving_post = false;
  835. fhttp.just_started_post = false;
  836. fhttp.state = IDLE;
  837. return;
  838. }
  839. }
  840. // Append the new line to the existing data
  841. if (fhttp.received_data == NULL)
  842. {
  843. fhttp.received_data = (char *)malloc(strlen(line) + 2); // +2 for newline and null terminator
  844. if (fhttp.received_data)
  845. {
  846. strcpy(fhttp.received_data, line);
  847. fhttp.received_data[strlen(line)] = '\n'; // Add newline
  848. fhttp.received_data[strlen(line) + 1] = '\0'; // Null terminator
  849. }
  850. }
  851. else
  852. {
  853. size_t current_len = strlen(fhttp.received_data);
  854. size_t new_size = current_len + strlen(line) + 2; // +2 for newline and null terminator
  855. fhttp.received_data = (char *)realloc(fhttp.received_data, new_size);
  856. if (fhttp.received_data)
  857. {
  858. memcpy(fhttp.received_data + current_len, line, strlen(line)); // Copy line at the end of the current data
  859. fhttp.received_data[current_len + strlen(line)] = '\n'; // Add newline
  860. fhttp.received_data[current_len + strlen(line) + 1] = '\0'; // Null terminator
  861. }
  862. }
  863. if (!fhttp.just_started_post)
  864. {
  865. fhttp.just_started_post = true;
  866. }
  867. return;
  868. }
  869. // Check if we've started receiving data from a PUT request
  870. else if (fhttp.started_receiving_put)
  871. {
  872. // Restart the timeout timer each time new data is received
  873. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  874. if (strstr(line, "[PUT/END]") != NULL)
  875. {
  876. FURI_LOG_I(HTTP_TAG, "PUT request completed.");
  877. // Stop the timer since we've completed the PUT request
  878. furi_timer_stop(fhttp.get_timeout_timer);
  879. if (fhttp.received_data)
  880. {
  881. // uncomment if you want to save the received data to the external storage
  882. // flipper_http_save_received_data(strlen(fhttp.received_data), fhttp.received_data);
  883. fhttp.started_receiving_put = false;
  884. fhttp.just_started_put = false;
  885. fhttp.state = IDLE;
  886. return;
  887. }
  888. else
  889. {
  890. FURI_LOG_E(HTTP_TAG, "No data received.");
  891. fhttp.started_receiving_put = false;
  892. fhttp.just_started_put = false;
  893. fhttp.state = IDLE;
  894. return;
  895. }
  896. }
  897. // Append the new line to the existing data
  898. if (fhttp.received_data == NULL)
  899. {
  900. fhttp.received_data = (char *)malloc(strlen(line) + 2); // +2 for newline and null terminator
  901. if (fhttp.received_data)
  902. {
  903. strcpy(fhttp.received_data, line);
  904. fhttp.received_data[strlen(line)] = '\n'; // Add newline
  905. fhttp.received_data[strlen(line) + 1] = '\0'; // Null terminator
  906. }
  907. }
  908. else
  909. {
  910. size_t current_len = strlen(fhttp.received_data);
  911. size_t new_size = current_len + strlen(line) + 2; // +2 for newline and null terminator
  912. fhttp.received_data = (char *)realloc(fhttp.received_data, new_size);
  913. if (fhttp.received_data)
  914. {
  915. memcpy(fhttp.received_data + current_len, line, strlen(line)); // Copy line at the end of the current data
  916. fhttp.received_data[current_len + strlen(line)] = '\n'; // Add newline
  917. fhttp.received_data[current_len + strlen(line) + 1] = '\0'; // Null terminator
  918. }
  919. }
  920. if (!fhttp.just_started_put)
  921. {
  922. fhttp.just_started_put = true;
  923. }
  924. return;
  925. }
  926. // Check if we've started receiving data from a DELETE request
  927. else if (fhttp.started_receiving_delete)
  928. {
  929. // Restart the timeout timer each time new data is received
  930. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  931. if (strstr(line, "[DELETE/END]") != NULL)
  932. {
  933. FURI_LOG_I(HTTP_TAG, "DELETE request completed.");
  934. // Stop the timer since we've completed the DELETE request
  935. furi_timer_stop(fhttp.get_timeout_timer);
  936. if (fhttp.received_data)
  937. {
  938. // uncomment if you want to save the received data to the external storage
  939. // flipper_http_save_received_data(strlen(fhttp.received_data), fhttp.received_data);
  940. fhttp.started_receiving_delete = false;
  941. fhttp.just_started_delete = false;
  942. fhttp.state = IDLE;
  943. return;
  944. }
  945. else
  946. {
  947. FURI_LOG_E(HTTP_TAG, "No data received.");
  948. fhttp.started_receiving_delete = false;
  949. fhttp.just_started_delete = false;
  950. fhttp.state = IDLE;
  951. return;
  952. }
  953. }
  954. // Append the new line to the existing data
  955. if (fhttp.received_data == NULL)
  956. {
  957. fhttp.received_data = (char *)malloc(strlen(line) + 2); // +2 for newline and null terminator
  958. if (fhttp.received_data)
  959. {
  960. strcpy(fhttp.received_data, line);
  961. fhttp.received_data[strlen(line)] = '\n'; // Add newline
  962. fhttp.received_data[strlen(line) + 1] = '\0'; // Null terminator
  963. }
  964. }
  965. else
  966. {
  967. size_t current_len = strlen(fhttp.received_data);
  968. size_t new_size = current_len + strlen(line) + 2; // +2 for newline and null terminator
  969. fhttp.received_data = (char *)realloc(fhttp.received_data, new_size);
  970. if (fhttp.received_data)
  971. {
  972. memcpy(fhttp.received_data + current_len, line, strlen(line)); // Copy line at the end of the current data
  973. fhttp.received_data[current_len + strlen(line)] = '\n'; // Add newline
  974. fhttp.received_data[current_len + strlen(line) + 1] = '\0'; // Null terminator
  975. }
  976. }
  977. if (!fhttp.just_started_delete)
  978. {
  979. fhttp.just_started_delete = true;
  980. }
  981. return;
  982. }
  983. // Handle different types of responses
  984. if (strstr(line, "[SUCCESS]") != NULL || strstr(line, "[CONNECTED]") != NULL)
  985. {
  986. FURI_LOG_I(HTTP_TAG, "Operation succeeded.");
  987. }
  988. else if (strstr(line, "[INFO]") != NULL)
  989. {
  990. FURI_LOG_I(HTTP_TAG, "Received info: %s", line);
  991. if (fhttp.state == INACTIVE && strstr(line, "[INFO] Already connected to Wifi.") != NULL)
  992. {
  993. fhttp.state = IDLE;
  994. }
  995. }
  996. else if (strstr(line, "[GET/SUCCESS]") != NULL)
  997. {
  998. FURI_LOG_I(HTTP_TAG, "GET request succeeded.");
  999. fhttp.started_receiving_get = true;
  1000. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1001. fhttp.state = RECEIVING;
  1002. fhttp.received_data = NULL;
  1003. return;
  1004. }
  1005. else if (strstr(line, "[POST/SUCCESS]") != NULL)
  1006. {
  1007. FURI_LOG_I(HTTP_TAG, "POST request succeeded.");
  1008. fhttp.started_receiving_post = true;
  1009. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1010. fhttp.state = RECEIVING;
  1011. fhttp.received_data = NULL;
  1012. fhttp.save_data = true;
  1013. return;
  1014. }
  1015. else if (strstr(line, "[PUT/SUCCESS]") != NULL)
  1016. {
  1017. FURI_LOG_I(HTTP_TAG, "PUT request succeeded.");
  1018. fhttp.started_receiving_put = true;
  1019. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1020. fhttp.state = RECEIVING;
  1021. fhttp.received_data = NULL;
  1022. return;
  1023. }
  1024. else if (strstr(line, "[DELETE/SUCCESS]") != NULL)
  1025. {
  1026. FURI_LOG_I(HTTP_TAG, "DELETE request succeeded.");
  1027. fhttp.started_receiving_delete = true;
  1028. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1029. fhttp.state = RECEIVING;
  1030. fhttp.received_data = NULL;
  1031. return;
  1032. }
  1033. else if (strstr(line, "[DISCONNECTED]") != NULL)
  1034. {
  1035. FURI_LOG_I(HTTP_TAG, "WiFi disconnected successfully.");
  1036. }
  1037. else if (strstr(line, "[ERROR]") != NULL)
  1038. {
  1039. FURI_LOG_E(HTTP_TAG, "Received error: %s", line);
  1040. fhttp.state = ISSUE;
  1041. return;
  1042. }
  1043. else if (strstr(line, "[PONG]") != NULL)
  1044. {
  1045. FURI_LOG_I(HTTP_TAG, "Received PONG response: Wifi Dev Board is still alive.");
  1046. // send command to connect to WiFi
  1047. if (fhttp.state == INACTIVE)
  1048. {
  1049. fhttp.state = IDLE;
  1050. return;
  1051. }
  1052. }
  1053. if (fhttp.state == INACTIVE && strstr(line, "[PONG]") != NULL)
  1054. {
  1055. fhttp.state = IDLE;
  1056. }
  1057. else if (fhttp.state == INACTIVE && strstr(line, "[PONG]") == NULL)
  1058. {
  1059. fhttp.state = INACTIVE;
  1060. }
  1061. else
  1062. {
  1063. fhttp.state = IDLE;
  1064. }
  1065. }
  1066. // Function to save received data to a file
  1067. /**
  1068. * @brief Save the received data to a file.
  1069. * @return true if the data was saved successfully, false otherwise.
  1070. * @param bytes_received The number of bytes received.
  1071. * @param line_buffer The buffer containing the received data.
  1072. * @note The data will be saved to a file in the STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag "/received_data.txt" directory.
  1073. */
  1074. bool flipper_http_save_received_data(size_t bytes_received, const char line_buffer[])
  1075. {
  1076. const char *output_file_path = STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag "/received_data.txt";
  1077. // Ensure the directory exists
  1078. char directory_path[128];
  1079. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/" http_tag);
  1080. Storage *_storage = NULL;
  1081. File *_file = NULL;
  1082. // Open the storage if not opened already
  1083. // Initialize storage and create the directory if it doesn't exist
  1084. _storage = furi_record_open(RECORD_STORAGE);
  1085. storage_common_mkdir(_storage, directory_path); // Create directory if it doesn't exist
  1086. _file = storage_file_alloc(_storage);
  1087. // Open file for writing and append data line by line
  1088. if (!storage_file_open(_file, output_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  1089. {
  1090. FURI_LOG_E(HTTP_TAG, "Failed to open output file for writing.");
  1091. storage_file_free(_file);
  1092. furi_record_close(RECORD_STORAGE);
  1093. return false;
  1094. }
  1095. // Write each line received from the UART to the file
  1096. if (bytes_received > 0 && _file)
  1097. {
  1098. storage_file_write(_file, line_buffer, bytes_received);
  1099. storage_file_write(_file, "\n", 1); // Add a newline after each line
  1100. }
  1101. else
  1102. {
  1103. FURI_LOG_E(HTTP_TAG, "No data received.");
  1104. return false;
  1105. }
  1106. if (_file)
  1107. {
  1108. storage_file_close(_file);
  1109. storage_file_free(_file);
  1110. _file = NULL;
  1111. }
  1112. if (_storage)
  1113. {
  1114. furi_record_close(RECORD_STORAGE);
  1115. _storage = NULL;
  1116. }
  1117. return true;
  1118. }
  1119. // Function to trim leading and trailing spaces and newlines from a constant string
  1120. char *trim(const char *str)
  1121. {
  1122. const char *end;
  1123. char *trimmed_str;
  1124. size_t len;
  1125. // Trim leading space
  1126. while (isspace((unsigned char)*str))
  1127. str++;
  1128. // All spaces?
  1129. if (*str == 0)
  1130. return strdup(""); // Return an empty string if all spaces
  1131. // Trim trailing space
  1132. end = str + strlen(str) - 1;
  1133. while (end > str && isspace((unsigned char)*end))
  1134. end--;
  1135. // Set length for the trimmed string
  1136. len = end - str + 1;
  1137. // Allocate space for the trimmed string and null terminator
  1138. trimmed_str = (char *)malloc(len + 1);
  1139. if (trimmed_str == NULL)
  1140. {
  1141. return NULL; // Handle memory allocation failure
  1142. }
  1143. // Copy the trimmed part of the string into trimmed_str
  1144. strncpy(trimmed_str, str, len);
  1145. trimmed_str[len] = '\0'; // Null terminate the string
  1146. return trimmed_str;
  1147. }
  1148. #endif // FLIPPER_HTTP_H