1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #include "nsWifiAccessPoint.h"
6 #include "nsString.h"
7 #include "nsMemory.h"
8 #include "mozilla/Logging.h"
9 
10 extern mozilla::LazyLogModule gWifiMonitorLog;
11 #define LOG(args) MOZ_LOG(gWifiMonitorLog, mozilla::LogLevel::Debug, args)
12 
NS_IMPL_ISUPPORTS(nsWifiAccessPoint,nsIWifiAccessPoint)13 NS_IMPL_ISUPPORTS(nsWifiAccessPoint, nsIWifiAccessPoint)
14 
15 nsWifiAccessPoint::nsWifiAccessPoint() {
16   // make sure these are null terminated (because we are paranoid)
17   mMac[0] = '\0';
18   mSsid[0] = '\0';
19   mSsidLen = 0;
20   mSignal = -1000;
21 }
22 
GetMac(nsACString & aMac)23 NS_IMETHODIMP nsWifiAccessPoint::GetMac(nsACString& aMac) {
24   aMac.Assign(mMac);
25   return NS_OK;
26 }
27 
GetSsid(nsAString & aSsid)28 NS_IMETHODIMP nsWifiAccessPoint::GetSsid(nsAString& aSsid) {
29   // just assign and embedded nulls will truncate resulting
30   // in a displayable string.
31   aSsid.AssignASCII(mSsid);
32   return NS_OK;
33 }
34 
GetRawSSID(nsACString & aRawSsid)35 NS_IMETHODIMP nsWifiAccessPoint::GetRawSSID(nsACString& aRawSsid) {
36   aRawSsid.Assign(mSsid, mSsidLen);  // SSIDs are 32 chars long
37   return NS_OK;
38 }
39 
GetSignal(int32_t * aSignal)40 NS_IMETHODIMP nsWifiAccessPoint::GetSignal(int32_t* aSignal) {
41   NS_ENSURE_ARG(aSignal);
42   *aSignal = mSignal;
43   return NS_OK;
44 }
45 
46 // Helper functions:
47 
AccessPointsEqual(nsCOMArray<nsWifiAccessPoint> & a,nsCOMArray<nsWifiAccessPoint> & b)48 bool AccessPointsEqual(nsCOMArray<nsWifiAccessPoint>& a,
49                        nsCOMArray<nsWifiAccessPoint>& b) {
50   if (a.Count() != b.Count()) {
51     LOG(("AccessPoint lists have different lengths\n"));
52     return false;
53   }
54 
55   for (int32_t i = 0; i < a.Count(); i++) {
56     LOG(("++ Looking for %s\n", a[i]->mSsid));
57     bool found = false;
58     for (int32_t j = 0; j < b.Count(); j++) {
59       LOG(("   %s->%s | %s->%s\n", a[i]->mSsid, b[j]->mSsid, a[i]->mMac,
60            b[j]->mMac));
61       if (!strcmp(a[i]->mSsid, b[j]->mSsid) &&
62           !strcmp(a[i]->mMac, b[j]->mMac) && a[i]->mSignal == b[j]->mSignal) {
63         found = true;
64       }
65     }
66     if (!found) return false;
67   }
68   LOG(("   match!\n"));
69   return true;
70 }
71 
ReplaceArray(nsCOMArray<nsWifiAccessPoint> & a,nsCOMArray<nsWifiAccessPoint> & b)72 void ReplaceArray(nsCOMArray<nsWifiAccessPoint>& a,
73                   nsCOMArray<nsWifiAccessPoint>& b) {
74   a.Clear();
75 
76   // better way to copy?
77   for (int32_t i = 0; i < b.Count(); i++) {
78     a.AppendObject(b[i]);
79   }
80 
81   b.Clear();
82 }
83