Merge "Merge branch 'master' into notification-service" into notification-service
[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.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
45 char* PrintWifiErr(wifi_error_e errType)
46 {
47     switch (errType) {
48         case WIFI_ERROR_NONE:
49             return "NONE";
50         case WIFI_ERROR_INVALID_PARAMETER:
51             return "INVALID_PARAMETER";
52         case WIFI_ERROR_OUT_OF_MEMORY:
53             return "OUT_OF_MEMORY";
54         case WIFI_ERROR_INVALID_OPERATION:
55             return "INVALID_OPERATION";
56         case WIFI_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
57             return "ADDRESS_FAMILY_NOT_SUPPORTED";
58         case WIFI_ERROR_OPERATION_FAILED:
59             return "OPERATION_FAILED";
60         case WIFI_ERROR_NO_CONNECTION:
61             return "NO_CONNECTION";
62         case WIFI_ERROR_NOW_IN_PROGRESS:
63             return "NOW_IN_PROGRESS";
64         case WIFI_ERROR_ALREADY_EXISTS:
65             return "ALREADY_EXISTS";
66         case WIFI_ERROR_OPERATION_ABORTED:
67             return "OPERATION_ABORTED";
68         case WIFI_ERROR_DHCP_FAILED:
69             return "DHCP_FAILED";
70         case WIFI_ERROR_INVALID_KEY:
71             return "INVALID_KEY";
72         case WIFI_ERROR_NO_REPLY:
73             return "NO_REPLY";
74         case WIFI_ERROR_SECURITY_RESTRICTED:
75             return "SECURITY_RESTRICTED";
76         case WIFI_ERROR_PERMISSION_DENIED:
77             return "PERMISSION_DENIED";
78         case WIFI_ERROR_NOT_SUPPORTED:
79             return "NOT_SUPPORTED";
80     }
81
82     return "UNKNOWN";
83 }
84
85 static void WiFiScanCallback(wifi_error_e err, void* data)
86 {
87     if(data != NULL) {
88         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "UserData : %s", (char *) data);
89     }
90
91     OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Scan Callback Res : %s", PrintWifiErr(err));
92 }
93
94 static void WiFiActivateCallback(wifi_error_e result, void* user_data)
95 {
96     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "__test_activated_callback IN");
97
98     int ret = wifi_scan(WiFiScanCallback, NULL);
99     if (ret != WIFI_ERROR_NONE) {
100         OIC_LOG(ERROR, ES_WIFICONN_TAG, "WiFi Scan Error");
101         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Scan Fail Status : %s", PrintWifiErr((wifi_error_e) ret));
102     }
103
104     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "__test_activated_callback OUT");
105
106 }
107
108 static void ConnectedCallback(wifi_error_e err, void* data)
109 {
110     if(err == WIFI_ERROR_NONE) {
111         OIC_LOG(DEBUG, ES_WIFICONN_TAG, "Success to Connect AP");        
112     }
113     else {
114         OIC_LOG(ERROR, ES_WIFICONN_TAG, "Fail to Connect AP");        
115     }
116 }
117
118 static bool WiFiFoundCallback(wifi_ap_h ap, void *data)
119 {
120     TargetWifiInfo* targetInfo = (TargetWifiInfo *) data;
121
122     char *foundAP = NULL;
123
124     int ret = wifi_ap_get_essid(ap, &foundAP);
125
126     if(ret != WIFI_ERROR_NONE) {
127         OIC_LOG(ERROR, ES_WIFICONN_TAG, "Fail to get AP");
128         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Fail Status : %s", PrintWifiErr((wifi_error_e) ret));
129         return false;
130     }
131
132     OIC_LOG_V(DEBUG, ES_WIFICONN_TAG, "Found AP name : %s", foundAP);
133
134     if(strstr(foundAP, targetInfo->wifiName) != NULL) {
135
136         bool needPasswd = false;
137
138         if (wifi_ap_is_passphrase_required(ap, &needPasswd) == WIFI_ERROR_NONE) {
139
140             OIC_LOG(DEBUG, ES_WIFICONN_TAG, "Passsword required");
141
142             ret = wifi_ap_set_passphrase(ap, targetInfo->wifiPasswd);
143             if (ret != WIFI_ERROR_NONE) {
144                 OIC_LOG(ERROR, ES_WIFICONN_TAG, "Fail to Set Password");
145                 OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Fail Status : %s", PrintWifiErr((wifi_error_e) ret));
146                 g_free(foundAP);
147                 return false;
148             }
149
150             ret = wifi_connect(ap, ConnectedCallback, targetInfo);
151             if (ret != WIFI_ERROR_NONE) {
152                 OIC_LOG(ERROR, ES_WIFICONN_TAG, "Fail to connect wifi");
153                 OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Connect Fail Status : %s", PrintWifiErr((wifi_error_e) ret));
154                 g_free(foundAP);
155                 return false;                
156             }
157             else {
158                 OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Success to connect wifi : %s", PrintWifiErr((wifi_error_e) ret));
159                 gWiFiConnFlag = true;
160                 g_free(foundAP);
161                 return false;                
162             }
163
164
165         }
166         else {
167             OIC_LOG(ERROR, ES_WIFICONN_TAG, "Fail to get Passphrase Required");
168             OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Fail Status : %s", PrintWifiErr((wifi_error_e) ret));
169             return false;
170         }
171
172     }
173     else {
174         OIC_LOG(DEBUG, ES_WIFICONN_TAG, "This AP is not the one wanted to be connected");
175         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Searching : %s / Searched : %s", targetInfo->wifiName, foundAP);
176     }
177
178     g_free(foundAP);
179
180     return true;
181 }
182
183 WiFiConnErrCode TizenWiFiDeinit()
184 {
185     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "TizenWiFiDeinit IN");
186
187     int ret = wifi_deinitialize();
188     if(ret != WIFI_ERROR_NONE) {
189         OIC_LOG(ERROR, ES_WIFICONN_TAG, "wifi deinit error");
190         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Fail Status : %s", PrintWifiErr((wifi_error_e) ret));
191         return WIFI_DEINIT_ERROR;
192     }
193
194     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "TizenWiFiDeinit OUT");
195     return WIFI_NO_ERROR;
196 }
197
198 WiFiConnErrCode TizenWiFiInit()
199 {
200     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "TizenWiFiInit IN");
201
202     gWiFiConnFlag = false;
203     int ret = wifi_initialize();
204     if (ret != WIFI_ERROR_NONE) {
205         OIC_LOG(ERROR, ES_WIFICONN_TAG, "WiFi Init Error");
206         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Init Fail Status : %s", PrintWifiErr((wifi_error_e) ret));
207         return WIFI_INIT_ERROR;
208     }
209
210     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "TizenWiFiInit OUT");
211
212     return WIFI_NO_ERROR;
213 }
214
215 WiFiConnErrCode TizenWiFiScanStart()
216 {
217     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "TizenWiFiScanStart IN");
218     int ret;
219
220     bool wifiState = false;
221     wifi_is_activated(&wifiState);
222
223     // if wifi is not activated, actviate & scan
224     if (wifiState == false) {
225         OIC_LOG(ERROR, ES_WIFICONN_TAG, "WiFi Not Activated...Activate it!");
226         ret = wifi_activate(WiFiActivateCallback, NULL);
227
228         if(ret != WIFI_ERROR_NONE) {
229             OIC_LOG(ERROR, ES_WIFICONN_TAG, "wifi activate error");
230             return WIFI_ACTIVATE_ERROR;
231         }
232     }
233     else{
234         ret = wifi_scan(WiFiScanCallback, NULL);
235         if (ret != WIFI_ERROR_NONE) {
236             OIC_LOG(ERROR, ES_WIFICONN_TAG, "WiFi Scan Error");
237             OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Scan Fail Status : %s", PrintWifiErr((wifi_error_e) ret));
238             return WIFI_SCAN_ERROR;
239         }
240     }
241
242     OIC_LOG(DEBUG, ES_WIFICONN_TAG, "TizenWiFiScanStart OUT");
243
244     return WIFI_NO_ERROR;
245 }
246
247
248 WiFiConnErrCode TizenWiFiConn(char *ssid, char* passwd)
249 {
250     TargetWifiInfo targetInfo;
251
252     memset(targetInfo.wifiName, 0, OIC_STRING_MAX_VALUE);
253     memset(targetInfo.wifiPasswd, 0, OIC_STRING_MAX_VALUE);
254     if(ssid != NULL)
255     {
256         strncpy(targetInfo.wifiName, ssid, strlen(ssid));
257     }
258
259     if(passwd != NULL)
260     {
261         strncpy(targetInfo.wifiPasswd, passwd, strlen(passwd));
262     }
263
264     int ret = wifi_foreach_found_aps(WiFiFoundCallback, &targetInfo);
265     if(ret != WIFI_ERROR_NONE) {
266         OIC_LOG(ERROR, ES_WIFICONN_TAG, "wifi_foreach_found_aps Error");
267         OIC_LOG_V(INFO, ES_WIFICONN_TAG, "Fail Status : %s", PrintWifiErr((wifi_error_e) ret));
268         memset(targetInfo.wifiName, 0, OIC_STRING_MAX_VALUE);
269         memset(targetInfo.wifiPasswd, 0, OIC_STRING_MAX_VALUE);
270
271         return WIFI_CONN_ERROR;
272     }
273
274     // try to find target AP during 10[sec]
275     int times = 1;
276     while(!gWiFiConnFlag) {
277         sleep(1);
278
279         if(times >= 10){
280             OIC_LOG(ERROR, ES_WIFICONN_TAG, "Connection Error... try 10[sec]");
281             memset(targetInfo.wifiName, 0, OIC_STRING_MAX_VALUE);
282             memset(targetInfo.wifiPasswd, 0, OIC_STRING_MAX_VALUE);
283
284             return WIFI_CONN_ERROR;
285         }
286
287         times ++;
288     }
289
290     gWiFiConnFlag = false;
291
292     return WIFI_NO_ERROR;
293 }
294