Skip to content

Commit

Permalink
LPCD: Only wake up if card is present in NVS
Browse files Browse the repository at this point in the history
  • Loading branch information
fox34 committed Nov 26, 2024
1 parent 9182dbd commit 1ae02e6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/LogMessages_DE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ const char ftpServerStarted[] = "FTP-Server gestartet";
const char freeHeapAfterSetup[] = "Freier Heap-Speicher nach Setup-Routine";
const char tryStaticIpConfig[] = "Statische IP-Konfiguration wird durchgeführt...";
const char staticIPConfigFailed[] = "Statische IP-Konfiguration fehlgeschlagen";
const char wakeUpRfidNoCard[] = "ESP32 wurde vom Kartenleser aus dem Deepsleep aufgeweckt. Allerdings wurde keine Karte gefunden. Gehe zurück in den Deepsleep...";
const char wakeUpRfidNoCard[] = "ESP32 wurde vom Kartenleser aus dem Deepsleep aufgeweckt. Allerdings wurde keine bekannte Karte gefunden. Gehe zurück in den Deepsleep...";
const char lowPowerCardSuccess[] = "Kartenerkennung via 'low power' erfolgreich durchgeführt";
const char rememberLastVolume[] = "Lautstärke vor dem letzten Shutdown wird wiederhergestellt. Dies überschreibt die Einstellung der initialen Lautstärke aus der GUI.";
const char unableToStartFtpServer[] = "Der FTP-Server konnte nicht gestartet werden. Entweder weil er ist bereits gestartet oder kein WLAN verfügbar ist.";
Expand Down
2 changes: 1 addition & 1 deletion src/LogMessages_EN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const char freeHeapAfterSetup[] = "Free heap after setup";
const char ftpServerStarted[] = "FTP-Server started";
const char tryStaticIpConfig[] = "Performing IP-configuration...";
const char staticIPConfigFailed[] = "IP-configuration failed";
const char wakeUpRfidNoCard[] = "Wakeup caused by low power card-detection. RF-field changed but no card on reader was found. So I'll return back to sleep now...";
const char wakeUpRfidNoCard[] = "Wakeup caused by low power card-detection. RF-field changed but no known card on reader was found. So I'll return back to sleep now...";
const char lowPowerCardSuccess[] = "Switch to low power card-detection: success";
const char rememberLastVolume[] = "Restored volume used before last shutdown. This overwrites the initial volume configured via webgui.";
const char unableToStartFtpServer[] = "FTP-server cannot be started. This is because FTP-service is already active or because WiFi is unavailable.";
Expand Down
2 changes: 1 addition & 1 deletion src/LogMessages_FR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const char freeHeapAfterSetup[] = "Espace libre après la configuration";
const char ftpServerStarted[] = "Serveur FTP démarré";
const char tryStaticIpConfig[] = "Configuration IP statique en cours...";
const char staticIPConfigFailed[] = "Échec de la configuration IP statique";
const char wakeUpRfidNoCard[] = "Réveil causé par la détection de carte à faible puissance. Le champ RF a changé mais aucune carte sur le lecteur n'a été trouvée. Je vais donc retourner en mode veille maintenant...";
const char wakeUpRfidNoCard[] = "Réveil causé par la détection de carte à faible puissance. Le champ RF a changé mais aucune carte connu sur le lecteur n'a été trouvée. Je vais donc retourner en mode veille maintenant...";
const char lowPowerCardSuccess[] = "Passage à la détection de carte à faible puissance : réussite";
const char rememberLastVolume[] = "Volume utilisé avant le dernier arrêt restauré. Cela écrase le volume initial configuré via l'interface Web.";
const char unableToStartFtpServer[] = "Le serveur FTP ne peut pas être démarré. Cela est dû au fait que le service FTP est déjà actif ou parce que le WiFi est indisponible.";
Expand Down
59 changes: 43 additions & 16 deletions src/RfidPn5180.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ void Rfid_EnableLpcd(void) {
#endif
}

