3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include <net_connection.h>
22 #include "mobileap_agent.h"
23 #include "mobileap_common.h"
24 #include "mobileap_network.h"
28 static connection_h connection = NULL;
29 static connection_profile_h cprof = NULL;
31 static void __print_profile(connection_profile_h profile)
39 char *home_url = NULL;
40 connection_cellular_network_type_e network_type;
41 connection_cellular_service_type_e service_type;
43 conn_ret = connection_profile_get_cellular_network_type(profile, &network_type);
44 if (conn_ret != CONNECTION_ERROR_NONE)
45 ERR("connection API fail : 0x%X\n", conn_ret);
47 DBG("Network type : %d\n", network_type);
49 conn_ret = connection_profile_get_cellular_service_type(profile, &service_type);
50 if (conn_ret != CONNECTION_ERROR_NONE)
51 ERR("connection API fail : 0x%X\n", conn_ret);
53 DBG("Service type : %d\n", service_type);
55 conn_ret = connection_profile_get_cellular_apn(profile, &apn);
56 if (conn_ret != CONNECTION_ERROR_NONE)
57 ERR("connection API fail : 0x%X\n", conn_ret);
59 DBG("APN : %s\n", apn);
63 conn_ret = connection_profile_get_cellular_home_url(profile, &home_url);
64 if (conn_ret != CONNECTION_ERROR_NONE)
65 ERR("connection API fail : 0x%X\n", conn_ret);
67 DBG("Home url : %s\n", home_url);
71 conn_ret = connection_profile_is_cellular_roaming(profile, &roaming);
72 if (conn_ret != CONNECTION_ERROR_NONE)
73 ERR("connection API fail : 0x%X\n", conn_ret);
75 DBG("Roaming : %d\n", roaming);
80 static gboolean __is_connected_profile(connection_profile_h profile)
82 if (profile == NULL) {
83 ERR("profile is NULL\n");
88 connection_profile_state_e pstat = CONNECTION_PROFILE_STATE_DISCONNECTED;
90 conn_ret = connection_profile_get_state(profile, &pstat);
91 if (conn_ret != CONNECTION_ERROR_NONE) {
92 ERR("connection_profile_get_state is failed: 0x%X\n", conn_ret);
96 if (pstat != CONNECTION_PROFILE_STATE_CONNECTED) {
97 DBG("Profile is not connected\n");
101 DBG("Profile is connected\n");
106 static gboolean __get_connected_profile(connection_profile_h *r_prof, connection_profile_type_e *r_net_type)
108 if (r_prof == NULL || r_net_type == NULL) {
109 ERR("Invalid param [%p] [%p]\n", r_prof, r_net_type);
114 connection_profile_h profile = NULL;
115 connection_profile_type_e net_type = CONNECTION_PROFILE_TYPE_CELLULAR;
117 conn_ret = connection_get_current_profile(connection, &profile);
118 if (conn_ret != CONNECTION_ERROR_NONE) {
119 ERR("connection_get_current_profile is failed : %d\n", conn_ret);
123 conn_ret = connection_profile_get_type(profile, &net_type);
124 if (conn_ret != CONNECTION_ERROR_NONE) {
125 ERR("connection_profile_get_type is failed : 0x%X\n", conn_ret);
126 connection_profile_destroy(profile);
131 *r_net_type = net_type;
135 static gboolean __get_network_profile(connection_profile_h *r_prof)
137 if (r_prof == NULL) {
138 ERR("r_prof is NULL\n");
142 connection_profile_h profile;
143 connection_profile_type_e net_type = CONNECTION_PROFILE_TYPE_CELLULAR;
145 if (__get_connected_profile(&profile, &net_type) == FALSE) {
146 ERR("There is no available network\n");
150 DBG("Current connected net_type : %d\n", net_type);
151 if (net_type == CONNECTION_PROFILE_TYPE_WIFI) {
156 if (net_type != CONNECTION_PROFILE_TYPE_CELLULAR) {
157 ERR("Network type [%d] is not supported\n", net_type);
160 __print_profile(profile);
166 static void __connection_type_changed_cb(connection_type_e type, void *user_data)
168 DBG("Changed connection type is %s\n",
169 type == CONNECTION_TYPE_DISCONNECTED ? "DISCONNECTED" :
170 type == CONNECTION_TYPE_WIFI ? "Wi-Fi" :
171 type == CONNECTION_TYPE_CELLULAR ? "Cellular" :
172 type == CONNECTION_TYPE_ETHERNET ? "Ethernet" :
176 if (_mobileap_is_disabled()) {
177 DBG("Tethering is not enabled\n");
181 if (_unset_masquerade() == FALSE) {
182 ERR("_unset_masquerade is failed\n");
186 connection_profile_destroy(cprof);
190 if (type == CONNECTION_TYPE_DISCONNECTED) {
199 gboolean _is_trying_network_operation(void)
205 gboolean _get_network_interface_name(char **if_name)
207 if (if_name == NULL) {
208 ERR("if_name is NULL\n");
213 ERR("There is no connected profile\n");
219 conn_ret = connection_profile_get_network_interface_name(cprof, if_name);
220 if (conn_ret != CONNECTION_ERROR_NONE) {
221 ERR("connection_profile_get_network_interface_name is failed : 0x%X\n", conn_ret);
228 gboolean _set_masquerade(void)
230 char *if_name = NULL;
232 if (_get_network_interface_name(&if_name) == FALSE) {
233 ERR("_get_network_interface_name is failed\n");
236 DBG("Network interface : %s\n", if_name);
238 _mh_core_enable_masquerade(if_name);
244 gboolean _unset_masquerade(void)
247 DBG("There is nothing to unset masquerading\n");
251 char *if_name = NULL;
253 if (_get_network_interface_name(&if_name) == FALSE) {
254 ERR("_get_network_interface_name is failed\n");
257 DBG("Network interface : %s\n", if_name);
259 _mh_core_disable_masquerade(if_name);
265 gboolean _open_network(void)
267 connection_profile_h profile = NULL;
271 if (__get_network_profile(&profile) == FALSE) {
272 ERR("__get_network_profile is failed\n");
276 if (!__is_connected_profile(profile)) {
277 connection_profile_destroy(profile);
282 if (_set_masquerade() == FALSE) {
283 ERR("_set_masquerade is failed\n");
293 gboolean _close_network(void)
299 ret = _unset_masquerade();
301 ERR("_unset_masquerade is failed\n");
303 connection_profile_destroy(cprof);
311 gboolean _init_network(void *user_data)
315 conn_ret = connection_create(&connection);
316 if (conn_ret != CONNECTION_ERROR_NONE) {
317 ERR("connection_create is failed : 0x%X\n", conn_ret);
321 conn_ret = connection_set_type_changed_cb(connection,
322 __connection_type_changed_cb, user_data);
323 if (conn_ret != CONNECTION_ERROR_NONE) {
324 ERR("connection_set_type_changed cb is failed : 0x%X\n", conn_ret);
325 connection_destroy(connection);
333 gboolean _deinit_network(void)
337 if (connection == NULL) {
338 ERR("Connection handle is not initialized\n");
342 conn_ret = connection_unset_type_changed_cb(connection);
343 if (conn_ret != CONNECTION_ERROR_NONE) {
344 ERR("connection_unset_type_changed_cb is failed : %d\n", conn_ret);
347 connection_destroy(connection);