Skip to content

MyMessage class maintenance #1325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 36 additions & 33 deletions core/MyMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@

MyMessage::MyMessage(void)
{
clear();
this->clear();
}

MyMessage::MyMessage(const uint8_t _sensorId, const mysensors_data_t _dataType)
{
clear();
(void)setSensor(_sensorId);
(void)setType(static_cast<uint8_t>(_dataType));
this->clear();
(void)this->setSensor(_sensorId);
(void)this->setType(static_cast<uint8_t>(_dataType));
}

void MyMessage::clear(void)
Expand All @@ -45,10 +45,11 @@ void MyMessage::clear(void)
this->command_echo_payload = 0u;
this->type = 0u;
this->sensor = 0u;
// clear data buffer
(void)memset((void *)this->data, 0u, sizeof(this->data));

// set message protocol version
(void)this->setVersion(PROTOCOL_VERSION);
(void)this->setVersion();
}

uint8_t MyMessage::getHeaderSize(void) const
Expand All @@ -68,17 +69,17 @@ uint8_t MyMessage::getExpectedMessageSize(void) const

bool MyMessage::isProtocolVersionValid(void) const
{
return (this->getVersion() == PROTOCOL_VERSION);
return (this->getVersion() == V2_MYS_HEADER_PROTOCOL_VERSION);
}

uint8_t MyMessage::getType(void) const
{
return this->type;
}

MyMessage& MyMessage::setType(const uint8_t _messageType)
MyMessage& MyMessage::setType(const uint8_t messageType)
{
this->type = _messageType;
this->type = messageType;
return *this;
}

Expand All @@ -87,9 +88,9 @@ uint8_t MyMessage::getLast(void) const
return this->last;
}

MyMessage& MyMessage::setLast(const uint8_t _lastId)
MyMessage& MyMessage::setLast(const uint8_t lastId)
{
this->last = _lastId;
this->last = lastId;
return *this;
}

Expand All @@ -98,9 +99,9 @@ uint8_t MyMessage::getSender(void) const
return this->sender;
}

MyMessage& MyMessage::setSender(const uint8_t _senderId)
MyMessage& MyMessage::setSender(const uint8_t senderId)
{
this->sender = _senderId;
this->sender = senderId;
return *this;
}

Expand All @@ -109,9 +110,9 @@ uint8_t MyMessage::getSensor(void) const
return this->sensor;
}

MyMessage& MyMessage::setSensor(const uint8_t _sensorId)
MyMessage& MyMessage::setSensor(const uint8_t sensorId)
{
this->sensor = _sensorId;
this->sensor = sensorId;
return *this;
}

Expand All @@ -120,9 +121,9 @@ uint8_t MyMessage::getDestination(void) const
return this->destination;
}

MyMessage& MyMessage::setDestination(const uint8_t _destinationId)
MyMessage& MyMessage::setDestination(const uint8_t destinationId)
{
this->destination = _destinationId;
this->destination = destinationId;
return *this;
}

Expand All @@ -138,9 +139,9 @@ bool MyMessage::isEcho(void) const
V2_MYS_HEADER_CEP_ECHOEQUEST_SIZE);
}

MyMessage& MyMessage::setEcho(const bool _echo)
MyMessage& MyMessage::setEcho(const bool echo)
{
BF_SET(this->command_echo_payload, _echo, V2_MYS_HEADER_CEP_ECHOREQUEST_POS,
BF_SET(this->command_echo_payload, echo, V2_MYS_HEADER_CEP_ECHOREQUEST_POS,
V2_MYS_HEADER_CEP_ECHOEQUEST_SIZE);
return *this;
}
Expand All @@ -151,9 +152,9 @@ bool MyMessage::getRequestEcho(void) const
V2_MYS_HEADER_CEP_ECHO_SIZE);
}

MyMessage& MyMessage::setRequestEcho(const bool _requestEcho)
MyMessage& MyMessage::setRequestEcho(const bool requestEcho)
{
BF_SET(this->command_echo_payload, _requestEcho, V2_MYS_HEADER_CEP_ECHO_POS,
BF_SET(this->command_echo_payload, requestEcho, V2_MYS_HEADER_CEP_ECHO_POS,
V2_MYS_HEADER_CEP_ECHO_SIZE);
return *this;
}
Expand All @@ -164,9 +165,9 @@ uint8_t MyMessage::getVersion(void) const
V2_MYS_HEADER_VSL_VERSION_SIZE);
}

MyMessage& MyMessage::setVersion(const uint8_t _version)
MyMessage& MyMessage::setVersion(void)
{
BF_SET(this->version_length, _version, V2_MYS_HEADER_VSL_VERSION_POS,
BF_SET(this->version_length, V2_MYS_HEADER_PROTOCOL_VERSION, V2_MYS_HEADER_VSL_VERSION_POS,
V2_MYS_HEADER_VSL_VERSION_SIZE);
return *this;
}
Expand All @@ -177,9 +178,9 @@ mysensors_command_t MyMessage::getCommand(void) const
V2_MYS_HEADER_CEP_COMMAND_POS, V2_MYS_HEADER_CEP_COMMAND_SIZE));
}

