<commit_before>//-------------------------------------------------------------------------------------------------
/**
 * @file gnss.c
 *
 * Tool to debug/monitor GNSS device.
 *
 * Copyright (C) Sierra Wireless Inc.
 */
//-------------------------------------------------------------------------------------------------


#include "legato.h"
#include "interfaces.h"

//-------------------------------------------------------------------------------------------------
/**
 * Default time(in second) for 3D fixing after starting gnss device.
 */
//-------------------------------------------------------------------------------------------------
#define DEFAULT_3D_FIX_TIME   60


//-------------------------------------------------------------------------------------------------
/**
 * Default watch period(in second) to get positioning information.
 */
//-------------------------------------------------------------------------------------------------
#define DEFAULT_WATCH_PERIOD   10*60


//-------------------------------------------------------------------------------------------------
/**
 * Different type of constellation.
 * {@
 */
//-------------------------------------------------------------------------------------------------
#define CONSTELLATION_GPS           1
#define CONSTELLATION_GLONASS       2
#define CONSTELLATION_BEIDOU        4
#define CONSTELLATION_GALILEO       8
#define CONSTELLATION_SBAS          0x10
#define CONSTELLATION_QZSS          0x12
// @}

//-------------------------------------------------------------------------------------------------
/**
 * Position handler reference.
 */
//-------------------------------------------------------------------------------------------------
static le_gnss_PositionHandlerRef_t PositionHandlerRef;


//-------------------------------------------------------------------------------------------------
/**
 * Array to store get parameter name.
 */
//-------------------------------------------------------------------------------------------------
static char ParamsName[128] = "";


//-------------------------------------------------------------------------------------------------
/**
 * Print the help text to stdout.
 */
//-------------------------------------------------------------------------------------------------
void PrintGnssHelp
(
    void
)
{
    puts("\
            NAME:\n\
                gnss - Used to access different functionality of gnss\n\
            \n\
            SYNOPSIS:\n\
                gnss help\n\
                gnss <enable/disable>\n\
                gnss <start/stop>\n\
                gnss restart <RestartType>\n\
                gnss fix [FixTime in seconds]\n\
                gnss get <parameter>\n\
                gnss get posInfo\n\
                gnss set constellation <ConstellationType>\n\
                gnss set agpsMode <ModeType>\n\
                gnss set acqRate <acqRate in milliseconds>\n\
                gnss set nmeaSentences <nmeaMask>\n\
                gnss watch [WatchPeriod in seconds]\n\
            \n\
            DESCRIPTION:\n\
                gnss help\n\
                    - Print this help message and exit\n\
                \n\
                gnss <enable/disable>\n\
                    - Enable/disable gnss device\n\
                \n\
                gnss <start/stop>\n\
                    - Start/stop gnss device\n\
                \n\
                gnss restart <RestartType>\n\
                    - Restart gnss device. Allowed when device in 'active' state. Restart type can\n\
                      be as follows:\n\
                         - hot\n\
                         - warm\n\
                         - cold\n\
                         - factory\n\
                    See GNSS topics in the Legato docs for more info on these restart types.\n\
                \n\
                gnss fix [FixTime in seconds]\n\
                    - Loop for certain time for first position fix. Here, FixTime is optional.\n\
                      Default time(60s) will be used if not specified\n\
                \n\
                gnss get <parameter>\n\
                    - Used to get different gnss parameter. Parameters and their descriptions as follow:\n\
                         - ttff --> Time to First Fix (milliseconds)\n\
                         - acqRate       --> Acquisition Rate (unit milliseconds)\n\
                         - agpsMode      --> Agps Mode\n\
                         - nmeaSentences --> Enabled NMEA sentences (bit mask)\n\
                         - constellation --> GNSS constellation\n\
                         - posState      --> Position fix state(no fix, 2D, 3D etc)\n\
                         - loc2d         --> 2D location (latitude, longitude, horizontal accuracy)\n\
                         - alt           --> Altitude (Altitude, Vertical accuracy)\n\
                         - loc3d         --> 3D location (latitude, longitude, altitude, horizontal accuracy,\n\
                                             vertical accuracy)\n\
                         - gpsTime       --> Get last updated gps time\n\
                         - time          --> Time of the last updated location\n\
                         - timeAcc       --> Time accuracy in milliseconds\n\
                         - date          --> Date of the last updated location\n\
                         - hSpeed        --> Horizontal speed(Horizontal Speed, Horizontal Speed accuracy)\n\
                         - vSpeed        --> Vertical speed(Vertical Speed, Vertical Speed accuracy)\n\
                         - motion        --> Motion data (Horizontal Speed, Horizontal Speed accuracy,\n\
                                             Vertical Speed, Vertical Speed accuracy)\n\
                         - direction     --> Direction indication\n\
                         - satInfo       --> Satellites Vehicle information\n\
                         - satStat       --> Satellites Vehicle status\n\
                         - dop           --> Dilution Of Precision for the fixed position\n\
                         - posInfo       --> Get all current position info of the device\n\
                         - status       --> Get gnss device's current status\n\
                \n\
                gnss set constellation <ConstellationType>\n\
                    - Used to set constellation. Allowed when device in 'ready' state. May require\n\
                      platform reboot, refer to platform documentation for details. ConstellationType\n\
                      can be as follows:\n\
                         - 1 --> GPS\n\
                         - 2 --> GLONASS\n\
                         - 4 --> BEIDOU\n\
                         - 8 --> GALILEO\n\
                      Please use sum of the values to set multiple constellation, e.g., 3 for GPS+GLONASS\n\
                      15 for GPS+GLONASS+BEIDOU+GALILEO\n\
                \n\
                gnss set agpsMode <ModeType>\n\
                    - Used to set agps mode. ModeType can be as follows:\n\
                         - alone --> Standalone agps mode\n\
                         - msBase --> MS-based agps mode\n\
                         - msAssist --> MS-assisted agps mode\n\
                \n\
                gnss set acqRate <acqRate in milliseconds>\n\
                    - Used to set acquisition rate. Available when device is in 'ready' state.\n\
                \n\
                gnss set nmeaSentences <nmeaMask>\n\
                    - Used to set the enabled NMEA sentences. \n\
                      Bit mask should be set with hexadecimal values, e.g., 7FFF\n\
                \n\
                gnss watch [WatchPeriod in seconds]\n\
                    - Used to monitor all gnss information (position, speed, satellites used, etc.).\n\
                      Here, WatchPeriod is optional. Default time(600s) will be used if not specified.\n\
                \n\
            Please note, some commands require gnss device to be in specific state (and platform reboot)\n\
            to produce valid result. See GNSS topics in the Legato docs for more info.\n\
         ");
}


