peers.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. #include <cstdio>
  15. #include <QImageReader>
  16. #include <QMessageBox>
  17. #include "wpa_ctrl.h"
  18. #include "wpagui.h"
  19. #include "stringquery.h"
  20. #include "peers.h"
  21. enum {
  22. peer_role_address = Qt::UserRole + 1,
  23. peer_role_type,
  24. peer_role_uuid,
  25. peer_role_details,
  26. peer_role_pri_dev_type,
  27. peer_role_ssid
  28. };
  29. /*
  30. * TODO:
  31. * - add current AP info (e.g., from WPS) in station mode
  32. */
  33. enum peer_type {
  34. PEER_TYPE_ASSOCIATED_STATION,
  35. PEER_TYPE_AP,
  36. PEER_TYPE_AP_WPS,
  37. PEER_TYPE_WPS_PIN_NEEDED,
  38. PEER_TYPE_WPS_ER_AP,
  39. PEER_TYPE_WPS_ER_AP_UNCONFIGURED,
  40. PEER_TYPE_WPS_ER_ENROLLEE
  41. };
  42. Peers::Peers(QWidget *parent, const char *, bool, Qt::WFlags)
  43. : QDialog(parent)
  44. {
  45. setupUi(this);
  46. if (QImageReader::supportedImageFormats().contains(QByteArray("svg")))
  47. {
  48. default_icon = new QIcon(":/icons/wpa_gui.svg");
  49. ap_icon = new QIcon(":/icons/ap.svg");
  50. laptop_icon = new QIcon(":/icons/laptop.svg");
  51. } else {
  52. default_icon = new QIcon(":/icons/wpa_gui.png");
  53. ap_icon = new QIcon(":/icons/ap.png");
  54. laptop_icon = new QIcon(":/icons/laptop.png");
  55. }
  56. peers->setModel(&model);
  57. peers->setResizeMode(QListView::Adjust);
  58. peers->setContextMenuPolicy(Qt::CustomContextMenu);
  59. connect(peers, SIGNAL(customContextMenuRequested(const QPoint &)),
  60. this, SLOT(context_menu(const QPoint &)));
  61. wpagui = NULL;
  62. }
  63. void Peers::setWpaGui(WpaGui *_wpagui)
  64. {
  65. wpagui = _wpagui;
  66. update_peers();
  67. }
  68. Peers::~Peers()
  69. {
  70. delete default_icon;
  71. delete ap_icon;
  72. delete laptop_icon;
  73. }
  74. void Peers::languageChange()
  75. {
  76. retranslateUi(this);
  77. }
  78. QString Peers::ItemType(int type)
  79. {
  80. QString title;
  81. switch (type) {
  82. case PEER_TYPE_ASSOCIATED_STATION:
  83. title = tr("Associated station");
  84. break;
  85. case PEER_TYPE_AP:
  86. title = tr("AP");
  87. break;
  88. case PEER_TYPE_AP_WPS:
  89. title = tr("WPS AP");
  90. break;
  91. case PEER_TYPE_WPS_PIN_NEEDED:
  92. title = tr("WPS PIN needed");
  93. break;
  94. case PEER_TYPE_WPS_ER_AP:
  95. title = tr("ER: WPS AP");
  96. break;
  97. case PEER_TYPE_WPS_ER_AP_UNCONFIGURED:
  98. title = tr("ER: WPS AP (Unconfigured)");
  99. break;
  100. case PEER_TYPE_WPS_ER_ENROLLEE:
  101. title = tr("ER: WPS Enrollee");
  102. break;
  103. }
  104. return title;
  105. }
  106. void Peers::context_menu(const QPoint &pos)
  107. {
  108. QMenu *menu = new QMenu;
  109. if (menu == NULL)
  110. return;
  111. QModelIndex idx = peers->indexAt(pos);
  112. if (idx.isValid()) {
  113. ctx_item = model.itemFromIndex(idx);
  114. int type = ctx_item->data(peer_role_type).toInt();
  115. menu->addAction(Peers::ItemType(type))->setEnabled(false);
  116. menu->addSeparator();
  117. if (type == PEER_TYPE_ASSOCIATED_STATION ||
  118. type == PEER_TYPE_AP_WPS ||
  119. type == PEER_TYPE_WPS_PIN_NEEDED ||
  120. type == PEER_TYPE_WPS_ER_ENROLLEE) {
  121. /* TODO: only for peers that are requesting WPS PIN
  122. * method */
  123. menu->addAction(tr("Enter WPS PIN"), this,
  124. SLOT(enter_pin()));
  125. }
  126. menu->addAction(tr("Properties"), this, SLOT(properties()));
  127. } else {
  128. ctx_item = NULL;
  129. menu->addAction(QString("Refresh"), this, SLOT(ctx_refresh()));
  130. }
  131. menu->exec(peers->mapToGlobal(pos));
  132. }
  133. void Peers::enter_pin()
  134. {
  135. if (ctx_item == NULL)
  136. return;
  137. int peer_type = ctx_item->data(peer_role_type).toInt();
  138. QString uuid;
  139. QString addr;
  140. if (peer_type == PEER_TYPE_WPS_ER_ENROLLEE)
  141. uuid = ctx_item->data(peer_role_uuid).toString();
  142. else
  143. addr = ctx_item->data(peer_role_address).toString();
  144. StringQuery input(tr("PIN:"));
  145. input.setWindowTitle(tr("PIN for ") + ctx_item->text());
  146. if (input.exec() != QDialog::Accepted)
  147. return;
  148. char cmd[100];
  149. char reply[100];
  150. size_t reply_len;
  151. if (peer_type == PEER_TYPE_WPS_ER_ENROLLEE) {
  152. snprintf(cmd, sizeof(cmd), "WPS_ER_PIN %s %s",
  153. uuid.toAscii().constData(),
  154. input.get_string().toAscii().constData());
  155. } else {
  156. snprintf(cmd, sizeof(cmd), "WPS_PIN %s %s",
  157. addr.toAscii().constData(),
  158. input.get_string().toAscii().constData());
  159. }
  160. reply_len = sizeof(reply) - 1;
  161. if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0) {
  162. QMessageBox msg;
  163. msg.setIcon(QMessageBox::Warning);
  164. msg.setText("Failed to set the WPS PIN.");
  165. msg.exec();
  166. }
  167. }
  168. void Peers::ctx_refresh()
  169. {
  170. update_peers();
  171. }
  172. void Peers::add_station(QString info)
  173. {
  174. QStringList lines = info.split(QRegExp("\\n"));
  175. QString name;
  176. for (QStringList::Iterator it = lines.begin();
  177. it != lines.end(); it++) {
  178. int pos = (*it).indexOf('=') + 1;
  179. if (pos < 1)
  180. continue;
  181. if ((*it).startsWith("wpsDeviceName="))
  182. name = (*it).mid(pos);
  183. }
  184. if (name.isEmpty())
  185. name = lines[0];
  186. QStandardItem *item = new QStandardItem(*laptop_icon, name);
  187. if (item) {
  188. item->setData(lines[0], peer_role_address);
  189. item->setData(PEER_TYPE_ASSOCIATED_STATION,
  190. peer_role_type);
  191. item->setData(info, peer_role_details);
  192. item->setToolTip(ItemType(PEER_TYPE_ASSOCIATED_STATION));
  193. model.appendRow(item);
  194. }
  195. }
  196. void Peers::add_stations()
  197. {
  198. char reply[2048];
  199. size_t reply_len;
  200. char cmd[30];
  201. int res;
  202. reply_len = sizeof(reply) - 1;
  203. if (wpagui->ctrlRequest("STA-FIRST", reply, &reply_len) < 0)
  204. return;
  205. do {
  206. reply[reply_len] = '\0';
  207. QString info(reply);
  208. char *txt = reply;
  209. while (*txt != '\0' && *txt != '\n')
  210. txt++;
  211. *txt++ = '\0';
  212. if (strncmp(reply, "FAIL", 4) == 0 ||
  213. strncmp(reply, "UNKNOWN", 7) == 0)
  214. break;
  215. add_station(info);
  216. reply_len = sizeof(reply) - 1;
  217. snprintf(cmd, sizeof(cmd), "STA-NEXT %s", reply);
  218. res = wpagui->ctrlRequest(cmd, reply, &reply_len);
  219. } while (res >= 0);
  220. }
  221. void Peers::add_single_station(const char *addr)
  222. {
  223. char reply[2048];
  224. size_t reply_len;
  225. char cmd[30];
  226. reply_len = sizeof(reply) - 1;
  227. snprintf(cmd, sizeof(cmd), "STA %s", addr);
  228. if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0)
  229. return;
  230. reply[reply_len] = '\0';
  231. QString info(reply);
  232. char *txt = reply;
  233. while (*txt != '\0' && *txt != '\n')
  234. txt++;
  235. *txt++ = '\0';
  236. if (strncmp(reply, "FAIL", 4) == 0 ||
  237. strncmp(reply, "UNKNOWN", 7) == 0)
  238. return;
  239. add_station(info);
  240. }
  241. void Peers::add_scan_results()
  242. {
  243. char reply[2048];
  244. size_t reply_len;
  245. int index;
  246. char cmd[20];
  247. index = 0;
  248. while (wpagui) {
  249. snprintf(cmd, sizeof(cmd), "BSS %d", index++);
  250. if (index > 1000)
  251. break;
  252. reply_len = sizeof(reply) - 1;
  253. if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0)
  254. break;
  255. reply[reply_len] = '\0';
  256. QString bss(reply);
  257. if (bss.isEmpty() || bss.startsWith("FAIL"))
  258. break;
  259. QString ssid, bssid, flags, wps_name, pri_dev_type;
  260. QStringList lines = bss.split(QRegExp("\\n"));
  261. for (QStringList::Iterator it = lines.begin();
  262. it != lines.end(); it++) {
  263. int pos = (*it).indexOf('=') + 1;
  264. if (pos < 1)
  265. continue;
  266. if ((*it).startsWith("bssid="))
  267. bssid = (*it).mid(pos);
  268. else if ((*it).startsWith("flags="))
  269. flags = (*it).mid(pos);
  270. else if ((*it).startsWith("ssid="))
  271. ssid = (*it).mid(pos);
  272. else if ((*it).startsWith("wps_device_name="))
  273. wps_name = (*it).mid(pos);
  274. else if ((*it).startsWith("wps_primary_device_type="))
  275. pri_dev_type = (*it).mid(pos);
  276. }
  277. QString name = wps_name;
  278. if (name.isEmpty())
  279. name = ssid + "\n" + bssid;
  280. QStandardItem *item = new QStandardItem(*ap_icon, name);
  281. if (item) {
  282. item->setData(bssid, peer_role_address);
  283. int type;
  284. if (flags.contains("[WPS"))
  285. type = PEER_TYPE_AP_WPS;
  286. else
  287. type = PEER_TYPE_AP;
  288. item->setData(type, peer_role_type);
  289. for (int i = 0; i < lines.size(); i++) {
  290. if (lines[i].length() > 60) {
  291. lines[i].remove(
  292. 60, lines[i].length());
  293. lines[i] += "..";
  294. }
  295. }
  296. item->setToolTip(ItemType(type));
  297. item->setData(lines.join("\n"), peer_role_details);
  298. if (!pri_dev_type.isEmpty())
  299. item->setData(pri_dev_type,
  300. peer_role_pri_dev_type);
  301. if (!ssid.isEmpty())
  302. item->setData(ssid, peer_role_ssid);
  303. model.appendRow(item);
  304. }
  305. }
  306. }
  307. void Peers::update_peers()
  308. {
  309. model.clear();
  310. if (wpagui == NULL)
  311. return;
  312. char reply[20];
  313. size_t replylen = sizeof(reply) - 1;
  314. wpagui->ctrlRequest("WPS_ER_START", reply, &replylen);
  315. add_stations();
  316. add_scan_results();
  317. }
  318. QStandardItem * Peers::find_addr(QString addr)
  319. {
  320. if (model.rowCount() == 0)
  321. return NULL;
  322. QModelIndexList lst = model.match(model.index(0, 0), peer_role_address,
  323. addr);
  324. if (lst.size() == 0)
  325. return NULL;
  326. return model.itemFromIndex(lst[0]);
  327. }
  328. QStandardItem * Peers::find_uuid(QString uuid)
  329. {
  330. if (model.rowCount() == 0)
  331. return NULL;
  332. QModelIndexList lst = model.match(model.index(0, 0), peer_role_uuid,
  333. uuid);
  334. if (lst.size() == 0)
  335. return NULL;
  336. return model.itemFromIndex(lst[0]);
  337. }
  338. void Peers::event_notify(WpaMsg msg)
  339. {
  340. QString text = msg.getMsg();
  341. if (text.startsWith(WPS_EVENT_PIN_NEEDED)) {
  342. /*
  343. * WPS-PIN-NEEDED 5a02a5fa-9199-5e7c-bc46-e183d3cb32f7
  344. * 02:2a:c4:18:5b:f3
  345. * [Wireless Client|Company|cmodel|123|12345|1-0050F204-1]
  346. */
  347. QStringList items = text.split(' ');
  348. QString uuid = items[1];
  349. QString addr = items[2];
  350. QString name = "";
  351. QStandardItem *item = find_addr(addr);
  352. if (item)
  353. return;
  354. int pos = text.indexOf('[');
  355. if (pos >= 0) {
  356. int pos2 = text.lastIndexOf(']');
  357. if (pos2 >= pos) {
  358. items = text.mid(pos + 1, pos2 - pos - 1).
  359. split('|');
  360. name = items[0];
  361. items.append(addr);
  362. }
  363. }
  364. item = new QStandardItem(*laptop_icon, name);
  365. if (item) {
  366. item->setData(addr, peer_role_address);
  367. item->setData(PEER_TYPE_WPS_PIN_NEEDED,
  368. peer_role_type);
  369. item->setToolTip(ItemType(PEER_TYPE_WPS_PIN_NEEDED));
  370. item->setData(items.join("\n"), peer_role_details);
  371. item->setData(items[5], peer_role_pri_dev_type);
  372. model.appendRow(item);
  373. }
  374. return;
  375. }
  376. if (text.startsWith(AP_STA_CONNECTED)) {
  377. /* AP-STA-CONNECTED 02:2a:c4:18:5b:f3 */
  378. QStringList items = text.split(' ');
  379. QString addr = items[1];
  380. QStandardItem *item = find_addr(addr);
  381. if (item == NULL || item->data(peer_role_type).toInt() !=
  382. PEER_TYPE_ASSOCIATED_STATION)
  383. add_single_station(addr.toAscii().constData());
  384. return;
  385. }
  386. if (text.startsWith(AP_STA_DISCONNECTED)) {
  387. /* AP-STA-DISCONNECTED 02:2a:c4:18:5b:f3 */
  388. QStringList items = text.split(' ');
  389. QString addr = items[1];
  390. if (model.rowCount() == 0)
  391. return;
  392. QModelIndexList lst = model.match(model.index(0, 0),
  393. peer_role_address, addr);
  394. for (int i = 0; i < lst.size(); i++) {
  395. QStandardItem *item = model.itemFromIndex(lst[i]);
  396. if (item && item->data(peer_role_type).toInt() ==
  397. PEER_TYPE_ASSOCIATED_STATION)
  398. model.removeRow(lst[i].row());
  399. }
  400. return;
  401. }
  402. if (text.startsWith(WPS_EVENT_ER_AP_ADD)) {
  403. /*
  404. * WPS-ER-AP-ADD 87654321-9abc-def0-1234-56789abc0002
  405. * 02:11:22:33:44:55 pri_dev_type=6-0050F204-1 wps_state=1
  406. * |Very friendly name|Company|Long description of the model|
  407. * WAP|http://w1.fi/|http://w1.fi/hostapd/
  408. */
  409. QStringList items = text.split(' ');
  410. if (items.size() < 5)
  411. return;
  412. QString uuid = items[1];
  413. QString addr = items[2];
  414. QString pri_dev_type = items[3].mid(13);
  415. int wps_state = items[4].mid(10).toInt();
  416. int pos = text.indexOf('|');
  417. if (pos < 0)
  418. return;
  419. items = text.mid(pos + 1).split('|');
  420. if (items.size() < 1)
  421. return;
  422. QStandardItem *item = find_uuid(uuid);
  423. if (item)
  424. return;
  425. item = new QStandardItem(*ap_icon, items[0]);
  426. if (item) {
  427. item->setData(uuid, peer_role_uuid);
  428. item->setData(addr, peer_role_address);
  429. int type = wps_state == 2 ? PEER_TYPE_WPS_ER_AP:
  430. PEER_TYPE_WPS_ER_AP_UNCONFIGURED;
  431. item->setData(type, peer_role_type);
  432. item->setToolTip(ItemType(type));
  433. item->setData(pri_dev_type, peer_role_pri_dev_type);
  434. item->setData(items.join(QString("\n")),
  435. peer_role_details);
  436. model.appendRow(item);
  437. }
  438. return;
  439. }
  440. if (text.startsWith(WPS_EVENT_ER_AP_REMOVE)) {
  441. /* WPS-ER-AP-REMOVE 87654321-9abc-def0-1234-56789abc0002 */
  442. QStringList items = text.split(' ');
  443. if (items.size() < 2)
  444. return;
  445. if (model.rowCount() == 0)
  446. return;
  447. QModelIndexList lst = model.match(model.index(0, 0),
  448. peer_role_uuid, items[1]);
  449. for (int i = 0; i < lst.size(); i++) {
  450. QStandardItem *item = model.itemFromIndex(lst[i]);
  451. if (item &&
  452. (item->data(peer_role_type).toInt() ==
  453. PEER_TYPE_WPS_ER_AP ||
  454. item->data(peer_role_type).toInt() ==
  455. PEER_TYPE_WPS_ER_AP_UNCONFIGURED))
  456. model.removeRow(lst[i].row());
  457. }
  458. return;
  459. }
  460. if (text.startsWith(WPS_EVENT_ER_ENROLLEE_ADD)) {
  461. /*
  462. * WPS-ER-ENROLLEE-ADD 2b7093f1-d6fb-5108-adbb-bea66bb87333
  463. * 02:66:a0:ee:17:27 M1=1 config_methods=0x14d dev_passwd_id=0
  464. * pri_dev_type=1-0050F204-1
  465. * |Wireless Client|Company|cmodel|123|12345|
  466. */
  467. QStringList items = text.split(' ');
  468. if (items.size() < 3)
  469. return;
  470. QString uuid = items[1];
  471. QString addr = items[2];
  472. QString pri_dev_type = items[6].mid(13);
  473. int pos = text.indexOf('|');
  474. if (pos < 0)
  475. return;
  476. items = text.mid(pos + 1).split('|');
  477. if (items.size() < 1)
  478. return;
  479. QString name = items[0];
  480. if (name.length() == 0)
  481. name = addr;
  482. remove_enrollee_uuid(uuid);
  483. QStandardItem *item;
  484. item = new QStandardItem(*laptop_icon, name);
  485. if (item) {
  486. item->setData(uuid, peer_role_uuid);
  487. item->setData(addr, peer_role_address);
  488. item->setData(PEER_TYPE_WPS_ER_ENROLLEE,
  489. peer_role_type);
  490. item->setToolTip(ItemType(PEER_TYPE_WPS_ER_ENROLLEE));
  491. item->setData(items.join(QString("\n")),
  492. peer_role_details);
  493. item->setData(pri_dev_type, peer_role_pri_dev_type);
  494. model.appendRow(item);
  495. }
  496. return;
  497. }
  498. if (text.startsWith(WPS_EVENT_ER_ENROLLEE_REMOVE)) {
  499. /*
  500. * WPS-ER-ENROLLEE-REMOVE 2b7093f1-d6fb-5108-adbb-bea66bb87333
  501. * 02:66:a0:ee:17:27
  502. */
  503. QStringList items = text.split(' ');
  504. if (items.size() < 2)
  505. return;
  506. remove_enrollee_uuid(items[1]);
  507. return;
  508. }
  509. }
  510. void Peers::closeEvent(QCloseEvent *)
  511. {
  512. if (wpagui) {
  513. char reply[20];
  514. size_t replylen = sizeof(reply) - 1;
  515. wpagui->ctrlRequest("WPS_ER_STOP", reply, &replylen);
  516. }
  517. }
  518. void Peers::done(int r)
  519. {
  520. QDialog::done(r);
  521. close();
  522. }
  523. void Peers::remove_enrollee_uuid(QString uuid)
  524. {
  525. if (model.rowCount() == 0)
  526. return;
  527. QModelIndexList lst = model.match(model.index(0, 0),
  528. peer_role_uuid, uuid);
  529. for (int i = 0; i < lst.size(); i++) {
  530. QStandardItem *item = model.itemFromIndex(lst[i]);
  531. if (item && item->data(peer_role_type).toInt() ==
  532. PEER_TYPE_WPS_ER_ENROLLEE)
  533. model.removeRow(lst[i].row());
  534. }
  535. }
  536. void Peers::properties()
  537. {
  538. if (ctx_item == NULL)
  539. return;
  540. QMessageBox msg(this);
  541. msg.setStandardButtons(QMessageBox::Ok);
  542. msg.setDefaultButton(QMessageBox::Ok);
  543. msg.setEscapeButton(QMessageBox::Ok);
  544. msg.setWindowTitle(tr("Peer Properties"));
  545. int type = ctx_item->data(peer_role_type).toInt();
  546. QString title = Peers::ItemType(type);
  547. msg.setText(title + QString("\n") + tr("Name: ") + ctx_item->text());
  548. QVariant var;
  549. QString info;
  550. var = ctx_item->data(peer_role_address);
  551. if (var.isValid())
  552. info += tr("Address: ") + var.toString() + QString("\n");
  553. var = ctx_item->data(peer_role_uuid);
  554. if (var.isValid())
  555. info += tr("UUID: ") + var.toString() + QString("\n");
  556. var = ctx_item->data(peer_role_pri_dev_type);
  557. if (var.isValid())
  558. info += tr("Primary Device Type: ") + var.toString() +
  559. QString("\n");
  560. var = ctx_item->data(peer_role_ssid);
  561. if (var.isValid())
  562. info += tr("SSID: ") + var.toString() + QString("\n");
  563. msg.setInformativeText(info);
  564. var = ctx_item->data(peer_role_details);
  565. if (var.isValid())
  566. msg.setDetailedText(var.toString());
  567. msg.exec();
  568. }