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.util.Log;
28 import java.io.IOException;
29 import java.util.ArrayList;
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
38 public class EasySetupService {
40 private static final String TAG = EasySetupService.class.getName();
42 private static EasySetupService sInstance;
44 private final EasySetupStatus mCallback;
46 private ArrayList<EnrolleeDevice> mEnrolleeDeviceList;
48 private final ProvisioningCallback mProvisioningCallback;
50 private static Context mContext;
52 protected RemoteEnrollee mRemoteEnrollee;
54 //function to call the native createEnrolleeDevice
55 private native RemoteEnrollee nativeCreateEnrolleeDevice(String ip, String ssid,
56 String password, int type, boolean isSecured );
59 // Load Easy Setup JNI interface
60 System.loadLibrary("ESMediatorRich");
61 System.loadLibrary("easysetup-jni");
64 private EasySetupService(EasySetupStatus callback) {
66 mProvisioningCallback = new ProvisioningCallbackImpl(mCallback);
67 mEnrolleeDeviceList = new ArrayList<EnrolleeDevice>();
72 * Gives a singleton instance of Easy setup service and initialize the service
74 * @param callback Application needs to provide this callback to receive the status of easy
78 public synchronized static EasySetupService getInstance(Context context, EasySetupStatus
80 if (sInstance == null) {
81 sInstance = new EasySetupService(callback);
88 * Reset the Easy setup Service
91 public void finish() {
92 //Call the stop Provisioning
93 for (EnrolleeDevice enrolleeDevice : mEnrolleeDeviceList) {
94 enrolleeDevice.stopProvisioningProcess();
99 * Starts Easy setup process for the enrolling device.
101 * @param enrolledevice Device to be enrolled in network
102 * @throws IOException Throws exception in case of any connection error.
105 public synchronized void startSetup(final EnrolleeDevice enrolledevice) throws IOException,ESException {
107 if (null == enrolledevice) {
108 throw new ESException("enrolledevice is NULL");
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 {
180 if (null == enrolleedevice) {
181 throw new ESException("enrolledevice is NULL");
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();
192 enrolleedevice.mState = EnrolleeState.DEVICE_INIT_STATE;
193 mCallback.onProgress(enrolleedevice);
194 mEnrolleeDeviceList.remove(enrolleedevice);
198 class ProvisioningCallbackImpl extends ProvisioningCallback {
200 private final EasySetupStatus mCallback;
202 ProvisioningCallbackImpl(EasySetupStatus callback) {
203 mCallback = callback;
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);
218 public void onProgress(EnrolleeDevice enrolledevice) {
219 mCallback.onProgress(enrolledevice);
223 private void delayProvisioning(OnBoardingConnection conn) {
224 if (((IpOnBoardingConnection)conn).getThrottlingDelay()>0) {
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) {