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;
// Time accuracy
uint32_t timeAccuracy;
// Position state
// Location
int32_t latitude;
int32_t longitude;
int32_t altitude;
int32_t hAccuracy;
int32_t vAccuracy;
// DOP parameter
uint16_t hdop;
uint16_t vdop;
uint16_t pdop;
// Horizontal speed
uint32_t hSpeed;
uint32_t hSpeedAccuracy;
// Vertical speed
int32_t vSpeed;
int32_t vSpeedAccuracy;
// Direction
int32_t direction;
int32_t directionAccuracy;
 
if(positionSampleRef == NULL)
{
LE_ERROR("New Position sample is NULL!");
}
else
{
LE_DEBUG("New Position sample %p", positionSampleRef);
}
 
// Get UTC date
result = le_gnss_GetDate(positionSampleRef
, &year
, &month
, &day);
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
// Get UTC time
result = le_gnss_GetTime(positionSampleRef
, &hours
, &minutes
, &seconds
, &milliseconds);
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
 
// Display time/date format 13:45:30 2009-06-15
LE_INFO("%02d:%02d:%02d %d-%02d-%02d,"
, hours, minutes, seconds
, year, month, day);
 
// Get GPS time
result = le_gnss_GetGpsTime(positionSampleRef
, &gpsWeek
, &gpsTimeOfWeek);
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
 
LE_INFO("GPS time W %02d:ToW %dms"
, gpsWeek
, gpsTimeOfWeek);
 
// Get time accuracy
result = le_gnss_GetTimeAccuracy(positionSampleRef
, &timeAccuracy);
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
 
LE_INFO("GPS time acc %d"
, timeAccuracy);
 
// Get position state
result = le_gnss_GetPositionState( positionSampleRef, &state);
LE_ASSERT((result == LE_OK));
LE_DEBUG("Position state: %s", (state == LE_GNSS_STATE_FIX_NO_POS)?"No Fix"
:(state == LE_GNSS_STATE_FIX_2D)?"2D Fix"
:(state == LE_GNSS_STATE_FIX_3D)?"3D Fix"
: "Unknown");
 
// Get Location
result = le_gnss_GetLocation( positionSampleRef
, &latitude
, &longitude
, &hAccuracy);
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
if(result == LE_OK)
{
LE_INFO("Position lat.%d, long.%d, hAccuracy.%d"
, latitude, longitude, hAccuracy/10);
}
else
{
LE_INFO("Position unknown [%d,%d,%d]"
, latitude, longitude, hAccuracy);
}
 
// Get altitude
result = le_gnss_GetAltitude( positionSampleRef
, &altitude
, &vAccuracy);
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
 
if(result == LE_OK)
{
LE_INFO("Altitude.%d, vAccuracy.%d"
, altitude/1000, vAccuracy/10);
}
else
{
LE_INFO("Altitude unknown [%d,%d]"
, altitude, vAccuracy);
}
 
// Get DOP parameter
result = le_gnss_GetDop( positionSampleRef
, &hdop
, &vdop
, &pdop);
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
LE_INFO("DOP [H%.1f,V%.1f,P%.1f]"
, (float)(hdop)/100, (float)(vdop)/100, (float)(pdop)/100);
 
// Get horizontal speed
result = le_gnss_GetHorizontalSpeed( positionSampleRef
, &hSpeed
, &hSpeedAccuracy);
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
 
if(result == LE_OK)
{
LE_INFO("hSpeed %d - Accuracy %d"
, hSpeed/100, hSpeedAccuracy/10);
}
else
{
LE_INFO("hSpeed unknown [%d,%d]"
, hSpeed, hSpeedAccuracy);
}
 
// Get vertical speed
result = le_gnss_GetVerticalSpeed( positionSampleRef
, &vSpeed
, &vSpeedAccuracy);
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
if(result == LE_OK)
{
LE_INFO("vSpeed %d - Accuracy %d"
, vSpeed/100, vSpeedAccuracy/10);
}
else
{
LE_INFO("vSpeed unknown [%d,%d]"
, vSpeed, vSpeedAccuracy);
}
 
// Get direction
result = le_gnss_GetDirection( positionSampleRef
, &direction
, &directionAccuracy);
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
if(result == LE_OK)
{
LE_INFO("direction %d - Accuracy %d"
, direction/10, directionAccuracy/10);
}
else
{
LE_INFO("direction unknown [%d,%d]"
, direction, directionAccuracy);
}
 
