From 5cefa06679651b8c6985387724ac2350808509a0 Mon Sep 17 00:00:00 2001 From: Yunhee Seo Date: Fri, 5 Jul 2024 13:41:03 +0900 Subject: [PATCH] Use only hal-rootstrap package to build As support hal-abi-versioning, the hal-backend package should be built using only the hal-rootstrap. Other packages not included in the hal-roostrap have been deleted to remove dependencies. util is added to replace functions of dlog and libsyscommon package. Change-Id: I5ba534c5b52a201b0cc4fba999a04b25970636c3 Signed-off-by: Yunhee Seo --- CMakeLists.txt | 4 +- hw/battery/CMakeLists.txt | 6 +- hw/battery/battery.c | 50 +++-- hw/display/CMakeLists.txt | 6 +- hw/display/display.c | 9 +- hw/external_connection/CMakeLists.txt | 6 +- hw/external_connection/external_connection.c | 43 +++- hw/haptic/CMakeLists.txt | 10 +- hw/haptic/emulator.c | 13 +- hw/memory/CMakeLists.txt | 6 +- hw/memory/memory.c | 2 +- include/util.h | 56 +++++ packaging/hal-backend-device-emulator.spec | 8 +- src/util.c | 206 +++++++++++++++++++ 14 files changed, 368 insertions(+), 57 deletions(-) create mode 100644 include/util.h create mode 100644 src/util.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a9f6f6..70275fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ IF(ENABLE_DLOG STREQUAL on) ENDIF() INCLUDE(FindPkgConfig) -pkg_check_modules(pkgs REQUIRED hal-backend-device-common) +pkg_check_modules(pkgs REQUIRED hal-rootstrap) FOREACH(flag ${pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") @@ -24,6 +24,8 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -Werror") SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include) + INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE DESTINATION ${HAL_LICENSE_DIR}/${PROJECT_NAME}) ADD_SUBDIRECTORY(hw/battery) diff --git a/hw/battery/CMakeLists.txt b/hw/battery/CMakeLists.txt index e17fce4..15ca410 100644 --- a/hw/battery/CMakeLists.txt +++ b/hw/battery/CMakeLists.txt @@ -3,8 +3,10 @@ PROJECT(hal-backend-device-battery C) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include) + INCLUDE(FindPkgConfig) -pkg_check_modules(hal-backend-device-battery_pkgs REQUIRED hal-backend-device-common glib-2.0 gio-2.0 libsyscommon) +pkg_check_modules(hal-backend-device-battery_pkgs REQUIRED hal-rootstrap) FOREACH(flag ${hal-backend-device-battery_pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") @@ -13,6 +15,6 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE battery.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE battery.c ${CMAKE_SOURCE_DIR}/src/util.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${hal-backend-device-battery_pkgs_LDFLAGS}) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${HAL_LIB_DIR} COMPONENT RuntimeLibraries) diff --git a/hw/battery/battery.c b/hw/battery/battery.c index 5d21d94..7487629 100644 --- a/hw/battery/battery.c +++ b/hw/battery/battery.c @@ -26,11 +26,8 @@ #include #include -#include -#include - -#include +#include "util.h" #define BATTERY_BUS "org.tizen.system.deviced" #define BATTERY_OBJECT "/Org/Tizen/System/DeviceD/SysNoti" @@ -50,6 +47,7 @@ static struct signal_data { } sdata = { 0, }; static guint signal_id; /* signal handler id */ +static dbus_handle_s *dh; static int get_power_source(int online, hal_device_battery_power_source_type_e *src) { @@ -141,9 +139,22 @@ static int battery_register_changed_event( return -EEXIST; } - signal_id = gdbus_signal_subscribe(NULL, BATTERY_OBJECT, - BATTERY_IFACE, BATTERY_SIGNAL, - signal_delivered, &sdata, NULL); + if (!dh) { + dh = (dbus_handle_s*)dbus_get_default_connection(); + if (!dh) { + _E("failed to get default connection, bustype:%d", (int)dbus_get_default_bus_type()); + return -EPERM; + } + } + + if (!dh->conn) { + _E("connection is null. check bus status"); + return -EPERM; + } + + signal_id = g_dbus_connection_signal_subscribe(dh->conn, NULL, BATTERY_IFACE, BATTERY_SIGNAL, BATTERY_OBJECT, NULL, + G_DBUS_SIGNAL_FLAGS_NONE, signal_delivered, &sdata, NULL); + if (signal_id <= 0) { _E("Failed to register signal"); return -ENOMEM; @@ -159,7 +170,20 @@ static void battery_unregister_changed_event( hal_device_battery_updated_cb updated_cb) { if (signal_id > 0) { - gdbus_signal_unsubscribe(NULL, signal_id); + if (!dh) { + dh = (dbus_handle_s*)dbus_get_default_connection(); + if (!dh) { + _E("failed to get default connection, bustype:%d", (int)dbus_get_default_bus_type()); + return; + } + } + + if (!dh->conn) { + _E("connection is null. check bus status"); + return; + } + + g_dbus_connection_signal_unsubscribe(dh->conn, signal_id); signal_id = 0; } sdata.updated_cb = NULL; @@ -184,7 +208,7 @@ static int battery_get_current_state( info.name = BATTERY_HARDWARE_DEVICE_ID; - ret = sys_get_str(FILE_BATTERY_CAPACITY, capacity_str, sizeof(capacity_str)); + ret = sysfs_read_str(FILE_BATTERY_CAPACITY, capacity_str, sizeof(capacity_str)); if (ret < 0) { _E("Failed to get value of (%s, %d)", FILE_BATTERY_CAPACITY, ret); return ret; @@ -194,24 +218,24 @@ static int battery_get_current_state( it sometimes returns an empty string. */ if (capacity_str[0] == 0) { _E("/sys/class/power_supply/battery/capacity returns null string, retry read it again"); - ret = sys_get_str(FILE_BATTERY_CAPACITY, capacity_str, sizeof(capacity_str)); + ret = sysfs_read_str(FILE_BATTERY_CAPACITY, capacity_str, sizeof(capacity_str)); if (ret < 0) { _E("Failed to get value of (%s, %d)", FILE_BATTERY_CAPACITY, ret); return ret; } } - ret = sys_get_str(FILE_BATTERY_CHARGER_ONLINE, charger_str, sizeof(charger_str)); + ret = sysfs_read_str(FILE_BATTERY_CHARGER_ONLINE, charger_str, sizeof(charger_str)); if (ret < 0) { _E("Failed to get value of (%s, %d)", FILE_BATTERY_CHARGER_ONLINE, ret); return ret; } - ret = sys_get_str(FILE_BATTERY_CHARGE_FULL, charge_full_str, sizeof(charge_full_str)); + ret = sysfs_read_str(FILE_BATTERY_CHARGE_FULL, charge_full_str, sizeof(charge_full_str)); if (ret < 0) { _E("Failed to get value of (%s, %d)", FILE_BATTERY_CHARGE_FULL, ret); return ret; } - ret = sys_get_str(FILE_BATTERY_CHARGE_NOW, charge_now_str, sizeof(charge_now_str)); + ret = sysfs_read_str(FILE_BATTERY_CHARGE_NOW, charge_now_str, sizeof(charge_now_str)); if (ret < 0) { _E("Failed to get value of (%s, %d)", FILE_BATTERY_CHARGE_NOW, ret); return ret; diff --git a/hw/display/CMakeLists.txt b/hw/display/CMakeLists.txt index 1db4a9e..7bf1a4b 100644 --- a/hw/display/CMakeLists.txt +++ b/hw/display/CMakeLists.txt @@ -3,8 +3,10 @@ PROJECT(hal-backend-device-display C) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include) + INCLUDE(FindPkgConfig) -pkg_check_modules(hal-backend-device-display_pkgs REQUIRED hal-backend-device-common libsyscommon) +pkg_check_modules(hal-backend-device-display_pkgs REQUIRED hal-rootstrap) FOREACH(flag ${display_pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") @@ -13,6 +15,6 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE display.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE display.c ${CMAKE_SOURCE_DIR}/src/util.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${hal-backend-device-display_pkgs_LDFLAGS}) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${HAL_LIB_DIR} COMPONENT RuntimeLibraries) diff --git a/hw/display/display.c b/hw/display/display.c index d010f60..63ab311 100644 --- a/hw/display/display.c +++ b/hw/display/display.c @@ -25,9 +25,8 @@ #include #include -#include -#include +#include "util.h" #ifndef BACKLIGHT_PATH #define BACKLIGHT_PATH "/sys/class/backlight/emulator" @@ -42,7 +41,7 @@ static int display_get_max_brightness(int *val) return -EINVAL; if (max < 0) { - r = sys_get_int(BACKLIGHT_PATH"/max_brightness", &max); + r = sysfs_read_int(BACKLIGHT_PATH"/max_brightness", &max); if (r < 0) return r; } @@ -60,7 +59,7 @@ static int display_get_brightness(int *brightness) return -EINVAL; } - r = sys_get_int(BACKLIGHT_PATH"/brightness", &v); + r = sysfs_read_int(BACKLIGHT_PATH"/brightness", &v); if (r < 0) { _E("fail to get brightness (errno:%d)", r); return r; @@ -85,7 +84,7 @@ static int display_set_brightness(int brightness) return -EINVAL; } - r = sys_set_int(BACKLIGHT_PATH"/brightness", brightness); + r = sysfs_write_int(BACKLIGHT_PATH"/brightness", brightness); if (r < 0) { _E("fail to set brightness (errno:%d)", r); return r; diff --git a/hw/external_connection/CMakeLists.txt b/hw/external_connection/CMakeLists.txt index 70b2fed..7b9c6ed 100644 --- a/hw/external_connection/CMakeLists.txt +++ b/hw/external_connection/CMakeLists.txt @@ -3,8 +3,10 @@ PROJECT(hal-backend-device-external-connection C) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include) + INCLUDE(FindPkgConfig) -pkg_check_modules(hal-backend-device-external-connection_pkgs REQUIRED hal-backend-device-common glib-2.0 gio-2.0 libsyscommon) +pkg_check_modules(hal-backend-device-external-connection_pkgs REQUIRED hal-rootstrap) FOREACH(flag ${hal-backend-device-external-connection_pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") @@ -13,6 +15,6 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE external_connection.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE external_connection.c ${CMAKE_SOURCE_DIR}/src/util.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${hal-backend-device-external-connection_pkgs_LDFLAGS}) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${HAL_LIB_DIR} COMPONENT RuntimeLibraries) diff --git a/hw/external_connection/external_connection.c b/hw/external_connection/external_connection.c index 09bfd11..08d6db2 100644 --- a/hw/external_connection/external_connection.c +++ b/hw/external_connection/external_connection.c @@ -27,11 +27,8 @@ #include #include -#include -#include - -#include +#include "util.h" #ifndef ARRAY_SIZE #define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0])) @@ -64,6 +61,8 @@ static struct signal_data { void *data; } sdata = { 0, }; +static dbus_handle_s *dh; + static void signal_delivered(GDBusConnection *conn, const gchar *sender, const gchar *object, @@ -121,9 +120,22 @@ static int external_connection_register_changed_event( return -EEXIST; } - signal_id = gdbus_signal_subscribe(NULL, EXTCON_OBJECT, - EXTCON_IFACE, EXTCON_SIGNAL, - signal_delivered, &sdata, NULL); + if (!dh) { + dh = (dbus_handle_s*)dbus_get_default_connection(); + if (!dh) { + _E("failed to get default connection, bustype:%d", (int)dbus_get_default_bus_type()); + return -EPERM; + } + } + + if (!dh->conn) { + _E("connection is null. check bus status"); + return -EPERM; + } + + signal_id = g_dbus_connection_signal_subscribe(dh->conn, NULL, EXTCON_IFACE, EXTCON_SIGNAL, EXTCON_OBJECT, NULL, + G_DBUS_SIGNAL_FLAGS_NONE, signal_delivered, &sdata, NULL); + if (signal_id <= 0) { _E("Failed to register signal"); return -ENOMEM; @@ -139,7 +151,20 @@ static void external_connection_unregister_changed_event( hal_device_external_connection_updated_cb updated_cb) { if (signal_id > 0) { - gdbus_signal_unsubscribe(NULL, signal_id); + if (!dh) { + dh = (dbus_handle_s*)dbus_get_default_connection(); + if (!dh) { + _E("failed to get default connection, bustype:%d", (int)dbus_get_default_bus_type()); + return; + } + } + + if (!dh->conn) { + _E("connection is null. check bus status"); + return; + } + + g_dbus_connection_signal_unsubscribe(dh->conn, signal_id); signal_id = 0; } sdata.updated_cb = NULL; @@ -157,7 +182,7 @@ static int external_connection_get_current_state( return -EINVAL; for (i = 0 ; i < ARRAY_SIZE(extcon_devices) ; i++) { - ret = sys_get_int(extcon_devices[i].path, &val); + ret = sysfs_read_int(extcon_devices[i].path, &val); if (ret < 0) { _E("Failed to get value of (%s, ret:%d)", extcon_devices[i].path, ret); diff --git a/hw/haptic/CMakeLists.txt b/hw/haptic/CMakeLists.txt index 0c53a66..1386c43 100644 --- a/hw/haptic/CMakeLists.txt +++ b/hw/haptic/CMakeLists.txt @@ -3,12 +3,10 @@ PROJECT(hal-backend-device-haptic C) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include) + INCLUDE(FindPkgConfig) -pkg_check_modules(haptic_pkgs REQUIRED - hal-backend-device-common - glib-2.0 - libsyscommon - capi-system-info) +pkg_check_modules(haptic_pkgs REQUIRED hal-rootstrap) FOREACH(flag ${haptic_pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") @@ -17,7 +15,7 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE emulator.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE emulator.c ${CMAKE_SOURCE_DIR}/src/util.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${haptic_pkgs_LDFLAGS}) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${HAL_LIB_DIR} COMPONENT RuntimeLibraries) diff --git a/hw/haptic/emulator.c b/hw/haptic/emulator.c index 48da525..eb80442 100644 --- a/hw/haptic/emulator.c +++ b/hw/haptic/emulator.c @@ -19,10 +19,9 @@ #include #include #include -#include #include -#include +#include "util.h" static GList *handle_list; static int unique_number = 0; @@ -46,12 +45,12 @@ static int open_device(int *device_handle) while (found != true) { ++unique_number; - elem = SYS_G_LIST_FIND(handle_list, (gpointer)(long)unique_number); + elem = g_list_find(handle_list, (gpointer)(long)unique_number); if (!elem) found = true; } handle = unique_number; - SYS_G_LIST_APPEND(handle_list, (gpointer)(long)handle); + handle_list = g_list_append(handle_list, (gpointer)(long)handle); if (device_handle) *device_handle = handle; @@ -61,7 +60,7 @@ static int open_device(int *device_handle) static int close_device(int device_handle) { - SYS_G_LIST_REMOVE(handle_list, (gpointer)(long)device_handle); + handle_list = g_list_remove(handle_list, (gpointer)(long)device_handle); return 0; } @@ -70,7 +69,7 @@ static int vibrate_monotone(int device_handle, int duration, int frequency, int { GList *elem; - elem = SYS_G_LIST_FIND(handle_list, (gpointer)(long)device_handle); + elem = g_list_find(handle_list, (gpointer)(long)device_handle); if (!elem) return -EINVAL; @@ -81,7 +80,7 @@ static int stop_device(int device_handle) { GList *elem; - elem = SYS_G_LIST_FIND(handle_list, (gpointer)(long)device_handle); + elem = g_list_find(handle_list, (gpointer)(long)device_handle); if (!elem) return -EINVAL; diff --git a/hw/memory/CMakeLists.txt b/hw/memory/CMakeLists.txt index a35248a..77b4f9c 100644 --- a/hw/memory/CMakeLists.txt +++ b/hw/memory/CMakeLists.txt @@ -3,8 +3,10 @@ PROJECT(hal-backend-device-memory C) SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include) + INCLUDE(FindPkgConfig) -pkg_check_modules(hal-backend-device-memory_pkgs REQUIRED dlog) +pkg_check_modules(hal-backend-device-memory_pkgs REQUIRED hal-rootstrap) FOREACH(flag ${hal-backend-device-memory_pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") @@ -13,6 +15,6 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") -ADD_LIBRARY(${PROJECT_NAME} MODULE memory.c) +ADD_LIBRARY(${PROJECT_NAME} MODULE memory.c ${CMAKE_SOURCE_DIR}/src/util.c) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${hal-backend-device-memory_pkgs_LDFLAGS}) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${HAL_LIB_DIR} COMPONENT RuntimeLibraries) diff --git a/hw/memory/memory.c b/hw/memory/memory.c index 66ed8a7..2afd2de 100644 --- a/hw/memory/memory.c +++ b/hw/memory/memory.c @@ -21,7 +21,7 @@ #include #include -#include +#include "util.h" static int memory_get_gpu_info(const int pid, hal_device_memory_gpu_info_s *info) { diff --git a/include/util.h b/include/util.h new file mode 100644 index 0000000..4bd3bcc --- /dev/null +++ b/include/util.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 + +#ifdef FEATURE_DLOG + #define LOG_TAG "HAL_BACKEND_DEVICE_EMULATOR" + #include + #define _D(fmt, args...) dlog_print(DLOG_DEBUG, LOG_TAG, fmt, ##args) + #define _I(fmt, args...) dlog_print(DLOG_INFO, LOG_TAG, fmt, ##args) + #define _W(fmt, args...) dlog_print(DLOG_WARN, LOG_TAG, fmt, ##args) + #define _E(fmt, args...) dlog_print(DLOG_ERROR, LOG_TAG, fmt, ##args) +#else + #define _D(x, ...) + #define _I(x, ...) + #define _W(x, ...) + #define _E(x, ...) +#endif + +#define EXPORT __attribute__ ((visibility("default"))) + +#define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0])) +#define MAX_BUF_SIZE 255 + +typedef struct { + GDBusConnection *conn; + GBusType bus_type; + gboolean priv; + GList *list_names; /* dbus_name */ + GList *list_object; /* dbus_object_handle_s */ + pthread_mutex_t mutex; +} dbus_handle_s; + +int sysfs_read_int(char *path, int *val); +int sysfs_read_str(char *path, char *str, int len); +int sysfs_write_int(char *path, int val); +int sysfs_write_str(char *path, char *str); +dbus_handle_s * dbus_get_default_connection(void); +GBusType dbus_get_default_bus_type(void); diff --git a/packaging/hal-backend-device-emulator.spec b/packaging/hal-backend-device-emulator.spec index 1587989..4cdd139 100644 --- a/packaging/hal-backend-device-emulator.spec +++ b/packaging/hal-backend-device-emulator.spec @@ -10,13 +10,7 @@ ExclusiveArch: %{ix86} x86_64 aarch64 %{arm} Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig BuildRequires: cmake -BuildRequires: pkgconfig(glib-2.0) -BuildRequires: pkgconfig(gio-2.0) -BuildRequires: pkgconfig(hal-api-common) -BuildRequires: pkgconfig(hal-api-device) -BuildRequires: pkgconfig(libsyscommon) -BuildRequires: pkgconfig(capi-system-info) -BuildRequires: pkgconfig(hal-backend-device-common) +BuildRequires: pkgconfig(hal-rootstrap) %description Device HAL backend drivers for emulator targets diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..4fb5d1e --- /dev/null +++ b/src/util.c @@ -0,0 +1,206 @@ +/* + * Copyright (c) 2024 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 "util.h" + +static GBusType g_default_bus_type = G_BUS_TYPE_SYSTEM; +pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; +/* global shared bus : system, session */ +static dbus_handle_s g_dh[2]; + +#define dbus_lock(handle) do {\ + assert(handle);\ + pthread_mutex_lock(&((handle)->mutex));\ +} while (0); + +#define dbus_unlock(handle) do {\ + assert(handle);\ + pthread_mutex_unlock(&(handle)->mutex);\ +} while (0); + +static int sysfs_read_buf(char *path, char *buf, int len) +{ + int r, fd; + + if ((!path) || (!buf) || (len < 0)) + return -EINVAL; + + fd = open(path, O_RDONLY); + if (fd == -1) + return -ENOENT; + + r = read(fd, buf, len); + close(fd); + + if ((r < 0) || (r > len)) + return -EIO; + + /* Replace '\n' with space (ascii code is 32) */ + buf[strcspn(buf, "\n")] = (char)32; + buf[r] = '\0'; + + return 0; +} + +static int sysfs_write_buf(char *path, char *buf) +{ + int w, fd; + + if ((!path) || (!buf)) + return -EINVAL; + + fd = open(path, O_WRONLY); + if (fd == -1) + return -ENOENT; + + w = write(fd, buf, strlen(buf)); + close(fd); + + if (w < 0) + return -EIO; + + return 0; +} + +int sysfs_read_int(char *path, int *val) +{ + char buf[MAX_BUF_SIZE + 1]; + int r; + + if ((!path) || (!val)) + return -EINVAL; + + r = sysfs_read_buf(path, buf, MAX_BUF_SIZE); + if (r < 0) + return r; + + *val = atoi(buf); + return 0; +} + +int sysfs_read_str(char *path, char *str, int len) +{ + int r; + + if ((!path) || (!str) || (len <= 0)) + return -EINVAL; + + r = sysfs_read_buf(path, str, len); + if (r < 0) + return r; + + return 0; +} + +int sysfs_write_int(char *path, int val) +{ + char buf[MAX_BUF_SIZE + 1]; + int w; + + if (!path) + return -EINVAL; + + snprintf(buf, MAX_BUF_SIZE, "%d", val); + w = sysfs_write_buf(path, buf); + if (w < 0) + return w; + + return 0; +} + +int sysfs_write_str(char *path, char *str) +{ + int w; + + if ((!path) || (!str)) + return -EINVAL; + + w = sysfs_write_buf(path, str); + if (w < 0) + return w; + + return 0; +} + +static GDBusConnection * _get_bus(GBusType bus_type) +{ + GDBusConnection *conn = NULL; + GError *err = NULL; + + if (bus_type != G_BUS_TYPE_SYSTEM && bus_type != G_BUS_TYPE_SESSION) { + _E("Wrong bus_type %d", bus_type); + return NULL; + } + + conn = g_bus_get_sync(bus_type, NULL, &err); + if (!conn || err) { + _E("failed to get bus:type:%d, %s\n", bus_type, err->message); + g_error_free(err); + return NULL; + } + + return conn; +} + +/* ref cout is 1 */ +static dbus_handle_s *_dbus_get_connection(GBusType bus_type) +{ + int ibus = bus_type - 1; + dbus_handle_s *dh = NULL; + + if (bus_type != G_BUS_TYPE_SYSTEM && bus_type != G_BUS_TYPE_SESSION) { + _E("Unknown bus type %d", bus_type); + return NULL; + } + dh = &g_dh[ibus]; + + dbus_lock(dh); + + if (!dh->conn) { + dh->conn = _get_bus(bus_type); + if (!dh->conn) { + dbus_unlock(dh); + return NULL; + } + dh->priv = FALSE; + dh->bus_type = bus_type; + } + + dbus_unlock(dh); + + return dh; +} + +GBusType dbus_get_default_bus_type(void) +{ + GBusType type; + + pthread_mutex_lock(&g_mutex); + type = g_default_bus_type; + pthread_mutex_unlock(&g_mutex); + + return type; +} + +dbus_handle_s* dbus_get_default_connection(void) +{ + return _dbus_get_connection(dbus_get_default_bus_type()); +} \ No newline at end of file -- 2.34.1