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" |
17 | |
18 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
19 #include "base/time.h" | |
20 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
21 | 15 |
| 16 struct gps_data_t; |
22 struct Geoposition; | 17 struct Geoposition; |
23 class LibGpsLibraryWrapper; | |
24 | 18 |
25 class CONTENT_EXPORT LibGps { | 19 class CONTENT_EXPORT LibGps { |
26 public: | 20 public: |
27 virtual ~LibGps(); | 21 virtual ~LibGps(); |
28 // Attempts to dynamically load the libgps.so library, and creates and | 22 // 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. | 23 // failure. |
31 static LibGps* New(); | 24 static LibGps* New(); |
32 | 25 |
33 bool Start(); | 26 bool Start(); |
34 void Stop(); | 27 void Stop(); |
35 bool Poll(); | 28 bool Read(Geoposition* position); |
36 bool GetPosition(Geoposition* position); | |
37 | 29 |
38 protected: | 30 protected: |
39 // Takes ownership of |dl_wrapper|. | 31 typedef int (*gps_open_fn)(const char*, const char*, struct gps_data_t*); |
40 explicit LibGps(LibGpsLibraryWrapper* dl_wrapper); | 32 typedef int (*gps_close_fn)(struct gps_data_t*); |
| 33 typedef int (*gps_read_fn)(struct gps_data_t*); |
41 | 34 |
42 LibGpsLibraryWrapper& library() { | 35 explicit LibGps(void* dl_handle, |
43 return *library_; | 36 gps_open_fn gps_open, |
44 } | 37 gps_close_fn gps_close, |
45 // Called be start Start after successful |gps_open| to setup streaming. | 38 gps_read_fn gps_read); |
46 virtual bool StartStreaming() = 0; | 39 |
47 virtual bool DataWaiting() = 0; | |
48 // Returns false if there is not fix available. | 40 // Returns false if there is not fix available. |
49 virtual bool GetPositionIfFixed(Geoposition* position) = 0; | 41 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 | 42 |
94 private: | 43 private: |
95 void* dl_handle_; | 44 void* dl_handle_; |
96 gps_open_fn gps_open_; | 45 gps_open_fn gps_open_; |
97 gps_close_fn gps_close_; | 46 gps_close_fn gps_close_; |
98 gps_poll_fn gps_poll_; | 47 gps_read_fn gps_read_; |
99 gps_stream_fn gps_stream_; | |
100 gps_waiting_fn gps_waiting_; | |
101 | 48 |
102 gps_data_t* gps_data_; | 49 scoped_ptr<gps_data_t> gps_data_; |
| 50 bool is_open_; |
103 | 51 |
104 DISALLOW_COPY_AND_ASSIGN(LibGpsLibraryWrapper); | 52 DISALLOW_COPY_AND_ASSIGN(LibGps); |
105 }; | 53 }; |
106 | 54 |
107 #endif // CONTENT_BROWSER_GEOLOCATION_LIBGPS_WRAPPER_LINUX_H_ | 55 #endif // CONTENT_BROWSER_GEOLOCATION_LIBGPS_WRAPPER_LINUX_H_ |
OLD | NEW |