summaryrefslogtreecommitdiff
path: root/src/commands.c
blob: 1a930d7ab78cb5f3155bd17b91e242431f048b26 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* SPDX-License-Identifier: LGPL-2.1+ */
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "thermal.h"

static struct nla_policy thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {
	/* Thermal zone */
	[THERMAL_GENL_ATTR_TZ]                  = { .type = NLA_NESTED },
	[THERMAL_GENL_ATTR_TZ_ID]               = { .type = NLA_U32 },
	[THERMAL_GENL_ATTR_TZ_TEMP]             = { .type = NLA_U32 },
	[THERMAL_GENL_ATTR_TZ_TRIP]             = { .type = NLA_NESTED },
	[THERMAL_GENL_ATTR_TZ_TRIP_ID]          = { .type = NLA_U32 },
	[THERMAL_GENL_ATTR_TZ_TRIP_TEMP]        = { .type = NLA_U32 },
	[THERMAL_GENL_ATTR_TZ_TRIP_TYPE]        = { .type = NLA_U32 },
	[THERMAL_GENL_ATTR_TZ_TRIP_HYST]        = { .type = NLA_U32 },
	[THERMAL_GENL_ATTR_TZ_MODE]             = { .type = NLA_U32 },
	[THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT]      = { .type = NLA_U32 },
	[THERMAL_GENL_ATTR_TZ_NAME]             = { .type = NLA_STRING },

	/* Governor(s) */
	[THERMAL_GENL_ATTR_TZ_GOV]              = { .type = NLA_NESTED },
	[THERMAL_GENL_ATTR_TZ_GOV_NAME]         = { .type = NLA_STRING },

	/* Cooling devices */
	[THERMAL_GENL_ATTR_CDEV]                = { .type = NLA_NESTED },
	[THERMAL_GENL_ATTR_CDEV_ID]             = { .type = NLA_U32 },
	[THERMAL_GENL_ATTR_CDEV_CUR_STATE]      = { .type = NLA_U32 },
	[THERMAL_GENL_ATTR_CDEV_MAX_STATE]      = { .type = NLA_U32 },
	[THERMAL_GENL_ATTR_CDEV_NAME]           = { .type = NLA_STRING },
};

int parse_tz_get(struct genl_info *info, struct thermal_zone **tz)
{
	struct nlattr *attr;
	struct thermal_zone *__tz = NULL;
	size_t size = 0;
	int rem;

	nla_for_each_nested(attr, info->attrs[THERMAL_GENL_ATTR_TZ], rem) {

		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_ID) {

			size++;

			__tz = realloc(__tz, sizeof(*__tz) * (size + 2));
			if (!__tz) {
				fprintf(stderr, "Failed to allocate memory\n");
				return -1;
			}

			__tz[size - 1].id = nla_get_u32(attr);
		}


		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_NAME)
			nla_strlcpy(__tz[size - 1].name, attr,
				    THERMAL_NAME_LENGTH);
	}

	/*
	 * We end the array of thermal zones
	 */
	__tz[size].id = -1;

	*tz = __tz;

	return 0;
}

int parse_cdev_get(struct genl_info *info, struct thermal_cdev **cdev)
{
	struct nlattr *attr;
	struct thermal_cdev *__cdev = NULL;
	size_t size = 0;
	int rem;

	nla_for_each_nested(attr, info->attrs[THERMAL_GENL_ATTR_CDEV], rem) {

		if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_ID) {

			size++;

			__cdev = realloc(__cdev, sizeof(*__cdev) * (size + 2));
			if (!__cdev) {
				fprintf(stderr, "Failed to allocate memory\n");
				return -1;
			}

			__cdev[size - 1].id = nla_get_u32(attr);
		}

		if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_NAME) {
			nla_strlcpy(__cdev[size - 1].name, attr,
				    THERMAL_NAME_LENGTH);
		}

		if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_CUR_STATE) {
			__cdev[size - 1].cur_state = nla_get_u32(attr);
		}

		if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_MAX_STATE) {
			__cdev[size - 1].max_state = nla_get_u32(attr);
		}
	}

	__cdev[size].id = -1;

	*cdev = __cdev;

	return 0;
}

int parse_tz_get_trip(struct genl_info *info, struct thermal_zone *tz)
{
	struct nlattr *attr;
	struct thermal_trip *__tt = NULL;
	size_t size = 0;
	int rem;

	nla_for_each_nested(attr, info->attrs[THERMAL_GENL_ATTR_TZ_TRIP], rem) {

		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_ID) {

			size++;

			__tt = realloc(__tt, sizeof(*__tt) * (size + 2));
			if (!__tt) {
				fprintf(stderr, "Failed to allocate memory\n");
				return -1;
			}

			__tt[size - 1].id = nla_get_u32(attr);
		}

		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_TYPE)
			__tt[size - 1].type = nla_get_u32(attr);

		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_TEMP)
			__tt[size - 1].temp = nla_get_u32(attr);

		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_HYST)
			__tt[size - 1].hyst = nla_get_u32(attr);
	}

	__tt[size].id = -1;

	tz->trip = __tt;

	return 0;
}

int parse_tz_get_temp(struct genl_info *info, struct thermal_zone *tz)
{
	int id = -1;

	if (info->attrs[THERMAL_GENL_ATTR_TZ_ID])
		id = nla_get_u32(info->attrs[THERMAL_GENL_ATTR_TZ_ID]);

	if (tz->id != id) {
		fprintf(stderr, "thermal zone id mismatch '%d' <> '%d'\n",
			tz->id, id);
		return -1;
	}

	if (info->attrs[THERMAL_GENL_ATTR_TZ_TEMP])
		tz->temp = nla_get_u32(info->attrs[THERMAL_GENL_ATTR_TZ_TEMP]);

	return 0;
}

int parse_tz_get_gov(struct genl_info *info, struct thermal_zone *tz)
{
	int id = -1;

	if (info->attrs[THERMAL_GENL_ATTR_TZ_ID])
		id = nla_get_u32(info->attrs[THERMAL_GENL_ATTR_TZ_ID]);

	if (tz->id != id) {
		fprintf(stderr, "thermal zone id mismatch '%d' <> '%d'\n",
			tz->id, id);
		return -1;
	}

	if (info->attrs[THERMAL_GENL_ATTR_TZ_GOV_NAME]) {
		nla_strlcpy(tz->governor,
			    info->attrs[THERMAL_GENL_ATTR_TZ_GOV_NAME],
			    THERMAL_NAME_LENGTH);
	}

	return 0;
}

static int handle_netlink(__maybe_unused struct nl_cache_ops *unused,
			  struct genl_cmd *cmd,
			  struct genl_info *info, void *arg)
{
	switch (cmd->c_id) {

	case THERMAL_GENL_CMD_TZ_GET_ID:
		parse_tz_get(info, arg);
		break;

	case THERMAL_GENL_CMD_CDEV_GET:
		parse_cdev_get(info, arg);
		break;

	case THERMAL_GENL_CMD_TZ_GET_TEMP:
		parse_tz_get_temp(info, arg);
		break;

	case THERMAL_GENL_CMD_TZ_GET_TRIP:
		parse_tz_get_trip(info, arg);
		break;

	case THERMAL_GENL_CMD_TZ_GET_GOV:
		parse_tz_get_gov(info, arg);
		break;

	default:
		printf("Unknown command id:%d\n", cmd->c_id);
	};

	return 0;
}

