From ff20dfc465391fdb8ce0b9d1a0dc790c8ed7a047 Mon Sep 17 00:00:00 2001
From: miohna <miohna@freenet.de>
Date: Thu, 11 Jan 2024 21:26:47 +0100
Subject: [PATCH 1/8] Bugfix: reenable Mifare Ultralight C support

Make use of read_id function from pirc552 package to reenable Mifare
Ultralight C support.

ATTENTION:
Previously a non-standard representation of NFC card uid was returned by
the Mfrc522Reader class. With this change a standard representation of
the NFCs uid is return which breaks all currently stored links between
cards and actions.
---
 scripts/Reader.py.experimental | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/scripts/Reader.py.experimental b/scripts/Reader.py.experimental
index b34479565..c3fa092b6 100755
--- a/scripts/Reader.py.experimental
+++ b/scripts/Reader.py.experimental
@@ -54,7 +54,7 @@ class Mfrc522Reader(object):
         import pirc522
         self.device = pirc522.RFID()
 
-    def readCard(self):
+    def readCard_legacy(self):
         # Scan for cards
         self.device.wait_for_tag()
         (error, tag_type) = self.device.request()
@@ -70,6 +70,17 @@ class Mfrc522Reader(object):
         logger.debug("No Device ID found.")
         return None
 
+    def readCard(self):
+        # Scan for cards
+        uid = self.device.read_id(as_number=True)
+        if not uid:
+            logger.debug("No Device ID found.")
+            return None
+        card_id = str(uid)
+        logger.info("Card detected.")
+        logger.info(card_id)
+        return card_id
+
     @staticmethod
     def cleanup():
         GPIO.cleanup()

From 1d9cf0c45b3ce63ed819b1012374247cc9e92bcc Mon Sep 17 00:00:00 2001
From: miohna <miohna@freenet.de>
Date: Wed, 17 Jan 2024 23:49:30 +0100
Subject: [PATCH 2/8] Add legacy mode option

Add a legacy mode similar to 'future3' implementation.
Use the legacy mode to allow correction of card uid read with Mfrc522
while keeping the class backward compatibil, cf. 'future3' handling.
---
 scripts/Reader.py.experimental   | 10 +++++++---
 scripts/inc.writeGlobalConfig.sh | 12 ++++++++++++
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/scripts/Reader.py.experimental b/scripts/Reader.py.experimental
index c3fa092b6..93a72247f 100755
--- a/scripts/Reader.py.experimental
+++ b/scripts/Reader.py.experimental
@@ -50,9 +50,11 @@ class UsbReader(object):
 
 
 class Mfrc522Reader(object):
-    def __init__(self):
+    def __init__(self, mode_legacy=True):
         import pirc522
         self.device = pirc522.RFID()
+        self.mode_legacy = mode_legacy
+        self.readCard = self.readCard_legacy if self.mode_legacy else self.readCard_normal
 
     def readCard_legacy(self):
         # Scan for cards
@@ -70,7 +72,7 @@ class Mfrc522Reader(object):
         logger.debug("No Device ID found.")
         return None
 
-    def readCard(self):
+    def readCard_normal(self):
         # Scan for cards
         uid = self.device.read_id(as_number=True)
         if not uid:
@@ -199,9 +201,11 @@ class Reader(object):
         else:
             with open(path + '/deviceName.txt', 'r') as f:
                 device_name = f.read().rstrip().split(';', 1)[0]
+            with open(path + '/../settings/Mode_Legacy', 'r') as f:
+                mode_legacy = f.read().rstrip().split(';', 1)[0] == 'ON'
 
             if device_name == 'MFRC522':
-                self.reader = Mfrc522Reader()
+                self.reader = Mfrc522Reader(mode_legacy)
             elif device_name == 'RDM6300':
                 # The Rdm6300Reader supports 2 Additional Number Formats which can bee choosen by an optional parameter dictionary:
                 # {'numberformat':'card_id_float'} or {'numberformat':'card_id_dec'}
diff --git a/scripts/inc.writeGlobalConfig.sh b/scripts/inc.writeGlobalConfig.sh
index 856c54fb4..0453442b4 100755
--- a/scripts/inc.writeGlobalConfig.sh
+++ b/scripts/inc.writeGlobalConfig.sh
@@ -109,6 +109,16 @@ fi
 # 2. then|or read value from file
 SECONDSWIPEPAUSECONTROLS=`cat $PATHDATA/../settings/Second_Swipe_Pause_Controls`
 