//-------------------------------------------------------------------------------------------------
/**
 * This function enables gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int Enable
(
    void
)
{
    le_result_t result = LE_FAULT;

    result = le_gnss_Enable();

    switch (result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_DUPLICATE:
            printf("The GNSS device is already enabled\n");
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is not initialized\n");
            break;
        case LE_FAULT:
            printf("Failed to enable GNSS device\n");
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}



//-------------------------------------------------------------------------------------------------
/**
 * This function disables gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int Disable
(
    void
)
{
    le_result_t result = LE_FAULT;

    result = le_gnss_Disable();

    switch (result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_DUPLICATE:
            printf("The GNSS device is already disabled\n");
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is not initialized or started. Please see log for details\n");
            break;
        case LE_FAULT:
            printf("Failed to disable GNSS device\n");
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function starts gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int Start
(
    void
)
{
    le_result_t result = LE_FAULT;

    result = le_gnss_Start();

    switch (result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_DUPLICATE:
            printf("The GNSS device is already started\n");
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is disabled or not initialized. See logs for details\n");
            break;
        case LE_FAULT:
            printf("Failed to start GNSS device. See logs for details\n");
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function stops gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int Stop
(
    void
)
{
    le_result_t result = LE_FAULT;

    result = le_gnss_Stop();

    switch (result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_DUPLICATE:
            printf("The GNSS device is already stopped\n");
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is not initialized or disabled. See logs for details\n");
            break;
        case LE_FAULT:
            printf("Failed to stop GNSS device. See logs for details\n");
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function restarts gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int Restart
(
    const char* restartTypePtr      ///< [IN] Type of restart, i.e. hot/warm/cold/factory etc
)
{
    le_result_t result = LE_FAULT;


    if (strcmp(restartTypePtr, "cold") == 0)
    {
        printf("Doing cold restart...\n");
        result = le_gnss_ForceColdRestart();
    }
    else if (strcmp(restartTypePtr, "warm") == 0)
    {
        printf("Doing warm restart...\n");
        result = le_gnss_ForceWarmRestart();
    }
    else if (strcmp(restartTypePtr, "hot") == 0)
    {
        printf("Doing hot restart...\n");
        result = le_gnss_ForceHotRestart();
    }
    else if (strcmp(restartTypePtr, "factory") == 0)
    {
        printf("Doing factory restart...\n");
        result = le_gnss_ForceFactoryRestart();
    }
    else
    {
        printf("Invalid parameter: %s\n", restartTypePtr);
        return EXIT_FAILURE;
    }

    switch (result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is not enabled or not started. See logs for details\n");
            break;
        case LE_FAULT:
            printf("Failed to do '%s' restart. See logs for details\n", restartTypePtr);
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function sets gnss device acquisition rate.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int SetAcquisitionRate
(
    const char* acqRateStr          ///< [IN] Acquisition rate in milliseconds.
)
{
    char *end;
    errno = 0;
    uint32_t acqRate = strtoul(acqRateStr, &end, 10);

    if (end[0] != '\0' || errno != 0)
    {
        printf("Bad acquisition rate: %s\n", acqRateStr);
        return EXIT_FAILURE;
    }

    le_result_t result = le_gnss_SetAcquisitionRate(acqRate);

    switch (result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_FAULT:
            printf("Failed to Set acquisition rate\n");
            break;
        case LE_UNSUPPORTED:
            printf("Request is not supported\n");
            break;
        case LE_NOT_PERMITTED:
            printf("GNSS device is not in \"ready\" state\n");
            break;
        case LE_TIMEOUT:
            printf("Timeout error\n");
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function sets constellation of gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int SetConstellation
(
    const char* constellationPtr
)
{
    int32_t constellationMask = 0;

    char *endPtr;
    errno = 0;
    int constellationSum = strtoul(constellationPtr, &endPtr, 10);

    if (endPtr[0] != '\0' || errno != 0 || constellationSum == 0)
    {
        fprintf(stderr, "Bad constellation parameter: %s\n", constellationPtr);
        exit(EXIT_FAILURE);
    }

    char constellationStr[256] = "[";
    if (constellationSum & CONSTELLATION_GPS)
    {
        constellationMask |= LE_GNSS_CONSTELLATION_GPS;
        constellationSum -= CONSTELLATION_GPS;
        strcat(constellationStr, "GPS ");
    }
    if (constellationSum & CONSTELLATION_GLONASS)
    {
        constellationMask |= LE_GNSS_CONSTELLATION_GLONASS;
        constellationSum -= CONSTELLATION_GLONASS;
        strcat(constellationStr, "GLONASS ");
    }
    if (constellationSum & CONSTELLATION_BEIDOU)
    {
        constellationMask = LE_GNSS_CONSTELLATION_BEIDOU;
        constellationSum -= CONSTELLATION_BEIDOU;
        strcat(constellationStr, "BEIDOU ");
    }
    if (constellationSum & CONSTELLATION_GALILEO)
    {
        constellationMask = LE_GNSS_CONSTELLATION_GALILEO;
        constellationSum -= CONSTELLATION_GALILEO;
        strcat(constellationStr, "GALILEO");
    }
    if (constellationSum & CONSTELLATION_SBAS)
    {
        constellationMask = LE_GNSS_CONSTELLATION_SBAS;
        constellationSum -= CONSTELLATION_SBAS;
        strcat(constellationStr, "SBAS");
    }
    if (constellationSum & CONSTELLATION_QZSS)
    {
        constellationMask = LE_GNSS_CONSTELLATION_QZSS;
        constellationSum -= CONSTELLATION_QZSS;
        strcat(constellationStr, "QZSS");
    }
    strcat(constellationStr, "]");

    LE_INFO("Setting constellation %s",constellationStr);

    // Right now all constellation sum should be zero
    if (constellationSum != 0)
    {
        fprintf(stderr, "Bad constellation parameter: %s\n", constellationPtr);
        exit(EXIT_FAILURE);
    }

    le_result_t result = le_gnss_SetConstellation(
                                                  (le_gnss_ConstellationBitMask_t)constellationMask
                                                 );

    switch(result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_UNSUPPORTED:
            printf("Setting constellation %s is not supported\n", constellationStr);
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is not initialized, disabled or active. See logs for  \
                    details\n");
            break;
        case LE_FAULT:
            printf("Failed!\n");
            break;
        default:
            printf("Bad return value: %d\n", result);
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function sets agps mode of gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int SetAgpsMode
(
    const char* agpsModePtr
)
{
    le_gnss_AssistedMode_t suplAgpsMode;

    if (strcmp(agpsModePtr, "alone") == 0)
    {
        suplAgpsMode = LE_GNSS_STANDALONE_MODE;
    }
    else if (strcmp(agpsModePtr,"msBase") == 0)
    {
        suplAgpsMode = LE_GNSS_MS_BASED_MODE;
    }
    else if (strcmp(agpsModePtr,"msAssist") == 0)
    {
        suplAgpsMode = LE_GNSS_MS_ASSISTED_MODE;
    }
    else
    {
        printf("Bad agps mode: %s\n", agpsModePtr);
        return EXIT_FAILURE;
    }

    le_result_t result = le_gnss_SetSuplAssistedMode(suplAgpsMode);

    switch(result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_UNSUPPORTED:
            printf("The request is not supported\n");
            break;
        case LE_TIMEOUT:
            printf("Timeout error\n");
            break;
        case LE_FAULT:
            printf("Failed!\n");
            break;
        default:
            printf("Bad return value: %d\n", result);
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function sets the enabled NMEA sentences.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int SetNmeaSentences
(
    const char* nmeaMaskStr     ///< [IN] Enabled NMEA sentences bit mask.
)
{
    int nmeaMask = le_hex_HexaToInteger(nmeaMaskStr);

    if (nmeaMask < 0)
    {
        printf("Bad NMEA sentences mask: %s\n", nmeaMaskStr);
        return EXIT_FAILURE;
    }

    le_result_t result = le_gnss_SetNmeaSentences(nmeaMask);

    switch (result)
    {
        case LE_OK:
            printf("Successfully set enabled NMEA sentences!\n");
            break;
        case LE_FAULT:
            printf("Failed to set enabled NMEA sentences. See logs for details\n");
            break;
        case LE_BAD_PARAMETER:
            printf("Failed to set enabled NMEA sentences, incompatible bit mask\n");
            break;
        case LE_BUSY:
            printf("Failed to set enabled NMEA sentences, service is busy\n");
            break;
        case LE_TIMEOUT:
            printf("Failed to set enabled NMEA sentences, timeout error\n");
            break;
        default:
            printf("Failed to set enabled NMEA sentences, error %d (%s)\n",
                    result, LE_RESULT_TXT(result));
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function gets ttff(Time to First Fix) value.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int GetTtff
(
    void
)
{
    uint32_t ttff;
    le_result_t result = le_gnss_GetTtff(&ttff);

    switch (result)
    {
        case LE_OK:
            printf("TTFF(Time to First Fix) = %ums\n", ttff);
            break;
        case LE_BUSY:
            printf("The position is not fixed and TTFF can't be measured. See logs for details\n");
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is not started or disabled. See logs for details\n");
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function gets the agps mode.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int GetAgpsMode
(
    void
)
{
    le_gnss_AssistedMode_t assistedMode;
    le_result_t result = le_gnss_GetSuplAssistedMode(&assistedMode);

    if (result == LE_OK)
    {
        switch (assistedMode)
        {
            case LE_GNSS_STANDALONE_MODE:
                printf("AGPS mode: Standalone\n");
                break;
            case LE_GNSS_MS_BASED_MODE:
                printf("AGPS mode: MS-based\n");
                break;
            case LE_GNSS_MS_ASSISTED_MODE:
                printf("AGPS mode: MS-assisted\n");
                break;
        }
    }
    else
    {
        printf("Failed! See log for details\n");
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function gets constellation of gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int GetConstellation
(
    void
)
{
    le_gnss_ConstellationBitMask_t constellationMask;
    le_result_t result = le_gnss_GetConstellation(&constellationMask);

    if (result == LE_OK)
    {
        if (constellationMask & LE_GNSS_CONSTELLATION_GPS)
        {
            printf("GPS activated\n");
        }
        if (constellationMask & LE_GNSS_CONSTELLATION_GLONASS)
        {
            printf("GLONASS activated\n");
        }
        if (constellationMask & LE_GNSS_CONSTELLATION_BEIDOU)
        {
            printf("BEIDOU activated\n");
        }
        if (constellationMask & LE_GNSS_CONSTELLATION_GALILEO)
        {
            printf("BEIDOU activated\n");
        }
    }
    else
    {
        printf("Failed! See log for details!\n");
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}
<commit_msg>[GNSS tool] Correct the constellation setting<commit_after>//-------------------------------------------------------------------------------------------------
/**
 * @file gnss.c
 *
 * Tool to debug/monitor GNSS device.
 *
 * Copyright (C) Sierra Wireless Inc.
 */
