flipper_http.c 55 KB

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