Added Automatic client adding enable/disable feature.

Added a function to get the mac of the device of the last received message.
This commit is contained in:
Marcel Schulz 2017-12-10 23:54:23 +01:00
parent fb1c6f7fb4
commit bf01311e36
3 changed files with 68 additions and 18 deletions

View File

@ -13,4 +13,3 @@ The target of this library is tobuild up a small and easy closed network of devi
It's quite simple. One device is defined as the master device. In my case it is an Wemos D1 mini / ESP826. The master devices is the device where the clients are connecting to. The master can allow or deny new client connections. If a client gets connected to the master, the master stores its mac in the internal flash. If a client is sending transmissions to a master and the master has not stored the clients mac address, the communication will be blocked. Clients are automatically searching for and connecting to a master. You don't have to configure this. The only thing that has to be done is to define a unique mac adress for the clients in the sketch. If a client is connected to a master the first time, it stores the master mac in the internal flash. Even if the client loses the power or turnes off, the master mac gets stored. After turning on the client again, the connection to the master gets restored.
The library uses the basic RF24 lib from https://tmrh20.github.io/RF24 and adds further functions for a simple master/client network.

View File

@ -64,7 +64,7 @@ void SBNetwork::initialize(SBMacAddress mac){
//this->radio.enableDynamicAck();
this->radio.setAutoAck(true);
//this->radio.enableAckPayload();
this->radio.setRetries(0, 15);
this->radio.setRetries(40, 5);
// Listen at the own address
this->radio.openReadingPipe(0, NetworkDevice.MAC);
@ -226,7 +226,7 @@ bool SBNetwork::receive(SBNetworkFrame *frame){
else{
byte buffer[32];
radio.read(buffer, size);
// We cant use the target address of frame, because the first element in fram is the header
// We cant use the target address of frame, because the first element in frame is the header
memcpy(frame, buffer, sizeof(SBNetworkHeader));
frame->MessageSize = size - sizeof(SBNetworkHeader);
if (frame->MessageSize > 0){
@ -450,26 +450,46 @@ bool SBNetwork::handleCommandPackage(SBNetworkFrame *frame){
}
case SBS_COMMAND_SEARCH_MASTER: {
#ifdef _DEBUG
Serial.println("Received 'SEARCH_MASTER' Package. Send MasterACK...");
Serial.print("Received 'SEARCH_MASTER' Package. ");
#endif
delay(100);
bool bSend = sendMasterAck(frame->Header.FromAddress);
if (bSend) {
return false;
if (_EnableAutomaticClientAdding) {
#ifdef _DEBUG
Serial.println("Send MasterACK...");
#endif
delay(100);
bool bSend = sendMasterAck(frame->Header.FromAddress);
if (bSend) {
return false;
}
Serial.println("Done");
}
Serial.println("Done");
#if defined(_DEBUG)
else {
Serial.println("AutomaticClientAdding is deactivaed. Ignoring package.");
}
#endif
break;
}
case SBS_COMMAND_REQUEST_PAIRING: {
#ifdef _DEBUG
Serial.println("Received 'PAIRING_REQUEST' Package. Send PairingACK");
Serial.print("Received 'PAIRING_REQUEST' Package. ");
#endif
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
bool bSend = sendPairingAck(frame->Header.FromAddress);
if (bSend) {
addMac(frame->Header.FromAddress);
if (_EnableAutomaticClientAdding) {
#ifdef _DEBUG
Serial.println("Send MasterACK...");
#endif
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
bool bSend = sendPairingAck(frame->Header.FromAddress);
if (bSend) {
addMac(frame->Header.FromAddress);
}
}
#if defined(_DEBUG)
else {
Serial.println("AutomaticClientAdding is deactivaed. Ignoring package.");
}
#endif
break;
}
case SBS_COMMAND_NO_COMMAND:
@ -479,6 +499,7 @@ bool SBNetwork::handleCommandPackage(SBNetworkFrame *frame){
break;
}
}
// Package was handled by handleCommandPackage();
return false;
}
else {

View File

@ -2,12 +2,15 @@
#ifndef _SB_NETWORK_
#define _SB_NETWORK_
#define SB_VERSION "1.0.0"
#define SB_VERSION "1.0.1"
#include <RF24_config.h>
#include <RF24.h>
#include <nRF24L01.h>
#include "SBTypes.h"
#include "SBNetwork_config.h"
#include "SBDevice.h"
#include <RF24.h>
#define BROADCAST_MAC (SBMacAddress(0x9E, 0x0E, 0x9E, 0x0E, 0x9E))
#define EMPTY_MAC (SBMacAddress(0x00, 0x00, 0x00, 0x00, 0x00))
@ -54,11 +57,17 @@ class SBNetwork{
Stores the mac of the sender of the last received message
*/
SBMacAddress _LastReceivedFromAddress;
/**
Enables or disables automatical client adding, when receiving a pairing resuest.
This is false by default.
*/
bool _EnableAutomaticClientAdding = false;
void initializeNetworkDevice(SBNetworkDevice &device, SBMacAddress mac);
bool sendToDevice(SBNetworkFrame frame);
// Return false, if the package was handled internally and if it is not relevant for the end user
bool handleCommandPackage(SBNetworkFrame *frame);
bool sendMasterAck(SBMacAddress mac);
@ -120,7 +129,12 @@ public:
/*
Returns a pointer to the buffer, where the last incomming transmission is stored
*/
void* getMessage() { return _LastReceivedMessage; }
void* getMessage() { return _LastReceivedMessage > 0 ? _LastReceivedMessage : NULL; }
/*
Returns the mac of the sender from the last receives package
*/
SBMacAddress getLastReceivedMac() { return _LastReceivedFromAddress; };
bool connectToNetwork();
@ -148,6 +162,22 @@ public:
unsigned long long uptime(){
return _Uptime;
}
/**
Enables or disables automatic client adding when receiving a pairing resuest.
The default is false.
If the flag is set to true and the master receives a pairing request or a search master broadcast, it will not respond.
*/
void enableAutomaticClientAdding(bool enable) {
_EnableAutomaticClientAdding = enable;
}
/**
Returns the flag, if the master will accept new clients automatically
*/
bool isAutomaticClientAddingEnabled() {
return _EnableAutomaticClientAdding;
}
};
#endif