From d0cc7e4082b1d7618925d840c9f9c8f73a3316c2 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Sat, 28 May 2022 13:10:16 +0100 Subject: [PATCH] d/remote: Use xrt_system_devices directly in the driver And also make shutdown properly work. --- src/xrt/drivers/remote/r_device.c | 4 +- src/xrt/drivers/remote/r_hmd.c | 4 +- src/xrt/drivers/remote/r_hub.c | 130 +++++++++++++++--- src/xrt/drivers/remote/r_interface.h | 13 +- src/xrt/drivers/remote/r_internal.h | 9 +- .../targets/common/target_builder_remote.c | 31 +---- 6 files changed, 126 insertions(+), 65 deletions(-) diff --git a/src/xrt/drivers/remote/r_device.c b/src/xrt/drivers/remote/r_device.c index dc1a87c82..f8e17d1f3 100644 --- a/src/xrt/drivers/remote/r_device.c +++ b/src/xrt/drivers/remote/r_device.c @@ -1,4 +1,4 @@ -// Copyright 2020, Collabora, Ltd. +// Copyright 2020-2022, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file @@ -195,7 +195,7 @@ r_device_create(struct r_hub *r, bool is_left) rd->base.get_view_poses = r_device_get_view_poses; rd->base.set_output = r_device_set_output; rd->base.destroy = r_device_destroy; - rd->base.tracking_origin = &r->base; + rd->base.tracking_origin = &r->origin; rd->base.orientation_tracking_supported = true; rd->base.position_tracking_supported = true; rd->base.hand_tracking_supported = true; diff --git a/src/xrt/drivers/remote/r_hmd.c b/src/xrt/drivers/remote/r_hmd.c index 4c8bc1fd9..55d93b9bc 100644 --- a/src/xrt/drivers/remote/r_hmd.c +++ b/src/xrt/drivers/remote/r_hmd.c @@ -1,4 +1,4 @@ -// Copyright 2020, Collabora, Ltd. +// Copyright 2020-2022, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file @@ -123,7 +123,7 @@ r_hmd_create(struct r_hub *r) rh->base.get_view_poses = r_hmd_get_view_poses; rh->base.set_output = r_hmd_set_output; rh->base.destroy = r_hmd_destroy; - rh->base.tracking_origin = &r->base; + rh->base.tracking_origin = &r->origin; rh->base.orientation_tracking_supported = true; rh->base.position_tracking_supported = true; rh->base.hand_tracking_supported = false; diff --git a/src/xrt/drivers/remote/r_hub.c b/src/xrt/drivers/remote/r_hub.c index dff1d4f24..1965c0c4f 100644 --- a/src/xrt/drivers/remote/r_hub.c +++ b/src/xrt/drivers/remote/r_hub.c @@ -1,4 +1,4 @@ -// Copyright 2020, Collabora, Ltd. +// Copyright 2020-2022, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file @@ -85,9 +85,28 @@ int do_accept(struct r_hub *r) { struct sockaddr_in addr = {0}; + struct timeval timeout = {.tv_sec = 1}; + fd_set set; int ret; int conn_fd; + // Shutting down. + if (r->accept_fd < 0) { + return -1; + } + + do { + FD_ZERO(&set); + FD_SET(r->accept_fd, &set); + + ret = select(r->accept_fd + 1, &set, NULL, NULL, &timeout); + } while (ret == 0); + + if (ret < 0) { + U_LOG_E("select failed: %i", ret); + return ret; + } + socklen_t addr_length = (socklen_t)sizeof(addr); ret = accept(r->accept_fd, (struct sockaddr *)&addr, &addr_length); if (ret < 0) { @@ -120,13 +139,18 @@ run_thread(void *ptr) ret = setup_accept_fd(r); if (ret < 0) { + U_LOG_I("Leaving thread"); return NULL; } - while (true) { + while (r->accept_fd >= 0) { U_LOG_I("Listening on port '%i'.", r->port); ret = do_accept(r); + if (ret < 0) { + U_LOG_I("Leaving thread"); + return NULL; + } r_remote_connection_write_one(&r->rc, &r->reset); r_remote_connection_write_one(&r->rc, &r->latest); @@ -143,33 +167,64 @@ run_thread(void *ptr) } } + U_LOG_I("Leaving thread"); return NULL; } void -r_hub_destroy(struct r_hub *r) +r_hub_destroy(struct xrt_system_devices *xsysd) { + struct r_hub *r = (struct r_hub *)xsysd; + + /* + * Destroy all of the devices first. + */ + + for (uint32_t i = 0; i < ARRAY_SIZE(r->base.xdevs); i++) { + xrt_device_destroy(&r->base.xdevs[i]); + } + + + /* + * Harshly pull the plug on the sockets to wakeup the thread. + */ + + if (r->accept_fd >= 0) { + close(r->accept_fd); + r->accept_fd = -1; + } + + if (r->rc.fd >= 0) { + close(r->rc.fd); + r->rc.fd = -1; + } + + + /* + * Should be safe to stop the thread now. + */ + + os_thread_helper_stop_and_wait(&r->oth); + free(r); } -/*! - * +/* * + * 'Exported' create function. * */ int -r_create_devices(uint16_t port, - struct xrt_device **out_hmd, - struct xrt_device **out_controller_left, - struct xrt_device **out_controller_right) +r_create_devices(uint16_t port, struct xrt_system_devices **out_xsysd) { struct r_hub *r = U_TYPED_CALLOC(struct r_hub); int ret; - r->base.type = XRT_TRACKING_TYPE_RGB; - r->base.offset.orientation.w = 1.0f; // All other members are zero. + r->base.destroy = r_hub_destroy; + r->origin.type = XRT_TRACKING_TYPE_RGB; + r->origin.offset.orientation.w = 1.0f; // All other members are zero. r->reset.hmd.pose.position.y = 1.6f; r->reset.hmd.pose.orientation.w = 1.0f; r->reset.left.active = true; @@ -193,27 +248,46 @@ r_create_devices(uint16_t port, r->accept_fd = -1; r->rc.fd = -1; - snprintf(r->base.name, sizeof(r->base.name), "Remote Simulator"); + snprintf(r->origin.name, sizeof(r->origin.name), "Remote Simulator"); ret = os_thread_helper_init(&r->oth); if (ret != 0) { U_LOG_E("Failed to init threading!"); - r_hub_destroy(r); - return ret; + r_hub_destroy(&r->base); + return XRT_ERROR_ALLOCATION; } ret = os_thread_helper_start(&r->oth, run_thread, r); if (ret != 0) { U_LOG_E("Failed to start thread!"); - r_hub_destroy(r); - return ret; + r_hub_destroy(&r->base); + return XRT_ERROR_ALLOCATION; } - *out_hmd = r_hmd_create(r); - *out_controller_left = r_device_create(r, true); - *out_controller_right = r_device_create(r, false); - // Setup variable tracker. + /* + * Setup system devices. + */ + + struct xrt_device *head = r_hmd_create(r); + struct xrt_device *left = r_device_create(r, true); + struct xrt_device *right = r_device_create(r, false); + + r->base.xdevs[r->base.xdev_count++] = head; + r->base.xdevs[r->base.xdev_count++] = left; + r->base.xdevs[r->base.xdev_count++] = right; + + r->base.roles.head = head; + r->base.roles.left = left; + r->base.roles.right = right; + r->base.roles.hand_tracking.left = left; + r->base.roles.hand_tracking.right = right; + + + /* + * Setup variable tracker. + */ + u_var_add_root(r, "Remote Hub", true); // u_var_add_gui_header(r, &r->gui.hmd, "MHD"); u_var_add_pose(r, &r->latest.hmd.pose, "hmd.pose"); @@ -228,9 +302,23 @@ r_create_devices(uint16_t port, u_var_add_bool(r, &r->latest.right.menu, "right.menu"); u_var_add_pose(r, &r->latest.right.pose, "right.pose"); - return 0; + + /* + * Done now. + */ + + *out_xsysd = &r->base; + + return XRT_SUCCESS; } + +/* + * + * 'Exported' connection functions. + * + */ + int r_remote_connection_init(struct r_remote_connection *rc, const char *ip_addr, uint16_t port) { diff --git a/src/xrt/drivers/remote/r_interface.h b/src/xrt/drivers/remote/r_interface.h index a5978ac96..23e7b326a 100644 --- a/src/xrt/drivers/remote/r_interface.h +++ b/src/xrt/drivers/remote/r_interface.h @@ -1,4 +1,4 @@ -// Copyright 2020, Collabora, Ltd. +// Copyright 2020-2022, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file @@ -16,7 +16,7 @@ extern "C" { #endif -struct xrt_device; +struct xrt_system_devices; /*! * @defgroup drv_remote Remote debugging driver @@ -85,15 +85,12 @@ struct r_remote_connection }; /*! - * Creates the remote devices. + * Creates the remote system devices. * * @ingroup drv_remote */ -int -r_create_devices(uint16_t port, - struct xrt_device **out_hmd, - struct xrt_device **out_controller_left, - struct xrt_device **out_controller_right); +xrt_result_t +r_create_devices(uint16_t port, struct xrt_system_devices **out_xsysd); /*! * Initializes and connects the connection. diff --git a/src/xrt/drivers/remote/r_internal.h b/src/xrt/drivers/remote/r_internal.h index e29f60997..031fc7090 100644 --- a/src/xrt/drivers/remote/r_internal.h +++ b/src/xrt/drivers/remote/r_internal.h @@ -1,4 +1,4 @@ -// Copyright 2020, Collabora, Ltd. +// Copyright 2020-2022, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file @@ -10,6 +10,7 @@ #pragma once #include "xrt/xrt_device.h" +#include "xrt/xrt_system.h" #include "xrt/xrt_tracking.h" #include "os/os_threading.h" @@ -30,7 +31,11 @@ extern "C" { */ struct r_hub { - struct xrt_tracking_origin base; + // System devices wrapper. + struct xrt_system_devices base; + + //! Origin for all locations. + struct xrt_tracking_origin origin; //! Connection to the controller. struct r_remote_connection rc; diff --git a/src/xrt/targets/common/target_builder_remote.c b/src/xrt/targets/common/target_builder_remote.c index 45b6049b3..707f3c6fe 100644 --- a/src/xrt/targets/common/target_builder_remote.c +++ b/src/xrt/targets/common/target_builder_remote.c @@ -73,8 +73,6 @@ remote_estimate_system(struct xrt_builder *xb, static xrt_result_t remote_open_system(struct xrt_builder *xb, cJSON *config, struct xrt_prober *xp, struct xrt_system_devices **out_xsysd) { - struct u_system_devices *usysd = u_system_devices_allocate(); - assert(out_xsysd != NULL); assert(*out_xsysd == NULL); @@ -84,34 +82,7 @@ remote_open_system(struct xrt_builder *xb, cJSON *config, struct xrt_prober *xp, port = 4242; } - struct xrt_device *head = NULL, *left = NULL, *right = NULL; - - r_create_devices(port, &head, &left, &right); - - if (head == NULL) { - u_system_devices_destroy(&usysd); - xrt_device_destroy(&left); - xrt_device_destroy(&right); - return XRT_ERROR_ALLOCATION; - } - - usysd->base.xdevs[usysd->base.xdev_count++] = head; - if (left != NULL) { - usysd->base.xdevs[usysd->base.xdev_count++] = left; - } - if (right != NULL) { - usysd->base.xdevs[usysd->base.xdev_count++] = right; - } - - usysd->base.roles.head = head; - usysd->base.roles.left = left; - usysd->base.roles.right = right; - usysd->base.roles.hand_tracking.left = left; - usysd->base.roles.hand_tracking.right = right; - - *out_xsysd = &usysd->base; - - return XRT_SUCCESS; + return r_create_devices(port, out_xsysd); } static void