-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiotsaRFID.cpp
279 lines (253 loc) · 7.78 KB
/
iotsaRFID.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "iotsa.h"
#include "iotsaRFID.h"
#include "iotsaConfigFile.h"
#include "MFRC522.h"
#include <set>
String addCard;
String removeCard;
String lastCard;
uint32_t lastCardReadTime;
bool lastCardKnown;
std::set<String> normalCards;
cardMode curMode;
uint32_t curModeEndTime;
// RFID reader interface object
MFRC522 mfrc522(PIN_RFID_SDA, PIN_RFID_RESET);
// Helper function: convert RFID UID to string
static String strUid(MFRC522::Uid& uid) {
String rv;
for (int i=0; i<uid.size; i++) {
char c1 = "0123456789ABCDEF"[((uid.uidByte[i] >> 4) & 0xf)];
char c2 = "0123456789ABCDEF"[(uid.uidByte[i] & 0xf)];
rv += c1;
rv += c2;
}
return rv;
}
bool lookupCard(String& uid) {
auto it = normalCards.find(uid);
bool ok = it != normalCards.end();
return ok;
}
void handleAddCard(String& uid) {
auto it = normalCards.find(uid);
if (it == normalCards.end()) {
normalCards.insert(uid);
IotsaSerial.print("added card: ");
IotsaSerial.println(uid);
}
}
void handleRemoveCard(String& uid) {
auto it = normalCards.find(uid);
if (it != normalCards.end()) {
normalCards.erase(it);
IotsaSerial.print("Removed card: ");
IotsaSerial.println(uid);
}
}
//
// Implementation of the RFID module
//
void IotsaRFIDMod::setup() {
SPI.begin();
mfrc522.PCD_Init();
configLoad();
}
void
IotsaRFIDMod::handler() {
// Handles the page that is specific to the RFID module.
if (needsAuthentication()) return;
bool anyChanged = false;
for (uint8_t i=0; i<server->args(); i++){
if( server->argName(i) == "addCard") {
if( server->arg(i) != addCard) {
anyChanged = true;
addCard = server->arg(i);
IotsaSerial.print("Set addCard: ");
IotsaSerial.println(addCard);
}
}
if( server->argName(i) == "removeCard") {
if( server->arg(i) != removeCard) {
anyChanged = true;
removeCard = server->arg(i);
IotsaSerial.print("Set removeCard: ");
IotsaSerial.println(removeCard);
}
}
if( server->argName(i) == "normalAdd") {
String val = server->arg(i);
if( val != "") {
anyChanged = true;
handleAddCard(val);
}
}
if( server->argName(i) == "normalRemove") {
String val = server->arg(i);
if( val != "") {
anyChanged = true;
handleRemoveCard(val);
}
}
if (anyChanged) configSave();
}
String message = "<html><head><title>RFID Server</title></head><body><h1>RFID Server</h1>";
if (lastCard != "") {
message += "<p>Last card was presented " + String((millis()-lastCardReadTime)/1000) + " seconds ago, Card ID " + lastCard;
if (!lastCardKnown) message += " (unknown)";
message += ".</p>";
} else {
message += "<p>No card presented since last power-on.</p>";
}
message += "<h2>Adding Cards</h2><form method='get'>Master addition card ID: <input name='addCard' value='";
message += addCard;
message += "'><br>Master removal card ID: <input name='removeCard' value='";
message += removeCard;
message += "'><br>Manually add normal card: <input name='normalAdd'><br><input type='submit'></form>";
message += "<h2>Known Cards</h2><ul>";
for (auto it=normalCards.begin(); it != normalCards.end(); it++) {
message += "<li>" + *it + "(<a href='/rfid?normalRemove=" + *it + "'>remove</a>)</li>";
}
message += "</ul>";
message += "</body></html>";
server->send(200, "text/html", message);
}
bool IotsaRFIDMod::getHandler(const char *path, JsonObject& reply) {
reply["addCard"] = addCard;
reply["removeCard"] = removeCard;
reply["lastCard"] = lastCard;
if (lastCard != "") {
reply["lastCardPresented"] = (millis()-lastCardReadTime)/1000;
}
JsonArray rCards = reply["cards"].as<JsonArray>();
for (auto value : normalCards) {
rCards.add(value);
}
return true;
}
bool IotsaRFIDMod::putHandler(const char *path, const JsonVariant& request, JsonObject& reply) {
if (!request.is<JsonObject>()) return false;
JsonObject reqObj = request.as<JsonObject>();
bool any = false;
if (getFromRequest<const char *>(reqObj, "addCard", addCard)) {
any = true;
}
if (getFromRequest<const char *>(reqObj, "removeCard", removeCard)) {
any = true;
}
JsonArray newCards;
if (getFromRequest<JsonArray>(reqObj, "cards", newCards)) {
any = true;
normalCards.clear();
for (auto value: newCards) {
normalCards.insert(value.as<String>());
}
}
if (any) configSave();
return any;
}
void IotsaRFIDMod::serverSetup() {
// Setup the web server hooks for this module.
server->on("/rfid", std::bind(&IotsaRFIDMod::handler, this));
api.setup("/api/rfid", true, true);
name = "rfid";
}
String IotsaRFIDMod::info() {
// Return some information about this module, for the main page of the web server.
String rv = "<p>See <a href=\"/rfid\">/rfid</a> for rfid management. Api available at <a href=\"/api/rfid\">/api/rfid</a>.</p>";
return rv;
}
void IotsaRFIDMod::loop() {
static uint32_t lastPoll;
uint32_t now = millis();
if (now > lastPoll && now < lastPoll + RFID_POLL_INTERVAL)
return;
lastPoll = now;
//IotsaSerial.println("rfid in");
if (curMode != card_idle && millis() > curModeEndTime) {
// No card present for 2 seconds: revert to idle
IotsaSerial.println("mode timeout.");
curModeEndTime = 0;
curMode = card_idle;
if (modeChanged) modeChanged(curMode);
}
if (!mfrc522.PICC_IsNewCardPresent()) {
//IotsaSerial.println("rfid no card");
return;
}
IotsaSerial.println("card present");
if (!mfrc522.PICC_ReadCardSerial()) {
//IotsaSerial.println("rfid no serial");
return;
}
IotsaSerial.println("serial read");
String newCard = strUid(mfrc522.uid);
handleCard(newCard);
//IotsaSerial.println("rfid out");
}
void IotsaRFIDMod::handleCard(String& newCard) {
lastCard = newCard;
lastCardReadTime = millis();
lastCardKnown = lookupCard(newCard);
// First check if we should add or remove this card
if (curMode == card_add && !lastCardKnown) {
handleAddCard(newCard);
configSave();
lastCardKnown = true;
curMode = card_idle;
if (modeChanged) modeChanged(curMode);
return;
}
if (curMode == card_remove && lastCardKnown) {
handleRemoveCard(newCard);
configSave();
lastCardKnown = false;
curMode = card_idle;
if (modeChanged) modeChanged(curMode);
return;
}
cardMode newMode = card_bad;
if (newCard == addCard) newMode = card_add;
else if (newCard == removeCard) newMode = card_remove;
else if (lastCardKnown) newMode = card_ok;
if (newMode != curMode) {
IotsaSerial.print("mode: ");
IotsaSerial.println(int(newMode));
curMode = newMode;
curModeEndTime = millis() + 2000;
if (newMode == card_add || newMode == card_remove) {
curModeEndTime = millis() + 5000;
}
if (modeChanged) modeChanged(curMode);
if (curMode == card_ok && cardPresented) cardPresented(newCard);
if (curMode == card_bad && unknownCardPresented) unknownCardPresented(newCard);
}
}
void IotsaRFIDMod::configLoad() {
IotsaConfigFileLoad cf("/config/rfid.cfg");
cf.get("addCard", addCard, "");
cf.get("removeCard", removeCard, "");
int idx=1;
// xxxjack should use object interface
normalCards.clear();
while(1) {
String newCard;
String name = "card"+String(idx);
cf.get(name, newCard, "");
if (newCard == "") break;
normalCards.insert(newCard);
idx++;
}
IotsaSerial.print("Cards loaded: "); IotsaSerial.println(idx-1);
}
void IotsaRFIDMod::configSave() {
IotsaConfigFileSave cf("/config/rfid.cfg");
cf.put("addCard", addCard);
cf.put("removeCard", removeCard);
int idx=1;
for (auto it=normalCards.begin(); it != normalCards.end(); it++, idx++) {
String name = "card"+String(idx);
cf.put(name, *it);
}
IotsaSerial.print("Cards saved: "); IotsaSerial.println(idx-1);
}