dbus_new_helpers.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "utils/includes.h"
  16. #include "utils/common.h"
  17. #include "utils/eloop.h"
  18. #include "dbus_common.h"
  19. #include "dbus_common_i.h"
  20. #include "dbus_new.h"
  21. #include "dbus_new_helpers.h"
  22. /**
  23. * recursive_iter_copy - Reads arguments from one iterator and
  24. * writes to another recursively
  25. * @from: iterator to read from
  26. * @to: iterator to write to
  27. *
  28. * Copies one iterator's elements to another. If any element in
  29. * iterator is of container type, its content is copied recursively
  30. */
  31. static void recursive_iter_copy(DBusMessageIter *from, DBusMessageIter *to)
  32. {
  33. char *subtype = NULL;
  34. int type;
  35. /* iterate over iterator to copy */
  36. while ((type = dbus_message_iter_get_arg_type(from)) !=
  37. DBUS_TYPE_INVALID) {
  38. /* simply copy basic type entries */
  39. if (dbus_type_is_basic(type)) {
  40. if (dbus_type_is_fixed(type)) {
  41. /*
  42. * According to DBus documentation all
  43. * fixed-length types are guaranteed to fit
  44. * 8 bytes
  45. */
  46. dbus_uint64_t v;
  47. dbus_message_iter_get_basic(from, &v);
  48. dbus_message_iter_append_basic(to, type, &v);
  49. } else {
  50. char *v;
  51. dbus_message_iter_get_basic(from, &v);
  52. dbus_message_iter_append_basic(to, type, &v);
  53. }
  54. } else {
  55. /* recursively copy container type entries */
  56. DBusMessageIter write_subiter, read_subiter;
  57. dbus_message_iter_recurse(from, &read_subiter);
  58. if (type == DBUS_TYPE_VARIANT ||
  59. type == DBUS_TYPE_ARRAY) {
  60. subtype = dbus_message_iter_get_signature(
  61. &read_subiter);
  62. }
  63. dbus_message_iter_open_container(to, type, subtype,
  64. &write_subiter);
  65. recursive_iter_copy(&read_subiter, &write_subiter);
  66. dbus_message_iter_close_container(to, &write_subiter);
  67. if (subtype)
  68. dbus_free(subtype);
  69. }
  70. dbus_message_iter_next(from);
  71. }
  72. }
  73. static unsigned int fill_dict_with_properties(
  74. DBusMessageIter *dict_iter, const struct wpa_dbus_property_desc *props,
  75. const char *interface, const void *user_data)
  76. {
  77. DBusMessage *reply;
  78. DBusMessageIter entry_iter, ret_iter;
  79. unsigned int counter = 0;
  80. const struct wpa_dbus_property_desc *dsc;
  81. for (dsc = props; dsc && dsc->dbus_property; dsc++) {
  82. if (!os_strncmp(dsc->dbus_interface, interface,
  83. WPAS_DBUS_INTERFACE_MAX) &&
  84. dsc->access != W && dsc->getter) {
  85. reply = dsc->getter(NULL, user_data);
  86. if (!reply)
  87. continue;
  88. if (dbus_message_get_type(reply) ==
  89. DBUS_MESSAGE_TYPE_ERROR) {
  90. dbus_message_unref(reply);
  91. continue;
  92. }
  93. dbus_message_iter_init(reply, &ret_iter);
  94. dbus_message_iter_open_container(dict_iter,
  95. DBUS_TYPE_DICT_ENTRY,
  96. NULL, &entry_iter);
  97. dbus_message_iter_append_basic(
  98. &entry_iter, DBUS_TYPE_STRING,
  99. &dsc->dbus_property);
  100. recursive_iter_copy(&ret_iter, &entry_iter);
  101. dbus_message_iter_close_container(dict_iter,
  102. &entry_iter);
  103. dbus_message_unref(reply);
  104. counter++;
  105. }
  106. }
  107. return counter;
  108. }
  109. /**
  110. * get_all_properties - Responds for GetAll properties calls on object
  111. * @message: Message with GetAll call
  112. * @interface: interface name which properties will be returned
  113. * @property_dsc: list of object's properties
  114. * Returns: Message with dict of variants as argument with properties values
  115. *
  116. * Iterates over all properties registered with object and execute getters
  117. * of those, which are readable and which interface matches interface
  118. * specified as argument. Returned message contains one dict argument
  119. * with properties names as keys and theirs values as values.
  120. */
  121. static DBusMessage * get_all_properties(
  122. DBusMessage *message, char *interface,
  123. struct wpa_dbus_object_desc *obj_dsc)
  124. {
  125. /* Create and initialize the return message */
  126. DBusMessage *reply = dbus_message_new_method_return(message);
  127. DBusMessageIter iter, dict_iter;
  128. int props_num;
  129. dbus_message_iter_init_append(reply, &iter);
  130. dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  131. DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
  132. DBUS_TYPE_STRING_AS_STRING
  133. DBUS_TYPE_VARIANT_AS_STRING
  134. DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
  135. &dict_iter);
  136. props_num = fill_dict_with_properties(&dict_iter, obj_dsc->properties,
  137. interface, obj_dsc->user_data);
  138. dbus_message_iter_close_container(&iter, &dict_iter);
  139. if (props_num == 0) {
  140. dbus_message_unref(reply);
  141. reply = dbus_message_new_error(message,
  142. DBUS_ERROR_INVALID_ARGS,
  143. "No readable properties in "
  144. "this interface");
  145. }
  146. return reply;
  147. }
  148. static int is_signature_correct(DBusMessage *message,
  149. const struct wpa_dbus_method_desc *method_dsc)
  150. {
  151. /* According to DBus documentation max length of signature is 255 */
  152. #define MAX_SIG_LEN 256
  153. char registered_sig[MAX_SIG_LEN], *pos;
  154. const char *sig = dbus_message_get_signature(message);
  155. int ret;
  156. const struct wpa_dbus_argument *arg;
  157. pos = registered_sig;
  158. *pos = '\0';
  159. for (arg = method_dsc->args; arg && arg->name; arg++) {
  160. if (arg->dir == ARG_IN) {
  161. size_t blen = registered_sig + MAX_SIG_LEN - pos;
  162. ret = os_snprintf(pos, blen, "%s", arg->type);
  163. if (ret < 0 || (size_t) ret >= blen)
  164. return 0;
  165. pos += ret;
  166. }
  167. }
  168. return !os_strncmp(registered_sig, sig, MAX_SIG_LEN);
  169. }
  170. static DBusMessage * properties_get_all(DBusMessage *message, char *interface,
  171. struct wpa_dbus_object_desc *obj_dsc)
  172. {
  173. if (os_strcmp(dbus_message_get_signature(message), "s") != 0)
  174. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  175. NULL);
  176. return get_all_properties(message, interface, obj_dsc);
  177. }
  178. static DBusMessage * properties_get(DBusMessage *message,
  179. const struct wpa_dbus_property_desc *dsc,
  180. void *user_data)
  181. {
  182. if (os_strcmp(dbus_message_get_signature(message), "ss"))
  183. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  184. NULL);
  185. if (dsc->access != W && dsc->getter)
  186. return dsc->getter(message, user_data);
  187. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  188. "Property is write-only");
  189. }
  190. static DBusMessage * properties_set(DBusMessage *message,
  191. const struct wpa_dbus_property_desc *dsc,
  192. void *user_data)
  193. {
  194. if (os_strcmp(dbus_message_get_signature(message), "ssv"))
  195. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  196. NULL);
  197. if (dsc->access != R && dsc->setter)
  198. return dsc->setter(message, user_data);
  199. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  200. "Property is read-only");
  201. }
  202. static DBusMessage *
  203. properties_get_or_set(DBusMessage *message, DBusMessageIter *iter,
  204. char *interface,
  205. struct wpa_dbus_object_desc *obj_dsc)
  206. {
  207. const struct wpa_dbus_property_desc *property_dsc;
  208. char *property;
  209. const char *method;
  210. method = dbus_message_get_member(message);
  211. property_dsc = obj_dsc->properties;
  212. /* Second argument: property name (DBUS_TYPE_STRING) */
  213. if (!dbus_message_iter_next(iter) ||
  214. dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING) {
  215. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  216. NULL);
  217. }
  218. dbus_message_iter_get_basic(iter, &property);
  219. while (property_dsc && property_dsc->dbus_property) {
  220. /* compare property names and
  221. * interfaces */
  222. if (!os_strncmp(property_dsc->dbus_property, property,
  223. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  224. !os_strncmp(property_dsc->dbus_interface, interface,
  225. WPAS_DBUS_INTERFACE_MAX))
  226. break;
  227. property_dsc++;
  228. }
  229. if (property_dsc == NULL || property_dsc->dbus_property == NULL) {
  230. wpa_printf(MSG_DEBUG, "no property handler for %s.%s on %s",
  231. interface, property,
  232. dbus_message_get_path(message));
  233. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  234. "No such property");
  235. }
  236. if (os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  237. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) == 0)
  238. return properties_get(message, property_dsc,
  239. obj_dsc->user_data);
  240. return properties_set(message, property_dsc, obj_dsc->user_data);
  241. }
  242. static DBusMessage * properties_handler(DBusMessage *message,
  243. struct wpa_dbus_object_desc *obj_dsc)
  244. {
  245. DBusMessageIter iter;
  246. char *interface;
  247. const char *method;
  248. method = dbus_message_get_member(message);
  249. dbus_message_iter_init(message, &iter);
  250. if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
  251. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  252. !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
  253. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
  254. !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  255. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  256. /* First argument: interface name (DBUS_TYPE_STRING) */
  257. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
  258. {
  259. return dbus_message_new_error(message,
  260. DBUS_ERROR_INVALID_ARGS,
  261. NULL);
  262. }
  263. dbus_message_iter_get_basic(&iter, &interface);
  264. if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
  265. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
  266. /* GetAll */
  267. return properties_get_all(message, interface, obj_dsc);
  268. }
  269. /* Get or Set */
  270. return properties_get_or_set(message, &iter, interface,
  271. obj_dsc);
  272. }
  273. return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
  274. NULL);
  275. }
  276. static DBusMessage * msg_method_handler(DBusMessage *message,
  277. struct wpa_dbus_object_desc *obj_dsc)
  278. {
  279. const struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
  280. const char *method;
  281. const char *msg_interface;
  282. method = dbus_message_get_member(message);
  283. msg_interface = dbus_message_get_interface(message);
  284. /* try match call to any registered method */
  285. while (method_dsc && method_dsc->dbus_method) {
  286. /* compare method names and interfaces */
  287. if (!os_strncmp(method_dsc->dbus_method, method,
  288. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  289. !os_strncmp(method_dsc->dbus_interface, msg_interface,
  290. WPAS_DBUS_INTERFACE_MAX))
  291. break;
  292. method_dsc++;
  293. }
  294. if (method_dsc == NULL || method_dsc->dbus_method == NULL) {
  295. wpa_printf(MSG_DEBUG, "no method handler for %s.%s on %s",
  296. msg_interface, method,
  297. dbus_message_get_path(message));
  298. return dbus_message_new_error(message,
  299. DBUS_ERROR_UNKNOWN_METHOD, NULL);
  300. }
  301. if (!is_signature_correct(message, method_dsc)) {
  302. return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
  303. NULL);
  304. }
  305. return method_dsc->method_handler(message,
  306. obj_dsc->user_data);
  307. }
  308. /**
  309. * message_handler - Handles incoming DBus messages
  310. * @connection: DBus connection on which message was received
  311. * @message: Received message
  312. * @user_data: pointer to description of object to which message was sent
  313. * Returns: Returns information whether message was handled or not
  314. *
  315. * Reads message interface and method name, then checks if they matches one
  316. * of the special cases i.e. introspection call or properties get/getall/set
  317. * methods and handles it. Else it iterates over registered methods list
  318. * and tries to match method's name and interface to those read from message
  319. * If appropriate method was found its handler function is called and
  320. * response is sent. Otherwise, the DBUS_ERROR_UNKNOWN_METHOD error message
  321. * will be sent.
  322. */
  323. static DBusHandlerResult message_handler(DBusConnection *connection,
  324. DBusMessage *message, void *user_data)
  325. {
  326. struct wpa_dbus_object_desc *obj_dsc = user_data;
  327. const char *method;
  328. const char *path;
  329. const char *msg_interface;
  330. DBusMessage *reply;
  331. /* get method, interface and path the message is addressed to */
  332. method = dbus_message_get_member(message);
  333. path = dbus_message_get_path(message);
  334. msg_interface = dbus_message_get_interface(message);
  335. if (!method || !path || !msg_interface)
  336. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  337. wpa_printf(MSG_MSGDUMP, "dbus: %s.%s (%s)",
  338. msg_interface, method, path);
  339. /* if message is introspection method call */
  340. if (!os_strncmp(WPA_DBUS_INTROSPECTION_METHOD, method,
  341. WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
  342. !os_strncmp(WPA_DBUS_INTROSPECTION_INTERFACE, msg_interface,
  343. WPAS_DBUS_INTERFACE_MAX)) {
  344. #ifdef CONFIG_CTRL_IFACE_DBUS_INTRO
  345. reply = wpa_dbus_introspect(message, obj_dsc);
  346. #else /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  347. reply = dbus_message_new_error(
  348. message, DBUS_ERROR_UNKNOWN_METHOD,
  349. "wpa_supplicant was compiled without "
  350. "introspection support.");
  351. #endif /* CONFIG_CTRL_IFACE_DBUS_INTRO */
  352. } else if (!os_strncmp(WPA_DBUS_PROPERTIES_INTERFACE, msg_interface,
  353. WPAS_DBUS_INTERFACE_MAX)) {
  354. /* if message is properties method call */
  355. reply = properties_handler(message, obj_dsc);
  356. } else {
  357. reply = msg_method_handler(message, obj_dsc);
  358. }
  359. /* If handler succeed returning NULL, reply empty message */
  360. if (!reply)
  361. reply = dbus_message_new_method_return(message);
  362. if (reply) {
  363. if (!dbus_message_get_no_reply(message))
  364. dbus_connection_send(connection, reply, NULL);
  365. dbus_message_unref(reply);
  366. }
  367. wpa_dbus_flush_all_changed_properties(connection);
  368. return DBUS_HANDLER_RESULT_HANDLED;
  369. }
  370. /**
  371. * free_dbus_object_desc - Frees object description data structure
  372. * @connection: DBus connection
  373. * @obj_dsc: Object description to free
  374. *
  375. * Frees each of properties, methods and signals description lists and
  376. * the object description structure itself.
  377. */
  378. void free_dbus_object_desc(struct wpa_dbus_object_desc *obj_dsc)
  379. {
  380. if (!obj_dsc)
  381. return;
  382. /* free handler's argument */
  383. if (obj_dsc->user_data_free_func)
  384. obj_dsc->user_data_free_func(obj_dsc->user_data);
  385. os_free(obj_dsc->path);
  386. os_free(obj_dsc->prop_changed_flags);
  387. os_free(obj_dsc);
  388. }
  389. static void free_dbus_object_desc_cb(DBusConnection *connection, void *obj_dsc)
  390. {
  391. free_dbus_object_desc(obj_dsc);
  392. }
  393. /**
  394. * wpa_dbus_ctrl_iface_init - Initialize dbus control interface
  395. * @application_data: Pointer to application specific data structure
  396. * @dbus_path: DBus path to interface object
  397. * @dbus_service: DBus service name to register with
  398. * @messageHandler: a pointer to function which will handle dbus messages
  399. * coming on interface
  400. * Returns: 0 on success, -1 on failure
  401. *
  402. * Initialize the dbus control interface and start receiving commands from
  403. * external programs over the bus.
  404. */
  405. int wpa_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface,
  406. char *dbus_path, char *dbus_service,
  407. struct wpa_dbus_object_desc *obj_desc)
  408. {
  409. DBusError error;
  410. int ret = -1;
  411. DBusObjectPathVTable wpa_vtable = {
  412. &free_dbus_object_desc_cb, &message_handler,
  413. NULL, NULL, NULL, NULL
  414. };
  415. obj_desc->connection = iface->con;
  416. obj_desc->path = os_strdup(dbus_path);
  417. /* Register the message handler for the global dbus interface */
  418. if (!dbus_connection_register_object_path(iface->con,
  419. dbus_path, &wpa_vtable,
  420. obj_desc)) {
  421. wpa_printf(MSG_ERROR, "dbus: Could not set up message "
  422. "handler");
  423. return -1;
  424. }
  425. /* Register our service with the message bus */
  426. dbus_error_init(&error);
  427. switch (dbus_bus_request_name(iface->con, dbus_service,
  428. 0, &error)) {
  429. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  430. ret = 0;
  431. break;
  432. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  433. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  434. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  435. wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
  436. "already registered");
  437. break;
  438. default:
  439. wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
  440. "%s %s", error.name, error.message);
  441. break;
  442. }
  443. dbus_error_free(&error);
  444. if (ret != 0)
  445. return -1;
  446. wpa_printf(MSG_DEBUG, "Providing DBus service '%s'.", dbus_service);
  447. return 0;
  448. }
  449. /**
  450. * wpa_dbus_register_object_per_iface - Register a new object with dbus
  451. * @ctrl_iface: pointer to dbus private data
  452. * @path: DBus path to object
  453. * @ifname: interface name
  454. * @obj_desc: description of object's methods, signals and properties
  455. * Returns: 0 on success, -1 on error
  456. *
  457. * Registers a new interface with dbus and assigns it a dbus object path.
  458. */
  459. int wpa_dbus_register_object_per_iface(
  460. struct wpas_dbus_priv *ctrl_iface,
  461. const char *path, const char *ifname,
  462. struct wpa_dbus_object_desc *obj_desc)
  463. {
  464. DBusConnection *con;
  465. DBusObjectPathVTable vtable = {
  466. &free_dbus_object_desc_cb, &message_handler,
  467. NULL, NULL, NULL, NULL
  468. };
  469. /* Do nothing if the control interface is not turned on */
  470. if (ctrl_iface == NULL)
  471. return 0;
  472. con = ctrl_iface->con;
  473. obj_desc->connection = con;
  474. obj_desc->path = os_strdup(path);
  475. /* Register the message handler for the interface functions */
  476. if (!dbus_connection_register_object_path(con, path, &vtable,
  477. obj_desc)) {
  478. wpa_printf(MSG_ERROR, "dbus: Could not set up message "
  479. "handler for interface %s object %s", ifname, path);
  480. return -1;
  481. }
  482. return 0;
  483. }
  484. static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx);
  485. /**
  486. * wpa_dbus_unregister_object_per_iface - Unregisters DBus object
  487. * @ctrl_iface: Pointer to dbus private data
  488. * @path: DBus path to object which will be unregistered
  489. * Returns: Zero on success and -1 on failure
  490. *
  491. * Unregisters DBus object given by its path
  492. */
  493. int wpa_dbus_unregister_object_per_iface(
  494. struct wpas_dbus_priv *ctrl_iface, const char *path)
  495. {
  496. DBusConnection *con = ctrl_iface->con;
  497. eloop_cancel_timeout(flush_object_timeout_handler, con, (void *) path);
  498. if (!dbus_connection_unregister_object_path(con, path))
  499. return -1;
  500. return 0;
  501. }
  502. static void put_changed_properties(const struct wpa_dbus_object_desc *obj_dsc,
  503. const char *interface,
  504. DBusMessageIter *dict_iter)
  505. {
  506. DBusMessage *getter_reply;
  507. DBusMessageIter prop_iter, entry_iter;
  508. const struct wpa_dbus_property_desc *dsc;
  509. int i;
  510. for (dsc = obj_dsc->properties, i = 0; dsc && dsc->dbus_property;
  511. dsc++, i++) {
  512. if (obj_dsc->prop_changed_flags == NULL ||
  513. !obj_dsc->prop_changed_flags[i])
  514. continue;
  515. if (os_strcmp(dsc->dbus_interface, interface) != 0)
  516. continue;
  517. obj_dsc->prop_changed_flags[i] = 0;
  518. getter_reply = dsc->getter(NULL, obj_dsc->user_data);
  519. if (!getter_reply ||
  520. dbus_message_get_type(getter_reply) ==
  521. DBUS_MESSAGE_TYPE_ERROR) {
  522. wpa_printf(MSG_ERROR, "dbus: %s: Cannot get new value "
  523. "of property %s", __func__,
  524. dsc->dbus_property);
  525. continue;
  526. }
  527. if (!dbus_message_iter_init(getter_reply, &prop_iter) ||
  528. !dbus_message_iter_open_container(dict_iter,
  529. DBUS_TYPE_DICT_ENTRY,
  530. NULL, &entry_iter) ||
  531. !dbus_message_iter_append_basic(&entry_iter,
  532. DBUS_TYPE_STRING,
  533. &dsc->dbus_property))
  534. goto err;
  535. recursive_iter_copy(&prop_iter, &entry_iter);
  536. if (!dbus_message_iter_close_container(dict_iter, &entry_iter))
  537. goto err;
  538. dbus_message_unref(getter_reply);
  539. }
  540. return;
  541. err:
  542. wpa_printf(MSG_ERROR, "dbus: %s: Cannot construct signal", __func__);
  543. }
  544. static void send_prop_changed_signal(
  545. DBusConnection *con, const char *path, const char *interface,
  546. const struct wpa_dbus_object_desc *obj_dsc)
  547. {
  548. DBusMessage *msg;
  549. DBusMessageIter signal_iter, dict_iter;
  550. msg = dbus_message_new_signal(path, interface, "PropertiesChanged");
  551. if (msg == NULL)
  552. return;
  553. dbus_message_iter_init_append(msg, &signal_iter);
  554. if (!dbus_message_iter_open_container(&signal_iter, DBUS_TYPE_ARRAY,
  555. "{sv}", &dict_iter))
  556. goto err;
  557. put_changed_properties(obj_dsc, interface, &dict_iter);
  558. if (!dbus_message_iter_close_container(&signal_iter, &dict_iter))
  559. goto err;
  560. dbus_connection_send(con, msg, NULL);
  561. out:
  562. dbus_message_unref(msg);
  563. return;
  564. err:
  565. wpa_printf(MSG_DEBUG, "dbus: %s: Failed to construct signal",
  566. __func__);
  567. goto out;
  568. }
  569. static void flush_object_timeout_handler(void *eloop_ctx, void *timeout_ctx)
  570. {
  571. DBusConnection *con = eloop_ctx;
  572. const char *path = timeout_ctx;
  573. wpa_printf(MSG_DEBUG, "dbus: %s: Timeout - sending changed properties "
  574. "of object %s", __func__, path);
  575. wpa_dbus_flush_object_changed_properties(con, path);
  576. }
  577. static void recursive_flush_changed_properties(DBusConnection *con,
  578. const char *path)
  579. {
  580. char **objects = NULL;
  581. char subobj_path[WPAS_DBUS_OBJECT_PATH_MAX];
  582. int i;
  583. wpa_dbus_flush_object_changed_properties(con, path);
  584. if (!dbus_connection_list_registered(con, path, &objects))
  585. goto out;
  586. for (i = 0; objects[i]; i++) {
  587. os_snprintf(subobj_path, WPAS_DBUS_OBJECT_PATH_MAX,
  588. "%s/%s", path, objects[i]);
  589. recursive_flush_changed_properties(con, subobj_path);
  590. }
  591. out:
  592. dbus_free_string_array(objects);
  593. }
  594. /**
  595. * wpa_dbus_flush_all_changed_properties - Send all PropertiesChanged signals
  596. * @con: DBus connection
  597. *
  598. * Traverses through all registered objects and sends PropertiesChanged for
  599. * each properties.
  600. */
  601. void wpa_dbus_flush_all_changed_properties(DBusConnection *con)
  602. {
  603. recursive_flush_changed_properties(con, WPAS_DBUS_NEW_PATH);
  604. }
  605. /**
  606. * wpa_dbus_flush_object_changed_properties - Send PropertiesChanged for object
  607. * @con: DBus connection
  608. * @path: path to a DBus object for which PropertiesChanged will be sent.
  609. *
  610. * Iterates over all properties registered with object and for each interface
  611. * containing properties marked as changed, sends a PropertiesChanged signal
  612. * containing names and new values of properties that have changed.
  613. *
  614. * You need to call this function after wpa_dbus_mark_property_changed()
  615. * if you want to send PropertiesChanged signal immediately (i.e., without
  616. * waiting timeout to expire). PropertiesChanged signal for an object is sent
  617. * automatically short time after first marking property as changed. All
  618. * PropertiesChanged signals are sent automatically after responding on DBus
  619. * message, so if you marked a property changed as a result of DBus call
  620. * (e.g., param setter), you usually do not need to call this function.
  621. */
  622. void wpa_dbus_flush_object_changed_properties(DBusConnection *con,
  623. const char *path)
  624. {
  625. struct wpa_dbus_object_desc *obj_desc = NULL;
  626. const struct wpa_dbus_property_desc *dsc;
  627. int i;
  628. eloop_cancel_timeout(flush_object_timeout_handler, con, (void *) path);
  629. dbus_connection_get_object_path_data(con, path, (void **) &obj_desc);
  630. if (!obj_desc)
  631. return;
  632. dsc = obj_desc->properties;
  633. for (dsc = obj_desc->properties, i = 0; dsc && dsc->dbus_property;
  634. dsc++, i++) {
  635. if (obj_desc->prop_changed_flags == NULL ||
  636. !obj_desc->prop_changed_flags[i])
  637. continue;
  638. send_prop_changed_signal(con, path, dsc->dbus_interface,
  639. obj_desc);
  640. }
  641. }
  642. #define WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT 5000
  643. /**
  644. * wpa_dbus_mark_property_changed - Mark a property as changed and
  645. * @iface: dbus priv struct
  646. * @path: path to DBus object which property has changed
  647. * @interface: interface containing changed property
  648. * @property: property name which has changed
  649. *
  650. * Iterates over all properties registered with an object and marks the one
  651. * given in parameters as changed. All parameters registered for an object
  652. * within a single interface will be aggregated together and sent in one
  653. * PropertiesChanged signal when function
  654. * wpa_dbus_flush_object_changed_properties() is called.
  655. */
  656. void wpa_dbus_mark_property_changed(struct wpas_dbus_priv *iface,
  657. const char *path, const char *interface,
  658. const char *property)
  659. {
  660. struct wpa_dbus_object_desc *obj_desc = NULL;
  661. const struct wpa_dbus_property_desc *dsc;
  662. int i = 0;
  663. dbus_connection_get_object_path_data(iface->con, path,
  664. (void **) &obj_desc);
  665. if (!obj_desc) {
  666. wpa_printf(MSG_ERROR, "dbus: wpa_dbus_property_changed: "
  667. "could not obtain object's private data: %s", path);
  668. return;
  669. }
  670. for (dsc = obj_desc->properties; dsc && dsc->dbus_property; dsc++, i++)
  671. if (os_strcmp(property, dsc->dbus_property) == 0 &&
  672. os_strcmp(interface, dsc->dbus_interface) == 0) {
  673. if (obj_desc->prop_changed_flags)
  674. obj_desc->prop_changed_flags[i] = 1;
  675. break;
  676. }
  677. if (!dsc || !dsc->dbus_property) {
  678. wpa_printf(MSG_ERROR, "dbus: wpa_dbus_property_changed: "
  679. "no property %s in object %s", property, path);
  680. return;
  681. }
  682. if (!eloop_is_timeout_registered(flush_object_timeout_handler,
  683. iface->con, obj_desc->path)) {
  684. eloop_register_timeout(0, WPA_DBUS_SEND_PROP_CHANGED_TIMEOUT,
  685. flush_object_timeout_handler,
  686. iface->con, obj_desc->path);
  687. }
  688. }
  689. /**
  690. * wpa_dbus_get_object_properties - Put object's properties into dictionary
  691. * @iface: dbus priv struct
  692. * @path: path to DBus object which properties will be obtained
  693. * @interface: interface name which properties will be obtained
  694. * @dict_iter: correct, open DBus dictionary iterator.
  695. *
  696. * Iterates over all properties registered with object and execute getters
  697. * of those, which are readable and which interface matches interface
  698. * specified as argument. Obtained properties values are stored in
  699. * dict_iter dictionary.
  700. */
  701. void wpa_dbus_get_object_properties(struct wpas_dbus_priv *iface,
  702. const char *path, const char *interface,
  703. DBusMessageIter *dict_iter)
  704. {
  705. struct wpa_dbus_object_desc *obj_desc = NULL;
  706. dbus_connection_get_object_path_data(iface->con, path,
  707. (void **) &obj_desc);
  708. if (!obj_desc) {
  709. wpa_printf(MSG_ERROR, "dbus: wpa_dbus_get_object_properties: "
  710. "could not obtain object's private data: %s", path);
  711. return;
  712. }
  713. fill_dict_with_properties(dict_iter, obj_desc->properties,
  714. interface, obj_desc->user_data);
  715. }