Sample code for GNSS position information

//--------------------------------------------------------------------------------------------------
/**
* Handler function for Position Notifications.
*
*/
//--------------------------------------------------------------------------------------------------
static void PositionHandlerFunction
(
le_gnss_SampleRef_t positionSampleRef,
void* contextPtr
)
{
le_result_t result;
// Date parameters
uint16_t year;
uint16_t month;
uint16_t day;
// Time parameters
uint16_t hours;
uint16_t minutes;
uint16_t seconds;
uint16_t milliseconds;
// GPS time
uint32_t gpsWeek;
uint32_t gpsTimeOfWeek;
// Leap seconds in advance
uint8_t leapSeconds = 0;
// Position state
// Location
int32_t latitude;
int32_t longitude;
int64_t latitudeOnPZ90;
int64_t longitudeOnPZ90;
int32_t altitude;
int32_t altitudeOnWgs84;
int64_t altitudeOnPZ90;
int32_t hAccuracy;
int32_t vAccuracy;
int32_t magneticDeviation;
// DOP parameter
uint16_t dop;
// Horizontal speed
uint32_t hSpeed;
uint32_t hSpeedAccuracy;
// Vertical speed
int32_t vSpeed = 0;
int32_t vSpeedAccuracy = 0;
// Direction
uint32_t direction = 0;
uint32_t directionAccuracy = 0;
uint16_t satIdPtr[LE_GNSS_SV_INFO_MAX_LEN];
size_t satIdNumElements = NUM_ARRAY_MEMBERS(satIdPtr);
size_t satConstNumElements = NUM_ARRAY_MEMBERS(satConstPtr);
bool satUsedPtr[LE_GNSS_SV_INFO_MAX_LEN];
size_t satUsedNumElements = NUM_ARRAY_MEMBERS(satUsedPtr);
uint8_t satSnrPtr[LE_GNSS_SV_INFO_MAX_LEN];
size_t satSnrNumElements = NUM_ARRAY_MEMBERS(satSnrPtr);
uint16_t satAzimPtr[LE_GNSS_SV_INFO_MAX_LEN];
size_t satAzimNumElements = NUM_ARRAY_MEMBERS(satAzimPtr);
uint8_t satElevPtr[LE_GNSS_SV_INFO_MAX_LEN];
size_t satElevNumElements = NUM_ARRAY_MEMBERS(satElevPtr);
uint8_t satsInViewCount;
uint8_t satsTrackingCount;
uint8_t satsUsedCount;
int i;
 
static const char *tabDop[] =
{
"Position dilution of precision (PDOP)",
"Horizontal dilution of precision (HDOP)",
"Vertical dilution of precision (VDOP)",
"Geometric dilution of precision (GDOP)",
"Time dilution of precision (TDOP)"
};
 
if (NULL == positionSampleRef)
{
LE_ERROR("New Position sample is NULL!");
}
else
{
LE_DEBUG("New Position sample %p", positionSampleRef);
}
 
// Get position state
result = le_gnss_GetPositionState(positionSampleRef, &state);
{
le_gnss_ReleaseSampleRef(positionSampleRef);
return;
}
 
 
LE_TEST_OK((LE_OK == result), "Get position state");
LE_TEST_INFO("Position state: %s", (LE_GNSS_STATE_FIX_NO_POS == state)?"No Fix"
:(LE_GNSS_STATE_FIX_2D == state)?"2D Fix"
:(LE_GNSS_STATE_FIX_3D == state)?"3D Fix"
: "Unknown");
 
LE_TEST_OK(LE_OK == le_gnss_Stop(), "Stop GNSS after getting a fix");
 
// Get UTC date
result = le_gnss_GetDate(positionSampleRef, &year, &month, &day);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Get date");
 
// Get UTC time
result = le_gnss_GetTime(positionSampleRef, &hours, &minutes, &seconds, &milliseconds);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Get time");
 
// Get Epoch time
result = le_gnss_GetEpochTime(positionSampleRef, &EpochTime);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Get epoch time");
 
// Display time/date format 13:45:30 2009-06-15
LE_TEST_INFO("%02d:%02d:%02d %d-%02d-%02d,", hours, minutes, seconds, year, month, day);
 
// Display Epoch time
LE_TEST_INFO("epoch time: %llu:", (unsigned long long int) EpochTime);
 
LE_TEST_BEGIN_SKIP(!LINUX_OS, 4);
LE_TEST_OK(LE_OK == le_gnss_InjectUtcTime(EpochTime , 0), "Inject UTC time");
 
// Get GPS time
result = le_gnss_GetGpsTime(positionSampleRef, &gpsWeek, &gpsTimeOfWeek);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Get GPS time");
 
LE_TEST_INFO("GPS time W %02d:ToW %dms", gpsWeek, gpsTimeOfWeek);
 
// Get time accuracy
result = le_gnss_GetTimeAccuracy(positionSampleRef, &TimeAccuracy);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Get time accuracy");
 
LE_TEST_INFO("GPS time acc %d", TimeAccuracy);
 
// Get UTC leap seconds in advance
result = le_gnss_GetGpsLeapSeconds(positionSampleRef, &leapSeconds);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Get GPS leap seconds");
 
LE_TEST_INFO("UTC leap seconds in advance %d", leapSeconds);
 
// Get Location
result = le_gnss_GetLocation(positionSampleRef, &latitude, &longitude, &hAccuracy);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Get location");
 
if (LE_OK == result)
{
LE_INFO("Position lat.%f, long.%f, hAccuracy.%f",
(float)latitude/1000000.0,
(float)longitude/1000000.0,
(float)hAccuracy/100.0);
 
// Latitude conversion
latitude,
&latitudeOnPZ90);
LE_TEST_OK((LE_OK == result) || (LE_UNSUPPORTED == result),
"Convert latitude from WGS84 to PZ90");
if (LE_OK == result)
{
LE_INFO("Latitude: On WGS84 %d, On PZ90 %" PRId64 ", float %f",
latitude,
latitudeOnPZ90,
(float)latitudeOnPZ90/1000000.0);
}
 
// Longitude conversion
longitude,
&longitudeOnPZ90);
LE_TEST_OK((LE_OK == result) || (LE_UNSUPPORTED == result),
"Convert longitude from WGS84 to PZ90");
if (LE_OK == result)
{
LE_TEST_INFO("Longitude: On WGS84 %d, On PZ90 %" PRId64 ", float %f",
longitude,
longitudeOnPZ90,
(float)longitudeOnPZ90/1000000.0);
}
}
else
{
if (INT32_MAX != latitude)
{
LE_TEST_INFO("Latitude %f", (float)latitude/1000000.0);
}
else
{
LE_TEST_INFO("Latitude unknown %d", latitude);
}
 
if (INT32_MAX != longitude)
{
LE_TEST_INFO("Latitude %f", (float)longitude/1000000.0);
}
else
{
LE_TEST_INFO("Longitude unknown %d", longitude);
}
 
if (INT32_MAX != hAccuracy)
{
LE_TEST_INFO("Horizontal accuracy %f", (float)hAccuracy/100.0);
}
else
{
LE_TEST_INFO("Horizontal accuracy unknown %d", hAccuracy);
}
}
 
