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