flipper_http.c 46 KB

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