// Get altitude
LE_TEST_INFO("Test SetDataResolution() for vAccuracy parameter of le_gnss_GetAltitude() function");
 
for (dataRes=LE_GNSS_RES_ZERO_DECIMAL; dataRes<LE_GNSS_RES_UNKNOWN; dataRes++)
{
LE_TEST_BEGIN_SKIP(!LINUX_OS, 1);
"Set data resolution for vAccuracy");
 
result = le_gnss_GetAltitude( positionSampleRef, &altitude, &vAccuracy);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Get altitude");
 
if (LE_OK == result)
{
switch(dataRes)
{
LE_TEST_INFO("Resolution: %d decimal place, altitude.%f, vAccuracy.%f",
dataRes, (float)altitude/1000.0, (float)vAccuracy);
break;
LE_TEST_INFO("Resolution: %d decimal place, altitude.%f, vAccuracy.%f",
dataRes, (float)altitude/1000.0, (float)vAccuracy/10.0);
break;
LE_TEST_INFO("Resolution: %d decimal place, altitude.%f, vAccuracy.%f",
dataRes, (float)altitude/1000.0, (float)vAccuracy/100.0);
break;
LE_TEST_INFO("Resolution: %d decimal place, altitude.%f, vAccuracy.%f",
dataRes, (float)altitude/1000.0, (float)vAccuracy/1000.0);
break;
default:
LE_TEST_INFO("Unknown resolution.");
break;
}
}
else
{
LE_TEST_INFO("Altitude unknown [%d,%d]", altitude, vAccuracy);
}
}
 
