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/action_handler.h>
22#include <supla/device/device_mode.h>
23#include <supla/local_action.h>
24#include <supla/uptime.h>
25
26#include "supla/device/security_logger.h"
27
28#define STATUS_UNKNOWN -1
29#define STATUS_ALREADY_INITIALIZED 1
30#define STATUS_MISSING_NETWORK_INTERFACE 2
31#define STATUS_UNKNOWN_SERVER_ADDRESS 3
32#define STATUS_UNKNOWN_LOCATION_ID 4
33#define STATUS_INITIALIZED 5
34#define STATUS_SERVER_DISCONNECTED 6
35#define STATUS_ITERATE_FAIL 7
36#define STATUS_NETWORK_DISCONNECTED 8
37#define STATUS_ALL_PROTOCOLS_DISABLED 9
38
39#define STATUS_REGISTER_IN_PROGRESS 10 // Don't change
40#define STATUS_REGISTERED_AND_READY 17 // Don't change
41
42#define STATUS_TEMPORARILY_UNAVAILABLE 21
43#define STATUS_INVALID_GUID 22
44#define STATUS_CHANNEL_LIMIT_EXCEEDED 23
45#define STATUS_PROTOCOL_VERSION_ERROR 24
46#define STATUS_BAD_CREDENTIALS 25
47#define STATUS_LOCATION_CONFLICT 26
48#define STATUS_CHANNEL_CONFLICT 27
49#define STATUS_DEVICE_IS_DISABLED 28
50#define STATUS_LOCATION_IS_DISABLED 29
51#define STATUS_DEVICE_LIMIT_EXCEEDED 30
52#define STATUS_REGISTRATION_DISABLED 31
53#define STATUS_MISSING_CREDENTIALS 32
54#define STATUS_INVALID_AUTHKEY 33
55#define STATUS_NO_LOCATION_AVAILABLE 34
56#define STATUS_UNKNOWN_ERROR 35
57#define STATUS_COUNTRY_REJECTED 36
58
59#define STATUS_CONFIG_MODE 40
60#define STATUS_SOFTWARE_RESET 41
61#define STATUS_SW_DOWNLOAD 50
62#define STATUS_SUPLA_PROTOCOL_DISABLED 60
63#define STATUS_TEST_WAIT_FOR_CFG_BUTTON 70
64#define STATUS_OFFLINE_MODE 80
65#define STATUS_NOT_CONFIGURED_MODE 90
66
67typedef void (*_impl_arduino_status)(int status, const char *msg);
68
69#ifdef ARDUINO
70class __FlashStringHelper;
71#else
72#define __FlashStringHelper char
73#endif
74
75namespace Supla {
76class Clock;
77class Mutex;
78class Element;
79
80// 10 days
81constexpr uint32_t AutomaticOtaCheckInterval = (10ULL * 24 * 60 * 60 * 1000);
82
83enum class SwUpdateMode : uint8_t {
84 NotSet,
85 PeriodicCheckAndUpdate,
86 OnlyCheck,
87 CheckAndUpdate,
88 RetryCheckAndUpdate,
89};
90
94enum class InitialMode : uint8_t {
98 StartInCfgMode = 0,
103 StartOffline = 1,
107 StartWithCfgModeThenOffline = 2,
111 StartInNotConfiguredMode = 3
112};
113
114enum class CfgModeState : uint8_t {
115 NotSet = 0,
116 CfgModeStartedFor1hPending = 1,
117 CfgModeStartedPending = 2,
118 CfgModeOngoing = 3,
119 Done = 4,
120};
121
122struct ConfigurationState {
123 uint8_t wifiEnabled : 1;
124 uint8_t wifiSsidFilled : 1;
125 uint8_t wifiPassFilled : 1;
126 uint8_t protocolFilled : 1;
127 uint8_t protocolNotEmpty : 1;
128 uint8_t configNotComplete : 1;
129 uint8_t atLeastOneProtoIsEnabled : 1;
130
131 bool isEmpty() const;
132
133 ConfigurationState() {
134 wifiEnabled = 0;
135 wifiSsidFilled = 0;
136 wifiPassFilled = 0;
137 protocolFilled = 0;
138 protocolNotEmpty = 0;
139 configNotComplete = 0;
140 atLeastOneProtoIsEnabled = 0;
141 }
142};
143
144const char *getInitialModeName(const InitialMode mode);
145
146namespace Device {
147class SwUpdate;
148class ChannelConflictResolver;
149class SubdevicePairingHandler;
150class StatusLed;
151class LastStateLogger;
152class SecurityLogger;
153} // namespace Device
154
155namespace Protocol {
156class SuplaSrpc;
157} // namespace Protocol
158
159} // namespace Supla
160
161class SuplaDeviceClass : public Supla::ActionHandler,
162 public Supla::LocalAction {
163 public:
164 SuplaDeviceClass();
165 ~SuplaDeviceClass();
166
167 void fillStateData(TDSC_ChannelState *channelState) const;
168 void addClock(Supla::Clock *clock); // DEPRECATED
169 Supla::Clock *getClock() const;
170
171 bool begin(const char GUID[SUPLA_GUID_SIZE],
172 const char *Server,
173 const char *email,
174 const char authkey[SUPLA_AUTHKEY_SIZE],
175 unsigned char protoVersion = 23);
176
177 bool begin(unsigned char protoVersion = 23);
178
179 // Use ASCII only in name
180 void setName(const char *Name);
181
182 void setGUID(const char GUID[SUPLA_GUID_SIZE]);
183 void setAuthKey(const char authkey[SUPLA_AUTHKEY_SIZE]);
184 void setEmail(const char *email);
185 void setServer(const char *server);
186 void setSwVersion(const char *);
187
188 // Do not set the manufacturer ID to anything other than 0. Values other than
189 // 0 are reserved for official Supla products only
190 void setManufacturerId(int16_t);
191
192 // Do not set the product ID to anything other than 0. Values other than
193 // 0 are reserved for official Supla products only
194 void setProductId(int16_t);
195
196 void addFlags(int32_t);
197 void removeFlags(int32_t);
198 bool isSleepingDeviceEnabled() const;
199
200 int generateHostname(char *, int macSize = 6);
201
202 // Timer with 100 Hz frequency (10 ms)
203 void onTimer(void);
204 // Timer with 2000 Hz frequency (0.5 ms or 1 ms)
205 void onFastTimer(void);
206 void iterate(void);
207
208 void status(int status,
209 const __FlashStringHelper *msg,
210 bool alwaysLog = false);
211 void setStatusFuncImpl(_impl_arduino_status impl_arduino_status);
212 void setServerPort(int value);
213
214 int handleCalcfgFromServer(TSD_DeviceCalCfgRequest *request,
215 TDS_DeviceCalCfgResult *result = nullptr);
216
217 void enterConfigMode();
218 void leaveConfigModeWithoutRestart();
219 void enterNormalMode();
220 // Schedules timeout to restart device. When provided timeout is 0
221 // then restart will be done asap.
222 void scheduleSoftRestart(int timeout = 0);
223 void scheduleProtocolsRestart(int timeout = 0);
224 void softRestart();
225 void saveStateToStorage();
226 void restartCfgModeTimeout(bool requireRestart);
227 void resetToFactorySettings();
228 void disableLocalActionsIfNeeded();
229 void requestCfgMode();
230
231 int8_t getCurrentStatus() const;
232 bool loadDeviceConfig();
233 bool prepareLastStateLog();
234 char *getLastStateLog();
235 void addLastStateLog(const char *);
236 void enableLastStateLog();
237 void disableLastStateLog();
238 void setRsaPublicKeyPtr(const uint8_t *ptr);
239 const uint8_t *getRsaPublicKey() const;
240 enum Supla::DeviceMode getDeviceMode() const;
241
242 void setActivityTimeout(_supla_int_t newActivityTimeout);
243 uint32_t getActivityTimeout();
244
245 void handleAction(int event, int action) override;
246
255 void setAutomaticResetOnConnectionProblem(unsigned int timeSec);
256
257 void setLastStateLogger(Supla::Device::LastStateLogger *logger);
258
259 void setSuplaCACert(const char *);
260 void setSupla3rdPartyCACert(const char *);
261 const char *getSuplaCACert() const;
262
263 Supla::Uptime uptime;
264
265 Supla::Protocol::SuplaSrpc *getSrpcLayer();
266
267 void enableNetwork();
268 void disableNetwork();
269 bool getStorageInitResult();
270 bool isSleepingAllowed();
271
283 void allowWorkInOfflineMode(int mode = 1);
284
292 void setInitialMode(Supla::InitialMode mode);
293
300 Supla::InitialMode getInitialMode() const {
301 return initialMode;
302 }
303
309 bool isRemoteDeviceConfigEnabled() const;
310
316 void setShowUptimeInChannelState(bool value);
317
323 void setProtoVerboseLog(bool value);
324
330 void setPermanentWebInterface(bool value = true);
331
332 Supla::Mutex *getTimerAccessMutex();
333
334 void setChannelConflictResolver(
336 void setSubdevicePairingHandler(
338
347 void setMacLengthInHostname(int value);
348
361 void setCustomHostnamePrefix(const char *prefix);
362
363 void setStatusLed(Supla::Device::StatusLed *led);
364
372 void setLeaveCfgModeAfterInactivityMin(int valueMin);
373
383
390
397
402 void identifyStatusLed();
403
409 void testStepStatusLed(int times);
410
419 void addSecurityLog(uint32_t source, const char *log) const;
420
428 void addSecurityLog(Supla::SecurityLogSource source, const char *log) const;
429
436
442 bool isSecurityLogEnabled() const;
443
444 protected:
449 void iterateSwUpdate();
450
460 bool initSwUpdateInstance(Supla::SwUpdateMode mode, int securityOnly = -1);
461
462 void iterateAlwaysElements(uint32_t _millis);
463 bool iterateNetworkSetup();
464 bool iterateSuplaProtocol(uint32_t _millis);
465 void handleLocalActionTriggers();
466 void checkIfLeaveCfgModeOrRestartIsNeeded();
467 void createSrpcLayerIfNeeded();
468 void setupDeviceMode();
469
470 uint32_t networkIsNotReadyCounter = 0;
471 uint32_t deviceRestartTimeoutTimestamp = 0;
472 uint32_t protocolRestartTimeoutTimestamp = 0;
473 uint32_t waitForIterate = 0;
474 uint32_t lastIterateTime = 0;
475 uint32_t enterConfigModeTimestamp = 0;
476 uint32_t forceRestartTimeMs = 0;
477 uint32_t protocolRestartTimeMs = 0;
478 uint32_t resetOnConnectionFailTimeoutSec = 0;
479 uint32_t lastSwUpdateCheckTimestamp = 0;
480
481 enum Supla::DeviceMode deviceMode = Supla::DEVICE_MODE_NOT_SET;
482 bool triggerResetToFactorySettings = false;
483 bool triggerStartLocalWebServer = false;
484 bool triggerStopLocalWebServer = false;
485 bool triggerCheckSwUpdate = false;
486 bool requestNetworkLayerRestart = false;
487 bool isNetworkSetupOk = false;
488 bool skipNetwork = false;
489 bool storageInitResult = false;
490 bool showUptimeInChannelState = true;
491 bool lastStateLogEnabled = true;
492 // used to indicate if begin() method was called - it will be set to
493 // true even if initialization procedure failed for some reason
494 bool initializationDone = false;
495 bool goToConfigModeAsap = false;
496
497 // used for permanent web server
498 bool startPermanentWebInterface = false;
499 bool runningPermanentWebInterface = false;
500
501 uint8_t leaveCfgModeAfterInactivityMin = 5;
502 uint8_t macLengthInHostname = 6;
503 int8_t currentStatus = STATUS_UNKNOWN;
504 uint8_t swUpdateAttempts = 0;
505
506 Supla::InitialMode initialMode = Supla::InitialMode::StartInNotConfiguredMode;
507 Supla::CfgModeState cfgModeState = Supla::CfgModeState::NotSet;
508 Supla::ConfigurationState configurationState = {};
509
510 Supla::Protocol::SuplaSrpc *srpcLayer = nullptr;
511 Supla::Device::SwUpdate *swUpdate = nullptr;
512 Supla::Element *iterateConnectedPtr = nullptr;
513 Supla::Device::LastStateLogger *lastStateLogger = nullptr;
514 Supla::Mutex *timerAccessMutex = nullptr;
515 Supla::Device::SubdevicePairingHandler *subdevicePairingHandler = nullptr;
516 Supla::Device::StatusLed *statusLed = nullptr;
517 Supla::Device::SecurityLogger *securityLogger = nullptr;
518 const uint8_t *rsaPublicKey = nullptr;
519 char *customHostnamePrefix = nullptr;
520 _impl_arduino_status impl_arduino_status = nullptr;
521};
522
523extern SuplaDeviceClass SuplaDevice;
524#endif // SRC_SUPLADEVICE_H_
Definition security_logger.h:62
Definition SuplaDevice.h:162
void identifyStatusLed()
Triggers "identify" action on status LED.
Definition SuplaDevice.cpp:1872
void setShowUptimeInChannelState(bool value)
Sets if "uptime" should be added to channel state (i).
Definition SuplaDevice.cpp:1798
void setCustomHostnamePrefix(const char *prefix)
Sets custom hostname prefix to be used in hostname and Wi-Fi Soft AP name.
Definition SuplaDevice.cpp:1699
void testStepStatusLed(int times)
Triggers special status LED flashing sequence based on provided "times".
Definition SuplaDevice.cpp:1879
void setInitialMode(Supla::InitialMode mode)
Sets the initial mode of the device.
Definition SuplaDevice.cpp:1910
void addSecurityLog(uint32_t source, const char *log) const
Adds security log.
Definition SuplaDevice.cpp:1893
void allowWorkInOfflineMode(int mode=1)
Allows device to work in offline mode DEPRECATED: use setInitialMode() instead.
Definition SuplaDevice.cpp:1767
void setProtoVerboseLog(bool value)
Enables/disables verbose logging of Supla protocol.
Definition SuplaDevice.cpp:1802
bool isSecurityLogEnabled() const
Checks if security log is enabled.
Definition SuplaDevice.cpp:1906
void setSecurityLogger(Supla::Device::SecurityLogger *logger)
Sets instance of Supla::Device::SecurityLogger.
Definition SuplaDevice.cpp:1885
void setMacLengthInHostname(int value)
Sets the byte length of MAC address in hostname and Wi-Fi Soft AP name.
Definition SuplaDevice.cpp:1833
void setAutomaticResetOnConnectionProblem(unsigned int timeSec)
Enables automatic software reset of device in case of network/server connection problems longer than ...
Definition SuplaDevice.cpp:1649
Supla::InitialMode getInitialMode() const
Returns the initial mode of the device @See Supla::InitialMode.
Definition SuplaDevice.h:300
bool isLeaveCfgModeAfterInactivityEnabled() const
Checks if leave configuration mode after inactivity is enabled.
Definition SuplaDevice.cpp:1957
void setLeaveCfgModeAfterInactivityMin(int valueMin)
Sets the leave configuration mode after inactivity timeout in minutes.
Definition SuplaDevice.cpp:1849
void iterateSwUpdate()
Performs software update if needed.
Definition SuplaDevice.cpp:694
void setAutomaticFirmwareUpdateSupported(bool value)
Sets automatic firmware update support.
Definition SuplaDevice.cpp:1860
bool isRemoteDeviceConfigEnabled() const
Checks if remote device configuration is enabled.
Definition SuplaDevice.cpp:1786
bool isAutomaticFirmwareUpdateEnabled() const
Checks if automatic firmware update is supported.
Definition SuplaDevice.cpp:1856
bool initSwUpdateInstance(Supla::SwUpdateMode mode, int securityOnly=-1)
Initializes SW update instance.
Definition SuplaDevice.cpp:610
void setPermanentWebInterface(bool value=true)
Enables/disables the permanent web server.
Definition SuplaDevice.cpp:1809
Definition action_handler.h:21
Definition clock.h:29
Definition channel_conflict_resolver.h:27
Definition last_state_logger.h:28
Definition security_logger.h:62
Definition status_led.h:58
Definition subdevice_pairing_handler.h:32
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 SuplaDevice.h:122
Definition proto.h:2646
Definition proto.h:2296
Definition proto.h:2286