summaryrefslogtreecommitdiff
path: root/gnss-service/test/gnss-service-client.c
blob: c928380b6a3b3b6ce03c8b8520cdfdb944fdf929 (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
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
/**************************************************************************
* @licence app begin@
*
* SPDX-License-Identifier: MPL-2.0
*
* \ingroup GNSSService
* \author Marco Residori <marco.residori@xse.de>
*
* \copyright Copyright (C) 2013, XS Embedded GmbH
* 
* \license
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
* this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* @licence end@
**************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
#include <unistd.h>

#include "gnss-init.h"
#include "gnss.h"
#include "log.h"

DLT_DECLARE_CONTEXT(gCtx);

static void cbTime(const TGNSSTime time[], uint16_t numElements)
{
    int i;    
    if(time == NULL || numElements < 1)
    {
        LOG_ERROR_MSG(gCtx,"cbTime failed!");
        return;
    }

    for (i = 0; i<numElements; i++)
    {
        if (time[i].validityBits & GNSS_TIME_DATE_VALID)
        {
            char month [12] [4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};         
            LOG_INFO(gCtx,"Time Update[%d/%d]: timestamp=%llu UTC: %04d-%s-%02d %02d:%02d:%02d",
                 i+1,
                 numElements,
                 time[i].timestamp, 
                 time[i].year,
                 month[time[i].month%12],
                 time[i].day,
                 time[i].hour,
                 time[i].minute,
                 time[i].second);
        }
        else
        {
            LOG_INFO(gCtx,"Time Update[%d/%d]: Invalid Date/Time",
                 i+1,
                 numElements);
            
        }
    }
}

static void cbPosition(const TGNSSPosition position[], uint16_t numElements)
{
    int i;    
    if(position == NULL || numElements < 1)
    {
        LOG_ERROR_MSG(gCtx,"cbPosition failed!");
        return;
    }

    for (i = 0; i<numElements; i++)
    {
        LOG_INFO(gCtx,"Position Update[%d/%d]: timestamp=%llu latitude=%.5f longitude=%.5f altitudeMSL=%.1f hSpeed=%.1f heading=%.1f\n hdop=%.1f usedSatellites=%d sigmaHPosition=%.1f sigmaHSpeed=%.1f sigmaHeading=%.1f fixStatus=%d fixTypeBits=0x%08X",
                 i+1,
                 numElements,
                 position[i].timestamp, 
                 position[i].latitude,
                 position[i].longitude,
                 position[i].altitudeMSL,
                 position[i].hSpeed,
                 position[i].heading,
                 position[i].hdop,
                 position[i].usedSatellites,
                 position[i].sigmaHPosition,
                 position[i].sigmaHSpeed,
                 position[i].sigmaHeading,
                 position[i].fixStatus,
                 position[i].fixTypeBits);
    }
}

static void cbSatelliteDetail(const TGNSSSatelliteDetail satelliteDetail[], uint16_t numElements)
{
    int i;    
    if(satelliteDetail == NULL || numElements < 1)
    {
        LOG_ERROR_MSG(gCtx,"cbSatelliteDetail failed!");
        return;
    }

    for (i = 0; i<numElements; i++)
    {
        LOG_INFO(gCtx,"SatelliteDetail Update[%d/%d]: satelliteId=%d azimuth=%d elevation=%d SNR=%d",
                 i+1,
                 numElements,
                 satelliteDetail[i].satelliteId, 
                 satelliteDetail[i].azimuth,
                 satelliteDetail[i].elevation,                                   
                 satelliteDetail[i].SNR);
    }    
}

bool checkMajorVersion(int expectedMajor)
{
    int major = -1;

    gnssGetVersion(&major, 0, 0);

    if(major != expectedMajor)
    {
        LOG_ERROR(gCtx,"Wrong API version: gnssGetVersion returned unexpected value %d != %d",
                  major, 
                  expectedMajor);

        return false;
    }

    return true;
}

void init()
{
    if(!checkMajorVersion(3))
    {
        exit(EXIT_FAILURE);
    }

    if(!gnssInit())
    {
        exit(EXIT_FAILURE);
    }
}

int main()
{
    init();

    DLT_REGISTER_APP("GNSS", "GNSS-SERVICE-CLIENT");
    DLT_REGISTER_CONTEXT(gCtx,"GCLT", "Global Context");

    LOG_INFO_MSG(gCtx,"Starting gnss-service-client...");

    // register for GNSS
    gnssRegisterTimeCallback(&cbTime);    
    gnssRegisterSatelliteDetailCallback(&cbSatelliteDetail);
    gnssRegisterPositionCallback(&cbPosition);

    // enter endless loop
    while(1)
    {
        sleep(1);
    }

    // deregister
    gnssDeregisterTimeCallback(&cbTime);    
    gnssDeregisterSatelliteDetailCallback(&cbSatelliteDetail);
    gnssDeregisterPositionCallback(&cbPosition);

    gnssDestroy();

    return EXIT_SUCCESS;
}