// Get altitude in meters, between WGS-84 earth ellipsoid
// and mean sea level [resolution 1e-3]
result = le_gnss_GetAltitudeOnWgs84(positionSampleRef, &altitudeOnWgs84);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Get altitude on WGS84");
 
if (LE_OK == result)
{
LE_TEST_INFO("AltitudeOnWgs84: %f", (float)altitudeOnWgs84/1000.0);
 
altitudeOnWgs84,
&altitudeOnPZ90);
LE_TEST_OK((LE_OK == result) || (LE_UNSUPPORTED == result),
"Convert altitude from WGS84 to PZ90");
if (LE_OK == result)
{
LE_TEST_INFO("Altitude: On WGS84: %d, On PZ90 %" PRId64 ", float %f",
altitudeOnWgs84,
altitudeOnPZ90,
(float)altitudeOnPZ90/1000.0);
}
}
else
{
LE_TEST_INFO("AltitudeOnWgs84 unknown [%d]", altitudeOnWgs84);
}
 
LE_TEST_INFO("Dop parameters: \n");
 
LE_TEST_OK(LE_OK == le_gnss_SetDopResolution(DopRes), "Set DOP resolution");
LE_TEST_INFO("Set DOP resolution: %d decimal place\n", DopRes);
 
do
{
// Get DOP parameter
result = le_gnss_GetDilutionOfPrecision(positionSampleRef, dopType, &dop);
LE_TEST_OK((result == LE_OK) || (result == LE_OUT_OF_RANGE), "Get dopType:%d", dopType);
if (LE_OK == result)
{
switch(DopRes)
{
LE_TEST_INFO("resolution: %d decimal place, %s %.1f\n",
DopRes, tabDop[dopType], (float)dop);
break;
LE_TEST_INFO("resolution: %d decimal place, %s %.1f\n",
DopRes, tabDop[dopType], (float)(dop)/10);
break;
LE_TEST_INFO("resolution: %d decimal place, %s %.2f\n",
DopRes, tabDop[dopType], (float)(dop)/100);
break;
default:
LE_TEST_INFO("resolution: %d decimal place, %s %.3f\n",
DopRes, tabDop[dopType], (float)(dop)/1000);
break;
}
}
else
{
LE_TEST_INFO("%s invalid %d\n", tabDop[dopType], dop);
}
dopType++;
}
while (dopType != LE_GNSS_DOP_LAST);
 
// Get horizontal speed
LE_TEST_INFO("Test SetDataResolution() for hSpeedAccuracy parameter of le_gnss_GetHorizontalSpeed() \
function");
 
for (dataRes=LE_GNSS_RES_ZERO_DECIMAL; dataRes<LE_GNSS_RES_UNKNOWN; dataRes++)
{
LE_TEST_BEGIN_SKIP(!LINUX_OS, 1);
"Set data resolution for hSpeedAccuracy");
 
result = le_gnss_GetHorizontalSpeed( positionSampleRef, &hSpeed, &hSpeedAccuracy);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Get horizontal speed");
 
if (LE_OK == result)
{
switch(dataRes)
{
LE_TEST_INFO("Resolution: %d decimal place, hSpeed %u - Accuracy %.3f",
dataRes, hSpeed/100, (float)hSpeedAccuracy);
break;
LE_TEST_INFO("Resolution: %d decimal place, hSpeed %u - Accuracy %.3f",
dataRes, hSpeed/100, (float)hSpeedAccuracy/10);
break;
LE_TEST_INFO("Resolution: %d decimal place, hSpeed %u - Accuracy %.3f",
dataRes, hSpeed/100, (float)hSpeedAccuracy/100);
break;
LE_TEST_INFO("Resolution: %d decimal place, hSpeed %u - Accuracy %.3f",
dataRes, hSpeed/100, (float)hSpeedAccuracy/1000);
break;
default:
LE_TEST_INFO("Unknown resolution.");
break;
}
}
else
{
LE_TEST_INFO("hSpeed unknown [%u,%.3f]", hSpeed, (float)hSpeedAccuracy);
}
}
 
// Get vertical speed
for (dataRes=LE_GNSS_RES_ZERO_DECIMAL; dataRes<LE_GNSS_RES_UNKNOWN; dataRes++)
{
LE_TEST_BEGIN_SKIP(!LINUX_OS, 2);
"Set data resolution for vSpeedAccuracy");
result = le_gnss_GetVerticalSpeed( positionSampleRef, &vSpeed, &vSpeedAccuracy);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Get vertical speed");
 
