This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/TM4C129ENCPDT: How to use the sntp task multiple times ?

Part Number: TM4C129ENCPDT
Other Parts Discussed in Thread: EK-TM4C1294XL

Tool/software: TI-RTOS

Hello,
I use the EK-TM4C1294XL board with TI-RTOS version 2.16.1.14 with the https example.

In the initialization of the https task, the function startNTP () is called and I receive the current time. Now I want to change the NTP address at runtime and use this new NTP address. But every time I call the startNTP function one more time, the getaddrinfo function returns a -2 and I can not use the new NTP address.


What do I have to do to update the ntp address?

void startNTP(void)
{
    int ret;
    int currPos;
    time_t ts;
    struct sockaddr_in ntpAddr;
    struct addrinfo hints;
    struct addrinfo *addrs;
    struct addrinfo *currAddr;
    Semaphore_Params semParams;


    //Semaphore_pend(semNtpHandle, BIOS_WAIT_FOREVER);

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_DGRAM;



    ret = getaddrinfo(ntp_Hostname, NTP_PORT, NULL, &addrs);
    if (ret != 0) {
        printError("startNTP: NTP host cannot be resolved!", ret);
        return;
    }


    currPos = 0;

    for (currAddr = addrs; currAddr != NULL; currAddr = currAddr->ai_next)
    {
        if (currPos < NTP_SERVERS_SIZE)
        {
            ntpAddr = *(struct sockaddr_in *)(currAddr->ai_addr);
            memcpy(ntpServers + currPos, &ntpAddr, sizeof(struct sockaddr_in));
            currPos += sizeof(struct sockaddr_in);
        }
        else {
            break;
        }
    }

    freeaddrinfo(addrs);

    ret = SNTP_start(Seconds_get, Seconds_set, timeUpdateHook, (struct sockaddr *)&ntpServers, NTP_SERVERS, 0);
    if (ret == 0) {
        printError("startNTP: SNTP cannot be started!", -1);
    }

    Semaphore_Params_init(&semParams);
    semParams.mode = Semaphore_Mode_BINARY;
    semHandle = Semaphore_create(0, &semParams, NULL);
    if (semHandle == NULL) {
        printError("startNTP: Cannot create semaphore!", -1);
    }

    SNTP_forceTimeSync();
    bool timeover = Semaphore_pend(semHandle, 1000);
    // timeover == 0 -> Keine NTP Verbindung
    // timeover == 1 -> Erfolgreiche NTP Verbindung
    ts = time(NULL);
    sysTime = ts;
    System_printf("Current time: %s\n", ctime(&ts));
    System_flush();
}

  • Hi,

    Someone will answer your question on Monday.

    Todd
  • Msark,

    The SNTP module contains a function to update the NTP server list dynamically at run-time: SNTP_setServers(). You should not need call startNTP() again. That function is only needed to start the client task.

    Look in the NDK API reference guide for more details. It is included in the TI-RTOS product install. Start by opening the NDK Release Notes:

    tirtos_tivac_2_16_01_14/products/ndk_2_25_00_09/ndk_2_25_00_09_release_notes.html

    Scroll down to the documentation section and click on the link for the SNTP API Documentation. Then click on the SNTP client module link:

    Documentation
    SNTP API Documentation
    SNTP client

    This page contains useful information, some example code, and some API documentation. Scroll down to the 'Updating The Server List' section.

    The SNTP_setServers() function takes a pointer to an array of objects and a count of how many items are in the array. Scroll back a couple of sections to see how to setup this array of objects.

    Initializing The List Of Servers Using Socket Address Structures
    Storing The Server List In A Contiguous Block Of Memory

    The example gives details on using both IPv4 and IPv6 addresses. If you are using only IPv4 address, it is not so complicated. For example, if you want to specify only one IPv4 address, use the following code. Note the SNTP port is indeed 123, but the IP address below is only an example; replace it with the actual IP address for your NTP server.

    #include <sys/socket.h>

    struct sockaddr_in ipv4addr[1];

    ipv4addr[0].sin_family = AF_INET;
    ipv4addr[0].sin_port = htons(123);
    inet_pton(AF_INET, "192.168.1.100", &(ipv4addr[0].sin_addr.s_addr));

    SNTP_setServers((struct sockaddr *)ipv4addr, 1);

    You must define your array as type 'struct sockaddr_in', but the function expects a pointer to the generic 'struct sockaddr' type. This is why you must use the type-cast in the function call.

    This function will replace the current list of NTP servers with the new list. The old addresses will be discarded.

    If you know the IP address of your NTP server, this is all you need. However, if you only have the hostname of the NTP server, you will need to use getaddrinfo() to retrieve the IP address. You reported this function was failing with a -2 error code. There are several points of failure which return this code. Look in your CCS Console or in ROV System Output buffer for additional debug messages. Let us know what you find.

    ~Ramsey