Use priority and strength to select known network
authorMarcel Holtmann <marcel@holtmann.org>
Wed, 7 Jan 2009 16:25:25 +0000 (17:25 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Wed, 7 Jan 2009 16:25:25 +0000 (17:25 +0100)
include/network.h
src/device.c
src/network.c

index dd435eb..2232dfd 100644 (file)
@@ -84,6 +84,8 @@ extern const char *connman_network_get_string(struct connman_network *network,
                                                        const char *key);
 extern int connman_network_set_uint8(struct connman_network *network,
                                        const char *key, connman_uint8_t value);
+extern connman_uint8_t connman_network_get_uint8(struct connman_network *network,
+                                                       const char *key);
 extern int connman_network_set_blob(struct connman_network *network,
                        const char *key, const void *data, unsigned int size);
 extern const void *connman_network_get_blob(struct connman_network *network,
index d865ad4..55e07f9 100644 (file)
@@ -976,6 +976,7 @@ int connman_device_set_carrier(struct connman_device *device,
 
 static void connect_known_network(struct connman_device *device)
 {
+       struct connman_network *network = NULL;
        GHashTableIter iter;
        gpointer key, value;
        unsigned int count = 0;
@@ -985,13 +986,37 @@ static void connect_known_network(struct connman_device *device)
        g_hash_table_iter_init(&iter, device->networks);
 
        while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
-               struct connman_network *network = value;
-               int err;
+               connman_uint8_t old_priority, new_priority;
+               connman_uint8_t old_strength, new_strength;
 
                count++;
 
-               if (connman_network_get_remember(network) == FALSE)
+               if (connman_network_get_remember(value) == FALSE)
+                       continue;
+
+               if (network == NULL) {
+                       network = value;
+                       continue;
+               }
+
+               old_priority = connman_network_get_uint8(network, "Priority");
+               new_priority = connman_network_get_uint8(value, "Priority");
+
+               if (new_priority != old_priority) {
+                       if (new_priority > old_priority)
+                               network = value;
                        continue;
+               }
+
+               old_strength = connman_network_get_uint8(network, "Strength");
+               old_strength = connman_network_get_uint8(value, "Strength");
+
+               if (new_strength > old_strength)
+                       network = value;
+       }
+
+       if (network != NULL) {
+               int err;
 
                err = connman_network_connect(network);
                if (err == 0 || err == -EINPROGRESS)
index 2cf4f08..2c6d6b8 100644 (file)
@@ -814,7 +814,9 @@ int connman_network_set_uint8(struct connman_network *network,
 {
        DBG("network %p key %s value %d", network, key, value);
 
-       if (g_str_equal(key, "Strength") == TRUE) {
+       if (g_str_equal(key, "Priority") == TRUE)
+               network->priority = value;
+       else if (g_str_equal(key, "Strength") == TRUE) {
                network->strength = value;
                connman_element_set_static_property(&network->element,
                                        "Strength", DBUS_TYPE_BYTE, &value);
@@ -824,6 +826,26 @@ int connman_network_set_uint8(struct connman_network *network,
 }
 
 /**
+ * connman_network_get_uint8:
+ * @network: network structure
+ * @key: unique identifier
+ *
+ * Get integer value for specific key
+ */
+connman_uint8_t connman_network_get_uint8(struct connman_network *network,
+                                                       const char *key)
+{
+       DBG("network %p key %s", network, key);
+
+       if (g_str_equal(key, "Priority") == TRUE)
+               return network->priority;
+       else if (g_str_equal(key, "Strength") == TRUE)
+               return network->strength;
+
+       return 0;
+}
+
+/**
  * connman_network_set_blob:
  * @network: network structure
  * @key: unique identifier