From a6445240db57d1302ed517732b94bf9efa67c27b Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Mon, 17 Apr 2017 16:41:13 +0900 Subject: [PATCH 01/16] Import source codes from iotbus - 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 --- CMakeLists.txt | 73 +++++ LICENSE.APLv2 | 204 ++++++++++++ NOTICE | 3 + capi-system-peripheral-io.pc.in | 13 + include/peripheral_common.h | 29 ++ include/peripheral_dbus.h | 40 +++ include/peripheral_internal.h | 29 ++ include/peripheral_io.h | 458 +++++++++++++++++++++++++++ packaging/capi-system-peripheral-io.manifest | 5 + packaging/capi-system-peripheral-io.spec | 56 ++++ src/peripheral_adc.c | 46 +++ src/peripheral_dbus.c | 187 +++++++++++ src/peripheral_gpio.c | 260 +++++++++++++++ src/peripheral_i2c.c | 105 ++++++ src/peripheral_pwm.c | 176 ++++++++++ src/peripheral_spi.c | 46 +++ src/peripheral_uart.c | 15 + test/CMakeLists.txt | 21 ++ test/peripheral-io-test.c | 277 ++++++++++++++++ 19 files changed, 2043 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 LICENSE.APLv2 create mode 100644 NOTICE create mode 100644 capi-system-peripheral-io.pc.in create mode 100644 include/peripheral_common.h create mode 100644 include/peripheral_dbus.h create mode 100644 include/peripheral_internal.h create mode 100644 include/peripheral_io.h create mode 100644 packaging/capi-system-peripheral-io.manifest create mode 100644 packaging/capi-system-peripheral-io.spec create mode 100644 src/peripheral_adc.c create mode 100644 src/peripheral_dbus.c create mode 100644 src/peripheral_gpio.c create mode 100644 src/peripheral_i2c.c create mode 100644 src/peripheral_pwm.c create mode 100644 src/peripheral_spi.c create mode 100644 src/peripheral_uart.c create mode 100644 test/CMakeLists.txt create mode 100644 test/peripheral-io-test.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a31234b --- /dev/null +++ b/CMakeLists.txt @@ -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 index 0000000..6b524fa --- /dev/null +++ b/LICENSE.APLv2 @@ -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 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 index 0000000..1dd9b2c --- /dev/null +++ b/capi-system-peripheral-io.pc.in @@ -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 index 0000000..1e5d88f --- /dev/null +++ b/include/peripheral_common.h @@ -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 +#include + +#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 index 0000000..c32b2cb --- /dev/null +++ b/include/peripheral_dbus.h @@ -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 + +#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 index 0000000..8ab8cc5 --- /dev/null +++ b/include/peripheral_internal.h @@ -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 index 0000000..d6dcd30 --- /dev/null +++ b/include/peripheral_io.h @@ -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 +#include + +#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 index 0000000..a76fdba --- /dev/null +++ b/packaging/capi-system-peripheral-io.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/capi-system-peripheral-io.spec b/packaging/capi-system-peripheral-io.spec new file mode 100644 index 0000000..3aa5b57 --- /dev/null +++ b/packaging/capi-system-peripheral-io.spec @@ -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 index 0000000..1aa2973 --- /dev/null +++ b/src/peripheral_adc.c @@ -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 +#include +#include + +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 index 0000000..83eb624 --- /dev/null +++ b/src/peripheral_dbus.c @@ -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 +#include +#include + +#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 index 0000000..7f10182 --- /dev/null +++ b/src/peripheral_gpio.c @@ -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 +#include +#include +#include + +#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 index 0000000..865cf92 --- /dev/null +++ b/src/peripheral_i2c.c @@ -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 +#include +#include +#include + +#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 index 0000000..3d6e950 --- /dev/null +++ b/src/peripheral_pwm.c @@ -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 +#include +#include +#include + +#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 index 0000000..e2f8465 --- /dev/null +++ b/src/peripheral_spi.c @@ -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 +#include +#include + +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 index 0000000..4082df6 --- /dev/null +++ b/src/peripheral_uart.c @@ -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 index 0000000..6f01d42 --- /dev/null +++ b/test/CMakeLists.txt @@ -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 index 0000000..928f09e --- /dev/null +++ b/test/peripheral-io-test.c @@ -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 +#include +#include + +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; +} -- 2.7.4 From ba00ecdc24c7672e355168216ffa06c478ced9b8 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Tue, 18 Apr 2017 15:55:45 +0900 Subject: [PATCH 02/16] Remove i2c_set_frequency API Remove the API and related enum type because ioctl for i2c frequency setting is not defiend in kernel. Change-Id: Ib151fdcfbcb5978ef4d88350e05ac9f09252b959 Signed-off-by: Hyeongsik Min --- include/peripheral_io.h | 8 -------- src/peripheral_i2c.c | 6 ------ 2 files changed, 14 deletions(-) diff --git a/include/peripheral_io.h b/include/peripheral_io.h index d6dcd30..e998f45 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -283,18 +283,10 @@ struct _peripheral_i2c_s { }; 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); diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 865cf92..0ea2644 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -74,12 +74,6 @@ int peripheral_i2c_stop(peripheral_i2c_context_h dev) 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 */ -- 2.7.4 From d1577cb349661404dbcc2a5f15e47ae63ba02d77 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Wed, 19 Apr 2017 19:36:34 +0900 Subject: [PATCH 03/16] Rearrange the order of gpio API functions Change-Id: I3cfbc14654c35e47d141bc43e41507906a36242d Signed-off-by: Hyeongsik Min --- include/peripheral_io.h | 129 ++++++++++++++++++++++++---------------------- src/peripheral_gpio.c | 132 +++++++++++++++++++++++------------------------- 2 files changed, 133 insertions(+), 128 deletions(-) diff --git a/include/peripheral_io.h b/include/peripheral_io.h index e998f45..7b164c0 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -58,8 +58,9 @@ typedef enum { */ 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_OUT, /**< Output mode with low value */ + PERIPHERAL_GPIO_DIRECTION_OUT_LOW = PERIPHERAL_GPIO_DIRECTION_OUT, /**< Same as above */ + PERIPHERAL_GPIO_DIRECTION_OUT_HIGH, /**< Output mode with high value */ } peripheral_gpio_direction_e; /** @@ -79,21 +80,10 @@ typedef enum { 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. + * @brief Initializes(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[in] gpio_pin The gpio pin number * @param[out] gpio The gpio handle is created on success * * @return 0 on success, otherwise a negative error value @@ -109,7 +99,7 @@ typedef void(*gpio_isr_cb)(void *user_data); int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio); /** - * @brief Release the gpio handle and finalize(unexport) the gpio pin. + * @brief Releases the gpio handle and finalize(unexport) the gpio pin. * @since_tizen 4.0 * * @param[in] gpio The handle to the gpio pin to release @@ -126,11 +116,11 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio); int peripheral_gpio_close(peripheral_gpio_h gpio); /** - * @brief Sets direction of the gpio pin. + * @brief Gets direction of the gpio. * @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 + * @param[in] gpio The handle to the gpio pin + * @param[out] value The direction(value) type of the gpio * * @return 0 on success, otherwise a negative error value * @retval #PERIPHERAL_ERROR_NONE Successful @@ -138,15 +128,17 @@ int peripheral_gpio_close(peripheral_gpio_h gpio); * @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_set_direction() */ -int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction); +int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction); /** - * @brief Sets the edge mode of the gpio pin. + * @brief Sets direction 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 + * @param[in] direction Direction(value) type of the gpio pin * * @return 0 on success, otherwise a negative error value * @retval #PERIPHERAL_ERROR_NONE Successful @@ -155,46 +147,50 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct * @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); +int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction); /** - * @brief Registers a callback function to be invoked when the gpio interrupt is triggered. + * @brief Reads value of the gpio. * @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 + * @param[in] gpio The handle to the gpio pin + * @param[out] value The value of the gpio (zero or non-zero) * * @return 0 on success, otherwise a negative error value - * @retval #PERIPHERAL_ERROR_NONE Successfu - * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parametera + * @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_set_edge_mode() - * @see peripheral_gpio_unregister_cb() + * @see peripheral_gpio_write() */ -int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data); +int peripheral_gpio_read(peripheral_gpio_h gpio, int *value); /** - * @brief Unregisters the callback function for the gpio handler. + * @brief Writes value to the gpio. * @since_tizen 4.0 * * @param[in] gpio The handle to the gpio pin + * @param[in] value Value to be written to the gpio (muse be zero or non-zero) * * @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_register_cb() + * @see peripheral_gpio_read() */ -int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio); +int peripheral_gpio_write(peripheral_gpio_h gpio, int value); /** - * @brief Reads the gpio value. + * @brief Gets the edge mode of the gpio. * @since_tizen 4.0 * * @param[in] gpio The handle to the gpio pin - * @param[out] value The result of the gpio + * @param[out] gpio_pin The edge mode of the gpio * * @return 0 on success, otherwise a negative error value * @retval #PERIPHERAL_ERROR_NONE Successful @@ -202,15 +198,17 @@ int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio); * @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_set_edge_mode() */ -int peripheral_gpio_read(peripheral_gpio_h gpio, int *value); +int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge); /** - * @brief Writes the gpio value. + * @brief Sets the edge mode of the gpio pin. * @since_tizen 4.0 * - * @param[in] gpio The handle to the gpio pin - * @param[in] value The value to be written to the gpio + * @param[in] gpio The handle to the gpio pin to set + * @param[in] edge The edge mode of the gpio pin * * @return 0 on success, otherwise a negative error value * @retval #PERIPHERAL_ERROR_NONE Successful @@ -218,53 +216,66 @@ int peripheral_gpio_read(peripheral_gpio_h gpio, int *value); * @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_get_edge_mode() */ -int peripheral_gpio_write(peripheral_gpio_h gpio, int value); +int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge); /** - * @brief Gets direction of the gpio. + * @brief Called when the gpio interrupt is triggered. * @since_tizen 4.0 * - * @param[in] gpio The handle to the gpio pin - * @param[out] value The value to be written to the gpio + * @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 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 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_set_edge_mode() + * @see peripheral_gpio_unregister_cb() */ -int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction); +int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data); /** - * @brief Gets pin number of the gpio. + * @brief Unregisters the callback function for the gpio handler. * @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 + * + * @see peripheral_gpio_register_cb() */ -int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin); +int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio); /** - * @brief Gets edge mode of the gpio. + * @brief Gets pin number of the gpio handle. * @since_tizen 4.0 * * @param[in] gpio The handle to the gpio pin - * @param[out] gpio_pin The number of the gpio + * @param[out] gpio_pin The pin 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); +int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin); /** * @} diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index 7f10182..8d23ff9 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -24,12 +24,8 @@ #include "peripheral_common.h" #include "peripheral_internal.h" -#ifdef __cplusplus -extern "C" { -#endif - /** - * @brief Initializes gpio_context, based on Gpio pin. + * @brief Initializes(export) gpio pin and creates gpio handle. */ #define GPIO_NAME "gpio" @@ -71,6 +67,7 @@ exit: /** * @brief Closes the gpio_context. + * @brief Releases the gpio handle and finalize(unexport) the gpio pin. */ int peripheral_gpio_close(peripheral_gpio_h gpio) { @@ -92,9 +89,9 @@ int peripheral_gpio_close(peripheral_gpio_h gpio) } /** - * @brief Sets Gpio direction. + * @brief Gets direction of the gpio. */ -int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction) +int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction) { int ret = PERIPHERAL_ERROR_NONE; @@ -102,23 +99,19 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct 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 */ + ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "GET_DIR", 0 , 0); + + if (ret == PERIPHERAL_ERROR_NONE) + (*direction) = gpio->direction; return ret; } + /** - * @brief Sets the edge mode on the Gpio. + * @brief Sets direction of the gpio pin. */ -int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge) +int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction) { int ret = PERIPHERAL_ERROR_NONE; @@ -126,46 +119,21 @@ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - if (edge > PERIPHERAL_GPIO_EDGE_FALLING) { + if (direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH) { ret = PERIPHERAL_ERROR_INVALID_PARAMETER; } else { - if (gpio->edge != edge) { - gpio->edge = edge; - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "SET_EDGE", 0 , 0); + if (gpio->direction != direction) { + gpio->direction = direction; + ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "SET_DIR", 0 , 0); } } - /* call gpio_set_edge_mode */ + /* call gpio_set_direction */ 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. + * @brief Reads value of the gpio. */ int peripheral_gpio_read(peripheral_gpio_h gpio, int *val) { @@ -184,7 +152,7 @@ int peripheral_gpio_read(peripheral_gpio_h gpio, int *val) } /** - * @brief Writes to the gpio value. + * @brief Writes value to the gpio. */ int peripheral_gpio_write(peripheral_gpio_h gpio, int value) { @@ -204,9 +172,9 @@ int peripheral_gpio_write(peripheral_gpio_h gpio, int value) } /** - * @brief Gets a direction of the Gpio. + * @brief Gets the edge mode of the gpio. */ -int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction) +int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge) { int ret = PERIPHERAL_ERROR_NONE; @@ -214,47 +182,73 @@ int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direct if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "GET_DIR", 0 , 0); + ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "GET_EDGE", 0 , 0); if (ret == PERIPHERAL_ERROR_NONE) - (*direction) = gpio->direction; + (*edge) = gpio->edge; return ret; } /** - * @brief Gets a pin number of the Gpio. + * @brief Sets the edge mode of the gpio pin. */ -int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin) +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; - *gpio_pin = gpio->pin; + 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 PERIPHERAL_ERROR_NONE; + return ret; } /** - * @brief Gets a edge mode of the Gpio. + * @brief Registers a callback function to be invoked when the gpio interrupt is triggered. */ -int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge) +int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data) { - 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; + //TODO + return PERIPHERAL_ERROR_INVALID_OPERATION; +} - return ret; +/** + * @brief Unregisters the callback function for the gpio handler. + */ +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; } -#ifdef __cplusplus +/** + * @brief Gets pin number of the gpio handle. + */ +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; } -#endif -- 2.7.4 From 88c3cfc66b2a78171750d57ddc5f45d93ed0e33c Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Wed, 19 Apr 2017 19:53:20 +0900 Subject: [PATCH 04/16] Rename peripheral_i2c_context_h to peripheral_i2c_h Change-Id: I533ee1772cdd9b055deac139226bdac3d04d5889 Signed-off-by: Hyeongsik Min --- include/peripheral_dbus.h | 2 +- include/peripheral_internal.h | 9 ++++++++- include/peripheral_io.h | 18 ++++++----------- src/peripheral_dbus.c | 6 +++--- src/peripheral_i2c.c | 45 ++++++++++++++++++++++--------------------- test/peripheral-io-test.c | 2 +- 6 files changed, 42 insertions(+), 40 deletions(-) diff --git a/include/peripheral_dbus.h b/include/peripheral_dbus.h index c32b2cb..1136792 100644 --- a/include/peripheral_dbus.h +++ b/include/peripheral_dbus.h @@ -34,7 +34,7 @@ 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_i2c(peripheral_i2c_h i2c, 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 index 8ab8cc5..f331b57 100644 --- a/include/peripheral_internal.h +++ b/include/peripheral_internal.h @@ -18,7 +18,7 @@ #define __PERIPHERAL_INTERNAL_H__ /** - * @brief Internal struct for keep gpio context information + * @brief Internal struct for gpio context */ struct _peripheral_gpio_s { int pin; @@ -26,4 +26,11 @@ struct _peripheral_gpio_s { peripheral_gpio_edge_e edge; }; +/** + * @brief Internal struct for i2c context + */ +struct _peripheral_i2c_s { + int fd; +}; + #endif /* __PERIPHERAL_INTERNAL_H__ */ diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 7b164c0..44c9b59 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -286,23 +286,17 @@ int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin); * @{ */ -/** - * @brief Struct for peripheral_gpio_s - */ -struct _peripheral_i2c_s { - int fd; -}; -typedef struct _peripheral_i2c_s *peripheral_i2c_context_h; +typedef struct _peripheral_i2c_s *peripheral_i2c_h; -peripheral_i2c_context_h peripheral_i2c_init(int bus); +peripheral_i2c_h peripheral_i2c_init(int bus); -int peripheral_i2c_stop(peripheral_i2c_context_h hnd); +int peripheral_i2c_stop(peripheral_i2c_h i2c); -int peripheral_i2c_set_address(peripheral_i2c_context_h hnd, int address); +int peripheral_i2c_set_address(peripheral_i2c_h i2c, int address); -int peripheral_i2c_read(peripheral_i2c_context_h hnd, uint8_t *data, int length); +int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); -int peripheral_i2c_write(peripheral_i2c_context_h hnd, uint8_t *data, int length); +int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); /** diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index 83eb624..e90997a 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -101,7 +101,7 @@ int peripheral_dbus_gpio(peripheral_gpio_h gpio, char * sensorid, char *funcname } -int peripheral_dbus_i2c(peripheral_i2c_context_h dev, char * sensorid, char *funcname, int value, unsigned char * data, int addr) +int peripheral_dbus_i2c(peripheral_i2c_h i2c, char * sensorid, char *funcname, int value, unsigned char * data, int addr) { GError *error = NULL; GVariant *ret_value = NULL; @@ -126,7 +126,7 @@ int peripheral_dbus_i2c(peripheral_i2c_context_h dev, char * sensorid, char *fun PERIPHERAL_DBUS_PATH, PERIPHERAL_DBUS_INTERFACE, sensorid, - g_variant_new("(siiayi)", funcname, value, dev->fd, builder, addr), + g_variant_new("(siiayi)", funcname, value, i2c->fd, builder, addr), G_VARIANT_TYPE("(iayi)"), G_DBUS_CALL_FLAGS_NONE, -1, @@ -141,7 +141,7 @@ int peripheral_dbus_i2c(peripheral_i2c_context_h dev, char * sensorid, char *fun return PERIPHERAL_ERROR_UNKNOWN; } - g_variant_get(ret_value, "(iayi)", &(dev->fd), &ret_data, &ret); + g_variant_get(ret_value, "(iayi)", &(i2c->fd), &ret_data, &ret); g_variant_unref(ret_value); if (data != NULL) { diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 0ea2644..35c5d06 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -22,6 +22,7 @@ #include "peripheral_io.h" #include "peripheral_dbus.h" #include "peripheral_common.h" +#include "peripheral_internal.h" #ifdef __cplusplus extern "C" { @@ -30,68 +31,68 @@ extern "C" { #define I2C_NAME "i2c" int I2C_Addr = 0; -peripheral_i2c_context_h peripheral_i2c_init(int bus) +peripheral_i2c_h peripheral_i2c_init(int bus) { - peripheral_i2c_context_h dev; + peripheral_i2c_h i2c; int ret = PERIPHERAL_ERROR_NONE; assert(bus >= 0); - /* Initialize peripheral_i2c_context_h */ - dev = (peripheral_i2c_context_h)malloc(sizeof(struct _peripheral_i2c_s)); + /* Initialize peripheral_i2c_h */ + i2c = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s)); - if (dev == NULL) { - _E("Failed to allocate peripheral_i2c_context_h"); + if (i2c == NULL) { + _E("Failed to allocate peripheral_i2c_h"); return NULL; } if (!get_dbus_connection()) set_dbus_connection(); - ret = peripheral_dbus_i2c(dev, I2C_NAME, "INIT", bus, 0, I2C_Addr); + ret = peripheral_dbus_i2c(i2c, I2C_NAME, "INIT", bus, 0, I2C_Addr); if (ret != PERIPHERAL_ERROR_NONE) { - free(dev); + free(i2c); _E("[PERIPHERAL] I2C init error\n"); - dev = NULL; + i2c = NULL; } - return dev; + return i2c; } -int peripheral_i2c_stop(peripheral_i2c_context_h dev) +int peripheral_i2c_stop(peripheral_i2c_h i2c) { int ret = PERIPHERAL_ERROR_NONE; - /* Free peripheral_i2c_context_h */ + /* Free peripheral_i2c_h */ - if (dev != NULL) { - ret = peripheral_dbus_i2c(dev, I2C_NAME, "STOP", 0, 0, I2C_Addr); + if (i2c != NULL) { + ret = peripheral_dbus_i2c(i2c, I2C_NAME, "STOP", 0, 0, I2C_Addr); - free(dev); - dev = NULL; + free(i2c); + i2c = NULL; } return ret; } -int peripheral_i2c_set_address(peripheral_i2c_context_h dev, int address) +int peripheral_i2c_set_address(peripheral_i2c_h i2c, int address) { /* Set the i2c slave address */ //I2C_Addr = address; - return peripheral_dbus_i2c(dev, I2C_NAME, "SET_ADDR", address, 0, I2C_Addr); + return peripheral_dbus_i2c(i2c, I2C_NAME, "SET_ADDR", address, 0, I2C_Addr); } -int peripheral_i2c_read(peripheral_i2c_context_h dev, uint8_t *data, int length) +int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) { /* Read i2c data */ - return peripheral_dbus_i2c(dev, I2C_NAME, "READ", length, data, I2C_Addr); + return peripheral_dbus_i2c(i2c, I2C_NAME, "READ", length, data, I2C_Addr); } -int peripheral_i2c_write(peripheral_i2c_context_h dev, uint8_t *data, int length) +int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) { /* Write i2c data */ - return peripheral_dbus_i2c(dev, I2C_NAME, "WRITE", length, data, I2C_Addr); + return peripheral_dbus_i2c(i2c, I2C_NAME, "WRITE", length, data, I2C_Addr); } #ifdef __cplusplus diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 928f09e..857fb66 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -78,7 +78,7 @@ int i2c_test(void) int cnt = 0; int bus_num; unsigned char buf[10]; - peripheral_i2c_context_h dev; + peripheral_i2c_h dev; printf(">> I2C bus number : "); if (scanf("%d", &bus_num) < 0) -- 2.7.4 From 1f3d084c98d98646a26611c3aa6b5ec6c56c80e0 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 20 Apr 2017 17:19:05 +0900 Subject: [PATCH 05/16] fix validation check of edge mode This patch fixed a value of validation check to the max value of enum type. PERIPHERAL_GPIO_EDGE_FALLING -> PERIPHERAL_GPIO_EDGE_BOTH Change-Id: I6199f51f007b972a44f079f5d13cfd929963df00 Signed-off-by: jino.cho --- src/peripheral_gpio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index 8d23ff9..074ee17 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -201,7 +201,7 @@ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - if (edge > PERIPHERAL_GPIO_EDGE_FALLING) { + if (edge > PERIPHERAL_GPIO_EDGE_BOTH) { ret = PERIPHERAL_ERROR_INVALID_PARAMETER; } else { if (gpio->edge != edge) { -- 2.7.4 From 3f67fb53729f3c9996e2d669d22389e276819ab2 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Thu, 20 Apr 2017 11:05:08 +0900 Subject: [PATCH 06/16] Return i2c handle through pointer argument Change-Id: If65c0330fcd61edafea318018144d463fa1d559f Signed-off-by: Hyeongsik Min --- include/peripheral_io.h | 2 +- src/peripheral_i2c.c | 30 ++++++++++++++++++------------ test/peripheral-io-test.c | 14 +++++++------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 44c9b59..4458ae6 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -288,7 +288,7 @@ int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin); typedef struct _peripheral_i2c_s *peripheral_i2c_h; -peripheral_i2c_h peripheral_i2c_init(int bus); +int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c); int peripheral_i2c_stop(peripheral_i2c_h i2c); diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 35c5d06..a63e6f8 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -31,33 +31,39 @@ extern "C" { #define I2C_NAME "i2c" int I2C_Addr = 0; -peripheral_i2c_h peripheral_i2c_init(int bus) +int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) { - peripheral_i2c_h i2c; + peripheral_i2c_h handle; int ret = PERIPHERAL_ERROR_NONE; - assert(bus >= 0); + if (bus < 0) + return PERIPHERAL_ERROR_INVALID_PARAMETER; /* Initialize peripheral_i2c_h */ - i2c = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s)); + handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s)); - if (i2c == NULL) { + if (handle == NULL) { _E("Failed to allocate peripheral_i2c_h"); - return NULL; + return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - if (!get_dbus_connection()) - set_dbus_connection(); + if (!get_dbus_connection()) { + ret = set_dbus_connection(); + if (ret != PERIPHERAL_ERROR_NONE) + goto exit; + } - ret = peripheral_dbus_i2c(i2c, I2C_NAME, "INIT", bus, 0, I2C_Addr); + ret = peripheral_dbus_i2c(handle, I2C_NAME, "INIT", bus, 0, I2C_Addr); +exit: if (ret != PERIPHERAL_ERROR_NONE) { - free(i2c); _E("[PERIPHERAL] I2C init error\n"); - i2c = NULL; + free(handle); + handle = NULL; } + *i2c = handle; - return i2c; + return ret; } int peripheral_i2c_stop(peripheral_i2c_h i2c) diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 857fb66..c9b4609 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -78,24 +78,24 @@ int i2c_test(void) int cnt = 0; int bus_num; unsigned char buf[10]; - peripheral_i2c_h dev; + peripheral_i2c_h i2c; printf(">> I2C bus number : "); if (scanf("%d", &bus_num) < 0) return 0; - if ((dev = peripheral_i2c_init(bus_num)) == NULL) { + if ((peripheral_i2c_init(bus_num, &i2c)) != 0) { printf("Failed to initialize I2C device\n"); return 0; } - if (peripheral_i2c_set_address(dev, GY30_ADDR) != 0) { + if (peripheral_i2c_set_address(i2c, GY30_ADDR) != 0) { printf("Failed to set address\n"); goto error; } buf[0] = GY30_CONT_HIGH_RES_MODE; - if (peripheral_i2c_write(dev, buf, 1) != 0) { + if (peripheral_i2c_write(i2c, buf, 1) != 0) { printf("Failed to write\n"); goto error; } @@ -103,16 +103,16 @@ int i2c_test(void) while (cnt++ < 15) { int result; sleep(1); - peripheral_i2c_read(dev, buf, 2); + peripheral_i2c_read(i2c, buf, 2); result = GY30_READ_INTENSITY(buf); printf("Result [%d]\n", result); } - peripheral_i2c_stop(dev); + peripheral_i2c_stop(i2c); return 1; error: - peripheral_i2c_stop(dev); + peripheral_i2c_stop(i2c); return 0; } -- 2.7.4 From a0b47e9c274f34a3ea4bf4bc479c6e18e4788f4e Mon Sep 17 00:00:00 2001 From: Sungguk Na Date: Fri, 21 Apr 2017 19:49:11 +0900 Subject: [PATCH 07/16] Change gdbus interface by using gdbus-codegen Change-Id: I8706817d7ad17952823fae1d1475b4dc1570f3e1 Signed-off-by: Sungguk Na --- CMakeLists.txt | 12 +- include/peripheral_dbus.h | 36 ++- src/peripheral_dbus.c | 577 ++++++++++++++++++++++++++++++++++++++-------- src/peripheral_gpio.c | 65 ++---- src/peripheral_i2c.c | 53 ++--- src/peripheral_io.xml | 117 ++++++++++ src/peripheral_pwm.c | 66 ++---- 7 files changed, 701 insertions(+), 225 deletions(-) create mode 100644 src/peripheral_io.xml diff --git a/CMakeLists.txt b/CMakeLists.txt index a31234b..c9d951a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,13 +9,22 @@ SET(fw_name "${project_prefix}-${service}-${submodule}") PROJECT(${fw_name}) -SET(dependents "dlog glib-2.0 gio-2.0 capi-base-common") +SET(dependents "dlog glib-2.0 gio-2.0 gio-unix-2.0 capi-base-common") SET(pc_dependents "capi-base-common") SET(CMAKE_INSTALL_PREFIX ${prefix}) SET(PREFIX $(CMAKE_INSTALL_PREFIX)) SET(VERSION ${version}) +FIND_PROGRAM(GDBUS_CODEGEN NAMES gdbus-codegen) +EXEC_PROGRAM(${GDBUS_CODEGEN} ARGS + " \\ + --generate-c-code ${CMAKE_SOURCE_DIR}/src/peripheral_io_gdbus \\ + --c-namespace PeripheralIoGdbus \\ + --interface-prefix org.tizen.system.peripheral_io. \\ + ${CMAKE_SOURCE_DIR}/src/peripheral_io.xml \\ + ") + SET(INC_DIR include) INCLUDE_DIRECTORIES(${INC_DIR}) @@ -38,6 +47,7 @@ SET(SOURCES src/peripheral_adc.c src/peripheral_pwm.c src/peripheral_uart.c src/peripheral_dbus.c + src/peripheral_io_gdbus.c src/peripheral_i2c.c src/peripheral_spi.c) diff --git a/include/peripheral_dbus.h b/include/peripheral_dbus.h index 1136792..d21fb2a 100644 --- a/include/peripheral_dbus.h +++ b/include/peripheral_dbus.h @@ -20,6 +20,9 @@ #define PERIPHERAL_DBUS_INTERFACE "org.tizen.system.peripheral_io" #define PERIPHERAL_DBUS_PATH "/Org/Tizen/System/Peripheral_io" +#define PERIPHERAL_DBUS_GPIO_PATH "/Org/Tizen/System/Peripheral_io/Gpio" +#define PERIPHERAL_DBUS_I2C_PATH "/Org/Tizen/System/Peripheral_io/I2c" +#define PERIPHERAL_DBUS_PWM_PATH "/Org/Tizen/System/Peripheral_io/Pwm" #define PERIPHERAL_DBUS_NAME "org.tizen.system.peripheral_io" #define PERIPHERAL_METHOD_GPIO "gpio" @@ -28,13 +31,36 @@ #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); +void gpio_proxy_init(void); +void i2c_proxy_init(void); +void pwm_proxy_init(void); +void gpio_proxy_deinit(); +void i2c_proxy_deinit(); +void pwm_proxy_deinit(); int peripheral_dbus_gpio(peripheral_gpio_h gpio, char * sensorid, char *funcname, int write_value, int *read_value); -int peripheral_dbus_i2c(peripheral_i2c_h i2c, char * sensorid, char *funcname, int value, unsigned char *data, int addr); -int peripheral_dbus_pwm(peripheral_pwm_context_h dev, char * sensorid, char *funcname); +int peripheral_dbus_gpio_open(peripheral_gpio_h gpio); +int peripheral_dbus_gpio_close(peripheral_gpio_h gpio); +int peripheral_dbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction); +int peripheral_dbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction); +int peripheral_dbus_gpio_read(peripheral_gpio_h gpio, int *value); +int peripheral_dbus_gpio_write(peripheral_gpio_h gpio, int value); +int peripheral_dbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge); +int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge); + +int peripheral_dbus_i2c_init(peripheral_i2c_h i2c, int bus); +int peripheral_dbus_i2c_stop(peripheral_i2c_h i2c); +int peripheral_dbus_i2c_set_address(peripheral_i2c_h i2c, int address); +int peripheral_dbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); +int peripheral_dbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); + +int peripheral_dbus_pwm_open(peripheral_pwm_context_h dev, int device, int channel); +int peripheral_dbus_pwm_close(peripheral_pwm_context_h dev); +int peripheral_dbus_pwm_get_duty_cycle(peripheral_pwm_context_h dev, int *duty_cycle); +int peripheral_dbus_pwm_set_duty_cycle(peripheral_pwm_context_h dev, int duty_cycle); +int peripheral_dbus_pwm_get_period(peripheral_pwm_context_h dev, int *period); +int peripheral_dbus_pwm_set_period(peripheral_pwm_context_h dev, int period); +int peripheral_dbus_pwm_set_enable(peripheral_pwm_context_h dev, peripheral_pwm_state_e enable); #endif /* __PERIPHERAL_DBUS_H_ */ diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index e90997a..67c7025 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -22,166 +22,547 @@ #include "peripheral_dbus.h" #include "peripheral_common.h" #include "peripheral_internal.h" +#include "peripheral_io_gdbus.h" -GDBusConnection *connection = NULL; +PeripheralIoGdbusGpio *gpio_proxy = NULL; +PeripheralIoGdbusI2c *i2c_proxy = NULL; +PeripheralIoGdbusPwm *pwm_proxy = NULL; -int set_dbus_connection(void) +void gpio_proxy_init(void) { GError *error = NULL; - if (connection) - return PERIPHERAL_ERROR_NONE; + if (gpio_proxy != NULL) + return; - connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); - if (error) { - _E("gdbus error occurred (%s)", error->message); + gpio_proxy = peripheral_io_gdbus_gpio_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + PERIPHERAL_DBUS_NAME, + PERIPHERAL_DBUS_GPIO_PATH, + NULL, + &error); +} + +void i2c_proxy_init(void) +{ + GError *error = NULL; + + if (i2c_proxy != NULL) + return; + + i2c_proxy = peripheral_io_gdbus_i2c_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + PERIPHERAL_DBUS_NAME, + PERIPHERAL_DBUS_I2C_PATH, + NULL, + &error); +} + +void pwm_proxy_init(void) +{ + GError *error = NULL; + + if (pwm_proxy != NULL) + return; + + pwm_proxy = peripheral_io_gdbus_pwm_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + PERIPHERAL_DBUS_NAME, + PERIPHERAL_DBUS_PWM_PATH, + NULL, + &error); +} + +void gpio_proxy_deinit() +{ + gpio_proxy = NULL; +} + +void i2c_proxy_deinit() +{ + i2c_proxy = NULL; +} + +void pwm_proxy_deinit() +{ + pwm_proxy = NULL; +} + +int peripheral_dbus_gpio_open(peripheral_gpio_h gpio) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_open_sync( + gpio_proxy, + gpio->pin, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - if (!connection) { - _E("Failed to get gdbus connection "); + return ret; +} + +int peripheral_dbus_gpio_close(peripheral_gpio_h gpio) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_close_sync( + gpio_proxy, + gpio->pin, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); 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; + return ret; } -void unset_dbus_connection(void) +int peripheral_dbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction) { - if (connection) { - g_object_unref(connection); - connection = NULL; + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_get_direction_sync( + gpio_proxy, + gpio->pin, + (gint*)direction, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; } + + return ret; } -GDBusConnection *get_dbus_connection(void) +int peripheral_dbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e direction) { - return connection; + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_set_direction_sync( + gpio_proxy, + gpio->pin, + direction, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; } -int peripheral_dbus_gpio(peripheral_gpio_h gpio, char * sensorid, char *funcname, int write_value, int *read_value) +int peripheral_dbus_gpio_read(peripheral_gpio_h gpio, int *value) { GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; - GVariant *ret_value = NULL; - gint32 read = 0; - gint32 ret = PERIPHERAL_ERROR_NONE; + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - error = NULL; + if (peripheral_io_gdbus_gpio_call_read_sync( + gpio_proxy, + gpio->pin, + value, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } - 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); + return ret; +} - if (ret_value == NULL) { - g_printerr("Error invoking %s () : %s\n", sensorid, error->message); +int peripheral_dbus_gpio_write(peripheral_gpio_h gpio, int value) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_write_sync( + gpio_proxy, + gpio->pin, + value, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, 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); + return ret; +} + +int peripheral_dbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_get_edge_mode_sync( + gpio_proxy, + gpio->pin, + (int*)edge, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; - if (read_value != 0) - (*read_value) = read; + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_set_edge_mode_sync( + gpio_proxy, + gpio->pin, + edge, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } return ret; +} +int peripheral_dbus_i2c_init(peripheral_i2c_h i2c, int bus) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_i2c_call_init_sync( + i2c_proxy, + bus, + &i2c->fd, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; } -int peripheral_dbus_i2c(peripheral_i2c_h i2c, char * sensorid, char *funcname, int value, unsigned char * data, int addr) +int peripheral_dbus_i2c_stop(peripheral_i2c_h i2c) { GError *error = NULL; - GVariant *ret_value = NULL; - gint32 ret = PERIPHERAL_ERROR_NONE; - GVariantBuilder *builder; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_i2c_call_stop_sync( + i2c_proxy, + i2c->fd, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_dbus_i2c_set_address(peripheral_i2c_h i2c, int address) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_i2c_call_set_address_sync( + i2c_proxy, + i2c->fd, + address, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + if (ret != PERIPHERAL_ERROR_NONE) + _E("%s failed, ret = %d", __func__, ret); + + return ret; +} + +int peripheral_dbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + GVariant *data_array; + GVariantIter *iter; 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, i2c->fd, builder, addr), - G_VARIANT_TYPE("(iayi)"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); + int i = 0; + + if (i2c_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_i2c_call_read_sync( + i2c_proxy, + i2c->fd, + length, + &data_array, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + g_variant_get(data_array, "a(y)", &iter); + while (g_variant_iter_loop(iter, "(y)", &str)) { + data[i] = str; + if (i++ == length) break; + } + g_variant_iter_free(iter); + + return ret; +} +int peripheral_dbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + GVariantBuilder *builder; + GVariant *g_data; + int i = 0; + + if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)")); + + for (i = 0; i < length; i++) + g_variant_builder_add(builder, "(y)", data[i]); + g_variant_builder_add(builder, "(y)", 0x00); + + g_data = g_variant_new("a(y)", builder); g_variant_builder_unref(builder); - if (ret_value == NULL) { - g_printerr("Error invoking %s () : %s\n", sensorid, error->message); + if (peripheral_io_gdbus_i2c_call_write_sync( + i2c_proxy, + i2c->fd, + length, + g_data, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } - g_variant_get(ret_value, "(iayi)", &(i2c->fd), &ret_data, &ret); - g_variant_unref(ret_value); + return ret; +} + +int peripheral_dbus_pwm_open(peripheral_pwm_context_h dev, int device, int channel) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; - if (data != NULL) { - int i = 0; - while (g_variant_iter_loop(ret_data, "y", &str)) { - data[i] = str; - i++; - if (i == value) - break; - } + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_open_sync( + pwm_proxy, + device, + channel, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; } return ret; } -int peripheral_dbus_pwm(peripheral_pwm_context_h dev, char * sensorid, char *funcname) +int peripheral_dbus_pwm_close(peripheral_pwm_context_h dev) { 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); + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_close_sync( + pwm_proxy, + dev->device, + dev->channel, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, 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; +} + +int peripheral_dbus_pwm_get_duty_cycle(peripheral_pwm_context_h dev, int *duty_cycle) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_get_duty_cycle_sync( + pwm_proxy, + dev->device, + dev->channel, + duty_cycle, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} +int peripheral_dbus_pwm_set_duty_cycle(peripheral_pwm_context_h dev, int duty_cycle) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_set_duty_cycle_sync( + pwm_proxy, + dev->device, + dev->channel, + duty_cycle, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_dbus_pwm_get_period(peripheral_pwm_context_h dev, int *period) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_get_period_sync( + pwm_proxy, + dev->device, + dev->channel, + period, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_dbus_pwm_set_period(peripheral_pwm_context_h dev, int period) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_set_period_sync( + pwm_proxy, + dev->device, + dev->channel, + period, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_dbus_pwm_set_enable(peripheral_pwm_context_h dev, peripheral_pwm_state_e enable) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (pwm_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + /* TODO: Need to reorganize arguments */ + if (peripheral_io_gdbus_pwm_call_set_enable_sync( + pwm_proxy, + dev->device, + dev->channel, + enable, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } return ret; } diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index 074ee17..5520c87 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -23,13 +23,11 @@ #include "peripheral_dbus.h" #include "peripheral_common.h" #include "peripheral_internal.h" +#include "peripheral_io_gdbus.h" /** * @brief Initializes(export) gpio pin and creates gpio handle. */ - -#define GPIO_NAME "gpio" - int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) { int ret = PERIPHERAL_ERROR_NONE; @@ -46,15 +44,10 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio) } handle->pin = gpio_pin; - if (!get_dbus_connection()) { - ret = set_dbus_connection(); - if (ret != PERIPHERAL_ERROR_NONE) - goto exit; - } + gpio_proxy_init(); - ret = peripheral_dbus_gpio(handle, GPIO_NAME, "OPEN", 0 , 0); + ret = peripheral_dbus_gpio_open(handle); -exit: if (ret != PERIPHERAL_ERROR_NONE) { free(handle); handle = NULL; @@ -78,9 +71,10 @@ int peripheral_gpio_close(peripheral_gpio_h gpio) return PERIPHERAL_ERROR_INVALID_PARAMETER; /* call gpio_close */ - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "CLOSE", 0 , 0); + ret = peripheral_dbus_gpio_close(gpio); if (ret) ret = TIZEN_ERROR_IO_ERROR; + gpio_proxy_deinit(); free(gpio); gpio = NULL; @@ -99,10 +93,9 @@ int peripheral_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direct if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "GET_DIR", 0 , 0); - + ret = peripheral_dbus_gpio_get_direction(gpio, direction); if (ret == PERIPHERAL_ERROR_NONE) - (*direction) = gpio->direction; + gpio->direction = (*direction); return ret; } @@ -119,15 +112,13 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct 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); - } - } + if (direction > PERIPHERAL_GPIO_DIRECTION_OUT_HIGH) + return PERIPHERAL_ERROR_INVALID_PARAMETER; + /* call gpio_set_direction */ + ret = peripheral_dbus_gpio_set_direction(gpio, direction); + if (ret == PERIPHERAL_ERROR_NONE) + gpio->direction = direction; return ret; } @@ -135,9 +126,8 @@ int peripheral_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_direct /** * @brief Reads value of the gpio. */ -int peripheral_gpio_read(peripheral_gpio_h gpio, int *val) +int peripheral_gpio_read(peripheral_gpio_h gpio, int *value) { - int value = 0; int ret = PERIPHERAL_ERROR_NONE; /* check validation of gpio context handle */ @@ -145,8 +135,7 @@ int peripheral_gpio_read(peripheral_gpio_h gpio, int *val) return PERIPHERAL_ERROR_INVALID_PARAMETER; /* call gpio_read */ - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "READ", 0, &value); - *val = value; + ret = peripheral_dbus_gpio_read(gpio, value); return ret; } @@ -162,11 +151,8 @@ int peripheral_gpio_write(peripheral_gpio_h gpio, int value) 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; + ret = peripheral_dbus_gpio_write(gpio, value); return ret; } @@ -182,10 +168,9 @@ int peripheral_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "GET_EDGE", 0 , 0); - + ret = peripheral_dbus_gpio_get_edge_mode(gpio, edge); if (ret == PERIPHERAL_ERROR_NONE) - (*edge) = gpio->edge; + gpio->edge = (*edge); return ret; } @@ -201,15 +186,13 @@ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - if (edge > PERIPHERAL_GPIO_EDGE_BOTH) { - ret = PERIPHERAL_ERROR_INVALID_PARAMETER; - } else { - if (gpio->edge != edge) { - gpio->edge = edge; - ret = peripheral_dbus_gpio(gpio, GPIO_NAME, "SET_EDGE", 0 , 0); - } - } + if (edge > PERIPHERAL_GPIO_EDGE_BOTH) + return PERIPHERAL_ERROR_INVALID_PARAMETER; + /* call gpio_set_edge_mode */ + ret = peripheral_dbus_gpio_set_edge_mode(gpio, edge); + if (ret == PERIPHERAL_ERROR_NONE) + gpio->edge = edge; return ret; } diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index a63e6f8..2f1981d 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -24,13 +24,6 @@ #include "peripheral_common.h" #include "peripheral_internal.h" -#ifdef __cplusplus -extern "C" { -#endif - -#define I2C_NAME "i2c" -int I2C_Addr = 0; - int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) { peripheral_i2c_h handle; @@ -47,15 +40,10 @@ int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) return PERIPHERAL_ERROR_OUT_OF_MEMORY; } - if (!get_dbus_connection()) { - ret = set_dbus_connection(); - if (ret != PERIPHERAL_ERROR_NONE) - goto exit; - } + i2c_proxy_init(); - ret = peripheral_dbus_i2c(handle, I2C_NAME, "INIT", bus, 0, I2C_Addr); + ret = peripheral_dbus_i2c_init(handle, bus); -exit: if (ret != PERIPHERAL_ERROR_NONE) { _E("[PERIPHERAL] I2C init error\n"); free(handle); @@ -69,38 +57,43 @@ exit: int peripheral_i2c_stop(peripheral_i2c_h i2c) { int ret = PERIPHERAL_ERROR_NONE; - /* Free peripheral_i2c_h */ - if (i2c != NULL) { - ret = peripheral_dbus_i2c(i2c, I2C_NAME, "STOP", 0, 0, I2C_Addr); + if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - free(i2c); - i2c = NULL; - } + ret = peripheral_dbus_i2c_stop(i2c); + gpio_proxy_deinit(); + + free(i2c); + i2c = NULL; return ret; } int peripheral_i2c_set_address(peripheral_i2c_h i2c, int address) { - /* Set the i2c slave address */ + if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - //I2C_Addr = address; - return peripheral_dbus_i2c(i2c, I2C_NAME, "SET_ADDR", address, 0, I2C_Addr); + return peripheral_dbus_i2c_set_address(i2c, address); } int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) { - /* Read i2c data */ - return peripheral_dbus_i2c(i2c, I2C_NAME, "READ", length, data, I2C_Addr); + int ret = PERIPHERAL_ERROR_NONE; + + if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + ret = peripheral_dbus_i2c_read(i2c, data, length); + /* + _D("I2C read data : "); + for (int i = 0 ; i < length ; i++) + _D("[%02x]", data[i]); + */ + return ret; } int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) { - /* Write i2c data */ - return peripheral_dbus_i2c(i2c, I2C_NAME, "WRITE", length, data, I2C_Addr); -} + if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; -#ifdef __cplusplus + return peripheral_dbus_i2c_write(i2c, data, length); } -#endif diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml new file mode 100644 index 0000000..07a0067 --- /dev/null +++ b/src/peripheral_io.xml @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/peripheral_pwm.c b/src/peripheral_pwm.c index 3d6e950..d4d728a 100644 --- a/src/peripheral_pwm.c +++ b/src/peripheral_pwm.c @@ -23,14 +23,9 @@ #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; @@ -47,13 +42,12 @@ peripheral_pwm_context_h peripheral_pwm_open(int device, int channel) return NULL; } - if (!get_dbus_connection()) - set_dbus_connection(); + pwm_proxy_init(); dev->device = device; dev->channel = channel; - ret = peripheral_dbus_pwm(dev, PWM_NAME, "OPEN"); + ret = peripheral_dbus_pwm_open(dev, device, channel); if (ret != PERIPHERAL_ERROR_NONE) { free(dev); @@ -67,7 +61,8 @@ int peripheral_pwm_close(peripheral_pwm_context_h pwm) { int ret = PERIPHERAL_ERROR_NONE; - ret = peripheral_dbus_pwm(pwm, PWM_NAME, "CLOSE"); + ret = peripheral_dbus_pwm_close(pwm); + pwm_proxy_deinit(); if (ret == PERIPHERAL_ERROR_NONE) { free(pwm); @@ -82,16 +77,10 @@ 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; + ret = peripheral_dbus_pwm_set_duty_cycle(pwm, duty_cycle); - duty_value = pwm->duty_cycle; + if (ret != PERIPHERAL_ERROR_NONE) 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; } @@ -100,16 +89,10 @@ 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; + ret = peripheral_dbus_pwm_set_period(pwm, period); - period_value = pwm->period; + if (ret != PERIPHERAL_ERROR_NONE) pwm->period = period; - ret = peripheral_dbus_pwm(pwm, PWM_NAME, "SET_PERIOD"); - - if (ret != PERIPHERAL_ERROR_NONE) - pwm->period = period_value; - } return ret; } @@ -118,16 +101,10 @@ int peripheral_pwm_set_enabled(peripheral_pwm_context_h pwm, peripheral_pwm_stat { int ret = PERIPHERAL_ERROR_NONE; - if (pwm->enabled != enable) { - int enable_value = 0; + ret = peripheral_dbus_pwm_set_enable(pwm, enable); - enable_value = pwm->enabled; + if (ret != PERIPHERAL_ERROR_NONE) 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; } @@ -138,20 +115,16 @@ int peripheral_pwm_is_enabled(peripheral_pwm_context_h pwm) 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"); + ret = peripheral_dbus_pwm_get_duty_cycle(pwm, duty_cycle); - (*duty_cycle) = pwm->duty_cycle; - pwm->duty_cycle = duty_value; + if (ret != PERIPHERAL_ERROR_NONE) + pwm->duty_cycle = *duty_cycle; return ret; } @@ -159,18 +132,11 @@ 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) { int ret = PERIPHERAL_ERROR_NONE; - int period_value = 0; - period_value = pwm->period; + ret = peripheral_dbus_pwm_get_period(pwm, period); - ret = peripheral_dbus_pwm(pwm, PWM_NAME, "GET_PERIOD"); - - (*period) = pwm->period; - pwm->period = period_value; + if (ret != PERIPHERAL_ERROR_NONE) + pwm->period = *period; return ret; } - -#ifdef __cplusplus -} -#endif -- 2.7.4 From 1b2171a5bfe6829d0b202c070d539b616b79d187 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Mon, 24 Apr 2017 13:11:26 +0900 Subject: [PATCH 08/16] Fix gdbus interface prefix and typo in xml Changed gdbus interface prefix to 'org.tizen.peripheral_io'. Change-Id: Iab717d474a9c01011fe08268999d7849433849b4 Signed-off-by: Hyeongsik Min --- CMakeLists.txt | 2 +- include/peripheral_dbus.h | 12 ++++++------ src/peripheral_io.xml | 22 +++++++++++----------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c9d951a..45ec9ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ EXEC_PROGRAM(${GDBUS_CODEGEN} ARGS " \\ --generate-c-code ${CMAKE_SOURCE_DIR}/src/peripheral_io_gdbus \\ --c-namespace PeripheralIoGdbus \\ - --interface-prefix org.tizen.system.peripheral_io. \\ + --interface-prefix org.tizen.peripheral_io. \\ ${CMAKE_SOURCE_DIR}/src/peripheral_io.xml \\ ") diff --git a/include/peripheral_dbus.h b/include/peripheral_dbus.h index d21fb2a..557b296 100644 --- a/include/peripheral_dbus.h +++ b/include/peripheral_dbus.h @@ -18,12 +18,12 @@ #include -#define PERIPHERAL_DBUS_INTERFACE "org.tizen.system.peripheral_io" -#define PERIPHERAL_DBUS_PATH "/Org/Tizen/System/Peripheral_io" -#define PERIPHERAL_DBUS_GPIO_PATH "/Org/Tizen/System/Peripheral_io/Gpio" -#define PERIPHERAL_DBUS_I2C_PATH "/Org/Tizen/System/Peripheral_io/I2c" -#define PERIPHERAL_DBUS_PWM_PATH "/Org/Tizen/System/Peripheral_io/Pwm" -#define PERIPHERAL_DBUS_NAME "org.tizen.system.peripheral_io" +#define PERIPHERAL_DBUS_INTERFACE "org.tizen.peripheral_io" +#define PERIPHERAL_DBUS_PATH "/Org/Tizen/Peripheral_io" +#define PERIPHERAL_DBUS_GPIO_PATH "/Org/Tizen/Peripheral_io/Gpio" +#define PERIPHERAL_DBUS_I2C_PATH "/Org/Tizen/Peripheral_io/I2c" +#define PERIPHERAL_DBUS_PWM_PATH "/Org/Tizen/Peripheral_io/Pwm" +#define PERIPHERAL_DBUS_NAME "org.tizen.peripheral_io" #define PERIPHERAL_METHOD_GPIO "gpio" #define PERIPHERAL_METHOD_I2C "i2c" diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index 07a0067..40672ad 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -1,46 +1,46 @@ - + - + - + - + - + - + - + - + - + - + @@ -72,7 +72,7 @@ - + -- 2.7.4 From 3fe9637e30d9ed38407a95f29eb47be03369410e Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Tue, 25 Apr 2017 18:10:09 +0900 Subject: [PATCH 09/16] Add parameters to the gpio open method - add parameters to the gpio open method. - update the gpio handle when the gpio attributes are changed. Change-Id: I2c2ba69605bcc85c65beeb512c32a4b5d1453f18 Signed-off-by: jino.cho --- src/peripheral_dbus.c | 6 ++++++ src/peripheral_io.xml | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index 67c7025..3c32644 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -101,6 +101,8 @@ int peripheral_dbus_gpio_open(peripheral_gpio_h gpio) if (peripheral_io_gdbus_gpio_call_open_sync( gpio_proxy, gpio->pin, + (gint*)&gpio->edge, + (gint*)&gpio->direction, &ret, NULL, &error) == FALSE) { @@ -151,6 +153,7 @@ int peripheral_dbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_d g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } + gpio->direction = *direction; return ret; } @@ -173,6 +176,7 @@ int peripheral_dbus_gpio_set_direction(peripheral_gpio_h gpio, peripheral_gpio_d g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } + gpio->direction = direction; return ret; } @@ -239,6 +243,7 @@ int peripheral_dbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_e g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } + gpio->edge = *edge; return ret; } @@ -261,6 +266,7 @@ int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_e g_error_free(error); return PERIPHERAL_ERROR_UNKNOWN; } + gpio->edge = edge; return ret; } diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index 40672ad..a99cbbc 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -3,6 +3,8 @@ + + -- 2.7.4 From 541a12a30d38f2b857f9204eb78c63b355522fec Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Tue, 25 Apr 2017 14:13:52 +0900 Subject: [PATCH 10/16] Remove i2c_set_address() API and rearrange header - i2c_open() will pass bus and address argument together. - Add description for i2c APIs and fix typo. Change-Id: I1313fbf1fae23d828e1ac001d20f880747404109 Signed-off-by: Hyeongsik Min --- include/peripheral_dbus.h | 6 ++-- include/peripheral_io.h | 85 ++++++++++++++++++++++++++++++++++++++++------- src/peripheral_dbus.c | 34 +++---------------- src/peripheral_i2c.c | 16 +++------ src/peripheral_io.xml | 10 ++---- test/peripheral-io-test.c | 13 +++----- 6 files changed, 91 insertions(+), 73 deletions(-) diff --git a/include/peripheral_dbus.h b/include/peripheral_dbus.h index 557b296..529128d 100644 --- a/include/peripheral_dbus.h +++ b/include/peripheral_dbus.h @@ -39,7 +39,6 @@ void gpio_proxy_deinit(); void i2c_proxy_deinit(); void pwm_proxy_deinit(); -int peripheral_dbus_gpio(peripheral_gpio_h gpio, char * sensorid, char *funcname, int write_value, int *read_value); int peripheral_dbus_gpio_open(peripheral_gpio_h gpio); int peripheral_dbus_gpio_close(peripheral_gpio_h gpio); int peripheral_dbus_gpio_get_direction(peripheral_gpio_h gpio, peripheral_gpio_direction_e *direction); @@ -49,9 +48,8 @@ int peripheral_dbus_gpio_write(peripheral_gpio_h gpio, int value); int peripheral_dbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge); int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge); -int peripheral_dbus_i2c_init(peripheral_i2c_h i2c, int bus); -int peripheral_dbus_i2c_stop(peripheral_i2c_h i2c); -int peripheral_dbus_i2c_set_address(peripheral_i2c_h i2c, int address); +int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, int address); +int peripheral_dbus_i2c_close(peripheral_i2c_h i2c); int peripheral_dbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); int peripheral_dbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 4458ae6..6c62c1f 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -37,7 +37,7 @@ 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_PERMISSION_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 */ @@ -49,7 +49,7 @@ typedef enum { } peripheral_error_e; /** - * @addtogroup CAPI_SYSTEM_PERPHERAL_GPIO_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_GPIO_MODULE * @{ */ @@ -90,7 +90,7 @@ typedef struct _peripheral_gpio_s* peripheral_gpio_h; * @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_PERMISSION_DENIED Permission denied * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed * @@ -282,20 +282,81 @@ int peripheral_gpio_get_pin(peripheral_gpio_h gpio, int *gpio_pin); */ /** - * @addtogroup CAPI_SYSTEM_PERPHERAL_I2C_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_I2C_MODULE * @{ */ +/** + * @brief The handle to the i2c device + * @since_tizen 4.0 + */ typedef struct _peripheral_i2c_s *peripheral_i2c_h; -int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c); - -int peripheral_i2c_stop(peripheral_i2c_h i2c); +/** + * @brief Initializes i2c communication and creates i2c handle. + * @since_tizen 4.0 + * + * @param[in] bus The i2c bus number that the slave device is connected + * @param[in] address The address of the slave device + * @param[out] i2c The i2c 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_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * @retval #PERIPHERAL_ERROR_NO_DEVICE Device is not exist or removed + * + * @see peripheral_i2c_close() + */ +int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c); -int peripheral_i2c_set_address(peripheral_i2c_h i2c, int address); +/** + * @brief Destory the i2c handle and release the communication. + * @since_tizen 4.0 + * + * @param[in] i2c The i2c handle + * + * @return 0 on success, otherwise a negative error value + * @retval #PERIPHERAL_ERROR_NONE Successful + * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error + * + * @see peripheral_i2c_open() + */ +int peripheral_i2c_close(peripheral_i2c_h i2c); +/** + * @brief Reads data from the i2c device. + * @since_tizen 4.0 + * + * @param[in] i2c The handle to the i2c device + * @param[in, out] data The address of read buffer + * @param[in] length The size of data buffer (in bytes) + * + * @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 + */ int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length); +/** + * @brief Write data to the i2c device. + * @since_tizen 4.0 + * + * @param[in] i2c The handle to the i2c device + * @param[in, out] data The address of buffer to write + * @param[in] length The size of data buffer (in bytes) + * + * @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 + */ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); @@ -304,7 +365,7 @@ int peripheral_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length); */ /** - * @addtogroup CAPI_SYSTEM_PERPHERAL_PWM_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_PWM_MODULE * @{ */ @@ -344,7 +405,7 @@ int peripheral_pwm_get_period(peripheral_pwm_context_h pwm, int *period); */ /** - * @addtogroup CAPI_SYSTEM_PERPHERAL_ADC_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_ADC_MODULE * @{ */ @@ -375,7 +436,7 @@ int peripheral_adc_close(peripheral_adc_context_h dev); */ /** - * @addtogroup CAPI_SYSTEM_PERPHERAL_UART_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_UART_MODULE * @{ */ struct _peripheral_uart_s { @@ -412,7 +473,7 @@ int peripheral_uart_write(peripheral_uart_context_h hnd, const char *buf, unsign */ /** - * @addtogroup CAPI_SYSTEM_PERPHERAL_SPI_MODULE + * @addtogroup CAPI_SYSTEM_PERIPHERAL_SPI_MODULE * @{ */ diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index 3c32644..9cf8af5 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -271,16 +271,17 @@ int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_e return ret; } -int peripheral_dbus_i2c_init(peripheral_i2c_h i2c, int bus) +int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, int address) { GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - if (peripheral_io_gdbus_i2c_call_init_sync( + if (peripheral_io_gdbus_i2c_call_open_sync( i2c_proxy, bus, + address, &i2c->fd, &ret, NULL, @@ -293,38 +294,16 @@ int peripheral_dbus_i2c_init(peripheral_i2c_h i2c, int bus) return ret; } -int peripheral_dbus_i2c_stop(peripheral_i2c_h i2c) -{ - GError *error = NULL; - peripheral_error_e ret = PERIPHERAL_ERROR_NONE; - - if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - - if (peripheral_io_gdbus_i2c_call_stop_sync( - i2c_proxy, - i2c->fd, - &ret, - NULL, - &error) == FALSE) { - _E("Error in %s() : %s\n", __func__, error->message); - g_error_free(error); - return PERIPHERAL_ERROR_UNKNOWN; - } - - return ret; -} - -int peripheral_dbus_i2c_set_address(peripheral_i2c_h i2c, int address) +int peripheral_dbus_i2c_close(peripheral_i2c_h i2c) { GError *error = NULL; peripheral_error_e ret = PERIPHERAL_ERROR_NONE; if (i2c_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; - if (peripheral_io_gdbus_i2c_call_set_address_sync( + if (peripheral_io_gdbus_i2c_call_close_sync( i2c_proxy, i2c->fd, - address, &ret, NULL, &error) == FALSE) { @@ -333,9 +312,6 @@ int peripheral_dbus_i2c_set_address(peripheral_i2c_h i2c, int address) return PERIPHERAL_ERROR_UNKNOWN; } - if (ret != PERIPHERAL_ERROR_NONE) - _E("%s failed, ret = %d", __func__, ret); - return ret; } diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 2f1981d..23a33f0 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -24,7 +24,7 @@ #include "peripheral_common.h" #include "peripheral_internal.h" -int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) +int peripheral_i2c_open(int bus, int address, peripheral_i2c_h *i2c) { peripheral_i2c_h handle; int ret = PERIPHERAL_ERROR_NONE; @@ -32,7 +32,6 @@ int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) if (bus < 0) return PERIPHERAL_ERROR_INVALID_PARAMETER; - /* Initialize peripheral_i2c_h */ handle = (peripheral_i2c_h)malloc(sizeof(struct _peripheral_i2c_s)); if (handle == NULL) { @@ -42,7 +41,7 @@ int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) i2c_proxy_init(); - ret = peripheral_dbus_i2c_init(handle, bus); + ret = peripheral_dbus_i2c_open(handle, bus, address); if (ret != PERIPHERAL_ERROR_NONE) { _E("[PERIPHERAL] I2C init error\n"); @@ -54,13 +53,13 @@ int peripheral_i2c_init(int bus, peripheral_i2c_h *i2c) return ret; } -int peripheral_i2c_stop(peripheral_i2c_h i2c) +int peripheral_i2c_close(peripheral_i2c_h i2c) { int ret = PERIPHERAL_ERROR_NONE; if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - ret = peripheral_dbus_i2c_stop(i2c); + ret = peripheral_dbus_i2c_close(i2c); gpio_proxy_deinit(); free(i2c); @@ -69,13 +68,6 @@ int peripheral_i2c_stop(peripheral_i2c_h i2c) return ret; } -int peripheral_i2c_set_address(peripheral_i2c_h i2c, int address) -{ - if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - - return peripheral_dbus_i2c_set_address(i2c, address); -} - int peripheral_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) { int ret = PERIPHERAL_ERROR_NONE; diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index a99cbbc..cbda49b 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -43,18 +43,14 @@ - + + - - - - - + - diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index c9b4609..048d136 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -84,16 +84,11 @@ int i2c_test(void) if (scanf("%d", &bus_num) < 0) return 0; - if ((peripheral_i2c_init(bus_num, &i2c)) != 0) { - printf("Failed to initialize I2C device\n"); + if ((peripheral_i2c_open(bus_num, GY30_ADDR, &i2c)) != 0) { + printf("Failed to open I2C communication\n"); return 0; } - if (peripheral_i2c_set_address(i2c, GY30_ADDR) != 0) { - printf("Failed to set address\n"); - goto error; - } - buf[0] = GY30_CONT_HIGH_RES_MODE; if (peripheral_i2c_write(i2c, buf, 1) != 0) { printf("Failed to write\n"); @@ -108,11 +103,11 @@ int i2c_test(void) printf("Result [%d]\n", result); } - peripheral_i2c_stop(i2c); + peripheral_i2c_close(i2c); return 1; error: - peripheral_i2c_stop(i2c); + peripheral_i2c_close(i2c); return 0; } -- 2.7.4 From c311eda10558d36aa456172808323daa3f058975 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Tue, 25 Apr 2017 14:26:16 +0900 Subject: [PATCH 11/16] Update package version to 0.0.3 Change-Id: I81b41fb565e5b83c2e680e892503e44b033ab200 Signed-off-by: Hyeongsik Min --- packaging/capi-system-peripheral-io.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/capi-system-peripheral-io.spec b/packaging/capi-system-peripheral-io.spec index 3aa5b57..91a3634 100644 --- a/packaging/capi-system-peripheral-io.spec +++ b/packaging/capi-system-peripheral-io.spec @@ -1,6 +1,6 @@ Name: capi-system-peripheral-io Summary: Tizen Peripheral Input & Output library -Version: 0.0.1 +Version: 0.0.3 Release: 0 Group: System & System Tools License: Apache-2.0 -- 2.7.4 From 8950dfd59f83e6df465786c5a841a8a7cef78f4a Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 27 Apr 2017 10:54:04 +0900 Subject: [PATCH 12/16] Decrease the reference count of the dbus proxy The resource will be destroyed when its reference count drops to zero. Change-Id: Ibaca7da33eb6538b9c1928c5b8ea2b06eaf03245 Signed-off-by: jino.cho --- src/peripheral_dbus.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index 9cf8af5..e685b71 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -78,17 +78,26 @@ void pwm_proxy_init(void) void gpio_proxy_deinit() { - gpio_proxy = NULL; + if (gpio_proxy) { + g_object_unref(gpio_proxy); + gpio_proxy = NULL; + } } void i2c_proxy_deinit() { - i2c_proxy = NULL; + if (i2c_proxy) { + g_object_unref(i2c_proxy); + i2c_proxy = NULL; + } } void pwm_proxy_deinit() { - pwm_proxy = NULL; + if (pwm_proxy) { + g_object_unref(pwm_proxy); + pwm_proxy = NULL; + } } int peripheral_dbus_gpio_open(peripheral_gpio_h gpio) -- 2.7.4 From e9a0d6bce9e00a8049ed478b88f19196b7694dc9 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Wed, 26 Apr 2017 20:50:23 +0900 Subject: [PATCH 13/16] Define condition checking macros(RET_IF) Change-Id: I5b39db783a6499c1c5429faa8a5777032e743306 Signed-off-by: Hyeongsik Min --- include/peripheral_common.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/include/peripheral_common.h b/include/peripheral_common.h index 1e5d88f..2d2f960 100644 --- a/include/peripheral_common.h +++ b/include/peripheral_common.h @@ -26,4 +26,33 @@ #define _D(fmt, arg...) LOGD(fmt, ##arg) #define _W(fmt, arg...) LOGW(fmt, ##arg) +#define RET_IF(expr) \ + do { \ + if (expr) { \ + _E("(%s)", #expr); \ + return; \ + }\ + } while(0) +#define RETV_IF(expr, val) \ + do {\ + if (expr) { \ + _E("(%s)", #expr); \ + return (val); \ + } \ + } while(0) +#define RETM_IF(expr, fmt, arg...) \ + do {\ + if (expr) { \ + _E(fmt, ##arg); \ + return; \ + }\ + } while(0) +#define RETVM_IF(expr, val, fmt, arg...) \ + do {\ + if (expr) { \ + _E(fmt, ##arg); \ + return (val); \ + } \ + } while(0) + #endif /* __PERIPHERAL_COMMON_H__ */ -- 2.7.4 From 1762a85eacbe1aa7f7e4cd82d0030d41793f7847 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Thu, 20 Apr 2017 10:33:55 +0900 Subject: [PATCH 14/16] Support gpio interrupt The gpio interrupt should work properly if the patch for peripheral-bus is applied together. Change-Id: I1c7b0b9db563ea3b3fd222bb4b05b1dbc7b279c8 Signed-off-by: jino.cho --- include/peripheral_dbus.h | 2 + src/peripheral_dbus.c | 69 ++++++++++++++++++++++++++++++ src/peripheral_gpio.c | 107 ++++++++++++++++++++++++++++++++++++++++++++-- src/peripheral_io.xml | 12 ++++++ test/peripheral-io-test.c | 80 +++++++++++++++++++++++++++++++++- 5 files changed, 264 insertions(+), 6 deletions(-) diff --git a/include/peripheral_dbus.h b/include/peripheral_dbus.h index 529128d..e1002fa 100644 --- a/include/peripheral_dbus.h +++ b/include/peripheral_dbus.h @@ -47,6 +47,8 @@ int peripheral_dbus_gpio_read(peripheral_gpio_h gpio, int *value); int peripheral_dbus_gpio_write(peripheral_gpio_h gpio, int value); int peripheral_dbus_gpio_get_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e *edge); int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e edge); +int peripheral_dbus_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data); +int peripheral_dbus_gpio_unregister_cb(peripheral_gpio_h gpio); int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, int address); int peripheral_dbus_i2c_close(peripheral_i2c_h i2c); diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index e685b71..3a61390 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -24,6 +24,9 @@ #include "peripheral_internal.h" #include "peripheral_io_gdbus.h" +extern int peripheral_gpio_isr_callback(int pin); +void handle_gpio_changed(PeripheralIoGdbusGpio *gpio, gint pin, gint state, gpointer user_data); + PeripheralIoGdbusGpio *gpio_proxy = NULL; PeripheralIoGdbusI2c *i2c_proxy = NULL; PeripheralIoGdbusPwm *pwm_proxy = NULL; @@ -42,6 +45,16 @@ void gpio_proxy_init(void) PERIPHERAL_DBUS_GPIO_PATH, NULL, &error); + if (gpio_proxy == NULL) { + _E("Can not create gpio proxy : %s", error->message); + g_error_free(error); + return; + } + + g_signal_connect(gpio_proxy, + "gpio-changed", + G_CALLBACK(handle_gpio_changed), + NULL); } void i2c_proxy_init(void) @@ -100,6 +113,20 @@ void pwm_proxy_deinit() } } +void handle_gpio_changed( + PeripheralIoGdbusGpio *gpio, + gint pin, + gint state, + gpointer user_data) +{ + if (!gpio) + return; + + _D("gpio=%d state=%d",pin, state); + + peripheral_gpio_isr_callback(pin); +} + int peripheral_dbus_gpio_open(peripheral_gpio_h gpio) { GError *error = NULL; @@ -280,6 +307,48 @@ int peripheral_dbus_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_e return ret; } +int peripheral_dbus_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_register_irq_sync( + gpio_proxy, + gpio->pin, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_dbus_gpio_unregister_cb(peripheral_gpio_h gpio) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (gpio_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_gpio_call_unregister_irq_sync( + gpio_proxy, + gpio->pin, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, int address) { GError *error = NULL; diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index 5520c87..f942759 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -25,6 +25,88 @@ #include "peripheral_internal.h" #include "peripheral_io_gdbus.h" +typedef struct { + int pin; + gpio_isr_cb callback; + void *user_data; +} gpio_isr_data_s; + +static GList *gpio_isr_list = NULL; + +int peripheral_gpio_isr_callback(int pin) +{ + GList *link; + gpio_isr_data_s *isr_data; + + link = gpio_isr_list; + while (link) { + isr_data = (gpio_isr_data_s*)link->data; + + if (isr_data->pin == pin) { + if (isr_data->callback) + isr_data->callback(isr_data->user_data); + return PERIPHERAL_ERROR_NONE; + } + link = g_list_next(link); + } + + return PERIPHERAL_ERROR_NONE; +} + +int peripheral_gpio_isr_set(int pin, gpio_isr_cb callback, void *user_data) +{ + GList *link; + gpio_isr_data_s *isr_data = NULL; + + link = gpio_isr_list; + while (link) { + gpio_isr_data_s *tmp; + tmp = (gpio_isr_data_s*)link->data; + if (tmp->pin == pin) { + isr_data = tmp; + break; + } + link = g_list_next(link); + } + + if (isr_data == NULL) { + isr_data = (gpio_isr_data_s*)calloc(1, sizeof(gpio_isr_data_s)); + if (isr_data == NULL) { + _E("failed to allocate gpio_isr_data_s"); + return PERIPHERAL_ERROR_OUT_OF_MEMORY; + } + + gpio_isr_list = g_list_append(gpio_isr_list, isr_data); + } + + isr_data->pin = pin; + isr_data->callback = callback; + isr_data->user_data = user_data; + + return PERIPHERAL_ERROR_NONE; +} + +int peripheral_gpio_isr_unset(int pin) +{ + GList *link; + gpio_isr_data_s *isr_data; + + link = gpio_isr_list; + while (link) { + isr_data = (gpio_isr_data_s*)link->data; + + if (isr_data->pin == pin) { + gpio_isr_list = g_list_remove_link(gpio_isr_list, link); + free(link->data); + g_list_free(link); + break; + } + link = g_list_next(link); + } + + return PERIPHERAL_ERROR_NONE; +} + /** * @brief Initializes(export) gpio pin and creates gpio handle. */ @@ -202,12 +284,20 @@ int peripheral_gpio_set_edge_mode(peripheral_gpio_h gpio, peripheral_gpio_edge_e */ int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, void *user_data) { + int ret = PERIPHERAL_ERROR_NONE; + /* check validation of gpio context handle */ if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - //TODO - return PERIPHERAL_ERROR_INVALID_OPERATION; + ret = peripheral_dbus_gpio_register_cb(gpio, callback, user_data); + if (ret != PERIPHERAL_ERROR_NONE) + return ret; + + /* set isr */ + ret = peripheral_gpio_isr_set(gpio->pin, callback, user_data); + + return ret; } /** @@ -215,11 +305,20 @@ int peripheral_gpio_register_cb(peripheral_gpio_h gpio, gpio_isr_cb callback, vo */ int peripheral_gpio_unregister_cb(peripheral_gpio_h gpio) { + int ret = PERIPHERAL_ERROR_NONE; + /* check validation of gpio context handle */ if (gpio == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; - //TODO - return PERIPHERAL_ERROR_INVALID_OPERATION; + + ret = peripheral_dbus_gpio_unregister_cb(gpio); + if (ret != PERIPHERAL_ERROR_NONE) + return ret; + + /* clean up isr */ + ret = peripheral_gpio_isr_unset(gpio->pin); + + return ret; } /** diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index cbda49b..ed9559c 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -41,6 +41,18 @@ + + + + + + + + + + + + diff --git a/test/peripheral-io-test.c b/test/peripheral-io-test.c index 048d136..bec2885 100644 --- a/test/peripheral-io-test.c +++ b/test/peripheral-io-test.c @@ -19,11 +19,14 @@ #include #include #include +#include extern int gpio_test(); extern int i2c_test(); extern int adc_test(); +GMainLoop *loop; + int gpio_test(void) { int num; @@ -64,6 +67,79 @@ error: return 0; } +void gpio_irq_test_isr(void *user_data) +{ + int pin; + peripheral_gpio_h gpio = user_data; + + peripheral_gpio_get_pin(gpio, &pin); + + printf("gpio_irq_test_isr: GPIO %d interrupt occurs.\n", pin); +} + +void *gpio_irq_test_thread(void *data) +{ + peripheral_gpio_h gpio = data; + int num; + + printf(">> Press any key to exit GPIO IRQ Test : \n"); + if (scanf("%d", &num) < 0) + return 0; + + peripheral_gpio_unregister_cb(gpio); + peripheral_gpio_close(gpio); + + g_main_loop_quit(loop); + return 0; +} + +int gpio_irq_test(void) +{ + GThread *test_thread; + int num; + peripheral_gpio_h gpio = NULL; + peripheral_gpio_edge_e edge = PERIPHERAL_GPIO_EDGE_NONE; + + printf("artik710 : 27 \n"); + printf(">> PIN NUMBER : "); + + if (scanf("%d", &num) < 0) + return 0; + + if (peripheral_gpio_open(num, &gpio) != PERIPHERAL_ERROR_NONE) { + printf("test dev is null\n"); + return 0; + } + + if (peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_IN) != 0) { + printf("test set direction error!!!"); + goto error; + } + + printf(">> Select Edge Mode (0 = None, 1 = Falling, 2 = Rising, 3 = Both) : "); + if (scanf("%d", &num) < 0) + return 0; + + if (num >= 0 && num <= 3) + edge = num; + + peripheral_gpio_set_edge_mode( gpio, edge); + peripheral_gpio_register_cb(gpio, gpio_irq_test_isr, gpio); + + test_thread = g_thread_new("key input thread", &gpio_irq_test_thread, gpio); + loop = g_main_loop_new(NULL, FALSE); + g_main_loop_run(loop); + + g_thread_join(test_thread); + if (loop != NULL) + g_main_loop_unref(loop); + + return 0; + +error: + peripheral_gpio_close(gpio); + return 0; +} /* Address of GY30 light sensor */ #define GY30_ADDR 0x23 @@ -233,7 +309,7 @@ int main(int argc, char **argv) printf(" 3. pwm led test\n"); printf(" 4. pwm motor test\n"); - printf(" 11. H/W IF GPIO Test\n"); + printf(" 11. GPIO Interrupt 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"); @@ -255,7 +331,7 @@ int main(int argc, char **argv) ret = pwm_test_motor(); break; case 11: - ret = gpio_test(); + ret = gpio_irq_test(); break; case 12: ret = i2c_test(); -- 2.7.4 From 2e470160b2df2af956825f72750ab14f3f415e55 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Sun, 7 May 2017 18:03:51 +0900 Subject: [PATCH 15/16] Change argument name and type of i2c gdbus method The i2c methods will pass handle instead of file descriptor. Change-Id: Ia370c2fe4f3284e8d0b8925cc7bd4bb64c5f0df4 Signed-off-by: Hyeongsik Min --- include/peripheral_internal.h | 2 +- src/peripheral_dbus.c | 10 +++++----- src/peripheral_io.xml | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/peripheral_internal.h b/include/peripheral_internal.h index f331b57..338f75e 100644 --- a/include/peripheral_internal.h +++ b/include/peripheral_internal.h @@ -30,7 +30,7 @@ struct _peripheral_gpio_s { * @brief Internal struct for i2c context */ struct _peripheral_i2c_s { - int fd; + uint handle; }; #endif /* __PERIPHERAL_INTERNAL_H__ */ diff --git a/src/peripheral_dbus.c b/src/peripheral_dbus.c index 3a61390..b9af3d1 100644 --- a/src/peripheral_dbus.c +++ b/src/peripheral_dbus.c @@ -122,7 +122,7 @@ void handle_gpio_changed( if (!gpio) return; - _D("gpio=%d state=%d",pin, state); + _D("gpio=%d state=%d", pin, state); peripheral_gpio_isr_callback(pin); } @@ -360,7 +360,7 @@ int peripheral_dbus_i2c_open(peripheral_i2c_h i2c, int bus, int address) i2c_proxy, bus, address, - &i2c->fd, + &i2c->handle, &ret, NULL, &error) == FALSE) { @@ -381,7 +381,7 @@ int peripheral_dbus_i2c_close(peripheral_i2c_h i2c) if (peripheral_io_gdbus_i2c_call_close_sync( i2c_proxy, - i2c->fd, + i2c->handle, &ret, NULL, &error) == FALSE) { @@ -406,7 +406,7 @@ int peripheral_dbus_i2c_read(peripheral_i2c_h i2c, uint8_t *data, int length) if (peripheral_io_gdbus_i2c_call_read_sync( i2c_proxy, - i2c->fd, + i2c->handle, length, &data_array, &ret, @@ -448,7 +448,7 @@ int peripheral_dbus_i2c_write(peripheral_i2c_h i2c, uint8_t *data, int length) if (peripheral_io_gdbus_i2c_call_write_sync( i2c_proxy, - i2c->fd, + i2c->handle, length, g_data, &ret, diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index ed9559c..ba2edef 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -58,15 +58,15 @@ - + - + - + @@ -74,7 +74,7 @@ - + -- 2.7.4 From 05a568491a938223e095bc7244384d14a2c05b32 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Mon, 8 May 2017 17:26:27 +0900 Subject: [PATCH 16/16] Worng method was called replace gpio_proxy_deinit() to i2c_proxy_deinit() Change-Id: I7df8f0213b5e129efe6400d60d1b990e189e7bfd Signed-off-by: jino.cho --- src/peripheral_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/peripheral_i2c.c b/src/peripheral_i2c.c index 23a33f0..b871014 100644 --- a/src/peripheral_i2c.c +++ b/src/peripheral_i2c.c @@ -60,7 +60,7 @@ int peripheral_i2c_close(peripheral_i2c_h i2c) if (i2c == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; ret = peripheral_dbus_i2c_close(i2c); - gpio_proxy_deinit(); + i2c_proxy_deinit(); free(i2c); i2c = NULL; -- 2.7.4