Re: [PATCH] Ad-hoc channels: patches respin



> 1) lets use "nm_utils_wifi_*" for the function names since they are clearly
> wifi specific.
> 
> 2) for nm_utils_find_next_channel() can you describe the @direction
> argument a bit more?  Something like:
> 
> @direction: whether going downward (0 or less) or upward (1 or more)
> 

Okay.
Fixed in 1_libnm-util_band_channel.patch.

> > patch2 :
> > Allow ad-hoc connections using band/channel - reworked the previous patch
> > to use new libnm-util code.
> > And one addition: when switched to infrastructure, band and channels are
> > set to automatic. (Because compatible checking has been enhanced to
> > compare channels too.)
> 
> For now, lets desensitize the band/channel widgets in the editor when in
> infrastructure mode since the supplicant isn't capable of using those
> values yet.  Have to get around to patching the supplicant.
> 

Do you mean using just sentitive/insensitive instead of show/hide here?

I've made both variants:
2a - previous show/hide concept

2b - do sensitive/insensitive band&channel

I've removed the resetting band&channel to 0 when switching to 
"infrastructure" mode.
My previous concern was that letting it set could prevent NM from connecting 
when looking for compatible connection. (But the check is there just for fake 
AP now.)

> > Jirka
> > 
> > PS:
> > Dan,
> > There are some explicit channels in
> > nm-device-wifi.c:build_supplicant_config for ad-hoc connections.
> > Is it intentional to use just these channels?
> 
> The intention here was to only choose non-overlapping channels (1, 6, 11
> and 13) when automatically picking a channel.  WiFi channels overlap
> since the channel bandwidth is 20MHz, but the channels are only
> separated by 5Mhz.  Putting an AP on channel 2 when something is already
> on channel 1 just increases interference for both since they have a
> 15MHz overlap.
> 
> The 13 is there (even though it overlaps with 11) to ensure that France
> or Japan (I forget which) got a valid channel since previously most of
> the 802.11bg band was illegal in one of those countries.
> 
Agreed. I thought it could be due to interference.
This helps in case of multiple NMs will activate adhoc. However there is still 
a problem if something else use another interfering channel. But of course, 
there's no way to avoid it.

> Thanks!
> Dan

Thanks for the comments.

Jirka
diff --git a/src/connection-editor/page-wireless.c b/src/connection-editor/page-wireless.c
index 078108c..4a9d7f3 100644
--- a/src/connection-editor/page-wireless.c
+++ b/src/connection-editor/page-wireless.c
@@ -17,7 +17,7 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
- * (C) Copyright 2008 Red Hat, Inc.
+ * (C) Copyright 2008 - 2010 Red Hat, Inc.
  */
 
 #include <string.h>
@@ -122,11 +122,11 @@ channel_spin_input_cb (GtkSpinButton *spin, gdouble *new_val, gpointer user_data
 	else
 		int_channel = ceil (channel);
 
-	if (utils_channel_to_freq (int_channel, aband ? "a" : "bg") == -1)
+	if (nm_utils_wifi_channel_to_freq (int_channel, aband ? "a" : "bg") == -1)
 		return GTK_INPUT_ERROR;
 
 	*new_val = channel;
-	return TRUE;
+	return 1;
 }
 
 static gint
@@ -147,23 +147,30 @@ channel_spin_output_cb (GtkSpinButton *spin, gpointer user_data)
 		if (channel == 0)
 			buf = g_strdup (_("default"));
 		else {
-			freq = utils_channel_to_freq (channel, aband ? "a" : "bg");
+			int direction = 0;
+			freq = nm_utils_wifi_channel_to_freq (channel, aband ? "a" : "bg");
 			if (freq == -1) {
-				int direction = 0;
-
 				if (priv->last_channel < channel)
 					direction = 1;
 				else if (priv->last_channel > channel)
 					direction = -1;
-				channel = utils_find_next_channel (channel, direction, aband ? "a" : "bg");
-				freq = utils_channel_to_freq (channel, aband ? "a" : "bg");
+				channel = nm_utils_wifi_find_next_channel (channel, direction, aband ? "a" : "bg");
+				gtk_spin_button_set_value (spin, channel);
+				freq = nm_utils_wifi_channel_to_freq (channel, aband ? "a" : "bg");
 				if (freq == -1) {
 					g_warning ("%s: invalid channel %d!", __func__, channel);
 					gtk_spin_button_set_value (spin, 0);
 					goto out;
 				}
+
 			}
-			buf = g_strdup_printf (_("%u (%u MHz)"), channel, freq);
+			/* Set spin button to zero to go to "default" from the lowest channel */
+			if (direction == -1 && priv->last_channel == channel) {
+				buf = g_strdup_printf (_("default"));
+				gtk_spin_button_set_value (spin, 0);
+				channel = 0;
+			} else
+				buf = g_strdup_printf (_("%u (%u MHz)"), channel, freq);
 		}
 		priv->last_channel = channel;
 	}
@@ -173,7 +180,7 @@ channel_spin_output_cb (GtkSpinButton *spin, gpointer user_data)
 
 out:
 	g_free (buf);
-	return TRUE;
+	return 1;
 }
 
 static void
@@ -195,13 +202,58 @@ band_value_changed_cb (GtkComboBox *box, gpointer user_data)
 		sensitive = FALSE;
  		break;
  	}
-	
+
 	gtk_widget_set_sensitive (GTK_WIDGET (priv->channel), sensitive);
 
 	ce_page_changed (CE_PAGE (self));
 }
 
 static void
