device-battery: Add hal-api passthrough module 84/320884/2
authorYoungjae Cho <y0.cho@samsung.com>
Fri, 7 Mar 2025 06:34:27 +0000 (15:34 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Wed, 12 Mar 2025 12:07:48 +0000 (21:07 +0900)
As hal-api module supporting ipc communication, it is required
to distinguish passthrough and ipc functions, so fix the existing
functions to have 'passthrough' in their name.

Change-Id: Ib83d4fe5154985fefb98fef373dbf57630b40aec
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
src/hal-api-device-battery-passthrough.c [new file with mode: 0644]
src/hal-api-device-battery-passthrough.h [new file with mode: 0644]

diff --git a/src/hal-api-device-battery-passthrough.c b/src/hal-api-device-battery-passthrough.c
new file mode 100644 (file)
index 0000000..398e80c
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2021 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 <stdlib.h>
+
+#include <hal/hal-common.h>
+
+#include "hal-device-battery-interface.h"
+#include "common.h"
+
+static hal_backend_device_battery_funcs *hal_device_battery_funcs = NULL;
+
+int hal_device_battery_passthrough_get_backend(void)
+{
+       int ret;
+
+       if (hal_device_battery_funcs)
+               return 0;
+
+       hal_device_battery_funcs = calloc(1, sizeof(hal_backend_device_battery_funcs));
+       if (!hal_device_battery_funcs)
+               return -ENOMEM;
+
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_BATTERY, (void **)&hal_device_battery_funcs);
+       if (ret < 0) {
+                _E("Failed to get device-battery backend");
+                free(hal_device_battery_funcs);
+                hal_device_battery_funcs = NULL;
+               return -ENOTSUP;
+       }
+
+       return 0;
+}
+
+int hal_device_battery_passthrough_put_backend(void)
+{
+       int ret = 0;
+
+       if (!hal_device_battery_funcs)
+               return 0;
+
+       ret = hal_common_put_backend(HAL_MODULE_DEVICE_BATTERY, (void *)hal_device_battery_funcs);
+       if (ret < 0) {
+               _E("Failed to put device-battery backend");
+               return ret;
+       }
+
+       free(hal_device_battery_funcs);
+       hal_device_battery_funcs = NULL;
+
+       return 0;
+}
+
+int hal_device_battery_passthrough_register_changed_event(hal_device_battery_updated_cb updated_cb, void *user_data)
+{
+       int ret;
+
+       if (!updated_cb)
+               return -EINVAL;
+
+       if (!hal_device_battery_funcs) {
+               if ((ret = hal_device_battery_passthrough_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_device_battery_funcs ||
+               !hal_device_battery_funcs->register_changed_event)
+               return -ENOTSUP;
+
+       return hal_device_battery_funcs->register_changed_event(updated_cb, user_data);
+}
+
+int hal_device_battery_passthrough_unregister_changed_event(hal_device_battery_updated_cb updated_cb)
+{
+       int ret;
+
+       if (!updated_cb)
+               return -EINVAL;
+
+       if (!hal_device_battery_funcs) {
+               if ((ret = hal_device_battery_passthrough_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_device_battery_funcs ||
+               !hal_device_battery_funcs->unregister_changed_event)
+               return -ENOTSUP;
+
+       hal_device_battery_funcs->unregister_changed_event(updated_cb);
+       return 0;
+}
+
+int hal_device_battery_passthrough_get_current_state(hal_device_battery_updated_cb updated_cb, void *user_data)
+{
+       int ret;
+
+       if (!updated_cb)
+               return -EINVAL;
+
+       if (!hal_device_battery_funcs) {
+               if ((ret = hal_device_battery_passthrough_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_device_battery_funcs ||
+               !hal_device_battery_funcs->get_current_state)
+               return -ENOTSUP;
+
+       return hal_device_battery_funcs->get_current_state(updated_cb, user_data);
+}
+
+int hal_device_battery_passthrough_get_power_source_name(hal_device_battery_power_source_type_e power_source_type, const char **power_source)
+{
+       if (!power_source)
+               return -EINVAL;
+
+       switch (power_source_type) {
+       case HAL_DEVICE_BATTERY_POWER_SOURCE_AC:
+               *power_source = "ac";
+               break;
+       case HAL_DEVICE_BATTERY_POWER_SOURCE_USB:
+               *power_source = "usb";
+               break;
+       case HAL_DEVICE_BATTERY_POWER_SOURCE_WIRELESS:
+               *power_source = "wireless";
+               break;
+       case HAL_DEVICE_BATTERY_POWER_SOURCE_NONE:
+       default:
+               *power_source = "none";
+               break;
+       }
+
+       return 0;
+}
+
+int hal_device_battery_passthrough_get_power_source_type(char *power_source, hal_device_battery_power_source_type_e *power_source_type)
+{
+       if (!power_source || !power_source_type)
+               return -EINVAL;
+
+       if (strcmp(power_source, "ac") == 0)
+               *power_source_type = HAL_DEVICE_BATTERY_POWER_SOURCE_AC;
+       else if (strcmp(power_source, "usb") == 0)
+               *power_source_type = HAL_DEVICE_BATTERY_POWER_SOURCE_USB;
+       else if (strcmp(power_source, "wireless") == 0)
+               *power_source_type = HAL_DEVICE_BATTERY_POWER_SOURCE_WIRELESS;
+       else
+               *power_source_type = HAL_DEVICE_BATTERY_POWER_SOURCE_NONE;
+
+       return 0;
+}
\ No newline at end of file
diff --git a/src/hal-api-device-battery-passthrough.h b/src/hal-api-device-battery-passthrough.h
new file mode 100644 (file)
index 0000000..afe68b1
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2021 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.
+ */
+
+
+#ifndef __HAL_DEVICE_BATTERY_PASSTHROUGH_H__
+#define __HAL_DEVICE_BATTERY_PASSTHROUGH_H__
+
+#include "hal-device-battery-interface.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int hal_device_battery_passthrough_get_backend(void);
+int hal_device_battery_passthrough_put_backend(void);
+int hal_device_battery_passthrough_register_changed_event(hal_device_battery_updated_cb updated_cb, void *user_data);
+int hal_device_battery_passthrough_unregister_changed_event(hal_device_battery_updated_cb updated_cb);
+int hal_device_battery_passthrough_get_current_state(hal_device_battery_updated_cb updated_cb, void *user_data);
+int hal_device_battery_passthrough_get_power_source_name(hal_device_battery_power_source_type_e power_source_type, const char **power_source);
+int hal_device_battery_passthrough_get_power_source_type(char *power_source, hal_device_battery_power_source_type_e *power_source_type);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // __HAL_DEVICE_BATTERY_PASSTHROUGH_H__