-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARDUINO_GATEWAY_TRIO.ino
147 lines (125 loc) · 3.29 KB
/
ARDUINO_GATEWAY_TRIO.ino
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
#include <SPI.h>
#include <EEPROM.h>
#include "RF24.h"
#define CE_PIN 7
#define CSN_PIN 8
#define SERVICE_KEY 123123123
RF24 radio(CE_PIN, CSN_PIN);
uint8_t address[][6] = {"00001", "00002", "00003"};
String maskPass(String password)
{
String mask = "";
for (int i = 0; i < password.length(); i++)
{
mask += char(password[i] ^ SERVICE_KEY);
}
return mask;
}
void writeStringToEEPROM(int addrOffset, const String &strToWrite)
{
byte len = strToWrite.length();
EEPROM.write(addrOffset, len);
for (int i = 0; i < len; i++)
{
EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
}
}
String readStringFromEEPROM(int addrOffset)
{
int newStrLen = EEPROM.read(addrOffset);
char data[newStrLen + 1];
for (int i = 0; i < newStrLen; i++)
{
data[i] = EEPROM.read(addrOffset + 1 + i);
}
data[newStrLen] = '\0';
return String(data);
}
int findLoginInEEPROM(String login)
{
int address = 0;
while (address < EEPROM.length())
{
String storedData = readStringFromEEPROM(address);
int colonIndex = storedData.indexOf(':');
String storedLogin = storedData.substring(0, colonIndex);
if (storedLogin == login)
{
return address;
}
address += storedData.length() + 1;
}
return -1;
}
void setup()
{
Serial.begin(115200);
while (!Serial)
{
}
if (!radio.begin())
{
while (1)
{
}
}
Serial.println(F("Gateway - Ouvindo solicitações de registro e login"));
radio.setPALevel(RF24_PA_LOW);
radio.openReadingPipe(1, address[0]);
radio.startListening();
}
void loop()
{
if (radio.available())
{
char receivedPayload[32] = {0};
radio.read(&receivedPayload, sizeof(receivedPayload));
Serial.print(F("Payload recebido: "));
Serial.println(receivedPayload);
String receivedString = String(receivedPayload);
int firstColon = receivedString.indexOf(':');
int secondColon = receivedString.indexOf(':', firstColon + 1);
String type = receivedString.substring(0, firstColon);
String login = receivedString.substring(firstColon + 1, secondColon);
String password = receivedString.substring(secondColon + 1);
if (type == "register")
{
int startAddress = findLoginInEEPROM(login);
if (startAddress == -1)
{
String maskPassword = maskPass(password);
startAddress = EEPROM.length();
writeStringToEEPROM(startAddress, login + ":" + maskPassword);
Serial.println(F("Registro feito com sucesso! salvo em EEPROM."));
}
else
{
Serial.println(F("Falhou. Login já utilizado."));
}
}
else if (type == "login")
{
int startAddress = findLoginInEEPROM(login);
if (startAddress != -1)
{
String storedData = readStringFromEEPROM(startAddress);
int storedColon = storedData.indexOf(':');
String storedmaskPassword = storedData.substring(storedColon + 1);
String decryptedStoredPassword = maskPass(storedmaskPassword);
if (password == decryptedStoredPassword)
{
Serial.println(F("Login realizado com sucesso"));
}
else
{
Serial.println(F("Falha ao fazer Login. Senha incorreta."));
}
}
else
{
Serial.println(F("Falha ao fazer Login. Usuário não encontrado."));
}
}
}
delay(100);
}