1
#include "com/ubuntu/location/providers/geoclue/provider.h"
3
#include "com/ubuntu/location/providers/geoclue/geoclue.h"
7
namespace cul = com::ubuntu::location;
8
namespace culpg = com::ubuntu::location::providers::geoclue;
10
namespace dbus = org::freedesktop::dbus;
14
dbus::Bus::Ptr the_session_bus()
16
static dbus::Bus::Ptr session_bus = std::make_shared<dbus::Bus>(dbus::WellKnownBus::session);
21
struct culpg::Provider::Private
23
Private(const culpg::Provider::Configuration& config)
24
: bus(the_session_bus()),
25
service(dbus::Service::use_service(bus, config.name)),
26
object(service->object_for_path(config.path)),
27
signal_position_changed(object->get_signal<org::freedesktop::Geoclue::Position::Signals::PositionChanged>()),
28
signal_velocity_changed(object->get_signal<org::freedesktop::Geoclue::Velocity::Signals::VelocityChanged>())
34
if (!worker.joinable())
35
worker = std::move(std::thread{std::bind(&dbus::Bus::run, bus)});
41
if (worker.joinable())
46
dbus::Service::Ptr service;
47
dbus::Object::Ptr object;
49
org::freedesktop::Geoclue::Position::Signals::PositionChanged,
50
org::freedesktop::Geoclue::Position::Signals::PositionChanged::ArgumentType
51
>::Ptr signal_position_changed;
53
org::freedesktop::Geoclue::Velocity::Signals::VelocityChanged,
54
org::freedesktop::Geoclue::Velocity::Signals::VelocityChanged::ArgumentType
55
>::Ptr signal_velocity_changed;
56
dbus::signals::ScopedConnection position_updates_connection;
57
dbus::signals::ScopedConnection velocity_updates_connection;
62
cul::Provider::Ptr culpg::Provider::create_instance(const cul::ProviderFactory::Configuration& config)
64
culpg::Provider::Configuration pConfig;
65
pConfig.name = config.count("name") > 0 ? config.at("name") : throw std::runtime_error("Missing bus-name");
66
pConfig.path = config.count("path") > 0 ? config.at("path") : throw std::runtime_error("Missing bus-path");
67
return cul::Provider::Ptr{new culpg::Provider{pConfig}};
70
const cul::Provider::FeatureFlags& culpg::Provider::default_feature_flags()
72
static const cul::Provider::FeatureFlags flags{"001"};
76
const cul::Provider::RequirementFlags& culpg::Provider::default_requirement_flags()
78
static const cul::Provider::RequirementFlags flags{"1010"};
82
culpg::Provider::Provider(const culpg::Provider::Configuration& config)
83
: com::ubuntu::location::Provider(config.features, config.requirements),
84
d(new Private(config))
86
d->position_updates_connection =
87
d->signal_position_changed->connect(
88
[this](const org::freedesktop::Geoclue::Position::Signals::PositionChanged::ArgumentType& arg)
90
org::freedesktop::Geoclue::Position::FieldFlags flags{static_cast<unsigned long>(std::get<0>(arg))};
91
cul::Update<cul::Position> update
94
flags.test(org::freedesktop::Geoclue::Position::Field::latitude) ?
95
cul::wgs84::Latitude{std::get<2>(arg)* cul::units::Degrees} : cul::wgs84::Latitude{},
96
flags.test(org::freedesktop::Geoclue::Position::Field::longitude) ?
97
cul::wgs84::Longitude{std::get<3>(arg)* cul::units::Degrees} : cul::wgs84::Longitude{},
98
flags.test(org::freedesktop::Geoclue::Position::Field::altitude) ?
99
cul::wgs84::Altitude{std::get<4>(arg)* cul::units::Meters} : cul::wgs84::Altitude{}
103
this->deliver_position_updates(update);
106
d->velocity_updates_connection =
107
d->signal_velocity_changed->connect(
108
[this](const org::freedesktop::Geoclue::Velocity::Signals::VelocityChanged::ArgumentType& arg)
110
org::freedesktop::Geoclue::Velocity::FieldFlags flags{static_cast<unsigned long>(std::get<0>(arg))};
113
if (flags.test(org::freedesktop::Geoclue::Velocity::Field::speed))
115
cul::Update<cul::Velocity> update
117
std::get<2>(arg) * cul::units::MetersPerSecond,
120
this->deliver_velocity_updates(update);
123
if (flags.test(org::freedesktop::Geoclue::Velocity::Field::direction))
125
cul::Update<cul::Heading> update
127
std::get<3>(arg) * cul::units::Degrees,
131
this->deliver_heading_updates(update);
135
auto info = d->object->invoke_method_synchronously<
136
org::freedesktop::Geoclue::GetProviderInfo,
137
org::freedesktop::Geoclue::GetProviderInfo::ResultType>();
138
auto status = d->object->invoke_method_synchronously<
139
org::freedesktop::Geoclue::GetStatus,
140
org::freedesktop::Geoclue::GetStatus::ResultType>();
142
std::cout << "GeoclueProvider: ["
143
<< std::get<0>(info.value()) << ", "
144
<< std::get<1>(info.value()) << ","
145
<< static_cast<org::freedesktop::Geoclue::Status>(status.value()) << "]" <<std::endl;
148
culpg::Provider::~Provider() noexcept
153
bool culpg::Provider::matches_criteria(const cul::Criteria&)
158
void culpg::Provider::start_position_updates()
163
void culpg::Provider::stop_position_updates()
168
void culpg::Provider::start_velocity_updates()
173
void culpg::Provider::stop_velocity_updates()
178
void culpg::Provider::start_heading_updates()
183
void culpg::Provider::stop_heading_updates()