Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased
**New features:**
* Added an option to generate uppercase unique IDs from byte arrays.

## 2.1.0

**New features:**
Expand Down
9 changes: 8 additions & 1 deletion src/HADevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,19 @@ HADevice::~HADevice()
}

bool HADevice::setUniqueId(const byte* uniqueId, const uint16_t length)
{
// For backward compatibility, default to lowercase.
return setUniqueId(uniqueId, length, false);
}

bool HADevice::setUniqueId(const byte* uniqueId, const uint16_t length, bool uppercase)
{
if (_uniqueId) {
return false; // unique ID cannot be changed at runtime once it's set
}

_uniqueId = HAUtils::byteArrayToStr(uniqueId, length);
// Call the new HAUtils function with the uppercase flag.
_uniqueId = HAUtils::byteArrayToStr(uniqueId, length, uppercase);
_ownsUniqueId = true;
_serializer->set(AHATOFSTR(HADeviceIdentifiersProperty), _uniqueId);
return true;
Expand Down
13 changes: 12 additions & 1 deletion src/HADevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,24 @@ class HADevice

/**
* Sets unique ID of the device based on the given byte array.
* Each byte is converted into a hex string representation, so the final length of the unique ID will be twice as given.
* Each byte is converted into a hex string representation (lowercase by default), so the final length of the unique ID will be twice as given.
*
* @param uniqueId Bytes array that's going to be converted into the string.
* @param length Number of bytes in the array.
* @note The unique ID can be set only once (via constructor or using this method).
*/
bool setUniqueId(const byte* uniqueId, const uint16_t length);

/**
* Sets unique ID of the device based on the given byte array with case selection.
* Each byte is converted into a hex string representation, so the final length of the unique ID will be twice as given.
*
* @param uniqueId Bytes array that's going to be converted into the string.
* @param length Number of bytes in the array.
* @param uppercase If true, the output will be uppercase; otherwise, it will be lowercase.
* @note The unique ID can be set only once (via constructor or using this method).
*/
bool setUniqueId(const byte* uniqueId, const uint16_t length, bool uppercase);

/**
* Sets the "manufacturer" property that's going to be displayed in the Home Assistant.
Expand Down
32 changes: 27 additions & 5 deletions src/utils/HAUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,34 @@ void HAUtils::byteArrayToStr(
}

char* HAUtils::byteArrayToStr(
const byte* src,
const uint16_t length
const byte* data,
const uint16_t length
)
{
// For backward compatibility, default to lowercase.
return byteArrayToStr(data, length, false);
}

char* HAUtils::byteArrayToStr(
const byte* data,
const uint16_t length,
bool uppercase
)
{
char* dst = new char[(length * 2) + 1]; // include null terminator
byteArrayToStr(dst, src, length);
const uint16_t strLength = length * 2;
if (strLength == 0) {
return nullptr;
}

char* str = new char[strLength + 1];
str[strLength] = '\0';

// Choose the format specifier based on the 'uppercase' parameter.
const char* format = uppercase ? "%02X" : "%02x";

for (int i = 0; i < length; i++) {
sprintf(&str[i * 2], format, data[i]);
}

return dst;
return str;
}
29 changes: 20 additions & 9 deletions src/utils/HAUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,28 @@ class HAUtils
);

/**
* Converts the given byte array into hex string.
* This method allocates a new memory.
*
* @param src Bytes array to convert.
* @param length Length of the bytes array.
* @returns Newly allocated string containing the hex representation.
* Converts given byte array into the string of hex values.
* @param data Bytes to convert.
* @param length Number of bytes to convert.
* @return Dynamically allocated string with hex values. You're responsible for deleting it.
*/
static char* byteArrayToStr(
const byte* src,
const uint16_t length
);
const byte* data,
const uint16_t length
);

/**
* Converts given byte array into the string of hex values with optional uppercase formatting.
* @param data Bytes to convert.
* @param length Number of bytes to convert.
* @param uppercase If true, the output will be uppercase; otherwise, it will be lowercase.
* @return Dynamically allocated string with hex values. You're responsible for deleting it.
*/
static char* byteArrayToStr(
const byte* data,
const uint16_t length,
bool uppercase
);
};

#endif