+##############################################
+# Legacy mode
+# 1. create a default if file does not exist
+if [ ! -f $PATHDATA/../settings/Mode_Legacy ]; then
+    echo "ON" > $PATHDATA/../settings/Mode_Legacy
+    chmod 777 $PATHDATA/../settings/Mode_Legacy
+fi
+# 2. then|or read value from file
+MODELEGACY=`cat $PATHDATA/../settings/Mode_Legacy`
+
 ##############################################
 # Audio_iFace_Name
 # 1. create a default if file does not exist
@@ -332,6 +342,7 @@ CMDSEEKBACK=`grep 'CMDSEEKBACK' $PATHDATA/../settings/rfid_trigger_play.conf|tai
 # SECONDSWIPE
 # SECONDSWIPEPAUSE
 # SECONDSWIPEPAUSECONTROLS
+# MODELEGACY
 # AUDIOIFACENAME
 # AUDIOIFACEACTIVE
 # VOLUMEMANAGER
@@ -369,6 +380,7 @@ echo "SWIPEORPLACE=\"${SWIPEORPLACE}\"" >> "${PATHDATA}/../settings/global.conf"
 echo "SECONDSWIPE=\"${SECONDSWIPE}\"" >> "${PATHDATA}/../settings/global.conf"
 echo "SECONDSWIPEPAUSE=\"${SECONDSWIPEPAUSE}\"" >> "${PATHDATA}/../settings/global.conf"
 echo "SECONDSWIPEPAUSECONTROLS=\"${SECONDSWIPEPAUSECONTROLS}\"" >> "${PATHDATA}/../settings/global.conf"
+echo "MODELEGACY=\"${MODELEGACY}\"" >> "${PATHDATA}/../settings/global.conf"
 echo "AUDIOIFACENAME=\"${AUDIOIFACENAME}\"" >> "${PATHDATA}/../settings/global.conf"
 echo "AUDIOIFACEACTIVE=\"${AUDIOIFACEACTIVE}\"" >> "${PATHDATA}/../settings/global.conf"
 echo "VOLUMEMANAGER=\"${VOLUMEMANAGER}\"" >> "${PATHDATA}/../settings/global.conf"

From a2b4b70b5699bd7204db91a8d83173ae66d76aa5 Mon Sep 17 00:00:00 2001
From: miohna <miohna@freenet.de>
Date: Thu, 18 Jan 2024 21:24:27 +0100
Subject: [PATCH 3/8] Add error handling for missing `Mode_Legacy` file.

---
 scripts/Reader.py.experimental | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/scripts/Reader.py.experimental b/scripts/Reader.py.experimental
index 93a72247f..1cea01662 100755
--- a/scripts/Reader.py.experimental
+++ b/scripts/Reader.py.experimental
@@ -201,8 +201,11 @@ class Reader(object):
         else:
             with open(path + '/deviceName.txt', 'r') as f:
                 device_name = f.read().rstrip().split(';', 1)[0]
-            with open(path + '/../settings/Mode_Legacy', 'r') as f:
-                mode_legacy = f.read().rstrip().split(';', 1)[0] == 'ON'
+            try:
+                with open(path + '/../settings/Mode_Legacy', 'r') as f:
+                    mode_legacy = f.read().rstrip().split(';', 1)[0] == 'ON'
+            except FileNotFoundError:
+                mode_legacy = True
 
             if device_name == 'MFRC522':
                 self.reader = Mfrc522Reader(mode_legacy)

From d618ec0db13a052664effedcf585677624a13bb5 Mon Sep 17 00:00:00 2001
From: miohna <miohna@freenet.de>
Date: Wed, 24 Jan 2024 21:52:15 +0100
Subject: [PATCH 4/8] Change legacy mode to UID option for rc522

Modified general legacy mode to a specific option of the rc522 reader
that allows to change between the read of the usual UID and the read of
the previous custom implementation of card ID.
---
 scripts/Reader.py.experimental   | 14 +++++++-------
 scripts/inc.writeGlobalConfig.sh | 14 +++++++-------
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/scripts/Reader.py.experimental b/scripts/Reader.py.experimental
index 1cea01662..12b2b604f 100755
--- a/scripts/Reader.py.experimental
+++ b/scripts/Reader.py.experimental
@@ -50,11 +50,11 @@ class UsbReader(object):
 
 
 class Mfrc522Reader(object):
-    def __init__(self, mode_legacy=True):
+    def __init__(self, readmode_uid=False):
         import pirc522
         self.device = pirc522.RFID()
-        self.mode_legacy = mode_legacy
-        self.readCard = self.readCard_legacy if self.mode_legacy else self.readCard_normal
+        self.readmode_uid = readmode_uid
+        self.readCard = self.readCard_normal if self.readmode_uid else self.readCard_legacy
 
     def readCard_legacy(self):
         # Scan for cards
@@ -202,13 +202,13 @@ class Reader(object):
             with open(path + '/deviceName.txt', 'r') as f:
                 device_name = f.read().rstrip().split(';', 1)[0]
             try:
-                with open(path + '/../settings/Mode_Legacy', 'r') as f:
-                    mode_legacy = f.read().rstrip().split(';', 1)[0] == 'ON'
+                with open(path + '/../settings/Rfidreader_Rc522_Readmode_UID', 'r') as f:
+                    readmode_uid = f.read().rstrip().split(';', 1)[0] == 'ON'
             except FileNotFoundError:
-                mode_legacy = True
+                readmode_uid = False
 
             if device_name == 'MFRC522':
-                self.reader = Mfrc522Reader(mode_legacy)
+                self.reader = Mfrc522Reader(readmode_uid)
             elif device_name == 'RDM6300':
                 # The Rdm6300Reader supports 2 Additional Number Formats which can bee choosen by an optional parameter dictionary:
                 # {'numberformat':'card_id_float'} or {'numberformat':'card_id_dec'}
diff --git a/scripts/inc.writeGlobalConfig.sh b/scripts/inc.writeGlobalConfig.sh
index 0453442b4..84aa559ef 100755
--- a/scripts/inc.writeGlobalConfig.sh
+++ b/scripts/inc.writeGlobalConfig.sh
@@ -110,14 +110,14 @@ fi
 SECONDSWIPEPAUSECONTROLS=`cat $PATHDATA/../settings/Second_Swipe_Pause_Controls`
 
 ##############################################
-# Legacy mode
+# RFID reader rc522 readmode UID
 # 1. create a default if file does not exist
-if [ ! -f $PATHDATA/../settings/Mode_Legacy ]; then
-    echo "ON" > $PATHDATA/../settings/Mode_Legacy
-    chmod 777 $PATHDATA/../settings/Mode_Legacy
+if [ ! -f $PATHDATA/../settings/Rfidreader_Rc522_Readmode_UID ]; then
+    echo "OFF" > $PATHDATA/../settings/Rfidreader_Rc522_Readmode_UID
+    chmod 777 $PATHDATA/../settings/Rfidreader_Rc522_Readmode_UID
 fi
 # 2. then|or read value from file
-MODELEGACY=`cat $PATHDATA/../settings/Mode_Legacy`
+RFIDREADERRC522READMODEUID=`cat $PATHDATA/../settings/Rfidreader_Rc522_Readmode_UID`
 
 ##############################################
 # Audio_iFace_Name
@@ -342,7 +342,7 @@ CMDSEEKBACK=`grep 'CMDSEEKBACK' $PATHDATA/../settings/rfid_trigger_play.conf|tai
 # SECONDSWIPE
 # SECONDSWIPEPAUSE
 # SECONDSWIPEPAUSECONTROLS
