Cleanup driver initialization

This commit is contained in:
Henrik Ekblad
2015-03-03 00:24:13 +01:00
parent afce932cff
commit 5aebe80711
19 changed files with 68 additions and 61 deletions
+16 -4
View File
@@ -1,6 +1,6 @@
#ifndef MyConfig_h
#define MyConfig_h
#include <stdint.h>
/**********************************
* Enable/Disable debug logging
***********************************/
@@ -15,13 +15,14 @@
// which might vary, especially in networks with many hops. 5s ought to be enough for anyone.
#define MY_VERIFICATION_TIMEOUT_MS 5000
// MySigningDriverAtsha204 settinga
// MySigningDriverAtsha204 default setting
#define MY_ATSHA204_PIN 17 // A3 - pin where ATSHA204 is attached
// MySigningDriverAtsha204Soft settings
// MySigningDriverAtsha204Soft default settings
#define MY_RANDOMSEED_PIN 7 // A7 - Pin used for random generation (do not connect anything to this)
// Key to use for HMAC calculation in MySigningDriverAtsha204Soft (32 bytes)
#define MY_HMAC_KEY 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
#define MY_HMAC_KEY 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
/**********************************
@@ -30,7 +31,18 @@
#define RF24_CE_PIN 9
#define RF24_CS_PIN 10
#define RF24_PA_LEVEL RF24_PA_MAX
#define RF24_CHANNEL 76 //RF channel for the sensor net, 0-127
#define RF24_DATARATE RF24_250KBPS //RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
#define RF24_BASE_RADIO_ID ((uint64_t)0xA8A8E1FC00LL) // This is also act as base value for sensor nodeId addresses. Change this (or channel) if you have more than one sensor network.
// Enable SOFTSPI for the W5100 Ethernet module
//#define SOFTSPI
#ifdef SOFTSPI
// Define the soft SPI pins used for NRF radio
const uint8_t SOFT_SPI_MISO_PIN = 16;
const uint8_t SOFT_SPI_MOSI_PIN = 15;
const uint8_t SOFT_SPI_SCK_PIN = 14;
#endif
#endif
-4
View File
@@ -7,10 +7,6 @@
#include "utility/RF24.h"
#include "utility/RF24_config.h"
#define RF24_CHANNEL 76 //RF channel for the sensor net, 0-127
#define RF24_DATARATE RF24_250KBPS //RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
#define RF24_BASE_RADIO_ID ((uint64_t)0xA8A8E1FC00LL) // This is also act as base value for sensor nodeId addresses. Change this (or channel) if you have more than one sensor network.
#define TO_ADDR(x) (RF24_BASE_RADIO_ID + x)
#define WRITE_PIPE ((uint8_t)0)
+4 -5
View File
@@ -45,12 +45,11 @@ MySensor::MySensor(MyRFDriver &_radio, MySigningDriver &_signer)
}
void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, uint8_t _parentNodeId, bool requestSignatures) {
void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, uint8_t _parentNodeId) {
Serial.begin(BAUD_RATE);
repeaterMode = _repeaterMode;
msgCallback = _msgCallback;
failedTransmissions = 0;
requireSigning = requestSignatures;
// Only gateway should use node id 0
isGateway = _nodeId == 0;
@@ -137,7 +136,7 @@ void MySensor::setupNode() {
present(NODE_SENSOR_ID, repeaterMode? S_ARDUINO_REPEATER_NODE : S_ARDUINO_NODE);
// Notify gateway (and possibly controller) about the signing preferences of this node
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_REQUEST_SIGNING, false).set(requireSigning));
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_REQUEST_SIGNING, false).set(signer.requestSignatures()));
// Send a configuration exchange request to controller
// Node sends parent node. Controller answers with latest node configuration
@@ -315,7 +314,7 @@ boolean MySensor::process() {
// Before processing message, reject unsigned messages if signing is required and check signature (if it is signed and addressed to us)
// Note that we do not care at all about any signature found if we do not require signing
if (requireSigning && msg.destination == nc.nodeId && mGetLength(msg) &&
if (signer.requestSignatures() && msg.destination == nc.nodeId && mGetLength(msg) &&
(mGetCommand(msg) != C_INTERNAL || (msg.type != I_GET_NONCE_RESPONSE && msg.type != I_GET_NONCE && msg.type != I_REQUEST_SIGNING))) {
if (!mGetSigned(msg)) return false; // Received an unsigned message but we do require signing. This message gets nowhere!
else if (!signer.verifyMsg(msg)) {
@@ -403,7 +402,7 @@ boolean MySensor::process() {
// We do not currently want a gateway to require signing from all nodes in a network just because it wants one node
// to sign it's messages
if (isGateway) {
if (requireSigning && DO_SIGN(msg.sender))
if (signer.requestSignatures() && DO_SIGN(msg.sender))
sendRoute(build(msg, nc.nodeId, msg.sender, NODE_SENSOR_ID, C_INTERNAL, I_REQUEST_SIGNING, false).set(true));
else
sendRoute(build(msg, nc.nodeId, msg.sender, NODE_SENSOR_ID, C_INTERNAL, I_REQUEST_SIGNING, false).set(false));
+4 -4
View File
@@ -14,8 +14,10 @@
#include "Version.h" // Auto generated by bot
#include "MyRFDriver.h"
#include "MyRFDriverNRF24.h"
#include "MyParser.h"
#include "MySigningDriver.h"
#include "MySigningDriverNone.h"
#include "MyConfig.h"
#include "MyMessage.h"
#include <stddef.h>
@@ -79,7 +81,7 @@ class MySensor
* Creates a new instance of Sensor class.
*
*/
MySensor(MyRFDriver &radio, MySigningDriver &signer);
MySensor(MyRFDriver &radio =*new MyRFDriverNRF24(), MySigningDriver &signer=*new MySigningDriverNone());
/**
* Begin operation of the MySensors library
@@ -89,10 +91,9 @@ class MySensor
* @param nodeId The unique id (1-254) for this sensor. Default is AUTO(255) which means sensor tries to fetch an id from controller.
* @param repeaterMode Activate repeater mode. This node will forward messages to other nodes in the radio network. Make sure to call process() regularly. Default in false
* @param parentNodeId Use this to force node to always communicate with a certain parent node. Default is AUTO which means node automatically tries to find a parent.
* @param requestSignatures Set this to true if you want destination node to sign all messages sent to this node. Default is not to require signing.
*/
void begin(void (* msgCallback)(const MyMessage &)=NULL, uint8_t nodeId=AUTO, boolean repeaterMode=false, uint8_t parentNodeId=AUTO, bool requestSignatures=false);
void begin(void (* msgCallback)(const MyMessage &)=NULL, uint8_t nodeId=AUTO, boolean repeaterMode=false, uint8_t parentNodeId=AUTO);
/**
* Return the nodes nodeId.
@@ -242,7 +243,6 @@ class MySensor
bool repeaterMode;
bool autoFindParent;
bool isGateway;
bool requireSigning;
uint16_t doSign[16]; // Bitfield indicating which sensors require signed communication
MyMessage msg; // Buffer for incoming messages.
+6 -1
View File
@@ -1,4 +1,9 @@
#include "MySigningDriver.h"
MySigningDriver::MySigningDriver() {
MySigningDriver::MySigningDriver(bool requestSignatures) : _requestSignatures(requestSignatures) {
}
bool MySigningDriver::requestSignatures() {
return _requestSignatures;
}
+8 -1
View File
@@ -16,7 +16,11 @@ public:
// MySigningDriver constructor.
// Different parameters would be needed depending on signing backend (e.g. pins connected etc.).
// Keeping these parameters as #define's in MyConfig to streamline the driver interface.
MySigningDriver();
// @param requestSignatures Set this to true if you want destination node to sign all messages sent to this node. Default is not to require signing.
MySigningDriver(bool requestSignatures=false);
// Returns the current requestSignature setting
bool requestSignatures();
// Stores signing identifier and a new nonce in provided message for signing operations.
// All space in message payload buffer is used for signing identifier and nonce.
@@ -55,6 +59,9 @@ public:
// This ends a signing operation at the receiving side so implementation is expected to do any
// deinitializations and enter a power saving state within this call.
virtual bool verifyMsg(MyMessage &msg) = 0;
public:
bool _requestSignatures;
};
#endif
@@ -36,10 +36,10 @@ static void DEBUG_ATSHA_PRINTBUF(char* str, uint8_t* buf, uint8_t sz)
#endif
MySigningDriverAtsha204::MySigningDriverAtsha204()
MySigningDriverAtsha204::MySigningDriverAtsha204(bool requestSignatures, uint8_t atshaPin)
:
MySigningDriver(),
atsha204(MY_ATSHA204_PIN),
MySigningDriver(requestSignatures),
atsha204(atshaPin),
verification_ongoing(false)
{
// We set the part of the 32-byte nonce that does not fit into a message to 0xAA
@@ -20,7 +20,7 @@
class MySigningDriverAtsha204 : public MySigningDriver
{
public:
MySigningDriverAtsha204();
MySigningDriverAtsha204(bool requestSignatures=true, uint8_t atshaPin = MY_ATSHA204_PIN);
bool getNonce(MyMessage &msg);
bool checkTimer(void);
bool putNonce(MyMessage &msg);
@@ -40,9 +40,10 @@ static void DEBUG_ATSHASOFT_PRINTBUF(char* str, uint8_t* buf, uint8_t sz)
#define DEBUG_ATSHASOFT_PRINTBUF(str, buf, sz)
#endif
MySigningDriverAtsha204Soft::MySigningDriverAtsha204Soft()
MySigningDriverAtsha204Soft::MySigningDriverAtsha204Soft(bool requestSignatures, uint8_t randomseedPin)
:
MySigningDriver(),
MySigningDriver(requestSignatures),
rndPin(randomseedPin),
hmacKey({MY_HMAC_KEY}),
verification_ongoing(false),
Sha256()
@@ -54,7 +55,7 @@ MySigningDriverAtsha204Soft::MySigningDriverAtsha204Soft()
bool MySigningDriverAtsha204Soft::getNonce(MyMessage &msg) {
// Set randomseed
randomSeed(analogRead(MY_RANDOMSEED_PIN));
randomSeed(analogRead(rndPin));
for (int i = 0; i < MAX_PAYLOAD; i++)
{
current_nonce[i] = random(255);
@@ -24,11 +24,11 @@
// Because it is completly software based, the quality of the generated random numbers
// is weaker though. Random numbers are generated using the Arduino library and seed
// is sampled from an analog pin. This pin should unconnected in the hardware.
// The pin is selected using RANDOMSEED_PIN in MyConfig.h.
// The pin is selected using MY_RANDOMSEED_PIN in MyConfig.h.
class MySigningDriverAtsha204Soft : public MySigningDriver
{
public:
MySigningDriverAtsha204Soft();
MySigningDriverAtsha204Soft(bool requestSignatures=true, uint8_t randomseedPin = MY_RANDOMSEED_PIN);
bool getNonce(MyMessage &msg);
bool checkTimer(void);
bool putNonce(MyMessage &msg);
@@ -41,6 +41,7 @@ private:
uint8_t current_nonce[NONCE_NUMIN_SIZE_PASSTHROUGH];
uint8_t temp_message[32];
uint8_t hmacKey[32];
uint8_t rndPin;
uint8_t* hmac;
bool calculateSignature(MyMessage &msg);
};
@@ -4,10 +4,6 @@
#include <DallasTemperature.h>
#include <OneWire.h>
#ifndef MYSENSORS_SENSOR
#error Please switch to MYSENSORS_SENSOR in MyConfig.h
#endif
#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected
#define MAX_ATTACHED_DS18B20 16
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
@@ -17,7 +17,7 @@
*
*
* COMPILING WIZNET (W5100) ETHERNET MODULE
* > Edit RF24_config.h in (libraries\MySensors\utility) to enable softspi (remove // before "#define SOFTSPI").
* > Edit MyConfig.h in (libraries\MySensors\) to enable softspi (remove // before "#define SOFTSPI").
*
* COMPILING ENC28J60 ETHERNET MODULE
* > Use Arduino IDE 1.5.7 (or later)
@@ -42,18 +42,17 @@
#include <DigitalIO.h> // This include can be removed when using UIPEthernet module
#include <SPI.h>
#include <MyParserSerial.h>
#include <MySensor.h>
#include <stdarg.h>
#include <MsTimer2.h>
#include <PinChangeInt.h>
#include "GatewayUtil.h"
#ifndef MYSENSORS_ETHERNET_GATEWAY
#error Please switch to MYSENSORS_ETHERNET_GATEWAY in MyConfig.h
#endif
// Use this if you have attached a Ethernet ENC28J60 shields
//#include <UIPEthernet.h>
// #include <UIPEthernet.h>
// Use this for WizNET W5100 module and Arduino Ethernet Shield
#include <Ethernet.h>
@@ -23,7 +23,7 @@ volatile uint8_t countErr;
boolean inclusionMode; // Keeps track on inclusion mode
void (*serial)(const char *fmt, ... );
MyParserClass parser;
MyParserSerial parser;
void setInclusionMode(boolean newMode);
void txBlink(uint8_t cnt);
@@ -62,9 +62,6 @@ http://forum.mysensors.org/topic/303/mqtt-broker-gateway
#include <Ethernet.h>
#include "MyMQTT.h"
#ifndef MYSENSORS_MQTT_GATEWAY
#error Please switch to MYSENSORS_MQTT_GATEWAY in MyConfig.h
#endif
#define INCLUSION_MODE_TIME 1 // Number of minutes inclusion mode is enabled
#define INCLUSION_MODE_PIN 3 // Digital pin used for inclusion mode button
@@ -1,8 +1,6 @@
// Example sketch showing how to control physical relays.
// This example will remember relay state even after power failure.
#include <MySigningDriverNone.h>
#include <MyRFDriverNRF24.h>
#include <MySensor.h>
#include <SPI.h>
@@ -11,9 +9,7 @@
#define RELAY_ON 1 // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay
MyRFDriverNRF24 radio; // NRFRF24L01 radio driver
MySigningDriverNone signer; // Message signing disabled
MySensor gw(radio, signer);
MySensor gw;
void setup()
{
@@ -1,24 +1,24 @@
// Example sketch showing how to securely control locks.
// This example will remember lock state even after power failure.
#include <MySigningDriverAtsha204Soft.h>
#include <MyRFDriverNRF24.h>
#include <MySensor.h>
#include <SPI.h>
#ifndef MYSENSORS_SENSOR
#error Please switch to MYSENSORS_SENSOR in MyConfig.h
#endif
#define LOCK_1 3 // Arduino Digital I/O pin number for first lock (second on pin+1 etc)
#define NOF_LOCKS 1 // Total number of attached locks
#define LOCK_LOCK 1 // GPIO value to write to lock attached lock
#define LOCK_UNLOCK 0 // GPIO value to write to unlock attached lock
MySensor gw;
MyRFDriverNRF24 radio; // NRFRF24L01 radio driver
MySigningDriverAtsha204Soft signer; // Message signing driver
MySensor gw(radio, signer);
void setup()
{
// Initialize library and add callback for incoming messages (signing is required)
gw.begin(incomingMessage, AUTO, true, AUTO, true);
gw.begin(incomingMessage, AUTO, true, AUTO);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("Secure Lock", "1.0");
@@ -23,7 +23,7 @@ volatile uint8_t countErr;
boolean inclusionMode; // Keeps track on inclusion mode
void (*serial)(const char *fmt, ... );
MyParserClass parser;
MyParserSerial parser;
void setInclusionMode(boolean newMode);
void txBlink(uint8_t cnt);
@@ -24,16 +24,13 @@
#define NO_PORTB_PINCHANGES
#include <SPI.h>
#include <MyParserSerial.h>
#include <MySensor.h>
#include <stdarg.h>
#include <MsTimer2.h>
#include <PinChangeInt.h>
#include "GatewayUtil.h"
#ifndef MYSENSORS_SERIAL_GATEWAY
#error Please switch to MYSENSORS_SERIAL_GATEWAY in MyConfig.h
#endif
#define INCLUSION_MODE_TIME 1 // Number of minutes inclusion mode is enabled
#define INCLUSION_MODE_PIN 3 // Digital pin used for inclusion mode button
#define RADIO_ERROR_LED_PIN 4 // Error led pin
+5 -4
View File
@@ -11,6 +11,7 @@
#ifndef __RF24_CONFIG_H__
#define __RF24_CONFIG_H__
#include <MyConfig.h>
#if ARDUINO < 100
#include <WProgram.h>
@@ -39,10 +40,10 @@
#define _SPI uspi
#elif defined SOFTSPI
// change these pins to your liking
//
const uint8_t SOFT_SPI_MISO_PIN = 16;
const uint8_t SOFT_SPI_MOSI_PIN = 15;
const uint8_t SOFT_SPI_SCK_PIN = 14;
//const uint8_t SOFT_SPI_MISO_PIN = 16; // <-- Moved to MyConfig.h
//const uint8_t SOFT_SPI_MOSI_PIN = 15; // <-- Moved to MyConfig.h
//const uint8_t SOFT_SPI_SCK_PIN = 14; // <-- Moved to MyConfig.h
const uint8_t SPI_MODE = 0;
#define _SPI spi