summaryrefslogtreecommitdiff
path: root/thermal-genl.c
blob: 82966c292885e9fbe7491ef878ba6f66d9fc5545 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include <stdio.h>
#include <errno.h>
#include <libnl3/netlink/genl/genl.h>
#include <libnl3/netlink/genl/mngt.h>
#include <libnl3/netlink/genl/ctrl.h>
#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>

#include "thermal.h"

static int nl_error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
			    void *arg)
{
	int *ret = arg;

	*ret = err->error;

	printf("%s\n", __func__);

	return NL_STOP;
}

static int nl_finish_handler(struct nl_msg *msg, void *arg)
{
	int *ret = arg;

	*ret = 1;

	/* printf("%s\n", __func__); */
		
	return NL_OK;
}

static int nl_ack_handler(struct nl_msg *msg, void *arg)
{
	int *ret = arg;

	*ret = 1;

	printf("%s\n", __func__);
		
	return NL_OK;
}

static int nl_overrun_handler(struct nl_msg *msg, void *arg)
{
	printf("%s\n", __func__);
	
	return NL_SKIP;
}

static int nl_skipped_handler(struct nl_msg *msg, void *arg)
{
	printf("%s\n", __func__);
	
	return NL_SKIP;
}

static int nl_seq_check_handler(struct nl_msg *msg, void *arg)
{
	printf("%s\n", __func__);
	
	return NL_OK;
}

int show_tz(struct genl_info *info)
{
	struct nlattr *attr;
	int rem;

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

		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_ID)
			printf("Thermal zone id: %d\n", nla_get_u32(attr));
		
		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_NAME)
			printf("Thermal zone name: %s\n",
			       nla_get_string(attr));
	}

	return 0;
}

int show_cdev(struct genl_info *info)
{
	struct nlattr *attr;
	int rem;

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

		if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_ID)
			printf("Cooling device id: %d\n", nla_get_u32(attr));

		if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_NAME)
			printf("Thermal zone name: %s\n", nla_get_string(attr));
	}

	return 0;
}

int show_trip(struct genl_info *info)
{
	struct nlattr *attr;
	int rem;

	if (info->attrs[THERMAL_GENL_ATTR_TZ_TRIP])
		printf("Trip zone !!!\n"); 

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

		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_NAME)
			printf("Thermal zone name: %s\n",
			       nla_get_string(attr));
		
		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_ID)
			printf("Trip id: %d\n", nla_get_u32(attr));

		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_TYPE)
			printf("Trip type: %d\n", nla_get_u32(attr));

		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_TEMP)
			printf("Trip temp: %d\n", nla_get_u32(attr));

		if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_HYST)
			printf("Trip hyst: %d\n", nla_get_u32(attr));
	}
	
	return 0;
}

int show_temp(struct genl_info *info)
{
	if (info->attrs[THERMAL_GENL_ATTR_TZ_TEMP])
		printf("Thermal zone0 temperature: %d\n",
		       nla_get_u32(info->attrs[THERMAL_GENL_ATTR_TZ_TEMP]));

	return 0;
}

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_MIN_STATE]      = { .type = NLA_U32 },
	[THERMAL_GENL_ATTR_CDEV_NAME]           = { .type = NLA_STRING },
};

static int handle_sampling(struct nl_msg *n, void *arg)
{
	struct nlmsghdr *nlh = nlmsg_hdr(n);
	struct genlmsghdr *genlhdr = genlmsg_hdr(nlh);
	struct nlattr *attrs[THERMAL_GENL_ATTR_MAX + 1];
	
	genlmsg_parse(nlh, 0, attrs, THERMAL_GENL_ATTR_MAX, NULL);
	
	if (genlhdr->cmd == THERMAL_GENL_SAMPLING_TEMP) {
		printf("THERMAL_GENL_SAMPLING_TEMP\n");

		if (attrs[THERMAL_GENL_ATTR_TZ_ID])
			printf("Thermal zone %d\n",
			       nla_get_u32(attrs[THERMAL_GENL_ATTR_TZ_ID]));

		if (attrs[THERMAL_GENL_ATTR_TZ_TEMP])
			printf("Thermal zone temp %d\n",
			       nla_get_u32(attrs[THERMAL_GENL_ATTR_TZ_TEMP]));
	}

	return 0;
}

