ubuntu-location-service  0.0.2
codec.h
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 
21 #include "com/ubuntu/location/accuracy.h"
22 #include "com/ubuntu/location/criteria.h"
23 #include "com/ubuntu/location/heading.h"
24 #include "com/ubuntu/location/position.h"
25 #include "com/ubuntu/location/update.h"
26 #include "com/ubuntu/location/velocity.h"
27 #include "com/ubuntu/location/units/units.h"
28 #include "com/ubuntu/location/wgs84/altitude.h"
29 #include "com/ubuntu/location/wgs84/latitude.h"
30 #include "com/ubuntu/location/wgs84/longitude.h"
31 
32 #include <core/dbus/codec.h>
33 
34 namespace core
35 {
36 namespace dbus
37 {
38 namespace helper
39 {
40 template<typename T>
41 struct TypeMapper<com::ubuntu::location::units::Quantity<T>>
42 {
43  constexpr static ArgumentType type_value()
44  {
45  return ArgumentType::floating_point;
46  }
47  constexpr static bool is_basic_type()
48  {
49  return true;
50  }
51  constexpr static bool requires_signature()
52  {
53  return false;
54  }
55 
56  static std::string signature()
57  {
58  static const std::string s = TypeMapper<double>::signature();
59  return s;
60  }
61 };
62 }
63 
64 template<typename T>
65 struct Codec<com::ubuntu::location::units::Quantity<T>>
66 {
67  static void encode_argument(core::dbus::Message::Writer& out, const com::ubuntu::location::units::Quantity<T>& in)
68  {
69  Codec<typename com::ubuntu::location::units::Quantity<T>::value_type>::encode_argument(out, in.value());
70  }
71 
72  static void decode_argument(core::dbus::Message::Reader& out, com::ubuntu::location::units::Quantity<T>& in)
73  {
74  typename com::ubuntu::location::units::Quantity<T>::value_type value;
75  Codec<typename com::ubuntu::location::units::Quantity<T>::value_type>::decode_argument(out, value);
76  in = com::ubuntu::location::units::Quantity<T>::from_value(value);
77  }
78 };
79 
80 namespace helper
81 {
82 template<typename T, typename U>
83 struct TypeMapper<com::ubuntu::location::wgs84::Coordinate<T,U>>
84 {
85  constexpr static ArgumentType type_value()
86  {
87  return ArgumentType::structure;
88  }
89  constexpr static bool is_basic_type()
90  {
91  return false;
92  }
93  constexpr static bool requires_signature()
94  {
95  return true;
96  }
97 
98  static std::string signature()
99  {
100  static const std::string s =
101  DBUS_STRUCT_BEGIN_CHAR_AS_STRING +
102  TypeMapper<com::ubuntu::location::units::Quantity<U>>::signature() +
103  DBUS_STRUCT_END_CHAR_AS_STRING;
104  return s;
105  }
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(core::dbus::Message::Writer& out, const com::ubuntu::location::wgs84::Coordinate<T, U>& in)
113  {
114  Codec<com::ubuntu::location::units::Quantity<U>>::encode_argument(out, in.value);
115  }
116 
117  static void decode_argument(core::dbus::Message::Reader& out, com::ubuntu::location::wgs84::Coordinate<T, U>& in)
118  {
119  Codec<com::ubuntu::location::units::Quantity<U>>::decode_argument(out, in.value);
120  }
121 };
122 
123 namespace helper
124 {
125 template<>
126 struct TypeMapper<com::ubuntu::location::Position>
127 {
128  constexpr static ArgumentType type_value()
129  {
130  return ArgumentType::structure;
131  }
132  constexpr static bool is_basic_type()
133  {
134  return false;
135  }
136  constexpr static bool requires_signature()
137  {
138  return true;
139  }
140 
141  static std::string signature()
142  {
143  static const std::string s =
144  TypeMapper<uint64_t>::signature() +
145  TypeMapper<com::ubuntu::location::wgs84::Latitude>::signature() +
146  TypeMapper<com::ubuntu::location::wgs84::Longitude>::signature() +
147  TypeMapper<com::ubuntu::location::wgs84::Altitude>::signature();
148  return s;
149  }
150 };
151 }
152 
153 template<>
154 struct Codec<com::ubuntu::location::Position>
155 {
156  static void encode_argument(core::dbus::Message::Writer& out, const com::ubuntu::location::Position& in)
157  {
158  Codec<uint64_t>::encode_argument(out, in.flags().to_ulong());
159  if (in.has_latitude())
160  Codec<com::ubuntu::location::wgs84::Latitude>::encode_argument(out, in.latitude());
161  if (in.has_longitude())
162  Codec<com::ubuntu::location::wgs84::Longitude>::encode_argument(out, in.longitude());
163  if (in.has_altitude())
164  Codec<com::ubuntu::location::wgs84::Altitude>::encode_argument(out, in.altitude());
165  }
166 
167  static void decode_argument(core::dbus::Message::Reader& out, com::ubuntu::location::Position& in)
168  {
169  com::ubuntu::location::wgs84::Latitude lat;
170  com::ubuntu::location::wgs84::Longitude lon;
171  com::ubuntu::location::wgs84::Altitude alt;
172  uint64_t flags_on_wire;
173  Codec<uint64_t>::decode_argument(out, flags_on_wire);
174 
175  com::ubuntu::location::Position::Flags flags{flags_on_wire};
176  if (flags.test(com::ubuntu::location::Position::latitude_flag))
177  {
178  Codec<com::ubuntu::location::wgs84::Latitude>::decode_argument(out, lat);
179  in.latitude(lat);
180  }
181  if (flags.test(com::ubuntu::location::Position::latitude_flag))
182  {
183  Codec<com::ubuntu::location::wgs84::Longitude>::decode_argument(out, lon);
184  in.longitude(lon);
185  }
186  if (flags.test(com::ubuntu::location::Position::altitude_flag))
187  {
188  Codec<com::ubuntu::location::wgs84::Altitude>::decode_argument(out, alt);
189  in.altitude(alt);
190  }
191  }
192 };
193 
194 namespace helper
195 {
196 template<>
197 struct TypeMapper<com::ubuntu::location::Velocity>
198 {
199  constexpr static ArgumentType type_value()
200  {
201  return ArgumentType::structure;
202  }
203  constexpr static bool is_basic_type()
204  {
205  return false;
206  }
207  constexpr static bool requires_signature()
208  {
209  return true;
210  }
211 
212  static std::string signature()
213  {
214  static const std::string s =
215  DBUS_STRUCT_BEGIN_CHAR_AS_STRING +
216  TypeMapper<typename com::ubuntu::location::Velocity::Quantity>::signature() +
217  DBUS_STRUCT_END_CHAR_AS_STRING;
218  return s;
219  }
220 };
221 }
222 
223 template<>
224 struct Codec<com::ubuntu::location::Velocity>
225 {
226  static void encode_argument(core::dbus::Message::Writer& out, const com::ubuntu::location::Velocity& in)
227  {
228  Codec<typename com::ubuntu::location::Velocity::Quantity>::encode_argument(out, in.value);
229  }
230 
231  static void decode_argument(core::dbus::Message::Reader& out, com::ubuntu::location::Velocity& in)
232  {
233  Codec<typename com::ubuntu::location::Velocity::Quantity>::decode_argument(out, in.value);
234  }
235 };
236 
237 namespace helper
238 {
239 template<>
240 struct TypeMapper<com::ubuntu::location::Heading>
241 {
242  constexpr static ArgumentType type_value()
243  {
244  return ArgumentType::structure;
245  }
246  constexpr static bool is_basic_type()
247  {
248  return false;
249  }
250  constexpr static bool requires_signature()
251  {
252  return true;
253  }
254 
255  static std::string signature()
256  {
257  static const std::string s =
258  DBUS_STRUCT_BEGIN_CHAR_AS_STRING +
259  TypeMapper<typename com::ubuntu::location::Heading::Quantity>::signature() +
260  DBUS_STRUCT_END_CHAR_AS_STRING;
261  return s;
262  }
263 };
264 }
265 
266 template<>
267 struct Codec<com::ubuntu::location::Heading>
268 {
269  static void encode_argument(core::dbus::Message::Writer& out, const com::ubuntu::location::Heading& in)
270  {
271  Codec<typename com::ubuntu::location::Heading::Quantity>::encode_argument(out, in.value);
272  }
273 
274  static void decode_argument(core::dbus::Message::Reader& out, com::ubuntu::location::Heading& in)
275  {
276  Codec<typename com::ubuntu::location::Heading::Quantity>::decode_argument(out, in.value);
277  }
278 };
279 
280 namespace helper
281 {
282 template<typename T>
283 struct TypeMapper<com::ubuntu::location::Accuracy<T>>
284 {
285  constexpr static ArgumentType type_value()
286  {
287  return ArgumentType::structure;
288  }
289  constexpr static bool is_basic_type()
290  {
291  return false;
292  }
293  constexpr static bool requires_signature()
294  {
295  return true;
296  }
297 
298  static std::string signature()
299  {
300  static const std::string s =
301  DBUS_STRUCT_BEGIN_CHAR_AS_STRING +
302  TypeMapper<T>::signature() +
303  DBUS_STRUCT_END_CHAR_AS_STRING;
304  return s;
305  }
306 };
307 }
308 
309 template<typename T>
310 struct Codec<com::ubuntu::location::Accuracy<T>>
311 {
312  static void encode_argument(core::dbus::Message::Writer& out, const com::ubuntu::location::Accuracy<T>& in)
313  {
314  Codec<T>::encode_argument(out, in.value);
315  }
316 
317  static void decode_argument(core::dbus::Message::Reader& out, com::ubuntu::location::Accuracy<T>& in)
318  {
319  Codec<T>::decode_argument(out, in.value);
320  }
321 };
322 
323 namespace helper
324 {
325 template<>
326 struct TypeMapper<com::ubuntu::location::Criteria>
327 {
328  constexpr static ArgumentType type_value()
329  {
330  return ArgumentType::structure;
331  }
332  constexpr static bool is_basic_type()
333  {
334  return false;
335  }
336  constexpr static bool requires_signature()
337  {
338  return true;
339  }
340 
341  static std::string signature()
342  {
343  static const std::string s =
344  DBUS_STRUCT_BEGIN_CHAR_AS_STRING +
345  helper::TypeMapper<com::ubuntu::location::Accuracy<com::ubuntu::location::wgs84::Latitude>>::signature() +
346  helper::TypeMapper<com::ubuntu::location::Accuracy<com::ubuntu::location::wgs84::Longitude>>::signature() +
347  helper::TypeMapper<com::ubuntu::location::Accuracy<com::ubuntu::location::wgs84::Altitude>>::signature() +
348  helper::TypeMapper<com::ubuntu::location::Accuracy<com::ubuntu::location::Velocity>>::signature() +
349  helper::TypeMapper<com::ubuntu::location::Accuracy<com::ubuntu::location::Heading>>::signature() +
350  DBUS_STRUCT_END_CHAR_AS_STRING;
351  return s;
352  }
353 };
354 }
355 
356 template<>
357 struct Codec<com::ubuntu::location::Criteria>
358 {
359  static void encode_argument(core::dbus::Message::Writer& out, const com::ubuntu::location::Criteria& in)
360  {
361  Codec<com::ubuntu::location::Accuracy<com::ubuntu::location::wgs84::Latitude>>::encode_argument(out, in.latitude_accuracy);
362  Codec<com::ubuntu::location::Accuracy<com::ubuntu::location::wgs84::Longitude>>::encode_argument(out, in.longitude_accuracy);
363  Codec<com::ubuntu::location::Accuracy<com::ubuntu::location::wgs84::Altitude>>::encode_argument(out, in.altitude_accuracy);
364  Codec<com::ubuntu::location::Accuracy<com::ubuntu::location::Velocity>>::encode_argument(out, in.velocity_accuracy);
365  Codec<com::ubuntu::location::Accuracy<com::ubuntu::location::Heading>>::encode_argument(out, in.heading_accuracy);
366  }
367 
368  static void decode_argument(core::dbus::Message::Reader& out, com::ubuntu::location::Criteria& in)
369  {
370  Codec<com::ubuntu::location::Accuracy<com::ubuntu::location::wgs84::Latitude>>::decode_argument(out, in.latitude_accuracy);
371  Codec<com::ubuntu::location::Accuracy<com::ubuntu::location::wgs84::Longitude>>::decode_argument(out, in.longitude_accuracy);
372  Codec<com::ubuntu::location::Accuracy<com::ubuntu::location::wgs84::Altitude>>::decode_argument(out, in.altitude_accuracy);
373  Codec<com::ubuntu::location::Accuracy<com::ubuntu::location::Velocity>>::decode_argument(out, in.velocity_accuracy);
374  Codec<com::ubuntu::location::Accuracy<com::ubuntu::location::Heading>>::decode_argument(out, in.heading_accuracy);
375  }
376 };
377 namespace helper
378 {
379 template<typename T>
380 struct TypeMapper<com::ubuntu::location::Update<T>>
381 {
382  constexpr static ArgumentType type_value()
383  {
384  return ArgumentType::structure;
385  }
386  constexpr static bool is_basic_type()
387  {
388  return false;
389  }
390  constexpr static bool requires_signature()
391  {
392  return true;
393  }
394 
395  static std::string signature()
396  {
397  static const std::string s =
398  helper::TypeMapper<T>::signature() +
399  helper::TypeMapper<uint64_t>::signature();
400  return s;
401  }
402 };
403 }
404 
405 template<typename T>
406 struct Codec<com::ubuntu::location::Update<T>>
407 {
408  static void encode_argument(core::dbus::Message::Writer& out, const com::ubuntu::location::Update<T>& in)
409  {
410  Codec<T>::encode_argument(out, in.value);
411  Codec<int64_t>::encode_argument(out, in.when.time_since_epoch().count());
412  }
413 
414  static void decode_argument(core::dbus::Message::Reader& out, com::ubuntu::location::Update<T>& in)
415  {
416  Codec<T>::decode_argument(out, in.value);
417  int64_t value;
418  Codec<int64_t>::decode_argument(out, value);
419  in.when = com::ubuntu::location::Clock::Timestamp(com::ubuntu::location::Clock::Duration(value));
420  }
421 };
422 }
423 }
424 
425 #endif // LOCATION_SERVICE_COM_UBUNTU_LOCATION_CODEC_H_