From: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
To: qemu-devel@nongnu.org
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
"Michael Roth" <mdroth@linux.vnet.ibm.com>,
"Philippe Mathieu-Daudé" <philmd@redhat.com>,
"Philippe Mathieu-Daudé" <f4bug@amsat.org>,
"Dr . David Alan Gilbert" <dgilbert@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Esteban Bosse" <estebanbosse@gmail.com>,
qemu-arm@nongnu.org, "Gerd Hoffmann" <kraxel@redhat.com>,
"Joel Stanley" <joel@jms.id.au>
Subject: [RFC PATCH v2 2/5] hw/misc/led: Add LED_STATUS_CHANGED QAPI event
Date: Fri, 12 Jun 2020 19:54:37 +0200 [thread overview]
Message-ID: <20200612175440.9901-3-f4bug@amsat.org> (raw)
In-Reply-To: <20200612175440.9901-1-f4bug@amsat.org>
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é <f4bug@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é <f4bug@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
next prev parent reply other threads:[~2020-06-12 17:56 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-12 17:54 [RFC PATCH v2 0/5] hw/misc: Add LED device Philippe Mathieu-Daudé
2020-06-12 17:54 ` [RFC PATCH v2 1/5] hw/misc: Add a " Philippe Mathieu-Daudé
2020-06-12 18:44 ` Stefan Weil
2020-06-15 10:55 ` Dr. David Alan Gilbert
2020-06-15 11:19 ` Philippe Mathieu-Daudé
2020-06-15 11:50 ` Dr. David Alan Gilbert
2020-06-12 17:54 ` Philippe Mathieu-Daudé [this message]
2020-06-15 16:05 ` [RFC PATCH v2 2/5] hw/misc/led: Add LED_STATUS_CHANGED QAPI event Peter Maydell
2020-06-12 17:54 ` [RFC PATCH v2 3/5] hw/misc/led: Add create_led_by_gpio_id() helper Philippe Mathieu-Daudé
2020-06-12 17:54 ` [RFC PATCH v2 4/5] hw/arm/microbit: Add a fake LED to use as proof-of-concept with Zephyr Philippe Mathieu-Daudé
2020-06-15 16:02 ` Peter Maydell
2020-06-15 16:10 ` Philippe Mathieu-Daudé
2020-06-12 17:54 ` [RFC PATCH v2 5/5] hw/arm/tosa: Use LED device for the Bluetooth led Philippe Mathieu-Daudé
2020-06-15 16:00 ` Peter Maydell
2020-06-15 16:18 ` Philippe Mathieu-Daudé
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200612175440.9901-3-f4bug@amsat.org \
--to=f4bug@amsat.org \
--cc=armbru@redhat.com \
--cc=dgilbert@redhat.com \
--cc=estebanbosse@gmail.com \
--cc=joel@jms.id.au \
--cc=kraxel@redhat.com \
--cc=mdroth@linux.vnet.ibm.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@redhat.com \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).