static int handle_netlink(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:
		printf("THERMAL_GENL_CMD_TZ_GET\n");
		show_tz(info);
		break;

	case THERMAL_GENL_CMD_CDEV_GET:
		printf("THERMAL_GENL_CMD_CDEV_GET\n");
		show_cdev(info);
		break;
		
	case THERMAL_GENL_CMD_TZ_GET_TEMP:
		printf("THERMAL_GENL_CMD_TZ_GET_TEMP\n");
		show_temp(info);
		break;

	case THERMAL_GENL_CMD_TZ_GET_TRIP:
		printf("THERMAL_GENL_CMD_TZ_GET_TRIP\n");
		show_trip(info);
		break;
	};

	return 0;
}

static struct genl_cmd thermal_cmds[] = {
	{
		.c_id		= THERMAL_GENL_CMD_TZ_GET,
		.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_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 nl_sock *sock, int family, int id, int cmd, int flags)
{
	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, family, 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_auto_complete(sock, msg) < 0) {
		fprintf(stderr, "Failed to nl_send_auto_complete\n");
		return -1;
	}

	{
		struct nl_cb *cb;
		int err = 0, done = 0;

		cb = nl_cb_alloc(NL_CB_DEFAULT);

		// nl_cb_err(cb, NL_CB_CUSTOM, nl_error_handler, &err);
		nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, nl_finish_handler, &done);
		// nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, nl_ack_handler, &done);
		// nl_cb_set(cb, NL_CB_OVERRUN, NL_CB_CUSTOM, nl_overrun_handler, &done);
		// nl_cb_set(cb, NL_CB_SKIPPED, NL_CB_CUSTOM, nl_skipped_handler, &done);
		nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl_seq_check_handler, &done);
		nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, genl_handle_msg, NULL);

		while (!done && !err)
			nl_recvmsgs(sock, cb);
	}

	return 0;
}

struct handler_args {
	const char *group;
	int id;
};

static int nl_send_msg(struct nl_sock *sock, struct nl_msg *msg,
		       int (*rx_handler)(struct nl_msg *, void *),
		       void *data)
{
	struct nl_cb *cb;
	int err, done;

	cb = nl_cb_alloc(NL_CB_DEFAULT);
	if (cb == NULL)
		return -ENOMEM;

	err = nl_send_auto_complete(sock, msg);
	if (err < 0) {
		nl_cb_put(cb);
		return err;
	}

	err = done = 0;
	nl_cb_err(cb, NL_CB_CUSTOM, nl_error_handler, &err);
	nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, nl_finish_handler, &done);
	nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, nl_ack_handler, &done);

	if (rx_handler != NULL)
		nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, rx_handler, data);

	while (err == 0 && done == 0)
		nl_recvmsgs(sock, cb);

	nl_cb_put(cb);

	return err;
}

static int nl_family_handler(struct nl_msg *msg, void *arg)
{
	struct handler_args *grp = arg;
	struct nlattr *tb[CTRL_ATTR_MAX + 1];
	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
	struct nlattr *mcgrp;
	int rem_mcgrp;

	nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
		  genlmsg_attrlen(gnlh, 0), NULL);

	if (!tb[CTRL_ATTR_MCAST_GROUPS]) {
		fprintf(stderr, "Multicast group not found\n");
		return -1;
	}

	nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], rem_mcgrp) {

		struct nlattr *tb_mcgrp[CTRL_ATTR_MCAST_GRP_MAX + 1];

		nla_parse(tb_mcgrp, CTRL_ATTR_MCAST_GRP_MAX,
			  nla_data(mcgrp), nla_len(mcgrp), NULL);

		if (!tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME] ||
		    !tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID])
			continue;

		if (strncmp(nla_data(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]),
			    grp->group,
			    nla_len(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME])))
			continue;

		grp->id = nla_get_u32(tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]);

		break;
	}
	
	return 0;
}

