flipper_http.c 46 KB

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