forked from nrfconnect/sdk-nrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_task.cpp
131 lines (102 loc) · 3.77 KB
/
app_task.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include "app_task.h"
#include "app/matter_init.h"
#include "app/task_executor.h"
#include "board/board.h"
#include "lib/core/CHIPError.h"
#include "lib/support/CodeUtils.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app/EventLogging.h>
#include <app/util/attribute-storage.h>
#include <setup_payload/OnboardingCodesUtil.h>
#include <dk_buttons_and_leds.h>
#include <zephyr/logging/log.h>
#include <zephyr/random/random.h>
LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);
using namespace ::chip;
using namespace ::chip::app;
using namespace ::chip::DeviceLayer;
namespace
{
#define BUTTON2_MASK DK_BTN2_MSK
constexpr EndpointId kBasicInformationEndpointId = 0;
constexpr EndpointId kNordicDevKitEndpointId = 1;
} /* namespace */
static void ButtonEventHandler(Nrf::ButtonState /* unused */, Nrf::ButtonMask has_changed)
{
/* Handle button press */
if (ConnectivityMgrImpl().IsIPv6NetworkProvisioned() && ConnectivityMgrImpl().IsIPv6NetworkEnabled() &&
BUTTON2_MASK & has_changed) {
AppTask::Instance().UpdateNordicDevkitClusterState();
}
}
void AppTask::UpdateNordicDevkitClusterState()
{
SystemLayer().ScheduleLambda([] {
Protocols::InteractionModel::Status status;
Nrf::ButtonState button_state;
dk_read_buttons(&button_state, nullptr);
status = Clusters::NordicDevKit::Attributes::UserLED::Set(
kNordicDevKitEndpointId, Nrf::GetBoard().GetLED(Nrf::DeviceLeds::LED2).GetState());
if (status != Protocols::InteractionModel::Status::Success) {
LOG_ERR("Updating NordicDevkit cluster failed: %x", to_underlying(status));
}
status = Clusters::NordicDevKit::Attributes::UserButton::Set(kNordicDevKitEndpointId,
BUTTON2_MASK & button_state);
if (status != Protocols::InteractionModel::Status::Success) {
LOG_ERR("Updating NordicDevkit cluster failed: %x", to_underlying(status));
}
for (auto endpoint : EnabledEndpointsWithServerCluster(Clusters::NordicDevKit::Id)) {
/* If NordicDevKit cluster is implemented on this endpoint */
Clusters::NordicDevKit::Events::UserButtonChanged::Type event;
EventNumber eventNumber;
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) {
ChipLogError(Zcl, "Failed to emit UserButtonChanged event");
}
}
});
}
void AppTask::UpdateBasicInformationClusterState()
{
SystemLayer().ScheduleLambda([] {
Protocols::InteractionModel::Status status;
status = Clusters::BasicInformation::Attributes::RandomNumber::Set(kBasicInformationEndpointId,
sys_rand16_get());
if (status != Protocols::InteractionModel::Status::Success) {
LOG_ERR("Updating Basic information cluster failed: %x", to_underlying(status));
}
for (auto endpoint : EnabledEndpointsWithServerCluster(Clusters::BasicInformation::Id)) {
/* If Basic cluster is implemented on this endpoint */
Clusters::BasicInformation::Events::RandomNumberChanged::Type event;
EventNumber eventNumber;
if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber)) {
ChipLogError(Zcl, "Failed to emit RandomNumberChanged event");
}
}
});
}
CHIP_ERROR AppTask::Init()
{
/* Initialize Matter stack */
ReturnErrorOnFailure(Nrf::Matter::PrepareServer());
if (!Nrf::GetBoard().Init(ButtonEventHandler)) {
LOG_ERR("User interface initialization failed.");
return CHIP_ERROR_INCORRECT_STATE;
}
/* Register Matter event handler that controls the connectivity status LED based on the captured Matter network
* state. */
ReturnErrorOnFailure(Nrf::Matter::RegisterEventHandler(Nrf::Board::DefaultMatterEventHandler, 0));
return Nrf::Matter::StartServer();
}
CHIP_ERROR AppTask::StartApp()
{
ReturnErrorOnFailure(Init());
while (true) {
Nrf::DispatchNextTask();
}
return CHIP_NO_ERROR;
}