2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 * @file download-manager-network.cpp
18 * @author Jungki Kwak (jungki.kwak@samsung.com)
19 * @brief Download netowkr manager
23 #include "download-manager-common.h"
24 #include "download-manager-network.h"
41 void NetMgr::initNetwork()
43 if (connection_create(&m_handle) < 0) {
44 DP_LOGE("Fail to create network handle");
48 if (connection_set_type_changed_cb(m_handle, netTypeChangedCB, NULL)
50 DP_LOGE("Fail to register network state changed cb");
53 if (connection_set_ip_address_changed_cb(m_handle, netConfigChangedCB, NULL)
55 DP_LOGE("Fail to register ip address changed cb");
60 void NetMgr::deinitNetwork()
62 if (connection_unset_type_changed_cb(m_handle) < 0) {
63 DP_LOGE("Fail to unregister network state changed cb");
65 if (connection_unset_ip_address_changed_cb(m_handle) < 0) {
66 DP_LOGE("Fail to unregister ip address changed cb");
68 if (connection_destroy(m_handle) < 0) {
69 DP_LOGE("Fail to destroy network handle");
73 int NetMgr::getConnectionState()
75 connection_type_e type = CONNECTION_TYPE_DISCONNECTED;
79 DP_LOGE("handle is NULL");
82 if (connection_get_type(m_handle, &type) < 0) {
83 DP_LOGE(" Fail to get network status");
88 case CONNECTION_TYPE_DISCONNECTED:
89 DP_LOG("CONNECTION_NETWORK_STATE_DISCONNECTED");
92 case CONNECTION_TYPE_WIFI:
93 DP_LOG("CONNECTION_NETWORK_STATE_WIFI");
94 ret = getCellularStatus();
96 case CONNECTION_TYPE_CELLULAR:
97 DP_LOG("CONNECTION_NETWORK_STATE_CELLULAR");
98 ret = getWifiStatus();
101 DP_LOGE("Cannot enter here");
108 int NetMgr::getCellularStatus()
110 connection_cellular_state_e status = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
114 DP_LOGE("handle is NULL");
118 if (connection_get_cellular_state(m_handle, &status) < 0) {
119 DP_LOGE(" Fail to get cellular status");
124 case CONNECTION_CELLULAR_STATE_AVAILABLE:
125 DP_LOG("CONNECTION_CELLULAR_STATE_AVAILABLE");
126 ret = NET_CELLULAR_ACTIVE;
128 case CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE:
129 DP_LOG("CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE");
132 case CONNECTION_CELLULAR_STATE_FLIGHT_MODE:
133 DP_LOG("CONNECTION_CELLULAR_STATE_FLIGHT_MODE");
136 case CONNECTION_CELLULAR_STATE_ROAMING_OFF:
137 DP_LOG("CONNECTION_CELLULAR_STATE_ROAMING_OFF");
140 case CONNECTION_CELLULAR_STATE_CALL_ONLY_AVAILABLE:
141 DP_LOG("CONNECTION_CELLULAR_STATE_CALL_ONLY_AVAILABLE");
145 DP_LOGE("Cannot enter here");
153 int NetMgr::getWifiStatus()
155 connection_wifi_state_e status = CONNECTION_WIFI_STATE_DEACTIVATED;
159 DP_LOGE("handle is NULL");
163 if (connection_get_wifi_state(m_handle, &status) < 0) {
164 DP_LOGE(" Fail to get wifi status");
169 case CONNECTION_WIFI_STATE_CONNECTED:
170 DP_LOG("CONNECTION_WIFI_STATE_CONNECTED");
171 ret = NET_WIFI_ACTIVE;
173 case CONNECTION_WIFI_STATE_DISCONNECTED:
174 DP_LOG("CONNECTION_WIFI_STATE_DISCONNECTED");
177 case CONNECTION_WIFI_STATE_DEACTIVATED:
178 DP_LOG("CONNECTION_WIFI_STATE_DEACTIVATED");
182 DP_LOGE("Cannot enter here");
189 void NetMgr::netTypeChanged()
191 int changedStatus = NET_INACTIVE;
192 changedStatus = getConnectionState();
193 DP_LOG("Previous[%d] Changed[%d]", m_netStatus, changedStatus);
194 if (m_netStatus != changedStatus) {
195 if (changedStatus == NET_INACTIVE)
196 DP_LOG("Netowrk is disconnected");
198 DP_LOG("Network is connected");
199 m_netStatus = changedStatus;
201 DP_LOG("Network berer type is changed. ex.3G->WIFI");
205 /* This routine should be operated in case of downloading state.
206 * After the download is finished, network event handler should be removed.
208 void NetMgr::netConfigChanged(string ipAddr)
213 if (ipAddr.length() > 1) {/* network is connected */
216 /* This notify is only for suspend event.
217 * If othere network event is added, it is needed to save event types
218 * and get function for it
222 DP_LOGE("Network connection is disconnected");
226 void NetMgr::getProxy()
229 connection_address_family_e family = CONNECTION_ADDRESS_FAMILY_IPV4;
232 DP_LOGE("handle is NULL");
235 if (connection_get_proxy(m_handle, family, &proxy) < 0) {
236 DP_LOGE("Fail to get ip address");
240 DP_LOG("===== Proxy address[%s] =====", proxy);
246 void NetMgr::getIPAddress()
249 connection_address_family_e family = CONNECTION_ADDRESS_FAMILY_IPV4;
251 DP_LOGE("handle is NULL");
254 if (connection_get_ip_address(m_handle, family, &ipAddr) < 0) {
255 DP_LOGE("Fail to get ip address");
259 DP_LOG("===== IP address[%s] =====", ipAddr);
265 void NetMgr::netTypeChangedCB(connection_type_e state, void *data)
267 NetMgr inst = NetMgr::getInstance();
268 inst.netTypeChanged();
271 void NetMgr::netConfigChangedCB(const char *ip, const char *ipv6,
275 NetMgr inst = NetMgr::getInstance();
276 inst.netConfigChanged(ipAddr);