DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (053b0bb00fed)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsWifiScannerDBus.h"
#include "mozilla/DBusHelpers.h"
#include "nsWifiAccessPoint.h"

namespace mozilla {

nsWifiScannerDBus::nsWifiScannerDBus(
    nsCOMArray<nsWifiAccessPoint>* aAccessPoints)
    : mAccessPoints(aAccessPoints) {
  MOZ_ASSERT(mAccessPoints);

  mConnection =
      already_AddRefed<DBusConnection>(dbus_bus_get(DBUS_BUS_SYSTEM, nullptr));

  if (mConnection) {
    dbus_connection_setup_with_g_main(mConnection, nullptr);
    dbus_connection_set_exit_on_disconnect(mConnection, false);
  }

  MOZ_COUNT_CTOR(nsWifiScannerDBus);
}

nsWifiScannerDBus::~nsWifiScannerDBus() { MOZ_COUNT_DTOR(nsWifiScannerDBus); }

nsresult nsWifiScannerDBus::Scan() {
  if (!mConnection) {
    return NS_ERROR_NOT_AVAILABLE;
  }
  return SendGetDevices();
}

// http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
// Refer to function dbus_connection_send_with_reply_and_block.
static const uint32_t DBUS_DEFAULT_TIMEOUT = -1;

nsresult nsWifiScannerDBus::SendGetDevices() {
  RefPtr<DBusMessage> msg =
      already_AddRefed<DBusMessage>(dbus_message_new_method_call(
          "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager",
          "org.freedesktop.NetworkManager", "GetDevices"));
  if (!msg) {
    return NS_ERROR_FAILURE;
  }

  DBusError err;
  dbus_error_init(&err);

  RefPtr<DBusMessage> reply =
      already_AddRefed<DBusMessage>(dbus_connection_send_with_reply_and_block(
          mConnection, msg, DBUS_DEFAULT_TIMEOUT, &err));
  if (dbus_error_is_set(&err)) {
    dbus_error_free(&err);
    return NS_ERROR_FAILURE;
  }

  return IdentifyDevices(reply);
}

nsresult nsWifiScannerDBus::SendGetDeviceType(const char* aPath) {
  RefPtr<DBusMessage> msg = already_AddRefed<DBusMessage>(
      dbus_message_new_method_call("org.freedesktop.NetworkManager", aPath,
                                   "org.freedesktop.DBus.Properties", "Get"));
  if (!msg) {
    return NS_ERROR_FAILURE;
  }

  DBusMessageIter argsIter;
  dbus_message_iter_init_append(msg, &argsIter);

  const char* paramInterface = "org.freedesktop.NetworkManager.Device";
  if (!dbus_message_iter_append_basic(&argsIter, DBUS_TYPE_STRING,
                                      &paramInterface)) {
    return NS_ERROR_FAILURE;
  }

  const char* paramDeviceType = "DeviceType";
  if (!dbus_message_iter_append_basic(&argsIter, DBUS_TYPE_STRING,
                                      &paramDeviceType)) {
    return NS_ERROR_FAILURE;
  }

  DBusError err;
  dbus_error_init(&err);

  RefPtr<DBusMessage> reply =
      already_AddRefed<DBusMessage>(dbus_connection_send_with_reply_and_block(
          mConnection, msg, DBUS_DEFAULT_TIMEOUT, &err));
  if (dbus_error_is_set(&err)) {
    dbus_error_free(&err);
    return NS_ERROR_FAILURE;
  }

  return IdentifyDeviceType(reply, aPath);
}

nsresult nsWifiScannerDBus::SendGetAccessPoints(const char* aPath) {
  RefPtr<DBusMessage> msg =
      already_AddRefed<DBusMessage>(dbus_message_new_method_call(
          "org.freedesktop.NetworkManager", aPath,
          "org.freedesktop.NetworkManager.Device.Wireless", "GetAccessPoints"));
  if (!msg) {
    return NS_ERROR_FAILURE;
  }

  DBusError err;
  dbus_error_init(&err);

  RefPtr<DBusMessage> reply =
      already_AddRefed<DBusMessage>(dbus_connection_send_with_reply_and_block(
          mConnection, msg, DBUS_DEFAULT_TIMEOUT, &err));
  if (dbus_error_is_set(&err)) {
    dbus_error_free(&err);
    // In the GetAccessPoints case, if there are no access points, error is set.
    // We don't want to error out here.
    return NS_OK;
  }

  return IdentifyAccessPoints(reply);
}

nsresult nsWifiScannerDBus::SendGetAPProperties(const char* aPath) {
  RefPtr<DBusMessage> msg =
      already_AddRefed<DBusMessage>(dbus_message_new_method_call(
          "org.freedesktop.NetworkManager", aPath,
          "org.freedesktop.DBus.Properties", "GetAll"));
  if (!msg) {
    return NS_ERROR_FAILURE;
  }

  DBusMessageIter argsIter;
  dbus_message_iter_init_append(msg, &argsIter);

  const char* param = "org.freedesktop.NetworkManager.AccessPoint";
  if (!dbus_message_iter_append_basic(&argsIter, DBUS_TYPE_STRING, &param)) {
    return NS_ERROR_FAILURE;
  }

  DBusError err;
  dbus_error_init(&err);

  RefPtr<DBusMessage> reply =
      already_AddRefed<DBusMessage>(dbus_connection_send_with_reply_and_block(
          mConnection, msg, DBUS_DEFAULT_TIMEOUT, &err));
  if (dbus_error_is_set(&err)) {
    dbus_error_free(&err);
    return NS_ERROR_FAILURE;
  }

  return IdentifyAPProperties(reply);
}

nsresult nsWifiScannerDBus::IdentifyDevices(DBusMessage* aMsg) {
  DBusMessageIter iter;
  nsresult rv = GetDBusIterator(aMsg, &iter);
  NS_ENSURE_SUCCESS(rv, rv);

  const char* devicePath;
  do {
    if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_OBJECT_PATH) {
      return NS_ERROR_FAILURE;
    }

    dbus_message_iter_get_basic(&iter, &devicePath);
    if (!devicePath) {
      return NS_ERROR_FAILURE;
    }

    rv = SendGetDeviceType(devicePath);
    NS_ENSURE_SUCCESS(rv, rv);
  } while (dbus_message_iter_next(&iter));

