JniNotificationConsumer.cpp 53.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
//******************************************************************
//
// Copyright 2016 Samsung Electronics All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include "JniNotificationConsumer.h"
#include "NSConsumerService.h"
23
#include "NSException.h"
24
#include "JniOcRepresentation.h"
25

26
static JavaVM *g_jvm_consumer = NULL;
27 28 29

static jobject g_obj_postListener = NULL;
static jobject g_obj_syncListener = NULL;
nikhil.a's avatar
nikhil.a committed
30
static jobject g_obj_discoverListener = NULL;
31 32 33 34 35
static jobject g_obj_acceptListener = NULL;

jclass g_cls_Message;
jclass g_cls_Provider;
jclass g_cls_SyncInfo;
nikhil.a's avatar
nikhil.a committed
36
jclass g_cls_SyncType;
37
jclass g_cls_MediaContents;
38 39
jclass g_cls_TopicState;
jclass g_cls_Message_Type;
nikhil.a's avatar
nikhil.a committed
40
jclass g_cls_ProviderState;
41 42
jclass g_cls_Topic;
jclass g_cls_TopicsList;
43 44
jclass g_cls_OcRepresentation;
jmethodID g_mid_OcRepresentation_N_ctor_bool = NULL;
45 46 47 48 49

static JNIEnv *GetJNIEnv(jint *ret)
{
    JNIEnv *env = NULL;

50
    *ret = g_jvm_consumer->GetEnv((void **) &env, JNI_CURRENT_VERSION);
51 52 53 54 55
    switch (*ret)
    {
        case JNI_OK:
            return env;
        case JNI_EDETACHED:
56
            if (g_jvm_consumer->AttachCurrentThread(&env, NULL) != JNI_OK)
57
            {
58
                NS_LOGE ("Failed to get the environment");
59 60 61 62 63 64 65
                return NULL;
            }
            else
            {
                return env;
            }
        case JNI_EVERSION:
66
            NS_LOGE ("JNI version is not supported");
67
            return NULL;
68
        default:
69
            NS_LOGE ("Failed to get the environment");
70 71 72 73
            return NULL;
    }
}

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
static jlong getNativeProvider(JNIEnv *env, jobject jObj)
{

    jclass providerClass = env->GetObjectClass(jObj);
    if (!providerClass)
    {
        ThrowNSException(JNI_INVALID_VALUE, "Failed to Get ObjectClass for Provider");
        return 0;
    }
    jfieldID nativeHandle = env->GetFieldID(providerClass, "mNativeHandle", "J");
    if (!nativeHandle)
    {
        ThrowNSException(JNI_INVALID_VALUE, "Failed to get nativeHandle for Provider");
        return 0;
    }
    return (env->GetLongField(jObj, nativeHandle));
}

nikhil.a's avatar
nikhil.a committed
92
jobject getJavaProviderState(JNIEnv *env, OIC::Service::NSProviderState state)
93
{
94
    NS_LOGD ("ConsumerService_getJavaProviderState - IN");
95
    jobject providerState = NULL;
nikhil.a's avatar
nikhil.a committed
96
    switch (state)
97
    {
nikhil.a's avatar
nikhil.a committed
98 99 100 101
        case OIC::Service::NSProviderState::ALLOW:
            {
                static jfieldID fieldID = env->GetStaticFieldID(g_cls_ProviderState,
                                          "ALLOW", "Lorg/iotivity/service/ns/consumer/Provider$ProviderState;");
102 103
                providerState = env->GetStaticObjectField(g_cls_ProviderState, fieldID);
                break;
nikhil.a's avatar
nikhil.a committed
104 105
            }
        case OIC::Service::NSProviderState::DENY:
106
            {
nikhil.a's avatar
nikhil.a committed
107 108
                static jfieldID fieldID = env->GetStaticFieldID(g_cls_ProviderState,
                                          "DENY", "Lorg/iotivity/service/ns/consumer/Provider$ProviderState;");
109 110
                providerState = env->GetStaticObjectField(g_cls_ProviderState, fieldID);
                break;
111
            }
nikhil.a's avatar
nikhil.a committed
112
        case OIC::Service::NSProviderState::TOPIC:
113
            {
nikhil.a's avatar
nikhil.a committed
114 115
                static jfieldID fieldID = env->GetStaticFieldID(g_cls_ProviderState,
                                          "TOPIC", "Lorg/iotivity/service/ns/consumer/Provider$ProviderState;");
116 117
                providerState = env->GetStaticObjectField(g_cls_ProviderState, fieldID);
                break;
118
            }
nikhil.a's avatar
nikhil.a committed
119
        case OIC::Service::NSProviderState::STOPPED:
120
            {
nikhil.a's avatar
nikhil.a committed
121 122
                static jfieldID fieldID = env->GetStaticFieldID(g_cls_ProviderState,
                                          "STOPPED", "Lorg/iotivity/service/ns/consumer/Provider$ProviderState;");
123 124
                providerState = env->GetStaticObjectField(g_cls_ProviderState, fieldID);
                break;
125 126
            }
        default:
127 128 129 130
            {
                providerState = NULL;
                break;
            }
131
    }
132
    NS_LOGD ("ConsumerService_getJavaProviderState - OUT");
133
    return providerState;
134 135
}

