release the package
[apps/web/download-manager.git] / src / download-manager-network.cpp
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
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
7  *
8  *    http://www.tizenopensource.org/license
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  * @file        download-manager-network.cpp
18  * @author      Jungki Kwak (jungki.kwak@samsung.com)
19  * @brief       Download netowkr manager
20  */
21
22 #include <stdlib.h>
23 #include "download-manager-common.h"
24 #include "download-manager-network.h"
25
26 enum {
27         NET_INACTIVE = 0,
28         NET_WIFI_ACTIVE,
29         NET_CELLULAR_ACTIVE
30 };
31
32 NetMgr::NetMgr()
33         :m_handle(NULL)
34 {
35 }
36
37 NetMgr::~NetMgr()
38 {
39 }
40
41 void NetMgr::initNetwork()
42 {
43         if (connection_create(&m_handle) < 0) {
44                 DP_LOGE("Fail to create network handle");
45                 return;
46         }
47
48         if (connection_set_type_changed_cb(m_handle, netTypeChangedCB, NULL)
49                         < 0) {
50                 DP_LOGE("Fail to register network state changed cb");
51                 return;
52         }
53         if (connection_set_ip_address_changed_cb(m_handle, netConfigChangedCB, NULL)
54                         < 0) {
55                 DP_LOGE("Fail to register ip address changed cb");
56                 return;
57         }
58 }
59
60 void NetMgr::deinitNetwork()
61 {
62         if (connection_unset_type_changed_cb(m_handle) < 0) {
63                 DP_LOGE("Fail to unregister network state changed cb");
64         }
65         if (connection_unset_ip_address_changed_cb(m_handle) < 0) {
66                 DP_LOGE("Fail to unregister ip address changed cb");
67         }
68         if (connection_destroy(m_handle) < 0) {
69                 DP_LOGE("Fail to destroy network handle");
70         }
71 }
72
73 int NetMgr::getConnectionState()
74 {
75         connection_type_e type = CONNECTION_TYPE_DISCONNECTED;
76         int ret = 0;
77
78         if (!m_handle) {
79                 DP_LOGE("handle is NULL");
80                 return NET_INACTIVE;
81         }
82         if (connection_get_type(m_handle, &type) < 0) {
83                 DP_LOGE(" Fail to get network status");
84                 return NET_INACTIVE;
85         }
86
87         switch (type) {
88         case CONNECTION_TYPE_DISCONNECTED:
89                 DP_LOG("CONNECTION_NETWORK_STATE_DISCONNECTED");
90                 ret = NET_INACTIVE;
91                 break;
92         case CONNECTION_TYPE_WIFI:
93                 DP_LOG("CONNECTION_NETWORK_STATE_WIFI");
94                 ret = getCellularStatus();
95                 break;
96         case CONNECTION_TYPE_CELLULAR:
97                 DP_LOG("CONNECTION_NETWORK_STATE_CELLULAR");
98                 ret = getWifiStatus();
99                 break;
100         default:
101                 DP_LOGE("Cannot enter here");
102                 ret = NET_INACTIVE;
103                 break;
104         }
105         return ret;
106 }
107
108 int NetMgr::getCellularStatus()
109 {
110         connection_cellular_state_e status = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
111         int ret = 0;
112
113         if (!m_handle) {
114                 DP_LOGE("handle is NULL");
115                 return NET_INACTIVE;
116         }
117
118         if (connection_get_cellular_state(m_handle, &status) < 0) {
119                 DP_LOGE(" Fail to get cellular status");
120                 return NET_INACTIVE;
121         }
122
123         switch(status) {
124         case CONNECTION_CELLULAR_STATE_AVAILABLE:
125                 DP_LOG("CONNECTION_CELLULAR_STATE_AVAILABLE");
126                 ret = NET_CELLULAR_ACTIVE;
127                 break;
128         case CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE:
129                 DP_LOG("CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE");
130                 ret = NET_INACTIVE;
131                 break;
132         case CONNECTION_CELLULAR_STATE_FLIGHT_MODE:
133                 DP_LOG("CONNECTION_CELLULAR_STATE_FLIGHT_MODE");
134                 ret = NET_INACTIVE;
135                 break;
136         case CONNECTION_CELLULAR_STATE_ROAMING_OFF:
137                 DP_LOG("CONNECTION_CELLULAR_STATE_ROAMING_OFF");
138                 ret = NET_INACTIVE;
139                 break;
140         case CONNECTION_CELLULAR_STATE_CALL_ONLY_AVAILABLE:
141                 DP_LOG("CONNECTION_CELLULAR_STATE_CALL_ONLY_AVAILABLE");
142                 ret = NET_INACTIVE;
143                 break;
144         default:
145                 DP_LOGE("Cannot enter here");
146                 ret = NET_INACTIVE;
147                 break;
148         }
149         return ret;
150
151 }
152
153 int NetMgr::getWifiStatus()
154 {
155         connection_wifi_state_e status = CONNECTION_WIFI_STATE_DEACTIVATED;
156         int ret = 0;
157
158         if (!m_handle) {
159                 DP_LOGE("handle is NULL");
160                 return NET_INACTIVE;
161         }
162
163         if (connection_get_wifi_state(m_handle, &status) < 0) {
164                 DP_LOGE(" Fail to get wifi status");
165                 return NET_INACTIVE;
166         }
167
168         switch(status) {
169         case CONNECTION_WIFI_STATE_CONNECTED:
170                 DP_LOG("CONNECTION_WIFI_STATE_CONNECTED");
171                 ret = NET_WIFI_ACTIVE;
172                 break;
173         case CONNECTION_WIFI_STATE_DISCONNECTED:
174                 DP_LOG("CONNECTION_WIFI_STATE_DISCONNECTED");
175                 ret = NET_INACTIVE;
176                 break;
177         case CONNECTION_WIFI_STATE_DEACTIVATED:
178                 DP_LOG("CONNECTION_WIFI_STATE_DEACTIVATED");
179                 ret = NET_INACTIVE;
180                 break;
181         default:
182                 DP_LOGE("Cannot enter here");
183                 ret = NET_INACTIVE;
184                 break;
185         }
186         return ret;
187 }
188
189 void NetMgr::netTypeChanged()
190 {
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");
197                 else
198                         DP_LOG("Network is connected");
199                 m_netStatus = changedStatus;
200         } else {
201                 DP_LOG("Network berer type is changed. ex.3G->WIFI");
202         }
203 }
204
205 /* This routine should be operated in case of downloading state.
206  * After the download is finished, network event handler should be removed.
207  */
208 void NetMgr::netConfigChanged(string ipAddr)
209 {
210
211         DP_LOG_FUNC();
212
213         if (ipAddr.length() > 1) {/* network is connected */
214                 getProxy();
215                 getIPAddress();
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
219                 **/
220                 notify();
221         } else {
222                 DP_LOGE("Network connection is disconnected");
223         }
224 }
225
226 void NetMgr::getProxy()
227 {
228         char *proxy = NULL;
229         connection_address_family_e family = CONNECTION_ADDRESS_FAMILY_IPV4;
230
231         if (!m_handle) {
232                 DP_LOGE("handle is NULL");
233                 return;
234         }
235         if (connection_get_proxy(m_handle, family, &proxy) < 0) {
236                 DP_LOGE("Fail to get ip address");
237                 return;
238         }
239         if (proxy) {
240                 DP_LOG("===== Proxy address[%s] =====", proxy);
241                 free(proxy);
242                 proxy = NULL;
243         }
244 }
245
246 void NetMgr::getIPAddress()
247 {
248         char *ipAddr = NULL;
249         connection_address_family_e family = CONNECTION_ADDRESS_FAMILY_IPV4;
250         if (!m_handle) {
251                 DP_LOGE("handle is NULL");
252                 return;
253         }
254         if (connection_get_ip_address(m_handle, family, &ipAddr) < 0) {
255                 DP_LOGE("Fail to get ip address");
256                 return;
257         }
258         if (ipAddr) {
259                 DP_LOG("===== IP address[%s] =====", ipAddr);
260                 free(ipAddr);
261                 ipAddr= NULL;
262         }
263 }
264
265 void NetMgr::netTypeChangedCB(connection_type_e state, void *data)
266 {
267         NetMgr inst = NetMgr::getInstance();
268         inst.netTypeChanged();
269 }
270
271 void NetMgr::netConfigChangedCB(const char *ip, const char *ipv6,
272                 void *data)
273 {
274         string ipAddr = ip;
275         NetMgr inst = NetMgr::getInstance();
276         inst.netConfigChanged(ipAddr);
277 }
278