Skip to content

Commit

Permalink
Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Feb 16, 2024
1 parent b481c7d commit bb4ce80
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 260 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
*
* Copyright (c) 2020-24 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 "MatterCommandObjects-JNI.h"

namespace matter {
namespace casting {
namespace clusters {

CHIP_ERROR ContentLauncherClusterLaunchURLCommand::GetCppRequestFromJava(
jobject inRequest, chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type & outRequest)
{
VerifyOrReturnValue(inRequest != nullptr, CHIP_NO_ERROR);

JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread();

jclass jRequestClass;
ReturnErrorOnFailure(chip::JniReferences::GetInstance().GetClassRef(
env, "com/matter/casting/clusters/MatterCommands$ContentLauncherClusterLaunchURLRequest", jRequestClass));

jfieldID jContentURLField = env->GetFieldID(jRequestClass, "contentURL", "Ljava/lang/String;");
jstring jContentURLObj = (jstring) env->GetObjectField(inRequest, jContentURLField);
VerifyOrReturnError(jContentURLObj != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
outRequest.contentURL = chip::CharSpan::fromCharString(env->GetStringUTFChars(jContentURLObj, 0));

jfieldID jDisplayStringField = env->GetFieldID(jRequestClass, "displayString", "Ljava/lang/String;");
jstring jDisplayStringObj = (jstring) env->GetObjectField(inRequest, jDisplayStringField);
if (jDisplayStringObj != nullptr)
{
const char * nativeValue = env->GetStringUTFChars(jDisplayStringObj, 0);
outRequest.displayString = chip::Optional<chip::CharSpan>(chip::CharSpan::fromCharString(nativeValue));
}
else
{
outRequest.displayString = chip::NullOptional;
}

// TODO: translate brandingInformation
outRequest.brandingInformation =
chip::MakeOptional(chip::app::Clusters::ContentLauncher::Structs::BrandingInformationStruct::Type());
return CHIP_NO_ERROR;
}

jobject ContentLauncherClusterLaunchURLCommand::GetJResponseFromCpp(
const chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType * response)
{
VerifyOrReturnValue(response != nullptr, nullptr);
JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread();

jclass responseTypeClass = nullptr;
VerifyOrReturnValue(
chip::JniReferences::GetInstance().GetClassRef(
env, "com/matter/casting/clusters/MatterCommands$ContentLauncherClusterResponse", responseTypeClass) == CHIP_NO_ERROR,
nullptr, ChipLogError(AppServer, "convertResponseFromCppToJava could not get ContentLauncherClusterResponse class ref"));

jmethodID constructor = env->GetMethodID(responseTypeClass, "<init>", "()V");
jobject jResponseObj = env->NewObject(responseTypeClass, constructor);

if (response->data.HasValue())
{
jfieldID dataField = env->GetFieldID(responseTypeClass, "data", "Ljava/lang/String;");
char * buffer = new char[response->data.Value().size() + 1];
strncpy(buffer, response->data.Value().data(), response->data.Value().size());
buffer[response->data.Value().size()] = '\0';
env->SetObjectField(jResponseObj, dataField, env->NewStringUTF(buffer));
delete[] buffer;
}

jclass integerClass = env->FindClass("java/lang/Integer");
jmethodID valueOf = env->GetStaticMethodID(integerClass, "valueOf", "(I)Ljava/lang/Integer;");
jobject statusObj = env->CallStaticObjectMethod(integerClass, valueOf, static_cast<jint>(response->status));
jfieldID statusField = env->GetFieldID(responseTypeClass, "status", "Ljava/lang/Integer;");
env->SetObjectField(jResponseObj, statusField, statusObj);

return jResponseObj;
}

}; // namespace clusters
}; // namespace casting
}; // namespace matter
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
*
* Copyright (c) 2020-24 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.
*/

#pragma once

#include "../core/MatterCommandTemplate-JNI.h"

#include <app-common/zap-generated/cluster-objects.h>

#include <jni.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>

namespace matter {
namespace casting {
namespace clusters {

class ContentLauncherClusterLaunchURLCommand
: public core::MatterCommandTemplateJNI<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type>
{
public:
ContentLauncherClusterLaunchURLCommand(jobject jCommand) :
core::MatterCommandTemplateJNI<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type>(jCommand)
{}

CHIP_ERROR GetCppRequestFromJava(jobject inRequest,
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type & outRequest);
jobject GetJResponseFromCpp(const chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType * response);
};

class MatterCommandManager
{
public:
static jobject Invoke(jobject jCommand, jobject jRequest, jobject jTimedInvokeTimeoutMs)
{
const char * commandClassName = core::MatterCommandJNI::GetCommandClassName(jCommand);
VerifyOrReturnValue(commandClassName != nullptr, nullptr,
ChipLogError(AppServer, "MatterCommandManager: Could not get commandClassName from jCommand"));

if (strcmp(commandClassName, "com.matter.casting.clusters.MatterCommands$ContentLauncherClusterLaunchURLCommand") == 0)
{
auto * command = new clusters::ContentLauncherClusterLaunchURLCommand(jCommand);
return command->Invoke(jRequest, jTimedInvokeTimeoutMs);
}
return nullptr;
}
};

}; // namespace clusters
}; // namespace casting
}; // namespace matter

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/

#include "MatterCommand-JNI.h"
#include "../clusters/MatterCommands-JNI.h"
#include "../clusters/MatterCommandObjects-JNI.h"

#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>

using namespace chip;

Expand All @@ -32,20 +35,7 @@ JNI_METHOD(jobject, invoke)
{
chip::DeviceLayer::StackLock lock;
ChipLogProgress(AppServer, "MatterCommand-JNI::invoke() called");

const char * commandClassName = MatterCommandJNI::GetCommandClassName(thiz);
VerifyOrReturnValue(commandClassName != nullptr, nullptr,
ChipLogError(AppServer, "MatterCommand-JNI::invoke() Could not get mCommandClassName from jCommand"));
if (strcmp(commandClassName, "com.matter.casting.clusters.MatterCommands$ContentLauncherClusterLaunchURLCommand") == 0)
{
CommandTemplateJNI<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type> * command =
new CommandTemplateJNI<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type>(
thiz, "com/matter/casting/clusters/MatterCommands$ContentLauncherClusterLaunchURLRequest",
clusters::ContentLauncherClusterLaunchURLCommand::convertRequestFromJavaToCpp,
clusters::ContentLauncherClusterLaunchURLCommand::convertResponseFromCppToJava);
return command->invoke(jRequest, jTimedInvokeTimeoutMs);
}
return nullptr;
return clusters::MatterCommandManager::Invoke(thiz, jRequest, jTimedInvokeTimeoutMs);
}

const char * MatterCommandJNI::GetCommandClassName(jobject jCommandObject)
Expand Down
Loading

0 comments on commit bb4ce80

Please sign in to comment.