Import source codes from iotbus 16/125416/2
authorHyeongsik Min <hyeongsik.min@samsung.com>
Mon, 17 Apr 2017 07:41:13 +0000 (16:41 +0900)
committerHyeongsik Min <hyeongsik.min@samsung.com>
Mon, 17 Apr 2017 09:59:11 +0000 (18:59 +0900)
- Import client API source codes from below
  https://github.com/tizen-artik/iotbus

- Rename the pacakge to peripheral-io
- Use 'system' bus as dbus bus type
- Fix coding style
- Fix Svace issues

Change-Id: If1e90777d57f05411bb995fbd0ba3b4d09ff5c7f
Signed-off-by: Hyeongsik Min <hyeongsik.min@samsung.com>
19 files changed:
CMakeLists.txt [new file with mode: 0644]
LICENSE.APLv2 [new file with mode: 0644]
NOTICE [new file with mode: 0644]
capi-system-peripheral-io.pc.in [new file with mode: 0644]
include/peripheral_common.h [new file with mode: 0644]
include/peripheral_dbus.h [new file with mode: 0644]
include/peripheral_internal.h [new file with mode: 0644]
include/peripheral_io.h [new file with mode: 0644]
packaging/capi-system-peripheral-io.manifest [new file with mode: 0644]
packaging/capi-system-peripheral-io.spec [new file with mode: 0644]
src/peripheral_adc.c [new file with mode: 0644]
src/peripheral_dbus.c [new file with mode: 0644]
src/peripheral_gpio.c [new file with mode: 0644]
src/peripheral_i2c.c [new file with mode: 0644]
src/peripheral_pwm.c [new file with mode: 0644]
src/peripheral_spi.c [new file with mode: 0644]
src/peripheral_uart.c [new file with mode: 0644]
test/CMakeLists.txt [new file with mode: 0644]
test/peripheral-io-test.c [new file with mode: 0644]

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..a31234b
--- /dev/null
@@ -0,0 +1,73 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+SET(project_prefix "capi")
+SET(prefix "/usr")
+SET(libdir ${LIB_INSTALL_DIR})
+SET(version "0.0.1")
+SET(service "system")
+SET(submodule "peripheral-io")
+SET(fw_name "${project_prefix}-${service}-${submodule}")
+
+PROJECT(${fw_name})
+
+SET(dependents "dlog glib-2.0 gio-2.0 capi-base-common")
+SET(pc_dependents "capi-base-common")
+
+SET(CMAKE_INSTALL_PREFIX ${prefix})
+SET(PREFIX $(CMAKE_INSTALL_PREFIX))
+SET(VERSION ${version})
+
+SET(INC_DIR include)
+INCLUDE_DIRECTORIES(${INC_DIR})
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(${fw_name} REQUIRED ${dependents})
+FOREACH(flag ${${fw_name}_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"")
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall -Werror -fPIC")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
+
+SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=%{_libdir}")
+
+SET(SOURCES src/peripheral_adc.c
+                       src/peripheral_gpio.c
+                       src/peripheral_pwm.c
+                       src/peripheral_uart.c
+                       src/peripheral_dbus.c
+                       src/peripheral_i2c.c
+                       src/peripheral_spi.c)
+
+ADD_LIBRARY(${fw_name} SHARED ${SOURCES})
+
+TARGET_LINK_LIBRARIES(${fw_name} ${${fw_name}_LDFLAGS})
+
+SET_TARGET_PROPERTIES(${fw_name}
+        PROPERTIES
+        VERSION ${FULLVER}
+        SOVERSION ${MAJORVER}
+        CLEAN_DIRECT_OUTPUT 1
+)
+
+INSTALL(TARGETS ${fw_name} DESTINATION ${libdir})
+INSTALL(
+               DIRECTORY ${INC_DIR}/ DESTINATION include
+               FILES_MATCHING
+               PATTERN "${INC_DIR}/*.h"
+               )
+
+SET(PC_NAME ${fw_name})
+SET(PC_REQUIRED ${pc_dependents})
+SET(PC_LDFLAGS -l${fw_name})
+
+CONFIGURE_FILE(
+       ${PROJECT_NAME}.pc.in
+       ${PROJECT_NAME}.pc
+       @ONLY
+)
+INSTALL(FILES ${PROJECT_NAME}.pc DESTINATION ${libdir}/pkgconfig)
+
+ADD_SUBDIRECTORY(test)
\ No newline at end of file
diff --git a/LICENSE.APLv2 b/LICENSE.APLv2
new file mode 100644 (file)
index 0000000..6b524fa
--- /dev/null
@@ -0,0 +1,204 @@
+Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   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.
+
diff --git a/NOTICE b/NOTICE
new file mode 100644 (file)
index 0000000..eff0219
--- /dev/null
+++ b/NOTICE
@@ -0,0 +1,3 @@
+Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+Except as noted, this software is licensed under Apache License, Version 2.
+Please, see the LICENSE.APLv2 file for Apache license, version 2 terms and conditions.
diff --git a/capi-system-peripheral-io.pc.in b/capi-system-peripheral-io.pc.in
new file mode 100644 (file)
index 0000000..1dd9b2c
--- /dev/null
@@ -0,0 +1,13 @@
+# Package Information for pkg-config
+
+prefix=@PREFIX@
+exec_prefix=/usr
+libdir=@LIB_INSTALL_DIR@
+includedir=/usr/include/
+
+Name: @PC_NAME@
+Description: @PACKAGE_DESCRIPTION@
+Version: @VERSION@
+Requires: @PC_REQUIRED@
+Libs: -L${libdir} @PC_LDFLAGS@
+Cflags: -I${includedir}
diff --git a/include/peripheral_common.h b/include/peripheral_common.h
new file mode 100644 (file)
index 0000000..1e5d88f
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2016-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_COMMON_H__
+#define __PERIPHERAL_COMMON_H__
+
+#include <unistd.h>
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "PERIPHERAL-IO"
+
+#define _E(fmt, arg...) LOGE(fmt, ##arg)
+#define _D(fmt, arg...) LOGD(fmt, ##arg)
+#define _W(fmt, arg...) LOGW(fmt, ##arg)
+
+#endif /* __PERIPHERAL_COMMON_H__ */
diff --git a/include/peripheral_dbus.h b/include/peripheral_dbus.h
new file mode 100644 (file)
index 0000000..c32b2cb
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2016-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_DBUS_H_
+#define __PERIPHERAL_DBUS_H_
+
+#include <gio/gio.h>
+
+#define PERIPHERAL_DBUS_INTERFACE      "org.tizen.system.peripheral_io"
+#define PERIPHERAL_DBUS_PATH           "/Org/Tizen/System/Peripheral_io"
+#define PERIPHERAL_DBUS_NAME           "org.tizen.system.peripheral_io"
+
+#define PERIPHERAL_METHOD_GPIO "gpio"
+#define PERIPHERAL_METHOD_I2C  "i2c"
+#define PERIPHERAL_METHOD_PWM  "pwm"
+#define PERIPHERAL_METHOD_SPI  "spi"
+#define PERIPHERAL_METHOD_UART "uart"
+
+int set_dbus_connection(void);
+void unset_dbus_connection(void);
+GDBusConnection *get_dbus_connection(void);
+
+
+int peripheral_dbus_gpio(peripheral_gpio_h gpio, char * sensorid, char *funcname, int write_value, int *read_value);
+int peripheral_dbus_i2c(peripheral_i2c_context_h dev, char * sensorid, char *funcname, int value, unsigned char *data, int addr);
+int peripheral_dbus_pwm(peripheral_pwm_context_h dev, char * sensorid, char *funcname);
+
+#endif /* __PERIPHERAL_DBUS_H_ */
diff --git a/include/peripheral_internal.h b/include/peripheral_internal.h
new file mode 100644 (file)
index 0000000..8ab8cc5
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * 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_INTERNAL_H__
+#define __PERIPHERAL_INTERNAL_H__
+
+/**
+ * @brief Internal struct for keep gpio context information
+ */
+struct _peripheral_gpio_s {
+       int pin;
+       peripheral_gpio_direction_e direction;
+       peripheral_gpio_edge_e edge;
+};
+
+#endif /* __PERIPHERAL_INTERNAL_H__ */
diff --git a/include/peripheral_io.h b/include/peripheral_io.h
new file mode 100644 (file)
index 0000000..d6dcd30
--- /dev/null
@@ -0,0 +1,458 @@
+/*
+ * Copyright (c) 2016-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_IO_H__
+#define __PERIPHERAL_IO_H__
+
+#include <stdint.h>
+#include <tizen.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @file peripheral_io.h
+ * @brief This file contains the peripheral-io API
+ */
+
+/**
+ * @brief Enumeration for peripheral-io error.
+ * @since_tizen
+ */
+typedef enum {
+       PERIPHERAL_ERROR_NONE                = TIZEN_ERROR_NONE,                /**< Successful */
+       PERIPHERAL_ERROR_IO_ERROR            = TIZEN_ERROR_IO_ERROR,            /**< I/O error */
+       PERIPHERAL_ERROR_OUT_OF_MEMORY       = TIZEN_ERROR_OUT_OF_MEMORY,       /**< Out of memory */
+       PERIPHERAL_ERROR_PERMISSON_DENIED    = TIZEN_ERROR_PERMISSION_DENIED,   /**< Permission denied */
+       PERIPHERAL_ERROR_RESOURCE_BUSY       = TIZEN_ERROR_RESOURCE_BUSY,       /**< Device or resource busy */
+       PERIPHERAL_ERROR_INVALID_PARAMETER   = TIZEN_ERROR_INVALID_PARAMETER,   /**< Invalid parameter */
+       PERIPHERAL_ERROR_NO_DATA             = TIZEN_ERROR_NO_DATA,             /**< No data available */
+       PERIPHERAL_ERROR_INVALID_OPERATION   = TIZEN_ERROR_INVALID_OPERATION,   /**< Function not implemented */
+       PERIPHERAL_ERROR_TIMED_OUT           = TIZEN_ERROR_TIMED_OUT,           /**< Time out */
+       PERIPHERAL_ERROR_NOT_SUPPORTED       = TIZEN_ERROR_NOT_SUPPORTED,       /**< Not supported */
+       PERIPHERAL_ERROR_UNKNOWN             = TIZEN_ERROR_UNKNOWN,             /**< Unknown error */
+       PERIPHERAL_ERROR_NO_DEVICE           = -ENODEV,                         /**< No such device */
+} peripheral_error_e;
+
+/**
+ * @addtogroup CAPI_SYSTEM_PERPHERAL_GPIO_MODULE
+ * @{
+ */
+
+/**
+ * @brief Enumeration of gpio direction
+ */
+typedef enum {
+       PERIPHERAL_GPIO_DIRECTION_IN = 0,    /**< Input Mode */
+       PERIPHERAL_GPIO_DIRECTION_OUT,       /**< Output mode and this implies "low" output value */
+       PERIPHERAL_GPIO_DIRECTION_OUT_HIGH,  /**< Output mode and value also be written as "high" */
+} peripheral_gpio_direction_e;
+
+/**
+ * @brief Enumeration of edge type for gpio interrupt
+ */
+typedef enum {
+       PERIPHERAL_GPIO_EDGE_NONE = 0,  /**< No interrupt on Gpio */
+       PERIPHERAL_GPIO_EDGE_RISING,    /**< Interrupt on rising only */
+       PERIPHERAL_GPIO_EDGE_FALLING,   /**< Interrupt on falling only */
+       PERIPHERAL_GPIO_EDGE_BOTH,      /**< Interrupt on rising & falling */
+} peripheral_gpio_edge_e;
+
+/**
+ * @brief The handle to the gpio pin
+ * @since_tizen 4.0
+ */
+typedef struct _peripheral_gpio_s* peripheral_gpio_h;
+
+/**
+ * @brief Called when the gpio interrupt is triggered.
+ * @since_tizen 4.0
+ *
+ * @param[in] user_data The user data passed from the callback registration function
+ *
+ * @see peripheral_gpio_register_cb()
+ * @see peripheral_gpio_unregister_cb()
+ */
+typedef void(*gpio_isr_cb)(void *user_data);
+
+/**
+ * @brief Initilizes(export) gpio pin and creates gpio handle.
+ * @since_tizen 4.0
+ *
+ * @param[in] gpio_pin The gpio pin number what you want to use
+ * @param[out] gpio The gpio 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_OUT_OF_MEMORY Memory allocation failed
+ * @retval #PERIPHERAL_ERROR_PERMISSON_DENIED Permission denied
+ * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error
+ * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed
+ *
+ * @see peripheral_gpio_close()
+ */
+int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio);
+
+/**
+ * @brief Release the gpio handle and finalize(unexport) the gpio pin.
+ * @since_tizen 4.0
+ *
+ * @param[in] gpio The handle to the gpio pin to release
+ *
+ * @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
+ *
+ * @see peripheral_gpio_open()
+ */
+int peripheral_gpio_close(peripheral_gpio_h gpio);
+
+/**
+ * @brief Sets direction of the gpio pin.
+ * @since_tizen 4.0
+ *
+ * @param[in] gpio The handle to the gpio pin to set
+ * @param[in] direction The direction type of the gpio pin
+ *
+ * @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
+ */
+int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction);
+
+/**
+ * @brief Sets the edge mode of the gpio pin.
+ * @since_tizen 4.0
+ *
+ * @param[in] gpio The handle to the gpio pin to set
+ * @param[in] edge The edge type of the gpio pin
+ *
+ * @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
+ */
+int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge);
+
+/**
+ * @brief Registers a callback function to be invoked when the gpio interrupt is triggered.
+ * @since_tizen 4.0
+ *
+ * @param[in] gpio The handle to the gpio pin to set
+ * @param[in] edge The edge type of the gpio pin
+ * @param[in] callback The callback function to register
+ * @param[in] user_data The user data to be passed to the callback function
+ *
+ * @return 0 on success, otherwise a negative error value
+ * @retval #PERIPHERAL_ERROR_NONE Successfu
+ * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parametera
+ *
+ * @see peripheral_gpio_set_edge_mode()
+ * @see peripheral_gpio_unregister_cb()
+ */
+int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data);
+
+/**
+ * @brief Unregisters the callback function for the gpio handler.
+ * @since_tizen 4.0
+ *
+ * @param[in] gpio The handle to the gpio pin
+ *
+ * @return 0 on success, otherwise a negative error value
+ * @retval #PERIPHERAL_ERROR_NONE Successful
+ * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @see peripheral_gpio_register_cb()
+ */
+int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio);
+
+/**
+ * @brief Reads the gpio value.
+ * @since_tizen 4.0
+ *
+ * @param[in] gpio The handle to the gpio pin
+ * @param[out] value The result of the gpio
+ *
+ * @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
+ */
+int peripheral_gpio_read(peripheral_gpio_h gpio, int *value);
+
+/**
+ * @brief Writes the gpio value.
+ * @since_tizen 4.0
+ *
+ * @param[in] gpio The handle to the gpio pin
+ * @param[in] value The value to be written to the gpio
+ *
+ * @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
+ */
+int peripheral_gpio_write(peripheral_gpio_h gpio, int value);
+
+/**
+ * @brief Gets direction of the gpio.
+ * @since_tizen 4.0
+ *
+ * @param[in] gpio The handle to the gpio pin
+ * @param[out] value The value to be written to the gpio
+ *
+ * @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
+ */
+int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction);
+
+/**
+ * @brief Gets pin number of the gpio.
+ * @since_tizen 4.0
+ *
+ * @param[in] gpio The handle to the gpio pin
+ * @param[out] gpio_pin The number of the gpio
+ *
+ * @return 0 on success, otherwise a negative error value
+ * @retval #PERIPHERAL_ERROR_NONE Successful
+ * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter
+ */
+int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin);
+
+/**
+ * @brief Gets edge mode of the gpio.
+ * @since_tizen 4.0
+ *
+ * @param[in] gpio The handle to the gpio pin
+ * @param[out] gpio_pin The number of the gpio
+ *
+ * @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
+ */
+int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge);
+
+/**
+* @}
+*/
+
+/**
+ * @addtogroup CAPI_SYSTEM_PERPHERAL_I2C_MODULE
+ * @{
+ */
+
+/**
+ * @brief Struct for peripheral_gpio_s
+ */
+struct _peripheral_i2c_s {
+       int fd;
+};
+typedef struct _peripheral_i2c_s *peripheral_i2c_context_h;
+
+typedef enum {
+       PERIPHERAL_I2C_STD = 0,
+       PERIPHERAL_I2C_FAST = 1,
+       PERIPHERAL_I2C_HIGH = 2
+} peripheral_i2c_mode_e;
+
+peripheral_i2c_context_h peripheral_i2c_init(int bus);
+
+int peripheral_i2c_stop(peripheral_i2c_context_h hnd);
+
+int peripheral_i2c_set_frequency(peripheral_i2c_context_h hnd, peripheral_i2c_mode_e mode);
+
+int peripheral_i2c_set_address(peripheral_i2c_context_h hnd, int address);
+
+int peripheral_i2c_read(peripheral_i2c_context_h hnd, uint8_t *data, int length);
+
+int peripheral_i2c_write(peripheral_i2c_context_h hnd, uint8_t *data, int length);
+
+
+/**
+* @}
+*/
+
+/**
+ * @addtogroup CAPI_SYSTEM_PERPHERAL_PWM_MODULE
+ * @{
+ */
+
+struct _peripheral_pwm_s {
+       int device;
+       int channel;
+       int period;
+       int duty_cycle;
+       int enabled;
+};
+typedef struct _peripheral_pwm_s *peripheral_pwm_context_h;
+
+typedef enum {
+       PERIPHERAL_PWM_DISABLE = 0,
+       PERIPHERAL_PWM_ENABLE,
+} peripheral_pwm_state_e;
+
+peripheral_pwm_context_h peripheral_pwm_open(int device, int channel);
+
+int peripheral_pwm_close(peripheral_pwm_context_h pwm);
+
+int peripheral_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle);
+
+int peripheral_pwm_set_period(peripheral_pwm_context_h pwm, int period);
+
+int peripheral_pwm_set_enabled(peripheral_pwm_context_h pwm, peripheral_pwm_state_e enable);
+
+int peripheral_pwm_is_enabled(peripheral_pwm_context_h pwm);
+
+int peripheral_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle);
+
+int peripheral_pwm_get_period(peripheral_pwm_context_h pwm, int *period);
+
+
+/**
+* @}
+*/
+
+/**
+ * @addtogroup CAPI_SYSTEM_PERPHERAL_ADC_MODULE
+ * @{
+ */
+
+/**
+ * @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
+ */
+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);
+
+/**
+* @}
+*/
+
+/**
+ * @addtogroup CAPI_SYSTEM_PERPHERAL_UART_MODULE
+ * @{
+ */
+struct _peripheral_uart_s {
+       int fd;
+};
+
+
+typedef struct _peripheral_uart_s* peripheral_uart_context_h;
+
+typedef enum {
+       PERIPHERAL_UART_PARITY_NONE = 0,
+       PERIPHERAL_UART_PARITY_EVEN,
+       PERIPHERAL_UART_PARITY_ODD,
+} peripheral_uart_parity_e;
+
+peripheral_uart_context_h peripheral_uart_init(const char *path);
+
+int peripheral_uart_stop(peripheral_uart_context_h hnd);
+
+int peripheral_uart_flush(peripheral_uart_context_h hnd);
+
+int peripheral_uart_set_baudrate(peripheral_uart_context_h hnd, unsigned int baud);
+
+int peripheral_uart_set_mode(peripheral_uart_context_h hnd, int bytesize, peripheral_uart_parity_e parity, int stopbits);
+
+int peripheral_uart_set_flowcontrol(peripheral_uart_context_h hnd, int xonxoff, int rtscts);
+
+int peripheral_uart_read(peripheral_uart_context_h hnd, char *buf, unsigned int length);
+
+int peripheral_uart_write(peripheral_uart_context_h hnd, const char *buf, unsigned int length);
+
+/**
+* @}
+*/
+
+/**
+ * @addtogroup CAPI_SYSTEM_PERPHERAL_SPI_MODULE
+ * @{
+ */
+
+typedef enum {
+       PERIPHERAL_SPI_MODE0 = 0,
+       PERIPHERAL_SPI_MODE1,
+       PERIPHERAL_SPI_MODE2,
+       PERIPHERAL_SPI_MODE3
+} peripheral_spi_mode_e;
+
+struct peripheral_spi_config_s {
+       int fd;
+       char bits_per_word;
+       int lsb;
+       unsigned int chip_select;
+       unsigned int frequency;
+       peripheral_spi_mode_e mode;
+};
+
+typedef struct peripheral_spi_config_s * peripheral_spi_context_h;
+
+peripheral_spi_context_h peripheral_spi_open(unsigned int bus, peripheral_spi_context_h config);
+
+int    peripheral_spi_write(peripheral_spi_context_h hnd, char *txbuf, int length);
+
+int    peripheral_spi_recv(peripheral_spi_context_h hnd, char *rxbuf, int length);
+
+int peripheral_spi_transfer_buf(peripheral_spi_context_h hnd, char *txbuf, char *rxbuf, int length);
+
+int    peripheral_spi_close(peripheral_spi_context_h hnd);
+
+/**
+* @}
+*/
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __PERIPHERAL_IO_H__ */
diff --git a/packaging/capi-system-peripheral-io.manifest b/packaging/capi-system-peripheral-io.manifest
new file mode 100644 (file)
index 0000000..a76fdba
--- /dev/null
@@ -0,0 +1,5 @@
+<manifest>
+       <request>
+               <domain name="_" />
+       </request>
+</manifest>
diff --git a/packaging/capi-system-peripheral-io.spec b/packaging/capi-system-peripheral-io.spec
new file mode 100644 (file)
index 0000000..3aa5b57
--- /dev/null
@@ -0,0 +1,56 @@
+Name:       capi-system-peripheral-io
+Summary:    Tizen Peripheral Input & Output library
+Version:    0.0.1
+Release:    0
+Group:      System & System Tools
+License:    Apache-2.0
+Source0:    %{name}-%{version}.tar.gz
+Source1:    %{name}.manifest
+BuildRequires:  cmake
+BuildRequires:  pkgconfig(glib-2.0)
+BuildRequires:  pkgconfig(gio-2.0)
+BuildRequires:  pkgconfig(dlog)
+BuildRequires:  pkgconfig(capi-base-common)
+
+Requires(post): /sbin/ldconfig
+Requires(postun): /sbin/ldconfig
+
+%description
+Tizen Peripheral Input & Output library
+
+%package devel
+Summary:    Tizen Peripheral Input & Output library (devel)
+Group:      System & System Tools/Development
+Requires:   %{name} = %{version}-%{release}
+
+%description devel
+Tizen Peripheral Input & Output library (devel)
+
+%prep
+%setup -q
+cp %{SOURCE1} ./%{name}.manifest
+
+%build
+MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
+%cmake . -DMAJORVER=${MAJORVER} -DFULLVER=%{version}
+
+%install
+%make_install
+
+%post -p /sbin/ldconfig
+
+%postun -p /sbin/ldconfig
+
+%files
+%manifest %{name}.manifest
+%defattr(-,root,root,-)
+%{_libdir}/lib%{name}.so*
+%{_bindir}/peripheral-io-test
+%license LICENSE.APLv2
+
+%files devel
+%manifest %{name}.manifest
+%defattr(-,root,root,-)
+%{_includedir}/*.h
+%{_libdir}/lib%{name}.so
+%{_libdir}/pkgconfig/%{name}.pc
diff --git a/src/peripheral_adc.c b/src/peripheral_adc.c
new file mode 100644 (file)
index 0000000..1aa2973
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2016-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 "peripheral_io.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)
+{
+       if (dev == NULL)
+               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+       if (data == NULL)
+               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+
+       return PERIPHERAL_ERROR_INVALID_OPERATION;
+}
+
+int peripheral_adc_close(peripheral_adc_context_h dev)
+{
+       if (dev != NULL)
+               free(dev);
+
+       dev = NULL;
+
+       return PERIPHERAL_ERROR_INVALID_OPERATION;
+}
diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c
new file mode 100644 (file)
index 0000000..83eb624
--- /dev/null
@@ -0,0 +1,187 @@
+/*
+ * Copyright (c) 2016-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 <gio/gio.h>
+
+#include "peripheral_io.h"
+#include "peripheral_dbus.h"
+#include "peripheral_common.h"
+#include "peripheral_internal.h"
+
+GDBusConnection *connection = NULL;
+
+int set_dbus_connection(void)
+{
+       GError *error = NULL;
+
+       if (connection)
+               return PERIPHERAL_ERROR_NONE;
+
+       connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+       if (error) {
+               _E("gdbus error occurred (%s)", error->message);
+               g_error_free(error);
+               return PERIPHERAL_ERROR_UNKNOWN;
+       }
+
+       if (!connection) {
+               _E("Failed to get gdbus connection ");
+               return PERIPHERAL_ERROR_UNKNOWN;
+       } else {
+               //Sets whether the process should be terminated when connection is closed by the remote peer
+               g_dbus_connection_set_exit_on_close(connection, FALSE); //FALSE shareable connection is NOT closed by the remote peer
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+void unset_dbus_connection(void)
+{
+       if (connection) {
+               g_object_unref(connection);
+               connection = NULL;
+       }
+}
+
+GDBusConnection *get_dbus_connection(void)
+{
+       return connection;
+}
+
+int peripheral_dbus_gpio(peripheral_gpio_h gpio, char * sensorid, char *funcname, int write_value, int *read_value)
+{
+       GError *error = NULL;
+
+       GVariant *ret_value = NULL;
+       gint32 read = 0;
+       gint32 ret = PERIPHERAL_ERROR_NONE;
+
+       error = NULL;
+
+       ret_value = g_dbus_connection_call_sync(connection,
+                                                                               PERIPHERAL_DBUS_NAME,
+                                                                               PERIPHERAL_DBUS_PATH,
+                                                                               PERIPHERAL_DBUS_INTERFACE,
+                                                                               sensorid,
+                                                                               g_variant_new("(siiii)", funcname, gpio->pin, gpio->direction, gpio->edge, write_value),
+                                                                               G_VARIANT_TYPE("(iiiii)"),
+                                                                               G_DBUS_CALL_FLAGS_NONE,
+                                                                               -1,
+                                                                               NULL,
+                                                                               &error);
+
+       if (ret_value == NULL) {
+               g_printerr("Error invoking %s () : %s\n", sensorid, error->message);
+               g_error_free(error);
+               return PERIPHERAL_ERROR_UNKNOWN;
+       }
+
+       g_variant_get(ret_value, "(iiiii)", &gpio->pin, &gpio->direction, &gpio->edge, &read, &ret);
+       g_variant_unref(ret_value);
+
+       if (read_value != 0)
+               (*read_value) = read;
+
+       return ret;
+
+}
+
+int peripheral_dbus_i2c(peripheral_i2c_context_h dev, char * sensorid, char *funcname, int value, unsigned char * data, int addr)
+{
+       GError *error = NULL;
+       GVariant *ret_value = NULL;
+       gint32 ret = PERIPHERAL_ERROR_NONE;
+       GVariantBuilder *builder;
+       guint8 str;
+       GVariantIter *ret_data;
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
+       if (data == NULL) {
+               g_variant_builder_add(builder, "y", 0x10);
+               g_variant_builder_add(builder, "y", 0x00);
+       } else {
+               int i = 0;
+               for (i = 0; i < value; i++)
+                       g_variant_builder_add(builder, "y", data[i]);
+               g_variant_builder_add(builder, "y", 0x00);
+       }
+
+       ret_value = g_dbus_connection_call_sync(connection,
+                                                                               PERIPHERAL_DBUS_NAME,
+                                                                               PERIPHERAL_DBUS_PATH,
+                                                                               PERIPHERAL_DBUS_INTERFACE,
+                                                                               sensorid,
+                                                                               g_variant_new("(siiayi)", funcname, value, dev->fd, builder, addr),
+                                                                               G_VARIANT_TYPE("(iayi)"),
+                                                                               G_DBUS_CALL_FLAGS_NONE,
+                                                                               -1,
+                                                                               NULL,
+                                                                               &error);
+
+       g_variant_builder_unref(builder);
+
+       if (ret_value == NULL) {
+               g_printerr("Error invoking %s () : %s\n", sensorid, error->message);
+               g_error_free(error);
+               return PERIPHERAL_ERROR_UNKNOWN;
+       }
+
+       g_variant_get(ret_value, "(iayi)", &(dev->fd), &ret_data, &ret);
+       g_variant_unref(ret_value);
+
+       if (data != NULL) {
+               int i = 0;
+               while (g_variant_iter_loop(ret_data, "y", &str)) {
+                       data[i] = str;
+                       i++;
+                       if (i == value)
+                               break;
+               }
+       }
+
+       return ret;
+}
+
+int peripheral_dbus_pwm(peripheral_pwm_context_h dev, char * sensorid, char *funcname)
+{
+       GError *error = NULL;
+       GVariant *ret_value = NULL;
+       gint32 ret = PERIPHERAL_ERROR_NONE;
+
+       ret_value = g_dbus_connection_call_sync(connection,
+                                                                               PERIPHERAL_DBUS_NAME,
+                                                                               PERIPHERAL_DBUS_PATH,
+                                                                               PERIPHERAL_DBUS_INTERFACE,
+                                                                               sensorid,
+                                                                               g_variant_new("(siiiii)", funcname, dev->device, dev->channel, dev->period, dev->duty_cycle, dev->enabled),
+                                                                               G_VARIANT_TYPE("(iii)"),
+                                                                               G_DBUS_CALL_FLAGS_NONE,
+                                                                               -1,
+                                                                               NULL,
+                                                                               &error);
+       if (ret_value == NULL) {
+               g_printerr("Error invoking %s () : %s\n", sensorid, error->message);
+               g_error_free(error);
+               return PERIPHERAL_ERROR_UNKNOWN;
+       }
+
+       g_variant_get(ret_value, "(iii)", &dev->period, &dev->duty_cycle, &ret);
+       g_variant_unref(ret_value);
+
+       return ret;
+}
diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c
new file mode 100644 (file)
index 0000000..7f10182
--- /dev/null
@@ -0,0 +1,260 @@
+/*
+ * Copyright (c) 2016-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 <unistd.h>
+#include <assert.h>
+
+#include "peripheral_io.h"
+#include "peripheral_dbus.h"
+#include "peripheral_common.h"
+#include "peripheral_internal.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @brief Initializes gpio_context, based on Gpio pin.
+ */
+
+#define GPIO_NAME      "gpio"
+
+int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+       peripheral_gpio_h handle;
+
+       assert(gpio_pin >= 0);
+
+       /* Initialize */
+       handle = (peripheral_gpio_h)calloc(1, sizeof(struct _peripheral_gpio_s));
+
+       if (handle == NULL) {
+               _E("Failed to allocate peripheral_gpio_h");
+               return PERIPHERAL_ERROR_OUT_OF_MEMORY;
+       }
+       handle->pin = gpio_pin;
+
+       if (!get_dbus_connection()) {
+               ret = set_dbus_connection();
+               if (ret != PERIPHERAL_ERROR_NONE)
+                       goto exit;
+       }
+
+       ret = peripheral_dbus_gpio(handle, GPIO_NAME, "OPEN", 0 , 0);
+
+exit:
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               free(handle);
+               handle = NULL;
+       }
+
+       *gpio = handle;
+
+       return ret;
+}
+
+/**
+ * @brief Closes the gpio_context.
+ */
+int peripheral_gpio_close(peripheral_gpio_h gpio)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       /* check validation of gpio context handle */
+       if (gpio == NULL)
+               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+
+       /* call gpio_close */
+       ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "CLOSE", 0 , 0);
+       if (ret)
+               ret = TIZEN_ERROR_IO_ERROR;
+
+       free(gpio);
+       gpio = NULL;
+
+       return ret;
+}
+
+/**
+ * @brief Sets Gpio direction.
+ */
+int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       /* check validation of gpio context handle */
+       if (gpio == NULL)
+               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+
+       if (direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH) {
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else {
+               if (gpio->direction != direction) {
+                       gpio->direction = direction;
+                       ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "SET_DIR", 0 , 0);
+               }
+       }
+       /* call gpio_set_direction */
+
+       return ret;
+}
+
+/**
+ * @brief Sets the edge mode on the Gpio.
+ */
+int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       /* check validation of gpio context handle */
+       if (gpio == NULL)
+               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+
+       if (edge > PERIPHERAL_GPIO_EDGE_FALLING) {
+               ret = PERIPHERAL_ERROR_INVALID_PARAMETER;
+       } else {
+               if (gpio->edge != edge) {
+                       gpio->edge = edge;
+                       ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "SET_EDGE", 0 , 0);
+               }
+       }
+       /* call gpio_set_edge_mode */
+
+       return ret;
+}
+
+/**
+ * @brief Registers event handler callback for interrupt.
+ */
+int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data)
+{
+       /* check validation of gpio context handle */
+       if (gpio == NULL)
+               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+
+       //TODO
+       return PERIPHERAL_ERROR_INVALID_OPERATION;
+}
+
+/**
+ * @brief Unregisters event handler callback for interrupt.
+ */
+int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio)
+{
+       /* check validation of gpio context handle */
+       if (gpio == NULL)
+               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+       //TODO
+       return PERIPHERAL_ERROR_INVALID_OPERATION;
+}
+
+/**
+ * @brief Reads the gpio value.
+ */
+int peripheral_gpio_read(peripheral_gpio_h gpio, int *val)
+{
+       int value = 0;
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       /* check validation of gpio context handle */
+       if (gpio == NULL)
+               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+
+       /* call gpio_read */
+       ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "READ", 0, &value);
+       *val = value;
+
+       return ret;
+}
+
+/**
+ * @brief Writes to the gpio value.
+ */
+int peripheral_gpio_write(peripheral_gpio_h gpio, int value)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       /* check validation of gpio context handle */
+       if (gpio == NULL)
+               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+
+       ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "WRITE", value , 0);
+       /* call gpio_write */
+
+       if (ret != PERIPHERAL_ERROR_NONE)
+               return ret;
+
+       return ret;
+}
+
+/**
+ * @brief Gets a direction of the Gpio.
+ */
+int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       /* check validation of gpio context handle */
+       if (gpio == NULL)
+               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+
+       ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "GET_DIR", 0 , 0);
+
+       if (ret == PERIPHERAL_ERROR_NONE)
+               (*direction) = gpio->direction;
+
+       return ret;
+}
+
+/**
+ * @brief Gets a pin number of the Gpio.
+ */
+int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin)
+{
+       /* check validation of gpio context handle */
+       if (gpio == NULL)
+               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+
+       *gpio_pin = gpio->pin;
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+/**
+ * @brief Gets a edge mode of the Gpio.
+ */
+int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       /* check validation of gpio context handle */
+       if (gpio == NULL)
+               return PERIPHERAL_ERROR_INVALID_PARAMETER;
+
+       ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "GET_EDGE", 0 , 0);
+
+       if (ret == PERIPHERAL_ERROR_NONE)
+               (*edge) = gpio->edge;
+
+       return ret;
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c
new file mode 100644 (file)
index 0000000..865cf92
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2016-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 <unistd.h>
+#include <assert.h>
+
+#include "peripheral_io.h"
+#include "peripheral_dbus.h"
+#include "peripheral_common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define I2C_NAME       "i2c"
+int I2C_Addr = 0;
+
+peripheral_i2c_context_h peripheral_i2c_init(int bus)
+{
+       peripheral_i2c_context_h dev;
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       assert(bus >= 0);
+
+       /* Initialize peripheral_i2c_context_h */
+       dev = (peripheral_i2c_context_h)malloc(sizeof(struct _peripheral_i2c_s));
+
+       if (dev == NULL) {
+               _E("Failed to allocate peripheral_i2c_context_h");
+               return NULL;
+       }
+
+       if (!get_dbus_connection())
+               set_dbus_connection();
+
+       ret = peripheral_dbus_i2c(dev, I2C_NAME, "INIT", bus, 0, I2C_Addr);
+
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               free(dev);
+               _E("[PERIPHERAL] I2C init error\n");
+               dev = NULL;
+       }
+
+       return dev;
+}
+
+int peripheral_i2c_stop(peripheral_i2c_context_h dev)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+       /* Free peripheral_i2c_context_h */
+
+       if (dev != NULL) {
+               ret = peripheral_dbus_i2c(dev, I2C_NAME, "STOP", 0, 0, I2C_Addr);
+
+               free(dev);
+               dev = NULL;
+       }
+
+       return ret;
+}
+
+int peripheral_i2c_set_frequency(peripheral_i2c_context_h dev, peripheral_i2c_mode_e mode)
+{
+       /* Set the clocking for the selected frequency */
+       return peripheral_dbus_i2c(dev, I2C_NAME, "SET_FREQ", mode, 0, I2C_Addr);
+}
+
+int peripheral_i2c_set_address(peripheral_i2c_context_h dev, int address)
+{
+       /* Set the i2c slave address */
+
+       //I2C_Addr = address;
+       return peripheral_dbus_i2c(dev, I2C_NAME, "SET_ADDR", address, 0, I2C_Addr);
+}
+
+int peripheral_i2c_read(peripheral_i2c_context_h dev, uint8_t *data, int length)
+{
+       /* Read i2c data */
+       return peripheral_dbus_i2c(dev, I2C_NAME, "READ", length, data, I2C_Addr);
+}
+
+int peripheral_i2c_write(peripheral_i2c_context_h dev, uint8_t *data, int length)
+{
+       /* Write i2c data */
+       return peripheral_dbus_i2c(dev, I2C_NAME, "WRITE", length, data, I2C_Addr);
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c
new file mode 100644 (file)
index 0000000..3d6e950
--- /dev/null
@@ -0,0 +1,176 @@
+/*
+ * 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 <unistd.h>
+#include <assert.h>
+
+#include "peripheral_io.h"
+#include "peripheral_dbus.h"
+#include "peripheral_common.h"
+
+#define PWM_NAME       "pwm"
+#define PWM_ENABLE     1
+#define PWM_DISABLE    0
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+peripheral_pwm_context_h peripheral_pwm_open(int device, int channel)
+{
+       peripheral_pwm_context_h dev = NULL;
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       assert(device >= 0);
+       assert(channel >= 0);
+
+       /* Initialize */
+       dev = (peripheral_pwm_context_h)malloc(sizeof(struct _peripheral_pwm_s));
+
+       if (dev == NULL) {
+               _E("Failed to allocate peripheral_pwm_context_h");
+               return NULL;
+       }
+
+       if (!get_dbus_connection())
+               set_dbus_connection();
+
+       dev->device = device;
+       dev->channel = channel;
+
+       ret = peripheral_dbus_pwm(dev, PWM_NAME, "OPEN");
+
+       if (ret != PERIPHERAL_ERROR_NONE) {
+               free(dev);
+               dev = NULL;
+       }
+
+       return dev;
+}
+
+int peripheral_pwm_close(peripheral_pwm_context_h pwm)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       ret = peripheral_dbus_pwm(pwm, PWM_NAME, "CLOSE");
+
+       if (ret == PERIPHERAL_ERROR_NONE) {
+               free(pwm);
+               pwm = NULL;
+       }
+
+       return ret;
+}
+
+
+int    peripheral_pwm_set_duty_cycle(peripheral_pwm_context_h pwm, int duty_cycle)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       if (pwm->duty_cycle != duty_cycle) {
+               int duty_value = 0;
+
+               duty_value = pwm->duty_cycle;
+               pwm->duty_cycle = duty_cycle;
+               ret = peripheral_dbus_pwm(pwm, PWM_NAME, "SET_DUTY");
+
+               if (ret != PERIPHERAL_ERROR_NONE)
+                       pwm->duty_cycle = duty_value;
+       }
+
+       return ret;
+}
+
+int peripheral_pwm_set_period(peripheral_pwm_context_h pwm, int period)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       if (pwm->period != period) {
+               int period_value = 0;
+
+               period_value = pwm->period;
+               pwm->period = period;
+               ret = peripheral_dbus_pwm(pwm, PWM_NAME, "SET_PERIOD");
+
+               if (ret != PERIPHERAL_ERROR_NONE)
+                       pwm->period = period_value;
+       }
+
+       return ret;
+}
+
+int    peripheral_pwm_set_enabled(peripheral_pwm_context_h pwm, peripheral_pwm_state_e enable)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       if (pwm->enabled != enable) {
+               int enable_value = 0;
+
+               enable_value = pwm->enabled;
+               pwm->enabled = enable;
+               ret = peripheral_dbus_pwm(pwm, PWM_NAME, "SET_ENABLE");
+
+               if (ret != PERIPHERAL_ERROR_NONE)
+                       pwm->enabled = enable_value;
+       }
+
+       return PERIPHERAL_ERROR_NONE;
+}
+
+int peripheral_pwm_is_enabled(peripheral_pwm_context_h pwm)
+{
+       if (pwm->enabled == PWM_ENABLE)
+               return PWM_ENABLE;
+       else
+               return PWM_DISABLE;
+
+}
+
+int peripheral_pwm_get_duty_cycle(peripheral_pwm_context_h pwm, int *duty_cycle)
+{
+       int duty_value = 0;
+       int ret = PERIPHERAL_ERROR_NONE;
+
+       duty_value = pwm->duty_cycle;
+
+       ret = peripheral_dbus_pwm(pwm, PWM_NAME, "GET_DUTY");
+
+       (*duty_cycle) = pwm->duty_cycle;
+       pwm->duty_cycle = duty_value;
+
+       return ret;
+}
+
+int peripheral_pwm_get_period(peripheral_pwm_context_h pwm, int *period)
+{
+       int ret = PERIPHERAL_ERROR_NONE;
+       int period_value = 0;
+
+       period_value = pwm->period;
+
+       ret = peripheral_dbus_pwm(pwm, PWM_NAME, "GET_PERIOD");
+
+       (*period) = pwm->period;
+       pwm->period = period_value;
+
+       return ret;
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/peripheral_spi.c b/src/peripheral_spi.c
new file mode 100644 (file)
index 0000000..e2f8465
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2016-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 "peripheral_io.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+peripheral_spi_context_h peripheral_spi_open(unsigned int bus, peripheral_spi_context_h config)
+{
+       return NULL;
+}
+
+int    peripheral_spi_write(peripheral_spi_context_h hnd, char *txbuf, int length)
+{
+       return PERIPHERAL_ERROR_INVALID_OPERATION;
+}
+
+int    peripheral_spi_recv(peripheral_spi_context_h hnd, char *rxbuf, int length)
+{
+       return PERIPHERAL_ERROR_INVALID_OPERATION;
+}
+
+int peripheral_spi_transfer_buf(peripheral_spi_context_h hnd, char *txbuf, char *rxbuf, int length)
+{
+       return PERIPHERAL_ERROR_INVALID_OPERATION;
+}
+
+int    peripheral_spi_close(peripheral_spi_context_h hnd)
+{
+       return PERIPHERAL_ERROR_INVALID_OPERATION;
+}
diff --git a/src/peripheral_uart.c b/src/peripheral_uart.c
new file mode 100644 (file)
index 0000000..4082df6
--- /dev/null
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2016-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.
+ */
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644 (file)
index 0000000..6f01d42
--- /dev/null
@@ -0,0 +1,21 @@
+PROJECT(peripheral-io-test C)
+SET(fw_test "${fw_name}-test")
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(${fw_test} REQUIRED dlog)
+FOREACH(flag ${${fw_test}_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+       MESSAGE(${flag})
+ENDFOREACH()
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall")
+
+aux_source_directory(. sources)
+FOREACH(src ${sources})
+       GET_FILENAME_COMPONENT(src_name ${src} NAME_WE)
+       MESSAGE("${src_name}")
+       ADD_EXECUTABLE(${src_name} ${src})
+       TARGET_LINK_LIBRARIES(${src_name} ${fw_name} ${${fw_test}_LDFLAGS})
+ENDFOREACH()
+
+INSTALL(TARGETS peripheral-io-test RUNTIME DESTINATION bin/)
\ No newline at end of file
diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c
new file mode 100644 (file)
index 0000000..928f09e
--- /dev/null
@@ -0,0 +1,277 @@
+/*
+ * Copyright (c) 2016-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 "peripheral_io.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+extern int gpio_test();
+extern int i2c_test();
+extern int adc_test();
+
+int gpio_test(void)
+{
+       int num;
+       int cnt = 0;
+       peripheral_gpio_h handle = NULL;
+
+       printf("artik5 : 135 \n");
+       printf("artik10 : 22 \n");
+       printf(">> PIN NUMBER : ");
+
+       if (scanf("%d", &num) < 0)
+               return 0;
+       printf("num %d\n", num);
+
+       if (peripheral_gpio_open(num, &handle) != PERIPHERAL_ERROR_NONE) {
+               printf("handle is null\n");
+               return 0;
+       }
+
+       if (peripheral_gpio_set_direction(handle, PERIPHERAL_GPIO_DIRECTION_OUT) != PERIPHERAL_ERROR_NONE) {
+               printf("set direction error!!!");
+               goto error;
+       }
+
+       while (cnt++ < 5) {
+               printf("write~\n");
+               peripheral_gpio_write(handle, 1);
+               sleep(1);
+               peripheral_gpio_write(handle, 0);
+               sleep(1);
+       }
+       printf("write finish\n");
+       peripheral_gpio_close(handle);
+       return 1;
+
+error:
+       peripheral_gpio_close(handle);
+       return 0;
+}
+
+
+/* Address of GY30 light sensor */
+#define GY30_ADDR 0x23
+
+/* Start measurement at 11x resolution. Measurement time is approx 120ms. */
+#define GY30_CONT_HIGH_RES_MODE 0x10
+
+#define GY30_READ_INTENSITY(buf) ((buf[0] << 8 | buf[1]) / 1.2)
+
+int i2c_test(void)
+{
+       int cnt = 0;
+       int bus_num;
+       unsigned char buf[10];
+       peripheral_i2c_context_h dev;
+
+       printf(">> I2C bus number : ");
+       if (scanf("%d", &bus_num) < 0)
+               return 0;
+
+       if ((dev = peripheral_i2c_init(bus_num)) == NULL) {
+               printf("Failed to initialize I2C device\n");
+               return 0;
+       }
+
+       if (peripheral_i2c_set_address(dev, 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) {
+               printf("Failed to write\n");
+               goto error;
+       }
+
+       while (cnt++ < 15) {
+               int result;
+               sleep(1);
+               peripheral_i2c_read(dev, buf, 2);
+               result = GY30_READ_INTENSITY(buf);
+               printf("Result [%d]\n", result);
+       }
+
+       peripheral_i2c_stop(dev);
+       return 1;
+
+error:
+       peripheral_i2c_stop(dev);
+       return 0;
+}
+
+int adc_test(void)
+{
+#if 0
+       int channel = 0;
+       int data = 0;
+       adc_context_h dev = NULL;
+
+       printf(">>channel :");
+       scanf("%d", &channel);
+
+       dev = peripheral_adc_open(channel);
+
+       if (!dev) {
+               printf("open error!\n");
+               return 1;
+       }
+
+       peripheral_adc_read(dev, &data);
+
+       peripheral_adc_close(dev);
+#endif
+       return 1;
+}
+
+int pwm_test_led(void)
+{
+       int device = 0, channel = 0;
+       int period = 1 * 1000;
+       int duty_cycle = 1 * 1000 / 100;
+       int cnt = 0;
+
+       int set_duty_cycle;
+       int get_period, get_duty_cycle;
+       peripheral_pwm_context_h dev;
+
+       printf("<<< pwm_test >>>\n");
+
+       dev = peripheral_pwm_open(device, channel);
+       peripheral_pwm_set_period(dev, period); /* period: nanosecond */
+       peripheral_pwm_set_duty_cycle(dev, duty_cycle); /* duty_cycle: nanosecond */
+       peripheral_pwm_set_enabled(dev, 1);     /* 0: disable, 1: enable */
+
+       while (cnt < 5) {
+               for (set_duty_cycle = period; set_duty_cycle > 0; set_duty_cycle -= 50) {
+                       /* set duty cycle */
+                       peripheral_pwm_set_duty_cycle(dev, set_duty_cycle);
+                       peripheral_pwm_get_period(dev, &get_period);
+                       peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle);
+                       printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle);
+                       usleep(500000);
+               }
+               for (set_duty_cycle = 0; set_duty_cycle < period; set_duty_cycle += 50) {
+                       /* set duty cycle */
+                       peripheral_pwm_set_duty_cycle(dev, set_duty_cycle);
+                       peripheral_pwm_get_period(dev, &get_period);
+                       peripheral_pwm_get_duty_cycle(dev, &get_duty_cycle);
+                       printf("period(%d), duty_cycle(%d)\n", get_period, get_duty_cycle);
+                       usleep(500000);
+               }
+               cnt++;
+       }
+       peripheral_pwm_set_enabled(dev, 0);     /* 0: disable, 1: enable */
+       peripheral_pwm_close(dev);
+
+       return 0;
+}
+
+int pwm_test_motor(void)
+{
+       int device = 0, channel = 0;
+       int period = 20000000;
+       int duty_cycle = 1500000;
+       int cnt = 0, idx = 0;
+       int degree[3] = {0, 45, 90};
+       peripheral_pwm_context_h dev;
+
+       printf("<<< pwm_test_motor >>>\n");
+
+       dev = peripheral_pwm_open(device, channel);
+       for (cnt = 0; cnt < 5; cnt++) {
+               for (idx = 0; idx < 3; idx++) {
+                       switch (degree[idx]) {
+                       case 0:
+                               duty_cycle = 1000000;
+                               break;
+                       case 45:
+                               duty_cycle = 1500000;
+                               break;
+                       case 90:
+                               duty_cycle = 2000000;
+                               break;
+                       default:
+                               duty_cycle = 2000000;
+                               break;
+                       }
+                       printf("set degree: %d\n", degree[idx]);
+                       peripheral_pwm_set_period(dev, period);
+                       peripheral_pwm_set_duty_cycle(dev, duty_cycle);
+                       peripheral_pwm_set_enabled(dev, 1);             /* 0: disable, 1: enable */
+                       usleep(500000);
+               }
+       }
+
+       peripheral_pwm_set_enabled(dev, 0);     /* 0: disable, 1: enable */
+       peripheral_pwm_close(dev);
+
+       return 0;
+}
+
+int main(int argc, char **argv)
+{
+       int num = 1;
+       int ret;
+
+       printf("===================\n");
+       printf("  test Menu\n");
+       printf("===================\n");
+       printf(" 1. GPIO Test\n");
+       printf(" 2. I2C Test\n");
+       printf(" 3. pwm led test\n");
+       printf(" 4. pwm motor test\n");
+
+       printf(" 11. H/W IF GPIO Test\n");
+       printf(" 12. H/W IF I2C Test\n");
+       printf(" 13. H/W IF PWM Test\n");
+       printf(" 14. H/W IF SPI Test\n");
+
+       if (scanf("%d", &num) < 0)
+               return 0;
+
+       switch (num) {
+       case 1:
+               ret = gpio_test();
+               break;
+       case 2:
+               ret = i2c_test();
+               break;
+       case 3:
+               ret = pwm_test_led();
+               break;
+       case 4:
+               ret = pwm_test_motor();
+               break;
+       case 11:
+               ret = gpio_test();
+               break;
+       case 12:
+               ret = i2c_test();
+               break;
+       case 14:
+               ret = adc_test();
+               break;
+       default:
+               printf("Not support \n");
+       }
+       printf(" return : %d\n", ret);
+
+       return 1;
+}