battery: add battery HAL 91/103791/2 accepted/tizen_3.0.m2_mobile accepted/tizen_3.0.m2_tv accepted/tizen_3.0.m2_wearable accepted/tizen_3.0_ivi tizen_3.0.m2 tizen_3.0_tv accepted/tizen/3.0.m2/mobile/20170104.100211 accepted/tizen/3.0.m2/tv/20170104.100404 accepted/tizen/3.0.m2/wearable/20170104.100942 accepted/tizen/3.0/common/20161212.060613 accepted/tizen/3.0/ivi/20161212.023904 accepted/tizen/3.0/mobile/20161212.023814 accepted/tizen/3.0/tv/20161212.023832 accepted/tizen/3.0/wearable/20161212.023855 submit/tizen_3.0.m2/20170104.093748 submit/tizen_3.0/20161209.130513
authortaeyoung <ty317.kim@samsung.com>
Fri, 9 Dec 2016 11:47:21 +0000 (20:47 +0900)
committertaeyoung <ty317.kim@samsung.com>
Fri, 9 Dec 2016 12:08:21 +0000 (21:08 +0900)
Change-Id: Ib16aa5f95b05787a36251724e7d995522fec0977
Signed-off-by: taeyoung <ty317.kim@samsung.com>
CMakeLists.txt
hw/battery/CMakeLists.txt [new file with mode: 0644]
hw/battery/battery.c [new file with mode: 0644]
hw/udev.c [new file with mode: 0644]
hw/udev.h [new file with mode: 0644]
packaging/device-manager-plugin-exynos5433.spec

index 19b1cfd..702dd44 100644 (file)
@@ -3,6 +3,7 @@ PROJECT(device-manager-exynos5433 C)
 
 SET(PREFIX ${CMAKE_INSTALL_PREFIX})
 
+ADD_SUBDIRECTORY(hw/battery)
 ADD_SUBDIRECTORY(hw/display)
 ADD_SUBDIRECTORY(hw/led)
 ADD_SUBDIRECTORY(hw/touchscreen)
