Add API for the adc device 13/184513/7 accepted/tizen_5.0_unified accepted/tizen/5.0/unified/20181102.013255 accepted/tizen/unified/20180822.164537 submit/tizen/20180821.133358 submit/tizen_5.0/20181101.000002
authorKonrad Kuchciak <k.kuchciak@samsung.com>
Wed, 18 Jul 2018 09:56:23 +0000 (11:56 +0200)
committerKonrad Kuchciak <k.kuchciak@samsung.com>
Tue, 21 Aug 2018 08:11:09 +0000 (10:11 +0200)
This commit adds support for the adc device.
For this to work, adc also has to be supported by peripheral-bus.

Change-Id: I3277815fde9e6b82e5d165ccb5003b11f505f0e9
Signed-off-by: Konrad Kuchciak <k.kuchciak@samsung.com>
14 files changed:
CMakeLists.txt
doc/peripheral_io_doc.h
include/gdbus/peripheral_gdbus_adc.h [new file with mode: 0644]
include/gdbus/peripheral_gdbus_common.h
include/interface/peripheral_interface_adc.h [new file with mode: 0644]
include/peripheral_handle.h
include/peripheral_io.h
src/gdbus/peripheral_gdbus_adc.c [new file with mode: 0644]
src/gdbus/peripheral_io.xml
src/interface/peripheral_interface_adc.c [new file with mode: 0644]
src/peripheral_adc.c [new file with mode: 0644]
test/include/test_peripheral_adc.h [new file with mode: 0644]
test/peripheral-io-test.c
test/src/test_peripheral_adc.c [new file with mode: 0644]

index 68a4f6a..b4da425 100644 (file)
@@ -50,16 +50,19 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=%{_libdir}")
 SET(SOURCES src/peripheral_gpio.c
                        src/peripheral_i2c.c
                        src/peripheral_pwm.c
+                       src/peripheral_adc.c
                        src/peripheral_uart.c
                        src/peripheral_spi.c
                        src/interface/peripheral_interface_gpio.c
                        src/interface/peripheral_interface_i2c.c
                        src/interface/peripheral_interface_pwm.c
+                       src/interface/peripheral_interface_adc.c
                        src/interface/peripheral_interface_spi.c
                        src/interface/peripheral_interface_uart.c
                        src/gdbus/peripheral_gdbus_gpio.c
                        src/gdbus/peripheral_gdbus_i2c.c
                        src/gdbus/peripheral_gdbus_pwm.c
+                       src/gdbus/peripheral_gdbus_adc.c
                        src/gdbus/peripheral_gdbus_uart.c
                        src/gdbus/peripheral_gdbus_spi.c
                        src/gdbus/peripheral_io_gdbus.c)
index 6741d54..6527e92 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017-2018 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.
@@ -24,7 +24,7 @@
  *
  * @section   CAPI_SYSTEM_PERIPHERAL_IO_MODULE_OVERVIEW Overview
  * This @ref CAPI_SYSTEM_PERIPHERAL_IO_MODULE API provides access to the low level device providers,
- * including GPIO, I2C, PWM, UART and SPI.
+ * including GPIO, I2C, PWM, ADC, UART and SPI.
  */
 
 
  *
  * More details on featuring your application can be found from <a href="https://developer.tizen.org/development/tizen-studio/native-tools/configuring-your-app/manifest-text-editor#feature"><b>Feature Element</b>.</a>
  */
