blob: 1b4881ff77db51f5285d5400e3de8726829b4592 [file] [log] [blame]
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2019 Google LLC.
* 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 "AppConfig.h"
#include "AppEvent.h"
#include "PumpManager.h"
#include "LEDWidget.h"
#include <app/clusters/on-off-server/on-off-server.h>
#include <app/server/OnboardingCodesUtil.h>
#include <app/server/Server.h>
#include <app/util/attribute-storage.h>
#include <app-common/zap-generated/attributes/Accessors.h>
#include <assert.h>
#include <platform/silabs/platformAbstraction/SilabsPlatform.h>
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>
#include <lib/support/CodeUtils.h>
#include <platform/CHIPDeviceLayer.h>
#define PCC_CLUSTER_ENDPOINT 1
#define ONOFF_CLUSTER_ENDPOINT 1
// Sparkfun only has one led
#if !defined(BRD2704A)
#define PUMP_LED 1
#else
#define PUMP_LED 0
#endif
#define APP_PUMP_SWITCH 1
#define APP_FUNCTION_BUTTON 0
using namespace chip;
using namespace chip::app;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceLayer::Silabs;
using namespace ::chip::app::Clusters;
namespace {
LEDWidget sPumpLED;
}
using namespace chip::TLV;
using namespace ::chip::DeviceLayer;
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 *) "Pump-App");
#endif
err = BaseApplication::Init();
if (err != CHIP_NO_ERROR)
{
SILABS_LOG("BaseApplication::Init() failed");
appError(err);
}
err = PumpMgr().Init();
if (err != CHIP_NO_ERROR)
{
SILABS_LOG("PumpMgr::Init() failed");
appError(err);
}
PumpMgr().SetCallbacks(ActionInitiated, ActionCompleted);
sPumpLED.Init(PUMP_LED);
sPumpLED.Set(!PumpMgr().IsStopped());
// Update the LCD with the Stored value. Show QR Code if not provisioned
#ifdef DISPLAY_ENABLED
GetLCD().WriteDemoUI(!PumpMgr().IsStopped());
#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::PumpActionEventHandler(AppEvent * aEvent)
{
bool initiated = false;
PumpManager::Action_t action;
int32_t actor;
CHIP_ERROR err = CHIP_NO_ERROR;
if (aEvent->Type == AppEvent::kEventType_PumpTimer)
{
action = static_cast<PumpManager::Action_t>(aEvent->PumpEvent.Action);
actor = aEvent->PumpEvent.Actor;
}
else if (aEvent->Type == AppEvent::kEventType_Button)
{
action = (PumpMgr().IsStopped()) ? PumpManager::START_ACTION : PumpManager::STOP_ACTION;
actor = AppEvent::kEventType_Button;
}
else
{
err = APP_ERROR_UNHANDLED_EVENT;
}
if (err == CHIP_NO_ERROR)
{
initiated = PumpMgr().InitiateAction(actor, action);
if (!initiated)
{
SILABS_LOG("Action is already in progress or active.");
}
}
}
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_PUMP_SWITCH && btnAction == static_cast<uint8_t>(SilabsPlatform::ButtonAction::ButtonPressed))
{
button_event.Handler = PumpActionEventHandler;
AppTask::GetAppTask().PostEvent(&button_event);
}
else if (button == APP_FUNCTION_BUTTON)
{
button_event.Handler = BaseApplication::ButtonHandler;
AppTask::GetAppTask().PostEvent(&button_event);
}
}
void AppTask::ActionInitiated(PumpManager::Action_t aAction, int32_t aActor)
{
// Action initiated, update the light led
bool pumpOn = aAction == PumpManager::START_ACTION;
SILABS_LOG(" Pump %s", (pumpOn) ? "starting" : "stopping")
sPumpLED.Set(pumpOn);
#ifdef DISPLAY_ENABLED
sAppTask.GetLCD().WriteDemoUI(pumpOn);
#endif
if (aActor == AppEvent::kEventType_Button)
{
sAppTask.mSyncClusterToButtonAction = true;
}
}
void AppTask::ActionCompleted(PumpManager::Action_t aAction, int32_t aActor)
{
// action has been completed bon the light
if (aAction == PumpManager::STOP_ACTION)
{
SILABS_LOG("Pump Stopped")
}
else if (aAction == PumpManager::STOP_ACTION)
{
SILABS_LOG("Pump OFF")
}
if (sAppTask.mSyncClusterToButtonAction)
{
chip::DeviceLayer::PlatformMgr().ScheduleWork(UpdateClusterState, reinterpret_cast<intptr_t>(nullptr));
sAppTask.mSyncClusterToButtonAction = false;
}
}
void AppTask::UpdateClusterState(intptr_t context)
{
// Set On/Off state
Protocols::InteractionModel::Status status;
bool onOffState = !PumpMgr().IsStopped();
status = chip::app::Clusters::OnOff::Attributes::OnOff::Set(PCC_CLUSTER_ENDPOINT, onOffState);
if (status != Protocols::InteractionModel::Status::Success)
{
ChipLogError(NotSpecified, "ERR: Updating On/Off state %x", to_underlying(status));
}
}