|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Intel Corporation |
| 3 | + * Copyright (c) 2023 Meta |
| 4 | + * Copyright (c) 2025 Tenstorrent AI ULC |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +#include <errno.h> |
| 10 | +#include <string.h> |
| 11 | +#include <sys/time.h> |
| 12 | +#include <time.h> |
| 13 | + |
| 14 | +#include <zephyr/drivers/hwinfo.h> |
| 15 | +#include <zephyr/logging/log.h> |
| 16 | +#include <zephyr/sys_clock.h> |
| 17 | +#include <zephyr/toolchain.h> |
| 18 | + |
| 19 | +LOG_MODULE_REGISTER(xsi_single_process, CONFIG_XSI_SINGLE_PROCESS_LOG_LEVEL); |
| 20 | + |
| 21 | +extern int z_setenv(const char *name, const char *val, int overwrite); |
| 22 | +extern int z_clock_gettime(clockid_t clockid, struct timespec *tp); |
| 23 | + |
| 24 | +long gethostid(void) |
| 25 | +{ |
| 26 | + int rc; |
| 27 | + uint32_t buf = 0; |
| 28 | + |
| 29 | + rc = hwinfo_get_device_id((uint8_t *)&buf, sizeof(buf)); |
| 30 | + if ((rc < 0) || (rc != sizeof(buf)) || (buf == 0)) { |
| 31 | + LOG_DBG("%s() failed: %d", "hwinfo_get_device_id", rc); |
| 32 | + return (long)rc; |
| 33 | + } |
| 34 | + |
| 35 | + return (long)buf; |
| 36 | +} |
| 37 | + |
| 38 | +int gettimeofday(struct timeval *tv, void *tz) |
| 39 | +{ |
| 40 | + struct timespec ts; |
| 41 | + int res; |
| 42 | + |
| 43 | + /* As per POSIX, "if tzp is not a null pointer, the behavior |
| 44 | + * is unspecified." "tzp" is the "tz" parameter above. */ |
| 45 | + ARG_UNUSED(tz); |
| 46 | + |
| 47 | + res = z_clock_gettime(CLOCK_REALTIME, &ts); |
| 48 | + if (res < 0) { |
| 49 | + LOG_DBG("%s() failed: %d", "clock_gettime", res); |
| 50 | + return res; |
| 51 | + } |
| 52 | + |
| 53 | + tv->tv_sec = ts.tv_sec; |
| 54 | + tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC; |
| 55 | + |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
| 59 | +int putenv(char *string) |
| 60 | +{ |
| 61 | + if (string != NULL) { |
| 62 | + char *const name = string; |
| 63 | + |
| 64 | + for (char *val = name; *val != '\0'; ++val) { |
| 65 | + if (*val == '=') { |
| 66 | + int rc; |
| 67 | + |
| 68 | + *val = '\0'; |
| 69 | + ++val; |
| 70 | + rc = z_setenv(name, val, 1); |
| 71 | + --val; |
| 72 | + *val = '='; |
| 73 | + |
| 74 | + if (rc < 0) { |
| 75 | + LOG_DBG("%s() failed: %d", "setenv", rc); |
| 76 | + return rc; |
| 77 | + } |
| 78 | + |
| 79 | + return 0; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /* was unable to find '=' in string */ |
| 85 | + errno = EINVAL; |
| 86 | + return -1; |
| 87 | +} |
0 commit comments