Set hidden network group name at time of association
[platform/upstream/connman.git] / src / network.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 struct connman_network {
34         struct connman_element element;
35         enum connman_network_type type;
36         enum connman_network_protocol protocol;
37         connman_bool_t associating;
38         connman_bool_t secondary;
39         connman_bool_t available;
40         connman_bool_t connected;
41         connman_uint8_t strength;
42         connman_uint16_t frequency;
43         char *identifier;
44         char *address;
45         char *name;
46         char *node;
47         char *group;
48         struct connman_ipconfig *ipconfig;
49
50         struct connman_network_driver *driver;
51         void *driver_data;
52
53         connman_bool_t registered;
54
55         struct connman_device *device;
56
57         struct {
58                 void *ssid;
59                 int ssid_len;
60                 char *mode;
61                 unsigned short channel;
62                 char *security;
63                 char *passphrase;
64         } wifi;
65 };
66
67 static const char *type2string(enum connman_network_type type)
68 {
69         switch (type) {
70         case CONNMAN_NETWORK_TYPE_UNKNOWN:
71         case CONNMAN_NETWORK_TYPE_VENDOR:
72                 break;
73         case CONNMAN_NETWORK_TYPE_WIFI:
74                 return "wifi";
75         case CONNMAN_NETWORK_TYPE_WIMAX:
76                 return "wimax";
77         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
78         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
79                 return "bluetooth";
80         case CONNMAN_NETWORK_TYPE_HSO:
81                 return "cellular";
82         }
83
84         return NULL;
85 }
86
87 static DBusMessage *get_properties(DBusConnection *conn,
88                                         DBusMessage *msg, void *data)
89 {
90         struct connman_network *network = data;
91         DBusMessage *reply;
92         DBusMessageIter array, dict;
93
94         DBG("conn %p", conn);
95
96         if (__connman_security_check_privilege(msg,
97                                         CONNMAN_SECURITY_PRIVILEGE_PUBLIC) < 0)
98                 return __connman_error_permission_denied(msg);
99
100         reply = dbus_message_new_method_return(msg);
101         if (reply == NULL)
102                 return NULL;
103
104         dbus_message_iter_init_append(reply, &array);
105
106         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
107                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
108                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
109                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
110
111         if (network->device) {
112                 const char *path = connman_device_get_path(network->device);
113                 if (path != NULL)
114                         connman_dbus_dict_append_variant(&dict, "Device",
115                                                 DBUS_TYPE_OBJECT_PATH, &path);
116         }
117
118         if (network->address != NULL)
119                 connman_dbus_dict_append_variant(&dict, "Address",
120                                         DBUS_TYPE_STRING, &network->address);
121
122         if (network->name != NULL)
123                 connman_dbus_dict_append_variant(&dict, "Name",
124                                         DBUS_TYPE_STRING, &network->name);
125
126         connman_dbus_dict_append_variant(&dict, "Connected",
127                                 DBUS_TYPE_BOOLEAN, &network->connected);
128
129         if (network->strength > 0)
130                 connman_dbus_dict_append_variant(&dict, "Strength",
131                                         DBUS_TYPE_BYTE, &network->strength);
132
133         if (network->frequency > 0)
134                 connman_dbus_dict_append_variant(&dict, "Frequency",
135                                         DBUS_TYPE_UINT16, &network->frequency);
136
137         if (network->wifi.ssid != NULL && network->wifi.ssid_len > 0)
138                 connman_dbus_dict_append_array(&dict, "WiFi.SSID",
139                                 DBUS_TYPE_BYTE, &network->wifi.ssid,
140                                                 network->wifi.ssid_len);
141
142         if (network->wifi.mode != NULL)
143                 connman_dbus_dict_append_variant(&dict, "WiFi.Mode",
144                                 DBUS_TYPE_STRING, &network->wifi.mode);
145
146         if (network->wifi.channel > 0)
147                 connman_dbus_dict_append_variant(&dict, "WiFi.Channel",
148                                 DBUS_TYPE_UINT16, &network->wifi.channel);
149
150         if (network->wifi.security != NULL)
151                 connman_dbus_dict_append_variant(&dict, "WiFi.Security",
152                                 DBUS_TYPE_STRING, &network->wifi.security);
153
154         if (network->wifi.passphrase != NULL &&
155                         __connman_security_check_privilege(msg,
156                                 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
157                 connman_dbus_dict_append_variant(&dict, "WiFi.Passphrase",
158                                 DBUS_TYPE_STRING, &network->wifi.passphrase);
159
160         dbus_message_iter_close_container(&array, &dict);
161
162         return reply;
163 }
164
165 static DBusMessage *set_property(DBusConnection *conn,
166                                         DBusMessage *msg, void *data)
167 {
168         struct connman_network *network = data;
169         DBusMessageIter iter, value;
170         const char *name;
171         int type;
172
173         DBG("conn %p", conn);
174
175         if (dbus_message_iter_init(msg, &iter) == FALSE)
176                 return __connman_error_invalid_arguments(msg);
177
178         dbus_message_iter_get_basic(&iter, &name);
179         dbus_message_iter_next(&iter);
180         dbus_message_iter_recurse(&iter, &value);
181
182         if (__connman_security_check_privilege(msg,
183                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
184                 return __connman_error_permission_denied(msg);
185
186         type = dbus_message_iter_get_arg_type(&value);
187
188         if (g_str_equal(name, "WiFi.Passphrase") == TRUE) {
189                 const char *passphrase;
190
191                 if (type != DBUS_TYPE_STRING)
192                         return __connman_error_invalid_arguments(msg);
193
194                 if (__connman_security_check_privilege(msg,
195                                         CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
196                         return __connman_error_permission_denied(msg);
197
198                 dbus_message_iter_get_basic(&value, &passphrase);
199
200                 g_free(network->wifi.passphrase);
201                 network->wifi.passphrase = g_strdup(passphrase);
202         } else if (g_str_has_prefix(name, "IPv4.") == TRUE) {
203                 int err;
204
205                 err = __connman_ipconfig_set_ipv4(network->ipconfig,
206                                                         name + 5, &value);
207                 if (err < 0)
208                         return __connman_error_failed(msg, -err);
209         } else
210                 return __connman_error_invalid_property(msg);
211
212         __connman_storage_save_network(network);
213
214         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
215 }
216
217 static DBusMessage *do_connect(DBusConnection *conn,
218                                         DBusMessage *msg, void *data)
219 {
220         struct connman_network *network = data;
221         int err;
222
223         DBG("conn %p", conn);
224
225         if (__connman_security_check_privilege(msg,
226                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
227                 return __connman_error_permission_denied(msg);
228
229         if (network->connected == TRUE)
230                 return __connman_error_failed(msg, EALREADY);
231
232         if (network->driver && network->driver->connect) {
233                 enum connman_device_mode mode;
234
235                 mode = connman_device_get_mode(network->device);
236                 if (mode == CONNMAN_DEVICE_MODE_NETWORK_SINGLE)
237                         __connman_device_disconnect(network->device);
238
239                 err = network->driver->connect(network);
240                 if (err < 0 && err != -EINPROGRESS)
241                         return __connman_error_failed(msg, -err);
242         } else
243                 network->connected = TRUE;
244
245         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
246 }
247
248 static DBusMessage *do_disconnect(DBusConnection *conn,
249                                         DBusMessage *msg, void *data)
250 {
251         struct connman_network *network = data;
252         int err;
253
254         DBG("conn %p", conn);
255
256         if (__connman_security_check_privilege(msg,
257                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
258                 return __connman_error_permission_denied(msg);
259
260         if (network->connected == FALSE)
261                 return __connman_error_failed(msg, EINVAL);
262
263         connman_element_unregister_children(&network->element);
264
265         connman_device_set_disconnected(network->device, TRUE);
266
267         if (network->driver && network->driver->disconnect) {
268                 err = network->driver->disconnect(network);
269                 if (err < 0 && err != -EINPROGRESS)
270                         return __connman_error_failed(msg, -err);
271         } else
272                 network->connected = FALSE;
273
274         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
275 }
276
277 static GDBusMethodTable network_methods[] = {
278         { "GetProperties", "",   "a{sv}", get_properties },
279         { "SetProperty",   "sv", "",      set_property   },
280         { "Connect",       "",   "",      do_connect     },
281         { "Disconnect",    "",   "",      do_disconnect  },
282         { },
283 };
284
285 static GDBusSignalTable network_signals[] = {
286         { "PropertyChanged", "sv" },
287         { },
288 };
289
290 static DBusConnection *connection;
291
292 static void append_networks(struct connman_device *device,
293                                                 DBusMessageIter *entry)
294 {
295         DBusMessageIter value, iter;
296         const char *key = "Networks";
297
298         dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
299
300         dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
301                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
302                                                                 &value);
303
304         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
305                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
306         __connman_element_list((struct connman_element *) device,
307                                         CONNMAN_ELEMENT_TYPE_NETWORK, &iter);
308         dbus_message_iter_close_container(&value, &iter);
309
310         dbus_message_iter_close_container(entry, &value);
311 }
312
313 static void emit_networks_signal(struct connman_device *device)
314 {
315         const char *path = connman_device_get_path(device);
316         DBusMessage *signal;
317         DBusMessageIter entry;
318
319         signal = dbus_message_new_signal(path,
320                                 CONNMAN_DEVICE_INTERFACE, "PropertyChanged");
321         if (signal == NULL)
322                 return;
323
324         dbus_message_iter_init_append(signal, &entry);
325
326         append_networks(device, &entry);
327
328         g_dbus_send_message(connection, signal);
329 }
330
331 static int register_interface(struct connman_element *element)
332 {
333         struct connman_network *network = element->network;
334
335         DBG("element %p name %s", element, element->name);
336
337         if (g_dbus_register_interface(connection, element->path,
338                                         CONNMAN_NETWORK_INTERFACE,
339                                         network_methods, network_signals,
340                                         NULL, network, NULL) == FALSE) {
341                 connman_error("Failed to register %s network", element->path);
342                 return -EIO;
343         }
344
345         network->registered = TRUE;
346
347         emit_networks_signal(network->device);
348
349         return 0;
350 }
351
352 static void unregister_interface(struct connman_element *element)
353 {
354         struct connman_network * network = element->network;
355
356         DBG("element %p name %s", element, element->name);
357
358         network->registered = FALSE;
359
360         emit_networks_signal(network->device);
361
362         g_dbus_unregister_interface(connection, element->path,
363                                                 CONNMAN_NETWORK_INTERFACE);
364 }
365
366 connman_bool_t __connman_network_has_driver(struct connman_network *network)
367 {
368         if (network == NULL || network->driver == NULL)
369                 return FALSE;
370
371         return network->registered;
372 }
373
374 static GSList *driver_list = NULL;
375
376 static gint compare_priority(gconstpointer a, gconstpointer b)
377 {
378         const struct connman_network_driver *driver1 = a;
379         const struct connman_network_driver *driver2 = b;
380
381         return driver2->priority - driver1->priority;
382 }
383
384 /**
385  * connman_network_driver_register:
386  * @driver: network driver definition
387  *
388  * Register a new network driver
389  *
390  * Returns: %0 on success
391  */
392 int connman_network_driver_register(struct connman_network_driver *driver)
393 {
394         DBG("driver %p name %s", driver, driver->name);
395
396         driver_list = g_slist_insert_sorted(driver_list, driver,
397                                                         compare_priority);
398
399         return 0;
400 }
401
402 /**
403  * connman_network_driver_unregister:
404  * @driver: network driver definition
405  *
406  * Remove a previously registered network driver
407  */
408 void connman_network_driver_unregister(struct connman_network_driver *driver)
409 {
410         DBG("driver %p name %s", driver, driver->name);
411
412         driver_list = g_slist_remove(driver_list, driver);
413 }
414
415 static void network_destruct(struct connman_element *element)
416 {
417         struct connman_network *network = element->network;
418
419         DBG("element %p name %s", element, element->name);
420
421         g_free(network->wifi.ssid);
422         g_free(network->wifi.mode);
423         g_free(network->wifi.security);
424         g_free(network->wifi.passphrase);
425
426         g_free(network->group);
427         g_free(network->node);
428         g_free(network->name);
429         g_free(network->address);
430         g_free(network->identifier);
431
432         connman_ipconfig_unref(network->ipconfig);
433 }
434
435 /**
436  * connman_network_create:
437  * @identifier: network identifier (for example an unqiue name)
438  *
439  * Allocate a new network and assign the #identifier to it.
440  *
441  * Returns: a newly-allocated #connman_network structure
442  */
443 struct connman_network *connman_network_create(const char *identifier,
444                                                 enum connman_network_type type)
445 {
446         struct connman_network *network;
447         connman_uint8_t strength = 0;
448         const char *str;
449         char *temp;
450
451         DBG("identifier %s type %d", identifier, type);
452
453         network = g_try_new0(struct connman_network, 1);
454         if (network == NULL)
455                 return NULL;
456
457         DBG("network %p", network);
458
459         __connman_element_initialize(&network->element);
460
461         //temp = connman_dbus_encode_string(identifier);
462         temp = g_strdup(identifier);
463         if (temp == NULL) {
464                 g_free(network);
465                 return NULL;
466         }
467
468         network->element.name = temp;
469         network->element.type = CONNMAN_ELEMENT_TYPE_NETWORK;
470
471         network->element.network = network;
472         network->element.destruct = network_destruct;
473
474         str = type2string(type);
475         if (str != NULL)
476                 connman_element_set_string(&network->element, "Type", str);
477
478         connman_element_set_uint8(&network->element, "Strength", strength);
479
480         network->type       = type;
481         network->secondary  = FALSE;
482         network->identifier = g_strdup(identifier);
483
484         network->ipconfig = connman_ipconfig_create();
485         if (network->ipconfig == NULL) {
486                 connman_network_unref(network);
487                 return NULL;
488         }
489
490         return network;
491 }
492
493 /**
494  * connman_network_ref:
495  * @network: network structure
496  *
497  * Increase reference counter of  network
498  */
499 struct connman_network *connman_network_ref(struct connman_network *network)
500 {
501         if (connman_element_ref(&network->element) == NULL)
502                 return NULL;
503
504         return network;
505 }
506
507 /**
508  * connman_network_unref:
509  * @network: network structure
510  *
511  * Decrease reference counter of network
512  */
513 void connman_network_unref(struct connman_network *network)
514 {
515         connman_element_unref(&network->element);
516 }
517
518 const char *__connman_network_get_type(struct connman_network *network)
519 {
520         return type2string(network->type);
521 }
522
523 /**
524  * connman_network_get_type:
525  * @network: network structure
526  *
527  * Get type of network
528  */
529 enum connman_network_type connman_network_get_type(struct connman_network *network)
530 {
531         return network->type;
532 }
533
534 /**
535  * connman_network_get_identifier:
536  * @network: network structure
537  *
538  * Get identifier of network
539  */
540 const char *connman_network_get_identifier(struct connman_network *network)
541 {
542         return network->identifier;
543 }
544
545 /**
546  * connman_network_get_path:
547  * @network: network structure
548  *
549  * Get path name of network
550  */
551 const char *connman_network_get_path(struct connman_network *network)
552 {
553         return network->element.path;
554 }
555
556 /**
557  * connman_network_set_index:
558  * @network: network structure
559  * @index: index number
560  *
561  * Set index number of network
562  */
563 void connman_network_set_index(struct connman_network *network, int index)
564 {
565         network->element.index = index;
566 }
567
568 /**
569  * connman_network_get_index:
570  * @network: network structure
571  *
572  * Get index number of network
573  */
574 int connman_network_get_index(struct connman_network *network)
575 {
576         return network->element.index;
577 }
578
579 /**
580  * connman_network_set_protocol:
581  * @network: network structure
582  * @protocol: network protocol
583  *
584  * Change protocol of network
585  */
586 void connman_network_set_protocol(struct connman_network *network,
587                                         enum connman_network_protocol protocol)
588 {
589         network->protocol = protocol;
590 }
591
592 /**
593  * connman_network_set_group:
594  * @network: network structure
595  * @group: group name
596  *
597  * Set group name for automatic clustering
598  */
599 void connman_network_set_group(struct connman_network *network,
600                                                         const char *group)
601 {
602         if (network->secondary == TRUE)
603                 return;
604
605         if (g_strcmp0(network->group, group) == 0)
606                 return;
607
608         switch (network->type) {
609         case CONNMAN_NETWORK_TYPE_UNKNOWN:
610         case CONNMAN_NETWORK_TYPE_VENDOR:
611         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
612         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
613         case CONNMAN_NETWORK_TYPE_HSO:
614                 return;
615         case CONNMAN_NETWORK_TYPE_WIFI:
616         case CONNMAN_NETWORK_TYPE_WIMAX:
617                 break;
618         }
619
620         if (network->group != NULL) {
621                 __connman_profile_remove_network(network);
622
623                 g_free(network->group);
624         }
625
626         network->group = g_strdup(group);
627
628         if (network->group != NULL)
629                 __connman_profile_add_network(network);
630 }
631
632 const char *connman_network_get_group(struct connman_network *network)
633 {
634         return network->group;
635 }
636
637 const char *__connman_network_get_ident(struct connman_network *network)
638 {
639         if (network->device == NULL)
640                 return NULL;
641
642         return __connman_device_get_ident(network->device);
643 }
644
645 /**
646  * connman_network_set_available:
647  * @network: network structure
648  * @available: availability state
649  *
650  * Change availability state of network (in range)
651  */
652 int connman_network_set_available(struct connman_network *network,
653                                                 connman_bool_t available)
654 {
655         DBG("network %p available %d", network, available);
656
657         if (network->available == available)
658                 return -EALREADY;
659
660         network->available = available;
661
662         return 0;
663 }
664
665 /**
666  * connman_network_get_available:
667  * @network: network structure
668  *
669  * Get network available setting
670  */
671 connman_bool_t connman_network_get_available(struct connman_network *network)
672 {
673         return network->available;
674 }
675
676 /**
677  * connman_network_set_associating:
678  * @network: network structure
679  * @associating: associating state
680  *
681  * Change associating state of network
682  */
683 int connman_network_set_associating(struct connman_network *network,
684                                                 connman_bool_t associating)
685 {
686         DBG("network %p associating %d", network, associating);
687
688         if (network->associating == associating)
689                 return -EALREADY;
690
691         network->associating = associating;
692
693         if (associating == TRUE) {
694                 struct connman_service *service;
695
696                 service = __connman_service_lookup_from_network(network);
697                 __connman_service_indicate_state(service,
698                                         CONNMAN_SERVICE_STATE_ASSOCIATION);
699         }
700
701         return 0;
702 }
703
704 static gboolean set_connected(gpointer user_data)
705 {
706         struct connman_network *network = user_data;
707         struct connman_service *service;
708
709         service = __connman_service_lookup_from_network(network);
710
711         if (network->connected == TRUE) {
712                 struct connman_element *element;
713                 enum connman_element_type type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
714
715                 switch (network->protocol) {
716                 case CONNMAN_NETWORK_PROTOCOL_UNKNOWN:
717                         return 0;
718                 case CONNMAN_NETWORK_PROTOCOL_IP:
719                         type = CONNMAN_ELEMENT_TYPE_DHCP;
720                         break;
721                 case CONNMAN_NETWORK_PROTOCOL_PPP:
722                         type = CONNMAN_ELEMENT_TYPE_PPP;
723                         break;
724                 }
725
726                 __connman_device_increase_connections(network->device);
727
728                 __connman_device_set_network(network->device, network);
729
730                 connman_device_set_disconnected(network->device, FALSE);
731
732                 element = connman_element_create(NULL);
733                 if (element != NULL) {
734                         element->type  = type;
735                         element->index = network->element.index;
736
737                         if (connman_element_register(element,
738                                                 &network->element) < 0)
739                                 connman_element_unref(element);
740
741                         __connman_service_indicate_state(service,
742                                         CONNMAN_SERVICE_STATE_CONFIGURATION);
743                 }
744         } else {
745                 connman_element_unregister_children(&network->element);
746
747                 __connman_device_set_network(network->device, NULL);
748
749                 __connman_device_decrease_connections(network->device);
750
751                 __connman_service_indicate_state(service,
752                                                 CONNMAN_SERVICE_STATE_IDLE);
753         }
754
755         return FALSE;
756 }
757
758 /**
759  * connman_network_set_connected:
760  * @network: network structure
761  * @connected: connected state
762  *
763  * Change connected state of network
764  */
765 int connman_network_set_connected(struct connman_network *network,
766                                                 connman_bool_t connected)
767 {
768         DBusMessage *signal;
769         DBusMessageIter entry, value;
770         const char *key = "Connected";
771
772         DBG("network %p connected %d", network, connected);
773
774         if (network->connected == connected)
775                 return -EALREADY;
776
777         network->connected = connected;
778
779         if (connected == TRUE)
780                 network->associating = FALSE;
781
782         if (network->registered == FALSE) {
783                 g_idle_add(set_connected, network);
784                 return 0;
785         }
786
787         signal = dbus_message_new_signal(network->element.path,
788                                 CONNMAN_NETWORK_INTERFACE, "PropertyChanged");
789         if (signal == NULL)
790                 return 0;
791
792         dbus_message_iter_init_append(signal, &entry);
793
794         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
795
796         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
797                                         DBUS_TYPE_BOOLEAN_AS_STRING, &value);
798         dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN, &connected);
799         dbus_message_iter_close_container(&entry, &value);
800
801         g_dbus_send_message(connection, signal);
802
803         set_connected(network);
804
805         return 0;
806 }
807
808 /**
809  * connman_network_get_connected:
810  * @network: network structure
811  *
812  * Get network connection status
813  */
814 connman_bool_t connman_network_get_connected(struct connman_network *network)
815 {
816         return network->connected;
817 }
818
819 /**
820  * __connman_network_connect:
821  * @network: network structure
822  *
823  * Connect network
824  */
825 int __connman_network_connect(struct connman_network *network)
826 {
827         int err;
828
829         DBG("network %p", network);
830
831         if (network->connected == TRUE)
832                 return -EALREADY;
833
834         if (network->driver == NULL)
835                 return -EUNATCH;
836
837         if (network->driver->connect == NULL)
838                 return -ENOSYS;
839
840         __connman_device_disconnect(network->device);
841
842         err = network->driver->connect(network);
843         if (err == 0) {
844                 network->connected = TRUE;
845                 set_connected(network);
846         }
847
848         return err;
849 }
850
851 /**
852  * __connman_network_disconnect:
853  * @network: network structure
854  *
855  * Disconnect network
856  */
857 int __connman_network_disconnect(struct connman_network *network)
858 {
859         int err;
860
861         DBG("network %p", network);
862
863         if (network->connected == FALSE)
864                 return -ENOTCONN;
865
866         if (network->driver == NULL)
867                 return -EUNATCH;
868
869         if (network->driver->disconnect == NULL)
870                 return -ENOSYS;
871
872         err = network->driver->disconnect(network);
873         if (err == 0) {
874                 network->connected = FALSE;
875                 set_connected(network);
876         }
877
878         return err;
879 }
880
881 /**
882  * connman_network_set_address:
883  * @network: network structure
884  * @address: binary address value
885  * @size: binary address length
886  *
887  * Set unique address value for network
888  */
889 int connman_network_set_address(struct connman_network *network,
890                                 const void *address, unsigned int size)
891 {
892         const unsigned char *addr_octet = address;
893         char *str;
894
895         DBG("network %p size %d", network, size);
896
897         if (size != 6)
898                 return -EINVAL;
899
900         str = g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
901                                 addr_octet[0], addr_octet[1], addr_octet[2],
902                                 addr_octet[3], addr_octet[4], addr_octet[5]);
903         if (str == NULL)
904                 return -ENOMEM;
905
906         g_free(network->address);
907         network->address = str;
908
909         return connman_element_set_string(&network->element,
910                                                 "Address", network->address);
911 }
912
913 /**
914  * connman_network_set_name:
915  * @network: network structure
916  * @name: name value
917  *
918  * Set display name value for network
919  */
920 int connman_network_set_name(struct connman_network *network,
921                                                         const char *name)
922 {
923         DBG("network %p name %s", network, name);
924
925         g_free(network->name);
926         network->name = g_strdup(name);
927
928         return connman_element_set_string(&network->element, "Name", name);
929 }
930
931 /**
932  * connman_network_set_strength:
933  * @network: network structure
934  * @strength: strength value
935  *
936  * Set signal strength value for network
937  */
938 int connman_network_set_strength(struct connman_network *network,
939                                                 connman_uint8_t strength)
940 {
941         DBG("network %p strengh %d", network, strength);
942
943         network->strength = strength;
944
945         return connman_element_set_uint8(&network->element,
946                                                 "Strength", strength);
947 }
948
949 /**
950  * connman_network_set_string:
951  * @network: network structure
952  * @key: unique identifier
953  * @value: string value
954  *
955  * Set string value for specific key
956  */
957 int connman_network_set_string(struct connman_network *network,
958                                         const char *key, const char *value)
959 {
960         DBG("network %p key %s value %s", network, key, value);
961
962         if (g_strcmp0(key, "Name") == 0)
963                 return connman_network_set_name(network, value);
964
965         if (g_str_equal(key, "Address") == TRUE) {
966                 g_free(network->address);
967                 network->address = g_strdup(value);
968         } else if (g_str_equal(key, "Node") == TRUE) {
969                 g_free(network->node);
970                 network->node = g_strdup(value);
971         } else if (g_str_equal(key, "WiFi.Mode") == TRUE) {
972                 g_free(network->wifi.mode);
973                 network->wifi.mode = g_strdup(value);
974         } else if (g_str_equal(key, "WiFi.Security") == TRUE) {
975                 g_free(network->wifi.security);
976                 network->wifi.security = g_strdup(value);
977         } else if (g_str_equal(key, "WiFi.Passphrase") == TRUE) {
978                 g_free(network->wifi.passphrase);
979                 network->wifi.passphrase = g_strdup(value);
980         }
981
982         return connman_element_set_string(&network->element, key, value);
983 }
984
985 /**
986  * connman_network_get_string:
987  * @network: network structure
988  * @key: unique identifier
989  *
990  * Get string value for specific key
991  */
992 const char *connman_network_get_string(struct connman_network *network,
993                                                         const char *key)
994 {
995         DBG("network %p key %s", network, key);
996
997         if (g_str_equal(key, "Address") == TRUE)
998                 return network->address;
999         else if (g_str_equal(key, "Name") == TRUE)
1000                 return network->name;
1001         else if (g_str_equal(key, "Node") == TRUE)
1002                 return network->node;
1003         else if (g_str_equal(key, "WiFi.Mode") == TRUE)
1004                 return network->wifi.mode;
1005         else if (g_str_equal(key, "WiFi.Security") == TRUE)
1006                 return network->wifi.security;
1007         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE)
1008                 return network->wifi.passphrase;
1009
1010         return connman_element_get_string(&network->element, key);
1011 }
1012
1013 /**
1014  * connman_network_set_uint8:
1015  * @network: network structure
1016  * @key: unique identifier
1017  * @value: integer value
1018  *
1019  * Set integer value for specific key
1020  */
1021 int connman_network_set_uint8(struct connman_network *network,
1022                                         const char *key, connman_uint8_t value)
1023 {
1024         DBG("network %p key %s value %d", network, key, value);
1025
1026         if (g_strcmp0(key, "Strength") == 0)
1027                 return connman_network_set_strength(network, value);
1028
1029         return connman_element_set_uint8(&network->element, key, value);
1030 }
1031
1032 /**
1033  * connman_network_get_uint8:
1034  * @network: network structure
1035  * @key: unique identifier
1036  *
1037  * Get integer value for specific key
1038  */
1039 connman_uint8_t connman_network_get_uint8(struct connman_network *network,
1040                                                         const char *key)
1041 {
1042         DBG("network %p key %s", network, key);
1043
1044         if (g_str_equal(key, "Strength") == TRUE)
1045                 return network->strength;
1046
1047         return connman_element_get_uint8(&network->element, key);
1048 }
1049
1050 /**
1051  * connman_network_set_uint16:
1052  * @network: network structure
1053  * @key: unique identifier
1054  * @value: integer value
1055  *
1056  * Set integer value for specific key
1057  */
1058 int connman_network_set_uint16(struct connman_network *network,
1059                                 const char *key, connman_uint16_t value)
1060 {
1061         DBG("network %p key %s value %d", network, key, value);
1062
1063         if (g_str_equal(key, "Frequency") == TRUE)
1064                 network->frequency = value;
1065         else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1066                 network->wifi.channel = value;
1067
1068         return -EINVAL;
1069 }
1070
1071 /**
1072  * connman_network_get_uint16:
1073  * @network: network structure
1074  * @key: unique identifier
1075  *
1076  * Get integer value for specific key
1077  */
1078 connman_uint16_t connman_network_get_uint16(struct connman_network *network,
1079                                                         const char *key)
1080 {
1081         DBG("network %p key %s", network, key);
1082
1083         if (g_str_equal(key, "Frequency") == TRUE)
1084                 return network->frequency;
1085         else if (g_str_equal(key, "WiFi.Channel") == TRUE)
1086                 return network->wifi.channel;
1087
1088         return 0;
1089 }
1090
1091 /**
1092  * connman_network_set_blob:
1093  * @network: network structure
1094  * @key: unique identifier
1095  * @data: blob data
1096  * @size: blob size
1097  *
1098  * Set binary blob value for specific key
1099  */
1100 int connman_network_set_blob(struct connman_network *network,
1101                         const char *key, const void *data, unsigned int size)
1102 {
1103         DBG("network %p key %s size %d", network, key, size);
1104
1105         if (g_strcmp0(key, "Address") == 0)
1106                 return connman_network_set_address(network, data, size);
1107
1108         if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1109                 g_free(network->wifi.ssid);
1110                 network->wifi.ssid = g_try_malloc(size);
1111                 if (network->wifi.ssid != NULL) {
1112                         memcpy(network->wifi.ssid, data, size);
1113                         network->wifi.ssid_len = size;
1114                 } else
1115                         network->wifi.ssid_len = 0;
1116         }
1117
1118         return connman_element_set_blob(&network->element, key, data, size);
1119 }
1120
1121 /**
1122  * connman_network_get_blob:
1123  * @network: network structure
1124  * @key: unique identifier
1125  * @size: pointer to blob size
1126  *
1127  * Get binary blob value for specific key
1128  */
1129 const void *connman_network_get_blob(struct connman_network *network,
1130                                         const char *key, unsigned int *size)
1131 {
1132         DBG("network %p key %s", network, key);
1133
1134         if (g_str_equal(key, "WiFi.SSID") == TRUE) {
1135                 if (size != NULL)
1136                         *size = network->wifi.ssid_len;
1137                 return network->wifi.ssid;
1138         }
1139
1140         return connman_element_get_blob(&network->element, key, size);
1141 }
1142
1143 void __connman_network_set_device(struct connman_network *network,
1144                                         struct connman_device *device)
1145 {
1146         network->device = device;
1147 }
1148
1149 /**
1150  * connman_network_get_device:
1151  * @network: network structure
1152  *
1153  * Get parent device of network
1154  */
1155 struct connman_device *connman_network_get_device(struct connman_network *network)
1156 {
1157         return network->device;
1158 }
1159
1160 /**
1161  * connman_network_get_data:
1162  * @network: network structure
1163  *
1164  * Get private network data pointer
1165  */
1166 void *connman_network_get_data(struct connman_network *network)
1167 {
1168         return network->driver_data;
1169 }
1170
1171 /**
1172  * connman_network_set_data:
1173  * @network: network structure
1174  * @data: data pointer
1175  *
1176  * Set private network data pointer
1177  */
1178 void connman_network_set_data(struct connman_network *network, void *data)
1179 {
1180         network->driver_data = data;
1181 }
1182
1183 static gboolean match_driver(struct connman_network *network,
1184                                         struct connman_network_driver *driver)
1185 {
1186         if (network->type == driver->type ||
1187                         driver->type == CONNMAN_NETWORK_TYPE_UNKNOWN)
1188                 return TRUE;
1189
1190         return FALSE;
1191 }
1192
1193 static int network_probe(struct connman_element *element)
1194 {
1195         struct connman_network *network = element->network;
1196         GSList *list;
1197         int err;
1198
1199         DBG("element %p name %s", element, element->name);
1200
1201         if (network == NULL)
1202                 return -ENODEV;
1203
1204         for (list = driver_list; list; list = list->next) {
1205                 struct connman_network_driver *driver = list->data;
1206
1207                 if (match_driver(network, driver) == FALSE)
1208                         continue;
1209
1210                 DBG("driver %p name %s", driver, driver->name);
1211
1212                 if (driver->probe(network) == 0) {
1213                         network->driver = driver;
1214                         break;
1215                 }
1216         }
1217
1218         if (network->driver == NULL)
1219                 return -ENODEV;
1220
1221         err = register_interface(element);
1222         if (err < 0) {
1223                 if (network->driver->remove)
1224                         network->driver->remove(network);
1225                 return err;
1226         }
1227
1228         network->secondary = connman_device_get_secondary(network->device);
1229
1230         switch (network->type) {
1231         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1232         case CONNMAN_NETWORK_TYPE_VENDOR:
1233         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1234         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1235         case CONNMAN_NETWORK_TYPE_HSO:
1236                 break;
1237         case CONNMAN_NETWORK_TYPE_WIFI:
1238         case CONNMAN_NETWORK_TYPE_WIMAX:
1239                 if (network->group != NULL && network->secondary == FALSE)
1240                         __connman_profile_add_network(network);
1241                 break;
1242         }
1243
1244         return 0;
1245 }
1246
1247 static void network_remove(struct connman_element *element)
1248 {
1249         struct connman_network *network = element->network;
1250
1251         DBG("element %p name %s", element, element->name);
1252
1253         if (network == NULL)
1254                 return;
1255
1256         if (network->driver == NULL)
1257                 return;
1258
1259         switch (network->type) {
1260         case CONNMAN_NETWORK_TYPE_UNKNOWN:
1261         case CONNMAN_NETWORK_TYPE_VENDOR:
1262         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
1263         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
1264         case CONNMAN_NETWORK_TYPE_HSO:
1265                 break;
1266         case CONNMAN_NETWORK_TYPE_WIFI:
1267         case CONNMAN_NETWORK_TYPE_WIMAX:
1268                 if (network->group != NULL && network->secondary == FALSE) {
1269                         __connman_profile_remove_network(network);
1270
1271                         g_free(network->group);
1272                         network->group = NULL;
1273                 }
1274                 break;
1275         }
1276
1277         unregister_interface(element);
1278
1279         if (network->driver->remove)
1280                 network->driver->remove(network);
1281 }
1282
1283 static void network_change(struct connman_element *element)
1284 {
1285         struct connman_network *network = element->network;
1286
1287         DBG("element %p name %s", element, element->name);
1288
1289         if (element->state != CONNMAN_ELEMENT_STATE_ERROR)
1290                 return;
1291
1292         if (element->error != CONNMAN_ELEMENT_ERROR_DHCP_FAILED)
1293                 return;
1294
1295         if (network->connected == FALSE)
1296                 return;
1297
1298         connman_element_unregister_children(element);
1299
1300         connman_device_set_disconnected(network->device, TRUE);
1301
1302         if (network->driver && network->driver->disconnect) {
1303                 network->driver->disconnect(network);
1304                 return;
1305         }
1306
1307         network->connected = FALSE;
1308 }
1309
1310 static struct connman_driver network_driver = {
1311         .name           = "network",
1312         .type           = CONNMAN_ELEMENT_TYPE_NETWORK,
1313         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
1314         .probe          = network_probe,
1315         .remove         = network_remove,
1316         .change         = network_change,
1317 };
1318
1319 int __connman_network_init(void)
1320 {
1321         DBG("");
1322
1323         connection = connman_dbus_get_connection();
1324
1325         return connman_driver_register(&network_driver);
1326 }
1327
1328 void __connman_network_cleanup(void)
1329 {
1330         DBG("");
1331
1332         connman_driver_unregister(&network_driver);
1333
1334         dbus_connection_unref(connection);
1335 }