//-------------------------------------------------------------------------------------------------


#include "legato.h"
#include "interfaces.h"

//-------------------------------------------------------------------------------------------------
/**
 * Default time(in second) for 3D fixing after starting gnss device.
 */
//-------------------------------------------------------------------------------------------------
#define DEFAULT_3D_FIX_TIME   60


//-------------------------------------------------------------------------------------------------
/**
 * Default watch period(in second) to get positioning information.
 */
//-------------------------------------------------------------------------------------------------
#define DEFAULT_WATCH_PERIOD   10*60


//-------------------------------------------------------------------------------------------------
/**
 * Max characters for constellations name.
 */
//-------------------------------------------------------------------------------------------------
#define CONSTELLATIONS_NAME_LEN     256

//-------------------------------------------------------------------------------------------------
/**
 * Different type of constellation.
 * {@
 */
//-------------------------------------------------------------------------------------------------
#define CONSTELLATION_GPS           0x1
#define CONSTELLATION_GLONASS       0x2
#define CONSTELLATION_BEIDOU        0x4
#define CONSTELLATION_GALILEO       0x8
#define CONSTELLATION_SBAS          0x10
#define CONSTELLATION_QZSS          0x20
// @}

//-------------------------------------------------------------------------------------------------
/**
 * Position handler reference.
 */
