2 * ***************************************************************
\r
4 * Copyright 2015 Samsung Electronics All Rights Reserved.
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
20 * ****************************************************************
\r
23 package org.iotivity.service.easysetup.mediator;
\r
25 import android.content.Context;
\r
26 import android.util.Log;
\r
28 import org.iotivity.service.easysetup.core.EasySetupManager;
\r
29 import org.iotivity.service.easysetup.core.ProvisionEnrollee;
\r
31 import java.io.IOException;
\r
32 import java.util.ArrayList;
\r
35 * This is facade class, a single point of contact for Application.
\r
36 * It contains set of APIs to do easy setup of the enrolling device.
\r
37 * ON-BOARDING - This is a step to establish connectivity between the device & Mediator device.
\r
38 * PROVISION - This is a step where the netowork's detail & credentials are given to the
\r
41 public class EasySetupService {
\r
43 private static final String TAG = EasySetupService.class.getName();
\r
45 private static EasySetupService sInstance;
\r
47 private final EasySetupStatus mCallback;
\r
49 private ArrayList<EnrolleeDevice> mEnrolleeDeviceList;
\r
51 private final ProvisioningCallback mProvisioningCallback;
\r
53 private static Context mContext;
\r
56 ProvisionEnrollee mProvisionEnrolleeInstance;
\r
58 private EasySetupService(EasySetupStatus callback) {
\r
59 mCallback = callback;
\r
60 mProvisioningCallback = new ProvisioningCallbackImpl(mCallback);
\r
61 mEnrolleeDeviceList = new ArrayList<EnrolleeDevice>();
\r
67 * Gives a singleton instance of Easy setup service and initialize the service
\r
69 * @param callback Application needs to provide this callback to receive the status of easy
\r
73 public synchronized static EasySetupService getInstance(Context context, EasySetupStatus
\r
75 if (sInstance == null) {
\r
76 sInstance = new EasySetupService(callback);
\r
83 * Reset the Easy setup Service
\r
86 public void finish() {
\r
88 //Native Api call to reset OIC stack
\r
89 if (mProvisionEnrolleeInstance != null) {
\r
90 mProvisionEnrolleeInstance.stopEnrolleeProvisioning(0);
\r
95 * Starts Easy setup process for the enrolling device.
\r
97 * @param enrolledevice Device to be enrolled in network
\r
98 * @throws IOException Throws exception in case of any connection error.
\r
101 public synchronized void startSetup(final EnrolleeDevice enrolledevice) throws IOException {
\r
103 mEnrolleeDeviceList.add(enrolledevice);
\r
105 // Starts the provisioning directly if the device is already on boarded on the network.
\r
106 if (enrolledevice.onBoarded()) {
\r
107 enrolledevice.startProvisioning(mProvisioningCallback);
\r
110 enrolledevice.mState = EnrolleeState.DEVICE_ON_BOARDING_STATE;
\r
111 mCallback.onProgress(enrolledevice);
\r
112 enrolledevice.startOnBoarding(new OnBoardingCallback() {
\r
115 public void onFinished(OnBoardingConnection connection) {
\r
116 if (connection.isConnected()) {
\r
117 Log.i(TAG, "On boarding is successful ");
\r
118 // Start provisioning here
\r
119 enrolledevice.mState = EnrolleeState.DEVICE_ON_BOARDED_STATE;
\r
120 mCallback.onProgress(enrolledevice);
\r
121 enrolledevice.setConnection(connection);
\r
123 /* Delay is set according to Soft AP host and Mediator platform;
\r
124 * For Android with Soft AP it is 2000 ms
\r
126 delayProvisioning(connection);
\r
127 enrolledevice.startProvisioning(mProvisioningCallback);
\r
129 enrolledevice.mState = EnrolleeState.DEVICE_INIT_STATE;
\r
130 mProvisioningCallback.onFinished(enrolledevice);
\r
140 * Stops on-going Easy setup process for enrolling device.
\r
142 * @param enrolleedevice Device to be enrolled in network
\r
144 public synchronized void stopSetup(EnrolleeDevice enrolleedevice) {
\r
145 if (mEnrolleeDeviceList.contains(enrolleedevice)) {
\r
146 enrolleedevice.mState = EnrolleeState.DEVICE_INIT_STATE;
\r
147 mCallback.onProgress(enrolleedevice);
\r
148 if (enrolleedevice.mState == EnrolleeState.DEVICE_ON_BOARDING_STATE) {
\r
149 Log.i(TAG, "stopOnBoardingProcess for enrolleedevice");
\r
150 enrolleedevice.stopOnBoardingProcess();
\r
151 mEnrolleeDeviceList.remove(enrolleedevice);
\r
152 } else if (enrolleedevice.mState == EnrolleeState.DEVICE_PROVISIONING_STATE) {
\r
153 Log.i(TAG, "stopOnBoardingProcess for enrolleedevice");
\r
154 enrolleedevice.stopOnBoardingProcess();
\r
155 Log.i(TAG, "stopEnrolleeProvisioning for enrolleedevice");
\r
156 //Native Api call to stop on-going enrolling process for the enrolling device
\r
157 EasySetupManager.getInstance().stopEnrolleeProvisioning(enrolleedevice
\r
158 .mOnBoardingConfig.getConnType().getValue());
\r
159 mEnrolleeDeviceList.remove(enrolleedevice);
\r
165 class ProvisioningCallbackImpl extends ProvisioningCallback {
\r
167 private final EasySetupStatus mCallback;
\r
169 ProvisioningCallbackImpl(EasySetupStatus callback) {
\r
170 mCallback = callback;
\r
174 public void onFinished(EnrolleeDevice enrolledevice) {
\r
175 synchronized (EasySetupService.this) {
\r
176 if (mEnrolleeDeviceList.contains(enrolledevice)) {
\r
177 Log.i(TAG, "onFinished() is received " + enrolledevice.isSetupSuccessful());
\r
178 mCallback.onFinished(enrolledevice);
\r
179 mEnrolleeDeviceList.remove(enrolledevice);
\r
185 public void onProgress(EnrolleeDevice enrolledevice) {
\r
186 mCallback.onProgress(enrolledevice);
\r
192 private void delayProvisioning(OnBoardingConnection conn) {
\r
193 if (((IpOnBoardingConnection)conn).getThrottlingDelay()>0) {
\r
195 Log.i(TAG, "waiting for 20 seconds to start provisioning");
\r
196 Thread.sleep(20000);//Sleep for allowing thin device to start the services
\r
197 } catch (InterruptedException e) {
\r
198 e.printStackTrace();
\r