~thomas-voss/location-service/add-snapcraft-setup-next

« back to all changes in this revision

Viewing changes to include/location/provider.h

Merge lp:~thomas-voss/location-service/simplify-provider-interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#ifndef LOCATION_PROVIDER_H_
19
19
#define LOCATION_PROVIDER_H_
20
20
 
 
21
#include <location/configuration.h>
21
22
#include <location/criteria.h>
 
23
#include <location/event.h>
22
24
#include <location/heading.h>
23
25
#include <location/position.h>
24
26
#include <location/space_vehicle.h>
35
37
 
36
38
namespace location
37
39
{
38
 
/**
39
 
 * @brief The Provider class is the abstract base of all positioning providers.
40
 
 */
41
 
class Provider
 
40
/// @brief The Provider class is the abstract base of all positioning providers.
 
41
class Provider : public Event::Receiver
42
42
{
43
43
public:
44
 
    typedef std::shared_ptr<Provider> Ptr;
45
 
 
46
 
    /**
47
 
     * @brief Enumerates the known features that can be supported by providers.
48
 
     */
49
 
    enum class Features : std::size_t
50
 
    {
51
 
        none = 0, ///< The provider does not support any feature.
52
 
        position = 1 << 0, ///< The provider features position updates.
53
 
        velocity = 1 << 1, ///< The provider features velocity updates.
54
 
        heading = 1 << 2 ///< The provider features heading updates.
55
 
    };    
56
 
 
57
 
    /**
58
 
     * @brief Enumerates the requirements of a provider implementation.
59
 
     */
 
44
    typedef std::shared_ptr<Provider> Ptr;    
 
45
 
 
46
    /// @brief Enumerates the requirements of a provider implementation.
60
47
    enum class Requirements : std::size_t
61
48
    {
62
 
        none = 0, ///< The provider does not require anything.
63
 
        satellites = 1 << 0, ///< The provider requires satellites to be visible.
64
 
        cell_network = 1 << 1, ///< The provider requires a cell-network to work correctly.
65
 
        data_network = 1 << 2, ///< The provider requires a data-network to work correctly.
66
 
        monetary_spending = 1 << 3 ///< Using the provider results in monetary cost.
67
 
    };
68
 
 
69
 
    /**
70
 
     * @brief Facade for controlling the state of position/heading/velocity updates.
71
 
     *
72
 
     * Multiple observers can request state changes for updates. This class ensures
73
 
     * that the specific updates are started and stopped if at least one observer
74
 
     * requests them and stopped when the last observer issues a stop request.
75
 
     */
76
 
    class Controller
77
 
    {
78
 
    public:
79
 
        typedef std::shared_ptr<Controller> Ptr; 
80
 
 
81
 
        virtual ~Controller() = default;
82
 
        Controller(const Controller&) = delete;
83
 
        Controller& operator=(const Controller&) = delete;        
84
 
 
85
 
        /**
86
 
         * @brief disable switches the provider to a disabled state, such that subsequent
87
 
         * calls to start* methods fail.
88
 
         */
89
 
        void disable();
90
 
 
91
 
        /**
92
 
         * @brief enable switches the provider to an enabled state, such that subsequent
93
 
         * calls to start* methods succeed.
94
 
         */
95
 
        void enable();
96
 
 
97
 
        /**
98
 
         * @brief Request to start position updates if not already running.
99
 
         */
100
 
        virtual void start_position_updates();
101
 
 
102
 
        /**
103
 
         * @brief Request to stop position updates. Only stops the provider when the last observer calls this function.
104
 
         */
105
 
        virtual void stop_position_updates();
106
 
 
107
 
        /**
108
 
         * @brief Checks if position updates are currently running.
109
 
         * @return true iff position updates are currently running.
110
 
         */
111
 
        bool are_position_updates_running() const;
112
 
 
113
 
        /**
114
 
         * @brief Request to start heading updates if not already running.
115
 
         */
116
 
        virtual void start_heading_updates();
117
 
 
118
 
        /**
119
 
         * @brief Request to stop heading updates. Only stops the provider when the last observer calls this function.
120
 
         */
121
 
        virtual void stop_heading_updates();
122
 
 
123
 
        /**
124
 
         * @brief Checks if position updates are currently running.
125
 
         * @return true iff position updates are currently running.
126
 
         */
127
 
        bool are_heading_updates_running() const;
128
 
 
129
 
        /**
130
 
         * @brief Request to start velocity updates if not already running.
131
 
         */
132
 
        virtual void start_velocity_updates();
133
 
 
134
 
        /**
135
 
         * @brief Request to stop velocity updates. Only stops the provider when the last observer calls this function.
136
 
         */
137
 
        virtual void stop_velocity_updates();
138
 
 
139
 
        /**
140
 
         * @brief Checks if velocity updates are currently running.
141
 
         * @return true iff velocity updates are currently running.
142
 
         */
143
 
        bool are_velocity_updates_running() const;
144
 
 
145
 
    protected:
146
 
        friend class Provider;
147
 
        explicit Controller(Provider& instance);
148
 
 
149
 
    private:
150
 
        Provider& instance;
151
 
        std::atomic<int> position_updates_counter;
152
 
        std::atomic<int> heading_updates_counter;
153
 
        std::atomic<int> velocity_updates_counter;
154
 
    };
155
 
 
156
 
    /**
157
 
     * @brief Wraps all updates that can be delivered by a provider.
158
 
     */
159
 
    struct Updates
160
 
    {
161
 
        /** Position updates. */
162
 
        core::Signal<Update<Position>> position;
163
 
        /** Heading updates. */
164
 
        core::Signal<Update<Heading>> heading;
165
 
        /** Velocity updates. */
166
 
        core::Signal<Update<Velocity>> velocity;
167
 
        /** Space vehicle visibility updates. */
168
 
        core::Signal<Update<std::set<SpaceVehicle>>> svs;
169
 
    };
170
 
 
 
49
        none = 0,                   ///< The provider does not require anything.
 
50
        satellites = 1 << 0,        ///< The provider requires satellites to be visible.
 
51
        cell_network = 1 << 1,      ///< The provider requires a cell-network to work correctly.
 
52
        data_network = 1 << 2,      ///< The provider requires a data-network to work correctly.
 
53
        monetary_spending = 1 << 3  ///< Using the provider results in monetary cost.
 
54
    };
 
55
 
 
56
    /// @cond
171
57
    virtual ~Provider() = default;
172
58
 
173
59
    Provider(const Provider&) = delete;
 
60
    Provider(Provider&&) = delete;
174
61
    Provider& operator=(const Provider&) = delete;
175
 
 
176
 
    /**
177
 
     * @brief Provides non-mutable access to this provider's updates.
178
 
     * @return A non-mutable reference to the updates.
179
 
     */
180
 
    virtual const Updates& updates() const;
181
 
 
182
 
    /**
183
 
     * @brief Access to the controller facade of this provider instance.
184
 
     */
185
 
    virtual const Controller::Ptr& state_controller() const;
186
 
 
187
 
    /**
188
 
     * @brief Checks if the provider supports a specific feature.
189
 
     * @param f Feature to test for
190
 
     * @return true iff the provider supports the feature.
191
 
     */
192
 
    virtual bool supports(const Features& f) const;
193
 
 
194
 
    /**
195
 
     * @brief Checks if the provider has got a specific requirement.
196
 
     * @param r Requirement to test for.
197
 
     * @return true iff the provider has the specific requirement.
198
 
     */
199
 
    virtual bool requires(const Requirements& r) const;
200
 
 
201
 
    /**
202
 
     * @brief Checks if a provider satisfies a set of accuracy criteria.
203
 
     * @param [in] criteria The criteria to check.
204
 
     * @return true iff the provider satisfies the given criteria.
205
 
     */
206
 
    virtual bool matches_criteria(const Criteria& criteria);
207
 
 
208
 
    /**
209
 
     * @brief Called by the engine whenever the wifi and cell ID reporting state changes.
210
 
     * @param state The new state.
211
 
     */
212
 
    virtual void on_wifi_and_cell_reporting_state_changed(WifiAndCellIdReportingState state);
213
 
 
214
 
    /**
215
 
     * @brief Called by the engine whenever the reference location changed.
216
 
     * @param position The new reference location.
217
 
     */
218
 
    virtual void on_reference_location_updated(const Update<Position>& position);
219
 
 
220
 
    /**
221
 
     * @brief Called by the engine whenever the reference velocity changed.
222
 
     * @param velocity The new reference velocity.
223
 
     */
224
 
    virtual void on_reference_velocity_updated(const Update<Velocity>& velocity);
225
 
 
226
 
    /**
227
 
     * @brief Called by the engine whenever the reference heading changed.
228
 
     * @param heading The new reference heading.
229
 
     */
230
 
    virtual void on_reference_heading_updated(const Update<Heading>& heading);
 
62
    Provider& operator=(Provider&&) = delete;
 
63
    /// @endcond
 
64
 
 
65
    /// @brief enable enables the provider, throws in case of issues.
 
66
    virtual void enable() = 0;
 
67
 
 
68
    /// @brief start disables the provider, throws in case of issues.
 
69
    virtual void disable() = 0;
 
70
 
 
71
    /// @brief activate triggers a state transition from enabled to active.
 
72
    virtual void activate() = 0;
 
73
 
 
74
    /// @brief deactivate triggers a state transition from active to enabled.
 
75
    virtual void deactivate() = 0;
 
76
 
 
77
    /// @brief requirements returns the requirements of the provider.
 
78
    virtual Requirements requirements() const = 0;
 
79
 
 
80
    /// @brief Checks if a provider satisfies criteria.
 
81
    /// @param [in] criteria The criteria to check.
 
82
    /// @return true iff the provider satisfies the given criteria.
 
83
    virtual bool satisfies(const Criteria& criteria) = 0;
 
84
 
 
85
    /// @brief position_updates returns a signal delivering position updates.
 
86
    virtual const core::Signal<Update<Position>>& position_updates() const = 0;
 
87
 
 
88
    /// @brief heading_updates returns a signal delivering heading updates.
 
89
    virtual const core::Signal<Update<Heading>>& heading_updates() const = 0;
 
90
 
 
91
    /// @brief velocity_updates returns a signal delivering velocity updates.
 
92
    virtual const core::Signal<Update<Velocity>>& velocity_updates() const = 0;
231
93
 
232
94
protected:
233
 
    explicit Provider(
234
 
        const Features& features = Features::none,
235
 
        const Requirements& requirements = Requirements::none);
236
 
 
237
 
    virtual Updates& mutable_updates();
238
 
 
239
 
    /**
240
 
     * @brief Implementation-specific, empty by default.
241
 
     */
242
 
    virtual void start_position_updates();
243
 
 
244
 
    /**
245
 
     * @brief Implementation-specific, empty by default.
246
 
     */
247
 
    virtual void stop_position_updates();
248
 
 
249
 
    /**
250
 
     * @brief Implementation-specific, empty by default.
251
 
     */
252
 
    virtual void start_heading_updates();
253
 
 
254
 
    /**
255
 
     * @brief Implementation-specific, empty by default.
256
 
     */
257
 
    virtual void stop_heading_updates();
258
 
 
259
 
    /**
260
 
     * @brief Implementation-specific, empty by default.
261
 
     */
262
 
    virtual void start_velocity_updates();
263
 
 
264
 
    /**
265
 
     * @brief Implementation-specific, empty by default.
266
 
     */
267
 
    virtual void stop_velocity_updates();
268
 
 
269
 
private:
270
 
    struct
271
 
    {
272
 
        Features features = Features::none;
273
 
        Requirements requirements = Requirements::none;
274
 
        Updates updates;
275
 
        Controller::Ptr controller = Controller::Ptr{};
276
 
    } d;
 
95
    Provider() = default;
277
96
};
278
97
 
279
 
Provider::Features operator|(Provider::Features lhs, Provider::Features rhs);
280
 
Provider::Features operator&(Provider::Features lhs, Provider::Features rhs);
281
 
 
 
98
/// @brief operator| returns the bitwise or of lhs and rhs.
282
99
Provider::Requirements operator|(Provider::Requirements lhs, Provider::Requirements rhs);
 
100
/// @brief operator& returns the bitwise and of lhs and rhs.
283
101
Provider::Requirements operator&(Provider::Requirements lhs, Provider::Requirements rhs);
284
102
}
285
103