nsWifiAccessPoint.cpp 2.2 KB

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