-
Notifications
You must be signed in to change notification settings - Fork 2k
/
AppTask.cpp
427 lines (369 loc) · 14.9 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* 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 "LEDWidget.h"
#include "LightingManager.h"
#include <app/server/OnboardingCodesUtil.h>
// FIXME: Undefine the `sleep()` function included by the CHIPDeviceLayer.h
// from unistd.h to avoid a conflicting declaration with the `sleep()` provided
// by Mbed-OS in mbed_power_mgmt.h.
#define sleep unistd_sleep
#include <app/server/Mdns.h>
#include <app/server/Server.h>
#include <platform/CHIPDeviceLayer.h>
#undef sleep
#include <support/logging/CHIPLogging.h>
// ZAP -- ZCL Advanced Platform
#include <app/common/gen/attribute-id.h>
#include <app/common/gen/attribute-type.h>
#include <app/common/gen/cluster-id.h>
#include <app/util/attribute-storage.h>
// mbed-os headers
#include "drivers/InterruptIn.h"
#include "drivers/Timeout.h"
#include "events/EventQueue.h"
#include "platform/Callback.h"
#define FACTORY_RESET_TRIGGER_TIMEOUT (MBED_CONF_APP_FACTORY_RESET_TRIGGER_TIMEOUT)
#define FACTORY_RESET_CANCEL_WINDOW_TIMEOUT (MBED_CONF_APP_FACTORY_RESET_CANCEL_WINDOW_TIMEOUT)
#define LIGHTING_BUTTON (MBED_CONF_APP_LIGHTING_BUTTON)
#define FUNCTION_BUTTON (MBED_CONF_APP_FUNCTION_BUTTON)
#define BUTTON_PUSH_EVENT 1
#define BUTTON_RELEASE_EVENT 0
constexpr uint32_t kPublishServicePeriodUs = 5000000;
static LEDWidget sStatusLED(MBED_CONF_APP_SYSTEM_STATE_LED);
static mbed::InterruptIn sLightingButton(LIGHTING_BUTTON);
static mbed::InterruptIn sFunctionButton(FUNCTION_BUTTON);
static bool sIsWiFiStationProvisioned = false;
static bool sIsWiFiStationEnabled = false;
static bool sIsWiFiStationConnected = false;
static bool sIsPairedToAccount = false;
static bool sHaveBLEConnections = false;
static bool sHaveServiceConnectivity = false;
static mbed::Timeout sFunctionTimer;
// TODO: change EventQueue default event size
static events::EventQueue sAppEventQueue;
using namespace ::chip::DeviceLayer;
AppTask AppTask::sAppTask;
int AppTask::Init()
{
// Register the callback to init the MDNS server when connectivity is available
PlatformMgr().AddEventHandler(
[](const ChipDeviceEvent * event, intptr_t arg) {
// Restart the server whenever an ip address is renewed
if (event->Type == DeviceEventType::kInternetConnectivityChange)
{
if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established ||
event->InternetConnectivityChange.IPv6 == kConnectivity_Established)
{
chip::app::Mdns::StartServer();
}
}
},
0);
//-------------
// Initialize button
sLightingButton.fall(mbed::callback(this, &AppTask::LightingButtonPressEventHandler));
sFunctionButton.fall(mbed::callback(this, &AppTask::FunctionButtonPressEventHandler));
sFunctionButton.rise(mbed::callback(this, &AppTask::FunctionButtonReleaseEventHandler));
//----------------
// Initialize lighting manager
LightingMgr().Init(MBED_CONF_APP_LIGHTING_STATE_LED);
LightingMgr().SetCallbacks(ActionInitiated, ActionCompleted);
// Start BLE advertising if needed
if (!CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART)
{
ChipLogProgress(NotSpecified, "Enabling BLE advertising.");
ConnectivityMgr().SetBLEAdvertisingEnabled(true);
}
#ifdef MBED_CONF_APP_DEVICE_NAME
ConnectivityMgr().SetBLEDeviceName(MBED_CONF_APP_DEVICE_NAME);
#endif
chip::DeviceLayer::ConnectivityMgrImpl().StartWiFiManagement();
// Init ZCL Data Model and start server
InitServer();
ConfigurationMgr().LogDeviceConfig();
// QR code will be used with CHIP Tool
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
return 0;
}
int AppTask::StartApp()
{
int ret = Init();
uint64_t mLastPublishServiceTimeUS = 0;
if (ret)
{
ChipLogError(NotSpecified, "AppTask.Init() failed");
return ret;
}
while (true)
{
sAppEventQueue.dispatch(100);
// Collect connectivity and configuration state from the CHIP stack. Because the
// CHIP event loop is being run in a separate task, the stack must be locked
// while these values are queried. However we use a non-blocking lock request
// (TryLockChipStack()) to avoid blocking other UI activities when the CHIP
// task is busy (e.g. with a long crypto operation).
if (PlatformMgr().TryLockChipStack())
{
sIsWiFiStationProvisioned = ConnectivityMgr().IsWiFiStationProvisioned();
sIsWiFiStationEnabled = ConnectivityMgr().IsWiFiStationEnabled();
sIsWiFiStationConnected = ConnectivityMgr().IsWiFiStationConnected();
sHaveBLEConnections = (ConnectivityMgr().NumBLEConnections() != 0);
sHaveServiceConnectivity = ConnectivityMgr().HaveServiceConnectivity();
PlatformMgr().UnlockChipStack();
}
// Consider the system to be "fully connected" if it has service
// connectivity and it is able to interact with the service on a regular basis.
bool isFullyConnected = sHaveServiceConnectivity;
// Update the status LED if factory reset has not been initiated.
//
// If system has "full connectivity", keep the LED On constantly.
//
// If thread and service provisioned, but not attached to the thread network yet OR no
// connectivity to the service OR subscriptions are not fully established
// THEN blink the LED Off for a short period of time.
//
// If the system has ble connection(s) uptill the stage above, THEN blink the LEDs at an even
// rate of 100ms.
//
// Otherwise, blink the LED ON for a very short time.
if (sAppTask.mFunction != kFunction_FactoryReset)
{
if (isFullyConnected)
{
sStatusLED.Set(true);
}
else if (sIsWiFiStationProvisioned && sIsWiFiStationEnabled && sIsPairedToAccount &&
(!sIsWiFiStationConnected || !isFullyConnected))
{
sStatusLED.Blink(950, 50);
}
else if (sHaveBLEConnections)
{
sStatusLED.Blink(100, 100);
}
else
{
sStatusLED.Blink(50, 950);
}
}
sStatusLED.Animate();
uint64_t nowUS = chip::System::Platform::Layer::GetClock_Monotonic();
uint64_t nextChangeTimeUS = mLastPublishServiceTimeUS + kPublishServicePeriodUs;
if (nowUS > nextChangeTimeUS)
{
// TODO:
// PublishService();
mLastPublishServiceTimeUS = nowUS;
}
}
}
void AppTask::LightingActionEventHandler(AppEvent * aEvent)
{
LightingManager::Action_t action = LightingManager::INVALID_ACTION;
int32_t actor = 0;
if (aEvent->Type == AppEvent::kEventType_Lighting)
{
action = static_cast<LightingManager::Action_t>(aEvent->LightingEvent.Action);
actor = aEvent->LightingEvent.Actor;
}
else if (aEvent->Type == AppEvent::kEventType_Button)
{
action = LightingMgr().IsTurnedOn() ? LightingManager::OFF_ACTION : LightingManager::ON_ACTION;
actor = AppEvent::kEventType_Button;
}
if (action != LightingManager::INVALID_ACTION && !LightingMgr().InitiateAction(action, actor, 0, NULL))
ChipLogProgress(NotSpecified, "Action is already in progress or active.");
}
void AppTask::LightingButtonPressEventHandler()
{
AppEvent button_event;
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.Pin = LIGHTING_BUTTON;
button_event.ButtonEvent.Action = BUTTON_PUSH_EVENT;
button_event.Handler = LightingActionEventHandler;
sAppTask.PostEvent(&button_event);
}
void AppTask::FunctionButtonPressEventHandler()
{
AppEvent button_event;
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.Pin = FUNCTION_BUTTON;
button_event.ButtonEvent.Action = BUTTON_PUSH_EVENT;
button_event.Handler = FunctionHandler;
sAppTask.PostEvent(&button_event);
}
void AppTask::FunctionButtonReleaseEventHandler()
{
AppEvent button_event;
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.Pin = FUNCTION_BUTTON;
button_event.ButtonEvent.Action = BUTTON_RELEASE_EVENT;
button_event.Handler = FunctionHandler;
sAppTask.PostEvent(&button_event);
}
void AppTask::ActionInitiated(LightingManager::Action_t aAction, int32_t aActor)
{
if (aAction == LightingManager::ON_ACTION)
{
ChipLogProgress(NotSpecified, "Turn On Action has been initiated");
}
else if (aAction == LightingManager::OFF_ACTION)
{
ChipLogProgress(NotSpecified, "Turn Off Action has been initiated");
}
else if (aAction == LightingManager::LEVEL_ACTION)
{
ChipLogProgress(NotSpecified, "Level Action has been initiated");
}
}
void AppTask::ActionCompleted(LightingManager::Action_t aAction, int32_t aActor)
{
if (aAction == LightingManager::ON_ACTION)
{
ChipLogProgress(NotSpecified, "Turn On Action has been completed");
}
else if (aAction == LightingManager::OFF_ACTION)
{
ChipLogProgress(NotSpecified, "Turn Off Action has been completed");
}
else if (aAction == LightingManager::LEVEL_ACTION)
{
ChipLogProgress(NotSpecified, "Level Action has been completed");
}
if (aActor == AppEvent::kEventType_Button)
{
sAppTask.UpdateClusterState();
}
}
void AppTask::CancelTimer()
{
sFunctionTimer.detach();
mFunctionTimerActive = false;
}
void AppTask::StartTimer(uint32_t aTimeoutInMs)
{
auto chronoTimeoutMs = std::chrono::duration<uint32_t, std::milli>(aTimeoutInMs);
sFunctionTimer.attach(mbed::callback(this, &AppTask::TimerEventHandler), chronoTimeoutMs);
mFunctionTimerActive = true;
}
void AppTask::PostEvent(AppEvent * aEvent)
{
auto handle = sAppEventQueue.call([event = *aEvent, this] { DispatchEvent(&event); });
if (!handle)
{
ChipLogError(NotSpecified, "Failed to post event to app task event queue: Not enough memory");
}
}
void AppTask::DispatchEvent(const AppEvent * aEvent)
{
if (aEvent->Handler)
{
aEvent->Handler(const_cast<AppEvent *>(aEvent));
}
else
{
ChipLogError(NotSpecified, "Event received with no handler. Dropping event.");
}
}
void AppTask::TimerEventHandler()
{
AppEvent event;
event.Type = AppEvent::kEventType_Timer;
// event.TimerEvent.Context = nullptr;
event.Handler = FunctionTimerEventHandler;
sAppTask.PostEvent(&event);
}
// static
void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
if (aEvent->Type != AppEvent::kEventType_Timer)
return;
// If we reached here, the button was held past FACTORY_RESET_TRIGGER_TIMEOUT, initiate factory reset
if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_SoftwareUpdate)
{
ChipLogProgress(NotSpecified, "Factory Reset Triggered. Release button within %ums to cancel.",
FACTORY_RESET_CANCEL_WINDOW_TIMEOUT);
// Start timer for FACTORY_RESET_CANCEL_WINDOW_TIMEOUT to allow user to
// cancel, if required.
sAppTask.StartTimer(FACTORY_RESET_CANCEL_WINDOW_TIMEOUT);
sAppTask.mFunction = kFunction_FactoryReset;
sStatusLED.Set(false);
sStatusLED.Blink(500);
}
else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_FactoryReset)
{
// Actually trigger Factory Reset
ChipLogProgress(NotSpecified, "Factory Reset initiated");
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();
}
}
void AppTask::FunctionHandler(AppEvent * aEvent)
{
if (aEvent->ButtonEvent.Pin != FUNCTION_BUTTON)
return;
// To trigger software update: press the FUNCTION_BUTTON button briefly (< FACTORY_RESET_TRIGGER_TIMEOUT)
// To initiate factory reset: press the FUNCTION_BUTTON for FACTORY_RESET_TRIGGER_TIMEOUT + FACTORY_RESET_CANCEL_WINDOW_TIMEOUT
// All LEDs start blinking after FACTORY_RESET_TRIGGER_TIMEOUT to signal factory reset has been initiated.
// To cancel factory reset: release the FUNCTION_BUTTON once all LEDs start blinking within the
// FACTORY_RESET_CANCEL_WINDOW_TIMEOUT
if (aEvent->ButtonEvent.Action == BUTTON_PUSH_EVENT)
{
if (!sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_NoneSelected)
{
sAppTask.StartTimer(FACTORY_RESET_TRIGGER_TIMEOUT);
sAppTask.mFunction = kFunction_SoftwareUpdate;
}
}
else
{
// If the button was released before factory reset got initiated, trigger a software update.
if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_SoftwareUpdate)
{
sAppTask.CancelTimer();
sAppTask.mFunction = kFunction_NoneSelected;
ChipLogError(NotSpecified, "Software Update not supported.");
}
else if (sAppTask.mFunctionTimerActive && sAppTask.mFunction == kFunction_FactoryReset)
{
sAppTask.CancelTimer();
// Change the function to none selected since factory reset has been canceled.
sAppTask.mFunction = kFunction_NoneSelected;
ChipLogProgress(NotSpecified, "Factory Reset has been Canceled");
}
}
}
void AppTask::UpdateClusterState()
{
uint8_t onoff = LightingMgr().IsTurnedOn();
// write the new on/off value
EmberAfStatus status = emberAfWriteAttribute(1, ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, CLUSTER_MASK_SERVER, &onoff,
ZCL_BOOLEAN_ATTRIBUTE_TYPE);
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(NotSpecified, "Updating on/off cluster failed: %x", status);
}
uint8_t level = LightingMgr().GetLevel();
status = emberAfWriteAttribute(1, ZCL_LEVEL_CONTROL_CLUSTER_ID, ZCL_CURRENT_LEVEL_ATTRIBUTE_ID, CLUSTER_MASK_SERVER, &level,
ZCL_INT8U_ATTRIBUTE_TYPE);
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(NotSpecified, "Updating level cluster failed: %x", status);
}
}