Skip to content

Commit adba65b

Browse files
committed
eeprom mate logic to factory reset on the first run
1 parent 0e1ad44 commit adba65b

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

turtlpass-firmware/EEPROMMate.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ EEPROMMate::EEPROMMate() {}
99

1010
void EEPROMMate::begin(size_t eepromSize) {
1111
EEPROM.begin(eepromSize);
12+
if (readTotalUsedBytes() > eepromSize) {
13+
factoryReset(); // first run
14+
}
1215
}
1316

1417
void EEPROMMate::commit() {
@@ -39,26 +42,26 @@ bool EEPROMMate::writeData(uint8_t* key, uint8_t keyLength, uint8_t* value, uint
3942
bool EEPROMMate::writeKeyValue(uint32_t key, uint8_t* value, uint16_t valueLength) {
4043
uint8_t* tmp = new uint8_t[valueLength];
4144
bool keyExists = readValueByKey(key, tmp, valueLength);
42-
delete [] tmp;
45+
delete[] tmp;
4346
if (keyExists) {
4447
Serial.println("Key already exists in the EEPROM");
45-
return false; // key already exists in the EEPROM
48+
return false; // key already exists in the EEPROM
4649
}
4750
if (valueLength == 0) {
4851
Serial.println("Value length cannot be zero");
49-
return false; // value length cannot be zero
52+
return false; // value length cannot be zero
5053
}
5154
uint16_t totalUsedBytes = readTotalUsedBytes();
5255
if ((totalUsedBytes + sizeof(uint16_t) + sizeof(uint32_t) + valueLength) > EEPROM.length()) {
5356
Serial.println("EEPROM does not have enough free space!");
5457
Serial.print((totalUsedBytes + sizeof(uint16_t) + sizeof(uint32_t) + valueLength));
55-
Serial.print(" / ");
58+
Serial.print(" / ");
5659
Serial.println(EEPROM.length());
5760
return false;
5861
}
5962
uint16_t addressEntry;
60-
if (totalUsedBytes == 0) { // empty
61-
addressEntry = sizeof(uint16_t); // skip header
63+
if (totalUsedBytes == 0) { // empty
64+
addressEntry = sizeof(uint16_t); // skip header
6265
} else {
6366
addressEntry = totalUsedBytes + 1;
6467
}
@@ -93,40 +96,40 @@ uint16_t EEPROMMate::readValueLength(uint8_t* keyInput, uint8_t keyLength) {
9396
uint32_t key = fastCrc.crc32(keyInput, keyLength);
9497
uint16_t totalUsedBytes = readTotalUsedBytes();
9598
if (totalUsedBytes == 0) {
96-
return -1; // there is nothing to read
99+
return -1; // there is nothing to read
97100
}
98101
uint32_t currentKey;
99102
uint16_t address = INDEX_FIRST_ENTRY;
100103

101104
while (address < totalUsedBytes) {
102105
uint16_t entryLength = readIntFromEEPROM(address);
103106
if (entryLength == 0 || entryLength > totalUsedBytes) {
104-
return -1; // invalid entry length
107+
return -1; // invalid entry length
105108
}
106109
currentKey = readLongFromEEPROM(address + sizeof(uint16_t));
107110
if (currentKey == key) {
108111
return entryLength;
109112
}
110113
address = address + sizeof(uint16_t) + sizeof(uint32_t) + entryLength + 1;
111114
}
112-
return -1; // key not found
115+
return -1; // key not found
113116
}
114117

115-
bool EEPROMMate::readValueByKey(uint32_t key, uint8_t *dstValue, uint16_t dstValueLength) {
118+
bool EEPROMMate::readValueByKey(uint32_t key, uint8_t* dstValue, uint16_t dstValueLength) {
116119
uint16_t totalUsedBytes = readTotalUsedBytes();
117120
if (totalUsedBytes == 0) {
118-
return false; // there is nothing to read
121+
return false; // there is nothing to read
119122
}
120123
if (dstValue == NULL) {
121-
return false; // destination buffer is NULL
124+
return false; // destination buffer is NULL
122125
}
123126
uint32_t currentKey;
124127
uint16_t address = INDEX_FIRST_ENTRY;
125128

126129
while (address < totalUsedBytes) {
127130
uint16_t entryLength = readIntFromEEPROM(address);
128131
if (entryLength == 0 || entryLength > totalUsedBytes) {
129-
return false; // invalid entry length
132+
return false; // invalid entry length
130133
}
131134
currentKey = readLongFromEEPROM(address + sizeof(uint16_t));
132135
if (currentKey == key) {
@@ -137,15 +140,15 @@ bool EEPROMMate::readValueByKey(uint32_t key, uint8_t *dstValue, uint16_t dstVal
137140
}
138141
// check if the destination buffer is big enough to hold the data
139142
if (sizeof(value) > dstValueLength) {
140-
return false; // destination buffer is too small
143+
return false; // destination buffer is too small
141144
}
142145
// copy the encrypted value to the destination
143146
memcpy(dstValue, value, sizeof(value));
144147
return true;
145148
}
146149
address = address + sizeof(uint16_t) + sizeof(uint32_t) + entryLength + 1;
147150
}
148-
return false; // key not found
151+
return false; // key not found
149152
}
150153

151154
bool EEPROMMate::readAllSavedData() {

turtlpass-firmware/turtlpass-firmware.ino

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
// Keyboard Output:
1616
// 100 characters password
1717
// characters type: [a-z][A-Z][0-9]
18+
19+
// \4dff4ea340f0a823f15d3f4f01ab62eae0e5da579ccb851f8db9dfe84c58b2b37b89903a740e1ee172da793a6e79d560e5f7f9bd058a12a280433ed6fa46510a
20+
//
21+
// Keyboard Output:
22+
// 100 characters password
23+
// characters type: [a-z][A-Z][0-9][ !\"#$%&'()*+,-./:;<=>?@[]^_`{|}~]
1824
//
1925
// - Encrypt bytes ('>', '<' to Decrypt)
2026
// >4dff4ea340f0a823f15d3f4f01ab62eae0e5da579ccb851f8db9dfe84c58b2b37b89903a740e1ee172da793a6e79d560e5f7f9bd058a12a280433ed6fa46510a

0 commit comments

Comments
 (0)