Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: LGPL-2.1+
0002 // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
0003 #include <errno.h>
0004 #include <stdio.h>
0005 #include <stdlib.h>
0006 #include <unistd.h>
0007 
0008 #include <thermal.h>
0009 #include "thermal_nl.h"
0010 
0011 static int handle_thermal_sample(struct nl_msg *n, void *arg)
0012 {
0013     struct nlmsghdr *nlh = nlmsg_hdr(n);
0014     struct genlmsghdr *genlhdr = genlmsg_hdr(nlh);
0015     struct nlattr *attrs[THERMAL_GENL_ATTR_MAX + 1];
0016     struct thermal_handler_param *thp = arg;
0017     struct thermal_handler *th = thp->th;
0018 
0019     genlmsg_parse(nlh, 0, attrs, THERMAL_GENL_ATTR_MAX, NULL);
0020 
0021     switch (genlhdr->cmd) {
0022 
0023     case THERMAL_GENL_SAMPLING_TEMP:
0024         return th->ops->sampling.tz_temp(
0025             nla_get_u32(attrs[THERMAL_GENL_ATTR_TZ_ID]),
0026             nla_get_u32(attrs[THERMAL_GENL_ATTR_TZ_TEMP]), arg);
0027     default:
0028         return THERMAL_ERROR;
0029     }
0030 }
0031 
0032 thermal_error_t thermal_sampling_handle(struct thermal_handler *th, void *arg)
0033 {
0034     struct thermal_handler_param thp = { .th = th, .arg = arg };
0035 
0036     if (!th)
0037         return THERMAL_ERROR;
0038 
0039     if (nl_cb_set(th->cb_sampling, NL_CB_VALID, NL_CB_CUSTOM,
0040               handle_thermal_sample, &thp))
0041         return THERMAL_ERROR;
0042 
0043     return nl_recvmsgs(th->sk_sampling, th->cb_sampling);
0044 }
0045 
0046 int thermal_sampling_fd(struct thermal_handler *th)
0047 {
0048     if (!th)
0049         return -1;
0050 
0051     return nl_socket_get_fd(th->sk_sampling);
0052 }
0053 
0054 thermal_error_t thermal_sampling_exit(struct thermal_handler *th)
0055 {
0056     if (nl_unsubscribe_thermal(th->sk_sampling, th->cb_sampling,
0057                    THERMAL_GENL_EVENT_GROUP_NAME))
0058         return THERMAL_ERROR;
0059 
0060     nl_thermal_disconnect(th->sk_sampling, th->cb_sampling);
0061 
0062     return THERMAL_SUCCESS;
0063 }
0064 
0065 thermal_error_t thermal_sampling_init(struct thermal_handler *th)
0066 {
0067     if (nl_thermal_connect(&th->sk_sampling, &th->cb_sampling))
0068         return THERMAL_ERROR;
0069 
0070     if (nl_subscribe_thermal(th->sk_sampling, th->cb_sampling,
0071                  THERMAL_GENL_SAMPLING_GROUP_NAME))
0072         return THERMAL_ERROR;
0073 
0074     return THERMAL_SUCCESS;
0075 }