e0ed145812eed124bac64e4ca0ca70367f198bfa
[platform/upstream/iotivity.git] / service / easy-setup / sdk / mediator / android / EasySetupCore / src / main / java / org / iotivity / service / easysetup / core / EasySetupService.java
1 /**\r
2  * ***************************************************************\r
3  * <p/>\r
4  * Copyright 2015 Samsung Electronics All Rights Reserved.\r
5  * <p/>\r
6  * <p/>\r
7  * <p/>\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
11  * <p/>\r
12  * http://www.apache.org/licenses/LICENSE-2.0\r
13  * <p/>\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
19  * <p/>\r
20  * ****************************************************************\r
21  */\r
22 \r
23 package org.iotivity.service.easysetup.core;\r
24 \r
25 import android.content.Context;\r
26 import android.util.Log;\r
27 \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
31 \r
32 import java.io.IOException;\r
33 import java.util.ArrayList;\r
34 \r
35 /**\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
40  * enrolling device.\r
41  */\r
42 public class EasySetupService {\r
43 \r
44     public static String TAG = EasySetupService.class.getName();\r
45 \r
46     private static EasySetupService sInstance;\r
47 \r
48     private final EasySetupStatus mCallback;\r
49 \r
50     private ArrayList<EnrolleeDevice> mEnrolleeDeviceList;\r
51 \r
52     private final ProvisioningCallback mProvisioningCallback;\r
53 \r
54     private static Context mContext;\r
55 \r
56     public EnrolleeDeviceFactory mDeviceFactory;\r
57 \r
58     ProvisionEnrollee mProvisionEnrolleeInstance;\r
59 \r
60     private EasySetupService(EasySetupStatus callback) {\r
61         mCallback = callback;\r
62         mProvisioningCallback = new ProvisioningCallbackImpl(mCallback);\r
63         mEnrolleeDeviceList = new ArrayList<EnrolleeDevice>();\r
64         mContext = null;\r
65         mDeviceFactory = null;\r
66     }\r
67 \r
68     /**\r
69      * Gives a singleton instance of Easy setup service and initialize the service\r
70      *\r
71      * @param callback Application needs to provide this callback to receive the status of easy\r
72      *                 setup process.\r
73      */\r
74 \r
75     public synchronized static EasySetupService getInstance(Context context, EasySetupStatus\r
76             callback) {\r
77         if (sInstance == null) {\r
78             sInstance = new EasySetupService(callback);\r
79             mContext = context;\r
80         }\r
81         return sInstance;\r
82     }\r
83 \r
84     /**\r
85      * Reset the Easy setup Service\r
86      */\r
87 \r
88     public void finish() {\r
89         //Native Api call to reset OIC stack\r
90         if(mProvisionEnrolleeInstance != null)\r
91         {\r
92             mProvisionEnrolleeInstance.stopEnrolleeProvisioning(0);\r
93         }\r
94     }\r
95 \r
96     /**\r
97      * Starts Easy setup process for the enrolling device.\r
98      *\r
99      * @param enrolledevice Device to be enrolled in network\r
100      * @throws IOException Throws exception in case of any connection error.\r
101      */\r
102 \r
103     public synchronized void startSetup(final EnrolleeDevice enrolledevice) throws IOException {\r
104 \r
105         mEnrolleeDeviceList.add(enrolledevice);\r
106 \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
110             return;\r
111         }\r
112 \r
113         enrolledevice.startOnBoarding(new OnBoardingCallback() {\r
114 \r
115             @Override\r
116             public void onFinished(OnBoardingConnection connection) {\r
117                 if (connection.isConnected()) {\r
118                     Log.i(TAG, "On boarding is successful ");\r
119                     try {\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
124                     }\r
125 \r
126                     // Start provisioning here\r
127                     enrolledevice.setConnection(connection);\r
128                     enrolledevice.startProvisioning(mProvisioningCallback);\r
129                 } else {\r
130                     enrolledevice.mState = EnrolleeState.DEVICE_PROVISIONING_FAILED_STATE;\r
131                     mProvisioningCallback.onFinished(enrolledevice);\r
132                 }\r
133 \r
134             }\r
135 \r
136         });\r
137 \r
138     }\r
139 \r
140     /**\r
141      * Stops on-going Easy setup process for enrolling device.\r
142      *\r
143      * @param enrolleedevice Device to be enrolled in network\r
144      */\r
145     public synchronized void stopSetup(EnrolleeDevice enrolleedevice) {\r
146         enrolleedevice.stopOnBoardingProcess();\r
147         mEnrolleeDeviceList.remove(enrolleedevice);\r
148 \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
151     }\r
152 \r
153     public synchronized void getEnrolleeDevice(OnBoardingConfig connType) {\r
154         mDeviceFactory = EnrolleeDeviceFactory.newInstance(mContext);\r
155     }\r
156 \r
157     class ProvisioningCallbackImpl extends ProvisioningCallback {\r
158 \r
159         private final EasySetupStatus mCallback;\r
160 \r
161         ProvisioningCallbackImpl(EasySetupStatus callback) {\r
162             mCallback = callback;\r
163         }\r
164 \r
165         @Override\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
170             // }\r
171         }\r
172 \r
173     }\r
174 \r
175 \r
176 }\r