test: search for i2c bus exposed by SMBus stub driver 27/224027/2 accepted/tizen/unified/20200217.213905 submit/tizen/20200214.101934 submit/tizen/20200217.095901
authorKonrad Kuchciak <k.kuchciak@samsung.com>
Wed, 5 Feb 2020 16:50:49 +0000 (17:50 +0100)
committerKonrad Kuchciak <k.kuchciak@samsung.com>
Tue, 11 Feb 2020 13:38:44 +0000 (14:38 +0100)
Change-Id: I1863416c93149bad73adaba734e5f2fc2c1fe16f

test/src/test_peripheral_i2c.c

index 58f1ae0..1afb104 100644 (file)
 
 #include <stdio.h>
 #include <string.h>
+#include <dirent.h>
+#include <limits.h>
 #include <peripheral_io.h>
 #include "test_peripheral_i2c.h"
 
-#define I2C_BUS_RPI3 4
-#define I2C_BUS_ARTIK530 11
 #define I2C_BUS_INVALID -99
 #define I2C_ADDRESS 0x39
 #define I2C_ADDRESS_INVALID -99
@@ -32,15 +32,54 @@ static bool g_feature = true;
 static int bus;
 static int address;
 
+static int __find_stub_driver(void)
+{
+       DIR *d;
+       struct dirent *dir;
+       FILE *fp;
+       char *dev_path = "/sys/class/i2c-dev/";
+       char name_file[PATH_MAX];
+       char name[17];
+       int bus, n;
+       int ret = -1;
+
+       d = opendir(dev_path);
+       if (!d)
+               return -1;
+
+       while ((dir = readdir(d)) != NULL) {
+               if (sscanf(dir->d_name, "i2c-%d", &bus) <= 0)
+                       continue;
+
+               snprintf(name_file, PATH_MAX, "%s/i2c-%d/name", dev_path, bus);
+               fp = fopen(name_file, "r");
+               if (!fp)
+                       continue;
+
+               memset(name, 0, sizeof(name));
+               n = fread(name, sizeof(char), sizeof(name), fp);
+               fclose(fp);
+
+               if (strncmp(name, "SMBus stub driver", n) == 0) {
+                       ret = bus;
+                       goto cleanup;
+               }
+       }
+
+cleanup:
+       closedir(d);
+       return ret;
+}
+
 int test_peripheral_io_i2c_initialize(char *model, bool feature)
 {
        g_feature = feature;
 
-       if (!strcmp(model, "rpi3"))
-               bus = I2C_BUS_RPI3;
-       else if (!strcmp(model, "artik"))
-               bus = I2C_BUS_ARTIK530;
-       else
+       if (strcmp(model, "rpi3") != 0 && strcmp(model, "artik") != 0)
+               return PERIPHERAL_ERROR_NO_DEVICE;
+
+       bus = __find_stub_driver();
+       if (bus == -1)
                return PERIPHERAL_ERROR_NO_DEVICE;
 
        address = I2C_ADDRESS;