8dbe36a2c455a0084fa1fc1569dee130ba48d229
[platform/core/api/connection.git] / src / connection.c
1 /*
2  * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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 #include <stdio.h>
18 #include <string.h>
19 #include <glib.h>
20 #include <vconf/vconf.h>
21 #include "net_connection_private.h"
22
23 static GSList *conn_handle_list = NULL;
24
25 static void __connection_cb_state_change_cb(keynode_t *node, void *user_data);
26 static void __connection_cb_ip_change_cb(keynode_t *node, void *user_data);
27 static void __connection_cb_proxy_change_cb(keynode_t *node, void *user_data);
28
29 static int __connection_convert_net_state(int status)
30 {
31         switch (status) {
32         case VCONFKEY_NETWORK_CELLULAR:
33                 return CONNECTION_TYPE_CELLULAR;
34         case VCONFKEY_NETWORK_WIFI:
35                 return CONNECTION_TYPE_WIFI;
36         case VCONFKEY_NETWORK_ETHERNET:
37                 return CONNECTION_TYPE_ETHERNET;
38         case VCONFKEY_NETWORK_BLUETOOTH:
39                 return CONNECTION_TYPE_BT;
40         default:
41                 return CONNECTION_TYPE_DISCONNECTED;
42         }
43 }
44
45 static int __connection_convert_cellular_state(int status)
46 {
47         switch (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;
56         default:
57                 return CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
58         }
59 }
60
61 static int __connection_get_type_changed_callback_count(void)
62 {
63         GSList *list;
64         int count = 0;
65
66         for (list = conn_handle_list; list; list = list->next) {
67                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
68                 if (local_handle->type_changed_callback) count++;
69         }
70
71         return count;
72 }
73
74 static int __connection_get_ip_changed_callback_count(void)
75 {
76         GSList *list;
77         int count = 0;
78
79         for (list = conn_handle_list; list; list = list->next) {
80                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
81                 if (local_handle->ip_changed_callback) count++;
82         }
83
84         return count;
85 }
86
87 static int __connection_get_proxy_changed_callback_count(void)
88 {
89         GSList *list;
90         int count = 0;
91
92         for (list = conn_handle_list; list; list = list->next) {
93                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
94                 if (local_handle->proxy_changed_callback) count++;
95         }
96
97         return count;
98 }
99
100 static void __connection_cb_ethernet_cable_state_changed_cb(connection_ethernet_cable_state_e state)
101 {
102         CONNECTION_LOG(CONNECTION_INFO, "Ethernet Cable state Indication");
103
104         GSList *list;
105
106         for (list = conn_handle_list; list; list = list->next) {
107                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
108                 if (local_handle->ethernet_cable_state_changed_callback)
109                         local_handle->ethernet_cable_state_changed_callback(state,
110                                         local_handle->ethernet_cable_state_changed_user_data);
111         }
112 }
113
114 static int __connection_get_ethernet_cable_state_changed_callback_count(void)
115 {
116         GSList *list;
117         int count = 0;
118
119         for (list = conn_handle_list; list; list = list->next) {
120                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
121                 if (local_handle->ethernet_cable_state_changed_callback) count++;
122         }
123
124         return count;
125 }
126
127 static int __connection_set_type_changed_callback(connection_h connection,
128                                                         void *callback, void *user_data)
129 {
130         connection_handle_s *local_handle = (connection_handle_s *)connection;
131
132         if (callback) {
133                 if (__connection_get_type_changed_callback_count() == 0)
134                         if (vconf_notify_key_changed(VCONFKEY_NETWORK_STATUS ,
135                                         __connection_cb_state_change_cb, NULL))
136                                 return CONNECTION_ERROR_OPERATION_FAILED;
137
138                 local_handle->type_changed_user_data = user_data;
139         } else {
140                 if (local_handle->type_changed_callback &&
141                     __connection_get_type_changed_callback_count() == 1)
142                         if (vconf_ignore_key_changed(VCONFKEY_NETWORK_STATUS,
143                                         __connection_cb_state_change_cb))
144                                 return CONNECTION_ERROR_OPERATION_FAILED;
145         }
146
147         local_handle->type_changed_callback = callback;
148         return CONNECTION_ERROR_NONE;
149 }
150
151 static int __connection_set_ip_changed_callback(connection_h connection,
152                                                         void *callback, void *user_data)
153 {
154         connection_handle_s *local_handle = (connection_handle_s *)connection;
155
156         if (callback) {
157                 if (__connection_get_ip_changed_callback_count() == 0)
158                         if (vconf_notify_key_changed(VCONFKEY_NETWORK_IP,
159                                         __connection_cb_ip_change_cb, NULL))
160                                 return CONNECTION_ERROR_OPERATION_FAILED;
161
162                 local_handle->ip_changed_user_data = user_data;
163         } else {
164                 if (local_handle->ip_changed_callback &&
165                     __connection_get_ip_changed_callback_count() == 1)
166                         if (vconf_ignore_key_changed(VCONFKEY_NETWORK_IP,
167                                         __connection_cb_ip_change_cb))
168                                 return CONNECTION_ERROR_OPERATION_FAILED;
169         }
170
171         local_handle->ip_changed_callback = callback;
172         return CONNECTION_ERROR_NONE;
173 }
174
175 static int __connection_set_proxy_changed_callback(connection_h connection,
176                                                         void *callback, void *user_data)
177 {
178         connection_handle_s *local_handle = (connection_handle_s *)connection;
179
180         if (callback) {
181                 if (__connection_get_proxy_changed_callback_count() == 0)
182                         if (vconf_notify_key_changed(VCONFKEY_NETWORK_PROXY,
183                                         __connection_cb_proxy_change_cb, NULL))
184                                 return CONNECTION_ERROR_OPERATION_FAILED;
185
186                 local_handle->proxy_changed_user_data = user_data;
187         } else {
188                 if (local_handle->proxy_changed_callback &&
189                     __connection_get_proxy_changed_callback_count() == 1)
190                         if (vconf_ignore_key_changed(VCONFKEY_NETWORK_PROXY,
191                                         __connection_cb_proxy_change_cb))
192                                 return CONNECTION_ERROR_OPERATION_FAILED;
193         }
194
195         local_handle->proxy_changed_callback = callback;
196         return CONNECTION_ERROR_NONE;
197 }
198
199 static void __connection_cb_state_change_cb(keynode_t *node, void *user_data)
200 {
201         CONNECTION_LOG(CONNECTION_INFO, "Net Status Changed Indication\n");
202
203         GSList *list;
204         int state = vconf_keynode_get_int(node);
205
206         for (list = conn_handle_list; list; list = list->next) {
207                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
208                 if (local_handle->type_changed_callback)
209                         local_handle->type_changed_callback(
210                                         __connection_convert_net_state(state),
211                                         local_handle->type_changed_user_data);
212         }
213 }
214
215 static void __connection_cb_ip_change_cb(keynode_t *node, void *user_data)
216 {
217         CONNECTION_LOG(CONNECTION_INFO, "Net IP Changed Indication\n");
218
219         GSList *list;
220         char *ip_addr = vconf_keynode_get_str(node);
221
222         for (list = conn_handle_list; list; list = list->next) {
223                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
224                 if (local_handle->ip_changed_callback)
225                         local_handle->ip_changed_callback(
226                                         ip_addr, NULL,
227                                         local_handle->ip_changed_user_data);
228         }
229 }
230
231 static void __connection_cb_proxy_change_cb(keynode_t *node, void *user_data)
232 {
233         CONNECTION_LOG(CONNECTION_INFO, "Net IP Changed Indication\n");
234
235         GSList *list;
236         char *proxy = vconf_keynode_get_str(node);
237
238         for (list = conn_handle_list; list; list = list->next) {
239                 connection_handle_s *local_handle = (connection_handle_s *)list->data;
240                 if (local_handle->proxy_changed_callback)
241                         local_handle->proxy_changed_callback(
242                                         proxy, NULL,
243                                         local_handle->proxy_changed_user_data);
244         }
245 }
246
247 static bool __connection_check_handle_validity(connection_h connection)
248 {
249         bool ret = false;
250
251         if (connection == NULL)
252                 return false;
253
254         if (g_slist_find(conn_handle_list, connection) != NULL)
255                 ret = true;
256
257         return ret;
258 }
259
260 static int __connection_set_ethernet_cable_state_changed_cb(connection_h connection,
261                 connection_ethernet_cable_state_chaged_cb callback, void *user_data)
262 {
263         connection_handle_s *local_handle = (connection_handle_s *)connection;
264
265         if (callback) {
266                 if (__connection_get_ethernet_cable_state_changed_callback_count() == 0)
267                         _connection_libnet_set_ethernet_cable_state_changed_cb(
268                                         __connection_cb_ethernet_cable_state_changed_cb);
269
270         } else {
271                 if (__connection_get_ethernet_cable_state_changed_callback_count() == 1)
272                         _connection_libnet_set_ethernet_cable_state_changed_cb(NULL);
273         }
274
275         local_handle->ethernet_cable_state_changed_callback = callback;
276         local_handle->ethernet_cable_state_changed_user_data = user_data;
277         return CONNECTION_ERROR_NONE;
278 }
279
280 static int __connection_get_handle_count(void)
281 {
282         GSList *list;
283         int count = 0;
284
285         if (!conn_handle_list)
286                 return count;
287
288         for (list = conn_handle_list; list; list = list->next) count++;
289
290         return count;
291 }
292
293 /* Connection Manager ********************************************************/
294 EXPORT_API int connection_create(connection_h* connection)
295 {
296         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
297
298         CONNECTION_MUTEX_LOCK;
299         int rv;
300         if (connection == NULL || __connection_check_handle_validity(*connection)) {
301                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
302                 CONNECTION_MUTEX_UNLOCK;
303                 return CONNECTION_ERROR_INVALID_PARAMETER;
304         }
305
306         rv = _connection_libnet_init();
307         if (rv == NET_ERR_ACCESS_DENIED) {
308                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
309                 CONNECTION_MUTEX_UNLOCK;
310                 return CONNECTION_ERROR_PERMISSION_DENIED;
311         }
312         else if (rv != NET_ERR_NONE) {
313                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to create connection[%d]", rv);
314                 CONNECTION_MUTEX_UNLOCK;
315                 return CONNECTION_ERROR_OPERATION_FAILED;
316         }
317
318         *connection = g_try_malloc0(sizeof(connection_handle_s));
319         if (*connection != NULL) {
320                 CONNECTION_LOG(CONNECTION_INFO, "New Handle Created %p\n", *connection);
321         } else {
322                 CONNECTION_MUTEX_UNLOCK;
323                 return CONNECTION_ERROR_OUT_OF_MEMORY;
324         }
325
326         conn_handle_list = g_slist_append(conn_handle_list, *connection);
327
328         CONNECTION_MUTEX_UNLOCK;
329         return CONNECTION_ERROR_NONE;
330 }
331
332 EXPORT_API int connection_destroy(connection_h connection)
333 {
334         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
335
336         CONNECTION_MUTEX_LOCK;
337
338         if (connection == NULL || !(__connection_check_handle_validity(connection))) {
339                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
340                 CONNECTION_MUTEX_UNLOCK;
341                 return CONNECTION_ERROR_INVALID_PARAMETER;
342         }
343
344         CONNECTION_LOG(CONNECTION_INFO, "Destroy Handle : %p\n", connection);
345
346         __connection_set_type_changed_callback(connection, NULL, NULL);
347         __connection_set_ip_changed_callback(connection, NULL, NULL);
348         __connection_set_proxy_changed_callback(connection, NULL, NULL);
349         __connection_set_ethernet_cable_state_changed_cb(connection, NULL, NULL);
350
351         conn_handle_list = g_slist_remove(conn_handle_list, connection);
352
353         g_free(connection);
354
355         if (__connection_get_handle_count() == 0)
356                 _connection_libnet_deinit();
357
358         CONNECTION_MUTEX_UNLOCK;
359         return CONNECTION_ERROR_NONE;
360 }
361
362 EXPORT_API int connection_get_type(connection_h connection, connection_type_e* type)
363 {
364         int status = 0;
365
366         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
367
368         if (type == NULL || !(__connection_check_handle_validity(connection))) {
369                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
370                 return CONNECTION_ERROR_INVALID_PARAMETER;
371         }
372
373         if (vconf_get_int(VCONFKEY_NETWORK_STATUS, &status)) {
374                 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_int Failed = %d\n", status);
375                 return CONNECTION_ERROR_OPERATION_FAILED;
376         }
377
378         CONNECTION_LOG(CONNECTION_INFO, "Connected Network = %d\n", status);
379
380         *type = __connection_convert_net_state(status);
381
382         return CONNECTION_ERROR_NONE;
383 }
384
385 EXPORT_API int connection_get_ip_address(connection_h connection,
386                                          connection_address_family_e address_family, char **ip_address)
387 {
388         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
389
390         if (ip_address == NULL || !(__connection_check_handle_validity(connection))) {
391                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
392                 return CONNECTION_ERROR_INVALID_PARAMETER;
393         }
394
395         switch (address_family) {
396         case CONNECTION_ADDRESS_FAMILY_IPV4:
397         case CONNECTION_ADDRESS_FAMILY_IPV6:
398                 *ip_address = vconf_get_str(VCONFKEY_NETWORK_IP);
399                 break;
400
401         default:
402                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
403                 return CONNECTION_ERROR_INVALID_PARAMETER;
404         }
405
406         if (*ip_address == NULL) {
407                 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_str Failed");
408                 return CONNECTION_ERROR_OPERATION_FAILED;
409         }
410
411         return CONNECTION_ERROR_NONE;
412 }
413
414 EXPORT_API int connection_get_proxy(connection_h connection,
415                                     connection_address_family_e address_family, char **proxy)
416 {
417         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
418
419         if (proxy == NULL || !(__connection_check_handle_validity(connection))) {
420                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
421                 return CONNECTION_ERROR_INVALID_PARAMETER;
422         }
423
424         switch (address_family) {
425         case CONNECTION_ADDRESS_FAMILY_IPV4:
426         case CONNECTION_ADDRESS_FAMILY_IPV6:
427                 *proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
428                 break;
429
430         default:
431                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
432                 return CONNECTION_ERROR_INVALID_PARAMETER;
433         }
434
435         if (*proxy == NULL) {
436                 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_str Failed");
437                 return CONNECTION_ERROR_OPERATION_FAILED;
438         }
439
440         return CONNECTION_ERROR_NONE;
441 }
442
443 EXPORT_API int connection_get_mac_address(connection_h connection, connection_type_e type, char** mac_addr)
444 {
445         FILE *fp;
446         char buf[CONNECTION_MAC_INFO_LENGTH + 1];
447
448         CHECK_FEATURE_SUPPORTED(WIFI_FEATURE, ETHERNET_FEATURE);
449
450         if(type == CONNECTION_TYPE_WIFI)
451                 CHECK_FEATURE_SUPPORTED(WIFI_FEATURE);
452         else if(type == CONNECTION_TYPE_ETHERNET)
453                 CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
454
455         if (mac_addr == NULL || !(__connection_check_handle_validity(connection))) {
456                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
457                 return CONNECTION_ERROR_INVALID_PARAMETER;
458         }
459
460         switch (type) {
461         case CONNECTION_TYPE_WIFI:
462                 fp = fopen(WIFI_MAC_INFO_FILE, "r");
463                 if (fp == NULL) {
464                         CONNECTION_LOG(CONNECTION_ERROR, "Failed to open file %s", WIFI_MAC_INFO_FILE);
465                         return CONNECTION_ERROR_OUT_OF_MEMORY;
466                 }
467
468                 if (fgets(buf, sizeof(buf), fp) == NULL) {
469                         CONNECTION_LOG(CONNECTION_ERROR, "Failed to get MAC info from %s", WIFI_MAC_INFO_FILE);
470                         fclose(fp);
471                         return CONNECTION_ERROR_OPERATION_FAILED;
472                 }
473
474                 CONNECTION_LOG(CONNECTION_INFO, "%s : %s", WIFI_MAC_INFO_FILE, buf);
475
476                 *mac_addr = (char *)malloc(CONNECTION_MAC_INFO_LENGTH + 1);
477                 if (*mac_addr == NULL) {
478                         CONNECTION_LOG(CONNECTION_ERROR, "malloc() failed");
479                         fclose(fp);
480                         return CONNECTION_ERROR_OUT_OF_MEMORY;
481                 }
482                 g_strlcpy(*mac_addr, buf, CONNECTION_MAC_INFO_LENGTH + 1);
483                 fclose(fp);
484                 break;
485         case CONNECTION_TYPE_ETHERNET:
486                 fp = fopen(ETHERNET_MAC_INFO_FILE, "r");
487                 if (fp == NULL) {
488                         CONNECTION_LOG(CONNECTION_ERROR, "Failed to open file %s", ETHERNET_MAC_INFO_FILE);
489                         return CONNECTION_ERROR_OUT_OF_MEMORY;
490                 }
491
492                 if (fgets(buf, sizeof(buf), fp) == NULL) {
493                         CONNECTION_LOG(CONNECTION_ERROR, "Failed to get MAC info from %s", ETHERNET_MAC_INFO_FILE);
494                         fclose(fp);
495                         return CONNECTION_ERROR_OPERATION_FAILED;
496                 }
497
498                 CONNECTION_LOG(CONNECTION_INFO, "%s : %s", ETHERNET_MAC_INFO_FILE, buf);
499
500                 *mac_addr = (char *)malloc(CONNECTION_MAC_INFO_LENGTH + 1);
501                 if (*mac_addr == NULL) {
502                         CONNECTION_LOG(CONNECTION_ERROR, "malloc() failed");
503                         fclose(fp);
504                         return CONNECTION_ERROR_OUT_OF_MEMORY;
505                 }
506
507                 g_strlcpy(*mac_addr, buf,CONNECTION_MAC_INFO_LENGTH + 1);
508                 fclose(fp);
509
510                 break;
511         default:
512                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
513                 return CONNECTION_ERROR_INVALID_PARAMETER;
514         }
515
516         /* Checking Invalid MAC Address */
517         if((strcmp(*mac_addr, "00:00:00:00:00:00") == 0) ||
518                         (strcmp(*mac_addr, "ff:ff:ff:ff:ff:ff") == 0)) {
519                 CONNECTION_LOG(CONNECTION_ERROR, "MAC Address(%s) is invalid", *mac_addr);
520                 return CONNECTION_ERROR_INVALID_OPERATION;
521         }
522
523         CONNECTION_LOG(CONNECTION_INFO, "MAC Address %s", *mac_addr);
524
525         return CONNECTION_ERROR_NONE;
526 }
527
528 EXPORT_API int connection_get_cellular_state(connection_h connection, connection_cellular_state_e* state)
529 {
530         int status = 0;
531         int cellular_state = 0;
532
533         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
534
535         if (state == NULL || !(__connection_check_handle_validity(connection))) {
536                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
537                 return CONNECTION_ERROR_INVALID_PARAMETER;
538         }
539
540         if (!vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE, &status)) {
541                 CONNECTION_LOG(CONNECTION_INFO, "Cellular = %d\n", status);
542                 *state = __connection_convert_cellular_state(status);
543
544                 if (*state == CONNECTION_CELLULAR_STATE_AVAILABLE) {
545                         if (vconf_get_int(VCONFKEY_DNET_STATE, &cellular_state)) {
546                                 CONNECTION_LOG(CONNECTION_ERROR,
547                                                 "vconf_get_int Failed = %d\n", cellular_state);
548                                 return CONNECTION_ERROR_OPERATION_FAILED;
549                         }
550                 }
551
552                 CONNECTION_LOG(CONNECTION_INFO, "Connection state = %d\n", cellular_state);
553
554                 if (cellular_state == VCONFKEY_DNET_NORMAL_CONNECTED ||
555                     cellular_state == VCONFKEY_DNET_SECURE_CONNECTED ||
556                     cellular_state == VCONFKEY_DNET_TRANSFER)
557                         *state = CONNECTION_CELLULAR_STATE_CONNECTED;
558
559                 return CONNECTION_ERROR_NONE;
560         } else {
561                 CONNECTION_LOG(CONNECTION_ERROR, "vconf_get_int Failed = %d\n", status);
562                 return CONNECTION_ERROR_OPERATION_FAILED;
563         }
564 }
565
566 EXPORT_API int connection_get_wifi_state(connection_h connection, connection_wifi_state_e* state)
567 {
568         CHECK_FEATURE_SUPPORTED(WIFI_FEATURE);
569
570         int rv;
571
572         if (state == NULL || !(__connection_check_handle_validity(connection))) {
573                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
574                 return CONNECTION_ERROR_INVALID_PARAMETER;
575         }
576
577         rv = _connection_libnet_get_wifi_state(state);
578         if (rv != CONNECTION_ERROR_NONE) {
579                 CONNECTION_LOG(CONNECTION_ERROR, "Fail to get Wi-Fi state[%d]", rv);
580                 return rv;
581         }
582
583         CONNECTION_LOG(CONNECTION_INFO, "WiFi state = %d\n", *state);
584
585         return CONNECTION_ERROR_NONE;
586 }
587
588 EXPORT_API int connection_get_ethernet_state(connection_h connection, connection_ethernet_state_e* state)
589 {
590         CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
591
592         if (state == NULL || !(__connection_check_handle_validity(connection))) {
593                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
594                 return CONNECTION_ERROR_INVALID_PARAMETER;
595         }
596
597         return _connection_libnet_get_ethernet_state(state);
598 }
599
600 EXPORT_API int connection_get_ethernet_cable_state(connection_h connection, connection_ethernet_cable_state_e *state)
601 {
602         CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
603
604         if (state == NULL || !(__connection_check_handle_validity(connection))) {
605                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
606                 return CONNECTION_ERROR_INVALID_PARAMETER;
607         }
608
609         return _connection_libnet_get_ethernet_cable_state(state);
610 }
611
612 EXPORT_API int connection_set_ethernet_cable_state_chaged_cb(connection_h connection,
613                           connection_ethernet_cable_state_chaged_cb callback, void *user_data)
614 {
615         CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
616
617         if (callback == NULL || !(__connection_check_handle_validity(connection))) {
618                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
619                 return CONNECTION_ERROR_INVALID_PARAMETER;
620         }
621
622         return __connection_set_ethernet_cable_state_changed_cb(connection,
623                                                         callback, user_data);
624 }
625
626 EXPORT_API int connection_unset_ethernet_cable_state_chaged_cb(connection_h connection)
627 {
628         CHECK_FEATURE_SUPPORTED(ETHERNET_FEATURE);
629
630         if ( !(__connection_check_handle_validity(connection)) ) {
631                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
632                 return CONNECTION_ERROR_INVALID_PARAMETER;
633         }
634
635         return __connection_set_ethernet_cable_state_changed_cb(connection,
636                                                         NULL, NULL);
637 }
638
639 EXPORT_API int connection_get_bt_state(connection_h connection, connection_bt_state_e* state)
640 {
641         CHECK_FEATURE_SUPPORTED(TETHERING_BLUETOOTH_FEATURE);
642
643         if (state == NULL || !(__connection_check_handle_validity(connection))) {
644                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
645                 return CONNECTION_ERROR_INVALID_PARAMETER;
646         }
647
648         return _connection_libnet_get_bluetooth_state(state);
649
650 }
651
652 EXPORT_API int connection_set_type_changed_cb(connection_h connection,
653                                         connection_type_changed_cb callback, void* user_data)
654 {
655         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
656
657         if (callback == NULL || !(__connection_check_handle_validity(connection))) {
658                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
659                 return CONNECTION_ERROR_INVALID_PARAMETER;
660         }
661
662         return __connection_set_type_changed_callback(connection, callback, user_data);
663 }
664
665 EXPORT_API int connection_unset_type_changed_cb(connection_h connection)
666 {
667         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
668
669         if (!(__connection_check_handle_validity(connection))) {
670                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
671                 return CONNECTION_ERROR_INVALID_PARAMETER;
672         }
673
674         return __connection_set_type_changed_callback(connection, NULL, NULL);
675 }
676
677 EXPORT_API int connection_set_ip_address_changed_cb(connection_h connection,
678                                 connection_address_changed_cb callback, void* user_data)
679 {
680         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
681
682         if (callback == NULL || !(__connection_check_handle_validity(connection))) {
683                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
684                 return CONNECTION_ERROR_INVALID_PARAMETER;
685         }
686
687         return __connection_set_ip_changed_callback(connection, callback, user_data);
688 }
689
690 EXPORT_API int connection_unset_ip_address_changed_cb(connection_h connection)
691 {
692         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
693
694         if (!(__connection_check_handle_validity(connection))) {
695                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
696                 return CONNECTION_ERROR_INVALID_PARAMETER;
697         }
698
699         return __connection_set_ip_changed_callback(connection, NULL, NULL);
700 }
701
702 EXPORT_API int connection_set_proxy_address_changed_cb(connection_h connection,
703                                 connection_address_changed_cb callback, void* user_data)
704 {
705         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
706
707         if (callback == NULL || !(__connection_check_handle_validity(connection))) {
708                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
709                 return CONNECTION_ERROR_INVALID_PARAMETER;
710         }
711
712         return __connection_set_proxy_changed_callback(connection, callback, user_data);
713 }
714
715 EXPORT_API int connection_unset_proxy_address_changed_cb(connection_h connection)
716 {
717         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
718
719         if (!(__connection_check_handle_validity(connection))) {
720                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
721                 return CONNECTION_ERROR_INVALID_PARAMETER;
722         }
723
724         return __connection_set_proxy_changed_callback(connection, NULL, NULL);
725 }
726
727 EXPORT_API int connection_add_profile(connection_h connection, connection_profile_h profile)
728 {
729         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
730
731         if (!(__connection_check_handle_validity(connection)) ||
732             !(_connection_libnet_check_profile_validity(profile))) {
733                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
734                 return CONNECTION_ERROR_INVALID_PARAMETER;
735         }
736
737         int rv = 0;
738
739         net_profile_info_t *profile_info = profile;
740
741         if (profile_info->profile_type != NET_DEVICE_CELLULAR) {
742                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
743                 return CONNECTION_ERROR_INVALID_PARAMETER;
744         }
745
746         rv = net_add_profile(profile_info->ProfileInfo.Pdp.ServiceType, (net_profile_info_t*)profile);
747         if (rv == NET_ERR_ACCESS_DENIED) {
748                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
749                 return CONNECTION_ERROR_PERMISSION_DENIED;
750         } else if (rv != NET_ERR_NONE) {
751                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to add profile[%d]", rv);
752                 return CONNECTION_ERROR_OPERATION_FAILED;
753         }
754
755         return CONNECTION_ERROR_NONE;
756 }
757
758 EXPORT_API int connection_remove_profile(connection_h connection, connection_profile_h profile)
759 {
760         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE);
761
762         if (!(__connection_check_handle_validity(connection)) ||
763             !(_connection_libnet_check_profile_validity(profile))) {
764                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
765                 return CONNECTION_ERROR_INVALID_PARAMETER;
766         }
767
768         int rv = 0;
769         net_profile_info_t *profile_info = profile;
770
771         if (profile_info->profile_type != NET_DEVICE_CELLULAR &&
772             profile_info->profile_type != NET_DEVICE_WIFI) {
773                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
774                 return CONNECTION_ERROR_INVALID_PARAMETER;
775         }
776
777         rv = net_delete_profile(profile_info->ProfileName);
778         if (rv == NET_ERR_ACCESS_DENIED) {
779                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
780                 return CONNECTION_ERROR_PERMISSION_DENIED;
781         } else if (rv != NET_ERR_NONE) {
782                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to delete profile[%d]", rv);
783                 return CONNECTION_ERROR_OPERATION_FAILED;
784         }
785
786         return CONNECTION_ERROR_NONE;
787 }
788
789 EXPORT_API int connection_update_profile(connection_h connection, connection_profile_h profile)
790 {
791         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, ETHERNET_FEATURE);
792
793         if (!(__connection_check_handle_validity(connection)) ||
794             !(_connection_libnet_check_profile_validity(profile))) {
795                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
796                 return CONNECTION_ERROR_INVALID_PARAMETER;
797         }
798
799         int rv = 0;
800         net_profile_info_t *profile_info = profile;
801
802         rv = net_modify_profile(profile_info->ProfileName, (net_profile_info_t*)profile);
803         if (rv == NET_ERR_ACCESS_DENIED) {
804                 CONNECTION_LOG(CONNECTION_ERROR, "Access denied");
805                 return CONNECTION_ERROR_PERMISSION_DENIED;
806         } else if (rv != NET_ERR_NONE) {
807                 CONNECTION_LOG(CONNECTION_ERROR, "Failed to modify profile[%d]", rv);
808                 return CONNECTION_ERROR_OPERATION_FAILED;
809         }
810
811         return CONNECTION_ERROR_NONE;
812 }
813
814 EXPORT_API int connection_get_profile_iterator(connection_h connection,
815                 connection_iterator_type_e type, connection_profile_iterator_h* profile_iterator)
816 {
817         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
818
819         if (!(__connection_check_handle_validity(connection)) ||
820             (type != CONNECTION_ITERATOR_TYPE_REGISTERED &&
821              type != CONNECTION_ITERATOR_TYPE_CONNECTED &&
822              type != CONNECTION_ITERATOR_TYPE_DEFAULT)) {
823                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
824                 return CONNECTION_ERROR_INVALID_PARAMETER;
825         }
826
827         return _connection_libnet_get_profile_iterator(type, profile_iterator);
828 }
829
830 EXPORT_API int connection_profile_iterator_next(connection_profile_iterator_h profile_iterator,
831                                                         connection_profile_h* profile)
832 {
833         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
834
835         return _connection_libnet_get_iterator_next(profile_iterator, profile);
836 }
837
838 EXPORT_API bool connection_profile_iterator_has_next(connection_profile_iterator_h profile_iterator)
839 {
840         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
841
842         return _connection_libnet_iterator_has_next(profile_iterator);
843 }
844
845 EXPORT_API int connection_destroy_profile_iterator(connection_profile_iterator_h profile_iterator)
846 {
847         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
848
849         return _connection_libnet_destroy_iterator(profile_iterator);
850 }
851
852 EXPORT_API int connection_get_current_profile(connection_h connection, connection_profile_h* profile)
853 {
854         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
855
856         if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
857                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
858                 return CONNECTION_ERROR_INVALID_PARAMETER;
859         }
860
861         return _connection_libnet_get_current_profile(profile);
862 }
863
864 EXPORT_API int connection_get_default_cellular_service_profile(connection_h connection,
865                 connection_cellular_service_type_e type, connection_profile_h* profile)
866 {
867         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
868
869         if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
870                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
871                 return CONNECTION_ERROR_INVALID_PARAMETER;
872         }
873
874         return _connection_libnet_get_cellular_service_profile(type, profile);
875 }
876
877 EXPORT_API int connection_set_default_cellular_service_profile(connection_h connection,
878                 connection_cellular_service_type_e type, connection_profile_h profile)
879 {
880         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
881
882         if (!(__connection_check_handle_validity(connection)) || profile == NULL) {
883                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
884                 return CONNECTION_ERROR_INVALID_PARAMETER;
885         }
886
887         return _connection_libnet_set_cellular_service_profile_sync(type, profile);
888 }
889
890 EXPORT_API int connection_set_default_cellular_service_profile_async(connection_h connection,
891                 connection_cellular_service_type_e type, connection_profile_h profile,
892                 connection_set_default_cb callback, void* user_data)
893 {
894         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
895
896         if (!(__connection_check_handle_validity(connection)) ||
897             profile == NULL || callback == NULL) {
898                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
899                 return CONNECTION_ERROR_INVALID_PARAMETER;
900         }
901
902         return _connection_libnet_set_cellular_service_profile_async(type, profile, callback, user_data);
903 }
904
905 EXPORT_API int connection_open_profile(connection_h connection, connection_profile_h profile,
906                                         connection_opened_cb callback, void* user_data)
907 {
908         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE);
909
910         if (!(__connection_check_handle_validity(connection)) ||
911             profile == NULL || callback == NULL) {
912                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
913                 return CONNECTION_ERROR_INVALID_PARAMETER;
914         }
915
916         return _connection_libnet_open_profile(profile, callback, user_data);
917 }
918
919 EXPORT_API int connection_close_profile(connection_h connection, connection_profile_h profile,
920                                         connection_closed_cb callback, void* user_data)
921 {
922         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE);
923
924         if (!(__connection_check_handle_validity(connection)) ||
925             profile == NULL || callback == NULL) {
926                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
927                 return CONNECTION_ERROR_INVALID_PARAMETER;
928         }
929
930         return _connection_libnet_close_profile(profile, callback, user_data);
931 }
932
933 EXPORT_API int connection_reset_profile(connection_h connection,
934                                 connection_reset_option_e type, int id, connection_reset_cb callback, void *user_data)
935 {
936         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
937
938         if (!(__connection_check_handle_validity(connection))) {
939                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed");
940                 return CONNECTION_ERROR_INVALID_PARAMETER;
941         }
942
943         if(id < 0 || id > 1) {
944                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed");
945                 return CONNECTION_ERROR_INVALID_PARAMETER;
946         }
947
948         return _connection_libnet_reset_profile(type, id, callback, user_data);
949 }
950
951 EXPORT_API int connection_add_route(connection_h connection, const char* interface_name, const char* host_address)
952 {
953         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
954
955         if (!(__connection_check_handle_validity(connection)) ||
956             interface_name == NULL || host_address == NULL) {
957                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
958                 return CONNECTION_ERROR_INVALID_PARAMETER;
959         }
960
961         return _connection_libnet_add_route(interface_name, host_address);
962 }
963
964 EXPORT_API int connection_remove_route(connection_h connection, const char* interface_name, const char* host_address)
965 {
966         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, TETHERING_BLUETOOTH_FEATURE, ETHERNET_FEATURE);
967
968         if (!(__connection_check_handle_validity(connection)) ||
969             interface_name == NULL || host_address == NULL) {
970                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
971                 return CONNECTION_ERROR_INVALID_PARAMETER;
972         }
973
974         return _connection_libnet_remove_route(interface_name, host_address);
975 }
976
977 EXPORT_API int connection_add_route_ipv6(connection_h connection, const char *interface_name, const char *host_address, const char * gateway)
978 {
979         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, ETHERNET_FEATURE);
980
981         if (!(__connection_check_handle_validity(connection)) ||
982             interface_name == NULL || host_address == NULL) {
983                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
984                 return CONNECTION_ERROR_INVALID_PARAMETER;
985         }
986
987         return _connection_libnet_add_route_ipv6(interface_name, host_address, gateway);
988 }
989
990 EXPORT_API int connection_remove_route_ipv6(connection_h connection, const char *interface_name, const char *host_address, const char * gateway)
991 {
992         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE, ETHERNET_FEATURE);
993
994         if (!(__connection_check_handle_validity(connection)) ||
995             interface_name == NULL || host_address == NULL) {
996                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
997                 return CONNECTION_ERROR_INVALID_PARAMETER;
998         }
999
1000         return _connection_libnet_remove_route_ipv6(interface_name, host_address, gateway);
1001 }
1002
1003 /* Connection Statistics module ******************************************************************/
1004
1005 static int __get_statistic(connection_type_e connection_type,
1006                         connection_statistics_type_e statistics_type, long long* llsize)
1007 {
1008         int rv, size;
1009         unsigned long long ull_size;
1010         int stat_type;
1011         char *key = NULL;
1012
1013         if (llsize == NULL) {
1014                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
1015                 return CONNECTION_ERROR_INVALID_PARAMETER;
1016         }
1017
1018         if (connection_type == CONNECTION_TYPE_CELLULAR) {
1019                 switch (statistics_type) {
1020                 case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
1021                         key = VCONFKEY_NETWORK_CELLULAR_PKT_LAST_SNT;
1022                         break;
1023                 case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
1024                         key = VCONFKEY_NETWORK_CELLULAR_PKT_LAST_RCV;
1025                         break;
1026                 case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
1027                         key = VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_SNT;
1028                         break;
1029                 case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
1030                         key = VCONFKEY_NETWORK_CELLULAR_PKT_TOTAL_RCV;
1031                         break;
1032                 default:
1033                         return CONNECTION_ERROR_INVALID_PARAMETER;
1034                 }
1035
1036                 if (vconf_get_int(key, &size)) {
1037                         CONNECTION_LOG(CONNECTION_ERROR, "Cannot Get %s = %d\n", key, size);
1038                         *llsize = 0;
1039                         return CONNECTION_ERROR_OPERATION_FAILED;
1040                 }
1041
1042                 CONNECTION_LOG(CONNECTION_INFO,"%s:%d bytes\n", key, size);
1043                 *llsize = (long long)size;
1044         } else if (connection_type == CONNECTION_TYPE_WIFI) {
1045                 switch (statistics_type) {
1046                 case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
1047                         stat_type = NET_STATISTICS_TYPE_LAST_SENT_DATA;
1048                         break;
1049                 case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
1050                         stat_type = NET_STATISTICS_TYPE_LAST_RECEIVED_DATA;
1051                         break;
1052                 case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
1053                         stat_type = NET_STATISTICS_TYPE_TOTAL_SENT_DATA;
1054                         break;
1055                 case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
1056                         stat_type = NET_STATISTICS_TYPE_TOTAL_RECEIVED_DATA;
1057                         break;
1058                 default:
1059                         return CONNECTION_ERROR_INVALID_PARAMETER;
1060                 }
1061
1062                 rv  = _connection_libnet_get_statistics(stat_type, &ull_size);
1063                 if (rv == CONNECTION_ERROR_PERMISSION_DENIED)
1064                         return rv;
1065                 else if (rv != CONNECTION_ERROR_NONE) {
1066                         CONNECTION_LOG(CONNECTION_ERROR, "Failed to get Wi-Fi statistics");
1067                         *llsize = 0;
1068                         return CONNECTION_ERROR_OPERATION_FAILED;
1069                 }
1070
1071                 CONNECTION_LOG(CONNECTION_INFO,"%d bytes\n", ull_size);
1072                 *llsize = (long long)ull_size;
1073         } else
1074                 return CONNECTION_ERROR_INVALID_PARAMETER;
1075
1076         return CONNECTION_ERROR_NONE;
1077 }
1078
1079 static int __reset_statistic(connection_type_e connection_type,
1080                         connection_statistics_type_e statistics_type)
1081 {
1082         int conn_type;
1083         int stat_type;
1084         int rv;
1085
1086         if (connection_type == CONNECTION_TYPE_CELLULAR)
1087                 conn_type = NET_DEVICE_CELLULAR;
1088         else if (connection_type == CONNECTION_TYPE_WIFI)
1089                 conn_type = NET_DEVICE_WIFI;
1090         else
1091                 return CONNECTION_ERROR_INVALID_PARAMETER;
1092
1093         switch (statistics_type) {
1094         case CONNECTION_STATISTICS_TYPE_LAST_SENT_DATA:
1095                 stat_type = NET_STATISTICS_TYPE_LAST_SENT_DATA;
1096                 break;
1097         case CONNECTION_STATISTICS_TYPE_LAST_RECEIVED_DATA:
1098                 stat_type = NET_STATISTICS_TYPE_LAST_RECEIVED_DATA;
1099                 break;
1100         case CONNECTION_STATISTICS_TYPE_TOTAL_SENT_DATA:
1101                 stat_type = NET_STATISTICS_TYPE_TOTAL_SENT_DATA;
1102                 break;
1103         case CONNECTION_STATISTICS_TYPE_TOTAL_RECEIVED_DATA:
1104                 stat_type = NET_STATISTICS_TYPE_TOTAL_RECEIVED_DATA;
1105                 break;
1106         default:
1107                 return CONNECTION_ERROR_INVALID_PARAMETER;
1108         }
1109
1110         rv = _connection_libnet_set_statistics(conn_type, stat_type);
1111         if(rv != CONNECTION_ERROR_NONE)
1112                 return rv;
1113
1114
1115         CONNECTION_LOG(CONNECTION_INFO,"connection_reset_statistics success\n");
1116
1117         return CONNECTION_ERROR_NONE;
1118 }
1119
1120 EXPORT_API int connection_get_statistics(connection_h connection,
1121                                 connection_type_e connection_type,
1122                                 connection_statistics_type_e statistics_type, long long* size)
1123 {
1124         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE);
1125
1126         if(connection_type == CONNECTION_TYPE_CELLULAR )
1127                 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
1128         else if(connection_type == CONNECTION_TYPE_WIFI)
1129                 CHECK_FEATURE_SUPPORTED(WIFI_FEATURE);
1130
1131         if (!(__connection_check_handle_validity(connection)) || size == NULL) {
1132                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid parameter");
1133                 return CONNECTION_ERROR_INVALID_PARAMETER;
1134         }
1135
1136         return __get_statistic(connection_type, statistics_type, size);
1137 }
1138
1139 EXPORT_API int connection_reset_statistics(connection_h connection,
1140                                 connection_type_e connection_type,
1141                                 connection_statistics_type_e statistics_type)
1142 {
1143         CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE, WIFI_FEATURE);
1144
1145         if(connection_type == CONNECTION_TYPE_CELLULAR )
1146                 CHECK_FEATURE_SUPPORTED(TELEPHONY_FEATURE);
1147         else if(connection_type == CONNECTION_TYPE_WIFI)
1148                 CHECK_FEATURE_SUPPORTED(WIFI_FEATURE);
1149
1150         if (!(__connection_check_handle_validity(connection))) {
1151                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed");
1152                 return CONNECTION_ERROR_INVALID_PARAMETER;
1153         }
1154
1155         return __reset_statistic(connection_type, statistics_type);
1156 }