roo_wifi
API Documentation for roo_wifi
Loading...
Searching...
No Matches
arduino_preferences_store.cpp
Go to the documentation of this file.
2
3namespace roo_wifi {
4
5namespace {
6
7uint64_t inline MurmurOAAT64(const char* key) {
8 uint64_t h(525201411107845655ull);
9 for (; *key != '\0'; ++key) {
10 h ^= *key;
11 h *= 0x5bd1e9955bd1e995;
12 h ^= h >> 47;
13 }
14 return h;
15}
16
17void ToSsiPwdKey(const std::string& ssid, char* result) {
18 uint64_t hash = MurmurOAAT64(ssid.c_str());
19 *result++ = 'p';
20 *result++ = 'w';
21 *result++ = '-';
22 // We break 64 bits into 11 groups of 6 bits; then to ASCII.
23 for (int i = 0; i < 11; i++) {
24 *result++ = (hash & 0x3F) + 48; // ASCII from '0' to 'o'.
25 hash >>= 6;
26 }
27 *result = '\0';
28}
29
30} // namespace
31
33 : collection_("roo/wifi"),
34 is_interface_enabled_(collection_, "enabled", false),
35 default_ssid_(collection_, "ssid", "") {}
36
38 return is_interface_enabled_.get();
39}
40
42 is_interface_enabled_.set(enabled);
43}
44
46 return default_ssid_.get();
47}
48
49void ArduinoPreferencesStore::setDefaultSSID(const std::string& ssid) {
50 default_ssid_.set(ssid);
51}
52
54 roo_prefs::Transaction t(collection_);
55 t.store().clear("ssid");
56}
57
58bool ArduinoPreferencesStore::getPassword(const std::string& ssid,
59 std::string& password) {
60 roo_prefs::Transaction t(collection_, true);
61 char pwkey[16];
62 ToSsiPwdKey(ssid, pwkey);
63 return (t.store().readString(pwkey, password) == roo_prefs::ReadResult::kOk);
64}
65
66void ArduinoPreferencesStore::setPassword(const std::string& ssid,
67 roo::string_view password) {
68 roo_prefs::Transaction t(collection_);
69 char pwkey[16];
70 ToSsiPwdKey(ssid, pwkey);
71 t.store().writeString(pwkey, password);
72}
73
74void ArduinoPreferencesStore::clearPassword(const std::string& ssid) {
75 roo_prefs::Transaction t(collection_);
76 char pwkey[16];
77 ToSsiPwdKey(ssid, pwkey);
78 t.store().clear(pwkey);
79}
80
81} // namespace roo_wifi
bool getIsInterfaceEnabled() override
Returns whether the Wi-Fi interface is enabled.
void setPassword(const std::string &ssid, roo::string_view password) override
Stores a password for an SSID.
bool getPassword(const std::string &ssid, std::string &password) override
Retrieves a stored password for an SSID.
void clearPassword(const std::string &ssid) override
Clears a stored password for an SSID.
void setIsInterfaceEnabled(bool enabled) override
Sets whether the Wi-Fi interface is enabled.
void clearDefaultSSID() override
Clears the default SSID.
void setDefaultSSID(const std::string &ssid) override
Sets the default SSID.
std::string getDefaultSSID() override
Returns the default SSID, if any.