Added CreateMessage API in Android APIs
[platform/upstream/iotivity.git] / service / notification / android / notification-service / src / main / jni / consumer / JniNotificationConsumer.cpp
1 //******************************************************************\r
2 //\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.\r
4 //\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
6 //\r
7 // Licensed under the Apache License, Version 2.0 (the "License");\r
8 // you may not use this file except in compliance with the License.\r
9 // You may obtain a copy of the License at\r
10 //\r
11 //      http://www.apache.org/licenses/LICENSE-2.0\r
12 //\r
13 // Unless required by applicable law or agreed to in writing, software\r
14 // distributed under the License is distributed on an "AS IS" BASIS,\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16 // See the License for the specific language governing permissions and\r
17 // limitations under the License.\r
18 //\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
20 \r
21 #include "JniNotificationConsumer.h"\r
22 #include "NSConsumerService.h"\r
23 \r
24 static JavaVM *g_jvm = NULL;\r
25 \r
26 static jobject g_obj_postListener = NULL;\r
27 static jobject g_obj_discoverListener = NULL;\r
28 static jobject g_obj_syncListener = NULL;\r
29 static jobject g_obj_acceptListener = NULL;\r
30 \r
31 jclass g_cls_Message;\r
32 jclass g_cls_Provider;\r
33 jclass g_cls_SyncInfo;\r
34 jclass g_cls_SyncType;\r
35 jclass g_cls_MediaContents;\r
36 jclass g_cls_TopicState;\r
37 jclass g_cls_Message_Type;\r
38 jclass g_cls_Response;\r
39 jclass g_cls_Topic;\r
40 jclass g_cls_TopicsList;\r
41 \r
42 static JNIEnv *GetJNIEnv(jint *ret)\r
43 {\r
44     JNIEnv *env = NULL;\r
45 \r
46     *ret = g_jvm->GetEnv((void **) &env, JNI_CURRENT_VERSION);\r
47     switch (*ret)\r
48     {\r
49         case JNI_OK:\r
50             return env;\r
51         case JNI_EDETACHED:\r
52             if (g_jvm->AttachCurrentThread(&env, NULL) != JNI_OK)\r
53             {\r
54                 LOGE ("Failed to get the environment");\r
55                 return NULL;\r
56             }\r
57             else\r
58             {\r
59                 return env;\r
60             }\r
61         case JNI_EVERSION:\r
62             LOGE ("JNI version is not supported");\r
63         default:\r
64             LOGE ("Failed to get the environment");\r
65             return NULL;\r
66     }\r
67 }\r
68 \r
69 jobject getJavaResponse(JNIEnv *env, OIC::Service::NSResponse response)\r
70 {\r
71     LOGD ("ConsumerService_getJavaResponse - IN");\r
72     switch (response)\r
73     {\r
74         case OIC::Service::NSResponse::ALLOW:\r
75             {\r
76                 static jfieldID fieldID = env->GetStaticFieldID(g_cls_Response,\r
77                                           "ALLOW", "Lorg/iotivity/service/ns/consumer/ConsumerService$Response;");\r
78                 return env->GetStaticObjectField(g_cls_Response, fieldID);\r
79             }\r
80         case OIC::Service::NSResponse::DENY:\r
81             {\r
82                 static jfieldID fieldID = env->GetStaticFieldID(g_cls_Response,\r
83                                           "DENY", "Lorg/iotivity/service/ns/consumer/ConsumerService$Response;");\r
84                 return env->GetStaticObjectField(g_cls_Response, fieldID);\r
85             }\r
86         case OIC::Service::NSResponse::TOPIC:\r
87             {\r
88                 static jfieldID fieldID = env->GetStaticFieldID(g_cls_Response,\r
89                                           "TOPIC", "Lorg/iotivity/service/ns/consumer/ConsumerService$Response;");\r
90                 return env->GetStaticObjectField(g_cls_Response, fieldID);\r
91             }\r
92         default:\r
93             return NULL;\r
94     }\r
95     LOGD ("ConsumerService_getJavaResponse - OUT");\r
96     return NULL;\r
97 }\r
98 \r
99 jobject getJavaSyncType(JNIEnv *env, OIC::Service::NSSyncInfo::NSSyncType nsType)\r
100 {\r
101     LOGD ("ConsumerService_getJavaSyncType - IN");\r
102 \r
103     // SyncType\r
104     jclass cls_SyncType = (jclass) (env->NewLocalRef(g_cls_SyncType));\r
105     if (!cls_SyncType)\r
106     {\r
107         LOGE ("Failed to Get ObjectClass for SyncType");\r
108         return NULL;\r
109     }\r
110     jobject syncType;\r
111     switch (nsType)\r
112     {\r
113         case OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_UNREAD:\r
114             {\r
115                 static jfieldID fieldID = env->GetStaticFieldID(cls_SyncType,\r
116                                           "UNREAD", "Lorg/iotivity/service/ns/common/SyncInfo$SyncType;");\r
117                 syncType = env->GetStaticObjectField(cls_SyncType, fieldID);\r
118             }\r
119         case OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ :\r
120             {\r
121                 static jfieldID fieldID = env->GetStaticFieldID(cls_SyncType,\r
122                                           "READ", "Lorg/iotivity/service/ns/common/SyncInfo$SyncType;");\r
123                 syncType = env->GetStaticObjectField(cls_SyncType, fieldID);\r
124             }\r
125         case OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED :\r
126             {\r
127                 static jfieldID fieldID = env->GetStaticFieldID(cls_SyncType,\r
128                                           "DELETED", "Lorg/iotivity/service/ns/common/SyncInfo$SyncType;");\r
129                 syncType = env->GetStaticObjectField(cls_SyncType, fieldID);\r
130             }\r
131 \r
132     }\r
133 \r
134     if (syncType == NULL)\r
135     {\r
136         LOGD("Error: object of field  Synctype  is null");\r
137     }\r
138 \r
139     env->DeleteLocalRef(cls_SyncType);\r
140     LOGD ("ConsumerService_getJavaSyncType - OUT");\r
141     return syncType;\r
142 }\r
143 \r
144 jobject getJavaTopicState(JNIEnv *env, OIC::Service::NSTopic::NSTopicState nsState)\r
145 {\r
146     LOGD ("ConsumerService_getJavaTopicState - IN");\r
147 \r
148     // TopicState\r
149     jclass cls_topicState = (jclass) (env->NewLocalRef(g_cls_TopicState));\r
150     if (!cls_topicState)\r
151     {\r
152         LOGE ("Failed to Get ObjectClass for TopicState Type");\r
153         return NULL;\r
154     }\r
155 \r
156     jobject obj_topicState;\r
157     switch (nsState)\r
158     {\r
159         case OIC::Service::NSTopic::NSTopicState::UNSUBSCRIBED:\r
160             {\r
161                 static jfieldID fieldID = env->GetStaticFieldID(cls_topicState,\r
162                                           "UNSUBSCRIBED", "Lorg/iotivity/service/ns/common/Topic$TopicState;");\r
163                 obj_topicState = env->GetStaticObjectField(cls_topicState, fieldID);\r
164             }\r
165         case OIC::Service::NSTopic::NSTopicState::SUBSCRIBED:\r
166             {\r
167                 static jfieldID fieldID = env->GetStaticFieldID(cls_topicState,\r
168                                           "SUBSCRIBED", "Lorg/iotivity/service/ns/common/Topic$TopicState;");\r
169                 obj_topicState = env->GetStaticObjectField(cls_topicState, fieldID);\r
170             }\r
171 \r
172     }\r
173     if (obj_topicState == NULL)\r
174     {\r
175         LOGD("Error: object of field  TopicState  is null");\r
176     }\r
177 \r
178     env->DeleteLocalRef(cls_topicState);\r
179     LOGD ("ConsumerService_getJavaTopicState - OUT");\r
180     return obj_topicState;\r
181 }\r
182 \r
183 jobject getJavaTopicsList(JNIEnv *env, OIC::Service::NSTopicsList *topicList)\r
184 {\r
185     LOGD ("ConsumerService_getJavaTopicsList - IN");\r
186     jclass cls_topicList = (jclass) (env->NewLocalRef(g_cls_TopicsList));\r
187     if (!cls_topicList)\r
188     {\r
189         LOGE ("Failed to Get ObjectClass for TopicsList");\r
190         return NULL;\r
191     }\r
192     jmethodID mid_topicList = env->GetMethodID(cls_topicList, "<init>", "()V");\r
193     if (!mid_topicList)\r
194     {\r
195         LOGE ("Failed to Get MethodID for TopicsList<init>");\r
196         return NULL;\r
197     }\r
198     jobject obj_topicList = env->NewObject(cls_topicList, mid_topicList);\r
199     if (!obj_topicList)\r
200     {\r
201         LOGE ("Failed to Get object for TopicsList");\r
202         return NULL;\r
203     }\r
204     jmethodID mid_addTopic = env->GetMethodID(cls_topicList, "addTopic",\r
205                              "(Ljava/lang/String;Lorg/iotivity/service/ns/common/Topic$TopicState;)V");\r
206     if (!mid_addTopic)\r
207     {\r
208         LOGE ("Failed to Get MethodID for addTopic");\r
209         return NULL;\r
210     }\r
211     for (auto it : topicList->getTopicsList())\r
212     {\r
213         jobject jState = getJavaTopicState(env, it->getState());\r
214         std::string topicName = it->getTopicName();\r
215         jstring jTopicName = env->NewStringUTF(topicName.c_str());\r
216         env->CallVoidMethod(obj_topicList, mid_addTopic, jTopicName, jState);\r
217     }\r
218     env->DeleteLocalRef(cls_topicList);\r
219     LOGD ("ConsumerService_getJavaTopicsList - OUT");\r
220     return obj_topicList;\r
221 }\r
222 \r
223 bool getNativeTopicState(JNIEnv *env,  jobject jTopic , OIC::Service::NSTopic::NSTopicState &state )\r
224 {\r
225     LOGD ("ConsumerService_getNativeTopicState - IN");\r
226 \r
227     jclass cls_topic = env->GetObjectClass( jTopic);\r
228     // TopicState\r
229     jclass cls_TopicState = (jclass) (env->NewLocalRef(g_cls_TopicState));\r
230     if (!cls_TopicState)\r
231     {\r
232         LOGE ("Failed to Get ObjectClass for cls_TopicState Type");\r
233         return false;\r
234     }\r
235     jmethodID mid = env->GetMethodID(cls_TopicState, "ordinal", "()I");\r
236     jfieldID fid_state = env->GetFieldID( cls_topic, "mState",\r
237                                           "Lorg/iotivity/service/ns/common/Topic$TopicState;");\r
238     if (fid_state == NULL)\r
239     {\r
240         LOGE("Error: jfieldID for state type  is null");\r
241         return false;\r
242     }\r
243     jobject jobj = env->GetObjectField( jTopic, fid_state);\r
244     if (jobj == NULL)\r
245     {\r
246         LOGE("Error: object of field  state Type is null");\r
247         return false;\r
248     }\r
249     jint jState = env->CallIntMethod(jobj, mid);\r
250     state = (OIC::Service::NSTopic::NSTopicState) jState;\r
251     LOGD ("ConsumerService_getNativeTopicState - OUT");\r
252     return true;\r
253 \r
254 }\r
255 \r
256 const char *getNativeTopicName(JNIEnv *env,  jobject jTopic)\r
257 {\r
258     LOGD ("ConsumerService_getNativeTopicName - IN");\r
259     jclass cls_topic = env->GetObjectClass( jTopic);\r
260     if (cls_topic == NULL)\r
261     {\r
262         LOGE("Error: Class for Topic is null");\r
263         return nullptr;\r
264     }\r
265     jfieldID fid_name = env->GetFieldID( cls_topic, "mTopicName", "Ljava/lang/String;");\r
266     if (fid_name == NULL)\r
267     {\r
268         LOGE("Error: jfieldID for Topic Name  is null");\r
269         return nullptr;\r
270     }\r
271     jstring jTopicName = (jstring) env->GetObjectField( jTopic, fid_name);\r
272     const char *topicName;\r
273     if (jTopicName)\r
274     {\r
275         topicName = env->GetStringUTFChars( jTopicName, NULL);\r
276     }\r
277     else\r
278     {\r
279         LOGI("Info: topicName is null");\r
280     }\r
281     LOGD ("ConsumerService_getNativeTopicName - OUT");\r
282     return topicName;\r
283 \r
284 }\r
285 \r
286 OIC::Service::NSTopicsList *getNativeTopicsList(JNIEnv *env, jobject jTopicList)\r
287 {\r
288     LOGD ("ConsumerService_getNativeTopicsList - IN");\r
289 \r
290     jclass cls_topicList = env->GetObjectClass( jTopicList);\r
291     if (cls_topicList == NULL)\r
292     {\r
293         LOGE("Error: Class for Topic List  is null");\r
294         return nullptr;\r
295     }\r
296     jfieldID fid_list = env->GetFieldID( cls_topicList, "mTopicsList", "Ljava/util/Vector;");\r
297     if (fid_list == NULL)\r
298     {\r
299         LOGE("Error: jfieldID for Topic List  is null");\r
300         return nullptr;\r
301     }\r
302     jobject jobj = env->GetObjectField( jTopicList, fid_list);\r
303     if (jobj == NULL)\r
304     {\r
305         LOGE("Error: object of field  Topic List is null");\r
306         return nullptr;\r
307     }\r
308     jclass cls_vec = env->FindClass("java/util/Vector");\r
309     if (cls_vec == NULL)\r
310     {\r
311         LOGE("Error: Class for Vector not found");\r
312         return nullptr;\r
313     }\r
314     jmethodID sizeMethod = env->GetMethodID(cls_vec,  "size", "()I");\r
315     if (sizeMethod == NULL)\r
316     {\r
317         LOGE("Error: MethodId for Vector Size  not found");\r
318         return nullptr;\r
319     }\r
320     int size = env->CallIntMethod(jobj, sizeMethod);\r
321     jmethodID getMethod = env->GetMethodID(cls_vec,  "get", "(I)Ljava/lang/Object;");\r
322     if (getMethod == NULL)\r
323     {\r
324         LOGE("Error: MethodId for Vector get  not found");\r
325         return nullptr;\r
326     }\r
327     OIC::Service::NSTopicsList *nsTopicList = new OIC::Service::NSTopicsList();\r
328     for (int index = 0; index < size; index++)\r
329     {\r
330         jobject topicObj = env->CallObjectMethod(jobj, getMethod, index);\r
331         if (topicObj == NULL)\r
332         {\r
333             LOGE("Error: object of field  Topic  is null");\r
334             return nullptr;\r
335         }\r
336         const char *name =  getNativeTopicName(env, topicObj);\r
337         if (name == nullptr)\r
338         {\r
339             LOGE("Error: Couldn't find topic Name");\r
340             return nullptr;\r
341         }\r
342         std::string topicName(name);\r
343         OIC::Service::NSTopic::NSTopicState state;\r
344         if (!getNativeTopicState(env, topicObj, state))\r
345         {\r
346             return nullptr;\r
347         }\r
348         nsTopicList->addTopic(topicName, state);\r
349     }\r
350 \r
351     env->DeleteLocalRef(cls_vec);\r
352     env->DeleteLocalRef(cls_topicList);\r
353     LOGD ("ConsumerService_getNativeTopicsList - OUT");\r
354     return nsTopicList;\r
355 }\r
356 \r
357 OIC::Service::NSProvider *getNativeProvider(JNIEnv *env, jobject jObj)\r
358 {\r
359     LOGD ("ConsumerService_getNativeProvider - IN");\r
360     jclass providerClass = env->GetObjectClass(jObj);\r
361     if (!providerClass)\r
362     {\r
363         ThrowNSException(NS_ERROR, "Failed to Get ObjectClass for Provider");\r
364         return NULL;\r
365     }\r
366     jfieldID jproviderId = env->GetFieldID(providerClass, "mProviderId", "Ljava/lang/String;");\r
367     if (!jproviderId)\r
368     {\r
369         ThrowNSException(NS_ERROR, "Failed to get providerId for Provider");\r
370         return NULL;\r
371     }\r
372     jstring jprovider_id = (jstring) env->GetObjectField(jObj, jproviderId);\r
373     if (!jprovider_id)\r
374     {\r
375         ThrowNSException(NS_ERROR, "ProviderId cannot be null");\r
376         return NULL;\r
377     }\r
378 \r
379     const char *providerId = env->GetStringUTFChars(jprovider_id, 0);\r
380     LOGD ("ProviderId : %s\n", providerId);\r
381 \r
382     OIC::Service::NSProvider *provider =\r
383         OIC::Service::NSConsumerService::getInstance()->getProvider(std::string(providerId));\r
384     if (provider == nullptr)\r
385     {\r
386         ThrowNSException(NS_ERROR, "Provider with Given Id doesn't exist");\r
387         return NULL;\r
388     }\r
389     env->ReleaseStringUTFChars(jprovider_id, providerId);\r
390     LOGD ("ConsumerService_getNativeProvider - OUT");\r
391     return provider;\r
392 }\r
393 \r
394 jobject getJavaProvider(JNIEnv *env, OIC::Service::NSProvider *provider)\r
395 {\r
396     LOGD ("ConsumerService_getJavaProvider - IN");\r
397     LOGD ("ProviderId : %s\n", provider->getProviderId().c_str());\r
398 \r
399     jstring jProviderId = env->NewStringUTF(provider->getProviderId().c_str());\r
400     jlong pProvider = (long)provider;\r
401 \r
402     jclass cls_provider = (jclass) (env->NewLocalRef(g_cls_Provider));\r
403     if (!cls_provider)\r
404     {\r
405         LOGE ("Failed to Get ObjectClass for Provider");\r
406         return NULL;\r
407     }\r
408     jmethodID mid_provider = env->GetMethodID(\r
409                                  cls_provider, "<init>", "(Ljava/lang/String;)V");\r
410     if (!mid_provider)\r
411     {\r
412         LOGE ("Failed to Get MethodID for Provider<init>");\r
413         return NULL;\r
414     }\r
415     jobject obj_provider = env->NewObject(cls_provider, mid_provider, jProviderId);\r
416     if (!obj_provider)\r
417     {\r
418         LOGE("Failed to create new Object for Provider");\r
419         return NULL;\r
420     }\r
421 \r
422     jfieldID nativeHandle = env->GetFieldID(cls_provider, "mNativeHandle", "J");\r
423     if (!nativeHandle)\r
424     {\r
425         LOGE("Failed to get nativeHandle for Provider");\r
426         return NULL;\r
427     }\r
428     env->SetLongField(obj_provider, nativeHandle, pProvider);\r
429 \r
430     jfieldID fid_topic = env->GetFieldID(cls_provider, "mTopicsList",\r
431                                          "Lorg/iotivity/service/ns/common/TopicsList;");\r
432     if (!fid_topic)\r
433     {\r
434         LOGE("Failed to get TopicList for Provider");\r
435         return NULL;\r
436     }\r
437     jobject topicList = getJavaTopicsList(env, provider->getTopicList());\r
438     env->SetObjectField(obj_provider, fid_topic, topicList);\r
439     env->DeleteLocalRef(cls_provider);\r
440     LOGD ("ConsumerService_getJavaProvider - OUT");\r
441     return obj_provider;\r
442 }\r
443 \r
444 jobject getJavaMessageType(JNIEnv *env, OIC::Service::NSMessage::NSMessageType type)\r
445 {\r
446     LOGD ("ConsumerService_getJavaMessageType - IN");\r
447     switch (type)\r
448     {\r
449         case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_ALERT:\r
450             {\r
451                 static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,\r
452                                           "ALERT", "Lorg/iotivity/service/ns/common/Message$MessageType;");\r
453                 return env->GetStaticObjectField(g_cls_Message_Type, fieldID);\r
454             }\r
455         case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_NOTICE:\r
456             {\r
457                 static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,\r
458                                           "NOTICE", "Lorg/iotivity/service/ns/common/Message$MessageType;");\r
459                 return env->GetStaticObjectField(g_cls_Message_Type, fieldID);\r
460             }\r
461         case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_EVENT:\r
462             {\r
463                 static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,\r
464                                           "EVENT", "Lorg/iotivity/service/ns/common/Message$MessageType;");\r
465                 return env->GetStaticObjectField(g_cls_Message_Type, fieldID);\r
466             }\r
467         case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_INFO:\r
468             {\r
469                 static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,\r
470                                           "INFO", "Lorg/iotivity/service/ns/common/Message$MessageType;");\r
471                 return env->GetStaticObjectField(g_cls_Message_Type, fieldID);\r
472             }\r
473         default:\r
474             return NULL;\r
475     }\r
476     LOGD ("ConsumerService_getJavaMessageType - OUT");\r
477     return NULL;\r
478 }\r
479 \r
480 jobject getJavaMessage(JNIEnv *env, OIC::Service::NSMessage *message)\r
481 {\r
482     LOGD ("ConsumerService_getJavaMessage - IN");\r
483 \r
484     LOGD ("id : %llu\n", message->getMessageId());\r
485     LOGD ("title : %s\n", message->getTitle().c_str());\r
486     LOGD ("content : %s\n", message->getContentText().c_str());\r
487     LOGD ("source : %s\n", message->getSourceName().c_str());\r
488 \r
489     jlong jMessageId = (jlong) message->getMessageId();\r
490     jstring jProviderId = env->NewStringUTF(message->getProviderId().c_str());\r
491     jstring jTitle = env->NewStringUTF(message->getTitle().c_str());\r
492     jstring jContentText = env->NewStringUTF(message->getContentText().c_str());\r
493     jstring jSourceName = env->NewStringUTF(message->getSourceName().c_str());\r
494     jstring jTopic = env->NewStringUTF(message->getTopic().c_str());\r
495 \r
496     jstring jTime = env->NewStringUTF(message->getTime().c_str());\r
497     jlong jTTL = (jlong) message->getTTL();\r
498 \r
499     jlong pMessage = (long) message;\r
500 \r
501     jclass cls_message = (jclass) (env->NewLocalRef(g_cls_Message));\r
502     if (!cls_message)\r
503     {\r
504         LOGE ("Failed to Get ObjectClass for Message");\r
505         return NULL ;\r
506     }\r
507     jmethodID mid_message = env->GetMethodID(\r
508                                 cls_message, "<init>",\r
509                                 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");\r
510     if (!mid_message)\r
511     {\r
512         LOGE ("Failed to Get MethodID for Message<init>");\r
513         return NULL;\r
514     }\r
515     jobject obj_message = env->NewObject(cls_message, mid_message,\r
516                                          jTitle, jContentText, jSourceName);\r
517     if (!obj_message)\r
518     {\r
519         LOGE ("Failed to Get Java Object for Message");\r
520         return NULL;\r
521     }\r
522     jfieldID fid_nativeHandle = env->GetFieldID(cls_message, "mNativeHandle", "J");\r
523     if (!fid_nativeHandle)\r
524     {\r
525         LOGE("Failed to get nativeHandle for Message");\r
526         return NULL;\r
527     }\r
528     env->SetLongField(obj_message, fid_nativeHandle, pMessage);\r
529 \r
530     jfieldID fid_messageId = env->GetFieldID(cls_message, "mMessageId", "J");\r
531     if (!fid_messageId)\r
532     {\r
533         LOGE("Failed to get field MessageID for Message");\r
534         return NULL;\r
535     }\r
536     env->SetLongField(obj_message, fid_messageId, jMessageId);\r
537 \r
538     jfieldID fid_providerId = env->GetFieldID(cls_message, "mProviderId", "Ljava/lang/String;");\r
539     if (!fid_providerId)\r
540     {\r
541         LOGE("Failed to get field ProviderID for Message");\r
542         return NULL;\r
543     }\r
544     env->SetObjectField(obj_message, fid_providerId, jProviderId);\r
545 \r
546     jfieldID fid_time = env->GetFieldID(cls_message, "mTime", "Ljava/lang/String;");\r
547     if (!fid_time)\r
548     {\r
549         LOGE("Failed to get field Time for Message");\r
550         return NULL;\r
551     }\r
552     env->SetObjectField(obj_message, fid_time, jTime);\r
553 \r
554     jfieldID fid_ttl = env->GetFieldID(cls_message, "mTTL", "J");\r
555     if (!fid_ttl)\r
556     {\r
557         LOGE("Failed to get field TTL for Message");\r
558         return NULL;\r
559     }\r
560     env->SetLongField(obj_message, fid_ttl, jTTL);\r
561 \r
562     jfieldID fid_topic = env->GetFieldID(cls_message, "mTopic", "Ljava/lang/String;");\r
563     if (!fid_topic)\r
564     {\r
565         LOGE("Failed to get mTopic for Message");\r
566         return NULL;\r
567     }\r
568     env->SetObjectField(obj_message, fid_topic, jTopic);\r
569 \r
570     OIC::Service::NSMediaContents *mediaCont = message->getMediaContents();\r
571     if (mediaCont != nullptr)\r
572     {\r
573         jstring jIconImage = env->NewStringUTF(mediaCont->getIconImage().c_str());\r
574         jclass cls_mediaContents = (jclass) (env->NewLocalRef(g_cls_MediaContents));\r
575         if (!cls_mediaContents)\r
576         {\r
577             LOGE ("Failed to Get ObjectClass for MediaContents");\r
578             return NULL;\r
579         }\r
580         jmethodID mid_mediaContents = env->GetMethodID(\r
581                                           cls_mediaContents, "<init>", "(Ljava/lang/String;)V");\r
582         if (!mid_mediaContents)\r
583         {\r
584             LOGE ("Failed to Get MethodID for MediaContents<init>");\r
585             return NULL;\r
586         }\r
587         jobject obj_mediaContents = env->NewObject(cls_mediaContents, mid_mediaContents,\r
588                                     jIconImage);\r
589 \r
590         jfieldID fid_mediaContents = env->GetFieldID(cls_message, "mMediaContents",\r
591                                      "Lorg/iotivity/service/ns/common/MediaContents;");\r
592         if (!fid_mediaContents)\r
593         {\r
594             LOGE("Failed to get field mediaContents for Message");\r
595             return NULL;\r
596         }\r
597         env->SetObjectField(obj_message, fid_mediaContents, obj_mediaContents);\r
598 \r
599     }\r
600 \r
601     jobject jType = getJavaMessageType(env, message->getType());\r
602     if (jType)\r
603     {\r
604         jfieldID fid_type = env->GetFieldID(cls_message, "mType",\r
605                                             "Lorg/iotivity/service/ns/common/Message$MessageType;");\r
606         if (!fid_type)\r
607         {\r
608             LOGE("Failed to get field Type for Message");\r
609             return NULL;\r
610         }\r
611         env->SetObjectField(obj_message, fid_type, jType);\r
612     }\r
613 \r
614     env->DeleteLocalRef(cls_message);\r
615     LOGD ("ConsumerService_getJavaMessage - OUT");\r
616     return obj_message;\r
617 }\r
618 \r
619 void onDiscoverProvider(OIC::Service::NSProvider *provider)\r
620 {\r
621     LOGD ("ConsumerService_onDiscoverProvider - IN");\r
622 \r
623     jint envRet;\r
624     JNIEnv *env = GetJNIEnv(&envRet);\r
625     if (NULL == env) return ;\r
626 \r
627     jobject jDiscoverListener = (jobject) env->NewLocalRef(g_obj_discoverListener);\r
628     if (!jDiscoverListener)\r
629     {\r
630         LOGE ("Failed to Get jDiscoverListener");\r
631         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
632         return ;\r
633     }\r
634 \r
635     jobject obj_provider = getJavaProvider(env, provider);\r
636     if (!obj_provider)\r
637     {\r
638         LOGE ("Failed to Get Provider Object");\r
639         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
640         return ;\r
641     }\r
642 \r
643     jclass cls = env->GetObjectClass(jDiscoverListener);\r
644     if (!cls)\r
645     {\r
646         LOGE ("Failed to Get ObjectClass for jDiscoverListener");\r
647         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
648         return ;\r
649     }\r
650     jmethodID mid = env->GetMethodID(\r
651                         cls,\r
652                         "onProviderDiscovered",\r
653                         "(Lorg/iotivity/service/ns/consumer/Provider;)V");\r
654     if (!mid)\r
655     {\r
656         LOGE ("Failed to Get MethodID for onProviderDiscovered");\r
657         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
658         return ;\r
659     }\r
660 \r
661     env->CallVoidMethod(jDiscoverListener, mid, obj_provider);\r
662 \r
663     env->DeleteLocalRef(jDiscoverListener);\r
664     if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
665     LOGD ("ConsumerService_onDiscoverProvider - OUT");\r
666     return ;\r
667 }\r
668 \r
669 void onProviderChanged(OIC::Service::NSProvider *provider, OIC::Service::NSResponse response)\r
670 {\r
671     LOGD ("ConsumerService_onProviderChanged -IN");\r
672 \r
673     jint envRet;\r
674     JNIEnv *env = GetJNIEnv(&envRet);\r
675     if (NULL == env) return ;\r
676 \r
677     jobject jAcceptListener = (jobject) env->NewLocalRef(g_obj_acceptListener);\r
678     if (!jAcceptListener)\r
679     {\r
680         LOGE ("Failed to Get jAcceptListener");\r
681         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
682         return ;\r
683     }\r
684     jobject obj_provider = getJavaProvider(env, provider);\r
685     if (!obj_provider)\r
686     {\r
687         LOGE ("Failed to Get Provider Object");\r
688         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
689         return ;\r
690     }\r
691 \r
692     jclass cls = env->GetObjectClass(jAcceptListener);\r
693     if (!cls)\r
694     {\r
695         LOGE ("Failed to Get ObjectClass for jAcceptListener");\r
696         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
697         return ;\r
698     }\r
699     jmethodID mid = env->GetMethodID(\r
700                         cls,\r
701                         "onProviderChanged",\r
702                         "(Lorg/iotivity/service/ns/consumer/Provider;Lorg/iotivity/service/ns/consumer/ConsumerService$Response;)V");\r
703     if (!mid)\r
704     {\r
705         LOGE ("Failed to Get MethodID for onProviderChanged");\r
706         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
707         return ;\r
708     }\r
709     jobject obj_response = getJavaResponse(env, response);\r
710     env->CallVoidMethod(jAcceptListener, mid, obj_provider, obj_response);\r
711 \r
712     env->DeleteLocalRef(jAcceptListener);\r
713     if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
714     LOGD ("ConsumerService_onProviderChanged -OUT");\r
715     return ;\r
716 \r
717 }\r
718 \r
719 void onMessagePosted(OIC::Service::NSMessage *message)\r
720 {\r
721     LOGD ("ConsumerService_onMessagePosted -IN");\r
722 \r
723     jint envRet;\r
724     JNIEnv *env = GetJNIEnv(&envRet);\r
725     if (NULL == env) return ;\r
726 \r
727     jobject jPostListener = (jobject) env->NewLocalRef(g_obj_postListener);\r
728     if (!jPostListener)\r
729     {\r
730         LOGE ("Failed to Get jPostListener");\r
731         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
732         return ;\r
733     }\r
734 \r
735     jobject obj_message = getJavaMessage( env, message);\r
736     if (!obj_message)\r
737     {\r
738         LOGE ("Failed to Get Message Object");\r
739         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
740         return ;\r
741     }\r
742     jclass cls = env->GetObjectClass(jPostListener);\r
743     if (!cls)\r
744     {\r
745         LOGE ("Failed to Get ObjectClass for jPostListener");\r
746         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
747         return ;\r
748     }\r
749     jmethodID mid = env->GetMethodID(\r
750                         cls,\r
751                         "onMessageReceived",\r
752                         "(Lorg/iotivity/service/ns/common/Message;)V");\r
753     if (!mid)\r
754     {\r
755         LOGE ("Failed to Get MethodID for onMessageReceived");\r
756         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
757         return ;\r
758     }\r
759 \r
760     env->CallVoidMethod(jPostListener, mid, obj_message);\r
761 \r
762     env->DeleteLocalRef(jPostListener);\r
763     if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
764     LOGD ("ConsumerService_onMessagePosted -OUT");\r
765     return ;\r
766 }\r
767 \r
768 void onSyncInfoReceived(OIC::Service::NSSyncInfo *sync)\r
769 {\r
770     LOGD ("ConsumerService_onSyncInfoReceived - IN");\r
771 \r
772     jint envRet;\r
773     JNIEnv *env = GetJNIEnv(&envRet);\r
774     if (NULL == env) return ;\r
775 \r
776     jobject jSyncListener = (jobject) env->NewLocalRef(g_obj_syncListener);\r
777     if (!jSyncListener)\r
778     {\r
779         LOGE ("Failed to Get jSyncListener");\r
780         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
781         return ;\r
782     }\r
783 \r
784     LOGD ("Sync ID : %llu\n", sync->getMessageId());\r
785     LOGD ("Sync ProviderId : %s\n", sync->getProviderId().c_str());\r
786     LOGD ("Sync STATE : %d\n", (int) sync->getState());\r
787 \r
788     jlong jMessageId = (jlong) sync->getMessageId();\r
789     jstring jProviderId = env->NewStringUTF(sync->getProviderId().c_str());\r
790     jobject syncType = getJavaSyncType(env, sync->getState());\r
791     if (!syncType)\r
792     {\r
793         LOGE ("Failed to Get syncType for SyncInfo");\r
794         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
795         return ;\r
796     }\r
797     jclass cls_SyncInfo = (jclass) (env->NewLocalRef(g_cls_SyncInfo));\r
798     if (!cls_SyncInfo)\r
799     {\r
800         LOGE ("Failed to Get ObjectClass for SyncInfo");\r
801         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
802         return ;\r
803     }\r
804     jmethodID mid_syncInfo = env->GetMethodID(\r
805                                  cls_SyncInfo,\r
806                                  "<init>",\r
807                                  "(JLjava/lang/String;Lorg/iotivity/service/ns/common/SyncInfo$SyncType;)V");\r
808     if (!mid_syncInfo)\r
809     {\r
810         LOGE ("Failed to Get MethodID for SyncInfo");\r
811         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
812         return ;\r
813     }\r
814 \r
815     jobject obj_syncInfo = env->NewObject( cls_SyncInfo, mid_syncInfo,\r
816                                            jMessageId, jProviderId, syncType);\r
817     if (!obj_syncInfo)\r
818     {\r
819         LOGE ("Failed to Get Object for SyncInfo");\r
820         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
821         return ;\r
822     }\r
823 \r
824     jclass cls = env->GetObjectClass(jSyncListener);\r
825     if (!cls)\r
826     {\r
827         LOGE ("Failed to Get ObjectClass for jSyncListener");\r
828         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
829         return ;\r
830     }\r
831     jmethodID mid = env->GetMethodID(\r
832                         cls,\r
833                         "onSyncInfoReceived",\r
834                         "(Lorg/iotivity/service/ns/common/SyncInfo;)V");\r
835     if (!mid)\r
836     {\r
837         LOGE ("Failed to Get MethodID for onSyncInfoReceived");\r
838         if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
839         return ;\r
840     }\r
841 \r
842     env->CallVoidMethod(jSyncListener, mid, obj_syncInfo);\r
843 \r
844     env->DeleteLocalRef(jSyncListener);\r
845     env->DeleteLocalRef(cls_SyncInfo);\r
846     if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();\r
847     LOGD ("ConsumerService_onSyncInfoReceived - OUT");\r
848     return ;\r
849 }\r
850 \r
851 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_consumer_ConsumerService_nativeStart\r
852 (JNIEnv *env, jobject jObj, jobject jDiscoverListener, jobject jAccpetListener)\r
853 {\r
854     LOGD ("ConsumerService_StartConsumer - IN");\r
855     if (!jDiscoverListener || !jAccpetListener)\r
856     {\r
857         ThrowNSException(NS_ERROR, "Listener cannot be null");\r
858         return ;\r
859     }\r
860 \r
861     if (g_obj_discoverListener != NULL)\r
862     {\r
863         env->DeleteGlobalRef(g_obj_discoverListener);\r
864     }\r
865     if (g_obj_acceptListener != NULL)\r
866     {\r
867         env->DeleteGlobalRef(g_obj_acceptListener);\r
868     }\r
869 \r
870     g_obj_discoverListener = (jobject) env->NewGlobalRef(jDiscoverListener);\r
871     g_obj_acceptListener = (jobject) env->NewGlobalRef(jAccpetListener);\r
872 \r
873     OIC::Service::NSConsumerService::ConsumerConfig cfg;\r
874     cfg.m_discoverCb = onDiscoverProvider;\r
875     cfg.m_changedCb = onProviderChanged;\r
876 \r
877     OIC::Service::NSConsumerService::getInstance()->Start(cfg);\r
878     LOGD ("ConsumerService_StartConsumer - OUT");\r
879     return;\r
880 \r
881 }\r
882 \r
883 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_consumer_ConsumerService_nativeStop\r
884 (JNIEnv *env, jobject jObj)\r
885 {\r
886     LOGD ("ConsumerService_StopConsumer - IN");\r
887 \r
888     env->DeleteGlobalRef(g_obj_postListener);\r
889     env->DeleteGlobalRef(g_obj_discoverListener);\r
890     env->DeleteGlobalRef(g_obj_syncListener);\r
891     env->DeleteGlobalRef(g_obj_acceptListener);\r
892     g_obj_postListener = NULL;\r
893     g_obj_discoverListener = NULL;\r
894     g_obj_syncListener = NULL;\r
895     g_obj_acceptListener = NULL;\r
896     OIC::Service::NSConsumerService::getInstance()->Stop();\r
897     LOGD ("ConsumerService_StopConsumer - OUT");\r
898     return;\r
899 }\r
900 \r
901 JNIEXPORT jint JNICALL\r
902 Java_org_iotivity_service_ns_consumer_ConsumerService_nativeEnableRemoteService\r
903 (JNIEnv *env, jobject jObj, jstring jServerAddress)\r
904 {\r
905     LOGD ("ConsumerService_EnableRemoteService - IN");\r
906     if (!jServerAddress)\r
907     {\r
908         ThrowNSException(NS_ERROR, "EnableRemoteService server address NULL");\r
909         return (jint) OIC::Service::NSResult::ERROR;\r
910     }\r
911     const char *serverAddress = env->GetStringUTFChars(jServerAddress, 0);\r
912     OIC::Service::NSResult res =\r
913         OIC::Service::NSConsumerService::getInstance()->EnableRemoteService(std::string(serverAddress));\r
914     env->ReleaseStringUTFChars(jServerAddress, serverAddress);\r
915     LOGD ("ConsumerService_EnableRemoteService - OUT");\r
916     return (jint) res;\r
917 }\r
918 \r
919 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_consumer_ConsumerService_nativeRescanProvider\r
920 (JNIEnv *env, jobject jObj)\r
921 {\r
922     LOGD ("ConsumerService_RescanProvider - IN");\r
923     OIC::Service::NSConsumerService::getInstance()->RescanProvider();\r
924     LOGD ("ConsumerService_RescanProvider - OUT");\r
925     return;\r
926 \r
927 }\r
928 \r
929 JNIEXPORT jobject JNICALL Java_org_iotivity_service_ns_consumer_ConsumerService_nativeGetProvider\r
930 (JNIEnv *env, jobject jObj, jstring jProviderId)\r
931 {\r
932     LOGD ("ConsumerService_GetProvider - IN");\r
933     if (!jProviderId)\r
934     {\r
935         ThrowNSException(NS_ERROR, "ProviderId cannot be null");\r
936         return NULL;\r
937     }\r
938 \r
939     const char *providerId = env->GetStringUTFChars(jProviderId, 0);\r
940     LOGD ("java_ProviderId : %s\n", providerId);\r
941 \r
942     OIC::Service::NSProvider *provider =\r
943         OIC::Service::NSConsumerService::getInstance()->getProvider(std::string(providerId));\r
944     if (provider == nullptr)\r
945     {\r
946         ThrowNSException(NS_ERROR, "Provider with Given Id doesn't exist");\r
947         return NULL;\r
948     }\r
949     LOGD ("native_ProviderId : %s\n", provider->getProviderId().c_str());\r
950 \r
951     jProviderId = env->NewStringUTF(provider->getProviderId().c_str());\r
952     jlong pProvider = (long)provider;\r
953 \r
954     jclass cls_provider = (jclass) (env->NewLocalRef(g_cls_Provider));\r
955     if (!cls_provider)\r
956     {\r
957         LOGE ("Failed to Get ObjectClass for Provider");\r
958         ThrowNSException(NS_ERROR, "Couldn't find objectClass for Provider");\r
959         return NULL;\r
960     }\r
961     jmethodID mid_provider = env->GetMethodID(\r
962                                  cls_provider, "<init>", "(Ljava/lang/String;)V");\r
963     if (!mid_provider)\r
964     {\r
965         LOGE ("Failed to Get MethodID for Provider<init>");\r
966         ThrowNSException(NS_ERROR, "Couldn't find MethodID for Provider<init>");\r
967         return NULL;\r
968     }\r
969     jobject obj_provider = env->NewObject(cls_provider, mid_provider,\r
970                                           jProviderId);\r
971 \r
972     jfieldID nativeHandle = env->GetFieldID(cls_provider, "mNativeHandle", "J");\r
973     if (!nativeHandle)\r
974     {\r
975         ThrowNSException(NS_ERROR, "Failed to get nativeHandle for Provider");\r
976         return NULL;\r
977     }\r
978     env->SetLongField(obj_provider, nativeHandle, pProvider);\r
979 \r
980     env->DeleteLocalRef(cls_provider);\r
981     env->ReleaseStringUTFChars(jProviderId, providerId);\r
982     LOGD ("ConsumerService_GetProvider - OUT");\r
983     return obj_provider;\r
984 }\r
985 \r
986 JNIEXPORT jobject JNICALL Java_org_iotivity_service_ns_consumer_ConsumerService_nativeGetMessage\r
987 (JNIEnv *env, jobject jObj, jlong jMsgId)\r
988 {\r
989     LOGI("JNIConsumerService: nativeGetMessage - IN");\r
990 \r
991     OIC::Service::NSMessage *msg  =\r
992         OIC::Service::NSConsumerService::getInstance()->getMessage((uint64_t)jMsgId);\r
993     if (msg == nullptr)\r
994     {\r
995         ThrowNSException(NS_ERROR, "Message doesn't exist");\r
996         return NULL;\r
997     }\r
998 \r
999     jobject obj_msg = getJavaMessage(env, msg);\r
1000 \r
1001     LOGI("JNIConsumerService: nativeGetMessage - OUT");\r
1002     return obj_msg;\r
1003 }\r
1004 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_consumer_Provider_nativeSubscribe\r
1005 (JNIEnv *env, jobject jObj)\r
1006 {\r
1007     LOGD ("Provider_Subscribe -IN");\r
1008     jclass providerClass = env->GetObjectClass(jObj);\r
1009     if (!providerClass)\r
1010     {\r
1011         ThrowNSException(NS_ERROR, "Failed to Get ObjectClass for Provider");\r
1012         return ;\r
1013     }\r
1014 \r
1015     jfieldID nativeHandle = env->GetFieldID(providerClass, "mNativeHandle", "J");\r
1016     if (!nativeHandle)\r
1017     {\r
1018         ThrowNSException(NS_ERROR, "Failed to get nativeHandle for Provider");\r
1019         return ;\r
1020     }\r
1021     jlong jProvider = env->GetLongField(jObj, nativeHandle);\r
1022     if (jProvider)\r
1023     {\r
1024         LOGD ("calling subscribe on mNativeHandle");\r
1025         OIC::Service::NSProvider *provider = (OIC::Service::NSProvider *) (jProvider);\r
1026         provider->subscribe();\r
1027     }\r
1028     else\r
1029     {\r
1030         OIC::Service::NSProvider *provider = getNativeProvider(env, jObj);\r
1031         if (provider == nullptr)\r
1032         {\r
1033             ThrowNSException(NS_ERROR, "Provider with Given Id doesn't exist");\r
1034             return;\r
1035         }\r
1036         LOGD ("calling subscribe on ProviderID");\r
1037         provider->subscribe();\r
1038     }\r
1039     LOGD ("Provider_Subscribe -OUT");\r
1040     return;\r
1041 }\r
1042 \r
1043 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_consumer_Provider_nativeUnsubscribe\r
1044 (JNIEnv *env, jobject jObj)\r
1045 {\r
1046     LOGD ("Provider_Unsubscribe - IN");\r
1047 \r
1048     jclass providerClass = env->GetObjectClass(jObj);\r
1049     if (!providerClass)\r
1050     {\r
1051         ThrowNSException(NS_ERROR, "Failed to Get ObjectClass for Provider");\r
1052         return ;\r
1053     }\r
1054 \r
1055     jfieldID nativeHandle = env->GetFieldID(providerClass, "mNativeHandle", "J");\r
1056     if (!nativeHandle)\r
1057     {\r
1058         ThrowNSException(NS_ERROR, "Failed to get nativeHandle for Provider");\r
1059         return ;\r
1060     }\r
1061     jlong jProvider = env->GetLongField(jObj, nativeHandle);\r
1062     if (jProvider)\r
1063     {\r
1064         LOGD ("calling unSubscribe on mNativeHandle");\r
1065         OIC::Service::NSProvider *provider = (OIC::Service::NSProvider *) (jProvider);\r
1066         provider->unSubscribe();\r
1067     }\r
1068     else\r
1069     {\r
1070         OIC::Service::NSProvider *provider = getNativeProvider(env, jObj);\r
1071         if (provider == nullptr)\r
1072         {\r
1073             ThrowNSException(NS_ERROR, "Provider with Given Id doesn't exist");\r
1074             return;\r
1075         }\r
1076         LOGD ("calling unSubscribe on ProviderID");\r
1077         provider->unSubscribe();\r
1078     }\r
1079     LOGD ("Provider_Unsubscribe - IN");\r
1080     return;\r
1081 }\r
1082 \r
1083 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_consumer_Provider_nativeSendSyncInfo\r
1084 (JNIEnv *env, jobject jObj, jlong jMessageId, jint jSyncType)\r
1085 {\r
1086     LOGD ("Provider_SendSyncInfo - IN");\r
1087     if (!jMessageId)\r
1088     {\r
1089         ThrowNSException(NS_ERROR, "MessageId cannot be 0");\r
1090         return ;\r
1091     }\r
1092 \r
1093     jclass providerClass = env->GetObjectClass(jObj);\r
1094     if (!providerClass)\r
1095     {\r
1096         ThrowNSException(NS_ERROR, "Failed to Get ObjectClass for Provider");\r
1097         return ;\r
1098     }\r
1099 \r
1100     jfieldID nativeHandle = env->GetFieldID(providerClass, "mNativeHandle", "J");\r
1101     if (!nativeHandle)\r
1102     {\r
1103         ThrowNSException(NS_ERROR, "Failed to get nativeHandle for Provider");\r
1104         return ;\r
1105     }\r
1106     uint64_t messageId = (uint64_t) jMessageId;\r
1107 \r
1108     LOGD("!!!!!!jMessageId: %lld", jMessageId);\r
1109     LOGD("!!!!!!messageId: %lld", messageId);\r
1110 \r
1111     jlong jProvider = env->GetLongField(jObj, nativeHandle);\r
1112     if (jProvider)\r
1113     {\r
1114         LOGD ("calling SendSyncInfo on mNativeHandle");\r
1115         OIC::Service::NSProvider *provider = (OIC::Service::NSProvider *) (jProvider);\r
1116         provider->SendSyncInfo(messageId, (OIC::Service::NSSyncInfo::NSSyncType)jSyncType);\r
1117     }\r
1118     else\r
1119     {\r
1120         OIC::Service::NSProvider *provider = getNativeProvider(env, jObj);\r
1121         if (provider == nullptr)\r
1122         {\r
1123             ThrowNSException(NS_ERROR, "Provider with Given Id doesn't exist");\r
1124             return;\r
1125         }\r
1126         LOGD ("calling SendSyncInfo on ProviderID");\r
1127         provider->SendSyncInfo(messageId, (OIC::Service::NSSyncInfo::NSSyncType)jSyncType);\r
1128     }\r
1129     LOGD ("Provider_SendSyncInfo - OUT");\r
1130     return;\r
1131 }\r
1132 \r
1133 \r
1134 JNIEXPORT void JNICALL Java_org_iotivity_service_ns_consumer_Provider_nativeSetListener\r
1135 (JNIEnv *env, jobject jObj, jobject jPostListener, jobject jSyncListener)\r
1136 {\r
1137     LOGD ("Provider_SetListener - IN");\r
1138     if (!jPostListener || !jSyncListener)\r
1139     {\r
1140         ThrowNSException(NS_ERROR, "Listener cannot be null");\r
1141         return ;\r
1142     }\r
1143 \r
1144     jclass providerClass = env->GetObjectClass(jObj);\r
1145     if (!providerClass)\r
1146     {\r
1147         ThrowNSException(NS_ERROR, "Failed to Get ObjectClass for Provider");\r
1148         return ;\r
1149     }\r
1150 \r
1151     jfieldID nativeHandle = env->GetFieldID(providerClass, "mNativeHandle", "J");\r
1152     if (!nativeHandle)\r
1153     {\r
1154         ThrowNSException(NS_ERROR, "Failed to get nativeHandle for Provider");\r
1155         return ;\r
1156     }\r
1157     jlong jProvider = env->GetLongField(jObj, nativeHandle);\r
1158     if (jProvider)\r
1159     {\r
1160         LOGD ("calling SetListener on mNativeHandle");\r
1161         if (g_obj_postListener != NULL)\r
1162         {\r
1163             env->DeleteGlobalRef(g_obj_postListener);\r
1164         }\r
1165         if (g_obj_syncListener != NULL)\r
1166         {\r
1167             env->DeleteGlobalRef(g_obj_syncListener);\r
1168         }\r
1169         g_obj_postListener = (jobject) env->NewGlobalRef(jPostListener);\r
1170         g_obj_syncListener = (jobject) env->NewGlobalRef(jSyncListener);\r
1171 \r
1172         OIC::Service::NSProvider *provider = (OIC::Service::NSProvider *) (jProvider);\r
1173         provider->setListener(onMessagePosted, onSyncInfoReceived);\r
1174     }\r
1175     else\r
1176     {\r
1177         OIC::Service::NSProvider *provider = getNativeProvider(env, jObj);\r
1178         if (provider == nullptr)\r
1179         {\r
1180             ThrowNSException(NS_ERROR, "Provider with Given Id doesn't exist");\r
1181             return;\r
1182         }\r
1183         LOGD ("calling SetListener on ProviderID");\r
1184         if (g_obj_postListener != NULL)\r
1185         {\r
1186             env->DeleteGlobalRef(g_obj_postListener);\r
1187         }\r
1188         if (g_obj_syncListener != NULL)\r
1189         {\r
1190             env->DeleteGlobalRef(g_obj_syncListener);\r
1191         }\r
1192         g_obj_postListener = (jobject) env->NewGlobalRef(jPostListener);\r
1193         g_obj_syncListener = (jobject) env->NewGlobalRef(jSyncListener);\r
1194 \r
1195         provider->setListener(onMessagePosted, onSyncInfoReceived);\r
1196     }\r
1197     LOGD ("Provider_SetListener - OUT");\r
1198     return;\r
1199 }\r
1200 \r
1201 JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_consumer_Provider_nativeSelectInterestTopics\r
1202 (JNIEnv *env, jobject jObj, jobject jTopicsList)\r
1203 {\r
1204     LOGD("Provider_SelectInterestTopics -IN");\r
1205     if (!jTopicsList)\r
1206     {\r
1207         LOGI("Fail to select Interest Topics - Topic List is null");\r
1208         ThrowNSException(NS_ERROR, "TopicList cannot be null");\r
1209         return (jint) OIC::Service::NSResult::ERROR;\r
1210     }\r
1211     OIC::Service::NSTopicsList *nsTopicsList = getNativeTopicsList(env, jTopicsList);\r
1212     if (nsTopicsList == nullptr)\r
1213     {\r
1214         ThrowNSException(NS_ERROR, "NSTopicList cannot be created ");\r
1215         return (jint) OIC::Service::NSResult::ERROR;\r
1216     }\r
1217 \r
1218     jclass providerClass = env->GetObjectClass(jObj);\r
1219     if (!providerClass)\r
1220     {\r
1221         ThrowNSException(NS_ERROR, "Failed to Get ObjectClass for Provider");\r
1222         return  (jint) OIC::Service::NSResult::ERROR;\r
1223     }\r
1224 \r
1225     jfieldID nativeHandle = env->GetFieldID(providerClass, "mNativeHandle", "J");\r
1226     if (!nativeHandle)\r
1227     {\r
1228         ThrowNSException(NS_ERROR, "Failed to get nativeHandle for Provider");\r
1229         return  (jint) OIC::Service::NSResult::ERROR;\r
1230     }\r
1231     jlong jProvider = env->GetLongField(jObj, nativeHandle);\r
1232     OIC::Service::NSResult result;\r
1233     if (jProvider)\r
1234     {\r
1235         LOGD ("calling subscribe on mNativeHandle");\r
1236         OIC::Service::NSProvider *provider = (OIC::Service::NSProvider *) (jProvider);\r
1237         result = provider->selectInterestTopics(nsTopicsList);\r
1238     }\r
1239     else\r
1240     {\r
1241         OIC::Service::NSProvider *provider = getNativeProvider(env, jObj);\r
1242         if (provider == nullptr)\r
1243         {\r
1244             ThrowNSException(NS_ERROR, "Provider with Given Id doesn't exist");\r
1245             return (jint) OIC::Service::NSResult::ERROR;\r
1246         }\r
1247         LOGD ("calling subscribe on ProviderID");\r
1248         result = provider->selectInterestTopics(nsTopicsList);\r
1249     }\r
1250     if (result !=  OIC::Service::NSResult::OK)\r
1251     {\r
1252         LOGI("Fail to select Interest Topics");\r
1253     }\r
1254     LOGD("Provider_SelectInterestTopics -OUT");\r
1255     return (jint) result;\r
1256 }\r
1257 // JNI OnLoad\r
1258 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved)\r
1259 {\r
1260     LOGD("ConsumerService_JNI_OnLoad");\r
1261     g_jvm = jvm;\r
1262 \r
1263     JNIEnv *env;\r
1264     if (jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)\r
1265     {\r
1266         LOGE("Failed to get the environment using GetEnv()");\r
1267         return JNI_ERR;\r
1268     }\r
1269 \r
1270     jclass localMessage = env->FindClass(\r
1271                               "org/iotivity/service/ns/common/Message");\r
1272     if (!localMessage)\r
1273     {\r
1274         LOGE("Failed to get local Message class");\r
1275         return JNI_ERR;\r
1276     }\r
1277     g_cls_Message = (jclass) (env->NewGlobalRef(localMessage));\r
1278     if (!g_cls_Message)\r
1279     {\r
1280         LOGE("Failed to set Global Message reference");\r
1281         return JNI_ERR;\r
1282     }\r
1283 \r
1284     jclass localProvider = env->FindClass(\r
1285                                "org/iotivity/service/ns/consumer/Provider");\r
1286     if (!localProvider)\r
1287     {\r
1288         LOGE("Failed to get local Provider class");\r
1289         return JNI_ERR;\r
1290     }\r
1291     g_cls_Provider = (jclass) (env->NewGlobalRef(localProvider));\r
1292     if (!g_cls_Provider)\r
1293     {\r
1294         LOGE("Failed to set Global Provider reference");\r
1295         return JNI_ERR;\r
1296     }\r
1297 \r
1298     jclass localSyncInfo = env->FindClass(\r
1299                                "org/iotivity/service/ns/common/SyncInfo");\r
1300     if (!localSyncInfo)\r
1301     {\r
1302         LOGE("Failed to get local SyncInfo class");\r
1303         return JNI_ERR;\r
1304     }\r
1305     g_cls_SyncInfo = (jclass) (env->NewGlobalRef(localSyncInfo));\r
1306     if (!g_cls_SyncInfo)\r
1307     {\r
1308         LOGE("Failed to set Global NSSyncInfo reference");\r
1309         return JNI_ERR;\r
1310     }\r
1311 \r
1312     jclass localSyncType = env->FindClass(\r
1313                                "org/iotivity/service/ns/common/SyncInfo$SyncType");\r
1314     if (!localSyncType)\r
1315     {\r
1316         LOGE("Failed to get local SyncType enum");\r
1317         return JNI_ERR;\r
1318     }\r
1319     g_cls_SyncType = (jclass) (env->NewGlobalRef(localSyncType));\r
1320     if (!g_cls_SyncType)\r
1321     {\r
1322         LOGE("Failed to set Global SyncType reference");\r
1323         return JNI_ERR;\r
1324     }\r
1325 \r
1326     jclass localMediaContents = env->FindClass(\r
1327                                     "org/iotivity/service/ns/common/MediaContents");\r
1328     if (!localMediaContents)\r
1329     {\r
1330         LOGE("Failed to get local MediaContents class");\r
1331         return JNI_ERR;\r
1332     }\r
1333     g_cls_MediaContents = (jclass) (env->NewGlobalRef(localMediaContents));\r
1334     if (!g_cls_MediaContents)\r
1335     {\r
1336         LOGE("Failed to set Global MediaContents reference");\r
1337         return JNI_ERR;\r
1338     }\r
1339     jclass localTopicState = env->FindClass(\r
1340                                  "org/iotivity/service/ns/common/Topic$TopicState");\r
1341     if (!localTopicState)\r
1342     {\r
1343         LOGE("Failed to get local TopicState enum");\r
1344         return JNI_ERR;\r
1345     }\r
1346     g_cls_TopicState = (jclass) (env->NewGlobalRef(localTopicState));\r
1347     if (!g_cls_TopicState)\r
1348     {\r
1349         LOGE("Failed to set Global TopicState reference");\r
1350         return JNI_ERR;\r
1351     }\r
1352 \r
1353     jclass localTopic = env->FindClass(\r
1354                             "org/iotivity/service/ns/common/Topic");\r
1355     if (!localTopic)\r
1356     {\r
1357         LOGE("Failed to get local TopicState enum");\r
1358         return JNI_ERR;\r
1359     }\r
1360     g_cls_Topic = (jclass) (env->NewGlobalRef(localTopic));\r
1361     if (!g_cls_Topic)\r
1362     {\r
1363         LOGE("Failed to set Global Topic reference");\r
1364         return JNI_ERR;\r
1365     }\r
1366 \r
1367     jclass localMessageType = env->FindClass(\r
1368                                   "org/iotivity/service/ns/common/Message$MessageType");\r
1369     if (!localMessageType)\r
1370     {\r
1371         LOGE("Failed to get local Message Type class");\r
1372         return JNI_ERR;\r
1373     }\r
1374     g_cls_Message_Type = (jclass) (env->NewGlobalRef(localMessageType));\r
1375     if (!g_cls_Message_Type)\r
1376     {\r
1377         LOGE("Failed to set Global Message Type reference");\r
1378         return JNI_ERR;\r
1379     }\r
1380 \r
1381     jclass localResponse = env->FindClass(\r
1382                                "org/iotivity/service/ns/consumer/ConsumerService$Response");\r
1383     if (!localResponse)\r
1384     {\r
1385         LOGE("Failed to get localResponse  Type class");\r
1386         return JNI_ERR;\r
1387     }\r
1388     g_cls_Response = (jclass) (env->NewGlobalRef(localResponse));\r
1389     if (!g_cls_Response)\r
1390     {\r
1391         LOGE("Failed to set Global Response Type reference");\r
1392         return JNI_ERR;\r
1393     }\r
1394 \r
1395     jclass localTopicsList = env->FindClass(\r
1396                                  "org/iotivity/service/ns/common/TopicsList");\r
1397     if (!localTopicsList)\r
1398     {\r
1399         LOGE("Failed to get local Topic class");\r
1400         return JNI_ERR;\r
1401     }\r
1402     g_cls_TopicsList = (jclass) (env->NewGlobalRef(localTopicsList));\r
1403     if (!g_cls_TopicsList)\r
1404     {\r
1405         LOGE("Failed to set Global TopicsList reference");\r
1406         return JNI_ERR;\r
1407     }\r
1408     env->DeleteLocalRef(localMessage);\r
1409     env->DeleteLocalRef(localProvider);\r
1410     env->DeleteLocalRef(localSyncInfo);\r
1411     env->DeleteLocalRef(localSyncType);\r
1412     env->DeleteLocalRef(localMediaContents);\r
1413     env->DeleteLocalRef(localTopicState);\r
1414     env->DeleteLocalRef(localMessageType);\r
1415     env->DeleteLocalRef(localResponse);\r
1416     env->DeleteLocalRef(localTopic);\r
1417     env->DeleteLocalRef(localTopicsList);\r
1418 \r
1419     return NSExceptionInit(env);\r
1420 }\r
1421 \r
1422 JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *jvm, void *reserved)\r
1423 {\r
1424     LOGI("ConsumerService_JNI_OnUnload");\r
1425     JNIEnv *env;\r
1426 \r
1427     if (jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK)\r
1428     {\r
1429         LOGE("Failed to get the environment using GetEnv()");\r
1430         return ;\r
1431     }\r
1432 \r
1433     env->DeleteGlobalRef(g_cls_Message);\r
1434     env->DeleteGlobalRef(g_cls_Provider);\r
1435     env->DeleteGlobalRef(g_cls_SyncInfo);\r
1436     env->DeleteGlobalRef(g_cls_SyncType);\r
1437     env->DeleteGlobalRef(g_cls_MediaContents);\r
1438     env->DeleteGlobalRef(g_cls_TopicState);\r
1439     env->DeleteGlobalRef(g_cls_Message_Type);\r
1440     env->DeleteGlobalRef(g_cls_Response);\r
1441     env->DeleteGlobalRef(g_cls_Topic);\r
1442     env->DeleteGlobalRef(g_cls_TopicsList);\r
1443 }\r