forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppTask.cpp
157 lines (134 loc) · 4.33 KB
/
AppTask.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "AppTask.h"
#include "AppConfig.h"
#include "AppEvent.h"
#include "LEDWidget.h"
#include <app/clusters/smoke-co-alarm-server/smoke-co-alarm-server.h>
#include <app/server/Server.h>
#include <app/util/attribute-storage.h>
#include <assert.h>
#include <lib/support/CodeUtils.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/silabs/platformAbstraction/SilabsPlatform.h>
#include <setup_payload/OnboardingCodesUtil.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
#if (defined(SL_CATALOG_SIMPLE_LED_LED1_PRESENT))
#define LIGHT_LED 1
#else
#define LIGHT_LED 0
#endif
#define APP_FUNCTION_BUTTON 0
#define APP_SELFTESTREQUEST_BUTTON 1
using namespace chip;
using namespace chip::app;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Silabs;
namespace {
LEDWidget sAlarmLED;
}
using namespace chip::TLV;
using namespace ::chip::DeviceLayer;
AppTask AppTask::sAppTask;
CHIP_ERROR AppTask::AppInit()
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::DeviceLayer::Silabs::GetPlatform().SetButtonsCb(AppTask::ButtonEventHandler);
err = AlarmMgr().Init();
if (err != CHIP_NO_ERROR)
{
SILABS_LOG("AlarmMgr::Init() failed");
appError(err);
}
// Register Smoke & Co Test Event Trigger
if (Server::GetInstance().GetTestEventTriggerDelegate() != nullptr)
{
Server::GetInstance().GetTestEventTriggerDelegate()->AddHandler(&AlarmMgr());
}
sAlarmLED.Init(LIGHT_LED);
sAlarmLED.Set(false);
// Update the LCD with the Stored value. Show QR Code if not provisioned
#ifdef DISPLAY_ENABLED
GetLCD().WriteDemoUI(false);
#ifdef QR_CODE_ENABLED
#ifdef SL_WIFI
if (!ConnectivityMgr().IsWiFiStationProvisioned())
#else
if (!ConnectivityMgr().IsThreadProvisioned())
#endif /* !SL_WIFI */
{
GetLCD().ShowQRCode(true);
}
#endif // QR_CODE_ENABLED
#endif
return err;
}
CHIP_ERROR AppTask::StartAppTask()
{
return BaseApplication::StartAppTask(AppTaskMain);
}
void AppTask::AppTaskMain(void * pvParameter)
{
AppEvent event;
osMessageQueueId_t sAppEventQueue = *(static_cast<osMessageQueueId_t *>(pvParameter));
CHIP_ERROR err = sAppTask.Init();
if (err != CHIP_NO_ERROR)
{
SILABS_LOG("AppTask.Init() failed");
appError(err);
}
#if !(defined(CHIP_CONFIG_ENABLE_ICD_SERVER) && CHIP_CONFIG_ENABLE_ICD_SERVER)
sAppTask.StartStatusLEDTimer();
#endif
SILABS_LOG("App Task started");
while (true)
{
osStatus_t eventReceived = osMessageQueueGet(sAppEventQueue, &event, NULL, osWaitForever);
while (eventReceived == osOK)
{
sAppTask.DispatchEvent(&event);
eventReceived = osMessageQueueGet(sAppEventQueue, &event, NULL, 0);
}
}
}
void AppTask::ButtonActionEventHandler(AppEvent * aEvent)
{
chip::DeviceLayer::PlatformMgr().LockChipStack();
bool success = SmokeCoAlarmServer::Instance().RequestSelfTest(1);
chip::DeviceLayer::PlatformMgr().UnlockChipStack();
if (!success)
{
SILABS_LOG("Manual self-test failed");
}
}
void AppTask::ButtonEventHandler(uint8_t button, uint8_t btnAction)
{
AppEvent button_event = {};
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.Action = btnAction;
if (button == APP_SELFTESTREQUEST_BUTTON && btnAction == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
button_event.Handler = ButtonActionEventHandler;
AppTask::GetAppTask().PostEvent(&button_event);
}
else if (button == APP_FUNCTION_BUTTON)
{
button_event.Handler = BaseApplication::ButtonHandler;
AppTask::GetAppTask().PostEvent(&button_event);
}
}