From 3f67fb53729f3c9996e2d669d22389e276819ab2 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Thu, 20 Apr 2017 11:05:08 +0900 Subject: [PATCH] Return i2c handle through pointer argument Change-Id: If65c0330fcd61edafea318018144d463fa1d559f Signed-off-by: Hyeongsik Min --- include/peripheral_io.h | 2 +- src/peripheral_i2c.c | 30 ++++++++++++++++++------------ test/peripheral-io-test.c | 14 +++++++------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 44c9b59..4458ae6 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -288,7 +288,7 @@ int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin); typedef struct _peripheral_i2c_s *peripheral_i2c_h; -peripheral_i2c_h peripheral_i2c_init(int bus); +int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c); int peripheral_i2c_stop(peripheral_i2c_h i2c); diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 35c5d06..a63e6f8 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -31,33 +31,39 @@ extern "C" { #define I2C_NAME "i2c" int I2C_Addr = 0; -peripheral_i2c_h peripheral_i2c_init(int bus) +int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) { - peripheral_i2c_h i2c; + peripheral_i2c_h handle; int ret = PERIPHERAL_ERROR_NONE; - assert(bus >= 0); + if (bus < 0) + return PERIPHERAL_ERROR_INVALID_PARAMETER; /* Initialize peripheral_i2c_h */ - i2c = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s)); + handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s)); - if (i2c == NULL) { + if (handle == NULL) { _E("Failed to allocate peripheral_i2c_h"); - return NULL; + return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - if (!get_dbus_connection()) - set_dbus_connection(); + if (!get_dbus_connection()) { + ret = set_dbus_connection(); + if (ret != PERIPHERAL_ERROR_NONE) + goto exit; + } - ret = peripheral_dbus_i2c(i2c, I2C_NAME, "INIT", bus, 0, I2C_Addr); + ret = peripheral_dbus_i2c(handle, I2C_NAME, "INIT", bus, 0, I2C_Addr); +exit: if (ret != PERIPHERAL_ERROR_NONE) { - free(i2c); _E("[PERIPHERAL] I2C init error\n"); - i2c = NULL; + free(handle); + handle = NULL; } + *i2c = handle; - return i2c; + return ret; } int peripheral_i2c_stop(peripheral_i2c_h i2c) diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 857fb66..c9b4609 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -78,24 +78,24 @@ int i2c_test(void) int cnt = 0; int bus_num; unsigned char buf[10]; - peripheral_i2c_h dev; + peripheral_i2c_h i2c; printf(">> I2C bus number : "); if (scanf("%d", &bus_num) < 0) return 0; - if ((dev = peripheral_i2c_init(bus_num)) == NULL) { + if ((peripheral_i2c_init(bus_num, &i2c)) != 0) { printf("Failed to initialize I2C device\n"); return 0; } - if (peripheral_i2c_set_address(dev, GY30_ADDR) != 0) { + if (peripheral_i2c_set_address(i2c, GY30_ADDR) != 0) { printf("Failed to set address\n"); goto error; } buf[0] = GY30_CONT_HIGH_RES_MODE; - if (peripheral_i2c_write(dev, buf, 1) != 0) { + if (peripheral_i2c_write(i2c, buf, 1) != 0) { printf("Failed to write\n"); goto error; } @@ -103,16 +103,16 @@ int i2c_test(void) while (cnt++ < 15) { int result; sleep(1); - peripheral_i2c_read(dev, buf, 2); + peripheral_i2c_read(i2c, buf, 2); result = GY30_READ_INTENSITY(buf); printf("Result [%d]\n", result); } - peripheral_i2c_stop(dev); + peripheral_i2c_stop(i2c); return 1; error: - peripheral_i2c_stop(dev); + peripheral_i2c_stop(i2c); return 0; } -- 2.34.1