flipper_http.c 45 KB

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