From 876e4044ab30fe122398a7b37755fc0dc6eeadde Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Tue, 21 Feb 2023 08:47:19 +1000 Subject: [PATCH] eis: error out if we're in the wrong state to start/stop emulating --- src/libeis-device.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/libeis-device.c b/src/libeis-device.c index e07441b..18a5927 100644 --- a/src/libeis-device.c +++ b/src/libeis-device.c @@ -223,44 +223,55 @@ client_msg_release(struct eis_device *device) static struct brei_result * client_msg_start_emulating(struct eis_device *device, uint32_t sequence) { + struct brei_result *result = NULL; + DISCONNECT_IF_RECEIVER_CONTEXT(device); switch (device->state) { case EIS_DEVICE_STATE_DEAD: case EIS_DEVICE_STATE_CLOSED_BY_CLIENT: case EIS_DEVICE_STATE_NEW: - case EIS_DEVICE_STATE_PAUSED: + case EIS_DEVICE_STATE_EMULATING: + result = brei_result_new(EIS_CONNECTION_DISCONNECT_REASON_PROTOCOL, + "Invalid device state %ud for a start_emulating event", device->state); break; case EIS_DEVICE_STATE_RESUMED: eis_queue_device_start_emulating_event(device, sequence); device->state = EIS_DEVICE_STATE_EMULATING; break; - case EIS_DEVICE_STATE_EMULATING: + case EIS_DEVICE_STATE_PAUSED: + /* race condition, that's fine */ break; } - return 0; + return result; } static struct brei_result * client_msg_stop_emulating(struct eis_device *device) { + struct brei_result *result = NULL; + DISCONNECT_IF_RECEIVER_CONTEXT(device); switch (device->state) { case EIS_DEVICE_STATE_DEAD: case EIS_DEVICE_STATE_CLOSED_BY_CLIENT: case EIS_DEVICE_STATE_NEW: - case EIS_DEVICE_STATE_PAUSED: case EIS_DEVICE_STATE_RESUMED: + result = brei_result_new(EIS_CONNECTION_DISCONNECT_REASON_PROTOCOL, + "Invalid device state %ud for a stop_emulating event", device->state); break; case EIS_DEVICE_STATE_EMULATING: eis_queue_device_stop_emulating_event(device); device->state = EIS_DEVICE_STATE_RESUMED; break; + case EIS_DEVICE_STATE_PAUSED: + /* race condition, that's fine */ + break; } - return 0; + return result; } static struct brei_result *