peers.cpp 13 KB

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