Skip to content

Commit cde853b

Browse files
implementing kvstore for samd
1 parent 1cd1953 commit cde853b

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

src/kvstore/implementation/Nina.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* This file is part of Arduino_KVStore.
3+
*
4+
* Copyright (c) 2024 Arduino SA
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#if defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_NANO_RP2040_CONNECT)
12+
13+
#include "Nina.h"
14+
15+
bool NinaKVStore::begin(const char* name, bool readOnly, const char* partitionLabel) {
16+
WiFiDrv::wifiDriverInit();
17+
18+
String fv = WiFi.firmwareVersion();
19+
if (fv < "3.0.0") {
20+
Serial.println("KVStore is not supported on Nina chip for versions older than 3.0.0");
21+
22+
return false;
23+
}
24+
25+
return WiFiDrv::prefBegin(name, readOnly, partitionLabel);
26+
}
27+
28+
bool NinaKVStore::begin() {
29+
return begin(DEFAULT_KVSTORE_NAME);
30+
}
31+
32+
bool NinaKVStore::end() {
33+
WiFiDrv::prefEnd();
34+
return true;
35+
}
36+
37+
bool NinaKVStore::clear() {
38+
return WiFiDrv::prefClear();
39+
}
40+
41+
typename KVStoreInterface::res_t NinaKVStore::remove(const key_t& key) {
42+
return WiFiDrv::prefRemove(key);
43+
}
44+
45+
typename KVStoreInterface::res_t NinaKVStore::putBytes(const key_t& key, const uint8_t value[], size_t len) {
46+
return WiFiDrv::prefPut(key, static_cast<PreferenceType>(PT_BLOB), value, len);
47+
}
48+
49+
typename KVStoreInterface::res_t NinaKVStore::getBytes(const key_t& key, uint8_t buf[], size_t maxLen) const {
50+
return WiFiDrv::prefGet(key, static_cast<PreferenceType>(PT_BLOB), buf, maxLen);
51+
}
52+
53+
size_t NinaKVStore::getBytesLength(const key_t& key) const {
54+
auto len = WiFiDrv::prefLen(key);
55+
return WiFiDrv::prefGetType(key) == PT_STR ? len-1 : len;
56+
}
57+
58+
bool NinaKVStore::exists(const key_t& key) const {
59+
return WiFiDrv::prefGetType(key) != PT_INVALID;
60+
}
61+
62+
typename KVStoreInterface::res_t NinaKVStore::_put(
63+
const key_t& key, const uint8_t value[], size_t len, KVStoreInterface::Type t) {
64+
if(t == PT_DOUBLE || t == PT_FLOAT) {
65+
t = PT_BLOB;
66+
}
67+
68+
if(t == PT_STR) {
69+
len++; // For strings we also send the \0
70+
}
71+
return WiFiDrv::prefPut(key, static_cast<PreferenceType>(t), value, len);
72+
}
73+
74+
typename KVStoreInterface::res_t NinaKVStore::_get(const key_t& key, uint8_t value[], size_t len, Type t) {
75+
if(t == PT_DOUBLE || t == PT_FLOAT) {
76+
t = PT_BLOB;
77+
}
78+
79+
size_t res = WiFiDrv::prefGet(key, static_cast<PreferenceType>(t), value, len);
80+
81+
if(t == PT_STR) {
82+
value[res] = '\0';
83+
}
84+
85+
return res;
86+
}
87+
88+
#endif // defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_NANO_RP2040_CONNECT)

src/kvstore/implementation/Nina.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of Arduino_KVStore.
3+
*
4+
* Copyright (c) 2024 Arduino SA
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
#pragma once
11+
12+
#include "../kvstore.h"
13+
14+
#include <WiFi.h>
15+
16+
using namespace std;
17+
18+
const char DEFAULT_KVSTORE_NAME[] = "arduino";
19+
20+
class NinaKVStore: public KVStoreInterface {
21+
public:
22+
NinaKVStore(const char* name=DEFAULT_KVSTORE_NAME): name(name) {}
23+
bool begin() override;
24+
bool begin(const char* name, bool readOnly=false, const char* partitionLabel=nullptr);
25+
bool end() override;
26+
bool clear() override;
27+
28+
typename KVStoreInterface::res_t remove(const key_t& key) override;
29+
bool exists(const key_t& key) const override;
30+
typename KVStoreInterface::res_t putBytes(const key_t& key, const uint8_t b[], size_t s) override;
31+
typename KVStoreInterface::res_t getBytes(const key_t& key, uint8_t b[], size_t s) const override;
32+
size_t getBytesLength(const key_t& key) const override;
33+
34+
protected:
35+
res_t _put(const key_t& key, const uint8_t value[], size_t len, Type t) override;
36+
res_t _get(const key_t& key, uint8_t value[], size_t len, Type t) override;
37+
private:
38+
const char* name;
39+
};

0 commit comments

Comments
 (0)