From 21e31893f2ded55f23c666c0eb509f0952505684 Mon Sep 17 00:00:00 2001 From: Marcel Schulz Date: Fri, 15 Dec 2017 00:03:49 +0100 Subject: [PATCH] Did some corrections regarding the new ping method. Now everything should work as mentioned. --- library.properties | 2 +- src/SBNetwork.cpp | 52 +++++++++++++++++++++++++++--------------- src/SBNetwork.h | 4 +++- src/SBNetwork_config.h | 2 +- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/library.properties b/library.properties index c74331b..c63e22b 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SBNetwork -version=1.0.3 +version=1.0.4 author=Schullebernd,schullebernd@googlemail.com maintainer=Schullebernd,schullebernd@googlemail.com sentence=Build simple master/client networks with nRF24L01 transmitter. diff --git a/src/SBNetwork.cpp b/src/SBNetwork.cpp index 4ad4942..0157e63 100644 --- a/src/SBNetwork.cpp +++ b/src/SBNetwork.cpp @@ -408,28 +408,14 @@ bool SBNetwork::connectToNetwork(){ } bool bMasterAvailable = this->pingDevice(this->NetworkDevice.MasterMAC); - if (bMasterAvailable) { - long lNow = millis(); - SBNetworkFrame pingAckFrame; - while ((lNow + 100) > millis()) { - if (this->receiveInternal(&pingAckFrame)) { - if (pingAckFrame.Header.CommandType == SB_COMMAND_PING) { - Serial.println(F("Done - Master available")); - return true; - } - } - } - Serial.println(F("Failed - Master not responding")); - } - Serial.println("Error - Sending Ping to Master"); - return false; + return bMasterAvailable; } else { return false; } } -bool SBNetwork::pingDevice(SBMacAddress mac){ +bool SBNetwork::pingDeviceInternal(SBMacAddress mac, bool waitForAck) { SBNetworkHeader header; header.ToAddress = mac; header.FromAddress = this->NetworkDevice.MAC; @@ -443,7 +429,35 @@ bool SBNetwork::pingDevice(SBMacAddress mac){ frame.Message = NULL; frame.MessageSize = 0; - return this->sendToDevice(frame); + bool bSend = this->sendToDevice(frame); + if (bSend) { + if (waitForAck) { + long lNow = millis(); + SBNetworkFrame pingAckFrame; + while ((lNow + 100) > millis()) { + if (this->receiveInternal(&pingAckFrame)) { + if (pingAckFrame.Header.CommandType == SB_COMMAND_PING) { + Serial.println(F("Done - Device available")); + return true; + } + } + } +#if defined(_DEBUG) + Serial.println(F("Failed - Device not responding")); +#endif + return false; + } + } +#if defined(_DEBUG) + else { + Serial.println("Failed - Device not responding"); + } +#endif + return bSend; +} + +bool SBNetwork::pingDevice(SBMacAddress mac){ + return pingDeviceInternal(mac, true); } bool SBNetwork::handleCommandPackage(SBNetworkFrame *frame){ @@ -473,7 +487,7 @@ bool SBNetwork::handleCommandPackage(SBNetworkFrame *frame){ Serial.println(F("Received 'PING'")); #endif if (bFound) { - pingDevice(frame->Header.FromAddress); + pingDeviceInternal(frame->Header.FromAddress, false); } break; } @@ -614,7 +628,7 @@ void SBNetwork::update(){ if (NetworkDevice.ConnectedToMaster && MASTER_CHECK_INTERVAL) { if (_Uptime > _NextCheck) { // Now we have to check our sensors if they are still available - _NextCheck += MASTER_CHECK_INTERVAL; + _NextCheck = _Uptime + MASTER_CHECK_INTERVAL; checkMaster(); } } diff --git a/src/SBNetwork.h b/src/SBNetwork.h index 76e456f..ec16068 100644 --- a/src/SBNetwork.h +++ b/src/SBNetwork.h @@ -2,7 +2,7 @@ #ifndef _SB_NETWORK_ #define _SB_NETWORK_ -#define SB_VERSION "1.0.3" +#define SB_VERSION "1.0.4" #include #include @@ -80,6 +80,8 @@ class SBNetwork{ bool receiveMessage(void **message, uint8_t *messageSize, SBMacAddress *mac); + bool pingDeviceInternal(SBMacAddress mac, bool waitForAck); + public: /* * Define the standard addresses for the sensor network diff --git a/src/SBNetwork_config.h b/src/SBNetwork_config.h index 29845c7..d0ea475 100644 --- a/src/SBNetwork_config.h +++ b/src/SBNetwork_config.h @@ -7,7 +7,7 @@ //#define _DEBUG -#define MASTER_CHECK_INTERVAL 0 // All sensors will ping the master every xxx milliseconds. if set to 0, they will not ping the master +#define MASTER_CHECK_INTERVAL 0 // All slaves will ping the master every xxx milliseconds. if set to 0, they will not ping the master #define MAX_CLIENTS 10