//-------------------------------------------------------------------------------------------------
static le_gnss_PositionHandlerRef_t PositionHandlerRef;


//-------------------------------------------------------------------------------------------------
/**
 * Array to store get parameter name.
 */
//-------------------------------------------------------------------------------------------------
static char ParamsName[128] = "";

//-------------------------------------------------------------------------------------------------
/**
 * Print the help text to stdout.
 */
//-------------------------------------------------------------------------------------------------
void PrintGnssHelp
(
    void
)
{
    puts("\n\t\tNAME:\n"
         "\t\t\tgnss - Used to access different functionality of gnss\n\n"
         "\t\tSYNOPSIS:\n"
         "\t\t\tgnss help\n"
         "\t\t\tgnss <enable/disable>\n"
         "\t\t\tgnss <start/stop>\n"
         "\t\t\tgnss restart <RestartType>\n"
         "\t\t\tgnss fix [FixTime in seconds]\n"
         "\t\t\tgnss get <parameter>\n"
         "\t\t\tgnss get posInfo\n"
         "\t\t\tgnss set constellation <ConstellationType>\n"
         "\t\t\tgnss set agpsMode <ModeType>\n"
         "\t\t\tgnss set acqRate <acqRate in milliseconds>\n"
         "\t\t\tgnss set nmeaSentences <nmeaMask>\n"
         "\t\t\tgnss watch [WatchPeriod in seconds]\n\n"
         "\t\tDESCRIPTION:\n"
         "\t\t\tgnss help\n"
         "\t\t\t\t- Print this help message and exit\n\n"
         "\t\t\tgnss <enable/disable>\n"
         "\t\t\t\t- Enable/disable gnss device\n\n"
         "\t\t\tgnss <start/stop>\n"
         "\t\t\t\t- Start/stop gnss device\n\n"
         "\t\t\tgnss restart <RestartType>\n"
         "\t\t\t\t- Restart gnss device. Allowed when device in 'active' state. Restart type can\n"
         "\t\t\t\t  be as follows:\n"
         "\t\t\t\t\t- hot\n"
         "\t\t\t\t\t- warm\n"
         "\t\t\t\t\t- cold\n"
         "\t\t\t\t\t- factory\n"
         "\t\t\t\tTo know more about these restart types, please look at: \n"
         "\t\t\t\t           http://legato.io/legato-docs/latest/c_gnss.html\n\n"
         "\t\t\tgnss fix [FixTime in seconds]\n"
         "\t\t\t\t- Loop for certain time for first position fix. Here, FixTime is optional.\n"
         "\t\t\t\t  Default time(60s) will be used if not specified\n\n"
         "\t\t\tgnss get <parameter>\n"
         "\t\t\t\t- Used to get different gnss parameter.\n"
         "\t\t\t\t  Follows parameters and their descriptions :\n"
         "\t\t\t\t\t- ttff          --> Time to First Fix (milliseconds)\n"
         "\t\t\t\t\t- acqRate       --> Acquisition Rate (unit milliseconds)\n"
         "\t\t\t\t\t- agpsMode      --> Agps Mode\n"
         "\t\t\t\t\t- nmeaSentences --> Enabled NMEA sentences (bit mask)\n"
         "\t\t\t\t\t- constellation --> GNSS constellation\n"
         "\t\t\t\t\t- posState      --> Position fix state(no fix, 2D, 3D etc)\n"
         "\t\t\t\t\t- loc2d         --> 2D location (latitude, longitude, horizontal accuracy)\n"
         "\t\t\t\t\t- alt           --> Altitude (Altitude, Vertical accuracy)\n"
         "\t\t\t\t\t- loc3d         --> 3D location (latitude, longitude, altitude,\n"
         "\t\t\t\t\t                    horizontal accuracy, vertical accuracy)\n"
         "\t\t\t\t\t- gpsTime       --> Get last updated gps time\n"
         "\t\t\t\t\t- time          --> Time of the last updated location\n"
         "\t\t\t\t\t- timeAcc       --> Time accuracy in milliseconds\n"
         "\t\t\t\t\t- date          --> Date of the last updated location\n"
         "\t\t\t\t\t- hSpeed        --> Horizontal speed(Horizontal Speed, Horizontal\n"
         "\t\t\t\t\t                    Speed accuracy)\n"
         "\t\t\t\t\t- vSpeed        --> Vertical speed(Vertical Speed, Vertical Speed accuracy)\n"
         "\t\t\t\t\t- motion        --> Motion data (Horizontal Speed, Horizontal Speed accuracy,\n"
         "\t\t\t\t\t                    Vertical Speed, Vertical Speed accuracy)\n"
         "\t\t\t\t\t- direction     --> Direction indication\n"
         "\t\t\t\t\t- satInfo       --> Satellites Vehicle information\n"
         "\t\t\t\t\t- satStat       --> Satellites Vehicle status\n"
         "\t\t\t\t\t- dop           --> Dilution Of Precision for the fixed position\n"
         "\t\t\t\t\t- posInfo       --> Get all current position info of the device\n"
         "\t\t\t\t\t- status        --> Get gnss device's current status\n\n"
         "\t\t\tgnss set constellation <ConstellationType>\n"
         "\t\t\t\t- Used to set constellation. Allowed when device in 'ready' state. May require\n"
         "\t\t\t\t  platform reboot, please look platform documentation for details.\n"
         "\t\t\t\t  ConstellationType can be as follows:\n"
         "\t\t\t\t\t- 1 ---> GPS\n"
         "\t\t\t\t\t- 2 ---> GLONASS\n"
         "\t\t\t\t\t- 4 ---> BEIDOU\n"
         "\t\t\t\t\t- 8 ---> GALILEO\n"
         "\t\t\t\t\t- 16 --> SBAS\n"
         "\t\t\t\t\t- 32 --> QZSS\n"
         "\t\t\t\tPlease use sum of the values to set multiple constellation, e.g.\n"
         "\t\t\t\t3 for GPS+GLONASS, 63 for GPS+GLONASS+BEIDOU+GALILEO+SBAS+QZSS\n\n"
         "\t\t\tgnss set agpsMode <ModeType>\n"
         "\t\t\t\t- Used to set agps mode. ModeType can be as follows:\n"
         "\t\t\t\t\t- alone -----> Standalone agps mode\n"
         "\t\t\t\t\t- msBase ----> MS-based agps mode\n"
         "\t\t\t\t\t- msAssist --> MS-assisted agps mode\n\n"
         "\t\t\tgnss set acqRate <acqRate in milliseconds>\n"
         "\t\t\t\t- Used to set acquisition rate.\n"
         "\t\t\t\t  Please note that it is available when the device is 'ready' state.\n\n"
         "\t\t\tgnss set nmeaSentences <nmeaMask>\n"
         "\t\t\t\t- Used to set the enabled NMEA sentences. \n"
         "\t\t\t\t  Bit mask should be set with hexadecimal values, e.g. 7FFF\n\n"
         "\t\t\tgnss watch [WatchPeriod in seconds]\n"
         "\t\t\t\t- Used to monitor all gnss information(position, speed, satellites used etc).\n"
         "\t\t\t\t  Here, WatchPeriod is optional. Default time(600s) will be used if not\n"
         "\t\t\t\t  specified\n\n"
         "\tPlease note, some commands require gnss device to be in specific state\n"
         "\t(and platform reboot) to produce valid result. Please look :\n"
         "\thttp://legato.io/legato-docs/latest/howToGNSS.html,\n"
         "\thttp://legato.io/legato-docs/latest/c_gnss.html and platform documentation for more\n"
         "\tdetails.\n"
         );
}




