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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsCOMPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsThreadUtils.h"
#include "nsXPCOM.h"
#include "nsXPCOMCID.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsWifiMonitor.h"
#include "nsWifiAccessPoint.h"
#include "nsServiceManagerUtils.h"
#include "nsComponentManagerUtils.h"
#include "mozilla/Services.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
using namespace mozilla;
LazyLogModule gWifiMonitorLog("WifiMonitor");
NS_IMPL_ISUPPORTS(nsWifiMonitor,
nsIWifiMonitor,
nsIObserver,
nsIWifiScanResultsReady)
nsWifiMonitor::nsWifiMonitor()
{
nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService();
if (obsSvc) {
obsSvc->AddObserver(this, "xpcom-shutdown", false);
}
LOG(("@@@@@ wifimonitor created\n"));
}
nsWifiMonitor::~nsWifiMonitor()
{
}
NS_IMETHODIMP
nsWifiMonitor::StartWatching(nsIWifiListener *aListener)
{
LOG(("@@@@@ nsWifiMonitor::StartWatching\n"));
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
if (!aListener) {
return NS_ERROR_NULL_POINTER;
}
mListeners.AppendElement(nsWifiListener(new nsMainThreadPtrHolder<nsIWifiListener>(aListener)));
if (!mTimer) {
mTimer = do_CreateInstance("@mozilla.org/timer;1");
mTimer->Init(this, 5000, nsITimer::TYPE_REPEATING_SLACK);
}
StartScan();
return NS_OK;
}
NS_IMETHODIMP
nsWifiMonitor::StopWatching(nsIWifiListener *aListener)
{
LOG(("@@@@@ nsWifiMonitor::StopWatching\n"));
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
if (!aListener) {
return NS_ERROR_NULL_POINTER;
}
for (uint32_t i = 0; i < mListeners.Length(); i++) {
if (mListeners[i].mListener == aListener) {
mListeners.RemoveElementAt(i);
break;
}
}
if (mListeners.Length() == 0) {
ClearTimer();
}
return NS_OK;
}
void
nsWifiMonitor::StartScan()
{
nsCOMPtr<nsIInterfaceRequestor> ir = do_GetService("@mozilla.org/telephony/system-worker-manager;1");
nsCOMPtr<nsIWifi> wifi = do_GetInterface(ir);
if (!wifi) {
return;
}
wifi->GetWifiScanResults(this);
}
NS_IMETHODIMP
nsWifiMonitor::Observe(nsISupports *subject, const char *topic,
const char16_t *data)
{
if (!strcmp(topic, "timer-callback")) {
LOG(("timer callback\n"));
StartScan();
return NS_OK;
}
if (!strcmp(topic, "xpcom-shutdown")) {
LOG(("Shutting down\n"));
ClearTimer();
return NS_OK;
}
return NS_ERROR_UNEXPECTED;
}
NS_IMETHODIMP
nsWifiMonitor::Onready(uint32_t count, nsIWifiScanResult **results)
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
LOG(("@@@@@ About to send data to the wifi listeners\n"));
nsCOMArray<nsWifiAccessPoint> accessPoints;
for (uint32_t i = 0; i < count; i++) {
RefPtr<nsWifiAccessPoint> ap = new nsWifiAccessPoint();
nsString temp;
results[i]->GetBssid(temp);
// 00:00:00:00:00:00 --> 00-00-00-00-00-00
for (int32_t x=0; x<6; x++) {
temp.ReplaceSubstring(NS_LITERAL_STRING(":"), NS_LITERAL_STRING("-")); // would it be too much to ask for a ReplaceAll()?
}
nsCString mac;
mac.AssignWithConversion(temp);
results[i]->GetSsid(temp);
nsCString ssid;
ssid.AssignWithConversion(temp);
uint32_t signal;
results[i]->GetSignalStrength(&signal);
ap->setSignal(signal);
ap->setMacRaw(mac.get());
ap->setSSIDRaw(ssid.get(), ssid.Length());
accessPoints.AppendObject(ap);
}
bool accessPointsChanged = !AccessPointsEqual(accessPoints, mLastAccessPoints);
ReplaceArray(mLastAccessPoints, accessPoints);
nsTArray<nsIWifiAccessPoint*> ac;
uint32_t resultCount = mLastAccessPoints.Count();
for (uint32_t i = 0; i < resultCount; i++) {
ac.AppendElement(mLastAccessPoints[i]);
}
for (uint32_t i = 0; i < mListeners.Length(); i++) {
if (!mListeners[i].mHasSentData || accessPointsChanged) {
mListeners[i].mHasSentData = true;
mListeners[i].mListener->OnChange(ac.Elements(), ac.Length());
}
}
return NS_OK;
}
NS_IMETHODIMP
nsWifiMonitor::Onfailure()
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
LOG(("@@@@@ About to send error to the wifi listeners\n"));
for (uint32_t i = 0; i < mListeners.Length(); i++) {
mListeners[i].mListener->OnError(NS_ERROR_UNEXPECTED);
}
ClearTimer();
return NS_OK;
}
|