From 0cab9f50bcf04615f839f69fff239312a3c148f8 Mon Sep 17 00:00:00 2001 From: Jinkun Jang Date: Wed, 13 Mar 2013 01:46:12 +0900 Subject: [PATCH] Tizen 2.1 base --- AUTHORS | 3 + CMakeLists.txt | 63 +++++ LICENSE.APLv2 | 203 +++++++++++++++ NOTICE | 3 + capi-network-serial.manifest | 5 + capi-network-serial.pc.in | 14 + debian/README | 0 debian/capi-network-serial-dev.install | 4 + debian/capi-network-serial-dev.postinst | 1 + debian/capi-network-serial.install | 1 + debian/capi-network-serial.postinst | 1 + debian/changelog | 32 +++ debian/compat | 1 + debian/control | 21 ++ debian/rules | 68 +++++ include/serial.h | 201 +++++++++++++++ include/serial_private.h | 67 +++++ packaging/capi-network-serial.spec | 58 +++++ src/serial.c | 435 ++++++++++++++++++++++++++++++++ 19 files changed, 1181 insertions(+) create mode 100644 AUTHORS create mode 100644 CMakeLists.txt create mode 100644 LICENSE.APLv2 create mode 100644 NOTICE create mode 100644 capi-network-serial.manifest create mode 100644 capi-network-serial.pc.in create mode 100644 debian/README create mode 100644 debian/capi-network-serial-dev.install create mode 100644 debian/capi-network-serial-dev.postinst create mode 100644 debian/capi-network-serial.install create mode 100644 debian/capi-network-serial.postinst create mode 100644 debian/changelog create mode 100644 debian/compat create mode 100644 debian/control create mode 100755 debian/rules create mode 100644 include/serial.h create mode 100644 include/serial_private.h create mode 100644 packaging/capi-network-serial.spec create mode 100644 src/serial.c diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..54bdcfd --- /dev/null +++ b/AUTHORS @@ -0,0 +1,3 @@ +Jinmin Jung +DoHyun Pyun +InJun Yang diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..77e9f04 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,63 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +SET(fw_name "capi-network-serial") + +PROJECT(${fw_name}) + +SET(CMAKE_INSTALL_PREFIX /usr) +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) + +SET(INC_DIR include) +INCLUDE_DIRECTORIES(${INC_DIR}) + +SET(dependents "dlog glib-2.0 capi-base-common dbus-glib-1") +SET(pc_dependents "capi-base-common") + +INCLUDE(FindPkgConfig) +pkg_check_modules(${fw_name} REQUIRED ${dependents}) +FOREACH(flag ${${fw_name}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIC -Wall -Werror") +SET(CMAKE_C_FLAGS_DEBUG "-O0 -g") + +IF("${ARCH}" STREQUAL "arm") + ADD_DEFINITIONS("-DTARGET") +ENDIF("${ARCH}" STREQUAL "arm") + +ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"") +ADD_DEFINITIONS("-DTIZEN_DEBUG") + +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -Wl,--rpath=/usr/lib") + +aux_source_directory(src SOURCES) +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 lib) +INSTALL( + DIRECTORY ${INC_DIR}/ DESTINATION include/network + FILES_MATCHING + PATTERN "*_private.h" EXCLUDE + PATTERN "${INC_DIR}/*.h" + ) + +SET(PC_NAME ${fw_name}) +SET(PC_REQUIRED ${pc_dependents}) +SET(PC_LDFLAGS -l${fw_name}) + +CONFIGURE_FILE( + capi-network-serial.pc.in + ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc + @ONLY +) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${fw_name}.pc DESTINATION lib/pkgconfig) + diff --git a/LICENSE.APLv2 b/LICENSE.APLv2 new file mode 100644 index 0000000..43e91eb --- /dev/null +++ b/LICENSE.APLv2 @@ -0,0 +1,203 @@ + + 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..0e0f016 --- /dev/null +++ b/NOTICE @@ -0,0 +1,3 @@ +Copyright (c) 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 terms and conditions. diff --git a/capi-network-serial.manifest b/capi-network-serial.manifest new file mode 100644 index 0000000..75b0fa5 --- /dev/null +++ b/capi-network-serial.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/capi-network-serial.pc.in b/capi-network-serial.pc.in new file mode 100644 index 0000000..127da21 --- /dev/null +++ b/capi-network-serial.pc.in @@ -0,0 +1,14 @@ +# Package Information for pkg-config + +prefix=@PREFIX@ +exec_prefix=/usr +libdir=/usr/lib +includedir=/usr/include/network + +Name: @PC_NAME@ +Description: @PACKAGE_DESCRIPTION@ +Version: @VERSION@ +Requires: @PC_REQUIRED@ +Libs: -L${libdir} @PC_LDFLAGS@ +Cflags: -I${includedir} + diff --git a/debian/README b/debian/README new file mode 100644 index 0000000..e69de29 diff --git a/debian/capi-network-serial-dev.install b/debian/capi-network-serial-dev.install new file mode 100644 index 0000000..761a28b --- /dev/null +++ b/debian/capi-network-serial-dev.install @@ -0,0 +1,4 @@ +/usr/include/* +/usr/include/*/* +/usr/lib/pkgconfig/*.pc + diff --git a/debian/capi-network-serial-dev.postinst b/debian/capi-network-serial-dev.postinst new file mode 100644 index 0000000..1a24852 --- /dev/null +++ b/debian/capi-network-serial-dev.postinst @@ -0,0 +1 @@ +#!/bin/sh diff --git a/debian/capi-network-serial.install b/debian/capi-network-serial.install new file mode 100644 index 0000000..4a755a4 --- /dev/null +++ b/debian/capi-network-serial.install @@ -0,0 +1 @@ +/usr/lib/lib*.so* diff --git a/debian/capi-network-serial.postinst b/debian/capi-network-serial.postinst new file mode 100644 index 0000000..1a24852 --- /dev/null +++ b/debian/capi-network-serial.postinst @@ -0,0 +1 @@ +#!/bin/sh diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..6513f06 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,32 @@ +capi-network-serial (0.0.2-3) unstable; urgency=low + + * Upload the package + * Git: slp-source.sec.samsung.net:slp/api/serial + * Tag: capi-network-serial_0.0.4 + + -- InJun Yang Sat, 08 Sep 2012 00:49:53 +0900 + +capi-network-serial (0.0.2-2) unstable; urgency=low + + * Upload the package + * Git: slp-source.sec.samsung.net:slp/api/serial + * Tag: capi-network-serial_0.0.3 + + -- InJun Yang Wed, 08 Aug 2012 13:25:47 +0900 + +capi-network-serial (0.0.2-1) unstable; urgency=low + + * Fix the crash issue + * Git: slp-source.sec.samsung.net:slp/api/serial + * Tag: capi-network-serial_0.0.2-1 + + -- InJun Yang Mon, 14 May 2012 10:49:29 +0900 + +capi-network-serial (0.0.1-1) unstable; urgency=low + + * Initial upload + * Git: slp-source.sec.samsung.net:slp/api/serial + * Tag: capi-network-serial_0.0.1-1 + + -- InJun Yang Mon, 08 Aug 2011 10:12:25 +0900 + diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..7ed6ff8 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +5 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..a85ecaf --- /dev/null +++ b/debian/control @@ -0,0 +1,21 @@ +Source: capi-network-serial +Section: libs +Priority: extra +Maintainer: Kangho Hur , ByungWoo Lee , Hocheol Seo , InJun Yang +Build-Depends: debhelper (>= 5), dlog-dev, capi-base-common-dev, libglib2.0-dev + +Package: capi-network-serial +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends} +Description: A Network Serial library in Tizen Native API + +Package: capi-network-serial-dev +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends}, capi-network-serial (= ${Source-Version}), capi-base-common-dev +Description: A Network Serial library in Tizen Native API (DEV) + +Package: capi-network-serial-dbg +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends}, capi-network-serial (= ${Source-Version}) +Description: A Network Serial library in Tizen Native API (DBG) + diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..6b8edf9 --- /dev/null +++ b/debian/rules @@ -0,0 +1,68 @@ +#!/usr/bin/make -f + +FULLVER ?= $(shell dpkg-parsechangelog | grep Version: | cut -d ' ' -f 2 | cut -d '-' -f 1) +MAJORVER ?= $(shell echo $(FULLVER) | cut -d '.' -f 1) + +CFLAGS = -Wall -g + +ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS))) + CFLAGS += -O0 +else + CFLAGS += -O2 +endif +CMAKE_ROOT_DIR ?= $(CURDIR) +CMAKE_BUILD_DIR ?= $(CURDIR)/cmake_build_tmp + +configure: configure-stamp +configure-stamp: + dh_testdir + mkdir -p $(CMAKE_BUILD_DIR) && cd $(CMAKE_BUILD_DIR) && cmake .. -DFULLVER=${FULLVER} -DMAJORVER=${MAJORVER} + touch configure-stamp + + +build: build-stamp +build-stamp: configure-stamp + dh_testdir + cd $(CMAKE_BUILD_DIR) && $(MAKE) + touch $@ + +clean: + cd $(CMAKE_ROOT_DIR) + dh_testdir + dh_testroot + rm -f build-stamp configure-stamp + rm -f `find . -name *.pc` + rm -rf $(CMAKE_BUILD_DIR) + dh_clean + +install: build + dh_testdir + dh_testroot + dh_clean -k + dh_installdirs + + cd $(CMAKE_BUILD_DIR) && $(MAKE) DESTDIR=$(CURDIR)/debian/tmp install + +binary-indep: build install + +binary-arch: build install + dh_testdir + dh_testroot + dh_installchangelogs + dh_installdocs + dh_installexamples + dh_install --sourcedir=debian/tmp + dh_installman + dh_link + dh_strip --dbg-package=capi-network-serial-dbg + dh_fixperms + dh_makeshlibs + dh_installdeb + dh_shlibdeps + dh_gencontrol + dh_md5sums + dh_builddeb + +binary: binary-indep binary-arch +.PHONY: build clean binary-indep binary-arch binary install configure + diff --git a/include/serial.h b/include/serial.h new file mode 100644 index 0000000..7f21fc5 --- /dev/null +++ b/include/serial.h @@ -0,0 +1,201 @@ +/* +* Copyright (c) 2012-2013 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 __TIZEN_NETWORK_SERIAL_H__ +#define __TIZEN_NETWORK_SERIAL_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/** +* @addtogroup CAPI_NETWORK_SERIAL_MODULE +* @{ +*/ + + +/** +* @brief The handle of serial +*/ +typedef void* serial_h; + +/** +* @brief Enumerations for the state of serial +*/ +typedef enum +{ + SERIAL_STATE_OPENED = 0, /**< Opened */ + SERIAL_STATE_CLOSED = 1, /**< Closed */ +} serial_state_e; + +/** +* @brief Enumerations for error code +*/ +typedef enum +{ + SERIAL_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */ + SERIAL_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */ + SERIAL_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ + SERIAL_ERROR_INVALID_OPERATION = TIZEN_ERROR_INVALID_OPERATION, /**< Invalid operation */ + SERIAL_ERROR_OPERATION_FAILED = TIZEN_ERROR_NETWORK_CLASS|0x0601, /**< Operation failed */ +} serial_error_e; + +/** +* @brief Called when you receive data. +* @remarks @a data is valid only in this function. +* @param[in] data The received data +* @param[in] user_data The user data passed from serial_set_data_received_cb() +* @pre If you register callback function using serial_set_data_received_cb(), this will be invoked when you receive data. +* @see serial_set_data_received_cb() +* @see serial_unset_data_received_cb() +*/ +typedef bool (*serial_data_received_cb)(const char *data, int data_length, void *user_data); + +/** +* @brief Called when the state of serial is changed. +* @param[in] result The result of opening the serial +* @param[in] state The state of serial +* @param[in] user_data The user data passed from serial_set_state_changed_cb() +* @pre If you register callback function using serial_set_state_changed_cb(), this will be invoked when the state of serial is changed. +* @see serial_set_state_changed_cb() +* @see serial_unset_state_changed_cb() +*/ +typedef void (*serial_state_changed_cb)(serial_error_e result, serial_state_e state, void *user_data); + + +/** +* @brief Creates the handle of serial. +* @param[out] serial The serial handle +* @return 0 on success, otherwise a negative error value. +* @retval #SERIAL_ERROR_NONE Successful +* @retval #SERIAL_ERROR_OUT_OF_MEMORY Out of memory +* @retval #SERIAL_ERROR_INVALID_PARAMETER Invalid parameter +* @retval #SERIAL_ERROR_OPERATION_FAILED Operation failed +* @see serial_destroy() +*/ +int serial_create(serial_h *serial); + +/** +* @brief Destroys the handle of serial. +* @param[in] serial The serial handle +* @return 0 on success, otherwise a negative error value. +* @retval #SERIAL_ERROR_NONE Successful +* @retval #SERIAL_ERROR_INVALID_PARAMETER Invalid parameter +* @see serial_create() +*/ +int serial_destroy(serial_h serial); + +/** +* @brief Opens the serial. +* @param[in] serial The serial handle +* @return 0 on success, otherwise a negative error value. +* @retval #SERIAL_ERROR_NONE Successful +* @retval #SERIAL_ERROR_INVALID_PARAMETER Invalid parameter +* @retval #SERIAL_ERROR_OPERATION_FAILED Operation failed +* @retval #SERIAL_ERROR_INVALID_OPERATION Invalid operation +* @post When a serial is opened, serial_state_changed_cb() will be called with #SERIAL_STATE_OPENED state +* @see serial_close() +*/ +int serial_open(serial_h serial); + +/** +* @brief Closes the serial. +* @param[in] serial The serial handle +* @return 0 on success, otherwise a negative error value. +* @retval #SERIAL_ERROR_NONE Successful +* @retval #SERIAL_ERROR_INVALID_PARAMETER Invalid parameter +* @retval #SERIAL_ERROR_OPERATION_FAILED Operation failed +* @retval #SERIAL_ERROR_INVALID_OPERATION Invalid operation +* @pre The serial must be opened. +* @post When a serial is closed, serial_state_changed_cb() will be called with #SERIAL_STATE_CLOSED state +* @see serial_open() +*/ +int serial_close(serial_h serial); + +/** +* @brief Writes data to serial server. +* @param[in] serial The serial handle +* @param[in] data The data to send +* @param[in] data_length The length of data to send +* @return The number of characters sent on success, otherwise a negative error value. +* @retval #SERIAL_ERROR_INVALID_PARAMETER Invalid parameter +* @retval #SERIAL_ERROR_OPERATION_FAILED Operation failed +* @pre The serial must be opened. +* @see serial_open() +*/ +int serial_write(serial_h serial, const char *data, int data_length); + +/** +* @brief Register a callback function to be invoked when you receive data. +* @param[in] serial The serial handle +* @param[in] callback The callback function to be invoked +* @param[in] user_data The user data to be passed to the callback function +* @return 0 on success, otherwise a negative error value. +* @retval #SERIAL_ERROR_NONE Successful +* @retval #SERIAL_ERROR_INVALID_PARAMETER Invalid parameter +* @post serial_data_received_cb() will be invoked when you receive data. +* @see serial_unset_data_received_cb() +* @see serial_data_received_cb() +*/ +int serial_set_data_received_cb(serial_h serial, serial_data_received_cb callback, void *user_data); + +/** +* @brief Unregister a callback function to be invoked when you receive data. +* @param[in] serial The serial handle +* @return 0 on success, otherwise a negative error value. +* @retval #SERIAL_ERROR_NONE Successful +* @retval #SERIAL_ERROR_INVALID_PARAMETER Invalid parameter +* @see serial_set_data_received_cb() +*/ +int serial_unset_data_received_cb(serial_h serial); + +/** +* @brief Register a callback function to be invoked when the state of serial is changed. +* @param[in] serial The serial handle +* @param[in] callback The callback function to be invoked +* @param[in] user_data The user data to be passed to the callback function +* @return 0 on success, otherwise a negative error value. +* @retval #SERIAL_ERROR_NONE Successful +* @retval #SERIAL_ERROR_INVALID_PARAMETER Invalid parameter +* @post serial_state_changed_cb() will be invoked when the state of serial is changed. +* @see serial_unset_state_changed_cb() +* @see serial_state_changed_cb() +*/ +int serial_set_state_changed_cb(serial_h serial, serial_state_changed_cb callback, void *user_data); + +/** +* @brief Unregister a callback function to be invoked when the state of serial is changed. +* @param[in] serial The serial handle +* @return 0 on success, otherwise a negative error value. +* @retval #SERIAL_ERROR_NONE Successful +* @retval #SERIAL_ERROR_INVALID_PARAMETER Invalid parameter +* @see serial_set_state_changed_cb() +*/ +int serial_unset_state_changed_cb(serial_h serial); + +/** +* @} +*/ + + +#ifdef __cplusplus +} +#endif + +#endif /* __TIZEN_NETWORK_SERIAL_H__ */ diff --git a/include/serial_private.h b/include/serial_private.h new file mode 100644 index 0000000..3b63b50 --- /dev/null +++ b/include/serial_private.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2012-2013 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 __TIZEN_NETWORK_SERIAL_PRIVATE_H__ +#define __TIZEN_NETWORK_SERIAL_PRIVATE_H__ + +#include + +#include "serial.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @internal + * @brief Serial callback. + */ + typedef struct _serial_event_s + { + const void *callback; + void *user_data; + } serial_event_s; + + + /** + * @internal + * @brief Serial handle + */ + typedef struct _serial_s{ + DBusGConnection *client_bus; + int client_socket; + int g_watch_id; + serial_event_s state_handler; + serial_event_s data_handler; + void *handle; +}serial_s; + +/** + * @internal + * @brief dbus signal from socket server + */ +typedef enum { + SERIAL_CLOSED, /* Serial session closed */ + SERIAL_OPENED, /* Serial session opened */ +}serial_event_e; + + +#ifdef __cplusplus +} +#endif + +#endif /* __TIZEN_NETWORK_SERIAL_PRIVATE_H__ */ diff --git a/packaging/capi-network-serial.spec b/packaging/capi-network-serial.spec new file mode 100644 index 0000000..6fa4dd5 --- /dev/null +++ b/packaging/capi-network-serial.spec @@ -0,0 +1,58 @@ +Name: capi-network-serial +Summary: Network Serial Framework +Version: 0.0.7 +Release: 0 +Group: TO_BE/FILLED_IN +License: Apache License, Version 2.0 +Source0: %{name}-%{version}.tar.gz +Requires(post): /sbin/ldconfig +Requires(postun): /sbin/ldconfig + +BuildRequires: pkgconfig(dbus-glib-1) +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(capi-base-common) + +BuildRequires: cmake + + +%description +Network Serial Framework + +%package devel +Summary: Network Serial Framework (DEV) +Group: TO_BE/FILLED +Requires: %{name} = %{version}-%{release} + +%description devel +Network Serial Framework (DEV). + +%prep +%setup -q + +%build +MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` +cmake . -DCMAKE_INSTALL_PREFIX=/usr -DFULLVER=%{version} -DMAJORVER=${MAJORVER} + +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +%make_install + + +%post -p /sbin/ldconfig + +%postun -p /sbin/ldconfig + +%files +%manifest capi-network-serial.manifest +%defattr(-,root,root,-) +%{_libdir}/libcapi-network-serial.so.* + +%files devel +%defattr(-,root,root,-) +%{_includedir}/network/serial.h +%{_libdir}/pkgconfig/capi-network-serial.pc +%{_libdir}/libcapi-network-serial.so + diff --git a/src/serial.c b/src/serial.c new file mode 100644 index 0000000..b30d4e3 --- /dev/null +++ b/src/serial.c @@ -0,0 +1,435 @@ +/* + * Copyright (c) 2012-2013 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" +#include "serial_private.h" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "CAPI_NETWORK_SERIAL" + +#define DBG(fmt, args...) SLOGD(fmt, ##args) +#define ERR(fmt, args...) SLOGE(fmt, ##args) + +#define SERIAL_SOCKET_PATH "/tmp/.dr_common_stream" +#define SERIAL_BUF_SIZE 65536 +#define SERIAL_INTERFACE "User.Data.Router.Introspectable" + +DBusConnection *dbus_connection = NULL; + + +/* + * Internal Functions + */ +static gboolean __g_io_client_handler(GIOChannel *io, GIOCondition cond, void *data) +{ + int fd; + serial_s *pHandle = (serial_s *)data; + if (pHandle == NULL) + return FALSE; + + if (pHandle->data_handler.callback) { + char buffer[SERIAL_BUF_SIZE] = { 0 }; + int len = 0; + fd = g_io_channel_unix_get_fd(io); + len = recv(fd, buffer, SERIAL_BUF_SIZE, 0); + if(len <= 0) { + ERR("Error occured or the peer is shutdownd. [%d]\n", len); + ((serial_state_changed_cb)pHandle->state_handler.callback) + (SERIAL_ERROR_NONE, + SERIAL_STATE_CLOSED, + pHandle->state_handler.user_data); + return FALSE; + } + + ((serial_data_received_cb)pHandle->data_handler.callback) + (buffer, len, pHandle->data_handler.user_data); + } + return TRUE; +} + +static void __init_client_giochannel(void *data) +{ + GIOChannel *io; + serial_s *pHandle = (serial_s *)data; + if (pHandle == NULL) + return; + + io = g_io_channel_unix_new(pHandle->client_socket); + g_io_channel_set_close_on_unref(io, TRUE); + pHandle->g_watch_id = g_io_add_watch(io, + G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, + __g_io_client_handler, pHandle); + g_io_channel_unref(io); + return; +} + +static int __connect_to_serial_server(void *data) +{ + int client_socket = -1; + struct sockaddr_un server_addr; + serial_s *pHandle = (serial_s *)data; + if (pHandle == NULL) + return -1; + + client_socket = socket(AF_UNIX, SOCK_STREAM, 0); + if (client_socket < 0) { + ERR("Create socket failed\n"); + return -1; + } + + memset(&server_addr, 0, sizeof(server_addr)); + server_addr.sun_family = AF_UNIX; + g_strlcpy(server_addr.sun_path, SERIAL_SOCKET_PATH, sizeof(server_addr.sun_path)); + + if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { + ERR("Connect failed\n"); + close(client_socket); + return -1; + } + pHandle->client_socket = client_socket; + + __init_client_giochannel(pHandle); + + return client_socket; +} + + +static DBusHandlerResult __dbus_event_filter(DBusConnection *sys_conn, + DBusMessage *msg, void *data) +{ + static int socket = -1; + const char *path = dbus_message_get_path(msg); + + if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_SIGNAL) + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + if (path == NULL || strcmp(path, "/") == 0) + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + if (dbus_message_is_signal(msg, SERIAL_INTERFACE, "serial_status")) { + int res = 0; + dbus_message_get_args(msg, NULL, + DBUS_TYPE_INT32, &res, + DBUS_TYPE_INVALID); + + serial_s *pHandle = (serial_s *)data; + if (res == SERIAL_OPENED) { + socket = __connect_to_serial_server(pHandle); + if (socket < 0) { + ((serial_state_changed_cb)pHandle->state_handler.callback) + (SERIAL_ERROR_OPERATION_FAILED, + SERIAL_STATE_OPENED, + pHandle->state_handler.user_data); + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + ((serial_state_changed_cb)pHandle->state_handler.callback) + (SERIAL_ERROR_NONE, + SERIAL_STATE_OPENED, + pHandle->state_handler.user_data); + } else if (res == SERIAL_CLOSED) { + if (socket < 0) + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + ((serial_state_changed_cb)pHandle->state_handler.callback) + (SERIAL_ERROR_NONE, + SERIAL_STATE_CLOSED, + pHandle->state_handler.user_data); + } + } else { + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + return DBUS_HANDLER_RESULT_HANDLED; +} + +int __send_serial_ready_done_signal(void) +{ + DBusMessage *msg = NULL; + const char *res = "OK"; + + if(dbus_connection == NULL) + return SERIAL_ERROR_INVALID_OPERATION; + + msg = dbus_message_new_signal("/Network/Serial", + "Capi.Network.Serial", + "ready_for_serial"); + if (!msg) { + ERR("Unable to allocate D-Bus signal\n"); + return SERIAL_ERROR_OPERATION_FAILED; + } + + if (!dbus_message_append_args(msg, + DBUS_TYPE_STRING, &res, + DBUS_TYPE_INVALID)) { + ERR("Event sending failed\n"); + dbus_message_unref(msg); + return SERIAL_ERROR_OPERATION_FAILED; + } + + dbus_connection_send(dbus_connection, msg, NULL); + dbus_message_unref(msg); + return SERIAL_ERROR_NONE; +} + + +static int __serial_set_state_changed_cb(serial_h serial, void *callback, void *user_data) +{ + if (!serial) { + ERR("Invalid parameter\n"); + return SERIAL_ERROR_INVALID_PARAMETER; + } + + serial_s *pHandle = (serial_s *)serial; + + if (callback) { + pHandle->state_handler.callback = callback; + pHandle->state_handler.user_data = user_data; + } else { + pHandle->state_handler.callback = NULL; + pHandle->state_handler.user_data = NULL; + } + + return SERIAL_ERROR_NONE; +} + +static int __serial_set_data_received_cb(serial_h serial, void *callback, void *user_data) +{ + if (!serial) { + ERR("Invalid parameter\n"); + return SERIAL_ERROR_INVALID_PARAMETER; + } + + serial_s *pHandle = (serial_s *)serial; + + if (callback) { + pHandle->data_handler.callback = callback; + pHandle->data_handler.user_data = user_data; + } else { + pHandle->data_handler.callback = NULL; + pHandle->data_handler.user_data = NULL; + } + + return SERIAL_ERROR_NONE; +} + + + +/* + * Public Functions + */ + +int serial_create(serial_h *serial) +{ + DBG("%s\n", __FUNCTION__); + + GError *error = NULL; + DBusError dbus_error; + serial_s *pHandle = NULL; + + if (serial == NULL) + return SERIAL_ERROR_INVALID_PARAMETER; + + pHandle = (serial_s *)g_try_malloc0(sizeof(serial_s)); + if (pHandle == NULL) + return SERIAL_ERROR_OUT_OF_MEMORY; + + g_type_init(); + + pHandle->client_bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); + if (error) { + ERR("Couldn't connect to the System bus[%s]", + error->message); + g_error_free(error); + g_free(pHandle); + return SERIAL_ERROR_OPERATION_FAILED; + } + dbus_connection = dbus_g_connection_get_connection(pHandle->client_bus); + + /* Add the filter for network client functions */ + dbus_error_init(&dbus_error); + dbus_connection_add_filter(dbus_connection, __dbus_event_filter, pHandle, NULL); + dbus_bus_add_match(dbus_connection, + "type=signal,interface=" SERIAL_INTERFACE + ",member=serial_status", &dbus_error); + if (dbus_error_is_set(&dbus_error)) { + ERR("Fail to add dbus filter signal\n"); + dbus_error_free(&dbus_error); + g_free(pHandle); + return SERIAL_ERROR_OPERATION_FAILED; + } + + *serial = (serial_h)pHandle; + + return SERIAL_ERROR_NONE; +} + + +int serial_open(serial_h serial) +{ + DBG("%s\n", __FUNCTION__); + + if (!serial) { + ERR("Invalid parameter\n"); + return SERIAL_ERROR_INVALID_PARAMETER; + } + + return __send_serial_ready_done_signal(); +} + +int serial_close(serial_h serial) +{ + DBG("%s\n", __FUNCTION__); + + if (!serial) { + ERR("Invalid parameter\n"); + return SERIAL_ERROR_INVALID_PARAMETER; + } + + serial_s *pHandle = (serial_s *)serial; + + if (pHandle->client_socket > 0) { + if (close(pHandle->client_socket) < 0) + return SERIAL_ERROR_OPERATION_FAILED; + + pHandle->client_socket = -1; + + return SERIAL_ERROR_NONE; + } else { + return SERIAL_ERROR_INVALID_OPERATION; + } +} + +int serial_destroy(serial_h serial) +{ + DBG("%s\n", __FUNCTION__); + + if (!serial) { + ERR("Invalid parameter\n"); + return SERIAL_ERROR_INVALID_PARAMETER; + } + + serial_s *pHandle = (serial_s *)serial; + + if (dbus_connection != NULL) { + dbus_connection_remove_filter(dbus_connection, __dbus_event_filter, pHandle); + dbus_connection = NULL; + } + + if (pHandle->client_bus != NULL) { + dbus_g_connection_unref(pHandle->client_bus); + pHandle->client_bus = NULL; + } + + if (pHandle->g_watch_id > 0) { + g_source_remove(pHandle->g_watch_id); + pHandle->g_watch_id = -1; + } + + if (pHandle->client_socket > 0) { + close(pHandle->client_socket); + pHandle->client_socket = -1; + } + + g_free(pHandle); + serial = NULL; + + return SERIAL_ERROR_NONE; +} + +int serial_write(serial_h serial, const char *data, int data_length) +{ + if (!serial) { + ERR("Invalid parameter\n"); + return SERIAL_ERROR_INVALID_PARAMETER; + } + int ret; + serial_s *pHandle = (serial_s *)serial; + + ret = send(pHandle->client_socket, data, data_length, MSG_EOR); + if (ret == -1) { + ERR("Send failed. "); + return SERIAL_ERROR_OPERATION_FAILED; + } + + return ret; +} + +int serial_set_state_changed_cb(serial_h serial, serial_state_changed_cb callback, void *user_data) +{ + DBG("%s\n", __FUNCTION__); + + if (!serial || !callback) { + ERR("Invalid parameter\n"); + return SERIAL_ERROR_INVALID_PARAMETER; + } + + return (__serial_set_state_changed_cb(serial, callback, user_data)); +} + +int serial_unset_state_changed_cb(serial_h serial) +{ + DBG("%s\n", __FUNCTION__); + + if (!serial) { + ERR("Invalid parameter\n"); + return SERIAL_ERROR_INVALID_PARAMETER; + } + + return (__serial_set_state_changed_cb(serial, NULL, NULL)); +} + +int serial_set_data_received_cb(serial_h serial, serial_data_received_cb callback, void *user_data) +{ + DBG("%s\n", __FUNCTION__); + + if (!serial || !callback) { + ERR("Invalid parameter\n"); + return SERIAL_ERROR_INVALID_PARAMETER; + } + + return (__serial_set_data_received_cb(serial, callback, user_data)); +} + +int serial_unset_data_received_cb(serial_h serial) +{ + DBG("%s\n", __FUNCTION__); + + if (!serial) { + ERR("Invalid parameter\n"); + return SERIAL_ERROR_INVALID_PARAMETER; + } + + return (__serial_set_data_received_cb(serial, NULL, NULL)); +} -- 2.7.4