9cdf64996fa4bc6e0ee8d214fa9152e0797b2401
[libeap.git] / wpa_supplicant / wpa_gui-qt4 / peers.cpp
1 /*
2  * wpa_gui - Peers class
3  * Copyright (c) 2009, Atheros Communications
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include <cstdio>
16 #include <QImageReader>
17 #include <QMessageBox>
18
19 #include "common/wpa_ctrl.h"
20 #include "wpagui.h"
21 #include "stringquery.h"
22 #include "peers.h"
23
24
25 enum {
26         peer_role_address = Qt::UserRole + 1,
27         peer_role_type,
28         peer_role_uuid,
29         peer_role_details,
30         peer_role_pri_dev_type,
31         peer_role_ssid,
32         peer_role_config_methods,
33         peer_role_dev_passwd_id,
34         peer_role_bss_id
35 };
36
37 /*
38  * TODO:
39  * - add current AP info (e.g., from WPS) in station mode
40  */
41
42 enum peer_type {
43         PEER_TYPE_ASSOCIATED_STATION,
44         PEER_TYPE_AP,
45         PEER_TYPE_AP_WPS,
46         PEER_TYPE_WPS_PIN_NEEDED,
47         PEER_TYPE_WPS_ER_AP,
48         PEER_TYPE_WPS_ER_AP_UNCONFIGURED,
49         PEER_TYPE_WPS_ER_ENROLLEE,
50         PEER_TYPE_WPS_ENROLLEE
51 };
52
53
54 Peers::Peers(QWidget *parent, const char *, bool, Qt::WFlags)
55         : QDialog(parent)
56 {
57         setupUi(this);
58
59         if (QImageReader::supportedImageFormats().contains(QByteArray("svg")))
60         {
61                 default_icon = new QIcon(":/icons/wpa_gui.svg");
62                 ap_icon = new QIcon(":/icons/ap.svg");
63                 laptop_icon = new QIcon(":/icons/laptop.svg");
64         } else {
65                 default_icon = new QIcon(":/icons/wpa_gui.png");
66                 ap_icon = new QIcon(":/icons/ap.png");
67                 laptop_icon = new QIcon(":/icons/laptop.png");
68         }
69
70         peers->setModel(&model);
71         peers->setResizeMode(QListView::Adjust);
72
73         peers->setContextMenuPolicy(Qt::CustomContextMenu);
74         connect(peers, SIGNAL(customContextMenuRequested(const QPoint &)),
75                 this, SLOT(context_menu(const QPoint &)));
76
77         wpagui = NULL;
78 }
79
80
81 void Peers::setWpaGui(WpaGui *_wpagui)
82 {
83         wpagui = _wpagui;
84         update_peers();
85 }
86
87
88 Peers::~Peers()
89 {
90         delete default_icon;
91         delete ap_icon;
92         delete laptop_icon;
93 }
94
95
96 void Peers::languageChange()
97 {
98         retranslateUi(this);
99 }
100
101
102 QString Peers::ItemType(int type)
103 {
104         QString title;
105         switch (type) {
106         case PEER_TYPE_ASSOCIATED_STATION:
107                 title = tr("Associated station");
108                 break;
109         case PEER_TYPE_AP:
110                 title = tr("AP");
111                 break;
112         case PEER_TYPE_AP_WPS:
113                 title = tr("WPS AP");
114                 break;
115         case PEER_TYPE_WPS_PIN_NEEDED:
116                 title = tr("WPS PIN needed");
117                 break;
118         case PEER_TYPE_WPS_ER_AP:
119                 title = tr("ER: WPS AP");
120                 break;
121         case PEER_TYPE_WPS_ER_AP_UNCONFIGURED:
122                 title = tr("ER: WPS AP (Unconfigured)");
123                 break;
124         case PEER_TYPE_WPS_ER_ENROLLEE:
125                 title = tr("ER: WPS Enrollee");
126                 break;
127         case PEER_TYPE_WPS_ENROLLEE:
128                 title = tr("WPS Enrollee");
129                 break;
130         }
131         return title;
132 }
133
134
135 void Peers::context_menu(const QPoint &pos)
136 {
137         QMenu *menu = new QMenu;
138         if (menu == NULL)
139                 return;
140
141         QModelIndex idx = peers->indexAt(pos);
142         if (idx.isValid()) {
143                 ctx_item = model.itemFromIndex(idx);
144                 int type = ctx_item->data(peer_role_type).toInt();
145                 menu->addAction(Peers::ItemType(type))->setEnabled(false);
146                 menu->addSeparator();
147
148                 int config_methods = -1;
149                 QVariant var = ctx_item->data(peer_role_config_methods);
150                 if (var.isValid())
151                         config_methods = var.toInt();
152
153                 if ((type == PEER_TYPE_ASSOCIATED_STATION ||
154                      type == PEER_TYPE_AP_WPS ||
155                      type == PEER_TYPE_WPS_PIN_NEEDED ||
156                      type == PEER_TYPE_WPS_ER_ENROLLEE ||
157                      type == PEER_TYPE_WPS_ENROLLEE) &&
158                     (config_methods == -1 || (config_methods & 0x010c))) {
159                         menu->addAction(tr("Enter WPS PIN"), this,
160                                         SLOT(enter_pin()));
161                 }
162
163                 if (type == PEER_TYPE_AP_WPS) {
164                         menu->addAction(tr("Connect (PBC)"), this,
165                                         SLOT(connect_pbc()));
166                 }
167
168                 if ((type == PEER_TYPE_ASSOCIATED_STATION ||
169                      type == PEER_TYPE_WPS_ER_ENROLLEE ||
170                      type == PEER_TYPE_WPS_ENROLLEE) &&
171                     config_methods >= 0 && (config_methods & 0x0080)) {
172                         menu->addAction(tr("Enroll (PBC)"), this,
173                                         SLOT(connect_pbc()));
174                 }
175
176                 if (type == PEER_TYPE_WPS_ER_AP) {
177                         menu->addAction(tr("Learn Configuration"), this,
178                                         SLOT(learn_ap_config()));
179                 }
180
181                 menu->addAction(tr("Properties"), this, SLOT(properties()));
182         } else {
183                 ctx_item = NULL;
184                 menu->addAction(QString("Refresh"), this, SLOT(ctx_refresh()));
185         }
186
187         menu->exec(peers->mapToGlobal(pos));
188 }
189
190
191 void Peers::enter_pin()
192 {
193         if (ctx_item == NULL)
194                 return;
195
196         int peer_type = ctx_item->data(peer_role_type).toInt();
197         QString uuid;
198         QString addr;
199         if (peer_type == PEER_TYPE_WPS_ER_ENROLLEE)
200                 uuid = ctx_item->data(peer_role_uuid).toString();
201         else
202                 addr = ctx_item->data(peer_role_address).toString();
203
204         StringQuery input(tr("PIN:"));
205         input.setWindowTitle(tr("PIN for ") + ctx_item->text());
206         if (input.exec() != QDialog::Accepted)
207                 return;
208
209         char cmd[100];
210         char reply[100];
211         size_t reply_len;
212
213         if (peer_type == PEER_TYPE_WPS_ER_ENROLLEE) {
214                 snprintf(cmd, sizeof(cmd), "WPS_ER_PIN %s %s",
215                          uuid.toAscii().constData(),
216                          input.get_string().toAscii().constData());
217         } else {
218                 snprintf(cmd, sizeof(cmd), "WPS_PIN %s %s",
219                          addr.toAscii().constData(),
220                          input.get_string().toAscii().constData());
221         }
222         reply_len = sizeof(reply) - 1;
223         if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0) {
224                 QMessageBox msg;
225                 msg.setIcon(QMessageBox::Warning);
226                 msg.setText("Failed to set the WPS PIN.");
227                 msg.exec();
228         }
229 }
230
231
232 void Peers::ctx_refresh()
233 {
234         update_peers();
235 }
236
237
238 void Peers::add_station(QString info)
239 {
240         QStringList lines = info.split(QRegExp("\\n"));
241         QString name;
242
243         for (QStringList::Iterator it = lines.begin();
244              it != lines.end(); it++) {
245                 int pos = (*it).indexOf('=') + 1;
246                 if (pos < 1)
247                         continue;
248
249                 if ((*it).startsWith("wpsDeviceName="))
250                         name = (*it).mid(pos);
251         }
252
253         if (name.isEmpty())
254                 name = lines[0];
255
256         QStandardItem *item = new QStandardItem(*laptop_icon, name);
257         if (item) {
258                 item->setData(lines[0], peer_role_address);
259                 item->setData(PEER_TYPE_ASSOCIATED_STATION,
260                               peer_role_type);
261                 item->setData(info, peer_role_details);
262                 item->setToolTip(ItemType(PEER_TYPE_ASSOCIATED_STATION));
263                 model.appendRow(item);
264         }
265 }
266
267
268 void Peers::add_stations()
269 {
270         char reply[2048];
271         size_t reply_len;
272         char cmd[30];
273         int res;
274
275         reply_len = sizeof(reply) - 1;
276         if (wpagui->ctrlRequest("STA-FIRST", reply, &reply_len) < 0)
277                 return;
278
279         do {
280                 reply[reply_len] = '\0';
281                 QString info(reply);
282                 char *txt = reply;
283                 while (*txt != '\0' && *txt != '\n')
284                         txt++;
285                 *txt++ = '\0';
286                 if (strncmp(reply, "FAIL", 4) == 0 ||
287                     strncmp(reply, "UNKNOWN", 7) == 0)
288                         break;
289
290                 add_station(info);
291
292                 reply_len = sizeof(reply) - 1;
293                 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", reply);
294                 res = wpagui->ctrlRequest(cmd, reply, &reply_len);
295         } while (res >= 0);
296 }
297
298
299 void Peers::add_single_station(const char *addr)
300 {
301         char reply[2048];
302         size_t reply_len;
303         char cmd[30];
304
305         reply_len = sizeof(reply) - 1;
306         snprintf(cmd, sizeof(cmd), "STA %s", addr);
307         if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0)
308                 return;
309
310         reply[reply_len] = '\0';
311         QString info(reply);
312         char *txt = reply;
313         while (*txt != '\0' && *txt != '\n')
314                 txt++;
315         *txt++ = '\0';
316         if (strncmp(reply, "FAIL", 4) == 0 ||
317             strncmp(reply, "UNKNOWN", 7) == 0)
318                 return;
319
320         add_station(info);
321 }
322
323
324 void Peers::remove_bss(int id)
325 {
326         if (model.rowCount() == 0)
327                 return;
328
329         QModelIndexList lst = model.match(model.index(0, 0), peer_role_bss_id,
330                                           id);
331         if (lst.size() == 0)
332                 return;
333         model.removeRow(lst[0].row());
334 }
335
336
337 void Peers::add_bss(const char *cmd)
338 {
339         char reply[2048];
340         size_t reply_len;
341
342         reply_len = sizeof(reply) - 1;
343         if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0)
344                 return;
345         reply[reply_len] = '\0';
346
347         QString bss(reply);
348         if (bss.isEmpty() || bss.startsWith("FAIL"))
349                 return;
350
351         QString ssid, bssid, flags, wps_name, pri_dev_type;
352         int id = -1;
353
354         QStringList lines = bss.split(QRegExp("\\n"));
355         for (QStringList::Iterator it = lines.begin();
356              it != lines.end(); it++) {
357                 int pos = (*it).indexOf('=') + 1;
358                 if (pos < 1)
359                         continue;
360
361                 if ((*it).startsWith("bssid="))
362                         bssid = (*it).mid(pos);
363                 else if ((*it).startsWith("id="))
364                         id = (*it).mid(pos).toInt();
365                 else if ((*it).startsWith("flags="))
366                         flags = (*it).mid(pos);
367                 else if ((*it).startsWith("ssid="))
368                         ssid = (*it).mid(pos);
369                 else if ((*it).startsWith("wps_device_name="))
370                         wps_name = (*it).mid(pos);
371                 else if ((*it).startsWith("wps_primary_device_type="))
372                         pri_dev_type = (*it).mid(pos);
373         }
374
375         QString name = wps_name;
376         if (name.isEmpty())
377                 name = ssid + "\n" + bssid;
378
379         QStandardItem *item = new QStandardItem(*ap_icon, name);
380         if (item) {
381                 item->setData(bssid, peer_role_address);
382                 if (id >= 0)
383                         item->setData(id, peer_role_bss_id);
384                 int type;
385                 if (flags.contains("[WPS"))
386                         type = PEER_TYPE_AP_WPS;
387                 else
388                         type = PEER_TYPE_AP;
389                 item->setData(type, peer_role_type);
390
391                 for (int i = 0; i < lines.size(); i++) {
392                         if (lines[i].length() > 60) {
393                                 lines[i].remove(60, lines[i].length());
394                                 lines[i] += "..";
395                         }
396                 }
397                 item->setToolTip(ItemType(type));
398                 item->setData(lines.join("\n"), peer_role_details);
399                 if (!pri_dev_type.isEmpty())
400                         item->setData(pri_dev_type,
401                                       peer_role_pri_dev_type);
402                 if (!ssid.isEmpty())
403                         item->setData(ssid, peer_role_ssid);
404                 model.appendRow(item);
405         }
406 }
407
408
409 void Peers::add_scan_results()
410 {
411         int index;
412         char cmd[20];
413
414         index = 0;
415         while (wpagui) {
416                 snprintf(cmd, sizeof(cmd), "BSS %d", index++);
417                 if (index > 1000)
418                         break;
419
420                 add_bss(cmd);
421         }
422 }
423
424
425 void Peers::update_peers()
426 {
427         model.clear();
428         if (wpagui == NULL)
429                 return;
430
431         char reply[20];
432         size_t replylen = sizeof(reply) - 1;
433         wpagui->ctrlRequest("WPS_ER_START", reply, &replylen);
434
435         add_stations();
436         add_scan_results();
437 }
438
439
440 QStandardItem * Peers::find_addr(QString addr)
441 {
442         if (model.rowCount() == 0)
443                 return NULL;
444
445         QModelIndexList lst = model.match(model.index(0, 0), peer_role_address,
446                                           addr);
447         if (lst.size() == 0)
448                 return NULL;
449         return model.itemFromIndex(lst[0]);
450 }
451
452
453 QStandardItem * Peers::find_uuid(QString uuid)
454 {
455         if (model.rowCount() == 0)
456                 return NULL;
457
458         QModelIndexList lst = model.match(model.index(0, 0), peer_role_uuid,
459                                           uuid);
460         if (lst.size() == 0)
461                 return NULL;
462         return model.itemFromIndex(lst[0]);
463 }
464
465
466 void Peers::event_notify(WpaMsg msg)
467 {
468         QString text = msg.getMsg();
469
470         if (text.startsWith(WPS_EVENT_PIN_NEEDED)) {
471                 /*
472                  * WPS-PIN-NEEDED 5a02a5fa-9199-5e7c-bc46-e183d3cb32f7
473                  * 02:2a:c4:18:5b:f3
474                  * [Wireless Client|Company|cmodel|123|12345|1-0050F204-1]
475                  */
476                 QStringList items = text.split(' ');
477                 QString uuid = items[1];
478                 QString addr = items[2];
479                 QString name = "";
480
481                 QStandardItem *item = find_addr(addr);
482                 if (item)
483                         return;
484
485                 int pos = text.indexOf('[');
486                 if (pos >= 0) {
487                         int pos2 = text.lastIndexOf(']');
488                         if (pos2 >= pos) {
489                                 items = text.mid(pos + 1, pos2 - pos - 1).
490                                         split('|');
491                                 name = items[0];
492                                 items.append(addr);
493                         }
494                 }
495
496                 item = new QStandardItem(*laptop_icon, name);
497                 if (item) {
498                         item->setData(addr, peer_role_address);
499                         item->setData(PEER_TYPE_WPS_PIN_NEEDED,
500                                       peer_role_type);
501                         item->setToolTip(ItemType(PEER_TYPE_WPS_PIN_NEEDED));
502                         item->setData(items.join("\n"), peer_role_details);
503                         item->setData(items[5], peer_role_pri_dev_type);
504                         model.appendRow(item);
505                 }
506                 return;
507         }
508
509         if (text.startsWith(AP_STA_CONNECTED)) {
510                 /* AP-STA-CONNECTED 02:2a:c4:18:5b:f3 */
511                 QStringList items = text.split(' ');
512                 QString addr = items[1];
513                 QStandardItem *item = find_addr(addr);
514                 if (item == NULL || item->data(peer_role_type).toInt() !=
515                     PEER_TYPE_ASSOCIATED_STATION)
516                         add_single_station(addr.toAscii().constData());
517                 return;
518         }
519
520         if (text.startsWith(AP_STA_DISCONNECTED)) {
521                 /* AP-STA-DISCONNECTED 02:2a:c4:18:5b:f3 */
522                 QStringList items = text.split(' ');
523                 QString addr = items[1];
524
525                 if (model.rowCount() == 0)
526                         return;
527
528                 QModelIndexList lst = model.match(model.index(0, 0),
529                                                   peer_role_address, addr);
530                 for (int i = 0; i < lst.size(); i++) {
531                         QStandardItem *item = model.itemFromIndex(lst[i]);
532                         if (item && item->data(peer_role_type).toInt() ==
533                             PEER_TYPE_ASSOCIATED_STATION)
534                                 model.removeRow(lst[i].row());
535                 }
536                 return;
537         }
538
539         if (text.startsWith(WPS_EVENT_ER_AP_ADD)) {
540                 /*
541                  * WPS-ER-AP-ADD 87654321-9abc-def0-1234-56789abc0002
542                  * 02:11:22:33:44:55 pri_dev_type=6-0050F204-1 wps_state=1
543                  * |Very friendly name|Company|Long description of the model|
544                  * WAP|http://w1.fi/|http://w1.fi/hostapd/
545                  */
546                 QStringList items = text.split(' ');
547                 if (items.size() < 5)
548                         return;
549                 QString uuid = items[1];
550                 QString addr = items[2];
551                 QString pri_dev_type = items[3].mid(13);
552                 int wps_state = items[4].mid(10).toInt();
553
554                 int pos = text.indexOf('|');
555                 if (pos < 0)
556                         return;
557                 items = text.mid(pos + 1).split('|');
558                 if (items.size() < 1)
559                         return;
560
561                 QStandardItem *item = find_uuid(uuid);
562                 if (item)
563                         return;
564
565                 item = new QStandardItem(*ap_icon, items[0]);
566                 if (item) {
567                         item->setData(uuid, peer_role_uuid);
568                         item->setData(addr, peer_role_address);
569                         int type = wps_state == 2 ? PEER_TYPE_WPS_ER_AP:
570                                 PEER_TYPE_WPS_ER_AP_UNCONFIGURED;
571                         item->setData(type, peer_role_type);
572                         item->setToolTip(ItemType(type));
573                         item->setData(pri_dev_type, peer_role_pri_dev_type);
574                         item->setData(items.join(QString("\n")),
575                                       peer_role_details);
576                         model.appendRow(item);
577                 }
578
579                 return;
580         }
581
582         if (text.startsWith(WPS_EVENT_ER_AP_REMOVE)) {
583                 /* WPS-ER-AP-REMOVE 87654321-9abc-def0-1234-56789abc0002 */
584                 QStringList items = text.split(' ');
585                 if (items.size() < 2)
586                         return;
587                 if (model.rowCount() == 0)
588                         return;
589
590                 QModelIndexList lst = model.match(model.index(0, 0),
591                                                   peer_role_uuid, items[1]);
592                 for (int i = 0; i < lst.size(); i++) {
593                         QStandardItem *item = model.itemFromIndex(lst[i]);
594                         if (item &&
595                             (item->data(peer_role_type).toInt() ==
596                              PEER_TYPE_WPS_ER_AP ||
597                              item->data(peer_role_type).toInt() ==
598                              PEER_TYPE_WPS_ER_AP_UNCONFIGURED))
599                                 model.removeRow(lst[i].row());
600                 }
601                 return;
602         }
603
604         if (text.startsWith(WPS_EVENT_ER_ENROLLEE_ADD)) {
605                 /*
606                  * WPS-ER-ENROLLEE-ADD 2b7093f1-d6fb-5108-adbb-bea66bb87333
607                  * 02:66:a0:ee:17:27 M1=1 config_methods=0x14d dev_passwd_id=0
608                  * pri_dev_type=1-0050F204-1
609                  * |Wireless Client|Company|cmodel|123|12345|
610                  */
611                 QStringList items = text.split(' ');
612                 if (items.size() < 3)
613                         return;
614                 QString uuid = items[1];
615                 QString addr = items[2];
616                 QString pri_dev_type = items[6].mid(13);
617                 int config_methods = -1;
618                 int dev_passwd_id = -1;
619
620                 for (int i = 3; i < items.size(); i++) {
621                         int pos = items[i].indexOf('=') + 1;
622                         if (pos < 1)
623                                 continue;
624                         QString val = items[i].mid(pos);
625                         if (items[i].startsWith("config_methods=")) {
626                                 config_methods = val.toInt(0, 0);
627                         } else if (items[i].startsWith("dev_passwd_id=")) {
628                                 dev_passwd_id = val.toInt();
629                         }
630                 }
631
632                 int pos = text.indexOf('|');
633                 if (pos < 0)
634                         return;
635                 items = text.mid(pos + 1).split('|');
636                 if (items.size() < 1)
637                         return;
638                 QString name = items[0];
639                 if (name.length() == 0)
640                         name = addr;
641
642                 remove_enrollee_uuid(uuid);
643
644                 QStandardItem *item;
645                 item = new QStandardItem(*laptop_icon, name);
646                 if (item) {
647                         item->setData(uuid, peer_role_uuid);
648                         item->setData(addr, peer_role_address);
649                         item->setData(PEER_TYPE_WPS_ER_ENROLLEE,
650                                       peer_role_type);
651                         item->setToolTip(ItemType(PEER_TYPE_WPS_ER_ENROLLEE));
652                         item->setData(items.join(QString("\n")),
653                                       peer_role_details);
654                         item->setData(pri_dev_type, peer_role_pri_dev_type);
655                         if (config_methods >= 0)
656                                 item->setData(config_methods,
657                                               peer_role_config_methods);
658                         if (dev_passwd_id >= 0)
659                                 item->setData(dev_passwd_id,
660                                               peer_role_dev_passwd_id);
661                         model.appendRow(item);
662                 }
663
664                 return;
665         }
666
667         if (text.startsWith(WPS_EVENT_ER_ENROLLEE_REMOVE)) {
668                 /*
669                  * WPS-ER-ENROLLEE-REMOVE 2b7093f1-d6fb-5108-adbb-bea66bb87333
670                  * 02:66:a0:ee:17:27
671                  */
672                 QStringList items = text.split(' ');
673                 if (items.size() < 2)
674                         return;
675                 remove_enrollee_uuid(items[1]);
676                 return;
677         }
678
679         if (text.startsWith(WPS_EVENT_ENROLLEE_SEEN)) {
680                 /* TODO: need to time out this somehow or remove on successful
681                  * WPS run, etc. */
682                 /*
683                  * WPS-ENROLLEE-SEEN 02:00:00:00:01:00
684                  * 572cf82f-c957-5653-9b16-b5cfb298abf1 1-0050F204-1 0x80 4 1
685                  * [Wireless Client]
686                  * (MAC addr, UUID-E, pri dev type, config methods,
687                  * dev passwd id, request type, [dev name])
688                  */
689                 QStringList items = text.split(' ');
690                 if (items.size() < 7)
691                         return;
692                 QString addr = items[1];
693                 QString uuid = items[2];
694                 QString pri_dev_type = items[3];
695                 int config_methods = items[4].toInt(0, 0);
696                 int dev_passwd_id = items[5].toInt();
697                 QString name;
698
699                 int pos = text.indexOf('[');
700                 if (pos >= 0) {
701                         int pos2 = text.lastIndexOf(']');
702                         if (pos2 >= pos) {
703                                 QStringList items2 =
704                                         text.mid(pos + 1, pos2 - pos - 1).
705                                         split('|');
706                                 name = items2[0];
707                         }
708                 }
709                 if (name.isEmpty())
710                         name = addr;
711
712                 QStandardItem *item;
713
714                 item = find_uuid(uuid);
715                 if (item) {
716                         QVariant var = item->data(peer_role_config_methods);
717                         QVariant var2 = item->data(peer_role_dev_passwd_id);
718                         if ((var.isValid() && config_methods != var.toInt()) ||
719                             (var2.isValid() && dev_passwd_id != var2.toInt()))
720                                 remove_enrollee_uuid(uuid);
721                         else
722                                 return;
723                 }
724
725                 item = new QStandardItem(*laptop_icon, name);
726                 if (item) {
727                         item->setData(uuid, peer_role_uuid);
728                         item->setData(addr, peer_role_address);
729                         item->setData(PEER_TYPE_WPS_ENROLLEE,
730                                       peer_role_type);
731                         item->setToolTip(ItemType(PEER_TYPE_WPS_ENROLLEE));
732                         item->setData(items.join(QString("\n")),
733                                       peer_role_details);
734                         item->setData(pri_dev_type, peer_role_pri_dev_type);
735                         item->setData(config_methods,
736                                       peer_role_config_methods);
737                         item->setData(dev_passwd_id, peer_role_dev_passwd_id);
738                         model.appendRow(item);
739                 }
740
741                 return;
742         }
743
744         if (text.startsWith(WPA_EVENT_BSS_ADDED)) {
745                 /* CTRL-EVENT-BSS-ADDED 34 00:11:22:33:44:55 */
746                 QStringList items = text.split(' ');
747                 if (items.size() < 2)
748                         return;
749                 char cmd[20];
750                 snprintf(cmd, sizeof(cmd), "BSS ID-%d", items[1].toInt());
751                 add_bss(cmd);
752                 return;
753         }
754
755         if (text.startsWith(WPA_EVENT_BSS_REMOVED)) {
756                 /* CTRL-EVENT-BSS-REMOVED 34 00:11:22:33:44:55 */
757                 QStringList items = text.split(' ');
758                 if (items.size() < 2)
759                         return;
760                 remove_bss(items[1].toInt());
761                 return;
762         }
763 }
764
765
766 void Peers::closeEvent(QCloseEvent *)
767 {
768         if (wpagui) {
769                 char reply[20];
770                 size_t replylen = sizeof(reply) - 1;
771                 wpagui->ctrlRequest("WPS_ER_STOP", reply, &replylen);
772         }
773 }
774
775
776 void Peers::done(int r)
777 {
778         QDialog::done(r);
779         close();
780 }
781
782
783 void Peers::remove_enrollee_uuid(QString uuid)
784 {
785         if (model.rowCount() == 0)
786                 return;
787
788         QModelIndexList lst = model.match(model.index(0, 0),
789                                           peer_role_uuid, uuid);
790         for (int i = 0; i < lst.size(); i++) {
791                 QStandardItem *item = model.itemFromIndex(lst[i]);
792                 if (item == NULL)
793                         continue;
794                 int type = item->data(peer_role_type).toInt();
795                 if (type == PEER_TYPE_WPS_ER_ENROLLEE ||
796                     type == PEER_TYPE_WPS_ENROLLEE)
797                         model.removeRow(lst[i].row());
798         }
799 }
800
801
802 void Peers::properties()
803 {
804         if (ctx_item == NULL)
805                 return;
806
807         QMessageBox msg(this);
808         msg.setStandardButtons(QMessageBox::Ok);
809         msg.setDefaultButton(QMessageBox::Ok);
810         msg.setEscapeButton(QMessageBox::Ok);
811         msg.setWindowTitle(tr("Peer Properties"));
812
813         int type = ctx_item->data(peer_role_type).toInt();
814         QString title = Peers::ItemType(type);
815
816         msg.setText(title + QString("\n") + tr("Name: ") + ctx_item->text());
817
818         QVariant var;
819         QString info;
820
821         var = ctx_item->data(peer_role_address);
822         if (var.isValid())
823                 info += tr("Address: ") + var.toString() + QString("\n");
824
825         var = ctx_item->data(peer_role_uuid);
826         if (var.isValid())
827                 info += tr("UUID: ") + var.toString() + QString("\n");
828
829         var = ctx_item->data(peer_role_pri_dev_type);
830         if (var.isValid())
831                 info += tr("Primary Device Type: ") + var.toString() +
832                         QString("\n");
833
834         var = ctx_item->data(peer_role_ssid);
835         if (var.isValid())
836                 info += tr("SSID: ") + var.toString() + QString("\n");
837
838         var = ctx_item->data(peer_role_config_methods);
839         if (var.isValid()) {
840                 int methods = var.toInt();
841                 info += tr("Configuration Methods: ");
842                 if (methods & 0x0001)
843                         info += tr("[USBA]");
844                 if (methods & 0x0002)
845                         info += tr("[Ethernet]");
846                 if (methods & 0x0004)
847                         info += tr("[Label]");
848                 if (methods & 0x0008)
849                         info += tr("[Display]");
850                 if (methods & 0x0010)
851                         info += tr("[Ext. NFC Token]");
852                 if (methods & 0x0020)
853                         info += tr("[Int. NFC Token]");
854                 if (methods & 0x0040)
855                         info += tr("[NFC Interface]");
856                 if (methods & 0x0080)
857                         info += tr("[Push Button]");
858                 if (methods & 0x0100)
859                         info += tr("[Keypad]");
860                 info += "\n";
861         }
862
863         var = ctx_item->data(peer_role_dev_passwd_id);
864         if (var.isValid()) {
865                 info += tr("Device Password ID: ") + var.toString();
866                 switch (var.toInt()) {
867                 case 0:
868                         info += tr(" (Default PIN)");
869                         break;
870                 case 1:
871                         info += tr(" (User-specified PIN)");
872                         break;
873                 case 2:
874                         info += tr(" (Machine-specified PIN)");
875                         break;
876                 case 3:
877                         info += tr(" (Rekey)");
878                         break;
879                 case 4:
880                         info += tr(" (Push Button)");
881                         break;
882                 case 5:
883                         info += tr(" (Registrar-specified)");
884                         break;
885                 }
886                 info += "\n";
887         }
888
889         msg.setInformativeText(info);
890
891         var = ctx_item->data(peer_role_details);
892         if (var.isValid())
893                 msg.setDetailedText(var.toString());
894
895         msg.exec();
896 }
897
898
899 void Peers::connect_pbc()
900 {
901         if (ctx_item == NULL)
902                 return;
903
904         char cmd[100];
905         char reply[100];
906         size_t reply_len;
907
908         int peer_type = ctx_item->data(peer_role_type).toInt();
909         if (peer_type == PEER_TYPE_WPS_ER_ENROLLEE) {
910                 snprintf(cmd, sizeof(cmd), "WPS_ER_PBC %s",
911                          ctx_item->data(peer_role_uuid).toString().toAscii().
912                          constData());
913         } else {
914                 snprintf(cmd, sizeof(cmd), "WPS_PBC");
915         }
916         reply_len = sizeof(reply) - 1;
917         if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0) {
918                 QMessageBox msg;
919                 msg.setIcon(QMessageBox::Warning);
920                 msg.setText("Failed to start WPS PBC.");
921                 msg.exec();
922         }
923 }
924
925
926 void Peers::learn_ap_config()
927 {
928         if (ctx_item == NULL)
929                 return;
930
931         QString uuid = ctx_item->data(peer_role_uuid).toString();
932
933         StringQuery input(tr("AP PIN:"));
934         input.setWindowTitle(tr("AP PIN for ") + ctx_item->text());
935         if (input.exec() != QDialog::Accepted)
936                 return;
937
938         char cmd[100];
939         char reply[100];
940         size_t reply_len;
941
942         snprintf(cmd, sizeof(cmd), "WPS_ER_LEARN %s %s",
943                  uuid.toAscii().constData(),
944                  input.get_string().toAscii().constData());
945         reply_len = sizeof(reply) - 1;
946         if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0) {
947                 QMessageBox msg;
948                 msg.setIcon(QMessageBox::Warning);
949                 msg.setText(tr("Failed to start learning AP configuration."));
950                 msg.exec();
951         }
952 }