Esempio n. 1
0
/**
 * Initialize this module.
 * Send out request instantly or after SNTP_STARTUP_DELAY(_FUNC).
 */
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
#endif /* SNTP_SERVER_ADDRESS */

  if (sntp_pcb == NULL) {
    SNTP_RESET_RETRY_TIMEOUT();
    sntp_pcb = udp_new();
    LWIP_ASSERT("Failed to allocate udp pcb for sntp client", sntp_pcb != NULL);
    if (sntp_pcb != NULL) {
      udp_recv(sntp_pcb, sntp_recv, NULL);
#if SNTP_STARTUP_DELAY
      sys_timeout((u32_t)SNTP_STARTUP_DELAY_FUNC, sntp_request, NULL);
#else
      sntp_request(NULL);
#endif
    }
  }
}
Esempio n. 2
0
/**
 * If Kiss-of-Death is received (or another packet parsing error),
 * try the next server or retry the current server and increase the retry
 * timeout if only one server is available.
 * (implicitly, SNTP_MAX_SERVERS > 1)
 *
 * @param arg is unused (only necessary to conform to sys_timeout)
 */
static void
sntp_try_next_server(void* arg)
{
  u8_t old_server, i;
  LWIP_UNUSED_ARG(arg);

  old_server = sntp_current_server;
  for (i = 0; i < SNTP_MAX_SERVERS - 1; i++) {
    sntp_current_server++;
    if (sntp_current_server >= SNTP_MAX_SERVERS) {
      sntp_current_server = 0;
    }
    if (!ip_addr_isany(&sntp_servers[sntp_current_server].addr)
#if SNTP_SERVER_DNS
        || (sntp_servers[sntp_current_server].name != NULL)
#endif
        ) {
      LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_try_next_server: Sending request to server %"U16_F"\n",
        (u16_t)sntp_current_server));
      /* new server: reset retry timeout */
      SNTP_RESET_RETRY_TIMEOUT();
      /* instantly send a request to the next server */
      sntp_request(NULL);
      return;
    }
  }
  /* no other valid server found */
  sntp_current_server = old_server;
  sntp_retry(NULL);
}
Esempio n. 3
0
/**
 * If Kiss-of-Death is received (or another packet parsing error),
 * try the next server or retry the current server and increase the retry
 * timeout if only one server is available.
 *
 * @param arg is unused (only necessary to conform to sys_timeout)
 */
static void
sntp_try_next_server(void* arg)
{
  LWIP_UNUSED_ARG(arg);

  /*os_sprintf(deb,"Trying next server\r\n");
   uart0_sendStr(deb);*/

  if (sntp_num_servers > 1) {
    /* new server: reset retry timeout */
    SNTP_RESET_RETRY_TIMEOUT();
    sntp_current_server++;
    if (sntp_current_server >= sntp_num_servers) {
      sntp_current_server = 0;
    }
    LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_try_next_server: Sending request to server %"U16_F"\n",
      (u16_t)sntp_current_server));
     os_sprintf(deb,"sntp_try_next_server: Sending request to server %"U16_F"\n",
      (u16_t)sntp_current_server);

     //uart0_sendStr(deb);
    /* instantly send a request to the next server */
    sntp_request(NULL);
  } else {
/*    os_sprintf(deb,"Retrying null\n");
    uart0_sendStr(deb);*/
    sntp_retry(NULL);
  }
}
Esempio n. 4
0
/**
 * Initialize this module when using raw API.
 * Send out request instantly or after SNTP_STARTUP_DELAY.
 */
void sntp_init(void) {
    SNTP_RESET_RETRY_TIMEOUT();
    sntp_pcb = udp_new();
    LWIP_ASSERT("Failed to allocate udp pcb for sntp client", sntp_pcb != NULL);

    if (sntp_pcb != NULL) {
        udp_recv(sntp_pcb, sntp_recv, NULL);
#if SNTP_STARTUP_DELAY
        sys_timeout((u32_t)SNTP_STARTUP_DELAY, sntp_request, NULL);
#else
        sntp_request(NULL);
#endif
    }
}
Esempio n. 5
0
/**
 * If Kiss-of-Death is received (or another packet parsing error),
 * try the next server or retry the current server and increase the retry
 * timeout if only one server is available.
 *
 * @param arg is unused (only necessary to conform to sys_timeout)
 */
static void sntp_try_next_server(void* arg) {
    LWIP_UNUSED_ARG(arg);

    if (sntp_num_servers > 1) {
        /* New server: reset retry timeout */
        SNTP_RESET_RETRY_TIMEOUT();

        sntp_current_server++;
        if (sntp_current_server >= sntp_num_servers) {
            sntp_current_server = 0;
        }
        LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_try_next_server: Sending request to server %"U16_F"\n", (u16_t)sntp_current_server));

        /* Instantly send a request to the next server */
        sntp_request(NULL);
    } else {
        sntp_retry(NULL);
    }
}
Esempio n. 6
0
/**
 * Initialize this module.
 * Send out request instantly or after SNTP_STARTUP_DELAY.
 */
