ubuntu-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_UBUNTU_LOCATION_CODEC_H_
19 #define LOCATION_SERVICE_COM_UBUNTU_LOCATION_CODEC_H_
20 
33 
34 #include <core/dbus/codec.h>
35 
36 #include <sstream>
37 
38 namespace core
39 {
40 namespace dbus
41 {
42 namespace helper
43 {
44 template<>
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 
69 template<>
71 {
72  static void encode_argument(Message::Writer& writer, const com::ubuntu::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::ubuntu::location::service::State& in)
79  {
80  auto s = reader.pop_string();
81  std::stringstream ss{s}; ss >> in;
82  }
83 };
84 namespace helper
85 {
86 template<typename T>
87 struct TypeMapper<com::ubuntu::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 
111 template<typename T>
113 {
114  static void encode_argument(Message::Writer& writer, const com::ubuntu::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::ubuntu::location::Optional<T>::value_type>::encode_argument(writer, *in);
120  }
121 
122  static void decode_argument(Message::Reader& reader, com::ubuntu::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::ubuntu::location::Optional<T>::value_type>::decode_argument(reader, value);
130  in = value;
131  } else
132  {
133  in.reset();
134  }
135  }
136 };
137 
138 template<typename T>
140 {
141  static void encode_argument(Message::Writer& writer, const com::ubuntu::location::units::Quantity<T>& in)
142  {
143  Codec<typename com::ubuntu::location::units::Quantity<T>::value_type>::encode_argument(writer, in.value());
144  }
145 
146  static void decode_argument(Message::Reader& reader, com::ubuntu::location::units::Quantity<T>& in)
147  {
149  Codec<typename com::ubuntu::location::units::Quantity<T>::value_type>::decode_argument(reader, value);
151  }
152 };
153 
154 template<typename T, typename U>
155 struct Codec<com::ubuntu::location::wgs84::Coordinate<T,U>>
156 {
157  static void encode_argument(Message::Writer& writer, const com::ubuntu::location::wgs84::Coordinate<T, U>& in)
158  {
159  Codec<com::ubuntu::location::units::Quantity<U>>::encode_argument(writer, in.value);
160  }
161 
162  static void decode_argument(Message::Reader& reader, com::ubuntu::location::wgs84::Coordinate<T, U>& in)
163  {
164  Codec<com::ubuntu::location::units::Quantity<U>>::decode_argument(reader, in.value);
165  }
166 };
167 
168 template<>
169 struct Codec<com::ubuntu::location::Position>
170 {
173 
174  static void encode_argument(Message::Writer& writer, const com::ubuntu::location::Position& in)
175  {
176  Codec<com::ubuntu::location::wgs84::Latitude>::encode_argument(writer, in.latitude);
177  Codec<com::ubuntu::location::wgs84::Longitude>::encode_argument(writer, in.longitude);
178  Codec<com::ubuntu::location::Optional<com::ubuntu::location::wgs84::Altitude>>::encode_argument(writer, in.altitude);
179 
180  Codec<com::ubuntu::location::Optional<HorizontalAccuracy>>::encode_argument(writer, in.accuracy.horizontal);
181  Codec<com::ubuntu::location::Optional<VerticalAccuracy>>::encode_argument(writer, in.accuracy.vertical);
182  }
183 
184  static void decode_argument(Message::Reader& reader, com::ubuntu::location::Position& in)
185  {
186  Codec<com::ubuntu::location::wgs84::Latitude>::decode_argument(reader, in.latitude);
187  Codec<com::ubuntu::location::wgs84::Longitude>::decode_argument(reader, in.longitude);
188  Codec<com::ubuntu::location::Optional<com::ubuntu::location::wgs84::Altitude>>::decode_argument(reader, in.altitude);
189 
190  Codec<com::ubuntu::location::Optional<HorizontalAccuracy>>::decode_argument(reader, in.accuracy.horizontal);
191  Codec<com::ubuntu::location::Optional<VerticalAccuracy>>::decode_argument(reader, in.accuracy.vertical);
192  }
193 };
194 
195 
196 namespace helper
197 {
198 template<>
199 struct TypeMapper<com::ubuntu::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 };
222 template<>
223 struct TypeMapper<com::ubuntu::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 +
243  helper::TypeMapper<float>::signature() +
244  helper::TypeMapper<bool>::signature() +
245  helper::TypeMapper<bool>::signature() +
246  helper::TypeMapper<bool>::signature() +
247  helper::TypeMapper<com::ubuntu::location::units::Quantity<com::ubuntu::location::units::PlaneAngle>>::signature() +
248  helper::TypeMapper<com::ubuntu::location::units::Quantity<com::ubuntu::location::units::PlaneAngle>>::signature() +
249  DBUS_STRUCT_END_CHAR_AS_STRING;
250  return s;
251  }
252 };
253 }
254 
255 template<>
256 struct Codec<com::ubuntu::location::SpaceVehicle::Key>
257 {
258  static void encode_argument(Message::Writer& writer, const com::ubuntu::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::ubuntu::location::SpaceVehicle::Key& in)
265  {
266  in.type = static_cast<com::ubuntu::location::SpaceVehicle::Type>(reader.pop_uint32());
267  in.id = reader.pop_uint32();
268  }
269 };
270 
271 template<>
272 struct Codec<com::ubuntu::location::SpaceVehicle>
273 {
274  inline static void encode_argument(Message::Writer& writer, const com::ubuntu::location::SpaceVehicle& in)
275  {
276  auto sub = writer.open_structure();
277 
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::ubuntu::location::units::Quantity<com::ubuntu::location::units::PlaneAngle>>::encode_argument(sub, in.azimuth);
284  Codec<com::ubuntu::location::units::Quantity<com::ubuntu::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::ubuntu::location::SpaceVehicle& in)
290  {
291  auto sub = reader.pop_structure();
292 
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::ubuntu::location::units::Quantity<com::ubuntu::location::units::PlaneAngle>>::decode_argument(sub, in.azimuth);
299  Codec<com::ubuntu::location::units::Quantity<com::ubuntu::location::units::PlaneAngle>>::decode_argument(sub, in.elevation);
300  }
301 };
302 
303 namespace helper
304 {
305 template<>
306 struct TypeMapper<std::map<com::ubuntu::location::SpaceVehicle::Key, com::ubuntu::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::ubuntu::location::SpaceVehicle>::signature();
324  return s;
325  }
326 };
327 }
328 template<>
329 struct Codec<std::map<com::ubuntu::location::SpaceVehicle::Key, com::ubuntu::location::SpaceVehicle>>
330 {
331  inline static void encode_argument(Message::Writer& writer, const std::map<com::ubuntu::location::SpaceVehicle::Key, com::ubuntu::location::SpaceVehicle>& arg)
332  {
334  auto sub = writer.open_array(signature);
335 
336  for(const auto& element : arg)
337  {
339  }
340 
341  writer.close_array(std::move(sub));
342  }
343 
344  inline static void decode_argument(Message::Reader& reader, std::map<com::ubuntu::location::SpaceVehicle::Key, com::ubuntu::location::SpaceVehicle>& out)
345  {
346  auto sub = reader.pop_array();
347  while (sub.type() != ArgumentType::invalid)
348  {
351  out.insert(std::make_pair(sv.key, sv));
352  }
353  }
354 };
355 
356 template<>
357 struct Codec<com::ubuntu::location::Criteria>
358 {
363 
364  static void encode_argument(Message::Writer& writer, const com::ubuntu::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::ubuntu::location::Optional<VerticalAccuracy>>::encode_argument(writer, in.accuracy.vertical);
373  Codec<com::ubuntu::location::Optional<VelocityAccuracy>>::encode_argument(writer, in.accuracy.velocity);
374  Codec<com::ubuntu::location::Optional<HeadingAccuracy>>::encode_argument(writer, in.accuracy.heading);
375  }
376 
377  static void decode_argument(Message::Reader& reader, com::ubuntu::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::ubuntu::location::Optional<VerticalAccuracy>>::decode_argument(reader, in.accuracy.vertical);
386  Codec<com::ubuntu::location::Optional<VelocityAccuracy>>::decode_argument(reader, in.accuracy.velocity);
387  Codec<com::ubuntu::location::Optional<HeadingAccuracy>>::decode_argument(reader, in.accuracy.heading);
388  }
389 };
390 
391 template<>
392 struct Codec<com::ubuntu::location::Provider::Features>
393 {
394  static void encode_argument(Message::Writer& writer, const com::ubuntu::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::ubuntu::location::Provider::Features& in)
400  {
401  in = static_cast<com::ubuntu::location::Provider::Features>(reader.pop_int32());
402  }
403 };
404 
405 template<>
406 struct Codec<com::ubuntu::location::Provider::Requirements>
407 {
408  static void encode_argument(Message::Writer& writer, const com::ubuntu::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::ubuntu::location::Provider::Requirements& in)
414  {
415  in = static_cast<com::ubuntu::location::Provider::Requirements>(reader.pop_int32());
416  }
417 };
418 
419 template<>
421 {
422  static void encode_argument(Message::Writer& writer, const com::ubuntu::location::WifiAndCellIdReportingState& in)
423  {
424  writer.push_int32(static_cast<std::int32_t>(in));
425  }
426 
427  static void decode_argument(Message::Reader& reader, com::ubuntu::location::WifiAndCellIdReportingState& in)
428  {
429  in = static_cast<com::ubuntu::location::WifiAndCellIdReportingState>(reader.pop_int32());
430  }
431 };
432 
433 namespace helper
434 {
435 template<typename T>
436 struct TypeMapper<com::ubuntu::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 
461 template<typename T>
462 struct Codec<com::ubuntu::location::Update<T>>
463 {
464  static void encode_argument(Message::Writer& writer, const com::ubuntu::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::ubuntu::location::Update<T>& in)
471  {
472  Codec<T>::decode_argument(reader, in.value);
474  }
475 };
476 }
477 }
478 
479 #endif // LOCATION_SERVICE_COM_UBUNTU_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
Enumerates all known system connectivity states.
Definition: manager.h:40
State
State enumerates the known states of the service.
Definition: state.h:27
boost::units::quantity< Unit, double > Quantity
Definition: units.h:53
boost::optional< T > Optional
Definition: optional.h:30
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::Velocity > > velocity
The client requires measurements of at least this velocity accuracy.
Definition: criteria.h:55
Optional< units::Quantity< units::Length > > vertical
The client requires measurements of at least this vertical accuracy.
Definition: criteria.h:54
bool position
The client needs position measurements.
Definition: criteria.h:45
bool heading
The client needs heading measurements.
Definition: criteria.h:48
bool velocity
The client needs velocity measurments.
Definition: criteria.h:47
bool altitude
The client needs altitude measurements.
Definition: criteria.h:46
Summarizes criteria of a client session with respect to functionality and accuracy for position,...
Definition: criteria.h:35
struct com::ubuntu::location::Criteria::Accuracy accuracy
struct com::ubuntu::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
wgs84::Longitude longitude
Definition: position.h:60
Optional< wgs84::Altitude > altitude
Definition: position.h:61
wgs84::Latitude latitude
Definition: position.h:59
Uniquely identifies a space vehicle, given its type and its id.
Definition: space_vehicle.h:53
Type type
The positioning system this vehicle belongs to.
Definition: space_vehicle.h:54
Id id
Unique id of the space vehicle.
Definition: space_vehicle.h:55
A space-vehicle as visible to providers.
Definition: space_vehicle.h:34
Key key
Unique key identifying an instance.
Definition: space_vehicle.h:86
bool has_ephimeris_data
Ephimeris data is available for this vehicle.
Definition: space_vehicle.h:89
float snr
Signal to noise ratio;.
Definition: space_vehicle.h:87
bool used_in_fix
This vehicle has been used to obtain a fix.
Definition: space_vehicle.h:90
units::Quantity< units::PlaneAngle > azimuth
Azimuth of SV.
Definition: space_vehicle.h:91
units::Quantity< units::PlaneAngle > elevation
Elevation of SV.
Definition: space_vehicle.h:92
Type
Enumerates all known space-vehicle types.
Definition: space_vehicle.h:40
bool has_almanac_data
Almanac data available for this vehicle.
Definition: space_vehicle.h:88
Templated class that wraps a value and timestamp.
Definition: update.h:37
Clock::Timestamp when
Definition: update.h:73
com::ubuntu::location::units::Quantity< com::ubuntu::location::units::Length > VerticalAccuracy
Definition: codec.h:360
com::ubuntu::location::units::Quantity< com::ubuntu::location::units::Velocity > VelocityAccuracy
Definition: codec.h:361
com::ubuntu::location::units::Quantity< com::ubuntu::location::units::Length > HorizontalAccuracy
Definition: codec.h:359
static void decode_argument(Message::Reader &reader, com::ubuntu::location::Criteria &in)
Definition: codec.h:377
static void encode_argument(Message::Writer &writer, const com::ubuntu::location::Criteria &in)
Definition: codec.h:364
com::ubuntu::location::units::Quantity< com::ubuntu::location::units::PlaneAngle > HeadingAccuracy
Definition: codec.h:362
static void decode_argument(Message::Reader &reader, com::ubuntu::location::Optional< T > &in)
Definition: codec.h:122
static void encode_argument(Message::Writer &writer, const com::ubuntu::location::Optional< T > &in)
Definition: codec.h:114
com::ubuntu::location::Position::Accuracy::Horizontal HorizontalAccuracy
Definition: codec.h:171
com::ubuntu::location::Position::Accuracy::Vertical VerticalAccuracy
Definition: codec.h:172
static void encode_argument(Message::Writer &writer, const com::ubuntu::location::Position &in)
Definition: codec.h:174
static void decode_argument(Message::Reader &reader, com::ubuntu::location::Position &in)
Definition: codec.h:184
static void decode_argument(Message::Reader &reader, com::ubuntu::location::Provider::Features &in)
Definition: codec.h:399
static void encode_argument(Message::Writer &writer, const com::ubuntu::location::Provider::Features &in)
Definition: codec.h:394
static void decode_argument(Message::Reader &reader, com::ubuntu::location::Provider::Requirements &in)
Definition: codec.h:413
static void encode_argument(Message::Writer &writer, const com::ubuntu::location::Provider::Requirements &in)
Definition: codec.h:408
static void encode_argument(Message::Writer &writer, const com::ubuntu::location::SpaceVehicle &in)
Definition: codec.h:274
static void decode_argument(Message::Reader &reader, com::ubuntu::location::SpaceVehicle &in)
Definition: codec.h:289
static void decode_argument(Message::Reader &reader, com::ubuntu::location::SpaceVehicle::Key &in)
Definition: codec.h:264
static void encode_argument(Message::Writer &writer, const com::ubuntu::location::SpaceVehicle::Key &in)
Definition: codec.h:258
static void decode_argument(Message::Reader &reader, com::ubuntu::location::Update< T > &in)
Definition: codec.h:470
static void encode_argument(Message::Writer &writer, const com::ubuntu::location::Update< T > &in)
Definition: codec.h:464
static void encode_argument(Message::Writer &writer, const com::ubuntu::location::WifiAndCellIdReportingState &in)
Definition: codec.h:422
static void decode_argument(Message::Reader &reader, com::ubuntu::location::WifiAndCellIdReportingState &in)
Definition: codec.h:427
static void decode_argument(Message::Reader &reader, com::ubuntu::location::service::State &in)
Definition: codec.h:78
static void encode_argument(Message::Writer &writer, const com::ubuntu::location::service::State &in)
Definition: codec.h:72
static void decode_argument(Message::Reader &reader, com::ubuntu::location::units::Quantity< T > &in)
Definition: codec.h:146
static void encode_argument(Message::Writer &writer, const com::ubuntu::location::units::Quantity< T > &in)
Definition: codec.h:141
static void decode_argument(Message::Reader &reader, com::ubuntu::location::wgs84::Coordinate< T, U > &in)
Definition: codec.h:162
static void encode_argument(Message::Writer &writer, const com::ubuntu::location::wgs84::Coordinate< T, U > &in)
Definition: codec.h:157
static void encode_argument(Message::Writer &writer, const std::map< com::ubuntu::location::SpaceVehicle::Key, com::ubuntu::location::SpaceVehicle > &arg)
Definition: codec.h:331
static void decode_argument(Message::Reader &reader, std::map< com::ubuntu::location::SpaceVehicle::Key, com::ubuntu::location::SpaceVehicle > &out)
Definition: codec.h:344