fota-introspect.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (C) 2018-2021 Alibaba Group Holding Limited
  3. */
  4. /*
  5. * fota_supplicant - D-Bus introspection
  6. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  7. * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
  8. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  9. *
  10. * This software may be distributed under the terms of the BSD license.
  11. * See README for more details.
  12. */
  13. #include "utils/includes.h"
  14. #include "utils/common.h"
  15. #include "utils/list.h"
  16. #include "utils/fota_buf.h"
  17. #include "dbus_new_helpers.h"
  18. #include <fota.h>
  19. #define fota_printf(...)
  20. struct interfaces {
  21. struct dl_list list;
  22. char *dbus_interface;
  23. struct fotabuf *xml;
  24. };
  25. static void *zalloc(size_t size)
  26. {
  27. void *n = malloc(size);
  28. if (n) {
  29. memset(n, 0, size);
  30. }
  31. return n;
  32. }
  33. static struct interfaces *add_interface(struct dl_list *list,
  34. const char *dbus_interface)
  35. {
  36. struct interfaces *iface;
  37. dl_list_for_each(iface, list, struct interfaces, list) {
  38. if (os_strcmp(iface->dbus_interface, dbus_interface) == 0) {
  39. return iface; /* already in the list */
  40. }
  41. }
  42. iface = zalloc(sizeof(struct interfaces));
  43. if (!iface) {
  44. return NULL;
  45. }
  46. iface->dbus_interface = os_strdup(dbus_interface);
  47. iface->xml = fotabuf_alloc(15000);
  48. if (iface->dbus_interface == NULL || iface->xml == NULL) {
  49. os_free(iface->dbus_interface);
  50. fotabuf_free(iface->xml);
  51. os_free(iface);
  52. return NULL;
  53. }
  54. fotabuf_printf(iface->xml, "<interface name=\"%s\">", dbus_interface);
  55. dl_list_add_tail(list, &iface->list);
  56. return iface;
  57. }
  58. static void add_arg(struct fotabuf *xml, const char *name, const char *type,
  59. const char *direction)
  60. {
  61. fotabuf_printf(xml, "<arg name=\"%s\"", name);
  62. if (type) {
  63. fotabuf_printf(xml, " type=\"%s\"", type);
  64. }
  65. if (direction) {
  66. fotabuf_printf(xml, " direction=\"%s\"", direction);
  67. }
  68. fotabuf_put_str(xml, "/>");
  69. }
  70. static void add_entry(struct fotabuf *xml, const char *type, const char *name,
  71. const struct fota_dbus_argument *args, int include_dir)
  72. {
  73. const struct fota_dbus_argument *arg;
  74. if (args == NULL || args->name == NULL) {
  75. fotabuf_printf(xml, "<%s name=\"%s\"/>", type, name);
  76. return;
  77. }
  78. fotabuf_printf(xml, "<%s name=\"%s\">", type, name);
  79. for (arg = args; arg && arg->name; arg++) {
  80. add_arg(xml, arg->name, arg->type,
  81. include_dir ? (arg->dir == ARG_IN ? "in" : "out") :
  82. NULL);
  83. }
  84. fotabuf_printf(xml, "</%s>", type);
  85. }
  86. static void add_property(struct fotabuf *xml,
  87. const struct fota_dbus_property_desc *dsc)
  88. {
  89. fotabuf_printf(xml, "<property name=\"%s\" type=\"%s\" "
  90. "access=\"%s%s\"/>",
  91. dsc->dbus_property, dsc->type,
  92. dsc->getter ? "read" : "",
  93. dsc->setter ? "write" : "");
  94. }
  95. static void extract_interfaces_methods(
  96. struct dl_list *list, const struct fota_dbus_method_desc *methods)
  97. {
  98. const struct fota_dbus_method_desc *dsc;
  99. struct interfaces *iface;
  100. for (dsc = methods; dsc && dsc->dbus_method; dsc++) {
  101. iface = add_interface(list, dsc->dbus_interface);
  102. if (iface) {
  103. add_entry(iface->xml, "method", dsc->dbus_method,
  104. dsc->args, 1);
  105. }
  106. }
  107. }
  108. static void extract_interfaces_signals(
  109. struct dl_list *list, const struct fota_dbus_signal_desc *signals)
  110. {
  111. const struct fota_dbus_signal_desc *dsc;
  112. struct interfaces *iface;
  113. for (dsc = signals; dsc && dsc->dbus_signal; dsc++) {
  114. iface = add_interface(list, dsc->dbus_interface);
  115. if (iface)
  116. add_entry(iface->xml, "signal", dsc->dbus_signal,
  117. dsc->args, 0);
  118. }
  119. }
  120. static void extract_interfaces_properties(
  121. struct dl_list *list, const struct fota_dbus_property_desc *properties)
  122. {
  123. const struct fota_dbus_property_desc *dsc;
  124. struct interfaces *iface;
  125. for (dsc = properties; dsc && dsc->dbus_property; dsc++) {
  126. iface = add_interface(list, dsc->dbus_interface);
  127. if (iface) {
  128. add_property(iface->xml, dsc);
  129. }
  130. }
  131. }
  132. /**
  133. * extract_interfaces - Extract interfaces from methods, signals and props
  134. * @list: Interface list to be filled
  135. * @obj_dsc: Description of object from which interfaces will be extracted
  136. *
  137. * Iterates over all methods, signals, and properties registered with an
  138. * object and collects all declared DBus interfaces and create interfaces'
  139. * node in XML root node for each. Returned list elements contain interface
  140. * name and XML node of corresponding interface.
  141. */
  142. static void extract_interfaces(struct dl_list *list,
  143. struct fota_dbus_object_desc *obj_dsc)
  144. {
  145. extract_interfaces_methods(list, obj_dsc->methods);
  146. extract_interfaces_signals(list, obj_dsc->signals);
  147. extract_interfaces_properties(list, obj_dsc->properties);
  148. }
  149. static void add_interfaces(struct dl_list *list, struct fotabuf *xml)
  150. {
  151. struct interfaces *iface, *n;
  152. dl_list_for_each_safe(iface, n, list, struct interfaces, list) {
  153. if (fotabuf_len(iface->xml) + 20 < fotabuf_tailroom(xml)) {
  154. fotabuf_put_buf(xml, iface->xml);
  155. fotabuf_put_str(xml, "</interface>");
  156. } else {
  157. fota_printf(MSG_DEBUG,
  158. "dbus: Not enough room for add_interfaces inspect data: tailroom %u, add %u",
  159. (unsigned int) fotabuf_tailroom(xml),
  160. (unsigned int) fotabuf_len(iface->xml));
  161. }
  162. dl_list_del(&iface->list);
  163. fotabuf_free(iface->xml);
  164. os_free(iface->dbus_interface);
  165. os_free(iface);
  166. }
  167. }
  168. static void add_child_nodes(struct fotabuf *xml, DBusConnection *con,
  169. const char *path)
  170. {
  171. char **children;
  172. int i;
  173. /* add child nodes to introspection tree */
  174. dbus_connection_list_registered(con, path, &children);
  175. for (i = 0; children[i]; i++) {
  176. fotabuf_printf(xml, "<node name=\"%s\"/>", children[i]);
  177. }
  178. dbus_free_string_array(children);
  179. }
  180. static void add_introspectable_interface(struct fotabuf *xml)
  181. {
  182. fotabuf_printf(xml, "<interface name=\"%s\">"
  183. "<method name=\"%s\">"
  184. "<arg name=\"data\" type=\"s\" direction=\"out\"/>"
  185. "</method>"
  186. "</interface>",
  187. FOTA_DBUS_INTROSPECTION_INTERFACE,
  188. FOTA_DBUS_INTROSPECTION_METHOD);
  189. }
  190. static void add_properties_interface(struct fotabuf *xml)
  191. {
  192. fotabuf_printf(xml, "<interface name=\"%s\">",
  193. FOTA_DBUS_PROPERTIES_INTERFACE);
  194. fotabuf_printf(xml, "<method name=\"%s\">", FOTA_DBUS_PROPERTIES_GET);
  195. add_arg(xml, "interface", "s", "in");
  196. add_arg(xml, "propname", "s", "in");
  197. add_arg(xml, "value", "v", "out");
  198. fotabuf_put_str(xml, "</method>");
  199. fotabuf_printf(xml, "<method name=\"%s\">", FOTA_DBUS_PROPERTIES_GETALL);
  200. add_arg(xml, "interface", "s", "in");
  201. add_arg(xml, "props", "a{sv}", "out");
  202. fotabuf_put_str(xml, "</method>");
  203. fotabuf_printf(xml, "<method name=\"%s\">", FOTA_DBUS_PROPERTIES_SET);
  204. add_arg(xml, "interface", "s", "in");
  205. add_arg(xml, "propname", "s", "in");
  206. add_arg(xml, "value", "v", "in");
  207. fotabuf_put_str(xml, "</method>");
  208. fotabuf_put_str(xml, "</interface>");
  209. }
  210. static void add_fotas_interfaces(struct fotabuf *xml,
  211. struct fota_dbus_object_desc *obj_dsc)
  212. {
  213. struct dl_list ifaces;
  214. dl_list_init(&ifaces);
  215. extract_interfaces(&ifaces, obj_dsc);
  216. add_interfaces(&ifaces, xml);
  217. }
  218. /**
  219. * fota_dbus_introspect - Responds for Introspect calls on object
  220. * @message: Message with Introspect call
  221. * @obj_dsc: Object description on which Introspect was called
  222. * Returns: Message with introspection result XML string as only argument
  223. *
  224. * Iterates over all methods, signals and properties registered with
  225. * object and generates introspection data for the object as XML string.
  226. */
  227. DBusMessage *fota_dbus_introspect(DBusMessage *msg,
  228. struct fota_dbus_object_desc *obj_dsc,
  229. fota_server_t *fota)
  230. {
  231. DBusMessage *reply;
  232. struct fotabuf *xml;
  233. DBusConnection *conn = fota->conn;
  234. xml = fotabuf_alloc(20000);
  235. if (xml == NULL) {
  236. return NULL;
  237. }
  238. fotabuf_put_str(xml, "<?xml version=\"1.0\"?>\n");
  239. fotabuf_put_str(xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
  240. fotabuf_put_str(xml, "<node>");
  241. add_introspectable_interface(xml);
  242. add_properties_interface(xml);
  243. add_fotas_interfaces(xml, obj_dsc);
  244. add_child_nodes(xml, obj_dsc->connection,
  245. dbus_message_get_path(msg));
  246. fotabuf_put_str(xml, "</node>\n");
  247. reply = dbus_message_new_method_return(msg);
  248. if (reply) {
  249. const char *intro_str = fotabuf_head(xml);
  250. dbus_message_append_args(reply, DBUS_TYPE_STRING, &intro_str,
  251. DBUS_TYPE_INVALID);
  252. if (!dbus_message_get_no_reply(msg)) {
  253. dbus_connection_send(conn, reply, NULL);
  254. }
  255. dbus_message_unref(reply);
  256. dbus_connection_flush(conn);
  257. }
  258. fotabuf_free(xml);
  259. return reply;
  260. }