1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // For OSX 10.5 we use the system API function WirelessScanSplit. This function
6 // is not documented or included in the SDK, so we use a reverse-engineered
7 // header, osx_wifi_.h. This file is taken from the iStumbler project
8 // (http://www.istumbler.net).
10 #include "content/browser/geolocation/wifi_data_provider_mac.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "content/browser/geolocation/osx_wifi.h"
17 #include "content/browser/geolocation/wifi_data_provider_common.h"
21 // The time periods, in milliseconds, between successive polls of the wifi data.
22 const int kDefaultPollingInterval
= 120000; // 2 mins
23 const int kNoChangePollingInterval
= 300000; // 5 mins
24 const int kTwoNoChangePollingInterval
= 600000; // 10 mins
25 const int kNoWifiPollingIntervalMilliseconds
= 20 * 1000; // 20s
27 // Provides the wifi API binding for use when running on OSX 10.5 machines using
28 // the Apple80211 framework.
29 class Apple80211Api
: public WifiDataProviderCommon::WlanApiInterface
{
32 virtual ~Apple80211Api();
34 // Must be called before any other interface method. Will return false if the
35 // Apple80211 framework cannot be initialized (e.g. running on post-10.5 OSX),
36 // in which case no other method may be called.
40 virtual bool GetAccessPointData(WifiData::AccessPointDataSet
* data
) OVERRIDE
;
43 // Handle, context and function pointers for Apple80211 library.
44 void* apple_80211_library_
;
45 WirelessContext
* wifi_context_
;
46 WirelessAttachFunction WirelessAttach_function_
;
47 WirelessScanSplitFunction WirelessScanSplit_function_
;
48 WirelessDetachFunction WirelessDetach_function_
;
52 DISALLOW_COPY_AND_ASSIGN(Apple80211Api
);
55 Apple80211Api::Apple80211Api()
56 : apple_80211_library_(NULL
), wifi_context_(NULL
),
57 WirelessAttach_function_(NULL
), WirelessScanSplit_function_(NULL
),
58 WirelessDetach_function_(NULL
) {
61 Apple80211Api::~Apple80211Api() {
62 if (WirelessDetach_function_
)
63 (*WirelessDetach_function_
)(wifi_context_
);
64 dlclose(apple_80211_library_
);
67 bool Apple80211Api::Init() {
68 DVLOG(1) << "Apple80211Api::Init";
69 apple_80211_library_
= dlopen(
70 "/System/Library/PrivateFrameworks/Apple80211.framework/Apple80211",
72 if (!apple_80211_library_
) {
73 DLOG(WARNING
) << "Could not open Apple80211 library";
76 WirelessAttach_function_
= reinterpret_cast<WirelessAttachFunction
>(
77 dlsym(apple_80211_library_
, "WirelessAttach"));
78 WirelessScanSplit_function_
= reinterpret_cast<WirelessScanSplitFunction
>(
79 dlsym(apple_80211_library_
, "WirelessScanSplit"));
80 WirelessDetach_function_
= reinterpret_cast<WirelessDetachFunction
>(
81 dlsym(apple_80211_library_
, "WirelessDetach"));
82 DCHECK(WirelessAttach_function_
);
83 DCHECK(WirelessScanSplit_function_
);
84 DCHECK(WirelessDetach_function_
);
86 if (!WirelessAttach_function_
|| !WirelessScanSplit_function_
||
87 !WirelessDetach_function_
) {
88 DLOG(WARNING
) << "Symbol error. Attach: " << !!WirelessAttach_function_
89 << " Split: " << !!WirelessScanSplit_function_
90 << " Detach: " << !!WirelessDetach_function_
;
94 WIErr err
= (*WirelessAttach_function_
)(&wifi_context_
, 0);
96 DLOG(WARNING
) << "Error attaching: " << err
;
102 bool Apple80211Api::GetAccessPointData(WifiData::AccessPointDataSet
* data
) {
103 DVLOG(1) << "Apple80211Api::GetAccessPointData";
105 DCHECK(WirelessScanSplit_function_
);
106 CFArrayRef managed_access_points
= NULL
;
107 CFArrayRef adhoc_access_points
= NULL
;
108 // Arrays returned here are owned by the caller.
109 WIErr err
= (*WirelessScanSplit_function_
)(wifi_context_
,
110 &managed_access_points
,
111 &adhoc_access_points
,
114 DLOG(WARNING
) << "Error spliting scan: " << err
;
118 if (managed_access_points
== NULL
) {
119 DLOG(WARNING
) << "managed_access_points == NULL";
123 int num_access_points
= CFArrayGetCount(managed_access_points
);
124 DVLOG(1) << "Found " << num_access_points
<< " managed access points";
125 for (int i
= 0; i
< num_access_points
; ++i
) {
126 const WirelessNetworkInfo
* access_point_info
=
127 reinterpret_cast<const WirelessNetworkInfo
*>(
129 reinterpret_cast<const CFDataRef
>(
130 CFArrayGetValueAtIndex(managed_access_points
, i
))));
132 // Currently we get only MAC address, signal strength, channel
133 // signal-to-noise and SSID
134 AccessPointData access_point_data
;
135 access_point_data
.mac_address
=
136 MacAddressAsString16(access_point_info
->macAddress
);
137 // WirelessNetworkInfo::signal appears to be signal strength in dBm.
138 access_point_data
.radio_signal_strength
= access_point_info
->signal
;
139 access_point_data
.channel
= access_point_info
->channel
;
140 // WirelessNetworkInfo::noise appears to be noise floor in dBm.
141 access_point_data
.signal_to_noise
= access_point_info
->signal
-
142 access_point_info
->noise
;
143 if (!base::UTF8ToUTF16(reinterpret_cast<const char*>(
144 access_point_info
->name
),
145 access_point_info
->nameLen
,
146 &access_point_data
.ssid
)) {
147 access_point_data
.ssid
.clear();
149 data
->insert(access_point_data
);
152 if (managed_access_points
)
153 CFRelease(managed_access_points
);
154 if (adhoc_access_points
)
155 CFRelease(adhoc_access_points
);
162 WifiDataProviderImplBase
* WifiDataProvider::DefaultFactoryFunction() {
163 return new MacWifiDataProvider();
166 MacWifiDataProvider::MacWifiDataProvider() {
169 MacWifiDataProvider::~MacWifiDataProvider() {
172 MacWifiDataProvider::WlanApiInterface
* MacWifiDataProvider::NewWlanApi() {
173 // Try and find a API binding that works: first try the officially supported
174 // CoreWLAN API, and if this fails (e.g. on OSX 10.5) fall back to the reverse
175 // engineered Apple80211 API.
176 MacWifiDataProvider::WlanApiInterface
* core_wlan_api
= NewCoreWlanApi();
178 return core_wlan_api
;
180 scoped_ptr
<Apple80211Api
> wlan_api(new Apple80211Api
);
181 if (wlan_api
->Init())
182 return wlan_api
.release();
184 DVLOG(1) << "MacWifiDataProvider : failed to initialize any wlan api";
188 WifiPollingPolicy
* MacWifiDataProvider::NewPollingPolicy() {
189 return new GenericWifiPollingPolicy
<kDefaultPollingInterval
,
190 kNoChangePollingInterval
,
191 kTwoNoChangePollingInterval
,
192 kNoWifiPollingIntervalMilliseconds
>;
195 } // namespace content