~thomas-voss/location-service/fix-1347887

« back to all changes in this revision

Viewing changes to src/location_service/com/ubuntu/location/connectivity/cached_wireless_network.h

This MP consolidates multiple related changes together, with the goal of:

(1.) Make the service instance accessible via a cli. Useful for testing scenarios.
(2.) To cut down time-to-first-fix (ttff) by:
  (2.1) Leveraging SUPL and other supplementary data downloaded over ordinary data connections.
  (2.2) Enabling network-based positioning providers to acquire fast position estimates.

In more detail:

* Added tests for daemon and cli.
* Unified daemon and cli header and implementation files.
* Add a command-line interface to the service.
* Split up provider selection policy to rely on an interface ProviderEnumerator to ease in testing.
* Trimmed down on types.
* Removed connectivity API draft to prepare for simpler approach.
* Refactored includes.
* Added a configuration option to handle cell and wifi ID reporting.
* Add a mock for a connectivity API exposed to providers and reporters.
* Add units for connectivity api.
* Refactor cell class into namespace radio. Fixes: 1226204, 1248973, 1281817

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012-2014 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
#ifndef CACHED_WIRELESS_NETWORK_H_
 
19
#define CACHED_WIRELESS_NETWORK_H_
 
20
 
 
21
#include <com/ubuntu/location/connectivity/wireless_network.h>
 
22
 
 
23
#include <com/ubuntu/location/logging.h>
 
24
 
 
25
#include "nm.h"
 
26
 
 
27
namespace
 
