1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/wifi/nsWifiMonitorGonk.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,182 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "nsCOMPtr.h" 1.9 +#include "nsComponentManagerUtils.h" 1.10 +#include "nsServiceManagerUtils.h" 1.11 +#include "nsThreadUtils.h" 1.12 +#include "nsXPCOM.h" 1.13 +#include "nsXPCOMCID.h" 1.14 +#include "nsIObserver.h" 1.15 +#include "nsIObserverService.h" 1.16 +#include "nsWifiMonitor.h" 1.17 +#include "nsWifiAccessPoint.h" 1.18 + 1.19 +#include "nsServiceManagerUtils.h" 1.20 +#include "nsComponentManagerUtils.h" 1.21 +#include "mozilla/Services.h" 1.22 + 1.23 +#include "nsIInterfaceRequestor.h" 1.24 +#include "nsIInterfaceRequestorUtils.h" 1.25 + 1.26 +using namespace mozilla; 1.27 + 1.28 +#if defined(PR_LOGGING) 1.29 +PRLogModuleInfo *gWifiMonitorLog; 1.30 +#endif 1.31 + 1.32 +NS_IMPL_ISUPPORTS(nsWifiMonitor, 1.33 + nsIWifiMonitor, 1.34 + nsIObserver, 1.35 + nsIWifiScanResultsReady) 1.36 + 1.37 +nsWifiMonitor::nsWifiMonitor() 1.38 +{ 1.39 +#if defined(PR_LOGGING) 1.40 + gWifiMonitorLog = PR_NewLogModule("WifiMonitor"); 1.41 +#endif 1.42 + 1.43 + nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService(); 1.44 + if (obsSvc) { 1.45 + obsSvc->AddObserver(this, "xpcom-shutdown", false); 1.46 + } 1.47 + LOG(("@@@@@ wifimonitor created\n")); 1.48 +} 1.49 + 1.50 +nsWifiMonitor::~nsWifiMonitor() 1.51 +{ 1.52 +} 1.53 + 1.54 +NS_IMETHODIMP 1.55 +nsWifiMonitor::StartWatching(nsIWifiListener *aListener) 1.56 +{ 1.57 + LOG(("@@@@@ nsWifiMonitor::StartWatching\n")); 1.58 + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); 1.59 + if (!aListener) { 1.60 + return NS_ERROR_NULL_POINTER; 1.61 + } 1.62 + 1.63 + mListeners.AppendElement(nsWifiListener(new nsMainThreadPtrHolder<nsIWifiListener>(aListener))); 1.64 + 1.65 + if (!mTimer) { 1.66 + mTimer = do_CreateInstance("@mozilla.org/timer;1"); 1.67 + mTimer->Init(this, 5000, nsITimer::TYPE_REPEATING_SLACK); 1.68 + } 1.69 + return NS_OK; 1.70 +} 1.71 + 1.72 +NS_IMETHODIMP 1.73 +nsWifiMonitor::StopWatching(nsIWifiListener *aListener) 1.74 +{ 1.75 + LOG(("@@@@@ nsWifiMonitor::StopWatching\n")); 1.76 + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); 1.77 + if (!aListener) { 1.78 + return NS_ERROR_NULL_POINTER; 1.79 + } 1.80 + 1.81 + for (uint32_t i = 0; i < mListeners.Length(); i++) { 1.82 + if (mListeners[i].mListener == aListener) { 1.83 + mListeners.RemoveElementAt(i); 1.84 + break; 1.85 + } 1.86 + } 1.87 + 1.88 + if (mListeners.Length() == 0) { 1.89 + ClearTimer(); 1.90 + } 1.91 + return NS_OK; 1.92 +} 1.93 + 1.94 +NS_IMETHODIMP 1.95 +nsWifiMonitor::Observe(nsISupports *subject, const char *topic, 1.96 + const char16_t *data) 1.97 +{ 1.98 + if (!strcmp(topic, "timer-callback")) { 1.99 + LOG(("timer callback\n")); 1.100 + 1.101 + nsCOMPtr<nsIInterfaceRequestor> ir = do_GetService("@mozilla.org/telephony/system-worker-manager;1"); 1.102 + nsCOMPtr<nsIWifi> wifi = do_GetInterface(ir); 1.103 + if (!wifi) { 1.104 + return NS_ERROR_UNEXPECTED; 1.105 + } 1.106 + 1.107 + wifi->GetWifiScanResults(this); 1.108 + return NS_OK; 1.109 + } 1.110 + 1.111 + if (!strcmp(topic, "xpcom-shutdown")) { 1.112 + LOG(("Shutting down\n")); 1.113 + ClearTimer(); 1.114 + return NS_OK; 1.115 + } 1.116 + 1.117 + return NS_ERROR_UNEXPECTED; 1.118 +} 1.119 + 1.120 +NS_IMETHODIMP 1.121 +nsWifiMonitor::Onready(uint32_t count, nsIWifiScanResult **results) 1.122 +{ 1.123 + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); 1.124 + LOG(("@@@@@ About to send data to the wifi listeners\n")); 1.125 + 1.126 + nsCOMArray<nsWifiAccessPoint> accessPoints; 1.127 + 1.128 + for (uint32_t i = 0; i < count; i++) { 1.129 + nsRefPtr<nsWifiAccessPoint> ap = new nsWifiAccessPoint(); 1.130 + 1.131 + nsString temp; 1.132 + results[i]->GetBssid(temp); 1.133 + // 00:00:00:00:00:00 --> 00-00-00-00-00-00 1.134 + for (int32_t x=0; x<6; x++) { 1.135 + temp.ReplaceSubstring(NS_LITERAL_STRING(":"), NS_LITERAL_STRING("-")); // would it be too much to ask for a ReplaceAll()? 1.136 + } 1.137 + 1.138 + nsCString mac; 1.139 + mac.AssignWithConversion(temp); 1.140 + 1.141 + results[i]->GetSsid(temp); 1.142 + 1.143 + nsCString ssid; 1.144 + ssid.AssignWithConversion(temp); 1.145 + 1.146 + uint32_t signal; 1.147 + results[i]->GetSignalStrength(&signal); 1.148 + 1.149 + ap->setSignal(signal); 1.150 + ap->setMacRaw(mac.get()); 1.151 + ap->setSSIDRaw(ssid.get(), ssid.Length()); 1.152 + 1.153 + accessPoints.AppendObject(ap); 1.154 + } 1.155 + 1.156 + bool accessPointsChanged = !AccessPointsEqual(accessPoints, mLastAccessPoints); 1.157 + ReplaceArray(mLastAccessPoints, accessPoints); 1.158 + 1.159 + nsTArray<nsIWifiAccessPoint*> ac; 1.160 + uint32_t resultCount = mLastAccessPoints.Count(); 1.161 + for (uint32_t i = 0; i < resultCount; i++) { 1.162 + ac.AppendElement(mLastAccessPoints[i]); 1.163 + } 1.164 + 1.165 + for (uint32_t i = 0; i < mListeners.Length(); i++) { 1.166 + if (!mListeners[i].mHasSentData || accessPointsChanged) { 1.167 + mListeners[i].mHasSentData = true; 1.168 + mListeners[i].mListener->OnChange(ac.Elements(), ac.Length()); 1.169 + } 1.170 + } 1.171 + return NS_OK; 1.172 +} 1.173 + 1.174 +NS_IMETHODIMP 1.175 +nsWifiMonitor::Onfailure() 1.176 +{ 1.177 + NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); 1.178 + LOG(("@@@@@ About to send error to the wifi listeners\n")); 1.179 + for (uint32_t i = 0; i < mListeners.Length(); i++) { 1.180 + mListeners[i].mListener->OnError(NS_ERROR_UNEXPECTED); 1.181 + } 1.182 + 1.183 + ClearTimer(); 1.184 + return NS_OK; 1.185 +}