-# MODELEGACY
+# RFIDREADERRC522READMODEUID
 # AUDIOIFACENAME
 # AUDIOIFACEACTIVE
 # VOLUMEMANAGER
@@ -380,7 +380,7 @@ echo "SWIPEORPLACE=\"${SWIPEORPLACE}\"" >> "${PATHDATA}/../settings/global.conf"
 echo "SECONDSWIPE=\"${SECONDSWIPE}\"" >> "${PATHDATA}/../settings/global.conf"
 echo "SECONDSWIPEPAUSE=\"${SECONDSWIPEPAUSE}\"" >> "${PATHDATA}/../settings/global.conf"
 echo "SECONDSWIPEPAUSECONTROLS=\"${SECONDSWIPEPAUSECONTROLS}\"" >> "${PATHDATA}/../settings/global.conf"
-echo "MODELEGACY=\"${MODELEGACY}\"" >> "${PATHDATA}/../settings/global.conf"
+echo "RFIDREADERRC522READMODEUID=\"${RFIDREADERRC522READMODEUID}\"" >> "${PATHDATA}/../settings/global.conf"
 echo "AUDIOIFACENAME=\"${AUDIOIFACENAME}\"" >> "${PATHDATA}/../settings/global.conf"
 echo "AUDIOIFACEACTIVE=\"${AUDIOIFACEACTIVE}\"" >> "${PATHDATA}/../settings/global.conf"
 echo "VOLUMEMANAGER=\"${VOLUMEMANAGER}\"" >> "${PATHDATA}/../settings/global.conf"

