78 lines · c
1// SPDX-License-Identifier: LGPL-2.1+2// Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>3#include <errno.h>4#include <stdio.h>5#include <stdlib.h>6#include <unistd.h>7 8#include <thermal.h>9#include "thermal_nl.h"10 11static int handle_thermal_sample(struct nl_msg *n, void *arg)12{13 struct nlmsghdr *nlh = nlmsg_hdr(n);14 struct genlmsghdr *genlhdr = genlmsg_hdr(nlh);15 struct nlattr *attrs[THERMAL_GENL_ATTR_MAX + 1];16 struct thermal_handler_param *thp = arg;17 struct thermal_handler *th = thp->th;18 19 arg = thp->arg;20 21 genlmsg_parse(nlh, 0, attrs, THERMAL_GENL_ATTR_MAX, NULL);22 23 switch (genlhdr->cmd) {24 25 case THERMAL_GENL_SAMPLING_TEMP:26 return th->ops->sampling.tz_temp(27 nla_get_u32(attrs[THERMAL_GENL_ATTR_TZ_ID]),28 nla_get_u32(attrs[THERMAL_GENL_ATTR_TZ_TEMP]), arg);29 default:30 return THERMAL_ERROR;31 }32}33 34thermal_error_t thermal_sampling_handle(struct thermal_handler *th, void *arg)35{36 struct thermal_handler_param thp = { .th = th, .arg = arg };37 38 if (!th)39 return THERMAL_ERROR;40 41 if (nl_cb_set(th->cb_sampling, NL_CB_VALID, NL_CB_CUSTOM,42 handle_thermal_sample, &thp))43 return THERMAL_ERROR;44 45 return nl_recvmsgs(th->sk_sampling, th->cb_sampling);46}47 48int thermal_sampling_fd(struct thermal_handler *th)49{50 if (!th)51 return -1;52 53 return nl_socket_get_fd(th->sk_sampling);54}55 56thermal_error_t thermal_sampling_exit(struct thermal_handler *th)57{58 if (nl_unsubscribe_thermal(th->sk_sampling, th->cb_sampling,59 THERMAL_GENL_SAMPLING_GROUP_NAME))60 return THERMAL_ERROR;61 62 nl_thermal_disconnect(th->sk_sampling, th->cb_sampling);63 64 return THERMAL_SUCCESS;65}66 67thermal_error_t thermal_sampling_init(struct thermal_handler *th)68{69 if (nl_thermal_connect(&th->sk_sampling, &th->cb_sampling))70 return THERMAL_ERROR;71 72 if (nl_subscribe_thermal(th->sk_sampling, th->cb_sampling,73 THERMAL_GENL_SAMPLING_GROUP_NAME))74 return THERMAL_ERROR;75 76 return THERMAL_SUCCESS;77}78