Skip to content

Commit b846a55

Browse files
authored
Merge pull request #297 from andreagilardoni/preferences-fixes
Preferences api fixes and cleaning
2 parents 5e3622b + ed86e47 commit b846a55

File tree

4 files changed

+108
-33
lines changed

4 files changed

+108
-33
lines changed

examples/PreferencesCounter/PreferencesCounter.ino

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ void setup() {
2828
while(1) {};
2929
}
3030

31-
// Serial.println(">Clear");
32-
// preferences.clear();
33-
// Serial.println("<Clear");
34-
35-
3631
// Remove all preferences under the opened namespace
3732
//preferences.clear();
3833

examples/PreferencesValidation/PreferencesValidation.ino

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,25 @@ void setup() {
222222
Serial.println();
223223
Serial.println();
224224

225-
Serial.println("Testing String operations");
225+
Serial.println("Testing C - String operations");
226226
if(!test_preferences("la mia stringsa asdasdafasdjdsnajdnaskjlasda\n\n\r\nsdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjl\r\nOK\r\nERROR",
227227
&preferences)) {
228-
Serial.println("String test failed");
228+
Serial.println("C - String test failed");
229+
preferences.remove(KEY);
230+
}
231+
232+
Serial.println();
233+
Serial.println();
234+
Serial.println();
235+
236+
Serial.println("Testing Arduino - String operations");
237+
if(!test_preferences<String>("la mia stringsa asdasdafasdjdsnajdnaskjlasda\n\n\r\nsdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjlasdasdafasdjdsnajdnaskjl\r\nOK\r\nERROR",
238+
std::bind(static_cast<size_t(Preferences::*)(const char*, String)>(&Preferences::putString), &preferences, std::placeholders::_1, std::placeholders::_2),
239+
[&preferences](const char* key) { // we need a lambda, because it requires a default value from apis
240+
return preferences.getString(key);
241+
}, &preferences)) {
242+
243+
Serial.println("Arduino - String test failed");
229244
preferences.remove(KEY);
230245
}
231246

examples/PreferencesValidation/test.h

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ bool test_preferences(T value,
2121
// .6 we compare the value with what we inserted at the beginning
2222
// .7 we remove the value
2323

24-
Serial.println("Tesing isKey()");
24+
Serial.println("Testing isKey()");
2525
if(preferences->isKey(KEY)) {
2626
Serial.println("[Error] kvstore already contains a key");
2727
return false;
2828
}
2929

30-
Serial.println("Tesing put()");
30+
Serial.println("Testing put()");
3131
size_t s;
3232
if((s=putf(KEY, value)) != sizeof(value)) {
3333
Serial.println("[Error] kvstore put returned a size that is different from the expected one: ");
@@ -37,13 +37,13 @@ bool test_preferences(T value,
3737
return false;
3838
}
3939

40-
Serial.println("Tesing isKey()"); // FIXME always failing
40+
Serial.println("Testing isKey()");
4141
if(!preferences->isKey(KEY)) {
4242
Serial.println("[Error] The inserted key is not present in the KVStore");
4343
return false;
4444
}
4545

46-
Serial.println("Tesing getf()");
46+
Serial.println("Testing getf()");
4747
T val;
4848
if((val = getf(KEY)) != value) {
4949
Serial.print("[Error] get of the previously value returned a wrong value: ");
@@ -53,7 +53,7 @@ bool test_preferences(T value,
5353
return false;
5454
}
5555

56-
Serial.println("Tesing remove()");
56+
Serial.println("Testing remove()");
5757
if(!preferences->remove(KEY)) {
5858
Serial.println("[Error] Failed removing the inserted key");
5959
return false;
@@ -62,6 +62,70 @@ bool test_preferences(T value,
6262
return true;
6363
}
6464

65+
template<>
66+
bool test_preferences(String value,
67+
std::function<size_t(const char*, String)> putf,
68+
std::function<String(const char*)> getf,
69+
Preferences *preferences) {
70+
// we are going now to test all the apis of kvstore:
71+
// .1 We check that the key do not exist
72+
// .2 we put a value inside the KVStore
73+
// .3 we check that the size returned is correct
74+
// .4 we check again the key exists
75+
// .5 we get the value contained
76+
// .6 we compare the value with what we inserted at the beginning
77+
// .7 we remove the value
78+
79+
Serial.println("Testing isKey()");
80+
if(preferences->isKey(KEY)) {
81+
Serial.println("[Error] kvstore already contains a key");
82+
return false;
83+
}
84+
85+
Serial.println("Testing put()");
86+
size_t s;
87+
if((s=putf(KEY, value)) != value.length()) {
88+
Serial.println("[Error] kvstore put returned a size that is different from the expected one: ");
89+
Serial.print(s);
90+
Serial.print(" != ");
91+
Serial.println(value.length());
92+
return false;
93+
}
94+
95+
Serial.println("Testing isKey()");
96+
if(!preferences->isKey(KEY)) {
97+
Serial.println("[Error] The inserted key is not present in the KVStore");
98+
return false;
99+
}
100+
101+
Serial.println("Testing getBytesLength()");
102+
if((s=preferences->getBytesLength(KEY)) != value.length()+1) {
103+
Serial.println("[Error] The length of the value do not match with the expected one");
104+
Serial.print(s);
105+
Serial.print(" != ");
106+
Serial.println(value.length()+1);
107+
return false;
108+
}
109+
110+
Serial.println("Testing getf()");
111+
String val;
112+
if((val = getf(KEY)) != value) {
113+
Serial.print("[Error] get of the previously inserted value returned a wrong value: ");
114+
Serial.print(val);
115+
Serial.print(" != ");
116+
Serial.println(value);
117+
return false;
118+
}
119+
120+
Serial.println("Testing remove()");
121+
if(!preferences->remove(KEY)) {
122+
Serial.println("[Error] Failed removing the inserted key");
123+
return false;
124+
}
125+
126+
return true;
127+
}
128+
65129
bool test_preferences(char* value, Preferences *preferences) {
66130
// we are going now to test all the apis of preferences:
67131
// .1 We check that the key do not exist
@@ -72,13 +136,13 @@ bool test_preferences(char* value, Preferences *preferences) {
72136
// .6 we compare the value with what we inserted at the beginning
73137
// .7 we remove the value
74138

75-
Serial.println("Tesing isKey()");
139+
Serial.println("Testing isKey()");
76140
if(preferences->isKey(KEY)) {
77141
Serial.println("[Error] kvstore already contains a key");
78142
return false;
79143
}
80144

81-
Serial.println("Tesing put()");
145+
Serial.println("Testing put()");
82146
size_t s;
83147
if((s=preferences->putString(KEY, value)) != strlen(value)) {
84148
Serial.println("[Error] kvstore put returned a size that is different from the expected one: ");
@@ -88,13 +152,13 @@ bool test_preferences(char* value, Preferences *preferences) {
88152
return false;
89153
}
90154

91-
Serial.println("Tesing isKey()");
155+
Serial.println("Testing isKey()");
92156
if(!preferences->isKey(KEY)) {
93157
Serial.println("[Error] The inserted key is not present in the KVStore");
94158
return false;
95159
}
96160

97-
Serial.println("Tesing getf()");
161+
Serial.println("Testing getf()");
98162
char val[500];
99163
preferences->getString(KEY, val, sizeof(val));
100164
if(strcmp(val, value) != 0) {
@@ -116,7 +180,7 @@ bool test_preferences(char* value, Preferences *preferences) {
116180
return false;
117181
}
118182

119-
Serial.println("Tesing remove()");
183+
Serial.println("Testing remove()");
120184
if(!preferences->remove(KEY)) {
121185
Serial.println("[Error] Failed removing the inserted key");
122186
return false;
@@ -134,14 +198,13 @@ bool test_preferences(uint8_t* value, size_t size, Preferences *preferences) {
134198
// .5 we get the value contained
135199
// .6 we compare the value with what we inserted at the beginning
136200
// .7 we remove the value
137-
138-
Serial.println("Tesing isKey()");
201+
Serial.println("Testing isKey()");
139202
if(preferences->isKey(KEY)) {
140203
Serial.println("[Error] kvstore already contains a key");
141204
return false;
142205
}
143206

144-
Serial.println("Tesing put()");
207+
Serial.println("Testing put()");
145208
size_t s;
146209
if((s=preferences->putBytes(KEY, value, size)) != size) {
147210
Serial.println("[Error] kvstore put returned a size that is different from the expected one: ");
@@ -151,13 +214,13 @@ bool test_preferences(uint8_t* value, size_t size, Preferences *preferences) {
151214
return false;
152215
}
153216

154-
Serial.println("Tesing isKey()");
217+
Serial.println("Testing isKey()");
155218
if(!preferences->isKey(KEY)) {
156219
Serial.println("[Error] The inserted key is not present in the KVStore");
157220
return false;
158221
}
159222

160-
Serial.println("Tesing getBytesLength()");
223+
Serial.println("Testing getBytesLength()");
161224
if((s=preferences->getBytesLength(KEY)) != size) {
162225
Serial.println("[Error] The length of the value do not match with the expected one");
163226
Serial.print(s);
@@ -166,7 +229,7 @@ bool test_preferences(uint8_t* value, size_t size, Preferences *preferences) {
166229
return false;
167230
}
168231

169-
Serial.println("Tesing getf()");
232+
Serial.println("Testing getf()");
170233
uint8_t val[500];
171234
s = preferences->getBytes(KEY, val, sizeof(val));
172235

@@ -183,7 +246,7 @@ bool test_preferences(uint8_t* value, size_t size, Preferences *preferences) {
183246
return false;
184247
}
185248

186-
Serial.println("Tesing remove()");
249+
Serial.println("Testing remove()");
187250
if(!preferences->remove(KEY)) {
188251
Serial.println("[Error] Failed removing the inserted key");
189252
return false;

src/WiFiPreferences.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,14 @@ size_t Preferences::putUChar(const char* key, uint8_t value) {
6666

6767
size_t Preferences::putShort(const char* key, int16_t value) {
6868
return WiFiDrv::prefPut(key, PT_I16, (uint8_t*) &value, sizeof(value));
69-
7069
}
7170

7271
size_t Preferences::putUShort(const char* key, uint16_t value) {
7372
return WiFiDrv::prefPut(key, PT_U16, (uint8_t*) &value, sizeof(value));
74-
7573
}
7674

7775
size_t Preferences::putInt(const char* key, int32_t value) {
7876
return WiFiDrv::prefPut(key, PT_I32, (uint8_t*) &value, sizeof(value));
79-
8077
}
8178

8279
size_t Preferences::putUInt(const char* key, uint32_t value) {
@@ -284,13 +281,18 @@ size_t Preferences::getString(const char* key, char* value, const size_t maxLen)
284281
return WiFiDrv::prefGet(key, PT_STR, (uint8_t*)value, maxLen);
285282
}
286283

287-
String Preferences::getString(const char* key, const String defaultValue) { // TODO
288-
Serial.println("This function is not implemented yet");
289-
if(!isKey(key)) {
290-
return defaultValue;
291-
}
284+
String Preferences::getString(const char* key, const String defaultValue) {
285+
size_t len = getBytesLength(key);
286+
char *str = new char[len+1];
287+
288+
getString(key, str, len+1);
289+
str[len] = '\0';
292290

293-
return defaultValue;
291+
String res(str);
292+
delete str;
293+
str = nullptr;
294+
295+
return res;
294296
}
295297

296298
size_t Preferences::getBytesLength(const char* key) {

0 commit comments

Comments
 (0)