1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 "JniNotificationProvider.h"
22 #include "NSProviderService.h"
24 static JavaVM *g_jvm = NULL;
26 static jobject g_obj_subscriptionListener = NULL;
27 static jobject g_obj_syncListener = NULL;
30 jclass g_cls_Message_Type;
31 jclass g_cls_Consumer;
32 jclass g_cls_SyncInfo;
33 jclass g_cls_SyncType;
34 jclass g_cls_MediaContents;
36 jclass g_cls_TopicsList;
37 jclass g_cls_TopicState;
39 static JNIEnv *GetJNIEnv(jint *ret)
43 *ret = g_jvm->GetEnv((void **) &env, JNI_CURRENT_VERSION);
49 if (g_jvm->AttachCurrentThread(&env, NULL) != JNI_OK)
51 LOGE ("Failed to get the environment");
59 LOGE ("JNI version is not supported");
61 LOGE ("Failed to get the environment");
66 OIC::Service::NSMessage *getNativeMessage(JNIEnv *env, jobject jMsg)
68 LOGD("JNIProviderService: getMessage - IN");
70 jclass cls = env->GetObjectClass( jMsg);
73 jclass cls_messageType = (jclass) (env->NewLocalRef(g_cls_Message_Type));
76 LOGE ("Failed to Get ObjectClass for Message Type");
79 jmethodID mid = env->GetMethodID(cls_messageType, "ordinal", "()I");
80 jfieldID fid_type = env->GetFieldID( cls, "mType",
81 "Lorg/iotivity/service/ns/common/Message$MessageType;");
84 LOGE("Error: jfieldID for message type is null");
87 jobject jobj = env->GetObjectField( jMsg, fid_type);
90 LOGE("Error: object of field Message Type is null");
93 jint jtype = env->CallIntMethod(jobj, mid);
94 OIC::Service::NSMessage::NSMessageType type = (OIC::Service::NSMessage::NSMessageType) jtype;
96 LOGD("Message Type: %ld\n", (long )type);
99 jfieldID fid_tm = env->GetFieldID( cls, "mTime", "Ljava/lang/String;");
102 LOGE("Error: jfieldID for message time is null");
105 jstring jtime = (jstring)env->GetObjectField( jMsg, fid_tm);
106 const char *time = "";
109 time = env->GetStringUTFChars( jtime, NULL);
113 LOGD("Info: messageTitle is null");
115 LOGD("Message Time: %s\n", time);
118 jfieldID fid_ttl = env->GetFieldID( cls, "mTTL", "J");
121 LOGE("Error: jfieldID for message ttl is null");
124 jlong jttl = (jlong) env->GetObjectField( jMsg, fid_ttl);
127 LOGD("Message ID: %lld\n", ttl);
130 jfieldID fid_title = env->GetFieldID( cls, "mTitle", "Ljava/lang/String;");
131 if (fid_title == NULL)
133 LOGE("Error: jfieldID for message title is null");
136 jstring jmsgTitle = (jstring)env->GetObjectField( jMsg, fid_title);
137 const char *messageTitle = "";
140 messageTitle = env->GetStringUTFChars( jmsgTitle, NULL);
144 LOGD("Info: messageTitle is null");
146 LOGD("Message Title: %s\n", messageTitle);
148 // Message Content Text
149 jfieldID fid_body = env->GetFieldID( cls, "mContentText", "Ljava/lang/String;");
150 if (fid_body == NULL)
152 LOGE("Error: jfieldID for message context Text is null");
155 jstring jmsgBody = (jstring)env->GetObjectField( jMsg, fid_body);
156 const char *messageBody = "";
159 messageBody = env->GetStringUTFChars( jmsgBody, NULL);
163 LOGD("Info: messageBody is null");
165 LOGD("Message Body: %s\n", messageBody);
168 jfieldID fid_source = env->GetFieldID( cls, "mSourceName", "Ljava/lang/String;");
169 if (fid_source == NULL)
171 LOGE("Error: jfieldID for message source is null");
174 jstring jmsgSource = (jstring)env->GetObjectField( jMsg, fid_source);
175 const char *messageSource = "";
178 messageSource = env->GetStringUTFChars( jmsgSource, NULL);
182 LOGD("Info: messageSource is null");
184 LOGD("Message Source: %s\n", messageSource);
186 // Message MediaContents
187 jfieldID fid_media = env->GetFieldID( cls, "mMediaContents",
188 "Lorg/iotivity/service/ns/common/MediaContents;");
189 if (fid_media == NULL)
191 LOGE("Error: jfieldID for MediaContents is null");
194 jobject jmedia = env->GetObjectField( jMsg, fid_media);
197 LOGE("Error: jmedia object of MediaContents inside Message is null");
200 jclass cls_MediaContents = (jclass) (env->NewLocalRef(g_cls_MediaContents));
201 if (!cls_MediaContents)
203 LOGE ("Failed to Get ObjectClass for class MediaContents");
206 jfieldID fid_icon = env->GetFieldID( cls_MediaContents, "mIconImage", "Ljava/lang/String;");
207 if (fid_icon == NULL)
209 LOGE("Error: jfieldID for iconImage is null");
212 jstring jiconImage = (jstring)env->GetObjectField( jmedia, fid_icon);
213 const char *iconImage = "";
216 iconImage = env->GetStringUTFChars( jiconImage, NULL);
220 LOGD("Info: iconImage is null");
223 LOGD("iconImage: %s\n", iconImage);
225 OIC::Service::NSMediaContents *media = new OIC::Service::NSMediaContents(std::string(iconImage));
226 OIC::Service::NSMessage *nsMsg = OIC::Service::NSProviderService::getInstance()->CreateMessage();
228 nsMsg->setType(type);
229 nsMsg->setTime(std::string(time));
231 nsMsg->setTitle(std::string(messageTitle));
232 nsMsg->setContentText(std::string(messageBody));
233 nsMsg->setSourceName(std::string(messageSource));
234 nsMsg->setMediaContents(media);
236 env->DeleteLocalRef(cls_messageType);
237 env->DeleteLocalRef(cls_MediaContents);
241 env->ReleaseStringUTFChars(jtime, time);
245 env->ReleaseStringUTFChars(jmsgTitle, messageTitle);
249 env->ReleaseStringUTFChars(jmsgBody, messageBody);
253 env->ReleaseStringUTFChars(jmsgSource, messageSource);
257 env->ReleaseStringUTFChars(jiconImage, iconImage);
260 LOGD("JNIProviderService: getMessage - OUT");
265 jobject getJavaState(JNIEnv *env, OIC::Service::NSTopic::NSTopicState nsState)
267 LOGD("JNIProviderService: getJavaState - IN");
270 jclass cls_topicState = (jclass) (env->NewLocalRef(g_cls_TopicState));
273 LOGE ("Failed to Get ObjectClass for TopicState Type");
277 jobject obj_topicState;
280 case OIC::Service::NSTopic::NSTopicState::UNSUBSCRIBED:
282 static jfieldID fieldID = env->GetStaticFieldID(cls_topicState,
283 "UNSUBSCRIBED", "Lorg/iotivity/service/ns/common/Topic$TopicState;");
284 obj_topicState = env->GetStaticObjectField(cls_topicState, fieldID);
286 case OIC::Service::NSTopic::NSTopicState::SUBSCRIBED:
288 static jfieldID fieldID = env->GetStaticFieldID(cls_topicState,
289 "SUBSCRIBED", "Lorg/iotivity/service/ns/common/Topic$TopicState;");
290 obj_topicState = env->GetStaticObjectField(cls_topicState, fieldID);
294 if (obj_topicState == NULL)
296 LOGE("Error: object of field TopicState is null");
299 env->DeleteLocalRef(cls_topicState);
300 LOGD("JNIProviderService: getJavaState - OUT");
301 return obj_topicState;
304 jobject getJavaTopicsList(JNIEnv *env, OIC::Service::NSTopicsList *topicList)
306 LOGD("JNIProviderService: getJavaTopicsList - IN");
307 jclass cls_topicList = (jclass) (env->NewLocalRef(g_cls_TopicsList));
310 LOGE ("Failed to Get ObjectClass for TopicsList");
313 jmethodID mid_topicList = env->GetMethodID(cls_topicList, "<init>", "()V");
316 LOGE ("Failed to Get MethodID for TopicsList<init>");
319 jobject obj_topicList = env->NewObject(cls_topicList, mid_topicList);
320 jmethodID mid_addTopic =
321 env->GetMethodID(cls_topicList, "addTopic", "(Lorg/iotivity/service/ns/common/Topic;)V");
322 for (auto it : topicList->getTopicsList())
324 jobject jState = getJavaState(env, it->getState());
325 std::string topicName = it->getTopicName();
326 jstring jTopicName = env->NewStringUTF(topicName.c_str());
327 env->CallVoidMethod(obj_topicList, mid_addTopic, jTopicName, jState);
329 env->DeleteLocalRef(cls_topicList);
330 LOGD("JNIProviderService: getJavaTopicsList - OUT");
331 return obj_topicList;
334 void onSubscribeListenerCb(OIC::Service::NSConsumer *consumer)
336 LOGD("JNIProviderService_onSubscribeListenerCb - IN");
339 JNIEnv *env = GetJNIEnv(&envRet);
340 if (NULL == env) return ;
342 jobject jSubscriptionListener = (jobject) env->NewLocalRef(g_obj_subscriptionListener);
343 if (!jSubscriptionListener)
345 LOGE ("Failed to Get jSubscriptionListener");
346 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
350 LOGD("consumer ID : %s\n", consumer->getConsumerId().c_str());
352 jstring jConsumerId = env->NewStringUTF( consumer->getConsumerId().c_str());
354 jclass cls_consumer = (jclass) (env->NewLocalRef(g_cls_Consumer));
357 LOGE ("Failed to Get ObjectClass for Consumer");
358 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
362 jmethodID mid_consumer = env->GetMethodID(
365 "(JLjava/lang/String;Lorg/iotivity/service/ns/provider/Consumer)V");
368 LOGE ("Failed to Get MethodID for Consumer<init>");
369 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
372 jobject obj_consumer = env->NewObject( cls_consumer, mid_consumer, jConsumerId);
374 jclass cls = env->GetObjectClass( jSubscriptionListener);
377 LOGE("Failed to Get ObjectClass of jSubscriptionListener");
378 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
381 jmethodID mid = env->GetMethodID(
383 "onConsumerSubscribed",
384 "(Lorg/iotivity/service/ns/provider/Consumer;)V");
387 LOGE("Failed to Get MethodID of onConsumerSubscribed");
388 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
392 env->CallVoidMethod( jSubscriptionListener, mid, obj_consumer);
393 env->DeleteLocalRef(jSubscriptionListener);
394 env->DeleteLocalRef(cls_consumer);
395 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
396 LOGD("JNIProviderService_onSubscribeListenerCb - OUT");
400 void onSyncInfoListenerCb(OIC::Service::NSSyncInfo *sync)
402 LOGD("JNIProviderService_onSyncInfoListenerCb - IN");
405 JNIEnv *env = GetJNIEnv(&envRet);
406 if (NULL == env) return ;
408 jobject jSyncListener = (jobject) env->NewLocalRef(g_obj_syncListener);
411 LOGE ("Failed to Get jSyncListener");
412 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
416 LOGD("Sync ID : %ld\n", (long) sync->getMessageId());
417 LOGD("Sync STATE : %d\n", (int) sync->getState());
419 jlong jMessageId = (long) sync->getMessageId();
420 jstring jProviderId = env->NewStringUTF(sync->getProviderId().c_str());
423 jclass cls_SyncType = (jclass) (env->NewLocalRef(g_cls_SyncType));
426 LOGE ("Failed to Get ObjectClass for SyncType");
427 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
430 switch (sync->getState())
432 case OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_UNREAD:
434 static jfieldID fieldID = env->GetStaticFieldID(cls_SyncType,
435 "UNREAD", "Lorg/iotivity/service/ns/common/SyncInfo$SyncType;");
436 syncType = env->GetStaticObjectField(cls_SyncType, fieldID);
438 case OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ :
440 static jfieldID fieldID = env->GetStaticFieldID(cls_SyncType,
441 "READ", "Lorg/iotivity/service/ns/common/SyncInfo$SyncType;");
442 syncType = env->GetStaticObjectField(cls_SyncType, fieldID);
444 case OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED :
446 static jfieldID fieldID = env->GetStaticFieldID(cls_SyncType,
447 "DELETED", "Lorg/iotivity/service/ns/common/SyncInfo$SyncType;");
448 syncType = env->GetStaticObjectField(cls_SyncType, fieldID);
453 jclass cls_SyncInfo = (jclass) (env->NewLocalRef(g_cls_SyncInfo));
456 LOGE ("Failed to Get ObjectClass for SyncInfo");
457 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
460 jmethodID mid_syncInfo = env->GetMethodID(
463 "(JLjava/lang/String;Lorg/iotivity/service/ns/common/SyncInfo$SyncType)V");
466 LOGE ("Failed to Get MethodID for SyncInfo");
467 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
471 jobject obj_syncInfo = env->NewObject( cls_SyncInfo, mid_syncInfo, jMessageId, jProviderId,
474 jclass cls = env->GetObjectClass( jSyncListener);
477 LOGE("Failed to Get ObjectClass");
478 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
481 jmethodID mid = env->GetMethodID( cls, "onMessageSynchronized",
482 "(Lorg/iotivity/service/ns/common/SyncInfo)V");
485 LOGE("Failed to Get MethodID");
486 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
489 env->CallVoidMethod( jSyncListener, mid, obj_syncInfo);
491 env->DeleteLocalRef(jSyncListener);
492 env->DeleteLocalRef(cls_SyncInfo);
493 env->DeleteLocalRef(cls_SyncType);
494 if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
496 LOGD("JNIProviderService: OnSyncInfoListenerCb - OUT");
501 JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeStart(
502 JNIEnv *env, jobject jObj, jboolean jPolicy, jobject jSubscriptionListener,
503 jobject jSyncListener)
505 LOGD("JNIProviderService: nativeStart - IN");
506 if (!jSubscriptionListener || !jSyncListener)
508 LOGE("Fail to set listeners");
509 ThrowNSException(NS_ERROR, "Listener cannot be null");
510 return (jint) OIC::Service::NSResult::ERROR;
513 if (g_obj_subscriptionListener != NULL)
515 env->DeleteGlobalRef(g_obj_subscriptionListener);
517 if (g_obj_syncListener != NULL)
519 env->DeleteGlobalRef(g_obj_syncListener);
522 g_obj_subscriptionListener = (jobject) env->NewGlobalRef(jSubscriptionListener);
523 g_obj_syncListener = (jobject) env->NewGlobalRef(jSyncListener);
525 // check access policy
527 OIC::Service::NSProviderService::ProviderConfig cfg;
528 cfg.m_subscribeRequestCb = onSubscribeListenerCb;
529 cfg.m_syncInfoCb = onSyncInfoListenerCb;
530 cfg.policy = (bool) jPolicy;
532 OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->Start(cfg);
533 if (result != OIC::Service::NSResult::OK)
535 LOGE("Fail to start NSProviderService");
539 LOGD("JNIProviderService: nativeStart - OUT");
540 return (jint) result;
543 JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeStop(
544 JNIEnv *env, jobject jObj)
546 LOGD("JNIProviderService: nativeStop - IN");
548 OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->Stop();
549 if (result != OIC::Service::NSResult::OK)
551 LOGD("Fail to stop NSProvider service");
552 return (jint) result;
555 env->DeleteGlobalRef( g_obj_subscriptionListener);
556 env->DeleteGlobalRef( g_obj_syncListener);
557 g_obj_subscriptionListener = NULL;
558 g_obj_syncListener = NULL;
560 LOGD("JNIProviderService: nativeStop - OUT");
561 return (jint) result;
564 JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeSendMessage(
565 JNIEnv *env, jobject jObj, jobject jMsg)
567 LOGD("JNIProviderService: nativeSendMessage - IN");
570 LOGD("Fail to send notification - Message is null");
571 ThrowNSException(NS_ERROR, "Message cannot be null");
572 return (jint) OIC::Service::NSResult::ERROR;
574 OIC::Service::NSMessage *nsMsg = getNativeMessage(env, jMsg);
575 if (nsMsg == nullptr)
577 ThrowNSException(NS_ERROR, "Message didn't have a field ID ");
578 return (jint) OIC::Service::NSResult::ERROR;
581 OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->SendMessage(nsMsg);
582 if (result != OIC::Service::NSResult::OK)
584 LOGD("Fail to send NSProvider Message");
586 LOGD("JNIProviderService: nativeSendMessage - OUT");
587 return (jint) result;
590 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeSendSyncInfo(
591 JNIEnv *env, jobject jObj, jlong messageId , jint syncState)
593 LOGD("JNIProviderService: nativeSendSyncInfo - IN");
594 OIC::Service::NSProviderService::getInstance()->SendSyncInfo( messageId,
595 (OIC::Service::NSSyncInfo::NSSyncType) syncState);
596 LOGD("JNIProviderService: nativeSendSyncInfo - OUT");
600 JNIEXPORT jint JNICALL
601 Java_org_iotivity_service_ns_provider_ProviderService_nativeEnableRemoteService(JNIEnv *env,
602 jobject jObj, jstring jstr)
604 LOGD("JNIProviderService: nativeEnableRemoteService - IN");
607 ThrowNSException(NS_ERROR, "Server Address Can't be NULL");
608 return (jint) OIC::Service::NSResult::ERROR;
611 const char *address = env->GetStringUTFChars( jstr, NULL);
612 std::string servAddress(address);
613 OIC::Service::NSResult result =
614 OIC::Service::NSProviderService::getInstance()->EnableRemoteService(
616 if (result != OIC::Service::NSResult::OK)
618 LOGE("Fail to Enable Remote Service");
620 env->ReleaseStringUTFChars(jstr, address);
621 LOGD("JNIProviderService: nativeEnableRemoteService - OUT");
622 return (jint) result;
625 JNIEXPORT jint JNICALL
626 Java_org_iotivity_service_ns_provider_ProviderService_nativeDisableRemoteService(JNIEnv *env,
627 jobject jObj, jstring jstr)
629 LOGD("JNIProviderService: nativeDisableRemoteService - IN");
632 ThrowNSException(NS_ERROR, "Server Address Can't be NULL");
633 return (jint) OIC::Service::NSResult::ERROR;
636 const char *address = env->GetStringUTFChars( jstr, NULL);
637 std::string servAddress(address);
638 OIC::Service::NSResult result =
639 OIC::Service::NSProviderService::getInstance()->DisableRemoteService(
641 if (result != OIC::Service::NSResult::OK)
643 LOGE("Fail to Disable Remote Service");
645 env->ReleaseStringUTFChars(jstr, address);
646 LOGD("JNIProviderService: nativeDisableRemoteService - OUT");
647 return (jint) result;
650 JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeAddTopic
651 (JNIEnv *env, jobject jObj, jstring jTopicName)
653 LOGD("JNIProviderService: nativeAddTopic - IN");
656 ThrowNSException(NS_ERROR, "Topic Name Can't be NULL");
657 return (jint) OIC::Service::NSResult::ERROR;
659 const char *name = env->GetStringUTFChars( jTopicName, NULL);
660 std::string topicName(name);
661 OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->AddTopic(
663 if (result != OIC::Service::NSResult::OK)
665 LOGE("Fail to Add Topic");
667 env->ReleaseStringUTFChars(jTopicName, name);
668 LOGD("JNIProviderService: nativeAddTopic - OUT");
669 return (jint) result;
671 JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeDeleteTopic
672 (JNIEnv *env, jobject jObj, jstring jTopicName)
674 LOGD("JNIProviderService: nativeDeleteTopic - IN");
677 ThrowNSException(NS_ERROR, "Topic Name Can't be NULL");
678 return (jint) OIC::Service::NSResult::ERROR;
680 const char *name = env->GetStringUTFChars( jTopicName, NULL);
681 std::string topicName(name);
682 OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->DeleteTopic(
684 if (result != OIC::Service::NSResult::OK)
686 LOGE("Fail to Add Topic");
688 env->ReleaseStringUTFChars(jTopicName, name);
689 LOGD("JNIProviderService: nativeDeleteTopic - OUT");
690 return (jint) result;
693 JNIEXPORT jobject JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeGetTopics
694 (JNIEnv *env, jobject jObj)
696 LOGD("JNIProviderService: nativeGetTopics - IN");
698 OIC::Service::NSTopicsList *topicList =
699 OIC::Service::NSProviderService::getInstance()->GetTopics();
700 if (topicList == nullptr)
702 ThrowNSException(NS_ERROR, "Topic List doesn't exist");
706 jobject obj_topicList = getJavaTopicsList(env, topicList);
708 LOGD("JNIProviderService: nativeGetTopics - OUT");
709 return obj_topicList;
712 JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeAcceptSubscription(
713 JNIEnv *env, jobject jObj, jobject jConsumer, jboolean jAccepted)
715 LOGD("JNIProviderService: nativeAcceptSubscription - IN");
717 jclass consumerClass = env->GetObjectClass( jConsumer);
720 ThrowNSException(NS_ERROR, "Failed to Get ObjectClass for Consumer");
721 return (jint) OIC::Service::NSResult::ERROR;
725 jfieldID fid_id = env->GetFieldID(consumerClass, "mConsumerId", "Ljava/lang/String;");
728 LOGE("Error: jfieldID for mConsumerId is null");
729 ThrowNSException(NS_ERROR, "ConsumerId not found");
730 return (jint) OIC::Service::NSResult::ERROR;
733 jstring jconId = (jstring)env->GetObjectField( jConsumer, fid_id);
736 ThrowNSException(NS_ERROR, "ProviderId cannot be null");
737 return (jint) OIC::Service::NSResult::ERROR;;
739 const char *conId = env->GetStringUTFChars( jconId, NULL);
740 std::string consumerId(conId);
741 env->ReleaseStringUTFChars(jconId, conId);
743 LOGD("Consumer ID: %s\n", consumerId.c_str());
745 OIC::Service::NSConsumer *consumer =
746 OIC::Service::NSProviderService::getInstance()->getConsumer(consumerId);
747 int result = consumer->acceptSubscription(consumer, (bool)jAccepted);
750 LOGD("Subscription Accepted");
754 LOGD("Subscription Denied");
757 LOGD("JNIProviderService: nativeAcceptSubscription - OUT");
760 JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeSelectTopic
761 (JNIEnv *env, jobject jObj, jstring jConsumerId, jstring jTopicName)
763 LOGD("JNIProviderService: nativeSelectTopic - IN");
764 if (!jConsumerId || !jTopicName)
766 ThrowNSException(NS_ERROR, "Topic Name or ConsumerId Can't be NULL");
767 return (jint) OIC::Service::NSResult::ERROR;
769 const char *name = env->GetStringUTFChars( jTopicName, NULL);
770 const char *id = env->GetStringUTFChars( jConsumerId, NULL);
771 std::string topicName(name);
772 std::string consumerId(id);
773 OIC::Service::NSConsumer *nsConsumer =
774 OIC::Service::NSProviderService::getInstance()->getConsumer(consumerId);
777 ThrowNSException(NS_ERROR, "Consumer does exists");
778 return (jint) OIC::Service::NSResult::ERROR;
780 OIC::Service::NSResult result = nsConsumer->selectTopic(topicName);
782 if (result != OIC::Service::NSResult::OK)
784 LOGD("Fail to Select Topic");
786 env->ReleaseStringUTFChars(jTopicName, name);
787 env->ReleaseStringUTFChars(jConsumerId, id);
788 LOGD("JNIProviderService: nativeSelectTopic - OUT");
789 return (jint) result;
791 JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeUnselectTopic
792 (JNIEnv *env, jobject jObj, jstring jConsumerId, jstring jTopicName)
794 LOGD("JNIProviderService: nativeUnselectTopic - IN");
795 if (!jConsumerId || !jTopicName)
797 ThrowNSException(NS_ERROR, "Topic Name or ConsumerId Can't be NULL");
798 return (jint) OIC::Service::NSResult::ERROR;
800 const char *name = env->GetStringUTFChars( jTopicName, NULL);
801 const char *id = env->GetStringUTFChars( jConsumerId, NULL);
802 std::string topicName(name);
803 std::string consumerId(id);
804 OIC::Service::NSConsumer *nsConsumer =
805 OIC::Service::NSProviderService::getInstance()->getConsumer(consumerId);
808 ThrowNSException(NS_ERROR, "Consumer does exists");
809 return (jint) OIC::Service::NSResult::ERROR;
811 OIC::Service::NSResult result = nsConsumer->unselectTopic(topicName);
813 if (result != OIC::Service::NSResult::OK)
815 LOGE("Fail to Unselect Topic");
817 env->ReleaseStringUTFChars(jTopicName, name);
818 env->ReleaseStringUTFChars(jConsumerId, id);
819 LOGD("JNIProviderService: nativeUnselectTopic - OUT");
820 return (jint) result;
823 JNIEXPORT jobject JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeGetConsumerTopics
824 (JNIEnv *env, jobject jObj, jstring jConsumerId)
826 LOGD("JNIProviderService: nativeGetConsumerTopics - IN");
829 ThrowNSException(NS_ERROR, "Topic Name or ConsumerId Can't be NULL");
832 const char *id = env->GetStringUTFChars( jConsumerId, NULL);
833 std::string consumerId(id);
834 OIC::Service::NSConsumer *nsConsumer =
835 OIC::Service::NSProviderService::getInstance()->getConsumer(consumerId);
838 ThrowNSException(NS_ERROR, "Consumer does exists");
841 OIC::Service::NSTopicsList *topicList = nsConsumer->getConsumerTopics();
842 if (topicList == nullptr)
844 ThrowNSException(NS_ERROR, "Topic List doesn't exist");
847 jobject obj_topicList = getJavaTopicsList(env, topicList);
849 env->ReleaseStringUTFChars(jConsumerId, id);
850 LOGD("JNIProviderService: nativeGetConsumerTopics - OUT");
851 return obj_topicList;
855 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved)
857 LOGD("ProviderService_JNI_OnLoad");
861 if (jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
863 LOGE("Failed to get the environment using GetEnv()");
867 jclass localMessage = env->FindClass(
868 "org/iotivity/service/ns/common/Message");
871 LOGE("Failed to get local Message class");
874 g_cls_Message = (jclass) (env->NewGlobalRef(localMessage));
877 LOGE("Failed to set Global Message reference");
881 jclass localMessageType = env->FindClass(
882 "org/iotivity/service/ns/common/Message$MessageType");
883 if (!localMessageType)
885 LOGE("Failed to get local Message Type class");
888 g_cls_Message_Type = (jclass) (env->NewGlobalRef(localMessageType));
889 if (!g_cls_Message_Type)
891 LOGE("Failed to set Global Message Type reference");
895 jclass localConsumer = env->FindClass(
896 "org/iotivity/service/ns/provider/Consumer");
899 LOGE("Failed to get local Provider class");
902 g_cls_Consumer = (jclass) (env->NewGlobalRef(localConsumer));
905 LOGE("Failed to set Global Provider reference");
909 jclass localSyncInfo = env->FindClass(
910 "org/iotivity/service/ns/common/SyncInfo");
913 LOGE("Failed to get local SyncInfo class");
916 g_cls_SyncInfo = (jclass) (env->NewGlobalRef(localSyncInfo));
919 LOGE("Failed to set Global SyncInfo reference");
923 jclass localSyncType = env->FindClass(
924 "org/iotivity/service/ns/common/SyncInfo$SyncType");
927 LOGE("Failed to get local SyncType enum");
930 g_cls_SyncType = (jclass) (env->NewGlobalRef(localSyncType));
933 LOGE("Failed to set Global SyncType reference");
937 jclass localMediaContents = env->FindClass(
938 "org/iotivity/service/ns/common/MediaContents");
939 if (!localMediaContents)
941 LOGE("Failed to get local MediaContents class");
944 g_cls_MediaContents = (jclass) (env->NewGlobalRef(localMediaContents));
945 if (!g_cls_MediaContents)
947 LOGE("Failed to set Global MediaContents reference");
951 jclass localTopic = env->FindClass(
952 "org/iotivity/service/ns/common/Topic");
955 LOGE("Failed to get local Topic class");
958 g_cls_Topic = (jclass) (env->NewGlobalRef(localTopic));
961 LOGE("Failed to set Global Topic reference");
965 jclass localTopicsList = env->FindClass(
966 "org/iotivity/service/ns/common/TopicsList");
967 if (!localTopicsList)
969 LOGE("Failed to get local Topic class");
972 g_cls_TopicsList = (jclass) (env->NewGlobalRef(localTopicsList));
973 if (!g_cls_TopicsList)
975 LOGE("Failed to set Global TopicsList reference");
979 jclass localTopicState = env->FindClass(
980 "org/iotivity/service/ns/common/Topic$TopicState");
981 if (!localTopicState)
983 LOGE("Failed to get local TopicState enum");
986 g_cls_TopicState = (jclass) (env->NewGlobalRef(localTopicState));
987 if (!g_cls_TopicState)
989 LOGE("Failed to set Global TopicState reference");
993 env->DeleteLocalRef(localMessage);
994 env->DeleteLocalRef(localMessageType);
995 env->DeleteLocalRef(localConsumer);
996 env->DeleteLocalRef(localSyncInfo);
997 env->DeleteLocalRef(localSyncType);
998 env->DeleteLocalRef(localMediaContents);
999 env->DeleteLocalRef(localTopic);
1000 env->DeleteLocalRef(localTopicsList);
1001 env->DeleteLocalRef(localTopicState);
1003 return NSExceptionInit(env);
1006 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserved)
1008 LOGD("ProviderService_JNI_OnUnload");
1011 if (jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
1013 LOGE("Failed to get the environment using GetEnv()");
1017 env->DeleteGlobalRef(g_cls_Message);
1018 env->DeleteGlobalRef(g_cls_Consumer);
1019 env->DeleteGlobalRef(g_cls_SyncInfo);
1020 env->DeleteGlobalRef(g_cls_SyncType);
1021 env->DeleteGlobalRef(g_cls_MediaContents);
1022 env->DeleteGlobalRef(g_cls_Message_Type);
1023 env->DeleteGlobalRef(g_cls_Topic);
1024 env->DeleteGlobalRef(g_cls_TopicsList);
1025 env->DeleteGlobalRef(g_cls_TopicState);