if (LE_OK == result)
{
switch(dataRes)
{
LE_TEST_INFO("Resolution: %d decimal place, vSpeed %d - Accuracy %.3f",
dataRes, vSpeed/100, (float)vSpeedAccuracy);
break;
LE_TEST_INFO("Resolution: %d decimal place, vSpeed %d - Accuracy %.3f",
dataRes, vSpeed/100, (float)vSpeedAccuracy/10);
break;
LE_TEST_INFO("Resolution: %d decimal place, vSpeed %d - Accuracy %.3f",
dataRes, vSpeed/100, (float)vSpeedAccuracy/100);
break;
LE_TEST_INFO("Resolution: %d decimal place, vSpeed %d - Accuracy %.3f",
dataRes, vSpeed/100, (float)vSpeedAccuracy/1000);
break;
default:
LE_TEST_INFO("Unknown resolution.");
break;
}
}
else
{
LE_TEST_INFO("vSpeed unknown [%d,%.3f]", vSpeed, (float)vSpeedAccuracy);
}
}
 
// Get direction
LE_TEST_BEGIN_SKIP(!LINUX_OS, 1);
LE_TEST_OK((LE_OK == (result = le_gnss_GetDirection(positionSampleRef, &direction,
&directionAccuracy)) || (LE_OUT_OF_RANGE == result)), "Get direction");
 
LE_TEST_BEGIN_SKIP(LINUX_OS, 1);
LE_TEST_OK((LE_OK == (result = le_gnss_GetDirection(positionSampleRef, &direction, NULL)) ||
(LE_OUT_OF_RANGE == result)), "Get direction");
 
if (LE_OK == result)
{
LE_TEST_INFO("direction %u - Accuracy %u", direction/10, directionAccuracy/10);
}
else
{
LE_TEST_INFO("direction unknown [%u,%u]", direction, directionAccuracy);
}
 
// Get the magnetic deviation
result = le_gnss_GetMagneticDeviation( positionSampleRef, &magneticDeviation);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Get magnetic deviation");
if (LE_OK == result)
{
LE_TEST_INFO("magnetic deviation %d", magneticDeviation/10);
}
else
{
LE_TEST_INFO("magnetic deviation unknown [%d]",magneticDeviation);
}
 
/* Satellites status */
LE_TEST_BEGIN_SKIP(!LINUX_OS, 2);
result = le_gnss_GetSatellitesStatus(positionSampleRef,
&satsInViewCount,
&satsTrackingCount,
&satsUsedCount);
 
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result),
"Get satellite status");
 
LE_TEST_INFO("satsInView %d - satsTracking %d - satsUsed %d",
satsInViewCount,
satsTrackingCount,
satsUsedCount);
 
/* Satellites information */
result = le_gnss_GetSatellitesInfo(positionSampleRef,
satIdPtr,
&satIdNumElements,
satConstPtr,
&satConstNumElements,
satUsedPtr,
&satUsedNumElements,
satSnrPtr,
&satSnrNumElements,
satAzimPtr,
&satAzimNumElements,
satElevPtr,
&satElevNumElements);
 
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result),
"Get satellite info");
 
// Satellite Vehicle information
for (i=0; i<satIdNumElements; i++)
{
if ((0 != satIdPtr[i]) && (UINT16_MAX != satIdPtr[i]))
{
LE_TEST_INFO("[%02d] SVid %03d - C%01d - U%d - SNR%02d - Azim%03d - Elev%02d",
i,
satIdPtr[i],
satConstPtr[i],
satUsedPtr[i],
satSnrPtr[i],
satAzimPtr[i],
satElevPtr[i]);
 
if (LE_GNSS_SV_CONSTELLATION_SBAS == satConstPtr[i])
{
LE_TEST_INFO("SBAS category : %d", le_gnss_GetSbasConstellationCategory(satIdPtr[i]));
}
}
}
 
// Release provided Position sample reference
le_gnss_ReleaseSampleRef(positionSampleRef);
 
le_sem_Post(PositionHandlerSem);
}
 
//--------------------------------------------------------------------------------------------------
/**
* Test: Add Position Handler
*
*/
//--------------------------------------------------------------------------------------------------
static void* PositionThread
(
void* context
)
{
 
 
LE_INFO("======== Position Handler thread ========");
PositionHandlerRef = le_gnss_AddPositionHandler(PositionHandlerFunction, NULL);
LE_TEST_OK((PositionHandlerRef != NULL),
"Confirm position handler was added successfully");
 
return NULL;
}
 
