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

USB serial number descriptor should report device ID in lower case (0.7.x) #1436

Merged
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
2 changes: 1 addition & 1 deletion bootloader/src/stm32f2xx/usbd_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ uint8_t * USBD_USR_SerialStrDescriptor( uint8_t speed , uint16_t *length)
char deviceIdHex[sizeof(deviceId) * 2 + 1] = {0};
unsigned deviceIdLen = 0;
deviceIdLen = HAL_device_ID(deviceId, sizeof(deviceId));
bytes2hexbuf(deviceId, deviceIdLen, deviceIdHex);
bytes2hexbuf_lower_case(deviceId, deviceIdLen, deviceIdHex);
USBD_GetString (deviceIdHex, USBD_StrDesc, length);
return USBD_StrDesc;
}
Expand Down
2 changes: 1 addition & 1 deletion hal/src/stm32f2xx/usbd_desc_stm32f2xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ uint8_t * USBD_USR_SerialStrDescriptor( uint8_t speed , uint16_t *length)
char deviceIdHex[sizeof(deviceId) * 2 + 1] = {0};
unsigned deviceIdLen = 0;
deviceIdLen = HAL_device_ID(deviceId, sizeof(deviceId));
bytes2hexbuf(deviceId, deviceIdLen, deviceIdHex);
bytes2hexbuf_lower_case(deviceId, deviceIdLen, deviceIdHex);
USBD_GetString (deviceIdHex, USBD_StrDesc, length);
return USBD_StrDesc;
}
Expand Down
34 changes: 29 additions & 5 deletions services/inc/bytes2hexbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@

static inline char ascii_nibble(uint8_t nibble) {
char hex_digit = nibble + 48;
if (57 < hex_digit)
if (57 < hex_digit) {
hex_digit += 7;
}
return hex_digit;
}

static inline char ascii_nibble_lower_case(uint8_t nibble) {
char hex_digit = nibble + 48;
if (57 < hex_digit) {
hex_digit += 39;
}
return hex_digit;
}

Expand All @@ -31,17 +40,32 @@ static inline char* concat_nibble(char* p, uint8_t nibble)
return p;
}

static inline char* concat_nibble_lower_case(char* p, uint8_t nibble)
{
*p++ = ascii_nibble_lower_case(nibble);
return p;
}

static inline char* bytes2hexbuf(const uint8_t* buf, unsigned len, char* out)
{
unsigned i;
char* result = out;
for (i = 0; i < len; ++i)
{
concat_nibble(out, (buf[i] >> 4));
out++;
concat_nibble(out, (buf[i] & 0xF));
out++;
out = concat_nibble(out, (buf[i] >> 4));
out = concat_nibble(out, (buf[i] & 0xF));
}
return result;
}

static inline char* bytes2hexbuf_lower_case(const uint8_t* buf, unsigned len, char* out)
{
unsigned i;
char* result = out;
for (i = 0; i < len; ++i)
{
out = concat_nibble_lower_case(out, (buf[i] >> 4));
out = concat_nibble_lower_case(out, (buf[i] & 0xF));
}
return result;
}
15 changes: 15 additions & 0 deletions user/tests/unit/service_bytes2hex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "catch.hpp"

#include "bytes2hexbuf.h"
#include <cstring>

TEST_CASE("bytes2hex_lower_") {
SECTION("bytes are converted to lowercase hex") {
uint8_t bytes[] = { 0x01, 0xFF, 0xA0, 0xb9 };
char out[4*2+1];
char* result = bytes2hexbuf_lower_case(bytes, 4, out);
REQUIRE(out[8]==0);
REQUIRE(!strcmp(out, "01ffa0b9"));
REQUIRE(result==out);
}
}