|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Henrik Brix Andersen <[email protected]> |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include <cmdline.h> |
| 7 | +#include <posix_native_task.h> |
| 8 | +#include <string.h> |
| 9 | +#include <zephyr/drivers/hwinfo.h> |
| 10 | +#include <zephyr/sys/byteorder.h> |
| 11 | + |
| 12 | +#include "hwinfo_native_bottom.h" |
| 13 | + |
| 14 | +static uint32_t native_hwinfo_device_id; |
| 15 | +static bool native_hwinfo_device_id_set; |
| 16 | + |
| 17 | +ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length) |
| 18 | +{ |
| 19 | + |
| 20 | + if (length > sizeof(native_hwinfo_device_id)) { |
| 21 | + length = sizeof(native_hwinfo_device_id); |
| 22 | + } |
| 23 | + |
| 24 | + sys_put_be(buffer, &native_hwinfo_device_id, length); |
| 25 | + |
| 26 | + return length; |
| 27 | +} |
| 28 | + |
| 29 | +static void native_hwinfo_gethostid(void) |
| 30 | +{ |
| 31 | + if (!native_hwinfo_device_id_set) { |
| 32 | + native_hwinfo_device_id = native_hwinfo_gethostid_bottom(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +static void native_hwinfo_device_id_was_set(char *argv, int offset) |
| 37 | +{ |
| 38 | + ARG_UNUSED(argv); |
| 39 | + ARG_UNUSED(offset); |
| 40 | + |
| 41 | + native_hwinfo_device_id_set = true; |
| 42 | +} |
| 43 | + |
| 44 | +static void native_hwinfo_add_options(void) |
| 45 | +{ |
| 46 | + static struct args_struct_t native_hwinfo_options[] = { |
| 47 | + { |
| 48 | + .option = "device_id", |
| 49 | + .name = "id", |
| 50 | + .type = 'u', |
| 51 | + .dest = (void *)&native_hwinfo_device_id, |
| 52 | + .call_when_found = native_hwinfo_device_id_was_set, |
| 53 | + .descript = "A 32-bit integer value to use as HWINFO device ID. " |
| 54 | + "If not set, the host gethostid() output will be used.", |
| 55 | + }, |
| 56 | + ARG_TABLE_ENDMARKER, |
| 57 | + }; |
| 58 | + |
| 59 | + native_add_command_line_opts(native_hwinfo_options); |
| 60 | +} |
| 61 | + |
| 62 | +NATIVE_TASK(native_hwinfo_add_options, PRE_BOOT_1, 10); |
| 63 | +NATIVE_TASK(native_hwinfo_gethostid, PRE_BOOT_2, 10); |
0 commit comments