diff --git a/platforms/common/board_identity.c b/platforms/common/board_identity.c index 483717b599d..6cde93109a8 100644 --- a/platforms/common/board_identity.c +++ b/platforms/common/board_identity.c @@ -103,4 +103,29 @@ int board_get_px4_guid_formated(char *format_buffer, int size) size -= offset; return board_get_mfguid_formated(&format_buffer[offset], size); } + +/* The bytes of each 32-bit word with the most significant first, as board_get_mfguid_formated() prints them */ +int board_get_mfguid(mfguid_t mfgid) +{ + uuid_uint32_t uuid; + board_get_uuid32(uuid); + + for (unsigned int i = 0; i < PX4_CPU_UUID_WORD32_LENGTH; i++) { + mfgid[4 * i] = (uuid[i] >> 24) & 0xff; + mfgid[4 * i + 1] = (uuid[i] >> 16) & 0xff; + mfgid[4 * i + 2] = (uuid[i] >> 8) & 0xff; + mfgid[4 * i + 3] = uuid[i] & 0xff; + } + + return PX4_CPU_MFGUID_BYTE_LENGTH; +} + +int board_get_px4_guid(px4_guid_t px4_guid) +{ + px4_guid[0] = (soc_arch_id >> 8) & 0xff; + px4_guid[1] = soc_arch_id & 0xff; + memset(&px4_guid[2], 0, PX4_GUID_BYTE_LENGTH - 2 - PX4_CPU_MFGUID_BYTE_LENGTH); + board_get_mfguid(&px4_guid[PX4_GUID_BYTE_LENGTH - PX4_CPU_MFGUID_BYTE_LENGTH]); + return PX4_GUID_BYTE_LENGTH; +} #endif diff --git a/platforms/common/board_identity_test.cpp b/platforms/common/board_identity_test.cpp index a028e9c29fd..4122cb74267 100644 --- a/platforms/common/board_identity_test.cpp +++ b/platforms/common/board_identity_test.cpp @@ -160,3 +160,27 @@ TEST(BoardIdentityTest, PX4GUIDBufferOverflow) board_get_px4_guid_formated(buf_40, sizeof(buf_40)); EXPECT_EQ(0, strncmp(groundtruth_px4_guid, buf_40, sizeof(buf_40) - 1)); } + +/** + * @brief Test that `board_get_px4_guid` and `board_get_mfguid` give the bytes of the formatted GUID + */ +TEST(BoardIdentityTest, PX4GUIDBytes) +{ + char formatted[PX4_GUID_FORMAT_SIZE]; + board_get_px4_guid_formated(formatted, sizeof(formatted)); + + px4_guid_t px4_guid; + EXPECT_EQ(PX4_GUID_BYTE_LENGTH, board_get_px4_guid(px4_guid)); + + char bytes[PX4_GUID_FORMAT_SIZE] {}; + + for (int i = 0; i < PX4_GUID_BYTE_LENGTH; i++) { + snprintf(&bytes[2 * i], sizeof(bytes) - 2 * i, "%02x", px4_guid[i]); + } + + EXPECT_STREQ(formatted, bytes); + + mfguid_t mfguid; + EXPECT_EQ(PX4_CPU_MFGUID_BYTE_LENGTH, board_get_mfguid(mfguid)); + EXPECT_EQ(0, memcmp(&px4_guid[PX4_GUID_BYTE_LENGTH - PX4_CPU_MFGUID_BYTE_LENGTH], mfguid, sizeof(mfguid))); +}