nikhil.a's avatar
nikhil.a committed
136 137
jobject getJavaSyncType(JNIEnv *env, OIC::Service::NSSyncInfo::NSSyncType nsType)
{
138
    NS_LOGD ("ConsumerService_getJavaSyncType - IN");
nikhil.a's avatar
nikhil.a committed
139 140 141 142 143

    // SyncType
    jclass cls_SyncType = (jclass) (env->NewLocalRef(g_cls_SyncType));
    if (!cls_SyncType)
    {
144
        NS_LOGE ("Failed to Get ObjectClass for SyncType");
nikhil.a's avatar
nikhil.a committed
145 146
        return NULL;
    }
147
    jobject syncType = NULL;
nikhil.a's avatar
nikhil.a committed
148 149 150 151 152 153
    switch (nsType)
    {
        case OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_UNREAD:
            {
                static jfieldID fieldID = env->GetStaticFieldID(cls_SyncType,
                                          "UNREAD", "Lorg/iotivity/service/ns/common/SyncInfo$SyncType;");
154 155
                syncType = env->GetStaticObjectField(cls_SyncType, fieldID);
                break;
nikhil.a's avatar
nikhil.a committed
156 157 158 159 160
            }
        case OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ :
            {
                static jfieldID fieldID = env->GetStaticFieldID(cls_SyncType,
                                          "READ", "Lorg/iotivity/service/ns/common/SyncInfo$SyncType;");
161 162
                syncType = env->GetStaticObjectField(cls_SyncType, fieldID);
                break;
nikhil.a's avatar
nikhil.a committed
163 164 165 166 167
            }
        case OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED :
            {
                static jfieldID fieldID = env->GetStaticFieldID(cls_SyncType,
                                          "DELETED", "Lorg/iotivity/service/ns/common/SyncInfo$SyncType;");
168 169
                syncType = env->GetStaticObjectField(cls_SyncType, fieldID);
                break;
nikhil.a's avatar
nikhil.a committed
170
            }
171
        default:
172 173 174 175
            {
                syncType = NULL;
                break;
            }
nikhil.a's avatar
nikhil.a committed
176 177
    }

178
    NS_LOGD ("ConsumerService_getJavaSyncType - OUT");
179
    return syncType;
nikhil.a's avatar
nikhil.a committed
180 181 182
}

jobject getJavaTopicState(JNIEnv *env, OIC::Service::NSTopic::NSTopicState nsState)
183
{
184
    NS_LOGD ("ConsumerService_getJavaTopicState - IN");
185

186 187 188 189
    // TopicState
    jclass cls_topicState = (jclass) (env->NewLocalRef(g_cls_TopicState));
    if (!cls_topicState)
    {
190
        NS_LOGE ("Failed to Get ObjectClass for TopicState Type");
nikhil.a's avatar
nikhil.a committed
191
        return NULL;
192
    }
193
    jobject topicState = NULL;
194

195
    switch (nsState)
196
    {
197 198 199 200
        case OIC::Service::NSTopic::NSTopicState::UNSUBSCRIBED:
            {
                static jfieldID fieldID = env->GetStaticFieldID(cls_topicState,
                                          "UNSUBSCRIBED", "Lorg/iotivity/service/ns/common/Topic$TopicState;");
201 202
                topicState = env->GetStaticObjectField(cls_topicState, fieldID);
                break;
203 204 205 206 207
            }
        case OIC::Service::NSTopic::NSTopicState::SUBSCRIBED:
            {
                static jfieldID fieldID = env->GetStaticFieldID(cls_topicState,
                                          "SUBSCRIBED", "Lorg/iotivity/service/ns/common/Topic$TopicState;");
208 209
                topicState = env->GetStaticObjectField(cls_topicState, fieldID);
                break;
210
            }
nikhil.a's avatar
nikhil.a committed
211
        default:
212 213 214 215
            {
                topicState = NULL;
                break;
            }
216 217
    }

218
    NS_LOGD ("ConsumerService_getJavaTopicState - OUT");
219
    return topicState;
220
}
221

