2 * wpa_gui - Peers class
3 * Copyright (c) 2009, Atheros Communications
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #include <QImageReader>
17 #include <QMessageBox>
19 #include "common/wpa_ctrl.h"
21 #include "stringquery.h"
26 peer_role_address = Qt::UserRole + 1,
30 peer_role_pri_dev_type,
32 peer_role_config_methods,
33 peer_role_dev_passwd_id,
39 * - add current AP info (e.g., from WPS) in station mode
43 PEER_TYPE_ASSOCIATED_STATION,
46 PEER_TYPE_WPS_PIN_NEEDED,
48 PEER_TYPE_WPS_ER_AP_UNCONFIGURED,
49 PEER_TYPE_WPS_ER_ENROLLEE,
50 PEER_TYPE_WPS_ENROLLEE
54 Peers::Peers(QWidget *parent, const char *, bool, Qt::WFlags)
59 if (QImageReader::supportedImageFormats().contains(QByteArray("svg")))
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");
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");
70 peers->setModel(&model);
71 peers->setResizeMode(QListView::Adjust);
73 peers->setContextMenuPolicy(Qt::CustomContextMenu);
74 connect(peers, SIGNAL(customContextMenuRequested(const QPoint &)),
75 this, SLOT(context_menu(const QPoint &)));
81 void Peers::setWpaGui(WpaGui *_wpagui)
96 void Peers::languageChange()
102 QString Peers::ItemType(int type)
106 case PEER_TYPE_ASSOCIATED_STATION:
107 title = tr("Associated station");
112 case PEER_TYPE_AP_WPS:
113 title = tr("WPS AP");
115 case PEER_TYPE_WPS_PIN_NEEDED:
116 title = tr("WPS PIN needed");
118 case PEER_TYPE_WPS_ER_AP:
119 title = tr("ER: WPS AP");
121 case PEER_TYPE_WPS_ER_AP_UNCONFIGURED:
122 title = tr("ER: WPS AP (Unconfigured)");
124 case PEER_TYPE_WPS_ER_ENROLLEE:
125 title = tr("ER: WPS Enrollee");
127 case PEER_TYPE_WPS_ENROLLEE:
128 title = tr("WPS Enrollee");
135 void Peers::context_menu(const QPoint &pos)
137 QMenu *menu = new QMenu;
141 QModelIndex idx = peers->indexAt(pos);
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();
148 int config_methods = -1;
149 QVariant var = ctx_item->data(peer_role_config_methods);
151 config_methods = var.toInt();
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,
163 if (type == PEER_TYPE_AP_WPS) {
164 menu->addAction(tr("Connect (PBC)"), this,
165 SLOT(connect_pbc()));
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()));
176 if (type == PEER_TYPE_WPS_ER_AP) {
177 menu->addAction(tr("Learn Configuration"), this,
178 SLOT(learn_ap_config()));
181 menu->addAction(tr("Properties"), this, SLOT(properties()));
184 menu->addAction(QString("Refresh"), this, SLOT(ctx_refresh()));
187 menu->exec(peers->mapToGlobal(pos));
191 void Peers::enter_pin()
193 if (ctx_item == NULL)
196 int peer_type = ctx_item->data(peer_role_type).toInt();
199 if (peer_type == PEER_TYPE_WPS_ER_ENROLLEE)
200 uuid = ctx_item->data(peer_role_uuid).toString();
202 addr = ctx_item->data(peer_role_address).toString();
204 StringQuery input(tr("PIN:"));
205 input.setWindowTitle(tr("PIN for ") + ctx_item->text());
206 if (input.exec() != QDialog::Accepted)
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());
218 snprintf(cmd, sizeof(cmd), "WPS_PIN %s %s",
219 addr.toAscii().constData(),
220 input.get_string().toAscii().constData());
222 reply_len = sizeof(reply) - 1;
223 if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0) {
225 msg.setIcon(QMessageBox::Warning);
226 msg.setText("Failed to set the WPS PIN.");
232 void Peers::ctx_refresh()
238 void Peers::add_station(QString info)
240 QStringList lines = info.split(QRegExp("\\n"));
243 for (QStringList::Iterator it = lines.begin();
244 it != lines.end(); it++) {
245 int pos = (*it).indexOf('=') + 1;
249 if ((*it).startsWith("wpsDeviceName="))
250 name = (*it).mid(pos);
256 QStandardItem *item = new QStandardItem(*laptop_icon, name);
258 item->setData(lines[0], peer_role_address);
259 item->setData(PEER_TYPE_ASSOCIATED_STATION,
261 item->setData(info, peer_role_details);
262 item->setToolTip(ItemType(PEER_TYPE_ASSOCIATED_STATION));
263 model.appendRow(item);
268 void Peers::add_stations()
275 reply_len = sizeof(reply) - 1;
276 if (wpagui->ctrlRequest("STA-FIRST", reply, &reply_len) < 0)
280 reply[reply_len] = '\0';
283 while (*txt != '\0' && *txt != '\n')
286 if (strncmp(reply, "FAIL", 4) == 0 ||
287 strncmp(reply, "UNKNOWN", 7) == 0)
292 reply_len = sizeof(reply) - 1;
293 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", reply);
294 res = wpagui->ctrlRequest(cmd, reply, &reply_len);
299 void Peers::add_single_station(const char *addr)
305 reply_len = sizeof(reply) - 1;
306 snprintf(cmd, sizeof(cmd), "STA %s", addr);
307 if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0)
310 reply[reply_len] = '\0';
313 while (*txt != '\0' && *txt != '\n')
316 if (strncmp(reply, "FAIL", 4) == 0 ||
317 strncmp(reply, "UNKNOWN", 7) == 0)
324 void Peers::remove_bss(int id)
326 if (model.rowCount() == 0)
329 QModelIndexList lst = model.match(model.index(0, 0), peer_role_bss_id,
333 model.removeRow(lst[0].row());
337 bool Peers::add_bss(const char *cmd)
342 reply_len = sizeof(reply) - 1;
343 if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0)
345 reply[reply_len] = '\0';
348 if (bss.isEmpty() || bss.startsWith("FAIL"))
351 QString ssid, bssid, flags, wps_name, pri_dev_type;
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;
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);
375 QString name = wps_name;
377 name = ssid + "\n" + bssid;
379 QStandardItem *item = new QStandardItem(*ap_icon, name);
381 item->setData(bssid, peer_role_address);
383 item->setData(id, peer_role_bss_id);
385 if (flags.contains("[WPS"))
386 type = PEER_TYPE_AP_WPS;
389 item->setData(type, peer_role_type);
391 for (int i = 0; i < lines.size(); i++) {
392 if (lines[i].length() > 60) {
393 lines[i].remove(60, lines[i].length());
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);
403 item->setData(ssid, peer_role_ssid);
404 model.appendRow(item);
411 void Peers::add_scan_results()
418 snprintf(cmd, sizeof(cmd), "BSS %d", index++);
428 void Peers::update_peers()
435 size_t replylen = sizeof(reply) - 1;
436 wpagui->ctrlRequest("WPS_ER_START", reply, &replylen);
443 QStandardItem * Peers::find_addr(QString addr)
445 if (model.rowCount() == 0)
448 QModelIndexList lst = model.match(model.index(0, 0), peer_role_address,
452 return model.itemFromIndex(lst[0]);
456 QStandardItem * Peers::find_uuid(QString uuid)
458 if (model.rowCount() == 0)
461 QModelIndexList lst = model.match(model.index(0, 0), peer_role_uuid,
465 return model.itemFromIndex(lst[0]);
469 void Peers::event_notify(WpaMsg msg)
471 QString text = msg.getMsg();
473 if (text.startsWith(WPS_EVENT_PIN_NEEDED)) {
475 * WPS-PIN-NEEDED 5a02a5fa-9199-5e7c-bc46-e183d3cb32f7
477 * [Wireless Client|Company|cmodel|123|12345|1-0050F204-1]
479 QStringList items = text.split(' ');
480 QString uuid = items[1];
481 QString addr = items[2];
484 QStandardItem *item = find_addr(addr);
488 int pos = text.indexOf('[');
490 int pos2 = text.lastIndexOf(']');
492 items = text.mid(pos + 1, pos2 - pos - 1).
499 item = new QStandardItem(*laptop_icon, name);
501 item->setData(addr, peer_role_address);
502 item->setData(PEER_TYPE_WPS_PIN_NEEDED,
504 item->setToolTip(ItemType(PEER_TYPE_WPS_PIN_NEEDED));
505 item->setData(items.join("\n"), peer_role_details);
506 item->setData(items[5], peer_role_pri_dev_type);
507 model.appendRow(item);
512 if (text.startsWith(AP_STA_CONNECTED)) {
513 /* AP-STA-CONNECTED 02:2a:c4:18:5b:f3 */
514 QStringList items = text.split(' ');
515 QString addr = items[1];
516 QStandardItem *item = find_addr(addr);
517 if (item == NULL || item->data(peer_role_type).toInt() !=
518 PEER_TYPE_ASSOCIATED_STATION)
519 add_single_station(addr.toAscii().constData());
523 if (text.startsWith(AP_STA_DISCONNECTED)) {
524 /* AP-STA-DISCONNECTED 02:2a:c4:18:5b:f3 */
525 QStringList items = text.split(' ');
526 QString addr = items[1];
528 if (model.rowCount() == 0)
531 QModelIndexList lst = model.match(model.index(0, 0),
532 peer_role_address, addr);
533 for (int i = 0; i < lst.size(); i++) {
534 QStandardItem *item = model.itemFromIndex(lst[i]);
535 if (item && item->data(peer_role_type).toInt() ==
536 PEER_TYPE_ASSOCIATED_STATION)
537 model.removeRow(lst[i].row());
542 if (text.startsWith(WPS_EVENT_ER_AP_ADD)) {
544 * WPS-ER-AP-ADD 87654321-9abc-def0-1234-56789abc0002
545 * 02:11:22:33:44:55 pri_dev_type=6-0050F204-1 wps_state=1
546 * |Very friendly name|Company|Long description of the model|
547 * WAP|http://w1.fi/|http://w1.fi/hostapd/
549 QStringList items = text.split(' ');
550 if (items.size() < 5)
552 QString uuid = items[1];
553 QString addr = items[2];
554 QString pri_dev_type = items[3].mid(13);
555 int wps_state = items[4].mid(10).toInt();
557 int pos = text.indexOf('|');
560 items = text.mid(pos + 1).split('|');
561 if (items.size() < 1)
564 QStandardItem *item = find_uuid(uuid);
568 item = new QStandardItem(*ap_icon, items[0]);
570 item->setData(uuid, peer_role_uuid);
571 item->setData(addr, peer_role_address);
572 int type = wps_state == 2 ? PEER_TYPE_WPS_ER_AP:
573 PEER_TYPE_WPS_ER_AP_UNCONFIGURED;
574 item->setData(type, peer_role_type);
575 item->setToolTip(ItemType(type));
576 item->setData(pri_dev_type, peer_role_pri_dev_type);
577 item->setData(items.join(QString("\n")),
579 model.appendRow(item);
585 if (text.startsWith(WPS_EVENT_ER_AP_REMOVE)) {
586 /* WPS-ER-AP-REMOVE 87654321-9abc-def0-1234-56789abc0002 */
587 QStringList items = text.split(' ');
588 if (items.size() < 2)
590 if (model.rowCount() == 0)
593 QModelIndexList lst = model.match(model.index(0, 0),
594 peer_role_uuid, items[1]);
595 for (int i = 0; i < lst.size(); i++) {
596 QStandardItem *item = model.itemFromIndex(lst[i]);
598 (item->data(peer_role_type).toInt() ==
599 PEER_TYPE_WPS_ER_AP ||
600 item->data(peer_role_type).toInt() ==
601 PEER_TYPE_WPS_ER_AP_UNCONFIGURED))
602 model.removeRow(lst[i].row());
607 if (text.startsWith(WPS_EVENT_ER_ENROLLEE_ADD)) {
609 * WPS-ER-ENROLLEE-ADD 2b7093f1-d6fb-5108-adbb-bea66bb87333
610 * 02:66:a0:ee:17:27 M1=1 config_methods=0x14d dev_passwd_id=0
611 * pri_dev_type=1-0050F204-1
612 * |Wireless Client|Company|cmodel|123|12345|
614 QStringList items = text.split(' ');
615 if (items.size() < 3)
617 QString uuid = items[1];
618 QString addr = items[2];
619 QString pri_dev_type = items[6].mid(13);
620 int config_methods = -1;
621 int dev_passwd_id = -1;
623 for (int i = 3; i < items.size(); i++) {
624 int pos = items[i].indexOf('=') + 1;
627 QString val = items[i].mid(pos);
628 if (items[i].startsWith("config_methods=")) {
629 config_methods = val.toInt(0, 0);
630 } else if (items[i].startsWith("dev_passwd_id=")) {
631 dev_passwd_id = val.toInt();
635 int pos = text.indexOf('|');
638 items = text.mid(pos + 1).split('|');
639 if (items.size() < 1)
641 QString name = items[0];
642 if (name.length() == 0)
645 remove_enrollee_uuid(uuid);
648 item = new QStandardItem(*laptop_icon, name);
650 item->setData(uuid, peer_role_uuid);
651 item->setData(addr, peer_role_address);
652 item->setData(PEER_TYPE_WPS_ER_ENROLLEE,
654 item->setToolTip(ItemType(PEER_TYPE_WPS_ER_ENROLLEE));
655 item->setData(items.join(QString("\n")),
657 item->setData(pri_dev_type, peer_role_pri_dev_type);
658 if (config_methods >= 0)
659 item->setData(config_methods,
660 peer_role_config_methods);
661 if (dev_passwd_id >= 0)
662 item->setData(dev_passwd_id,
663 peer_role_dev_passwd_id);
664 model.appendRow(item);
670 if (text.startsWith(WPS_EVENT_ER_ENROLLEE_REMOVE)) {
672 * WPS-ER-ENROLLEE-REMOVE 2b7093f1-d6fb-5108-adbb-bea66bb87333
675 QStringList items = text.split(' ');
676 if (items.size() < 2)
678 remove_enrollee_uuid(items[1]);
682 if (text.startsWith(WPS_EVENT_ENROLLEE_SEEN)) {
683 /* TODO: need to time out this somehow or remove on successful
686 * WPS-ENROLLEE-SEEN 02:00:00:00:01:00
687 * 572cf82f-c957-5653-9b16-b5cfb298abf1 1-0050F204-1 0x80 4 1
689 * (MAC addr, UUID-E, pri dev type, config methods,
690 * dev passwd id, request type, [dev name])
692 QStringList items = text.split(' ');
693 if (items.size() < 7)
695 QString addr = items[1];
696 QString uuid = items[2];
697 QString pri_dev_type = items[3];
698 int config_methods = items[4].toInt(0, 0);
699 int dev_passwd_id = items[5].toInt();
702 int pos = text.indexOf('[');
704 int pos2 = text.lastIndexOf(']');
707 text.mid(pos + 1, pos2 - pos - 1).
717 item = find_uuid(uuid);
719 QVariant var = item->data(peer_role_config_methods);
720 QVariant var2 = item->data(peer_role_dev_passwd_id);
721 if ((var.isValid() && config_methods != var.toInt()) ||
722 (var2.isValid() && dev_passwd_id != var2.toInt()))
723 remove_enrollee_uuid(uuid);
728 item = new QStandardItem(*laptop_icon, name);
730 item->setData(uuid, peer_role_uuid);
731 item->setData(addr, peer_role_address);
732 item->setData(PEER_TYPE_WPS_ENROLLEE,
734 item->setToolTip(ItemType(PEER_TYPE_WPS_ENROLLEE));
735 item->setData(items.join(QString("\n")),
737 item->setData(pri_dev_type, peer_role_pri_dev_type);
738 item->setData(config_methods,
739 peer_role_config_methods);
740 item->setData(dev_passwd_id, peer_role_dev_passwd_id);
741 model.appendRow(item);
747 if (text.startsWith(WPA_EVENT_BSS_ADDED)) {
748 /* CTRL-EVENT-BSS-ADDED 34 00:11:22:33:44:55 */
749 QStringList items = text.split(' ');
750 if (items.size() < 2)
753 snprintf(cmd, sizeof(cmd), "BSS ID-%d", items[1].toInt());
758 if (text.startsWith(WPA_EVENT_BSS_REMOVED)) {
759 /* CTRL-EVENT-BSS-REMOVED 34 00:11:22:33:44:55 */
760 QStringList items = text.split(' ');
761 if (items.size() < 2)
763 remove_bss(items[1].toInt());
769 void Peers::closeEvent(QCloseEvent *)
773 size_t replylen = sizeof(reply) - 1;
774 wpagui->ctrlRequest("WPS_ER_STOP", reply, &replylen);
779 void Peers::done(int r)
786 void Peers::remove_enrollee_uuid(QString uuid)
788 if (model.rowCount() == 0)
791 QModelIndexList lst = model.match(model.index(0, 0),
792 peer_role_uuid, uuid);
793 for (int i = 0; i < lst.size(); i++) {
794 QStandardItem *item = model.itemFromIndex(lst[i]);
797 int type = item->data(peer_role_type).toInt();
798 if (type == PEER_TYPE_WPS_ER_ENROLLEE ||
799 type == PEER_TYPE_WPS_ENROLLEE)
800 model.removeRow(lst[i].row());
805 void Peers::properties()
807 if (ctx_item == NULL)
810 QMessageBox msg(this);
811 msg.setStandardButtons(QMessageBox::Ok);
812 msg.setDefaultButton(QMessageBox::Ok);
813 msg.setEscapeButton(QMessageBox::Ok);
814 msg.setWindowTitle(tr("Peer Properties"));
816 int type = ctx_item->data(peer_role_type).toInt();
817 QString title = Peers::ItemType(type);
819 msg.setText(title + QString("\n") + tr("Name: ") + ctx_item->text());
824 var = ctx_item->data(peer_role_address);
826 info += tr("Address: ") + var.toString() + QString("\n");
828 var = ctx_item->data(peer_role_uuid);
830 info += tr("UUID: ") + var.toString() + QString("\n");
832 var = ctx_item->data(peer_role_pri_dev_type);
834 info += tr("Primary Device Type: ") + var.toString() +
837 var = ctx_item->data(peer_role_ssid);
839 info += tr("SSID: ") + var.toString() + QString("\n");
841 var = ctx_item->data(peer_role_config_methods);
843 int methods = var.toInt();
844 info += tr("Configuration Methods: ");
845 if (methods & 0x0001)
846 info += tr("[USBA]");
847 if (methods & 0x0002)
848 info += tr("[Ethernet]");
849 if (methods & 0x0004)
850 info += tr("[Label]");
851 if (methods & 0x0008)
852 info += tr("[Display]");
853 if (methods & 0x0010)
854 info += tr("[Ext. NFC Token]");
855 if (methods & 0x0020)
856 info += tr("[Int. NFC Token]");
857 if (methods & 0x0040)
858 info += tr("[NFC Interface]");
859 if (methods & 0x0080)
860 info += tr("[Push Button]");
861 if (methods & 0x0100)
862 info += tr("[Keypad]");
866 var = ctx_item->data(peer_role_dev_passwd_id);
868 info += tr("Device Password ID: ") + var.toString();
869 switch (var.toInt()) {
871 info += tr(" (Default PIN)");
874 info += tr(" (User-specified PIN)");
877 info += tr(" (Machine-specified PIN)");
880 info += tr(" (Rekey)");
883 info += tr(" (Push Button)");
886 info += tr(" (Registrar-specified)");
892 msg.setInformativeText(info);
894 var = ctx_item->data(peer_role_details);
896 msg.setDetailedText(var.toString());
902 void Peers::connect_pbc()
904 if (ctx_item == NULL)
911 int peer_type = ctx_item->data(peer_role_type).toInt();
912 if (peer_type == PEER_TYPE_WPS_ER_ENROLLEE) {
913 snprintf(cmd, sizeof(cmd), "WPS_ER_PBC %s",
914 ctx_item->data(peer_role_uuid).toString().toAscii().
917 snprintf(cmd, sizeof(cmd), "WPS_PBC");
919 reply_len = sizeof(reply) - 1;
920 if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0) {
922 msg.setIcon(QMessageBox::Warning);
923 msg.setText("Failed to start WPS PBC.");
929 void Peers::learn_ap_config()
931 if (ctx_item == NULL)
934 QString uuid = ctx_item->data(peer_role_uuid).toString();
936 StringQuery input(tr("AP PIN:"));
937 input.setWindowTitle(tr("AP PIN for ") + ctx_item->text());
938 if (input.exec() != QDialog::Accepted)
945 snprintf(cmd, sizeof(cmd), "WPS_ER_LEARN %s %s",
946 uuid.toAscii().constData(),
947 input.get_string().toAscii().constData());
948 reply_len = sizeof(reply) - 1;
949 if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0) {
951 msg.setIcon(QMessageBox::Warning);
952 msg.setText(tr("Failed to start learning AP configuration."));