From 59eb02daa9fd95fdda31928d1c8849f6dc829226 Mon Sep 17 00:00:00 2001 From: Benjamin Runnels Date: Thu, 3 Dec 2015 23:43:19 -0600 Subject: [PATCH] Added in sntp update --- cmd/cmd.c | 16 ++++++++-------- esp-link/cgiwifi.c | 40 ++++++++++++++++++++++++++++++++++++++-- esp-link/main.c | 2 +- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/cmd/cmd.c b/cmd/cmd.c index e8fd745..8f04faf 100644 --- a/cmd/cmd.c +++ b/cmd/cmd.c @@ -9,12 +9,6 @@ #ifdef CMD_DBG #define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) -#else -#define DBG(format, ...) do { } while(0) -#endif - -extern const CmdList commands[]; - static const char *cmd_names[] = { "NULL", "RESET", "IS_READY", "WIFI_CONNECT", "MQTT_SETUP", "MQTT_CONNECT", "MQTT_DISCONNECT", @@ -22,6 +16,11 @@ static const char *cmd_names[] = { "REST_SETUP", "REST_REQUEST", "REST_SETHEADER", "REST_EVENTS", "CB_ADD", "CB_EVENTS", }; +#else +#define DBG(format, ...) do { } while(0) +#endif + +extern const CmdList commands[]; //===== ESP -> Serial responses @@ -125,7 +124,7 @@ CMD_parse_packet(uint8_t *buf, short len) { CmdPacket *packet = (CmdPacket*)buf; uint8_t *data_ptr = (uint8_t*)&packet->args; uint8_t *data_limit = data_ptr+len; - uint16_t argn = 0; + DBG("CMD_parse_packet: cmd=%d(%s) argc=%d cb=%p ret=%lu\n", packet->cmd, cmd_names[packet->cmd], @@ -136,6 +135,7 @@ CMD_parse_packet(uint8_t *buf, short len) { #if 0 // print out arguments + uint16_t argn = 0; uint16_t argc = packet->argc; while (data_ptr+2 < data_limit && argc--) { short l = *(uint16_t*)data_ptr; @@ -151,7 +151,7 @@ CMD_parse_packet(uint8_t *buf, short len) { if (data_ptr <= data_limit) { CMD_Exec(commands, packet); } else { - DBG("CMD_parse_packet: packet length overrun, parsing arg %d\n", argn - 1); + DBG("CMD_parse_packet: packet length overrun, parsing arg %d\n", packet->argc); } } diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index a3f3a42..9fe6fe1 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -20,7 +20,7 @@ Cgi/template routines for the /wifi url. #include "status.h" #include "config.h" #include "log.h" -#include "mdns.h" +#include "sntp.h" #ifdef CGIWIFI_DBG #define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) @@ -29,6 +29,7 @@ Cgi/template routines for the /wifi url. #endif static void wifiStartMDNS(struct ip_addr); +static void wifiStartSNTP(); // ===== wifi status change callbacks static WifiStateChangeCb wifi_state_change_cb[4]; @@ -84,6 +85,7 @@ static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) { IP2STR(&evt->event_info.got_ip.gw)); statusWifiUpdate(wifiState); wifiStartMDNS(evt->event_info.got_ip.ip); + wifiStartSNTP(); break; case EVENT_SOFTAPMODE_STACONNECTED: DBG("Wifi AP: station " MACSTR " joined, AID = %d\n", @@ -116,7 +118,6 @@ wifiAddStateChangeCb(WifiStateChangeCb cb) { static bool mdns_started = false; -// static ICACHE_FLASH_ATTR void wifiStartMDNS(struct ip_addr ip) { if (!mdns_started) { @@ -130,6 +131,41 @@ void wifiStartMDNS(struct ip_addr ip) { } } +static bool sntp_started = false; + +static ETSTimer sntp_timer; + +void ICACHE_FLASH_ATTR +user_check_sntp_stamp(void *arg){ + uint32 current_stamp; + current_stamp = sntp_get_current_timestamp(); + if (current_stamp == 0){ + os_timer_arm(&sntp_timer, 100, 0); + } + else{ + os_timer_disarm(&sntp_timer); + os_printf("sntp: %d, %s \n", current_stamp, sntp_get_real_time(current_stamp)); + } +} + +static ICACHE_FLASH_ATTR +void wifiStartSNTP() { + if (!sntp_started) { + ip_addr_t *addr = (ip_addr_t *)os_zalloc(sizeof(ip_addr_t)); + sntp_setservername(0, "us.pool.ntp.org"); // set server 0 by domain name + sntp_setservername(1, "ntp.sjtu.edu.cn"); // set server 1 by domain name + IP4_ADDR(addr, 210, 72, 145, 44); + sntp_setserver(2, addr); // set server 2 by IP address + sntp_init(); + os_free(addr); + + os_timer_disarm(&sntp_timer); + os_timer_setfn(&sntp_timer, (os_timer_func_t *)user_check_sntp_stamp, NULL); + os_timer_arm(&sntp_timer, 100, 0); + sntp_started = true; + } +} + // ===== wifi scanning //WiFi access point data diff --git a/esp-link/main.c b/esp-link/main.c index 3719d9c..6819046 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -29,7 +29,7 @@ #include "console.h" #include "config.h" #include "log.h" -#include +#include "gpio.h" static int ICACHE_FLASH_ATTR cgiSystemInfo(HttpdConnData *connData); static int ICACHE_FLASH_ATTR cgiSystemSet(HttpdConnData *connData);