Skip to content

Commit

Permalink
numChannels moved into base device - so the number of channels is always
Browse files Browse the repository at this point in the history
correct in device info
  • Loading branch information
pa-pa committed Sep 18, 2017
1 parent e40a862 commit c6e165c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
20 changes: 19 additions & 1 deletion Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ class Device {
Message msg;
KeyStore kstore;

uint8_t numChannels;



public:
Device (uint16_t addr,List0& l) : hal(0), list0(l), msgcount(0), lastmsg(0), kstore(addr) {
Device (uint16_t addr,List0& l,uint8_t ch) : hal(0), list0(l), msgcount(0), lastmsg(0), kstore(addr), numChannels(ch) {
// TODO init seed
}
virtual ~Device () {}
Expand All @@ -85,6 +88,19 @@ class Device {

Message& message () { return msg; }

void channels (uint8_t num) {
numChannels = num;
}

uint8_t channels () const {
return numChannels;
}

bool hasChannel (uint8_t number) const {
return number != 0 && number <= channels();
}


bool isRepeat(const Message& m) {
if( m.isRepeated() && lastdev == m.from() && lastmsg == m.count() ) {
return true;
Expand Down Expand Up @@ -148,6 +164,8 @@ class Device {
void getDeviceInfo (uint8_t* info) {
uint8_t di[3] = {DEVICE_INFO};
memcpy(info,di,sizeof(di));
// patch real channel count into device info
*info = this->channels();
}

HMID getMasterID () {
Expand Down
56 changes: 23 additions & 33 deletions MultiChannelDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ class ChannelDevice : public Device<HalType> {

List0Type list0;
ChannelType* devchannels[ChannelCount];
uint8_t numChannels;
uint8_t cfgChannel;
GenericList cfgList;

public:

typedef Device<HalType> DeviceType;

ChannelDevice (uint16_t addr) : Device<HalType>(addr,list0), list0(addr + this->keystore().size()), numChannels(ChannelCount), cfgChannel(0xff) {}
ChannelDevice (uint16_t addr) : Device<HalType>(addr,list0,ChannelCount), list0(addr + this->keystore().size()), cfgChannel(0xff) {}

virtual ~ChannelDevice () {}

Expand All @@ -46,14 +45,23 @@ class ChannelDevice : public Device<HalType> {

void layoutChannels () {
uint16_t addr = list0.address() + list0.size();
for( uint8_t i=0; i<channels(); ++i ) {
for( uint8_t i=0; i<this->channels(); ++i ) {
devchannels[i]->setup(this,i+1,addr);
addr += devchannels[i]->size();
}
}

void channels (uint8_t num) {
DeviceType::channels(min(num,ChannelCount));
}

uint8_t channels () const {
return DeviceType::channels();
}


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

Expand All @@ -66,7 +74,7 @@ class ChannelDevice : public Device<HalType> {
crc = HalType::crc16(crc,list0.getRegister(i));
}
// add number of channels
for( uint8_t c=1; c<=channels(); ++c ) {
for( uint8_t c=1; c<=this->channels(); ++c ) {
ChannelType& ch = channel(c);
// add register list 1
GenericList l = ch.getList1();
Expand All @@ -93,24 +101,6 @@ class ChannelDevice : public Device<HalType> {
return list0;
}

void getDeviceInfo (uint8_t* info) {
DeviceType::getDeviceInfo(info);
// patch real channel count into device info
*info = channels();
}

void channels (uint8_t num) {
numChannels = min(num,ChannelCount);
}

uint8_t channels () const {
return numChannels;
}

bool hasChannel (uint8_t number) const {
return number != 0 && number <= channels();
}

void init (HalType& hal) {
layoutChannels();
dumpSize();
Expand All @@ -130,7 +120,7 @@ class ChannelDevice : public Device<HalType> {
void firstinit () {
this->keystore().defaults(); // init aes key infrastructure
list0.defaults();
for( uint8_t i=0; i<channels(); ++i ) {
for( uint8_t i=0; i<this->channels(); ++i ) {
devchannels[i]->firstinit();
}
}
Expand Down Expand Up @@ -161,7 +151,7 @@ class ChannelDevice : public Device<HalType> {

bool pollRadio () {
bool worked = DeviceType::pollRadio();
for( uint8_t i=1; i<=channels(); ++i ) {
for( uint8_t i=1; i<=this->channels(); ++i ) {
ChannelType& ch = channel(i);
if( ch.changed() == true ) {
this->sendInfoActuatorStatus(this->getMasterID(),this->nextcount(),ch);
Expand All @@ -175,7 +165,7 @@ class ChannelDevice : public Device<HalType> {
if( getList0().aesActive() == true ) {
return true;
}
for( uint8_t i=1; i<=channels(); ++i) {
for( uint8_t i=1; i<=this->channels(); ++i) {
if( channel(i).aesActive() == true ) {
return true;
}
Expand All @@ -194,7 +184,7 @@ class ChannelDevice : public Device<HalType> {

bool validSignature(uint8_t ch,Message& msg) {
#ifdef USE_AES
if( (ch==0 && aesActive()) || (hasChannel(ch)==true && channel(ch).aesActive()==true) ) {
if( (ch==0 && aesActive()) || (this->hasChannel(ch)==true && channel(ch).aesActive()==true) ) {
return this->requestSignature(msg);
}
#endif
Expand Down Expand Up @@ -225,7 +215,7 @@ class ChannelDevice : public Device<HalType> {
else if ( msubc == AS_CONFIG_PEER_ADD ) {
const ConfigPeerAddMsg& pm = msg.configPeerAdd();
bool success = false;
if( hasChannel(pm.channel()) == true ) {
if( this->hasChannel(pm.channel()) == true ) {
if( validSignature(pm.channel(),msg) == true ) {
ChannelType& ch = channel(pm.channel());
if( pm.peers() == 1 ) {
Expand All @@ -248,7 +238,7 @@ class ChannelDevice : public Device<HalType> {
else if ( msubc == AS_CONFIG_PEER_REMOVE ) {
const ConfigPeerRemoveMsg& pm = msg.configPeerRemove();
bool success = false;
if( hasChannel(pm.channel()) == true ) {
if( this->hasChannel(pm.channel()) == true ) {
if( validSignature(pm.channel(),msg) == true ) {
ChannelType& ch = channel(pm.channel());
success = ch.deletepeer(pm.peer1());
Expand All @@ -268,7 +258,7 @@ class ChannelDevice : public Device<HalType> {
// CONFIG_PEER_LIST_REQ
else if( msubc == AS_CONFIG_PEER_LIST_REQ ) {
const ConfigPeerListReqMsg& pm = msg.configPeerListReq();
if( hasChannel(pm.channel()) == true ) {
if( this->hasChannel(pm.channel()) == true ) {
this->sendInfoPeerList(msg.from(),msg.count(),channel(pm.channel()));
}
}
Expand Down Expand Up @@ -353,7 +343,7 @@ class ChannelDevice : public Device<HalType> {
else {
bool ack=false;
const ActionMsg& pm = msg.action();
if( hasChannel(pm.channel())==true ) {
if( this->hasChannel(pm.channel())==true ) {
ChannelType& ch = channel(pm.channel());
if( validSignature(pm.channel(),msg)==true ) {
switch( mcomm ) {
Expand Down Expand Up @@ -429,7 +419,7 @@ class ChannelDevice : public Device<HalType> {
}

uint8_t channelForPeer (const Peer& p) {
for( uint8_t x=1; x<=channels(); ++x ) {
for( uint8_t x=1; x<=this->channels(); ++x ) {
ChannelType& ch = channel(x);
for( uint8_t y=0; y<ch.peers(); ++y ) {
if( ch.peer(y) == p ) {
Expand All @@ -443,7 +433,7 @@ class ChannelDevice : public Device<HalType> {
GenericList findList(uint8_t ch,const Peer& peer,uint8_t numlist) {
if (numlist == 0) {
return list0;
} else if (hasChannel(ch) == true) {
} else if (this->hasChannel(ch) == true) {
ChannelType& c = channel(ch);
if (numlist == 1) {
return c.getList1();
Expand Down

0 comments on commit c6e165c

Please sign in to comment.