flipper_http.c 45 KB

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