supla-device
Loading...
Searching...
No Matches
SuplaDevice.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 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15*/
16
17#ifndef SRC_SUPLADEVICE_H_
18#define SRC_SUPLADEVICE_H_
19
20#include <supla-common/proto.h>
21#include <supla/uptime.h>
22#include <supla/action_handler.h>
23#include <supla/local_action.h>
24#include <supla/device/auto_update_mode.h>
25#include <supla/device/device_mode.h>
26
27#define STATUS_UNKNOWN -1
28#define STATUS_ALREADY_INITIALIZED 1
29#define STATUS_MISSING_NETWORK_INTERFACE 2
30#define STATUS_UNKNOWN_SERVER_ADDRESS 3
31#define STATUS_UNKNOWN_LOCATION_ID 4
32#define STATUS_INITIALIZED 5
33#define STATUS_SERVER_DISCONNECTED 6
34#define STATUS_ITERATE_FAIL 7
35#define STATUS_NETWORK_DISCONNECTED 8
36#define STATUS_ALL_PROTOCOLS_DISABLED 9
37
38#define STATUS_REGISTER_IN_PROGRESS 10 // Don't change
39#define STATUS_REGISTERED_AND_READY 17 // Don't change
40
41#define STATUS_TEMPORARILY_UNAVAILABLE 21
42#define STATUS_INVALID_GUID 22
43#define STATUS_CHANNEL_LIMIT_EXCEEDED 23
44#define STATUS_PROTOCOL_VERSION_ERROR 24
45#define STATUS_BAD_CREDENTIALS 25
46#define STATUS_LOCATION_CONFLICT 26
47#define STATUS_CHANNEL_CONFLICT 27
48#define STATUS_DEVICE_IS_DISABLED 28
49#define STATUS_LOCATION_IS_DISABLED 29
50#define STATUS_DEVICE_LIMIT_EXCEEDED 30
51#define STATUS_REGISTRATION_DISABLED 31
52#define STATUS_MISSING_CREDENTIALS 32
53#define STATUS_INVALID_AUTHKEY 33
54#define STATUS_NO_LOCATION_AVAILABLE 34
55#define STATUS_UNKNOWN_ERROR 35
56#define STATUS_COUNTRY_REJECTED 36
57
58#define STATUS_CONFIG_MODE 40
59#define STATUS_SOFTWARE_RESET 41
60#define STATUS_SW_DOWNLOAD 50
61#define STATUS_SUPLA_PROTOCOL_DISABLED 60
62#define STATUS_TEST_WAIT_FOR_CFG_BUTTON 70
63#define STATUS_OFFLINE_MODE 80
64
65// 10 days
66#define SUPLA_AUTOMATIC_OTA_CHECK_INTERVAL (10ULL * 24 * 60 * 60 * 1000)
67
68typedef void (*_impl_arduino_status)(int status, const char *msg);
69
70#ifdef ARDUINO
71class __FlashStringHelper;
72#else
73#define __FlashStringHelper char
74#endif
75
76namespace Supla {
77class Clock;
78class Mutex;
79class Element;
80
81namespace Device {
82class SwUpdate;
85class StatusLed;
86class LastStateLogger;
87} // namespace Device
88
89namespace Protocol {
90class SuplaSrpc;
91} // namespace Protocol
92
93} // namespace Supla
94
95class SuplaDeviceClass : public Supla::ActionHandler,
96 public Supla::LocalAction {
97 public:
98 SuplaDeviceClass();
99 ~SuplaDeviceClass();
100
101 void fillStateData(TDSC_ChannelState *channelState);
102 void addClock(Supla::Clock *clock); // DEPRECATED
103 Supla::Clock *getClock();
104
105 bool begin(const char GUID[SUPLA_GUID_SIZE],
106 const char *Server,
107 const char *email,
108 const char authkey[SUPLA_AUTHKEY_SIZE],
109 unsigned char protoVersion = 23);
110
111 bool begin(unsigned char protoVersion = 23);
112
113 // Use ASCII only in name
114 void setName(const char *Name);
115
116 void setGUID(const char GUID[SUPLA_GUID_SIZE]);
117 void setAuthKey(const char authkey[SUPLA_AUTHKEY_SIZE]);
118 void setEmail(const char *email);
119 void setServer(const char *server);
120 void setSwVersion(const char *);
121
122 // Do not set the manufacturer ID to anything other than 0. Values other than
123 // 0 are reserved for official Supla products only
124 void setManufacturerId(int16_t);
125
126 // Do not set the product ID to anything other than 0. Values other than
127 // 0 are reserved for official Supla products only
128 void setProductId(int16_t);
129
130 void addFlags(int32_t);
131 void removeFlags(int32_t);
132 bool isSleepingDeviceEnabled();
133
134 int generateHostname(char*, int macSize = 6);
135
136 // Timer with 100 Hz frequency (10 ms)
137 void onTimer(void);
138 // Timer with 2000 Hz frequency (0.5 ms or 1 ms)
139 void onFastTimer(void);
140 void iterate(void);
141
142 void status(int status,
143 const __FlashStringHelper *msg,
144 bool alwaysLog = false);
145 void setStatusFuncImpl(_impl_arduino_status impl_arduino_status);
146 void setServerPort(int value);
147
148 int handleCalcfgFromServer(TSD_DeviceCalCfgRequest *request,
149 TDS_DeviceCalCfgResult *result = nullptr);
150
151 void enterConfigMode();
152 void leaveConfigModeWithoutRestart();
153 void enterNormalMode();
154 // Schedules timeout to restart device. When provided timeout is 0
155 // then restart will be done asap.
156 void scheduleSoftRestart(int timeout = 0);
157 void scheduleProtocolsRestart(int timeout = 0);
158 void softRestart();
159 void saveStateToStorage();
160 void restartCfgModeTimeout(bool requireRestart);
161 void resetToFactorySettings();
162 void disableLocalActionsIfNeeded();
163 void requestCfgMode();
164
165 int getCurrentStatus();
166 bool loadDeviceConfig();
167 bool prepareLastStateLog();
168 char *getLastStateLog();
169 void addLastStateLog(const char*);
170 void enableLastStateLog();
171 void disableLastStateLog();
172 void setRsaPublicKeyPtr(const uint8_t *ptr);
173 const uint8_t *getRsaPublicKey();
174 enum Supla::DeviceMode getDeviceMode() const;
175
176 void setActivityTimeout(_supla_int_t newActivityTimeout);
177 uint32_t getActivityTimeout();
178
179 void handleAction(int event, int action) override;
180
181 // Enables automatic software reset of device in case of network/server
182 // connection problems longer than timeSec.
183 // timeSec is always round down to multiplication of 10 s.
184 // timeSec <= 60 will disable automatic restart.
185 void setAutomaticResetOnConnectionProblem(unsigned int timeSec);
186
187 void setLastStateLogger(Supla::Device::LastStateLogger *logger);
188
189 void setSuplaCACert(const char *);
190 void setSupla3rdPartyCACert(const char *);
191 const char* getSuplaCACert() const;
192
193 Supla::Uptime uptime;
194
195 Supla::Protocol::SuplaSrpc *getSrpcLayer();
196
197 void setCustomHostnamePrefix(const char *prefix);
198
199 void enableNetwork();
200 void disableNetwork();
201 bool getStorageInitResult();
202 bool isSleepingAllowed();
203
204 // Call this method if you want to allow device to work in offline mode
205 // without Wi-Fi network configuration
206 // 1 - offline mode with empty config, but communication protocols may be
207 // enabled
208 // 2 - offline mode only with empty config and communication protocols
209 // disabled
210 // 0 - no offline mode
211 void allowWorkInOfflineMode(int mode = 1);
212
213 bool isRemoteDeviceConfigEnabled() const;
214 void setShowUptimeInChannelState(bool value);
215
216 void setProtoVerboseLog(bool value);
217 bool isOfflineModeDuringConfig() const;
218
219 Supla::Mutex *getTimerAccessMutex();
220
221 void setChannelConflictResolver(
223 void setSubdevicePairingHandler(
225
226 void setMacLengthInHostname(int value);
227
228 void setStatusLed(Supla::Device::StatusLed *led);
229
237 void setLeaveCfgModeAfterInactivityMin(int valueMin);
238
245
252
258 Supla::AutoUpdateMode getAutoUpdateMode() const;
259
260 protected:
265 void iterateSwUpdate();
266
276 bool initSwUpdateInstance(bool performUpdate, int securityOnly = -1);
277
278 int networkIsNotReadyCounter = 0;
279
280 uint32_t deviceRestartTimeoutTimestamp = 0;
281 uint32_t protocolRestartTimeoutTimestamp = 0;
282 uint32_t waitForIterate = 0;
283 uint32_t lastIterateTime = 0;
284 uint32_t enterConfigModeTimestamp = 0;
285 uint32_t forceRestartTimeMs = 0;
286 uint32_t protocolRestartTimeMs = 0;
287 uint32_t resetOnConnectionFailTimeoutSec = 0;
288 uint32_t lastSwUpdateCheckTimestamp = 0;
289 int allowOfflineMode = 1;
290 int currentStatus = STATUS_UNKNOWN;
291
292 enum Supla::DeviceMode deviceMode = Supla::DEVICE_MODE_NOT_SET;
293 bool triggerResetToFactorySettings = false;
294 bool triggerStartLocalWebServer = false;
295 bool triggerStopLocalWebServer = false;
296 bool triggerCheckSwUpdate = false;
297 bool requestNetworkLayerRestart = false;
298 bool isNetworkSetupOk = false;
299 bool skipNetwork = false;
300 bool storageInitResult = false;
301 bool configEmpty = true;
302 bool atLeastOneProtoIsEnabled = false;
303 bool showUptimeInChannelState = true;
304 bool lastStateLogEnabled = true;
305 // used to indicate if begin() method was called - it will be set to
306 // true even if initialization procedure failed for some reason
307 bool initializationDone = false;
308 bool goToConfigModeAsap = false;
309 bool triggerSwUpdateIfAvailable = false;
310
311 uint8_t goToOfflineModeTimeout = 0;
312 uint8_t leaveCfgModeAfterInactivityMin = 5;
313 uint8_t macLengthInHostname = 6;
314
315 Supla::Protocol::SuplaSrpc *srpcLayer = nullptr;
316 Supla::Device::SwUpdate *swUpdate = nullptr;
317 const uint8_t *rsaPublicKey = nullptr;
318 Supla::Element *iterateConnectedPtr = nullptr;
319 _impl_arduino_status impl_arduino_status = nullptr;
320 Supla::Device::LastStateLogger *lastStateLogger = nullptr;
321 char *customHostnamePrefix = nullptr;
322 Supla::Mutex *timerAccessMutex = nullptr;
323 Supla::Device::SubdevicePairingHandler *subdevicePairingHandler = nullptr;
324 Supla::Device::StatusLed *statusLed = nullptr;
325
326 void iterateAlwaysElements(uint32_t _millis);
327 bool iterateNetworkSetup();
328 bool iterateSuplaProtocol(uint32_t _millis);
329 void handleLocalActionTriggers();
330 void checkIfRestartIsNeeded(uint32_t _millis);
331 void createSrpcLayerIfNeeded();
332 void setupDeviceMode();
333};
334
335extern SuplaDeviceClass SuplaDevice;
336#endif // SRC_SUPLADEVICE_H_
Definition SuplaDevice.h:96
Supla::AutoUpdateMode getAutoUpdateMode() const
Returns current automatic firmware update mode.
Definition SuplaDevice.cpp:1623
bool initSwUpdateInstance(bool performUpdate, int securityOnly=-1)
Initializes SW update instance.
Definition SuplaDevice.cpp:529
void setLeaveCfgModeAfterInactivityMin(int valueMin)
Sets the leave configuration mode after inactivity timeout in minutes.
Definition SuplaDevice.cpp:1605
void iterateSwUpdate()
Performs software update if needed.
Definition SuplaDevice.cpp:601
void setAutomaticFirmwareUpdateSupported(bool value)
Sets automatic firmware update support.
Definition SuplaDevice.cpp:1615
bool isAutomaticFirmwareUpdateEnabled() const
Checks if automatic firmware update is supported.
Definition SuplaDevice.cpp:1611
Definition action_handler.h:21
Definition clock.h:29
Definition channel_conflict_resolver.h:27
Definition last_state_logger.h:28
Definition status_led.h:53
Definition subdevice_pairing_handler.h:32
Definition sw_update.h:28
Base class for all elements of SuplaDevice.
Definition element.h:37
Definition local_action.h:52
Definition mutex.h:22
Definition supla_srpc.h:55
Definition uptime.h:24
Definition proto.h:2599
Definition proto.h:2270
Definition proto.h:2260