Skip to content

Commit

Permalink
Fix ESP32 Core v3.x.x warning and some improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Jan 8, 2025
1 parent c51b822 commit 5df0c69
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mobizt/FirebaseClient/.github%2Fworkflows%2Fcompile_library.yml?logo=github&label=compile) [![Github Stars](https://img.shields.io/github/stars/mobizt/FirebaseClient?logo=github)](https://github.com/mobizt/FirebaseClient/stargazers) ![Github Issues](https://img.shields.io/github/issues/mobizt/FirebaseClient?logo=github)

![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.4.15-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)
![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.4.16-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)

[![GitHub Sponsors](https://img.shields.io/github/sponsors/mobizt?logo=github)](https://github.com/sponsors/mobizt)

Revision `2025-01-07T12:14:01Z`
Revision `2025-01-08T04:07:12Z`

## Table of Contents

Expand Down Expand Up @@ -1043,7 +1043,7 @@ ServiceAuth::ServiceAuth(<TimeStatusCallback>, <file_config_data>, <expire>)
The auth token need to be re-created instead of refreshing.
The `CustomAuth` and `ServiceAuth` classes required the JWT token processor which is done via the function `JWTClass::loop(<auth_data_t*>)` which accepts the pointer to the `auth_data_t` from the `FirebaseApp::getAuth()`.
The `CustomAuth` and `ServiceAuth` classes required the JWT token processor which is done via the function `firebase::JWTClass::loop(<auth_data_t*>)` which accepts the pointer to the `auth_data_t` from the `FirebaseApp::getAuth()`.
The examples in this library, the static object of `JWTClass` called `JWT` will be used to save the stack memory usage and it is not thread safe when using in multi-threaded operations (`multi-FirebaseApp`) because of sharing internal `jwt_token_data_t`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void printResult(AsyncResult &aResult);

DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection

// Don't forget to call firebase::JWTClass::loop() in the main loop(), otherwise the JWT token process cannot be started.
CustomAuth custom_auth(timeStatusCB, API_KEY, FIREBASE_CLIENT_EMAIL, FIREBASE_PROJECT_ID, PRIVATE_KEY, "myId" /* UID */, "" /* scope */, "" /* claims */, 3600 /* expire period in seconds (<3600) */);

FirebaseApp app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ DefaultNetwork network; // initilize with boolean parameter to enable/disable ne

FileConfig sa_file("/sa_file.json", fileCallback);

// Don't forget to call firebase::JWTClass::loop() in the main loop(), otherwise the JWT token process cannot be started.
CustomAuth sa_file_auth(timeStatusCB, getFile(sa_file), API_KEY, "myId");

FirebaseApp app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void printResult(AsyncResult &aResult);

DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection

// Don't forget to call firebase::JWTClass::loop() in the main loop(), otherwise the JWT token process cannot be started.
ServiceAuth sa_auth(timeStatusCB, FIREBASE_CLIENT_EMAIL, FIREBASE_PROJECT_ID, PRIVATE_KEY, 3000 /* expire period in seconds (<3600) */);

FirebaseApp app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode);

FileConfig sa_file("/sa_file.json", fileCallback);

// Don't forget to call firebase::JWTClass::loop() in the main loop(), otherwise the JWT token process cannot be started.
ServiceAuth sa_file_auth(timeStatusCB, getFile(sa_file));
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ void printResult(AsyncResult &aResult);

DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection

// Don't forget to call firebase::JWTClass::loop() in the main loop(), otherwise the JWT token process cannot be started.
CustomAuth custom_auth(timeStatusCB, API_KEY, FIREBASE_CLIENT_EMAIL, FIREBASE_PROJECT_ID, PRIVATE_KEY, "myId" /* UID */, "" /* scope */, "" /* claims */, 3600 /* expire period in seconds (<3600) */);

FirebaseApp app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ DefaultNetwork network; // initilize with boolean parameter to enable/disable ne

FileConfig sa_file("/sa_file.json", fileCallback);

// Don't forget to call firebase::JWTClass::loop() in the main loop(), otherwise the JWT token process cannot be started.
CustomAuth sa_file_auth(timeStatusCB, getFile(sa_file), API_KEY, "myId");

FirebaseApp app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void printResult(AsyncResult &aResult);

DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection

// Don't forget to call firebase::JWTClass::loop() in the main loop(), otherwise the JWT token process cannot be started.
ServiceAuth sa_auth(timeStatusCB, FIREBASE_CLIENT_EMAIL, FIREBASE_PROJECT_ID, PRIVATE_KEY, 3000 /* expire period in seconds (<3600) */);

FirebaseApp app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode);

FileConfig sa_file("/sa_file.json", fileCallback);

// Don't forget to call firebase::JWTClass::loop() in the main loop(), otherwise the JWT token process cannot be started.
ServiceAuth sa_file_auth(timeStatusCB, getFile(sa_file));
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void printResult(AsyncResult &aResult);

DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection

// Don't forget to call firebase::JWTClass::loop() in the main loop(), otherwise the JWT token process cannot be started.
CustomAuth custom_auth(timeStatusCB, API_KEY, FIREBASE_CLIENT_EMAIL, FIREBASE_PROJECT_ID, PRIVATE_KEY, "myId" /* UID */, "" /* scope */, "" /* claims */, 3600 /* expire period in seconds (<3600) */);

FirebaseApp app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ DefaultNetwork network; // initilize with boolean parameter to enable/disable ne

FileConfig sa_file("/sa_file.json", fileCallback);

// Don't forget to call firebase::JWTClass::loop() in the main loop(), otherwise the JWT token process cannot be started.
CustomAuth sa_file_auth(timeStatusCB, getFile(sa_file), API_KEY, "myId");

FirebaseApp app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void printResult(AsyncResult &aResult);

DefaultNetwork network; // initilize with boolean parameter to enable/disable network reconnection

// Don't forget to call firebase::JWTClass::loop() in the main loop(), otherwise the JWT token process cannot be started.
ServiceAuth sa_auth(timeStatusCB, FIREBASE_CLIENT_EMAIL, FIREBASE_PROJECT_ID, PRIVATE_KEY, 3000 /* expire period in seconds (<3600) */);

FirebaseApp app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void fileCallback(File &file, const char *filename, file_operating_mode mode);

FileConfig sa_file("/sa_file.json", fileCallback);

// Don't forget to call firebase::JWTClass::loop() in the main loop(), otherwise the JWT token process cannot be started.
ServiceAuth sa_file_auth(timeStatusCB, getFile(sa_file));
#endif

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": "FirebaseClient",
"version": "1.4.15",
"version": "1.4.16",
"keywords": "communication, REST, esp32, esp8266, arduino",
"description": "Async Firebase Client library for Arduino.",
"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=FirebaseClient

version=1.4.15
version=1.4.16

author=Mobizt

Expand Down
43 changes: 37 additions & 6 deletions src/core/AsyncClient/AsyncClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
bool sse = false;
String host;
uint16_t port;
bool connected = false;
std::vector<uint32_t> sVec;
Memory mem;
Base64Util b64ut;
Expand Down Expand Up @@ -526,7 +527,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
{
newCon(sData, getHost(sData, true).c_str(), sData->request.port);

if ((client_type == async_request_handler_t::tcp_client_type_sync && !client->connected()) || client_type == async_request_handler_t::tcp_client_type_async)
if ((client_type == async_request_handler_t::tcp_client_type_sync && !isConnected()) || client_type == async_request_handler_t::tcp_client_type_async)
{
ret = connect(sData, getHost(sData, true).c_str(), sData->request.port);

Expand Down Expand Up @@ -627,7 +628,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
String ext;
String _host = getHost(sData, false, &ext);
if (client)
{
client->stop();
this->connected = false;
}
if (connect(sData, _host.c_str(), sData->request.port) > function_return_type_failure)
{
URLUtil uut;
Expand Down Expand Up @@ -1424,10 +1428,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
sData->aResult.conn_ms = millis();
resetDebug(app_debug);

if (client && !client->connected() && !sData->auth_used) // This info is already show in auth task
if (!isConnected() && !sData->auth_used) // This info is already show in auth task
setDebugBase(app_debug, FPSTR("Connecting to server..."));

if (client && !client->connected() && client_type == async_request_handler_t::tcp_client_type_sync)
if (!isConnected() && client_type == async_request_handler_t::tcp_client_type_sync)
sData->return_type = client->connect(host, port) > 0 ? function_return_type_complete : function_return_type_failure;
else if (client_type == async_request_handler_t::tcp_client_type_async)
{
Expand Down Expand Up @@ -1455,8 +1459,9 @@ class AsyncClientClass : public ResultBase, RTDBResultBase

this->host = host;
this->port = port;
this->connected = sData->return_type == function_return_type_complete;

if (client && client->connected() && session_timeout_sec >= FIREBASE_SESSION_TIMEOUT_SEC)
if (isConnected() && session_timeout_sec >= FIREBASE_SESSION_TIMEOUT_SEC)
session_timer.feed(session_timeout_sec);

return sData->return_type;
Expand Down Expand Up @@ -2048,9 +2053,31 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
inStopAsync = false;
}

bool isConnected()
{
// Re-check the client status only when the connected flag was set.
// This prevents calling client's connected() if it was not yet connected.
// This also prevents ESP32 Arduino Core v3.x.x's WiFiClientSecure connected() method
// (error) warning when it was trying to read 0 byte from, and set the socket option to
// the unopen socket.
if (this->connected)
{
if (client_type == async_request_handler_t::tcp_client_type_sync)
this->connected = client && client->connected();
else if (client_type == async_request_handler_t::tcp_client_type_async)
{
#if defined(ENABLE_ASYNC_TCP_CLIENT)
if (async_tcp_config && async_tcp_config->tcpStatus)
async_tcp_config->tcpStatus(this->connected);
#endif
}
}
return this->connected;
}

void stop(async_data_item_t *sData)
{
if (client && client->connected())
if (isConnected())
setDebugBase(app_debug, FPSTR("Terminating the server connection..."));

if (client_type == async_request_handler_t::tcp_client_type_sync)
Expand All @@ -2067,7 +2094,8 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
}

clear(host);
port = 0;
this->port = 0;
this->connected = false;
client_changed = false;
network_changed = false;
}
Expand Down Expand Up @@ -2538,7 +2566,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase

// Some changes, stop the current network client.
if (client_changed && this->client)
{
this->client->stop();
this->connected = false;
}

// Change the network interface.
// Should not check the type changes, just overwrite
Expand Down
2 changes: 1 addition & 1 deletion src/core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#undef FIREBASE_CLIENT_VERSION
#endif

#define FIREBASE_CLIENT_VERSION "1.4.15"
#define FIREBASE_CLIENT_VERSION "1.4.16"

static void sys_idle()
{
Expand Down

0 comments on commit 5df0c69

Please sign in to comment.