Serial Interface
This commit is contained in:
parent
b5623e2a74
commit
0034d2d5e5
1 changed files with 159 additions and 1 deletions
160
src/main.cpp
160
src/main.cpp
|
@ -125,8 +125,9 @@ void handleData1Interrupt() {
|
|||
}
|
||||
|
||||
|
||||
|
||||
void setup() {
|
||||
#ifdef SERIAL_DEBUG
|
||||
#if defined SERIAL_DEBUG || defined SERIAL_ACL
|
||||
Serial.begin(9600);
|
||||
#endif
|
||||
|
||||
|
@ -208,8 +209,165 @@ void setup() {
|
|||
|
||||
}
|
||||
|
||||
|
||||
#ifdef LOCAL_ACL
|
||||
#ifdef SERIAL_ACL
|
||||
|
||||
struct WordList {
|
||||
char** words;
|
||||
int length;
|
||||
};
|
||||
|
||||
WordList splitString(const char* inputString, const char* delimiter = " ") {
|
||||
WordList result;
|
||||
result.words = NULL;
|
||||
result.length = 0;
|
||||
|
||||
// Create a temporary copy of the input string since strtok modifies the string
|
||||
char* inputCopy = strdup(inputString);
|
||||
|
||||
// Get the first word
|
||||
char* word = strtok(inputCopy, delimiter);
|
||||
|
||||
// Split the string and store each word in the dynamic list
|
||||
while (word != NULL) {
|
||||
result.length++;
|
||||
|
||||
// Reallocate memory for the dynamic list
|
||||
result.words = (char**)realloc(result.words, (result.length + 1) * sizeof(char*));
|
||||
|
||||
// Allocate memory for the current word and copy it into the list
|
||||
result.words[result.length - 1] = (char*)malloc(strlen(word) + 1);
|
||||
strcpy(result.words[result.length - 1], word);
|
||||
|
||||
// Set the next element in the list to NULL for termination
|
||||
result.words[result.length] = NULL;
|
||||
|
||||
// Get the next word
|
||||
word = strtok(NULL, delimiter);
|
||||
}
|
||||
|
||||
// Free the temporary copy of the input string
|
||||
free(inputCopy);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void freeWordList(char** wordList) {
|
||||
for (int i = 0; wordList[i] != NULL; i++) {
|
||||
free(wordList[i]);
|
||||
}
|
||||
free(wordList);
|
||||
}
|
||||
void checkSerialCommand() {
|
||||
if (Serial.available() > 0) {
|
||||
String command = Serial.readStringUntil('\n'); // Read the incoming command until a newline character is received
|
||||
Serial.println("Received command: " + command); // Echo back the received command
|
||||
command.trim(); // Remove any leading or trailing whitespaces
|
||||
|
||||
if (command.length() == 0) {
|
||||
Serial.println("Invalid command"); // If the command is not recognized
|
||||
}
|
||||
else {
|
||||
WordList wordData = splitString(command.c_str());
|
||||
char** wordList = wordData.words;
|
||||
int wordCount = wordData.length;
|
||||
|
||||
String prefix = wordList[0];
|
||||
|
||||
// Command Validate Access
|
||||
if (prefix.equals("validateAccess")) {
|
||||
if (wordCount == 2) {
|
||||
bool result = acl.validateAccess(String(wordList[1]));
|
||||
|
||||
if (result) {
|
||||
Serial.println("Access granted!");
|
||||
}
|
||||
else {
|
||||
Serial.println("Access denied!");
|
||||
}
|
||||
}
|
||||
else {
|
||||
Serial.println("validateAccess requires exactly 1 arguments (cardId)");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Command Add User
|
||||
if (prefix.equals("addUser")) {
|
||||
if (wordCount == 3) {
|
||||
acl.addUser(wordList[1], wordList[2]);
|
||||
acl.saveToEEPROM();
|
||||
Serial.println("User Added!");
|
||||
}
|
||||
else {
|
||||
Serial.println("addUser requires exactly 2 arguments (cardId, Description)");
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Command Update User
|
||||
if (prefix.equals("updateUser")) {
|
||||
if (wordCount == 4) {
|
||||
acl.updateUser(wordList[1], wordList[2], wordList[3]);
|
||||
acl.saveToEEPROM();
|
||||
Serial.println("User Updated!");
|
||||
}
|
||||
else {
|
||||
Serial.println("userUser requires exactly 2 arguments (cardId, newCardId, newDescription)");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Command Remove User
|
||||
if (prefix.equals("removeUser")) {
|
||||
if (wordCount == 2) {
|
||||
acl.removeUser(wordList[1]);
|
||||
acl.saveToEEPROM();
|
||||
Serial.println("User Removed!!");
|
||||
}
|
||||
else {
|
||||
Serial.println("removeUser requires exactly 1 argument (cardId)");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Command List Uses
|
||||
if (prefix.equals("listUsers")) {
|
||||
if (wordCount == 1) {
|
||||
// Retrieve the ACL data using the getter function
|
||||
const User* aclData = acl.getACL();
|
||||
|
||||
// Iterate over each user in the ACL and add it to the JSON array
|
||||
for (int i = 0; i < acl.getACLSize(); i++) {
|
||||
Serial.print(aclData[i].cardId);
|
||||
Serial.print(" : ");
|
||||
Serial.println(aclData[i].desc);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Serial.println("removeUser requires exactly 0 arguments");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
freeWordList(wordList);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void loop() {
|
||||
|
||||
#ifdef LOCAL_ACL
|
||||
#ifdef SERIAL_ACL
|
||||
checkSerialCommand();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
if (newDataAvailable) {
|
||||
newDataAvailable = false;
|
||||
lastDataTime = millis(); // Reset the time of last received data
|
||||
|
|
Loading…
Reference in a new issue