~sergiusens/libhybris/autotests

« back to all changes in this revision

Viewing changes to hybris/tests/test_gps.c

  • Committer: Package Import Robot
  • Author(s): Ricardo Salveti de Araujo
  • Date: 2013-06-04 07:33:11 UTC
  • Revision ID: package-import@ubuntu.com-20130604073311-20ldi2hm1axkvjl1
Tags: upstream-0.1.0+git20130601+dfb2e26
ImportĀ upstreamĀ versionĀ 0.1.0+git20130601+dfb2e26

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2013 Jolla Mobile oy
 
3
 * Author: Philippe De Swert <philippe.deswert@jollamobile.com>
 
4
 *
 
5
 * Licensed under the Apache License, Version 2.0 (the "License");
 
6
 * you may not use this file except in compliance with the License.
 
7
 * You may obtain a copy of the License at
 
8
 *
 
9
 * http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
14
 * See the License for the specific language governing permissions and
 
15
 * limitations under the License.
 
16
 *
 
17
*/
 
18
 
 
19
#include <pthread.h>
 
20
#include <signal.h>
 
21
#include <stdio.h>
 
22
#include <unistd.h>
 
23
#include <stdlib.h>
 
24
#include <sys/time.h>
 
25
#include <getopt.h>
 
26
 
 
27
#include <android/hardware/gps.h>
 
28
 
 
29
const GpsInterface* Gps = NULL;
 
30
const AGpsInterface* AGps = NULL;
 
31
const AGpsRilInterface* AGpsRil = NULL;
 
32
 
 
33
static const GpsInterface* get_gps_interface()
 
