Return i2c handle through pointer argument 36/126036/3
authorHyeongsik Min <hyeongsik.min@samsung.com>
Thu, 20 Apr 2017 02:05:08 +0000 (11:05 +0900)
committerHyeongsik Min <hyeongsik.min@samsung.com>
Thu, 20 Apr 2017 10:42:14 +0000 (03:42 -0700)
Change-Id: If65c0330fcd61edafea318018144d463fa1d559f
Signed-off-by: Hyeongsik Min <hyeongsik.min@samsung.com>
include/peripheral_io.h
src/peripheral_i2c.c
test/peripheral-io-test.c

index 44c9b59..4458ae6 100644 (file)
@@ -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);
 
index 35c5d06..a63e6f8 100644 (file)
@@ -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)
index 857fb66..c9b4609 100644 (file)
@@ -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;
 }