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:
parent
1128d8c3c4
commit
a99d91c30b
@ -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
|
|
@ -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,103 +302,106 @@ bool SBNetwork::receiveMessage(void **message, uint8_t *messageSize, SBMacAddres
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef RUN_AS_MASTER
|
|
||||||
bool SBNetwork::connectToNetwork(){
|
bool SBNetwork::connectToNetwork(){
|
||||||
Serial.print(F("Try to connect to master..."));
|
if (this->RunAsClient) {
|
||||||
// First we have to check, if we already have a master stored
|
Serial.print(F("Try to connect to master..."));
|
||||||
if (!this->NetworkDevice.ConnectedToMaster){
|
// First we have to check, if we already have a master stored
|
||||||
Serial.println("Warning - Not paired to a master");
|
if (!this->NetworkDevice.ConnectedToMaster) {
|
||||||
Serial.print(F("Sending broadcast transmission to find a master..."));
|
Serial.println("Warning - Not paired to a master");
|
||||||
// If not, we have to search for a master
|
Serial.print(F("Sending broadcast transmission to find a master..."));
|
||||||
SBNetworkHeader header;
|
// If not, we have to search for a master
|
||||||
header.ToAddress = this->_BroadcastAddress;
|
SBNetworkHeader header;
|
||||||
header.FromAddress = this->NetworkDevice.MAC;
|
header.ToAddress = this->_BroadcastAddress;
|
||||||
header.CommandType = SBS_COMMAND_SEARCH_MASTER;
|
header.FromAddress = this->NetworkDevice.MAC;
|
||||||
header.FragmentCount = 1;
|
header.CommandType = SBS_COMMAND_SEARCH_MASTER;
|
||||||
header.PackageId = millis();
|
header.FragmentCount = 1;
|
||||||
|
header.PackageId = millis();
|
||||||
|
|
||||||
SBNetworkFrame frame;
|
SBNetworkFrame frame;
|
||||||
frame.Header = header;
|
frame.Header = header;
|
||||||
frame.Message = NULL;
|
frame.Message = NULL;
|
||||||
frame.MessageSize = 0;
|
frame.MessageSize = 0;
|
||||||
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){
|
|
||||||
Serial.println(F("Timeout"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
if (frame.Header.CommandType != SBS_COMMAND_MASTER_ACK){
|
|
||||||
if (frame.MessageSize > 0){
|
|
||||||
free(frame.Message);
|
|
||||||
}
|
}
|
||||||
Serial.println(F("Failed - Got answer but no master ack"));
|
}
|
||||||
|
|
||||||
|
if (timeout) {
|
||||||
|
Serial.println(F("Timeout"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
Serial.println(F("Done"));
|
if (frame.Header.CommandType != SBS_COMMAND_MASTER_ACK) {
|
||||||
Serial.print(F("Got answer from a master. Master-MAC is "));
|
if (frame.MessageSize > 0) {
|
||||||
printAddress(frame.Header.FromAddress);
|
free(frame.Message);
|
||||||
Serial.println();
|
}
|
||||||
Serial.print(F("Try to pair with master..."));
|
Serial.println(F("Failed - Got answer but no master ack"));
|
||||||
SBNetworkFrame conFrame;
|
return false;
|
||||||
conFrame.Header.CommandType = SBS_COMMAND_REQUEST_PAIRING;
|
|
||||||
conFrame.Header.FragmentCount = 1;
|
|
||||||
conFrame.Header.FragmentNr = 0;
|
|
||||||
conFrame.Header.FromAddress = this->NetworkDevice.MAC;
|
|
||||||
conFrame.Header.PackageId = millis();
|
|
||||||
conFrame.Header.ToAddress = frame.Header.FromAddress;
|
|
||||||
conFrame.MessageSize = 0;
|
|
||||||
if (!this->sendToDevice(conFrame)){
|
|
||||||
Serial.println("Failed - Sending pairing request");
|
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
while (!this->receive(&frame)){
|
Serial.println(F("Done"));
|
||||||
if (millis() - started_waiting_at > 1000){
|
Serial.print(F("Got answer from a master. Master-MAC is "));
|
||||||
timeout = true;
|
printAddress(frame.Header.FromAddress);
|
||||||
break;
|
Serial.println();
|
||||||
|
Serial.print(F("Try to pair with master..."));
|
||||||
|
SBNetworkFrame conFrame;
|
||||||
|
conFrame.Header.CommandType = SBS_COMMAND_REQUEST_PAIRING;
|
||||||
|
conFrame.Header.FragmentCount = 1;
|
||||||
|
conFrame.Header.FragmentNr = 0;
|
||||||
|
conFrame.Header.FromAddress = this->NetworkDevice.MAC;
|
||||||
|
conFrame.Header.PackageId = millis();
|
||||||
|
conFrame.Header.ToAddress = frame.Header.FromAddress;
|
||||||
|
conFrame.MessageSize = 0;
|
||||||
|
if (!this->sendToDevice(conFrame)) {
|
||||||
|
Serial.println("Failed - Sending pairing request");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
while (!this->receive(&frame)) {
|
||||||
|
if (millis() - started_waiting_at > 1000) {
|
||||||
|
timeout = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (timeout) {
|
||||||
|
Serial.println(F("Timeout"));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (frame.Header.CommandType != SBS_COMMAND_PAIRING_ACK) {
|
||||||
|
Serial.println(F("Failed - Pairing rejected from the master"));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this->NetworkDevice.MasterMAC = frame.Header.FromAddress;
|
||||||
|
this->NetworkDevice.NetworkKey = *(frame.Message);
|
||||||
|
this->NetworkDevice.ConnectedToMaster = -1;
|
||||||
|
EEPROM.put(0, NetworkDevice);
|
||||||
|
Serial.println("Suceeded");
|
||||||
|
Serial.print("Try to ping to master...");
|
||||||
|
delay(100);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (timeout) {
|
|
||||||
Serial.println(F("Timeout"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (frame.Header.CommandType != SBS_COMMAND_PAIRING_ACK){
|
|
||||||
Serial.println(F("Failed - Pairing rejected from the master"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
this->NetworkDevice.MasterMAC = frame.Header.FromAddress;
|
|
||||||
this->NetworkDevice.NetworkKey = *(frame.Message);
|
|
||||||
this->NetworkDevice.ConnectedToMaster = -1;
|
|
||||||
EEPROM.put(0, NetworkDevice);
|
|
||||||
Serial.println("Suceeded");
|
|
||||||
Serial.print("Try to ping to master...");
|
|
||||||
delay(100);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
||||||
|
Serial.println(F("Failed - Master not responding"));
|
||||||
|
}
|
||||||
|
return bMasterAvailable;
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
Serial.println(F("Failed - Master not responding"));
|
return false;
|
||||||
}
|
}
|
||||||
return bMasterAvailable;
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
bool SBNetwork::pingDevice(SBMacAddress mac){
|
bool SBNetwork::pingDevice(SBMacAddress mac){
|
||||||
SBNetworkHeader header;
|
SBNetworkHeader header;
|
||||||
@ -419,118 +421,128 @@ 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: ");
|
||||||
printAddress(frame->Header.FromAddress);
|
printAddress(frame->Header.FromAddress);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
#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;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
#else
|
|
||||||
return true;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(RUN_AS_MASTER)
|
|
||||||
bool SBNetwork::sendMasterAck(SBMacAddress mac){
|
bool SBNetwork::sendMasterAck(SBMacAddress mac){
|
||||||
SBNetworkHeader header;
|
if (!this->RunAsClient) {
|
||||||
header.ToAddress = mac;
|
SBNetworkHeader header;
|
||||||
header.FromAddress = this->NetworkDevice.MAC;
|
header.ToAddress = mac;
|
||||||
header.CommandType = SBS_COMMAND_MASTER_ACK;
|
header.FromAddress = this->NetworkDevice.MAC;
|
||||||
header.FragmentCount = 1;
|
header.CommandType = SBS_COMMAND_MASTER_ACK;
|
||||||
header.PackageId = millis();
|
header.FragmentCount = 1;
|
||||||
|
header.PackageId = millis();
|
||||||
|
|
||||||
SBNetworkFrame frame;
|
SBNetworkFrame frame;
|
||||||
frame.Header = header;
|
frame.Header = header;
|
||||||
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){
|
||||||
SBNetworkHeader header;
|
if (!this->RunAsClient) {
|
||||||
header.ToAddress = mac;
|
SBNetworkHeader header;
|
||||||
header.FromAddress = this->NetworkDevice.MAC;
|
header.ToAddress = mac;
|
||||||
header.CommandType = SBS_COMMAND_PAIRING_ACK;
|
header.FromAddress = this->NetworkDevice.MAC;
|
||||||
header.FragmentCount = 1;
|
header.CommandType = SBS_COMMAND_PAIRING_ACK;
|
||||||
header.PackageId = millis();
|
header.FragmentCount = 1;
|
||||||
|
header.PackageId = millis();
|
||||||
|
|
||||||
SBNetworkFrame frame;
|
SBNetworkFrame frame;
|
||||||
frame.Header = header;
|
frame.Header = header;
|
||||||
frame.Message = NULL;
|
frame.Message = NULL;
|
||||||
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;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t SBNetwork::removeMac(SBMacAddress mac){
|
uint8_t SBNetwork::removeMac(SBMacAddress mac){
|
||||||
// iterate through the storage and look if the mac is in the list, if not, then return -1. If yes, remove it.
|
if (!this->RunAsClient) {
|
||||||
for (uint8_t iPos = 0; iPos < MAX_CLIENTS; iPos++){
|
// iterate through the storage and look if the mac is in the list, if not, then return -1. If yes, remove it.
|
||||||
if (_MasterStorage.Slaves[iPos].isEquals(mac)){
|
for (uint8_t iPos = 0; iPos < MAX_CLIENTS; iPos++) {
|
||||||
_MasterStorage.Slaves[iPos] = EMPTY_MAC;
|
if (MasterStorage.Slaves[iPos].isEquals(mac)) {
|
||||||
_MasterStorage.save();
|
MasterStorage.Slaves[iPos] = EMPTY_MAC;
|
||||||
return iPos;
|
MasterStorage.save();
|
||||||
|
return iPos;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
#endif
|
|
213
src/SBNetwork.h
213
src/SBNetwork.h
@ -14,147 +14,140 @@
|
|||||||
|
|
||||||
class SBNetwork{
|
class SBNetwork{
|
||||||
private:
|
private:
|
||||||
|
/*
|
||||||
|
* Stores the uptime of the device
|
||||||
|
*/
|
||||||
|
unsigned long long _Uptime;
|
||||||
|
/*
|
||||||
|
* Stores the last time from millis()
|
||||||
|
*/
|
||||||
|
unsigned long _LastTime;
|
||||||
|
/*
|
||||||
|
* Stores the time when the device should ping to the master
|
||||||
|
*/
|
||||||
|
unsigned long _NextCheck;
|
||||||
|
/*
|
||||||
|
* Stores the connection state to a master device in case of it is a client device
|
||||||
|
*/
|
||||||
|
bool _Connected;
|
||||||
|
/**
|
||||||
|
* Here the payload will be stored for each frame receive
|
||||||
|
*/
|
||||||
|
uint8_t _ReceiveBuffer[MAX_PACKAGE_SIZE];
|
||||||
|
/**
|
||||||
|
* Here the received message is stored after a full message receive (fragmented or not fragmented)
|
||||||
|
*/
|
||||||
|
uint8_t _ReadBuffer[MAX_FRAME_SIZE];
|
||||||
|
/*
|
||||||
|
* Store the times, when the slaves sent the last signal to the master
|
||||||
|
*/
|
||||||
|
unsigned long long _SlavePings[MAX_CLIENTS];
|
||||||
|
/*
|
||||||
|
Points to the last message which was received
|
||||||
|
*/
|
||||||
|
void* _LastReceivedMessage;
|
||||||
|
/**
|
||||||
|
Stores the length of the last receives message
|
||||||
|
*/
|
||||||
|
uint8_t _LastReceivedMessageSize;
|
||||||
|
/**
|
||||||
|
Stores the mac of the sender of the last received message
|
||||||
|
*/
|
||||||
|
SBMacAddress _LastReceivedFromAddress;
|
||||||
|
|
||||||
/*
|
void initializeNetworkDevice(SBNetworkDevice &device, SBMacAddress mac);
|
||||||
* Stores the uptime of the device
|
|
||||||
*/
|
|
||||||
unsigned long long _Uptime;
|
|
||||||
/*
|
|
||||||
* Stores the last time from millis()
|
|
||||||
*/
|
|
||||||
unsigned long _LastTime;
|
|
||||||
/*
|
|
||||||
* Stores the time when the device should ping to the master
|
|
||||||
*/
|
|
||||||
unsigned long _NextCheck;
|
|
||||||
|
|
||||||
/*
|
bool sendToDevice(SBNetworkFrame frame);
|
||||||
* Stores the connection state to a master device in case of it is a client device
|
|
||||||
*/
|
|
||||||
bool _Connected;
|
|
||||||
|
|
||||||
/**
|
bool handleCommandPackage(SBNetworkFrame *frame);
|
||||||
* Here the payload will be stored for each frame receive
|
|
||||||
*/
|
|
||||||
uint8_t _ReceiveBuffer[MAX_PACKAGE_SIZE];
|
|
||||||
/**
|
|
||||||
* Here the received message is stored after a full message receive (fragmented or not fragmented)
|
|
||||||
*/
|
|
||||||
uint8_t _ReadBuffer[MAX_FRAME_SIZE];
|
|
||||||
|
|
||||||
#if defined(RUN_AS_MASTER)
|
bool sendMasterAck(SBMacAddress mac);
|
||||||
/*
|
|
||||||
* Store the times, when the slaves sent the last signal to the master
|
|
||||||
*/
|
|
||||||
unsigned long long _SlavePings[MAX_CLIENTS];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
bool sendPairingAck(SBMacAddress mac);
|
||||||
Points to the last message which was received
|
|
||||||
*/
|
|
||||||
void* _LastReceivedMessage;
|
|
||||||
/**
|
|
||||||
Stores the length of the last receives message
|
|
||||||
*/
|
|
||||||
uint8_t _LastReceivedMessageSize;
|
|
||||||
/**
|
|
||||||
Stores the mac of the sender of the last received message
|
|
||||||
*/
|
|
||||||
SBMacAddress _LastReceivedFromAddress;
|
|
||||||
|
|
||||||
void initializeNetworkDevice(SBNetworkDevice &device, SBMacAddress mac);
|
bool receive(SBNetworkFrame *frame);
|
||||||
|
|
||||||
bool sendToDevice(SBNetworkFrame frame);
|
bool receiveMessage(void **message, uint8_t *messageSize, SBMacAddress *mac);
|
||||||
|
|
||||||
bool handleCommandPackage(SBNetworkFrame *frame);
|
|
||||||
|
|
||||||
#if defined(RUN_AS_MASTER)
|
|
||||||
bool sendMasterAck(SBMacAddress mac);
|
|
||||||
|
|
||||||
bool sendPairingAck(SBMacAddress mac);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
bool receive(SBNetworkFrame *frame);
|
|
||||||
|
|
||||||
bool receiveMessage(void **message, uint8_t *messageSize, SBMacAddress *mac);
|
|
||||||
|
|
||||||
public:
|
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
|
||||||
|
*/
|
||||||
|
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(uint8_t cePin, uint8_t csPin);
|
SBNetwork(bool client, uint8_t cePin, uint8_t csPin);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Constructor with setting the used pins for commnicate with the NRF24L01(+) chip.
|
* 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);
|
||||||
void initialize(byte mac[]) { initialize(SBMacAddress(mac[0], mac[1], mac[2], mac[3], mac[4])); }
|
void initialize(byte mac[]) { initialize(SBMacAddress(mac[0], mac[1], mac[2], mac[3], mac[4])); }
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Resets the Sensors data (including the eeprom).
|
* Resets the Sensors data (including the eeprom).
|
||||||
* The sensor then will loos the connaction to the master.
|
* The sensor then will loos the connaction to the master.
|
||||||
*/
|
*/
|
||||||
void resetData();
|
void resetData();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Sends a SBNetworkFrame to a device.
|
Sends a SBNetworkFrame to a device.
|
||||||
*/
|
*/
|
||||||
bool sendToDevice(SBMacAddress mac, void* message, uint8_t messageSize);
|
bool sendToDevice(SBMacAddress mac, void* message, uint8_t messageSize);
|
||||||
/*
|
/*
|
||||||
Check if a new incomming transmission is available
|
Check if a new incomming transmission is available
|
||||||
*/
|
*/
|
||||||
uint8_t available() { return _LastReceivedMessageSize; }
|
uint8_t available() { return _LastReceivedMessageSize; }
|
||||||
/*
|
/*
|
||||||
Returns a pointer to the buffer, where the last incomming transmission is stored
|
Returns a pointer to the buffer, where the last incomming transmission is stored
|
||||||
*/
|
*/
|
||||||
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.
|
/*
|
||||||
uint8_t addMac(SBMacAddress mac);
|
Adds a mac to the storage and returns the position in the storage.
|
||||||
|
*/
|
||||||
|
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.
|
/*
|
||||||
uint8_t removeMac(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.
|
||||||
#endif
|
*/
|
||||||
|
uint8_t removeMac(SBMacAddress mac);
|
||||||
|
|
||||||
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.
|
||||||
void update();
|
If SENSOR_CHECK_INTERVAL is set to 0, it will not ping the master.
|
||||||
|
*/
|
||||||
|
void update();
|
||||||
|
|
||||||
unsigned long long uptime(){
|
unsigned long long uptime(){
|
||||||
return _Uptime;
|
return _Uptime;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user