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

« back to all changes in this revision

Viewing changes to src/location_service/com/ubuntu/location/state_tracking_provider.h

Expose service::State to the bus. (LP: #1536774)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2016 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_STATE_TRACKING_PROVIDER_H_
 
19
#define LOCATION_SERVICE_COM_UBUNTU_LOCATION_STATE_TRACKING_PROVIDER_H_
 
20
 
 
21
#include <com/ubuntu/location/provider.h>
 
22
 
 
23
#include <memory>
 
24
 
 
25
namespace com
 
26
{
 
27
namespace ubuntu
 
28
{
 
29
namespace location
 
30
{
 
31
// StateTrackingProvider keeps track of the State of an individual provider instance.
 
32
//
 
33
// TODO(tvoss): Factor this functionality into the public interface once we find time
 
34
// to break API/ABI of providers.
 
35
class StateTrackingProvider : public Provider
 
36
{
 
37
public:
 
38
    // Safe us some typing
 
39
    typedef std::shared_ptr<StateTrackingProvider> Ptr;
 
40
 
 
41
    // State models the different states of a provider.
 
42
    enum class State
 
43
    {
 
44
        active, // The provider is actively delivering updates.
 
45
        enabled // The provider is enabled but not actively delivering updates.
 
46
    };
 
47
 
 
48
    StateTrackingProvider(const Provider::Ptr& impl)
 
49
        : impl_{impl},
 
50
          connections
 
51
          {
 
52
              impl_->updates().position.connect(
 
53
                  [this](const Update<Position>& u)
 
54
                  {
 
55
                      mutable_updates().position(u);
 
56
                  }),
 
57
              impl_->updates().heading.connect(
 
58
                  [this](const Update<Heading>& u)
 
59
                  {
 
60
                      mutable_updates().heading(u);
 
61
                  }),
 
62
              impl_->updates().velocity.connect(
 
63
                  [this](const Update<Velocity>& u)
 
64
                  {
 
65
                      mutable_updates().velocity(u);
 
66
                  })
 
67
           },
 
68
          state_{State::enabled}
 
69
    {
 
70
    }
 
71
 
 
72
    const core::Property<State>& state() const
 
73
    {
 
74
        return state_;
 
75
    }
 
76
 
 
77
    bool supports(const Features& f) const override
 
78
    {
 
79
        return impl_->supports(f);
 
80
    }
 
81
 
 
82
    bool requires(const Requirements& r) const override
 
83
    {
 
84
        return impl_->requires(r);
 
85
    }
 
86
 
 
87
    bool matches_criteria(const Criteria& criteria) override
 
88
    {
 
89
        return impl_->matches_criteria(criteria);
 
90
    }
 
91
 
 
92
    void on_wifi_and_cell_reporting_state_changed(WifiAndCellIdReportingState state) override
 
93
    {
 
94
        impl_->on_wifi_and_cell_reporting_state_changed(state);
 
95
    }
 
96
 
 
97
    void on_reference_location_updated(const Update<Position>& position) override
 
98
    {
 
99
        impl_->on_reference_location_updated(position);
 
100
    }
 
101
 
 
102
    void on_reference_velocity_updated(const Update<Velocity>& velocity) override
 
103
    {
 
104
        impl_->on_reference_velocity_updated(velocity);
 
105
    }
 
106
 
 
107
    void on_reference_heading_updated(const Update<Heading>& heading) override
 
108
    {
 
109
        impl_->on_reference_heading_updated(heading);
 
110
    }
 
111
 
 
112
    void start_position_updates() override
 
113
    {
 
114
        state_ = State::active;
 
115
        impl_->state_controller()->start_position_updates();
 
116
    }
 
117
 
 
118
    void stop_position_updates() override
 
119
    {
 
120
        state_ = State::enabled;
 
121
        impl_->state_controller()->stop_position_updates();
 
122
    }
 
123
 
 
124
    void start_velocity_updates() override
 
125
    {
 
126
        state_ = State::active;
 
127
        impl_->state_controller()->start_velocity_updates();
 
128
    }
 
129
 
 
130
    void stop_velocity_updates() override
 
131
    {
 
132
        state_ = State::enabled;
 
133
        impl_->state_controller()->stop_velocity_updates();
 
134
    }
 
135
 
 
136
    void start_heading_updates() override
 
137
    {
 
138
        state_ = State::active;
 
139
        impl_->state_controller()->start_heading_updates();
 
140
    }
 
141
 
 
142
    void stop_heading_updates() override
 
143
    {
 
144
        state_ = State::enabled;
 
145
        impl_->state_controller()->stop_heading_updates();
 
146
    }
 
147
 
 
148
private:
 
149
    Provider::Ptr impl_;
 
150
    struct
 
151
    {
 
152
        core::ScopedConnection position_updates;
 
153
        core::ScopedConnection heading_updates;
 
154
        core::ScopedConnection velocity_updates;
 
155
    } connections;
 
156
    core::Property<State> state_;
 
157
};
 
158
}
 
159
}
 
160
}
 
161
 
 
162
#endif // LOCATION_SERVICE_COM_UBUNTU_LOCATION_STATE_TRACKING_PROVIDER_H_