blob: 0f330e700b95482388a3959d73887e1ec5ae1879 [file] [log] [blame]
/*
*
* Copyright (c) 2023 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.
*/
/**********************************************************
* Includes
*********************************************************/
#include "AppTask.h"
#include "AppConfig.h"
#include "AppEvent.h"
#include "LEDWidget.h"
#ifdef DISPLAY_ENABLED
#include "lcd.h"
#ifdef QR_CODE_ENABLED
#include "qrcodegen.h"
#endif // QR_CODE_ENABLED
#endif // DISPLAY_ENABLED
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/server/OnboardingCodesUtil.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/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
/**********************************************************
* Defines and Constants
*********************************************************/
#define SYSTEM_STATE_LED &sl_led_led0
#define APP_FUNCTION_BUTTON 0
#define APP_USER_ACTION 1
using namespace chip;
using namespace chip::app;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Silabs;
using namespace chip::TLV;
using namespace ::chip::DeviceLayer;
/**********************************************************
* AppTask Definitions
*********************************************************/
AppTask AppTask::sAppTask;
CHIP_ERROR AppTask::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::DeviceLayer::Silabs::GetPlatform().SetButtonsCb(AppTask::ButtonEventHandler);
#ifdef DISPLAY_ENABLED
GetLCD().Init((uint8_t *) "LIT ICD");
#endif
err = BaseApplication::Init();
if (err != CHIP_NO_ERROR)
{
SILABS_LOG("BaseApplication::Init() failed");
appError(err);
}
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);
}
chip::Server::GetInstance().GetICDManager().RegisterObserver(&sAppTask);
#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::ApplicationEventHandler(AppEvent * aEvent)
{
VerifyOrReturn(aEvent->Type == AppEvent::kEventType_Button);
VerifyOrReturn(aEvent->ButtonEvent.Action == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed));
// Simple Application logic that toggles the BoleanState StateValue attribute.
// DO NOT COPY for product logic. LIT ICD app is a test app with very simple application logic to enable testing.
// The goal of the app is just to enable testing of LIT ICD features without impacting product sample apps.
PlatformMgr().ScheduleWork([](intptr_t) {
bool state = true;
Protocols::InteractionModel::Status status = chip::app::Clusters::BooleanState::Attributes::StateValue::Get(1, &state);
if (status != Protocols::InteractionModel::Status::Success)
{
// Failed to read StateValue. Default to true (open state)
state = true;
ChipLogError(NotSpecified, "ERR: reading boolean status value %x", to_underlying(status));
}
status = chip::app::Clusters::BooleanState::Attributes::StateValue::Set(1, !state);
if (status != Protocols::InteractionModel::Status::Success)
{
ChipLogError(NotSpecified, "ERR: updating boolean status value %x", to_underlying(status));
}
});
}
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_USER_ACTION)
{
button_event.Handler = ApplicationEventHandler;
sAppTask.PostEvent(&button_event);
}
else if (button == APP_FUNCTION_BUTTON)
{
button_event.Handler = BaseApplication::ButtonHandler;
sAppTask.PostEvent(&button_event);
}
}
// DO NOT COPY for product logic. LIT ICD app is a test app with very simple application logic to enable testing.
void AppTask::OnEnterActiveMode()
{
#ifdef DISPLAY_ENABLED
sAppTask.GetLCD().WriteDemoUI(true);
#endif
}
// DO NOT COPY for product logic. LIT ICD app is a test app with very simple application logic to enable testing.
void AppTask::OnEnterIdleMode()
{
#ifdef DISPLAY_ENABLED
sAppTask.GetLCD().WriteDemoUI(false);
#endif
}