diff --git a/hw/battery/CMakeLists.txt b/hw/battery/CMakeLists.txt
new file mode 100644 (file)
index 0000000..48ef1aa
--- /dev/null
@@ -0,0 +1,19 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(battery C)
+
+SET(PREFIX ${CMAKE_INSTALL_PREFIX})
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(battery_pkgs REQUIRED hwcommon dlog glib-2.0 libudev)
+
+FOREACH(flag ${battery_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 battery.c ../shared.c ../udev.c)
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${battery_pkgs_LDFLAGS})
+SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "")
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}/hw COMPONENT RuntimeLibraries)
diff --git a/hw/battery/battery.c b/hw/battery/battery.c
new file mode 100644 (file)
index 0000000..f0404bc
--- /dev/null
@@ -0,0 +1,329 @@
+/*
+ * device-node
+ *
+ * Copyright (c) 2016 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 <linux/limits.h>
+#include <dirent.h>
+
+#include <hw/battery.h>
+#include "../shared.h"
+#include "../udev.h"
+
+#define BATTERY_ROOT_PATH "/sys/class/power_supply"
+#define CHARGER_PATH      "/sys/class/extcon/max77843-muic/state"
+
+static struct uevent_data {
+       BatteryUpdated updated_cb;
+       void *data;
+} udata = { 0, };
+
+static int get_power_source(char **src)
+{
+       int val;
+       FILE *fp;
+       char buf[256];
+
+       if (!src)
+               return -EINVAL;
+
+       fp = fopen(CHARGER_PATH, "r");
+       if (!fp) {
+               _E("Failed to open power source path(%d)", errno);
+               return -errno;
+       }
+
+       *src = POWER_SOURCE_NONE;
+
+       while(fgets(buf, sizeof(buf), fp)) {
+               if (strstr(buf, "USB=")) {
+                       val = atoi(buf + 4); /* 4 == "USB=" */
+                       if (val == 0)
+                               continue;
+                       *src = POWER_SOURCE_USB;
+                       break;
+               }
+
+               if (strstr(buf, "TA=")) {
+                       val = atoi(buf + 3); /* 3 == "TA=" */
+                       if (val == 0)
+                               continue;
+                       *src = POWER_SOURCE_AC;
+                       break;
+               }
+       }
+
+       fclose(fp);
+
+       return 0;
+}
+
+static void adjust_current(struct battery_info *info)
+{
+       if (!info)
+               return;
+       if (info->current_now == 0 && info->current_average == 0) {
+               if (strncmp(info->status, "Charging", 9)) {
+                       info->current_now = -1;
+                       info->current_average = -1;
+               } else {
+                       info->current_now = 1;
+                       info->current_average = 1;
+               }
+       }
+}
+
+static void remove_not_string(char *str)
+{
+       char *t = str;
+
+       while (*t != '\0') {
+               if (*t == '\r' ||
+                       *t == '\n' ||
+                       *t == '\x0a')
+                       *t = '\0';
+               else
+                       t++;
+       }
+}
+
+static void uevent_delivered(struct udev_device *dev)
+{
+       struct battery_info info;
+       char *val;
+       int ret;
+
+       _I("POWER_SUPPLY uevent is delivered");
+
+       if (!udata.updated_cb) {
+               _E("POWER_SUPPLY callback is NULL");
+               return;
+       }
+
+       val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_NAME");
+       if (!val)
+               return;
+       info.name = val;
+
+       val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_STATUS");
+       if (!val)
+               return;
+       info.status = val;
+
+       val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_HEALTH");
+       if (!val)
+               return;
+       info.health = val;
+
+       val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_ONLINE");
+       if (!val)
+               return;
+       info.online = atoi(val);
+
+       val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_PRESENT");
+       if (!val)
+               return;
+       info.present = atoi(val);
+
+       val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_CAPACITY");
+       if (!val)
+               return;
+       info.capacity = atoi(val);
+
+       val = (char *)udev_device_get_property_value(dev, "POWER_SUPPLY_CURRENT_NOW");
+       if (!val)
+               return;
+       info.current_now = atoi(val); /* uA */
+
+       info.current_average = info.current_now;
+
+       adjust_current(&info);
+
+       ret = get_power_source(&val);
+       if (ret < 0)
+               return;
+       info.power_source = val;
+
+       udata.updated_cb(&info, udata.data);
+}
+
+static struct uevent_handler uh = {
+       .subsystem = "power_supply",
+       .uevent_func = uevent_delivered,
+};
+
+static int battery_register_changed_event(
+               BatteryUpdated updated_cb, void *data)
+{
+       int ret;
+
+       ret = uevent_control_kernel_start();
+       if (ret < 0) {
+               _E("Failed to register uevent handler (%d)", ret);
+               return ret;
+       }
+
+       ret = register_kernel_event_control(&uh);
+       if (ret < 0)
+               _E("Failed to register kernel event control (%d)", ret);
+
+       if (udata.updated_cb == NULL) {
+               udata.updated_cb = updated_cb;
+               udata.data = data;
+       } else
+               _E("update callback is already registered");
+
+       return ret;
+}
+
+static void battery_unregister_changed_event(
+               BatteryUpdated updated_cb)
+{
+       unregister_kernel_event_control(&uh);
+       uevent_control_kernel_stop();
+       udata.updated_cb = NULL;
+       udata.data = NULL;
+}
+
+static int battery_get_current_state(
+               BatteryUpdated updated_cb, void *data)
+{
+       int ret, val;
+       struct battery_info info;
+       char *path;
+       char status[32];
+       char health[32];
+       char *power_source;
+
+       if (!updated_cb)
+               return -EINVAL;
+
+       info.name = BATTERY_HARDWARE_DEVICE_ID;
+
+       path = BATTERY_ROOT_PATH"/battery/status";
+       ret = sys_get_str(path, status, sizeof(status));
+       if (ret < 0) {
+               _E("Failed to get value of (%s, %d)", path, ret);
+               return ret;
+       }
+       remove_not_string(status);
+       info.status = status;
+
+       path = BATTERY_ROOT_PATH"/battery/health";
+       ret = sys_get_str(path, health, sizeof(health));
+       if (ret < 0) {
+               _E("Failed to get value of (%s, %d)", path, ret);
+               return ret;
+       }
+       remove_not_string(health);
+       info.health = health;
+
+       ret = get_power_source(&power_source);
+       if (ret < 0) {
+               _E("Failed to get power source (%d)", ret);
+               return ret;
+       }
+       remove_not_string(power_source);
+       info.power_source = power_source;
+
+       path = BATTERY_ROOT_PATH"/battery/online";
+       ret = sys_get_int(path, &val);
+       if (ret < 0) {
+               _E("Failed to get value of (%s, %d)", path, ret);
+               return ret;
+       }
+       info.online = val;
+
+       path = BATTERY_ROOT_PATH"/battery/present";
+       ret = sys_get_int(path, &val);
+       if (ret < 0) {
+               _E("Failed to get value of (%s, %d)", path, ret);
+               return ret;
+       }
+       info.present = val;
+
+       path = BATTERY_ROOT_PATH"/battery/capacity";
+       ret = sys_get_int(path, &val);
+       if (ret < 0) {
+               _E("Failed to get value of (%s, %d)", path, ret);
+               return ret;
+       }
+       info.capacity = val;
+
+       path = BATTERY_ROOT_PATH"/battery/current_now";
+       ret = sys_get_int(path, &val);
+       if (ret < 0) {
+               _E("Failed to get value of (%s, %d)", path, ret);
+               return ret;
+       }
+       info.current_now = val;
+
+       info.current_average = info.current_now;
+
+       adjust_current(&info);
+
+       updated_cb(&info, data);
+
+       return 0;
+}
+
+static int battery_open(struct hw_info *info,
+               const char *id, struct hw_common **common)
+{
+       struct battery_device *battery_dev;
+
+       if (!info || !common)
+               return -EINVAL;
+
+       battery_dev = calloc(1, sizeof(struct battery_device));
+       if (!battery_dev)
+               return -ENOMEM;
+
+       battery_dev->common.info = info;
+       battery_dev->register_changed_event
+               = battery_register_changed_event;
+       battery_dev->unregister_changed_event
+               = battery_unregister_changed_event;
+       battery_dev->get_current_state
+               = battery_get_current_state;
+
+       *common = (struct hw_common *)battery_dev;
+       return 0;
+}
+
+static int battery_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 = BATTERY_HARDWARE_DEVICE_VERSION,
+       .id = BATTERY_HARDWARE_DEVICE_ID,
+       .name = "battery",
+       .open = battery_open,
+       .close = battery_close,
+};
diff --git a/hw/udev.c b/hw/udev.c
new file mode 100644 (file)
index 0000000..562725b
--- /dev/null
+++ b/hw/udev.c
@@ -0,0 +1,298 @@
+/*
+ * device-manager
+ *
+ * Copyright (c) 2015 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 "shared.h"
+#include "udev.h"
+
+#define EVENT_KERNEL       "kernel"
+#define EVENT_UDEV         "udev"
+
+#define UDEV_MONITOR_SIZE   (10*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;
+       }
+
+       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 (file)
index 0000000..d2aeff1
--- /dev/null
+++ b/hw/udev.h
@@ -0,0 +1,43 @@
+/*
+ * device-manager
+ *
+ * Copyright (c) 2015 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__ */
index 48ef1bd..a1592fe 100644 (file)
@@ -12,6 +12,7 @@ BuildRequires:  cmake
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(hwcommon)
 BuildRequires:  pkgconfig(glib-2.0)
+BuildRequires:  pkgconfig(libudev)
 
 %description
 Device manager plugin exynos 5433