flipper_http.h 47 KB

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