WifiAccessPoint *createNMWifiAccessPoint(NMAccessPoint *ap) {
  const GByteArray *ssid = nm_access_point_get_ssid(ap);
  //GBytes *ssid = nm_access_point_get_ssid(ap);
  char *ssid_str = NULL, *ssid_hex_str = NULL;
  bool security = resolveAPSecurity(ap);

  /* Convert to strings */
  if (ssid) {
    const guint8 *ssid_data = ssid->data;
    gsize ssid_len = ssid->len;

    //ssid_data = (const guint8 *) g_bytes_get_data(ssid, &ssid_len);
    ssid_str = nm_utils_ssid_to_utf8(ssid);
  }

  if (!ssid_str || !ssid)
    DBG("libnm conversion of ssid to utf8 failed, using empty string");

  return new WifiAccessPoint {
    !ssid_str ? "" : ssid_str,
    nm_access_point_get_strength(ap),
    security,
    utils_hash_ap(nm_access_point_get_ssid(ap),
                  nm_access_point_get_mode(ap),
                  nm_access_point_get_flags(ap),
                  nm_access_point_get_wpa_flags(ap),
                  nm_access_point_get_rsn_flags(ap)),
  };
}
static char *
make_hash (const char *str,
           NM80211Mode mode,
           guint32 flags,
           guint32 wpa_flags,
           guint32 rsn_flags)
{
	GByteArray *ssid;
	char *hash, *hash2;

	ssid = string_to_ssid (str);

	hash = utils_hash_ap (ssid, mode, flags, wpa_flags, rsn_flags);
	g_assert (hash != NULL);

	hash2 = utils_hash_ap (ssid, mode, flags, wpa_flags, rsn_flags);
	g_assert (hash2 != NULL);

	/* Make sure they are the same each time */
	g_assert (!strcmp (hash, hash2));

	g_byte_array_free (ssid, TRUE);
	return hash;
}
void WifiStatusNM::setConnectedAccessPoint(WifiAccessPoint *ap, String psk) {
  ScopedPointer<StringArray> cmd;

  for (const auto& listener : listeners)
    listener->handleWifiBusy();

  // disconnect if no ap provided
  if (ap == nullptr) {
    NMActiveConnection *conn = nm_device_get_active_connection(nmdevice);
    removeNMConnection(nmdevice, conn);

    return;
  }
  // try to connect to ap, dispatch events on success and failure
  else {
    NMConnection *connection = NULL;
    NMSettingWireless *s_wifi = NULL;
    NMSettingWirelessSecurity *s_wsec = NULL;
    const char *nm_ap_path = NULL;
    const GPtrArray *ap_list;
    NMAccessPoint *candidate_ap;

    //FIXME: expand WifiAccessPoint struct to know which NMAccessPoint it is
    ap_list = nm_device_wifi_get_access_points(NM_DEVICE_WIFI(nmdevice));
    if (ap_list == NULL)
      return;

    for (int i = 0; i < ap_list->len; i++) {
      const char *candidate_hash;
      candidate_ap = (NMAccessPoint *) g_ptr_array_index(ap_list, i);
      candidate_hash = utils_hash_ap(nm_access_point_get_ssid(candidate_ap),
                                     nm_access_point_get_mode(candidate_ap),
                                     nm_access_point_get_flags(candidate_ap),
                                     nm_access_point_get_wpa_flags(candidate_ap),
                                     nm_access_point_get_rsn_flags(candidate_ap));

      if (ap->hash == candidate_hash) {
        nm_ap_path = nm_object_get_path(NM_OBJECT(candidate_ap));
        break;
      }
    }

    if (!nm_ap_path)
      return;

    connecting = true;

    connection = nm_connection_new();
    s_wifi = (NMSettingWireless *) nm_setting_wireless_new();
    nm_connection_add_setting(connection, NM_SETTING(s_wifi));
    g_object_set(s_wifi,
                 NM_SETTING_WIRELESS_SSID, nm_access_point_get_ssid(candidate_ap),
                 NM_SETTING_WIRELESS_HIDDEN, false,
                 NULL);

    if (!psk.isEmpty()) {
      s_wsec = (NMSettingWirelessSecurity *) nm_setting_wireless_security_new();
      nm_connection_add_setting(connection, NM_SETTING(s_wsec));

      if (nm_access_point_get_wpa_flags(candidate_ap) == NM_802_11_AP_SEC_NONE &&
          nm_access_point_get_rsn_flags(candidate_ap) == NM_802_11_AP_SEC_NONE) {
        /* WEP */
        nm_setting_wireless_security_set_wep_key(s_wsec, 0, psk.toRawUTF8());
	if (isValidWEPKeyFormat(psk))
          g_object_set(G_OBJECT(s_wsec), NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE,
                       NM_WEP_KEY_TYPE_KEY, NULL);
	else if (isValidWEPPassphraseFormat(psk))
          g_object_set(G_OBJECT(s_wsec), NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE,
                       NM_WEP_KEY_TYPE_PASSPHRASE, NULL);
	else
	  DBG("User input invalid WEP Key type, psk.length() = " << psk.length()
              << ", not in [5,10,13,26]");
      } else {
        g_object_set(s_wsec, NM_SETTING_WIRELESS_SECURITY_PSK, psk.toRawUTF8(), NULL);
      }
    }

    nm_client_add_and_activate_connection(nmclient,
                                                connection,
                                                nmdevice,
                                                nm_ap_path,
                                                handle_add_and_activate_finish,
                                                this);
  }
}