From f3f994e35b4823f5c7f796ec174300a1256455f9 Mon Sep 17 00:00:00 2001 From: Michael Spang Date: Tue, 15 Feb 2022 21:11:04 -0500 Subject: [PATCH] Make sure to queue m5stack actions to the Matter thread as needed. (#15066) (#15234) Fixes https://github.com/project-chip/connectedhomeip/issues/15065 (cherry picked from commit 379d144081041ebdffa6cd7376c19ee37cb692e3) Co-authored-by: Boris Zbarsky --- .../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 2738a4b4f9ebfb..70b1e95541cf41 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -181,7 +181,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; @@ -220,7 +248,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); @@ -394,7 +423,7 @@ class MdnsDebugListModel : public ActionListModel } }; -class SetupListModel : public ListScreen::Model +class SetupListModel : public TouchesMatterStackModel { public: SetupListModel() @@ -409,7 +438,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)