| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2025 Martin Rodriguez Reboredo <yakoyoku@gmail.com> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 5 | */ |
| 6 | |
| 7 | #include "wifip2ppeer.h" |
| 8 | #include "accesspoint.h" |
| 9 | #include "manager_p.h" |
| 10 | #include "wifip2ppeer_p.h" |
| 11 | |
| 12 | #include "nmdebug.h" |
| 13 | |
| 14 | using namespace Qt::Literals; |
| 15 | |
| 16 | NetworkManager::WifiP2PPeerPrivate::WifiP2PPeerPrivate(const QString &path, WifiP2PPeer *q) |
| 17 | #ifdef NMQT_STATIC |
| 18 | : iface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::sessionBus()) |
| 19 | #else |
| 20 | : iface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::systemBus()) |
| 21 | #endif |
| 22 | , uni(path) |
| 23 | , flags(AccessPoint::None) |
| 24 | , lastSeen(-1) |
| 25 | , strength(0) |
| 26 | , q_ptr(q) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | NetworkManager::AccessPoint::Capabilities NetworkManager::WifiP2PPeerPrivate::convertCapabilities(int caps) |
| 31 | { |
| 32 | NetworkManager::AccessPoint::Capabilities capFlags; |
| 33 | if (caps & NM_802_11_AP_FLAGS_PRIVACY) { |
| 34 | capFlags |= AccessPoint::Privacy; |
| 35 | } |
| 36 | return capFlags; |
| 37 | } |
| 38 | |
| 39 | NetworkManager::WifiP2PPeer::WifiP2PPeer(const QString &path, QObject *parent) |
| 40 | : QObject(parent) |
| 41 | , d_ptr(std::make_unique<WifiP2PPeerPrivate>(args: path, args: this)) |
| 42 | { |
| 43 | Q_D(WifiP2PPeer); |
| 44 | |
| 45 | QDBusConnection::systemBus().connect(service: NetworkManagerPrivate::DBUS_SERVICE, |
| 46 | path: d->uni, |
| 47 | interface: NetworkManagerPrivate::FDO_DBUS_PROPERTIES, |
| 48 | name: QLatin1String(), |
| 49 | receiver: d, |
| 50 | SLOT(dbusPropertiesChanged(QString, QVariantMap, QStringList))); |
| 51 | |
| 52 | // Retrieve all WifiP2PPeer's properties at once |
| 53 | const QVariantMap initialProperties = NetworkManagerPrivate::retrieveInitialProperties(interfaceName: d->iface.staticInterfaceName(), path); |
| 54 | if (!initialProperties.isEmpty()) { |
| 55 | d->propertiesChanged(properties: initialProperties); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | NetworkManager::WifiP2PPeer::~WifiP2PPeer() = default; |
| 60 | |
| 61 | uint NetworkManager::WifiP2PPeer::flags() const |
| 62 | { |
| 63 | Q_D(const WifiP2PPeer); |
| 64 | return d->flags; |
| 65 | } |
| 66 | |
| 67 | QString NetworkManager::WifiP2PPeer::hardwareAddress() const |
| 68 | { |
| 69 | Q_D(const WifiP2PPeer); |
| 70 | return d->hardwareAddress; |
| 71 | } |
| 72 | |
| 73 | int NetworkManager::WifiP2PPeer::lastSeen() const |
| 74 | { |
| 75 | Q_D(const WifiP2PPeer); |
| 76 | return d->lastSeen; |
| 77 | } |
| 78 | |
| 79 | QString NetworkManager::WifiP2PPeer::manufacturer() const |
| 80 | { |
| 81 | Q_D(const WifiP2PPeer); |
| 82 | return d->manufacturer; |
| 83 | } |
| 84 | |
| 85 | QString NetworkManager::WifiP2PPeer::model() const |
| 86 | { |
| 87 | Q_D(const WifiP2PPeer); |
| 88 | return d->model; |
| 89 | } |
| 90 | |
| 91 | QString NetworkManager::WifiP2PPeer::modelNumber() const |
| 92 | { |
| 93 | Q_D(const WifiP2PPeer); |
| 94 | return d->modelNumber; |
| 95 | } |
| 96 | |
| 97 | QString NetworkManager::WifiP2PPeer::name() const |
| 98 | { |
| 99 | Q_D(const WifiP2PPeer); |
| 100 | return d->name; |
| 101 | } |
| 102 | |
| 103 | QString NetworkManager::WifiP2PPeer::serial() const |
| 104 | { |
| 105 | Q_D(const WifiP2PPeer); |
| 106 | return d->serial; |
| 107 | } |
| 108 | |
| 109 | uchar NetworkManager::WifiP2PPeer::strength() const |
| 110 | { |
| 111 | Q_D(const WifiP2PPeer); |
| 112 | return d->strength; |
| 113 | } |
| 114 | |
| 115 | QByteArray NetworkManager::WifiP2PPeer::wfdIEs() const |
| 116 | { |
| 117 | Q_D(const WifiP2PPeer); |
| 118 | return d->wfdIEs; |
| 119 | } |
| 120 | |
| 121 | void NetworkManager::WifiP2PPeerPrivate::dbusPropertiesChanged(const QString &interfaceName, |
| 122 | const QVariantMap &properties, |
| 123 | const QStringList &invalidatedProperties) |
| 124 | { |
| 125 | Q_UNUSED(invalidatedProperties); |
| 126 | if (interfaceName == "org.freedekstop.NetworkManager.WifiP2PPeer"_L1 ) { |
| 127 | propertiesChanged(properties); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void NetworkManager::WifiP2PPeerPrivate::propertiesChanged(const QVariantMap &properties) |
| 132 | { |
| 133 | Q_Q(WifiP2PPeer); |
| 134 | |
| 135 | QVariantMap::const_iterator it = properties.constBegin(); |
| 136 | while (it != properties.constEnd()) { |
| 137 | const QString property = it.key(); |
| 138 | if (property == "Flags"_L1 ) { |
| 139 | flags = convertCapabilities(caps: it->toUInt()); |
| 140 | Q_EMIT q->flagsChanged(flags); |
| 141 | } else if (property == "HwAddress"_L1 ) { |
| 142 | hardwareAddress = it->toString(); |
| 143 | Q_EMIT q->hardwareAddressChanged(hardwareAddress); |
| 144 | } else if (property == "LastSeen"_L1 ) { |
| 145 | lastSeen = it->toInt(); |
| 146 | Q_EMIT q->lastSeenChanged(lastSeen); |
| 147 | } else if (property == "Manufacturer"_L1 ) { |
| 148 | manufacturer = it->toString(); |
| 149 | Q_EMIT q->manufacturerChanged(manufacturer); |
| 150 | } else if (property == "Model"_L1 ) { |
| 151 | model = it->toString(); |
| 152 | Q_EMIT q->modelChanged(model); |
| 153 | } else if (property == "ModelNumber"_L1 ) { |
| 154 | modelNumber = it->toString(); |
| 155 | Q_EMIT q->modelNumberChanged(modelNumber); |
| 156 | } else if (property == "Name"_L1 ) { |
| 157 | name = it->toString(); |
| 158 | Q_EMIT q->nameChanged(name); |
| 159 | } else if (property == "Serial"_L1 ) { |
| 160 | serial = it->toString(); |
| 161 | Q_EMIT q->serialChanged(serial); |
| 162 | } else if (property == "Strength"_L1 ) { |
| 163 | strength = it->value<uchar>(); |
| 164 | Q_EMIT q->strengthChanged(strength); |
| 165 | } else if (property == "WfdIEs"_L1 ) { |
| 166 | wfdIEs = it->toByteArray(); |
| 167 | Q_EMIT q->wfdIEsChanged(wfdIEs); |
| 168 | } else { |
| 169 | qCDebug(NMQT) << Q_FUNC_INFO << "Unhandled property" << property; |
| 170 | } |
| 171 | ++it; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | #include "moc_wifip2ppeer.cpp" |
| 176 | #include "moc_wifip2ppeer_p.cpp" |
| 177 | |