Skip to content

Commit

Permalink
Make sure to queue m5stack actions to the Matter thread as needed.
Browse files Browse the repository at this point in the history
Fixes project-chip#15065

(cherry picked from commit 379d144)

Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
2 people authored and step0035 committed Feb 17, 2022
1 parent abeb98b commit 317a870
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
7 changes: 7 additions & 0 deletions examples/all-clusters-app/esp32/main/CHIPDeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 33 additions & 4 deletions examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueuedAction>(this, i);
chip::DeviceLayer::PlatformMgr().ScheduleWork(QueuedActionHandler, reinterpret_cast<intptr_t>(action));
}

static void QueuedActionHandler(intptr_t closure)
{
auto * queuedAction = reinterpret_cast<QueuedAction *>(closure);
queuedAction->self->DoAction(queuedAction->i);
chip::Platform::Delete(queuedAction);
}

virtual void DoAction(int i) = 0;
};

class EditAttributeListModel : public TouchesMatterStackModel
{
int deviceIndex;
int endpointIndex;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -394,7 +423,7 @@ class MdnsDebugListModel : public ActionListModel
}
};

class SetupListModel : public ListScreen::Model
class SetupListModel : public TouchesMatterStackModel
{
public:
SetupListModel()
Expand All @@ -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)
Expand Down

0 comments on commit 317a870

Please sign in to comment.