battery: Move plugin iot-headless battery to core 46/291546/29
authorTaeminYeom <taemin.yeom@samsung.com>
Tue, 18 Apr 2023 05:26:33 +0000 (14:26 +0900)
committerTaeminYeom <taemin.yeom@samsung.com>
Tue, 16 May 2023 09:05:45 +0000 (18:05 +0900)
In plugin iot-headless battery, there are battery charger event handler codes.
But all of them are functions, not policy depending some profiles.
So, they are can be used in common devices.

And the element "action" and "user_data" are removed because they were
planned to be used in power transition, but now they are not used.

Change-Id: I7a7a9281e7d2da5f6838ed302d2a7a330f57ebf3
Signed-off-by: TaeminYeom <taemin.yeom@samsung.com>
CMakeLists.txt
packaging/deviced.spec
plugins/iot-headless/battery/CMakeLists.txt [deleted file]
plugins/iot-headless/battery/battery-plugin.c [deleted file]
src/battery/battery-parser.c
src/battery/battery-parser.h
src/battery/battery.h
src/battery/lowbat-handler.c
src/battery/power-supply.c

index 4c5e56c..1876051 100644 (file)
@@ -345,7 +345,6 @@ ADD_SUBDIRECTORY(plugins/wearable/display)
 ADD_SUBDIRECTORY(plugins/tv/display)
 ADD_SUBDIRECTORY(plugins/iot-headed/display)
 ADD_SUBDIRECTORY(plugins/iot-headless/input)
-ADD_SUBDIRECTORY(plugins/iot-headless/battery)
 ADD_SUBDIRECTORY(plugins/iot-headless/display)
 IF(BATTERY_MODULE STREQUAL on)
        ADD_SUBDIRECTORY(plugins/mobile/battery)
index a963293..2fbff7f 100644 (file)
@@ -257,7 +257,6 @@ mv %{_sysconfdir}/deviced/input-profile-iot-headless.conf %{_sysconfdir}/deviced
 mv %{_sysconfdir}/deviced/power-profile-iot-headless.conf %{_sysconfdir}/deviced/power.conf
 mkdir -p %{_libdir}/deviced
 mv %{_libdir}/iot-headless-input-handler.so %{_libdir}/deviced/input-handler.so
-mv %{_libdir}/iot-headless-battery.so %{_libdir}/deviced/battery.so
 mv %{_libdir}/iot-headless-display.so %{_libdir}/deviced/display.so
 
 %files
@@ -393,7 +392,6 @@ mv %{_libdir}/iot-headless-display.so %{_libdir}/deviced/display.so
 %config %{_sysconfdir}/deviced/input-profile-iot-headless.conf
 %config %{_sysconfdir}/deviced/power-profile-iot-headless.conf
 %{_libdir}/iot-headless-input-handler.so
-%{_libdir}/iot-headless-battery.so
 %{_libdir}/iot-headless-display.so
 %{_unitdir}/rndis.service
 %{_bindir}/rndis.sh