MyMessage& MyMessage::setCommand(const mysensors_command_t _command)
MyMessage& MyMessage::setCommand(const mysensors_command_t command)
{
BF_SET(this->command_echo_payload, static_cast<uint8_t>(_command), V2_MYS_HEADER_CEP_COMMAND_POS,
BF_SET(this->command_echo_payload, static_cast<uint8_t>(command), V2_MYS_HEADER_CEP_COMMAND_POS,
V2_MYS_HEADER_CEP_COMMAND_SIZE);
return *this;
}
Expand All @@ -190,9 +191,9 @@ mysensors_payload_t MyMessage::getPayloadType(void) const
V2_MYS_HEADER_CEP_PAYLOADTYPE_POS, V2_MYS_HEADER_CEP_PAYLOADTYPE_SIZE));
}

MyMessage& MyMessage::setPayloadType(const mysensors_payload_t _payloadType)
MyMessage& MyMessage::setPayloadType(const mysensors_payload_t payloadType)
{
BF_SET(this->command_echo_payload, static_cast<uint8_t>(_payloadType),
BF_SET(this->command_echo_payload, static_cast<uint8_t>(payloadType),
V2_MYS_HEADER_CEP_PAYLOADTYPE_POS, V2_MYS_HEADER_CEP_PAYLOADTYPE_SIZE);
return *this;
}
Expand All @@ -203,9 +204,10 @@ bool MyMessage::getSigned(void) const
V2_MYS_HEADER_VSL_SIGNED_SIZE);
}

MyMessage& MyMessage::setSigned(const bool _signed)
MyMessage& MyMessage::setSigned(const bool signedFlag)
{
BF_SET(this->version_length, _signed, V2_MYS_HEADER_VSL_SIGNED_POS, V2_MYS_HEADER_VSL_SIGNED_SIZE);
BF_SET(this->version_length, signedFlag, V2_MYS_HEADER_VSL_SIGNED_POS,
V2_MYS_HEADER_VSL_SIGNED_SIZE);
return *this;
}

Expand All @@ -220,15 +222,16 @@ uint8_t MyMessage::getLength(void) const
return length;
}

