X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fdevice.c;h=3a5e467d5a2a522946344b4f9548f3822a4595c4;hb=0bfab55f1a737afedaffeff652fc3e75ee80f2b4;hp=12fa49a0723cae120690eac97f9a99a4260675c0;hpb=fcfc5cc4268e7458a64b22d31537473327fed6a8;p=framework%2Fconnectivity%2Fconnman.git diff --git a/src/device.c b/src/device.c index 12fa49a..3a5e467 100644 --- a/src/device.c +++ b/src/device.c @@ -2,7 +2,7 @@ * * Connection Manager * - * Copyright (C) 2007-2010 Intel Corporation. All rights reserved. + * Copyright (C) 2007-2012 Intel Corporation. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -32,19 +32,21 @@ static GSList *device_list = NULL; static gchar **device_filter = NULL; static gchar **nodevice_filter = NULL; +enum connman_pending_type { + PENDING_NONE = 0, + PENDING_ENABLE = 1, + PENDING_DISABLE = 2, +}; + struct connman_device { - gint refcount; + int refcount; enum connman_device_type type; - connman_bool_t offlinemode; - connman_bool_t blocked; + enum connman_pending_type powered_pending; /* Indicates a pending + enable/disable request */ connman_bool_t powered; - connman_bool_t powered_pending; - connman_bool_t powered_persistent; connman_bool_t scanning; connman_bool_t disconnected; connman_bool_t reconnect; - connman_uint16_t scan_interval; - connman_uint16_t backoff_interval; char *name; char *node; char *address; @@ -54,8 +56,7 @@ struct connman_device { char *devname; int phyindex; int index; - unsigned int connections; - guint scan_timeout; + guint pending_timeout; struct connman_device_driver *driver; void *driver_data; @@ -65,71 +66,14 @@ struct connman_device { GHashTable *networks; }; -#define SCAN_INITIAL_DELAY 10 - -static gboolean device_scan_trigger(gpointer user_data) -{ - struct connman_device *device = user_data; - - DBG("device %p", device); - - if (device->driver == NULL) { - device->scan_timeout = 0; - return FALSE; - } - - if (device->driver->scan) - device->driver->scan(device); - - return TRUE; -} - -static void clear_scan_trigger(struct connman_device *device) -{ - if (device->scan_timeout > 0) { - g_source_remove(device->scan_timeout); - device->scan_timeout = 0; - } -} - -static void reset_scan_trigger(struct connman_device *device) +static void clear_pending_trigger(struct connman_device *device) { - clear_scan_trigger(device); - - if (device->scan_interval > 0) { - guint interval; - - if (g_hash_table_size(device->networks) == 0) { - if (device->backoff_interval >= device->scan_interval) - device->backoff_interval = SCAN_INITIAL_DELAY; - interval = device->backoff_interval; - } else - interval = device->scan_interval; - - DBG("interval %d", interval); - - device->scan_timeout = g_timeout_add_seconds(interval, - device_scan_trigger, device); - - device->backoff_interval *= 2; - if (device->backoff_interval > device->scan_interval) - device->backoff_interval = device->scan_interval; + if (device->pending_timeout > 0) { + g_source_remove(device->pending_timeout); + device->pending_timeout = 0; } } -static void force_scan_trigger(struct connman_device *device) -{ - clear_scan_trigger(device); - - device->scan_timeout = g_timeout_add_seconds(5, - device_scan_trigger, device); -} - -void connman_device_schedule_scan(struct connman_device *device) -{ - reset_scan_trigger(device); -} - static const char *type2description(enum connman_device_type type) { switch (type) { @@ -209,109 +153,110 @@ enum connman_service_type __connman_device_get_service_type(struct connman_devic return CONNMAN_SERVICE_TYPE_UNKNOWN; } +static gboolean device_pending_reset(gpointer user_data) +{ + struct connman_device *device = user_data; + + DBG("device %p", device); + + /* Power request timedout, reset power pending state. */ + device->pending_timeout = 0; + device->powered_pending = PENDING_NONE; + + return FALSE; +} + int __connman_device_enable(struct connman_device *device) { int err; - enum connman_service_type type; - DBG("device %p %d", device, device->blocked); + DBG("device %p", device); if (!device->driver || !device->driver->enable) return -EOPNOTSUPP; - if (device->powered_pending == TRUE) + /* There is an ongoing power disable request. */ + if (device->powered_pending == PENDING_DISABLE) + return -EBUSY; + + if (device->powered_pending == PENDING_ENABLE) return -EALREADY; - if (device->blocked == TRUE) - return -ENOLINK; + if (device->powered_pending == PENDING_NONE && device->powered == TRUE) + return -EALREADY; - connman_device_set_disconnected(device, FALSE); - device->scanning = FALSE; + device->powered_pending = PENDING_ENABLE; err = device->driver->enable(device); - if (err < 0 && err != -EALREADY) { - if (err == -EINPROGRESS) { - device->powered_pending = TRUE; - device->offlinemode = FALSE; - if (__connman_profile_get_offlinemode() == TRUE) - __connman_profile_set_offlinemode(FALSE, FALSE); - } - return err; + /* + * device gets enabled right away. + * Invoke the callback + */ + if (err == 0) { + connman_device_set_powered(device, TRUE); + goto done; } - device->powered_pending = TRUE; - device->powered = TRUE; - device->offlinemode = FALSE; - if (__connman_profile_get_offlinemode() == TRUE) - __connman_profile_set_offlinemode(FALSE, FALSE); - - type = __connman_device_get_service_type(device); - __connman_technology_enable(type); - - return 0; + if (err == -EALREADY) { + /* If device is already powered, but connman is not updated */ + connman_device_set_powered(device, TRUE); + goto done; + } + /* + * if err == -EINPROGRESS, then the DBus call to the respective daemon + * was successful. We set a 4 sec timeout so if the daemon never + * returns a reply, we would reset the pending request. + */ + if (err == -EINPROGRESS) + device->pending_timeout = g_timeout_add_seconds(4, + device_pending_reset, device); +done: + return err; } int __connman_device_disable(struct connman_device *device) { int err; - enum connman_service_type type; DBG("device %p", device); if (!device->driver || !device->driver->disable) return -EOPNOTSUPP; - if (device->powered == FALSE) - return -ENOLINK; + /* Ongoing power enable request */ + if (device->powered_pending == PENDING_ENABLE) + return -EBUSY; + + if (device->powered_pending == PENDING_DISABLE) + return -EALREADY; - if (device->powered_pending == FALSE) + if (device->powered_pending == PENDING_NONE && device->powered == FALSE) return -EALREADY; + device->powered_pending = PENDING_DISABLE; device->reconnect = FALSE; - clear_scan_trigger(device); + if (device->network) { + struct connman_service *service = + __connman_service_lookup_from_network(device->network); - err = device->driver->disable(device); - if (err < 0 && err != -EALREADY) { - if (err == -EINPROGRESS) - device->powered_pending = FALSE; - return err; + if (service != NULL) + __connman_service_disconnect(service); + else + connman_network_set_connected(device->network, FALSE); } - g_hash_table_remove_all(device->networks); - - device->connections = 0; - - device->powered_pending = FALSE; - device->powered = FALSE; - - type = __connman_device_get_service_type(device); - __connman_technology_disable(type); - - return 0; -} - -static int set_powered(struct connman_device *device, connman_bool_t powered) -{ - DBG("device %p powered %d", device, powered); - - if (powered == TRUE) - return __connman_device_enable(device); - else - return __connman_device_disable(device); -} - -static int setup_device(struct connman_device *device) -{ - DBG("device %p", device); - - __connman_technology_add_device(device); - - if (device->offlinemode == FALSE && - device->powered_persistent == TRUE) - __connman_device_enable(device); + err = device->driver->disable(device); + if (err == 0 || err == -EALREADY) { + connman_device_set_powered(device, FALSE); + goto done; + } - return 0; + if (err == -EINPROGRESS) + device->pending_timeout = g_timeout_add_seconds(4, + device_pending_reset, device); +done: + return err; } static void probe_driver(struct connman_device_driver *driver) @@ -334,7 +279,7 @@ static void probe_driver(struct connman_device_driver *driver) device->driver = driver; - setup_device(device); + __connman_technology_add_device(device); } } @@ -433,7 +378,7 @@ static void device_destruct(struct connman_device *device) { DBG("device %p name %s", device, device->name); - clear_scan_trigger(device); + clear_pending_trigger(device); g_free(device->ident); g_free(device->node); @@ -464,8 +409,6 @@ struct connman_device *connman_device_create(const char *node, enum connman_device_type type) { struct connman_device *device; - enum connman_service_type service_type; - connman_bool_t bg_scan; DBG("node %s type %d", node, type); @@ -477,38 +420,11 @@ struct connman_device *connman_device_create(const char *node, device->refcount = 1; - bg_scan = connman_setting_get_bool("BackgroundScanning"); - device->type = type; device->name = g_strdup(type2description(device->type)); - device->powered_persistent = TRUE; - device->phyindex = -1; - service_type = __connman_device_get_service_type(device); - device->blocked = __connman_technology_get_blocked(service_type); - device->backoff_interval = SCAN_INITIAL_DELAY; - - switch (type) { - case CONNMAN_DEVICE_TYPE_UNKNOWN: - case CONNMAN_DEVICE_TYPE_ETHERNET: - case CONNMAN_DEVICE_TYPE_WIMAX: - case CONNMAN_DEVICE_TYPE_BLUETOOTH: - case CONNMAN_DEVICE_TYPE_CELLULAR: - case CONNMAN_DEVICE_TYPE_GPS: - case CONNMAN_DEVICE_TYPE_GADGET: - case CONNMAN_DEVICE_TYPE_VENDOR: - device->scan_interval = 0; - break; - case CONNMAN_DEVICE_TYPE_WIFI: - if (bg_scan == TRUE) - device->scan_interval = 300; - else - device->scan_interval = 0; - break; - } - device->networks = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, free_network); @@ -523,11 +439,13 @@ struct connman_device *connman_device_create(const char *node, * * Increase reference counter of device */ -struct connman_device *connman_device_ref(struct connman_device *device) +struct connman_device *connman_device_ref_debug(struct connman_device *device, + const char *file, int line, const char *caller) { - DBG("%p", device); + DBG("%p ref %d by %s:%d:%s()", device, device->refcount + 1, + file, line, caller); - g_atomic_int_inc(&device->refcount); + __sync_fetch_and_add(&device->refcount, 1); return device; } @@ -538,9 +456,13 @@ struct connman_device *connman_device_ref(struct connman_device *device) * * Decrease reference counter of device */ -void connman_device_unref(struct connman_device *device) +void connman_device_unref_debug(struct connman_device *device, + const char *file, int line, const char *caller) { - if (g_atomic_int_dec_and_test(&device->refcount) == FALSE) + DBG("%p ref %d by %s:%d:%s()", device, device->refcount - 1, + file, line, caller); + + if (__sync_fetch_and_sub(&device->refcount, 1) != 1) return; if (device->driver) { @@ -656,75 +578,39 @@ const char *connman_device_get_ident(struct connman_device *device) int connman_device_set_powered(struct connman_device *device, connman_bool_t powered) { - int err; enum connman_service_type type; DBG("driver %p powered %d", device, powered); - if (device->powered == powered) { - device->powered_pending = powered; + if (device->powered == powered) return -EALREADY; - } - if (powered == TRUE) - err = __connman_device_enable(device); - else - err = __connman_device_disable(device); + clear_pending_trigger(device); - if (err < 0 && err != -EINPROGRESS && err != -EALREADY) - return err; + device->powered_pending = PENDING_NONE; device->powered = powered; - device->powered_pending = powered; type = __connman_device_get_service_type(device); - if (device->powered == TRUE) - __connman_technology_enable(type); - else - __connman_technology_disable(type); - - if (device->offlinemode == TRUE && powered == TRUE) - return connman_device_set_powered(device, FALSE); - - if (powered == FALSE) + if (device->powered == FALSE) { + __connman_technology_disabled(type); return 0; + } - reset_scan_trigger(device); + __connman_technology_enabled(type); + + connman_device_set_disconnected(device, FALSE); + device->scanning = FALSE; - if (device->driver && device->driver->scan) + if (device->driver && device->driver->scan_fast) + device->driver->scan_fast(device); + else if (device->driver && device->driver->scan) device->driver->scan(device); return 0; } -int __connman_device_set_blocked(struct connman_device *device, - connman_bool_t blocked) -{ - connman_bool_t powered; - - DBG("device %p blocked %d", device, blocked); - - device->blocked = blocked; - - if (device->offlinemode == TRUE) - return 0; - - connman_info("%s {rfkill} blocked %d", device->interface, blocked); - - if (blocked == FALSE) - powered = device->powered_persistent; - else - powered = FALSE; - - return set_powered(device, powered); -} - -connman_bool_t __connman_device_get_blocked(struct connman_device *device) -{ - return device->blocked; -} - static int device_scan(struct connman_device *device) { if (!device->driver || !device->driver->scan) @@ -733,45 +619,9 @@ static int device_scan(struct connman_device *device) if (device->powered == FALSE) return -ENOLINK; - reset_scan_trigger(device); - return device->driver->scan(device); } -int __connman_device_enable_persistent(struct connman_device *device) -{ - int err; - - DBG("device %p", device); - - device->powered_persistent = TRUE; - - __connman_storage_save_device(device); - - err = __connman_device_enable(device); - if (err == 0 || err == -EINPROGRESS) { - device->offlinemode = FALSE; - if (__connman_profile_get_offlinemode() == TRUE) { - __connman_profile_set_offlinemode(FALSE, FALSE); - - __connman_profile_save_default(); - } - } - - return err; -} - -int __connman_device_disable_persistent(struct connman_device *device) -{ - DBG("device %p", device); - - device->powered_persistent = FALSE; - - __connman_storage_save_device(device); - - return __connman_device_disable(device); -} - int __connman_device_disconnect(struct connman_device *device) { GHashTableIter iter; @@ -847,18 +697,15 @@ void __connman_device_cleanup_networks(struct connman_device *device) remove_unavailable_network, NULL); } -connman_bool_t __connman_device_scanning(struct connman_device *device) +connman_bool_t connman_device_get_scanning(struct connman_device *device) { return device->scanning; } void connman_device_reset_scanning(struct connman_device *device) { - device->scanning = FALSE; - g_hash_table_foreach(device->networks, mark_network_available, NULL); - } /** @@ -882,7 +729,7 @@ int connman_device_set_scanning(struct connman_device *device, device->scanning = scanning; if (scanning == TRUE) { - reset_scan_trigger(device); + __connman_technology_scan_started(device); g_hash_table_foreach(device->networks, mark_network_unavailable, NULL); @@ -892,8 +739,7 @@ int connman_device_set_scanning(struct connman_device *device, __connman_device_cleanup_networks(device); - if (device->connections > 0) - return 0; + __connman_technology_scan_stopped(device); __connman_service_auto_connect(); @@ -917,9 +763,6 @@ int connman_device_set_disconnected(struct connman_device *device, device->disconnected = disconnected; - if (disconnected == TRUE) - force_scan_trigger(device); - return 0; } @@ -992,68 +835,6 @@ const char *connman_device_get_string(struct connman_device *device, return NULL; } -static void set_offlinemode(struct connman_device *device, - connman_bool_t offlinemode) -{ - connman_bool_t powered; - - DBG("device %p name %s", device, device->name); - - if (device == NULL) - return; - - device->offlinemode = offlinemode; - - if (device->blocked == TRUE) - return; - - powered = (offlinemode == TRUE) ? FALSE : TRUE; - - if (device->powered == powered) - return; - - if (device->powered_persistent == FALSE) - powered = FALSE; - - set_powered(device, powered); -} - -int __connman_device_set_offlinemode(connman_bool_t offlinemode) -{ - GSList *list; - - DBG("offlinmode %d", offlinemode); - - for (list = device_list; list != NULL; list = list->next) { - struct connman_device *device = list->data; - - set_offlinemode(device, offlinemode); - } - - __connman_notifier_offlinemode(offlinemode); - - return 0; -} - -void __connman_device_increase_connections(struct connman_device *device) -{ - if (device == NULL) - return; - - device->connections++; -} - -void __connman_device_decrease_connections(struct connman_device *device) -{ - if (device == NULL) - return; - - device->connections--; - - if (device->connections == 0) - device->backoff_interval = SCAN_INITIAL_DELAY; -} - /** * connman_device_add_network: * @device: device structure @@ -1075,7 +856,7 @@ int connman_device_add_network(struct connman_device *device, __connman_network_set_device(network, device); - g_hash_table_insert(device->networks, g_strdup(identifier), + g_hash_table_replace(device->networks, g_strdup(identifier), network); return 0; @@ -1171,7 +952,13 @@ static gboolean match_driver(struct connman_device *device, return FALSE; } -static int device_probe(struct connman_device *device) +/** + * connman_device_register: + * @device: device structure + * + * Register device with the system + */ +int connman_device_register(struct connman_device *device) { GSList *list; @@ -1197,32 +984,7 @@ static int device_probe(struct connman_device *device) if (device->driver == NULL) return 0; - return setup_device(device); -} - -static void device_remove(struct connman_device *device) -{ - DBG("device %p name %s", device, device->name); - - if (device->driver == NULL) - return; - - remove_device(device); -} - -/** - * connman_device_register: - * @device: device structure - * - * Register device with the system - */ -int connman_device_register(struct connman_device *device) -{ - __connman_storage_load_device(device); - - device->offlinemode = __connman_profile_get_offlinemode(); - - return device_probe(device); + return __connman_technology_add_device(device); } /** @@ -1233,9 +995,12 @@ int connman_device_register(struct connman_device *device) */ void connman_device_unregister(struct connman_device *device) { - __connman_storage_save_device(device); + DBG("device %p name %s", device, device->name); - device_remove(device); + if (device->driver == NULL) + return; + + remove_device(device); } /** @@ -1282,6 +1047,8 @@ struct connman_device *__connman_device_find_device( int __connman_device_request_scan(enum connman_service_type type) { + connman_bool_t success = FALSE; + int last_err = -ENOSYS; GSList *list; int err; @@ -1294,7 +1061,7 @@ int __connman_device_request_scan(enum connman_service_type type) case CONNMAN_SERVICE_TYPE_GPS: case CONNMAN_SERVICE_TYPE_VPN: case CONNMAN_SERVICE_TYPE_GADGET: - return 0; + return -EOPNOTSUPP; case CONNMAN_SERVICE_TYPE_WIFI: case CONNMAN_SERVICE_TYPE_WIMAX: break; @@ -1311,71 +1078,36 @@ int __connman_device_request_scan(enum connman_service_type type) } err = device_scan(device); - if (err < 0 && err != -EINPROGRESS) { - DBG("err %d", err); - /* XXX maybe only a continue? */ - return err; + if (err == 0 || err == -EALREADY || err == -EINPROGRESS) { + success = TRUE; + } else { + last_err = err; + DBG("device %p err %d", device, err); } } - return 0; -} - -static int set_technology(enum connman_service_type type, connman_bool_t enable) -{ - GSList *list; - int err; - - DBG("type %d enable %d", type, enable); - - switch (type) { - case CONNMAN_SERVICE_TYPE_UNKNOWN: - case CONNMAN_SERVICE_TYPE_SYSTEM: - case CONNMAN_SERVICE_TYPE_GPS: - case CONNMAN_SERVICE_TYPE_VPN: - case CONNMAN_SERVICE_TYPE_GADGET: + if (success == TRUE) return 0; - case CONNMAN_SERVICE_TYPE_ETHERNET: - case CONNMAN_SERVICE_TYPE_WIFI: - case CONNMAN_SERVICE_TYPE_WIMAX: - case CONNMAN_SERVICE_TYPE_BLUETOOTH: - case CONNMAN_SERVICE_TYPE_CELLULAR: - break; - } - - for (list = device_list; list != NULL; list = list->next) { - struct connman_device *device = list->data; - enum connman_service_type service_type = - __connman_device_get_service_type(device); - - if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN && - service_type != type) { - continue; - } - - if (enable == TRUE) - err = __connman_device_enable_persistent(device); - else - err = __connman_device_disable_persistent(device); - if (err < 0 && err != -EINPROGRESS) { - DBG("err %d", err); - /* XXX maybe only a continue? */ - return err; - } - } - - return 0; + return last_err; } -int __connman_device_enable_technology(enum connman_service_type type) +int __connman_device_request_hidden_scan(struct connman_device *device, + const char *ssid, unsigned int ssid_len, + const char *identity, const char *passphrase, + void *user_data) { - return set_technology(type, TRUE); -} + DBG("device %p", device); -int __connman_device_disable_technology(enum connman_service_type type) -{ - return set_technology(type, FALSE); + if (device == NULL || device->driver == NULL || + device->driver->scan_hidden == NULL) + return -EINVAL; + + if (device->scanning == TRUE) + return -EALREADY; + + return device->driver->scan_hidden(device, ssid, ssid_len, + identity, passphrase, user_data); } connman_bool_t __connman_device_isfiltered(const char *devname) @@ -1411,72 +1143,6 @@ nodevice: return FALSE; } -static int device_load(struct connman_device *device) -{ - const char *ident = __connman_profile_active_ident(); - GKeyFile *keyfile; - GError *error = NULL; - gchar *identifier; - connman_bool_t powered; - - DBG("device %p", device); - - keyfile = __connman_storage_open_profile(ident); - if (keyfile == NULL) - return 0; - - identifier = g_strdup_printf("device_%s", device->name); - if (identifier == NULL) - goto done; - - powered = g_key_file_get_boolean(keyfile, identifier, - "Powered", &error); - if (error == NULL) - device->powered_persistent = powered; - g_clear_error(&error); - -done: - g_free(identifier); - - __connman_storage_close_profile(ident, keyfile, FALSE); - - return 0; -} - -static int device_save(struct connman_device *device) -{ - const char *ident = __connman_profile_active_ident(); - GKeyFile *keyfile; - gchar *identifier; - - DBG("device %p", device); - - keyfile = __connman_storage_open_profile(ident); - if (keyfile == NULL) - return 0; - - identifier = g_strdup_printf("device_%s", device->name); - if (identifier == NULL) - goto done; - - g_key_file_set_boolean(keyfile, identifier, - "Powered", device->powered_persistent); - -done: - g_free(identifier); - - __connman_storage_close_profile(ident, keyfile, TRUE); - - return 0; -} - -static struct connman_storage device_storage = { - .name = "device", - .priority = CONNMAN_STORAGE_PRIORITY_LOW, - .device_load = device_load, - .device_save = device_save, -}; - int __connman_device_init(const char *device, const char *nodevice) { DBG(""); @@ -1487,7 +1153,7 @@ int __connman_device_init(const char *device, const char *nodevice) if (nodevice != NULL) nodevice_filter = g_strsplit(nodevice, ",", -1); - return connman_storage_register(&device_storage); + return 0; } void __connman_device_cleanup(void) @@ -1496,6 +1162,4 @@ void __connman_device_cleanup(void) g_strfreev(nodevice_filter); g_strfreev(device_filter); - - connman_storage_unregister(&device_storage); }