-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeoclueProvider.cpp
204 lines (173 loc) · 7.72 KB
/
geoclueProvider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "geoclueProvider.hpp"
#include "geoclue2.h"
#include "service.hpp"
#include "ulsProvider.hpp"
#include <core/dbus/property.h>
using namespace core::dbus;
using namespace com::ubuntu::location;
GeoclueObject::GeoclueObject(std::shared_ptr<Object> obj, Bus::Ptr bus, std::shared_ptr<core::dbus::Service> service) {
this->obj = obj;
this->bus = bus;
this->service = service;
}
Heading GeoclueObject::getHeadingFromLocation(std::shared_ptr<core::dbus::Object> lobj) {
// Transforms a Geoclue Location into a Ubuntu Location Service Heading
//
double heading = lobj->get_property<org::freedesktop::Geoclue2::Location::Heading>()->get();
return heading * units::Degrees;
}
Velocity GeoclueObject::getVelocityFromLocation(std::shared_ptr<core::dbus::Object> lobj) {
// Transforms a Geoclue Location into a Ubuntu Location Service Velocity
double velocity = lobj->get_property<org::freedesktop::Geoclue2::Location::Speed>()->get();
return velocity * units::MetersPerSecond;
}
Position GeoclueObject::getPositionFromLocation(std::shared_ptr<core::dbus::Object> lobj) {
// Transforms a Geoclue Location into a Ubuntu Location Service Position
Position pos;
double lat = lobj->get_property<org::freedesktop::Geoclue2::Location::Latitude>()->get();
double longitude = lobj->get_property<org::freedesktop::Geoclue2::Location::Longitude>()->get();
double altitude = lobj->get_property<org::freedesktop::Geoclue2::Location::Altitude>()->get();
pos.latitude = wgs84::Latitude{ lat * units::Degrees};
pos.longitude = wgs84::Longitude{ longitude * units::Degrees};
if (altitude >= -1e+308){
pos.altitude = wgs84::Altitude{ altitude * units::Meters };
}
pos.accuracy.horizontal = lobj->get_property<org::freedesktop::Geoclue2::Location::Accuracy>()->get() * units::Meters;
pos.accuracy.vertical = lobj->get_property<org::freedesktop::Geoclue2::Location::Accuracy>()->get() * units::Meters;
return pos;
}
void GeoclueObject::updateUsingLocationPath(types::ObjectPath op) {
// If available, updates the position, velocity, and heading
auto lobj = this->service->object_for_path(op);
// During testing, GeoClue sometimes claims not to support position even though the documentation said it does. Therefore, to not crash the program, we catch the exception
try {
if (this->status & this->client_status::position) {
this->uobj->emitPositionChangedSignal(this->getPositionFromLocation(lobj));
}
}
catch (...){
std::cerr << "Geoclue doesn't support position" << "\n";
}
// During testing, GeoClue sometimes claims not to support speed even though the documentation claims it does. Therefore, to not crash the program, we catch the exception
try {
if (this->status & this->client_status::velocity) {
this->uobj->emitVelocityChangedSignal(this->getVelocityFromLocation(lobj));
}
}
catch (...){
std::cerr << "Geoclue doesn't support speed" << "\n";
}
// During testing, GeoClue sometimes claims not to support heading even though the documentation claims it does. Therefore, to not crash the program, we catch the exception
try {
if (this->status & this->client_status::heading) {
this->uobj->emitHeadingChangedSignal(this->getHeadingFromLocation(lobj));
}
}
catch (...){
std::cerr << "Geoclue doesn't support heading" << "\n";
}
}
void GeoclueObject::connectPositionChangedSignal() {
// Adds a signal handler to handle location updates from Geoclue
this->luSignal = this->client->get_signal<org::freedesktop::Geoclue2::Client::LocationUpdated>();
this->luSignal->connect(
[this](const std::tuple<types::ObjectPath,types::ObjectPath> arg)
{
types::ObjectPath before;
types::ObjectPath after;
std::tie(before, after) = arg;
std::cerr << "Geoclue position changed" << "\n";
this->updateUsingLocationPath(after);
}
);
}
void GeoclueObject::prepareClient() {
/*
* We set the 'DistanceThreshold' property before starting the client. This property
* decides how often the 'LocationUpdated' signal is emitted. If the distance moved is below
* threshold, the signal won't be emitted. We have set this threshold to 10 meters.
* We set the 'RequestedAccuracy Level' property as well. This property is used to specify
* the level of accuracy requested by, or allowed by the client. We have set this to 8.
*/
std::cerr << "Prepare client";
this->client = this->GetClient();
std::cerr << client->path();
auto property = this->client->get_property<org::freedesktop::Geoclue2::Client::DistanceThreshold>();
property->set(10);
auto ral = this->client->get_property<org::freedesktop::Geoclue2::Client::RequestedAccuracyLevel>();
ral->set(8);
}
std::shared_ptr<Object> GeoclueObject::GetClient() {
// This method returns the path to the newly created client
auto res = this->obj->invoke_method_synchronously<org::freedesktop::Geoclue2::Manager::GetClient, types::ObjectPath>();
return this->service->object_for_path(res.value());
}
void GeoclueObject::authorize() {
// Authorizes the client by setting the DesktopId property
auto property = this->client->get_property<org::freedesktop::Geoclue2::Client::DesktopId>();
property->set("geoclue2-provider");
}
void GeoclueObject::startClient() {
// Start the Geoclue Client
std::cerr << "Start client" << "\n";
this->prepareClient();
this->connectPositionChangedSignal();
this->authorize();
this->client->invoke_method_asynchronously<org::freedesktop::Geoclue2::Client::Start, void>();
auto current_loc = this->client->get_property<org::freedesktop::Geoclue2::Client::Location>()->get();
// If the location property is already set, use it
if (current_loc != types::ObjectPath("/")){
this->updateUsingLocationPath(current_loc);
}
}
void GeoclueObject::stopClient() {
// Stops the Geoclue client again
std::cerr << "Stop client" << "\n";
this->client->invoke_method_synchronously<org::freedesktop::Geoclue2::Client::Stop, void>();
}
void GeoclueObject::startVelocityUpdates() {
auto status = this->status.fetch_or(this->client_status::velocity);
if (!status) {
this->startClient();
}
else {
this->updateUsingLocationPath(this->client->get_property<org::freedesktop::Geoclue2::Client::Location>()->get());
}
}
void GeoclueObject::stopVelocityUpdates() {
auto status = this->status.fetch_and(~this->client_status::velocity);
if (!(status & ~this->client_status::position)) {
this->stopClient();
}
}
void GeoclueObject::startHeadingUpdates() {
auto status = this->status.fetch_or(this->client_status::heading);
if (!status) {
this->startClient();
}
else {
this->updateUsingLocationPath(this->client->get_property<org::freedesktop::Geoclue2::Client::Location>()->get());
}
}
void GeoclueObject::stopHeadingUpdates() {
auto status = this->status.fetch_and(~this->client_status::heading);
if (!(status & ~this->client_status::position)) {
this->stopClient();
}
}
void GeoclueObject::startPositionUpdates() {
// Start receiving events about current location
auto status = this->status.fetch_or(this->client_status::position);
if (!status) {
this->startClient();
}
else {
this->updateUsingLocationPath(this->client->get_property<org::freedesktop::Geoclue2::Client::Location>()->get());
}
}
void GeoclueObject::stopPositionUpdates() {
auto status = this->status.fetch_and(~this->client_status::position);
if (!(status & ~this->client_status::position)) {
this->stopClient();
}
}