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 <stddef.h>
25#include <stdint.h>
26
27#define SUPLA_CONFIG_MAX_KEY_SIZE 16
28
29#define MAX_SSID_SIZE 33 // actual SSID should be at most 32 bytes
30 // but we add here extra byte for null
31 // termination
32#define MAX_WIFI_PASSWORD_SIZE 64
33#define MQTT_CLIENTID_MAX_SIZE 23
34#define MQTT_USERNAME_MAX_SIZE 256
35#define MQTT_PASSWORD_MAX_SIZE 256
36#define SUPLA_AES_KEY_SIZE 32
37#define SUPLA_CFG_MODE_SALT_SIZE 16
38#define SUPLA_CFG_MODE_PASSWORD_SIZE 32
39
40namespace Supla {
41
42#pragma pack(push, 1)
44 uint8_t salt[SUPLA_CFG_MODE_SALT_SIZE] = {};
45 uint8_t passwordSha[SUPLA_CFG_MODE_PASSWORD_SIZE] = {};
46
47 void copySalt(const SaltPassword& other);
48 bool isSaltEmpty() const { return salt[0] == 0; }
49 bool operator==(const SaltPassword& other) const;
50 bool isPasswordStrong(const char *password) const;
51 void clear();
52};
53#pragma pack(pop)
54
55
56class Config {
57 public:
58 Config();
59 virtual ~Config();
60 virtual bool init() = 0;
61 virtual void removeAll() = 0;
62 virtual bool isMinimalConfigReady(bool showLogs = true);
63 virtual bool isConfigModeSupported();
64 virtual bool isEncryptionEnabled();
65
66 // Override this method and setup all default value if needed
67 virtual void initDefaultDeviceConfig();
68
69 // Generic getters and setters
70 virtual bool setString(const char* key, const char* value) = 0;
71 virtual bool getString(const char* key, char* value, size_t maxSize) = 0;
72 virtual int getStringSize(const char* key) = 0;
73
74 virtual bool setBlob(const char* key, const char* value, size_t blobSize) = 0;
75 virtual bool getBlob(const char* key, char* value, size_t blobSize) = 0;
76
77 virtual bool getInt8(const char* key, int8_t* result) = 0;
78 virtual bool getUInt8(const char* key, uint8_t* result) = 0;
79 virtual bool getInt32(const char* key, int32_t* result) = 0;
80 virtual bool getUInt32(const char* key, uint32_t* result) = 0;
81
82 virtual bool setInt8(const char* key, const int8_t value) = 0;
83 virtual bool setUInt8(const char* key, const uint8_t value) = 0;
84 virtual bool setInt32(const char* key, const int32_t value) = 0;
85 virtual bool setUInt32(const char* key, const uint32_t value) = 0;
86 virtual bool eraseKey(const char* key) = 0;
87
88 static void generateKey(char *, int, const char *);
89
90 virtual void commit();
91 virtual void saveWithDelay(uint16_t delayMs);
92 virtual void saveIfNeeded();
93
94 // Device generic config
95 virtual bool generateGuidAndAuthkey();
96 virtual bool setDeviceName(const char* name);
97 virtual bool setDeviceMode(enum Supla::DeviceMode mode);
98 virtual bool setGUID(const char* guid);
99 virtual bool getDeviceName(char* result);
100 virtual enum Supla::DeviceMode getDeviceMode();
101 virtual bool getGUID(char* result);
102 virtual bool getSwUpdateServer(char* url);
103 virtual bool isSwUpdateSkipCert();
104 virtual bool isSwUpdateBeta();
105 virtual bool setSwUpdateSkipCert(bool skipCert);
106 virtual bool setSwUpdateServer(const char* url);
107 virtual bool setSwUpdateBeta(bool enabled);
108 virtual bool getCustomCA(char* result, int maxSize);
109 virtual int getCustomCASize();
110 virtual bool setCustomCA(const char* customCA);
111 virtual bool getAESKey(uint8_t* result);
112
113 static void generateSaltPassword(const char* password,
114 Supla::SaltPassword* result);
115 virtual bool setCfgModeSaltPassword(const Supla::SaltPassword& saltPassword);
116 virtual bool getCfgModeSaltPassword(Supla::SaltPassword* result);
117
123 Supla::AutoUpdatePolicy getAutoUpdatePolicy();
124
130 void setAutoUpdatePolicy(Supla::AutoUpdatePolicy policy);
131
132 // Supla protocol config
133 virtual bool setSuplaCommProtocolEnabled(bool enabled);
134 virtual bool setSuplaServer(const char* server);
135 virtual bool setSuplaServerPort(int32_t port);
136 virtual bool setEmail(const char* email);
137 virtual bool setAuthKey(const char* authkey);
138 virtual bool isSuplaCommProtocolEnabled();
139 virtual bool getSuplaServer(char* result);
140 virtual int32_t getSuplaServerPort();
141 virtual bool getEmail(char* result);
142 virtual bool getAuthKey(char* result);
143
144 // MQTT protocol config
145 virtual bool setMqttCommProtocolEnabled(bool enabled);
146 virtual bool setMqttServer(const char* server);
147 virtual bool setMqttServerPort(int32_t port);
148 virtual bool setMqttUser(const char* user);
149 virtual bool setMqttPassword(const char* password);
150 virtual bool setMqttQos(int32_t qos);
151 virtual bool isMqttCommProtocolEnabled();
152 virtual bool setMqttTlsEnabled(bool enabled);
153 virtual bool isMqttTlsEnabled();
154 virtual bool setMqttAuthEnabled(bool enabled);
155 virtual bool isMqttAuthEnabled();
156 virtual bool setMqttRetainEnabled(bool enabled);
157 virtual bool isMqttRetainEnabled();
158 virtual bool getMqttServer(char* result);
159 virtual int32_t getMqttServerPort();
160 virtual bool getMqttUser(char* result);
161 virtual bool getMqttPassword(char* result);
162 virtual int32_t getMqttQos();
163 virtual bool setMqttPrefix(const char* prefix);
164 virtual bool getMqttPrefix(char* result);
165
166 // WiFi config
167 virtual bool setWiFiSSID(const char* ssid);
168 virtual bool setWiFiPassword(const char* password);
169 virtual bool setAltWiFiSSID(const char* ssid);
170 virtual bool setAltWiFiPassword(const char* password);
171 virtual bool getWiFiSSID(char* result);
172 virtual bool getWiFiPassword(char* result);
173 virtual bool getAltWiFiSSID(char* result);
174 virtual bool getAltWiFiPassword(char* result);
175
176 virtual bool isDeviceConfigChangeFlagSet();
177 virtual bool isDeviceConfigChangeReadyToSend();
178 virtual bool setDeviceConfigChangeFlag();
179 virtual bool clearDeviceConfigChangeFlag();
180
181 virtual bool setChannelConfigChangeFlag(int channelNo, int configType = 0);
182 virtual bool clearChannelConfigChangeFlag(int channelNo, int configType = 0);
183 virtual bool isChannelConfigChangeFlagSet(int channelNo, int configType = 0);
184
185 protected:
186 virtual int getBlobSize(const char* key) = 0;
187
188 uint32_t saveDelayTimestamp = 0;
189 uint32_t deviceConfigUpdateDelayTimestamp = 0;
190 uint16_t saveDelayMs = 0;
191 int8_t deviceConfigChangeFlag = -1;
192};
193}; // namespace Supla
194
195#endif // SRC_SUPLA_STORAGE_CONFIG_H_
Supla::AutoUpdatePolicy getAutoUpdatePolicy()
Returns current automatic firmware update policy.
Definition config.cpp:654
void setAutoUpdatePolicy(Supla::AutoUpdatePolicy policy)
Sets automatic firmware update policy.
Definition config.cpp:664
Definition config.h:43