supla-device
Loading...
Searching...
No Matches
local_action.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_LOCAL_ACTION_H_
5#define SRC_SUPLA_LOCAL_ACTION_H_
6
7#include <stdint.h>
8
9namespace Supla {
10
11// Fixed-size action list used by the local-action filter. This deliberately
12// avoids std::initializer_list because the AVR Arduino toolchain does not
13// provide that header. The current firmware uses at most three entries for a
14// single event (the selected lock action and the two config-mode actions).
16 public:
17 ActionAllowList() : count(0) {
18 actions[0] = 0;
19 actions[1] = 0;
20 actions[2] = 0;
21 }
22
23 ActionAllowList(uint16_t action) : count(1) { // NOLINT(runtime/explicit)
24 actions[0] = action;
25 actions[1] = 0;
26 actions[2] = 0;
27 }
28
29 ActionAllowList(uint16_t action1, uint16_t action2) : count(2) {
30 actions[0] = action1;
31 actions[1] = action2;
32 actions[2] = 0;
33 }
34
35 ActionAllowList(uint16_t action1, uint16_t action2, uint16_t action3)
36 : count(3) {
37 actions[0] = action1;
38 actions[1] = action2;
39 actions[2] = action3;
40 }
41
42 bool contains(uint16_t action) const {
43 for (uint8_t i = 0; i < count; i++) {
44 if (actions[i] == action) {
45 return true;
46 }
47 }
48 return false;
49 }
50
51 private:
52 uint16_t actions[3];
53 uint8_t count;
54};
55
56class ActionHandler;
57class LocalAction;
58
60 public:
62
63 virtual ~ActionHandlerClient();
64
65 LocalAction *trigger = nullptr;
66 ActionHandler *client = nullptr;
67 ActionHandlerClient *next = nullptr;
68 uint16_t onEvent = 0;
69 uint16_t action = 0;
70 static ActionHandlerClient *begin;
71
72 bool isEnabled();
73
74 virtual void setAlwaysEnabled();
75 virtual void enable();
76 virtual void disable();
77 void disableForConfigMode();
78 void restoreAfterConfigMode();
79 virtual bool isAlwaysEnabled();
80
81 protected:
82 bool enabled = true;
83 bool alwaysEnabled = false;
84 bool disabledForConfigMode = false;
85};
86
88 public:
89 virtual ~LocalAction();
90 virtual void addAction(uint16_t action,
91 ActionHandler &client, // NOLINT(runtime/references)
92 uint16_t event,
93 bool alwaysEnabled = false);
94 virtual void addAction(uint16_t action, ActionHandler *client, uint16_t event,
95 bool alwaysEnabled = false);
96
97 virtual void runAction(uint16_t event) const;
98 // The allow-list is an additional restriction. An empty list deliberately
99 // blocks every action, while the one-argument overload keeps the historic,
100 // unrestricted behavior.
101 virtual void runAction(
102 uint16_t event,
103 const ActionAllowList &allowOnlyActions) const;
104
105 virtual bool isEventAlreadyUsed(uint16_t event, bool ignoreAlwaysEnabled);
106 virtual bool hasEnabledAction(uint16_t event, uint16_t action) const;
107 virtual ActionHandlerClient *getHandlerForFirstClient(uint16_t event);
108 virtual ActionHandlerClient *getHandlerForClient(ActionHandler *client,
109 uint16_t event);
110
111 virtual void disableOtherClients(const ActionHandler &client, uint16_t event);
112 virtual void enableOtherClients(const ActionHandler &client, uint16_t event);
113 virtual void disableOtherClients(const ActionHandler *client, uint16_t event);
114 virtual void enableOtherClients(const ActionHandler *client, uint16_t event);
115
116 static void DeleteActionsHandledBy(const ActionHandler *client);
117 static void DeleteActionsHandledByExcept(
118 const ActionHandler *client,
119 const ActionAllowList &preservedActions);
120 static void DeleteActionsTriggeredBy(const LocalAction *action);
121 static void DeleteAction(const LocalAction *trigger,
122 const ActionHandler *client,
123 uint16_t event,
124 uint16_t action);
125 static void NullifyActionsHandledBy(const ActionHandler *client);
126
127 // action and event are internally uint16_t type, however -1 is used
128 // as "all events/actions", so here we pass int32_t
129 virtual void disableAction(int32_t action,
130 ActionHandler *client,
131 int32_t event);
132 virtual void enableAction(int32_t action,
133 ActionHandler *client,
134 int32_t event);
135
136 virtual bool disableActionsInConfigMode();
137
138 static ActionHandlerClient *getClientListPtr();
139
140 private:
141 void runActionInternal(uint16_t event,
142 const ActionAllowList *allowOnlyActions) const;
143};
144
145}; // namespace Supla
146
147#endif // SRC_SUPLA_LOCAL_ACTION_H_
Definition local_action.h:15
Definition local_action.h:59
Definition action_handler.h:8
Definition local_action.h:87