flipper_http.c 47 KB

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