MyMessage& MyMessage::setLength(const uint8_t _length)
MyMessage& MyMessage::setLength(const uint8_t length)
{
uint8_t length = _length;
uint8_t finalLength = length;
// limit length
if (length > MAX_PAYLOAD_SIZE) {
length = MAX_PAYLOAD_SIZE;
if (finalLength > MAX_PAYLOAD_SIZE) {
finalLength = MAX_PAYLOAD_SIZE;
}

BF_SET(this->version_length, length, V2_MYS_HEADER_VSL_LENGTH_POS, V2_MYS_HEADER_VSL_LENGTH_SIZE);
BF_SET(this->version_length, finalLength, V2_MYS_HEADER_VSL_LENGTH_POS,
V2_MYS_HEADER_VSL_LENGTH_SIZE);
return *this;
}

Expand Down
59 changes: 28 additions & 31 deletions core/MyMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
#define V2_MYS_HEADER_CEP_PAYLOADTYPE_POS (5u) //!< bitfield position payload type field
#define V2_MYS_HEADER_CEP_PAYLOADTYPE_SIZE (3u) //!< size payload type field

#define PROTOCOL_VERSION V2_MYS_HEADER_PROTOCOL_VERSION //!< The version of the protocol
#define MAX_MESSAGE_SIZE V2_MYS_HEADER_MAX_MESSAGE_SIZE //!< The maximum size of a message (including header)
#define HEADER_SIZE V2_MYS_HEADER_SIZE //!< The size of the header
#define MAX_PAYLOAD_SIZE (MAX_MESSAGE_SIZE - HEADER_SIZE) //!< The maximum size of a payload depends on #MAX_MESSAGE_SIZE and #HEADER_SIZE
Expand Down Expand Up @@ -301,10 +300,10 @@ class MyMessage

/**
* Constructor
* @param _sensorId id of the child sensor for this message
* @param _dataType
* @param sensorId id of the child sensor for this message
* @param dataType
*/
MyMessage(const uint8_t _sensorId, const mysensors_data_t _dataType);
MyMessage(const uint8_t sensorId, const mysensors_data_t dataType);

/**
* @brief Clear message contents.
Expand Down Expand Up @@ -396,7 +395,6 @@ class MyMessage
*/
uint8_t getExpectedMessageSize(void) const;


/**
* @brief isProtocolVersionValid
* @return true if the protocol version is valid
Expand All @@ -411,9 +409,9 @@ class MyMessage

/**
* @brief Setter for echo request
* @param _requestEcho
* @param requestEcho
*/
MyMessage& setRequestEcho(const bool _requestEcho);
MyMessage& setRequestEcho(const bool requestEcho);

/**
* @brief Getter for version
Expand All @@ -423,9 +421,8 @@ class MyMessage

/**
* @brief Setter for version
* @param _version
*/
MyMessage& setVersion(const uint8_t _version);
MyMessage& setVersion(void);

/**
* @brief Getter for length
Expand All @@ -435,9 +432,9 @@ class MyMessage

/**
* @brief Setter for length
* @param _length
* @param length
*/
MyMessage& setLength(const uint8_t _length);
MyMessage& setLength(const uint8_t length);

/**
* @brief Getter for command type
Expand All @@ -447,9 +444,9 @@ class MyMessage

/**
* @brief Setter for command type
* @param _command
* @param command
*/
MyMessage& setCommand(const mysensors_command_t _command);
MyMessage& setCommand(const mysensors_command_t command);

/**
* @brief Getter for payload type
Expand All @@ -459,9 +456,9 @@ class MyMessage

/**
* @brief Setter for payload type
* @param _payloadType
* @param payloadType
*/
MyMessage& setPayloadType(const mysensors_payload_t _payloadType);
MyMessage& setPayloadType(const mysensors_payload_t payloadType);

/**
* @brief Getter for sign field
Expand All @@ -471,9 +468,9 @@ class MyMessage

/**
* @brief Setter for sign field
* @param _signed
* @param signedFlag
*/
MyMessage& setSigned(const bool _signed);
MyMessage& setSigned(const bool signedFlag);

/**
* \deprecated use isEcho()
Expand All @@ -490,9 +487,9 @@ class MyMessage

/**
* @brief Setter for echo-flag.
* @param _echo true if this an echo message
* @param echo true if this an echo message
*/
MyMessage& setEcho(const bool _echo);
MyMessage& setEcho(const bool echo);

/**
* @brief Get message type
Expand All @@ -502,9 +499,9 @@ class MyMessage

/**
* @brief Set message type
* @param _messageType
* @param messageType
*/
MyMessage& setType(const uint8_t _messageType);
MyMessage& setType(const uint8_t messageType);

/**
* @brief Get last ID
Expand All @@ -514,9 +511,9 @@ class MyMessage

/**
* @brief Set last ID
* @param _lastId
* @param lastId
*/
MyMessage& setLast(const uint8_t _lastId);
MyMessage& setLast(const uint8_t lastId);

/**
* @brief Get sender ID
Expand All @@ -526,9 +523,9 @@ class MyMessage

/**
* @brief Set sender ID
* @param _senderId
* @param senderId
*/
MyMessage& setSender(const uint8_t _senderId);
MyMessage& setSender(const uint8_t senderId);

/**
* @brief Get sensor ID of message
Expand All @@ -538,9 +535,9 @@ class MyMessage

/**
* @brief Set which child sensor this message belongs to
* @param _sensorId
* @param sensorId
*/
MyMessage& setSensor(const uint8_t _sensorId);
MyMessage& setSensor(const uint8_t sensorId);

/**
* @brief Get destination
Expand All @@ -550,16 +547,16 @@ class MyMessage

/**
* @brief Set final destination node id for this message
* @param _destinationId
* @param destinationId
*/
MyMessage& setDestination(const uint8_t _destinationId);
MyMessage& setDestination(const uint8_t destinationId);

/**
* @brief Set entire payload
* @param payload pointer to the buffer where the payload is stored
* @param _length of the payload
* @param length of the payload
*/
MyMessage& set(const void* payload, const size_t _length);
MyMessage& set(const void* payload, const size_t length);

/**
* @brief Set payload to character array
Expand Down
1 change: 1 addition & 0 deletions core/MyTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,7 @@ void transportProcessFIFO(void)
bool transportSendWrite(const uint8_t to, MyMessage &message)
{
message.setLast(_transportConfig.nodeId); // Update last

// sign message if required
if (!signerSignMsg(message)) {
TRANSPORT_DEBUG(PSTR("!TSF:MSG:SIGN FAIL\n"));
Expand Down
4 changes: 2 additions & 2 deletions hal/transport/MyTransportHAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ bool transportHALReceive(MyMessage *inMsg, uint8_t *msgLength)
MyMessage tmp = *inMsg;
if (!tmp.isProtocolVersionValid()) {
setIndication(INDICATION_ERR_VERSION);
TRANSPORT_HAL_DEBUG(PSTR("!THA:RCV:PVER,%" PRIu8 "!=%" PRIu8 "\n"), tmp.getVersion(),
PROTOCOL_VERSION); // protocol version mismatch
TRANSPORT_HAL_DEBUG(PSTR("!THA:RCV:PVER=%" PRIu8 "\n"),
tmp.getVersion()); // protocol version mismatch
return false;
}
*msgLength = tmp.getLength();
Expand Down
Loading