1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "JniRcsRemoteResourceObject.h"
23 #include "JniRcsObject.h"
24 #include "JniRcsResourceAttributes.h"
25 #include "JavaClasses.h"
26 #include "JavaExceptions.h"
27 #include "JavaGlobalRef.h"
29 #include "ScopedEnv.h"
32 #include "RCSRemoteResourceObject.h"
34 using namespace OIC::Service;
36 #define LOG_TAG "JNI-RCSRemoteResourceObject"
38 #define CLS_NAME_RESOURCE_STATE CLS_NAME_REMOTERESOURCEOBJECT "$ResourceState"
39 #define CLS_NAME_CACHE_STATE CLS_NAME_REMOTERESOURCEOBJECT "$CacheState"
41 #define CLS_NAME_ON_STATE_CHANGED_LISTENER CLS_NAME_REMOTERESOURCEOBJECT "$OnStateChangedListener"
42 #define CLS_NAME_ON_CACHE_UPDATED_LISTENER CLS_NAME_REMOTERESOURCEOBJECT "$OnCacheUpdatedListener"
43 #define CLS_NAME_ON_REMOTE_ATTRIBUTES_RECEIVED_LISTENER \
44 CLS_NAME_REMOTERESOURCEOBJECT "$OnRemoteAttributesReceivedListener"
48 jclass g_cls_RCSRemoteResourceObject;
49 jclass g_cls_ResourceState;
51 jmethodID g_ctor_RCSRemoteResourceObject;
53 jmethodID g_method_onStateChanged;
54 jmethodID g_method_onCacheUpdated;
55 jmethodID g_method_onAttributesReceived;
57 jobject g_obj_ResourceState_None;
58 jobject g_obj_ResourceState_Requested;
59 jobject g_obj_ResourceState_Alive;
60 jobject g_obj_ResourceState_LostSignal;
61 jobject g_obj_ResourceState_Destoryed;
63 jobject g_obj_CacheState_None;
64 jobject g_obj_CacheState_Unready;
65 jobject g_obj_CacheState_Ready;
66 jobject g_obj_CacheState_LostSignal;
69 inline jobjectArray toJavaStringArray(JNIEnv* env, const std::vector< std::string >& vec)
71 jobjectArray arrayObj = env->NewObjectArray(vec.size(), g_cls_String, nullptr);
72 if (!arrayObj) return nullptr;
73 for (size_t i = 0; i < vec.size(); ++i)
75 jstring strObj = env->NewStringUTF(vec[i].c_str());
76 VERIFY_NO_EXC_RET_DEF(env);
78 env->SetObjectArrayElement(arrayObj, i, strObj);
79 VERIFY_NO_EXC_RET_DEF(env);
81 env->DeleteLocalRef(strObj);
86 template < typename ENV >
87 inline jobject convertResourceState(ENV* env, ResourceState state)
91 case ResourceState::NONE: return g_obj_ResourceState_None;
92 case ResourceState::REQUESTED: return g_obj_ResourceState_Requested;
93 case ResourceState::ALIVE: return g_obj_ResourceState_Alive;
94 case ResourceState::LOST_SIGNAL: return g_obj_ResourceState_LostSignal;
95 case ResourceState::DESTROYED: return g_obj_ResourceState_Destoryed;
98 throwRCSException(env, "Failed to convert ResourceState");
102 inline jobject convertCacheState(JNIEnv* env, CacheState state)
106 case CacheState::NONE: return g_obj_CacheState_None;
107 case CacheState::UNREADY: return g_obj_CacheState_Unready;
108 case CacheState::READY: return g_obj_CacheState_Ready;
109 case CacheState::LOST_SIGNAL: return g_obj_CacheState_LostSignal;
112 throwRCSException(env, "Failed to convert CacheState");
116 inline RCSRemoteResourceObject::Ptr& getResource(JNIEnv* env, jobject obj) noexcept
118 return getNativeHandleAs< RCSRemoteResourceObject::Ptr >(env, obj);
121 void onStateChanged(ResourceState newState, const JavaGlobalRef& listener)
123 ScopedEnvWrapper env;
124 EXPECT(env, "env is null!");
128 env->CallVoidMethod(listener, g_method_onStateChanged,
129 convertResourceState(env.get(), newState));
131 catch (const JavaException&)
133 env->ExceptionDescribe();
134 env->ExceptionClear();
138 void onCacheUpdated(const RCSResourceAttributes& attrs, const JavaGlobalRef& listener)
140 LOGD("onCacheUpdated");
142 ScopedEnvWrapper env;
143 EXPECT(env, "env is null!");
147 env->CallVoidMethod(listener, g_method_onCacheUpdated,
148 newAttributesObject(env.get(), attrs));
150 catch (const JavaException&)
152 env->ExceptionDescribe();
153 env->ExceptionClear();
157 void onRemoteAttributesReceived(const RCSResourceAttributes& attrs, int errorCode,
158 const JavaGlobalRef& listener)
160 ScopedEnvWrapper env;
161 EXPECT(env, "env is null!");
165 env->CallVoidMethod(listener, g_method_onAttributesReceived,
166 newAttributesObject(env.get(), attrs), errorCode);
168 catch (const JavaException&)
170 env->ExceptionDescribe();
171 env->ExceptionClear();
177 void initRCSRemoteResourceObject(JNIEnvWrapper* env)
179 g_cls_RCSRemoteResourceObject = env->FindClassAsGlobalRef(CLS_NAME_REMOTERESOURCEOBJECT);
181 g_ctor_RCSRemoteResourceObject = env->GetConstructorID(g_cls_RCSRemoteResourceObject, "()V");
183 auto clsOnStateChangedListener = env->FindClass(CLS_NAME_ON_STATE_CHANGED_LISTENER);
184 g_method_onStateChanged = env->GetMethodID(clsOnStateChangedListener, "onStateChanged",
185 "(" AS_SIG(CLS_NAME_RESOURCE_STATE) ")V");
187 auto clsOnCacheUpdatedListener = env->FindClass(CLS_NAME_ON_CACHE_UPDATED_LISTENER);
188 g_method_onCacheUpdated = env->GetMethodID(clsOnCacheUpdatedListener, "onCacheUpdated",
189 "(" AS_SIG(CLS_NAME_RESOURCEATTRIBUTES) ")V");
191 auto clsOnRemoteAttributesReceivedListener =
192 env->FindClass(CLS_NAME_ON_REMOTE_ATTRIBUTES_RECEIVED_LISTENER);
193 g_method_onAttributesReceived = env->GetMethodID(clsOnRemoteAttributesReceivedListener,
194 "onAttributesReceived", "(" AS_SIG(CLS_NAME_RESOURCEATTRIBUTES) "I)V");
196 auto clsResourceState = env->FindClass(CLS_NAME_RESOURCE_STATE);
198 g_obj_ResourceState_None = env->NewGlobalRef(env->GetStaticObjectField(clsResourceState,
199 "NONE", AS_SIG(CLS_NAME_RESOURCE_STATE)));
201 g_obj_ResourceState_Requested = env->NewGlobalRef(env->GetStaticObjectField(clsResourceState,
202 "REQUESTED", AS_SIG(CLS_NAME_RESOURCE_STATE)));
204 g_obj_ResourceState_Alive = env->NewGlobalRef(env->GetStaticObjectField(clsResourceState,
205 "ALIVE", AS_SIG(CLS_NAME_RESOURCE_STATE)));
207 g_obj_ResourceState_LostSignal = env->NewGlobalRef(env->GetStaticObjectField(clsResourceState,
208 "LOST_SIGNAL", AS_SIG(CLS_NAME_RESOURCE_STATE)));
210 g_obj_ResourceState_Destoryed = env->NewGlobalRef(env->GetStaticObjectField(clsResourceState,
211 "DESTROYED", AS_SIG(CLS_NAME_RESOURCE_STATE)));
213 auto clsCacheState = env->FindClass(CLS_NAME_CACHE_STATE);
215 g_obj_CacheState_None = env->NewGlobalRef(env->GetStaticObjectField(clsCacheState,
216 "NONE", AS_SIG(CLS_NAME_CACHE_STATE)));
218 g_obj_CacheState_Unready = env->NewGlobalRef(env->GetStaticObjectField(clsCacheState,
219 "UNREADY", AS_SIG(CLS_NAME_CACHE_STATE)));
221 g_obj_CacheState_Ready = env->NewGlobalRef(env->GetStaticObjectField(clsCacheState,
222 "READY", AS_SIG(CLS_NAME_CACHE_STATE)));
224 g_obj_CacheState_LostSignal = env->NewGlobalRef(env->GetStaticObjectField(clsCacheState,
225 "LOST_SIGNAL", AS_SIG(CLS_NAME_CACHE_STATE)));
230 void clearRCSRemoteResourceObject(JNIEnvWrapper* env)
232 env->DeleteGlobalRef(g_cls_RCSRemoteResourceObject);
233 env->DeleteGlobalRef(g_cls_ResourceState);
235 env->DeleteGlobalRef(g_obj_ResourceState_None);
236 env->DeleteGlobalRef(g_obj_ResourceState_Requested);
237 env->DeleteGlobalRef(g_obj_ResourceState_Alive);
238 env->DeleteGlobalRef(g_obj_ResourceState_LostSignal);
239 env->DeleteGlobalRef(g_obj_ResourceState_Destoryed);
241 env->DeleteGlobalRef(g_obj_CacheState_None);
242 env->DeleteGlobalRef(g_obj_CacheState_Unready);
243 env->DeleteGlobalRef(g_obj_CacheState_Ready);
244 env->DeleteGlobalRef(g_obj_CacheState_LostSignal);
247 jobject newRemoteResourceObject(JNIEnvWrapper* env)
249 return env->NewObject(g_cls_RCSRemoteResourceObject, g_ctor_RCSRemoteResourceObject);
252 JNIEXPORT jboolean JNICALL
253 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeIsMonitoring
254 (JNIEnv* env, jobject obj)
256 LOGD("nativeIsMonitoring");
257 auto res = getResource(env, obj);
258 VERIFY_NO_EXC_RET_DEF(env);
260 return res->isMonitoring();
263 JNIEXPORT jboolean JNICALL
264 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeIsCaching
265 (JNIEnv* env, jobject obj)
267 LOGD("nativeIsCaching");
268 auto res = getResource(env, obj);
269 VERIFY_NO_EXC_RET_DEF(env);
271 return res->isCaching();
274 JNIEXPORT jboolean JNICALL
275 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeIsObservable
276 (JNIEnv* env, jobject obj)
278 LOGD("nativeIsObservable");
279 auto res = getResource(env, obj);
280 VERIFY_NO_EXC_RET_DEF(env);
282 return res->isObservable();
285 JNIEXPORT void JNICALL
286 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeStartMonitoring
287 (JNIEnv* env, jobject obj, jobject listener)
289 LOGD("nativeStartMonitoring");
290 EXPECT(listener, "listener is null.");
292 auto res = getResource(env, obj);
297 res->startMonitoring(
298 std::bind(onStateChanged, std::placeholders::_1, JavaGlobalRef{ env, listener }));
300 catch (const BadRequestException& e)
302 env->ThrowNew(env->FindClass(EXC_NAME_ILLEGAL_STATE), e.what());
306 JNIEXPORT void JNICALL
307 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeStopMonitoring
308 (JNIEnv* env, jobject obj)
310 LOGD("nativeStopMonitoring");
312 auto res = getResource(env, obj);
315 res->stopMonitoring();
318 JNIEXPORT jobject JNICALL
319 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeGetState
320 (JNIEnv* env, jobject obj)
322 LOGD("nativeGetState");
324 auto res = getResource(env, obj);
325 VERIFY_NO_EXC_RET_DEF(env);
327 return convertResourceState(env, res->getState());
330 JNIEXPORT void JNICALL
331 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeStartCaching
332 (JNIEnv* env, jobject obj, jobject listener)
334 LOGD("nativeStartCaching");
336 auto res = getResource(env, obj);
343 res->startCaching(std::bind(onCacheUpdated,
344 std::placeholders::_1, JavaGlobalRef{ env, listener }));
351 catch (const BadRequestException& e)
353 env->ThrowNew(env->FindClass(EXC_NAME_ILLEGAL_STATE), e.what());
357 JNIEXPORT void JNICALL
358 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeStopCaching
359 (JNIEnv* env, jobject obj)
361 LOGD("nativeStopCaching");
363 auto res = getResource(env, obj);
369 JNIEXPORT jobject JNICALL
370 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeGetCacheState
371 (JNIEnv* env, jobject obj)
373 LOGD("nativeGetCacheState");
375 auto res = getResource(env, obj);
376 VERIFY_NO_EXC_RET_DEF(env);
378 return convertCacheState(env, res->getCacheState());
381 JNIEXPORT jboolean JNICALL
382 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeIsCachedAvailable
383 (JNIEnv* env, jobject obj)
385 LOGD("nativeIsCachedAvailable");
387 auto res = getResource(env, obj);
388 VERIFY_NO_EXC_RET_DEF(env);
392 return res->isCachedAvailable();
394 catch (const BadRequestException& e)
396 env->ThrowNew(env->FindClass(EXC_NAME_ILLEGAL_STATE), e.what());
401 JNIEXPORT jobject JNICALL
402 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeGetCachedAttributes
403 (JNIEnv* env, jobject obj)
405 LOGD("nativeGetCachedAttributes");
407 auto res = getResource(env, obj);
408 VERIFY_NO_EXC_RET_DEF(env);
412 auto attrs = res->getCachedAttributes();
413 VERIFY_NO_EXC_RET_DEF(env);
415 return newAttributesObject(env, attrs);
417 catch (const BadRequestException& e)
419 env->ThrowNew(env->FindClass(EXC_NAME_ILLEGAL_STATE), e.what());
424 JNIEXPORT void JNICALL
425 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeGetRemoteAttributes
426 (JNIEnv* env, jobject obj, jobject listener)
428 LOGD("nativeGetRemoteAttributes");
429 EXPECT(listener, "listener is null.");
431 auto res = getResource(env, obj);
436 res->getRemoteAttributes(std::bind(onRemoteAttributesReceived,
437 std::placeholders::_1, std::placeholders::_2, JavaGlobalRef{ env, listener }));
439 catch (const PlatformException& e) {
440 throwPlatformException(env, e);
444 JNIEXPORT void JNICALL
445 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeSetRemoteAttributes
446 (JNIEnv* env, jobject obj, jobject attrsObj, jobject listener)
448 LOGD("nativeSetRemoteAttributes");
449 auto res = getResource(env, obj);
452 RCSResourceAttributes attrs = toNativeAttributes(env, attrsObj);
457 res->setRemoteAttributes(attrs, std::bind(onRemoteAttributesReceived,
458 std::placeholders::_1, std::placeholders::_2, JavaGlobalRef{ env, listener }));
460 catch (const PlatformException& e) {
461 throwPlatformException(env, e);
465 JNIEXPORT jstring JNICALL
466 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeGetUri
467 (JNIEnv* env, jobject obj)
469 LOGD("nativeGetUri");
471 auto res = getResource(env, obj);
472 VERIFY_NO_EXC_RET_DEF(env);
474 return env->NewStringUTF(res->getUri().c_str());
477 JNIEXPORT jstring JNICALL
478 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeGetAddress
479 (JNIEnv* env, jobject obj)
481 LOGD("nativeGetAddress");
482 auto res = getResource(env, obj);
483 VERIFY_NO_EXC_RET_DEF(env);
485 return env->NewStringUTF(res->getAddress().c_str());
488 JNIEXPORT jobjectArray JNICALL
489 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeGetTypes
490 (JNIEnv* env, jobject obj)
492 LOGD("nativeGetTypes");
493 auto res = getResource(env, obj);
494 VERIFY_NO_EXC_RET_DEF(env);
496 return toJavaStringArray(env, res->getTypes());
499 JNIEXPORT jobjectArray JNICALL
500 Java_org_iotivity_service_client_RcsRemoteResourceObject_nativeGetInterfaces
501 (JNIEnv* env, jobject obj)
503 LOGD("nativeGetInterfaces");
505 auto res = getResource(env, obj);
506 VERIFY_NO_EXC_RET_DEF(env);
508 return toJavaStringArray(env, res->getInterfaces());