nfc_playlist.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <furi.h>
  2. #include <nfc/nfc.h>
  3. #include <nfc/nfc_common.h>
  4. #include <nfc/nfc_device.h>
  5. #include <nfc/nfc_device_i.h>
  6. #include <nfc/nfc_listener.h>
  7. #include <nfc/nfc_poller.h>
  8. #include <nfc/nfc_scanner.h>
  9. #include <nfc/protocols/iso14443_3a/iso14443_3a.h>
  10. #include <storage/storage.h>
  11. #include <toolbox/stream/stream.h>
  12. #include <toolbox/stream/file_stream.h>
  13. // Define log tag
  14. #define TAG "NfcPlaylist"
  15. // Application entry point
  16. int32_t nfc_playlist_main(void* p) {
  17. // Mark argument as unused
  18. UNUSED(p);
  19. // Just a little debug toggle
  20. const bool DEBUG = false;
  21. Storage* storage = furi_record_open(RECORD_STORAGE);
  22. Stream* stream = file_stream_alloc(storage);
  23. FuriString* line = furi_string_alloc();
  24. NfcDevice* nfcDevice = nfc_device_alloc();
  25. Nfc* nfc = nfc_alloc();
  26. // Read file
  27. if(file_stream_open(stream, APP_DATA_PATH("playlistTest.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
  28. while(stream_read_line(stream, line)) {
  29. // Store file location
  30. const char* fileLocation = strcat("/ext/nfc/",furi_string_get_cstr(line));
  31. // Load file
  32. if (nfc_device_load(nfcDevice, fileLocation)) {
  33. if (DEBUG) {FURI_LOG_I(TAG, "Loaded file");}
  34. // Get protocol
  35. const NfcProtocol protocol = nfc_device_get_protocol(nfcDevice);
  36. // Get listern
  37. NfcListener* mfu_listener = nfc_listener_alloc(nfc, protocol, nfc_device_get_data(nfcDevice, protocol));
  38. // Start listener
  39. nfc_listener_start(mfu_listener, NULL, NULL);
  40. // Worst timer ever
  41. int counter = 0;
  42. while(true) {
  43. furi_delay_ms(50);
  44. counter++;
  45. if (counter == 100) {
  46. break;
  47. }
  48. }
  49. // Stop listener && free
  50. nfc_listener_stop(mfu_listener);
  51. nfc_listener_free(mfu_listener);
  52. } else {
  53. if (DEBUG) {FURI_LOG_E(TAG, "Failed to load file");}
  54. }
  55. // output file location
  56. if (DEBUG) {FURI_LOG_I(TAG, "%s", fileLocation);}
  57. // clear instance
  58. nfc_device_clear(nfcDevice);
  59. }
  60. } else {
  61. if (DEBUG) {FURI_LOG_E(TAG, "Failed to open file");}
  62. }
  63. // Free/close resources
  64. furi_string_free(line);
  65. file_stream_close(stream);
  66. stream_free(stream);
  67. nfc_device_free(nfcDevice);
  68. nfc_free(nfc);
  69. // Close storage
  70. furi_record_close(RECORD_STORAGE);
  71. return 0;
  72. }