usb_gadget: Retrieve device serial number 53/179953/1 submit/tizen/20180527.233450 submit/tizen/20180531.063655
authorPaweł Szewczyk <p.szewczyk@samsung.com>
Wed, 23 May 2018 11:06:15 +0000 (13:06 +0200)
committerPaweł Szewczyk <p.szewczyk@samsung.com>
Wed, 23 May 2018 11:15:25 +0000 (13:15 +0200)
Serial number is retrieved from mmc serial number.

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

index cc965a6..38e9e6c 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,35 @@ out:
        return -ENOMEM;
 }
 
+#define MMC_ID_PATH "/sys/class/mmc_host/mmc0/mmc0:0001/cid"
+#define SERIAL_OFFSET 16
+#define SERIAL_LEN 16
+#define LINE_LEN (SERIAL_OFFSET + SERIAL_LEN + 2)
+
+static int get_device_serial(char **out)
+{
+       FILE *fp;
+       char line[LINE_LEN], *p;
+
+       fp = fopen(MMC_ID_PATH, "r");
+       if (!fp)
+               return -1;
+
+       p = fgets(line, LINE_LEN, fp);
+       fclose(fp);
+       if (p == NULL)
+               return -1;
+
+       *out = strndup(line + SERIAL_OFFSET, SERIAL_LEN);
+       return 0;
+}
+
 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 +168,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;