2 * Copyright (c) 2011 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.
21 #include <vconf/vconf.h>
22 #include <net_connection_private.h>
24 #define TIZEN_NET_CONNECTION "CAPI_NETWORK_CONNECTION"
26 static GSList *conn_handle_list = NULL;
28 static void __connection_cb_state_change_cb(keynode_t *node, void *user_data);
29 static void __connection_cb_ip_change_cb(keynode_t *node, void *user_data);
30 static void __connection_cb_proxy_change_cb(keynode_t *node, void *user_data);
33 static int __connection_convert_net_state(int status)
36 case VCONFKEY_NETWORK_CELLULAR:
37 return CONNECTION_NETWORK_STATE_CELLULAR;
38 case VCONFKEY_NETWORK_WIFI:
39 return CONNECTION_NETWORK_STATE_WIFI;
41 return CONNECTION_NETWORK_STATE_DISCONNECTED;
45 static int __connection_convert_cellular_state(int status)
48 case VCONFKEY_NETWORK_CELLULAR_ON:
49 return CONNECTION_CELLULAR_STATE_AVAILABLE;
50 case VCONFKEY_NETWORK_CELLULAR_3G_OPTION_OFF:
51 return CONNECTION_CELLULAR_STATE_CALL_ONLY_AVAILABLE;
52 case VCONFKEY_NETWORK_CELLULAR_ROAMING_OFF:
53 return CONNECTION_CELLULAR_STATE_ROAMING_OFF;
54 case VCONFKEY_NETWORK_CELLULAR_FLIGHT_MODE:
55 return CONNECTION_CELLULAR_STATE_FLIGHT_MODE;
57 return CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
61 static int __connection_convert_wifi_state(int status)
64 case VCONFKEY_NETWORK_WIFI_CONNECTED:
65 return CONNECTION_WIFI_STATE_CONNECTED;
66 case VCONFKEY_NETWORK_WIFI_NOT_CONNECTED:
67 return CONNECTION_WIFI_STATE_DISCONNECTED;
69 return CONNECTION_WIFI_STATE_DEACTIVATED;
73 static int __connection_get_state_changed_callback_count(void)
78 for (list = conn_handle_list; list; list = list->next) {
79 connection_handle_s *local_handle = (connection_handle_s *)list->data;
80 if (local_handle->state_changed_callback) count++;
86 static int __connection_get_ip_changed_callback_count(void)
91 for (list = conn_handle_list; list; list = list->next) {
92 connection_handle_s *local_handle = (connection_handle_s *)list->data;
93 if (local_handle->ip_changed_callback) count++;
99 static int __connection_get_proxy_changed_callback_count(void)
104 for (list = conn_handle_list; list; list = list->next) {
105 connection_handle_s *local_handle = (connection_handle_s *)list->data;
106 if (local_handle->proxy_changed_callback) count++;
112 static int __connection_set_state_changed_callback(connection_h connection, void *callback, void *user_data)
114 connection_handle_s *local_handle = (connection_handle_s *)connection;
117 if (__connection_get_state_changed_callback_count() == 0)
118 vconf_notify_key_changed(VCONFKEY_NETWORK_STATUS ,
119 __connection_cb_state_change_cb,
122 local_handle->state_changed_user_data = user_data;
124 if (local_handle->state_changed_callback &&
125 __connection_get_state_changed_callback_count() == 1)
126 vconf_ignore_key_changed(VCONFKEY_NETWORK_STATUS,
127 __connection_cb_state_change_cb);
130 local_handle->state_changed_callback = callback;
131 return CONNECTION_ERROR_NONE;
134 static int __connection_set_ip_changed_callback(connection_h connection, void *callback, void *user_data)
136 connection_handle_s *local_handle = (connection_handle_s *)connection;
139 if (__connection_get_ip_changed_callback_count() == 0)
140 vconf_notify_key_changed(VCONFKEY_NETWORK_IP,
141 __connection_cb_ip_change_cb,
144 local_handle->ip_changed_user_data = user_data;
146 if (local_handle->ip_changed_callback &&
147 __connection_get_ip_changed_callback_count() == 1)
148 vconf_ignore_key_changed(VCONFKEY_NETWORK_IP,
149 __connection_cb_ip_change_cb);
152 local_handle->ip_changed_callback = callback;
153 return CONNECTION_ERROR_NONE;
156 static int __connection_set_proxy_changed_callback(connection_h connection, void *callback, void *user_data)
158 connection_handle_s *local_handle = (connection_handle_s *)connection;
161 if (__connection_get_proxy_changed_callback_count() == 0)
162 vconf_notify_key_changed(VCONFKEY_NETWORK_PROXY,
163 __connection_cb_proxy_change_cb,
166 local_handle->proxy_changed_callback = user_data;
168 if (local_handle->proxy_changed_callback &&
169 __connection_get_proxy_changed_callback_count() == 1)
170 vconf_ignore_key_changed(VCONFKEY_NETWORK_PROXY,
171 __connection_cb_proxy_change_cb);
174 local_handle->proxy_changed_callback = callback;
175 return CONNECTION_ERROR_NONE;
178 static void __connection_cb_state_change_cb(keynode_t *node, void *user_data)
180 LOGI(TIZEN_NET_CONNECTION,"Net Status Changed Indication\n");
183 int state = vconf_keynode_get_int(node);
185 for (list = conn_handle_list; list; list = list->next) {
186 connection_handle_s *local_handle = (connection_handle_s *)list->data;
187 if (local_handle->state_changed_callback)
188 local_handle->state_changed_callback(
189 __connection_convert_net_state(state),
190 local_handle->state_changed_user_data);
194 static void __connection_cb_ip_change_cb(keynode_t *node, void *user_data)
196 LOGI(TIZEN_NET_CONNECTION,"Net IP Changed Indication\n");
199 char *ip_addr = vconf_keynode_get_str(node);
201 for (list = conn_handle_list; list; list = list->next) {
202 connection_handle_s *local_handle = (connection_handle_s *)list->data;
203 if (local_handle->ip_changed_callback)
204 local_handle->ip_changed_callback(
206 local_handle->ip_changed_user_data);
210 static void __connection_cb_proxy_change_cb(keynode_t *node, void *user_data)
212 LOGI(TIZEN_NET_CONNECTION,"Net IP Changed Indication\n");
215 char *proxy = vconf_keynode_get_str(node);
217 for (list = conn_handle_list; list; list = list->next) {
218 connection_handle_s *local_handle = (connection_handle_s *)list->data;
219 if (local_handle->proxy_changed_callback)
220 local_handle->proxy_changed_callback(
222 local_handle->proxy_changed_user_data);
226 static bool __connection_check_handle_validity(connection_h connection)
230 for (list = conn_handle_list; list; list = list->next)
231 if (connection == list->data) return true;
236 int connection_create(connection_h* connection)
238 if (connection == NULL || __connection_check_handle_validity(*connection)) {
239 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
240 return CONNECTION_ERROR_INVALID_PARAMETER;
243 *connection = g_try_malloc0(sizeof(connection_handle_s));
244 if (*connection != NULL) {
245 LOGI(TIZEN_NET_CONNECTION, "New Handle Created %p\n", *connection);
247 return CONNECTION_ERROR_OUT_OF_MEMORY;
250 conn_handle_list = g_slist_append(conn_handle_list, *connection);
252 return CONNECTION_ERROR_NONE;
255 int connection_destroy(connection_h connection)
257 if (connection == NULL || !(__connection_check_handle_validity(connection))) {
258 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
259 return CONNECTION_ERROR_INVALID_PARAMETER;
262 conn_handle_list = g_slist_remove(conn_handle_list, connection);
264 LOGI(TIZEN_NET_CONNECTION, "Destroy Handle : %p\n", connection);
266 __connection_set_state_changed_callback(connection, NULL, NULL);
267 __connection_set_ip_changed_callback(connection, NULL, NULL);
268 __connection_set_proxy_changed_callback(connection, NULL, NULL);
272 return CONNECTION_ERROR_NONE;
275 int connection_get_network_state(connection_h connection, connection_network_state_e* state)
279 if (state == NULL || !(__connection_check_handle_validity(connection))) {
280 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
281 return CONNECTION_ERROR_INVALID_PARAMETER;
284 if (vconf_get_int(VCONFKEY_NETWORK_STATUS, &status)) {
285 LOGI(TIZEN_NET_CONNECTION,"vconf_get_int Failed = %d\n", status);
286 return CONNECTION_ERROR_INVALID_OPERATION;
289 LOGI(TIZEN_NET_CONNECTION,"Connected Network = %d\n", status);
291 *state = __connection_convert_net_state(status);
293 return CONNECTION_ERROR_NONE;
296 int connection_get_ip_address(connection_h connection, connection_address_family_e address_family, char** ip_address)
298 if (ip_address == NULL || !(__connection_check_handle_validity(connection))) {
299 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
300 return CONNECTION_ERROR_INVALID_PARAMETER;
303 switch (address_family) {
304 case CONNECTION_ADDRESS_FAMILY_IPV4:
305 *ip_address = vconf_get_str(VCONFKEY_NETWORK_IP);
307 case CONNECTION_ADDRESS_FAMILY_IPV6:
308 LOGI(TIZEN_NET_CONNECTION, "Not supported yet\n");
309 return CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED;
312 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
313 return CONNECTION_ERROR_INVALID_PARAMETER;
316 if (*ip_address == NULL) {
317 LOGI(TIZEN_NET_CONNECTION,"vconf_get_str Failed\n");
318 return CONNECTION_ERROR_INVALID_OPERATION;
321 LOGI(TIZEN_NET_CONNECTION,"IP Address %s\n", *ip_address);
323 return CONNECTION_ERROR_NONE;
326 int connection_get_proxy(connection_h connection, connection_address_family_e address_family, char** proxy)
328 if (proxy == NULL || !(__connection_check_handle_validity(connection))) {
329 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
330 return CONNECTION_ERROR_INVALID_PARAMETER;
333 switch (address_family) {
334 case CONNECTION_ADDRESS_FAMILY_IPV4:
335 *proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
337 case CONNECTION_ADDRESS_FAMILY_IPV6:
338 LOGI(TIZEN_NET_CONNECTION, "Not supported yet\n");
339 return CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED;
342 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
343 return CONNECTION_ERROR_INVALID_PARAMETER;
346 if (*proxy == NULL) {
347 LOGI(TIZEN_NET_CONNECTION,"vconf_get_str Failed\n");
348 return CONNECTION_ERROR_INVALID_OPERATION;
351 LOGI(TIZEN_NET_CONNECTION,"Proxy Address %s\n", *proxy);
353 return CONNECTION_ERROR_NONE;
356 int connection_get_cellular_state(connection_h connection, connection_cellular_state_e* state)
360 if (state == NULL || !(__connection_check_handle_validity(connection))) {
361 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
362 return CONNECTION_ERROR_INVALID_PARAMETER;
365 if (!vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE, &status)) {
366 LOGI(TIZEN_NET_CONNECTION,"Cellular = %d\n", status);
367 *state = __connection_convert_cellular_state(status);
368 return CONNECTION_ERROR_NONE;
370 LOGI(TIZEN_NET_CONNECTION,"vconf_get_int Failed = %d\n", status);
371 return CONNECTION_ERROR_INVALID_OPERATION;
375 int connection_get_wifi_state(connection_h connection, connection_wifi_state_e* state)
379 if (state == NULL || !(__connection_check_handle_validity(connection))) {
380 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
381 return CONNECTION_ERROR_INVALID_PARAMETER;
384 if (!vconf_get_int(VCONFKEY_NETWORK_WIFI_STATE, &status)) {
385 LOGI(TIZEN_NET_CONNECTION,"WiFi = %d\n", status);
386 *state = __connection_convert_wifi_state(status);
387 return CONNECTION_ERROR_NONE;
389 LOGI(TIZEN_NET_CONNECTION,"vconf_get_int Failed = %d\n", status);
390 return CONNECTION_ERROR_INVALID_OPERATION;
394 int connection_set_network_state_changed_cb(connection_h connection,
395 connection_network_state_changed_cb callback, void* user_data)
397 if (callback == NULL || !(__connection_check_handle_validity(connection))) {
398 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
399 return CONNECTION_ERROR_INVALID_PARAMETER;
402 return __connection_set_state_changed_callback(connection, callback, user_data);
405 int connection_unset_network_state_changed_cb(connection_h connection)
407 if (!(__connection_check_handle_validity(connection))) {
408 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
409 return CONNECTION_ERROR_INVALID_PARAMETER;
412 return __connection_set_state_changed_callback(connection, NULL, NULL);
415 int connection_set_ip_address_changed_cb(connection_h connection,
416 connection_address_changed_cb callback, void* user_data)
418 if (callback == NULL || !(__connection_check_handle_validity(connection))) {
419 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
420 return CONNECTION_ERROR_INVALID_PARAMETER;
423 return __connection_set_ip_changed_callback(connection, callback, user_data);
426 int connection_unset_ip_address_changed_cb(connection_h connection)
428 if (!(__connection_check_handle_validity(connection))) {
429 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
430 return CONNECTION_ERROR_INVALID_PARAMETER;
433 return __connection_set_ip_changed_callback(connection, NULL, NULL);
436 int connection_set_proxy_address_changed_cb(connection_h connection,
437 connection_address_changed_cb callback, void* user_data)
439 if (callback == NULL || !(__connection_check_handle_validity(connection))) {
440 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
441 return CONNECTION_ERROR_INVALID_PARAMETER;
444 return __connection_set_proxy_changed_callback(connection, callback, user_data);
447 int connection_unset_proxy_address_changed_cb(connection_h connection)
449 if (!(__connection_check_handle_validity(connection))) {
450 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
451 return CONNECTION_ERROR_INVALID_PARAMETER;
454 return __connection_set_proxy_changed_callback(connection, NULL, NULL);
457 static int __fill_call_statistic(connection_h connection, stat_request_e member, int *size)
459 if (size == NULL || !(__connection_check_handle_validity(connection))) {
460 LOGI(TIZEN_NET_CONNECTION, "Wrong Parameter Passed\n");
461 return CONNECTION_ERROR_INVALID_PARAMETER;
465 case LAST_SENT_DATA_SIZE:
466 if (vconf_get_int(VCONFKEY_NETWORK_CELLULAR_PKT_LAST_SNT, size)) {
467 LOGI(TIZEN_NET_CONNECTION,
468 "Cannot Get LAST_SENT_DATA_SIZE = %d\n",
471 return CONNECTION_ERROR_INVALID_OPERATION;
473 LOGI(TIZEN_NET_CONNECTION,"LAST_SENT_DATA_SIZE:%d bytes\n", *size);
476 case LAST_RECEIVED_DATA_SIZE:
477 if (vconf_get_int(VCONFKEY_NETWORK_CELLULAR_PKT_LAST_RCV, size)) {
478 LOGI(TIZEN_NET_CONNECTION,
479 "Cannot Get LAST_RECEIVED_DATA_SIZE: = %d\n",
482 return CONNECTION_ERROR_INVALID_OPERATION;
484 LOGI(TIZEN_NET_CONNECTION,"LAST_RECEIVED_DATA_SIZE:%d bytes\n", *size);
486 case TOTAL_SENT_DATA_SIZE:
487 if (vconf_get_int(VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_SNT, size)) {
488 LOGI(TIZEN_NET_CONNECTION,
489 "Cannot Get TOTAL_SENT_DATA_SIZE: = %d\n",
492 return CONNECTION_ERROR_INVALID_OPERATION;
494 LOGI(TIZEN_NET_CONNECTION,"TOTAL_SENT_DATA_SIZE:%d bytes\n", *size);
496 case TOTAL_RECEIVED_DATA_SIZE:
497 if (vconf_get_int(VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_RCV, size)) {
498 LOGI(TIZEN_NET_CONNECTION,
499 "Cannot Get TOTAL_RECEIVED_DATA_SIZE: = %d\n",
502 return CONNECTION_ERROR_INVALID_OPERATION;
504 LOGI(TIZEN_NET_CONNECTION,"TOTAL_RECEIVED_DATA_SIZE:%d bytes\n", *size);
506 case LAST_WIFI_SENT_DATA_SIZE:
507 if (vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_SNT, size)) {
508 LOGI(TIZEN_NET_CONNECTION,
509 "Cannot Get LAST_WIFI_SENT_DATA_SIZE: = %d\n",
512 return CONNECTION_ERROR_INVALID_OPERATION;
514 LOGI(TIZEN_NET_CONNECTION,"LAST_WIFI_SENT_DATA_SIZE:%d bytes\n", *size);
516 case LAST_WIFI_RECEIVED_DATA_SIZE:
517 if (vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_LAST_RCV, size)) {
518 LOGI(TIZEN_NET_CONNECTION,
519 "Cannot Get LAST_WIFI_RECEIVED_DATA_SIZE: = %d\n",
522 return CONNECTION_ERROR_INVALID_OPERATION;
524 LOGI(TIZEN_NET_CONNECTION,"LAST_WIFI_RECEIVED_DATA_SIZE:%d bytes\n", *size);
526 case TOTAL_WIFI_SENT_DATA_SIZE:
527 if (vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_SNT, size)) {
528 LOGI(TIZEN_NET_CONNECTION,
529 "Cannot Get TOTAL_WIFI_SENT_DATA_SIZE: = %d\n",
532 return CONNECTION_ERROR_INVALID_OPERATION;
534 LOGI(TIZEN_NET_CONNECTION,"TOTAL_WIFI_SENT_DATA_SIZE:%d bytes\n", *size);
536 case TOTAL_WIFI_RECEIVED_DATA_SIZE:
537 if (vconf_get_int(VCONFKEY_NETWORK_WIFI_PKT_TOTAL_RCV, size)) {
538 LOGI(TIZEN_NET_CONNECTION,
539 "Cannot Get TOTAL_WIFI_RECEIVED_DATA_SIZE: = %d\n",
542 return CONNECTION_ERROR_INVALID_OPERATION;
544 LOGI(TIZEN_NET_CONNECTION,"TOTAL_WIFI_RECEIVED_DATA_SIZE:%d bytes\n", *size);
547 return CONNECTION_ERROR_NONE;
550 int connection_get_last_received_data_size(connection_h connection, int *size)
552 return __fill_call_statistic(connection, LAST_RECEIVED_DATA_SIZE, size);
555 int connection_get_last_sent_data_size(connection_h connection, int *size)
557 return __fill_call_statistic(connection, LAST_SENT_DATA_SIZE, size);
560 int connection_get_total_received_data_size (connection_h connection, int *size)
562 return __fill_call_statistic(connection, TOTAL_RECEIVED_DATA_SIZE, size);
565 int connection_get_total_sent_data_size (connection_h connection, int *size)
567 return __fill_call_statistic(connection, TOTAL_SENT_DATA_SIZE, size);
570 int connection_get_wifi_last_received_data_size(connection_h connection, int *size)
572 return __fill_call_statistic(connection, LAST_WIFI_RECEIVED_DATA_SIZE, size);
575 int connection_get_wifi_last_sent_data_size(connection_h connection, int *size)
577 return __fill_call_statistic(connection, LAST_WIFI_SENT_DATA_SIZE, size);
580 int connection_get_wifi_total_received_data_size (connection_h connection, int *size)
582 return __fill_call_statistic(connection, TOTAL_WIFI_RECEIVED_DATA_SIZE, size);
585 int connection_get_wifi_total_sent_data_size (connection_h connection, int *size)
587 return __fill_call_statistic(connection, TOTAL_WIFI_SENT_DATA_SIZE, size);