flipper_http.h 46 KB

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