sampling.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #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. arg = thp->arg;
  17. genlmsg_parse(nlh, 0, attrs, THERMAL_GENL_ATTR_MAX, NULL);
  18. switch (genlhdr->cmd) {
  19. case THERMAL_GENL_SAMPLING_TEMP:
  20. return th->ops->sampling.tz_temp(
  21. nla_get_u32(attrs[THERMAL_GENL_ATTR_TZ_ID]),
  22. nla_get_u32(attrs[THERMAL_GENL_ATTR_TZ_TEMP]), arg);
  23. default:
  24. return THERMAL_ERROR;
  25. }
  26. }
  27. thermal_error_t thermal_sampling_handle(struct thermal_handler *th, void *arg)
  28. {
  29. struct thermal_handler_param thp = { .th = th, .arg = arg };
  30. if (!th)
  31. return THERMAL_ERROR;
  32. if (nl_cb_set(th->cb_sampling, NL_CB_VALID, NL_CB_CUSTOM,
  33. handle_thermal_sample, &thp))
  34. return THERMAL_ERROR;
  35. return nl_recvmsgs(th->sk_sampling, th->cb_sampling);
  36. }
  37. int thermal_sampling_fd(struct thermal_handler *th)
  38. {
  39. if (!th)
  40. return -1;
  41. return nl_socket_get_fd(th->sk_sampling);
  42. }
  43. thermal_error_t thermal_sampling_exit(struct thermal_handler *th)
  44. {
  45. if (nl_unsubscribe_thermal(th->sk_sampling, th->cb_sampling,
  46. THERMAL_GENL_SAMPLING_GROUP_NAME))
  47. return THERMAL_ERROR;
  48. nl_thermal_disconnect(th->sk_sampling, th->cb_sampling);
  49. return THERMAL_SUCCESS;
  50. }
  51. thermal_error_t thermal_sampling_init(struct thermal_handler *th)
  52. {
  53. if (nl_thermal_connect(&th->sk_sampling, &th->cb_sampling))
  54. return THERMAL_ERROR;
  55. if (nl_subscribe_thermal(th->sk_sampling, th->cb_sampling,
  56. THERMAL_GENL_SAMPLING_GROUP_NAME))
  57. return THERMAL_ERROR;
  58. return THERMAL_SUCCESS;
  59. }