+
+
+/**
+ * @ingroup   CAPI_SYSTEM_PERIPHERAL_IO_MODULE
+ * @defgroup  CAPI_SYSTEM_PERIPHERAL_IO_ADC_MODULE ADC
+ * @brief     The @ref CAPI_SYSTEM_PERIPHERAL_IO_ADC_MODULE API provides functions to control ADC peripherals connected to the IoT device.
+ *
+ * @section   CAPI_SYSTEM_PERIPHERAL_IO_ADC_MODULE_HEADER Required Header
+ *            \#include <peripheral_io.h>
+ *
+ * @section   CAPI_SYSTEM_PERIPHERAL_IO_ADC_MODULE_OVERVIEW Overview
+ *
+ * This @ref CAPI_SYSTEM_PERIPHERAL_IO_ADC_MODULE API provides functions to control ADC peripherals connected to the IoT device.
+ *
+ * @section   CAPI_SYSTEM_PERIPHERAL_IO_ADC_MODULE_FEATURE Realted Features
+ *
+ * This API is related with the following feature:\n
+ * - http://tizen.org/feature/peripheral_io.adc\n
+ *
+ * It is recommended to use features in your application for reliability.\n
+ *
+ * You can check if a IoT device supports the related features for this API \n
+ * by using @ref CAPI_SYSTEM_SYSTEM_INFO_MODULE, and control your application's actions accordingly.\n
+ *
+ * To ensure your application is only running on the IoT device with specific features, \n
+ * please define the features in your manifest file using the manifest editor in the SDK.\n
+ *
+ * More details on featuring your application can be found from <a href="https://developer.tizen.org/development/tizen-studio/native-tools/configuring-your-app/manifest-text-editor#feature"><b>Feature Element</b>.</a>
+ */
diff --git a/include/gdbus/peripheral_gdbus_adc.h b/include/gdbus/peripheral_gdbus_adc.h
new file mode 100644 (file)
index 0000000..ffb1af7
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2018 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__
+
+#include "peripheral_gdbus_common.h"
+
+int peripheral_gdbus_adc_open(peripheral_adc_h adc, int device, int channel);
+int peripheral_gdbus_adc_close(peripheral_adc_h adc);
+
+#endif /* __PERIPHERAL_GDBUS_ADC_H__ */
index 62b0717..803faf3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017-2018 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.
@@ -30,6 +30,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/interface/peripheral_interface_adc.h b/include/interface/peripheral_interface_adc.h
new file mode 100644 (file)
index 0000000..87d5383
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018 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_INTERFACE_ADC_H__
+#define __PERIPHERAL_INTERFACE_ADC_H__
+
+#include "peripheral_interface_common.h"
+
+#define ADC_BUFFER_MAX 64
+
+void peripheral_interface_adc_close(peripheral_adc_h adc);
+
+int peripheral_interface_adc_read(peripheral_adc_h adc, uint32_t *value);
+
+#endif/*__PERIPHERAL_INTERFACE_GPIO_H__*/
index 18f260d..9023b4e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017-2018 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.
@@ -60,6 +60,14 @@ struct _peripheral_pwm_s {
 };
 
 /**
+ * @brief Internal struct for adc context
+ */
+struct _peripheral_adc_s {
+       uint handle;
+       int fd;
+};
+
+/**
  * @brief Internal struct for uart context
  */
 struct _peripheral_uart_s {
index f652186..59187db 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2016-2018 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.
@@ -663,6 +663,92 @@ int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, bool enabled);
 */
 
 /**
+ * @addtogroup CAPI_SYSTEM_PERIPHERAL_IO_ADC_MODULE
+ * @{
+ */
+
+/**
+ * @brief The handle of the ADC peripherals.
+ * @since_tizen 5.0
+ */
+typedef struct _peripheral_adc_s *peripheral_adc_h;
+
+/**
+ * @platform
+ * @brief Opens the ADC pin.
+ * @since_tizen 5.0
+ * @privlevel platform
+ * @privilege http://tizen.org/privilege/peripheralio
+ * @remarks @a adc should be released with peripheral_adc_close()
+ *
+ * @param[in] device The ADC device number
+ * @param[in] channel The ADC channel number to control
+ * @param[out] adc The ADC handle is created on success
+ *
+ * @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_NO_DEVICE Device does not exist or is removed
+ * @retval #PERIPHERAL_ERROR_OUT_OF_MEMORY Memory allocation failed
+ * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied
+ * @retval #PERIPHERAL_ERROR_RESOURCE_BUSY Device is in use
+ * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported
+ * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
+ *
+ * @post peripheral_adc_close()
+ */
+int peripheral_adc_open(int device, int channel, peripheral_adc_h *adc);
+
+/**
+ * @platform
+ * @brief Closes the ADC pin.
+ * @since_tizen 5.0
+ * @privlevel platform
+ * @privilege http://tizen.org/privilege/peripheralio
+ *
+ * @param[in] adc The ADC handle
+ *
+ * @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_NO_DEVICE Device does not exist or is removed
+ * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied
+ * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported
+ * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
+ *
+ * @pre peripheral_adc_open()
+ */
+int peripheral_adc_close(peripheral_adc_h adc);
+
+/**
+ * @platform
+ * @brief Gets the current value of the ADC pin.
+ * @since_tizen 5.0
+ * @privlevel platform
+ * @privilege http://tizen.org/privilege/peripheralio
+ *
+ * @param[in] adc The ADC handle
+ * @param[out] value The value to get
+ *
+ * @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_NO_DEVICE Device does not exist or is removed
+ * @retval #PERIPHERAL_ERROR_PERMISSION_DENIED Permission denied
+ * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #PERIPHERAL_ERROR_NOT_SUPPORTED Not supported
+ * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
+ *
+ */
+int peripheral_adc_read(peripheral_adc_h adc, uint32_t *value);
+
+/**
+* @}
+*/
+
+/**
  * @addtogroup CAPI_SYSTEM_PERIPHERAL_IO_UART_MODULE
  * @{
  */
