summaryrefslogtreecommitdiff
path: root/chromium/device/geolocation/wifi_data_provider_common_win.cc
blob: 3e6bd971cdca2ab16d5aea3f5354c4d94e18b599 (plain)
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
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "device/geolocation/wifi_data_provider_common_win.h"

#include <assert.h>
#include <stdint.h>

#include "base/strings/utf_string_conversions.h"
#include "device/geolocation/wifi_data_provider_common.h"

namespace device {

bool ConvertToAccessPointData(const NDIS_WLAN_BSSID& data,
                              AccessPointData* access_point_data) {
  // Currently we get only MAC address, signal strength and SSID.
  // TODO(steveblock): Work out how to get age, channel and signal-to-noise.
  DCHECK(access_point_data);
  access_point_data->mac_address = MacAddressAsString16(data.MacAddress);
  access_point_data->radio_signal_strength = data.Rssi;
  // Note that _NDIS_802_11_SSID::Ssid::Ssid is not null-terminated.
  base::UTF8ToUTF16(reinterpret_cast<const char*>(data.Ssid.Ssid),
                    data.Ssid.SsidLength, &access_point_data->ssid);
  return true;
}

int GetDataFromBssIdList(const NDIS_802_11_BSSID_LIST& bss_id_list,
                         int list_size,
                         WifiData::AccessPointDataSet* data) {
  // Walk through the BSS IDs.
  int found = 0;
  const uint8_t* iterator =
      reinterpret_cast<const uint8_t*>(&bss_id_list.Bssid[0]);
  const uint8_t* end_of_buffer =
      reinterpret_cast<const uint8_t*>(&bss_id_list) + list_size;
  for (int i = 0; i < static_cast<int>(bss_id_list.NumberOfItems); ++i) {
    const NDIS_WLAN_BSSID* bss_id =
        reinterpret_cast<const NDIS_WLAN_BSSID*>(iterator);
    // Check that the length of this BSS ID is reasonable.
    if (bss_id->Length < sizeof(NDIS_WLAN_BSSID) ||
        iterator + bss_id->Length > end_of_buffer) {
      break;
    }
    AccessPointData access_point_data;
    if (ConvertToAccessPointData(*bss_id, &access_point_data)) {
      data->insert(access_point_data);
      ++found;
    }
    // Move to the next BSS ID.
    iterator += bss_id->Length;
  }
  return found;
}

}  // namespace device