-
Notifications
You must be signed in to change notification settings - Fork 21
/
gps.c
286 lines (242 loc) · 9.88 KB
/
gps.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/******************************************************************************
* GPS HAL shim
* load older GPS libraries on gingerbread (and above?)
*
* Copyright 2010 - Ricardo Cerqueira
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
#include <hardware/gps.h>
//#define LOG_NDEBUG 0
#include <stdlib.h>
#define LOG_TAG "gps-shim"
#include <utils/Log.h>
#include <gpsshim.h>
GpsCallbacks *originalCallbacks;
static const OldGpsXtraInterface* oldXTRA = NULL;
static GpsXtraInterface newXTRA;
static const OldAGpsInterface* oldAGPS = NULL;
static AGpsInterface newAGPS;
static const OldAGpsRilInterface* oldAGPSRIL = NULL;
static AGpsRilInterface newAGPSRIL;
static const OldGpsNiInterface* oldNI = NULL;
static GpsNiInterface newNI;
static const OldGpsInterface* originalGpsInterface = NULL;
static GpsInterface newGpsInterface;
extern const OldGpsInterface* gps_get_hardware_interface();
static void location_callback_wrapper(OldGpsLocation *location) {
static GpsLocation newLocation;
LOGV("I have a location");
newLocation.size = sizeof(GpsLocation);
newLocation.flags = location->flags;
newLocation.latitude = location->latitude;
newLocation.longitude = location->longitude;
newLocation.altitude = location->altitude;
newLocation.speed = location->speed;
newLocation.bearing = location->bearing;
newLocation.accuracy = location->accuracy;
newLocation.timestamp = location->timestamp;
originalCallbacks->create_thread_cb("gpsshim-location",(void *)originalCallbacks->location_cb,(void *)&newLocation);
}
static void status_callback_wrapper(OldGpsStatus *status) {
static GpsStatus newStatus;
newStatus.size = sizeof(GpsStatus);
LOGV("Status value is %u",status->status);
newStatus.status = status->status;
originalCallbacks->create_thread_cb("gpsshim-status",(void *)originalCallbacks->status_cb,(void *)&newStatus);
}
static void svstatus_callback_wrapper(OldGpsSvStatus *sv_info) {
static GpsSvStatus newSvStatus;
int i=0;
LOGV("I have a svstatus");
newSvStatus.size = sizeof(GpsSvStatus);
newSvStatus.num_svs = sv_info->num_svs;
for (i=0; i<newSvStatus.num_svs; i++) {
newSvStatus.sv_list[i].size = sizeof(GpsSvInfo);
newSvStatus.sv_list[i].prn = sv_info->sv_list[i].prn;
newSvStatus.sv_list[i].snr = sv_info->sv_list[i].snr;
newSvStatus.sv_list[i].elevation = sv_info->sv_list[i].elevation;
newSvStatus.sv_list[i].azimuth = sv_info->sv_list[i].azimuth;
}
newSvStatus.ephemeris_mask = sv_info->ephemeris_mask;
newSvStatus.almanac_mask = sv_info->almanac_mask;
newSvStatus.used_in_fix_mask = sv_info->used_in_fix_mask;
originalCallbacks->create_thread_cb("gpsshim-svstatus",(void *)originalCallbacks->sv_status_cb,(void *)&newSvStatus);
}
GpsUtcTime nmeasave_timestamp; const char* nmeasave_nmea; int nmeasave_length;
static void nmea_callback(void *unused) {
LOGV("Invoking nmea callback");
originalCallbacks->nmea_cb(nmeasave_timestamp, nmeasave_nmea, nmeasave_length);
}
static void nmea_callback_wrapper(GpsUtcTime timestamp, const char* nmea, int length) {
nmeasave_timestamp = timestamp; nmeasave_nmea = nmea; nmeasave_length=length;
originalCallbacks->create_thread_cb("gpsshim-nmea",(void *)nmea_callback,NULL);
}
static OldAGpsCallbacks oldAGpsCallbacks;
static const AGpsCallbacks* newAGpsCallbacks = NULL;
static void agps_status_cb(OldAGpsStatus* status)
{
AGpsStatus newAGpsStatus;
newAGpsStatus.size = sizeof(AGpsStatus);
newAGpsStatus.type = status->type;
newAGpsStatus.status = status->status;
newAGpsCallbacks->create_thread_cb("gpsshim-agpsstatus",(void *)newAGpsCallbacks->status_cb,(void*)&newAGpsStatus);
}
static void agps_init_wrapper(AGpsCallbacks * callbacks)
{
newAGpsCallbacks = callbacks;
oldAGpsCallbacks.status_cb = agps_status_cb;
oldAGPS->init(&oldAGpsCallbacks);
}
static OldAGpsRilCallbacks oldAGpsRilCallbacks;
static const AGpsRilCallbacks* newAGpsRilCallbacks = NULL;
static void agpsril_setid_cb(uint32_t flags)
{
LOGV("AGPSRIL setid callback");
newAGpsRilCallbacks->create_thread_cb("gpsshim-agpsril-setid",(void *)newAGpsRilCallbacks->request_setid,&flags);
}
static void agpsril_refloc_cb(uint32_t flags)
{
LOGV("AGPSRIL refloc callback");
newAGpsRilCallbacks->create_thread_cb("gpsshim-agpsril-refloc",(void *)newAGpsRilCallbacks->request_refloc,&flags);
}
static void agpsril_init_wrapper(AGpsRilCallbacks * callbacks)
{
newAGpsRilCallbacks = callbacks;
oldAGpsRilCallbacks.request_setid = agpsril_setid_cb;
oldAGpsRilCallbacks.request_refloc = agpsril_refloc_cb;
LOGV("AGPSRIL init");
oldAGPSRIL->init(&oldAGpsRilCallbacks);
}
static OldGpsXtraCallbacks oldXtraCallbacks;
static const GpsXtraCallbacks* newXtraCallbacks = NULL;
static void xtra_download_cb()
{
newXtraCallbacks->create_thread_cb("gpsshim-xtradownload",(void *)newXtraCallbacks->download_request_cb,NULL);
}
static int xtra_init_wrapper(GpsXtraCallbacks * callbacks)
{
newXtraCallbacks = callbacks;
oldXtraCallbacks.download_request_cb = xtra_download_cb;
#ifdef NEEDS_INITIAL_XTRA
xtra_download_cb();
#endif
return oldXTRA->init(&oldXtraCallbacks);
}
static const void* wrapper_get_extension(const char* name)
{
if (!strcmp(name, GPS_XTRA_INTERFACE) && (oldXTRA = originalGpsInterface->get_extension(name)))
{
newXTRA.size = sizeof(GpsXtraInterface);
newXTRA.init = xtra_init_wrapper;
newXTRA.inject_xtra_data = oldXTRA->inject_xtra_data;
return &newXTRA;
}
else if (!strcmp(name, AGPS_INTERFACE) && (oldAGPS = originalGpsInterface->get_extension(name)))
{
newAGPS.size = sizeof(AGpsInterface);
newAGPS.init = agps_init_wrapper;
newAGPS.data_conn_open = oldAGPS->data_conn_open;
newAGPS.data_conn_closed = oldAGPS->data_conn_closed;
newAGPS.data_conn_failed = oldAGPS->data_conn_failed;
newAGPS.set_server = oldAGPS->set_server;
return &newAGPS;
}
else if (!strcmp(name, AGPS_RIL_INTERFACE) && (oldAGPSRIL = originalGpsInterface->get_extension(name)))
{
newAGPSRIL.size = sizeof(AGpsRilInterface);
newAGPSRIL.init = agpsril_init_wrapper;
newAGPSRIL.set_ref_location = oldAGPSRIL->set_ref_location;
newAGPSRIL.set_set_id = oldAGPSRIL->set_set_id;
newAGPSRIL.ni_message = oldAGPSRIL->ni_message;
return &newAGPSRIL;
}
/*else if (strcmp(name, GPS_NI_INTERFACE) == 0)
{
oldNI = originalGpsInterface->get_extension(name);
newNI.size = sizeof(GpsNiInterface);
newNI.init = oldNI->init;
newNI.respond = oldNI->respond;
return &newNI;
}*/
return NULL;
}
static int init_wrapper(GpsCallbacks* callbacks) {
LOGV("init_wrapper was called");
static OldGpsCallbacks oldCallbacks;
originalCallbacks = callbacks;
oldCallbacks.location_cb = location_callback_wrapper;
oldCallbacks.status_cb = status_callback_wrapper;
oldCallbacks.sv_status_cb = svstatus_callback_wrapper;
oldCallbacks.nmea_cb = nmea_callback_wrapper;
#ifdef NO_AGPS
originalCallbacks->set_capabilities_cb(0);
#else
originalCallbacks->set_capabilities_cb(GPS_CAPABILITY_MSB|GPS_CAPABILITY_MSA);
#endif
return originalGpsInterface->init(&oldCallbacks);
}
static int set_position_mode_wrapper(GpsPositionMode mode, GpsPositionRecurrence recurrence, uint32_t min_interval, uint32_t preferred_accuracy, uint32_t preferred_time) {
return originalGpsInterface->set_position_mode(mode, recurrence ? 0 : (min_interval/1000));
}
static int stop_wrapper() {
int ret = originalGpsInterface->stop();
originalCallbacks->release_wakelock_cb();
return ret;
}
static int start_wrapper() {
originalCallbacks->acquire_wakelock_cb();
return originalGpsInterface->start();
}
/* HAL Methods */
const GpsInterface* gps__get_gps_interface(struct gps_device_t* dev)
{
LOGV("get_interface was called");
originalGpsInterface = gps_get_hardware_interface();
newGpsInterface.size = sizeof(GpsInterface);
newGpsInterface.init = init_wrapper;
newGpsInterface.start = start_wrapper;
newGpsInterface.stop = stop_wrapper;
newGpsInterface.cleanup = originalGpsInterface->cleanup;
newGpsInterface.inject_time = originalGpsInterface->inject_time;
newGpsInterface.inject_location = originalGpsInterface->inject_location;
newGpsInterface.delete_aiding_data = originalGpsInterface->delete_aiding_data;
newGpsInterface.set_position_mode = set_position_mode_wrapper;
newGpsInterface.get_extension = wrapper_get_extension;
return &newGpsInterface;
}
static int open_gps(const struct hw_module_t* module, char const* name,
struct hw_device_t** device)
{
struct gps_device_t *dev = malloc(sizeof(struct gps_device_t));
memset(dev, 0, sizeof(*dev));
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (struct hw_module_t*)module;
dev->get_gps_interface = gps__get_gps_interface;
*device = (struct hw_device_t*)dev;
return 0;
}
static struct hw_module_methods_t gps_module_methods = {
.open = open_gps
};
const struct hw_module_t HAL_MODULE_INFO_SYM = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = GPS_HARDWARE_MODULE_ID,
.name = "GPS HAL Wrapper Module",
.author = "Ricardo Cerqueira",
.methods = &gps_module_methods,
};