222
jobject getJavaTopicsList(JNIEnv *env, std::shared_ptr<OIC::Service::NSTopicsList> topicList)
223
{
224
    NS_LOGD ("ConsumerService_getJavaTopicsList - IN");
225 226
    jclass cls_topicList = (jclass) (env->NewLocalRef(g_cls_TopicsList));
    if (!cls_topicList)
227
    {
228
        NS_LOGE ("Failed to Get ObjectClass for TopicsList");
229
        return NULL;
230
    }
231 232
    jmethodID mid_topicList = env->GetMethodID(cls_topicList, "<init>", "()V");
    if (!mid_topicList)
233
    {
234
        NS_LOGE ("Failed to Get MethodID for TopicsList<init>");
235
        return NULL;
236
    }
237
    jobject obj_topicList = env->NewObject(cls_topicList, mid_topicList);
nikhil.a's avatar
nikhil.a committed
238 239
    if (!obj_topicList)
    {
240
        NS_LOGE ("Failed to Get object for TopicsList");
nikhil.a's avatar
nikhil.a committed
241 242 243 244 245 246
        return NULL;
    }
    jmethodID mid_addTopic = env->GetMethodID(cls_topicList, "addTopic",
                             "(Ljava/lang/String;Lorg/iotivity/service/ns/common/Topic$TopicState;)V");
    if (!mid_addTopic)
    {
247
        NS_LOGE ("Failed to Get MethodID for addTopic");
nikhil.a's avatar
nikhil.a committed
248 249
        return NULL;
    }
250 251
    for (auto it : topicList->getTopicsList())
    {
252 253
        jobject jState = getJavaTopicState(env, it.getState());
        std::string topicName = it.getTopicName();
254 255 256 257
        jstring jTopicName = env->NewStringUTF(topicName.c_str());
        env->CallVoidMethod(obj_topicList, mid_addTopic, jTopicName, jState);
    }
    env->DeleteLocalRef(cls_topicList);
258
    NS_LOGD ("ConsumerService_getJavaTopicsList - OUT");
259 260
    return obj_topicList;
}
261

nikhil.a's avatar
nikhil.a committed
262
bool getNativeTopicState(JNIEnv *env,  jobject jTopic , OIC::Service::NSTopic::NSTopicState &state )
263
{
264
    NS_LOGD ("ConsumerService_getNativeTopicState - IN");
265 266 267 268 269

    jclass cls_topic = env->GetObjectClass( jTopic);
    // TopicState
    jclass cls_TopicState = (jclass) (env->NewLocalRef(g_cls_TopicState));
    if (!cls_TopicState)
270
    {
271
        NS_LOGE ("Failed to Get ObjectClass for cls_TopicState Type");
nikhil.a's avatar
nikhil.a committed
272
        return false;
273
    }
274
    jmethodID mid = env->GetMethodID(cls_TopicState, "ordinal", "()I");
nikhil.a's avatar
nikhil.a committed
275
    jfieldID fid_state = env->GetFieldID( cls_topic, "mState",
276 277 278
                                          "Lorg/iotivity/service/ns/common/Topic$TopicState;");
    if (fid_state == NULL)
    {
279
        NS_LOGE ("Error: jfieldID for state type  is null");
nikhil.a's avatar
nikhil.a committed
280
        return false;
281 282 283 284
    }
    jobject jobj = env->GetObjectField( jTopic, fid_state);
    if (jobj == NULL)
    {
285
        NS_LOGE ("Error: object of field  state Type is null");
nikhil.a's avatar
nikhil.a committed
286
        return false;
287 288
    }
    jint jState = env->CallIntMethod(jobj, mid);
nikhil.a's avatar
nikhil.a committed
289
    state = (OIC::Service::NSTopic::NSTopicState) jState;
290
    NS_LOGD ("ConsumerService_getNativeTopicState - OUT");
nikhil.a's avatar
nikhil.a committed
291
    return true;
292

293 294 295 296
}

const char *getNativeTopicName(JNIEnv *env,  jobject jTopic)
{
297
    NS_LOGD ("ConsumerService_getNativeTopicName - IN");
298
    jclass cls_topic = env->GetObjectClass( jTopic);
nikhil.a's avatar
nikhil.a committed
299 300
    if (cls_topic == NULL)
    {
301
        NS_LOGE ("Error: Class for Topic is null");
nikhil.a's avatar
nikhil.a committed
302 303 304
        return nullptr;
    }
    jfieldID fid_name = env->GetFieldID( cls_topic, "mTopicName", "Ljava/lang/String;");
305
    if (fid_name == NULL)
306
    {
307
        NS_LOGE ("Error: jfieldID for Topic Name  is null");
308
        return nullptr;
309
    }
310
    jstring jTopicName = (jstring) env->GetObjectField( jTopic, fid_name);
311
    const char *topicName = NULL;
312
    if (jTopicName)
313
    {
314 315 316 317
        topicName = env->GetStringUTFChars( jTopicName, NULL);
    }
    else
    {
318
        NS_LOGI ("topicName is null");
319
    }
320
    NS_LOGD ("ConsumerService_getNativeTopicName - OUT");
321
    return topicName;
322

323
}
324

