-
Notifications
You must be signed in to change notification settings - Fork 9
/
wifiNotifer_darwin.go
95 lines (73 loc) · 2.67 KB
/
wifiNotifer_darwin.go
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
// +build darwin
package wifiNotifier
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa -framework SystemConfiguration -framework CoreWLAN -framework Foundation
#import <Cocoa/Cocoa.h>
#import <AppKit/NSApplication.h>
#include <stdlib.h>
#import <Foundation/Foundation.h>
#import <CoreWLAN/CoreWLAN.h>
#import <SystemConfiguration/SystemConfiguration.h>
static inline char* nsstring2cstring(NSString *s){
if (s == NULL) { return NULL; }
char *cstr = strdup([s UTF8String]);
return cstr;
}
#define NOT_CONNECTED @""
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
static inline void wifi_network_changed(SCDynamicStoreRef store, CFArrayRef changedKeys, void *ctx)
{
//NSLog(@"wifi_network_changed");
CWInterface *WiFiInterface = [CWInterface interface];
NSString *currentSSID = [WiFiInterface ssid] ? [WiFiInterface ssid] : NOT_CONNECTED;
extern void __onWifiChanged(char *);
__onWifiChanged(nsstring2cstring(currentSSID));
}
static inline char * getCurrentSSID(void) {
CWInterface *WiFiInterface = [CWInterface interface];
NSString *ssid = [WiFiInterface ssid] ? [WiFiInterface ssid] : NOT_CONNECTED;
return nsstring2cstring(ssid);
}
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
static inline void setWifiNotifier(void) {
CWInterface *WiFiInterface = [CWInterface interface];
NSSet *wifiInterfaces = [CWInterface interfaceNames];
NSMutableArray *scKeys = [[NSMutableArray alloc] init];
[wifiInterfaces enumerateObjectsUsingBlock:^(NSString *ifName, BOOL *stop)
{
[scKeys addObject: [NSString stringWithFormat:@"State:/Network/Interface/%@/AirPort", ifName]];
}];
SCDynamicStoreContext ctx = { 0, NULL, NULL, NULL, NULL };
SCDynamicStoreRef store = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("myapp"), wifi_network_changed, &ctx);
SCDynamicStoreSetNotificationKeys(store, (__bridge CFArrayRef)scKeys, NULL);
CFRunLoopSourceRef src = SCDynamicStoreCreateRunLoopSource(kCFAllocatorDefault, store, 0);
CFRunLoopAddSource([[NSRunLoop currentRunLoop] getCFRunLoop], src, kCFRunLoopCommonModes);
CFRunLoopRun();
}
*/
import "C"
import (
"unsafe"
)
var internalOnWifiChangedCb func(string)
var internalOnGetSSIDCb func(string)
//export __onWifiChanged
func __onWifiChanged(ssid *C.char) {
goSsid := C.GoString(ssid)
C.free(unsafe.Pointer(ssid))
if internalOnWifiChangedCb != nil {
internalOnWifiChangedCb(goSsid)
}
}
func GetCurrentSSID() string {
ssid := C.getCurrentSSID()
goSsid := C.GoString(ssid)
C.free(unsafe.Pointer(ssid))
return goSsid
}
func SetWifiNotifier(cb func(string)) {
internalOnWifiChangedCb = cb
go C.setWifiNotifier()
// log.Println("setWifiNotifier complated")
}