diff --git a/plugins/iot-headless/battery/CMakeLists.txt b/plugins/iot-headless/battery/CMakeLists.txt
deleted file mode 100644 (file)
index a9ab36c..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
-
-PROJECT(iot-headless-battery C)
-
-INCLUDE(FindPkgConfig)
-PKG_CHECK_MODULES(REQUIRED_PKGS REQUIRED
-       glib-2.0
-       dlog
-       libsyscommon)
-
-INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
-INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../power)
-
-FILE(GLOB SRCS "*.c")
-ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS})
-SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "")
-SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES OUTPUT_NAME iot-headless-battery)
-INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries)
diff --git a/plugins/iot-headless/battery/battery-plugin.c b/plugins/iot-headless/battery/battery-plugin.c
deleted file mode 100644 (file)
index 1c1d2d2..0000000
+++ /dev/null
@@ -1,184 +0,0 @@
-/*
- * deviced
- *
- * Copyright (c) 2022 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 <glib.h>
-#include <libsyscommon/ini-parser.h>
-#include <libsyscommon/list.h>
-
-#include "shared/device-notifier.h"
-#include "shared/log.h"
-#include "shared/event.h"
-#include "battery/battery-ops.h"
-
-#include "power/power.h"
-
-#define BATTERY_CONF_PATH    "/etc/deviced/battery.conf"
-
-struct battery_event_handler {
-       char *name;
-       int id;
-
-       /* broadcast upon occuring this event */
-       int broadcast;
-
-       /* which action to do on receiving an event */
-       enum device_notifier_type action;
-       void *user_data;
-
-       /* hold wakelock for a duration on detecting the event */
-       int wakelock_duration;
-};
-
-static struct battery_event_handler *handler_connected;
-static struct battery_event_handler *handler_disconnected;
-
-static void parse_device_notifier(struct battery_event_handler *handler, const char *action)
-{
-       if (MATCH(action, "DEVICE_NOTIFIER_BATTERY_CHARGER_CONNECTED")) {
-               handler_connected = handler;
-       } else if (MATCH(action, "DEVICE_NOTIFIER_BATTERY_CHARGER_DISCONNECTED")) {
-               handler_disconnected = handler;
-       }
-}
-
-static void add_action_transition_info(struct battery_event_handler *handler, char *curr, char *next)
-{
-       struct trans_info *ti = NULL;
-       GList **action_list = (GList **) &(handler->user_data);
-
-       ti = calloc(1, sizeof(struct trans_info));
-       if (!ti)
-               return;
-
-       /* In configuration file, Enum= must be followed by Action=.
-        * Otherwise, handler->id won't be defined at this point.*/
-       ti->reason = handler->id;
-
-       ti->curr = convert_action_string_to_power_state(curr);
-       ti->next = convert_action_string_to_power_state(next);
-
-       SYS_G_LIST_APPEND(*action_list, ti);
-}
-
-static void parse_action(struct battery_event_handler *handler, const char *action)
-{
-       char curr[16] = { 0, };
-       char next[16] = { 0, };
-
-       if (sscanf(action, "%15[^,],%15s", curr, next) == 2) {
-               handler->action = DEVICE_NOTIFIER_REQUEST_TRANSITION_STATE;
-               /* append transition info to handler->user_data */
-               add_action_transition_info(handler, curr, next);
-       } else {
-               _E("Invalid action=%s", action);
-       }
-}
-
-static void parse_event_action_property(gpointer data, gpointer user_data)
-{
-       struct section_property *prop = (struct section_property *) data;
-       struct battery_event_handler *handler = (struct battery_event_handler *) user_data;
-
-       if (!prop)
-               return;
-
-       _D("Key=%s, Value=%s", prop->key, prop->value);
-
-       if (MATCH(prop->key, "Name")) {
-               handler->name = strndup(prop->value, 32);
-       } else if (MATCH(prop->key, "Enum")) {
-               sscanf(prop->value, "%d", &handler->id);
-       } else if (MATCH(prop->key, "DeviceNotifier")) {
-               parse_device_notifier(handler, prop->value);
-       } else if (MATCH(prop->key, "ActionChangeState")) {
-               parse_action(handler, prop->value);
-       } else if (MATCH(prop->key, "ActionBroadcast")) {
-               handler->broadcast = MATCH(prop->value, "yes");
-       } else if (MATCH(prop->key, "WakeLockDurationSec")) {
-               sscanf(prop->value, "%d", &handler->wakelock_duration);
-       }
-}
-
-static int parse_event_action(const struct parse_result *result, void *data)
-{
-       struct battery_event_handler *handler = NULL;
-
-       if (!result || !result->props)
-               return 0;
-
-       if (MATCH(result->section, "EventAction")) {
-               handler = calloc(1, sizeof(struct battery_event_handler));
-               if (!handler)
-                       return 0;
-
-               g_list_foreach(result->props, parse_event_action_property, handler);
-       }
-
-       return 0;
-}
-
-static int charger_connected_callback(void *data)
-{
-       _D("event=%s(%d), action=%d", handler_connected->name, handler_connected->id, handler_connected->action);
-
-       if (handler_connected->wakelock_duration)
-               event_acquire_wakelock(handler_connected->id, handler_connected->wakelock_duration);
-
-       if (handler_connected->broadcast)
-               event_broadcast_id(handler_connected->id);
-
-       if (handler_connected->action)
-               device_notify(handler_connected->action, handler_connected->user_data);
-
-       return 0;
-}
-
-static int charger_disconnected_callback(void *data)
-{
-       _D("event=%s(%d), action=%d", handler_disconnected->name, handler_disconnected->id, handler_disconnected->action);
-
-       if (handler_disconnected->wakelock_duration)
-               event_acquire_wakelock(handler_disconnected->id, handler_disconnected->wakelock_duration);
-
-       if (handler_disconnected->broadcast)
-               event_broadcast_id(handler_disconnected->id);
-
-       if (handler_disconnected->action)
-               device_notify(handler_disconnected->action, handler_disconnected->user_data);
-
-       return 0;
-}
-
-static void battery_plugin_init(void *data)
-{
-       libsys_config_parse_by_section(BATTERY_CONF_PATH, parse_event_action, NULL);
-
-       if (handler_connected)
-               register_notifier(DEVICE_NOTIFIER_BATTERY_CHARGER_CONNECTED, charger_connected_callback);
-
-       if (handler_disconnected)
-               register_notifier(DEVICE_NOTIFIER_BATTERY_CHARGER_DISCONNECTED, charger_disconnected_callback);
-}
-
-static const struct battery_ops battery_plugin_ops = {
-       .name     = "battery-plugin",
-       .init     = battery_plugin_init,
-};
-
-BATTERY_OPS_REGISTER(&battery_plugin_ops)
index 99cdac3..52932d5 100644 (file)
 
 #define BAT_CONF_FILE  "/etc/deviced/battery.conf"
 
