summaryrefslogtreecommitdiffstats
path: root/tests/gpstest/gpstest.cpp
blob: 27cbd17a2846f3218eb1d1e81bfc7fc5b7305032 (plain)
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
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * 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 <stdlib.h>
#include <stdio.h>
#include <unistd.h>

extern "C" size_t  dlmalloc_footprint();

#include "hardware_legacy/gps.h"

static const GpsInterface* sGpsInterface = NULL;

static bool sDone = false;
static int sFixes = 0;
static int sMaxFixes = 0;
static int sStatus = GPS_STATUS_ENGINE_OFF;

static void location_callback(GpsLocation* location)
{
    printf("Got Fix: latitude: %lf longitude: %lf altitude: %.1lf\n", 
            location->latitude, location->longitude, location->altitude);
    sFixes++;
    if (sMaxFixes > 0 && sFixes >= sMaxFixes) {
        sDone = true;
    }
}

static void status_callback(GpsStatus* status)
{
    switch (status->status) {
        case GPS_STATUS_NONE:
            printf("status: GPS_STATUS_NONE\n");
            break;
        case GPS_STATUS_SESSION_BEGIN:
            printf("status: GPS_STATUS_SESSION_BEGIN\n");
            break;
        case GPS_STATUS_SESSION_END:
            printf("status: GPS_STATUS_SESSION_END\n");
            break;
        case GPS_STATUS_ENGINE_ON:
            printf("status: GPS_STATUS_ENGINE_ON\n");
            break;
        case GPS_STATUS_ENGINE_OFF:
            printf("status: GPS_STATUS_ENGINE_OFF\n");
            break;
        default:
            printf("unknown status: %d\n", status->status);
            break;
    }

    sStatus = status->status;
}

static void sv_status_callback(GpsSvStatus* sv_status)
{
    if (sv_status->num_svs > 0) {
        for (int i = 0; i < sv_status->num_svs; i++) {
            printf("SV: %2d SNR: %.1f Elev: %.1f Azim: %.1f %s %s\n", sv_status->sv_list[i].prn, 
                    sv_status->sv_list[i].snr, sv_status->sv_list[i].elevation, 
                    sv_status->sv_list[i].azimuth,
             ((sv_status->ephemeris_mask & (1 << (sv_status->sv_list[i].prn - 1))) ? "E" : " "), 
             ((sv_status->almanac_mask & (1 << (sv_status->sv_list[i].prn - 1))) ? "A" : " ")
            );
        }
        printf("\n");
    }
}

GpsCallbacks sCallbacks = {
    location_callback,
    status_callback,
    sv_status_callback,
};

int main(int argc, char *argv[])
{
    size_t   initial = dlmalloc_footprint();
    
    if (argc >= 2) {
        sMaxFixes = atoi(argv[1]);
        printf("max fixes: %d\n", sMaxFixes);
    }

    sGpsInterface = gps_get_interface();
    if (!sGpsInterface) {
        fprintf(stderr, "could not get gps interface\n");
        return -1;
    }

    int err = sGpsInterface->init(&sCallbacks);
    if (err) {
        fprintf(stderr, "gps_init failed %d\n", err);
        return err;
    }

    sGpsInterface->start();
    
    while (!sDone) {
        sleep(1);
    }
    
    sGpsInterface->stop();
    
    printf("waiting for GPS to shut down\n");
     while (sStatus != GPS_STATUS_ENGINE_OFF) {
        sleep(1);
    }
   
    sGpsInterface->cleanup();

    size_t   final = dlmalloc_footprint();
    fprintf(stderr, "KO: initial == %d, final == %d\n", initial, final );

    return 0;
}