nfc_playlist.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // Read file
  25. if(file_stream_open(stream, APP_DATA_PATH("playlistTest.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
  26. // Get resources
  27. NfcDevice* nfc_device = nfc_device_alloc();
  28. Nfc* nfc = nfc_alloc();
  29. while(stream_read_line(stream, line)) {
  30. // Store file location
  31. const char* fileLocation = strcat("/ext/nfc/", furi_string_get_cstr(line));
  32. // Load file
  33. if (nfc_device_load(nfc_device, fileLocation)) {
  34. if (DEBUG) {FURI_LOG_I(TAG, "Loaded file");}
  35. // Get protocol
  36. const NfcProtocol nfc_protocol = nfc_device_get_protocol(nfc_device);
  37. // Get listern
  38. NfcListener* listener = nfc_listener_alloc(nfc, nfc_protocol, nfc_device_get_data(nfc_device, nfc_protocol));
  39. // Start listener
  40. nfc_listener_start(listener, NULL, NULL);
  41. // Worst timer ever
  42. int counter = 0;
  43. while(true) {
  44. furi_delay_ms(50);
  45. counter++;
  46. if (counter == 100) {
  47. break;
  48. }
  49. }
  50. // Stop listener && free
  51. nfc_listener_stop(listener);
  52. nfc_listener_free(listener);
  53. } else {
  54. if (DEBUG) {FURI_LOG_E(TAG, "Failed to load file");}
  55. }
  56. // output file location
  57. if (DEBUG) {FURI_LOG_I(TAG, "%s", fileLocation);}
  58. // clear instance
  59. nfc_device_clear(nfc_device);
  60. }
  61. // Free/close resources
  62. nfc_device_free(nfc_device);
  63. nfc_free(nfc);
  64. } else {
  65. if (DEBUG) {FURI_LOG_E(TAG, "Failed to open file");}
  66. }
  67. // Free/close resources
  68. furi_string_free(line);
  69. file_stream_close(stream);
  70. stream_free(stream);
  71. // Close storage
  72. furi_record_close(RECORD_STORAGE);
  73. return 0;
  74. }