Merge branch 'master' into notification-service
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / main / jni / JniEasySetup.cpp
1 /******************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20 #include <memory>
21 #include <vector>
22
23 #include "OCPlatform.h"
24 #include "OCResource.h"
25 #include "octypes.h"
26 #include "ESRichCommon.h"
27
28 #include "JniOcResource.h"
29 #include "JniEasySetup.h"
30
31 using namespace OC;
32 using namespace OIC::Service;
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 JNIEXPORT jobject JNICALL
38 Java_org_iotivity_service_easysetup_mediator_EasySetup_nativeCreateRemoteEnrollee
39 (JNIEnv *env, jobject thiz, jobject jResource)
40 {
41     ES_LOGI("JniEasySetup::nativeCreateRemoteEnrollee enter");
42
43     std::shared_ptr<RemoteEnrollee> nativeRemoteEnrollee;
44     jobject jRemoteEnrollee;
45
46     JniOcResource* jniOcResource = JniOcResource::getJniOcResourcePtr(env, jResource);
47
48     if (!jniOcResource)
49     {
50         ES_LOGE("JniEasySetup::nativeCreateRemoteEnrollee getJniOcResourcePtr returns nullptr.");
51         return nullptr;
52     }
53
54     try
55     {
56         nativeRemoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(jniOcResource->getOCResource());
57         //create the java object
58         jRemoteEnrollee = env->NewObject(g_cls_RemoteEnrollee, g_mid_RemoteEnrollee_ctor);
59         if (!jRemoteEnrollee)
60         {
61             ES_LOGE("JniEasySetup::nativeCreateRemoteEnrollee Unable to create the java object");
62             return NULL;
63         }
64         JniRemoteEnrollee *jniRemoteEnrollee = new JniRemoteEnrollee(nativeRemoteEnrollee);
65         ESSetHandle<JniRemoteEnrollee>(env, jRemoteEnrollee, jniRemoteEnrollee);
66     }
67     catch (ESBadRequestException exception)
68     {
69         ES_LOGE("JniEasySetup::nativeCreateRemoteEnrollee Unable to create the Native EnrolleeDevice");
70         //throw the exception to java layer
71         throwESException( env,  exception.what());
72     }
73
74     ES_LOGI("JniEasySetup::nativeCreateRemoteEnrollee exit");
75
76     return jRemoteEnrollee;
77 }
78 #ifdef __cplusplus
79 }
80 #endif
81