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 void 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);
409 void Peers::add_scan_results()
416 snprintf(cmd, sizeof(cmd), "BSS %d", index++);
425 void Peers::update_peers()
432 size_t replylen = sizeof(reply) - 1;
433 wpagui->ctrlRequest("WPS_ER_START", reply, &replylen);
440 QStandardItem * Peers::find_addr(QString addr)
442 if (model.rowCount() == 0)
445 QModelIndexList lst = model.match(model.index(0, 0), peer_role_address,
449 return model.itemFromIndex(lst[0]);
453 QStandardItem * Peers::find_uuid(QString uuid)
455 if (model.rowCount() == 0)
458 QModelIndexList lst = model.match(model.index(0, 0), peer_role_uuid,
462 return model.itemFromIndex(lst[0]);
466 void Peers::event_notify(WpaMsg msg)
468 QString text = msg.getMsg();
470 if (text.startsWith(WPS_EVENT_PIN_NEEDED)) {
472 * WPS-PIN-NEEDED 5a02a5fa-9199-5e7c-bc46-e183d3cb32f7
474 * [Wireless Client|Company|cmodel|123|12345|1-0050F204-1]
476 QStringList items = text.split(' ');
477 QString uuid = items[1];
478 QString addr = items[2];
481 QStandardItem *item = find_addr(addr);
485 int pos = text.indexOf('[');
487 int pos2 = text.lastIndexOf(']');
489 items = text.mid(pos + 1, pos2 - pos - 1).
496 item = new QStandardItem(*laptop_icon, name);
498 item->setData(addr, peer_role_address);
499 item->setData(PEER_TYPE_WPS_PIN_NEEDED,
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);
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());
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];
525 if (model.rowCount() == 0)
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());
539 if (text.startsWith(WPS_EVENT_ER_AP_ADD)) {
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/
546 QStringList items = text.split(' ');
547 if (items.size() < 5)
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();
554 int pos = text.indexOf('|');
557 items = text.mid(pos + 1).split('|');
558 if (items.size() < 1)
561 QStandardItem *item = find_uuid(uuid);
565 item = new QStandardItem(*ap_icon, items[0]);
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")),
576 model.appendRow(item);
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)
587 if (model.rowCount() == 0)
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]);
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());
604 if (text.startsWith(WPS_EVENT_ER_ENROLLEE_ADD)) {
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|
611 QStringList items = text.split(' ');
612 if (items.size() < 3)
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;
620 for (int i = 3; i < items.size(); i++) {
621 int pos = items[i].indexOf('=') + 1;
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();
632 int pos = text.indexOf('|');
635 items = text.mid(pos + 1).split('|');
636 if (items.size() < 1)
638 QString name = items[0];
639 if (name.length() == 0)
642 remove_enrollee_uuid(uuid);
645 item = new QStandardItem(*laptop_icon, name);
647 item->setData(uuid, peer_role_uuid);
648 item->setData(addr, peer_role_address);
649 item->setData(PEER_TYPE_WPS_ER_ENROLLEE,
651 item->setToolTip(ItemType(PEER_TYPE_WPS_ER_ENROLLEE));
652 item->setData(items.join(QString("\n")),
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);
667 if (text.startsWith(WPS_EVENT_ER_ENROLLEE_REMOVE)) {
669 * WPS-ER-ENROLLEE-REMOVE 2b7093f1-d6fb-5108-adbb-bea66bb87333
672 QStringList items = text.split(' ');
673 if (items.size() < 2)
675 remove_enrollee_uuid(items[1]);
679 if (text.startsWith(WPS_EVENT_ENROLLEE_SEEN)) {
680 /* TODO: need to time out this somehow or remove on successful
683 * WPS-ENROLLEE-SEEN 02:00:00:00:01:00
684 * 572cf82f-c957-5653-9b16-b5cfb298abf1 1-0050F204-1 0x80 4 1
686 * (MAC addr, UUID-E, pri dev type, config methods,
687 * dev passwd id, request type, [dev name])
689 QStringList items = text.split(' ');
690 if (items.size() < 7)
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();
699 int pos = text.indexOf('[');
701 int pos2 = text.lastIndexOf(']');
704 text.mid(pos + 1, pos2 - pos - 1).
714 item = find_uuid(uuid);
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);
725 item = new QStandardItem(*laptop_icon, name);
727 item->setData(uuid, peer_role_uuid);
728 item->setData(addr, peer_role_address);
729 item->setData(PEER_TYPE_WPS_ENROLLEE,
731 item->setToolTip(ItemType(PEER_TYPE_WPS_ENROLLEE));
732 item->setData(items.join(QString("\n")),
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);
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)
750 snprintf(cmd, sizeof(cmd), "BSS ID-%d", items[1].toInt());
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)
760 remove_bss(items[1].toInt());
766 void Peers::closeEvent(QCloseEvent *)
770 size_t replylen = sizeof(reply) - 1;
771 wpagui->ctrlRequest("WPS_ER_STOP", reply, &replylen);
776 void Peers::done(int r)
783 void Peers::remove_enrollee_uuid(QString uuid)
785 if (model.rowCount() == 0)
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]);
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());
802 void Peers::properties()
804 if (ctx_item == NULL)
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"));
813 int type = ctx_item->data(peer_role_type).toInt();
814 QString title = Peers::ItemType(type);
816 msg.setText(title + QString("\n") + tr("Name: ") + ctx_item->text());
821 var = ctx_item->data(peer_role_address);
823 info += tr("Address: ") + var.toString() + QString("\n");
825 var = ctx_item->data(peer_role_uuid);
827 info += tr("UUID: ") + var.toString() + QString("\n");
829 var = ctx_item->data(peer_role_pri_dev_type);
831 info += tr("Primary Device Type: ") + var.toString() +
834 var = ctx_item->data(peer_role_ssid);
836 info += tr("SSID: ") + var.toString() + QString("\n");
838 var = ctx_item->data(peer_role_config_methods);
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]");
863 var = ctx_item->data(peer_role_dev_passwd_id);
865 info += tr("Device Password ID: ") + var.toString();
866 switch (var.toInt()) {
868 info += tr(" (Default PIN)");
871 info += tr(" (User-specified PIN)");
874 info += tr(" (Machine-specified PIN)");
877 info += tr(" (Rekey)");
880 info += tr(" (Push Button)");
883 info += tr(" (Registrar-specified)");
889 msg.setInformativeText(info);
891 var = ctx_item->data(peer_role_details);
893 msg.setDetailedText(var.toString());
899 void Peers::connect_pbc()
901 if (ctx_item == NULL)
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().
914 snprintf(cmd, sizeof(cmd), "WPS_PBC");
916 reply_len = sizeof(reply) - 1;
917 if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0) {
919 msg.setIcon(QMessageBox::Warning);
920 msg.setText("Failed to start WPS PBC.");
926 void Peers::learn_ap_config()
928 if (ctx_item == NULL)
931 QString uuid = ctx_item->data(peer_role_uuid).toString();
933 StringQuery input(tr("AP PIN:"));
934 input.setWindowTitle(tr("AP PIN for ") + ctx_item->text());
935 if (input.exec() != QDialog::Accepted)
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) {
948 msg.setIcon(QMessageBox::Warning);
949 msg.setText(tr("Failed to start learning AP configuration."));