supla-device
Loading...
Searching...
No Matches
config.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_STORAGE_CONFIG_H_
20#define SRC_SUPLA_STORAGE_CONFIG_H_
21
22#include <supla/device/device_mode.h>
23#include <supla/device/auto_update_policy.h>
24#include <supla/network/netif_config.h>
25#include <stddef.h>
26#include <stdint.h>
27
28#define SUPLA_CONFIG_MAX_KEY_SIZE 16
29
30#define MAX_SSID_SIZE 33 // actual SSID should be at most 32 bytes
31 // but we add here extra byte for null
32 // termination
33#define MAX_WIFI_PASSWORD_SIZE 64
34#define MQTT_CLIENTID_MAX_SIZE 23
35#define MQTT_USERNAME_MAX_SIZE 256
36#define MQTT_PASSWORD_MAX_SIZE 256
37#define SUPLA_AES_KEY_SIZE 32
38#define SUPLA_CFG_MODE_SALT_SIZE 16
39#define SUPLA_CFG_MODE_PASSWORD_SIZE 32
40
41namespace Supla {
42
43class Storage;
44
45#pragma pack(push, 1)
47 uint8_t salt[SUPLA_CFG_MODE_SALT_SIZE] = {};
48 uint8_t passwordSha[SUPLA_CFG_MODE_PASSWORD_SIZE] = {};
49
50 void copySalt(const SaltPassword& other);
51 bool isSaltEmpty() const { return salt[0] == 0; }
52 bool operator==(const SaltPassword& other) const;
53 bool isPasswordStrong(const char *password) const;
54 void clear();
55};
56#pragma pack(pop)
57
58
59class Config {
60 public:
61 friend class Storage;
62
63 Config();
64 virtual ~Config();
65 virtual bool init() = 0;
66 virtual void removeAll() = 0;
67 virtual bool isMinimalConfigReady(bool showLogs = true);
68 virtual bool isConfigModeSupported();
69 virtual bool isEncryptionEnabled();
70 // Returns true when the device-data partition is present in the partition
71 // table, regardless of whether its contents are usable.
72 virtual bool isDeviceDataPartitionDeclared();
73 virtual bool isDeviceDataPartitionAvailable();
74
75 // Override this method and setup all default value if needed
76 virtual void initDefaultDeviceConfig();
77
78 // Generic getters and setters
79 virtual bool setString(const char* key, const char* value) = 0;
80 virtual bool getString(const char* key, char* value, size_t maxSize) = 0;
81 virtual int getStringSize(const char* key) = 0;
82
83 virtual bool setBlob(const char* key, const char* value, size_t blobSize) = 0;
84 virtual bool getBlob(const char* key, char* value, size_t blobSize) = 0;
85 // Returns exact blob payload size, or -1 when the key is missing, invalid, or
86 // stores a non-blob value.
87 virtual int getBlobSize(const char* key) = 0;
88
89 virtual bool getInt8(const char* key, int8_t* result) = 0;
90 virtual bool getUInt8(const char* key, uint8_t* result) = 0;
91 virtual bool getInt32(const char* key, int32_t* result) = 0;
92 virtual bool getUInt32(const char* key, uint32_t* result) = 0;
93
94 virtual bool setInt8(const char* key, const int8_t value) = 0;
95 virtual bool setUInt8(const char* key, const uint8_t value) = 0;
96 virtual bool setInt32(const char* key, const int32_t value) = 0;
97 virtual bool setUInt32(const char* key, const uint32_t value) = 0;
98 virtual bool eraseKey(const char* key) = 0;
99
100 static void generateKey(char *, int, const char *);
101
102 virtual void commit();
103 virtual void saveWithDelay(uint16_t delayMs);
104 virtual void saveIfNeeded();
105
106 // Device generic config
107 virtual bool generateGuidAndAuthkey();
108 virtual bool setDeviceName(const char* name);
109 virtual bool setDeviceMode(enum Supla::DeviceMode mode);
110 virtual bool setGUID(const char* guid);
111 virtual bool getDeviceName(char* result);
112 virtual enum Supla::DeviceMode getDeviceMode();
113 virtual bool getGUID(char* result);
114 virtual bool getSwUpdateServer(char* url);
115 virtual bool isSwUpdateSkipCert();
116 virtual bool isSwUpdateBeta();
117 virtual bool setSwUpdateSkipCert(bool skipCert);
118 virtual bool setSwUpdateServer(const char* url);
119 virtual bool setSwUpdateBeta(bool enabled);
120 virtual bool getCustomCA(char* result, int maxSize);
121 virtual int getCustomCASize();
122 virtual bool setCustomCA(const char* customCA);
123 virtual bool getAESKey(uint8_t* result);
124
125 virtual bool loadNetifConfig(const char* blobName, NetifConfigBlob* cfg);
126 virtual bool saveNetifConfig(const char* blobName,
127 const NetifConfigBlob& cfg);
128 virtual bool removeNetifConfig(const char* blobName);
129
130#ifndef ARDUINO_ARCH_AVR
131 static void generateSaltPassword(const char* password,
132 Supla::SaltPassword* result);
133#endif
134 virtual bool setCfgModeSaltPassword(const Supla::SaltPassword& saltPassword);
135 virtual bool getCfgModeSaltPassword(Supla::SaltPassword* result);
136
142 Supla::AutoUpdatePolicy getAutoUpdatePolicy();
143
149 void setAutoUpdatePolicy(Supla::AutoUpdatePolicy policy);
150
151 // Supla protocol config
152 virtual bool setSuplaCommProtocolEnabled(bool enabled);
153 virtual bool setSuplaServer(const char* server);
154 virtual bool setSuplaServerPort(int32_t port);
155 virtual bool setEmail(const char* email);
156 virtual bool setAuthKey(const char* authkey);
157 virtual bool isSuplaCommProtocolEnabled();
158 virtual bool getSuplaServer(char* result);
159 virtual int32_t getSuplaServerPort();
160 virtual bool getEmail(char* result);
161 virtual bool getAuthKey(char* result);
162
163 // MQTT protocol config
164 virtual bool setMqttCommProtocolEnabled(bool enabled);
165 virtual bool setMqttServer(const char* server);
166 virtual bool setMqttServerPort(int32_t port);
167 virtual bool setMqttUser(const char* user);
168 virtual bool setMqttPassword(const char* password);
169 virtual bool setMqttQos(int32_t qos);
170 virtual bool isMqttCommProtocolEnabled();
171 virtual bool setMqttTlsEnabled(bool enabled);
172 virtual bool isMqttTlsEnabled();
173 virtual bool setMqttAuthEnabled(bool enabled);
174 virtual bool isMqttAuthEnabled();
175 virtual bool setMqttRetainEnabled(bool enabled);
176 virtual bool isMqttRetainEnabled();
177 virtual bool getMqttServer(char* result);
178 virtual int32_t getMqttServerPort();
179 virtual bool getMqttUser(char* result);
180 virtual bool getMqttPassword(char* result);
181 virtual int32_t getMqttQos();
182 virtual bool setMqttPrefix(const char* prefix);
183 virtual bool getMqttPrefix(char* result);
184
185 // WiFi config
186 virtual bool setWiFiSSID(const char* ssid);
187 virtual bool setWiFiPassword(const char* password);
188 virtual bool setAltWiFiSSID(const char* ssid);
189 virtual bool setAltWiFiPassword(const char* password);
190 virtual bool getWiFiSSID(char* result);
191 virtual bool getWiFiPassword(char* result);
192 virtual bool getAltWiFiSSID(char* result);
193 virtual bool getAltWiFiPassword(char* result);
194
195 virtual bool isDeviceConfigChangeFlagSet();
196 virtual bool isDeviceConfigChangeReadyToSend();
197 virtual bool setDeviceConfigChangeFlag();
198 virtual bool clearDeviceConfigChangeFlag();
199
200 virtual bool setChannelConfigChangeFlag(int channelNo, int configType = 0);
201 virtual bool clearChannelConfigChangeFlag(int channelNo, int configType = 0);
202 virtual bool isChannelConfigChangeFlagSet(int channelNo, int configType = 0);
203
211 int32_t getChannelFunction(int channelNo);
212
221 bool setChannelFunction(int channelNo, int32_t channelFunction);
222
228 bool getInitResult() const;
229
235 bool isConfigInitDone() const { return configInitDone; }
236
237 protected:
238 void setConfigInitDone(bool done) { configInitDone = done; }
239
240 uint32_t saveDelayTimestamp = 0;
241 uint32_t deviceConfigUpdateDelayTimestamp = 0;
242 uint16_t saveDelayMs = 0;
243 int8_t deviceConfigChangeFlag = -1;
244 bool initResult = false;
245 bool configInitDone = false;
246};
247}; // namespace Supla
248
249#endif // SRC_SUPLA_STORAGE_CONFIG_H_
bool isConfigInitDone() const
Checks if config has been initialized.
Definition config.h:235
Supla::AutoUpdatePolicy getAutoUpdatePolicy()
Returns current automatic firmware update policy.
Definition config.cpp:751
void setAutoUpdatePolicy(Supla::AutoUpdatePolicy policy)
Sets automatic firmware update policy.
Definition config.cpp:761
bool setChannelFunction(int channelNo, int32_t channelFunction)
Stores channel function in config.
Definition config.cpp:809
int32_t getChannelFunction(int channelNo)
Returns channel function stored in config.
Definition config.cpp:798
bool getInitResult() const
Returns init result.
Definition config.cpp:818
Definition storage.h:38
Definition netif_config.h:37
Definition config.h:46