Kaynağa Gözat

Worker Added!

- Improves how the NFC's are handled in the main app
- Makes it so it can now handle multiple NFC's and doesn't end up looping the same one
acegoal07 2 yıl önce
ebeveyn
işleme
a27df3d00d
4 değiştirilmiş dosya ile 165 ekleme ve 54 silme
  1. 14 54
      nfc_playlist.c
  2. 114 0
      nfc_playlist_worker.c
  3. 34 0
      nfc_playlist_worker.h
  4. 3 0
      playlist.txt

+ 14 - 54
nfc_playlist.c

@@ -5,15 +5,13 @@
 #include <nfc/nfc_device.h>
 #include <nfc/nfc_device.h>
 #include <nfc/nfc_device_i.h>
 #include <nfc/nfc_device_i.h>
 #include <nfc/nfc_listener.h>
 #include <nfc/nfc_listener.h>
-#include <nfc/nfc_poller.h>
-#include <nfc/nfc_scanner.h>
-
-#include <nfc/protocols/iso14443_3a/iso14443_3a.h>
 
 
 #include <storage/storage.h>
 #include <storage/storage.h>
 #include <toolbox/stream/stream.h>
 #include <toolbox/stream/stream.h>
 #include <toolbox/stream/file_stream.h>
 #include <toolbox/stream/file_stream.h>
 
 
+#include <nfc_playlist_worker.h>
+
 // Define log tag
 // Define log tag
 #define TAG "NfcPlaylist"
 #define TAG "NfcPlaylist"
 
 
