flipper_http.c 56 KB

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