  return NS_OK;
}

nsresult nsWifiScannerDBus::IdentifyDeviceType(DBusMessage* aMsg,
                                               const char* aDevicePath) {
  DBusMessageIter args;
  if (!dbus_message_iter_init(aMsg, &args)) {
    return NS_ERROR_FAILURE;
  }

  if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_VARIANT) {
    return NS_ERROR_FAILURE;
  }

  DBusMessageIter variantIter;
  dbus_message_iter_recurse(&args, &variantIter);
  if (dbus_message_iter_get_arg_type(&variantIter) != DBUS_TYPE_UINT32) {
    return NS_ERROR_FAILURE;
  }

  uint32_t deviceType;
  dbus_message_iter_get_basic(&variantIter, &deviceType);

  // http://projects.gnome.org/NetworkManager/developers/api/07/spec-07.html
  // Refer to NM_DEVICE_TYPE_WIFI under NM_DEVICE_TYPE.
  const uint32_t NM_DEVICE_TYPE_WIFI = 2;
  nsresult rv = NS_OK;
  if (deviceType == NM_DEVICE_TYPE_WIFI) {
    rv = SendGetAccessPoints(aDevicePath);
  }

  return rv;
}

nsresult nsWifiScannerDBus::IdentifyAccessPoints(DBusMessage* aMsg) {
  DBusMessageIter iter;
  nsresult rv = GetDBusIterator(aMsg, &iter);
  NS_ENSURE_SUCCESS(rv, rv);

  const char* path;
  do {
    if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_OBJECT_PATH) {
      return NS_ERROR_FAILURE;
    }
    dbus_message_iter_get_basic(&iter, &path);
    if (!path) {
      return NS_ERROR_FAILURE;
    }

    rv = SendGetAPProperties(path);
    NS_ENSURE_SUCCESS(rv, rv);
  } while (dbus_message_iter_next(&iter));

  return NS_OK;
}

