flipper_http.h 47 KB

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