34
{
 
35
  int error;
 
36
  hw_module_t* module;
 
37
  const GpsInterface* interface = NULL;
 
38
  struct gps_device_t *device;
 
39
 
 
40
  error = hw_get_module(GPS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
 
41
 
 
42
  if (!error)
 
43
  {
 
44
     error = module->methods->open(module, GPS_HARDWARE_MODULE_ID, (struct hw_device_t **) &device);
 
45
     fprintf(stdout, "*** device info\n id = %s\n name = %s\n author = %s\n",
 
46
     module->id, module->name, module->author);
 
47
 
 
48
     if (!error)
 
49
     {
 
50
       interface = device->get_gps_interface(device);
 
51
     }
 
52
  }
 
53
  else
 
54
  {
 
55
        fprintf(stdout, "*** GPS interface not found :\(\n Bye! \n");
 
56
        exit(1);
 
57
  }
 
58
 
 
59
  return interface;
 
60
}
 
61
 
 
62
static const AGpsInterface* get_agps_interface(const GpsInterface *gps)
 
63
{
 
64
  const AGpsInterface* interface = NULL;
 
65
 
 
66
  if (gps)
 
67
  {
 
68
    interface = (const AGpsInterface*)gps->get_extension(AGPS_INTERFACE);
 
69
  }
 
70
  return interface;
 
71
}
 
72
 
 
73
static const AGpsRilInterface* get_agps_ril_interface(const GpsInterface *gps)
 
74
{
 
75
  const AGpsRilInterface* interface = NULL;
 
76
 
 
77
  if (gps)
 
78
  {
 
79
    interface = (const AGpsRilInterface*)gps->get_extension(AGPS_RIL_INTERFACE);
 
80
  }
 
81
  return interface;
 
82
}
 
83
 
 
84
static void location_callback(GpsLocation* location)
 
85
{
 
86
  fprintf(stdout, "*** location callback\n");
 
87
  fprintf(stdout, "flags:\t%d\n", location->flags);
 
88
  fprintf(stdout, "latitude: \t%lf\n", location->latitude);
 
89
  fprintf(stdout, "longtide: \t%lf\n", location->longitude);
 
90
  fprintf(stdout, "accuracy:\t%f\n", location->accuracy);
 
91
  fprintf(stdout, "utc: \t%ld\n", (long)location->timestamp);
 
92
}
 
93
 
 
94
static void status_callback(GpsStatus* status)
 
95
{
 
96
  fprintf(stdout, "*** status callback\n");
 
97
 
 
98
  switch (status->status)
 
99
  {
 
100
    case GPS_STATUS_NONE:
 
101
        fprintf(stdout, "*** no gps\n");
 
102
        break;
 
103
    case GPS_STATUS_SESSION_BEGIN:
 
104
        fprintf(stdout, "*** session begin\n");
 
105
        break;
 
106
    case GPS_STATUS_SESSION_END:
 
107
        fprintf(stdout, "*** session end\n");
 
108
        break;
 
109
    case GPS_STATUS_ENGINE_ON:
 
110
        fprintf(stdout, "*** engine on\n");
 
111
        break;
 
112
    case GPS_STATUS_ENGINE_OFF:
 
113
        fprintf(stdout, "*** engine off\n");
 
114
        break;
 
115
    default:
 
116
        fprintf(stdout, "*** unknown status\n");
 
117
  }
 
118
}
 
119
 
 
120
static void sv_status_callback(GpsSvStatus* sv_info)
 
121
{
 
122
/* Too much useless info
 
123
  fprintf(stdout, "*** sv status\n");
 
124
  fprintf(stdout, "sv_size:\t%d\n", sv_info->size);
 
125
*/
 
126
}
 
127
 
 
128
static void nmea_callback(GpsUtcTime timestamp, const char* nmea, int length)
 
129
{
 
130
  fprintf(stdout, "*** nmea info\n");
 
131
  fprintf(stdout, "timestamp:\t%ld\n", (long)timestamp);
 
132
  fprintf(stdout, "nmea: \t%s\n", nmea);
 
133
  fprintf(stdout, "length: \t%d\n", length);
 
134
}
 
135
 
 
136
static void set_capabilities_callback(uint32_t capabilities)
 
137
{
 
138
  fprintf(stdout, "*** set capabilities\n");
 
139
  /* do nothing */
 
140
}
 
141
 
 
142
static void acquire_wakelock_callback()
 
143
{
 
144
  fprintf(stdout, "*** acquire wakelock\n");
 
145
  /* do nothing */
 
146
}
 
147
 
 
148
static void release_wakelock_callback()
 
149
{
 
150
  fprintf(stdout, "*** release wakelock\n");
 
151
  /* do nothing */
 
152
}
 
153
 
 
154
static pthread_t create_thread_callback(const char* name, void (*start)(void *), void* arg)
 
155
{
 
156
  pthread_t thread_id;
 
157
  pthread_attr_t attr;
 
158
  int error = 0;
 
159
 
 
160
  error = pthread_attr_init(&attr);
 
161
  error = pthread_create(&thread_id, &attr, (void*(*)(void*))start, arg);
 
162
 
 
163
  if(error != 0)
 
164
        return 0;
 
165
 
 
166
  return thread_id;
 
167
}
 
168
 
 
169
 
 
170
static void agps_status_cb(AGpsStatus *status)
 
171
{
 
172
  switch (status->status)
 
173
  {
 
174
    case GPS_REQUEST_AGPS_DATA_CONN:
 
175
        fprintf(stdout, "*** data_conn_open\n");
 
176
        AGps->data_conn_open("internet");
 
177
        break;
 
178
    case GPS_RELEASE_AGPS_DATA_CONN:
 
179
        fprintf(stdout, "*** data_conn_closed\n");
 
180
        AGps->data_conn_closed();
 
181
        break;
 
182
  }
 
183
}
 
184
 
 
185
static void agps_ril_set_id_cb(uint32_t flags)
 
186
{
 
187
  fprintf(stdout, "*** set_id_cb\n");
 
188
  AGpsRil->set_set_id(AGPS_SETID_TYPE_IMSI, "000000000000000");
 
189
}
 
190
 
 
191
static void agps_ril_refloc_cb(uint32_t flags)
 
192
{
 
193
  fprintf(stdout, "*** refloc_cb\n");
 
194
  /* TODO : find out how to fill in location
 
195
  AGpsRefLocation location;
 
196
  AGpsRil->set_ref_location(&location, sizeof(location));
 
197
  */
 
198
}
 
199
 
 
200
GpsCallbacks callbacks = {
 
201
  sizeof(GpsCallbacks),
 
202
  location_callback,
 
203
  status_callback,
 
204
  sv_status_callback,
 
205
  nmea_callback,
 
206
  set_capabilities_callback,
 
207
  acquire_wakelock_callback,
 
208
  release_wakelock_callback,
 
209
  create_thread_callback,
 
210
};
 
211
 
 
212
AGpsCallbacks callbacks2 = {
 
213
  agps_status_cb,
 
214
  create_thread_callback,
 
215
};
 
216
 
 
217
AGpsRilCallbacks callbacks3 = {
 
218
  agps_ril_set_id_cb,
 
219
  agps_ril_refloc_cb,
 
220
  create_thread_callback,
 
221
};
 
222
 
 
223
void sigint_handler(int signum)
 
224
{
 
225
  fprintf(stdout, "*** cleanup\n");
 
226
  if (Gps)
 
227
  {
 
228
        Gps->stop();
 
229
        Gps->cleanup();
 
230
  }
 
231
  exit (0);
 
232
}
 
233
 
 
234
int main(int argc, char *argv[])
 
235
{
 
236
  int sleeptime = 6000, opt, initok = 0;
 
237
  struct timeval tv;
 
238
  int agps = 0, agpsril = 0, injecttime = 0;
 
239
 
 
240
  while ((opt = getopt(argc, argv, "art")) != -1)
 
241
  {
 
242
               switch (opt) {
 
243
               case 'a':
 
244
                   agps = 1;
 
245
                   fprintf(stdout, "*** Using agps\n");
 
246
                   break;
 
247
               case 'r':
 
248
                   agpsril = 1;
 
249
                   fprintf(stdout, "*** Using agpsril\n");
 
250
                   break;
 
251
               case 't':
 
252
                   injecttime = 1;
 
253
                   fprintf(stdout, "*** Timing info will be injected\n");
 
254
                   break;
 
255
               default:
 
256
                   fprintf(stderr, "\n Usage: %s \n \
 
257
                           \t-a for agps,\n \
 
258
                           \t-r for agpsril,\n \
 
259
                           \t-t to inject time,\n \
 
260
                           \tnone for standalone gps\n",
 
261
                           argv[0]);
 
262
                   exit(1);
 
263
               }
 
264
  }
 
265
 
 
266
 
 
267
  fprintf(stdout, "*** setup signal handler\n");
 
268
  signal(SIGINT, sigint_handler);
 
269
 
 
270
  fprintf(stdout, "*** get gps interface\n");
 
271
  Gps = get_gps_interface();
 
272
 
 
273
  fprintf(stdout, "*** init gps interface\n");
 
274
  initok = Gps->init(&callbacks);
 
275
  fprintf(stdout, "*** setting positioning mode\n");
 
276
  /* need to be done before starting gps or no info will come out */
 
277
  if((agps||agpsril) && !initok)
 
278
        Gps->set_position_mode(GPS_POSITION_MODE_MS_BASED, GPS_POSITION_RECURRENCE_PERIODIC, 100, 0, 0);
 
279
  else
 
280
        Gps->set_position_mode(GPS_POSITION_MODE_STANDALONE, GPS_POSITION_RECURRENCE_PERIODIC, 100, 0, 0);
 
281
 
 
282
  if (Gps && !initok && (agps||agpsril))
 
283
  {
 
284
    fprintf(stdout, "*** get agps interface\n");
 
285
    AGps = get_agps_interface(Gps);
 
286
    if (AGps)
 
287
    {
 
288
        fprintf(stdout, "*** set up agps interface\n");
 
289
        AGps->init(&callbacks2);
 
290
        fprintf(stdout, "*** set up agps server\n");
 
291
        AGps->set_server(AGPS_TYPE_SUPL, "supl.google.com", 7276);
 
292
    }
 
293
 
 
294
    fprintf(stdout, "*** get agps ril interface\n");
 
295
 
 
296
    AGpsRil = get_agps_ril_interface(Gps);
 
297
    if (AGpsRil)
 
298
    {
 
299
        AGpsRil->init(&callbacks3);
 
300
    }
 
301
 
 
302
    fprintf(stdout, "*** delete aiding data\n");
 
303
    Gps->delete_aiding_data(GPS_DELETE_ALL);
 
304
  }
 
305
 
 
306
  if(injecttime)
 
307
  {
 
308
    fprintf(stdout, "*** aiding gps by injecting time information\n");
 
309
    gettimeofday(&tv, NULL);
 
310
    Gps->inject_time(tv.tv_sec, tv.tv_sec, 0);
 
311
  }
 
312
 
 
313
  fprintf(stdout, "*** start gps track\n");
 
314
  Gps->start();
 
315
 
 
316
  fprintf(stdout, "*** gps tracking started\n");
 
317
 
 
318
  while(sleeptime > 0)
 
319
  {
 
320
    fprintf(stdout, "*** tracking.... \n");
 
321
    sleep(100);
 
322
    sleeptime = sleeptime - 100;
 
323
  }
 
324
 
 
325
  fprintf(stdout, "*** stop tracking\n");
 
326
  Gps->stop();
 
327
  fprintf(stdout, "*** cleaning up\n");
 
328
  Gps->cleanup();
 
329
 
 
330
  return 0;
 
331
}