nsresult nsWifiScannerDBus::IdentifyAPProperties(DBusMessage* aMsg) {
  DBusMessageIter arr;
  nsresult rv = GetDBusIterator(aMsg, &arr);
  NS_ENSURE_SUCCESS(rv, rv);

  RefPtr<nsWifiAccessPoint> ap = new nsWifiAccessPoint();
  do {
    DBusMessageIter dict;
    dbus_message_iter_recurse(&arr, &dict);

    do {
      const char* key;
      dbus_message_iter_get_basic(&dict, &key);
      if (!key) {
        return NS_ERROR_FAILURE;
      }
      dbus_message_iter_next(&dict);

      DBusMessageIter variant;
      dbus_message_iter_recurse(&dict, &variant);

      if (!strncmp(key, "Ssid", strlen("Ssid"))) {
        nsresult rv = StoreSsid(&variant, ap);
        NS_ENSURE_SUCCESS(rv, rv);
        break;
      }

      if (!strncmp(key, "HwAddress", strlen("HwAddress"))) {
        nsresult rv = SetMac(&variant, ap);
        NS_ENSURE_SUCCESS(rv, rv);
        break;
      }

      if (!strncmp(key, "Strength", strlen("Strength"))) {
        if (dbus_message_iter_get_arg_type(&variant) != DBUS_TYPE_BYTE) {
          return NS_ERROR_FAILURE;
        }

        uint8_t strength;  // in %
        dbus_message_iter_get_basic(&variant, &strength);
        int signal_strength = (strength / 2) - 100;  // strength to dB
        ap->setSignal(signal_strength);
      }
    } while (dbus_message_iter_next(&dict));
  } while (dbus_message_iter_next(&arr));

  mAccessPoints->AppendObject(ap);
  return NS_OK;
}

nsresult nsWifiScannerDBus::StoreSsid(DBusMessageIter* aVariant,
                                      nsWifiAccessPoint* aAp) {
  if (dbus_message_iter_get_arg_type(aVariant) != DBUS_TYPE_ARRAY) {
    return NS_ERROR_FAILURE;
  }

  DBusMessageIter variantMember;
  dbus_message_iter_recurse(aVariant, &variantMember);

  const uint32_t MAX_SSID_LEN = 32;
  char ssid[MAX_SSID_LEN];
  memset(ssid, '\0', ArrayLength(ssid));
  uint32_t i = 0;
  do {
    if (dbus_message_iter_get_arg_type(&variantMember) != DBUS_TYPE_BYTE) {
      return NS_ERROR_FAILURE;
    }

    dbus_message_iter_get_basic(&variantMember, &ssid[i]);
    i++;
  } while (dbus_message_iter_next(&variantMember) && i < MAX_SSID_LEN);

  aAp->setSSID(ssid, i);
  return NS_OK;
}

nsresult nsWifiScannerDBus::SetMac(DBusMessageIter* aVariant,
                                   nsWifiAccessPoint* aAp) {
  if (dbus_message_iter_get_arg_type(aVariant) != DBUS_TYPE_STRING) {
    return NS_ERROR_FAILURE;
  }

  // hwAddress format is XX:XX:XX:XX:XX:XX. Need to convert to XXXXXX format.
  char* hwAddress;
  dbus_message_iter_get_basic(aVariant, &hwAddress);
  if (!hwAddress) {
    return NS_ERROR_FAILURE;
  }

  const uint32_t MAC_LEN = 6;
  uint8_t macAddress[MAC_LEN];
  char* token = strtok(hwAddress, ":");
  for (uint32_t i = 0; i < ArrayLength(macAddress); i++) {
    if (!token) {
      return NS_ERROR_FAILURE;
    }
    macAddress[i] = strtoul(token, nullptr, 16);
    token = strtok(nullptr, ":");
  }
  aAp->setMac(macAddress);
  return NS_OK;
}

nsresult nsWifiScannerDBus::GetDBusIterator(DBusMessage* aMsg,
                                            DBusMessageIter* aIterArray) {
  DBusMessageIter iter;
  if (!dbus_message_iter_init(aMsg, &iter)) {
    return NS_ERROR_FAILURE;
  }

  if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
    return NS_ERROR_FAILURE;
  }

  dbus_message_iter_recurse(&iter, aIterArray);
  return NS_OK;
}

}  // namespace mozilla

nsresult nsWifiMonitor::DoScan() {
  nsCOMArray<nsWifiAccessPoint> accessPoints;
  mozilla::nsWifiScannerDBus wifiScanner(&accessPoints);
  nsCOMArray<nsWifiAccessPoint> lastAccessPoints;

  while (mKeepGoing) {
    accessPoints.Clear();
    nsresult rv = wifiScanner.Scan();
    NS_ENSURE_SUCCESS(rv, rv);
    bool accessPointsChanged =
        !AccessPointsEqual(accessPoints, lastAccessPoints);
    ReplaceArray(lastAccessPoints, accessPoints);

    rv = CallWifiListeners(lastAccessPoints, accessPointsChanged);
    NS_ENSURE_SUCCESS(rv, rv);

    LOG(("waiting on monitor\n"));
    mozilla::ReentrantMonitorAutoEnter mon(mReentrantMonitor);
    mon.Wait(PR_SecondsToInterval(kDefaultWifiScanInterval));
  }

  return NS_OK;
}