Fix merge conflict
[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 pthread_mutex_t g_dp_network_mutex = PTHREAD_MUTEX_INITIALIZER;
30 int g_network_status = DP_NETWORK_OFF;
31 connection_h g_network_connection = 0;
32 int g_network_is_wifi_direct = 0;
33
34 #ifdef SUPPORT_COMPANION_MODE
35 // support B3 companion mode
36 #include "vconf.h"
37 #include "vconf-keys.h"
38 #include "SAPInterface.h"
39 #define VCONFKEY_SAP_CONNECTION_NOTIFICATION VCONFKEY_WMS_WMANAGER_CONNECTED
40 #define VCONFKEY_SAP_CONNECTION_TYPE "memory/private/sap/conn_type"
41 #endif
42
43 static int __dp_network_is_companion_mode()
44 {
45 #ifdef SUPPORT_COMPANION_MODE
46         int wms_connected = 0;
47         int companion_mode = 0;
48         vconf_get_int(VCONFKEY_SAP_CONNECTION_NOTIFICATION, &wms_connected);
49         vconf_get_int(VCONFKEY_SAP_CONNECTION_TYPE, &companion_mode);
50         TRACE_INFO("wms_connected:%d, companion_mode:%d", wms_connected, companion_mode);
51         if (wms_connected == 1 && (companion_mode & SAP_BT))
52                 return 1;
53
54 #endif
55         return 0;
56 }
57
58 #ifdef SUPPORT_WIFI_DIRECT
59
60 static int __dp_network_wifi_direct_status()
61 {
62         int is_connected = 0;
63         wifi_direct_state_e wifi_state = WIFI_DIRECT_STATE_DEACTIVATED;
64         if (wifi_direct_get_state(&wifi_state) == 0) {
65                 if (wifi_state == WIFI_DIRECT_STATE_CONNECTED) {
66                         TRACE_INFO("WIFI_DIRECT_STATE_CONNECTED");
67                         is_connected = 1;
68                 }
69         }
70         return is_connected;
71 }
72
73 // support WIFI-Direct
74 static void __dp_network_wifi_direct_changed_cb(wifi_direct_error_e error_code,
75                 wifi_direct_connection_state_e connection_state, const char *mac_address, void *data)
76 {
77         pthread_mutex_lock(&g_dp_network_mutex);
78         if (connection_state == WIFI_DIRECT_CONNECTION_RSP) {
79                 TRACE_INFO("WIFI_DIRECT_CONNECTION_RSP");
80                 g_network_is_wifi_direct = __dp_network_wifi_direct_status();
81         } else {
82                 TRACE_INFO("WIFI_DIRECT_DISCONNECTION");
83                 g_network_is_wifi_direct = 0;
84         }
85         pthread_mutex_unlock(&g_dp_network_mutex);
86 }
87 #endif
88
89 //////////////////////////////////////////////////////////////////////////
90 /// @brief              check the status in more detail by connection type
91 /// @return     dp_network_type
92 static int __dp_get_network_connection_status(connection_h connection, connection_type_e type)
93 {
94         int network_type = DP_NETWORK_OFF;
95         if (__dp_network_is_companion_mode() == 1) {
96                 network_type = DP_NETWORK_ALL;
97                 TRACE_INFO("COMPANION MODE");
98         } else if (type == CONNECTION_TYPE_WIFI) {
99                 TRACE_INFO("WIFI CONNECTED");
100                 network_type = DP_NETWORK_WIFI;
101         } else if (type == CONNECTION_TYPE_CELLULAR) {
102                 TRACE_INFO("DATA NETWORK CONNECTED");
103                 network_type = DP_NETWORK_DATA_NETWORK;
104         } else if (type == CONNECTION_TYPE_ETHERNET) {
105                 TRACE_INFO("ETHERNET CONNECTED");
106                 network_type = DP_NETWORK_WIFI;
107         } else if (type == CONNECTION_TYPE_BT) {
108                 TRACE_INFO("BT CONNECTED");
109                 network_type = DP_NETWORK_WIFI;
110         } else if (type == CONNECTION_TYPE_NET_PROXY) {
111                 TRACE_INFO("NET PROXY CONNECTED");
112                 network_type = DP_NETWORK_WIFI;
113         } else {
114                 TRACE_INFO("DISCONNECTED");
115                 network_type = DP_NETWORK_OFF;
116         }
117
118         return network_type;
119 }
120
121 //////////////////////////////////////////////////////////////////////////
122 /// @brief              [callback] called whenever changed network status
123 /// @todo               care requests by network status
124 static void __dp_network_connection_type_changed_cb(connection_type_e type, void *data)
125 {
126         pthread_mutex_lock(&g_dp_network_mutex);
127         // this callback guarantee that already connected
128         if (__dp_network_is_companion_mode() == 1) {
129                 TRACE_INFO("COMPANION MODE");
130                 g_network_status = DP_NETWORK_ALL;
131         } else if (type == CONNECTION_TYPE_WIFI) {
132                 TRACE_INFO("WIFI CONNECTED");
133                 g_network_status = DP_NETWORK_WIFI;
134         } else if (type == CONNECTION_TYPE_CELLULAR) {
135                 TRACE_INFO("DATA NETWORK CONNECTED");
136                 g_network_status = DP_NETWORK_DATA_NETWORK;
137         } else if (type == CONNECTION_TYPE_ETHERNET) {
138                 TRACE_INFO("ETHERNET CONNECTED");
139                 g_network_status = DP_NETWORK_WIFI;
140         } else {
141                 TRACE_INFO("DISCONNECTED");
142                 g_network_status = DP_NETWORK_OFF;
143         }
144         pthread_mutex_unlock(&g_dp_network_mutex);
145 }
146
147 //////////////////////////////////////////////////////////////////////////
148 /// @brief              [callback] called when changed network ip
149 /// @todo               auto resume feature
150 static void __dp_network_connection_ip_changed_cb(const char *ip, const char *ipv6, void *data)
151 {
152         if (dp_network_get_status() != DP_NETWORK_OFF) {
153                 TRACE_DEBUG("[CONNECTION] IP CHANGED");
154                 // broadcast to all thread for clients
155                 dp_broadcast_signal();
156         }
157 }
158
159 int dp_network_get_status()
160 {
161         int status = DP_NETWORK_OFF;
162         pthread_mutex_lock(&g_dp_network_mutex);
163         status = g_network_status;
164         pthread_mutex_unlock(&g_dp_network_mutex);
165         return status;
166 }
167
168 int dp_network_is_wifi_direct()
169 {
170         pthread_mutex_lock(&g_dp_network_mutex);
171         int status = g_network_is_wifi_direct;
172         pthread_mutex_unlock(&g_dp_network_mutex);
173         return status;
174 }
175
176 //////////////////////////////////////////////////////////////////////////
177 /// @brief              create connection handle & regist callback
178 /// @return     0 : success -1 : failed
179 int dp_network_connection_init()
180 {
181         int retcode = 0;
182
183 #ifdef SUPPORT_WIFI_DIRECT
184         if (wifi_direct_initialize() == 0) {
185                 wifi_direct_set_connection_state_changed_cb
186                         (__dp_network_wifi_direct_changed_cb, NULL);
187                 g_network_is_wifi_direct = __dp_network_wifi_direct_status();
188         }
189 #endif
190
191         if ((retcode = connection_create(&g_network_connection)) !=
192                         CONNECTION_ERROR_NONE) {
193                 TRACE_ERROR("Failed connection_create [%d]", retcode);
194                 return -1;
195         }
196         if ((retcode = connection_set_type_changed_cb(g_network_connection,
197                                         __dp_network_connection_type_changed_cb, NULL)) !=
198                         CONNECTION_ERROR_NONE) {
199                 TRACE_ERROR("Failed connection_set_type_changed_cb [%d]", retcode);
200                 connection_destroy(g_network_connection);
201                 g_network_connection = 0;
202                 return -1;
203         }
204
205         if ((retcode = connection_set_ip_address_changed_cb
206                                 (g_network_connection, __dp_network_connection_ip_changed_cb,
207                                  NULL)) != CONNECTION_ERROR_NONE) {
208                 TRACE_ERROR("Failed __dp_network_connection_ip_changed_cb [%d]", retcode);
209                 connection_destroy(g_network_connection);
210                 g_network_connection = 0;
211                 return -1;
212         }
213
214         connection_type_e type = CONNECTION_TYPE_DISCONNECTED;
215         if ((retcode = connection_get_type(g_network_connection, &type)) !=
216                         CONNECTION_ERROR_NONE) {
217                 TRACE_ERROR("Failed connection_get_type [%d]", retcode);
218                 connection_destroy(g_network_connection);
219                 g_network_connection = 0;
220                 return -1;
221         }
222         pthread_mutex_lock(&g_dp_network_mutex);
223         g_network_status =
224                 __dp_get_network_connection_status(g_network_connection, type);
225         pthread_mutex_unlock(&g_dp_network_mutex);
226         return 0;
227 }
228
229 //////////////////////////////////////////////////////////////////////////
230 /// @brief              destroy connection handle
231 void dp_network_connection_destroy()
232 {
233         pthread_mutex_lock(&g_dp_network_mutex);
234 #ifdef SUPPORT_WIFI_DIRECT
235         wifi_direct_unset_connection_state_changed_cb();
236         wifi_direct_deinitialize();
237 #endif
238
239         if (g_network_connection != 0) {
240                 connection_unset_type_changed_cb(g_network_connection);
241                 connection_unset_ip_address_changed_cb(g_network_connection);
242                 connection_destroy(g_network_connection);
243         }
244         g_network_connection = 0;
245         pthread_mutex_unlock(&g_dp_network_mutex);
246 }