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/network/network.h>
22#include <supla/storage/config.h>
23#include <supla/uptime.h>
24#include <supla/clock/clock.h>
25#include <supla/device/last_state_logger.h>
26#include <supla/action_handler.h>
27#include <supla/protocol/supla_srpc.h>
28#include <supla/log_wrapper.h>
29#include <supla/local_action.h>
30
31#define STATUS_UNKNOWN -1
32#define STATUS_ALREADY_INITIALIZED 1
33#define STATUS_MISSING_NETWORK_INTERFACE 2
34#define STATUS_UNKNOWN_SERVER_ADDRESS 3
35#define STATUS_UNKNOWN_LOCATION_ID 4
36#define STATUS_INITIALIZED 5
37#define STATUS_SERVER_DISCONNECTED 6
38#define STATUS_ITERATE_FAIL 7
39#define STATUS_NETWORK_DISCONNECTED 8
40#define STATUS_ALL_PROTOCOLS_DISABLED 9
41
42#define STATUS_REGISTER_IN_PROGRESS 10 // Don't change
43#define STATUS_REGISTERED_AND_READY 17 // Don't change
44
45#define STATUS_TEMPORARILY_UNAVAILABLE 21
46#define STATUS_INVALID_GUID 22
47#define STATUS_CHANNEL_LIMIT_EXCEEDED 23
48#define STATUS_PROTOCOL_VERSION_ERROR 24
49#define STATUS_BAD_CREDENTIALS 25
50#define STATUS_LOCATION_CONFLICT 26
51#define STATUS_CHANNEL_CONFLICT 27
52#define STATUS_DEVICE_IS_DISABLED 28
53#define STATUS_LOCATION_IS_DISABLED 29
54#define STATUS_DEVICE_LIMIT_EXCEEDED 30
55#define STATUS_REGISTRATION_DISABLED 31
56#define STATUS_MISSING_CREDENTIALS 32
57#define STATUS_INVALID_AUTHKEY 33
58#define STATUS_NO_LOCATION_AVAILABLE 34
59#define STATUS_UNKNOWN_ERROR 35
60#define STATUS_COUNTRY_REJECTED 36
61
62#define STATUS_CONFIG_MODE 40
63#define STATUS_SOFTWARE_RESET 41
64#define STATUS_SW_DOWNLOAD 50
65#define STATUS_SUPLA_PROTOCOL_DISABLED 60
66#define STATUS_TEST_WAIT_FOR_CFG_BUTTON 70
67#define STATUS_OFFLINE_MODE 80
68
69typedef void (*_impl_arduino_status)(int status, const char *msg);
70
71#ifdef ARDUINO
72class __FlashStringHelper;
73#else
74#define __FlashStringHelper char
75#endif
76
77namespace Supla {
78namespace Device {
79 enum RequestConfigModeType {
80 None,
81 WithTimeout,
82 WithoutTimeout
83 };
84class SwUpdate;
85class Mutex;
88class StatusLed;
89} // namespace Device
90} // namespace Supla
91
92class SuplaDeviceClass : public Supla::ActionHandler,
93 public Supla::LocalAction {
94 public:
95 SuplaDeviceClass();
96 ~SuplaDeviceClass();
97
98 void fillStateData(TDSC_ChannelState *channelState);
99 void addClock(Supla::Clock *clock); // DEPRECATED
100 Supla::Clock *getClock();
101
102 bool begin(const char GUID[SUPLA_GUID_SIZE],
103 const char *Server,
104 const char *email,
105 const char authkey[SUPLA_AUTHKEY_SIZE],
106 unsigned char protoVersion = 23);
107
108 bool begin(unsigned char protoVersion = 23);
109
110 // Use ASCII only in name
111 void setName(const char *Name);
112
113 void setGUID(const char GUID[SUPLA_GUID_SIZE]);
114 void setAuthKey(const char authkey[SUPLA_AUTHKEY_SIZE]);
115 void setEmail(const char *email);
116 void setServer(const char *server);
117 void setSwVersion(const char *);
118
119 // Do not set the manufacturer ID to anything other than 0. Values other than
120 // 0 are reserved for official Supla products only
121 void setManufacturerId(int16_t);
122
123 // Do not set the product ID to anything other than 0. Values other than
124 // 0 are reserved for official Supla products only
125 void setProductId(int16_t);
126
127 void addFlags(int32_t);
128 void removeFlags(int32_t);
129 bool isSleepingDeviceEnabled();
130
131 int generateHostname(char*, int macSize = 6);
132
133 // Timer with 100 Hz frequency (10 ms)
134 void onTimer(void);
135 // Timer with 2000 Hz frequency (0.5 ms or 1 ms)
136 void onFastTimer(void);
137 void iterate(void);
138
139 void status(int status,
140 const __FlashStringHelper *msg,
141 bool alwaysLog = false);
142 void setStatusFuncImpl(_impl_arduino_status impl_arduino_status);
143 void setServerPort(int value);
144
145 int handleCalcfgFromServer(TSD_DeviceCalCfgRequest *request,
146 TDS_DeviceCalCfgResult *result = nullptr);
147
148 void enterConfigMode();
149 void leaveConfigModeWithoutRestart();
150 void enterNormalMode();
151 // Schedules timeout to restart device. When provided timeout is 0
152 // then restart will be done asap.
153 void scheduleSoftRestart(int timeout = 0);
154 void scheduleProtocolsRestart(int timeout = 0);
155 void softRestart();
156 void saveStateToStorage();
157 void disableCfgModeTimeout();
158 void resetToFactorySettings();
159 void disableLocalActionsIfNeeded();
160 void requestCfgMode(Supla::Device::RequestConfigModeType);
161
162 int getCurrentStatus();
163 bool loadDeviceConfig();
164 bool prepareLastStateLog();
165 char *getLastStateLog();
166 void addLastStateLog(const char*);
167 void enableLastStateLog();
168 void disableLastStateLog();
169 void setRsaPublicKeyPtr(const uint8_t *ptr);
170 const uint8_t *getRsaPublicKey();
171 enum Supla::DeviceMode getDeviceMode() const;
172
173 void setActivityTimeout(_supla_int_t newActivityTimeout);
174 uint32_t getActivityTimeout();
175
176 void handleAction(int event, int action) override;
177
178 // Enables automatic software reset of device in case of network/server
179 // connection problems longer than timeSec.
180 // timeSec is always round down to multiplication of 10 s.
181 // timeSec <= 60 will disable automatic restart.
182 void setAutomaticResetOnConnectionProblem(unsigned int timeSec);
183
184 void setLastStateLogger(Supla::Device::LastStateLogger *logger);
185
186 void setSuplaCACert(const char *);
187 void setSupla3rdPartyCACert(const char *);
188
189 Supla::Uptime uptime;
190
191 Supla::Protocol::SuplaSrpc *getSrpcLayer();
192
193 void setCustomHostnamePrefix(const char *prefix);
194
195 void enableNetwork();
196 void disableNetwork();
197 bool getStorageInitResult();
198 bool isSleepingAllowed();
199
200 // Call this method if you want to allow device to work in offline mode
201 // without Wi-Fi network configuration
202 // 1 - offline mode with empty config, but communication protocols may be
203 // enabled
204 // 2 - offline mode only with empty config and communication protocols
205 // disabled
206 // 0 - no offline mode
207 void allowWorkInOfflineMode(int mode = 1);
208
209 bool isRemoteDeviceConfigEnabled() const;
210 void setShowUptimeInChannelState(bool value);
211
212 void setProtoVerboseLog(bool value);
213 bool isOfflineModeDuringConfig() const;
214
215 Supla::Mutex *getTimerAccessMutex();
216
217 void setChannelConflictResolver(
219 void setSubdevicePairingHandler(
221
222 void setMacLengthInHostname(int value);
223
224 void setStatusLed(Supla::Device::StatusLed *led);
225
226 protected:
227 int networkIsNotReadyCounter = 0;
228
229 uint32_t deviceRestartTimeoutTimestamp = 0;
230 uint32_t protocolRestartTimeoutTimestamp = 0;
231 uint32_t waitForIterate = 0;
232 uint32_t lastIterateTime = 0;
233 uint32_t enterConfigModeTimestamp = 0;
234 uint32_t forceRestartTimeMs = 0;
235 uint32_t protocolRestartTimeMs = 0;
236 uint32_t resetOnConnectionFailTimeoutSec = 0;
237 int allowOfflineMode = 0;
238 int currentStatus = STATUS_UNKNOWN;
239
240 enum Supla::DeviceMode deviceMode = Supla::DEVICE_MODE_NOT_SET;
241 Supla::Device::RequestConfigModeType goToConfigModeAsap = Supla::Device::None;
242 bool triggerResetToFactorySettings = false;
243 bool triggerStartLocalWebServer = false;
244 bool triggerStopLocalWebServer = false;
245 bool triggerCheckSwUpdate = false;
246 bool requestNetworkLayerRestart = false;
247 bool isNetworkSetupOk = false;
248 bool skipNetwork = false;
249 bool storageInitResult = false;
250 bool configEmpty = true;
251 bool atLeastOneProtoIsEnabled = false;
252 bool showUptimeInChannelState = true;
253 bool lastStateLogEnabled = true;
254 // used to indicate if begin() method was called - it will be set to
255 // true even if initialization procedure failed for some reason
256 bool initializationDone = false;
257
258 uint8_t goToOfflineModeTimeout = 0;
259 uint8_t macLengthInHostname = 6;
260
261 Supla::Protocol::SuplaSrpc *srpcLayer = nullptr;
262 Supla::Device::SwUpdate *swUpdate = nullptr;
263 const uint8_t *rsaPublicKey = nullptr;
264 Supla::Element *iterateConnectedPtr = nullptr;
265 _impl_arduino_status impl_arduino_status = nullptr;
266 Supla::Device::LastStateLogger *lastStateLogger = nullptr;
267 char *customHostnamePrefix = nullptr;
268 Supla::Mutex *timerAccessMutex = nullptr;
269 Supla::Device::SubdevicePairingHandler *subdevicePairingHandler = nullptr;
270 Supla::Device::StatusLed *statusLed = nullptr;
271
272 void iterateAlwaysElements(uint32_t _millis);
273 bool iterateNetworkSetup();
274 bool iterateSuplaProtocol(uint32_t _millis);
275 void handleLocalActionTriggers();
276 void checkIfRestartIsNeeded(uint32_t _millis);
277 void createSrpcLayerIfNeeded();
278 void setupDeviceMode();
279};
280
281extern SuplaDeviceClass SuplaDevice;
282#endif // SRC_SUPLADEVICE_H_
Definition SuplaDevice.h:93
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:33
Definition local_action.h:53
Definition mutex.h:22
Definition supla_srpc.h:55
Definition uptime.h:24
Definition proto.h:2575
Definition proto.h:2256
Definition proto.h:2246