static void get_time()
{
    struct timeval now;
    int sntp_retry_cnt = 0;
    int sntp_retry_time = 0;

    sntp_setoperatingmode(0);
    sntp_setservername(0, "pool.ntp.org");
    sntp_init();

    while (1) {
        for (int32_t i = 0; (i < (SNTP_RECV_TIMEOUT / 100)) && now.tv_sec < 1525952900; i++) {
            vTaskDelay(100 / portTICK_RATE_MS);
            gettimeofday(&now, NULL);
        }

        if (now.tv_sec < 1525952900) {
            sntp_retry_time = SNTP_RECV_TIMEOUT << sntp_retry_cnt;

            if (SNTP_RECV_TIMEOUT << (sntp_retry_cnt + 1) < SNTP_RETRY_TIMEOUT_MAX) {
                sntp_retry_cnt ++;
            }

            printf("SNTP get time failed, retry after %d ms\n", sntp_retry_time);
            vTaskDelay(sntp_retry_time / portTICK_RATE_MS);
        } else {
            printf("SNTP get time success\n");
            break;
        }
    }
}
Ejemplo n.º 2
0
void
sntp_task(void *pvParameters) {

    while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) {
        vTaskDelay(10);
    };

    /*
     * Set one of SNTP_OPMODE_LISTENONLY or SNTP_OPMODE_POLL
     */


    /*
     * SNTP_OPMODE_LISTENONLY
     *     just needs the mode set, no server names required
     *     (requires broadcast NTP on your network)
     */

    sntp_setoperatingmode(SNTP_OPMODE_LISTENONLY);

    /*
     * SNTP_OPMODE_POLL
     *     Needs one or more server names set
     *     additional servers are "fail over"
     *     Can use a DNS name or an address literal
     *     LWIP can also be configured with SNTP_GET_SERVERS_FROM_DHCP
     *     (DHCP-specified SNTP servers untested at this time)
     *
     *  NOTE: Early testing with polling shows higher deviations
     *        than seen with broadcast, even with RTT compensation
     *        Cause unknown at this time, but believed to be within SNTP
     *        amd not part of timekeeping itself.
     */

/*
    sntp_setoperatingmode(SNTP_OPMODE_POLL);
    sntp_setservername(0, "ntp_a.example.com");
    sntp_setservername(1, "ntp_b.example.com");
    sntp_setservername(2, "ntp_c.example.com");
*/

    /* Once set up, this is all it takes */
    sntp_init();

    /*
     * Have high-priority thread "parked", might as well use it
     * Show calling gettimeofday() once an hour to check for timer wrap
     * (the SNTP process itself, if connected, should be sufficient)
     */
    while (1) {
        vTaskDelay(60 * 60 * (1000 / portTICK_PERIOD_MS));
        printf("gettimeofday(NULL, NULL)\n");
        gettimeofday(NULL, NULL);
    }
}
Ejemplo n.º 3
0
static void
tcpip_init_done(void *arg)
{
  sys_sem_t *sem;
  sem = (sys_sem_t *)arg;

  init_netifs();

#if LWIP_IPV4
  netbiosns_set_name("simhost");
  netbiosns_init();
#endif /* LWIP_IPV4 */

  sntp_setoperatingmode(SNTP_OPMODE_POLL);
#if LWIP_DHCP
  sntp_servermode_dhcp(1); /* get SNTP server via DHCP */
#else /* LWIP_DHCP */
#if LWIP_IPV4
  sntp_setserver(0, netif_ip_gw4(&netif));
#endif /* LWIP_IPV4 */
#endif /* LWIP_DHCP */
  sntp_init();

#if LWIP_SNMP
  lwip_privmib_init();
#if SNMP_LWIP_MIB2
#if SNMP_USE_NETCONN
  snmp_threadsync_init(&snmp_mib2_lwip_locks, snmp_mib2_lwip_synchronizer);
#endif /* SNMP_USE_NETCONN */
  snmp_mib2_set_syscontact_readonly((const u8_t*)"root", NULL);
  snmp_mib2_set_syslocation_readonly((const u8_t*)"lwIP development PC", NULL);
  snmp_mib2_set_sysdescr((const u8_t*)"simhost", NULL);
#endif /* SNMP_LWIP_MIB2 */

  snmp_set_mibs(mibs, LWIP_ARRAYSIZE(mibs));
  snmp_init();
#endif /* LWIP_SNMP */
  
  sys_sem_signal(sem);
}
Ejemplo n.º 4
0
static void initialize_sntp(void)
{
    sntp_setoperatingmode(SNTP_OPMODE_POLL);
    sntp_setservername(0, "pool.ntp.org");
    sntp_init();
}