Add API for the adc device 65/133265/4
authorjino.cho <jino.cho@samsung.com>
Thu, 1 Jun 2017 01:32:26 +0000 (10:32 +0900)
committerjino.cho <jino.cho@samsung.com>
Tue, 13 Jun 2017 04:28:28 +0000 (13:28 +0900)
This patch support the adc device. And, it should work properly
if the patch for peripheral-bus is applied together.

Change-Id: Iac0b0d3421634443ef2de95268b748ab098d5170
Signed-off-by: jino.cho <jino.cho@samsung.com>
CMakeLists.txt
include/peripheral_gdbus.h
include/peripheral_gdbus_adc.h [new file with mode: 0644]
include/peripheral_io.h
src/peripheral_adc.c
src/peripheral_gdbus_adc.c [new file with mode: 0644]
src/peripheral_io.xml
test/peripheral-io-test.c

index ebacf9e..725fed5 100644 (file)
@@ -51,6 +51,7 @@ SET(SOURCES src/peripheral_gpio.c
                        src/peripheral_gdbus_gpio.c
                        src/peripheral_gdbus_i2c.c
                        src/peripheral_gdbus_pwm.c
+                       src/peripheral_gdbus_adc.c
                        src/peripheral_gdbus_uart.c
                        src/peripheral_gdbus_spi.c
                        src/peripheral_io_gdbus.c
@@ -85,4 +86,4 @@ CONFIGURE_FILE(
 )
 INSTALL(FILES ${PROJECT_NAME}.pc DESTINATION ${libdir}/pkgconfig)
 
-ADD_SUBDIRECTORY(test)
\ No newline at end of file
+ADD_SUBDIRECTORY(test)
index ff726de..0e4f15a 100644 (file)
@@ -22,6 +22,7 @@
 #define PERIPHERAL_GDBUS_GPIO_PATH     "/Org/Tizen/Peripheral_io/Gpio"
 #define PERIPHERAL_GDBUS_I2C_PATH      "/Org/Tizen/Peripheral_io/I2c"
 #define PERIPHERAL_GDBUS_PWM_PATH      "/Org/Tizen/Peripheral_io/Pwm"
+#define PERIPHERAL_GDBUS_ADC_PATH      "/Org/Tizen/Peripheral_io/Adc"
 #define PERIPHERAL_GDBUS_UART_PATH     "/Org/Tizen/Peripheral_io/Uart"
 #define PERIPHERAL_GDBUS_SPI_PATH      "/Org/Tizen/Peripheral_io/Spi"
 #define PERIPHERAL_GDBUS_NAME          "org.tizen.peripheral_io"
diff --git a/include/peripheral_gdbus_adc.h b/include/peripheral_gdbus_adc.h
new file mode 100644 (file)
index 0000000..2f90ecf
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __PERIPHERAL_GDBUS_ADC_H__
+#define __PERIPHERAL_GDBUS_ADC_H__
+
+void adc_proxy_init(void);
+void adc_proxy_deinit(void);
+
+int peripheral_gdbus_adc_read(unsigned int device, unsigned int channel, int *data);
+
+#endif /* __PERIPHERAL_GDBUS_ADC_H__ */
index baa48e1..bba13ca 100644 (file)
@@ -662,26 +662,21 @@ int peripheral_pwm_get_enable(peripheral_pwm_h pwm, bool *enable);
  */
 
 /**
- * @brief Struct for peripheral_gpio_s
- */
-
-#define DEVICE_NAME_SIZE       20
-
-struct _peripheral_adc_s {
-       char device_name[DEVICE_NAME_SIZE];
-       int channel;
-};
-
-/**
- * @brief Pointer definition to the internal struct peripheral_adc_s
+ * @brief Reads data from the adc device.
+ * @since_tizen 4.0
+ *
+ * @param[in] device The device number of the adc device
+ * @param[in] channel The channel number to read
+ * @param[out] data The address of buffer to read
+ *
+ * @return 0 on success, otherwise a negative error value
+ * @retval #PERIPHERAL_ERROR_NONE Successful
+ * @retval #PERIPHERAL_ERROR_IO_ERROR I/O operation failed
+ * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
+ * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
  */
-typedef struct _peripheral_adc_s* peripheral_adc_context_h;
-
-peripheral_adc_context_h peripheral_adc_open(int channel);
-
-int peripheral_adc_read(peripheral_adc_context_h dev, int *data);
-
-int peripheral_adc_close(peripheral_adc_context_h dev);
+int peripheral_adc_read(unsigned int device, unsigned int channel, int *data);
 
 /**
 * @}
index 1aa2973..8df1d75 100644 (file)
  */
 
 #include "peripheral_io.h"
+#include "peripheral_gdbus_adc.h"
+#include "peripheral_common.h"
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-peripheral_adc_context_h peripheral_adc_open(int channel)
-{
-       return NULL;
-}
-
-int peripheral_adc_read(peripheral_adc_context_h dev, int *data)
+int peripheral_adc_read(unsigned int device, unsigned int channel, int *data)
 {
-       if (dev == NULL)
-               return PERIPHERAL_ERROR_INVALID_PARAMETER;
-       if (data == NULL)
-               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+       int ret = PERIPHERAL_ERROR_NONE;
 
-       return PERIPHERAL_ERROR_INVALID_OPERATION;
-}
-
-int peripheral_adc_close(peripheral_adc_context_h dev)
-{
-       if (dev != NULL)
-               free(dev);
+       adc_proxy_init();
 
-       dev = NULL;
+       ret = peripheral_gdbus_adc_read(device, channel, data);
+       if (ret < PERIPHERAL_ERROR_NONE)
+               _E("Failed to read, ret : %d", ret);
 
-       return PERIPHERAL_ERROR_INVALID_OPERATION;
+       return ret;
 }
diff --git a/src/peripheral_gdbus_adc.c b/src/peripheral_gdbus_adc.c
new file mode 100644 (file)
index 0000000..e9a2513
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "peripheral_io.h"
+#include "peripheral_gdbus.h"
+#include "peripheral_common.h"
+#include "peripheral_io_gdbus.h"
+
+PeripheralIoGdbusAdc *adc_proxy = NULL;
+
+void adc_proxy_init(void)
+{
+       GError *error = NULL;
+
+       if (adc_proxy != NULL)
+               return;
+
+       adc_proxy = peripheral_io_gdbus_adc_proxy_new_for_bus_sync(
+               G_BUS_TYPE_SYSTEM,
+               G_DBUS_PROXY_FLAGS_NONE,
+               PERIPHERAL_GDBUS_NAME,
+               PERIPHERAL_GDBUS_ADC_PATH,
+               NULL,
+               &error);
+}
+
+void adc_proxy_deinit()
+{
+       if (adc_proxy) {
+               g_object_unref(adc_proxy);
+               if (!G_IS_OBJECT(adc_proxy))
+                       adc_proxy = NULL;
+       }
+}
+
+int peripheral_gdbus_adc_read(unsigned int device, unsigned int channel, int *data)
+{
+       GError *error = NULL;
+       peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
+       gint value;
+
+       if (adc_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN;
+
+       if (peripheral_io_gdbus_adc_call_read_sync(
+                       adc_proxy,
+                       device,
+                       channel,
+                       &value,
+                       &ret,
+                       NULL,
+                       &error) == FALSE) {
+               _E("Error in %s() : %s\n", __func__, error->message);
+               g_error_free(error);
+               return PERIPHERAL_ERROR_UNKNOWN;
+       }
+
+       *data = value;
+
+       return ret;
+}
index ea4b1f9..16b3cd2 100644 (file)
                        <arg type="i" name="result" direction="out"/>
                </method>
        </interface>
+       <interface name="org.tizen.peripheral_io.adc">
+               <method name="Read">
+                       <arg type="u" name="device" direction="in"/>
+                       <arg type="u" name="channel" direction="in"/>
+                       <arg type="i" name="value" direction="out"/>
+                       <arg type="i" name="result" direction="out"/>
+               </method>
+       </interface>
        <interface name="org.tizen.peripheral_io.uart">
                <method name="Open">
                        <arg type="i" name="port" direction="in"/>
index 00bb5f7..23a63ae 100644 (file)
@@ -908,8 +908,40 @@ int enter_pwm_test(void)
        return 0;
 }
 
+int adc_read_channel(void)
+{
+       int device, channel, ret;
+       int value;
+
+       printf("%s\n", __func__);
+
+       printf("Enter adc device number\n");
+       if (read_int_input(&device) < 0)
+               return -1;
+
+       printf("Enter adc channel number\n");
+       if (read_int_input(&channel) < 0)
+               return -1;
+
+       if ((ret = peripheral_adc_read(device, channel, &value)) < 0) {
+               printf(">>>>> Failed to read adc value, ret : %d\n", ret);
+               return -1;
+       }
+       printf("ADC(%d,%d) Value = %d\n", device, channel, value);
+
+       return 0;
+}
+
+tc_table_t adc_tc_table[] = {
+       {"Read ADC Channel",                    1, adc_read_channel},
+       {"Go back to main",                             0, enter_main},
+       {NULL,  0, NULL},
+};
+
 int enter_adc_test(void)
 {
+       tc_table = adc_tc_table;
+
        return 0;
 }