Skip to content

Commit

Permalink
Delete lwesp_datetime_t and use generic struct tm instead
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Jun 19, 2023
1 parent 0af20eb commit 49e529a
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 66 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"examples_common_lwesp_callback_func.h": "c",
"*.hex": "c",
"lwesp_netconn.h": "c",
"netconn_client.h": "c"
"netconn_client.h": "c",
"lwesp_sntp.h": "c",
"utils.h": "c"
},
"esbonio.sphinx.confDir": ""
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- SSL: Added experimental support
- FileSystem: Added support for erase and write operation
- Code improvement: Change multiple local variables to single structure
- Delete `lwesp_datetime_t` and use generic `struct tm` instead

## 1.1.2-dev

Expand Down
8 changes: 6 additions & 2 deletions dev/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "netconn_client.h"
#include "netconn_server.h"
#include "netconn_server_1thread.h"
#include "sntp.h"
#include "station_manager.h"
#include "utils.h"

Expand Down Expand Up @@ -411,12 +412,15 @@ main_thread(void* arg) {

/* HTTP server application example */

/* SNTP example */
sntp_gettime();

/* Netconn client in separate thread */
//lwesp_sys_thread_create(NULL, "netconn_client", (lwesp_sys_thread_fn)netconn_client_thread, NULL, 0, LWESP_SYS_THREAD_PRIO);

/* Netconn client in separate thread */
lwesp_sys_thread_create(NULL, "netconn_client_ssl", (lwesp_sys_thread_fn)netconn_client_ssl_thread, NULL, 0,
LWESP_SYS_THREAD_PRIO);
//lwesp_sys_thread_create(NULL, "netconn_client_ssl", (lwesp_sys_thread_fn)netconn_client_ssl_thread, NULL, 0,
// LWESP_SYS_THREAD_PRIO);

/* Netconn server with multiple threads */
//lwesp_sys_thread_create(NULL, "netconn_server", (lwesp_sys_thread_fn)netconn_server_thread, NULL, 0, LWESP_SYS_THREAD_PRIO);
Expand Down
6 changes: 0 additions & 6 deletions docs/examples_src/ping.c

This file was deleted.

12 changes: 0 additions & 12 deletions docs/examples_src/sntp_basic.c

This file was deleted.

2 changes: 1 addition & 1 deletion lwesp/src/include/lwesp/lwesp_evt.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ uint32_t lwesp_evt_ping_get_time(lwesp_evt_t* cc);
*/

lwespr_t lwesp_evt_sntp_time_get_result(lwesp_evt_t* cc);
const lwesp_datetime_t* lwesp_evt_sntp_time_get_datetime(lwesp_evt_t* cc);
const struct tm* lwesp_evt_sntp_time_get_datetime(lwesp_evt_t* cc);

/**
* \}
Expand Down
6 changes: 3 additions & 3 deletions lwesp/src/include/lwesp/lwesp_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,9 @@ typedef struct lwesp_msg {
} tcpip_sntp_intv; /*!< SNTP interval configuration */

struct {
lwesp_datetime_t* dt; /*!< Pointer to datetime structure */
} tcpip_sntp_time; /*!< SNTP get time */
#endif /* LWESP_CFG_SNTP || __DOXYGEN__ */
struct tm* dt; /*!< Pointer to datetime structure */
} tcpip_sntp_time; /*!< SNTP get time */
#endif /* LWESP_CFG_SNTP || __DOXYGEN__ */
#if LWESP_CFG_WPS || __DOXYGEN__
struct {
uint8_t en; /*!< Status if WPS is enabled or not */
Expand Down
2 changes: 1 addition & 1 deletion lwesp/src/include/lwesp/lwesp_sntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ lwespr_t lwesp_sntp_set_config(uint8_t en, int16_t tz, const char* h1, const 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_gettime(lwesp_datetime_t* dt, const lwesp_api_cmd_evt_fn evt_fn, void* const evt_arg,
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
9 changes: 5 additions & 4 deletions lwesp/src/include/lwesp/lwesp_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "lwesp/lwesp_opt.h"

#ifdef __cplusplus
Expand Down Expand Up @@ -591,10 +592,10 @@ typedef struct lwesp_evt {
#endif /* LWESP_CFG_PING || __DOXYGEN__ */
#if LWESP_CFG_SNTP || __DOXYGEN__
struct {
lwespr_t res; /*!< Result of command */
const lwesp_datetime_t* dt; /*!< Pointer to datetime structure */
} cip_sntp_time; /*!< SNTP time finished. Use with \ref LWESP_EVT_SNTP_TIME event */
#endif /* LWESP_CFG_SNTP || __DOXYGEN__ */
lwespr_t res; /*!< Result of command */
const struct tm* dt; /*!< Pointer to datetime structure */
} cip_sntp_time; /*!< SNTP time finished. Use with \ref LWESP_EVT_SNTP_TIME event */
#endif /* LWESP_CFG_SNTP || __DOXYGEN__ */

#if LWESP_CFG_WEBSERVER || __DOXYGEN__
struct {
Expand Down
2 changes: 1 addition & 1 deletion lwesp/src/lwesp/lwesp_evt.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ lwesp_evt_sntp_time_get_result(lwesp_evt_t* cc) {
* \param[in] cc: Event handle
* \return pointer to read-only structure with datetime
*/
const lwesp_datetime_t*
const struct tm*
lwesp_evt_sntp_time_get_datetime(lwesp_evt_t* cc) {
return cc->evt.cip_sntp_time.dt;
}
Expand Down
48 changes: 24 additions & 24 deletions lwesp/src/lwesp/lwesp_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,61 +623,61 @@ lwespi_parse_cipsntptime(const char* str, lwesp_msg_t* msg) {

/* Scan for day in a week */
if (!strncmp(str, "Mon", 3)) {
msg->msg.tcpip_sntp_time.dt->day = 1;
msg->msg.tcpip_sntp_time.dt->tm_mday = 0;
} else if (!strncmp(str, "Tue", 3)) {
msg->msg.tcpip_sntp_time.dt->day = 2;
msg->msg.tcpip_sntp_time.dt->tm_mday = 1;
} else if (!strncmp(str, "Wed", 3)) {
msg->msg.tcpip_sntp_time.dt->day = 3;
msg->msg.tcpip_sntp_time.dt->tm_mday = 2;
} else if (!strncmp(str, "Thu", 3)) {
msg->msg.tcpip_sntp_time.dt->day = 4;
msg->msg.tcpip_sntp_time.dt->tm_mday = 3;
} else if (!strncmp(str, "Fri", 3)) {
msg->msg.tcpip_sntp_time.dt->day = 5;
msg->msg.tcpip_sntp_time.dt->tm_mday = 4;
} else if (!strncmp(str, "Sat", 3)) {
msg->msg.tcpip_sntp_time.dt->day = 6;
msg->msg.tcpip_sntp_time.dt->tm_mday = 5;
} else if (!strncmp(str, "Sun", 3)) {
msg->msg.tcpip_sntp_time.dt->day = 7;
msg->msg.tcpip_sntp_time.dt->tm_mday = 6;
}
str += 4;

/* Scan for month in a year */
if (!strncmp(str, "Jan", 3)) {
msg->msg.tcpip_sntp_time.dt->month = 1;
msg->msg.tcpip_sntp_time.dt->tm_mon = 0;
} else if (!strncmp(str, "Feb", 3)) {
msg->msg.tcpip_sntp_time.dt->month = 2;
msg->msg.tcpip_sntp_time.dt->tm_mon = 1;
} else if (!strncmp(str, "Mar", 3)) {
msg->msg.tcpip_sntp_time.dt->month = 3;
msg->msg.tcpip_sntp_time.dt->tm_mon = 2;
} else if (!strncmp(str, "Apr", 3)) {
msg->msg.tcpip_sntp_time.dt->month = 4;
msg->msg.tcpip_sntp_time.dt->tm_mon = 3;
} else if (!strncmp(str, "May", 3)) {
msg->msg.tcpip_sntp_time.dt->month = 5;
msg->msg.tcpip_sntp_time.dt->tm_mon = 4;
} else if (!strncmp(str, "Jun", 3)) {
msg->msg.tcpip_sntp_time.dt->month = 6;
msg->msg.tcpip_sntp_time.dt->tm_mon = 5;
} else if (!strncmp(str, "Jul", 3)) {
msg->msg.tcpip_sntp_time.dt->month = 7;
msg->msg.tcpip_sntp_time.dt->tm_mon = 6;
} else if (!strncmp(str, "Aug", 3)) {
msg->msg.tcpip_sntp_time.dt->month = 8;
msg->msg.tcpip_sntp_time.dt->tm_mon = 7;
} else if (!strncmp(str, "Sep", 3)) {
msg->msg.tcpip_sntp_time.dt->month = 9;
msg->msg.tcpip_sntp_time.dt->tm_mon = 8;
} else if (!strncmp(str, "Oct", 3)) {
msg->msg.tcpip_sntp_time.dt->month = 10;
msg->msg.tcpip_sntp_time.dt->tm_mon = 9;
} else if (!strncmp(str, "Nov", 3)) {
msg->msg.tcpip_sntp_time.dt->month = 11;
msg->msg.tcpip_sntp_time.dt->tm_mon = 10;
} else if (!strncmp(str, "Dec", 3)) {
msg->msg.tcpip_sntp_time.dt->month = 12;
msg->msg.tcpip_sntp_time.dt->tm_mon = 11;
}
str += 4;
if (*str == ' ') { /* Numbers < 10 could have one more space */
++str;
}
msg->msg.tcpip_sntp_time.dt->date = lwespi_parse_number(&str);
msg->msg.tcpip_sntp_time.dt->tm_mday = lwespi_parse_number(&str);
++str;
msg->msg.tcpip_sntp_time.dt->hours = lwespi_parse_number(&str);
msg->msg.tcpip_sntp_time.dt->tm_hour = lwespi_parse_number(&str);
++str;
msg->msg.tcpip_sntp_time.dt->minutes = lwespi_parse_number(&str);
msg->msg.tcpip_sntp_time.dt->tm_min = lwespi_parse_number(&str);
++str;
msg->msg.tcpip_sntp_time.dt->seconds = lwespi_parse_number(&str);
msg->msg.tcpip_sntp_time.dt->tm_sec = lwespi_parse_number(&str);
++str;
msg->msg.tcpip_sntp_time.dt->year = lwespi_parse_number(&str);
msg->msg.tcpip_sntp_time.dt->tm_year = lwespi_parse_number(&str) - 1900;
return 1;
}

Expand Down
5 changes: 2 additions & 3 deletions lwesp/src/lwesp/lwesp_sntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,14 @@ lwesp_sntp_set_interval(uint32_t interval, const lwesp_api_cmd_evt_fn evt_fn, vo
/**
* \brief Get time from SNTP servers
* SNTP must be configured using \ref lwesp_sntp_set_config before you can use this function
* \param[out] dt: Pointer to \ref lwesp_datetime_t structure to fill with date and time values
* \param[out] dt: Pointer to struct tm structure to fill with date and time values
* \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_gettime(lwesp_datetime_t* dt, const lwesp_api_cmd_evt_fn evt_fn, void* const evt_arg,
const uint32_t blocking) {
lwesp_sntp_gettime(struct tm* dt, const lwesp_api_cmd_evt_fn evt_fn, void* const evt_arg, const uint32_t blocking) {
LWESP_MSG_VAR_DEFINE(msg);

LWESP_MSG_VAR_ALLOC(msg, blocking);
Expand Down
8 changes: 4 additions & 4 deletions snippets/netconn_client_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ netconn_client_ssl_thread(void const* arg) {
*/
client = lwesp_netconn_new(LWESP_NETCONN_TYPE_SSL);
if (client != NULL) {
lwesp_datetime_t dt;
struct tm dt;

/* First erase all client SSL settings */
lwesp_flash_erase(LWESP_FLASH_PARTITION_CLIENT_CA, 0, 0, NULL, NULL, 1);
Expand All @@ -81,7 +81,7 @@ netconn_client_ssl_thread(void const* arg) {
lwesp_sntp_set_config(1, 2, NULL, NULL, NULL, NULL, NULL, 1);
do {
lwesp_sntp_gettime(&dt, NULL, NULL, 1);
if (dt.year > 2000) {
if (dt.tm_year > 100) {
break;
}
} while (1);
Expand All @@ -97,9 +97,9 @@ netconn_client_ssl_thread(void const* arg) {
printf("Connected to " NETCONN_HOST "\r\n");
res = lwesp_netconn_write(client, request_header, sizeof(request_header) - 1); /* Send data to server */
if (res == lwespOK) {
res = lwesp_netconn_flush(client); /* Flush data to output */
res = lwesp_netconn_flush(client); /* Flush data to output */
}
if (res == lwespOK) { /* Were data sent? */
if (res == lwespOK) { /* Were data sent? */
printf("Data were successfully sent to server\r\n");

/*
Expand Down
7 changes: 3 additions & 4 deletions snippets/sntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@
*/
void
sntp_gettime(void) {
lwesp_datetime_t dt;
struct tm dt;

/* Enable SNTP with default configuration for NTP servers */
if (lwesp_sntp_set_config(1, 1, NULL, NULL, NULL, NULL, NULL, 1) == lwespOK) {
lwesp_delay(5000);

/* Get actual time and print it */
if (lwesp_sntp_gettime(&dt, NULL, NULL, 1) == lwespOK) {
printf("Date & time: %d.%d.%d, %d:%d:%d\r\n",
(int)dt.date, (int)dt.month, (int)dt.year,
(int)dt.hours, (int)dt.minutes, (int)dt.seconds);
printf("Date & time: %d.%d.%d, %d:%d:%d\r\n", (int)dt.tm_mday, (int)(dt.tm_mon + 1),
(int)(dt.tm_year + 1900), (int)dt.tm_hour, (int)dt.tm_min, (int)dt.tm_sec);
}
}
}

0 comments on commit 49e529a

Please sign in to comment.