28
{
 
29
std::string utf8_ssid_to_string(const org::freedesktop::NetworkManager::AccessPoint::Ssid::ValueType& ssid)
 
30
{
 
31
    return std::string(ssid.begin(), ssid.end());
 
32
}
 
33
 
 
34
com::ubuntu::location::connectivity::WirelessNetwork::Mode
 
35
wifi_mode_from_ap_mode(org::freedesktop::NetworkManager::AccessPoint::Mode::ValueType value)
 
36
{
 
37
    com::ubuntu::location::connectivity::WirelessNetwork::Mode mode
 
38
    {
 
39
        com::ubuntu::location::connectivity::WirelessNetwork::Mode::unknown
 
40
    };
 
41
 
 
42
    switch (value)
 
43
    {
 
44
    case org::freedesktop::NetworkManager::AccessPoint::Mode::Value::unknown:
 
45
        mode = com::ubuntu::location::connectivity::WirelessNetwork::Mode::unknown;
 
46
        break;
 
47
    case org::freedesktop::NetworkManager::AccessPoint::Mode::Value::adhoc:
 
48
        mode = com::ubuntu::location::connectivity::WirelessNetwork::Mode::adhoc;
 
49
        break;
 
50
    case org::freedesktop::NetworkManager::AccessPoint::Mode::Value::infra:
 
51
        mode = com::ubuntu::location::connectivity::WirelessNetwork::Mode::infrastructure;
 
52
        break;
 
53
    }
 
54
 
 
55
    return mode;
 
56
}
 
57
 
 
58
struct CachedWirelessNetwork : public com::ubuntu::location::connectivity::WirelessNetwork
 
59
{
 
60
    typedef std::shared_ptr<CachedWirelessNetwork> Ptr;
 
61
 
 
62
    const core::Property<std::chrono::system_clock::time_point>& last_seen() const override
 
63
    {
 
64
        return last_seen_;
 
65
    }
 
66
 
 
67
    const core::Property<std::string>& bssid() const override
 
68
    {
 
69
        return bssid_;
 
70
    }
 
71
 
 
72
    const core::Property<std::string>& ssid() const override
 
73
    {
 
74
        return ssid_;
 
75
    }
 
76
 
 
77
    const core::Property<Mode>& mode() const override
 
78
    {
 
79
        return mode_;
 
80
    }
 
81
 
 
82
    const core::Property<Frequency>& frequency() const override
 
83
    {
 
84
        return frequency_;
 
85
    }
 
86
 
 
87
    const core::Property<SignalStrength>& signal_strength() const override
 
88
    {
 
89
        return signal_strength_;
 
90
    }
 
91
 
 
92
    CachedWirelessNetwork(
 
93
            const org::freedesktop::NetworkManager::Device& device,
 
94
            const org::freedesktop::NetworkManager::AccessPoint& ap)
 
95
        : device_(device),
 
96
          access_point_(ap)
 
97
    {
 
98
        try
 
99
        {
 
100
            last_seen_ = std::chrono::system_clock::time_point
 
101
            {
 
102
                std::chrono::system_clock::duration{access_point_.last_seen->get()}
 
103
            };
 
104
        } catch(const std::exception& e)
 
105
        {
 
106
            LOG(WARNING) << e.what();
 
107
        }
 
108
 
 
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
 
113
        {
 
114
            access_point_.frequency->get()
 
115
        };
 
116
        signal_strength_ = com::ubuntu::location::connectivity::WirelessNetwork::SignalStrength
 
117
        {
 
118
            int(access_point_.strength->get())
 
119
        };
 
120
 
 
121
        // Wire up all the connections
 
122
        access_point_.properties_changed->connect([this](const std::map<std::string, core::dbus::types::Variant>& dict)
 
123
        {
 
124
            on_access_point_properties_changed(dict);
 
125
        });
 
126
    }
 
127
 
 
128
    void on_access_point_properties_changed(const std::map<std::string, core::dbus::types::Variant>& dict)
 
129
    {
 
130
        // We route by string
 
131
        static const std::unordered_map<std::string, std::function<void(CachedWirelessNetwork&, const core::dbus::types::Variant&)> > lut
 
132
        {
 
133
            {
 
134
                org::freedesktop::NetworkManager::AccessPoint::HwAddress::name(),
 
135
                [](CachedWirelessNetwork& thiz, const core::dbus::types::Variant& value)
 
136
                {
 
137
                    thiz.bssid_ = value.as<org::freedesktop::NetworkManager::AccessPoint::HwAddress::ValueType>();
 
138
                }
 
139
            },
 
140
            {
 
141
                org::freedesktop::NetworkManager::AccessPoint::Ssid::name(),
 
142
                [](CachedWirelessNetwork& thiz, const core::dbus::types::Variant& value)
 
143
                {
 
144
                    thiz.ssid_ = utf8_ssid_to_string(value.as<org::freedesktop::NetworkManager::AccessPoint::Ssid::ValueType>());
 
145
                }
 
146
            },
 
147
            {
 
148
                org::freedesktop::NetworkManager::AccessPoint::Strength::name(),
 
149
                [](CachedWirelessNetwork& thiz, const core::dbus::types::Variant& value)
 
150
                {
 
151
                    thiz.signal_strength_ = com::ubuntu::location::connectivity::WirelessNetwork::SignalStrength
 
152
                    {
 
153
                        value.as<org::freedesktop::NetworkManager::AccessPoint::Strength::ValueType>()
 
154
                    };
 
155
                }
 
156
            },
 
157
            {
 
158
                org::freedesktop::NetworkManager::AccessPoint::Frequency::name(),
 
159
                [](CachedWirelessNetwork& thiz, const core::dbus::types::Variant& value)
 
160
                {
 
161
                    thiz.frequency_ = com::ubuntu::location::connectivity::WirelessNetwork::Frequency
 
162
                    {
 
163
                        value.as<org::freedesktop::NetworkManager::AccessPoint::Frequency::ValueType>()
 
164
                    };
 
165
                }
 
166
            },
 
167
            {
 
168
                org::freedesktop::NetworkManager::AccessPoint::Mode::name(),
 
169
                [](CachedWirelessNetwork& thiz, const core::dbus::types::Variant& value)
 
170
                {
 
171
                    thiz.mode_ = wifi_mode_from_ap_mode(value.as<org::freedesktop::NetworkManager::AccessPoint::Mode::ValueType>());
 
172
                }
 
173
            },
 
174
            {
 
175
                org::freedesktop::NetworkManager::AccessPoint::LastSeen::name(),
 
176
                [](CachedWirelessNetwork& thiz, const core::dbus::types::Variant& value)
 
177
                {
 
178
                    thiz.last_seen_ = std::chrono::system_clock::time_point
 
179
                    {
 
180
                        std::chrono::system_clock::duration
 
181
                        {
 
182
                            value.as<org::freedesktop::NetworkManager::AccessPoint::LastSeen::ValueType>()
 
183
                        }
 
184
                    };
 
185
                }
 
186
            }
 
187
        };
 
188
 
 
189
        for (const auto& pair : dict)
 
190
        {
 
191
            VLOG(1) << "Properties on access point " << ssid_.get() << " changed: \n"
 
192
                    << "  " << pair.first;
 
193
 
 
194
            // We do not treat failing property updates as fatal but instead just
 
195
            // log the issue for later analysis.
 
196
            try
 
197
            {
 
198
                if (lut.count(pair.first) > 0) lut.at(pair.first)(*this, pair.second);
 
199
            } catch (const std::exception& e)
 
200
            {
 
201
                LOG(WARNING) << "Exception while updating state for property change: " << pair.first;
 
202
            }
 
203
        }
 
204
    }
 
205
 
 
206
    org::freedesktop::NetworkManager::Device device_;
 
207
    org::freedesktop::NetworkManager::AccessPoint access_point_;
 
208
 
 
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_;
 
215
};
 
216
}
 
217
 
 
218
#endif // CACHED_WIRELESS_NETWORK_H_