Skip to content

Commit 8bdfd03

Browse files
committed
Add hwinfo based device serial
1 parent e7483e1 commit 8bdfd03

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

prj.conf

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
2626

2727
CONFIG_ASSERT=y
2828
CONFIG_COMPILER_WARNINGS_AS_ERRORS=y
29+
30+
CONFIG_HWINFO=y
31+
32+
# Required for using pyOCD to flash the application
33+
CONFIG_BUILD_OUTPUT_HEX=y

src/main.c

+25-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include MEMFAULT_ZEPHYR_INCLUDE(kernel.h)
1515
#include MEMFAULT_ZEPHYR_INCLUDE(logging/log.h)
1616
#include MEMFAULT_ZEPHYR_INCLUDE(shell/shell.h)
17+
#include MEMFAULT_ZEPHYR_INCLUDE(drivers/hwinfo.h)
1718
#include <zephyr/net/net_if.h>
1819
#include <zephyr/net/dhcpv4.h>
1920

@@ -73,9 +74,32 @@ static void blink_forever(void) {
7374
#endif // CONFIG_QEMU_TARGET
7475
}
7576

77+
static const char *prv_get_device_id(void) {
78+
uint8_t dev_id[16] = { 0 };
79+
static char dev_id_str[sizeof(dev_id) * 2 + 1];
80+
static const char *dev_str = "UNKNOWN";
81+
82+
// Obtain the device id
83+
ssize_t length = hwinfo_get_device_id(dev_id, sizeof(dev_id));
84+
85+
// If this fails for some reason, use a fixed ID instead
86+
if (length <= 0) {
87+
dev_str = CONFIG_SOC_SERIES "-test";
88+
length = strlen(dev_str);
89+
} else {
90+
// Render the obtained serial number in hexadecimal representation
91+
for (size_t i = 0; i < length; i++) {
92+
(void)snprintf(&dev_id_str[i * 2], sizeof(dev_id_str), "%02x", dev_id[i]);
93+
}
94+
dev_str = dev_id_str;
95+
}
96+
97+
return dev_str;
98+
}
99+
76100
void memfault_platform_get_device_info(sMemfaultDeviceInfo *info) {
77101
*info = (sMemfaultDeviceInfo){
78-
.device_serial = "DEMOSERIAL",
102+
.device_serial = prv_get_device_id(),
79103
.software_type = "zephyr-app",
80104
.software_version =
81105
CONFIG_ZEPHYR_MEMFAULT_EXAMPLE_SOFTWARE_VERSION "+" ZEPHYR_MEMFAULT_EXAMPLE_GIT_SHA1,
@@ -249,7 +273,6 @@ static void initialize_net(void) {
249273
#endif
250274
addr_str, sizeof(addr_str)));
251275

252-
memfault_zephyr_port_install_root_certs();
253276
#endif // CONFIG_NETWORKING
254277
}
255278

0 commit comments

Comments
 (0)