Skip to content

Commit

Permalink
refactor(device): ignore post-state packets
Browse files Browse the repository at this point in the history
Ignore and warn about packets received after a device has been
disconnected.
  • Loading branch information
andyholmes committed Dec 17, 2024
1 parent 0f1a5f8 commit 414c822
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/libvalent/device/valent-device-plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 414c822

Please sign in to comment.