Skip to content

Commit

Permalink
Some changes after the first Kamil's review
Browse files Browse the repository at this point in the history
  • Loading branch information
ArekBalysNordic committed Dec 4, 2023
1 parent 6840e62 commit 97b5f8c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
17 changes: 9 additions & 8 deletions samples/matter/common/src/board_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void BoardInterface::FunctionTimerEventHandler(const void *context)

/* If we reached here, the button was held past kFactoryResetTriggerTimeout, initiate factory reset */
if (sInstance.mFunction == SystemEventType::SoftwareUpdate) {
LOG_INF("Factory Reset Triggered. Release button within %ums to cancel.",
LOG_INF("Factory reset has been triggered. Release button within %ums to cancel.",
FactoryResetConsts::kFactoryResetTriggerTimeout);

/* Start timer for kFactoryResetCancelWindowTimeout to allow user to cancel, if required. */
Expand Down Expand Up @@ -220,7 +220,7 @@ void BoardInterface::FunctionTimerEventHandler(const void *context)
void BoardInterface::ButtonEventHandler(uint32_t buttonState, uint32_t hasChanged)
{
SystemEvent buttonEvent;
bool iSAppButtonEvent = false;
bool isAppButtonEvent = false;
ButtonActions action;
DeviceButtons source;

Expand Down Expand Up @@ -249,24 +249,24 @@ void BoardInterface::ButtonEventHandler(uint32_t buttonState, uint32_t hasChange
source = DeviceButtons::kAppButton;
action = (APPLICATION_BUTTON_MASK & buttonState) ? ButtonActions::kButtonPressed :
ButtonActions::kButtonReleased;
iSAppButtonEvent = true;
isAppButtonEvent = true;
}

if (USER_BUTTON_1_MASK & hasChanged) {
source = DeviceButtons::kUserButton1;
action = (USER_BUTTON_1_MASK & buttonState) ? ButtonActions::kButtonPressed :
ButtonActions::kButtonReleased;
iSAppButtonEvent = true;
isAppButtonEvent = true;
}
if (USER_BUTTON_2_MASK & hasChanged) {
source = DeviceButtons::kUserButton2;
action = (USER_BUTTON_2_MASK & buttonState) ? ButtonActions::kButtonPressed :
ButtonActions::kButtonReleased;
iSAppButtonEvent = true;
isAppButtonEvent = true;
}
#endif

if (iSAppButtonEvent && sInstance.mButtonCallback) {
if (isAppButtonEvent && sInstance.mButtonCallback) {
sInstance.mButtonCallback(source, action);
}
}
Expand All @@ -279,8 +279,9 @@ void BoardInterface::FunctionHandler(const void *context)

SystemEvent event(context);

if (event.ButtonEvent.PinNo != FUNCTION_BUTTON)
if (event.ButtonEvent.PinNo != FUNCTION_BUTTON) {
return;
}

if (event.ButtonEvent.Action == static_cast<uint8_t>(SystemEventType::ButtonPushed)) {
if (!sInstance.mFunctionTimerActive && sInstance.mFunction == SystemEventType::None) {
Expand All @@ -303,7 +304,7 @@ void BoardInterface::FunctionHandler(const void *context)
sInstance.CancelTimer();
sInstance.UpdateStatusLED();
sInstance.mFunction = SystemEventType::None;
LOG_INF("Factory Reset has been Canceled");
LOG_INF("Factory reset has been canceled");
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions samples/matter/common/src/event_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ constexpr size_t kEventQueueSize = 10;

K_MSGQ_DEFINE(sEventQueue, sizeof(Event), kEventQueueSize, alignof(Event));

void EventManager::PostEvent(Event &event)
CHIP_ERROR EventManager::PostEvent(Event &event)
{
/* Allocate context on the Heap */
void* context = chip::DeviceLayer::Malloc::Malloc(event.mContextSize);
if(!context){
LOG_INF("Failed to store context in Heap");
LOG_ERR("Failed to store context in Heap");
return CHIP_ERROR_NO_MEMORY;
}
memcpy(context, event.mContext, event.mContextSize);

/* Replace context with the allocated bytes */
event.mContext = context;

if (k_msgq_put(&sEventQueue, &event, K_NO_WAIT) != 0) {
LOG_INF("Failed to post event to app task event queue");
LOG_ERR("Failed to post event to app task event queue");
return CHIP_ERROR_NO_MEMORY;
}

return CHIP_NO_ERROR;
}

void EventManager::DispatchEvent()
Expand Down
8 changes: 6 additions & 2 deletions samples/matter/common/src/event_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <cstdlib>
#include <cstring>

#include <lib/core/CHIPError.h>

enum class EventSource : uint8_t { Undefined, Application, System };

struct Event {
Expand Down Expand Up @@ -41,7 +43,7 @@ class EventManager {
public:

/**
* @brief Dispatch en event from the event queue and call associated handler.
* @brief Dispatch an event from the event queue and call associated handler.
*
* This method should be run in the while loop within the application thread.
*
Expand All @@ -55,6 +57,8 @@ class EventManager {
* It should be called from the Zephyr's thread.
*
* @param event event to be posted.
* @return CHIP_ERROR_NO_MEMORY if there is no memory to post new Event
* @return CHIP_NO_ERROR if the Event has been posted
*/
static void PostEvent(Event &event);
static CHIP_ERROR PostEvent(Event &event);
};

0 comments on commit 97b5f8c

Please sign in to comment.