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/OMAP-L138: Problem with SNTP

Part Number: OMAP-L138
Other Parts Discussed in Thread: OMAPL138,

Tool/software: TI-RTOS

I have a problem with SNTP. I have configured EMAC in C code using the CfgAddEntry function. This working good, because I using FTP and I don't have any problems.

My code:

#define SNTP_PORT 123
    int32_t  retVal;
    uint32_t seconds;
    uint32_t secondsFraction;

    SlNetSock_Timeval_t timeval;
    uint64_t ntpTimeStamp = 0;
    SlNetSock_AddrIn_t ipv4addr;

    ipv4addr.sin_family = SLNETSOCK_AF_INET;
    ipv4addr.sin_port = SlNetUtil_htons(SNTP_PORT);
    ipv4addr.sin_addr.s_addr = SlNetUtil_htonl(3639550724);
    //ipv4addr.sin_addr.s_addr = inet_addr("216.239.35.4");//time.google.com
    timeval.tv_sec  = 5;
    timeval.tv_usec = 0;
    retVal = SNTP_getTimeByAddr((SlNetSock_Addr_t *)&ipv4addr, &timeval,
            &ntpTimeStamp);

SNTP_getTimeByAddr function return (-102) -> SNTP_ESOCKCREATEFAIL (Failed to create a socket)I noticed that the problem is when function "Create a UDP socket to communicate with NTP server".

I added SlNetSock_init(0) before SNTP_getTimeByAddr() and now SNTP_getTimeByAddr return (-105) -> SNTP_ECONNECTFAIL (Failed to connect to the NTP server).Additionally I observed that SlNetSock_getVirtualSdConf() in SlNetSock_connect() return -2023L

Where is the problem?

I'm using: 

-> pdk_omapl138_1_0_6

-> ndk_3_40_01_01

-> ns_2_40_01_02

Regards,

