From 76465a52999fcbe37dadd3d24bf29ab015501097 Mon Sep 17 00:00:00 2001 From: "jino.cho" Date: Fri, 12 May 2017 15:43:49 +0900 Subject: [PATCH] Add APIs and functions for uart This patch support uart device. And, it should work properly if the patch for peripheral-bus is applied together. Change-Id: I79777883a705824af7bf9329f3470bd51ffdfb7d Signed-off-by: jino.cho --- CMakeLists.txt | 1 + include/peripheral_gdbus.h | 1 + include/peripheral_gdbus_uart.h | 31 ++++ include/peripheral_io.h | 4 +- src/peripheral_gdbus_uart.c | 252 ++++++++++++++++++++++++++++++++ src/peripheral_io.xml | 49 +++++++ src/peripheral_uart.c | 121 +++++++++++++++ 7 files changed, 457 insertions(+), 2 deletions(-) create mode 100644 include/peripheral_gdbus_uart.h create mode 100644 src/peripheral_gdbus_uart.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bbc0bd..d051617 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ SET(SOURCES src/peripheral_gpio.c src/peripheral_gdbus_gpio.c src/peripheral_gdbus_i2c.c src/peripheral_gdbus_pwm.c + src/peripheral_gdbus_uart.c src/peripheral_io_gdbus.c src/peripheral_spi.c) diff --git a/include/peripheral_gdbus.h b/include/peripheral_gdbus.h index 2195320..aefe770 100644 --- a/include/peripheral_gdbus.h +++ b/include/peripheral_gdbus.h @@ -22,6 +22,7 @@ #define PERIPHERAL_GDBUS_GPIO_PATH "/Org/Tizen/Peripheral_io/Gpio" #define PERIPHERAL_GDBUS_I2C_PATH "/Org/Tizen/Peripheral_io/I2c" #define PERIPHERAL_GDBUS_PWM_PATH "/Org/Tizen/Peripheral_io/Pwm" +#define PERIPHERAL_GDBUS_UART_PATH "/Org/Tizen/Peripheral_io/Uart" #define PERIPHERAL_GDBUS_NAME "org.tizen.peripheral_io" #endif /* __PERIPHERAL_GDBUS_H__ */ diff --git a/include/peripheral_gdbus_uart.h b/include/peripheral_gdbus_uart.h new file mode 100644 index 0000000..7c7f8e7 --- /dev/null +++ b/include/peripheral_gdbus_uart.h @@ -0,0 +1,31 @@ +/* + * 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_GDBUS_UART_H_ +#define __PERIPHERAL_GDBUS_UART_H_ + +void uart_proxy_init(void); +void uart_proxy_deinit(); + +int peripheral_gdbus_uart_open(peripheral_uart_h uart, int port); +int peripheral_gdbus_uart_close(peripheral_uart_h uart); +int peripheral_gdbus_uart_flush(peripheral_uart_h uart); +int peripheral_gdbus_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrate_e baud); +int peripheral_gdbus_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e bytesize, peripheral_uart_parity_e parity, peripheral_uart_stopbits_e stopbits); +int peripheral_gdbus_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool rtscts); +int peripheral_gdbus_uart_read(peripheral_uart_h uart, uint8_t *data, int length); +int peripheral_gdbus_uart_write(peripheral_uart_h uart, uint8_t *data, int length); + +#endif /* __PERIPHERAL_GDBUS_UART_H_ */ diff --git a/include/peripheral_io.h b/include/peripheral_io.h index 568c7a3..a8ed110 100644 --- a/include/peripheral_io.h +++ b/include/peripheral_io.h @@ -611,7 +611,7 @@ int peripheral_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool r * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error */ -int peripheral_uart_read(peripheral_uart_h uart, char *data, int length); +int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, int length); /** * @brief Write data to the uart device. @@ -627,7 +627,7 @@ int peripheral_uart_read(peripheral_uart_h uart, char *data, int length); * @retval #PERIPHERAL_ERROR_INVALID_PARAMETER Invalid parameter * @retval #PERIPHERAL_ERROR_UNKNOWN Unknown internal error */ -int peripheral_uart_write(peripheral_uart_h uart, const char *data, int length); +int peripheral_uart_write(peripheral_uart_h uart, uint8_t *data, int length); /** * @} diff --git a/src/peripheral_gdbus_uart.c b/src/peripheral_gdbus_uart.c new file mode 100644 index 0000000..fc94a89 --- /dev/null +++ b/src/peripheral_gdbus_uart.c @@ -0,0 +1,252 @@ +/* + * 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 "peripheral_io.h" +#include "peripheral_gdbus.h" +#include "peripheral_common.h" +#include "peripheral_internal.h" +#include "peripheral_io_gdbus.h" + +PeripheralIoGdbusUart *uart_proxy = NULL; + +void uart_proxy_init(void) +{ + GError *error = NULL; + + if (uart_proxy != NULL) + return; + + uart_proxy = peripheral_io_gdbus_uart_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + PERIPHERAL_GDBUS_NAME, + PERIPHERAL_GDBUS_UART_PATH, + NULL, + &error); +} + +void uart_proxy_deinit() +{ + if (uart_proxy) { + g_object_unref(uart_proxy); + uart_proxy = NULL; + } +} + +int peripheral_gdbus_uart_open(peripheral_uart_h uart, int port) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_open_sync( + uart_proxy, + port, + &uart->handle, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_gdbus_uart_close(peripheral_uart_h uart) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_close_sync( + uart_proxy, + uart->handle, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_gdbus_uart_flush(peripheral_uart_h uart) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_flush_sync( + uart_proxy, + uart->handle, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_gdbus_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrate_e baud) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_set_baudrate_sync( + uart_proxy, + uart->handle, + baud, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_gdbus_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e bytesize, peripheral_uart_parity_e parity, peripheral_uart_stopbits_e stopbits) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_set_mode_sync( + uart_proxy, + uart->handle, + bytesize, + parity, + stopbits, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_gdbus_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool rtscts) +{ + GError *error = NULL; + gint32 ret = PERIPHERAL_ERROR_NONE; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_set_flowcontrol_sync( + uart_proxy, + uart->handle, + xonxoff, + rtscts, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} + +int peripheral_gdbus_uart_read(peripheral_uart_h uart, uint8_t *data, int length) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + GVariant *data_array; + GVariantIter *iter; + guint8 str; + int i = 0; + + if (uart_proxy == NULL || data == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + if (peripheral_io_gdbus_uart_call_read_sync( + uart_proxy, + uart->handle, + length, + &data_array, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + g_variant_get(data_array, "a(y)", &iter); + while (g_variant_iter_loop(iter, "(y)", &str)) { + data[i] = str; + if (i++ == length) break; + } + g_variant_iter_free(iter); + + return ret; +} + +int peripheral_gdbus_uart_write(peripheral_uart_h uart, uint8_t *data, int length) +{ + GError *error = NULL; + peripheral_error_e ret = PERIPHERAL_ERROR_NONE; + GVariantBuilder *builder; + GVariant *g_data; + int i = 0; + + if (uart_proxy == NULL) return PERIPHERAL_ERROR_UNKNOWN; + + builder = g_variant_builder_new(G_VARIANT_TYPE("a(y)")); + + for (i = 0; i < length; i++) + g_variant_builder_add(builder, "(y)", data[i]); + g_variant_builder_add(builder, "(y)", 0x00); + + g_data = g_variant_new("a(y)", builder); + g_variant_builder_unref(builder); + + if (peripheral_io_gdbus_uart_call_write_sync( + uart_proxy, + uart->handle, + length, + g_data, + &ret, + NULL, + &error) == FALSE) { + _E("Error in %s() : %s\n", __func__, error->message); + g_error_free(error); + return PERIPHERAL_ERROR_UNKNOWN; + } + + return ret; +} diff --git a/src/peripheral_io.xml b/src/peripheral_io.xml index ba2edef..c6343c4 100644 --- a/src/peripheral_io.xml +++ b/src/peripheral_io.xml @@ -124,4 +124,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/peripheral_uart.c b/src/peripheral_uart.c index 4082df6..66d96f0 100644 --- a/src/peripheral_uart.c +++ b/src/peripheral_uart.c @@ -13,3 +13,124 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +#include +#include +#include +#include + +#include "peripheral_io.h" +#include "peripheral_gdbus_uart.h" +#include "peripheral_common.h" +#include "peripheral_internal.h" + +/** + * @brief Initializes uart communication and creates uart handle. + */ +int peripheral_uart_open(int port, peripheral_uart_h *uart) +{ + peripheral_uart_h handle; + int ret = PERIPHERAL_ERROR_NONE; + + if (port < 0) + return PERIPHERAL_ERROR_INVALID_PARAMETER; + + handle = (peripheral_uart_h)calloc(1, sizeof(struct _peripheral_uart_s)); + + if (handle == NULL) { + _E("Failed to allocate peripheral_uart_h"); + return PERIPHERAL_ERROR_OUT_OF_MEMORY; + } + + uart_proxy_init(); + + ret = peripheral_gdbus_uart_open(handle, port); + + if (ret != PERIPHERAL_ERROR_NONE) { + _E("[PERIPHERAL] UART open error\n"); + free(handle); + handle = NULL; + } + *uart = handle; + + return ret; +} + +/** + * @brief Destory the uart handle and release the communication. + */ +int peripheral_uart_close(peripheral_uart_h uart) +{ + int ret = PERIPHERAL_ERROR_NONE; + + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + ret = peripheral_gdbus_uart_close(uart); + uart_proxy_deinit(); + + free(uart); + uart = NULL; + + return ret; +} + +/** + * @brief Flush all input that has received but not yet been read by the uart + * device, or all output written but not transmitted to the uart device. + */ +int peripheral_uart_flush(peripheral_uart_h uart) +{ + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_uart_flush(uart); +} + +/** + * @brief Sets baudrate of the uart device. + */ +int peripheral_uart_set_baudrate(peripheral_uart_h uart, peripheral_uart_baudrate_e baud) +{ + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_uart_set_baudrate(uart, baud); +} + +/** + * @brief Sets baudrate of the uart device. + */ +int peripheral_uart_set_mode(peripheral_uart_h uart, peripheral_uart_bytesize_e bytesize, peripheral_uart_parity_e parity, peripheral_uart_stopbits_e stopbits) +{ + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_uart_set_mode(uart, bytesize, parity, stopbits); +} + +/** + * @brief Sets baudrate of the uart device. + */ +int peripheral_uart_set_flowcontrol(peripheral_uart_h uart, bool xonxoff, bool rtscts) +{ + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_uart_set_flowcontrol(uart, xonxoff, rtscts); +} + +/** + * @brief Reads data from the uart device. + */ +int peripheral_uart_read(peripheral_uart_h uart, uint8_t *data, int length) +{ + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_uart_read(uart, data, length); +} + +/** + * @brief Write data to the uart device. + */ +int peripheral_uart_write(peripheral_uart_h uart, uint8_t *data, int length) +{ + if (uart == NULL) return PERIPHERAL_ERROR_INVALID_PARAMETER; + + return peripheral_gdbus_uart_write(uart, data, length); +} -- 2.34.1