2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
23 #include <net_connection.h>
25 #ifdef SUPPORT_WIFI_DIRECT
26 #include <wifi-direct.h>
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;
34 #ifdef SUPPORT_COMPANION_MODE
35 // support B3 companion mode
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"
43 static int __dp_network_is_companion_mode()
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))
58 #ifdef SUPPORT_WIFI_DIRECT
60 static int __dp_network_wifi_direct_status()
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");
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)
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();
82 TRACE_INFO("WIFI_DIRECT_DISCONNECTION");
83 g_network_is_wifi_direct = 0;
85 pthread_mutex_unlock(&g_dp_network_mutex);
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)
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;
114 TRACE_INFO("DISCONNECTED");
115 network_type = DP_NETWORK_OFF;
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)
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;
141 TRACE_INFO("DISCONNECTED");
142 g_network_status = DP_NETWORK_OFF;
144 pthread_mutex_unlock(&g_dp_network_mutex);
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)
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();
159 int dp_network_get_status()
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);
168 int dp_network_is_wifi_direct()
170 pthread_mutex_lock(&g_dp_network_mutex);
171 int status = g_network_is_wifi_direct;
172 pthread_mutex_unlock(&g_dp_network_mutex);
176 //////////////////////////////////////////////////////////////////////////
177 /// @brief create connection handle & regist callback
178 /// @return 0 : success -1 : failed
179 int dp_network_connection_init()
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();
191 if ((retcode = connection_create(&g_network_connection)) !=
192 CONNECTION_ERROR_NONE) {
193 TRACE_ERROR("Failed connection_create [%d]", retcode);
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;
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;
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;
222 pthread_mutex_lock(&g_dp_network_mutex);
224 __dp_get_network_connection_status(g_network_connection, type);
225 pthread_mutex_unlock(&g_dp_network_mutex);
229 //////////////////////////////////////////////////////////////////////////
230 /// @brief destroy connection handle
231 void dp_network_connection_destroy()
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();
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);
244 g_network_connection = 0;
245 pthread_mutex_unlock(&g_dp_network_mutex);