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.core;
\r
25 import android.content.Context;
\r
26 import android.util.Log;
\r
28 import org.iotivity.service.easysetup.impl.EnrolleeDeviceFactory;
\r
29 import org.iotivity.service.easysetup.mediator.EasySetupManager;
\r
30 import org.iotivity.service.easysetup.mediator.ProvisionEnrollee;
\r
32 import java.io.IOException;
\r
33 import java.util.ArrayList;
\r
36 * This is facade class, a single point of contact for Application.
\r
37 * It contains set of APIs to do easy setup of the enrolling device.
\r
38 * ON-BOARDING - This is a step to establish connectivity between the device & Mediator device.
\r
39 * PROVISION - This is a step where the netowork's detail & credentials are given to the
\r
42 public class EasySetupService {
\r
44 public static String TAG = EasySetupService.class.getName();
\r
46 private static EasySetupService sInstance;
\r
48 private final EasySetupStatus mCallback;
\r
50 private ArrayList<EnrolleeDevice> mEnrolleeDeviceList;
\r
52 private final ProvisioningCallback mProvisioningCallback;
\r
54 private static Context mContext;
\r
56 public EnrolleeDeviceFactory mDeviceFactory;
\r
58 ProvisionEnrollee mProvisionEnrolleeInstance;
\r
60 private EasySetupService(EasySetupStatus callback) {
\r
61 mCallback = callback;
\r
62 mProvisioningCallback = new ProvisioningCallbackImpl(mCallback);
\r
63 mEnrolleeDeviceList = new ArrayList<EnrolleeDevice>();
\r
65 mDeviceFactory = null;
\r
69 * Gives a singleton instance of Easy setup service and initialize the service
\r
71 * @param callback Application needs to provide this callback to receive the status of easy
\r
75 public synchronized static EasySetupService getInstance(Context context, EasySetupStatus
\r
77 if (sInstance == null) {
\r
78 sInstance = new EasySetupService(callback);
\r
85 * Reset the Easy setup Service
\r
88 public void finish() {
\r
89 //Native Api call to reset OIC stack
\r
90 if(mProvisionEnrolleeInstance != null)
\r
92 mProvisionEnrolleeInstance.stopEnrolleeProvisioning(0);
\r
97 * Starts Easy setup process for the enrolling device.
\r
99 * @param enrolledevice Device to be enrolled in network
\r
100 * @throws IOException Throws exception in case of any connection error.
\r
103 public synchronized void startSetup(final EnrolleeDevice enrolledevice) throws IOException {
\r
105 mEnrolleeDeviceList.add(enrolledevice);
\r
107 // Starts the provisioning directly if the device is already on boarded on the network.
\r
108 if (enrolledevice.onBoarded()) {
\r
109 enrolledevice.startProvisioning(mProvisioningCallback);
\r
113 enrolledevice.startOnBoarding(new OnBoardingCallback() {
\r
116 public void onFinished(OnBoardingConnection connection) {
\r
117 if (connection.isConnected()) {
\r
118 Log.i(TAG, "On boarding is successful ");
\r
120 Log.i(TAG, "waiting for 15 seconds to start provisioning");
\r
121 Thread.sleep(15000);//Sleep for allowing thin device to start the services
\r
122 } catch (InterruptedException e) {
\r
123 e.printStackTrace();
\r
126 // Start provisioning here
\r
127 enrolledevice.setConnection(connection);
\r
128 enrolledevice.startProvisioning(mProvisioningCallback);
\r
130 enrolledevice.mState = EnrolleeState.DEVICE_PROVISIONING_FAILED_STATE;
\r
131 mProvisioningCallback.onFinished(enrolledevice);
\r
141 * Stops on-going Easy setup process for enrolling device.
\r
143 * @param enrolleedevice Device to be enrolled in network
\r
145 public synchronized void stopSetup(EnrolleeDevice enrolleedevice) {
\r
146 enrolleedevice.stopOnBoardingProcess();
\r
147 mEnrolleeDeviceList.remove(enrolleedevice);
\r
149 //Native Api call to stop on-going enrolling process for the enrolling device
\r
150 EasySetupManager.getInstance().stopEnrolleeProvisioning(enrolleedevice.mOnBoardingConfig.getConnType().getValue());
\r
153 public synchronized void getEnrolleeDevice(OnBoardingConfig connType) {
\r
154 mDeviceFactory = EnrolleeDeviceFactory.newInstance(mContext);
\r
157 class ProvisioningCallbackImpl extends ProvisioningCallback {
\r
159 private final EasySetupStatus mCallback;
\r
161 ProvisioningCallbackImpl(EasySetupStatus callback) {
\r
162 mCallback = callback;
\r
166 public void onFinished(EnrolleeDevice enrolledevice) {
\r
167 //if(mEnrolleeDeviceList.contains(enrolledevice)) {
\r
168 Log.i(TAG, "onFinished() is received " + enrolledevice.isSetupSuccessful());
\r
169 mCallback.onFinished(enrolledevice);
\r