/* * PASS (Power Aware System Service) - Network 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-network.c * @brief Provide network resource driver supporting the various resource * attributes. It supports the network attributes such as network * bandwidth and so on. * @ingroup RESOURCE_MONITOR */ #include #include #include #include #include #include #include #include struct network_context { char *device_name; int index; }; static int network_get_name(int resource_id, const struct syscommon_resman_resource_attribute *attr, void *data) { struct network_context *ctx; char *buf = (char *)data; if (resource_id < 0 || !attr || !data) return -EINVAL; ctx = syscommon_resman_get_resource_privdata(resource_id); if (!ctx) return -EINVAL; if (!ctx->device_name) { _E("%s: NETWORK_CTRL_DEVICE_ID is not yet initialized\n", syscommon_resman_get_resource_name(resource_id)); return -EINVAL; } strncpy(buf, ctx->device_name, BUFF_MAX); return 0; } static const struct syscommon_resman_resource_attribute network_attrs[] = { { .name = "NETWORK_ATTR_NAME", .id = NETWORK_ATTR_NAME, .type = SYSCOMMON_RESMAN_DATA_TYPE_STRING, .flag = SYSCOMMON_RESMAN_RESOURCE_ATTR_FLAG_PRIVATE, .ops = { .get = network_get_name, }, }, }; static int network_setup_device_id(int resource_id, const struct syscommon_resman_resource_control *ctrl, const void *data) { struct network_context *ctx; const struct syscommon_resman_resource_device *device; int resource_index = (int)(intptr_t)data; if (resource_id < 0 || !ctrl) return -EINVAL; ctx = syscommon_resman_get_resource_privdata(resource_id); if (!ctx) return -EINVAL; device = syscommon_resman_find_resource_device(syscommon_resman_get_resource_type(resource_id), resource_index); if (!device) { _E("Not available resource: type: %s, index: %d\n", syscommon_resman_get_resource_name(resource_id), resource_index); return -EINVAL; } if (ctx->device_name) free(ctx->device_name); ctx->device_name = g_strdup(device->name); ctx->index = resource_index; return 0; } static const struct syscommon_resman_resource_control network_ctrls[] = { { .name = "NETWORK_CTRL_DEVICE_ID", .id = NETWORK_CTRL_DEVICE_ID, .ops = { .set = network_setup_device_id, }, }, }; static int network_create(int resource_id) { struct network_context *ctx; ctx = calloc(1, sizeof(struct network_context)); if (!ctx) return -ENOMEM; ctx->index = -1; syscommon_resman_set_resource_privdata(resource_id, ctx); return 0; } static void network_delete(int resource_id) { struct network_context *ctx; if (resource_id < 0) return; ctx = syscommon_resman_get_resource_privdata(resource_id); if (!ctx) return; if (ctx->device_name) free(ctx->device_name); free(ctx); syscommon_resman_set_resource_privdata(resource_id, NULL); } static const struct syscommon_resman_resource_driver network_resource_driver = { .name = "NETWORK", .type = RESOURCE_TYPE_NETWORK, .attrs = network_attrs, .num_attrs = ARRAY_SIZE(network_attrs), .ctrls = network_ctrls, .num_ctrls = ARRAY_SIZE(network_ctrls), .ops = { .create = network_create, .delete = network_delete, }, }; SYSCOMMON_RESMAN_RESOURCE_DRIVER_REGISTER(&network_resource_driver)