From 30342573581021e799ace9ef77d6bb5feddf91ca Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Fri, 11 Feb 2022 10:52:22 -0500 Subject: [PATCH] Make sure to queue m5stack actions to the Matter thread as needed. (#15066) Fixes https://github.com/project-chip/connectedhomeip/issues/15065 --- .../esp32/main/CHIPDeviceManager.cpp | 7 ++++ examples/all-clusters-app/esp32/main/main.cpp | 37 +++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/examples/all-clusters-app/esp32/main/CHIPDeviceManager.cpp b/examples/all-clusters-app/esp32/main/CHIPDeviceManager.cpp index 87dccff0773f23..59f822bb017cd9 100644 --- a/examples/all-clusters-app/esp32/main/CHIPDeviceManager.cpp +++ b/examples/all-clusters-app/esp32/main/CHIPDeviceManager.cpp @@ -99,6 +99,13 @@ CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb) void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & path, uint8_t mask, uint8_t type, uint16_t size, uint8_t * value) { + TaskHandle_t task = xTaskGetCurrentTaskHandle(); + const char * name = pcTaskGetName(task); + if (!strcmp(name, "CHIP")) + { + ESP_LOGE("all-clusters-app", "Attribute changed on non-Matter task '%s'\n", name); + } + chip::DeviceManager::CHIPDeviceManagerCallbacks * cb = chip::DeviceManager::CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks(); if (cb != nullptr) diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 025d208b89fdb1..9e6693bca9c0c8 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -185,7 +185,35 @@ void AddDevice(std::string name) #if CONFIG_DEVICE_TYPE_M5STACK -class EditAttributeListModel : public ListScreen::Model +class TouchesMatterStackModel : public ListScreen::Model +{ + // We could override Action() and then hope focusIndex has not changed by + // the time our queued task runs, but it's cleaner to just capture its value + // now. + struct QueuedAction + { + QueuedAction(TouchesMatterStackModel * selfArg, int iArg) : self(selfArg), i(iArg) {} + TouchesMatterStackModel * self; + int i; + }; + + void ItemAction(int i) final + { + auto * action = chip::Platform::New(this, i); + chip::DeviceLayer::PlatformMgr().ScheduleWork(QueuedActionHandler, reinterpret_cast(action)); + } + + static void QueuedActionHandler(intptr_t closure) + { + auto * queuedAction = reinterpret_cast(closure); + queuedAction->self->DoAction(queuedAction->i); + chip::Platform::Delete(queuedAction); + } + + virtual void DoAction(int i) = 0; +}; + +class EditAttributeListModel : public TouchesMatterStackModel { int deviceIndex; int endpointIndex; @@ -224,7 +252,8 @@ class EditAttributeListModel : public ListScreen::Model } return i == 0 ? "+" : "-"; } - virtual void ItemAction(int i) + + void DoAction(int i) override { auto & attribute = this->attribute(); auto & value = std::get<1>(attribute); @@ -398,7 +427,7 @@ class MdnsDebugListModel : public ActionListModel } }; -class SetupListModel : public ListScreen::Model +class SetupListModel : public TouchesMatterStackModel { public: SetupListModel() @@ -413,7 +442,7 @@ class SetupListModel : public ListScreen::Model virtual std::string GetTitle() { return "Setup"; } virtual int GetItemCount() { return options.size(); } virtual std::string GetItemText(int i) { return options.at(i); } - virtual void ItemAction(int i) + void DoAction(int i) override { ESP_LOGI(TAG, "Opening options %d: %s", i, GetItemText(i).c_str()); if (i == 0)