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/device_mode.h>
25#include "supla/device/security_logger.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;
87class SecurityLogger;
88} // namespace Device
89
90namespace Protocol {
91class SuplaSrpc;
92} // namespace Protocol
93
94} // namespace Supla
95
96class SuplaDeviceClass : public Supla::ActionHandler,
97 public Supla::LocalAction {
98 public:
99 SuplaDeviceClass();
100 ~SuplaDeviceClass();
101
102 void fillStateData(TDSC_ChannelState *channelState);
103 void addClock(Supla::Clock *clock); // DEPRECATED
104 Supla::Clock *getClock();
105
106 bool begin(const char GUID[SUPLA_GUID_SIZE],
107 const char *Server,
108 const char *email,
109 const char authkey[SUPLA_AUTHKEY_SIZE],
110 unsigned char protoVersion = 23);
111
112 bool begin(unsigned char protoVersion = 23);
113
114 // Use ASCII only in name
115 void setName(const char *Name);
116
117 void setGUID(const char GUID[SUPLA_GUID_SIZE]);
118 void setAuthKey(const char authkey[SUPLA_AUTHKEY_SIZE]);
119 void setEmail(const char *email);
120 void setServer(const char *server);
121 void setSwVersion(const char *);
122
123 // Do not set the manufacturer ID to anything other than 0. Values other than
124 // 0 are reserved for official Supla products only
125 void setManufacturerId(int16_t);
126
127 // Do not set the product ID to anything other than 0. Values other than
128 // 0 are reserved for official Supla products only
129 void setProductId(int16_t);
130
131 void addFlags(int32_t);
132 void removeFlags(int32_t);
133 bool isSleepingDeviceEnabled();
134
135 int generateHostname(char*, int macSize = 6);
136
137 // Timer with 100 Hz frequency (10 ms)
138 void onTimer(void);
139 // Timer with 2000 Hz frequency (0.5 ms or 1 ms)
140 void onFastTimer(void);
141 void iterate(void);
142
143 void status(int status,
144 const __FlashStringHelper *msg,
145 bool alwaysLog = false);
146 void setStatusFuncImpl(_impl_arduino_status impl_arduino_status);
147 void setServerPort(int value);
148
149 int handleCalcfgFromServer(TSD_DeviceCalCfgRequest *request,
150 TDS_DeviceCalCfgResult *result = nullptr);
151
152 void enterConfigMode();
153 void leaveConfigModeWithoutRestart();
154 void enterNormalMode();
155 // Schedules timeout to restart device. When provided timeout is 0
156 // then restart will be done asap.
157 void scheduleSoftRestart(int timeout = 0);
158 void scheduleProtocolsRestart(int timeout = 0);
159 void softRestart();
160 void saveStateToStorage();
161 void restartCfgModeTimeout(bool requireRestart);
162 void resetToFactorySettings();
163 void disableLocalActionsIfNeeded();
164 void requestCfgMode();
165
166 int getCurrentStatus();
167 bool loadDeviceConfig();
168 bool prepareLastStateLog();
169 char *getLastStateLog();
170 void addLastStateLog(const char*);
171 void enableLastStateLog();
172 void disableLastStateLog();
173 void setRsaPublicKeyPtr(const uint8_t *ptr);
174 const uint8_t *getRsaPublicKey();
175 enum Supla::DeviceMode getDeviceMode() const;
176
177 void setActivityTimeout(_supla_int_t newActivityTimeout);
178 uint32_t getActivityTimeout();
179
180 void handleAction(int event, int action) override;
181
182 // Enables automatic software reset of device in case of network/server
183 // connection problems longer than timeSec.
184 // timeSec is always round down to multiplication of 10 s.
185 // timeSec <= 60 will disable automatic restart.
186 void setAutomaticResetOnConnectionProblem(unsigned int timeSec);
187
188 void setLastStateLogger(Supla::Device::LastStateLogger *logger);
189
190 void setSuplaCACert(const char *);
191 void setSupla3rdPartyCACert(const char *);
192 const char* getSuplaCACert() const;
193
194 Supla::Uptime uptime;
195
196 Supla::Protocol::SuplaSrpc *getSrpcLayer();
197
198 void setCustomHostnamePrefix(const char *prefix);
199
200 void enableNetwork();
201 void disableNetwork();
202 bool getStorageInitResult();
203 bool isSleepingAllowed();
204
205 // Call this method if you want to allow device to work in offline mode
206 // without Wi-Fi network configuration
207 // 1 - offline mode with empty config, but communication protocols may be
208 // enabled
209 // 2 - offline mode only with empty config and communication protocols
210 // disabled
211 // 0 - no offline mode
212 void allowWorkInOfflineMode(int mode = 1);
213
214 bool isRemoteDeviceConfigEnabled() const;
215 void setShowUptimeInChannelState(bool value);
216
217 void setProtoVerboseLog(bool value);
218 bool isOfflineModeDuringConfig() const;
219
220 Supla::Mutex *getTimerAccessMutex();
221
222 void setChannelConflictResolver(
224 void setSubdevicePairingHandler(
226
227 void setMacLengthInHostname(int value);
228
229 void setStatusLed(Supla::Device::StatusLed *led);
230
238 void setLeaveCfgModeAfterInactivityMin(int valueMin);
239
246
253
258 void identifyStatusLed();
259
265 void testStepStatusLed(int times);
266
275 void addSecurityLog(uint32_t source, const char *log) const;
276
284 void addSecurityLog(Supla::SecurityLogSource source, const char *log) const;
285
292
298 bool isSecurityLogEnabled() const { return securityLogger != nullptr; }
299
300 protected:
305 void iterateSwUpdate();
306
316 bool initSwUpdateInstance(bool performUpdate, int securityOnly = -1);
317
318 int networkIsNotReadyCounter = 0;
319
320 uint32_t deviceRestartTimeoutTimestamp = 0;
321 uint32_t protocolRestartTimeoutTimestamp = 0;
322 uint32_t waitForIterate = 0;
323 uint32_t lastIterateTime = 0;
324 uint32_t enterConfigModeTimestamp = 0;
325 uint32_t forceRestartTimeMs = 0;
326 uint32_t protocolRestartTimeMs = 0;
327 uint32_t resetOnConnectionFailTimeoutSec = 0;
328 uint32_t lastSwUpdateCheckTimestamp = 0;
329 int allowOfflineMode = 1;
330 int currentStatus = STATUS_UNKNOWN;
331
332 enum Supla::DeviceMode deviceMode = Supla::DEVICE_MODE_NOT_SET;
333 bool triggerResetToFactorySettings = false;
334 bool triggerStartLocalWebServer = false;
335 bool triggerStopLocalWebServer = false;
336 bool triggerCheckSwUpdate = false;
337 bool requestNetworkLayerRestart = false;
338 bool isNetworkSetupOk = false;
339 bool skipNetwork = false;
340 bool storageInitResult = false;
341 bool configEmpty = true;
342 bool atLeastOneProtoIsEnabled = false;
343 bool showUptimeInChannelState = true;
344 bool lastStateLogEnabled = true;
345 // used to indicate if begin() method was called - it will be set to
346 // true even if initialization procedure failed for some reason
347 bool initializationDone = false;
348 bool goToConfigModeAsap = false;
349 bool triggerSwUpdateIfAvailable = false;
350 bool cfgModeStartedRemotelyAndNotRefreshed = false;
351
352 uint8_t goToOfflineModeTimeout = 0;
353 uint8_t leaveCfgModeAfterInactivityMin = 5;
354 uint8_t macLengthInHostname = 6;
355
356 Supla::Protocol::SuplaSrpc *srpcLayer = nullptr;
357 Supla::Device::SwUpdate *swUpdate = nullptr;
358 const uint8_t *rsaPublicKey = nullptr;
359 Supla::Element *iterateConnectedPtr = nullptr;
360 _impl_arduino_status impl_arduino_status = nullptr;
361 Supla::Device::LastStateLogger *lastStateLogger = nullptr;
362 char *customHostnamePrefix = nullptr;
363 Supla::Mutex *timerAccessMutex = nullptr;
364 Supla::Device::SubdevicePairingHandler *subdevicePairingHandler = nullptr;
365 Supla::Device::StatusLed *statusLed = nullptr;
366 Supla::Device::SecurityLogger *securityLogger = nullptr;
367
368 void iterateAlwaysElements(uint32_t _millis);
369 bool iterateNetworkSetup();
370 bool iterateSuplaProtocol(uint32_t _millis);
371 void handleLocalActionTriggers();
372 void checkIfRestartIsNeeded();
373 void createSrpcLayerIfNeeded();
374 void setupDeviceMode();
375};
376
377extern SuplaDeviceClass SuplaDevice;
378#endif // SRC_SUPLADEVICE_H_
Definition SuplaDevice.h:97
void identifyStatusLed()
Triggers "identify" action on status LED.
Definition SuplaDevice.cpp:1742
void testStepStatusLed(int times)
Triggers special status LED flashing sequence based on provided "times".
Definition SuplaDevice.cpp:1748
void addSecurityLog(uint32_t source, const char *log) const
Adds security log.
Definition SuplaDevice.cpp:1759
bool initSwUpdateInstance(bool performUpdate, int securityOnly=-1)
Initializes SW update instance.
Definition SuplaDevice.cpp:553
bool isSecurityLogEnabled() const
Checks if security log is enabled.
Definition SuplaDevice.h:298
void setSecurityLogger(Supla::Device::SecurityLogger *logger)
Sets instance of Supla::Device::SecurityLogger.
Definition SuplaDevice.cpp:1754
void setLeaveCfgModeAfterInactivityMin(int valueMin)
Sets the leave configuration mode after inactivity timeout in minutes.
Definition SuplaDevice.cpp:1720
void iterateSwUpdate()
Performs software update if needed.
Definition SuplaDevice.cpp:625
void setAutomaticFirmwareUpdateSupported(bool value)
Sets automatic firmware update support.
Definition SuplaDevice.cpp:1730
bool isAutomaticFirmwareUpdateEnabled() const
Checks if automatic firmware update is supported.
Definition SuplaDevice.cpp:1726
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:61
Definition status_led.h:57
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:2615
Definition proto.h:2273
Definition proto.h:2263