supla-device
Loading...
Searching...
No Matches
element_with_channel_actions.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_ELEMENT_WITH_CHANNEL_ACTIONS_H_
5#define SRC_SUPLA_ELEMENT_WITH_CHANNEL_ACTIONS_H_
6
7#include <stdint.h>
8#include <supla-common/proto.h>
9#include <supla/apply_config_result.h>
10#include <supla/element.h>
11#include <supla/local_action.h>
12
13namespace Supla {
14
15enum class ChannelConfigState : uint8_t {
16 None = 0,
17 LocalChangePending = 1,
18 SetChannelConfigSend = 2,
19 SetChannelConfigFailed = 3,
20 WaitForConfigFinished = 4,
21 ResendConfig = 5,
22 LocalChangeSent = 6,
23 LAST_STATE_MAX // Sentinel: one past the last valid state.
24};
25
26static_assert(static_cast<uint8_t>(ChannelConfigState::LAST_STATE_MAX) <= 16,
27 "ChannelConfigState does not fit in 4 bits");
28
29#pragma pack(push, 1)
31 private:
32 // This is a runtime-only bitmap. The local-change storage keeps the raw
33 // value as uint32_t, so the bit assignments must remain compatible with
34 // SUPLA_CONFIG_TYPE_* values used by legacy devices.
35 uint8_t all = 0;
36
37 public:
38 bool isSet(int configType) const;
39 void clear(int configType);
40 void clearAll();
41 void setAll(uint8_t values, int channelNumber = -1);
42 uint8_t getAll() const;
43 void set(int configType, bool value = true);
44 bool operator!=(const ConfigTypesBitmap &other) const;
45};
46#pragma pack(pop)
47
48static_assert(sizeof(ConfigTypesBitmap) == 1,
49 "ConfigTypesBitmap must remain one byte");
50
51class Condition;
52class ActionHandler;
53namespace Protocol {
54class SuplaSrpc;
55} // namespace Protocol
57 public:
59 ElementMode mode = ElementMode::Registered);
61
62 // Override local action methods in order to delegate execution to Channel
63 void addAction(uint16_t action,
64 ActionHandler &client, // NOLINT(runtime/references)
65 uint16_t event,
66 bool alwaysEnabled = false) override;
67 void addAction(uint16_t action, ActionHandler *client, uint16_t event,
68 bool alwaysEnabled = false) override;
76 virtual void addAction(uint16_t action,
77 ActionHandler &client, // NOLINT(runtime/references)
78 uint16_t event,
79 Supla::Condition *condition,
80 bool alwaysEnabled = false);
84 virtual void addAction(uint16_t action, ActionHandler *client,
85 uint16_t event,
86 Supla::Condition *condition,
87 bool alwaysEnabled = false);
88 virtual void addAction(uint16_t action,
89 ActionHandler &client, // NOLINT(runtime/references)
90 Supla::Condition *condition,
91 bool alwaysEnabled = false);
92 virtual void addAction(uint16_t action, ActionHandler *client,
93 Supla::Condition *condition,
94 bool alwaysEnabled = false);
95
96 bool isEventAlreadyUsed(uint16_t event, bool ignoreAlwaysEnabled) override;
97 void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc) override;
98 bool iterateConnected() override;
99 // Keep the deprecated Element overload visible for source compatibility.
101 void handleChannelConfigFinished() override;
102 void handleChannelConfigFinished(int channelNumber) override;
104 bool local = false) override;
106 bool altSchedule = false,
107 bool local = false) override;
109 TSDS_SetChannelConfigResult *result) override;
110 void purgeConfig() override;
111
112
113 void runAction(uint16_t event) const override;
114 void runAction(
115 uint16_t event,
116 const ActionAllowList &allowOnlyActions) const override;
117
118 bool isAnyUpdatePending() const override;
119
120 // methods to override for channels with runtime config support
121 virtual ApplyConfigResult applyChannelConfig(TSD_ChannelConfig *result,
122 bool local);
123 virtual void fillChannelConfig(void *channelConfig, int *size, uint8_t index);
124
125 void triggerSetChannelConfig(
126 int configType = SUPLA_CONFIG_TYPE_DEFAULT,
127 bool localChange = false);
128
129 protected:
130 // Config handlers with their own summary suppress the generic success log.
131 virtual bool hasChannelConfigSummary(uint8_t) const { return false; }
132 // returns true if function was changed (previous one was different)
133 virtual bool setAndSaveFunction(uint32_t channelFunction);
134 virtual bool loadFunctionFromConfig();
135 virtual bool shouldProcessChannelFunctionFromConfig() const;
136 bool setAndSaveConfigChangeFlag(bool value);
137 virtual bool saveConfigChangeFlag() const;
138 virtual bool loadConfigChangeFlag();
139 void clearChannelConfigChangedFlag();
140 void markChannelConfigReceived(int configType);
141 void markAllChannelConfigsReceived();
142 bool isChannelConfigFinishedReceived() const;
143 bool isLocalChannelConfigChangePending(int configType) const;
144 bool iterateConfigExchange();
150 int getNextConfigType() const;
151 int getNextLocalConfigType() const;
152 bool setLocalConfigChange(int configType, bool value = true);
153 void clearLocalConfigChanges(int configType, int secondConfigType = -1);
154 uint8_t getUsedLocalConfigTypes() const;
155 ChannelConfigState getChannelConfigState() const {
156 return static_cast<ChannelConfigState>(channelConfigState);
157 }
158 void setChannelConfigState(ChannelConfigState state) {
159 channelConfigState = static_cast<uint8_t>(state);
160 }
161 // Keep exchange state in one byte. The retry counter must stay in 2 bits.
162 // Older GCC warns about the enum's full underlying range in a bitfield.
163 uint8_t channelConfigState : 4;
164 uint8_t setChannelConfigAttempts : 2;
165 uint8_t configFinishedReceived : 1;
166 uint8_t reserved : 1;
167 // Server rejections, independent of incoming invalid data and send attempts.
168 uint8_t weeklyConfigFailures : 2;
169 uint8_t altWeeklyConfigFailures : 2;
170 uint8_t sentWeeklyConfigType : 2;
171 ConfigTypesBitmap locallyChangedConfigTypes;
172 ConfigTypesBitmap usedConfigTypes;
173 ConfigTypesBitmap receivedConfigTypes;
174
175 uint8_t finishChannelConfig(
176 TSD_ChannelConfig *result, Supla::ApplyConfigResult applyResult,
177 bool local);
178
179 private:
180 uint8_t getWeeklyConfigFailures(int configType) const;
181 void resetWeeklyConfigFailures(int configType);
182};
183
184}; // namespace Supla
185
186#endif // SRC_SUPLA_ELEMENT_WITH_CHANNEL_ACTIONS_H_
Definition local_action.h:15
Definition action_handler.h:8
Definition condition.h:14
Definition element_with_channel_actions.h:56
void purgeConfig() override
Removes all configration data related to the element from Storage::Config.
Definition element_with_channel_actions.cpp:728
void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc) override
Method called each time when device successfully registers to Supla server.
Definition element_with_channel_actions.cpp:446
uint8_t handleChannelConfig(TSD_ChannelConfig *result, bool local=false) override
Handles "set channel config" request from server.
Definition element_with_channel_actions.cpp:523
bool isAnyUpdatePending() const override
Returns true if any update is pending.
Definition element_with_channel_actions.cpp:383
void handleSetChannelConfigResult(TSDS_SetChannelConfigResult *result) override
Handles "set channel config" reply from server.
Definition element_with_channel_actions.cpp:652
int getNextConfigType() const
Returns the next config type to be sent.
Definition element_with_channel_actions.cpp:977
void handleChannelConfigFinished() override
Handles "channel config finished" message from server.
Definition element_with_channel_actions.cpp:479
bool iterateConnected() override
Method called on each SuplaDevice iteration when device is connected and registered to Supla server o...
Definition element_with_channel_actions.cpp:511
uint8_t handleWeeklySchedule(TSD_ChannelConfig *result, bool altSchedule=false, bool local=false) override
Handles "set channel config" with "weekly schedule" type request from server.
Definition element_with_channel_actions.cpp:569
Base class for all elements of SuplaDevice.
Definition element.h:30
virtual bool iterateConnected()
Method called on each SuplaDevice iteration when device is connected and registered to Supla server o...
Definition element.cpp:143
Definition local_action.h:87
Definition supla_srpc.h:47
Definition element_with_channel_actions.h:30
Definition proto.h:3222
Definition proto.h:3206