nsWifiScannerSolaris.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "nsWifiMonitor.h"
  5. #include "nsWifiAccessPoint.h"
  6. #include "nsServiceManagerUtils.h"
  7. #include "nsComponentManagerUtils.h"
  8. #include "nsIMutableArray.h"
  9. #include "plstr.h"
  10. #include <glib.h>
  11. #define DLADM_STRSIZE 256
  12. #define DLADM_SECTIONS 3
  13. using namespace mozilla;
  14. struct val_strength_t {
  15. const char *strength_name;
  16. int signal_value;
  17. };
  18. static val_strength_t strength_vals[] = {
  19. { "very weak", -112 },
  20. { "weak", -88 },
  21. { "good", -68 },
  22. { "very good", -40 },
  23. { "excellent", -16 }
  24. };
  25. static nsWifiAccessPoint *
  26. do_parse_str(char *bssid_str, char *essid_str, char *strength)
  27. {
  28. unsigned char mac_as_int[6] = { 0 };
  29. sscanf(bssid_str, "%x:%x:%x:%x:%x:%x", &mac_as_int[0], &mac_as_int[1],
  30. &mac_as_int[2], &mac_as_int[3], &mac_as_int[4], &mac_as_int[5]);
  31. int signal = 0;
  32. uint32_t strength_vals_count = sizeof(strength_vals) / sizeof (val_strength_t);
  33. for (uint32_t i = 0; i < strength_vals_count; i++) {
  34. if (!strncasecmp(strength, strength_vals[i].strength_name, DLADM_STRSIZE)) {
  35. signal = strength_vals[i].signal_value;
  36. break;
  37. }
  38. }
  39. nsWifiAccessPoint *ap = new nsWifiAccessPoint();
  40. if (ap) {
  41. ap->setMac(mac_as_int);
  42. ap->setSignal(signal);
  43. ap->setSSID(essid_str, PL_strnlen(essid_str, DLADM_STRSIZE));
  44. }
  45. return ap;
  46. }
  47. static void
  48. do_dladm(nsCOMArray<nsWifiAccessPoint> &accessPoints)
  49. {
  50. GError *err = nullptr;
  51. char *sout = nullptr;
  52. char *serr = nullptr;
  53. int exit_status = 0;
  54. char * dladm_args[] = { "/usr/bin/pfexec", "/usr/sbin/dladm",
  55. "scan-wifi", "-p", "-o", "BSSID,ESSID,STRENGTH" };
  56. gboolean rv = g_spawn_sync("/", dladm_args, nullptr, (GSpawnFlags)0, nullptr,
  57. nullptr, &sout, &serr, &exit_status, &err);
  58. if (rv && !exit_status) {
  59. char wlan[DLADM_SECTIONS][DLADM_STRSIZE+1];
  60. uint32_t section = 0;
  61. uint32_t sout_scan = 0;
  62. uint32_t wlan_put = 0;
  63. bool escape = false;
  64. nsWifiAccessPoint* ap;
  65. char sout_char;
  66. do {
  67. sout_char = sout[sout_scan++];
  68. if (escape) {
  69. escape = false;
  70. if (sout_char != '\0') {
  71. wlan[section][wlan_put++] = sout_char;
  72. continue;
  73. }
  74. }
  75. if (sout_char =='\\') {
  76. escape = true;
  77. continue;
  78. }
  79. if (sout_char == ':') {
  80. wlan[section][wlan_put] = '\0';
  81. section++;
  82. wlan_put = 0;
  83. continue;
  84. }
  85. if ((sout_char == '\0') || (sout_char == '\n')) {
  86. wlan[section][wlan_put] = '\0';
  87. if (section == DLADM_SECTIONS - 1) {
  88. ap = do_parse_str(wlan[0], wlan[1], wlan[2]);
  89. if (ap) {
  90. accessPoints.AppendObject(ap);
  91. }
  92. }
  93. section = 0;
  94. wlan_put = 0;
  95. continue;
  96. }
  97. wlan[section][wlan_put++] = sout_char;
  98. } while ((wlan_put <= DLADM_STRSIZE) && (section < DLADM_SECTIONS) &&
  99. (sout_char != '\0'));
  100. }
  101. g_free(sout);
  102. g_free(serr);
  103. }
  104. nsresult
  105. nsWifiMonitor::DoScan()
  106. {
  107. // Regularly get the access point data.
  108. nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
  109. nsCOMArray<nsWifiAccessPoint> accessPoints;
  110. while (mKeepGoing) {
  111. accessPoints.Clear();
  112. do_dladm(accessPoints);
  113. bool accessPointsChanged = !AccessPointsEqual(accessPoints, lastAccessPoints);
  114. ReplaceArray(lastAccessPoints, accessPoints);
  115. nsresult rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
  116. NS_ENSURE_SUCCESS(rv, rv);
  117. LOG(("waiting on monitor\n"));
  118. ReentrantMonitorAutoEnter mon(mReentrantMonitor);
  119. mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
  120. }
  121. return NS_OK;
  122. }