Changed the lib architecture. Now you don't need to change a define in the SBNetwork_config.h to change the device type from Master to Client. You can do this in the constructor ob SBNetwork class. The code get a little bit bigger now, because the lib always compiles for both master and client device. But it is definitively better to use now.

This commit is contained in:
Marcel Schulz 2017-11-21 23:50:29 +01:00
parent 1128d8c3c4
commit a99d91c30b
5 changed files with 322 additions and 315 deletions

View File

@ -1,5 +1,5 @@
#ifndef _SB_SENSOR_NETWORK_DEVCIE_ #ifndef _SB_NETWORK_DEVCIE_
#define _SB_SENSOR_NETWORK_DEVCIE_ #define _SB_NETWORK_DEVCIE_
#include <EEPROM.h> #include <EEPROM.h>
@ -12,8 +12,6 @@ public:
uint32_t NetworkKey; uint32_t NetworkKey;
}; };
#ifdef RUN_AS_MASTER
class SBMasterStorage{ class SBMasterStorage{
public: public:
SBMasterStorage(){}; SBMasterStorage(){};
@ -53,5 +51,3 @@ public:
}; };
#endif #endif
#endif

View File

@ -23,14 +23,13 @@ void printDeviceData(SBNetworkDevice &device){
Serial.print(F("Master MAC = ")); Serial.print(F("Master MAC = "));
printAddress(device.MasterMAC.Bytes); printAddress(device.MasterMAC.Bytes);
Serial.println(""); Serial.println("");
#ifdef RUN_AS_MASTER
Serial.print(F("NetKey = ")); Serial.print(F("NetKey = "));
Serial.print(device.NetworkKey, DEC); Serial.print(device.NetworkKey, DEC);
Serial.println(""); Serial.println("");
#endif
} }
SBNetwork::SBNetwork(uint8_t cePin, uint8_t csPin) : radio(cePin, csPin){ SBNetwork::SBNetwork(bool client, uint8_t cePin, uint8_t csPin) : radio(cePin, csPin){
RunAsClient = client;
} }
void SBNetwork::initialize(SBMacAddress mac){ void SBNetwork::initialize(SBMacAddress mac){
@ -45,14 +44,14 @@ void SBNetwork::initialize(SBMacAddress mac){
this->initializeNetworkDevice(NetworkDevice, mac); this->initializeNetworkDevice(NetworkDevice, mac);
#if defined(RUN_AS_MASTER) if (!this->RunAsClient) {
this->_MasterStorage = SBMasterStorage::initialize(); this->MasterStorage = SBMasterStorage::initialize();
for (uint8_t i = 0; i < MAX_CLIENTS; i++) { for (uint8_t i = 0; i < MAX_CLIENTS; i++) {
Serial.print("Masterstorage Slot "); Serial.print(i); Serial.print(" "); Serial.print("Masterstorage Slot "); Serial.print(i); Serial.print(" ");
printAddress(_MasterStorage.Slaves[i]); printAddress(MasterStorage.Slaves[i]);
Serial.println(); Serial.println();
} }
#endif }
Serial.print(F("Initializing NRF24L01 transmitter...")); Serial.print(F("Initializing NRF24L01 transmitter..."));
this->radio.begin(); this->radio.begin();
@ -77,14 +76,14 @@ void SBNetwork::initialize(SBMacAddress mac){
this->radio.startListening(); this->radio.startListening();
Serial.println(F("Done")); Serial.println(F("Done"));
#ifndef RUN_AS_MASTER // In case of we defined a client device if (this->RunAsClient) {
// Connect to a master // Connect to a master
_Connected = false; _Connected = false;
while (!_Connected) { while (!_Connected) {
_Connected = connectToNetwork(); _Connected = connectToNetwork();
delay(500); // This can be an endless loop in case of no connection to master is available delay(500); // This can be an endless loop in case of no connection to master is available
} }
#endif }
} }
void SBNetwork::initializeNetworkDevice(SBNetworkDevice &device, SBMacAddress mac){ void SBNetwork::initializeNetworkDevice(SBNetworkDevice &device, SBMacAddress mac){
@ -303,11 +302,11 @@ bool SBNetwork::receiveMessage(void **message, uint8_t *messageSize, SBMacAddres
return false; return false;
} }
#ifndef RUN_AS_MASTER
bool SBNetwork::connectToNetwork(){ bool SBNetwork::connectToNetwork(){
if (this->RunAsClient) {
Serial.print(F("Try to connect to master...")); Serial.print(F("Try to connect to master..."));
// First we have to check, if we already have a master stored // First we have to check, if we already have a master stored
if (!this->NetworkDevice.ConnectedToMaster){ if (!this->NetworkDevice.ConnectedToMaster) {
Serial.println("Warning - Not paired to a master"); Serial.println("Warning - Not paired to a master");
Serial.print(F("Sending broadcast transmission to find a master...")); Serial.print(F("Sending broadcast transmission to find a master..."));
// If not, we have to search for a master // If not, we have to search for a master
@ -325,26 +324,26 @@ bool SBNetwork::connectToNetwork(){
bool bMasterAck = this->sendToDevice(frame); bool bMasterAck = this->sendToDevice(frame);
unsigned long started_waiting_at = millis(); unsigned long started_waiting_at = millis();
boolean timeout = false; boolean timeout = false;
while (!this->receive(&frame)){ while (!this->receive(&frame)) {
if ((millis() - started_waiting_at) > 1000){ if ((millis() - started_waiting_at) > 1000) {
timeout = true; timeout = true;
break; break;
} }
} }
if (timeout){ if (timeout) {
Serial.println(F("Timeout")); Serial.println(F("Timeout"));
return false; return false;
} }
else{ else {
if (frame.Header.CommandType != SBS_COMMAND_MASTER_ACK){ if (frame.Header.CommandType != SBS_COMMAND_MASTER_ACK) {
if (frame.MessageSize > 0){ if (frame.MessageSize > 0) {
free(frame.Message); free(frame.Message);
} }
Serial.println(F("Failed - Got answer but no master ack")); Serial.println(F("Failed - Got answer but no master ack"));
return false; return false;
} }
else{ else {
Serial.println(F("Done")); Serial.println(F("Done"));
Serial.print(F("Got answer from a master. Master-MAC is ")); Serial.print(F("Got answer from a master. Master-MAC is "));
printAddress(frame.Header.FromAddress); printAddress(frame.Header.FromAddress);
@ -358,12 +357,12 @@ bool SBNetwork::connectToNetwork(){
conFrame.Header.PackageId = millis(); conFrame.Header.PackageId = millis();
conFrame.Header.ToAddress = frame.Header.FromAddress; conFrame.Header.ToAddress = frame.Header.FromAddress;
conFrame.MessageSize = 0; conFrame.MessageSize = 0;
if (!this->sendToDevice(conFrame)){ if (!this->sendToDevice(conFrame)) {
Serial.println("Failed - Sending pairing request"); Serial.println("Failed - Sending pairing request");
} }
else{ else {
while (!this->receive(&frame)){ while (!this->receive(&frame)) {
if (millis() - started_waiting_at > 1000){ if (millis() - started_waiting_at > 1000) {
timeout = true; timeout = true;
break; break;
} }
@ -372,11 +371,11 @@ bool SBNetwork::connectToNetwork(){
Serial.println(F("Timeout")); Serial.println(F("Timeout"));
return false; return false;
} }
if (frame.Header.CommandType != SBS_COMMAND_PAIRING_ACK){ if (frame.Header.CommandType != SBS_COMMAND_PAIRING_ACK) {
Serial.println(F("Failed - Pairing rejected from the master")); Serial.println(F("Failed - Pairing rejected from the master"));
return false; return false;
} }
else{ else {
this->NetworkDevice.MasterMAC = frame.Header.FromAddress; this->NetworkDevice.MasterMAC = frame.Header.FromAddress;
this->NetworkDevice.NetworkKey = *(frame.Message); this->NetworkDevice.NetworkKey = *(frame.Message);
this->NetworkDevice.ConnectedToMaster = -1; this->NetworkDevice.ConnectedToMaster = -1;
@ -391,15 +390,18 @@ bool SBNetwork::connectToNetwork(){
} }
bool bMasterAvailable = this->pingDevice(this->NetworkDevice.MasterMAC); bool bMasterAvailable = this->pingDevice(this->NetworkDevice.MasterMAC);
if (bMasterAvailable){ if (bMasterAvailable) {
Serial.println(F("Done - Master available")); Serial.println(F("Done - Master available"));
} }
else{ else {
Serial.println(F("Failed - Master not responding")); Serial.println(F("Failed - Master not responding"));
} }
return bMasterAvailable; return bMasterAvailable;
}
else {
return false;
}
} }
#endif
bool SBNetwork::pingDevice(SBMacAddress mac){ bool SBNetwork::pingDevice(SBMacAddress mac){
SBNetworkHeader header; SBNetworkHeader header;
@ -419,19 +421,18 @@ bool SBNetwork::pingDevice(SBMacAddress mac){
} }
bool SBNetwork::handleCommandPackage(SBNetworkFrame *frame){ bool SBNetwork::handleCommandPackage(SBNetworkFrame *frame){
if (!this->RunAsClient) {
#if defined(RUN_AS_MASTER)
// First check, if the device is listed in the storage // First check, if the device is listed in the storage
bool bFound = false; bool bFound = false;
for (uint8_t i = 0; i < MAX_CLIENTS; i++){ for (uint8_t i = 0; i < MAX_CLIENTS; i++) {
if (this->_MasterStorage.Slaves[i].isEquals(frame->Header.FromAddress)){ if (this->MasterStorage.Slaves[i].isEquals(frame->Header.FromAddress)) {
_SlavePings[i] = _Uptime; _SlavePings[i] = _Uptime;
bFound = true; bFound = true;
break; break;
} }
} }
if (!bFound){ if (!bFound) {
// If an unknown device was detected, then never handle the network control traffic and never handle the messages // If an unknown device was detected, then never handle the network control traffic and never handle the messages
#ifdef _DEBUG #ifdef _DEBUG
Serial.print("Unknown device detected with MAC: "); Serial.print("Unknown device detected with MAC: ");
@ -440,53 +441,53 @@ bool SBNetwork::handleCommandPackage(SBNetworkFrame *frame){
#endif #endif
//return false; //return false;
} }
switch (frame->Header.CommandType){ switch (frame->Header.CommandType) {
case SBS_COMMAND_PING:{ case SBS_COMMAND_PING: {
#ifdef _DEBUG #ifdef _DEBUG
Serial.println("Received 'PING'"); Serial.println("Received 'PING'");
#endif #endif
break; break;
} }
case SBS_COMMAND_SEARCH_MASTER:{ case SBS_COMMAND_SEARCH_MASTER: {
#ifdef _DEBUG #ifdef _DEBUG
Serial.println("Received 'SEARCH_MASTER' Package. Send MasterACK..."); Serial.println("Received 'SEARCH_MASTER' Package. Send MasterACK...");
#endif #endif
delay(100); delay(100);
bool bSend = sendMasterAck(frame->Header.FromAddress); bool bSend = sendMasterAck(frame->Header.FromAddress);
if (bSend){ if (bSend) {
return false; return false;
} }
Serial.println("Done"); Serial.println("Done");
break; break;
} }
case SBS_COMMAND_REQUEST_PAIRING:{ case SBS_COMMAND_REQUEST_PAIRING: {
#ifdef _DEBUG #ifdef _DEBUG
Serial.println("Received 'PAIRING_REQUEST' Package. Send PairingACK"); Serial.println("Received 'PAIRING_REQUEST' Package. Send PairingACK");
#endif #endif
delay(100); delay(100);
// This is the point where we could stop orpcessing and wait for an user input on the controller to let the new device access the network // This is the point where we could stop orpcessing and wait for an user input on the controller to let the new device access the network
bool bSend = sendPairingAck(frame->Header.FromAddress); bool bSend = sendPairingAck(frame->Header.FromAddress);
if (bSend){ if (bSend) {
addMac(frame->Header.FromAddress); addMac(frame->Header.FromAddress);
} }
break; break;
} }
case SBS_COMMAND_NO_COMMAND: case SBS_COMMAND_NO_COMMAND:
default:{ default: {
//Serial.println("No Command received. Passing through transport layer."); //Serial.println("No Command received. Passing through transport layer.");
return bFound; return bFound;
break; break;
} }
} }
return false; return false;
#else }
else {
return true; return true;
#endif }
} }
#if defined(RUN_AS_MASTER)
bool SBNetwork::sendMasterAck(SBMacAddress mac){ bool SBNetwork::sendMasterAck(SBMacAddress mac){
if (!this->RunAsClient) {
SBNetworkHeader header; SBNetworkHeader header;
header.ToAddress = mac; header.ToAddress = mac;
header.FromAddress = this->NetworkDevice.MAC; header.FromAddress = this->NetworkDevice.MAC;
@ -499,10 +500,15 @@ bool SBNetwork::sendMasterAck(SBMacAddress mac){
frame.Message = (uint8_t*)&(this->NetworkDevice.NetworkKey); frame.Message = (uint8_t*)&(this->NetworkDevice.NetworkKey);
frame.MessageSize = sizeof(uint32_t); frame.MessageSize = sizeof(uint32_t);
return this->sendToDevice(frame); return this->sendToDevice(frame);
}
else {
return false;
}
} }
bool SBNetwork::sendPairingAck(SBMacAddress mac){ bool SBNetwork::sendPairingAck(SBMacAddress mac){
if (!this->RunAsClient) {
SBNetworkHeader header; SBNetworkHeader header;
header.ToAddress = mac; header.ToAddress = mac;
header.FromAddress = this->NetworkDevice.MAC; header.FromAddress = this->NetworkDevice.MAC;
@ -516,21 +522,27 @@ bool SBNetwork::sendPairingAck(SBMacAddress mac){
frame.MessageSize = 0; frame.MessageSize = 0;
return this->sendToDevice(frame); return this->sendToDevice(frame);
}
#endif
#ifndef RUN_AS_MASTER
bool SBNetwork::checkMaster(){
if (this->pingDevice(this->NetworkDevice.MasterMAC)){
Serial.println("Master OK");
return true;
} }
else{ else {
Serial.println("Master ERROR"); return false;
}
}
bool SBNetwork::checkMaster(){
if (this->RunAsClient) {
if (this->pingDevice(this->NetworkDevice.MasterMAC)) {
Serial.println("Master OK");
return true;
}
else {
Serial.println("Master ERROR");
return false;
}
}
else {
return false; return false;
} }
} }
#endif
void SBNetwork::update(){ void SBNetwork::update(){
@ -545,15 +557,15 @@ void SBNetwork::update(){
} }
_LastTime = millis(); _LastTime = millis();
#ifndef RUN_AS_MASTER if (this->RunAsClient) {
if (NetworkDevice.ConnectedToMaster && MASTER_CHECK_INTERVAL){ if (NetworkDevice.ConnectedToMaster && MASTER_CHECK_INTERVAL) {
if (_Uptime > _NextCheck){ if (_Uptime > _NextCheck) {
// Now we have to check our sensors if they are still available // Now we have to check our sensors if they are still available
_NextCheck += MASTER_CHECK_INTERVAL; _NextCheck += MASTER_CHECK_INTERVAL;
checkMaster(); checkMaster();
} }
} }
#endif }
_LastReceivedMessageSize = 0; _LastReceivedMessageSize = 0;
_LastReceivedMessage = NULL; _LastReceivedMessage = NULL;
@ -564,36 +576,40 @@ void SBNetwork::update(){
} }
} }
#if defined(RUN_AS_MASTER)
uint8_t SBNetwork::addMac(SBMacAddress mac){ uint8_t SBNetwork::addMac(SBMacAddress mac){
if (!this->RunAsClient) {
// iterate through the storage and look if the mac already exists // iterate through the storage and look if the mac already exists
uint8_t iPos; uint8_t iPos;
for (iPos = 0; iPos < MAX_CLIENTS; iPos++){ for (iPos = 0; iPos < MAX_CLIENTS; iPos++) {
if (_MasterStorage.Slaves[iPos].isEquals(mac)){ if (MasterStorage.Slaves[iPos].isEquals(mac)) {
return iPos; return iPos;
} }
} }
// Search the first free place and add the mac // Search the first free place and add the mac
for (iPos = 0; iPos < MAX_CLIENTS; iPos++){ for (iPos = 0; iPos < MAX_CLIENTS; iPos++) {
if (_MasterStorage.Slaves[iPos].isEquals(EMPTY_MAC)){ if (MasterStorage.Slaves[iPos].isEquals(EMPTY_MAC)) {
_MasterStorage.Slaves[iPos] = mac; MasterStorage.Slaves[iPos] = mac;
_MasterStorage.save(); MasterStorage.save();
return iPos; return iPos;
} }
} }
return -1; return -1;
}
else {
return -1;
}
} }
uint8_t SBNetwork::removeMac(SBMacAddress mac){ uint8_t SBNetwork::removeMac(SBMacAddress mac){
if (!this->RunAsClient) {
// iterate through the storage and look if the mac is in the list, if not, then return -1. If yes, remove it. // iterate through the storage and look if the mac is in the list, if not, then return -1. If yes, remove it.
for (uint8_t iPos = 0; iPos < MAX_CLIENTS; iPos++){ for (uint8_t iPos = 0; iPos < MAX_CLIENTS; iPos++) {
if (_MasterStorage.Slaves[iPos].isEquals(mac)){ if (MasterStorage.Slaves[iPos].isEquals(mac)) {
_MasterStorage.Slaves[iPos] = EMPTY_MAC; MasterStorage.Slaves[iPos] = EMPTY_MAC;
_MasterStorage.save(); MasterStorage.save();
return iPos; return iPos;
} }
} }
return -1; return -1;
}
} }
#endif

View File

@ -14,7 +14,6 @@
class SBNetwork{ class SBNetwork{
private: private:
/* /*
* Stores the uptime of the device * Stores the uptime of the device
*/ */
@ -27,12 +26,10 @@ class SBNetwork{
* Stores the time when the device should ping to the master * Stores the time when the device should ping to the master
*/ */
unsigned long _NextCheck; unsigned long _NextCheck;
/* /*
* Stores the connection state to a master device in case of it is a client device * Stores the connection state to a master device in case of it is a client device
*/ */
bool _Connected; bool _Connected;
/** /**
* Here the payload will be stored for each frame receive * Here the payload will be stored for each frame receive
*/ */
@ -41,15 +38,11 @@ class SBNetwork{
* Here the received message is stored after a full message receive (fragmented or not fragmented) * Here the received message is stored after a full message receive (fragmented or not fragmented)
*/ */
uint8_t _ReadBuffer[MAX_FRAME_SIZE]; uint8_t _ReadBuffer[MAX_FRAME_SIZE];
#if defined(RUN_AS_MASTER)
/* /*
* Store the times, when the slaves sent the last signal to the master * Store the times, when the slaves sent the last signal to the master
*/ */
unsigned long long _SlavePings[MAX_CLIENTS]; unsigned long long _SlavePings[MAX_CLIENTS];
#endif /*
/**
Points to the last message which was received Points to the last message which was received
*/ */
void* _LastReceivedMessage; void* _LastReceivedMessage;
@ -68,11 +61,9 @@ class SBNetwork{
bool handleCommandPackage(SBNetworkFrame *frame); bool handleCommandPackage(SBNetworkFrame *frame);
#if defined(RUN_AS_MASTER)
bool sendMasterAck(SBMacAddress mac); bool sendMasterAck(SBMacAddress mac);
bool sendPairingAck(SBMacAddress mac); bool sendPairingAck(SBMacAddress mac);
#endif
bool receive(SBNetworkFrame *frame); bool receive(SBNetworkFrame *frame);
@ -82,29 +73,30 @@ public:
/* /*
* Define the standard addresses for the sensor network * Define the standard addresses for the sensor network
*/ */
//SBMacAddress _StandardSensorAddress = SBMacAddress(0x01, 0x01, 0x01, 0x01, 0x01);
SBMacAddress _BroadcastAddress = BROADCAST_MAC; SBMacAddress _BroadcastAddress = BROADCAST_MAC;
SBNetworkDevice NetworkDevice; SBNetworkDevice NetworkDevice;
#if defined(RUN_AS_MASTER)
SBMasterStorage _MasterStorage; SBMasterStorage MasterStorage;
#endif
RF24 radio; RF24 radio;
//###################################################################################### /*
* Stores the runtime mode of the device true=Client, false=Master
/*
* Constructor with setting the used pins for commnicate with the NRF24L01(+) chip.
*/ */
SBNetwork(uint8_t cePin, uint8_t csPin); bool RunAsClient;
/* /*
* Constructor with setting the used pins for commnicate with the NRF24L01(+) chip. * Constructor with setting the used pins for commnicate with the NRF24L01(+) chip.
*/ */
SBNetwork(bool client, uint8_t cePin, uint8_t csPin);
/*
* Constructor no settings. The used pins for commnicate with the NRF24L01(+) chip will be the standard pins.
*/
SBNetwork(); SBNetwork();
/* /*
* Initializes the sensor / master * Initializes the sensor / master
*/ */
void initialize(SBMacAddress mac); void initialize(SBMacAddress mac);
@ -130,24 +122,27 @@ public:
*/ */
void* getMessage() { return _LastReceivedMessage; } void* getMessage() { return _LastReceivedMessage; }
#ifndef RUN_AS_MASTER
bool connectToNetwork(); bool connectToNetwork();
bool checkMaster(); bool checkMaster();
#else
// Adds a mac to the storage and returns the position in the storage. /*
Adds a mac to the storage and returns the position in the storage.
*/
uint8_t addMac(SBMacAddress mac); uint8_t addMac(SBMacAddress mac);
// Removes the mac from the storage. If the mac was stored, it returns the position of the mac, if not, it returns -1. /*
Removes the mac from the storage. If the mac was stored, it returns the position of the mac, if not, it returns -1.
*/
uint8_t removeMac(SBMacAddress mac); uint8_t removeMac(SBMacAddress mac);
#endif
bool pingDevice(SBMacAddress mac); bool pingDevice(SBMacAddress mac);
// Updates the uptime counter. /*
// If this device is a sensor, it pings the master after a time. The time is definded in the config under SENSOR_CHECK_INTERVAL. Updates the uptime counter.
// If SENSOR_CHECK_INTERVAL is set to 0, it will not ping the master. If this device is a sensor, it pings the master after a time. The time is definded in the config under SENSOR_CHECK_INTERVAL.
If SENSOR_CHECK_INTERVAL is set to 0, it will not ping the master.
*/
void update(); void update();
unsigned long long uptime(){ unsigned long long uptime(){
@ -155,6 +150,4 @@ public:
} }
}; };
#endif #endif

View File

@ -3,7 +3,7 @@
#define _SB_NETWORK_CONFIG_ #define _SB_NETWORK_CONFIG_
// Uncomment the following line, to compile the library for a master device. // Uncomment the following line, to compile the library for a master device.
#define RUN_AS_MASTER //#define RUN_AS_MASTER
#define _DEBUG #define _DEBUG

View File

@ -3,11 +3,13 @@
#define _SB_TYPES_ #define _SB_TYPES_
#include <arduino.h> #include <arduino.h>
// Will be sent to check, if a device is available
#define SBS_COMMAND_PING 0 // Will be sent to check, if a device is available #define SBS_COMMAND_PING 0
#define SBS_COMMAND_NO_COMMAND 1 #define SBS_COMMAND_NO_COMMAND 1
#define SBS_COMMAND_SEARCH_MASTER 2 // Will be sent from a slave to find search a master // Will be sent from a slave to find search a master
#define SBS_COMMAND_MASTER_ACK 3 // Will be sent from a master after receiving a search master request #define SBS_COMMAND_SEARCH_MASTER 2
// Will be sent from a master after receiving a search master request
#define SBS_COMMAND_MASTER_ACK 3
#define SBS_COMMAND_REQUEST_PAIRING 4 #define SBS_COMMAND_REQUEST_PAIRING 4
#define SBS_COMMAND_PAIRING_ACK 5 #define SBS_COMMAND_PAIRING_ACK 5