OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Defines a wrapper around the C libgps API (gps.h). Similar to the libgpsmm.h | 5 // Defines a wrapper around the C libgps API (gps.h). Similar to the libgpsmm.h |
6 // API provided by that package, but adds: | 6 // API provided by that package. |
7 // - shared object dynamic loading | |
8 // - support for (and abstraction from) different libgps.so versions | |
9 // - configurable for testing | |
10 // - more convenient error handling. | |
11 | 7 |
12 #ifndef CONTENT_BROWSER_GEOLOCATION_LIBGPS_WRAPPER_LINUX_H_ | 8 #ifndef CONTENT_BROWSER_GEOLOCATION_LIBGPS_WRAPPER_LINUX_H_ |
13 #define CONTENT_BROWSER_GEOLOCATION_LIBGPS_WRAPPER_LINUX_H_ | 9 #define CONTENT_BROWSER_GEOLOCATION_LIBGPS_WRAPPER_LINUX_H_ |
14 #pragma once | 10 #pragma once |
15 | 11 |
16 #include <string> | 12 #include "base/basictypes.h" |
13 #include "content/common/content_export.h" | |
14 #include "third_party/gpsd/release-3.1/gps.h" | |
joth
2011/11/11 18:40:40
could we keep this #include in the .cc ? even if w
Yufeng Shen (Slow to review)
2011/11/11 21:11:35
Done.
| |
17 | 15 |
18 #include "base/memory/scoped_ptr.h" | 16 COMPILE_ASSERT(GPSD_API_MAJOR_VERSION == 5, GPSD_API_version_is_not_5); |
joth
2011/11/11 18:40:40
...so this would have to go to the .cc too which s
Yufeng Shen (Slow to review)
2011/11/11 21:11:35
Done.
| |
19 #include "base/time.h" | 17 #define LIBGPS_VERSION "libgps.so.20" |
joth
2011/11/11 18:40:40
use const char[] in anon namespace in the cc
Yufeng Shen (Slow to review)
2011/11/11 21:11:35
Done.
| |
20 #include "content/common/content_export.h" | |
21 | 18 |
22 struct Geoposition; | 19 struct Geoposition; |
23 class LibGpsLibraryWrapper; | |
24 | 20 |
25 class CONTENT_EXPORT LibGps { | 21 class CONTENT_EXPORT LibGps { |
26 public: | 22 public: |
27 virtual ~LibGps(); | 23 virtual ~LibGps(); |
28 // Attempts to dynamically load the libgps.so library, and creates and | 24 // Attempts to dynamically load the libgps.so library and returns NULL on |
29 // appropriate LibGps instance for the version loaded. Returns NULL on | |
30 // failure. | 25 // failure. |
31 static LibGps* New(); | 26 static LibGps* New(); |
32 | 27 |
33 bool Start(); | 28 bool Start(); |
34 void Stop(); | 29 void Stop(); |
35 bool Poll(); | 30 bool Read(Geoposition* position); |
36 bool GetPosition(Geoposition* position); | |
37 | 31 |
38 protected: | 32 protected: |
39 // Takes ownership of |dl_wrapper|. | 33 typedef int (*gps_open_fn)(const char*, const char*, struct gps_data_t*); |
40 explicit LibGps(LibGpsLibraryWrapper* dl_wrapper); | 34 typedef int (*gps_close_fn)(struct gps_data_t*); |
35 typedef int (*gps_read_fn)(struct gps_data_t*); | |
41 | 36 |
42 LibGpsLibraryWrapper& library() { | 37 explicit LibGps(void* dl_handle, |
43 return *library_; | 38 gps_open_fn gps_open, |
44 } | 39 gps_close_fn gps_close, |
45 // Called be start Start after successful |gps_open| to setup streaming. | 40 gps_read_fn gps_read); |
46 virtual bool StartStreaming() = 0; | 41 |
47 virtual bool DataWaiting() = 0; | |
48 // Returns false if there is not fix available. | 42 // Returns false if there is not fix available. |
49 virtual bool GetPositionIfFixed(Geoposition* position) = 0; | 43 virtual bool GetPositionIfFixed(Geoposition* position); |
50 | |
51 private: | |
52 // Factory functions to create instances of LibGps using the corresponding | |
53 // libgps API versions (v2.38 => libgps.so.17, v2.94 => libgps.so.19). | |
54 // See LibGps::New() for the public API to this. | |
55 // Takes ownership of |dl_wrapper|. | |
56 static LibGps* NewV294(LibGpsLibraryWrapper* dl_wrapper); | |
57 | |
58 scoped_ptr<LibGpsLibraryWrapper> library_; | |
59 std::string last_error_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(LibGps); | |
62 }; | |
63 | |
64 struct gps_data_t; | |
65 | |
66 // Wraps the low-level shared object, binding C++ member functions onto the | |
67 // underlying C functions obtained from the library. | |
68 class CONTENT_EXPORT LibGpsLibraryWrapper { | |
69 public: | |
70 typedef gps_data_t* (*gps_open_fn)(const char*, const char*); | |
71 typedef int (*gps_close_fn)(gps_data_t*); | |
72 typedef int (*gps_poll_fn)(gps_data_t*); | |
73 // v2.90+ | |
74 typedef int (*gps_stream_fn)(gps_data_t*, unsigned int, void*); | |
75 typedef bool (*gps_waiting_fn)(gps_data_t*); | |
76 | |
77 LibGpsLibraryWrapper(void* dl_handle, | |
78 gps_open_fn gps_open, | |
79 gps_close_fn gps_close, | |
80 gps_poll_fn gps_poll, | |
81 gps_stream_fn gps_stream, | |
82 gps_waiting_fn gps_waiting); | |
83 ~LibGpsLibraryWrapper(); | |
84 | |
85 // Analogs of gps_xxx methods in gps.h | |
86 bool open(const char* host, const char* port); | |
87 void close(); | |
88 int poll(); | |
89 int stream(int flags); | |
90 bool waiting(); | |
91 const gps_data_t& data() const; | |
92 bool is_open() const; | |
93 | 44 |
94 private: | 45 private: |
95 void* dl_handle_; | 46 void* dl_handle_; |
96 gps_open_fn gps_open_; | 47 gps_open_fn gps_open_; |
97 gps_close_fn gps_close_; | 48 gps_close_fn gps_close_; |
98 gps_poll_fn gps_poll_; | 49 gps_read_fn gps_read_; |
99 gps_stream_fn gps_stream_; | |
100 gps_waiting_fn gps_waiting_; | |
101 | 50 |
102 gps_data_t* gps_data_; | 51 gps_data_t gps_data_; |
52 bool is_open_; | |
103 | 53 |
104 DISALLOW_COPY_AND_ASSIGN(LibGpsLibraryWrapper); | 54 DISALLOW_COPY_AND_ASSIGN(LibGps); |
105 }; | 55 }; |
106 | 56 |
107 #endif // CONTENT_BROWSER_GEOLOCATION_LIBGPS_WRAPPER_LINUX_H_ | 57 #endif // CONTENT_BROWSER_GEOLOCATION_LIBGPS_WRAPPER_LINUX_H_ |
OLD | NEW |