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-log.h"
18 #include "download-provider-config.h"
19 #include "download-provider-pthread.h"
20 #include "download-provider-network.h"
22 extern pthread_mutex_t g_dp_queue_mutex;
23 extern pthread_cond_t g_dp_queue_cond;
28 CONNECTION_TYPE_DISCONNECTED = 0, /**< Disconnected */
29 CONNECTION_TYPE_WIFI = 1, /**< Wi-Fi type */
30 CONNECTION_TYPE_CELLULAR = 2, /**< Cellular type */
31 CONNECTION_TYPE_ETHERNET = 3, /**< Ethernet type */
32 CONNECTION_TYPE_BT = 4, /**< Bluetooth type */
36 CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE = 0, /**< Out of service */
37 CONNECTION_CELLULAR_STATE_FLIGHT_MODE = 1, /**< Flight mode */
38 CONNECTION_CELLULAR_STATE_ROAMING_OFF = 2, /**< Roaming is turned off */
39 CONNECTION_CELLULAR_STATE_CALL_ONLY_AVAILABLE = 3, /**< Call is only available */
40 CONNECTION_CELLULAR_STATE_AVAILABLE = 4, /**< Available but not connected yet */
41 CONNECTION_CELLULAR_STATE_CONNECTED = 5, /**< Connected */
42 } connection_cellular_state_e
45 CONNECTION_WIFI_STATE_DEACTIVATED = 0, /**< Deactivated state */
46 CONNECTION_WIFI_STATE_DISCONNECTED = 1, /**< disconnected state */
47 CONNECTION_WIFI_STATE_CONNECTED = 2, /**< Connected state */
48 } connection_wifi_state_e;
51 CONNECTION_ETHERNET_STATE_DEACTIVATED = 0, /**< Deactivated state */
52 CONNECTION_ETHERNET_STATE_DISCONNECTED = 1, /**< disconnected state */
53 CONNECTION_ETHERNET_STATE_CONNECTED = 2, /**< Connected state */
54 } connection_ethernet_state_e;
57 CONNECTION_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */
58 CONNECTION_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
59 CONNECTION_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory error */
60 CONNECTION_ERROR_INVALID_OPERATION = TIZEN_ERROR_INVALID_OPERATION, /**< Invalid Operation */
61 CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED = TIZEN_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED, /**< Address family not supported */
62 CONNECTION_ERROR_OPERATION_FAILED = TIZEN_ERROR_NETWORK_CLASS|0x0401, /**< Operation failed */
63 CONNECTION_ERROR_ITERATOR_END = TIZEN_ERROR_NETWORK_CLASS|0x0402, /**< End of iteration */
64 CONNECTION_ERROR_NO_CONNECTION = TIZEN_ERROR_NETWORK_CLASS|0x0403, /**< There is no connection */
65 CONNECTION_ERROR_NOW_IN_PROGRESS = TIZEN_ERROR_NOW_IN_PROGRESS, /** Now in progress */
66 CONNECTION_ERROR_ALREADY_EXISTS = TIZEN_ERROR_NETWORK_CLASS|0x0404, /**< Already exists */
67 CONNECTION_ERROR_OPERATION_ABORTED = TIZEN_ERROR_NETWORK_CLASS|0x0405, /**< Operation is aborted */
68 CONNECTION_ERROR_DHCP_FAILED = TIZEN_ERROR_NETWORK_CLASS|0x0406, /**< DHCP failed */
69 CONNECTION_ERROR_INVALID_KEY = TIZEN_ERROR_NETWORK_CLASS|0x0407, /**< Invalid key */
70 CONNECTION_ERROR_NO_REPLY = TIZEN_ERROR_NETWORK_CLASS|0x0408, /**< No reply */
74 static void __print_connection_errorcode_to_string(connection_error_e errorcode)
78 case CONNECTION_ERROR_INVALID_PARAMETER :
79 TRACE_INFO("CONNECTION_ERROR_INVALID_PARAMETER");
81 case CONNECTION_ERROR_OUT_OF_MEMORY :
82 TRACE_INFO("CONNECTION_ERROR_OUT_OF_MEMORY");
84 case CONNECTION_ERROR_INVALID_OPERATION :
85 TRACE_INFO("CONNECTION_ERROR_INVALID_OPERATION");
87 case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED :
88 TRACE_INFO("CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED");
90 case CONNECTION_ERROR_OPERATION_FAILED :
91 TRACE_INFO("CONNECTION_ERROR_OPERATION_FAILED");
93 case CONNECTION_ERROR_ITERATOR_END :
94 TRACE_INFO("CONNECTION_ERROR_ITERATOR_END");
96 case CONNECTION_ERROR_NO_CONNECTION :
97 TRACE_INFO("CONNECTION_ERROR_NO_CONNECTION");
99 case CONNECTION_ERROR_NOW_IN_PROGRESS :
100 TRACE_INFO("CONNECTION_ERROR_NOW_IN_PROGRESS");
102 case CONNECTION_ERROR_ALREADY_EXISTS :
103 TRACE_INFO("CONNECTION_ERROR_ALREADY_EXISTS");
105 case CONNECTION_ERROR_OPERATION_ABORTED :
106 TRACE_INFO("CONNECTION_ERROR_OPERATION_ABORTED");
108 case CONNECTION_ERROR_DHCP_FAILED :
109 TRACE_INFO("CONNECTION_ERROR_DHCP_FAILED");
111 case CONNECTION_ERROR_INVALID_KEY :
112 TRACE_INFO("CONNECTION_ERROR_INVALID_KEY");
114 case CONNECTION_ERROR_NO_REPLY :
115 TRACE_INFO("CONNECTION_ERROR_NO_REPLY");
118 TRACE_INFO("CONNECTION_ERROR_NONE");
130 //////////////////////////////////////////////////////////////////////////
131 /// @brief check the status in more detail by connection type
132 /// @return dp_network_type
133 static dp_network_type __dp_get_network_connection_status(connection_h connection, connection_type_e type)
135 dp_network_type network_type = DP_NETWORK_TYPE_OFF;
136 if (type == CONNECTION_TYPE_WIFI) {
137 connection_wifi_state_e wifi_state;
138 wifi_state = CONNECTION_WIFI_STATE_DEACTIVATED;
139 if (connection_get_wifi_state
140 (connection, &wifi_state) != CONNECTION_ERROR_NONE)
141 TRACE_ERROR("Failed connection_get_wifi_state");
142 if (wifi_state == CONNECTION_WIFI_STATE_CONNECTED) {
143 TRACE_INFO("[CONNECTION_WIFI] CONNECTED");
144 network_type = DP_NETWORK_TYPE_WIFI;
146 TRACE_INFO("[CONNECTION_WIFI] [%d]", wifi_state);
148 } else if (type == CONNECTION_TYPE_CELLULAR) {
149 connection_cellular_state_e cellular_state;
150 cellular_state = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
151 if (connection_get_cellular_state
152 (connection, &cellular_state) != CONNECTION_ERROR_NONE)
153 TRACE_ERROR("Failed connection_get_cellular_state");
154 if (cellular_state == CONNECTION_CELLULAR_STATE_CONNECTED) {
155 TRACE_INFO("[CONNECTION_CELLULAR] DATA NETWORK CONNECTED");
156 network_type = DP_NETWORK_TYPE_DATA_NETWORK;
158 TRACE_INFO("[CONNECTION_CELLULAR] [%d]", cellular_state);
160 } else if (type == CONNECTION_TYPE_ETHERNET) {
161 connection_ethernet_state_e ethernet_state;
162 ethernet_state = CONNECTION_ETHERNET_STATE_DISCONNECTED;
163 if (connection_get_ethernet_state
164 (connection, ðernet_state) != CONNECTION_ERROR_NONE)
165 TRACE_ERROR("Failed connection_get_ethernet_state");
166 if (ethernet_state == CONNECTION_ETHERNET_STATE_CONNECTED) {
167 TRACE_INFO("[CONNECTION_ETHERNET] ETHERNET CONNECTED");
168 network_type = DP_NETWORK_TYPE_ETHERNET;
170 TRACE_INFO("[CONNECTION_ETHERNET] [%d]", ethernet_state);
173 TRACE_INFO("[DISCONNECTED]");
174 network_type = DP_NETWORK_TYPE_OFF;
179 //////////////////////////////////////////////////////////////////////////
180 /// @brief [callback] called whenever changed network status
181 /// @todo care requests by network status
182 static void __dp_network_connection_type_changed_cb(connection_type_e type, void *data)
184 TRACE_INFO("type[%d]", type);
185 dp_privates *privates = (dp_privates*)data;
187 TRACE_ERROR("[CRITICAL] Invalid data");
190 CLIENT_MUTEX_LOCK(&(g_dp_queue_mutex));
191 #if 1 // this callback guarantee that already connectdd
192 if (type == CONNECTION_TYPE_WIFI) {
193 TRACE_INFO("[CONNECTION_WIFI] CONNECTED");
194 privates->network_status = DP_NETWORK_TYPE_WIFI;
195 } else if (type == CONNECTION_TYPE_CELLULAR) {
196 TRACE_INFO("[CONNECTION_CELLULAR] DATA NETWORK CONNECTED");
197 privates->network_status = DP_NETWORK_TYPE_DATA_NETWORK;
198 } else if (type == CONNECTION_TYPE_ETHERNET) {
199 TRACE_INFO("[CONNECTION_ETHERNET] ETHERNET CONNECTED");
200 privates->network_status = DP_NETWORK_TYPE_ETHERNET;
202 TRACE_INFO("[DISCONNECTED]");
203 privates->network_status = DP_NETWORK_TYPE_OFF;
205 if (privates->network_status != DP_NETWORK_TYPE_OFF)
206 pthread_cond_signal(&g_dp_queue_cond);
208 privates->network_status =
209 __dp_get_network_connection_status(privates->connection, type);
211 CLIENT_MUTEX_UNLOCK(&(g_dp_queue_mutex));
214 //////////////////////////////////////////////////////////////////////////
215 /// @brief create connection handle & regist callback
216 /// @return 0 : success -1 : failed
217 int dp_network_connection_init(dp_privates *privates)
223 TRACE_ERROR("[CRITICAL] Invalid data");
226 if ((retcode = connection_create(&privates->connection)) !=
227 CONNECTION_ERROR_NONE) {
228 TRACE_ERROR("Failed connection_create [%d]", retcode);
231 if ((retcode = connection_set_type_changed_cb
232 (privates->connection, __dp_network_connection_type_changed_cb,
233 privates)) != CONNECTION_ERROR_NONE) {
234 TRACE_ERROR("Failed connection_set_type_changed_cb [%d]", retcode);
235 connection_destroy(privates->connection);
238 connection_type_e type = CONNECTION_TYPE_DISCONNECTED;
239 if ((retcode = connection_get_type(privates->connection, &type)) !=
240 CONNECTION_ERROR_NONE) {
241 TRACE_ERROR("Failed connection_get_type [%d]", retcode);
242 connection_destroy(privates->connection);
245 privates->network_status =
246 __dp_get_network_connection_status(privates->connection, type);
250 //////////////////////////////////////////////////////////////////////////
251 /// @brief destroy connection handle
252 void dp_network_connection_destroy(connection_h connection)
255 connection_unset_type_changed_cb (connection);
256 connection_destroy(connection);
259 //////////////////////////////////////////////////////////////////////////
260 /// @brief check network status using connection API
261 /// @todo the standard of enabled networking can be changed later
262 /// @return Network type
263 dp_network_type dp_get_network_connection_instant_status()
266 connection_h network_handle = NULL;
267 dp_network_type network_type = DP_NETWORK_TYPE_OFF;
268 if ((retcode = connection_create(&network_handle)) !=
269 CONNECTION_ERROR_NONE) {
270 TRACE_ERROR("Failed connection_create [%d]", retcode);
271 return DP_NETWORK_TYPE_OFF;
274 connection_type_e type = CONNECTION_TYPE_DISCONNECTED;
275 if ((retcode = connection_get_type(network_handle, &type)) !=
276 CONNECTION_ERROR_NONE) {
277 TRACE_ERROR("Failed connection_get_type [%d]", retcode);
278 connection_destroy(network_handle);
279 return DP_NETWORK_TYPE_OFF;
282 __dp_get_network_connection_status(network_handle, type);
284 if (connection_destroy(network_handle) != CONNECTION_ERROR_NONE)
285 TRACE_ERROR("Failed connection_destroy");