Skip to content

Commit

Permalink
Add ValueID::GetAsString() for debugging purposes (OpenZWave#2040)
Browse files Browse the repository at this point in the history
* Add ValueID::GetAsString() for debugging purposes

This function allows to convert a valueID to human readable form

Example (from tests):

ValueID(static_cast<uint64>(0x1), 0x02, ValueID::ValueGenre_System, 0x04, 5, 6, ValueID::ValueType_String).GetAsString()

"HomeID: 0x00000001, ValueID: (Id 0x0006000002c10057, NodeID 2, Genre system, CC 0x04, Instance 5, Index 6, Type string)"

Related to OpenZWave#2038 "Ozwcache corruption leading to memory corruption and crash"

* Add Log to Node::GetValue in case retrieved ID does not match request ID

* no need to introduce a namespace

Co-authored-by: Justin Hammond <[email protected]>
  • Loading branch information
petergebruers and Fishwaldo committed Dec 26, 2019
1 parent a53dde3 commit 4d1d442
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
12 changes: 11 additions & 1 deletion cpp/src/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2758,7 +2758,17 @@ void Node::ReadValueFromXML(uint8 const _commandClassId, TiXmlElement const* _va
Internal::VC::Value* Node::GetValue(ValueID const& _id)
{
// This increments the value's reference count
return GetValueStore()->GetValue(_id.GetValueStoreKey());
auto value = GetValueStore()->GetValue(_id.GetValueStoreKey());

if (value->GetID().GetId() != _id.GetId())
{
Log::Write(LogLevel_Error, m_nodeId, "Node::GetValue called with: %s but GetValueStore returned: %s",
_id.GetAsString().c_str(), value->GetID().GetAsString().c_str());

value->Release();
return nullptr;
}
return value;
}

//-----------------------------------------------------------------------------
Expand Down
21 changes: 21 additions & 0 deletions cpp/src/value_classes/ValueID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

#include "ValueID.h"
#include "Value.h"
#include <sstream>
#include <iomanip>


namespace OpenZWave
{
Expand All @@ -48,4 +51,22 @@ namespace OpenZWave
return Internal::VC::Value::GetTypeNameFromEnum(GetType());
}

string const ValueID::GetAsString() const
{
// Match constructor order
// ValueID(uint32 const _homeId, uint8 const _nodeId, ValueGenre const _genre, uint8 const _commandClassId,
// uint8 const _instance, uint16 const _valueIndex, ValueType const _type)
std::ostringstream s;

s
<< "HomeID: 0x" << hex << setfill('0') << setw(8) << GetHomeId()
<< ", ValueID: (Id 0x" << setw(16) << GetId() << dec << setfill(' ')
<< ", NodeID " << static_cast<unsigned int>(GetNodeId())
<< ", Genre " << GetGenreAsString()
<< ", CC 0x" << hex << setfill('0') << setw(2) << static_cast<unsigned int>(GetCommandClassId()) << dec << setfill(' ')
<< ", Instance " << static_cast<unsigned int>(GetInstance())
<< ", Index " << static_cast<unsigned int>(GetIndex())
<< ", Type " << GetTypeAsString() << ')';
return s.str();
}
}// namespace OpenZWave
5 changes: 5 additions & 0 deletions cpp/src/value_classes/ValueID.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ namespace OpenZWave
{
return (uint64) (((uint64) m_id1 << 32) | m_id);
}
/**
* GetAsString returns a string representing the ValueID in human readable form
* \return a std::string
*/
string const GetAsString() const;

// Comparison Operators
bool operator ==(ValueID const& _other) const
Expand Down
14 changes: 13 additions & 1 deletion cpp/test/ValueID_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TEST(OpenZWave, Version)
// "Expected: (ozw_vers_revision) >= (900), actual: 0 vs 900 [ FAILED ] OpenZWave.Version (0 ms)""
if (ozw_vers_revision != 0)
{
EXPECT_GE(ozw_vers_revision, 900);
EXPECT_GE(ozw_vers_revision, 992);
}
}
TEST(ValueID, Constructor)
Expand Down Expand Up @@ -120,5 +120,17 @@ TEST(ValueID, GetStoreKey)
delete vid5;
delete vid6;
}
TEST(ValueID, GetAsString)
{
EXPECT_EQ(
ValueID(0xFFFF, static_cast<uint64>(0x400000133002A)).GetAsString(),
"HomeID: 0x0000ffff, ValueID: (Id 0x000400000133002a, NodeID 1, Genre basic, CC 0xcc, Instance 2, Index 4, Type bitset)");
EXPECT_EQ(
ValueID(static_cast<uint64>(0x1), 0x02, ValueID::ValueGenre_System, 0x04, 5, 6, ValueID::ValueType_String).GetAsString(),
"HomeID: 0x00000001, ValueID: (Id 0x0006000002c10057, NodeID 2, Genre system, CC 0x04, Instance 5, Index 6, Type string)");
EXPECT_EQ(
ValueID(static_cast<uint64>(0xABCDEF01), static_cast<uint64>(0x0123456789ABCDEF)).GetAsString(),
"HomeID: 0xabcdef01, ValueID: (Id 0x0123456789abcdef, NodeID 137, Genre config, CC 0xaf, Instance 222, Index 291, Type invalid type)");
}
} // namespace Testing
} // namespace OpenZWave

0 comments on commit 4d1d442

Please sign in to comment.