flipper_http.c 44 KB

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