flipper_http.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. if(!flipper_http_send_data("[WIFI/SCAN]")) {
  510. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi scan command.");
  511. return false;
  512. }
  513. // custom for FlipWiFi app
  514. fhttp.just_started_get = true;
  515. snprintf(
  516. fhttp.file_path,
  517. sizeof(fhttp.file_path),
  518. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_wifi/scan.txt");
  519. fhttp.save_received_data = true;
  520. // The response will be handled asynchronously via the callback
  521. return true;
  522. }
  523. // Function to save WiFi settings (returns true if successful)
  524. /**
  525. * @brief Send a command to save WiFi settings.
  526. * @return true if the request was successful, false otherwise.
  527. * @note The received data will be handled asynchronously via the callback.
  528. */
  529. bool flipper_http_save_wifi(const char* ssid, const char* password) {
  530. if(!ssid || !password) {
  531. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_save_wifi.");
  532. return false;
  533. }
  534. char buffer[256];
  535. int ret = snprintf(
  536. buffer, sizeof(buffer), "[WIFI/SAVE]{\"ssid\":\"%s\",\"password\":\"%s\"}", ssid, password);
  537. if(ret < 0 || ret >= (int)sizeof(buffer)) {
  538. FURI_LOG_E("FlipperHTTP", "Failed to format WiFi save command.");
  539. return false;
  540. }
  541. if(!flipper_http_send_data(buffer)) {
  542. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi save command.");
  543. return false;
  544. }
  545. // The response will be handled asynchronously via the callback
  546. return true;
  547. }
  548. // Function to get IP address of WiFi Devboard
  549. /**
  550. * @brief Send a command to get the IP address of the WiFi Devboard
  551. * @return true if the request was successful, false otherwise.
  552. * @note The received data will be handled asynchronously via the callback.
  553. */
  554. bool flipper_http_ip_address() {
  555. if(!flipper_http_send_data("[IP/ADDRESS]")) {
  556. FURI_LOG_E("FlipperHTTP", "Failed to send IP address command.");
  557. return false;
  558. }
  559. // The response will be handled asynchronously via the callback
  560. return true;
  561. }
  562. // Function to get IP address of the connected WiFi network
  563. /**
  564. * @brief Send a command to get the IP address of the connected WiFi network.
  565. * @return true if the request was successful, false otherwise.
  566. * @note The received data will be handled asynchronously via the callback.
  567. */
  568. bool flipper_http_ip_wifi() {
  569. const char* command = "[WIFI/IP]";
  570. if(!flipper_http_send_data(command)) {
  571. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi IP command.");
  572. return false;
  573. }
  574. // The response will be handled asynchronously via the callback
  575. return true;
  576. }
  577. // Function to disconnect from WiFi (returns true if successful)
  578. /**
  579. * @brief Send a command to disconnect from WiFi.
  580. * @return true if the request was successful, false otherwise.
  581. * @note The received data will be handled asynchronously via the callback.
  582. */
  583. bool flipper_http_disconnect_wifi() {
  584. if(!flipper_http_send_data("[WIFI/DISCONNECT]")) {
  585. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi disconnect command.");
  586. return false;
  587. }
  588. // The response will be handled asynchronously via the callback
  589. return true;
  590. }
  591. // Function to connect to WiFi (returns true if successful)
  592. /**
  593. * @brief Send a command to connect to WiFi.
  594. * @return true if the request was successful, false otherwise.
  595. * @note The received data will be handled asynchronously via the callback.
  596. */
  597. bool flipper_http_connect_wifi() {
  598. if(!flipper_http_send_data("[WIFI/CONNECT]")) {
  599. FURI_LOG_E("FlipperHTTP", "Failed to send WiFi connect command.");
  600. return false;
  601. }
  602. // The response will be handled asynchronously via the callback
  603. return true;
  604. }
  605. // Function to send a GET request
  606. /**
  607. * @brief Send a GET request to the specified URL.
  608. * @return true if the request was successful, false otherwise.
  609. * @param url The URL to send the GET request to.
  610. * @note The received data will be handled asynchronously via the callback.
  611. */
  612. bool flipper_http_get_request(const char* url) {
  613. if(!url) {
  614. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_get_request.");
  615. return false;
  616. }
  617. // Prepare GET request command
  618. char command[256];
  619. int ret = snprintf(command, sizeof(command), "[GET]%s", url);
  620. if(ret < 0 || ret >= (int)sizeof(command)) {
  621. FURI_LOG_E("FlipperHTTP", "Failed to format GET request command.");
  622. return false;
  623. }
  624. // Send GET request via UART
  625. if(!flipper_http_send_data(command)) {
  626. FURI_LOG_E("FlipperHTTP", "Failed to send GET request command.");
  627. return false;
  628. }
  629. // The response will be handled asynchronously via the callback
  630. return true;
  631. }
  632. // Function to send a GET request with headers
  633. /**
  634. * @brief Send a GET request to the specified URL.
  635. * @return true if the request was successful, false otherwise.
  636. * @param url The URL to send the GET request to.
  637. * @param headers The headers to send with the GET request.
  638. * @note The received data will be handled asynchronously via the callback.
  639. */
  640. bool flipper_http_get_request_with_headers(const char* url, const char* headers) {
  641. if(!url || !headers) {
  642. FURI_LOG_E(
  643. "FlipperHTTP", "Invalid arguments provided to flipper_http_get_request_with_headers.");
  644. return false;
  645. }
  646. // Prepare GET request command with headers
  647. char command[256];
  648. int ret = snprintf(
  649. command, sizeof(command), "[GET/HTTP]{\"url\":\"%s\",\"headers\":%s}", url, headers);
  650. if(ret < 0 || ret >= (int)sizeof(command)) {
  651. FURI_LOG_E("FlipperHTTP", "Failed to format GET request command with headers.");
  652. return false;
  653. }
  654. // Send GET request via UART
  655. if(!flipper_http_send_data(command)) {
  656. FURI_LOG_E("FlipperHTTP", "Failed to send GET request command with headers.");
  657. return false;
  658. }
  659. // The response will be handled asynchronously via the callback
  660. return true;
  661. }
  662. // Function to send a GET request with headers and return bytes
  663. /**
  664. * @brief Send a GET request to the specified URL.
  665. * @return true if the request was successful, false otherwise.
  666. * @param url The URL to send the GET request to.
  667. * @param headers The headers to send with the GET request.
  668. * @note The received data will be handled asynchronously via the callback.
  669. */
  670. bool flipper_http_get_request_bytes(const char* url, const char* headers) {
  671. if(!url || !headers) {
  672. FURI_LOG_E("FlipperHTTP", "Invalid arguments provided to flipper_http_get_request_bytes.");
  673. return false;
  674. }
  675. // Prepare GET request command with headers
  676. char command[256];
  677. int ret = snprintf(
  678. command, sizeof(command), "[GET/BYTES]{\"url\":\"%s\",\"headers\":%s}", url, headers);
  679. if(ret < 0 || ret >= (int)sizeof(command)) {
  680. FURI_LOG_E("FlipperHTTP", "Failed to format GET request command with headers.");
  681. return false;
  682. }
  683. // Send GET request via UART
  684. if(!flipper_http_send_data(command)) {
  685. FURI_LOG_E("FlipperHTTP", "Failed to send GET request command with headers.");
  686. return false;
  687. }
  688. // The response will be handled asynchronously via the callback
  689. return true;
  690. }
  691. // Function to send a POST request with headers
  692. /**
  693. * @brief Send a POST request to the specified URL.
  694. * @return true if the request was successful, false otherwise.
  695. * @param url The URL to send the POST request to.
  696. * @param headers The headers to send with the POST request.
  697. * @param data The data to send with the POST request.
  698. * @note The received data will be handled asynchronously via the callback.
  699. */
  700. bool flipper_http_post_request_with_headers(
  701. const char* url,
  702. const char* headers,
  703. const char* payload) {
  704. if(!url || !headers || !payload) {
  705. FURI_LOG_E(
  706. "FlipperHTTP",
  707. "Invalid arguments provided to flipper_http_post_request_with_headers.");
  708. return false;
  709. }
  710. // Prepare POST request command with headers and data
  711. char command[256];
  712. int ret = snprintf(
  713. command,
  714. sizeof(command),
  715. "[POST/HTTP]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}",
  716. url,
  717. headers,
  718. payload);
  719. if(ret < 0 || ret >= (int)sizeof(command)) {
  720. FURI_LOG_E("FlipperHTTP", "Failed to format POST request command with headers and data.");
  721. return false;
  722. }
  723. // Send POST request via UART
  724. if(!flipper_http_send_data(command)) {
  725. FURI_LOG_E("FlipperHTTP", "Failed to send POST request command with headers and data.");
  726. return false;
  727. }
  728. // The response will be handled asynchronously via the callback
  729. return true;
  730. }
  731. // Function to send a POST request with headers and return bytes
  732. /**
  733. * @brief Send a POST request to the specified URL.
  734. * @return true if the request was successful, false otherwise.
  735. * @param url The URL to send the POST request to.
  736. * @param headers The headers to send with the POST request.
  737. * @param payload The data to send with the POST request.
  738. * @note The received data will be handled asynchronously via the callback.
  739. */
  740. bool flipper_http_post_request_bytes(const char* url, const char* headers, const char* payload) {
  741. if(!url || !headers || !payload) {
  742. FURI_LOG_E(
  743. "FlipperHTTP", "Invalid arguments provided to flipper_http_post_request_bytes.");
  744. return false;
  745. }
  746. // Prepare POST request command with headers and data
  747. char command[256];
  748. int ret = snprintf(
  749. command,
  750. sizeof(command),
  751. "[POST/BYTES]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}",
  752. url,
  753. headers,
  754. payload);
  755. if(ret < 0 || ret >= (int)sizeof(command)) {
  756. FURI_LOG_E("FlipperHTTP", "Failed to format POST request command with headers and data.");
  757. return false;
  758. }
  759. // Send POST request via UART
  760. if(!flipper_http_send_data(command)) {
  761. FURI_LOG_E("FlipperHTTP", "Failed to send POST request command with headers and data.");
  762. return false;
  763. }
  764. // The response will be handled asynchronously via the callback
  765. return true;
  766. }
  767. // Function to send a PUT request with headers
  768. /**
  769. * @brief Send a PUT request to the specified URL.
  770. * @return true if the request was successful, false otherwise.
  771. * @param url The URL to send the PUT request to.
  772. * @param headers The headers to send with the PUT request.
  773. * @param data The data to send with the PUT request.
  774. * @note The received data will be handled asynchronously via the callback.
  775. */
  776. bool flipper_http_put_request_with_headers(
  777. const char* url,
  778. const char* headers,
  779. const char* payload) {
  780. if(!url || !headers || !payload) {
  781. FURI_LOG_E(
  782. "FlipperHTTP", "Invalid arguments provided to flipper_http_put_request_with_headers.");
  783. return false;
  784. }
  785. // Prepare PUT request command with headers and data
  786. char command[256];
  787. int ret = snprintf(
  788. command,
  789. sizeof(command),
  790. "[PUT/HTTP]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}",
  791. url,
  792. headers,
  793. payload);
  794. if(ret < 0 || ret >= (int)sizeof(command)) {
  795. FURI_LOG_E("FlipperHTTP", "Failed to format PUT request command with headers and data.");
  796. return false;
  797. }
  798. // Send PUT request via UART
  799. if(!flipper_http_send_data(command)) {
  800. FURI_LOG_E("FlipperHTTP", "Failed to send PUT request command with headers and data.");
  801. return false;
  802. }
  803. // The response will be handled asynchronously via the callback
  804. return true;
  805. }
  806. // Function to send a DELETE request with headers
  807. /**
  808. * @brief Send a DELETE request to the specified URL.
  809. * @return true if the request was successful, false otherwise.
  810. * @param url The URL to send the DELETE request to.
  811. * @param headers The headers to send with the DELETE request.
  812. * @param data The data to send with the DELETE request.
  813. * @note The received data will be handled asynchronously via the callback.
  814. */
  815. bool flipper_http_delete_request_with_headers(
  816. const char* url,
  817. const char* headers,
  818. const char* payload) {
  819. if(!url || !headers || !payload) {
  820. FURI_LOG_E(
  821. "FlipperHTTP",
  822. "Invalid arguments provided to flipper_http_delete_request_with_headers.");
  823. return false;
  824. }
  825. // Prepare DELETE request command with headers and data
  826. char command[256];
  827. int ret = snprintf(
  828. command,
  829. sizeof(command),
  830. "[DELETE/HTTP]{\"url\":\"%s\",\"headers\":%s,\"payload\":%s}",
  831. url,
  832. headers,
  833. payload);
  834. if(ret < 0 || ret >= (int)sizeof(command)) {
  835. FURI_LOG_E(
  836. "FlipperHTTP", "Failed to format DELETE request command with headers and data.");
  837. return false;
  838. }
  839. // Send DELETE request via UART
  840. if(!flipper_http_send_data(command)) {
  841. FURI_LOG_E("FlipperHTTP", "Failed to send DELETE request command with headers and data.");
  842. return false;
  843. }
  844. // The response will be handled asynchronously via the callback
  845. return true;
  846. }
  847. // Function to handle received data asynchronously
  848. /**
  849. * @brief Callback function to handle received data asynchronously.
  850. * @return void
  851. * @param line The received line.
  852. * @param context The context passed to the callback.
  853. * @note The received data will be handled asynchronously via the callback and handles the state of the UART.
  854. */
  855. void flipper_http_rx_callback(const char* line, void* context) {
  856. if(!line || !context) {
  857. FURI_LOG_E(HTTP_TAG, "Invalid arguments provided to flipper_http_rx_callback.");
  858. return;
  859. }
  860. // Trim the received line to check if it's empty
  861. char* trimmed_line = trim(line);
  862. if(trimmed_line != NULL && trimmed_line[0] != '\0') {
  863. // if the line is not [GET/END] or [POST/END] or [PUT/END] or [DELETE/END]
  864. if(strstr(trimmed_line, "[GET/END]") == NULL &&
  865. strstr(trimmed_line, "[POST/END]") == NULL &&
  866. strstr(trimmed_line, "[PUT/END]") == NULL &&
  867. strstr(trimmed_line, "[DELETE/END]") == NULL) {
  868. strncpy(fhttp.last_response, trimmed_line, RX_BUF_SIZE);
  869. }
  870. }
  871. free(trimmed_line); // Free the allocated memory for trimmed_line
  872. if(fhttp.state != INACTIVE && fhttp.state != ISSUE) {
  873. fhttp.state = RECEIVING;
  874. }
  875. // Uncomment below line to log the data received over UART
  876. // FURI_LOG_I(HTTP_TAG, "Received UART line: %s", line);
  877. // custom function to FlipWiFi
  878. if(fhttp.save_received_data) {
  879. if(!flipper_http_append_to_file(
  880. line, strlen(line), fhttp.just_started_get, fhttp.file_path)) {
  881. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  882. fhttp.just_started_get = false;
  883. fhttp.state = ISSUE;
  884. return;
  885. }
  886. }
  887. // Check if we've started receiving data from a GET request
  888. if(fhttp.started_receiving_get) {
  889. // Restart the timeout timer each time new data is received
  890. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  891. if(strstr(line, "[GET/END]") != NULL) {
  892. FURI_LOG_I(HTTP_TAG, "GET request completed.");
  893. // Stop the timer since we've completed the GET request
  894. furi_timer_stop(fhttp.get_timeout_timer);
  895. fhttp.started_receiving_get = false;
  896. fhttp.just_started_get = false;
  897. fhttp.state = IDLE;
  898. fhttp.save_bytes = false;
  899. fhttp.save_received_data = false;
  900. if(fhttp.is_bytes_request) {
  901. // Search for the binary marker `[GET/END]` in the file buffer
  902. const char marker[] = "[GET/END]";
  903. const size_t marker_len = sizeof(marker) - 1; // Exclude null terminator
  904. for(size_t i = 0; i <= file_buffer_len - marker_len; i++) {
  905. // Check if the marker is found
  906. if(memcmp(&file_buffer[i], marker, marker_len) == 0) {
  907. // Remove the marker by shifting the remaining data left
  908. size_t remaining_len = file_buffer_len - (i + marker_len);
  909. memmove(&file_buffer[i], &file_buffer[i + marker_len], remaining_len);
  910. file_buffer_len -= marker_len;
  911. break;
  912. }
  913. }
  914. // If there is data left in the buffer, append it to the file
  915. if(file_buffer_len > 0) {
  916. if(!flipper_http_append_to_file(
  917. file_buffer, file_buffer_len, false, fhttp.file_path)) {
  918. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  919. }
  920. file_buffer_len = 0;
  921. }
  922. }
  923. fhttp.is_bytes_request = false;
  924. return;
  925. }
  926. // Append the new line to the existing data
  927. if(fhttp.save_received_data &&
  928. !flipper_http_append_to_file(
  929. line, strlen(line), !fhttp.just_started_get, fhttp.file_path)) {
  930. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  931. fhttp.started_receiving_get = false;
  932. fhttp.just_started_get = false;
  933. fhttp.state = IDLE;
  934. return;
  935. }
  936. if(!fhttp.just_started_get) {
  937. fhttp.just_started_get = true;
  938. }
  939. return;
  940. }
  941. // Check if we've started receiving data from a POST request
  942. else if(fhttp.started_receiving_post) {
  943. // Restart the timeout timer each time new data is received
  944. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  945. if(strstr(line, "[POST/END]") != NULL) {
  946. FURI_LOG_I(HTTP_TAG, "POST request completed.");
  947. // Stop the timer since we've completed the POST request
  948. furi_timer_stop(fhttp.get_timeout_timer);
  949. fhttp.started_receiving_post = false;
  950. fhttp.just_started_post = false;
  951. fhttp.state = IDLE;
  952. fhttp.save_bytes = false;
  953. fhttp.save_received_data = false;
  954. if(fhttp.is_bytes_request) {
  955. // Search for the binary marker `[POST/END]` in the file buffer
  956. const char marker[] = "[POST/END]";
  957. const size_t marker_len = sizeof(marker) - 1; // Exclude null terminator
  958. for(size_t i = 0; i <= file_buffer_len - marker_len; i++) {
  959. // Check if the marker is found
  960. if(memcmp(&file_buffer[i], marker, marker_len) == 0) {
  961. // Remove the marker by shifting the remaining data left
  962. size_t remaining_len = file_buffer_len - (i + marker_len);
  963. memmove(&file_buffer[i], &file_buffer[i + marker_len], remaining_len);
  964. file_buffer_len -= marker_len;
  965. break;
  966. }
  967. }
  968. // If there is data left in the buffer, append it to the file
  969. if(file_buffer_len > 0) {
  970. if(!flipper_http_append_to_file(
  971. file_buffer, file_buffer_len, false, fhttp.file_path)) {
  972. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  973. }
  974. file_buffer_len = 0;
  975. }
  976. }
  977. fhttp.is_bytes_request = false;
  978. return;
  979. }
  980. // Append the new line to the existing data
  981. if(fhttp.save_received_data &&
  982. !flipper_http_append_to_file(
  983. line, strlen(line), !fhttp.just_started_post, fhttp.file_path)) {
  984. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  985. fhttp.started_receiving_post = false;
  986. fhttp.just_started_post = false;
  987. fhttp.state = IDLE;
  988. return;
  989. }
  990. if(!fhttp.just_started_post) {
  991. fhttp.just_started_post = true;
  992. }
  993. return;
  994. }
  995. // Check if we've started receiving data from a PUT request
  996. else if(fhttp.started_receiving_put) {
  997. // Restart the timeout timer each time new data is received
  998. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  999. if(strstr(line, "[PUT/END]") != NULL) {
  1000. FURI_LOG_I(HTTP_TAG, "PUT request completed.");
  1001. // Stop the timer since we've completed the PUT request
  1002. furi_timer_stop(fhttp.get_timeout_timer);
  1003. fhttp.started_receiving_put = false;
  1004. fhttp.just_started_put = false;
  1005. fhttp.state = IDLE;
  1006. fhttp.save_bytes = false;
  1007. fhttp.is_bytes_request = false;
  1008. fhttp.save_received_data = false;
  1009. return;
  1010. }
  1011. // Append the new line to the existing data
  1012. if(fhttp.save_received_data &&
  1013. !flipper_http_append_to_file(
  1014. line, strlen(line), !fhttp.just_started_put, fhttp.file_path)) {
  1015. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  1016. fhttp.started_receiving_put = false;
  1017. fhttp.just_started_put = false;
  1018. fhttp.state = IDLE;
  1019. return;
  1020. }
  1021. if(!fhttp.just_started_put) {
  1022. fhttp.just_started_put = true;
  1023. }
  1024. return;
  1025. }
  1026. // Check if we've started receiving data from a DELETE request
  1027. else if(fhttp.started_receiving_delete) {
  1028. // Restart the timeout timer each time new data is received
  1029. furi_timer_restart(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1030. if(strstr(line, "[DELETE/END]") != NULL) {
  1031. FURI_LOG_I(HTTP_TAG, "DELETE request completed.");
  1032. // Stop the timer since we've completed the DELETE request
  1033. furi_timer_stop(fhttp.get_timeout_timer);
  1034. fhttp.started_receiving_delete = false;
  1035. fhttp.just_started_delete = false;
  1036. fhttp.state = IDLE;
  1037. fhttp.save_bytes = false;
  1038. fhttp.is_bytes_request = false;
  1039. fhttp.save_received_data = false;
  1040. return;
  1041. }
  1042. // Append the new line to the existing data
  1043. if(fhttp.save_received_data &&
  1044. !flipper_http_append_to_file(
  1045. line, strlen(line), !fhttp.just_started_delete, fhttp.file_path)) {
  1046. FURI_LOG_E(HTTP_TAG, "Failed to append data to file.");
  1047. fhttp.started_receiving_delete = false;
  1048. fhttp.just_started_delete = false;
  1049. fhttp.state = IDLE;
  1050. return;
  1051. }
  1052. if(!fhttp.just_started_delete) {
  1053. fhttp.just_started_delete = true;
  1054. }
  1055. return;
  1056. }
  1057. // Handle different types of responses
  1058. if(strstr(line, "[SUCCESS]") != NULL || strstr(line, "[CONNECTED]") != NULL) {
  1059. FURI_LOG_I(HTTP_TAG, "Operation succeeded.");
  1060. } else if(strstr(line, "[INFO]") != NULL) {
  1061. FURI_LOG_I(HTTP_TAG, "Received info: %s", line);
  1062. if(fhttp.state == INACTIVE && strstr(line, "[INFO] Already connected to Wifi.") != NULL) {
  1063. fhttp.state = IDLE;
  1064. }
  1065. } else if(strstr(line, "[GET/SUCCESS]") != NULL) {
  1066. FURI_LOG_I(HTTP_TAG, "GET request succeeded.");
  1067. fhttp.started_receiving_get = true;
  1068. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1069. fhttp.state = RECEIVING;
  1070. // for GET request, save data only if it's a bytes request
  1071. fhttp.save_bytes = fhttp.is_bytes_request;
  1072. fhttp.just_started_bytes = true;
  1073. file_buffer_len = 0;
  1074. return;
  1075. } else if(strstr(line, "[POST/SUCCESS]") != NULL) {
  1076. FURI_LOG_I(HTTP_TAG, "POST request succeeded.");
  1077. fhttp.started_receiving_post = true;
  1078. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1079. fhttp.state = RECEIVING;
  1080. // for POST request, save data only if it's a bytes request
  1081. fhttp.save_bytes = fhttp.is_bytes_request;
  1082. fhttp.just_started_bytes = true;
  1083. file_buffer_len = 0;
  1084. return;
  1085. } else if(strstr(line, "[PUT/SUCCESS]") != NULL) {
  1086. FURI_LOG_I(HTTP_TAG, "PUT request succeeded.");
  1087. fhttp.started_receiving_put = true;
  1088. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1089. fhttp.state = RECEIVING;
  1090. return;
  1091. } else if(strstr(line, "[DELETE/SUCCESS]") != NULL) {
  1092. FURI_LOG_I(HTTP_TAG, "DELETE request succeeded.");
  1093. fhttp.started_receiving_delete = true;
  1094. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1095. fhttp.state = RECEIVING;
  1096. return;
  1097. } else if(strstr(line, "[DISCONNECTED]") != NULL) {
  1098. FURI_LOG_I(HTTP_TAG, "WiFi disconnected successfully.");
  1099. } else if(strstr(line, "[ERROR]") != NULL) {
  1100. FURI_LOG_E(HTTP_TAG, "Received error: %s", line);
  1101. fhttp.state = ISSUE;
  1102. return;
  1103. } else if(strstr(line, "[PONG]") != NULL) {
  1104. FURI_LOG_I(HTTP_TAG, "Received PONG response: Wifi Dev Board is still alive.");
  1105. // send command to connect to WiFi
  1106. if(fhttp.state == INACTIVE) {
  1107. fhttp.state = IDLE;
  1108. return;
  1109. }
  1110. }
  1111. if(fhttp.state == INACTIVE && strstr(line, "[PONG]") != NULL) {
  1112. fhttp.state = IDLE;
  1113. } else if(fhttp.state == INACTIVE && strstr(line, "[PONG]") == NULL) {
  1114. fhttp.state = INACTIVE;
  1115. } else {
  1116. fhttp.state = IDLE;
  1117. }
  1118. }
  1119. // Function to trim leading and trailing spaces and newlines from a constant string
  1120. char* trim(const char* str) {
  1121. const char* end;
  1122. char* trimmed_str;
  1123. size_t len;
  1124. // Trim leading space
  1125. while(isspace((unsigned char)*str))
  1126. str++;
  1127. // All spaces?
  1128. if(*str == 0) return strdup(""); // Return an empty string if all spaces
  1129. // Trim trailing space
  1130. end = str + strlen(str) - 1;
  1131. while(end > str && isspace((unsigned char)*end))
  1132. end--;
  1133. // Set length for the trimmed string
  1134. len = end - str + 1;
  1135. // Allocate space for the trimmed string and null terminator
  1136. trimmed_str = (char*)malloc(len + 1);
  1137. if(trimmed_str == NULL) {
  1138. return NULL; // Handle memory allocation failure
  1139. }
  1140. // Copy the trimmed part of the string into trimmed_str
  1141. strncpy(trimmed_str, str, len);
  1142. trimmed_str[len] = '\0'; // Null terminate the string
  1143. return trimmed_str;
  1144. }
  1145. /**
  1146. * @brief Process requests and parse JSON data asynchronously
  1147. * @param http_request The function to send the request
  1148. * @param parse_json The function to parse the JSON
  1149. * @return true if successful, false otherwise
  1150. */
  1151. bool flipper_http_process_response_async(bool (*http_request)(void), bool (*parse_json)(void)) {
  1152. if(http_request()) // start the async request
  1153. {
  1154. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  1155. fhttp.state = RECEIVING;
  1156. } else {
  1157. FURI_LOG_E(HTTP_TAG, "Failed to send request");
  1158. return false;
  1159. }
  1160. while(fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0) {
  1161. // Wait for the request to be received
  1162. furi_delay_ms(100);
  1163. }
  1164. furi_timer_stop(fhttp.get_timeout_timer);
  1165. if(!parse_json()) // parse the JSON before switching to the view (synchonous)
  1166. {
  1167. FURI_LOG_E(HTTP_TAG, "Failed to parse the JSON...");
  1168. return false;
  1169. }
  1170. return true;
  1171. }