@@ -22,70 +20,32 @@ int32_t nfc_playlist_main(void* p) {
    // Mark argument as unused
    // Mark argument as unused
    UNUSED(p);
    UNUSED(p);
    // Just a little debug toggle
    // Just a little debug toggle
-   const bool DEBUG = false;
-
+   const bool DEBUG = true;
+   // open/alloc resources
    Storage* storage = furi_record_open(RECORD_STORAGE);
    Storage* storage = furi_record_open(RECORD_STORAGE);
    Stream* stream = file_stream_alloc(storage);
    Stream* stream = file_stream_alloc(storage);
    FuriString* line = furi_string_alloc();
    FuriString* line = furi_string_alloc();
-
+   NfcPlaylistWorker* nfc_playlist_worker = nfc_playlist_worker_alloc();
    // Read file
    // Read file
-   if(file_stream_open(stream, APP_DATA_PATH("playlistTest.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
-
-      // Get resources
-      NfcDevice* nfc_device = nfc_device_alloc();
-      Nfc* nfc = nfc_alloc();
+   if(file_stream_open(stream, APP_DATA_PATH("playlist.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
 
 
+      // read the file line by line and print the text 
       while(stream_read_line(stream, line)) {
       while(stream_read_line(stream, line)) {
-         // Store file location
-         const char* fileLocation = strcat("/ext/nfc/", furi_string_get_cstr(line));
-
-         // Load file
-         if (nfc_device_load(nfc_device, fileLocation)) {
-
-            if (DEBUG) {FURI_LOG_I(TAG, "Loaded file");}
-
-            // Get protocol
-            const NfcProtocol nfc_protocol = nfc_device_get_protocol(nfc_device);
-            // Get listern
-            NfcListener* listener = nfc_listener_alloc(nfc, nfc_protocol, nfc_device_get_data(nfc_device, nfc_protocol));
-            // Start listener
-            nfc_listener_start(listener, NULL, NULL);
-
-            // Worst timer ever
-            int counter = 0;
-            while(true) {
-               furi_delay_ms(50);
-               counter++;
-               if (counter == 100) {
-                  break;
-               }
-            }
-
-            // Stop listener && free
-            nfc_listener_stop(listener);
-            nfc_listener_free(listener);
-            
-         } else {
-            if (DEBUG) {FURI_LOG_E(TAG, "Failed to load file");}
-         }
-         // output file location
-         if (DEBUG) {FURI_LOG_I(TAG, "%s", fileLocation);}
-         // clear instance
-         nfc_device_clear(nfc_device);
+         if (DEBUG) {FURI_LOG_I(TAG, "Read line: %s", furi_string_get_cstr(line));}
+         nfc_playlist_worker_set_nfc_data(nfc_playlist_worker, (char*)furi_string_get_cstr(line));
+         nfc_playlist_worker_start(nfc_playlist_worker);
+         furi_delay_ms(1500);
+         nfc_playlist_worker_stop(nfc_playlist_worker);
+         furi_string_reset(line);
       }
       }
-
-      // Free/close resources
-      nfc_device_free(nfc_device);
-      nfc_free(nfc);
-
    } else {
    } else {
       if (DEBUG) {FURI_LOG_E(TAG, "Failed to open file");}
       if (DEBUG) {FURI_LOG_E(TAG, "Failed to open file");}
    }
    }
-
    // Free/close resources
    // Free/close resources
    furi_string_free(line);
    furi_string_free(line);
    file_stream_close(stream);
    file_stream_close(stream);
    stream_free(stream);
    stream_free(stream);
+   nfc_playlist_worker_free(nfc_playlist_worker);
 
 
    // Close storage
    // Close storage
    furi_record_close(RECORD_STORAGE);
    furi_record_close(RECORD_STORAGE);

+ 114 - 0
nfc_playlist_worker.c

@@ -0,0 +1,114 @@
+#include "nfc_playlist_worker.h"
+
+/// @brief nfc_playlist_worker_alloc()
+/// @return
+NfcPlaylistWorker* nfc_playlist_worker_alloc() {
+    NfcPlaylistWorker* nfc_playlist_worker = malloc(sizeof(NfcPlaylistWorker));
+    // Worker thread attributes
+    nfc_playlist_worker->thread = furi_thread_alloc_ex(
+        "NfcPlaylistWorker", 8192, nfc_playlist_worker_task, nfc_playlist_worker);
+    nfc_playlist_worker->state = NfcPlaylistWorkerState_Stopped;
+
+    nfc_playlist_worker->nfc = nfc_alloc();
+    nfc_playlist_worker->nfc_device = nfc_device_alloc();
+
+    return nfc_playlist_worker;
+}
+
+/// @brief nfc_playlist_worker_free()
+/// @param nfc_playlist_worker
+void nfc_playlist_worker_free(NfcPlaylistWorker* nfc_playlist_worker) {
+    furi_assert(nfc_playlist_worker);
+    furi_thread_free(nfc_playlist_worker->thread);
+
+    nfc_free(nfc_playlist_worker->nfc);
+    nfc_device_free(nfc_playlist_worker->nfc_device);
+
+    free(nfc_playlist_worker);
+}
+
+/// @brief nfc_playlist_worker_stop()
+/// @param nfc_playlist_worker
+void nfc_playlist_worker_stop(NfcPlaylistWorker* nfc_playlist_worker) {
+    furi_assert(nfc_playlist_worker);
+    if(nfc_playlist_worker->state != NfcPlaylistWorkerState_Stopped) {
+        nfc_playlist_worker->state = NfcPlaylistWorkerState_Stopped;
+        furi_thread_join(nfc_playlist_worker->thread);
+    }
+}
+
+/// @brief nfc_playlist_worker_start()
+/// @param nfc_playlist_worker
+void nfc_playlist_worker_start(NfcPlaylistWorker* nfc_playlist_worker) {
+    furi_assert(nfc_playlist_worker);
+    nfc_playlist_worker->state = NfcPlaylistWorkerState_Emulating;
+    furi_thread_start(nfc_playlist_worker->thread);
+}
+
+/// @brief nfc_playlist_worker_task()
+/// @param context
+/// @return
+int32_t nfc_playlist_worker_task(void* context) {
+    NfcPlaylistWorker* nfc_playlist_worker = context;
+
+    if(nfc_playlist_worker->state == NfcPlaylistWorkerState_Emulating) {
+
+        nfc_playlist_worker->nfc_listener =
+            nfc_listener_alloc(nfc_playlist_worker->nfc, 
+                nfc_playlist_worker->nfc_protocol, 
+                nfc_device_get_data(nfc_playlist_worker->nfc_device, nfc_playlist_worker->nfc_protocol)
+            );
+        nfc_listener_start(nfc_playlist_worker->nfc_listener, NULL, NULL);
+
+
+        int counter = 0;
+        while(true) {
+            furi_delay_ms(50);
+            counter++;
+            if (counter == 100) {
+                break;
+            }
+        }
+
+        // while(nfc_playlist_worker->state == NfcPlaylistWorkerState_Emulating) {
+        //     furi_delay_ms(50);
+        // }
+
+        nfc_listener_stop(nfc_playlist_worker->nfc_listener);
+        nfc_listener_free(nfc_playlist_worker->nfc_listener);
+    }
+
+    nfc_playlist_worker->state = NfcPlaylistWorkerState_Stopped;
+
+    return 0;
+}
+
+/// @brief nfc_playlist_worker_is_emulating()
+/// @param nfc_playlist_worker
+/// @return
+bool nfc_playlist_worker_is_emulating(NfcPlaylistWorker* nfc_playlist_worker) {
+    if(nfc_playlist_worker->state == NfcPlaylistWorkerState_Emulating) {
+        return true;
+    }
+    return false;
+}
+
+/// @brief nfc_playlist_worker_set_nfc_data()
+/// @param nfc_playlist_worker
+/// @param nfc_data
+void nfc_playlist_worker_set_nfc_data(NfcPlaylistWorker* nfc_playlist_worker, char* file_path) {
+
+    FURI_LOG_I("NfcPlaylistWorker", "nfc_playlist_worker_set_nfc_data: %s", file_path);
+    
+    nfc_device_clear(nfc_playlist_worker->nfc_device);
+    nfc_device_load(nfc_playlist_worker->nfc_device, file_path);
+
+    nfc_playlist_worker->nfc_protocol = nfc_device_get_protocol(nfc_playlist_worker->nfc_device);
+}
+
+/// @brief nfc_playlist_worker_get_nfc_data()
+/// @param nfc_playlist_worker
+/// @return
+NfcDeviceData* nfc_playlist_worker_get_nfc_data(NfcPlaylistWorker* nfc_playlist_worker) {
+    return nfc_playlist_worker->nfc_data;
+}

+ 34 - 0
nfc_playlist_worker.h

@@ -0,0 +1,34 @@
+#pragma once
+#include <furi.h>
+#include <furi_hal.h>
+
+#include <nfc/nfc.h>
+#include <nfc/nfc_device.h>
+#include <nfc/nfc_listener.h>
+
+typedef enum NfcPlaylistWorkerState {
+   NfcPlaylistWorkerState_Emulating,
+   NfcPlaylistWorkerState_Stopped,
+} NfcPlaylistWorkerState;
+
+typedef struct NfcPlaylistWorker {
+   FuriThread* thread;
+   NfcPlaylistWorkerState state;
+   NfcListener* nfc_listener;
+   NfcDevice* nfc_device;
+   NfcProtocol nfc_protocol;
+   NfcDeviceData* nfc_data;
+   Nfc* nfc;
+} NfcPlaylistWorker;
+
+/// Worker
+NfcPlaylistWorker* nfc_playlist_worker_alloc();
+void nfc_playlist_worker_free(NfcPlaylistWorker* nfc_playlist_worker);
+void nfc_playlist_worker_stop(NfcPlaylistWorker* nfc_playlist_worker);
+void nfc_playlist_worker_start(NfcPlaylistWorker* nfc_playlist_worker);
+// task
+int32_t nfc_playlist_worker_task(void* context);
+//
+bool nfc_playlist_worker_is_emulating(NfcPlaylistWorker* nfc_playlist_worker);
+void nfc_playlist_worker_set_nfc_data(NfcPlaylistWorker* nfc_playlist_worker, char* file_path);
+NfcDeviceData* nfc_playlist_worker_get_nfc_data(NfcPlaylistWorker* nfc_playlist_worker);

+ 3 - 0
playlist.txt

@@ -0,0 +1,3 @@
+/ext/nfc/RickRoll.nfc
+/ext/nfc/Website.nfc
+/ext/nfc/Phone_number.nfc