• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 
21 extern "C" size_t  dlmalloc_footprint();
22 
23 #include "hardware_legacy/gps.h"
24 
25 static const GpsInterface* sGpsInterface = NULL;
26 
27 static bool sDone = false;
28 static int sFixes = 0;
29 static int sMaxFixes = 0;
30 static int sStatus = GPS_STATUS_ENGINE_OFF;
31 
location_callback(GpsLocation * location)32 static void location_callback(GpsLocation* location)
33 {
34     printf("Got Fix: latitude: %lf longitude: %lf altitude: %.1lf\n",
35             location->latitude, location->longitude, location->altitude);
36     sFixes++;
37     if (sMaxFixes > 0 && sFixes >= sMaxFixes) {
38         sDone = true;
39     }
40 }
41 
status_callback(GpsStatus * status)42 static void status_callback(GpsStatus* status)
43 {
44     switch (status->status) {
45         case GPS_STATUS_NONE:
46             printf("status: GPS_STATUS_NONE\n");
47             break;
48         case GPS_STATUS_SESSION_BEGIN:
49             printf("status: GPS_STATUS_SESSION_BEGIN\n");
50             break;
51         case GPS_STATUS_SESSION_END:
52             printf("status: GPS_STATUS_SESSION_END\n");
53             break;
54         case GPS_STATUS_ENGINE_ON:
55             printf("status: GPS_STATUS_ENGINE_ON\n");
56             break;
57         case GPS_STATUS_ENGINE_OFF:
58             printf("status: GPS_STATUS_ENGINE_OFF\n");
59             break;
60         default:
61             printf("unknown status: %d\n", status->status);
62             break;
63     }
64 
65     sStatus = status->status;
66 }
67 
sv_status_callback(GpsSvStatus * sv_status)68 static void sv_status_callback(GpsSvStatus* sv_status)
69 {
70     if (sv_status->num_svs > 0) {
71         for (int i = 0; i < sv_status->num_svs; i++) {
72             printf("SV: %2d SNR: %.1f Elev: %.1f Azim: %.1f %s %s\n", sv_status->sv_list[i].prn,
73                     sv_status->sv_list[i].snr, sv_status->sv_list[i].elevation,
74                     sv_status->sv_list[i].azimuth,
75              ((sv_status->ephemeris_mask & (1 << (sv_status->sv_list[i].prn - 1))) ? "E" : " "),
76              ((sv_status->almanac_mask & (1 << (sv_status->sv_list[i].prn - 1))) ? "A" : " ")
77             );
78         }
79         printf("\n");
80     }
81 }
82 
83 GpsCallbacks sCallbacks = {
84     location_callback,
85     status_callback,
86     sv_status_callback,
87 };
88 
main(int argc,char * argv[])89 int main(int argc, char *argv[])
90 {
91     size_t   initial = dlmalloc_footprint();
92 
93     if (argc >= 2) {
94         sMaxFixes = atoi(argv[1]);
95         printf("max fixes: %d\n", sMaxFixes);
96     }
97 
98     sGpsInterface = gps_get_interface();
99     if (!sGpsInterface) {
100         fprintf(stderr, "could not get gps interface\n");
101         return -1;
102     }
103 
104     int err = sGpsInterface->init(&sCallbacks);
105     if (err) {
106         fprintf(stderr, "gps_init failed %d\n", err);
107         return err;
108     }
109 
110     sGpsInterface->start();
111 
112     while (!sDone) {
113         sleep(1);
114     }
115 
116     sGpsInterface->stop();
117 
118     printf("waiting for GPS to shut down\n");
119      while (sStatus != GPS_STATUS_ENGINE_OFF) {
120         sleep(1);
121     }
122 
123     sGpsInterface->cleanup();
124 
125     size_t   final = dlmalloc_footprint();
126     fprintf(stderr, "KO: initial == %d, final == %d\n", initial, final );
127 
128     return 0;
129 }
130