325
std::shared_ptr<OIC::Service::NSTopicsList> getNativeTopicsList(JNIEnv *env, jobject jTopicList)
326
{
327
    NS_LOGD ("ConsumerService_getNativeTopicsList - IN");
328 329

    jclass cls_topicList = env->GetObjectClass( jTopicList);
nikhil.a's avatar
nikhil.a committed
330 331
    if (cls_topicList == NULL)
    {
332
        NS_LOGE ("Error: Class for Topic List  is null");
nikhil.a's avatar
nikhil.a committed
333 334 335
        return nullptr;
    }
    jfieldID fid_list = env->GetFieldID( cls_topicList, "mTopicsList", "Ljava/util/Vector;");
336 337
    if (fid_list == NULL)
    {
338
        NS_LOGE ("Error: jfieldID for Topic List  is null");
339 340 341 342 343
        return nullptr;
    }
    jobject jobj = env->GetObjectField( jTopicList, fid_list);
    if (jobj == NULL)
    {
344
        NS_LOGE ("Error: object of field  Topic List is null");
345 346 347
        return nullptr;
    }
    jclass cls_vec = env->FindClass("java/util/Vector");
nikhil.a's avatar
nikhil.a committed
348 349
    if (cls_vec == NULL)
    {
350
        NS_LOGE ("Error: Class for Vector not found");
nikhil.a's avatar
nikhil.a committed
351 352 353 354 355
        return nullptr;
    }
    jmethodID sizeMethod = env->GetMethodID(cls_vec,  "size", "()I");
    if (sizeMethod == NULL)
    {
356
        NS_LOGE ("Error: MethodId for Vector Size  not found");
nikhil.a's avatar
nikhil.a committed
357 358
        return nullptr;
    }
359 360
    int size = env->CallIntMethod(jobj, sizeMethod);
    jmethodID getMethod = env->GetMethodID(cls_vec,  "get", "(I)Ljava/lang/Object;");
nikhil.a's avatar
nikhil.a committed
361 362
    if (getMethod == NULL)
    {
363
        NS_LOGE ("Error: MethodId for Vector get  not found");
nikhil.a's avatar
nikhil.a committed
364 365
        return nullptr;
    }
366 367
    std::shared_ptr<OIC::Service::NSTopicsList> nsTopicList =
        std::make_shared<OIC::Service::NSTopicsList>();
368 369 370
    for (int index = 0; index < size; index++)
    {
        jobject topicObj = env->CallObjectMethod(jobj, getMethod, index);
nikhil.a's avatar
nikhil.a committed
371 372
        if (topicObj == NULL)
        {
373
            NS_LOGE ("Error: object of field  Topic  is null");
nikhil.a's avatar
nikhil.a committed
374 375
            return nullptr;
        }
376
        const char *name =  getNativeTopicName(env, topicObj);
nikhil.a's avatar
nikhil.a committed
377 378
        if (name == nullptr)
        {
379
            NS_LOGE ("Error: Couldn't find topic Name");
nikhil.a's avatar
nikhil.a committed
380 381
            return nullptr;
        }
382
        std::string topicName(name);
383
        OIC::Service::NSTopic::NSTopicState state = OIC::Service::NSTopic::NSTopicState::UNSUBSCRIBED;
nikhil.a's avatar
nikhil.a committed
384 385 386 387
        if (!getNativeTopicState(env, topicObj, state))
        {
            return nullptr;
        }
388 389 390 391 392
        nsTopicList->addTopic(topicName, state);
    }

    env->DeleteLocalRef(cls_vec);
    env->DeleteLocalRef(cls_topicList);
393
    NS_LOGD ("ConsumerService_getNativeTopicsList - OUT");
394
    return nsTopicList;
395 396
}

