examples/sensorbd_demo: fix gpio loopback test
authorJunhwan Park <junhwan.park@samsung.com>
Thu, 4 May 2017 05:16:51 +0000 (14:16 +0900)
committerHeesub Shin <heesub.shin@samsung.com>
Sat, 6 May 2017 14:00:44 +0000 (23:00 +0900)
Since we reworked the entire GPIO layer, 'sensorbd gpio' command have
been broken. This commit fixed it.

Change-Id: Ic04a800fc2bebd7991db8d1d8daf197ef367bb3e
Signed-off-by: Junhwan Park <junhwan.park@samsung.com>
apps/examples/sensorbd_demo/examples/gpio_loopback.c

index 90bca2f..8ba99e5 100644 (file)
 
 static int gpio_read(int port)
 {
-       int value = -1;
-       static char buf[16];
-       snprintf(buf, 16, "/dev/gpio%d", port);
-       int fd = open(buf, O_RDWR);
-
-       ioctl(fd, GPIOIOC_SET_DIRECTION, GPIO_DIRECTION_IN);
-       read(fd, (void *)&value, sizeof(int));
+       char buf[4];
+       char devpath[16];
+       snprintf(devpath, 16, "/dev/gpio%d", port);
+       int fd = open(devpath, O_RDWR);
 
+       read(fd, buf, sizeof(buf));
        close(fd);
-       return value;
+
+       return buf[0] == '1';
 }
 
 static void gpio_write(int port, int value)
 {
-       static char buf[16];
-       snprintf(buf, 16, "/dev/gpio%d", port);
-       int fd = open(buf, O_RDWR);
+       char buf[4];
+       char devpath[16];
+       snprintf(devpath, 16, "/dev/gpio%d", port);
+       int fd = open(devpath, O_RDWR);
 
-       int mode = value ? GPIO_DRIVE_PULLUP : GPIO_DRIVE_PULLDOWN;
-       ioctl(fd, GPIOIOC_SET_DIRECTION, GPIO_DIRECTION_IN);
-       ioctl(fd, GPIOIOC_SET_DRIVE, mode);
        ioctl(fd, GPIOIOC_SET_DIRECTION, GPIO_DIRECTION_OUT);
-       write(fd, (void *)&value, sizeof(int));
-
+       write(fd, buf, snprintf(buf, sizeof(buf), "%d", !!value));
        close(fd);
 }
 
@@ -123,7 +119,8 @@ void gpioloopback_main(int argc, char *argv[])
 
                        if (readA != readB) {
                                printf("gpio%d and gpio%d is not connect!\n", nA, nB);
-                               gpio_write(nA, 0);
+                       } else {
+                               printf("gpio%d and gpio%d is connect!\n", nA, nB);
                        }
                } else {
                        gpio_write(nA, 0);
@@ -133,11 +130,11 @@ void gpioloopback_main(int argc, char *argv[])
 
                        if (readA != readB) {
                                printf("gpio%d and gpio%d is not connect!\n", nA, nB);
-                               gpio_write(nA, 0);
+                       } else {
+                               printf("gpio%d and gpio%d is connect!\n", nA, nB);
                        }
                }
 
-               printf("gpio%d and gpio%d is connect!\n", nA, nB);
                gpio_write(nA, 0);
        }
 }