Use the same type to compare values
[platform/framework/web/download-provider.git] / provider / download-provider-network.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "download-provider.h"
18 #include "download-provider-log.h"
19 #include "download-provider-pthread.h"
20 #include "download-provider-network.h"
21 #include "download-provider-client-manager.h"
22
23 #include <net_connection.h>
24
25 #ifdef SUPPORT_WIFI_DIRECT
26 #include <wifi-direct.h>
27 #endif
28
29 static pthread_mutex_t g_dp_network_mutex = PTHREAD_MUTEX_INITIALIZER;
30
31 static int g_network_status = DP_NETWORK_OFF;
32 static connection_h g_network_connection = 0;
33 static int g_network_is_wifi_direct = 0;
34
35 #ifdef SUPPORT_COMPANION_MODE
36 // support B3 companion mode
37 #include "vconf.h"
38 #include "vconf-keys.h"
39 #include "SAPInterface.h"
40 #define VCONFKEY_SAP_CONNECTION_NOTIFICATION VCONFKEY_WMS_WMANAGER_CONNECTED
41 #define VCONFKEY_SAP_CONNECTION_TYPE "memory/private/sap/conn_type"
42 #endif
43
44 static int __dp_network_is_companion_mode()
45 {
46 #ifdef SUPPORT_COMPANION_MODE
47         int wms_connected = 0;
48         int companion_mode = 0;
49         vconf_get_int(VCONFKEY_SAP_CONNECTION_NOTIFICATION, &wms_connected);
50         vconf_get_int(VCONFKEY_SAP_CONNECTION_TYPE, &companion_mode);
51         TRACE_INFO("wms_connected:%d, companion_mode:%d", wms_connected, companion_mode);
52         if (wms_connected == 1 && (companion_mode & SAP_BT))
53                 return 1;
54
55 #endif
56         return 0;
57 }
58
59 #ifdef SUPPORT_WIFI_DIRECT
60
61 static int __dp_network_wifi_direct_status()
62 {
63         int is_connected = 0;
64         wifi_direct_state_e wifi_state = WIFI_DIRECT_STATE_DEACTIVATED;
65         if (wifi_direct_get_state(&wifi_state) == 0) {
66                 if (wifi_state == WIFI_DIRECT_STATE_CONNECTED) {
67                         TRACE_INFO("WIFI_DIRECT_STATE_CONNECTED");
68                         is_connected = 1;
69                 }
70         }
71         return is_connected;
72 }
73
74 // support WIFI-Direct
75 static void __dp_network_wifi_direct_changed_cb(wifi_direct_error_e error_code,
76                 wifi_direct_connection_state_e connection_state, const char *mac_address, void *data)
77 {
78         pthread_mutex_lock(&g_dp_network_mutex);
79         if (connection_state == WIFI_DIRECT_CONNECTION_RSP) {
80                 TRACE_INFO("WIFI_DIRECT_CONNECTION_RSP");
81                 g_network_is_wifi_direct = __dp_network_wifi_direct_status();
82         } else {
83                 TRACE_INFO("WIFI_DIRECT_DISCONNECTION");
84                 g_network_is_wifi_direct = 0;
85         }
86         pthread_mutex_unlock(&g_dp_network_mutex);
87 }
88 #endif
89
90 //////////////////////////////////////////////////////////////////////////
91 /// @brief              check the status in more detail by connection type
92 /// @return     dp_network_type
93 static int __dp_get_network_connection_status(connection_h connection, connection_type_e type)
94 {
95         int network_type = DP_NETWORK_OFF;
96         if (__dp_network_is_companion_mode() == 1) {
97                 network_type = DP_NETWORK_ALL;
98                 TRACE_INFO("COMPANION MODE");
99         } else if (type == CONNECTION_TYPE_WIFI) {
100                 TRACE_INFO("WIFI CONNECTED");
101                 network_type = DP_NETWORK_WIFI;
102         } else if (type == CONNECTION_TYPE_CELLULAR) {
103                 TRACE_INFO("DATA NETWORK CONNECTED");
104                 network_type = DP_NETWORK_DATA_NETWORK;
105         } else if (type == CONNECTION_TYPE_ETHERNET) {
106                 TRACE_INFO("ETHERNET CONNECTED");
107                 network_type = DP_NETWORK_WIFI;
108         } else if (type == CONNECTION_TYPE_BT) {
109                 TRACE_INFO("BT CONNECTED");
110                 network_type = DP_NETWORK_WIFI;
111         } else if (type == CONNECTION_TYPE_NET_PROXY) {
112                 TRACE_INFO("NET PROXY CONNECTED");
113                 network_type = DP_NETWORK_WIFI;
114         } else {
115                 TRACE_INFO("DISCONNECTED");
116                 network_type = DP_NETWORK_OFF;
117         }
118
119         return network_type;
120 }
121
122 //////////////////////////////////////////////////////////////////////////
123 /// @brief              [callback] called whenever changed network status
124 /// @todo               care requests by network status
125 static void __dp_network_connection_type_changed_cb(connection_type_e type, void *data)
126 {
127         pthread_mutex_lock(&g_dp_network_mutex);
128         // this callback guarantee that already connected
129         if (__dp_network_is_companion_mode() == 1) {
130                 TRACE_INFO("COMPANION MODE");
131                 g_network_status = DP_NETWORK_ALL;
132         } else if (type == CONNECTION_TYPE_WIFI) {
133                 TRACE_INFO("WIFI CONNECTED");
134                 g_network_status = DP_NETWORK_WIFI;
135         } else if (type == CONNECTION_TYPE_CELLULAR) {
136                 TRACE_INFO("DATA NETWORK CONNECTED");
137                 g_network_status = DP_NETWORK_DATA_NETWORK;
138         } else if (type == CONNECTION_TYPE_ETHERNET) {
139                 TRACE_INFO("ETHERNET CONNECTED");
140                 g_network_status = DP_NETWORK_WIFI;
141         } else {
142                 TRACE_INFO("DISCONNECTED");
143                 g_network_status = DP_NETWORK_OFF;
144         }
145         pthread_mutex_unlock(&g_dp_network_mutex);
146 }
147
148 //////////////////////////////////////////////////////////////////////////
149 /// @brief              [callback] called when changed network ip
150 /// @todo               auto resume feature
151 static void __dp_network_connection_ip_changed_cb(const char *ip, const char *ipv6, void *data)
152 {
153         if (dp_network_get_status() != DP_NETWORK_OFF) {
154                 TRACE_DEBUG("[CONNECTION] IP CHANGED");
155                 // broadcast to all thread for clients
156                 dp_broadcast_signal();
157         }
158 }
159
160 int dp_network_get_status()
161 {
162         int status = DP_NETWORK_OFF;
163         pthread_mutex_lock(&g_dp_network_mutex);
164         status = g_network_status;
165         pthread_mutex_unlock(&g_dp_network_mutex);
166         return status;
167 }
168
169 int dp_network_is_wifi_direct()
170 {
171         pthread_mutex_lock(&g_dp_network_mutex);
172         int status = g_network_is_wifi_direct;
173         pthread_mutex_unlock(&g_dp_network_mutex);
174         return status;
175 }
176
177 //////////////////////////////////////////////////////////////////////////
178 /// @brief              create connection handle & regist callback
179 /// @return     0 : success -1 : failed
180 int dp_network_connection_init()
181 {
182         int retcode = 0;
183
184 #ifdef SUPPORT_WIFI_DIRECT
185         if (wifi_direct_initialize() == 0) {
186                 wifi_direct_set_connection_state_changed_cb
187                         (__dp_network_wifi_direct_changed_cb, NULL);
188                 g_network_is_wifi_direct = __dp_network_wifi_direct_status();
189         }
190 #endif
191
192         if ((retcode = connection_create(&g_network_connection)) !=
193                         CONNECTION_ERROR_NONE) {
194                 TRACE_ERROR("Failed connection_create [%d]", retcode);
195                 return -1;
196         }
197         if ((retcode = connection_set_type_changed_cb(g_network_connection,
198                                         __dp_network_connection_type_changed_cb, NULL)) !=
199                         CONNECTION_ERROR_NONE) {
200                 TRACE_ERROR("Failed connection_set_type_changed_cb [%d]", retcode);
201                 connection_destroy(g_network_connection);
202                 g_network_connection = 0;
203                 return -1;
204         }
205
206         if ((retcode = connection_set_ip_address_changed_cb
207                                 (g_network_connection, __dp_network_connection_ip_changed_cb,
208                                  NULL)) != CONNECTION_ERROR_NONE) {
209                 TRACE_ERROR("Failed __dp_network_connection_ip_changed_cb [%d]", retcode);
210                 connection_destroy(g_network_connection);
211                 g_network_connection = 0;
212                 return -1;
213         }
214
215         connection_type_e type = CONNECTION_TYPE_DISCONNECTED;
216         if ((retcode = connection_get_type(g_network_connection, &type)) !=
217                         CONNECTION_ERROR_NONE) {
218                 TRACE_ERROR("Failed connection_get_type [%d]", retcode);
219                 connection_destroy(g_network_connection);
220                 g_network_connection = 0;
221                 return -1;
222         }
223         pthread_mutex_lock(&g_dp_network_mutex);
224         g_network_status =
225                 __dp_get_network_connection_status(g_network_connection, type);
226         pthread_mutex_unlock(&g_dp_network_mutex);
227         return 0;
228 }
229
230 //////////////////////////////////////////////////////////////////////////
231 /// @brief              destroy connection handle
232 void dp_network_connection_destroy()
233 {
234         pthread_mutex_lock(&g_dp_network_mutex);
235 #ifdef SUPPORT_WIFI_DIRECT
236         wifi_direct_unset_connection_state_changed_cb();
237         wifi_direct_deinitialize();
238 #endif
239
240         if (g_network_connection != 0) {
241                 connection_unset_type_changed_cb(g_network_connection);
242                 connection_unset_ip_address_changed_cb(g_network_connection);
243                 connection_destroy(g_network_connection);
244         }
245         g_network_connection = 0;
246         pthread_mutex_unlock(&g_dp_network_mutex);
247 }