diff --git a/README.md b/README.md index 9e6535b..ebafe27 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Arduino Google Sheet Client Library for ESP8266 and ESP32 -Arduino Google Sheet Client Library for ESP8266 and ESP32 v1.0.3. +Arduino Google Sheet Client Library for ESP8266 and ESP32 v1.0.4. This library allows devices to authenticate and communicate with Google Sheet APIs using the Service Account. @@ -17,13 +17,6 @@ You can create, edit and deploy the Apps Script code via extension of spreadshee Spreadsheet created or owned by you, needed to share the access with Service Account's client email then library can read, and edit except for delete the user's spreadsheet due to permission denied. -## Important Note - -The connection to Google Sheet API endpoint takes time and the time from sending request to the complete response received can be as much as 3 seconds. - -The speed is not the library or device issue unless the server issue. - - ## Dependencies diff --git a/library.json b/library.json index 78aa183..3faa802 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ESP-Google-Sheet-Client", - "version": "1.0.3", + "version": "1.0.4", "keywords": "communication, REST, esp32, esp8266, arduino", "description": "Arduino Google Sheet REST client library for ESP8266 and ESP32. This library allows devices to communicate with Google Sheet API to read, edit and delete the spreadsheets", "repository": { diff --git a/library.properties b/library.properties index 69b5d21..2e67dbb 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=ESP-Google-Sheet-Client -version=1.0.3 +version=1.0.4 author=Mobizt diff --git a/src/ESP_Google_Sheet_Client.cpp b/src/ESP_Google_Sheet_Client.cpp index 3916d10..c17cad3 100644 --- a/src/ESP_Google_Sheet_Client.cpp +++ b/src/ESP_Google_Sheet_Client.cpp @@ -1,9 +1,9 @@ /** - * Google Sheet Client, ESP_Google_Sheet_Client.cpp v1.0.1 + * Google Sheet Client, ESP_Google_Sheet_Client.cpp v1.0.4 * * This library supports Espressif ESP8266 and ESP32 MCUs * - * Created April 18, 2022 + * Created April 23, 2022 * * The MIT License (MIT) * Copyright (c) 2022 K. Suwatchai (Mobizt) @@ -57,7 +57,7 @@ void GSheetClass::auth(const char *client_email, const char *project_id, const c config.service_account.data.private_key = private_key; config.signer.expiredSeconds = 3600; config.signer.preRefreshSeconds = 60; - config._int.esp_signer_reconnect_wifi = WiFi.getAutoReconnect(); + config.internal.esp_signer_reconnect_wifi = WiFi.getAutoReconnect(); config.signer.tokens.scope = (const char *)FPSTR("https://www.googleapis.com/auth/drive.metadata,https://www.googleapis.com/auth/drive.appdata,https://www.googleapis.com/auth/spreadsheets,https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/drive.file"); this->begin(&config); } @@ -69,25 +69,20 @@ void GSheetClass::setTokenCallback(TokenStatusCallback callback) bool GSheetClass::checkToken() { - if (setClock(0)) - { - return this->tokenReady(); - } - - return false; + return this->tokenReady(); } bool GSheetClass::setClock(float gmtOffset) { - if (time(nullptr) > ESP_DEFAULT_TS && gmtOffset == config._int.esp_signer_gmt_offset) + if (time(nullptr) > ESP_DEFAULT_TS && gmtOffset == config.internal.esp_signer_gmt_offset) return true; time_t now = time(nullptr); - config._int.esp_signer_clock_rdy = now > ESP_DEFAULT_TS; + config.internal.esp_signer_clock_rdy = now > ESP_DEFAULT_TS; - 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) { configTime(gmtOffset * 3600, 0, "pool.ntp.org", "time.nist.gov"); @@ -102,11 +97,11 @@ bool GSheetClass::setClock(float gmtOffset) } } - config._int.esp_signer_clock_rdy = now > ESP_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 GSheetClass::beginRequest(FirebaseJson *response, MB_String &req, host_type_t host_type) @@ -189,30 +184,25 @@ void GSheetClass::setSecure() if (config.signer.wcs->_certType == -1 || cert_updated) { - if (!config._int.esp_signer_clock_rdy && (cert_addr > 0)) + if (!config.internal.esp_signer_clock_rdy && (cert_addr > 0)) { #if defined(ESP8266) - int retry = 0; - while (!config._int.esp_signer_clock_rdy && retry < 5) - { setClock(0); - retry++; - } #endif } - config.signer.wcs->_clockReady = config._int.esp_signer_clock_rdy; + config.signer.wcs->_clockReady = config.internal.esp_signer_clock_rdy; if (certFile.length() > 0) { #if defined(ESP8266) - if (config._int.sd_config.ss == -1) - config._int.sd_config.ss = SD_CS_PIN; + if (config.internal.sd_config.ss == -1) + config.internal.sd_config.ss = SD_CS_PIN; #endif int type = certFileStorageType == esP_google_sheet_file_storage_type_flash ? 1 : 2; - config.signer.wcs->setCACertFile(certFile.c_str(), type, config._int.sd_config); + config.signer.wcs->setCACertFile(certFile.c_str(), type, config.internal.sd_config); }else { if (cert_addr > 0) diff --git a/src/ESP_Google_Sheet_Client.h b/src/ESP_Google_Sheet_Client.h index d4e447e..36178e4 100644 --- a/src/ESP_Google_Sheet_Client.h +++ b/src/ESP_Google_Sheet_Client.h @@ -1,13 +1,13 @@ #ifndef ESP_GOOGLE_SHEET_CLIENT_VERSION -#define ESP_GOOGLE_SHEET_CLIENT_VERSION "1.0.3" +#define ESP_GOOGLE_SHEET_CLIENT_VERSION "1.0.4" #endif /** - * Google Sheet Client, ESP_Google_Sheet_Client.h v1.0.3 + * Google Sheet Client, ESP_Google_Sheet_Client.h v1.0.4 * * This library supports Espressif ESP8266 and ESP32 MCUs * - * Created April 20, 2022 + * Created April 23, 2022 * * The MIT License (MIT) * Copyright (c) 2022 K. Suwatchai (Mobizt) diff --git a/src/lib/ESPSigner/ESPSigner.cpp b/src/lib/ESPSigner/ESPSigner.cpp index c6d9a45..0d2a075 100644 --- a/src/lib/ESPSigner/ESPSigner.cpp +++ b/src/lib/ESPSigner/ESPSigner.cpp @@ -1,5 +1,5 @@ /** - * 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. * @@ -7,7 +7,7 @@ * * 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) @@ -53,7 +53,7 @@ void ESP_Signer::begin(SignerConfig *cfg) ut = new SignerUtils(config); config->signer.tokens.error.message.clear(); - config->_int.esp_signer_reconnect_wifi = WiFi.getAutoReconnect(); + config->internal.esp_signer_reconnect_wifi = WiFi.getAutoReconnect(); if (config->service_account.json.path.length() > 0) { @@ -65,13 +65,13 @@ void ESP_Signer::begin(SignerConfig *cfg) config->signer.tokens.token_type = esp_signer_token_type_oauth2_access_token; if (strlen_P(config->cert.data)) - config->_int.esp_signer_caCert = config->cert.data; + config->internal.esp_signer_caCert = config->cert.data; if (config->cert.file.length() > 0) { - if (config->cert.file_storage == esp_signer_mem_storage_type_sd && !config->_int.esp_signer_sd_rdy) - config->_int.esp_signer_sd_rdy = ut->sdTest(config->_int.esp_signer_file); - else if ((config->cert.file_storage == esp_signer_mem_storage_type_flash) && !config->_int.esp_signer_flash_rdy) + if (config->cert.file_storage == esp_signer_mem_storage_type_sd && !config->internal.esp_signer_sd_rdy) + config->internal.esp_signer_sd_rdy = ut->sdTest(config->internal.esp_signer_file); + else if ((config->cert.file_storage == esp_signer_mem_storage_type_flash) && !config->internal.esp_signer_flash_rdy) ut->flashTest(); } @@ -93,39 +93,39 @@ bool ESP_Signer::parseSAFile() if (config->signer.pk.length() > 0) return false; - if (config->service_account.json.storage_type == esp_signer_mem_storage_type_sd && !config->_int.esp_signer_sd_rdy) - config->_int.esp_signer_sd_rdy = ut->sdTest(config->_int.esp_signer_file); - else if (config->service_account.json.storage_type == esp_signer_mem_storage_type_flash && !config->_int.esp_signer_flash_rdy) + if (config->service_account.json.storage_type == esp_signer_mem_storage_type_sd && !config->internal.esp_signer_sd_rdy) + config->internal.esp_signer_sd_rdy = ut->sdTest(config->internal.esp_signer_file); + else if (config->service_account.json.storage_type == esp_signer_mem_storage_type_flash && !config->internal.esp_signer_flash_rdy) ut->flashTest(); - if (config->_int.esp_signer_sd_rdy || config->_int.esp_signer_flash_rdy) + if (config->internal.esp_signer_sd_rdy || config->internal.esp_signer_flash_rdy) { if (config->service_account.json.storage_type == esp_signer_mem_storage_type_flash) { if (FLASH_FS.exists(config->service_account.json.path.c_str())) - config->_int.esp_signer_file = FLASH_FS.open(config->service_account.json.path.c_str(), "r"); + config->internal.esp_signer_file = FLASH_FS.open(config->service_account.json.path.c_str(), "r"); } else { if (SD_FS.exists(config->service_account.json.path.c_str())) - config->_int.esp_signer_file = SD_FS.open(config->service_account.json.path.c_str(), "r"); + config->internal.esp_signer_file = SD_FS.open(config->service_account.json.path.c_str(), "r"); } - if (config->_int.esp_signer_file) + if (config->internal.esp_signer_file) { clearSA(); config->signer.json = new FirebaseJson(); config->signer.result = new FirebaseJsonData(); char *tmp = nullptr; - size_t len = config->_int.esp_signer_file.size(); + size_t len = config->internal.esp_signer_file.size(); char *buf = (char *)ut->newP(len + 10); - if (config->_int.esp_signer_file.available()) + if (config->internal.esp_signer_file.available()) { - config->_int.esp_signer_file.readBytes(buf, len); + config->internal.esp_signer_file.readBytes(buf, len); config->signer.json->setJsonData(buf); } - config->_int.esp_signer_file.close(); + config->internal.esp_signer_file.close(); ut->delP(&buf); if (parseJsonResponse(esp_signer_pgm_str_13)) @@ -200,30 +200,6 @@ bool ESP_Signer::handleToken() if (!config) return false; -#if defined(ESP8266) - if ((config->cert.data != NULL || config->cert.file.length() > 0) && !config->_int.esp_signer_clock_rdy) - { - ut->idle(); - time_t now = time(nullptr); - config->_int.esp_signer_clock_rdy = now > ut->default_ts; - - if (!config->_int.esp_signer_clock_rdy) - { - ut->setClock(config->time_zone); - - if (config->signer.tokens.status == esp_signer_token_status_uninitialized) - { - config->signer.tokens.status = esp_signer_token_status_on_initialize; - config->signer.tokens.error.code = 0; - config->signer.tokens.error.message.clear(); - config->_int.esp_signer_last_jwt_generation_error_cb_millis = 0; - sendTokenStatusCB(); - } - return false; - } - } -#endif - if (config->signer.tokens.token_type == esp_signer_token_type_oauth2_access_token && isExpired()) { @@ -240,11 +216,11 @@ bool ESP_Signer::handleToken() if (config->signer.tokens.status != esp_signer_token_status_on_initialize) { - + config->signer.tokens.status = esp_signer_token_status_on_initialize; config->signer.tokens.error.code = 0; config->signer.tokens.error.message.clear(); - config->_int.esp_signer_last_jwt_generation_error_cb_millis = 0; + config->internal.esp_signer_last_jwt_generation_error_cb_millis = 0; sendTokenStatusCB(); } @@ -278,8 +254,6 @@ bool ESP_Signer::isExpired() void ESP_Signer::tokenProcessingTask() { -#if defined(ESP32) - if (config->signer.tokenTaskRunning) return; @@ -287,76 +261,54 @@ void ESP_Signer::tokenProcessingTask() config->signer.tokenTaskRunning = true; + bool sslValidTime = false; + +#if defined(ESP8266) + if (config->cert.data != NULL || config->cert.file.length() > 0) + sslValidTime = true; +#endif + while (!ret && config->signer.tokens.status != esp_signer_token_status_ready) { delay(0); - if (config->signer.step == esp_signer_jwt_generation_step_begin && (millis() - config->_int.esp_signer_last_jwt_begin_step_millis > config->timeout.tokenGenerationBeginStep || config->_int.esp_signer_last_jwt_begin_step_millis == 0)) - { - config->_int.esp_signer_last_jwt_begin_step_millis = millis(); - ut->setClock(config->time_zone); - time_t now = time(nullptr); - config->_int.esp_signer_clock_rdy = now > ut->default_ts; - - if (config->_int.esp_signer_clock_rdy) - config->signer.step = esp_signer_jwt_generation_step_encode_header_payload; - } - else if (config->signer.step == esp_signer_jwt_generation_step_encode_header_payload) - { - if (createJWT()) - config->signer.step = esp_signer_jwt_generation_step_sign; - } - else if (config->signer.step == esp_signer_jwt_generation_step_sign) - { - if (createJWT()) - config->signer.step = esp_signer_jwt_generation_step_exchange; - } - else if (config->signer.step == esp_signer_jwt_generation_step_exchange) + // check time if clock synching once set + if (!config->internal.esp_signer_clock_rdy && (config->internal.esp_signer_clock_synched || sslValidTime)) { - - if (requestTokens()) - { - config->signer.attempts = 0; - _token_processing_task_enable = false; - config->signer.step = esp_signer_jwt_generation_step_begin; - ret = true; - } - else + if (millis() - config->internal.esp_signer_last_time_sync_millis > 1500) { - config->signer.step = esp_signer_jwt_generation_step_begin; + config->internal.esp_signer_last_time_sync_millis = millis(); - if (config->signer.attempts < config->max_token_generation_retry) - config->signer.attempts++; - else + if (millis() - config->internal.esp_signer_last_ntp_sync_timeout_millis > config->timeout.ntpServerRequest) { + config->internal.esp_signer_last_ntp_sync_timeout_millis = millis(); config->signer.tokens.error.message.clear(); - setTokenError(ESP_SIGNER_ERROR_TOKEN_EXCHANGE_MAX_RETRY_REACHED); - config->_int.esp_signer_last_jwt_generation_error_cb_millis = 0; + setTokenError(ESP_SIGNER_ERROR_NTP_SYNC_TIMED_OUT); sendTokenStatusCB(); - config->signer.attempts = 0; - config->signer.step = esp_signer_jwt_generation_step_begin; - ret = true; + config->signer.tokens.status = esp_signer_token_status_on_initialize; + config->internal.esp_signer_last_jwt_generation_error_cb_millis = 0; } - } - } - } - config->signer.tokenTaskRunning = false; + // set clock again if timed out + config->internal.esp_signer_clock_synched = false; + } -#elif defined(ESP8266) + // check or set time again + ut->syncClock(config->time_zone); - if (_token_processing_task_enable && config->signer.tokens.status != esp_signer_token_status_ready) - { + if (!config->internal.esp_signer_clock_rdy) + { + config->signer.tokenTaskRunning = false; + return; + } + } - if (config->signer.step == esp_signer_jwt_generation_step_begin && (millis() - config->_int.esp_signer_last_jwt_begin_step_millis > 200 || config->_int.esp_signer_last_jwt_begin_step_millis == 0)) + if (config->signer.step == esp_signer_jwt_generation_step_begin && (millis() - config->internal.esp_signer_last_jwt_begin_step_millis > config->timeout.tokenGenerationBeginStep || config->internal.esp_signer_last_jwt_begin_step_millis == 0)) { - config->_int.esp_signer_last_jwt_begin_step_millis = millis(); - config->signer.tokenTaskRunning = true; - ut->setClock(config->time_zone); - time_t now = time(nullptr); - config->_int.esp_signer_clock_rdy = now > ut->default_ts; + config->internal.esp_signer_last_jwt_begin_step_millis = millis(); + ut->syncClock(config->time_zone); - if (config->_int.esp_signer_clock_rdy) + if (config->internal.esp_signer_clock_rdy) config->signer.step = esp_signer_jwt_generation_step_encode_header_payload; } else if (config->signer.step == esp_signer_jwt_generation_step_encode_header_payload) @@ -372,37 +324,15 @@ void ESP_Signer::tokenProcessingTask() else if (config->signer.step == esp_signer_jwt_generation_step_exchange) { - if (requestTokens()) - { - config->signer.tokenTaskRunning = false; - _token_processing_task_enable = false; - config->signer.attempts = 0; - config->signer.step = esp_signer_jwt_generation_step_begin; - return; - } - else - { - if (config->signer.attempts < config->max_token_generation_retry) - config->signer.attempts++; - else - { - config->signer.tokens.error.message.clear(); - setTokenError(ESP_SIGNER_ERROR_TOKEN_EXCHANGE_MAX_RETRY_REACHED); - config->_int.esp_signer_last_jwt_generation_error_cb_millis = 0; - sendTokenStatusCB(); - config->signer.tokenTaskRunning = false; - _token_processing_task_enable = false; - config->signer.attempts = 0; - config->signer.step = esp_signer_jwt_generation_step_begin; - return; - } - } - } + requestTokens(); - set_scheduled_callback(std::bind(&ESP_Signer::tokenProcessingTask, this)); + _token_processing_task_enable = false; + config->signer.step = esp_signer_jwt_generation_step_begin; + ret = true; + } } -#endif + config->signer.tokenTaskRunning = false; } void ESP_Signer::setTokenError(int code) @@ -419,7 +349,7 @@ void ESP_Signer::setTokenError(int code) if (config->signer.tokens.error.message.length() == 0) { - config->_int.esp_signer_processing = false; + config->internal.esp_signer_processing = false; switch (code) { case ESP_SIGNER_ERROR_TOKEN_SET_TIME: @@ -452,6 +382,9 @@ void ESP_Signer::setTokenError(int code) case ESP_SIGNER_ERROR_HTTP_CODE_REQUEST_TIMEOUT: config->signer.tokens.error.message += esp_signer_pgm_str_30; break; + case ESP_SIGNER_ERROR_NTP_SYNC_TIMED_OUT: + config->signer.tokens.error.message += esp_signer_pgm_str_116; + break; default: break; @@ -463,10 +396,10 @@ bool ESP_Signer::sdBegin(int8_t ss, int8_t sck, int8_t miso, int8_t mosi) { 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) @@ -491,9 +424,9 @@ bool ESP_Signer::sdMMCBegin(const char *mountpoint, bool mode1bit, bool format_i #if defined(CARD_TYPE_SD_MMC) 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); #endif @@ -510,7 +443,7 @@ bool ESP_Signer::handleSignerError(int code, int httpCode) case 1: config->signer.tokens.error.message.clear(); setTokenError(ESP_SIGNER_ERROR_TCP_ERROR_NOT_CONNECTED); - config->_int.esp_signer_last_jwt_generation_error_cb_millis = 0; + config->internal.esp_signer_last_jwt_generation_error_cb_millis = 0; sendTokenStatusCB(); break; case 2: @@ -520,7 +453,7 @@ bool ESP_Signer::handleSignerError(int code, int httpCode) config->signer.tokens.error.message.clear(); setTokenError(ESP_SIGNER_ERROR_TCP_ERROR_CONNECTION_LOST); - config->_int.esp_signer_last_jwt_generation_error_cb_millis = 0; + config->internal.esp_signer_last_jwt_generation_error_cb_millis = 0; sendTokenStatusCB(); break; case 3: @@ -538,7 +471,7 @@ bool ESP_Signer::handleSignerError(int code, int httpCode) errorToString(httpCode, config->signer.tokens.error.message); setTokenError(httpCode); } - config->_int.esp_signer_last_jwt_generation_error_cb_millis = 0; + config->internal.esp_signer_last_jwt_generation_error_cb_millis = 0; sendTokenStatusCB(); break; @@ -558,7 +491,7 @@ bool ESP_Signer::handleSignerError(int code, int httpCode) config->signer.json = NULL; config->signer.result = NULL; - config->_int.esp_signer_processing = false; + config->internal.esp_signer_processing = false; if (code > 0 && code < 4) { @@ -570,9 +503,8 @@ bool ESP_Signer::handleSignerError(int code, int httpCode) { config->signer.tokens.error.message.clear(); config->signer.tokens.status = esp_signer_token_status_ready; - config->signer.attempts = 0; config->signer.step = esp_signer_jwt_generation_step_begin; - config->_int.esp_signer_last_jwt_generation_error_cb_millis = 0; + config->internal.esp_signer_last_jwt_generation_error_cb_millis = 0; if (code == 0) sendTokenStatusCB(); return true; @@ -589,9 +521,9 @@ void ESP_Signer::sendTokenStatusCB() if (config->token_status_callback) { - if (millis() - config->_int.esp_signer_last_jwt_generation_error_cb_millis > config->timeout.tokenGenerationError || config->_int.esp_signer_last_jwt_generation_error_cb_millis == 0) + if (millis() - config->internal.esp_signer_last_jwt_generation_error_cb_millis > config->timeout.tokenGenerationError || config->internal.esp_signer_last_jwt_generation_error_cb_millis == 0) { - config->_int.esp_signer_last_jwt_generation_error_cb_millis = millis(); + config->internal.esp_signer_last_jwt_generation_error_cb_millis = millis(); config->token_status_callback(tokenInfo); } } @@ -608,7 +540,7 @@ bool ESP_Signer::parseJsonResponse(PGM_P key_path) bool ESP_Signer::handleTokenResponse(int &httpCode) { - if (config->_int.esp_signer_reconnect_wifi) + if (config->internal.esp_signer_reconnect_wifi) ut->reconnect(0); if (WiFi.status() != WL_CONNECTED && !ut->ethLinkUp(&config->spi_ethernet_module)) @@ -652,7 +584,7 @@ bool ESP_Signer::handleTokenResponse(int &httpCode) { ut->idle(); - if (config->_int.esp_signer_reconnect_wifi) + if (config->internal.esp_signer_reconnect_wifi) ut->reconnect(0); if (WiFi.status() != WL_CONNECTED && !ut->ethLinkUp(&config->spi_ethernet_module)) @@ -769,7 +701,7 @@ bool ESP_Signer::createJWT() config->signer.tokens.status = esp_signer_token_status_on_signing; config->signer.tokens.error.code = 0; config->signer.tokens.error.message.clear(); - config->_int.esp_signer_last_jwt_generation_error_cb_millis = 0; + config->internal.esp_signer_last_jwt_generation_error_cb_millis = 0; sendTokenStatusCB(); config->signer.json = new FirebaseJson(); @@ -1049,7 +981,7 @@ bool ESP_Signer::createJWT() bool ESP_Signer::requestTokens() { - if (config->_int.esp_signer_reconnect_wifi) + if (config->internal.esp_signer_reconnect_wifi) ut->reconnect(0); if (WiFi.status() != WL_CONNECTED && !ut->ethLinkUp(&config->spi_ethernet_module)) @@ -1057,14 +989,14 @@ bool ESP_Signer::requestTokens() ut->idle(); - if (config->signer.tokens.status == esp_signer_token_status_on_request || config->signer.tokens.status == esp_signer_token_status_on_refresh || time(nullptr) < ut->default_ts || config->_int.esp_signer_processing) + if (config->signer.tokens.status == esp_signer_token_status_on_request || config->signer.tokens.status == esp_signer_token_status_on_refresh || time(nullptr) < ESP_DEFAULT_TS || config->internal.esp_signer_processing) return false; config->signer.tokens.status = esp_signer_token_status_on_request; - config->_int.esp_signer_processing = true; + config->internal.esp_signer_processing = true; config->signer.tokens.error.code = 0; config->signer.tokens.error.message.clear(); - config->_int.esp_signer_last_jwt_generation_error_cb_millis = 0; + config->internal.esp_signer_last_jwt_generation_error_cb_millis = 0; sendTokenStatusCB(); config->signer.wcs = new ESP_SIGNER_TCP_Client(); @@ -1166,7 +1098,7 @@ bool ESP_Signer::requestTokens() tokenInfo.status = config->signer.tokens.status; tokenInfo.type = config->signer.tokens.token_type; tokenInfo.error = config->signer.tokens.error; - config->_int.esp_signer_last_jwt_generation_error_cb_millis = 0; + config->internal.esp_signer_last_jwt_generation_error_cb_millis = 0; if (error.code != 0) sendTokenStatusCB(); @@ -1222,7 +1154,7 @@ void ESP_Signer::checkToken() config->signer.tokens.status = esp_signer_token_status_uninitialized; config->signer.tokens.error.code = 0; config->signer.tokens.error.message.clear(); - config->_int.esp_signer_last_jwt_generation_error_cb_millis = 0; + config->internal.esp_signer_last_jwt_generation_error_cb_millis = 0; _token_processing_task_end_request = false; _token_processing_task_enable = false; } diff --git a/src/lib/ESPSigner/ESPSigner.h b/src/lib/ESPSigner/ESPSigner.h index 9d81d1a..5352f44 100644 --- a/src/lib/ESPSigner/ESPSigner.h +++ b/src/lib/ESPSigner/ESPSigner.h @@ -1,5 +1,5 @@ /** - * 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. * @@ -7,7 +7,7 @@ * * 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) diff --git a/src/lib/ESPSigner/SignerConst.h b/src/lib/ESPSigner/SignerConst.h index 1c65f22..c42a00c 100644 --- a/src/lib/ESPSigner/SignerConst.h +++ b/src/lib/ESPSigner/SignerConst.h @@ -1,6 +1,6 @@ /** - * Created April 18, 2022 + * Created April 23, 2022 * * * The MIT License (MIT) @@ -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; @@ -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; @@ -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 @@ -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; @@ -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"; diff --git a/src/lib/ESPSigner/SignerUtils.h b/src/lib/ESPSigner/SignerUtils.h index 9824257..efa1fe9 100644 --- a/src/lib/ESPSigner/SignerUtils.h +++ b/src/lib/ESPSigner/SignerUtils.h @@ -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) @@ -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; @@ -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 @@ -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) @@ -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) @@ -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); } @@ -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 @@ -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 @@ -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 @@ -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) { @@ -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(); } } } diff --git a/src/lib/ESPSigner/wcs/HTTPCode.h b/src/lib/ESPSigner/wcs/HTTPCode.h index 16271e4..08a5beb 100644 --- a/src/lib/ESPSigner/wcs/HTTPCode.h +++ b/src/lib/ESPSigner/wcs/HTTPCode.h @@ -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) @@ -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