Skip to content

Commit

Permalink
virtual-device-app: Add ColorControlManager for Extended Color Light (p…
Browse files Browse the repository at this point in the history
…roject-chip#28653)

Signed-off-by: Hunsup Jung <[email protected]>
Signed-off-by: Charles Kim <[email protected]>
  • Loading branch information
HunsupJung authored Aug 16, 2023
1 parent 446dd19 commit bd771be
Show file tree
Hide file tree
Showing 8 changed files with 673 additions and 7 deletions.
3 changes: 3 additions & 0 deletions examples/virtual-device-app/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ shared_library("jni") {
"java/AppImpl.cpp",
"java/AppImpl.h",
"java/ClusterChangeAttribute.cpp",
"java/ColorControlManager.cpp",
"java/ColorControlManager.h",
"java/DeviceApp-JNI.cpp",
"java/JNIDACProvider.cpp",
"java/JNIDACProvider.h",
Expand Down Expand Up @@ -65,6 +67,7 @@ android_library("java") {

sources = [
"java/src/com/matter/virtual/device/app/Clusters.java",
"java/src/com/matter/virtual/device/app/ColorControlManager.java",
"java/src/com/matter/virtual/device/app/DACProvider.java",
"java/src/com/matter/virtual/device/app/DeviceApp.java",
"java/src/com/matter/virtual/device/app/DeviceAppCallback.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

#include "ColorControlManager.h"
#include "OnOffManager.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/ids/Attributes.h>
Expand All @@ -38,6 +39,56 @@ static void OnOffClusterAttributeChangeCallback(const app::ConcreteAttributePath
}
}

static void ColorControlClusterAttributeChangeCallback(const app::ConcreteAttributePath & attributePath, uint16_t size,
uint8_t * value)
{
if (attributePath.mAttributeId == ColorControl::Attributes::CurrentHue::Id)
{
uint8_t currentHue = static_cast<uint8_t>(*value);

ChipLogProgress(Zcl, "Received CurrentHue command endpoint %d value = %d", static_cast<int>(attributePath.mEndpointId),
currentHue);

ColorControlManager().PostCurrentHueChanged(attributePath.mEndpointId, currentHue);
}
else if (attributePath.mAttributeId == ColorControl::Attributes::CurrentSaturation::Id)
{
uint8_t currentSaturation = static_cast<uint8_t>(*value);

ChipLogProgress(Zcl, "Received CurrentSaturation command endpoint %d value = %d",
static_cast<int>(attributePath.mEndpointId), currentSaturation);

ColorControlManager().PostCurrentSaturationChanged(attributePath.mEndpointId, currentSaturation);
}
else if (attributePath.mAttributeId == ColorControl::Attributes::ColorTemperatureMireds::Id)
{
int16_t colorTemperatureMireds = static_cast<int16_t>(*value);

ChipLogProgress(Zcl, "Received ColorTemperatureMireds command endpoint %d value = %d",
static_cast<int>(attributePath.mEndpointId), colorTemperatureMireds);

ColorControlManager().PostColorTemperatureChanged(attributePath.mEndpointId, colorTemperatureMireds);
}
else if (attributePath.mAttributeId == ColorControl::Attributes::ColorMode::Id)
{
uint8_t colorMode = static_cast<uint8_t>(*value);

ChipLogProgress(Zcl, "Received ColorMode command endpoint %d value = %d", static_cast<int>(attributePath.mEndpointId),
colorMode);

ColorControlManager().PostColorModeChanged(attributePath.mEndpointId, colorMode);
}
else if (attributePath.mAttributeId == ColorControl::Attributes::EnhancedColorMode::Id)
{
uint8_t enhancedColorMode = static_cast<uint8_t>(*value);

ChipLogProgress(Zcl, "Received EnhancedColorMode command endpoint %d value = %d",
static_cast<int>(attributePath.mEndpointId), enhancedColorMode);

ColorControlManager().PostEnhancedColorModeChanged(attributePath.mEndpointId, enhancedColorMode);
}
}

void MatterPostAttributeChangeCallback(const app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size,
uint8_t * value)
{
Expand All @@ -49,6 +100,9 @@ void MatterPostAttributeChangeCallback(const app::ConcreteAttributePath & attrib
case OnOff::Id:
OnOffClusterAttributeChangeCallback(attributePath, size, value);
break;
case ColorControl::Id:
ColorControlClusterAttributeChangeCallback(attributePath, size, value);
break;

default:
break;
Expand Down
269 changes: 269 additions & 0 deletions examples/virtual-device-app/android/java/ColorControlManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
/**
*
* Copyright (c) 2023 Project CHIP Authors
*
* 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 "ColorControlManager.h"
#include "DeviceApp-JNI.h"
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/util/af.h>
#include <jni.h>
#include <lib/support/CHIPJNIError.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>

using namespace chip;

static constexpr size_t kColorControlManagerTableSize = EMBER_AF_COLOR_CONTROL_CLUSTER_SERVER_ENDPOINT_COUNT;

namespace {

ColorControlManager * gColorControlManagerTable[kColorControlManagerTableSize] = { nullptr };
static_assert(kColorControlManagerTableSize <= kEmberInvalidEndpointIndex, "gColorControlManagerTable table size error");

} // namespace

void emberAfColorControlClusterInitCallback(EndpointId endpoint)
{
ChipLogProgress(Zcl, "Device App::ColorControl::PostClusterInit");
DeviceAppJNIMgr().PostClusterInit(chip::app::Clusters::ColorControl::Id, endpoint);
}

void ColorControlManager::NewManager(jint endpoint, jobject manager)
{
ChipLogProgress(Zcl, "Device App: ColorControlManager::NewManager");
uint16_t ep = emberAfGetClusterServerEndpointIndex(static_cast<chip::EndpointId>(endpoint), app::Clusters::ColorControl::Id,
EMBER_AF_COLOR_CONTROL_CLUSTER_SERVER_ENDPOINT_COUNT);
VerifyOrReturn(ep < kColorControlManagerTableSize,
ChipLogError(Zcl, "Device App::ColorControl::NewManager: endpoint %d not found", endpoint));

VerifyOrReturn(gColorControlManagerTable[ep] == nullptr,
ChipLogError(Zcl, "ST Device App::ColorControl::NewManager: endpoint %d already has a manager", endpoint));
ColorControlManager * mgr = new ColorControlManager();
CHIP_ERROR err = mgr->InitializeWithObjects(manager);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "ST Device App::ColorControl::NewManager: failed to initialize manager for endpoint %d", endpoint);
delete mgr;
}
else
{
gColorControlManagerTable[ep] = mgr;
}
}

static ColorControlManager * GetColorControlManager(EndpointId endpoint)
{
uint16_t ep = emberAfGetClusterServerEndpointIndex(endpoint, app::Clusters::ColorControl::Id,
EMBER_AF_COLOR_CONTROL_CLUSTER_SERVER_ENDPOINT_COUNT);
return (ep >= kColorControlManagerTableSize ? nullptr : gColorControlManagerTable[ep]);
}

void ColorControlManager::PostCurrentHueChanged(chip::EndpointId endpoint, int value)
{
ChipLogProgress(Zcl, "Device App: ColorControlManager::PostCurrentHueChanged");
ColorControlManager * mgr = GetColorControlManager(endpoint);
VerifyOrReturn(mgr != nullptr, ChipLogError(Zcl, "ColorControlManager null"));

mgr->HandleCurrentHueChanged(value);
}

void ColorControlManager::PostCurrentSaturationChanged(chip::EndpointId endpoint, int value)
{
ChipLogProgress(Zcl, "Device App: ColorControlManager::PostCurrentSaturationChanged");
ColorControlManager * mgr = GetColorControlManager(endpoint);
VerifyOrReturn(mgr != nullptr, ChipLogError(Zcl, "ColorControlManager null"));

mgr->HandleCurrentSaturationChanged(value);
}

void ColorControlManager::PostColorTemperatureChanged(chip::EndpointId endpoint, int value)
{
ChipLogProgress(Zcl, "Device App: ColorControlManager::PostColorTemperatureChanged");
ColorControlManager * mgr = GetColorControlManager(endpoint);
VerifyOrReturn(mgr != nullptr, ChipLogError(Zcl, "ColorControlManager null"));

mgr->HandleColorTemperatureChanged(value);
}

void ColorControlManager::PostColorModeChanged(chip::EndpointId endpoint, int value)
{
ChipLogProgress(Zcl, "Device App: ColorControlManager::PostColorModeChanged");
ColorControlManager * mgr = GetColorControlManager(endpoint);
VerifyOrReturn(mgr != nullptr, ChipLogError(Zcl, "ColorControlManager null"));

mgr->HandleColorModeChanged(value);
}

void ColorControlManager::PostEnhancedColorModeChanged(chip::EndpointId endpoint, int value)
{
ChipLogProgress(Zcl, "Device App: ColorControlManager::PostEnhancedColorModeChanged");
ColorControlManager * mgr = GetColorControlManager(endpoint);
VerifyOrReturn(mgr != nullptr, ChipLogError(Zcl, "ColorControlManager null"));

mgr->HandleEnhancedColorModeChanged(value);
}

CHIP_ERROR ColorControlManager::InitializeWithObjects(jobject managerObject)
{
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturnLogError(env != nullptr, CHIP_ERROR_INCORRECT_STATE);

mColorControlManagerObject = env->NewGlobalRef(managerObject);
VerifyOrReturnLogError(mColorControlManagerObject != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

jclass ColorControlManagerClass = env->GetObjectClass(managerObject);
VerifyOrReturnLogError(ColorControlManagerClass != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

mHandleCurrentHueChangedMethod = env->GetMethodID(ColorControlManagerClass, "HandleCurrentHueChanged", "(I)V");
if (mHandleCurrentHueChangedMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access ColorControlManager 'HandleCurrentHueChanged' method");
env->ExceptionClear();
return CHIP_ERROR_INVALID_ARGUMENT;
}

mHandleCurrentSaturationChangedMethod = env->GetMethodID(ColorControlManagerClass, "HandleCurrentSaturationChanged", "(I)V");
if (mHandleCurrentSaturationChangedMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access ColorControlManager 'HandleCurrentSaturationChanged' method");
env->ExceptionClear();
return CHIP_ERROR_INVALID_ARGUMENT;
}

mHandleColorTemperatureChangedMethod = env->GetMethodID(ColorControlManagerClass, "HandleColorTemperatureChanged", "(I)V");
if (mHandleColorTemperatureChangedMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access ColorControlManager 'HandleColorTemperatureChanged' method");
env->ExceptionClear();
return CHIP_ERROR_INVALID_ARGUMENT;
}

mHandleColorModeChangedMethod = env->GetMethodID(ColorControlManagerClass, "HandleColorModeChanged", "(I)V");
if (mHandleColorModeChangedMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access ColorControlManager 'HandleColorModeChanged' method");
env->ExceptionClear();
return CHIP_ERROR_INVALID_ARGUMENT;
}

mHandleEnhancedColorModeChangedMethod = env->GetMethodID(ColorControlManagerClass, "HandleEnhancedColorModeChanged", "(I)V");
if (mHandleEnhancedColorModeChangedMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access ColorControlManager 'HandleEnhancedColorModeChanged' method");
env->ExceptionClear();
return CHIP_ERROR_INVALID_ARGUMENT;
}

return CHIP_NO_ERROR;
}

void ColorControlManager::HandleCurrentHueChanged(int value)
{
ChipLogProgress(Zcl, "ColorControlManager::HandleCurrentHueChanged");

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturn(env != NULL, ChipLogProgress(Zcl, "env null"));
VerifyOrReturn(mColorControlManagerObject != nullptr, ChipLogProgress(Zcl, "mColorControlManagerObject null"));
VerifyOrReturn(mHandleCurrentHueChangedMethod != nullptr, ChipLogProgress(Zcl, "mHandleCurrentHueChangedMethod null"));

env->ExceptionClear();
env->CallVoidMethod(mColorControlManagerObject, mHandleCurrentHueChangedMethod, value);
if (env->ExceptionCheck())
{
ChipLogError(AppServer, "Java exception in ColorControlManager::HandleCurrentHueChanged");
env->ExceptionDescribe();
env->ExceptionClear();
}
}

void ColorControlManager::HandleCurrentSaturationChanged(int value)
{
ChipLogProgress(Zcl, "ColorControlManager::HandleCurrentSaturationChanged");

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturn(env != NULL, ChipLogProgress(Zcl, "env null"));
VerifyOrReturn(mColorControlManagerObject != nullptr, ChipLogProgress(Zcl, "mColorControlManagerObject null"));
VerifyOrReturn(mHandleCurrentSaturationChangedMethod != nullptr,
ChipLogProgress(Zcl, "mHandleCurrentSaturationChangedMethod null"));

env->ExceptionClear();
env->CallVoidMethod(mColorControlManagerObject, mHandleCurrentSaturationChangedMethod, value);
if (env->ExceptionCheck())
{
ChipLogError(AppServer, "Java exception in ColorControlManager::HandleCurrentSaturationChanged");
env->ExceptionDescribe();
env->ExceptionClear();
}
}

void ColorControlManager::HandleColorTemperatureChanged(int value)
{
ChipLogProgress(Zcl, "ColorControlManager::HandleColorTemperatureChanged");

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturn(env != NULL, ChipLogProgress(Zcl, "env null"));
VerifyOrReturn(mColorControlManagerObject != nullptr, ChipLogProgress(Zcl, "mColorControlManagerObject null"));
VerifyOrReturn(mHandleColorTemperatureChangedMethod != nullptr,
ChipLogProgress(Zcl, "mHandleColorTemperatureChangedMethod null"));

env->ExceptionClear();
env->CallVoidMethod(mColorControlManagerObject, mHandleColorTemperatureChangedMethod, value);
if (env->ExceptionCheck())
{
ChipLogError(AppServer, "Java exception in ColorControlManager::mHandleColorTemperatureChanged");
env->ExceptionDescribe();
env->ExceptionClear();
}
}

void ColorControlManager::HandleColorModeChanged(int value)
{
ChipLogProgress(Zcl, "ColorControlManager::HandleColorModeChanged");

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturn(env != NULL, ChipLogProgress(Zcl, "env null"));
VerifyOrReturn(mColorControlManagerObject != nullptr, ChipLogProgress(Zcl, "mColorControlManagerObject null"));
VerifyOrReturn(mHandleColorModeChangedMethod != nullptr, ChipLogProgress(Zcl, "mHandleColorModeChangedMethod null"));

env->ExceptionClear();
env->CallVoidMethod(mColorControlManagerObject, mHandleColorModeChangedMethod, value);
if (env->ExceptionCheck())
{
ChipLogError(AppServer, "Java exception in ColorControlManager::HandleColorModeChanged");
env->ExceptionDescribe();
env->ExceptionClear();
}
}

void ColorControlManager::HandleEnhancedColorModeChanged(int value)
{
ChipLogProgress(Zcl, "ColorControlManager::HandleEnhancedColorModeChanged");

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturn(env != NULL, ChipLogProgress(Zcl, "env null"));
VerifyOrReturn(mColorControlManagerObject != nullptr, ChipLogProgress(Zcl, "mColorControlManagerObject null"));
VerifyOrReturn(mHandleEnhancedColorModeChangedMethod != nullptr,
ChipLogProgress(Zcl, "mHandleEnhancedColorModeChangedMethod null"));

env->ExceptionClear();
env->CallVoidMethod(mColorControlManagerObject, mHandleEnhancedColorModeChangedMethod, value);
if (env->ExceptionCheck())
{
ChipLogError(AppServer, "Java exception in ColorControlManager::HandleEnhancedColorModeChanged");
env->ExceptionDescribe();
env->ExceptionClear();
}
}
Loading

0 comments on commit bd771be

Please sign in to comment.