author Doug Turner <>
Mon, 16 Mar 2009 17:30:58 -0700
changeset 23898 042250237c7d909b1b1d596828c6715242c337c2
child 26398 63a535083f91f926d3db08b7c63b94149de7957c
permissions -rw-r--r--
Bug 479898 - Support for wifi scanning. r=dolske/stuart, sr=jst

/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 * The Original Code is Geolocation.
 * The Initial Developer of the Original Code is Mozilla Corporation
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 * This is a derivative of work done by Google under a BSD style License.
 * See:
 * Contributor(s):
 *  Doug Turner <>  (Original Author)
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * ***** END LICENSE BLOCK ***** */

#include <mach-o/dyld.h>
#include <dlfcn.h>
#include <unistd.h>

#include "osx_wifi.h"

#include "nsAutoPtr.h"
#include "nsCOMArray.h"
#include "nsWifiMonitor.h"
#include "nsWifiAccessPoint.h"

#include "nsIProxyObjectManager.h"
#include "nsServiceManagerUtils.h"
#include "nsComponentManagerUtils.h"
#include "nsIMutableArray.h"

  void *apple_80211_library = dlopen(

  WirelessContextPtr wifi_context_;

  WirelessAttachFunction WirelessAttach_function_ = reinterpret_cast<WirelessAttachFunction>(dlsym(apple_80211_library, "WirelessAttach"));
  WirelessScanSplitFunction WirelessScanSplit_function_ = reinterpret_cast<WirelessScanSplitFunction>(dlsym(apple_80211_library, "WirelessScanSplit"));
  WirelessDetachFunction WirelessDetach_function_ = reinterpret_cast<WirelessDetachFunction>(dlsym(apple_80211_library, "WirelessDetach"));
  if (!WirelessAttach_function_ || !WirelessScanSplit_function_ || !WirelessDetach_function_)

  if ((*WirelessAttach_function_)(&wifi_context_, 0) != noErr)
    return NS_ERROR_FAILURE;

  // Regularly get the access point data.
  nsCOMArray<nsWifiAccessPoint> lastAccessPoints;
  nsCOMArray<nsWifiAccessPoint> accessPoints;

  do {

    CFArrayRef managed_access_points = NULL;
    CFArrayRef adhoc_access_points = NULL;
    if ((*WirelessScanSplit_function_)(wifi_context_,
                                      0) != noErr) {
    if (managed_access_points == NULL) {

    int accessPointsCount = CFArrayGetCount(managed_access_points);

    for (int i = 0; i < accessPointsCount; ++i) {

      nsWifiAccessPoint* ap = new nsWifiAccessPoint();
      if (!ap)

      const WirelessNetworkInfo *access_point_info =
        reinterpret_cast<const WirelessNetworkInfo*>(CFDataGetBytePtr(reinterpret_cast<const CFDataRef>(CFArrayGetValueAtIndex(managed_access_points, i))));


      // WirelessNetworkInfo::signal appears to be signal strength in dBm.

      ap->setSSID(reinterpret_cast<const char*>(access_point_info->name),


    PRBool accessPointsChanged = !AccessPointsEqual(accessPoints, lastAccessPoints);
    nsCOMArray<nsIWifiListener> currentListeners;

      nsAutoMonitor mon(mMonitor);
      for (PRUint32 i = 0; i < mListeners.Length(); i++) {
        if (!mListeners[i].mHasSentData || accessPointsChanged) {
          mListeners[i].mHasSentData = PR_TRUE;
    ReplaceArray(lastAccessPoints, accessPoints);

    if (currentListeners.Count() > 0)
      PRUint32 resultCount = lastAccessPoints.Count();
      nsIWifiAccessPoint** result = static_cast<nsIWifiAccessPoint**> (nsMemory::Alloc(sizeof(nsIWifiAccessPoint*) * resultCount));
      if (!result)
        return NS_ERROR_OUT_OF_MEMORY;
      for (PRUint32 i = 0; i < resultCount; i++)
        result[i] = lastAccessPoints[i];

      for (PRInt32 i = 0; i < currentListeners.Count(); i++) {
        LOG(("About to send data to the wifi listeners\n"));
        nsCOMPtr<nsIWifiListener> proxy;
        nsCOMPtr<nsIProxyObjectManager> proxyObjMgr = do_GetService(";1");
                                       NS_PROXY_SYNC | NS_PROXY_ALWAYS,
        if (!proxy) {
          LOG(("There is no proxy available.  this should never happen\n"));
          nsresult rv = proxy->OnChange(result, resultCount);
          LOG( ("... sent %d\n", rv));


    // wait for some reasonable amount of time.  pref?
    LOG(("waiting on monitor\n"));
    nsAutoMonitor mon(mMonitor);
  while (mKeepGoing == PR_TRUE);
  return NS_OK;