diff options
author | lokilee73 <changjoo.lee@samsung.com> | 2019-01-08 16:30:04 +0900 |
---|---|---|
committer | lokilee73 <changjoo.lee@samsung.com> | 2019-02-18 17:05:11 +0900 |
commit | 0ee8b0a3fd8001d5743816ca4d4d675440303de1 (patch) | |
tree | f32dc18cc7939928284f4877a4aa8dccc35a8310 | |
parent | 1a42cd9a0804c841b7cc15e5846e05b7090c574e (diff) | |
download | device-manager-plugin-odroid-0ee8b0a3fd8001d5743816ca4d4d675440303de1.tar.gz device-manager-plugin-odroid-0ee8b0a3fd8001d5743816ca4d4d675440303de1.tar.bz2 device-manager-plugin-odroid-0ee8b0a3fd8001d5743816ca4d4d675440303de1.zip |
Add thermal module for device_thermal_get_temperaturetizen_6.0.m2_releasetizen_5.5.m2_releasesubmit/tizen_6.0_hotfix/20201103.114801submit/tizen_6.0_hotfix/20201102.192501submit/tizen_6.0/20201029.205101submit/tizen_5.5_wearable_hotfix/20201026.184301submit/tizen_5.5_mobile_hotfix/20201026.185101submit/tizen_5.5/20191031.000002submit/tizen/20190220.073046submit/tizen/20190218.105211accepted/tizen/unified/20190221.165804accepted/tizen/unified/20190219.154142accepted/tizen/6.0/unified/hotfix/20201103.051529accepted/tizen/6.0/unified/20201030.123800accepted/tizen/5.5/unified/wearable/hotfix/20201027.123019accepted/tizen/5.5/unified/mobile/hotfix/20201027.081640accepted/tizen/5.5/unified/20191031.025652tizen_6.0_hotfixtizen_5.5_wearable_hotfixtizen_5.5_tvtizen_5.5_mobile_hotfixaccepted/tizen_6.0_unified_hotfixaccepted/tizen_5.5_unified_wearable_hotfixaccepted/tizen_5.5_unified_mobile_hotfixaccepted/tizen_5.5_unified
Change-Id: I4965e1e6127f1103673f98ada9487864c75392a8
Signed-off-by: lokilee73 <changjoo.lee@samsung.com>
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | hw/thermal/CMakeLists.txt | 19 | ||||
-rw-r--r-- | hw/thermal/thermal.c | 156 | ||||
-rw-r--r-- | hw/udev.c | 299 | ||||
-rw-r--r-- | hw/udev.h | 43 | ||||
-rw-r--r-- | packaging/device-manager-plugin-odroid.spec | 1 |
6 files changed, 519 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index cb4c8be..cbac95c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,3 +8,4 @@ ADD_SUBDIRECTORY(hw/display) ADD_SUBDIRECTORY(hw/usb_gadget) ADD_SUBDIRECTORY(hw/usb_client) ADD_SUBDIRECTORY(hw/usb_cfs_client) +ADD_SUBDIRECTORY(hw/thermal) diff --git a/hw/thermal/CMakeLists.txt b/hw/thermal/CMakeLists.txt new file mode 100644 index 0000000..42bcc20 --- /dev/null +++ b/hw/thermal/CMakeLists.txt @@ -0,0 +1,19 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(thermal C) + +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) + +INCLUDE(FindPkgConfig) +pkg_check_modules(thermal_pkgs REQUIRED hwcommon dlog glib-2.0 libudev) + +FOREACH(flag ${thermal_pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") + +ADD_LIBRARY(${PROJECT_NAME} MODULE thermal.c ../udev.c) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${thermal_pkgs_LDFLAGS}) +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "") +INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries) diff --git a/hw/thermal/thermal.c b/hw/thermal/thermal.c new file mode 100644 index 0000000..10eaea4 --- /dev/null +++ b/hw/thermal/thermal.c @@ -0,0 +1,156 @@ +/* + * device-node + * + * Copyright (c) 2019 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 <glib.h> + +#include <hw/thermal.h> +#include <hw/shared.h> + +#define AP_PATH "/sys/class/thermal/thermal_zone0/temp" + +static struct event_data { + ThermalUpdated updated_cb; + void *data; +} edata = { 0, }; + +static guint timer; + +static int thermal_get_info(device_thermal_e type, struct thermal_info *info) +{ + FILE *fp; + char buf[32]; + size_t len; + + if (!info) + return -EINVAL; + + fp = fopen(AP_PATH, "r"); + if (!fp) { + _E("Failed to open thermal path(%d)", errno); + return -errno; + } + + len = fread(buf, 1, sizeof(buf) - 1, fp); + fclose(fp); + if (len == 0) { + _E("Failed to read thermal value(%d)", errno); + return -errno; + } + buf[len] = '\0'; + info->temp = atoi(buf); + info->temp /= 1000; + info->adc = 0; + + _I("temp(%d) adc(%d)", info->temp, info->adc); + return 0; +} + +static gboolean thermal_timeout(gpointer data) +{ + struct thermal_info info; + int ret; + + ret = thermal_get_info(DEVICE_THERMAL_AP, &info); + if (ret < 0) { + _E("Failed to read thermal info (%d)", ret); + return G_SOURCE_CONTINUE; + } + + if (edata.updated_cb) + edata.updated_cb(&info, edata.data); + + return G_SOURCE_CONTINUE; +} + +static int thermal_register_changed_event(ThermalUpdated updated_cb, void *data) +{ + if (timer) + g_source_remove(timer); + + timer = g_timeout_add(10000, thermal_timeout, NULL); + if (timer == 0) { + _E("Failed to add timer for thermal"); + return -ENOENT; + } + + edata.updated_cb = updated_cb; + edata.data = data; + + return 0; +} + +static int thermal_unregister_changed_event(ThermalUpdated updated_cb) +{ + if (timer) { + g_source_remove(timer); + timer = 0; + } + + edata.updated_cb = NULL; + edata.data = NULL; + + return 0; +} + +static int thermal_open(struct hw_info *info, + const char *id, struct hw_common **common) +{ + struct thermal_device *thermal_dev; + + if (!info || !common) + return -EINVAL; + + thermal_dev = calloc(1, sizeof(struct thermal_device)); + if (!thermal_dev) + return -ENOMEM; + + thermal_dev->common.info = info; + thermal_dev->register_changed_event + = thermal_register_changed_event; + thermal_dev->unregister_changed_event + = thermal_unregister_changed_event; + thermal_dev->get_info + = thermal_get_info; + + *common = (struct hw_common *)thermal_dev; + return 0; +} + +static int thermal_close(struct hw_common *common) +{ + if (!common) + return -EINVAL; + + free(common); + return 0; +} + +HARDWARE_MODULE_STRUCTURE = { + .magic = HARDWARE_INFO_TAG, + .hal_version = HARDWARE_INFO_VERSION, + .device_version = THERMAL_HARDWARE_DEVICE_VERSION, + .id = THERMAL_HARDWARE_DEVICE_ID, + .name = "thermal", + .open = thermal_open, + .close = thermal_close, +}; diff --git a/hw/udev.c b/hw/udev.c new file mode 100644 index 0000000..da43d5f --- /dev/null +++ b/hw/udev.c @@ -0,0 +1,299 @@ +/* + * device-manager + * + * Copyright (c) 2019 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 <stdbool.h> +#include <errno.h> +#include <libudev.h> +#include <glib.h> +#include <string.h> +#include <hw/shared.h> +#include "udev.h" + +#define EVENT_KERNEL "kernel" +#define EVENT_UDEV "udev" + +#define UDEV_MONITOR_SIZE (128*1024) + +struct uevent_info { + struct udev_monitor *mon; + GIOChannel *ch; + guint eventid; + GList *event_list; +}; + + +/* Uevent */ +static struct udev *udev; +static struct uevent_info kevent; /* kernel */ +static struct uevent_info uevent; /* udev */ + +static gboolean uevent_control_cb(GIOChannel *channel, + GIOCondition cond, void *data) +{ + struct uevent_info *info = data; + struct udev_device *dev; + struct uevent_handler *l; + GList *elem; + const char *subsystem; + int len; + + if (!info) { + _E("data is invalid"); + return TRUE; + } + + _I("Set udev monitor buffer size %d", UDEV_MONITOR_SIZE); + dev = udev_monitor_receive_device(info->mon); + if (!dev) + return TRUE; + + subsystem = udev_device_get_subsystem(dev); + if (!subsystem) + goto out; + + len = strlen(subsystem); + + for (elem = info->event_list ; elem ; elem = g_list_next(elem)) { + l = elem->data; + if (!l) + continue; + if (!strncmp(l->subsystem, subsystem, len) && + l->uevent_func) + l->uevent_func(dev); + } + +out: + udev_device_unref(dev); + return TRUE; +} + +static int uevent_control_stop(struct uevent_info *info) +{ + struct udev_device *dev; + + if (!info) + return -EINVAL; + + if (info->eventid) { + g_source_remove(info->eventid); + info->eventid = 0; + } + if (info->ch) { + g_io_channel_unref(info->ch); + info->ch = NULL; + } + if (info->mon) { + dev = udev_monitor_receive_device(info->mon); + if (dev) + udev_device_unref(dev); + udev_monitor_unref(info->mon); + info->mon = NULL; + } + if (udev) + udev = udev_unref(udev); + return 0; +} + +static int uevent_control_start(const char *type, + struct uevent_info *info) +{ + struct uevent_handler *l; + GList *elem; + int fd; + int ret; + + if (!info) + return -EINVAL; + + if (info->mon) { + _E("%s uevent control routine is alreay started", type); + return -EINVAL; + } + + if (!udev) { + udev = udev_new(); + if (!udev) { + _E("error create udev"); + return -EINVAL; + } + } else + udev = udev_ref(udev); + + info->mon = udev_monitor_new_from_netlink(udev, type); + if (info->mon == NULL) { + _E("error udev_monitor create"); + goto stop; + } + + ret = udev_monitor_set_receive_buffer_size(info->mon, + UDEV_MONITOR_SIZE); + if (ret != 0) { + _E("fail to set receive buffer size"); + goto stop; + } + + for (elem = info->event_list ; elem ; elem = g_list_next(elem)) { + l = elem->data; + ret = udev_monitor_filter_add_match_subsystem_devtype( + info->mon, + l->subsystem, NULL); + if (ret < 0) { + _E("error apply subsystem filter"); + goto stop; + } + } + + ret = udev_monitor_filter_update(info->mon); + if (ret < 0) + _E("error udev_monitor_filter_update"); + + fd = udev_monitor_get_fd(info->mon); + if (fd == -1) { + _E("error udev_monitor_get_fd"); + goto stop; + } + + info->ch = g_io_channel_unix_new(fd); + info->eventid = g_io_add_watch(info->ch, + G_IO_IN, uevent_control_cb, info); + if (info->eventid == 0) { + _E("Failed to add channel watch"); + goto stop; + } + + if (udev_monitor_enable_receiving(info->mon) < 0) { + _E("error unable to subscribe to udev events"); + goto stop; + } + + return 0; +stop: + uevent_control_stop(info); + return -EINVAL; +} + +int uevent_control_kernel_start(void) +{ + return uevent_control_start(EVENT_KERNEL, &kevent); +} + +void uevent_control_kernel_stop(void) +{ + uevent_control_stop(&kevent); +} + +int uevent_control_udev_start(void) +{ + return uevent_control_start(EVENT_UDEV, &uevent); +} + +void uevent_control_udev_stop(void) +{ + uevent_control_stop(&uevent); +} + +static int register_uevent_control(struct uevent_info *info, + struct uevent_handler *uh) +{ + struct uevent_handler *l; + GList *elem; + int r; + bool matched = false; + int len; + + if (!info || !uh || !uh->subsystem) + return -EINVAL; + + /* if udev is not initialized, it just will be added list */ + if (!udev || !info->mon) + goto add_list; + + len = strlen(uh->subsystem); + /* check if the same subsystem is already added */ + for (elem = info->event_list; elem ; elem = g_list_next(elem)) { + l = elem->data; + if (!strncmp(l->subsystem, uh->subsystem, len)) { + matched = true; + break; + } + } + + /* the first request to add subsystem */ + if (!matched) { + r = udev_monitor_filter_add_match_subsystem_devtype(info->mon, + uh->subsystem, NULL); + if (r < 0) { + _E("fail to add %s subsystem : %d", uh->subsystem, r); + return -EPERM; + } + } + + r = udev_monitor_filter_update(info->mon); + if (r < 0) + _E("fail to update udev monitor filter : %d", r); + +add_list: + info->event_list = g_list_append(info->event_list, uh); + return 0; +} + +static int unregister_uevent_control(struct uevent_info *info, + const struct uevent_handler *uh) +{ + struct uevent_handler *l; + GList *n, *next; + int len; + + if (!info || !uh || !uh->subsystem) + return -EINVAL; + + len = strlen(uh->subsystem); + for (n = info->event_list, next = g_list_next(n) ; + n ; n = next, next = g_list_next(n)) { + l = n->data; + if (!strncmp(l->subsystem, uh->subsystem, len) && + l->uevent_func == uh->uevent_func) { + info->event_list = g_list_delete_link(info->event_list, n); + return 0; + } + } + + return -ENOENT; +} + +int register_kernel_event_control(struct uevent_handler *uh) +{ + return register_uevent_control(&kevent, uh); +} + +void unregister_kernel_event_control(struct uevent_handler *uh) +{ + unregister_uevent_control(&kevent, uh); +} + +int register_udev_event_control(struct uevent_handler *uh) +{ + return register_uevent_control(&uevent, uh); +} + +void unregister_udev_event_control(struct uevent_handler *uh) +{ + unregister_uevent_control(&uevent, uh); +} diff --git a/hw/udev.h b/hw/udev.h new file mode 100644 index 0000000..69a85a7 --- /dev/null +++ b/hw/udev.h @@ -0,0 +1,43 @@ +/* + * device-manager + * + * Copyright (c) 2019 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 __UDEV_H__ +#define __UDEV_H__ + +#include <libudev.h> + +struct uevent_handler { + const char *subsystem; + void (*uevent_func)(struct udev_device *dev); + void *data; +}; + +int uevent_control_kernel_start(void); +void uevent_control_kernel_stop(void); + +int uevent_control_udev_start(void); +void uevent_control_udev_stop(void); + +int register_kernel_event_control(struct uevent_handler *uh); +void unregister_kernel_event_control(struct uevent_handler *uh); + +int register_udev_event_control(struct uevent_handler *uh); +void unregister_udev_event_control(struct uevent_handler *uh); + +#endif /* __UDEV_H__ */ diff --git a/packaging/device-manager-plugin-odroid.spec b/packaging/device-manager-plugin-odroid.spec index 977274a..2443c3c 100644 --- a/packaging/device-manager-plugin-odroid.spec +++ b/packaging/device-manager-plugin-odroid.spec @@ -13,6 +13,7 @@ BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(hwcommon) BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(libusbgx) +BuildRequires: pkgconfig(libudev) %description Device manager plugin for ODROID development board. |