device: add connman_device_get_ident()
[framework/connectivity/connman.git] / src / network.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <string.h>
28
29 #include <gdbus.h>
30
31 #include "connman.h"
32
33 static unsigned int hidden_counter = 0;
34
35 struct connman_network {
36         struct connman_element element;
37         enum connman_network_type type;
38         enum connman_network_protocol protocol;
39         connman_bool_t available;
40         connman_bool_t connected;
41         connman_bool_t roaming;
42         connman_bool_t hidden;
43         connman_uint8_t strength;
44         connman_uint16_t frequency;
45         char *identifier;
46         char *address;
47         char *name;
48         char *node;
49         char *group;
50         struct connman_ipconfig *ipconfig;
51
52         struct connman_network_driver *driver;
53         void *driver_data;
54
55         connman_bool_t registered;
56         connman_bool_t connecting;
57         connman_bool_t associating;
58
59         struct connman_device *device;
60
61         struct {
62                 void *ssid;
63                 int ssid_len;
64                 char *mode;
65                 unsigned short channel;
66                 char *security;
67                 char *passphrase;
68                 char *eap;
69                 char *identity;
70                 char *ca_cert_path;
71                 char *client_cert_path;
72                 char *private_key_path;
73                 char *private_key_passphrase;
74                 char *phase2_auth;
75         } wifi;
76 };
77
78 static const char *type2string(enum connman_network_type type)
79 {
80         switch (type) {
81         case CONNMAN_NETWORK_TYPE_UNKNOWN:
82         case CONNMAN_NETWORK_TYPE_VENDOR:
83                 break;
84         case CONNMAN_NETWORK_TYPE_ETHERNET:
85                 return "ethernet";
86         case CONNMAN_NETWORK_TYPE_WIFI:
87                 return "wifi";
88         case CONNMAN_NETWORK_TYPE_WIMAX:
89                 return "wimax";
90         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
91         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
92                 return "bluetooth";
93         case CONNMAN_NETWORK_TYPE_CELLULAR:
94                 return "cellular";
95         }
96
97         return NULL;
98 }
99
100 static DBusMessage *get_properties(DBusConnection *conn,
101                                         DBusMessage *msg, void *data)
102 {
103         struct connman_network *network = data;
104         DBusMessage *reply;
105         DBusMessageIter array, dict;
106
107         DBG("conn %p", conn);
108
109         reply = dbus_message_new_method_return(msg);
110         if (reply == NULL)
111                 return NULL;
112
113         dbus_message_iter_init_append(reply, &array);
114
115         connman_dbus_dict_open(&array, &dict);
116
117         if (network->device) {
118                 const char *path = connman_device_get_path(network->device);
119                 if (path != NULL)
120                         connman_dbus_dict_append_basic(&dict, "Device",
121                                                 DBUS_TYPE_OBJECT_PATH, &path);
122         }
123
124         if (network->address != NULL)
125                 connman_dbus_dict_append_basic(&dict, "Address",
126                                         DBUS_TYPE_STRING, &network->address);
127
128         if (network->name != NULL)
129                 connman_dbus_dict_append_basic(&dict, "Name",
130                                         DBUS_TYPE_STRING, &network->name);
131
132         connman_dbus_dict_append_basic(&dict, "Connected",
133                                 DBUS_TYPE_BOOLEAN, &network->connected);
134
135         if (network->strength > 0)
136                 connman_dbus_dict_append_basic(&dict, "Strength",
137                                         DBUS_TYPE_BYTE, &network->strength);
138
139         if (network->frequency > 0)
140                 connman_dbus_dict_append_basic(&dict, "Frequency",
141                                         DBUS_TYPE_UINT16, &network->frequency);
142
143         if (network->wifi.ssid != NULL && network->wifi.ssid_len > 0)
144                 connman_dbus_dict_append_fixed_array(&dict, "WiFi.SSID",
145                                 DBUS_TYPE_BYTE, &network->wifi.ssid,
146                                                 network->wifi.ssid_len);
147
148         if (network->wifi.mode != NULL)
149                 connman_dbus_dict_append_basic(&dict, "WiFi.Mode",
150                                 DBUS_TYPE_STRING, &network->wifi.mode);
151
152         if (network->wifi.channel > 0)
153                 connman_dbus_dict_append_basic(&dict, "WiFi.Channel",
154                                 DBUS_TYPE_UINT16, &network->wifi.channel);
155
156         if (network->wifi.security != NULL) {
157                 connman_dbus_dict_append_basic(&dict, "WiFi.Security",
158                                 DBUS_TYPE_STRING, &network->wifi.security);
159
160                 if (g_strcmp0(network->wifi.security, "ieee8021x") == 0 &&
161                                                     network->wifi.eap != NULL)
162                         connman_dbus_dict_append_basic(&dict, "WiFi.EAP",
163                                         DBUS_TYPE_STRING, &network->wifi.eap);
164         }
165
166
167         if (network->wifi.passphrase != NULL)
168                 connman_dbus_dict_append_basic(&dict, "WiFi.Passphrase",
169                                 DBUS_TYPE_STRING, &network->wifi.passphrase);
170
171         connman_dbus_dict_close(&array, &dict);
172
173         return reply;
174 }
175
176 static GDBusMethodTable network_methods[] = {
177         { "GetProperties", "",   "a{sv}", get_properties },
178         { },
179 };
180
181 static GDBusSignalTable network_signals[] = {
182         { "PropertyChanged", "sv" },
183         { },
184 };
185
186 static DBusConnection *connection;
187
188 static void append_networks(DBusMessageIter *iter, void *user_data)
189 {
190         struct connman_device *device = user_data;
191
192         __connman_element_list((struct connman_element *) device,
193                                         CONNMAN_ELEMENT_TYPE_NETWORK, iter);
194 }
195
196 static void emit_networks_signal(struct connman_device *device)
197 {
198         const char *path = connman_device_get_path(device);
199
200         connman_dbus_property_changed_array(path,
201                         CONNMAN_DEVICE_INTERFACE, "Networks",
202                         DBUS_TYPE_OBJECT_PATH, append_networks, device);
203 }
204
205 static int register_interface(struct connman_element *element)
206 {
207         struct connman_network *network = element->network;
208
209         DBG("element %p name %s", element, element->name);
210
211         if (g_dbus_register_interface(connection, element->path,
212                                         CONNMAN_NETWORK_INTERFACE,
213                                         network_methods, network_signals,
214                                         NULL, network, NULL) == FALSE) {
215                 connman_error("Failed to register %s network", element->path);
216                 return -EIO;
217         }
218
219         network->registered = TRUE;
220
221         emit_networks_signal(network->device);
222
223         return 0;
224 }
225
226 static void unregister_interface(struct connman_element *element)
227 {
228         struct connman_network * network = element->network;
229
230         DBG("element %p name %s", element, element->name);
231
232         network->registered = FALSE;
233
234         if (network->device != NULL)
235                 emit_networks_signal(network->device);
236
237         g_dbus_unregister_interface(connection, element->path,
238                                                 CONNMAN_NETWORK_INTERFACE);
239 }
240
241 connman_bool_t __connman_network_has_driver(struct connman_network *network)
242 {
243         if (network == NULL || network->driver == NULL)
244                 return FALSE;
245
246         return network->registered;
247 }
248
249 static GSList *driver_list = NULL;
250
251 static gint compare_priority(gconstpointer a, gconstpointer b)
252 {
253         const struct connman_network_driver *driver1 = a;
254         const struct connman_network_driver *driver2 = b;
255
256         return driver2->priority - driver1->priority;
257 }
258
259 /**
260  * connman_network_driver_register:
261  * @driver: network driver definition
262  *
263  * Register a new network driver
264  *
265  * Returns: %0 on success
266  */
267 int connman_network_driver_register(struct connman_network_driver *driver)
268 {
269         DBG("driver %p name %s", driver, driver->name);
270
271         driver_list = g_slist_insert_sorted(driver_list, driver,
272                                                         compare_priority);
273
274         return 0;
275 }
276
277 /**
278  * connman_network_driver_unregister:
279  * @driver: network driver definition
280  *
281  * Remove a previously registered network driver
282  */
283 void connman_network_driver_unregister(struct connman_network_driver *driver)
284 {
285         DBG("driver %p name %s", driver, driver->name);
286
287         driver_list = g_slist_remove(driver_list, driver);
288 }
289
290 static void network_destruct(struct connman_element *element)
291 {
292         struct connman_network *network = element->network;
293
294         DBG("element %p name %s", element, element->name);
295
296         g_free(network->wifi.ssid);
297         g_free(network->wifi.mode);
298         g_free(network->wifi.security);
299         g_free(network->wifi.passphrase);
300
301         g_free(network->group);
302         g_free(network->node);
303         g_free(network->name);
304         g_free(network->address);
305         g_free(network->identifier);
306
307         if (network->ipconfig) {
308                 connman_ipconfig_unref(network->ipconfig);
309                 network->ipconfig = NULL;
310         }
311
312         network->device = NULL;
313 }
314
315 /**
316  * connman_network_create:
317  * @identifier: network identifier (for example an unqiue name)
318  *
319  * Allocate a new network and assign the #identifier to it.
320  *
321  * Returns: a newly-allocated #connman_network structure
322  */
323 struct connman_network *connman_network_create(const char *identifier,
324                                                 enum connman_network_type type)
325 {
326         struct connman_network *network;
327         connman_uint8_t strength = 0;
328         const char *str;
329         char *temp;
330
331         DBG("identifier %s type %d", identifier, type);
332
333         network = g_try_new0(struct connman_network, 1);
334         if (network == NULL)
335                 return NULL;
336
337         DBG("network %p", network);
338
339         __connman_element_initialize(&network->element);
340
341         //temp = connman_dbus_encode_string(identifier);
342         if (identifier == NULL) {
343                 temp = g_strdup_printf("hidden_%d", hidden_counter++);
344                 network->hidden = TRUE;
345         } else
346                 temp = g_strdup(identifier);
347
348         if (temp == NULL) {
349                 g_free(network);
350                 return NULL;
351         }
352
353         network->element.name = temp;
354         network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
355
356         network->element.network = network;
357         network->element.destruct = network_destruct;
358
359         str = type2string(type);
360         if (str != NULL)
361                 connman_element_set_string(&network->element, "Type", str);
362
363         connman_element_set_uint8(&network->element, "Strength", strength);
364
365         network->type       = type;
366         network->identifier = g_strdup(temp);
367
368         return network;
369 }
370
371 /**
372  * connman_network_ref:
373  * @network: network structure
374  *
375  * Increase reference counter of  network
376  */
377 struct connman_network *connman_network_ref(struct connman_network *network)
378 {
379         if (connman_element_ref(&network->element) == NULL)
380                 return NULL;
381
382         return network;
383 }
384
385 /**
386  * connman_network_unref:
387  * @network: network structure
388  *
389  * Decrease reference counter of network
390  */
391 void connman_network_unref(struct connman_network *network)
392 {
393         connman_element_unref(&network->element);
394 }
395
396 const char *__connman_network_get_type(struct connman_network *network)
397 {
398         return type2string(network->type);
399 }
400
401 /**
402  * connman_network_get_type:
403  * @network: network structure
404  *
405  * Get type of network
406  */
407 enum connman_network_type connman_network_get_type(struct connman_network *network)
408 {
409         return network->type;
410 }
411
412 /**
413  * connman_network_get_identifier:
414  * @network: network structure
415  *
416  * Get identifier of network
417  */
418 const char *connman_network_get_identifier(struct connman_network *network)
419 {
420         return network->identifier;
421 }
422
423 /**
424  * connman_network_get_path:
425  * @network: network structure
426  *
427  * Get path name of network
428  */
429 const char *connman_network_get_path(struct connman_network *network)
430 {
431         return network->element.path;
432 }
433
434 /**
435  * connman_network_set_index:
436  * @network: network structure
437  * @index: index number
438  *
439  * Set index number of network
440  */
441 void connman_network_set_index(struct connman_network *network, int index)
442 {
443         struct connman_service *service;
444         struct connman_ipconfig *ipconfig;
445
446         service = __connman_service_lookup_from_network(network);
447         if (service == NULL)
448                 goto done;
449
450         ipconfig = __connman_service_get_ipconfig(service);
451
452         if (network->element.index < 0 && ipconfig == NULL)
453                 /*
454                  * This is needed for plugins that havent set their ipconfig
455                  * layer yet, due to not being able to get a network index
456                  * prior to creating a service.
457                  */
458                 __connman_service_create_ipconfig(service, index);
459         else {
460                 /* If index changed, the index of ipconfig must be reset. */
461                 if (ipconfig == NULL)
462                         goto done;
463
464                 __connman_ipconfig_set_index(ipconfig, index);
465         }
466
467 done:
468         network->element.index = index;
469 }
470
471 /**
472  * connman_network_get_index:
473  * @network: network structure
474  *
475  * Get index number of network
476  */
477 int connman_network_get_index(struct connman_network *network)
478 {
479         return network->element.index;
480 }
481
482 /**
483  * connman_network_get_element:
484  * @network: network structure
485  *
486  * Get connman_element of network
487  */
488 struct connman_element *connman_network_get_element(
489                                 struct connman_network *network)
490 {
491         return &network->element;
492 }
493
494 /**
495  * connman_network_set_protocol:
496  * @network: network structure
497  * @protocol: network protocol
498  *
499  * Change protocol of network
500  */
501 void connman_network_set_protocol(struct connman_network *network,
502                                         enum connman_network_protocol protocol)
503 {
504         network->protocol = protocol;
505 }
506
507 /**
508  * connman_network_set_group:
509  * @network: network structure
510  * @group: group name
511  *
512  * Set group name for automatic clustering
513  */
514 void connman_network_set_group(struct connman_network *network,
515                                                         const char *group)
516 {
517         switch (network->type) {
518         case CONNMAN_NETWORK_TYPE_UNKNOWN:
519         case CONNMAN_NETWORK_TYPE_VENDOR:
520                 return;
521         case CONNMAN_NETWORK_TYPE_ETHERNET:
522         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
523         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
524         case CONNMAN_NETWORK_TYPE_CELLULAR:
525         case CONNMAN_NETWORK_TYPE_WIFI:
526         case CONNMAN_NETWORK_TYPE_WIMAX:
527                 break;
528         }
529
530         if (g_strcmp0(network->group, group) == 0) {
531                 if (group != NULL && network->registered)
532                         __connman_profile_update_network(network);
533                 return;
534         }
535
536         if (network->group != NULL) {
537                 if (network->registered)
538                         __connman_profile_remove_network(network);
539
540                 g_free(network->group);
541         }
542
543         network->group = g_strdup(group);
544
545         if (network->group != NULL) {
546                 if (network->registered)
547                         __connman_profile_add_network(network);
548         }
549 }
550
551 /**
552  * connman_network_get_group:
553  * @network: network structure
554  *
555  * Get group name for automatic clustering
556  */
557 const char *connman_network_get_group(struct connman_network *network)
558 {
559         return network->group;
560 }
561
562 const char *__connman_network_get_ident(struct connman_network *network)
563 {
564         if (network->device == NULL)
565                 return NULL;
566
567         return connman_device_get_ident(network->device);
568 }
569
570 connman_bool_t __connman_network_get_weakness(struct connman_network *network)
571 {
572         switch (network->type) {
573         case CONNMAN_NETWORK_TYPE_UNKNOWN:
574         case CONNMAN_NETWORK_TYPE_VENDOR:
575         case CONNMAN_NETWORK_TYPE_ETHERNET:
576         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
577         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
578         case CONNMAN_NETWORK_TYPE_CELLULAR:
579         case CONNMAN_NETWORK_TYPE_WIMAX:
580                 break;
581         case CONNMAN_NETWORK_TYPE_WIFI:
582                 if (g_strcmp0(network->wifi.mode, "adhoc") == 0)
583                         return TRUE;
584                 if (network->strength > 0 && network->strength < 20)
585                         return TRUE;
586                 break;
587         }
588
589         return FALSE;
590 }
591
592 connman_bool_t __connman_network_get_connecting(struct connman_network *network)
593 {
594         return network->connecting;
595 }
596
597 /**
598  * connman_network_set_available:
599  * @network: network structure
600  * @available: availability state
601  *
602  * Change availability state of network (in range)
603  */
604 int connman_network_set_available(struct connman_network *network,
605                                                 connman_bool_t available)
606 {
607         DBG("network %p available %d", network, available);
608
609         if (network->available == available)
610                 return -EALREADY;
611
612         network->available = available;
613
614         return 0;
615 }
616
617 /**
618  * connman_network_get_available:
619  * @network: network structure
620  *
621  * Get network available setting
622  */
623 connman_bool_t connman_network_get_available(struct connman_network *network)
624 {
625         if (network->hidden == TRUE)
626                 return TRUE;
627
628         return network->available;
629 }
630
631 /**
632  * connman_network_set_associating:
633  * @network: network structure
634  * @associating: associating state
635  *
636  * Change associating state of network
637  */
638 int connman_network_set_associating(struct connman_network *network,
639                                                 connman_bool_t associating)
640 {
641         DBG("network %p associating %d", network, associating);
642
643         if (network->associating == associating)
644                 return -EALREADY;
645
646         network->associating = associating;
647
648         if (associating == TRUE) {
649                 struct connman_service *service;
650
651                 service = __connman_service_lookup_from_network(network);
652                 __connman_service_indicate_state(service,
653                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
654         }
655
656         return 0;
657 }
658
659 static void set_associate_error(struct connman_network *network)
660 {
661         struct connman_service *service;
662
663         if (network->associating == FALSE)
664                 return ;
665
666         network->associating = FALSE;
667
668         service = __connman_service_lookup_from_network(network);
669
670         __connman_service_indicate_state(service,
671                                         CONNMAN_SERVICE_STATE_FAILURE);
672 }
673
674 static void set_configure_error(struct connman_network *network)
675 {
676         struct connman_service *service;
677
678         network->connecting = FALSE;
679
680         service = __connman_service_lookup_from_network(network);
681
682         __connman_service_indicate_state(service,
683                                         CONNMAN_SERVICE_STATE_FAILURE);
684 }
685
686 void connman_network_set_method(struct connman_network *network,
687                                         enum connman_ipconfig_method method)
688 {
689         struct connman_service *service;
690         struct connman_ipconfig *ipconfig;
691
692         network->element.ipv4.method = method;
693
694         service = __connman_service_lookup_from_network(network);
695         if (service == NULL)
696                 return;
697
698         ipconfig = __connman_service_get_ipconfig(service);
699         if (ipconfig == NULL)
700                 return;
701
702         connman_ipconfig_set_method(ipconfig, method);
703 }
704
705 void connman_network_set_error(struct connman_network *network,
706                                         enum connman_network_error error)
707 {
708         DBG("nework %p, error %d", network, error);
709
710         network->connecting = FALSE;
711
712         switch (error) {
713         case CONNMAN_NETWORK_ERROR_UNKNOWN:
714                 return;
715         case CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL:
716                 set_associate_error(network);
717                 break;
718         case CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL:
719                 set_configure_error(network);
720                 break;
721         }
722 }
723
724 static void set_configuration(struct connman_network *network)
725 {
726         struct connman_service *service;
727
728         DBG("network %p", network);
729
730         __connman_device_increase_connections(network->device);
731
732         __connman_device_set_network(network->device, network);
733
734         connman_device_set_disconnected(network->device, FALSE);
735
736         service = __connman_service_lookup_from_network(network);
737         __connman_service_indicate_state(service,
738                                         CONNMAN_SERVICE_STATE_CONFIGURATION);
739 }
740
741 static int set_connected_fixed(struct connman_network *network)
742 {
743         struct connman_service *service;
744         struct connman_element *parent, *element;
745
746         DBG("");
747
748         service = __connman_service_lookup_from_network(network);
749
750         parent = connman_network_get_element(network);
751
752         set_configuration(network);
753
754         if (parent->ipv4.address == NULL)
755                 return -EINVAL;
756
757         if (parent->ipv4.netmask == NULL)
758                 return -EINVAL;
759
760         element = connman_element_create(NULL);
761         if (element == NULL) {
762                 connman_error("Can not create connman_element");
763                 return -ENOMEM;
764         }
765
766         element->type = CONNMAN_ELEMENT_TYPE_IPV4;
767         element->index = parent->index;
768
769         if (connman_element_register(element, parent) < 0) {
770                 connman_error("Can not register connman_element");
771                 return -EINVAL;
772         }
773
774         network->connecting = FALSE;
775
776         connman_network_set_associating(network, FALSE);
777
778         return 0;
779 }
780
781 static void set_connected_manual(struct connman_network *network)
782 {
783         struct connman_service *service;
784         struct connman_ipconfig *ipconfig;
785         const char *nameserver = NULL;
786         int err;
787
788         DBG("network %p", network);
789
790         service = __connman_service_lookup_from_network(network);
791
792         ipconfig = __connman_service_get_ipconfig(service);
793
794         set_configuration(network);
795
796         err = __connman_ipconfig_set_address(ipconfig);
797         if (err < 0) {
798                 connman_network_set_error(network,
799                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
800                 return;
801         }
802
803         connman_element_get_value(&network->element,
804                         CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, &nameserver);
805         if (nameserver != NULL)
806                 __connman_service_append_nameserver(service, nameserver);
807
808         __connman_ipconfig_set_gateway(ipconfig, &network->element);
809
810         network->connecting = FALSE;
811
812         connman_network_set_associating(network, FALSE);
813
814         __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY);
815 }
816
817 static int set_connected_dhcp(struct connman_network *network)
818 {
819         struct connman_element *element;
820         int error;
821
822         DBG("network %p", network);
823
824         if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
825                 return -EINVAL;
826
827         element = connman_element_create(NULL);
828         if (element == NULL)
829                 return -ENOMEM;
830
831         element->type  = CONNMAN_ELEMENT_TYPE_DHCP;
832         element->index = network->element.index;
833
834         error = connman_element_register(element, &network->element);
835         if (error < 0) {
836                 connman_element_unref(element);
837                 return error;
838         }
839
840         set_configuration(network);
841
842         return 0;
843 }
844
845 static int manual_ipv6_set(struct connman_network *network,
846                                 struct connman_ipconfig *ipconfig_ipv6)
847 {
848         struct connman_service *service;
849         int err;
850
851         service = __connman_service_lookup_from_network(network);
852         if (service == NULL)
853                 return -EINVAL;
854
855         err = __connman_ipconfig_set_address(ipconfig_ipv6);
856         if (err < 0) {
857                 connman_network_set_error(network,
858                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
859                 return err;
860         }
861
862         /*
863          * READY state will be indicated by IPV4 setting
864          * gateway will be set by IPV4 setting
865          */
866
867         return 0;
868 }
869
870 static gboolean set_connected(gpointer user_data)
871 {
872         struct connman_network *network = user_data;
873         struct connman_service *service;
874         struct connman_ipconfig *ipconfig;
875         enum connman_ipconfig_method method;
876
877         service = __connman_service_lookup_from_network(network);
878
879         ipconfig = __connman_service_get_ipconfig(service);
880
881         method = __connman_ipconfig_get_method(ipconfig);
882
883         DBG("method %d", method);
884
885         if (network->connected == TRUE) {
886                 enum connman_ipconfig_method ipv6_method;
887                 struct connman_ipconfig *ipv6config;
888                 int ret;
889
890                 ipv6config = connman_ipconfig_get_ipv6config(ipconfig);
891                 ipv6_method = __connman_ipconfig_get_method(ipv6config);
892                 switch (ipv6_method) {
893                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
894                 case CONNMAN_IPCONFIG_METHOD_OFF:
895                         break;
896                 case CONNMAN_IPCONFIG_METHOD_FIXED:
897                 case CONNMAN_IPCONFIG_METHOD_MANUAL:
898                         ret = manual_ipv6_set(network, ipv6config);
899                         if (ret != 0) {
900                                 connman_network_set_error(network,
901                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
902                                 return FALSE;
903                         }
904                         break;
905                 case CONNMAN_IPCONFIG_METHOD_DHCP:
906                         break;
907                 }
908
909                 switch (method) {
910                 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
911                 case CONNMAN_IPCONFIG_METHOD_OFF:
912                         return FALSE;
913                 case CONNMAN_IPCONFIG_METHOD_FIXED:
914                         if (set_connected_fixed(network) < 0) {
915                                 connman_network_set_error(network,
916                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
917                                 return FALSE;
918                         }
919                         return TRUE;
920                 case CONNMAN_IPCONFIG_METHOD_MANUAL:
921                         set_connected_manual(network);
922                         return TRUE;
923                 case CONNMAN_IPCONFIG_METHOD_DHCP:
924                         if (set_connected_dhcp(network) < 0) {
925                                 connman_network_set_error(network,
926                                         CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
927                                 return FALSE;
928                         }
929                 }
930
931         } else {
932                 struct connman_service *service;
933
934                 connman_element_unregister_children(&network->element);
935
936                 __connman_device_set_network(network->device, NULL);
937                 network->hidden = FALSE;
938
939                 __connman_device_decrease_connections(network->device);
940
941                 service = __connman_service_lookup_from_network(network);
942
943                 __connman_service_indicate_state(service,
944                                                 CONNMAN_SERVICE_STATE_IDLE);
945         }
946
947         network->connecting = FALSE;
948
949         connman_network_set_associating(network, FALSE);
950
951         return FALSE;
952 }
953
954 /**
955  * connman_network_set_connected:
956  * @network: network structure
957  * @connected: connected state
958  *
959  * Change connected state of network
960  */
961 int connman_network_set_connected(struct connman_network *network,
962                                                 connman_bool_t connected)
963 {
964         DBG("network %p connected %d", network, connected);
965
966         if ((network->connecting == TRUE || network->associating == TRUE) &&
967                                                         connected == FALSE) {
968                 connman_element_set_error(&network->element,
969                                         CONNMAN_ELEMENT_ERROR_CONNECT_FAILED);
970                 __connman_network_disconnect(network);
971         }
972
973         if (network->connected == connected)
974                 return -EALREADY;
975
976         network->connected = connected;
977
978         if (network->registered == FALSE) {
979                 g_idle_add(set_connected, network);
980                 return 0;
981         }
982
983         connman_dbus_property_changed_basic(network->element.path,
984                                 CONNMAN_NETWORK_INTERFACE, "Connected",
985                                                 DBUS_TYPE_BOOLEAN, &connected);
986
987         set_connected(network);
988
989         return 0;
990 }
991
992 /**
993  * connman_network_get_connected:
994  * @network: network structure
995  *
996  * Get network connection status
997  */
998 connman_bool_t connman_network_get_connected(struct connman_network *network)
999 {
1000         return network->connected;
1001 }
1002
1003 /**
1004  * connman_network_get_associating:
1005  * @network: network structure
1006  *
1007  * Get network associating status
1008  */
1009 connman_bool_t connman_network_get_associating(struct connman_network *network)
1010 {
1011         return network->associating;
1012 }
1013
1014 /**
1015  * __connman_network_connect:
1016  * @network: network structure
1017  *
1018  * Connect network
1019  */
1020 int __connman_network_connect(struct connman_network *network)
1021 {
1022         struct connman_service *service;
1023         int err;
1024
1025         DBG("network %p", network);
1026
1027         if (network->connected == TRUE)
1028                 return -EISCONN;
1029
1030         if (network->connecting == TRUE || network->associating == TRUE)
1031                 return -EALREADY;
1032
1033         if (network->driver == NULL)
1034                 return -EUNATCH;
1035
1036         if (network->driver->connect == NULL)
1037                 return -ENOSYS;
1038
1039         if (network->device == NULL)
1040                 return -ENODEV;
1041
1042         __connman_device_disconnect(network->device);
1043
1044         network->connecting = TRUE;
1045
1046         service = __connman_service_lookup_from_network(network);
1047
1048         err = network->driver->connect(network);
1049         if (err < 0) {
1050                 if (err == -EINPROGRESS)
1051                         connman_network_set_associating(network, TRUE);
1052                 else {
1053                         network->connecting = FALSE;
1054                         network->hidden = FALSE;
1055                 }
1056
1057                 return err;
1058         }
1059
1060         network->connected = TRUE;
1061         set_connected(network);
1062
1063         return err;
1064 }
1065
1066 /**
1067  * __connman_network_disconnect:
1068  * @network: network structure
1069  *
1070  * Disconnect network
1071  */
1072 int __connman_network_disconnect(struct connman_network *network)
1073 {
1074         int err;
1075
1076         DBG("network %p", network);
1077
1078         if (network->connected == FALSE && network->connecting == FALSE &&
1079                                                 network->associating == FALSE)
1080                 return -ENOTCONN;
1081
1082         if (network->driver == NULL)
1083                 return -EUNATCH;
1084
1085         if (network->driver->disconnect == NULL)
1086                 return -ENOSYS;
1087
1088         network->connecting = FALSE;
1089
1090         err = network->driver->disconnect(network);
1091         if (err == 0) {
1092                 connman_network_set_connected(network, FALSE);
1093                 set_connected(network);
1094         }
1095
1096         return err;
1097 }
1098
1099 static int dhcp_start(struct connman_network *network)
1100 {
1101         struct connman_element *element;
1102         int error;
1103
1104         if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
1105                 return -EINVAL;
1106
1107         element = connman_element_create(NULL);
1108         if (element == NULL)
1109                 return -ENOMEM;
1110
1111         element->type  = CONNMAN_ELEMENT_TYPE_DHCP;
1112         element->index = network->element.index;
1113
1114         error = connman_element_register(element, &network->element);
1115         if (error < 0) {
1116                 connman_element_unref(element);
1117                 return error;
1118         }
1119
1120         return 0;
1121 }
1122
1123 static int dhcp_stop(struct connman_network *network)
1124 {
1125         if (network->protocol != CONNMAN_NETWORK_PROTOCOL_IP)
1126                 return -EINVAL;
1127
1128         connman_element_unregister_children_type(&network->element,
1129                                         CONNMAN_ELEMENT_TYPE_CONNECTION);
1130         connman_element_unregister_children_type(&network->element,
1131                                                 CONNMAN_ELEMENT_TYPE_IPV4);
1132         connman_element_unregister_children_type(&network->element,
1133                                                 CONNMAN_ELEMENT_TYPE_DHCP);
1134
1135         return 0;
1136 }
1137
1138 static int manual_ipv4_set(struct connman_network *network,
1139                                 struct connman_ipconfig *ipconfig)
1140 {
1141         struct connman_service *service;
1142         int err;
1143
1144         service = __connman_service_lookup_from_network(network);
1145         if (service == NULL)
1146                 return -EINVAL;
1147
1148         err = __connman_ipconfig_set_address(ipconfig);
1149         if (err < 0) {
1150                 connman_network_set_error(network,
1151                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1152                 return err;
1153         }
1154
1155         __connman_ipconfig_set_gateway(ipconfig, &network->element);
1156
1157         __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY);
1158
1159         return 0;
1160 }
1161
1162 int __connman_network_clear_ipconfig(struct connman_network *network,
1163                                         struct connman_ipconfig *ipconfig)
1164 {
1165         struct connman_service *service;
1166         enum connman_ipconfig_method method;
1167
1168         service = __connman_service_lookup_from_network(network);
1169         if (service == NULL)
1170                 return -EINVAL;
1171
1172         method = __connman_ipconfig_get_method(ipconfig);
1173
1174         switch (method) {
1175         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1176         case CONNMAN_IPCONFIG_METHOD_OFF:
1177         case CONNMAN_IPCONFIG_METHOD_FIXED:
1178                 return -EINVAL;
1179         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1180                 connman_element_unregister_children_type(&network->element,
1181                                         CONNMAN_ELEMENT_TYPE_CONNECTION);
1182                 __connman_ipconfig_clear_address(ipconfig);
1183                 break;
1184         case CONNMAN_IPCONFIG_METHOD_DHCP:
1185                 dhcp_stop(network);
1186                 break;
1187         }
1188
1189         __connman_service_indicate_state(service,
1190                                         CONNMAN_SERVICE_STATE_CONFIGURATION);
1191
1192         return 0;
1193 }
1194
1195 int __connman_network_set_ipconfig(struct connman_network *network,
1196                                         struct connman_ipconfig *ipconfig)
1197 {
1198         struct connman_ipconfig *ipv6config;
1199         enum connman_ipconfig_method method;
1200         int ret;
1201
1202         ipv6config = connman_ipconfig_get_ipv6config(ipconfig);
1203         method = __connman_ipconfig_get_method(ipv6config);
1204         switch (method) {
1205         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1206         case CONNMAN_IPCONFIG_METHOD_OFF:
1207                 break;
1208         case CONNMAN_IPCONFIG_METHOD_FIXED:
1209         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1210                 ret = manual_ipv6_set(network, ipv6config);
1211                 if (ret != 0) {
1212                         connman_network_set_error(network,
1213                                 CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
1214                         return FALSE;
1215                 }
1216                 break;
1217         case CONNMAN_IPCONFIG_METHOD_DHCP:
1218                 break;
1219         }
1220
1221         method = __connman_ipconfig_get_method(ipconfig);
1222
1223         switch (method) {
1224         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1225         case CONNMAN_IPCONFIG_METHOD_OFF:
1226         case CONNMAN_IPCONFIG_METHOD_FIXED:
1227                 return -EINVAL;
1228         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1229                 return manual_ipv4_set(network, ipconfig);
1230         case CONNMAN_IPCONFIG_METHOD_DHCP:
1231                 return dhcp_start(network);
1232         }
1233
1234         return 0;
1235 }
1236
1237 /**
1238  * connman_network_set_address:
1239  * @network: network structure
1240  * @address: binary address value
1241  * @size: binary address length
1242  *
1243  * Set unique address value for network
1244  */
1245 int connman_network_set_address(struct connman_network *network,
1246                                 const void *address, unsigned int size)
1247 {
1248         const unsigned char *addr_octet = address;
1249         char *str;
1250
1251         DBG("network %p size %d", network, size);
1252
1253         if (size != 6)
1254                 return -EINVAL;
1255
1256         str = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
1257                                 addr_octet[0], addr_octet[1], addr_octet[2],
1258                                 addr_octet[3], addr_octet[4], addr_octet[5]);
1259         if (str == NULL)
1260                 return -ENOMEM;
1261
1262         g_free(network->address);
1263         network->address = str;
1264
1265         return connman_element_set_string(&network->element,
1266                                                 "Address", network->address);
1267 }
1268
1269 /**
1270  * connman_network_set_name:
1271  * @network: network structure
1272  * @name: name value
1273  *
1274  * Set display name value for network
1275  */
1276 int connman_network_set_name(struct connman_network *network,
1277                                                         const char *name)
1278 {
1279         DBG("network %p name %s", network, name);
1280
1281         g_free(network->name);
1282         network->name = g_strdup(name);
1283
1284         return connman_element_set_string(&network->element, "Name", name);
1285 }
1286
1287 /**
1288  * connman_network_set_strength:
1289  * @network: network structure
1290  * @strength: strength value
1291  *
1292  * Set signal strength value for network
1293  */
1294 int connman_network_set_strength(struct connman_network *network,
1295                                                 connman_uint8_t strength)
1296 {
1297         DBG("network %p strengh %d", network, strength);
1298
1299         network->strength = strength;
1300
1301         return connman_element_set_uint8(&network->element,
1302                                                 "Strength", strength);
1303 }
1304
1305 /**
1306  * connman_network_set_roaming:
1307  * @network: network structure
1308  * @roaming: roaming state
1309  *
1310  * Set roaming state for network
1311  */
1312 int connman_network_set_roaming(struct connman_network *network,
1313                                                 connman_bool_t roaming)
1314 {
1315         DBG("network %p roaming %d", network, roaming);
1316
1317         network->roaming = roaming;
1318
1319         return connman_element_set_bool(&network->element,
1320                                                 "Roaming", roaming);
1321 }
1322
1323 /**
1324  * connman_network_set_string:
1325  * @network: network structure
1326  * @key: unique identifier
1327  * @value: string value
1328  *
1329  * Set string value for specific key
1330  */
1331 int connman_network_set_string(struct connman_network *network,
1332                                         const char *key, const char *value)
1333 {
1334         int err;
1335
1336         DBG("network %p key %s value %s", network, key, value);
1337
1338         if (g_strcmp0(key, "Name") == 0)
1339                 return connman_network_set_name(network, value);
1340
1341         if (g_str_equal(key, "Address") == TRUE) {
1342                 g_free(network->address);
1343                 network->address = g_strdup(value);
1344         } else if (g_str_equal(key, "Node") == TRUE) {
1345                 g_free(network->node);
1346                 network->node = g_strdup(value);
1347         } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
1348                 g_free(network->wifi.mode);
1349                 network->wifi.mode = g_strdup(value);
1350         } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
1351                 g_free(network->wifi.security);
1352                 network->wifi.security = g_strdup(value);
1353         } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
1354                 g_free(network->wifi.passphrase);
1355                 network->wifi.passphrase = g_strdup(value);
1356         } else if (g_str_equal(key, "WiFi.EAP") == TRUE) {
1357                 g_free(network->wifi.eap);
1358                 network->wifi.eap = g_strdup(value);
1359         } else if (g_str_equal(key, "WiFi.Identity") == TRUE) {
1360                 g_free(network->wifi.identity);
1361                 network->wifi.identity = g_strdup(value);
1362         } else if (g_str_equal(key, "WiFi.CACertFile") == TRUE) {
1363                 g_free(network->wifi.ca_cert_path);
1364                 network->wifi.ca_cert_path = g_strdup(value);
1365         } else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE) {
1366                 g_free(network->wifi.client_cert_path);
1367                 network->wifi.client_cert_path = g_strdup(value);
1368         } else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE) {
1369                 g_free(network->wifi.private_key_path);
1370                 network->wifi.private_key_path = g_strdup(value);
1371         } else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE) {
1372                 g_free(network->wifi.private_key_passphrase);
1373                 network->wifi.private_key_passphrase = g_strdup(value);
1374         } else if (g_str_equal(key, "WiFi.Phase2") == TRUE) {
1375                 g_free(network->wifi.phase2_auth);
1376                 network->wifi.phase2_auth = g_strdup(value);
1377         }
1378
1379         err = connman_element_set_string(&network->element, key, value);
1380         if (err < 0)
1381                 return err;
1382
1383         if (network->driver == NULL)
1384                 return 0;
1385
1386         if (network->driver->setup)
1387                 return network->driver->setup(network, key);
1388
1389         return 0;
1390 }
1391
1392 /**
1393  * connman_network_get_string:
1394  * @network: network structure
1395  * @key: unique identifier
1396  *
1397  * Get string value for specific key
1398  */
1399 const char *connman_network_get_string(struct connman_network *network,
1400                                                         const char *key)
1401 {
1402         DBG("network %p key %s", network, key);
1403
1404         if (g_str_equal(key, "Address") == TRUE)
1405                 return network->address;
1406         else if (g_str_equal(key, "Name") == TRUE)
1407                 return network->name;
1408         else if (g_str_equal(key, "Node") == TRUE)
1409                 return network->node;
1410         else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1411                 return network->wifi.mode;
1412         else if (g_str_equal(key, "WiFi.Security") == TRUE)
1413                 return network->wifi.security;
1414         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1415                 return network->wifi.passphrase;
1416         else if (g_str_equal(key, "WiFi.EAP") == TRUE)
1417                 return network->wifi.eap;
1418         else if (g_str_equal(key, "WiFi.Identity") == TRUE)
1419                 return network->wifi.identity;
1420         else if (g_str_equal(key, "WiFi.CACertFile") == TRUE)
1421                 return network->wifi.ca_cert_path;
1422         else if (g_str_equal(key, "WiFi.ClientCertFile") == TRUE)
1423                 return network->wifi.client_cert_path;
1424         else if (g_str_equal(key, "WiFi.PrivateKeyFile") == TRUE)
1425                 return network->wifi.private_key_path;
1426         else if (g_str_equal(key, "WiFi.PrivateKeyPassphrase") == TRUE)
1427                 return network->wifi.private_key_passphrase;
1428         else if (g_str_equal(key, "WiFi.Phase2") == TRUE)
1429                 return network->wifi.phase2_auth;
1430
1431         return connman_element_get_string(&network->element, key);
1432 }
1433
1434 /**
1435  * connman_network_set_bool:
1436  * @network: network structure
1437  * @key: unique identifier
1438  * @value: boolean value
1439  *
1440  * Set boolean value for specific key
1441  */
1442 int connman_network_set_bool(struct connman_network *network,
1443                                         const char *key, connman_bool_t value)
1444 {
1445         DBG("network %p key %s value %d", network, key, value);
1446
1447         if (g_strcmp0(key, "Roaming") == 0)
1448                 return connman_network_set_roaming(network, value);
1449
1450         return connman_element_set_bool(&network->element, key, value);
1451 }
1452
1453 /**
1454  * connman_network_get_bool:
1455  * @network: network structure
1456  * @key: unique identifier
1457  *
1458  * Get boolean value for specific key
1459  */
1460 connman_bool_t connman_network_get_bool(struct connman_network *network,
1461                                                         const char *key)
1462 {
1463         DBG("network %p key %s", network, key);
1464
1465         if (g_str_equal(key, "Roaming") == TRUE)
1466                 return network->roaming;
1467
1468         return connman_element_get_bool(&network->element, key);
1469 }
1470
1471 /**
1472  * connman_network_set_uint8:
1473  * @network: network structure
1474  * @key: unique identifier
1475  * @value: integer value
1476  *
1477  * Set integer value for specific key
1478  */
1479 int connman_network_set_uint8(struct connman_network *network,
1480                                         const char *key, connman_uint8_t value)
1481 {
1482         DBG("network %p key %s value %d", network, key, value);
1483
1484         if (g_strcmp0(key, "Strength") == 0)
1485                 return connman_network_set_strength(network, value);
1486
1487         return connman_element_set_uint8(&network->element, key, value);
1488 }
1489
1490 /**
1491  * connman_network_get_uint8:
1492  * @network: network structure
1493  * @key: unique identifier
1494  *
1495  * Get integer value for specific key
1496  */
1497 connman_uint8_t connman_network_get_uint8(struct connman_network *network,
1498                                                         const char *key)
1499 {
1500         DBG("network %p key %s", network, key);
1501
1502         if (g_str_equal(key, "Strength") == TRUE)
1503                 return network->strength;
1504
1505         return connman_element_get_uint8(&network->element, key);
1506 }
1507
1508 /**
1509  * connman_network_set_uint16:
1510  * @network: network structure
1511  * @key: unique identifier
1512  * @value: integer value
1513  *
1514  * Set integer value for specific key
1515  */
1516 int connman_network_set_uint16(struct connman_network *network,
1517                                 const char *key, connman_uint16_t value)
1518 {
1519         DBG("network %p key %s value %d", network, key, value);
1520
1521         if (g_str_equal(key, "Frequency") == TRUE)
1522                 network->frequency = value;
1523         else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1524                 network->wifi.channel = value;
1525
1526         return -EINVAL;
1527 }
1528
1529 /**
1530  * connman_network_get_uint16:
1531  * @network: network structure
1532  * @key: unique identifier
1533  *
1534  * Get integer value for specific key
1535  */
1536 connman_uint16_t connman_network_get_uint16(struct connman_network *network,
1537                                                         const char *key)
1538 {
1539         DBG("network %p key %s", network, key);
1540
1541         if (g_str_equal(key, "Frequency") == TRUE)
1542                 return network->frequency;
1543         else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1544                 return network->wifi.channel;
1545
1546         return 0;
1547 }
1548
1549 /**
1550  * connman_network_set_blob:
1551  * @network: network structure
1552  * @key: unique identifier
1553  * @data: blob data
1554  * @size: blob size
1555  *
1556  * Set binary blob value for specific key
1557  */
1558 int connman_network_set_blob(struct connman_network *network,
1559                         const char *key, const void *data, unsigned int size)
1560 {
1561         DBG("network %p key %s size %d", network, key, size);
1562
1563         if (g_strcmp0(key, "Address") == 0)
1564                 return connman_network_set_address(network, data, size);
1565
1566         if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1567                 g_free(network->wifi.ssid);
1568                 network->wifi.ssid = g_try_malloc(size);
1569                 if (network->wifi.ssid != NULL) {
1570                         memcpy(network->wifi.ssid, data, size);
1571                         network->wifi.ssid_len = size;
1572                 } else
1573                         network->wifi.ssid_len = 0;
1574         }
1575
1576         return connman_element_set_blob(&network->element, key, data, size);
1577 }
1578
1579 /**
1580  * connman_network_get_blob:
1581  * @network: network structure
1582  * @key: unique identifier
1583  * @size: pointer to blob size
1584  *
1585  * Get binary blob value for specific key
1586  */
1587 const void *connman_network_get_blob(struct connman_network *network,
1588                                         const char *key, unsigned int *size)
1589 {
1590         DBG("network %p key %s", network, key);
1591
1592         if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1593                 if (size != NULL)
1594                         *size = network->wifi.ssid_len;
1595                 return network->wifi.ssid;
1596         }
1597
1598         return connman_element_get_blob(&network->element, key, size);
1599 }
1600
1601 void __connman_network_set_device(struct connman_network *network,
1602                                         struct connman_device *device)
1603 {
1604         network->device = device;
1605 }
1606
1607 /**
1608  * connman_network_get_device:
1609  * @network: network structure
1610  *
1611  * Get parent device of network
1612  */
1613 struct connman_device *connman_network_get_device(struct connman_network *network)
1614 {
1615         return network->device;
1616 }
1617
1618 /**
1619  * connman_network_get_data:
1620  * @network: network structure
1621  *
1622  * Get private network data pointer
1623  */
1624 void *connman_network_get_data(struct connman_network *network)
1625 {
1626         return network->driver_data;
1627 }
1628
1629 /**
1630  * connman_network_set_data:
1631  * @network: network structure
1632  * @data: data pointer
1633  *
1634  * Set private network data pointer
1635  */
1636 void connman_network_set_data(struct connman_network *network, void *data)
1637 {
1638         network->driver_data = data;
1639 }
1640
1641 void connman_network_update(struct connman_network *network)
1642 {
1643         switch (network->type) {
1644         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1645         case CONNMAN_NETWORK_TYPE_VENDOR:
1646                 return;
1647         case CONNMAN_NETWORK_TYPE_ETHERNET:
1648         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1649         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1650         case CONNMAN_NETWORK_TYPE_CELLULAR:
1651         case CONNMAN_NETWORK_TYPE_WIFI:
1652         case CONNMAN_NETWORK_TYPE_WIMAX:
1653                 break;
1654         }
1655
1656         if (network->group != NULL)
1657                 __connman_service_update_from_network(network);
1658
1659         return;
1660 }
1661
1662 static gboolean match_driver(struct connman_network *network,
1663                                         struct connman_network_driver *driver)
1664 {
1665         if (network->type == driver->type ||
1666                         driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
1667                 return TRUE;
1668
1669         return FALSE;
1670 }
1671
1672 static int network_probe(struct connman_element *element)
1673 {
1674         struct connman_network *network = element->network;
1675         GSList *list;
1676         int err;
1677
1678         DBG("element %p name %s", element, element->name);
1679
1680         if (network == NULL)
1681                 return -ENODEV;
1682
1683         for (list = driver_list; list; list = list->next) {
1684                 struct connman_network_driver *driver = list->data;
1685
1686                 if (match_driver(network, driver) == FALSE)
1687                         continue;
1688
1689                 DBG("driver %p name %s", driver, driver->name);
1690
1691                 if (driver->probe(network) == 0) {
1692                         network->driver = driver;
1693                         break;
1694                 }
1695         }
1696
1697         if (network->driver == NULL)
1698                 return -ENODEV;
1699
1700         err = register_interface(element);
1701         if (err < 0) {
1702                 if (network->driver->remove)
1703                         network->driver->remove(network);
1704                 return err;
1705         }
1706
1707         switch (network->type) {
1708         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1709         case CONNMAN_NETWORK_TYPE_VENDOR:
1710                 break;
1711         case CONNMAN_NETWORK_TYPE_ETHERNET:
1712         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1713         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1714         case CONNMAN_NETWORK_TYPE_CELLULAR:
1715         case CONNMAN_NETWORK_TYPE_WIFI:
1716         case CONNMAN_NETWORK_TYPE_WIMAX:
1717                 if (network->group != NULL)
1718                         __connman_profile_add_network(network);
1719                 break;
1720         }
1721
1722         return 0;
1723 }
1724
1725 static void network_remove(struct connman_element *element)
1726 {
1727         struct connman_network *network = element->network;
1728
1729         DBG("element %p name %s", element, element->name);
1730
1731         if (network == NULL)
1732                 return;
1733
1734         if (network->driver == NULL)
1735                 return;
1736
1737         switch (network->type) {
1738         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1739         case CONNMAN_NETWORK_TYPE_VENDOR:
1740                 break;
1741         case CONNMAN_NETWORK_TYPE_ETHERNET:
1742         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1743         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1744         case CONNMAN_NETWORK_TYPE_CELLULAR:
1745         case CONNMAN_NETWORK_TYPE_WIFI:
1746         case CONNMAN_NETWORK_TYPE_WIMAX:
1747                 if (network->group != NULL) {
1748                         if (network->registered)
1749                                 __connman_profile_remove_network(network);
1750
1751                         g_free(network->group);
1752                         network->group = NULL;
1753                 }
1754                 break;
1755         }
1756
1757         unregister_interface(element);
1758
1759         if (network->driver->remove)
1760                 network->driver->remove(network);
1761 }
1762
1763 static void network_change(struct connman_element *element)
1764 {
1765         struct connman_network *network = element->network;
1766
1767         DBG("element %p name %s", element, element->name);
1768
1769         if (element->state != CONNMAN_ELEMENT_STATE_ERROR)
1770                 return;
1771
1772         if (element->error != CONNMAN_ELEMENT_ERROR_DHCP_FAILED)
1773                 return;
1774
1775         if (network->connected == FALSE)
1776                 return;
1777
1778         connman_element_unregister_children(element);
1779
1780         connman_device_set_disconnected(network->device, TRUE);
1781
1782         if (network->driver && network->driver->disconnect) {
1783                 network->driver->disconnect(network);
1784                 return;
1785         }
1786
1787         network->connected = FALSE;
1788 }
1789
1790 static struct connman_driver network_driver = {
1791         .name           = "network",
1792         .type           = CONNMAN_ELEMENT_TYPE_NETWORK,
1793         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1794         .probe          = network_probe,
1795         .remove         = network_remove,
1796         .change         = network_change,
1797 };
1798
1799 int __connman_network_init(void)
1800 {
1801         DBG("");
1802
1803         connection = connman_dbus_get_connection();
1804
1805         return connman_driver_register(&network_driver);
1806 }
1807
1808 void __connman_network_cleanup(void)
1809 {
1810         DBG("");
1811
1812         connman_driver_unregister(&network_driver);
1813
1814         dbus_connection_unref(connection);
1815 }