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"
23 #include "JniOcRepresentation.h"
25 static JavaVM *g_jvm_provider = NULL;
27 static jobject g_obj_subscriptionListener = NULL;
28 static jobject g_obj_syncListener = NULL;
31 jclass g_cls_Message_Type;
32 jclass g_cls_Consumer;
33 jclass g_cls_SyncInfo;
34 jclass g_cls_SyncType;
35 jclass g_cls_MediaContents;
37 jclass g_cls_TopicsList;
38 jclass g_cls_TopicState;
39 jclass g_cls_OcRepresentation;
40 jmethodID g_mid_OcRepresentation_N_ctor_bool = NULL;
42 static JNIEnv *GetJNIEnv(jint *ret)
46 *ret = g_jvm_provider->GetEnv((void **) &env, JNI_CURRENT_VERSION);
52 if (g_jvm_provider->AttachCurrentThread(&env, NULL) != JNI_OK)
54 NS_LOGE ("Failed to get the environment");
62 NS_LOGE ("JNI version is not supported");
65 NS_LOGE ("Failed to get the environment");
70 OIC::Service::NSMessage getNativeMessage(JNIEnv *env, jobject jMsg)
72 NS_LOGD ("JNIProviderService: getMessage - IN");
74 jclass cls = env->GetObjectClass( jMsg);
77 jclass cls_messageType = (jclass) (env->NewLocalRef(g_cls_Message_Type));
80 ThrowNSException(JNI_INVALID_VALUE, "Failed to Get ObjectClass for Message Type");
83 jmethodID mid = env->GetMethodID(cls_messageType, "ordinal", "()I");
84 jfieldID fid_type = env->GetFieldID( cls, "mType",
85 "Lorg/iotivity/service/ns/common/Message$MessageType;");
88 ThrowNSException(JNI_INVALID_VALUE, "Error: jfieldID for message type is null");
91 jobject jobj = env->GetObjectField( jMsg, fid_type);
94 ThrowNSException(JNI_INVALID_VALUE, "Error: object of field Message Type is null");
97 jint jtype = env->CallIntMethod(jobj, mid);
98 OIC::Service::NSMessage::NSMessageType type = (OIC::Service::NSMessage::NSMessageType) jtype;
100 NS_LOGD ("Message Type: %ld\n", (long )type);
103 jfieldID fid_tm = env->GetFieldID( cls, "mTime", "Ljava/lang/String;");
106 ThrowNSException(JNI_INVALID_VALUE, "Error: jfieldID for message time is null");
109 jstring jtime = (jstring)env->GetObjectField( jMsg, fid_tm);
110 const char *time = "";
113 time = env->GetStringUTFChars( jtime, NULL);
117 NS_LOGD ("Info: messageTitle is null");
119 NS_LOGD ("Message Time: %s\n", time);
122 jfieldID fid_ttl = env->GetFieldID( cls, "mTTL", "J");
125 ThrowNSException(JNI_INVALID_VALUE, "Error: jfieldID for message ttl is null");
129 jlong jttl = (jlong) env->GetLongField( jMsg, fid_ttl);
131 NS_LOGD ("TTL: %lld\n", ttl);
134 jfieldID fid_title = env->GetFieldID( cls, "mTitle", "Ljava/lang/String;");
135 if (fid_title == NULL)
137 ThrowNSException(JNI_INVALID_VALUE, "Error: jfieldID for message title is null");
140 jstring jmsgTitle = (jstring)env->GetObjectField( jMsg, fid_title);
141 const char *messageTitle = "";
144 messageTitle = env->GetStringUTFChars( jmsgTitle, NULL);
148 NS_LOGD ("Info: messageTitle is null");
150 NS_LOGD ("Message Title: %s\n", messageTitle);
152 // Message Content Text
153 jfieldID fid_body = env->GetFieldID( cls, "mContentText", "Ljava/lang/String;");
154 if (fid_body == NULL)
156 ThrowNSException(JNI_INVALID_VALUE, "Error: jfieldID for message context Text is null");
159 jstring jmsgBody = (jstring)env->GetObjectField( jMsg, fid_body);
160 const char *messageBody = "";
163 messageBody = env->GetStringUTFChars( jmsgBody, NULL);
167 NS_LOGD ("Info: messageBody is null");
169 NS_LOGD ("Message Body: %s\n", messageBody);
172 jfieldID fid_source = env->GetFieldID( cls, "mSourceName", "Ljava/lang/String;");
173 if (fid_source == NULL)
175 ThrowNSException(JNI_INVALID_VALUE, "Error: jfieldID for message source is null");
178 jstring jmsgSource = (jstring)env->GetObjectField( jMsg, fid_source);
179 const char *messageSource = "";
182 messageSource = env->GetStringUTFChars( jmsgSource, NULL);
186 NS_LOGD ("Info: messageSource is null");
188 NS_LOGD ("Message Source: %s\n", messageSource);
191 jfieldID fid_topic = env->GetFieldID( cls, "mTopic", "Ljava/lang/String;");
192 if (fid_topic == NULL)
194 ThrowNSException(JNI_INVALID_VALUE, "Error: jfieldID for topic is null");
197 jstring jtopic = (jstring)env->GetObjectField( jMsg, fid_topic);
198 const char *topic = "";
201 topic = env->GetStringUTFChars( jtopic, NULL);
205 NS_LOGD ("Info: topic is null");
207 NS_LOGD ("Topic : %s\n", topic);
209 // Message MediaContents
210 jfieldID fid_media = env->GetFieldID( cls, "mMediaContents",
211 "Lorg/iotivity/service/ns/common/MediaContents;");
212 if (fid_media == NULL)
214 ThrowNSException(JNI_INVALID_VALUE, "Error: jfieldID for MediaContents is null");
217 jobject jmedia = env->GetObjectField( jMsg, fid_media);
218 OIC::Service::NSMediaContents *media = nullptr;
221 NS_LOGD ("Info: jmedia object of MediaContents inside Message is null");
225 jclass cls_MediaContents = (jclass) (env->NewLocalRef(g_cls_MediaContents));
226 if (!cls_MediaContents)
228 ThrowNSException(JNI_INVALID_VALUE, "Failed to Get ObjectClass for class MediaContents");
231 jfieldID fid_icon = env->GetFieldID( cls_MediaContents, "mIconImage", "Ljava/lang/String;");
232 if (fid_icon == NULL)
234 ThrowNSException(JNI_INVALID_VALUE, "Error: jfieldID for iconImage is null");
237 jstring jiconImage = (jstring)env->GetObjectField( jmedia, fid_icon);
238 const char *iconImage = "";
241 iconImage = env->GetStringUTFChars( jiconImage, NULL);
242 media = new OIC::Service::NSMediaContents(std::string(iconImage));
243 env->ReleaseStringUTFChars(jiconImage, iconImage);
247 NS_LOGD ("Info: iconImage is null");
249 env->DeleteLocalRef(cls_MediaContents);
250 NS_LOGD ("iconImage: %s\n", iconImage);
254 jfieldID fid_extraInfo = env->GetFieldID( cls, "mExtraInfo",
255 "Lorg/iotivity/base/OcRepresentation;");
256 if (fid_extraInfo == NULL)
258 ThrowNSException(JNI_INVALID_VALUE, "Error: jfieldID for mExtraInfo is null");
261 jobject jExtraInfo = env->GetObjectField( jMsg, fid_extraInfo);
262 OC::OCRepresentation *representation = nullptr;
263 if (jExtraInfo == NULL)
265 NS_LOGE ("Error: jExtraInfo object of Message is null");
269 representation = GetHandle<OC::OCRepresentation>(env, jExtraInfo);
270 if (env->ExceptionCheck())
272 NS_LOGE ("Failed to get native handle from OcRepresentation");
276 NS_LOGE ("Failed to get native object OcRepresentation");
281 jfieldID fid_mid = env->GetFieldID( cls, "mMessageId", "J");
284 ThrowNSException(JNI_INVALID_VALUE, "Error: jfieldID for message mMessageId is null");
288 jlong jId = (jlong) env->GetLongField( jMsg, fid_mid);
290 NS_LOGD ("Message Id: %lld\n", jmId);
293 jfieldID fid_pid = env->GetFieldID( cls, "mProviderId", "Ljava/lang/String;");
296 ThrowNSException(JNI_INVALID_VALUE, "Error: jfieldID for message mProviderId is null");
299 jstring jProId = (jstring)env->GetObjectField( jMsg, fid_pid);
300 const char *jpId = "";
303 jpId = env->GetStringUTFChars( jProId, NULL);
307 NS_LOGD ("Info: messageSource is null");
309 NS_LOGD ("Provider Id : %s\n", jpId);
312 NSMessage *mesg = new NSMessage;
313 mesg->messageId = jmId;
314 mesg->providerId[0] = '\0';
315 strncat(mesg->providerId, jpId, NS_UTILS_UUID_STRING_SIZE - 1);
317 mesg->dateTime = nullptr;
318 mesg->title = nullptr;
319 mesg->contentText = nullptr;
320 mesg->sourceName = nullptr;
321 mesg->mediaContents = nullptr;
322 mesg->topic = nullptr;
323 mesg->extraInfo = nullptr;
325 OIC::Service::NSMessage nsMsg(mesg);
326 NS_LOGD ("Created NSMessage");
329 nsMsg.setTime(std::string(time));
331 nsMsg.setTitle(std::string(messageTitle));
332 nsMsg.setContentText(std::string(messageBody));
333 nsMsg.setSourceName(std::string(messageSource));
334 if (media != nullptr)
336 nsMsg.setMediaContents(media);
338 nsMsg.setTopic(std::string(topic));
339 if (representation != nullptr)
341 nsMsg.setExtraInfo(*representation);
344 env->DeleteLocalRef(cls_messageType);
348 env->ReleaseStringUTFChars(jtime, time);
352 env->ReleaseStringUTFChars(jmsgTitle, messageTitle);
356 env->ReleaseStringUTFChars(jmsgBody, messageBody);
360 env->ReleaseStringUTFChars(jmsgSource, messageSource);
364 env->ReleaseStringUTFChars(jtopic, topic);
367 NS_LOGD ("JNIProviderService: getMessage - OUT");
372 jobject getJavaMessageType(JNIEnv *env, OIC::Service::NSMessage::NSMessageType type)
374 NS_LOGD ("JNIProviderService: getJavaMessageType - IN");
375 jobject messageType = NULL;
378 case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_ALERT:
380 static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,
381 "ALERT", "Lorg/iotivity/service/ns/common/Message$MessageType;");
382 messageType = env->GetStaticObjectField(g_cls_Message_Type, fieldID);
385 case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_NOTICE:
387 static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,
388 "NOTICE", "Lorg/iotivity/service/ns/common/Message$MessageType;");
389 messageType = env->GetStaticObjectField(g_cls_Message_Type, fieldID);
392 case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_EVENT:
394 static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,
395 "EVENT", "Lorg/iotivity/service/ns/common/Message$MessageType;");
396 messageType = env->GetStaticObjectField(g_cls_Message_Type, fieldID);
399 case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_INFO:
401 static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,
402 "INFO", "Lorg/iotivity/service/ns/common/Message$MessageType;");
403 messageType = env->GetStaticObjectField(g_cls_Message_Type, fieldID);
412 NS_LOGD ("JNIProviderService: getJavaMessageType - OUT");
416 jobject getJavaMessage(JNIEnv *env, OIC::Service::NSMessage message)
418 NS_LOGD ("JNIProviderService: getJavaMessage - IN");
420 NS_LOGD ("id : %llu\n", message.getMessageId());
421 NS_LOGD ("title : %s\n", message.getTitle().c_str());
422 NS_LOGD ("content : %s\n", message.getContentText().c_str());
423 NS_LOGD ("source : %s\n", message.getSourceName().c_str());
425 jlong jMessageId = (jlong) message.getMessageId();
426 jstring jProviderId = env->NewStringUTF(message.getProviderId().c_str());
427 jstring jTitle = env->NewStringUTF(message.getTitle().c_str());
428 jstring jContentText = env->NewStringUTF(message.getContentText().c_str());
429 jstring jSourceName = env->NewStringUTF(message.getSourceName().c_str());
430 jstring jTopic = env->NewStringUTF(message.getTopic().c_str());
432 jstring jTime = env->NewStringUTF(message.getTime().c_str());
433 jlong jTTL = (jlong) message.getTTL();
435 jclass cls_message = (jclass) (env->NewLocalRef(g_cls_Message));
438 NS_LOGE ("Failed to Get ObjectClass for Message");
441 jmethodID mid_message = env->GetMethodID(
442 cls_message, "<init>",
443 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
446 NS_LOGE ("Failed to Get MethodID for Message<init>");
449 jobject obj_message = env->NewObject(cls_message, mid_message,
450 jTitle, jContentText, jSourceName);
453 NS_LOGE ("Failed to Get Java Object for Message");
457 jfieldID fid_messageId = env->GetFieldID(cls_message, "mMessageId", "J");
460 NS_LOGE ("Failed to get field MessageID for Message");
463 env->SetLongField(obj_message, fid_messageId, jMessageId);
465 jfieldID fid_providerId = env->GetFieldID(cls_message, "mProviderId", "Ljava/lang/String;");
468 NS_LOGE ("Failed to get field ProviderID for Message");
471 env->SetObjectField(obj_message, fid_providerId, jProviderId);
473 jfieldID fid_time = env->GetFieldID(cls_message, "mTime", "Ljava/lang/String;");
476 NS_LOGE ("Failed to get field Time for Message");
479 env->SetObjectField(obj_message, fid_time, jTime);
481 jfieldID fid_ttl = env->GetFieldID(cls_message, "mTTL", "J");
484 NS_LOGE ("Failed to get field TTL for Message");
487 env->SetLongField(obj_message, fid_ttl, jTTL);
489 jfieldID fid_topic = env->GetFieldID(cls_message, "mTopic", "Ljava/lang/String;");
492 NS_LOGE ("Failed to get mTopic for Message");
495 env->SetObjectField(obj_message, fid_topic, jTopic);
497 OIC::Service::NSMediaContents *mediaCont = message.getMediaContents();
498 if (mediaCont != nullptr)
500 jstring jIconImage = env->NewStringUTF(mediaCont->getIconImage().c_str());
501 jclass cls_mediaContents = (jclass) (env->NewLocalRef(g_cls_MediaContents));
502 if (!cls_mediaContents)
504 NS_LOGE ("Failed to Get ObjectClass for MediaContents");
507 jmethodID mid_mediaContents = env->GetMethodID(
508 cls_mediaContents, "<init>", "(Ljava/lang/String;)V");
509 if (!mid_mediaContents)
511 NS_LOGE ("Failed to Get MethodID for MediaContents<init>");
514 jobject obj_mediaContents = env->NewObject(cls_mediaContents, mid_mediaContents,
517 jfieldID fid_mediaContents = env->GetFieldID(cls_message, "mMediaContents",
518 "Lorg/iotivity/service/ns/common/MediaContents;");
519 if (!fid_mediaContents)
521 NS_LOGE ("Failed to get field mediaContents for Message");
524 env->SetObjectField(obj_message, fid_mediaContents, obj_mediaContents);
528 jobject jType = getJavaMessageType(env, message.getType());
531 jfieldID fid_type = env->GetFieldID(cls_message, "mType",
532 "Lorg/iotivity/service/ns/common/Message$MessageType;");
535 NS_LOGE ("Failed to get field Type for Message");
538 env->SetObjectField(obj_message, fid_type, jType);
541 NS_LOGD ("Reading OCRepresentation Object from Native");
542 OC::OCRepresentation *ocRepresentation = new OC::OCRepresentation(message.getExtraInfo());
543 jlong handle = reinterpret_cast<jlong>(ocRepresentation);
544 jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
546 if (!jRepresentation)
548 NS_LOGE ("Failed to create OcRepresentation");
549 delete ocRepresentation;
553 NS_LOGD ("Created OCRepresentation Object from Native");
555 jfieldID fid_extraInfo = env->GetFieldID(cls_message, "mExtraInfo",
556 "Lorg/iotivity/base/OcRepresentation;");
559 NS_LOGE ("Failed to get mExtraInfo for Message");
560 delete ocRepresentation;
563 NS_LOGD ("setting extraInfo field");
564 env->SetObjectField(obj_message, fid_extraInfo, jRepresentation);
566 env->DeleteLocalRef(cls_message);
567 NS_LOGD ("JNIProviderService: getJavaMessage - OUT");
571 jobject getJavaTopicState(JNIEnv *env, OIC::Service::NSTopic::NSTopicState nsState)
573 NS_LOGD ("JNIProviderService: getJavaTopicState - IN");
576 jclass cls_topicState = (jclass) (env->NewLocalRef(g_cls_TopicState));
579 NS_LOGE ("Failed to Get ObjectClass for TopicState Type");
582 jobject topicState = NULL;
586 case OIC::Service::NSTopic::NSTopicState::UNSUBSCRIBED:
588 static jfieldID fieldID = env->GetStaticFieldID(cls_topicState,
589 "UNSUBSCRIBED", "Lorg/iotivity/service/ns/common/Topic$TopicState;");
590 topicState = env->GetStaticObjectField(cls_topicState, fieldID);
593 case OIC::Service::NSTopic::NSTopicState::SUBSCRIBED:
595 static jfieldID fieldID = env->GetStaticFieldID(cls_topicState,
596 "SUBSCRIBED", "Lorg/iotivity/service/ns/common/Topic$TopicState;");
597 topicState = env->GetStaticObjectField(cls_topicState, fieldID);
607 NS_LOGD ("JNIProviderService: getJavaTopicState - OUT");
611 jobject getJavaTopicsList(JNIEnv *env, OIC::Service::NSTopicsList *topicList)
613 NS_LOGD ("JNIProviderService: getJavaTopicsList - IN");
614 jclass cls_topicList = (jclass) (env->NewLocalRef(g_cls_TopicsList));
617 NS_LOGE ("Failed to Get ObjectClass for TopicsList");
620 jmethodID mid_topicList = env->GetMethodID(cls_topicList, "<init>", "()V");
623 NS_LOGE ("Failed to Get MethodID for TopicsList<init>");
626 jobject obj_topicList = env->NewObject(cls_topicList, mid_topicList);
629 NS_LOGE ("Failed to Get object for TopicsList");
632 jmethodID mid_addTopic = env->GetMethodID(cls_topicList, "addTopic",
633 "(Ljava/lang/String;Lorg/iotivity/service/ns/common/Topic$TopicState;)V");
636 NS_LOGE ("Failed to Get MethodID for addTopic");
639 for (auto it : topicList->getTopicsList())
641 jobject jState = getJavaTopicState(env, it->getState());
642 std::string topicName = it->getTopicName();
643 jstring jTopicName = env->NewStringUTF(topicName.c_str());
644 env->CallVoidMethod(obj_topicList, mid_addTopic, jTopicName, jState);
646 env->DeleteLocalRef(cls_topicList);
647 NS_LOGD ("JNIProviderService: getJavaTopicsList - OUT");
648 return obj_topicList;
651 jobject getJavaSyncType(JNIEnv *env, OIC::Service::NSSyncInfo::NSSyncType nsType)
653 NS_LOGD ("JNIProviderService: getJavaSyncType - IN");
656 jclass cls_SyncType = (jclass) (env->NewLocalRef(g_cls_SyncType));
659 NS_LOGE ("Failed to Get ObjectClass for SyncType");
662 jobject syncType = NULL;
665 case OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_UNREAD:
667 static jfieldID fieldID = env->GetStaticFieldID(cls_SyncType,
668 "UNREAD", "Lorg/iotivity/service/ns/common/SyncInfo$SyncType;");
669 syncType = env->GetStaticObjectField(cls_SyncType, fieldID);
672 case OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ :
674 static jfieldID fieldID = env->GetStaticFieldID(cls_SyncType,
675 "READ", "Lorg/iotivity/service/ns/common/SyncInfo$SyncType;");
676 syncType = env->GetStaticObjectField(cls_SyncType, fieldID);
679 case OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED :
681 static jfieldID fieldID = env->GetStaticFieldID(cls_SyncType,
682 "DELETED", "Lorg/iotivity/service/ns/common/SyncInfo$SyncType;");
683 syncType = env->GetStaticObjectField(cls_SyncType, fieldID);
693 NS_LOGD ("JNIProviderService: getJavaSyncType - OUT");
698 void onSubscribeListenerCb(std::shared_ptr<OIC::Service::NSConsumer> consumer)
700 NS_LOGD ("JNIProviderService_onSubscribeListenerCb - IN");
703 JNIEnv *env = GetJNIEnv(&envRet);
709 jobject jSubscriptionListener = (jobject) env->NewLocalRef(g_obj_subscriptionListener);
710 if (!jSubscriptionListener)
712 NS_LOGE ("Failed to Get jSubscriptionListener");
713 if (JNI_EDETACHED == envRet)
715 g_jvm_provider->DetachCurrentThread();
720 NS_LOGD ("consumer ID : %s\n", consumer->getConsumerId().c_str());
722 jstring jConsumerId = env->NewStringUTF( consumer->getConsumerId().c_str());
724 jclass cls_consumer = (jclass) (env->NewLocalRef(g_cls_Consumer));
727 NS_LOGE ("Failed to Get ObjectClass for Consumer");
728 if (JNI_EDETACHED == envRet)
730 g_jvm_provider->DetachCurrentThread();
735 jmethodID mid_consumer = env->GetMethodID(
738 "(Ljava/lang/String;)V");
741 NS_LOGE ("Failed to Get MethodID for Consumer<init>");
742 if (JNI_EDETACHED == envRet)
744 g_jvm_provider->DetachCurrentThread();
748 jobject obj_consumer = env->NewObject( cls_consumer, mid_consumer, jConsumerId);
750 jclass cls = env->GetObjectClass( jSubscriptionListener);
753 NS_LOGE ("Failed to Get ObjectClass of jSubscriptionListener");
754 if (JNI_EDETACHED == envRet)
756 g_jvm_provider->DetachCurrentThread();
760 jmethodID mid = env->GetMethodID(
762 "onConsumerSubscribed",
763 "(Lorg/iotivity/service/ns/provider/Consumer;)V");
766 NS_LOGE ("Failed to Get MethodID of onConsumerSubscribed");
767 if (JNI_EDETACHED == envRet)
769 g_jvm_provider->DetachCurrentThread();
774 env->CallVoidMethod( jSubscriptionListener, mid, obj_consumer);
775 env->DeleteLocalRef(jSubscriptionListener);
776 env->DeleteLocalRef(cls_consumer);
777 if (JNI_EDETACHED == envRet)
779 g_jvm_provider->DetachCurrentThread();
781 NS_LOGD ("JNIProviderService_onSubscribeListenerCb - OUT");
785 void onSyncInfoListenerCb(OIC::Service::NSSyncInfo sync)
787 NS_LOGD ("JNIProviderService_onSyncInfoListenerCb - IN");
790 JNIEnv *env = GetJNIEnv(&envRet);
796 jobject jSyncListener = (jobject) env->NewLocalRef(g_obj_syncListener);
799 NS_LOGE ("Failed to Get jSyncListener");
800 if (JNI_EDETACHED == envRet)
802 g_jvm_provider->DetachCurrentThread();
807 NS_LOGD ("Sync ID : %ld\n", (long) sync.getMessageId());
808 NS_LOGD ("Sync STATE : %d\n", (int) sync.getState());
810 jlong jMessageId = (long) sync.getMessageId();
811 jstring jProviderId = env->NewStringUTF(sync.getProviderId().c_str());
812 jobject syncType = getJavaSyncType(env, sync.getState());
815 NS_LOGE ("Failed to Get syncType for SyncInfo");
816 if (JNI_EDETACHED == envRet)
818 g_jvm_provider->DetachCurrentThread();
822 jclass cls_SyncInfo = (jclass) (env->NewLocalRef(g_cls_SyncInfo));
825 NS_LOGE ("Failed to Get ObjectClass for SyncInfo");
826 if (JNI_EDETACHED == envRet)
828 g_jvm_provider->DetachCurrentThread();
832 jmethodID mid_syncInfo = env->GetMethodID(
835 "(JLjava/lang/String;Lorg/iotivity/service/ns/common/SyncInfo$SyncType;)V");
838 NS_LOGE ("Failed to Get MethodID for SyncInfo");
839 if (JNI_EDETACHED == envRet)
841 g_jvm_provider->DetachCurrentThread();
846 jobject obj_syncInfo = env->NewObject( cls_SyncInfo, mid_syncInfo,
847 jMessageId, jProviderId, syncType);
850 NS_LOGE ("Failed to Get Object for SyncInfo");
851 if (JNI_EDETACHED == envRet)
853 g_jvm_provider->DetachCurrentThread();
858 jclass cls = env->GetObjectClass( jSyncListener);
861 NS_LOGE ("Failed to Get ObjectClass");
862 if (JNI_EDETACHED == envRet)
864 g_jvm_provider->DetachCurrentThread();
868 jmethodID mid = env->GetMethodID(
870 "onMessageSynchronized",
871 "(Lorg/iotivity/service/ns/common/SyncInfo;)V");
874 NS_LOGE ("Failed to Get MethodID");
875 if (JNI_EDETACHED == envRet)
877 g_jvm_provider->DetachCurrentThread();
881 env->CallVoidMethod( jSyncListener, mid, obj_syncInfo);
883 env->DeleteLocalRef(jSyncListener);
884 env->DeleteLocalRef(cls_SyncInfo);
885 if (JNI_EDETACHED == envRet)
887 g_jvm_provider->DetachCurrentThread();
889 NS_LOGD ("JNIProviderService: OnSyncInfoListenerCb - OUT");
894 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeStart
895 (JNIEnv *env, jobject jObj, jobject jSubscriptionListener, jobject jSyncListener,
896 jboolean jPolicy, jstring jUserInfo, jboolean jResourceSecurity)
898 NS_LOGD ("JNIProviderService: nativeStart - IN");
899 if (!jSubscriptionListener || !jSyncListener)
901 ThrowNSException(JNI_INVALID_VALUE, "Listener cannot be null");
905 if (g_obj_subscriptionListener != NULL)
907 env->DeleteGlobalRef(g_obj_subscriptionListener);
909 if (g_obj_syncListener != NULL)
911 env->DeleteGlobalRef(g_obj_syncListener);
914 g_obj_subscriptionListener = (jobject) env->NewGlobalRef(jSubscriptionListener);
915 g_obj_syncListener = (jobject) env->NewGlobalRef(jSyncListener);
917 // check access policy
919 OIC::Service::NSProviderService::ProviderConfig cfg;
920 cfg.m_subscribeRequestCb = onSubscribeListenerCb;
921 cfg.m_syncInfoCb = onSyncInfoListenerCb;
922 cfg.subControllability = (bool) jPolicy;
925 const char *info = env->GetStringUTFChars( jUserInfo, NULL);
926 std::string userInfo(info);
927 cfg.userInfo = userInfo;
929 cfg.resourceSecurity = (bool) jResourceSecurity;
931 OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->start(cfg);
932 if (result != OIC::Service::NSResult::OK)
934 ThrowNSException((int) result, "Fail to start NSProviderService");
938 NS_LOGD ("JNIProviderService: nativeStart - OUT");
942 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeStop
943 (JNIEnv *env, jobject jObj)
945 NS_LOGD ("JNIProviderService: nativeStop - IN");
947 OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->stop();
948 if (result != OIC::Service::NSResult::OK)
950 ThrowNSException((int) result, "Fail to stop NSProviderService");
954 env->DeleteGlobalRef( g_obj_subscriptionListener);
955 env->DeleteGlobalRef( g_obj_syncListener);
956 g_obj_subscriptionListener = NULL;
957 g_obj_syncListener = NULL;
959 NS_LOGD ("JNIProviderService: nativeStop - OUT");
963 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeSendMessage
964 (JNIEnv *env, jobject jObj, jobject jMsg)
966 NS_LOGD ("JNIProviderService: nativeSendMessage - IN");
969 ThrowNSException(JNI_INVALID_VALUE, "Message cannot be null");
972 OIC::Service::NSMessage nsMsg = getNativeMessage(env, jMsg);
974 OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->sendMessage(nsMsg);
975 if (result != OIC::Service::NSResult::OK)
977 ThrowNSException((int) result, "Fail to send NSProvider Message");
980 NS_LOGD ("JNIProviderService: nativeSendMessage - OUT");
984 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeSendSyncInfo
985 (JNIEnv *env, jobject jObj, jlong messageId , jint syncState)
987 NS_LOGD ("JNIProviderService: nativeSendSyncInfo - IN");
988 OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->sendSyncInfo(
990 (OIC::Service::NSSyncInfo::NSSyncType) syncState);
991 if (result != OIC::Service::NSResult::OK)
993 ThrowNSException((int) result, "Fail to send NSProvider SendSyncInfo");
996 NS_LOGD ("JNIProviderService: nativeSendSyncInfo - OUT");
1000 JNIEXPORT jobject JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeCreateMessage
1001 (JNIEnv *env, jobject jObj)
1003 NS_LOGD ("JNIProviderService: nativeCreateMessage - IN");
1004 OIC::Service::NSMessage message =
1005 OIC::Service::NSProviderService::getInstance()->createMessage();
1006 jobject jMsg = getJavaMessage(env, message);
1009 ThrowNSException(JNI_INVALID_VALUE, "Couldn't create Java Message");
1012 NS_LOGD ("JNIProviderService: nativeCreateMessage - OUT");
1016 JNIEXPORT void JNICALL
1017 Java_org_iotivity_service_ns_provider_ProviderService_nativeEnableRemoteService
1018 (JNIEnv *env, jobject jObj, jstring jstr)
1020 NS_LOGD ("JNIProviderService: nativeEnableRemoteService - IN");
1023 ThrowNSException(JNI_INVALID_VALUE, "Server Address Can't be NULL");
1027 const char *address = env->GetStringUTFChars( jstr, NULL);
1028 std::string servAddress(address);
1029 OIC::Service::NSResult result =
1030 OIC::Service::NSProviderService::getInstance()->enableRemoteService(
1032 if (result != OIC::Service::NSResult::OK)
1034 ThrowNSException((int) result, "Fail to Enable Remote Service");
1037 env->ReleaseStringUTFChars(jstr, address);
1038 NS_LOGD ("JNIProviderService: nativeEnableRemoteService - OUT");
1042 JNIEXPORT void JNICALL
1043 Java_org_iotivity_service_ns_provider_ProviderService_nativeDisableRemoteService
1044 (JNIEnv *env, jobject jObj, jstring jstr)
1046 NS_LOGD ("JNIProviderService: nativeDisableRemoteService - IN");
1049 ThrowNSException(JNI_INVALID_VALUE, "Server Address Can't be NULL");
1053 const char *address = env->GetStringUTFChars( jstr, NULL);
1054 std::string servAddress(address);
1055 OIC::Service::NSResult result =
1056 OIC::Service::NSProviderService::getInstance()->disableRemoteService(
1058 if (result != OIC::Service::NSResult::OK)
1060 ThrowNSException((int) result, "Fail to Disable Remote Service");
1063 env->ReleaseStringUTFChars(jstr, address);
1064 NS_LOGD ("JNIProviderService: nativeDisableRemoteService - OUT");
1068 JNIEXPORT void JNICALL
1069 Java_org_iotivity_service_ns_provider_ProviderService_nativeSubscribeMQService
1070 (JNIEnv *env, jobject jObj, jstring jserverAddress, jstring jTopicName)
1072 NS_LOGD ("JNIProviderService: nativeSubscribeMQService - IN");
1073 if (!jserverAddress)
1075 ThrowNSException(JNI_INVALID_VALUE, "Server Address Can't be NULL");
1080 ThrowNSException(JNI_INVALID_VALUE, "TopicName Can't be NULL");
1084 const char *address = env->GetStringUTFChars( jserverAddress, NULL);
1085 std::string servAddress(address);
1086 const char *topic = env->GetStringUTFChars( jTopicName, NULL);
1087 std::string topicName(topic);
1089 OIC::Service::NSResult result =
1090 OIC::Service::NSProviderService::getInstance()->subscribeMQService(
1091 servAddress, topicName);
1092 if (result != OIC::Service::NSResult::OK)
1094 ThrowNSException((int) result, "Fail to Subscribe to MQ Service");
1097 env->ReleaseStringUTFChars(jserverAddress, address);
1098 env->ReleaseStringUTFChars(jTopicName, topic);
1099 NS_LOGD ("JNIProviderService: nativeSubscribeMQService - OUT");
1103 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeRegisterTopic
1104 (JNIEnv *env, jobject jObj, jstring jTopicName)
1106 NS_LOGD ("JNIProviderService: nativeRegisterTopic - IN");
1109 ThrowNSException(JNI_INVALID_VALUE, "Topic Name Can't be NULL");
1112 const char *name = env->GetStringUTFChars( jTopicName, NULL);
1113 std::string topicName(name);
1114 OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->registerTopic(
1116 if (result != OIC::Service::NSResult::OK)
1118 ThrowNSException((int) result, "Fail to Register Topic");
1121 env->ReleaseStringUTFChars(jTopicName, name);
1122 NS_LOGD ("JNIProviderService: nativeRegisterTopic - OUT");
1125 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nativeUnregisterTopic
1126 (JNIEnv *env, jobject jObj, jstring jTopicName)
1128 NS_LOGD ("JNIProviderService: nativeUnregisterTopic - IN");
1131 ThrowNSException(JNI_INVALID_VALUE, "Topic Name Can't be NULL");
1134 const char *name = env->GetStringUTFChars( jTopicName, NULL);
1135 std::string topicName(name);
1136 OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->unregisterTopic(
1138 if (result != OIC::Service::NSResult::OK)
1140 ThrowNSException((int) result, "Fail to Unregister Topic");
1143 env->ReleaseStringUTFChars(jTopicName, name);
1144 NS_LOGD ("JNIProviderService: nativeUnregisterTopic - OUT");
1148 JNIEXPORT jobject JNICALL
1149 Java_org_iotivity_service_ns_provider_ProviderService_nativeGetRegisteredTopicList
1150 (JNIEnv *env, jobject jObj)
1152 NS_LOGD ("JNIProviderService: nativeGetRegisteredTopicList - IN");
1154 OIC::Service::NSTopicsList *topicList =
1155 OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList();
1156 if (topicList == nullptr)
1158 ThrowNSException(JNI_INVALID_VALUE, "Topic List doesn't exist");
1162 jobject obj_topicList = getJavaTopicsList(env, topicList);
1165 NS_LOGD ("JNIProviderService: nativeGetRegisteredTopicList - OUT");
1166 return obj_topicList;
1169 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeAcceptSubscription
1170 (JNIEnv *env, jobject jObj, jstring jConsumerId, jboolean jAccepted)
1172 NS_LOGD ("JNIProviderService: nativeAcceptSubscription - IN");
1175 ThrowNSException(JNI_INVALID_VALUE, "ConsumerId Can't be NULL");
1178 const char *id = env->GetStringUTFChars( jConsumerId, NULL);
1179 std::string consumerId(id);
1180 NS_LOGD ("Consumer ID: %s\n", consumerId.c_str());
1182 std::shared_ptr<OIC::Service::NSConsumer> consumer =
1183 OIC::Service::NSProviderService::getInstance()->getConsumer(consumerId);
1186 OIC::Service::NSResult result = consumer->acceptSubscription((bool)jAccepted);
1187 if (result != OIC::Service::NSResult::OK)
1189 ThrowNSException((int) result, "Fail to acceptSubscription");
1195 NS_LOGE ("Couldn't find consumer");
1196 ThrowNSException(JNI_NO_NATIVE_POINTER, "Fail to find consumer");
1200 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeSetConsumerTopic
1201 (JNIEnv *env, jobject jObj, jstring jConsumerId, jstring jTopicName)
1203 NS_LOGD ("JNIProviderService: nativeSetConsumerTopic - IN");
1204 if (!jConsumerId || !jTopicName)
1206 ThrowNSException(JNI_INVALID_VALUE, "Topic Name or ConsumerId Can't be NULL");
1209 const char *name = env->GetStringUTFChars( jTopicName, NULL);
1210 const char *id = env->GetStringUTFChars( jConsumerId, NULL);
1211 std::string topicName(name);
1212 std::string consumerId(id);
1213 std::shared_ptr<OIC::Service::NSConsumer> nsConsumer =
1214 OIC::Service::NSProviderService::getInstance()->getConsumer(consumerId);
1217 ThrowNSException(JNI_NO_NATIVE_POINTER, "Consumer does exists");
1220 OIC::Service::NSResult result = nsConsumer->setTopic(topicName);
1222 if (result != OIC::Service::NSResult::OK)
1224 ThrowNSException((int) result, "Fail to Select Topic");
1227 env->ReleaseStringUTFChars(jTopicName, name);
1228 env->ReleaseStringUTFChars(jConsumerId, id);
1229 NS_LOGD ("JNIProviderService: nativeSetConsumerTopic - OUT");
1232 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeUnsetConsumerTopic
1233 (JNIEnv *env, jobject jObj, jstring jConsumerId, jstring jTopicName)
1235 NS_LOGD ("JNIProviderService: nativeUnsetConsumerTopic - IN");
1236 if (!jConsumerId || !jTopicName)
1238 ThrowNSException(JNI_INVALID_VALUE, "Topic Name or ConsumerId Can't be NULL");
1241 const char *name = env->GetStringUTFChars( jTopicName, NULL);
1242 const char *id = env->GetStringUTFChars( jConsumerId, NULL);
1243 std::string topicName(name);
1244 std::string consumerId(id);
1245 std::shared_ptr<OIC::Service::NSConsumer> nsConsumer =
1246 OIC::Service::NSProviderService::getInstance()->getConsumer(consumerId);
1249 ThrowNSException(JNI_NO_NATIVE_POINTER, "Consumer does exists");
1252 OIC::Service::NSResult result = nsConsumer->unsetTopic(topicName);
1254 if (result != OIC::Service::NSResult::OK)
1256 ThrowNSException((int) result, "Fail to Unselect Topic");
1259 env->ReleaseStringUTFChars(jTopicName, name);
1260 env->ReleaseStringUTFChars(jConsumerId, id);
1261 NS_LOGD ("JNIProviderService: nativeUnsetConsumerTopic - OUT");
1265 JNIEXPORT jobject JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeGetConsumerTopicList
1266 (JNIEnv *env, jobject jObj, jstring jConsumerId)
1268 NS_LOGD ("JNIProviderService: nativeGetConsumerTopicList - IN");
1271 ThrowNSException(JNI_INVALID_VALUE, "Topic Name or ConsumerId Can't be NULL");
1274 const char *id = env->GetStringUTFChars( jConsumerId, NULL);
1275 std::string consumerId(id);
1276 std::shared_ptr<OIC::Service::NSConsumer> nsConsumer =
1277 OIC::Service::NSProviderService::getInstance()->getConsumer(consumerId);
1280 ThrowNSException(JNI_NO_NATIVE_POINTER, "Consumer does exists");
1283 env->ReleaseStringUTFChars(jConsumerId, id);
1284 OIC::Service::NSTopicsList *topicList = nsConsumer->getConsumerTopicList();
1285 if (topicList == nullptr)
1287 ThrowNSException(JNI_NO_NATIVE_POINTER, "Topic List doesn't exist");
1290 jobject obj_topicList = getJavaTopicsList(env, topicList);
1293 NS_LOGD ("JNIProviderService: nativeGetConsumerTopicList - OUT");
1294 return obj_topicList;
1298 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved)
1300 NS_LOGD ("ProviderService_JNI_OnLoad");
1301 g_jvm_provider = jvm;
1304 if (jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
1306 NS_LOGE ("Failed to get the environment using GetEnv()");
1310 jclass localMessage = env->FindClass(
1311 "org/iotivity/service/ns/common/Message");
1314 NS_LOGE ("Failed to get local Message class");
1317 g_cls_Message = (jclass) (env->NewGlobalRef(localMessage));
1320 NS_LOGE ("Failed to set Global Message reference");
1324 jclass localMessageType = env->FindClass(
1325 "org/iotivity/service/ns/common/Message$MessageType");
1326 if (!localMessageType)
1328 NS_LOGE ("Failed to get local Message Type class");
1331 g_cls_Message_Type = (jclass) (env->NewGlobalRef(localMessageType));
1332 if (!g_cls_Message_Type)
1334 NS_LOGE ("Failed to set Global Message Type reference");
1338 jclass localConsumer = env->FindClass(
1339 "org/iotivity/service/ns/provider/Consumer");
1342 NS_LOGE ("Failed to get local Provider class");
1345 g_cls_Consumer = (jclass) (env->NewGlobalRef(localConsumer));
1346 if (!g_cls_Consumer)
1348 NS_LOGE ("Failed to set Global Provider reference");
1352 jclass localSyncInfo = env->FindClass(
1353 "org/iotivity/service/ns/common/SyncInfo");
1356 NS_LOGE ("Failed to get local SyncInfo class");
1359 g_cls_SyncInfo = (jclass) (env->NewGlobalRef(localSyncInfo));
1360 if (!g_cls_SyncInfo)
1362 NS_LOGE ("Failed to set Global SyncInfo reference");
1366 jclass localSyncType = env->FindClass(
1367 "org/iotivity/service/ns/common/SyncInfo$SyncType");
1370 NS_LOGE ("Failed to get local SyncType enum");
1373 g_cls_SyncType = (jclass) (env->NewGlobalRef(localSyncType));
1374 if (!g_cls_SyncType)
1376 NS_LOGE ("Failed to set Global SyncType reference");
1380 jclass localMediaContents = env->FindClass(
1381 "org/iotivity/service/ns/common/MediaContents");
1382 if (!localMediaContents)
1384 NS_LOGE ("Failed to get local MediaContents class");
1387 g_cls_MediaContents = (jclass) (env->NewGlobalRef(localMediaContents));
1388 if (!g_cls_MediaContents)
1390 NS_LOGE ("Failed to set Global MediaContents reference");
1394 jclass localTopic = env->FindClass(
1395 "org/iotivity/service/ns/common/Topic");
1398 NS_LOGE ("Failed to get local Topic class");
1401 g_cls_Topic = (jclass) (env->NewGlobalRef(localTopic));
1404 NS_LOGE ("Failed to set Global Topic reference");
1408 jclass localTopicsList = env->FindClass(
1409 "org/iotivity/service/ns/common/TopicsList");
1410 if (!localTopicsList)
1412 NS_LOGE ("Failed to get local Topic class");
1415 g_cls_TopicsList = (jclass) (env->NewGlobalRef(localTopicsList));
1416 if (!g_cls_TopicsList)
1418 NS_LOGE ("Failed to set Global TopicsList reference");
1422 jclass localTopicState = env->FindClass(
1423 "org/iotivity/service/ns/common/Topic$TopicState");
1424 if (!localTopicState)
1426 NS_LOGE ("Failed to get local TopicState enum");
1429 g_cls_TopicState = (jclass) (env->NewGlobalRef(localTopicState));
1430 if (!g_cls_TopicState)
1432 NS_LOGE ("Failed to set Global TopicState reference");
1437 jclass localOcRepresentation = env->FindClass("org/iotivity/base/OcRepresentation");
1438 if (!localOcRepresentation)
1440 NS_LOGE ("Failed to get local OcRepresentation class");
1443 g_cls_OcRepresentation = (jclass) env->NewGlobalRef(localOcRepresentation);
1444 if (!g_cls_OcRepresentation)
1446 NS_LOGE ("Failed to set Global OcRepresentation reference");
1450 g_mid_OcRepresentation_N_ctor_bool = env->GetMethodID(g_cls_OcRepresentation, "<init>", "(JZ)V");
1451 if (!g_mid_OcRepresentation_N_ctor_bool)
1453 NS_LOGE ("Failed to get Global OcRepresentation Constructor reference");
1457 env->DeleteLocalRef(localMessage);
1458 env->DeleteLocalRef(localMessageType);
1459 env->DeleteLocalRef(localConsumer);
1460 env->DeleteLocalRef(localSyncInfo);
1461 env->DeleteLocalRef(localSyncType);
1462 env->DeleteLocalRef(localMediaContents);
1463 env->DeleteLocalRef(localTopic);
1464 env->DeleteLocalRef(localTopicsList);
1465 env->DeleteLocalRef(localTopicState);
1466 env->DeleteLocalRef(localOcRepresentation);
1468 return NSExceptionInit(env);
1471 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserved)
1473 NS_LOGD ("ProviderService_JNI_OnUnload");
1476 if (jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)
1478 NS_LOGE ("Failed to get the environment using GetEnv()");
1482 env->DeleteGlobalRef(g_cls_Message);
1483 env->DeleteGlobalRef(g_cls_Consumer);
1484 env->DeleteGlobalRef(g_cls_SyncInfo);
1485 env->DeleteGlobalRef(g_cls_SyncType);
1486 env->DeleteGlobalRef(g_cls_MediaContents);
1487 env->DeleteGlobalRef(g_cls_Message_Type);
1488 env->DeleteGlobalRef(g_cls_Topic);
1489 env->DeleteGlobalRef(g_cls_TopicsList);
1490 env->DeleteGlobalRef(g_cls_TopicState);
1491 env->DeleteGlobalRef(g_cls_OcRepresentation);