//--------------------------------------------------------------------------------------------------
/**
* Test: GNSS position handler
*
*/
//--------------------------------------------------------------------------------------------------
static void TestLeGnssPositionHandler
(
void
)
{
le_result_t result;
le_thread_Ref_t positionThreadRef;
uint32_t ttff = 0;
uint8_t minElevation;
le_gnss_NmeaBitMask_t mask = LE_GNSS_NMEA_MASK_GPGGA | LE_GNSS_NMEA_MASK_GPGLL |
LE_GNSS_NMEA_MASK_GPRMC | LE_GNSS_NMEA_MASK_GPGNS |
LE_GNSS_NMEA_MASK_GPVTG | LE_GNSS_NMEA_MASK_GPZDA |
LE_GNSS_NMEA_MASK_GPGST | LE_GNSS_NMEA_MASK_GPGSA |
LE_GNSS_NMEA_MASK_GPGSV;
 
LE_TEST_INFO("Start Test Testle_gnss_PositionHandlerTest");
 
// All NMEA sentences must be enabled to get full position data on alt1250
LE_TEST_BEGIN_SKIP(LINUX_OS, 1);
LE_TEST_OK(LE_OK == le_gnss_SetNmeaSentences(mask), "Enable all supported NMEA sentences");
 
// NMEA frame GPGSA is checked that no SV with elevation below 10
// degrees are given.
LE_TEST_BEGIN_SKIP(!LINUX_OS, 1);
minElevation = 10;
result = le_gnss_SetMinElevation(minElevation);
LE_TEST_OK((LE_OK == result) || (LE_OUT_OF_RANGE == result), "Set min elevation to 10");
if (LE_OK == result)
{
LE_INFO("Set minElevation %d",minElevation);
}
 
// Test le_gnss_SetDataResolution() before starting GNSS
LE_TEST_INFO("Sanity test for le_gnss_SetDataResolution");
"Set invalid data resolution");
LE_TEST_INFO("Start GNSS");
LE_TEST_ASSERT(LE_OK == le_gnss_Start(), "Start GNSS");
LE_TEST_INFO("Wait 5 seconds");
 
// Test le_gnss_SetDataResolution() after starting GNSS
LE_GNSS_RES_UNKNOWN),
"Set invalid data resolution for vAccuracy");
 
// Add Position Handler Test
positionThreadRef = le_thread_Create("PositionThread",PositionThread,NULL);
le_thread_Start(positionThreadRef);
 
// test Cold Restart boosted by le_gnss_InjectUtcTime
// EpochTime and timeAccuracy should be valid and saved by now
LE_TEST_BEGIN_SKIP(!LINUX_OS, 3);
LE_TEST_OK(LE_OK == le_gnss_ForceColdRestart(), "Force cold restart");
 
// Last accurate epochTime and timeAccuracy are used
LE_TEST_OK(0 != EpochTime, "Confirm EpochTime is not 0");
LE_TEST_INFO("TimeAccuracy %d EpochTime %llu",TimeAccuracy, (unsigned long long int)EpochTime);
 
LE_TEST_OK(LE_OK == le_gnss_InjectUtcTime(EpochTime , TimeAccuracy), "Inject UTC time");
 
// Get TTFF,position fix should be still in progress for the FACTORY start
result = le_gnss_GetTtff(&ttff);
LE_TEST_OK(LE_BUSY == result, "Confirm TTFF is unavailable");
 
// Wait for a 3D fix
LE_TEST_INFO("Wait 60 seconds for a 3D fix");
time.sec = WAIT_MAX_FOR_3DFIX;
time.usec = 0;
LE_TEST_OK(LE_OK == le_sem_WaitWithTimeOut(PositionHandlerSem, time),
"Wait until position handler has executed successfully");
 
// Get TTFF
result = le_gnss_GetTtff(&ttff);
LE_TEST_OK((LE_OK == result) || (LE_BUSY == result), "Get TTFF");
if(result == LE_OK)
{
LE_TEST_INFO("TTFF cold restart = %d msec", ttff);
}
else
{
LE_TEST_INFO("TTFF cold restart not available");
}
 
le_gnss_RemovePositionHandler(PositionHandlerRef);
LE_TEST_INFO("Wait 5 seconds");
 
// stop thread
#ifdef LE_CONFIG_LINUX
le_thread_Cancel(positionThreadRef);
#endif
 
EpochTime=0;
TimeAccuracy=0;
}