[ENROLLEE] Tizen enrollee sample application build using scons command
[platform/upstream/iotivity.git] / service / easy-setup / sdk / mediator / android / richsdk / jni / JniRemoteEnrollee.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 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
21 #include "JniRemoteEnrollee.h"
22
23 using namespace OIC::Service;
24
25 JniRemoteEnrollee::JniRemoteEnrollee(std::shared_ptr<RemoteEnrollee> remoteEnrollee)
26     : m_sharedResource(remoteEnrollee) {}
27
28 JniRemoteEnrollee::~JniRemoteEnrollee()
29 {
30     LOGD("JniRemoteEnrollee::~JniRemoteEnrollee()");
31     m_sharedResource = NULL;
32
33     jint envRet;
34     JNIEnv *env = GetESJNIEnv(envRet);
35     if (NULL == env) return;
36
37     if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread();
38
39 }
40
41 JniRemoteEnrollee *JniRemoteEnrollee::getJniRemoteEnrollee(JNIEnv *env, jobject thiz)
42 {
43     JniRemoteEnrollee *remoteEnrollee = ESGetHandle<JniRemoteEnrollee>(env, thiz);
44     if (env->ExceptionCheck())
45     {
46         LOGE("getJniRemoteEnrollee :: Failed to get native handle from RemoteEnrollee object");
47     }
48     if (!remoteEnrollee)
49     {
50         LOGE("getJniRemoteEnrollee :: no resource");
51     }
52     return remoteEnrollee;
53 }
54
55 void JniRemoteEnrollee::startProvisioning(JNIEnv *env)
56 {
57     try
58     {
59         m_sharedResource->startProvisioning();
60     }
61     catch (ESBadRequestException exception)
62     {
63         LOGE("JNI startProvisioning :: Exception occured");
64         //throw the exception to java
65         throwESException( env,  exception.what());
66     }
67 }
68 void JniRemoteEnrollee::stopProvisioning(JNIEnv *env)
69 {
70     try
71     {
72         m_sharedResource->stopProvisioning();
73     }
74     catch (ESBadRequestException exception)
75     {
76         LOGE("JNI stopProvisioning :: Exception occured");
77         //throw the exception to java
78         throwESException( env,  exception.what());
79     }
80 }
81
82 void JniRemoteEnrollee::registerProvisioningHandler(JNIEnv *env, jobject jListener)
83 {
84     JniProvisioningStatusListener *onProvisioningStatusReceived = addProvisioningStatusListener(env,
85             jListener);
86
87     RemoteEnrollee::EasySetupStatusCB provisionStatusCallback = [onProvisioningStatusReceived]
88             (std::shared_ptr< OIC::Service::EasySetupStatus > easySetupStatus)
89
90     {
91         onProvisioningStatusReceived->provisionStatusCallback(easySetupStatus);
92     };
93
94     try
95     {
96         m_sharedResource->registerEasySetupStatusHandler(provisionStatusCallback);
97     }
98     catch (ESException exception)
99     {
100         LOGE("JNI stopProvisioning :: Exception occured");
101         //throw the exception to java
102         throwESException( env,  exception.what());
103     }
104
105 }
106
107 JniProvisioningStatusListener *JniRemoteEnrollee::addProvisioningStatusListener(JNIEnv *env,
108         jobject jListener)
109 {
110     return this->m_provisioingStatus.addListener(env, jListener, this);
111 }
112
113 void JniRemoteEnrollee::removeProvisioningStatusListener(JNIEnv *env, jobject jListener)
114 {
115     this->m_provisioingStatus.removeListener(env, jListener);
116 }
117
118 JNIEXPORT void JNICALL
119 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeStartProvision
120 (JNIEnv *env, jobject jClass)
121 {
122     LOGD("nativeStartProvision Enter");
123
124     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
125     remoteEnrollee->startProvisioning(env);
126
127     LOGD("nativeStartProvision Exit");
128 }
129
130 JNIEXPORT void JNICALL
131 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeStopProvision
132 (JNIEnv *env, jobject jClass)
133 {
134
135     LOGD("nativeStopProvision Enter");
136
137     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
138     remoteEnrollee->stopProvisioning(env);
139
140     LOGD("nativeStopProvision Exit");
141 }
142
143 JNIEXPORT void JNICALL
144 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeRegisterProvisioningHandler
145 (JNIEnv *env, jobject jClass, jobject provisiongListener)
146 {
147     LOGD("nativeRegisterProvisioningHandler Enter");
148
149     if (!provisiongListener)
150     {
151         LOGE("nativeRegisterProvisioningHandler : listener is NULL");
152         return;
153     }
154     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
155     remoteEnrollee->registerProvisioningHandler(env, provisiongListener);
156
157     LOGD("nativeRegisterProvisioningHandler Exit");
158 }
159