16386c04605b7b109c2346ae4e6024995df27556
[platform/upstream/iotivity.git] / service / easy-setup / sdk / mediator / android / jni / jni_easy_setup.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 "jni_easy_setup.h"
22
23 #include "jni_easy_setup_jvm.h"
24 #include "prov_adapter.h"
25
26 void JNIProvisioningStatusCallback(ProvisioningInfo * provInfo) {
27         JNIEnv *env = EasySetupJVM::getEnv();
28         if (env == NULL) {
29                 LOGE("JNIProvisioningStatusCallback : Getting JNIEnv failed");
30                 return;
31         }
32
33         // Get EasySetupHandler class reference
34         jclass easysetupCallbacks = GetJClass(
35                         EASY_SETUP_SERVICE_CALLBACK_NATIVE_API_CLASS_PATH);
36         if (NULL == easysetupCallbacks) {
37                 LOGE(
38                                 "JNIProvisioningStatusCallback : GetJClass easysetupCallbacks failed");
39                 EasySetupJVM::releaseEnv();
40                 return;
41         }
42
43         // Get the easysetupCallback class instance
44         jobject jobjectCallback = GetJObjectInstance(
45                         EASY_SETUP_SERVICE_CALLBACK_NATIVE_API_CLASS_PATH);
46         if (NULL == jobjectCallback) {
47                 LOGE("getInstance( %s) failed!",
48                                 EASY_SETUP_SERVICE_CALLBACK_NATIVE_API_CLASS_PATH);
49                 EasySetupJVM::releaseEnv();
50                 return;
51         }
52
53         // Get onResourceCallback method reference
54         jmethodID method_id = env->GetMethodID(easysetupCallbacks,
55                         "ProvisioningStatusCallBack",
56                         METHOD_PROVISIONING_STATUS_INTEGER_CALLBACK);
57         if (NULL == method_id) {
58                 LOGE(
59                                 "JNIProvisioningStatusCallback: onResourceCallback : GetMethodID failed");
60                 EasySetupJVM::releaseEnv();
61                 return;
62         }
63
64         if ((env)->ExceptionCheck()) {
65                 LOGE("JNIProvisioningStatusCallback : ExceptionCheck failed");
66                 EasySetupJVM::releaseEnv();
67                 return;
68         }
69
70         if (NULL == method_id) {
71                 LOGI("JNI method_id is NULL");
72         } else {
73                 LOGI("JNI method_id is VALID");
74
75                 jint result;
76                 if (provInfo->provStatus == DEVICE_PROVISIONED) {
77                         result = 0;
78                 } else {
79                         result = -1;
80                 }
81
82                 env->CallVoidMethod(jobjectCallback, method_id, (jint) result);
83         }
84
85         EasySetupJVM::releaseEnv();
86 }
87
88 JNIEXPORT void JNICALL
89 JNIInitEasySetup(JNIEnv
90                 *env,
91                 jobject thisObj
92 )
93 {
94         LOGI("JNI JNIInitEasySetup: Enter");
95
96         InitProvProcess();
97
98         RegisterCallback(JNIProvisioningStatusCallback);
99 }
100
101 JNIEXPORT void JNICALL
102 JNITerminateEasySetup(JNIEnv
103                 *env,
104                 jobject thisObj
105 )
106 {
107         LOGI("JNI JNITerminateEasySetup: Enter");
108
109         ResetProvProcess();
110
111 }
112
113 JNIEXPORT void JNICALL
114 JNIProvisionEnrollee(JNIEnv
115                 *env,
116                 jobject thisObj,
117                 jstring
118                 jIPAddress,
119                 jstring jNetSSID,
120                 jstring
121                 jNetPWD,
122                 jint jConnectivityType
123 )
124 {
125         LOGI("JNI JNIProvisionEnrollee: Enter");
126
127         if (!jIPAddress)
128         {
129                 LOGE("JNI JNIProvisionEnrollee : jIPAddress is NULL!");
130                 return;
131         }
132
133         const char *ipAddress = env->GetStringUTFChars(jIPAddress, NULL);
134         if (NULL == ipAddress)
135         {
136                 LOGE("JNI JNIProvisionEnrollee : Failed to convert jstring to char string!");
137         }
138
139         LOGI("JNI JNIProvisionEnrollee : ipAddress is : %s",ipAddress);
140
141         const char *netSSID = env->GetStringUTFChars(jNetSSID, NULL);
142         if (NULL == netSSID)
143         {
144                 LOGE("JNI JNIProvisionEnrollee : Failed to convert jstring to char string!");
145         }
146
147         LOGI("JNI JNIProvisionEnrollee : netSSID is : %s",netSSID);
148
149         const char *netPWD = env->GetStringUTFChars(jNetPWD, NULL);
150         if (NULL == netPWD)
151         {
152                 LOGE("JNI JNIProvisionEnrollee : Failed to convert jstring to char string!");
153         }
154
155         LOGI("JNI JNIProvisionEnrollee : netPWD is : %s",netPWD);
156
157         OCConnectivityType connecitivityType;
158         EnrolleeNWProvInfo netInfo =
159         {       0};
160         strncpy(netInfo
161                         .netAddressInfo.WIFI.ipAddress, ipAddress, IPV4_ADDR_SIZE-1);
162         strncpy(netInfo
163                         .netAddressInfo.WIFI.ssid, netSSID, NET_WIFI_SSID_SIZE-1);
164         strncpy(netInfo
165                         .netAddressInfo.WIFI.pwd, netPWD, NET_WIFI_PWD_SIZE-1);
166         netInfo.
167         connType = (OCConnectivityType) jConnectivityType;
168
169         netInfo.
170         isSecured = true;
171
172         StartProvisioning(&netInfo);
173
174         return;
175 }
176
177 JNIEXPORT void JNICALL
178 JNIStopEnrolleeProvisioning(JNIEnv
179                 *env,
180                 jobject thisObj,
181                 jint
182                 jConnectivityType)
183 {
184         LOGI("JNI Stop Easy Setup: Entering");
185
186         StopProvisioning((OCConnectivityType)
187                         jConnectivityType);
188
189         return;
190 }
191