-
Notifications
You must be signed in to change notification settings - Fork 121
/
RfidCommon.cpp
126 lines (114 loc) · 3.71 KB
/
RfidCommon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <Arduino.h>
#include "settings.h"
#include "AudioPlayer.h"
#include "Cmd.h"
#include "Common.h"
#include "Log.h"
#include "MemX.h"
#include "Mqtt.h"
#include "Queues.h"
#include "Rfid.h"
#include "System.h"
#include "Web.h"
unsigned long Rfid_LastRfidCheckTimestamp = 0;
char gCurrentRfidTagId[cardIdStringSize] = ""; // No crap here as otherwise it could be shown in GUI
#ifdef DONT_ACCEPT_SAME_RFID_TWICE_ENABLE
char gOldRfidTagId[cardIdStringSize] = "X"; // Init with crap
#endif
// check if we have RFID-reader enabled
#if defined(RFID_READER_TYPE_MFRC522_SPI) || defined(RFID_READER_TYPE_MFRC522_I2C) || defined(RFID_READER_TYPE_PN5180)
#define RFID_READER_ENABLED 1
#endif
// Tries to lookup RFID-tag-string in NVS and extracts parameter from it if found
void Rfid_PreferenceLookupHandler(void) {
#if defined(RFID_READER_ENABLED)
BaseType_t rfidStatus;
char rfidTagId[cardIdStringSize];
char _file[255];
uint32_t _lastPlayPos = 0;
uint16_t _trackLastPlayed = 0;
uint32_t _playMode = 1;
rfidStatus = xQueueReceive(gRfidCardQueue, &rfidTagId, 0);
if (rfidStatus == pdPASS) {
System_UpdateActivityTimer();
strncpy(gCurrentRfidTagId, rfidTagId, cardIdStringSize - 1);
Log_Printf(LOGLEVEL_INFO, "%s: %s", rfidTagReceived, gCurrentRfidTagId);
Web_SendWebsocketData(0, WebsocketCodeType::CurrentRfid); // Push new rfidTagId to all websocket-clients
String s = "-1";
if (gPrefsRfid.isKey(gCurrentRfidTagId)) {
s = gPrefsRfid.getString(gCurrentRfidTagId, "-1"); // Try to lookup rfidId in NVS
}
if (!s.compareTo("-1")) {
Log_Println(rfidTagUnknownInNvs, LOGLEVEL_ERROR);
System_IndicateError();
// allow to escape from bluetooth mode with an unknown card, switch back to normal mode
System_SetOperationMode(OPMODE_NORMAL);
return;
}
char *token;
uint8_t i = 1;
token = strtok((char *) s.c_str(), stringDelimiter);
while (token != NULL) { // Try to extract data from string after lookup
if (i == 1) {
strncpy(_file, token, sizeof(_file) / sizeof(_file[0]));
} else if (i == 2) {
_lastPlayPos = strtoul(token, NULL, 10);
} else if (i == 3) {
_playMode = strtoul(token, NULL, 10);
} else if (i == 4) {
_trackLastPlayed = strtoul(token, NULL, 10);
}
i++;
token = strtok(NULL, stringDelimiter);
}
if (i != 5) {
Log_Println(errorOccuredNvs, LOGLEVEL_ERROR);
System_IndicateError();
} else {
// Only pass file to queue if strtok revealed 3 items
if (_playMode >= 100) {
// Modification-cards can change some settings (e.g. introducing track-looping or sleep after track/playlist).
Cmd_Action(_playMode);
} else {
#ifdef DONT_ACCEPT_SAME_RFID_TWICE_ENABLE
if (strncmp(gCurrentRfidTagId, gOldRfidTagId, 12) == 0) {
Log_Printf(LOGLEVEL_ERROR, dontAccepctSameRfid, gCurrentRfidTagId);
// System_IndicateError(); // Enable to have shown error @neopixel every time
return;
} else {
strncpy(gOldRfidTagId, gCurrentRfidTagId, 12);
}
#endif
#ifdef MQTT_ENABLE
publishMqtt(topicRfidState, gCurrentRfidTagId, false);
#endif
#ifdef BLUETOOTH_ENABLE
// if music rfid was read, go back to normal mode
if (System_GetOperationMode() == OPMODE_BLUETOOTH_SINK) {
System_SetOperationMode(OPMODE_NORMAL);
}
#endif
AudioPlayer_TrackQueueDispatcher(_file, _lastPlayPos, _playMode, _trackLastPlayed);
}
}
}
#endif
}
#ifdef DONT_ACCEPT_SAME_RFID_TWICE_ENABLE
void Rfid_ResetOldRfid() {
strncpy(gOldRfidTagId, "X", cardIdStringSize - 1);
}
#endif
#if defined(RFID_READER_ENABLED)
extern TaskHandle_t rfidTaskHandle;
#endif
void Rfid_TaskPause(void) {
#if defined(RFID_READER_ENABLED)
vTaskSuspend(rfidTaskHandle);
#endif
}
void Rfid_TaskResume(void) {
#if defined(RFID_READER_ENABLED)
vTaskResume(rfidTaskHandle);
#endif
}