Skip to content

Commit

Permalink
move OTA code into library
Browse files Browse the repository at this point in the history
shorter device code - saves also some bytes in flash and ram
  • Loading branch information
pa-pa committed May 8, 2017
1 parent 7632eac commit 07e100e
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 449 deletions.
6 changes: 3 additions & 3 deletions BatterySensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ class BatterySensor : public Alarm {
m_LastValue = voltage();
}

uint8_t current () {
uint8_t current () const{
return m_LastValue;
}

bool critical () {
bool critical () const {
return m_LastValue < CRITICALVALUE;
}

bool low () {
bool low () const {
return m_LastValue < LOWVALUE;
}

Expand Down
2 changes: 2 additions & 0 deletions Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Channel {

DeviceType& device () { return *dev; }

const DeviceType& device () const { return *dev; }

uint8_t number () const { return num; }

uint16_t address () const { return addr; }
Expand Down
104 changes: 42 additions & 62 deletions Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include "Radio.h"
#include "Led.h"

#define OTA_MODEL_START 0x7ff0 // start address of 2 byte model id in bootloader
#define OTA_SERIAL_START 0x7ff2 // start address of 10 byte serial number in bootloader
#define OTA_HMID_START 0x7ffc // start address of 3 byte device id in bootloader

namespace as {

class DeviceType {
Expand Down Expand Up @@ -53,17 +57,9 @@ class Device {
typedef typename HalType::RadioType RadioType;

private:
HMID devid;
HMID master;
char serial[11];

uint8_t firmversion;
uint8_t model[2];
uint8_t subtype;
uint8_t devinfo[3];

HalType* hal;
uint8_t msgcount;
List0& list0;
uint8_t msgcount;

HMID lastdev;
uint8_t lastmsg;
Expand All @@ -74,13 +70,14 @@ class Device {


public:
Device (uint16_t addr) : firmversion(0), subtype(0), hal(0), msgcount(0), lastmsg(0), kstore(addr) {
Device (uint16_t addr,List0& l) : hal(0), list0(l), msgcount(0), lastmsg(0), kstore(addr) {
// TODO init seed
}
virtual ~Device () {}

LedType& led () { return hal->led; }
BatteryType& battery () { return hal->battery; }
const BatteryType& battery () const { return hal->battery; }
RadioType& radio () { return hal->radio; }
KeyStore& keystore () { return this->kstore; }
Activity& activity () { return hal->activity; }
Expand All @@ -101,64 +98,44 @@ class Device {
hal = &h;
}

void setFirmwareVersion (uint8_t v) {
firmversion = v;
}

void setModel (uint8_t m1, uint8_t m2) {
model[0] = m1;
model[1] = m2;
}

const uint8_t* getModel () const {
return model;
}

void setModel (uint16_t address) {
HalType::pgm_read(model,address,sizeof(model));
}

void setSubType (uint8_t st) {
subtype = st;
}

void setInfo (uint8_t i1, uint8_t i2, uint8_t i3) {
devinfo[0] = i1;
devinfo[1] = i2;
devinfo[2] = i3;
}

void setMasterID (const HMID& id) {
master = id;
}

const HMID& getMasterID () const {
return master;
}

void setDeviceID (const HMID& id) {
devid=id;
void getDeviceID (HMID& id) {
#ifdef USE_OTA_BOOTLOADER
HalType::pgm_read((uint8_t*)&id,OTA_HMID_START,sizeof(id));
#else
id = DEVICE_ID;
#endif
}

void setDeviceID (uint16_t address) {
HalType::pgm_read((uint8_t*)&devid,address,sizeof(devid));
void getDeviceSerial (uint8_t* serial) {
#ifdef USE_OTA_BOOTLOADER
HalType::pgm_read((uint8_t*)serial,OTA_SERIAL_START,10);
#else
memcpy(serial,DEVICE_SERIAL,10);
#endif
}

const HMID& getDeviceID () const {
return devid;
bool isDeviceSerial (const uint8_t* serial) {
uint8_t tmp[10];
getDeviceSerial(tmp);
return memcmp(tmp,serial,10)==0;
}

void setSerial (const char* ser) {
memcpy(serial,ser,10);
serial[10] = 0;
void getDeviceModel (uint8_t* model) {
#ifdef USE_OTA_BOOTLOADER
HalType::pgm_read(model,OTA_MODEL_START,2);
#else
uint8_t dm[2] = {DEVICE_MODEL};
memcpy(model,dm,sizeof(dm));
#endif
}

void setSerial (uint16_t address) {
HalType::pgm_read((uint8_t*)serial,address,10);
void getDeviceInfo (uint8_t* info) {
uint8_t di[3] = {DEVICE_INFO};
memcpy(info,di,sizeof(di));
}

const char* getSerial () const {
return serial;
HMID getMasterID () {
return list0.masterid();
}

template <class ChannelType>
Expand Down Expand Up @@ -222,7 +199,7 @@ class Device {

bool send(Message& msg,const HMID& to) {
msg.to(to);
msg.from(devid);
getDeviceID(msg.from());
msg.setRpten(); // has to be set always
bool result = false;
uint8_t maxsend = 6;
Expand Down Expand Up @@ -293,14 +270,17 @@ class Device {
void sendDeviceInfo (const HMID& to,uint8_t count) {
DeviceInfoMsg& pm = msg.deviceInfo();
pm.init(to,count);
pm.fill(firmversion,model,serial,subtype,devinfo);
pm.fill(DEVICE_FIRMWARE,DEVICE_TYPE);
getDeviceModel(pm.model());
getDeviceSerial(pm.serial());
getDeviceInfo(pm.info());
send(msg,to);
}

void sendSerialInfo (const HMID& to,uint8_t count) {
SerialInfoMsg& pm = msg.serialInfo();
pm.init(to,count);
pm.fill(serial);
getDeviceSerial(pm.serial());
send(msg,to);
}

Expand Down
3 changes: 2 additions & 1 deletion Dimmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,8 @@ class DimmerDevice : public MultiChannelDevice<HalType,ChannelType,ChannelCount,
}
}

void initPwm (uint8_t p) {
void init (HalType& hal,uint8_t p) {
DeviceType::init(hal);
pin = p;
pinMode(pin,OUTPUT);
for( uint8_t i=1; i<=DeviceType::channels(); ++i ) {
Expand Down
13 changes: 13 additions & 0 deletions Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ class Message {
return fromID;
}

HMID& from () {
return fromID;
}

void to(const HMID& hmid) {
toID = hmid;
}
Expand Down Expand Up @@ -584,6 +588,14 @@ class DeviceInfoMsg : public Message {
*(buf+13) = subtype;
memcpy(buf+14,devinfo,3);
}
void fill(uint8_t firmversion,uint8_t subtype) {
uint8_t* buf = data();
*buf = firmversion;
*(buf+13) = subtype;
}
uint8_t* serial () { return data() + 3; }
uint8_t* model () { return data() + 1; }
uint8_t* info () { return data() + 14; }
};

class SerialInfoMsg : public Message {
Expand All @@ -596,6 +608,7 @@ class SerialInfoMsg : public Message {
uint8_t* buf = data();
memcpy(buf+3,serial,10);
}
uint8_t* serial () { return data() + 3; }
};

}
Expand Down
35 changes: 14 additions & 21 deletions MultiChannelDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MultiChannelDevice : public Device<HalType> {

typedef Device<HalType> DeviceType;

MultiChannelDevice (uint16_t addr) : Device<HalType>(addr), list0(addr + DeviceType::keystore().size()), numChannels(ChannelCount), cfgChannel(0xff) {
MultiChannelDevice (uint16_t addr) : Device<HalType>(addr,list0), list0(addr + DeviceType::keystore().size()), numChannels(ChannelCount), cfgChannel(0xff) {
addr = list0.address() + list0.size();
for( uint8_t i=0; i<channels(); ++i ) {
devchannels[i].setup(this,i+1,addr);
Expand All @@ -44,8 +44,7 @@ class MultiChannelDevice : public Device<HalType> {

void dumpSize () {
ChannelType ch = channel(channels());
uint16_t addr = ch.address() + ch.size();
DPRINT("Address Space: ");DDEC(DeviceType::keystore().address());DPRINT(" - ");DDECLN(addr);
DPRINT("Address Space: ");DDEC(DeviceType::keystore().address());DPRINT(" - ");DDECLN((uint16_t)(ch.address() + ch.size()));
}

uint16_t checksum () {
Expand Down Expand Up @@ -91,23 +90,16 @@ class MultiChannelDevice : public Device<HalType> {
return number != 0 && number <= channels();
}

void init (HalType& hal,const HMID& id,const char* serial) {
DeviceType::keystore().init();
// read master id from flash
DeviceType::setHal(hal);
DeviceType::setMasterID(list0.masterid());
DeviceType::setDeviceID(id);
DeviceType::setSerial(serial);
}

void init (HalType& hal,uint16_t idaddr, uint16_t serialaddr) {
void init (HalType& hal) {
dumpSize();
// first initialize EEProm if needed
if( storage.setup(checksum()) == true ) {
firstinit();
storage.store();
}
DeviceType::keystore().init();
DeviceType::setHal(hal);
// read master id from flash
DeviceType::setMasterID(list0.masterid());
// read id & serial from bootloader
DeviceType::setDeviceID(idaddr);
DeviceType::setSerial(serialaddr);
hal.init();
}

void firstinit () {
Expand Down Expand Up @@ -168,7 +160,9 @@ class MultiChannelDevice : public Device<HalType> {
}

void process(Message& msg) {
if( msg.to() == DeviceType::getDeviceID() || (msg.to() == HMID::boardcast && DeviceType::isBoardcastMsg(msg))) {
HMID devid;
DeviceType::getDeviceID(devid);
if( msg.to() == devid || (msg.to() == HMID::boardcast && DeviceType::isBoardcastMsg(msg))) {
DPRINT(F("-> "));
msg.dump();
// ignore repeated messages
Expand All @@ -184,7 +178,7 @@ class MultiChannelDevice : public Device<HalType> {
if( mtype == AS_MESSAGE_CONFIG ) {
DeviceType::activity().stayAwake(millis2ticks(500));
// PAIR_SERIAL
if( msubc == AS_CONFIG_PAIR_SERIAL && memcmp(msg.data(),DeviceType::getSerial(),10)==0 ) {
if( msubc == AS_CONFIG_PAIR_SERIAL && DeviceType::isDeviceSerial(msg.data())==true ) {
DeviceType::led().set(LedStates::pairing);
DeviceType::activity().stayAwake( seconds2ticks(20) ); // 20 seconds
DeviceType::sendDeviceInfo(DeviceType::getMasterID(),msg.length());
Expand Down Expand Up @@ -269,7 +263,6 @@ class MultiChannelDevice : public Device<HalType> {
// CONFIG_END
else if( msubc == AS_CONFIG_END ) {
if( cfgList.address() == list0.address() ) {
DeviceType::setMasterID(list0.masterid());
DeviceType::led().set(LedStates::nothing);
}
else {
Expand Down

0 comments on commit 07e100e

Please sign in to comment.