lomiri-location-service ..
An aggregating location service providing positioning and geocoding capabilities to applications.
codec.h
Go to the documentation of this file.
1/*
2 * Copyright © 2012-2013 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_LOMIRI_LOCATION_CODEC_H_
19#define LOCATION_SERVICE_COM_LOMIRI_LOCATION_CODEC_H_
20
33
34#include <core/dbus/codec.h>
35
36#include <sstream>
37
38namespace core
39{
40namespace dbus
41{
42namespace helper
43{
44template<>
45struct TypeMapper<com::lomiri::location::service::State>
46{
47 constexpr static ArgumentType type_value()
48 {
49 return ArgumentType::string;
50 }
51
52 constexpr static bool is_basic_type()
53 {
54 return true;
55 }
56 constexpr static bool requires_signature()
57 {
58 return false;
59 }
60
61 static std::string signature()
62 {
63 static const std::string s = TypeMapper<std::string>::signature();
64 return s;
65 }
66};
67}
68
69template<>
70struct Codec<com::lomiri::location::service::State>
71{
72 static void encode_argument(Message::Writer& writer, const com::lomiri::location::service::State& in)
73 {
74 std::stringstream ss; ss << in; auto s = ss.str();
75 writer.push_stringn(s.c_str(), s.size());
76 }
77
78 static void decode_argument(Message::Reader& reader, com::lomiri::location::service::State& in)
79 {
80 auto s = reader.pop_string();
81 std::stringstream ss{s}; ss >> in;
82 }
83};
84namespace helper
85{
86template<typename T>
87struct TypeMapper<com::lomiri::location::units::Quantity<T>>
88{
89 constexpr static ArgumentType type_value()
90 {
91 return ArgumentType::floating_point;
92 }
93
94 constexpr static bool is_basic_type()
95 {
96 return true;
97 }
98 constexpr static bool requires_signature()
99 {
100 return false;
101 }
102
103 static std::string signature()
104 {
105 static const std::string s = TypeMapper<double>::signature();
106 return s;
107 }
108};
109}
110
111template<typename T>
112struct Codec<com::lomiri::location::Optional<T>>
113{
114 static void encode_argument(Message::Writer& writer, const com::lomiri::location::Optional<T>& in)
115 {
116 bool has_value{in};
117 Codec<bool>::encode_argument(writer, has_value);
118 if (has_value)
119 Codec<typename com::lomiri::location::Optional<T>::value_type>::encode_argument(writer, *in);
120 }
121
122 static void decode_argument(Message::Reader& reader, com::lomiri::location::Optional<T>& in)
123 {
124 bool has_value{false};
125 Codec<bool>::decode_argument(reader, has_value);
126 if (has_value)
127 {
129 Codec<typename com::lomiri::location::Optional<T>::value_type>::decode_argument(reader, value);
130 in = value;
131 } else
132 {
133 in.reset();
134 }
135 }
136};
137
138template<typename T>
139struct Codec<com::lomiri::location::units::Quantity<T>>
140{
141 static void encode_argument(Message::Writer& writer, const com::lomiri::location::units::Quantity<T>& in)
142 {
143 Codec<typename com::lomiri::location::units::Quantity<T>::value_type>::encode_argument(writer, in.value());
144 }
145
146 static void decode_argument(Message::Reader& reader, com::lomiri::location::units::Quantity<T>& in)
147 {
149 Codec<typename com::lomiri::location::units::Quantity<T>::value_type>::decode_argument(reader, value);
151 }
152};
153
154template<typename T, typename U>
155struct Codec<com::lomiri::location::wgs84::Coordinate<T,U>>
156{
157 static void encode_argument(Message::Writer& writer, const com::lomiri::location::wgs84::Coordinate<T, U>& in)
158 {
159 Codec<com::lomiri::location::units::Quantity<U>>::encode_argument(writer, in.value);
160 }
161
162 static void decode_argument(Message::Reader& reader, com::lomiri::location::wgs84::Coordinate<T, U>& in)
163 {
164 Codec<com::lomiri::location::units::Quantity<U>>::decode_argument(reader, in.value);
165 }
166};
167
168template<>
169struct Codec<com::lomiri::location::Position>
170{
173
174 static void encode_argument(Message::Writer& writer, const com::lomiri::location::Position& in)
175 {
176 Codec<com::lomiri::location::wgs84::Latitude>::encode_argument(writer, in.latitude);
177 Codec<com::lomiri::location::wgs84::Longitude>::encode_argument(writer, in.longitude);
178 Codec<com::lomiri::location::Optional<com::lomiri::location::wgs84::Altitude>>::encode_argument(writer, in.altitude);
179
180 Codec<com::lomiri::location::Optional<HorizontalAccuracy>>::encode_argument(writer, in.accuracy.horizontal);
181 Codec<com::lomiri::location::Optional<VerticalAccuracy>>::encode_argument(writer, in.accuracy.vertical);
182 }
183
184 static void decode_argument(Message::Reader& reader, com::lomiri::location::Position& in)
185 {
186 Codec<com::lomiri::location::wgs84::Latitude>::decode_argument(reader, in.latitude);
187 Codec<com::lomiri::location::wgs84::Longitude>::decode_argument(reader, in.longitude);
188 Codec<com::lomiri::location::Optional<com::lomiri::location::wgs84::Altitude>>::decode_argument(reader, in.altitude);
189
190 Codec<com::lomiri::location::Optional<HorizontalAccuracy>>::decode_argument(reader, in.accuracy.horizontal);
191 Codec<com::lomiri::location::Optional<VerticalAccuracy>>::decode_argument(reader, in.accuracy.vertical);
192 }
193};
194
195
196namespace helper
197{
198template<>
199struct TypeMapper<com::lomiri::location::SpaceVehicle::Key>
200{
201 constexpr static ArgumentType type_value()
202 {
203 return ArgumentType::structure;
204 }
205 constexpr static bool is_basic_type()
206 {
207 return false;
208 }
209 constexpr static bool requires_signature()
210 {
211 return true;
212 }
213
214 static std::string signature()
215 {
216 static const std::string s =
217 helper::TypeMapper<std::uint32_t>::signature() +
218 helper::TypeMapper<std::uint32_t>::signature();
219 return s;
220 }
221};
222template<>
223struct TypeMapper<com::lomiri::location::SpaceVehicle>
224{
225 constexpr static ArgumentType type_value()
226 {
227 return ArgumentType::structure;
228 }
229 constexpr static bool is_basic_type()
230 {
231 return false;
232 }
233 constexpr static bool requires_signature()
234 {
235 return true;
236 }
237
238 inline static std::string signature()
239 {
240 std::string s =
241 DBUS_STRUCT_BEGIN_CHAR_AS_STRING +
242 helper::TypeMapper<com::lomiri::location::SpaceVehicle::Key>::signature() +
243 helper::TypeMapper<float>::signature() +
244 helper::TypeMapper<bool>::signature() +
245 helper::TypeMapper<bool>::signature() +
246 helper::TypeMapper<bool>::signature() +
247 helper::TypeMapper<com::lomiri::location::units::Quantity<com::lomiri::location::units::PlaneAngle>>::signature() +
248 helper::TypeMapper<com::lomiri::location::units::Quantity<com::lomiri::location::units::PlaneAngle>>::signature() +
249 DBUS_STRUCT_END_CHAR_AS_STRING;
250 return s;
251 }
252};
253}
254
255template<>
256struct Codec<com::lomiri::location::SpaceVehicle::Key>
257{
258 static void encode_argument(Message::Writer& writer, const com::lomiri::location::SpaceVehicle::Key& in)
259 {
260 writer.push_uint32(static_cast<std::uint32_t>(in.type));
261 writer.push_uint32(in.id);
262 }
263
264 static void decode_argument(Message::Reader& reader, com::lomiri::location::SpaceVehicle::Key& in)
265 {
266 in.type = static_cast<com::lomiri::location::SpaceVehicle::Type>(reader.pop_uint32());
267 in.id = reader.pop_uint32();
268 }
269};
270
271template<>
272struct Codec<com::lomiri::location::SpaceVehicle>
273{
274 inline static void encode_argument(Message::Writer& writer, const com::lomiri::location::SpaceVehicle& in)
275 {
276 auto sub = writer.open_structure();
277
278 Codec<com::lomiri::location::SpaceVehicle::Key>::encode_argument(sub, in.key);
279 sub.push_floating_point(in.snr);
280 sub.push_boolean(in.has_almanac_data);
281 sub.push_boolean(in.has_ephimeris_data);
282 sub.push_boolean(in.used_in_fix);
283 Codec<com::lomiri::location::units::Quantity<com::lomiri::location::units::PlaneAngle>>::encode_argument(sub, in.azimuth);
284 Codec<com::lomiri::location::units::Quantity<com::lomiri::location::units::PlaneAngle>>::encode_argument(sub, in.elevation);
285
286 writer.close_structure(std::move(sub));
287 }
288
289 inline static void decode_argument(Message::Reader& reader, com::lomiri::location::SpaceVehicle& in)
290 {
291 auto sub = reader.pop_structure();
292
293 Codec<com::lomiri::location::SpaceVehicle::Key>::decode_argument(sub, in.key);
294 in.snr = sub.pop_floating_point();
295 in.has_almanac_data = sub.pop_boolean();
296 in.has_ephimeris_data = sub.pop_boolean();
297 in.used_in_fix = sub.pop_boolean();
298 Codec<com::lomiri::location::units::Quantity<com::lomiri::location::units::PlaneAngle>>::decode_argument(sub, in.azimuth);
299 Codec<com::lomiri::location::units::Quantity<com::lomiri::location::units::PlaneAngle>>::decode_argument(sub, in.elevation);
300 }
301};
302
303namespace helper
304{
305template<>
306struct TypeMapper<std::map<com::lomiri::location::SpaceVehicle::Key, com::lomiri::location::SpaceVehicle>>
307{
308 constexpr static ArgumentType type_value()
309 {
310 return ArgumentType::array;
311 }
312 constexpr static bool is_basic_type()
313 {
314 return false;
315 }
316 constexpr static bool requires_signature()
317 {
318 return true;
319 }
320
321 static std::string signature()
322 {
323 static const std::string s = DBUS_TYPE_ARRAY_AS_STRING + TypeMapper<com::lomiri::location::SpaceVehicle>::signature();
324 return s;
325 }
326};
327}
328template<>
329struct Codec<std::map<com::lomiri::location::SpaceVehicle::Key, com::lomiri::location::SpaceVehicle>>
330{
331 inline static void encode_argument(Message::Writer& writer, const std::map<com::lomiri::location::SpaceVehicle::Key, com::lomiri::location::SpaceVehicle>& arg)
332 {
333 types::Signature signature(helper::TypeMapper<com::lomiri::location::SpaceVehicle>::signature());
334 auto sub = writer.open_array(signature);
335
336 for(const auto& element : arg)
337 {
338 Codec<com::lomiri::location::SpaceVehicle>::encode_argument(sub, element.second);
339 }
340
341 writer.close_array(std::move(sub));
342 }
343
344 inline static void decode_argument(Message::Reader& reader, std::map<com::lomiri::location::SpaceVehicle::Key, com::lomiri::location::SpaceVehicle>& out)
345 {
346 auto sub = reader.pop_array();
347 while (sub.type() != ArgumentType::invalid)
348 {
350 Codec<com::lomiri::location::SpaceVehicle>::decode_argument(sub, sv);
351 out.insert(std::make_pair(sv.key, sv));
352 }
353 }
354};
355
356template<>
357struct Codec<com::lomiri::location::Criteria>
358{
363
364 static void encode_argument(Message::Writer& writer, const com::lomiri::location::Criteria& in)
365 {
366 Codec<bool>::encode_argument(writer, in.requires.position);
367 Codec<bool>::encode_argument(writer, in.requires.altitude);
368 Codec<bool>::encode_argument(writer, in.requires.heading);
369 Codec<bool>::encode_argument(writer, in.requires.velocity);
370
371 Codec<HorizontalAccuracy>::encode_argument(writer, in.accuracy.horizontal);
372 Codec<com::lomiri::location::Optional<VerticalAccuracy>>::encode_argument(writer, in.accuracy.vertical);
373 Codec<com::lomiri::location::Optional<VelocityAccuracy>>::encode_argument(writer, in.accuracy.velocity);
374 Codec<com::lomiri::location::Optional<HeadingAccuracy>>::encode_argument(writer, in.accuracy.heading);
375 }
376
377 static void decode_argument(Message::Reader& reader, com::lomiri::location::Criteria& in)
378 {
379 Codec<bool>::decode_argument(reader, in.requires.position);
380 Codec<bool>::decode_argument(reader, in.requires.altitude);
381 Codec<bool>::decode_argument(reader, in.requires.heading);
382 Codec<bool>::decode_argument(reader, in.requires.velocity);
383
384 Codec<HorizontalAccuracy>::decode_argument(reader, in.accuracy.horizontal);
385 Codec<com::lomiri::location::Optional<VerticalAccuracy>>::decode_argument(reader, in.accuracy.vertical);
386 Codec<com::lomiri::location::Optional<VelocityAccuracy>>::decode_argument(reader, in.accuracy.velocity);
387 Codec<com::lomiri::location::Optional<HeadingAccuracy>>::decode_argument(reader, in.accuracy.heading);
388 }
389};
390
391template<>
392struct Codec<com::lomiri::location::Provider::Features>
393{
394 static void encode_argument(Message::Writer& writer, const com::lomiri::location::Provider::Features& in)
395 {
396 writer.push_int32(static_cast<std::int32_t>(in));
397 }
398
399 static void decode_argument(Message::Reader& reader, com::lomiri::location::Provider::Features& in)
400 {
401 in = static_cast<com::lomiri::location::Provider::Features>(reader.pop_int32());
402 }
403};
404
405template<>
406struct Codec<com::lomiri::location::Provider::Requirements>
407{
408 static void encode_argument(Message::Writer& writer, const com::lomiri::location::Provider::Requirements& in)
409 {
410 writer.push_int32(static_cast<std::int32_t>(in));
411 }
412
413 static void decode_argument(Message::Reader& reader, com::lomiri::location::Provider::Requirements& in)
414 {
415 in = static_cast<com::lomiri::location::Provider::Requirements>(reader.pop_int32());
416 }
417};
418
419template<>
420struct Codec<com::lomiri::location::WifiAndCellIdReportingState>
421{
422 static void encode_argument(Message::Writer& writer, const com::lomiri::location::WifiAndCellIdReportingState& in)
423 {
424 writer.push_int32(static_cast<std::int32_t>(in));
425 }
426
428 {
429 in = static_cast<com::lomiri::location::WifiAndCellIdReportingState>(reader.pop_int32());
430 }
431};
432
433namespace helper
434{
435template<typename T>
436struct TypeMapper<com::lomiri::location::Update<T>>
437{
438 constexpr static ArgumentType type_value()
439 {
440 return ArgumentType::structure;
441 }
442 constexpr static bool is_basic_type()
443 {
444 return false;
445 }
446 constexpr static bool requires_signature()
447 {
448 return true;
449 }
450
451 static std::string signature()
452 {
453 static const std::string s =
454 helper::TypeMapper<T>::signature() +
455 helper::TypeMapper<uint64_t>::signature();
456 return s;
457 }
458};
459}
460
461template<typename T>
462struct Codec<com::lomiri::location::Update<T>>
463{
464 static void encode_argument(Message::Writer& writer, const com::lomiri::location::Update<T>& in)
465 {
466 Codec<T>::encode_argument(writer, in.value);
467 Codec<int64_t>::encode_argument(writer, in.when.time_since_epoch().count());
468 }
469
470 static void decode_argument(Message::Reader& reader, com::lomiri::location::Update<T>& in)
471 {
472 Codec<T>::decode_argument(reader, in.value);
474 }
475};
476}
477}
478
479#endif // LOCATION_SERVICE_COM_LOMIRI_LOCATION_CODEC_H_
Features
Enumerates the known features that can be supported by providers.
Definition provider.h:54
Requirements
Enumerates the requirements of a provider implementation.
Definition provider.h:65
State
State enumerates the known states of the service.
Definition state.h:27
boost::units::quantity< Unit, double > Quantity
Definition units.h:60
boost::optional< T > Optional
Definition optional.h:31
Definition accuracy.h:24
Definition codec.h:39
std::chrono::high_resolution_clock::time_point Timestamp
Timestamp type of the location service clock.
Definition clock.h:45
std::chrono::high_resolution_clock::duration Duration
Duration type of the location service clock.
Definition clock.h:40
units::Quantity< units::Length > horizontal
The client requires measurements of at least this horizontal accuracy.
Definition criteria.h:53
Optional< units::Quantity< units::PlaneAngle > > heading
The client requires measurements of at least this heading accuracy.
Definition criteria.h:56
Optional< units::Quantity< units::Length > > vertical
The client requires measurements of at least this vertical accuracy.
Definition criteria.h:54
Optional< units::Quantity< units::Velocity > > velocity
The client requires measurements of at least this velocity accuracy.
Definition criteria.h:55
bool heading
The client needs heading measurements.
Definition criteria.h:48
bool altitude
The client needs altitude measurements.
Definition criteria.h:46
bool velocity
The client needs velocity measurments.
Definition criteria.h:47
bool position
The client needs position measurements.
Definition criteria.h:45
Summarizes criteria of a client session with respect to functionality and accuracy for position,...
Definition criteria.h:35
struct com::lomiri::location::Criteria::Accuracy accuracy
struct com::lomiri::location::Criteria::Requires requires
units::Quantity< units::Length > Vertical
Definition position.h:44
units::Quantity< units::Length > Horizontal
Definition position.h:43
Optional< Horizontal > horizontal
Definition position.h:46
The Position struct models a position in the wgs84 coordinate system.
Definition position.h:40
Optional< wgs84::Altitude > altitude
Definition position.h:61
wgs84::Longitude longitude
Definition position.h:60
Uniquely identifies a space vehicle, given its type and its id.
Id id
Unique id of the space vehicle.
Type type
The positioning system this vehicle belongs to.
A space-vehicle as visible to providers.
Type
Enumerates all known space-vehicle types.
Key key
Unique key identifying an instance.
units::Quantity< units::PlaneAngle > azimuth
Azimuth of SV.
bool has_almanac_data
Almanac data available for this vehicle.
units::Quantity< units::PlaneAngle > elevation
Elevation of SV.
bool used_in_fix
This vehicle has been used to obtain a fix.
bool has_ephimeris_data
Ephimeris data is available for this vehicle.
float snr
Signal to noise ratio;.
Templated class that wraps a value and timestamp.
Definition update.h:37
Clock::Timestamp when
Definition update.h:73
static void encode_argument(Message::Writer &writer, const com::lomiri::location::Criteria &in)
Definition codec.h:364
com::lomiri::location::units::Quantity< com::lomiri::location::units::PlaneAngle > HeadingAccuracy
Definition codec.h:362
com::lomiri::location::units::Quantity< com::lomiri::location::units::Velocity > VelocityAccuracy
Definition codec.h:361
com::lomiri::location::units::Quantity< com::lomiri::location::units::Length > HorizontalAccuracy
Definition codec.h:359
static void decode_argument(Message::Reader &reader, com::lomiri::location::Criteria &in)
Definition codec.h:377
com::lomiri::location::units::Quantity< com::lomiri::location::units::Length > VerticalAccuracy
Definition codec.h:360
static void encode_argument(Message::Writer &writer, const com::lomiri::location::Optional< T > &in)
Definition codec.h:114
static void decode_argument(Message::Reader &reader, com::lomiri::location::Optional< T > &in)
Definition codec.h:122
com::lomiri::location::Position::Accuracy::Vertical VerticalAccuracy
Definition codec.h:172
static void encode_argument(Message::Writer &writer, const com::lomiri::location::Position &in)
Definition codec.h:174
com::lomiri::location::Position::Accuracy::Horizontal HorizontalAccuracy
Definition codec.h:171
static void decode_argument(Message::Reader &reader, com::lomiri::location::Position &in)
Definition codec.h:184
static void decode_argument(Message::Reader &reader, com::lomiri::location::Provider::Features &in)
Definition codec.h:399
static void encode_argument(Message::Writer &writer, const com::lomiri::location::Provider::Features &in)
Definition codec.h:394
static void decode_argument(Message::Reader &reader, com::lomiri::location::Provider::Requirements &in)
Definition codec.h:413
static void encode_argument(Message::Writer &writer, const com::lomiri::location::Provider::Requirements &in)
Definition codec.h:408
static void encode_argument(Message::Writer &writer, const com::lomiri::location::SpaceVehicle &in)
Definition codec.h:274
static void decode_argument(Message::Reader &reader, com::lomiri::location::SpaceVehicle &in)
Definition codec.h:289
static void decode_argument(Message::Reader &reader, com::lomiri::location::SpaceVehicle::Key &in)
Definition codec.h:264
static void encode_argument(Message::Writer &writer, const com::lomiri::location::SpaceVehicle::Key &in)
Definition codec.h:258
static void decode_argument(Message::Reader &reader, com::lomiri::location::Update< T > &in)
Definition codec.h:470
static void encode_argument(Message::Writer &writer, const com::lomiri::location::Update< T > &in)
Definition codec.h:464
static void encode_argument(Message::Writer &writer, const com::lomiri::location::WifiAndCellIdReportingState &in)
Definition codec.h:422
static void decode_argument(Message::Reader &reader, com::lomiri::location::WifiAndCellIdReportingState &in)
Definition codec.h:427
static void encode_argument(Message::Writer &writer, const com::lomiri::location::service::State &in)
Definition codec.h:72
static void decode_argument(Message::Reader &reader, com::lomiri::location::service::State &in)
Definition codec.h:78
static void decode_argument(Message::Reader &reader, com::lomiri::location::units::Quantity< T > &in)
Definition codec.h:146
static void encode_argument(Message::Writer &writer, const com::lomiri::location::units::Quantity< T > &in)
Definition codec.h:141
static void encode_argument(Message::Writer &writer, const com::lomiri::location::wgs84::Coordinate< T, U > &in)
Definition codec.h:157
static void decode_argument(Message::Reader &reader, com::lomiri::location::wgs84::Coordinate< T, U > &in)
Definition codec.h:162
static void encode_argument(Message::Writer &writer, const std::map< com::lomiri::location::SpaceVehicle::Key, com::lomiri::location::SpaceVehicle > &arg)
Definition codec.h:331
static void decode_argument(Message::Reader &reader, std::map< com::lomiri::location::SpaceVehicle::Key, com::lomiri::location::SpaceVehicle > &out)
Definition codec.h:344