commands.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: LGPL-2.1+
  2. // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <[email protected]>
  3. #define _GNU_SOURCE
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <thermal.h>
  9. #include "thermal_nl.h"
  10. static struct nla_policy thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {
  11. /* Thermal zone */
  12. [THERMAL_GENL_ATTR_TZ] = { .type = NLA_NESTED },
  13. [THERMAL_GENL_ATTR_TZ_ID] = { .type = NLA_U32 },
  14. [THERMAL_GENL_ATTR_TZ_TEMP] = { .type = NLA_U32 },
  15. [THERMAL_GENL_ATTR_TZ_TRIP] = { .type = NLA_NESTED },
  16. [THERMAL_GENL_ATTR_TZ_TRIP_ID] = { .type = NLA_U32 },
  17. [THERMAL_GENL_ATTR_TZ_TRIP_TEMP] = { .type = NLA_U32 },
  18. [THERMAL_GENL_ATTR_TZ_TRIP_TYPE] = { .type = NLA_U32 },
  19. [THERMAL_GENL_ATTR_TZ_TRIP_HYST] = { .type = NLA_U32 },
  20. [THERMAL_GENL_ATTR_TZ_MODE] = { .type = NLA_U32 },
  21. [THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT] = { .type = NLA_U32 },
  22. [THERMAL_GENL_ATTR_TZ_NAME] = { .type = NLA_STRING },
  23. /* Governor(s) */
  24. [THERMAL_GENL_ATTR_TZ_GOV] = { .type = NLA_NESTED },
  25. [THERMAL_GENL_ATTR_TZ_GOV_NAME] = { .type = NLA_STRING },
  26. /* Cooling devices */
  27. [THERMAL_GENL_ATTR_CDEV] = { .type = NLA_NESTED },
  28. [THERMAL_GENL_ATTR_CDEV_ID] = { .type = NLA_U32 },
  29. [THERMAL_GENL_ATTR_CDEV_CUR_STATE] = { .type = NLA_U32 },
  30. [THERMAL_GENL_ATTR_CDEV_MAX_STATE] = { .type = NLA_U32 },
  31. [THERMAL_GENL_ATTR_CDEV_NAME] = { .type = NLA_STRING },
  32. };
  33. static int parse_tz_get(struct genl_info *info, struct thermal_zone **tz)
  34. {
  35. struct nlattr *attr;
  36. struct thermal_zone *__tz = NULL;
  37. size_t size = 0;
  38. int rem;
  39. nla_for_each_nested(attr, info->attrs[THERMAL_GENL_ATTR_TZ], rem) {
  40. if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_ID) {
  41. size++;
  42. __tz = realloc(__tz, sizeof(*__tz) * (size + 2));
  43. if (!__tz)
  44. return THERMAL_ERROR;
  45. __tz[size - 1].id = nla_get_u32(attr);
  46. }
  47. if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_NAME)
  48. nla_strlcpy(__tz[size - 1].name, attr,
  49. THERMAL_NAME_LENGTH);
  50. }
  51. if (__tz)
  52. __tz[size].id = -1;
  53. *tz = __tz;
  54. return THERMAL_SUCCESS;
  55. }
  56. static int parse_cdev_get(struct genl_info *info, struct thermal_cdev **cdev)
  57. {
  58. struct nlattr *attr;
  59. struct thermal_cdev *__cdev = NULL;
  60. size_t size = 0;
  61. int rem;
  62. nla_for_each_nested(attr, info->attrs[THERMAL_GENL_ATTR_CDEV], rem) {
  63. if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_ID) {
  64. size++;
  65. __cdev = realloc(__cdev, sizeof(*__cdev) * (size + 2));
  66. if (!__cdev)
  67. return THERMAL_ERROR;
  68. __cdev[size - 1].id = nla_get_u32(attr);
  69. }
  70. if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_NAME) {
  71. nla_strlcpy(__cdev[size - 1].name, attr,
  72. THERMAL_NAME_LENGTH);
  73. }
  74. if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_CUR_STATE)
  75. __cdev[size - 1].cur_state = nla_get_u32(attr);
  76. if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_MAX_STATE)
  77. __cdev[size - 1].max_state = nla_get_u32(attr);
  78. }
  79. if (__cdev)
  80. __cdev[size].id = -1;
  81. *cdev = __cdev;
  82. return THERMAL_SUCCESS;
  83. }
  84. static int parse_tz_get_trip(struct genl_info *info, struct thermal_zone *tz)
  85. {
  86. struct nlattr *attr;
  87. struct thermal_trip *__tt = NULL;
  88. size_t size = 0;
  89. int rem;
  90. nla_for_each_nested(attr, info->attrs[THERMAL_GENL_ATTR_TZ_TRIP], rem) {
  91. if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_ID) {
  92. size++;
  93. __tt = realloc(__tt, sizeof(*__tt) * (size + 2));
  94. if (!__tt)
  95. return THERMAL_ERROR;
  96. __tt[size - 1].id = nla_get_u32(attr);
  97. }
  98. if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_TYPE)
  99. __tt[size - 1].type = nla_get_u32(attr);
  100. if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_TEMP)
  101. __tt[size - 1].temp = nla_get_u32(attr);
  102. if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_HYST)
  103. __tt[size - 1].hyst = nla_get_u32(attr);
  104. }
  105. if (__tt)
  106. __tt[size].id = -1;
  107. tz->trip = __tt;
  108. return THERMAL_SUCCESS;
  109. }
  110. static int parse_tz_get_temp(struct genl_info *info, struct thermal_zone *tz)
  111. {
  112. int id = -1;
  113. if (info->attrs[THERMAL_GENL_ATTR_TZ_ID])
  114. id = nla_get_u32(info->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  115. if (tz->id != id)
  116. return THERMAL_ERROR;
  117. if (info->attrs[THERMAL_GENL_ATTR_TZ_TEMP])
  118. tz->temp = nla_get_u32(info->attrs[THERMAL_GENL_ATTR_TZ_TEMP]);
  119. return THERMAL_SUCCESS;
  120. }
  121. static int parse_tz_get_gov(struct genl_info *info, struct thermal_zone *tz)
  122. {
  123. int id = -1;
  124. if (info->attrs[THERMAL_GENL_ATTR_TZ_ID])
  125. id = nla_get_u32(info->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  126. if (tz->id != id)
  127. return THERMAL_ERROR;
  128. if (info->attrs[THERMAL_GENL_ATTR_TZ_GOV_NAME]) {
  129. nla_strlcpy(tz->governor,
  130. info->attrs[THERMAL_GENL_ATTR_TZ_GOV_NAME],
  131. THERMAL_NAME_LENGTH);
  132. }
  133. return THERMAL_SUCCESS;
  134. }
  135. static int handle_netlink(struct nl_cache_ops *unused,
  136. struct genl_cmd *cmd,
  137. struct genl_info *info, void *arg)
  138. {
  139. int ret;
  140. switch (cmd->c_id) {
  141. case THERMAL_GENL_CMD_TZ_GET_ID:
  142. ret = parse_tz_get(info, arg);
  143. break;
  144. case THERMAL_GENL_CMD_CDEV_GET:
  145. ret = parse_cdev_get(info, arg);
  146. break;
  147. case THERMAL_GENL_CMD_TZ_GET_TEMP:
  148. ret = parse_tz_get_temp(info, arg);
  149. break;
  150. case THERMAL_GENL_CMD_TZ_GET_TRIP:
  151. ret = parse_tz_get_trip(info, arg);
  152. break;
  153. case THERMAL_GENL_CMD_TZ_GET_GOV:
  154. ret = parse_tz_get_gov(info, arg);
  155. break;
  156. default:
  157. return THERMAL_ERROR;
  158. }
  159. return ret;
  160. }
  161. static struct genl_cmd thermal_cmds[] = {
  162. {
  163. .c_id = THERMAL_GENL_CMD_TZ_GET_ID,
  164. .c_name = (char *)"List thermal zones",
  165. .c_msg_parser = handle_netlink,
  166. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  167. .c_attr_policy = thermal_genl_policy,
  168. },
  169. {
  170. .c_id = THERMAL_GENL_CMD_TZ_GET_GOV,
  171. .c_name = (char *)"Get governor",
  172. .c_msg_parser = handle_netlink,
  173. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  174. .c_attr_policy = thermal_genl_policy,
  175. },
  176. {
  177. .c_id = THERMAL_GENL_CMD_TZ_GET_TEMP,
  178. .c_name = (char *)"Get thermal zone temperature",
  179. .c_msg_parser = handle_netlink,
  180. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  181. .c_attr_policy = thermal_genl_policy,
  182. },
  183. {
  184. .c_id = THERMAL_GENL_CMD_TZ_GET_TRIP,
  185. .c_name = (char *)"Get thermal zone trip points",
  186. .c_msg_parser = handle_netlink,
  187. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  188. .c_attr_policy = thermal_genl_policy,
  189. },
  190. {
  191. .c_id = THERMAL_GENL_CMD_CDEV_GET,
  192. .c_name = (char *)"Get cooling devices",
  193. .c_msg_parser = handle_netlink,
  194. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  195. .c_attr_policy = thermal_genl_policy,
  196. },
  197. };
  198. static struct genl_ops thermal_cmd_ops = {
  199. .o_name = (char *)"thermal",
  200. .o_cmds = thermal_cmds,
  201. .o_ncmds = ARRAY_SIZE(thermal_cmds),
  202. };
  203. static thermal_error_t thermal_genl_auto(struct thermal_handler *th, int id, int cmd,
  204. int flags, void *arg)
  205. {
  206. struct nl_msg *msg;
  207. void *hdr;
  208. msg = nlmsg_alloc();
  209. if (!msg)
  210. return THERMAL_ERROR;
  211. hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, thermal_cmd_ops.o_id,
  212. 0, flags, cmd, THERMAL_GENL_VERSION);
  213. if (!hdr)
  214. return THERMAL_ERROR;
  215. if (id >= 0 && nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id))
  216. return THERMAL_ERROR;
  217. if (nl_send_msg(th->sk_cmd, th->cb_cmd, msg, genl_handle_msg, arg))
  218. return THERMAL_ERROR;
  219. nlmsg_free(msg);
  220. return THERMAL_SUCCESS;
  221. }
  222. thermal_error_t thermal_cmd_get_tz(struct thermal_handler *th, struct thermal_zone **tz)
  223. {
  224. return thermal_genl_auto(th, -1, THERMAL_GENL_CMD_TZ_GET_ID,
  225. NLM_F_DUMP | NLM_F_ACK, tz);
  226. }
  227. thermal_error_t thermal_cmd_get_cdev(struct thermal_handler *th, struct thermal_cdev **tc)
  228. {
  229. return thermal_genl_auto(th, -1, THERMAL_GENL_CMD_CDEV_GET,
  230. NLM_F_DUMP | NLM_F_ACK, tc);
  231. }
  232. thermal_error_t thermal_cmd_get_trip(struct thermal_handler *th, struct thermal_zone *tz)
  233. {
  234. return thermal_genl_auto(th, tz->id, THERMAL_GENL_CMD_TZ_GET_TRIP,
  235. 0, tz);
  236. }
  237. thermal_error_t thermal_cmd_get_governor(struct thermal_handler *th, struct thermal_zone *tz)
  238. {
  239. return thermal_genl_auto(th, tz->id, THERMAL_GENL_CMD_TZ_GET_GOV, 0, tz);
  240. }
  241. thermal_error_t thermal_cmd_get_temp(struct thermal_handler *th, struct thermal_zone *tz)
  242. {
  243. return thermal_genl_auto(th, tz->id, THERMAL_GENL_CMD_TZ_GET_TEMP, 0, tz);
  244. }
  245. thermal_error_t thermal_cmd_exit(struct thermal_handler *th)
  246. {
  247. if (genl_unregister_family(&thermal_cmd_ops))
  248. return THERMAL_ERROR;
  249. nl_thermal_disconnect(th->sk_cmd, th->cb_cmd);
  250. return THERMAL_SUCCESS;
  251. }
  252. thermal_error_t thermal_cmd_init(struct thermal_handler *th)
  253. {
  254. int ret;
  255. int family;
  256. if (nl_thermal_connect(&th->sk_cmd, &th->cb_cmd))
  257. return THERMAL_ERROR;
  258. ret = genl_register_family(&thermal_cmd_ops);
  259. if (ret)
  260. return THERMAL_ERROR;
  261. ret = genl_ops_resolve(th->sk_cmd, &thermal_cmd_ops);
  262. if (ret)
  263. return THERMAL_ERROR;
  264. family = genl_ctrl_resolve(th->sk_cmd, "nlctrl");
  265. if (family != GENL_ID_CTRL)
  266. return THERMAL_ERROR;
  267. return THERMAL_SUCCESS;
  268. }