#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
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;