2 * ***************************************************************
4 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * ****************************************************************
23 package org.iotivity.service.easysetup.mediator;
25 import android.content.Context;
26 import android.net.wifi.WifiConfiguration;
27 import android.util.Log;
29 import java.io.IOException;
30 import java.util.ArrayList;
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
39 public class EasySetupService {
41 private static final String TAG = EasySetupService.class.getName();
43 private static EasySetupService sInstance;
45 private final EasySetupStatus mCallback;
47 private ArrayList<EnrolleeDevice> mEnrolleeDeviceList;
49 private final ProvisioningCallback mProvisioningCallback;
51 private static Context mContext;
53 protected RemoteEnrollee mRemoteEnrollee;
55 //function to call the native createEnrolleeDevice
56 private native RemoteEnrollee nativeCreateEnrolleeDevice(String ip, String ssid,
57 String password, int type, boolean isSecured );
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");
68 private EasySetupService(EasySetupStatus callback) {
70 mProvisioningCallback = new ProvisioningCallbackImpl(mCallback);
71 mEnrolleeDeviceList = new ArrayList<EnrolleeDevice>();
76 * Gives a singleton instance of Easy setup service and initialize the service
78 * @param callback Application needs to provide this callback to receive the status of easy
82 public synchronized static EasySetupService getInstance(Context context, EasySetupStatus
84 if (sInstance == null) {
85 sInstance = new EasySetupService(callback);
92 * Reset the Easy setup Service
95 public void finish() {
96 //Call the stop Provisioning
97 for (EnrolleeDevice enrolleeDevice : mEnrolleeDeviceList) {
98 enrolleeDevice.stopProvisioningProcess();
103 * Starts Easy setup process for the enrolling device.
105 * @param enrolledevice Device to be enrolled in network
106 * @throws IOException Throws exception in case of any connection error.
109 public synchronized void startSetup(final EnrolleeDevice enrolledevice) throws IOException,ESException {
111 mEnrolleeDeviceList.add(enrolledevice);
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;
119 String ssid = config.getSsId();
120 String password = config.getPassword();
121 int connectivityType = config.getConnType().getValue();
122 boolean isSecured = config.isSecured();
125 mRemoteEnrollee = nativeCreateEnrolleeDevice(ip, ssid, password,
126 connectivityType, isSecured);
127 enrolledevice.mRemoteEnrollee = mRemoteEnrollee;
129 enrolledevice.startProvisioning(mProvisioningCallback);
132 enrolledevice.mState = EnrolleeState.DEVICE_ON_BOARDING_STATE;
133 mCallback.onProgress(enrolledevice);
134 enrolledevice.startOnBoarding(new OnBoardingCallback() {
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);
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();
155 mRemoteEnrollee = nativeCreateEnrolleeDevice(ip, ssid, password,
156 connectivityType, isSecured);
158 enrolledevice.mRemoteEnrollee = mRemoteEnrollee;
160 /* Delay is set according to Soft AP host and Mediator platform;
161 * For Android with Soft AP it is 2000 ms
163 delayProvisioning(connection);
164 enrolledevice.startProvisioning(mProvisioningCallback);
166 enrolledevice.mState = EnrolleeState.DEVICE_INIT_STATE;
167 mProvisioningCallback.onFinished(enrolledevice);
174 * Stops on-going Easy setup process for enrolling device.
176 * @param enrolleedevice Device to be enrolled in network
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();
187 enrolleedevice.mState = EnrolleeState.DEVICE_INIT_STATE;
188 mCallback.onProgress(enrolleedevice);
189 mEnrolleeDeviceList.remove(enrolleedevice);
193 class ProvisioningCallbackImpl extends ProvisioningCallback {
195 private final EasySetupStatus mCallback;
197 ProvisioningCallbackImpl(EasySetupStatus callback) {
198 mCallback = callback;
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);
213 public void onProgress(EnrolleeDevice enrolledevice) {
214 mCallback.onProgress(enrolledevice);
218 private void delayProvisioning(OnBoardingConnection conn) {
219 if (((IpOnBoardingConnection)conn).getThrottlingDelay()>0) {
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) {