//-------------------------------------------------------------------------------------------------
/**
 * This function enables gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int Enable
(
    void
)
{
    le_result_t result = LE_FAULT;

    result = le_gnss_Enable();

    switch (result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_DUPLICATE:
            printf("The GNSS device is already enabled\n");
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is not initialized\n");
            break;
        case LE_FAULT:
            printf("Failed to enable GNSS device\n");
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}



//-------------------------------------------------------------------------------------------------
/**
 * This function disables gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int Disable
(
    void
)
{
    le_result_t result = LE_FAULT;

    result = le_gnss_Disable();

    switch (result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_DUPLICATE:
            printf("The GNSS device is already disabled\n");
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is not initialized or started. Please see log for details\n");
            break;
        case LE_FAULT:
            printf("Failed to disable GNSS device\n");
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function starts gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int Start
(
    void
)
{
    le_result_t result = LE_FAULT;

    result = le_gnss_Start();

    switch (result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_DUPLICATE:
            printf("The GNSS device is already started\n");
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is disabled or not initialized. See logs for details\n");
            break;
        case LE_FAULT:
            printf("Failed to start GNSS device. See logs for details\n");
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function stops gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int Stop
(
    void
)
{
    le_result_t result = LE_FAULT;

    result = le_gnss_Stop();

    switch (result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_DUPLICATE:
            printf("The GNSS device is already stopped\n");
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is not initialized or disabled. See logs for details\n");
            break;
        case LE_FAULT:
            printf("Failed to stop GNSS device. See logs for details\n");
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function restarts gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int Restart
(
    const char* restartTypePtr      ///< [IN] Type of restart, i.e. hot/warm/cold/factory etc
)
{
    le_result_t result = LE_FAULT;


    if (strcmp(restartTypePtr, "cold") == 0)
    {
        printf("Doing cold restart...\n");
        result = le_gnss_ForceColdRestart();
    }
    else if (strcmp(restartTypePtr, "warm") == 0)
    {
        printf("Doing warm restart...\n");
        result = le_gnss_ForceWarmRestart();
    }
    else if (strcmp(restartTypePtr, "hot") == 0)
    {
        printf("Doing hot restart...\n");
        result = le_gnss_ForceHotRestart();
    }
    else if (strcmp(restartTypePtr, "factory") == 0)
    {
        printf("Doing factory restart...\n");
        result = le_gnss_ForceFactoryRestart();
    }
    else
    {
        printf("Invalid parameter: %s\n", restartTypePtr);
        return EXIT_FAILURE;
    }

    switch (result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is not enabled or not started. See logs for details\n");
            break;
        case LE_FAULT:
            printf("Failed to do '%s' restart. See logs for details\n", restartTypePtr);
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function sets gnss device acquisition rate.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int SetAcquisitionRate
(
    const char* acqRateStr          ///< [IN] Acquisition rate in milliseconds.
)
{
    char *end;
    errno = 0;
    uint32_t acqRate = strtoul(acqRateStr, &end, 10);

    if (end[0] != '\0' || errno != 0)
    {
        printf("Bad acquisition rate: %s\n", acqRateStr);
        return EXIT_FAILURE;
    }

    le_result_t result = le_gnss_SetAcquisitionRate(acqRate);

    switch (result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_FAULT:
            printf("Failed to Set acquisition rate\n");
            break;
        case LE_UNSUPPORTED:
            printf("Request is not supported\n");
            break;
        case LE_NOT_PERMITTED:
            printf("GNSS device is not in \"ready\" state\n");
            break;
        case LE_TIMEOUT:
            printf("Timeout error\n");
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function sets constellation of gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int SetConstellation
(
    const char* constellationPtr
)
{
    uint32_t constellationMask = 0;

    char *endPtr;
    errno = 0;
    int constellationSum = strtoul(constellationPtr, &endPtr, 10);

    if (endPtr[0] != '\0' || errno != 0 || constellationSum == 0)
    {
        fprintf(stderr, "Bad constellation parameter: %s\n", constellationPtr);
        exit(EXIT_FAILURE);
    }

    char constellationStr[CONSTELLATIONS_NAME_LEN] = "[";
    if (constellationSum & CONSTELLATION_GPS)
    {
        constellationMask |= (uint32_t)LE_GNSS_CONSTELLATION_GPS;
        constellationSum -= CONSTELLATION_GPS;
        strncat(constellationStr, "GPS ", CONSTELLATIONS_NAME_LEN);
    }
    if (constellationSum & CONSTELLATION_GLONASS)
    {
        constellationMask |= (uint32_t)LE_GNSS_CONSTELLATION_GLONASS;
        constellationSum -= CONSTELLATION_GLONASS;
        strncat(constellationStr, "GLONASS ", CONSTELLATIONS_NAME_LEN);
    }
    if (constellationSum & CONSTELLATION_BEIDOU)
    {
        constellationMask |= (uint32_t)LE_GNSS_CONSTELLATION_BEIDOU;
        constellationSum -= CONSTELLATION_BEIDOU;
        strncat(constellationStr, "BEIDOU ", CONSTELLATIONS_NAME_LEN);
    }
    if (constellationSum & CONSTELLATION_GALILEO)
    {
        constellationMask |= (uint32_t)LE_GNSS_CONSTELLATION_GALILEO;
        constellationSum -= CONSTELLATION_GALILEO;
        strncat(constellationStr, "GALILEO ", CONSTELLATIONS_NAME_LEN);
    }
    if (constellationSum & CONSTELLATION_SBAS)
    {
        constellationMask |= (uint32_t)LE_GNSS_CONSTELLATION_SBAS;
        constellationSum -= CONSTELLATION_SBAS;
        strncat(constellationStr, "SBAS ", CONSTELLATIONS_NAME_LEN);
    }
    if (constellationSum & CONSTELLATION_QZSS)
    {
        constellationMask |= (uint32_t)LE_GNSS_CONSTELLATION_QZSS;
        constellationSum -= CONSTELLATION_QZSS;
        strncat(constellationStr, "QZSS ", CONSTELLATIONS_NAME_LEN);
    }
    strncat(constellationStr, "]", CONSTELLATIONS_NAME_LEN);

    LE_INFO("Setting constellation %s",constellationStr);

    // Right now all constellation sum should be zero
    if (constellationSum != 0)
    {
        fprintf(stderr, "Bad constellation parameter: %s\n", constellationPtr);
        exit(EXIT_FAILURE);
    }

    le_result_t result = le_gnss_SetConstellation(
                                                  (le_gnss_ConstellationBitMask_t)constellationMask
                                                 );

    switch(result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_UNSUPPORTED:
            printf("Setting constellation %s is not supported\n", constellationStr);
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is not initialized, disabled or active. See logs for  \
                    details\n");
            break;
        case LE_FAULT:
            printf("Failed!\n");
            break;
        default:
            printf("Bad return value: %d\n", result);
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function sets agps mode of gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int SetAgpsMode
(
    const char* agpsModePtr
)
{
    le_gnss_AssistedMode_t suplAgpsMode;

    if (strcmp(agpsModePtr, "alone") == 0)
    {
        suplAgpsMode = LE_GNSS_STANDALONE_MODE;
    }
    else if (strcmp(agpsModePtr,"msBase") == 0)
    {
        suplAgpsMode = LE_GNSS_MS_BASED_MODE;
    }
    else if (strcmp(agpsModePtr,"msAssist") == 0)
    {
        suplAgpsMode = LE_GNSS_MS_ASSISTED_MODE;
    }
    else
    {
        printf("Bad agps mode: %s\n", agpsModePtr);
        return EXIT_FAILURE;
    }

    le_result_t result = le_gnss_SetSuplAssistedMode(suplAgpsMode);

    switch(result)
    {
        case LE_OK:
            printf("Success!\n");
            break;
        case LE_UNSUPPORTED:
            printf("The request is not supported\n");
            break;
        case LE_TIMEOUT:
            printf("Timeout error\n");
            break;
        case LE_FAULT:
            printf("Failed!\n");
            break;
        default:
            printf("Bad return value: %d\n", result);
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function sets the enabled NMEA sentences.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int SetNmeaSentences
(
    const char* nmeaMaskStr     ///< [IN] Enabled NMEA sentences bit mask.
)
{
    int nmeaMask = le_hex_HexaToInteger(nmeaMaskStr);

    if (nmeaMask < 0)
    {
        printf("Bad NMEA sentences mask: %s\n", nmeaMaskStr);
        return EXIT_FAILURE;
    }

    le_result_t result = le_gnss_SetNmeaSentences(nmeaMask);

    switch (result)
    {
        case LE_OK:
            printf("Successfully set enabled NMEA sentences!\n");
            break;
        case LE_FAULT:
            printf("Failed to set enabled NMEA sentences. See logs for details\n");
            break;
        case LE_BAD_PARAMETER:
            printf("Failed to set enabled NMEA sentences, incompatible bit mask\n");
            break;
        case LE_BUSY:
            printf("Failed to set enabled NMEA sentences, service is busy\n");
            break;
        case LE_TIMEOUT:
            printf("Failed to set enabled NMEA sentences, timeout error\n");
            break;
        default:
            printf("Failed to set enabled NMEA sentences, error %d (%s)\n",
                    result, LE_RESULT_TXT(result));
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function gets ttff(Time to First Fix) value.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int GetTtff
(
    void
)
{
    uint32_t ttff;
    le_result_t result = le_gnss_GetTtff(&ttff);

    switch (result)
    {
        case LE_OK:
            printf("TTFF(Time to First Fix) = %ums\n", ttff);
            break;
        case LE_BUSY:
            printf("The position is not fixed and TTFF can't be measured. See logs for details\n");
            break;
        case LE_NOT_PERMITTED:
            printf("The GNSS device is not started or disabled. See logs for details\n");
            break;
        default:
            printf("Invalid status\n");
            break;
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function gets the agps mode.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int GetAgpsMode
(
    void
)
{
    le_gnss_AssistedMode_t assistedMode;
    le_result_t result = le_gnss_GetSuplAssistedMode(&assistedMode);

    if (result == LE_OK)
    {
        switch (assistedMode)
        {
            case LE_GNSS_STANDALONE_MODE:
                printf("AGPS mode: Standalone\n");
                break;
            case LE_GNSS_MS_BASED_MODE:
                printf("AGPS mode: MS-based\n");
                break;
            case LE_GNSS_MS_ASSISTED_MODE:
                printf("AGPS mode: MS-assisted\n");
                break;
        }
    }
    else
    {
        printf("Failed! See log for details\n");
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}


//-------------------------------------------------------------------------------------------------
/**
 * This function gets constellation of gnss device.
 *
 * @return
 *     - EXIT_SUCCESS on success.
 *     - EXIT_FAILURE on failure.
 */
