utils_check_ap_compatible (NMAccessPoint *ap,
                           NMConnection *connection)
{
	NMSettingWireless *s_wireless;
	NMSettingWirelessSecurity *s_wireless_sec;
	const GByteArray *setting_bssid;
	const char *setting_mode;
	const char *setting_band;
	NM80211Mode ap_mode;
	guint32 freq;

	g_return_val_if_fail (NM_IS_ACCESS_POINT (ap), FALSE);
	g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);

	s_wireless = NM_SETTING_WIRELESS (nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS));
	if (s_wireless == NULL)
		return FALSE;
	
	if (!nm_utils_same_ssid (nm_setting_wireless_get_ssid (s_wireless), nm_access_point_get_ssid (ap), TRUE))
		return FALSE;

	setting_bssid = nm_setting_wireless_get_bssid (s_wireless);
	if (setting_bssid) {
		struct ether_addr ap_addr;

		if (ether_aton_r (nm_access_point_get_hw_address (ap), &ap_addr)) {
			if (memcmp (setting_bssid->data, &ap_addr, ETH_ALEN))
				return FALSE;
		}
	}

	ap_mode = nm_access_point_get_mode (ap);
	setting_mode = nm_setting_wireless_get_mode (s_wireless);
	if (setting_mode) {
		if (   !strcmp (setting_mode, "infrastructure")
		    && (ap_mode != NM_802_11_MODE_INFRA))
			return FALSE;
		if (   !strcmp (setting_mode, "adhoc")
		    && (ap_mode != NM_802_11_MODE_ADHOC))
			return FALSE;
	}

	freq = nm_access_point_get_frequency (ap);
	setting_band = nm_setting_wireless_get_band (s_wireless);
	if (setting_band) {
		if (!strcmp (setting_band, "a")) {
			if (freq < 5170 || freq > 5825)
				return FALSE;
		} else if (!strcmp (setting_band, "bg")) {
			if (freq < 2412 || freq > 2472)
				return FALSE;
		}
	}

	// FIXME: channel check

	s_wireless_sec = (NMSettingWirelessSecurity *) nm_connection_get_setting (connection,
															    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));
}
utils_check_ap_compatible (NMAccessPoint *ap,
                           NMConnection *connection)
{
	NMSettingWireless *s_wireless;
	NMSettingWirelessSecurity *s_wireless_sec;
	const GByteArray *setting_bssid;
	const char *setting_mode;
	const char *setting_band;
	NM80211Mode ap_mode;
	guint32 freq;

	g_return_val_if_fail (NM_IS_ACCESS_POINT (ap), FALSE);
	g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);

	s_wireless = NM_SETTING_WIRELESS (nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS));
	if (s_wireless == NULL)
		return FALSE;
	
	if (!nm_utils_same_ssid (nm_setting_wireless_get_ssid (s_wireless), nm_access_point_get_ssid (ap), TRUE))
		return FALSE;

	setting_bssid = nm_setting_wireless_get_bssid (s_wireless);
	if (setting_bssid) {
		struct ether_addr ap_addr;

		if (ether_aton_r (nm_access_point_get_hw_address (ap), &ap_addr)) {
			if (memcmp (setting_bssid->data, &ap_addr, ETH_ALEN))
				return FALSE;
		}
	}

	ap_mode = nm_access_point_get_mode (ap);
	setting_mode = nm_setting_wireless_get_mode (s_wireless);
	if (setting_mode) {
		if (   !strcmp (setting_mode, "infrastructure")
		    && (ap_mode != NM_802_11_MODE_INFRA))
			return FALSE;
		if (   !strcmp (setting_mode, "adhoc")
		    && (ap_mode != NM_802_11_MODE_ADHOC))
			return FALSE;
	}

	freq = nm_access_point_get_frequency (ap);
	setting_band = nm_setting_wireless_get_band (s_wireless);
	if (setting_band) {
		if (!strcmp (setting_band, "a")) {
			if (freq < 5170 || freq > 5825)
				return FALSE;
		} else if (!strcmp (setting_band, "bg")) {
			if (freq < 2412 || freq > 2472)
				return FALSE;
		}
	}

	 

	s_wireless_sec = (NMSettingWirelessSecurity *) nm_connection_get_setting (connection,
															    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));
}