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

« back to all changes in this revision

Viewing changes to src/location_service/com/ubuntu/location/engine.cpp

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
#include <com/ubuntu/location/logging.h>
21
21
#include <com/ubuntu/location/provider_selection_policy.h>
 
22
#include <com/ubuntu/location/state_tracking_provider.h>
22
23
 
23
24
#include <iostream>
24
25
#include <stdexcept>
138
139
    return provider_selection_policy->determine_provider_selection_for_criteria(criteria, *this);
139
140
}
140
141
 
141
 
bool cul::Engine::has_provider(const cul::Provider::Ptr& provider) noexcept
142
 
{
143
 
    return providers.count(provider) > 0;
144
 
}
145
 
 
146
 
void cul::Engine::add_provider(const cul::Provider::Ptr& provider)
147
 
{
148
 
    if (!provider)
 
142
void cul::Engine::add_provider(const cul::Provider::Ptr& impl)
 
143
{
 
144
    if (!impl)
149
145
        throw std::runtime_error("Cannot add null provider");
150
146
 
 
147
    auto provider = std::make_shared<StateTrackingProvider>(impl);
 
148
 
151
149
    // We synchronize to the engine state.
152
150
    if (provider->requires(Provider::Requirements::satellites) && configuration.satellite_based_positioning_state == SatelliteBasedPositioningState::off)
153
151
        provider->state_controller()->disable();
207
205
        updates.last_known_location = update_policy->verify_update(src);
208
206
    });
209
207
 
210
 
    std::lock_guard<std::mutex> lg(guard);
211
 
    providers.emplace(provider, std::move(ProviderConnections{cp, ch, cv, cr, cs, cpr}));
212
 
}
213
 
 
214
 
void cul::Engine::remove_provider(const cul::Provider::Ptr& provider) noexcept
215
 
{
216
 
    std::lock_guard<std::mutex> lg(guard);
217
 
 
218
 
    auto it = providers.find(provider);
219
 
    if (it != providers.end())
 
208
    auto cps = provider->state().changed().connect([this](const StateTrackingProvider::State&)
220
209
    {
221
 
        providers.erase(it);
222
 
    }
 
210
        bool is_any_active = false;
 
211
 
 
212
        std::lock_guard<std::recursive_mutex> lg(guard);
 
213
        for (const auto& pair : providers)
 
214
            is_any_active = pair.first->state() == StateTrackingProvider::State::active;
 
215
 
 
216
        configuration.engine_state = is_any_active ? Engine::Status::active : Engine::Status::on;
 
217
    });
 
218
 
 
219
    std::lock_guard<std::recursive_mutex> lg(guard);
 
220
    providers.emplace(provider, std::move(ProviderConnections{cp, ch, cv, cr, cs, cpr, cps}));
223
221
}
224
222
 
225
223
void cul::Engine::for_each_provider(const std::function<void(const Provider::Ptr&)>& enumerator) const noexcept
226
224
{
227
 
    std::lock_guard<std::mutex> lg(guard);
 
225
    std::lock_guard<std::recursive_mutex> lg(guard);
228
226
    for (const auto& provider : providers)
229
227
    {
230
228
        try