Skip to content

Commit 23f28a8

Browse files
committed
chore: Improve connection awareness
1 parent a6fd3e2 commit 23f28a8

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

src/Arduino_NotecardConnectionHandler.cpp

+50-19
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,10 @@ int NotecardConnectionHandler::setWiFiCredentials (const String & ssid_, const S
162162
int result;
163163

164164
// Validate the connection state is not in an initialization state
165-
if (check() == NetworkConnectionState::INIT)
165+
const NetworkConnectionState current_net_connection_state = check();
166+
if (NetworkConnectionState::INIT == current_net_connection_state)
166167
{
167-
Debug.print(DBG_ERROR, F("Failed to set WiFi credentials. Connection has not been initialized."));
168+
Debug.print(DBG_ERROR, F("Unable to set WiFi credentials. Connection to Notecard uninitialized."));
168169
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
169170
} else if (J *req = _notecard.newRequest("card.wifi")) {
170171
JAddStringToObject(req, "ssid", ssid_.c_str());
@@ -195,10 +196,12 @@ int NotecardConnectionHandler::setWiFiCredentials (const String & ssid_, const S
195196

196197
const String & NotecardConnectionHandler::syncArduinoDeviceId (const String & device_id_)
197198
{
198-
// Validate the connection state is not in an initialization state
199-
if (check() == NetworkConnectionState::INIT)
199+
// Validate the connection state is not uninitialized or in error state
200+
const NetworkConnectionState current_net_connection_state = check();
201+
if ((NetworkConnectionState::INIT == current_net_connection_state)
202+
|| (NetworkConnectionState::ERROR == current_net_connection_state))
200203
{
201-
Debug.print(DBG_ERROR, F("Failed to sync Arduino Device ID. Connection has not been initialized."));
204+
Debug.print(DBG_ERROR, F("Unable to sync Arduino Device ID. Connection to Notecard uninitialized or in error state."));
202205
return device_id_;
203206
}
204207

@@ -249,23 +252,30 @@ int NotecardConnectionHandler::syncSecretDeviceKey (const String & secret_device
249252
{
250253
int result;
251254

252-
// Validate the connection state is not in an initialization state
253-
if (check() == NetworkConnectionState::INIT)
255+
// Validate the connection state is not uninitialized or in error state
256+
const NetworkConnectionState current_net_connection_state = check();
257+
if ((NetworkConnectionState::INIT == current_net_connection_state)
258+
|| (NetworkConnectionState::ERROR == current_net_connection_state))
254259
{
255-
Debug.print(DBG_ERROR, F("Failed to sync Secret Device Key. Connection has not been initialized."));
260+
Debug.print(DBG_ERROR, F("Unable to sync Secret Device Key. Connection to Notecard uninitialized or in error state."));
256261
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
257262
} else if (J *req = _notecard.newRequest("var.set")) {
258263
JAddStringToObject(req, "file", NOTEFILE_SECURE_DATABASE);
259264
JAddStringToObject(req, "name", "secret_device_key");
260265
if (secret_device_key_.length() > 0) {
261266
JAddStringToObject(req, "text", secret_device_key_.c_str());
262267
}
263-
JAddBoolToObject(req, "live", true);
264-
JAddBoolToObject(req, "sync", true);
268+
if (NetworkConnectionState::CONNECTED == current_net_connection_state) {
269+
JAddBoolToObject(req, "live", true);
270+
JAddBoolToObject(req, "sync", true);
271+
}
265272
if (J *rsp = _notecard.requestAndResponse(req)) {
266273
// Check the response for errors
267274
if (NoteResponseError(rsp)) {
268275
const char *err = JGetString(rsp, "err");
276+
if (NoteErrorContains(err, "{hub-not-connected}")) {
277+
// _current_net_connection_state = NetworkConnectionState::DISCONNECTED;
278+
}
269279
Debug.print(DBG_ERROR, F("%s"), err);
270280
Debug.print(DBG_ERROR, F("Failed to sync Secret Device Key."));
271281
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
@@ -372,13 +382,20 @@ int NotecardConnectionHandler::write(const uint8_t * buf_, size_t size_)
372382
{
373383
int result;
374384

375-
if (J * req = _notecard.newRequest("note.add")) {
385+
// Validate the connection state is not uninitialized or in error state
386+
const NetworkConnectionState current_net_connection_state = check();
387+
if ((NetworkConnectionState::INIT == current_net_connection_state)
388+
|| (NetworkConnectionState::ERROR == current_net_connection_state))
389+
{
390+
Debug.print(DBG_ERROR, F("Unable to write message. Connection to Notecard uninitialized or in error state."));
391+
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
392+
} else if (J * req = _notecard.newRequest("note.add")) {
376393
JAddStringToObject(req, "file", NOTEFILE_SECURE_OUTBOUND);
377394
if (buf_) {
378395
JAddBinaryToObject(req, "payload", buf_, size_);
379396
}
380-
// Queue the Note when `_keep_alive` is disabled
381-
if (_keep_alive) {
397+
// Queue the Note when `_keep_alive` is disabled or not connected to Notehub
398+
if (_keep_alive && (NetworkConnectionState::CONNECTED == current_net_connection_state)) {
382399
JAddBoolToObject(req, "live", true);
383400
JAddBoolToObject(req, "sync", true);
384401
}
@@ -387,6 +404,9 @@ int NotecardConnectionHandler::write(const uint8_t * buf_, size_t size_)
387404
J * rsp = _notecard.requestAndResponse(req);
388405
if (NoteResponseError(rsp)) {
389406
const char *err = JGetString(rsp, "err");
407+
if (NoteErrorContains(err, "{hub-not-connected}")) {
408+
// _current_net_connection_state = NetworkConnectionState::DISCONNECTED;
409+
}
390410
Debug.print(DBG_ERROR, F("%s\n"), err);
391411
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
392412
} else {
@@ -786,11 +806,11 @@ bool NotecardConnectionHandler::configureConnection (bool connect_) const
786806
// Configure the connection mode based on the `connect_` parameter
787807
if (connect_) {
788808
JAddStringToObject(req, "mode", "continuous");
789-
JAddIntToObject(req, "inbound", 15); // Unnecessary fail safe value
809+
JAddIntToObject(req, "inbound", 15); // Fail-safe (theoretically unnecessary)
790810
JAddBoolToObject(req, "sync", true);
791811
} else {
792812
JAddStringToObject(req, "mode", "periodic");
793-
JAddIntToObject(req, "inbound", 1440); //TODO: Revisit this value
813+
JAddIntToObject(req, "inbound", 1440); // Once daily
794814
JAddIntToObject(req, "outbound", -1);
795815
JAddStringToObject(req, "vinbound", "-");
796816
JAddStringToObject(req, "voutbound", "-");
@@ -936,19 +956,27 @@ int NotecardConnectionHandler::initiateNotehubSync (void) const
936956
return result;
937957
}
938958

939-
int NotecardConnectionHandler::notehubLogging (bool enable_) const
959+
int NotecardConnectionHandler::notehubLogging (bool enable_) /* const - disabled by `check()` */
940960
{
941961
int result;
962+
Debug.print(DBG_INFO, F("%sabling Notehub logging..."), (enable_ ? "En" : "Dis"));
942963

943-
if (J * req = _notecard.newRequest("note.add")) {
964+
// Validate the connection state is not uninitialized or in error state
965+
const NetworkConnectionState current_net_connection_state = check();
966+
if ((NetworkConnectionState::INIT == current_net_connection_state)
967+
|| (NetworkConnectionState::ERROR == current_net_connection_state))
968+
{
969+
Debug.print(DBG_ERROR, F("Unable to %sable Notehub logging. Connection to Notecard uninitialized or in error state."), (enable_ ? "en" : "dis"));
970+
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
971+
} else if (J * req = _notecard.newRequest("note.add")) {
944972
JAddStringToObject(req, "file", NOTEFILE_SECURE_OUTBOUND);
945973
if (enable_) {
946974
JAddBinaryToObject(req, "payload", "1", sizeof("1"));
947975
} else {
948976
JAddBinaryToObject(req, "payload", "0", sizeof("0"));
949977
}
950-
// Queue the Note when `_keep_alive` is disabled
951-
if (_keep_alive) {
978+
// Queue the Note when `_keep_alive` is disabled or not connected to Notehub
979+
if (_keep_alive && (NetworkConnectionState::CONNECTED == current_net_connection_state)) {
952980
JAddBoolToObject(req, "live", true);
953981
JAddBoolToObject(req, "sync", true);
954982
}
@@ -957,6 +985,9 @@ int NotecardConnectionHandler::notehubLogging (bool enable_) const
957985
J * rsp = _notecard.requestAndResponse(req);
958986
if (NoteResponseError(rsp)) {
959987
const char *err = JGetString(rsp, "err");
988+
if (NoteErrorContains(err, "{hub-not-connected}")) {
989+
// _current_net_connection_state = NetworkConnectionState::DISCONNECTED;
990+
}
960991
Debug.print(DBG_ERROR, F("%s\n"), err);
961992
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
962993
} else {

src/Arduino_NotecardConnectionHandler.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
a commercial license, send an email to [email protected].
1616
*/
1717

18-
//TODO: Understand how `_keep_alive` is updated and used in the `ConnectionHandler` class
19-
2018
#ifndef ARDUINO_NOTECARD_CONNECTION_HANDLER_H_
2119
#define ARDUINO_NOTECARD_CONNECTION_HANDLER_H_
2220

@@ -74,14 +72,12 @@ class NotecardConnectionHandler final : public ConnectionHandler
7472
);
7573

7674
// Notehub Logging
77-
inline int disableNotehubLogging (void) const
75+
inline int disableNotehubLogging (void) /* const */
7876
{
79-
Debug.print(DBG_INFO, F("Disabling Notehub logging..."));
8077
return notehubLogging(false);
8178
}
82-
inline int enableNotehubLogging (void) const
79+
inline int enableNotehubLogging (void) /* const */
8380
{
84-
Debug.print(DBG_INFO, F("Enabling Notehub logging..."));
8581
return notehubLogging(true);
8682
}
8783

@@ -142,7 +138,7 @@ class NotecardConnectionHandler final : public ConnectionHandler
142138
uint_fast8_t connected (void) const;
143139
J * getNote (bool pop = false) const;
144140
int initiateNotehubSync (void) const;
145-
int notehubLogging (bool enable) const;
141+
int notehubLogging (bool enable) /* const */;
146142
bool updateUidCache (void);
147143
};
148144

0 commit comments

Comments
 (0)