+mode_combo_changed_cb (GtkComboBox *combo,
+                       gpointer user_data)
+{
+	CEPageWireless *self = CE_PAGE_WIRELESS (user_data);
+	CEPageWirelessPrivate *priv = CE_PAGE_WIRELESS_GET_PRIVATE (self);
+	CEPage *parent = CE_PAGE (self);
+	GtkWidget *widget;
+	gboolean show;
+
+ 	switch (gtk_combo_box_get_active (GTK_COMBO_BOX (combo))) {
+ 	case 1: /* adhoc */
+		show = TRUE;
+ 		break;
+ 	default: /* infrastructure */
+		show = FALSE;
+ 		break;
+ 	}
+
+	if (show) {
+		widget = glade_xml_get_widget (parent->xml, "wireless_band_label");
+		gtk_widget_show (widget);
+		gtk_widget_show (GTK_WIDGET (priv->band));
+		widget = glade_xml_get_widget (parent->xml, "wireless_channel_label");
+		gtk_widget_show (widget);
+		gtk_widget_show (GTK_WIDGET (priv->channel));
+	} else {
+		widget = glade_xml_get_widget (parent->xml, "wireless_band_label");
+		gtk_widget_hide (widget);
+		gtk_widget_hide (GTK_WIDGET (priv->band));
+		widget = glade_xml_get_widget (parent->xml, "wireless_channel_label");
+		gtk_widget_hide (widget);
+		gtk_widget_hide (GTK_WIDGET (priv->channel));
+	}
+
+	widget = glade_xml_get_widget (parent->xml, "wireless_band_label");
+	gtk_widget_set_sensitive (GTK_WIDGET (widget), show);
+	gtk_widget_set_sensitive (GTK_WIDGET (priv->band), show);
+	widget = glade_xml_get_widget (parent->xml, "wireless_channel_label");
+	gtk_widget_set_sensitive (GTK_WIDGET (widget), show);
+	gtk_widget_set_sensitive (GTK_WIDGET (priv->channel), show);
+
+	ce_page_changed (CE_PAGE (self));
+}
+
+static void
 populate_ui (CEPageWireless *self)
 {
 	CEPageWirelessPrivate *priv = CE_PAGE_WIRELESS_GET_PRIVATE (self);
@@ -251,7 +303,8 @@ populate_ui (CEPageWireless *self)
 	gtk_combo_box_set_active (priv->mode, 0);
 	if (mode && !strcmp (mode, "adhoc"))
 		gtk_combo_box_set_active (priv->mode, 1);
-	g_signal_connect_swapped (priv->mode, "changed", G_CALLBACK (ce_page_changed), self);
+	mode_combo_changed_cb (priv->mode, self);
+	g_signal_connect (priv->mode, "changed", G_CALLBACK (mode_combo_changed_cb), self);
 
 	g_signal_connect (priv->channel, "output",
 	                  G_CALLBACK (channel_spin_output_cb),
@@ -306,17 +359,6 @@ finish_setup (CEPageWireless *self, gpointer unused, GError *error, gpointer use
 
 	populate_ui (self);
 
-	/* Hide widgets we don't yet support */
-	widget = glade_xml_get_widget (parent->xml, "wireless_band_label");
-	gtk_widget_hide (widget);
-	widget = glade_xml_get_widget (parent->xml, "wireless_band");
-	gtk_widget_hide (widget);
-
-	widget = glade_xml_get_widget (parent->xml, "wireless_channel_label");
-	gtk_widget_hide (widget);
-	widget = glade_xml_get_widget (parent->xml, "wireless_channel");
-	gtk_widget_hide (widget);
-
 	widget = glade_xml_get_widget (parent->xml, "wireless_tx_power_label");
 	gtk_widget_hide (widget);
 	widget = glade_xml_get_widget (parent->xml, "wireless_tx_power_hbox");
diff --git a/src/utils/utils.c b/src/utils/utils.c
index 227c382..6d4f33e 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -196,152 +196,6 @@ utils_get_device_description (NMDevice *device)
 	return description;
 }
 
-struct cf_pair {
-	guint32 chan;
-	guint32 freq;
-};
-
-static struct cf_pair a_table[] = {
-	/* A band */
-	{  7, 5035 },
-	{  8, 5040 },
-	{  9, 5045 },
-	{ 11, 5055 },
-	{ 12, 5060 },
-	{ 16, 5080 },
-	{ 34, 5170 },
-	{ 36, 5180 },
-	{ 38, 5190 },
-	{ 40, 5200 },
-	{ 42, 5210 },
-	{ 44, 5220 },
-	{ 46, 5230 },
-	{ 48, 5240 },
-	{ 50, 5250 },
-	{ 52, 5260 },
-	{ 56, 5280 },
-	{ 58, 5290 },
-	{ 60, 5300 },
-	{ 64, 5320 },
-	{ 100, 5500 },
-	{ 104, 5520 },
-	{ 108, 5540 },
-	{ 112, 5560 },
-	{ 116, 5580 },
-	{ 120, 5600 },
-	{ 124, 5620 },
-	{ 128, 5640 },
-	{ 132, 5660 },
-	{ 136, 5680 },
-	{ 140, 5700 },
-	{ 149, 5745 },
-	{ 152, 5760 },
-	{ 153, 5765 },
-	{ 157, 5785 },
-	{ 160, 5800 },
-	{ 161, 5805 },
-	{ 165, 5825 },
-	{ 183, 4915 },
-	{ 184, 4920 },
-	{ 185, 4925 },
-	{ 187, 4935 },
-	{ 188, 4945 },
-	{ 192, 4960 },
-	{ 196, 4980 },
-	{ 0, -1 }
-};
-
-static struct cf_pair bg_table[] = {
-	/* B/G band */
-	{ 1, 2412 },
-	{ 2, 2417 },
-	{ 3, 2422 },
-	{ 4, 2427 },
-	{ 5, 2432 },
-	{ 6, 2437 },
-	{ 7, 2442 },
-	{ 8, 2447 },
-	{ 9, 2452 },
-	{ 10, 2457 },
-	{ 11, 2462 },
-	{ 12, 2467 },
-	{ 13, 2472 },
-	{ 14, 2484 },
-	{ 0, -1 }
-};
-
-guint32
-utils_freq_to_channel (guint32 freq)
-{
-	int i = 0;
-
-	while (a_table[i].chan && (a_table[i].freq != freq))
-		i++;
-	if (a_table[i].chan)
-		return a_table[i].chan;
-
-	i = 0;
-	while (bg_table[i].chan && (bg_table[i].freq != freq))
-		i++;
-	return bg_table[i].chan;
-}
-
-guint32
-utils_channel_to_freq (guint32 channel, char *band)
-{
-	int i = 0;
-
-	if (!strcmp (band, "a")) {
-		while (a_table[i].chan && (a_table[i].chan != channel))
-			i++;
-		return a_table[i].freq;
-	} else if (!strcmp (band, "bg")) {
-		while (bg_table[i].chan && (bg_table[i].chan != channel))
-			i++;
-		return bg_table[i].freq;
-	}
-
-	return 0;
-}
-
-guint32
-utils_find_next_channel (guint32 channel, int direction, char *band)
-{
-	size_t a_size = sizeof (a_table) / sizeof (struct cf_pair);
-	size_t bg_size = sizeof (bg_table) / sizeof (struct cf_pair);
-	struct cf_pair *pair = NULL;
-
-	if (!strcmp (band, "a")) {
-		if (channel < a_table[0].chan)
-			return a_table[0].chan;
-		if (channel > a_table[a_size - 2].chan)
-			return a_table[a_size - 2].chan;
-		pair = &a_table[0];
-	} else if (!strcmp (band, "bg")) {
-		if (channel < bg_table[0].chan)
-			return bg_table[0].chan;
-		if (channel > bg_table[bg_size - 2].chan)
-			return bg_table[bg_size - 2].chan;
-		pair = &bg_table[0];
-	} else {
-		g_assert_not_reached ();
-		return 0;
-	}
-
-	while (pair->chan) {
-		if (channel == pair->chan)
-			return channel;
-		if ((channel < (pair+1)->chan) && (channel > pair->chan)) {
-			if (direction > 0)	
-				return (pair+1)->chan;
-			else
-				return pair->chan;
-		}
-		pair++;
-	}
-	return 0;
-}
-
 /*
  * utils_ether_addr_valid
  *
@@ -387,7 +241,7 @@ utils_check_ap_compatible (NMAccessPoint *ap,
 	const char *setting_mode;
 	const char *setting_band;
 	NM80211Mode ap_mode;
-	guint32 freq;
+	guint32 freq, channel;
 
 	g_return_val_if_fail (NM_IS_ACCESS_POINT (ap), FALSE);
 	g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
@@ -424,25 +278,31 @@ utils_check_ap_compatible (NMAccessPoint *ap,
 	setting_band = nm_setting_wireless_get_band (s_wireless);
 	if (setting_band) {
 		if (!strcmp (setting_band, "a")) {
-			if (freq < 5170 || freq > 5825)
+			if (freq < 4915 || freq > 5825)
 				return FALSE;
 		} else if (!strcmp (setting_band, "bg")) {
-			if (freq < 2412 || freq > 2472)
+			if (freq < 2412 || freq > 2484)
 				return FALSE;
 		}
 	}
 
-	// FIXME: channel check
+	channel = nm_setting_wireless_get_channel (s_wireless);
+	if (channel) {
+		guint32 ap_chan = nm_utils_wifi_freq_to_channel (freq);
+
+		if (channel != ap_chan)
+			return FALSE;
+	}
 
 	s_wireless_sec = (NMSettingWirelessSecurity *) nm_connection_get_setting (connection,
-															    NM_TYPE_SETTING_WIRELESS_SECURITY);
+	                                                                          NM_TYPE_SETTING_WIRELESS_SECURITY);
 
 	return nm_setting_wireless_ap_security_compatible (s_wireless,
-											 s_wireless_sec,
-											 nm_access_point_get_flags (ap),
-											 nm_access_point_get_wpa_flags (ap),
-											 nm_access_point_get_rsn_flags (ap),
-											 nm_access_point_get_mode (ap));
+	                                                   s_wireless_sec,
+	                                                   nm_access_point_get_flags (ap),
+	                                                   nm_access_point_get_wpa_flags (ap),
+	                                                   nm_access_point_get_rsn_flags (ap),
+	                                                   nm_access_point_get_mode (ap));
 }
 
 static gboolean
diff --git a/src/connection-editor/page-wireless.c b/src/connection-editor/page-wireless.c
index 078108c..2358eb0 100644
--- a/src/connection-editor/page-wireless.c
+++ b/src/connection-editor/page-wireless.c
@@ -17,7 +17,7 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
- * (C) Copyright 2008 Red Hat, Inc.
+ * (C) Copyright 2008 - 2010 Red Hat, Inc.
  */
 
 #include <string.h>
@@ -122,11 +122,11 @@ channel_spin_input_cb (GtkSpinButton *spin, gdouble *new_val, gpointer user_data
 	else
 		int_channel = ceil (channel);
 
-	if (utils_channel_to_freq (int_channel, aband ? "a" : "bg") == -1)
+	if (nm_utils_wifi_channel_to_freq (int_channel, aband ? "a" : "bg") == -1)
 		return GTK_INPUT_ERROR;
 
 	*new_val = channel;
-	return TRUE;
+	return 1;
 }
 
 static gint
@@ -147,23 +147,30 @@ channel_spin_output_cb (GtkSpinButton *spin, gpointer user_data)
 		if (channel == 0)
 			buf = g_strdup (_("default"));
 		else {
-			freq = utils_channel_to_freq (channel, aband ? "a" : "bg");
+			int direction = 0;
+			freq = nm_utils_wifi_channel_to_freq (channel, aband ? "a" : "bg");
 			if (freq == -1) {
-				int direction = 0;
-
 				if (priv->last_channel < channel)
 					direction = 1;
 				else if (priv->last_channel > channel)
 					direction = -1;
-				channel = utils_find_next_channel (channel, direction, aband ? "a" : "bg");
-				freq = utils_channel_to_freq (channel, aband ? "a" : "bg");
+				channel = nm_utils_wifi_find_next_channel (channel, direction, aband ? "a" : "bg");
+				gtk_spin_button_set_value (spin, channel);
+				freq = nm_utils_wifi_channel_to_freq (channel, aband ? "a" : "bg");
 				if (freq == -1) {
 					g_warning ("%s: invalid channel %d!", __func__, channel);
 					gtk_spin_button_set_value (spin, 0);
 					goto out;
 				}
+
 			}
-			buf = g_strdup_printf (_("%u (%u MHz)"), channel, freq);
+			/* Set spin button to zero to go to "default" from the lowest channel */
+			if (direction == -1 && priv->last_channel == channel) {
+				buf = g_strdup_printf (_("default"));
+				gtk_spin_button_set_value (spin, 0);
+				channel = 0;
+			} else
+				buf = g_strdup_printf (_("%u (%u MHz)"), channel, freq);
 		}
 		priv->last_channel = channel;
 	}
@@ -173,7 +180,7 @@ channel_spin_output_cb (GtkSpinButton *spin, gpointer user_data)
 
 out:
 	g_free (buf);
-	return TRUE;
+	return 1;
 }
 
 static void