From 08365b1eda851b55643b0edd7f09015eae9ad4b2 Mon Sep 17 00:00:00 2001
From: miohna <miohna@freenet.de>
Date: Wed, 24 Jan 2024 22:10:29 +0100
Subject: [PATCH 5/8] Changed error handling of non-existing
 `Rfidreader_Rc522_Readmode_UID` file.

---
 scripts/Reader.py.experimental | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/scripts/Reader.py.experimental b/scripts/Reader.py.experimental
index 12b2b604f..9e8a32d45 100755
--- a/scripts/Reader.py.experimental
+++ b/scripts/Reader.py.experimental
@@ -201,10 +201,10 @@ class Reader(object):
         else:
             with open(path + '/deviceName.txt', 'r') as f:
                 device_name = f.read().rstrip().split(';', 1)[0]
-            try:
+            if os.path.isfile(path + '/../settings/Rfidreader_Rc522_Readmode_UID'):
                 with open(path + '/../settings/Rfidreader_Rc522_Readmode_UID', 'r') as f:
                     readmode_uid = f.read().rstrip().split(';', 1)[0] == 'ON'
-            except FileNotFoundError:
+            else:
                 readmode_uid = False
 
             if device_name == 'MFRC522':

From cd883df5ea6553b5b2fdda089c0c849a1c4fd4dd Mon Sep 17 00:00:00 2001
From: miohna <miohna@freenet.de>
Date: Wed, 24 Jan 2024 22:44:49 +0100
Subject: [PATCH 6/8] Allow rc522 readmode UID setting during setup

Extend rc522 setup script to question user for the usage of UID mode
rather than the old custom card ID.
---
 components/rfid-reader/RC522/setup_rc522.sh | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/components/rfid-reader/RC522/setup_rc522.sh b/components/rfid-reader/RC522/setup_rc522.sh
index ece9a9144..30f67cff7 100644
--- a/components/rfid-reader/RC522/setup_rc522.sh
+++ b/components/rfid-reader/RC522/setup_rc522.sh
@@ -28,6 +28,12 @@ printf "MFRC522" > "${JUKEBOX_HOME_DIR}"/scripts/deviceName.txt
 sudo chown pi:www-data "${JUKEBOX_HOME_DIR}"/scripts/deviceName.txt
 sudo chmod 644 "${JUKEBOX_HOME_DIR}"/scripts/deviceName.txt
 
+read -p "Use backward-compatible card ID (not suggested for new installations) (y/n)? " choice
+case "$choice" in
+  y|Y ) printf "OFF" > "${JUKEBOX_HOME_DIR}"/settings/Rfidreader_Rc522_Readmode_UID;;
+  * ) printf "ON" > "${JUKEBOX_HOME_DIR}"/settings/Rfidreader_Rc522_Readmode_UID;;
+esac
+
 printf "Restarting phoniebox-rfid-reader service...\n"
 sudo systemctl start phoniebox-rfid-reader.service
 

From ed087d71f30b88bf506baa0e395288fc3a40bbbe Mon Sep 17 00:00:00 2001
From: miohna <miohna@freenet.de>
Date: Wed, 24 Jan 2024 22:48:18 +0100
Subject: [PATCH 7/8] Bugfix: restart rfid service properly during setup of
 rc522.