static struct genl_cmd thermal_cmds[] = {
	{
		.c_id		= THERMAL_GENL_CMD_TZ_GET_ID,
		.c_name		= "List thermal zones",
		.c_msg_parser	= handle_netlink,
		.c_maxattr	= THERMAL_GENL_ATTR_MAX,
		.c_attr_policy	= thermal_genl_policy,
	},
	{
		.c_id		= THERMAL_GENL_CMD_TZ_GET_GOV,
		.c_name		= "Get governor",
		.c_msg_parser	= handle_netlink,
		.c_maxattr	= THERMAL_GENL_ATTR_MAX,
		.c_attr_policy	= thermal_genl_policy,
	},
	{
		.c_id		= THERMAL_GENL_CMD_TZ_GET_TEMP,
		.c_name		= "Get thermal zone temperature",
		.c_msg_parser	= handle_netlink,
		.c_maxattr	= THERMAL_GENL_ATTR_MAX,
		.c_attr_policy	= thermal_genl_policy,
	},
	{
		.c_id		= THERMAL_GENL_CMD_TZ_GET_TRIP,
		.c_name		= "Get thermal zone trip points",
		.c_msg_parser	= handle_netlink,
		.c_maxattr	= THERMAL_GENL_ATTR_MAX,
		.c_attr_policy	= thermal_genl_policy,
	},
	{
		.c_id		= THERMAL_GENL_CMD_CDEV_GET,
		.c_name		= "Get cooling devices",
		.c_msg_parser	= handle_netlink,
		.c_maxattr	= THERMAL_GENL_ATTR_MAX,
		.c_attr_policy	= thermal_genl_policy,
	},
};

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

static struct genl_ops thermal_cmd_ops = {
	.o_name		= "thermal",
	.o_cmds		= thermal_cmds,
	.o_ncmds	= ARRAY_SIZE(thermal_cmds),
};

static int thermal_genl_auto(struct thermal_handler *th, int id, int cmd,
			     int flags, void *arg)
{
	struct nl_msg *msg;
	void *hdr;

	msg = nlmsg_alloc();
	if (!msg) {
		fprintf(stderr, "Failed to allocate message\n");
		return -1;
	}

	hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, thermal_cmd_ops.o_id,
			  0, flags, cmd, THERMAL_GENL_VERSION);
	if (!hdr) {
		fprintf(stderr, "Failed to set message\n");
		return -1;
	}

	if (id >= 0 && nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id)) {
		fprintf(stderr, "Failed to set tz id\n");
		return -1;
	}

	if (nl_send_msg(th->sk_cmd, th->cb_cmd, msg, genl_handle_msg, arg)) {
		fprintf(stderr, "Failed to send command\n");
		return -1;
	}

	nlmsg_free(msg);

	return 0;
}

int thermal_cmd_get_tz(struct thermal_handler *th, struct thermal_zone **tz)
{
	return thermal_genl_auto(th, -1, THERMAL_GENL_CMD_TZ_GET_ID,
				 NLM_F_DUMP | NLM_F_ACK, tz);
}

int thermal_cmd_get_cdev(struct thermal_handler *th, struct thermal_cdev **tc)
{
	return thermal_genl_auto(th, -1, THERMAL_GENL_CMD_CDEV_GET,
				 NLM_F_DUMP | NLM_F_ACK, tc);
}

int thermal_cmd_get_trip(struct thermal_handler *th, struct thermal_zone *tz)
{
	return thermal_genl_auto(th, tz->id, THERMAL_GENL_CMD_TZ_GET_TRIP,
				 0, tz);
}

int thermal_cmd_get_governor(struct thermal_handler *th, struct thermal_zone *tz)
{
	return thermal_genl_auto(th, tz->id, THERMAL_GENL_CMD_TZ_GET_GOV, 0, tz);
}

int thermal_cmd_get_temp(struct thermal_handler *th, struct thermal_zone *tz)
{
	return thermal_genl_auto(th, tz->id, THERMAL_GENL_CMD_TZ_GET_TEMP, 0, tz);
}

int thermal_cmd_init(struct thermal_handler *th)
{
	int ret;
	int family;

	if (nl_thermal_connect(&th->sk_cmd, &th->cb_cmd))
		return -1;

	ret = genl_register_family(&thermal_cmd_ops);
	if (ret) {
		fprintf(stderr, "genl_register_family: %d\n", ret);
		return -1;
	}

	ret = genl_ops_resolve(th->sk_cmd, &thermal_cmd_ops);
	if (ret) {
		fprintf(stderr, "genl_ops_resolve: %d\n", ret);
		return -1;
	}

	family = genl_ctrl_resolve(th->sk_cmd, "nlctrl");
	if (family != GENL_ID_CTRL) {
		fprintf(stderr, "genl_ctrl_resolve: %d\n", family);
		return -1;
	}

	return 0;
}