@@ -195,7 +202,36 @@ band_value_changed_cb (GtkComboBox *box, gpointer user_data)
 		sensitive = FALSE;
  		break;
  	}
-	
+
+	gtk_widget_set_sensitive (GTK_WIDGET (priv->channel), sensitive);
+
+	ce_page_changed (CE_PAGE (self));
+}
+
+static void
+mode_combo_changed_cb (GtkComboBox *combo,
+                       gpointer user_data)
+{
+	CEPageWireless *self = CE_PAGE_WIRELESS (user_data);
+	CEPageWirelessPrivate *priv = CE_PAGE_WIRELESS_GET_PRIVATE (self);
+	CEPage *parent = CE_PAGE (self);
+	GtkWidget *widget;
+	gboolean sensitive;
+
+ 	switch (gtk_combo_box_get_active (GTK_COMBO_BOX (combo))) {
+ 	case 1: /* adhoc */
+		sensitive = TRUE;
+ 		break;
+ 	default: /* infrastructure */
+		sensitive = FALSE;
+ 		break;
+ 	}
+
+	widget = glade_xml_get_widget (parent->xml, "wireless_band_label");
+	gtk_widget_set_sensitive (GTK_WIDGET (widget), sensitive);
+	gtk_widget_set_sensitive (GTK_WIDGET (priv->band), sensitive);
+	widget = glade_xml_get_widget (parent->xml, "wireless_channel_label");
+	gtk_widget_set_sensitive (GTK_WIDGET (widget), sensitive);
 	gtk_widget_set_sensitive (GTK_WIDGET (priv->channel), sensitive);
 
 	ce_page_changed (CE_PAGE (self));
@@ -251,7 +287,8 @@ populate_ui (CEPageWireless *self)
 	gtk_combo_box_set_active (priv->mode, 0);
 	if (mode && !strcmp (mode, "adhoc"))
 		gtk_combo_box_set_active (priv->mode, 1);
-	g_signal_connect_swapped (priv->mode, "changed", G_CALLBACK (ce_page_changed), self);
+	mode_combo_changed_cb (priv->mode, self);
+	g_signal_connect (priv->mode, "changed", G_CALLBACK (mode_combo_changed_cb), self);
 
 	g_signal_connect (priv->channel, "output",
 	                  G_CALLBACK (channel_spin_output_cb),
@@ -264,10 +301,12 @@ populate_ui (CEPageWireless *self)
 	if (band) {
 		if (!strcmp (band ? band : "", "a")) {
 			band_idx = 1;
-			gtk_widget_set_sensitive (GTK_WIDGET (priv->channel), TRUE);
+			if (gtk_combo_box_get_active (priv->mode) == 1)
+				gtk_widget_set_sensitive (GTK_WIDGET (priv->channel), TRUE);
 		} else if (!strcmp (band ? band : "", "bg")) {
 			band_idx = 2;
-			gtk_widget_set_sensitive (GTK_WIDGET (priv->channel), TRUE);
+			if (gtk_combo_box_get_active (priv->mode) == 1)
+				gtk_widget_set_sensitive (GTK_WIDGET (priv->channel), TRUE);
 		}
 	}
 
@@ -306,17 +345,6 @@ finish_setup (CEPageWireless *self, gpointer unused, GError *error, gpointer use
 
 	populate_ui (self);
 
-	/* Hide widgets we don't yet support */
-	widget = glade_xml_get_widget (parent->xml, "wireless_band_label");
-	gtk_widget_hide (widget);
-	widget = glade_xml_get_widget (parent->xml, "wireless_band");
-	gtk_widget_hide (widget);
-
-	widget = glade_xml_get_widget (parent->xml, "wireless_channel_label");
-	gtk_widget_hide (widget);
-	widget = glade_xml_get_widget (parent->xml, "wireless_channel");
-	gtk_widget_hide (widget);
-
 	widget = glade_xml_get_widget (parent->xml, "wireless_tx_power_label");
 	gtk_widget_hide (widget);
 	widget = glade_xml_get_widget (parent->xml, "wireless_tx_power_hbox");
diff --git a/src/utils/utils.c b/src/utils/utils.c
index 227c382..6d4f33e 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -196,152 +196,6 @@ utils_get_device_description (NMDevice *device)
 	return description;
 }
 
-struct cf_pair {
-	guint32 chan;
-	guint32 freq;
-};
-
-static struct cf_pair a_table[] = {
-	/* A band */
-	{  7, 5035 },
-	{  8, 5040 },
-	{  9, 5045 },
-	{ 11, 5055 },
-	{ 12, 5060 },
-	{ 16, 5080 },
-	{ 34, 5170 },
-	{ 36, 5180 },
-	{ 38, 5190 },
-	{ 40, 5200 },
-	{ 42, 5210 },
-	{ 44, 5220 },
-	{ 46, 5230 },
-	{ 48, 5240 },
-	{ 50, 5250 },
-	{ 52, 5260 },
-	{ 56, 5280 },
-	{ 58, 5290 },
-	{ 60, 5300 },
-	{ 64, 5320 },
-	{ 100, 5500 },
-	{ 104, 5520 },
-	{ 108, 5540 },
-	{ 112, 5560 },
-	{ 116, 5580 },
-	{ 120, 5600 },
-	{ 124, 5620 },
-	{ 128, 5640 },
-	{ 132, 5660 },
-	{ 136, 5680 },
-	{ 140, 5700 },
-	{ 149, 5745 },
-	{ 152, 5760 },
-	{ 153, 5765 },
-	{ 157, 5785 },
-	{ 160, 5800 },
-	{ 161, 5805 },
-	{ 165, 5825 },
-	{ 183, 4915 },
-	{ 184, 4920 },
-	{ 185, 4925 },
-	{ 187, 4935 },
-	{ 188, 4945 },
-	{ 192, 4960 },
-	{ 196, 4980 },
-	{ 0, -1 }
-};
-
-static struct cf_pair bg_table[] = {
-	/* B/G band */
-	{ 1, 2412 },
-	{ 2, 2417 },
-	{ 3, 2422 },
-	{ 4, 2427 },
-	{ 5, 2432 },
-	{ 6, 2437 },
-	{ 7, 2442 },
-	{ 8, 2447 },
-	{ 9, 2452 },
-	{ 10, 2457 },
-	{ 11, 2462 },
-	{ 12, 2467 },
-	{ 13, 2472 },
-	{ 14, 2484 },
-	{ 0, -1 }
-};
-
-guint32
-utils_freq_to_channel (guint32 freq)
-{
-	int i = 0;
-
-	while (a_table[i].chan && (a_table[i].freq != freq))
-		i++;
-	if (a_table[i].chan)
-		return a_table[i].chan;
-
-	i = 0;
-	while (bg_table[i].chan && (bg_table[i].freq != freq))
-		i++;
-	return bg_table[i].chan;
-}
-
-guint32
-utils_channel_to_freq (guint32 channel, char *band)
-{
-	int i = 0;
-
-	if (!strcmp (band, "a")) {
-		while (a_table[i].chan && (a_table[i].chan != channel))
-			i++;
-		return a_table[i].freq;
-	} else if (!strcmp (band, "bg")) {
-		while (bg_table[i].chan && (bg_table[i].chan != channel))
-			i++;
-		return bg_table[i].freq;
-	}
-
-	return 0;
-}
-
-guint32
-utils_find_next_channel (guint32 channel, int direction, char *band)
-{
-	size_t a_size = sizeof (a_table) / sizeof (struct cf_pair);
-	size_t bg_size = sizeof (bg_table) / sizeof (struct cf_pair);
-	struct cf_pair *pair = NULL;
-
-	if (!strcmp (band, "a")) {
-		if (channel < a_table[0].chan)
-			return a_table[0].chan;
-		if (channel > a_table[a_size - 2].chan)
-			return a_table[a_size - 2].chan;
-		pair = &a_table[0];
-	} else if (!strcmp (band, "bg")) {
-		if (channel < bg_table[0].chan)
-			return bg_table[0].chan;
-		if (channel > bg_table[bg_size - 2].chan)
-			return bg_table[bg_size - 2].chan;
-		pair = &bg_table[0];
-	} else {
-		g_assert_not_reached ();
-		return 0;
-	}
-
-	while (pair->chan) {
-		if (channel == pair->chan)
-			return channel;
-		if ((channel < (pair+1)->chan) && (channel > pair->chan)) {
-			if (direction > 0)	
-				return (pair+1)->chan;
-			else
-				return pair->chan;
-		}
-		pair++;
-	}
-	return 0;
-}
-
 /*
  * utils_ether_addr_valid
  *
@@ -387,7 +241,7 @@ utils_check_ap_compatible (NMAccessPoint *ap,
 	const char *setting_mode;
 	const char *setting_band;
 	NM80211Mode ap_mode;
-	guint32 freq;
+	guint32 freq, channel;
 
 	g_return_val_if_fail (NM_IS_ACCESS_POINT (ap), FALSE);
 	g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
@@ -424,25 +278,31 @@ utils_check_ap_compatible (NMAccessPoint *ap,
 	setting_band = nm_setting_wireless_get_band (s_wireless);
 	if (setting_band) {
 		if (!strcmp (setting_band, "a")) {
-			if (freq < 5170 || freq > 5825)
+			if (freq < 4915 || freq > 5825)
 				return FALSE;
 		} else if (!strcmp (setting_band, "bg")) {
-			if (freq < 2412 || freq > 2472)
+			if (freq < 2412 || freq > 2484)
 				return FALSE;
 		}
 	}
 
-	// FIXME: channel check
+	channel = nm_setting_wireless_get_channel (s_wireless);
+	if (channel) {
+		guint32 ap_chan = nm_utils_wifi_freq_to_channel (freq);
+
+		if (channel != ap_chan)
+			return FALSE;
+	}
 
 	s_wireless_sec = (NMSettingWirelessSecurity *) nm_connection_get_setting (connection,
-															    NM_TYPE_SETTING_WIRELESS_SECURITY);
+	                                                                          NM_TYPE_SETTING_WIRELESS_SECURITY);
 
 	return nm_setting_wireless_ap_security_compatible (s_wireless,
-											 s_wireless_sec,
-											 nm_access_point_get_flags (ap),
-											 nm_access_point_get_wpa_flags (ap),
-											 nm_access_point_get_rsn_flags (ap),
-											 nm_access_point_get_mode (ap));
+	                                                   s_wireless_sec,
+	                                                   nm_access_point_get_flags (ap),
+	                                                   nm_access_point_get_wpa_flags (ap),
+	                                                   nm_access_point_get_rsn_flags (ap),
+	                                                   nm_access_point_get_mode (ap));
 }
 
 static gboolean
diff --git a/libnm-util/Makefile.am b/libnm-util/Makefile.am
index ddc6840..3233d40 100644
--- a/libnm-util/Makefile.am
+++ b/libnm-util/Makefile.am
@@ -59,7 +59,7 @@ libnm_util_la_SOURCES=			\
 libnm_util_la_LIBADD = $(GLIB_LIBS) $(DBUS_LIBS) $(UUID_LIBS)
 
 libnm_util_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libnm-util.ver \
-	-version-info "4:4:3"
+	-version-info "5:0:4"
 
 if WITH_GNUTLS
 libnm_util_la_SOURCES += crypto_gnutls.c
diff --git a/libnm-util/libnm-util.ver b/libnm-util/libnm-util.ver
index ed1d8a4..2c4919c 100644
--- a/libnm-util/libnm-util.ver
+++ b/libnm-util/libnm-util.ver
@@ -365,6 +365,10 @@ global:
 	nm_utils_ssid_to_utf8;
 	nm_utils_uuid_generate;
 	nm_utils_uuid_generate_from_string;
+	nm_utils_wifi_freq_to_channel;
+	nm_utils_wifi_channel_to_freq;
+	nm_utils_wifi_find_next_channel;
+	nm_utils_wifi_is_channel_valid;
 	nm_ip4_address_compare;
 	nm_ip4_address_dup;
 	nm_ip4_address_get_address;
diff --git a/libnm-util/nm-setting-wireless.c b/libnm-util/nm-setting-wireless.c
index 9213429..44d010f 100644
--- a/libnm-util/nm-setting-wireless.c
+++ b/libnm-util/nm-setting-wireless.c
@@ -19,7 +19,7 @@
  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA 02110-1301 USA.
  *
- * (C) Copyright 2007 - 2008 Red Hat, Inc.
+ * (C) Copyright 2007 - 2010 Red Hat, Inc.
  * (C) Copyright 2007 - 2008 Novell, Inc.
  */
 
@@ -480,31 +480,11 @@ verify (NMSetting *setting, GSList *all_settings, GError **error)
 	}
 
 	if (priv->channel) {
-		if (!strcmp (priv->band, "a")) {
-			int i;
-			int valid_channels[] = { 7, 8, 9, 11, 12, 16, 34, 36, 40, 44, 48,
-			                         52, 56, 60, 64, 100, 104, 108, 112, 116,
-			                         120, 124, 128, 132, 136, 140, 149, 153,
-			                         157, 161, 165, 183, 184, 185, 187, 188,
-			                         192, 196, 0 };
-
-			for (i = 0; valid_channels[i]; i++) {
-				if (priv->channel == valid_channels[i])
-					break;
-			}
-
-			if (valid_channels[i] == 0) {
-				g_set_error (error,
-				             NM_SETTING_WIRELESS_ERROR,
-				             NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY,
-				             NM_SETTING_WIRELESS_CHANNEL);
-				return FALSE;
-			}
-		} else if (!strcmp (priv->band, "bg") && priv->channel > 14) {
-				g_set_error (error,
-				             NM_SETTING_WIRELESS_ERROR,
-				             NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY,
-				             NM_SETTING_WIRELESS_CHANNEL);
+		if (!nm_utils_wifi_is_channel_valid (priv->channel, priv->band)) {
+			g_set_error (error,
+			             NM_SETTING_WIRELESS_ERROR,
+			             NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY,
+			             NM_SETTING_WIRELESS_CHANNEL);
 			return FALSE;
 		}
 	}
diff --git a/libnm-util/nm-utils.c b/libnm-util/nm-utils.c
index 5a8a880..8c76b6a 100644
--- a/libnm-util/nm-utils.c
+++ b/libnm-util/nm-utils.c
@@ -117,7 +117,7 @@ static const struct IsoLangToEncodings isoLangEntries2[] =
 	/* Arabic */
 	{ "ar",		{"iso-8859-6",	"windows-1256",	NULL} },
 
-	/* Balitc */
+	/* Baltic */
 	{ "et",		{"iso-8859-4",	"windows-1257",	NULL} },	/* Estonian */
 	{ "lt",		{"iso-8859-4",	"windows-1257",	NULL} },	/* Lithuanian */
 	{ "lv",		{"iso-8859-4",	"windows-1257",	NULL} },	/* Latvian */
@@ -2102,3 +2102,212 @@ out:
 	return ret;
 }
 
+/* Band, channel/frequency stuff for wireless */
+struct cf_pair {
+	guint32 chan;
+	guint32 freq;
+};
+
+static struct cf_pair a_table[] = {
+	/* A band */
+	{  7, 5035 },
+	{  8, 5040 },
+	{  9, 5045 },
+	{ 11, 5055 },
+	{ 12, 5060 },
+	{ 16, 5080 },
+	{ 34, 5170 },
+	{ 36, 5180 },
+	{ 38, 5190 },
+	{ 40, 5200 },
+	{ 42, 5210 },
+	{ 44, 5220 },
+	{ 46, 5230 },
+	{ 48, 5240 },
+	{ 50, 5250 },
+	{ 52, 5260 },
+	{ 56, 5280 },
+	{ 58, 5290 },
+	{ 60, 5300 },
+	{ 64, 5320 },
+	{ 100, 5500 },
+	{ 104, 5520 },
+	{ 108, 5540 },
+	{ 112, 5560 },
+	{ 116, 5580 },
+	{ 120, 5600 },
+	{ 124, 5620 },
+	{ 128, 5640 },
+	{ 132, 5660 },
+	{ 136, 5680 },
+	{ 140, 5700 },
+	{ 149, 5745 },
+	{ 152, 5760 },
+	{ 153, 5765 },
+	{ 157, 5785 },
+	{ 160, 5800 },
+	{ 161, 5805 },
+	{ 165, 5825 },
+	{ 183, 4915 },
+	{ 184, 4920 },
+	{ 185, 4925 },
+	{ 187, 4935 },
+	{ 188, 4945 },
+	{ 192, 4960 },
+	{ 196, 4980 },
+	{ 0, -1 }
+};
+
+static struct cf_pair bg_table[] = {
+	/* B/G band */
+	{ 1, 2412 },
+	{ 2, 2417 },
+	{ 3, 2422 },
+	{ 4, 2427 },
+	{ 5, 2432 },
+	{ 6, 2437 },
+	{ 7, 2442 },
+	{ 8, 2447 },
+	{ 9, 2452 },
+	{ 10, 2457 },
+	{ 11, 2462 },
+	{ 12, 2467 },
+	{ 13, 2472 },
+	{ 14, 2484 },
+	{ 0, -1 }
+};
+
+/**
+ * nm_utils_wifi_freq_to_channel:
+ * @freq: frequency
+ *
+ * Utility function to translate a WiFi frequency to its corresponding channel.
+ *
+ * Returns: the channel represented by the frequency or 0
+ **/
+guint32
+nm_utils_wifi_freq_to_channel (guint32 freq)
+{
+	int i = 0;
+
+	if (freq > 4900) {
+		while (a_table[i].chan && (a_table[i].freq != freq))
+			i++;
+		return a_table[i].chan;
+	} else {
+		while (bg_table[i].chan && (bg_table[i].freq != freq))
+			i++;
+		return bg_table[i].chan;
+	}
+
+	return 0;
+}
+
+/**
+ * nm_utils_wifi_channel_to_freq:
+ * @channel: channel
+ * @band: frequency band for wireless ("a" or "bg")
+ *
+ * Utility function to translate a WiFi channel to its corresponding frequency.
+ *
+ * Returns: the frequency represented by the channel of the band,
+ *          or -1 when the freq is invalid, or 0 when the band
+ *          is invalid
+ **/
+guint32
+nm_utils_wifi_channel_to_freq (guint32 channel, const char *band)
+{
+	int i = 0;
+
+	if (!strcmp (band, "a")) {
+		while (a_table[i].chan && (a_table[i].chan != channel))
+			i++;
+		return a_table[i].freq;
+	} else if (!strcmp (band, "bg")) {
+		while (bg_table[i].chan && (bg_table[i].chan != channel))
+			i++;
+		return bg_table[i].freq;
+	}
+
+	return 0;
+}
+
+/**
+ * nm_utils_wifi_find_next_channel:
+ * @channel: current channel
+ * @direction: whether going downward (0 or less) or upward (1 or more)
+ * @band: frequency band for wireless ("a" or "bg")
+ *
+ * Utility function to find out next/previous WiFi channel for a channel.
+ *
+ * Returns: the next channel in the specified direction or 0
+ **/
+guint32
+nm_utils_wifi_find_next_channel (guint32 channel, int direction, char *band)
+{
+	size_t a_size = sizeof (a_table) / sizeof (struct cf_pair);
+	size_t bg_size = sizeof (bg_table) / sizeof (struct cf_pair);
+	struct cf_pair *pair = NULL;
+
+	if (!strcmp (band, "a")) {
+		if (channel < a_table[0].chan)
+			return a_table[0].chan;
+		if (channel > a_table[a_size - 2].chan)
+			return a_table[a_size - 2].chan;
+		pair = &a_table[0];
+	} else if (!strcmp (band, "bg")) {
+		if (channel < bg_table[0].chan)
+			return bg_table[0].chan;
+		if (channel > bg_table[bg_size - 2].chan)
+			return bg_table[bg_size - 2].chan;
+		pair = &bg_table[0];
+	} else {
+		g_assert_not_reached ();
+		return 0;
+	}
+
+	while (pair->chan) {
+		if (channel == pair->chan)
+			return channel;
+		if ((channel < (pair+1)->chan) && (channel > pair->chan)) {
+			if (direction > 0)	
+				return (pair+1)->chan;
+			else
+				return pair->chan;
+		}
+		pair++;
+	}
+	return 0;
+}
+
+/**
+ * nm_utils_wifi_is_channel_valid:
+ * @channel: channel
+ * @band: frequency band for wireless ("a" or "bg")
+ *
+ * Utility function to verify WiFi channel validity.
+ *
+ * Returns: TRUE or FALSE
+ **/
+gboolean
+nm_utils_wifi_is_channel_valid (guint32 channel, const char *band)
+{
+	struct cf_pair *table = NULL;
+	int i = 0;
+
+	if (!strcmp (band, "a"))
+		table = a_table;
+	else if (!strcmp (band, "bg"))
+		table = bg_table;
+	else
+		return FALSE;
+
+	while (table[i].chan && (table[i].chan != channel))
+		i++;
+
+	if (table[i].chan != 0)
+		return TRUE;
+	else
+		return FALSE;
+}
+
diff --git a/libnm-util/nm-utils.h b/libnm-util/nm-utils.h
index 8308a23..cac0a75 100644
--- a/libnm-util/nm-utils.h
+++ b/libnm-util/nm-utils.h
@@ -211,4 +211,9 @@ GByteArray *nm_utils_rsa_key_encrypt (const GByteArray *data,
                                       char **out_password,
                                       GError **error);
 
+guint32 nm_utils_wifi_freq_to_channel (guint32 freq);
+guint32 nm_utils_wifi_channel_to_freq (guint32 channel, const char *band);
+guint32 nm_utils_wifi_find_next_channel (guint32 channel, int direction, char *band);
+gboolean nm_utils_wifi_is_channel_valid (guint32 channel, const char *band);
+
 #endif /* NM_UTILS_H */
diff --git a/src/nm-wifi-ap.c b/src/nm-wifi-ap.c
index c7b5d8a..8a7e4e8 100644
--- a/src/nm-wifi-ap.c
+++ b/src/nm-wifi-ap.c
@@ -15,7 +15,7 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
- * Copyright (C) 2004 - 2008 Red Hat, Inc.
+ * Copyright (C) 2004 - 2010 Red Hat, Inc.
  * Copyright (C) 2006 - 2008 Novell, Inc.
  */
 
@@ -604,7 +604,7 @@ nm_ap_new_fake_from_connection (NMConnection *connection)
 	channel = nm_setting_wireless_get_channel (s_wireless);
 
 	if (band && channel) {
-		guint32 freq = channel_to_freq (channel, band);
+		guint32 freq = nm_utils_wifi_channel_to_freq (channel, band);
 
 		if (freq == 0)
 			goto error;
@@ -1256,21 +1256,21 @@ nm_ap_check_compatible (NMAccessPoint *self,
 
 	channel = nm_setting_wireless_get_channel (s_wireless);
 	if (channel) {
-		guint32 ap_chan = freq_to_channel (priv->freq);
+		guint32 ap_chan = nm_utils_wifi_freq_to_channel (priv->freq);
 
 		if (channel != ap_chan)
 			return FALSE;
 	}
 
 	s_wireless_sec = (NMSettingWirelessSecurity *) nm_connection_get_setting (connection,
-															    NM_TYPE_SETTING_WIRELESS_SECURITY);
+	                                                                          NM_TYPE_SETTING_WIRELESS_SECURITY);
 
 	return nm_setting_wireless_ap_security_compatible (s_wireless,
-											 s_wireless_sec,
-											 nm_ap_get_flags (self),
-											 nm_ap_get_wpa_flags (self),
-											 nm_ap_get_rsn_flags (self),
-											 nm_ap_get_mode (self));
+	                                                   s_wireless_sec,
+	                                                   nm_ap_get_flags (self),
+	                                                   nm_ap_get_wpa_flags (self),
+	                                                   nm_ap_get_rsn_flags (self),
+	                                                   nm_ap_get_mode (self));
 }
 
 static gboolean
