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