flipper_http.c 47 KB

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