From 414c8227bc78547d288ea047c537121fc6d1da6c Mon Sep 17 00:00:00 2001 From: Andy Holmes Date: Mon, 16 Dec 2024 16:24:16 -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 | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/libvalent/device/valent-device-plugin.c b/src/libvalent/device/valent-device-plugin.c index 3d93793511..fc1f06b7f6 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,20 @@ 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)); + if G_UNLIKELY ((priv->state & VALENT_DEVICE_STATE_CONNECTED) == 0) + { + g_warning ("Packet passed to disconnected device"); + return; + } + VALENT_DEVICE_PLUGIN_GET_CLASS (plugin)->handle_packet (plugin, type, packet); VALENT_EXIT; @@ -304,10 +317,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;