Allow LED devices to emit STATUS_CHANGED events on a QMP chardev. QMP event examples:
{ "timestamp": { "seconds": 1591704274, "microseconds": 520850 }, "event": "LED_STATUS_CHANGED", "data": { "name": "Green LED #0", "status": "on" } } { "timestamp": { "seconds": 1591704275, "microseconds": 530912 }, "event": "LED_STATUS_CHANGED", "data": { "name": "Green LED #0", "status": "off" } } Signed-off-by: Philippe Mathieu-Daudé <f4...@amsat.org> --- Since v1: rate limit 4/sec (eblake) --- qapi/led.json | 47 +++++++++++++++++++++++++++++++++++++++++++ qapi/qapi-schema.json | 1 + include/hw/misc/led.h | 1 + hw/misc/led.c | 24 +++++++++++++++++++++- MAINTAINERS | 1 + qapi/Makefile.objs | 2 +- 6 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 qapi/led.json diff --git a/qapi/led.json b/qapi/led.json new file mode 100644 index 0000000000..b6cef8a5dd --- /dev/null +++ b/qapi/led.json @@ -0,0 +1,47 @@ +# -*- Mode: Python -*- +# + +## +# = LED device +## + +## +# @LedState: +# +# Status of a LED +# +# @on: device is emitting +# +# @off: device is off +# +# Since: 5.1 +## +{ 'enum': 'LedState', 'data': [ 'on', 'off' ] } + +## +# @LED_STATUS_CHANGED: +# +# Emitted when LED status changed +# +# @name: LED description +# +# @status: New status +# +# Since: 5.1 +# +# Example: +# +# <- {"timestamp": {"seconds": 1541579657, "microseconds": 986760}, +# "event": "LED_STATUS_CHANGED", +# "data": +# {"name": "Blue LED #3", +# "status": "on" +# } +# } +# +## +{ 'event': 'LED_STATUS_CHANGED', + 'data': { 'name' : 'str', + 'status' : 'LedState' + } +} diff --git a/qapi/qapi-schema.json b/qapi/qapi-schema.json index 43b0ba0dea..6f3ffc0ae1 100644 --- a/qapi/qapi-schema.json +++ b/qapi/qapi-schema.json @@ -84,3 +84,4 @@ { 'include': 'misc.json' } { 'include': 'misc-target.json' } { 'include': 'audio.json' } +{ 'include': 'led.json' } diff --git a/include/hw/misc/led.h b/include/hw/misc/led.h index 427ca1418e..9300d4db6c 100644 --- a/include/hw/misc/led.h +++ b/include/hw/misc/led.h @@ -21,6 +21,7 @@ typedef struct LEDState { qemu_irq irq; uint8_t current_state; + int64_t last_event_ms; /* Properties */ char *name; diff --git a/hw/misc/led.c b/hw/misc/led.c index 1bae1a34c0..11c7e8bb89 100644 --- a/hw/misc/led.c +++ b/hw/misc/led.c @@ -7,18 +7,40 @@ */ #include "qemu/osdep.h" #include "qapi/error.h" +#include "qapi/qapi-events-led.h" +#include "qemu/timer.h" #include "migration/vmstate.h" #include "hw/qdev-properties.h" #include "hw/misc/led.h" #include "hw/irq.h" #include "trace.h" +#define MAX_QMP_LED_EVENTS_PER_SEC 4 /* TODO shared between LED children? */ + +static void emit_led_status_changed_event(LEDState *s, int state) +{ + static const int64_t delay_min_ms = NANOSECONDS_PER_SECOND / SCALE_MS + / MAX_QMP_LED_EVENTS_PER_SEC; + int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); + + if (now - s->last_event_ms > delay_min_ms) { + qapi_event_send_led_status_changed(s->name, state + ? LED_STATE_ON + : LED_STATE_OFF); + } else { + /* TODO count skipped events? */ + } + s->last_event_ms = now; +} + static void led_set(void *opaque, int line, int new_state) { LEDState *s = LED(opaque); trace_led_set(s->name, s->current_state, new_state); - + if (new_state != s->current_state) { + emit_led_status_changed_event(s, new_state); + } s->current_state = new_state; } diff --git a/MAINTAINERS b/MAINTAINERS index 10593863dc..266b07c4b4 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1860,6 +1860,7 @@ F: stubs/vmgenid.c LED M: Philippe Mathieu-Daudé <f4...@amsat.org> S: Maintained +F: qapi/led.json F: include/hw/misc/led.h F: hw/misc/led.c diff --git a/qapi/Makefile.objs b/qapi/Makefile.objs index 4673ab7490..e9f6570c32 100644 --- a/qapi/Makefile.objs +++ b/qapi/Makefile.objs @@ -6,7 +6,7 @@ util-obj-y += qmp-event.o util-obj-y += qapi-util.o QAPI_COMMON_MODULES = audio authz block-core block char common control crypto -QAPI_COMMON_MODULES += dump error introspect job machine migration misc +QAPI_COMMON_MODULES += dump error introspect job led machine migration misc QAPI_COMMON_MODULES += net pragma qdev qom rdma rocker run-state sockets tpm QAPI_COMMON_MODULES += trace transaction ui QAPI_TARGET_MODULES = machine-target misc-target -- 2.21.3