[ENROLLEE] Tizen enrollee sample application build using scons command
[platform/upstream/iotivity.git] / service / easy-setup / sdk / mediator / android / richsdk / EasySetupCore / src / main / java / org / iotivity / service / easysetup / mediator / EnrolleeDeviceWiFiOnboarding.java
1 /**
2  * ***************************************************************
3  * <p/>
4  * Copyright 2015 Samsung Electronics All Rights Reserved.
5  * <p/>
6  * <p/>
7  * <p/>
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
11  * <p/>
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * <p/>
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.
19  * <p/>
20  * ****************************************************************
21  */
22
23 package org.iotivity.service.easysetup.mediator;
24
25 import java.util.Timer;
26 import java.util.TimerTask;
27
28 import org.iotivity.service.easysetup.core.EnrolleeInfo;
29 import org.iotivity.service.easysetup.core.IOnBoardingStatus;
30 import org.iotivity.service.easysetup.core.ip.WiFiSoftAPManager;
31
32 import android.content.Context;
33 import android.net.wifi.WifiConfiguration;
34 import android.util.Log;
35
36 /**
37  * This is a ready to use class for Enrollee device having Soft AP as on-boarding connectivity.
38  */
39 public class EnrolleeDeviceWiFiOnboarding extends EnrolleeDevice {
40
41     public static final String TAG = EnrolleeDeviceWiFiOnboarding.class.getName();
42
43     final Context mContext;
44     final WiFiSoftAPManager mWifiSoftAPManager;
45     EnrolleeInfo connectedDevice;
46     Timer myTimer = null;
47
48     IOnBoardingStatus deviceScanListener = new IOnBoardingStatus() {
49
50         @Override
51         public void deviceOnBoardingStatus(EnrolleeInfo enrolleStatus) {
52             myTimer.cancel();
53             Log.d("ESSoftAPOnBoarding", "Entered");
54             if (mState == EnrolleeState.DEVICE_ON_BOARDING_STATE) {
55                 Log.d("ESSoftAPOnBoarding", "Device in OnBoarding State");
56                 if (enrolleStatus != null && enrolleStatus.getIpAddr() != null) {
57                     String finalResult = "Easy Connect : ";
58
59                     if (enrolleStatus.isReachable()) {
60                         finalResult = "Device OnBoarded" + "["
61                                 + enrolleStatus.getIpAddr() + "]";
62
63                         connectedDevice = enrolleStatus;
64                         IpOnBoardingConnection conn = new IpOnBoardingConnection();
65
66                         conn.setConnectivity(true);
67                         conn.setIp(connectedDevice.getIpAddr());
68                         conn.setHardwareAddress(enrolleStatus.getHWAddr());
69                         conn.setDeviceName(enrolleStatus.getDevice());
70
71                         Log.d("ESSoftAPOnBoarding", "Entered" + finalResult);
72                         mOnBoardingCallback.onFinished(conn);
73                         return;
74
75                     }
76                 }
77
78                 IpOnBoardingConnection conn = new IpOnBoardingConnection();
79                 conn.setConnectivity(false);
80                 mOnBoardingCallback.onFinished(conn);
81             } else {
82                 Log.e("ESSoftAPOnBoarding", "Device NOT in OnBoarding State. Ignoring the event");
83             }
84         }
85     };
86
87
88     protected EnrolleeDeviceWiFiOnboarding(Context context, OnBoardingConfig onBoardingConfig,
89                                            ProvisioningConfig provConfig) {
90         super(onBoardingConfig, provConfig);
91         mContext = context;
92         mState = EnrolleeState.DEVICE_INIT_STATE;
93         mWifiSoftAPManager = new WiFiSoftAPManager(mContext);
94     }
95
96     protected EnrolleeDeviceWiFiOnboarding(Context context, IpOnBoardingConnection conn,
97                                            ProvisioningConfig provConfig) {
98         super(new WiFiOnBoardingConfig(), provConfig);
99         mContext = context;
100         mState = EnrolleeState.DEVICE_ON_BOARDED_STATE;
101         mConnection = conn;
102         mWifiSoftAPManager = new WiFiSoftAPManager(mContext);
103     }
104
105     @Override
106     protected void startOnBoardingProcess() {
107         Log.i(TAG, "Starting on boarding process");
108
109         //1. Create Soft AP
110         boolean status = mWifiSoftAPManager.setWifiApEnabled((WifiConfiguration)
111                 mOnBoardingConfig.getConfig(), true);
112
113         mState = EnrolleeState.DEVICE_ON_BOARDING_STATE;
114
115         Log.i(TAG, "Soft AP is created with status " + status);
116
117         myTimer = new Timer();
118         myTimer.schedule(new TimerTask() {
119             @Override
120             public void run() {
121                 // Below function to be called after 5 seconds
122                 mWifiSoftAPManager.getClientList(deviceScanListener, 300);
123             }
124
125         }, 0, 5000);
126     }
127
128     protected void stopOnBoardingProcess() {
129         Log.i(TAG, "Stopping on boarding process");
130         if(myTimer != null)
131         {
132             myTimer.cancel();
133         }
134         boolean status = mWifiSoftAPManager.setWifiApEnabled(null, false);
135         Log.i(TAG, "Soft AP is disabled with status " + status);
136     }
137
138     @Override
139     protected void startProvisioningProcess(OnBoardingConnection conn)  {
140
141         mState = EnrolleeState.DEVICE_PROVISIONING_STATE;
142         mProvisioningCallback.onProgress(this);
143         final EnrolleeDevice device = this;
144         if (mProvConfig.getConnType() == ProvisioningConfig.ConnType.WiFi) {
145              try {
146                  mRemoteEnrollee.registerProvisioningHandler(new IProvisionStatusNativeHandler() {
147                      @Override
148                      public void onStatusRecieved(int state) {
149                          device.mState = convertIntToProvisioningState(state);
150                          Log.i(TAG,"Device state changed :"+device.mState);
151                          mProvisioningCallback.onProgress(device);
152                          if(0==state) {
153                              mProvisioningCallback.onFinished(EnrolleeDeviceWiFiOnboarding.this);
154                          }
155                      }
156                  });
157                  //native call
158                 mRemoteEnrollee.startProvision();
159              }catch(ESException e) {
160                  Log.i(TAG,"startProvisioningProcess Register Listener to native exception");
161              }
162         }
163     }
164
165     protected void stopProvisioningProcess(){
166         if(mState == EnrolleeState.DEVICE_PROVISIONING_STATE)
167         {    //native call
168             try {
169                 mRemoteEnrollee.stopProvision();
170             } catch (ESException e) {
171                 Log.i(TAG,"stopProvisioningProcess exception");
172             }
173         }else{
174            Log.i(TAG,"stopProvisioningProcess : Provisioning is not in progress");
175         }
176     }
177
178     private EnrolleeState convertIntToProvisioningState(int state){
179
180         switch(state)
181         {
182             case 0 :
183                 return EnrolleeState.DEVICE_PROVISIONED_STATE;
184             case 1:
185                 return EnrolleeState.DEVICE_NOT_PROVISIONED;
186             case 2:
187                 return EnrolleeState.DEVICE_PROVISIONED_STATE;
188             case 3:
189                 return EnrolleeState.DEVICE_NOT_PROVISIONED;
190         }
191         return EnrolleeState.DEVICE_INIT_STATE;
192     }
193 }