Added missing license headers to Java files
[platform/upstream/iotivity.git] / service / easy-setup / sdk / mediator / android / EasySetupCore / src / main / java / org / iotivity / service / easysetup / impl / 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.impl;
24
25 import java.util.Timer;
26 import java.util.TimerTask;
27
28 import org.iotivity.service.easysetup.core.ConnectionInterface;
29 import org.iotivity.service.easysetup.core.EnrolleeDevice;
30 import org.iotivity.service.easysetup.core.EnrolleeState;
31 import org.iotivity.service.easysetup.core.IpConnection;
32 import org.iotivity.service.easysetup.core.OnBoardingConfig;
33 import org.iotivity.service.easysetup.core.ProvisioningConfig;
34 import org.iotivity.service.easysetup.mediator.EasySetupCallbackHandler;
35 import org.iotivity.service.easysetup.mediator.EasySetupManager;
36 import org.iotivity.service.easysetup.mediator.EnrolleeInfo;
37 import org.iotivity.service.easysetup.mediator.IOnBoardingStatus;
38 import org.iotivity.service.easysetup.mediator.IProvisioningListener;
39 import org.iotivity.service.easysetup.mediator.ProvisionEnrollee;
40 import org.iotivity.service.easysetup.mediator.ip.WiFiSoftAPManager;
41
42 import android.content.Context;
43 import android.net.wifi.WifiConfiguration;
44 import android.util.Log;
45 import android.widget.Toast;
46
47 /**
48  * This is a ready to use class for Enrollee device having Soft AP as on-boarding connectivity.
49  */
50 public class EnrolleeDeviceWiFiOnboarding extends EnrolleeDevice {
51
52     public static final String TAG = EnrolleeDeviceWiFiOnboarding.class.getName();
53
54     final Context mContext;
55     final WiFiSoftAPManager mWifiSoftAPManager;
56     EnrolleeInfo connectedDevice;
57     private EasySetupManager easySetupManagerNativeInstance;
58     ProvisionEnrollee provisionEnrolleInstance;
59
60     IOnBoardingStatus deviceScanListener = new IOnBoardingStatus() {
61
62         @Override
63         public void deviceOnBoardingStatus(EnrolleeInfo enrolleStatus) {
64             Log.d("ESSoftAPOnBoarding", "Entered");
65             if (enrolleStatus != null && enrolleStatus.getIpAddr() != null) {
66                 String finalResult = "Easy Connect : ";
67
68                 if (enrolleStatus.isReachable()) {
69                     finalResult = "Device OnBoarded" + "["
70                             + enrolleStatus.getIpAddr() + "]";
71
72                     connectedDevice = enrolleStatus;
73                     IpConnection conn = new IpConnection();
74                     conn.setConnectivity(true);
75                     conn.setIp(connectedDevice.getIpAddr());
76                     Log.d("ESSoftAPOnBoarding", "Entered");
77                     mOnBoardingCallback.onFinished(conn);
78                     return;
79
80                 }
81             }
82
83             IpConnection conn = new IpConnection();
84             conn.setConnectivity(false);
85             mOnBoardingCallback.onFinished(conn);
86         }
87     };
88
89
90     protected EnrolleeDeviceWiFiOnboarding(Context context, OnBoardingConfig onBoardingConfig, ProvisioningConfig provConfig) {
91         super(onBoardingConfig, provConfig);
92         mContext = context;
93         mWifiSoftAPManager = new WiFiSoftAPManager(mContext);
94     }
95
96     @Override
97     protected void startOnBoardingProcess() {
98         Log.i(TAG, "Starging on boarding process");
99
100         //1. Create Soft AP
101         boolean status = mWifiSoftAPManager.setWifiApEnabled((WifiConfiguration) mOnBoardingConfig.getConfig(), true);
102
103         Log.i(TAG, "Soft AP is created with status " + status);
104
105         Timer myTimer;
106         myTimer = new Timer();
107         myTimer.schedule(new TimerTask() {
108             @Override
109             public void run() {
110                 // Below function to be called after 5 seconds
111                 mWifiSoftAPManager.getClientList(deviceScanListener, 300);
112             }
113
114         }, 0, 5000);
115     }
116
117     protected void stopOnBoardingProcess() {
118         Log.i(TAG, "Stopping on boarding process");
119         boolean status = mWifiSoftAPManager.setWifiApEnabled(null, false);
120         Log.i(TAG, "Soft AP is disabled with status " + status);
121     }
122
123     @Override
124     protected void startProvisioningProcess(ConnectionInterface conn) {
125
126         if (mProvConfig.getConnType() == ProvisioningConfig.ConnType.WiFi) {
127
128             provisionEnrolleInstance = new ProvisionEnrollee(mContext);
129             provisionEnrolleInstance.registerProvisioningHandler(new IProvisioningListener() {
130                 @Override
131                 public void onFinishProvisioning(int statuscode) {
132                     mState = (statuscode == 0) ? EnrolleeState.DEVICE_PROVISIONING_SUCCESS_STATE : EnrolleeState.DEVICE_PROVISIONING_FAILED_STATE;
133                     mProvisioningCallback.onFinished(EnrolleeDeviceWiFiOnboarding.this);
134                 }
135             });
136
137             IpConnection connection = (IpConnection) conn;
138             WiFiProvConfig wifiProvConfig = (WiFiProvConfig) mProvConfig;
139             easySetupManagerNativeInstance = EasySetupManager.getInstance();
140             easySetupManagerNativeInstance.initEasySetup();
141             easySetupManagerNativeInstance.provisionEnrollee(connection.getIp(), wifiProvConfig.getSsId(), wifiProvConfig.getPassword(), 0 /*In base code '0' is hard coded*/);
142
143         }
144
145     }
146
147
148 }