+static void parse_device_notifier(struct battery_config_charger_event_handler *handler,
+                               const char *notifier)
+{
+       if (MATCH(notifier, "DEVICE_NOTIFIER_BATTERY_CHARGER_CONNECTED"))
+               handler->notifier = DEVICE_NOTIFIER_BATTERY_CHARGER_CONNECTED;
+       else if (MATCH(notifier, "DEVICE_NOTIFIER_BATTERY_CHARGER_DISCONNECTED"))
+               handler->notifier = DEVICE_NOTIFIER_BATTERY_CHARGER_DISCONNECTED;
+}
+
+static void parse_event_action_property(gpointer data, gpointer user_data)
+{
+       struct section_property *prop = (struct section_property *) data;
+       struct battery_config_charger_event_handler *handler =
+               (struct battery_config_charger_event_handler *) user_data;
+
+       if (!prop || !handler)
+               return;
+
+       _D("Key=%s, Value=%s", prop->key, prop->value);
+
+       if (MATCH(prop->key, "Name")) {
+               handler->name = strndup(prop->value, 32);
+       } else if (MATCH(prop->key, "Enum")) {
+               sscanf(prop->value, "%d", &handler->id);
+       } else if (MATCH(prop->key, "DeviceNotifier")) {
+               parse_device_notifier(handler, prop->value);
+       } else if (MATCH(prop->key, "ActionBroadcast")) {
+               handler->broadcast = MATCH(prop->value, "yes");
+       } else if (MATCH(prop->key, "WakeLockDurationSec")) {
+               sscanf(prop->value, "%d", &handler->wakelock_duration);
+       }
+}
+
+static int parse_event_action(const struct parse_result *result, void *data)
+{
+       struct battery_config_info *info = data;
+       struct battery_config_charger_event_handler *handler;
+
+       if (!result || !result->props || !info)
+               return 0;
+
+       if (!MATCH(result->section, "EventAction"))
+               return 0;
+
+       handler = calloc(1, sizeof(struct battery_config_charger_event_handler));
+       if (!handler) {
+               _E("Failed to allocate memory");
+               return 0;
+       }
+
+       g_list_foreach(result->props, parse_event_action_property, handler);
+
+       if (handler->notifier == 0) {
+               _E("Invalid device notifier");
+
+               free(handler->name);
+               free(handler);
+
+               return 0;
+       }
+
+       info->event_handlers = realloc(info->event_handlers,
+               sizeof(struct battery_config_charger_event_handler*) * (info->event_handler_number + 1));
+       info->event_handlers[info->event_handler_number++] = handler;
+
+       return 0;
+}
+
 static int load_config(struct parse_result *result, void *user_data)
 {
        struct battery_config_info *info = user_data;
@@ -62,7 +130,7 @@ static int load_config(struct parse_result *result, void *user_data)
        return 0;
 }
 
