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.

TM4C1294NCPDT: How to call startNTP() function

Part Number: TM4C1294NCPDT

Dear TI community,

I was reading through the code of the "httpsgetCC3100_EK_TM4C1294XL_TI " in which the SNTP protocol have been used , and there a task has been created to run the http client which is as follow:

/*
 *  ======== httpsTask ========
 */
Void httpsTask(UArg arg0, UArg arg1)
{
    void *netIF;

    /* Open WiFi and await a connection */
    netIF = socketsStartUp();

    httpsFxn();

    /* Close the network - don't do this if other tasks are using it */
    socketsShutDown(netIF);
}

Now in the httpsFxn() the function startNTP() has been called which in return after all the NTP server initialisation call the SNTP library function SNTP_start.

So I am wondering since the SNTP_start function is inside a thread but the thread is not having any infinite while loop so is this http client is going to run only once or it is using some internal task or thread to run continuously. 

Also I want to know that do the SNTP_start function need to be called once in any thread or task or again and again after a fixed interval of time.

regards

  • Hi Piyush,

    The SNTP_start() API creates a task that communicates with the NTP server. You don't need to do anything else. If you look in ROV, you'll see the task. The task function name is synctime() and runs at a low priority (1).

    Todd
  • Hi Todd,

    Also I want to tell you that I am facing a warning from the compiler that function inet_pton() is implicitly defined , as all we know that the definition of the function is available in the inetaddr.c file and I have included the proper path to the header file in which the function prototype is defined in the include section of the compiler section of the properties of project.

    Here is the code snippet of it:

        ipv4addr.sin_family = AF_INET;
        ipv4addr.sin_port = htons(123);
    
        inet_pton(AF_INET, "216.239.35.4", &(ipv4addr.sin_addr.s_addr));        // google NTP server 2

    What could be the possible reason for this.

    Also is there any specific port for the sntp like http , ftp protocols or I can use any port for this.

    piyush

  • Hi Todd and other TI community members,

    I have one more query.

    Actually I have implemented the sntp code after reading and going through all the NDK docs and also searching a lot on this forum and reading the relevant contents and now I am confident that the code which I have implemented must work, but unfortunately it is not working as it is supposed to be.

    See I have implemented two functions and provided the list of servers as said in the doc. I implemented the get function to get the time of my system and also the set function , here below I am attaching the snippets of both. Actually I implemented nothing but modified it a little bit from the reference example of TI.

    void setTime(uint32_t t)
    {
        struct tm tm;
        time_t ts;
    
        Seconds_set(t);
    
        time(&ts);
        tm = *localtime(&ts);
    }

    Gettime code snippet:

    uint32_t getTime(void)
    {
        return (Seconds_get());
    }

    rest part is just adding of the servers to the list inside my sntp init function and calling of the SNTP_start() inside the same function after setting all the necessary environment.

    The fact that I am confident of it that it will work is that there is nothing complexity about it, the sntp client must call the set and get function as mentioned in the doc after the SNTP time out period mentioned in the doc.

    But it is not happening as I supposed.

    Also the possibility of NTP servers not replying can't be ignored but I have added 5 servers to the list as follow:

        inet_pton(AF_INET, "216.239.35.4", &(ipv4addr.sin_addr.s_addr));        // google NTP server 2
    
        //unsigned char ntpServers[SIZE];
        //int currPos = 0;
    
        memcpy((ntpServers + currPos), &ipv4addr, sizeof(struct sockaddr_in));
        currPos += sizeof(struct sockaddr_in);
    
        inet_pton(AF_INET, "132.163.96.2", &(ipv4addr.sin_addr.s_addr));
    
        memcpy((ntpServers + currPos), &ipv4addr, sizeof(struct sockaddr_in));
        currPos += sizeof(struct sockaddr_in);
    
        inet_pton(AF_INET, "128.138.140.44", &(ipv4addr.sin_addr.s_addr));
    
        memcpy((ntpServers + currPos), &ipv4addr, sizeof(struct sockaddr_in));
        currPos += sizeof(struct sockaddr_in);
    
        inet_pton(AF_INET, "132.163.96.4", &(ipv4addr.sin_addr.s_addr));
    
        memcpy((ntpServers + currPos), &ipv4addr, sizeof(struct sockaddr_in));
        currPos += sizeof(struct sockaddr_in);
    
        inet_pton(AF_INET, "216.239.35.0", &(ipv4addr.sin_addr.s_addr));
    
        memcpy((ntpServers + currPos), &ipv4addr, sizeof(struct sockaddr_in));
        currPos += sizeof(struct sockaddr_in);
    

    So this chance is also very little.

    So todd now you tell me buddy how to resolve this issue and make it a proper running sntp client on my board.

    with warm regards

    piyush pandey

  • Piyush,

    Can you start new thread regarding hte inet_pton() question? It makes the thread too messy trying to answer too many questions in a single thread.

    Can you confirm what is coming out/going into the TM4C by looking at WireShark? The whole currPos looks like a bug waiting to happen. Can you just make an array instead and then index accordingly?

    Todd
  • ToddMullanix said:


    The whole currPos looks like a bug waiting to happen. Can you just make an array instead and then index accordingly?

    why the currPos is looking a bug to you.

    Here is how I implemented the addition of multiple servers in code.

    #define NTP_SERVERS       5
    #define NTP_SERVERS_SIZE  (NTP_SERVERS * sizeof(struct sockaddr_in))
    
    
    char ntpServers[NTP_SERVERS_SIZE];
    
    
    void startNTP(void)
    {
    
        int currPos = 0;
    
    
        ipv4addr.sin_family = AF_INET;
        ipv4addr.sin_port = htons(123);
    
        //------------------- Addition of NTP Server Address  1 ------------------------------------------------------
    
    
        inet_pton(AF_INET, "216.239.35.4", &(ipv4addr.sin_addr.s_addr));        // google NTP server 2
    
        memcpy((ntpServers + currPos), &ipv4addr, sizeof(struct sockaddr_in));
        currPos += sizeof(struct sockaddr_in);
    
    
        //------------------- Addition of NTP Server Address  2 ------------------------------------------------------
    
    
        inet_pton(AF_INET, "132.163.96.2", &(ipv4addr.sin_addr.s_addr));
    
        memcpy((ntpServers + currPos), &ipv4addr, sizeof(struct sockaddr_in));
        currPos += sizeof(struct sockaddr_in);
    
    
        //------------------- Addition of NTP Server Address  3 ------------------------------------------------------
    
    
        inet_pton(AF_INET, "128.138.140.44", &(ipv4addr.sin_addr.s_addr));
    
        memcpy((ntpServers + currPos), &ipv4addr, sizeof(struct sockaddr_in));
        currPos += sizeof(struct sockaddr_in);
    
    
        //------------------- Addition of NTP Server Address  4 ------------------------------------------------------
    
    
        inet_pton(AF_INET, "132.163.96.4", &(ipv4addr.sin_addr.s_addr));
    
        memcpy((ntpServers + currPos), &ipv4addr, sizeof(struct sockaddr_in));
        currPos += sizeof(struct sockaddr_in);
    
    
        //------------------- Addition of NTP Server Address  5 ------------------------------------------------------
    
    
        inet_pton(AF_INET, "216.239.35.0", &(ipv4addr.sin_addr.s_addr));
    
        memcpy((ntpServers + currPos), &ipv4addr, sizeof(struct sockaddr_in));
        currPos += sizeof(struct sockaddr_in);
    
    
    
        ------------------
        ------------------
    
    }

    Here as you can see I am storing the server addresses in the contiguous manner in the memory and using curPos as offset to it  , also I have already allocated the memory for the 5 servers addresses as you can see above.

    Now tell me if there is something wrong it.

    I know its a little bit messy to use clone of the same thing but I am testing and debugging right now so not focussing on making it more readable. 

    Meanwhile I will try to make it more systematic and going to try on wireshark if some packet is transfer appear there.

    But todd, I want to ask you one thing about the  wireshark  and that is wireshark captures the communication packet between my PC and the Tiva board and how can it can capture the communication packets between Tiva board and some remote NTP server.

    I mean my PC is not acting as NTP server to the Tiva board as you can see all the above ip addresses are legitimate ip addresses of NTP servers from various regions of Europe and America.

  • You can use a Ethernet hub to connect both your PC and TM4C to. That way, your PC sees every going to or from the TM4C. Another option is to add your PC into the NTP list just to see if anything is coming from the TM4C. Try having your PC first in the list and then last. I realize you don't have a NTP on your PC, but the TM4C should still try to communicate to it.

    Note: I don't have a board available for testing (I'm out of the office for a couple days) so I cannot try and reproduce the problem.

    Note: are you calling SNTP_setservers?

  • Hi Todd,

    As you said I follow the steps of debugging the project via the wireshark.

    I have saved the wireshark files which are 43 MB and 38 MB in sizes so I uploaded them to my file sharing site and now I am sharing it with you here. I also have attached a link to the screenshot of the NTP query send by the tiva board to my PC , the ip address of my tiva board is 192.168.80.154 and of my PC is 192.168.80.171.

    Following things were noticed during the debug session:

    1 -- The tiva board query the NTP packet only once in the entire wireshark capture and not multiple times as mentioned in the sntp document that after the sntp timeout period it will again ping the ntp server and so on. The wireshark captures are of around 1 hour , so it is supposed to have atleast 2 SNTP query packets from the tiva side.

    2 -- I am not getting this point that when I start the capture session of the wireshark and also start the debug session of the tiva board than I wait for the NTP query packet from the tiva side but did not get anything captured in the wireshark window until I also start pinging the tiva board and as soon as I ping the tiva board its packets start appearing in the window of the wireshark. So what exactly is this problem, can you please explain it.

    Here are the links to the files of captures and the screenshots:

    1drv.ms/.../s!AiYsdn8Q78GdqhzQc_smPJopwzN-


    1drv.ms/.../s!AiYsdn8Q78Gdqhs2XNq-rDguhWjC


    Also I want to add that now I am not pinging any ntp server , I have just put one ip address in the ntp server address and that is of my PC i.e. 192.168.80.171 so that I can see that atleast tiva board is querying the NTP server but after one response there is nothing, as mentioned above and captured in the wireshark output.

    regards

    Piyush Pandey

      

  • Hi Piyush,

    The code you have to copy the socket address structures of the servers into the ntpServers array should be fine. Note that the example code was written using memcpy() instead of an array because the server list can hold both IPv6 and IPv4 servers, so an array wouldn't work in that generic case. But, if you are only using IPv4 servers in your list, then you could simplify ntpServers as an array of sockaddr_in structs, as Todd suggested. But, if everything's working as is, it's probably best to save this kind of change for the clean up step (as you were already thinking).

    Piyush Pandey22 said:
    Also is there any specific port for the sntp like http , ftp protocols or I can use any port for this.

    NTP servers use port 123. So, an NTP client must specify 123 for the port of the destination server. Your local port should be any standard user port number (within the valid range).

    Piyush Pandey22 said:
    I have saved the wireshark files which are 43 MB and 38 MB in sizes so I uploaded them to my file sharing site and now I am sharing it with you here. I also have attached a link to the screenshot of the NTP query send by the tiva board to my PC

    I'm not able to access these files due to our security policies. Were you not able to attach the files due to their large size?

    You shouldn't hit any size limits regarding the screen shot. Can you please post the screen shot (2018-04-30 12_04_52-ntp_debug_01.png) to the thread?

    Piyush Pandey22 said:
    Also I want to add that now I am not pinging any ntp server , I have just put one ip address in the ntp server address and that is of my PC i.e. 192.168.80.171 so that I can see that atleast tiva board is querying the NTP server but after one response there is nothing, as mentioned above and captured in the wireshark output.

    From this, it sounds like you are able to see the NTP request being sent from the Tiva board to your PC. Is that correct? If so, could you please take a Wireshark capture of this small scenario and then upload it here? (a small capture like this should not result in a very large file size).

    Another thing to try would be to change your servers list to have multiple servers that are all the same server. To clarify, I mean update the server list to contain your PC's IP address and repeat that, say, 3 times.

    The expected outcome is that you would see the Tiva board send out n different NTP requests (in this example, n = 3 so I would expect you to see 3 different NTP packets). I'm not sure which version of TIRTOS you're using but I could guess that there may be a delay in between transfers (so you might want to give it several minutes to run).

    Steve