397
jobject getJavaProvider(JNIEnv *env, std::shared_ptr<OIC::Service::NSProvider> provider)
398
{
399 400
    NS_LOGD ("ConsumerService_getJavaProvider - IN");
    NS_LOGD ("ProviderId : %s\n", provider->getProviderId().c_str());
401 402

    jstring jProviderId = env->NewStringUTF(provider->getProviderId().c_str());
403 404 405 406 407 408 409
    auto *objectHolder = JniSharedObjectHolder<OIC::Service::NSProvider>::create(provider);
    if (!objectHolder)
    {
        NS_LOGE ("Failed to create new Object Holder for Provider");
        return NULL;
    }
    jlong pProvider = (long)objectHolder;
410 411 412 413

    jclass cls_provider = (jclass) (env->NewLocalRef(g_cls_Provider));
    if (!cls_provider)
    {
414
        NS_LOGE ("Failed to Get ObjectClass for Provider");
415
        delete objectHolder;
416
        return NULL;
417 418 419 420 421
    }
    jmethodID mid_provider = env->GetMethodID(
                                 cls_provider, "<init>", "(Ljava/lang/String;)V");
    if (!mid_provider)
    {
422
        NS_LOGE ("Failed to Get MethodID for Provider<init>");
423
        delete objectHolder;
424
        return NULL;
425
    }
426
    jobject obj_provider = env->NewObject(cls_provider, mid_provider, jProviderId);
nikhil.a's avatar
nikhil.a committed
427 428
    if (!obj_provider)
    {
429
        NS_LOGE ("Failed to create new Object for Provider");
430
        delete objectHolder;
nikhil.a's avatar
nikhil.a committed
431 432
        return NULL;
    }
433 434 435 436

    jfieldID nativeHandle = env->GetFieldID(cls_provider, "mNativeHandle", "J");
    if (!nativeHandle)
    {
437
        NS_LOGE ("Failed to get nativeHandle for Provider");
438
        delete objectHolder;
439
        return NULL;
440 441 442 443
    }
    env->SetLongField(obj_provider, nativeHandle, pProvider);

    env->DeleteLocalRef(cls_provider);
444
    NS_LOGD ("ConsumerService_getJavaProvider - OUT");
445
    return obj_provider;
446 447
}

nikhil.a's avatar
nikhil.a committed
448 449
jobject getJavaMessageType(JNIEnv *env, OIC::Service::NSMessage::NSMessageType type)
{
450
    NS_LOGD ("ConsumerService_getJavaMessageType - IN");
451
    jobject messageType = NULL;
nikhil.a's avatar
nikhil.a committed
452 453 454 455
    switch (type)
    {
        case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_ALERT:
            {
456
                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,
nikhil.a's avatar
nikhil.a committed
457
                                          "ALERT", "Lorg/iotivity/service/ns/common/Message$MessageType;");
458 459
                messageType = env->GetStaticObjectField(g_cls_Message_Type, fieldID);
                break;
nikhil.a's avatar
nikhil.a committed
460 461 462
            }
        case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_NOTICE:
            {
463
                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,
nikhil.a's avatar
nikhil.a committed
464
                                          "NOTICE", "Lorg/iotivity/service/ns/common/Message$MessageType;");
465 466
                messageType = env->GetStaticObjectField(g_cls_Message_Type, fieldID);
                break;
nikhil.a's avatar
nikhil.a committed
467 468 469
            }
        case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_EVENT:
            {
470
                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,
nikhil.a's avatar
nikhil.a committed
471
                                          "EVENT", "Lorg/iotivity/service/ns/common/Message$MessageType;");
472 473
                messageType = env->GetStaticObjectField(g_cls_Message_Type, fieldID);
                break;
nikhil.a's avatar
nikhil.a committed
474 475 476
            }
        case OIC::Service::NSMessage::NSMessageType::NS_MESSAGE_INFO:
            {
477
                static jfieldID fieldID = env->GetStaticFieldID(g_cls_Message_Type,
nikhil.a's avatar
nikhil.a committed
478
                                          "INFO", "Lorg/iotivity/service/ns/common/Message$MessageType;");
479 480
                messageType = env->GetStaticObjectField(g_cls_Message_Type, fieldID);
                break;
nikhil.a's avatar
nikhil.a committed
481 482
            }
        default:
483 484 485 486
            {
                messageType = NULL;
                break;
            }
nikhil.a's avatar
nikhil.a committed
487
    }
488
    NS_LOGD ("ConsumerService_getJavaMessageType - OUT");
489
    return messageType;
nikhil.a's avatar
nikhil.a committed
490
}
491

