forked from linuxdeepin/dde-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iw.go
133 lines (114 loc) · 2.96 KB
/
iw.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
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
/*
* Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package iw
// #cgo CFLAGS: -Wall -g
// #cgo pkg-config: libnl-3.0 libnl-genl-3.0
// #include <stdlib.h>
// #include "core.h"
import "C"
import (
"fmt"
"io/ioutil"
"strings"
"unsafe"
"pkg.deepin.io/lib/strv"
)
type WirelessInfo struct {
Wiphy string
HwAddress string
IFCModes []string
}
type WirelessInfos []*WirelessInfo
var wirelessSets = make(map[string][]string)
func ListWirelessInfo() (WirelessInfos, error) {
ret := C.wireless_info_query()
if int(ret) != 0 {
fmt.Println("Failed to query wireless info")
return nil, fmt.Errorf("Query wireless info failed")
}
var infos WirelessInfos
for phy, modes := range wirelessSets {
infos = append(infos, &WirelessInfo{
Wiphy: phy,
HwAddress: getHwAddressByFile(hwAddressFile(phy)),
IFCModes: modes,
})
}
return infos, nil
}
func (infos WirelessInfos) ListMiracastDevice() WirelessInfos {
var ret WirelessInfos
for _, info := range infos {
if !info.SupportedMiracast() {
continue
}
ret = append(ret, info)
}
return ret
}
func (infos WirelessInfos) ListHotspotDevice() WirelessInfos {
var ret WirelessInfos
for _, info := range infos {
if !info.SupportedHotspot() {
continue
}
ret = append(ret, info)
}
return ret
}
func (infos WirelessInfos) Get(address string) *WirelessInfo {
for _, info := range infos {
if strings.EqualFold(info.HwAddress, address) {
return info
}
}
return nil
}
func (info WirelessInfo) SupportedHotspot() bool {
return strv.Strv(info.IFCModes).Contains("AP")
}
func (info *WirelessInfo) SupportedMiracast() bool {
list := strv.Strv(info.IFCModes)
return list.Contains("P2P-client") &&
list.Contains("P2P-GO")
// list.Contains("P2P-device")
}
//export addWirelessInfo
func addWirelessInfo(cname, cmode *C.char) {
name := C.GoString(cname)
mode := C.GoString(cmode)
C.free(unsafe.Pointer(cmode))
modes, ok := wirelessSets[name]
if !ok {
wirelessSets[name] = []string{mode}
return
}
modes = append(modes, mode)
wirelessSets[name] = modes
}
func hwAddressFile(name string) string {
return "/sys/class/ieee80211/" + name + "/macaddress"
}
func getHwAddressByFile(file string) string {
contents, err := ioutil.ReadFile(file)
if err != nil {
return ""
}
return strings.TrimSpace(string(contents))
}