// wake up from LPCD, check card is present. This works only for ISO-14443 compatible cards
// wake up from LPCD, check card is present in NVS
void Rfid_WakeupCheck(void) {
#ifdef PN5180_ENABLE_LPCD
// disable pin hold from deep sleep
Expand All @@ -387,36 +387,63 @@ void Rfid_WakeupCheck(void) {
#if (RFID_IRQ >= 0 && RFID_IRQ <= MAX_GPIO)
pinMode(RFID_IRQ, INPUT);
#endif

// read card serial to check NVS for entries
uint8_t uid[10];
bool isCardPresent;
bool cardInNVS = false;

static PN5180ISO14443 nfc14443(RFID_CS, RFID_BUSY, RFID_RST);
nfc14443.begin();
nfc14443.reset();
// enable RF field
nfc14443.setupRF();
// 1.check for ISO-14443 card present
bool isCardPresent = nfc14443.isCardPresent();

// 1. check for ISO-14443 card present
isCardPresent = nfc14443.readCardSerial(uid) >= 4;

// 2. check for ISO-15693 card present
if (!isCardPresent) {
// 2.check for ISO-15693 card present
static PN5180ISO15693 nfc15693(RFID_CS, RFID_BUSY, RFID_RST);
nfc15693.begin();
nfc15693.setupRF();
uint8_t uid[10];
// do not handle encrypted cards in any way here
isCardPresent = nfc15693.getInventory(uid) == ISO15693_EC_OK;
}
if (!isCardPresent) {
// no card found, go back to deep sleep

// check for card id in NVS
if (isCardPresent) {
// uint8_t[10] -> char[cardIdStringSize]
char tagId[cardIdStringSize];
size_t pos = 0;
for (size_t i = 0; i < 10; i++) {
pos += snprintf(tagId + pos, cardIdStringSize - pos, "%03d", uid[i]);
}
tagId[cardIdStringSize - 1] = '\0';

// Try to lookup tagId in NVS
if (gPrefsRfid.isKey(tagId)) {
cardInNVS = gPrefsRfid.getString(tagId, "-1").compareTo("-1");
}
}

if (!cardInNVS) {
// no card found or card not in NVS, go back to deep sleep
nfc14443.reset();
uint16_t wakeupCounterInMs = 0x3FF; // needs to be in the range of 0x0 - 0xA82. max wake-up time is 2960 ms.
if (nfc14443.switchToLPCD(wakeupCounterInMs)) {
Log_Println(lowPowerCardSuccess, LOGLEVEL_INFO);
// configure wakeup pin for deep-sleep wake-up, use ext1
#if (RFID_IRQ >= 0 && RFID_IRQ <= MAX_GPIO)
// configure wakeup pin for deep-sleep wake-up, use ext1. For a real GPIO only, not PE
esp_sleep_enable_ext1_wakeup((1ULL << (RFID_IRQ)), ESP_EXT1_WAKEUP_ALL_LOW);
#endif
#if (defined(PORT_EXPANDER_ENABLE) && (RFID_IRQ > 99))
// reset IRQ state on port-expander
Port_Exit();
#endif

// configure wakeup pin for deep-sleep wake-up, use ext1
#if (RFID_IRQ >= 0 && RFID_IRQ <= MAX_GPIO)
// configure wakeup pin for deep-sleep wake-up, use ext1. For a real GPIO only, not PE
esp_sleep_enable_ext1_wakeup((1ULL << (RFID_IRQ)), ESP_EXT1_WAKEUP_ALL_LOW);
#endif
#if (defined(PORT_EXPANDER_ENABLE) && (RFID_IRQ > 99))
// reset IRQ state on port-expander
Port_Exit();
#endif

// freeze pin states in deep sleep
gpio_hold_en(gpio_num_t(RFID_CS)); // CS/NSS
gpio_hold_en(gpio_num_t(RFID_RST)); // RST
Expand Down
11 changes: 10 additions & 1 deletion src/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ volatile uint8_t System_OperationMode;
void System_SleepHandler(void);
void System_DeepSleepManager(void);

// Init only NVS required for LPCD
void System_Init_LPCD(void) {
#ifdef PN5180_ENABLE_LPCD
gPrefsRfid.begin(prefsRfidNamespace);
#endif
}

void System_Init(void) {
srand(esp_random());

gPrefsRfid.begin(prefsRfidNamespace);
#ifndef PN5180_ENABLE_LPCD
gPrefsRfid.begin(prefsRfidNamespace);
#endif
gPrefsSettings.begin(prefsSettingsNamespace);

// Get maximum inactivity-time from NVS
Expand Down
1 change: 1 addition & 0 deletions src/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern Preferences gPrefsRfid;
extern Preferences gPrefsSettings;
extern TaskHandle_t AudioTaskHandle;

void System_Init_LPCD(void);
void System_Init(void);
void System_Cyclic(void);
void System_UpdateActivityTimer(void);
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ void setup() {

// Make sure all wakeups can be enabled *before* initializing RFID, which can enter sleep immediately
Button_Init(); // To preseed internal button-storage with values

#ifdef PN5180_ENABLE_LPCD
System_Init_LPCD();
Rfid_Init();
#endif

Expand Down

0 comments on commit 1ae02e6

Please sign in to comment.