492
jobject getJavaMessage(JNIEnv *env, OIC::Service::NSMessage message)
493
{
494
    NS_LOGD ("ConsumerService_getJavaMessage - IN");
495

496 497 498 499
    NS_LOGD ("id : %llu\n", message.getMessageId());
    NS_LOGD ("title : %s\n", message.getTitle().c_str());
    NS_LOGD ("content : %s\n", message.getContentText().c_str());
    NS_LOGD ("source : %s\n", message.getSourceName().c_str());
500

501 502 503 504 505 506
    jlong jMessageId = (jlong) message.getMessageId();
    jstring jProviderId = env->NewStringUTF(message.getProviderId().c_str());
    jstring jTitle = env->NewStringUTF(message.getTitle().c_str());
    jstring jContentText = env->NewStringUTF(message.getContentText().c_str());
    jstring jSourceName = env->NewStringUTF(message.getSourceName().c_str());
    jstring jTopic = env->NewStringUTF(message.getTopic().c_str());
507

508 509
    jstring jTime = env->NewStringUTF(message.getTime().c_str());
    jlong jTTL = (jlong) message.getTTL();
510 511 512 513

    jclass cls_message = (jclass) (env->NewLocalRef(g_cls_Message));
    if (!cls_message)
    {
514
        NS_LOGE ("Failed to Get ObjectClass for Message");
515
        return NULL ;
516 517 518 519 520 521
    }
    jmethodID mid_message = env->GetMethodID(
                                cls_message, "<init>",
                                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
    if (!mid_message)
    {
522
        NS_LOGE ("Failed to Get MethodID for Message<init>");
523
        return NULL;
524 525 526
    }
    jobject obj_message = env->NewObject(cls_message, mid_message,
                                         jTitle, jContentText, jSourceName);
nikhil.a's avatar
nikhil.a committed
527 528
    if (!obj_message)
    {
529
        NS_LOGE ("Failed to Get Java Object for Message");
nikhil.a's avatar
nikhil.a committed
530 531
        return NULL;
    }
532 533 534 535

    jfieldID fid_messageId = env->GetFieldID(cls_message, "mMessageId", "J");
    if (!fid_messageId)
    {
536
        NS_LOGE ("Failed to get field MessageID for Message");
537
        return NULL;
538 539 540 541 542 543
    }
    env->SetLongField(obj_message, fid_messageId, jMessageId);

    jfieldID fid_providerId = env->GetFieldID(cls_message, "mProviderId", "Ljava/lang/String;");
    if (!fid_providerId)
    {
544
        NS_LOGE ("Failed to get field ProviderID for Message");
545
        return NULL;
546 547 548 549 550 551
    }
    env->SetObjectField(obj_message, fid_providerId, jProviderId);

    jfieldID fid_time = env->GetFieldID(cls_message, "mTime", "Ljava/lang/String;");
    if (!fid_time)
    {
552
        NS_LOGE ("Failed to get field Time for Message");
553
        return NULL;
554 555 556 557 558 559
    }
    env->SetObjectField(obj_message, fid_time, jTime);

    jfieldID fid_ttl = env->GetFieldID(cls_message, "mTTL", "J");
    if (!fid_ttl)
    {
560
        NS_LOGE ("Failed to get field TTL for Message");
561
        return NULL;
562 563 564
    }
    env->SetLongField(obj_message, fid_ttl, jTTL);

565 566 567
    jfieldID fid_topic = env->GetFieldID(cls_message, "mTopic", "Ljava/lang/String;");
    if (!fid_topic)
    {
568
        NS_LOGE ("Failed to get mTopic for Message");
569 570 571 572
        return NULL;
    }
    env->SetObjectField(obj_message, fid_topic, jTopic);

573
    OIC::Service::NSMediaContents *mediaCont = message.getMediaContents();
574 575 576 577 578 579
    if (mediaCont != nullptr)
    {
        jstring jIconImage = env->NewStringUTF(mediaCont->getIconImage().c_str());
        jclass cls_mediaContents = (jclass) (env->NewLocalRef(g_cls_MediaContents));
        if (!cls_mediaContents)
        {
580
            NS_LOGE ("Failed to Get ObjectClass for MediaContents");
581
            return NULL;
582 583 584 585 586
        }
        jmethodID mid_mediaContents = env->GetMethodID(
                                          cls_mediaContents, "<init>", "(Ljava/lang/String;)V");
        if (!mid_mediaContents)
        {
587
            NS_LOGE ("Failed to Get MethodID for MediaContents<init>");
588
            return NULL;
589 590 591 592 593 594 595 596
        }
        jobject obj_mediaContents = env->NewObject(cls_mediaContents, mid_mediaContents,
                                    jIconImage);

        jfieldID fid_mediaContents = env->GetFieldID(cls_message, "mMediaContents",
                                     "Lorg/iotivity/service/ns/common/MediaContents;");
        if (!fid_mediaContents)
        {
597
            NS_LOGE ("Failed to get field mediaContents for Message");
598
            return NULL;
599 600 601 602 603
        }
        env->SetObjectField(obj_message, fid_mediaContents, obj_mediaContents);

    }

604
    jobject jType = getJavaMessageType(env, message.getType());
nikhil.a's avatar
nikhil.a committed
605
    if (jType)
606
    {
nikhil.a's avatar
nikhil.a committed
607 608 609 610
        jfieldID fid_type = env->GetFieldID(cls_message, "mType",
                                            "Lorg/iotivity/service/ns/common/Message$MessageType;");
        if (!fid_type)
        {
611
            NS_LOGE ("Failed to get field Type for Message");
nikhil.a's avatar
nikhil.a committed
612 613 614
            return NULL;
        }
        env->SetObjectField(obj_message, fid_type, jType);
615
    }
616

617
    NS_LOGD ("Reading OCRepresentation Object from Native");
nikhil.a's avatar
nikhil.a committed
618

619
    OC::OCRepresentation *ocRepresentation = new OC::OCRepresentation(message.getExtraInfo());
620 621 622 623 624
    jlong handle = reinterpret_cast<jlong>(ocRepresentation);
    jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool,
                              handle, true);
    if (!jRepresentation)
    {
625
        NS_LOGE ("Failed to create OcRepresentation");
626 627 628 629
        delete ocRepresentation;
    }
    else
    {
630
        NS_LOGD ("Created OCRepresentation Object from Native");
631 632 633 634 635
    }
    jfieldID fid_extraInfo = env->GetFieldID(cls_message, "mExtraInfo",
                             "Lorg/iotivity/base/OcRepresentation;");
    if (!fid_extraInfo)
    {
636
        NS_LOGE ("Failed to get mExtraInfo for Message");
637 638 639 640
        if (ocRepresentation)
        {
            delete ocRepresentation;
        }
641 642
        return NULL;
    }
643
    NS_LOGD ("setting extraInfo field");
644
    env->SetObjectField(obj_message, fid_extraInfo, jRepresentation);
645

646
    env->DeleteLocalRef(cls_message);
647
    NS_LOGD ("ConsumerService_getJavaMessage - OUT");
648 649 650
    return obj_message;
}

