|
28 | 28 | #include <app/util/attribute-storage.h>
|
29 | 29 | #include <platform/PlatformManager.h>
|
30 | 30 |
|
| 31 | +#include "ButtonEventsSimulator.h" |
31 | 32 | #include <air-quality-instance.h>
|
32 | 33 | #include <dishwasher-mode.h>
|
33 | 34 | #include <laundry-washer-mode.h>
|
|
36 | 37 | #include <oven-operational-state-delegate.h>
|
37 | 38 | #include <rvc-modes.h>
|
38 | 39 |
|
| 40 | +#include <memory> |
39 | 41 | #include <string>
|
| 42 | +#include <utility> |
40 | 43 |
|
41 | 44 | using namespace chip;
|
42 | 45 | using namespace chip::app;
|
43 | 46 | using namespace chip::app::Clusters;
|
44 | 47 | using namespace chip::DeviceLayer;
|
45 | 48 |
|
| 49 | +namespace { |
| 50 | + |
| 51 | +std::unique_ptr<ButtonEventsSimulator> sButtonSimulatorInstance{ nullptr }; |
| 52 | + |
| 53 | +bool HasNumericField(Json::Value & jsonValue, const std::string & field) |
| 54 | +{ |
| 55 | + return jsonValue.isMember(field) && jsonValue[field].isNumeric(); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Named pipe handler for simulated long press on an action switch. |
| 60 | + * |
| 61 | + * Usage example: |
| 62 | + * echo '{"Name": "SimulateActionSwitchLongPress", "EndpointId": 3, "ButtonId": 1, "LongPressDelayMillis": 800, |
| 63 | + * "LongPressDurationMillis": 1000}' > /tmp/chip_all_clusters_fifo_1146610 |
| 64 | + * |
| 65 | + * JSON Arguments: |
| 66 | + * - "Name": Must be "SimulateActionSwitchLongPress" |
| 67 | + * - "EndpointId": number of endpoint having a switch cluster |
| 68 | + * - "ButtonId": switch position in the switch cluster for "down" button (not idle) |
| 69 | + * - "LongPressDelayMillis": Time in milliseconds before the LongPress |
| 70 | + * - "LongPressDurationMillis": Total duration in milliseconds from start of the press to LongRelease |
| 71 | + * |
| 72 | + * @param jsonValue - JSON payload from named pipe |
| 73 | + */ |
| 74 | +void HandleSimulateActionSwitchLongPress(Json::Value & jsonValue) |
| 75 | +{ |
| 76 | + if (sButtonSimulatorInstance != nullptr) |
| 77 | + { |
| 78 | + ChipLogError(NotSpecified, "Button simulation already in progress! Ignoring request."); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + bool hasEndpointId = HasNumericField(jsonValue, "EndpointId"); |
| 83 | + bool hasButtonId = HasNumericField(jsonValue, "ButtonId"); |
| 84 | + bool hasLongPressDelayMillis = HasNumericField(jsonValue, "LongPressDelayMillis"); |
| 85 | + bool hasLongPressDurationMillis = HasNumericField(jsonValue, "LongPressDurationMillis"); |
| 86 | + if (!hasEndpointId || !hasButtonId || !hasLongPressDelayMillis || !hasLongPressDurationMillis) |
| 87 | + { |
| 88 | + std::string inputJson = jsonValue.toStyledString(); |
| 89 | + ChipLogError( |
| 90 | + NotSpecified, |
| 91 | + "Missing or invalid value for one of EndpointId, ButtonId, LongPressDelayMillis or LongPressDurationMillis in %s", |
| 92 | + inputJson.c_str()); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + EndpointId endpointId = static_cast<EndpointId>(jsonValue["EndpointId"].asUInt()); |
| 97 | + uint8_t buttonId = static_cast<uint8_t>(jsonValue["ButtonId"].asUInt()); |
| 98 | + System::Clock::Milliseconds32 longPressDelayMillis{ static_cast<unsigned>(jsonValue["LongPressDelayMillis"].asUInt()) }; |
| 99 | + System::Clock::Milliseconds32 longPressDurationMillis{ static_cast<unsigned>(jsonValue["LongPressDurationMillis"].asUInt()) }; |
| 100 | + auto buttonSimulator = std::make_unique<ButtonEventsSimulator>(); |
| 101 | + |
| 102 | + bool success = buttonSimulator->SetMode(ButtonEventsSimulator::Mode::kModeLongPress) |
| 103 | + .SetLongPressDelayMillis(longPressDelayMillis) |
| 104 | + .SetLongPressDurationMillis(longPressDurationMillis) |
| 105 | + .SetIdleButtonId(0) |
| 106 | + .SetPressedButtonId(buttonId) |
| 107 | + .SetEndpointId(endpointId) |
| 108 | + .Execute([]() { sButtonSimulatorInstance.reset(); }); |
| 109 | + |
| 110 | + if (!success) |
| 111 | + { |
| 112 | + ChipLogError(NotSpecified, "Failed to start execution of button simulator!"); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + sButtonSimulatorInstance = std::move(buttonSimulator); |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Named pipe handler for simulated multi-press on an action switch. |
| 121 | + * |
| 122 | + * Usage example: |
| 123 | + * echo '{"Name": "SimulateActionSwitchMultiPress", "EndpointId": 3, "ButtonId": 1, "MultiPressPressedTimeMillis": 100, |
| 124 | + * "MultiPressReleasedTimeMillis": 350, "MultiPressNumPresses": 2}' > /tmp/chip_all_clusters_fifo_1146610 |
| 125 | + * |
| 126 | + * JSON Arguments: |
| 127 | + * - "Name": Must be "SimulateActionSwitchMultiPress" |
| 128 | + * - "EndpointId": number of endpoint having a switch cluster |
| 129 | + * - "ButtonId": switch position in the switch cluster for "down" button (not idle) |
| 130 | + * - "MultiPressPressedTimeMillis": Pressed time in milliseconds for each press |
| 131 | + * - "MultiPressReleasedTimeMillis": Released time in milliseconds after each press |
| 132 | + * - "MultiPressNumPresses": Number of presses to simulate |
| 133 | + * |
| 134 | + * @param jsonValue - JSON payload from named pipe |
| 135 | + */ |
| 136 | +void HandleSimulateActionSwitchMultiPress(Json::Value & jsonValue) |
| 137 | +{ |
| 138 | + if (sButtonSimulatorInstance != nullptr) |
| 139 | + { |
| 140 | + ChipLogError(NotSpecified, "Button simulation already in progress! Ignoring request."); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + bool hasEndpointId = HasNumericField(jsonValue, "EndpointId"); |
| 145 | + bool hasButtonId = HasNumericField(jsonValue, "ButtonId"); |
| 146 | + bool hasMultiPressPressedTimeMillis = HasNumericField(jsonValue, "MultiPressPressedTimeMillis"); |
| 147 | + bool hasMultiPressReleasedTimeMillis = HasNumericField(jsonValue, "MultiPressReleasedTimeMillis"); |
| 148 | + bool hasMultiPressNumPresses = HasNumericField(jsonValue, "MultiPressNumPresses"); |
| 149 | + if (!hasEndpointId || !hasButtonId || !hasMultiPressPressedTimeMillis || !hasMultiPressReleasedTimeMillis || |
| 150 | + !hasMultiPressNumPresses) |
| 151 | + { |
| 152 | + std::string inputJson = jsonValue.toStyledString(); |
| 153 | + ChipLogError(NotSpecified, |
| 154 | + "Missing or invalid value for one of EndpointId, ButtonId, MultiPressPressedTimeMillis, " |
| 155 | + "MultiPressReleasedTimeMillis or MultiPressNumPresses in %s", |
| 156 | + inputJson.c_str()); |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + EndpointId endpointId = static_cast<EndpointId>(jsonValue["EndpointId"].asUInt()); |
| 161 | + uint8_t buttonId = static_cast<uint8_t>(jsonValue["ButtonId"].asUInt()); |
| 162 | + System::Clock::Milliseconds32 multiPressPressedTimeMillis{ static_cast<unsigned>( |
| 163 | + jsonValue["MultiPressPressedTimeMillis"].asUInt()) }; |
| 164 | + System::Clock::Milliseconds32 multiPressReleasedTimeMillis{ static_cast<unsigned>( |
| 165 | + jsonValue["MultiPressReleasedTimeMillis"].asUInt()) }; |
| 166 | + uint8_t multiPressNumPresses = static_cast<uint8_t>(jsonValue["MultiPressNumPresses"].asUInt()); |
| 167 | + auto buttonSimulator = std::make_unique<ButtonEventsSimulator>(); |
| 168 | + |
| 169 | + bool success = buttonSimulator->SetMode(ButtonEventsSimulator::Mode::kModeMultiPress) |
| 170 | + .SetMultiPressPressedTimeMillis(multiPressPressedTimeMillis) |
| 171 | + .SetMultiPressReleasedTimeMillis(multiPressReleasedTimeMillis) |
| 172 | + .SetMultiPressNumPresses(multiPressNumPresses) |
| 173 | + .SetIdleButtonId(0) |
| 174 | + .SetPressedButtonId(buttonId) |
| 175 | + .SetEndpointId(endpointId) |
| 176 | + .Execute([]() { sButtonSimulatorInstance.reset(); }); |
| 177 | + |
| 178 | + if (!success) |
| 179 | + { |
| 180 | + ChipLogError(NotSpecified, "Failed to start execution of button simulator!"); |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + sButtonSimulatorInstance = std::move(buttonSimulator); |
| 185 | +} |
| 186 | + |
| 187 | +} // namespace |
| 188 | + |
46 | 189 | AllClustersAppCommandHandler * AllClustersAppCommandHandler::FromJSON(const char * json)
|
47 | 190 | {
|
48 | 191 | Json::Reader reader;
|
@@ -190,6 +333,14 @@ void AllClustersAppCommandHandler::HandleCommand(intptr_t context)
|
190 | 333 | std::string operation = self->mJsonValue["Operation"].asString();
|
191 | 334 | self->OnOperationalStateChange(device, operation, self->mJsonValue["Param"]);
|
192 | 335 | }
|
| 336 | + else if (name == "SimulateActionSwitchLongPress") |
| 337 | + { |
| 338 | + HandleSimulateActionSwitchLongPress(self->mJsonValue); |
| 339 | + } |
| 340 | + else if (name == "SimulateActionSwitchMultiPress") |
| 341 | + { |
| 342 | + HandleSimulateActionSwitchMultiPress(self->mJsonValue); |
| 343 | + } |
193 | 344 | else
|
194 | 345 | {
|
195 | 346 | ChipLogError(NotSpecified, "Unhandled command: Should never happens");
|
|
0 commit comments