diff --git a/src/gdbus/peripheral_gdbus_adc.c b/src/gdbus/peripheral_gdbus_adc.c
new file mode 100644 (file)
index 0000000..4ef8ed3
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2018 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 "peripheral_gdbus_adc.h"
+
+#define ADC_FD_INDEX 0
+
+static PeripheralIoGdbusAdc *adc_proxy = NULL;
+
+static int __adc_proxy_init(void)
+{
+       GError *error = NULL;
+
+       if (adc_proxy != NULL) {
+               _E("Adc proxy is already created");
+               g_object_ref(adc_proxy);
+               return PERIPHERAL_ERROR_NONE;
+       }
+
+       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);
+
+       if (adc_proxy == NULL) {
+               _E("Failed to create adc proxy : %s", error->message);
+               g_error_free(error);
+               return PERIPHERAL_ERROR_IO_ERROR;
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+static int __adc_proxy_deinit(void)
+{
+       RETVM_IF(adc_proxy == NULL, PERIPHERAL_ERROR_IO_ERROR, "Adc proxy is NULL");
+
+       g_object_unref(adc_proxy);
+       if (!G_IS_OBJECT(adc_proxy))
+               adc_proxy = NULL;
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+int peripheral_gdbus_adc_open(peripheral_adc_h adc, int device, int channel)
+{
+       int ret;
+       GError *error = NULL;
+       GUnixFDList *fd_list = NULL;
+
+       ret = __adc_proxy_init();
+       if (ret != PERIPHERAL_ERROR_NONE)
+               return ret;
+
+       if (peripheral_io_gdbus_adc_call_open_sync(
+                       adc_proxy,
+                       device,
+                       channel,
+                       NULL,
+                       &adc->handle,
+                       &ret,
+                       &fd_list,
+                       NULL,
+                       &error) == FALSE) {
+               _E("Failed to request daemon to adc open : %s", error->message);
+               g_error_free(error);
+               return PERIPHERAL_ERROR_IO_ERROR;
+       }
+
+       // TODO : If ret is not PERIPHERAL_ERROR_NONE, fd list it NULL from daemon.
+       if (ret != PERIPHERAL_ERROR_NONE)
+               return ret;
+
+       adc->fd = g_unix_fd_list_get(fd_list, ADC_FD_INDEX, &error);
+       if (adc->fd < 0) {
+               _E("Failed to get fd for adc : %s", error->message);
+               g_error_free(error);
+               ret = PERIPHERAL_ERROR_IO_ERROR;
+       }
+
+       g_object_unref(fd_list);
+
+       return ret;
+}
+
+int peripheral_gdbus_adc_close(peripheral_adc_h adc)
+{
+       RETVM_IF(adc_proxy == NULL, PERIPHERAL_ERROR_IO_ERROR, "Adc proxy is NULL");
+
+       int ret;
+       GError *error = NULL;
+
+       if (peripheral_io_gdbus_adc_call_close_sync(
+                       adc_proxy,
+                       adc->handle,
+                       &ret,
+                       NULL,
+                       &error) == FALSE) {
+               _E("Failed to request daemon to adc close : %s", error->message);
+               g_error_free(error);
+               return PERIPHERAL_ERROR_IO_ERROR;
+       }
+
+       __adc_proxy_deinit();
+
+       return ret;
+}
index 00c49e7..db72361 100644 (file)
                        <arg type="i" name="result" direction="out"/>
                </method>
        </interface>
+       <interface name="org.tizen.peripheral_io.adc">
+               <method name="Open">
+                       <annotation name="org.gtk.GDBus.C.UnixFD" value="true"/>
+                       <arg type="i" name="device" direction="in"/>
+                       <arg type="i" name="channel" direction="in"/>
+                       <arg type="u" name="handle" direction="out"/>
+                       <arg type="i" name="result" direction="out"/>
+               </method>
+               <method name="Close">
+                       <arg type="u" name="handle" direction="in"/>
+                       <arg type="i" name="result" direction="out"/>
+               </method>
+       </interface>
        <interface name="org.tizen.peripheral_io.uart">
                <method name="Open">
                        <annotation name="org.gtk.GDBus.C.UnixFD" value="true"/>
diff --git a/src/interface/peripheral_interface_adc.c b/src/interface/peripheral_interface_adc.c
new file mode 100644 (file)
index 0000000..9282858
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2018 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 "peripheral_interface_adc.h"
+
+int peripheral_interface_adc_read(peripheral_adc_h adc, uint32_t *value)
+{
+       int ret;
+       uint32_t tmp_val;
+       char adc_buf[ADC_BUFFER_MAX] = {0, };
+
+       lseek(adc->fd, 0, SEEK_SET);
+       ret = read(adc->fd, &adc_buf, ADC_BUFFER_MAX);
+       CHECK_ERROR(ret <= 0);
+
+       ret = sscanf(adc_buf, "%d", &tmp_val);
+       if (ret == 1) {
+               *value = tmp_val;
+       } else {
+               _E("Error: unable to read adc value \n");
+               return PERIPHERAL_ERROR_IO_ERROR;
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+void peripheral_interface_adc_close(peripheral_adc_h adc)
+{
+       close(adc->fd);
+}
diff --git a/src/peripheral_adc.c b/src/peripheral_adc.c
new file mode 100644 (file)
index 0000000..84baf24
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2018 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 <stdlib.h>
+#include <system_info.h>
+
+#include "peripheral_io.h"
+#include "peripheral_handle.h"
+#include "peripheral_gdbus_adc.h"
+#include "peripheral_interface_adc.h"
+#include "peripheral_log.h"
+
+#define PERIPHERAL_IO_ADC_FEATURE "http://tizen.org/feature/peripheral_io.adc"
+
+#define ADC_FEATURE_UNKNOWN -1
+#define ADC_FEATURE_FALSE    0
+#define ADC_FEATURE_TRUE     1
+
+static int adc_feature = ADC_FEATURE_UNKNOWN;
+
+static bool __is_feature_supported(void)
+{
+       int ret = SYSTEM_INFO_ERROR_NONE;
+       bool feature = false;
+
+       if (adc_feature == ADC_FEATURE_UNKNOWN) {
+               ret = system_info_get_platform_bool(PERIPHERAL_IO_ADC_FEATURE, &feature);
+               RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info");
+
+               adc_feature = (feature ? ADC_FEATURE_TRUE : ADC_FEATURE_FALSE);
+       }
+       return (adc_feature == ADC_FEATURE_TRUE ? true : false);
+}
+
+/**
+ * @brief Initializes adc pin and creates adc handle.
+ */
+int peripheral_adc_open(int device, int channel, peripheral_adc_h *adc)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+       peripheral_adc_h handle;
+
+       RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "ADC feature is not supported");
+       RETVM_IF(adc == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc handle");
+       RETVM_IF(device < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc device number");
+       RETVM_IF(channel < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc channel number");
+
+       handle = (peripheral_adc_h)calloc(1, sizeof(struct _peripheral_adc_s));
+       if (handle == NULL) {
+               _E("Failed to allocate peripheral_adc_h");
+               return PERIPHERAL_ERROR_OUT_OF_MEMORY;
+       }
+
+       ret = peripheral_gdbus_adc_open(handle, device, channel);
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               _E("Failed to open the adc pin, ret : %d", ret);
+               free(handle);
+               handle = NULL;
+               return ret;
+       }
+
+       *adc = handle;
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+/**
+ * @brief Releases the adc handle.
+ */
+int peripheral_adc_close(peripheral_adc_h adc)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "ADC feature is not supported");
+       RETVM_IF(adc == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "adc handle is NULL");
+
+       /* call adc_close */
+       ret = peripheral_gdbus_adc_close(adc);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               _E("Failed to close the adc pin, ret : %d", ret);
+
+       peripheral_interface_adc_close(adc);
+
+       free(adc);
+       adc = NULL;
+
+       return ret;
+}
+
+/**
+ * @brief Reads value of the adc.
+ */
+int peripheral_adc_read(peripheral_adc_h adc, uint32_t *value)
+{
+       RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "ADC feature is not supported");
+       RETVM_IF(adc == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "adc handle is NULL");
+       RETVM_IF(value == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "adc read value is invalid");
+
+       return peripheral_interface_adc_read(adc, value);
+}
diff --git a/test/include/test_peripheral_adc.h b/test/include/test_peripheral_adc.h
new file mode 100644 (file)
index 0000000..acc5b88
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2018 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 __TEST_PERIPHERAL_ADC_H__
+#define __TEST_PERIPHERAL_ADC_H__
+
+int test_peripheral_io_adc_initialize(char *model, bool feature);
+
+int test_peripheral_io_adc_peripheral_adc_open_p(void);
+int test_peripheral_io_adc_peripheral_adc_open_n1(void);
+int test_peripheral_io_adc_peripheral_adc_open_n2(void);
+int test_peripheral_io_adc_peripheral_adc_open_n3(void);
+int test_peripheral_io_adc_peripheral_adc_close_p(void);
+int test_peripheral_io_adc_peripheral_adc_close_n(void);
+int test_peripheral_io_adc_peripheral_adc_read_p(void);
+int test_peripheral_io_adc_peripheral_adc_read_n1(void);
+int test_peripheral_io_adc_peripheral_adc_read_n2(void);
+
+#endif /* __TEST_PERIPHERAL_ADC_H__ */
index 23b118d..d152203 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017-2018 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.
@@ -23,6 +23,7 @@
 #include "test_peripheral_gpio.h"
 #include "test_peripheral_i2c.h"
 #include "test_peripheral_pwm.h"
+#include "test_peripheral_adc.h"
 #include "test_peripheral_uart.h"
 #include "test_peripheral_spi.h"
 
@@ -30,6 +31,7 @@
 #define KEY_FEATURE_PERIPHERAL_IO_GPIO "http://tizen.org/feature/peripheral_io.gpio"
 #define KEY_FEATURE_PERIPHERAL_IO_I2C  "http://tizen.org/feature/peripheral_io.i2c"
 #define KEY_FEATURE_PERIPHERAL_IO_PWM  "http://tizen.org/feature/peripheral_io.pwm"
+#define KEY_FEATURE_PERIPHERAL_IO_ADC  "http://tizen.org/feature/peripheral_io.adc"
 #define KEY_FEATURE_PERIPHERAL_IO_UART "http://tizen.org/feature/peripheral_io.uart"
 #define KEY_FEATURE_PERIPHERAL_IO_SPI  "http://tizen.org/feature/peripheral_io.spi"
 
@@ -101,6 +103,13 @@ static int __test_peripheral_init()
        if (ret != PERIPHERAL_ERROR_NONE)
                goto ERR;
 
+       ret = __get_feature(KEY_FEATURE_PERIPHERAL_IO_ADC, &feature);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               goto ERR;
+       ret = test_peripheral_io_adc_initialize(model_name, feature);
+       if (ret != PERIPHERAL_ERROR_NONE)
+               goto ERR;
+
        ret = __get_feature(KEY_FEATURE_PERIPHERAL_IO_UART, &feature);
        if (ret != PERIPHERAL_ERROR_NONE)
                goto ERR;
@@ -278,6 +287,30 @@ static void __test_peripheral_pwm_run()
        __error_check(ret, "test_peripheral_io_pwm_peripheral_pwm_set_enabled_n");
 }
 
+static void __test_peripheral_adc_run()
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       ret = test_peripheral_io_adc_peripheral_adc_open_p();
+       __error_check(ret, "test_peripheral_io_adc_peripheral_adc_open_p");
+       ret = test_peripheral_io_adc_peripheral_adc_open_n1();
+       __error_check(ret, "test_peripheral_io_adc_peripheral_adc_open_n1");
+       ret = test_peripheral_io_adc_peripheral_adc_open_n2();
+       __error_check(ret, "test_peripheral_io_adc_peripheral_adc_open_n2");
+       ret = test_peripheral_io_adc_peripheral_adc_open_n3();
+       __error_check(ret, "test_peripheral_io_adc_peripheral_adc_open_n3");
+       ret = test_peripheral_io_adc_peripheral_adc_close_p();
+       __error_check(ret, "test_peripheral_io_adc_peripheral_adc_close_p");
+       ret = test_peripheral_io_adc_peripheral_adc_close_n();
+       __error_check(ret, "test_peripheral_io_adc_peripheral_adc_close_n");
+       ret = test_peripheral_io_adc_peripheral_adc_read_p();
+       __error_check(ret, "test_peripheral_io_adc_peripheral_adc_read_p");
+       ret = test_peripheral_io_adc_peripheral_adc_read_n1();
+       __error_check(ret, "test_peripheral_io_adc_peripheral_adc_read_n1");
+       ret = test_peripheral_io_adc_peripheral_adc_read_n2();
+       __error_check(ret, "test_peripheral_io_adc_peripheral_adc_read_n2");
+}
+
 static void __test_peripheral_uart_run()
 {
        int ret = PERIPHERAL_ERROR_NONE;
@@ -484,11 +517,11 @@ int main(int argc, char **argv)
 
        while (1) {
 
-               printf("\n\n******************************************************************\n");
-               printf("*                                                                *\n");
-               printf("*    MENU: 1.All  2.GPIO  3.I2C  4.PWM  5.UART  6.SPI  7.Exit    *\n");
-               printf("*                                                                *\n");
-               printf("******************************************************************\n");
+               printf("\n\n*************************************************************************\n");
+               printf("*                                                                       *\n");
+               printf("*    MENU: 1.All  2.GPIO  3.I2C  4.PWM  5.ADC  6.UART  7.SPI  8.Exit    *\n");
+               printf("*                                                                       *\n");
+               printf("*************************************************************************\n");
                printf("     Input Menu : ");
 
                if (scanf("%d", &menu) < 0) return -1;
@@ -509,6 +542,7 @@ int main(int argc, char **argv)
                        __test_peripheral_gpio_run();
                        __test_peripheral_i2c_run();
                        __test_peripheral_pwm_run();
+                       __test_peripheral_adc_run();
                        __test_peripheral_uart_run();
                        break;
                case 2:
@@ -521,12 +555,15 @@ int main(int argc, char **argv)
                        __test_peripheral_pwm_run();
                        break;
                case 5:
-                       __test_peripheral_uart_run();
+                       __test_peripheral_adc_run();
                        break;
                case 6:
-                       __test_peripheral_spi_run();
+                       __test_peripheral_uart_run();
                        break;
                case 7:
+                       __test_peripheral_spi_run();
+                       break;
+               case 8:
                        printf("[Message] Close the Peripheral-IO API local Test...\n\n");
                        return 0;
                default:
diff --git a/test/src/test_peripheral_adc.c b/test/src/test_peripheral_adc.c
new file mode 100644 (file)
index 0000000..20595c0
--- /dev/null
@@ -0,0 +1,251 @@
+/*
+ * Copyright (c) 2018 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 <string.h>
+#include <peripheral_io.h>
+#include "test_peripheral_adc.h"
+
+#define ADC_DEVICE_ARTIK530 0
+#define ADC_DEVICE_INVALID -99
+#define ADC_CHANNEL 3
+#define ADC_CHANNEL_INVALID -99
+
+static bool g_feature = true;
+static int device;
+static int channel;
+
+int test_peripheral_io_adc_initialize(char *model, bool feature)
+{
+       g_feature = feature;
+
+       if (!strcmp(model, "artik"))
+               device = ADC_DEVICE_ARTIK530;
+       else
+               return PERIPHERAL_ERROR_NO_DEVICE;
+
+       channel = ADC_CHANNEL;
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+int test_peripheral_io_adc_peripheral_adc_open_p(void)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       peripheral_adc_h adc_h = NULL;
+
+       if (g_feature == false) {
+               ret = peripheral_adc_open(device, channel, &adc_h);
+               if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED)
+                       return ret;
+
+       } else {
+               ret = peripheral_adc_open(device, channel, &adc_h);
+               if (ret != PERIPHERAL_ERROR_NONE)
+                       return ret;
+
+               ret = peripheral_adc_close(adc_h);
+               if (ret != PERIPHERAL_ERROR_NONE)
+                       return ret;
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+int test_peripheral_io_adc_peripheral_adc_open_n1(void)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       if (g_feature == false) {
+               ret = peripheral_adc_open(device, channel, NULL);
+               if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED)
+                       return ret;
+
+       } else {
+               ret = peripheral_adc_open(device, channel, NULL);
+               if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER)
+                       return ret;
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+int test_peripheral_io_adc_peripheral_adc_open_n2(void)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       peripheral_adc_h adc_h = NULL;
+
+       if (g_feature == false) {
+               ret = peripheral_adc_open(ADC_DEVICE_INVALID, channel, &adc_h);
+               if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED)
+                       return ret;
+
+       } else {
+               ret = peripheral_adc_open(ADC_DEVICE_INVALID, channel, &adc_h);
+               if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER)
+                       return ret;
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+int test_peripheral_io_adc_peripheral_adc_open_n3(void)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       peripheral_adc_h adc_h = NULL;
+
+       if (g_feature == false) {
+               ret = peripheral_adc_open(device, ADC_CHANNEL_INVALID, &adc_h);
+               if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED)
+                       return ret;
+
+       } else {
+               ret = peripheral_adc_open(device, ADC_CHANNEL_INVALID, &adc_h);
+               if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER)
+                       return ret;
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+int test_peripheral_io_adc_peripheral_adc_close_p(void)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       peripheral_adc_h adc_h = NULL;
+
+       if (g_feature == false) {
+               ret = peripheral_adc_close(adc_h);
+               if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED)
+                       return ret;
+
+       } else {
+               ret = peripheral_adc_open(device, channel, &adc_h);
+               if (ret != PERIPHERAL_ERROR_NONE)
+                       return ret;
+
+               ret = peripheral_adc_close(adc_h);
+               if (ret != PERIPHERAL_ERROR_NONE)
+                       return ret;
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+int test_peripheral_io_adc_peripheral_adc_close_n(void)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       if (g_feature == false) {
+               ret = peripheral_adc_close(NULL);
+               if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED)
+                       return ret;
+
+       } else {
+               ret = peripheral_adc_close(NULL);
+               if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER)
+                       return ret;
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+int test_peripheral_io_adc_peripheral_adc_read_p(void)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       peripheral_adc_h adc_h = NULL;
+
+       uint32_t value;
+
+       if (g_feature == false) {
+               ret = peripheral_adc_read(adc_h, &value);
+               if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED)
+                       return ret;
+
+       } else {
+               ret = peripheral_adc_open(device, channel, &adc_h);
+               if (ret != PERIPHERAL_ERROR_NONE)
+                       return ret;
+
+               ret = peripheral_adc_read(adc_h, &value);
+               if (ret != PERIPHERAL_ERROR_NONE) {
+                       peripheral_adc_close(adc_h);
+                       return ret;
+               }
+
+               ret = peripheral_adc_close(adc_h);
+               if (ret != PERIPHERAL_ERROR_NONE)
+                       return ret;
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+int test_peripheral_io_adc_peripheral_adc_read_n1(void)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       uint32_t value;
+
+       if (g_feature == false) {
+               ret = peripheral_adc_read(NULL, &value);
+               if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED)
+                       return ret;
+
+       } else {
+               ret = peripheral_adc_read(NULL, &value);
+               if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER)
+                       return ret;
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+int test_peripheral_io_adc_peripheral_adc_read_n2(void)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       peripheral_adc_h adc_h = NULL;
+
+       uint32_t value;
+
+       if (g_feature == false) {
+               ret = peripheral_adc_read(adc_h, &value);
+               if (ret != PERIPHERAL_ERROR_NOT_SUPPORTED)
+                       return ret;
+
+       } else {
+               ret = peripheral_adc_open(device, channel, &adc_h);
+               if (ret != PERIPHERAL_ERROR_NONE)
+                       return ret;
+
+               ret = peripheral_adc_read(adc_h, &value);
+               if (ret != PERIPHERAL_ERROR_INVALID_PARAMETER) {
+                       peripheral_adc_close(adc_h);
+                       return ret;
+               }
+
+               ret = peripheral_adc_close(adc_h);
+               if (ret != PERIPHERAL_ERROR_NONE)
+                       return ret;
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}