Skip to content
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

Fix Sending Wrong Values on Failed Conversion to DPT #30

Merged
merged 9 commits into from
Nov 18, 2024
33 changes: 25 additions & 8 deletions src/knx/group_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,11 @@ GroupObjectUpdatedHandler GroupObject::callback()

void GroupObject::value(const KNXValue& value, const Dpt& type)
{
valueNoSend(value, type);
objectWritten();
if (_valueNoSend(value, type))
{
// write on successful conversion/setting value only
objectWritten();
}
}


Expand Down Expand Up @@ -282,26 +285,40 @@ void GroupObject::valueNoSend(const KNXValue& value)

void GroupObject::valueNoSend(const KNXValue& value, const Dpt& type)
{
if (_commFlagEx.uninitialized)
// ignore actual updating TODO check replacing valueNoSend with _valueNoSend
_valueNoSend(value, type);
}

bool GroupObject::_valueNoSend(const KNXValue& value, const Dpt& type)
{
const bool encodingDone = KNX_Encode_Value(value, _data, _dataLength, type);

// initialize on succesful conversion only
if (encodingDone && _commFlagEx.uninitialized)
commFlag(Ok);

KNX_Encode_Value(value, _data, _dataLength, type);
return encodingDone;
}

bool GroupObject::valueNoSendCompare(const KNXValue& value, const Dpt& type)
{
if (_commFlagEx.uninitialized)
{
// always set first value
this->valueNoSend(value, type);
return true;
return _valueNoSend(value, type);
}
else
{
// convert new value to given dtp
// convert new value to given DPT
uint8_t newData[_dataLength];
memset(newData, 0, _dataLength);
KNX_Encode_Value(value, newData, _dataLength, type);
const bool encodingDone = KNX_Encode_Value(value, newData, _dataLength, type);
if (!encodingDone)
{
// value conversion to DPT failed
// do NOT update the value of the KO!
return false;
}

// check for change in converted value / update value on change only
const bool dataChanged = memcmp(_data, newData, _dataLength);
Expand Down
11 changes: 11 additions & 0 deletions src/knx/group_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,15 @@ class GroupObject
GroupObjectUpdatedHandler _updateHandler;
Dpt _datapointType;
#endif

/**
* set the current value of the group object and show success.
* @param value the value the group object is set to
* @param type the datapoint type used for the conversion.
*
* The parameters must fit the group object. Otherwise it will stay unchanged.
*
* @returns true if the value of the group object was updated after successful conversion.
*/
bool _valueNoSend(const KNXValue& value, const Dpt& type);
};
Loading