Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / 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.util.Log;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30
31 /**
32  * This is facade class, a single point of contact for Application.
33  * It contains set of APIs to do easy setup of the enrolling device.
34  * ON-BOARDING - This is a step to establish connectivity between the device & Mediator device.
35  * PROVISION   - This is a step where the netowork's detail & credentials are given to the
36  * enrolling device.
37  */
38 public class EasySetupService {
39
40     private static final String TAG = EasySetupService.class.getName();
41
42     private static EasySetupService sInstance;
43
44     private final EasySetupStatus mCallback;
45
46     private ArrayList<EnrolleeDevice> mEnrolleeDeviceList;
47
48     private final ProvisioningCallback mProvisioningCallback;
49
50     private static Context mContext;
51
52     protected RemoteEnrollee mRemoteEnrollee;
53
54     //function to call the native createEnrolleeDevice
55     private native RemoteEnrollee nativeCreateEnrolleeDevice(String ip, String ssid,
56                                                      String password, int type, boolean isSecured );
57
58     static {
59         // Load Easy Setup JNI interface
60         System.loadLibrary("ESMediatorRich");
61         System.loadLibrary("easysetup-jni");
62     }
63
64     private EasySetupService(EasySetupStatus callback) {
65         mCallback = callback;
66         mProvisioningCallback = new ProvisioningCallbackImpl(mCallback);
67         mEnrolleeDeviceList = new ArrayList<EnrolleeDevice>();
68         mContext = null;
69     }
70
71     /**
72      * Gives a singleton instance of Easy setup service and initialize the service
73      *
74      * @param callback Application needs to provide this callback to receive the status of easy
75      *                 setup process.
76      */
77
78     public synchronized static EasySetupService getInstance(Context context, EasySetupStatus
79             callback) {
80         if (sInstance == null) {
81             sInstance = new EasySetupService(callback);
82             mContext = context;
83         }
84         return sInstance;
85     }
86
87     /**
88      * Reset the Easy setup Service
89      */
90
91     public void finish() {
92             //Call the stop Provisioning
93             for (EnrolleeDevice enrolleeDevice : mEnrolleeDeviceList) {
94                 enrolleeDevice.stopProvisioningProcess();
95         }
96     }
97
98     /**
99      * Starts Easy setup process for the enrolling device.
100      *
101      * @param enrolledevice Device to be enrolled in network
102      * @throws IOException Throws exception in case of any connection error.
103      */
104
105     public synchronized void startSetup(final EnrolleeDevice enrolledevice) throws IOException,ESException {
106
107         if (null == enrolledevice) {
108             throw new ESException("enrolledevice is NULL");
109         }
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
180         if (null == enrolleedevice) {
181             throw new ESException("enrolledevice is NULL");
182         }
183
184         if (mEnrolleeDeviceList.contains(enrolleedevice)) {
185             if (enrolleedevice.mState == EnrolleeState.DEVICE_ON_BOARDING_STATE) {
186                 Log.i(TAG, "stopOnBoardingProcess for enrolleedevice");
187                 enrolleedevice.stopOnBoardingProcess();
188             }else if (enrolleedevice.mState == EnrolleeState.DEVICE_PROVISIONING_STATE) {
189                 Log.i(TAG, "stopEnrolleeProvisioning for enrolleedevice");
190                 enrolleedevice.stopProvisioningProcess();
191             }
192             enrolleedevice.mState = EnrolleeState.DEVICE_INIT_STATE;
193             mCallback.onProgress(enrolleedevice);
194             mEnrolleeDeviceList.remove(enrolleedevice);
195         }
196     }
197
198     class ProvisioningCallbackImpl extends ProvisioningCallback {
199
200         private final EasySetupStatus mCallback;
201
202         ProvisioningCallbackImpl(EasySetupStatus callback) {
203             mCallback = callback;
204         }
205
206         @Override
207         public void onFinished(EnrolleeDevice enrolledevice) {
208             synchronized (EasySetupService.this) {
209                 if (mEnrolleeDeviceList.contains(enrolledevice)) {
210                     Log.i(TAG, "onFinished() is received " + enrolledevice.isSetupSuccessful());
211                     mCallback.onFinished(enrolledevice);
212                     mEnrolleeDeviceList.remove(enrolledevice);
213                 }
214             }
215         }
216
217         @Override
218         public void onProgress(EnrolleeDevice enrolledevice) {
219             mCallback.onProgress(enrolledevice);
220         }
221     }
222
223     private void delayProvisioning(OnBoardingConnection conn) {
224         if (((IpOnBoardingConnection)conn).getThrottlingDelay()>0) {
225             try {
226                 Log.i(TAG, "waiting for 20 seconds to start provisioning");
227                 Thread.sleep(20000);//Sleep for allowing thin device to start the services
228             } catch (InterruptedException e) {
229                 e.printStackTrace();
230             }
231         }
232     }
233 }