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 static pthread_mutex_t g_dp_network_mutex = PTHREAD_MUTEX_INITIALIZER;
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;
35 #ifdef SUPPORT_COMPANION_MODE
36 // support B3 companion mode
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"
44 static int __dp_network_is_companion_mode()
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))
59 #ifdef SUPPORT_WIFI_DIRECT
61 static int __dp_network_wifi_direct_status()
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");
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)
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();
83 TRACE_INFO("WIFI_DIRECT_DISCONNECTION");
84 g_network_is_wifi_direct = 0;
86 pthread_mutex_unlock(&g_dp_network_mutex);
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)
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;
115 TRACE_INFO("DISCONNECTED");
116 network_type = DP_NETWORK_OFF;
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)
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;
142 TRACE_INFO("DISCONNECTED");
143 g_network_status = DP_NETWORK_OFF;
145 pthread_mutex_unlock(&g_dp_network_mutex);
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)
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();
160 int dp_network_get_status()
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);
169 int dp_network_is_wifi_direct()
171 pthread_mutex_lock(&g_dp_network_mutex);
172 int status = g_network_is_wifi_direct;
173 pthread_mutex_unlock(&g_dp_network_mutex);
177 //////////////////////////////////////////////////////////////////////////
178 /// @brief create connection handle & regist callback
179 /// @return 0 : success -1 : failed
180 int dp_network_connection_init()
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();
192 if ((retcode = connection_create(&g_network_connection)) !=
193 CONNECTION_ERROR_NONE) {
194 TRACE_ERROR("Failed connection_create [%d]", retcode);
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;
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;
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;
223 pthread_mutex_lock(&g_dp_network_mutex);
225 __dp_get_network_connection_status(g_network_connection, type);
226 pthread_mutex_unlock(&g_dp_network_mutex);
230 //////////////////////////////////////////////////////////////////////////
231 /// @brief destroy connection handle
232 void dp_network_connection_destroy()
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();
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);
245 g_network_connection = 0;
246 pthread_mutex_unlock(&g_dp_network_mutex);