Uniot Core
0.8.1
Loading...
Searching...
No Matches
WifiStorage.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2024 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <CBORStorage.h>
22#include <EEPROM.h>
23
24namespace uniot {
34class WifiStorage : public CBORStorage {
35 public:
41 WifiStorage() : CBORStorage("wifi.cbor") {
42 }
43
49 const String& getSsid() const {
50 return mSsid;
51 }
52
58 const String& getPassword() const {
59 return mPassword;
60 }
61
68 void setCredentials(const String& ssid, const String& password) {
69 mSsid = ssid;
70 mPassword = password;
71 }
72
83 return !mSsid.isEmpty();
84 }
85
94 virtual bool store() override {
95 object().put("ssid", mSsid.c_str());
96 object().put("pass", mPassword.c_str());
97 return CBORStorage::store();
98 }
99
108 virtual bool restore() override {
109 if (CBORStorage::restore()) {
110 mSsid = object().getString("ssid");
111 mPassword = object().getString("pass");
112 return true;
113 }
114 return false;
115 }
116
125 virtual bool clean() override {
126 mSsid = "";
127 mPassword = "";
128 return CBORStorage::clean();
129 }
130
131 private:
132 String mSsid;
133 String mPassword;
134};
135
136} // namespace uniot
CBORObject & put(int key, int value)
Put an integer value at a specific integer key.
Definition CBORObject.h:170
String getString(int key) const
Get a string value at a specific integer key.
Definition CBORObject.h:419
virtual bool restore() override
Restore the CBOR object from the filesystem.
Definition CBORStorage.h:92
virtual bool clean() override
Clean the CBOR object and remove the storage file.
Definition CBORStorage.h:110
virtual bool store() override
Store the CBOR object to the filesystem.
Definition CBORStorage.h:74
CBORStorage(const String &path)
Constructs a new CBORStorage object.
Definition CBORStorage.h:46
CBORObject & object()
Get access to the underlying CBORObject.
Definition CBORStorage.h:60
virtual bool restore() override
Restore WiFi credentials from persistent storage.
Definition WifiStorage.h:108
bool isCredentialsValid()
Check if stored credentials are valid.
Definition WifiStorage.h:82
WifiStorage()
Construct a new WifiStorage object.
Definition WifiStorage.h:41
void setCredentials(const String &ssid, const String &password)
Set WiFi credentials.
Definition WifiStorage.h:68
const String & getSsid() const
Get the stored WiFi SSID.
Definition WifiStorage.h:49
virtual bool clean() override
Clear stored WiFi credentials.
Definition WifiStorage.h:125
const String & getPassword() const
Get the stored WiFi password.
Definition WifiStorage.h:58
virtual bool store() override
Store WiFi credentials to persistent storage.
Definition WifiStorage.h:94
Contains all classes and functions related to the Uniot Core.