2
* Copyright © 2012-2014 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Thomas Voß <thomas.voss@canonical.com>
18
#ifndef CACHED_WIRELESS_NETWORK_H_
19
#define CACHED_WIRELESS_NETWORK_H_
21
#include <com/ubuntu/location/connectivity/wireless_network.h>
23
#include <com/ubuntu/location/logging.h>
29
std::string utf8_ssid_to_string(const org::freedesktop::NetworkManager::AccessPoint::Ssid::ValueType& ssid)
31
return std::string(ssid.begin(), ssid.end());
34
com::ubuntu::location::connectivity::WirelessNetwork::Mode
35
wifi_mode_from_ap_mode(org::freedesktop::NetworkManager::AccessPoint::Mode::ValueType value)
37
com::ubuntu::location::connectivity::WirelessNetwork::Mode mode
39
com::ubuntu::location::connectivity::WirelessNetwork::Mode::unknown
44
case org::freedesktop::NetworkManager::AccessPoint::Mode::Value::unknown:
45
mode = com::ubuntu::location::connectivity::WirelessNetwork::Mode::unknown;
47
case org::freedesktop::NetworkManager::AccessPoint::Mode::Value::adhoc:
48
mode = com::ubuntu::location::connectivity::WirelessNetwork::Mode::adhoc;
50
case org::freedesktop::NetworkManager::AccessPoint::Mode::Value::infra:
51
mode = com::ubuntu::location::connectivity::WirelessNetwork::Mode::infrastructure;
58
struct CachedWirelessNetwork : public com::ubuntu::location::connectivity::WirelessNetwork
60
typedef std::shared_ptr<CachedWirelessNetwork> Ptr;
62
const core::Property<std::chrono::system_clock::time_point>& last_seen() const override
67
const core::Property<std::string>& bssid() const override
72
const core::Property<std::string>& ssid() const override
77
const core::Property<Mode>& mode() const override
82
const core::Property<Frequency>& frequency() const override
87
const core::Property<SignalStrength>& signal_strength() const override
89
return signal_strength_;
92
CachedWirelessNetwork(
93
const org::freedesktop::NetworkManager::Device& device,
94
const org::freedesktop::NetworkManager::AccessPoint& ap)
100
last_seen_ = std::chrono::system_clock::time_point
102
std::chrono::system_clock::duration{access_point_.last_seen->get()}
104
} catch(const std::exception& e)
106
LOG(WARNING) << e.what();
109
bssid_ = access_point_.hw_address->get();
110
ssid_ = utf8_ssid_to_string(access_point_.ssid->get());
111
mode_ = wifi_mode_from_ap_mode(access_point_.mode->get());
112
frequency_ = com::ubuntu::location::connectivity::WirelessNetwork::Frequency
114
access_point_.frequency->get()
116
signal_strength_ = com::ubuntu::location::connectivity::WirelessNetwork::SignalStrength
118
int(access_point_.strength->get())
121
// Wire up all the connections
122
access_point_.properties_changed->connect([this](const std::map<std::string, core::dbus::types::Variant>& dict)
124
on_access_point_properties_changed(dict);
128
void on_access_point_properties_changed(const std::map<std::string, core::dbus::types::Variant>& dict)
130
// We route by string
131
static const std::unordered_map<std::string, std::function<void(CachedWirelessNetwork&, const core::dbus::types::Variant&)> > lut
134
org::freedesktop::NetworkManager::AccessPoint::HwAddress::name(),
135
[](CachedWirelessNetwork& thiz, const core::dbus::types::Variant& value)
137
thiz.bssid_ = value.as<org::freedesktop::NetworkManager::AccessPoint::HwAddress::ValueType>();
141
org::freedesktop::NetworkManager::AccessPoint::Ssid::name(),
142
[](CachedWirelessNetwork& thiz, const core::dbus::types::Variant& value)
144
thiz.ssid_ = utf8_ssid_to_string(value.as<org::freedesktop::NetworkManager::AccessPoint::Ssid::ValueType>());
148
org::freedesktop::NetworkManager::AccessPoint::Strength::name(),
149
[](CachedWirelessNetwork& thiz, const core::dbus::types::Variant& value)
151
thiz.signal_strength_ = com::ubuntu::location::connectivity::WirelessNetwork::SignalStrength
153
value.as<org::freedesktop::NetworkManager::AccessPoint::Strength::ValueType>()
158
org::freedesktop::NetworkManager::AccessPoint::Frequency::name(),
159
[](CachedWirelessNetwork& thiz, const core::dbus::types::Variant& value)
161
thiz.frequency_ = com::ubuntu::location::connectivity::WirelessNetwork::Frequency
163
value.as<org::freedesktop::NetworkManager::AccessPoint::Frequency::ValueType>()
168
org::freedesktop::NetworkManager::AccessPoint::Mode::name(),
169
[](CachedWirelessNetwork& thiz, const core::dbus::types::Variant& value)
171
thiz.mode_ = wifi_mode_from_ap_mode(value.as<org::freedesktop::NetworkManager::AccessPoint::Mode::ValueType>());
175
org::freedesktop::NetworkManager::AccessPoint::LastSeen::name(),
176
[](CachedWirelessNetwork& thiz, const core::dbus::types::Variant& value)
178
thiz.last_seen_ = std::chrono::system_clock::time_point
180
std::chrono::system_clock::duration
182
value.as<org::freedesktop::NetworkManager::AccessPoint::LastSeen::ValueType>()
189
for (const auto& pair : dict)
191
VLOG(1) << "Properties on access point " << ssid_.get() << " changed: \n"
192
<< " " << pair.first;
194
// We do not treat failing property updates as fatal but instead just
195
// log the issue for later analysis.
198
if (lut.count(pair.first) > 0) lut.at(pair.first)(*this, pair.second);
199
} catch (const std::exception& e)
201
LOG(WARNING) << "Exception while updating state for property change: " << pair.first;
206
org::freedesktop::NetworkManager::Device device_;
207
org::freedesktop::NetworkManager::AccessPoint access_point_;
209
core::Property<std::chrono::system_clock::time_point> last_seen_;
210
core::Property<std::string> bssid_;
211
core::Property<std::string> ssid_;
212
core::Property<WirelessNetwork::Mode> mode_;
213
core::Property<WirelessNetwork::Frequency> frequency_;
214
core::Property<WirelessNetwork::SignalStrength> signal_strength_;
218
#endif // CACHED_WIRELESS_NETWORK_H_