summaryrefslogtreecommitdiff
path: root/src/resource/resource-network.c
blob: a46754853e29c7691ed6572f30c49e275d82d662 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * 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 <glib.h>

#include <util/common.h>
#include <util/devices.h>
#include <util/log.h>

#include <libsyscommon/resource-manager.h>
#include <libsyscommon/resource-type.h>
#include <libsyscommon/resource-device.h>

#include <resource-monitor/resource-monitor.h>

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)