(Libnm)从C程序连接到wifi时指定密码

(Libnm)从C程序连接到wifi时指定密码,c,wifi,glib,C,Wifi,Glib,我已经了解了如何使用以下功能从C程序连接到wifi网络: 现在,我的问题是;如果网络需要密码怎么办?该函数没有此类参数。您需要生成一个带有密码的NMSettingWirelessSecurity。看看使用libnm的nmcli 生成NMSettingWirelessSecurity s_wsec = nm_connection_get_setting_wireless_security(connection); if (ap_wpa_flags == NM_802_11_AP_SEC_NON

我已经了解了如何使用以下功能从C程序连接到wifi网络:


现在,我的问题是;如果网络需要密码怎么办?该函数没有此类参数。

您需要生成一个带有密码的
NMSettingWirelessSecurity
。看看使用libnm的nmcli

生成
NMSettingWirelessSecurity

s_wsec = nm_connection_get_setting_wireless_security(connection);
if (ap_wpa_flags == NM_802_11_AP_SEC_NONE && ap_rsn_flags == NM_802_11_AP_SEC_NONE) {
    /* WEP */
    nm_setting_wireless_security_set_wep_key(s_wsec, 0, password);
    g_object_set(G_OBJECT(s_wsec),
                    NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE,
                    wep_passphrase ? NM_WEP_KEY_TYPE_PASSPHRASE : NM_WEP_KEY_TYPE_KEY,
                    NULL);
} else if ((ap_wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK)
            || (ap_rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK)
            || (ap_rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_SAE)) {
    /* WPA PSK */
    g_object_set(s_wsec, NM_SETTING_WIRELESS_SECURITY_PSK, password, NULL);
}
添加设置

nm_connection_add_setting(connection, NM_SETTING(s_wsec));
设置密码
NMSettingWirelessSecurity

s_wsec = nm_connection_get_setting_wireless_security(connection);
if (ap_wpa_flags == NM_802_11_AP_SEC_NONE && ap_rsn_flags == NM_802_11_AP_SEC_NONE) {
    /* WEP */
    nm_setting_wireless_security_set_wep_key(s_wsec, 0, password);
    g_object_set(G_OBJECT(s_wsec),
                    NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE,
                    wep_passphrase ? NM_WEP_KEY_TYPE_PASSPHRASE : NM_WEP_KEY_TYPE_KEY,
                    NULL);
} else if ((ap_wpa_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK)
            || (ap_rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_PSK)
            || (ap_rsn_flags & NM_802_11_AP_SEC_KEY_MGMT_SAE)) {
    /* WPA PSK */
    g_object_set(s_wsec, NM_SETTING_WIRELESS_SECURITY_PSK, password, NULL);
}
激活

nm_client_add_and_activate_connection_async(nmc->client,
                                                    connection,
                                                    info->device,
                                                    info->specific_object,
                                                    NULL,
                                                    add_and_activate_cb,
                                                    info);

你知道怎么做吗?我也在想同样的事情@CMDoolittle我想我只是运行了一个系统命令:“nmcli设备wifi连接密码”