Skip to content

Commit

Permalink
Bugfix: re-enable Mifare Ultralight C support (MiczFlor#2214)
Browse files Browse the repository at this point in the history
* 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.

* 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.

* Add error handling for missing `Mode_Legacy` file.

* 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.

* Changed error handling of non-existing `Rfidreader_Rc522_Readmode_UID` file.

* 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.

* Bugfix: restart rfid service properly during setup of rc522.

* add installation test parameter

---------

Co-authored-by: Alvin Schiller <[email protected]>
  • Loading branch information
miohna and AlvinSchiller authored Jan 30, 2024
1 parent 9bb656b commit 01734de
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
9 changes: 8 additions & 1 deletion components/rfid-reader/RC522/setup_rc522.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -29,6 +36,6 @@ sudo chown pi:www-data "${JUKEBOX_HOME_DIR}"/scripts/deviceName.txt
sudo chmod 644 "${JUKEBOX_HOME_DIR}"/scripts/deviceName.txt

printf "Restarting phoniebox-rfid-reader service...\n"
sudo systemctl start phoniebox-rfid-reader.service
sudo systemctl restart phoniebox-rfid-reader.service

printf "Done.\n"
24 changes: 21 additions & 3 deletions scripts/Reader.py.experimental
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ class UsbReader(object):


class Mfrc522Reader(object):
def __init__(self):
def __init__(self, readmode_uid=False):
import pirc522
self.device = pirc522.RFID()
self.readmode_uid = readmode_uid
self.readCard = self.readCard_normal if self.readmode_uid else self.readCard_legacy

def readCard(self):
def readCard_legacy(self):
# Scan for cards
self.device.wait_for_tag()
(error, tag_type) = self.device.request()
Expand All @@ -70,6 +72,17 @@ class Mfrc522Reader(object):
logger.debug("No Device ID found.")
return None

def readCard_normal(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()
Expand Down Expand Up @@ -188,9 +201,14 @@ class Reader(object):
else:
with open(path + '/deviceName.txt', 'r') as f:
device_name = f.read().rstrip().split(';', 1)[0]
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'
else:
readmode_uid = False

if device_name == 'MFRC522':
self.reader = Mfrc522Reader()
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'}
Expand Down
12 changes: 12 additions & 0 deletions scripts/inc.writeGlobalConfig.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ fi
# 2. then|or read value from file
SECONDSWIPEPAUSECONTROLS=`cat $PATHDATA/../settings/Second_Swipe_Pause_Controls`

##############################################
# RFID reader rc522 readmode UID
# 1. create a default if file does not exist
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
RFIDREADERRC522READMODEUID=`cat $PATHDATA/../settings/Rfidreader_Rc522_Readmode_UID`

##############################################
# Audio_iFace_Name
# 1. create a default if file does not exist
Expand Down Expand Up @@ -332,6 +342,7 @@ CMDSEEKBACK=`grep 'CMDSEEKBACK' $PATHDATA/../settings/rfid_trigger_play.conf|tai
# SECONDSWIPE
# SECONDSWIPEPAUSE
# SECONDSWIPEPAUSECONTROLS
# RFIDREADERRC522READMODEUID
# AUDIOIFACENAME
# AUDIOIFACEACTIVE
# VOLUMEMANAGER
Expand Down Expand Up @@ -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 "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"
Expand Down
5 changes: 3 additions & 2 deletions scripts/installscripts/tests/run_installation_tests2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 01734de

Please sign in to comment.