---
 components/rfid-reader/RC522/setup_rc522.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/components/rfid-reader/RC522/setup_rc522.sh b/components/rfid-reader/RC522/setup_rc522.sh
index 30f67cff7..2f23d81f0 100644
--- a/components/rfid-reader/RC522/setup_rc522.sh
+++ b/components/rfid-reader/RC522/setup_rc522.sh
@@ -35,6 +35,6 @@ case "$choice" in
 esac
 
 printf "Restarting phoniebox-rfid-reader service...\n"
-sudo systemctl start phoniebox-rfid-reader.service
+sudo systemctl restart phoniebox-rfid-reader.service
 
 printf "Done.\n"

From 1feac89d6609a1cbee7555cd6cee4056d2148102 Mon Sep 17 00:00:00 2001
From: Alvin Schiller <103769832+AlvinSchiller@users.noreply.github.com>
Date: Tue, 30 Jan 2024 02:18:04 +0100
Subject: [PATCH 8/8] add installation test parameter

---
 components/rfid-reader/RC522/setup_rc522.sh         | 13 +++++++------
 .../installscripts/tests/run_installation_tests2.sh |  5 +++--
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/components/rfid-reader/RC522/setup_rc522.sh b/components/rfid-reader/RC522/setup_rc522.sh
index 2f23d81f0..3c0e406ae 100644
--- a/components/rfid-reader/RC522/setup_rc522.sh
+++ b/components/rfid-reader/RC522/setup_rc522.sh
@@ -16,6 +16,13 @@ question() {
 printf "Please make sure that the RC522 reader is wired up correctly to the GPIO ports before continuing...\n"
 question "Continue"
 
+printf "Use backward-compatible card ID (not suggested for new installations)?\n"
+read -p "(y/n) " choice
+case "$choice" in
+  y|Y ) printf "OFF" > "${JUKEBOX_HOME_DIR}"/settings/Rfidreader_Rc522_Readmode_UID;;
+  * ) printf "ON" > "${JUKEBOX_HOME_DIR}"/settings/Rfidreader_Rc522_Readmode_UID;;
+esac
+
 printf "Installing Python requirements for RC522...\n"
 sudo python3 -m pip install --upgrade --force-reinstall -q -r "${JUKEBOX_HOME_DIR}"/components/rfid-reader/RC522/requirements.txt
 
@@ -28,12 +35,6 @@ printf "MFRC522" > "${JUKEBOX_HOME_DIR}"/scripts/deviceName.txt
 sudo chown pi:www-data "${JUKEBOX_HOME_DIR}"/scripts/deviceName.txt
 sudo chmod 644 "${JUKEBOX_HOME_DIR}"/scripts/deviceName.txt
 
-read -p "Use backward-compatible card ID (not suggested for new installations) (y/n)? " choice
-case "$choice" in
-  y|Y ) printf "OFF" > "${JUKEBOX_HOME_DIR}"/settings/Rfidreader_Rc522_Readmode_UID;;
-  * ) printf "ON" > "${JUKEBOX_HOME_DIR}"/settings/Rfidreader_Rc522_Readmode_UID;;
-esac
-
 printf "Restarting phoniebox-rfid-reader service...\n"
 sudo systemctl restart phoniebox-rfid-reader.service
 
diff --git a/scripts/installscripts/tests/run_installation_tests2.sh b/scripts/installscripts/tests/run_installation_tests2.sh
index 17b51f893..da2521ebc 100644
--- a/scripts/installscripts/tests/run_installation_tests2.sh
+++ b/scripts/installscripts/tests/run_installation_tests2.sh
@@ -24,10 +24,11 @@ echo 'debconf debconf/frontend select Noninteractive' | sudo debconf-set-selecti
 # y use gpio
 # y RFID registration
 # 2 use RC522 reader
-# yes, reader is connected
+# y, reader is connected
+# y, use legacy readermode
 # n No reboot
 
-./../install-jukebox.sh <<< $'y\nn\n\nn\n\ny\n\nn\n\ny\n\ny\n\ny\n\ny\ny\n2\ny\nn\n'
+./../install-jukebox.sh <<< $'y\nn\n\nn\n\ny\n\nn\n\ny\n\ny\n\ny\n\ny\ny\n2\ny\ny\nn\n'
 INSTALLATION_EXITCODE=$?
 
 # Test installation