Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / easy-setup / enrollee / tizen / wifi / networkhandler.c
1 /******************************************************************
2  *
3  * Copyright 2015 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 #include "networkhandler.h"
22
23 #include <wifi.h>
24 #include <unistd.h>
25
26 #include "logger.h"
27 #include "easysetup.h"
28 #include "oic_string.h"
29
30 #define LOG_TAG "TIZEN ES"
31
32 const char *gSsid = "DLNA_LISMORE1";
33 const char *gPass = "dlna@010203";
34 char *gIpAddress = NULL;
35 wifi_ap_h connectedWifi;
36 ESEnrolleeNetworkEventCallback gNetworkEventCb;
37 static void ESActivateWifi();
38
39 static const char*
40 print_state(wifi_connection_state_e state)
41 {
42     switch (state)
43     {
44         case WIFI_CONNECTION_STATE_DISCONNECTED:
45             return "Disconnected";
46         case WIFI_CONNECTION_STATE_ASSOCIATION:
47             return "Association";
48         case WIFI_CONNECTION_STATE_CONNECTED:
49             return "Connected";
50         case WIFI_CONNECTION_STATE_CONFIGURATION:
51             return "Configuration";
52     }
53 }
54
55 void __wifi_connected_cb(wifi_error_e error_code, void *user_data)
56 {
57     OIC_LOG(INFO,LOG_TAG,"#### __connected ");
58
59     if (gIpAddress)
60         free(gIpAddress);
61
62     wifi_ap_get_ip_address(connectedWifi, WIFI_ADDRESS_FAMILY_IPV4, &gIpAddress);
63     OIC_LOG_V(INFO,LOG_TAG,"#### __connected, Ipaddress=%s", gIpAddress);
64     gNetworkEventCb(ES_OK);
65
66 }
67
68 bool __wifi_found_ap_cb(wifi_ap_h ap, void *user_data)
69 {
70     OIC_LOG(INFO,LOG_TAG,"#### __wifi_found_ap_cb received ");
71
72     int error_code = 0;
73     char *ap_name = NULL;
74     wifi_connection_state_e state;
75
76     error_code = wifi_ap_get_essid(ap, &ap_name);
77     if (error_code != WIFI_ERROR_NONE)
78     {
79         OIC_LOG(ERROR,LOG_TAG,"#### Fail to get AP name.");
80
81         return false;
82     }
83     error_code = wifi_ap_get_connection_state(ap, &state);
84     if (error_code != WIFI_ERROR_NONE)
85     {
86         OIC_LOG(ERROR,LOG_TAG,"#### Fail to get state.");
87         free(ap_name);
88
89         return false;
90     }
91     OIC_LOG_V(INFO,LOG_TAG,"#### AP name : %s, state : %s", ap_name, print_state(state));
92
93     if (strcmp(ap_name, gSsid) == 0)
94     {
95         OIC_LOG(INFO,LOG_TAG,"#### network found");
96         wifi_ap_set_passphrase(ap, gPass);
97         connectedWifi = ap;
98         error_code = wifi_connect(ap, __wifi_connected_cb, NULL);
99         OIC_LOG_V(INFO,LOG_TAG,"Code=%d", error_code);
100     }
101     OIC_LOG(INFO,LOG_TAG,"#### __wifi_found_ap_cb received ");
102     free(ap_name);
103     return true;
104 }
105 void __scan_request_cb(wifi_error_e error_code, void *user_data)
106 {
107     OIC_LOG(INFO, LOG_TAG, "__scan_request_cb");
108     int error_code1;
109     error_code1 = wifi_foreach_found_aps(__wifi_found_ap_cb, NULL);
110     if (error_code1 != WIFI_ERROR_NONE)
111         OIC_LOG(INFO,LOG_TAG,"#### Fail to scan");
112
113     OIC_LOG(INFO, LOG_TAG,"#### __scan_request_cb exit ");
114 }
115
116 static void __wifi_activated_cb(wifi_error_e result, void *user_data)
117 {
118     OIC_LOG(INFO, LOG_TAG, "__wifi_activated_cb");
119     if (result == WIFI_ERROR_NONE)
120     {
121         OIC_LOG(INFO,LOG_TAG,"#### Success to activate Wi-Fi device!");
122     }
123     wifi_scan(__scan_request_cb, NULL);
124
125 }
126 static void ESActivateWifi()
127 {
128     int error_code;
129     error_code = wifi_initialize();
130     OIC_LOG_V(INFO,LOG_TAG,"#### WIFI INITIALIZED WITH STATUS :%d", error_code);
131
132     error_code = wifi_activate(__wifi_activated_cb, NULL);
133     OIC_LOG_V(INFO,LOG_TAG,"#### WIFI ACTIVATED WITH STATUS :%d", error_code);
134
135     bool wifi_activated = false;
136     wifi_is_activated(&wifi_activated);
137     if (wifi_activated)
138     {
139         OIC_LOG(INFO,LOG_TAG,"#### Success to get Wi-Fi device state.");
140         int scan_result = wifi_scan(__scan_request_cb, NULL);
141         OIC_LOG_V(INFO,LOG_TAG,"#### Wifi scan result:%d", scan_result);
142     }
143     else
144     {
145         OIC_LOG(ERROR,LOG_TAG, "#### Fail to get Wi-Fi device state.");
146     }
147 }
148
149 static void start()
150 {
151     OIC_LOG(INFO, LOG_TAG, "START");
152     ESActivateWifi();
153 }
154
155 void ConnectToWiFiNetwork(const char *ssid, const char *pass,
156                                                             ESEnrolleeNetworkEventCallback cb)
157 {
158     OIC_LOG_V(INFO, LOG_TAG, "ConnectToWiFiNetwork %s %s",ssid,pass);
159     gPass = pass;
160     gSsid = ssid;
161     gNetworkEventCb = cb;
162     start();
163 }
164
165 ESResult getCurrentNetworkInfo(OCConnectivityType targetType, NetworkInfo *info)
166 {
167     if (targetType == CT_ADAPTER_IP)
168     {
169         info->type = CT_ADAPTER_IP;
170         info->ipaddr = gIpAddress;
171         if (strlen(gSsid) <= MAXSSIDLEN)
172         {
173             OICStrcpy(info->ssid, sizeof(info->ssid), gSsid);
174             return ES_OK;
175         }
176         else
177         {
178             return ES_ERROR;
179         }
180     }
181
182     return ES_ERROR;
183 }
184