usb_gadget: Retrieve device serial number 97/179097/1 accepted/tizen/unified/20180516.065547 submit/tizen/20180516.042321
authorPaweł Szewczyk <p.szewczyk@samsung.com>
Tue, 15 May 2018 14:32:56 +0000 (16:32 +0200)
committerPaweł Szewczyk <p.szewczyk@samsung.com>
Tue, 15 May 2018 16:14:37 +0000 (18:14 +0200)
The serial number is retrieved from /proc/cpuinfo and used as USB serial
number string.

Change-Id: Ibac051862011f697d7a63e8efeb5f50d0ea16c29
Signed-off-by: Paweł Szewczyk <p.szewczyk@samsung.com>
hw/usb_gadget/usb_gadget.c

index 00bd9a13f99ca9a7162de04a5c1f60e1ccdce268..c412b8e9c2c71a3131ebf7332a2a20ec8e6bd2d4 100755 (executable)
@@ -20,6 +20,7 @@
 
 #include <hw/usb_gadget.h>
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
@@ -122,11 +123,52 @@ out:
        return -ENOMEM;
 }
 
+#define CPUINFO_DIR "/proc/cpuinfo"
+#define SERIAL_TAG "Serial"
+#define LINE_LEN 64
+
+static int get_device_serial(char **out)
+{
+       FILE *fp;
+       char line[LINE_LEN], *p, *q;
+
+       fp = fopen(CPUINFO_DIR, "r");
+       if (!fp)
+               return -1;
+
+       while ((p = fgets(line, LINE_LEN, fp)) != NULL) {
+               p = strchr(p, '\t');
+               if (!p)
+                       continue;
+
+               *p = '\0';
+
+               if (strncmp(line, SERIAL_TAG, LINE_LEN) != 0)
+                       continue;
+
+               ++p;
+               p = strchr(p, ':');
+               if (!p)
+                       continue;
+               p += 2;
+
+               q = strchrnul(p, '\n');
+               *q = '\0';
+               *out = strdup(p);
+               fclose(fp);
+               return 0;
+       }
+
+       fclose(fp);
+       return -1;
+}
+
 static int alloc_default_gadget(struct usb_gadget **_gadget)
 {
        struct usb_gadget *gadget;
        struct usb_gadget_strings *strs;
        struct usb_configuration **configs;
+       int ret;
 
        gadget = zalloc(sizeof(*gadget));
        if (!gadget)
@@ -143,7 +185,9 @@ static int alloc_default_gadget(struct usb_gadget **_gadget)
        strs[0].lang_code = 0x409;
        strs[0].manufacturer = strdup(DEFAULT_MANUFACTURER);
        strs[0].product = strdup(DEFAULT_PRODUCT);
-       strs[0].serial = strdup(DEFAULT_SERIAL);
+       ret = get_device_serial(&strs[0].serial);
+       if (ret < 0)
+               strs[0].serial = strdup(DEFAULT_SERIAL);
 
        if (!strs[0].manufacturer || !strs[0].product || !strs[0].serial)
                goto free_strs;