651
void onDiscoverProvider(std::shared_ptr<OIC::Service::NSProvider> provider)
652
{
653
    NS_LOGD ("ConsumerService_onDiscoverProvider - IN");
654

655
    jint envRet = 0;;
656
    JNIEnv *env = GetJNIEnv(&envRet);
657 658 659 660
    if (NULL == env)
    {
        return ;
    }
661 662 663 664

    jobject jDiscoverListener = (jobject) env->NewLocalRef(g_obj_discoverListener);
    if (!jDiscoverListener)
    {
665
        NS_LOGE ("Failed to Get jDiscoverListener");
666 667 668 669
        if (JNI_EDETACHED == envRet)
        {
            g_jvm_consumer->DetachCurrentThread();
        }
670 671 672 673 674 675
        return ;
    }

    jobject obj_provider = getJavaProvider(env, provider);
    if (!obj_provider)
    {
676
        NS_LOGE ("Failed to Get Provider Object");
677 678 679 680
        if (JNI_EDETACHED == envRet)
        {
            g_jvm_consumer->DetachCurrentThread();
        }
681 682 683 684 685 686
        return ;
    }

    jclass cls = env->GetObjectClass(jDiscoverListener);
    if (!cls)
    {
687
        NS_LOGE ("Failed to Get ObjectClass for jDiscoverListener");
688 689 690 691
        if (JNI_EDETACHED == envRet)
        {
            g_jvm_consumer->DetachCurrentThread();
        }
692 693 694 695 696 697 698 699
        return ;
    }
    jmethodID mid = env->GetMethodID(
                        cls,
                        "onProviderDiscovered",
                        "(Lorg/iotivity/service/ns/consumer/Provider;)V");
    if (!mid)
    {
700
        NS_LOGE ("Failed to Get MethodID for onProviderDiscovered");
701 702 703 704
        if (JNI_EDETACHED == envRet)
        {
            g_jvm_consumer->DetachCurrentThread();
        }
705 706 707 708 709 710
        return ;
    }

    env->CallVoidMethod(jDiscoverListener, mid, obj_provider);

    env->DeleteLocalRef(jDiscoverListener);
711 712 713 714
    if (JNI_EDETACHED == envRet)
    {
        g_jvm_consumer->DetachCurrentThread();
    }
715
    NS_LOGD ("ConsumerService_onDiscoverProvider - OUT");
716 717 718
    return ;
}