-void battery_parser_load_config(struct battery_config_info *info)
+static void load_battery_level(struct battery_config_info *info)
 {
        int ret;
 
@@ -78,3 +146,44 @@ void battery_parser_load_config(struct battery_config_info *info)
                info->poweroff = -3;
        }
 }
+
+static void load_battery_charger_event(struct battery_config_info *info)
+{
+       if (!info)
+               return;
+
+       info->event_handlers = NULL;
+       info->event_handler_number = 0;
+       libsys_config_parse_by_section(BAT_CONF_FILE, parse_event_action, info);
+}
+
+void battery_parser_load_config(struct battery_config_info *info)
+{
+       if (!info) {
+               _E("Invalid parameter");
+               return;
+       }
+
+       load_battery_level(info);
+       load_battery_charger_event(info);
+}
+
+void battery_parser_unload_config(struct battery_config_info *info)
+{
+       if (!info) {
+               _E("Invalid parameter");
+               return;
+       }
+
+       if (info->event_handler_number <= 0 || !info->event_handlers)
+               return;
+
+       for (int i = 0; i < info->event_handler_number; ++i) {
+               free(info->event_handlers[i]->name);
+               free(info->event_handlers[i]);
+       }
+
+       free(info->event_handlers);
+       info->event_handlers = NULL;
+       info->event_handler_number = 0;
+}
index 820718e..9744302 100644 (file)
@@ -26,5 +26,8 @@
 #define BATTERY_CRITICAL 5
 #define BATTERY_POWEROFF 1
 
+#include "battery.h"
+
 void battery_parser_load_config(struct battery_config_info *info);
+void battery_parser_unload_config(struct battery_config_info *info);
 #endif /* __BATTERY_PARSER_H__ */
index 0aa7b8d..2a6caa1 100644 (file)
@@ -20,6 +20,8 @@
 #ifndef __BATTERY_H__
 #define __BATTERY_H__
 
+#include "shared/device-notifier.h"
+
 #define BATTERY_LEVEL_CHECK_FULL       95
 #define BATTERY_LEVEL_CHECK_HIGH       15
 #define BATTERY_LEVEL_CHECK_LOW                5
@@ -36,11 +38,29 @@ enum battery_option_type {
 };
 
 #define METHOD_NAME_MAX                        32
+
+struct battery_config_charger_event_handler {
+       char *name;
+       int id;
+       enum device_notifier_type notifier;
+
+       /* broadcast upon occuring this event */
+       int broadcast;
+
+       /* hold wakelock for a duration on detecting the event */
+       int wakelock_duration;
+};
+
 struct battery_config_info {
+       /* battery level config */
        int normal;
        int warning;
        int critical;
        int poweroff;
+
+       /* battery charger event config */
+       struct battery_config_charger_event_handler **event_handlers;
+       int event_handler_number;
 };
 
 int battery_charge_err_low_act(void *data);
index 8ff3c68..3d294ab 100644 (file)
@@ -700,6 +700,8 @@ static void lowbat_exit(void *data)
                g_source_remove(low_batt_sig_timer);
                low_batt_sig_timer = 0;
        }
+
+       battery_parser_unload_config(&battery_info);
 }
 
 static gboolean low_battery_charging_status(void *data)
index fb65178..1f63c8a 100644 (file)
@@ -35,6 +35,7 @@
 #include "shared/eventsystem.h"
 #include "shared/plugin.h"
 #include "shared/apps.h"
+#include "shared/event.h"
 #include "core.h"
 #include "display-ops.h"
 #include "power-supply.h"
@@ -42,6 +43,7 @@
 #include "battery-ops.h"
 #include "extcon/extcon.h"
 #include "lowbat-handler.h"
+#include "battery-parser.h"
 
 #ifndef VCONFKEY_SETAPPL_BLOCKMODE_WEARABLE_BOOL
 #define VCONFKEY_SETAPPL_BLOCKMODE_WEARABLE_BOOL "db/setting/blockmode_wearable"