//-------------------------------------------------------------------------------------------------
static int GetConstellation
(
    void
)
{
    le_gnss_ConstellationBitMask_t constellationMask;
    le_result_t result = le_gnss_GetConstellation(&constellationMask);

    printf("ConstellationType %d\n", constellationMask);

    if (result == LE_OK)
    {
        (constellationMask & LE_GNSS_CONSTELLATION_GPS)     ? printf("GPS activated\n") :
                                                              printf("GPS not activated\n");
        (constellationMask & LE_GNSS_CONSTELLATION_GLONASS) ? printf("GLONASS activated\n") :
                                                              printf("GLONASS not activated\n");
        (constellationMask & LE_GNSS_CONSTELLATION_BEIDOU)  ? printf("BEIDOU activated\n") :
                                                              printf("BEIDOU not activated\n");
        (constellationMask & LE_GNSS_CONSTELLATION_GALILEO) ? printf("GALILEO activated\n") :
                                                              printf("GALILEO not activated\n");
        (constellationMask & LE_GNSS_CONSTELLATION_SBAS)    ? printf("SBAS activated\n") :
                                                              printf("SBAS not activated\n");
        (constellationMask & LE_GNSS_CONSTELLATION_QZSS)    ? printf("QZSS activated\n") :
                                                              printf("QZSS not activated\n");
    }
    else
    {
        printf("Failed! See log for details!\n");
    }

    int status = (result == LE_OK) ? EXIT_SUCCESS: EXIT_FAILURE;
    return status;
}

{'fraction_non_alphanumeric': 0.24027335559524735, 'fraction_numerical': 0.0031839714219150422, 'mean_word_length': 1.1617005203961726, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6849481', 'n_tokens_mistral': 14231, 'n_tokens_neox': 13620, 'n_words': 3411}