nikhil.a's avatar
nikhil.a committed
719
void onProviderState( OIC::Service::NSProviderState state)
720
{
721
    NS_LOGD ("ConsumerService_onProviderState -IN");
722

723
    jint envRet = 0;;
724
    JNIEnv *env = GetJNIEnv(&envRet);
725 726 727 728
    if (NULL == env)
    {
        return ;
    }
729 730 731 732

    jobject jAcceptListener = (jobject) env->NewLocalRef(g_obj_acceptListener);
    if (!jAcceptListener)
    {
733
        NS_LOGE ("Failed to Get jAcceptListener");
734 735 736 737
        if (JNI_EDETACHED == envRet)
        {
            g_jvm_consumer->DetachCurrentThread();
        }
738 739
        return ;
    }
nikhil.a's avatar
nikhil.a committed
740 741
    jobject obj_state = getJavaProviderState(env, state);
    if (!obj_state)
742
    {
743
        NS_LOGE ("Failed to Get ProviderState Object");
744 745 746 747
        if (JNI_EDETACHED == envRet)
        {
            g_jvm_consumer->DetachCurrentThread();
        }
748 749 750 751 752 753
        return ;
    }

    jclass cls = env->GetObjectClass(jAcceptListener);
    if (!cls)
    {
754
        NS_LOGE ("Failed to Get ObjectClass for jAcceptListener");
755 756 757 758
        if (JNI_EDETACHED == envRet)
        {
            g_jvm_consumer->DetachCurrentThread();
        }
759 760 761 762
        return ;
    }
    jmethodID mid = env->GetMethodID(
                        cls,
nikhil.a's avatar
nikhil.a committed
763 764
                        "onProviderStateReceived",
                        "(Lorg/iotivity/service/ns/consumer/Provider$ProviderState;)V");
765 766
    if (!mid)
    {
767
        NS_LOGE ("Failed to Get MethodID for onProviderState");
768 769 770 771
        if (JNI_EDETACHED == envRet)
        {
            g_jvm_consumer->DetachCurrentThread();
        }
772 773
        return ;
    }
nikhil.a's avatar
nikhil.a committed
774
    env->CallVoidMethod(jAcceptListener, mid, obj_state);
775 776

    env->DeleteLocalRef(jAcceptListener);
777 778 779 780
    if (JNI_EDETACHED == envRet)
    {
        g_jvm_consumer->DetachCurrentThread();
    }
781
    NS_LOGD ("ConsumerService_onProviderState -OUT");
782 783 784 785
    return ;

}

786
void onMessagePosted(OIC::Service::NSMessage message)
787
{
788
    NS_LOGD ("ConsumerService_onMessagePosted -IN");
789

790
    jint envRet = 0;;
791
    JNIEnv *env = GetJNIEnv(&envRet);
792 793 794 795
    if (NULL == env)
    {
        return ;
    }
796

797 798 799
    jobject jPostListener = (jobject) env->NewLocalRef(g_obj_postListener);
    if (!jPostListener)
    {
800
        NS_LOGE ("Failed to Get jPostListener");
801 802 803 804
        if (JNI_EDETACHED == envRet)
        {
            g_jvm_consumer->DetachCurrentThread();
        }
805 806 807 808 809 810
        return ;
    }

    jobject obj_message = getJavaMessage( env, message);
    if (!obj_message)
    {
811
        NS_LOGE ("Failed to Get Message Object");
812 813 814 815
        if (JNI_EDETACHED == envRet)
        {
            g_jvm_consumer->DetachCurrentThread();
        }
816 817
        return ;
    }
818 819 820
    jclass cls = env->GetObjectClass(jPostListener);
    if (!cls)
    {
821
        NS_LOGE ("Failed to Get ObjectClass for jPostListener");
822 823 824 825
        if (JNI_EDETACHED == envRet)
        {
            g_jvm_consumer->DetachCurrentThread();
        }
826 827 828 829 830 831 832 833
        return ;
    }
    jmethodID mid = env->GetMethodID(
                        cls,
                        "onMessageReceived",
                        "(Lorg/iotivity/service/ns/common/Message;)V");
    if (!mid)
    {
834
        NS_LOGE ("Failed to Get MethodID for onMessageReceived");
835 836 837 838
        if (JNI_EDETACHED == envRet)
        {
            g_jvm_consumer->DetachCurrentThread();
        }
839 840 841 842 843 844
        return ;
    }

    env->CallVoidMethod(jPostListener, mid, obj_message);

    env->DeleteLocalRef(jPostListener);
845 846 847 848
    if (JNI_EDETACHED == envRet)
    {
        g_jvm_consumer->DetachCurrentThread();
    }
849
    NS_LOGD ("ConsumerService_onMessagePosted -OUT");
850 851 852
    return ;
}

853
void onSyncInfoReceived(OIC::Service::NSSyncInfo sync)
854
{
855
    NS_LOGD ("ConsumerService_onSyncInfoReceived - IN");
856

Abitha Shankar's avatar