Skip to content

Commit 955e399

Browse files
committed
state handling for dishwasher
avert your eyes, for the hacks are hacky, but we are where we are.
1 parent 56280ce commit 955e399

File tree

4 files changed

+202
-147
lines changed

4 files changed

+202
-147
lines changed

examples/google-multi-device/device-common/include/GoogleMultiDeviceCommon.h

+15-9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#include <stdint.h>
88

9-
#include "GenericSwitchStateMachine.h"
109
#include "DefaultGenericSwitchStateMachineDriver.h"
10+
#include "GenericSwitchStateMachine.h"
1111
#include "GoogleMultiDeviceDishwasherOpstate.h"
1212
#include "app/clusters/occupancy-sensor-server/occupancy-sensor-server.h"
1313
#include "app/clusters/operational-state-server/operational-state-server.h"
@@ -17,11 +17,12 @@ namespace matter {
1717

1818
class GoogleMultiDeviceIntegration
1919
{
20-
public:
21-
enum class ButtonId : uint8_t {
22-
kRed = 0,
20+
public:
21+
enum class ButtonId : uint8_t
22+
{
23+
kRed = 0,
2324
kYellow = 1,
24-
kGreen = 2,
25+
kGreen = 2,
2526
kLatch1 = 3,
2627
kLatch2 = 4,
2728
kLatch3 = 5
@@ -42,7 +43,9 @@ class GoogleMultiDeviceIntegration
4243
void HandleOccupancyUndetected(uint8_t sensorId);
4344

4445
// IMPLEMENT IN THE ACTUAL PRODUCT MODULE
45-
void SetDebugLed(bool enabled);
46+
// Yeah, I know the idx thing is gross, it's 8pm the day before SVE.
47+
// 0 == red, 1 == green , 2 == yellow
48+
void SetDebugLed(bool enabled, int idx = 0);
4649
void EmitDebugCode(uint8_t code);
4750
uint8_t GetEp4LatchInitialPosition();
4851
bool IsAlternativeDiscriminator();
@@ -52,7 +55,8 @@ class GoogleMultiDeviceIntegration
5255
static GoogleMultiDeviceIntegration instance;
5356
return instance;
5457
}
55-
private:
58+
59+
private:
5660
chip::app::DefaultGenericSwitchStateMachineDriver mGenericSwitchDriverEp2;
5761
chip::app::GenericSwitchStateMachine mGenericSwitchStateMachineEp2;
5862

@@ -62,11 +66,13 @@ class GoogleMultiDeviceIntegration
6266
chip::app::DefaultGenericSwitchStateMachineDriver mGenericSwitchDriverEp4;
6367
chip::app::GenericSwitchStateMachine mGenericSwitchStateMachineEp4;
6468

65-
std::unique_ptr<chip::app::Clusters::OccupancySensing::Instance> mOccupancyInstanceEp5 = nullptr;
69+
std::unique_ptr<chip::app::Clusters::OccupancySensing::Instance> mOccupancyInstanceEp5 = nullptr;
6670
std::unique_ptr<chip::app::Clusters::OccupancySensing::Instance::Delegate> mOccupancyDelegateEp5 = nullptr;
6771

68-
std::unique_ptr<GoogleFakeDishwasherInterface> mFakeDishwasherEp6 = nullptr;
72+
std::unique_ptr<GoogleFakeDishwasherInterface> mFakeDishwasherEp6 = nullptr;
6973
std::unique_ptr<chip::app::Clusters::OperationalState::Instance> mOpStateInstanceEp6 = nullptr;
74+
75+
uint8_t mLatchPos = 0;
7076
};
7177

7278
} // namespace matter

examples/google-multi-device/device-common/src/GoogleMultiDeviceCommon.cpp

+79-39
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77

88
#include "GoogleMultiDeviceCommon.h"
99

10-
#include "GoogleMultiDeviceInfoProvider.h"
11-
#include "GoogleMultiDeviceAttestationProvider.h"
12-
#include "GenericSwitchStateMachine.h"
1310
#include "DefaultGenericSwitchStateMachineDriver.h"
11+
#include "GenericSwitchStateMachine.h"
12+
#include "GoogleMultiDeviceAttestationProvider.h"
1413
#include "GoogleMultiDeviceDishwasherOpstate.h"
14+
#include "GoogleMultiDeviceInfoProvider.h"
1515

16-
#include <app/server/Server.h>
1716
#include <app-common/zap-generated/cluster-enums.h>
17+
#include <app/server/Server.h>
1818
#include <include/platform/ConnectivityManager.h>
1919
#include <include/platform/DeviceInstanceInfoProvider.h>
20-
#include <lib/support/CodeUtils.h>
2120
#include <lib/core/CHIPError.h>
2221
#include <lib/core/DataModelTypes.h>
2322
#include <lib/support/BitFlags.h>
23+
#include <lib/support/CodeUtils.h>
2424

2525
#include <system/SystemClock.h>
2626
#include <system/SystemLayer.h>
@@ -43,8 +43,8 @@ GoogleMultiDeviceAttestationProvider sAttestationProvider;
4343

4444
class OccupancyDelegate : public OccupancySensing::Instance::Delegate
4545
{
46-
public:
47-
//OccupancyDelegate() = delete;
46+
public:
47+
// OccupancyDelegate() = delete;
4848
OccupancyDelegate(EndpointId endpointId) : mEndpoint(endpointId) {}
4949

5050
// Not copyable.
@@ -102,9 +102,11 @@ class OccupancyDelegate : public OccupancySensing::Instance::Delegate
102102
bool StartHoldTimer(uint16_t numSeconds, std::function<void()> callback) override
103103
{
104104
mHoldTimerCallback = callback;
105-
return CHIP_NO_ERROR == chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Timeout(numSeconds * 1000u), &HoldTimerHandler, this);
105+
return CHIP_NO_ERROR ==
106+
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Timeout(numSeconds * 1000u), &HoldTimerHandler, this);
106107
}
107-
private:
108+
109+
private:
108110
static void HoldTimerHandler(System::Layer * layer, void * ctx)
109111
{
110112
if (ctx != nullptr)
@@ -149,8 +151,7 @@ void GoogleMultiDeviceIntegration::InitializeProduct()
149151
{
150152
mGenericSwitchDriverEp2.SetEndpointId(2);
151153
BitFlags<Clusters::Switch::Feature> ep2Features;
152-
ep2Features
153-
.Set(Clusters::Switch::Feature::kActionSwitch)
154+
ep2Features.Set(Clusters::Switch::Feature::kActionSwitch)
154155
.Set(Clusters::Switch::Feature::kMomentarySwitch)
155156
.Set(Clusters::Switch::Feature::kMomentarySwitchLongPress)
156157
.Set(Clusters::Switch::Feature::kMomentarySwitchMultiPress);
@@ -166,8 +167,7 @@ void GoogleMultiDeviceIntegration::InitializeProduct()
166167
{
167168
mGenericSwitchDriverEp3.SetEndpointId(3);
168169
BitFlags<Clusters::Switch::Feature> ep3Features;
169-
ep3Features
170-
.Set(Clusters::Switch::Feature::kMomentarySwitch)
170+
ep3Features.Set(Clusters::Switch::Feature::kMomentarySwitch)
171171
.Set(Clusters::Switch::Feature::kMomentarySwitchLongPress)
172172
.Set(Clusters::Switch::Feature::kMomentarySwitchRelease)
173173
.Set(Clusters::Switch::Feature::kMomentarySwitchMultiPress);
@@ -192,64 +192,105 @@ void GoogleMultiDeviceIntegration::InitializeProduct()
192192
mGenericSwitchStateMachineEp4.SetDriver(&mGenericSwitchDriverEp4);
193193

194194
// Initialize initial state.
195-
mGenericSwitchDriverEp4.SetButtonPosition(GetEp4LatchInitialPosition());
195+
mLatchPos = GetEp4LatchInitialPosition();
196+
mGenericSwitchDriverEp4.SetButtonPosition(mLatchPos);
196197
}
197198

198199
// EP5: Occupancy sensor setup
199200
const EndpointId kOccupancyEndpointId = 5;
200-
mOccupancyDelegateEp5 = std::make_unique<OccupancyDelegate>(kOccupancyEndpointId);
201+
mOccupancyDelegateEp5 = std::make_unique<OccupancyDelegate>(kOccupancyEndpointId);
201202
mOccupancyInstanceEp5 = std::make_unique<OccupancySensing::Instance>(mOccupancyDelegateEp5.get(), kOccupancyEndpointId);
202203
VerifyOrDie(mOccupancyInstanceEp5->Init() == CHIP_NO_ERROR);
203204

204205
// EP6: Dishwasher setup
205206
const EndpointId kDishwasherEndpointId = 6;
206-
mFakeDishwasherEp6 = MakeGoogleFakeDishwasher(kDishwasherEndpointId);
207+
mFakeDishwasherEp6 = MakeGoogleFakeDishwasher(kDishwasherEndpointId);
207208
mOpStateInstanceEp6 = std::make_unique<OperationalState::Instance>(mFakeDishwasherEp6->GetDelegate(), kDishwasherEndpointId);
208209

209210
mOpStateInstanceEp6->SetOperationalState(to_underlying(OperationalState::OperationalStateEnum::kStopped));
210211
mOpStateInstanceEp6->Init();
211212

212213
if (chip::Server::GetInstance().GetFabricTable().FabricCount() == 0)
213214
{
214-
chip::DeviceLayer::SystemLayer().ScheduleLambda([](){
215-
chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingMode(chip::DeviceLayer::ConnectivityManager::BLEAdvertisingMode::kFastAdvertising);
215+
chip::DeviceLayer::SystemLayer().ScheduleLambda([]() {
216+
chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingMode(
217+
chip::DeviceLayer::ConnectivityManager::BLEAdvertisingMode::kFastAdvertising);
216218
});
217219
}
218220
}
219221

220222
void GoogleMultiDeviceIntegration::HandleButtonPress(ButtonId buttonId)
221223
{
222-
uint8_t latchPos = 0;
223224
bool isLatch = false;
224225

225226
switch (buttonId)
226227
{
227228
case ButtonId::kRed:
228229
// Position 1 on EP2
229-
chip::DeviceLayer::SystemLayer().ScheduleLambda([this](){
230+
chip::DeviceLayer::SystemLayer().ScheduleLambda([this]() {
230231
this->mGenericSwitchStateMachineEp2.HandleEvent(GenericSwitchStateMachine::Event::MakeButtonPressEvent(1));
231232
});
232233
break;
233234
case ButtonId::kYellow:
234235
// Position 1 on EP3
235-
chip::DeviceLayer::SystemLayer().ScheduleLambda([this](){
236+
chip::DeviceLayer::SystemLayer().ScheduleLambda([this]() {
236237
this->mGenericSwitchStateMachineEp3.HandleEvent(GenericSwitchStateMachine::Event::MakeButtonPressEvent(1));
237238
});
238239
break;
239-
case ButtonId::kGreen:
240-
// TODO: Handle for opstate
241-
break;
240+
case ButtonId::kGreen: {
241+
if (mLatchPos == 1)
242+
{
243+
// Controlling start and stop
244+
if (mOpStateInstanceEp6->GetCurrentOperationalState() ==
245+
to_underlying(OperationalState::OperationalStateEnum::kRunning))
246+
{
247+
chip::DeviceLayer::SystemLayer().ScheduleLambda([this]() {
248+
OperationalState::GenericOperationalError err(to_underlying(OperationalState::ErrorStateEnum::kNoError));
249+
mFakeDishwasherEp6->GetDelegate()->HandleStopStateCallback(err);
250+
});
251+
}
252+
else if (mOpStateInstanceEp6->GetCurrentOperationalState() ==
253+
to_underlying(OperationalState::OperationalStateEnum::kStopped))
254+
{
255+
chip::DeviceLayer::SystemLayer().ScheduleLambda([this]() {
256+
OperationalState::GenericOperationalError err(to_underlying(OperationalState::ErrorStateEnum::kNoError));
257+
mFakeDishwasherEp6->GetDelegate()->HandleStartStateCallback(err);
258+
});
259+
}
260+
}
261+
else if (mLatchPos == 2)
262+
{
263+
// Controlling pause and resume
264+
if (mOpStateInstanceEp6->GetCurrentOperationalState() ==
265+
to_underlying(OperationalState::OperationalStateEnum::kRunning))
266+
{
267+
chip::DeviceLayer::SystemLayer().ScheduleLambda([this]() {
268+
OperationalState::GenericOperationalError err(to_underlying(OperationalState::ErrorStateEnum::kNoError));
269+
mFakeDishwasherEp6->GetDelegate()->HandlePauseStateCallback(err);
270+
});
271+
}
272+
else if (mOpStateInstanceEp6->GetCurrentOperationalState() ==
273+
to_underlying(OperationalState::OperationalStateEnum::kPaused))
274+
{
275+
chip::DeviceLayer::SystemLayer().ScheduleLambda([this]() {
276+
OperationalState::GenericOperationalError err(to_underlying(OperationalState::ErrorStateEnum::kNoError));
277+
mFakeDishwasherEp6->GetDelegate()->HandleResumeStateCallback(err);
278+
});
279+
}
280+
}
281+
}
282+
break;
242283
case ButtonId::kLatch1:
243-
latchPos = 0;
244-
isLatch = true;
284+
mLatchPos = 0;
285+
isLatch = true;
245286
break;
246287
case ButtonId::kLatch2:
247-
latchPos = 1;
248-
isLatch = true;
288+
mLatchPos = 1;
289+
isLatch = true;
249290
break;
250291
case ButtonId::kLatch3:
251-
latchPos = 2;
252-
isLatch = true;
292+
mLatchPos = 2;
293+
isLatch = true;
253294
break;
254295
default:
255296
break;
@@ -258,7 +299,8 @@ void GoogleMultiDeviceIntegration::HandleButtonPress(ButtonId buttonId)
258299
if (isLatch)
259300
{
260301
// Latch positions on EP4
261-
chip::DeviceLayer::SystemLayer().ScheduleLambda([this, latchPos](){
302+
uint8_t latchPos = mLatchPos;
303+
chip::DeviceLayer::SystemLayer().ScheduleLambda([this, latchPos]() {
262304
this->mGenericSwitchStateMachineEp4.HandleEvent(GenericSwitchStateMachine::Event::MakeLatchSwitchChangeEvent(latchPos));
263305
});
264306
}
@@ -270,13 +312,13 @@ void GoogleMultiDeviceIntegration::HandleButtonRelease(ButtonId buttonId)
270312
{
271313
case ButtonId::kRed:
272314
// Position 1 on EP2
273-
chip::DeviceLayer::SystemLayer().ScheduleLambda([this](){
315+
chip::DeviceLayer::SystemLayer().ScheduleLambda([this]() {
274316
this->mGenericSwitchStateMachineEp2.HandleEvent(GenericSwitchStateMachine::Event::MakeButtonReleaseEvent(1));
275317
});
276318
break;
277319
case ButtonId::kYellow:
278320
// Position 1 on EP3
279-
chip::DeviceLayer::SystemLayer().ScheduleLambda([this](){
321+
chip::DeviceLayer::SystemLayer().ScheduleLambda([this]() {
280322
this->mGenericSwitchStateMachineEp3.HandleEvent(GenericSwitchStateMachine::Event::MakeButtonReleaseEvent(1));
281323
});
282324
break;
@@ -292,19 +334,17 @@ void GoogleMultiDeviceIntegration::HandleOccupancyDetected(uint8_t sensorId)
292334
{
293335
if (sensorId == 0)
294336
{
295-
chip::DeviceLayer::SystemLayer().ScheduleLambda([this](){
296-
this->mOccupancyInstanceEp5->SetOccupancyDetectedFromSensor(true);
297-
});
337+
chip::DeviceLayer::SystemLayer().ScheduleLambda(
338+
[this]() { this->mOccupancyInstanceEp5->SetOccupancyDetectedFromSensor(true); });
298339
}
299340
}
300341

301342
void GoogleMultiDeviceIntegration::HandleOccupancyUndetected(uint8_t sensorId)
302343
{
303344
if (sensorId == 0)
304345
{
305-
chip::DeviceLayer::SystemLayer().ScheduleLambda([this](){
306-
this->mOccupancyInstanceEp5->SetOccupancyDetectedFromSensor(false);
307-
});
346+
chip::DeviceLayer::SystemLayer().ScheduleLambda(
347+
[this]() { this->mOccupancyInstanceEp5->SetOccupancyDetectedFromSensor(false); });
308348
}
309349
}
310350

0 commit comments

Comments
 (0)