Received: from eggs.gnu.org ([2001:4830:134:3::10]:36069) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZTwrn-0005kZ-TR for qemu-devel@nongnu.org; Mon, 24 Aug 2015 14:53:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZTwrk-00009b-Ms for qemu-devel@nongnu.org; Mon, 24 Aug 2015 14:53:11 -0400 Received: from mail-qg0-x22e.google.com ([2607:f8b0:400d:c04::22e]:34147) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZTwrk-00009X-Gr for qemu-devel@nongnu.org; Mon, 24 Aug 2015 14:53:08 -0400 Received: by qgeg42 with SMTP id g42so92828711qge.1 for ; Mon, 24 Aug 2015 11:53:08 -0700 (PDT) From: Programmingkid Content-Type: multipart/alternative; boundary=Apple-Mail-5-426202984 Date: Mon, 24 Aug 2015 14:53:07 -0400 Message-Id: <29C62C49-06A5-4F99-8062-7269A28C29A3@gmail.com> Mime-Version: 1.0 (Apple Message framework v1084) Subject: [Qemu-devel] [PATCH] qdev-monitor.c: Add device id generation List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel qemu-devel , Markus Armbruster --Apple-Mail-5-426202984 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Add device ID generation to each device if an ID isn't given. Signed-off-by: John Arbuckle --- 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.=20 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 + +/* USB's max number of devices is 127. This number is 3 digits long. */ +#define MAX_NUM_DIGITS_FOR_USB_ID 3 =20 /* * 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 =3D qemu_opts_id(opts); if (id) { dev->id =3D 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 =3D (char *) malloc(sizeof(char) * + MAX_NUM_DIGITS_FOR_USB_ID + = 1); + sprintf(device_id, "%d", device_id_count++); + dev->id =3D (const char *) device_id; + + /* if device_id_count >=3D 10^MAX_NUM_DIGITS_FOR_USB_ID */ + if (device_id_count >=3D 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"); + } } =20 if (dev->id) { object_property_add_child(qdev_get_peripheral(), dev->id, OBJECT(dev), NULL); - } else { - static int anon_count; - gchar *name =3D g_strdup_printf("device[%d]", anon_count++); - object_property_add_child(qdev_get_peripheral_anon(), name, - OBJECT(dev), NULL); - g_free(name); } =20 /* set properties */ --=20 1.7.5.4 --Apple-Mail-5-426202984 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii
Add = device ID generation to each device if an ID isn't given.

Signed-off-by: John Arbuckle <programmingkidx@gmail.com>= ;

---
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
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/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)
         = dev->id =3D 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 =3D = (char *) malloc(sizeof(char) *
+          =                     =               = MAX_NUM_DIGITS_FOR_USB_ID + 1);
+        = sprintf(device_id, "%d", device_id_count++);
+
+        /* if device_id_count = >=3D 10^MAX_NUM_DIGITS_FOR_USB_ID */
+        = if (device_id_count >=3D pow(10, MAX_NUM_DIGITS_FOR_USB_ID)) = {
+        =     printf("Time for you to make your own device = ID's.\n");
+        }
 

     if (dev->id) = {
-    } else {
-        = g_free(name);
     }

 

-- 

= --Apple-Mail-5-426202984--