1 | /* |
2 | SPDX-FileCopyrightText: 2012-2013 Jan Grulich <jgrulich@redhat.com> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | |
7 | #include "wirelesssetting.h" |
8 | #include "wirelesssetting_p.h" |
9 | |
10 | #include "utils.h" |
11 | |
12 | // define the deprecated&dropped values |
13 | #define NM_SETTING_WIRELESS_SEC "security" |
14 | #define NM_SETTING_WIRELESS_ASSIGNED_MAC_ADDRESS "assigned-mac-address" |
15 | |
16 | #include <QDebug> |
17 | |
18 | NetworkManager::WirelessSettingPrivate::WirelessSettingPrivate() |
19 | : name(NM_SETTING_WIRELESS_SETTING_NAME) |
20 | , mode(NetworkManager::WirelessSetting::Infrastructure) |
21 | , band(NetworkManager::WirelessSetting::Automatic) |
22 | , channel(0) |
23 | , rate(0) |
24 | , txPower(0) |
25 | , mtu(0) |
26 | , hidden(false) |
27 | , macAddressRandomization(NetworkManager::Setting::MacAddressRandomizationDefault) |
28 | , powersave(NetworkManager::WirelessSetting::PowerSaveDefault) |
29 | { |
30 | } |
31 | |
32 | NetworkManager::WirelessSetting::WirelessSetting() |
33 | : Setting(Setting::Wireless) |
34 | , d_ptr(new WirelessSettingPrivate()) |
35 | { |
36 | } |
37 | |
38 | NetworkManager::WirelessSetting::WirelessSetting(const Ptr &setting) |
39 | : Setting(setting) |
40 | , d_ptr(new WirelessSettingPrivate) |
41 | { |
42 | setSsid(setting->ssid()); |
43 | setMode(setting->mode()); |
44 | setBand(setting->band()); |
45 | setChannel(setting->channel()); |
46 | setBssid(setting->bssid()); |
47 | setRate(setting->rate()); |
48 | setTxPower(setting->txPower()); |
49 | setMacAddress(setting->macAddress()); |
50 | setGenerateMacAddressMask(setting->generateMacAddressMask()); |
51 | setClonedMacAddress(setting->clonedMacAddress()); |
52 | setMacAddressBlacklist(setting->macAddressBlacklist()); |
53 | setMacAddressRandomization(setting->macAddressRandomization()); |
54 | setMtu(setting->mtu()); |
55 | setSeenBssids(setting->seenBssids()); |
56 | setPowerSave(setting->powerSave()); |
57 | setSecurity(setting->security()); |
58 | setHidden(setting->hidden()); |
59 | setAssignedMacAddress(setting->assignedMacAddress()); |
60 | } |
61 | |
62 | NetworkManager::WirelessSetting::~WirelessSetting() |
63 | { |
64 | delete d_ptr; |
65 | } |
66 | |
67 | QString NetworkManager::WirelessSetting::name() const |
68 | { |
69 | Q_D(const WirelessSetting); |
70 | |
71 | return d->name; |
72 | } |
73 | |
74 | void NetworkManager::WirelessSetting::setSsid(const QByteArray &ssid) |
75 | { |
76 | Q_D(WirelessSetting); |
77 | |
78 | d->ssid = ssid; |
79 | } |
80 | |
81 | QByteArray NetworkManager::WirelessSetting::ssid() const |
82 | { |
83 | Q_D(const WirelessSetting); |
84 | |
85 | return d->ssid; |
86 | } |
87 | |
88 | void NetworkManager::WirelessSetting::setMode(NetworkManager::WirelessSetting::NetworkMode mode) |
89 | { |
90 | Q_D(WirelessSetting); |
91 | |
92 | d->mode = mode; |
93 | } |
94 | |
95 | NetworkManager::WirelessSetting::NetworkMode NetworkManager::WirelessSetting::mode() const |
96 | { |
97 | Q_D(const WirelessSetting); |
98 | |
99 | return d->mode; |
100 | } |
101 | |
102 | void NetworkManager::WirelessSetting::setBand(NetworkManager::WirelessSetting::FrequencyBand band) |
103 | { |
104 | Q_D(WirelessSetting); |
105 | |
106 | d->band = band; |
107 | } |
108 | |
109 | NetworkManager::WirelessSetting::FrequencyBand NetworkManager::WirelessSetting::band() const |
110 | { |
111 | Q_D(const WirelessSetting); |
112 | |
113 | return d->band; |
114 | } |
115 | |
116 | void NetworkManager::WirelessSetting::setChannel(quint32 channel) |
117 | { |
118 | Q_D(WirelessSetting); |
119 | |
120 | d->channel = channel; |
121 | } |
122 | |
123 | quint32 NetworkManager::WirelessSetting::channel() const |
124 | { |
125 | Q_D(const WirelessSetting); |
126 | |
127 | return d->channel; |
128 | } |
129 | |
130 | void NetworkManager::WirelessSetting::setBssid(const QByteArray &bssid) |
131 | { |
132 | Q_D(WirelessSetting); |
133 | |
134 | d->bssid = bssid; |
135 | } |
136 | |
137 | QByteArray NetworkManager::WirelessSetting::bssid() const |
138 | { |
139 | Q_D(const WirelessSetting); |
140 | |
141 | return d->bssid; |
142 | } |
143 | |
144 | void NetworkManager::WirelessSetting::setRate(quint32 rate) |
145 | { |
146 | Q_D(WirelessSetting); |
147 | |
148 | d->rate = rate; |
149 | } |
150 | |
151 | quint32 NetworkManager::WirelessSetting::rate() const |
152 | { |
153 | Q_D(const WirelessSetting); |
154 | |
155 | return d->rate; |
156 | } |
157 | |
158 | void NetworkManager::WirelessSetting::setTxPower(quint32 power) |
159 | { |
160 | Q_D(WirelessSetting); |
161 | |
162 | d->txPower = power; |
163 | } |
164 | |
165 | quint32 NetworkManager::WirelessSetting::txPower() const |
166 | { |
167 | Q_D(const WirelessSetting); |
168 | |
169 | return d->txPower; |
170 | } |
171 | |
172 | void NetworkManager::WirelessSetting::setAssignedMacAddress(const QString &assignedMacAddress) |
173 | { |
174 | Q_D(WirelessSetting); |
175 | |
176 | d->assignedMacAddress = assignedMacAddress; |
177 | } |
178 | |
179 | QString NetworkManager::WirelessSetting::assignedMacAddress() const |
180 | { |
181 | Q_D(const WirelessSetting); |
182 | |
183 | return d->assignedMacAddress; |
184 | } |
185 | |
186 | void NetworkManager::WirelessSetting::setMacAddress(const QByteArray &address) |
187 | { |
188 | Q_D(WirelessSetting); |
189 | |
190 | d->macAddress = address; |
191 | } |
192 | |
193 | QByteArray NetworkManager::WirelessSetting::macAddress() const |
194 | { |
195 | Q_D(const WirelessSetting); |
196 | |
197 | return d->macAddress; |
198 | } |
199 | |
200 | void NetworkManager::WirelessSetting::setClonedMacAddress(const QByteArray &address) |
201 | { |
202 | Q_D(WirelessSetting); |
203 | |
204 | d->assignedMacAddress = NetworkManager::macAddressAsString(ba: address); |
205 | } |
206 | |
207 | QByteArray NetworkManager::WirelessSetting::clonedMacAddress() const |
208 | { |
209 | Q_D(const WirelessSetting); |
210 | |
211 | return NetworkManager::macAddressFromString(s: d->assignedMacAddress.toUtf8()); |
212 | } |
213 | |
214 | void NetworkManager::WirelessSetting::setGenerateMacAddressMask(const QString &macAddressMask) |
215 | { |
216 | Q_D(WirelessSetting); |
217 | |
218 | d->generateMacAddressMask = macAddressMask; |
219 | } |
220 | |
221 | QString NetworkManager::WirelessSetting::generateMacAddressMask() const |
222 | { |
223 | Q_D(const WirelessSetting); |
224 | |
225 | return d->generateMacAddressMask; |
226 | } |
227 | |
228 | void NetworkManager::WirelessSetting::setMacAddressBlacklist(const QStringList &list) |
229 | { |
230 | Q_D(WirelessSetting); |
231 | |
232 | d->macAddressBlacklist = list; |
233 | } |
234 | |
235 | QStringList NetworkManager::WirelessSetting::macAddressBlacklist() const |
236 | { |
237 | Q_D(const WirelessSetting); |
238 | |
239 | return d->macAddressBlacklist; |
240 | } |
241 | |
242 | void NetworkManager::WirelessSetting::setMacAddressRandomization(NetworkManager::Setting::MacAddressRandomization randomization) |
243 | { |
244 | Q_D(WirelessSetting); |
245 | |
246 | d->macAddressRandomization = randomization; |
247 | } |
248 | |
249 | NetworkManager::Setting::MacAddressRandomization NetworkManager::WirelessSetting::macAddressRandomization() const |
250 | { |
251 | Q_D(const WirelessSetting); |
252 | |
253 | return d->macAddressRandomization; |
254 | } |
255 | |
256 | void NetworkManager::WirelessSetting::setMtu(quint32 mtu) |
257 | { |
258 | Q_D(WirelessSetting); |
259 | |
260 | d->mtu = mtu; |
261 | } |
262 | |
263 | quint32 NetworkManager::WirelessSetting::mtu() const |
264 | { |
265 | Q_D(const WirelessSetting); |
266 | |
267 | return d->mtu; |
268 | } |
269 | |
270 | void NetworkManager::WirelessSetting::setSeenBssids(const QStringList &list) |
271 | { |
272 | Q_D(WirelessSetting); |
273 | |
274 | d->seenBssids = list; |
275 | } |
276 | |
277 | QStringList NetworkManager::WirelessSetting::seenBssids() const |
278 | { |
279 | Q_D(const WirelessSetting); |
280 | |
281 | return d->seenBssids; |
282 | } |
283 | |
284 | void NetworkManager::WirelessSetting::setPowerSave(NetworkManager::WirelessSetting::PowerSave powersave) |
285 | { |
286 | Q_D(WirelessSetting); |
287 | |
288 | d->powersave = powersave; |
289 | } |
290 | |
291 | NetworkManager::WirelessSetting::PowerSave NetworkManager::WirelessSetting::powerSave() const |
292 | { |
293 | Q_D(const WirelessSetting); |
294 | |
295 | return d->powersave; |
296 | } |
297 | |
298 | void NetworkManager::WirelessSetting::setSecurity(const QString &security) |
299 | { |
300 | Q_D(WirelessSetting); |
301 | |
302 | d->security = security; |
303 | } |
304 | |
305 | QString NetworkManager::WirelessSetting::security() const |
306 | { |
307 | Q_D(const WirelessSetting); |
308 | |
309 | return d->security; |
310 | } |
311 | |
312 | void NetworkManager::WirelessSetting::setHidden(bool hidden) |
313 | { |
314 | Q_D(WirelessSetting); |
315 | |
316 | d->hidden = hidden; |
317 | } |
318 | |
319 | bool NetworkManager::WirelessSetting::hidden() const |
320 | { |
321 | Q_D(const WirelessSetting); |
322 | |
323 | return d->hidden; |
324 | } |
325 | |
326 | void NetworkManager::WirelessSetting::fromMap(const QVariantMap &setting) |
327 | { |
328 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_SSID))) { |
329 | setSsid(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_SSID)).toByteArray()); |
330 | } |
331 | |
332 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_MODE))) { |
333 | const QString mode = setting.value(key: QLatin1String(NM_SETTING_WIRELESS_MODE)).toString(); |
334 | if (mode == QLatin1String(NM_SETTING_WIRELESS_MODE_INFRA)) { |
335 | setMode(Infrastructure); |
336 | } else if (mode == QLatin1String(NM_SETTING_WIRELESS_MODE_ADHOC)) { |
337 | setMode(Adhoc); |
338 | } else if (mode == QLatin1String(NM_SETTING_WIRELESS_MODE_AP)) { |
339 | setMode(Ap); |
340 | } |
341 | } |
342 | |
343 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_BAND))) { |
344 | const QString band = setting.value(key: QLatin1String(NM_SETTING_WIRELESS_BAND)).toString(); |
345 | if (band == "a" ) { |
346 | setBand(A); |
347 | } else if (band == "bg" ) { |
348 | setBand(Bg); |
349 | } |
350 | } |
351 | |
352 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_CHANNEL))) { |
353 | setChannel(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_CHANNEL)).toUInt()); |
354 | } |
355 | |
356 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_BSSID))) { |
357 | setBssid(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_BSSID)).toByteArray()); |
358 | } |
359 | |
360 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_RATE))) { |
361 | setRate(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_RATE)).toUInt()); |
362 | } |
363 | |
364 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_TX_POWER))) { |
365 | setTxPower(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_TX_POWER)).toUInt()); |
366 | } |
367 | |
368 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_ASSIGNED_MAC_ADDRESS))) { |
369 | setAssignedMacAddress(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_ASSIGNED_MAC_ADDRESS)).toString()); |
370 | } |
371 | |
372 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_MAC_ADDRESS))) { |
373 | setMacAddress(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_MAC_ADDRESS)).toByteArray()); |
374 | } |
375 | |
376 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_GENERATE_MAC_ADDRESS_MASK))) { |
377 | setGenerateMacAddressMask(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_GENERATE_MAC_ADDRESS_MASK)).toString()); |
378 | } |
379 | |
380 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS))) { |
381 | setClonedMacAddress(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS)).toByteArray()); |
382 | } |
383 | |
384 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST))) { |
385 | setMacAddressBlacklist(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST)).toStringList()); |
386 | } |
387 | |
388 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_MAC_ADDRESS_RANDOMIZATION))) { |
389 | setMacAddressRandomization( |
390 | static_cast<Setting::MacAddressRandomization>(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_MAC_ADDRESS_RANDOMIZATION)).toUInt())); |
391 | } |
392 | |
393 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_MTU))) { |
394 | setMtu(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_MTU)).toUInt()); |
395 | } |
396 | |
397 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_SEEN_BSSIDS))) { |
398 | setSeenBssids(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_SEEN_BSSIDS)).toStringList()); |
399 | } |
400 | |
401 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_POWERSAVE))) { |
402 | setPowerSave(static_cast<WirelessSetting::PowerSave>(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_POWERSAVE)).toUInt())); |
403 | } |
404 | |
405 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_SEC))) { |
406 | setSecurity(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_SEC)).toString()); |
407 | } |
408 | |
409 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRELESS_HIDDEN))) { |
410 | setHidden(setting.value(key: QLatin1String(NM_SETTING_WIRELESS_HIDDEN)).toBool()); |
411 | } |
412 | } |
413 | |
414 | QVariantMap NetworkManager::WirelessSetting::toMap() const |
415 | { |
416 | QVariantMap setting; |
417 | |
418 | if (!ssid().isEmpty()) { |
419 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_SSID), value: ssid()); |
420 | } |
421 | |
422 | if (mode() == Infrastructure) { |
423 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_MODE), value: QLatin1String(NM_SETTING_WIRELESS_MODE_INFRA)); |
424 | } else if (mode() == Adhoc) { |
425 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_MODE), value: QLatin1String(NM_SETTING_WIRELESS_MODE_ADHOC)); |
426 | } else if (mode() == Ap) { |
427 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_MODE), value: QLatin1String(NM_SETTING_WIRELESS_MODE_AP)); |
428 | } |
429 | |
430 | if (band() != Automatic) { |
431 | if (band() == A) { |
432 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_BAND), value: "a" ); |
433 | } else if (band() == Bg) { |
434 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_BAND), value: "bg" ); |
435 | } |
436 | } |
437 | |
438 | if (channel()) { |
439 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_CHANNEL), value: channel()); |
440 | } |
441 | |
442 | if (!bssid().isEmpty()) { |
443 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_BSSID), value: bssid()); |
444 | } |
445 | |
446 | if (rate()) { |
447 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_RATE), value: rate()); |
448 | } |
449 | |
450 | if (txPower()) { |
451 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_TX_POWER), value: txPower()); |
452 | } |
453 | |
454 | if (!assignedMacAddress().isEmpty()) { |
455 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_ASSIGNED_MAC_ADDRESS), value: assignedMacAddress()); |
456 | } |
457 | |
458 | if (!macAddress().isEmpty()) { |
459 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_MAC_ADDRESS), value: macAddress()); |
460 | } |
461 | |
462 | if (!generateMacAddressMask().isEmpty()) { |
463 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_GENERATE_MAC_ADDRESS_MASK), value: generateMacAddressMask()); |
464 | } |
465 | |
466 | if (!macAddressBlacklist().isEmpty()) { |
467 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST), value: macAddressBlacklist()); |
468 | } |
469 | |
470 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_MAC_ADDRESS_RANDOMIZATION), value: macAddressRandomization()); |
471 | |
472 | if (mtu()) { |
473 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_MTU), value: mtu()); |
474 | } |
475 | |
476 | if (!seenBssids().isEmpty()) { |
477 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_SEEN_BSSIDS), value: seenBssids()); |
478 | } |
479 | |
480 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_POWERSAVE), value: powerSave()); |
481 | |
482 | if (!security().isEmpty()) { |
483 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_SEC), value: security()); |
484 | } |
485 | |
486 | if (hidden()) { |
487 | setting.insert(key: QLatin1String(NM_SETTING_WIRELESS_HIDDEN), value: hidden()); |
488 | } |
489 | |
490 | return setting; |
491 | } |
492 | |
493 | QDebug NetworkManager::operator<<(QDebug dbg, const NetworkManager::WirelessSetting &setting) |
494 | { |
495 | dbg.nospace() << "type: " << setting.typeAsString(type: setting.type()) << '\n'; |
496 | dbg.nospace() << "initialized: " << !setting.isNull() << '\n'; |
497 | |
498 | dbg.nospace() << NM_SETTING_WIRELESS_SSID << ": " << setting.ssid() << '\n'; |
499 | dbg.nospace() << NM_SETTING_WIRELESS_MODE << ": " << setting.mode() << '\n'; |
500 | dbg.nospace() << NM_SETTING_WIRELESS_BAND << ": " << setting.band() << '\n'; |
501 | dbg.nospace() << NM_SETTING_WIRELESS_CHANNEL << ": " << setting.channel() << '\n'; |
502 | dbg.nospace() << NM_SETTING_WIRELESS_BSSID << ": " << setting.bssid() << '\n'; |
503 | dbg.nospace() << NM_SETTING_WIRELESS_RATE << ": " << setting.rate() << '\n'; |
504 | dbg.nospace() << NM_SETTING_WIRELESS_TX_POWER << ": " << setting.txPower() << '\n'; |
505 | dbg.nospace() << NM_SETTING_WIRELESS_ASSIGNED_MAC_ADDRESS << ": " << setting.assignedMacAddress() << '\n'; |
506 | dbg.nospace() << NM_SETTING_WIRELESS_MAC_ADDRESS << ": " << setting.macAddress() << '\n'; |
507 | dbg.nospace() << NM_SETTING_WIRELESS_GENERATE_MAC_ADDRESS_MASK << ": " << setting.generateMacAddressMask() << '\n'; |
508 | dbg.nospace() << NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST << ": " << setting.macAddressBlacklist() << '\n'; |
509 | dbg.nospace() << NM_SETTING_WIRELESS_MAC_ADDRESS_RANDOMIZATION << ": " << setting.macAddressRandomization() << '\n'; |
510 | dbg.nospace() << NM_SETTING_WIRELESS_MTU << ": " << setting.mtu() << '\n'; |
511 | dbg.nospace() << NM_SETTING_WIRELESS_SEEN_BSSIDS << ": " << setting.seenBssids() << '\n'; |
512 | dbg.nospace() << NM_SETTING_WIRELESS_POWERSAVE << ": " << setting.powerSave() << '\n'; |
513 | dbg.nospace() << NM_SETTING_WIRELESS_SEC << ": " << setting.security() << '\n'; |
514 | dbg.nospace() << NM_SETTING_WIRELESS_HIDDEN << ": " << setting.hidden() << '\n'; |
515 | |
516 | return dbg.maybeSpace(); |
517 | } |
518 | |