| OLD | NEW |
| (Empty) |
| 1 // Copyright 2008, Google Inc. | |
| 2 // | |
| 3 // Redistribution and use in source and binary forms, with or without | |
| 4 // modification, are permitted provided that the following conditions are met: | |
| 5 // | |
| 6 // 1. Redistributions of source code must retain the above copyright notice, | |
| 7 // this list of conditions and the following disclaimer. | |
| 8 // 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 9 // this list of conditions and the following disclaimer in the documentation | |
| 10 // and/or other materials provided with the distribution. | |
| 11 // 3. Neither the name of Google Inc. nor the names of its contributors may be | |
| 12 // used to endorse or promote products derived from this software without | |
| 13 // specific prior written permission. | |
| 14 // | |
| 15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
| 16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| 17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
| 18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
| 21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
| 23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
| 24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 25 // | |
| 26 // We use the OSX system API function WirelessScanSplit. This function is not | |
| 27 // documented or included in the SDK, so we use a reverse-engineered header, | |
| 28 // osx_wifi_.h. This file is taken from the iStumbler project | |
| 29 // (http://www.istumbler.net). | |
| 30 | |
| 31 // TODO(cprince): remove platform-specific #ifdef guards when OS-specific | |
| 32 // sources (e.g. WIN32_CPPSRCS) are implemented | |
| 33 #ifdef OS_MACOSX | |
| 34 | |
| 35 #include "gears/geolocation/wifi_data_provider_osx.h" | |
| 36 | |
| 37 #include <dlfcn.h> | |
| 38 #include <stdio.h> | |
| 39 #include "gears/base/common/string_utils.h" | |
| 40 #include "gears/geolocation/wifi_data_provider_common.h" | |
| 41 | |
| 42 // The time periods, in milliseconds, between successive polls of the wifi data. | |
| 43 extern const int kDefaultPollingInterval = 120000; // 2 mins | |
| 44 extern const int kNoChangePollingInterval = 300000; // 5 mins | |
| 45 extern const int kTwoNoChangePollingInterval = 600000; // 10 mins | |
| 46 | |
| 47 // static | |
| 48 template<> | |
| 49 WifiDataProviderImplBase *WifiDataProvider::DefaultFactoryFunction() { | |
| 50 return new OsxWifiDataProvider(); | |
| 51 } | |
| 52 | |
| 53 | |
| 54 OsxWifiDataProvider::OsxWifiDataProvider() : is_first_scan_complete_(false) { | |
| 55 Start(); | |
| 56 } | |
| 57 | |
| 58 OsxWifiDataProvider::~OsxWifiDataProvider() { | |
| 59 stop_event_.Signal(); | |
| 60 Join(); | |
| 61 } | |
| 62 | |
| 63 bool OsxWifiDataProvider::GetData(WifiData *data) { | |
| 64 assert(data); | |
| 65 MutexLock lock(&data_mutex_); | |
| 66 *data = wifi_data_; | |
| 67 // If we've successfully completed a scan, indicate that we have all of the | |
| 68 // data we can get. | |
| 69 return is_first_scan_complete_; | |
| 70 } | |
| 71 | |
| 72 // Thread implementation | |
| 73 void OsxWifiDataProvider::Run() { | |
| 74 void *apple_80211_library = dlopen( | |
| 75 "/System/Library/PrivateFrameworks/Apple80211.framework/Apple80211", | |
| 76 RTLD_LAZY); | |
| 77 if (!apple_80211_library) { | |
| 78 is_first_scan_complete_ = true; | |
| 79 return; | |
| 80 } | |
| 81 | |
| 82 WirelessAttach_function_ = reinterpret_cast<WirelessAttachFunction>( | |
| 83 dlsym(apple_80211_library, "WirelessAttach")); | |
| 84 WirelessScanSplit_function_ = reinterpret_cast<WirelessScanSplitFunction>( | |
| 85 dlsym(apple_80211_library, "WirelessScanSplit")); | |
| 86 WirelessDetach_function_ = reinterpret_cast<WirelessDetachFunction>( | |
| 87 dlsym(apple_80211_library, "WirelessDetach")); | |
| 88 assert(WirelessAttach_function_ && | |
| 89 WirelessScanSplit_function_ && | |
| 90 WirelessDetach_function_); | |
| 91 | |
| 92 if ((*WirelessAttach_function_)(&wifi_context_, 0) != noErr) { | |
| 93 is_first_scan_complete_ = true; | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 // Regularly get the access point data. | |
| 98 int polling_interval = kDefaultPollingInterval; | |
| 99 do { | |
| 100 WifiData new_data; | |
| 101 GetAccessPointData(&new_data.access_point_data); | |
| 102 bool update_available; | |
| 103 data_mutex_.Lock(); | |
| 104 update_available = wifi_data_.DiffersSignificantly(new_data); | |
| 105 wifi_data_ = new_data; | |
| 106 data_mutex_.Unlock(); | |
| 107 polling_interval = | |
| 108 UpdatePollingInterval(polling_interval, update_available); | |
| 109 if (update_available) { | |
| 110 is_first_scan_complete_ = true; | |
| 111 NotifyListeners(); | |
| 112 } | |
| 113 } while (!stop_event_.WaitWithTimeout(polling_interval)); | |
| 114 | |
| 115 (*WirelessDetach_function_)(wifi_context_); | |
| 116 | |
| 117 dlclose(apple_80211_library); | |
| 118 } | |
| 119 | |
| 120 void OsxWifiDataProvider::GetAccessPointData( | |
| 121 WifiData::AccessPointDataSet *access_points) { | |
| 122 assert(access_points); | |
| 123 assert(WirelessScanSplit_function_); | |
| 124 CFArrayRef managed_access_points = NULL; | |
| 125 CFArrayRef adhoc_access_points = NULL; | |
| 126 if ((*WirelessScanSplit_function_)(wifi_context_, | |
| 127 &managed_access_points, | |
| 128 &adhoc_access_points, | |
| 129 0) != noErr) { | |
| 130 return; | |
| 131 } | |
| 132 | |
| 133 if (managed_access_points == NULL) { | |
| 134 return; | |
| 135 } | |
| 136 | |
| 137 int num_access_points = CFArrayGetCount(managed_access_points); | |
| 138 for (int i = 0; i < num_access_points; ++i) { | |
| 139 const WirelessNetworkInfo *access_point_info = | |
| 140 reinterpret_cast<const WirelessNetworkInfo*>( | |
| 141 CFDataGetBytePtr( | |
| 142 reinterpret_cast<const CFDataRef>( | |
| 143 CFArrayGetValueAtIndex(managed_access_points, i)))); | |
| 144 | |
| 145 // Currently we get only MAC address, signal strength, channel | |
| 146 // signal-to-noise and SSID | |
| 147 // TODO(steveblock): Work out how to get age. | |
| 148 AccessPointData access_point_data; | |
| 149 access_point_data.mac_address = | |
| 150 MacAddressAsString16(access_point_info->macAddress); | |
| 151 // WirelessNetworkInfo::signal appears to be signal strength in dBm. | |
| 152 access_point_data.radio_signal_strength = access_point_info->signal; | |
| 153 access_point_data.channel = access_point_info->channel; | |
| 154 // WirelessNetworkInfo::noise appears to be noise floor in dBm. | |
| 155 access_point_data.signal_to_noise = access_point_info->signal - | |
| 156 access_point_info->noise; | |
| 157 std::string16 ssid; | |
| 158 if (UTF8ToString16(reinterpret_cast<const char*>(access_point_info->name), | |
| 159 access_point_info->nameLen, | |
| 160 &ssid)) { | |
| 161 access_point_data.ssid = ssid; | |
| 162 } | |
| 163 | |
| 164 access_points->insert(access_point_data); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 #endif // OS_MACOSX | |
| OLD | NEW |