From b8245a550752ed3826902f63a75203c67282ace7 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Mon, 18 Apr 2022 11:51:40 +0100 Subject: [PATCH] Expose API for hardware unique ID (#16869) --- .../onekey/keymaps/hardware_id/keymap.c | 28 +++++++++++++++++++ platforms/arm_atsam/hardware_id.c | 9 ++++++ platforms/avr/hardware_id.c | 13 +++++++++ platforms/chibios/hardware_id.c | 15 ++++++++++ platforms/common.mk | 1 + platforms/hardware_id.h | 18 ++++++++++++ platforms/test/hardware_id.c | 9 ++++++ 7 files changed, 93 insertions(+) create mode 100644 keyboards/handwired/onekey/keymaps/hardware_id/keymap.c create mode 100644 platforms/arm_atsam/hardware_id.c create mode 100644 platforms/avr/hardware_id.c create mode 100644 platforms/chibios/hardware_id.c create mode 100644 platforms/hardware_id.h create mode 100644 platforms/test/hardware_id.c diff --git a/keyboards/handwired/onekey/keymaps/hardware_id/keymap.c b/keyboards/handwired/onekey/keymaps/hardware_id/keymap.c new file mode 100644 index 0000000000..bcec8b3ca1 --- /dev/null +++ b/keyboards/handwired/onekey/keymaps/hardware_id/keymap.c @@ -0,0 +1,28 @@ +#include QMK_KEYBOARD_H +#include "hardware_id.h" +#include +enum custom_keycodes { + DUMP_ID = SAFE_RANGE, +}; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { + LAYOUT_ortho_1x1(DUMP_ID) +}; + +bool process_record_user(uint16_t keycode, keyrecord_t *record) { + switch (keycode) { + case DUMP_ID: + if (record->event.pressed) { + hardware_id_t id = get_hardware_id(); + char buffer[100]; + sprintf(buffer, "ID:%lu:%lu:%lu:%lu\n", id.data[0], id.data[1], id.data[2], id.data[3]); +#ifdef CONSOLE_ENABLE + printf(buffer); +#else + send_string(buffer); +#endif + } + break; + } + return false; +}; diff --git a/platforms/arm_atsam/hardware_id.c b/platforms/arm_atsam/hardware_id.c new file mode 100644 index 0000000000..8b3b35a492 --- /dev/null +++ b/platforms/arm_atsam/hardware_id.c @@ -0,0 +1,9 @@ +// Copyright 2022 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "hardware_id.h" + +hardware_id_t get_hardware_id(void) { + hardware_id_t id = {0}; + return id; +} diff --git a/platforms/avr/hardware_id.c b/platforms/avr/hardware_id.c new file mode 100644 index 0000000000..073834c19c --- /dev/null +++ b/platforms/avr/hardware_id.c @@ -0,0 +1,13 @@ +// Copyright 2022 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include "hardware_id.h" + +hardware_id_t get_hardware_id(void) { + hardware_id_t id = {0}; + for (uint8_t i = 0; i < 10; i += 1) { + ((uint8_t*)&id)[i] = boot_signature_byte_get(i + 0x0E); + } + return id; +} diff --git a/platforms/chibios/hardware_id.c b/platforms/chibios/hardware_id.c new file mode 100644 index 0000000000..888a275465 --- /dev/null +++ b/platforms/chibios/hardware_id.c @@ -0,0 +1,15 @@ +// Copyright 2022 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include "hardware_id.h" + +hardware_id_t get_hardware_id(void) { + hardware_id_t id = {0}; +#ifdef UID_BASE + id.data[0] = (uint32_t)(*((uint32_t *)UID_BASE)); + id.data[1] = (uint32_t)(*((uint32_t *)(UID_BASE + 4))); + id.data[1] = (uint32_t)(*((uint32_t *)(UID_BASE + 8))); +#endif + return id; +} diff --git a/platforms/common.mk b/platforms/common.mk index 2a1fc8d377..693bdc8cf0 100644 --- a/platforms/common.mk +++ b/platforms/common.mk @@ -2,6 +2,7 @@ PLATFORM_COMMON_DIR = $(PLATFORM_PATH)/$(PLATFORM_KEY) TMK_COMMON_SRC += \ $(PLATFORM_PATH)/suspend.c \ + $(PLATFORM_COMMON_DIR)/hardware_id.c \ $(PLATFORM_COMMON_DIR)/platform.c \ $(PLATFORM_COMMON_DIR)/suspend.c \ $(PLATFORM_COMMON_DIR)/timer.c \ diff --git a/platforms/hardware_id.h b/platforms/hardware_id.h new file mode 100644 index 0000000000..0c161863d6 --- /dev/null +++ b/platforms/hardware_id.h @@ -0,0 +1,18 @@ +// Copyright 2022 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include + +/** \brief Storage for a hardware ID + * + * Ensure this is sized to cover all hardware scenarios + */ +typedef struct hardware_id_t { + uint32_t data[4]; +} hardware_id_t; + +/** \brief Query the devices "unique" ID + */ +hardware_id_t get_hardware_id(void); diff --git a/platforms/test/hardware_id.c b/platforms/test/hardware_id.c new file mode 100644 index 0000000000..8b3b35a492 --- /dev/null +++ b/platforms/test/hardware_id.c @@ -0,0 +1,9 @@ +// Copyright 2022 QMK +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "hardware_id.h" + +hardware_id_t get_hardware_id(void) { + hardware_id_t id = {0}; + return id; +}