wpa_gui-qt4: Update BSS entries in Peers dialog dynamically
[mech_eap.git] / wpa_supplicant / wpa_gui-qt4 / peers.cpp
index d1439e2..9cdf649 100644 (file)
@@ -30,7 +30,8 @@ enum {
        peer_role_pri_dev_type,
        peer_role_ssid,
        peer_role_config_methods,
-       peer_role_dev_passwd_id
+       peer_role_dev_passwd_id,
+       peer_role_bss_id
 };
 
 /*
@@ -45,7 +46,8 @@ enum peer_type {
        PEER_TYPE_WPS_PIN_NEEDED,
        PEER_TYPE_WPS_ER_AP,
        PEER_TYPE_WPS_ER_AP_UNCONFIGURED,
-       PEER_TYPE_WPS_ER_ENROLLEE
+       PEER_TYPE_WPS_ER_ENROLLEE,
+       PEER_TYPE_WPS_ENROLLEE
 };
 
 
@@ -122,6 +124,9 @@ QString Peers::ItemType(int type)
        case PEER_TYPE_WPS_ER_ENROLLEE:
                title = tr("ER: WPS Enrollee");
                break;
+       case PEER_TYPE_WPS_ENROLLEE:
+               title = tr("WPS Enrollee");
+               break;
        }
        return title;
 }
@@ -148,7 +153,8 @@ void Peers::context_menu(const QPoint &pos)
                if ((type == PEER_TYPE_ASSOCIATED_STATION ||
                     type == PEER_TYPE_AP_WPS ||
                     type == PEER_TYPE_WPS_PIN_NEEDED ||
-                    type == PEER_TYPE_WPS_ER_ENROLLEE) &&
+                    type == PEER_TYPE_WPS_ER_ENROLLEE ||
+                    type == PEER_TYPE_WPS_ENROLLEE) &&
                    (config_methods == -1 || (config_methods & 0x010c))) {
                        menu->addAction(tr("Enter WPS PIN"), this,
                                        SLOT(enter_pin()));
@@ -160,7 +166,8 @@ void Peers::context_menu(const QPoint &pos)
                }
 
                if ((type == PEER_TYPE_ASSOCIATED_STATION ||
-                    type == PEER_TYPE_WPS_ER_ENROLLEE) &&
+                    type == PEER_TYPE_WPS_ER_ENROLLEE ||
+                    type == PEER_TYPE_WPS_ENROLLEE) &&
                    config_methods >= 0 && (config_methods & 0x0080)) {
                        menu->addAction(tr("Enroll (PBC)"), this,
                                        SLOT(connect_pbc()));
@@ -314,79 +321,103 @@ void Peers::add_single_station(const char *addr)
 }
 
 
-void Peers::add_scan_results()
+void Peers::remove_bss(int id)
+{
+       if (model.rowCount() == 0)
+               return;
+
+       QModelIndexList lst = model.match(model.index(0, 0), peer_role_bss_id,
+                                         id);
+       if (lst.size() == 0)
+               return;
+       model.removeRow(lst[0].row());
+}
+
+
+void Peers::add_bss(const char *cmd)
 {
        char reply[2048];
        size_t reply_len;
-       int index;
-       char cmd[20];
 
-       index = 0;
-       while (wpagui) {
-               snprintf(cmd, sizeof(cmd), "BSS %d", index++);
-               if (index > 1000)
-                       break;
+       reply_len = sizeof(reply) - 1;
+       if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0)
+               return;
+       reply[reply_len] = '\0';
 
-               reply_len = sizeof(reply) - 1;
-               if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0)
-                       break;
-               reply[reply_len] = '\0';
+       QString bss(reply);
+       if (bss.isEmpty() || bss.startsWith("FAIL"))
+               return;
 
-               QString bss(reply);
-               if (bss.isEmpty() || bss.startsWith("FAIL"))
-                       break;
+       QString ssid, bssid, flags, wps_name, pri_dev_type;
+       int id = -1;
 
-               QString ssid, bssid, flags, wps_name, pri_dev_type;
+       QStringList lines = bss.split(QRegExp("\\n"));
+       for (QStringList::Iterator it = lines.begin();
+            it != lines.end(); it++) {
+               int pos = (*it).indexOf('=') + 1;
+               if (pos < 1)
+                       continue;
 
-               QStringList lines = bss.split(QRegExp("\\n"));
-               for (QStringList::Iterator it = lines.begin();
-                    it != lines.end(); it++) {
-                       int pos = (*it).indexOf('=') + 1;
-                       if (pos < 1)
-                               continue;
+               if ((*it).startsWith("bssid="))
+                       bssid = (*it).mid(pos);
+               else if ((*it).startsWith("id="))
+                       id = (*it).mid(pos).toInt();
+               else if ((*it).startsWith("flags="))
+                       flags = (*it).mid(pos);
+               else if ((*it).startsWith("ssid="))
+                       ssid = (*it).mid(pos);
+               else if ((*it).startsWith("wps_device_name="))
+                       wps_name = (*it).mid(pos);
+               else if ((*it).startsWith("wps_primary_device_type="))
+                       pri_dev_type = (*it).mid(pos);
+       }
 
-                       if ((*it).startsWith("bssid="))
-                               bssid = (*it).mid(pos);
-                       else if ((*it).startsWith("flags="))
-                               flags = (*it).mid(pos);
-                       else if ((*it).startsWith("ssid="))
-                               ssid = (*it).mid(pos);
-                       else if ((*it).startsWith("wps_device_name="))
-                               wps_name = (*it).mid(pos);
-                       else if ((*it).startsWith("wps_primary_device_type="))
-                               pri_dev_type = (*it).mid(pos);
+       QString name = wps_name;
+       if (name.isEmpty())
+               name = ssid + "\n" + bssid;
+
+       QStandardItem *item = new QStandardItem(*ap_icon, name);
+       if (item) {
+               item->setData(bssid, peer_role_address);
+               if (id >= 0)
+                       item->setData(id, peer_role_bss_id);
+               int type;
+               if (flags.contains("[WPS"))
+                       type = PEER_TYPE_AP_WPS;
+               else
+                       type = PEER_TYPE_AP;
+               item->setData(type, peer_role_type);
+
+               for (int i = 0; i < lines.size(); i++) {
+                       if (lines[i].length() > 60) {
+                               lines[i].remove(60, lines[i].length());
+                               lines[i] += "..";
+                       }
                }
+               item->setToolTip(ItemType(type));
+               item->setData(lines.join("\n"), peer_role_details);
+               if (!pri_dev_type.isEmpty())
+                       item->setData(pri_dev_type,
+                                     peer_role_pri_dev_type);
+               if (!ssid.isEmpty())
+                       item->setData(ssid, peer_role_ssid);
+               model.appendRow(item);
+       }
+}
 
-               QString name = wps_name;
-               if (name.isEmpty())
-                       name = ssid + "\n" + bssid;
 
-               QStandardItem *item = new QStandardItem(*ap_icon, name);
-               if (item) {
-                       item->setData(bssid, peer_role_address);
-                       int type;
-                       if (flags.contains("[WPS"))
-                               type = PEER_TYPE_AP_WPS;
-                       else
-                               type = PEER_TYPE_AP;
-                       item->setData(type, peer_role_type);
+void Peers::add_scan_results()
+{
+       int index;
+       char cmd[20];
 
-                       for (int i = 0; i < lines.size(); i++) {
-                               if (lines[i].length() > 60) {
-                                       lines[i].remove(
-                                               60, lines[i].length());
-                                       lines[i] += "..";
-                               }
-                       }
-                       item->setToolTip(ItemType(type));
-                       item->setData(lines.join("\n"), peer_role_details);
-                       if (!pri_dev_type.isEmpty())
-                               item->setData(pri_dev_type,
-                                             peer_role_pri_dev_type);
-                       if (!ssid.isEmpty())
-                               item->setData(ssid, peer_role_ssid);
-                       model.appendRow(item);
-               }
+       index = 0;
+       while (wpagui) {
+               snprintf(cmd, sizeof(cmd), "BSS %d", index++);
+               if (index > 1000)
+                       break;
+
+               add_bss(cmd);
        }
 }
 
@@ -644,6 +675,91 @@ void Peers::event_notify(WpaMsg msg)
                remove_enrollee_uuid(items[1]);
                return;
        }
+
+       if (text.startsWith(WPS_EVENT_ENROLLEE_SEEN)) {
+               /* TODO: need to time out this somehow or remove on successful
+                * WPS run, etc. */
+               /*
+                * WPS-ENROLLEE-SEEN 02:00:00:00:01:00
+                * 572cf82f-c957-5653-9b16-b5cfb298abf1 1-0050F204-1 0x80 4 1
+                * [Wireless Client]
+                * (MAC addr, UUID-E, pri dev type, config methods,
+                * dev passwd id, request type, [dev name])
+                */
+               QStringList items = text.split(' ');
+               if (items.size() < 7)
+                       return;
+               QString addr = items[1];
+               QString uuid = items[2];
+               QString pri_dev_type = items[3];
+               int config_methods = items[4].toInt(0, 0);
+               int dev_passwd_id = items[5].toInt();
+               QString name;
+
+               int pos = text.indexOf('[');
+               if (pos >= 0) {
+                       int pos2 = text.lastIndexOf(']');
+                       if (pos2 >= pos) {
+                               QStringList items2 =
+                                       text.mid(pos + 1, pos2 - pos - 1).
+                                       split('|');
+                               name = items2[0];
+                       }
+               }
+               if (name.isEmpty())
+                       name = addr;
+
+               QStandardItem *item;
+
+               item = find_uuid(uuid);
+               if (item) {
+                       QVariant var = item->data(peer_role_config_methods);
+                       QVariant var2 = item->data(peer_role_dev_passwd_id);
+                       if ((var.isValid() && config_methods != var.toInt()) ||
+                           (var2.isValid() && dev_passwd_id != var2.toInt()))
+                               remove_enrollee_uuid(uuid);
+                       else
+                               return;
+               }
+
+               item = new QStandardItem(*laptop_icon, name);
+               if (item) {
+                       item->setData(uuid, peer_role_uuid);
+                       item->setData(addr, peer_role_address);
+                       item->setData(PEER_TYPE_WPS_ENROLLEE,
+                                     peer_role_type);
+                       item->setToolTip(ItemType(PEER_TYPE_WPS_ENROLLEE));
+                       item->setData(items.join(QString("\n")),
+                                     peer_role_details);
+                       item->setData(pri_dev_type, peer_role_pri_dev_type);
+                       item->setData(config_methods,
+                                     peer_role_config_methods);
+                       item->setData(dev_passwd_id, peer_role_dev_passwd_id);
+                       model.appendRow(item);
+               }
+
+               return;
+       }
+
+       if (text.startsWith(WPA_EVENT_BSS_ADDED)) {
+               /* CTRL-EVENT-BSS-ADDED 34 00:11:22:33:44:55 */
+               QStringList items = text.split(' ');
+               if (items.size() < 2)
+                       return;
+               char cmd[20];
+               snprintf(cmd, sizeof(cmd), "BSS ID-%d", items[1].toInt());
+               add_bss(cmd);
+               return;
+       }
+
+       if (text.startsWith(WPA_EVENT_BSS_REMOVED)) {
+               /* CTRL-EVENT-BSS-REMOVED 34 00:11:22:33:44:55 */
+               QStringList items = text.split(' ');
+               if (items.size() < 2)
+                       return;
+               remove_bss(items[1].toInt());
+               return;
+       }
 }
 
 
@@ -673,8 +789,11 @@ void Peers::remove_enrollee_uuid(QString uuid)
                                          peer_role_uuid, uuid);
        for (int i = 0; i < lst.size(); i++) {
                QStandardItem *item = model.itemFromIndex(lst[i]);
-               if (item && item->data(peer_role_type).toInt() ==
-                   PEER_TYPE_WPS_ER_ENROLLEE)
+               if (item == NULL)
+                       continue;
+               int type = item->data(peer_role_type).toInt();
+               if (type == PEER_TYPE_WPS_ER_ENROLLEE ||
+                   type == PEER_TYPE_WPS_ENROLLEE)
                        model.removeRow(lst[i].row());
        }
 }