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

« back to all changes in this revision

Viewing changes to src/location_service/com/ubuntu/location/connectivity/nm.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-2013 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 LOCATION_SERVICE_COM_UBUNTU_LOCATION_CONNECTIVITY_NM_H
 
19
#define LOCATION_SERVICE_COM_UBUNTU_LOCATION_CONNECTIVITY_NM_H
 
20
 
 
21
#include <core/dbus/bus.h>
 
22
#include <core/dbus/object.h>
 
23
#include <core/dbus/property.h>
 
24
#include <core/dbus/service.h>
 
25
#include <core/dbus/types/object_path.h>
 
26
#include <core/dbus/types/struct.h>
 
27
#include <core/dbus/types/stl/map.h>
 
28
#include <core/dbus/types/stl/string.h>
 
29
#include <core/dbus/types/stl/tuple.h>
 
30
#include <core/dbus/types/stl/vector.h>
 
31
 
 
32
namespace org
 
33
{
 
34
namespace freedesktop
 
35
{
 
36
struct NetworkManager
 
37
{
 
38
    typedef std::shared_ptr<NetworkManager> Ptr;
 
39
 
 
40
    struct AccessPoint
 
41
    {
 
42
        static const std::string& name()
 
43
        {
 
44
            static const std::string s{"org.freedesktop.NetworkManager.AccessPoint"};
 
45
            return s;
 
46
        }
 
47
 
 
48
        struct Frequency
 
49
        {
 
50
            static const std::string& name()
 
51
            {
 
52
                static const std::string s{"Frequency"};
 
53
                return s;
 
54
            }
 
55
 
 
56
            typedef AccessPoint Interface;
 
57
            typedef std::uint32_t ValueType;
 
58
            static const bool readable = true;
 
59
            static const bool writable = false;
 
60
        };
 
61
 
 
62
        struct LastSeen
 
63
        {
 
64
            static const std::string& name()
 
65
            {
 
66
                static const std::string s{"LastSeen"};
 
67
                return s;
 
68
            }
 
69
 
 
70
            typedef AccessPoint Interface;
 
71
            typedef std::uint32_t ValueType;
 
72
            static const bool readable = true;
 
73
            static const bool writable = false;
 
74
        };
 
75
 
 
76
        struct Mode
 
77
        {
 
78
            enum Value
 
79
            {
 
80
                unknown = 0,
 
81
                adhoc = 1,
 
82
                infra = 2,
 
83
                ap = 3
 
84
            };
 
85
 
 
86
            static const std::string& name()
 
87
            {
 
88
                static const std::string s{"Mode"};
 
89
                return s;
 
90
            }
 
91
 
 
92
            typedef AccessPoint Interface;
 
93
            typedef std::uint32_t ValueType;
 
94
            static const bool readable = true;
 
95
            static const bool writable = false;
 
96
        };
 
97
 
 
98
        struct HwAddress
 
99
        {
 
100
            static const std::string& name()
 
101
            {
 
102
                static const std::string s{"HwAddress"};
 
103
                return s;
 
104
            }
 
105
 
 
106
            typedef AccessPoint Interface;
 
107
            typedef std::string ValueType;
 
108
            static const bool readable = true;
 
109
            static const bool writable = false;
 
110
        };
 
111
 
 
112
        struct Ssid
 
113
        {
 
114
            static const std::string& name()
 
115
            {
 
116
                static const std::string s{"Ssid"};
 
117
                return s;
 
118
            }
 
119
 
 
120
            typedef AccessPoint Interface;
 
121
            typedef std::vector<std::int8_t> ValueType;
 
122
            static const bool readable = true;
 
123
            static const bool writable = false;
 
124
        };
 
125
 
 
126
        struct Strength
 
127
        {
 
128
            static const std::string& name()
 
129
            {
 
130
                static const std::string s{"Strength"};
 
131
                return s;
 
132
            }
 
133
 
 
134
            typedef AccessPoint Interface;
 
135
            typedef std::int8_t ValueType;
 
136
            static const bool readable = true;
 
137
            static const bool writable = false;
 
138
        };
 
139
 
 
140
        struct PropertiesChanged
 
141
        {
 
142
            inline static std::string name()
 
143
            {
 
144
                return "PropertiesChanged";
 
145
            }
 
146
 
 
147
            typedef AccessPoint Interface;
 
148
 
 
149
            typedef std::map<std::string, core::dbus::types::Variant> ArgumentType;
 
150
        };
 
151
 
 
152
        AccessPoint(const std::shared_ptr<core::dbus::Object>& object)
 
153
            : object(object),
 
154
              frequency(object->get_property<Frequency>()),
 
155
              last_seen(object->get_property<LastSeen>()),
 
156
              mode(object->get_property<Mode>()),
 
157
              hw_address(object->get_property<HwAddress>()),
 
158
              ssid(object->get_property<Ssid>()),
 
159
              strength(object->get_property<Strength>()),
 
160
              properties_changed(object->get_signal<PropertiesChanged>())
 
161
        {
 
162
        }
 
163
 
 
164
        std::shared_ptr<core::dbus::Object> object;
 
165
        std::shared_ptr<core::dbus::Property<Frequency>> frequency;
 
166
        std::shared_ptr<core::dbus::Property<LastSeen>> last_seen;
 
167
        std::shared_ptr<core::dbus::Property<Mode>> mode;
 
168
        std::shared_ptr<core::dbus::Property<HwAddress>> hw_address;
 
169
        std::shared_ptr<core::dbus::Property<Ssid>> ssid;
 
170
        std::shared_ptr<core::dbus::Property<Strength>> strength;
 
171
        std::shared_ptr<core::dbus::Signal<PropertiesChanged, PropertiesChanged::ArgumentType>> properties_changed;
 
172
    };
 
173
 
 
174
    struct Device
 
175
    {
 
176
        static const std::string& name()
 
177
        {
 
178
            static const std::string s{"org.freedesktop.NetworkManager.Device"};
 
179
            return s;
 
180
        }
 
181
 
 
182
        enum class Type
 
183
        {
 
184
            unknown = 0,
 
185
            ethernet = 1,
 
186
            wifi = 2,
 
187
            unused_1 = 3,
 
188
            unused_2 = 4,
 
189
            bluetooth = 5,
 
190
            olpc_mesh = 6,
 
191
            wimax = 7,
 
192
            modem = 8,
 
193
            infiniband = 9,
 
194
            bond = 10,
 
195
            vlan = 11,
 
196
            adsl = 12,
 
197
            bridge = 13
 
198
        };
 
199
 
 
200
        struct Wireless
 
201
        {
 
202
            static const std::string& name()
 
203
            {
 
204
                static const std::string s{"org.freedesktop.NetworkManager.Device.Wireless"};
 
205
                return s;
 
206
            }
 
207
 
 
208
            struct GetAccessPoints
 
209
            {
 
210
                static const std::string& name()
 
211
                {
 
212
                    static const std::string s{"GetAccessPoints"};
 
213
                    return s;
 
214
                }
 
215
 
 
216
                typedef Wireless Interface;
 
217
 
 
218
                static std::chrono::milliseconds default_timeout()
 
219
                {
 
220
                    return std::chrono::seconds{1};
 
221
                }
 
222
            };
 
223
 
 
224
            struct RequestScan
 
225
            {
 
226
                static const std::string& name()
 
227
                {
 
228
                    static const std::string s{"RequestScan"};
 
229
                    return s;
 
230
                }
 
231
 
 
232
                typedef Wireless Interface;
 
233
 
 
234
                static std::chrono::milliseconds default_timeout()
 
235
                {
 
236
                    return std::chrono::seconds{1};
 
237
                }
 
238
            };
 
239
 
 
240
            struct Signals
 
241
            {
 
242
                struct ScanDone
 
243
                {
 
244
                    inline static std::string name()
 
245
                    {
 
246
                        return "ScanDone";
 
247
                    }
 
248
 
 
249
                    typedef Wireless Interface;
 
250
                    typedef void ArgumentType;
 
251
                };
 
252
 
 
253
                struct AccessPointAdded
 
254
                {
 
255
                    inline static std::string name()
 
256
                    {
 
257
                        return "AccessPointAdded";
 
258
                    }
 
259
 
 
260
                    typedef Wireless Interface;
 
261
 
 
262
                    typedef core::dbus::types::ObjectPath ArgumentType;
 
263
                };
 
264
 
 
265
                struct AccessPointRemoved
 
266
                {
 
267
                    inline static std::string name()
 
268
                    {
 
269
                        return "AccessPointRemoved";
 
270
                    }
 
271
 
 
272
                    typedef Wireless Interface;
 
273
 
 
274
                    typedef core::dbus::types::ObjectPath ArgumentType;
 
275
                };
 
276
            };
 
277
        };
 
278
 
 
279
        struct DeviceType
 
280
        {
 
281
            static const std::string& name()
 
282
            {
 
283
                static const std::string s{"DeviceType"};
 
284
                return s;
 
285
            }
 
286
 
 
287
            typedef Device Interface;
 
288
            typedef std::uint32_t ValueType;
 
289
            static const bool readable = true;
 
290
            static const bool writable = false;
 
291
        };
 
292
 
 
293
        Device(const std::shared_ptr<core::dbus::Service>& service,
 
294
               const std::shared_ptr<core::dbus::Object>& object)
 
295
            : service(service),
 
296
              object(object),
 
297
              device_type(object->get_property<DeviceType>()),
 
298
              signals
 
299
              {
 
300
                  object->get_signal<Wireless::Signals::ScanDone>(),
 
301
                  object->get_signal<Wireless::Signals::AccessPointAdded>(),
 
302
                  object->get_signal<Wireless::Signals::AccessPointRemoved>()
 
303
              }
 
304
        {
 
305
        }
 
306
 
 
307
        Type type() const
 
308
        {
 
309
            return static_cast<Type>(device_type->get());
 
310
        }
 
311
 
 
312
        void for_each_access_point(const std::function<void(const core::dbus::types::ObjectPath&)>& f) const
 
313
        {
 
314
            typedef std::vector<core::dbus::types::ObjectPath> ResultType;
 
315
            auto result = object->transact_method<Wireless::GetAccessPoints, ResultType>();
 
316
 
 
317
            if (result.is_error())
 
318
                throw std::runtime_error(result.error().print());
 
319
 
 
320
            for (const auto& path : result.value())
 
321
                f(path);
 
322
        }
 
323
 
 
324
        std::vector<AccessPoint> get_access_points() const
 
325
        {
 
326
            typedef std::vector<core::dbus::types::ObjectPath> ResultType;
 
327
            auto result = object->invoke_method_synchronously<Wireless::GetAccessPoints, ResultType>();
 
328
 
 
329
            if (result.is_error())
 
330
                throw std::runtime_error(result.error().print());
 
331
 
 
332
            std::vector<AccessPoint> aps;
 
333
 
 
334
            for (const auto& path : result.value())
 
335
                aps.push_back(AccessPoint(service->object_for_path(path)));
 
336
 
 
337
            return aps;
 
338
        }
 
339
 
 
340
        void request_scan() const
 
341
        {
 
342
            static const std::map<std::string, core::dbus::types::Variant> dictionary;
 
343
            auto result = object->invoke_method_asynchronously<Wireless::RequestScan, void>(dictionary);
 
344
        }
 
345
 
 
346
        std::shared_ptr<core::dbus::Service> service;
 
347
        std::shared_ptr<core::dbus::Object> object;
 
348
        std::shared_ptr<core::dbus::Property<DeviceType>> device_type;
 
349
        struct
 
350
        {
 
351
            core::dbus::Signal<Wireless::Signals::ScanDone, Wireless::Signals::ScanDone::ArgumentType>::Ptr scan_done;
 
352
            core::dbus::Signal<Wireless::Signals::AccessPointAdded, Wireless::Signals::AccessPointAdded::ArgumentType>::Ptr ap_added;
 
353
            core::dbus::Signal<Wireless::Signals::AccessPointRemoved, Wireless::Signals::AccessPointRemoved::ArgumentType>::Ptr ap_removed;
 
354
        } signals;
 
355
    };
 
356
 
 
357
    static const std::string& name()
 
358
    {
 
359
        static const std::string s{"org.freedesktop.NetworkManager"};
 
360
        return s;
 
361
    }
 
362
 
 
363
    struct GetDevices
 
364
    {
 
365
        static const std::string& name()
 
366
        {
 
367
            static const std::string s{"GetDevices"};
 
368
            return s;
 
369
        }
 
370
 
 
371
        typedef NetworkManager Interface;
 
372
 
 
373
        static std::chrono::milliseconds default_timeout()
 
374
        {
 
375
            return std::chrono::seconds{1};
 
376
        }
 
377
    };
 
378
 
 
379
    struct Properties
 
380
    {
 
381
        struct Connectivity
 
382
        {
 
383
            enum Values
 
384
            {
 
385
                unknown = 0,
 
386
                none = 1,
 
387
                portal = 2,
 
388
                limited = 3,
 
389
                full = 4
 
390
            };
 
391
 
 
392
            static const std::string& name()
 
393
            {
 
394
                static const std::string s{"Connectivity"};
 
395
                return s;
 
396
            }
 
397
 
 
398
            typedef NetworkManager Interface;
 
399
            typedef std::uint32_t ValueType;
 
400
            static const bool readable = true;
 
401
            static const bool writable = false;
 
402
        };
 
403
    };
 
404
 
 
405
    struct Signals
 
406
    {
 
407
        struct DeviceAdded
 
408
        {
 
409
            inline static std::string name()
 
410
            {
 
411
                return "DeviceAdded";
 
412
            }
 
413
 
 
414
            typedef NetworkManager Interface;
 
415
 
 
416
            typedef core::dbus::types::ObjectPath ArgumentType;
 
417
        };
 
418
 
 
419
        struct DeviceRemoved
 
420
        {
 
421
            inline static std::string name()
 
422
            {
 
423
                return "DeviceRemoved";
 
424
            }
 
425
 
 
426
            typedef NetworkManager Interface;
 
427
 
 
428
            typedef core::dbus::types::ObjectPath ArgumentType;
 
429
        };
 
430
 
 
431
        struct PropertiesChanged
 
432
        {
 
433
            inline static std::string name()
 
434
            {
 
435
                return "PropertiesChanged";
 
436
            }
 
437
 
 
438
            typedef NetworkManager Interface;
 
439
 
 
440
            typedef std::map<std::string, core::dbus::types::Variant> ArgumentType;
 
441
        };
 
442
    };
 
443
 
 
444
    NetworkManager(const core::dbus::Bus::Ptr& bus)
 
445
        : service(core::dbus::Service::use_service<NetworkManager>(bus)),
 
446
          object(service->object_for_path(core::dbus::types::ObjectPath("/org/freedesktop/NetworkManager"))),
 
447
          properties
 
448
          {
 
449
              object->get_property<Properties::Connectivity>()
 
450
          },
 
451
          signals
 
452
          {
 
453
              object->get_signal<Signals::DeviceAdded>(),
 
454
              object->get_signal<Signals::DeviceRemoved>(),
 
455
              object->get_signal<Signals::PropertiesChanged>()
 
456
          }
 
457
    {
 
458
    }
 
459
 
 
460
    void for_each_device(const std::function<void(const core::dbus::types::ObjectPath&)>& f)
 
461
    {
 
462
        auto result =
 
463
                object->transact_method<
 
464
                    NetworkManager::GetDevices,
 
465
                    std::vector<core::dbus::types::ObjectPath>
 
466
                >();
 
467
 
 
468
        if (result.is_error())
 
469
            throw std::runtime_error(result.error().print());
 
470
 
 
471
        for (const auto& path : result.value())
 
472
            f(path);
 
473
    }
 
474
 
 
475
    Device device_for_path(const core::dbus::types::ObjectPath& path)
 
476
    {
 
477
        return Device(service, service->object_for_path(path));
 
478
    }
 
479
 
 
480
    std::vector<Device> get_devices()
 
481
    {
 
482
        auto result =
 
483
                object->invoke_method_synchronously<
 
484
                    NetworkManager::GetDevices,
 
485
                    std::vector<core::dbus::types::ObjectPath>>();
 
486
 
 
487
        if (result.is_error())
 
488
            throw std::runtime_error(result.error().print());
 
489
 
 
490
        std::vector<Device> devices;
 
491
        for (const auto& path : result.value())
 
492
        {
 
493
            devices.emplace_back(
 
494
                        Device(
 
495
                            service,
 
496
                            service->object_for_path(path)));
 
497
        }
 
498
 
 
499
        return devices;
 
500
    }
 
501
 
 
502
    std::shared_ptr<core::dbus::Service> service;
 
503
    std::shared_ptr<core::dbus::Object> object;
 
504
 
 
505
    struct
 
506
    {
 
507
        std::shared_ptr<core::dbus::Property<Properties::Connectivity> > connectivity;
 
508
    } properties;
 
509
    struct
 
510
    {
 
511
        core::dbus::Signal<Signals::DeviceAdded, Signals::DeviceAdded::ArgumentType>::Ptr device_added;
 
512
        core::dbus::Signal<Signals::DeviceRemoved, Signals::DeviceRemoved::ArgumentType>::Ptr device_removed;
 
513
        core::dbus::Signal<Signals::PropertiesChanged, Signals::PropertiesChanged::ArgumentType>::Ptr properties_changed;
 
514
    } signals;
 
515
};
 
516
}
 
517
}
 
518
 
 
519
#endif // LOCATION_SERVICE_COM_UBUNTU_LOCATION_CONNECTIVITY_NM_H