wpa_supplicant: Report EAP connection progress to DBus
[mech_eap.git] / wpa_supplicant / dbus / dbus_new.c
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-2010, Witold Sowa <witold.sowa@gmail.com>
5  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * Alternatively, this software may be distributed under the terms of BSD
12  * license.
13  *
14  * See README and COPYING for more details.
15  */
16
17 #include "includes.h"
18
19 #include "common.h"
20 #include "common/ieee802_11_defs.h"
21 #include "wps/wps.h"
22 #include "../config.h"
23 #include "../wpa_supplicant_i.h"
24 #include "../bss.h"
25 #include "../wpas_glue.h"
26 #include "dbus_new_helpers.h"
27 #include "dbus_dict_helpers.h"
28 #include "dbus_new.h"
29 #include "dbus_new_handlers.h"
30 #include "dbus_common_i.h"
31 #include "dbus_new_handlers_p2p.h"
32 #include "p2p/p2p.h"
33
34 #ifdef CONFIG_AP /* until needed by something else */
35
36 /*
37  * NameOwnerChanged handling
38  *
39  * Some services we provide allow an application to register for
40  * a signal that it needs. While it can also unregister, we must
41  * be prepared for the case where the application simply crashes
42  * and thus doesn't clean up properly. The way to handle this in
43  * DBus is to register for the NameOwnerChanged signal which will
44  * signal an owner change to NULL if the peer closes the socket
45  * for whatever reason.
46  *
47  * Handle this signal via a filter function whenever necessary.
48  * The code below also handles refcounting in case in the future
49  * there will be multiple instances of this subscription scheme.
50  */
51 static const char wpas_dbus_noc_filter_str[] =
52         "interface=org.freedesktop.DBus,member=NameOwnerChanged";
53
54
55 static DBusHandlerResult noc_filter(DBusConnection *conn,
56                                     DBusMessage *message, void *data)
57 {
58         struct wpas_dbus_priv *priv = data;
59
60         if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL)
61                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
62
63         if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS,
64                                    "NameOwnerChanged")) {
65                 const char *name;
66                 const char *prev_owner;
67                 const char *new_owner;
68                 DBusError derr;
69                 struct wpa_supplicant *wpa_s;
70
71                 dbus_error_init(&derr);
72
73                 if (!dbus_message_get_args(message, &derr,
74                                            DBUS_TYPE_STRING, &name,
75                                            DBUS_TYPE_STRING, &prev_owner,
76                                            DBUS_TYPE_STRING, &new_owner,
77                                            DBUS_TYPE_INVALID)) {
78                         /* Ignore this error */
79                         dbus_error_free(&derr);
80                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
81                 }
82
83                 for (wpa_s = priv->global->ifaces; wpa_s; wpa_s = wpa_s->next)
84                 {
85                         if (wpa_s->preq_notify_peer != NULL &&
86                             os_strcmp(name, wpa_s->preq_notify_peer) == 0 &&
87                             (new_owner == NULL || os_strlen(new_owner) == 0)) {
88                                 /* probe request owner disconnected */
89                                 os_free(wpa_s->preq_notify_peer);
90                                 wpa_s->preq_notify_peer = NULL;
91                                 wpas_dbus_unsubscribe_noc(priv);
92                         }
93                 }
94         }
95
96         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
97 }
98
99
100 void wpas_dbus_subscribe_noc(struct wpas_dbus_priv *priv)
101 {
102         priv->dbus_noc_refcnt++;
103         if (priv->dbus_noc_refcnt > 1)
104                 return;
105
106         if (!dbus_connection_add_filter(priv->con, noc_filter, priv, NULL)) {
107                 wpa_printf(MSG_ERROR, "dbus: failed to add filter");
108                 return;
109         }
110
111         dbus_bus_add_match(priv->con, wpas_dbus_noc_filter_str, NULL);
112 }
113
114
115 void wpas_dbus_unsubscribe_noc(struct wpas_dbus_priv *priv)
116 {
117         priv->dbus_noc_refcnt--;
118         if (priv->dbus_noc_refcnt > 0)
119                 return;
120
121         dbus_bus_remove_match(priv->con, wpas_dbus_noc_filter_str, NULL);
122         dbus_connection_remove_filter(priv->con, noc_filter, priv);
123 }
124
125 #endif /* CONFIG_AP */
126
127
128 /**
129  * wpas_dbus_signal_interface - Send a interface related event signal
130  * @wpa_s: %wpa_supplicant network interface data
131  * @sig_name: signal name - InterfaceAdded or InterfaceRemoved
132  * @properties: Whether to add second argument with object properties
133  *
134  * Notify listeners about event related with interface
135  */
136 static void wpas_dbus_signal_interface(struct wpa_supplicant *wpa_s,
137                                        const char *sig_name, int properties)
138 {
139         struct wpas_dbus_priv *iface;
140         DBusMessage *msg;
141         DBusMessageIter iter;
142
143         iface = wpa_s->global->dbus;
144
145         /* Do nothing if the control interface is not turned on */
146         if (iface == NULL)
147                 return;
148
149         msg = dbus_message_new_signal(WPAS_DBUS_NEW_PATH,
150                                       WPAS_DBUS_NEW_INTERFACE, sig_name);
151         if (msg == NULL)
152                 return;
153
154         dbus_message_iter_init_append(msg, &iter);
155         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
156                                             &wpa_s->dbus_new_path))
157                 goto err;
158
159         if (properties) {
160                 if (!wpa_dbus_get_object_properties(
161                             iface, wpa_s->dbus_new_path,
162                             WPAS_DBUS_NEW_IFACE_INTERFACE, &iter))
163                         goto err;
164         }
165
166         dbus_connection_send(iface->con, msg, NULL);
167         dbus_message_unref(msg);
168         return;
169
170 err:
171         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
172         dbus_message_unref(msg);
173 }
174
175
176 /**
177  * wpas_dbus_signal_interface_added - Send a interface created signal
178  * @wpa_s: %wpa_supplicant network interface data
179  *
180  * Notify listeners about creating new interface
181  */
182 static void wpas_dbus_signal_interface_added(struct wpa_supplicant *wpa_s)
183 {
184         wpas_dbus_signal_interface(wpa_s, "InterfaceAdded", TRUE);
185 }
186
187
188 /**
189  * wpas_dbus_signal_interface_removed - Send a interface removed signal
190  * @wpa_s: %wpa_supplicant network interface data
191  *
192  * Notify listeners about removing interface
193  */
194 static void wpas_dbus_signal_interface_removed(struct wpa_supplicant *wpa_s)
195 {
196         wpas_dbus_signal_interface(wpa_s, "InterfaceRemoved", FALSE);
197
198 }
199
200
201 /**
202  * wpas_dbus_signal_scan_done - send scan done signal
203  * @wpa_s: %wpa_supplicant network interface data
204  * @success: indicates if scanning succeed or failed
205  *
206  * Notify listeners about finishing a scan
207  */
208 void wpas_dbus_signal_scan_done(struct wpa_supplicant *wpa_s, int success)
209 {
210         struct wpas_dbus_priv *iface;
211         DBusMessage *msg;
212         dbus_bool_t succ;
213
214         iface = wpa_s->global->dbus;
215
216         /* Do nothing if the control interface is not turned on */
217         if (iface == NULL)
218                 return;
219
220         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
221                                       WPAS_DBUS_NEW_IFACE_INTERFACE,
222                                       "ScanDone");
223         if (msg == NULL)
224                 return;
225
226         succ = success ? TRUE : FALSE;
227         if (dbus_message_append_args(msg, DBUS_TYPE_BOOLEAN, &succ,
228                                      DBUS_TYPE_INVALID))
229                 dbus_connection_send(iface->con, msg, NULL);
230         else
231                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
232         dbus_message_unref(msg);
233 }
234
235
236 /**
237  * wpas_dbus_signal_blob - Send a BSS related event signal
238  * @wpa_s: %wpa_supplicant network interface data
239  * @bss_obj_path: BSS object path
240  * @sig_name: signal name - BSSAdded or BSSRemoved
241  * @properties: Whether to add second argument with object properties
242  *
243  * Notify listeners about event related with BSS
244  */
245 static void wpas_dbus_signal_bss(struct wpa_supplicant *wpa_s,
246                                  const char *bss_obj_path,
247                                  const char *sig_name, int properties)
248 {
249         struct wpas_dbus_priv *iface;
250         DBusMessage *msg;
251         DBusMessageIter iter;
252
253         iface = wpa_s->global->dbus;
254
255         /* Do nothing if the control interface is not turned on */
256         if (iface == NULL)
257                 return;
258
259         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
260                                       WPAS_DBUS_NEW_IFACE_INTERFACE,
261                                       sig_name);
262         if (msg == NULL)
263                 return;
264
265         dbus_message_iter_init_append(msg, &iter);
266         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
267                                             &bss_obj_path))
268                 goto err;
269
270         if (properties) {
271                 if (!wpa_dbus_get_object_properties(iface, bss_obj_path,
272                                                     WPAS_DBUS_NEW_IFACE_BSS,
273                                                     &iter))
274                         goto err;
275         }
276
277         dbus_connection_send(iface->con, msg, NULL);
278         dbus_message_unref(msg);
279         return;
280
281 err:
282         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
283         dbus_message_unref(msg);
284 }
285
286
287 /**
288  * wpas_dbus_signal_bss_added - Send a BSS added signal
289  * @wpa_s: %wpa_supplicant network interface data
290  * @bss_obj_path: new BSS object path
291  *
292  * Notify listeners about adding new BSS
293  */
294 static void wpas_dbus_signal_bss_added(struct wpa_supplicant *wpa_s,
295                                        const char *bss_obj_path)
296 {
297         wpas_dbus_signal_bss(wpa_s, bss_obj_path, "BSSAdded", TRUE);
298 }
299
300
301 /**
302  * wpas_dbus_signal_bss_removed - Send a BSS removed signal
303  * @wpa_s: %wpa_supplicant network interface data
304  * @bss_obj_path: BSS object path
305  *
306  * Notify listeners about removing BSS
307  */
308 static void wpas_dbus_signal_bss_removed(struct wpa_supplicant *wpa_s,
309                                          const char *bss_obj_path)
310 {
311         wpas_dbus_signal_bss(wpa_s, bss_obj_path, "BSSRemoved", FALSE);
312 }
313
314
315 /**
316  * wpas_dbus_signal_blob - Send a blob related event signal
317  * @wpa_s: %wpa_supplicant network interface data
318  * @name: blob name
319  * @sig_name: signal name - BlobAdded or BlobRemoved
320  *
321  * Notify listeners about event related with blob
322  */
323 static void wpas_dbus_signal_blob(struct wpa_supplicant *wpa_s,
324                                   const char *name, const char *sig_name)
325 {
326         struct wpas_dbus_priv *iface;
327         DBusMessage *msg;
328
329         iface = wpa_s->global->dbus;
330
331         /* Do nothing if the control interface is not turned on */
332         if (iface == NULL)
333                 return;
334
335         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
336                                       WPAS_DBUS_NEW_IFACE_INTERFACE,
337                                       sig_name);
338         if (msg == NULL)
339                 return;
340
341         if (dbus_message_append_args(msg, DBUS_TYPE_STRING, &name,
342                                      DBUS_TYPE_INVALID))
343                 dbus_connection_send(iface->con, msg, NULL);
344         else
345                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
346         dbus_message_unref(msg);
347 }
348
349
350 /**
351  * wpas_dbus_signal_blob_added - Send a blob added signal
352  * @wpa_s: %wpa_supplicant network interface data
353  * @name: blob name
354  *
355  * Notify listeners about adding a new blob
356  */
357 void wpas_dbus_signal_blob_added(struct wpa_supplicant *wpa_s,
358                                  const char *name)
359 {
360         wpas_dbus_signal_blob(wpa_s, name, "BlobAdded");
361 }
362
363
364 /**
365  * wpas_dbus_signal_blob_removed - Send a blob removed signal
366  * @wpa_s: %wpa_supplicant network interface data
367  * @name: blob name
368  *
369  * Notify listeners about removing blob
370  */
371 void wpas_dbus_signal_blob_removed(struct wpa_supplicant *wpa_s,
372                                    const char *name)
373 {
374         wpas_dbus_signal_blob(wpa_s, name, "BlobRemoved");
375 }
376
377
378 /**
379  * wpas_dbus_signal_network - Send a network related event signal
380  * @wpa_s: %wpa_supplicant network interface data
381  * @id: new network id
382  * @sig_name: signal name - NetworkAdded, NetworkRemoved or NetworkSelected
383  * @properties: determines if add second argument with object properties
384  *
385  * Notify listeners about event related with configured network
386  */
387 static void wpas_dbus_signal_network(struct wpa_supplicant *wpa_s,
388                                      int id, const char *sig_name,
389                                      int properties)
390 {
391         struct wpas_dbus_priv *iface;
392         DBusMessage *msg;
393         DBusMessageIter iter;
394         char net_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
395
396         iface = wpa_s->global->dbus;
397
398         /* Do nothing if the control interface is not turned on */
399         if (iface == NULL)
400                 return;
401
402         os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
403                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
404                     wpa_s->dbus_new_path, id);
405
406         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
407                                       WPAS_DBUS_NEW_IFACE_INTERFACE,
408                                       sig_name);
409         if (msg == NULL)
410                 return;
411
412         dbus_message_iter_init_append(msg, &iter);
413         path = net_obj_path;
414         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
415                                             &path))
416                 goto err;
417
418         if (properties) {
419                 if (!wpa_dbus_get_object_properties(
420                             iface, net_obj_path, WPAS_DBUS_NEW_IFACE_NETWORK,
421                             &iter))
422                         goto err;
423         }
424
425         dbus_connection_send(iface->con, msg, NULL);
426
427         dbus_message_unref(msg);
428         return;
429
430 err:
431         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
432         dbus_message_unref(msg);
433 }
434
435
436 /**
437  * wpas_dbus_signal_network_added - Send a network added signal
438  * @wpa_s: %wpa_supplicant network interface data
439  * @id: new network id
440  *
441  * Notify listeners about adding new network
442  */
443 static void wpas_dbus_signal_network_added(struct wpa_supplicant *wpa_s,
444                                            int id)
445 {
446         wpas_dbus_signal_network(wpa_s, id, "NetworkAdded", TRUE);
447 }
448
449
450 /**
451  * wpas_dbus_signal_network_removed - Send a network removed signal
452  * @wpa_s: %wpa_supplicant network interface data
453  * @id: network id
454  *
455  * Notify listeners about removing a network
456  */
457 static void wpas_dbus_signal_network_removed(struct wpa_supplicant *wpa_s,
458                                              int id)
459 {
460         wpas_dbus_signal_network(wpa_s, id, "NetworkRemoved", FALSE);
461 }
462
463
464 /**
465  * wpas_dbus_signal_network_selected - Send a network selected signal
466  * @wpa_s: %wpa_supplicant network interface data
467  * @id: network id
468  *
469  * Notify listeners about selecting a network
470  */
471 void wpas_dbus_signal_network_selected(struct wpa_supplicant *wpa_s, int id)
472 {
473         wpas_dbus_signal_network(wpa_s, id, "NetworkSelected", FALSE);
474 }
475
476
477 /**
478  * wpas_dbus_signal_network_request - Indicate that additional information
479  * (EAP password, etc.) is required to complete the association to this SSID
480  * @wpa_s: %wpa_supplicant network interface data
481  * @rtype: The specific additional information required
482  * @default_text: Optional description of required information
483  *
484  * Request additional information or passwords to complete an association
485  * request.
486  */
487 void wpas_dbus_signal_network_request(struct wpa_supplicant *wpa_s,
488                                       struct wpa_ssid *ssid,
489                                       enum wpa_ctrl_req_type rtype,
490                                       const char *default_txt)
491 {
492         struct wpas_dbus_priv *iface;
493         DBusMessage *msg;
494         DBusMessageIter iter;
495         char net_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
496         const char *field, *txt = NULL, *net_ptr;
497
498         iface = wpa_s->global->dbus;
499
500         /* Do nothing if the control interface is not turned on */
501         if (iface == NULL)
502                 return;
503
504         field = wpa_supplicant_ctrl_req_to_string(rtype, default_txt, &txt);
505         if (field == NULL)
506                 return;
507
508         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
509                                       WPAS_DBUS_NEW_IFACE_INTERFACE,
510                                       "NetworkRequest");
511         if (msg == NULL)
512                 return;
513
514         os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
515                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
516                     wpa_s->dbus_new_path, ssid->id);
517         net_ptr = &net_obj_path[0];
518
519         dbus_message_iter_init_append(msg, &iter);
520         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
521                                             &net_ptr))
522                 goto err;
523         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &field))
524                 goto err;
525         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &txt))
526                 goto err;
527
528         dbus_connection_send(iface->con, msg, NULL);
529         dbus_message_unref(msg);
530         return;
531
532 err:
533         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
534         dbus_message_unref(msg);
535 }
536
537
538 /**
539  * wpas_dbus_signal_network_enabled_changed - Signals Enabled property changes
540  * @wpa_s: %wpa_supplicant network interface data
541  * @ssid: configured network which Enabled property has changed
542  *
543  * Sends PropertyChanged signals containing new value of Enabled property
544  * for specified network
545  */
546 void wpas_dbus_signal_network_enabled_changed(struct wpa_supplicant *wpa_s,
547                                               struct wpa_ssid *ssid)
548 {
549
550         char path[WPAS_DBUS_OBJECT_PATH_MAX];
551         os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
552                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
553                     wpa_s->dbus_new_path, ssid->id);
554
555         wpa_dbus_mark_property_changed(wpa_s->global->dbus, path,
556                                        WPAS_DBUS_NEW_IFACE_NETWORK, "Enabled");
557 }
558
559
560 #ifdef CONFIG_WPS
561
562 /**
563  * wpas_dbus_signal_wps_event_success - Signals Success WPS event
564  * @wpa_s: %wpa_supplicant network interface data
565  *
566  * Sends Event dbus signal with name "success" and empty dict as arguments
567  */
568 void wpas_dbus_signal_wps_event_success(struct wpa_supplicant *wpa_s)
569 {
570
571         DBusMessage *msg;
572         DBusMessageIter iter, dict_iter;
573         struct wpas_dbus_priv *iface;
574         char *key = "success";
575
576         iface = wpa_s->global->dbus;
577
578         /* Do nothing if the control interface is not turned on */
579         if (iface == NULL)
580                 return;
581
582         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
583                                       WPAS_DBUS_NEW_IFACE_WPS, "Event");
584         if (msg == NULL)
585                 return;
586
587         dbus_message_iter_init_append(msg, &iter);
588
589         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) ||
590             !wpa_dbus_dict_open_write(&iter, &dict_iter) ||
591             !wpa_dbus_dict_close_write(&iter, &dict_iter))
592                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
593         else
594                 dbus_connection_send(iface->con, msg, NULL);
595
596         dbus_message_unref(msg);
597 }
598
599
600 /**
601  * wpas_dbus_signal_wps_event_fail - Signals Fail WPS event
602  * @wpa_s: %wpa_supplicant network interface data
603  *
604  * Sends Event dbus signal with name "fail" and dictionary containing
605  * "msg field with fail message number (int32) as arguments
606  */
607 void wpas_dbus_signal_wps_event_fail(struct wpa_supplicant *wpa_s,
608                                      struct wps_event_fail *fail)
609 {
610
611         DBusMessage *msg;
612         DBusMessageIter iter, dict_iter;
613         struct wpas_dbus_priv *iface;
614         char *key = "fail";
615
616         iface = wpa_s->global->dbus;
617
618         /* Do nothing if the control interface is not turned on */
619         if (iface == NULL)
620                 return;
621
622         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
623                                       WPAS_DBUS_NEW_IFACE_WPS, "Event");
624         if (msg == NULL)
625                 return;
626
627         dbus_message_iter_init_append(msg, &iter);
628
629         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) ||
630             !wpa_dbus_dict_open_write(&iter, &dict_iter) ||
631             !wpa_dbus_dict_append_int32(&dict_iter, "msg", fail->msg) ||
632             !wpa_dbus_dict_close_write(&iter, &dict_iter))
633                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
634         else
635                 dbus_connection_send(iface->con, msg, NULL);
636
637         dbus_message_unref(msg);
638 }
639
640
641 /**
642  * wpas_dbus_signal_wps_event_m2d - Signals M2D WPS event
643  * @wpa_s: %wpa_supplicant network interface data
644  *
645  * Sends Event dbus signal with name "m2d" and dictionary containing
646  * fields of wps_event_m2d structure.
647  */
648 void wpas_dbus_signal_wps_event_m2d(struct wpa_supplicant *wpa_s,
649                                     struct wps_event_m2d *m2d)
650 {
651
652         DBusMessage *msg;
653         DBusMessageIter iter, dict_iter;
654         struct wpas_dbus_priv *iface;
655         char *key = "m2d";
656
657         iface = wpa_s->global->dbus;
658
659         /* Do nothing if the control interface is not turned on */
660         if (iface == NULL)
661                 return;
662
663         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
664                                       WPAS_DBUS_NEW_IFACE_WPS, "Event");
665         if (msg == NULL)
666                 return;
667
668         dbus_message_iter_init_append(msg, &iter);
669
670         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) ||
671             !wpa_dbus_dict_open_write(&iter, &dict_iter) ||
672             !wpa_dbus_dict_append_uint16(&dict_iter, "config_methods",
673                                          m2d->config_methods) ||
674             !wpa_dbus_dict_append_byte_array(&dict_iter, "manufacturer",
675                                              (const char *) m2d->manufacturer,
676                                              m2d->manufacturer_len) ||
677             !wpa_dbus_dict_append_byte_array(&dict_iter, "model_name",
678                                              (const char *) m2d->model_name,
679                                              m2d->model_name_len) ||
680             !wpa_dbus_dict_append_byte_array(&dict_iter, "model_number",
681                                              (const char *) m2d->model_number,
682                                              m2d->model_number_len) ||
683             !wpa_dbus_dict_append_byte_array(&dict_iter, "serial_number",
684                                              (const char *)
685                                              m2d->serial_number,
686                                              m2d->serial_number_len) ||
687             !wpa_dbus_dict_append_byte_array(&dict_iter, "dev_name",
688                                              (const char *) m2d->dev_name,
689                                              m2d->dev_name_len) ||
690             !wpa_dbus_dict_append_byte_array(&dict_iter, "primary_dev_type",
691                                              (const char *)
692                                              m2d->primary_dev_type, 8) ||
693             !wpa_dbus_dict_append_uint16(&dict_iter, "config_error",
694                                          m2d->config_error) ||
695             !wpa_dbus_dict_append_uint16(&dict_iter, "dev_password_id",
696                                          m2d->dev_password_id) ||
697             !wpa_dbus_dict_close_write(&iter, &dict_iter))
698                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
699         else
700                 dbus_connection_send(iface->con, msg, NULL);
701
702         dbus_message_unref(msg);
703 }
704
705
706 /**
707  * wpas_dbus_signal_wps_cred - Signals new credentials
708  * @wpa_s: %wpa_supplicant network interface data
709  *
710  * Sends signal with credentials in directory argument
711  */
712 void wpas_dbus_signal_wps_cred(struct wpa_supplicant *wpa_s,
713                                const struct wps_credential *cred)
714 {
715         DBusMessage *msg;
716         DBusMessageIter iter, dict_iter;
717         struct wpas_dbus_priv *iface;
718         char *auth_type[6]; /* we have six possible authorization types */
719         int at_num = 0;
720         char *encr_type[4]; /* we have four possible encryption types */
721         int et_num = 0;
722
723         iface = wpa_s->global->dbus;
724
725         /* Do nothing if the control interface is not turned on */
726         if (iface == NULL)
727                 return;
728
729         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
730                                       WPAS_DBUS_NEW_IFACE_WPS,
731                                       "Credentials");
732         if (msg == NULL)
733                 return;
734
735         dbus_message_iter_init_append(msg, &iter);
736         if (!wpa_dbus_dict_open_write(&iter, &dict_iter))
737                 goto nomem;
738
739         if (cred->auth_type & WPS_AUTH_OPEN)
740                 auth_type[at_num++] = "open";
741         if (cred->auth_type & WPS_AUTH_WPAPSK)
742                 auth_type[at_num++] = "wpa-psk";
743         if (cred->auth_type & WPS_AUTH_SHARED)
744                 auth_type[at_num++] = "shared";
745         if (cred->auth_type & WPS_AUTH_WPA)
746                 auth_type[at_num++] = "wpa-eap";
747         if (cred->auth_type & WPS_AUTH_WPA2)
748                 auth_type[at_num++] = "wpa2-eap";
749         if (cred->auth_type & WPS_AUTH_WPA2PSK)
750                 auth_type[at_num++] =
751                 "wpa2-psk";
752
753         if (cred->encr_type & WPS_ENCR_NONE)
754                 encr_type[et_num++] = "none";
755         if (cred->encr_type & WPS_ENCR_WEP)
756                 encr_type[et_num++] = "wep";
757         if (cred->encr_type & WPS_ENCR_TKIP)
758                 encr_type[et_num++] = "tkip";
759         if (cred->encr_type & WPS_ENCR_AES)
760                 encr_type[et_num++] = "aes";
761
762         if (wpa_s->current_ssid) {
763                 if (!wpa_dbus_dict_append_byte_array(
764                             &dict_iter, "BSSID",
765                             (const char *) wpa_s->current_ssid->bssid,
766                             ETH_ALEN))
767                         goto nomem;
768         }
769
770         if (!wpa_dbus_dict_append_byte_array(&dict_iter, "SSID",
771                                              (const char *) cred->ssid,
772                                              cred->ssid_len) ||
773             !wpa_dbus_dict_append_string_array(&dict_iter, "AuthType",
774                                                (const char **) auth_type,
775                                                at_num) ||
776             !wpa_dbus_dict_append_string_array(&dict_iter, "EncrType",
777                                                (const char **) encr_type,
778                                                et_num) ||
779             !wpa_dbus_dict_append_byte_array(&dict_iter, "Key",
780                                              (const char *) cred->key,
781                                              cred->key_len) ||
782             !wpa_dbus_dict_append_uint32(&dict_iter, "KeyIndex",
783                                          cred->key_idx) ||
784             !wpa_dbus_dict_close_write(&iter, &dict_iter))
785                 goto nomem;
786
787         dbus_connection_send(iface->con, msg, NULL);
788
789 nomem:
790         dbus_message_unref(msg);
791 }
792
793 #endif /* CONFIG_WPS */
794
795 void wpas_dbus_signal_certification(struct wpa_supplicant *wpa_s,
796                                     int depth, const char *subject,
797                                     const char *cert_hash,
798                                     const struct wpabuf *cert)
799 {
800         struct wpas_dbus_priv *iface;
801         DBusMessage *msg;
802         DBusMessageIter iter, dict_iter;
803
804         iface = wpa_s->global->dbus;
805
806         /* Do nothing if the control interface is not turned on */
807         if (iface == NULL)
808                 return;
809
810         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
811                                       WPAS_DBUS_NEW_IFACE_INTERFACE,
812                                       "Certification");
813         if (msg == NULL)
814                 return;
815
816         dbus_message_iter_init_append(msg, &iter);
817         if (!wpa_dbus_dict_open_write(&iter, &dict_iter))
818                 goto nomem;
819
820         if (!wpa_dbus_dict_append_uint32(&dict_iter, "depth", depth) ||
821             !wpa_dbus_dict_append_string(&dict_iter, "subject", subject))
822                 goto nomem;
823
824         if (cert_hash &&
825             !wpa_dbus_dict_append_string(&dict_iter, "cert_hash", cert_hash))
826                 goto nomem;
827
828         if (cert &&
829             !wpa_dbus_dict_append_byte_array(&dict_iter, "cert",
830                                              wpabuf_head(cert),
831                                              wpabuf_len(cert)))
832                 goto nomem;
833
834         if (!wpa_dbus_dict_close_write(&iter, &dict_iter))
835                 goto nomem;
836
837         dbus_connection_send(iface->con, msg, NULL);
838
839 nomem:
840         dbus_message_unref(msg);
841 }
842
843
844 void wpas_dbus_signal_eap_status(struct wpa_supplicant *wpa_s,
845                                  const char *status, const char *parameter)
846 {
847         struct wpas_dbus_priv *iface;
848         DBusMessage *msg;
849         DBusMessageIter iter;
850
851         iface = wpa_s->global->dbus;
852
853         /* Do nothing if the control interface is not turned on */
854         if (iface == NULL)
855                 return;
856
857         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
858                                       WPAS_DBUS_NEW_IFACE_INTERFACE,
859                                       "EAP");
860         if (msg == NULL)
861                 return;
862
863         dbus_message_iter_init_append(msg, &iter);
864
865         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &status)
866             ||
867             !dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING,
868                                             &parameter))
869                 goto nomem;
870
871         dbus_connection_send(iface->con, msg, NULL);
872
873 nomem:
874         dbus_message_unref(msg);
875 }
876
877
878 #ifdef CONFIG_P2P
879
880 /**
881  * wpas_dbus_signal_p2p_group_removed - Signals P2P group was removed
882  * @wpa_s: %wpa_supplicant network interface data
883  * @role: role of this device (client or GO)
884  * Sends signal with i/f name and role as string arguments
885  */
886 void wpas_dbus_signal_p2p_group_removed(struct wpa_supplicant *wpa_s,
887                                         const char *role)
888 {
889
890         DBusMessage *msg;
891         DBusMessageIter iter;
892         struct wpas_dbus_priv *iface = wpa_s->global->dbus;
893         char *ifname = wpa_s->ifname;
894
895         /* Do nothing if the control interface is not turned on */
896         if (iface == NULL)
897                 return;
898
899         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
900                                       WPAS_DBUS_NEW_IFACE_P2PDEVICE,
901                                       "GroupFinished");
902         if (msg == NULL)
903                 return;
904
905         dbus_message_iter_init_append(msg, &iter);
906
907         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &ifname)) {
908                 wpa_printf(MSG_ERROR, "dbus: Failed to construct GroupFinished"
909                                       "signal -not enough memory for ifname ");
910                 goto err;
911         }
912
913         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &role))
914                 wpa_printf(MSG_ERROR, "dbus: Failed to construct GroupFinished"
915                                       "signal -not enough memory for role ");
916         else
917                 dbus_connection_send(iface->con, msg, NULL);
918
919 err:
920         dbus_message_unref(msg);
921 }
922
923
924 /**
925  * wpas_dbus_signal_p2p_provision_discovery - Signals various PD events
926  *
927  * @dev_addr - who sent the request or responded to our request.
928  * @request - Will be 1 if request, 0 for response.
929  * @status - valid only in case of response
930  * @config_methods - wps config methods
931  * @generated_pin - pin to be displayed in case of WPS_CONFIG_DISPLAY method
932  *
933  * Sends following provision discovery related events:
934  *      ProvisionDiscoveryRequestDisplayPin
935  *      ProvisionDiscoveryResponseDisplayPin
936  *      ProvisionDiscoveryRequestEnterPin
937  *      ProvisionDiscoveryResponseEnterPin
938  *      ProvisionDiscoveryPBCRequest
939  *      ProvisionDiscoveryPBCResponse
940  *
941  *      TODO::
942  *      ProvisionDiscoveryFailure (timeout case)
943  */
944 void wpas_dbus_signal_p2p_provision_discovery(struct wpa_supplicant *wpa_s,
945                                               const u8 *dev_addr, int request,
946                                               enum p2p_prov_disc_status status,
947                                               u16 config_methods,
948                                               unsigned int generated_pin)
949 {
950         DBusMessage *msg;
951         DBusMessageIter iter;
952         struct wpas_dbus_priv *iface;
953         char *_signal;
954         int add_pin = 0;
955         char peer_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
956         int error_ret = 1;
957         char pin[9], *p_pin = NULL;
958
959         iface = wpa_s->global->dbus;
960
961         /* Do nothing if the control interface is not turned on */
962         if (iface == NULL)
963                 return;
964
965         if (request || !status) {
966                 if (config_methods & WPS_CONFIG_DISPLAY)
967                         _signal = request ?
968                                  "ProvisionDiscoveryRequestDisplayPin" :
969                                  "ProvisionDiscoveryResponseEnterPin";
970                 else if (config_methods & WPS_CONFIG_KEYPAD)
971                         _signal = request ?
972                                  "ProvisionDiscoveryRequestEnterPin" :
973                                  "ProvisionDiscoveryResponseDisplayPin";
974                 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
975                         _signal = request ? "ProvisionDiscoveryPBCRequest" :
976                                    "ProvisionDiscoveryPBCResponse";
977                 else
978                         return; /* Unknown or un-supported method */
979         } else if (!request && status)
980                 /* Explicit check for failure response */
981                 _signal = "ProvisionDiscoveryFailure";
982
983         add_pin = ((request && (config_methods & WPS_CONFIG_DISPLAY)) ||
984                    (!request && !status &&
985                         (config_methods & WPS_CONFIG_KEYPAD)));
986
987         if (add_pin) {
988                 os_snprintf(pin, sizeof(pin), "%08d", generated_pin);
989                 p_pin = pin;
990         }
991
992         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
993                                       WPAS_DBUS_NEW_IFACE_P2PDEVICE, _signal);
994         if (msg == NULL)
995                 return;
996
997         /* Check if this is a known peer */
998         if (!p2p_peer_known(wpa_s->global->p2p, dev_addr))
999                 goto error;
1000
1001         os_snprintf(peer_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1002                         "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART "/"
1003                         COMPACT_MACSTR,
1004                         wpa_s->dbus_new_path, MAC2STR(dev_addr));
1005
1006         path = peer_obj_path;
1007
1008         dbus_message_iter_init_append(msg, &iter);
1009
1010         if (!dbus_message_iter_append_basic(&iter,
1011                                             DBUS_TYPE_OBJECT_PATH,
1012                                             &path))
1013                         goto error;
1014
1015         if (!request && status)
1016                 /* Attach status to ProvisionDiscoveryFailure */
1017                 error_ret = !dbus_message_iter_append_basic(&iter,
1018                                                     DBUS_TYPE_INT32,
1019                                                     &status);
1020         else
1021                 error_ret = (add_pin &&
1022                                  !dbus_message_iter_append_basic(&iter,
1023                                                         DBUS_TYPE_STRING,
1024                                                         &p_pin));
1025
1026 error:
1027         if (!error_ret)
1028                 dbus_connection_send(iface->con, msg, NULL);
1029         else
1030                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
1031
1032         dbus_message_unref(msg);
1033 }
1034
1035
1036 void wpas_dbus_signal_p2p_go_neg_req(struct wpa_supplicant *wpa_s,
1037                                      const u8 *src, u16 dev_passwd_id)
1038 {
1039         DBusMessage *msg;
1040         DBusMessageIter iter;
1041         struct wpas_dbus_priv *iface;
1042         char peer_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
1043
1044         iface = wpa_s->global->dbus;
1045
1046         /* Do nothing if the control interface is not turned on */
1047         if (iface == NULL)
1048                 return;
1049
1050         os_snprintf(peer_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1051                     "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART "/" COMPACT_MACSTR,
1052                     wpa_s->dbus_new_path, MAC2STR(src));
1053         path = peer_obj_path;
1054
1055         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
1056                                       WPAS_DBUS_NEW_IFACE_P2PDEVICE,
1057                                       "GONegotiationRequest");
1058         if (msg == NULL)
1059                 return;
1060
1061         dbus_message_iter_init_append(msg, &iter);
1062
1063         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1064                                             &path) ||
1065             !dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT16,
1066                                             &dev_passwd_id))
1067                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
1068         else
1069                 dbus_connection_send(iface->con, msg, NULL);
1070
1071         dbus_message_unref(msg);
1072 }
1073
1074
1075 static int wpas_dbus_get_group_obj_path(struct wpa_supplicant *wpa_s,
1076                                         const struct wpa_ssid *ssid,
1077                                         char *group_obj_path)
1078 {
1079         char group_name[3];
1080
1081         if (os_memcmp(ssid->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN))
1082                 return -1;
1083
1084         os_memcpy(group_name, ssid->ssid + P2P_WILDCARD_SSID_LEN, 2);
1085         group_name[2] = '\0';
1086
1087         os_snprintf(group_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1088                     "%s/" WPAS_DBUS_NEW_P2P_GROUPS_PART "/%s",
1089                     wpa_s->dbus_new_path, group_name);
1090
1091         return 0;
1092 }
1093
1094
1095 /**
1096  * wpas_dbus_signal_p2p_group_started - Signals P2P group has
1097  * started. Emitted when a group is successfully started
1098  * irrespective of the role (client/GO) of the current device
1099  *
1100  * @wpa_s: %wpa_supplicant network interface data
1101  * @ssid: SSID object
1102  * @client: this device is P2P client
1103  * @network_id: network id of the group started, use instead of ssid->id
1104  *      to account for persistent groups
1105  */
1106 void wpas_dbus_signal_p2p_group_started(struct wpa_supplicant *wpa_s,
1107                                         const struct wpa_ssid *ssid,
1108                                         int client, int network_id)
1109 {
1110         DBusMessage *msg;
1111         DBusMessageIter iter, dict_iter;
1112         struct wpas_dbus_priv *iface;
1113         char net_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
1114         char group_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
1115
1116         iface = wpa_s->parent->global->dbus;
1117
1118         /* Do nothing if the control interface is not turned on */
1119         if (iface == NULL)
1120                 return;
1121
1122         if (wpas_dbus_get_group_obj_path(wpa_s, ssid, group_obj_path) < 0)
1123                 return;
1124
1125         /* New interface has been created for this group */
1126         msg = dbus_message_new_signal(wpa_s->parent->dbus_new_path,
1127                                       WPAS_DBUS_NEW_IFACE_P2PDEVICE,
1128                                       "GroupStarted");
1129
1130         if (msg == NULL)
1131                 return;
1132
1133         dbus_message_iter_init_append(msg, &iter);
1134         if (!wpa_dbus_dict_open_write(&iter, &dict_iter))
1135                 goto nomem;
1136
1137         /*
1138          * In case the device supports creating a separate interface the
1139          * DBus client will need to know the object path for the interface
1140          * object this group was created on, so include it here.
1141          */
1142         if (!wpa_dbus_dict_append_object_path(&dict_iter,
1143                                         "interface_object",
1144                                         wpa_s->dbus_new_path))
1145                 goto nomem;
1146
1147         if (!wpa_dbus_dict_append_string(&dict_iter, "role",
1148                                          client ? "client" : "GO"))
1149                 goto nomem;
1150
1151         os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1152                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
1153                     wpa_s->parent->dbus_new_path, network_id);
1154
1155         if (!wpa_dbus_dict_append_object_path(&dict_iter, "group_object",
1156                                              group_obj_path) ||
1157            !wpa_dbus_dict_append_object_path(&dict_iter, "network_object",
1158                                              net_obj_path) ||
1159            !wpa_dbus_dict_close_write(&iter, &dict_iter))
1160                 goto nomem;
1161
1162         dbus_connection_send(iface->con, msg, NULL);
1163
1164 nomem:
1165         dbus_message_unref(msg);
1166 }
1167
1168
1169 /**
1170  *
1171  * Method to emit GONeogtiation Success or Failure signals based
1172  * on status.
1173  * @status: Status of the GO neg request. 0 for success, other for errors.
1174  */
1175 void wpas_dbus_signal_p2p_go_neg_resp(struct wpa_supplicant *wpa_s,
1176                                       struct p2p_go_neg_results *res)
1177 {
1178         DBusMessage *msg;
1179         DBusMessageIter iter, dict_iter;
1180         DBusMessageIter iter_dict_entry, iter_dict_val, iter_dict_array;
1181         struct wpas_dbus_priv *iface;
1182         char peer_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
1183         dbus_int32_t freqs[P2P_MAX_CHANNELS];
1184         dbus_int32_t *f_array = freqs;
1185
1186
1187         iface = wpa_s->global->dbus;
1188
1189         os_memset(freqs, 0, sizeof(freqs));
1190         /* Do nothing if the control interface is not turned on */
1191         if (iface == NULL)
1192                 return;
1193
1194         os_snprintf(peer_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1195                     "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART "/" COMPACT_MACSTR,
1196                     wpa_s->dbus_new_path, MAC2STR(res->peer_device_addr));
1197         path = peer_obj_path;
1198
1199         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
1200                                       WPAS_DBUS_NEW_IFACE_P2PDEVICE,
1201                                       res->status ? "GONegotiationFailure" :
1202                                                     "GONegotiationSuccess");
1203         if (msg == NULL)
1204                 return;
1205
1206         dbus_message_iter_init_append(msg, &iter);
1207         if (!wpa_dbus_dict_open_write(&iter, &dict_iter))
1208                 goto err;
1209         if (!wpa_dbus_dict_append_object_path(&dict_iter, "peer_object",
1210                                               path) ||
1211             !wpa_dbus_dict_append_int32(&dict_iter, "status", res->status))
1212                 goto err;
1213
1214         if (!res->status) {
1215                 int i = 0;
1216                 int freq_list_num = 0;
1217
1218                 if (res->role_go) {
1219                         if (!wpa_dbus_dict_append_byte_array(
1220                                     &dict_iter, "passphrase",
1221                                     (const char *) res->passphrase,
1222                                     sizeof(res->passphrase)))
1223                                 goto err;
1224                 }
1225
1226                 if (!wpa_dbus_dict_append_string(&dict_iter, "role_go",
1227                                                  res->role_go ? "GO" :
1228                                                  "client") ||
1229                     !wpa_dbus_dict_append_int32(&dict_iter, "frequency",
1230                                                 res->freq) ||
1231                     !wpa_dbus_dict_append_byte_array(&dict_iter, "ssid",
1232                                                      (const char *) res->ssid,
1233                                                      res->ssid_len) ||
1234                     !wpa_dbus_dict_append_byte_array(&dict_iter,
1235                                                      "peer_device_addr",
1236                                                      (const char *)
1237                                                      res->peer_device_addr,
1238                                                      ETH_ALEN) ||
1239                     !wpa_dbus_dict_append_byte_array(&dict_iter,
1240                                                      "peer_interface_addr",
1241                                                      (const char *)
1242                                                      res->peer_interface_addr,
1243                                                      ETH_ALEN) ||
1244                     !wpa_dbus_dict_append_string(&dict_iter, "wps_method",
1245                                                  p2p_wps_method_text(
1246                                                          res->wps_method)))
1247                         goto err;
1248
1249                 for (i = 0; i < P2P_MAX_CHANNELS; i++) {
1250                         if (res->freq_list[i]) {
1251                                 freqs[i] = res->freq_list[i];
1252                                 freq_list_num++;
1253                         }
1254                 }
1255
1256                 if (!wpa_dbus_dict_begin_array(&dict_iter,
1257                                                "frequency_list",
1258                                                DBUS_TYPE_INT32_AS_STRING,
1259                                                &iter_dict_entry,
1260                                                &iter_dict_val,
1261                                                &iter_dict_array))
1262                         goto err;
1263
1264                 if (!dbus_message_iter_append_fixed_array(&iter_dict_array,
1265                                                           DBUS_TYPE_INT32,
1266                                                           &f_array,
1267                                                           freq_list_num))
1268                         goto err;
1269
1270                 if (!wpa_dbus_dict_end_array(&dict_iter,
1271                                              &iter_dict_entry,
1272                                              &iter_dict_val,
1273                                              &iter_dict_array))
1274                         goto err;
1275
1276                 if (!wpa_dbus_dict_append_int32(&dict_iter, "persistent_group",
1277                                                 res->persistent_group) ||
1278                     !wpa_dbus_dict_append_uint32(&dict_iter,
1279                                                  "peer_config_timeout",
1280                                                  res->peer_config_timeout))
1281                         goto err;
1282         }
1283
1284         if (!wpa_dbus_dict_close_write(&iter, &dict_iter))
1285                 goto err;
1286
1287         dbus_connection_send(iface->con, msg, NULL);
1288 err:
1289         dbus_message_unref(msg);
1290 }
1291
1292
1293 /**
1294  *
1295  * Method to emit Invitation Result signal based on status and
1296  * bssid
1297  * @status: Status of the Invite request. 0 for success, other
1298  * for errors
1299  * @bssid : Basic Service Set Identifier
1300  */
1301 void wpas_dbus_signal_p2p_invitation_result(struct wpa_supplicant *wpa_s,
1302                                             int status, const u8 *bssid)
1303 {
1304         DBusMessage *msg;
1305         DBusMessageIter iter, dict_iter;
1306         struct wpas_dbus_priv *iface;
1307
1308         wpa_printf(MSG_INFO, "%s\n", __func__);
1309
1310         iface = wpa_s->global->dbus;
1311         /* Do nothing if the control interface is not turned on */
1312         if (iface == NULL)
1313                 return;
1314
1315         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
1316                                       WPAS_DBUS_NEW_IFACE_P2PDEVICE,
1317                                       "InvitationResult");
1318
1319         if (msg == NULL)
1320                 return;
1321
1322         dbus_message_iter_init_append(msg, &iter);
1323         if (!wpa_dbus_dict_open_write(&iter, &dict_iter))
1324                 goto nomem;
1325
1326         if (!wpa_dbus_dict_append_int32(&dict_iter, "status", status))
1327                 goto nomem;
1328         if (bssid) {
1329                 if (!wpa_dbus_dict_append_byte_array(&dict_iter, "BSSID",
1330                                                      (const char *) bssid,
1331                                                      ETH_ALEN))
1332                         goto nomem;
1333         }
1334         if (!wpa_dbus_dict_close_write(&iter, &dict_iter))
1335                 goto nomem;
1336
1337         dbus_connection_send(iface->con, msg, NULL);
1338
1339 nomem:
1340         dbus_message_unref(msg);
1341 }
1342
1343
1344 /**
1345  *
1346  * Method to emit a signal for a peer joining the group.
1347  * The signal will carry path to the group member object
1348  * constructed using p2p i/f addr used for connecting.
1349  *
1350  * @wpa_s: %wpa_supplicant network interface data
1351  * @member_addr: addr (p2p i/f) of the peer joining the group
1352  */
1353 void wpas_dbus_signal_p2p_peer_joined(struct wpa_supplicant *wpa_s,
1354                                       const u8 *member)
1355 {
1356         struct wpas_dbus_priv *iface;
1357         DBusMessage *msg;
1358         DBusMessageIter iter;
1359         char groupmember_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
1360
1361         iface = wpa_s->global->dbus;
1362
1363         /* Do nothing if the control interface is not turned on */
1364         if (iface == NULL)
1365                 return;
1366
1367         if (!wpa_s->dbus_groupobj_path)
1368                 return;
1369
1370         os_snprintf(groupmember_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1371                         "%s/"  WPAS_DBUS_NEW_P2P_GROUPMEMBERS_PART "/"
1372                         COMPACT_MACSTR,
1373                         wpa_s->dbus_groupobj_path, MAC2STR(member));
1374
1375         msg = dbus_message_new_signal(wpa_s->dbus_groupobj_path,
1376                                       WPAS_DBUS_NEW_IFACE_P2P_GROUP,
1377                                       "PeerJoined");
1378         if (msg == NULL)
1379                 return;
1380
1381         dbus_message_iter_init_append(msg, &iter);
1382         path = groupmember_obj_path;
1383         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1384                                             &path))
1385                 goto err;
1386
1387         dbus_connection_send(iface->con, msg, NULL);
1388
1389         dbus_message_unref(msg);
1390         return;
1391
1392 err:
1393         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
1394         dbus_message_unref(msg);
1395 }
1396
1397
1398 /**
1399  *
1400  * Method to emit a signal for a peer disconnecting the group.
1401  * The signal will carry path to the group member object
1402  * constructed using p2p i/f addr used for connecting.
1403  *
1404  * @wpa_s: %wpa_supplicant network interface data
1405  * @member_addr: addr (p2p i/f) of the peer joining the group
1406  */
1407 void wpas_dbus_signal_p2p_peer_disconnected(struct wpa_supplicant *wpa_s,
1408                                       const u8 *member)
1409 {
1410         struct wpas_dbus_priv *iface;
1411         DBusMessage *msg;
1412         DBusMessageIter iter;
1413         char groupmember_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
1414
1415         iface = wpa_s->global->dbus;
1416
1417         /* Do nothing if the control interface is not turned on */
1418         if (iface == NULL)
1419                 return;
1420
1421         if (!wpa_s->dbus_groupobj_path)
1422                 return;
1423
1424         os_snprintf(groupmember_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1425                         "%s/"  WPAS_DBUS_NEW_P2P_GROUPMEMBERS_PART "/"
1426                         COMPACT_MACSTR,
1427                         wpa_s->dbus_groupobj_path, MAC2STR(member));
1428
1429         msg = dbus_message_new_signal(wpa_s->dbus_groupobj_path,
1430                                       WPAS_DBUS_NEW_IFACE_P2P_GROUP,
1431                                       "PeerDisconnected");
1432         if (msg == NULL)
1433                 return;
1434
1435         dbus_message_iter_init_append(msg, &iter);
1436         path = groupmember_obj_path;
1437         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1438                                             &path))
1439                 goto err;
1440
1441         dbus_connection_send(iface->con, msg, NULL);
1442
1443         dbus_message_unref(msg);
1444         return;
1445
1446 err:
1447         wpa_printf(MSG_ERROR, "dbus: Failed to construct PeerDisconnected "
1448                               "signal");
1449         dbus_message_unref(msg);
1450 }
1451
1452
1453 /**
1454  *
1455  * Method to emit a signal for a service discovery request.
1456  * The signal will carry station address, frequency, dialog token,
1457  * update indicator and it tlvs
1458  *
1459  * @wpa_s: %wpa_supplicant network interface data
1460  * @sa: station addr (p2p i/f) of the peer
1461  * @dialog_token: service discovery request dialog token
1462  * @update_indic: service discovery request update indicator
1463  * @tlvs: service discovery request genrated byte array of tlvs
1464  * @tlvs_len: service discovery request tlvs length
1465  */
1466 void wpas_dbus_signal_p2p_sd_request(struct wpa_supplicant *wpa_s,
1467                                      int freq, const u8 *sa, u8 dialog_token,
1468                                      u16 update_indic, const u8 *tlvs,
1469                                      size_t tlvs_len)
1470 {
1471         DBusMessage *msg;
1472         DBusMessageIter iter, dict_iter;
1473         struct wpas_dbus_priv *iface;
1474         char peer_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
1475         iface = wpa_s->global->dbus;
1476
1477         /* Do nothing if the control interface is not turned on */
1478         if (iface == NULL)
1479                 return;
1480
1481         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
1482                                       WPAS_DBUS_NEW_IFACE_P2PDEVICE,
1483                                       "ServiceDiscoveryRequest");
1484         if (msg == NULL)
1485                 return;
1486
1487         /* Check if this is a known peer */
1488         if (!p2p_peer_known(wpa_s->global->p2p, sa))
1489                 goto error;
1490
1491         os_snprintf(peer_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1492                     "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART "/"
1493                     COMPACT_MACSTR, wpa_s->dbus_new_path, MAC2STR(sa));
1494
1495         path = peer_obj_path;
1496
1497         dbus_message_iter_init_append(msg, &iter);
1498         if (!wpa_dbus_dict_open_write(&iter, &dict_iter))
1499                 goto error;
1500
1501
1502         if (!wpa_dbus_dict_append_object_path(&dict_iter, "peer_object",
1503                                               path) ||
1504             !wpa_dbus_dict_append_int32(&dict_iter, "frequency", freq) ||
1505             !wpa_dbus_dict_append_int32(&dict_iter, "dialog_token",
1506                                         dialog_token) ||
1507             !wpa_dbus_dict_append_uint16(&dict_iter, "update_indicator",
1508                                          update_indic) ||
1509             !wpa_dbus_dict_append_byte_array(&dict_iter, "tlvs",
1510                                              (const char *) tlvs,
1511                                              tlvs_len) ||
1512             !wpa_dbus_dict_close_write(&iter, &dict_iter))
1513                 goto error;
1514
1515         dbus_connection_send(iface->con, msg, NULL);
1516         dbus_message_unref(msg);
1517         return;
1518 error:
1519         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
1520         dbus_message_unref(msg);
1521 }
1522
1523
1524 /**
1525  *
1526  * Method to emit a signal for a service discovery response.
1527  * The signal will carry station address, update indicator and it
1528  * tlvs
1529  *
1530  * @wpa_s: %wpa_supplicant network interface data
1531  * @sa: station addr (p2p i/f) of the peer
1532  * @update_indic: service discovery request update indicator
1533  * @tlvs: service discovery request genrated byte array of tlvs
1534  * @tlvs_len: service discovery request tlvs length
1535  */
1536 void wpas_dbus_signal_p2p_sd_response(struct wpa_supplicant *wpa_s,
1537                                       const u8 *sa, u16 update_indic,
1538                                       const u8 *tlvs, size_t tlvs_len)
1539 {
1540         DBusMessage *msg;
1541         DBusMessageIter iter, dict_iter;
1542         struct wpas_dbus_priv *iface;
1543         char peer_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
1544         iface = wpa_s->global->dbus;
1545
1546         /* Do nothing if the control interface is not turned on */
1547         if (iface == NULL)
1548                 return;
1549
1550         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
1551                                       WPAS_DBUS_NEW_IFACE_P2PDEVICE,
1552                                 "ServiceDiscoveryResponse");
1553         if (msg == NULL)
1554                 return;
1555
1556         /* Check if this is a known peer */
1557         if (!p2p_peer_known(wpa_s->global->p2p, sa))
1558                 goto error;
1559
1560         os_snprintf(peer_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1561                     "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART "/"
1562                     COMPACT_MACSTR, wpa_s->dbus_new_path, MAC2STR(sa));
1563
1564         path = peer_obj_path;
1565
1566         dbus_message_iter_init_append(msg, &iter);
1567         if (!wpa_dbus_dict_open_write(&iter, &dict_iter))
1568                 goto error;
1569
1570         if (!wpa_dbus_dict_append_object_path(&dict_iter, "peer_object",
1571                                               path) ||
1572             !wpa_dbus_dict_append_uint16(&dict_iter, "update_indicator",
1573                                          update_indic) ||
1574             !wpa_dbus_dict_append_byte_array(&dict_iter, "tlvs",
1575                                              (const char *) tlvs,
1576                                              tlvs_len) ||
1577             !wpa_dbus_dict_close_write(&iter, &dict_iter))
1578                 goto error;
1579
1580
1581         dbus_connection_send(iface->con, msg, NULL);
1582         dbus_message_unref(msg);
1583         return;
1584 error:
1585         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
1586         dbus_message_unref(msg);
1587 }
1588
1589 /**
1590  * wpas_dbus_signal_persistent_group - Send a persistent group related
1591  *      event signal
1592  * @wpa_s: %wpa_supplicant network interface data
1593  * @id: new persistent group id
1594  * @sig_name: signal name - PersistentGroupAdded, PersistentGroupRemoved
1595  * @properties: determines if add second argument with object properties
1596  *
1597  * Notify listeners about an event related to persistent groups.
1598  */
1599 static void wpas_dbus_signal_persistent_group(struct wpa_supplicant *wpa_s,
1600                                               int id, const char *sig_name,
1601                                               int properties)
1602 {
1603         struct wpas_dbus_priv *iface;
1604         DBusMessage *msg;
1605         DBusMessageIter iter;
1606         char pgrp_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
1607
1608         iface = wpa_s->global->dbus;
1609
1610         /* Do nothing if the control interface is not turned on */
1611         if (iface == NULL)
1612                 return;
1613
1614         os_snprintf(pgrp_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1615                     "%s/" WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/%u",
1616                     wpa_s->dbus_new_path, id);
1617
1618         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
1619                                       WPAS_DBUS_NEW_IFACE_P2PDEVICE,
1620                                       sig_name);
1621         if (msg == NULL)
1622                 return;
1623
1624         dbus_message_iter_init_append(msg, &iter);
1625         path = pgrp_obj_path;
1626         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
1627                                             &path))
1628                 goto err;
1629
1630         if (properties) {
1631                 if (!wpa_dbus_get_object_properties(
1632                             iface, pgrp_obj_path,
1633                             WPAS_DBUS_NEW_IFACE_PERSISTENT_GROUP, &iter))
1634                         goto err;
1635         }
1636
1637         dbus_connection_send(iface->con, msg, NULL);
1638
1639         dbus_message_unref(msg);
1640         return;
1641
1642 err:
1643         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
1644         dbus_message_unref(msg);
1645 }
1646
1647
1648 /**
1649  * wpas_dbus_signal_persistent_group_added - Send a persistent_group
1650  *      added signal
1651  * @wpa_s: %wpa_supplicant network interface data
1652  * @id: new persistent group id
1653  *
1654  * Notify listeners about addition of a new persistent group.
1655  */
1656 static void wpas_dbus_signal_persistent_group_added(
1657         struct wpa_supplicant *wpa_s, int id)
1658 {
1659         wpas_dbus_signal_persistent_group(wpa_s, id, "PersistentGroupAdded",
1660                                           TRUE);
1661 }
1662
1663
1664 /**
1665  * wpas_dbus_signal_persistent_group_removed - Send a persistent_group
1666  *      removed signal
1667  * @wpa_s: %wpa_supplicant network interface data
1668  * @id: persistent group id
1669  *
1670  * Notify listeners about removal of a persistent group.
1671  */
1672 static void wpas_dbus_signal_persistent_group_removed(
1673         struct wpa_supplicant *wpa_s, int id)
1674 {
1675         wpas_dbus_signal_persistent_group(wpa_s, id, "PersistentGroupRemoved",
1676                                           FALSE);
1677 }
1678
1679
1680 /**
1681  * wpas_dbus_signal_p2p_wps_failed - Signals WpsFailed event
1682  * @wpa_s: %wpa_supplicant network interface data
1683  *
1684  * Sends Event dbus signal with name "fail" and dictionary containing
1685  * "msg" field with fail message number (int32) as arguments
1686  */
1687 void wpas_dbus_signal_p2p_wps_failed(struct wpa_supplicant *wpa_s,
1688                                      struct wps_event_fail *fail)
1689 {
1690
1691         DBusMessage *msg;
1692         DBusMessageIter iter, dict_iter;
1693         struct wpas_dbus_priv *iface;
1694         char *key = "fail";
1695
1696         iface = wpa_s->global->dbus;
1697
1698         /* Do nothing if the control interface is not turned on */
1699         if (iface == NULL)
1700                 return;
1701
1702         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
1703                                       WPAS_DBUS_NEW_IFACE_P2PDEVICE,
1704                                       "WpsFailed");
1705         if (msg == NULL)
1706                 return;
1707
1708         dbus_message_iter_init_append(msg, &iter);
1709
1710         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) ||
1711             !wpa_dbus_dict_open_write(&iter, &dict_iter) ||
1712             !wpa_dbus_dict_append_int32(&dict_iter, "msg", fail->msg) ||
1713             !wpa_dbus_dict_append_int16(&dict_iter, "config_error",
1714                                         fail->config_error) ||
1715             !wpa_dbus_dict_close_write(&iter, &dict_iter))
1716                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
1717         else
1718                 dbus_connection_send(iface->con, msg, NULL);
1719
1720         dbus_message_unref(msg);
1721 }
1722
1723 #endif /*CONFIG_P2P*/
1724
1725
1726 /**
1727  * wpas_dbus_signal_prop_changed - Signals change of property
1728  * @wpa_s: %wpa_supplicant network interface data
1729  * @property: indicates which property has changed
1730  *
1731  * Sends PropertyChanged signals with path, interface and arguments
1732  * depending on which property has changed.
1733  */
1734 void wpas_dbus_signal_prop_changed(struct wpa_supplicant *wpa_s,
1735                                    enum wpas_dbus_prop property)
1736 {
1737         char *prop;
1738
1739         if (wpa_s->dbus_new_path == NULL)
1740                 return; /* Skip signal since D-Bus setup is not yet ready */
1741
1742         switch (property) {
1743         case WPAS_DBUS_PROP_AP_SCAN:
1744                 prop = "ApScan";
1745                 break;
1746         case WPAS_DBUS_PROP_SCANNING:
1747                 prop = "Scanning";
1748                 break;
1749         case WPAS_DBUS_PROP_STATE:
1750                 prop = "State";
1751                 break;
1752         case WPAS_DBUS_PROP_CURRENT_BSS:
1753                 prop = "CurrentBSS";
1754                 break;
1755         case WPAS_DBUS_PROP_CURRENT_NETWORK:
1756                 prop = "CurrentNetwork";
1757                 break;
1758         case WPAS_DBUS_PROP_BSSS:
1759                 prop = "BSSs";
1760                 break;
1761         case WPAS_DBUS_PROP_CURRENT_AUTH_MODE:
1762                 prop = "CurrentAuthMode";
1763                 break;
1764         default:
1765                 wpa_printf(MSG_ERROR, "dbus: %s: Unknown Property value %d",
1766                            __func__, property);
1767                 return;
1768         }
1769
1770         wpa_dbus_mark_property_changed(wpa_s->global->dbus,
1771                                        wpa_s->dbus_new_path,
1772                                        WPAS_DBUS_NEW_IFACE_INTERFACE, prop);
1773 }
1774
1775
1776 /**
1777  * wpas_dbus_bss_signal_prop_changed - Signals change of BSS property
1778  * @wpa_s: %wpa_supplicant network interface data
1779  * @property: indicates which property has changed
1780  * @id: unique BSS identifier
1781  *
1782  * Sends PropertyChanged signals with path, interface, and arguments depending
1783  * on which property has changed.
1784  */
1785 void wpas_dbus_bss_signal_prop_changed(struct wpa_supplicant *wpa_s,
1786                                        enum wpas_dbus_bss_prop property,
1787                                        unsigned int id)
1788 {
1789         char path[WPAS_DBUS_OBJECT_PATH_MAX];
1790         char *prop;
1791
1792         switch (property) {
1793         case WPAS_DBUS_BSS_PROP_SIGNAL:
1794                 prop = "Signal";
1795                 break;
1796         case WPAS_DBUS_BSS_PROP_FREQ:
1797                 prop = "Frequency";
1798                 break;
1799         case WPAS_DBUS_BSS_PROP_MODE:
1800                 prop = "Mode";
1801                 break;
1802         case WPAS_DBUS_BSS_PROP_PRIVACY:
1803                 prop = "Privacy";
1804                 break;
1805         case WPAS_DBUS_BSS_PROP_RATES:
1806                 prop = "Rates";
1807                 break;
1808         case WPAS_DBUS_BSS_PROP_WPA:
1809                 prop = "WPA";
1810                 break;
1811         case WPAS_DBUS_BSS_PROP_RSN:
1812                 prop = "RSN";
1813                 break;
1814         case WPAS_DBUS_BSS_PROP_IES:
1815                 prop = "IEs";
1816                 break;
1817         default:
1818                 wpa_printf(MSG_ERROR, "dbus: %s: Unknown Property value %d",
1819                            __func__, property);
1820                 return;
1821         }
1822
1823         os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
1824                     "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
1825                     wpa_s->dbus_new_path, id);
1826
1827         wpa_dbus_mark_property_changed(wpa_s->global->dbus, path,
1828                                        WPAS_DBUS_NEW_IFACE_BSS, prop);
1829 }
1830
1831
1832 /**
1833  * wpas_dbus_signal_debug_level_changed - Signals change of debug param
1834  * @global: wpa_global structure
1835  *
1836  * Sends PropertyChanged signals informing that debug level has changed.
1837  */
1838 void wpas_dbus_signal_debug_level_changed(struct wpa_global *global)
1839 {
1840         wpa_dbus_mark_property_changed(global->dbus, WPAS_DBUS_NEW_PATH,
1841                                        WPAS_DBUS_NEW_INTERFACE,
1842                                        "DebugLevel");
1843 }
1844
1845
1846 /**
1847  * wpas_dbus_signal_debug_timestamp_changed - Signals change of debug param
1848  * @global: wpa_global structure
1849  *
1850  * Sends PropertyChanged signals informing that debug timestamp has changed.
1851  */
1852 void wpas_dbus_signal_debug_timestamp_changed(struct wpa_global *global)
1853 {
1854         wpa_dbus_mark_property_changed(global->dbus, WPAS_DBUS_NEW_PATH,
1855                                        WPAS_DBUS_NEW_INTERFACE,
1856                                        "DebugTimestamp");
1857 }
1858
1859
1860 /**
1861  * wpas_dbus_signal_debug_show_keys_changed - Signals change of debug param
1862  * @global: wpa_global structure
1863  *
1864  * Sends PropertyChanged signals informing that debug show_keys has changed.
1865  */
1866 void wpas_dbus_signal_debug_show_keys_changed(struct wpa_global *global)
1867 {
1868         wpa_dbus_mark_property_changed(global->dbus, WPAS_DBUS_NEW_PATH,
1869                                        WPAS_DBUS_NEW_INTERFACE,
1870                                        "DebugShowKeys");
1871 }
1872
1873
1874 static void wpas_dbus_register(struct wpa_dbus_object_desc *obj_desc,
1875                                void *priv,
1876                                WPADBusArgumentFreeFunction priv_free,
1877                                const struct wpa_dbus_method_desc *methods,
1878                                const struct wpa_dbus_property_desc *properties,
1879                                const struct wpa_dbus_signal_desc *signals)
1880 {
1881         int n;
1882
1883         obj_desc->user_data = priv;
1884         obj_desc->user_data_free_func = priv_free;
1885         obj_desc->methods = methods;
1886         obj_desc->properties = properties;
1887         obj_desc->signals = signals;
1888
1889         for (n = 0; properties && properties->dbus_property; properties++)
1890                 n++;
1891
1892         obj_desc->prop_changed_flags = os_zalloc(n);
1893         if (!obj_desc->prop_changed_flags)
1894                 wpa_printf(MSG_DEBUG, "dbus: %s: can't register handlers",
1895                            __func__);
1896 }
1897
1898
1899 static const struct wpa_dbus_method_desc wpas_dbus_global_methods[] = {
1900         { "CreateInterface", WPAS_DBUS_NEW_INTERFACE,
1901           (WPADBusMethodHandler) &wpas_dbus_handler_create_interface,
1902           {
1903                   { "args", "a{sv}", ARG_IN },
1904                   { "path", "o", ARG_OUT },
1905                   END_ARGS
1906           }
1907         },
1908         { "RemoveInterface", WPAS_DBUS_NEW_INTERFACE,
1909           (WPADBusMethodHandler) &wpas_dbus_handler_remove_interface,
1910           {
1911                   { "path", "o", ARG_IN },
1912                   END_ARGS
1913           }
1914         },
1915         { "GetInterface", WPAS_DBUS_NEW_INTERFACE,
1916           (WPADBusMethodHandler) &wpas_dbus_handler_get_interface,
1917           {
1918                   { "ifname", "s", ARG_IN },
1919                   { "path", "o", ARG_OUT },
1920                   END_ARGS
1921           }
1922         },
1923         { NULL, NULL, NULL, { END_ARGS } }
1924 };
1925
1926 static const struct wpa_dbus_property_desc wpas_dbus_global_properties[] = {
1927         { "DebugLevel", WPAS_DBUS_NEW_INTERFACE, "s",
1928           wpas_dbus_getter_debug_level,
1929           wpas_dbus_setter_debug_level
1930         },
1931         { "DebugTimestamp", WPAS_DBUS_NEW_INTERFACE, "b",
1932           wpas_dbus_getter_debug_timestamp,
1933           wpas_dbus_setter_debug_timestamp
1934         },
1935         { "DebugShowKeys", WPAS_DBUS_NEW_INTERFACE, "b",
1936           wpas_dbus_getter_debug_show_keys,
1937           wpas_dbus_setter_debug_show_keys
1938         },
1939         { "Interfaces", WPAS_DBUS_NEW_INTERFACE, "ao",
1940           wpas_dbus_getter_interfaces,
1941           NULL
1942         },
1943         { "EapMethods", WPAS_DBUS_NEW_INTERFACE, "as",
1944           wpas_dbus_getter_eap_methods,
1945           NULL
1946         },
1947         { NULL, NULL, NULL, NULL, NULL }
1948 };
1949
1950 static const struct wpa_dbus_signal_desc wpas_dbus_global_signals[] = {
1951         { "InterfaceAdded", WPAS_DBUS_NEW_INTERFACE,
1952           {
1953                   { "path", "o", ARG_OUT },
1954                   { "properties", "a{sv}", ARG_OUT },
1955                   END_ARGS
1956           }
1957         },
1958         { "InterfaceRemoved", WPAS_DBUS_NEW_INTERFACE,
1959           {
1960                   { "path", "o", ARG_OUT },
1961                   END_ARGS
1962           }
1963         },
1964         { "NetworkRequest", WPAS_DBUS_NEW_IFACE_INTERFACE,
1965           {
1966                   { "path", "o", ARG_OUT },
1967                   { "field", "s", ARG_OUT },
1968                   { "text", "s", ARG_OUT },
1969                   END_ARGS
1970           }
1971         },
1972         /* Deprecated: use org.freedesktop.DBus.Properties.PropertiesChanged */
1973         { "PropertiesChanged", WPAS_DBUS_NEW_INTERFACE,
1974           {
1975                   { "properties", "a{sv}", ARG_OUT },
1976                   END_ARGS
1977           }
1978         },
1979         { NULL, NULL, { END_ARGS } }
1980 };
1981
1982
1983 /**
1984  * wpas_dbus_ctrl_iface_init - Initialize dbus control interface
1985  * @global: Pointer to global data from wpa_supplicant_init()
1986  * Returns: 0 on success or -1 on failure
1987  *
1988  * Initialize the dbus control interface for wpa_supplicantand and start
1989  * receiving commands from external programs over the bus.
1990  */
1991 int wpas_dbus_ctrl_iface_init(struct wpas_dbus_priv *priv)
1992 {
1993         struct wpa_dbus_object_desc *obj_desc;
1994         int ret;
1995
1996         obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
1997         if (!obj_desc) {
1998                 wpa_printf(MSG_ERROR, "Not enough memory "
1999                            "to create object description");
2000                 return -1;
2001         }
2002
2003         wpas_dbus_register(obj_desc, priv->global, NULL,
2004                            wpas_dbus_global_methods,
2005                            wpas_dbus_global_properties,
2006                            wpas_dbus_global_signals);
2007
2008         wpa_printf(MSG_DEBUG, "dbus: Register D-Bus object '%s'",
2009                    WPAS_DBUS_NEW_PATH);
2010         ret = wpa_dbus_ctrl_iface_init(priv, WPAS_DBUS_NEW_PATH,
2011                                        WPAS_DBUS_NEW_SERVICE,
2012                                        obj_desc);
2013         if (ret < 0)
2014                 free_dbus_object_desc(obj_desc);
2015         else
2016                 priv->dbus_new_initialized = 1;
2017
2018         return ret;
2019 }
2020
2021
2022 /**
2023  * wpas_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface for
2024  * wpa_supplicant
2025  * @iface: Pointer to dbus private data from wpas_dbus_init()
2026  *
2027  * Deinitialize the dbus control interface that was initialized with
2028  * wpas_dbus_ctrl_iface_init().
2029  */
2030 void wpas_dbus_ctrl_iface_deinit(struct wpas_dbus_priv *iface)
2031 {
2032         if (!iface->dbus_new_initialized)
2033                 return;
2034         wpa_printf(MSG_DEBUG, "dbus: Unregister D-Bus object '%s'",
2035                    WPAS_DBUS_NEW_PATH);
2036         dbus_connection_unregister_object_path(iface->con,
2037                                                WPAS_DBUS_NEW_PATH);
2038 }
2039
2040
2041 static void wpa_dbus_free(void *ptr)
2042 {
2043         os_free(ptr);
2044 }
2045
2046
2047 static const struct wpa_dbus_property_desc wpas_dbus_network_properties[] = {
2048         { "Properties", WPAS_DBUS_NEW_IFACE_NETWORK, "a{sv}",
2049           wpas_dbus_getter_network_properties,
2050           wpas_dbus_setter_network_properties
2051         },
2052         { "Enabled", WPAS_DBUS_NEW_IFACE_NETWORK, "b",
2053           wpas_dbus_getter_enabled,
2054           wpas_dbus_setter_enabled
2055         },
2056         { NULL, NULL, NULL, NULL, NULL }
2057 };
2058
2059
2060 static const struct wpa_dbus_signal_desc wpas_dbus_network_signals[] = {
2061         /* Deprecated: use org.freedesktop.DBus.Properties.PropertiesChanged */
2062         { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_NETWORK,
2063           {
2064                   { "properties", "a{sv}", ARG_OUT },
2065                   END_ARGS
2066           }
2067         },
2068         { NULL, NULL, { END_ARGS } }
2069 };
2070
2071
2072 /**
2073  * wpas_dbus_register_network - Register a configured network with dbus
2074  * @wpa_s: wpa_supplicant interface structure
2075  * @ssid: network configuration data
2076  * Returns: 0 on success, -1 on failure
2077  *
2078  * Registers network representing object with dbus
2079  */
2080 int wpas_dbus_register_network(struct wpa_supplicant *wpa_s,
2081                                struct wpa_ssid *ssid)
2082 {
2083         struct wpas_dbus_priv *ctrl_iface;
2084         struct wpa_dbus_object_desc *obj_desc;
2085         struct network_handler_args *arg;
2086         char net_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
2087
2088 #ifdef CONFIG_P2P
2089         /*
2090          * If it is a persistent group register it as such.
2091          * This is to handle cases where an interface is being initialized
2092          * with a list of networks read from config.
2093          */
2094         if (network_is_persistent_group(ssid))
2095                 return wpas_dbus_register_persistent_group(wpa_s, ssid);
2096 #endif /* CONFIG_P2P */
2097
2098         /* Do nothing if the control interface is not turned on */
2099         if (wpa_s == NULL || wpa_s->global == NULL)
2100                 return 0;
2101         ctrl_iface = wpa_s->global->dbus;
2102         if (ctrl_iface == NULL)
2103                 return 0;
2104
2105         os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
2106                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
2107                     wpa_s->dbus_new_path, ssid->id);
2108
2109         wpa_printf(MSG_DEBUG, "dbus: Register network object '%s'",
2110                    net_obj_path);
2111         obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
2112         if (!obj_desc) {
2113                 wpa_printf(MSG_ERROR, "Not enough memory "
2114                            "to create object description");
2115                 goto err;
2116         }
2117
2118         /* allocate memory for handlers arguments */
2119         arg = os_zalloc(sizeof(struct network_handler_args));
2120         if (!arg) {
2121                 wpa_printf(MSG_ERROR, "Not enough memory "
2122                            "to create arguments for method");
2123                 goto err;
2124         }
2125
2126         arg->wpa_s = wpa_s;
2127         arg->ssid = ssid;
2128
2129         wpas_dbus_register(obj_desc, arg, wpa_dbus_free, NULL,
2130                            wpas_dbus_network_properties,
2131                            wpas_dbus_network_signals);
2132
2133         if (wpa_dbus_register_object_per_iface(ctrl_iface, net_obj_path,
2134                                                wpa_s->ifname, obj_desc))
2135                 goto err;
2136
2137         wpas_dbus_signal_network_added(wpa_s, ssid->id);
2138
2139         return 0;
2140
2141 err:
2142         free_dbus_object_desc(obj_desc);
2143         return -1;
2144 }
2145
2146
2147 /**
2148  * wpas_dbus_unregister_network - Unregister a configured network from dbus
2149  * @wpa_s: wpa_supplicant interface structure
2150  * @nid: network id
2151  * Returns: 0 on success, -1 on failure
2152  *
2153  * Unregisters network representing object from dbus
2154  */
2155 int wpas_dbus_unregister_network(struct wpa_supplicant *wpa_s, int nid)
2156 {
2157         struct wpas_dbus_priv *ctrl_iface;
2158         char net_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
2159         int ret;
2160 #ifdef CONFIG_P2P
2161         struct wpa_ssid *ssid;
2162
2163         ssid = wpa_config_get_network(wpa_s->conf, nid);
2164
2165         /* If it is a persistent group unregister it as such */
2166         if (ssid && network_is_persistent_group(ssid))
2167                 return wpas_dbus_unregister_persistent_group(wpa_s, nid);
2168 #endif /* CONFIG_P2P */
2169
2170         /* Do nothing if the control interface is not turned on */
2171         if (wpa_s->global == NULL || wpa_s->dbus_new_path == NULL)
2172                 return 0;
2173         ctrl_iface = wpa_s->global->dbus;
2174         if (ctrl_iface == NULL)
2175                 return 0;
2176
2177         os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
2178                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
2179                     wpa_s->dbus_new_path, nid);
2180
2181         wpa_printf(MSG_DEBUG, "dbus: Unregister network object '%s'",
2182                    net_obj_path);
2183         ret = wpa_dbus_unregister_object_per_iface(ctrl_iface, net_obj_path);
2184
2185         if (!ret)
2186                 wpas_dbus_signal_network_removed(wpa_s, nid);
2187
2188         return ret;
2189 }
2190
2191
2192 static const struct wpa_dbus_property_desc wpas_dbus_bss_properties[] = {
2193         { "SSID", WPAS_DBUS_NEW_IFACE_BSS, "ay",
2194           wpas_dbus_getter_bss_ssid,
2195           NULL
2196         },
2197         { "BSSID", WPAS_DBUS_NEW_IFACE_BSS, "ay",
2198           wpas_dbus_getter_bss_bssid,
2199           NULL
2200         },
2201         { "Privacy", WPAS_DBUS_NEW_IFACE_BSS, "b",
2202           wpas_dbus_getter_bss_privacy,
2203           NULL
2204         },
2205         { "Mode", WPAS_DBUS_NEW_IFACE_BSS, "s",
2206           wpas_dbus_getter_bss_mode,
2207           NULL
2208         },
2209         { "Signal", WPAS_DBUS_NEW_IFACE_BSS, "n",
2210           wpas_dbus_getter_bss_signal,
2211           NULL
2212         },
2213         { "Frequency", WPAS_DBUS_NEW_IFACE_BSS, "q",
2214           wpas_dbus_getter_bss_frequency,
2215           NULL
2216         },
2217         { "Rates", WPAS_DBUS_NEW_IFACE_BSS, "au",
2218           wpas_dbus_getter_bss_rates,
2219           NULL
2220         },
2221         { "WPA", WPAS_DBUS_NEW_IFACE_BSS, "a{sv}",
2222           wpas_dbus_getter_bss_wpa,
2223           NULL
2224         },
2225         { "RSN", WPAS_DBUS_NEW_IFACE_BSS, "a{sv}",
2226           wpas_dbus_getter_bss_rsn,
2227           NULL
2228         },
2229         { "IEs", WPAS_DBUS_NEW_IFACE_BSS, "ay",
2230           wpas_dbus_getter_bss_ies,
2231           NULL
2232         },
2233         { NULL, NULL, NULL, NULL, NULL }
2234 };
2235
2236
2237 static const struct wpa_dbus_signal_desc wpas_dbus_bss_signals[] = {
2238         /* Deprecated: use org.freedesktop.DBus.Properties.PropertiesChanged */
2239         { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_BSS,
2240           {
2241                   { "properties", "a{sv}", ARG_OUT },
2242                   END_ARGS
2243           }
2244         },
2245         { NULL, NULL, { END_ARGS } }
2246 };
2247
2248
2249 /**
2250  * wpas_dbus_unregister_bss - Unregister a scanned BSS from dbus
2251  * @wpa_s: wpa_supplicant interface structure
2252  * @bssid: scanned network bssid
2253  * @id: unique BSS identifier
2254  * Returns: 0 on success, -1 on failure
2255  *
2256  * Unregisters BSS representing object from dbus
2257  */
2258 int wpas_dbus_unregister_bss(struct wpa_supplicant *wpa_s,
2259                              u8 bssid[ETH_ALEN], unsigned int id)
2260 {
2261         struct wpas_dbus_priv *ctrl_iface;
2262         char bss_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
2263
2264         /* Do nothing if the control interface is not turned on */
2265         if (wpa_s == NULL || wpa_s->global == NULL)
2266                 return 0;
2267         ctrl_iface = wpa_s->global->dbus;
2268         if (ctrl_iface == NULL)
2269                 return 0;
2270
2271         os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
2272                     "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
2273                     wpa_s->dbus_new_path, id);
2274
2275         wpa_printf(MSG_DEBUG, "dbus: Unregister BSS object '%s'",
2276                    bss_obj_path);
2277         if (wpa_dbus_unregister_object_per_iface(ctrl_iface, bss_obj_path)) {
2278                 wpa_printf(MSG_ERROR, "dbus: Cannot unregister BSS object %s",
2279                            bss_obj_path);
2280                 return -1;
2281         }
2282
2283         wpas_dbus_signal_bss_removed(wpa_s, bss_obj_path);
2284         wpas_dbus_signal_prop_changed(wpa_s, WPAS_DBUS_PROP_BSSS);
2285
2286         return 0;
2287 }
2288
2289
2290 /**
2291  * wpas_dbus_register_bss - Register a scanned BSS with dbus
2292  * @wpa_s: wpa_supplicant interface structure
2293  * @bssid: scanned network bssid
2294  * @id: unique BSS identifier
2295  * Returns: 0 on success, -1 on failure
2296  *
2297  * Registers BSS representing object with dbus
2298  */
2299 int wpas_dbus_register_bss(struct wpa_supplicant *wpa_s,
2300                            u8 bssid[ETH_ALEN], unsigned int id)
2301 {
2302         struct wpas_dbus_priv *ctrl_iface;
2303         struct wpa_dbus_object_desc *obj_desc;
2304         char bss_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
2305         struct bss_handler_args *arg;
2306
2307         /* Do nothing if the control interface is not turned on */
2308         if (wpa_s == NULL || wpa_s->global == NULL)
2309                 return 0;
2310         ctrl_iface = wpa_s->global->dbus;
2311         if (ctrl_iface == NULL)
2312                 return 0;
2313
2314         os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
2315                     "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
2316                     wpa_s->dbus_new_path, id);
2317
2318         obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
2319         if (!obj_desc) {
2320                 wpa_printf(MSG_ERROR, "Not enough memory "
2321                            "to create object description");
2322                 goto err;
2323         }
2324
2325         arg = os_zalloc(sizeof(struct bss_handler_args));
2326         if (!arg) {
2327                 wpa_printf(MSG_ERROR, "Not enough memory "
2328                            "to create arguments for handler");
2329                 goto err;
2330         }
2331         arg->wpa_s = wpa_s;
2332         arg->id = id;
2333
2334         wpas_dbus_register(obj_desc, arg, wpa_dbus_free, NULL,
2335                            wpas_dbus_bss_properties,
2336                            wpas_dbus_bss_signals);
2337
2338         wpa_printf(MSG_DEBUG, "dbus: Register BSS object '%s'",
2339                    bss_obj_path);
2340         if (wpa_dbus_register_object_per_iface(ctrl_iface, bss_obj_path,
2341                                                wpa_s->ifname, obj_desc)) {
2342                 wpa_printf(MSG_ERROR,
2343                            "Cannot register BSSID dbus object %s.",
2344                            bss_obj_path);
2345                 goto err;
2346         }
2347
2348         wpas_dbus_signal_bss_added(wpa_s, bss_obj_path);
2349         wpas_dbus_signal_prop_changed(wpa_s, WPAS_DBUS_PROP_BSSS);
2350
2351         return 0;
2352
2353 err:
2354         free_dbus_object_desc(obj_desc);
2355         return -1;
2356 }
2357
2358
2359 static const struct wpa_dbus_method_desc wpas_dbus_interface_methods[] = {
2360         { "Scan", WPAS_DBUS_NEW_IFACE_INTERFACE,
2361           (WPADBusMethodHandler) &wpas_dbus_handler_scan,
2362           {
2363                   { "args", "a{sv}", ARG_IN },
2364                   END_ARGS
2365           }
2366         },
2367         { "Disconnect", WPAS_DBUS_NEW_IFACE_INTERFACE,
2368           (WPADBusMethodHandler) &wpas_dbus_handler_disconnect,
2369           {
2370                   END_ARGS
2371           }
2372         },
2373         { "AddNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE,
2374           (WPADBusMethodHandler) &wpas_dbus_handler_add_network,
2375           {
2376                   { "args", "a{sv}", ARG_IN },
2377                   { "path", "o", ARG_OUT },
2378                   END_ARGS
2379           }
2380         },
2381         { "RemoveNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE,
2382           (WPADBusMethodHandler) &wpas_dbus_handler_remove_network,
2383           {
2384                   { "path", "o", ARG_IN },
2385                   END_ARGS
2386           }
2387         },
2388         { "RemoveAllNetworks", WPAS_DBUS_NEW_IFACE_INTERFACE,
2389           (WPADBusMethodHandler) &wpas_dbus_handler_remove_all_networks,
2390           {
2391                   END_ARGS
2392           }
2393         },
2394         { "SelectNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE,
2395           (WPADBusMethodHandler) &wpas_dbus_handler_select_network,
2396           {
2397                   { "path", "o", ARG_IN },
2398                   END_ARGS
2399           }
2400         },
2401         { "NetworkReply", WPAS_DBUS_NEW_IFACE_INTERFACE,
2402           (WPADBusMethodHandler) &wpas_dbus_handler_network_reply,
2403           {
2404                   { "path", "o", ARG_IN },
2405                   { "field", "s", ARG_IN },
2406                   { "value", "s", ARG_IN },
2407                   END_ARGS
2408           }
2409         },
2410         { "AddBlob", WPAS_DBUS_NEW_IFACE_INTERFACE,
2411           (WPADBusMethodHandler) &wpas_dbus_handler_add_blob,
2412           {
2413                   { "name", "s", ARG_IN },
2414                   { "data", "ay", ARG_IN },
2415                   END_ARGS
2416           }
2417         },
2418         { "GetBlob", WPAS_DBUS_NEW_IFACE_INTERFACE,
2419           (WPADBusMethodHandler) &wpas_dbus_handler_get_blob,
2420           {
2421                   { "name", "s", ARG_IN },
2422                   { "data", "ay", ARG_OUT },
2423                   END_ARGS
2424           }
2425         },
2426         { "RemoveBlob", WPAS_DBUS_NEW_IFACE_INTERFACE,
2427           (WPADBusMethodHandler) &wpas_dbus_handler_remove_blob,
2428           {
2429                   { "name", "s", ARG_IN },
2430                   END_ARGS
2431           }
2432         },
2433 #ifdef CONFIG_WPS
2434         { "Start", WPAS_DBUS_NEW_IFACE_WPS,
2435           (WPADBusMethodHandler) &wpas_dbus_handler_wps_start,
2436           {
2437                   { "args", "a{sv}", ARG_IN },
2438                   { "output", "a{sv}", ARG_OUT },
2439                   END_ARGS
2440           }
2441         },
2442 #endif /* CONFIG_WPS */
2443 #ifdef CONFIG_P2P
2444         { "Find", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2445           (WPADBusMethodHandler)wpas_dbus_handler_p2p_find,
2446           {
2447                   { "args", "a{sv}", ARG_IN },
2448                   END_ARGS
2449           }
2450         },
2451         { "StopFind", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2452           (WPADBusMethodHandler)wpas_dbus_handler_p2p_stop_find,
2453           {
2454                   END_ARGS
2455           }
2456         },
2457         { "Listen", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2458           (WPADBusMethodHandler)wpas_dbus_handler_p2p_listen,
2459           {
2460                   { "timeout", "i", ARG_IN },
2461                   END_ARGS
2462           }
2463         },
2464         { "ExtendedListen", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2465           (WPADBusMethodHandler)wpas_dbus_handler_p2p_extendedlisten,
2466           {
2467                   { "args", "a{sv}", ARG_IN },
2468                   END_ARGS
2469           }
2470         },
2471         { "PresenceRequest", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2472           (WPADBusMethodHandler)wpas_dbus_handler_p2p_presence_request,
2473           {
2474                   { "args", "a{sv}", ARG_IN },
2475                   END_ARGS
2476           }
2477         },
2478         { "ProvisionDiscoveryRequest", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2479           (WPADBusMethodHandler)wpas_dbus_handler_p2p_prov_disc_req,
2480           {
2481                   { "peer", "o", ARG_IN },
2482                   { "config_method", "s", ARG_IN },
2483                   END_ARGS
2484           }
2485         },
2486         { "Connect", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2487           (WPADBusMethodHandler)wpas_dbus_handler_p2p_connect,
2488           {
2489                   { "args", "a{sv}", ARG_IN },
2490                   { "generated_pin", "s", ARG_OUT },
2491                   END_ARGS
2492           }
2493         },
2494         { "GroupAdd", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2495           (WPADBusMethodHandler)wpas_dbus_handler_p2p_group_add,
2496           {
2497                   { "args", "a{sv}", ARG_IN },
2498                   END_ARGS
2499           }
2500         },
2501         { "Invite", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2502           (WPADBusMethodHandler)wpas_dbus_handler_p2p_invite,
2503           {
2504                   { "args", "a{sv}", ARG_IN },
2505                   END_ARGS
2506           }
2507         },
2508         { "Disconnect", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2509           (WPADBusMethodHandler)wpas_dbus_handler_p2p_disconnect,
2510           {
2511                   END_ARGS
2512           }
2513         },
2514         { "RejectPeer", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2515           (WPADBusMethodHandler)wpas_dbus_handler_p2p_rejectpeer,
2516           {
2517                   { "peer", "o", ARG_IN },
2518                   END_ARGS
2519           }
2520         },
2521         { "Flush", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2522           (WPADBusMethodHandler)wpas_dbus_handler_p2p_flush,
2523           {
2524                   END_ARGS
2525           }
2526         },
2527         { "AddService", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2528           (WPADBusMethodHandler)wpas_dbus_handler_p2p_add_service,
2529           {
2530                   { "args", "a{sv}", ARG_IN },
2531                   END_ARGS
2532           }
2533         },
2534         { "DeleteService", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2535           (WPADBusMethodHandler)wpas_dbus_handler_p2p_delete_service,
2536           {
2537                   { "args", "a{sv}", ARG_IN },
2538                   END_ARGS
2539           }
2540         },
2541         { "FlushService", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2542           (WPADBusMethodHandler)wpas_dbus_handler_p2p_flush_service,
2543           {
2544                   END_ARGS
2545           }
2546         },
2547         { "ServiceDiscoveryRequest", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2548           (WPADBusMethodHandler)wpas_dbus_handler_p2p_service_sd_req,
2549           {
2550                   { "args", "a{sv}", ARG_IN },
2551                   END_ARGS
2552           }
2553         },
2554         { "ServiceDiscoveryResponse", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2555           (WPADBusMethodHandler)wpas_dbus_handler_p2p_service_sd_res,
2556           {
2557                   { "args", "a{sv}", ARG_IN },
2558                   END_ARGS
2559           }
2560         },
2561         { "ServiceDiscoveryCancelRequest", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2562           (WPADBusMethodHandler)wpas_dbus_handler_p2p_service_sd_cancel_req,
2563           {
2564                   { "args", "t", ARG_IN },
2565                   END_ARGS
2566           }
2567         },
2568         { "ServiceUpdate", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2569           (WPADBusMethodHandler)wpas_dbus_handler_p2p_service_update,
2570           {
2571                   END_ARGS
2572           }
2573         },
2574         { "ServiceDiscoveryExternal", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2575           (WPADBusMethodHandler)wpas_dbus_handler_p2p_serv_disc_external,
2576           {
2577                   { "arg", "i", ARG_IN },
2578                   END_ARGS
2579           }
2580         },
2581         { "ServiceDiscoveryExternal", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2582           (WPADBusMethodHandler)wpas_dbus_handler_p2p_serv_disc_external,
2583           {
2584                   { "arg", "i", ARG_IN },
2585                   END_ARGS
2586           }
2587         },
2588         { "AddPersistentGroup", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2589           (WPADBusMethodHandler) wpas_dbus_handler_add_persistent_group,
2590           {
2591                   { "args", "a{sv}", ARG_IN },
2592                   { "path", "o", ARG_OUT },
2593                   END_ARGS
2594           }
2595         },
2596         { "RemovePersistentGroup", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2597           (WPADBusMethodHandler) wpas_dbus_handler_remove_persistent_group,
2598           {
2599                   { "path", "o", ARG_IN },
2600                   END_ARGS
2601           }
2602         },
2603         { "RemoveAllPersistentGroups", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2604           (WPADBusMethodHandler)
2605           wpas_dbus_handler_remove_all_persistent_groups,
2606           {
2607                   END_ARGS
2608           }
2609         },
2610 #endif /* CONFIG_P2P */
2611         { "FlushBSS", WPAS_DBUS_NEW_IFACE_INTERFACE,
2612           (WPADBusMethodHandler) &wpas_dbus_handler_flush_bss,
2613           {
2614                   { "age", "u", ARG_IN },
2615                   END_ARGS
2616           }
2617         },
2618 #ifdef CONFIG_AP
2619         { "SubscribeProbeReq", WPAS_DBUS_NEW_IFACE_INTERFACE,
2620           (WPADBusMethodHandler) wpas_dbus_handler_subscribe_preq,
2621           {
2622                   END_ARGS
2623           }
2624         },
2625         { "UnsubscribeProbeReq", WPAS_DBUS_NEW_IFACE_INTERFACE,
2626           (WPADBusMethodHandler) wpas_dbus_handler_unsubscribe_preq,
2627           {
2628                   END_ARGS
2629           }
2630         },
2631 #endif /* CONFIG_AP */
2632         { NULL, NULL, NULL, { END_ARGS } }
2633 };
2634
2635 static const struct wpa_dbus_property_desc wpas_dbus_interface_properties[] = {
2636         { "Capabilities", WPAS_DBUS_NEW_IFACE_INTERFACE, "a{sv}",
2637           wpas_dbus_getter_capabilities,
2638           NULL
2639         },
2640         { "State", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
2641           wpas_dbus_getter_state,
2642           NULL
2643         },
2644         { "Scanning", WPAS_DBUS_NEW_IFACE_INTERFACE, "b",
2645           wpas_dbus_getter_scanning,
2646           NULL
2647         },
2648         { "ApScan", WPAS_DBUS_NEW_IFACE_INTERFACE, "u",
2649           wpas_dbus_getter_ap_scan,
2650           wpas_dbus_setter_ap_scan
2651         },
2652         { "BSSExpireAge", WPAS_DBUS_NEW_IFACE_INTERFACE, "u",
2653           wpas_dbus_getter_bss_expire_age,
2654           wpas_dbus_setter_bss_expire_age
2655         },
2656         { "BSSExpireCount", WPAS_DBUS_NEW_IFACE_INTERFACE, "u",
2657           wpas_dbus_getter_bss_expire_count,
2658           wpas_dbus_setter_bss_expire_count
2659         },
2660         { "Country", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
2661           wpas_dbus_getter_country,
2662           wpas_dbus_setter_country
2663         },
2664         { "Ifname", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
2665           wpas_dbus_getter_ifname,
2666           NULL
2667         },
2668         { "Driver", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
2669           wpas_dbus_getter_driver,
2670           NULL
2671         },
2672         { "BridgeIfname", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
2673           wpas_dbus_getter_bridge_ifname,
2674           NULL
2675         },
2676         { "CurrentBSS", WPAS_DBUS_NEW_IFACE_INTERFACE, "o",
2677           wpas_dbus_getter_current_bss,
2678           NULL
2679         },
2680         { "CurrentNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE, "o",
2681           wpas_dbus_getter_current_network,
2682           NULL
2683         },
2684         { "CurrentAuthMode", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
2685           wpas_dbus_getter_current_auth_mode,
2686           NULL
2687         },
2688         { "Blobs", WPAS_DBUS_NEW_IFACE_INTERFACE, "a{say}",
2689           wpas_dbus_getter_blobs,
2690           NULL
2691         },
2692         { "BSSs", WPAS_DBUS_NEW_IFACE_INTERFACE, "ao",
2693           wpas_dbus_getter_bsss,
2694           NULL
2695         },
2696         { "Networks", WPAS_DBUS_NEW_IFACE_INTERFACE, "ao",
2697           wpas_dbus_getter_networks,
2698           NULL
2699         },
2700         { "FastReauth", WPAS_DBUS_NEW_IFACE_INTERFACE, "b",
2701           wpas_dbus_getter_fast_reauth,
2702           wpas_dbus_setter_fast_reauth
2703         },
2704         { "ScanInterval", WPAS_DBUS_NEW_IFACE_INTERFACE, "i",
2705           wpas_dbus_getter_scan_interval,
2706           wpas_dbus_setter_scan_interval
2707         },
2708 #ifdef CONFIG_WPS
2709         { "ProcessCredentials", WPAS_DBUS_NEW_IFACE_WPS, "b",
2710           wpas_dbus_getter_process_credentials,
2711           wpas_dbus_setter_process_credentials
2712         },
2713 #endif /* CONFIG_WPS */
2714 #ifdef CONFIG_P2P
2715         { "P2PDeviceConfig", WPAS_DBUS_NEW_IFACE_P2PDEVICE, "a{sv}",
2716           wpas_dbus_getter_p2p_device_config,
2717           wpas_dbus_setter_p2p_device_config
2718         },
2719         { "Peers", WPAS_DBUS_NEW_IFACE_P2PDEVICE, "ao",
2720           wpas_dbus_getter_p2p_peers,
2721           NULL
2722         },
2723         { "Role", WPAS_DBUS_NEW_IFACE_P2PDEVICE, "s",
2724           wpas_dbus_getter_p2p_role,
2725           NULL
2726         },
2727         { "Group", WPAS_DBUS_NEW_IFACE_P2PDEVICE, "o",
2728           wpas_dbus_getter_p2p_group,
2729           NULL
2730         },
2731         { "PeerGO", WPAS_DBUS_NEW_IFACE_P2PDEVICE, "o",
2732           wpas_dbus_getter_p2p_peergo,
2733           NULL
2734         },
2735         { "PersistentGroups", WPAS_DBUS_NEW_IFACE_P2PDEVICE, "ao",
2736           wpas_dbus_getter_persistent_groups,
2737           NULL
2738         },
2739 #endif /* CONFIG_P2P */
2740         { NULL, NULL, NULL, NULL, NULL }
2741 };
2742
2743 static const struct wpa_dbus_signal_desc wpas_dbus_interface_signals[] = {
2744         { "ScanDone", WPAS_DBUS_NEW_IFACE_INTERFACE,
2745           {
2746                   { "success", "b", ARG_OUT },
2747                   END_ARGS
2748           }
2749         },
2750         { "BSSAdded", WPAS_DBUS_NEW_IFACE_INTERFACE,
2751           {
2752                   { "path", "o", ARG_OUT },
2753                   { "properties", "a{sv}", ARG_OUT },
2754                   END_ARGS
2755           }
2756         },
2757         { "BSSRemoved", WPAS_DBUS_NEW_IFACE_INTERFACE,
2758           {
2759                   { "path", "o", ARG_OUT },
2760                   END_ARGS
2761           }
2762         },
2763         { "BlobAdded", WPAS_DBUS_NEW_IFACE_INTERFACE,
2764           {
2765                   { "name", "s", ARG_OUT },
2766                   END_ARGS
2767           }
2768         },
2769         { "BlobRemoved", WPAS_DBUS_NEW_IFACE_INTERFACE,
2770           {
2771                   { "name", "s", ARG_OUT },
2772                   END_ARGS
2773           }
2774         },
2775         { "NetworkAdded", WPAS_DBUS_NEW_IFACE_INTERFACE,
2776           {
2777                   { "path", "o", ARG_OUT },
2778                   { "properties", "a{sv}", ARG_OUT },
2779                   END_ARGS
2780           }
2781         },
2782         { "NetworkRemoved", WPAS_DBUS_NEW_IFACE_INTERFACE,
2783           {
2784                   { "path", "o", ARG_OUT },
2785                   END_ARGS
2786           }
2787         },
2788         { "NetworkSelected", WPAS_DBUS_NEW_IFACE_INTERFACE,
2789           {
2790                   { "path", "o", ARG_OUT },
2791                   END_ARGS
2792           }
2793         },
2794         /* Deprecated: use org.freedesktop.DBus.Properties.PropertiesChanged */
2795         { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_INTERFACE,
2796           {
2797                   { "properties", "a{sv}", ARG_OUT },
2798                   END_ARGS
2799           }
2800         },
2801 #ifdef CONFIG_WPS
2802         { "Event", WPAS_DBUS_NEW_IFACE_WPS,
2803           {
2804                   { "name", "s", ARG_OUT },
2805                   { "args", "a{sv}", ARG_OUT },
2806                   END_ARGS
2807           }
2808         },
2809         { "Credentials", WPAS_DBUS_NEW_IFACE_WPS,
2810           {
2811                   { "credentials", "a{sv}", ARG_OUT },
2812                   END_ARGS
2813           }
2814         },
2815         /* Deprecated: use org.freedesktop.DBus.Properties.PropertiesChanged */
2816         { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_WPS,
2817           {
2818                   { "properties", "a{sv}", ARG_OUT },
2819                   END_ARGS
2820           }
2821         },
2822 #endif /* CONFIG_WPS */
2823 #ifdef CONFIG_P2P
2824         { "P2PStateChanged", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2825           {
2826                   { "states", "a{ss}", ARG_OUT },
2827                   END_ARGS
2828           }
2829         },
2830         { "DeviceFound", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2831           {
2832                   { "path", "o", ARG_OUT },
2833                   { "properties", "a{sv}", ARG_OUT },
2834                   END_ARGS
2835           }
2836         },
2837         { "DeviceLost", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2838           {
2839                   { "path", "o", ARG_OUT },
2840                   END_ARGS
2841           }
2842         },
2843         { "ProvisionDiscoveryRequestDisplayPin", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2844           {
2845                   { "peer_object", "o", ARG_OUT },
2846                   { "pin", "s", ARG_OUT },
2847                   END_ARGS
2848           }
2849         },
2850         { "ProvisionDiscoveryResponseDisplayPin", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2851           {
2852                   { "peer_object", "o", ARG_OUT },
2853                   { "pin", "s", ARG_OUT },
2854                   END_ARGS
2855           }
2856         },
2857         { "ProvisionDiscoveryRequestEnterPin", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2858           {
2859                   { "peer_object", "o", ARG_OUT },
2860                   END_ARGS
2861           }
2862         },
2863         { "ProvisionDiscoveryResponseEnterPin", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2864           {
2865                   { "peer_object", "o", ARG_OUT },
2866                   END_ARGS
2867           }
2868         },
2869         { "ProvisionDiscoveryPBCRequest", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2870           {
2871                   { "peer_object", "o", ARG_OUT },
2872                   END_ARGS
2873           }
2874         },
2875         { "ProvisionDiscoveryPBCResponse", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2876           {
2877                   { "peer_object", "o", ARG_OUT },
2878                   END_ARGS
2879           }
2880         },
2881         { "ProvisionDiscoveryFailure", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2882           {
2883                   { "peer_object", "o", ARG_OUT },
2884                   { "status", "i", ARG_OUT },
2885                   END_ARGS
2886           }
2887         },
2888         { "GroupStarted", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2889           {
2890                   { "properties", "a{sv}", ARG_OUT },
2891                   END_ARGS
2892           }
2893         },
2894         { "GONegotiationSuccess", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2895           {
2896                   END_ARGS
2897           }
2898         },
2899         { "GONegotiationFailure", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2900           {
2901                   { "status", "i", ARG_OUT },
2902                   END_ARGS
2903           }
2904         },
2905         { "GONegotiationRequest", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2906           {
2907                   { "path", "o", ARG_OUT },
2908                   { "dev_passwd_id", "i", ARG_OUT },
2909                   END_ARGS
2910           }
2911         },
2912         { "InvitationResult", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2913           {
2914                   { "invite_result", "a{sv}", ARG_OUT },
2915                   END_ARGS
2916           }
2917         },
2918         { "GroupFinished", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2919           {
2920                   { "ifname", "s", ARG_OUT },
2921                   { "role", "s", ARG_OUT },
2922                   END_ARGS
2923           }
2924         },
2925         { "ServiceDiscoveryRequest", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2926           {
2927                   { "sd_request", "a{sv}", ARG_OUT },
2928                   END_ARGS
2929           }
2930         },
2931         { "ServiceDiscoveryResponse", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2932           {
2933                   { "sd_response", "a{sv}", ARG_OUT },
2934                   END_ARGS
2935           }
2936         },
2937         { "PersistentGroupAdded", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2938           {
2939                   { "path", "o", ARG_OUT },
2940                   { "properties", "a{sv}", ARG_OUT },
2941                   END_ARGS
2942           }
2943         },
2944         { "PersistentGroupRemoved", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2945           {
2946                   { "path", "o", ARG_OUT },
2947                   END_ARGS
2948           }
2949         },
2950         { "WpsFailed", WPAS_DBUS_NEW_IFACE_P2PDEVICE,
2951           {
2952                   { "name", "s", ARG_OUT },
2953                   { "args", "a{sv}", ARG_OUT },
2954                   END_ARGS
2955           }
2956         },
2957 #endif /* CONFIG_P2P */
2958 #ifdef CONFIG_AP
2959         { "ProbeRequest", WPAS_DBUS_NEW_IFACE_INTERFACE,
2960           {
2961                   { "args", "a{sv}", ARG_OUT },
2962                   END_ARGS
2963           }
2964         },
2965 #endif /* CONFIG_AP */
2966         { "Certification", WPAS_DBUS_NEW_IFACE_INTERFACE,
2967           {
2968                   { "certification", "a{sv}", ARG_OUT },
2969                   END_ARGS
2970           }
2971         },
2972         { "EAP", WPAS_DBUS_NEW_IFACE_INTERFACE,
2973           {
2974                   { "status", "s", ARG_OUT },
2975                   { "parameter", "s", ARG_OUT },
2976                   END_ARGS
2977           }
2978         },
2979         { NULL, NULL, { END_ARGS } }
2980 };
2981
2982
2983 int wpas_dbus_register_interface(struct wpa_supplicant *wpa_s)
2984 {
2985
2986         struct wpa_dbus_object_desc *obj_desc = NULL;
2987         struct wpas_dbus_priv *ctrl_iface = wpa_s->global->dbus;
2988         int next;
2989
2990         /* Do nothing if the control interface is not turned on */
2991         if (ctrl_iface == NULL)
2992                 return 0;
2993
2994         /* Create and set the interface's object path */
2995         wpa_s->dbus_new_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
2996         if (wpa_s->dbus_new_path == NULL)
2997                 return -1;
2998         next = ctrl_iface->next_objid++;
2999         os_snprintf(wpa_s->dbus_new_path, WPAS_DBUS_OBJECT_PATH_MAX,
3000                     WPAS_DBUS_NEW_PATH_INTERFACES "/%u",
3001                     next);
3002
3003         obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
3004         if (!obj_desc) {
3005                 wpa_printf(MSG_ERROR, "Not enough memory "
3006                            "to create object description");
3007                 goto err;
3008         }
3009
3010         wpas_dbus_register(obj_desc, wpa_s, NULL, wpas_dbus_interface_methods,
3011                            wpas_dbus_interface_properties,
3012                            wpas_dbus_interface_signals);
3013
3014         wpa_printf(MSG_DEBUG, "dbus: Register interface object '%s'",
3015                    wpa_s->dbus_new_path);
3016         if (wpa_dbus_register_object_per_iface(ctrl_iface,
3017                                                wpa_s->dbus_new_path,
3018                                                wpa_s->ifname, obj_desc))
3019                 goto err;
3020
3021         wpas_dbus_signal_interface_added(wpa_s);
3022
3023         return 0;
3024
3025 err:
3026         os_free(wpa_s->dbus_new_path);
3027         wpa_s->dbus_new_path = NULL;
3028         free_dbus_object_desc(obj_desc);
3029         return -1;
3030 }
3031
3032
3033 int wpas_dbus_unregister_interface(struct wpa_supplicant *wpa_s)
3034 {
3035         struct wpas_dbus_priv *ctrl_iface;
3036
3037         /* Do nothing if the control interface is not turned on */
3038         if (wpa_s == NULL || wpa_s->global == NULL)
3039                 return 0;
3040         ctrl_iface = wpa_s->global->dbus;
3041         if (ctrl_iface == NULL)
3042                 return 0;
3043
3044         wpa_printf(MSG_DEBUG, "dbus: Unregister interface object '%s'",
3045                    wpa_s->dbus_new_path);
3046
3047 #ifdef CONFIG_AP
3048         if (wpa_s->preq_notify_peer) {
3049                 wpas_dbus_unsubscribe_noc(ctrl_iface);
3050                 os_free(wpa_s->preq_notify_peer);
3051                 wpa_s->preq_notify_peer = NULL;
3052         }
3053 #endif /* CONFIG_AP */
3054
3055         if (wpa_dbus_unregister_object_per_iface(ctrl_iface,
3056                                                  wpa_s->dbus_new_path))
3057                 return -1;
3058
3059         wpas_dbus_signal_interface_removed(wpa_s);
3060
3061         os_free(wpa_s->dbus_new_path);
3062         wpa_s->dbus_new_path = NULL;
3063
3064         return 0;
3065 }
3066
3067 #ifdef CONFIG_P2P
3068
3069 static const struct wpa_dbus_property_desc wpas_dbus_p2p_peer_properties[] = {
3070         { "DeviceName", WPAS_DBUS_NEW_IFACE_P2P_PEER, "s",
3071           wpas_dbus_getter_p2p_peer_device_name,
3072           NULL
3073         },
3074         { "PrimaryDeviceType", WPAS_DBUS_NEW_IFACE_P2P_PEER, "ay",
3075           wpas_dbus_getter_p2p_peer_primary_device_type,
3076           NULL
3077         },
3078         { "config_method", WPAS_DBUS_NEW_IFACE_P2P_PEER, "q",
3079           wpas_dbus_getter_p2p_peer_config_method,
3080           NULL
3081         },
3082         { "level", WPAS_DBUS_NEW_IFACE_P2P_PEER, "i",
3083           wpas_dbus_getter_p2p_peer_level,
3084           NULL
3085         },
3086         { "devicecapability", WPAS_DBUS_NEW_IFACE_P2P_PEER, "y",
3087           wpas_dbus_getter_p2p_peer_device_capability,
3088           NULL
3089         },
3090         { "groupcapability", WPAS_DBUS_NEW_IFACE_P2P_PEER, "y",
3091           wpas_dbus_getter_p2p_peer_group_capability,
3092           NULL
3093         },
3094         { "SecondaryDeviceTypes", WPAS_DBUS_NEW_IFACE_P2P_PEER, "aay",
3095           wpas_dbus_getter_p2p_peer_secondary_device_types,
3096           NULL
3097         },
3098         { "VendorExtension", WPAS_DBUS_NEW_IFACE_P2P_PEER, "aay",
3099           wpas_dbus_getter_p2p_peer_vendor_extension,
3100           NULL
3101         },
3102         { "IEs", WPAS_DBUS_NEW_IFACE_P2P_PEER, "ay",
3103           wpas_dbus_getter_p2p_peer_ies,
3104           NULL
3105         },
3106         { NULL, NULL, NULL, NULL, NULL }
3107 };
3108
3109 static const struct wpa_dbus_signal_desc wpas_dbus_p2p_peer_signals[] = {
3110
3111         { NULL, NULL, { END_ARGS } }
3112 };
3113
3114 /**
3115  * wpas_dbus_signal_peer - Send a peer related event signal
3116  * @wpa_s: %wpa_supplicant network interface data
3117  * @dev: peer device object
3118  * @interface: name of the interface emitting this signal.
3119  *      In case of peer objects, it would be emitted by either
3120  *      the "interface object" or by "peer objects"
3121  * @sig_name: signal name - DeviceFound
3122  *
3123  * Notify listeners about event related with newly found p2p peer device
3124  */
3125 static void wpas_dbus_signal_peer(struct wpa_supplicant *wpa_s,
3126                                   const u8 *dev_addr, const char *interface,
3127                                   const char *sig_name)
3128 {
3129         struct wpas_dbus_priv *iface;
3130         DBusMessage *msg;
3131         DBusMessageIter iter;
3132         char peer_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
3133
3134         iface = wpa_s->global->dbus;
3135
3136         /* Do nothing if the control interface is not turned on */
3137         if (iface == NULL)
3138                 return;
3139
3140         os_snprintf(peer_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
3141                     "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART "/" COMPACT_MACSTR,
3142                     wpa_s->dbus_new_path, MAC2STR(dev_addr));
3143
3144         msg = dbus_message_new_signal(wpa_s->dbus_new_path, interface,
3145                                       sig_name);
3146         if (msg == NULL)
3147                 return;
3148
3149         dbus_message_iter_init_append(msg, &iter);
3150         path = peer_obj_path;
3151         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
3152                                             &path))
3153                 goto err;
3154
3155         dbus_connection_send(iface->con, msg, NULL);
3156
3157         dbus_message_unref(msg);
3158         return;
3159
3160 err:
3161         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
3162         dbus_message_unref(msg);
3163 }
3164
3165
3166 /**
3167  * wpas_dbus_signal_peer_found - Send a peer found signal
3168  * @wpa_s: %wpa_supplicant network interface data
3169  * @dev: peer device object
3170  *
3171  * Notify listeners about find a p2p peer device found
3172  */
3173 void wpas_dbus_signal_peer_device_found(struct wpa_supplicant *wpa_s,
3174                                         const u8 *dev_addr)
3175 {
3176         wpas_dbus_signal_peer(wpa_s, dev_addr,
3177                               WPAS_DBUS_NEW_IFACE_P2PDEVICE,
3178                               "DeviceFound");
3179 }
3180
3181 /**
3182  * wpas_dbus_signal_peer_lost - Send a peer lost signal
3183  * @wpa_s: %wpa_supplicant network interface data
3184  * @dev: peer device object
3185  *
3186  * Notify listeners about lost a p2p peer device
3187  */
3188 void wpas_dbus_signal_peer_device_lost(struct wpa_supplicant *wpa_s,
3189                                        const u8 *dev_addr)
3190 {
3191         wpas_dbus_signal_peer(wpa_s, dev_addr,
3192                               WPAS_DBUS_NEW_IFACE_P2PDEVICE,
3193                               "DeviceLost");
3194 }
3195
3196 /**
3197  * wpas_dbus_register_peer - Register a discovered peer object with dbus
3198  * @wpa_s: wpa_supplicant interface structure
3199  * @ssid: network configuration data
3200  * Returns: 0 on success, -1 on failure
3201  *
3202  * Registers network representing object with dbus
3203  */
3204 int wpas_dbus_register_peer(struct wpa_supplicant *wpa_s, const u8 *dev_addr)
3205 {
3206         struct wpas_dbus_priv *ctrl_iface;
3207         struct wpa_dbus_object_desc *obj_desc;
3208         struct peer_handler_args *arg;
3209         char peer_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
3210
3211         /* Do nothing if the control interface is not turned on */
3212         if (wpa_s == NULL || wpa_s->global == NULL)
3213                 return 0;
3214
3215         ctrl_iface = wpa_s->global->dbus;
3216         if (ctrl_iface == NULL)
3217                 return 0;
3218
3219         os_snprintf(peer_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
3220                     "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART "/" COMPACT_MACSTR,
3221                     wpa_s->dbus_new_path, MAC2STR(dev_addr));
3222
3223         wpa_printf(MSG_INFO, "dbus: Register peer object '%s'",
3224                    peer_obj_path);
3225         obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
3226         if (!obj_desc) {
3227                 wpa_printf(MSG_ERROR, "Not enough memory "
3228                            "to create object description");
3229                 goto err;
3230         }
3231
3232         /* allocate memory for handlers arguments */
3233         arg = os_zalloc(sizeof(struct peer_handler_args));
3234         if (!arg) {
3235                 wpa_printf(MSG_ERROR, "Not enough memory "
3236                            "to create arguments for method");
3237                 goto err;
3238         }
3239
3240         arg->wpa_s = wpa_s;
3241         os_memcpy(arg->p2p_device_addr, dev_addr, ETH_ALEN);
3242
3243         wpas_dbus_register(obj_desc, arg, wpa_dbus_free,
3244                            NULL,
3245                            wpas_dbus_p2p_peer_properties,
3246                            wpas_dbus_p2p_peer_signals);
3247
3248         if (wpa_dbus_register_object_per_iface(ctrl_iface, peer_obj_path,
3249                                                wpa_s->ifname, obj_desc))
3250                 goto err;
3251
3252         return 0;
3253
3254 err:
3255         free_dbus_object_desc(obj_desc);
3256         return -1;
3257 }
3258
3259 /**
3260  * wpas_dbus_unregister_peer - Unregister a peer object with dbus
3261  * @wpa_s: wpa_supplicant interface structure
3262  * @dev_addr: p2p device addr
3263  * Returns: 0 on success, -1 on failure
3264  *
3265  * Registers network representing object with dbus
3266  */
3267 int wpas_dbus_unregister_peer(struct wpa_supplicant *wpa_s,
3268                                   const u8 *dev_addr)
3269 {
3270         struct wpas_dbus_priv *ctrl_iface;
3271         char peer_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
3272         int ret;
3273
3274         /* Do nothing if the control interface is not turned on */
3275         if (wpa_s == NULL || wpa_s->global == NULL ||
3276             wpa_s->dbus_new_path == NULL)
3277                 return 0;
3278         ctrl_iface = wpa_s->global->dbus;
3279         if (ctrl_iface == NULL)
3280                 return 0;
3281
3282         os_snprintf(peer_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
3283                     "%s/" WPAS_DBUS_NEW_P2P_PEERS_PART "/" COMPACT_MACSTR,
3284                     wpa_s->dbus_new_path, MAC2STR(dev_addr));
3285
3286         wpa_printf(MSG_INFO, "dbus: Unregister peer object '%s'",
3287                    peer_obj_path);
3288         ret = wpa_dbus_unregister_object_per_iface(ctrl_iface, peer_obj_path);
3289
3290         return ret;
3291 }
3292
3293
3294 static const struct wpa_dbus_property_desc wpas_dbus_p2p_group_properties[] = {
3295         { "Members", WPAS_DBUS_NEW_IFACE_P2P_GROUP, "ao",
3296           wpas_dbus_getter_p2p_group_members,
3297           NULL
3298         },
3299         { "Group", WPAS_DBUS_NEW_IFACE_P2P_GROUP, "o",
3300           wpas_dbus_getter_p2p_group,
3301           NULL
3302         },
3303         { "Role", WPAS_DBUS_NEW_IFACE_P2P_GROUP, "s",
3304           wpas_dbus_getter_p2p_role,
3305           NULL
3306         },
3307         { "SSID", WPAS_DBUS_NEW_IFACE_P2P_GROUP, "ay",
3308           wpas_dbus_getter_p2p_group_ssid,
3309           NULL
3310         },
3311         { "BSSID", WPAS_DBUS_NEW_IFACE_P2P_GROUP, "ay",
3312           wpas_dbus_getter_p2p_group_bssid,
3313           NULL
3314         },
3315         { "Frequency", WPAS_DBUS_NEW_IFACE_P2P_GROUP, "q",
3316           wpas_dbus_getter_p2p_group_frequency,
3317           NULL
3318         },
3319         { "Passphrase", WPAS_DBUS_NEW_IFACE_P2P_GROUP, "s",
3320           wpas_dbus_getter_p2p_group_passphrase,
3321           NULL
3322         },
3323         { "PSK", WPAS_DBUS_NEW_IFACE_P2P_GROUP, "ay",
3324           wpas_dbus_getter_p2p_group_psk,
3325           NULL
3326         },
3327         { "WPSVendorExtensions", WPAS_DBUS_NEW_IFACE_P2P_GROUP, "aay",
3328           wpas_dbus_getter_p2p_group_vendor_ext,
3329           wpas_dbus_setter_p2p_group_vendor_ext
3330         },
3331         { NULL, NULL, NULL, NULL, NULL }
3332 };
3333
3334 static const struct wpa_dbus_signal_desc wpas_dbus_p2p_group_signals[] = {
3335         { "PeerJoined", WPAS_DBUS_NEW_IFACE_P2P_GROUP,
3336           {
3337                   { "peer", "o", ARG_OUT },
3338                   END_ARGS
3339           }
3340         },
3341         { "PeerDisconnected", WPAS_DBUS_NEW_IFACE_P2P_GROUP,
3342           {
3343                   { "peer", "o", ARG_OUT },
3344                   END_ARGS
3345           }
3346         },
3347         { NULL, NULL, { END_ARGS } }
3348 };
3349
3350 /**
3351  * wpas_dbus_register_p2p_group - Register a p2p group object with dbus
3352  * @wpa_s: wpa_supplicant interface structure
3353  * @ssid: SSID struct
3354  * Returns: 0 on success, -1 on failure
3355  *
3356  * Registers p2p group representing object with dbus
3357  */
3358 void wpas_dbus_register_p2p_group(struct wpa_supplicant *wpa_s,
3359                                   struct wpa_ssid *ssid)
3360 {
3361         struct wpas_dbus_priv *ctrl_iface;
3362         struct wpa_dbus_object_desc *obj_desc;
3363         char group_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
3364
3365         /* Do nothing if the control interface is not turned on */
3366         if (wpa_s == NULL || wpa_s->global == NULL)
3367                 return;
3368
3369         ctrl_iface = wpa_s->global->dbus;
3370         if (ctrl_iface == NULL)
3371                 return;
3372
3373         if (wpa_s->dbus_groupobj_path) {
3374                 wpa_printf(MSG_INFO, "%s: Group object '%s' already exists",
3375                            __func__, wpa_s->dbus_groupobj_path);
3376                 return;
3377         }
3378
3379         if (wpas_dbus_get_group_obj_path(wpa_s, ssid, group_obj_path) < 0)
3380                 return;
3381
3382         wpa_s->dbus_groupobj_path = os_strdup(group_obj_path);
3383         if (wpa_s->dbus_groupobj_path == NULL)
3384                 return;
3385
3386         wpa_printf(MSG_INFO, "dbus: Register group object '%s'",
3387                    group_obj_path);
3388         obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
3389         if (!obj_desc) {
3390                 wpa_printf(MSG_ERROR, "Not enough memory "
3391                            "to create object description");
3392                 goto err;
3393         }
3394
3395         wpas_dbus_register(obj_desc, wpa_s, NULL, NULL,
3396                            wpas_dbus_p2p_group_properties,
3397                            wpas_dbus_p2p_group_signals);
3398
3399         if (wpa_dbus_register_object_per_iface(ctrl_iface, group_obj_path,
3400                                                wpa_s->ifname, obj_desc))
3401                 goto err;
3402
3403         return;
3404
3405 err:
3406         if (wpa_s->dbus_groupobj_path) {
3407                 os_free(wpa_s->dbus_groupobj_path);
3408                 wpa_s->dbus_groupobj_path = NULL;
3409         }
3410
3411         free_dbus_object_desc(obj_desc);
3412 }
3413
3414 /**
3415  * wpas_dbus_unregister_p2p_group - Unregister a p2p group object from dbus
3416  * @wpa_s: wpa_supplicant interface structure
3417  * @ssid: network name of the p2p group started
3418  */
3419 void wpas_dbus_unregister_p2p_group(struct wpa_supplicant *wpa_s,
3420                                     const struct wpa_ssid *ssid)
3421 {
3422         struct wpas_dbus_priv *ctrl_iface;
3423
3424         /* Do nothing if the control interface is not turned on */
3425         if (wpa_s == NULL || wpa_s->global == NULL)
3426                 return;
3427
3428         ctrl_iface = wpa_s->global->dbus;
3429         if (ctrl_iface == NULL)
3430                 return;
3431
3432         if (!wpa_s->dbus_groupobj_path) {
3433                 wpa_printf(MSG_DEBUG,
3434                            "%s: Group object '%s' already unregistered",
3435                            __func__, wpa_s->dbus_groupobj_path);
3436                 return;
3437         }
3438
3439         wpa_printf(MSG_DEBUG, "dbus: Unregister group object '%s'",
3440                    wpa_s->dbus_groupobj_path);
3441
3442         wpa_dbus_unregister_object_per_iface(ctrl_iface,
3443                                              wpa_s->dbus_groupobj_path);
3444
3445         os_free(wpa_s->dbus_groupobj_path);
3446         wpa_s->dbus_groupobj_path = NULL;
3447 }
3448
3449 static const struct wpa_dbus_property_desc
3450 wpas_dbus_p2p_groupmember_properties[] = {
3451         { NULL, NULL, NULL, NULL, NULL }
3452 };
3453
3454 /**
3455  * wpas_dbus_register_p2p_groupmember - Register a p2p groupmember
3456  * object with dbus
3457  * @wpa_s: wpa_supplicant interface structure
3458  * @p2p_if_addr: i/f addr of the device joining this group
3459  *
3460  * Registers p2p groupmember representing object with dbus
3461  */
3462 void wpas_dbus_register_p2p_groupmember(struct wpa_supplicant *wpa_s,
3463                                         const u8 *p2p_if_addr)
3464 {
3465         struct wpas_dbus_priv *ctrl_iface;
3466         struct wpa_dbus_object_desc *obj_desc = NULL;
3467         struct groupmember_handler_args *arg;
3468         char groupmember_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
3469
3470         /* Do nothing if the control interface is not turned on */
3471         if (wpa_s == NULL || wpa_s->global == NULL)
3472                 return;
3473
3474         ctrl_iface = wpa_s->global->dbus;
3475         if (ctrl_iface == NULL)
3476                 return;
3477
3478         if (!wpa_s->dbus_groupobj_path)
3479                 return;
3480
3481         os_snprintf(groupmember_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
3482                 "%s/" WPAS_DBUS_NEW_P2P_GROUPMEMBERS_PART "/" COMPACT_MACSTR,
3483                 wpa_s->dbus_groupobj_path, MAC2STR(p2p_if_addr));
3484
3485         obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
3486         if (!obj_desc) {
3487                 wpa_printf(MSG_ERROR, "Not enough memory "
3488                            "to create object description");
3489                 goto err;
3490         }
3491
3492         /* allocate memory for handlers arguments */
3493         arg = os_zalloc(sizeof(struct groupmember_handler_args));
3494         if (!arg) {
3495                 wpa_printf(MSG_ERROR, "Not enough memory "
3496                            "to create arguments for method");
3497                 goto err;
3498         }
3499
3500         arg->wpa_s = wpa_s;
3501         os_memcpy(arg->member_addr, p2p_if_addr, ETH_ALEN);
3502
3503         wpas_dbus_register(obj_desc, arg, wpa_dbus_free, NULL,
3504                            wpas_dbus_p2p_groupmember_properties, NULL);
3505
3506         if (wpa_dbus_register_object_per_iface(ctrl_iface, groupmember_obj_path,
3507                                                wpa_s->ifname, obj_desc))
3508                 goto err;
3509
3510         wpa_printf(MSG_INFO,
3511                    "dbus: Registered group member object '%s' successfully",
3512                    groupmember_obj_path);
3513         return;
3514
3515 err:
3516         free_dbus_object_desc(obj_desc);
3517 }
3518
3519 /**
3520  * wpas_dbus_unregister_p2p_groupmember - Unregister a p2p groupmember
3521  * object with dbus
3522  * @wpa_s: wpa_supplicant interface structure
3523  * @p2p_if_addr: i/f addr of the device joining this group
3524  *
3525  * Unregisters p2p groupmember representing object with dbus
3526  */
3527 void wpas_dbus_unregister_p2p_groupmember(struct wpa_supplicant *wpa_s,
3528                                           const u8 *p2p_if_addr)
3529 {
3530         struct wpas_dbus_priv *ctrl_iface;
3531         char groupmember_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
3532
3533         /* Do nothing if the control interface is not turned on */
3534         if (wpa_s == NULL || wpa_s->global == NULL)
3535                 return;
3536
3537         ctrl_iface = wpa_s->global->dbus;
3538         if (ctrl_iface == NULL)
3539                 return;
3540
3541         if (!wpa_s->dbus_groupobj_path)
3542                 return;
3543
3544         os_snprintf(groupmember_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
3545                 "%s/" WPAS_DBUS_NEW_P2P_GROUPMEMBERS_PART "/" COMPACT_MACSTR,
3546                 wpa_s->dbus_groupobj_path, MAC2STR(p2p_if_addr));
3547
3548         wpa_dbus_unregister_object_per_iface(ctrl_iface, groupmember_obj_path);
3549 }
3550
3551
3552 static const struct wpa_dbus_property_desc
3553         wpas_dbus_persistent_group_properties[] = {
3554         { "Properties", WPAS_DBUS_NEW_IFACE_PERSISTENT_GROUP, "a{sv}",
3555           wpas_dbus_getter_persistent_group_properties,
3556           wpas_dbus_setter_persistent_group_properties
3557         },
3558         { NULL, NULL, NULL, NULL, NULL }
3559 };
3560
3561 /* No signals intended for persistent group objects */
3562
3563 /**
3564  * wpas_dbus_register_persistent_group - Register a configured(saved)
3565  *      persistent group with dbus
3566  * @wpa_s: wpa_supplicant interface structure
3567  * @ssid: persistent group (still represented as a network within wpa)
3568  *        configuration data
3569  * Returns: 0 on success, -1 on failure
3570  *
3571  * Registers a persistent group representing object with dbus.
3572  */
3573 int wpas_dbus_register_persistent_group(struct wpa_supplicant *wpa_s,
3574                                         struct wpa_ssid *ssid)
3575 {
3576         struct wpas_dbus_priv *ctrl_iface;
3577         struct wpa_dbus_object_desc *obj_desc;
3578         struct network_handler_args *arg;
3579         char pgrp_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
3580
3581         /* Do nothing if the control interface is not turned on */
3582         if (wpa_s == NULL || wpa_s->global == NULL)
3583                 return 0;
3584
3585         /* Make sure ssid is a persistent group */
3586         if (ssid->disabled != 2 && !ssid->p2p_persistent_group)
3587                 return -1; /* should we return w/o complaining? */
3588
3589         ctrl_iface = wpa_s->global->dbus;
3590         if (ctrl_iface == NULL)
3591                 return 0;
3592
3593         /*
3594          * Intentionally not coming up with different numbering scheme
3595          * for persistent groups.
3596          */
3597         os_snprintf(pgrp_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
3598                     "%s/" WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/%u",
3599                     wpa_s->dbus_new_path, ssid->id);
3600
3601         wpa_printf(MSG_DEBUG, "dbus: Register persistent group object '%s'",
3602                    pgrp_obj_path);
3603         obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
3604         if (!obj_desc) {
3605                 wpa_printf(MSG_ERROR, "dbus: Not enough memory to create "
3606                            "object description");
3607                 goto err;
3608         }
3609
3610         /*
3611          * Reusing the same context structure as that for networks
3612          * since these are represented using same data structure.
3613          */
3614         /* allocate memory for handlers arguments */
3615         arg = os_zalloc(sizeof(struct network_handler_args));
3616         if (!arg) {
3617                 wpa_printf(MSG_ERROR, "dbus: Not enough memory to create "
3618                            "arguments for method");
3619                 goto err;
3620         }
3621
3622         arg->wpa_s = wpa_s;
3623         arg->ssid = ssid;
3624
3625         wpas_dbus_register(obj_desc, arg, wpa_dbus_free, NULL,
3626                            wpas_dbus_persistent_group_properties,
3627                            NULL);
3628
3629         if (wpa_dbus_register_object_per_iface(ctrl_iface, pgrp_obj_path,
3630                                                wpa_s->ifname, obj_desc))
3631                 goto err;
3632
3633         wpas_dbus_signal_persistent_group_added(wpa_s, ssid->id);
3634
3635         return 0;
3636
3637 err:
3638         free_dbus_object_desc(obj_desc);
3639         return -1;
3640 }
3641
3642
3643 /**
3644  * wpas_dbus_unregister_persistent_group - Unregister a persistent_group
3645  *      from dbus
3646  * @wpa_s: wpa_supplicant interface structure
3647  * @nid: network id
3648  * Returns: 0 on success, -1 on failure
3649  *
3650  * Unregisters persistent group representing object from dbus
3651  *
3652  * NOTE: There is a slight issue with the semantics here. While the
3653  * implementation simply means the persistent group is unloaded from memory,
3654  * it should not get interpreted as the group is actually being erased/removed
3655  * from persistent storage as well.
3656  */
3657 int wpas_dbus_unregister_persistent_group(struct wpa_supplicant *wpa_s,
3658                                           int nid)
3659 {
3660         struct wpas_dbus_priv *ctrl_iface;
3661         char pgrp_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
3662         int ret;
3663
3664         /* Do nothing if the control interface is not turned on */
3665         if (wpa_s == NULL || wpa_s->global == NULL ||
3666             wpa_s->dbus_new_path == NULL)
3667                 return 0;
3668         ctrl_iface = wpa_s->global->dbus;
3669         if (ctrl_iface == NULL)
3670                 return 0;
3671
3672         os_snprintf(pgrp_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
3673                     "%s/" WPAS_DBUS_NEW_PERSISTENT_GROUPS_PART "/%u",
3674                     wpa_s->dbus_new_path, nid);
3675
3676         wpa_printf(MSG_DEBUG, "dbus: Unregister persistent group object '%s'",
3677                    pgrp_obj_path);
3678         ret = wpa_dbus_unregister_object_per_iface(ctrl_iface, pgrp_obj_path);
3679
3680         if (!ret)
3681                 wpas_dbus_signal_persistent_group_removed(wpa_s, nid);
3682
3683         return ret;
3684 }
3685
3686 #endif /* CONFIG_P2P */