osx_corewlan.mm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #import <Cocoa/Cocoa.h>
  5. #import <CoreWLAN/CoreWLAN.h>
  6. #include <dlfcn.h>
  7. #include <unistd.h>
  8. #include <objc/objc.h>
  9. #include <objc/objc-runtime.h>
  10. #include "nsObjCExceptions.h"
  11. #include "nsAutoPtr.h"
  12. #include "nsCOMArray.h"
  13. #include "nsWifiMonitor.h"
  14. #include "nsWifiAccessPoint.h"
  15. nsresult
  16. GetAccessPointsFromWLAN(nsCOMArray<nsWifiAccessPoint> &accessPoints)
  17. {
  18. NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
  19. accessPoints.Clear();
  20. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  21. @try {
  22. NSBundle * bundle = [[[NSBundle alloc] initWithPath:@"/System/Library/Frameworks/CoreWLAN.framework"] autorelease];
  23. if (!bundle) {
  24. [pool release];
  25. return NS_ERROR_NOT_AVAILABLE;
  26. }
  27. Class CWI_class = [bundle classNamed:@"CWInterface"];
  28. if (!CWI_class) {
  29. [pool release];
  30. return NS_ERROR_NOT_AVAILABLE;
  31. }
  32. id scanResult = [[CWI_class interface] scanForNetworksWithParameters:nil error:nil];
  33. if (!scanResult) {
  34. [pool release];
  35. return NS_ERROR_NOT_AVAILABLE;
  36. }
  37. NSArray* scan = [NSMutableArray arrayWithArray:scanResult];
  38. NSEnumerator *enumerator = [scan objectEnumerator];
  39. while (id anObject = [enumerator nextObject]) {
  40. auto* ap = new nsWifiAccessPoint();
  41. if (!ap) {
  42. [pool release];
  43. return NS_ERROR_OUT_OF_MEMORY;
  44. }
  45. // [CWInterface bssidData] is deprecated on OS X 10.7 and up. Which is
  46. // is a pain, so we'll use it for as long as it's available.
  47. unsigned char macData[6] = {0};
  48. if ([anObject respondsToSelector:@selector(bssidData)]) {
  49. NSData* data = [anObject bssidData];
  50. if (data) {
  51. memcpy(macData, [data bytes], 6);
  52. }
  53. } else {
  54. // [CWInterface bssid] returns a string formatted "00:00:00:00:00:00".
  55. NSString* macString = [anObject bssid];
  56. if (macString && ([macString length] == 17)) {
  57. for (NSUInteger i = 0; i < 6; ++i) {
  58. NSString* part = [macString substringWithRange:NSMakeRange(i * 3, 2)];
  59. NSScanner* scanner = [NSScanner scannerWithString:part];
  60. unsigned int data = 0;
  61. if (![scanner scanHexInt:&data]) {
  62. data = 0;
  63. }
  64. macData[i] = (unsigned char) data;
  65. }
  66. }
  67. }
  68. // [CWInterface rssiValue] is available on OS X 10.7 and up (and
  69. // [CWInterface rssi] is deprecated).
  70. int signal = 0;
  71. if ([anObject respondsToSelector:@selector(rssiValue)]) {
  72. signal = (int) ((NSInteger) [anObject rssiValue]);
  73. } else {
  74. signal = [[anObject rssi] intValue];
  75. }
  76. ap->setMac(macData);
  77. ap->setSignal(signal);
  78. ap->setSSID([[anObject ssid] UTF8String], 32);
  79. accessPoints.AppendObject(ap);
  80. }
  81. }
  82. @catch(NSException*) {
  83. [pool release];
  84. return NS_ERROR_NOT_AVAILABLE;
  85. }
  86. [pool release];
  87. return NS_OK;
  88. NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NS_ERROR_NOT_AVAILABLE);
  89. }