sampling.c 1.9 KB

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