peers.cpp 21 KB

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