replace : iotivity -> iotivity-sec
[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 = NULL;
44     jobject jRemoteEnrollee = NULL;
45
46     if(!jResource)
47     {
48         ES_LOGE("JniEasySetup::nativeCreateRemoteEnrollee Invalid param.");
49         return NULL;
50     }
51
52     JniOcResource* jniOcResource = JniOcResource::getJniOcResourcePtr(env, jResource);
53
54     if (!jniOcResource)
55     {
56         ES_LOGE("JniEasySetup::nativeCreateRemoteEnrollee getJniOcResourcePtr returns nullptr.");
57         return NULL;
58     }
59
60     try
61     {
62         nativeRemoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(jniOcResource->getOCResource());
63         if (!nativeRemoteEnrollee)
64         {
65             ES_LOGE("Failed to create RemoteEnrollee object.");
66             return NULL;
67         }
68
69         //create the java object
70         jRemoteEnrollee = env->NewObject(g_cls_RemoteEnrollee, g_mid_RemoteEnrollee_ctor);
71         if (!jRemoteEnrollee)
72         {
73             ES_LOGE("JniEasySetup::nativeCreateRemoteEnrollee Unable to create the java object");
74             return NULL;
75         }
76         JniRemoteEnrollee *jniRemoteEnrollee = new JniRemoteEnrollee(nativeRemoteEnrollee);
77         ESSetHandle<JniRemoteEnrollee>(env, jRemoteEnrollee, jniRemoteEnrollee);
78     }
79     catch (ESBadRequestException exception)
80     {
81         ES_LOGE("JniEasySetup::nativeCreateRemoteEnrollee Unable to create the Native EnrolleeDevice");
82         //throw the exception to java layer
83         throwESException( env,  exception.what());
84     }
85
86     ES_LOGI("JniEasySetup::nativeCreateRemoteEnrollee exit");
87
88     return jRemoteEnrollee;
89 }
90 #ifdef __cplusplus
91 }
92 #endif
93