Skip to content
Snippets Groups Projects
Commit fd246ef0 authored by Peter Švihra's avatar Peter Švihra
Browse files

changed class inheritance to static method and dynamic allocations

parent 3c4654e5
Branches
No related merge requests found
......@@ -15,11 +15,19 @@ commsControl::commsControl(uint32_t baudrate) {
memset(commsReceived_, 0, sizeof(commsReceived_));
memset(commsSend_ , 0, sizeof(commsSend_ ));
queueAlarm_ = DataQueue<commsALARM>(CONST_MAX_SIZE_QUEUE);
queueData_ = DataQueue<commsDATA> (CONST_MAX_SIZE_QUEUE);
queueCmd_ = DataQueue<commsCMD> (CONST_MAX_SIZE_QUEUE);
queueAlarm_ = new DataQueue<commsFormat *>(CONST_MAX_SIZE_QUEUE);
queueData_ = new DataQueue<commsFormat *>(CONST_MAX_SIZE_QUEUE);
queueCmd_ = new DataQueue<commsFormat *>(CONST_MAX_SIZE_QUEUE);
commsTmp_ = commsFormat(CONST_MAX_SIZE_PACKET - CONST_MIN_SIZE_PACKET );
commsAck_ = commsFormat::generateACK();
commsNck_ = commsFormat::generateNACK();
}
// WIP
commsControl::~commsControl() {
;
}
void commsControl::beginSerial() {
......@@ -30,11 +38,11 @@ void commsControl::beginSerial() {
// TODO: needs switch on data type with global timeouts on data pushing
void commsControl::sender() {
if (millis() > lastTransTime_ + 5 ) {
sendQueue(reinterpret_cast<DataQueue<commsFormat>*>(&queueAlarm_));
sendQueue(queueAlarm_);
}
if (millis() > lastTransTime_ + 5000 ) {
sendQueue(reinterpret_cast<DataQueue<commsFormat>*>(&queueData_));
sendQueue(queueData_);
}
}
......@@ -71,7 +79,7 @@ void commsControl::receiver() {
// to decide what kind of packets received
uint8_t address = commsReceived_[1];
DataQueue<commsFormat>* tmpQueue = getQueue(address);
DataQueue<commsFormat *> *tmpQueue = getQueue(address);
if (tmpQueue != nullptr) {
// switch on received data to know what to do - received ACK/NACK or other
switch(control & COMMS_CONTROL_TYPES) {
......@@ -87,7 +95,8 @@ void commsControl::receiver() {
default:
// received DATA
receivePacket(tmpQueue);
sendPacket(&commsACK(address));
commsAck_->setAddress(&address);
sendPacket(commsAck_);
break;
}
}
......@@ -112,15 +121,15 @@ void commsControl::receiver() {
// adding new values into queue
// WIP
void commsControl::registerData(dataType type, dataFormat* values) {
commsDATA newValue;
void commsControl::registerData(dataType type, dataFormat *values) {
commsFormat *newValue = commsFormat::generateDATA();
// switch on different received data types
switch(type) {
case dataAlarm:
break;
case dataNormal:
newValue.setInformation(values);
newValue->setInformation(values);
break;
case dataCommand:
break;
......@@ -128,15 +137,13 @@ void commsControl::registerData(dataType type, dataFormat* values) {
break;
}
// calculate CRC for the new entry
newValue.generateCrc();
// add new entry to the queue
queueData_.enqueue(newValue);
queueData_->enqueue(newValue);
}
// general encoder of any transmission
bool commsControl::encoder(uint8_t* data, uint8_t dataSize) {
bool commsControl::encoder(uint8_t *data, uint8_t dataSize) {
if (dataSize > 0) {
commsSendSize_ = 0;
uint8_t tmpVal = 0;
......@@ -185,16 +192,15 @@ bool commsControl::decoder(uint8_t* data, uint8_t dataStart, uint8_t dataStop) {
}
// sending anything of commsDATA format
void commsControl::sendQueue(DataQueue<commsFormat>* queue) {
void commsControl::sendQueue(DataQueue<commsFormat *> *queue) {
// if have data to send
if (!queue->isEmpty()) {
// reset sending counter
lastTransTime_ = millis();
// TODO define transmission counter
// queue->front().setCounter(someValue)
// queue->front().setSequenceSend(sequenceSend_);
sendPacket(&(queue->front()));
sendPacket(queue->front());
}
}
......@@ -209,34 +215,35 @@ void commsControl::sendPacket(commsFormat *packet) {
// resending the packet, can lower the timeout since either NACK or wrong FCS already checked
//WIP
void commsControl::resendPacket(DataQueue<commsFormat>* queue) {
void commsControl::resendPacket(DataQueue<commsFormat *> *queue) {
;
}
// receiving anything of commsFormat
// WIP
void commsControl::receivePacket(DataQueue<commsFormat>* queue) {
void commsControl::receivePacket(DataQueue<commsFormat *> *queue) {
;
}
// if FCS is ok, remove from queue
void commsControl::finishPacket(DataQueue<commsFormat>* queue) {
void commsControl::finishPacket(DataQueue<commsFormat *> *queue) {
// TODO check if transmission counter is aligned with transfer to remove
if (!queue->isEmpty()) {
sequenceSend_ = (sequenceSend_ + 1) % 128;
queue->dequeue();
}
}
// get link to queue according to packet format
DataQueue<commsFormat>* commsControl::getQueue(uint8_t address) {
DataQueue<commsFormat *> *commsControl::getQueue(uint8_t address) {
switch (address & PACKET_TYPE) {
case PACKET_ALARM:
return reinterpret_cast<DataQueue<commsFormat>*>(&queueAlarm_);
return queueAlarm_;
case PACKET_CMD:
return reinterpret_cast<DataQueue<commsFormat>*>(&queueCmd_);
return queueCmd_;
case PACKET_DATA:
return reinterpret_cast<DataQueue<commsFormat>*>(&queueData_);
return queueData_;
default:
return nullptr;
}
......
#ifndef COMMS_CONTROL_H
#define COMMS_CONTROL_H
// Communication protocol between rasp and arduino based on HDLC format
// author Peter Svihra <peter.svihra@cern.ch>
#include <Arduino.h>
#include "Queue.h"
......@@ -12,6 +15,7 @@
class commsControl {
public:
commsControl(uint32_t baudrate = 115200);
~commsControl();
void beginSerial();
......@@ -22,12 +26,12 @@ public:
void receiver();
private:
DataQueue<commsFormat>* getQueue(uint8_t address);
DataQueue<commsFormat *> *getQueue(uint8_t address);
void sendQueue (DataQueue<commsFormat>* queue);
void resendPacket (DataQueue<commsFormat>* queue);
void receivePacket(DataQueue<commsFormat>* queue);
void finishPacket (DataQueue<commsFormat>* queue);
void sendQueue (DataQueue<commsFormat *> *queue);
void resendPacket (DataQueue<commsFormat *> *queue);
void receivePacket(DataQueue<commsFormat *> *queue);
void finishPacket (DataQueue<commsFormat *> *queue);
bool encoder(uint8_t* data, uint8_t dataSize);
bool decoder(uint8_t* data, uint8_t dataStart, uint8_t dataStop);
......@@ -35,9 +39,15 @@ private:
void sendPacket(commsFormat* packet);
private:
DataQueue<commsALARM> queueAlarm_;
DataQueue<commsDATA> queueData_ ;
DataQueue<commsCMD> queueCmd_ ;
uint8_t sequenceSend_;
uint8_t sequenceReceive_;
commsFormat* commsAck_;
commsFormat* commsNck_;
DataQueue<commsFormat*>* queueAlarm_;
DataQueue<commsFormat*>* queueData_ ;
DataQueue<commsFormat*>* queueCmd_ ;
commsFormat commsTmp_;
......
......@@ -27,8 +27,17 @@ void commsFormat::assignBytes(uint8_t* target, uint8_t* source, uint8_t size, bo
}
}
void commsFormat::setCounter(uint8_t counter) {
*(getControl() + 2) |= (counter << 4);
void commsFormat::setSequenceSend(uint8_t counter) {
// sequence sent valid only for info frames (not supervisory ACK/NACK)
if ((*getControl() & COMMS_CONTROL_TYPES) == 0) {
counter = (counter << 1) & 0xFE;
assignBytes(getControl() + 1, &counter, 1);
}
}
void commsFormat::setSequenceReceive(uint8_t counter) {
counter = (counter << 1) & 0xFE;
assignBytes(getControl() , &counter, 1);
}
// compare calculated and received CRC value
......@@ -61,6 +70,8 @@ void commsFormat::generateCrc(bool assign) {
void commsFormat::setInformation(dataFormat* values) {
// assign values to
memcpy(getInformation(), &values->count, 2);
generateCrc();
}
void commsFormat::copyData(uint8_t* data, uint8_t dataSize) {
......
#ifndef COMMSFORMAT_H
#define COMMSFORMAT_H
// Communication protocol based on HDLC format
// author Peter Svihra <peter.svihra@cern.ch>
#include <Arduino.h>
#include <uCRC16Lib.h>
#include "commsConstants.h"
///////////////////////////////////////////////////////////////////////////
// class to provide all needed control in data format
class commsFormat {
......@@ -16,6 +18,8 @@ public:
uint8_t* getData() { return data_; }
uint8_t getSize() { return packetSize_; }
void setAddress(uint8_t* address) {assignBytes(getAddress(), address, 1); }
void setControl(uint8_t* control) {assignBytes(getControl(), control, 2); }
void setInformation(dataFormat* values);
void assignBytes(uint8_t* target, uint8_t* source, uint8_t size, bool calcCrc = true);
......@@ -31,9 +35,19 @@ public:
uint8_t* getFcs() {return data_ + 4 + infoSize_;} // checksum
uint8_t* getStop() {return data_ + 4 + infoSize_ + 2;} // ending flag of the chain
void setCounter(uint8_t counter);
void setSequenceSend (uint8_t counter);
void setSequenceReceive(uint8_t counter);
void copyData(uint8_t* data, uint8_t dataSize);
static commsFormat* generateACK() { return new commsFormat(0, 0, COMMS_CONTROL_ACK << 8); }
static commsFormat* generateNACK() { return new commsFormat(0, 0, COMMS_CONTROL_NACK << 8); }
static commsFormat* generateALARM() { return new commsFormat(4, PACKET_ALARM); }
static commsFormat* generateCMD() { return new commsFormat(8, PACKET_CMD ); }
static commsFormat* generateDATA() { return new commsFormat(8, PACKET_DATA ); }
private:
uint8_t data_[CONST_MAX_SIZE_PACKET];
uint8_t packetSize_;
......@@ -41,39 +55,4 @@ private:
uint16_t crc_;
};
///////////////////////////////////////////////////////////////////////////
// ALARM specific class - contains X bytes of data and specific control flag
class commsALARM: public commsFormat {
public:
commsALARM() : commsFormat(4, PACKET_ALARM) {;} // contains 4 information bytes
};
///////////////////////////////////////////////////////////////////////////
// CMD specific class - contains X bytes of data and specific control flag
class commsCMD: public commsFormat {
public:
commsCMD() : commsFormat(8, PACKET_CMD) {;} // contains 8 information bytes
};
///////////////////////////////////////////////////////////////////////////
// DATA specific class - contains X bytes of data
class commsDATA: public commsFormat {
public:
commsDATA() : commsFormat(8, PACKET_DATA) {;} // contains 8 information bytes
};
///////////////////////////////////////////////////////////////////////////
// ACK specific class - contains specific control flag
class commsACK : public commsFormat {
public:
commsACK(uint8_t address) : commsFormat(0, address, COMMS_CONTROL_ACK << 8) {;} // contains 0 information bytes, has specific control type
};
///////////////////////////////////////////////////////////////////////////
// NACK specific class - contains specific control flag
class commsNACK: public commsFormat {
public:
commsNACK(uint8_t address) : commsFormat(0, address, COMMS_CONTROL_NACK << 8) {;} // contains 0 information bytes, has specific control type
};
#endif // COMMSFORMAT_H
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment