Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / resource-encapsulation / android / service / src / main / jni / JniRcsResourceObject.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include "JniRcsResourceObject.h"
22
23 #include "JniRcsObject.h"
24 #include "JniRcsResourceAttributes.h"
25 #include "JavaClasses.h"
26 #include "JavaExceptions.h"
27 #include "JavaGlobalRef.h"
28 #include "JniRcsValue.h"
29 #include "Log.h"
30 #include "ScopedEnv.h"
31 #include "Verify.h"
32
33 #include "RCSResourceObject.h"
34 #include "RCSResponse.h"
35 #include "RequestHandler.h"
36
37 #define LOG_TAG "JNI-RCSResourceObject"
38
39 #define CLS_NAME_RCS_RESOURCE_OBJECT PACKAGE_NAME "/server/RcsResourceObject"
40 #define CLS_NAME_RCS_REQUEST PACKAGE_NAME "/server/RcsRequest"
41
42 #define CLS_NAME_AUTO_NOTIFY_POLICY CLS_NAME_RCS_RESOURCE_OBJECT "$AutoNotifyPolicy"
43 #define CLS_NAME_SET_REQUEST_HANDLER_POLICY CLS_NAME_RCS_RESOURCE_OBJECT "$SetRequestHandlerPolicy"
44
45 #define CLS_NAME_GET_REQUEST_HANDLER CLS_NAME_RCS_RESOURCE_OBJECT "$GetRequestHandler"
46 #define CLS_NAME_SET_REQUEST_HANDLER CLS_NAME_RCS_RESOURCE_OBJECT "$SetRequestHandler"
47 #define CLS_NAME_ON_ATTRIBUTE_UPDATESD_LISTENER \
48     CLS_NAME_RCS_RESOURCE_OBJECT "$OnAttributeUpdatedListener"
49
50 #define CLS_NAME_RCS_RESPONSE PACKAGE_NAME "/server/RcsResponse"
51
52 #define CLS_NAME_RCS_GET_RESPONSE PACKAGE_NAME "/server/RcsGetResponse"
53 #define CLS_NAME_RCS_SET_RESPONSE PACKAGE_NAME "/server/RcsSetResponse"
54 #define CLS_NAME_RCS_SET_RESPONSE_ACCEPTANCE_METHOD CLS_NAME_RCS_SET_RESPONSE "$AcceptanceMethod"
55
56 using namespace OIC::Service;
57
58 namespace
59 {
60     jclass g_cls_RCSRequest;
61     jclass g_cls_RCSResourceObject;
62
63     jmethodID g_ctor_RCSRequest;
64     jmethodID g_ctor_RCSResourceObject;
65
66     jmethodID g_method_GetRequestHandler_onGetRequested;
67     jmethodID g_method_SetRequestHandler_onSetRequested;
68     jmethodID g_method_OnAttributeUpdatedListener_onAttributeUpdated;
69
70     jfieldID g_field_RCSResponse_mErrorCode;
71     jfieldID g_field_RCSResponse_mAttrs;
72     jfieldID g_field_RCSSetResponse_mAcceptanceMethod;
73
74     jobject g_obj_RCSSetResponse_AcceptanceMethod_DEFAULT;
75     jobject g_obj_RCSSetResponse_AcceptanceMethod_ACCEPT;
76     jobject g_obj_RCSSetResponse_AcceptanceMethod_IGNORE;
77
78     jobject g_obj_AutoNotifyPolicy_NEVER;
79     jobject g_obj_AutoNotifyPolicy_ALWAYS;
80     jobject g_obj_AutoNotifyPolicy_UPDATED;
81
82     jobject g_obj_SetRequestHandlerPolicy_NEVER;
83     jobject g_obj_SetRequestHandlerPolicy_ACCEPT;
84
85     inline RCSResourceObject::Ptr& getResource(JNIEnv* env, jobject obj)
86     {
87         return getNativeHandleAs< RCSResourceObject::Ptr >(env, obj);
88     }
89
90     inline RCSResourceObject::AutoNotifyPolicy convertAutoNotifyPolicy(JNIEnv* env, jobject obj)
91     {
92         if (env->IsSameObject(g_obj_AutoNotifyPolicy_NEVER, obj))
93         {
94             return RCSResourceObject::AutoNotifyPolicy::NEVER;
95         }
96
97         if (env->IsSameObject(g_obj_AutoNotifyPolicy_ALWAYS, obj))
98         {
99             return RCSResourceObject::AutoNotifyPolicy::ALWAYS;
100         }
101
102         if (env->IsSameObject(g_obj_AutoNotifyPolicy_UPDATED, obj))
103         {
104             return RCSResourceObject::AutoNotifyPolicy::UPDATED;
105         }
106
107         throwRCSException(env, "Failed to convert AutoNotifyPolicy");
108         return {};
109     }
110
111     inline jobject convertAutoNotifyPolicy(JNIEnv* env, RCSResourceObject::AutoNotifyPolicy policy)
112     {
113         switch(policy)
114         {
115             case RCSResourceObject::AutoNotifyPolicy::NEVER: return g_obj_AutoNotifyPolicy_NEVER;
116             case RCSResourceObject::AutoNotifyPolicy::ALWAYS: return g_obj_AutoNotifyPolicy_ALWAYS;
117             case RCSResourceObject::AutoNotifyPolicy::UPDATED: return g_obj_AutoNotifyPolicy_UPDATED;
118         }
119
120         throwRCSException(env, "Failed to convert AutoNotifyPolicy");
121         return {};
122     }
123
124     inline RCSResourceObject::SetRequestHandlerPolicy convertSetRequestHandlerPolicy(JNIEnv* env,
125             jobject obj)
126     {
127         if (env->IsSameObject(g_obj_SetRequestHandlerPolicy_NEVER, obj))
128         {
129             return RCSResourceObject::SetRequestHandlerPolicy::NEVER;
130         }
131
132         if (env->IsSameObject(g_obj_SetRequestHandlerPolicy_ACCEPT, obj))
133         {
134             return RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE;
135         }
136
137         throwRCSException(env, "Failed to convert SetRequestHandlerPolicy");
138         return {};
139     }
140
141     inline jobject convertSetRequestHandlerPolicy(JNIEnv* env,
142             RCSResourceObject::SetRequestHandlerPolicy policy)
143     {
144         switch(policy)
145         {
146             case RCSResourceObject::SetRequestHandlerPolicy::NEVER:
147                 return g_obj_SetRequestHandlerPolicy_NEVER;
148             case RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE:
149                 return g_obj_SetRequestHandlerPolicy_ACCEPT;
150         }
151
152         throwRCSException(env, "Failed to convert SetRequestHandlerPolicy");
153         return {};
154     }
155
156
157     template< typename ENV >
158     inline jobject getAttrsObj(ENV& env, jobject responseObj)
159     {
160         return env->GetObjectField(responseObj, g_field_RCSResponse_mAttrs);
161     }
162
163     template< typename ENV >
164     inline int getErrorCode(ENV& env, jobject responseObj)
165     {
166         return env->GetIntField(responseObj, g_field_RCSResponse_mErrorCode);
167     }
168
169     template< typename ENV >
170     inline RCSSetResponse::AcceptanceMethod getAcceptanceMethod(ENV& env, jobject responseObj)
171     {
172         auto methodObj = env->GetObjectField(responseObj, g_field_RCSSetResponse_mAcceptanceMethod);
173
174         if (env->IsSameObject(methodObj, g_obj_RCSSetResponse_AcceptanceMethod_IGNORE))
175         {
176             return RCSSetResponse::AcceptanceMethod::IGNORE;
177         }
178
179         if (env->IsSameObject(methodObj, g_obj_RCSSetResponse_AcceptanceMethod_ACCEPT))
180         {
181             return RCSSetResponse::AcceptanceMethod::ACCEPT;
182         }
183
184         return RCSSetResponse::AcceptanceMethod::DEFAULT;
185     }
186
187     inline jobject callHandler(ScopedEnvWrapper& env, jobject listener, jmethodID methodId,
188             const RCSRequest& request, const RCSResourceAttributes& attrs)
189     {
190         auto requestObj = env->NewObject(g_cls_RCSRequest, g_ctor_RCSRequest,
191                 env->NewStringUTF(request.getResourceUri().c_str()));
192         auto attrsObj = newAttributesObject(env.get(), attrs);
193
194         return env->CallObjectMethod(listener, methodId, requestObj, attrsObj);
195     }
196
197     template< typename RESPONSE >
198     inline RESPONSE createResponse(ScopedEnvWrapper& env, jobject responseObj)
199     {
200         auto errorCode = getErrorCode(env, responseObj);
201         auto responseAttrsObj = getAttrsObj(env, responseObj);
202
203         if (responseAttrsObj)
204         {
205             return RESPONSE::create(toNativeAttributes(env.get(), responseAttrsObj), errorCode);
206         }
207
208         return RESPONSE::create(errorCode);
209     }
210
211     RCSGetResponse onGetRequest(const RCSRequest& request, const RCSResourceAttributes& attrs,
212             const JavaGlobalRef& listener)
213     {
214         ScopedEnvWrapper env;
215         EXPECT_RET(env, "env is null!", RCSGetResponse::create(-1));
216
217         try
218         {
219             auto responseObj = callHandler(env, listener, g_method_GetRequestHandler_onGetRequested,
220                     request, attrs);
221
222             return createResponse< RCSGetResponse >(env, responseObj);
223         }
224         catch (const JavaException&)
225         {
226             env->ExceptionDescribe();
227             env->ExceptionClear();
228         }
229         return RCSGetResponse::create({ }, -1);
230     }
231
232     RCSSetResponse onSetRequest(const RCSRequest& request, const RCSResourceAttributes& attrs,
233              const JavaGlobalRef& listener)
234     {
235         ScopedEnvWrapper env;
236         EXPECT_RET(env, "env is null!", RCSSetResponse::create(-1));
237
238         try
239         {
240             auto responseObj = callHandler(env, listener, g_method_SetRequestHandler_onSetRequested,
241                                request, attrs);
242
243             auto acceptanceMethod = getAcceptanceMethod(env, responseObj);
244
245             return createResponse< RCSSetResponse >(env, responseObj).
246                     setAcceptanceMethod(acceptanceMethod);
247         }
248         catch (const JavaException&)
249         {
250             env->ExceptionDescribe();
251             env->ExceptionClear();
252         }
253         return RCSSetResponse::create(-1);
254     }
255
256
257     void onAttributeUpdated(const RCSResourceAttributes::Value& oldVal,
258             const RCSResourceAttributes::Value& newVal, const JavaGlobalRef& listener)
259     {
260         ScopedEnvWrapper env;
261         EXPECT(env, "env is null!");
262
263         try
264         {
265             env->CallVoidMethod(listener, g_method_OnAttributeUpdatedListener_onAttributeUpdated,
266                     newRCSValueObject(env.get(), oldVal), newRCSValueObject(env.get(), newVal));
267         }
268         catch (const JavaException&)
269         {
270             env->ExceptionDescribe();
271             env->ExceptionClear();
272         }
273     }
274
275     void initRCSResponse(JNIEnvWrapper* env)
276     {
277         auto clsRCSResponse = env->FindClass(CLS_NAME_RCS_RESPONSE);
278
279         g_field_RCSResponse_mErrorCode = env->GetFieldID(clsRCSResponse, "mErrorCode", "I");
280
281         g_field_RCSResponse_mAttrs = env->GetFieldID(clsRCSResponse, "mAttrs",
282                 AS_SIG(CLS_NAME_RESOURCEATTRIBUTES));
283
284         auto clsRCSSetResponse = env->FindClass(CLS_NAME_RCS_SET_RESPONSE);
285
286         g_field_RCSSetResponse_mAcceptanceMethod = env->GetFieldID(clsRCSSetResponse,
287                 "mAcceptanceMethod", AS_SIG(CLS_NAME_RCS_SET_RESPONSE_ACCEPTANCE_METHOD));
288
289         auto clsAcceptanceMethod = env->FindClass(CLS_NAME_RCS_SET_RESPONSE_ACCEPTANCE_METHOD);
290
291         g_obj_RCSSetResponse_AcceptanceMethod_DEFAULT = env->NewGlobalRef(
292                 env->GetStaticObjectField(clsAcceptanceMethod, "DEFAULT",
293                         AS_SIG(CLS_NAME_RCS_SET_RESPONSE_ACCEPTANCE_METHOD)));
294
295         g_obj_RCSSetResponse_AcceptanceMethod_ACCEPT = env->NewGlobalRef(
296                 env->GetStaticObjectField(clsAcceptanceMethod, "ACCEPT",
297                         AS_SIG(CLS_NAME_RCS_SET_RESPONSE_ACCEPTANCE_METHOD)));
298
299         g_obj_RCSSetResponse_AcceptanceMethod_IGNORE = env->NewGlobalRef(
300                 env->GetStaticObjectField(clsAcceptanceMethod, "IGNORE",
301                         AS_SIG(CLS_NAME_RCS_SET_RESPONSE_ACCEPTANCE_METHOD)));
302     }
303 }
304
305 void initRCSResourceObject(JNIEnvWrapper* env)
306 {
307     g_cls_RCSResourceObject = env->FindClassAsGlobalRef(CLS_NAME_RCS_RESOURCE_OBJECT);
308
309     g_ctor_RCSResourceObject = env->GetMethodID(g_cls_RCSResourceObject, "<init>", "()V");
310
311     g_cls_RCSRequest = env->FindClassAsGlobalRef(CLS_NAME_RCS_REQUEST);
312
313     g_ctor_RCSRequest = env->GetMethodID(g_cls_RCSRequest, "<init>",
314             "(" AS_SIG(CLS_NAME_STRING) ")V");
315
316     auto clsGetRequestHandler = env->FindClass(CLS_NAME_GET_REQUEST_HANDLER);
317
318     g_method_GetRequestHandler_onGetRequested = env->GetMethodID(clsGetRequestHandler,
319             "onGetRequested",
320             "(" AS_SIG(CLS_NAME_RCS_REQUEST) AS_SIG(CLS_NAME_RESOURCEATTRIBUTES) ")"
321                 AS_SIG(CLS_NAME_RCS_GET_RESPONSE));
322
323     auto clsSetRequestHandler = env->FindClass(CLS_NAME_SET_REQUEST_HANDLER);
324
325     g_method_SetRequestHandler_onSetRequested = env->GetMethodID(clsSetRequestHandler,
326             "onSetRequested",
327             "(" AS_SIG(CLS_NAME_RCS_REQUEST) AS_SIG(CLS_NAME_RESOURCEATTRIBUTES) ")"
328                 AS_SIG(CLS_NAME_RCS_SET_RESPONSE));
329
330     auto clsOnAttributeUpdatedListener = env->FindClass(CLS_NAME_ON_ATTRIBUTE_UPDATESD_LISTENER);
331
332     g_method_OnAttributeUpdatedListener_onAttributeUpdated = env->GetMethodID(
333             clsOnAttributeUpdatedListener, "onAttributeUpdated",
334             "(" AS_SIG(CLS_NAME_VALUE) AS_SIG(CLS_NAME_VALUE) ")V");
335
336     auto clsAutoNotifyPolicy = env->FindClass(CLS_NAME_AUTO_NOTIFY_POLICY);
337
338     g_obj_AutoNotifyPolicy_NEVER = env->NewGlobalRef(
339             env->GetStaticObjectField(clsAutoNotifyPolicy, "NEVER",
340                     AS_SIG(CLS_NAME_AUTO_NOTIFY_POLICY)));
341
342     g_obj_AutoNotifyPolicy_ALWAYS = env->NewGlobalRef(
343             env->GetStaticObjectField(clsAutoNotifyPolicy, "ALWAYS",
344                     AS_SIG(CLS_NAME_AUTO_NOTIFY_POLICY)));
345
346
347     g_obj_AutoNotifyPolicy_UPDATED = env->NewGlobalRef(
348             env->GetStaticObjectField(clsAutoNotifyPolicy, "UPDATED",
349                     AS_SIG(CLS_NAME_AUTO_NOTIFY_POLICY)));
350
351     auto clsSetRequestHandlerPolicy = env->FindClass(CLS_NAME_SET_REQUEST_HANDLER_POLICY);
352
353     g_obj_SetRequestHandlerPolicy_NEVER = env->NewGlobalRef(
354             env->GetStaticObjectField(clsSetRequestHandlerPolicy, "NEVER",
355                     AS_SIG(CLS_NAME_SET_REQUEST_HANDLER_POLICY)));
356
357     g_obj_SetRequestHandlerPolicy_ACCEPT = env->NewGlobalRef(
358             env->GetStaticObjectField(clsSetRequestHandlerPolicy, "ACCEPT",
359                     AS_SIG(CLS_NAME_SET_REQUEST_HANDLER_POLICY)));
360
361     initRCSResponse(env);
362 }
363
364 void clearRCSResourceObject(JNIEnvWrapper* env)
365 {
366     env->DeleteGlobalRef(g_cls_RCSRequest);
367
368     env->DeleteGlobalRef(g_obj_RCSSetResponse_AcceptanceMethod_DEFAULT);
369     env->DeleteGlobalRef(g_obj_RCSSetResponse_AcceptanceMethod_ACCEPT);
370     env->DeleteGlobalRef(g_obj_RCSSetResponse_AcceptanceMethod_IGNORE);
371
372     env->DeleteGlobalRef(g_obj_AutoNotifyPolicy_NEVER);
373     env->DeleteGlobalRef(g_obj_AutoNotifyPolicy_ALWAYS);
374     env->DeleteGlobalRef(g_obj_AutoNotifyPolicy_UPDATED);
375
376     env->DeleteGlobalRef(g_obj_SetRequestHandlerPolicy_NEVER);
377     env->DeleteGlobalRef(g_obj_SetRequestHandlerPolicy_ACCEPT);
378 }
379
380 JNIEXPORT jint JNICALL
381 Java_org_iotivity_service_server_RcsResponse_nativeGetDefaultErrorCode
382 (JNIEnv*, jclass)
383 {
384     return RequestHandler::DEFAULT_ERROR_CODE;
385 }
386
387 JNIEXPORT jobject JNICALL
388 Java_org_iotivity_service_server_RcsResourceObject_nativeBuild
389 (JNIEnv* env, jclass, jstring uriObj, jstring typeObj, jstring interfaceObj, jboolean isObservable,
390         jboolean isDiscoverable, jobject attrsObj)
391 {
392     LOGI("nativeBuild");
393
394     EXPECT_RET_DEF(uriObj, "uri is null.");
395
396     auto uri = toStdString(env, uriObj);
397     auto type = toStdString(env, typeObj);
398     auto interface = toStdString(env, interfaceObj);
399
400     RCSResourceAttributes attrs;
401     if (attrsObj)
402     {
403         attrs = toNativeAttributes(env, attrsObj);
404         VERIFY_NO_EXC_RET_DEF(env);
405     }
406
407     try
408     {
409         auto resource = RCSResourceObject::Builder(uri, type,interface).
410                 setDiscoverable(isDiscoverable). setObservable(isObservable).setAttributes(attrs).
411                 build();
412
413         auto resourceObj = env->NewObject(g_cls_RCSResourceObject, g_ctor_RCSResourceObject);
414         VERIFY_NO_EXC_RET_DEF(env);
415
416         setSafeNativeHandle< decltype(resource) >(env, resourceObj, resource);
417
418         return resourceObj;
419     }
420     catch (const RCSPlatformException& e)
421     {
422         LOGE("%s", e.what());
423         throwPlatformException(env, e);
424     }
425
426     return nullptr;
427 }
428
429
430 JNIEXPORT void JNICALL
431 Java_org_iotivity_service_server_RcsResourceObject_nativeSetAttribute
432 (JNIEnv* env, jobject obj, jstring keyObj, jobject valueObj)
433 {
434     LOGD("nativeSetAttributeInteger");
435
436     EXPECT(keyObj, "key is null.");
437     EXPECT(valueObj, "value is null.");
438
439     auto res = getResource(env, obj);
440     VERIFY_NO_EXC(env);
441
442     res->setAttribute(toStdString(env, keyObj), toNativeAttrsValue(env, valueObj));
443 }
444
445 JNIEXPORT jobject JNICALL
446 Java_org_iotivity_service_server_RcsResourceObject_nativeGetAttributeValue
447 (JNIEnv *env, jobject obj, jstring keyObj)
448 {
449     LOGD("nativeGetAttributeValue");
450
451     EXPECT_RET_DEF(keyObj, "key is null.");
452
453     auto res = getResource(env, obj);
454     VERIFY_NO_EXC_RET_DEF(env);
455
456     try
457     {
458         auto valueObj = newRCSValueObject(env, res->getAttributeValue(toStdString(env, keyObj)));
459         VERIFY_NO_EXC_RET_DEF(env);
460
461         return valueObj;
462     }
463     catch(const RCSInvalidKeyException& e)
464     {
465         return nullptr;
466     }
467 }
468
469 JNIEXPORT jboolean JNICALL
470 Java_org_iotivity_service_server_RcsResourceObject_nativeRemoveAttribute
471 (JNIEnv* env, jobject obj, jstring keyObj)
472 {
473     LOGD("nativeRemoveAttribute");
474
475     EXPECT_RET_DEF(keyObj, "key is null.");
476
477     auto res = getResource(env, obj);
478     VERIFY_NO_EXC_RET_DEF(env);
479
480     return res->removeAttribute(toStdString(env, keyObj));
481 }
482
483 JNIEXPORT jboolean JNICALL
484 Java_org_iotivity_service_server_RcsResourceObject_nativeContainsAttribute
485 (JNIEnv* env, jobject obj, jstring keyObj)
486 {
487     LOGD("nativeContainsAttribute");
488
489     EXPECT_RET_DEF(keyObj, "key is null.");
490
491     auto res = getResource(env, obj);
492     VERIFY_NO_EXC_RET_DEF(env);
493
494     return res->containsAttribute(toStdString(env, keyObj));
495 }
496
497 JNIEXPORT jobject JNICALL
498 Java_org_iotivity_service_server_RcsResourceObject_nativeGetAttributes
499 (JNIEnv* env, jobject obj)
500 {
501     LOGD("nativeGetAttributes");
502
503     auto res = getResource(env, obj);
504     VERIFY_NO_EXC_RET_DEF(env);
505
506     RCSResourceObject::LockGuard lock{ res };
507     return newAttributesObject(env, res->getAttributes());
508 }
509
510 JNIEXPORT jboolean JNICALL
511 Java_org_iotivity_service_server_RcsResourceObject_nativeIsObservable
512 (JNIEnv* env, jobject obj)
513 {
514     LOGD("nativeIsObservable");
515
516     auto res = getResource(env, obj);
517     VERIFY_NO_EXC_RET_DEF(env);
518
519     return res->isObservable();
520 }
521
522 JNIEXPORT jboolean JNICALL
523 Java_org_iotivity_service_server_RcsResourceObject_nativeIsDiscoverable
524 (JNIEnv *env, jobject obj)
525 {
526     LOGD("nativeIsDiscoverable");
527
528     auto res = getResource(env, obj);
529     VERIFY_NO_EXC_RET_DEF(env);
530
531     return res->isDiscoverable();
532 }
533
534 JNIEXPORT void JNICALL
535 Java_org_iotivity_service_server_RcsResourceObject_nativeSetGetRequestHandler
536 (JNIEnv *env, jobject obj, jobject listenerObj)
537 {
538     LOGD("nativeSetGetRequestHandler");
539
540     auto res = getResource(env, obj);
541     VERIFY_NO_EXC(env);
542
543     if (listenerObj)
544     {
545         res->setGetRequestHandler(std::bind(onGetRequest, std::placeholders::_1, std::placeholders::_2,
546                 JavaGlobalRef{ env, listenerObj }));
547     }
548     else
549     {
550         res->setGetRequestHandler({ });
551     }
552 }
553
554 JNIEXPORT void JNICALL
555 Java_org_iotivity_service_server_RcsResourceObject_nativeSetSetRequestHandler
556 (JNIEnv* env, jobject obj, jobject listenerObj)
557 {
558     LOGD("nativeSetSetRequestHandler");
559
560     auto res = getResource(env, obj);
561     VERIFY_NO_EXC(env);
562
563     if (listenerObj)
564     {
565         res->setSetRequestHandler(std::bind(onSetRequest, std::placeholders::_1,
566                 std::placeholders::_2, JavaGlobalRef{ env, listenerObj }));
567     }
568     else
569     {
570         res->setSetRequestHandler({ });
571     }
572 }
573
574 JNIEXPORT void JNICALL
575 Java_org_iotivity_service_server_RcsResourceObject_nativeAddAttributeUpdatedListener
576 (JNIEnv* env, jobject obj, jstring keyObj, jobject listenerObj)
577 {
578     LOGD("nativeAddAttributeUpdatedListener");
579
580     auto res = getResource(env, obj);
581     VERIFY_NO_EXC(env);
582
583     auto key = toStdString(env, keyObj);
584     VERIFY_NO_EXC(env);
585
586     res->addAttributeUpdatedListener(std::move(key), std::bind(onAttributeUpdated,
587             std::placeholders::_1, std::placeholders::_2, JavaGlobalRef{ env, listenerObj }));
588 }
589
590 JNIEXPORT jboolean JNICALL
591 Java_org_iotivity_service_server_RcsResourceObject_nativeRemoveAttributeUpdatedListener
592 (JNIEnv* env, jobject obj, jstring keyObj)
593 {
594     LOGD("nativeAddAttributeUpdatedListener");
595
596     EXPECT_RET_DEF(keyObj, "key is null");
597
598     auto res = getResource(env, obj);
599     VERIFY_NO_EXC_RET_DEF(env);
600
601     auto key = toStdString(env, keyObj);
602     VERIFY_NO_EXC_RET_DEF(env);
603
604     return res->removeAttributeUpdatedListener(key);
605 }
606
607 JNIEXPORT void JNICALL Java_org_iotivity_service_server_RcsResourceObject_nativeNotify
608 (JNIEnv* env, jobject obj)
609 {
610     LOGD("nativeNotify");
611
612     auto res = getResource(env, obj);
613     VERIFY_NO_EXC(env);
614
615     try
616     {
617         res->notify();
618     }
619     catch (const RCSPlatformException& e) {
620         throwPlatformException(env, e);
621     }
622 }
623
624 JNIEXPORT void JNICALL
625 Java_org_iotivity_service_server_RcsResourceObject_nativeSetAutoNotifyPolicy
626 (JNIEnv* env, jobject obj, jobject policyObj)
627 {
628     LOGD("nativeSetAutoNotifyPolicy");
629
630     EXPECT(policyObj, "policyObj is null");
631
632     auto res = getResource(env, obj);
633     VERIFY_NO_EXC(env);
634
635     auto policy = convertAutoNotifyPolicy(env, policyObj);
636     VERIFY_NO_EXC(env);
637
638     res->setAutoNotifyPolicy(policy);
639 }
640
641 JNIEXPORT jobject JNICALL
642 Java_org_iotivity_service_server_RcsResourceObject_nativeGetAutoNotifyPolicy
643 (JNIEnv* env, jobject obj)
644 {
645     LOGD("nativeGetAutoNotifyPolicy");
646
647     auto res = getResource(env, obj);
648     VERIFY_NO_EXC_RET_DEF(env);
649
650     return convertAutoNotifyPolicy(env, res->getAutoNotifyPolicy());
651 }
652
653 JNIEXPORT void JNICALL
654 Java_org_iotivity_service_server_RcsResourceObject_nativeSetSetRequestHandlerPolicy
655 (JNIEnv* env, jobject obj, jobject policyObj)
656 {
657     LOGD("nativeSetSetRequestHandlerPolicy");
658
659     EXPECT(policyObj, "policyObj is null");
660
661     auto res = getResource(env, obj);
662     VERIFY_NO_EXC(env);
663
664     auto policy = convertSetRequestHandlerPolicy(env, policyObj);
665     VERIFY_NO_EXC(env);
666
667     res->setSetRequestHandlerPolicy(policy);
668 }
669
670 JNIEXPORT jobject JNICALL
671 Java_org_iotivity_service_server_RcsResourceObject_nativeGetSetRequestHandlerPolicy
672 (JNIEnv* env, jobject obj)
673 {
674     LOGD("nativeGetSetRequestHandlerPolicy");
675
676     auto res = getResource(env, obj);
677     VERIFY_NO_EXC_RET_DEF(env);
678
679     return convertSetRequestHandlerPolicy(env, res->getSetRequestHandlerPolicy());
680 }