From e7dcb5e62d9ae88dd06196bad83b37f6e64fe45b Mon Sep 17 00:00:00 2001 From: Konrad Kuchciak Date: Wed, 5 Feb 2020 17:50:49 +0100 Subject: [PATCH] test: search for i2c bus exposed by SMBus stub driver Change-Id: I1863416c93149bad73adaba734e5f2fc2c1fe16f --- test/src/test_peripheral_i2c.c | 53 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/test/src/test_peripheral_i2c.c b/test/src/test_peripheral_i2c.c index 58f1ae0..1afb104 100644 --- a/test/src/test_peripheral_i2c.c +++ b/test/src/test_peripheral_i2c.c @@ -16,11 +16,11 @@ #include #include +#include +#include #include #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; -- 2.7.4