Patryk

  • Hi Patryk,

    We'll need some time to look into this and get back to you.

    Regards,
    Sahin
  • Thanks Sahin,

    I'm waiting. But I tried to add:

    int32_t SlNetIf_add (uint16_t ifID, char *ifName, const SlNetIf_Config_t *ifConf, uint8_t priority)

    I have a problem with SlNetIf_Config_t stucture. Should I write all the functions to this structure? Don't have ready solutions or examples? What should I do? 
    Am I trying to solve this problem well?

    Regards,
    Patryk

  • Patryk,

    It looks that your are implementing a SNTP client and want to get the timestamp from a SNTP server over network. In the older NDK 2.26.0.8, the header file sntp.h and helper file sntp_8h.html had good explanations of the API call flow and pre-requisite. It also mentioned that "SNTP is now deprecated from the NDK, The SNTP module will soon be removed from the NDK and made available in a separate product."

    In the ndk_3_40_01_01, SNTP is split into ns_2_40_01_02. The functions used before SNTP_start() and SNTP_stop() were gone. Only the SNTP_getTime() functions are there, I am not sure how the service get started. I am looking if we have any migration guide for the SNTP and will update here. Sorry for the late response!

    Regards, Eric
  • Thanks Eric for your response.

    I still haven't solved this problem. I would be very grateful if you could find a migration guide. Otherwise, the SNTP is useless for me.

    Regards, Patryk

    Thanks for your response

  • Patryk,

    The issue looks like a case of not initializing the NS stack. Once NS is initialized correctly, SNTP_getTime() should work.  The Processor SDK RTOS for L138 doesn't have example code for NS. We have such example for another processor MSP 432, the network code should be the same. You can refer to the attached code on your OMAP-L138 project.

    /*
     * Copyright (c) 2018-2019, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    /*
     *    ======== httpsgethooks.c ========
     */
    
    #include <stdlib.h>
    #include <stdint.h>
    #include <stdbool.h>
    
    #include <pthread.h>
    
    #include <ti/ndk/inc/netmain.h>
    
    #include <ti/ndk/slnetif/slnetifndk.h>
    #include <ti/net/slnetif.h>
    #include <ti/net/slnetutils.h>
    
    #include <ti/display/Display.h>
    #include <ti/drivers/emac/EMACMSP432E4.h>
    
    
    #define HTTPSTASKSTACKSIZE 8192
    #define IFPRI  4   /* Ethernet interface priority */
    
    /* Prototypes */
    extern Display_Handle display;
    extern void *httpsTask(void *arg0);
    
    /*
     *  ======== netIPAddrHook ========
     *  user defined network IP address hook
     */
    void netIPAddrHook(uint32_t IPAddr, unsigned int IfIdx, unsigned int fAdd)
    {
        pthread_t          thread;
        pthread_attr_t     attrs;
        struct sched_param priParam;
        int                retc;
        int                detachState;
        uint32_t           hostByteAddr;
        static bool createTask = true;
        int32_t status = 0;
    
        if (fAdd) {
            Display_printf(display, 0, 0, "Network Added: ");
        }
        else {
            Display_printf(display, 0, 0, "Network Removed: ");
        }
    
        /* print the IP address that was added/removed */
        hostByteAddr = NDK_ntohl(IPAddr);
        Display_printf(display, 0, 0, "If-%d:%d.%d.%d.%d\n", IfIdx,
                (uint8_t)(hostByteAddr>>24)&0xFF, (uint8_t)(hostByteAddr>>16)&0xFF,
                (uint8_t)(hostByteAddr>>8)&0xFF, (uint8_t)hostByteAddr&0xFF);
    
        status = SlNetSock_init(0);
        if (status != 0) {
            Display_printf(display, 0, 0, "SlNetSock_init fail (%d)\n",
                status);
        }
    
        status = SlNetIf_init(0);
        if (status != 0) {
            Display_printf(display, 0, 0, "SlNetIf_init fail (%d)\n",
                status);
        }
    
        status = SlNetUtil_init(0);
        if (status != 0) {
            Display_printf(display, 0, 0, "SlNetUtil_init fail (%d)\n",
                status);
        }
    
        status = SlNetIf_add(SLNETIF_ID_2, EMACMSP432E4_ETHERNET_NAME,
                (const SlNetIf_Config_t *)&SlNetIfConfigNDKSec, IFPRI);
        if (status != 0) {
            Display_printf(display, 0, 0, "SlNetIf_add fail (%d)\n",
                status);
        }
    
        if (fAdd && createTask) {
            /*
             *  Create the Task that farms out incoming TCP connections.
             *  arg0 will be the port that this task listens to.
             */
    
            /* Set priority and stack size attributes */
            pthread_attr_init(&attrs);
            priParam.sched_priority = 1;
    
            detachState = PTHREAD_CREATE_DETACHED;
            retc = pthread_attr_setdetachstate(&attrs, detachState);
            if (retc != 0) {
                Display_printf(display, 0, 0,
                        "netIPAddrHook: pthread_attr_setdetachstate() failed\n");
                while (1);
            }
    
            pthread_attr_setschedparam(&attrs, &priParam);
    
            retc |= pthread_attr_setstacksize(&attrs, HTTPSTASKSTACKSIZE);
            if (retc != 0) {
                Display_printf(display, 0, 0,
                        "netIPAddrHook: pthread_attr_setstacksize() failed\n");
                while (1);
            }
    
            retc = pthread_create(&thread, &attrs, httpsTask, 0);
            if (retc != 0) {
                Display_printf(display, 0, 0,
                        "netIPAddrHook: pthread_create() failed\n");
                while (1);
            }
    
            createTask = false;
        }
    }
    
    /*
     *  ======== serviceReportHook ========
     *  NDK service report hook
     */
    void serviceReportHook(uint32_t item, uint32_t status, uint32_t report, void *h)
    {
        static char *taskName[] = {"Telnet", "HTTP", "NAT", "DHCPS", "DHCPC", "DNS"};
        static char *reportStr[] = {"", "Running", "Updated", "Complete", "Fault"};
        static char *statusStr[] =
            {"Disabled", "Waiting", "IPTerm", "Failed","Enabled"};
    
        Display_printf(display, 0, 0, "Service Status: %-9s: %-9s: %-9s: %03d\n",
                taskName[item - 1], statusStr[status], reportStr[report / 256],
                report & 0xFF);
    }
    
    /*
     *  ======== netOpenHook ========
     *  NDK network open hook
     */
    void netOpenHook()
    {
    }
    

    /*
     * Copyright (c) 2018, Texas Instruments Incorporated
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * *  Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * *  Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * *  Neither the name of Texas Instruments Incorporated nor the names of
     *    its contributors may be used to endorse or promote products derived
     *    from this software without specific prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     */
    
    #include <time.h>
    #include <unistd.h>
    
    #include <ti/display/Display.h>
    #include <ti/net/sntp/sntp.h>
    
    /*
     * Difference between NTP Epoch (seconds since January 1, 1900 GMT) and UNIX
     * Epoch (seconds since January 1, 1970 GMT)
     */
    #define TIME_BASEDIFF ((((uint32_t)70 * 365 + 17) * 24 * 3600))
    
    #define TIME_NTP_TO_UNIX(t) ((t) - TIME_BASEDIFF)
    
    #define NTP_SERVERS 1
    #define NTP_SERVER_PORT 123
    
    /*  Time to wait for reply from server (seconds) */
    #define NTP_REPLY_WAIT_TIME 5
    
    /* Must wait at least 15 sec to retry NTP server (RFC 4330) */
    #define NTP_POLL_TIME 15
    
    extern Display_Handle display;
    
    /*
     *  ======== startSNTP ========
     */
    void startSNTP(void)
    {
        uint64_t ntpTimeStamp;
        uint32_t currentTimeNtp = 0;
        uint32_t currentTimeUnix = 0;
        int32_t retval;
        time_t ts;
        SlNetSock_Timeval_t timeval;
        struct timespec tspec;
    
        /* Set timeout value for NTP server reply */
        timeval.tv_sec = NTP_REPLY_WAIT_TIME;
        timeval.tv_usec = 0;
    
        do {
            /* Get the time using the built in NTP server list: */
            retval = SNTP_getTime(NULL, 0, &timeval, &ntpTimeStamp);
            if (retval != 0) {
                Display_printf(display, 0, 0,
                    "startSNTP: couldn't get time (%d), will retry in %d secs ...",
                    retval, NTP_POLL_TIME);
                sleep(NTP_POLL_TIME);
                Display_printf(display, 0, 0, "startSNTP: retrying ...");
            }
    
            /* Save the current (NTP Epoch based) time */
            currentTimeNtp = ntpTimeStamp >> 32;
    
        } while (retval < 0);
    
         /* Set the time. Always pass a time value based on the UNIX Epoch */
        currentTimeUnix = TIME_NTP_TO_UNIX(currentTimeNtp);
        tspec.tv_nsec = 0;
        tspec.tv_sec = currentTimeUnix;
        if (clock_settime(CLOCK_REALTIME, &tspec) != 0) {
            Display_printf(display, 0, 0,
                    "startSNTP: Failed to set current time\n");
            while(1);
        }
    
        /*
         * Use time.h APIs to get the time and display it on the console.
         * Must call time.h APIs using the appropriate (toolchain dependent) Epoch
         * time base
         */
    #if defined(__TI_COMPILER_VERSION__)
        /* For the TI toolchain, time APIs expect times based on the NTP Epoch */
        ts = currentTimeNtp;
    #else
        /* Time APIs for GCC and IAR expect times based on the UNIX Epoch */
        ts = currentTimeUnix;
    #endif
        Display_printf(display, 0, 0,
                "startSNTP: Current time: %s\n", ctime(&ts));
    }
    

    Regards, Eric  

  • Thanks Eric for support,

    Could you share me file with stucture "SlNetIfConfigNDKSec" which is used in httpsgethooks.c file?

    status = SlNetIf_add(SLNETIF_ID_2, EMACMSP432E4_ETHERNET_NAME,
                (const SlNetIf_Config_t *)&SlNetIfConfigNDKSec, IFPRI);

    PS: I downloaded the SDK from the  MSP 432 processor and I have this stucture.

    Regards, Patryk

  • slnetifndk.cHi,

    I attached the file.

    Regards, Eric