@@ -1364,114 +1364,3 @@ nm_ap_match_in_list (NMAccessPoint *find_ap,
 	return NULL;
 }
 
-
-struct cf_pair {
-	guint32 chan;
-	guint32 freq;
-};
-
-static struct cf_pair a_table[] = {
-	/* A band */
-	{  7, 5035 },
-	{  8, 5040 },
-	{  9, 5045 },
-	{ 11, 5055 },
-	{ 12, 5060 },
-	{ 16, 5080 },
-	{ 34, 5170 },
-	{ 36, 5180 },
-	{ 38, 5190 },
-	{ 40, 5200 },
-	{ 42, 5210 },
-	{ 44, 5220 },
-	{ 46, 5230 },
-	{ 48, 5240 },
-	{ 50, 5250 },
-	{ 52, 5260 },
-	{ 56, 5280 },
-	{ 58, 5290 },
-	{ 60, 5300 },
-	{ 64, 5320 },
-	{ 100, 5500 },
-	{ 104, 5520 },
-	{ 108, 5540 },
-	{ 112, 5560 },
-	{ 116, 5580 },
-	{ 120, 5600 },
-	{ 124, 5620 },
-	{ 128, 5640 },
-	{ 132, 5660 },
-	{ 136, 5680 },
-	{ 140, 5700 },
-	{ 149, 5745 },
-	{ 152, 5760 },
-	{ 153, 5765 },
-	{ 157, 5785 },
-	{ 160, 5800 },
-	{ 161, 5805 },
-	{ 165, 5825 },
-	{ 183, 4915 },
-	{ 184, 4920 },
-	{ 185, 4925 },
-	{ 187, 4935 },
-	{ 188, 4945 },
-	{ 192, 4960 },
-	{ 196, 4980 },
-	{ 0, -1 }
-};
-
-static struct cf_pair bg_table[] = {
-	/* B/G band */
-	{ 1, 2412 },
-	{ 2, 2417 },
-	{ 3, 2422 },
-	{ 4, 2427 },
-	{ 5, 2432 },
-	{ 6, 2437 },
-	{ 7, 2442 },
-	{ 8, 2447 },
-	{ 9, 2452 },
-	{ 10, 2457 },
-	{ 11, 2462 },
-	{ 12, 2467 },
-	{ 13, 2472 },
-	{ 14, 2484 },
-	{ 0, -1 }
-};
-
-guint32
-freq_to_channel (guint32 freq)
-{
-	int i = 0;
-
-	if (freq > 4900) {
-		while (a_table[i].chan && (a_table[i].freq != freq))
-			i++;
-		return a_table[i].chan;
-	} else {
-		while (bg_table[i].chan && (bg_table[i].freq != freq))
-			i++;
-		return bg_table[i].chan;
-	}
-
-	return 0;
-}
-
-guint32
-channel_to_freq (guint32 channel, const char *band)
-{
-	int i = 0;
-
-	if (!strcmp (band, "a")) {
-		while (a_table[i].chan && (a_table[i].chan != channel))
-			i++;
-		return a_table[i].freq;
-	} else if (!strcmp (band, "bg")) {
-		while (bg_table[i].chan && (bg_table[i].chan != channel))
-			i++;
-		return bg_table[i].freq;
-	}
-
-	return 0;
-}
-
diff --git a/src/nm-wifi-ap.h b/src/nm-wifi-ap.h
index edc9e56..86b785a 100644
--- a/src/nm-wifi-ap.h
+++ b/src/nm-wifi-ap.h
@@ -15,7 +15,7 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *
- * Copyright (C) 2004 - 2008 Red Hat, Inc.
+ * Copyright (C) 2004 - 2010 Red Hat, Inc.
  * Copyright (C) 2006 - 2008 Novell, Inc.
  */
 
@@ -123,7 +123,4 @@ NMAccessPoint *     nm_ap_match_in_list (NMAccessPoint *find_ap,
 
 void				nm_ap_print_self (NMAccessPoint *ap, const char * prefix);
 
-guint32 freq_to_channel (guint32 freq);
-guint32 channel_to_freq (guint32 channel, const char *band);
-
 #endif /* NM_ACCESS_POINT_H */


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