From a6445240db57d1302ed517732b94bf9efa67c27b Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Mon, 17 Apr 2017 16:41:13 +0900 Subject: [PATCH 2/5] 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.34.1 From ba00ecdc24c7672e355168216ffa06c478ced9b8 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Tue, 18 Apr 2017 15:55:45 +0900 Subject: [PATCH 3/5] 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.34.1 From d1577cb349661404dbcc2a5f15e47ae63ba02d77 Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Wed, 19 Apr 2017 19:36:34 +0900 Subject: [PATCH 4/5] 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.34.1 From 88c3cfc66b2a78171750d57ddc5f45d93ed0e33c Mon Sep 17 00:00:00 2001 From: Hyeongsik Min Date: Wed, 19 Apr 2017 19:53:20 +0900 Subject: [PATCH 5/5] 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.34.1