void
sntp_init(int tz)
{
  LWIP_DEBUGF(SNTP_DEBUG_WARN_STATE,"Sntp initializing TZ: GMT%s%02d...\n",tz > 0 ? "+" : "",tz);
  ets_uart_printf("Sntp initializing...\n");
  sntp_tz = tz;
  if (sntp_pcb == NULL) {
    os_timer_setfn(&ntp_timer,ntp_time_update,NULL);
    SNTP_RESET_RETRY_TIMEOUT();
    sntp_pcb = udp_new();
    LWIP_ASSERT("Failed to allocate udp pcb for sntp client", sntp_pcb != NULL);
    if (sntp_pcb != NULL) {
      udp_recv(sntp_pcb, sntp_recv, NULL);
#if SNTP_STARTUP_DELAY
      sys_timeout((u32_t)SNTP_STARTUP_DELAY, sntp_request, NULL);
#else
      sntp_request(NULL);
#endif
    }
  }
}
Esempio n. 7
0
/** UDP receive callback for the sntp pcb */
static void sntp_recv(void *arg, struct udp_pcb* pcb, struct pbuf *p, ip_addr_t *addr, u16_t port) {
    u8_t mode;
    u8_t stratum;
    u32_t receive_timestamp[SNTP_RECEIVE_TIME_SIZE];
    err_t err;

    LWIP_UNUSED_ARG(arg);
    LWIP_UNUSED_ARG(pcb);

    /* packet received: stop retry timeout  */
    sys_untimeout(sntp_try_next_server, NULL);
    sys_untimeout(sntp_request, NULL);

    err = ERR_ARG;
#if SNTP_CHECK_RESPONSE >= 1
    /* Check server address and port */
    if (ip_addr_cmp(addr, &sntp_last_server_address) && (port == SNTP_PORT))
#else /* SNTP_CHECK_RESPONSE >= 1 */
    LWIP_UNUSED_ARG(addr);
    LWIP_UNUSED_ARG(port);
#endif /* SNTP_CHECK_RESPONSE >= 1 */
    {
        /* Process the response */
        if (p->tot_len == SNTP_MSG_LEN) {
            pbuf_copy_partial(p, &mode, 1, SNTP_OFFSET_LI_VN_MODE);
            mode &= SNTP_MODE_MASK;
            /* If this is a SNTP response... */
            if ((mode == SNTP_MODE_SERVER) || (mode == SNTP_MODE_BROADCAST)) {
                pbuf_copy_partial(p, &stratum, 1, SNTP_OFFSET_STRATUM);
                if (stratum == SNTP_STRATUM_KOD) {
                    /* Kiss-of-death packet. Use another server or increase UPDATE_DELAY. */
                    err = SNTP_ERR_KOD;
                    LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_recv: Received Kiss-of-Death\n"));
                } else {
#if SNTP_CHECK_RESPONSE >= 2
                    /* check originate_timetamp against sntp_last_timestamp_sent */
                    u32_t originate_timestamp[2];
                    pbuf_copy_partial(p, &originate_timestamp, 8, SNTP_OFFSET_ORIGINATE_TIME);
                    if ((originate_timestamp[0] != sntp_last_timestamp_sent[0]) ||
                            (originate_timestamp[1] != sntp_last_timestamp_sent[1])) {
                        LWIP_DEBUGF(SNTP_DEBUG_WARN, ("sntp_recv: Invalid originate timestamp in response\n"));
                    } else
#endif /* SNTP_CHECK_RESPONSE >= 2 */
                        /* @todo: add code for SNTP_CHECK_RESPONSE >= 3 and >= 4 here */
                    {
                        /* Correct answer */
                        err = ERR_OK;
                        pbuf_copy_partial(p, &receive_timestamp, SNTP_RECEIVE_TIME_SIZE * 4, SNTP_OFFSET_RECEIVE_TIME);
                    }
                }
            } else {
                LWIP_DEBUGF(SNTP_DEBUG_WARN, ("sntp_recv: Invalid mode in response: %"U16_F"\n", (u16_t)mode));
            }
        } else {
            LWIP_DEBUGF(SNTP_DEBUG_WARN, ("sntp_recv: Invalid packet length: %"U16_F"\n", p->tot_len));
        }
    }
    pbuf_free(p);

    if (err == ERR_OK) {
        /* Correct response, reset retry timeout */
        SNTP_RESET_RETRY_TIMEOUT();

        sntp_process(receive_timestamp);

        /* Set up timeout for next request */
        sys_timeout((u32_t)SNTP_UPDATE_DELAY, sntp_request, NULL);
        LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_recv: Scheduled next time request: %"U32_F" ms\n", (u32_t)SNTP_UPDATE_DELAY));
    } else if (err == SNTP_ERR_KOD) {
        /* Kiss-of-death packet. Use another server or increase UPDATE_DELAY. */
        sntp_try_next_server(NULL);
    } else {
        /* Another error, try the same server again */
        sntp_retry(NULL);
    }
}