Restructuring of the easy-setup service.
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / 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
21 #include "JniEasySetup.h"
22
23 using namespace OIC::Service;
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 JNIEXPORT jobject JNICALL
29 Java_org_iotivity_service_easysetup_mediator_EasySetupService_nativeCreateEnrolleeDevice
30 (JNIEnv *env, jobject interfaceClass, jstring ip, jstring ssid, jstring password,
31  jint connectivityType, jboolean needSecuredEasysetup)
32 {
33
34     LOGI("JniEasySetup::nativeCreateEnrolleeDevice enter");
35
36     std::shared_ptr<RemoteEnrollee> nativeRemoteEnrollee;
37     jobject jRemoteEnrollee;
38     EnrolleeNWProvInfo netInfo;
39
40     const char *cIp = (env)->GetStringUTFChars(ip, NULL);
41     const char *cSssid = (env)->GetStringUTFChars(ssid, NULL);
42     const char *cPassword = (env)->GetStringUTFChars(password, NULL);
43
44     std::string sIp(cIp);
45     std::string sSssid(cSssid);
46     std::string sPassword(cPassword);
47
48     netInfo.connType = getOCConnectivityTypeFromInt(connectivityType);
49     netInfo.isSecured = bool(
50                             needSecuredEasysetup);       // may be need to remove, if removed from c++ layer
51     netInfo.needSecuredEasysetup =  bool(needSecuredEasysetup);
52
53     OICStrcpy(netInfo.netAddressInfo.WIFI.ipAddress, IPV4_ADDR_SIZE - 1, sIp.c_str());
54     OICStrcpy(netInfo.netAddressInfo.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, sSssid.c_str());
55     OICStrcpy(netInfo.netAddressInfo.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, sPassword.c_str());
56
57     try
58     {
59         nativeRemoteEnrollee = EasySetup::getInstance()->createEnrolleeDevice(netInfo);
60         //create the java object
61         jRemoteEnrollee = env->NewObject(g_cls_RemoteEnrollee, g_mid_RemoteEnrollee_ctor);
62         if (!jRemoteEnrollee)
63         {
64             LOGE("JniEasySetup::nativeCreateEnrolleeDevice Unable to create the java object");
65             return NULL;
66         }
67
68         JniRemoteEnrollee *jniRemoteEnrollee = new JniRemoteEnrollee(nativeRemoteEnrollee);
69         ESSetHandle<JniRemoteEnrollee>(env, jRemoteEnrollee, jniRemoteEnrollee);
70     }
71     catch (ESBadRequestException exception)
72     {
73         LOGE("JniEasySetup::nativeCreateEnrolleeDevice Unable to create the Native EnrolleeDevice");
74         //throw the exception to java layer
75         throwESException( env,  exception.what());
76     }
77
78     LOGI("JniEasySetup::nativeCreateEnrolleeDevice exit");
79
80     return jRemoteEnrollee;
81 }
82 #ifdef __cplusplus
83 }
84 #endif
85