static int nl_get_multicast_id(struct nl_sock *sock, const char *family,
			       const char *group)
{
	struct nl_msg *msg;
	int err = 0, ctrlid;
	struct handler_args grp = {
		.group = group,
		.id = -ENOENT,
	};

	msg = nlmsg_alloc();
	if (!msg)
		return -ENOMEM;
	
	ctrlid = genl_ctrl_resolve(sock, "nlctrl");

	genlmsg_put(msg, 0, 0, ctrlid, 0, 0, CTRL_CMD_GETFAMILY, 0);

	nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family);

	err = nl_send_msg(sock, msg, nl_family_handler, &grp);
	if (err)
		goto nla_put_failure;

	err = grp.id;

nla_put_failure:
	nlmsg_free(msg);
	return err;
}

int main(int argc, char *argv[])
{
	struct nl_sock *sk_cmd;
	struct nl_sock *sk_sample;
	int ret, family;
	int mcid;

	nl_debug = 0;

	sk_cmd = nl_socket_alloc();
	if (!sk_cmd) {
		perror("nl_socket_alloc");
		return -1;
	}

	nl_socket_modify_cb(sk_cmd, NL_CB_VALID, NL_CB_CUSTOM,
			    genl_handle_msg, NULL);

	ret = genl_connect(sk_cmd);
	if (ret) {
		fprintf(stderr, "genl_connect: %d\n", ret);
		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(sk_cmd, &thermal_cmd_ops);
	if (ret) {
		fprintf(stderr, "genl_ops_resolve: %d\n", ret);
		return -1;
	}

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

	goto listen;
	
	ret = thermal_genl_auto(sk_cmd, thermal_cmd_ops.o_id, -1,
				THERMAL_GENL_CMD_TZ_GET,
				NLM_F_DUMP | NLM_F_ACK);
	if (ret < 0) {
		fprintf(stderr, "Failed to thermal_genl_send: %d\n", ret);
		return -1;
	}

	ret = thermal_genl_auto(sk_cmd, thermal_cmd_ops.o_id, -1,
				THERMAL_GENL_CMD_CDEV_GET,
				NLM_F_DUMP | NLM_F_ACK);
	if (ret < 0) {
		fprintf(stderr, "Failed to thermal_genl_send: %d\n", ret);
		return -1;
	}

	ret = thermal_genl_auto(sk_cmd, thermal_cmd_ops.o_id, 1,
				THERMAL_GENL_CMD_TZ_GET_TEMP, 0);
	if (ret < 0) {
		fprintf(stderr, "Failed to thermal_genl_send: %d\n", ret);
		return -1;
	}

	ret = thermal_genl_auto(sk_cmd, thermal_cmd_ops.o_id, 1,
				THERMAL_GENL_CMD_TZ_GET_TRIP, 0);
	if (ret < 0) {
		fprintf(stderr, "Failed to thermal_genl_send: %d\n", ret);
		return -1;
	}
listen:
	/* Sampling receiving */
	sk_sample = nl_socket_alloc();
	if (!sk_sample) {
		fprintf(stderr, "nl_socket_alloc failed\n");
		return -1;
	}
	
	if (genl_connect(sk_sample)) {
		fprintf(stderr, "genl_connect(sk_sample) failed\n");
		return -1;
	}

	mcid = nl_get_multicast_id(sk_sample, THERMAL_GENL_FAMILY_NAME,
				   THERMAL_GENL_SAMPLING_GROUP_NAME);
	if (mcid < 0) {
		fprintf(stderr, "nl_get_multicast_id failed\n");
		return -1;
	}

	if (nl_socket_add_membership(sk_sample, mcid)) {
		fprintf(stderr, "nl_socket_add_membership failed");
		return -1;
	}

	{
		struct nl_cb *cb;
		int err = 0, done = 0;

		cb = nl_cb_alloc(NL_CB_DEFAULT);

		// nl_cb_err(cb, NL_CB_CUSTOM, nl_error_handler, &err);
		nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, nl_finish_handler, &done);
		// nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, nl_ack_handler, &done);
		// nl_cb_set(cb, NL_CB_OVERRUN, NL_CB_CUSTOM, nl_overrun_handler, &done);
		// nl_cb_set(cb, NL_CB_SKIPPED, NL_CB_CUSTOM, nl_skipped_handler, &done);
		nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl_seq_check_handler, &done);
		nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, handle_sampling, NULL);

		while (!done && !err)
			nl_recvmsgs(sk_sample, cb);
	}
	
	return 0;
}