[PATCH 1/6] devices/wifi: Move AP list utilities to nm-wifi-ap.c



Move three functions for easier code reuse.
---
 src/devices/wifi/nm-device-wifi.c | 111 ++++++--------------------------------
 src/devices/wifi/nm-wifi-ap.c     |  85 +++++++++++++++++++++++++++++
 src/devices/wifi/nm-wifi-ap.h     |   8 +++
 3 files changed, 108 insertions(+), 96 deletions(-)

diff --git a/src/devices/wifi/nm-device-wifi.c b/src/devices/wifi/nm-device-wifi.c
index ebd5317a5..23777ae60 100644
--- a/src/devices/wifi/nm-device-wifi.c
+++ b/src/devices/wifi/nm-device-wifi.c
@@ -682,35 +682,14 @@ check_connection_compatible (NMDevice *device, NMConnection *connection)
        return TRUE;
 }
 
-static NMWifiAP *
-find_first_compatible_ap (NMDeviceWifi *self,
-                          NMConnection *connection,
-                          gboolean allow_unstable_order)
-{
-       GHashTableIter iter;
-       NMWifiAP *ap;
-       NMWifiAP *cand_ap = NULL;
-
-       g_return_val_if_fail (connection != NULL, NULL);
-
-       g_hash_table_iter_init (&iter, NM_DEVICE_WIFI_GET_PRIVATE (self)->aps);
-       while (g_hash_table_iter_next (&iter, NULL, (gpointer) &ap)) {
-               if (!nm_wifi_ap_check_compatible (ap, connection))
-                       continue;
-               if (allow_unstable_order)
-                       return ap;
-               if (!cand_ap || (nm_wifi_ap_get_id (cand_ap) < nm_wifi_ap_get_id (ap)))
-                       cand_ap = ap;
-       }
-       return cand_ap;
-}
-
 static gboolean
 check_connection_available (NMDevice *device,
                             NMConnection *connection,
                             NMDeviceCheckConAvailableFlags flags,
                             const char *specific_object)
 {
+       NMDeviceWifi *self = NM_DEVICE_WIFI (device);
+       NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
        NMSettingWireless *s_wifi;
        const char *mode;
 
@@ -723,7 +702,7 @@ check_connection_available (NMDevice *device,
        if (specific_object) {
                NMWifiAP *ap;
 
-               ap = get_ap_by_path (NM_DEVICE_WIFI (device), specific_object);
+               ap = get_ap_by_path (self, specific_object);
                return ap ? nm_wifi_ap_check_compatible (ap, connection) : FALSE;
        }
 
@@ -747,7 +726,7 @@ check_connection_available (NMDevice *device,
                return TRUE;
 
        /* check at least one AP is compatible with this connection */
-       return !!find_first_compatible_ap (NM_DEVICE_WIFI (device), connection, TRUE);
+       return !!nm_wifi_aps_find_first_compatible (priv->aps, connection, TRUE);
 }
 
 static gboolean
@@ -791,6 +770,7 @@ complete_connection (NMDevice *device,
                      GError **error)
 {
        NMDeviceWifi *self = NM_DEVICE_WIFI (device);
+       NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
        NMSettingWireless *s_wifi;
        const char *setting_mac;
        char *str_ssid = NULL;
@@ -827,7 +807,7 @@ complete_connection (NMDevice *device,
 
                if (!nm_streq0 (mode, NM_SETTING_WIRELESS_MODE_AP)) {
                        /* Find a compatible AP in the scan list */
-                       ap = find_first_compatible_ap (self, connection, FALSE);
+                       ap = nm_wifi_aps_find_first_compatible (priv->aps, connection, FALSE);
 
                        /* If we still don't have an AP, then the WiFI settings needs to be
                         * fully specified by the client.  Might not be able to find an AP
@@ -1012,6 +992,7 @@ can_auto_connect (NMDevice *device,
                   char **specific_object)
 {
        NMDeviceWifi *self = NM_DEVICE_WIFI (device);
+       NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
        NMSettingWireless *s_wifi;
        NMWifiAP *ap;
        const char *method, *mode;
@@ -1043,7 +1024,7 @@ can_auto_connect (NMDevice *device,
                        return FALSE;
        }
 
-       ap = find_first_compatible_ap (self, connection, FALSE);
+       ap = nm_wifi_aps_find_first_compatible (priv->aps, connection, FALSE);
        if (ap) {
                /* All good; connection is usable */
                NM_SET_OUT (specific_object, g_strdup (nm_exported_object_get_path (NM_EXPORTED_OBJECT 
(ap))));
@@ -1053,78 +1034,15 @@ can_auto_connect (NMDevice *device,
        return FALSE;
 }
 
-static int
-ap_id_compare (gconstpointer p_a, gconstpointer p_b, gpointer user_data)
-{
-       guint64 a_id = nm_wifi_ap_get_id (*((NMWifiAP **) p_a));
-       guint64 b_id = nm_wifi_ap_get_id (*((NMWifiAP **) p_b));
-
-       return a_id < b_id ? -1 : (a_id == b_id ? 0 : 1);
-}
-
-static NMWifiAP **
-ap_list_get_sorted (NMDeviceWifi *self, gboolean include_without_ssid)
-{
-       NMDeviceWifiPrivate *priv;
-       NMWifiAP **list;
-       GHashTableIter iter;
-       NMWifiAP *ap;
-       gsize i, n;
-
-       priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
-
-       n = g_hash_table_size (priv->aps);
-       list = g_new (NMWifiAP *, n + 1);
-
-       i = 0;
-       if (n > 0) {
-               g_hash_table_iter_init (&iter, priv->aps);
-               while (g_hash_table_iter_next (&iter, NULL, (gpointer) &ap)) {
-                       nm_assert (i < n);
-                       if (   include_without_ssid
-                           || nm_wifi_ap_get_ssid (ap))
-                               list[i++] = ap;
-               }
-               nm_assert (i <= n);
-               nm_assert (!include_without_ssid || i == n);
-
-               g_qsort_with_data (list,
-                                  i,
-                                  sizeof (gpointer),
-                                  ap_id_compare,
-                                  NULL);
-       }
-       list[i] = NULL;
-       return list;
-}
-
-static const char **
-ap_list_get_sorted_paths (NMDeviceWifi *self, gboolean include_without_ssid)
-{
-       gpointer *list;
-       gsize i, j;
-
-       list = (gpointer *) ap_list_get_sorted (self, include_without_ssid);
-       for (i = 0, j = 0; list[i]; i++) {
-               NMWifiAP *ap = list[i];
-               const char *path;
-
-               /* update @list inplace to hold instead the export-path. */
-               path = nm_exported_object_get_path (NM_EXPORTED_OBJECT (ap));
-               nm_assert (path);
-               list[j++] = (gpointer) path;
-       }
-       return (const char **) list;
-}
-
 static void
 impl_device_wifi_get_access_points (NMDeviceWifi *self,
                                     GDBusMethodInvocation *context)
 {
+       NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
        gs_free const char **list = NULL;
        GVariant *v;
 
-       list = ap_list_get_sorted_paths (self, FALSE);
+       list = nm_wifi_aps_get_sorted_paths (priv->aps, FALSE);
        v = g_variant_new_objv (list, -1);
        g_dbus_method_invocation_return_value (context, g_variant_new_tuple (&v, 1));
 }
@@ -1133,10 +1051,11 @@ static void
 impl_device_wifi_get_all_access_points (NMDeviceWifi *self,
                                         GDBusMethodInvocation *context)
 {
+       NMDeviceWifiPrivate *priv = NM_DEVICE_WIFI_GET_PRIVATE (self);
        gs_free const char **list = NULL;
        GVariant *v;
 
-       list = ap_list_get_sorted_paths (self, TRUE);
+       list = nm_wifi_aps_get_sorted_paths (priv->aps, TRUE);
        v = g_variant_new_objv (list, -1);
        g_dbus_method_invocation_return_value (context, g_variant_new_tuple (&v, 1));
 }
@@ -1621,7 +1540,7 @@ ap_list_dump (gpointer user_data)
                       now_s,
                       priv->last_scan,
                       priv->scheduled_scan_time);
-               list = ap_list_get_sorted (self, TRUE);
+               list = nm_wifi_aps_get_sorted (priv->aps, TRUE);
                for (i = 0; list[i]; i++)
                        _ap_dump (self, LOGL_DEBUG, list[i], "dump", now_s);
        }
@@ -2667,7 +2586,7 @@ act_stage1_prepare (NMDevice *device, NMDeviceStateReason *out_failure_reason)
                if (ap)
                        goto done;
 
-               ap = find_first_compatible_ap (self, connection, FALSE);
+               ap = nm_wifi_aps_find_first_compatible (priv->aps, connection, FALSE);
        }
 
        if (ap) {
@@ -3317,7 +3236,7 @@ get_property (GObject *object, guint prop_id,
                g_value_set_uint (value, priv->capabilities);
                break;
        case PROP_ACCESS_POINTS:
-               list = (char **) ap_list_get_sorted_paths (self, TRUE);
+               list = (char **) nm_wifi_aps_get_sorted_paths (priv->aps, TRUE);
                for (i = 0; list[i]; i++)
                        list[i] = g_strdup (list[i]);
                g_value_take_boxed (value, list);
diff --git a/src/devices/wifi/nm-wifi-ap.c b/src/devices/wifi/nm-wifi-ap.c
index bc823af0a..ed6ad2881 100644
--- a/src/devices/wifi/nm-wifi-ap.c
+++ b/src/devices/wifi/nm-wifi-ap.c
@@ -1430,3 +1430,88 @@ nm_wifi_ap_class_init (NMWifiAPClass *ap_class)
                                                NULL);
 }
 
+/*****************************************************************************/
+
+static int
+ap_id_compare (gconstpointer p_a, gconstpointer p_b, gpointer user_data)
+{
+       guint64 a_id = nm_wifi_ap_get_id (*((NMWifiAP **) p_a));
+       guint64 b_id = nm_wifi_ap_get_id (*((NMWifiAP **) p_b));
+
+       return a_id < b_id ? -1 : (a_id == b_id ? 0 : 1);
+}
+
+NMWifiAP **
+nm_wifi_aps_get_sorted (GHashTable *aps, gboolean include_without_ssid)
+{
+       NMWifiAP **list;
+       GHashTableIter iter;
+       NMWifiAP *ap;
+       gsize i, n;
+
+       n = g_hash_table_size (aps);
+       list = g_new (NMWifiAP *, n + 1);
+
+       i = 0;
+       if (n > 0) {
+               g_hash_table_iter_init (&iter, aps);
+               while (g_hash_table_iter_next (&iter, NULL, (gpointer) &ap)) {
+                       nm_assert (i < n);
+                       if (   include_without_ssid
+                           || nm_wifi_ap_get_ssid (ap))
+                               list[i++] = ap;
+               }
+               nm_assert (i <= n);
+               nm_assert (!include_without_ssid || i == n);
+
+               g_qsort_with_data (list,
+                                  i,
+                                  sizeof (gpointer),
+                                  ap_id_compare,
+                                  NULL);
+       }
+       list[i] = NULL;
+       return list;
+}
+
+const char **
+nm_wifi_aps_get_sorted_paths (GHashTable *aps, gboolean include_without_ssid)
+{
+       gpointer *list;
+       gsize i, j;
+
+       list = (gpointer *) nm_wifi_aps_get_sorted (aps, include_without_ssid);
+       for (i = 0, j = 0; list[i]; i++) {
+               NMWifiAP *ap = list[i];
+               const char *path;
+
+               /* update @list inplace to hold instead the export-path. */
+               path = nm_exported_object_get_path (NM_EXPORTED_OBJECT (ap));
+               nm_assert (path);
+               list[j++] = (gpointer) path;
+       }
+       return (const char **) list;
+}
+
+NMWifiAP *
+nm_wifi_aps_find_first_compatible (GHashTable *aps,
+                                   NMConnection *connection,
+                                   gboolean allow_unstable_order)
+{
+       GHashTableIter iter;
+       NMWifiAP *ap;
+       NMWifiAP *cand_ap = NULL;
+
+       g_return_val_if_fail (connection != NULL, NULL);
+
+       g_hash_table_iter_init (&iter, aps);
+       while (g_hash_table_iter_next (&iter, NULL, (gpointer) &ap)) {
+               if (!nm_wifi_ap_check_compatible (ap, connection))
+                       continue;
+               if (allow_unstable_order)
+                       return ap;
+               if (!cand_ap || (nm_wifi_ap_get_id (cand_ap) < nm_wifi_ap_get_id (ap)))
+                       cand_ap = ap;
+       }
+       return cand_ap;
+}
diff --git a/src/devices/wifi/nm-wifi-ap.h b/src/devices/wifi/nm-wifi-ap.h
index dd5a4ad12..174822312 100644
--- a/src/devices/wifi/nm-wifi-ap.h
+++ b/src/devices/wifi/nm-wifi-ap.h
@@ -95,4 +95,12 @@ const char       *nm_wifi_ap_to_string                (const NMWifiAP *self,
                                                        gulong buf_len,
                                                        gint32 now_s);
 
+NMWifiAP        **nm_wifi_aps_get_sorted              (GHashTable *aps,
+                                                       gboolean include_without_ssid);
+const char      **nm_wifi_aps_get_sorted_paths        (GHashTable *aps,
+                                                       gboolean include_without_ssid);
+NMWifiAP         *nm_wifi_aps_find_first_compatible   (GHashTable *aps,
+                                                       NMConnection *connection,
+                                                       gboolean allow_unstable_order);
+
 #endif /* __NM_WIFI_AP_H__ */
-- 
2.11.0



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]