From 93f459a078592c80c75473a16eb34180274c5c53 Mon Sep 17 00:00:00 2001 From: Andy Holmes Date: Mon, 16 Dec 2024 16:37:10 -0800 Subject: [PATCH] refactor(device): ignore post-state packets Ignore and warn about packets received after a device has been disconnected. --- src/libvalent/device/valent-device-plugin.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/libvalent/device/valent-device-plugin.c b/src/libvalent/device/valent-device-plugin.c index 3d93793511..8d04714d15 100644 --- a/src/libvalent/device/valent-device-plugin.c +++ b/src/libvalent/device/valent-device-plugin.c @@ -78,7 +78,12 @@ * Since: 1.0 */ -G_DEFINE_ABSTRACT_TYPE (ValentDevicePlugin, valent_device_plugin, VALENT_TYPE_EXTENSION) +typedef struct +{ + ValentDeviceState state; +} ValentDevicePluginPrivate; + +G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ValentDevicePlugin, valent_device_plugin, VALENT_TYPE_EXTENSION) /* LCOV_EXCL_START */ static void @@ -273,12 +278,18 @@ valent_device_plugin_handle_packet (ValentDevicePlugin *plugin, const char *type, JsonNode *packet) { + ValentDevicePluginPrivate *priv = valent_device_plugin_get_instance_private (plugin); + VALENT_ENTRY; g_return_if_fail (VALENT_IS_DEVICE_PLUGIN (plugin)); g_return_if_fail (type != NULL && *type != '\0'); g_return_if_fail (VALENT_IS_PACKET (packet)); + // FIXME: due to a race-condition while swapping device channels, packets + // may be handled while a device is briefly in a disconnected state + g_return_if_fail ((priv->state & VALENT_DEVICE_STATE_CONNECTED) != 0); + VALENT_DEVICE_PLUGIN_GET_CLASS (plugin)->handle_packet (plugin, type, packet); VALENT_EXIT; @@ -304,10 +315,13 @@ void valent_device_plugin_update_state (ValentDevicePlugin *plugin, ValentDeviceState state) { + ValentDevicePluginPrivate *priv = valent_device_plugin_get_instance_private (plugin); + VALENT_ENTRY; g_return_if_fail (VALENT_IS_DEVICE_PLUGIN (plugin)); + priv->state = state; VALENT_DEVICE_PLUGIN_GET_CLASS (plugin)->update_state (plugin, state); VALENT_EXIT;