--- /dev/null
+/*
+ * PASS (Power Aware System Service) - CPU Resource Driver
+ *
+ * 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.
+ */
+
+/**
+ * @file resource-memory.c
+ * @brief TBD
+ * @ingroup TBD
+ */
+
+#include <glib.h>
+
+#include <hal/hal-power.h>
+
+#include <util/common.h>
+#include <util/log.h>
+#include <util/resource.h>
+
+#include <monitor/monitor.h>
+
+#define PROC_MEM_INFO_PATH "/proc/meminfo"
+#define PROC_MEM_INFO_MEM_TOTAL "MemTotal"
+#define PROC_MEM_INFO_MEM_AVAILABLE "MemAvailable"
+#define PROC_MEM_INFO_MEM_FREE "MemFree"
+
+static int __memory_read_val_from_proc_node(const char *key)
+{
+ int ret = -EINVAL;
+ char buf[BUFF_MAX];
+ char str[BUFF_MAX];
+ FILE *fp = NULL;
+
+ fp = fopen(PROC_MEM_INFO_PATH, "r");
+ if (!fp)
+ return -EIO;
+
+ while (fgets(buf, BUFF_MAX, fp)) {
+ if (!strncmp(buf, key, strlen(key))) {
+ sscanf(buf, "%s %d", str, &ret);
+ break;
+ }
+ }
+ fclose(fp);
+
+ return ret;
+}
+
+static inline int memory_read_val_from_proc_node(uint32_t val_id)
+{
+ switch (val_id) {
+ case MEMORY_TOTAL:
+ return __memory_read_val_from_proc_node(PROC_MEM_INFO_MEM_TOTAL);
+ case MEMORY_AVAILABLE:
+ return __memory_read_val_from_proc_node(PROC_MEM_INFO_MEM_AVAILABLE);
+ case MEMORY_FREE:
+ return __memory_read_val_from_proc_node(PROC_MEM_INFO_MEM_FREE);
+ }
+ return -EINVAL;
+}
+
+static int memory_get_total_memory(const struct resource *res,
+ const struct resource_attribute *attr,
+ char *buf, void *user_data)
+{
+ int val;
+
+ if (!res || !attr || !buf)
+ return -EINVAL;
+
+ val = memory_read_val_from_proc_node(attr->id);
+ if (val < 0)
+ return -EINVAL;
+
+ return sprintf(buf, "%d", val);
+}
+
+static int memory_get_available_memory(const struct resource *res,
+ const struct resource_attribute *attr,
+ char *buf, void *user_data)
+{
+ int val;
+
+ if (!res || !attr || !buf)
+ return -EINVAL;
+
+ val = memory_read_val_from_proc_node(attr->id);
+ if (val < 0)
+ return -EINVAL;
+
+ return sprintf(buf, "%d", val);
+}
+
+static int memory_get_free_memory(const struct resource *res,
+ const struct resource_attribute *attr,
+ char *buf, void *user_data)
+{
+ int val;
+
+ if (!res || !attr || !buf)
+ return -EINVAL;
+
+ val = memory_read_val_from_proc_node(attr->id);
+ if (val < 0)
+ return -EINVAL;
+
+ return sprintf(buf, "%d", val);
+}
+
+static const struct resource_attribute memory_attrs[] = {
+ {
+ .name = "MEMORY_TOTAL",
+ .id = MEMORY_TOTAL,
+ .type = DATA_TYPE_INT,
+ .ops = {
+ .get = memory_get_total_memory,
+ },
+ }, {
+ .name = "MEMORY_AVAILABLE",
+ .id = MEMORY_AVAILABLE,
+ .type = DATA_TYPE_INT,
+ .ops = {
+ .get = memory_get_available_memory,
+ },
+ }, {
+ .name = "MEMORY_FREE",
+ .id = MEMORY_FREE,
+ .type = DATA_TYPE_INT,
+ .ops = {
+ .get = memory_get_free_memory,
+ }
+ },
+};
+
+static const struct resource_driver cpu_resource_driver = {
+ .name = "MEMORY",
+ .type = PASS_RESOURCE_MEMORY_ID,
+ .attrs = memory_attrs,
+ .num_attrs = ARRAY_SIZE(memory_attrs),
+};
+RESOURCE_DRIVER_REGISTER(&cpu_resource_driver)