flipper_http.c 45 KB

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