Skip to content

Commit

Permalink
Add SNTP get-interval option
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Aug 3, 2023
1 parent 54f3421 commit d590cbc
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 12 deletions.
1 change: 1 addition & 0 deletions lwesp/src/include/lwesp/lwesp_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ uint8_t lwespi_parse_cwjap(const char* str, lwesp_msg_t* msg);
uint8_t lwespi_parse_cwlif(const char* str, lwesp_msg_t* msg);
uint8_t lwespi_parse_cipdomain(const char* src, lwesp_msg_t* msg);
uint8_t lwespi_parse_cipsntptime(const char* str, lwesp_msg_t* msg);
uint8_t lwespi_parse_cipsntpintv(const char* str, lwesp_msg_t* msg);
uint8_t lwespi_parse_sntp_cfg(const char* str, lwesp_msg_t* msg);
uint8_t lwespi_parse_ping_time(const char* str, lwesp_msg_t* msg);
uint8_t lwespi_parse_hostname(const char* str, lwesp_msg_t* msg);
Expand Down
17 changes: 11 additions & 6 deletions lwesp/src/include/lwesp/lwesp_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ typedef enum {
LWESP_CMD_TCPIP_CIPRECVLEN, /*!< Gets number of available bytes in connection to be read */
LWESP_CMD_TCPIP_CIUPDATE, /*!< Perform self-update */
#if LWESP_CFG_SNTP || __DOXYGEN__
LWESP_CMD_TCPIP_CIPSNTPCFG, /*!< Configure SNTP servers */
LWESP_CMD_TCPIP_CIPSNTPCFG_GET, /*!< Get SNTP config */
LWESP_CMD_TCPIP_CIPSNTPTIME, /*!< Get current time using SNTP */
LWESP_CMD_TCPIP_CIPSNTPINTV, /*!< Query/Set the SNTP time synchronization interval */
#endif /* LWESP_SNT || __DOXYGEN__ */
LWESP_CMD_TCPIP_CIPDINFO, /*!< Configure what data are received on +IPD statement */
LWESP_CMD_TCPIP_CIPSNTPCFG, /*!< Configure SNTP servers */
LWESP_CMD_TCPIP_CIPSNTPCFG_GET, /*!< Get SNTP config */
LWESP_CMD_TCPIP_CIPSNTPTIME, /*!< Get current time using SNTP */
LWESP_CMD_TCPIP_CIPSNTPINTV, /*!< Set the SNTP time synchronization interval */
LWESP_CMD_TCPIP_CIPSNTPINTV_GET, /*!< Query the SNTP time synchronization interval */
#endif /* LWESP_SNT || __DOXYGEN__ */
LWESP_CMD_TCPIP_CIPDINFO, /*!< Configure what data are received on +IPD statement */
#if LWESP_CFG_PING || __DOXYGEN__
LWESP_CMD_TCPIP_PING, /*!< Ping domain */
#endif /* LWESP_CFG_PING || __DOXYGEN__ */
Expand Down Expand Up @@ -500,6 +501,10 @@ typedef struct lwesp_msg {
uint32_t interval; /*!< Time in units of seconds */
} tcpip_sntp_intv; /*!< SNTP interval configuration */

struct {
uint32_t* interval; /*!< Pointer to write time to */
} tcpip_sntp_intv_get; /*!< SNTP interval configuration */

struct {
struct tm* dt; /*!< Pointer to datetime structure */
} tcpip_sntp_time; /*!< SNTP get time */
Expand Down
2 changes: 2 additions & 0 deletions lwesp/src/include/lwesp/lwesp_sntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ lwespr_t lwesp_sntp_get_config(uint8_t* en, int16_t* tz, char* h1, char* h2, cha
const lwesp_api_cmd_evt_fn evt_fn, void* const evt_arg, const uint32_t blocking);
lwespr_t lwesp_sntp_set_interval(uint32_t interval, const lwesp_api_cmd_evt_fn evt_fn, void* const evt_arg,
const uint32_t blocking);
lwespr_t lwesp_sntp_get_interval(uint32_t* interval, const lwesp_api_cmd_evt_fn evt_fn, void* const evt_arg,
const uint32_t blocking);
lwespr_t lwesp_sntp_gettime(struct tm* dt, const lwesp_api_cmd_evt_fn evt_fn, void* const evt_arg,
const uint32_t blocking);

Expand Down
12 changes: 10 additions & 2 deletions lwesp/src/lwesp/lwesp_int.c
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,9 @@ lwespi_parse_received(lwesp_recv_t* rcv) {
lwespi_parse_cipsntptime(rcv->data, esp.msg); /* Parse CIPSNTPTIME entry */
} else if (CMD_IS_CUR(LWESP_CMD_TCPIP_CIPSNTPCFG_GET) && !strncmp(rcv->data, "+CIPSNTPCFG", 11)) {
lwespi_parse_sntp_cfg(rcv->data, esp.msg); /* Parse CIPSNTPTIME entry */
#endif /* LWESP_CFG_SNTP */
} else if (CMD_IS_CUR(LWESP_CMD_TCPIP_CIPSNTPINTV_GET) && !strncmp(rcv->data, "+CIPSNTPINTV", 12)) {
lwespi_parse_cipsntpintv(rcv->data, esp.msg); /* Parse CIPSNTPINTV entry */
#endif /* LWESP_CFG_SNTP */
#if LWESP_CFG_HOSTNAME
} else if (CMD_IS_CUR(LWESP_CMD_WIFI_CWHOSTNAME_GET) && !strncmp(rcv->data, "+CWHOSTNAME", 11)) {
lwespi_parse_hostname(rcv->data, esp.msg); /* Parse HOSTNAME entry */
Expand Down Expand Up @@ -2711,7 +2713,7 @@ lwespi_initiate_cmd(lwesp_msg_t* msg) {
AT_PORT_SEND_END_AT();
break;
}
case LWESP_CMD_TCPIP_CIPSNTPTIME: { /* Get time over SNTP */
case LWESP_CMD_TCPIP_CIPSNTPTIME: { /* Query time over SNTP */
AT_PORT_SEND_BEGIN_AT();
AT_PORT_SEND_CONST_STR("+CIPSNTPTIME?");
AT_PORT_SEND_END_AT();
Expand All @@ -2724,6 +2726,12 @@ lwespi_initiate_cmd(lwesp_msg_t* msg) {
AT_PORT_SEND_END_AT();
break;
}
case LWESP_CMD_TCPIP_CIPSNTPINTV_GET: { /* Query SNTP sync interval */
AT_PORT_SEND_BEGIN_AT();
AT_PORT_SEND_CONST_STR("+CIPSNTPINTV?");
AT_PORT_SEND_END_AT();
break;
}
#endif /* LWESP_CFG_SNTP */
#if LWESP_CFG_SMART
case LWESP_CMD_WIFI_SMART_START: { /* Start smart config */
Expand Down
28 changes: 25 additions & 3 deletions lwesp/src/lwesp/lwesp_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ lwespi_parse_cipdomain(const char* str, lwesp_msg_t* msg) {

/**
* \brief Parse received message for SNTP configuration
* \param[in] str: Pointer to input string starting with +CWLAP
* \param[in] str: Pointer to input string starting with +CIPSNTPCFG
* \param[in] msg: Pointer to message
* \return `1` on success, `0` otherwise
*/
Expand All @@ -681,7 +681,7 @@ lwespi_parse_sntp_cfg(const char* str, lwesp_msg_t* msg) {
return 0;
}
if (*str == '+') { /* Check input string */
str += 13;
str += 12;
}
num = lwespi_parse_number(&str);
if (msg->msg.tcpip_sntp_cfg_get.en != NULL) {
Expand All @@ -695,9 +695,31 @@ lwespi_parse_sntp_cfg(const char* str, lwesp_msg_t* msg) {
return 1;
}

/**
* \brief Parse received message for SNTP sync interval
* \param[in] str: Pointer to input string starting with +CIPSNTPINTV
* \param[in] msg: Pointer to message
* \return `1` on success, `0` otherwise
*/
uint8_t
lwespi_parse_cipsntpintv(const char* str, lwesp_msg_t* msg) {
int32_t num;
if (!CMD_IS_DEF(LWESP_CMD_TCPIP_CIPSNTPINTV_GET)) {
return 0;
}
if (*str == '+') { /* Check input string */
str += 13;
}
num = lwespi_parse_number(&str);
if (msg->msg.tcpip_sntp_intv_get.interval != NULL) {
*msg->msg.tcpip_sntp_intv_get.interval = (uint32_t)num;
}
return 1;
}

/**
* \brief Parse received message for SNTP time
* \param[in] str: Pointer to input string starting with +CWLAP
* \param[in] str: Pointer to input string starting with +CIPSNTPTIME
* \param[in] msg: Pointer to message
* \return `1` on success, `0` otherwise
*/
Expand Down
28 changes: 28 additions & 0 deletions lwesp/src/lwesp/lwesp_sntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,34 @@ lwesp_sntp_set_interval(uint32_t interval, const lwesp_api_cmd_evt_fn evt_fn, vo
return lwespi_send_msg_to_producer_mbox(&LWESP_MSG_VAR_REF(msg), lwespi_initiate_cmd, 1000);
}

/**
* \brief Get SNTP synchronization interval on Espressif device
* SNTP must be configured using \ref lwesp_sntp_set_config before you can use this function.
*
* \note This command is not available for all Espressif devices using AT commands
* and will return error when this is the case.
* \param[in] interval: Pointer to variable to write interval.
* It is value in seconds. It must not be `NULL`
* \param[in] evt_fn: Callback function called when command has finished. Set to `NULL` when not used
* \param[in] evt_arg: Custom argument for event callback function
* \param[in] blocking: Status whether command should be blocking or not
* \return \ref lwespOK on success, member of \ref lwespr_t enumeration otherwise
*/
lwespr_t
lwesp_sntp_get_interval(uint32_t* interval, const lwesp_api_cmd_evt_fn evt_fn, void* const evt_arg,
const uint32_t blocking) {
LWESP_MSG_VAR_DEFINE(msg);

LWESP_ASSERT(interval != NULL);

LWESP_MSG_VAR_ALLOC(msg, blocking);
LWESP_MSG_VAR_SET_EVT(msg, evt_fn, evt_arg);
LWESP_MSG_VAR_REF(msg).cmd_def = LWESP_CMD_TCPIP_CIPSNTPINTV_GET;
LWESP_MSG_VAR_REF(msg).msg.tcpip_sntp_intv_get.interval = interval;

return lwespi_send_msg_to_producer_mbox(&LWESP_MSG_VAR_REF(msg), lwespi_initiate_cmd, 1000);
}

/**
* \brief Get time from SNTP servers
* SNTP must be configured using \ref lwesp_sntp_set_config before you can use this function
Expand Down
5 changes: 4 additions & 1 deletion snippets/netconn_client_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ netconn_client_ssl_thread(void const* arg) {
client = lwesp_netconn_new(LWESP_NETCONN_TYPE_SSL);
if (client != NULL) {
struct tm dt;
uint8_t sntp_en;
uint32_t sntp_interval = 0;
uint8_t sntp_en = 0;

/* Write data to coresponding manuf NVS */
res = lwesp_mfg_write(LWESP_MFG_NAMESPACE_CLIENT_CA, "client_ca.0", LWESP_MFG_VALTYPE_BLOB, client_ca,
Expand All @@ -81,6 +82,8 @@ netconn_client_ssl_thread(void const* arg) {
if (!sntp_en) {
lwesp_sntp_set_config(1, 2, NULL, NULL, NULL, NULL, NULL, 1);
}
lwesp_sntp_get_interval(&sntp_interval, NULL, NULL, 1);
printf("SNTP interval: %u seconds\r\n", (unsigned)sntp_interval);
do {
lwesp_sntp_gettime(&dt, NULL, NULL, 1);
if (dt.tm_year > 100) {
Expand Down

0 comments on commit d590cbc

Please sign in to comment.