blob: ed5fc52e7e56387783c1c50fc6cd6d9e060e0d3f [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 "LightingManager.h"
#include "AppConfig.h"
#include "AppTask.h"
#include <FreeRTOS.h>
using namespace chip;
using namespace ::chip::DeviceLayer;
LightingManager LightingManager::sLight;
TimerHandle_t sLightTimer;
CHIP_ERROR LightingManager::Init()
{
// Create FreeRTOS sw timer for light timer.
sLightTimer = xTimerCreate("lightTmr", // Just a text name, not used by the RTOS kernel
1, // == default timer period (mS)
false, // no timer reload (==one-shot)
(void *) this, // init timer id = light obj context
TimerEventHandler // timer callback handler
);
if (sLightTimer == NULL)
{
PLAT_LOG("sLightTimer timer create failed");
}
bool currentLedState;
// read current on/off value on endpoint one.
OnOffServer::Instance().getOnOffValue(1, &currentLedState);
mState = currentLedState ? kState_OnCompleted : kState_OffCompleted;
return CHIP_NO_ERROR;
}
void LightingManager::SetCallbacks(Callback_fn_initiated aActionInitiated_CB, Callback_fn_completed aActionCompleted_CB)
{
mActionInitiated_CB = aActionInitiated_CB;
mActionCompleted_CB = aActionCompleted_CB;
}
bool LightingManager::IsActionInProgress()
{
return (mState == kState_OffInitiated || mState == kState_OnInitiated);
}
bool LightingManager::IsLightOn()
{
return (mState == kState_OnCompleted);
}
bool LightingManager::InitiateAction(int32_t aActor, Action_t aAction)
{
bool action_initiated = false;
State_t new_state;
// Initiate Turn On/Off Action only when the previous one is complete.
if (mState == kState_OffCompleted && aAction == ON_ACTION)
{
action_initiated = true;
new_state = kState_OnInitiated;
}
else if (mState == kState_OnCompleted && aAction == OFF_ACTION)
{
action_initiated = true;
new_state = kState_OffInitiated;
}
if (action_initiated)
{
StartTimer(ACTUATOR_MOVEMENT_PERIOD_MS);
// Since the timer started successfully, update the state and trigger callback
mState = new_state;
if (mActionInitiated_CB)
{
mActionInitiated_CB(aAction, aActor);
}
}
return action_initiated;
}
void LightingManager::StartTimer(uint32_t aTimeoutMs)
{
if (xTimerIsTimerActive(sLightTimer))
{
PLAT_LOG("app timer already started!");
CancelTimer();
}
// timer is not active, change its period to required value (== restart).
// FreeRTOS- Block for a maximum of 100 ticks if the change period command
// cannot immediately be sent to the timer command queue.
if (xTimerChangePeriod(sLightTimer, pdMS_TO_TICKS(aTimeoutMs), 100) != pdPASS)
{
PLAT_LOG("sLightTimer timer start() failed");
}
}
void LightingManager::CancelTimer(void)
{
if (xTimerStop(sLightTimer, 0) == pdFAIL)
{
PLAT_LOG("sLightTimer stop() failed");
}
}
void LightingManager::TimerEventHandler(TimerHandle_t xTimer)
{
// Get light obj context from timer id.
LightingManager * light = static_cast<LightingManager *>(pvTimerGetTimerID(xTimer));
// The timer event handler will be called in the context of the timer task
// once sLightTimer expires. Post an event to apptask queue with the actual handler
// so that the event can be handled in the context of the apptask.
AppEvent event;
event.Type = AppEvent::kEventType_AppEvent;
event.LightEvent.Context = light;
event.Handler = ActuatorMovementTimerEventHandler;
AppTask::GetAppTask().PostEvent(&event);
}
void LightingManager::ActuatorMovementTimerEventHandler(AppEvent * aEvent)
{
Action_t actionCompleted = INVALID_ACTION;
LightingManager * light = static_cast<LightingManager *>(aEvent->LightEvent.Context);
if (light->mState == kState_OffInitiated)
{
light->mState = kState_OffCompleted;
actionCompleted = OFF_ACTION;
OnOffServer::Instance().setOnOffValue(1, 0, false);
}
else if (light->mState == kState_OnInitiated)
{
light->mState = kState_OnCompleted;
actionCompleted = ON_ACTION;
OnOffServer::Instance().setOnOffValue(1, 1, false);
}
if (actionCompleted != INVALID_ACTION)
{
if (light->mActionCompleted_CB)
{
light->mActionCompleted_CB(actionCompleted);
}
}
}