xiaoxiang781216 commented on code in PR #20108: URL: https://github.com/apache/nuttx/pull/20108#discussion_r3986009580
########## arch/sim/src/Makefile: ########## @@ -134,6 +134,40 @@ endif HOSTSRCS = sim_hostirq.c sim_hostmemory.c sim_hostmisc.c sim_hosttime.c sim_hostuart.c HOSTSRCS += sim_hostfs.c sim_errno.c +ifeq ($(CONFIG_SIM_BSIM_TIME),y) + HOSTSRCS += sim_bsimtime.c + BSIM_SKIP_PATH_CHECK_GOALS := clean distclean clean_context context + BSIM_CHECK_PATHS := + ifeq ($(MAKECMDGOALS),) + BSIM_CHECK_PATHS := y + else ifneq ($(filter-out $(BSIM_SKIP_PATH_CHECK_GOALS),$(MAKECMDGOALS)),) + BSIM_CHECK_PATHS := y + endif + ifeq ($(BSIM_CHECK_PATHS),y) Review Comment: why not always check BSIM_COMPONENTS_PATH ########## arch/sim/src/sim/posix/sim_bsimtime.c: ########## @@ -0,0 +1,130 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_bsimtime.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> + +#include "bs_pc_base.h" +#include "sim_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define NSEC_PER_BSIM_USEC 1000ull + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static pb_dev_state_t g_bsim_dev; +static uint64_t g_now_nsec; +static bool g_connected; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void bsimtime_disconnect_atexit(void) +{ + host_bsimtime_disconnect(); +} + +static void bsimtime_fail(const char *what) +{ + (void)what; + host_abort(1); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int host_bsimtime_init(const char *sim_id, const char *phy_id, + unsigned int dev_nbr) +{ + if (sim_id == NULL || phy_id == NULL) + { + errno = EINVAL; + return -1; + } + + if (pb_dev_init_com(&g_bsim_dev, dev_nbr, sim_id, phy_id) != 0) + { + return -1; Review Comment: could we return from pb_dev_init_com ########## arch/sim/src/sim/posix/sim_bsimtime.c: ########## @@ -0,0 +1,130 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_bsimtime.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> + +#include "bs_pc_base.h" +#include "sim_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define NSEC_PER_BSIM_USEC 1000ull + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static pb_dev_state_t g_bsim_dev; +static uint64_t g_now_nsec; +static bool g_connected; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void bsimtime_disconnect_atexit(void) +{ + host_bsimtime_disconnect(); +} + +static void bsimtime_fail(const char *what) +{ + (void)what; + host_abort(1); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int host_bsimtime_init(const char *sim_id, const char *phy_id, + unsigned int dev_nbr) +{ + if (sim_id == NULL || phy_id == NULL) + { + errno = EINVAL; + return -1; + } + + if (pb_dev_init_com(&g_bsim_dev, dev_nbr, sim_id, phy_id) != 0) + { + return -1; + } + + g_connected = true; + atexit(bsimtime_disconnect_atexit); + + return 0; +} + +uint64_t host_bsimtime_gettime(void) +{ + return g_now_nsec; +} + +bool host_bsimtime_is_enabled(void) +{ + return g_connected; +} + +void host_bsimtime_sleepuntil(uint64_t nsec) +{ + pb_wait_t wait; + + if (nsec <= g_now_nsec) + { + return; + } + + if (!g_connected) + { + errno = ENOTCONN; Review Comment: why need ########## arch/sim/src/sim/posix/sim_bsimtime.c: ########## @@ -0,0 +1,130 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_bsimtime.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> + +#include "bs_pc_base.h" +#include "sim_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define NSEC_PER_BSIM_USEC 1000ull + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static pb_dev_state_t g_bsim_dev; +static uint64_t g_now_nsec; +static bool g_connected; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void bsimtime_disconnect_atexit(void) +{ + host_bsimtime_disconnect(); +} + +static void bsimtime_fail(const char *what) +{ + (void)what; + host_abort(1); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int host_bsimtime_init(const char *sim_id, const char *phy_id, + unsigned int dev_nbr) +{ + if (sim_id == NULL || phy_id == NULL) + { + errno = EINVAL; Review Comment: let's return -errcode directly ########## arch/sim/src/sim/posix/sim_hosthcisocket.c: ########## @@ -60,10 +64,139 @@ struct sockaddr_hci unsigned short hci_channel; }; +enum bthcisock_target_e +{ + BTHCISOCK_TARGET_DEFAULT = 0, + BTHCISOCK_TARGET_BLUEZ, + BTHCISOCK_TARGET_UNIX +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static enum bthcisock_target_e g_bthcisock_target; +static int g_bthcisock_devid; +static char g_bthcisock_path[sizeof(((struct sockaddr_un *)0)->sun_path)]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int host_bthcisock_parse_devid(const char *target, int *devid) +{ + unsigned long value; + char *endptr; + + if (strncmp(target, "hci", 3) != 0 || target[3] == '\0') + { + return -EINVAL; + } + + target += 3; + if (target[0] == '\0') + { + return -EINVAL; + } + + errno = 0; + value = strtoul(target, &endptr, 10); + if (errno != 0 || endptr == target || endptr[0] != '\0' || + value > USHRT_MAX) + { + return -EINVAL; + } + + *devid = (int)value; + return 0; +} + +static int host_bthcisock_open_unix(const char *path) +{ + struct sockaddr_un addr; + size_t len; + int ret; + int fd; + + len = strlen(path); + if (len == 0) + { + return -EINVAL; + } + + if (len >= sizeof(addr.sun_path)) + { + return -ENAMETOOLONG; + } + + fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); + if (fd < 0) + { + return fd; + } + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + memcpy(addr.sun_path, path, len + 1); + + ret = connect(fd, (struct sockaddr *)&addr, + offsetof(struct sockaddr_un, sun_path) + len + 1); + if (ret < 0) + { + close(fd); + return ret; + } + + return fd; +} + /**************************************************************************** * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: host_bthcisock_configure + * + * Description: + * Override the default HCI target. Accepted values: + * - "hci<n>" for BlueZ HCI user channel + * - "/path/to.sock" for Unix-domain HCI socket + * + ****************************************************************************/ + +int host_bthcisock_configure(const char *target) +{ + size_t len; + int devid; + + if (target == NULL || target[0] == '\0') + { + return -EINVAL; + } + + if (target[0] == '/') + { + len = strlen(target); + if (len >= sizeof(g_bthcisock_path)) + { + return -ENAMETOOLONG; + } + + memcpy(g_bthcisock_path, target, len + 1); + g_bthcisock_target = BTHCISOCK_TARGET_UNIX; + return 0; + } + + if (host_bthcisock_parse_devid(target, &devid) < 0) Review Comment: return the error value from host_bthcisock_parse_devid ########## arch/sim/src/sim/posix/sim_hosthcisocket.c: ########## @@ -189,8 +322,20 @@ int host_bthcisock_open(int dev_idx) { int err; struct sockaddr_hci addr; - int fd = socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, - BTPROTO_HCI); + int fd; + + if (g_bthcisock_target == BTHCISOCK_TARGET_UNIX) + { + return host_bthcisock_open_unix(g_bthcisock_path); + } + + if (g_bthcisock_target == BTHCISOCK_TARGET_BLUEZ) + { + dev_idx = g_bthcisock_devid; + } + + fd = socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, + BTPROTO_HCI); if (fd < 0) { return fd; Review Comment: -errno ########## arch/sim/src/sim/posix/sim_hosthcisocket.c: ########## @@ -60,10 +64,139 @@ struct sockaddr_hci unsigned short hci_channel; }; +enum bthcisock_target_e +{ + BTHCISOCK_TARGET_DEFAULT = 0, + BTHCISOCK_TARGET_BLUEZ, + BTHCISOCK_TARGET_UNIX +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static enum bthcisock_target_e g_bthcisock_target; +static int g_bthcisock_devid; +static char g_bthcisock_path[sizeof(((struct sockaddr_un *)0)->sun_path)]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int host_bthcisock_parse_devid(const char *target, int *devid) +{ + unsigned long value; + char *endptr; + + if (strncmp(target, "hci", 3) != 0 || target[3] == '\0') + { + return -EINVAL; + } + + target += 3; + if (target[0] == '\0') + { + return -EINVAL; + } + + errno = 0; + value = strtoul(target, &endptr, 10); + if (errno != 0 || endptr == target || endptr[0] != '\0' || + value > USHRT_MAX) + { + return -EINVAL; + } + + *devid = (int)value; + return 0; +} + +static int host_bthcisock_open_unix(const char *path) +{ + struct sockaddr_un addr; + size_t len; + int ret; + int fd; + + len = strlen(path); + if (len == 0) + { + return -EINVAL; + } + + if (len >= sizeof(addr.sun_path)) + { + return -ENAMETOOLONG; + } + + fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); + if (fd < 0) + { + return fd; + } + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + memcpy(addr.sun_path, path, len + 1); + + ret = connect(fd, (struct sockaddr *)&addr, + offsetof(struct sockaddr_un, sun_path) + len + 1); + if (ret < 0) + { + close(fd); + return ret; Review Comment: -errno ########## arch/sim/src/sim/posix/sim_hosthcisocket.c: ########## @@ -60,10 +64,139 @@ struct sockaddr_hci unsigned short hci_channel; }; +enum bthcisock_target_e +{ + BTHCISOCK_TARGET_DEFAULT = 0, + BTHCISOCK_TARGET_BLUEZ, + BTHCISOCK_TARGET_UNIX +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static enum bthcisock_target_e g_bthcisock_target; +static int g_bthcisock_devid; +static char g_bthcisock_path[sizeof(((struct sockaddr_un *)0)->sun_path)]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int host_bthcisock_parse_devid(const char *target, int *devid) +{ + unsigned long value; + char *endptr; + + if (strncmp(target, "hci", 3) != 0 || target[3] == '\0') + { + return -EINVAL; + } + + target += 3; + if (target[0] == '\0') Review Comment: dup with line 91 ########## arch/sim/src/sim/posix/sim_bsimtime.c: ########## @@ -0,0 +1,130 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_bsimtime.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> + +#include "bs_pc_base.h" +#include "sim_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define NSEC_PER_BSIM_USEC 1000ull + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static pb_dev_state_t g_bsim_dev; +static uint64_t g_now_nsec; +static bool g_connected; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void bsimtime_disconnect_atexit(void) +{ + host_bsimtime_disconnect(); +} + +static void bsimtime_fail(const char *what) +{ + (void)what; Review Comment: remove what, or use it ########## arch/sim/src/sim/posix/sim_hosthcisocket.c: ########## @@ -60,10 +64,139 @@ struct sockaddr_hci unsigned short hci_channel; }; +enum bthcisock_target_e +{ + BTHCISOCK_TARGET_DEFAULT = 0, + BTHCISOCK_TARGET_BLUEZ, + BTHCISOCK_TARGET_UNIX +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static enum bthcisock_target_e g_bthcisock_target; +static int g_bthcisock_devid; +static char g_bthcisock_path[sizeof(((struct sockaddr_un *)0)->sun_path)]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int host_bthcisock_parse_devid(const char *target, int *devid) +{ + unsigned long value; + char *endptr; + + if (strncmp(target, "hci", 3) != 0 || target[3] == '\0') + { + return -EINVAL; + } + + target += 3; + if (target[0] == '\0') + { + return -EINVAL; + } + + errno = 0; + value = strtoul(target, &endptr, 10); Review Comment: *devid = atoi(target) ########## arch/sim/src/sim/posix/sim_hosthcisocket.c: ########## Review Comment: -errno ########## arch/sim/src/sim/posix/sim_hosthcisocket.c: ########## Review Comment: -errno ########## arch/sim/src/sim/posix/sim_hosthcisocket.c: ########## @@ -60,10 +64,139 @@ struct sockaddr_hci unsigned short hci_channel; }; +enum bthcisock_target_e +{ + BTHCISOCK_TARGET_DEFAULT = 0, + BTHCISOCK_TARGET_BLUEZ, + BTHCISOCK_TARGET_UNIX +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static enum bthcisock_target_e g_bthcisock_target; +static int g_bthcisock_devid; +static char g_bthcisock_path[sizeof(((struct sockaddr_un *)0)->sun_path)]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int host_bthcisock_parse_devid(const char *target, int *devid) +{ + unsigned long value; + char *endptr; + + if (strncmp(target, "hci", 3) != 0 || target[3] == '\0') + { + return -EINVAL; + } + + target += 3; + if (target[0] == '\0') + { + return -EINVAL; + } + + errno = 0; + value = strtoul(target, &endptr, 10); + if (errno != 0 || endptr == target || endptr[0] != '\0' || + value > USHRT_MAX) + { + return -EINVAL; + } + + *devid = (int)value; + return 0; +} + +static int host_bthcisock_open_unix(const char *path) +{ + struct sockaddr_un addr; + size_t len; + int ret; + int fd; + + len = strlen(path); + if (len == 0) + { + return -EINVAL; + } + + if (len >= sizeof(addr.sun_path)) + { + return -ENAMETOOLONG; + } + + fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); + if (fd < 0) + { + return fd; Review Comment: -errno ########## arch/sim/src/sim/posix/sim_hosthcisocket.c: ########## Review Comment: close(fd) < 0 ? -errno : 0 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
