Merge 2.3 code for sync
[platform/core/api/connection.git] / src / libnetwork.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 *prof_handle_list = NULL;
24 static GHashTable *profile_cb_table = NULL;
25
26 struct _profile_cb_s {
27         connection_profile_state_changed_cb callback;
28         connection_profile_state_e state;
29         void *user_data;
30 };
31
32 struct _profile_list_s {
33         int count;
34         int next;
35         net_profile_info_t *profiles;
36 };
37
38 struct _libnet_s {
39         connection_opened_cb opened_cb;
40         connection_closed_cb closed_cb;
41         connection_set_default_cb set_default_cb;
42         void *opened_user_data;
43         void *closed_user_data;
44         void *set_default_user_data;
45         bool registered;
46 };
47
48 static struct _profile_list_s profile_iterator = {0, 0, NULL};
49 static struct _libnet_s libnet = {NULL, NULL, NULL, NULL, NULL, NULL, false};
50
51 static connection_error_e __libnet_convert_to_cp_error_type(net_err_t err_type)
52 {
53         switch (err_type) {
54         case NET_ERR_NONE:
55                 return CONNECTION_ERROR_NONE;
56         case NET_ERR_APP_ALREADY_REGISTERED:
57                 return CONNECTION_ERROR_INVALID_OPERATION;
58         case NET_ERR_APP_NOT_REGISTERED:
59                 return CONNECTION_ERROR_INVALID_OPERATION;
60         case NET_ERR_NO_ACTIVE_CONNECTIONS:
61                 return CONNECTION_ERROR_NO_CONNECTION;
62         case NET_ERR_ACTIVE_CONNECTION_EXISTS:
63                 return CONNECTION_ERROR_ALREADY_EXISTS;
64         case NET_ERR_CONNECTION_DHCP_FAILED:
65                 return CONNECTION_ERROR_DHCP_FAILED;
66         case NET_ERR_CONNECTION_INVALID_KEY:
67                 return CONNECTION_ERROR_INVALID_KEY;
68         case NET_ERR_IN_PROGRESS:
69                 return CONNECTION_ERROR_NOW_IN_PROGRESS;
70         case NET_ERR_OPERATION_ABORTED:
71                 return CONNECTION_ERROR_OPERATION_ABORTED;
72         case NET_ERR_TIME_OUT:
73                 return CONNECTION_ERROR_NO_REPLY;
74         default:
75                 return CONNECTION_ERROR_OPERATION_FAILED;
76         }
77 }
78
79 static const char *__libnet_convert_cp_error_type_to_string(connection_error_e err_type)
80 {
81         switch (err_type) {
82         case CONNECTION_ERROR_NONE:
83                 return "NONE";
84         case CONNECTION_ERROR_INVALID_PARAMETER:
85                 return "INVALID_PARAMETER";
86         case CONNECTION_ERROR_OUT_OF_MEMORY:
87                 return "OUT_OF_MEMORY";
88         case CONNECTION_ERROR_INVALID_OPERATION:
89                 return "INVALID_OPERATION";
90         case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
91                 return "ADDRESS_FAMILY_NOT_SUPPORTED";
92         case CONNECTION_ERROR_OPERATION_FAILED:
93                 return "OPERATION_FAILED";
94         case CONNECTION_ERROR_ITERATOR_END:
95                 return "ITERATOR_END";
96         case CONNECTION_ERROR_NO_CONNECTION:
97                 return "NO_CONNECTION";
98         case CONNECTION_ERROR_NOW_IN_PROGRESS:
99                 return "NOW_IN_PROGRESS";
100         case CONNECTION_ERROR_ALREADY_EXISTS:
101                 return "ALREADY_EXISTS";
102         case CONNECTION_ERROR_OPERATION_ABORTED:
103                 return "OPERATION_ABORTED";
104         case CONNECTION_ERROR_DHCP_FAILED:
105                 return "DHCP_FAILED";
106         case CONNECTION_ERROR_INVALID_KEY:
107                 return "INVALID_KEY";
108         case CONNECTION_ERROR_NO_REPLY:
109                 return "NO_REPLY";
110         case CONNECTION_ERROR_PERMISSION_DENIED:
111                 return "PERMISSION_DENIED";
112         case CONNECTION_ERROR_NOT_SUPPORTED:
113                 return "NOT_SUPPORTED";
114         }
115
116         return "UNKNOWN";
117 }
118
119 static const char *__libnet_convert_cp_state_to_string(connection_profile_state_e state)
120 {
121         switch (state) {
122         case CONNECTION_PROFILE_STATE_DISCONNECTED:
123                 return "DISCONNECTED";
124         case CONNECTION_PROFILE_STATE_ASSOCIATION:
125                 return "ASSOCIATION";
126         case CONNECTION_PROFILE_STATE_CONFIGURATION:
127                 return "CONFIGURATION";
128         case CONNECTION_PROFILE_STATE_CONNECTED:
129                 return "CONNECTED";
130         default:
131                 return "UNKNOWN";
132         }
133 }
134
135 static void __libnet_set_opened_cb(connection_opened_cb user_cb, void *user_data)
136 {
137         if (user_cb) {
138                 libnet.opened_cb = user_cb;
139                 libnet.opened_user_data = user_data;
140         }
141 }
142
143 static void __libnet_opened_cb(connection_error_e result)
144 {
145         if (libnet.opened_cb)
146                 libnet.opened_cb(result, libnet.opened_user_data);
147
148         libnet.opened_cb = NULL;
149         libnet.opened_user_data = NULL;
150 }
151
152 static void __libnet_set_closed_cb(connection_closed_cb user_cb, void *user_data)
153 {
154         if (user_cb) {
155                 libnet.closed_cb = user_cb;
156                 libnet.closed_user_data = user_data;
157         }
158 }
159
160 static void __libnet_closed_cb(connection_error_e result)
161 {
162         if (libnet.closed_cb)
163                 libnet.closed_cb(result, libnet.closed_user_data);
164
165         libnet.closed_cb = NULL;
166         libnet.closed_user_data = NULL;
167 }
168
169 static void __libnet_set_default_cb(connection_set_default_cb user_cb, void *user_data)
170 {
171         if (user_cb) {
172                 libnet.set_default_cb = user_cb;
173                 libnet.set_default_user_data = user_data;
174         }
175 }
176
177 static void __libnet_default_cb(connection_error_e result)
178 {
179         if (libnet.set_default_cb)
180                 libnet.set_default_cb(result, libnet.set_default_user_data);
181
182         libnet.set_default_cb = NULL;
183         libnet.set_default_user_data = NULL;
184 }
185
186 static void __libnet_state_changed_cb(char *profile_name, connection_profile_state_e state)
187 {
188         if (profile_name == NULL)
189                 return;
190
191         struct _profile_cb_s *cb_info;
192         cb_info = g_hash_table_lookup(profile_cb_table, profile_name);
193
194         if (cb_info == NULL)
195                 return;
196
197         if (cb_info->state == state)
198                 return;
199
200         cb_info->state = state;
201
202         if (state >= 0 && cb_info->callback)
203                 cb_info->callback(state, cb_info->user_data);
204 }
205
206 static void __libnet_clear_profile_list(struct _profile_list_s *profile_list)
207 {
208         if (profile_list->count > 0)
209                 g_free(profile_list->profiles);
210
211         profile_list->count = 0;
212         profile_list->next = 0;
213         profile_list->profiles = NULL;
214 }
215
216 static void __libnet_evt_cb(net_event_info_t*  event_cb, void* user_data)
217 {
218         bool is_requested = false;
219         connection_error_e result = CONNECTION_ERROR_NONE;
220
221         switch (event_cb->Event) {
222         case NET_EVENT_OPEN_RSP:
223                 is_requested = true;
224                 /* fall through */
225         case NET_EVENT_OPEN_IND:
226                 result = __libnet_convert_to_cp_error_type(event_cb->Error);
227                 CONNECTION_LOG(CONNECTION_INFO, "Got connection open %s : %s\n",
228                                         (is_requested) ? "RSP":"IND",
229                                         __libnet_convert_cp_error_type_to_string(result));
230
231                 if (is_requested)
232                         __libnet_opened_cb(result);
233
234                 switch (event_cb->Error) {
235                 case NET_ERR_NONE:
236                 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
237                         CONNECTION_LOG(CONNECTION_INFO, "'Open connection' succeeded\n");
238
239                         __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_CONNECTED);
240                         return;
241                 default:
242                         CONNECTION_LOG(CONNECTION_ERROR, "'Open connection' failed!! [%s]\n",
243                                                 __libnet_convert_cp_error_type_to_string(result));
244                 }
245
246                 __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_DISCONNECTED);
247
248                 break;
249         case NET_EVENT_CLOSE_RSP:
250                 is_requested = true;
251                 /* fall through */
252         case NET_EVENT_CLOSE_IND:
253                 result = __libnet_convert_to_cp_error_type(event_cb->Error);
254                 CONNECTION_LOG(CONNECTION_INFO, "Got connection close %s : %s\n",
255                                         (is_requested) ? "RSP":"IND",
256                                         __libnet_convert_cp_error_type_to_string(result));
257
258                 if (is_requested)
259                         __libnet_closed_cb(result);
260
261                 switch (event_cb->Error) {
262                 case NET_ERR_NONE:
263                         CONNECTION_LOG(CONNECTION_INFO, "'Close connection' succeeded!\n");
264
265                         __libnet_state_changed_cb(event_cb->ProfileName, CONNECTION_PROFILE_STATE_DISCONNECTED);
266                         return;
267                 default:
268                         CONNECTION_LOG(CONNECTION_ERROR, "'Close connection' failed!! [%s]\n",
269                                                 __libnet_convert_cp_error_type_to_string(result));
270                 }
271
272                 break;
273         case NET_EVENT_NET_STATE_IND:
274                 CONNECTION_LOG(CONNECTION_INFO, "Got State changed IND\n");
275
276                 if (event_cb->Datalength != sizeof(net_state_type_t))
277                         return;
278
279                 net_state_type_t *profile_state = (net_state_type_t*)event_cb->Data;
280                 connection_profile_state_e cp_state = _profile_convert_to_cp_state(*profile_state);
281
282                 CONNECTION_LOG(CONNECTION_INFO,
283                                 "Profile State : %s, profile name : %s\n",
284                                 __libnet_convert_cp_state_to_string(cp_state),
285                                 event_cb->ProfileName);
286
287                 __libnet_state_changed_cb(event_cb->ProfileName, cp_state);
288
289                 break;
290         case NET_EVENT_WIFI_SCAN_IND:
291         case NET_EVENT_WIFI_SCAN_RSP:
292                 CONNECTION_LOG(CONNECTION_INFO, "Got wifi scan IND\n");
293                 break;
294         case NET_EVENT_WIFI_POWER_IND:
295         case NET_EVENT_WIFI_POWER_RSP:
296                 CONNECTION_LOG(CONNECTION_INFO, "Got wifi power IND\n");
297                 break;
298         case NET_EVENT_CELLULAR_SET_DEFAULT_RSP:
299                 result = __libnet_convert_to_cp_error_type(event_cb->Error);
300                 CONNECTION_LOG(CONNECTION_INFO, "Got set default profile RSP %d\n", result);
301                 __libnet_default_cb(result);
302                 break;
303         case NET_EVENT_WIFI_WPS_RSP:
304                 CONNECTION_LOG(CONNECTION_INFO, "Got wifi WPS RSP\n");
305                 /* fall through */
306         default :
307                 CONNECTION_LOG(CONNECTION_ERROR, "Error! Unknown Event\n\n");
308                 break;
309         }
310 }
311
312 int __libnet_get_connected_count(struct _profile_list_s *profile_list)
313 {
314         int count = 0;
315         int i = 0;
316
317         for (;i < profile_list->count;i++) {
318                 if (profile_list->profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
319                     profile_list->profiles[i].ProfileState == NET_STATE_TYPE_READY)
320                         count++;
321         }
322
323         return count;
324 }
325
326 void __libnet_copy_connected_profile(net_profile_info_t **dest, struct _profile_list_s *source)
327 {
328         int i = 0;
329
330         for (;i < source->count;i++) {
331                 if (source->profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
332                     source->profiles[i].ProfileState == NET_STATE_TYPE_READY) {
333                         memcpy(*dest, &source->profiles[i], sizeof(net_profile_info_t));
334                         (*dest)++;
335                 }
336         }
337 }
338
339 bool _connection_libnet_init(void)
340 {
341         int rv;
342
343         if (!libnet.registered) {
344                 rv = net_register_client_ext((net_event_cb_t)__libnet_evt_cb, NET_DEVICE_DEFAULT, NULL);
345                 if (rv != NET_ERR_NONE)
346                         return false;
347
348                 libnet.registered = true;
349
350                 if (profile_cb_table == NULL)
351                         profile_cb_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
352         }
353
354         return true;
355 }
356
357 bool _connection_libnet_deinit(void)
358 {
359         if (libnet.registered) {
360                 if (net_deregister_client_ext(NET_DEVICE_DEFAULT) != NET_ERR_NONE)
361                         return false;
362
363                 libnet.registered = false;
364
365                 if (profile_cb_table) {
366                         g_hash_table_destroy(profile_cb_table);
367                         profile_cb_table = NULL;
368                 }
369
370                 __libnet_clear_profile_list(&profile_iterator);
371
372                 if (prof_handle_list) {
373                         g_slist_free_full(prof_handle_list, g_free);
374                         prof_handle_list = NULL;
375                 }
376         }
377
378         return true;
379 }
380
381 bool _connection_libnet_check_profile_validity(connection_profile_h profile)
382 {
383         GSList *list;
384         int i = 0;
385
386         for (list = prof_handle_list; list; list = list->next)
387                 if (profile == list->data) return true;
388
389         for (;i < profile_iterator.count;i++)
390                 if (profile == &profile_iterator.profiles[i]) return true;
391
392         return false;
393 }
394
395 bool _connection_libnet_check_profile_cb_validity(connection_profile_h profile)
396 {
397         struct _profile_cb_s *cb_info;
398         net_profile_info_t *profile_info = profile;
399
400         if (profile == NULL)
401                 return false;
402
403         cb_info = g_hash_table_lookup(profile_cb_table, profile_info->ProfileName);
404         if (cb_info != NULL)
405                 return true;
406
407         return false;
408 }
409
410
411 int _connection_libnet_get_wifi_state(connection_wifi_state_e *state)
412 {
413         int rv;
414         net_wifi_state_t wlan_state;
415         net_profile_name_t profile_name;
416
417         rv = net_get_wifi_state(&wlan_state, &profile_name);
418         if (rv != NET_ERR_NONE) {
419                 CONNECTION_LOG(CONNECTION_ERROR, "Error!! net_get_wifi_state() failed.\n");
420                 return CONNECTION_ERROR_OPERATION_FAILED;
421         }
422
423         switch (wlan_state) {
424         case WIFI_OFF:
425                 *state = CONNECTION_WIFI_STATE_DEACTIVATED;
426                 break;
427         case WIFI_ON:
428         case WIFI_CONNECTING:
429                 *state = CONNECTION_WIFI_STATE_DISCONNECTED;
430                 break;
431         case WIFI_CONNECTED:
432         case WIFI_DISCONNECTING:
433                 *state = CONNECTION_WIFI_STATE_CONNECTED;
434                 break;
435         default :
436                 CONNECTION_LOG(CONNECTION_ERROR, "Error!! Unknown state\n");
437                 return CONNECTION_ERROR_INVALID_OPERATION;
438         }
439
440         return CONNECTION_ERROR_NONE;
441 }
442
443 int _connection_libnet_get_ethernet_state(connection_ethernet_state_e* state)
444 {
445         struct _profile_list_s ethernet_profiles = {0, 0, NULL};
446         net_get_profile_list(NET_DEVICE_ETHERNET, &ethernet_profiles.profiles, &ethernet_profiles.count);
447
448         if (ethernet_profiles.count == 0) {
449                 *state = CONNECTION_ETHERNET_STATE_DEACTIVATED;
450                 return CONNECTION_ERROR_NONE;
451         }
452
453         switch (ethernet_profiles.profiles->ProfileState) {
454         case NET_STATE_TYPE_ONLINE:
455         case NET_STATE_TYPE_READY:
456                 *state = CONNECTION_ETHERNET_STATE_CONNECTED;
457                 break;
458         case NET_STATE_TYPE_IDLE:
459         case NET_STATE_TYPE_FAILURE:
460         case NET_STATE_TYPE_ASSOCIATION:
461         case NET_STATE_TYPE_CONFIGURATION:
462         case NET_STATE_TYPE_DISCONNECT:
463                 *state = CONNECTION_ETHERNET_STATE_DISCONNECTED;
464                 break;
465         default:
466                 return CONNECTION_ERROR_OPERATION_FAILED;
467         }
468
469         __libnet_clear_profile_list(&ethernet_profiles);
470
471         return CONNECTION_ERROR_NONE;
472 }
473
474 int _connection_libnet_get_bluetooth_state(connection_bt_state_e* state)
475 {
476         int i = 0;
477         struct _profile_list_s bluetooth_profiles = {0, 0, NULL};
478         net_get_profile_list(NET_DEVICE_BLUETOOTH, &bluetooth_profiles.profiles, &bluetooth_profiles.count);
479
480         if (bluetooth_profiles.count == 0) {
481                 *state = CONNECTION_BT_STATE_DEACTIVATED;
482                 return CONNECTION_ERROR_NONE;
483         }
484
485         for (; i < bluetooth_profiles.count; i++) {
486                 switch (bluetooth_profiles.profiles[i].ProfileState) {
487                 case NET_STATE_TYPE_ONLINE:
488                 case NET_STATE_TYPE_READY:
489                         *state = CONNECTION_BT_STATE_CONNECTED;
490                         goto done;
491                 case NET_STATE_TYPE_IDLE:
492                 case NET_STATE_TYPE_FAILURE:
493                 case NET_STATE_TYPE_ASSOCIATION:
494                 case NET_STATE_TYPE_CONFIGURATION:
495                 case NET_STATE_TYPE_DISCONNECT:
496                         *state = CONNECTION_BT_STATE_DISCONNECTED;
497                         break;
498                 default:
499                         __libnet_clear_profile_list(&bluetooth_profiles);
500                         return CONNECTION_ERROR_OPERATION_FAILED;
501                 }
502         }
503
504 done:
505         __libnet_clear_profile_list(&bluetooth_profiles);
506
507         return CONNECTION_ERROR_NONE;
508 }
509
510 int _connection_libnet_get_profile_iterator(connection_iterator_type_e type, connection_profile_iterator_h* profile_iter_h)
511 {
512         int count = 0;
513         int rv;
514         net_profile_info_t *profiles = NULL;
515
516         struct _profile_list_s all_profiles = {0, 0, NULL};
517
518         __libnet_clear_profile_list(&profile_iterator);
519
520         rv = net_get_profile_list(NET_DEVICE_MAX, &all_profiles.profiles, &all_profiles.count);
521
522         if (rv != NET_ERR_NONE) {
523                 if (rv == NET_ERR_NO_SERVICE) {
524                         *profile_iter_h = &profile_iterator;
525                         return CONNECTION_ERROR_NONE;
526                 } else
527                         return CONNECTION_ERROR_OPERATION_FAILED;
528         }
529
530         *profile_iter_h = &profile_iterator;
531
532         switch (type) {
533         case CONNECTION_ITERATOR_TYPE_REGISTERED:
534                 count = all_profiles.count;
535                 CONNECTION_LOG(CONNECTION_INFO, "Total profile count : %d\n", count);
536
537                 if (count == 0)
538                         return CONNECTION_ERROR_NONE;
539
540                 profile_iterator.profiles = all_profiles.profiles;
541
542                 break;
543         case CONNECTION_ITERATOR_TYPE_CONNECTED:
544                 count = __libnet_get_connected_count(&all_profiles);
545                 CONNECTION_LOG(CONNECTION_INFO, "Total connected profile count : %d\n", count);
546
547                 if (count == 0)
548                         return CONNECTION_ERROR_NONE;
549
550                 profiles = g_try_new0(net_profile_info_t, count);
551                 if (profiles == NULL) {
552                         __libnet_clear_profile_list(&all_profiles);
553                         return CONNECTION_ERROR_OUT_OF_MEMORY;
554                 break;
555         case CONNECTION_ITERATOR_TYPE_DEFAULT:
556                         /* To do : Not supported yet */
557                 break;
558                 }
559
560                 profile_iterator.profiles = profiles;
561
562                 __libnet_copy_connected_profile(&profiles, &all_profiles);
563
564                 __libnet_clear_profile_list(&all_profiles);
565         }
566
567         profile_iterator.count = count;
568
569         return CONNECTION_ERROR_NONE;
570 }
571
572 int _connection_libnet_get_iterator_next(connection_profile_iterator_h profile_iter_h, connection_profile_h *profile)
573 {
574         if (profile_iter_h != &profile_iterator)
575                 return CONNECTION_ERROR_INVALID_PARAMETER;
576
577         if (profile_iterator.count <= profile_iterator.next)
578                 return CONNECTION_ERROR_ITERATOR_END;
579
580         *profile = &profile_iterator.profiles[profile_iterator.next];
581         profile_iterator.next++;
582
583         return CONNECTION_ERROR_NONE;
584 }
585
586 bool _connection_libnet_iterator_has_next(connection_profile_iterator_h profile_iter_h)
587 {
588         if (profile_iter_h != &profile_iterator)
589                 return false;
590
591         if (profile_iterator.count <= profile_iterator.next)
592                 return false;
593
594         return true;
595 }
596
597 int _connection_libnet_destroy_iterator(connection_profile_iterator_h profile_iter_h)
598 {
599         if (profile_iter_h != &profile_iterator)
600                 return CONNECTION_ERROR_INVALID_PARAMETER;
601
602         __libnet_clear_profile_list(&profile_iterator);
603
604         return CONNECTION_ERROR_NONE;
605 }
606
607 int _connection_libnet_get_current_profile(connection_profile_h *profile)
608 {
609         net_profile_info_t active_profile;
610         int rv;
611
612         rv = net_get_active_net_info(&active_profile);
613         if (rv == NET_ERR_NO_SERVICE)
614                 return CONNECTION_ERROR_NO_CONNECTION;
615         else if (rv != NET_ERR_NONE)
616                 return CONNECTION_ERROR_OPERATION_FAILED;
617
618         *profile = g_try_malloc0(sizeof(net_profile_info_t));
619         if (*profile == NULL)
620                 return CONNECTION_ERROR_OUT_OF_MEMORY;
621
622         memcpy(*profile, &active_profile, sizeof(net_profile_info_t));
623         prof_handle_list = g_slist_append(prof_handle_list, *profile);
624
625         return CONNECTION_ERROR_NONE;
626 }
627
628 int _connection_libnet_open_profile(connection_profile_h profile, connection_opened_cb callback, void* user_data)
629 {
630         if (!(_connection_libnet_check_profile_validity(profile))) {
631                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
632                 return CONNECTION_ERROR_INVALID_PARAMETER;
633         }
634
635         net_profile_info_t *profile_info = profile;
636
637         if (net_open_connection_with_profile(profile_info->ProfileName) != NET_ERR_NONE)
638                 return CONNECTION_ERROR_OPERATION_FAILED;
639
640         __libnet_set_opened_cb(callback, user_data);
641
642         return CONNECTION_ERROR_NONE;
643 }
644
645 int _connection_libnet_get_cellular_service_profile(connection_cellular_service_type_e type, connection_profile_h *profile)
646 {
647         int i = 0;
648         int j = 0;
649         int rv = NET_ERR_NONE;
650         net_service_type_t service_type = _connection_profile_convert_to_libnet_cellular_service_type(type);
651
652         struct _profile_list_s cellular_profiles = {0, 0, NULL};
653
654         rv = net_get_profile_list(NET_DEVICE_CELLULAR, &cellular_profiles.profiles, &cellular_profiles.count);
655         if (rv != NET_ERR_NONE)
656                 return CONNECTION_ERROR_OPERATION_FAILED;
657
658         for (;i < cellular_profiles.count;i++)
659                 if (cellular_profiles.profiles[i].ProfileInfo.Pdp.ServiceType == service_type)
660                         break;
661
662         if (i >= cellular_profiles.count)
663                 return CONNECTION_ERROR_OPERATION_FAILED;
664
665         *profile = g_try_malloc0(sizeof(net_profile_info_t));
666         if (*profile == NULL)
667                 return CONNECTION_ERROR_OUT_OF_MEMORY;
668
669         memcpy(*profile, &cellular_profiles.profiles[i], sizeof(net_profile_info_t));
670
671         if (cellular_profiles.profiles[i].ProfileInfo.Pdp.DefaultConn)
672                 goto done;
673
674         if (type != CONNECTION_CELLULAR_SERVICE_TYPE_INTERNET &&
675             type != CONNECTION_CELLULAR_SERVICE_TYPE_PREPAID_INTERNET)
676                 goto done;
677
678         for (;j < cellular_profiles.count;j++) {
679                 if (i == j)
680                         continue;
681
682                 if (cellular_profiles.profiles[j].ProfileInfo.Pdp.ServiceType != service_type)
683                         continue;
684
685                 if (cellular_profiles.profiles[j].ProfileInfo.Pdp.DefaultConn) {
686                         memcpy(*profile, &cellular_profiles.profiles[j], sizeof(net_profile_info_t));
687                         goto done;
688                 }
689         }
690
691 done:
692         prof_handle_list = g_slist_append(prof_handle_list, *profile);
693
694         return CONNECTION_ERROR_NONE;
695 }
696
697 int _connection_libnet_set_cellular_service_profile_sync(connection_cellular_service_type_e type, connection_profile_h profile)
698 {
699         if (!(_connection_libnet_check_profile_validity(profile))) {
700                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
701                 return CONNECTION_ERROR_INVALID_PARAMETER;
702         }
703
704         net_profile_info_t *profile_info = profile;
705         connection_cellular_service_type_e service_type;
706
707         service_type = _profile_convert_to_connection_cellular_service_type(profile_info->ProfileInfo.Pdp.ServiceType);
708
709         if (service_type != type)
710                 return CONNECTION_ERROR_INVALID_PARAMETER;
711
712         if (net_set_default_cellular_service_profile(profile_info->ProfileName) != NET_ERR_NONE)
713                 return CONNECTION_ERROR_OPERATION_FAILED;
714
715         return CONNECTION_ERROR_NONE;
716 }
717
718 int _connection_libnet_set_cellular_service_profile_async(connection_cellular_service_type_e type,
719                         connection_profile_h profile, connection_set_default_cb callback, void* user_data)
720 {
721         if (!(_connection_libnet_check_profile_validity(profile))) {
722                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
723                 return CONNECTION_ERROR_INVALID_PARAMETER;
724         }
725
726         net_profile_info_t *profile_info = profile;
727         connection_cellular_service_type_e service_type;
728
729         service_type = _profile_convert_to_connection_cellular_service_type(profile_info->ProfileInfo.Pdp.ServiceType);
730
731         if (service_type != type)
732                 return CONNECTION_ERROR_INVALID_PARAMETER;
733
734         if (net_set_default_cellular_service_profile_async(profile_info->ProfileName) != NET_ERR_NONE)
735                 return CONNECTION_ERROR_OPERATION_FAILED;
736
737         __libnet_set_default_cb(callback, user_data);
738
739         return CONNECTION_ERROR_NONE;
740 }
741
742 int _connection_libnet_close_profile(connection_profile_h profile, connection_closed_cb callback, void *user_data)
743 {
744         if (!(_connection_libnet_check_profile_validity(profile))) {
745                 CONNECTION_LOG(CONNECTION_ERROR, "Wrong Parameter Passed\n");
746                 return CONNECTION_ERROR_INVALID_PARAMETER;
747         }
748
749         net_profile_info_t *profile_info = profile;
750
751         if (net_close_connection(profile_info->ProfileName) != NET_ERR_NONE)
752                 return CONNECTION_ERROR_OPERATION_FAILED;
753
754         __libnet_set_closed_cb(callback, user_data);
755
756         return CONNECTION_ERROR_NONE;
757 }
758
759 int _connection_libnet_add_route(const char *interface_name, const char *host_address)
760 {
761         char *endstr = strrchr(host_address, '.');
762
763         if (endstr == NULL ||
764             strcmp(endstr, ".0") == 0 ||
765             strncmp(host_address, "0.", 2) == 0 ||
766             strstr(host_address, ".0.") != NULL ||
767             strstr(host_address, "255") != NULL) {
768                 CONNECTION_LOG(CONNECTION_ERROR, "Invalid IP address Passed\n");
769                 return CONNECTION_ERROR_INVALID_PARAMETER;
770         }
771
772         if (net_add_route(host_address, interface_name) != NET_ERR_NONE)
773                 return CONNECTION_ERROR_OPERATION_FAILED;
774
775         return CONNECTION_ERROR_NONE;
776 }
777
778 void _connection_libnet_add_to_profile_list(connection_profile_h profile)
779 {
780         prof_handle_list = g_slist_append(prof_handle_list, profile);
781 }
782
783 void _connection_libnet_remove_from_profile_list(connection_profile_h profile)
784 {
785         prof_handle_list = g_slist_remove(prof_handle_list, profile);
786         g_free(profile);
787 }
788
789 bool _connection_libnet_add_to_profile_cb_list(connection_profile_h profile,
790                 connection_profile_state_changed_cb callback, void *user_data)
791 {
792         net_profile_info_t *profile_info = profile;
793         char *profile_name = g_strdup(profile_info->ProfileName);
794
795         struct _profile_cb_s *profile_cb_info = g_try_malloc0(sizeof(struct _profile_cb_s));
796         if (profile_cb_info == NULL) {
797                 g_free(profile_name);
798                 return false;
799         }
800
801         profile_cb_info->callback = callback;
802         profile_cb_info->user_data = user_data;
803
804         g_hash_table_insert(profile_cb_table, profile_name, profile_cb_info);
805
806         return true;
807 }
808
809 bool _connection_libnet_remove_from_profile_cb_list(connection_profile_h profile)
810 {
811         net_profile_info_t *profile_info = profile;
812         if (g_hash_table_remove(profile_cb_table, profile_info->ProfileName) == TRUE)
813                 return true;
814
815         return false;
816 }
817
818 int _connection_libnet_set_statistics(net_device_t device_type, net_statistics_type_e statistics_type)
819 {
820         if (net_set_statistics(device_type, statistics_type) != NET_ERR_NONE)
821                 return CONNECTION_ERROR_OPERATION_FAILED;
822
823         return CONNECTION_ERROR_NONE;
824 }
825
826 int _connection_libnet_get_statistics(net_statistics_type_e statistics_type, unsigned long long *size)
827 {
828         if (net_get_statistics(NET_DEVICE_WIFI, statistics_type, size) != NET_ERR_NONE)
829                         return CONNECTION_ERROR_OPERATION_FAILED;
830
831                 return CONNECTION_ERROR_NONE;
832 }
833