-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathAppPlatform-JNI.cpp
184 lines (162 loc) · 7.51 KB
/
AppPlatform-JNI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright (c) 2021 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 "AppImpl.h"
#include <app/app-platform/ContentApp.h>
#include <jni.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPJNIError.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>
using namespace chip;
using namespace chip::app;
using namespace chip::AppPlatform;
using namespace chip::Credentials;
/*
* This file provides the native implementation of methods of the
* com.matter.tv.server.tvapp.AppPlatform class.
*/
// Forward declaration
std::vector<ContentApp::SupportedCluster> convert_to_cpp(JNIEnv * env, jobject supportedClusters);
#define JNI_METHOD(RETURN, METHOD_NAME) \
extern "C" JNIEXPORT RETURN JNICALL Java_com_matter_tv_server_tvapp_AppPlatform_##METHOD_NAME
JNI_METHOD(void, nativeInit)(JNIEnv *, jobject app, jobject contentAppEndpointManager)
{
chip::DeviceLayer::StackLock lock;
InitVideoPlayerPlatform(contentAppEndpointManager);
}
JNI_METHOD(jint, addContentApp)
(JNIEnv *, jobject, jstring vendorName, jint vendorId, jstring appName, jint productId, jstring appVersion,
jobject supportedClusters, jobject manager)
{
chip::DeviceLayer::StackLock lock;
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
JniUtfString vName(env, vendorName);
JniUtfString aName(env, appName);
JniUtfString aVersion(env, appVersion);
EndpointId epId = AddContentApp(vName.c_str(), static_cast<uint16_t>(vendorId), aName.c_str(), static_cast<uint16_t>(productId),
aVersion.c_str(), convert_to_cpp(env, supportedClusters), manager);
return static_cast<uint16_t>(epId);
}
JNI_METHOD(jint, addContentAppAtEndpoint)
(JNIEnv *, jobject, jstring vendorName, jint vendorId, jstring appName, jint productId, jstring appVersion,
jobject supportedClusters, jint endpointId, jobject manager)
{
chip::DeviceLayer::StackLock lock;
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
JniUtfString vName(env, vendorName);
JniUtfString aName(env, appName);
JniUtfString aVersion(env, appVersion);
EndpointId epId =
AddContentApp(vName.c_str(), static_cast<uint16_t>(vendorId), aName.c_str(), static_cast<uint16_t>(productId),
aVersion.c_str(), convert_to_cpp(env, supportedClusters), static_cast<EndpointId>(endpointId), manager);
return static_cast<uint16_t>(epId);
}
JNI_METHOD(jint, removeContentApp)
(JNIEnv *, jobject, jint endpointId)
{
chip::DeviceLayer::StackLock lock;
EndpointId epId = RemoveContentApp(static_cast<EndpointId>(endpointId));
return static_cast<uint16_t>(epId);
}
JNI_METHOD(void, reportAttributeChange)
(JNIEnv *, jobject, jint endpointId, jint clusterId, jint attributeId)
{
chip::DeviceLayer::StackLock lock;
ReportAttributeChange(static_cast<EndpointId>(endpointId), static_cast<chip::ClusterId>(clusterId),
static_cast<chip::AttributeId>(attributeId));
}
JNI_METHOD(void, addSelfVendorAsAdmin)
(JNIEnv *, jobject, jint endpointId, jint clusterId, jint attributeId)
{
AddSelfVendorAsAdmin();
}
std::vector<uint32_t> consume_and_convert_to_cpp(JNIEnv * env, jobject intArrayObject)
{
std::vector<uint32_t> uintVector;
if (intArrayObject != nullptr)
{
jsize length = env->GetArrayLength(static_cast<jintArray>(intArrayObject));
jint * elements = env->GetIntArrayElements(static_cast<jintArray>(intArrayObject), nullptr);
if (elements != nullptr)
{
// OBS: Implicit type ambiguation from int32_t to uint32_t
uintVector.assign(elements, elements + length);
env->ReleaseIntArrayElements(static_cast<jintArray>(intArrayObject), elements, JNI_ABORT);
}
env->DeleteLocalRef(intArrayObject);
}
return uintVector;
}
std::vector<ContentApp::SupportedCluster> convert_to_cpp(JNIEnv * env, jobject supportedClustersObject)
{
if (supportedClustersObject == nullptr || env == nullptr)
{
return {};
}
// Find Java classes. WARNING: Reflection
jclass collectionClass = env->FindClass("java/util/Collection");
jclass iteratorClass = env->FindClass("java/util/Iterator");
jclass clusterClass = env->FindClass("com/matter/tv/server/tvapp/ContentAppSupportedCluster");
if (collectionClass == nullptr || iteratorClass == nullptr || clusterClass == nullptr)
{
return {};
}
// Find Java methods. WARNING: Reflection
jmethodID iteratorMethod = env->GetMethodID(collectionClass, "iterator", "()Ljava/util/Iterator;");
jmethodID hasNextMethod = env->GetMethodID(iteratorClass, "hasNext", "()Z");
jmethodID nextMethod = env->GetMethodID(iteratorClass, "next", "()Ljava/lang/Object;");
if (iteratorMethod == nullptr || hasNextMethod == nullptr || nextMethod == nullptr)
{
return {};
}
// Find Java SupportedCluster fields. WARNING: Reflection
jfieldID clusterIdentifierField = env->GetFieldID(clusterClass, "clusterIdentifier", "I");
jfieldID featuresField = env->GetFieldID(clusterClass, "features", "I");
jfieldID optionalCommandIdentifiersField = env->GetFieldID(clusterClass, "optionalCommandIdentifiers", "[I");
jfieldID optionalAttributesIdentifiersField = env->GetFieldID(clusterClass, "optionalAttributesIdentifiers", "[I");
if (clusterIdentifierField == nullptr || featuresField == nullptr || optionalCommandIdentifiersField == nullptr ||
optionalAttributesIdentifiersField == nullptr)
{
return {};
}
// Find Set Iterator Object
jobject iteratorObject = env->CallObjectMethod(supportedClustersObject, iteratorMethod);
if (iteratorObject == nullptr)
{
return {};
}
// Iterate over the Java Collection and convert each SupportedCluster
std::vector<SupportedCluster> supportedClusters;
while (env->CallBooleanMethod(iteratorObject, hasNextMethod))
{
jobject clusterObject = env->CallObjectMethod(iteratorObject, nextMethod);
if (clusterObject != nullptr)
{
jint clusterIdentifier = env->GetIntField(clusterObject, clusterIdentifierField);
jint features = env->GetIntField(clusterObject, featuresField);
jobject commandIdsObject = env->GetObjectField(clusterObject, optionalCommandIdentifiersField);
jobject attributeIdsObject = env->GetObjectField(clusterObject, optionalAttributesIdentifiersField);
// OBS: Implicit type ambiguation from int32_t to uint32_t
supportedClusters.emplace_back(clusterIdentifier, features, consume_and_convert_to_cpp(env, commandIdsObject),
consume_and_convert_to_cpp(env, attributeIdsObject));
env->DeleteLocalRef(clusterObject);
}
}
env->DeleteLocalRef(iteratorObject);
return supportedClusters;
}