@@ -96,6 +98,8 @@ static device_notifier_state_e old_state = -1;
 
 static enum state_t g_display_state = S_NORMAL;
 
+static struct battery_config_info g_battery_info;
+
 bool battery_initialized;
 
 bool battery_do_not_disturb(void);
@@ -634,6 +638,36 @@ static void check_misc_status(const char *env_value)
        battery.misc = atoi(env_value);
 }
 
+static void notify_charger_event(int notifier)
+{
+       struct battery_config_charger_event_handler **event_handlers =
+                                       g_battery_info.event_handlers;
+       int event_handler_number = g_battery_info.event_handler_number;
+
+       if (!event_handlers || event_handler_number <= 0) {
+               _D("There is no charger event handler");
+               return;
+       }
+
+       for (int i = 0; i < event_handler_number; ++i) {
+               if (!event_handlers[i])
+                       continue;
+
+               if (event_handlers[i]->notifier == notifier) {
+                       _D("Battery charger event=%s(%d)",
+                               event_handlers[i]->name,
+                               event_handlers[i]->id);
+
+                       if (event_handlers[i]->wakelock_duration)
+                               event_acquire_wakelock(event_handlers[i]->id,
+                                               event_handlers[i]->wakelock_duration);
+
+                       if (event_handlers[i]->broadcast)
+                               event_broadcast_id(event_handlers[i]->id);
+               }
+       }
+}
+
 static void process_power_supply(void *data)
 {
        bool broadcasted = true;
@@ -686,10 +720,12 @@ static void process_power_supply(void *data)
                        CRITICAL_LOG("Battery charger disconnected");
                        event_system_send(SYS_EVENT_BATTERY_CHARGER_STATUS, EVT_KEY_BATTERY_CHARGER_STATUS, EVT_VAL_BATTERY_CHARGER_CONNECTED);
                        device_notify(DEVICE_NOTIFIER_BATTERY_CHARGER_DISCONNECTED, NULL);
+                       notify_charger_event(DEVICE_NOTIFIER_BATTERY_CHARGER_DISCONNECTED);
                } else if (battery.charger_connected == 1) {
                        CRITICAL_LOG("Battery charger connected");
                        event_system_send(SYS_EVENT_BATTERY_CHARGER_STATUS, EVT_KEY_BATTERY_CHARGER_STATUS, EVT_VAL_BATTERY_CHARGER_DISCONNECTED);
                        device_notify(DEVICE_NOTIFIER_BATTERY_CHARGER_CONNECTED, NULL);
+                       notify_charger_event(DEVICE_NOTIFIER_BATTERY_CHARGER_CONNECTED);
                } else {
                        _E("Invalid charger connected");
                }
@@ -1453,7 +1489,7 @@ static int event_handler_state_changed(void *data)
 
 static void power_supply_init(void *data)
 {
-       int ret_dbus;
+       int ret;
 
        memset(&battery, 0, sizeof(struct battery_status));
        memset(&old_battery, 0, sizeof(struct battery_status));
@@ -1470,11 +1506,13 @@ static void power_supply_init(void *data)
        register_notifier(DEVICE_NOTIFIER_EVENT_HANDLER, event_handler_state_changed);
        register_notifier(DEVICE_NOTIFIER_LCD, handle_display_state_changed);
 
-       ret_dbus = gdbus_add_object(NULL, DEVICED_PATH_BATTERY, &dbus_interface);
-       if (ret_dbus < 0)
-               _E("Failed to init dbus method: %d", ret_dbus);
+       ret = gdbus_add_object(NULL, DEVICED_PATH_BATTERY, &dbus_interface);
+       if (ret < 0)
+               _E("Failed to init dbus method: %d", ret);
 
        battery_ops_init((void *)battery_plgn);
+
+       battery_parser_load_config(&g_battery_info);
 }
 
 static void power_supply_exit(void *data)
@@ -1487,6 +1525,8 @@ static void power_supply_exit(void *data)
        event_handler_state_changed((void *)&state);
 
        battery_ops_exit(NULL);
+
+       battery_parser_unload_config(&g_battery_info);
 }
 
 static const struct device_ops power_supply_ops = {