f85fedfb40839273dce6cc0c7b9b91d19d40c052
[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 "wps/wps.h"
21 #include "../config.h"
22 #include "../wpa_supplicant_i.h"
23 #include "../bss.h"
24 #include "dbus_new_helpers.h"
25 #include "dbus_dict_helpers.h"
26 #include "dbus_new.h"
27 #include "dbus_new_handlers.h"
28 #include "dbus_common.h"
29 #include "dbus_common_i.h"
30
31
32 /**
33  * wpas_dbus_signal_interface - Send a interface related event signal
34  * @wpa_s: %wpa_supplicant network interface data
35  * @sig_name: signal name - InterfaceAdded or InterfaceRemoved
36  * @properties: Whether to add second argument with object properties
37  *
38  * Notify listeners about event related with interface
39  */
40 static void wpas_dbus_signal_interface(struct wpa_supplicant *wpa_s,
41                                        const char *sig_name, int properties)
42 {
43         struct wpas_dbus_priv *iface;
44         DBusMessage *msg;
45         DBusMessageIter iter, iter_dict;
46
47         iface = wpa_s->global->dbus;
48
49         /* Do nothing if the control interface is not turned on */
50         if (iface == NULL)
51                 return;
52
53         msg = dbus_message_new_signal(WPAS_DBUS_NEW_PATH,
54                                       WPAS_DBUS_NEW_INTERFACE, sig_name);
55         if (msg == NULL)
56                 return;
57
58         dbus_message_iter_init_append(msg, &iter);
59         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
60                                             &wpa_s->dbus_new_path))
61                 goto err;
62
63         if (properties) {
64                 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
65                         goto err;
66
67                 wpa_dbus_get_object_properties(iface, wpa_s->dbus_new_path,
68                                                WPAS_DBUS_NEW_IFACE_INTERFACE,
69                                                &iter_dict);
70
71                 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
72                         goto err;
73         }
74
75         dbus_connection_send(iface->con, msg, NULL);
76         dbus_message_unref(msg);
77         return;
78
79 err:
80         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
81         dbus_message_unref(msg);
82 }
83
84
85 /**
86  * wpas_dbus_signal_interface_added - Send a interface created signal
87  * @wpa_s: %wpa_supplicant network interface data
88  *
89  * Notify listeners about creating new interface
90  */
91 static void wpas_dbus_signal_interface_added(struct wpa_supplicant *wpa_s)
92 {
93         wpas_dbus_signal_interface(wpa_s, "InterfaceAdded", TRUE);
94 }
95
96
97 /**
98  * wpas_dbus_signal_interface_removed - Send a interface removed signal
99  * @wpa_s: %wpa_supplicant network interface data
100  *
101  * Notify listeners about removing interface
102  */
103 static void wpas_dbus_signal_interface_removed(struct wpa_supplicant *wpa_s)
104 {
105         wpas_dbus_signal_interface(wpa_s, "InterfaceRemoved", FALSE);
106
107 }
108
109
110 /**
111  * wpas_dbus_signal_scan_done - send scan done signal
112  * @wpa_s: %wpa_supplicant network interface data
113  * @success: indicates if scanning succeed or failed
114  *
115  * Notify listeners about finishing a scan
116  */
117 void wpas_dbus_signal_scan_done(struct wpa_supplicant *wpa_s, int success)
118 {
119         struct wpas_dbus_priv *iface;
120         DBusMessage *msg;
121         dbus_bool_t succ;
122
123         iface = wpa_s->global->dbus;
124
125         /* Do nothing if the control interface is not turned on */
126         if (iface == NULL)
127                 return;
128
129         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
130                                       WPAS_DBUS_NEW_IFACE_INTERFACE,
131                                       "ScanDone");
132         if (msg == NULL)
133                 return;
134
135         succ = success ? TRUE : FALSE;
136         if (dbus_message_append_args(msg, DBUS_TYPE_BOOLEAN, &succ,
137                                      DBUS_TYPE_INVALID))
138                 dbus_connection_send(iface->con, msg, NULL);
139         else
140                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
141         dbus_message_unref(msg);
142 }
143
144
145 /**
146  * wpas_dbus_signal_blob - Send a BSS related event signal
147  * @wpa_s: %wpa_supplicant network interface data
148  * @bss_obj_path: BSS object path
149  * @sig_name: signal name - BSSAdded or BSSRemoved
150  * @properties: Whether to add second argument with object properties
151  *
152  * Notify listeners about event related with BSS
153  */
154 static void wpas_dbus_signal_bss(struct wpa_supplicant *wpa_s,
155                                  const char *bss_obj_path,
156                                  const char *sig_name, int properties)
157 {
158         struct wpas_dbus_priv *iface;
159         DBusMessage *msg;
160         DBusMessageIter iter, iter_dict;
161
162         iface = wpa_s->global->dbus;
163
164         /* Do nothing if the control interface is not turned on */
165         if (iface == NULL)
166                 return;
167
168         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
169                                       WPAS_DBUS_NEW_IFACE_INTERFACE,
170                                       sig_name);
171         if (msg == NULL)
172                 return;
173
174         dbus_message_iter_init_append(msg, &iter);
175         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
176                                             &bss_obj_path))
177                 goto err;
178
179         if (properties) {
180                 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
181                         goto err;
182
183                 wpa_dbus_get_object_properties(iface, bss_obj_path,
184                                                WPAS_DBUS_NEW_IFACE_BSS,
185                                                &iter_dict);
186
187                 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
188                         goto err;
189         }
190
191         dbus_connection_send(iface->con, msg, NULL);
192         dbus_message_unref(msg);
193         return;
194
195 err:
196         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
197         dbus_message_unref(msg);
198 }
199
200
201 /**
202  * wpas_dbus_signal_bss_added - Send a BSS added signal
203  * @wpa_s: %wpa_supplicant network interface data
204  * @bss_obj_path: new BSS object path
205  *
206  * Notify listeners about adding new BSS
207  */
208 static void wpas_dbus_signal_bss_added(struct wpa_supplicant *wpa_s,
209                                        const char *bss_obj_path)
210 {
211         wpas_dbus_signal_bss(wpa_s, bss_obj_path, "BSSAdded", TRUE);
212 }
213
214
215 /**
216  * wpas_dbus_signal_bss_removed - Send a BSS removed signal
217  * @wpa_s: %wpa_supplicant network interface data
218  * @bss_obj_path: BSS object path
219  *
220  * Notify listeners about removing BSS
221  */
222 static void wpas_dbus_signal_bss_removed(struct wpa_supplicant *wpa_s,
223                                          const char *bss_obj_path)
224 {
225         wpas_dbus_signal_bss(wpa_s, bss_obj_path, "BSSRemoved", FALSE);
226 }
227
228
229 /**
230  * wpas_dbus_signal_blob - Send a blob related event signal
231  * @wpa_s: %wpa_supplicant network interface data
232  * @name: blob name
233  * @sig_name: signal name - BlobAdded or BlobRemoved
234  *
235  * Notify listeners about event related with blob
236  */
237 static void wpas_dbus_signal_blob(struct wpa_supplicant *wpa_s,
238                                   const char *name, const char *sig_name)
239 {
240         struct wpas_dbus_priv *iface;
241         DBusMessage *msg;
242
243         iface = wpa_s->global->dbus;
244
245         /* Do nothing if the control interface is not turned on */
246         if (iface == NULL)
247                 return;
248
249         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
250                                       WPAS_DBUS_NEW_IFACE_INTERFACE,
251                                       sig_name);
252         if (msg == NULL)
253                 return;
254
255         if (dbus_message_append_args(msg, DBUS_TYPE_STRING, &name,
256                                      DBUS_TYPE_INVALID))
257                 dbus_connection_send(iface->con, msg, NULL);
258         else
259                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
260         dbus_message_unref(msg);
261 }
262
263
264 /**
265  * wpas_dbus_signal_blob_added - Send a blob added signal
266  * @wpa_s: %wpa_supplicant network interface data
267  * @name: blob name
268  *
269  * Notify listeners about adding a new blob
270  */
271 void wpas_dbus_signal_blob_added(struct wpa_supplicant *wpa_s,
272                                  const char *name)
273 {
274         wpas_dbus_signal_blob(wpa_s, name, "BlobAdded");
275 }
276
277
278 /**
279  * wpas_dbus_signal_blob_removed - Send a blob removed signal
280  * @wpa_s: %wpa_supplicant network interface data
281  * @name: blob name
282  *
283  * Notify listeners about removing blob
284  */
285 void wpas_dbus_signal_blob_removed(struct wpa_supplicant *wpa_s,
286                                    const char *name)
287 {
288         wpas_dbus_signal_blob(wpa_s, name, "BlobRemoved");
289 }
290
291
292 /**
293  * wpas_dbus_signal_network - Send a network related event signal
294  * @wpa_s: %wpa_supplicant network interface data
295  * @id: new network id
296  * @sig_name: signal name - NetworkAdded, NetworkRemoved or NetworkSelected
297  * @properties: determines if add second argument with object properties
298  *
299  * Notify listeners about event related with configured network
300  */
301 static void wpas_dbus_signal_network(struct wpa_supplicant *wpa_s,
302                                      int id, const char *sig_name,
303                                      int properties)
304 {
305         struct wpas_dbus_priv *iface;
306         DBusMessage *msg;
307         DBusMessageIter iter, iter_dict;
308         char net_obj_path[WPAS_DBUS_OBJECT_PATH_MAX], *path;
309
310         iface = wpa_s->global->dbus;
311
312         /* Do nothing if the control interface is not turned on */
313         if (iface == NULL)
314                 return;
315
316         os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
317                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
318                     wpa_s->dbus_new_path, id);
319
320         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
321                                       WPAS_DBUS_NEW_IFACE_INTERFACE,
322                                       sig_name);
323         if (msg == NULL)
324                 return;
325
326         dbus_message_iter_init_append(msg, &iter);
327         path = net_obj_path;
328         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
329                                             &path))
330                 goto err;
331
332         if (properties) {
333                 if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
334                         goto err;
335
336                 wpa_dbus_get_object_properties(iface, net_obj_path,
337                                                WPAS_DBUS_NEW_IFACE_NETWORK,
338                                                &iter_dict);
339
340                 if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
341                         goto err;
342         }
343
344         dbus_connection_send(iface->con, msg, NULL);
345
346         dbus_message_unref(msg);
347         return;
348
349 err:
350         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
351         dbus_message_unref(msg);
352 }
353
354
355 /**
356  * wpas_dbus_signal_network_added - Send a network added signal
357  * @wpa_s: %wpa_supplicant network interface data
358  * @id: new network id
359  *
360  * Notify listeners about adding new network
361  */
362 static void wpas_dbus_signal_network_added(struct wpa_supplicant *wpa_s,
363                                            int id)
364 {
365         wpas_dbus_signal_network(wpa_s, id, "NetworkAdded", TRUE);
366 }
367
368
369 /**
370  * wpas_dbus_signal_network_removed - Send a network removed signal
371  * @wpa_s: %wpa_supplicant network interface data
372  * @id: network id
373  *
374  * Notify listeners about removing a network
375  */
376 static void wpas_dbus_signal_network_removed(struct wpa_supplicant *wpa_s,
377                                              int id)
378 {
379         wpas_dbus_signal_network(wpa_s, id, "NetworkRemoved", FALSE);
380 }
381
382
383 /**
384  * wpas_dbus_signal_network_selected - Send a network selected signal
385  * @wpa_s: %wpa_supplicant network interface data
386  * @id: network id
387  *
388  * Notify listeners about selecting a network
389  */
390 void wpas_dbus_signal_network_selected(struct wpa_supplicant *wpa_s, int id)
391 {
392         wpas_dbus_signal_network(wpa_s, id, "NetworkSelected", FALSE);
393 }
394
395
396 /**
397  * wpas_dbus_signal_network_enabled_changed - Signals Enabled property changes
398  * @wpa_s: %wpa_supplicant network interface data
399  * @ssid: configured network which Enabled property has changed
400  *
401  * Sends PropertyChanged signals containing new value of Enabled property
402  * for specified network
403  */
404 void wpas_dbus_signal_network_enabled_changed(struct wpa_supplicant *wpa_s,
405                                               struct wpa_ssid *ssid)
406 {
407
408         char path[WPAS_DBUS_OBJECT_PATH_MAX];
409         os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
410                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
411                     wpa_s->dbus_new_path, ssid->id);
412
413         wpa_dbus_mark_property_changed(wpa_s->global->dbus, path,
414                                        WPAS_DBUS_NEW_IFACE_NETWORK, "Enabled");
415 }
416
417
418 #ifdef CONFIG_WPS
419
420 /**
421  * wpas_dbus_signal_wps_event_success - Signals Success WPS event
422  * @wpa_s: %wpa_supplicant network interface data
423  *
424  * Sends Event dbus signal with name "success" and empty dict as arguments
425  */
426 void wpas_dbus_signal_wps_event_success(struct wpa_supplicant *wpa_s)
427 {
428
429         DBusMessage *msg;
430         DBusMessageIter iter, dict_iter;
431         struct wpas_dbus_priv *iface;
432         char *key = "success";
433
434         iface = wpa_s->global->dbus;
435
436         /* Do nothing if the control interface is not turned on */
437         if (iface == NULL)
438                 return;
439
440         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
441                                       WPAS_DBUS_NEW_IFACE_WPS, "Event");
442         if (msg == NULL)
443                 return;
444
445         dbus_message_iter_init_append(msg, &iter);
446
447         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) ||
448             !wpa_dbus_dict_open_write(&iter, &dict_iter) ||
449             !wpa_dbus_dict_close_write(&iter, &dict_iter))
450                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
451         else
452                 dbus_connection_send(iface->con, msg, NULL);
453
454         dbus_message_unref(msg);
455 }
456
457
458 /**
459  * wpas_dbus_signal_wps_event_fail - Signals Fail WPS event
460  * @wpa_s: %wpa_supplicant network interface data
461  *
462  * Sends Event dbus signal with name "fail" and dictionary containing
463  * "msg field with fail message number (int32) as arguments
464  */
465 void wpas_dbus_signal_wps_event_fail(struct wpa_supplicant *wpa_s,
466                                      struct wps_event_fail *fail)
467 {
468
469         DBusMessage *msg;
470         DBusMessageIter iter, dict_iter;
471         struct wpas_dbus_priv *iface;
472         char *key = "fail";
473
474         iface = wpa_s->global->dbus;
475
476         /* Do nothing if the control interface is not turned on */
477         if (iface == NULL)
478                 return;
479
480         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
481                                       WPAS_DBUS_NEW_IFACE_WPS, "Event");
482         if (msg == NULL)
483                 return;
484
485         dbus_message_iter_init_append(msg, &iter);
486
487         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) ||
488             !wpa_dbus_dict_open_write(&iter, &dict_iter) ||
489             !wpa_dbus_dict_append_int32(&dict_iter, "msg", fail->msg) ||
490             !wpa_dbus_dict_close_write(&iter, &dict_iter))
491                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
492         else
493                 dbus_connection_send(iface->con, msg, NULL);
494
495         dbus_message_unref(msg);
496 }
497
498
499 /**
500  * wpas_dbus_signal_wps_event_m2d - Signals M2D WPS event
501  * @wpa_s: %wpa_supplicant network interface data
502  *
503  * Sends Event dbus signal with name "m2d" and dictionary containing
504  * fields of wps_event_m2d structure.
505  */
506 void wpas_dbus_signal_wps_event_m2d(struct wpa_supplicant *wpa_s,
507                                     struct wps_event_m2d *m2d)
508 {
509
510         DBusMessage *msg;
511         DBusMessageIter iter, dict_iter;
512         struct wpas_dbus_priv *iface;
513         char *key = "m2d";
514
515         iface = wpa_s->global->dbus;
516
517         /* Do nothing if the control interface is not turned on */
518         if (iface == NULL)
519                 return;
520
521         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
522                                       WPAS_DBUS_NEW_IFACE_WPS, "Event");
523         if (msg == NULL)
524                 return;
525
526         dbus_message_iter_init_append(msg, &iter);
527
528         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key) ||
529             !wpa_dbus_dict_open_write(&iter, &dict_iter) ||
530             !wpa_dbus_dict_append_uint16(&dict_iter, "config_methods",
531                                          m2d->config_methods) ||
532             !wpa_dbus_dict_append_byte_array(&dict_iter, "manufacturer",
533                                              (const char *) m2d->manufacturer,
534                                              m2d->manufacturer_len) ||
535             !wpa_dbus_dict_append_byte_array(&dict_iter, "model_name",
536                                              (const char *) m2d->model_name,
537                                              m2d->model_name_len) ||
538             !wpa_dbus_dict_append_byte_array(&dict_iter, "model_number",
539                                              (const char *) m2d->model_number,
540                                              m2d->model_number_len) ||
541             !wpa_dbus_dict_append_byte_array(&dict_iter, "serial_number",
542                                              (const char *)
543                                              m2d->serial_number,
544                                              m2d->serial_number_len) ||
545             !wpa_dbus_dict_append_byte_array(&dict_iter, "dev_name",
546                                              (const char *) m2d->dev_name,
547                                              m2d->dev_name_len) ||
548             !wpa_dbus_dict_append_byte_array(&dict_iter, "primary_dev_type",
549                                              (const char *)
550                                              m2d->primary_dev_type, 8) ||
551             !wpa_dbus_dict_append_uint16(&dict_iter, "config_error",
552                                          m2d->config_error) ||
553             !wpa_dbus_dict_append_uint16(&dict_iter, "dev_password_id",
554                                          m2d->dev_password_id) ||
555             !wpa_dbus_dict_close_write(&iter, &dict_iter))
556                 wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
557         else
558                 dbus_connection_send(iface->con, msg, NULL);
559
560         dbus_message_unref(msg);
561 }
562
563
564 /**
565  * wpas_dbus_signal_wps_cred - Signals new credentials
566  * @wpa_s: %wpa_supplicant network interface data
567  *
568  * Sends signal with credentials in directory argument
569  */
570 void wpas_dbus_signal_wps_cred(struct wpa_supplicant *wpa_s,
571                                const struct wps_credential *cred)
572 {
573         DBusMessage *msg;
574         DBusMessageIter iter, dict_iter;
575         struct wpas_dbus_priv *iface;
576         char *auth_type[6]; /* we have six possible authorization types */
577         int at_num = 0;
578         char *encr_type[4]; /* we have four possible encryption types */
579         int et_num = 0;
580
581         iface = wpa_s->global->dbus;
582
583         /* Do nothing if the control interface is not turned on */
584         if (iface == NULL)
585                 return;
586
587         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
588                                       WPAS_DBUS_NEW_IFACE_WPS,
589                                       "Credentials");
590         if (msg == NULL)
591                 return;
592
593         dbus_message_iter_init_append(msg, &iter);
594         if (!wpa_dbus_dict_open_write(&iter, &dict_iter))
595                 goto nomem;
596
597         if (cred->auth_type & WPS_AUTH_OPEN)
598                 auth_type[at_num++] = "open";
599         if (cred->auth_type & WPS_AUTH_WPAPSK)
600                 auth_type[at_num++] = "wpa-psk";
601         if (cred->auth_type & WPS_AUTH_SHARED)
602                 auth_type[at_num++] = "shared";
603         if (cred->auth_type & WPS_AUTH_WPA)
604                 auth_type[at_num++] = "wpa-eap";
605         if (cred->auth_type & WPS_AUTH_WPA2)
606                 auth_type[at_num++] = "wpa2-eap";
607         if (cred->auth_type & WPS_AUTH_WPA2PSK)
608                 auth_type[at_num++] =
609                 "wpa2-psk";
610
611         if (cred->encr_type & WPS_ENCR_NONE)
612                 encr_type[et_num++] = "none";
613         if (cred->encr_type & WPS_ENCR_WEP)
614                 encr_type[et_num++] = "wep";
615         if (cred->encr_type & WPS_ENCR_TKIP)
616                 encr_type[et_num++] = "tkip";
617         if (cred->encr_type & WPS_ENCR_AES)
618                 encr_type[et_num++] = "aes";
619
620         if (wpa_s->current_ssid) {
621                 if (!wpa_dbus_dict_append_byte_array(
622                             &dict_iter, "BSSID",
623                             (const char *) wpa_s->current_ssid->bssid,
624                             ETH_ALEN))
625                         goto nomem;
626         }
627
628         if (!wpa_dbus_dict_append_byte_array(&dict_iter, "SSID",
629                                              (const char *) cred->ssid,
630                                              cred->ssid_len) ||
631             !wpa_dbus_dict_append_string_array(&dict_iter, "AuthType",
632                                                (const char **) auth_type,
633                                                at_num) ||
634             !wpa_dbus_dict_append_string_array(&dict_iter, "EncrType",
635                                                (const char **) encr_type,
636                                                et_num) ||
637             !wpa_dbus_dict_append_byte_array(&dict_iter, "Key",
638                                              (const char *) cred->key,
639                                              cred->key_len) ||
640             !wpa_dbus_dict_append_uint32(&dict_iter, "KeyIndex",
641                                          cred->key_idx) ||
642             !wpa_dbus_dict_close_write(&iter, &dict_iter))
643                 goto nomem;
644
645         dbus_connection_send(iface->con, msg, NULL);
646
647 nomem:
648         dbus_message_unref(msg);
649 }
650
651 #endif /* CONFIG_WPS */
652
653
654 /**
655  * wpas_dbus_signal_prop_changed - Signals change of property
656  * @wpa_s: %wpa_supplicant network interface data
657  * @property: indicates which property has changed
658  *
659  * Sends ProertyChanged signals with path, interface and arguments
660  * depending on which property has changed.
661  */
662 void wpas_dbus_signal_prop_changed(struct wpa_supplicant *wpa_s,
663                                    enum wpas_dbus_prop property)
664 {
665         WPADBusPropertyAccessor getter;
666         char *prop;
667
668         if (wpa_s->dbus_new_path == NULL)
669                 return; /* Skip signal since D-Bus setup is not yet ready */
670
671         switch (property) {
672         case WPAS_DBUS_PROP_AP_SCAN:
673                 getter = (WPADBusPropertyAccessor) wpas_dbus_getter_ap_scan;
674                 prop = "ApScan";
675                 break;
676         case WPAS_DBUS_PROP_SCANNING:
677                 getter = (WPADBusPropertyAccessor) wpas_dbus_getter_scanning;
678                 prop = "Scanning";
679                 break;
680         case WPAS_DBUS_PROP_STATE:
681                 getter = (WPADBusPropertyAccessor) wpas_dbus_getter_state;
682                 prop = "State";
683                 break;
684         case WPAS_DBUS_PROP_CURRENT_BSS:
685                 getter = (WPADBusPropertyAccessor)
686                         wpas_dbus_getter_current_bss;
687                 prop = "CurrentBSS";
688                 break;
689         case WPAS_DBUS_PROP_CURRENT_NETWORK:
690                 getter = (WPADBusPropertyAccessor)
691                         wpas_dbus_getter_current_network;
692                 prop = "CurrentNetwork";
693                 break;
694         case WPAS_DBUS_PROP_BSSS:
695                 getter = (WPADBusPropertyAccessor) wpas_dbus_getter_bsss;
696                 prop = "BSSs";
697                 break;
698         case WPAS_DBUS_PROP_CURRENT_AUTH_MODE:
699                 getter = (WPADBusPropertyAccessor)
700                         wpas_dbus_getter_current_auth_mode;
701                 prop = "CurrentAuthMode";
702                 break;
703         default:
704                 wpa_printf(MSG_ERROR, "dbus: %s: Unknown Property value %d",
705                            __func__, property);
706                 return;
707         }
708
709         wpa_dbus_mark_property_changed(wpa_s->global->dbus,
710                                        wpa_s->dbus_new_path,
711                                        WPAS_DBUS_NEW_IFACE_INTERFACE, prop);
712 }
713
714
715 /**
716  * wpas_dbus_bss_signal_prop_changed - Signals change of BSS property
717  * @wpa_s: %wpa_supplicant network interface data
718  * @property: indicates which property has changed
719  * @id: unique BSS identifier
720  *
721  * Sends PropertyChanged signals with path, interface, and arguments depending
722  * on which property has changed.
723  */
724 void wpas_dbus_bss_signal_prop_changed(struct wpa_supplicant *wpa_s,
725                                        enum wpas_dbus_bss_prop property,
726                                        unsigned int id)
727 {
728         char path[WPAS_DBUS_OBJECT_PATH_MAX];
729         char *prop;
730
731         switch (property) {
732         case WPAS_DBUS_BSS_PROP_SIGNAL:
733                 prop = "Signal";
734                 break;
735         case WPAS_DBUS_BSS_PROP_FREQ:
736                 prop = "Frequency";
737                 break;
738         case WPAS_DBUS_BSS_PROP_MODE:
739                 prop = "Mode";
740                 break;
741         case WPAS_DBUS_BSS_PROP_PRIVACY:
742                 prop = "Privacy";
743                 break;
744         case WPAS_DBUS_BSS_PROP_RATES:
745                 prop = "Rates";
746                 break;
747         case WPAS_DBUS_BSS_PROP_WPA:
748                 prop = "WPA";
749                 break;
750         case WPAS_DBUS_BSS_PROP_RSN:
751                 prop = "RSN";
752                 break;
753         case WPAS_DBUS_BSS_PROP_IES:
754                 prop = "IEs";
755                 break;
756         default:
757                 wpa_printf(MSG_ERROR, "dbus: %s: Unknown Property value %d",
758                            __func__, property);
759                 return;
760         }
761
762         os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
763                     "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
764                     wpa_s->dbus_new_path, id);
765
766         wpa_dbus_mark_property_changed(wpa_s->global->dbus, path,
767                                        WPAS_DBUS_NEW_IFACE_BSS, prop);
768 }
769
770
771 /**
772  * wpas_dbus_signal_debug_level_changed - Signals change of debug param
773  * @global: wpa_global structure
774  *
775  * Sends ProertyChanged signals informing that debug level has changed.
776  */
777 void wpas_dbus_signal_debug_level_changed(struct wpa_global *global)
778 {
779         wpa_dbus_mark_property_changed(global->dbus, WPAS_DBUS_NEW_PATH,
780                                        WPAS_DBUS_NEW_INTERFACE,
781                                        "DebugLevel");
782 }
783
784
785 /**
786  * wpas_dbus_signal_debug_timestamp_changed - Signals change of debug param
787  * @global: wpa_global structure
788  *
789  * Sends ProertyChanged signals informing that debug timestamp has changed.
790  */
791 void wpas_dbus_signal_debug_timestamp_changed(struct wpa_global *global)
792 {
793         wpa_dbus_mark_property_changed(global->dbus, WPAS_DBUS_NEW_PATH,
794                                        WPAS_DBUS_NEW_INTERFACE,
795                                        "DebugTimestamp");
796 }
797
798
799 /**
800  * wpas_dbus_signal_debug_show_keys_changed - Signals change of debug param
801  * @global: wpa_global structure
802  *
803  * Sends ProertyChanged signals informing that debug show_keys has changed.
804  */
805 void wpas_dbus_signal_debug_show_keys_changed(struct wpa_global *global)
806 {
807         wpa_dbus_mark_property_changed(global->dbus, WPAS_DBUS_NEW_PATH,
808                                        WPAS_DBUS_NEW_INTERFACE,
809                                        "DebugShowKeys");
810 }
811
812
813 static void wpas_dbus_register(struct wpa_dbus_object_desc *obj_desc,
814                                void *priv,
815                                WPADBusArgumentFreeFunction priv_free,
816                                const struct wpa_dbus_method_desc *methods,
817                                const struct wpa_dbus_property_desc *properties,
818                                const struct wpa_dbus_signal_desc *signals)
819 {
820         int n;
821
822         obj_desc->user_data = priv;
823         obj_desc->user_data_free_func = priv_free;
824         obj_desc->methods = methods;
825         obj_desc->properties = properties;
826         obj_desc->signals = signals;
827
828         for (n = 0; properties && properties->dbus_property; properties++)
829                 n++;
830
831         obj_desc->prop_changed_flags = os_zalloc(n);
832         if (!obj_desc->prop_changed_flags)
833                 wpa_printf(MSG_DEBUG, "dbus: %s: can't register handlers",
834                            __func__);
835 }
836
837
838 static const struct wpa_dbus_method_desc wpas_dbus_global_methods[] = {
839         { "CreateInterface", WPAS_DBUS_NEW_INTERFACE,
840           (WPADBusMethodHandler) &wpas_dbus_handler_create_interface,
841           {
842                   { "args", "a{sv}", ARG_IN },
843                   { "path", "o", ARG_OUT },
844                   END_ARGS
845           }
846         },
847         { "RemoveInterface", WPAS_DBUS_NEW_INTERFACE,
848           (WPADBusMethodHandler) &wpas_dbus_handler_remove_interface,
849           {
850                   { "path", "o", ARG_IN },
851                   END_ARGS
852           }
853         },
854         { "GetInterface", WPAS_DBUS_NEW_INTERFACE,
855           (WPADBusMethodHandler) &wpas_dbus_handler_get_interface,
856           {
857                   { "ifname", "s", ARG_IN },
858                   { "path", "o", ARG_OUT },
859                   END_ARGS
860           }
861         },
862         { NULL, NULL, NULL, { END_ARGS } }
863 };
864
865 static const struct wpa_dbus_property_desc wpas_dbus_global_properties[] = {
866         { "DebugLevel", WPAS_DBUS_NEW_INTERFACE, "s",
867           (WPADBusPropertyAccessor) wpas_dbus_getter_debug_level,
868           (WPADBusPropertyAccessor) wpas_dbus_setter_debug_level,
869           RW
870         },
871         { "DebugTimestamp", WPAS_DBUS_NEW_INTERFACE, "b",
872           (WPADBusPropertyAccessor) wpas_dbus_getter_debug_timestamp,
873           (WPADBusPropertyAccessor) wpas_dbus_setter_debug_timestamp,
874           RW
875         },
876         { "DebugShowKeys", WPAS_DBUS_NEW_INTERFACE, "b",
877           (WPADBusPropertyAccessor) wpas_dbus_getter_debug_show_keys,
878           (WPADBusPropertyAccessor) wpas_dbus_setter_debug_show_keys,
879           RW
880         },
881         { "Interfaces", WPAS_DBUS_NEW_INTERFACE, "ao",
882           (WPADBusPropertyAccessor) &wpas_dbus_getter_interfaces,
883           NULL,
884           R
885         },
886         { "EapMethods", WPAS_DBUS_NEW_INTERFACE, "as",
887           (WPADBusPropertyAccessor) wpas_dbus_getter_eap_methods,
888           NULL,
889           R
890         },
891         { NULL, NULL, NULL, NULL, NULL, 0 }
892 };
893
894 static const struct wpa_dbus_signal_desc wpas_dbus_global_signals[] = {
895         { "InterfaceAdded", WPAS_DBUS_NEW_INTERFACE,
896           {
897                   { "path", "o", ARG_OUT },
898                   { "properties", "a{sv}", ARG_OUT },
899                   END_ARGS
900           }
901         },
902         { "InterfaceRemoved", WPAS_DBUS_NEW_INTERFACE,
903           {
904                   { "path", "o", ARG_OUT },
905                   END_ARGS
906           }
907         },
908         { "PropertiesChanged", WPAS_DBUS_NEW_INTERFACE,
909           {
910                   { "properties", "a{sv}", ARG_OUT },
911                   END_ARGS
912           }
913         },
914         { NULL, NULL, { END_ARGS } }
915 };
916
917
918 /**
919  * wpas_dbus_ctrl_iface_init - Initialize dbus control interface
920  * @global: Pointer to global data from wpa_supplicant_init()
921  * Returns: 0 on success or -1 on failure
922  *
923  * Initialize the dbus control interface for wpa_supplicantand and start
924  * receiving commands from external programs over the bus.
925  */
926 int wpas_dbus_ctrl_iface_init(struct wpas_dbus_priv *priv)
927 {
928         struct wpa_dbus_object_desc *obj_desc;
929         int ret;
930
931         obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
932         if (!obj_desc) {
933                 wpa_printf(MSG_ERROR, "Not enough memory "
934                            "to create object description");
935                 return -1;
936         }
937
938         wpas_dbus_register(obj_desc, priv->global, NULL,
939                            wpas_dbus_global_methods,
940                            wpas_dbus_global_properties,
941                            wpas_dbus_global_signals);
942
943         wpa_printf(MSG_DEBUG, "dbus: Register D-Bus object '%s'",
944                    WPAS_DBUS_NEW_PATH);
945         ret = wpa_dbus_ctrl_iface_init(priv, WPAS_DBUS_NEW_PATH,
946                                        WPAS_DBUS_NEW_SERVICE,
947                                        obj_desc);
948         if (ret < 0)
949                 free_dbus_object_desc(obj_desc);
950         else
951                 priv->dbus_new_initialized = 1;
952
953         return ret;
954 }
955
956
957 /**
958  * wpas_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface for
959  * wpa_supplicant
960  * @iface: Pointer to dbus private data from wpas_dbus_init()
961  *
962  * Deinitialize the dbus control interface that was initialized with
963  * wpas_dbus_ctrl_iface_init().
964  */
965 void wpas_dbus_ctrl_iface_deinit(struct wpas_dbus_priv *iface)
966 {
967         if (!iface->dbus_new_initialized)
968                 return;
969         wpa_printf(MSG_DEBUG, "dbus: Unregister D-Bus object '%s'",
970                    WPAS_DBUS_NEW_PATH);
971         dbus_connection_unregister_object_path(iface->con,
972                                                WPAS_DBUS_NEW_PATH);
973 }
974
975
976 static void wpa_dbus_free(void *ptr)
977 {
978         os_free(ptr);
979 }
980
981
982 static const struct wpa_dbus_property_desc wpas_dbus_network_properties[] = {
983         { "Properties", WPAS_DBUS_NEW_IFACE_NETWORK, "a{sv}",
984           (WPADBusPropertyAccessor) wpas_dbus_getter_network_properties,
985           (WPADBusPropertyAccessor) wpas_dbus_setter_network_properties,
986           RW
987         },
988         { "Enabled", WPAS_DBUS_NEW_IFACE_NETWORK, "b",
989           (WPADBusPropertyAccessor) wpas_dbus_getter_enabled,
990           (WPADBusPropertyAccessor) wpas_dbus_setter_enabled,
991           RW
992         },
993         { NULL, NULL, NULL, NULL, NULL, 0 }
994 };
995
996
997 static const struct wpa_dbus_signal_desc wpas_dbus_network_signals[] = {
998         { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_NETWORK,
999           {
1000                   { "properties", "a{sv}", ARG_OUT },
1001                   END_ARGS
1002           }
1003         },
1004         { NULL, NULL, { END_ARGS } }
1005 };
1006
1007
1008 /**
1009  * wpas_dbus_register_network - Register a configured network with dbus
1010  * @wpa_s: wpa_supplicant interface structure
1011  * @ssid: network configuration data
1012  * Returns: 0 on success, -1 on failure
1013  *
1014  * Registers network representing object with dbus
1015  */
1016 int wpas_dbus_register_network(struct wpa_supplicant *wpa_s,
1017                                struct wpa_ssid *ssid)
1018 {
1019         struct wpas_dbus_priv *ctrl_iface;
1020         struct wpa_dbus_object_desc *obj_desc;
1021         struct network_handler_args *arg;
1022         char net_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
1023
1024         /* Do nothing if the control interface is not turned on */
1025         if (wpa_s == NULL || wpa_s->global == NULL)
1026                 return 0;
1027         ctrl_iface = wpa_s->global->dbus;
1028         if (ctrl_iface == NULL)
1029                 return 0;
1030
1031         os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1032                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
1033                     wpa_s->dbus_new_path, ssid->id);
1034
1035         wpa_printf(MSG_DEBUG, "dbus: Register network object '%s'",
1036                    net_obj_path);
1037         obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
1038         if (!obj_desc) {
1039                 wpa_printf(MSG_ERROR, "Not enough memory "
1040                            "to create object description");
1041                 goto err;
1042         }
1043
1044         /* allocate memory for handlers arguments */
1045         arg = os_zalloc(sizeof(struct network_handler_args));
1046         if (!arg) {
1047                 wpa_printf(MSG_ERROR, "Not enough memory "
1048                            "to create arguments for method");
1049                 goto err;
1050         }
1051
1052         arg->wpa_s = wpa_s;
1053         arg->ssid = ssid;
1054
1055         wpas_dbus_register(obj_desc, arg, wpa_dbus_free, NULL,
1056                            wpas_dbus_network_properties,
1057                            wpas_dbus_network_signals);
1058
1059         if (wpa_dbus_register_object_per_iface(ctrl_iface, net_obj_path,
1060                                                wpa_s->ifname, obj_desc))
1061                 goto err;
1062
1063         wpas_dbus_signal_network_added(wpa_s, ssid->id);
1064
1065         return 0;
1066
1067 err:
1068         free_dbus_object_desc(obj_desc);
1069         return -1;
1070 }
1071
1072
1073 /**
1074  * wpas_dbus_unregister_network - Unregister a configured network from dbus
1075  * @wpa_s: wpa_supplicant interface structure
1076  * @nid: network id
1077  * Returns: 0 on success, -1 on failure
1078  *
1079  * Unregisters network representing object from dbus
1080  */
1081 int wpas_dbus_unregister_network(struct wpa_supplicant *wpa_s, int nid)
1082 {
1083         struct wpas_dbus_priv *ctrl_iface;
1084         char net_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
1085         int ret;
1086
1087         /* Do nothing if the control interface is not turned on */
1088         if (wpa_s == NULL || wpa_s->global == NULL ||
1089             wpa_s->dbus_new_path == NULL)
1090                 return 0;
1091         ctrl_iface = wpa_s->global->dbus;
1092         if (ctrl_iface == NULL)
1093                 return 0;
1094
1095         os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1096                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
1097                     wpa_s->dbus_new_path, nid);
1098
1099         wpa_printf(MSG_DEBUG, "dbus: Unregister network object '%s'",
1100                    net_obj_path);
1101         ret = wpa_dbus_unregister_object_per_iface(ctrl_iface, net_obj_path);
1102
1103         if (!ret)
1104                 wpas_dbus_signal_network_removed(wpa_s, nid);
1105
1106         return ret;
1107 }
1108
1109
1110 static const struct wpa_dbus_property_desc wpas_dbus_bss_properties[] = {
1111         { "SSID", WPAS_DBUS_NEW_IFACE_BSS, "ay",
1112           (WPADBusPropertyAccessor) wpas_dbus_getter_bss_ssid,
1113           NULL,
1114           R
1115         },
1116         { "BSSID", WPAS_DBUS_NEW_IFACE_BSS, "ay",
1117           (WPADBusPropertyAccessor) wpas_dbus_getter_bss_bssid,
1118           NULL,
1119           R
1120         },
1121         { "Privacy", WPAS_DBUS_NEW_IFACE_BSS, "b",
1122           (WPADBusPropertyAccessor) wpas_dbus_getter_bss_privacy,
1123           NULL,
1124           R
1125         },
1126         { "Mode", WPAS_DBUS_NEW_IFACE_BSS, "s",
1127           (WPADBusPropertyAccessor) wpas_dbus_getter_bss_mode,
1128           NULL,
1129           R
1130         },
1131         { "Signal", WPAS_DBUS_NEW_IFACE_BSS, "n",
1132           (WPADBusPropertyAccessor) wpas_dbus_getter_bss_signal,
1133           NULL,
1134           R
1135         },
1136         { "Frequency", WPAS_DBUS_NEW_IFACE_BSS, "q",
1137           (WPADBusPropertyAccessor) wpas_dbus_getter_bss_frequency,
1138           NULL,
1139           R
1140         },
1141         { "Rates", WPAS_DBUS_NEW_IFACE_BSS, "au",
1142           (WPADBusPropertyAccessor) wpas_dbus_getter_bss_rates,
1143           NULL,
1144           R
1145         },
1146         { "WPA", WPAS_DBUS_NEW_IFACE_BSS, "a{sv}",
1147           (WPADBusPropertyAccessor) wpas_dbus_getter_bss_wpa,
1148           NULL,
1149           R
1150         },
1151         { "RSN", WPAS_DBUS_NEW_IFACE_BSS, "a{sv}",
1152           (WPADBusPropertyAccessor) wpas_dbus_getter_bss_rsn,
1153           NULL,
1154           R
1155         },
1156         { "IEs", WPAS_DBUS_NEW_IFACE_BSS, "ay",
1157           (WPADBusPropertyAccessor) wpas_dbus_getter_bss_ies,
1158           NULL,
1159           R
1160         },
1161         { NULL, NULL, NULL, NULL, NULL, 0 }
1162 };
1163
1164
1165 static const struct wpa_dbus_signal_desc wpas_dbus_bss_signals[] = {
1166         { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_BSS,
1167           {
1168                   { "properties", "a{sv}", ARG_OUT },
1169                   END_ARGS
1170           }
1171         },
1172         { NULL, NULL, { END_ARGS } }
1173 };
1174
1175
1176 /**
1177  * wpas_dbus_unregister_bss - Unregister a scanned BSS from dbus
1178  * @wpa_s: wpa_supplicant interface structure
1179  * @bssid: scanned network bssid
1180  * @id: unique BSS identifier
1181  * Returns: 0 on success, -1 on failure
1182  *
1183  * Unregisters BSS representing object from dbus
1184  */
1185 int wpas_dbus_unregister_bss(struct wpa_supplicant *wpa_s,
1186                              u8 bssid[ETH_ALEN], unsigned int id)
1187 {
1188         struct wpas_dbus_priv *ctrl_iface;
1189         char bss_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
1190
1191         /* Do nothing if the control interface is not turned on */
1192         if (wpa_s == NULL || wpa_s->global == NULL)
1193                 return 0;
1194         ctrl_iface = wpa_s->global->dbus;
1195         if (ctrl_iface == NULL)
1196                 return 0;
1197
1198         os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1199                     "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
1200                     wpa_s->dbus_new_path, id);
1201
1202         wpa_printf(MSG_DEBUG, "dbus: Unregister BSS object '%s'",
1203                    bss_obj_path);
1204         if (wpa_dbus_unregister_object_per_iface(ctrl_iface, bss_obj_path)) {
1205                 wpa_printf(MSG_ERROR, "dbus: Cannot unregister BSS object %s",
1206                            bss_obj_path);
1207                 return -1;
1208         }
1209
1210         wpas_dbus_signal_bss_removed(wpa_s, bss_obj_path);
1211         wpas_dbus_signal_prop_changed(wpa_s, WPAS_DBUS_PROP_BSSS);
1212
1213         return 0;
1214 }
1215
1216
1217 /**
1218  * wpas_dbus_register_bss - Register a scanned BSS with dbus
1219  * @wpa_s: wpa_supplicant interface structure
1220  * @bssid: scanned network bssid
1221  * @id: unique BSS identifier
1222  * Returns: 0 on success, -1 on failure
1223  *
1224  * Registers BSS representing object with dbus
1225  */
1226 int wpas_dbus_register_bss(struct wpa_supplicant *wpa_s,
1227                            u8 bssid[ETH_ALEN], unsigned int id)
1228 {
1229         struct wpas_dbus_priv *ctrl_iface;
1230         struct wpa_dbus_object_desc *obj_desc;
1231         char bss_obj_path[WPAS_DBUS_OBJECT_PATH_MAX];
1232         struct bss_handler_args *arg;
1233
1234         /* Do nothing if the control interface is not turned on */
1235         if (wpa_s == NULL || wpa_s->global == NULL)
1236                 return 0;
1237         ctrl_iface = wpa_s->global->dbus;
1238         if (ctrl_iface == NULL)
1239                 return 0;
1240
1241         os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
1242                     "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
1243                     wpa_s->dbus_new_path, id);
1244
1245         obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
1246         if (!obj_desc) {
1247                 wpa_printf(MSG_ERROR, "Not enough memory "
1248                            "to create object description");
1249                 goto err;
1250         }
1251
1252         arg = os_zalloc(sizeof(struct bss_handler_args));
1253         if (!arg) {
1254                 wpa_printf(MSG_ERROR, "Not enough memory "
1255                            "to create arguments for handler");
1256                 goto err;
1257         }
1258         arg->wpa_s = wpa_s;
1259         arg->id = id;
1260
1261         wpas_dbus_register(obj_desc, arg, wpa_dbus_free, NULL,
1262                            wpas_dbus_bss_properties,
1263                            wpas_dbus_bss_signals);
1264
1265         wpa_printf(MSG_DEBUG, "dbus: Register BSS object '%s'",
1266                    bss_obj_path);
1267         if (wpa_dbus_register_object_per_iface(ctrl_iface, bss_obj_path,
1268                                                wpa_s->ifname, obj_desc)) {
1269                 wpa_printf(MSG_ERROR,
1270                            "Cannot register BSSID dbus object %s.",
1271                            bss_obj_path);
1272                 goto err;
1273         }
1274
1275         wpas_dbus_signal_bss_added(wpa_s, bss_obj_path);
1276         wpas_dbus_signal_prop_changed(wpa_s, WPAS_DBUS_PROP_BSSS);
1277
1278         return 0;
1279
1280 err:
1281         free_dbus_object_desc(obj_desc);
1282         return -1;
1283 }
1284
1285
1286 static const struct wpa_dbus_method_desc wpas_dbus_interface_methods[] = {
1287         { "Scan", WPAS_DBUS_NEW_IFACE_INTERFACE,
1288           (WPADBusMethodHandler) &wpas_dbus_handler_scan,
1289           {
1290                   { "args", "a{sv}", ARG_IN },
1291                   END_ARGS
1292           }
1293         },
1294         { "Disconnect", WPAS_DBUS_NEW_IFACE_INTERFACE,
1295           (WPADBusMethodHandler) &wpas_dbus_handler_disconnect,
1296           {
1297                   END_ARGS
1298           }
1299         },
1300         { "AddNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE,
1301           (WPADBusMethodHandler) &wpas_dbus_handler_add_network,
1302           {
1303                   { "args", "a{sv}", ARG_IN },
1304                   { "path", "o", ARG_OUT },
1305                   END_ARGS
1306           }
1307         },
1308         { "RemoveNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE,
1309           (WPADBusMethodHandler) &wpas_dbus_handler_remove_network,
1310           {
1311                   { "path", "o", ARG_IN },
1312                   END_ARGS
1313           }
1314         },
1315         { "SelectNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE,
1316           (WPADBusMethodHandler) &wpas_dbus_handler_select_network,
1317           {
1318                   { "path", "o", ARG_IN },
1319                   END_ARGS
1320           }
1321         },
1322         { "AddBlob", WPAS_DBUS_NEW_IFACE_INTERFACE,
1323           (WPADBusMethodHandler) &wpas_dbus_handler_add_blob,
1324           {
1325                   { "name", "s", ARG_IN },
1326                   { "data", "ay", ARG_IN },
1327                   END_ARGS
1328           }
1329         },
1330         { "GetBlob", WPAS_DBUS_NEW_IFACE_INTERFACE,
1331           (WPADBusMethodHandler) &wpas_dbus_handler_get_blob,
1332           {
1333                   { "name", "s", ARG_IN },
1334                   { "data", "ay", ARG_OUT },
1335                   END_ARGS
1336           }
1337         },
1338         { "RemoveBlob", WPAS_DBUS_NEW_IFACE_INTERFACE,
1339           (WPADBusMethodHandler) &wpas_dbus_handler_remove_blob,
1340           {
1341                   { "name", "s", ARG_IN },
1342                   END_ARGS
1343           }
1344         },
1345 #ifdef CONFIG_WPS
1346         { "Start", WPAS_DBUS_NEW_IFACE_WPS,
1347           (WPADBusMethodHandler) &wpas_dbus_handler_wps_start,
1348           {
1349                   { "args", "a{sv}", ARG_IN },
1350                   { "output", "a{sv}", ARG_OUT },
1351                   END_ARGS
1352           }
1353         },
1354 #endif /* CONFIG_WPS */
1355         { NULL, NULL, NULL, { END_ARGS } }
1356 };
1357
1358 static const struct wpa_dbus_property_desc wpas_dbus_interface_properties[] = {
1359         { "Capabilities", WPAS_DBUS_NEW_IFACE_INTERFACE, "a{sv}",
1360           (WPADBusPropertyAccessor) wpas_dbus_getter_capabilities,
1361           NULL, R
1362         },
1363         { "State", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
1364           (WPADBusPropertyAccessor) wpas_dbus_getter_state,
1365           NULL, R
1366         },
1367         { "Scanning", WPAS_DBUS_NEW_IFACE_INTERFACE, "b",
1368           (WPADBusPropertyAccessor) wpas_dbus_getter_scanning,
1369           NULL, R
1370         },
1371         { "ApScan", WPAS_DBUS_NEW_IFACE_INTERFACE, "u",
1372           (WPADBusPropertyAccessor) wpas_dbus_getter_ap_scan,
1373           (WPADBusPropertyAccessor) wpas_dbus_setter_ap_scan,
1374           RW
1375         },
1376         { "Ifname", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
1377           (WPADBusPropertyAccessor) wpas_dbus_getter_ifname,
1378           NULL, R
1379         },
1380         { "Driver", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
1381           (WPADBusPropertyAccessor) wpas_dbus_getter_driver,
1382           NULL, R
1383         },
1384         { "BridgeIfname", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
1385           (WPADBusPropertyAccessor) wpas_dbus_getter_bridge_ifname,
1386           NULL, R
1387         },
1388         { "CurrentBSS", WPAS_DBUS_NEW_IFACE_INTERFACE, "o",
1389           (WPADBusPropertyAccessor) wpas_dbus_getter_current_bss,
1390           NULL, R
1391         },
1392         { "CurrentNetwork", WPAS_DBUS_NEW_IFACE_INTERFACE, "o",
1393           (WPADBusPropertyAccessor) wpas_dbus_getter_current_network,
1394           NULL, R
1395         },
1396         { "CurrentAuthMode", WPAS_DBUS_NEW_IFACE_INTERFACE, "s",
1397           (WPADBusPropertyAccessor) wpas_dbus_getter_current_auth_mode,
1398           NULL, R
1399         },
1400         { "Blobs", WPAS_DBUS_NEW_IFACE_INTERFACE, "a{say}",
1401           (WPADBusPropertyAccessor) wpas_dbus_getter_blobs,
1402           NULL, R
1403         },
1404         { "BSSs", WPAS_DBUS_NEW_IFACE_INTERFACE, "ao",
1405           (WPADBusPropertyAccessor) wpas_dbus_getter_bsss,
1406           NULL, R
1407         },
1408         { "Networks", WPAS_DBUS_NEW_IFACE_INTERFACE, "ao",
1409           (WPADBusPropertyAccessor) wpas_dbus_getter_networks,
1410           NULL, R
1411         },
1412 #ifdef CONFIG_WPS
1413         { "ProcessCredentials", WPAS_DBUS_NEW_IFACE_WPS, "b",
1414           (WPADBusPropertyAccessor) wpas_dbus_getter_process_credentials,
1415           (WPADBusPropertyAccessor) wpas_dbus_setter_process_credentials,
1416           RW
1417         },
1418 #endif /* CONFIG_WPS */
1419         { NULL, NULL, NULL, NULL, NULL, 0 }
1420 };
1421
1422 static const struct wpa_dbus_signal_desc wpas_dbus_interface_signals[] = {
1423         { "ScanDone", WPAS_DBUS_NEW_IFACE_INTERFACE,
1424           {
1425                   { "success", "b", ARG_OUT },
1426                   END_ARGS
1427           }
1428         },
1429         { "BSSAdded", WPAS_DBUS_NEW_IFACE_INTERFACE,
1430           {
1431                   { "path", "o", ARG_OUT },
1432                   { "properties", "a{sv}", ARG_OUT },
1433                   END_ARGS
1434           }
1435         },
1436         { "BSSRemoved", WPAS_DBUS_NEW_IFACE_INTERFACE,
1437           {
1438                   { "path", "o", ARG_OUT },
1439                   END_ARGS
1440           }
1441         },
1442         { "BlobAdded", WPAS_DBUS_NEW_IFACE_INTERFACE,
1443           {
1444                   { "name", "s", ARG_OUT },
1445                   END_ARGS
1446           }
1447         },
1448         { "BlobRemoved", WPAS_DBUS_NEW_IFACE_INTERFACE,
1449           {
1450                   { "name", "s", ARG_OUT },
1451                   END_ARGS
1452           }
1453         },
1454         { "NetworkAdded", WPAS_DBUS_NEW_IFACE_INTERFACE,
1455           {
1456                   { "path", "o", ARG_OUT },
1457                   { "properties", "a{sv}", ARG_OUT },
1458                   END_ARGS
1459           }
1460         },
1461         { "NetworkRemoved", WPAS_DBUS_NEW_IFACE_INTERFACE,
1462           {
1463                   { "path", "o", ARG_OUT },
1464                   END_ARGS
1465           }
1466         },
1467         { "NetworkSelected", WPAS_DBUS_NEW_IFACE_INTERFACE,
1468           {
1469                   { "path", "o", ARG_OUT },
1470                   END_ARGS
1471           }
1472         },
1473         { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_INTERFACE,
1474           {
1475                   { "properties", "a{sv}", ARG_OUT },
1476                   END_ARGS
1477           }
1478         },
1479 #ifdef CONFIG_WPS
1480         { "Event", WPAS_DBUS_NEW_IFACE_WPS,
1481           {
1482                   { "name", "s", ARG_OUT },
1483                   { "args", "a{sv}", ARG_OUT },
1484                   END_ARGS
1485           }
1486         },
1487         { "Credentials", WPAS_DBUS_NEW_IFACE_WPS,
1488           {
1489                   { "credentials", "a{sv}", ARG_OUT },
1490                   END_ARGS
1491           }
1492         },
1493         { "PropertiesChanged", WPAS_DBUS_NEW_IFACE_WPS,
1494           {
1495                   { "properties", "a{sv}", ARG_OUT },
1496                   END_ARGS
1497           }
1498         },
1499 #endif /* CONFIG_WPS */
1500         { NULL, NULL, { END_ARGS } }
1501 };
1502
1503
1504 int wpas_dbus_register_interface(struct wpa_supplicant *wpa_s)
1505 {
1506
1507         struct wpa_dbus_object_desc *obj_desc = NULL;
1508         struct wpas_dbus_priv *ctrl_iface = wpa_s->global->dbus;
1509         int next;
1510
1511         /* Do nothing if the control interface is not turned on */
1512         if (ctrl_iface == NULL)
1513                 return 0;
1514
1515         /* Create and set the interface's object path */
1516         wpa_s->dbus_new_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
1517         if (wpa_s->dbus_new_path == NULL)
1518                 return -1;
1519         next = ctrl_iface->next_objid++;
1520         os_snprintf(wpa_s->dbus_new_path, WPAS_DBUS_OBJECT_PATH_MAX,
1521                     WPAS_DBUS_NEW_PATH_INTERFACES "/%u",
1522                     next);
1523
1524         obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
1525         if (!obj_desc) {
1526                 wpa_printf(MSG_ERROR, "Not enough memory "
1527                            "to create object description");
1528                 goto err;
1529         }
1530
1531         wpas_dbus_register(obj_desc, wpa_s, NULL, wpas_dbus_interface_methods,
1532                            wpas_dbus_interface_properties,
1533                            wpas_dbus_interface_signals);
1534
1535         wpa_printf(MSG_DEBUG, "dbus: Register interface object '%s'",
1536                    wpa_s->dbus_new_path);
1537         if (wpa_dbus_register_object_per_iface(ctrl_iface,
1538                                                wpa_s->dbus_new_path,
1539                                                wpa_s->ifname, obj_desc))
1540                 goto err;
1541
1542         wpas_dbus_signal_interface_added(wpa_s);
1543
1544         return 0;
1545
1546 err:
1547         os_free(wpa_s->dbus_new_path);
1548         wpa_s->dbus_new_path = NULL;
1549         free_dbus_object_desc(obj_desc);
1550         return -1;
1551 }
1552
1553
1554 int wpas_dbus_unregister_interface(struct wpa_supplicant *wpa_s)
1555 {
1556         struct wpas_dbus_priv *ctrl_iface;
1557
1558         /* Do nothing if the control interface is not turned on */
1559         if (wpa_s == NULL || wpa_s->global == NULL)
1560                 return 0;
1561         ctrl_iface = wpa_s->global->dbus;
1562         if (ctrl_iface == NULL)
1563                 return 0;
1564
1565         wpa_printf(MSG_DEBUG, "dbus: Unregister interface object '%s'",
1566                    wpa_s->dbus_new_path);
1567         if (wpa_dbus_unregister_object_per_iface(ctrl_iface,
1568                                                  wpa_s->dbus_new_path))
1569                 return -1;
1570
1571         wpas_dbus_signal_interface_removed(wpa_s);
1572
1573         os_free(wpa_s->dbus_new_path);
1574         wpa_s->dbus_new_path = NULL;
1575
1576         return 0;
1577 }