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