/* Satellites status */
uint8_t satsInViewCount;
uint8_t satsTrackingCount;
uint8_t satsUsedCount;
result = le_gnss_GetSatellitesStatus(positionSampleRef
, &satsInViewCount
, &satsTrackingCount
, &satsUsedCount);
 
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
 
LE_INFO("satsInView %d - satsTracking %d - satsUsed %d"
, satsInViewCount, satsTrackingCount, satsUsedCount);
 
/* Satellites information */
uint16_t satIdPtr[LE_GNSS_SV_INFO_MAX_LEN];
size_t satIdNumElements = sizeof(satIdPtr);
size_t satConstNumElements = sizeof(satConstPtr);
bool satUsedPtr[LE_GNSS_SV_INFO_MAX_LEN];
size_t satUsedNumElements = sizeof(satUsedPtr);
uint8_t satSnrPtr[LE_GNSS_SV_INFO_MAX_LEN];
size_t satSnrNumElements = sizeof(satSnrPtr);
uint16_t satAzimPtr[LE_GNSS_SV_INFO_MAX_LEN];
size_t satAzimNumElements = sizeof(satAzimPtr);
uint8_t satElevPtr[LE_GNSS_SV_INFO_MAX_LEN];
size_t satElevNumElements = sizeof(satElevPtr);
int i;
 
result = le_gnss_GetSatellitesInfo(positionSampleRef
, satIdPtr
, &satIdNumElements
, satConstPtr
, &satConstNumElements
, satUsedPtr
, &satUsedNumElements
, satSnrPtr
, &satSnrNumElements
, satAzimPtr
, &satAzimNumElements
, satElevPtr
, &satElevNumElements);
 
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
 
// Satellite Vehicle information
for(i=0; i<satIdNumElements; i++)
{
if((satIdPtr[i] != 0)&&(satIdPtr[i] != UINT16_MAX))
{
LE_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]);
}
}
 
// Get satellites latency
int32_t latencyPtr[LE_GNSS_SV_INFO_MAX_LEN];
size_t latencyNumElements = sizeof(latencyPtr);
result = le_gnss_GetSatellitesLatency(positionSampleRef
, satIdPtr
, &satIdNumElements
, latencyPtr
, &latencyNumElements);
 
LE_ASSERT((result == LE_OK)||(result == LE_OUT_OF_RANGE));
 
// Satellite Vehicle information
for(i=0; i<latencyNumElements; i++)
{
if((satIdPtr[i] != 0)&&(satIdPtr[i] != UINT16_MAX))
{
LE_INFO("[%02d] SVid %03d - Latency %d"
, i
, satIdPtr[i]
, latencyPtr[i]);
}
}
 
// Release provided Position sample reference
le_gnss_ReleaseSampleRef(positionSampleRef);
}
 
 
//--------------------------------------------------------------------------------------------------
/**
* Test: Add Position Handler
*
*/
//--------------------------------------------------------------------------------------------------
static void* PositionThread
(
void* context
)
{
 
 
LE_INFO("======== Position Handler thread ========");
PositionHandlerRef = le_gnss_AddPositionHandler(PositionHandlerFunction, NULL);
LE_ASSERT((PositionHandlerRef != NULL));
 
return NULL;
}
 
//--------------------------------------------------------------------------------------------------
/**
* Test: GNSS position handler
*
*/
//--------------------------------------------------------------------------------------------------
static void TestLeGnssPositionHandler
(
void
)
{
le_thread_Ref_t positionThreadRef;
 
LE_INFO("Start Test Testle_gnss_PositionHandlerTest");
 
LE_INFO("Start GNSS");
LE_ASSERT((le_gnss_Start()) == LE_OK);
LE_INFO("Wait 5 seconds");
sleep(5);
 
// Add Position Handler Test
positionThreadRef = le_thread_Create("PositionThread",PositionThread,NULL);
le_thread_Start(positionThreadRef);
 
LE_INFO("Wait for a 3D fix");
sleep(60);
 
le_gnss_RemovePositionHandler(PositionHandlerRef);
 
LE_INFO("Wait 5 seconds");
sleep(5);
 
// stop thread
le_thread_Cancel(positionThreadRef);
 
LE_INFO("Stop GNSS");
LE_ASSERT((le_gnss_Stop()) == LE_OK);
}