flipper_http.c 47 KB

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