Skip to content

Commit

Permalink
Merge pull request #9 from wizzomafizzo/wizzo/migrate-nfcui
Browse files Browse the repository at this point in the history
Migrate nfcui
  • Loading branch information
wizzomafizzo authored Jan 21, 2024
2 parents dd3e416 + ac19a90 commit 367bda0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
26 changes: 15 additions & 11 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,22 @@ func pollDevice(
activeCard Token,
ttp int,
pbp time.Duration,
) (Token, error) {
) (Token, bool, error) {
removed := false

count, target, err := pnd.InitiatorPollTarget(tokens.SupportedCardTypes, ttp, pbp)
if err != nil && !errors.Is(err, nfc.Error(nfc.ETIMEOUT)) {
return activeCard, err
return activeCard, removed, err
}

if count <= 0 {
if activeCard.UID != "" && time.Since(activeCard.ScanTime) > timeToForgetCard {
log.Info().Msg("card removed")
activeCard = Token{}

if cfg.TapTo.ExitGame {
mister.ExitGame()
}
removed = true
}

return activeCard, nil
return activeCard, removed, nil
}

cardUid := tokens.GetCardUID(target)
Expand All @@ -78,7 +77,7 @@ func pollDevice(
}

if cardUid == activeCard.UID {
return activeCard, nil
return activeCard, removed, nil
}

log.Info().Msgf("found token UID: %s", cardUid)
Expand All @@ -90,7 +89,7 @@ func pollDevice(
log.Info().Msg("NTAG detected")
record, err = tokens.ReadNtag(*pnd)
if err != nil {
return activeCard, fmt.Errorf("error reading ntag: %s", err)
return activeCard, removed, fmt.Errorf("error reading ntag: %s", err)
}
cardType = tokens.TypeNTAG
}
Expand Down Expand Up @@ -119,7 +118,7 @@ func pollDevice(
ScanTime: time.Now(),
}

return card, nil
return card, removed, nil
}

