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