Add device ID generation to each device if an ID isn't given.
Signed-off-by: John Arbuckle <[email protected]>
---
This patch can be tested by adding adding usb devices using the monitor.
Start QEMU with the -usb option. Then go to the monitor and type
"device_add usb-mouse". The ID of the device will be set to a number.
Since QEMU will not allow an user to add a device with an ID set to a
number, there is no chance for ID collisions.
qdev-monitor.c | 24 ++++++++++++++++++------
1 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/qdev-monitor.c b/qdev-monitor.c
index f9e2d62..98267c4 100644
--- a/qdev-monitor.c
+++ b/qdev-monitor.c
@@ -26,6 +26,10 @@
#include "qapi/qmp/qerror.h"
#include "qemu/config-file.h"
#include "qemu/error-report.h"
+#include <math.h>
+
+/* USB's max number of devices is 127. This number is 3 digits long. */
+#define MAX_NUM_DIGITS_FOR_USB_ID 3
/*
* Aliases were a bad idea from the start. Let's keep them
@@ -574,17 +578,25 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
id = qemu_opts_id(opts);
if (id) {
dev->id = id;
+ } else { /* create an id for a device if none is provided */
+ static int device_id_count;
+
+ /* Add one for '\0' character */
+ char *device_id = (char *) malloc(sizeof(char) *
+ MAX_NUM_DIGITS_FOR_USB_ID + 1);
+ sprintf(device_id, "%d", device_id_count++);
+ dev->id = (const char *) device_id;
+
+ /* if device_id_count >= 10^MAX_NUM_DIGITS_FOR_USB_ID */
+ if (device_id_count >= pow(10, MAX_NUM_DIGITS_FOR_USB_ID)) {
+ printf("Warning: Maximum number of device ID's generated!\n\a");
+ printf("Time for you to make your own device ID's.\n");
+ }
}
if (dev->id) {
object_property_add_child(qdev_get_peripheral(), dev->id,
OBJECT(dev), NULL);
- } else {
- static int anon_count;
- gchar *name = g_strdup_printf("device[%d]", anon_count++);
- object_property_add_child(qdev_get_peripheral_anon(), name,
- OBJECT(dev), NULL);
- g_free(name);
}
/* set properties */
--
1.7.5.4