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