Skip to content

Commit

Permalink
Fixed NTP time synching issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Apr 23, 2022
1 parent e1652af commit 90a3653
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 203 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Google OAuth2.0 Access Token generation Arduino Library v1.1.5
# Google OAuth2.0 Access Token generation Arduino Library v1.1.6


This is the library will create the OAuth2.0 access token used in the Google API's http request (REST).
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ESP Signer",
"version": "1.1.5",
"version": "1.1.6",
"keywords": "communication, REST, esp32, esp8266, arduino",
"description": "The Google OAuth2.0 access token generation for Google REST API's http request.",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=ESP Signer

version=1.1.5
version=1.1.6

author=Mobizt

Expand Down
234 changes: 83 additions & 151 deletions src/ESPSigner.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/ESPSigner.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Google's OAuth2.0 Access token Generation class, Signer.h version 1.1.5
* Google's OAuth2.0 Access token Generation class, Signer.h version 1.1.6
*
* This library used RS256 for signing algorithm.
*
* The signed JWT token will be generated and exchanged with the access token in the final generating process.
*
* This library supports Espressif ESP8266 and ESP32
*
* Created April 20, 2022
* Created April 23, 2022
*
* The MIT License (MIT)
* Copyright (c) 2022 K. Suwatchai (Mobizt)
Expand Down
12 changes: 8 additions & 4 deletions src/SignerConst.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/**
* Created April 18, 2022
* Created April 23, 2022
*
*
* The MIT License (MIT)
Expand Down Expand Up @@ -179,7 +179,6 @@ struct esp_signer_service_account_t
struct esp_signer_token_signer_resources_t
{
int step = 0;
int attempts = 0;
bool tokenTaskRunning = false;
unsigned long lastReqMillis = 0;
unsigned long preRefreshSeconds = 60;
Expand Down Expand Up @@ -223,11 +222,13 @@ struct esp_signer_cfg_int_t
unsigned long esp_signer_last_jwt_begin_step_millis = 0;
uint16_t esp_signer_reconnect_tmo = WIFI_RECONNECT_TIMEOUT;
bool esp_signer_clock_rdy = false;
bool esp_signer_clock_init = false;
bool esp_signer_clock_synched = false;
float esp_signer_gmt_offset = 0;
const char *esp_signer_caCert = nullptr;
bool esp_signer_processing = false;
unsigned long esp_signer_last_jwt_generation_error_cb_millis = 0;
unsigned long esp_signer_last_time_sync_millis = 0;
unsigned long esp_signer_last_ntp_sync_timeout_millis = 0;

#if defined(ESP32)
TaskHandle_t token_processing_task_handle = NULL;
Expand All @@ -251,6 +252,8 @@ struct esp_signer_client_timeout_t
uint16_t tokenGenerationBeginStep = 300;

uint16_t tokenGenerationError = 5 * 1000;

uint16_t ntpServerRequest = 15 * 1000;
};

typedef struct token_info_t
Expand All @@ -268,7 +271,7 @@ struct esp_signer_cfg_t
float time_zone = 0;
struct esp_signer_auth_cert_t cert;
struct esp_signer_token_signer_resources_t signer;
struct esp_signer_cfg_int_t _int;
struct esp_signer_cfg_int_t internal;
TokenStatusCallback token_status_callback = NULL;
int8_t max_token_generation_retry = MAX_EXCHANGE_TOKEN_ATTEMPTS;
SPI_ETH_Module spi_ethernet_module;
Expand Down Expand Up @@ -414,6 +417,7 @@ static const char esp_signer_pgm_str_112[] PROGMEM = "ready";
static const char esp_signer_pgm_str_113[] PROGMEM = "error";
static const char esp_signer_pgm_str_114[] PROGMEM = "code: ";
static const char esp_signer_pgm_str_115[] PROGMEM = ", message: ";
static const char esp_signer_pgm_str_116[] PROGMEM = "NTP server time synching failed";

static const unsigned char esp_signer_base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char esp_signer_boundary_table[] PROGMEM = "=_abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Expand Down
81 changes: 40 additions & 41 deletions src/SignerUtils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Util class, SignerUtils.h version 1.0.4
* Util class, SignerUtils.h version 1.0.5
*
*
* Created April 18, 2022
* Created April 23, 2022
*
* This work is a part of ESP Signer library
* Copyright (c) 2022 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -39,7 +39,6 @@ class SignerUtils
{

public:
long default_ts = ESP_DEFAULT_TS;
uint16_t ntpTimeout = 20;
esp_signer_callback_function_t _callback_function = nullptr;
SignerConfig *config = nullptr;
Expand Down Expand Up @@ -890,12 +889,12 @@ class SignerUtils
if (!config)
return;

if (config->_int.esp_signer_file)
config->_int.esp_signer_file.close();
if (config->internal.esp_signer_file)
config->internal.esp_signer_file.close();
if (sd)
{
config->_int.esp_signer_sd_used = false;
config->_int.esp_signer_sd_rdy = false;
config->internal.esp_signer_sd_used = false;
config->internal.esp_signer_sd_rdy = false;
#if defined SD_FS
SD_FS.end();
#endif
Expand Down Expand Up @@ -1250,39 +1249,39 @@ class SignerUtils
return ret;
}

bool setClock(float gmtOffset)
bool syncClock(float gmtOffset)
{
if (!config)
return false;

if (time(nullptr) > default_ts && gmtOffset == config->_int.esp_signer_gmt_offset)
return true;
time_t now = time(nullptr);

if (config->_int.esp_signer_reconnect_wifi)
reconnect(0);
config->internal.esp_signer_clock_rdy = now > ESP_DEFAULT_TS;

time_t now = time(nullptr);
if (config->internal.esp_signer_clock_rdy && gmtOffset == config->internal.esp_signer_gmt_offset)
return true;

config->_int.esp_signer_clock_rdy = now > default_ts;
if (config->internal.esp_signer_reconnect_wifi)
reconnect(0);

if (!config->_int.esp_signer_clock_rdy || gmtOffset != config->_int.esp_signer_gmt_offset)
if (!config->internal.esp_signer_clock_rdy || gmtOffset != config->internal.esp_signer_gmt_offset)
{
if (gmtOffset != config->_int.esp_signer_gmt_offset)
config->_int.esp_signer_clock_init = false;
if (gmtOffset != config->internal.esp_signer_gmt_offset)
config->internal.esp_signer_clock_synched = false;

if (!config->_int.esp_signer_clock_init)
if (!config->internal.esp_signer_clock_synched)
configTime(gmtOffset * 3600, 0, "pool.ntp.org", "time.nist.gov");

config->_int.esp_signer_clock_init = true;
config->internal.esp_signer_clock_synched = true;

now = time(nullptr);
}

config->_int.esp_signer_clock_rdy = now > default_ts;
if (config->_int.esp_signer_clock_rdy)
config->_int.esp_signer_gmt_offset = gmtOffset;
config->internal.esp_signer_clock_rdy = now > ESP_DEFAULT_TS;
if (config->internal.esp_signer_clock_rdy)
config->internal.esp_signer_gmt_offset = gmtOffset;

return config->_int.esp_signer_clock_rdy;
return config->internal.esp_signer_clock_rdy;
}

void encodeBase64Url(char *encoded, unsigned char *string, size_t len)
Expand Down Expand Up @@ -1489,10 +1488,10 @@ class SignerUtils

if (config)
{
config->_int.sd_config.sck = sck;
config->_int.sd_config.miso = miso;
config->_int.sd_config.mosi = mosi;
config->_int.sd_config.ss = ss;
config->internal.sd_config.sck = sck;
config->internal.sd_config.miso = miso;
config->internal.sd_config.mosi = mosi;
config->internal.sd_config.ss = ss;
}
#if defined(ESP32)
if (ss > -1)
Expand Down Expand Up @@ -1523,9 +1522,9 @@ class SignerUtils

if (config)
{
config->_int.sd_config.sd_mmc_mountpoint = mountpoint;
config->_int.sd_config.sd_mmc_mode1bit = mode1bit;
config->_int.sd_config.sd_mmc_format_if_mount_failed = format_if_mount_failed;
config->internal.sd_config.sd_mmc_mountpoint = mountpoint;
config->internal.sd_config.sd_mmc_mode1bit = mode1bit;
config->internal.sd_config.sd_mmc_format_if_mount_failed = format_if_mount_failed;
}
return SD_FS.begin(mountpoint, mode1bit, format_if_mount_failed);
}
Expand All @@ -1539,13 +1538,13 @@ class SignerUtils
return false;
#if defined(ESP32)
if (FORMAT_FLASH == 1)
config->_int.esp_signer_flash_rdy = FLASH_FS.begin(true);
config->internal.esp_signer_flash_rdy = FLASH_FS.begin(true);
else
config->_int.esp_signer_flash_rdy = FLASH_FS.begin();
config->internal.esp_signer_flash_rdy = FLASH_FS.begin();
#elif defined(ESP8266)
config->_int.esp_signer_flash_rdy = FLASH_FS.begin();
config->internal.esp_signer_flash_rdy = FLASH_FS.begin();
#endif
return config->_int.esp_signer_flash_rdy;
return config->internal.esp_signer_flash_rdy;
#else
return false;
#endif
Expand All @@ -1559,12 +1558,12 @@ class SignerUtils

MB_String filepath = "/sdtest01.txt";
#if defined(CARD_TYPE_SD)
if (!sdBegin(config->_int.sd_config.ss, config->_int.sd_config.sck, config->_int.sd_config.miso, config->_int.sd_config.mosi))
if (!sdBegin(config->internal.sd_config.ss, config->internal.sd_config.sck, config->internal.sd_config.miso, config->internal.sd_config.mosi))
return false;
#endif
#if defined(ESP32)
#if defined(CARD_TYPE_SD_MMC)
if (!sdBegin(config->_int.sd_config.sd_mmc_mountpoint, config->_int.sd_config.sd_mmc_mode1bit, config->_int.sd_config.sd_mmc_format_if_mount_failed))
if (!sdBegin(config->internal.sd_config.sd_mmc_mountpoint, config->internal.sd_config.sd_mmc_mode1bit, config->internal.sd_config.sd_mmc_format_if_mount_failed))
return false;
#endif
#endif
Expand Down Expand Up @@ -1598,7 +1597,7 @@ class SignerUtils

MB_String().swap(filepath);

config->_int.esp_signer_sd_rdy = true;
config->internal.esp_signer_sd_rdy = true;

return true;
#else
Expand All @@ -1620,7 +1619,7 @@ class SignerUtils
#if defined(ESP32)

unsigned long wTime = millis();
while (config->_int.esp_signer_processing)
while (config->internal.esp_signer_processing)
{
if (millis() - wTime > 3000)
{
Expand Down Expand Up @@ -1680,14 +1679,14 @@ class SignerUtils

if (config)
{
if (config->_int.esp_signer_reconnect_wifi)
if (config->internal.esp_signer_reconnect_wifi)
{
if (config->timeout.wifiReconnect < 10000 || config->timeout.wifiReconnect > 5 * 60 * 1000)
config->timeout.wifiReconnect = 10000;
if (millis() - config->_int.esp_signer_last_reconnect_millis > config->timeout.wifiReconnect)
if (millis() - config->internal.esp_signer_last_reconnect_millis > config->timeout.wifiReconnect)
{
WiFi.reconnect();
config->_int.esp_signer_last_reconnect_millis = millis();
config->internal.esp_signer_last_reconnect_millis = millis();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/wcs/HTTPCode.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Created April 18, 2022
* Created April 23, 2022
*
* This work is a part of ESP Signer library
* Copyright (c) 2022, K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -55,7 +55,7 @@
#define ESP_SIGNER_ERROR_TOKEN_EXCHANGE -23
#define ESP_SIGNER_ERROR_TOKEN_EXCHANGE_MAX_RETRY_REACHED -24
#define ESP_SIGNER_ERROR_TOKEN_NOT_READY -25

#define ESP_SIGNER_ERROR_NTP_SYNC_TIMED_OUT -26

#define ESP_SIGNER_ERROR_HTTP_CODE_UNDEFINED -1000

Expand Down

0 comments on commit 90a3653

Please sign in to comment.