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

« back to all changes in this revision

Viewing changes to src/location_service/com/ubuntu/location/service/program_options.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:
19
19
#include <boost/program_options/options_description.hpp>
20
20
#include <boost/program_options/variables_map.hpp>
21
21
 
 
22
#include <core/dbus/well_known_bus.h>
 
23
 
22
24
#include <functional>
23
25
#include <iostream>
24
26
 
28
30
 
29
31
struct ProgramOptions
30
32
{
 
33
    struct Errors
 
34
    {
 
35
        struct OptionNotSet {};
 
36
    };
 
37
 
 
38
    struct Options
 
39
    {
 
40
        static const char* bus() { return "bus"; }
 
41
    };
 
42
 
31
43
    ProgramOptions(bool do_allow_unregistered = true) : allow_unregistered(do_allow_unregistered)
32
44
    {
 
45
        add(Options::bus(),
 
46
            "The well-known bus to connect to the service upon",
 
47
            std::string{"session"});
33
48
    }
34
49
 
35
50
    ProgramOptions& add(const char* name, const char* desc)
65
80
        return *this;
66
81
    }
67
82
 
68
 
    bool parse_from_command_line_args(int argc, char** argv)
 
83
    bool parse_from_command_line_args(int argc, char const** argv)
69
84
    {
70
85
        try
71
86
        {
86
101
        return true;
87
102
    }
88
103
 
 
104
    bool parse_from_environment()
 
105
    {
 
106
        try
 
107
        {
 
108
            auto parsed = boost::program_options::parse_environment(od, env_prefix);
 
109
            boost::program_options::store(parsed, vm);
 
110
            vm.notify();
 
111
        } catch(const std::runtime_error& e)
 
112
        {
 
113
            std::cerr << e.what() << std::endl;
 
114
            return false;
 
115
        }
 
116
 
 
117
        return true;
 
118
    }
 
119
 
 
120
    ProgramOptions& environment_prefix(const std::string& prefix)
 
121
    {
 
122
        env_prefix = prefix;
 
123
        return *this;
 
124
    }
 
125
 
 
126
    core::dbus::WellKnownBus bus()
 
127
    {
 
128
        static const std::map<std::string, core::dbus::WellKnownBus> lut =
 
129
        {
 
130
            {"session", core::dbus::WellKnownBus::session},
 
131
            {"system", core::dbus::WellKnownBus::system},
 
132
        };
 
133
 
 
134
        return lut.at(value_for_key<std::string>(Options::bus()));
 
135
    }
 
136
 
89
137
    template<typename T>
90
138
    T value_for_key(const std::string& key)
91
139
    {
92
140
        return vm[key].as<T>();
93
141
    }
94
142
 
 
143
    template<typename T>
 
144
    T value_for_key(const std::string& key) const
 
145
    {
 
146
        return vm[key].as<T>();
 
147
    }
 
148
 
95
149
    std::size_t value_count_for_key(const std::string& key)
96
150
    {
97
151
        return vm.count(key);
103
157
            enumerator(s);
104
158
    }
105
159
 
 
160
    void print(std::ostream& out) const
 
161
    {
 
162
        for (const auto& pair : vm)
 
163
            out << pair.first << ": " << (pair.second.defaulted() ? "default" : "set") << std::endl;
 
164
    }
 
165
 
106
166
    void print_help(std::ostream& out)
107
167
    {
108
168
        out << od;
109
169
    }
110
170
 
111
171
    bool allow_unregistered;
 
172
    std::string env_prefix;
112
173
    boost::program_options::options_description od;
113
174
    boost::program_options::variables_map vm;
114
175
    std::vector<std::string> unrecognized;