blob: 6607be264a53e7e77dc2aee748acc242fef0a1cc [file]
/*
*
* Copyright (c) 2022 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 "LightSwitch.h"
#include "AppEvent.h"
#include "BindingHandler.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app/clusters/bindings/binding-table.h>
#include <app/clusters/switch-server/switch-server.h>
#include <app/server/Server.h>
#include <controller/InvokeInteraction.h>
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
void LightSwitch::Init(chip::EndpointId aLightDimmerSwitchEndpoint)
{
BindingHandler::GetInstance().Init();
mLightSwitchEndpoint = aLightDimmerSwitchEndpoint;
}
void LightSwitch::InitiateActionSwitch(Action mAction)
{
BindingHandler::BindingData * data = Platform::New<BindingHandler::BindingData>();
if (data)
{
data->EndpointId = mLightSwitchEndpoint;
data->ClusterId = Clusters::OnOff::Id;
switch (mAction)
{
case Action::Toggle:
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
break;
case Action::On:
data->CommandId = Clusters::OnOff::Commands::On::Id;
break;
case Action::Off:
data->CommandId = Clusters::OnOff::Commands::Off::Id;
break;
default:
Platform::Delete(data);
return;
}
data->IsGroup = BindingHandler::GetInstance().IsGroupBound();
TEMPORARY_RETURN_IGNORED DeviceLayer::PlatformMgr().ScheduleWork(BindingHandler::SwitchWorkerHandler,
reinterpret_cast<intptr_t>(data));
}
}
void LightSwitch::SwitchChangeBrightness(uint16_t sBrightness)
{
BindingHandler::BindingData * data = Platform::New<BindingHandler::BindingData>();
if (data)
{
data->EndpointId = mLightSwitchEndpoint;
data->CommandId = Clusters::LevelControl::Commands::MoveToLevel::Id;
data->ClusterId = Clusters::LevelControl::Id;
// set brightness of light.
if (sBrightness > kMaximumBrightness)
{
sBrightness = kMaximumBrightness;
}
data->Value = (uint8_t) sBrightness;
data->IsGroup = BindingHandler::GetInstance().IsGroupBound();
TEMPORARY_RETURN_IGNORED DeviceLayer::PlatformMgr().ScheduleWork(BindingHandler::SwitchWorkerHandler,
reinterpret_cast<intptr_t>(data));
}
}
void LightSwitch::InitGeneric(chip::EndpointId aGenericSwitchEndpoint)
{
BindingHandler::GetInstance().Init();
mGenericSwitchEndpointId = aGenericSwitchEndpoint;
}
void LightSwitch::GenericSwitchInitialPress()
{
TEMPORARY_RETURN_IGNORED DeviceLayer::SystemLayer().ScheduleLambda([this] {
// Press moves Position from 0 (idle) to 1 (press)
uint8_t newPosition = 1;
auto switchCluster = Clusters::Switch::FindClusterOnEndpoint(mGenericSwitchEndpointId);
VerifyOrReturn(switchCluster != nullptr);
CHIP_ERROR status = switchCluster->SetCurrentPosition(newPosition);
VerifyOrReturn(CHIP_NO_ERROR == status, ChipLogError(NotSpecified, "Failed to set CurrentPosition attribute"));
RETURN_SAFELY_IGNORED switchCluster->OnInitialPress(newPosition);
});
}
void LightSwitch::GenericSwitchReleasePress()
{
TEMPORARY_RETURN_IGNORED DeviceLayer::SystemLayer().ScheduleLambda([this] {
// Release moves Position from 1 (press) to 0 (idle)
uint8_t previousPosition = 1;
uint8_t newPosition = 0;
auto switchCluster = Clusters::Switch::FindClusterOnEndpoint(mGenericSwitchEndpointId);
VerifyOrReturn(switchCluster != nullptr);
CHIP_ERROR status = switchCluster->SetCurrentPosition(newPosition);
VerifyOrReturn(CHIP_NO_ERROR == status, ChipLogError(NotSpecified, "Failed to set CurrentPosition attribute"));
RETURN_SAFELY_IGNORED switchCluster->OnShortRelease(previousPosition);
});
}