SNTP lwip

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all. I tried to sync my device using the SNTP client lwip 2.02.http://download.savannah.nongnu.org/releases/lwip/lwip-2.0.2.zip

SNTP server address: 192.168.0.101

SNTP client address: 192.168.0.100

In function sntp_init I had to make some changes:

void sntp_init(void)
{
#ifdef SNTP_SERVER_ADDRESS
#if SNTP_SERVER_DNS
  sntp_setservername(0, SNTP_SERVER_ADDRESS);
#else
#error SNTP_SERVER_ADDRESS string not supported SNTP_SERVER_DNS==0
#endif
#else
 struct ip4_addr sntp_ip_addr;
 IP4_ADDR(&sntp_ip_addr, SNTP_CONF_IPADDR0, SNTP_CONF_IPADDR1,
 SNTP_CONF_IPADDR2, SNTP_CONF_IPADDR3);
 sntp_setserver(0, &sntp_ip_addr);
#endif /* SNTP_SERVER_ADDRESS */
  sntp_setoperatingmode(SNTP_OPMODE_POLL);

  if (sntp_pcb == NULL) {
    sntp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
    LWIP_ASSERT("Failed to allocate udp pcb for sntp client", sntp_pcb != NULL);
    if (sntp_pcb != NULL) {
  
   sntp_pcb->local_port=SNTP_PORT;
   sntp_pcb->remote_port=SNTP_PORT;  
      udp_recv(sntp_pcb, sntp_recv, NULL);

      if (sntp_opmode == SNTP_OPMODE_POLL) {
        SNTP_RESET_RETRY_TIMEOUT();
#if SNTP_STARTUP_DELAY
        sys_timeout((u32_t)SNTP_STARTUP_DELAY_FUNC, sntp_request, NULL);
#else
        sntp_request(NULL);
#endif
      } else if (sntp_opmode == SNTP_OPMODE_LISTENONLY) {
        ip_set_option(sntp_pcb, SOF_BROADCAST);
        //udp_bind(sntp_pcb, IP_ANY_TYPE, SNTP_PORT);
  udp_bind(sntp_pcb, &sntp_ip_addr, SNTP_PORT);
      }
    }
  }
}

The debugger displays the following message:


SNTP_OPMODE_POOL
sys_timeout: 0x20016e50 msecs=1000 handler=cyclic_timer arg=0x436d34
sys_timeout: 0x20016e3c msecs=1000 handler=cyclic_timer arg=0x436d40
sys_timeout: 0x20016e28 msecs=100 handler=cyclic_timer arg=0x436d4c
sntp_request: current server address is 192.168.0.101
sntp_send_request: Sending request to server
udp_send
udp_send: added header in given pbuf 0x2000f744
udp_send: sending datagram of length 56
udp_send: UDP packet length 56
udp_send: UDP checksum 0x6e59
udp_send: ip_output_if (,,,,0x11,)
sys_timeout: 0x20016e14 msecs=3000 handler=sntp_try_next_server arg=0x0
udp_input: received datagram of length 56
UDP header:
+-------------------------------+
|       123     |       123     | (src port, dest port)
+-------------------------------+
|        56     |     0xc3e5    | (len, chksum)
+-------------------------------+
udp (192.168.0.100, 123) <-- (192.168.0.101, 123)
udp_input: calculating checksum
udp_input: not for us.
udp_input: received datagram of length 58
UDP header:
+-------------------------------+
|       137     |       137     | (src port, dest port)
+-------------------------------+
|        58     |     0xe6e8    | (len, chksum)
+-------------------------------+
udp (192.168.0.255, 137) <-- (192.168.0.101, 137)
udp_input: received datagram of length 58
UDP header:
+-------------------------------+
|       137     |       137     | (src port, dest port)
+-------------------------------+
|        58     |     0xe6e4    | (len, chksum)
+-------------------------------+
udp (192.168.0.255, 137) <-- (192.168.0.101, 137)
udp_input: received datagram of length 58
UDP header:
+-------------------------------+
|       137     |       137     | (src port, dest port)
+-------------------------------+
|        58     |     0xe6e4    | (len, chksum)
+-------------------------------+
udp (192.168.0.255, 137) <-- (192.168.0.101, 137)
udp_input: received datagram of length 58


The message "udp_input: not for us." means that pcb was not found, but the pcb was created.

What did I do wrong?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I understand, what was the error: in function sntp_init no call udp_bind and udp_connect functions.

static struct ip4_addr sntpserver_ip_addr;
static struct ip4_addr sntpclient_ip_addr;
//--------------------------------------------------------------------------------------------------------------
void sntp_init(void)
{
#ifdef SNTP_SERVER_ADDRESS
#if SNTP_SERVER_DNS
  sntp_setservername(0, SNTP_SERVER_ADDRESS);
#else
#error SNTP_SERVER_ADDRESS string not supported SNTP_SERVER_DNS==0
#endif
#else
 
 IP4_ADDR(&sntpserver_ip_addr, SNTP_CONF_IPADDR0, SNTP_CONF_IPADDR1, SNTP_CONF_IPADDR2, SNTP_CONF_IPADDR3);
 sntp_setserver(0, &sntpserver_ip_addr);
#endif /* SNTP_SERVER_ADDRESS */
 IP4_ADDR(&sntpclient_ip_addr, ETHERNET_CONF_IPADDR0, ETHERNET_CONF_IPADDR1, ETHERNET_CONF_IPADDR2, ETHERNET_CONF_IPADDR3);
  sntp_setoperatingmode(SNTP_OPMODE_POLL);

  if (sntp_pcb == NULL) {
    //sntp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
 sntp_pcb = udp_new_ip_type(IPADDR_TYPE_V4);
    LWIP_ASSERT("Failed to allocate udp pcb for sntp client", sntp_pcb != NULL);
    if (sntp_pcb != NULL) {
 
      udp_recv(sntp_pcb, sntp_recv, NULL);
      udp_bind(sntp_pcb, &sntpclient_ip_addr, SNTP_PORT);
      udp_connect(sntp_pcb, &sntpserver_ip_addr, SNTP_PORT);

      if (sntp_opmode == SNTP_OPMODE_POLL) {
        SNTP_RESET_RETRY_TIMEOUT();
#if SNTP_STARTUP_DELAY
        sys_timeout((u32_t)SNTP_STARTUP_DELAY_FUNC, sntp_request, NULL);
#else
        sntp_request(NULL);
#endif
      } else if (sntp_opmode == SNTP_OPMODE_LISTENONLY) {
        ip_set_option(sntp_pcb, SOF_BROADCAST);
        udp_bind(sntp_pcb, IP_ANY_TYPE, SNTP_PORT);
        udp_connect(sntp_pcb, &sntpserver_ip_addr, SNTP_PORT);
      }
    }
  }
}