flipper_http.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. return false;
  354. }
  355. // Create a buffer with data + '\n'
  356. size_t send_length = data_length + 1; // +1 for '\n'
  357. if(send_length > 256) { // Ensure buffer size is sufficient
  358. FURI_LOG_E("FlipperHTTP", "Data too long to send over FHTTP.");
  359. return false;
  360. }
  361. char send_buffer[257]; // 256 + 1 for safety
  362. strncpy(send_buffer, data, 256);
  363. send_buffer[data_length] = '\n'; // Append newline
  364. send_buffer[data_length + 1] = '\0'; // Null-terminate
  365. if(fhttp.state == INACTIVE && ((strstr(send_buffer, "[PING]") == NULL) &&
  366. (strstr(send_buffer, "[WIFI/CONNECT]") == NULL))) {
  367. FURI_LOG_E("FlipperHTTP", "Cannot send data while INACTIVE.");
  368. fhttp.last_response = "Cannot send data while INACTIVE.";
  369. return false;
  370. }
  371. fhttp.state = SENDING;
  372. furi_hal_serial_tx(fhttp.serial_handle, (const uint8_t*)send_buffer, send_length);
  373. // Uncomment below line to log the data sent over UART
  374. // FURI_LOG_I("FlipperHTTP", "Sent data over UART: %s", send_buffer);
  375. fhttp.state = IDLE;
  376. return true;
  377. }
  378. // Function to send a PING request
  379. /**
  380. * @brief Send a PING request to check if the Wifi Dev Board is connected.
  381. * @return true if the request was successful, false otherwise.
  382. * @note The received data will be handled asynchronously via the callback.
  383. * @note This is best used to check if the Wifi Dev Board is connected.
  384. * @note The state will remain INACTIVE until a PONG is received.
  385. */
  386. bool flipper_http_ping() {
  387. const char* command = "[PING]";
  388. if(!flipper_http_send_data(command)) {
  389. FURI_LOG_E("FlipperHTTP", "Failed to send PING command.");
  390. return false;
  391. }
  392. // set state as INACTIVE to be made IDLE if PONG is received
  393. fhttp.state = INACTIVE;
  394. // The response will be handled asynchronously via the callback
  395. return true;
  396. }
  397. // Function to list available commands
  398. /**
  399. * @brief Send a command to list available commands.
  400. * @return true if the request was successful, false otherwise.
  401. * @note The received data will be handled asynchronously via the callback.
  402. */
  403. bool flipper_http_list_commands() {
  404. const char* command = "[LIST]";
  405. if(!flipper_http_send_data(command)) {
  406. FURI_LOG_E("FlipperHTTP", "Failed to send LIST command.");
  407. return false;
  408. }
  409. // The response will be handled asynchronously via the callback
  410. return true;
  411. }
  412. // Function to turn on the LED
  413. /**
  414. * @brief Allow the LED to display while processing.
  415. * @return true if the request was successful, false otherwise.
  416. * @note The received data will be handled asynchronously via the callback.
  417. */
  418. bool flipper_http_led_on() {
  419. const char* command = "[LED/ON]";
  420. if(!flipper_http_send_data(command)) {
  421. FURI_LOG_E("FlipperHTTP", "Failed to send LED ON command.");
  422. return false;
  423. }
  424. // The response will be handled asynchronously via the callback
  425. return true;
  426. }
  427. // Function to turn off the LED
  428. /**
  429. * @brief Disable the LED from displaying while processing.
  430. * @return true if the request was successful, false otherwise.
  431. * @note The received data will be handled asynchronously via the callback.
  432. */
  433. bool flipper_http_led_off() {
  434. const char* command = "[LED/OFF]";
  435. if(!flipper_http_send_data(command)) {
  436. FURI_LOG_E("FlipperHTTP", "Failed to send LED OFF command.");
  437. return false;
  438. }
  439. // The response will be handled asynchronously via the callback
  440. return true;
  441. }
  442. // Function to parse JSON data
  443. /**
  444. * @brief Parse JSON data.
  445. * @return true if the JSON data was parsed successfully, false otherwise.
  446. * @param key The key to parse from the JSON data.
  447. * @param json_data The JSON data to parse.
  448. * @note The received data will be handled asynchronously via the callback.
  449. */
  450. bool flipper_http_parse_json(const char* key, const char* json_data) {
  451. if(!key || !json_data) {
  452. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_parse_json.");
  453. return false;
  454. }
  455. char buffer[256];
  456. int ret =
  457. snprintf(buffer, sizeof(buffer), "[PARSE]{\"key\":\"%s\",\"json\":%s}", key, json_data);
  458. if(ret < 0 || ret >= (int)sizeof(buffer)) {
  459. FURI_LOG_E("FlipperHTTP", "Failed to format JSON parse command.");
  460. return false;
  461. }
  462. if(!flipper_http_send_data(buffer)) {
  463. FURI_LOG_E("FlipperHTTP", "Failed to send JSON parse command.");
  464. return false;
  465. }
  466. // The response will be handled asynchronously via the callback
  467. return true;
  468. }
  469. // Function to parse JSON array data
  470. /**
  471. * @brief Parse JSON array data.
  472. * @return true if the JSON array data was parsed successfully, false otherwise.
  473. * @param key The key to parse from the JSON array data.
  474. * @param index The index to parse from the JSON array data.
  475. * @param json_data The JSON array data to parse.
  476. * @note The received data will be handled asynchronously via the callback.
  477. */
  478. bool flipper_http_parse_json_array(const char* key, int index, const char* json_data) {
  479. if(!key || !json_data) {
  480. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_parse_json_array.");
  481. return false;
  482. }
  483. char buffer[256];
  484. int ret = snprintf(
  485. buffer,
  486. sizeof(buffer),
  487. "[PARSE/ARRAY]{\"key\":\"%s\",\"index\":%d,\"json\":%s}",
  488. key,
  489. index,
  490. json_data);
  491. if(ret < 0 || ret >= (int)sizeof(buffer)) {
  492. FURI_LOG_E("FlipperHTTP", "Failed to format JSON parse array command.");
  493. return false;
  494. }
  495. if(!flipper_http_send_data(buffer)) {
  496. FURI_LOG_E("FlipperHTTP", "Failed to send JSON parse array command.");
  497. return false;
  498. }
  499. // The response will be handled asynchronously via the callback
  500. return true;
  501. }
  502. // Function to scan for WiFi networks
  503. /**
  504. * @brief Send a command to scan for WiFi networks.
  505. * @return true if the request was successful, false otherwise.
  506. * @note The received data will be handled asynchronously via the callback.
  507. */
  508. bool flipper_http_scan_wifi() {
  509. const char* command = "[WIFI/SCAN]";
  510. if(!flipper_http_send_data(command)) {
  511. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi scan command.");
  512. return false;
  513. }
  514. // The response will be handled asynchronously via the callback
  515. return true;
  516. }
  517. // Function to save WiFi settings (returns true if successful)
  518. /**
  519. * @brief Send a command to save WiFi settings.
  520. * @return true if the request was successful, false otherwise.
  521. * @note The received data will be handled asynchronously via the callback.
  522. */
  523. bool flipper_http_save_wifi(const char* ssid, const char* password) {
  524. if(!ssid || !password) {
  525. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_save_wifi.");
  526. return false;
  527. }
  528. char buffer[256];
  529. int ret = snprintf(
  530. buffer, sizeof(buffer), "[WIFI/SAVE]{\"ssid\":\"%s\",\"password\":\"%s\"}", ssid, password);
  531. if(ret < 0 || ret >= (int)sizeof(buffer)) {
  532. FURI_LOG_E("FlipperHTTP", "Failed to format WiFi save command.");
  533. return false;
  534. }
  535. if(!flipper_http_send_data(buffer)) {
  536. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi save command.");
  537. return false;
  538. }
  539. // The response will be handled asynchronously via the callback
  540. return true;
  541. }
  542. // Function to get IP address of WiFi Devboard
  543. /**
  544. * @brief Send a command to get the IP address of the WiFi Devboard
  545. * @return true if the request was successful, false otherwise.
  546. * @note The received data will be handled asynchronously via the callback.
  547. */
  548. bool flipper_http_ip_address() {
  549. const char* command = "[IP/ADDRESS]";
  550. if(!flipper_http_send_data(command)) {
  551. FURI_LOG_E("FlipperHTTP", "Failed to send IP address command.");
  552. return false;
  553. }
  554. // The response will be handled asynchronously via the callback
  555. return true;
  556. }
  557. // Function to get IP address of the connected WiFi network
  558. /**
  559. * @brief Send a command to get the IP address of the connected WiFi network.
  560. * @return true if the request was successful, false otherwise.
  561. * @note The received data will be handled asynchronously via the callback.
  562. */
  563. bool flipper_http_ip_wifi() {
  564. const char* command = "[WIFI/IP]";
  565. if(!flipper_http_send_data(command)) {
  566. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi IP command.");
  567. return false;
  568. }
  569. // The response will be handled asynchronously via the callback
  570. return true;
  571. }
  572. // Function to disconnect from WiFi (returns true if successful)
  573. /**
  574. * @brief Send a command to disconnect from WiFi.
  575. * @return true if the request was successful, false otherwise.
  576. * @note The received data will be handled asynchronously via the callback.
  577. */
  578. bool flipper_http_disconnect_wifi() {
  579. const char* command = "[WIFI/DISCONNECT]";
  580. if(!flipper_http_send_data(command)) {
  581. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi disconnect command.");
  582. return false;
  583. }
  584. // The response will be handled asynchronously via the callback
  585. return true;
  586. }
  587. // Function to connect to WiFi (returns true if successful)
  588. /**
  589. * @brief Send a command to connect to WiFi.
  590. * @return true if the request was successful, false otherwise.
  591. * @note The received data will be handled asynchronously via the callback.
  592. */
  593. bool flipper_http_connect_wifi() {
  594. const char* command = "[WIFI/CONNECT]";
  595. if(!flipper_http_send_data(command)) {
  596. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi connect command.");
  597. return false;
  598. }
  599. // The response will be handled asynchronously via the callback
  600. return true;
  601. }
  602. // Function to send a GET request
  603. /**
  604. * @brief Send a GET request to the specified URL.
  605. * @return true if the request was successful, false otherwise.
  606. * @param url The URL to send the GET request to.
  607. * @note The received data will be handled asynchronously via the callback.
  608. */
  609. bool flipper_http_get_request(const char* url) {
  610. if(!url) {
  611. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_get_request.");
  612. return false;
  613. }
  614. // Prepare GET request command
  615. char command[256];
  616. int ret = snprintf(command, sizeof(command), "[GET]%s", url);
  617. if(ret < 0 || ret >= (int)sizeof(command)) {
  618. FURI_LOG_E("FlipperHTTP", "Failed to format GET request command.");
  619. return false;
  620. }
  621. // Send GET request via UART
  622. if(!flipper_http_send_data(command)) {
  623. FURI_LOG_E("FlipperHTTP", "Failed to send GET request command.");
  624. return false;
  625. }
  626. // The response will be handled asynchronously via the callback
  627. return true;
  628. }
  629. // Function to send a GET request with headers
  630. /**
  631. * @brief Send a GET request to the specified URL.
  632. * @return true if the request was successful, false otherwise.
  633. * @param url The URL to send the GET request to.
  634. * @param headers The headers to send with the GET request.
  635. * @note The received data will be handled asynchronously via the callback.
  636. */
  637. bool flipper_http_get_request_with_headers(const char* url, const char* headers) {
  638. if(!url || !headers) {
  639. FURI_LOG_E(
  640. "FlipperHTTP", "Invalid arguments provided to flipper_http_get_request_with_headers.");
  641. return false;
  642. }
  643. // Prepare GET request command with headers
  644. char command[256];
  645. int ret = snprintf(
  646. command, sizeof(command), "[GET/HTTP]{\"url\":\"%s\",\"headers\":%s}", url, headers);
  647. if(ret < 0 || ret >= (int)sizeof(command)) {
  648. FURI_LOG_E("FlipperHTTP", "Failed to format GET request command with headers.");
  649. return false;
  650. }
  651. // Send GET request via UART
  652. if(!flipper_http_send_data(command)) {
  653. FURI_LOG_E("FlipperHTTP", "Failed to send GET request command with headers.");
  654. return false;
  655. }
  656. // The response will be handled asynchronously via the callback
  657. return true;
  658. }
  659. // Function to send a GET request with headers and return bytes
  660. /**
  661. * @brief Send a GET request to the specified URL.
  662. * @return true if the request was successful, false otherwise.
  663. * @param url The URL to send the GET request to.
  664. * @param headers The headers to send with the GET request.
  665. * @note The received data will be handled asynchronously via the callback.
  666. */
  667. bool flipper_http_get_request_bytes(const char* url, const char* headers) {
  668. if(!url || !headers) {
  669. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_get_request_bytes.");
  670. return false;
  671. }
  672. // Prepare GET request command with headers
  673. char command[256];
  674. int ret = snprintf(
  675. command, sizeof(command), "[GET/BYTES]{\"url\":\"%s\",\"headers\":%s}", url, headers);
  676. if(ret < 0 || ret >= (int)sizeof(command)) {
  677. FURI_LOG_E("FlipperHTTP", "Failed to format GET request command with headers.");
  678. return false;
  679. }
  680. // Send GET request via UART
  681. if(!flipper_http_send_data(command)) {
  682. FURI_LOG_E("FlipperHTTP", "Failed to send GET request command with headers.");
  683. return false;
  684. }
  685. // The response will be handled asynchronously via the callback
  686. return true;
  687. }
  688. // Function to send a POST request with headers
  689. /**
  690. * @brief Send a POST request to the specified URL.
  691. * @return true if the request was successful, false otherwise.
  692. * @param url The URL to send the POST request to.
  693. * @param headers The headers to send with the POST request.
  694. * @param data The data to send with the POST request.
  695. * @note The received data will be handled asynchronously via the callback.
  696. */
  697. bool flipper_http_post_request_with_headers(
  698. const char* url,
  699. const char* headers,
  700. const char* payload) {
  701. if(!url || !headers || !payload) {
  702. FURI_LOG_E(
  703. "FlipperHTTP",
  704. "Invalid arguments provided to flipper_http_post_request_with_headers.");
  705. return false;
  706. }
  707. // Prepare POST request command with headers and data
  708. char command[256];
  709. int ret = snprintf(
  710. command,
  711. sizeof(command),
  712. "[POST/HTTP]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}",
  713. url,
  714. headers,
  715. payload);
  716. if(ret < 0 || ret >= (int)sizeof(command)) {
  717. FURI_LOG_E("FlipperHTTP", "Failed to format POST request command with headers and data.");
  718. return false;
  719. }
  720. // Send POST request via UART
  721. if(!flipper_http_send_data(command)) {
  722. FURI_LOG_E("FlipperHTTP", "Failed to send POST request command with headers and data.");
  723. return false;
  724. }
  725. // The response will be handled asynchronously via the callback
  726. return true;
  727. }
  728. // Function to send a POST request with headers and return bytes
  729. /**
  730. * @brief Send a POST request to the specified URL.
  731. * @return true if the request was successful, false otherwise.
  732. * @param url The URL to send the POST request to.
  733. * @param headers The headers to send with the POST request.
  734. * @param payload The data to send with the POST request.
  735. * @note The received data will be handled asynchronously via the callback.
  736. */
  737. bool flipper_http_post_request_bytes(const char* url, const char* headers, const char* payload) {
  738. if(!url || !headers || !payload) {
  739. FURI_LOG_E(
  740. "FlipperHTTP", "Invalid arguments provided to flipper_http_post_request_bytes.");
  741. return false;
  742. }
  743. // Prepare POST request command with headers and data
  744. char command[256];
  745. int ret = snprintf(
  746. command,
  747. sizeof(command),
  748. "[POST/BYTES]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}",
  749. url,
  750. headers,
  751. payload);
  752. if(ret < 0 || ret >= (int)sizeof(command)) {
  753. FURI_LOG_E("FlipperHTTP", "Failed to format POST request command with headers and data.");
  754. return false;
  755. }
  756. // Send POST request via UART
  757. if(!flipper_http_send_data(command)) {
  758. FURI_LOG_E("FlipperHTTP", "Failed to send POST request command with headers and data.");
  759. return false;
  760. }
  761. // The response will be handled asynchronously via the callback
  762. return true;
  763. }
  764. // Function to send a PUT request with headers
  765. /**
  766. * @brief Send a PUT request to the specified URL.
  767. * @return true if the request was successful, false otherwise.
  768. * @param url The URL to send the PUT request to.
  769. * @param headers The headers to send with the PUT request.
  770. * @param data The data to send with the PUT request.
  771. * @note The received data will be handled asynchronously via the callback.
  772. */
  773. bool flipper_http_put_request_with_headers(
  774. const char* url,
  775. const char* headers,
  776. const char* payload) {
  777. if(!url || !headers || !payload) {
  778. FURI_LOG_E(
  779. "FlipperHTTP", "Invalid arguments provided to flipper_http_put_request_with_headers.");
  780. return false;
  781. }
  782. // Prepare PUT request command with headers and data
  783. char command[256];
  784. int ret = snprintf(
  785. command,
  786. sizeof(command),
  787. "[PUT/HTTP]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}",
  788. url,
  789. headers,
  790. payload);
  791. if(ret < 0 || ret >= (int)sizeof(command)) {
  792. FURI_LOG_E("FlipperHTTP", "Failed to format PUT request command with headers and data.");
  793. return false;
  794. }
  795. // Send PUT request via UART
  796. if(!flipper_http_send_data(command)) {
  797. FURI_LOG_E("FlipperHTTP", "Failed to send PUT request command with headers and data.");
  798. return false;
  799. }
  800. // The response will be handled asynchronously via the callback
  801. return true;
  802. }
  803. // Function to send a DELETE request with headers
  804. /**
  805. * @brief Send a DELETE request to the specified URL.
  806. * @return true if the request was successful, false otherwise.
  807. * @param url The URL to send the DELETE request to.
  808. * @param headers The headers to send with the DELETE request.
  809. * @param data The data to send with the DELETE request.
  810. * @note The received data will be handled asynchronously via the callback.
  811. */
  812. bool flipper_http_delete_request_with_headers(
  813. const char* url,
  814. const char* headers,
  815. const char* payload) {
  816. if(!url || !headers || !payload) {
  817. FURI_LOG_E(
  818. "FlipperHTTP",
  819. "Invalid arguments provided to flipper_http_delete_request_with_headers.");
  820. return false;
  821. }
  822. // Prepare DELETE request command with headers and data
  823. char command[256];
  824. int ret = snprintf(
  825. command,
  826. sizeof(command),
  827. "[DELETE/HTTP]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}",
  828. url,
  829. headers,
  830. payload);
  831. if(ret < 0 || ret >= (int)sizeof(command)) {
  832. FURI_LOG_E(
  833. "FlipperHTTP", "Failed to format DELETE request command with headers and data.");
  834. return false;
  835. }
  836. // Send DELETE request via UART
  837. if(!flipper_http_send_data(command)) {
  838. FURI_LOG_E("FlipperHTTP", "Failed to send DELETE request command with headers and data.");
  839. return false;
  840. }
  841. // The response will be handled asynchronously via the callback
  842. return true;
  843. }
  844. // Function to handle received data asynchronously
  845. /**
  846. * @brief Callback function to handle received data asynchronously.
  847. * @return void
  848. * @param line The received line.
  849. * @param context The context passed to the callback.
  850. * @note The received data will be handled asynchronously via the callback and handles the state of the UART.
  851. */
  852. void flipper_http_rx_callback(const char* line, void* context) {
  853. if(!line || !context) {
  854. FURI_LOG_E(HTTP_TAG, "Invalid arguments provided to flipper_http_rx_callback.");
  855. return;
  856. }
  857. // Trim the received line to check if it's empty
  858. char* trimmed_line = trim(line);
  859. if(trimmed_line != NULL && trimmed_line[0] != '\0') {
  860. // if the line is not [GET/END] or [POST/END] or [PUT/END] or [DELETE/END]
  861. if(strstr(trimmed_line, "[GET/END]") == NULL &&
  862. strstr(trimmed_line, "[POST/END]") == NULL &&
  863. strstr(trimmed_line, "[PUT/END]") == NULL &&
  864. strstr(trimmed_line, "[DELETE/END]") == NULL) {
  865. strncpy(fhttp.last_response, trimmed_line, RX_BUF_SIZE);
  866. }
  867. }
  868. free(trimmed_line); // Free the allocated memory for trimmed_line
  869. if(fhttp.state != INACTIVE && fhttp.state != ISSUE) {
  870. fhttp.state = RECEIVING;
  871. }
  872. // Uncomment below line to log the data received over UART
  873. // FURI_LOG_I(HTTP_TAG, "Received UART line: %s", line);
  874. // Check if we've started receiving data from a GET request
  875. if(fhttp.started_receiving_get) {
  876. // Restart the timeout timer each time new data is received
  877. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  878. if(strstr(line, "[GET/END]") != NULL) {
  879. FURI_LOG_I(HTTP_TAG, "GET request completed.");
  880. // Stop the timer since we've completed the GET request
  881. furi_timer_stop(fhttp.get_timeout_timer);
  882. fhttp.started_receiving_get = false;
  883. fhttp.just_started_get = false;
  884. fhttp.state = IDLE;
  885. fhttp.save_bytes = false;
  886. fhttp.save_received_data = false;
  887. if(fhttp.is_bytes_request) {
  888. // Search for the binary marker `[GET/END]` in the file buffer
  889. const char marker[] = "[GET/END]";
  890. const size_t marker_len = sizeof(marker) - 1; // Exclude null terminator
  891. for(size_t i = 0; i <= file_buffer_len - marker_len; i++) {
  892. // Check if the marker is found
  893. if(memcmp(&file_buffer[i], marker, marker_len) == 0) {
  894. // Remove the marker by shifting the remaining data left
  895. size_t remaining_len = file_buffer_len - (i + marker_len);
  896. memmove(&file_buffer[i], &file_buffer[i + marker_len], remaining_len);
  897. file_buffer_len -= marker_len;
  898. break;
  899. }
  900. }
  901. // If there is data left in the buffer, append it to the file
  902. if(file_buffer_len > 0) {
  903. if(!flipper_http_append_to_file(
  904. file_buffer, file_buffer_len, false, fhttp.file_path)) {
  905. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  906. }
  907. file_buffer_len = 0;
  908. }
  909. }
  910. fhttp.is_bytes_request = false;
  911. return;
  912. }
  913. // Append the new line to the existing data
  914. if(fhttp.save_received_data &&
  915. !flipper_http_append_to_file(
  916. line, strlen(line), !fhttp.just_started_get, fhttp.file_path)) {
  917. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  918. fhttp.started_receiving_get = false;
  919. fhttp.just_started_get = false;
  920. fhttp.state = IDLE;
  921. return;
  922. }
  923. if(!fhttp.just_started_get) {
  924. fhttp.just_started_get = true;
  925. }
  926. return;
  927. }
  928. // Check if we've started receiving data from a POST request
  929. else if(fhttp.started_receiving_post) {
  930. // Restart the timeout timer each time new data is received
  931. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  932. if(strstr(line, "[POST/END]") != NULL) {
  933. FURI_LOG_I(HTTP_TAG, "POST request completed.");
  934. // Stop the timer since we've completed the POST request
  935. furi_timer_stop(fhttp.get_timeout_timer);
  936. fhttp.started_receiving_post = false;
  937. fhttp.just_started_post = false;
  938. fhttp.state = IDLE;
  939. fhttp.save_bytes = false;
  940. fhttp.save_received_data = false;
  941. if(fhttp.is_bytes_request) {
  942. // Search for the binary marker `[POST/END]` in the file buffer
  943. const char marker[] = "[POST/END]";
  944. const size_t marker_len = sizeof(marker) - 1; // Exclude null terminator
  945. for(size_t i = 0; i <= file_buffer_len - marker_len; i++) {
  946. // Check if the marker is found
  947. if(memcmp(&file_buffer[i], marker, marker_len) == 0) {
  948. // Remove the marker by shifting the remaining data left
  949. size_t remaining_len = file_buffer_len - (i + marker_len);
  950. memmove(&file_buffer[i], &file_buffer[i + marker_len], remaining_len);
  951. file_buffer_len -= marker_len;
  952. break;
  953. }
  954. }
  955. // If there is data left in the buffer, append it to the file
  956. if(file_buffer_len > 0) {
  957. if(!flipper_http_append_to_file(
  958. file_buffer, file_buffer_len, false, fhttp.file_path)) {
  959. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  960. }
  961. file_buffer_len = 0;
  962. }
  963. }
  964. fhttp.is_bytes_request = false;
  965. return;
  966. }
  967. // Append the new line to the existing data
  968. if(fhttp.save_received_data &&
  969. !flipper_http_append_to_file(
  970. line, strlen(line), !fhttp.just_started_post, fhttp.file_path)) {
  971. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  972. fhttp.started_receiving_post = false;
  973. fhttp.just_started_post = false;
  974. fhttp.state = IDLE;
  975. return;
  976. }
  977. if(!fhttp.just_started_post) {
  978. fhttp.just_started_post = true;
  979. }
  980. return;
  981. }
  982. // Check if we've started receiving data from a PUT request
  983. else if(fhttp.started_receiving_put) {
  984. // Restart the timeout timer each time new data is received
  985. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  986. if(strstr(line, "[PUT/END]") != NULL) {
  987. FURI_LOG_I(HTTP_TAG, "PUT request completed.");
  988. // Stop the timer since we've completed the PUT request
  989. furi_timer_stop(fhttp.get_timeout_timer);
  990. fhttp.started_receiving_put = false;
  991. fhttp.just_started_put = false;
  992. fhttp.state = IDLE;
  993. fhttp.save_bytes = false;
  994. fhttp.is_bytes_request = false;
  995. fhttp.save_received_data = false;
  996. return;
  997. }
  998. // Append the new line to the existing data
  999. if(fhttp.save_received_data &&
  1000. !flipper_http_append_to_file(
  1001. line, strlen(line), !fhttp.just_started_put, fhttp.file_path)) {
  1002. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  1003. fhttp.started_receiving_put = false;
  1004. fhttp.just_started_put = false;
  1005. fhttp.state = IDLE;
  1006. return;
  1007. }
  1008. if(!fhttp.just_started_put) {
  1009. fhttp.just_started_put = true;
  1010. }
  1011. return;
  1012. }
  1013. // Check if we've started receiving data from a DELETE request
  1014. else if(fhttp.started_receiving_delete) {
  1015. // Restart the timeout timer each time new data is received
  1016. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1017. if(strstr(line, "[DELETE/END]") != NULL) {
  1018. FURI_LOG_I(HTTP_TAG, "DELETE request completed.");
  1019. // Stop the timer since we've completed the DELETE request
  1020. furi_timer_stop(fhttp.get_timeout_timer);
  1021. fhttp.started_receiving_delete = false;
  1022. fhttp.just_started_delete = false;
  1023. fhttp.state = IDLE;
  1024. fhttp.save_bytes = false;
  1025. fhttp.is_bytes_request = false;
  1026. fhttp.save_received_data = false;
  1027. return;
  1028. }
  1029. // Append the new line to the existing data
  1030. if(fhttp.save_received_data &&
  1031. !flipper_http_append_to_file(
  1032. line, strlen(line), !fhttp.just_started_delete, fhttp.file_path)) {
  1033. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  1034. fhttp.started_receiving_delete = false;
  1035. fhttp.just_started_delete = false;
  1036. fhttp.state = IDLE;
  1037. return;
  1038. }
  1039. if(!fhttp.just_started_delete) {
  1040. fhttp.just_started_delete = true;
  1041. }
  1042. return;
  1043. }
  1044. // Handle different types of responses
  1045. if(strstr(line, "[SUCCESS]") != NULL || strstr(line, "[CONNECTED]") != NULL) {
  1046. FURI_LOG_I(HTTP_TAG, "Operation succeeded.");
  1047. } else if(strstr(line, "[INFO]") != NULL) {
  1048. FURI_LOG_I(HTTP_TAG, "Received info: %s", line);
  1049. if(fhttp.state == INACTIVE && strstr(line, "[INFO] Already connected to Wifi.") != NULL) {
  1050. fhttp.state = IDLE;
  1051. }
  1052. } else if(strstr(line, "[GET/SUCCESS]") != NULL) {
  1053. FURI_LOG_I(HTTP_TAG, "GET request succeeded.");
  1054. fhttp.started_receiving_get = true;
  1055. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1056. fhttp.state = RECEIVING;
  1057. // for GET request, save data only if it's a bytes request
  1058. fhttp.save_bytes = fhttp.is_bytes_request;
  1059. fhttp.just_started_bytes = true;
  1060. file_buffer_len = 0;
  1061. return;
  1062. } else if(strstr(line, "[POST/SUCCESS]") != NULL) {
  1063. FURI_LOG_I(HTTP_TAG, "POST request succeeded.");
  1064. fhttp.started_receiving_post = true;
  1065. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1066. fhttp.state = RECEIVING;
  1067. // for POST request, save data only if it's a bytes request
  1068. fhttp.save_bytes = fhttp.is_bytes_request;
  1069. fhttp.just_started_bytes = true;
  1070. file_buffer_len = 0;
  1071. return;
  1072. } else if(strstr(line, "[PUT/SUCCESS]") != NULL) {
  1073. FURI_LOG_I(HTTP_TAG, "PUT request succeeded.");
  1074. fhttp.started_receiving_put = true;
  1075. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1076. fhttp.state = RECEIVING;
  1077. return;
  1078. } else if(strstr(line, "[DELETE/SUCCESS]") != NULL) {
  1079. FURI_LOG_I(HTTP_TAG, "DELETE request succeeded.");
  1080. fhttp.started_receiving_delete = true;
  1081. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1082. fhttp.state = RECEIVING;
  1083. return;
  1084. } else if(strstr(line, "[DISCONNECTED]") != NULL) {
  1085. FURI_LOG_I(HTTP_TAG, "WiFi disconnected successfully.");
  1086. } else if(strstr(line, "[ERROR]") != NULL) {
  1087. FURI_LOG_E(HTTP_TAG, "Received error: %s", line);
  1088. fhttp.state = ISSUE;
  1089. return;
  1090. } else if(strstr(line, "[PONG]") != NULL) {
  1091. FURI_LOG_I(HTTP_TAG, "Received PONG response: Wifi Dev Board is still alive.");
  1092. // send command to connect to WiFi
  1093. if(fhttp.state == INACTIVE) {
  1094. fhttp.state = IDLE;
  1095. return;
  1096. }
  1097. }
  1098. if(fhttp.state == INACTIVE && strstr(line, "[PONG]") != NULL) {
  1099. fhttp.state = IDLE;
  1100. } else if(fhttp.state == INACTIVE && strstr(line, "[PONG]") == NULL) {
  1101. fhttp.state = INACTIVE;
  1102. } else {
  1103. fhttp.state = IDLE;
  1104. }
  1105. }
  1106. // Function to trim leading and trailing spaces and newlines from a constant string
  1107. char* trim(const char* str) {
  1108. const char* end;
  1109. char* trimmed_str;
  1110. size_t len;
  1111. // Trim leading space
  1112. while(isspace((unsigned char)*str))
  1113. str++;
  1114. // All spaces?
  1115. if(*str == 0) return strdup(""); // Return an empty string if all spaces
  1116. // Trim trailing space
  1117. end = str + strlen(str) - 1;
  1118. while(end > str && isspace((unsigned char)*end))
  1119. end--;
  1120. // Set length for the trimmed string
  1121. len = end - str + 1;
  1122. // Allocate space for the trimmed string and null terminator
  1123. trimmed_str = (char*)malloc(len + 1);
  1124. if(trimmed_str == NULL) {
  1125. return NULL; // Handle memory allocation failure
  1126. }
  1127. // Copy the trimmed part of the string into trimmed_str
  1128. strncpy(trimmed_str, str, len);
  1129. trimmed_str[len] = '\0'; // Null terminate the string
  1130. return trimmed_str;
  1131. }
  1132. /**
  1133. * @brief Process requests and parse JSON data asynchronously
  1134. * @param http_request The function to send the request
  1135. * @param parse_json The function to parse the JSON
  1136. * @return true if successful, false otherwise
  1137. */
  1138. bool flipper_http_process_response_async(bool (*http_request)(void), bool (*parse_json)(void)) {
  1139. if(http_request()) // start the async request
  1140. {
  1141. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1142. fhttp.state = RECEIVING;
  1143. } else {
  1144. FURI_LOG_E(HTTP_TAG, "Failed to send request");
  1145. return false;
  1146. }
  1147. while(fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0) {
  1148. // Wait for the request to be received
  1149. furi_delay_ms(100);
  1150. }
  1151. furi_timer_stop(fhttp.get_timeout_timer);
  1152. if(!parse_json()) // parse the JSON before switching to the view (synchonous)
  1153. {
  1154. FURI_LOG_E(HTTP_TAG, "Failed to parse the JSON...");
  1155. return false;
  1156. }
  1157. return true;
  1158. }