func detectConnectionString() string {
Expand Down Expand Up @@ -288,7 +287,7 @@ func StartDaemon(cfg *config.UserConfig) (func() error, error) {
}

activeCard := state.GetActiveCard()
newScanned, err := pollDevice(cfg, &pnd, activeCard, ttp, pbp)
newScanned, removed, err := pollDevice(cfg, &pnd, activeCard, ttp, pbp)
if errors.Is(err, nfc.Error(nfc.EIO)) {
log.Error().Msgf("error during poll: %s", err)
log.Error().Msg("fatal IO error, device was unplugged, exiting...")
Expand All @@ -307,6 +306,11 @@ func StartDaemon(cfg *config.UserConfig) (func() error, error) {

state.SetActiveCard(newScanned)

if removed && cfg.TapTo.ExitGame && !state.IsLauncherDisabled() {
mister.ExitGame()
goto end
}

if newScanned.UID == "" || activeCard.UID == newScanned.UID {
goto end
}
Expand Down
44 changes: 22 additions & 22 deletions scripts/nfcui/nfcui.sh → scripts/taptui/taptui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@
# You should have received a copy of the GNU General Public License
# along with TapTo. If not, see <http://www.gnu.org/licenses/>.

title="MiSTer NFC"
title="TapTo"
scriptdir="$(dirname "$(readlink -f "${0}")")"
version="0.2"
version="0.3"
fullFileBrowser="false"
basedir="/media"
sdroot="${basedir}/fat"
searchCommand="${scriptdir}/search.sh"
nfcCommand="${scriptdir}/nfc.sh"
settings="${scriptdir}/nfc.ini"
nfcCommand="${scriptdir}/tapto.sh"
settings="${scriptdir}/tapto.ini"
map="${sdroot}/nfc.csv"
#For debugging purpouse
[[ -d "${sdroot}" ]] || map="${scriptdir}/nfc.csv"
[[ -d "${sdroot}" ]] && PATH="${sdroot}/linux:${sdroot}/Scripts:${PATH}"
mapHeader="match_uid,match_text,text"
nfcStatus="$("${nfcCommand}" --service status)"
case "${nfcStatus}" in
"nfc service running")
"tapto service running")
nfcStatus="true"
nfcUnavailable="false"
msg="Service: Enabled"
;;
"nfc service not running")
"tapto service not running")
nfcStatus="false"
nfcUnavailable="false"
msg="Service: Disabled"
Expand All @@ -50,7 +50,7 @@ case "${nfcStatus}" in
nfcUnavailable="true"
msg="Service: Unavailable"
esac
nfcSocket="UNIX-CONNECT:/tmp/nfc.sock"
nfcSocket="UNIX-CONNECT:/tmp/tapto/tapto.sock"
if [[ -S "${nfcSocket#*:}" ]]; then
nfcReadingStatus="$(echo "status" | socat - "${nfcSocket}")"
nfcReadingStatus="$(cut -d ',' -f 3 <<< "${nfcReadingStatus}")"
Expand Down Expand Up @@ -469,7 +469,7 @@ _depends() {
_exit 1
fi

[[ -x "${nfcCommand}" ]] || _error "${nfcCommand} not found\n\nRead more at ${underline}github.com/wizzomafizzo/mrext${noUnderline}" "1" --colors
[[ -x "${nfcCommand}" ]] || _error "${nfcCommand} not found\n\nRead more at ${underline}github.com/wizzomafizzo/tapto${noUnderline}" "1" --colors

[[ -x "$(command -v rg)" ]] && grep() { rg "${@}"; }
}
Expand Down Expand Up @@ -707,7 +707,7 @@ _craftCommand(){
_Settings() {
local menuOptions selected
menuOptions=(
"Service" "Start/stop the NFC service"
"Service" "Start/stop the TapTo service"
"Commands" "Toggles the ability to run Linux commands from NFC tags"
"Sounds" "Toggles sounds played when a tag is scanned"
"Connection" "Hardware configuration for certain NFC readers"
Expand All @@ -730,26 +730,26 @@ _Settings() {
_serviceSetting() {
local menuOptions selected

"${nfcUnavailable}" && { _error "NFC Service Unavailable!\n\nIs the NFC script installed?"; return; }
"${nfcUnavailable}" && { _error "TapTo Service Unavailable!\n\nIs TapTo installed?"; return; }

menuOptions=(
"Enable" "Enable NFC service" "off"
"Disable" "Disable NFC service" "off"
"Enable" "Enable TapTo service" "off"
"Disable" "Disable TapTo service" "off"
)
"${nfcStatus}" && menuOptions[2]="on"
"${nfcStatus}" || menuOptions[5]="on"

selected="$(_radiolist -- "${menuOptions[@]}" )"
case "${selected}" in
Enable)
"${nfcCommand}" -service start || { _error "Unable to start the NFC service"; return; }
"${nfcCommand}" -service start || { _error "Unable to start the TapTo service"; return; }
export nfcStatus="true" msg="Service: Enabled"
_msgbox "The NFC service started"
_msgbox "The TapTo service started"
;;
Disable)
"${nfcCommand}" -service stop || { _error "Unable to stop the NFC service"; return; }
"${nfcCommand}" -service stop || { _error "Unable to stop the TapTo service"; return; }
export nfcStatus="false" msg="Service: Disabled"
_msgbox "The NFC service stopped"
_msgbox "The TapTo service stopped"
;;
esac
}
Expand All @@ -761,7 +761,7 @@ _commandSetting() {
"Disable" "Disable Linux commands" "off"
)

[[ -f "${settings}" ]] || echo "[nfc]" > "${settings}" || { _error "Can't create settings file" ; return 1 ; }
[[ -f "${settings}" ]] || echo "[tapto]" > "${settings}" || { _error "Can't create settings file" ; return 1 ; }

if grep -q "^allow_commands=yes" "${settings}"; then
menuOptions[2]="on"
Expand Down Expand Up @@ -802,7 +802,7 @@ _soundSetting() {
"Disable" "Disable sounds played when a tag is scanned" "off"
)

[[ -f "${settings}" ]] || echo "[nfc]" > "${settings}" || { _error "Can't create settings file" ; return 1 ; }
[[ -f "${settings}" ]] || echo "[tapto]" > "${settings}" || { _error "Can't create settings file" ; return 1 ; }

if grep -q "^disable_sounds=no" "${settings}"; then
menuOptions[5]="on"
Expand Down Expand Up @@ -837,7 +837,7 @@ _connectionSetting() {
"Custom" "Manually enter a custom connection string" "off"
)

[[ -f "${settings}" ]] || echo "[nfc]" > "${settings}" || { _error "Can't create settings file" ; return 1 ; }
[[ -f "${settings}" ]] || echo "[tapto]" > "${settings}" || { _error "Can't create settings file" ; return 1 ; }

if ! grep -q "^connection_string=.*" "${settings}"; then
menuOptions[2]="on"
Expand Down Expand Up @@ -886,7 +886,7 @@ _probeSetting() {
"Disable" "Disable detection of a serial based reader device" "off"
)

[[ -f "${settings}" ]] || echo "[nfc]" > "${settings}" || { _error "Can't create settings file" ; return 1 ; }
[[ -f "${settings}" ]] || echo "[tapto]" > "${settings}" || { _error "Can't create settings file" ; return 1 ; }

# Check if probe_device is set to "no" in the settings
if grep -q "^probe_device=no" "${settings}"; then
Expand Down Expand Up @@ -924,7 +924,7 @@ ${version}
A tool for making and working with NFC tags on MiSTer FPGA
Whats New? Get involved? Need help?
${underline}github.com/wizzomafizzo/mrext${noUnderline}
${underline}github.com/wizzomafizzo/tapto${noUnderline}
${nfcjokes[$((RANDOM % ${#nfcjokes[@]}))]}
Expand All @@ -933,7 +933,7 @@ Wizzo ${underline}github.com/wizzomafizzo${noUnderline}
Ziggurat ${underline}github.com/sigboe${noUnderline}
License: GPL v3.0
${underline}github.com/wizzomafizzo/mrext/blob/main/LICENSE${noUnderline}
${underline}github.com/wizzomafizzo/tapto/blob/main/LICENSE${noUnderline}
_EOF_
_msgbox "${about}" --no-collapse --colors --title "About"
}
Expand Down

0 comments on commit 367bda0

Please sign in to comment.