INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include/handle)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include/gdbus)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include/interface)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include/util)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/gdbus)
SET(PERIPHERAL-BUS "peripheral-bus")
SET(SRCS
src/peripheral_bus.c
- src/peripheral_bus_board.c
- src/privilege_checker.c
src/gdbus/peripheral_io_gdbus.c
src/gdbus/peripheral_gdbus_gpio.c
src/gdbus/peripheral_gdbus_i2c.c
src/interface/peripheral_interface_i2c.c
src/interface/peripheral_interface_pwm.c
src/interface/peripheral_interface_uart.c
- src/interface/peripheral_interface_spi.c)
+ src/interface/peripheral_interface_spi.c
+ src/util/peripheral_board.c
+ src/util/peripheral_privilege.c)
INCLUDE(FindPkgConfig)
pkg_check_modules(pbus_pkgs REQUIRED ${dependents})
#include <gio/gio.h>
+#include "peripheral_board.h"
#include "peripheral_io_gdbus.h"
-#include "peripheral_bus_board.h"
typedef enum {
PERIPHERAL_BUS_TYPE_GPIO = 0,
+++ /dev/null
-/*
- * 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_BUS_BOARD_H__
-#define __PERIPHERAL_BUS_BOARD_H__
-
-#define BOARD_DEVICE_TREE "/proc/device-tree/model"
-#define BOARD_PINS_MAX 4
-#define BOARD_ARGS_MAX 2
-
-typedef enum {
- PB_BOARD_ARTIK710 = 0,
- PB_BOARD_ARTIK530,
- PB_BOARD_ARTIK520,
- PB_BOARD_RP3_B,
- PB_BOARD_UNKNOWN,
-} pb_board_type_e;
-
-typedef enum {
- PB_BOARD_DEV_GPIO = 0,
- PB_BOARD_DEV_I2C,
- PB_BOARD_DEV_PWM,
- PB_BOARD_DEV_UART,
- PB_BOARD_DEV_SPI,
- PB_BOARD_DEV_MAX,
-} pb_board_dev_e;
-
-typedef struct {
- pb_board_type_e type;
- char *name;
- char *path;
-} pb_board_type_s;
-
-typedef struct {
- pb_board_dev_e dev_type;
- unsigned int pins[BOARD_PINS_MAX];
- unsigned int num_pins;
- unsigned int args[BOARD_ARGS_MAX];
-} pb_board_dev_s;
-
-typedef struct {
- pb_board_type_e type;
- pb_board_dev_s *dev;
- unsigned int num_dev;
-} pb_board_s;
-
-pb_board_dev_s *peripheral_bus_board_find_device(pb_board_dev_e dev_type, pb_board_s *board, int arg, ...);
-pb_board_s *peripheral_bus_board_init(void);
-void peripheral_bus_board_deinit(pb_board_s *board);
-
-#endif /* __PERIPHERAL_BUS_BOARD_H__ */
+++ /dev/null
-/*
- * 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_LOG_H__
-#define __PERIPHERAL_LOG_H__
-
-// #include <unistd.h>
-#include <dlog.h>
-
-#undef LOG_TAG
-#define LOG_TAG "PERIPHERAL-BUS"
-
-#define _E(fmt, arg...) LOGE(fmt, ##arg)
-#define _D(fmt, arg...) LOGD(fmt, ##arg)
-#define _W(fmt, arg...) LOGW(fmt, ##arg)
-
-#define RET_IF(expr) \
- do { \
- if (expr) { \
- _E("(%s)", #expr); \
- return; \
- } \
- } while (0)
-#define RETV_IF(expr, val) \
- do {\
- if (expr) { \
- _E("(%s)", #expr); \
- return (val); \
- } \
- } while (0)
-#define RETM_IF(expr, fmt, arg...) \
- do {\
- if (expr) { \
- _E(fmt, ##arg); \
- return; \
- } \
- } while (0)
-#define RETVM_IF(expr, val, fmt, arg...) \
- do {\
- if (expr) { \
- _E(fmt, ##arg); \
- return (val); \
- } \
- } while (0)
-
-#endif /* __PERIPHERAL_COMMON_H__ */
+++ /dev/null
-/*
- * 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 __PRIVILEGE_CHECKER_H__
-#define __PRIVILEGE_CHECKER_H__
-
-#include <gio/gio.h>
-
-void peripheral_privilege_init(void);
-void peripheral_privilege_deinit(void);
-int peripheral_privilege_check(GDBusMethodInvocation *invocation, GDBusConnection *connection);
-
-#endif /* __PRIVILEGE_CHECKER_H__ */
--- /dev/null
+/*
+ * 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_BUS_BOARD_H__
+#define __PERIPHERAL_BUS_BOARD_H__
+
+#define BOARD_DEVICE_TREE "/proc/device-tree/model"
+#define BOARD_PINS_MAX 4
+#define BOARD_ARGS_MAX 2
+
+typedef enum {
+ PB_BOARD_ARTIK710 = 0,
+ PB_BOARD_ARTIK530,
+ PB_BOARD_ARTIK520,
+ PB_BOARD_RP3_B,
+ PB_BOARD_UNKNOWN,
+} pb_board_type_e;
+
+typedef enum {
+ PB_BOARD_DEV_GPIO = 0,
+ PB_BOARD_DEV_I2C,
+ PB_BOARD_DEV_PWM,
+ PB_BOARD_DEV_UART,
+ PB_BOARD_DEV_SPI,
+ PB_BOARD_DEV_MAX,
+} pb_board_dev_e;
+
+typedef struct {
+ pb_board_type_e type;
+ char *name;
+ char *path;
+} pb_board_type_s;
+
+typedef struct {
+ pb_board_dev_e dev_type;
+ unsigned int pins[BOARD_PINS_MAX];
+ unsigned int num_pins;
+ unsigned int args[BOARD_ARGS_MAX];
+} pb_board_dev_s;
+
+typedef struct {
+ pb_board_type_e type;
+ pb_board_dev_s *dev;
+ unsigned int num_dev;
+} pb_board_s;
+
+pb_board_dev_s *peripheral_bus_board_find_device(pb_board_dev_e dev_type, pb_board_s *board, int arg, ...);
+pb_board_s *peripheral_bus_board_init(void);
+void peripheral_bus_board_deinit(pb_board_s *board);
+
+#endif /* __PERIPHERAL_BUS_BOARD_H__ */
--- /dev/null
+/*
+ * 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_LOG_H__
+#define __PERIPHERAL_LOG_H__
+
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "PERIPHERAL-BUS"
+
+#define _E(fmt, arg...) LOGE(fmt, ##arg)
+#define _D(fmt, arg...) LOGD(fmt, ##arg)
+#define _W(fmt, arg...) LOGW(fmt, ##arg)
+
+#define RET_IF(expr) \
+ do { \
+ if (expr) { \
+ _E("(%s)", #expr); \
+ return; \
+ } \
+ } while (0)
+#define RETV_IF(expr, val) \
+ do {\
+ if (expr) { \
+ _E("(%s)", #expr); \
+ return (val); \
+ } \
+ } while (0)
+#define RETM_IF(expr, fmt, arg...) \
+ do {\
+ if (expr) { \
+ _E(fmt, ##arg); \
+ return; \
+ } \
+ } while (0)
+#define RETVM_IF(expr, val, fmt, arg...) \
+ do {\
+ if (expr) { \
+ _E(fmt, ##arg); \
+ return (val); \
+ } \
+ } while (0)
+
+#endif /* __PERIPHERAL_COMMON_H__ */
--- /dev/null
+/*
+ * 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 __PRIVILEGE_CHECKER_H__
+#define __PRIVILEGE_CHECKER_H__
+
+#include <gio/gio.h>
+
+void peripheral_privilege_init(void);
+void peripheral_privilege_deinit(void);
+int peripheral_privilege_check(GDBusMethodInvocation *invocation, GDBusConnection *connection);
+
+#endif /* __PRIVILEGE_CHECKER_H__ */
#include <peripheral_io.h>
#include <gio/gunixfdlist.h>
+#include "peripheral_log.h"
+#include "peripheral_privilege.h"
#include "peripheral_io_gdbus.h"
#include "peripheral_handle.h"
#include "peripheral_handle_gpio.h"
-#include "peripheral_log.h"
#include "peripheral_gdbus_gpio.h"
-#include "privilege_checker.h"
static void __gpio_on_name_vanished(GDBusConnection *connection,
const gchar *name,
#include <peripheral_io.h>
+#include "peripheral_log.h"
+#include "peripheral_privilege.h"
#include "peripheral_io_gdbus.h"
#include "peripheral_handle.h"
#include "peripheral_handle_i2c.h"
-#include "peripheral_log.h"
#include "peripheral_gdbus_i2c.h"
-#include "privilege_checker.h"
+
static void __i2c_on_name_vanished(GDBusConnection *connection,
const gchar *name,
#include <peripheral_io.h>
+#include "peripheral_log.h"
+#include "peripheral_privilege.h"
#include "peripheral_io_gdbus.h"
#include "peripheral_handle.h"
#include "peripheral_handle_pwm.h"
-#include "peripheral_log.h"
#include "peripheral_gdbus_pwm.h"
-#include "privilege_checker.h"
static void __pwm_on_name_vanished(GDBusConnection *connection,
const gchar *name,
#include <peripheral_io.h>
+#include "peripheral_log.h"
+#include "peripheral_privilege.h"
#include "peripheral_io_gdbus.h"
#include "peripheral_handle.h"
#include "peripheral_handle_spi.h"
-#include "peripheral_log.h"
#include "peripheral_gdbus_spi.h"
-#include "privilege_checker.h"
static void __spi_on_name_vanished(GDBusConnection *connection,
const gchar *name,
#include <peripheral_io.h>
+#include "peripheral_log.h"
+#include "peripheral_privilege.h"
#include "peripheral_io_gdbus.h"
#include "peripheral_handle.h"
#include "peripheral_handle_uart.h"
-#include "peripheral_log.h"
#include "peripheral_gdbus_uart.h"
-#include "privilege_checker.h"
static void __uart_on_name_vanished(GDBusConnection *connection,
const gchar *name,
#include <peripheral_io.h>
-#include "peripheral_io_gdbus.h"
-#include "peripheral_handle.h"
#include "peripheral_log.h"
+#include "peripheral_privilege.h"
+#include "peripheral_handle.h"
+#include "peripheral_io_gdbus.h"
#include "peripheral_gdbus_gpio.h"
#include "peripheral_gdbus_i2c.h"
#include "peripheral_gdbus_pwm.h"
#include "peripheral_gdbus_spi.h"
#include "peripheral_gdbus_uart.h"
-#include "privilege_checker.h"
-
#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"
+++ /dev/null
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <iniparser.h>
-
-#include <peripheral_io.h>
-
-#include "peripheral_bus_board.h"
-#include "peripheral_log.h"
-
-#define STR_BUF_MAX 255
-
-#define BOARD_INI_BASE SYSCONFDIR "/peripheral-bus/"
-
-#define BOARD_INI_ARTIK710_PATH BOARD_INI_BASE "pio_board_artik710.ini"
-#define BOARD_INI_ARTIK520_PATH BOARD_INI_BASE "pio_board_artik520.ini"
-#define BOARD_INI_RP3_B_PATH BOARD_INI_BASE "pio_board_rp3_b.ini"
-#define BOARD_INI_UNKNOWN_PATH BOARD_INI_BASE "pio_board_unknown.ini"
-
-static const pb_board_type_s pb_board_type[] = {
- {PB_BOARD_ARTIK710, "artik710 raptor", BOARD_INI_ARTIK710_PATH},
- {PB_BOARD_ARTIK530, "artik530 raptor", BOARD_INI_ARTIK710_PATH},
- {PB_BOARD_ARTIK520, "ARTIK5 board", BOARD_INI_ARTIK520_PATH},
- {PB_BOARD_RP3_B, "Raspberry Pi 3 Model B", BOARD_INI_RP3_B_PATH},
- {PB_BOARD_UNKNOWN, "unknown board", BOARD_INI_UNKNOWN_PATH},
-};
-
-static int peripheral_bus_board_get_device_type(char *string)
-{
- if (0 == strncmp(string, "gpio", strlen("gpio")))
- return PB_BOARD_DEV_GPIO;
- else if (0 == strncmp(string, "i2c", strlen("i2c")))
- return PB_BOARD_DEV_I2C;
- else if (0 == strncmp(string, "pwm", strlen("pwm")))
- return PB_BOARD_DEV_PWM;
- else if (0 == strncmp(string, "uart", strlen("uart")))
- return PB_BOARD_DEV_UART;
- else if (0 == strncmp(string, "spi", strlen("spi")))
- return PB_BOARD_DEV_SPI;
-
- return -1;
-}
-
-static void peripheral_bus_board_ini_parse_key(pb_board_dev_e type, char *string, unsigned int *args)
-{
- switch (type) {
- case PB_BOARD_DEV_GPIO:
- sscanf(string, "%*50[^0-9]%d", &args[0]);
- break;
- case PB_BOARD_DEV_I2C:
- sscanf(string, "%*50[^-]-%d", &args[0]);
- break;
- case PB_BOARD_DEV_PWM:
- sscanf(string, "%*50[^0-9]%d%*50[^0-9]%d", &args[0], &args[1]);
- break;
- case PB_BOARD_DEV_UART:
- sscanf(string, "%*50[^0-9]%d", &args[0]);
- break;
- case PB_BOARD_DEV_SPI:
- sscanf(string, "%*50[^0-9]%d.%d", &args[0], &args[1]);
- break;
- default:
- break;
- }
-}
-
-static int peripheral_bus_board_ini_parse_pins(char *string, unsigned int *pins)
-{
- const char delimiter[] = ", ";
- int cnt_pins = 0;
- char *token, *ptr = NULL;
-
- if (string == NULL) return 0;
-
- token = strtok_r(string, delimiter, &ptr);
- while (token) {
- if (cnt_pins >= BOARD_PINS_MAX) break;
-
- pins[cnt_pins++] = atoi(token);
- token = strtok_r(NULL, delimiter, &ptr);
- }
-
- return cnt_pins;
-}
-
-static int peripheral_bus_board_ini_get_nkeys(dictionary *dict)
-{
- int i, sec_num, key_num, ret = 0;
- char *section;
-
- sec_num = iniparser_getnsec(dict);
- for (i = 0; i < sec_num; i++) {
- section = iniparser_getsecname(dict, i);
- key_num = iniparser_getsecnkeys(dict, section);
- if (key_num <= 0) continue;
- ret += key_num;
- }
-
- return ret;
-}
-
-static int peripheral_bus_board_get_type(void)
-{
- int fd, i, ret = 0;
- char str_buf[STR_BUF_MAX] = {0};
- int type = PB_BOARD_UNKNOWN;
-
- fd = open(BOARD_DEVICE_TREE, O_RDONLY);
- if (fd < 0) {
- strerror_r(errno, str_buf, STR_BUF_MAX);
- _E("Cannot open %s, errmsg : %s", BOARD_DEVICE_TREE, str_buf);
- return -ENXIO;
- }
-
- ret = read(fd, str_buf, STR_BUF_MAX);
- if (ret < 0) {
- _E("Failed to read model information, path: %s, ret: %d", BOARD_DEVICE_TREE, ret);
- close(fd);
- return -EIO;
- }
-
- str_buf[STR_BUF_MAX - 1] = '\0';
-
- for (i = 0; i < PB_BOARD_UNKNOWN; i++) {
- if (strstr(str_buf, pb_board_type[i].name)) {
- type = pb_board_type[i].type;
- break;
- }
- }
-
- close(fd);
-
- return type;
-}
-
-static pb_board_s *peripheral_bus_board_get_info()
-{
- dictionary *dict = NULL;
- int i, j, ret;
- int sec_num, key_num, cnt_key = 0;
- pb_board_dev_s *dev;
- pb_board_dev_e enum_dev;
- pb_board_s *board;
-
- board = (pb_board_s*)calloc(1, sizeof(pb_board_s));
- if (board == NULL) {
- _E("Failed to allocate pb_board_s");
- return NULL;
- }
-
- ret = peripheral_bus_board_get_type();
- if (ret < 0) {
- _E("Failed to get board type");
- goto err_get_type;
- }
-
- board->type = (pb_board_type_e)ret;
- dict = iniparser_load(pb_board_type[board->type].path);
- if (dict == NULL) {
- _E("Failed to load %s", pb_board_type[board->type].path);
- goto err_get_type;
- }
-
- board->num_dev = peripheral_bus_board_ini_get_nkeys(dict);
- if (board->num_dev == 0) {
- _E("There is no device to open");
- goto err_get_nkeys;
- }
-
- board->dev = calloc(board->num_dev, sizeof(pb_board_dev_s));
- if (board->dev == NULL) {
- _E("Failed to allocate pb_board_dev_s");
- goto err_get_nkeys;
- }
-
- sec_num = iniparser_getnsec(dict);
- for (i = 0; i < sec_num; i++) {
- char *section, *key_str;
- char **key_list = NULL;
-
- section = iniparser_getsecname(dict, i);
- ret = peripheral_bus_board_get_device_type(section);
- if (ret < 0)
- continue;
-
- enum_dev = (pb_board_dev_e)ret;
- key_list = iniparser_getseckeys(dict, section);
- key_num = iniparser_getsecnkeys(dict, section);
- if (key_num <= 0)
- continue;
-
- for (j = 0; j < key_num; j++) {
- dev = &board->dev[cnt_key];
- dev->dev_type = enum_dev;
- key_str = iniparser_getstring(dict, key_list[j], NULL);
- peripheral_bus_board_ini_parse_key(dev->dev_type, key_list[j], dev->args);
- dev->num_pins = peripheral_bus_board_ini_parse_pins(key_str, dev->pins);
- cnt_key++;
- }
- }
-
- iniparser_freedict(dict);
-
- return board;
-
-err_get_nkeys:
- iniparser_freedict(dict);
-
-err_get_type:
- free(board);
- return NULL;
-}
-
-pb_board_dev_s *peripheral_bus_board_find_device(pb_board_dev_e dev_type, pb_board_s *board, int arg, ...)
-{
- int i, args[2] = {0, };
- va_list ap;
-
- RETV_IF(board == NULL, false);
-
- args[0] = arg;
- if (dev_type == PB_BOARD_DEV_PWM || dev_type == PB_BOARD_DEV_SPI) {
- va_start(ap, arg);
- args[1] = va_arg(ap, int);
- va_end(ap);
- }
-
- for (i = 0; i < board->num_dev; i++) {
- if (board->dev[i].dev_type != dev_type) continue;
-
- if (board->dev[i].args[0] == args[0] && board->dev[i].args[1] == args[1])
- return &board->dev[i];
- }
-
- return NULL;
-}
-
-pb_board_s *peripheral_bus_board_init(void)
-{
- pb_board_s *board;
-
- board = peripheral_bus_board_get_info();
-
- return board;
-}
-
-void peripheral_bus_board_deinit(pb_board_s *board)
-{
- if (board) {
- if (board->dev)
- free(board->dev);
-
- free(board);
- }
-}
+++ /dev/null
-/*
- * 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 <cynara-creds-gdbus.h>
-#include <cynara-client.h>
-#include <cynara-session.h>
-
-#include "privilege_checker.h"
-#include "peripheral_log.h"
-
-#define PERIPHERAL_PRIVILEGE "http://tizen.org/privilege/peripheralio"
-
-#define CACHE_SIZE 100
-
-static cynara *__cynara;
-
-void peripheral_privilege_init(void)
-{
- int err;
- cynara_configuration* conf = NULL;
-
- err = cynara_configuration_create(&conf);
- RETM_IF(err != CYNARA_API_SUCCESS, "Failed to create cynara configuration");
-
- err = cynara_configuration_set_cache_size(conf, CACHE_SIZE);
- if (err != CYNARA_API_SUCCESS) {
- _E("Failed to set cynara cache size");
- cynara_configuration_destroy(conf);
- return;
- }
-
- err = cynara_initialize(&__cynara, conf);
- cynara_configuration_destroy(conf);
- if (err != CYNARA_API_SUCCESS) {
- _E("Failed to initialize cynara");
- __cynara = NULL;
- return;
- }
-
- _D("Cynara initialized");
-}
-
-void peripheral_privilege_deinit(void)
-{
- if (__cynara)
- cynara_finish(__cynara);
-
- _D("Cynara deinitialized");
-}
-
-int peripheral_privilege_check(GDBusMethodInvocation *invocation, GDBusConnection *connection)
-{
- int ret;
- int pid;
- const char *sender;
- char *session;
- char *client;
- char *user;
-
- sender = g_dbus_method_invocation_get_sender(invocation);
-
- cynara_creds_gdbus_get_pid(connection, sender, &pid);
- session = cynara_session_from_pid(pid);
-
- cynara_creds_gdbus_get_client(connection, sender, CLIENT_METHOD_DEFAULT, &client);
- cynara_creds_gdbus_get_user(connection, sender, USER_METHOD_DEFAULT, &user);
-
- if (!session || !client || !user) {
- _E("Failed to get client info");
- return -1;
- }
-
- ret = cynara_check(__cynara, client, session, user, PERIPHERAL_PRIVILEGE);
- if (ret != 0) {
- g_free(session);
- g_free(client);
- g_free(user);
- return -EACCES;
- }
-
- g_free(session);
- g_free(client);
- g_free(user);
-
- return 0;
-}
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <iniparser.h>
+
+#include <peripheral_io.h>
+
+#include "peripheral_board.h"
+#include "peripheral_log.h"
+
+#define STR_BUF_MAX 255
+
+#define BOARD_INI_BASE SYSCONFDIR "/peripheral-bus/"
+
+#define BOARD_INI_ARTIK710_PATH BOARD_INI_BASE "pio_board_artik710.ini"
+#define BOARD_INI_ARTIK520_PATH BOARD_INI_BASE "pio_board_artik520.ini"
+#define BOARD_INI_RP3_B_PATH BOARD_INI_BASE "pio_board_rp3_b.ini"
+#define BOARD_INI_UNKNOWN_PATH BOARD_INI_BASE "pio_board_unknown.ini"
+
+static const pb_board_type_s pb_board_type[] = {
+ {PB_BOARD_ARTIK710, "artik710 raptor", BOARD_INI_ARTIK710_PATH},
+ {PB_BOARD_ARTIK530, "artik530 raptor", BOARD_INI_ARTIK710_PATH},
+ {PB_BOARD_ARTIK520, "ARTIK5 board", BOARD_INI_ARTIK520_PATH},
+ {PB_BOARD_RP3_B, "Raspberry Pi 3 Model B", BOARD_INI_RP3_B_PATH},
+ {PB_BOARD_UNKNOWN, "unknown board", BOARD_INI_UNKNOWN_PATH},
+};
+
+static int peripheral_bus_board_get_device_type(char *string)
+{
+ if (0 == strncmp(string, "gpio", strlen("gpio")))
+ return PB_BOARD_DEV_GPIO;
+ else if (0 == strncmp(string, "i2c", strlen("i2c")))
+ return PB_BOARD_DEV_I2C;
+ else if (0 == strncmp(string, "pwm", strlen("pwm")))
+ return PB_BOARD_DEV_PWM;
+ else if (0 == strncmp(string, "uart", strlen("uart")))
+ return PB_BOARD_DEV_UART;
+ else if (0 == strncmp(string, "spi", strlen("spi")))
+ return PB_BOARD_DEV_SPI;
+
+ return -1;
+}
+
+static void peripheral_bus_board_ini_parse_key(pb_board_dev_e type, char *string, unsigned int *args)
+{
+ switch (type) {
+ case PB_BOARD_DEV_GPIO:
+ sscanf(string, "%*50[^0-9]%d", &args[0]);
+ break;
+ case PB_BOARD_DEV_I2C:
+ sscanf(string, "%*50[^-]-%d", &args[0]);
+ break;
+ case PB_BOARD_DEV_PWM:
+ sscanf(string, "%*50[^0-9]%d%*50[^0-9]%d", &args[0], &args[1]);
+ break;
+ case PB_BOARD_DEV_UART:
+ sscanf(string, "%*50[^0-9]%d", &args[0]);
+ break;
+ case PB_BOARD_DEV_SPI:
+ sscanf(string, "%*50[^0-9]%d.%d", &args[0], &args[1]);
+ break;
+ default:
+ break;
+ }
+}
+
+static int peripheral_bus_board_ini_parse_pins(char *string, unsigned int *pins)
+{
+ const char delimiter[] = ", ";
+ int cnt_pins = 0;
+ char *token, *ptr = NULL;
+
+ if (string == NULL) return 0;
+
+ token = strtok_r(string, delimiter, &ptr);
+ while (token) {
+ if (cnt_pins >= BOARD_PINS_MAX) break;
+
+ pins[cnt_pins++] = atoi(token);
+ token = strtok_r(NULL, delimiter, &ptr);
+ }
+
+ return cnt_pins;
+}
+
+static int peripheral_bus_board_ini_get_nkeys(dictionary *dict)
+{
+ int i, sec_num, key_num, ret = 0;
+ char *section;
+
+ sec_num = iniparser_getnsec(dict);
+ for (i = 0; i < sec_num; i++) {
+ section = iniparser_getsecname(dict, i);
+ key_num = iniparser_getsecnkeys(dict, section);
+ if (key_num <= 0) continue;
+ ret += key_num;
+ }
+
+ return ret;
+}
+
+static int peripheral_bus_board_get_type(void)
+{
+ int fd, i, ret = 0;
+ char str_buf[STR_BUF_MAX] = {0};
+ int type = PB_BOARD_UNKNOWN;
+
+ fd = open(BOARD_DEVICE_TREE, O_RDONLY);
+ if (fd < 0) {
+ strerror_r(errno, str_buf, STR_BUF_MAX);
+ _E("Cannot open %s, errmsg : %s", BOARD_DEVICE_TREE, str_buf);
+ return -ENXIO;
+ }
+
+ ret = read(fd, str_buf, STR_BUF_MAX);
+ if (ret < 0) {
+ _E("Failed to read model information, path: %s, ret: %d", BOARD_DEVICE_TREE, ret);
+ close(fd);
+ return -EIO;
+ }
+
+ str_buf[STR_BUF_MAX - 1] = '\0';
+
+ for (i = 0; i < PB_BOARD_UNKNOWN; i++) {
+ if (strstr(str_buf, pb_board_type[i].name)) {
+ type = pb_board_type[i].type;
+ break;
+ }
+ }
+
+ close(fd);
+
+ return type;
+}
+
+static pb_board_s *peripheral_bus_board_get_info()
+{
+ dictionary *dict = NULL;
+ int i, j, ret;
+ int sec_num, key_num, cnt_key = 0;
+ pb_board_dev_s *dev;
+ pb_board_dev_e enum_dev;
+ pb_board_s *board;
+
+ board = (pb_board_s*)calloc(1, sizeof(pb_board_s));
+ if (board == NULL) {
+ _E("Failed to allocate pb_board_s");
+ return NULL;
+ }
+
+ ret = peripheral_bus_board_get_type();
+ if (ret < 0) {
+ _E("Failed to get board type");
+ goto err_get_type;
+ }
+
+ board->type = (pb_board_type_e)ret;
+ dict = iniparser_load(pb_board_type[board->type].path);
+ if (dict == NULL) {
+ _E("Failed to load %s", pb_board_type[board->type].path);
+ goto err_get_type;
+ }
+
+ board->num_dev = peripheral_bus_board_ini_get_nkeys(dict);
+ if (board->num_dev == 0) {
+ _E("There is no device to open");
+ goto err_get_nkeys;
+ }
+
+ board->dev = calloc(board->num_dev, sizeof(pb_board_dev_s));
+ if (board->dev == NULL) {
+ _E("Failed to allocate pb_board_dev_s");
+ goto err_get_nkeys;
+ }
+
+ sec_num = iniparser_getnsec(dict);
+ for (i = 0; i < sec_num; i++) {
+ char *section, *key_str;
+ char **key_list = NULL;
+
+ section = iniparser_getsecname(dict, i);
+ ret = peripheral_bus_board_get_device_type(section);
+ if (ret < 0)
+ continue;
+
+ enum_dev = (pb_board_dev_e)ret;
+ key_list = iniparser_getseckeys(dict, section);
+ key_num = iniparser_getsecnkeys(dict, section);
+ if (key_num <= 0)
+ continue;
+
+ for (j = 0; j < key_num; j++) {
+ dev = &board->dev[cnt_key];
+ dev->dev_type = enum_dev;
+ key_str = iniparser_getstring(dict, key_list[j], NULL);
+ peripheral_bus_board_ini_parse_key(dev->dev_type, key_list[j], dev->args);
+ dev->num_pins = peripheral_bus_board_ini_parse_pins(key_str, dev->pins);
+ cnt_key++;
+ }
+ }
+
+ iniparser_freedict(dict);
+
+ return board;
+
+err_get_nkeys:
+ iniparser_freedict(dict);
+
+err_get_type:
+ free(board);
+ return NULL;
+}
+
+pb_board_dev_s *peripheral_bus_board_find_device(pb_board_dev_e dev_type, pb_board_s *board, int arg, ...)
+{
+ int i, args[2] = {0, };
+ va_list ap;
+
+ RETV_IF(board == NULL, false);
+
+ args[0] = arg;
+ if (dev_type == PB_BOARD_DEV_PWM || dev_type == PB_BOARD_DEV_SPI) {
+ va_start(ap, arg);
+ args[1] = va_arg(ap, int);
+ va_end(ap);
+ }
+
+ for (i = 0; i < board->num_dev; i++) {
+ if (board->dev[i].dev_type != dev_type) continue;
+
+ if (board->dev[i].args[0] == args[0] && board->dev[i].args[1] == args[1])
+ return &board->dev[i];
+ }
+
+ return NULL;
+}
+
+pb_board_s *peripheral_bus_board_init(void)
+{
+ pb_board_s *board;
+
+ board = peripheral_bus_board_get_info();
+
+ return board;
+}
+
+void peripheral_bus_board_deinit(pb_board_s *board)
+{
+ if (board) {
+ if (board->dev)
+ free(board->dev);
+
+ free(board);
+ }
+}
--- /dev/null
+/*
+ * 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 <cynara-creds-gdbus.h>
+#include <cynara-client.h>
+#include <cynara-session.h>
+
+#include "peripheral_privilege.h"
+#include "peripheral_log.h"
+
+#define PERIPHERAL_PRIVILEGE "http://tizen.org/privilege/peripheralio"
+
+#define CACHE_SIZE 100
+
+static cynara *__cynara;
+
+void peripheral_privilege_init(void)
+{
+ int err;
+ cynara_configuration* conf = NULL;
+
+ err = cynara_configuration_create(&conf);
+ RETM_IF(err != CYNARA_API_SUCCESS, "Failed to create cynara configuration");
+
+ err = cynara_configuration_set_cache_size(conf, CACHE_SIZE);
+ if (err != CYNARA_API_SUCCESS) {
+ _E("Failed to set cynara cache size");
+ cynara_configuration_destroy(conf);
+ return;
+ }
+
+ err = cynara_initialize(&__cynara, conf);
+ cynara_configuration_destroy(conf);
+ if (err != CYNARA_API_SUCCESS) {
+ _E("Failed to initialize cynara");
+ __cynara = NULL;
+ return;
+ }
+
+ _D("Cynara initialized");
+}
+
+void peripheral_privilege_deinit(void)
+{
+ if (__cynara)
+ cynara_finish(__cynara);
+
+ _D("Cynara deinitialized");
+}
+
+int peripheral_privilege_check(GDBusMethodInvocation *invocation, GDBusConnection *connection)
+{
+ int ret;
+ int pid;
+ const char *sender;
+ char *session;
+ char *client;
+ char *user;
+
+ sender = g_dbus_method_invocation_get_sender(invocation);
+
+ cynara_creds_gdbus_get_pid(connection, sender, &pid);
+ session = cynara_session_from_pid(pid);
+
+ cynara_creds_gdbus_get_client(connection, sender, CLIENT_METHOD_DEFAULT, &client);
+ cynara_creds_gdbus_get_user(connection, sender, USER_METHOD_DEFAULT, &user);
+
+ if (!session || !client || !user) {
+ _E("Failed to get client info");
+ return -1;
+ }
+
+ ret = cynara_check(__cynara, client, session, user, PERIPHERAL_PRIVILEGE);
+ if (ret != 0) {
+ g_free(session);
+ g_free(client);
+ g_free(user);
+ return -EACCES;
+ }
+
+ g_free(session);
+ g_free(client);
+ g_free(user);
+
+ return 0;
+}