@@ -9,6 +9,9 @@ EEPROMMate::EEPROMMate() {}
9
9
10
10
void EEPROMMate::begin (size_t eepromSize) {
11
11
EEPROM.begin (eepromSize);
12
+ if (readTotalUsedBytes () > eepromSize) {
13
+ factoryReset (); // first run
14
+ }
12
15
}
13
16
14
17
void EEPROMMate::commit () {
@@ -39,26 +42,26 @@ bool EEPROMMate::writeData(uint8_t* key, uint8_t keyLength, uint8_t* value, uint
39
42
bool EEPROMMate::writeKeyValue (uint32_t key, uint8_t * value, uint16_t valueLength) {
40
43
uint8_t * tmp = new uint8_t [valueLength];
41
44
bool keyExists = readValueByKey (key, tmp, valueLength);
42
- delete [] tmp;
45
+ delete[] tmp;
43
46
if (keyExists) {
44
47
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
46
49
}
47
50
if (valueLength == 0 ) {
48
51
Serial.println (" Value length cannot be zero" );
49
- return false ; // value length cannot be zero
52
+ return false ; // value length cannot be zero
50
53
}
51
54
uint16_t totalUsedBytes = readTotalUsedBytes ();
52
55
if ((totalUsedBytes + sizeof (uint16_t ) + sizeof (uint32_t ) + valueLength) > EEPROM.length ()) {
53
56
Serial.println (" EEPROM does not have enough free space!" );
54
57
Serial.print ((totalUsedBytes + sizeof (uint16_t ) + sizeof (uint32_t ) + valueLength));
55
- Serial.print (" / " );
58
+ Serial.print (" / " );
56
59
Serial.println (EEPROM.length ());
57
60
return false ;
58
61
}
59
62
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
62
65
} else {
63
66
addressEntry = totalUsedBytes + 1 ;
64
67
}
@@ -93,40 +96,40 @@ uint16_t EEPROMMate::readValueLength(uint8_t* keyInput, uint8_t keyLength) {
93
96
uint32_t key = fastCrc.crc32 (keyInput, keyLength);
94
97
uint16_t totalUsedBytes = readTotalUsedBytes ();
95
98
if (totalUsedBytes == 0 ) {
96
- return -1 ; // there is nothing to read
99
+ return -1 ; // there is nothing to read
97
100
}
98
101
uint32_t currentKey;
99
102
uint16_t address = INDEX_FIRST_ENTRY;
100
103
101
104
while (address < totalUsedBytes) {
102
105
uint16_t entryLength = readIntFromEEPROM (address);
103
106
if (entryLength == 0 || entryLength > totalUsedBytes) {
104
- return -1 ; // invalid entry length
107
+ return -1 ; // invalid entry length
105
108
}
106
109
currentKey = readLongFromEEPROM (address + sizeof (uint16_t ));
107
110
if (currentKey == key) {
108
111
return entryLength;
109
112
}
110
113
address = address + sizeof (uint16_t ) + sizeof (uint32_t ) + entryLength + 1 ;
111
114
}
112
- return -1 ; // key not found
115
+ return -1 ; // key not found
113
116
}
114
117
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) {
116
119
uint16_t totalUsedBytes = readTotalUsedBytes ();
117
120
if (totalUsedBytes == 0 ) {
118
- return false ; // there is nothing to read
121
+ return false ; // there is nothing to read
119
122
}
120
123
if (dstValue == NULL ) {
121
- return false ; // destination buffer is NULL
124
+ return false ; // destination buffer is NULL
122
125
}
123
126
uint32_t currentKey;
124
127
uint16_t address = INDEX_FIRST_ENTRY;
125
128
126
129
while (address < totalUsedBytes) {
127
130
uint16_t entryLength = readIntFromEEPROM (address);
128
131
if (entryLength == 0 || entryLength > totalUsedBytes) {
129
- return false ; // invalid entry length
132
+ return false ; // invalid entry length
130
133
}
131
134
currentKey = readLongFromEEPROM (address + sizeof (uint16_t ));
132
135
if (currentKey == key) {
@@ -137,15 +140,15 @@ bool EEPROMMate::readValueByKey(uint32_t key, uint8_t *dstValue, uint16_t dstVal
137
140
}
138
141
// check if the destination buffer is big enough to hold the data
139
142
if (sizeof (value) > dstValueLength) {
140
- return false ; // destination buffer is too small
143
+ return false ; // destination buffer is too small
141
144
}
142
145
// copy the encrypted value to the destination
143
146
memcpy (dstValue, value, sizeof (value));
144
147
return true ;
145
148
}
146
149
address = address + sizeof (uint16_t ) + sizeof (uint32_t ) + entryLength + 1 ;
147
150
}
148
- return false ; // key not found
151
+ return false ; // key not found
149
152
}
150
153
151
154
bool EEPROMMate::readAllSavedData () {
0 commit comments