Remove a dependency of the deprecated wifi API
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / enrollee / tizen-sdb / EnrolleeSample / easysetup_wifi_conn.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21
22 #include "easysetup_wifi_conn.h"
23 #include "string.h"
24 #include "logger.h"
25 #include "escommon.h"
26
27 #include <wifi-manager.h>
28 #include <tizen_error.h>
29 #include <glib.h>
30 #include <unistd.h>
31
32 /**
33  * @var ES_WIFICONN_TAG
34  * @brief Logging tag for module name.
35  */
36 #define ES_WIFICONN_TAG "ESWIFICONN"
37
38 typedef struct{
39     char wifiName[OIC_STRING_MAX_VALUE];
40     char wifiPasswd[OIC_STRING_MAX_VALUE];
41 } TargetWifiInfo;
42
43 static bool gWiFiConnFlag;
44 static wifi_manager_h *wifi_handle;
45
46 char* PrintWifiErr(wifi_manager_error_e errType)
47 {
48     switch (errType) {
49         case WIFI_MANAGER_ERROR_NONE:
50             return "NONE";
51         case WIFI_MANAGER_ERROR_INVALID_PARAMETER:
52             return "INVALID_PARAMETER";
53         case WIFI_MANAGER_ERROR_OUT_OF_MEMORY:
54             return "OUT_OF_MEMORY";
55         case WIFI_MANAGER_ERROR_INVALID_OPERATION:
56             return "INVALID_OPERATION";
57         case WIFI_MANAGER_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
58             return "ADDRESS_FAMILY_NOT_SUPPORTED";
59         case WIFI_MANAGER_ERROR_OPERATION_FAILED:
60             return "OPERATION_FAILED";
61         case WIFI_MANAGER_ERROR_NO_CONNECTION:
62             return "NO_CONNECTION";
63         case WIFI_MANAGER_ERROR_NOW_IN_PROGRESS:
64             return "NOW_IN_PROGRESS";
65         case WIFI_MANAGER_ERROR_ALREADY_EXISTS:
66             return "ALREADY_EXISTS";
67         case WIFI_MANAGER_ERROR_OPERATION_ABORTED:
68             return "OPERATION_ABORTED";
69         case WIFI_MANAGER_ERROR_DHCP_FAILED:
70             return "DHCP_FAILED";
71         case WIFI_MANAGER_ERROR_INVALID_KEY:
72             return "INVALID_KEY";
73         case WIFI_MANAGER_ERROR_NO_REPLY:
74             return "NO_REPLY";
75         case WIFI_MANAGER_ERROR_SECURITY_RESTRICTED:
76             return "SECURITY_RESTRICTED";
77         case WIFI_MANAGER_ERROR_PERMISSION_DENIED:
78             return "PERMISSION_DENIED";
79         case WIFI_MANAGER_ERROR_NOT_SUPPORTED:
80             return "NOT_SUPPORTED";
81     }
82
83     return "UNKNOWN";
84 }
85
86 static void WiFiScanCallback(wifi_manager_error_e err, void* data)
87 {
88     if(data != NULL) {
89         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "UserData : %s", (char *) data);
90     }
91
92     OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Scan Callback Res : %s", PrintWifiErr(err));
93 }
94
95 static void WiFiActivateCallback(wifi_manager_error_e result, void* user_data)
96 {
97     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "__test_activated_callback IN");
98
99     int ret = wifi_manager_scan(wifi_handle, WiFiScanCallback, NULL);
100     if (ret != WIFI_MANAGER_ERROR_NONE) {
101         OIC_LOG(ERROR, ES_WIFICONN_TAG, "WiFi Scan Error");
102         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Scan Fail Status : %s", PrintWifiErr((wifi_manager_error_e) ret));
103     }
104
105     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "__test_activated_callback OUT");
106
107 }
108
109 static void ConnectedCallback(wifi_manager_error_e err, void* data)
110 {
111     if(err == WIFI_MANAGER_ERROR_NONE) {
112         OIC_LOG(DEBUG, ES_WIFICONN_TAG, "Success to Connect AP");        
113     }
114     else {
115         OIC_LOG(ERROR, ES_WIFICONN_TAG, "Fail to Connect AP");        
116     }
117 }
118
119 static bool WiFiFoundCallback(wifi_ap_h ap, void *data)
120 {
121     TargetWifiInfo* targetInfo = (TargetWifiInfo *) data;
122
123     char *foundAP = NULL;
124
125     int ret = wifi_ap_get_essid(ap, &foundAP);
126
127     if(ret != WIFI_MANAGER_ERROR_NONE) {
128         OIC_LOG(ERROR, ES_WIFICONN_TAG, "Fail to get AP");
129         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Fail Status : %s", PrintWifiErr((wifi_manager_error_e) ret));
130         return false;
131     }
132
133     OIC_LOG_V(DEBUG, ES_WIFICONN_TAG, "Found AP name : %s", foundAP);
134
135     if(strstr(foundAP, targetInfo->wifiName) != NULL) {
136
137         bool needPasswd = false;
138
139         if (wifi_ap_is_passphrase_required(ap, &needPasswd) == WIFI_MANAGER_ERROR_NONE) {
140
141             OIC_LOG(DEBUG, ES_WIFICONN_TAG, "Passsword required");
142
143             ret = wifi_ap_set_passphrase(ap, targetInfo->wifiPasswd);
144             if (ret != WIFI_MANAGER_ERROR_NONE) {
145                 OIC_LOG(ERROR, ES_WIFICONN_TAG, "Fail to Set Password");
146                 OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Fail Status : %s", PrintWifiErr((wifi_manager_error_e) ret));
147                 g_free(foundAP);
148                 return false;
149             }
150
151             ret = wifi_manager_connect(wifi_handle, ap, ConnectedCallback, targetInfo);
152             if (ret != WIFI_MANAGER_ERROR_NONE) {
153                 OIC_LOG(ERROR, ES_WIFICONN_TAG, "Fail to connect wifi");
154                 OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Connect Fail Status : %s", PrintWifiErr((wifi_manager_error_e) ret));
155                 g_free(foundAP);
156                 return false;                
157             }
158             else {
159                 OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Success to connect wifi : %s", PrintWifiErr((wifi_manager_error_e) ret));
160                 gWiFiConnFlag = true;
161                 g_free(foundAP);
162                 return false;                
163             }
164
165
166         }
167         else {
168             OIC_LOG(ERROR, ES_WIFICONN_TAG, "Fail to get Passphrase Required");
169             OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Fail Status : %s", PrintWifiErr((wifi_manager_error_e) ret));
170             return false;
171         }
172
173     }
174     else {
175         OIC_LOG(DEBUG, ES_WIFICONN_TAG, "This AP is not the one wanted to be connected");
176         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Searching : %s / Searched : %s", targetInfo->wifiName, foundAP);
177     }
178
179     g_free(foundAP);
180
181     return true;
182 }
183
184 WiFiConnErrCode TizenWiFiDeinit()
185 {
186     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "TizenWiFiDeinit IN");
187
188     int ret = wifi_manager_deinitialize(&wifi_handle);
189         wifi_handle = null;
190
191     if(ret != WIFI_MANAGER_ERROR_NONE) {
192         OIC_LOG(ERROR, ES_WIFICONN_TAG, "wifi deinit error");
193         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Fail Status : %s", PrintWifiErr((wifi_manager_error_e) ret));
194         return WIFI_DEINIT_ERROR;
195     }
196
197     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "TizenWiFiDeinit OUT");
198     return WIFI_NO_ERROR;
199 }
200
201 WiFiConnErrCode TizenWiFiInit()
202 {
203     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "TizenWiFiInit IN");
204
205     gWiFiConnFlag = false;
206     int ret = wifi_mananger_initialize(&wifi_handle);
207     if (ret != WIFI_MANAGER_ERROR_NONE) {
208         OIC_LOG(ERROR, ES_WIFICONN_TAG, "WiFi Init Error");
209         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Init Fail Status : %s", PrintWifiErr((wifi_manager_error_e) ret));
210         return WIFI_INIT_ERROR;
211     }
212
213     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "TizenWiFiInit OUT");
214
215     return WIFI_NO_ERROR;
216 }
217
218 WiFiConnErrCode TizenWiFiScanStart()
219 {
220     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "TizenWiFiScanStart IN");
221     int ret;
222
223     bool wifiState = false;
224     wifi_manager_is_activated(wifi_handle, &wifiState);
225
226     // if wifi is not activated, actviate & scan
227     if (wifiState == false) {
228         OIC_LOG(ERROR, ES_WIFICONN_TAG, "WiFi Not Activated...Activate it!");
229         ret = wifi_manager_activate(wifi_handle, WiFiActivateCallback, NULL);
230
231         if(ret != WIFI_MANAGER_ERROR_NONE) {
232             OIC_LOG(ERROR, ES_WIFICONN_TAG, "wifi activate error");
233             return WIFI_ACTIVATE_ERROR;
234         }
235     }
236     else{
237         ret = wifi_manager_scan(wifi_handle, WiFiScanCallback, NULL);
238         if (ret != WIFI_MANAGER_ERROR_NONE) {
239             OIC_LOG(ERROR, ES_WIFICONN_TAG, "WiFi Scan Error");
240             OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Scan Fail Status : %s", PrintWifiErr((wifi_manager_error_e) ret));
241             return WIFI_SCAN_ERROR;
242         }
243     }
244
245     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "TizenWiFiScanStart OUT");
246
247     return WIFI_NO_ERROR;
248 }
249
250
251 WiFiConnErrCode TizenWiFiConn(char *ssid, char* passwd)
252 {
253     TargetWifiInfo targetInfo;
254
255     memset(targetInfo.wifiName, 0, OIC_STRING_MAX_VALUE);
256     memset(targetInfo.wifiPasswd, 0, OIC_STRING_MAX_VALUE);
257     if(ssid != NULL)
258     {
259         strncpy(targetInfo.wifiName, ssid, strlen(ssid));
260     }
261
262     if(passwd != NULL)
263     {
264         strncpy(targetInfo.wifiPasswd, passwd, strlen(passwd));
265     }
266
267     int ret = wifi_manager_foreach_found_ap(wifi_handle, WiFiFoundCallback, &targetInfo);
268     if(ret != WIFI_MANAGER_ERROR_NONE) {
269         OIC_LOG(ERROR, ES_WIFICONN_TAG, "wifi_foreach_found_aps Error");
270         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Fail Status : %s", PrintWifiErr((wifi_manager_error_e) ret));
271         memset(targetInfo.wifiName, 0, OIC_STRING_MAX_VALUE);
272         memset(targetInfo.wifiPasswd, 0, OIC_STRING_MAX_VALUE);
273
274         return WIFI_CONN_ERROR;
275     }
276
277     // try to find target AP during 10[sec]
278     int times = 1;
279     while(!gWiFiConnFlag) {
280         sleep(1);
281
282         if(times >= 10){
283             OIC_LOG(ERROR, ES_WIFICONN_TAG, "Connection Error... try 10[sec]");
284             memset(targetInfo.wifiName, 0, OIC_STRING_MAX_VALUE);
285             memset(targetInfo.wifiPasswd, 0, OIC_STRING_MAX_VALUE);
286
287             return WIFI_CONN_ERROR;
288         }
289
290         times ++;
291     }
292
293     gWiFiConnFlag = false;
294
295     return WIFI_NO_ERROR;
296 }
297