|
|
|
@ -40,14 +40,14 @@ const int state_need_auth = 60; |
|
|
|
|
const int reason_wrong_password = 8; |
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
T get_response(QDBusMessage response){ |
|
|
|
|
T get_response(QDBusMessage response) { |
|
|
|
|
QVariant first = response.arguments().at(0); |
|
|
|
|
QDBusVariant dbvFirst = first.value<QDBusVariant>(); |
|
|
|
|
QVariant vFirst = dbvFirst.variant(); |
|
|
|
|
return vFirst.value<T>(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool compare_by_strength(const Network &a, const Network &b){ |
|
|
|
|
bool compare_by_strength(const Network &a, const Network &b) { |
|
|
|
|
if (a.connected == ConnectedType::CONNECTED) return true; |
|
|
|
|
if (b.connected == ConnectedType::CONNECTED) return false; |
|
|
|
|
if (a.connected == ConnectedType::CONNECTING) return true; |
|
|
|
@ -56,13 +56,13 @@ bool compare_by_strength(const Network &a, const Network &b){ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WifiManager::WifiManager(){ |
|
|
|
|
WifiManager::WifiManager() { |
|
|
|
|
qDBusRegisterMetaType<Connection>(); |
|
|
|
|
qDBusRegisterMetaType<IpConfig>(); |
|
|
|
|
connecting_to_network = ""; |
|
|
|
|
adapter = get_adapter(); |
|
|
|
|
has_adapter = adapter != ""; |
|
|
|
|
if (has_adapter){ |
|
|
|
|
if (has_adapter) { |
|
|
|
|
QDBusInterface nm(nm_service, adapter, device_iface, bus); |
|
|
|
|
bus.connect(nm_service, adapter, device_iface, "StateChanged", this, SLOT(change(unsigned int, unsigned int, unsigned int))); |
|
|
|
|
|
|
|
|
@ -75,20 +75,20 @@ WifiManager::WifiManager(){ |
|
|
|
|
// Compute tethering ssid as "Weedle" + first 4 characters of a dongle id
|
|
|
|
|
tethering_ssid = "weedle"; |
|
|
|
|
std::string bytes = Params().get("DongleId"); |
|
|
|
|
if (bytes.length() >= 4){ |
|
|
|
|
if (bytes.length() >= 4) { |
|
|
|
|
tethering_ssid+="-"+QString::fromStdString(bytes.substr(0,4)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void WifiManager::refreshNetworks(){ |
|
|
|
|
void WifiManager::refreshNetworks() { |
|
|
|
|
if (!has_adapter) return; |
|
|
|
|
|
|
|
|
|
bus = QDBusConnection::systemBus(); |
|
|
|
|
seen_networks.clear(); |
|
|
|
|
seen_ssids.clear(); |
|
|
|
|
|
|
|
|
|
for (Network &network : get_networks()){ |
|
|
|
|
if (seen_ssids.count(network.ssid)){ |
|
|
|
|
for (Network &network : get_networks()) { |
|
|
|
|
if (seen_ssids.count(network.ssid)) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
seen_ssids.push_back(network.ssid); |
|
|
|
@ -96,7 +96,7 @@ void WifiManager::refreshNetworks(){ |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
QList<Network> WifiManager::get_networks(){ |
|
|
|
|
QList<Network> WifiManager::get_networks() { |
|
|
|
|
QList<Network> r; |
|
|
|
|
QDBusInterface nm(nm_service, adapter, wireless_device_iface, bus); |
|
|
|
|
QDBusMessage response = nm.call("GetAllAccessPoints"); |
|
|
|
@ -113,18 +113,18 @@ QList<Network> WifiManager::get_networks(){ |
|
|
|
|
unsigned int strength = get_ap_strength(path.path()); |
|
|
|
|
SecurityType security = getSecurityType(path.path()); |
|
|
|
|
ConnectedType ctype; |
|
|
|
|
if (path.path() != active_ap){ |
|
|
|
|
if (path.path() != active_ap) { |
|
|
|
|
ctype = ConnectedType::DISCONNECTED; |
|
|
|
|
}else { |
|
|
|
|
if (ssid == connecting_to_network){ |
|
|
|
|
} else { |
|
|
|
|
if (ssid == connecting_to_network) { |
|
|
|
|
ctype = ConnectedType::CONNECTING; |
|
|
|
|
}else { |
|
|
|
|
} else { |
|
|
|
|
ctype = ConnectedType::CONNECTED; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Network network = {path.path(), ssid, strength, ctype, security}; |
|
|
|
|
|
|
|
|
|
if (ssid.length()){ |
|
|
|
|
if (ssid.length()) { |
|
|
|
|
r.push_back(network); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -134,7 +134,7 @@ QList<Network> WifiManager::get_networks(){ |
|
|
|
|
return r; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
SecurityType WifiManager::getSecurityType(QString path){ |
|
|
|
|
SecurityType WifiManager::getSecurityType(QString path) { |
|
|
|
|
int sflag = get_property(path, "Flags").toInt(); |
|
|
|
|
int wpaflag = get_property(path, "WpaFlags").toInt(); |
|
|
|
|
int rsnflag = get_property(path, "RsnFlags").toInt(); |
|
|
|
@ -143,7 +143,7 @@ SecurityType WifiManager::getSecurityType(QString path){ |
|
|
|
|
// obtained by looking at flags of networks in the office as reported by an Android phone
|
|
|
|
|
const int supports_wpa = NM_802_11_AP_SEC_PAIR_WEP40 | NM_802_11_AP_SEC_PAIR_WEP104 | NM_802_11_AP_SEC_GROUP_WEP40 | NM_802_11_AP_SEC_GROUP_WEP104 | NM_802_11_AP_SEC_KEY_MGMT_PSK; |
|
|
|
|
|
|
|
|
|
if (sflag == 0){ |
|
|
|
|
if (sflag == 0) { |
|
|
|
|
return SecurityType::OPEN; |
|
|
|
|
} else if ((sflag & NM_802_11_AP_FLAGS_PRIVACY) && (wpa_props & supports_wpa) && !(wpa_props & NM_802_11_AP_SEC_KEY_MGMT_802_1X)) { |
|
|
|
|
return SecurityType::WPA; |
|
|
|
@ -152,22 +152,22 @@ SecurityType WifiManager::getSecurityType(QString path){ |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void WifiManager::connect(Network n){ |
|
|
|
|
void WifiManager::connect(Network n) { |
|
|
|
|
return connect(n, "", ""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void WifiManager::connect(Network n, QString password){ |
|
|
|
|
void WifiManager::connect(Network n, QString password) { |
|
|
|
|
return connect(n, "", password); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void WifiManager::connect(Network n, QString username, QString password){ |
|
|
|
|
void WifiManager::connect(Network n, QString username, QString password) { |
|
|
|
|
connecting_to_network = n.ssid; |
|
|
|
|
disconnect(); |
|
|
|
|
clear_connections(n.ssid); //Clear all connections that may already exist to the network we are connecting
|
|
|
|
|
connect(n.ssid, username, password, n.security_type); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void WifiManager::connect(QByteArray ssid, QString username, QString password, SecurityType security_type){ |
|
|
|
|
void WifiManager::connect(QByteArray ssid, QString username, QString password, SecurityType security_type) { |
|
|
|
|
Connection connection; |
|
|
|
|
connection["connection"]["type"] = "802-11-wireless"; |
|
|
|
|
connection["connection"]["uuid"] = QUuid::createUuid().toString().remove('{').remove('}'); |
|
|
|
@ -177,7 +177,7 @@ void WifiManager::connect(QByteArray ssid, QString username, QString password, S |
|
|
|
|
connection["802-11-wireless"]["ssid"] = ssid; |
|
|
|
|
connection["802-11-wireless"]["mode"] = "infrastructure"; |
|
|
|
|
|
|
|
|
|
if (security_type == SecurityType::WPA){ |
|
|
|
|
if (security_type == SecurityType::WPA) { |
|
|
|
|
connection["802-11-wireless-security"]["key-mgmt"] = "wpa-psk"; |
|
|
|
|
connection["802-11-wireless-security"]["auth-alg"] = "open"; |
|
|
|
|
connection["802-11-wireless-security"]["psk"] = password; |
|
|
|
@ -190,20 +190,20 @@ void WifiManager::connect(QByteArray ssid, QString username, QString password, S |
|
|
|
|
nm_settings.call("AddConnection", QVariant::fromValue(connection)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void WifiManager::deactivate_connections(QString ssid){ |
|
|
|
|
for (QDBusObjectPath active_connection_raw : get_active_connections()){ |
|
|
|
|
void WifiManager::deactivate_connections(QString ssid) { |
|
|
|
|
for (QDBusObjectPath active_connection_raw : get_active_connections()) { |
|
|
|
|
QString active_connection = active_connection_raw.path(); |
|
|
|
|
QDBusInterface nm(nm_service, active_connection, props_iface, bus); |
|
|
|
|
QDBusObjectPath pth = get_response<QDBusObjectPath>(nm.call("Get", connection_iface, "SpecificObject")); |
|
|
|
|
QString Ssid = get_property(pth.path(), "Ssid"); |
|
|
|
|
if (Ssid == ssid){ |
|
|
|
|
if (Ssid == ssid) { |
|
|
|
|
QDBusInterface nm2(nm_service, nm_path, nm_iface, bus); |
|
|
|
|
nm2.call("DeactivateConnection", QVariant::fromValue(active_connection_raw)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
QVector<QDBusObjectPath> WifiManager::get_active_connections(){ |
|
|
|
|
QVector<QDBusObjectPath> WifiManager::get_active_connections() { |
|
|
|
|
QDBusInterface nm(nm_service, nm_path, props_iface, bus); |
|
|
|
|
QDBusMessage response = nm.call("Get", nm_iface, "ActiveConnections"); |
|
|
|
|
QDBusArgument arr = get_response<QDBusArgument>(response); |
|
|
|
@ -211,14 +211,14 @@ QVector<QDBusObjectPath> WifiManager::get_active_connections(){ |
|
|
|
|
|
|
|
|
|
QDBusObjectPath path; |
|
|
|
|
arr.beginArray(); |
|
|
|
|
while (!arr.atEnd()){ |
|
|
|
|
while (!arr.atEnd()) { |
|
|
|
|
arr >> path; |
|
|
|
|
conns.push_back(path); |
|
|
|
|
} |
|
|
|
|
return conns; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void WifiManager::clear_connections(QString ssid){ |
|
|
|
|
void WifiManager::clear_connections(QString ssid) { |
|
|
|
|
QDBusInterface nm(nm_service, nm_settings_path, nm_settings_iface, bus); |
|
|
|
|
QDBusMessage response = nm.call("ListConnections"); |
|
|
|
|
QVariant first = response.arguments().at(0); |
|
|
|
@ -237,9 +237,9 @@ void WifiManager::clear_connections(QString ssid){ |
|
|
|
|
for (QString outer_key : map.keys()) { |
|
|
|
|
QMap<QString,QVariant> innerMap = map.value(outer_key); |
|
|
|
|
for (QString inner_key : innerMap.keys()) { |
|
|
|
|
if (inner_key == "ssid"){ |
|
|
|
|
if (inner_key == "ssid") { |
|
|
|
|
QString value = innerMap.value(inner_key).value<QString>(); |
|
|
|
|
if (value == ssid){ |
|
|
|
|
if (value == ssid) { |
|
|
|
|
nm2.call("Delete"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -248,40 +248,40 @@ void WifiManager::clear_connections(QString ssid){ |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void WifiManager::request_scan(){ |
|
|
|
|
void WifiManager::request_scan() { |
|
|
|
|
if (!has_adapter) return; |
|
|
|
|
|
|
|
|
|
QDBusInterface nm(nm_service, adapter, wireless_device_iface, bus); |
|
|
|
|
nm.call("RequestScan", QVariantMap()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
uint WifiManager::get_wifi_device_state(){ |
|
|
|
|
uint WifiManager::get_wifi_device_state() { |
|
|
|
|
QDBusInterface device_props(nm_service, adapter, props_iface, bus); |
|
|
|
|
QDBusMessage response = device_props.call("Get", device_iface, "State"); |
|
|
|
|
uint resp = get_response<uint>(response); |
|
|
|
|
return resp; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
QString WifiManager::get_active_ap(){ |
|
|
|
|
QString WifiManager::get_active_ap() { |
|
|
|
|
QDBusInterface device_props(nm_service, adapter, props_iface, bus); |
|
|
|
|
QDBusMessage response = device_props.call("Get", wireless_device_iface, "ActiveAccessPoint"); |
|
|
|
|
QDBusObjectPath r = get_response<QDBusObjectPath>(response); |
|
|
|
|
return r.path(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
QByteArray WifiManager::get_property(QString network_path ,QString property){ |
|
|
|
|
QByteArray WifiManager::get_property(QString network_path ,QString property) { |
|
|
|
|
QDBusInterface device_props(nm_service, network_path, props_iface, bus); |
|
|
|
|
QDBusMessage response = device_props.call("Get", ap_iface, property); |
|
|
|
|
return get_response<QByteArray>(response); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
unsigned int WifiManager::get_ap_strength(QString network_path){ |
|
|
|
|
unsigned int WifiManager::get_ap_strength(QString network_path) { |
|
|
|
|
QDBusInterface device_props(nm_service, network_path, props_iface, bus); |
|
|
|
|
QDBusMessage response = device_props.call("Get", ap_iface, "Strength"); |
|
|
|
|
return get_response<unsigned int>(response); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
QString WifiManager::get_adapter(){ |
|
|
|
|
QString WifiManager::get_adapter() { |
|
|
|
|
|
|
|
|
|
QDBusInterface nm(nm_service, nm_path, nm_iface, bus); |
|
|
|
|
QDBusMessage response = nm.call("GetDevices"); |
|
|
|
@ -310,25 +310,25 @@ QString WifiManager::get_adapter(){ |
|
|
|
|
return adapter_path; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void WifiManager::change(unsigned int new_state,unsigned int previous_state,unsigned int change_reason){ |
|
|
|
|
void WifiManager::change(unsigned int new_state,unsigned int previous_state,unsigned int change_reason) { |
|
|
|
|
raw_adapter_state = new_state; |
|
|
|
|
if (new_state == state_need_auth && change_reason == reason_wrong_password){ |
|
|
|
|
if (new_state == state_need_auth && change_reason == reason_wrong_password) { |
|
|
|
|
emit wrongPassword(connecting_to_network); |
|
|
|
|
}else if (new_state == state_connected){ |
|
|
|
|
} else if (new_state == state_connected) { |
|
|
|
|
connecting_to_network = ""; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void WifiManager::disconnect(){ |
|
|
|
|
void WifiManager::disconnect() { |
|
|
|
|
QString active_ap = get_active_ap(); |
|
|
|
|
if (active_ap!="" && active_ap!="/"){ |
|
|
|
|
if (active_ap!="" && active_ap!="/") { |
|
|
|
|
deactivate_connections(get_property(active_ap, "Ssid")); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//Functions for tethering
|
|
|
|
|
|
|
|
|
|
void WifiManager::enableTethering(){ |
|
|
|
|
void WifiManager::enableTethering() { |
|
|
|
|
disconnect(); |
|
|
|
|
Connection connection; |
|
|
|
|
connection["connection"]["id"] = "Hotspot"; |
|
|
|
@ -359,11 +359,11 @@ void WifiManager::enableTethering(){ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void WifiManager::disableTethering(){ |
|
|
|
|
void WifiManager::disableTethering() { |
|
|
|
|
clear_connections(tethering_ssid); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool WifiManager::tetheringEnabled(){ |
|
|
|
|
bool WifiManager::tetheringEnabled() { |
|
|
|
|
QString active_ap = get_active_ap(); |
|
|
|
|
return get_property(active_ap, "Ssid") == tethering_ssid; |
|
|
|
|
} |
|
|
|
|