[ENROLLEE] Tizen enrollee sample application build using scons command
[platform/upstream/iotivity.git] / service / easy-setup / sdk / mediator / android / richsdk / EasySetupCore / src / main / java / org / iotivity / service / easysetup / mediator / EasySetupService.java
1 /**
2  * ***************************************************************
3  * <p/>
4  * Copyright 2015 Samsung Electronics All Rights Reserved.
5  * <p/>
6  * <p/>
7  * <p/>
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * <p/>
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * <p/>
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * <p/>
20  * ****************************************************************
21  */
22
23 package org.iotivity.service.easysetup.mediator;
24
25 import android.content.Context;
26 import android.net.wifi.WifiConfiguration;
27 import android.util.Log;
28
29 import java.io.IOException;
30 import java.util.ArrayList;
31
32 /**
33  * This is facade class, a single point of contact for Application.
34  * It contains set of APIs to do easy setup of the enrolling device.
35  * ON-BOARDING - This is a step to establish connectivity between the device & Mediator device.
36  * PROVISION   - This is a step where the netowork's detail & credentials are given to the
37  * enrolling device.
38  */
39 public class EasySetupService {
40
41     private static final String TAG = EasySetupService.class.getName();
42
43     private static EasySetupService sInstance;
44
45     private final EasySetupStatus mCallback;
46
47     private ArrayList<EnrolleeDevice> mEnrolleeDeviceList;
48
49     private final ProvisioningCallback mProvisioningCallback;
50
51     private static Context mContext;
52
53     protected RemoteEnrollee mRemoteEnrollee;
54
55     //function to call the native createEnrolleeDevice
56     private native RemoteEnrollee nativeCreateEnrolleeDevice(String ip, String ssid,
57                                                      String password, int type, boolean isSecured );
58
59     static {
60         // Load Easy Setup JNI interface
61         System.loadLibrary("gnustl_shared");
62         System.loadLibrary("octbstack");
63         System.loadLibrary("connectivity_abstraction");
64         System.loadLibrary("ESSDKLibrary");
65         System.loadLibrary("easysetup-jni");
66     }
67
68     private EasySetupService(EasySetupStatus callback) {
69         mCallback = callback;
70         mProvisioningCallback = new ProvisioningCallbackImpl(mCallback);
71         mEnrolleeDeviceList = new ArrayList<EnrolleeDevice>();
72         mContext = null;
73     }
74
75     /**
76      * Gives a singleton instance of Easy setup service and initialize the service
77      *
78      * @param callback Application needs to provide this callback to receive the status of easy
79      *                 setup process.
80      */
81
82     public synchronized static EasySetupService getInstance(Context context, EasySetupStatus
83             callback) {
84         if (sInstance == null) {
85             sInstance = new EasySetupService(callback);
86             mContext = context;
87         }
88         return sInstance;
89     }
90
91     /**
92      * Reset the Easy setup Service
93      */
94
95     public void finish() {
96             //Call the stop Provisioning
97             for (EnrolleeDevice enrolleeDevice : mEnrolleeDeviceList) {
98                 enrolleeDevice.stopProvisioningProcess();
99         }
100     }
101
102     /**
103      * Starts Easy setup process for the enrolling device.
104      *
105      * @param enrolledevice Device to be enrolled in network
106      * @throws IOException Throws exception in case of any connection error.
107      */
108
109     public synchronized void startSetup(final EnrolleeDevice enrolledevice) throws IOException,ESException {
110
111         mEnrolleeDeviceList.add(enrolledevice);
112
113         // Starts the provisioning directly if the device is already on boarded on the network.
114         if (enrolledevice.onBoarded()) {
115             if(null == enrolledevice.mRemoteEnrollee){
116                 //create Native RemoteEnrollee
117                 WiFiProvConfig config = (WiFiProvConfig)enrolledevice.mProvConfig;
118                 String ip = "";
119                 String ssid = config.getSsId();
120                 String password = config.getPassword();
121                 int connectivityType = config.getConnType().getValue();
122                 boolean isSecured = config.isSecured();
123
124                 //native call
125                 mRemoteEnrollee = nativeCreateEnrolleeDevice(ip, ssid, password,
126                         connectivityType, isSecured);
127                 enrolledevice.mRemoteEnrollee = mRemoteEnrollee;
128             }
129             enrolledevice.startProvisioning(mProvisioningCallback);
130             return;
131         }
132         enrolledevice.mState = EnrolleeState.DEVICE_ON_BOARDING_STATE;
133         mCallback.onProgress(enrolledevice);
134         enrolledevice.startOnBoarding(new OnBoardingCallback() {
135
136             @Override
137             public void onFinished(OnBoardingConnection connection) {
138                 if (connection.isConnected()) {
139                     Log.i(TAG, "On boarding is successful ");
140                     // Start provisioning here
141                     enrolledevice.mState = EnrolleeState.DEVICE_ON_BOARDED_STATE;
142                     mCallback.onProgress(enrolledevice);
143                     enrolledevice.setConnection(connection);
144
145                     //create a native RemoteEnrollee with network info
146                     IpOnBoardingConnection conn = (IpOnBoardingConnection) connection;
147                     WiFiProvConfig config = (WiFiProvConfig)enrolledevice.mProvConfig;
148                     String ip = conn.getIp();
149                     String ssid = config.getSsId();
150                     String password = config.getPassword();
151                     int connectivityType = config.getConnType().getValue();
152                     boolean isSecured = config.isSecured();
153
154                     //native call
155                     mRemoteEnrollee = nativeCreateEnrolleeDevice(ip, ssid, password,
156                                 connectivityType, isSecured);
157
158                     enrolledevice.mRemoteEnrollee = mRemoteEnrollee;
159
160                     /* Delay is set according to Soft AP host and Mediator platform;
161                     *  For Android with Soft AP it is 2000 ms
162                     */
163                     delayProvisioning(connection);
164                     enrolledevice.startProvisioning(mProvisioningCallback);
165                 } else {
166                     enrolledevice.mState = EnrolleeState.DEVICE_INIT_STATE;
167                     mProvisioningCallback.onFinished(enrolledevice);
168                 }
169             }
170         });
171     }
172
173     /**
174      * Stops on-going Easy setup process for enrolling device.
175      *
176      * @param enrolleedevice Device to be enrolled in network
177      */
178     public synchronized void stopSetup(EnrolleeDevice enrolleedevice) throws ESException {
179         if (mEnrolleeDeviceList.contains(enrolleedevice)) {
180             if (enrolleedevice.mState == EnrolleeState.DEVICE_ON_BOARDING_STATE) {
181                 Log.i(TAG, "stopOnBoardingProcess for enrolleedevice");
182                 enrolleedevice.stopOnBoardingProcess();
183             }else if (enrolleedevice.mState == EnrolleeState.DEVICE_PROVISIONING_STATE) {
184                 Log.i(TAG, "stopEnrolleeProvisioning for enrolleedevice");
185                 enrolleedevice.stopProvisioningProcess();
186             }
187             enrolleedevice.mState = EnrolleeState.DEVICE_INIT_STATE;
188             mCallback.onProgress(enrolleedevice);
189             mEnrolleeDeviceList.remove(enrolleedevice);
190         }
191     }
192
193     class ProvisioningCallbackImpl extends ProvisioningCallback {
194
195         private final EasySetupStatus mCallback;
196
197         ProvisioningCallbackImpl(EasySetupStatus callback) {
198             mCallback = callback;
199         }
200
201         @Override
202         public void onFinished(EnrolleeDevice enrolledevice) {
203             synchronized (EasySetupService.this) {
204                 if (mEnrolleeDeviceList.contains(enrolledevice)) {
205                     Log.i(TAG, "onFinished() is received " + enrolledevice.isSetupSuccessful());
206                     mCallback.onFinished(enrolledevice);
207                     mEnrolleeDeviceList.remove(enrolledevice);
208                 }
209             }
210         }
211
212         @Override
213         public void onProgress(EnrolleeDevice enrolledevice) {
214             mCallback.onProgress(enrolledevice);
215         }
216     }
217
218     private void delayProvisioning(OnBoardingConnection conn) {
219         if (((IpOnBoardingConnection)conn).getThrottlingDelay()>0) {
220             try {
221                 Log.i(TAG, "waiting for 20 seconds to start provisioning");
222                 Thread.sleep(20000);//Sleep for allowing thin device to start the services
223             } catch (InterruptedException e) {
224                 e.printStackTrace();
225             }
226         }
227     }
228 }